blob: a6f4c710d663a812869cafe897fe60179db0e830 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <pty.h>
62#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000063
bellarde57a8c02005-11-10 23:58:52 +000064#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000065#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000066#endif
67#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000068#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000071#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000072#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_icmp.h> // must come after ip.h
75#include <netinet/udp.h>
76#include <netinet/tcp.h>
77#include <net/if.h>
78#include <syslog.h>
79#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000080#endif
bellard7d3505c2004-05-12 19:32:15 +000081#endif
bellardec530c82006-04-25 22:36:06 +000082#endif
bellard67b915a2004-03-31 23:37:16 +000083
blueswir19892fbf2008-08-24 10:34:20 +000084#if defined(__OpenBSD__)
85#include <util.h>
86#endif
87
ths8a16d272008-07-19 09:56:24 +000088#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
bellard67b915a2004-03-31 23:37:16 +000092#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000093#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
95
bellard73332e52004-04-04 20:22:28 +000096#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020097#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020098#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000099int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
Stefan Weil59a36a22009-06-18 20:11:03 +0200102 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000103}
104#undef main
105#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000106#endif
bellard73332e52004-04-04 20:22:28 +0000107#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000108
bellard5b0753e2005-03-01 21:37:28 +0000109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
Anthony Liguori69e5bb62011-08-22 08:12:52 -0500114#include <glib.h>
115
blueswir1511d2b12009-03-07 15:32:56 +0000116#include "hw/hw.h"
117#include "hw/boards.h"
118#include "hw/usb.h"
119#include "hw/pcmcia.h"
120#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000121#include "hw/isa.h"
122#include "hw/baum.h"
123#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100124#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000125#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000126#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200127#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200128#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000129#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000131#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000132#include "monitor.h"
133#include "console.h"
134#include "sysemu.h"
135#include "gdbstub.h"
136#include "qemu-timer.h"
137#include "qemu-char.h"
138#include "cache-utils.h"
139#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200140#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200141#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000142#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000143#include "audio/audio.h"
144#include "migration.h"
145#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200146#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200147#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200148#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200149#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700150#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530151#include "fsdev/qemu-fsdev.h"
152#endif
blueswir1511d2b12009-03-07 15:32:56 +0000153
bellard0824d6f2003-06-24 13:42:40 +0000154#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000155
blueswir1511d2b12009-03-07 15:32:56 +0000156#include "qemu_socket.h"
157
Jan Kiszkad918f232009-06-24 14:42:30 +0200158#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000159
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000160#include "trace.h"
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000161#include "simpletrace.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000162#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000163#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000164#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000165
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300166#include "ui/qemu-spice.h"
167
blueswir19dc63a12008-10-04 07:25:46 +0000168//#define DEBUG_NET
169//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000170
bellard1bfe8562004-07-08 21:17:50 +0000171#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000172
Amit Shah98b19252010-01-20 00:36:52 +0530173#define MAX_VIRTIO_CONSOLES 1
174
Paul Brook5cea8592009-05-30 00:52:44 +0100175static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000176const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000177enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500178DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200179int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000180const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500181ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000186int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000187NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000188int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200189int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530190int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000191static int rtc_utc = 1;
192static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200193QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100194int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000197static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000198#endif
ths667acca2006-12-11 02:08:05 +0000199int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000200CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000201CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000202CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000203int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000204int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000205int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000206int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000207int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200208int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200209int smp_cores = 1;
210int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100211#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000212const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100213#endif
bellard6515b202006-05-03 22:02:44 +0000214int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000215int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000216int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000217int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000218int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000219int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000220int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200221uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200222const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200223QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000224int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000225int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000226int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000227const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000228int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500229int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000230unsigned int nb_prom_envs = 0;
231const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200232int boot_menu;
wayne3d3b8302011-07-27 18:04:55 +0800233uint8_t *boot_splash_filedata;
234int boot_splash_filedata_size;
235uint8_t qemu_extra_params_fw[2];
bellard0824d6f2003-06-24 13:42:40 +0000236
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200237typedef struct FWBootEntry FWBootEntry;
238
239struct FWBootEntry {
240 QTAILQ_ENTRY(FWBootEntry) link;
241 int32_t bootindex;
242 DeviceState *dev;
243 char *suffix;
244};
245
246QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
247
aliguori268a3622009-04-21 22:30:27 +0000248int nb_numa_nodes;
249uint64_t node_mem[MAX_NODES];
250uint64_t node_cpumask[MAX_NODES];
251
blueswir19043b622009-01-21 19:28:13 +0000252static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000253
blueswir18fcb1b92008-09-18 18:29:08 +0000254uint8_t qemu_uuid[16];
255
Jan Kiszka76e30d02009-07-02 00:19:02 +0200256static QEMUBootSetHandler *boot_set_handler;
257static void *boot_set_opaque;
258
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200259static NotifierList exit_notifiers =
260 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
261
Gleb Natapov4cab9462010-12-08 13:35:08 +0200262static NotifierList machine_init_done_notifiers =
263 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
264
Anthony PERARD303d4e82010-09-21 20:05:31 +0100265static int tcg_allowed = 1;
Blue Swirld745bef2010-03-29 19:23:49 +0000266int kvm_allowed = 0;
Anthony PERARD3285cf42010-08-19 12:27:56 +0100267int xen_allowed = 0;
Blue Swirld745bef2010-03-29 19:23:49 +0000268uint32_t xen_domid;
269enum xen_mode xen_mode = XEN_EMULATE;
Jan Kiszkad5ab9712011-08-02 16:10:21 +0200270static int tcg_tb_size;
Blue Swirld745bef2010-03-29 19:23:49 +0000271
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100272static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100273static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100274static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100275static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100276static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100277static int default_floppy = 1;
278static int default_cdrom = 1;
279static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100280
281static struct {
282 const char *driver;
283 int *flag;
284} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100285 { .driver = "isa-serial", .flag = &default_serial },
286 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100287 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200288 { .driver = "ide-cd", .flag = &default_cdrom },
289 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100290 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200291 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530292 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
293 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
294 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100295 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100296 { .driver = "cirrus-vga", .flag = &default_vga },
297 { .driver = "vmware-svga", .flag = &default_vga },
Markus Armbruster0826c712011-06-07 10:34:30 +0200298 { .driver = "isa-vga", .flag = &default_vga },
Gerd Hoffmann42138042011-05-16 09:28:58 +0200299 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100300};
301
wayne3d3b8302011-07-27 18:04:55 +0800302static void res_free(void)
303{
304 if (boot_splash_filedata != NULL) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500305 g_free(boot_splash_filedata);
wayne3d3b8302011-07-27 18:04:55 +0800306 boot_splash_filedata = NULL;
307 }
308}
309
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100310static int default_driver_check(QemuOpts *opts, void *opaque)
311{
312 const char *driver = qemu_opt_get(opts, "driver");
313 int i;
314
315 if (!driver)
316 return 0;
317 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
318 if (strcmp(default_list[i].driver, driver) != 0)
319 continue;
320 *(default_list[i].flag) = 0;
321 }
322 return 0;
323}
324
bellard0824d6f2003-06-24 13:42:40 +0000325/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100326/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000327
bellardc4b1fcc2004-03-14 21:44:30 +0000328/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000329/* host time/date access */
330void qemu_get_timedate(struct tm *tm, int offset)
331{
332 time_t ti;
333 struct tm *ret;
334
335 time(&ti);
336 ti += offset;
337 if (rtc_date_offset == -1) {
338 if (rtc_utc)
339 ret = gmtime(&ti);
340 else
341 ret = localtime(&ti);
342 } else {
343 ti -= rtc_date_offset;
344 ret = gmtime(&ti);
345 }
346
347 memcpy(tm, ret, sizeof(struct tm));
348}
349
350int qemu_timedate_diff(struct tm *tm)
351{
352 time_t seconds;
353
354 if (rtc_date_offset == -1)
355 if (rtc_utc)
356 seconds = mktimegm(tm);
357 else
358 seconds = mktime(tm);
359 else
360 seconds = mktimegm(tm) + rtc_date_offset;
361
362 return seconds - time(NULL);
363}
364
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300365void rtc_change_mon_event(struct tm *tm)
366{
367 QObject *data;
368
369 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
370 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
371 qobject_decref(data);
372}
373
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200374static void configure_rtc_date_offset(const char *startdate, int legacy)
375{
376 time_t rtc_start_date;
377 struct tm tm;
378
379 if (!strcmp(startdate, "now") && legacy) {
380 rtc_date_offset = -1;
381 } else {
382 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
383 &tm.tm_year,
384 &tm.tm_mon,
385 &tm.tm_mday,
386 &tm.tm_hour,
387 &tm.tm_min,
388 &tm.tm_sec) == 6) {
389 /* OK */
390 } else if (sscanf(startdate, "%d-%d-%d",
391 &tm.tm_year,
392 &tm.tm_mon,
393 &tm.tm_mday) == 3) {
394 tm.tm_hour = 0;
395 tm.tm_min = 0;
396 tm.tm_sec = 0;
397 } else {
398 goto date_fail;
399 }
400 tm.tm_year -= 1900;
401 tm.tm_mon--;
402 rtc_start_date = mktimegm(&tm);
403 if (rtc_start_date == -1) {
404 date_fail:
405 fprintf(stderr, "Invalid date format. Valid formats are:\n"
406 "'2006-06-17T16:01:21' or '2006-06-17'\n");
407 exit(1);
408 }
409 rtc_date_offset = time(NULL) - rtc_start_date;
410 }
411}
412
413static void configure_rtc(QemuOpts *opts)
414{
415 const char *value;
416
417 value = qemu_opt_get(opts, "base");
418 if (value) {
419 if (!strcmp(value, "utc")) {
420 rtc_utc = 1;
421 } else if (!strcmp(value, "localtime")) {
422 rtc_utc = 0;
423 } else {
424 configure_rtc_date_offset(value, 0);
425 }
426 }
Jan Kiszka68752042009-09-15 13:36:04 +0200427 value = qemu_opt_get(opts, "clock");
428 if (value) {
429 if (!strcmp(value, "host")) {
430 rtc_clock = host_clock;
431 } else if (!strcmp(value, "vm")) {
432 rtc_clock = vm_clock;
433 } else {
434 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
435 exit(1);
436 }
437 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200438 value = qemu_opt_get(opts, "driftfix");
439 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000440 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200441 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000442 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200443 rtc_td_hack = 0;
444 } else {
445 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
446 exit(1);
447 }
448 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200449}
450
balrog1ae26a12008-09-28 23:19:47 +0000451/***********************************************************/
452/* Bluetooth support */
453static int nb_hcis;
454static int cur_hci;
455static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000456
balrog1ae26a12008-09-28 23:19:47 +0000457static struct bt_vlan_s {
458 struct bt_scatternet_s net;
459 int id;
460 struct bt_vlan_s *next;
461} *first_bt_vlan;
462
463/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000464static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000465{
466 struct bt_vlan_s **pvlan, *vlan;
467 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
468 if (vlan->id == id)
469 return &vlan->net;
470 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500471 vlan = g_malloc0(sizeof(struct bt_vlan_s));
balrog1ae26a12008-09-28 23:19:47 +0000472 vlan->id = id;
473 pvlan = &first_bt_vlan;
474 while (*pvlan != NULL)
475 pvlan = &(*pvlan)->next;
476 *pvlan = vlan;
477 return &vlan->net;
478}
479
480static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
481{
482}
483
484static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
485{
486 return -ENOTSUP;
487}
488
489static struct HCIInfo null_hci = {
490 .cmd_send = null_hci_send,
491 .sco_send = null_hci_send,
492 .acl_send = null_hci_send,
493 .bdaddr_set = null_hci_addr_set,
494};
495
496struct HCIInfo *qemu_next_hci(void)
497{
498 if (cur_hci == nb_hcis)
499 return &null_hci;
500
501 return hci_table[cur_hci++];
502}
503
balrogdc72ac12008-11-09 00:04:26 +0000504static struct HCIInfo *hci_init(const char *str)
505{
506 char *endp;
507 struct bt_scatternet_s *vlan = 0;
508
509 if (!strcmp(str, "null"))
510 /* null */
511 return &null_hci;
512 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
513 /* host[:hciN] */
514 return bt_host_hci(str[4] ? str + 5 : "hci0");
515 else if (!strncmp(str, "hci", 3)) {
516 /* hci[,vlan=n] */
517 if (str[3]) {
518 if (!strncmp(str + 3, ",vlan=", 6)) {
519 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
520 if (*endp)
521 vlan = 0;
522 }
523 } else
524 vlan = qemu_find_bt_vlan(0);
525 if (vlan)
526 return bt_new_hci(vlan);
527 }
528
529 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
530
531 return 0;
532}
533
534static int bt_hci_parse(const char *str)
535{
536 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500537 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000538
539 if (nb_hcis >= MAX_NICS) {
540 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
541 return -1;
542 }
543
544 hci = hci_init(str);
545 if (!hci)
546 return -1;
547
548 bdaddr.b[0] = 0x52;
549 bdaddr.b[1] = 0x54;
550 bdaddr.b[2] = 0x00;
551 bdaddr.b[3] = 0x12;
552 bdaddr.b[4] = 0x34;
553 bdaddr.b[5] = 0x56 + nb_hcis;
554 hci->bdaddr_set(hci, bdaddr.b);
555
556 hci_table[nb_hcis++] = hci;
557
558 return 0;
559}
560
561static void bt_vhci_add(int vlan_id)
562{
563 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
564
565 if (!vlan->slave)
566 fprintf(stderr, "qemu: warning: adding a VHCI to "
567 "an empty scatternet %i\n", vlan_id);
568
569 bt_vhci_init(bt_new_hci(vlan));
570}
571
572static struct bt_device_s *bt_device_add(const char *opt)
573{
574 struct bt_scatternet_s *vlan;
575 int vlan_id = 0;
576 char *endp = strstr(opt, ",vlan=");
577 int len = (endp ? endp - opt : strlen(opt)) + 1;
578 char devname[10];
579
580 pstrcpy(devname, MIN(sizeof(devname), len), opt);
581
582 if (endp) {
583 vlan_id = strtol(endp + 6, &endp, 0);
584 if (*endp) {
585 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
586 return 0;
587 }
588 }
589
590 vlan = qemu_find_bt_vlan(vlan_id);
591
592 if (!vlan->slave)
593 fprintf(stderr, "qemu: warning: adding a slave device to "
594 "an empty scatternet %i\n", vlan_id);
595
596 if (!strcmp(devname, "keyboard"))
597 return bt_keyboard_init(vlan);
598
599 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
600 return 0;
601}
602
603static int bt_parse(const char *opt)
604{
605 const char *endp, *p;
606 int vlan;
607
608 if (strstart(opt, "hci", &endp)) {
609 if (!*endp || *endp == ',') {
610 if (*endp)
611 if (!strstart(endp, ",vlan=", 0))
612 opt = endp + 1;
613
614 return bt_hci_parse(opt);
615 }
616 } else if (strstart(opt, "vhci", &endp)) {
617 if (!*endp || *endp == ',') {
618 if (*endp) {
619 if (strstart(endp, ",vlan=", &p)) {
620 vlan = strtol(p, (char **) &endp, 0);
621 if (*endp) {
622 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
623 return 1;
624 }
625 } else {
626 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
627 return 1;
628 }
629 } else
630 vlan = 0;
631
632 bt_vhci_add(vlan);
633 return 0;
634 }
635 } else if (strstart(opt, "device:", &endp))
636 return !bt_device_add(endp);
637
638 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
639 return 1;
640}
641
balrog1ae26a12008-09-28 23:19:47 +0000642/***********************************************************/
643/* QEMU Block devices */
644
Markus Armbruster2292dda2011-01-28 11:21:41 +0100645#define HD_OPTS "media=disk"
646#define CDROM_OPTS "media=cdrom"
647#define FD_OPTS ""
648#define PFLASH_OPTS ""
649#define MTD_OPTS ""
650#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000651
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200652static int drive_init_func(QemuOpts *opts, void *opaque)
653{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200654 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200655
Markus Armbruster319ae522011-01-28 11:21:46 +0100656 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200657}
658
659static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
660{
661 if (NULL == qemu_opt_get(opts, "snapshot")) {
662 qemu_opt_set(opts, "snapshot", "on");
663 }
664 return 0;
665}
666
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100667static void default_drive(int enable, int snapshot, int use_scsi,
668 BlockInterfaceType type, int index,
669 const char *optstr)
670{
671 QemuOpts *opts;
672
673 if (type == IF_DEFAULT) {
674 type = use_scsi ? IF_SCSI : IF_IDE;
675 }
676
677 if (!enable || drive_get_by_index(type, index)) {
678 return;
679 }
680
681 opts = drive_add(type, index, NULL, optstr);
682 if (snapshot) {
683 drive_enable_snapshot(opts, NULL);
684 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100685 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100686 exit(1);
687 }
688}
689
Jan Kiszka76e30d02009-07-02 00:19:02 +0200690void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
691{
692 boot_set_handler = func;
693 boot_set_opaque = opaque;
694}
695
696int qemu_boot_set(const char *boot_devices)
697{
698 if (!boot_set_handler) {
699 return -EINVAL;
700 }
701 return boot_set_handler(boot_set_opaque, boot_devices);
702}
703
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300704static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200705{
706 /* We just do some generic consistency checks */
707 const char *p;
708 int bitmap = 0;
709
710 for (p = devices; *p != '\0'; p++) {
711 /* Allowed boot devices are:
712 * a-b: floppy disk drives
713 * c-f: IDE disk drives
714 * g-m: machine implementation dependant drives
715 * n-p: network devices
716 * It's up to each machine implementation to check if the given boot
717 * devices match the actual hardware implementation and firmware
718 * features.
719 */
720 if (*p < 'a' || *p > 'p') {
721 fprintf(stderr, "Invalid boot device '%c'\n", *p);
722 exit(1);
723 }
724 if (bitmap & (1 << (*p - 'a'))) {
725 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
726 exit(1);
727 }
728 bitmap |= 1 << (*p - 'a');
729 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200730}
731
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200732static void restore_boot_devices(void *opaque)
733{
734 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600735 static int first = 1;
736
737 /* Restore boot order and remove ourselves after the first boot */
738 if (first) {
739 first = 0;
740 return;
741 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200742
743 qemu_boot_set(standard_boot_devices);
744
745 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
Anthony Liguori7267c092011-08-20 22:09:37 -0500746 g_free(standard_boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200747}
748
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200749void add_boot_device_path(int32_t bootindex, DeviceState *dev,
750 const char *suffix)
751{
752 FWBootEntry *node, *i;
753
754 if (bootindex < 0) {
755 return;
756 }
757
758 assert(dev != NULL || suffix != NULL);
759
Anthony Liguori7267c092011-08-20 22:09:37 -0500760 node = g_malloc0(sizeof(FWBootEntry));
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200761 node->bootindex = bootindex;
Anthony Liguori7267c092011-08-20 22:09:37 -0500762 node->suffix = suffix ? g_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200763 node->dev = dev;
764
765 QTAILQ_FOREACH(i, &fw_boot_order, link) {
766 if (i->bootindex == bootindex) {
767 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
768 exit(1);
769 } else if (i->bootindex < bootindex) {
770 continue;
771 }
772 QTAILQ_INSERT_BEFORE(i, node, link);
773 return;
774 }
775 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
776}
777
Gleb Natapov962630f2010-12-08 13:35:09 +0200778/*
779 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +1000780 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +0200781 *
782 * memory pointed by "size" is assigned total length of the array in bytes
783 *
784 */
785char *get_boot_devices_list(uint32_t *size)
786{
787 FWBootEntry *i;
788 uint32_t total = 0;
789 char *list = NULL;
790
791 QTAILQ_FOREACH(i, &fw_boot_order, link) {
792 char *devpath = NULL, *bootpath;
793 int len;
794
795 if (i->dev) {
796 devpath = qdev_get_fw_dev_path(i->dev);
797 assert(devpath);
798 }
799
800 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +0000801 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
802
Anthony Liguori7267c092011-08-20 22:09:37 -0500803 bootpath = g_malloc(bootpathlen);
Blue Swirl4fd37a92010-12-19 14:05:43 +0000804 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Anthony Liguori7267c092011-08-20 22:09:37 -0500805 g_free(devpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200806 } else if (devpath) {
807 bootpath = devpath;
808 } else {
Anthony Liguori7267c092011-08-20 22:09:37 -0500809 bootpath = g_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200810 assert(bootpath);
811 }
812
813 if (total) {
814 list[total-1] = '\n';
815 }
816 len = strlen(bootpath) + 1;
Anthony Liguori7267c092011-08-20 22:09:37 -0500817 list = g_realloc(list, total + len);
Gleb Natapov962630f2010-12-08 13:35:09 +0200818 memcpy(&list[total], bootpath, len);
819 total += len;
Anthony Liguori7267c092011-08-20 22:09:37 -0500820 g_free(bootpath);
Gleb Natapov962630f2010-12-08 13:35:09 +0200821 }
822
823 *size = total;
824
825 return list;
826}
827
aliguori268a3622009-04-21 22:30:27 +0000828static void numa_add(const char *optarg)
829{
830 char option[128];
831 char *endptr;
832 unsigned long long value, endvalue;
833 int nodenr;
834
835 optarg = get_opt_name(option, 128, optarg, ',') + 1;
836 if (!strcmp(option, "node")) {
837 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
838 nodenr = nb_numa_nodes;
839 } else {
840 nodenr = strtoull(option, NULL, 10);
841 }
842
843 if (get_param_value(option, 128, "mem", optarg) == 0) {
844 node_mem[nodenr] = 0;
845 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100846 int64_t sval;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200847 sval = strtosz(option, NULL);
848 if (sval < 0) {
849 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
850 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000851 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200852 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000853 }
854 if (get_param_value(option, 128, "cpus", optarg) == 0) {
855 node_cpumask[nodenr] = 0;
856 } else {
857 value = strtoull(option, &endptr, 10);
858 if (value >= 64) {
859 value = 63;
860 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
861 } else {
862 if (*endptr == '-') {
863 endvalue = strtoull(endptr+1, &endptr, 10);
864 if (endvalue >= 63) {
865 endvalue = 62;
866 fprintf(stderr,
867 "only 63 CPUs in NUMA mode supported.\n");
868 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100869 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000870 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100871 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000872 }
873 }
874 node_cpumask[nodenr] = value;
875 }
876 nb_numa_nodes++;
877 }
878 return;
879}
880
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200881static void smp_parse(const char *optarg)
882{
883 int smp, sockets = 0, threads = 0, cores = 0;
884 char *endptr;
885 char option[128];
886
887 smp = strtoul(optarg, &endptr, 10);
888 if (endptr != optarg) {
889 if (*endptr == ',') {
890 endptr++;
891 }
892 }
893 if (get_param_value(option, 128, "sockets", endptr) != 0)
894 sockets = strtoull(option, NULL, 10);
895 if (get_param_value(option, 128, "cores", endptr) != 0)
896 cores = strtoull(option, NULL, 10);
897 if (get_param_value(option, 128, "threads", endptr) != 0)
898 threads = strtoull(option, NULL, 10);
899 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
900 max_cpus = strtoull(option, NULL, 10);
901
902 /* compute missing values, prefer sockets over cores over threads */
903 if (smp == 0 || sockets == 0) {
904 sockets = sockets > 0 ? sockets : 1;
905 cores = cores > 0 ? cores : 1;
906 threads = threads > 0 ? threads : 1;
907 if (smp == 0) {
908 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200909 }
910 } else {
911 if (cores == 0) {
912 threads = threads > 0 ? threads : 1;
913 cores = smp / (sockets * threads);
914 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500915 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200916 }
917 }
918 smp_cpus = smp;
919 smp_cores = cores > 0 ? cores : 1;
920 smp_threads = threads > 0 ? threads : 1;
921 if (max_cpus == 0)
922 max_cpus = smp_cpus;
923}
924
bellard330d0412003-07-26 18:11:40 +0000925/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000926/* USB devices */
927
Markus Armbrusterfb080002010-05-28 15:38:44 +0200928static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000929{
930 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200931 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000932
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200933 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000934 return -1;
935
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100936 /* drivers with .usbdevice_name entry in USBDeviceInfo */
937 dev = usbdevice_create(devname);
938 if (dev)
939 goto done;
940
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200941 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +0200942#ifndef CONFIG_LINUX
943 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +0000944 if (strstart(devname, "host:", &p)) {
945 dev = usb_host_device_open(p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +0200946 } else
947#endif
948 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
balrogdc72ac12008-11-09 00:04:26 +0000949 dev = usb_bt_init(devname[2] ? hci_init(p) :
950 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000951 } else {
952 return -1;
953 }
pbrook0d92ed32006-05-21 16:30:15 +0000954 if (!dev)
955 return -1;
956
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200957done:
bellarda594cfb2005-11-06 16:13:29 +0000958 return 0;
959}
960
aliguori1f3870a2008-08-21 19:27:48 +0000961static int usb_device_del(const char *devname)
962{
963 int bus_num, addr;
964 const char *p;
965
aliguori5d0c5752008-09-14 01:07:41 +0000966 if (strstart(devname, "host:", &p))
967 return usb_host_device_close(p);
968
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200969 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000970 return -1;
971
972 p = strchr(devname, '.');
973 if (!p)
974 return -1;
975 bus_num = strtoul(devname, NULL, 0);
976 addr = strtoul(p + 1, NULL, 0);
977
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200978 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000979}
980
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200981static int usb_parse(const char *cmdline)
982{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800983 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200984 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800985 if (r < 0) {
986 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
987 }
988 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200989}
990
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300991void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000992{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800993 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200994 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100995 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800996 }
bellarda594cfb2005-11-06 16:13:29 +0000997}
998
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300999void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001000{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001001 const char *devname = qdict_get_str(qdict, "devname");
1002 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001003 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001004 }
bellarda594cfb2005-11-06 16:13:29 +00001005}
1006
bellardf7cce892004-12-08 22:21:25 +00001007/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001008/* PCMCIA/Cardbus */
1009
1010static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001011 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001012 struct pcmcia_socket_entry_s *next;
1013} *pcmcia_sockets = 0;
1014
Paul Brookbc24a222009-05-10 01:44:56 +01001015void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001016{
1017 struct pcmcia_socket_entry_s *entry;
1018
Anthony Liguori7267c092011-08-20 22:09:37 -05001019 entry = g_malloc(sizeof(struct pcmcia_socket_entry_s));
balrog201a51f2007-04-30 00:51:09 +00001020 entry->socket = socket;
1021 entry->next = pcmcia_sockets;
1022 pcmcia_sockets = entry;
1023}
1024
Paul Brookbc24a222009-05-10 01:44:56 +01001025void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001026{
1027 struct pcmcia_socket_entry_s *entry, **ptr;
1028
1029 ptr = &pcmcia_sockets;
1030 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1031 if (entry->socket == socket) {
1032 *ptr = entry->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001033 g_free(entry);
balrog201a51f2007-04-30 00:51:09 +00001034 }
1035}
1036
aliguori376253e2009-03-05 23:01:23 +00001037void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001038{
1039 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001040
balrog201a51f2007-04-30 00:51:09 +00001041 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001042 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001043
1044 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001045 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1046 iter->socket->attached ? iter->socket->card_string :
1047 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001048}
1049
1050/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001051/* machine registration */
1052
blueswir1bdaf78e2008-10-04 07:24:27 +00001053static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001054QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001055
1056int qemu_register_machine(QEMUMachine *m)
1057{
1058 QEMUMachine **pm;
1059 pm = &first_machine;
1060 while (*pm != NULL)
1061 pm = &(*pm)->next;
1062 m->next = NULL;
1063 *pm = m;
1064 return 0;
1065}
1066
pbrook9596ebb2007-11-18 01:44:38 +00001067static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001068{
1069 QEMUMachine *m;
1070
1071 for(m = first_machine; m != NULL; m = m->next) {
1072 if (!strcmp(m->name, name))
1073 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001074 if (m->alias && !strcmp(m->alias, name))
1075 return m;
bellardcc1daa42005-06-05 14:49:17 +00001076 }
1077 return NULL;
1078}
1079
Anthony Liguori0c257432009-05-21 20:41:01 -05001080static QEMUMachine *find_default_machine(void)
1081{
1082 QEMUMachine *m;
1083
1084 for(m = first_machine; m != NULL; m = m->next) {
1085 if (m->is_default) {
1086 return m;
1087 }
1088 }
1089 return NULL;
1090}
1091
bellardcc1daa42005-06-05 14:49:17 +00001092/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001093/* main execution loop */
1094
pbrook9596ebb2007-11-18 01:44:38 +00001095static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001096{
aliguori7d957bd2009-01-15 22:14:11 +00001097 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001098 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001099 DisplayChangeListener *dcl = ds->listeners;
1100
Sheng Yang62a27442010-01-26 19:21:16 +08001101 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001102 dpy_refresh(ds);
1103
1104 while (dcl != NULL) {
1105 if (dcl->gui_timer_interval &&
1106 dcl->gui_timer_interval < interval)
1107 interval = dcl->gui_timer_interval;
1108 dcl = dcl->next;
1109 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001110 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001111}
1112
blueswir19043b622009-01-21 19:28:13 +00001113static void nographic_update(void *opaque)
1114{
1115 uint64_t interval = GUI_REFRESH_INTERVAL;
1116
Sheng Yang62a27442010-01-26 19:21:16 +08001117 qemu_flush_coalesced_mmio_buffer();
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001118 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00001119}
1120
bellard0bd48852005-11-11 00:00:47 +00001121struct vm_change_state_entry {
1122 VMChangeStateHandler *cb;
1123 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001124 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001125};
1126
Blue Swirl72cf2d42009-09-12 07:36:22 +00001127static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001128
1129VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1130 void *opaque)
1131{
1132 VMChangeStateEntry *e;
1133
Anthony Liguori7267c092011-08-20 22:09:37 -05001134 e = g_malloc0(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001135
1136 e->cb = cb;
1137 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001138 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001139 return e;
1140}
1141
1142void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1143{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001144 QLIST_REMOVE (e, entries);
Anthony Liguori7267c092011-08-20 22:09:37 -05001145 g_free (e);
bellard0bd48852005-11-11 00:00:47 +00001146}
1147
Blue Swirl296af7c2010-03-29 19:23:50 +00001148void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001149{
1150 VMChangeStateEntry *e;
1151
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001152 trace_vm_state_notify(running, reason);
1153
bellard0bd48852005-11-11 00:00:47 +00001154 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001155 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001156 }
1157}
1158
bellard8a7ddc32004-03-31 19:00:16 +00001159void vm_start(void)
1160{
1161 if (!vm_running) {
1162 cpu_enable_ticks();
1163 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001164 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001165 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001166 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001167 }
1168}
1169
bellardbb0c6722004-06-20 12:37:32 +00001170/* reset/shutdown handler */
1171
1172typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001173 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001174 QEMUResetHandler *func;
1175 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001176} QEMUResetEntry;
1177
Blue Swirl72cf2d42009-09-12 07:36:22 +00001178static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1179 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001180static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001181static int shutdown_requested, shutdown_signal = -1;
1182static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001183static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001184static int debug_requested;
1185static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001186
Anthony PERARD1291eb32010-07-22 15:52:48 +01001187int qemu_shutdown_requested_get(void)
1188{
1189 return shutdown_requested;
1190}
1191
1192int qemu_reset_requested_get(void)
1193{
1194 return reset_requested;
1195}
1196
aurel32cf7a2fe2008-03-18 06:53:05 +00001197int qemu_shutdown_requested(void)
1198{
1199 int r = shutdown_requested;
1200 shutdown_requested = 0;
1201 return r;
1202}
1203
Gleb Natapovf64622c2011-03-15 13:56:04 +02001204void qemu_kill_report(void)
1205{
1206 if (shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001207 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1208 if (shutdown_pid == 0) {
1209 /* This happens for eg ^C at the terminal, so it's worth
1210 * avoiding printing an odd message in that case.
1211 */
1212 fputc('\n', stderr);
1213 } else {
Andreas Färber953ffe02011-06-02 19:58:06 +02001214 fprintf(stderr, " from pid " FMT_pid "\n", shutdown_pid);
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001215 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001216 shutdown_signal = -1;
1217 }
1218}
1219
aurel32cf7a2fe2008-03-18 06:53:05 +00001220int qemu_reset_requested(void)
1221{
1222 int r = reset_requested;
1223 reset_requested = 0;
1224 return r;
1225}
1226
1227int qemu_powerdown_requested(void)
1228{
1229 int r = powerdown_requested;
1230 powerdown_requested = 0;
1231 return r;
1232}
1233
aliguorie5689022009-04-24 18:03:54 +00001234static int qemu_debug_requested(void)
1235{
1236 int r = debug_requested;
1237 debug_requested = 0;
1238 return r;
1239}
1240
aliguori6e29f5d2009-04-24 18:04:02 +00001241static int qemu_vmstop_requested(void)
1242{
1243 int r = vmstop_requested;
1244 vmstop_requested = 0;
1245 return r;
1246}
1247
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001248void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001249{
Anthony Liguori7267c092011-08-20 22:09:37 -05001250 QEMUResetEntry *re = g_malloc0(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001251
bellardbb0c6722004-06-20 12:37:32 +00001252 re->func = func;
1253 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001254 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001255}
1256
Jan Kiszkadda9b292009-07-02 00:19:02 +02001257void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001258{
1259 QEMUResetEntry *re;
1260
Blue Swirl72cf2d42009-09-12 07:36:22 +00001261 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001262 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001263 QTAILQ_REMOVE(&reset_handlers, re, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001264 g_free(re);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001265 return;
1266 }
1267 }
1268}
1269
Jan Kiszkae063eb12011-06-14 18:29:43 +02001270void qemu_system_reset(bool report)
Jan Kiszkadda9b292009-07-02 00:19:02 +02001271{
1272 QEMUResetEntry *re, *nre;
1273
1274 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001275 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001276 re->func(re->opaque);
1277 }
Jan Kiszkae063eb12011-06-14 18:29:43 +02001278 if (report) {
1279 monitor_protocol_event(QEVENT_RESET, NULL);
1280 }
Jan Kiszkaea375f92010-03-01 19:10:30 +01001281 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001282}
1283
1284void qemu_system_reset_request(void)
1285{
bellardd1beab82006-10-02 19:44:22 +00001286 if (no_reboot) {
1287 shutdown_requested = 1;
1288 } else {
1289 reset_requested = 1;
1290 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001291 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001292 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001293}
1294
Gleb Natapovf64622c2011-03-15 13:56:04 +02001295void qemu_system_killed(int signal, pid_t pid)
1296{
1297 shutdown_signal = signal;
1298 shutdown_pid = pid;
1299 qemu_system_shutdown_request();
1300}
1301
bellardbb0c6722004-06-20 12:37:32 +00001302void qemu_system_shutdown_request(void)
1303{
1304 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001305 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001306}
1307
bellard34751872005-07-02 14:31:34 +00001308void qemu_system_powerdown_request(void)
1309{
1310 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001311 qemu_notify_event();
1312}
1313
Jan Kiszka8cf71712011-02-07 12:19:16 +01001314void qemu_system_debug_request(void)
1315{
1316 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001317 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001318}
1319
1320void qemu_system_vmstop_request(int reason)
1321{
1322 vmstop_requested = reason;
1323 qemu_notify_event();
1324}
1325
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001326static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
1327static int n_poll_fds;
1328static int max_priority;
1329
1330static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
1331 fd_set *xfds, struct timeval *tv)
1332{
1333 GMainContext *context = g_main_context_default();
1334 int i;
1335 int timeout = 0, cur_timeout;
1336
1337 g_main_context_prepare(context, &max_priority);
1338
1339 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
1340 poll_fds, ARRAY_SIZE(poll_fds));
1341 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
1342
1343 for (i = 0; i < n_poll_fds; i++) {
1344 GPollFD *p = &poll_fds[i];
1345
1346 if ((p->events & G_IO_IN)) {
1347 FD_SET(p->fd, rfds);
1348 *max_fd = MAX(*max_fd, p->fd);
1349 }
1350 if ((p->events & G_IO_OUT)) {
1351 FD_SET(p->fd, wfds);
1352 *max_fd = MAX(*max_fd, p->fd);
1353 }
1354 if ((p->events & G_IO_ERR)) {
1355 FD_SET(p->fd, xfds);
1356 *max_fd = MAX(*max_fd, p->fd);
1357 }
1358 }
1359
1360 cur_timeout = (tv->tv_sec * 1000) + ((tv->tv_usec + 500) / 1000);
1361 if (timeout >= 0 && timeout < cur_timeout) {
1362 tv->tv_sec = timeout / 1000;
1363 tv->tv_usec = (timeout % 1000) * 1000;
1364 }
1365}
1366
1367static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
1368 bool err)
1369{
1370 GMainContext *context = g_main_context_default();
1371
1372 if (!err) {
1373 int i;
1374
1375 for (i = 0; i < n_poll_fds; i++) {
1376 GPollFD *p = &poll_fds[i];
1377
1378 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
1379 p->revents |= G_IO_IN;
1380 }
1381 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
1382 p->revents |= G_IO_OUT;
1383 }
1384 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
1385 p->revents |= G_IO_ERR;
1386 }
1387 }
1388 }
1389
1390 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
1391 g_main_context_dispatch(context);
1392 }
1393}
1394
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001395int main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001396{
aliguori56f3a5d2008-10-31 18:07:17 +00001397 fd_set rfds, wfds, xfds;
1398 int ret, nfds;
1399 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001400 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001401
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001402 if (nonblocking)
1403 timeout = 0;
1404 else {
1405 timeout = qemu_calculate_timeout();
1406 qemu_bh_update_timeout(&timeout);
1407 }
aliguori56f3a5d2008-10-31 18:07:17 +00001408
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001409 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001410
Paolo Bonzini02981412011-03-09 18:21:09 +01001411 tv.tv_sec = timeout / 1000;
1412 tv.tv_usec = (timeout % 1000) * 1000;
1413
bellardfd1dff42006-02-01 21:29:26 +00001414 /* poll any events */
1415 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001416 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001417 FD_ZERO(&rfds);
1418 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001419 FD_ZERO(&xfds);
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001420
Paolo Bonzini02981412011-03-09 18:21:09 +01001421 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
Jan Kiszkad918f232009-06-24 14:42:30 +02001422 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001423 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &tv);
Jan Kiszkad918f232009-06-24 14:42:30 +02001424
Jan Kiszka200668b2011-08-22 17:46:01 +02001425 if (timeout > 0) {
1426 qemu_mutex_unlock_iothread();
1427 }
1428
bellarde0356492006-05-01 13:33:02 +00001429 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
Jan Kiszka200668b2011-08-22 17:46:01 +02001430
1431 if (timeout > 0) {
1432 qemu_mutex_lock_iothread();
1433 }
thscafffd42007-02-28 21:59:44 +00001434
Paolo Bonzini02981412011-03-09 18:21:09 +01001435 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
Jan Kiszkad918f232009-06-24 14:42:30 +02001436 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
Anthony Liguori69e5bb62011-08-22 08:12:52 -05001437 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001438
Paolo Bonzinib6964822010-03-10 11:38:45 +01001439 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001440
pbrook423f0742007-05-23 00:06:54 +00001441 /* Check bottom-halves last in case any of the earlier events triggered
1442 them. */
1443 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001444
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001445 return ret;
bellard5905b2e2004-08-01 21:53:26 +00001446}
1447
Jan Kiszka46481d32011-02-01 22:15:47 +01001448#ifndef CONFIG_IOTHREAD
1449static int vm_request_pending(void)
aliguori43b96852009-04-24 18:03:33 +00001450{
Jan Kiszka46481d32011-02-01 22:15:47 +01001451 return powerdown_requested ||
1452 reset_requested ||
1453 shutdown_requested ||
1454 debug_requested ||
1455 vmstop_requested;
aliguori43b96852009-04-24 18:03:33 +00001456}
Jan Kiszka46481d32011-02-01 22:15:47 +01001457#endif
aliguori43b96852009-04-24 18:03:33 +00001458
Blue Swirld9c32312009-08-09 08:42:19 +00001459qemu_irq qemu_system_powerdown;
1460
aliguori43b96852009-04-24 18:03:33 +00001461static void main_loop(void)
1462{
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001463 bool nonblocking;
1464 int last_io __attribute__ ((unused)) = 0;
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001465#ifdef CONFIG_PROFILER
1466 int64_t ti;
1467#endif
aliguori6e29f5d2009-04-24 18:04:02 +00001468 int r;
aliguori43b96852009-04-24 18:03:33 +00001469
Blue Swirl7277e022010-04-12 17:19:06 +00001470 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001471
aliguori6e29f5d2009-04-24 18:04:02 +00001472 for (;;) {
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001473#ifdef CONFIG_IOTHREAD
1474 nonblocking = !kvm_enabled() && last_io > 0;
1475#else
Jan Kiszka46481d32011-02-01 22:15:47 +01001476 nonblocking = cpu_exec_all();
1477 if (vm_request_pending()) {
1478 nonblocking = true;
1479 }
aliguorid6dc3d42009-04-24 18:04:07 +00001480#endif
aliguori43b96852009-04-24 18:03:33 +00001481#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001482 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001483#endif
Jan Kiszkac9f711a2011-08-22 17:46:02 +02001484 last_io = main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001485#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001486 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001487#endif
aliguori43b96852009-04-24 18:03:33 +00001488
Jan Kiszka8cf71712011-02-07 12:19:16 +01001489 if (qemu_debug_requested()) {
1490 vm_stop(VMSTOP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001491 }
aliguori43b96852009-04-24 18:03:33 +00001492 if (qemu_shutdown_requested()) {
Gleb Natapovf64622c2011-03-15 13:56:04 +02001493 qemu_kill_report();
Blue Swirl242cd002009-12-04 18:05:45 +00001494 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001495 if (no_shutdown) {
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001496 vm_stop(VMSTOP_SHUTDOWN);
aliguori43b96852009-04-24 18:03:33 +00001497 } else
1498 break;
1499 }
aliguorid6dc3d42009-04-24 18:04:07 +00001500 if (qemu_reset_requested()) {
1501 pause_all_vcpus();
Jan Kiszkaa7ada152011-03-02 08:56:11 +01001502 cpu_synchronize_all_states();
Jan Kiszkae063eb12011-06-14 18:29:43 +02001503 qemu_system_reset(VMRESET_REPORT);
aliguorid6dc3d42009-04-24 18:04:07 +00001504 resume_all_vcpus();
1505 }
Blue Swirld9c32312009-08-09 08:42:19 +00001506 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001507 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001508 qemu_irq_raise(qemu_system_powerdown);
1509 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001510 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001511 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001512 }
aliguori43b96852009-04-24 18:03:33 +00001513 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001514 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001515 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001516}
1517
pbrook9bd7e6d2009-04-07 22:58:45 +00001518static void version(void)
1519{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001520 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001521}
1522
ths15f82202007-06-29 23:26:08 +00001523static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001524{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001525 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001526#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1527 opt_help
blueswir15824d652009-03-28 06:44:27 +00001528#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001529#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001530#undef DEF
1531#undef DEFHEADING
1532#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001533 ;
1534 version();
1535 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001536 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001537 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001538 "\n"
1539 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001540 "During emulation, the following keys are useful:\n"
1541 "ctrl-alt-f toggle full screen\n"
1542 "ctrl-alt-n switch to virtual console 'n'\n"
1543 "ctrl-alt toggle mouse and keyboard grab\n"
1544 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001545 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1546 "qemu",
1547 options_help);
ths15f82202007-06-29 23:26:08 +00001548 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001549}
1550
bellardcd6f1162004-05-13 22:02:20 +00001551#define HAS_ARG 0x0001
1552
bellardcd6f1162004-05-13 22:02:20 +00001553typedef struct QEMUOption {
1554 const char *name;
1555 int flags;
1556 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001557 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001558} QEMUOption;
1559
blueswir1dbed7e42008-10-01 19:38:09 +00001560static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001561 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1562#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1563 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001564#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001565#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001566#undef DEF
1567#undef DEFHEADING
1568#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001569 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001570};
malc3893c122008-09-28 00:42:05 +00001571static void select_vgahw (const char *p)
1572{
1573 const char *opts;
1574
Gerd Hoffmann64465292009-12-08 13:11:45 +01001575 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001576 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001577 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001578 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001579 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001580 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001581 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001582 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001583 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001584 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001585 } else if (strstart(p, "qxl", &opts)) {
1586 vga_interface_type = VGA_QXL;
aliguori28b85ed2009-04-22 15:19:48 +00001587 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001588 invalid_vga:
1589 fprintf(stderr, "Unknown vga type: %s\n", p);
1590 exit(1);
1591 }
malccb5a7aa2008-09-28 00:42:12 +00001592 while (*opts) {
1593 const char *nextopt;
1594
1595 if (strstart(opts, ",retrace=", &nextopt)) {
1596 opts = nextopt;
1597 if (strstart(opts, "dumb", &nextopt))
1598 vga_retrace_method = VGA_RETRACE_DUMB;
1599 else if (strstart(opts, "precise", &nextopt))
1600 vga_retrace_method = VGA_RETRACE_PRECISE;
1601 else goto invalid_vga;
1602 } else goto invalid_vga;
1603 opts = nextopt;
1604 }
malc3893c122008-09-28 00:42:05 +00001605}
1606
Jes Sorensen1472a952011-03-16 13:33:31 +01001607static DisplayType select_display(const char *p)
1608{
1609 const char *opts;
1610 DisplayType display = DT_DEFAULT;
1611
1612 if (strstart(p, "sdl", &opts)) {
1613#ifdef CONFIG_SDL
1614 display = DT_SDL;
1615 while (*opts) {
1616 const char *nextopt;
1617
1618 if (strstart(opts, ",frame=", &nextopt)) {
1619 opts = nextopt;
1620 if (strstart(opts, "on", &nextopt)) {
1621 no_frame = 0;
1622 } else if (strstart(opts, "off", &nextopt)) {
1623 no_frame = 1;
1624 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001625 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001626 }
1627 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1628 opts = nextopt;
1629 if (strstart(opts, "on", &nextopt)) {
1630 alt_grab = 1;
1631 } else if (strstart(opts, "off", &nextopt)) {
1632 alt_grab = 0;
1633 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001634 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001635 }
1636 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1637 opts = nextopt;
1638 if (strstart(opts, "on", &nextopt)) {
1639 ctrl_grab = 1;
1640 } else if (strstart(opts, "off", &nextopt)) {
1641 ctrl_grab = 0;
1642 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001643 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001644 }
1645 } else if (strstart(opts, ",window_close=", &nextopt)) {
1646 opts = nextopt;
1647 if (strstart(opts, "on", &nextopt)) {
1648 no_quit = 0;
1649 } else if (strstart(opts, "off", &nextopt)) {
1650 no_quit = 1;
1651 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001652 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001653 }
1654 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001655 invalid_sdl_args:
1656 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1657 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001658 }
1659 opts = nextopt;
1660 }
1661#else
1662 fprintf(stderr, "SDL support is disabled\n");
1663 exit(1);
1664#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001665 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001666#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001667 display_remote++;
1668
1669 if (*opts) {
1670 const char *nextopt;
1671
1672 if (strstart(opts, "=", &nextopt)) {
1673 vnc_display = nextopt;
1674 }
1675 }
1676 if (!vnc_display) {
1677 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1678 exit(1);
1679 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001680#else
1681 fprintf(stderr, "VNC support is disabled\n");
1682 exit(1);
1683#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001684 } else if (strstart(p, "curses", &opts)) {
1685#ifdef CONFIG_CURSES
1686 display = DT_CURSES;
1687#else
1688 fprintf(stderr, "Curses support is disabled\n");
1689 exit(1);
1690#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001691 } else if (strstart(p, "none", &opts)) {
1692 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001693 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001694 fprintf(stderr, "Unknown display type: %s\n", p);
1695 exit(1);
1696 }
1697
1698 return display;
1699}
1700
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001701static int balloon_parse(const char *arg)
1702{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001703 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001704
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001705 if (strcmp(arg, "none") == 0) {
1706 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001707 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001708
1709 if (!strncmp(arg, "virtio", 6)) {
1710 if (arg[6] == ',') {
1711 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001712 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001713 if (!opts)
1714 return -1;
1715 } else {
1716 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001717 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001718 }
Alexander Graf29f82b32011-03-29 15:29:29 +02001719 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001720 return 0;
1721 }
1722
1723 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001724}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001725
Paul Brook5cea8592009-05-30 00:52:44 +01001726char *qemu_find_file(int type, const char *name)
1727{
1728 int len;
1729 const char *subdir;
1730 char *buf;
1731
1732 /* If name contains path separators then try it as a straight path. */
1733 if ((strchr(name, '/') || strchr(name, '\\'))
1734 && access(name, R_OK) == 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001735 return g_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001736 }
1737 switch (type) {
1738 case QEMU_FILE_TYPE_BIOS:
1739 subdir = "";
1740 break;
1741 case QEMU_FILE_TYPE_KEYMAP:
1742 subdir = "keymaps/";
1743 break;
1744 default:
1745 abort();
1746 }
1747 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
Anthony Liguori7267c092011-08-20 22:09:37 -05001748 buf = g_malloc0(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001749 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001750 if (access(buf, R_OK)) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001751 g_free(buf);
Paul Brook5cea8592009-05-30 00:52:44 +01001752 return NULL;
1753 }
1754 return buf;
1755}
1756
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001757static int device_help_func(QemuOpts *opts, void *opaque)
1758{
1759 return qdev_device_help(opts);
1760}
1761
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001762static int device_init_func(QemuOpts *opts, void *opaque)
1763{
1764 DeviceState *dev;
1765
1766 dev = qdev_device_add(opts);
1767 if (!dev)
1768 return -1;
1769 return 0;
1770}
1771
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001772static int chardev_init_func(QemuOpts *opts, void *opaque)
1773{
1774 CharDriverState *chr;
1775
Anthony Liguorif69554b2011-08-15 11:17:37 -05001776 chr = qemu_chr_new_from_opts(opts, NULL);
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001777 if (!chr)
1778 return -1;
1779 return 0;
1780}
1781
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001782#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301783static int fsdev_init_func(QemuOpts *opts, void *opaque)
1784{
1785 int ret;
1786 ret = qemu_fsdev_add(opts);
1787
1788 return ret;
1789}
1790#endif
1791
Gerd Hoffmann88589342009-12-08 13:11:50 +01001792static int mon_init_func(QemuOpts *opts, void *opaque)
1793{
1794 CharDriverState *chr;
1795 const char *chardev;
1796 const char *mode;
1797 int flags;
1798
1799 mode = qemu_opt_get(opts, "mode");
1800 if (mode == NULL) {
1801 mode = "readline";
1802 }
1803 if (strcmp(mode, "readline") == 0) {
1804 flags = MONITOR_USE_READLINE;
1805 } else if (strcmp(mode, "control") == 0) {
1806 flags = MONITOR_USE_CONTROL;
1807 } else {
1808 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1809 exit(1);
1810 }
1811
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001812 if (qemu_opt_get_bool(opts, "pretty", 0))
1813 flags |= MONITOR_USE_PRETTY;
1814
Gerd Hoffmann88589342009-12-08 13:11:50 +01001815 if (qemu_opt_get_bool(opts, "default", 0))
1816 flags |= MONITOR_IS_DEFAULT;
1817
1818 chardev = qemu_opt_get(opts, "chardev");
1819 chr = qemu_chr_find(chardev);
1820 if (chr == NULL) {
1821 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1822 exit(1);
1823 }
1824
1825 monitor_init(chr, flags);
1826 return 0;
1827}
1828
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001829static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001830{
1831 static int monitor_device_index = 0;
1832 QemuOpts *opts;
1833 const char *p;
1834 char label[32];
1835 int def = 0;
1836
1837 if (strstart(optarg, "chardev:", &p)) {
1838 snprintf(label, sizeof(label), "%s", p);
1839 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001840 snprintf(label, sizeof(label), "compat_monitor%d",
1841 monitor_device_index);
1842 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001843 def = 1;
1844 }
1845 opts = qemu_chr_parse_compat(label, optarg);
1846 if (!opts) {
1847 fprintf(stderr, "parse error: %s\n", optarg);
1848 exit(1);
1849 }
1850 }
1851
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001852 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001853 if (!opts) {
1854 fprintf(stderr, "duplicate chardev: %s\n", label);
1855 exit(1);
1856 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001857 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001858 qemu_opt_set(opts, "chardev", label);
1859 if (def)
1860 qemu_opt_set(opts, "default", "on");
1861 monitor_device_index++;
1862}
1863
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001864struct device_config {
1865 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001866 DEV_USB, /* -usbdevice */
1867 DEV_BT, /* -bt */
1868 DEV_SERIAL, /* -serial */
1869 DEV_PARALLEL, /* -parallel */
1870 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001871 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001872 } type;
1873 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001874 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001875};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001876QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001877
1878static void add_device_config(int type, const char *cmdline)
1879{
1880 struct device_config *conf;
1881
Anthony Liguori7267c092011-08-20 22:09:37 -05001882 conf = g_malloc0(sizeof(*conf));
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001883 conf->type = type;
1884 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001885 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001886}
1887
1888static int foreach_device_config(int type, int (*func)(const char *cmdline))
1889{
1890 struct device_config *conf;
1891 int rc;
1892
Blue Swirl72cf2d42009-09-12 07:36:22 +00001893 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001894 if (conf->type != type)
1895 continue;
1896 rc = func(conf->cmdline);
1897 if (0 != rc)
1898 return rc;
1899 }
1900 return 0;
1901}
1902
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001903static int serial_parse(const char *devname)
1904{
1905 static int index = 0;
1906 char label[32];
1907
1908 if (strcmp(devname, "none") == 0)
1909 return 0;
1910 if (index == MAX_SERIAL_PORTS) {
1911 fprintf(stderr, "qemu: too many serial ports\n");
1912 exit(1);
1913 }
1914 snprintf(label, sizeof(label), "serial%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05001915 serial_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001916 if (!serial_hds[index]) {
1917 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1918 devname, strerror(errno));
1919 return -1;
1920 }
1921 index++;
1922 return 0;
1923}
1924
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001925static int parallel_parse(const char *devname)
1926{
1927 static int index = 0;
1928 char label[32];
1929
1930 if (strcmp(devname, "none") == 0)
1931 return 0;
1932 if (index == MAX_PARALLEL_PORTS) {
1933 fprintf(stderr, "qemu: too many parallel ports\n");
1934 exit(1);
1935 }
1936 snprintf(label, sizeof(label), "parallel%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05001937 parallel_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001938 if (!parallel_hds[index]) {
1939 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1940 devname, strerror(errno));
1941 return -1;
1942 }
1943 index++;
1944 return 0;
1945}
1946
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001947static int virtcon_parse(const char *devname)
1948{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001949 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001950 static int index = 0;
1951 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301952 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001953
1954 if (strcmp(devname, "none") == 0)
1955 return 0;
1956 if (index == MAX_VIRTIO_CONSOLES) {
1957 fprintf(stderr, "qemu: too many virtio consoles\n");
1958 exit(1);
1959 }
Amit Shah392ecf52010-01-21 16:19:23 +05301960
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001961 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301962 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1963
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001964 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301965 qemu_opt_set(dev_opts, "driver", "virtconsole");
1966
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001967 snprintf(label, sizeof(label), "virtcon%d", index);
Anthony Liguori27143a42011-08-15 11:17:36 -05001968 virtcon_hds[index] = qemu_chr_new(label, devname, NULL);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001969 if (!virtcon_hds[index]) {
1970 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1971 devname, strerror(errno));
1972 return -1;
1973 }
Amit Shah392ecf52010-01-21 16:19:23 +05301974 qemu_opt_set(dev_opts, "chardev", label);
1975
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001976 index++;
1977 return 0;
1978}
1979
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001980static int debugcon_parse(const char *devname)
1981{
1982 QemuOpts *opts;
1983
Anthony Liguori27143a42011-08-15 11:17:36 -05001984 if (!qemu_chr_new("debugcon", devname, NULL)) {
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001985 exit(1);
1986 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001987 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001988 if (!opts) {
1989 fprintf(stderr, "qemu: already have a debugcon device\n");
1990 exit(1);
1991 }
1992 qemu_opt_set(opts, "driver", "isa-debugcon");
1993 qemu_opt_set(opts, "chardev", "debugcon");
1994 return 0;
1995}
1996
Jan Kiszka9052ea62011-07-23 12:38:37 +02001997static QEMUMachine *machine_parse(const char *name)
1998{
1999 QEMUMachine *m, *machine = NULL;
2000
2001 if (name) {
2002 machine = find_machine(name);
2003 }
2004 if (machine) {
2005 return machine;
2006 }
2007 printf("Supported machines are:\n");
2008 for (m = first_machine; m != NULL; m = m->next) {
2009 if (m->alias) {
2010 printf("%-10s %s (alias of %s)\n", m->alias, m->desc, m->name);
2011 }
2012 printf("%-10s %s%s\n", m->name, m->desc,
2013 m->is_default ? " (default)" : "");
2014 }
2015 exit(!name || *name != '?');
2016}
2017
Anthony PERARD303d4e82010-09-21 20:05:31 +01002018static int tcg_init(void)
2019{
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002020 tcg_exec_init(tcg_tb_size * 1024 * 1024);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002021 return 0;
2022}
2023
2024static struct {
2025 const char *opt_name;
2026 const char *name;
2027 int (*available)(void);
2028 int (*init)(void);
2029 int *allowed;
2030} accel_list[] = {
2031 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01002032 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01002033 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
2034};
2035
2036static int configure_accelerator(void)
2037{
2038 const char *p = NULL;
2039 char buf[10];
2040 int i, ret;
2041 bool accel_initalised = 0;
2042 bool init_failed = 0;
2043
2044 QemuOptsList *list = qemu_find_opts("machine");
2045 if (!QTAILQ_EMPTY(&list->head)) {
2046 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2047 }
2048
2049 if (p == NULL) {
2050 /* Use the default "accelerator", tcg */
2051 p = "tcg";
2052 }
2053
2054 while (!accel_initalised && *p != '\0') {
2055 if (*p == ':') {
2056 p++;
2057 }
2058 p = get_opt_name(buf, sizeof (buf), p, ':');
2059 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
2060 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002061 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002062 ret = accel_list[i].init();
2063 if (ret < 0) {
2064 init_failed = 1;
2065 if (!accel_list[i].available()) {
2066 printf("%s not supported for this target\n",
2067 accel_list[i].name);
2068 } else {
2069 fprintf(stderr, "failed to initialize %s: %s\n",
2070 accel_list[i].name,
2071 strerror(-ret));
2072 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05002073 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002074 } else {
2075 accel_initalised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01002076 }
2077 break;
2078 }
2079 }
2080 if (i == ARRAY_SIZE(accel_list)) {
2081 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
2082 }
2083 }
2084
2085 if (!accel_initalised) {
2086 fprintf(stderr, "No accelerator found!\n");
2087 exit(1);
2088 }
2089
2090 if (init_failed) {
2091 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
2092 }
2093
2094 return !accel_initalised;
2095}
2096
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002097void qemu_add_exit_notifier(Notifier *notify)
2098{
2099 notifier_list_add(&exit_notifiers, notify);
2100}
2101
2102void qemu_remove_exit_notifier(Notifier *notify)
2103{
2104 notifier_list_remove(&exit_notifiers, notify);
2105}
2106
2107static void qemu_run_exit_notifiers(void)
2108{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002109 notifier_list_notify(&exit_notifiers, NULL);
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002110}
2111
Gleb Natapov4cab9462010-12-08 13:35:08 +02002112void qemu_add_machine_init_done_notifier(Notifier *notify)
2113{
2114 notifier_list_add(&machine_init_done_notifiers, notify);
2115}
2116
2117static void qemu_run_machine_init_done_notifiers(void)
2118{
Jan Kiszka9e8dd452011-06-20 14:06:26 +02002119 notifier_list_notify(&machine_init_done_notifiers, NULL);
Gleb Natapov4cab9462010-12-08 13:35:08 +02002120}
2121
Anthony Liguori6530a972010-01-22 09:18:06 -06002122static const QEMUOption *lookup_opt(int argc, char **argv,
2123 const char **poptarg, int *poptind)
2124{
2125 const QEMUOption *popt;
2126 int optind = *poptind;
2127 char *r = argv[optind];
2128 const char *optarg;
2129
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002130 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002131 optind++;
2132 /* Treat --foo the same as -foo. */
2133 if (r[1] == '-')
2134 r++;
2135 popt = qemu_options;
2136 for(;;) {
2137 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002138 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002139 exit(1);
2140 }
2141 if (!strcmp(popt->name, r + 1))
2142 break;
2143 popt++;
2144 }
2145 if (popt->flags & HAS_ARG) {
2146 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002147 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002148 exit(1);
2149 }
2150 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002151 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002152 } else {
2153 optarg = NULL;
2154 }
2155
2156 *poptarg = optarg;
2157 *poptind = optind;
2158
2159 return popt;
2160}
2161
Anthony Liguori07501122011-08-20 22:38:31 -05002162static gpointer malloc_and_trace(gsize n_bytes)
2163{
2164 void *ptr = malloc(n_bytes);
2165 trace_qemu_malloc(n_bytes, ptr);
2166 return ptr;
2167}
2168
2169static gpointer realloc_and_trace(gpointer mem, gsize n_bytes)
2170{
2171 void *ptr = realloc(mem, n_bytes);
2172 trace_qemu_realloc(mem, n_bytes, ptr);
2173 return ptr;
2174}
2175
2176static void free_and_trace(gpointer mem)
2177{
2178 trace_qemu_free(mem);
2179 free(mem);
2180}
2181
malc902b3d52008-12-10 19:18:40 +00002182int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002183{
aliguori59030a82009-04-05 18:43:41 +00002184 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002185 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002186 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002187 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002188 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002189 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002190 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002191 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002192 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002193 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002194 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002195 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002196 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002197 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002198 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002199 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002200 const char *cpu_model;
ths93815bc2007-03-19 15:58:31 +00002201 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002202 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002203#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002204 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002205#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06002206 int defconfig = 1;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002207 const char *trace_file = NULL;
Matthew Fernandezc235d732011-06-07 16:32:40 +00002208 const char *log_mask = NULL;
2209 const char *log_file = NULL;
Anthony Liguori07501122011-08-20 22:38:31 -05002210 GMemVTable mem_trace = {
2211 .malloc = malloc_and_trace,
2212 .realloc = realloc_and_trace,
2213 .free = free_and_trace,
2214 };
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002215
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002216 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002217 error_set_progname(argv[0]);
2218
Anthony Liguori07501122011-08-20 22:38:31 -05002219 g_mem_set_vtable(&mem_trace);
2220
Jan Kiszka68752042009-09-15 13:36:04 +02002221 init_clocks();
2222
malc902b3d52008-12-10 19:18:40 +00002223 qemu_cache_utils_init(envp);
2224
Blue Swirl72cf2d42009-09-12 07:36:22 +00002225 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002226 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002227
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002228 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002229 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002230 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002231 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002232 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002233 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002234 kernel_filename = NULL;
2235 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002236 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002237 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002238
aliguori268a3622009-04-21 22:30:27 +00002239 for (i = 0; i < MAX_NODES; i++) {
2240 node_mem[i] = 0;
2241 node_cpumask[i] = 0;
2242 }
2243
aliguori268a3622009-04-21 22:30:27 +00002244 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002245 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002246
blueswir141bd6392008-10-05 09:56:21 +00002247 autostart= 1;
2248
Anthony Liguori292444c2010-01-21 10:57:58 -06002249 /* first pass of option parsing */
2250 optind = 1;
2251 while (optind < argc) {
2252 if (argv[optind][0] != '-') {
2253 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002254 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002255 continue;
2256 } else {
2257 const QEMUOption *popt;
2258
2259 popt = lookup_opt(argc, argv, &optarg, &optind);
2260 switch (popt->index) {
2261 case QEMU_OPTION_nodefconfig:
2262 defconfig=0;
2263 break;
2264 }
2265 }
2266 }
2267
2268 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002269 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002270
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002271 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002272 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002273 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002274 }
2275
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002276 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002277 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002278 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002279 }
2280 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002281 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002282
2283 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002284 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002285 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002286 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002287 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002288 if (argv[optind][0] != '-') {
Markus Armbruster2292dda2011-01-28 11:21:41 +01002289 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002290 } else {
2291 const QEMUOption *popt;
2292
Anthony Liguori6530a972010-01-22 09:18:06 -06002293 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002294 if (!(popt->arch_mask & arch_type)) {
2295 printf("Option %s not supported for this target\n", popt->name);
2296 exit(1);
2297 }
bellardcd6f1162004-05-13 22:02:20 +00002298 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002299 case QEMU_OPTION_M:
Jan Kiszka9052ea62011-07-23 12:38:37 +02002300 machine = machine_parse(optarg);
bellardcc1daa42005-06-05 14:49:17 +00002301 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002302 case QEMU_OPTION_cpu:
2303 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002304 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002305 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002306 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002307 } else {
2308 cpu_model = optarg;
2309 }
2310 break;
bellardcd6f1162004-05-13 22:02:20 +00002311 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002312 initrd_filename = optarg;
2313 break;
bellardcd6f1162004-05-13 22:02:20 +00002314 case QEMU_OPTION_hda:
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002315 {
2316 char buf[256];
2317 if (cyls == 0)
2318 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2319 else
2320 snprintf(buf, sizeof(buf),
2321 "%s,cyls=%d,heads=%d,secs=%d%s",
2322 HD_OPTS , cyls, heads, secs,
2323 translation == BIOS_ATA_TRANSLATION_LBA ?
thse4bcb142007-12-02 04:51:10 +00002324 ",trans=lba" :
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002325 translation == BIOS_ATA_TRANSLATION_NONE ?
thse4bcb142007-12-02 04:51:10 +00002326 ",trans=none" : "");
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002327 drive_add(IF_DEFAULT, 0, optarg, buf);
2328 break;
2329 }
bellardcd6f1162004-05-13 22:02:20 +00002330 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002331 case QEMU_OPTION_hdc:
2332 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002333 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2334 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002335 break;
thse4bcb142007-12-02 04:51:10 +00002336 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002337 if (drive_def(optarg) == NULL) {
2338 exit(1);
2339 }
thse4bcb142007-12-02 04:51:10 +00002340 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002341 case QEMU_OPTION_set:
2342 if (qemu_set_option(optarg) != 0)
2343 exit(1);
2344 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002345 case QEMU_OPTION_global:
2346 if (qemu_global_option(optarg) != 0)
2347 exit(1);
2348 break;
balrog3e3d5812007-04-30 02:09:25 +00002349 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002350 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002351 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002352 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002353 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002354 break;
j_mayer86f55662007-04-24 06:52:59 +00002355 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002356 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002357 break;
bellardcd6f1162004-05-13 22:02:20 +00002358 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002359 snapshot = 1;
2360 break;
bellardcd6f1162004-05-13 22:02:20 +00002361 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002362 {
bellard330d0412003-07-26 18:11:40 +00002363 const char *p;
2364 p = optarg;
2365 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002366 if (cyls < 1 || cyls > 16383)
2367 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002368 if (*p != ',')
2369 goto chs_fail;
2370 p++;
2371 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002372 if (heads < 1 || heads > 16)
2373 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002374 if (*p != ',')
2375 goto chs_fail;
2376 p++;
2377 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002378 if (secs < 1 || secs > 63)
2379 goto chs_fail;
2380 if (*p == ',') {
2381 p++;
2382 if (!strcmp(p, "none"))
2383 translation = BIOS_ATA_TRANSLATION_NONE;
2384 else if (!strcmp(p, "lba"))
2385 translation = BIOS_ATA_TRANSLATION_LBA;
2386 else if (!strcmp(p, "auto"))
2387 translation = BIOS_ATA_TRANSLATION_AUTO;
2388 else
2389 goto chs_fail;
2390 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002391 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002392 fprintf(stderr, "qemu: invalid physical CHS format\n");
2393 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002394 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002395 if (hda_opts != NULL) {
2396 char num[16];
2397 snprintf(num, sizeof(num), "%d", cyls);
2398 qemu_opt_set(hda_opts, "cyls", num);
2399 snprintf(num, sizeof(num), "%d", heads);
2400 qemu_opt_set(hda_opts, "heads", num);
2401 snprintf(num, sizeof(num), "%d", secs);
2402 qemu_opt_set(hda_opts, "secs", num);
2403 if (translation == BIOS_ATA_TRANSLATION_LBA)
2404 qemu_opt_set(hda_opts, "trans", "lba");
2405 if (translation == BIOS_ATA_TRANSLATION_NONE)
2406 qemu_opt_set(hda_opts, "trans", "none");
2407 }
bellard330d0412003-07-26 18:11:40 +00002408 }
2409 break;
aliguori268a3622009-04-21 22:30:27 +00002410 case QEMU_OPTION_numa:
2411 if (nb_numa_nodes >= MAX_NODES) {
2412 fprintf(stderr, "qemu: too many NUMA nodes\n");
2413 exit(1);
2414 }
2415 numa_add(optarg);
2416 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002417 case QEMU_OPTION_display:
2418 display_type = select_display(optarg);
2419 break;
bellardcd6f1162004-05-13 22:02:20 +00002420 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002421 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002422 break;
balrog4d3b6f62008-02-10 16:33:14 +00002423 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002424#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002425 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002426#else
2427 fprintf(stderr, "Curses support is disabled\n");
2428 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002429#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002430 break;
balroga171fe32007-04-30 01:48:07 +00002431 case QEMU_OPTION_portrait:
Vasily Khoruzhick93128052011-06-17 13:04:36 +03002432 graphic_rotate = 90;
2433 break;
2434 case QEMU_OPTION_rotate:
2435 graphic_rotate = strtol(optarg, (char **) &optarg, 10);
2436 if (graphic_rotate != 0 && graphic_rotate != 90 &&
2437 graphic_rotate != 180 && graphic_rotate != 270) {
2438 fprintf(stderr,
2439 "qemu: only 90, 180, 270 deg rotation is available\n");
2440 exit(1);
2441 }
balroga171fe32007-04-30 01:48:07 +00002442 break;
bellardcd6f1162004-05-13 22:02:20 +00002443 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002444 kernel_filename = optarg;
2445 break;
bellardcd6f1162004-05-13 22:02:20 +00002446 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002447 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002448 break;
bellardcd6f1162004-05-13 22:02:20 +00002449 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002450 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002451 break;
bellardcd6f1162004-05-13 22:02:20 +00002452 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002453 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002454 static const char * const params[] = {
wayne3d3b8302011-07-27 18:04:55 +08002455 "order", "once", "menu",
2456 "splash", "splash-time", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002457 };
2458 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002459 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002460 int legacy = 0;
2461
2462 if (!strchr(optarg, '=')) {
2463 legacy = 1;
2464 pstrcpy(buf, sizeof(buf), optarg);
2465 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2466 fprintf(stderr,
2467 "qemu: unknown boot parameter '%s' in '%s'\n",
2468 buf, optarg);
2469 exit(1);
2470 }
2471
2472 if (legacy ||
2473 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002474 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002475 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002476 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002477 if (!legacy) {
2478 if (get_param_value(buf, sizeof(buf),
2479 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002480 validate_bootdevices(buf);
Anthony Liguori7267c092011-08-20 22:09:37 -05002481 standard_boot_devices = g_strdup(boot_devices);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002482 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2483 qemu_register_reset(restore_boot_devices,
2484 standard_boot_devices);
2485 }
Jan Kiszka95387492009-07-02 00:19:02 +02002486 if (get_param_value(buf, sizeof(buf),
2487 "menu", optarg)) {
2488 if (!strcmp(buf, "on")) {
2489 boot_menu = 1;
2490 } else if (!strcmp(buf, "off")) {
2491 boot_menu = 0;
2492 } else {
2493 fprintf(stderr,
2494 "qemu: invalid option value '%s'\n",
2495 buf);
2496 exit(1);
2497 }
2498 }
wayne3d3b8302011-07-27 18:04:55 +08002499 qemu_opts_parse(qemu_find_opts("boot-opts"),
2500 optarg, 0);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002501 }
bellard36b486b2003-11-11 13:36:08 +00002502 }
2503 break;
bellardcd6f1162004-05-13 22:02:20 +00002504 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002505 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002506 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2507 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002508 break;
bellard52ca8d62006-06-14 16:03:05 +00002509 case QEMU_OPTION_no_fd_bootchk:
2510 fd_bootchk = 0;
2511 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002512 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002513 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002514 exit(1);
2515 }
2516 break;
bellard7c9d8e02005-11-15 22:16:05 +00002517 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002518 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002519 exit(1);
2520 }
bellard702c6512004-04-02 21:21:32 +00002521 break;
bellardc7f74642004-08-24 21:57:12 +00002522#ifdef CONFIG_SLIRP
2523 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002524 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002525 break;
ths47d5d012007-02-20 00:05:08 +00002526 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002527 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002528 break;
bellard9bf05442004-08-25 22:12:49 +00002529 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002530 if (net_slirp_redir(optarg) < 0)
2531 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002532 break;
bellardc7f74642004-08-24 21:57:12 +00002533#endif
balrogdc72ac12008-11-09 00:04:26 +00002534 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002535 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002536 break;
bellard1d14ffa2005-10-30 18:58:22 +00002537 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002538 if (!(audio_available())) {
2539 printf("Option %s not supported for this target\n", popt->name);
2540 exit(1);
2541 }
bellard1d14ffa2005-10-30 18:58:22 +00002542 AUD_help ();
2543 exit (0);
2544 break;
2545 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002546 if (!(audio_available())) {
2547 printf("Option %s not supported for this target\n", popt->name);
2548 exit(1);
2549 }
bellard1d14ffa2005-10-30 18:58:22 +00002550 select_soundhw (optarg);
2551 break;
bellardcd6f1162004-05-13 22:02:20 +00002552 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002553 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002554 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002555 case QEMU_OPTION_version:
2556 version();
2557 exit(0);
2558 break;
aurel3200f82b82008-04-27 21:12:55 +00002559 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002560 int64_t value;
aurel3200f82b82008-04-27 21:12:55 +00002561
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002562 value = strtosz(optarg, NULL);
2563 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002564 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002565 exit(1);
2566 }
aurel3200f82b82008-04-27 21:12:55 +00002567
Anthony Liguoric227f092009-10-01 16:12:16 -05002568 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002569 fprintf(stderr, "qemu: ram size too large\n");
2570 exit(1);
2571 }
2572 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002573 break;
aurel3200f82b82008-04-27 21:12:55 +00002574 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002575 case QEMU_OPTION_mempath:
2576 mem_path = optarg;
2577 break;
2578#ifdef MAP_POPULATE
2579 case QEMU_OPTION_mem_prealloc:
2580 mem_prealloc = 1;
2581 break;
2582#endif
bellardcd6f1162004-05-13 22:02:20 +00002583 case QEMU_OPTION_d:
Matthew Fernandezc235d732011-06-07 16:32:40 +00002584 log_mask = optarg;
2585 break;
2586 case QEMU_OPTION_D:
2587 log_file = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002588 break;
bellardcd6f1162004-05-13 22:02:20 +00002589 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002590 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002591 break;
aliguori59030a82009-04-05 18:43:41 +00002592 case QEMU_OPTION_gdb:
2593 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002594 break;
bellardcd6f1162004-05-13 22:02:20 +00002595 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002596 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002597 break;
j_mayer1192dad2007-10-05 13:08:35 +00002598 case QEMU_OPTION_bios:
2599 bios_name = optarg;
2600 break;
aurel321b530a62009-04-05 20:08:59 +00002601 case QEMU_OPTION_singlestep:
2602 singlestep = 1;
2603 break;
bellardcd6f1162004-05-13 22:02:20 +00002604 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002605 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002606 break;
bellard3d11d0e2004-12-12 16:56:30 +00002607 case QEMU_OPTION_k:
2608 keyboard_layout = optarg;
2609 break;
bellardee22c2f2004-06-03 12:49:50 +00002610 case QEMU_OPTION_localtime:
2611 rtc_utc = 0;
2612 break;
malc3893c122008-09-28 00:42:05 +00002613 case QEMU_OPTION_vga:
2614 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002615 break;
bellarde9b137c2004-06-21 16:46:10 +00002616 case QEMU_OPTION_g:
2617 {
2618 const char *p;
2619 int w, h, depth;
2620 p = optarg;
2621 w = strtol(p, (char **)&p, 10);
2622 if (w <= 0) {
2623 graphic_error:
2624 fprintf(stderr, "qemu: invalid resolution or depth\n");
2625 exit(1);
2626 }
2627 if (*p != 'x')
2628 goto graphic_error;
2629 p++;
2630 h = strtol(p, (char **)&p, 10);
2631 if (h <= 0)
2632 goto graphic_error;
2633 if (*p == 'x') {
2634 p++;
2635 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002636 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002637 depth != 24 && depth != 32)
2638 goto graphic_error;
2639 } else if (*p == '\0') {
2640 depth = graphic_depth;
2641 } else {
2642 goto graphic_error;
2643 }
ths3b46e622007-09-17 08:09:54 +00002644
bellarde9b137c2004-06-21 16:46:10 +00002645 graphic_width = w;
2646 graphic_height = h;
2647 graphic_depth = depth;
2648 }
2649 break;
ths20d8a3e2007-02-18 17:04:49 +00002650 case QEMU_OPTION_echr:
2651 {
2652 char *r;
2653 term_escape_char = strtol(optarg, &r, 0);
2654 if (r == optarg)
2655 printf("Bad argument to echr\n");
2656 break;
2657 }
bellard82c643f2004-07-14 17:28:13 +00002658 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002659 monitor_parse(optarg, "readline");
2660 default_monitor = 0;
2661 break;
2662 case QEMU_OPTION_qmp:
2663 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002664 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002665 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002666 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002667 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002668 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002669 exit(1);
2670 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002671 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002672 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002673 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002674 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002675 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002676 exit(1);
2677 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002678 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302679 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002680 olist = qemu_find_opts("fsdev");
2681 if (!olist) {
2682 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2683 exit(1);
2684 }
2685 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302686 if (!opts) {
2687 fprintf(stderr, "parse error: %s\n", optarg);
2688 exit(1);
2689 }
2690 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302691 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002692 QemuOpts *fsdev;
2693 QemuOpts *device;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302694
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002695 olist = qemu_find_opts("virtfs");
2696 if (!olist) {
2697 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2698 exit(1);
2699 }
2700 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302701 if (!opts) {
2702 fprintf(stderr, "parse error: %s\n", optarg);
2703 exit(1);
2704 }
2705
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002706 if (qemu_opt_get(opts, "fstype") == NULL ||
2707 qemu_opt_get(opts, "mount_tag") == NULL ||
2708 qemu_opt_get(opts, "path") == NULL ||
2709 qemu_opt_get(opts, "security_model") == NULL) {
2710 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302711 "security_model=[mapped|passthrough|none],"
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002712 "mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002713 exit(1);
2714 }
2715
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002716 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2717 qemu_opt_get(opts, "mount_tag"), 1);
2718 if (!fsdev) {
2719 fprintf(stderr, "duplicate fsdev id: %s\n",
2720 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302721 exit(1);
2722 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002723 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2724 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2725 qemu_opt_set(fsdev, "security_model",
2726 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302727
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002728 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2729 qemu_opt_set(device, "driver", "virtio-9p-pci");
2730 qemu_opt_set(device, "fsdev",
2731 qemu_opt_get(opts, "mount_tag"));
2732 qemu_opt_set(device, "mount_tag",
2733 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302734 break;
2735 }
bellard82c643f2004-07-14 17:28:13 +00002736 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002737 add_device_config(DEV_SERIAL, optarg);
2738 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002739 if (strncmp(optarg, "mon:", 4) == 0) {
2740 default_monitor = 0;
2741 }
bellard82c643f2004-07-14 17:28:13 +00002742 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002743 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002744 if (watchdog) {
2745 fprintf(stderr,
2746 "qemu: only one watchdog option may be given\n");
2747 return 1;
2748 }
2749 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002750 break;
2751 case QEMU_OPTION_watchdog_action:
2752 if (select_watchdog_action(optarg) == -1) {
2753 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2754 exit(1);
2755 }
2756 break;
aliguori51ecf132009-01-15 20:06:40 +00002757 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002758 add_device_config(DEV_VIRTCON, optarg);
2759 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002760 if (strncmp(optarg, "mon:", 4) == 0) {
2761 default_monitor = 0;
2762 }
aliguori51ecf132009-01-15 20:06:40 +00002763 break;
bellard6508fe52005-01-15 12:02:56 +00002764 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002765 add_device_config(DEV_PARALLEL, optarg);
2766 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002767 if (strncmp(optarg, "mon:", 4) == 0) {
2768 default_monitor = 0;
2769 }
bellard6508fe52005-01-15 12:02:56 +00002770 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002771 case QEMU_OPTION_debugcon:
2772 add_device_config(DEV_DEBUGCON, optarg);
2773 break;
bellardd63d3072004-10-03 13:29:03 +00002774 case QEMU_OPTION_loadvm:
2775 loadvm = optarg;
2776 break;
2777 case QEMU_OPTION_full_screen:
2778 full_screen = 1;
2779 break;
ths667acca2006-12-11 02:08:05 +00002780#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002781 case QEMU_OPTION_no_frame:
2782 no_frame = 1;
2783 break;
ths3780e192007-06-21 21:08:02 +00002784 case QEMU_OPTION_alt_grab:
2785 alt_grab = 1;
2786 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002787 case QEMU_OPTION_ctrl_grab:
2788 ctrl_grab = 1;
2789 break;
ths667acca2006-12-11 02:08:05 +00002790 case QEMU_OPTION_no_quit:
2791 no_quit = 1;
2792 break;
aliguori7d957bd2009-01-15 22:14:11 +00002793 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002794 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002795 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01002796#else
2797 case QEMU_OPTION_no_frame:
2798 case QEMU_OPTION_alt_grab:
2799 case QEMU_OPTION_ctrl_grab:
2800 case QEMU_OPTION_no_quit:
2801 case QEMU_OPTION_sdl:
2802 fprintf(stderr, "SDL support is disabled\n");
2803 exit(1);
ths667acca2006-12-11 02:08:05 +00002804#endif
bellardf7cce892004-12-08 22:21:25 +00002805 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002806 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002807 break;
bellarda09db212005-04-30 16:10:35 +00002808 case QEMU_OPTION_win2k_hack:
2809 win2k_install_hack = 1;
2810 break;
aliguori73822ec2009-01-15 20:11:34 +00002811 case QEMU_OPTION_rtc_td_hack:
2812 rtc_td_hack = 1;
2813 break;
aliguori8a92ea22009-02-27 20:12:36 +00002814 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002815 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002816 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002817 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002818 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002819 break;
aliguori7ba1e612008-11-05 16:04:33 +00002820 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01002821 olist = qemu_find_opts("machine");
2822 qemu_opts_reset(olist);
2823 qemu_opts_parse(olist, "accel=kvm", 0);
2824 break;
2825 case QEMU_OPTION_machine:
2826 olist = qemu_find_opts("machine");
2827 qemu_opts_reset(olist);
Jan Kiszka9052ea62011-07-23 12:38:37 +02002828 opts = qemu_opts_parse(olist, optarg, 1);
Anthony PERARD303d4e82010-09-21 20:05:31 +01002829 if (!opts) {
2830 fprintf(stderr, "parse error: %s\n", optarg);
2831 exit(1);
2832 }
Jan Kiszka2645c6d2011-07-25 18:11:20 +02002833 optarg = qemu_opt_get(opts, "type");
2834 if (optarg) {
2835 machine = machine_parse(optarg);
2836 }
aliguori7ba1e612008-11-05 16:04:33 +00002837 break;
bellardbb36d472005-11-05 14:22:28 +00002838 case QEMU_OPTION_usb:
2839 usb_enabled = 1;
2840 break;
bellarda594cfb2005-11-06 16:13:29 +00002841 case QEMU_OPTION_usbdevice:
2842 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002843 add_device_config(DEV_USB, optarg);
2844 break;
2845 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002846 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002847 exit(1);
2848 }
bellarda594cfb2005-11-06 16:13:29 +00002849 break;
bellard6a00d602005-11-21 23:25:50 +00002850 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002851 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002852 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002853 fprintf(stderr, "Invalid number of CPUs\n");
2854 exit(1);
2855 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002856 if (max_cpus < smp_cpus) {
2857 fprintf(stderr, "maxcpus must be equal to or greater than "
2858 "smp\n");
2859 exit(1);
2860 }
2861 if (max_cpus > 255) {
2862 fprintf(stderr, "Unsupported number of maxcpus\n");
2863 exit(1);
2864 }
bellard6a00d602005-11-21 23:25:50 +00002865 break;
bellard24236862006-04-30 21:28:36 +00002866 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01002867#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002868 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01002869 vnc_display = optarg;
2870#else
2871 fprintf(stderr, "VNC support is disabled\n");
2872 exit(1);
2873#endif
2874 break;
bellard6515b202006-05-03 22:02:44 +00002875 case QEMU_OPTION_no_acpi:
2876 acpi_enabled = 0;
2877 break;
aliguori16b29ae2008-12-17 23:28:44 +00002878 case QEMU_OPTION_no_hpet:
2879 no_hpet = 1;
2880 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002881 case QEMU_OPTION_balloon:
2882 if (balloon_parse(optarg) < 0) {
2883 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2884 exit(1);
2885 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002886 break;
bellardd1beab82006-10-02 19:44:22 +00002887 case QEMU_OPTION_no_reboot:
2888 no_reboot = 1;
2889 break;
aurel32b2f76162008-04-11 21:35:52 +00002890 case QEMU_OPTION_no_shutdown:
2891 no_shutdown = 1;
2892 break;
balrog9467cd42007-05-01 01:34:14 +00002893 case QEMU_OPTION_show_cursor:
2894 cursor_hide = 0;
2895 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002896 case QEMU_OPTION_uuid:
2897 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2898 fprintf(stderr, "Fail to parse UUID string."
2899 " Wrong format.\n");
2900 exit(1);
2901 }
2902 break;
ths9ae02552007-01-05 17:39:04 +00002903 case QEMU_OPTION_option_rom:
2904 if (nb_option_roms >= MAX_OPTION_ROMS) {
2905 fprintf(stderr, "Too many option ROMs\n");
2906 exit(1);
2907 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02002908 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2909 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2910 option_rom[nb_option_roms].bootindex =
2911 qemu_opt_get_number(opts, "bootindex", -1);
2912 if (!option_rom[nb_option_roms].name) {
2913 fprintf(stderr, "Option ROM file is not specified\n");
2914 exit(1);
2915 }
ths9ae02552007-01-05 17:39:04 +00002916 nb_option_roms++;
2917 break;
pbrook8e716212007-01-20 17:12:09 +00002918 case QEMU_OPTION_semihosting:
2919 semihosting_enabled = 1;
2920 break;
thsc35734b2007-03-19 15:17:08 +00002921 case QEMU_OPTION_name:
Anthony Liguori7267c092011-08-20 22:09:37 -05002922 qemu_name = g_strdup(optarg);
Andi Kleen18894652009-07-02 09:34:17 +02002923 {
2924 char *p = strchr(qemu_name, ',');
2925 if (p != NULL) {
2926 *p++ = 0;
2927 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01002928 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02002929 exit(1);
2930 }
2931 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002932 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002933 }
2934 }
thsc35734b2007-03-19 15:17:08 +00002935 break;
blueswir166508602007-05-01 14:16:52 +00002936 case QEMU_OPTION_prom_env:
2937 if (nb_prom_envs >= MAX_PROM_ENVS) {
2938 fprintf(stderr, "Too many prom variables\n");
2939 exit(1);
2940 }
2941 prom_envs[nb_prom_envs] = optarg;
2942 nb_prom_envs++;
2943 break;
balrog2b8f2d42007-07-27 22:08:46 +00002944 case QEMU_OPTION_old_param:
2945 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002946 break;
thsf3dcfad2007-08-24 01:26:02 +00002947 case QEMU_OPTION_clock:
2948 configure_alarms(optarg);
2949 break;
bellard7e0af5d02007-11-07 16:24:33 +00002950 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002951 configure_rtc_date_offset(optarg, 1);
2952 break;
2953 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002954 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002955 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002956 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002957 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002958 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002959 break;
bellard26a5f132008-05-28 12:30:31 +00002960 case QEMU_OPTION_tb_size:
Jan Kiszkad5ab9712011-08-02 16:10:21 +02002961 tcg_tb_size = strtol(optarg, NULL, 0);
2962 if (tcg_tb_size < 0) {
2963 tcg_tb_size = 0;
2964 }
bellard26a5f132008-05-28 12:30:31 +00002965 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002966 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002967 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002968 break;
aliguori5bb79102008-10-13 03:12:02 +00002969 case QEMU_OPTION_incoming:
2970 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302971 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002972 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002973 case QEMU_OPTION_nodefaults:
2974 default_serial = 0;
2975 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002976 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002977 default_monitor = 0;
2978 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002979 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002980 default_floppy = 0;
2981 default_cdrom = 0;
2982 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002983 break;
aliguorie37630c2009-04-22 15:19:10 +00002984 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002985 if (!(xen_available())) {
2986 printf("Option %s not supported for this target\n", popt->name);
2987 exit(1);
2988 }
aliguorie37630c2009-04-22 15:19:10 +00002989 xen_domid = atoi(optarg);
2990 break;
2991 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002992 if (!(xen_available())) {
2993 printf("Option %s not supported for this target\n", popt->name);
2994 exit(1);
2995 }
aliguorie37630c2009-04-22 15:19:10 +00002996 xen_mode = XEN_CREATE;
2997 break;
2998 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002999 if (!(xen_available())) {
3000 printf("Option %s not supported for this target\n", popt->name);
3001 exit(1);
3002 }
aliguorie37630c2009-04-22 15:19:10 +00003003 xen_mode = XEN_ATTACH;
3004 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01003005#ifdef CONFIG_SIMPLE_TRACE
3006 case QEMU_OPTION_trace:
3007 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
3008 if (opts) {
3009 trace_file = qemu_opt_get(opts, "file");
3010 }
3011 break;
3012#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003013 case QEMU_OPTION_readconfig:
3014 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003015 int ret = qemu_read_config_file(optarg);
3016 if (ret < 0) {
3017 fprintf(stderr, "read config %s: %s\n", optarg,
3018 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003019 exit(1);
3020 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003021 break;
3022 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03003023 case QEMU_OPTION_spice:
3024 olist = qemu_find_opts("spice");
3025 if (!olist) {
3026 fprintf(stderr, "spice is not supported by this qemu build.\n");
3027 exit(1);
3028 }
3029 opts = qemu_opts_parse(olist, optarg, 0);
3030 if (!opts) {
3031 fprintf(stderr, "parse error: %s\n", optarg);
3032 exit(1);
3033 }
3034 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003035 case QEMU_OPTION_writeconfig:
3036 {
3037 FILE *fp;
3038 if (strcmp(optarg, "-") == 0) {
3039 fp = stdout;
3040 } else {
3041 fp = fopen(optarg, "w");
3042 if (fp == NULL) {
3043 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3044 exit(1);
3045 }
3046 }
3047 qemu_config_write(fp);
3048 fclose(fp);
3049 break;
3050 }
Jes Sorensen59a52642010-06-10 11:42:25 +02003051 default:
3052 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003053 }
bellard0824d6f2003-06-24 13:42:40 +00003054 }
3055 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003056 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003057
Matthew Fernandezc235d732011-06-07 16:32:40 +00003058 /* Open the logfile at this point, if necessary. We can't open the logfile
3059 * when encountering either of the logging options (-d or -D) because the
3060 * other one may be encountered later on the command line, changing the
3061 * location or level of logging.
3062 */
3063 if (log_mask) {
3064 if (log_file) {
3065 set_cpu_log_filename(log_file);
3066 }
3067 set_cpu_log(log_mask);
3068 }
3069
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00003070 if (!st_init(trace_file)) {
3071 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
3072 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00003073
Paul Brook5cea8592009-05-30 00:52:44 +01003074 /* If no data_dir is specified then try to find it relative to the
3075 executable path. */
3076 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003077 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003078 }
3079 /* If all else fails use the install patch specified when building. */
3080 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003081 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003082 }
3083
Jes Sorensen6be68d72009-07-23 17:03:42 +02003084 /*
3085 * Default to max_cpus = smp_cpus, in case the user doesn't
3086 * specify a max_cpus value.
3087 */
3088 if (!max_cpus)
3089 max_cpus = smp_cpus;
3090
balrog3d878ca2008-10-28 10:59:59 +00003091 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003092 if (smp_cpus > machine->max_cpus) {
3093 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3094 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3095 machine->max_cpus);
3096 exit(1);
3097 }
3098
Anthony PERARD67b724e2010-11-22 15:44:15 +00003099 /*
3100 * Get the default machine options from the machine if it is not already
3101 * specified either by the configuration file or by the command line.
3102 */
3103 if (machine->default_machine_opts) {
3104 QemuOptsList *list = qemu_find_opts("machine");
3105 const char *p = NULL;
3106
3107 if (!QTAILQ_EMPTY(&list->head)) {
3108 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
3109 }
3110 if (p == NULL) {
Jan Kiszka9052ea62011-07-23 12:38:37 +02003111 qemu_opts_reset(list);
3112 opts = qemu_opts_parse(list, machine->default_machine_opts, 0);
Anthony PERARD67b724e2010-11-22 15:44:15 +00003113 if (!opts) {
3114 fprintf(stderr, "parse error for machine %s: %s\n",
3115 machine->name, machine->default_machine_opts);
3116 exit(1);
3117 }
3118 }
3119 }
3120
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003121 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
3122 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003123
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003124 if (machine->no_serial) {
3125 default_serial = 0;
3126 }
3127 if (machine->no_parallel) {
3128 default_parallel = 0;
3129 }
3130 if (!machine->use_virtcon) {
3131 default_virtcon = 0;
3132 }
3133 if (machine->no_vga) {
3134 default_vga = 0;
3135 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003136 if (machine->no_floppy) {
3137 default_floppy = 0;
3138 }
3139 if (machine->no_cdrom) {
3140 default_cdrom = 0;
3141 }
3142 if (machine->no_sdcard) {
3143 default_sdcard = 0;
3144 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003145
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003146 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003147 if (default_parallel)
3148 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003149 if (default_serial && default_monitor) {
3150 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003151 } else if (default_virtcon && default_monitor) {
3152 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003153 } else {
3154 if (default_serial)
3155 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003156 if (default_virtcon)
3157 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003158 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003159 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003160 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003161 } else {
3162 if (default_serial)
3163 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003164 if (default_parallel)
3165 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003166 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003167 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003168 if (default_virtcon)
3169 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003170 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003171 if (default_vga)
3172 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003173
TeLeMana5829fd2010-04-15 12:37:55 +08003174 socket_init();
3175
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003176 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003177 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003178#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003179 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303180 exit(1);
3181 }
3182#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003183
Jes Sorenseneb505be2010-06-10 11:42:28 +02003184 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003185
thsaa26bb22007-03-25 21:33:06 +00003186 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003187 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003188 exit(1);
3189 }
3190
Jan Kiszka0ac543d2011-08-15 16:18:57 -07003191 /* init the memory */
3192 if (ram_size == 0) {
3193 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
3194 }
3195
Anthony PERARD303d4e82010-09-21 20:05:31 +01003196 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003197
aliguori3fcf7b62009-04-24 18:03:25 +00003198 if (qemu_init_main_loop()) {
3199 fprintf(stderr, "qemu_init_main_loop failed\n");
3200 exit(1);
3201 }
bellarda20dd502003-09-30 21:07:02 +00003202 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003203
thsf8d39c02008-07-03 10:01:15 +00003204 if (!linux_boot && *kernel_cmdline != '\0') {
3205 fprintf(stderr, "-append only allowed with -kernel option\n");
3206 exit(1);
3207 }
3208
3209 if (!linux_boot && initrd_filename != NULL) {
3210 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3211 exit(1);
3212 }
3213
Jes Sorensen9156d762010-06-10 11:42:30 +02003214 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003215
aliguori7183b4b2008-11-05 20:40:18 +00003216 if (init_timer_alarm() < 0) {
3217 fprintf(stderr, "could not initialize alarm timer\n");
3218 exit(1);
3219 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003220 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003221
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003222 if (net_init_clients() < 0) {
3223 exit(1);
bellard702c6512004-04-02 21:21:32 +00003224 }
bellardf1510b22003-06-25 00:07:40 +00003225
balrogdc72ac12008-11-09 00:04:26 +00003226 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003227 if (foreach_device_config(DEV_BT, bt_parse))
3228 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003229
Anthony PERARD834e76e2011-07-20 08:17:44 +00003230 if (!xen_enabled()) {
3231 /* On 32-bit hosts, QEMU is limited by virtual address space */
3232 if (ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
3233 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
3234 exit(1);
3235 }
3236 }
balrog7fb4fdc2008-04-24 17:59:27 +00003237
Jan Kiszkad5ab9712011-08-02 16:10:21 +02003238 cpu_exec_init_all();
bellard26a5f132008-05-28 12:30:31 +00003239
Markus Armbrustereb852012009-10-27 18:41:44 +01003240 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003241
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003242 blk_mig_init();
3243
ths96d30e42007-01-07 20:42:14 +00003244 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003245 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003246 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3247 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003248 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003249
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003250 default_drive(default_cdrom, snapshot, machine->use_scsi,
3251 IF_DEFAULT, 2, CDROM_OPTS);
3252 default_drive(default_floppy, snapshot, machine->use_scsi,
3253 IF_FLOPPY, 0, FD_OPTS);
3254 default_drive(default_sdcard, snapshot, machine->use_scsi,
3255 IF_SD, 0, SD_OPTS);
3256
Alex Williamson97ab12d2010-06-25 11:09:50 -06003257 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003258 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003259
aliguori268a3622009-04-21 22:30:27 +00003260 if (nb_numa_nodes > 0) {
3261 int i;
3262
Sasha Levinea0e5412011-06-29 23:29:39 -04003263 if (nb_numa_nodes > MAX_NODES) {
3264 nb_numa_nodes = MAX_NODES;
aliguori268a3622009-04-21 22:30:27 +00003265 }
3266
3267 /* If no memory size if given for any node, assume the default case
3268 * and distribute the available memory equally across all nodes
3269 */
3270 for (i = 0; i < nb_numa_nodes; i++) {
3271 if (node_mem[i] != 0)
3272 break;
3273 }
3274 if (i == nb_numa_nodes) {
3275 uint64_t usedmem = 0;
3276
3277 /* On Linux, the each node's border has to be 8MB aligned,
3278 * the final node gets the rest.
3279 */
3280 for (i = 0; i < nb_numa_nodes - 1; i++) {
3281 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3282 usedmem += node_mem[i];
3283 }
3284 node_mem[i] = ram_size - usedmem;
3285 }
3286
3287 for (i = 0; i < nb_numa_nodes; i++) {
3288 if (node_cpumask[i] != 0)
3289 break;
3290 }
3291 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3292 * must cope with this anyway, because there are BIOSes out there in
3293 * real machines which also use this scheme.
3294 */
3295 if (i == nb_numa_nodes) {
3296 for (i = 0; i < smp_cpus; i++) {
3297 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3298 }
3299 }
3300 }
3301
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003302 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003303 exit(1);
3304 }
3305
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003306 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3307 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003308 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3309 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003310 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3311 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003312 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3313 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003314
Paul Brookaae94602009-05-14 22:35:06 +01003315 module_call_init(MODULE_INIT_DEVICE);
3316
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003317 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003318 exit(0);
3319
Markus Armbruster09aaa162009-08-21 10:31:34 +02003320 if (watchdog) {
3321 i = select_watchdog(watchdog);
3322 if (i > 0)
3323 exit (i == 1 ? 1 : 0);
3324 }
3325
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003326 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003327 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003328 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003329 qemu_add_globals();
3330
Paul Brookfbe1b592009-05-13 17:56:25 +01003331 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003332 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3333
Jan Kiszkaea375f92010-03-01 19:10:30 +01003334 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003335
Blue Swirl87d0a282010-03-27 18:24:45 +00003336 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003337
aliguori6f338c32009-02-11 15:21:54 +00003338 current_machine = machine;
3339
aliguori3023f332009-01-16 19:04:14 +00003340 /* init USB devices */
3341 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003342 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3343 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003344 }
3345
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003346 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003347 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003348 exit(1);
3349
Markus Armbruster668680f2010-02-11 14:44:58 +01003350 net_check_clients();
3351
aliguori3023f332009-01-16 19:04:14 +00003352 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003353 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003354
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003355 if (using_spice)
3356 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003357 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003358#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003359 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003360#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003361 vnc_display = "localhost:0,to=99";
3362 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003363#else
3364 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003365#endif
3366 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003367
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003368
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003369 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003370 switch (display_type) {
3371 case DT_NOGRAPHIC:
3372 break;
3373#if defined(CONFIG_CURSES)
3374 case DT_CURSES:
3375 curses_display_init(ds, full_screen);
3376 break;
3377#endif
bellard5b0753e2005-03-01 21:37:28 +00003378#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003379 case DT_SDL:
3380 sdl_display_init(ds, full_screen, no_frame);
3381 break;
bellard5b0753e2005-03-01 21:37:28 +00003382#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003383 case DT_SDL:
3384 cocoa_display_init(ds, full_screen);
3385 break;
bellard313aa562003-08-10 21:52:11 +00003386#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003387 default:
3388 break;
3389 }
3390
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003391 /* must be after terminal init, SDL library changes signal handlers */
3392 os_setup_signal_handling();
3393
Jes Sorensen821601e2011-03-16 13:33:36 +01003394#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003395 /* init remote displays */
3396 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003397 vnc_display_init(ds);
3398 if (vnc_display_open(ds, vnc_display) < 0)
3399 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003400
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003401 if (show_vnc_port) {
3402 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003403 }
bellard313aa562003-08-10 21:52:11 +00003404 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003405#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003406#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003407 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003408 qemu_spice_display_init(ds);
3409 }
3410#endif
aliguori5b08fc12008-08-21 20:08:03 +00003411
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003412 /* display setup */
3413 dpy_resize(ds);
aliguori3023f332009-01-16 19:04:14 +00003414 dcl = ds->listeners;
3415 while (dcl != NULL) {
3416 if (dcl->dpy_refresh != NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003417 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3418 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003419 break;
ths20d8a3e2007-02-18 17:04:49 +00003420 }
aliguori3023f332009-01-16 19:04:14 +00003421 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003422 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003423 if (ds->gui_timer == NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003424 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3425 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00003426 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003427 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003428
aliguori59030a82009-04-05 18:43:41 +00003429 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3430 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3431 gdbstub_dev);
3432 exit(1);
balrog45669e02007-07-02 13:20:17 +00003433 }
balrog45669e02007-07-02 13:20:17 +00003434
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003435 qdev_machine_creation_done();
3436
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003437 if (rom_load_all() != 0) {
3438 fprintf(stderr, "rom loading failed\n");
3439 exit(1);
3440 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003441
Isaku Yamahata80376c32010-12-20 14:33:35 +09003442 /* TODO: once all bus devices are qdevified, this should be done
3443 * when bus is created by qdev.c */
3444 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003445 qemu_run_machine_init_done_notifiers();
3446
Jan Kiszkae063eb12011-06-14 18:29:43 +02003447 qemu_system_reset(VMRESET_SILENT);
Juan Quintela05f24012009-08-20 19:42:22 +02003448 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003449 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003450 autostart = 0;
3451 }
3452 }
bellardd63d3072004-10-03 13:29:03 +00003453
Glauber Costa2bb8c102009-07-24 16:20:23 -04003454 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003455 int ret = qemu_start_incoming_migration(incoming);
3456 if (ret < 0) {
3457 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3458 incoming, ret);
3459 exit(ret);
3460 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003461 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003462 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003463 }
thsffd843b2006-12-21 19:46:43 +00003464
Jes Sorenseneb505be2010-06-10 11:42:28 +02003465 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003466
bellard8a7ddc32004-03-31 19:00:16 +00003467 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003468 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003469 net_cleanup();
wayne3d3b8302011-07-27 18:04:55 +08003470 res_free();
thsb46a8902007-10-21 23:20:45 +00003471
bellard0824d6f2003-06-24 13:42:40 +00003472 return 0;
3473}