blob: cadd988fd215eec8572589c56afbcc371e1423e6 [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>
Juan Quintela71e72a12009-07-27 16:12:56 +020050#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000051#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010052#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000053#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010054#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
Andi Kleen18894652009-07-02 09:34:17 +020062#include <sys/prctl.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>
Blue Swirl8d32cf02009-10-02 19:32:12 +000080/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
81 discussion about Solaris header problems */
82extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000083#endif
bellard7d3505c2004-05-12 19:32:15 +000084#endif
bellardec530c82006-04-25 22:36:06 +000085#endif
bellard67b915a2004-03-31 23:37:16 +000086
blueswir19892fbf2008-08-24 10:34:20 +000087#if defined(__OpenBSD__)
88#include <util.h>
89#endif
90
ths8a16d272008-07-19 09:56:24 +000091#if defined(CONFIG_VDE)
92#include <libvdeplug.h>
93#endif
94
bellard67b915a2004-03-31 23:37:16 +000095#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000096#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000097#endif
98
bellard73332e52004-04-04 20:22:28 +000099#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200100#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200101#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000102int qemu_main(int argc, char **argv, char **envp);
103int main(int argc, char **argv)
104{
Stefan Weil59a36a22009-06-18 20:11:03 +0200105 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000106}
107#undef main
108#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000109#endif
bellard73332e52004-04-04 20:22:28 +0000110#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000111
bellard5b0753e2005-03-01 21:37:28 +0000112#ifdef CONFIG_COCOA
113#undef main
114#define main qemu_main
115#endif /* CONFIG_COCOA */
116
blueswir1511d2b12009-03-07 15:32:56 +0000117#include "hw/hw.h"
118#include "hw/boards.h"
119#include "hw/usb.h"
120#include "hw/pcmcia.h"
121#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000122#include "hw/isa.h"
123#include "hw/baum.h"
124#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100125#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000126#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000127#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200128#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200129#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000130#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000131#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000132#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "monitor.h"
134#include "console.h"
135#include "sysemu.h"
136#include "gdbstub.h"
137#include "qemu-timer.h"
138#include "qemu-char.h"
139#include "cache-utils.h"
140#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200141#include "block_int.h"
142#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000143#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000144#include "audio/audio.h"
145#include "migration.h"
146#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200147#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200148#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200149#include "qemu-objects.h"
Jes Sorensen59a52642010-06-10 11:42:25 +0200150#include "qemu-options.h"
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530151#ifdef CONFIG_LINUX
152#include "fsdev/qemu-fsdev.h"
153#endif
blueswir1511d2b12009-03-07 15:32:56 +0000154
bellard0824d6f2003-06-24 13:42:40 +0000155#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000156
blueswir1511d2b12009-03-07 15:32:56 +0000157#include "qemu_socket.h"
158
Jan Kiszkad918f232009-06-24 14:42:30 +0200159#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
Blue Swirl72cf2d42009-09-12 07:36:22 +0000161#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000162#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000163#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000164
blueswir19dc63a12008-10-04 07:25:46 +0000165//#define DEBUG_NET
166//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000167
bellard1bfe8562004-07-08 21:17:50 +0000168#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000169
Amit Shah98b19252010-01-20 00:36:52 +0530170#define MAX_VIRTIO_CONSOLES 1
171
Paul Brook5cea8592009-05-30 00:52:44 +0100172static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000173const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000174/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000175 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000176struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
177struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000178enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500179DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000180const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500181ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300182const char *mem_path = NULL;
183#ifdef MAP_POPULATE
184int mem_prealloc = 0; /* force preallocation of physical target memory */
185#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000186int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000187NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000188int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200189int autostart;
balrogf6503052008-02-17 11:42:19 +0000190static int rtc_utc = 1;
191static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200192QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100193int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000194static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000195#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000196static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000197#endif
ths667acca2006-12-11 02:08:05 +0000198int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000199CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000200CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000201CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000202int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000203int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000204int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000205int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000206int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200207int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200208int smp_cores = 1;
209int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000210const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000211int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000212int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000213int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000214int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000215int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000216int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000217int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200218uint8_t irq0override = 1;
Markus Armbruster09aaa162009-08-21 10:31:34 +0200219const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000220const char *option_rom[MAX_OPTION_ROMS];
221int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000222int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000223int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000224const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000225int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500226int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000227unsigned int nb_prom_envs = 0;
228const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200229int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000230
aliguori268a3622009-04-21 22:30:27 +0000231int nb_numa_nodes;
232uint64_t node_mem[MAX_NODES];
233uint64_t node_cpumask[MAX_NODES];
234
blueswir19043b622009-01-21 19:28:13 +0000235static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000236
blueswir18fcb1b92008-09-18 18:29:08 +0000237uint8_t qemu_uuid[16];
238
Jan Kiszka76e30d02009-07-02 00:19:02 +0200239static QEMUBootSetHandler *boot_set_handler;
240static void *boot_set_opaque;
241
Blue Swirld745bef2010-03-29 19:23:49 +0000242int kvm_allowed = 0;
243uint32_t xen_domid;
244enum xen_mode xen_mode = XEN_EMULATE;
245
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100246static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100247static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100248static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100249static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100250static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100251static int default_floppy = 1;
252static int default_cdrom = 1;
253static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100254
255static struct {
256 const char *driver;
257 int *flag;
258} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100259 { .driver = "isa-serial", .flag = &default_serial },
260 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100261 { .driver = "isa-fdc", .flag = &default_floppy },
262 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530263 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
264 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
265 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100266 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100267 { .driver = "cirrus-vga", .flag = &default_vga },
268 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100269};
270
271static int default_driver_check(QemuOpts *opts, void *opaque)
272{
273 const char *driver = qemu_opt_get(opts, "driver");
274 int i;
275
276 if (!driver)
277 return 0;
278 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
279 if (strcmp(default_list[i].driver, driver) != 0)
280 continue;
281 *(default_list[i].flag) = 0;
282 }
283 return 0;
284}
285
bellard0824d6f2003-06-24 13:42:40 +0000286/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000287
Andi Kleen18894652009-07-02 09:34:17 +0200288static void set_proc_name(const char *s)
289{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700290#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200291 char name[16];
292 if (!s)
293 return;
294 name[sizeof(name) - 1] = 0;
295 strncpy(name, s, sizeof(name));
296 /* Could rewrite argv[0] too, but that's a bit more complicated.
297 This simple way is enough for `top'. */
298 prctl(PR_SET_NAME, name);
299#endif
300}
aliguoridf751fa2008-12-04 20:19:35 +0000301
bellard8a7ddc32004-03-31 19:00:16 +0000302/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100303/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000304
bellard87858c82003-06-27 12:01:39 +0000305/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000306uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000307{
308 union {
309 uint64_t ll;
310 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200311#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000312 uint32_t high, low;
313#else
314 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000315#endif
bellard87858c82003-06-27 12:01:39 +0000316 } l;
317 } u, res;
318 uint64_t rl, rh;
319
320 u.ll = a;
321 rl = (uint64_t)u.l.low * (uint64_t)b;
322 rh = (uint64_t)u.l.high * (uint64_t)b;
323 rh += (rl >> 32);
324 res.l.high = rh / c;
325 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
326 return res.ll;
327}
328
bellardc4b1fcc2004-03-14 21:44:30 +0000329/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000330/* host time/date access */
331void qemu_get_timedate(struct tm *tm, int offset)
332{
333 time_t ti;
334 struct tm *ret;
335
336 time(&ti);
337 ti += offset;
338 if (rtc_date_offset == -1) {
339 if (rtc_utc)
340 ret = gmtime(&ti);
341 else
342 ret = localtime(&ti);
343 } else {
344 ti -= rtc_date_offset;
345 ret = gmtime(&ti);
346 }
347
348 memcpy(tm, ret, sizeof(struct tm));
349}
350
351int qemu_timedate_diff(struct tm *tm)
352{
353 time_t seconds;
354
355 if (rtc_date_offset == -1)
356 if (rtc_utc)
357 seconds = mktimegm(tm);
358 else
359 seconds = mktime(tm);
360 else
361 seconds = mktimegm(tm) + rtc_date_offset;
362
363 return seconds - time(NULL);
364}
365
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300366void rtc_change_mon_event(struct tm *tm)
367{
368 QObject *data;
369
370 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
371 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
372 qobject_decref(data);
373}
374
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200375static void configure_rtc_date_offset(const char *startdate, int legacy)
376{
377 time_t rtc_start_date;
378 struct tm tm;
379
380 if (!strcmp(startdate, "now") && legacy) {
381 rtc_date_offset = -1;
382 } else {
383 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
384 &tm.tm_year,
385 &tm.tm_mon,
386 &tm.tm_mday,
387 &tm.tm_hour,
388 &tm.tm_min,
389 &tm.tm_sec) == 6) {
390 /* OK */
391 } else if (sscanf(startdate, "%d-%d-%d",
392 &tm.tm_year,
393 &tm.tm_mon,
394 &tm.tm_mday) == 3) {
395 tm.tm_hour = 0;
396 tm.tm_min = 0;
397 tm.tm_sec = 0;
398 } else {
399 goto date_fail;
400 }
401 tm.tm_year -= 1900;
402 tm.tm_mon--;
403 rtc_start_date = mktimegm(&tm);
404 if (rtc_start_date == -1) {
405 date_fail:
406 fprintf(stderr, "Invalid date format. Valid formats are:\n"
407 "'2006-06-17T16:01:21' or '2006-06-17'\n");
408 exit(1);
409 }
410 rtc_date_offset = time(NULL) - rtc_start_date;
411 }
412}
413
414static void configure_rtc(QemuOpts *opts)
415{
416 const char *value;
417
418 value = qemu_opt_get(opts, "base");
419 if (value) {
420 if (!strcmp(value, "utc")) {
421 rtc_utc = 1;
422 } else if (!strcmp(value, "localtime")) {
423 rtc_utc = 0;
424 } else {
425 configure_rtc_date_offset(value, 0);
426 }
427 }
Jan Kiszka68752042009-09-15 13:36:04 +0200428 value = qemu_opt_get(opts, "clock");
429 if (value) {
430 if (!strcmp(value, "host")) {
431 rtc_clock = host_clock;
432 } else if (!strcmp(value, "vm")) {
433 rtc_clock = vm_clock;
434 } else {
435 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
436 exit(1);
437 }
438 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200439 value = qemu_opt_get(opts, "driftfix");
440 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000441 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200442 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000443 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200444 rtc_td_hack = 0;
445 } else {
446 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
447 exit(1);
448 }
449 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200450}
451
balrog1ae26a12008-09-28 23:19:47 +0000452/***********************************************************/
453/* Bluetooth support */
454static int nb_hcis;
455static int cur_hci;
456static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000457
balrog1ae26a12008-09-28 23:19:47 +0000458static struct bt_vlan_s {
459 struct bt_scatternet_s net;
460 int id;
461 struct bt_vlan_s *next;
462} *first_bt_vlan;
463
464/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000465static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000466{
467 struct bt_vlan_s **pvlan, *vlan;
468 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
469 if (vlan->id == id)
470 return &vlan->net;
471 }
472 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
473 vlan->id = id;
474 pvlan = &first_bt_vlan;
475 while (*pvlan != NULL)
476 pvlan = &(*pvlan)->next;
477 *pvlan = vlan;
478 return &vlan->net;
479}
480
481static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
482{
483}
484
485static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
486{
487 return -ENOTSUP;
488}
489
490static struct HCIInfo null_hci = {
491 .cmd_send = null_hci_send,
492 .sco_send = null_hci_send,
493 .acl_send = null_hci_send,
494 .bdaddr_set = null_hci_addr_set,
495};
496
497struct HCIInfo *qemu_next_hci(void)
498{
499 if (cur_hci == nb_hcis)
500 return &null_hci;
501
502 return hci_table[cur_hci++];
503}
504
balrogdc72ac12008-11-09 00:04:26 +0000505static struct HCIInfo *hci_init(const char *str)
506{
507 char *endp;
508 struct bt_scatternet_s *vlan = 0;
509
510 if (!strcmp(str, "null"))
511 /* null */
512 return &null_hci;
513 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
514 /* host[:hciN] */
515 return bt_host_hci(str[4] ? str + 5 : "hci0");
516 else if (!strncmp(str, "hci", 3)) {
517 /* hci[,vlan=n] */
518 if (str[3]) {
519 if (!strncmp(str + 3, ",vlan=", 6)) {
520 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
521 if (*endp)
522 vlan = 0;
523 }
524 } else
525 vlan = qemu_find_bt_vlan(0);
526 if (vlan)
527 return bt_new_hci(vlan);
528 }
529
530 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
531
532 return 0;
533}
534
535static int bt_hci_parse(const char *str)
536{
537 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500538 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000539
540 if (nb_hcis >= MAX_NICS) {
541 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
542 return -1;
543 }
544
545 hci = hci_init(str);
546 if (!hci)
547 return -1;
548
549 bdaddr.b[0] = 0x52;
550 bdaddr.b[1] = 0x54;
551 bdaddr.b[2] = 0x00;
552 bdaddr.b[3] = 0x12;
553 bdaddr.b[4] = 0x34;
554 bdaddr.b[5] = 0x56 + nb_hcis;
555 hci->bdaddr_set(hci, bdaddr.b);
556
557 hci_table[nb_hcis++] = hci;
558
559 return 0;
560}
561
562static void bt_vhci_add(int vlan_id)
563{
564 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
565
566 if (!vlan->slave)
567 fprintf(stderr, "qemu: warning: adding a VHCI to "
568 "an empty scatternet %i\n", vlan_id);
569
570 bt_vhci_init(bt_new_hci(vlan));
571}
572
573static struct bt_device_s *bt_device_add(const char *opt)
574{
575 struct bt_scatternet_s *vlan;
576 int vlan_id = 0;
577 char *endp = strstr(opt, ",vlan=");
578 int len = (endp ? endp - opt : strlen(opt)) + 1;
579 char devname[10];
580
581 pstrcpy(devname, MIN(sizeof(devname), len), opt);
582
583 if (endp) {
584 vlan_id = strtol(endp + 6, &endp, 0);
585 if (*endp) {
586 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
587 return 0;
588 }
589 }
590
591 vlan = qemu_find_bt_vlan(vlan_id);
592
593 if (!vlan->slave)
594 fprintf(stderr, "qemu: warning: adding a slave device to "
595 "an empty scatternet %i\n", vlan_id);
596
597 if (!strcmp(devname, "keyboard"))
598 return bt_keyboard_init(vlan);
599
600 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
601 return 0;
602}
603
604static int bt_parse(const char *opt)
605{
606 const char *endp, *p;
607 int vlan;
608
609 if (strstart(opt, "hci", &endp)) {
610 if (!*endp || *endp == ',') {
611 if (*endp)
612 if (!strstart(endp, ",vlan=", 0))
613 opt = endp + 1;
614
615 return bt_hci_parse(opt);
616 }
617 } else if (strstart(opt, "vhci", &endp)) {
618 if (!*endp || *endp == ',') {
619 if (*endp) {
620 if (strstart(endp, ",vlan=", &p)) {
621 vlan = strtol(p, (char **) &endp, 0);
622 if (*endp) {
623 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
624 return 1;
625 }
626 } else {
627 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
628 return 1;
629 }
630 } else
631 vlan = 0;
632
633 bt_vhci_add(vlan);
634 return 0;
635 }
636 } else if (strstart(opt, "device:", &endp))
637 return !bt_device_add(endp);
638
639 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
640 return 1;
641}
642
balrog1ae26a12008-09-28 23:19:47 +0000643/***********************************************************/
644/* QEMU Block devices */
645
balrog609497a2008-01-14 02:56:53 +0000646#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000647#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000648#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000649#define PFLASH_ALIAS "if=pflash"
650#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000651#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000652
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200653QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +0000654{
655 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200656 char optstr[1024];
657 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +0000658
thse4bcb142007-12-02 04:51:10 +0000659 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200660 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +0000661 va_end(ap);
662
Markus Armbruster8212c642010-02-10 19:52:18 +0100663 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200664 if (!opts) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200665 return NULL;
666 }
667 if (file)
668 qemu_opt_set(opts, "file", file);
669 return opts;
aliguorib01b1112009-02-11 15:20:20 +0000670}
671
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200672DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +0000673{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200674 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000675
676 /* seek interface, bus and unit */
677
Blue Swirl72cf2d42009-09-12 07:36:22 +0000678 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200679 if (dinfo->type == type &&
680 dinfo->bus == bus &&
681 dinfo->unit == unit)
682 return dinfo;
683 }
thse4bcb142007-12-02 04:51:10 +0000684
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200685 return NULL;
thse4bcb142007-12-02 04:51:10 +0000686}
687
Gerd Hoffmann2e810b32009-07-31 12:25:38 +0200688DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200689{
690 DriveInfo *dinfo;
691
Blue Swirl72cf2d42009-09-12 07:36:22 +0000692 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200693 if (strcmp(id, dinfo->id))
694 continue;
695 return dinfo;
696 }
697 return NULL;
698}
699
thsf60d39b2007-12-17 03:55:57 +0000700int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +0000701{
702 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200703 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000704
705 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000706 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200707 if(dinfo->type == type &&
708 dinfo->bus > max_bus)
709 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +0000710 }
711 return max_bus;
712}
713
aliguorifa879c62009-01-07 17:32:33 +0000714const char *drive_get_serial(BlockDriverState *bdrv)
715{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200716 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +0000717
Blue Swirl72cf2d42009-09-12 07:36:22 +0000718 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200719 if (dinfo->bdrv == bdrv)
720 return dinfo->serial;
721 }
aliguorifa879c62009-01-07 17:32:33 +0000722
723 return "\0";
724}
725
Kevin Wolff7850092009-11-27 13:25:36 +0100726BlockInterfaceErrorAction drive_get_on_error(
727 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +0000728{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200729 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +0000730
Blue Swirl72cf2d42009-09-12 07:36:22 +0000731 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200732 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100733 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200734 }
aliguori428c5702009-01-21 18:59:04 +0000735
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100736 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +0000737}
738
aurel32a1620fa2008-04-29 05:58:01 +0000739static void bdrv_format_print(void *opaque, const char *name)
740{
741 fprintf(stderr, " %s", name);
742}
743
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200744void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +0000745{
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200746 qemu_opts_del(dinfo->opts);
747 bdrv_delete(dinfo->bdrv);
748 QTAILQ_REMOVE(&drives, dinfo, next);
749 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +0000750}
751
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100752static int parse_block_error_action(const char *buf, int is_read)
753{
754 if (!strcmp(buf, "ignore")) {
755 return BLOCK_ERR_IGNORE;
756 } else if (!is_read && !strcmp(buf, "enospc")) {
757 return BLOCK_ERR_STOP_ENOSPC;
758 } else if (!strcmp(buf, "stop")) {
759 return BLOCK_ERR_STOP_ANY;
760 } else if (!strcmp(buf, "report")) {
761 return BLOCK_ERR_REPORT;
762 } else {
763 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
764 buf, is_read ? "read" : "write");
765 return -1;
766 }
767}
768
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200769DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200770 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +0000771{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200772 const char *buf;
773 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +0000774 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200775 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +0000776 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +0000777 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +0000778 enum { MEDIA_DISK, MEDIA_CDROM } media;
779 int bus_id, unit_id;
780 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +0000781 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +0000782 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +0000783 int max_devs;
784 int index;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200785 int ro = 0;
Christoph Hellwig763b6082010-04-06 19:12:04 +0200786 int bdrv_flags = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100787 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200788 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200789 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200790 int snapshot = 0;
791
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200792 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +0000793
thse4bcb142007-12-02 04:51:10 +0000794 translation = BIOS_ATA_TRANSLATION_AUTO;
thse4bcb142007-12-02 04:51:10 +0000795
Gerd Hoffmann4d007812009-08-31 14:23:57 +0200796 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +0000797 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000798 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000799 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +0000800 } else {
thsf60d39b2007-12-17 03:55:57 +0000801 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000802 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000803 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +0000804 }
805 media = MEDIA_DISK;
806
807 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200808 bus_id = qemu_opt_get_number(opts, "bus", 0);
809 unit_id = qemu_opt_get_number(opts, "unit", -1);
810 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +0000811
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200812 cyls = qemu_opt_get_number(opts, "cyls", 0);
813 heads = qemu_opt_get_number(opts, "heads", 0);
814 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +0000815
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200816 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +0200817 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +0000818
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200819 file = qemu_opt_get(opts, "file");
820 serial = qemu_opt_get(opts, "serial");
821
822 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +0000823 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +0000824 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +0000825 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000826 max_devs = MAX_IDE_DEVS;
827 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +0000828 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000829 max_devs = MAX_SCSI_DEVS;
830 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +0000831 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +0000832 max_devs = 0;
833 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +0000834 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +0000835 max_devs = 0;
836 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +0000837 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +0000838 max_devs = 0;
839 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +0000840 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +0000841 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +0000842 } else if (!strcmp(buf, "virtio")) {
843 type = IF_VIRTIO;
844 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000845 } else if (!strcmp(buf, "xen")) {
846 type = IF_XEN;
847 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +0200848 } else if (!strcmp(buf, "none")) {
849 type = IF_NONE;
850 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000851 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200852 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200853 return NULL;
thse4bcb142007-12-02 04:51:10 +0000854 }
855 }
856
thse4bcb142007-12-02 04:51:10 +0000857 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +0000858 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200859 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200860 return NULL;
thse4bcb142007-12-02 04:51:10 +0000861 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000862 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200863 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200864 return NULL;
thse4bcb142007-12-02 04:51:10 +0000865 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000866 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200867 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200868 return NULL;
thse4bcb142007-12-02 04:51:10 +0000869 }
870 }
871
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200872 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000873 if (!cyls) {
874 fprintf(stderr,
875 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200876 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200877 return NULL;
thse4bcb142007-12-02 04:51:10 +0000878 }
879 if (!strcmp(buf, "none"))
880 translation = BIOS_ATA_TRANSLATION_NONE;
881 else if (!strcmp(buf, "lba"))
882 translation = BIOS_ATA_TRANSLATION_LBA;
883 else if (!strcmp(buf, "auto"))
884 translation = BIOS_ATA_TRANSLATION_AUTO;
885 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200886 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200887 return NULL;
thse4bcb142007-12-02 04:51:10 +0000888 }
889 }
890
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200891 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000892 if (!strcmp(buf, "disk")) {
893 media = MEDIA_DISK;
894 } else if (!strcmp(buf, "cdrom")) {
895 if (cyls || secs || heads) {
896 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200897 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200898 return NULL;
thse4bcb142007-12-02 04:51:10 +0000899 }
900 media = MEDIA_CDROM;
901 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200902 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200903 return NULL;
thse4bcb142007-12-02 04:51:10 +0000904 }
905 }
906
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200907 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
Christoph Hellwig763b6082010-04-06 19:12:04 +0200908 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
909 bdrv_flags |= BDRV_O_NOCACHE;
910 } else if (!strcmp(buf, "writeback")) {
911 bdrv_flags |= BDRV_O_CACHE_WB;
Alexander Graf016f5cf2010-05-26 17:51:49 +0200912 } else if (!strcmp(buf, "unsafe")) {
913 bdrv_flags |= BDRV_O_CACHE_WB;
914 bdrv_flags |= BDRV_O_NO_FLUSH;
Christoph Hellwig763b6082010-04-06 19:12:04 +0200915 } else if (!strcmp(buf, "writethrough")) {
916 /* this is the default */
917 } else {
balrog33f00272007-12-24 14:33:24 +0000918 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200919 return NULL;
balrog33f00272007-12-24 14:33:24 +0000920 }
921 }
922
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200923#ifdef CONFIG_LINUX_AIO
924 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
Christoph Hellwig763b6082010-04-06 19:12:04 +0200925 if (!strcmp(buf, "native")) {
926 bdrv_flags |= BDRV_O_NATIVE_AIO;
927 } else if (!strcmp(buf, "threads")) {
928 /* this is the default */
929 } else {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200930 fprintf(stderr, "qemu: invalid aio option\n");
931 return NULL;
932 }
933 }
934#endif
935
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200936 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +0000937 if (strcmp(buf, "?") == 0) {
938 fprintf(stderr, "qemu: Supported formats:");
939 bdrv_iterate_format(bdrv_format_print, NULL);
940 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200941 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +0000942 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100943 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +0000944 if (!drv) {
945 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200946 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +0000947 }
948 }
949
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100950 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200951 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +0200952 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
aliguoriea8a5d72009-01-22 19:52:21 +0000953 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200954 return NULL;
aliguori428c5702009-01-21 18:59:04 +0000955 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100956
957 on_write_error = parse_block_error_action(buf, 0);
958 if (on_write_error < 0) {
959 return NULL;
960 }
961 }
962
963 on_read_error = BLOCK_ERR_REPORT;
964 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +0200965 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100966 fprintf(stderr, "rerror is no supported by this format\n");
967 return NULL;
968 }
969
970 on_read_error = parse_block_error_action(buf, 1);
971 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200972 return NULL;
aliguori428c5702009-01-21 18:59:04 +0000973 }
974 }
975
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200976 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200977 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200978 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200979 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200980 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200981 }
982
thse4bcb142007-12-02 04:51:10 +0000983 /* compute bus and unit according index */
984
985 if (index != -1) {
986 if (bus_id != 0 || unit_id != -1) {
987 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200988 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200989 return NULL;
thse4bcb142007-12-02 04:51:10 +0000990 }
991 if (max_devs == 0)
992 {
993 unit_id = index;
994 bus_id = 0;
995 } else {
996 unit_id = index % max_devs;
997 bus_id = index / max_devs;
998 }
999 }
1000
1001 /* if user doesn't specify a unit_id,
1002 * try to find the first free
1003 */
1004
1005 if (unit_id == -1) {
1006 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001007 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001008 unit_id++;
1009 if (max_devs && unit_id >= max_devs) {
1010 unit_id -= max_devs;
1011 bus_id++;
1012 }
1013 }
1014 }
1015
1016 /* check unit id */
1017
1018 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001019 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1020 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001021 return NULL;
thse4bcb142007-12-02 04:51:10 +00001022 }
1023
1024 /*
1025 * ignore multiple definitions
1026 */
1027
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001028 if (drive_get(type, bus_id, unit_id) != NULL) {
1029 *fatal_error = 0;
1030 return NULL;
1031 }
thse4bcb142007-12-02 04:51:10 +00001032
1033 /* init */
1034
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001035 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02001036 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001037 dinfo->id = qemu_strdup(buf);
1038 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001039 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001040 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001041 if (type == IF_IDE || type == IF_SCSI)
1042 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1043 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001044 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001045 devname, bus_id, mediastr, unit_id);
1046 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001047 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001048 devname, mediastr, unit_id);
1049 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001050 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001051 dinfo->devaddr = devaddr;
1052 dinfo->type = type;
1053 dinfo->bus = bus_id;
1054 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001055 dinfo->on_read_error = on_read_error;
1056 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001057 dinfo->opts = opts;
1058 if (serial)
1059 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001060 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00001061
thsf60d39b2007-12-17 03:55:57 +00001062 switch(type) {
thse4bcb142007-12-02 04:51:10 +00001063 case IF_IDE:
1064 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00001065 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00001066 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00001067 switch(media) {
1068 case MEDIA_DISK:
1069 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001070 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1071 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00001072 }
1073 break;
1074 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001075 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00001076 break;
1077 }
1078 break;
1079 case IF_SD:
1080 /* FIXME: This isn't really a floppy, but it's a reasonable
1081 approximation. */
1082 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001083 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00001084 break;
1085 case IF_PFLASH:
1086 case IF_MTD:
1087 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02001088 case IF_VIRTIO:
1089 /* add virtio block device */
1090 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1091 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1092 qemu_opt_set(opts, "drive", dinfo->id);
1093 if (devaddr)
1094 qemu_opt_set(opts, "addr", devaddr);
1095 break;
Paul Brookaae94602009-05-14 22:35:06 +01001096 case IF_COUNT:
1097 abort();
thse4bcb142007-12-02 04:51:10 +00001098 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001099 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001100 *fatal_error = 0;
1101 return NULL;
1102 }
aliguori9f7965c2008-10-14 14:42:54 +00001103 if (snapshot) {
Alexander Grafc3177282010-05-26 21:04:32 +02001104 /* always use cache=unsafe with snapshot */
Christoph Hellwig763b6082010-04-06 19:12:04 +02001105 bdrv_flags &= ~BDRV_O_CACHE_MASK;
Alexander Grafc3177282010-05-26 21:04:32 +02001106 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001107 }
1108
Naphtali Sprei2db7ad52010-03-23 12:17:16 +02001109 if (media == MEDIA_CDROM) {
1110 /* CDROM is fine for any interface, don't check. */
1111 ro = 1;
1112 } else if (ro == 1) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +02001113 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001114 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
Naphtali Sprei59f26892009-10-26 16:25:16 +02001115 return NULL;
1116 }
Naphtali Sprei59f26892009-10-26 16:25:16 +02001117 }
Naphtali Sprei2db7ad52010-03-23 12:17:16 +02001118
Naphtali Spreif5edb012010-01-17 16:48:13 +02001119 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001120
Kevin Wolfd6e90982010-03-31 14:40:27 +02001121 if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05001122 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1123 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001124 return NULL;
thse4bcb142007-12-02 04:51:10 +00001125 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001126
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001127 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00001128 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001129 *fatal_error = 0;
1130 return dinfo;
thse4bcb142007-12-02 04:51:10 +00001131}
1132
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001133static int drive_init_func(QemuOpts *opts, void *opaque)
1134{
1135 QEMUMachine *machine = opaque;
1136 int fatal_error = 0;
1137
1138 if (drive_init(opts, machine, &fatal_error) == NULL) {
1139 if (fatal_error)
1140 return 1;
1141 }
1142 return 0;
1143}
1144
1145static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1146{
1147 if (NULL == qemu_opt_get(opts, "snapshot")) {
1148 qemu_opt_set(opts, "snapshot", "on");
1149 }
1150 return 0;
1151}
1152
Jan Kiszka76e30d02009-07-02 00:19:02 +02001153void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1154{
1155 boot_set_handler = func;
1156 boot_set_opaque = opaque;
1157}
1158
1159int qemu_boot_set(const char *boot_devices)
1160{
1161 if (!boot_set_handler) {
1162 return -EINVAL;
1163 }
1164 return boot_set_handler(boot_set_opaque, boot_devices);
1165}
1166
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03001167static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001168{
1169 /* We just do some generic consistency checks */
1170 const char *p;
1171 int bitmap = 0;
1172
1173 for (p = devices; *p != '\0'; p++) {
1174 /* Allowed boot devices are:
1175 * a-b: floppy disk drives
1176 * c-f: IDE disk drives
1177 * g-m: machine implementation dependant drives
1178 * n-p: network devices
1179 * It's up to each machine implementation to check if the given boot
1180 * devices match the actual hardware implementation and firmware
1181 * features.
1182 */
1183 if (*p < 'a' || *p > 'p') {
1184 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1185 exit(1);
1186 }
1187 if (bitmap & (1 << (*p - 'a'))) {
1188 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1189 exit(1);
1190 }
1191 bitmap |= 1 << (*p - 'a');
1192 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001193}
1194
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001195static void restore_boot_devices(void *opaque)
1196{
1197 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001198 static int first = 1;
1199
1200 /* Restore boot order and remove ourselves after the first boot */
1201 if (first) {
1202 first = 0;
1203 return;
1204 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001205
1206 qemu_boot_set(standard_boot_devices);
1207
1208 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1209 qemu_free(standard_boot_devices);
1210}
1211
aliguori268a3622009-04-21 22:30:27 +00001212static void numa_add(const char *optarg)
1213{
1214 char option[128];
1215 char *endptr;
1216 unsigned long long value, endvalue;
1217 int nodenr;
1218
1219 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1220 if (!strcmp(option, "node")) {
1221 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1222 nodenr = nb_numa_nodes;
1223 } else {
1224 nodenr = strtoull(option, NULL, 10);
1225 }
1226
1227 if (get_param_value(option, 128, "mem", optarg) == 0) {
1228 node_mem[nodenr] = 0;
1229 } else {
1230 value = strtoull(option, &endptr, 0);
1231 switch (*endptr) {
1232 case 0: case 'M': case 'm':
1233 value <<= 20;
1234 break;
1235 case 'G': case 'g':
1236 value <<= 30;
1237 break;
1238 }
1239 node_mem[nodenr] = value;
1240 }
1241 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1242 node_cpumask[nodenr] = 0;
1243 } else {
1244 value = strtoull(option, &endptr, 10);
1245 if (value >= 64) {
1246 value = 63;
1247 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1248 } else {
1249 if (*endptr == '-') {
1250 endvalue = strtoull(endptr+1, &endptr, 10);
1251 if (endvalue >= 63) {
1252 endvalue = 62;
1253 fprintf(stderr,
1254 "only 63 CPUs in NUMA mode supported.\n");
1255 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001256 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +00001257 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001258 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +00001259 }
1260 }
1261 node_cpumask[nodenr] = value;
1262 }
1263 nb_numa_nodes++;
1264 }
1265 return;
1266}
1267
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001268static void smp_parse(const char *optarg)
1269{
1270 int smp, sockets = 0, threads = 0, cores = 0;
1271 char *endptr;
1272 char option[128];
1273
1274 smp = strtoul(optarg, &endptr, 10);
1275 if (endptr != optarg) {
1276 if (*endptr == ',') {
1277 endptr++;
1278 }
1279 }
1280 if (get_param_value(option, 128, "sockets", endptr) != 0)
1281 sockets = strtoull(option, NULL, 10);
1282 if (get_param_value(option, 128, "cores", endptr) != 0)
1283 cores = strtoull(option, NULL, 10);
1284 if (get_param_value(option, 128, "threads", endptr) != 0)
1285 threads = strtoull(option, NULL, 10);
1286 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1287 max_cpus = strtoull(option, NULL, 10);
1288
1289 /* compute missing values, prefer sockets over cores over threads */
1290 if (smp == 0 || sockets == 0) {
1291 sockets = sockets > 0 ? sockets : 1;
1292 cores = cores > 0 ? cores : 1;
1293 threads = threads > 0 ? threads : 1;
1294 if (smp == 0) {
1295 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001296 }
1297 } else {
1298 if (cores == 0) {
1299 threads = threads > 0 ? threads : 1;
1300 cores = smp / (sockets * threads);
1301 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +05301302 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001303 threads = smp / (cores * sockets);
1304 }
1305 }
1306 }
1307 smp_cpus = smp;
1308 smp_cores = cores > 0 ? cores : 1;
1309 smp_threads = threads > 0 ? threads : 1;
1310 if (max_cpus == 0)
1311 max_cpus = smp_cpus;
1312}
1313
bellard330d0412003-07-26 18:11:40 +00001314/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001315/* USB devices */
1316
aliguoric0f4ce72009-03-05 23:01:01 +00001317static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00001318{
1319 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001320 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001321
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001322 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001323 return -1;
1324
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001325 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1326 dev = usbdevice_create(devname);
1327 if (dev)
1328 goto done;
1329
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001330 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00001331 if (strstart(devname, "host:", &p)) {
1332 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00001333 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1334 dev = usb_bt_init(devname[2] ? hci_init(p) :
1335 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001336 } else {
1337 return -1;
1338 }
pbrook0d92ed32006-05-21 16:30:15 +00001339 if (!dev)
1340 return -1;
1341
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001342done:
bellarda594cfb2005-11-06 16:13:29 +00001343 return 0;
1344}
1345
aliguori1f3870a2008-08-21 19:27:48 +00001346static int usb_device_del(const char *devname)
1347{
1348 int bus_num, addr;
1349 const char *p;
1350
aliguori5d0c5752008-09-14 01:07:41 +00001351 if (strstart(devname, "host:", &p))
1352 return usb_host_device_close(p);
1353
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001354 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001355 return -1;
1356
1357 p = strchr(devname, '.');
1358 if (!p)
1359 return -1;
1360 bus_num = strtoul(devname, NULL, 0);
1361 addr = strtoul(p + 1, NULL, 0);
1362
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001363 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001364}
1365
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001366static int usb_parse(const char *cmdline)
1367{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001368 int r;
1369 r = usb_device_add(cmdline, 0);
1370 if (r < 0) {
1371 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1372 }
1373 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001374}
1375
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001376void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001377{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001378 const char *devname = qdict_get_str(qdict, "devname");
1379 if (usb_device_add(devname, 1) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001380 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001381 }
bellarda594cfb2005-11-06 16:13:29 +00001382}
1383
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001384void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001385{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001386 const char *devname = qdict_get_str(qdict, "devname");
1387 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001388 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001389 }
bellarda594cfb2005-11-06 16:13:29 +00001390}
1391
bellardf7cce892004-12-08 22:21:25 +00001392/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001393/* PCMCIA/Cardbus */
1394
1395static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001396 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001397 struct pcmcia_socket_entry_s *next;
1398} *pcmcia_sockets = 0;
1399
Paul Brookbc24a222009-05-10 01:44:56 +01001400void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001401{
1402 struct pcmcia_socket_entry_s *entry;
1403
1404 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1405 entry->socket = socket;
1406 entry->next = pcmcia_sockets;
1407 pcmcia_sockets = entry;
1408}
1409
Paul Brookbc24a222009-05-10 01:44:56 +01001410void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001411{
1412 struct pcmcia_socket_entry_s *entry, **ptr;
1413
1414 ptr = &pcmcia_sockets;
1415 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1416 if (entry->socket == socket) {
1417 *ptr = entry->next;
1418 qemu_free(entry);
1419 }
1420}
1421
aliguori376253e2009-03-05 23:01:23 +00001422void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001423{
1424 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001425
balrog201a51f2007-04-30 00:51:09 +00001426 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001427 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001428
1429 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001430 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1431 iter->socket->attached ? iter->socket->card_string :
1432 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001433}
1434
1435/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001436/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001437
bellardc4b1fcc2004-03-14 21:44:30 +00001438typedef struct IOHandlerRecord {
1439 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +01001440 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +00001441 IOHandler *fd_read;
1442 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00001443 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00001444 void *opaque;
1445 /* temporary data */
1446 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +01001447 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +00001448} IOHandlerRecord;
1449
Juan Quintela31d4ee62010-03-11 17:55:37 +01001450static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1451 QLIST_HEAD_INITIALIZER(io_handlers);
1452
bellardc4b1fcc2004-03-14 21:44:30 +00001453
bellard7c9d8e02005-11-15 22:16:05 +00001454/* XXX: fd_read_poll should be suppressed, but an API change is
1455 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00001456int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +01001457 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +00001458 IOHandler *fd_read,
1459 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001460 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00001461{
Juan Quintela31d4ee62010-03-11 17:55:37 +01001462 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +00001463
bellard7c9d8e02005-11-15 22:16:05 +00001464 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001465 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001466 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00001467 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00001468 break;
1469 }
bellard8a7ddc32004-03-31 19:00:16 +00001470 }
bellard7c9d8e02005-11-15 22:16:05 +00001471 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001472 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001473 if (ioh->fd == fd)
1474 goto found;
1475 }
1476 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +01001477 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +00001478 found:
1479 ioh->fd = fd;
1480 ioh->fd_read_poll = fd_read_poll;
1481 ioh->fd_read = fd_read;
1482 ioh->fd_write = fd_write;
1483 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001484 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001485 }
bellard7c9d8e02005-11-15 22:16:05 +00001486 return 0;
1487}
1488
ths5fafdf22007-09-16 21:08:06 +00001489int qemu_set_fd_handler(int fd,
1490 IOHandler *fd_read,
1491 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001492 void *opaque)
1493{
1494 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001495}
1496
bellard8a7ddc32004-03-31 19:00:16 +00001497/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001498/* machine registration */
1499
blueswir1bdaf78e2008-10-04 07:24:27 +00001500static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001501QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001502
1503int qemu_register_machine(QEMUMachine *m)
1504{
1505 QEMUMachine **pm;
1506 pm = &first_machine;
1507 while (*pm != NULL)
1508 pm = &(*pm)->next;
1509 m->next = NULL;
1510 *pm = m;
1511 return 0;
1512}
1513
pbrook9596ebb2007-11-18 01:44:38 +00001514static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001515{
1516 QEMUMachine *m;
1517
1518 for(m = first_machine; m != NULL; m = m->next) {
1519 if (!strcmp(m->name, name))
1520 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001521 if (m->alias && !strcmp(m->alias, name))
1522 return m;
bellardcc1daa42005-06-05 14:49:17 +00001523 }
1524 return NULL;
1525}
1526
Anthony Liguori0c257432009-05-21 20:41:01 -05001527static QEMUMachine *find_default_machine(void)
1528{
1529 QEMUMachine *m;
1530
1531 for(m = first_machine; m != NULL; m = m->next) {
1532 if (m->is_default) {
1533 return m;
1534 }
1535 }
1536 return NULL;
1537}
1538
bellardcc1daa42005-06-05 14:49:17 +00001539/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001540/* main execution loop */
1541
pbrook9596ebb2007-11-18 01:44:38 +00001542static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001543{
aliguori7d957bd2009-01-15 22:14:11 +00001544 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001545 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001546 DisplayChangeListener *dcl = ds->listeners;
1547
Sheng Yang62a27442010-01-26 19:21:16 +08001548 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001549 dpy_refresh(ds);
1550
1551 while (dcl != NULL) {
1552 if (dcl->gui_timer_interval &&
1553 dcl->gui_timer_interval < interval)
1554 interval = dcl->gui_timer_interval;
1555 dcl = dcl->next;
1556 }
1557 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001558}
1559
blueswir19043b622009-01-21 19:28:13 +00001560static void nographic_update(void *opaque)
1561{
1562 uint64_t interval = GUI_REFRESH_INTERVAL;
1563
Sheng Yang62a27442010-01-26 19:21:16 +08001564 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001565 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1566}
1567
bellard0bd48852005-11-11 00:00:47 +00001568struct vm_change_state_entry {
1569 VMChangeStateHandler *cb;
1570 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001571 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001572};
1573
Blue Swirl72cf2d42009-09-12 07:36:22 +00001574static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001575
1576VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1577 void *opaque)
1578{
1579 VMChangeStateEntry *e;
1580
1581 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001582
1583 e->cb = cb;
1584 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001585 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001586 return e;
1587}
1588
1589void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1590{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001591 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001592 qemu_free (e);
1593}
1594
Blue Swirl296af7c2010-03-29 19:23:50 +00001595void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001596{
1597 VMChangeStateEntry *e;
1598
1599 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001600 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001601 }
1602}
1603
bellard8a7ddc32004-03-31 19:00:16 +00001604void vm_start(void)
1605{
1606 if (!vm_running) {
1607 cpu_enable_ticks();
1608 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001609 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001610 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001611 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001612 }
1613}
1614
bellardbb0c6722004-06-20 12:37:32 +00001615/* reset/shutdown handler */
1616
1617typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001618 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001619 QEMUResetHandler *func;
1620 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001621} QEMUResetEntry;
1622
Blue Swirl72cf2d42009-09-12 07:36:22 +00001623static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1624 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001625static int reset_requested;
1626static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001627static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001628int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001629int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001630
aurel32cf7a2fe2008-03-18 06:53:05 +00001631int qemu_shutdown_requested(void)
1632{
1633 int r = shutdown_requested;
1634 shutdown_requested = 0;
1635 return r;
1636}
1637
1638int qemu_reset_requested(void)
1639{
1640 int r = reset_requested;
1641 reset_requested = 0;
1642 return r;
1643}
1644
1645int qemu_powerdown_requested(void)
1646{
1647 int r = powerdown_requested;
1648 powerdown_requested = 0;
1649 return r;
1650}
1651
aliguorie5689022009-04-24 18:03:54 +00001652static int qemu_debug_requested(void)
1653{
1654 int r = debug_requested;
1655 debug_requested = 0;
1656 return r;
1657}
1658
aliguori6e29f5d2009-04-24 18:04:02 +00001659static int qemu_vmstop_requested(void)
1660{
1661 int r = vmstop_requested;
1662 vmstop_requested = 0;
1663 return r;
1664}
1665
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001666void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001667{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001668 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001669
bellardbb0c6722004-06-20 12:37:32 +00001670 re->func = func;
1671 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001672 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001673}
1674
Jan Kiszkadda9b292009-07-02 00:19:02 +02001675void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001676{
1677 QEMUResetEntry *re;
1678
Blue Swirl72cf2d42009-09-12 07:36:22 +00001679 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001680 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001681 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001682 qemu_free(re);
1683 return;
1684 }
1685 }
1686}
1687
1688void qemu_system_reset(void)
1689{
1690 QEMUResetEntry *re, *nre;
1691
1692 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001693 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001694 re->func(re->opaque);
1695 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001696 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001697 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001698}
1699
1700void qemu_system_reset_request(void)
1701{
bellardd1beab82006-10-02 19:44:22 +00001702 if (no_reboot) {
1703 shutdown_requested = 1;
1704 } else {
1705 reset_requested = 1;
1706 }
aliguorid9f75a42009-04-24 18:03:11 +00001707 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001708}
1709
1710void qemu_system_shutdown_request(void)
1711{
1712 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001713 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001714}
1715
bellard34751872005-07-02 14:31:34 +00001716void qemu_system_powerdown_request(void)
1717{
1718 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001719 qemu_notify_event();
1720}
1721
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001722void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001723{
1724 IOHandlerRecord *ioh;
1725 fd_set rfds, wfds, xfds;
1726 int ret, nfds;
1727 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001728 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001729
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001730 if (nonblocking)
1731 timeout = 0;
1732 else {
1733 timeout = qemu_calculate_timeout();
1734 qemu_bh_update_timeout(&timeout);
1735 }
aliguori56f3a5d2008-10-31 18:07:17 +00001736
Jes Sorensen0d93ca72010-06-10 11:42:18 +02001737 os_host_main_loop_wait(&timeout);
aliguori56f3a5d2008-10-31 18:07:17 +00001738
bellardfd1dff42006-02-01 21:29:26 +00001739 /* poll any events */
1740 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001741 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001742 FD_ZERO(&rfds);
1743 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001744 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001745 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001746 if (ioh->deleted)
1747 continue;
bellardfd1dff42006-02-01 21:29:26 +00001748 if (ioh->fd_read &&
1749 (!ioh->fd_read_poll ||
1750 ioh->fd_read_poll(ioh->opaque) != 0)) {
1751 FD_SET(ioh->fd, &rfds);
1752 if (ioh->fd > nfds)
1753 nfds = ioh->fd;
1754 }
1755 if (ioh->fd_write) {
1756 FD_SET(ioh->fd, &wfds);
1757 if (ioh->fd > nfds)
1758 nfds = ioh->fd;
1759 }
1760 }
ths3b46e622007-09-17 08:09:54 +00001761
aliguori56f3a5d2008-10-31 18:07:17 +00001762 tv.tv_sec = timeout / 1000;
1763 tv.tv_usec = (timeout % 1000) * 1000;
1764
Jan Kiszkad918f232009-06-24 14:42:30 +02001765 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1766
aliguori48708522009-04-24 18:03:49 +00001767 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001768 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001769 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001770 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001771 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001772
Juan Quintela31d4ee62010-03-11 17:55:37 +01001773 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001774 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001775 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001776 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001777 continue;
1778 }
1779 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1780 ioh->fd_read(ioh->opaque);
1781 }
1782 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1783 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001784 }
thscafffd42007-02-28 21:59:44 +00001785 }
bellardfd1dff42006-02-01 21:29:26 +00001786 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001787
1788 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001789
Paolo Bonzinib6964822010-03-10 11:38:45 +01001790 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001791
pbrook423f0742007-05-23 00:06:54 +00001792 /* Check bottom-halves last in case any of the earlier events triggered
1793 them. */
1794 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001795
bellard5905b2e2004-08-01 21:53:26 +00001796}
1797
aliguori43b96852009-04-24 18:03:33 +00001798static int vm_can_run(void)
1799{
1800 if (powerdown_requested)
1801 return 0;
1802 if (reset_requested)
1803 return 0;
1804 if (shutdown_requested)
1805 return 0;
aliguorie5689022009-04-24 18:03:54 +00001806 if (debug_requested)
1807 return 0;
aliguori43b96852009-04-24 18:03:33 +00001808 return 1;
1809}
1810
Blue Swirld9c32312009-08-09 08:42:19 +00001811qemu_irq qemu_system_powerdown;
1812
aliguori43b96852009-04-24 18:03:33 +00001813static void main_loop(void)
1814{
aliguori6e29f5d2009-04-24 18:04:02 +00001815 int r;
aliguori43b96852009-04-24 18:03:33 +00001816
Blue Swirl7277e022010-04-12 17:19:06 +00001817 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001818
aliguori6e29f5d2009-04-24 18:04:02 +00001819 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001820 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001821 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001822#ifdef CONFIG_PROFILER
1823 int64_t ti;
1824#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001825#ifndef CONFIG_IOTHREAD
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001826 nonblocking = tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00001827#endif
aliguori43b96852009-04-24 18:03:33 +00001828#ifdef CONFIG_PROFILER
1829 ti = profile_getclock();
1830#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001831 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001832#ifdef CONFIG_PROFILER
1833 dev_time += profile_getclock() - ti;
1834#endif
aliguorie5689022009-04-24 18:03:54 +00001835 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001836
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001837 if ((r = qemu_debug_requested())) {
1838 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001839 }
aliguori43b96852009-04-24 18:03:33 +00001840 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001841 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001842 if (no_shutdown) {
1843 vm_stop(0);
1844 no_shutdown = 0;
1845 } else
1846 break;
1847 }
aliguorid6dc3d42009-04-24 18:04:07 +00001848 if (qemu_reset_requested()) {
1849 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001850 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001851 resume_all_vcpus();
1852 }
Blue Swirld9c32312009-08-09 08:42:19 +00001853 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001854 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001855 qemu_irq_raise(qemu_system_powerdown);
1856 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001857 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001858 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001859 }
aliguori43b96852009-04-24 18:03:33 +00001860 }
aliguorid6dc3d42009-04-24 18:04:07 +00001861 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001862}
1863
pbrook9bd7e6d2009-04-07 22:58:45 +00001864static void version(void)
1865{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001866 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00001867}
1868
ths15f82202007-06-29 23:26:08 +00001869static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00001870{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001871 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00001872#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1873 opt_help
blueswir15824d652009-03-28 06:44:27 +00001874#define DEFHEADING(text) stringify(text) "\n"
Jes Sorensen9f167322010-06-10 11:42:24 +02001875#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001876#undef DEF
1877#undef DEFHEADING
1878#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001879 ;
1880 version();
1881 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03001882 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02001883 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001884 "\n"
1885 "%s\n"
malc3f020d72010-02-08 12:04:56 +03001886 "During emulation, the following keys are useful:\n"
1887 "ctrl-alt-f toggle full screen\n"
1888 "ctrl-alt-n switch to virtual console 'n'\n"
1889 "ctrl-alt toggle mouse and keyboard grab\n"
1890 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01001891 "When using -nographic, press 'ctrl-a h' to get some help.\n",
1892 "qemu",
1893 options_help);
ths15f82202007-06-29 23:26:08 +00001894 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00001895}
1896
bellardcd6f1162004-05-13 22:02:20 +00001897#define HAS_ARG 0x0001
1898
bellardcd6f1162004-05-13 22:02:20 +00001899typedef struct QEMUOption {
1900 const char *name;
1901 int flags;
1902 int index;
Blue Swirlad960902010-03-29 19:23:52 +00001903 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00001904} QEMUOption;
1905
blueswir1dbed7e42008-10-01 19:38:09 +00001906static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00001907 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
1908#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
1909 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00001910#define DEFHEADING(text)
Jes Sorensen9f167322010-06-10 11:42:24 +02001911#include "qemu-options.def"
blueswir15824d652009-03-28 06:44:27 +00001912#undef DEF
1913#undef DEFHEADING
1914#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00001915 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00001916};
malc3893c122008-09-28 00:42:05 +00001917static void select_vgahw (const char *p)
1918{
1919 const char *opts;
1920
Gerd Hoffmann64465292009-12-08 13:11:45 +01001921 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10001922 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00001923 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001924 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00001925 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001926 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00001927 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001928 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00001929 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10001930 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00001931 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00001932 invalid_vga:
1933 fprintf(stderr, "Unknown vga type: %s\n", p);
1934 exit(1);
1935 }
malccb5a7aa2008-09-28 00:42:12 +00001936 while (*opts) {
1937 const char *nextopt;
1938
1939 if (strstart(opts, ",retrace=", &nextopt)) {
1940 opts = nextopt;
1941 if (strstart(opts, "dumb", &nextopt))
1942 vga_retrace_method = VGA_RETRACE_DUMB;
1943 else if (strstart(opts, "precise", &nextopt))
1944 vga_retrace_method = VGA_RETRACE_PRECISE;
1945 else goto invalid_vga;
1946 } else goto invalid_vga;
1947 opts = nextopt;
1948 }
malc3893c122008-09-28 00:42:05 +00001949}
1950
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001951static int balloon_parse(const char *arg)
1952{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001953 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001954
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001955 if (strcmp(arg, "none") == 0) {
1956 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001957 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001958
1959 if (!strncmp(arg, "virtio", 6)) {
1960 if (arg[6] == ',') {
1961 /* have params -> parse them */
Markus Armbruster8212c642010-02-10 19:52:18 +01001962 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02001963 if (!opts)
1964 return -1;
1965 } else {
1966 /* create empty opts */
1967 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1968 }
1969 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
1970 return 0;
1971 }
1972
1973 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001974}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02001975
Paul Brook5cea8592009-05-30 00:52:44 +01001976char *qemu_find_file(int type, const char *name)
1977{
1978 int len;
1979 const char *subdir;
1980 char *buf;
1981
1982 /* If name contains path separators then try it as a straight path. */
1983 if ((strchr(name, '/') || strchr(name, '\\'))
1984 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02001985 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01001986 }
1987 switch (type) {
1988 case QEMU_FILE_TYPE_BIOS:
1989 subdir = "";
1990 break;
1991 case QEMU_FILE_TYPE_KEYMAP:
1992 subdir = "keymaps/";
1993 break;
1994 default:
1995 abort();
1996 }
1997 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
1998 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00001999 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002000 if (access(buf, R_OK)) {
2001 qemu_free(buf);
2002 return NULL;
2003 }
2004 return buf;
2005}
2006
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002007static int device_help_func(QemuOpts *opts, void *opaque)
2008{
2009 return qdev_device_help(opts);
2010}
2011
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002012static int device_init_func(QemuOpts *opts, void *opaque)
2013{
2014 DeviceState *dev;
2015
2016 dev = qdev_device_add(opts);
2017 if (!dev)
2018 return -1;
2019 return 0;
2020}
2021
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002022static int chardev_init_func(QemuOpts *opts, void *opaque)
2023{
2024 CharDriverState *chr;
2025
2026 chr = qemu_chr_open_opts(opts, NULL);
2027 if (!chr)
2028 return -1;
2029 return 0;
2030}
2031
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302032#ifdef CONFIG_LINUX
2033static int fsdev_init_func(QemuOpts *opts, void *opaque)
2034{
2035 int ret;
2036 ret = qemu_fsdev_add(opts);
2037
2038 return ret;
2039}
2040#endif
2041
Gerd Hoffmann88589342009-12-08 13:11:50 +01002042static int mon_init_func(QemuOpts *opts, void *opaque)
2043{
2044 CharDriverState *chr;
2045 const char *chardev;
2046 const char *mode;
2047 int flags;
2048
2049 mode = qemu_opt_get(opts, "mode");
2050 if (mode == NULL) {
2051 mode = "readline";
2052 }
2053 if (strcmp(mode, "readline") == 0) {
2054 flags = MONITOR_USE_READLINE;
2055 } else if (strcmp(mode, "control") == 0) {
2056 flags = MONITOR_USE_CONTROL;
2057 } else {
2058 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2059 exit(1);
2060 }
2061
2062 if (qemu_opt_get_bool(opts, "default", 0))
2063 flags |= MONITOR_IS_DEFAULT;
2064
2065 chardev = qemu_opt_get(opts, "chardev");
2066 chr = qemu_chr_find(chardev);
2067 if (chr == NULL) {
2068 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2069 exit(1);
2070 }
2071
2072 monitor_init(chr, flags);
2073 return 0;
2074}
2075
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002076static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002077{
2078 static int monitor_device_index = 0;
2079 QemuOpts *opts;
2080 const char *p;
2081 char label[32];
2082 int def = 0;
2083
2084 if (strstart(optarg, "chardev:", &p)) {
2085 snprintf(label, sizeof(label), "%s", p);
2086 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002087 snprintf(label, sizeof(label), "compat_monitor%d",
2088 monitor_device_index);
2089 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002090 def = 1;
2091 }
2092 opts = qemu_chr_parse_compat(label, optarg);
2093 if (!opts) {
2094 fprintf(stderr, "parse error: %s\n", optarg);
2095 exit(1);
2096 }
2097 }
2098
2099 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2100 if (!opts) {
2101 fprintf(stderr, "duplicate chardev: %s\n", label);
2102 exit(1);
2103 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002104 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002105 qemu_opt_set(opts, "chardev", label);
2106 if (def)
2107 qemu_opt_set(opts, "default", "on");
2108 monitor_device_index++;
2109}
2110
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002111struct device_config {
2112 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002113 DEV_USB, /* -usbdevice */
2114 DEV_BT, /* -bt */
2115 DEV_SERIAL, /* -serial */
2116 DEV_PARALLEL, /* -parallel */
2117 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002118 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002119 } type;
2120 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002121 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002122};
Blue Swirl72cf2d42009-09-12 07:36:22 +00002123QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002124
2125static void add_device_config(int type, const char *cmdline)
2126{
2127 struct device_config *conf;
2128
2129 conf = qemu_mallocz(sizeof(*conf));
2130 conf->type = type;
2131 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002132 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002133}
2134
2135static int foreach_device_config(int type, int (*func)(const char *cmdline))
2136{
2137 struct device_config *conf;
2138 int rc;
2139
Blue Swirl72cf2d42009-09-12 07:36:22 +00002140 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002141 if (conf->type != type)
2142 continue;
2143 rc = func(conf->cmdline);
2144 if (0 != rc)
2145 return rc;
2146 }
2147 return 0;
2148}
2149
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002150static int serial_parse(const char *devname)
2151{
2152 static int index = 0;
2153 char label[32];
2154
2155 if (strcmp(devname, "none") == 0)
2156 return 0;
2157 if (index == MAX_SERIAL_PORTS) {
2158 fprintf(stderr, "qemu: too many serial ports\n");
2159 exit(1);
2160 }
2161 snprintf(label, sizeof(label), "serial%d", index);
2162 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2163 if (!serial_hds[index]) {
2164 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2165 devname, strerror(errno));
2166 return -1;
2167 }
2168 index++;
2169 return 0;
2170}
2171
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002172static int parallel_parse(const char *devname)
2173{
2174 static int index = 0;
2175 char label[32];
2176
2177 if (strcmp(devname, "none") == 0)
2178 return 0;
2179 if (index == MAX_PARALLEL_PORTS) {
2180 fprintf(stderr, "qemu: too many parallel ports\n");
2181 exit(1);
2182 }
2183 snprintf(label, sizeof(label), "parallel%d", index);
2184 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2185 if (!parallel_hds[index]) {
2186 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2187 devname, strerror(errno));
2188 return -1;
2189 }
2190 index++;
2191 return 0;
2192}
2193
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002194static int virtcon_parse(const char *devname)
2195{
2196 static int index = 0;
2197 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302198 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002199
2200 if (strcmp(devname, "none") == 0)
2201 return 0;
2202 if (index == MAX_VIRTIO_CONSOLES) {
2203 fprintf(stderr, "qemu: too many virtio consoles\n");
2204 exit(1);
2205 }
Amit Shah392ecf52010-01-21 16:19:23 +05302206
2207 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2208 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2209
2210 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2211 qemu_opt_set(dev_opts, "driver", "virtconsole");
2212
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002213 snprintf(label, sizeof(label), "virtcon%d", index);
2214 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2215 if (!virtcon_hds[index]) {
2216 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2217 devname, strerror(errno));
2218 return -1;
2219 }
Amit Shah392ecf52010-01-21 16:19:23 +05302220 qemu_opt_set(dev_opts, "chardev", label);
2221
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002222 index++;
2223 return 0;
2224}
2225
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002226static int debugcon_parse(const char *devname)
2227{
2228 QemuOpts *opts;
2229
2230 if (!qemu_chr_open("debugcon", devname, NULL)) {
2231 exit(1);
2232 }
2233 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2234 if (!opts) {
2235 fprintf(stderr, "qemu: already have a debugcon device\n");
2236 exit(1);
2237 }
2238 qemu_opt_set(opts, "driver", "isa-debugcon");
2239 qemu_opt_set(opts, "chardev", "debugcon");
2240 return 0;
2241}
2242
Anthony Liguori6530a972010-01-22 09:18:06 -06002243static const QEMUOption *lookup_opt(int argc, char **argv,
2244 const char **poptarg, int *poptind)
2245{
2246 const QEMUOption *popt;
2247 int optind = *poptind;
2248 char *r = argv[optind];
2249 const char *optarg;
2250
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002251 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002252 optind++;
2253 /* Treat --foo the same as -foo. */
2254 if (r[1] == '-')
2255 r++;
2256 popt = qemu_options;
2257 for(;;) {
2258 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002259 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002260 exit(1);
2261 }
2262 if (!strcmp(popt->name, r + 1))
2263 break;
2264 popt++;
2265 }
2266 if (popt->flags & HAS_ARG) {
2267 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002268 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002269 exit(1);
2270 }
2271 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002272 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002273 } else {
2274 optarg = NULL;
2275 }
2276
2277 *poptarg = optarg;
2278 *poptind = optind;
2279
2280 return popt;
2281}
2282
malc902b3d52008-12-10 19:18:40 +00002283int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002284{
aliguori59030a82009-04-05 18:43:41 +00002285 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002286 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002287 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002288 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002289 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002290 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002291 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002292 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002293 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002294 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002295 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00002296 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002297 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002298 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002299 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002300 const char *cpu_model;
bellard26a5f132008-05-28 12:30:31 +00002301 int tb_size;
ths93815bc2007-03-19 15:58:31 +00002302 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002303 const char *incoming = NULL;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002304 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06002305 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00002306
Markus Armbruster65abca02010-02-24 14:37:14 +01002307 error_set_progname(argv[0]);
2308
Jan Kiszka68752042009-09-15 13:36:04 +02002309 init_clocks();
2310
malc902b3d52008-12-10 19:18:40 +00002311 qemu_cache_utils_init(envp);
2312
Blue Swirl72cf2d42009-09-12 07:36:22 +00002313 QLIST_INIT (&vm_change_state_head);
Jes Sorensenfe98ac12010-06-10 11:42:21 +02002314 os_setup_early_signal_handling();
bellardbe995c22006-06-25 16:25:21 +00002315
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002316 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002317 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002318 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002319 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002320 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002321 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002322 kernel_filename = NULL;
2323 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002324 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002325 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002326
aliguori268a3622009-04-21 22:30:27 +00002327 for (i = 0; i < MAX_NODES; i++) {
2328 node_mem[i] = 0;
2329 node_cpumask[i] = 0;
2330 }
2331
aliguori268a3622009-04-21 22:30:27 +00002332 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002333 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002334
bellard26a5f132008-05-28 12:30:31 +00002335 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00002336 autostart= 1;
2337
Anthony Liguori292444c2010-01-21 10:57:58 -06002338 /* first pass of option parsing */
2339 optind = 1;
2340 while (optind < argc) {
2341 if (argv[optind][0] != '-') {
2342 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002343 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002344 continue;
2345 } else {
2346 const QEMUOption *popt;
2347
2348 popt = lookup_opt(argc, argv, &optarg, &optind);
2349 switch (popt->index) {
2350 case QEMU_OPTION_nodefconfig:
2351 defconfig=0;
2352 break;
2353 }
2354 }
2355 }
2356
2357 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002358 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002359
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002360 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002361 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002362 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002363 }
2364
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002365 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002366 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002367 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002368 }
2369 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002370 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002371
2372 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002373 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002374 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002375 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002376 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002377 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002378 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00002379 } else {
2380 const QEMUOption *popt;
2381
Anthony Liguori6530a972010-01-22 09:18:06 -06002382 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002383 if (!(popt->arch_mask & arch_type)) {
2384 printf("Option %s not supported for this target\n", popt->name);
2385 exit(1);
2386 }
bellardcd6f1162004-05-13 22:02:20 +00002387 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002388 case QEMU_OPTION_M:
2389 machine = find_machine(optarg);
2390 if (!machine) {
2391 QEMUMachine *m;
2392 printf("Supported machines are:\n");
2393 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002394 if (m->alias)
2395 printf("%-10s %s (alias of %s)\n",
2396 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00002397 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00002398 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05002399 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00002400 }
ths15f82202007-06-29 23:26:08 +00002401 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00002402 }
2403 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002404 case QEMU_OPTION_cpu:
2405 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002406 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002407 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002408 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002409 } else {
2410 cpu_model = optarg;
2411 }
2412 break;
bellardcd6f1162004-05-13 22:02:20 +00002413 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002414 initrd_filename = optarg;
2415 break;
bellardcd6f1162004-05-13 22:02:20 +00002416 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00002417 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002418 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00002419 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002420 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00002421 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00002422 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00002423 translation == BIOS_ATA_TRANSLATION_LBA ?
2424 ",trans=lba" :
2425 translation == BIOS_ATA_TRANSLATION_NONE ?
2426 ",trans=none" : "");
2427 break;
bellardcd6f1162004-05-13 22:02:20 +00002428 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002429 case QEMU_OPTION_hdc:
2430 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00002431 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00002432 break;
thse4bcb142007-12-02 04:51:10 +00002433 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00002434 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00002435 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002436 case QEMU_OPTION_set:
2437 if (qemu_set_option(optarg) != 0)
2438 exit(1);
2439 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002440 case QEMU_OPTION_global:
2441 if (qemu_global_option(optarg) != 0)
2442 exit(1);
2443 break;
balrog3e3d5812007-04-30 02:09:25 +00002444 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00002445 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00002446 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002447 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00002448 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00002449 break;
j_mayer86f55662007-04-24 06:52:59 +00002450 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00002451 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00002452 break;
bellardcd6f1162004-05-13 22:02:20 +00002453 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002454 snapshot = 1;
2455 break;
bellardcd6f1162004-05-13 22:02:20 +00002456 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002457 {
bellard330d0412003-07-26 18:11:40 +00002458 const char *p;
2459 p = optarg;
2460 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002461 if (cyls < 1 || cyls > 16383)
2462 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002463 if (*p != ',')
2464 goto chs_fail;
2465 p++;
2466 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002467 if (heads < 1 || heads > 16)
2468 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002469 if (*p != ',')
2470 goto chs_fail;
2471 p++;
2472 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002473 if (secs < 1 || secs > 63)
2474 goto chs_fail;
2475 if (*p == ',') {
2476 p++;
2477 if (!strcmp(p, "none"))
2478 translation = BIOS_ATA_TRANSLATION_NONE;
2479 else if (!strcmp(p, "lba"))
2480 translation = BIOS_ATA_TRANSLATION_LBA;
2481 else if (!strcmp(p, "auto"))
2482 translation = BIOS_ATA_TRANSLATION_AUTO;
2483 else
2484 goto chs_fail;
2485 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002486 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002487 fprintf(stderr, "qemu: invalid physical CHS format\n");
2488 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002489 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002490 if (hda_opts != NULL) {
2491 char num[16];
2492 snprintf(num, sizeof(num), "%d", cyls);
2493 qemu_opt_set(hda_opts, "cyls", num);
2494 snprintf(num, sizeof(num), "%d", heads);
2495 qemu_opt_set(hda_opts, "heads", num);
2496 snprintf(num, sizeof(num), "%d", secs);
2497 qemu_opt_set(hda_opts, "secs", num);
2498 if (translation == BIOS_ATA_TRANSLATION_LBA)
2499 qemu_opt_set(hda_opts, "trans", "lba");
2500 if (translation == BIOS_ATA_TRANSLATION_NONE)
2501 qemu_opt_set(hda_opts, "trans", "none");
2502 }
bellard330d0412003-07-26 18:11:40 +00002503 }
2504 break;
aliguori268a3622009-04-21 22:30:27 +00002505 case QEMU_OPTION_numa:
2506 if (nb_numa_nodes >= MAX_NODES) {
2507 fprintf(stderr, "qemu: too many NUMA nodes\n");
2508 exit(1);
2509 }
2510 numa_add(optarg);
2511 break;
bellardcd6f1162004-05-13 22:02:20 +00002512 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002513 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002514 break;
balrog4d3b6f62008-02-10 16:33:14 +00002515#ifdef CONFIG_CURSES
2516 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002517 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002518 break;
2519#endif
balroga171fe32007-04-30 01:48:07 +00002520 case QEMU_OPTION_portrait:
2521 graphic_rotate = 1;
2522 break;
bellardcd6f1162004-05-13 22:02:20 +00002523 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002524 kernel_filename = optarg;
2525 break;
bellardcd6f1162004-05-13 22:02:20 +00002526 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002527 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002528 break;
bellardcd6f1162004-05-13 22:02:20 +00002529 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002530 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002531 break;
bellardcd6f1162004-05-13 22:02:20 +00002532 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002533 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002534 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002535 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002536 };
2537 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002538 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002539 int legacy = 0;
2540
2541 if (!strchr(optarg, '=')) {
2542 legacy = 1;
2543 pstrcpy(buf, sizeof(buf), optarg);
2544 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2545 fprintf(stderr,
2546 "qemu: unknown boot parameter '%s' in '%s'\n",
2547 buf, optarg);
2548 exit(1);
2549 }
2550
2551 if (legacy ||
2552 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002553 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002554 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002555 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002556 if (!legacy) {
2557 if (get_param_value(buf, sizeof(buf),
2558 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002559 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002560 standard_boot_devices = qemu_strdup(boot_devices);
2561 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2562 qemu_register_reset(restore_boot_devices,
2563 standard_boot_devices);
2564 }
Jan Kiszka95387492009-07-02 00:19:02 +02002565 if (get_param_value(buf, sizeof(buf),
2566 "menu", optarg)) {
2567 if (!strcmp(buf, "on")) {
2568 boot_menu = 1;
2569 } else if (!strcmp(buf, "off")) {
2570 boot_menu = 0;
2571 } else {
2572 fprintf(stderr,
2573 "qemu: invalid option value '%s'\n",
2574 buf);
2575 exit(1);
2576 }
2577 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002578 }
bellard36b486b2003-11-11 13:36:08 +00002579 }
2580 break;
bellardcd6f1162004-05-13 22:02:20 +00002581 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002582 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002583 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002584 break;
bellard52ca8d62006-06-14 16:03:05 +00002585 case QEMU_OPTION_no_fd_bootchk:
2586 fd_bootchk = 0;
2587 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002588 case QEMU_OPTION_netdev:
2589 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2590 exit(1);
2591 }
2592 break;
bellard7c9d8e02005-11-15 22:16:05 +00002593 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01002594 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002595 exit(1);
2596 }
bellard702c6512004-04-02 21:21:32 +00002597 break;
bellardc7f74642004-08-24 21:57:12 +00002598#ifdef CONFIG_SLIRP
2599 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002600 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002601 break;
ths47d5d012007-02-20 00:05:08 +00002602 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002603 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002604 break;
bellard9bf05442004-08-25 22:12:49 +00002605 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002606 if (net_slirp_redir(optarg) < 0)
2607 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002608 break;
bellardc7f74642004-08-24 21:57:12 +00002609#endif
balrogdc72ac12008-11-09 00:04:26 +00002610 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002611 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002612 break;
bellard1d14ffa2005-10-30 18:58:22 +00002613 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002614 if (!(audio_available())) {
2615 printf("Option %s not supported for this target\n", popt->name);
2616 exit(1);
2617 }
bellard1d14ffa2005-10-30 18:58:22 +00002618 AUD_help ();
2619 exit (0);
2620 break;
2621 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002622 if (!(audio_available())) {
2623 printf("Option %s not supported for this target\n", popt->name);
2624 exit(1);
2625 }
bellard1d14ffa2005-10-30 18:58:22 +00002626 select_soundhw (optarg);
2627 break;
bellardcd6f1162004-05-13 22:02:20 +00002628 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002629 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002630 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002631 case QEMU_OPTION_version:
2632 version();
2633 exit(0);
2634 break;
aurel3200f82b82008-04-27 21:12:55 +00002635 case QEMU_OPTION_m: {
2636 uint64_t value;
2637 char *ptr;
2638
2639 value = strtoul(optarg, &ptr, 10);
2640 switch (*ptr) {
2641 case 0: case 'M': case 'm':
2642 value <<= 20;
2643 break;
2644 case 'G': case 'g':
2645 value <<= 30;
2646 break;
2647 default:
2648 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002649 exit(1);
2650 }
aurel3200f82b82008-04-27 21:12:55 +00002651
2652 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002653 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002654 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2655 exit(1);
2656 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002657 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002658 fprintf(stderr, "qemu: ram size too large\n");
2659 exit(1);
2660 }
2661 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002662 break;
aurel3200f82b82008-04-27 21:12:55 +00002663 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002664 case QEMU_OPTION_mempath:
2665 mem_path = optarg;
2666 break;
2667#ifdef MAP_POPULATE
2668 case QEMU_OPTION_mem_prealloc:
2669 mem_prealloc = 1;
2670 break;
2671#endif
bellardcd6f1162004-05-13 22:02:20 +00002672 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002673 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002674 break;
bellardcd6f1162004-05-13 22:02:20 +00002675 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002676 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002677 break;
aliguori59030a82009-04-05 18:43:41 +00002678 case QEMU_OPTION_gdb:
2679 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002680 break;
bellardcd6f1162004-05-13 22:02:20 +00002681 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002682 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002683 break;
j_mayer1192dad2007-10-05 13:08:35 +00002684 case QEMU_OPTION_bios:
2685 bios_name = optarg;
2686 break;
aurel321b530a62009-04-05 20:08:59 +00002687 case QEMU_OPTION_singlestep:
2688 singlestep = 1;
2689 break;
bellardcd6f1162004-05-13 22:02:20 +00002690 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00002691 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00002692 break;
bellard3d11d0e2004-12-12 16:56:30 +00002693 case QEMU_OPTION_k:
2694 keyboard_layout = optarg;
2695 break;
bellardee22c2f2004-06-03 12:49:50 +00002696 case QEMU_OPTION_localtime:
2697 rtc_utc = 0;
2698 break;
malc3893c122008-09-28 00:42:05 +00002699 case QEMU_OPTION_vga:
2700 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00002701 break;
bellarde9b137c2004-06-21 16:46:10 +00002702 case QEMU_OPTION_g:
2703 {
2704 const char *p;
2705 int w, h, depth;
2706 p = optarg;
2707 w = strtol(p, (char **)&p, 10);
2708 if (w <= 0) {
2709 graphic_error:
2710 fprintf(stderr, "qemu: invalid resolution or depth\n");
2711 exit(1);
2712 }
2713 if (*p != 'x')
2714 goto graphic_error;
2715 p++;
2716 h = strtol(p, (char **)&p, 10);
2717 if (h <= 0)
2718 goto graphic_error;
2719 if (*p == 'x') {
2720 p++;
2721 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00002722 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00002723 depth != 24 && depth != 32)
2724 goto graphic_error;
2725 } else if (*p == '\0') {
2726 depth = graphic_depth;
2727 } else {
2728 goto graphic_error;
2729 }
ths3b46e622007-09-17 08:09:54 +00002730
bellarde9b137c2004-06-21 16:46:10 +00002731 graphic_width = w;
2732 graphic_height = h;
2733 graphic_depth = depth;
2734 }
2735 break;
ths20d8a3e2007-02-18 17:04:49 +00002736 case QEMU_OPTION_echr:
2737 {
2738 char *r;
2739 term_escape_char = strtol(optarg, &r, 0);
2740 if (r == optarg)
2741 printf("Bad argument to echr\n");
2742 break;
2743 }
bellard82c643f2004-07-14 17:28:13 +00002744 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002745 monitor_parse(optarg, "readline");
2746 default_monitor = 0;
2747 break;
2748 case QEMU_OPTION_qmp:
2749 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002750 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00002751 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002752 case QEMU_OPTION_mon:
Markus Armbruster8212c642010-02-10 19:52:18 +01002753 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002754 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002755 exit(1);
2756 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05002757 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01002758 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002759 case QEMU_OPTION_chardev:
Markus Armbruster8212c642010-02-10 19:52:18 +01002760 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002761 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002762 exit(1);
2763 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02002764 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302765#ifdef CONFIG_LINUX
2766 case QEMU_OPTION_fsdev:
2767 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
2768 if (!opts) {
2769 fprintf(stderr, "parse error: %s\n", optarg);
2770 exit(1);
2771 }
2772 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05302773 case QEMU_OPTION_virtfs: {
2774 char *arg_fsdev = NULL;
2775 char *arg_9p = NULL;
2776 int len = 0;
2777
2778 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
2779 if (!opts) {
2780 fprintf(stderr, "parse error: %s\n", optarg);
2781 exit(1);
2782 }
2783
2784 len = strlen(",id=,path=");
2785 len += strlen(qemu_opt_get(opts, "fstype"));
2786 len += strlen(qemu_opt_get(opts, "mount_tag"));
2787 len += strlen(qemu_opt_get(opts, "path"));
2788 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
2789
2790 if (!arg_fsdev) {
2791 fprintf(stderr, "No memory to parse -fsdev for %s\n",
2792 optarg);
2793 exit(1);
2794 }
2795
2796 sprintf(arg_fsdev, "%s,id=%s,path=%s",
2797 qemu_opt_get(opts, "fstype"),
2798 qemu_opt_get(opts, "mount_tag"),
2799 qemu_opt_get(opts, "path"));
2800
2801 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
2802 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
2803 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
2804
2805 if (!arg_9p) {
2806 fprintf(stderr, "No memory to parse -device for %s\n",
2807 optarg);
2808 exit(1);
2809 }
2810
2811 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
2812 qemu_opt_get(opts, "mount_tag"),
2813 qemu_opt_get(opts, "mount_tag"));
2814
2815 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
2816 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
2817 exit(1);
2818 }
2819
2820 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
2821 fprintf(stderr, "parse error [device]: %s\n", optarg);
2822 exit(1);
2823 }
2824
2825 qemu_free(arg_fsdev);
2826 qemu_free(arg_9p);
2827 break;
2828 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302829#endif
bellard82c643f2004-07-14 17:28:13 +00002830 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002831 add_device_config(DEV_SERIAL, optarg);
2832 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002833 if (strncmp(optarg, "mon:", 4) == 0) {
2834 default_monitor = 0;
2835 }
bellard82c643f2004-07-14 17:28:13 +00002836 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002837 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02002838 if (watchdog) {
2839 fprintf(stderr,
2840 "qemu: only one watchdog option may be given\n");
2841 return 1;
2842 }
2843 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01002844 break;
2845 case QEMU_OPTION_watchdog_action:
2846 if (select_watchdog_action(optarg) == -1) {
2847 fprintf(stderr, "Unknown -watchdog-action parameter\n");
2848 exit(1);
2849 }
2850 break;
aliguori51ecf132009-01-15 20:06:40 +00002851 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002852 add_device_config(DEV_VIRTCON, optarg);
2853 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002854 if (strncmp(optarg, "mon:", 4) == 0) {
2855 default_monitor = 0;
2856 }
aliguori51ecf132009-01-15 20:06:40 +00002857 break;
bellard6508fe52005-01-15 12:02:56 +00002858 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002859 add_device_config(DEV_PARALLEL, optarg);
2860 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01002861 if (strncmp(optarg, "mon:", 4) == 0) {
2862 default_monitor = 0;
2863 }
bellard6508fe52005-01-15 12:02:56 +00002864 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002865 case QEMU_OPTION_debugcon:
2866 add_device_config(DEV_DEBUGCON, optarg);
2867 break;
bellardd63d3072004-10-03 13:29:03 +00002868 case QEMU_OPTION_loadvm:
2869 loadvm = optarg;
2870 break;
2871 case QEMU_OPTION_full_screen:
2872 full_screen = 1;
2873 break;
ths667acca2006-12-11 02:08:05 +00002874#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00002875 case QEMU_OPTION_no_frame:
2876 no_frame = 1;
2877 break;
ths3780e192007-06-21 21:08:02 +00002878 case QEMU_OPTION_alt_grab:
2879 alt_grab = 1;
2880 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05002881 case QEMU_OPTION_ctrl_grab:
2882 ctrl_grab = 1;
2883 break;
ths667acca2006-12-11 02:08:05 +00002884 case QEMU_OPTION_no_quit:
2885 no_quit = 1;
2886 break;
aliguori7d957bd2009-01-15 22:14:11 +00002887 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002888 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00002889 break;
ths667acca2006-12-11 02:08:05 +00002890#endif
bellardf7cce892004-12-08 22:21:25 +00002891 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00002892 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00002893 break;
bellarda09db212005-04-30 16:10:35 +00002894 case QEMU_OPTION_win2k_hack:
2895 win2k_install_hack = 1;
2896 break;
aliguori73822ec2009-01-15 20:11:34 +00002897 case QEMU_OPTION_rtc_td_hack:
2898 rtc_td_hack = 1;
2899 break;
aliguori8a92ea22009-02-27 20:12:36 +00002900 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002901 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00002902 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00002903 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002904 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00002905 break;
aliguori7ba1e612008-11-05 16:04:33 +00002906 case QEMU_OPTION_enable_kvm:
2907 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00002908 break;
bellardbb36d472005-11-05 14:22:28 +00002909 case QEMU_OPTION_usb:
2910 usb_enabled = 1;
2911 break;
bellarda594cfb2005-11-06 16:13:29 +00002912 case QEMU_OPTION_usbdevice:
2913 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002914 add_device_config(DEV_USB, optarg);
2915 break;
2916 case QEMU_OPTION_device:
Markus Armbruster8212c642010-02-10 19:52:18 +01002917 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002918 exit(1);
2919 }
bellarda594cfb2005-11-06 16:13:29 +00002920 break;
bellard6a00d602005-11-21 23:25:50 +00002921 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002922 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00002923 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00002924 fprintf(stderr, "Invalid number of CPUs\n");
2925 exit(1);
2926 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02002927 if (max_cpus < smp_cpus) {
2928 fprintf(stderr, "maxcpus must be equal to or greater than "
2929 "smp\n");
2930 exit(1);
2931 }
2932 if (max_cpus > 255) {
2933 fprintf(stderr, "Unsupported number of maxcpus\n");
2934 exit(1);
2935 }
bellard6a00d602005-11-21 23:25:50 +00002936 break;
bellard24236862006-04-30 21:28:36 +00002937 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002938 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00002939 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00002940 break;
bellard6515b202006-05-03 22:02:44 +00002941 case QEMU_OPTION_no_acpi:
2942 acpi_enabled = 0;
2943 break;
aliguori16b29ae2008-12-17 23:28:44 +00002944 case QEMU_OPTION_no_hpet:
2945 no_hpet = 1;
2946 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002947 case QEMU_OPTION_balloon:
2948 if (balloon_parse(optarg) < 0) {
2949 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
2950 exit(1);
2951 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03002952 break;
bellardd1beab82006-10-02 19:44:22 +00002953 case QEMU_OPTION_no_reboot:
2954 no_reboot = 1;
2955 break;
aurel32b2f76162008-04-11 21:35:52 +00002956 case QEMU_OPTION_no_shutdown:
2957 no_shutdown = 1;
2958 break;
balrog9467cd42007-05-01 01:34:14 +00002959 case QEMU_OPTION_show_cursor:
2960 cursor_hide = 0;
2961 break;
blueswir18fcb1b92008-09-18 18:29:08 +00002962 case QEMU_OPTION_uuid:
2963 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
2964 fprintf(stderr, "Fail to parse UUID string."
2965 " Wrong format.\n");
2966 exit(1);
2967 }
2968 break;
ths9ae02552007-01-05 17:39:04 +00002969 case QEMU_OPTION_option_rom:
2970 if (nb_option_roms >= MAX_OPTION_ROMS) {
2971 fprintf(stderr, "Too many option ROMs\n");
2972 exit(1);
2973 }
2974 option_rom[nb_option_roms] = optarg;
2975 nb_option_roms++;
2976 break;
pbrook8e716212007-01-20 17:12:09 +00002977 case QEMU_OPTION_semihosting:
2978 semihosting_enabled = 1;
2979 break;
thsc35734b2007-03-19 15:17:08 +00002980 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02002981 qemu_name = qemu_strdup(optarg);
2982 {
2983 char *p = strchr(qemu_name, ',');
2984 if (p != NULL) {
2985 *p++ = 0;
2986 if (strncmp(p, "process=", 8)) {
2987 fprintf(stderr, "Unknown subargument %s to -name", p);
2988 exit(1);
2989 }
2990 p += 8;
2991 set_proc_name(p);
2992 }
2993 }
thsc35734b2007-03-19 15:17:08 +00002994 break;
blueswir166508602007-05-01 14:16:52 +00002995 case QEMU_OPTION_prom_env:
2996 if (nb_prom_envs >= MAX_PROM_ENVS) {
2997 fprintf(stderr, "Too many prom variables\n");
2998 exit(1);
2999 }
3000 prom_envs[nb_prom_envs] = optarg;
3001 nb_prom_envs++;
3002 break;
balrog2b8f2d42007-07-27 22:08:46 +00003003 case QEMU_OPTION_old_param:
3004 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003005 break;
thsf3dcfad2007-08-24 01:26:02 +00003006 case QEMU_OPTION_clock:
3007 configure_alarms(optarg);
3008 break;
bellard7e0af5d02007-11-07 16:24:33 +00003009 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003010 configure_rtc_date_offset(optarg, 1);
3011 break;
3012 case QEMU_OPTION_rtc:
Markus Armbruster8212c642010-02-10 19:52:18 +01003013 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003014 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003015 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003016 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003017 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003018 break;
bellard26a5f132008-05-28 12:30:31 +00003019 case QEMU_OPTION_tb_size:
3020 tb_size = strtol(optarg, NULL, 0);
3021 if (tb_size < 0)
3022 tb_size = 0;
3023 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003024 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003025 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003026 break;
aliguori5bb79102008-10-13 03:12:02 +00003027 case QEMU_OPTION_incoming:
3028 incoming = optarg;
3029 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003030 case QEMU_OPTION_nodefaults:
3031 default_serial = 0;
3032 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003033 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003034 default_monitor = 0;
3035 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003036 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003037 default_floppy = 0;
3038 default_cdrom = 0;
3039 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003040 break;
aliguorie37630c2009-04-22 15:19:10 +00003041 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003042 if (!(xen_available())) {
3043 printf("Option %s not supported for this target\n", popt->name);
3044 exit(1);
3045 }
aliguorie37630c2009-04-22 15:19:10 +00003046 xen_domid = atoi(optarg);
3047 break;
3048 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003049 if (!(xen_available())) {
3050 printf("Option %s not supported for this target\n", popt->name);
3051 exit(1);
3052 }
aliguorie37630c2009-04-22 15:19:10 +00003053 xen_mode = XEN_CREATE;
3054 break;
3055 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003056 if (!(xen_available())) {
3057 printf("Option %s not supported for this target\n", popt->name);
3058 exit(1);
3059 }
aliguorie37630c2009-04-22 15:19:10 +00003060 xen_mode = XEN_ATTACH;
3061 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003062 case QEMU_OPTION_readconfig:
3063 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003064 int ret = qemu_read_config_file(optarg);
3065 if (ret < 0) {
3066 fprintf(stderr, "read config %s: %s\n", optarg,
3067 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003068 exit(1);
3069 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003070 break;
3071 }
3072 case QEMU_OPTION_writeconfig:
3073 {
3074 FILE *fp;
3075 if (strcmp(optarg, "-") == 0) {
3076 fp = stdout;
3077 } else {
3078 fp = fopen(optarg, "w");
3079 if (fp == NULL) {
3080 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3081 exit(1);
3082 }
3083 }
3084 qemu_config_write(fp);
3085 fclose(fp);
3086 break;
3087 }
Jes Sorensen59a52642010-06-10 11:42:25 +02003088 default:
3089 os_parse_cmd_args(popt->index, optarg);
bellardcd6f1162004-05-13 22:02:20 +00003090 }
bellard0824d6f2003-06-24 13:42:40 +00003091 }
3092 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003093 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003094
Paul Brook5cea8592009-05-30 00:52:44 +01003095 /* If no data_dir is specified then try to find it relative to the
3096 executable path. */
3097 if (!data_dir) {
Jes Sorensen61705402010-06-10 11:42:23 +02003098 data_dir = os_find_datadir(argv[0]);
Paul Brook5cea8592009-05-30 00:52:44 +01003099 }
3100 /* If all else fails use the install patch specified when building. */
3101 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003102 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003103 }
3104
Jes Sorensen6be68d72009-07-23 17:03:42 +02003105 /*
3106 * Default to max_cpus = smp_cpus, in case the user doesn't
3107 * specify a max_cpus value.
3108 */
3109 if (!max_cpus)
3110 max_cpus = smp_cpus;
3111
balrog3d878ca2008-10-28 10:59:59 +00003112 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003113 if (smp_cpus > machine->max_cpus) {
3114 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3115 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3116 machine->max_cpus);
3117 exit(1);
3118 }
3119
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003120 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01003121 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003122
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003123 if (machine->no_serial) {
3124 default_serial = 0;
3125 }
3126 if (machine->no_parallel) {
3127 default_parallel = 0;
3128 }
3129 if (!machine->use_virtcon) {
3130 default_virtcon = 0;
3131 }
3132 if (machine->no_vga) {
3133 default_vga = 0;
3134 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003135 if (machine->no_floppy) {
3136 default_floppy = 0;
3137 }
3138 if (machine->no_cdrom) {
3139 default_cdrom = 0;
3140 }
3141 if (machine->no_sdcard) {
3142 default_sdcard = 0;
3143 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003144
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003145 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003146 if (default_parallel)
3147 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003148 if (default_serial && default_monitor) {
3149 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003150 } else if (default_virtcon && default_monitor) {
3151 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003152 } else {
3153 if (default_serial)
3154 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003155 if (default_virtcon)
3156 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003157 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003158 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003159 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003160 } else {
3161 if (default_serial)
3162 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003163 if (default_parallel)
3164 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003165 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003166 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003167 if (default_virtcon)
3168 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003169 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003170 if (default_vga)
3171 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003172
TeLeMana5829fd2010-04-15 12:37:55 +08003173 socket_init();
3174
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003175 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3176 exit(1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303177#ifdef CONFIG_LINUX
3178 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3179 exit(1);
3180 }
3181#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003182
Jes Sorenseneb505be2010-06-10 11:42:28 +02003183 os_daemonize();
ths71e3ceb2006-12-22 02:11:31 +00003184
thsaa26bb22007-03-25 21:33:06 +00003185 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +02003186 os_pidfile_error();
ths93815bc2007-03-19 15:58:31 +00003187 exit(1);
3188 }
3189
Paolo Bonzini98c85732010-04-19 18:59:30 +00003190 if (kvm_allowed) {
3191 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003192 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00003193 if (!kvm_available()) {
3194 printf("KVM not supported for this target\n");
3195 } else {
3196 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3197 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003198 exit(1);
3199 }
3200 }
3201
aliguori3fcf7b62009-04-24 18:03:25 +00003202 if (qemu_init_main_loop()) {
3203 fprintf(stderr, "qemu_init_main_loop failed\n");
3204 exit(1);
3205 }
bellarda20dd502003-09-30 21:07:02 +00003206 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003207
thsf8d39c02008-07-03 10:01:15 +00003208 if (!linux_boot && *kernel_cmdline != '\0') {
3209 fprintf(stderr, "-append only allowed with -kernel option\n");
3210 exit(1);
3211 }
3212
3213 if (!linux_boot && initrd_filename != NULL) {
3214 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3215 exit(1);
3216 }
3217
Filip Navarabf65f532009-07-27 10:02:04 -05003218#ifndef _WIN32
3219 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00003220 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05003221#endif
ths3b46e622007-09-17 08:09:54 +00003222
aliguori7183b4b2008-11-05 20:40:18 +00003223 if (init_timer_alarm() < 0) {
3224 fprintf(stderr, "could not initialize alarm timer\n");
3225 exit(1);
3226 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003227 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003228
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003229 if (net_init_clients() < 0) {
3230 exit(1);
bellard702c6512004-04-02 21:21:32 +00003231 }
bellardf1510b22003-06-25 00:07:40 +00003232
balrogdc72ac12008-11-09 00:04:26 +00003233 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003234 if (foreach_device_config(DEV_BT, bt_parse))
3235 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003236
bellard0824d6f2003-06-24 13:42:40 +00003237 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00003238 if (ram_size == 0)
3239 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00003240
bellard26a5f132008-05-28 12:30:31 +00003241 /* init the dynamic translator */
3242 cpu_exec_init_all(tb_size * 1024 * 1024);
3243
Markus Armbrustereb852012009-10-27 18:41:44 +01003244 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003245
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003246 blk_mig_init();
3247
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003248 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003249 /* we always create the cdrom drive, even if no disk is there */
3250 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003251 }
thse4bcb142007-12-02 04:51:10 +00003252
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003253 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003254 /* we always create at least one floppy */
3255 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003256 }
bellardc4b1fcc2004-03-14 21:44:30 +00003257
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003258 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003259 /* we always create one sd slot, even if no card is in it */
3260 drive_add(NULL, SD_ALIAS);
3261 }
balrog9d413d12007-12-04 00:10:34 +00003262
ths96d30e42007-01-07 20:42:14 +00003263 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003264 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02003265 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3266 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003267 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003268
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003269 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3270 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003271
aliguori268a3622009-04-21 22:30:27 +00003272 if (nb_numa_nodes > 0) {
3273 int i;
3274
3275 if (nb_numa_nodes > smp_cpus) {
3276 nb_numa_nodes = smp_cpus;
3277 }
3278
3279 /* If no memory size if given for any node, assume the default case
3280 * and distribute the available memory equally across all nodes
3281 */
3282 for (i = 0; i < nb_numa_nodes; i++) {
3283 if (node_mem[i] != 0)
3284 break;
3285 }
3286 if (i == nb_numa_nodes) {
3287 uint64_t usedmem = 0;
3288
3289 /* On Linux, the each node's border has to be 8MB aligned,
3290 * the final node gets the rest.
3291 */
3292 for (i = 0; i < nb_numa_nodes - 1; i++) {
3293 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3294 usedmem += node_mem[i];
3295 }
3296 node_mem[i] = ram_size - usedmem;
3297 }
3298
3299 for (i = 0; i < nb_numa_nodes; i++) {
3300 if (node_cpumask[i] != 0)
3301 break;
3302 }
3303 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3304 * must cope with this anyway, because there are BIOSes out there in
3305 * real machines which also use this scheme.
3306 */
3307 if (i == nb_numa_nodes) {
3308 for (i = 0; i < smp_cpus; i++) {
3309 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3310 }
3311 }
3312 }
3313
Jan Kiszka157b9312010-04-06 16:55:53 +02003314 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3315 exit(1);
3316 }
3317
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003318 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3319 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003320 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3321 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003322 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3323 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003324 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3325 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003326
Paul Brookaae94602009-05-14 22:35:06 +01003327 module_call_init(MODULE_INIT_DEVICE);
3328
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003329 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3330 exit(0);
3331
Markus Armbruster09aaa162009-08-21 10:31:34 +02003332 if (watchdog) {
3333 i = select_watchdog(watchdog);
3334 if (i > 0)
3335 exit (i == 1 ? 1 : 0);
3336 }
3337
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003338 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003339 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003340 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003341 qemu_add_globals();
3342
Paul Brookfbe1b592009-05-13 17:56:25 +01003343 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003344 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3345
Jan Kiszkaea375f92010-03-01 19:10:30 +01003346 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003347
Juan Quintela67b3b712009-08-28 19:25:15 +02003348 /* must be after terminal init, SDL library changes signal handlers */
Jes Sorensen8d963e62010-06-10 11:42:22 +02003349 os_setup_signal_handling();
Juan Quintela67b3b712009-08-28 19:25:15 +02003350
Blue Swirl87d0a282010-03-27 18:24:45 +00003351 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003352
aliguori6f338c32009-02-11 15:21:54 +00003353 current_machine = machine;
3354
aliguori3023f332009-01-16 19:04:14 +00003355 /* init USB devices */
3356 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003357 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3358 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003359 }
3360
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003361 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003362 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003363 exit(1);
3364
Markus Armbruster668680f2010-02-11 14:44:58 +01003365 net_check_clients();
3366
aliguori3023f332009-01-16 19:04:14 +00003367 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003368 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003369
3370 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003371#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003372 display_type = DT_SDL;
3373#else
3374 display_type = DT_VNC;
3375 vnc_display = "localhost:0,to=99";
3376 show_vnc_port = 1;
3377#endif
3378 }
3379
3380
3381 switch (display_type) {
3382 case DT_NOGRAPHIC:
3383 break;
3384#if defined(CONFIG_CURSES)
3385 case DT_CURSES:
3386 curses_display_init(ds, full_screen);
3387 break;
3388#endif
bellard5b0753e2005-03-01 21:37:28 +00003389#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003390 case DT_SDL:
3391 sdl_display_init(ds, full_screen, no_frame);
3392 break;
bellard5b0753e2005-03-01 21:37:28 +00003393#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003394 case DT_SDL:
3395 cocoa_display_init(ds, full_screen);
3396 break;
bellard313aa562003-08-10 21:52:11 +00003397#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003398 case DT_VNC:
3399 vnc_display_init(ds);
3400 if (vnc_display_open(ds, vnc_display) < 0)
3401 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003402
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003403 if (show_vnc_port) {
3404 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003405 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003406 break;
3407 default:
3408 break;
bellard313aa562003-08-10 21:52:11 +00003409 }
aliguori7d957bd2009-01-15 22:14:11 +00003410 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00003411
aliguori3023f332009-01-16 19:04:14 +00003412 dcl = ds->listeners;
3413 while (dcl != NULL) {
3414 if (dcl->dpy_refresh != NULL) {
3415 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3416 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003417 break;
ths20d8a3e2007-02-18 17:04:49 +00003418 }
aliguori3023f332009-01-16 19:04:14 +00003419 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003420 }
aliguori3023f332009-01-16 19:04:14 +00003421
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003422 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00003423 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3424 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3425 }
3426
Paolo Bonzinib473df62010-02-11 00:29:55 +01003427 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003428
aliguori59030a82009-04-05 18:43:41 +00003429 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3430 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3431 gdbstub_dev);
3432 exit(1);
balrog45669e02007-07-02 13:20:17 +00003433 }
balrog45669e02007-07-02 13:20:17 +00003434
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003435 qdev_machine_creation_done();
3436
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003437 if (rom_load_all() != 0) {
3438 fprintf(stderr, "rom loading failed\n");
3439 exit(1);
3440 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003441
Juan Quintela504c2942009-11-12 00:39:13 +01003442 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003443 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003444 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003445 autostart = 0;
3446 }
3447 }
bellardd63d3072004-10-03 13:29:03 +00003448
Glauber Costa2bb8c102009-07-24 16:20:23 -04003449 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00003450 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03003451 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003452 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003453 }
thsffd843b2006-12-21 19:46:43 +00003454
Jes Sorenseneb505be2010-06-10 11:42:28 +02003455 os_setup_post();
ths71e3ceb2006-12-22 02:11:31 +00003456
bellard8a7ddc32004-03-31 19:00:16 +00003457 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003458 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003459 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003460
bellard0824d6f2003-06-24 13:42:40 +00003461 return 0;
3462}