blob: 3d7ce522b184ab5e544046036702aaf37ffcd2b0 [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>
aliguori08585322009-02-27 22:09:45 +000037#include <pwd.h>
bellard67b915a2004-03-31 23:37:16 +000038#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000039#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000040#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000041#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000042#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000043#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000044#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000045#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000046#include <net/if.h>
blueswir124646c72008-11-07 16:55:48 +000047#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000048#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000049#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000050#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020051#ifdef CONFIG_BSD
bellard7d3505c2004-05-12 19:32:15 +000052#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010053#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
bellard7d3505c2004-05-12 19:32:15 +000054#include <libutil.h>
Juergen Lock92c0e652010-03-25 22:07:12 +010055#include <sys/sysctl.h>
blueswir124646c72008-11-07 16:55:48 +000056#else
57#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000058#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010059#else
blueswir1223f0d72008-09-30 18:12:18 +000060#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000061#include <pty.h>
62#include <malloc.h>
Andi Kleen18894652009-07-02 09:34:17 +020063#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000064
bellarde57a8c02005-11-10 23:58:52 +000065#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000066#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000067#endif
68#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000069#include <sys/stat.h>
70#include <sys/ethernet.h>
71#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000072#include <netinet/arp.h>
73#include <netinet/in.h>
74#include <netinet/in_systm.h>
75#include <netinet/ip.h>
76#include <netinet/ip_icmp.h> // must come after ip.h
77#include <netinet/udp.h>
78#include <netinet/tcp.h>
79#include <net/if.h>
80#include <syslog.h>
81#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000082/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
83 discussion about Solaris header problems */
84extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000085#endif
bellard7d3505c2004-05-12 19:32:15 +000086#endif
bellardec530c82006-04-25 22:36:06 +000087#endif
bellard67b915a2004-03-31 23:37:16 +000088
blueswir19892fbf2008-08-24 10:34:20 +000089#if defined(__OpenBSD__)
90#include <util.h>
91#endif
92
ths8a16d272008-07-19 09:56:24 +000093#if defined(CONFIG_VDE)
94#include <libvdeplug.h>
95#endif
96
bellard67b915a2004-03-31 23:37:16 +000097#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000098#include <windows.h>
bellard67b915a2004-03-31 23:37:16 +000099#endif
100
bellard73332e52004-04-04 20:22:28 +0000101#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200102#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200103#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000104int qemu_main(int argc, char **argv, char **envp);
105int main(int argc, char **argv)
106{
Stefan Weil59a36a22009-06-18 20:11:03 +0200107 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000108}
109#undef main
110#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000111#endif
bellard73332e52004-04-04 20:22:28 +0000112#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000113
bellard5b0753e2005-03-01 21:37:28 +0000114#ifdef CONFIG_COCOA
115#undef main
116#define main qemu_main
117#endif /* CONFIG_COCOA */
118
blueswir1511d2b12009-03-07 15:32:56 +0000119#include "hw/hw.h"
120#include "hw/boards.h"
121#include "hw/usb.h"
122#include "hw/pcmcia.h"
123#include "hw/pc.h"
blueswir1511d2b12009-03-07 15:32:56 +0000124#include "hw/isa.h"
125#include "hw/baum.h"
126#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100127#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000128#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000129#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200130#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200131#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000132#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000133#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000134#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000135#include "monitor.h"
136#include "console.h"
137#include "sysemu.h"
138#include "gdbstub.h"
139#include "qemu-timer.h"
140#include "qemu-char.h"
141#include "cache-utils.h"
142#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200143#include "block_int.h"
144#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000145#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000146#include "audio/audio.h"
147#include "migration.h"
148#include "kvm.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200149#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200150#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200151#include "qemu-objects.h"
Gautham R Shenoy74db9202010-04-29 17:44:43 +0530152#ifdef CONFIG_LINUX
153#include "fsdev/qemu-fsdev.h"
154#endif
blueswir1511d2b12009-03-07 15:32:56 +0000155
bellard0824d6f2003-06-24 13:42:40 +0000156#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000157
blueswir1511d2b12009-03-07 15:32:56 +0000158#include "qemu_socket.h"
159
Jan Kiszkad918f232009-06-24 14:42:30 +0200160#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000161
Blue Swirl72cf2d42009-09-12 07:36:22 +0000162#include "qemu-queue.h"
Blue Swirl296af7c2010-03-29 19:23:50 +0000163#include "cpus.h"
Blue Swirlad960902010-03-29 19:23:52 +0000164#include "arch_init.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +0000165
blueswir19dc63a12008-10-04 07:25:46 +0000166//#define DEBUG_NET
167//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000168
bellard1bfe8562004-07-08 21:17:50 +0000169#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000170
Amit Shah98b19252010-01-20 00:36:52 +0530171#define MAX_VIRTIO_CONSOLES 1
172
Paul Brook5cea8592009-05-30 00:52:44 +0100173static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000174const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000175/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000176 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000177struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
178struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000179enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500180DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000181const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500182ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300183const char *mem_path = NULL;
184#ifdef MAP_POPULATE
185int mem_prealloc = 0; /* force preallocation of physical target memory */
186#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000187int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000188NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000189int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200190int autostart;
balrogf6503052008-02-17 11:42:19 +0000191static int rtc_utc = 1;
192static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200193QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100194int vga_interface_type = VGA_NONE;
blueswir1dbed7e42008-10-01 19:38:09 +0000195static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000196#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000197static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000198#endif
ths667acca2006-12-11 02:08:05 +0000199int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000200CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000201CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000202CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000203int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000204int rtc_td_hack = 0;
bellardbb36d472005-11-05 14:22:28 +0000205int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000206int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000207int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200208int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200209int smp_cores = 1;
210int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000211const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000212int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000213int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000214int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000215int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000216int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000217int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000218int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200219uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000220#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000221int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000222#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200223const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000224const char *option_rom[MAX_OPTION_ROMS];
225int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000226int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000227int old_param = 0;
thsc35734b2007-03-19 15:17:08 +0000228const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000229int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500230int ctrl_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000231unsigned int nb_prom_envs = 0;
232const char *prom_envs[MAX_PROM_ENVS];
Jan Kiszka95387492009-07-02 00:19:02 +0200233int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000234
aliguori268a3622009-04-21 22:30:27 +0000235int nb_numa_nodes;
236uint64_t node_mem[MAX_NODES];
237uint64_t node_cpumask[MAX_NODES];
238
blueswir19043b622009-01-21 19:28:13 +0000239static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000240
blueswir18fcb1b92008-09-18 18:29:08 +0000241uint8_t qemu_uuid[16];
242
Jan Kiszka76e30d02009-07-02 00:19:02 +0200243static QEMUBootSetHandler *boot_set_handler;
244static void *boot_set_opaque;
245
Blue Swirld745bef2010-03-29 19:23:49 +0000246int kvm_allowed = 0;
247uint32_t xen_domid;
248enum xen_mode xen_mode = XEN_EMULATE;
249
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100250static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100251static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100252static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100253static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100254static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100255static int default_floppy = 1;
256static int default_cdrom = 1;
257static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100258
259static struct {
260 const char *driver;
261 int *flag;
262} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100263 { .driver = "isa-serial", .flag = &default_serial },
264 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100265 { .driver = "isa-fdc", .flag = &default_floppy },
266 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530267 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
268 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
269 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100270 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100271 { .driver = "cirrus-vga", .flag = &default_vga },
272 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100273};
274
275static int default_driver_check(QemuOpts *opts, void *opaque)
276{
277 const char *driver = qemu_opt_get(opts, "driver");
278 int i;
279
280 if (!driver)
281 return 0;
282 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
283 if (strcmp(default_list[i].driver, driver) != 0)
284 continue;
285 *(default_list[i].flag) = 0;
286 }
287 return 0;
288}
289
bellard0824d6f2003-06-24 13:42:40 +0000290/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000291
Andi Kleen18894652009-07-02 09:34:17 +0200292static void set_proc_name(const char *s)
293{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700294#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200295 char name[16];
296 if (!s)
297 return;
298 name[sizeof(name) - 1] = 0;
299 strncpy(name, s, sizeof(name));
300 /* Could rewrite argv[0] too, but that's a bit more complicated.
301 This simple way is enough for `top'. */
302 prctl(PR_SET_NAME, name);
303#endif
304}
aliguoridf751fa2008-12-04 20:19:35 +0000305
bellard8a7ddc32004-03-31 19:00:16 +0000306/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100307/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000308
bellard87858c82003-06-27 12:01:39 +0000309/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000310uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000311{
312 union {
313 uint64_t ll;
314 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200315#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000316 uint32_t high, low;
317#else
318 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000319#endif
bellard87858c82003-06-27 12:01:39 +0000320 } l;
321 } u, res;
322 uint64_t rl, rh;
323
324 u.ll = a;
325 rl = (uint64_t)u.l.low * (uint64_t)b;
326 rh = (uint64_t)u.l.high * (uint64_t)b;
327 rh += (rl >> 32);
328 res.l.high = rh / c;
329 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
330 return res.ll;
331}
332
bellardc4b1fcc2004-03-14 21:44:30 +0000333/***********************************************************/
balrogf6503052008-02-17 11:42:19 +0000334/* host time/date access */
335void qemu_get_timedate(struct tm *tm, int offset)
336{
337 time_t ti;
338 struct tm *ret;
339
340 time(&ti);
341 ti += offset;
342 if (rtc_date_offset == -1) {
343 if (rtc_utc)
344 ret = gmtime(&ti);
345 else
346 ret = localtime(&ti);
347 } else {
348 ti -= rtc_date_offset;
349 ret = gmtime(&ti);
350 }
351
352 memcpy(tm, ret, sizeof(struct tm));
353}
354
355int qemu_timedate_diff(struct tm *tm)
356{
357 time_t seconds;
358
359 if (rtc_date_offset == -1)
360 if (rtc_utc)
361 seconds = mktimegm(tm);
362 else
363 seconds = mktime(tm);
364 else
365 seconds = mktimegm(tm) + rtc_date_offset;
366
367 return seconds - time(NULL);
368}
369
Luiz Capitulino80cd3472010-02-25 12:11:44 -0300370void rtc_change_mon_event(struct tm *tm)
371{
372 QObject *data;
373
374 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
375 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
376 qobject_decref(data);
377}
378
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200379static void configure_rtc_date_offset(const char *startdate, int legacy)
380{
381 time_t rtc_start_date;
382 struct tm tm;
383
384 if (!strcmp(startdate, "now") && legacy) {
385 rtc_date_offset = -1;
386 } else {
387 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
388 &tm.tm_year,
389 &tm.tm_mon,
390 &tm.tm_mday,
391 &tm.tm_hour,
392 &tm.tm_min,
393 &tm.tm_sec) == 6) {
394 /* OK */
395 } else if (sscanf(startdate, "%d-%d-%d",
396 &tm.tm_year,
397 &tm.tm_mon,
398 &tm.tm_mday) == 3) {
399 tm.tm_hour = 0;
400 tm.tm_min = 0;
401 tm.tm_sec = 0;
402 } else {
403 goto date_fail;
404 }
405 tm.tm_year -= 1900;
406 tm.tm_mon--;
407 rtc_start_date = mktimegm(&tm);
408 if (rtc_start_date == -1) {
409 date_fail:
410 fprintf(stderr, "Invalid date format. Valid formats are:\n"
411 "'2006-06-17T16:01:21' or '2006-06-17'\n");
412 exit(1);
413 }
414 rtc_date_offset = time(NULL) - rtc_start_date;
415 }
416}
417
418static void configure_rtc(QemuOpts *opts)
419{
420 const char *value;
421
422 value = qemu_opt_get(opts, "base");
423 if (value) {
424 if (!strcmp(value, "utc")) {
425 rtc_utc = 1;
426 } else if (!strcmp(value, "localtime")) {
427 rtc_utc = 0;
428 } else {
429 configure_rtc_date_offset(value, 0);
430 }
431 }
Jan Kiszka68752042009-09-15 13:36:04 +0200432 value = qemu_opt_get(opts, "clock");
433 if (value) {
434 if (!strcmp(value, "host")) {
435 rtc_clock = host_clock;
436 } else if (!strcmp(value, "vm")) {
437 rtc_clock = vm_clock;
438 } else {
439 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
440 exit(1);
441 }
442 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200443 value = qemu_opt_get(opts, "driftfix");
444 if (value) {
Blue Swirl7e4c0332010-03-27 21:33:46 +0000445 if (!strcmp(value, "slew")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200446 rtc_td_hack = 1;
Blue Swirl7e4c0332010-03-27 21:33:46 +0000447 } else if (!strcmp(value, "none")) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200448 rtc_td_hack = 0;
449 } else {
450 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
451 exit(1);
452 }
453 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +0200454}
455
balrog1ae26a12008-09-28 23:19:47 +0000456/***********************************************************/
457/* Bluetooth support */
458static int nb_hcis;
459static int cur_hci;
460static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +0000461
balrog1ae26a12008-09-28 23:19:47 +0000462static struct bt_vlan_s {
463 struct bt_scatternet_s net;
464 int id;
465 struct bt_vlan_s *next;
466} *first_bt_vlan;
467
468/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +0000469static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +0000470{
471 struct bt_vlan_s **pvlan, *vlan;
472 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
473 if (vlan->id == id)
474 return &vlan->net;
475 }
476 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
477 vlan->id = id;
478 pvlan = &first_bt_vlan;
479 while (*pvlan != NULL)
480 pvlan = &(*pvlan)->next;
481 *pvlan = vlan;
482 return &vlan->net;
483}
484
485static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
486{
487}
488
489static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
490{
491 return -ENOTSUP;
492}
493
494static struct HCIInfo null_hci = {
495 .cmd_send = null_hci_send,
496 .sco_send = null_hci_send,
497 .acl_send = null_hci_send,
498 .bdaddr_set = null_hci_addr_set,
499};
500
501struct HCIInfo *qemu_next_hci(void)
502{
503 if (cur_hci == nb_hcis)
504 return &null_hci;
505
506 return hci_table[cur_hci++];
507}
508
balrogdc72ac12008-11-09 00:04:26 +0000509static struct HCIInfo *hci_init(const char *str)
510{
511 char *endp;
512 struct bt_scatternet_s *vlan = 0;
513
514 if (!strcmp(str, "null"))
515 /* null */
516 return &null_hci;
517 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
518 /* host[:hciN] */
519 return bt_host_hci(str[4] ? str + 5 : "hci0");
520 else if (!strncmp(str, "hci", 3)) {
521 /* hci[,vlan=n] */
522 if (str[3]) {
523 if (!strncmp(str + 3, ",vlan=", 6)) {
524 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
525 if (*endp)
526 vlan = 0;
527 }
528 } else
529 vlan = qemu_find_bt_vlan(0);
530 if (vlan)
531 return bt_new_hci(vlan);
532 }
533
534 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
535
536 return 0;
537}
538
539static int bt_hci_parse(const char *str)
540{
541 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -0500542 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +0000543
544 if (nb_hcis >= MAX_NICS) {
545 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
546 return -1;
547 }
548
549 hci = hci_init(str);
550 if (!hci)
551 return -1;
552
553 bdaddr.b[0] = 0x52;
554 bdaddr.b[1] = 0x54;
555 bdaddr.b[2] = 0x00;
556 bdaddr.b[3] = 0x12;
557 bdaddr.b[4] = 0x34;
558 bdaddr.b[5] = 0x56 + nb_hcis;
559 hci->bdaddr_set(hci, bdaddr.b);
560
561 hci_table[nb_hcis++] = hci;
562
563 return 0;
564}
565
566static void bt_vhci_add(int vlan_id)
567{
568 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
569
570 if (!vlan->slave)
571 fprintf(stderr, "qemu: warning: adding a VHCI to "
572 "an empty scatternet %i\n", vlan_id);
573
574 bt_vhci_init(bt_new_hci(vlan));
575}
576
577static struct bt_device_s *bt_device_add(const char *opt)
578{
579 struct bt_scatternet_s *vlan;
580 int vlan_id = 0;
581 char *endp = strstr(opt, ",vlan=");
582 int len = (endp ? endp - opt : strlen(opt)) + 1;
583 char devname[10];
584
585 pstrcpy(devname, MIN(sizeof(devname), len), opt);
586
587 if (endp) {
588 vlan_id = strtol(endp + 6, &endp, 0);
589 if (*endp) {
590 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
591 return 0;
592 }
593 }
594
595 vlan = qemu_find_bt_vlan(vlan_id);
596
597 if (!vlan->slave)
598 fprintf(stderr, "qemu: warning: adding a slave device to "
599 "an empty scatternet %i\n", vlan_id);
600
601 if (!strcmp(devname, "keyboard"))
602 return bt_keyboard_init(vlan);
603
604 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
605 return 0;
606}
607
608static int bt_parse(const char *opt)
609{
610 const char *endp, *p;
611 int vlan;
612
613 if (strstart(opt, "hci", &endp)) {
614 if (!*endp || *endp == ',') {
615 if (*endp)
616 if (!strstart(endp, ",vlan=", 0))
617 opt = endp + 1;
618
619 return bt_hci_parse(opt);
620 }
621 } else if (strstart(opt, "vhci", &endp)) {
622 if (!*endp || *endp == ',') {
623 if (*endp) {
624 if (strstart(endp, ",vlan=", &p)) {
625 vlan = strtol(p, (char **) &endp, 0);
626 if (*endp) {
627 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
628 return 1;
629 }
630 } else {
631 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
632 return 1;
633 }
634 } else
635 vlan = 0;
636
637 bt_vhci_add(vlan);
638 return 0;
639 }
640 } else if (strstart(opt, "device:", &endp))
641 return !bt_device_add(endp);
642
643 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
644 return 1;
645}
646
balrog1ae26a12008-09-28 23:19:47 +0000647/***********************************************************/
648/* QEMU Block devices */
649
balrog609497a2008-01-14 02:56:53 +0000650#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +0000651#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +0000652#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +0000653#define PFLASH_ALIAS "if=pflash"
654#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +0000655#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +0000656
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200657QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +0000658{
659 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200660 char optstr[1024];
661 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +0000662
thse4bcb142007-12-02 04:51:10 +0000663 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200664 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +0000665 va_end(ap);
666
Markus Armbruster8212c642010-02-10 19:52:18 +0100667 opts = qemu_opts_parse(&qemu_drive_opts, optstr, 0);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200668 if (!opts) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200669 return NULL;
670 }
671 if (file)
672 qemu_opt_set(opts, "file", file);
673 return opts;
aliguorib01b1112009-02-11 15:20:20 +0000674}
675
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200676DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +0000677{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200678 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000679
680 /* seek interface, bus and unit */
681
Blue Swirl72cf2d42009-09-12 07:36:22 +0000682 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200683 if (dinfo->type == type &&
684 dinfo->bus == bus &&
685 dinfo->unit == unit)
686 return dinfo;
687 }
thse4bcb142007-12-02 04:51:10 +0000688
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200689 return NULL;
thse4bcb142007-12-02 04:51:10 +0000690}
691
Gerd Hoffmann2e810b32009-07-31 12:25:38 +0200692DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200693{
694 DriveInfo *dinfo;
695
Blue Swirl72cf2d42009-09-12 07:36:22 +0000696 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +0200697 if (strcmp(id, dinfo->id))
698 continue;
699 return dinfo;
700 }
701 return NULL;
702}
703
thsf60d39b2007-12-17 03:55:57 +0000704int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +0000705{
706 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200707 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +0000708
709 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +0000710 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200711 if(dinfo->type == type &&
712 dinfo->bus > max_bus)
713 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +0000714 }
715 return max_bus;
716}
717
aliguorifa879c62009-01-07 17:32:33 +0000718const char *drive_get_serial(BlockDriverState *bdrv)
719{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200720 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +0000721
Blue Swirl72cf2d42009-09-12 07:36:22 +0000722 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200723 if (dinfo->bdrv == bdrv)
724 return dinfo->serial;
725 }
aliguorifa879c62009-01-07 17:32:33 +0000726
727 return "\0";
728}
729
Kevin Wolff7850092009-11-27 13:25:36 +0100730BlockInterfaceErrorAction drive_get_on_error(
731 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +0000732{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200733 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +0000734
Blue Swirl72cf2d42009-09-12 07:36:22 +0000735 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200736 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100737 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200738 }
aliguori428c5702009-01-21 18:59:04 +0000739
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100740 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +0000741}
742
aurel32a1620fa2008-04-29 05:58:01 +0000743static void bdrv_format_print(void *opaque, const char *name)
744{
745 fprintf(stderr, " %s", name);
746}
747
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200748void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +0000749{
Gerd Hoffmann56a14932009-09-25 21:42:46 +0200750 qemu_opts_del(dinfo->opts);
751 bdrv_delete(dinfo->bdrv);
752 QTAILQ_REMOVE(&drives, dinfo, next);
753 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +0000754}
755
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100756static int parse_block_error_action(const char *buf, int is_read)
757{
758 if (!strcmp(buf, "ignore")) {
759 return BLOCK_ERR_IGNORE;
760 } else if (!is_read && !strcmp(buf, "enospc")) {
761 return BLOCK_ERR_STOP_ENOSPC;
762 } else if (!strcmp(buf, "stop")) {
763 return BLOCK_ERR_STOP_ANY;
764 } else if (!strcmp(buf, "report")) {
765 return BLOCK_ERR_REPORT;
766 } else {
767 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
768 buf, is_read ? "read" : "write");
769 return -1;
770 }
771}
772
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200773DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200774 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +0000775{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200776 const char *buf;
777 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +0000778 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200779 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +0000780 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +0000781 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +0000782 enum { MEDIA_DISK, MEDIA_CDROM } media;
783 int bus_id, unit_id;
784 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +0000785 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +0000786 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +0000787 int max_devs;
788 int index;
Naphtali Sprei59f26892009-10-26 16:25:16 +0200789 int ro = 0;
Christoph Hellwig763b6082010-04-06 19:12:04 +0200790 int bdrv_flags = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100791 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200792 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200793 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200794 int snapshot = 0;
795
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200796 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +0000797
thse4bcb142007-12-02 04:51:10 +0000798 translation = BIOS_ATA_TRANSLATION_AUTO;
thse4bcb142007-12-02 04:51:10 +0000799
Gerd Hoffmann4d007812009-08-31 14:23:57 +0200800 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +0000801 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000802 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000803 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +0000804 } else {
thsf60d39b2007-12-17 03:55:57 +0000805 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000806 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +0000807 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +0000808 }
809 media = MEDIA_DISK;
810
811 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200812 bus_id = qemu_opt_get_number(opts, "bus", 0);
813 unit_id = qemu_opt_get_number(opts, "unit", -1);
814 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +0000815
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200816 cyls = qemu_opt_get_number(opts, "cyls", 0);
817 heads = qemu_opt_get_number(opts, "heads", 0);
818 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +0000819
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200820 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +0200821 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +0000822
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200823 file = qemu_opt_get(opts, "file");
824 serial = qemu_opt_get(opts, "serial");
825
826 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +0000827 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +0000828 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +0000829 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +0000830 max_devs = MAX_IDE_DEVS;
831 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +0000832 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +0000833 max_devs = MAX_SCSI_DEVS;
834 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +0000835 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +0000836 max_devs = 0;
837 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +0000838 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +0000839 max_devs = 0;
840 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +0000841 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +0000842 max_devs = 0;
843 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +0000844 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +0000845 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +0000846 } else if (!strcmp(buf, "virtio")) {
847 type = IF_VIRTIO;
848 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000849 } else if (!strcmp(buf, "xen")) {
850 type = IF_XEN;
851 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +0200852 } else if (!strcmp(buf, "none")) {
853 type = IF_NONE;
854 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +0000855 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200856 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200857 return NULL;
thse4bcb142007-12-02 04:51:10 +0000858 }
859 }
860
thse4bcb142007-12-02 04:51:10 +0000861 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +0000862 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200863 fprintf(stderr, "qemu: '%s' invalid physical cyls 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 (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200867 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200868 return NULL;
thse4bcb142007-12-02 04:51:10 +0000869 }
Blue Swirl5afe3f02009-10-17 09:08:47 +0000870 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200871 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200872 return NULL;
thse4bcb142007-12-02 04:51:10 +0000873 }
874 }
875
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200876 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000877 if (!cyls) {
878 fprintf(stderr,
879 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200880 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200881 return NULL;
thse4bcb142007-12-02 04:51:10 +0000882 }
883 if (!strcmp(buf, "none"))
884 translation = BIOS_ATA_TRANSLATION_NONE;
885 else if (!strcmp(buf, "lba"))
886 translation = BIOS_ATA_TRANSLATION_LBA;
887 else if (!strcmp(buf, "auto"))
888 translation = BIOS_ATA_TRANSLATION_AUTO;
889 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200890 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200891 return NULL;
thse4bcb142007-12-02 04:51:10 +0000892 }
893 }
894
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200895 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +0000896 if (!strcmp(buf, "disk")) {
897 media = MEDIA_DISK;
898 } else if (!strcmp(buf, "cdrom")) {
899 if (cyls || secs || heads) {
900 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200901 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200902 return NULL;
thse4bcb142007-12-02 04:51:10 +0000903 }
904 media = MEDIA_CDROM;
905 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200906 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200907 return NULL;
thse4bcb142007-12-02 04:51:10 +0000908 }
909 }
910
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200911 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
Christoph Hellwig763b6082010-04-06 19:12:04 +0200912 if (!strcmp(buf, "off") || !strcmp(buf, "none")) {
913 bdrv_flags |= BDRV_O_NOCACHE;
914 } else if (!strcmp(buf, "writeback")) {
915 bdrv_flags |= BDRV_O_CACHE_WB;
Alexander Graf016f5cf2010-05-26 17:51:49 +0200916 } else if (!strcmp(buf, "unsafe")) {
917 bdrv_flags |= BDRV_O_CACHE_WB;
918 bdrv_flags |= BDRV_O_NO_FLUSH;
Christoph Hellwig763b6082010-04-06 19:12:04 +0200919 } else if (!strcmp(buf, "writethrough")) {
920 /* this is the default */
921 } else {
balrog33f00272007-12-24 14:33:24 +0000922 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200923 return NULL;
balrog33f00272007-12-24 14:33:24 +0000924 }
925 }
926
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200927#ifdef CONFIG_LINUX_AIO
928 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
Christoph Hellwig763b6082010-04-06 19:12:04 +0200929 if (!strcmp(buf, "native")) {
930 bdrv_flags |= BDRV_O_NATIVE_AIO;
931 } else if (!strcmp(buf, "threads")) {
932 /* this is the default */
933 } else {
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200934 fprintf(stderr, "qemu: invalid aio option\n");
935 return NULL;
936 }
937 }
938#endif
939
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200940 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +0000941 if (strcmp(buf, "?") == 0) {
942 fprintf(stderr, "qemu: Supported formats:");
943 bdrv_iterate_format(bdrv_format_print, NULL);
944 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200945 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +0000946 }
Markus Armbrustereb852012009-10-27 18:41:44 +0100947 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +0000948 if (!drv) {
949 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200950 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +0000951 }
952 }
953
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100954 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200955 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +0200956 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO && type != IF_NONE) {
aliguoriea8a5d72009-01-22 19:52:21 +0000957 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200958 return NULL;
aliguori428c5702009-01-21 18:59:04 +0000959 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100960
961 on_write_error = parse_block_error_action(buf, 0);
962 if (on_write_error < 0) {
963 return NULL;
964 }
965 }
966
967 on_read_error = BLOCK_ERR_REPORT;
968 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +0200969 if (type != IF_IDE && type != IF_VIRTIO && type != IF_NONE) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +0100970 fprintf(stderr, "rerror is no supported by this format\n");
971 return NULL;
972 }
973
974 on_read_error = parse_block_error_action(buf, 1);
975 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200976 return NULL;
aliguori428c5702009-01-21 18:59:04 +0000977 }
978 }
979
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200980 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200981 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200982 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200983 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200984 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +0200985 }
986
thse4bcb142007-12-02 04:51:10 +0000987 /* compute bus and unit according index */
988
989 if (index != -1) {
990 if (bus_id != 0 || unit_id != -1) {
991 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +0200992 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +0200993 return NULL;
thse4bcb142007-12-02 04:51:10 +0000994 }
995 if (max_devs == 0)
996 {
997 unit_id = index;
998 bus_id = 0;
999 } else {
1000 unit_id = index % max_devs;
1001 bus_id = index / max_devs;
1002 }
1003 }
1004
1005 /* if user doesn't specify a unit_id,
1006 * try to find the first free
1007 */
1008
1009 if (unit_id == -1) {
1010 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001011 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00001012 unit_id++;
1013 if (max_devs && unit_id >= max_devs) {
1014 unit_id -= max_devs;
1015 bus_id++;
1016 }
1017 }
1018 }
1019
1020 /* check unit id */
1021
1022 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001023 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
1024 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001025 return NULL;
thse4bcb142007-12-02 04:51:10 +00001026 }
1027
1028 /*
1029 * ignore multiple definitions
1030 */
1031
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001032 if (drive_get(type, bus_id, unit_id) != NULL) {
1033 *fatal_error = 0;
1034 return NULL;
1035 }
thse4bcb142007-12-02 04:51:10 +00001036
1037 /* init */
1038
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001039 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02001040 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001041 dinfo->id = qemu_strdup(buf);
1042 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001043 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001044 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001045 if (type == IF_IDE || type == IF_SCSI)
1046 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
1047 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001048 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001049 devname, bus_id, mediastr, unit_id);
1050 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001051 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001052 devname, mediastr, unit_id);
1053 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001054 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001055 dinfo->devaddr = devaddr;
1056 dinfo->type = type;
1057 dinfo->bus = bus_id;
1058 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001059 dinfo->on_read_error = on_read_error;
1060 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001061 dinfo->opts = opts;
1062 if (serial)
1063 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00001064 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00001065
thsf60d39b2007-12-17 03:55:57 +00001066 switch(type) {
thse4bcb142007-12-02 04:51:10 +00001067 case IF_IDE:
1068 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00001069 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00001070 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00001071 switch(media) {
1072 case MEDIA_DISK:
1073 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001074 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
1075 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00001076 }
1077 break;
1078 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001079 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00001080 break;
1081 }
1082 break;
1083 case IF_SD:
1084 /* FIXME: This isn't really a floppy, but it's a reasonable
1085 approximation. */
1086 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001087 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00001088 break;
1089 case IF_PFLASH:
1090 case IF_MTD:
1091 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02001092 case IF_VIRTIO:
1093 /* add virtio block device */
1094 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
1095 qemu_opt_set(opts, "driver", "virtio-blk-pci");
1096 qemu_opt_set(opts, "drive", dinfo->id);
1097 if (devaddr)
1098 qemu_opt_set(opts, "addr", devaddr);
1099 break;
Paul Brookaae94602009-05-14 22:35:06 +01001100 case IF_COUNT:
1101 abort();
thse4bcb142007-12-02 04:51:10 +00001102 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001103 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001104 *fatal_error = 0;
1105 return NULL;
1106 }
aliguori9f7965c2008-10-14 14:42:54 +00001107 if (snapshot) {
Alexander Grafc3177282010-05-26 21:04:32 +02001108 /* always use cache=unsafe with snapshot */
Christoph Hellwig763b6082010-04-06 19:12:04 +02001109 bdrv_flags &= ~BDRV_O_CACHE_MASK;
Alexander Grafc3177282010-05-26 21:04:32 +02001110 bdrv_flags |= (BDRV_O_SNAPSHOT|BDRV_O_CACHE_WB|BDRV_O_NO_FLUSH);
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001111 }
1112
Naphtali Sprei2db7ad52010-03-23 12:17:16 +02001113 if (media == MEDIA_CDROM) {
1114 /* CDROM is fine for any interface, don't check. */
1115 ro = 1;
1116 } else if (ro == 1) {
Gerd Hoffmanndc33bb32010-05-26 10:44:44 +02001117 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY && type != IF_NONE) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02001118 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
Naphtali Sprei59f26892009-10-26 16:25:16 +02001119 return NULL;
1120 }
Naphtali Sprei59f26892009-10-26 16:25:16 +02001121 }
Naphtali Sprei2db7ad52010-03-23 12:17:16 +02001122
Naphtali Spreif5edb012010-01-17 16:48:13 +02001123 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001124
Kevin Wolfd6e90982010-03-31 14:40:27 +02001125 if (bdrv_open(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05001126 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
1127 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001128 return NULL;
thse4bcb142007-12-02 04:51:10 +00001129 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001130
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001131 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00001132 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001133 *fatal_error = 0;
1134 return dinfo;
thse4bcb142007-12-02 04:51:10 +00001135}
1136
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001137static int drive_init_func(QemuOpts *opts, void *opaque)
1138{
1139 QEMUMachine *machine = opaque;
1140 int fatal_error = 0;
1141
1142 if (drive_init(opts, machine, &fatal_error) == NULL) {
1143 if (fatal_error)
1144 return 1;
1145 }
1146 return 0;
1147}
1148
1149static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
1150{
1151 if (NULL == qemu_opt_get(opts, "snapshot")) {
1152 qemu_opt_set(opts, "snapshot", "on");
1153 }
1154 return 0;
1155}
1156
Jan Kiszka76e30d02009-07-02 00:19:02 +02001157void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
1158{
1159 boot_set_handler = func;
1160 boot_set_opaque = opaque;
1161}
1162
1163int qemu_boot_set(const char *boot_devices)
1164{
1165 if (!boot_set_handler) {
1166 return -EINVAL;
1167 }
1168 return boot_set_handler(boot_set_opaque, boot_devices);
1169}
1170
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03001171static void validate_bootdevices(char *devices)
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001172{
1173 /* We just do some generic consistency checks */
1174 const char *p;
1175 int bitmap = 0;
1176
1177 for (p = devices; *p != '\0'; p++) {
1178 /* Allowed boot devices are:
1179 * a-b: floppy disk drives
1180 * c-f: IDE disk drives
1181 * g-m: machine implementation dependant drives
1182 * n-p: network devices
1183 * It's up to each machine implementation to check if the given boot
1184 * devices match the actual hardware implementation and firmware
1185 * features.
1186 */
1187 if (*p < 'a' || *p > 'p') {
1188 fprintf(stderr, "Invalid boot device '%c'\n", *p);
1189 exit(1);
1190 }
1191 if (bitmap & (1 << (*p - 'a'))) {
1192 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
1193 exit(1);
1194 }
1195 bitmap |= 1 << (*p - 'a');
1196 }
Jan Kiszkaef3adf62009-07-02 00:19:02 +02001197}
1198
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001199static void restore_boot_devices(void *opaque)
1200{
1201 char *standard_boot_devices = opaque;
Alex Williamson37905d62010-04-30 15:21:11 -06001202 static int first = 1;
1203
1204 /* Restore boot order and remove ourselves after the first boot */
1205 if (first) {
1206 first = 0;
1207 return;
1208 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02001209
1210 qemu_boot_set(standard_boot_devices);
1211
1212 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
1213 qemu_free(standard_boot_devices);
1214}
1215
aliguori268a3622009-04-21 22:30:27 +00001216static void numa_add(const char *optarg)
1217{
1218 char option[128];
1219 char *endptr;
1220 unsigned long long value, endvalue;
1221 int nodenr;
1222
1223 optarg = get_opt_name(option, 128, optarg, ',') + 1;
1224 if (!strcmp(option, "node")) {
1225 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
1226 nodenr = nb_numa_nodes;
1227 } else {
1228 nodenr = strtoull(option, NULL, 10);
1229 }
1230
1231 if (get_param_value(option, 128, "mem", optarg) == 0) {
1232 node_mem[nodenr] = 0;
1233 } else {
1234 value = strtoull(option, &endptr, 0);
1235 switch (*endptr) {
1236 case 0: case 'M': case 'm':
1237 value <<= 20;
1238 break;
1239 case 'G': case 'g':
1240 value <<= 30;
1241 break;
1242 }
1243 node_mem[nodenr] = value;
1244 }
1245 if (get_param_value(option, 128, "cpus", optarg) == 0) {
1246 node_cpumask[nodenr] = 0;
1247 } else {
1248 value = strtoull(option, &endptr, 10);
1249 if (value >= 64) {
1250 value = 63;
1251 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
1252 } else {
1253 if (*endptr == '-') {
1254 endvalue = strtoull(endptr+1, &endptr, 10);
1255 if (endvalue >= 63) {
1256 endvalue = 62;
1257 fprintf(stderr,
1258 "only 63 CPUs in NUMA mode supported.\n");
1259 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001260 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +00001261 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01001262 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +00001263 }
1264 }
1265 node_cpumask[nodenr] = value;
1266 }
1267 nb_numa_nodes++;
1268 }
1269 return;
1270}
1271
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001272static void smp_parse(const char *optarg)
1273{
1274 int smp, sockets = 0, threads = 0, cores = 0;
1275 char *endptr;
1276 char option[128];
1277
1278 smp = strtoul(optarg, &endptr, 10);
1279 if (endptr != optarg) {
1280 if (*endptr == ',') {
1281 endptr++;
1282 }
1283 }
1284 if (get_param_value(option, 128, "sockets", endptr) != 0)
1285 sockets = strtoull(option, NULL, 10);
1286 if (get_param_value(option, 128, "cores", endptr) != 0)
1287 cores = strtoull(option, NULL, 10);
1288 if (get_param_value(option, 128, "threads", endptr) != 0)
1289 threads = strtoull(option, NULL, 10);
1290 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
1291 max_cpus = strtoull(option, NULL, 10);
1292
1293 /* compute missing values, prefer sockets over cores over threads */
1294 if (smp == 0 || sockets == 0) {
1295 sockets = sockets > 0 ? sockets : 1;
1296 cores = cores > 0 ? cores : 1;
1297 threads = threads > 0 ? threads : 1;
1298 if (smp == 0) {
1299 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001300 }
1301 } else {
1302 if (cores == 0) {
1303 threads = threads > 0 ? threads : 1;
1304 cores = smp / (sockets * threads);
1305 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +05301306 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02001307 threads = smp / (cores * sockets);
1308 }
1309 }
1310 }
1311 smp_cpus = smp;
1312 smp_cores = cores > 0 ? cores : 1;
1313 smp_threads = threads > 0 ? threads : 1;
1314 if (max_cpus == 0)
1315 max_cpus = smp_cpus;
1316}
1317
bellard330d0412003-07-26 18:11:40 +00001318/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00001319/* USB devices */
1320
aliguoric0f4ce72009-03-05 23:01:01 +00001321static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00001322{
1323 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001324 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00001325
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001326 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00001327 return -1;
1328
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01001329 /* drivers with .usbdevice_name entry in USBDeviceInfo */
1330 dev = usbdevice_create(devname);
1331 if (dev)
1332 goto done;
1333
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001334 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00001335 if (strstart(devname, "host:", &p)) {
1336 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00001337 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
1338 dev = usb_bt_init(devname[2] ? hci_init(p) :
1339 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00001340 } else {
1341 return -1;
1342 }
pbrook0d92ed32006-05-21 16:30:15 +00001343 if (!dev)
1344 return -1;
1345
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001346done:
bellarda594cfb2005-11-06 16:13:29 +00001347 return 0;
1348}
1349
aliguori1f3870a2008-08-21 19:27:48 +00001350static int usb_device_del(const char *devname)
1351{
1352 int bus_num, addr;
1353 const char *p;
1354
aliguori5d0c5752008-09-14 01:07:41 +00001355 if (strstart(devname, "host:", &p))
1356 return usb_host_device_close(p);
1357
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001358 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00001359 return -1;
1360
1361 p = strchr(devname, '.');
1362 if (!p)
1363 return -1;
1364 bus_num = strtoul(devname, NULL, 0);
1365 addr = strtoul(p + 1, NULL, 0);
1366
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02001367 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00001368}
1369
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001370static int usb_parse(const char *cmdline)
1371{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001372 int r;
1373 r = usb_device_add(cmdline, 0);
1374 if (r < 0) {
1375 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
1376 }
1377 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02001378}
1379
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001380void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001381{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001382 const char *devname = qdict_get_str(qdict, "devname");
1383 if (usb_device_add(devname, 1) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001384 error_report("could not add USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001385 }
bellarda594cfb2005-11-06 16:13:29 +00001386}
1387
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001388void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00001389{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001390 const char *devname = qdict_get_str(qdict, "devname");
1391 if (usb_device_del(devname) < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001392 error_report("could not delete USB device '%s'", devname);
Scott Tsai59d1c1c2009-12-23 04:30:18 +08001393 }
bellarda594cfb2005-11-06 16:13:29 +00001394}
1395
bellardf7cce892004-12-08 22:21:25 +00001396/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00001397/* PCMCIA/Cardbus */
1398
1399static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01001400 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00001401 struct pcmcia_socket_entry_s *next;
1402} *pcmcia_sockets = 0;
1403
Paul Brookbc24a222009-05-10 01:44:56 +01001404void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001405{
1406 struct pcmcia_socket_entry_s *entry;
1407
1408 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
1409 entry->socket = socket;
1410 entry->next = pcmcia_sockets;
1411 pcmcia_sockets = entry;
1412}
1413
Paul Brookbc24a222009-05-10 01:44:56 +01001414void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00001415{
1416 struct pcmcia_socket_entry_s *entry, **ptr;
1417
1418 ptr = &pcmcia_sockets;
1419 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
1420 if (entry->socket == socket) {
1421 *ptr = entry->next;
1422 qemu_free(entry);
1423 }
1424}
1425
aliguori376253e2009-03-05 23:01:23 +00001426void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00001427{
1428 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00001429
balrog201a51f2007-04-30 00:51:09 +00001430 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00001431 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00001432
1433 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00001434 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
1435 iter->socket->attached ? iter->socket->card_string :
1436 "Empty");
balrog201a51f2007-04-30 00:51:09 +00001437}
1438
1439/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001440/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00001441
bellardc4b1fcc2004-03-14 21:44:30 +00001442typedef struct IOHandlerRecord {
1443 int fd;
Juan Quintela7b27a762010-03-11 17:55:39 +01001444 IOCanReadHandler *fd_read_poll;
bellard7c9d8e02005-11-15 22:16:05 +00001445 IOHandler *fd_read;
1446 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00001447 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00001448 void *opaque;
1449 /* temporary data */
1450 struct pollfd *ufd;
Juan Quintela31d4ee62010-03-11 17:55:37 +01001451 QLIST_ENTRY(IOHandlerRecord) next;
bellardc4b1fcc2004-03-14 21:44:30 +00001452} IOHandlerRecord;
1453
Juan Quintela31d4ee62010-03-11 17:55:37 +01001454static QLIST_HEAD(, IOHandlerRecord) io_handlers =
1455 QLIST_HEAD_INITIALIZER(io_handlers);
1456
bellardc4b1fcc2004-03-14 21:44:30 +00001457
bellard7c9d8e02005-11-15 22:16:05 +00001458/* XXX: fd_read_poll should be suppressed, but an API change is
1459 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00001460int qemu_set_fd_handler2(int fd,
Juan Quintela7b27a762010-03-11 17:55:39 +01001461 IOCanReadHandler *fd_read_poll,
ths5fafdf22007-09-16 21:08:06 +00001462 IOHandler *fd_read,
1463 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001464 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00001465{
Juan Quintela31d4ee62010-03-11 17:55:37 +01001466 IOHandlerRecord *ioh;
bellard8a7ddc32004-03-31 19:00:16 +00001467
bellard7c9d8e02005-11-15 22:16:05 +00001468 if (!fd_read && !fd_write) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001469 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001470 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00001471 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00001472 break;
1473 }
bellard8a7ddc32004-03-31 19:00:16 +00001474 }
bellard7c9d8e02005-11-15 22:16:05 +00001475 } else {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001476 QLIST_FOREACH(ioh, &io_handlers, next) {
bellard7c9d8e02005-11-15 22:16:05 +00001477 if (ioh->fd == fd)
1478 goto found;
1479 }
1480 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
Juan Quintela31d4ee62010-03-11 17:55:37 +01001481 QLIST_INSERT_HEAD(&io_handlers, ioh, next);
bellard7c9d8e02005-11-15 22:16:05 +00001482 found:
1483 ioh->fd = fd;
1484 ioh->fd_read_poll = fd_read_poll;
1485 ioh->fd_read = fd_read;
1486 ioh->fd_write = fd_write;
1487 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00001488 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00001489 }
bellard7c9d8e02005-11-15 22:16:05 +00001490 return 0;
1491}
1492
ths5fafdf22007-09-16 21:08:06 +00001493int qemu_set_fd_handler(int fd,
1494 IOHandler *fd_read,
1495 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00001496 void *opaque)
1497{
1498 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00001499}
1500
aliguori56f3a5d2008-10-31 18:07:17 +00001501#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00001502/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00001503/* Polling handling */
1504
1505typedef struct PollingEntry {
1506 PollingFunc *func;
1507 void *opaque;
1508 struct PollingEntry *next;
1509} PollingEntry;
1510
1511static PollingEntry *first_polling_entry;
1512
1513int qemu_add_polling_cb(PollingFunc *func, void *opaque)
1514{
1515 PollingEntry **ppe, *pe;
1516 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00001517 pe->func = func;
1518 pe->opaque = opaque;
1519 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
1520 *ppe = pe;
1521 return 0;
1522}
1523
1524void qemu_del_polling_cb(PollingFunc *func, void *opaque)
1525{
1526 PollingEntry **ppe, *pe;
1527 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
1528 pe = *ppe;
1529 if (pe->func == func && pe->opaque == opaque) {
1530 *ppe = pe->next;
1531 qemu_free(pe);
1532 break;
1533 }
1534 }
1535}
1536
bellarda18e5242006-06-25 17:18:27 +00001537/***********************************************************/
1538/* Wait objects support */
1539typedef struct WaitObjects {
1540 int num;
1541 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
1542 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
1543 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
1544} WaitObjects;
1545
1546static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00001547
bellarda18e5242006-06-25 17:18:27 +00001548int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1549{
1550 WaitObjects *w = &wait_objects;
1551
1552 if (w->num >= MAXIMUM_WAIT_OBJECTS)
1553 return -1;
1554 w->events[w->num] = handle;
1555 w->func[w->num] = func;
1556 w->opaque[w->num] = opaque;
1557 w->num++;
1558 return 0;
1559}
1560
1561void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
1562{
1563 int i, found;
1564 WaitObjects *w = &wait_objects;
1565
1566 found = 0;
1567 for (i = 0; i < w->num; i++) {
1568 if (w->events[i] == handle)
1569 found = 1;
1570 if (found) {
1571 w->events[i] = w->events[i + 1];
1572 w->func[i] = w->func[i + 1];
1573 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00001574 }
bellarda18e5242006-06-25 17:18:27 +00001575 }
1576 if (found)
1577 w->num--;
1578}
1579#endif
1580
bellard8a7ddc32004-03-31 19:00:16 +00001581/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00001582/* machine registration */
1583
blueswir1bdaf78e2008-10-04 07:24:27 +00001584static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00001585QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00001586
1587int qemu_register_machine(QEMUMachine *m)
1588{
1589 QEMUMachine **pm;
1590 pm = &first_machine;
1591 while (*pm != NULL)
1592 pm = &(*pm)->next;
1593 m->next = NULL;
1594 *pm = m;
1595 return 0;
1596}
1597
pbrook9596ebb2007-11-18 01:44:38 +00001598static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00001599{
1600 QEMUMachine *m;
1601
1602 for(m = first_machine; m != NULL; m = m->next) {
1603 if (!strcmp(m->name, name))
1604 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01001605 if (m->alias && !strcmp(m->alias, name))
1606 return m;
bellardcc1daa42005-06-05 14:49:17 +00001607 }
1608 return NULL;
1609}
1610
Anthony Liguori0c257432009-05-21 20:41:01 -05001611static QEMUMachine *find_default_machine(void)
1612{
1613 QEMUMachine *m;
1614
1615 for(m = first_machine; m != NULL; m = m->next) {
1616 if (m->is_default) {
1617 return m;
1618 }
1619 }
1620 return NULL;
1621}
1622
bellardcc1daa42005-06-05 14:49:17 +00001623/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00001624/* main execution loop */
1625
pbrook9596ebb2007-11-18 01:44:38 +00001626static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00001627{
aliguori7d957bd2009-01-15 22:14:11 +00001628 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00001629 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00001630 DisplayChangeListener *dcl = ds->listeners;
1631
Sheng Yang62a27442010-01-26 19:21:16 +08001632 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00001633 dpy_refresh(ds);
1634
1635 while (dcl != NULL) {
1636 if (dcl->gui_timer_interval &&
1637 dcl->gui_timer_interval < interval)
1638 interval = dcl->gui_timer_interval;
1639 dcl = dcl->next;
1640 }
1641 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00001642}
1643
blueswir19043b622009-01-21 19:28:13 +00001644static void nographic_update(void *opaque)
1645{
1646 uint64_t interval = GUI_REFRESH_INTERVAL;
1647
Sheng Yang62a27442010-01-26 19:21:16 +08001648 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00001649 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
1650}
1651
bellard0bd48852005-11-11 00:00:47 +00001652struct vm_change_state_entry {
1653 VMChangeStateHandler *cb;
1654 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001655 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00001656};
1657
Blue Swirl72cf2d42009-09-12 07:36:22 +00001658static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00001659
1660VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
1661 void *opaque)
1662{
1663 VMChangeStateEntry *e;
1664
1665 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00001666
1667 e->cb = cb;
1668 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001669 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00001670 return e;
1671}
1672
1673void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
1674{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001675 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00001676 qemu_free (e);
1677}
1678
Blue Swirl296af7c2010-03-29 19:23:50 +00001679void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00001680{
1681 VMChangeStateEntry *e;
1682
1683 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00001684 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00001685 }
1686}
1687
bellard8a7ddc32004-03-31 19:00:16 +00001688void vm_start(void)
1689{
1690 if (!vm_running) {
1691 cpu_enable_ticks();
1692 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00001693 vm_state_notify(1, 0);
aliguorid6dc3d42009-04-24 18:04:07 +00001694 resume_all_vcpus();
Luiz Capitulino6ed2c482010-04-27 20:35:59 -03001695 monitor_protocol_event(QEVENT_RESUME, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00001696 }
1697}
1698
bellardbb0c6722004-06-20 12:37:32 +00001699/* reset/shutdown handler */
1700
1701typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001702 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00001703 QEMUResetHandler *func;
1704 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00001705} QEMUResetEntry;
1706
Blue Swirl72cf2d42009-09-12 07:36:22 +00001707static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
1708 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00001709static int reset_requested;
1710static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00001711static int powerdown_requested;
Blue Swirl296af7c2010-03-29 19:23:50 +00001712int debug_requested;
Blue Swirl7277e022010-04-12 17:19:06 +00001713int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00001714
aurel32cf7a2fe2008-03-18 06:53:05 +00001715int qemu_shutdown_requested(void)
1716{
1717 int r = shutdown_requested;
1718 shutdown_requested = 0;
1719 return r;
1720}
1721
1722int qemu_reset_requested(void)
1723{
1724 int r = reset_requested;
1725 reset_requested = 0;
1726 return r;
1727}
1728
1729int qemu_powerdown_requested(void)
1730{
1731 int r = powerdown_requested;
1732 powerdown_requested = 0;
1733 return r;
1734}
1735
aliguorie5689022009-04-24 18:03:54 +00001736static int qemu_debug_requested(void)
1737{
1738 int r = debug_requested;
1739 debug_requested = 0;
1740 return r;
1741}
1742
aliguori6e29f5d2009-04-24 18:04:02 +00001743static int qemu_vmstop_requested(void)
1744{
1745 int r = vmstop_requested;
1746 vmstop_requested = 0;
1747 return r;
1748}
1749
Jan Kiszkaa08d4362009-06-27 09:25:07 +02001750void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001751{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02001752 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00001753
bellardbb0c6722004-06-20 12:37:32 +00001754 re->func = func;
1755 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001756 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00001757}
1758
Jan Kiszkadda9b292009-07-02 00:19:02 +02001759void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00001760{
1761 QEMUResetEntry *re;
1762
Blue Swirl72cf2d42009-09-12 07:36:22 +00001763 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02001764 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001765 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02001766 qemu_free(re);
1767 return;
1768 }
1769 }
1770}
1771
1772void qemu_system_reset(void)
1773{
1774 QEMUResetEntry *re, *nre;
1775
1776 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001777 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00001778 re->func(re->opaque);
1779 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06001780 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01001781 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00001782}
1783
1784void qemu_system_reset_request(void)
1785{
bellardd1beab82006-10-02 19:44:22 +00001786 if (no_reboot) {
1787 shutdown_requested = 1;
1788 } else {
1789 reset_requested = 1;
1790 }
aliguorid9f75a42009-04-24 18:03:11 +00001791 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001792}
1793
1794void qemu_system_shutdown_request(void)
1795{
1796 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001797 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00001798}
1799
bellard34751872005-07-02 14:31:34 +00001800void qemu_system_powerdown_request(void)
1801{
1802 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001803 qemu_notify_event();
1804}
1805
ths877cf882007-04-18 18:11:47 +00001806#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00001807static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00001808{
1809 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00001810 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00001811
bellardf3311102006-04-12 20:21:17 +00001812
1813 /* XXX: need to suppress polling by better using win32 events */
1814 ret = 0;
1815 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
1816 ret |= pe->func(pe->opaque);
1817 }
thse6b1e552007-04-18 17:56:02 +00001818 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00001819 int err;
1820 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00001821
aliguori56f3a5d2008-10-31 18:07:17 +00001822 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00001823 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
1824 if (w->func[ret - WAIT_OBJECT_0])
1825 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00001826
ths5fafdf22007-09-16 21:08:06 +00001827 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00001828 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00001829
thse6b1e552007-04-18 17:56:02 +00001830 /* Check if event is signaled */
1831 ret2 = WaitForSingleObject(w->events[i], 0);
1832 if(ret2 == WAIT_OBJECT_0) {
1833 if (w->func[i])
1834 w->func[i](w->opaque[i]);
1835 } else if (ret2 == WAIT_TIMEOUT) {
1836 } else {
1837 err = GetLastError();
1838 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00001839 }
1840 }
bellarda18e5242006-06-25 17:18:27 +00001841 } else if (ret == WAIT_TIMEOUT) {
1842 } else {
1843 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00001844 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00001845 }
bellardf3311102006-04-12 20:21:17 +00001846 }
aliguori56f3a5d2008-10-31 18:07:17 +00001847
1848 *timeout = 0;
1849}
1850#else
blueswir169d64512008-12-07 19:30:18 +00001851static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00001852{
1853}
bellardfd1dff42006-02-01 21:29:26 +00001854#endif
aliguori56f3a5d2008-10-31 18:07:17 +00001855
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001856void main_loop_wait(int nonblocking)
aliguori56f3a5d2008-10-31 18:07:17 +00001857{
1858 IOHandlerRecord *ioh;
1859 fd_set rfds, wfds, xfds;
1860 int ret, nfds;
1861 struct timeval tv;
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001862 int timeout;
aliguori56f3a5d2008-10-31 18:07:17 +00001863
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001864 if (nonblocking)
1865 timeout = 0;
1866 else {
1867 timeout = qemu_calculate_timeout();
1868 qemu_bh_update_timeout(&timeout);
1869 }
aliguori56f3a5d2008-10-31 18:07:17 +00001870
1871 host_main_loop_wait(&timeout);
1872
bellardfd1dff42006-02-01 21:29:26 +00001873 /* poll any events */
1874 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00001875 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00001876 FD_ZERO(&rfds);
1877 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00001878 FD_ZERO(&xfds);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001879 QLIST_FOREACH(ioh, &io_handlers, next) {
thscafffd42007-02-28 21:59:44 +00001880 if (ioh->deleted)
1881 continue;
bellardfd1dff42006-02-01 21:29:26 +00001882 if (ioh->fd_read &&
1883 (!ioh->fd_read_poll ||
1884 ioh->fd_read_poll(ioh->opaque) != 0)) {
1885 FD_SET(ioh->fd, &rfds);
1886 if (ioh->fd > nfds)
1887 nfds = ioh->fd;
1888 }
1889 if (ioh->fd_write) {
1890 FD_SET(ioh->fd, &wfds);
1891 if (ioh->fd > nfds)
1892 nfds = ioh->fd;
1893 }
1894 }
ths3b46e622007-09-17 08:09:54 +00001895
aliguori56f3a5d2008-10-31 18:07:17 +00001896 tv.tv_sec = timeout / 1000;
1897 tv.tv_usec = (timeout % 1000) * 1000;
1898
Jan Kiszkad918f232009-06-24 14:42:30 +02001899 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1900
aliguori48708522009-04-24 18:03:49 +00001901 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00001902 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00001903 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00001904 if (ret > 0) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001905 IOHandlerRecord *pioh;
thscafffd42007-02-28 21:59:44 +00001906
Juan Quintela31d4ee62010-03-11 17:55:37 +01001907 QLIST_FOREACH_SAFE(ioh, &io_handlers, next, pioh) {
thscafffd42007-02-28 21:59:44 +00001908 if (ioh->deleted) {
Juan Quintela31d4ee62010-03-11 17:55:37 +01001909 QLIST_REMOVE(ioh, next);
thscafffd42007-02-28 21:59:44 +00001910 qemu_free(ioh);
Juan Quintela4bed9832010-03-11 17:55:41 +01001911 continue;
1912 }
1913 if (ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
1914 ioh->fd_read(ioh->opaque);
1915 }
1916 if (ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
1917 ioh->fd_write(ioh->opaque);
Juan Quintela31d4ee62010-03-11 17:55:37 +01001918 }
thscafffd42007-02-28 21:59:44 +00001919 }
bellardfd1dff42006-02-01 21:29:26 +00001920 }
Jan Kiszkad918f232009-06-24 14:42:30 +02001921
1922 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00001923
Paolo Bonzinib6964822010-03-10 11:38:45 +01001924 qemu_run_all_timers();
Jan Kiszka21d5d122009-09-15 13:36:04 +02001925
pbrook423f0742007-05-23 00:06:54 +00001926 /* Check bottom-halves last in case any of the earlier events triggered
1927 them. */
1928 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00001929
bellard5905b2e2004-08-01 21:53:26 +00001930}
1931
aliguori43b96852009-04-24 18:03:33 +00001932static int vm_can_run(void)
1933{
1934 if (powerdown_requested)
1935 return 0;
1936 if (reset_requested)
1937 return 0;
1938 if (shutdown_requested)
1939 return 0;
aliguorie5689022009-04-24 18:03:54 +00001940 if (debug_requested)
1941 return 0;
aliguori43b96852009-04-24 18:03:33 +00001942 return 1;
1943}
1944
Blue Swirld9c32312009-08-09 08:42:19 +00001945qemu_irq qemu_system_powerdown;
1946
aliguori43b96852009-04-24 18:03:33 +00001947static void main_loop(void)
1948{
aliguori6e29f5d2009-04-24 18:04:02 +00001949 int r;
aliguori43b96852009-04-24 18:03:33 +00001950
Blue Swirl7277e022010-04-12 17:19:06 +00001951 qemu_main_loop_start();
aliguorid6dc3d42009-04-24 18:04:07 +00001952
aliguori6e29f5d2009-04-24 18:04:02 +00001953 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00001954 do {
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001955 bool nonblocking = false;
aliguorie6e35b12009-04-24 18:03:57 +00001956#ifdef CONFIG_PROFILER
1957 int64_t ti;
1958#endif
aliguorid6dc3d42009-04-24 18:04:07 +00001959#ifndef CONFIG_IOTHREAD
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001960 nonblocking = tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00001961#endif
aliguori43b96852009-04-24 18:03:33 +00001962#ifdef CONFIG_PROFILER
1963 ti = profile_getclock();
1964#endif
Paolo Bonzinid6f4ade2010-03-10 11:38:54 +01001965 main_loop_wait(nonblocking);
aliguori43b96852009-04-24 18:03:33 +00001966#ifdef CONFIG_PROFILER
1967 dev_time += profile_getclock() - ti;
1968#endif
aliguorie5689022009-04-24 18:03:54 +00001969 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00001970
Blue Swirl54fc6ea2010-03-29 19:23:46 +00001971 if ((r = qemu_debug_requested())) {
1972 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001973 }
aliguori43b96852009-04-24 18:03:33 +00001974 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001975 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00001976 if (no_shutdown) {
1977 vm_stop(0);
1978 no_shutdown = 0;
1979 } else
1980 break;
1981 }
aliguorid6dc3d42009-04-24 18:04:07 +00001982 if (qemu_reset_requested()) {
1983 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00001984 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00001985 resume_all_vcpus();
1986 }
Blue Swirld9c32312009-08-09 08:42:19 +00001987 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00001988 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00001989 qemu_irq_raise(qemu_system_powerdown);
1990 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001991 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00001992 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02001993 }
aliguori43b96852009-04-24 18:03:33 +00001994 }
aliguorid6dc3d42009-04-24 18:04:07 +00001995 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00001996}
1997
pbrook9bd7e6d2009-04-07 22:58:45 +00001998static void version(void)
1999{
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02002000 printf("QEMU emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00002001}
2002
ths15f82202007-06-29 23:26:08 +00002003static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00002004{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002005 const char *options_help =
Blue Swirlad960902010-03-29 19:23:52 +00002006#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2007 opt_help
blueswir15824d652009-03-28 06:44:27 +00002008#define DEFHEADING(text) stringify(text) "\n"
2009#include "qemu-options.h"
2010#undef DEF
2011#undef DEFHEADING
2012#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002013 ;
2014 version();
2015 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03002016 "\n"
Thomas Monjalonf75ca1a2010-04-28 14:42:01 +02002017 "'disk_image' is a raw hard disk image for IDE hard disk 0\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002018 "\n"
2019 "%s\n"
malc3f020d72010-02-08 12:04:56 +03002020 "During emulation, the following keys are useful:\n"
2021 "ctrl-alt-f toggle full screen\n"
2022 "ctrl-alt-n switch to virtual console 'n'\n"
2023 "ctrl-alt toggle mouse and keyboard grab\n"
2024 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01002025 "When using -nographic, press 'ctrl-a h' to get some help.\n",
2026 "qemu",
2027 options_help);
ths15f82202007-06-29 23:26:08 +00002028 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00002029}
2030
bellardcd6f1162004-05-13 22:02:20 +00002031#define HAS_ARG 0x0001
2032
2033enum {
Blue Swirlad960902010-03-29 19:23:52 +00002034#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
blueswir15824d652009-03-28 06:44:27 +00002035 opt_enum,
2036#define DEFHEADING(text)
2037#include "qemu-options.h"
2038#undef DEF
2039#undef DEFHEADING
2040#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00002041};
2042
2043typedef struct QEMUOption {
2044 const char *name;
2045 int flags;
2046 int index;
Blue Swirlad960902010-03-29 19:23:52 +00002047 uint32_t arch_mask;
bellardcd6f1162004-05-13 22:02:20 +00002048} QEMUOption;
2049
blueswir1dbed7e42008-10-01 19:38:09 +00002050static const QEMUOption qemu_options[] = {
Blue Swirlad960902010-03-29 19:23:52 +00002051 { "h", 0, QEMU_OPTION_h, QEMU_ARCH_ALL },
2052#define DEF(option, opt_arg, opt_enum, opt_help, arch_mask) \
2053 { option, opt_arg, opt_enum, arch_mask },
blueswir15824d652009-03-28 06:44:27 +00002054#define DEFHEADING(text)
2055#include "qemu-options.h"
2056#undef DEF
2057#undef DEFHEADING
2058#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00002059 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00002060};
malc3893c122008-09-28 00:42:05 +00002061static void select_vgahw (const char *p)
2062{
2063 const char *opts;
2064
Gerd Hoffmann64465292009-12-08 13:11:45 +01002065 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10002066 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00002067 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002068 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00002069 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002070 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00002071 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002072 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00002073 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10002074 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00002075 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00002076 invalid_vga:
2077 fprintf(stderr, "Unknown vga type: %s\n", p);
2078 exit(1);
2079 }
malccb5a7aa2008-09-28 00:42:12 +00002080 while (*opts) {
2081 const char *nextopt;
2082
2083 if (strstart(opts, ",retrace=", &nextopt)) {
2084 opts = nextopt;
2085 if (strstart(opts, "dumb", &nextopt))
2086 vga_retrace_method = VGA_RETRACE_DUMB;
2087 else if (strstart(opts, "precise", &nextopt))
2088 vga_retrace_method = VGA_RETRACE_PRECISE;
2089 else goto invalid_vga;
2090 } else goto invalid_vga;
2091 opts = nextopt;
2092 }
malc3893c122008-09-28 00:42:05 +00002093}
2094
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002095static int balloon_parse(const char *arg)
2096{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002097 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002098
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002099 if (strcmp(arg, "none") == 0) {
2100 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002101 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002102
2103 if (!strncmp(arg, "virtio", 6)) {
2104 if (arg[6] == ',') {
2105 /* have params -> parse them */
Markus Armbruster8212c642010-02-10 19:52:18 +01002106 opts = qemu_opts_parse(&qemu_device_opts, arg+7, 0);
Gerd Hoffmann382f0742009-08-14 10:34:22 +02002107 if (!opts)
2108 return -1;
2109 } else {
2110 /* create empty opts */
2111 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2112 }
2113 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
2114 return 0;
2115 }
2116
2117 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002118}
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02002119
bellard3587d7e2006-06-26 20:03:44 +00002120#ifdef _WIN32
2121static BOOL WINAPI qemu_ctrl_handler(DWORD type)
2122{
2123 exit(STATUS_CONTROL_C_EXIT);
2124 return TRUE;
2125}
2126#endif
2127
aliguori5b08fc12008-08-21 20:08:03 +00002128#ifndef _WIN32
2129
2130static void termsig_handler(int signal)
2131{
2132 qemu_system_shutdown_request();
2133}
2134
Jan Kiszka7c3370d2009-05-08 12:34:17 +02002135static void sigchld_handler(int signal)
2136{
2137 waitpid(-1, NULL, WNOHANG);
2138}
2139
2140static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00002141{
2142 struct sigaction act;
2143
2144 memset(&act, 0, sizeof(act));
2145 act.sa_handler = termsig_handler;
2146 sigaction(SIGINT, &act, NULL);
2147 sigaction(SIGHUP, &act, NULL);
2148 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02002149
2150 act.sa_handler = sigchld_handler;
2151 act.sa_flags = SA_NOCLDSTOP;
2152 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00002153}
2154
2155#endif
2156
Paul Brook5cea8592009-05-30 00:52:44 +01002157#ifdef _WIN32
2158/* Look for support files in the same directory as the executable. */
2159static char *find_datadir(const char *argv0)
2160{
2161 char *p;
2162 char buf[MAX_PATH];
2163 DWORD len;
2164
2165 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
2166 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03002167 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01002168 }
2169
2170 buf[len] = 0;
2171 p = buf + len - 1;
2172 while (p != buf && *p != '\\')
2173 p--;
2174 *p = 0;
2175 if (access(buf, R_OK) == 0) {
2176 return qemu_strdup(buf);
2177 }
2178 return NULL;
2179}
2180#else /* !_WIN32 */
2181
2182/* Find a likely location for support files using the location of the binary.
2183 For installed binaries this will be "$bindir/../share/qemu". When
2184 running from the build tree this will be "$bindir/../pc-bios". */
2185#define SHARE_SUFFIX "/share/qemu"
2186#define BUILD_SUFFIX "/pc-bios"
2187static char *find_datadir(const char *argv0)
2188{
2189 char *dir;
2190 char *p = NULL;
2191 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01002192 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00002193 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01002194
2195#if defined(__linux__)
2196 {
2197 int len;
2198 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
2199 if (len > 0) {
2200 buf[len] = 0;
2201 p = buf;
2202 }
2203 }
2204#elif defined(__FreeBSD__)
2205 {
Juergen Lock92c0e652010-03-25 22:07:12 +01002206 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
2207 size_t len = sizeof(buf) - 1;
2208
2209 *buf = '\0';
2210 if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
2211 *buf) {
2212 buf[sizeof(buf) - 1] = '\0';
Paul Brook5cea8592009-05-30 00:52:44 +01002213 p = buf;
2214 }
2215 }
2216#endif
2217 /* If we don't have any way of figuring out the actual executable
2218 location then try argv[0]. */
2219 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02002220 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01002221 if (!p) {
2222 return NULL;
2223 }
2224 }
2225 dir = dirname(p);
2226 dir = dirname(dir);
2227
Blue Swirl3a417592009-06-09 19:12:21 +00002228 max_len = strlen(dir) +
2229 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
2230 res = qemu_mallocz(max_len);
2231 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01002232 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00002233 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01002234 if (access(res, R_OK)) {
2235 qemu_free(res);
2236 res = NULL;
2237 }
2238 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02002239
Paul Brook5cea8592009-05-30 00:52:44 +01002240 return res;
2241}
2242#undef SHARE_SUFFIX
2243#undef BUILD_SUFFIX
2244#endif
2245
2246char *qemu_find_file(int type, const char *name)
2247{
2248 int len;
2249 const char *subdir;
2250 char *buf;
2251
2252 /* If name contains path separators then try it as a straight path. */
2253 if ((strchr(name, '/') || strchr(name, '\\'))
2254 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02002255 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01002256 }
2257 switch (type) {
2258 case QEMU_FILE_TYPE_BIOS:
2259 subdir = "";
2260 break;
2261 case QEMU_FILE_TYPE_KEYMAP:
2262 subdir = "keymaps/";
2263 break;
2264 default:
2265 abort();
2266 }
2267 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
2268 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00002269 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01002270 if (access(buf, R_OK)) {
2271 qemu_free(buf);
2272 return NULL;
2273 }
2274 return buf;
2275}
2276
Markus Armbrusterff952ba2010-01-29 19:48:57 +01002277static int device_help_func(QemuOpts *opts, void *opaque)
2278{
2279 return qdev_device_help(opts);
2280}
2281
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002282static int device_init_func(QemuOpts *opts, void *opaque)
2283{
2284 DeviceState *dev;
2285
2286 dev = qdev_device_add(opts);
2287 if (!dev)
2288 return -1;
2289 return 0;
2290}
2291
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01002292static int chardev_init_func(QemuOpts *opts, void *opaque)
2293{
2294 CharDriverState *chr;
2295
2296 chr = qemu_chr_open_opts(opts, NULL);
2297 if (!chr)
2298 return -1;
2299 return 0;
2300}
2301
Gautham R Shenoy74db9202010-04-29 17:44:43 +05302302#ifdef CONFIG_LINUX
2303static int fsdev_init_func(QemuOpts *opts, void *opaque)
2304{
2305 int ret;
2306 ret = qemu_fsdev_add(opts);
2307
2308 return ret;
2309}
2310#endif
2311
Gerd Hoffmann88589342009-12-08 13:11:50 +01002312static int mon_init_func(QemuOpts *opts, void *opaque)
2313{
2314 CharDriverState *chr;
2315 const char *chardev;
2316 const char *mode;
2317 int flags;
2318
2319 mode = qemu_opt_get(opts, "mode");
2320 if (mode == NULL) {
2321 mode = "readline";
2322 }
2323 if (strcmp(mode, "readline") == 0) {
2324 flags = MONITOR_USE_READLINE;
2325 } else if (strcmp(mode, "control") == 0) {
2326 flags = MONITOR_USE_CONTROL;
2327 } else {
2328 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
2329 exit(1);
2330 }
2331
2332 if (qemu_opt_get_bool(opts, "default", 0))
2333 flags |= MONITOR_IS_DEFAULT;
2334
2335 chardev = qemu_opt_get(opts, "chardev");
2336 chr = qemu_chr_find(chardev);
2337 if (chr == NULL) {
2338 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
2339 exit(1);
2340 }
2341
2342 monitor_init(chr, flags);
2343 return 0;
2344}
2345
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002346static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01002347{
2348 static int monitor_device_index = 0;
2349 QemuOpts *opts;
2350 const char *p;
2351 char label[32];
2352 int def = 0;
2353
2354 if (strstart(optarg, "chardev:", &p)) {
2355 snprintf(label, sizeof(label), "%s", p);
2356 } else {
Jan Kiszka140e0652010-04-06 16:55:52 +02002357 snprintf(label, sizeof(label), "compat_monitor%d",
2358 monitor_device_index);
2359 if (monitor_device_index == 0) {
Gerd Hoffmann88589342009-12-08 13:11:50 +01002360 def = 1;
2361 }
2362 opts = qemu_chr_parse_compat(label, optarg);
2363 if (!opts) {
2364 fprintf(stderr, "parse error: %s\n", optarg);
2365 exit(1);
2366 }
2367 }
2368
2369 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
2370 if (!opts) {
2371 fprintf(stderr, "duplicate chardev: %s\n", label);
2372 exit(1);
2373 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01002374 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01002375 qemu_opt_set(opts, "chardev", label);
2376 if (def)
2377 qemu_opt_set(opts, "default", "on");
2378 monitor_device_index++;
2379}
2380
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002381struct device_config {
2382 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002383 DEV_USB, /* -usbdevice */
2384 DEV_BT, /* -bt */
2385 DEV_SERIAL, /* -serial */
2386 DEV_PARALLEL, /* -parallel */
2387 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002388 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002389 } type;
2390 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002391 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002392};
Blue Swirl72cf2d42009-09-12 07:36:22 +00002393QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002394
2395static void add_device_config(int type, const char *cmdline)
2396{
2397 struct device_config *conf;
2398
2399 conf = qemu_mallocz(sizeof(*conf));
2400 conf->type = type;
2401 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002402 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002403}
2404
2405static int foreach_device_config(int type, int (*func)(const char *cmdline))
2406{
2407 struct device_config *conf;
2408 int rc;
2409
Blue Swirl72cf2d42009-09-12 07:36:22 +00002410 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002411 if (conf->type != type)
2412 continue;
2413 rc = func(conf->cmdline);
2414 if (0 != rc)
2415 return rc;
2416 }
2417 return 0;
2418}
2419
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01002420static int serial_parse(const char *devname)
2421{
2422 static int index = 0;
2423 char label[32];
2424
2425 if (strcmp(devname, "none") == 0)
2426 return 0;
2427 if (index == MAX_SERIAL_PORTS) {
2428 fprintf(stderr, "qemu: too many serial ports\n");
2429 exit(1);
2430 }
2431 snprintf(label, sizeof(label), "serial%d", index);
2432 serial_hds[index] = qemu_chr_open(label, devname, NULL);
2433 if (!serial_hds[index]) {
2434 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
2435 devname, strerror(errno));
2436 return -1;
2437 }
2438 index++;
2439 return 0;
2440}
2441
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01002442static int parallel_parse(const char *devname)
2443{
2444 static int index = 0;
2445 char label[32];
2446
2447 if (strcmp(devname, "none") == 0)
2448 return 0;
2449 if (index == MAX_PARALLEL_PORTS) {
2450 fprintf(stderr, "qemu: too many parallel ports\n");
2451 exit(1);
2452 }
2453 snprintf(label, sizeof(label), "parallel%d", index);
2454 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
2455 if (!parallel_hds[index]) {
2456 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
2457 devname, strerror(errno));
2458 return -1;
2459 }
2460 index++;
2461 return 0;
2462}
2463
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002464static int virtcon_parse(const char *devname)
2465{
2466 static int index = 0;
2467 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05302468 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002469
2470 if (strcmp(devname, "none") == 0)
2471 return 0;
2472 if (index == MAX_VIRTIO_CONSOLES) {
2473 fprintf(stderr, "qemu: too many virtio consoles\n");
2474 exit(1);
2475 }
Amit Shah392ecf52010-01-21 16:19:23 +05302476
2477 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2478 qemu_opt_set(bus_opts, "driver", "virtio-serial");
2479
2480 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2481 qemu_opt_set(dev_opts, "driver", "virtconsole");
2482
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002483 snprintf(label, sizeof(label), "virtcon%d", index);
2484 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
2485 if (!virtcon_hds[index]) {
2486 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
2487 devname, strerror(errno));
2488 return -1;
2489 }
Amit Shah392ecf52010-01-21 16:19:23 +05302490 qemu_opt_set(dev_opts, "chardev", label);
2491
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01002492 index++;
2493 return 0;
2494}
2495
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08002496static int debugcon_parse(const char *devname)
2497{
2498 QemuOpts *opts;
2499
2500 if (!qemu_chr_open("debugcon", devname, NULL)) {
2501 exit(1);
2502 }
2503 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
2504 if (!opts) {
2505 fprintf(stderr, "qemu: already have a debugcon device\n");
2506 exit(1);
2507 }
2508 qemu_opt_set(opts, "driver", "isa-debugcon");
2509 qemu_opt_set(opts, "chardev", "debugcon");
2510 return 0;
2511}
2512
Anthony Liguori6530a972010-01-22 09:18:06 -06002513static const QEMUOption *lookup_opt(int argc, char **argv,
2514 const char **poptarg, int *poptind)
2515{
2516 const QEMUOption *popt;
2517 int optind = *poptind;
2518 char *r = argv[optind];
2519 const char *optarg;
2520
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002521 loc_set_cmdline(argv, optind, 1);
Anthony Liguori6530a972010-01-22 09:18:06 -06002522 optind++;
2523 /* Treat --foo the same as -foo. */
2524 if (r[1] == '-')
2525 r++;
2526 popt = qemu_options;
2527 for(;;) {
2528 if (!popt->name) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002529 error_report("invalid option");
Anthony Liguori6530a972010-01-22 09:18:06 -06002530 exit(1);
2531 }
2532 if (!strcmp(popt->name, r + 1))
2533 break;
2534 popt++;
2535 }
2536 if (popt->flags & HAS_ARG) {
2537 if (optind >= argc) {
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002538 error_report("requires an argument");
Anthony Liguori6530a972010-01-22 09:18:06 -06002539 exit(1);
2540 }
2541 optarg = argv[optind++];
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01002542 loc_set_cmdline(argv, optind - 2, 2);
Anthony Liguori6530a972010-01-22 09:18:06 -06002543 } else {
2544 optarg = NULL;
2545 }
2546
2547 *poptarg = optarg;
2548 *poptind = optind;
2549
2550 return popt;
2551}
2552
malc902b3d52008-12-10 19:18:40 +00002553int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00002554{
aliguori59030a82009-04-05 18:43:41 +00002555 const char *gdbstub_dev = NULL;
thse4bcb142007-12-02 04:51:10 +00002556 int i;
Eduardo Habkostda1fcfd2010-04-06 19:22:07 -03002557 int snapshot, linux_boot;
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01002558 const char *icount_option = NULL;
bellard7f7f9872003-10-30 01:11:23 +00002559 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00002560 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05002561 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00002562 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00002563 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00002564 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02002565 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00002566 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06002567 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00002568 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002569 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00002570 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00002571#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00002572 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00002573#endif
bellard26a5f132008-05-28 12:30:31 +00002574 int tb_size;
ths93815bc2007-03-19 15:58:31 +00002575 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00002576 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00002577#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00002578 int fd = 0;
2579 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00002580 const char *chroot_dir = NULL;
2581 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00002582#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002583 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06002584 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00002585
Markus Armbruster65abca02010-02-24 14:37:14 +01002586 error_set_progname(argv[0]);
2587
Jan Kiszka68752042009-09-15 13:36:04 +02002588 init_clocks();
2589
malc902b3d52008-12-10 19:18:40 +00002590 qemu_cache_utils_init(envp);
2591
Blue Swirl72cf2d42009-09-12 07:36:22 +00002592 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00002593#ifndef _WIN32
2594 {
2595 struct sigaction act;
2596 sigfillset(&act.sa_mask);
2597 act.sa_flags = 0;
2598 act.sa_handler = SIG_IGN;
2599 sigaction(SIGPIPE, &act, NULL);
2600 }
bellard3587d7e2006-06-26 20:03:44 +00002601#else
2602 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00002603 /* Note: cpu_interrupt() is currently not SMP safe, so we force
2604 QEMU to run on a single CPU */
2605 {
2606 HANDLE h;
2607 DWORD mask, smask;
2608 int i;
2609 h = GetCurrentProcess();
2610 if (GetProcessAffinityMask(h, &mask, &smask)) {
2611 for(i = 0; i < 32; i++) {
2612 if (mask & (1 << i))
2613 break;
2614 }
2615 if (i != 32) {
2616 mask = 1 << i;
2617 SetProcessAffinityMask(h, mask);
2618 }
2619 }
2620 }
bellard67b915a2004-03-31 23:37:16 +00002621#endif
bellardbe995c22006-06-25 16:25:21 +00002622
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05002623 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05002624 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00002625 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00002626 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00002627 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00002628 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00002629 kernel_filename = NULL;
2630 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00002631 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00002632 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00002633
aliguori268a3622009-04-21 22:30:27 +00002634 for (i = 0; i < MAX_NODES; i++) {
2635 node_mem[i] = 0;
2636 node_cpumask[i] = 0;
2637 }
2638
aliguori268a3622009-04-21 22:30:27 +00002639 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00002640 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00002641
bellard26a5f132008-05-28 12:30:31 +00002642 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00002643 autostart= 1;
2644
Anthony Liguori292444c2010-01-21 10:57:58 -06002645 /* first pass of option parsing */
2646 optind = 1;
2647 while (optind < argc) {
2648 if (argv[optind][0] != '-') {
2649 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06002650 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06002651 continue;
2652 } else {
2653 const QEMUOption *popt;
2654
2655 popt = lookup_opt(argc, argv, &optarg, &optind);
2656 switch (popt->index) {
2657 case QEMU_OPTION_nodefconfig:
2658 defconfig=0;
2659 break;
2660 }
2661 }
2662 }
2663
2664 if (defconfig) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002665 int ret;
Markus Armbrustercf5a65a2010-02-18 19:48:33 +01002666
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002667 ret = qemu_read_config_file(CONFIG_QEMU_CONFDIR "/qemu.conf");
Kevin Wolf019e78b2010-05-17 10:36:47 +02002668 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002669 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002670 }
2671
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002672 ret = qemu_read_config_file(arch_config_name);
Kevin Wolf019e78b2010-05-17 10:36:47 +02002673 if (ret < 0 && ret != -ENOENT) {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01002674 exit(1);
Anthony Liguori292444c2010-01-21 10:57:58 -06002675 }
2676 }
Blue Swirlde06f8d2010-03-29 19:23:50 +00002677 cpudef_init();
Anthony Liguori292444c2010-01-21 10:57:58 -06002678
2679 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00002680 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00002681 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00002682 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00002683 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06002684 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002685 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00002686 } else {
2687 const QEMUOption *popt;
2688
Anthony Liguori6530a972010-01-22 09:18:06 -06002689 popt = lookup_opt(argc, argv, &optarg, &optind);
Blue Swirlad960902010-03-29 19:23:52 +00002690 if (!(popt->arch_mask & arch_type)) {
2691 printf("Option %s not supported for this target\n", popt->name);
2692 exit(1);
2693 }
bellardcd6f1162004-05-13 22:02:20 +00002694 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00002695 case QEMU_OPTION_M:
2696 machine = find_machine(optarg);
2697 if (!machine) {
2698 QEMUMachine *m;
2699 printf("Supported machines are:\n");
2700 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002701 if (m->alias)
2702 printf("%-10s %s (alias of %s)\n",
2703 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00002704 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00002705 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05002706 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00002707 }
ths15f82202007-06-29 23:26:08 +00002708 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00002709 }
2710 break;
j_mayer94fc95c2007-03-05 19:44:02 +00002711 case QEMU_OPTION_cpu:
2712 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00002713 if (*optarg == '?') {
Blue Swirl262353c2010-05-04 19:55:35 +00002714 list_cpus(stdout, &fprintf, optarg);
ths15f82202007-06-29 23:26:08 +00002715 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00002716 } else {
2717 cpu_model = optarg;
2718 }
2719 break;
bellardcd6f1162004-05-13 22:02:20 +00002720 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00002721 initrd_filename = optarg;
2722 break;
bellardcd6f1162004-05-13 22:02:20 +00002723 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00002724 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002725 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00002726 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002727 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00002728 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00002729 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00002730 translation == BIOS_ATA_TRANSLATION_LBA ?
2731 ",trans=lba" :
2732 translation == BIOS_ATA_TRANSLATION_NONE ?
2733 ",trans=none" : "");
2734 break;
bellardcd6f1162004-05-13 22:02:20 +00002735 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00002736 case QEMU_OPTION_hdc:
2737 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00002738 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00002739 break;
thse4bcb142007-12-02 04:51:10 +00002740 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00002741 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00002742 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02002743 case QEMU_OPTION_set:
2744 if (qemu_set_option(optarg) != 0)
2745 exit(1);
2746 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01002747 case QEMU_OPTION_global:
2748 if (qemu_global_option(optarg) != 0)
2749 exit(1);
2750 break;
balrog3e3d5812007-04-30 02:09:25 +00002751 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00002752 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00002753 break;
pbrooka1bb27b2007-04-06 16:49:48 +00002754 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00002755 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00002756 break;
j_mayer86f55662007-04-24 06:52:59 +00002757 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00002758 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00002759 break;
bellardcd6f1162004-05-13 22:02:20 +00002760 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00002761 snapshot = 1;
2762 break;
bellardcd6f1162004-05-13 22:02:20 +00002763 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00002764 {
bellard330d0412003-07-26 18:11:40 +00002765 const char *p;
2766 p = optarg;
2767 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002768 if (cyls < 1 || cyls > 16383)
2769 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002770 if (*p != ',')
2771 goto chs_fail;
2772 p++;
2773 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002774 if (heads < 1 || heads > 16)
2775 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00002776 if (*p != ',')
2777 goto chs_fail;
2778 p++;
2779 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00002780 if (secs < 1 || secs > 63)
2781 goto chs_fail;
2782 if (*p == ',') {
2783 p++;
2784 if (!strcmp(p, "none"))
2785 translation = BIOS_ATA_TRANSLATION_NONE;
2786 else if (!strcmp(p, "lba"))
2787 translation = BIOS_ATA_TRANSLATION_LBA;
2788 else if (!strcmp(p, "auto"))
2789 translation = BIOS_ATA_TRANSLATION_AUTO;
2790 else
2791 goto chs_fail;
2792 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00002793 chs_fail:
bellard46d47672004-11-16 01:45:27 +00002794 fprintf(stderr, "qemu: invalid physical CHS format\n");
2795 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00002796 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002797 if (hda_opts != NULL) {
2798 char num[16];
2799 snprintf(num, sizeof(num), "%d", cyls);
2800 qemu_opt_set(hda_opts, "cyls", num);
2801 snprintf(num, sizeof(num), "%d", heads);
2802 qemu_opt_set(hda_opts, "heads", num);
2803 snprintf(num, sizeof(num), "%d", secs);
2804 qemu_opt_set(hda_opts, "secs", num);
2805 if (translation == BIOS_ATA_TRANSLATION_LBA)
2806 qemu_opt_set(hda_opts, "trans", "lba");
2807 if (translation == BIOS_ATA_TRANSLATION_NONE)
2808 qemu_opt_set(hda_opts, "trans", "none");
2809 }
bellard330d0412003-07-26 18:11:40 +00002810 }
2811 break;
aliguori268a3622009-04-21 22:30:27 +00002812 case QEMU_OPTION_numa:
2813 if (nb_numa_nodes >= MAX_NODES) {
2814 fprintf(stderr, "qemu: too many NUMA nodes\n");
2815 exit(1);
2816 }
2817 numa_add(optarg);
2818 break;
bellardcd6f1162004-05-13 22:02:20 +00002819 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002820 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00002821 break;
balrog4d3b6f62008-02-10 16:33:14 +00002822#ifdef CONFIG_CURSES
2823 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05002824 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00002825 break;
2826#endif
balroga171fe32007-04-30 01:48:07 +00002827 case QEMU_OPTION_portrait:
2828 graphic_rotate = 1;
2829 break;
bellardcd6f1162004-05-13 22:02:20 +00002830 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00002831 kernel_filename = optarg;
2832 break;
bellardcd6f1162004-05-13 22:02:20 +00002833 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00002834 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00002835 break;
bellardcd6f1162004-05-13 22:02:20 +00002836 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00002837 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00002838 break;
bellardcd6f1162004-05-13 22:02:20 +00002839 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00002840 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002841 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02002842 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002843 };
2844 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002845 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002846 int legacy = 0;
2847
2848 if (!strchr(optarg, '=')) {
2849 legacy = 1;
2850 pstrcpy(buf, sizeof(buf), optarg);
2851 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
2852 fprintf(stderr,
2853 "qemu: unknown boot parameter '%s' in '%s'\n",
2854 buf, optarg);
2855 exit(1);
2856 }
2857
2858 if (legacy ||
2859 get_param_value(buf, sizeof(buf), "order", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002860 validate_bootdevices(buf);
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002861 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00002862 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002863 if (!legacy) {
2864 if (get_param_value(buf, sizeof(buf),
2865 "once", optarg)) {
Eduardo Habkost4e9e9d62010-04-06 19:22:08 -03002866 validate_bootdevices(buf);
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002867 standard_boot_devices = qemu_strdup(boot_devices);
2868 pstrcpy(boot_devices, sizeof(boot_devices), buf);
2869 qemu_register_reset(restore_boot_devices,
2870 standard_boot_devices);
2871 }
Jan Kiszka95387492009-07-02 00:19:02 +02002872 if (get_param_value(buf, sizeof(buf),
2873 "menu", optarg)) {
2874 if (!strcmp(buf, "on")) {
2875 boot_menu = 1;
2876 } else if (!strcmp(buf, "off")) {
2877 boot_menu = 0;
2878 } else {
2879 fprintf(stderr,
2880 "qemu: invalid option value '%s'\n",
2881 buf);
2882 exit(1);
2883 }
2884 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002885 }
bellard36b486b2003-11-11 13:36:08 +00002886 }
2887 break;
bellardcd6f1162004-05-13 22:02:20 +00002888 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00002889 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00002890 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00002891 break;
bellard52ca8d62006-06-14 16:03:05 +00002892 case QEMU_OPTION_no_fd_bootchk:
2893 fd_bootchk = 0;
2894 break;
Mark McLoughlina1ea4582009-10-08 19:58:26 +01002895 case QEMU_OPTION_netdev:
2896 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
2897 exit(1);
2898 }
2899 break;
bellard7c9d8e02005-11-15 22:16:05 +00002900 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01002901 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00002902 exit(1);
2903 }
bellard702c6512004-04-02 21:21:32 +00002904 break;
bellardc7f74642004-08-24 21:57:12 +00002905#ifdef CONFIG_SLIRP
2906 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002907 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00002908 break;
ths47d5d012007-02-20 00:05:08 +00002909 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02002910 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00002911 break;
bellardc94c8d62004-09-13 21:37:34 +00002912#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00002913 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01002914 if (net_slirp_smb(optarg) < 0)
2915 exit(1);
bellard9d728e82004-09-05 23:09:03 +00002916 break;
bellardc94c8d62004-09-13 21:37:34 +00002917#endif
bellard9bf05442004-08-25 22:12:49 +00002918 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01002919 if (net_slirp_redir(optarg) < 0)
2920 exit(1);
bellard9bf05442004-08-25 22:12:49 +00002921 break;
bellardc7f74642004-08-24 21:57:12 +00002922#endif
balrogdc72ac12008-11-09 00:04:26 +00002923 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002924 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00002925 break;
bellard1d14ffa2005-10-30 18:58:22 +00002926 case QEMU_OPTION_audio_help:
Blue Swirlad960902010-03-29 19:23:52 +00002927 if (!(audio_available())) {
2928 printf("Option %s not supported for this target\n", popt->name);
2929 exit(1);
2930 }
bellard1d14ffa2005-10-30 18:58:22 +00002931 AUD_help ();
2932 exit (0);
2933 break;
2934 case QEMU_OPTION_soundhw:
Blue Swirlad960902010-03-29 19:23:52 +00002935 if (!(audio_available())) {
2936 printf("Option %s not supported for this target\n", popt->name);
2937 exit(1);
2938 }
bellard1d14ffa2005-10-30 18:58:22 +00002939 select_soundhw (optarg);
2940 break;
bellardcd6f1162004-05-13 22:02:20 +00002941 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00002942 help(0);
bellardcd6f1162004-05-13 22:02:20 +00002943 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00002944 case QEMU_OPTION_version:
2945 version();
2946 exit(0);
2947 break;
aurel3200f82b82008-04-27 21:12:55 +00002948 case QEMU_OPTION_m: {
2949 uint64_t value;
2950 char *ptr;
2951
2952 value = strtoul(optarg, &ptr, 10);
2953 switch (*ptr) {
2954 case 0: case 'M': case 'm':
2955 value <<= 20;
2956 break;
2957 case 'G': case 'g':
2958 value <<= 30;
2959 break;
2960 default:
2961 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00002962 exit(1);
2963 }
aurel3200f82b82008-04-27 21:12:55 +00002964
2965 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05002966 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00002967 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
2968 exit(1);
2969 }
Anthony Liguoric227f092009-10-01 16:12:16 -05002970 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00002971 fprintf(stderr, "qemu: ram size too large\n");
2972 exit(1);
2973 }
2974 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00002975 break;
aurel3200f82b82008-04-27 21:12:55 +00002976 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03002977 case QEMU_OPTION_mempath:
2978 mem_path = optarg;
2979 break;
2980#ifdef MAP_POPULATE
2981 case QEMU_OPTION_mem_prealloc:
2982 mem_prealloc = 1;
2983 break;
2984#endif
bellardcd6f1162004-05-13 22:02:20 +00002985 case QEMU_OPTION_d:
Blue Swirlde06f8d2010-03-29 19:23:50 +00002986 set_cpu_log(optarg);
bellardcd6f1162004-05-13 22:02:20 +00002987 break;
bellardcd6f1162004-05-13 22:02:20 +00002988 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00002989 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00002990 break;
aliguori59030a82009-04-05 18:43:41 +00002991 case QEMU_OPTION_gdb:
2992 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002993 break;
bellardcd6f1162004-05-13 22:02:20 +00002994 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01002995 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00002996 break;
j_mayer1192dad2007-10-05 13:08:35 +00002997 case QEMU_OPTION_bios:
2998 bios_name = optarg;
2999 break;
aurel321b530a62009-04-05 20:08:59 +00003000 case QEMU_OPTION_singlestep:
3001 singlestep = 1;
3002 break;
bellardcd6f1162004-05-13 22:02:20 +00003003 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00003004 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00003005 break;
bellard3d11d0e2004-12-12 16:56:30 +00003006 case QEMU_OPTION_k:
3007 keyboard_layout = optarg;
3008 break;
bellardee22c2f2004-06-03 12:49:50 +00003009 case QEMU_OPTION_localtime:
3010 rtc_utc = 0;
3011 break;
malc3893c122008-09-28 00:42:05 +00003012 case QEMU_OPTION_vga:
3013 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00003014 break;
bellarde9b137c2004-06-21 16:46:10 +00003015 case QEMU_OPTION_g:
3016 {
3017 const char *p;
3018 int w, h, depth;
3019 p = optarg;
3020 w = strtol(p, (char **)&p, 10);
3021 if (w <= 0) {
3022 graphic_error:
3023 fprintf(stderr, "qemu: invalid resolution or depth\n");
3024 exit(1);
3025 }
3026 if (*p != 'x')
3027 goto graphic_error;
3028 p++;
3029 h = strtol(p, (char **)&p, 10);
3030 if (h <= 0)
3031 goto graphic_error;
3032 if (*p == 'x') {
3033 p++;
3034 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00003035 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00003036 depth != 24 && depth != 32)
3037 goto graphic_error;
3038 } else if (*p == '\0') {
3039 depth = graphic_depth;
3040 } else {
3041 goto graphic_error;
3042 }
ths3b46e622007-09-17 08:09:54 +00003043
bellarde9b137c2004-06-21 16:46:10 +00003044 graphic_width = w;
3045 graphic_height = h;
3046 graphic_depth = depth;
3047 }
3048 break;
ths20d8a3e2007-02-18 17:04:49 +00003049 case QEMU_OPTION_echr:
3050 {
3051 char *r;
3052 term_escape_char = strtol(optarg, &r, 0);
3053 if (r == optarg)
3054 printf("Bad argument to echr\n");
3055 break;
3056 }
bellard82c643f2004-07-14 17:28:13 +00003057 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003058 monitor_parse(optarg, "readline");
3059 default_monitor = 0;
3060 break;
3061 case QEMU_OPTION_qmp:
3062 monitor_parse(optarg, "control");
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003063 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00003064 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003065 case QEMU_OPTION_mon:
Markus Armbruster8212c642010-02-10 19:52:18 +01003066 opts = qemu_opts_parse(&qemu_mon_opts, optarg, 1);
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003067 if (!opts) {
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003068 exit(1);
3069 }
Anthony Liguori2d114dc2010-03-21 14:14:38 -05003070 default_monitor = 0;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01003071 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003072 case QEMU_OPTION_chardev:
Markus Armbruster8212c642010-02-10 19:52:18 +01003073 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, 1);
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003074 if (!opts) {
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003075 exit(1);
3076 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02003077 break;
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303078#ifdef CONFIG_LINUX
3079 case QEMU_OPTION_fsdev:
3080 opts = qemu_opts_parse(&qemu_fsdev_opts, optarg, 1);
3081 if (!opts) {
3082 fprintf(stderr, "parse error: %s\n", optarg);
3083 exit(1);
3084 }
3085 break;
Gautham R Shenoy3d54abc2010-04-29 17:45:03 +05303086 case QEMU_OPTION_virtfs: {
3087 char *arg_fsdev = NULL;
3088 char *arg_9p = NULL;
3089 int len = 0;
3090
3091 opts = qemu_opts_parse(&qemu_virtfs_opts, optarg, 1);
3092 if (!opts) {
3093 fprintf(stderr, "parse error: %s\n", optarg);
3094 exit(1);
3095 }
3096
3097 len = strlen(",id=,path=");
3098 len += strlen(qemu_opt_get(opts, "fstype"));
3099 len += strlen(qemu_opt_get(opts, "mount_tag"));
3100 len += strlen(qemu_opt_get(opts, "path"));
3101 arg_fsdev = qemu_malloc((len + 1) * sizeof(*arg_fsdev));
3102
3103 if (!arg_fsdev) {
3104 fprintf(stderr, "No memory to parse -fsdev for %s\n",
3105 optarg);
3106 exit(1);
3107 }
3108
3109 sprintf(arg_fsdev, "%s,id=%s,path=%s",
3110 qemu_opt_get(opts, "fstype"),
3111 qemu_opt_get(opts, "mount_tag"),
3112 qemu_opt_get(opts, "path"));
3113
3114 len = strlen("virtio-9p-pci,fsdev=,mount_tag=");
3115 len += 2*strlen(qemu_opt_get(opts, "mount_tag"));
3116 arg_9p = qemu_malloc((len + 1) * sizeof(*arg_9p));
3117
3118 if (!arg_9p) {
3119 fprintf(stderr, "No memory to parse -device for %s\n",
3120 optarg);
3121 exit(1);
3122 }
3123
3124 sprintf(arg_9p, "virtio-9p-pci,fsdev=%s,mount_tag=%s",
3125 qemu_opt_get(opts, "mount_tag"),
3126 qemu_opt_get(opts, "mount_tag"));
3127
3128 if (!qemu_opts_parse(&qemu_fsdev_opts, arg_fsdev, 1)) {
3129 fprintf(stderr, "parse error [fsdev]: %s\n", optarg);
3130 exit(1);
3131 }
3132
3133 if (!qemu_opts_parse(&qemu_device_opts, arg_9p, 1)) {
3134 fprintf(stderr, "parse error [device]: %s\n", optarg);
3135 exit(1);
3136 }
3137
3138 qemu_free(arg_fsdev);
3139 qemu_free(arg_9p);
3140 break;
3141 }
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303142#endif
bellard82c643f2004-07-14 17:28:13 +00003143 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003144 add_device_config(DEV_SERIAL, optarg);
3145 default_serial = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003146 if (strncmp(optarg, "mon:", 4) == 0) {
3147 default_monitor = 0;
3148 }
bellard82c643f2004-07-14 17:28:13 +00003149 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003150 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02003151 if (watchdog) {
3152 fprintf(stderr,
3153 "qemu: only one watchdog option may be given\n");
3154 return 1;
3155 }
3156 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01003157 break;
3158 case QEMU_OPTION_watchdog_action:
3159 if (select_watchdog_action(optarg) == -1) {
3160 fprintf(stderr, "Unknown -watchdog-action parameter\n");
3161 exit(1);
3162 }
3163 break;
aliguori51ecf132009-01-15 20:06:40 +00003164 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003165 add_device_config(DEV_VIRTCON, optarg);
3166 default_virtcon = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003167 if (strncmp(optarg, "mon:", 4) == 0) {
3168 default_monitor = 0;
3169 }
aliguori51ecf132009-01-15 20:06:40 +00003170 break;
bellard6508fe52005-01-15 12:02:56 +00003171 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003172 add_device_config(DEV_PARALLEL, optarg);
3173 default_parallel = 0;
Jan Kiszka18141ed2010-03-07 11:28:40 +01003174 if (strncmp(optarg, "mon:", 4) == 0) {
3175 default_monitor = 0;
3176 }
bellard6508fe52005-01-15 12:02:56 +00003177 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003178 case QEMU_OPTION_debugcon:
3179 add_device_config(DEV_DEBUGCON, optarg);
3180 break;
bellardd63d3072004-10-03 13:29:03 +00003181 case QEMU_OPTION_loadvm:
3182 loadvm = optarg;
3183 break;
3184 case QEMU_OPTION_full_screen:
3185 full_screen = 1;
3186 break;
ths667acca2006-12-11 02:08:05 +00003187#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00003188 case QEMU_OPTION_no_frame:
3189 no_frame = 1;
3190 break;
ths3780e192007-06-21 21:08:02 +00003191 case QEMU_OPTION_alt_grab:
3192 alt_grab = 1;
3193 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05003194 case QEMU_OPTION_ctrl_grab:
3195 ctrl_grab = 1;
3196 break;
ths667acca2006-12-11 02:08:05 +00003197 case QEMU_OPTION_no_quit:
3198 no_quit = 1;
3199 break;
aliguori7d957bd2009-01-15 22:14:11 +00003200 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003201 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00003202 break;
ths667acca2006-12-11 02:08:05 +00003203#endif
bellardf7cce892004-12-08 22:21:25 +00003204 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00003205 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00003206 break;
bellarda09db212005-04-30 16:10:35 +00003207 case QEMU_OPTION_win2k_hack:
3208 win2k_install_hack = 1;
3209 break;
aliguori73822ec2009-01-15 20:11:34 +00003210 case QEMU_OPTION_rtc_td_hack:
3211 rtc_td_hack = 1;
3212 break;
aliguori8a92ea22009-02-27 20:12:36 +00003213 case QEMU_OPTION_acpitable:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003214 do_acpitable_option(optarg);
aliguori8a92ea22009-02-27 20:12:36 +00003215 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00003216 case QEMU_OPTION_smbios:
Blue Swirlde06f8d2010-03-29 19:23:50 +00003217 do_smbios_option(optarg);
aliguorib6f6e3d2009-04-17 18:59:56 +00003218 break;
aliguori7ba1e612008-11-05 16:04:33 +00003219 case QEMU_OPTION_enable_kvm:
3220 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00003221 break;
bellardbb36d472005-11-05 14:22:28 +00003222 case QEMU_OPTION_usb:
3223 usb_enabled = 1;
3224 break;
bellarda594cfb2005-11-06 16:13:29 +00003225 case QEMU_OPTION_usbdevice:
3226 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003227 add_device_config(DEV_USB, optarg);
3228 break;
3229 case QEMU_OPTION_device:
Markus Armbruster8212c642010-02-10 19:52:18 +01003230 if (!qemu_opts_parse(&qemu_device_opts, optarg, 1)) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003231 exit(1);
3232 }
bellarda594cfb2005-11-06 16:13:29 +00003233 break;
bellard6a00d602005-11-21 23:25:50 +00003234 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003235 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00003236 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00003237 fprintf(stderr, "Invalid number of CPUs\n");
3238 exit(1);
3239 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02003240 if (max_cpus < smp_cpus) {
3241 fprintf(stderr, "maxcpus must be equal to or greater than "
3242 "smp\n");
3243 exit(1);
3244 }
3245 if (max_cpus > 255) {
3246 fprintf(stderr, "Unsupported number of maxcpus\n");
3247 exit(1);
3248 }
bellard6a00d602005-11-21 23:25:50 +00003249 break;
bellard24236862006-04-30 21:28:36 +00003250 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003251 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00003252 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00003253 break;
bellard6515b202006-05-03 22:02:44 +00003254 case QEMU_OPTION_no_acpi:
3255 acpi_enabled = 0;
3256 break;
aliguori16b29ae2008-12-17 23:28:44 +00003257 case QEMU_OPTION_no_hpet:
3258 no_hpet = 1;
3259 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02003260 case QEMU_OPTION_balloon:
3261 if (balloon_parse(optarg) < 0) {
3262 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
3263 exit(1);
3264 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03003265 break;
bellardd1beab82006-10-02 19:44:22 +00003266 case QEMU_OPTION_no_reboot:
3267 no_reboot = 1;
3268 break;
aurel32b2f76162008-04-11 21:35:52 +00003269 case QEMU_OPTION_no_shutdown:
3270 no_shutdown = 1;
3271 break;
balrog9467cd42007-05-01 01:34:14 +00003272 case QEMU_OPTION_show_cursor:
3273 cursor_hide = 0;
3274 break;
blueswir18fcb1b92008-09-18 18:29:08 +00003275 case QEMU_OPTION_uuid:
3276 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
3277 fprintf(stderr, "Fail to parse UUID string."
3278 " Wrong format.\n");
3279 exit(1);
3280 }
3281 break;
blueswir15824d652009-03-28 06:44:27 +00003282#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003283 case QEMU_OPTION_daemonize:
3284 daemonize = 1;
3285 break;
blueswir15824d652009-03-28 06:44:27 +00003286#endif
ths9ae02552007-01-05 17:39:04 +00003287 case QEMU_OPTION_option_rom:
3288 if (nb_option_roms >= MAX_OPTION_ROMS) {
3289 fprintf(stderr, "Too many option ROMs\n");
3290 exit(1);
3291 }
3292 option_rom[nb_option_roms] = optarg;
3293 nb_option_roms++;
3294 break;
pbrook8e716212007-01-20 17:12:09 +00003295 case QEMU_OPTION_semihosting:
3296 semihosting_enabled = 1;
3297 break;
thsc35734b2007-03-19 15:17:08 +00003298 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02003299 qemu_name = qemu_strdup(optarg);
3300 {
3301 char *p = strchr(qemu_name, ',');
3302 if (p != NULL) {
3303 *p++ = 0;
3304 if (strncmp(p, "process=", 8)) {
3305 fprintf(stderr, "Unknown subargument %s to -name", p);
3306 exit(1);
3307 }
3308 p += 8;
3309 set_proc_name(p);
3310 }
3311 }
thsc35734b2007-03-19 15:17:08 +00003312 break;
blueswir166508602007-05-01 14:16:52 +00003313 case QEMU_OPTION_prom_env:
3314 if (nb_prom_envs >= MAX_PROM_ENVS) {
3315 fprintf(stderr, "Too many prom variables\n");
3316 exit(1);
3317 }
3318 prom_envs[nb_prom_envs] = optarg;
3319 nb_prom_envs++;
3320 break;
balrog2b8f2d42007-07-27 22:08:46 +00003321 case QEMU_OPTION_old_param:
3322 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00003323 break;
thsf3dcfad2007-08-24 01:26:02 +00003324 case QEMU_OPTION_clock:
3325 configure_alarms(optarg);
3326 break;
bellard7e0af5d02007-11-07 16:24:33 +00003327 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003328 configure_rtc_date_offset(optarg, 1);
3329 break;
3330 case QEMU_OPTION_rtc:
Markus Armbruster8212c642010-02-10 19:52:18 +01003331 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, 0);
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003332 if (!opts) {
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003333 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00003334 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02003335 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00003336 break;
bellard26a5f132008-05-28 12:30:31 +00003337 case QEMU_OPTION_tb_size:
3338 tb_size = strtol(optarg, NULL, 0);
3339 if (tb_size < 0)
3340 tb_size = 0;
3341 break;
pbrook2e70f6e2008-06-29 01:03:05 +00003342 case QEMU_OPTION_icount:
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003343 icount_option = optarg;
pbrook2e70f6e2008-06-29 01:03:05 +00003344 break;
aliguori5bb79102008-10-13 03:12:02 +00003345 case QEMU_OPTION_incoming:
3346 incoming = optarg;
3347 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003348 case QEMU_OPTION_nodefaults:
3349 default_serial = 0;
3350 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003351 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003352 default_monitor = 0;
3353 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01003354 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003355 default_floppy = 0;
3356 default_cdrom = 0;
3357 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01003358 break;
blueswir15824d652009-03-28 06:44:27 +00003359#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00003360 case QEMU_OPTION_chroot:
3361 chroot_dir = optarg;
3362 break;
3363 case QEMU_OPTION_runas:
3364 run_as = optarg;
3365 break;
blueswir15824d652009-03-28 06:44:27 +00003366#endif
aliguorie37630c2009-04-22 15:19:10 +00003367 case QEMU_OPTION_xen_domid:
Blue Swirlad960902010-03-29 19:23:52 +00003368 if (!(xen_available())) {
3369 printf("Option %s not supported for this target\n", popt->name);
3370 exit(1);
3371 }
aliguorie37630c2009-04-22 15:19:10 +00003372 xen_domid = atoi(optarg);
3373 break;
3374 case QEMU_OPTION_xen_create:
Blue Swirlad960902010-03-29 19:23:52 +00003375 if (!(xen_available())) {
3376 printf("Option %s not supported for this target\n", popt->name);
3377 exit(1);
3378 }
aliguorie37630c2009-04-22 15:19:10 +00003379 xen_mode = XEN_CREATE;
3380 break;
3381 case QEMU_OPTION_xen_attach:
Blue Swirlad960902010-03-29 19:23:52 +00003382 if (!(xen_available())) {
3383 printf("Option %s not supported for this target\n", popt->name);
3384 exit(1);
3385 }
aliguorie37630c2009-04-22 15:19:10 +00003386 xen_mode = XEN_ATTACH;
3387 break;
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003388 case QEMU_OPTION_readconfig:
3389 {
Kevin Wolfdcfb0932010-03-05 17:25:55 +01003390 int ret = qemu_read_config_file(optarg);
3391 if (ret < 0) {
3392 fprintf(stderr, "read config %s: %s\n", optarg,
3393 strerror(-ret));
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003394 exit(1);
3395 }
Gerd Hoffmann715a6642009-10-14 10:39:28 +02003396 break;
3397 }
3398 case QEMU_OPTION_writeconfig:
3399 {
3400 FILE *fp;
3401 if (strcmp(optarg, "-") == 0) {
3402 fp = stdout;
3403 } else {
3404 fp = fopen(optarg, "w");
3405 if (fp == NULL) {
3406 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
3407 exit(1);
3408 }
3409 }
3410 qemu_config_write(fp);
3411 fclose(fp);
3412 break;
3413 }
bellardcd6f1162004-05-13 22:02:20 +00003414 }
bellard0824d6f2003-06-24 13:42:40 +00003415 }
3416 }
Markus Armbruster0f0bc3f2010-02-18 20:13:51 +01003417 loc_set_none();
bellard330d0412003-07-26 18:11:40 +00003418
Paul Brook5cea8592009-05-30 00:52:44 +01003419 /* If no data_dir is specified then try to find it relative to the
3420 executable path. */
3421 if (!data_dir) {
3422 data_dir = find_datadir(argv[0]);
3423 }
3424 /* If all else fails use the install patch specified when building. */
3425 if (!data_dir) {
Paolo Bonzini1dabe052010-05-26 16:08:25 +02003426 data_dir = CONFIG_QEMU_DATADIR;
Paul Brook5cea8592009-05-30 00:52:44 +01003427 }
3428
Jes Sorensen6be68d72009-07-23 17:03:42 +02003429 /*
3430 * Default to max_cpus = smp_cpus, in case the user doesn't
3431 * specify a max_cpus value.
3432 */
3433 if (!max_cpus)
3434 max_cpus = smp_cpus;
3435
balrog3d878ca2008-10-28 10:59:59 +00003436 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00003437 if (smp_cpus > machine->max_cpus) {
3438 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
3439 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
3440 machine->max_cpus);
3441 exit(1);
3442 }
3443
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003444 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01003445 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003446
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003447 if (machine->no_serial) {
3448 default_serial = 0;
3449 }
3450 if (machine->no_parallel) {
3451 default_parallel = 0;
3452 }
3453 if (!machine->use_virtcon) {
3454 default_virtcon = 0;
3455 }
3456 if (machine->no_vga) {
3457 default_vga = 0;
3458 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003459 if (machine->no_floppy) {
3460 default_floppy = 0;
3461 }
3462 if (machine->no_cdrom) {
3463 default_cdrom = 0;
3464 }
3465 if (machine->no_sdcard) {
3466 default_sdcard = 0;
3467 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003468
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003469 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003470 if (default_parallel)
3471 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003472 if (default_serial && default_monitor) {
3473 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003474 } else if (default_virtcon && default_monitor) {
3475 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003476 } else {
3477 if (default_serial)
3478 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01003479 if (default_virtcon)
3480 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003481 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003482 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01003483 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003484 } else {
3485 if (default_serial)
3486 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003487 if (default_parallel)
3488 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01003489 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01003490 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01003491 if (default_virtcon)
3492 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00003493 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01003494 if (default_vga)
3495 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00003496
TeLeMana5829fd2010-04-15 12:37:55 +08003497 socket_init();
3498
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003499 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
3500 exit(1);
Gautham R Shenoy74db9202010-04-29 17:44:43 +05303501#ifdef CONFIG_LINUX
3502 if (qemu_opts_foreach(&qemu_fsdev_opts, fsdev_init_func, NULL, 1) != 0) {
3503 exit(1);
3504 }
3505#endif
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01003506
ths71e3ceb2006-12-22 02:11:31 +00003507#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003508 if (daemonize) {
3509 pid_t pid;
3510
3511 if (pipe(fds) == -1)
3512 exit(1);
3513
3514 pid = fork();
3515 if (pid > 0) {
3516 uint8_t status;
3517 ssize_t len;
3518
3519 close(fds[1]);
3520
3521 again:
ths93815bc2007-03-19 15:58:31 +00003522 len = read(fds[0], &status, 1);
3523 if (len == -1 && (errno == EINTR))
3524 goto again;
3525
3526 if (len != 1)
3527 exit(1);
3528 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05003529 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00003530 exit(1);
3531 } else
3532 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00003533 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00003534 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00003535
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003536 close(fds[0]);
3537 qemu_set_cloexec(fds[1]);
3538
ths71e3ceb2006-12-22 02:11:31 +00003539 setsid();
3540
3541 pid = fork();
3542 if (pid > 0)
3543 exit(0);
3544 else if (pid < 0)
3545 exit(1);
3546
3547 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00003548
3549 signal(SIGTSTP, SIG_IGN);
3550 signal(SIGTTOU, SIG_IGN);
3551 signal(SIGTTIN, SIG_IGN);
3552 }
Juha Riihimäki099fe232009-12-03 15:56:03 +02003553#endif
ths71e3ceb2006-12-22 02:11:31 +00003554
thsaa26bb22007-03-25 21:33:06 +00003555 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Juha Riihimäki099fe232009-12-03 15:56:03 +02003556#ifndef _WIN32
ths93815bc2007-03-19 15:58:31 +00003557 if (daemonize) {
3558 uint8_t status = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003559 if (write(fds[1], &status, 1) != 1) {
3560 perror("daemonize. Writing to pipe\n");
3561 }
ths93815bc2007-03-19 15:58:31 +00003562 } else
Juha Riihimäki099fe232009-12-03 15:56:03 +02003563#endif
Justin M. Forbes850810d2009-10-01 09:42:56 -05003564 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00003565 exit(1);
3566 }
3567
Paolo Bonzini98c85732010-04-19 18:59:30 +00003568 if (kvm_allowed) {
3569 int ret = kvm_init(smp_cpus);
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003570 if (ret < 0) {
Paolo Bonzini98c85732010-04-19 18:59:30 +00003571 if (!kvm_available()) {
3572 printf("KVM not supported for this target\n");
3573 } else {
3574 fprintf(stderr, "failed to initialize KVM: %s\n", strerror(-ret));
3575 }
Marcelo Tosatti214910a2009-09-18 02:41:23 -03003576 exit(1);
3577 }
3578 }
3579
aliguori3fcf7b62009-04-24 18:03:25 +00003580 if (qemu_init_main_loop()) {
3581 fprintf(stderr, "qemu_init_main_loop failed\n");
3582 exit(1);
3583 }
bellarda20dd502003-09-30 21:07:02 +00003584 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00003585
thsf8d39c02008-07-03 10:01:15 +00003586 if (!linux_boot && *kernel_cmdline != '\0') {
3587 fprintf(stderr, "-append only allowed with -kernel option\n");
3588 exit(1);
3589 }
3590
3591 if (!linux_boot && initrd_filename != NULL) {
3592 fprintf(stderr, "-initrd only allowed with -kernel option\n");
3593 exit(1);
3594 }
3595
Filip Navarabf65f532009-07-27 10:02:04 -05003596#ifndef _WIN32
3597 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00003598 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05003599#endif
ths3b46e622007-09-17 08:09:54 +00003600
aliguori7183b4b2008-11-05 20:40:18 +00003601 if (init_timer_alarm() < 0) {
3602 fprintf(stderr, "could not initialize alarm timer\n");
3603 exit(1);
3604 }
Paolo Bonzini4e3de9e2010-03-10 11:38:48 +01003605 configure_icount(icount_option);
pbrook634fce92006-07-15 17:40:09 +00003606
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01003607 if (net_init_clients() < 0) {
3608 exit(1);
bellard702c6512004-04-02 21:21:32 +00003609 }
bellardf1510b22003-06-25 00:07:40 +00003610
balrogdc72ac12008-11-09 00:04:26 +00003611 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003612 if (foreach_device_config(DEV_BT, bt_parse))
3613 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00003614
bellard0824d6f2003-06-24 13:42:40 +00003615 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00003616 if (ram_size == 0)
3617 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00003618
bellard26a5f132008-05-28 12:30:31 +00003619 /* init the dynamic translator */
3620 cpu_exec_init_all(tb_size * 1024 * 1024);
3621
Markus Armbrustereb852012009-10-27 18:41:44 +01003622 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00003623
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003624 blk_mig_init();
3625
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003626 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003627 /* we always create the cdrom drive, even if no disk is there */
3628 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003629 }
thse4bcb142007-12-02 04:51:10 +00003630
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003631 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003632 /* we always create at least one floppy */
3633 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003634 }
bellardc4b1fcc2004-03-14 21:44:30 +00003635
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01003636 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01003637 /* we always create one sd slot, even if no card is in it */
3638 drive_add(NULL, SD_ALIAS);
3639 }
balrog9d413d12007-12-04 00:10:34 +00003640
ths96d30e42007-01-07 20:42:14 +00003641 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003642 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02003643 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
3644 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02003645 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00003646
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02003647 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
3648 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00003649
aliguori268a3622009-04-21 22:30:27 +00003650 if (nb_numa_nodes > 0) {
3651 int i;
3652
3653 if (nb_numa_nodes > smp_cpus) {
3654 nb_numa_nodes = smp_cpus;
3655 }
3656
3657 /* If no memory size if given for any node, assume the default case
3658 * and distribute the available memory equally across all nodes
3659 */
3660 for (i = 0; i < nb_numa_nodes; i++) {
3661 if (node_mem[i] != 0)
3662 break;
3663 }
3664 if (i == nb_numa_nodes) {
3665 uint64_t usedmem = 0;
3666
3667 /* On Linux, the each node's border has to be 8MB aligned,
3668 * the final node gets the rest.
3669 */
3670 for (i = 0; i < nb_numa_nodes - 1; i++) {
3671 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
3672 usedmem += node_mem[i];
3673 }
3674 node_mem[i] = ram_size - usedmem;
3675 }
3676
3677 for (i = 0; i < nb_numa_nodes; i++) {
3678 if (node_cpumask[i] != 0)
3679 break;
3680 }
3681 /* assigning the VCPUs round-robin is easier to implement, guest OSes
3682 * must cope with this anyway, because there are BIOSes out there in
3683 * real machines which also use this scheme.
3684 */
3685 if (i == nb_numa_nodes) {
3686 for (i = 0; i < smp_cpus; i++) {
3687 node_cpumask[i % nb_numa_nodes] |= 1 << i;
3688 }
3689 }
3690 }
3691
Jan Kiszka157b9312010-04-06 16:55:53 +02003692 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0) {
3693 exit(1);
3694 }
3695
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01003696 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
3697 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01003698 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
3699 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01003700 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
3701 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08003702 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
3703 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00003704
Paul Brookaae94602009-05-14 22:35:06 +01003705 module_call_init(MODULE_INIT_DEVICE);
3706
Markus Armbrusterff952ba2010-01-29 19:48:57 +01003707 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
3708 exit(0);
3709
Markus Armbruster09aaa162009-08-21 10:31:34 +02003710 if (watchdog) {
3711 i = select_watchdog(watchdog);
3712 if (i > 0)
3713 exit (i == 1 ? 1 : 0);
3714 }
3715
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003716 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01003717 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02003718 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01003719 qemu_add_globals();
3720
Paul Brookfbe1b592009-05-13 17:56:25 +01003721 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00003722 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
3723
Jan Kiszkaea375f92010-03-01 19:10:30 +01003724 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00003725
Juan Quintela67b3b712009-08-28 19:25:15 +02003726#ifndef _WIN32
3727 /* must be after terminal init, SDL library changes signal handlers */
3728 sighandler_setup();
3729#endif
3730
Blue Swirl87d0a282010-03-27 18:24:45 +00003731 set_numa_modes();
aliguori268a3622009-04-21 22:30:27 +00003732
aliguori6f338c32009-02-11 15:21:54 +00003733 current_machine = machine;
3734
aliguori3023f332009-01-16 19:04:14 +00003735 /* init USB devices */
3736 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01003737 if (foreach_device_config(DEV_USB, usb_parse) < 0)
3738 exit(1);
aliguori3023f332009-01-16 19:04:14 +00003739 }
3740
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003741 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02003742 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02003743 exit(1);
3744
Markus Armbruster668680f2010-02-11 14:44:58 +01003745 net_check_clients();
3746
aliguori3023f332009-01-16 19:04:14 +00003747 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01003748 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003749
3750 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003751#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003752 display_type = DT_SDL;
3753#else
3754 display_type = DT_VNC;
3755 vnc_display = "localhost:0,to=99";
3756 show_vnc_port = 1;
3757#endif
3758 }
3759
3760
3761 switch (display_type) {
3762 case DT_NOGRAPHIC:
3763 break;
3764#if defined(CONFIG_CURSES)
3765 case DT_CURSES:
3766 curses_display_init(ds, full_screen);
3767 break;
3768#endif
bellard5b0753e2005-03-01 21:37:28 +00003769#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003770 case DT_SDL:
3771 sdl_display_init(ds, full_screen, no_frame);
3772 break;
bellard5b0753e2005-03-01 21:37:28 +00003773#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003774 case DT_SDL:
3775 cocoa_display_init(ds, full_screen);
3776 break;
bellard313aa562003-08-10 21:52:11 +00003777#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003778 case DT_VNC:
3779 vnc_display_init(ds);
3780 if (vnc_display_open(ds, vnc_display) < 0)
3781 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003782
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003783 if (show_vnc_port) {
3784 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05003785 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003786 break;
3787 default:
3788 break;
bellard313aa562003-08-10 21:52:11 +00003789 }
aliguori7d957bd2009-01-15 22:14:11 +00003790 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00003791
aliguori3023f332009-01-16 19:04:14 +00003792 dcl = ds->listeners;
3793 while (dcl != NULL) {
3794 if (dcl->dpy_refresh != NULL) {
3795 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
3796 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
Isaku Yamahata0f2ad632010-05-27 14:38:47 +09003797 break;
ths20d8a3e2007-02-18 17:04:49 +00003798 }
aliguori3023f332009-01-16 19:04:14 +00003799 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00003800 }
aliguori3023f332009-01-16 19:04:14 +00003801
Anthony Liguori993fbfd2009-05-21 16:54:00 -05003802 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00003803 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
3804 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
3805 }
3806
Paolo Bonzinib473df62010-02-11 00:29:55 +01003807 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00003808
aliguori59030a82009-04-05 18:43:41 +00003809 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
3810 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
3811 gdbstub_dev);
3812 exit(1);
balrog45669e02007-07-02 13:20:17 +00003813 }
balrog45669e02007-07-02 13:20:17 +00003814
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02003815 qdev_machine_creation_done();
3816
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01003817 if (rom_load_all() != 0) {
3818 fprintf(stderr, "rom loading failed\n");
3819 exit(1);
3820 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02003821
Juan Quintela504c2942009-11-12 00:39:13 +01003822 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02003823 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01003824 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02003825 autostart = 0;
3826 }
3827 }
bellardd63d3072004-10-03 13:29:03 +00003828
Glauber Costa2bb8c102009-07-24 16:20:23 -04003829 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00003830 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03003831 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00003832 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03003833 }
thsffd843b2006-12-21 19:46:43 +00003834
blueswir1b9e82a52009-04-05 18:03:31 +00003835#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00003836 if (daemonize) {
3837 uint8_t status = 0;
3838 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00003839
3840 again1:
3841 len = write(fds[1], &status, 1);
3842 if (len == -1 && (errno == EINTR))
3843 goto again1;
3844
3845 if (len != 1)
3846 exit(1);
3847
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003848 if (chdir("/")) {
3849 perror("not able to chdir to /");
3850 exit(1);
3851 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01003852 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00003853 if (fd == -1)
3854 exit(1);
aliguori08585322009-02-27 22:09:45 +00003855 }
ths71e3ceb2006-12-22 02:11:31 +00003856
aliguori08585322009-02-27 22:09:45 +00003857 if (run_as) {
3858 pwd = getpwnam(run_as);
3859 if (!pwd) {
3860 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
3861 exit(1);
3862 }
3863 }
ths71e3ceb2006-12-22 02:11:31 +00003864
aliguori08585322009-02-27 22:09:45 +00003865 if (chroot_dir) {
3866 if (chroot(chroot_dir) < 0) {
3867 fprintf(stderr, "chroot failed\n");
3868 exit(1);
3869 }
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003870 if (chdir("/")) {
3871 perror("not able to chdir to /");
3872 exit(1);
3873 }
aliguori08585322009-02-27 22:09:45 +00003874 }
3875
3876 if (run_as) {
3877 if (setgid(pwd->pw_gid) < 0) {
3878 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
3879 exit(1);
3880 }
3881 if (setuid(pwd->pw_uid) < 0) {
3882 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
3883 exit(1);
3884 }
3885 if (setuid(0) != -1) {
3886 fprintf(stderr, "Dropping privileges failed\n");
3887 exit(1);
3888 }
3889 }
aliguori08585322009-02-27 22:09:45 +00003890
3891 if (daemonize) {
3892 dup2(fd, 0);
3893 dup2(fd, 1);
3894 dup2(fd, 2);
3895
3896 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00003897 }
blueswir1b9e82a52009-04-05 18:03:31 +00003898#endif
ths71e3ceb2006-12-22 02:11:31 +00003899
bellard8a7ddc32004-03-31 19:00:16 +00003900 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00003901 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00003902 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00003903
bellard0824d6f2003-06-24 13:42:40 +00003904 return 0;
3905}