blob: c1cc6146667c6647c54cd17faf4f467d71a39462 [file] [log] [blame]
bellard0824d6f2003-06-24 13:42:40 +00001/*
bellard80cabfa2004-03-14 12:20:30 +00002 * QEMU System Emulator
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard1df912c2003-06-25 16:20:35 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
bellard0824d6f2003-06-24 13:42:40 +000023 */
bellard0824d6f2003-06-24 13:42:40 +000024#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000025#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000028#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000029#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000030#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000031
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
bellard67b915a2004-03-31 23:37:16 +000035#ifndef _WIN32
Paul Brook5cea8592009-05-30 00:52:44 +010036#include <libgen.h>
bellard67b915a2004-03-31 23:37:16 +000037#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000038#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000039#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000041#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000042#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000043#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000044#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000045#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000047#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000048#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000049#include <sys/select.h>
Prerna Saxenaab6540d2010-08-09 11:48:32 +010050
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <pty.h>
62#include <malloc.h>
thsbd494f42007-09-16 20:03:23 +000063
bellarde57a8c02005-11-10 23:58:52 +000064#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000065#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000066#endif
67#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000068#include <sys/stat.h>
69#include <sys/ethernet.h>
70#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000071#include <netinet/arp.h>
thsd5d10bc2007-02-17 22:54:49 +000072#include <netinet/in_systm.h>
73#include <netinet/ip.h>
74#include <netinet/ip_icmp.h> // must come after ip.h
75#include <netinet/udp.h>
76#include <netinet/tcp.h>
77#include <net/if.h>
78#include <syslog.h>
79#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +000080#endif
bellard7d3505c2004-05-12 19:32:15 +000081#endif
bellardec530c82006-04-25 22:36:06 +000082#endif
bellard67b915a2004-03-31 23:37:16 +000083
blueswir19892fbf2008-08-24 10:34:20 +000084#if defined(__OpenBSD__)
85#include <util.h>
86#endif
87
ths8a16d272008-07-19 09:56:24 +000088#if defined(CONFIG_VDE)
89#include <libvdeplug.h>
90#endif
91
bellard67b915a2004-03-31 23:37:16 +000092#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000093#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000094#endif
95
bellard73332e52004-04-04 20:22:28 +000096#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +020097#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +020098#include <SDL.h>
malc880fec52009-02-15 20:18:41 +000099int qemu_main(int argc, char **argv, char **envp);
100int main(int argc, char **argv)
101{
Stefan Weil59a36a22009-06-18 20:11:03 +0200102 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000103}
104#undef main
105#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000106#endif
bellard73332e52004-04-04 20:22:28 +0000107#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000108
bellard5b0753e2005-03-01 21:37:28 +0000109#ifdef CONFIG_COCOA
110#undef main
111#define main qemu_main
112#endif /* CONFIG_COCOA */
113
blueswir1511d2b12009-03-07 15:32:56 +0000114#include "hw/hw.h"
115#include "hw/boards.h"
116#include "hw/usb.h"
117#include "hw/pcmcia.h"
118#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000119#include "hw/isa.h"
120#include "hw/baum.h"
121#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100122#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000123#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000124#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200125#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200126#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000127#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000128#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000129#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000130#include "monitor.h"
131#include "console.h"
132#include "sysemu.h"
133#include "gdbstub.h"
134#include "qemu-timer.h"
135#include "qemu-char.h"
136#include "cache-utils.h"
137#include "block.h"
Markus Armbruster666daa62010-06-02 18:48:27 +0200138#include "blockdev.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200139#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000140#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000141#include "audio/audio.h"
142#include "migration.h"
143#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200144#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200145#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200146#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200147#include "qemu-options.h"
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -0700148#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530149#include "fsdev/qemu-fsdev.h"
150#endif
blueswir1511d2b12009-03-07 15:32:56 +0000151
bellard0824d6f2003-06-24 13:42:40 +0000152#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000153
blueswir1511d2b12009-03-07 15:32:56 +0000154#include "qemu_socket.h"
155
Jan Kiszkad918f232009-06-24 14:42:30 +0200156#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000157
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +0000158#include "trace.h"
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +0000159#include "simpletrace.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000160#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000161#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000162#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000163
Gerd Hoffmann29b00402010-03-11 11:13:27 -0300164#include "ui/qemu-spice.h"
165
blueswir19dc63a12008-10-04 07:25:46 +0000166//#define DEBUG_NET
167//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000168
bellard1bfe8562004-07-08 21:17:50 +0000169#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000170
Amit Shah98b19252010-01-20 00:36:52 +0530171#define MAX_VIRTIO_CONSOLES 1
172
Paul Brook5cea8592009-05-30 00:52:44 +0100173static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000174const char *bios_name = NULL;
malccb5a7aa2008-09-28 00:42:12 +0000175enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500176DisplayType display_type = DT_DEFAULT;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +0200177int display_remote = 0;
bellard3d11d0e2004-12-12 16:56:30 +0000178const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500179ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300180const char *mem_path = NULL;
181#ifdef MAP_POPULATE
182int mem_prealloc = 0; /* force preallocation of physical target memory */
183#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000184int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000185NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000186int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200187int autostart;
Amit Shah8e848652010-07-27 15:49:19 +0530188int incoming_expected; /* Started with -incoming and waiting for incoming */
balrogf6503052008-02-17 11:42:19 +0000189static int rtc_utc = 1;
190static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200191QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100192int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000193static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000194#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#endif
ths667acca2006-12-11 02:08:05 +0000197int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000198CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000199CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000200CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000201int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000202int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000203int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000204int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000205int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200206int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200207int smp_cores = 1;
208int smp_threads = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +0100209#ifdef CONFIG_VNC
ths73fc9742006-12-22 02:09:07 +0000210const char *vnc_display;
Jes Sorensen821601e2011-03-16 13:33:36 +0100211#endif
bellard6515b202006-05-03 22:02:44 +0000212int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000213int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000214int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000215int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000216int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200219uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200220const char *watchdog;
Gleb Natapov2e55e842010-12-08 13:35:07 +0200221QEMUOptionRom option_rom[MAX_OPTION_ROMS];
ths9ae02552007-01-05 17:39:04 +0000222int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000223int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000224int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000225const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000226int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500227int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000228unsigned int nb_prom_envs = 0;
229const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200230int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000231
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200232typedef struct FWBootEntry FWBootEntry;
233
234struct FWBootEntry {
235 QTAILQ_ENTRY(FWBootEntry) link;
236 int32_t bootindex;
237 DeviceState *dev;
238 char *suffix;
239};
240
241QTAILQ_HEAD(, FWBootEntry) fw_boot_order = QTAILQ_HEAD_INITIALIZER(fw_boot_order);
242
aliguori268a3622009-04-21 22:30:27 +0000243int nb_numa_nodes;
244uint64_t node_mem[MAX_NODES];
245uint64_t node_cpumask[MAX_NODES];
246
blueswir19043b622009-01-21 19:28:13 +0000247static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000248
blueswir18fcb1b92008-09-18 18:29:08 +0000249uint8_t qemu_uuid[16];
250
Jan Kiszka76e30d02009-07-02 00:19:02 +0200251static QEMUBootSetHandler *boot_set_handler;
252static void *boot_set_opaque;
253
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +0200254static NotifierList exit_notifiers =
255 NOTIFIER_LIST_INITIALIZER(exit_notifiers);
256
Gleb Natapov4cab9462010-12-08 13:35:08 +0200257static NotifierList machine_init_done_notifiers =
258 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
259
Anthony PERARD303d4e82010-09-21 20:05:31 +0100260static int tcg_allowed = 1;
Blue Swirld745bef2010-03-29 19:23:49 +0000261int kvm_allowed = 0;
Anthony PERARD3285cf42010-08-19 12:27:56 +0100262int xen_allowed = 0;
Blue Swirld745bef2010-03-29 19:23:49 +0000263uint32_t xen_domid;
264enum xen_mode xen_mode = XEN_EMULATE;
265
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100266static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100267static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100268static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100269static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100270static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100271static int default_floppy = 1;
272static int default_cdrom = 1;
273static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100274
275static struct {
276 const char *driver;
277 int *flag;
278} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100279 { .driver = "isa-serial", .flag = &default_serial },
280 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100281 { .driver = "isa-fdc", .flag = &default_floppy },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200282 { .driver = "ide-cd", .flag = &default_cdrom },
283 { .driver = "ide-hd", .flag = &default_cdrom },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100284 { .driver = "ide-drive", .flag = &default_cdrom },
Markus Armbrusteraf6bf132011-05-18 18:31:02 +0200285 { .driver = "scsi-cd", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530286 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
287 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
288 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100289 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100290 { .driver = "cirrus-vga", .flag = &default_vga },
291 { .driver = "vmware-svga", .flag = &default_vga },
Markus Armbruster0826c712011-06-07 10:34:30 +0200292 { .driver = "isa-vga", .flag = &default_vga },
Gerd Hoffmann42138042011-05-16 09:28:58 +0200293 { .driver = "qxl-vga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100294};
295
296static int default_driver_check(QemuOpts *opts, void *opaque)
297{
298 const char *driver = qemu_opt_get(opts, "driver");
299 int i;
300
301 if (!driver)
302 return 0;
303 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
304 if (strcmp(default_list[i].driver, driver) != 0)
305 continue;
306 *(default_list[i].flag) = 0;
307 }
308 return 0;
309}
310
bellard0824d6f2003-06-24 13:42:40 +0000311/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100312/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000313
bellardc4b1fcc2004-03-14 21:44:30 +0000314/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000315/* host time/date access */
316void qemu_get_timedate(struct tm *tm, int offset)
317{
318 time_t ti;
319 struct tm *ret;
320
321 time(&ti);
322 ti += offset;
323 if (rtc_date_offset == -1) {
324 if (rtc_utc)
325 ret = gmtime(&ti);
326 else
327 ret = localtime(&ti);
328 } else {
329 ti -= rtc_date_offset;
330 ret = gmtime(&ti);
331 }
332
333 memcpy(tm, ret, sizeof(struct tm));
334}
335
336int qemu_timedate_diff(struct tm *tm)
337{
338 time_t seconds;
339
340 if (rtc_date_offset == -1)
341 if (rtc_utc)
342 seconds = mktimegm(tm);
343 else
344 seconds = mktime(tm);
345 else
346 seconds = mktimegm(tm) + rtc_date_offset;
347
348 return seconds - time(NULL);
349}
350
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300351void rtc_change_mon_event(struct tm *tm)
352{
353 QObject *data;
354
355 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
356 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
357 qobject_decref(data);
358}
359
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200360static void configure_rtc_date_offset(const char *startdate, int legacy)
361{
362 time_t rtc_start_date;
363 struct tm tm;
364
365 if (!strcmp(startdate, "now") && legacy) {
366 rtc_date_offset = -1;
367 } else {
368 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
369 &tm.tm_year,
370 &tm.tm_mon,
371 &tm.tm_mday,
372 &tm.tm_hour,
373 &tm.tm_min,
374 &tm.tm_sec) == 6) {
375 /* OK */
376 } else if (sscanf(startdate, "%d-%d-%d",
377 &tm.tm_year,
378 &tm.tm_mon,
379 &tm.tm_mday) == 3) {
380 tm.tm_hour = 0;
381 tm.tm_min = 0;
382 tm.tm_sec = 0;
383 } else {
384 goto date_fail;
385 }
386 tm.tm_year -= 1900;
387 tm.tm_mon--;
388 rtc_start_date = mktimegm(&tm);
389 if (rtc_start_date == -1) {
390 date_fail:
391 fprintf(stderr, "Invalid date format. Valid formats are:\n"
392 "'2006-06-17T16:01:21' or '2006-06-17'\n");
393 exit(1);
394 }
395 rtc_date_offset = time(NULL) - rtc_start_date;
396 }
397}
398
399static void configure_rtc(QemuOpts *opts)
400{
401 const char *value;
402
403 value = qemu_opt_get(opts, "base");
404 if (value) {
405 if (!strcmp(value, "utc")) {
406 rtc_utc = 1;
407 } else if (!strcmp(value, "localtime")) {
408 rtc_utc = 0;
409 } else {
410 configure_rtc_date_offset(value, 0);
411 }
412 }
Jan Kiszka68752042009-09-15 13:36:04 +0200413 value = qemu_opt_get(opts, "clock");
414 if (value) {
415 if (!strcmp(value, "host")) {
416 rtc_clock = host_clock;
417 } else if (!strcmp(value, "vm")) {
418 rtc_clock = vm_clock;
419 } else {
420 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
421 exit(1);
422 }
423 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200424 value = qemu_opt_get(opts, "driftfix");
425 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000426 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200427 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000428 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200429 rtc_td_hack = 0;
430 } else {
431 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
432 exit(1);
433 }
434 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200435}
436
balrog1ae26a12008-09-28 23:19:47 +0000437/***********************************************************/
438/* Bluetooth support */
439static int nb_hcis;
440static int cur_hci;
441static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000442
balrog1ae26a12008-09-28 23:19:47 +0000443static struct bt_vlan_s {
444 struct bt_scatternet_s net;
445 int id;
446 struct bt_vlan_s *next;
447} *first_bt_vlan;
448
449/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000450static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000451{
452 struct bt_vlan_s **pvlan, *vlan;
453 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
454 if (vlan->id == id)
455 return &vlan->net;
456 }
457 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
458 vlan->id = id;
459 pvlan = &first_bt_vlan;
460 while (*pvlan != NULL)
461 pvlan = &(*pvlan)->next;
462 *pvlan = vlan;
463 return &vlan->net;
464}
465
466static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
467{
468}
469
470static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
471{
472 return -ENOTSUP;
473}
474
475static struct HCIInfo null_hci = {
476 .cmd_send = null_hci_send,
477 .sco_send = null_hci_send,
478 .acl_send = null_hci_send,
479 .bdaddr_set = null_hci_addr_set,
480};
481
482struct HCIInfo *qemu_next_hci(void)
483{
484 if (cur_hci == nb_hcis)
485 return &null_hci;
486
487 return hci_table[cur_hci++];
488}
489
balrogdc72ac12008-11-09 00:04:26 +0000490static struct HCIInfo *hci_init(const char *str)
491{
492 char *endp;
493 struct bt_scatternet_s *vlan = 0;
494
495 if (!strcmp(str, "null"))
496 /* null */
497 return &null_hci;
498 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
499 /* host[:hciN] */
500 return bt_host_hci(str[4] ? str + 5 : "hci0");
501 else if (!strncmp(str, "hci", 3)) {
502 /* hci[,vlan=n] */
503 if (str[3]) {
504 if (!strncmp(str + 3, ",vlan=", 6)) {
505 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
506 if (*endp)
507 vlan = 0;
508 }
509 } else
510 vlan = qemu_find_bt_vlan(0);
511 if (vlan)
512 return bt_new_hci(vlan);
513 }
514
515 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
516
517 return 0;
518}
519
520static int bt_hci_parse(const char *str)
521{
522 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500523 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000524
525 if (nb_hcis >= MAX_NICS) {
526 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
527 return -1;
528 }
529
530 hci = hci_init(str);
531 if (!hci)
532 return -1;
533
534 bdaddr.b[0] = 0x52;
535 bdaddr.b[1] = 0x54;
536 bdaddr.b[2] = 0x00;
537 bdaddr.b[3] = 0x12;
538 bdaddr.b[4] = 0x34;
539 bdaddr.b[5] = 0x56 + nb_hcis;
540 hci->bdaddr_set(hci, bdaddr.b);
541
542 hci_table[nb_hcis++] = hci;
543
544 return 0;
545}
546
547static void bt_vhci_add(int vlan_id)
548{
549 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
550
551 if (!vlan->slave)
552 fprintf(stderr, "qemu: warning: adding a VHCI to "
553 "an empty scatternet %i\n", vlan_id);
554
555 bt_vhci_init(bt_new_hci(vlan));
556}
557
558static struct bt_device_s *bt_device_add(const char *opt)
559{
560 struct bt_scatternet_s *vlan;
561 int vlan_id = 0;
562 char *endp = strstr(opt, ",vlan=");
563 int len = (endp ? endp - opt : strlen(opt)) + 1;
564 char devname[10];
565
566 pstrcpy(devname, MIN(sizeof(devname), len), opt);
567
568 if (endp) {
569 vlan_id = strtol(endp + 6, &endp, 0);
570 if (*endp) {
571 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
572 return 0;
573 }
574 }
575
576 vlan = qemu_find_bt_vlan(vlan_id);
577
578 if (!vlan->slave)
579 fprintf(stderr, "qemu: warning: adding a slave device to "
580 "an empty scatternet %i\n", vlan_id);
581
582 if (!strcmp(devname, "keyboard"))
583 return bt_keyboard_init(vlan);
584
585 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
586 return 0;
587}
588
589static int bt_parse(const char *opt)
590{
591 const char *endp, *p;
592 int vlan;
593
594 if (strstart(opt, "hci", &endp)) {
595 if (!*endp || *endp == ',') {
596 if (*endp)
597 if (!strstart(endp, ",vlan=", 0))
598 opt = endp + 1;
599
600 return bt_hci_parse(opt);
601 }
602 } else if (strstart(opt, "vhci", &endp)) {
603 if (!*endp || *endp == ',') {
604 if (*endp) {
605 if (strstart(endp, ",vlan=", &p)) {
606 vlan = strtol(p, (char **) &endp, 0);
607 if (*endp) {
608 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
609 return 1;
610 }
611 } else {
612 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
613 return 1;
614 }
615 } else
616 vlan = 0;
617
618 bt_vhci_add(vlan);
619 return 0;
620 }
621 } else if (strstart(opt, "device:", &endp))
622 return !bt_device_add(endp);
623
624 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
625 return 1;
626}
627
balrog1ae26a12008-09-28 23:19:47 +0000628/***********************************************************/
629/* QEMU Block devices */
630
Markus Armbruster2292dda2011-01-28 11:21:41 +0100631#define HD_OPTS "media=disk"
632#define CDROM_OPTS "media=cdrom"
633#define FD_OPTS ""
634#define PFLASH_OPTS ""
635#define MTD_OPTS ""
636#define SD_OPTS ""
thse4bcb142007-12-02 04:51:10 +0000637
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200638static int drive_init_func(QemuOpts *opts, void *opaque)
639{
Markus Armbrustera803cb82010-06-02 13:31:55 +0200640 int *use_scsi = opaque;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200641
Markus Armbruster319ae522011-01-28 11:21:46 +0100642 return drive_init(opts, *use_scsi) == NULL;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200643}
644
645static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
646{
647 if (NULL == qemu_opt_get(opts, "snapshot")) {
648 qemu_opt_set(opts, "snapshot", "on");
649 }
650 return 0;
651}
652
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100653static void default_drive(int enable, int snapshot, int use_scsi,
654 BlockInterfaceType type, int index,
655 const char *optstr)
656{
657 QemuOpts *opts;
658
659 if (type == IF_DEFAULT) {
660 type = use_scsi ? IF_SCSI : IF_IDE;
661 }
662
663 if (!enable || drive_get_by_index(type, index)) {
664 return;
665 }
666
667 opts = drive_add(type, index, NULL, optstr);
668 if (snapshot) {
669 drive_enable_snapshot(opts, NULL);
670 }
Markus Armbruster319ae522011-01-28 11:21:46 +0100671 if (!drive_init(opts, use_scsi)) {
Markus Armbruster4e5d9b52011-01-28 11:21:45 +0100672 exit(1);
673 }
674}
675
Jan Kiszka76e30d02009-07-02 00:19:02 +0200676void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
677{
678 boot_set_handler = func;
679 boot_set_opaque = opaque;
680}
681
682int qemu_boot_set(const char *boot_devices)
683{
684 if (!boot_set_handler) {
685 return -EINVAL;
686 }
687 return boot_set_handler(boot_set_opaque, boot_devices);
688}
689
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -0300690static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200691{
692 /* We just do some generic consistency checks */
693 const char *p;
694 int bitmap = 0;
695
696 for (p = devices; *p != '\0'; p++) {
697 /* Allowed boot devices are:
698 * a-b: floppy disk drives
699 * c-f: IDE disk drives
700 * g-m: machine implementation dependant drives
701 * n-p: network devices
702 * It's up to each machine implementation to check if the given boot
703 * devices match the actual hardware implementation and firmware
704 * features.
705 */
706 if (*p < 'a' || *p > 'p') {
707 fprintf(stderr, "Invalid boot device '%c'\n", *p);
708 exit(1);
709 }
710 if (bitmap & (1 << (*p - 'a'))) {
711 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
712 exit(1);
713 }
714 bitmap |= 1 << (*p - 'a');
715 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +0200716}
717
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200718static void restore_boot_devices(void *opaque)
719{
720 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -0600721 static int first = 1;
722
723 /* Restore boot order and remove ourselves after the first boot */
724 if (first) {
725 first = 0;
726 return;
727 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +0200728
729 qemu_boot_set(standard_boot_devices);
730
731 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
732 qemu_free(standard_boot_devices);
733}
734
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200735void add_boot_device_path(int32_t bootindex, DeviceState *dev,
736 const char *suffix)
737{
738 FWBootEntry *node, *i;
739
740 if (bootindex < 0) {
741 return;
742 }
743
744 assert(dev != NULL || suffix != NULL);
745
746 node = qemu_mallocz(sizeof(FWBootEntry));
747 node->bootindex = bootindex;
Gleb Natapov4fef9302011-02-02 17:34:34 +0200748 node->suffix = suffix ? qemu_strdup(suffix) : NULL;
Gleb Natapov1ca4d092010-12-08 13:35:05 +0200749 node->dev = dev;
750
751 QTAILQ_FOREACH(i, &fw_boot_order, link) {
752 if (i->bootindex == bootindex) {
753 fprintf(stderr, "Two devices with same boot index %d\n", bootindex);
754 exit(1);
755 } else if (i->bootindex < bootindex) {
756 continue;
757 }
758 QTAILQ_INSERT_BEFORE(i, node, link);
759 return;
760 }
761 QTAILQ_INSERT_TAIL(&fw_boot_order, node, link);
762}
763
Gleb Natapov962630f2010-12-08 13:35:09 +0200764/*
765 * This function returns null terminated string that consist of new line
Brad Hards71785ab2011-04-23 21:50:06 +1000766 * separated device paths.
Gleb Natapov962630f2010-12-08 13:35:09 +0200767 *
768 * memory pointed by "size" is assigned total length of the array in bytes
769 *
770 */
771char *get_boot_devices_list(uint32_t *size)
772{
773 FWBootEntry *i;
774 uint32_t total = 0;
775 char *list = NULL;
776
777 QTAILQ_FOREACH(i, &fw_boot_order, link) {
778 char *devpath = NULL, *bootpath;
779 int len;
780
781 if (i->dev) {
782 devpath = qdev_get_fw_dev_path(i->dev);
783 assert(devpath);
784 }
785
786 if (i->suffix && devpath) {
Blue Swirl4fd37a92010-12-19 14:05:43 +0000787 size_t bootpathlen = strlen(devpath) + strlen(i->suffix) + 1;
788
789 bootpath = qemu_malloc(bootpathlen);
790 snprintf(bootpath, bootpathlen, "%s%s", devpath, i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200791 qemu_free(devpath);
792 } else if (devpath) {
793 bootpath = devpath;
794 } else {
Gleb Natapov4fef9302011-02-02 17:34:34 +0200795 bootpath = qemu_strdup(i->suffix);
Gleb Natapov962630f2010-12-08 13:35:09 +0200796 assert(bootpath);
797 }
798
799 if (total) {
800 list[total-1] = '\n';
801 }
802 len = strlen(bootpath) + 1;
803 list = qemu_realloc(list, total + len);
804 memcpy(&list[total], bootpath, len);
805 total += len;
806 qemu_free(bootpath);
807 }
808
809 *size = total;
810
811 return list;
812}
813
aliguori268a3622009-04-21 22:30:27 +0000814static void numa_add(const char *optarg)
815{
816 char option[128];
817 char *endptr;
818 unsigned long long value, endvalue;
819 int nodenr;
820
821 optarg = get_opt_name(option, 128, optarg, ',') + 1;
822 if (!strcmp(option, "node")) {
823 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
824 nodenr = nb_numa_nodes;
825 } else {
826 nodenr = strtoull(option, NULL, 10);
827 }
828
829 if (get_param_value(option, 128, "mem", optarg) == 0) {
830 node_mem[nodenr] = 0;
831 } else {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100832 int64_t sval;
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200833 sval = strtosz(option, NULL);
834 if (sval < 0) {
835 fprintf(stderr, "qemu: invalid numa mem size: %s\n", optarg);
836 exit(1);
aliguori268a3622009-04-21 22:30:27 +0000837 }
Jes Sorensen9f9b17a2010-10-21 17:15:46 +0200838 node_mem[nodenr] = sval;
aliguori268a3622009-04-21 22:30:27 +0000839 }
840 if (get_param_value(option, 128, "cpus", optarg) == 0) {
841 node_cpumask[nodenr] = 0;
842 } else {
843 value = strtoull(option, &endptr, 10);
844 if (value >= 64) {
845 value = 63;
846 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
847 } else {
848 if (*endptr == '-') {
849 endvalue = strtoull(endptr+1, &endptr, 10);
850 if (endvalue >= 63) {
851 endvalue = 62;
852 fprintf(stderr,
853 "only 63 CPUs in NUMA mode supported.\n");
854 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100855 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +0000856 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +0100857 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +0000858 }
859 }
860 node_cpumask[nodenr] = value;
861 }
862 nb_numa_nodes++;
863 }
864 return;
865}
866
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200867static void smp_parse(const char *optarg)
868{
869 int smp, sockets = 0, threads = 0, cores = 0;
870 char *endptr;
871 char option[128];
872
873 smp = strtoul(optarg, &endptr, 10);
874 if (endptr != optarg) {
875 if (*endptr == ',') {
876 endptr++;
877 }
878 }
879 if (get_param_value(option, 128, "sockets", endptr) != 0)
880 sockets = strtoull(option, NULL, 10);
881 if (get_param_value(option, 128, "cores", endptr) != 0)
882 cores = strtoull(option, NULL, 10);
883 if (get_param_value(option, 128, "threads", endptr) != 0)
884 threads = strtoull(option, NULL, 10);
885 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
886 max_cpus = strtoull(option, NULL, 10);
887
888 /* compute missing values, prefer sockets over cores over threads */
889 if (smp == 0 || sockets == 0) {
890 sockets = sockets > 0 ? sockets : 1;
891 cores = cores > 0 ? cores : 1;
892 threads = threads > 0 ? threads : 1;
893 if (smp == 0) {
894 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200895 }
896 } else {
897 if (cores == 0) {
898 threads = threads > 0 ? threads : 1;
899 cores = smp / (sockets * threads);
900 } else {
Joel Schoppdca98162010-07-21 15:05:17 -0500901 threads = smp / (cores * sockets);
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200902 }
903 }
904 smp_cpus = smp;
905 smp_cores = cores > 0 ? cores : 1;
906 smp_threads = threads > 0 ? threads : 1;
907 if (max_cpus == 0)
908 max_cpus = smp_cpus;
909}
910
bellard330d0412003-07-26 18:11:40 +0000911/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +0000912/* USB devices */
913
Markus Armbrusterfb080002010-05-28 15:38:44 +0200914static int usb_device_add(const char *devname)
bellarda594cfb2005-11-06 16:13:29 +0000915{
916 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200917 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +0000918
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200919 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +0000920 return -1;
921
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +0100922 /* drivers with .usbdevice_name entry in USBDeviceInfo */
923 dev = usbdevice_create(devname);
924 if (dev)
925 goto done;
926
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200927 /* the other ones */
Gerd Hoffmanne447fc62011-06-01 14:41:59 +0200928#ifndef CONFIG_LINUX
929 /* only the linux version is qdev-ified, usb-bsd still needs this */
bellarda594cfb2005-11-06 16:13:29 +0000930 if (strstart(devname, "host:", &p)) {
931 dev = usb_host_device_open(p);
Gerd Hoffmanne447fc62011-06-01 14:41:59 +0200932 } else
933#endif
934 if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
balrogdc72ac12008-11-09 00:04:26 +0000935 dev = usb_bt_init(devname[2] ? hci_init(p) :
936 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +0000937 } else {
938 return -1;
939 }
pbrook0d92ed32006-05-21 16:30:15 +0000940 if (!dev)
941 return -1;
942
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200943done:
bellarda594cfb2005-11-06 16:13:29 +0000944 return 0;
945}
946
aliguori1f3870a2008-08-21 19:27:48 +0000947static int usb_device_del(const char *devname)
948{
949 int bus_num, addr;
950 const char *p;
951
aliguori5d0c5752008-09-14 01:07:41 +0000952 if (strstart(devname, "host:", &p))
953 return usb_host_device_close(p);
954
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200955 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +0000956 return -1;
957
958 p = strchr(devname, '.');
959 if (!p)
960 return -1;
961 bus_num = strtoul(devname, NULL, 0);
962 addr = strtoul(p + 1, NULL, 0);
963
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +0200964 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +0000965}
966
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200967static int usb_parse(const char *cmdline)
968{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800969 int r;
Markus Armbrusterfb080002010-05-28 15:38:44 +0200970 r = usb_device_add(cmdline);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800971 if (r < 0) {
972 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
973 }
974 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200975}
976
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300977void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000978{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800979 const char *devname = qdict_get_str(qdict, "devname");
Markus Armbrusterfb080002010-05-28 15:38:44 +0200980 if (usb_device_add(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100981 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800982 }
bellarda594cfb2005-11-06 16:13:29 +0000983}
984
Luiz Capitulinod54908a2009-08-28 15:27:13 -0300985void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +0000986{
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800987 const char *devname = qdict_get_str(qdict, "devname");
988 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100989 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +0800990 }
bellarda594cfb2005-11-06 16:13:29 +0000991}
992
bellardf7cce892004-12-08 22:21:25 +0000993/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +0000994/* PCMCIA/Cardbus */
995
996static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +0100997 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +0000998 struct pcmcia_socket_entry_s *next;
999} *pcmcia_sockets = 0;
1000
Paul Brookbc24a222009-05-10 01:44:56 +01001001void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001002{
1003 struct pcmcia_socket_entry_s *entry;
1004
1005 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1006 entry->socket = socket;
1007 entry->next = pcmcia_sockets;
1008 pcmcia_sockets = entry;
1009}
1010
Paul Brookbc24a222009-05-10 01:44:56 +01001011void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001012{
1013 struct pcmcia_socket_entry_s *entry, **ptr;
1014
1015 ptr = &pcmcia_sockets;
1016 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1017 if (entry->socket == socket) {
1018 *ptr = entry->next;
1019 qemu_free(entry);
1020 }
1021}
1022
aliguori376253e2009-03-05 23:01:23 +00001023void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001024{
1025 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001026
balrog201a51f2007-04-30 00:51:09 +00001027 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001028 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001029
1030 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001031 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1032 iter->socket->attached ? iter->socket->card_string :
1033 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001034}
1035
1036/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001037/* machine registration */
1038
blueswir1bdaf78e2008-10-04 07:24:27 +00001039static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001040QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001041
1042int qemu_register_machine(QEMUMachine *m)
1043{
1044 QEMUMachine **pm;
1045 pm = &first_machine;
1046 while (*pm != NULL)
1047 pm = &(*pm)->next;
1048 m->next = NULL;
1049 *pm = m;
1050 return 0;
1051}
1052
pbrook9596ebb2007-11-18 01:44:38 +00001053static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001054{
1055 QEMUMachine *m;
1056
1057 for(m = first_machine; m != NULL; m = m->next) {
1058 if (!strcmp(m->name, name))
1059 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001060 if (m->alias && !strcmp(m->alias, name))
1061 return m;
bellardcc1daa42005-06-05 14:49:17 +00001062 }
1063 return NULL;
1064}
1065
Anthony Liguori0c257432009-05-21 20:41:01 -05001066static QEMUMachine *find_default_machine(void)
1067{
1068 QEMUMachine *m;
1069
1070 for(m = first_machine; m != NULL; m = m->next) {
1071 if (m->is_default) {
1072 return m;
1073 }
1074 }
1075 return NULL;
1076}
1077
bellardcc1daa42005-06-05 14:49:17 +00001078/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001079/* main execution loop */
1080
pbrook9596ebb2007-11-18 01:44:38 +00001081static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001082{
aliguori7d957bd2009-01-15 22:14:11 +00001083 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001084 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001085 DisplayChangeListener *dcl = ds->listeners;
1086
Sheng Yang62a27442010-01-26 19:21:16 +08001087 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001088 dpy_refresh(ds);
1089
1090 while (dcl != NULL) {
1091 if (dcl->gui_timer_interval &&
1092 dcl->gui_timer_interval < interval)
1093 interval = dcl->gui_timer_interval;
1094 dcl = dcl->next;
1095 }
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001096 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock_ms(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001097}
1098
blueswir19043b622009-01-21 19:28:13 +00001099static void nographic_update(void *opaque)
1100{
1101 uint64_t interval = GUI_REFRESH_INTERVAL;
1102
Sheng Yang62a27442010-01-26 19:21:16 +08001103 qemu_flush_coalesced_mmio_buffer();
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01001104 qemu_mod_timer(nographic_timer, interval + qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00001105}
1106
bellard0bd48852005-11-11 00:00:47 +00001107struct vm_change_state_entry {
1108 VMChangeStateHandler *cb;
1109 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001110 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001111};
1112
Blue Swirl72cf2d42009-09-12 07:36:22 +00001113static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001114
1115VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1116 void *opaque)
1117{
1118 VMChangeStateEntry *e;
1119
1120 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001121
1122 e->cb = cb;
1123 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001124 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001125 return e;
1126}
1127
1128void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1129{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001130 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001131 qemu_free (e);
1132}
1133
Blue Swirl296af7c2010-03-29 19:23:50 +00001134void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001135{
1136 VMChangeStateEntry *e;
1137
Stefan Hajnoczi94b0b5f2010-11-16 12:20:25 +00001138 trace_vm_state_notify(running, reason);
1139
bellard0bd48852005-11-11 00:00:47 +00001140 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001141 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001142 }
1143}
1144
bellard8a7ddc32004-03-31 19:00:16 +00001145void vm_start(void)
1146{
1147 if (!vm_running) {
1148 cpu_enable_ticks();
1149 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001150 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001151 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001152 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001153 }
1154}
1155
bellardbb0c6722004-06-20 12:37:32 +00001156/* reset/shutdown handler */
1157
1158typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001159 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001160 QEMUResetHandler *func;
1161 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001162} QEMUResetEntry;
1163
Blue Swirl72cf2d42009-09-12 07:36:22 +00001164static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1165 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001166static int reset_requested;
Gleb Natapovf64622c2011-03-15 13:56:04 +02001167static int shutdown_requested, shutdown_signal = -1;
1168static pid_t shutdown_pid;
bellard34751872005-07-02 14:31:34 +00001169static int powerdown_requested;
Jan Kiszka8cf71712011-02-07 12:19:16 +01001170static int debug_requested;
1171static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001172
Anthony PERARD1291eb32010-07-22 15:52:48 +01001173int qemu_shutdown_requested_get(void)
1174{
1175 return shutdown_requested;
1176}
1177
1178int qemu_reset_requested_get(void)
1179{
1180 return reset_requested;
1181}
1182
aurel32cf7a2fe2008-03-18 06:53:05 +00001183int qemu_shutdown_requested(void)
1184{
1185 int r = shutdown_requested;
1186 shutdown_requested = 0;
1187 return r;
1188}
1189
Gleb Natapovf64622c2011-03-15 13:56:04 +02001190void qemu_kill_report(void)
1191{
1192 if (shutdown_signal != -1) {
Peter Maydellf1d3fb02011-03-30 22:03:38 +01001193 fprintf(stderr, "qemu: terminating on signal %d", shutdown_signal);
1194 if (shutdown_pid == 0) {
1195 /* This happens for eg ^C at the terminal, so it's worth
1196 * avoiding printing an odd message in that case.
1197 */
1198 fputc('\n', stderr);
1199 } else {
1200 fprintf(stderr, " from pid %d\n", shutdown_pid);
1201 }
Gleb Natapovf64622c2011-03-15 13:56:04 +02001202 shutdown_signal = -1;
1203 }
1204}
1205
aurel32cf7a2fe2008-03-18 06:53:05 +00001206int qemu_reset_requested(void)
1207{
1208 int r = reset_requested;
1209 reset_requested = 0;
1210 return r;
1211}
1212
1213int qemu_powerdown_requested(void)
1214{
1215 int r = powerdown_requested;
1216 powerdown_requested = 0;
1217 return r;
1218}
1219
aliguorie5689022009-04-24 18:03:54 +00001220static int qemu_debug_requested(void)
1221{
1222 int r = debug_requested;
1223 debug_requested = 0;
1224 return r;
1225}
1226
aliguori6e29f5d2009-04-24 18:04:02 +00001227static int qemu_vmstop_requested(void)
1228{
1229 int r = vmstop_requested;
1230 vmstop_requested = 0;
1231 return r;
1232}
1233
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001234void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001235{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001236 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001237
bellardbb0c6722004-06-20 12:37:32 +00001238 re->func = func;
1239 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001240 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001241}
1242
Jan Kiszkadda9b292009-07-02 00:19:02 +02001243void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001244{
1245 QEMUResetEntry *re;
1246
Blue Swirl72cf2d42009-09-12 07:36:22 +00001247 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001248 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001249 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001250 qemu_free(re);
1251 return;
1252 }
1253 }
1254}
1255
1256void qemu_system_reset(void)
1257{
1258 QEMUResetEntry *re, *nre;
1259
1260 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001261 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001262 re->func(re->opaque);
1263 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001264 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001265 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001266}
1267
1268void qemu_system_reset_request(void)
1269{
bellardd1beab82006-10-02 19:44:22 +00001270 if (no_reboot) {
1271 shutdown_requested = 1;
1272 } else {
1273 reset_requested = 1;
1274 }
Jan Kiszkab4a3d962011-02-01 22:15:43 +01001275 cpu_stop_current();
aliguorid9f75a42009-04-24 18:03:11 +00001276 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001277}
1278
Gleb Natapovf64622c2011-03-15 13:56:04 +02001279void qemu_system_killed(int signal, pid_t pid)
1280{
1281 shutdown_signal = signal;
1282 shutdown_pid = pid;
1283 qemu_system_shutdown_request();
1284}
1285
bellardbb0c6722004-06-20 12:37:32 +00001286void qemu_system_shutdown_request(void)
1287{
1288 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001289 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001290}
1291
bellard34751872005-07-02 14:31:34 +00001292void qemu_system_powerdown_request(void)
1293{
1294 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001295 qemu_notify_event();
1296}
1297
Jan Kiszka8cf71712011-02-07 12:19:16 +01001298void qemu_system_debug_request(void)
1299{
1300 debug_requested = 1;
Jan Kiszka83f338f2011-02-07 12:19:17 +01001301 qemu_notify_event();
Jan Kiszka8cf71712011-02-07 12:19:16 +01001302}
1303
1304void qemu_system_vmstop_request(int reason)
1305{
1306 vmstop_requested = reason;
1307 qemu_notify_event();
1308}
1309
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001310void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001311{
aliguori56f3a5d2008-10-31 18:07:17 +00001312 fd_set rfds, wfds, xfds;
1313 int ret, nfds;
1314 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001315 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001316
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001317 if (nonblocking)
1318 timeout = 0;
1319 else {
1320 timeout = qemu_calculate_timeout();
1321 qemu_bh_update_timeout(&timeout);
1322 }
aliguori56f3a5d2008-10-31 18:07:17 +00001323
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001324 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001325
Paolo Bonzini02981412011-03-09 18:21:09 +01001326 tv.tv_sec = timeout / 1000;
1327 tv.tv_usec = (timeout % 1000) * 1000;
1328
bellardfd1dff42006-02-01 21:29:26 +00001329 /* poll any events */
1330 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001331 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001332 FD_ZERO(&rfds);
1333 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001334 FD_ZERO(&xfds);
Paolo Bonzini02981412011-03-09 18:21:09 +01001335 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
Jan Kiszkad918f232009-06-24 14:42:30 +02001336 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1337
aliguori48708522009-04-24 18:03:49 +00001338 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001339 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001340 qemu_mutex_lock_iothread();
thscafffd42007-02-28 21:59:44 +00001341
Paolo Bonzini02981412011-03-09 18:21:09 +01001342 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
Jan Kiszkad918f232009-06-24 14:42:30 +02001343 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001344
Paolo Bonzinib6964822010-03-10 11:38:45 +01001345 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001346
pbrook423f0742007-05-23 00:06:54 +00001347 /* Check bottom-halves last in case any of the earlier events triggered
1348 them. */
1349 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001350
bellard5905b2e2004-08-01 21:53:26 +00001351}
1352
Jan Kiszka46481d32011-02-01 22:15:47 +01001353#ifndef CONFIG_IOTHREAD
1354static int vm_request_pending(void)
aliguori43b96852009-04-24 18:03:33 +00001355{
Jan Kiszka46481d32011-02-01 22:15:47 +01001356 return powerdown_requested ||
1357 reset_requested ||
1358 shutdown_requested ||
1359 debug_requested ||
1360 vmstop_requested;
aliguori43b96852009-04-24 18:03:33 +00001361}
Jan Kiszka46481d32011-02-01 22:15:47 +01001362#endif
aliguori43b96852009-04-24 18:03:33 +00001363
Blue Swirld9c32312009-08-09 08:42:19 +00001364qemu_irq qemu_system_powerdown;
1365
aliguori43b96852009-04-24 18:03:33 +00001366static void main_loop(void)
1367{
Jan Kiszka8e1b90e2011-02-01 22:15:46 +01001368 bool nonblocking = false;
1369#ifdef CONFIG_PROFILER
1370 int64_t ti;
1371#endif
aliguori6e29f5d2009-04-24 18:04:02 +00001372 int r;
aliguori43b96852009-04-24 18:03:33 +00001373
Blue Swirl7277e022010-04-12 17:19:06 +00001374 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001375
aliguori6e29f5d2009-04-24 18:04:02 +00001376 for (;;) {
aliguorid6dc3d42009-04-24 18:04:07 +00001377#ifndef CONFIG_IOTHREAD
Jan Kiszka46481d32011-02-01 22:15:47 +01001378 nonblocking = cpu_exec_all();
1379 if (vm_request_pending()) {
1380 nonblocking = true;
1381 }
aliguorid6dc3d42009-04-24 18:04:07 +00001382#endif
aliguori43b96852009-04-24 18:03:33 +00001383#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001384 ti = profile_getclock();
aliguori43b96852009-04-24 18:03:33 +00001385#endif
Jan Kiszka46481d32011-02-01 22:15:47 +01001386 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001387#ifdef CONFIG_PROFILER
Jan Kiszka46481d32011-02-01 22:15:47 +01001388 dev_time += profile_getclock() - ti;
aliguori43b96852009-04-24 18:03:33 +00001389#endif
aliguori43b96852009-04-24 18:03:33 +00001390
Jan Kiszka8cf71712011-02-07 12:19:16 +01001391 if (qemu_debug_requested()) {
1392 vm_stop(VMSTOP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001393 }
aliguori43b96852009-04-24 18:03:33 +00001394 if (qemu_shutdown_requested()) {
Gleb Natapovf64622c2011-03-15 13:56:04 +02001395 qemu_kill_report();
Blue Swirl242cd002009-12-04 18:05:45 +00001396 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001397 if (no_shutdown) {
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001398 vm_stop(VMSTOP_SHUTDOWN);
aliguori43b96852009-04-24 18:03:33 +00001399 no_shutdown = 0;
1400 } else
1401 break;
1402 }
aliguorid6dc3d42009-04-24 18:04:07 +00001403 if (qemu_reset_requested()) {
1404 pause_all_vcpus();
Jan Kiszkaa7ada152011-03-02 08:56:11 +01001405 cpu_synchronize_all_states();
aliguori43b96852009-04-24 18:03:33 +00001406 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001407 resume_all_vcpus();
1408 }
Blue Swirld9c32312009-08-09 08:42:19 +00001409 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001410 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001411 qemu_irq_raise(qemu_system_powerdown);
1412 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001413 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001414 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001415 }
aliguori43b96852009-04-24 18:03:33 +00001416 }
MORITA Kazutaka2bc93fe2010-05-28 11:44:57 +09001417 bdrv_close_all();
aliguorid6dc3d42009-04-24 18:04:07 +00001418 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001419}
1420
pbrook9bd7e6d2009-04-07 22:58:45 +00001421static void version(void)
1422{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001423 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001424}
1425
ths15f82202007-06-29 23:26:08 +00001426static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001427{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001428 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001429#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1430 opt_help
blueswir15824d652009-03-28 06:44:27 +00001431#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001432#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001433#undef DEF
1434#undef DEFHEADING
1435#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001436 ;
1437 version();
1438 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001439 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001440 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001441 "\n"
1442 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001443 "During emulation, the following keys are useful:\n"
1444 "ctrl-alt-f toggle full screen\n"
1445 "ctrl-alt-n switch to virtual console 'n'\n"
1446 "ctrl-alt toggle mouse and keyboard grab\n"
1447 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001448 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1449 "qemu",
1450 options_help);
ths15f82202007-06-29 23:26:08 +00001451 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001452}
1453
bellardcd6f1162004-05-13 22:02:20 +00001454#define HAS_ARG 0x0001
1455
bellardcd6f1162004-05-13 22:02:20 +00001456typedef struct QEMUOption {
1457 const char *name;
1458 int flags;
1459 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001460 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001461} QEMUOption;
1462
blueswir1dbed7e42008-10-01 19:38:09 +00001463static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001464 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1465#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1466 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001467#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001468#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001469#undef DEF
1470#undef DEFHEADING
1471#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001472 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001473};
malc3893c122008-09-28 00:42:05 +00001474static void select_vgahw (const char *p)
1475{
1476 const char *opts;
1477
Gerd Hoffmann64465292009-12-08 13:11:45 +01001478 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001479 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001480 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001481 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001482 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001483 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001484 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001485 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001486 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001487 vga_interface_type = VGA_XENFB;
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02001488 } else if (strstart(p, "qxl", &opts)) {
1489 vga_interface_type = VGA_QXL;
aliguori28b85ed2009-04-22 15:19:48 +00001490 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001491 invalid_vga:
1492 fprintf(stderr, "Unknown vga type: %s\n", p);
1493 exit(1);
1494 }
malccb5a7aa2008-09-28 00:42:12 +00001495 while (*opts) {
1496 const char *nextopt;
1497
1498 if (strstart(opts, ",retrace=", &nextopt)) {
1499 opts = nextopt;
1500 if (strstart(opts, "dumb", &nextopt))
1501 vga_retrace_method = VGA_RETRACE_DUMB;
1502 else if (strstart(opts, "precise", &nextopt))
1503 vga_retrace_method = VGA_RETRACE_PRECISE;
1504 else goto invalid_vga;
1505 } else goto invalid_vga;
1506 opts = nextopt;
1507 }
malc3893c122008-09-28 00:42:05 +00001508}
1509
Jes Sorensen1472a952011-03-16 13:33:31 +01001510static DisplayType select_display(const char *p)
1511{
1512 const char *opts;
1513 DisplayType display = DT_DEFAULT;
1514
1515 if (strstart(p, "sdl", &opts)) {
1516#ifdef CONFIG_SDL
1517 display = DT_SDL;
1518 while (*opts) {
1519 const char *nextopt;
1520
1521 if (strstart(opts, ",frame=", &nextopt)) {
1522 opts = nextopt;
1523 if (strstart(opts, "on", &nextopt)) {
1524 no_frame = 0;
1525 } else if (strstart(opts, "off", &nextopt)) {
1526 no_frame = 1;
1527 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001528 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001529 }
1530 } else if (strstart(opts, ",alt_grab=", &nextopt)) {
1531 opts = nextopt;
1532 if (strstart(opts, "on", &nextopt)) {
1533 alt_grab = 1;
1534 } else if (strstart(opts, "off", &nextopt)) {
1535 alt_grab = 0;
1536 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001537 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001538 }
1539 } else if (strstart(opts, ",ctrl_grab=", &nextopt)) {
1540 opts = nextopt;
1541 if (strstart(opts, "on", &nextopt)) {
1542 ctrl_grab = 1;
1543 } else if (strstart(opts, "off", &nextopt)) {
1544 ctrl_grab = 0;
1545 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001546 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001547 }
1548 } else if (strstart(opts, ",window_close=", &nextopt)) {
1549 opts = nextopt;
1550 if (strstart(opts, "on", &nextopt)) {
1551 no_quit = 0;
1552 } else if (strstart(opts, "off", &nextopt)) {
1553 no_quit = 1;
1554 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001555 goto invalid_sdl_args;
Jes Sorensen1472a952011-03-16 13:33:31 +01001556 }
1557 } else {
Peter Maydell05175532011-03-23 03:40:57 +00001558 invalid_sdl_args:
1559 fprintf(stderr, "Invalid SDL option string: %s\n", p);
1560 exit(1);
Jes Sorensen1472a952011-03-16 13:33:31 +01001561 }
1562 opts = nextopt;
1563 }
1564#else
1565 fprintf(stderr, "SDL support is disabled\n");
1566 exit(1);
1567#endif
Jes Sorensen3264ff12011-03-16 13:33:33 +01001568 } else if (strstart(p, "vnc", &opts)) {
Jes Sorensen821601e2011-03-16 13:33:36 +01001569#ifdef CONFIG_VNC
Jes Sorensen3264ff12011-03-16 13:33:33 +01001570 display_remote++;
1571
1572 if (*opts) {
1573 const char *nextopt;
1574
1575 if (strstart(opts, "=", &nextopt)) {
1576 vnc_display = nextopt;
1577 }
1578 }
1579 if (!vnc_display) {
1580 fprintf(stderr, "VNC requires a display argument vnc=<display>\n");
1581 exit(1);
1582 }
Jes Sorensen821601e2011-03-16 13:33:36 +01001583#else
1584 fprintf(stderr, "VNC support is disabled\n");
1585 exit(1);
1586#endif
Jes Sorensen1472a952011-03-16 13:33:31 +01001587 } else if (strstart(p, "curses", &opts)) {
1588#ifdef CONFIG_CURSES
1589 display = DT_CURSES;
1590#else
1591 fprintf(stderr, "Curses support is disabled\n");
1592 exit(1);
1593#endif
Jes Sorensen4171d322011-03-16 13:33:32 +01001594 } else if (strstart(p, "none", &opts)) {
1595 display = DT_NONE;
Jes Sorensen1472a952011-03-16 13:33:31 +01001596 } else {
Jes Sorensen1472a952011-03-16 13:33:31 +01001597 fprintf(stderr, "Unknown display type: %s\n", p);
1598 exit(1);
1599 }
1600
1601 return display;
1602}
1603
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001604static int balloon_parse(const char *arg)
1605{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001606 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001607
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001608 if (strcmp(arg, "none") == 0) {
1609 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001610 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001611
1612 if (!strncmp(arg, "virtio", 6)) {
1613 if (arg[6] == ',') {
1614 /* have params -> parse them */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001615 opts = qemu_opts_parse(qemu_find_opts("device"), arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001616 if (!opts)
1617 return -1;
1618 } else {
1619 /* create empty opts */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001620 opts = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001621 }
Alexander Graf29f82b32011-03-29 15:29:29 +02001622 qemu_opt_set(opts, "driver", "virtio-balloon");
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001623 return 0;
1624 }
1625
1626 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001627}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001628
Paul Brook5cea8592009-05-30 00:52:44 +01001629char *qemu_find_file(int type, const char *name)
1630{
1631 int len;
1632 const char *subdir;
1633 char *buf;
1634
1635 /* If name contains path separators then try it as a straight path. */
1636 if ((strchr(name, '/') || strchr(name, '\\'))
1637 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001638 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001639 }
1640 switch (type) {
1641 case QEMU_FILE_TYPE_BIOS:
1642 subdir = "";
1643 break;
1644 case QEMU_FILE_TYPE_KEYMAP:
1645 subdir = "keymaps/";
1646 break;
1647 default:
1648 abort();
1649 }
1650 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1651 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001652 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01001653 if (access(buf, R_OK)) {
1654 qemu_free(buf);
1655 return NULL;
1656 }
1657 return buf;
1658}
1659
Markus Armbrusterff952ba2010-01-29 19:48:57 +01001660static int device_help_func(QemuOpts *opts, void *opaque)
1661{
1662 return qdev_device_help(opts);
1663}
1664
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02001665static int device_init_func(QemuOpts *opts, void *opaque)
1666{
1667 DeviceState *dev;
1668
1669 dev = qdev_device_add(opts);
1670 if (!dev)
1671 return -1;
1672 return 0;
1673}
1674
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01001675static int chardev_init_func(QemuOpts *opts, void *opaque)
1676{
1677 CharDriverState *chr;
1678
1679 chr = qemu_chr_open_opts(opts, NULL);
1680 if (!chr)
1681 return -1;
1682 return 0;
1683}
1684
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07001685#ifdef CONFIG_VIRTFS
Gautham R Shenoy74db9202010-04-29 17:44:43 +05301686static int fsdev_init_func(QemuOpts *opts, void *opaque)
1687{
1688 int ret;
1689 ret = qemu_fsdev_add(opts);
1690
1691 return ret;
1692}
1693#endif
1694
Gerd Hoffmann88589342009-12-08 13:11:50 +01001695static int mon_init_func(QemuOpts *opts, void *opaque)
1696{
1697 CharDriverState *chr;
1698 const char *chardev;
1699 const char *mode;
1700 int flags;
1701
1702 mode = qemu_opt_get(opts, "mode");
1703 if (mode == NULL) {
1704 mode = "readline";
1705 }
1706 if (strcmp(mode, "readline") == 0) {
1707 flags = MONITOR_USE_READLINE;
1708 } else if (strcmp(mode, "control") == 0) {
1709 flags = MONITOR_USE_CONTROL;
1710 } else {
1711 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
1712 exit(1);
1713 }
1714
Daniel P. Berrange39eaab92010-06-07 15:42:31 +01001715 if (qemu_opt_get_bool(opts, "pretty", 0))
1716 flags |= MONITOR_USE_PRETTY;
1717
Gerd Hoffmann88589342009-12-08 13:11:50 +01001718 if (qemu_opt_get_bool(opts, "default", 0))
1719 flags |= MONITOR_IS_DEFAULT;
1720
1721 chardev = qemu_opt_get(opts, "chardev");
1722 chr = qemu_chr_find(chardev);
1723 if (chr == NULL) {
1724 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
1725 exit(1);
1726 }
1727
1728 monitor_init(chr, flags);
1729 return 0;
1730}
1731
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001732static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01001733{
1734 static int monitor_device_index = 0;
1735 QemuOpts *opts;
1736 const char *p;
1737 char label[32];
1738 int def = 0;
1739
1740 if (strstart(optarg, "chardev:", &p)) {
1741 snprintf(label, sizeof(label), "%s", p);
1742 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02001743 snprintf(label, sizeof(label), "compat_monitor%d",
1744 monitor_device_index);
1745 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01001746 def = 1;
1747 }
1748 opts = qemu_chr_parse_compat(label, optarg);
1749 if (!opts) {
1750 fprintf(stderr, "parse error: %s\n", optarg);
1751 exit(1);
1752 }
1753 }
1754
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001755 opts = qemu_opts_create(qemu_find_opts("mon"), label, 1);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001756 if (!opts) {
1757 fprintf(stderr, "duplicate chardev: %s\n", label);
1758 exit(1);
1759 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01001760 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01001761 qemu_opt_set(opts, "chardev", label);
1762 if (def)
1763 qemu_opt_set(opts, "default", "on");
1764 monitor_device_index++;
1765}
1766
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001767struct device_config {
1768 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001769 DEV_USB, /* -usbdevice */
1770 DEV_BT, /* -bt */
1771 DEV_SERIAL, /* -serial */
1772 DEV_PARALLEL, /* -parallel */
1773 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001774 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001775 } type;
1776 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001777 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001778};
Blue Swirl72cf2d42009-09-12 07:36:22 +00001779QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001780
1781static void add_device_config(int type, const char *cmdline)
1782{
1783 struct device_config *conf;
1784
1785 conf = qemu_mallocz(sizeof(*conf));
1786 conf->type = type;
1787 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001788 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001789}
1790
1791static int foreach_device_config(int type, int (*func)(const char *cmdline))
1792{
1793 struct device_config *conf;
1794 int rc;
1795
Blue Swirl72cf2d42009-09-12 07:36:22 +00001796 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001797 if (conf->type != type)
1798 continue;
1799 rc = func(conf->cmdline);
1800 if (0 != rc)
1801 return rc;
1802 }
1803 return 0;
1804}
1805
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01001806static int serial_parse(const char *devname)
1807{
1808 static int index = 0;
1809 char label[32];
1810
1811 if (strcmp(devname, "none") == 0)
1812 return 0;
1813 if (index == MAX_SERIAL_PORTS) {
1814 fprintf(stderr, "qemu: too many serial ports\n");
1815 exit(1);
1816 }
1817 snprintf(label, sizeof(label), "serial%d", index);
1818 serial_hds[index] = qemu_chr_open(label, devname, NULL);
1819 if (!serial_hds[index]) {
1820 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
1821 devname, strerror(errno));
1822 return -1;
1823 }
1824 index++;
1825 return 0;
1826}
1827
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01001828static int parallel_parse(const char *devname)
1829{
1830 static int index = 0;
1831 char label[32];
1832
1833 if (strcmp(devname, "none") == 0)
1834 return 0;
1835 if (index == MAX_PARALLEL_PORTS) {
1836 fprintf(stderr, "qemu: too many parallel ports\n");
1837 exit(1);
1838 }
1839 snprintf(label, sizeof(label), "parallel%d", index);
1840 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
1841 if (!parallel_hds[index]) {
1842 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
1843 devname, strerror(errno));
1844 return -1;
1845 }
1846 index++;
1847 return 0;
1848}
1849
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001850static int virtcon_parse(const char *devname)
1851{
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001852 QemuOptsList *device = qemu_find_opts("device");
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001853 static int index = 0;
1854 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05301855 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001856
1857 if (strcmp(devname, "none") == 0)
1858 return 0;
1859 if (index == MAX_VIRTIO_CONSOLES) {
1860 fprintf(stderr, "qemu: too many virtio consoles\n");
1861 exit(1);
1862 }
Amit Shah392ecf52010-01-21 16:19:23 +05301863
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001864 bus_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301865 qemu_opt_set(bus_opts, "driver", "virtio-serial");
1866
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001867 dev_opts = qemu_opts_create(device, NULL, 0);
Amit Shah392ecf52010-01-21 16:19:23 +05301868 qemu_opt_set(dev_opts, "driver", "virtconsole");
1869
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001870 snprintf(label, sizeof(label), "virtcon%d", index);
1871 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
1872 if (!virtcon_hds[index]) {
1873 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
1874 devname, strerror(errno));
1875 return -1;
1876 }
Amit Shah392ecf52010-01-21 16:19:23 +05301877 qemu_opt_set(dev_opts, "chardev", label);
1878
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01001879 index++;
1880 return 0;
1881}
1882
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001883static int debugcon_parse(const char *devname)
1884{
1885 QemuOpts *opts;
1886
1887 if (!qemu_chr_open("debugcon", devname, NULL)) {
1888 exit(1);
1889 }
Gerd Hoffmann3329f072010-08-20 13:52:01 +02001890 opts = qemu_opts_create(qemu_find_opts("device"), "debugcon", 1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08001891 if (!opts) {
1892 fprintf(stderr, "qemu: already have a debugcon device\n");
1893 exit(1);
1894 }
1895 qemu_opt_set(opts, "driver", "isa-debugcon");
1896 qemu_opt_set(opts, "chardev", "debugcon");
1897 return 0;
1898}
1899
Anthony PERARD303d4e82010-09-21 20:05:31 +01001900static int tcg_init(void)
1901{
1902 return 0;
1903}
1904
1905static struct {
1906 const char *opt_name;
1907 const char *name;
1908 int (*available)(void);
1909 int (*init)(void);
1910 int *allowed;
1911} accel_list[] = {
1912 { "tcg", "tcg", tcg_available, tcg_init, &tcg_allowed },
Anthony PERARD3285cf42010-08-19 12:27:56 +01001913 { "xen", "Xen", xen_available, xen_init, &xen_allowed },
Anthony PERARD303d4e82010-09-21 20:05:31 +01001914 { "kvm", "KVM", kvm_available, kvm_init, &kvm_allowed },
1915};
1916
1917static int configure_accelerator(void)
1918{
1919 const char *p = NULL;
1920 char buf[10];
1921 int i, ret;
1922 bool accel_initalised = 0;
1923 bool init_failed = 0;
1924
1925 QemuOptsList *list = qemu_find_opts("machine");
1926 if (!QTAILQ_EMPTY(&list->head)) {
1927 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
1928 }
1929
1930 if (p == NULL) {
1931 /* Use the default "accelerator", tcg */
1932 p = "tcg";
1933 }
1934
1935 while (!accel_initalised && *p != '\0') {
1936 if (*p == ':') {
1937 p++;
1938 }
1939 p = get_opt_name(buf, sizeof (buf), p, ':');
1940 for (i = 0; i < ARRAY_SIZE(accel_list); i++) {
1941 if (strcmp(accel_list[i].opt_name, buf) == 0) {
Anthony Liguoria16c53b2011-06-06 08:25:06 -05001942 *(accel_list[i].allowed) = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01001943 ret = accel_list[i].init();
1944 if (ret < 0) {
1945 init_failed = 1;
1946 if (!accel_list[i].available()) {
1947 printf("%s not supported for this target\n",
1948 accel_list[i].name);
1949 } else {
1950 fprintf(stderr, "failed to initialize %s: %s\n",
1951 accel_list[i].name,
1952 strerror(-ret));
1953 }
Anthony Liguoria16c53b2011-06-06 08:25:06 -05001954 *(accel_list[i].allowed) = 0;
Anthony PERARD303d4e82010-09-21 20:05:31 +01001955 } else {
1956 accel_initalised = 1;
Anthony PERARD303d4e82010-09-21 20:05:31 +01001957 }
1958 break;
1959 }
1960 }
1961 if (i == ARRAY_SIZE(accel_list)) {
1962 fprintf(stderr, "\"%s\" accelerator does not exist.\n", buf);
1963 }
1964 }
1965
1966 if (!accel_initalised) {
1967 fprintf(stderr, "No accelerator found!\n");
1968 exit(1);
1969 }
1970
1971 if (init_failed) {
1972 fprintf(stderr, "Back to %s accelerator.\n", accel_list[i].name);
1973 }
1974
1975 return !accel_initalised;
1976}
1977
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02001978void qemu_add_exit_notifier(Notifier *notify)
1979{
1980 notifier_list_add(&exit_notifiers, notify);
1981}
1982
1983void qemu_remove_exit_notifier(Notifier *notify)
1984{
1985 notifier_list_remove(&exit_notifiers, notify);
1986}
1987
1988static void qemu_run_exit_notifiers(void)
1989{
1990 notifier_list_notify(&exit_notifiers);
1991}
1992
Gleb Natapov4cab9462010-12-08 13:35:08 +02001993void qemu_add_machine_init_done_notifier(Notifier *notify)
1994{
1995 notifier_list_add(&machine_init_done_notifiers, notify);
1996}
1997
1998static void qemu_run_machine_init_done_notifiers(void)
1999{
2000 notifier_list_notify(&machine_init_done_notifiers);
2001}
2002
Anthony Liguori6530a972010-01-22 09:18:06 -06002003static const QEMUOption *lookup_opt(int argc, char **argv,
2004 const char **poptarg, int *poptind)
2005{
2006 const QEMUOption *popt;
2007 int optind = *poptind;
2008 char *r = argv[optind];
2009 const char *optarg;
2010
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002011 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002012 optind++;
2013 /* Treat --foo the same as -foo. */
2014 if (r[1] == '-')
2015 r++;
2016 popt = qemu_options;
2017 for(;;) {
2018 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002019 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002020 exit(1);
2021 }
2022 if (!strcmp(popt->name, r + 1))
2023 break;
2024 popt++;
2025 }
2026 if (popt->flags & HAS_ARG) {
2027 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002028 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002029 exit(1);
2030 }
2031 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002032 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002033 } else {
2034 optarg = NULL;
2035 }
2036
2037 *poptarg = optarg;
2038 *poptind = optind;
2039
2040 return popt;
2041}
2042
malc902b3d52008-12-10 19:18:40 +00002043int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002044{
aliguori59030a82009-04-05 18:43:41 +00002045 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002046 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002047 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002048 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002049 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002050 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002051 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f3322009-01-16 19:04:14 +00002052 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002053 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002054 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002055 QemuOpts *hda_opts = NULL, *opts;
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002056 QemuOptsList *olist;
bellardcd6f1162004-05-13 22:02:20 +00002057 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002058 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002059 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002060 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002061 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00002062 int tb_size;
ths93815bc2007-03-19 15:58:31 +00002063 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002064 const char *incoming = NULL;
Jes Sorensen821601e2011-03-16 13:33:36 +01002065#ifdef CONFIG_VNC
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002066 int show_vnc_port = 0;
Jes Sorensen821601e2011-03-16 13:33:36 +01002067#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06002068 int defconfig = 1;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002069 const char *trace_file = NULL;
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002070
Gerd Hoffmannfd42dee2010-06-04 14:08:07 +02002071 atexit(qemu_run_exit_notifiers);
Markus Armbruster65abca02010-02-24 14:37:14 +01002072 error_set_progname(argv[0]);
2073
Jan Kiszka68752042009-09-15 13:36:04 +02002074 init_clocks();
2075
malc902b3d52008-12-10 19:18:40 +00002076 qemu_cache_utils_init(envp);
2077
Blue Swirl72cf2d42009-09-12 07:36:22 +00002078 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002079 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002080
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002081 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002082 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002083 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002084 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002085 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002086 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002087 kernel_filename = NULL;
2088 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002089 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002090 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002091
aliguori268a3622009-04-21 22:30:27 +00002092 for (i = 0; i < MAX_NODES; i++) {
2093 node_mem[i] = 0;
2094 node_cpumask[i] = 0;
2095 }
2096
aliguori268a3622009-04-21 22:30:27 +00002097 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002098 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002099
bellard26a5f132008-05-28 12:30:31 +00002100 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00002101 autostart= 1;
2102
Anthony Liguori292444c2010-01-21 10:57:58 -06002103 /* first pass of option parsing */
2104 optind = 1;
2105 while (optind < argc) {
2106 if (argv[optind][0] != '-') {
2107 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002108 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002109 continue;
2110 } else {
2111 const QEMUOption *popt;
2112
2113 popt = lookup_opt(argc, argv, &optarg, &optind);
2114 switch (popt->index) {
2115 case QEMU_OPTION_nodefconfig:
2116 defconfig=0;
2117 break;
2118 }
2119 }
2120 }
2121
2122 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002123 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002124
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002125 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002126 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002127 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002128 }
2129
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002130 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002131 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002132 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002133 }
2134 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002135 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002136
2137 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002138 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002139 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002140 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002141 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002142 if (argv[optind][0] != '-') {
Markus Armbruster2292dda2011-01-28 11:21:41 +01002143 hda_opts = drive_add(IF_DEFAULT, 0, argv[optind++], HD_OPTS);
bellardcd6f1162004-05-13 22:02:20 +00002144 } else {
2145 const QEMUOption *popt;
2146
Anthony Liguori6530a972010-01-22 09:18:06 -06002147 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002148 if (!(popt->arch_mask & arch_type)) {
2149 printf("Option %s not supported for this target\n", popt->name);
2150 exit(1);
2151 }
bellardcd6f1162004-05-13 22:02:20 +00002152 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002153 case QEMU_OPTION_M:
2154 machine = find_machine(optarg);
2155 if (!machine) {
2156 QEMUMachine *m;
2157 printf("Supported machines are:\n");
2158 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002159 if (m->alias)
2160 printf("%-10s %s (alias of %s)\n",
2161 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00002162 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00002163 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05002164 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00002165 }
ths15f82202007-06-29 23:26:08 +00002166 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00002167 }
2168 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002169 case QEMU_OPTION_cpu:
2170 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002171 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002172 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002173 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002174 } else {
2175 cpu_model = optarg;
2176 }
2177 break;
bellardcd6f1162004-05-13 22:02:20 +00002178 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002179 initrd_filename = optarg;
2180 break;
bellardcd6f1162004-05-13 22:02:20 +00002181 case QEMU_OPTION_hda:
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002182 {
2183 char buf[256];
2184 if (cyls == 0)
2185 snprintf(buf, sizeof(buf), "%s", HD_OPTS);
2186 else
2187 snprintf(buf, sizeof(buf),
2188 "%s,cyls=%d,heads=%d,secs=%d%s",
2189 HD_OPTS , cyls, heads, secs,
2190 translation == BIOS_ATA_TRANSLATION_LBA ?
thse4bcb142007-12-02 04:51:10 +00002191 ",trans=lba" :
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002192 translation == BIOS_ATA_TRANSLATION_NONE ?
thse4bcb142007-12-02 04:51:10 +00002193 ",trans=none" : "");
Markus Armbruster5645b0f2011-01-31 11:50:09 +01002194 drive_add(IF_DEFAULT, 0, optarg, buf);
2195 break;
2196 }
bellardcd6f1162004-05-13 22:02:20 +00002197 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002198 case QEMU_OPTION_hdc:
2199 case QEMU_OPTION_hdd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002200 drive_add(IF_DEFAULT, popt->index - QEMU_OPTION_hda, optarg,
2201 HD_OPTS);
bellardfc01f7e2003-06-30 10:03:06 +00002202 break;
thse4bcb142007-12-02 04:51:10 +00002203 case QEMU_OPTION_drive:
Michael Tokareve2982c32011-03-30 16:31:05 +04002204 if (drive_def(optarg) == NULL) {
2205 exit(1);
2206 }
thse4bcb142007-12-02 04:51:10 +00002207 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002208 case QEMU_OPTION_set:
2209 if (qemu_set_option(optarg) != 0)
2210 exit(1);
2211 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002212 case QEMU_OPTION_global:
2213 if (qemu_global_option(optarg) != 0)
2214 exit(1);
2215 break;
balrog3e3d5812007-04-30 02:09:25 +00002216 case QEMU_OPTION_mtdblock:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002217 drive_add(IF_MTD, -1, optarg, MTD_OPTS);
balrog3e3d5812007-04-30 02:09:25 +00002218 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002219 case QEMU_OPTION_sd:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002220 drive_add(IF_SD, 0, optarg, SD_OPTS);
pbrooka1bb27b2007-04-06 16:49:48 +00002221 break;
j_mayer86f55662007-04-24 06:52:59 +00002222 case QEMU_OPTION_pflash:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002223 drive_add(IF_PFLASH, -1, optarg, PFLASH_OPTS);
j_mayer86f55662007-04-24 06:52:59 +00002224 break;
bellardcd6f1162004-05-13 22:02:20 +00002225 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002226 snapshot = 1;
2227 break;
bellardcd6f1162004-05-13 22:02:20 +00002228 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002229 {
bellard330d0412003-07-26 18:11:40 +00002230 const char *p;
2231 p = optarg;
2232 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002233 if (cyls < 1 || cyls > 16383)
2234 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002235 if (*p != ',')
2236 goto chs_fail;
2237 p++;
2238 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002239 if (heads < 1 || heads > 16)
2240 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002241 if (*p != ',')
2242 goto chs_fail;
2243 p++;
2244 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002245 if (secs < 1 || secs > 63)
2246 goto chs_fail;
2247 if (*p == ',') {
2248 p++;
2249 if (!strcmp(p, "none"))
2250 translation = BIOS_ATA_TRANSLATION_NONE;
2251 else if (!strcmp(p, "lba"))
2252 translation = BIOS_ATA_TRANSLATION_LBA;
2253 else if (!strcmp(p, "auto"))
2254 translation = BIOS_ATA_TRANSLATION_AUTO;
2255 else
2256 goto chs_fail;
2257 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002258 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002259 fprintf(stderr, "qemu: invalid physical CHS format\n");
2260 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002261 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002262 if (hda_opts != NULL) {
2263 char num[16];
2264 snprintf(num, sizeof(num), "%d", cyls);
2265 qemu_opt_set(hda_opts, "cyls", num);
2266 snprintf(num, sizeof(num), "%d", heads);
2267 qemu_opt_set(hda_opts, "heads", num);
2268 snprintf(num, sizeof(num), "%d", secs);
2269 qemu_opt_set(hda_opts, "secs", num);
2270 if (translation == BIOS_ATA_TRANSLATION_LBA)
2271 qemu_opt_set(hda_opts, "trans", "lba");
2272 if (translation == BIOS_ATA_TRANSLATION_NONE)
2273 qemu_opt_set(hda_opts, "trans", "none");
2274 }
bellard330d0412003-07-26 18:11:40 +00002275 }
2276 break;
aliguori268a3622009-04-21 22:30:27 +00002277 case QEMU_OPTION_numa:
2278 if (nb_numa_nodes >= MAX_NODES) {
2279 fprintf(stderr, "qemu: too many NUMA nodes\n");
2280 exit(1);
2281 }
2282 numa_add(optarg);
2283 break;
Jes Sorensen1472a952011-03-16 13:33:31 +01002284 case QEMU_OPTION_display:
2285 display_type = select_display(optarg);
2286 break;
bellardcd6f1162004-05-13 22:02:20 +00002287 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002288 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002289 break;
balrog4d3b6f62008-02-10 16:33:14 +00002290 case QEMU_OPTION_curses:
Jes Sorensen47b05362011-03-16 13:33:35 +01002291#ifdef CONFIG_CURSES
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002292 display_type = DT_CURSES;
Jes Sorensen47b05362011-03-16 13:33:35 +01002293#else
2294 fprintf(stderr, "Curses support is disabled\n");
2295 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00002296#endif
Jes Sorensen47b05362011-03-16 13:33:35 +01002297 break;
balroga171fe32007-04-30 01:48:07 +00002298 case QEMU_OPTION_portrait:
2299 graphic_rotate = 1;
2300 break;
bellardcd6f1162004-05-13 22:02:20 +00002301 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002302 kernel_filename = optarg;
2303 break;
bellardcd6f1162004-05-13 22:02:20 +00002304 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002305 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002306 break;
bellardcd6f1162004-05-13 22:02:20 +00002307 case QEMU_OPTION_cdrom:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002308 drive_add(IF_DEFAULT, 2, optarg, CDROM_OPTS);
bellard36b486b2003-11-11 13:36:08 +00002309 break;
bellardcd6f1162004-05-13 22:02:20 +00002310 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002311 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002312 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002313 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002314 };
2315 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002316 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002317 int legacy = 0;
2318
2319 if (!strchr(optarg, '=')) {
2320 legacy = 1;
2321 pstrcpy(buf, sizeof(buf), optarg);
2322 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2323 fprintf(stderr,
2324 "qemu: unknown boot parameter '%s' in '%s'\n",
2325 buf, optarg);
2326 exit(1);
2327 }
2328
2329 if (legacy ||
2330 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002331 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002332 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002333 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002334 if (!legacy) {
2335 if (get_param_value(buf, sizeof(buf),
2336 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002337 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002338 standard_boot_devices = qemu_strdup(boot_devices);
2339 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2340 qemu_register_reset(restore_boot_devices,
2341 standard_boot_devices);
2342 }
Jan Kiszka95387492009-07-02 00:19:02 +02002343 if (get_param_value(buf, sizeof(buf),
2344 "menu", optarg)) {
2345 if (!strcmp(buf, "on")) {
2346 boot_menu = 1;
2347 } else if (!strcmp(buf, "off")) {
2348 boot_menu = 0;
2349 } else {
2350 fprintf(stderr,
2351 "qemu: invalid option value '%s'\n",
2352 buf);
2353 exit(1);
2354 }
2355 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002356 }
bellard36b486b2003-11-11 13:36:08 +00002357 }
2358 break;
bellardcd6f1162004-05-13 22:02:20 +00002359 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002360 case QEMU_OPTION_fdb:
Markus Armbruster2292dda2011-01-28 11:21:41 +01002361 drive_add(IF_FLOPPY, popt->index - QEMU_OPTION_fda,
2362 optarg, FD_OPTS);
bellardc45886d2004-01-05 00:02:06 +00002363 break;
bellard52ca8d62006-06-14 16:03:05 +00002364 case QEMU_OPTION_no_fd_bootchk:
2365 fd_bootchk = 0;
2366 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002367 case QEMU_OPTION_netdev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002368 if (net_client_parse(qemu_find_opts("netdev"), optarg) == -1) {
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002369 exit(1);
2370 }
2371 break;
bellard7c9d8e02005-11-15 22:16:05 +00002372 case QEMU_OPTION_net:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002373 if (net_client_parse(qemu_find_opts("net"), optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002374 exit(1);
2375 }
bellard702c6512004-04-02 21:21:32 +00002376 break;
bellardc7f74642004-08-24 21:57:12 +00002377#ifdef CONFIG_SLIRP
2378 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002379 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002380 break;
ths47d5d012007-02-20 00:05:08 +00002381 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002382 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002383 break;
bellard9bf05442004-08-25 22:12:49 +00002384 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002385 if (net_slirp_redir(optarg) < 0)
2386 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002387 break;
bellardc7f74642004-08-24 21:57:12 +00002388#endif
balrogdc72ac12008-11-09 00:04:26 +00002389 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002390 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002391 break;
bellard1d14ffa2005-10-30 18:58:22 +00002392 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002393 if (!(audio_available())) {
2394 printf("Option %s not supported for this target\n", popt->name);
2395 exit(1);
2396 }
bellard1d14ffa2005-10-30 18:58:22 +00002397 AUD_help ();
2398 exit (0);
2399 break;
2400 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002401 if (!(audio_available())) {
2402 printf("Option %s not supported for this target\n", popt->name);
2403 exit(1);
2404 }
bellard1d14ffa2005-10-30 18:58:22 +00002405 select_soundhw (optarg);
2406 break;
bellardcd6f1162004-05-13 22:02:20 +00002407 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002408 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002409 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002410 case QEMU_OPTION_version:
2411 version();
2412 exit(0);
2413 break;
aurel3200f82b82008-04-27 21:12:55 +00002414 case QEMU_OPTION_m: {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +01002415 int64_t value;
aurel3200f82b82008-04-27 21:12:55 +00002416
Jes Sorensen9f9b17a2010-10-21 17:15:46 +02002417 value = strtosz(optarg, NULL);
2418 if (value < 0) {
aurel3200f82b82008-04-27 21:12:55 +00002419 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002420 exit(1);
2421 }
aurel3200f82b82008-04-27 21:12:55 +00002422
2423 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002424 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002425 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2426 exit(1);
2427 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002428 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002429 fprintf(stderr, "qemu: ram size too large\n");
2430 exit(1);
2431 }
2432 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002433 break;
aurel3200f82b82008-04-27 21:12:55 +00002434 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002435 case QEMU_OPTION_mempath:
2436 mem_path = optarg;
2437 break;
2438#ifdef MAP_POPULATE
2439 case QEMU_OPTION_mem_prealloc:
2440 mem_prealloc = 1;
2441 break;
2442#endif
bellardcd6f1162004-05-13 22:02:20 +00002443 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002444 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002445 break;
bellardcd6f1162004-05-13 22:02:20 +00002446 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002447 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002448 break;
aliguori59030a82009-04-05 18:43:41 +00002449 case QEMU_OPTION_gdb:
2450 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002451 break;
bellardcd6f1162004-05-13 22:02:20 +00002452 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002453 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002454 break;
j_mayer1192dad2007-10-05 13:08:35 +00002455 case QEMU_OPTION_bios:
2456 bios_name = optarg;
2457 break;
aurel321b530a62009-04-05 20:08:59 +00002458 case QEMU_OPTION_singlestep:
2459 singlestep = 1;
2460 break;
bellardcd6f1162004-05-13 22:02:20 +00002461 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002462 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002463 break;
bellard3d11d0e2004-12-12 16:56:30 +00002464 case QEMU_OPTION_k:
2465 keyboard_layout = optarg;
2466 break;
bellardee22c2f2004-06-03 12:49:50 +00002467 case QEMU_OPTION_localtime:
2468 rtc_utc = 0;
2469 break;
malc3893c122008-09-28 00:42:05 +00002470 case QEMU_OPTION_vga:
2471 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002472 break;
bellarde9b137c2004-06-21 16:46:10 +00002473 case QEMU_OPTION_g:
2474 {
2475 const char *p;
2476 int w, h, depth;
2477 p = optarg;
2478 w = strtol(p, (char **)&p, 10);
2479 if (w <= 0) {
2480 graphic_error:
2481 fprintf(stderr, "qemu: invalid resolution or depth\n");
2482 exit(1);
2483 }
2484 if (*p != 'x')
2485 goto graphic_error;
2486 p++;
2487 h = strtol(p, (char **)&p, 10);
2488 if (h <= 0)
2489 goto graphic_error;
2490 if (*p == 'x') {
2491 p++;
2492 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002493 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002494 depth != 24 && depth != 32)
2495 goto graphic_error;
2496 } else if (*p == '\0') {
2497 depth = graphic_depth;
2498 } else {
2499 goto graphic_error;
2500 }
ths3b46e622007-09-17 08:09:54 +00002501
bellarde9b137c2004-06-21 16:46:10 +00002502 graphic_width = w;
2503 graphic_height = h;
2504 graphic_depth = depth;
2505 }
2506 break;
ths20d8a3e2007-02-18 17:04:49 +00002507 case QEMU_OPTION_echr:
2508 {
2509 char *r;
2510 term_escape_char = strtol(optarg, &r, 0);
2511 if (r == optarg)
2512 printf("Bad argument to echr\n");
2513 break;
2514 }
bellard82c643f2004-07-14 17:28:13 +00002515 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002516 monitor_parse(optarg, "readline");
2517 default_monitor = 0;
2518 break;
2519 case QEMU_OPTION_qmp:
2520 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002521 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002522 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002523 case QEMU_OPTION_mon:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002524 opts = qemu_opts_parse(qemu_find_opts("mon"), optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002525 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002526 exit(1);
2527 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002528 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002529 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002530 case QEMU_OPTION_chardev:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002531 opts = qemu_opts_parse(qemu_find_opts("chardev"), optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002532 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002533 exit(1);
2534 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002535 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302536 case QEMU_OPTION_fsdev:
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002537 olist = qemu_find_opts("fsdev");
2538 if (!olist) {
2539 fprintf(stderr, "fsdev is not supported by this qemu build.\n");
2540 exit(1);
2541 }
2542 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302543 if (!opts) {
2544 fprintf(stderr, "parse error: %s\n", optarg);
2545 exit(1);
2546 }
2547 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302548 case QEMU_OPTION_virtfs: {
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002549 QemuOpts *fsdev;
2550 QemuOpts *device;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302551
Gerd Hoffmann03b0ba72010-08-20 13:52:02 +02002552 olist = qemu_find_opts("virtfs");
2553 if (!olist) {
2554 fprintf(stderr, "virtfs is not supported by this qemu build.\n");
2555 exit(1);
2556 }
2557 opts = qemu_opts_parse(olist, optarg, 1);
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302558 if (!opts) {
2559 fprintf(stderr, "parse error: %s\n", optarg);
2560 exit(1);
2561 }
2562
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002563 if (qemu_opt_get(opts, "fstype") == NULL ||
2564 qemu_opt_get(opts, "mount_tag") == NULL ||
2565 qemu_opt_get(opts, "path") == NULL ||
2566 qemu_opt_get(opts, "security_model") == NULL) {
2567 fprintf(stderr, "Usage: -virtfs fstype,path=/share_path/,"
Aneesh Kumar K.V12848bf2010-09-02 11:09:07 +05302568 "security_model=[mapped|passthrough|none],"
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002569 "mount_tag=tag.\n");
Venkateswararao Jujjuri (JV)9ce56db2010-06-14 13:34:40 -07002570 exit(1);
2571 }
2572
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002573 fsdev = qemu_opts_create(qemu_find_opts("fsdev"),
2574 qemu_opt_get(opts, "mount_tag"), 1);
2575 if (!fsdev) {
2576 fprintf(stderr, "duplicate fsdev id: %s\n",
2577 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302578 exit(1);
2579 }
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002580 qemu_opt_set(fsdev, "fstype", qemu_opt_get(opts, "fstype"));
2581 qemu_opt_set(fsdev, "path", qemu_opt_get(opts, "path"));
2582 qemu_opt_set(fsdev, "security_model",
2583 qemu_opt_get(opts, "security_model"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302584
Stefan Hajnoczie14ea472011-03-16 08:31:43 +00002585 device = qemu_opts_create(qemu_find_opts("device"), NULL, 0);
2586 qemu_opt_set(device, "driver", "virtio-9p-pci");
2587 qemu_opt_set(device, "fsdev",
2588 qemu_opt_get(opts, "mount_tag"));
2589 qemu_opt_set(device, "mount_tag",
2590 qemu_opt_get(opts, "mount_tag"));
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302591 break;
2592 }
bellard82c643f2004-07-14 17:28:13 +00002593 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002594 add_device_config(DEV_SERIAL, optarg);
2595 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002596 if (strncmp(optarg, "mon:", 4) == 0) {
2597 default_monitor = 0;
2598 }
bellard82c643f2004-07-14 17:28:13 +00002599 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002600 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002601 if (watchdog) {
2602 fprintf(stderr,
2603 "qemu: only one watchdog option may be given\n");
2604 return 1;
2605 }
2606 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002607 break;
2608 case QEMU_OPTION_watchdog_action:
2609 if (select_watchdog_action(optarg) == -1) {
2610 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2611 exit(1);
2612 }
2613 break;
aliguori51ecf132009-01-15 20:06:40 +00002614 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002615 add_device_config(DEV_VIRTCON, optarg);
2616 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002617 if (strncmp(optarg, "mon:", 4) == 0) {
2618 default_monitor = 0;
2619 }
aliguori51ecf132009-01-15 20:06:40 +00002620 break;
bellard6508fe52005-01-15 12:02:56 +00002621 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002622 add_device_config(DEV_PARALLEL, optarg);
2623 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002624 if (strncmp(optarg, "mon:", 4) == 0) {
2625 default_monitor = 0;
2626 }
bellard6508fe52005-01-15 12:02:56 +00002627 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002628 case QEMU_OPTION_debugcon:
2629 add_device_config(DEV_DEBUGCON, optarg);
2630 break;
bellardd63d3072004-10-03 13:29:03 +00002631 case QEMU_OPTION_loadvm:
2632 loadvm = optarg;
2633 break;
2634 case QEMU_OPTION_full_screen:
2635 full_screen = 1;
2636 break;
ths667acca2006-12-11 02:08:05 +00002637#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002638 case QEMU_OPTION_no_frame:
2639 no_frame = 1;
2640 break;
ths3780e192007-06-21 21:08:02 +00002641 case QEMU_OPTION_alt_grab:
2642 alt_grab = 1;
2643 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002644 case QEMU_OPTION_ctrl_grab:
2645 ctrl_grab = 1;
2646 break;
ths667acca2006-12-11 02:08:05 +00002647 case QEMU_OPTION_no_quit:
2648 no_quit = 1;
2649 break;
aliguori7d957bd2009-01-15 22:14:11 +00002650 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002651 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002652 break;
Jes Sorensen58fc0962011-03-16 13:33:34 +01002653#else
2654 case QEMU_OPTION_no_frame:
2655 case QEMU_OPTION_alt_grab:
2656 case QEMU_OPTION_ctrl_grab:
2657 case QEMU_OPTION_no_quit:
2658 case QEMU_OPTION_sdl:
2659 fprintf(stderr, "SDL support is disabled\n");
2660 exit(1);
ths667acca2006-12-11 02:08:05 +00002661#endif
bellardf7cce892004-12-08 22:21:25 +00002662 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002663 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002664 break;
bellarda09db212005-04-30 16:10:35 +00002665 case QEMU_OPTION_win2k_hack:
2666 win2k_install_hack = 1;
2667 break;
aliguori73822ec2009-01-15 20:11:34 +00002668 case QEMU_OPTION_rtc_td_hack:
2669 rtc_td_hack = 1;
2670 break;
aliguori8a92ea22009-02-27 20:12:36 +00002671 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002672 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002673 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002674 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002675 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002676 break;
aliguori7ba1e612008-11-05 16:04:33 +00002677 case QEMU_OPTION_enable_kvm:
Anthony PERARD303d4e82010-09-21 20:05:31 +01002678 olist = qemu_find_opts("machine");
2679 qemu_opts_reset(olist);
2680 qemu_opts_parse(olist, "accel=kvm", 0);
2681 break;
2682 case QEMU_OPTION_machine:
2683 olist = qemu_find_opts("machine");
2684 qemu_opts_reset(olist);
2685 opts = qemu_opts_parse(olist, optarg, 0);
2686 if (!opts) {
2687 fprintf(stderr, "parse error: %s\n", optarg);
2688 exit(1);
2689 }
aliguori7ba1e612008-11-05 16:04:33 +00002690 break;
bellardbb36d472005-11-05 14:22:28 +00002691 case QEMU_OPTION_usb:
2692 usb_enabled = 1;
2693 break;
bellarda594cfb2005-11-06 16:13:29 +00002694 case QEMU_OPTION_usbdevice:
2695 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002696 add_device_config(DEV_USB, optarg);
2697 break;
2698 case QEMU_OPTION_device:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002699 if (!qemu_opts_parse(qemu_find_opts("device"), optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002700 exit(1);
2701 }
bellarda594cfb2005-11-06 16:13:29 +00002702 break;
bellard6a00d602005-11-21 23:25:50 +00002703 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002704 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002705 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002706 fprintf(stderr, "Invalid number of CPUs\n");
2707 exit(1);
2708 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002709 if (max_cpus < smp_cpus) {
2710 fprintf(stderr, "maxcpus must be equal to or greater than "
2711 "smp\n");
2712 exit(1);
2713 }
2714 if (max_cpus > 255) {
2715 fprintf(stderr, "Unsupported number of maxcpus\n");
2716 exit(1);
2717 }
bellard6a00d602005-11-21 23:25:50 +00002718 break;
bellard24236862006-04-30 21:28:36 +00002719 case QEMU_OPTION_vnc:
Jes Sorensen821601e2011-03-16 13:33:36 +01002720#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02002721 display_remote++;
Jes Sorensen821601e2011-03-16 13:33:36 +01002722 vnc_display = optarg;
2723#else
2724 fprintf(stderr, "VNC support is disabled\n");
2725 exit(1);
2726#endif
2727 break;
bellard6515b202006-05-03 22:02:44 +00002728 case QEMU_OPTION_no_acpi:
2729 acpi_enabled = 0;
2730 break;
aliguori16b29ae2008-12-17 23:28:44 +00002731 case QEMU_OPTION_no_hpet:
2732 no_hpet = 1;
2733 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002734 case QEMU_OPTION_balloon:
2735 if (balloon_parse(optarg) < 0) {
2736 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2737 exit(1);
2738 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002739 break;
bellardd1beab82006-10-02 19:44:22 +00002740 case QEMU_OPTION_no_reboot:
2741 no_reboot = 1;
2742 break;
aurel32b2f76162008-04-11 21:35:52 +00002743 case QEMU_OPTION_no_shutdown:
2744 no_shutdown = 1;
2745 break;
balrog9467cd42007-05-01 01:34:14 +00002746 case QEMU_OPTION_show_cursor:
2747 cursor_hide = 0;
2748 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002749 case QEMU_OPTION_uuid:
2750 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2751 fprintf(stderr, "Fail to parse UUID string."
2752 " Wrong format.\n");
2753 exit(1);
2754 }
2755 break;
ths9ae02552007-01-05 17:39:04 +00002756 case QEMU_OPTION_option_rom:
2757 if (nb_option_roms >= MAX_OPTION_ROMS) {
2758 fprintf(stderr, "Too many option ROMs\n");
2759 exit(1);
2760 }
Gleb Natapov2e55e842010-12-08 13:35:07 +02002761 opts = qemu_opts_parse(qemu_find_opts("option-rom"), optarg, 1);
2762 option_rom[nb_option_roms].name = qemu_opt_get(opts, "romfile");
2763 option_rom[nb_option_roms].bootindex =
2764 qemu_opt_get_number(opts, "bootindex", -1);
2765 if (!option_rom[nb_option_roms].name) {
2766 fprintf(stderr, "Option ROM file is not specified\n");
2767 exit(1);
2768 }
ths9ae02552007-01-05 17:39:04 +00002769 nb_option_roms++;
2770 break;
pbrook8e716212007-01-20 17:12:09 +00002771 case QEMU_OPTION_semihosting:
2772 semihosting_enabled = 1;
2773 break;
thsc35734b2007-03-19 15:17:08 +00002774 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002775 qemu_name = qemu_strdup(optarg);
2776 {
2777 char *p = strchr(qemu_name, ',');
2778 if (p != NULL) {
2779 *p++ = 0;
2780 if (strncmp(p, "process=", 8)) {
Aurelien Jarno5697f6a2010-12-27 18:29:20 +01002781 fprintf(stderr, "Unknown subargument %s to -name\n", p);
Andi Kleen18894652009-07-02 09:34:17 +02002782 exit(1);
2783 }
2784 p += 8;
Jes Sorensence798cf2010-06-10 11:42:31 +02002785 os_set_proc_name(p);
Andi Kleen18894652009-07-02 09:34:17 +02002786 }
2787 }
thsc35734b2007-03-19 15:17:08 +00002788 break;
blueswir166508602007-05-01 14:16:52 +00002789 case QEMU_OPTION_prom_env:
2790 if (nb_prom_envs >= MAX_PROM_ENVS) {
2791 fprintf(stderr, "Too many prom variables\n");
2792 exit(1);
2793 }
2794 prom_envs[nb_prom_envs] = optarg;
2795 nb_prom_envs++;
2796 break;
balrog2b8f2d42007-07-27 22:08:46 +00002797 case QEMU_OPTION_old_param:
2798 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00002799 break;
thsf3dcfad2007-08-24 01:26:02 +00002800 case QEMU_OPTION_clock:
2801 configure_alarms(optarg);
2802 break;
bellard7e0af5d02007-11-07 16:24:33 +00002803 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002804 configure_rtc_date_offset(optarg, 1);
2805 break;
2806 case QEMU_OPTION_rtc:
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002807 opts = qemu_opts_parse(qemu_find_opts("rtc"), optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002808 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002809 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00002810 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02002811 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00002812 break;
bellard26a5f132008-05-28 12:30:31 +00002813 case QEMU_OPTION_tb_size:
2814 tb_size = strtol(optarg, NULL, 0);
2815 if (tb_size < 0)
2816 tb_size = 0;
2817 break;
pbrook2e70f6e2008-06-29 01:03:05 +00002818 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002819 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00002820 break;
aliguori5bb79102008-10-13 03:12:02 +00002821 case QEMU_OPTION_incoming:
2822 incoming = optarg;
Amit Shah8e848652010-07-27 15:49:19 +05302823 incoming_expected = true;
aliguori5bb79102008-10-13 03:12:02 +00002824 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002825 case QEMU_OPTION_nodefaults:
2826 default_serial = 0;
2827 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002828 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002829 default_monitor = 0;
2830 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01002831 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002832 default_floppy = 0;
2833 default_cdrom = 0;
2834 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01002835 break;
aliguorie37630c2009-04-22 15:19:10 +00002836 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00002837 if (!(xen_available())) {
2838 printf("Option %s not supported for this target\n", popt->name);
2839 exit(1);
2840 }
aliguorie37630c2009-04-22 15:19:10 +00002841 xen_domid = atoi(optarg);
2842 break;
2843 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00002844 if (!(xen_available())) {
2845 printf("Option %s not supported for this target\n", popt->name);
2846 exit(1);
2847 }
aliguorie37630c2009-04-22 15:19:10 +00002848 xen_mode = XEN_CREATE;
2849 break;
2850 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00002851 if (!(xen_available())) {
2852 printf("Option %s not supported for this target\n", popt->name);
2853 exit(1);
2854 }
aliguorie37630c2009-04-22 15:19:10 +00002855 xen_mode = XEN_ATTACH;
2856 break;
Prerna Saxenaab6540d2010-08-09 11:48:32 +01002857#ifdef CONFIG_SIMPLE_TRACE
2858 case QEMU_OPTION_trace:
2859 opts = qemu_opts_parse(qemu_find_opts("trace"), optarg, 0);
2860 if (opts) {
2861 trace_file = qemu_opt_get(opts, "file");
2862 }
2863 break;
2864#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002865 case QEMU_OPTION_readconfig:
2866 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002867 int ret = qemu_read_config_file(optarg);
2868 if (ret < 0) {
2869 fprintf(stderr, "read config %s: %s\n", optarg,
2870 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002871 exit(1);
2872 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002873 break;
2874 }
Gerd Hoffmann29b00402010-03-11 11:13:27 -03002875 case QEMU_OPTION_spice:
2876 olist = qemu_find_opts("spice");
2877 if (!olist) {
2878 fprintf(stderr, "spice is not supported by this qemu build.\n");
2879 exit(1);
2880 }
2881 opts = qemu_opts_parse(olist, optarg, 0);
2882 if (!opts) {
2883 fprintf(stderr, "parse error: %s\n", optarg);
2884 exit(1);
2885 }
2886 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02002887 case QEMU_OPTION_writeconfig:
2888 {
2889 FILE *fp;
2890 if (strcmp(optarg, "-") == 0) {
2891 fp = stdout;
2892 } else {
2893 fp = fopen(optarg, "w");
2894 if (fp == NULL) {
2895 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
2896 exit(1);
2897 }
2898 }
2899 qemu_config_write(fp);
2900 fclose(fp);
2901 break;
2902 }
Jes Sorensen59a52642010-06-10 11:42:25 +02002903 default:
2904 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00002905 }
bellard0824d6f2003-06-24 13:42:40 +00002906 }
2907 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002908 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00002909
Stefan Hajnoczi31d3c9b2011-03-13 20:14:30 +00002910 if (!st_init(trace_file)) {
2911 fprintf(stderr, "warning: unable to initialize simple trace backend\n");
2912 }
Stefan Hajnoczi0b5538c2011-02-26 18:38:39 +00002913
Paul Brook5cea8592009-05-30 00:52:44 +01002914 /* If no data_dir is specified then try to find it relative to the
2915 executable path. */
2916 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02002917 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01002918 }
2919 /* If all else fails use the install patch specified when building. */
2920 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02002921 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01002922 }
2923
Jes Sorensen6be68d72009-07-23 17:03:42 +02002924 /*
2925 * Default to max_cpus = smp_cpus, in case the user doesn't
2926 * specify a max_cpus value.
2927 */
2928 if (!max_cpus)
2929 max_cpus = smp_cpus;
2930
balrog3d878ca2008-10-28 10:59:59 +00002931 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00002932 if (smp_cpus > machine->max_cpus) {
2933 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
2934 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
2935 machine->max_cpus);
2936 exit(1);
2937 }
2938
Anthony PERARD67b724e2010-11-22 15:44:15 +00002939 /*
2940 * Get the default machine options from the machine if it is not already
2941 * specified either by the configuration file or by the command line.
2942 */
2943 if (machine->default_machine_opts) {
2944 QemuOptsList *list = qemu_find_opts("machine");
2945 const char *p = NULL;
2946
2947 if (!QTAILQ_EMPTY(&list->head)) {
2948 p = qemu_opt_get(QTAILQ_FIRST(&list->head), "accel");
2949 }
2950 if (p == NULL) {
2951 opts = qemu_opts_parse(qemu_find_opts("machine"),
2952 machine->default_machine_opts, 0);
2953 if (!opts) {
2954 fprintf(stderr, "parse error for machine %s: %s\n",
2955 machine->name, machine->default_machine_opts);
2956 exit(1);
2957 }
2958 }
2959 }
2960
Gerd Hoffmann3329f072010-08-20 13:52:01 +02002961 qemu_opts_foreach(qemu_find_opts("device"), default_driver_check, NULL, 0);
2962 qemu_opts_foreach(qemu_find_opts("global"), default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002963
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002964 if (machine->no_serial) {
2965 default_serial = 0;
2966 }
2967 if (machine->no_parallel) {
2968 default_parallel = 0;
2969 }
2970 if (!machine->use_virtcon) {
2971 default_virtcon = 0;
2972 }
2973 if (machine->no_vga) {
2974 default_vga = 0;
2975 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01002976 if (machine->no_floppy) {
2977 default_floppy = 0;
2978 }
2979 if (machine->no_cdrom) {
2980 default_cdrom = 0;
2981 }
2982 if (machine->no_sdcard) {
2983 default_sdcard = 0;
2984 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002985
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002986 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002987 if (default_parallel)
2988 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002989 if (default_serial && default_monitor) {
2990 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002991 } else if (default_virtcon && default_monitor) {
2992 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002993 } else {
2994 if (default_serial)
2995 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01002996 if (default_virtcon)
2997 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01002998 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002999 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003000 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003001 } else {
3002 if (default_serial)
3003 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003004 if (default_parallel)
3005 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003006 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003007 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003008 if (default_virtcon)
3009 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003010 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003011 if (default_vga)
3012 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003013
TeLeMana5829fd2010-04-15 12:37:55 +08003014 socket_init();
3015
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003016 if (qemu_opts_foreach(qemu_find_opts("chardev"), chardev_init_func, NULL, 1) != 0)
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003017 exit(1);
Venkateswararao Jujjuri (JV)758e8e32010-06-14 13:34:41 -07003018#ifdef CONFIG_VIRTFS
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003019 if (qemu_opts_foreach(qemu_find_opts("fsdev"), fsdev_init_func, NULL, 1) != 0) {
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303020 exit(1);
3021 }
3022#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003023
Jes Sorenseneb505be2010-06-10 11:42:28 +02003024 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003025
thsaa26bb22007-03-25 21:33:06 +00003026 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003027 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003028 exit(1);
3029 }
3030
Anthony PERARD303d4e82010-09-21 20:05:31 +01003031 configure_accelerator();
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003032
aliguori3fcf7b62009-04-24 18:03:25 +00003033 if (qemu_init_main_loop()) {
3034 fprintf(stderr, "qemu_init_main_loop failed\n");
3035 exit(1);
3036 }
bellarda20dd502003-09-30 21:07:02 +00003037 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003038
thsf8d39c02008-07-03 10:01:15 +00003039 if (!linux_boot && *kernel_cmdline != '\0') {
3040 fprintf(stderr, "-append only allowed with -kernel option\n");
3041 exit(1);
3042 }
3043
3044 if (!linux_boot && initrd_filename != NULL) {
3045 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3046 exit(1);
3047 }
3048
Jes Sorensen9156d762010-06-10 11:42:30 +02003049 os_set_line_buffering();
ths3b46e622007-09-17 08:09:54 +00003050
aliguori7183b4b2008-11-05 20:40:18 +00003051 if (init_timer_alarm() < 0) {
3052 fprintf(stderr, "could not initialize alarm timer\n");
3053 exit(1);
3054 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003055 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003056
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003057 if (net_init_clients() < 0) {
3058 exit(1);
bellard702c6512004-04-02 21:21:32 +00003059 }
bellardf1510b22003-06-25 00:07:40 +00003060
balrogdc72ac12008-11-09 00:04:26 +00003061 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003062 if (foreach_device_config(DEV_BT, bt_parse))
3063 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003064
bellard0824d6f2003-06-24 13:42:40 +00003065 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00003066 if (ram_size == 0)
3067 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00003068
bellard26a5f132008-05-28 12:30:31 +00003069 /* init the dynamic translator */
3070 cpu_exec_init_all(tb_size * 1024 * 1024);
3071
Markus Armbrustereb852012009-10-27 18:41:44 +01003072 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003073
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003074 blk_mig_init();
3075
ths96d30e42007-01-07 20:42:14 +00003076 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003077 if (snapshot)
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003078 qemu_opts_foreach(qemu_find_opts("drive"), drive_enable_snapshot, NULL, 0);
3079 if (qemu_opts_foreach(qemu_find_opts("drive"), drive_init_func, &machine->use_scsi, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003080 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003081
Markus Armbruster4e5d9b52011-01-28 11:21:45 +01003082 default_drive(default_cdrom, snapshot, machine->use_scsi,
3083 IF_DEFAULT, 2, CDROM_OPTS);
3084 default_drive(default_floppy, snapshot, machine->use_scsi,
3085 IF_FLOPPY, 0, FD_OPTS);
3086 default_drive(default_sdcard, snapshot, machine->use_scsi,
3087 IF_SD, 0, SD_OPTS);
3088
Alex Williamson97ab12d2010-06-25 11:09:50 -06003089 register_savevm_live(NULL, "ram", 0, 4, NULL, ram_save_live, NULL,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003090 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003091
aliguori268a3622009-04-21 22:30:27 +00003092 if (nb_numa_nodes > 0) {
3093 int i;
3094
3095 if (nb_numa_nodes > smp_cpus) {
3096 nb_numa_nodes = smp_cpus;
3097 }
3098
3099 /* If no memory size if given for any node, assume the default case
3100 * and distribute the available memory equally across all nodes
3101 */
3102 for (i = 0; i < nb_numa_nodes; i++) {
3103 if (node_mem[i] != 0)
3104 break;
3105 }
3106 if (i == nb_numa_nodes) {
3107 uint64_t usedmem = 0;
3108
3109 /* On Linux, the each node's border has to be 8MB aligned,
3110 * the final node gets the rest.
3111 */
3112 for (i = 0; i < nb_numa_nodes - 1; i++) {
3113 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3114 usedmem += node_mem[i];
3115 }
3116 node_mem[i] = ram_size - usedmem;
3117 }
3118
3119 for (i = 0; i < nb_numa_nodes; i++) {
3120 if (node_cpumask[i] != 0)
3121 break;
3122 }
3123 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3124 * must cope with this anyway, because there are BIOSes out there in
3125 * real machines which also use this scheme.
3126 */
3127 if (i == nb_numa_nodes) {
3128 for (i = 0; i < smp_cpus; i++) {
3129 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3130 }
3131 }
3132 }
3133
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003134 if (qemu_opts_foreach(qemu_find_opts("mon"), mon_init_func, NULL, 1) != 0) {
Jan Kiszka157b9312010-04-06 16:55:53 +02003135 exit(1);
3136 }
3137
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003138 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3139 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003140 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3141 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003142 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3143 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003144 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3145 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003146
Paul Brookaae94602009-05-14 22:35:06 +01003147 module_call_init(MODULE_INIT_DEVICE);
3148
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003149 if (qemu_opts_foreach(qemu_find_opts("device"), device_help_func, NULL, 0) != 0)
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003150 exit(0);
3151
Markus Armbruster09aaa162009-08-21 10:31:34 +02003152 if (watchdog) {
3153 i = select_watchdog(watchdog);
3154 if (i > 0)
3155 exit (i == 1 ? 1 : 0);
3156 }
3157
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003158 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003159 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003160 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003161 qemu_add_globals();
3162
Paul Brookfbe1b592009-05-13 17:56:25 +01003163 machine->init(ram_size, boot_devices,
aliguori3023f3322009-01-16 19:04:14 +00003164 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3165
Jan Kiszkaea375f92010-03-01 19:10:30 +01003166 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003167
Blue Swirl87d0a282010-03-27 18:24:45 +00003168 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003169
aliguori6f338c32009-02-11 15:21:54 +00003170 current_machine = machine;
3171
aliguori3023f3322009-01-16 19:04:14 +00003172 /* init USB devices */
3173 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003174 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3175 exit(1);
aliguori3023f3322009-01-16 19:04:14 +00003176 }
3177
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003178 /* init generic devices */
Gerd Hoffmann3329f072010-08-20 13:52:01 +02003179 if (qemu_opts_foreach(qemu_find_opts("device"), device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003180 exit(1);
3181
Markus Armbruster668680f2010-02-11 14:44:58 +01003182 net_check_clients();
3183
aliguori3023f3322009-01-16 19:04:14 +00003184 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003185 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003186
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003187 if (using_spice)
3188 display_remote++;
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003189 if (display_type == DT_DEFAULT && !display_remote) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003190#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003191 display_type = DT_SDL;
Jes Sorensen821601e2011-03-16 13:33:36 +01003192#elif defined(CONFIG_VNC)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003193 vnc_display = "localhost:0,to=99";
3194 show_vnc_port = 1;
Jes Sorensen821601e2011-03-16 13:33:36 +01003195#else
3196 display_type = DT_NONE;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003197#endif
3198 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003199
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003200
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003201 /* init local displays */
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003202 switch (display_type) {
3203 case DT_NOGRAPHIC:
3204 break;
3205#if defined(CONFIG_CURSES)
3206 case DT_CURSES:
3207 curses_display_init(ds, full_screen);
3208 break;
3209#endif
bellard5b0753e2005-03-01 21:37:28 +00003210#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003211 case DT_SDL:
3212 sdl_display_init(ds, full_screen, no_frame);
3213 break;
bellard5b0753e2005-03-01 21:37:28 +00003214#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003215 case DT_SDL:
3216 cocoa_display_init(ds, full_screen);
3217 break;
bellard313aa562003-08-10 21:52:11 +00003218#endif
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003219 default:
3220 break;
3221 }
3222
Gleb Natapov0ce235a2011-03-31 11:27:23 +02003223 /* must be after terminal init, SDL library changes signal handlers */
3224 os_setup_signal_handling();
3225
Jes Sorensen821601e2011-03-16 13:33:36 +01003226#ifdef CONFIG_VNC
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003227 /* init remote displays */
3228 if (vnc_display) {
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003229 vnc_display_init(ds);
3230 if (vnc_display_open(ds, vnc_display) < 0)
3231 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003232
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003233 if (show_vnc_port) {
3234 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003235 }
bellard313aa562003-08-10 21:52:11 +00003236 }
Jes Sorensen821601e2011-03-16 13:33:36 +01003237#endif
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003238#ifdef CONFIG_SPICE
Gerd Hoffmanna19cbfb2010-04-27 11:50:11 +02003239 if (using_spice && !qxl_enabled) {
Gerd Hoffmanna3e22262010-08-25 15:32:06 +02003240 qemu_spice_display_init(ds);
3241 }
3242#endif
aliguori5b08fc12008-08-21 20:08:03 +00003243
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003244 /* display setup */
3245 dpy_resize(ds);
aliguori3023f3322009-01-16 19:04:14 +00003246 dcl = ds->listeners;
3247 while (dcl != NULL) {
3248 if (dcl->dpy_refresh != NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003249 ds->gui_timer = qemu_new_timer_ms(rt_clock, gui_update, ds);
3250 qemu_mod_timer(ds->gui_timer, qemu_get_clock_ms(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003251 break;
ths20d8a3e2007-02-18 17:04:49 +00003252 }
aliguori3023f3322009-01-16 19:04:14 +00003253 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003254 }
Gerd Hoffmann6b62dc22010-08-25 10:51:06 +02003255 if (ds->gui_timer == NULL) {
Paolo Bonzini7bd427d2011-03-11 16:47:48 +01003256 nographic_timer = qemu_new_timer_ms(rt_clock, nographic_update, NULL);
3257 qemu_mod_timer(nographic_timer, qemu_get_clock_ms(rt_clock));
blueswir19043b622009-01-21 19:28:13 +00003258 }
Paolo Bonzinib473df62010-02-11 00:29:55 +01003259 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003260
aliguori59030a82009-04-05 18:43:41 +00003261 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3262 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3263 gdbstub_dev);
3264 exit(1);
balrog45669e02007-07-02 13:20:17 +00003265 }
balrog45669e02007-07-02 13:20:17 +00003266
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003267 qdev_machine_creation_done();
3268
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003269 if (rom_load_all() != 0) {
3270 fprintf(stderr, "rom loading failed\n");
3271 exit(1);
3272 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003273
Isaku Yamahata80376c32010-12-20 14:33:35 +09003274 /* TODO: once all bus devices are qdevified, this should be done
3275 * when bus is created by qdev.c */
3276 qemu_register_reset(qbus_reset_all_fn, sysbus_get_default());
Gleb Natapov4cab9462010-12-08 13:35:08 +02003277 qemu_run_machine_init_done_notifiers();
3278
Juan Quintela504c2942009-11-12 00:39:13 +01003279 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003280 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003281 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003282 autostart = 0;
3283 }
3284 }
bellardd63d3072004-10-03 13:29:03 +00003285
Glauber Costa2bb8c102009-07-24 16:20:23 -04003286 if (incoming) {
Juan Quintela8ca5e802010-06-09 14:10:54 +02003287 int ret = qemu_start_incoming_migration(incoming);
3288 if (ret < 0) {
3289 fprintf(stderr, "Migration failed. Exit code %s(%d), exiting.\n",
3290 incoming, ret);
3291 exit(ret);
3292 }
Avi Kivity6b99dad2009-08-09 14:39:20 +03003293 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003294 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003295 }
thsffd843b2006-12-21 19:46:43 +00003296
Jes Sorenseneb505be2010-06-10 11:42:28 +02003297 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003298
bellard8a7ddc32004-03-31 19:00:16 +00003299 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003300 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003301 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003302
bellard0824d6f2003-06-24 13:42:40 +00003303 return 0;
3304}