blob: 0030d5493fc603f960bd4c36892f41e06705786f [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>
blueswir124646c72008-11-07 16:55:48 +000055#else
56#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000057#endif
Aurelien Jarnobbe813a2009-11-30 15:42:59 +010058#else
blueswir1223f0d72008-09-30 18:12:18 +000059#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000060#include <pty.h>
61#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000062#include <linux/rtc.h>
Andi Kleen18894652009-07-02 09:34:17 +020063#include <sys/prctl.h>
thsbd494f42007-09-16 20:03:23 +000064
65/* For the benefit of older linux systems which don't supply it,
66 we use a local copy of hpet.h. */
67/* #include <linux/hpet.h> */
68#include "hpet.h"
69
bellarde57a8c02005-11-10 23:58:52 +000070#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000071#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000072#endif
73#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000074#include <sys/stat.h>
75#include <sys/ethernet.h>
76#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000077#include <netinet/arp.h>
78#include <netinet/in.h>
79#include <netinet/in_systm.h>
80#include <netinet/ip.h>
81#include <netinet/ip_icmp.h> // must come after ip.h
82#include <netinet/udp.h>
83#include <netinet/tcp.h>
84#include <net/if.h>
85#include <syslog.h>
86#include <stropts.h>
Blue Swirl8d32cf02009-10-02 19:32:12 +000087/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
88 discussion about Solaris header problems */
89extern int madvise(caddr_t, size_t, int);
bellard67b915a2004-03-31 23:37:16 +000090#endif
bellard7d3505c2004-05-12 19:32:15 +000091#endif
bellardec530c82006-04-25 22:36:06 +000092#endif
bellard67b915a2004-03-31 23:37:16 +000093
blueswir19892fbf2008-08-24 10:34:20 +000094#if defined(__OpenBSD__)
95#include <util.h>
96#endif
97
ths8a16d272008-07-19 09:56:24 +000098#if defined(CONFIG_VDE)
99#include <libvdeplug.h>
100#endif
101
bellard67b915a2004-03-31 23:37:16 +0000102#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +0000103#include <windows.h>
ths4fddf622007-12-17 04:42:29 +0000104#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000105#endif
106
bellard73332e52004-04-04 20:22:28 +0000107#ifdef CONFIG_SDL
Stefan Weil59a36a22009-06-18 20:11:03 +0200108#if defined(__APPLE__) || defined(main)
Stefan Weil66936652009-06-13 13:19:11 +0200109#include <SDL.h>
malc880fec52009-02-15 20:18:41 +0000110int qemu_main(int argc, char **argv, char **envp);
111int main(int argc, char **argv)
112{
Stefan Weil59a36a22009-06-18 20:11:03 +0200113 return qemu_main(argc, argv, NULL);
malc880fec52009-02-15 20:18:41 +0000114}
115#undef main
116#define main qemu_main
bellard96bcd4f2004-07-10 16:26:15 +0000117#endif
bellard73332e52004-04-04 20:22:28 +0000118#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000119
bellard5b0753e2005-03-01 21:37:28 +0000120#ifdef CONFIG_COCOA
121#undef main
122#define main qemu_main
123#endif /* CONFIG_COCOA */
124
blueswir1511d2b12009-03-07 15:32:56 +0000125#include "hw/hw.h"
126#include "hw/boards.h"
127#include "hw/usb.h"
128#include "hw/pcmcia.h"
129#include "hw/pc.h"
130#include "hw/audiodev.h"
131#include "hw/isa.h"
132#include "hw/baum.h"
133#include "hw/bt.h"
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +0100134#include "hw/watchdog.h"
aliguorib6f6e3d2009-04-17 18:59:56 +0000135#include "hw/smbios.h"
aliguorie37630c2009-04-22 15:19:10 +0000136#include "hw/xen.h"
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +0200137#include "hw/qdev.h"
Gerd Hoffmann45a50b12009-10-01 16:42:33 +0200138#include "hw/loader.h"
aurel325ef4efa2009-03-10 21:43:35 +0000139#include "bt-host.h"
blueswir1511d2b12009-03-07 15:32:56 +0000140#include "net.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000141#include "net/slirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000142#include "monitor.h"
143#include "console.h"
144#include "sysemu.h"
145#include "gdbstub.h"
146#include "qemu-timer.h"
147#include "qemu-char.h"
148#include "cache-utils.h"
149#include "block.h"
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200150#include "block_int.h"
151#include "block-migration.h"
blueswir1a718ace2009-03-28 08:24:44 +0000152#include "dma.h"
blueswir1511d2b12009-03-07 15:32:56 +0000153#include "audio/audio.h"
154#include "migration.h"
155#include "kvm.h"
156#include "balloon.h"
Kevin Wolfd3f24362009-05-18 16:42:09 +0200157#include "qemu-option.h"
Gerd Hoffmann7282a032009-07-31 12:25:35 +0200158#include "qemu-config.h"
Luiz Capitulinoe78c48e2009-12-10 17:16:04 -0200159#include "qemu-objects.h"
blueswir1511d2b12009-03-07 15:32:56 +0000160
bellard0824d6f2003-06-24 13:42:40 +0000161#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000162
bellard8a7ddc32004-03-31 19:00:16 +0000163#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000164
blueswir1511d2b12009-03-07 15:32:56 +0000165#include "qemu_socket.h"
166
Jan Kiszkad918f232009-06-24 14:42:30 +0200167#include "slirp/libslirp.h"
blueswir1511d2b12009-03-07 15:32:56 +0000168
Blue Swirl72cf2d42009-09-12 07:36:22 +0000169#include "qemu-queue.h"
170
blueswir19dc63a12008-10-04 07:25:46 +0000171//#define DEBUG_NET
172//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000173
bellard1bfe8562004-07-08 21:17:50 +0000174#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000175
Amit Shah98b19252010-01-20 00:36:52 +0530176#define MAX_VIRTIO_CONSOLES 1
177
Paul Brook5cea8592009-05-30 00:52:44 +0100178static const char *data_dir;
j_mayer1192dad2007-10-05 13:08:35 +0000179const char *bios_name = NULL;
thse4bcb142007-12-02 04:51:10 +0000180/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
bellardfaea38e2006-08-05 21:31:00 +0000181 to store the VM snapshots */
Blue Swirl72cf2d42009-09-12 07:36:22 +0000182struct drivelist drives = QTAILQ_HEAD_INITIALIZER(drives);
183struct driveoptlist driveopts = QTAILQ_HEAD_INITIALIZER(driveopts);
malccb5a7aa2008-09-28 00:42:12 +0000184enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
Anthony Liguori993fbfd2009-05-21 16:54:00 -0500185DisplayType display_type = DT_DEFAULT;
bellard3d11d0e2004-12-12 16:56:30 +0000186const char* keyboard_layout = NULL;
Anthony Liguoric227f092009-10-01 16:12:16 -0500187ram_addr_t ram_size;
Marcelo Tosattic9027602010-03-01 20:25:08 -0300188const char *mem_path = NULL;
189#ifdef MAP_POPULATE
190int mem_prealloc = 0; /* force preallocation of physical target memory */
191#endif
bellardc4b1fcc2004-03-14 21:44:30 +0000192int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000193NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000194int vm_running;
Paolo Bonzinid399f672009-07-27 23:17:51 +0200195int autostart;
balrogf6503052008-02-17 11:42:19 +0000196static int rtc_utc = 1;
197static int rtc_date_offset = -1; /* -1 means no change */
Jan Kiszka68752042009-09-15 13:36:04 +0200198QEMUClock *rtc_clock;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100199int vga_interface_type = VGA_NONE;
bellardd8272202005-04-06 20:32:23 +0000200#ifdef TARGET_SPARC
201int graphic_width = 1024;
202int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000203int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000204#else
bellard1bfe8562004-07-08 21:17:50 +0000205int graphic_width = 800;
206int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000207int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000208#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000209static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000210#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000211static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000212#endif
ths667acca2006-12-11 02:08:05 +0000213int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000214CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000215CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000216CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000217#ifdef TARGET_I386
218int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000219int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000220#endif
bellardbb36d472005-11-05 14:22:28 +0000221int usb_enabled = 0;
aurel321b530a62009-04-05 20:08:59 +0000222int singlestep = 0;
bellard6a00d602005-11-21 23:25:50 +0000223int smp_cpus = 1;
Jes Sorensen6be68d72009-07-23 17:03:42 +0200224int max_cpus = 0;
Andre Przywaradc6b1c02009-08-19 15:42:40 +0200225int smp_cores = 1;
226int smp_threads = 1;
ths73fc9742006-12-22 02:09:07 +0000227const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000228int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000229int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000230int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000231int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000232int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000233int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000234int graphic_rotate = 0;
Jes Sorensen6b35e7b2009-08-06 16:25:50 +0200235uint8_t irq0override = 1;
blueswir1b9e82a52009-04-05 18:03:31 +0000236#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +0000237int daemonize = 0;
blueswir1b9e82a52009-04-05 18:03:31 +0000238#endif
Markus Armbruster09aaa162009-08-21 10:31:34 +0200239const char *watchdog;
ths9ae02552007-01-05 17:39:04 +0000240const char *option_rom[MAX_OPTION_ROMS];
241int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000242int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000243#ifdef TARGET_ARM
244int old_param = 0;
245#endif
thsc35734b2007-03-19 15:17:08 +0000246const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000247int alt_grab = 0;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -0500248int ctrl_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000249#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000250unsigned int nb_prom_envs = 0;
251const char *prom_envs[MAX_PROM_ENVS];
252#endif
Jan Kiszka95387492009-07-02 00:19:02 +0200253int boot_menu;
bellard0824d6f2003-06-24 13:42:40 +0000254
aliguori268a3622009-04-21 22:30:27 +0000255int nb_numa_nodes;
256uint64_t node_mem[MAX_NODES];
257uint64_t node_cpumask[MAX_NODES];
258
balrogee5605e2007-12-03 03:01:40 +0000259static CPUState *cur_cpu;
260static CPUState *next_cpu;
aliguori43b96852009-04-24 18:03:33 +0000261static int timer_alarm_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000262/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000263static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000264/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000265#define MAX_ICOUNT_SHIFT 10
266/* Compensate for varying guest execution speed. */
267static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000268static QEMUTimer *icount_rt_timer;
269static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000270static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000271
blueswir18fcb1b92008-09-18 18:29:08 +0000272uint8_t qemu_uuid[16];
273
Jan Kiszka76e30d02009-07-02 00:19:02 +0200274static QEMUBootSetHandler *boot_set_handler;
275static void *boot_set_opaque;
276
Marcelo Tosatticc84de92010-02-17 20:14:42 -0200277#ifdef SIGRTMIN
278#define SIG_IPI (SIGRTMIN+4)
279#else
280#define SIG_IPI SIGUSR1
281#endif
282
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100283static int default_serial = 1;
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100284static int default_parallel = 1;
Gerd Hoffmann986c5f72009-12-08 13:11:54 +0100285static int default_virtcon = 1;
Gerd Hoffmannabdeed02009-12-08 13:11:43 +0100286static int default_monitor = 1;
Gerd Hoffmann64465292009-12-08 13:11:45 +0100287static int default_vga = 1;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +0100288static int default_floppy = 1;
289static int default_cdrom = 1;
290static int default_sdcard = 1;
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100291
292static struct {
293 const char *driver;
294 int *flag;
295} default_list[] = {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +0100296 { .driver = "isa-serial", .flag = &default_serial },
297 { .driver = "isa-parallel", .flag = &default_parallel },
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +0100298 { .driver = "isa-fdc", .flag = &default_floppy },
299 { .driver = "ide-drive", .flag = &default_cdrom },
Amit Shah392ecf52010-01-21 16:19:23 +0530300 { .driver = "virtio-serial-pci", .flag = &default_virtcon },
301 { .driver = "virtio-serial-s390", .flag = &default_virtcon },
302 { .driver = "virtio-serial", .flag = &default_virtcon },
Gerd Hoffmann64465292009-12-08 13:11:45 +0100303 { .driver = "VGA", .flag = &default_vga },
Gerd Hoffmann69fd02e2009-12-16 13:35:19 +0100304 { .driver = "cirrus-vga", .flag = &default_vga },
305 { .driver = "vmware-svga", .flag = &default_vga },
Gerd Hoffmann998bbd72009-12-08 13:11:41 +0100306};
307
308static int default_driver_check(QemuOpts *opts, void *opaque)
309{
310 const char *driver = qemu_opt_get(opts, "driver");
311 int i;
312
313 if (!driver)
314 return 0;
315 for (i = 0; i < ARRAY_SIZE(default_list); i++) {
316 if (strcmp(default_list[i].driver, driver) != 0)
317 continue;
318 *(default_list[i].flag) = 0;
319 }
320 return 0;
321}
322
bellard0824d6f2003-06-24 13:42:40 +0000323/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000324/* x86 ISA bus support */
325
Anthony Liguoric227f092009-10-01 16:12:16 -0500326target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000327PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000328
bellard0824d6f2003-06-24 13:42:40 +0000329/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000330void hw_error(const char *fmt, ...)
331{
332 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000333 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000334
335 va_start(ap, fmt);
336 fprintf(stderr, "qemu: hardware error: ");
337 vfprintf(stderr, fmt, ap);
338 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000339 for(env = first_cpu; env != NULL; env = env->next_cpu) {
340 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000341#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000342 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000343#else
bellard6a00d602005-11-21 23:25:50 +0000344 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000345#endif
bellard6a00d602005-11-21 23:25:50 +0000346 }
bellard0824d6f2003-06-24 13:42:40 +0000347 va_end(ap);
348 abort();
349}
Andi Kleen18894652009-07-02 09:34:17 +0200350
351static void set_proc_name(const char *s)
352{
Nathan Froyd6ca8d0f2009-08-03 07:32:12 -0700353#if defined(__linux__) && defined(PR_SET_NAME)
Andi Kleen18894652009-07-02 09:34:17 +0200354 char name[16];
355 if (!s)
356 return;
357 name[sizeof(name) - 1] = 0;
358 strncpy(name, s, sizeof(name));
359 /* Could rewrite argv[0] too, but that's a bit more complicated.
360 This simple way is enough for `top'. */
361 prctl(PR_SET_NAME, name);
362#endif
363}
aliguoridf751fa2008-12-04 20:19:35 +0000364
365/***************/
366/* ballooning */
367
368static QEMUBalloonEvent *qemu_balloon_event;
369void *qemu_balloon_event_opaque;
370
371void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
372{
373 qemu_balloon_event = func;
374 qemu_balloon_event_opaque = opaque;
375}
376
Adam Litke625a5be2010-01-26 14:17:35 -0600377int qemu_balloon(ram_addr_t target, MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +0000378{
Adam Litke625a5be2010-01-26 14:17:35 -0600379 if (qemu_balloon_event) {
380 qemu_balloon_event(qemu_balloon_event_opaque, target, cb, opaque);
381 return 1;
382 } else {
383 return 0;
384 }
aliguoridf751fa2008-12-04 20:19:35 +0000385}
386
Adam Litke625a5be2010-01-26 14:17:35 -0600387int qemu_balloon_status(MonitorCompletion cb, void *opaque)
aliguoridf751fa2008-12-04 20:19:35 +0000388{
Adam Litke625a5be2010-01-26 14:17:35 -0600389 if (qemu_balloon_event) {
390 qemu_balloon_event(qemu_balloon_event_opaque, 0, cb, opaque);
391 return 1;
392 } else {
393 return 0;
394 }
aliguoridf751fa2008-12-04 20:19:35 +0000395}
bellard0824d6f2003-06-24 13:42:40 +0000396
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100397
bellard8a7ddc32004-03-31 19:00:16 +0000398/***********************************************************/
Paolo Bonzini8f0056b2010-01-13 14:05:34 +0100399/* real time host monotonic timer */
bellard09b26c52006-04-12 21:09:08 +0000400
bellard87858c82003-06-27 12:01:39 +0000401/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000402uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000403{
404 union {
405 uint64_t ll;
406 struct {
Juan Quintelae2542fe2009-07-27 16:13:06 +0200407#ifdef HOST_WORDS_BIGENDIAN
bellard87858c82003-06-27 12:01:39 +0000408 uint32_t high, low;
409#else
410 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000411#endif
bellard87858c82003-06-27 12:01:39 +0000412 } l;
413 } u, res;
414 uint64_t rl, rh;
415
416 u.ll = a;
417 rl = (uint64_t)u.l.low * (uint64_t)b;
418 rh = (uint64_t)u.l.high * (uint64_t)b;
419 rh += (rl >> 32);
420 res.l.high = rh / c;
421 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
422 return res.ll;
423}
424
Jan Kiszka21d5d122009-09-15 13:36:04 +0200425static int64_t get_clock_realtime(void)
426{
427 struct timeval tv;
428
429 gettimeofday(&tv, NULL);
430 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
431}
432
bellard1dce7c32006-07-13 23:20:22 +0000433#ifdef WIN32
434
435static int64_t clock_freq;
436
437static void init_get_clock(void)
438{
bellarda8e5ac32006-07-14 09:36:13 +0000439 LARGE_INTEGER freq;
440 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000441 ret = QueryPerformanceFrequency(&freq);
442 if (ret == 0) {
443 fprintf(stderr, "Could not calibrate ticks\n");
444 exit(1);
445 }
446 clock_freq = freq.QuadPart;
447}
448
449static int64_t get_clock(void)
450{
451 LARGE_INTEGER ti;
452 QueryPerformanceCounter(&ti);
Anthony Liguori274dfed2009-09-11 10:28:26 -0500453 return muldiv64(ti.QuadPart, get_ticks_per_sec(), clock_freq);
bellard1dce7c32006-07-13 23:20:22 +0000454}
455
456#else
457
458static int use_rt_clock;
459
460static void init_get_clock(void)
461{
462 use_rt_clock = 0;
blueswir1c5e97232009-03-07 20:06:23 +0000463#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100464 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000465 {
466 struct timespec ts;
467 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
468 use_rt_clock = 1;
469 }
470 }
471#endif
472}
473
474static int64_t get_clock(void)
475{
blueswir1c5e97232009-03-07 20:06:23 +0000476#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
Aurelien Jarnoa167ba52009-11-29 18:00:41 +0100477 || defined(__DragonFly__) || defined(__FreeBSD_kernel__)
bellard1dce7c32006-07-13 23:20:22 +0000478 if (use_rt_clock) {
479 struct timespec ts;
480 clock_gettime(CLOCK_MONOTONIC, &ts);
481 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000482 } else
bellard1dce7c32006-07-13 23:20:22 +0000483#endif
484 {
485 /* XXX: using gettimeofday leads to problems if the date
486 changes, so it should be avoided. */
Jan Kiszka21d5d122009-09-15 13:36:04 +0200487 return get_clock_realtime();
bellard1dce7c32006-07-13 23:20:22 +0000488 }
489}
bellard1dce7c32006-07-13 23:20:22 +0000490#endif
491
pbrook2e70f6e2008-06-29 01:03:05 +0000492/* Return the virtual CPU time, based on the instruction counter. */
493static int64_t cpu_get_icount(void)
494{
495 int64_t icount;
496 CPUState *env = cpu_single_env;;
497 icount = qemu_icount;
498 if (env) {
499 if (!can_do_io(env))
500 fprintf(stderr, "Bad clock read\n");
501 icount -= (env->icount_decr.u16.low + env->icount_extra);
502 }
503 return qemu_icount_bias + (icount << icount_time_shift);
504}
505
bellard1dce7c32006-07-13 23:20:22 +0000506/***********************************************************/
507/* guest cycle counter */
508
Juan Quintela6f68e332009-09-10 03:04:27 +0200509typedef struct TimersState {
510 int64_t cpu_ticks_prev;
511 int64_t cpu_ticks_offset;
512 int64_t cpu_clock_offset;
513 int32_t cpu_ticks_enabled;
Anthony Liguori274dfed2009-09-11 10:28:26 -0500514 int64_t dummy;
Juan Quintela6f68e332009-09-10 03:04:27 +0200515} TimersState;
516
517TimersState timers_state;
bellard1dce7c32006-07-13 23:20:22 +0000518
519/* return the host CPU cycle counter and handle stop/restart */
520int64_t cpu_get_ticks(void)
521{
pbrook2e70f6e2008-06-29 01:03:05 +0000522 if (use_icount) {
523 return cpu_get_icount();
524 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200525 if (!timers_state.cpu_ticks_enabled) {
526 return timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000527 } else {
528 int64_t ticks;
529 ticks = cpu_get_real_ticks();
Juan Quintela6f68e332009-09-10 03:04:27 +0200530 if (timers_state.cpu_ticks_prev > ticks) {
bellard1dce7c32006-07-13 23:20:22 +0000531 /* Note: non increasing ticks may happen if the host uses
532 software suspend */
Juan Quintela6f68e332009-09-10 03:04:27 +0200533 timers_state.cpu_ticks_offset += timers_state.cpu_ticks_prev - ticks;
bellard1dce7c32006-07-13 23:20:22 +0000534 }
Juan Quintela6f68e332009-09-10 03:04:27 +0200535 timers_state.cpu_ticks_prev = ticks;
536 return ticks + timers_state.cpu_ticks_offset;
bellard1dce7c32006-07-13 23:20:22 +0000537 }
538}
539
540/* return the host CPU monotonic timer and handle stop/restart */
541static int64_t cpu_get_clock(void)
542{
543 int64_t ti;
Juan Quintela6f68e332009-09-10 03:04:27 +0200544 if (!timers_state.cpu_ticks_enabled) {
545 return timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000546 } else {
547 ti = get_clock();
Juan Quintela6f68e332009-09-10 03:04:27 +0200548 return ti + timers_state.cpu_clock_offset;
bellard1dce7c32006-07-13 23:20:22 +0000549 }
550}
551
552/* enable cpu_get_ticks() */
553void cpu_enable_ticks(void)
554{
Juan Quintela6f68e332009-09-10 03:04:27 +0200555 if (!timers_state.cpu_ticks_enabled) {
556 timers_state.cpu_ticks_offset -= cpu_get_real_ticks();
557 timers_state.cpu_clock_offset -= get_clock();
558 timers_state.cpu_ticks_enabled = 1;
bellard1dce7c32006-07-13 23:20:22 +0000559 }
560}
561
562/* disable cpu_get_ticks() : the clock is stopped. You must not call
563 cpu_get_ticks() after that. */
564void cpu_disable_ticks(void)
565{
Juan Quintela6f68e332009-09-10 03:04:27 +0200566 if (timers_state.cpu_ticks_enabled) {
567 timers_state.cpu_ticks_offset = cpu_get_ticks();
568 timers_state.cpu_clock_offset = cpu_get_clock();
569 timers_state.cpu_ticks_enabled = 0;
bellard1dce7c32006-07-13 23:20:22 +0000570 }
571}
572
573/***********************************************************/
574/* timers */
ths5fafdf22007-09-16 21:08:06 +0000575
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200576#define QEMU_CLOCK_REALTIME 0
577#define QEMU_CLOCK_VIRTUAL 1
Jan Kiszka21d5d122009-09-15 13:36:04 +0200578#define QEMU_CLOCK_HOST 2
bellard8a7ddc32004-03-31 19:00:16 +0000579
580struct QEMUClock {
581 int type;
582 /* XXX: add frequency */
583};
584
585struct QEMUTimer {
586 QEMUClock *clock;
587 int64_t expire_time;
588 QEMUTimerCB *cb;
589 void *opaque;
590 struct QEMUTimer *next;
591};
592
thsc8994012007-08-19 21:56:03 +0000593struct qemu_alarm_timer {
594 char const *name;
thsefe75412007-08-24 01:36:32 +0000595 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000596
597 int (*start)(struct qemu_alarm_timer *t);
598 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000599 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000600 void *priv;
601};
602
thsefe75412007-08-24 01:36:32 +0000603#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000604#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000605
606static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
607{
Jean-Christophe Duboise3323402009-05-17 18:38:39 +0200608 return t && (t->flags & ALARM_FLAG_DYNTICKS);
thsefe75412007-08-24 01:36:32 +0000609}
610
611static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
612{
613 if (!alarm_has_dynticks(t))
614 return;
615
616 t->rearm(t);
617}
618
619/* TODO: MIN_TIMER_REARM_US should be optimized */
620#define MIN_TIMER_REARM_US 250
621
thsc8994012007-08-19 21:56:03 +0000622static struct qemu_alarm_timer *alarm_timer;
623
624#ifdef _WIN32
625
626struct qemu_alarm_win32 {
627 MMRESULT timerId;
thsc8994012007-08-19 21:56:03 +0000628 unsigned int period;
Blue Swirlef28c4b2009-04-25 12:56:37 +0000629} alarm_win32_data = {0, -1};
thsc8994012007-08-19 21:56:03 +0000630
631static int win32_start_timer(struct qemu_alarm_timer *t);
632static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000633static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000634
635#else
636
637static int unix_start_timer(struct qemu_alarm_timer *t);
638static void unix_stop_timer(struct qemu_alarm_timer *t);
639
ths231c6582007-08-26 17:29:15 +0000640#ifdef __linux__
641
thsefe75412007-08-24 01:36:32 +0000642static int dynticks_start_timer(struct qemu_alarm_timer *t);
643static void dynticks_stop_timer(struct qemu_alarm_timer *t);
644static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
645
thsc40ec5a2007-08-19 22:09:40 +0000646static int hpet_start_timer(struct qemu_alarm_timer *t);
647static void hpet_stop_timer(struct qemu_alarm_timer *t);
648
thsc8994012007-08-19 21:56:03 +0000649static int rtc_start_timer(struct qemu_alarm_timer *t);
650static void rtc_stop_timer(struct qemu_alarm_timer *t);
651
thsefe75412007-08-24 01:36:32 +0000652#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000653
654#endif /* _WIN32 */
655
pbrook2e70f6e2008-06-29 01:03:05 +0000656/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000657 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000658 When the guest is idle real and virtual time will be aligned in
659 the IO wait loop. */
Anthony Liguori274dfed2009-09-11 10:28:26 -0500660#define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
pbrook2e70f6e2008-06-29 01:03:05 +0000661
662static void icount_adjust(void)
663{
664 int64_t cur_time;
665 int64_t cur_icount;
666 int64_t delta;
667 static int64_t last_delta;
668 /* If the VM is not running, then do nothing. */
669 if (!vm_running)
670 return;
671
672 cur_time = cpu_get_clock();
673 cur_icount = qemu_get_clock(vm_clock);
674 delta = cur_icount - cur_time;
675 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
676 if (delta > 0
677 && last_delta + ICOUNT_WOBBLE < delta * 2
678 && icount_time_shift > 0) {
679 /* The guest is getting too far ahead. Slow time down. */
680 icount_time_shift--;
681 }
682 if (delta < 0
683 && last_delta - ICOUNT_WOBBLE > delta * 2
684 && icount_time_shift < MAX_ICOUNT_SHIFT) {
685 /* The guest is getting too far behind. Speed time up. */
686 icount_time_shift++;
687 }
688 last_delta = delta;
689 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
690}
691
692static void icount_adjust_rt(void * opaque)
693{
694 qemu_mod_timer(icount_rt_timer,
695 qemu_get_clock(rt_clock) + 1000);
696 icount_adjust();
697}
698
699static void icount_adjust_vm(void * opaque)
700{
701 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500702 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000703 icount_adjust();
704}
705
706static void init_icount_adjust(void)
707{
708 /* Have both realtime and virtual time triggers for speed adjustment.
709 The realtime trigger catches emulated time passing too slowly,
710 the virtual time trigger catches emulated time passing too fast.
711 Realtime triggers occur even when idle, so use them less frequently
712 than VM triggers. */
713 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
714 qemu_mod_timer(icount_rt_timer,
715 qemu_get_clock(rt_clock) + 1000);
716 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
717 qemu_mod_timer(icount_vm_timer,
Anthony Liguori274dfed2009-09-11 10:28:26 -0500718 qemu_get_clock(vm_clock) + get_ticks_per_sec() / 10);
pbrook2e70f6e2008-06-29 01:03:05 +0000719}
720
thsc8994012007-08-19 21:56:03 +0000721static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000722#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000723#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000724 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
725 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000726 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +0000727 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +0000728 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +0000729 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000730#endif
thsefe75412007-08-24 01:36:32 +0000731 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +0000732#else
thsefe75412007-08-24 01:36:32 +0000733 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
734 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
735 {"win32", 0, win32_start_timer,
736 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +0000737#endif
738 {NULL, }
739};
740
blueswir13f47aa82008-03-09 06:59:01 +0000741static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +0000742{
743 int i;
744
745 printf("Available alarm timers, in order of precedence:\n");
746 for (i = 0; alarm_timers[i].name; i++)
747 printf("%s\n", alarm_timers[i].name);
748}
749
750static void configure_alarms(char const *opt)
751{
752 int i;
753 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +0000754 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +0000755 char *arg;
756 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +0000757 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +0000758
aurel323adda042008-03-09 23:43:49 +0000759 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +0000760 show_available_alarms();
761 exit(0);
762 }
763
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200764 arg = qemu_strdup(opt);
thsf3dcfad2007-08-24 01:26:02 +0000765
766 /* Reorder the array */
767 name = strtok(arg, ",");
768 while (name) {
balroge2b577e2007-09-17 21:25:20 +0000769 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +0000770 if (!strcmp(alarm_timers[i].name, name))
771 break;
772 }
773
774 if (i == count) {
775 fprintf(stderr, "Unknown clock %s\n", name);
776 goto next;
777 }
778
779 if (i < cur)
780 /* Ignore */
781 goto next;
782
783 /* Swap */
784 tmp = alarm_timers[i];
785 alarm_timers[i] = alarm_timers[cur];
786 alarm_timers[cur] = tmp;
787
788 cur++;
789next:
790 name = strtok(NULL, ",");
791 }
792
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +0200793 qemu_free(arg);
thsf3dcfad2007-08-24 01:26:02 +0000794
795 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +0000796 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +0000797 for (i = cur; i < count; i++)
798 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +0000799 } else {
800 show_available_alarms();
801 exit(1);
thsf3dcfad2007-08-24 01:26:02 +0000802 }
thsf3dcfad2007-08-24 01:26:02 +0000803}
804
Jan Kiszka21d5d122009-09-15 13:36:04 +0200805#define QEMU_NUM_CLOCKS 3
806
bellard8a7ddc32004-03-31 19:00:16 +0000807QEMUClock *rt_clock;
808QEMUClock *vm_clock;
Jan Kiszka21d5d122009-09-15 13:36:04 +0200809QEMUClock *host_clock;
bellard8a7ddc32004-03-31 19:00:16 +0000810
Jan Kiszka21d5d122009-09-15 13:36:04 +0200811static QEMUTimer *active_timers[QEMU_NUM_CLOCKS];
bellard8a7ddc32004-03-31 19:00:16 +0000812
pbrook9596ebb2007-11-18 01:44:38 +0000813static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +0000814{
815 QEMUClock *clock;
816 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +0000817 clock->type = type;
818 return clock;
819}
820
821QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
822{
823 QEMUTimer *ts;
824
825 ts = qemu_mallocz(sizeof(QEMUTimer));
826 ts->clock = clock;
827 ts->cb = cb;
828 ts->opaque = opaque;
829 return ts;
830}
831
832void qemu_free_timer(QEMUTimer *ts)
833{
834 qemu_free(ts);
835}
836
837/* stop a timer, but do not dealloc it */
838void qemu_del_timer(QEMUTimer *ts)
839{
840 QEMUTimer **pt, *t;
841
842 /* NOTE: this code must be signal safe because
843 qemu_timer_expired() can be called from a signal. */
844 pt = &active_timers[ts->clock->type];
845 for(;;) {
846 t = *pt;
847 if (!t)
848 break;
849 if (t == ts) {
850 *pt = t->next;
851 break;
852 }
853 pt = &t->next;
854 }
855}
856
857/* modify the current timer so that it will be fired when current_time
858 >= expire_time. The corresponding callback will be called. */
859void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
860{
861 QEMUTimer **pt, *t;
862
863 qemu_del_timer(ts);
864
865 /* add the timer in the sorted list */
866 /* NOTE: this code must be signal safe because
867 qemu_timer_expired() can be called from a signal. */
868 pt = &active_timers[ts->clock->type];
869 for(;;) {
870 t = *pt;
871 if (!t)
872 break;
ths5fafdf22007-09-16 21:08:06 +0000873 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +0000874 break;
875 pt = &t->next;
876 }
877 ts->expire_time = expire_time;
878 ts->next = *pt;
879 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +0000880
881 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +0000882 if (pt == &active_timers[ts->clock->type]) {
883 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
884 qemu_rearm_alarm_timer(alarm_timer);
885 }
886 /* Interrupt execution to force deadline recalculation. */
aliguorid9f75a42009-04-24 18:03:11 +0000887 if (use_icount)
888 qemu_notify_event();
pbrook2e70f6e2008-06-29 01:03:05 +0000889 }
bellard8a7ddc32004-03-31 19:00:16 +0000890}
891
892int qemu_timer_pending(QEMUTimer *ts)
893{
894 QEMUTimer *t;
895 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
896 if (t == ts)
897 return 1;
898 }
899 return 0;
900}
901
Stefano Stabellini2430ffe2009-08-03 10:56:01 +0100902int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
bellard8a7ddc32004-03-31 19:00:16 +0000903{
904 if (!timer_head)
905 return 0;
906 return (timer_head->expire_time <= current_time);
907}
908
909static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
910{
911 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +0000912
bellard8a7ddc32004-03-31 19:00:16 +0000913 for(;;) {
914 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +0000915 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +0000916 break;
917 /* remove timer from the list before calling the callback */
918 *ptimer_head = ts->next;
919 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +0000920
bellard8a7ddc32004-03-31 19:00:16 +0000921 /* run the callback (the timer list can be modified) */
922 ts->cb(ts->opaque);
923 }
924}
925
926int64_t qemu_get_clock(QEMUClock *clock)
927{
928 switch(clock->type) {
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200929 case QEMU_CLOCK_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +0000930 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +0000931 default:
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200932 case QEMU_CLOCK_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +0000933 if (use_icount) {
934 return cpu_get_icount();
935 } else {
936 return cpu_get_clock();
937 }
Jan Kiszka21d5d122009-09-15 13:36:04 +0200938 case QEMU_CLOCK_HOST:
939 return get_clock_realtime();
bellard8a7ddc32004-03-31 19:00:16 +0000940 }
941}
942
Paolo Bonzini41c872b2010-01-26 10:31:46 +0200943int64_t qemu_get_clock_ns(QEMUClock *clock)
944{
945 switch(clock->type) {
946 case QEMU_CLOCK_REALTIME:
947 return get_clock();
948 default:
949 case QEMU_CLOCK_VIRTUAL:
950 if (use_icount) {
951 return cpu_get_icount();
952 } else {
953 return cpu_get_clock();
954 }
955 case QEMU_CLOCK_HOST:
956 return get_clock_realtime();
957 }
958}
959
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200960static void init_clocks(void)
bellard1dce7c32006-07-13 23:20:22 +0000961{
962 init_get_clock();
Jan Kiszka0fdddf82009-09-15 13:36:04 +0200963 rt_clock = qemu_new_clock(QEMU_CLOCK_REALTIME);
964 vm_clock = qemu_new_clock(QEMU_CLOCK_VIRTUAL);
Jan Kiszka21d5d122009-09-15 13:36:04 +0200965 host_clock = qemu_new_clock(QEMU_CLOCK_HOST);
Jan Kiszka68752042009-09-15 13:36:04 +0200966
967 rtc_clock = host_clock;
bellard1dce7c32006-07-13 23:20:22 +0000968}
969
bellard8a7ddc32004-03-31 19:00:16 +0000970/* save a timer */
971void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
972{
973 uint64_t expire_time;
974
975 if (qemu_timer_pending(ts)) {
976 expire_time = ts->expire_time;
977 } else {
978 expire_time = -1;
979 }
980 qemu_put_be64(f, expire_time);
981}
982
983void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
984{
985 uint64_t expire_time;
986
987 expire_time = qemu_get_be64(f);
988 if (expire_time != -1) {
989 qemu_mod_timer(ts, expire_time);
990 } else {
991 qemu_del_timer(ts);
992 }
993}
994
Juan Quintela2faf58c2009-09-10 03:04:28 +0200995static const VMStateDescription vmstate_timers = {
996 .name = "timer",
997 .version_id = 2,
998 .minimum_version_id = 1,
999 .minimum_version_id_old = 1,
1000 .fields = (VMStateField []) {
1001 VMSTATE_INT64(cpu_ticks_offset, TimersState),
Anthony Liguori274dfed2009-09-11 10:28:26 -05001002 VMSTATE_INT64(dummy, TimersState),
Juan Quintela2faf58c2009-09-10 03:04:28 +02001003 VMSTATE_INT64_V(cpu_clock_offset, TimersState, 2),
1004 VMSTATE_END_OF_LIST()
bellardc88676f2006-08-06 13:36:11 +00001005 }
Juan Quintela2faf58c2009-09-10 03:04:28 +02001006};
bellard8a7ddc32004-03-31 19:00:16 +00001007
aliguori50317c72009-04-24 18:03:29 +00001008static void qemu_event_increment(void);
1009
bellard67b915a2004-03-31 23:37:16 +00001010#ifdef _WIN32
blueswir1b9e82a52009-04-05 18:03:31 +00001011static void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1012 DWORD_PTR dwUser, DWORD_PTR dw1,
1013 DWORD_PTR dw2)
bellard67b915a2004-03-31 23:37:16 +00001014#else
bellard8a7ddc32004-03-31 19:00:16 +00001015static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001016#endif
bellard8a7ddc32004-03-31 19:00:16 +00001017{
bellard02ba45c2004-06-25 14:46:23 +00001018#if 0
1019#define DISP_FREQ 1000
1020 {
1021 static int64_t delta_min = INT64_MAX;
1022 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1023 static int count;
1024 ti = qemu_get_clock(vm_clock);
1025 if (last_clock != 0) {
1026 delta = ti - last_clock;
1027 if (delta < delta_min)
1028 delta_min = delta;
1029 if (delta > delta_max)
1030 delta_max = delta;
1031 delta_cum += delta;
1032 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001033 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
Juan Quintela6ee093c2009-09-10 03:04:26 +02001034 muldiv64(delta_min, 1000000, get_ticks_per_sec()),
1035 muldiv64(delta_max, 1000000, get_ticks_per_sec()),
1036 muldiv64(delta_cum, 1000000 / DISP_FREQ, get_ticks_per_sec()),
1037 (double)get_ticks_per_sec() / ((double)delta_cum / DISP_FREQ));
bellard02ba45c2004-06-25 14:46:23 +00001038 count = 0;
1039 delta_min = INT64_MAX;
1040 delta_max = 0;
1041 delta_cum = 0;
1042 }
1043 }
1044 last_clock = ti;
1045 }
1046#endif
thsefe75412007-08-24 01:36:32 +00001047 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001048 (!use_icount &&
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001049 qemu_timer_expired(active_timers[QEMU_CLOCK_VIRTUAL],
pbrook2e70f6e2008-06-29 01:03:05 +00001050 qemu_get_clock(vm_clock))) ||
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001051 qemu_timer_expired(active_timers[QEMU_CLOCK_REALTIME],
Jan Kiszka21d5d122009-09-15 13:36:04 +02001052 qemu_get_clock(rt_clock)) ||
1053 qemu_timer_expired(active_timers[QEMU_CLOCK_HOST],
1054 qemu_get_clock(host_clock))) {
aliguori50317c72009-04-24 18:03:29 +00001055 qemu_event_increment();
Jean-Christophe Duboise3323402009-05-17 18:38:39 +02001056 if (alarm_timer) alarm_timer->flags |= ALARM_FLAG_EXPIRED;
balrogd5d08332008-01-05 19:41:47 +00001057
aliguorid6dc3d42009-04-24 18:04:07 +00001058#ifndef CONFIG_IOTHREAD
1059 if (next_cpu) {
balrog4f8eb8d2007-12-16 12:39:38 +00001060 /* stop the currently executing cpu because a timer occured */
aliguorid6dc3d42009-04-24 18:04:07 +00001061 cpu_exit(next_cpu);
balrog4f8eb8d2007-12-16 12:39:38 +00001062 }
aliguorid6dc3d42009-04-24 18:04:07 +00001063#endif
aliguori43b96852009-04-24 18:03:33 +00001064 timer_alarm_pending = 1;
aliguorid9f75a42009-04-24 18:03:11 +00001065 qemu_notify_event();
bellard8a7ddc32004-03-31 19:00:16 +00001066 }
1067}
1068
pbrook2e70f6e2008-06-29 01:03:05 +00001069static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001070{
Jan Kiszka21d5d122009-09-15 13:36:04 +02001071 /* To avoid problems with overflow limit this to 2^32. */
1072 int64_t delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001073
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001074 if (active_timers[QEMU_CLOCK_VIRTUAL]) {
1075 delta = active_timers[QEMU_CLOCK_VIRTUAL]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001076 qemu_get_clock(vm_clock);
Jan Kiszka21d5d122009-09-15 13:36:04 +02001077 }
1078 if (active_timers[QEMU_CLOCK_HOST]) {
1079 int64_t hdelta = active_timers[QEMU_CLOCK_HOST]->expire_time -
1080 qemu_get_clock(host_clock);
1081 if (hdelta < delta)
1082 delta = hdelta;
thsefe75412007-08-24 01:36:32 +00001083 }
1084
pbrook2e70f6e2008-06-29 01:03:05 +00001085 if (delta < 0)
1086 delta = 0;
thsefe75412007-08-24 01:36:32 +00001087
pbrook2e70f6e2008-06-29 01:03:05 +00001088 return delta;
1089}
1090
Jan Kiszkaf64382b2009-09-15 13:36:04 +02001091#if defined(__linux__)
pbrook2e70f6e2008-06-29 01:03:05 +00001092static uint64_t qemu_next_deadline_dyntick(void)
1093{
1094 int64_t delta;
1095 int64_t rtdelta;
1096
1097 if (use_icount)
1098 delta = INT32_MAX;
1099 else
1100 delta = (qemu_next_deadline() + 999) / 1000;
1101
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001102 if (active_timers[QEMU_CLOCK_REALTIME]) {
1103 rtdelta = (active_timers[QEMU_CLOCK_REALTIME]->expire_time -
pbrook2e70f6e2008-06-29 01:03:05 +00001104 qemu_get_clock(rt_clock))*1000;
1105 if (rtdelta < delta)
1106 delta = rtdelta;
1107 }
1108
1109 if (delta < MIN_TIMER_REARM_US)
1110 delta = MIN_TIMER_REARM_US;
1111
1112 return delta;
thsefe75412007-08-24 01:36:32 +00001113}
blueswir18632fb92008-09-14 13:59:34 +00001114#endif
thsefe75412007-08-24 01:36:32 +00001115
bellardfd872592004-05-12 19:11:15 +00001116#ifndef _WIN32
1117
aliguori7183b4b2008-11-05 20:40:18 +00001118/* Sets a specific flag */
1119static int fcntl_setfl(int fd, int flag)
1120{
1121 int flags;
1122
1123 flags = fcntl(fd, F_GETFL);
1124 if (flags == -1)
1125 return -errno;
1126
1127 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1128 return -errno;
1129
1130 return 0;
1131}
1132
bellard829309c2004-05-20 13:20:12 +00001133#if defined(__linux__)
1134
bellardfd872592004-05-12 19:11:15 +00001135#define RTC_FREQ 1024
1136
aurel32de9a95f2008-11-11 13:41:01 +00001137static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001138{
thsc8994012007-08-19 21:56:03 +00001139 struct sigaction act;
1140
1141 /* timer signal */
1142 sigfillset(&act.sa_mask);
1143 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001144 act.sa_handler = host_alarm_handler;
1145
1146 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001147 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001148 fcntl(fd, F_SETOWN, getpid());
1149}
1150
thsc40ec5a2007-08-19 22:09:40 +00001151static int hpet_start_timer(struct qemu_alarm_timer *t)
1152{
1153 struct hpet_info info;
1154 int r, fd;
1155
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001156 fd = qemu_open("/dev/hpet", O_RDONLY);
thsc40ec5a2007-08-19 22:09:40 +00001157 if (fd < 0)
1158 return -1;
1159
1160 /* Set frequency */
1161 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1162 if (r < 0) {
1163 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1164 "error, but for better emulation accuracy type:\n"
1165 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1166 goto fail;
1167 }
1168
1169 /* Check capabilities */
1170 r = ioctl(fd, HPET_INFO, &info);
1171 if (r < 0)
1172 goto fail;
1173
1174 /* Enable periodic mode */
1175 r = ioctl(fd, HPET_EPI, 0);
1176 if (info.hi_flags && (r < 0))
1177 goto fail;
1178
1179 /* Enable interrupt */
1180 r = ioctl(fd, HPET_IE_ON, 0);
1181 if (r < 0)
1182 goto fail;
1183
1184 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001185 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001186
1187 return 0;
1188fail:
1189 close(fd);
1190 return -1;
1191}
1192
1193static void hpet_stop_timer(struct qemu_alarm_timer *t)
1194{
pbrookfcdc2122007-08-23 20:22:22 +00001195 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001196
1197 close(fd);
1198}
1199
thsc8994012007-08-19 21:56:03 +00001200static int rtc_start_timer(struct qemu_alarm_timer *t)
1201{
1202 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001203 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001204
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001205 TFR(rtc_fd = qemu_open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001206 if (rtc_fd < 0)
1207 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001208 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1209 if (current_rtc_freq != RTC_FREQ &&
1210 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001211 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1212 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1213 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1214 goto fail;
1215 }
1216 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1217 fail:
1218 close(rtc_fd);
1219 return -1;
1220 }
thsc8994012007-08-19 21:56:03 +00001221
1222 enable_sigio_timer(rtc_fd);
1223
pbrookfcdc2122007-08-23 20:22:22 +00001224 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001225
bellardfd872592004-05-12 19:11:15 +00001226 return 0;
1227}
1228
thsc8994012007-08-19 21:56:03 +00001229static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001230{
pbrookfcdc2122007-08-23 20:22:22 +00001231 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001232
1233 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001234}
1235
thsefe75412007-08-24 01:36:32 +00001236static int dynticks_start_timer(struct qemu_alarm_timer *t)
1237{
1238 struct sigevent ev;
1239 timer_t host_timer;
1240 struct sigaction act;
1241
1242 sigfillset(&act.sa_mask);
1243 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001244 act.sa_handler = host_alarm_handler;
1245
1246 sigaction(SIGALRM, &act, NULL);
1247
Jean-Christophe Dubois9ed415b2009-05-17 18:41:16 +02001248 /*
1249 * Initialize ev struct to 0 to avoid valgrind complaining
1250 * about uninitialized data in timer_create call
1251 */
1252 memset(&ev, 0, sizeof(ev));
thsefe75412007-08-24 01:36:32 +00001253 ev.sigev_value.sival_int = 0;
1254 ev.sigev_notify = SIGEV_SIGNAL;
1255 ev.sigev_signo = SIGALRM;
1256
1257 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1258 perror("timer_create");
1259
1260 /* disable dynticks */
1261 fprintf(stderr, "Dynamic Ticks disabled\n");
1262
1263 return -1;
1264 }
1265
blueswir10399bfe2008-11-16 11:37:18 +00001266 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001267
1268 return 0;
1269}
1270
1271static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1272{
blueswir10399bfe2008-11-16 11:37:18 +00001273 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001274
1275 timer_delete(host_timer);
1276}
1277
1278static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1279{
blueswir10399bfe2008-11-16 11:37:18 +00001280 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001281 struct itimerspec timeout;
1282 int64_t nearest_delta_us = INT64_MAX;
1283 int64_t current_us;
1284
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001285 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001286 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1287 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001288 return;
thsefe75412007-08-24 01:36:32 +00001289
pbrook2e70f6e2008-06-29 01:03:05 +00001290 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001291
1292 /* check whether a timer is already running */
1293 if (timer_gettime(host_timer, &timeout)) {
1294 perror("gettime");
1295 fprintf(stderr, "Internal timer error: aborting\n");
1296 exit(1);
1297 }
1298 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1299 if (current_us && current_us <= nearest_delta_us)
1300 return;
1301
1302 timeout.it_interval.tv_sec = 0;
1303 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1304 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1305 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1306 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1307 perror("settime");
1308 fprintf(stderr, "Internal timer error: aborting\n");
1309 exit(1);
1310 }
1311}
1312
ths70744b32007-08-26 17:31:30 +00001313#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001314
thsc8994012007-08-19 21:56:03 +00001315static int unix_start_timer(struct qemu_alarm_timer *t)
1316{
1317 struct sigaction act;
1318 struct itimerval itv;
1319 int err;
1320
1321 /* timer signal */
1322 sigfillset(&act.sa_mask);
1323 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001324 act.sa_handler = host_alarm_handler;
1325
1326 sigaction(SIGALRM, &act, NULL);
1327
1328 itv.it_interval.tv_sec = 0;
1329 /* for i386 kernel 2.6 to get 1 ms */
1330 itv.it_interval.tv_usec = 999;
1331 itv.it_value.tv_sec = 0;
1332 itv.it_value.tv_usec = 10 * 1000;
1333
1334 err = setitimer(ITIMER_REAL, &itv, NULL);
1335 if (err)
1336 return -1;
1337
1338 return 0;
1339}
1340
1341static void unix_stop_timer(struct qemu_alarm_timer *t)
1342{
1343 struct itimerval itv;
1344
1345 memset(&itv, 0, sizeof(itv));
1346 setitimer(ITIMER_REAL, &itv, NULL);
1347}
1348
bellard829309c2004-05-20 13:20:12 +00001349#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001350
aliguorif49e58d2008-11-05 21:22:34 +00001351
thsc8994012007-08-19 21:56:03 +00001352#ifdef _WIN32
1353
1354static int win32_start_timer(struct qemu_alarm_timer *t)
1355{
1356 TIMECAPS tc;
1357 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001358 UINT flags;
thsc8994012007-08-19 21:56:03 +00001359
thsc8994012007-08-19 21:56:03 +00001360 memset(&tc, 0, sizeof(tc));
1361 timeGetDevCaps(&tc, sizeof(tc));
1362
1363 if (data->period < tc.wPeriodMin)
1364 data->period = tc.wPeriodMin;
1365
1366 timeBeginPeriod(data->period);
1367
thsefe75412007-08-24 01:36:32 +00001368 flags = TIME_CALLBACK_FUNCTION;
1369 if (alarm_has_dynticks(t))
1370 flags |= TIME_ONESHOT;
1371 else
1372 flags |= TIME_PERIODIC;
1373
thsc8994012007-08-19 21:56:03 +00001374 data->timerId = timeSetEvent(1, // interval (ms)
1375 data->period, // resolution
1376 host_alarm_handler, // function
1377 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001378 flags);
thsc8994012007-08-19 21:56:03 +00001379
1380 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001381 fprintf(stderr, "Failed to initialize win32 alarm timer: %ld\n",
malc705e83f2009-09-27 14:30:33 +04001382 GetLastError());
thsc8994012007-08-19 21:56:03 +00001383 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001384 return -1;
1385 }
1386
thsc8994012007-08-19 21:56:03 +00001387 return 0;
1388}
1389
1390static void win32_stop_timer(struct qemu_alarm_timer *t)
1391{
1392 struct qemu_alarm_win32 *data = t->priv;
1393
1394 timeKillEvent(data->timerId);
1395 timeEndPeriod(data->period);
thsc8994012007-08-19 21:56:03 +00001396}
1397
thsefe75412007-08-24 01:36:32 +00001398static void win32_rearm_timer(struct qemu_alarm_timer *t)
1399{
1400 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001401
Jan Kiszka0fdddf82009-09-15 13:36:04 +02001402 if (!active_timers[QEMU_CLOCK_REALTIME] &&
Jan Kiszka21d5d122009-09-15 13:36:04 +02001403 !active_timers[QEMU_CLOCK_VIRTUAL] &&
1404 !active_timers[QEMU_CLOCK_HOST])
balrogd5d08332008-01-05 19:41:47 +00001405 return;
thsefe75412007-08-24 01:36:32 +00001406
thsefe75412007-08-24 01:36:32 +00001407 timeKillEvent(data->timerId);
1408
1409 data->timerId = timeSetEvent(1,
1410 data->period,
1411 host_alarm_handler,
1412 (DWORD)t,
1413 TIME_ONESHOT | TIME_PERIODIC);
1414
1415 if (!data->timerId) {
Blue Swirl20889d42009-09-27 20:03:56 +00001416 fprintf(stderr, "Failed to re-arm win32 alarm timer %ld\n",
malc705e83f2009-09-27 14:30:33 +04001417 GetLastError());
thsefe75412007-08-24 01:36:32 +00001418
1419 timeEndPeriod(data->period);
thsefe75412007-08-24 01:36:32 +00001420 exit(1);
1421 }
1422}
1423
thsc8994012007-08-19 21:56:03 +00001424#endif /* _WIN32 */
1425
aliguori7183b4b2008-11-05 20:40:18 +00001426static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001427{
blueswir1223f0d72008-09-30 18:12:18 +00001428 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001429 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001430
thsc8994012007-08-19 21:56:03 +00001431 for (i = 0; alarm_timers[i].name; i++) {
1432 t = &alarm_timers[i];
1433
thsc8994012007-08-19 21:56:03 +00001434 err = t->start(t);
1435 if (!err)
1436 break;
bellard67b915a2004-03-31 23:37:16 +00001437 }
bellardfd872592004-05-12 19:11:15 +00001438
thsc8994012007-08-19 21:56:03 +00001439 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001440 err = -ENOENT;
1441 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001442 }
thsc8994012007-08-19 21:56:03 +00001443
1444 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001445
aliguori6abfbd72008-11-05 20:49:37 +00001446 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001447
1448fail:
aliguori7183b4b2008-11-05 20:40:18 +00001449 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001450}
1451
pbrook9596ebb2007-11-18 01:44:38 +00001452static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001453{
thsc8994012007-08-19 21:56:03 +00001454 alarm_timer->stop(alarm_timer);
1455 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001456}
1457
bellardc4b1fcc2004-03-14 21:44:30 +00001458/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001459/* host time/date access */
1460void qemu_get_timedate(struct tm *tm, int offset)
1461{
1462 time_t ti;
1463 struct tm *ret;
1464
1465 time(&ti);
1466 ti += offset;
1467 if (rtc_date_offset == -1) {
1468 if (rtc_utc)
1469 ret = gmtime(&ti);
1470 else
1471 ret = localtime(&ti);
1472 } else {
1473 ti -= rtc_date_offset;
1474 ret = gmtime(&ti);
1475 }
1476
1477 memcpy(tm, ret, sizeof(struct tm));
1478}
1479
1480int qemu_timedate_diff(struct tm *tm)
1481{
1482 time_t seconds;
1483
1484 if (rtc_date_offset == -1)
1485 if (rtc_utc)
1486 seconds = mktimegm(tm);
1487 else
1488 seconds = mktime(tm);
1489 else
1490 seconds = mktimegm(tm) + rtc_date_offset;
1491
1492 return seconds - time(NULL);
1493}
1494
Luiz Capitulino80cd3472010-02-25 12:11:44 -03001495void rtc_change_mon_event(struct tm *tm)
1496{
1497 QObject *data;
1498
1499 data = qobject_from_jsonf("{ 'offset': %d }", qemu_timedate_diff(tm));
1500 monitor_protocol_event(QEVENT_RTC_CHANGE, data);
1501 qobject_decref(data);
1502}
1503
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001504static void configure_rtc_date_offset(const char *startdate, int legacy)
1505{
1506 time_t rtc_start_date;
1507 struct tm tm;
1508
1509 if (!strcmp(startdate, "now") && legacy) {
1510 rtc_date_offset = -1;
1511 } else {
1512 if (sscanf(startdate, "%d-%d-%dT%d:%d:%d",
1513 &tm.tm_year,
1514 &tm.tm_mon,
1515 &tm.tm_mday,
1516 &tm.tm_hour,
1517 &tm.tm_min,
1518 &tm.tm_sec) == 6) {
1519 /* OK */
1520 } else if (sscanf(startdate, "%d-%d-%d",
1521 &tm.tm_year,
1522 &tm.tm_mon,
1523 &tm.tm_mday) == 3) {
1524 tm.tm_hour = 0;
1525 tm.tm_min = 0;
1526 tm.tm_sec = 0;
1527 } else {
1528 goto date_fail;
1529 }
1530 tm.tm_year -= 1900;
1531 tm.tm_mon--;
1532 rtc_start_date = mktimegm(&tm);
1533 if (rtc_start_date == -1) {
1534 date_fail:
1535 fprintf(stderr, "Invalid date format. Valid formats are:\n"
1536 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1537 exit(1);
1538 }
1539 rtc_date_offset = time(NULL) - rtc_start_date;
1540 }
1541}
1542
1543static void configure_rtc(QemuOpts *opts)
1544{
1545 const char *value;
1546
1547 value = qemu_opt_get(opts, "base");
1548 if (value) {
1549 if (!strcmp(value, "utc")) {
1550 rtc_utc = 1;
1551 } else if (!strcmp(value, "localtime")) {
1552 rtc_utc = 0;
1553 } else {
1554 configure_rtc_date_offset(value, 0);
1555 }
1556 }
Jan Kiszka68752042009-09-15 13:36:04 +02001557 value = qemu_opt_get(opts, "clock");
1558 if (value) {
1559 if (!strcmp(value, "host")) {
1560 rtc_clock = host_clock;
1561 } else if (!strcmp(value, "vm")) {
1562 rtc_clock = vm_clock;
1563 } else {
1564 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1565 exit(1);
1566 }
1567 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02001568#ifdef CONFIG_TARGET_I386
1569 value = qemu_opt_get(opts, "driftfix");
1570 if (value) {
1571 if (!strcmp(buf, "slew")) {
1572 rtc_td_hack = 1;
1573 } else if (!strcmp(buf, "none")) {
1574 rtc_td_hack = 0;
1575 } else {
1576 fprintf(stderr, "qemu: invalid option value '%s'\n", value);
1577 exit(1);
1578 }
1579 }
1580#endif
1581}
1582
bellardfd1dff42006-02-01 21:29:26 +00001583#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001584static void socket_cleanup(void)
1585{
1586 WSACleanup();
1587}
bellard82c643f2004-07-14 17:28:13 +00001588
bellardfd1dff42006-02-01 21:29:26 +00001589static int socket_init(void)
1590{
1591 WSADATA Data;
1592 int ret, err;
1593
1594 ret = WSAStartup(MAKEWORD(2,2), &Data);
1595 if (ret != 0) {
1596 err = WSAGetLastError();
1597 fprintf(stderr, "WSAStartup: %d\n", err);
1598 return -1;
1599 }
1600 atexit(socket_cleanup);
1601 return 0;
1602}
aurel3264b7b732008-05-05 10:05:31 +00001603#endif
1604
balrog1ae26a12008-09-28 23:19:47 +00001605/***********************************************************/
1606/* Bluetooth support */
1607static int nb_hcis;
1608static int cur_hci;
1609static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001610
balrog1ae26a12008-09-28 23:19:47 +00001611static struct bt_vlan_s {
1612 struct bt_scatternet_s net;
1613 int id;
1614 struct bt_vlan_s *next;
1615} *first_bt_vlan;
1616
1617/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001618static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001619{
1620 struct bt_vlan_s **pvlan, *vlan;
1621 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1622 if (vlan->id == id)
1623 return &vlan->net;
1624 }
1625 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1626 vlan->id = id;
1627 pvlan = &first_bt_vlan;
1628 while (*pvlan != NULL)
1629 pvlan = &(*pvlan)->next;
1630 *pvlan = vlan;
1631 return &vlan->net;
1632}
1633
1634static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1635{
1636}
1637
1638static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1639{
1640 return -ENOTSUP;
1641}
1642
1643static struct HCIInfo null_hci = {
1644 .cmd_send = null_hci_send,
1645 .sco_send = null_hci_send,
1646 .acl_send = null_hci_send,
1647 .bdaddr_set = null_hci_addr_set,
1648};
1649
1650struct HCIInfo *qemu_next_hci(void)
1651{
1652 if (cur_hci == nb_hcis)
1653 return &null_hci;
1654
1655 return hci_table[cur_hci++];
1656}
1657
balrogdc72ac12008-11-09 00:04:26 +00001658static struct HCIInfo *hci_init(const char *str)
1659{
1660 char *endp;
1661 struct bt_scatternet_s *vlan = 0;
1662
1663 if (!strcmp(str, "null"))
1664 /* null */
1665 return &null_hci;
1666 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1667 /* host[:hciN] */
1668 return bt_host_hci(str[4] ? str + 5 : "hci0");
1669 else if (!strncmp(str, "hci", 3)) {
1670 /* hci[,vlan=n] */
1671 if (str[3]) {
1672 if (!strncmp(str + 3, ",vlan=", 6)) {
1673 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
1674 if (*endp)
1675 vlan = 0;
1676 }
1677 } else
1678 vlan = qemu_find_bt_vlan(0);
1679 if (vlan)
1680 return bt_new_hci(vlan);
1681 }
1682
1683 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
1684
1685 return 0;
1686}
1687
1688static int bt_hci_parse(const char *str)
1689{
1690 struct HCIInfo *hci;
Anthony Liguoric227f092009-10-01 16:12:16 -05001691 bdaddr_t bdaddr;
balrogdc72ac12008-11-09 00:04:26 +00001692
1693 if (nb_hcis >= MAX_NICS) {
1694 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
1695 return -1;
1696 }
1697
1698 hci = hci_init(str);
1699 if (!hci)
1700 return -1;
1701
1702 bdaddr.b[0] = 0x52;
1703 bdaddr.b[1] = 0x54;
1704 bdaddr.b[2] = 0x00;
1705 bdaddr.b[3] = 0x12;
1706 bdaddr.b[4] = 0x34;
1707 bdaddr.b[5] = 0x56 + nb_hcis;
1708 hci->bdaddr_set(hci, bdaddr.b);
1709
1710 hci_table[nb_hcis++] = hci;
1711
1712 return 0;
1713}
1714
1715static void bt_vhci_add(int vlan_id)
1716{
1717 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
1718
1719 if (!vlan->slave)
1720 fprintf(stderr, "qemu: warning: adding a VHCI to "
1721 "an empty scatternet %i\n", vlan_id);
1722
1723 bt_vhci_init(bt_new_hci(vlan));
1724}
1725
1726static struct bt_device_s *bt_device_add(const char *opt)
1727{
1728 struct bt_scatternet_s *vlan;
1729 int vlan_id = 0;
1730 char *endp = strstr(opt, ",vlan=");
1731 int len = (endp ? endp - opt : strlen(opt)) + 1;
1732 char devname[10];
1733
1734 pstrcpy(devname, MIN(sizeof(devname), len), opt);
1735
1736 if (endp) {
1737 vlan_id = strtol(endp + 6, &endp, 0);
1738 if (*endp) {
1739 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
1740 return 0;
1741 }
1742 }
1743
1744 vlan = qemu_find_bt_vlan(vlan_id);
1745
1746 if (!vlan->slave)
1747 fprintf(stderr, "qemu: warning: adding a slave device to "
1748 "an empty scatternet %i\n", vlan_id);
1749
1750 if (!strcmp(devname, "keyboard"))
1751 return bt_keyboard_init(vlan);
1752
1753 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
1754 return 0;
1755}
1756
1757static int bt_parse(const char *opt)
1758{
1759 const char *endp, *p;
1760 int vlan;
1761
1762 if (strstart(opt, "hci", &endp)) {
1763 if (!*endp || *endp == ',') {
1764 if (*endp)
1765 if (!strstart(endp, ",vlan=", 0))
1766 opt = endp + 1;
1767
1768 return bt_hci_parse(opt);
1769 }
1770 } else if (strstart(opt, "vhci", &endp)) {
1771 if (!*endp || *endp == ',') {
1772 if (*endp) {
1773 if (strstart(endp, ",vlan=", &p)) {
1774 vlan = strtol(p, (char **) &endp, 0);
1775 if (*endp) {
1776 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
1777 return 1;
1778 }
1779 } else {
1780 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
1781 return 1;
1782 }
1783 } else
1784 vlan = 0;
1785
1786 bt_vhci_add(vlan);
1787 return 0;
1788 }
1789 } else if (strstart(opt, "device:", &endp))
1790 return !bt_device_add(endp);
1791
1792 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
1793 return 1;
1794}
1795
balrog1ae26a12008-09-28 23:19:47 +00001796/***********************************************************/
1797/* QEMU Block devices */
1798
balrog609497a2008-01-14 02:56:53 +00001799#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001800#define CDROM_ALIAS "index=2,media=cdrom"
thse4bcb142007-12-02 04:51:10 +00001801#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001802#define PFLASH_ALIAS "if=pflash"
1803#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001804#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00001805
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001806QemuOpts *drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00001807{
1808 va_list ap;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001809 char optstr[1024];
1810 QemuOpts *opts;
thse4bcb142007-12-02 04:51:10 +00001811
thse4bcb142007-12-02 04:51:10 +00001812 va_start(ap, fmt);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001813 vsnprintf(optstr, sizeof(optstr), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00001814 va_end(ap);
1815
Gerd Hoffmann7282a032009-07-31 12:25:35 +02001816 opts = qemu_opts_parse(&qemu_drive_opts, optstr, NULL);
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001817 if (!opts) {
1818 fprintf(stderr, "%s: huh? duplicate? (%s)\n",
1819 __FUNCTION__, optstr);
1820 return NULL;
1821 }
1822 if (file)
1823 qemu_opt_set(opts, "file", file);
1824 return opts;
aliguorib01b1112009-02-11 15:20:20 +00001825}
1826
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001827DriveInfo *drive_get(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00001828{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001829 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001830
1831 /* seek interface, bus and unit */
1832
Blue Swirl72cf2d42009-09-12 07:36:22 +00001833 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001834 if (dinfo->type == type &&
1835 dinfo->bus == bus &&
1836 dinfo->unit == unit)
1837 return dinfo;
1838 }
thse4bcb142007-12-02 04:51:10 +00001839
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001840 return NULL;
thse4bcb142007-12-02 04:51:10 +00001841}
1842
Gerd Hoffmann2e810b32009-07-31 12:25:38 +02001843DriveInfo *drive_get_by_id(const char *id)
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001844{
1845 DriveInfo *dinfo;
1846
Blue Swirl72cf2d42009-09-12 07:36:22 +00001847 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02001848 if (strcmp(id, dinfo->id))
1849 continue;
1850 return dinfo;
1851 }
1852 return NULL;
1853}
1854
thsf60d39b2007-12-17 03:55:57 +00001855int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00001856{
1857 int max_bus;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001858 DriveInfo *dinfo;
thse4bcb142007-12-02 04:51:10 +00001859
1860 max_bus = -1;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001861 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001862 if(dinfo->type == type &&
1863 dinfo->bus > max_bus)
1864 max_bus = dinfo->bus;
thse4bcb142007-12-02 04:51:10 +00001865 }
1866 return max_bus;
1867}
1868
aliguorifa879c62009-01-07 17:32:33 +00001869const char *drive_get_serial(BlockDriverState *bdrv)
1870{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001871 DriveInfo *dinfo;
aliguorifa879c62009-01-07 17:32:33 +00001872
Blue Swirl72cf2d42009-09-12 07:36:22 +00001873 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001874 if (dinfo->bdrv == bdrv)
1875 return dinfo->serial;
1876 }
aliguorifa879c62009-01-07 17:32:33 +00001877
1878 return "\0";
1879}
1880
Kevin Wolff7850092009-11-27 13:25:36 +01001881BlockInterfaceErrorAction drive_get_on_error(
1882 BlockDriverState *bdrv, int is_read)
aliguori428c5702009-01-21 18:59:04 +00001883{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001884 DriveInfo *dinfo;
aliguori428c5702009-01-21 18:59:04 +00001885
Blue Swirl72cf2d42009-09-12 07:36:22 +00001886 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001887 if (dinfo->bdrv == bdrv)
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001888 return is_read ? dinfo->on_read_error : dinfo->on_write_error;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001889 }
aliguori428c5702009-01-21 18:59:04 +00001890
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001891 return is_read ? BLOCK_ERR_REPORT : BLOCK_ERR_STOP_ENOSPC;
aliguori428c5702009-01-21 18:59:04 +00001892}
1893
aurel32a1620fa2008-04-29 05:58:01 +00001894static void bdrv_format_print(void *opaque, const char *name)
1895{
1896 fprintf(stderr, " %s", name);
1897}
1898
Gerd Hoffmann56a14932009-09-25 21:42:46 +02001899void drive_uninit(DriveInfo *dinfo)
aliguorib01b1112009-02-11 15:20:20 +00001900{
Gerd Hoffmann56a14932009-09-25 21:42:46 +02001901 qemu_opts_del(dinfo->opts);
1902 bdrv_delete(dinfo->bdrv);
1903 QTAILQ_REMOVE(&drives, dinfo, next);
1904 qemu_free(dinfo);
aliguorib01b1112009-02-11 15:20:20 +00001905}
1906
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001907static int parse_block_error_action(const char *buf, int is_read)
1908{
1909 if (!strcmp(buf, "ignore")) {
1910 return BLOCK_ERR_IGNORE;
1911 } else if (!is_read && !strcmp(buf, "enospc")) {
1912 return BLOCK_ERR_STOP_ENOSPC;
1913 } else if (!strcmp(buf, "stop")) {
1914 return BLOCK_ERR_STOP_ANY;
1915 } else if (!strcmp(buf, "report")) {
1916 return BLOCK_ERR_REPORT;
1917 } else {
1918 fprintf(stderr, "qemu: '%s' invalid %s error action\n",
1919 buf, is_read ? "read" : "write");
1920 return -1;
1921 }
1922}
1923
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001924DriveInfo *drive_init(QemuOpts *opts, void *opaque,
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001925 int *fatal_error)
thse4bcb142007-12-02 04:51:10 +00001926{
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001927 const char *buf;
1928 const char *file = NULL;
balrogc8522bd2007-12-06 22:11:20 +00001929 char devname[128];
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001930 const char *serial;
balrogc8522bd2007-12-06 22:11:20 +00001931 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00001932 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00001933 enum { MEDIA_DISK, MEDIA_CDROM } media;
1934 int bus_id, unit_id;
1935 int cyls, heads, secs, translation;
aurel321e72d3b2008-04-28 20:26:45 +00001936 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00001937 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00001938 int max_devs;
1939 int index;
balrog33f00272007-12-24 14:33:24 +00001940 int cache;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02001941 int aio = 0;
Naphtali Sprei59f26892009-10-26 16:25:16 +02001942 int ro = 0;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01001943 int bdrv_flags;
1944 int on_read_error, on_write_error;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02001945 const char *devaddr;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001946 DriveInfo *dinfo;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001947 int snapshot = 0;
1948
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001949 *fatal_error = 1;
thse4bcb142007-12-02 04:51:10 +00001950
thse4bcb142007-12-02 04:51:10 +00001951 translation = BIOS_ATA_TRANSLATION_AUTO;
Kevin Wolf0aa217e2009-06-30 13:06:04 +02001952 cache = 1;
thse4bcb142007-12-02 04:51:10 +00001953
Gerd Hoffmann4d007812009-08-31 14:23:57 +02001954 if (machine && machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00001955 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00001956 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00001957 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00001958 } else {
thsf60d39b2007-12-17 03:55:57 +00001959 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00001960 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00001961 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00001962 }
1963 media = MEDIA_DISK;
1964
1965 /* extract parameters */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001966 bus_id = qemu_opt_get_number(opts, "bus", 0);
1967 unit_id = qemu_opt_get_number(opts, "unit", -1);
1968 index = qemu_opt_get_number(opts, "index", -1);
thse4bcb142007-12-02 04:51:10 +00001969
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001970 cyls = qemu_opt_get_number(opts, "cyls", 0);
1971 heads = qemu_opt_get_number(opts, "heads", 0);
1972 secs = qemu_opt_get_number(opts, "secs", 0);
thse4bcb142007-12-02 04:51:10 +00001973
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001974 snapshot = qemu_opt_get_bool(opts, "snapshot", 0);
Naphtali Sprei59f26892009-10-26 16:25:16 +02001975 ro = qemu_opt_get_bool(opts, "readonly", 0);
thse4bcb142007-12-02 04:51:10 +00001976
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02001977 file = qemu_opt_get(opts, "file");
1978 serial = qemu_opt_get(opts, "serial");
1979
1980 if ((buf = qemu_opt_get(opts, "if")) != NULL) {
bellardae45d362008-06-11 09:44:44 +00001981 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00001982 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00001983 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00001984 max_devs = MAX_IDE_DEVS;
1985 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00001986 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00001987 max_devs = MAX_SCSI_DEVS;
1988 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00001989 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00001990 max_devs = 0;
1991 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00001992 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00001993 max_devs = 0;
1994 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00001995 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00001996 max_devs = 0;
1997 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00001998 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00001999 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002000 } else if (!strcmp(buf, "virtio")) {
2001 type = IF_VIRTIO;
2002 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002003 } else if (!strcmp(buf, "xen")) {
2004 type = IF_XEN;
2005 max_devs = 0;
Gerd Hoffmanna8659e92009-07-31 12:25:39 +02002006 } else if (!strcmp(buf, "none")) {
2007 type = IF_NONE;
2008 max_devs = 0;
aliguori62d23ef2009-04-22 15:19:30 +00002009 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002010 fprintf(stderr, "qemu: unsupported bus type '%s'\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002011 return NULL;
thse4bcb142007-12-02 04:51:10 +00002012 }
2013 }
2014
thse4bcb142007-12-02 04:51:10 +00002015 if (cyls || heads || secs) {
Blue Swirl5afe3f02009-10-17 09:08:47 +00002016 if (cyls < 1 || (type == IF_IDE && cyls > 16383)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002017 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002018 return NULL;
thse4bcb142007-12-02 04:51:10 +00002019 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002020 if (heads < 1 || (type == IF_IDE && heads > 16)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002021 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002022 return NULL;
thse4bcb142007-12-02 04:51:10 +00002023 }
Blue Swirl5afe3f02009-10-17 09:08:47 +00002024 if (secs < 1 || (type == IF_IDE && secs > 63)) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002025 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002026 return NULL;
thse4bcb142007-12-02 04:51:10 +00002027 }
2028 }
2029
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002030 if ((buf = qemu_opt_get(opts, "trans")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002031 if (!cyls) {
2032 fprintf(stderr,
2033 "qemu: '%s' trans must be used with cyls,heads and secs\n",
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002034 buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002035 return NULL;
thse4bcb142007-12-02 04:51:10 +00002036 }
2037 if (!strcmp(buf, "none"))
2038 translation = BIOS_ATA_TRANSLATION_NONE;
2039 else if (!strcmp(buf, "lba"))
2040 translation = BIOS_ATA_TRANSLATION_LBA;
2041 else if (!strcmp(buf, "auto"))
2042 translation = BIOS_ATA_TRANSLATION_AUTO;
2043 else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002044 fprintf(stderr, "qemu: '%s' invalid translation type\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002045 return NULL;
thse4bcb142007-12-02 04:51:10 +00002046 }
2047 }
2048
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002049 if ((buf = qemu_opt_get(opts, "media")) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002050 if (!strcmp(buf, "disk")) {
2051 media = MEDIA_DISK;
2052 } else if (!strcmp(buf, "cdrom")) {
2053 if (cyls || secs || heads) {
2054 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002055 "qemu: '%s' invalid physical CHS format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002056 return NULL;
thse4bcb142007-12-02 04:51:10 +00002057 }
2058 media = MEDIA_CDROM;
2059 } else {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002060 fprintf(stderr, "qemu: '%s' invalid media\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002061 return NULL;
thse4bcb142007-12-02 04:51:10 +00002062 }
2063 }
2064
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002065 if ((buf = qemu_opt_get(opts, "cache")) != NULL) {
aliguori9f7965c2008-10-14 14:42:54 +00002066 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002067 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002068 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002069 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002070 else if (!strcmp(buf, "writeback"))
2071 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002072 else {
2073 fprintf(stderr, "qemu: invalid cache option\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002074 return NULL;
balrog33f00272007-12-24 14:33:24 +00002075 }
2076 }
2077
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002078#ifdef CONFIG_LINUX_AIO
2079 if ((buf = qemu_opt_get(opts, "aio")) != NULL) {
2080 if (!strcmp(buf, "threads"))
2081 aio = 0;
2082 else if (!strcmp(buf, "native"))
2083 aio = 1;
2084 else {
2085 fprintf(stderr, "qemu: invalid aio option\n");
2086 return NULL;
2087 }
2088 }
2089#endif
2090
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002091 if ((buf = qemu_opt_get(opts, "format")) != NULL) {
aurel32a1620fa2008-04-29 05:58:01 +00002092 if (strcmp(buf, "?") == 0) {
2093 fprintf(stderr, "qemu: Supported formats:");
2094 bdrv_iterate_format(bdrv_format_print, NULL);
2095 fprintf(stderr, "\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002096 return NULL;
aurel32a1620fa2008-04-29 05:58:01 +00002097 }
Markus Armbrustereb852012009-10-27 18:41:44 +01002098 drv = bdrv_find_whitelisted_format(buf);
aurel321e72d3b2008-04-28 20:26:45 +00002099 if (!drv) {
2100 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002101 return NULL;
aurel321e72d3b2008-04-28 20:26:45 +00002102 }
2103 }
2104
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002105 on_write_error = BLOCK_ERR_STOP_ENOSPC;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002106 if ((buf = qemu_opt_get(opts, "werror")) != NULL) {
aliguori869a5c62009-01-22 19:52:25 +00002107 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002108 fprintf(stderr, "werror is no supported by this format\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002109 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002110 }
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002111
2112 on_write_error = parse_block_error_action(buf, 0);
2113 if (on_write_error < 0) {
2114 return NULL;
2115 }
2116 }
2117
2118 on_read_error = BLOCK_ERR_REPORT;
2119 if ((buf = qemu_opt_get(opts, "rerror")) != NULL) {
Kevin Wolff35d68f2009-11-27 13:25:39 +01002120 if (type != IF_IDE && type != IF_VIRTIO) {
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002121 fprintf(stderr, "rerror is no supported by this format\n");
2122 return NULL;
2123 }
2124
2125 on_read_error = parse_block_error_action(buf, 1);
2126 if (on_read_error < 0) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002127 return NULL;
aliguori428c5702009-01-21 18:59:04 +00002128 }
2129 }
2130
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002131 if ((devaddr = qemu_opt_get(opts, "addr")) != NULL) {
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002132 if (type != IF_VIRTIO) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002133 fprintf(stderr, "addr is not supported\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002134 return NULL;
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002135 }
Markus Armbrusterc2cc47a2009-06-18 15:14:10 +02002136 }
2137
thse4bcb142007-12-02 04:51:10 +00002138 /* compute bus and unit according index */
2139
2140 if (index != -1) {
2141 if (bus_id != 0 || unit_id != -1) {
2142 fprintf(stderr,
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002143 "qemu: index cannot be used with bus and unit\n");
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002144 return NULL;
thse4bcb142007-12-02 04:51:10 +00002145 }
2146 if (max_devs == 0)
2147 {
2148 unit_id = index;
2149 bus_id = 0;
2150 } else {
2151 unit_id = index % max_devs;
2152 bus_id = index / max_devs;
2153 }
2154 }
2155
2156 /* if user doesn't specify a unit_id,
2157 * try to find the first free
2158 */
2159
2160 if (unit_id == -1) {
2161 unit_id = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002162 while (drive_get(type, bus_id, unit_id) != NULL) {
thse4bcb142007-12-02 04:51:10 +00002163 unit_id++;
2164 if (max_devs && unit_id >= max_devs) {
2165 unit_id -= max_devs;
2166 bus_id++;
2167 }
2168 }
2169 }
2170
2171 /* check unit id */
2172
2173 if (max_devs && unit_id >= max_devs) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002174 fprintf(stderr, "qemu: unit %d too big (max is %d)\n",
2175 unit_id, max_devs - 1);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002176 return NULL;
thse4bcb142007-12-02 04:51:10 +00002177 }
2178
2179 /*
2180 * ignore multiple definitions
2181 */
2182
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002183 if (drive_get(type, bus_id, unit_id) != NULL) {
2184 *fatal_error = 0;
2185 return NULL;
2186 }
thse4bcb142007-12-02 04:51:10 +00002187
2188 /* init */
2189
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002190 dinfo = qemu_mallocz(sizeof(*dinfo));
Gerd Hoffmanne23d9c42009-07-31 12:25:34 +02002191 if ((buf = qemu_opts_id(opts)) != NULL) {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002192 dinfo->id = qemu_strdup(buf);
2193 } else {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002194 /* no id supplied -> create one */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002195 dinfo->id = qemu_mallocz(32);
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002196 if (type == IF_IDE || type == IF_SCSI)
2197 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2198 if (max_devs)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002199 snprintf(dinfo->id, 32, "%s%i%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002200 devname, bus_id, mediastr, unit_id);
2201 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002202 snprintf(dinfo->id, 32, "%s%s%i",
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002203 devname, mediastr, unit_id);
2204 }
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002205 dinfo->bdrv = bdrv_new(dinfo->id);
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002206 dinfo->devaddr = devaddr;
2207 dinfo->type = type;
2208 dinfo->bus = bus_id;
2209 dinfo->unit = unit_id;
Kevin Wolfe9b2e812009-11-27 13:25:37 +01002210 dinfo->on_read_error = on_read_error;
2211 dinfo->on_write_error = on_write_error;
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002212 dinfo->opts = opts;
2213 if (serial)
2214 strncpy(dinfo->serial, serial, sizeof(serial));
Blue Swirl72cf2d42009-09-12 07:36:22 +00002215 QTAILQ_INSERT_TAIL(&drives, dinfo, next);
thse4bcb142007-12-02 04:51:10 +00002216
thsf60d39b2007-12-17 03:55:57 +00002217 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002218 case IF_IDE:
2219 case IF_SCSI:
aliguori62d23ef2009-04-22 15:19:30 +00002220 case IF_XEN:
Gerd Hoffmannc2193312009-09-15 19:23:28 +00002221 case IF_NONE:
thse4bcb142007-12-02 04:51:10 +00002222 switch(media) {
2223 case MEDIA_DISK:
2224 if (cyls != 0) {
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002225 bdrv_set_geometry_hint(dinfo->bdrv, cyls, heads, secs);
2226 bdrv_set_translation_hint(dinfo->bdrv, translation);
thse4bcb142007-12-02 04:51:10 +00002227 }
2228 break;
2229 case MEDIA_CDROM:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002230 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_CDROM);
thse4bcb142007-12-02 04:51:10 +00002231 break;
2232 }
2233 break;
2234 case IF_SD:
2235 /* FIXME: This isn't really a floppy, but it's a reasonable
2236 approximation. */
2237 case IF_FLOPPY:
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002238 bdrv_set_type_hint(dinfo->bdrv, BDRV_TYPE_FLOPPY);
thse4bcb142007-12-02 04:51:10 +00002239 break;
2240 case IF_PFLASH:
2241 case IF_MTD:
2242 break;
Gerd Hoffmannd176c492009-07-31 12:25:41 +02002243 case IF_VIRTIO:
2244 /* add virtio block device */
2245 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
2246 qemu_opt_set(opts, "driver", "virtio-blk-pci");
2247 qemu_opt_set(opts, "drive", dinfo->id);
2248 if (devaddr)
2249 qemu_opt_set(opts, "addr", devaddr);
2250 break;
Paul Brookaae94602009-05-14 22:35:06 +01002251 case IF_COUNT:
2252 abort();
thse4bcb142007-12-02 04:51:10 +00002253 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002254 if (!file) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002255 *fatal_error = 0;
2256 return NULL;
2257 }
balrog33f00272007-12-24 14:33:24 +00002258 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002259 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002260 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002261 cache = 2; /* always use write-back with snapshot */
2262 }
2263 if (cache == 0) /* no caching */
2264 bdrv_flags |= BDRV_O_NOCACHE;
2265 else if (cache == 2) /* write-back */
2266 bdrv_flags |= BDRV_O_CACHE_WB;
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002267
2268 if (aio == 1) {
2269 bdrv_flags |= BDRV_O_NATIVE_AIO;
2270 } else {
2271 bdrv_flags &= ~BDRV_O_NATIVE_AIO;
2272 }
2273
Naphtali Sprei59f26892009-10-26 16:25:16 +02002274 if (ro == 1) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02002275 if (type != IF_SCSI && type != IF_VIRTIO && type != IF_FLOPPY) {
2276 fprintf(stderr, "qemu: readonly flag not supported for drive with this interface\n");
Naphtali Sprei59f26892009-10-26 16:25:16 +02002277 return NULL;
2278 }
Naphtali Sprei59f26892009-10-26 16:25:16 +02002279 }
Naphtali Spreib196b152010-01-17 16:48:12 +02002280 /*
2281 * cdrom is read-only. Set it now, after above interface checking
2282 * since readonly attribute not explicitly required, so no error.
2283 */
2284 if (media == MEDIA_CDROM) {
Naphtali Spreif5edb012010-01-17 16:48:13 +02002285 ro = 1;
Naphtali Spreib196b152010-01-17 16:48:12 +02002286 }
Naphtali Spreif5edb012010-01-17 16:48:13 +02002287 bdrv_flags |= ro ? 0 : BDRV_O_RDWR;
Naphtali Sprei59f26892009-10-26 16:25:16 +02002288
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002289 if (bdrv_open2(dinfo->bdrv, file, bdrv_flags, drv) < 0) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05002290 fprintf(stderr, "qemu: could not open disk image %s: %s\n",
2291 file, strerror(errno));
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002292 return NULL;
thse4bcb142007-12-02 04:51:10 +00002293 }
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +02002294
Gerd Hoffmann1dae12e2009-07-22 16:42:58 +02002295 if (bdrv_key_required(dinfo->bdrv))
aliguoric0f4ce72009-03-05 23:01:01 +00002296 autostart = 0;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002297 *fatal_error = 0;
2298 return dinfo;
thse4bcb142007-12-02 04:51:10 +00002299}
2300
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02002301static int drive_init_func(QemuOpts *opts, void *opaque)
2302{
2303 QEMUMachine *machine = opaque;
2304 int fatal_error = 0;
2305
2306 if (drive_init(opts, machine, &fatal_error) == NULL) {
2307 if (fatal_error)
2308 return 1;
2309 }
2310 return 0;
2311}
2312
2313static int drive_enable_snapshot(QemuOpts *opts, void *opaque)
2314{
2315 if (NULL == qemu_opt_get(opts, "snapshot")) {
2316 qemu_opt_set(opts, "snapshot", "on");
2317 }
2318 return 0;
2319}
2320
Jan Kiszka76e30d02009-07-02 00:19:02 +02002321void qemu_register_boot_set(QEMUBootSetHandler *func, void *opaque)
2322{
2323 boot_set_handler = func;
2324 boot_set_opaque = opaque;
2325}
2326
2327int qemu_boot_set(const char *boot_devices)
2328{
2329 if (!boot_set_handler) {
2330 return -EINVAL;
2331 }
2332 return boot_set_handler(boot_set_opaque, boot_devices);
2333}
2334
Jan Kiszkaef3adf62009-07-02 00:19:02 +02002335static int parse_bootdevices(char *devices)
2336{
2337 /* We just do some generic consistency checks */
2338 const char *p;
2339 int bitmap = 0;
2340
2341 for (p = devices; *p != '\0'; p++) {
2342 /* Allowed boot devices are:
2343 * a-b: floppy disk drives
2344 * c-f: IDE disk drives
2345 * g-m: machine implementation dependant drives
2346 * n-p: network devices
2347 * It's up to each machine implementation to check if the given boot
2348 * devices match the actual hardware implementation and firmware
2349 * features.
2350 */
2351 if (*p < 'a' || *p > 'p') {
2352 fprintf(stderr, "Invalid boot device '%c'\n", *p);
2353 exit(1);
2354 }
2355 if (bitmap & (1 << (*p - 'a'))) {
2356 fprintf(stderr, "Boot device '%c' was given twice\n", *p);
2357 exit(1);
2358 }
2359 bitmap |= 1 << (*p - 'a');
2360 }
2361 return bitmap;
2362}
2363
Jan Kiszkae0f084b2009-07-02 00:19:02 +02002364static void restore_boot_devices(void *opaque)
2365{
2366 char *standard_boot_devices = opaque;
2367
2368 qemu_boot_set(standard_boot_devices);
2369
2370 qemu_unregister_reset(restore_boot_devices, standard_boot_devices);
2371 qemu_free(standard_boot_devices);
2372}
2373
aliguori268a3622009-04-21 22:30:27 +00002374static void numa_add(const char *optarg)
2375{
2376 char option[128];
2377 char *endptr;
2378 unsigned long long value, endvalue;
2379 int nodenr;
2380
2381 optarg = get_opt_name(option, 128, optarg, ',') + 1;
2382 if (!strcmp(option, "node")) {
2383 if (get_param_value(option, 128, "nodeid", optarg) == 0) {
2384 nodenr = nb_numa_nodes;
2385 } else {
2386 nodenr = strtoull(option, NULL, 10);
2387 }
2388
2389 if (get_param_value(option, 128, "mem", optarg) == 0) {
2390 node_mem[nodenr] = 0;
2391 } else {
2392 value = strtoull(option, &endptr, 0);
2393 switch (*endptr) {
2394 case 0: case 'M': case 'm':
2395 value <<= 20;
2396 break;
2397 case 'G': case 'g':
2398 value <<= 30;
2399 break;
2400 }
2401 node_mem[nodenr] = value;
2402 }
2403 if (get_param_value(option, 128, "cpus", optarg) == 0) {
2404 node_cpumask[nodenr] = 0;
2405 } else {
2406 value = strtoull(option, &endptr, 10);
2407 if (value >= 64) {
2408 value = 63;
2409 fprintf(stderr, "only 64 CPUs in NUMA mode supported.\n");
2410 } else {
2411 if (*endptr == '-') {
2412 endvalue = strtoull(endptr+1, &endptr, 10);
2413 if (endvalue >= 63) {
2414 endvalue = 62;
2415 fprintf(stderr,
2416 "only 63 CPUs in NUMA mode supported.\n");
2417 }
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01002418 value = (2ULL << endvalue) - (1ULL << value);
aliguori268a3622009-04-21 22:30:27 +00002419 } else {
Paolo Bonzini0dfbd512010-02-04 14:31:50 +01002420 value = 1ULL << value;
aliguori268a3622009-04-21 22:30:27 +00002421 }
2422 }
2423 node_cpumask[nodenr] = value;
2424 }
2425 nb_numa_nodes++;
2426 }
2427 return;
2428}
2429
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002430static void smp_parse(const char *optarg)
2431{
2432 int smp, sockets = 0, threads = 0, cores = 0;
2433 char *endptr;
2434 char option[128];
2435
2436 smp = strtoul(optarg, &endptr, 10);
2437 if (endptr != optarg) {
2438 if (*endptr == ',') {
2439 endptr++;
2440 }
2441 }
2442 if (get_param_value(option, 128, "sockets", endptr) != 0)
2443 sockets = strtoull(option, NULL, 10);
2444 if (get_param_value(option, 128, "cores", endptr) != 0)
2445 cores = strtoull(option, NULL, 10);
2446 if (get_param_value(option, 128, "threads", endptr) != 0)
2447 threads = strtoull(option, NULL, 10);
2448 if (get_param_value(option, 128, "maxcpus", endptr) != 0)
2449 max_cpus = strtoull(option, NULL, 10);
2450
2451 /* compute missing values, prefer sockets over cores over threads */
2452 if (smp == 0 || sockets == 0) {
2453 sockets = sockets > 0 ? sockets : 1;
2454 cores = cores > 0 ? cores : 1;
2455 threads = threads > 0 ? threads : 1;
2456 if (smp == 0) {
2457 smp = cores * threads * sockets;
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002458 }
2459 } else {
2460 if (cores == 0) {
2461 threads = threads > 0 ? threads : 1;
2462 cores = smp / (sockets * threads);
2463 } else {
Amit Shah5cdc9b72010-01-13 16:24:42 +05302464 if (sockets) {
Andre Przywaradc6b1c02009-08-19 15:42:40 +02002465 threads = smp / (cores * sockets);
2466 }
2467 }
2468 }
2469 smp_cpus = smp;
2470 smp_cores = cores > 0 ? cores : 1;
2471 smp_threads = threads > 0 ? threads : 1;
2472 if (max_cpus == 0)
2473 max_cpus = smp_cpus;
2474}
2475
bellard330d0412003-07-26 18:11:40 +00002476/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002477/* USB devices */
2478
aliguoric0f4ce72009-03-05 23:01:01 +00002479static int usb_device_add(const char *devname, int is_hotplug)
bellarda594cfb2005-11-06 16:13:29 +00002480{
2481 const char *p;
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002482 USBDevice *dev = NULL;
bellarda594cfb2005-11-06 16:13:29 +00002483
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002484 if (!usb_enabled)
bellarda594cfb2005-11-06 16:13:29 +00002485 return -1;
2486
Gerd Hoffmann0958b4c2009-10-26 15:56:45 +01002487 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2488 dev = usbdevice_create(devname);
2489 if (dev)
2490 goto done;
2491
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002492 /* the other ones */
bellarda594cfb2005-11-06 16:13:29 +00002493 if (strstart(devname, "host:", &p)) {
2494 dev = usb_host_device_open(p);
balrogdc72ac12008-11-09 00:04:26 +00002495 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2496 dev = usb_bt_init(devname[2] ? hci_init(p) :
2497 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002498 } else {
2499 return -1;
2500 }
pbrook0d92ed32006-05-21 16:30:15 +00002501 if (!dev)
2502 return -1;
2503
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002504done:
bellarda594cfb2005-11-06 16:13:29 +00002505 return 0;
2506}
2507
aliguori1f3870a2008-08-21 19:27:48 +00002508static int usb_device_del(const char *devname)
2509{
2510 int bus_num, addr;
2511 const char *p;
2512
aliguori5d0c5752008-09-14 01:07:41 +00002513 if (strstart(devname, "host:", &p))
2514 return usb_host_device_close(p);
2515
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002516 if (!usb_enabled)
aliguori1f3870a2008-08-21 19:27:48 +00002517 return -1;
2518
2519 p = strchr(devname, '.');
2520 if (!p)
2521 return -1;
2522 bus_num = strtoul(devname, NULL, 0);
2523 addr = strtoul(p + 1, NULL, 0);
2524
Gerd Hoffmanna5d2f722009-08-31 14:24:00 +02002525 return usb_device_delete_addr(bus_num, addr);
aliguori1f3870a2008-08-21 19:27:48 +00002526}
2527
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002528static int usb_parse(const char *cmdline)
2529{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08002530 int r;
2531 r = usb_device_add(cmdline, 0);
2532 if (r < 0) {
2533 fprintf(stderr, "qemu: could not add USB device '%s'\n", cmdline);
2534 }
2535 return r;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02002536}
2537
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002538void do_usb_add(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002539{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08002540 const char *devname = qdict_get_str(qdict, "devname");
2541 if (usb_device_add(devname, 1) < 0) {
2542 qemu_error("could not add USB device '%s'\n", devname);
2543 }
bellarda594cfb2005-11-06 16:13:29 +00002544}
2545
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002546void do_usb_del(Monitor *mon, const QDict *qdict)
bellarda594cfb2005-11-06 16:13:29 +00002547{
Scott Tsai59d1c1c2009-12-23 04:30:18 +08002548 const char *devname = qdict_get_str(qdict, "devname");
2549 if (usb_device_del(devname) < 0) {
2550 qemu_error("could not delete USB device '%s'\n", devname);
2551 }
bellarda594cfb2005-11-06 16:13:29 +00002552}
2553
bellardf7cce892004-12-08 22:21:25 +00002554/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002555/* PCMCIA/Cardbus */
2556
2557static struct pcmcia_socket_entry_s {
Paul Brookbc24a222009-05-10 01:44:56 +01002558 PCMCIASocket *socket;
balrog201a51f2007-04-30 00:51:09 +00002559 struct pcmcia_socket_entry_s *next;
2560} *pcmcia_sockets = 0;
2561
Paul Brookbc24a222009-05-10 01:44:56 +01002562void pcmcia_socket_register(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002563{
2564 struct pcmcia_socket_entry_s *entry;
2565
2566 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2567 entry->socket = socket;
2568 entry->next = pcmcia_sockets;
2569 pcmcia_sockets = entry;
2570}
2571
Paul Brookbc24a222009-05-10 01:44:56 +01002572void pcmcia_socket_unregister(PCMCIASocket *socket)
balrog201a51f2007-04-30 00:51:09 +00002573{
2574 struct pcmcia_socket_entry_s *entry, **ptr;
2575
2576 ptr = &pcmcia_sockets;
2577 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2578 if (entry->socket == socket) {
2579 *ptr = entry->next;
2580 qemu_free(entry);
2581 }
2582}
2583
aliguori376253e2009-03-05 23:01:23 +00002584void pcmcia_info(Monitor *mon)
balrog201a51f2007-04-30 00:51:09 +00002585{
2586 struct pcmcia_socket_entry_s *iter;
aliguori376253e2009-03-05 23:01:23 +00002587
balrog201a51f2007-04-30 00:51:09 +00002588 if (!pcmcia_sockets)
aliguori376253e2009-03-05 23:01:23 +00002589 monitor_printf(mon, "No PCMCIA sockets\n");
balrog201a51f2007-04-30 00:51:09 +00002590
2591 for (iter = pcmcia_sockets; iter; iter = iter->next)
aliguori376253e2009-03-05 23:01:23 +00002592 monitor_printf(mon, "%s: %s\n", iter->socket->slot_string,
2593 iter->socket->attached ? iter->socket->card_string :
2594 "Empty");
balrog201a51f2007-04-30 00:51:09 +00002595}
2596
2597/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002598/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002599
bellardc4b1fcc2004-03-14 21:44:30 +00002600typedef struct IOHandlerRecord {
2601 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002602 IOCanRWHandler *fd_read_poll;
2603 IOHandler *fd_read;
2604 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002605 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002606 void *opaque;
2607 /* temporary data */
2608 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002609 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002610} IOHandlerRecord;
2611
bellard8a7ddc32004-03-31 19:00:16 +00002612static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002613
bellard7c9d8e02005-11-15 22:16:05 +00002614/* XXX: fd_read_poll should be suppressed, but an API change is
2615 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002616int qemu_set_fd_handler2(int fd,
2617 IOCanRWHandler *fd_read_poll,
2618 IOHandler *fd_read,
2619 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002620 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002621{
bellard8a7ddc32004-03-31 19:00:16 +00002622 IOHandlerRecord **pioh, *ioh;
2623
bellard7c9d8e02005-11-15 22:16:05 +00002624 if (!fd_read && !fd_write) {
2625 pioh = &first_io_handler;
2626 for(;;) {
2627 ioh = *pioh;
2628 if (ioh == NULL)
2629 break;
2630 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002631 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002632 break;
2633 }
2634 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002635 }
bellard7c9d8e02005-11-15 22:16:05 +00002636 } else {
2637 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2638 if (ioh->fd == fd)
2639 goto found;
2640 }
2641 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002642 ioh->next = first_io_handler;
2643 first_io_handler = ioh;
2644 found:
2645 ioh->fd = fd;
2646 ioh->fd_read_poll = fd_read_poll;
2647 ioh->fd_read = fd_read;
2648 ioh->fd_write = fd_write;
2649 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002650 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002651 }
bellard7c9d8e02005-11-15 22:16:05 +00002652 return 0;
2653}
2654
ths5fafdf22007-09-16 21:08:06 +00002655int qemu_set_fd_handler(int fd,
2656 IOHandler *fd_read,
2657 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002658 void *opaque)
2659{
2660 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002661}
2662
aliguori56f3a5d2008-10-31 18:07:17 +00002663#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002664/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002665/* Polling handling */
2666
2667typedef struct PollingEntry {
2668 PollingFunc *func;
2669 void *opaque;
2670 struct PollingEntry *next;
2671} PollingEntry;
2672
2673static PollingEntry *first_polling_entry;
2674
2675int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2676{
2677 PollingEntry **ppe, *pe;
2678 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002679 pe->func = func;
2680 pe->opaque = opaque;
2681 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2682 *ppe = pe;
2683 return 0;
2684}
2685
2686void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2687{
2688 PollingEntry **ppe, *pe;
2689 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2690 pe = *ppe;
2691 if (pe->func == func && pe->opaque == opaque) {
2692 *ppe = pe->next;
2693 qemu_free(pe);
2694 break;
2695 }
2696 }
2697}
2698
bellarda18e5242006-06-25 17:18:27 +00002699/***********************************************************/
2700/* Wait objects support */
2701typedef struct WaitObjects {
2702 int num;
2703 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2704 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2705 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2706} WaitObjects;
2707
2708static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002709
bellarda18e5242006-06-25 17:18:27 +00002710int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2711{
2712 WaitObjects *w = &wait_objects;
2713
2714 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2715 return -1;
2716 w->events[w->num] = handle;
2717 w->func[w->num] = func;
2718 w->opaque[w->num] = opaque;
2719 w->num++;
2720 return 0;
2721}
2722
2723void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2724{
2725 int i, found;
2726 WaitObjects *w = &wait_objects;
2727
2728 found = 0;
2729 for (i = 0; i < w->num; i++) {
2730 if (w->events[i] == handle)
2731 found = 1;
2732 if (found) {
2733 w->events[i] = w->events[i + 1];
2734 w->func[i] = w->func[i + 1];
2735 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002736 }
bellarda18e5242006-06-25 17:18:27 +00002737 }
2738 if (found)
2739 w->num--;
2740}
2741#endif
2742
bellard8a7ddc32004-03-31 19:00:16 +00002743/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002744/* ram save/restore */
2745
Juan Quintela94fb0902009-09-10 03:04:23 +02002746#define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
aliguori475e4272008-10-06 20:21:51 +00002747#define RAM_SAVE_FLAG_COMPRESS 0x02
2748#define RAM_SAVE_FLAG_MEM_SIZE 0x04
2749#define RAM_SAVE_FLAG_PAGE 0x08
2750#define RAM_SAVE_FLAG_EOS 0x10
2751
2752static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00002753{
aliguori475e4272008-10-06 20:21:51 +00002754 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
2755 uint32_t *array = (uint32_t *)page;
2756 int i;
ths3b46e622007-09-17 08:09:54 +00002757
aliguori475e4272008-10-06 20:21:51 +00002758 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
2759 if (array[i] != val)
2760 return 0;
bellardc88676f2006-08-06 13:36:11 +00002761 }
aliguori475e4272008-10-06 20:21:51 +00002762
2763 return 1;
bellardc88676f2006-08-06 13:36:11 +00002764}
2765
aliguori475e4272008-10-06 20:21:51 +00002766static int ram_save_block(QEMUFile *f)
2767{
Anthony Liguoric227f092009-10-01 16:12:16 -05002768 static ram_addr_t current_addr = 0;
2769 ram_addr_t saved_addr = current_addr;
2770 ram_addr_t addr = 0;
aliguori475e4272008-10-06 20:21:51 +00002771 int found = 0;
2772
pbrook94a6b542009-04-11 17:15:54 +00002773 while (addr < last_ram_offset) {
aliguori475e4272008-10-06 20:21:51 +00002774 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
pbrook5579c7f2009-04-11 14:47:08 +00002775 uint8_t *p;
aliguori475e4272008-10-06 20:21:51 +00002776
2777 cpu_physical_memory_reset_dirty(current_addr,
2778 current_addr + TARGET_PAGE_SIZE,
2779 MIGRATION_DIRTY_FLAG);
2780
pbrook5579c7f2009-04-11 14:47:08 +00002781 p = qemu_get_ram_ptr(current_addr);
aliguori475e4272008-10-06 20:21:51 +00002782
pbrook5579c7f2009-04-11 14:47:08 +00002783 if (is_dup_page(p, *p)) {
aliguori475e4272008-10-06 20:21:51 +00002784 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
pbrook5579c7f2009-04-11 14:47:08 +00002785 qemu_put_byte(f, *p);
aliguori475e4272008-10-06 20:21:51 +00002786 } else {
2787 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
pbrook5579c7f2009-04-11 14:47:08 +00002788 qemu_put_buffer(f, p, TARGET_PAGE_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002789 }
2790
2791 found = 1;
2792 break;
2793 }
2794 addr += TARGET_PAGE_SIZE;
pbrook94a6b542009-04-11 17:15:54 +00002795 current_addr = (saved_addr + addr) % last_ram_offset;
aliguori475e4272008-10-06 20:21:51 +00002796 }
2797
2798 return found;
2799}
2800
Jan Kiszka84307932009-12-02 11:29:38 +01002801static uint64_t bytes_transferred;
aliguori475e4272008-10-06 20:21:51 +00002802
Anthony Liguoric227f092009-10-01 16:12:16 -05002803static ram_addr_t ram_save_remaining(void)
aliguori475e4272008-10-06 20:21:51 +00002804{
Anthony Liguoric227f092009-10-01 16:12:16 -05002805 ram_addr_t addr;
2806 ram_addr_t count = 0;
aliguori475e4272008-10-06 20:21:51 +00002807
pbrook94a6b542009-04-11 17:15:54 +00002808 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002809 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2810 count++;
2811 }
2812
2813 return count;
2814}
2815
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002816uint64_t ram_bytes_remaining(void)
2817{
2818 return ram_save_remaining() * TARGET_PAGE_SIZE;
2819}
2820
2821uint64_t ram_bytes_transferred(void)
2822{
2823 return bytes_transferred;
2824}
2825
2826uint64_t ram_bytes_total(void)
2827{
2828 return last_ram_offset;
2829}
2830
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002831static int ram_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
aliguori475e4272008-10-06 20:21:51 +00002832{
Anthony Liguoric227f092009-10-01 16:12:16 -05002833 ram_addr_t addr;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002834 uint64_t bytes_transferred_last;
2835 double bwidth = 0;
2836 uint64_t expected_time = 0;
aliguori475e4272008-10-06 20:21:51 +00002837
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002838 if (stage < 0) {
2839 cpu_physical_memory_set_dirty_tracking(0);
2840 return 0;
2841 }
2842
Jan Kiszka9fa06382009-05-22 23:51:45 +02002843 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX) != 0) {
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002844 qemu_file_set_error(f);
2845 return 0;
2846 }
2847
aliguori475e4272008-10-06 20:21:51 +00002848 if (stage == 1) {
Jan Kiszka84307932009-12-02 11:29:38 +01002849 bytes_transferred = 0;
2850
aliguori475e4272008-10-06 20:21:51 +00002851 /* Make sure all dirty bits are set */
pbrook94a6b542009-04-11 17:15:54 +00002852 for (addr = 0; addr < last_ram_offset; addr += TARGET_PAGE_SIZE) {
aliguori475e4272008-10-06 20:21:51 +00002853 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
2854 cpu_physical_memory_set_dirty(addr);
2855 }
Jan Kiszkab0a46a32009-05-02 00:22:51 +02002856
aliguori475e4272008-10-06 20:21:51 +00002857 /* Enable dirty memory tracking */
2858 cpu_physical_memory_set_dirty_tracking(1);
2859
pbrook94a6b542009-04-11 17:15:54 +00002860 qemu_put_be64(f, last_ram_offset | RAM_SAVE_FLAG_MEM_SIZE);
aliguori475e4272008-10-06 20:21:51 +00002861 }
2862
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002863 bytes_transferred_last = bytes_transferred;
Paolo Bonzini41c872b2010-01-26 10:31:46 +02002864 bwidth = qemu_get_clock_ns(rt_clock);
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002865
aliguori475e4272008-10-06 20:21:51 +00002866 while (!qemu_file_rate_limit(f)) {
2867 int ret;
2868
2869 ret = ram_save_block(f);
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002870 bytes_transferred += ret * TARGET_PAGE_SIZE;
aliguori475e4272008-10-06 20:21:51 +00002871 if (ret == 0) /* no more blocks */
2872 break;
2873 }
2874
Paolo Bonzini41c872b2010-01-26 10:31:46 +02002875 bwidth = qemu_get_clock_ns(rt_clock) - bwidth;
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002876 bwidth = (bytes_transferred - bytes_transferred_last) / bwidth;
2877
2878 /* if we haven't transferred anything this round, force expected_time to a
2879 * a very high value, but without crashing */
2880 if (bwidth == 0)
2881 bwidth = 0.000001;
2882
aliguori475e4272008-10-06 20:21:51 +00002883 /* try transferring iterative blocks of memory */
aliguori475e4272008-10-06 20:21:51 +00002884 if (stage == 3) {
aliguori475e4272008-10-06 20:21:51 +00002885 /* flush all remaining blocks regardless of rate limiting */
Glauber Costa9f9e28c2009-05-21 17:38:01 -04002886 while (ram_save_block(f) != 0) {
2887 bytes_transferred += TARGET_PAGE_SIZE;
2888 }
aliguori8215e912009-04-05 19:30:55 +00002889 cpu_physical_memory_set_dirty_tracking(0);
aliguori475e4272008-10-06 20:21:51 +00002890 }
2891
2892 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
2893
Glauber Costaa0a3fd62009-05-28 15:22:57 -04002894 expected_time = ram_save_remaining() * TARGET_PAGE_SIZE / bwidth;
2895
2896 return (stage == 2) && (expected_time <= migrate_max_downtime());
aliguori475e4272008-10-06 20:21:51 +00002897}
2898
aliguori475e4272008-10-06 20:21:51 +00002899static int ram_load(QEMUFile *f, void *opaque, int version_id)
2900{
Anthony Liguoric227f092009-10-01 16:12:16 -05002901 ram_addr_t addr;
aliguori475e4272008-10-06 20:21:51 +00002902 int flags;
2903
aliguori475e4272008-10-06 20:21:51 +00002904 if (version_id != 3)
2905 return -EINVAL;
2906
2907 do {
2908 addr = qemu_get_be64(f);
2909
2910 flags = addr & ~TARGET_PAGE_MASK;
2911 addr &= TARGET_PAGE_MASK;
2912
2913 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
pbrook94a6b542009-04-11 17:15:54 +00002914 if (addr != last_ram_offset)
aliguori475e4272008-10-06 20:21:51 +00002915 return -EINVAL;
2916 }
2917
aliguori475e4272008-10-06 20:21:51 +00002918 if (flags & RAM_SAVE_FLAG_COMPRESS) {
2919 uint8_t ch = qemu_get_byte(f);
Anthony Liguori779c6be2009-06-22 12:39:00 -05002920 memset(qemu_get_ram_ptr(addr), ch, TARGET_PAGE_SIZE);
2921#ifndef _WIN32
Anthony Liguori30868442009-06-17 16:46:12 -05002922 if (ch == 0 &&
2923 (!kvm_enabled() || kvm_has_sync_mmu())) {
2924 madvise(qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE, MADV_DONTNEED);
Anthony Liguori779c6be2009-06-22 12:39:00 -05002925 }
Anthony Liguori30868442009-06-17 16:46:12 -05002926#endif
Jan Kiszka9a743e52009-11-30 18:21:21 +01002927 } else if (flags & RAM_SAVE_FLAG_PAGE) {
pbrook5579c7f2009-04-11 14:47:08 +00002928 qemu_get_buffer(f, qemu_get_ram_ptr(addr), TARGET_PAGE_SIZE);
Jan Kiszka9a743e52009-11-30 18:21:21 +01002929 }
2930 if (qemu_file_has_error(f)) {
2931 return -EIO;
2932 }
aliguori475e4272008-10-06 20:21:51 +00002933 } while (!(flags & RAM_SAVE_FLAG_EOS));
2934
bellardc88676f2006-08-06 13:36:11 +00002935 return 0;
2936}
2937
aliguori9e472e12008-10-08 19:50:24 +00002938void qemu_service_io(void)
2939{
aliguorid9f75a42009-04-24 18:03:11 +00002940 qemu_notify_event();
aliguori9e472e12008-10-08 19:50:24 +00002941}
2942
bellard8a7ddc32004-03-31 19:00:16 +00002943/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00002944/* machine registration */
2945
blueswir1bdaf78e2008-10-04 07:24:27 +00002946static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00002947QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00002948
2949int qemu_register_machine(QEMUMachine *m)
2950{
2951 QEMUMachine **pm;
2952 pm = &first_machine;
2953 while (*pm != NULL)
2954 pm = &(*pm)->next;
2955 m->next = NULL;
2956 *pm = m;
2957 return 0;
2958}
2959
pbrook9596ebb2007-11-18 01:44:38 +00002960static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00002961{
2962 QEMUMachine *m;
2963
2964 for(m = first_machine; m != NULL; m = m->next) {
2965 if (!strcmp(m->name, name))
2966 return m;
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01002967 if (m->alias && !strcmp(m->alias, name))
2968 return m;
bellardcc1daa42005-06-05 14:49:17 +00002969 }
2970 return NULL;
2971}
2972
Anthony Liguori0c257432009-05-21 20:41:01 -05002973static QEMUMachine *find_default_machine(void)
2974{
2975 QEMUMachine *m;
2976
2977 for(m = first_machine; m != NULL; m = m->next) {
2978 if (m->is_default) {
2979 return m;
2980 }
2981 }
2982 return NULL;
2983}
2984
bellardcc1daa42005-06-05 14:49:17 +00002985/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002986/* main execution loop */
2987
pbrook9596ebb2007-11-18 01:44:38 +00002988static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00002989{
aliguori7d957bd2009-01-15 22:14:11 +00002990 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00002991 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00002992 DisplayChangeListener *dcl = ds->listeners;
2993
Sheng Yang62a27442010-01-26 19:21:16 +08002994 qemu_flush_coalesced_mmio_buffer();
aliguori7d957bd2009-01-15 22:14:11 +00002995 dpy_refresh(ds);
2996
2997 while (dcl != NULL) {
2998 if (dcl->gui_timer_interval &&
2999 dcl->gui_timer_interval < interval)
3000 interval = dcl->gui_timer_interval;
3001 dcl = dcl->next;
3002 }
3003 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003004}
3005
blueswir19043b622009-01-21 19:28:13 +00003006static void nographic_update(void *opaque)
3007{
3008 uint64_t interval = GUI_REFRESH_INTERVAL;
3009
Sheng Yang62a27442010-01-26 19:21:16 +08003010 qemu_flush_coalesced_mmio_buffer();
blueswir19043b622009-01-21 19:28:13 +00003011 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3012}
3013
Jan Kiszkaea375f92010-03-01 19:10:30 +01003014void cpu_synchronize_all_states(void)
3015{
3016 CPUState *cpu;
3017
3018 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
3019 cpu_synchronize_state(cpu);
3020 }
3021}
3022
3023void cpu_synchronize_all_post_reset(void)
3024{
3025 CPUState *cpu;
3026
3027 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
3028 cpu_synchronize_post_reset(cpu);
3029 }
3030}
3031
3032void cpu_synchronize_all_post_init(void)
3033{
3034 CPUState *cpu;
3035
3036 for (cpu = first_cpu; cpu; cpu = cpu->next_cpu) {
3037 cpu_synchronize_post_init(cpu);
3038 }
3039}
3040
bellard0bd48852005-11-11 00:00:47 +00003041struct vm_change_state_entry {
3042 VMChangeStateHandler *cb;
3043 void *opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003044 QLIST_ENTRY (vm_change_state_entry) entries;
bellard0bd48852005-11-11 00:00:47 +00003045};
3046
Blue Swirl72cf2d42009-09-12 07:36:22 +00003047static QLIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
bellard0bd48852005-11-11 00:00:47 +00003048
3049VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3050 void *opaque)
3051{
3052 VMChangeStateEntry *e;
3053
3054 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003055
3056 e->cb = cb;
3057 e->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003058 QLIST_INSERT_HEAD(&vm_change_state_head, e, entries);
bellard0bd48852005-11-11 00:00:47 +00003059 return e;
3060}
3061
3062void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3063{
Blue Swirl72cf2d42009-09-12 07:36:22 +00003064 QLIST_REMOVE (e, entries);
bellard0bd48852005-11-11 00:00:47 +00003065 qemu_free (e);
3066}
3067
aliguori9781e042009-01-22 17:15:29 +00003068static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003069{
3070 VMChangeStateEntry *e;
3071
3072 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003073 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003074 }
3075}
3076
aliguorid6dc3d42009-04-24 18:04:07 +00003077static void resume_all_vcpus(void);
3078static void pause_all_vcpus(void);
3079
bellard8a7ddc32004-03-31 19:00:16 +00003080void vm_start(void)
3081{
3082 if (!vm_running) {
3083 cpu_enable_ticks();
3084 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003085 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003086 qemu_rearm_alarm_timer(alarm_timer);
aliguorid6dc3d42009-04-24 18:04:07 +00003087 resume_all_vcpus();
bellard8a7ddc32004-03-31 19:00:16 +00003088 }
3089}
3090
bellardbb0c6722004-06-20 12:37:32 +00003091/* reset/shutdown handler */
3092
3093typedef struct QEMUResetEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003094 QTAILQ_ENTRY(QEMUResetEntry) entry;
bellardbb0c6722004-06-20 12:37:32 +00003095 QEMUResetHandler *func;
3096 void *opaque;
bellardbb0c6722004-06-20 12:37:32 +00003097} QEMUResetEntry;
3098
Blue Swirl72cf2d42009-09-12 07:36:22 +00003099static QTAILQ_HEAD(reset_handlers, QEMUResetEntry) reset_handlers =
3100 QTAILQ_HEAD_INITIALIZER(reset_handlers);
bellardbb0c6722004-06-20 12:37:32 +00003101static int reset_requested;
3102static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003103static int powerdown_requested;
aliguorie5689022009-04-24 18:03:54 +00003104static int debug_requested;
aliguori6e29f5d2009-04-24 18:04:02 +00003105static int vmstop_requested;
bellardbb0c6722004-06-20 12:37:32 +00003106
aurel32cf7a2fe2008-03-18 06:53:05 +00003107int qemu_shutdown_requested(void)
3108{
3109 int r = shutdown_requested;
3110 shutdown_requested = 0;
3111 return r;
3112}
3113
3114int qemu_reset_requested(void)
3115{
3116 int r = reset_requested;
3117 reset_requested = 0;
3118 return r;
3119}
3120
3121int qemu_powerdown_requested(void)
3122{
3123 int r = powerdown_requested;
3124 powerdown_requested = 0;
3125 return r;
3126}
3127
aliguorie5689022009-04-24 18:03:54 +00003128static int qemu_debug_requested(void)
3129{
3130 int r = debug_requested;
3131 debug_requested = 0;
3132 return r;
3133}
3134
aliguori6e29f5d2009-04-24 18:04:02 +00003135static int qemu_vmstop_requested(void)
3136{
3137 int r = vmstop_requested;
3138 vmstop_requested = 0;
3139 return r;
3140}
3141
3142static void do_vm_stop(int reason)
3143{
3144 if (vm_running) {
3145 cpu_disable_ticks();
3146 vm_running = 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003147 pause_all_vcpus();
aliguori6e29f5d2009-04-24 18:04:02 +00003148 vm_state_notify(0, reason);
Luiz Capitulino6b8f8ff2010-02-25 12:06:59 -03003149 monitor_protocol_event(QEVENT_STOP, NULL);
aliguori6e29f5d2009-04-24 18:04:02 +00003150 }
3151}
3152
Jan Kiszkaa08d4362009-06-27 09:25:07 +02003153void qemu_register_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003154{
Jan Kiszka55ddfe82009-07-02 00:19:02 +02003155 QEMUResetEntry *re = qemu_mallocz(sizeof(QEMUResetEntry));
bellardbb0c6722004-06-20 12:37:32 +00003156
bellardbb0c6722004-06-20 12:37:32 +00003157 re->func = func;
3158 re->opaque = opaque;
Blue Swirl72cf2d42009-09-12 07:36:22 +00003159 QTAILQ_INSERT_TAIL(&reset_handlers, re, entry);
bellardbb0c6722004-06-20 12:37:32 +00003160}
3161
Jan Kiszkadda9b292009-07-02 00:19:02 +02003162void qemu_unregister_reset(QEMUResetHandler *func, void *opaque)
bellardbb0c6722004-06-20 12:37:32 +00003163{
3164 QEMUResetEntry *re;
3165
Blue Swirl72cf2d42009-09-12 07:36:22 +00003166 QTAILQ_FOREACH(re, &reset_handlers, entry) {
Jan Kiszkadda9b292009-07-02 00:19:02 +02003167 if (re->func == func && re->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00003168 QTAILQ_REMOVE(&reset_handlers, re, entry);
Jan Kiszkadda9b292009-07-02 00:19:02 +02003169 qemu_free(re);
3170 return;
3171 }
3172 }
3173}
3174
3175void qemu_system_reset(void)
3176{
3177 QEMUResetEntry *re, *nre;
3178
3179 /* reset all devices */
Blue Swirl72cf2d42009-09-12 07:36:22 +00003180 QTAILQ_FOREACH_SAFE(re, &reset_handlers, entry, nre) {
bellardbb0c6722004-06-20 12:37:32 +00003181 re->func(re->opaque);
3182 }
Luiz Capitulino81d9b782010-03-10 09:06:34 -06003183 monitor_protocol_event(QEVENT_RESET, NULL);
Jan Kiszkaea375f92010-03-01 19:10:30 +01003184 cpu_synchronize_all_post_reset();
bellardbb0c6722004-06-20 12:37:32 +00003185}
3186
3187void qemu_system_reset_request(void)
3188{
bellardd1beab82006-10-02 19:44:22 +00003189 if (no_reboot) {
3190 shutdown_requested = 1;
3191 } else {
3192 reset_requested = 1;
3193 }
aliguorid9f75a42009-04-24 18:03:11 +00003194 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003195}
3196
3197void qemu_system_shutdown_request(void)
3198{
3199 shutdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003200 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003201}
3202
bellard34751872005-07-02 14:31:34 +00003203void qemu_system_powerdown_request(void)
3204{
3205 powerdown_requested = 1;
aliguorid9f75a42009-04-24 18:03:11 +00003206 qemu_notify_event();
3207}
3208
aliguorid6dc3d42009-04-24 18:04:07 +00003209#ifdef CONFIG_IOTHREAD
3210static void qemu_system_vmstop_request(int reason)
aliguorid9f75a42009-04-24 18:03:11 +00003211{
aliguorid6dc3d42009-04-24 18:04:07 +00003212 vmstop_requested = reason;
3213 qemu_notify_event();
bellardbb0c6722004-06-20 12:37:32 +00003214}
aliguorid6dc3d42009-04-24 18:04:07 +00003215#endif
bellardbb0c6722004-06-20 12:37:32 +00003216
aliguori50317c72009-04-24 18:03:29 +00003217#ifndef _WIN32
3218static int io_thread_fd = -1;
3219
3220static void qemu_event_increment(void)
3221{
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01003222 /* Write 8 bytes to be compatible with eventfd. */
3223 static uint64_t val = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003224 ssize_t ret;
aliguori50317c72009-04-24 18:03:29 +00003225
3226 if (io_thread_fd == -1)
3227 return;
3228
Paolo Bonzini652ce2d2010-02-02 20:33:10 +01003229 do {
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01003230 ret = write(io_thread_fd, &val, sizeof(val));
Paolo Bonzini652ce2d2010-02-02 20:33:10 +01003231 } while (ret < 0 && errno == EINTR);
3232
3233 /* EAGAIN is fine, a read must be pending. */
3234 if (ret < 0 && errno != EAGAIN) {
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01003235 fprintf(stderr, "qemu_event_increment: write() filed: %s\n",
3236 strerror(errno));
3237 exit (1);
3238 }
aliguori50317c72009-04-24 18:03:29 +00003239}
3240
3241static void qemu_event_read(void *opaque)
3242{
3243 int fd = (unsigned long)opaque;
3244 ssize_t len;
Paolo Bonzini1d0f0d92010-02-02 20:33:09 +01003245 char buffer[512];
aliguori50317c72009-04-24 18:03:29 +00003246
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01003247 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
aliguori50317c72009-04-24 18:03:29 +00003248 do {
aliguori50317c72009-04-24 18:03:29 +00003249 len = read(fd, buffer, sizeof(buffer));
Paolo Bonzini1d0f0d92010-02-02 20:33:09 +01003250 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
aliguori50317c72009-04-24 18:03:29 +00003251}
3252
3253static int qemu_event_init(void)
3254{
3255 int err;
3256 int fds[2];
3257
Paolo Bonzinif3dfda62010-02-11 00:23:46 +01003258 err = qemu_eventfd(fds);
aliguori50317c72009-04-24 18:03:29 +00003259 if (err == -1)
3260 return -errno;
3261
3262 err = fcntl_setfl(fds[0], O_NONBLOCK);
3263 if (err < 0)
3264 goto fail;
3265
3266 err = fcntl_setfl(fds[1], O_NONBLOCK);
3267 if (err < 0)
3268 goto fail;
3269
3270 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
3271 (void *)(unsigned long)fds[0]);
3272
3273 io_thread_fd = fds[1];
Jan Kiszkaa7e21212009-04-29 18:38:28 +00003274 return 0;
3275
aliguori50317c72009-04-24 18:03:29 +00003276fail:
3277 close(fds[0]);
3278 close(fds[1]);
3279 return err;
3280}
3281#else
3282HANDLE qemu_event_handle;
3283
3284static void dummy_event_handler(void *opaque)
3285{
3286}
3287
3288static int qemu_event_init(void)
3289{
3290 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
3291 if (!qemu_event_handle) {
Blue Swirl20889d42009-09-27 20:03:56 +00003292 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
aliguori50317c72009-04-24 18:03:29 +00003293 return -1;
3294 }
3295 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
3296 return 0;
3297}
3298
3299static void qemu_event_increment(void)
3300{
malcde1c90c2009-09-27 14:38:18 +04003301 if (!SetEvent(qemu_event_handle)) {
Blue Swirl20889d42009-09-27 20:03:56 +00003302 fprintf(stderr, "qemu_event_increment: SetEvent failed: %ld\n",
malcde1c90c2009-09-27 14:38:18 +04003303 GetLastError());
3304 exit (1);
3305 }
aliguori50317c72009-04-24 18:03:29 +00003306}
3307#endif
3308
aliguorid6dc3d42009-04-24 18:04:07 +00003309static int cpu_can_run(CPUState *env)
3310{
3311 if (env->stop)
3312 return 0;
3313 if (env->stopped)
3314 return 0;
Marcelo Tosattic5f32c92010-02-03 21:44:17 -02003315 if (!vm_running)
3316 return 0;
aliguorid6dc3d42009-04-24 18:04:07 +00003317 return 1;
3318}
3319
3320#ifndef CONFIG_IOTHREAD
aliguori3fcf7b62009-04-24 18:03:25 +00003321static int qemu_init_main_loop(void)
3322{
aliguori50317c72009-04-24 18:03:29 +00003323 return qemu_event_init();
aliguori3fcf7b62009-04-24 18:03:25 +00003324}
3325
aliguori0bf46a42009-04-24 18:03:41 +00003326void qemu_init_vcpu(void *_env)
3327{
3328 CPUState *env = _env;
3329
Andre Przywaradc6b1c02009-08-19 15:42:40 +02003330 env->nr_cores = smp_cores;
3331 env->nr_threads = smp_threads;
Jiri Denemark3f7638e2010-01-05 17:26:34 +01003332 if (kvm_enabled())
3333 kvm_init_vcpu(env);
aliguori0bf46a42009-04-24 18:03:41 +00003334 return;
3335}
3336
aliguori8edac962009-04-24 18:03:45 +00003337int qemu_cpu_self(void *env)
3338{
3339 return 1;
3340}
3341
aliguorid6dc3d42009-04-24 18:04:07 +00003342static void resume_all_vcpus(void)
3343{
3344}
3345
3346static void pause_all_vcpus(void)
3347{
3348}
3349
aliguori8edac962009-04-24 18:03:45 +00003350void qemu_cpu_kick(void *env)
3351{
3352 return;
3353}
3354
aliguorid6dc3d42009-04-24 18:04:07 +00003355void qemu_notify_event(void)
3356{
3357 CPUState *env = cpu_single_env;
3358
3359 if (env) {
3360 cpu_exit(env);
Anthony Liguori4a1418e2009-08-10 17:07:24 -05003361 }
aliguorid6dc3d42009-04-24 18:04:07 +00003362}
3363
Glauber Costad549db52009-10-07 16:38:03 -03003364void qemu_mutex_lock_iothread(void) {}
3365void qemu_mutex_unlock_iothread(void) {}
aliguori48708522009-04-24 18:03:49 +00003366
aliguori6e29f5d2009-04-24 18:04:02 +00003367void vm_stop(int reason)
3368{
3369 do_vm_stop(reason);
3370}
3371
aliguorid6dc3d42009-04-24 18:04:07 +00003372#else /* CONFIG_IOTHREAD */
3373
3374#include "qemu-thread.h"
3375
3376QemuMutex qemu_global_mutex;
3377static QemuMutex qemu_fair_mutex;
3378
3379static QemuThread io_thread;
3380
3381static QemuThread *tcg_cpu_thread;
3382static QemuCond *tcg_halt_cond;
3383
3384static int qemu_system_ready;
3385/* cpu creation */
3386static QemuCond qemu_cpu_cond;
3387/* system init */
3388static QemuCond qemu_system_cond;
3389static QemuCond qemu_pause_cond;
3390
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003391static void tcg_block_io_signals(void);
3392static void kvm_block_io_signals(CPUState *env);
aliguorid6dc3d42009-04-24 18:04:07 +00003393static void unblock_io_signals(void);
3394static int tcg_has_work(void);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02003395static int cpu_has_work(CPUState *env);
aliguorid6dc3d42009-04-24 18:04:07 +00003396
3397static int qemu_init_main_loop(void)
3398{
3399 int ret;
3400
3401 ret = qemu_event_init();
3402 if (ret)
3403 return ret;
3404
3405 qemu_cond_init(&qemu_pause_cond);
3406 qemu_mutex_init(&qemu_fair_mutex);
3407 qemu_mutex_init(&qemu_global_mutex);
3408 qemu_mutex_lock(&qemu_global_mutex);
3409
3410 unblock_io_signals();
3411 qemu_thread_self(&io_thread);
3412
3413 return 0;
3414}
3415
Marcelo Tosattifed6c342010-02-17 20:14:41 -02003416static void qemu_wait_io_event_common(CPUState *env)
3417{
3418 if (env->stop) {
3419 env->stop = 0;
3420 env->stopped = 1;
3421 qemu_cond_signal(&qemu_pause_cond);
3422 }
3423}
3424
aliguorid6dc3d42009-04-24 18:04:07 +00003425static void qemu_wait_io_event(CPUState *env)
3426{
3427 while (!tcg_has_work())
3428 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3429
3430 qemu_mutex_unlock(&qemu_global_mutex);
3431
3432 /*
3433 * Users of qemu_global_mutex can be starved, having no chance
3434 * to acquire it since this path will get to it first.
3435 * So use another lock to provide fairness.
3436 */
3437 qemu_mutex_lock(&qemu_fair_mutex);
3438 qemu_mutex_unlock(&qemu_fair_mutex);
3439
3440 qemu_mutex_lock(&qemu_global_mutex);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02003441 qemu_wait_io_event_common(env);
3442}
3443
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003444static void qemu_kvm_eat_signal(CPUState *env, int timeout)
3445{
3446 struct timespec ts;
3447 int r, e;
3448 siginfo_t siginfo;
3449 sigset_t waitset;
3450
3451 ts.tv_sec = timeout / 1000;
3452 ts.tv_nsec = (timeout % 1000) * 1000000;
3453
3454 sigemptyset(&waitset);
3455 sigaddset(&waitset, SIG_IPI);
3456
3457 qemu_mutex_unlock(&qemu_global_mutex);
3458 r = sigtimedwait(&waitset, &siginfo, &ts);
3459 e = errno;
3460 qemu_mutex_lock(&qemu_global_mutex);
3461
3462 if (r == -1 && !(e == EAGAIN || e == EINTR)) {
3463 fprintf(stderr, "sigtimedwait: %s\n", strerror(e));
3464 exit(1);
aliguorid6dc3d42009-04-24 18:04:07 +00003465 }
3466}
3467
Marcelo Tosattifed6c342010-02-17 20:14:41 -02003468static void qemu_kvm_wait_io_event(CPUState *env)
3469{
3470 while (!cpu_has_work(env))
3471 qemu_cond_timedwait(env->halt_cond, &qemu_global_mutex, 1000);
3472
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003473 qemu_kvm_eat_signal(env, 0);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02003474 qemu_wait_io_event_common(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003475}
3476
3477static int qemu_cpu_exec(CPUState *env);
3478
3479static void *kvm_cpu_thread_fn(void *arg)
3480{
3481 CPUState *env = arg;
3482
aliguorid6dc3d42009-04-24 18:04:07 +00003483 qemu_thread_self(env->thread);
Jean-Christophe DUBOIS321c1cb2009-09-02 23:59:04 +02003484 if (kvm_enabled())
3485 kvm_init_vcpu(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003486
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003487 kvm_block_io_signals(env);
3488
aliguorid6dc3d42009-04-24 18:04:07 +00003489 /* signal CPU creation */
3490 qemu_mutex_lock(&qemu_global_mutex);
3491 env->created = 1;
3492 qemu_cond_signal(&qemu_cpu_cond);
3493
3494 /* and wait for machine initialization */
3495 while (!qemu_system_ready)
3496 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3497
3498 while (1) {
3499 if (cpu_can_run(env))
3500 qemu_cpu_exec(env);
Marcelo Tosattifed6c342010-02-17 20:14:41 -02003501 qemu_kvm_wait_io_event(env);
aliguorid6dc3d42009-04-24 18:04:07 +00003502 }
3503
3504 return NULL;
3505}
3506
3507static void tcg_cpu_exec(void);
3508
3509static void *tcg_cpu_thread_fn(void *arg)
3510{
3511 CPUState *env = arg;
3512
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003513 tcg_block_io_signals();
aliguorid6dc3d42009-04-24 18:04:07 +00003514 qemu_thread_self(env->thread);
3515
3516 /* signal CPU creation */
3517 qemu_mutex_lock(&qemu_global_mutex);
3518 for (env = first_cpu; env != NULL; env = env->next_cpu)
3519 env->created = 1;
3520 qemu_cond_signal(&qemu_cpu_cond);
3521
3522 /* and wait for machine initialization */
3523 while (!qemu_system_ready)
3524 qemu_cond_timedwait(&qemu_system_cond, &qemu_global_mutex, 100);
3525
3526 while (1) {
3527 tcg_cpu_exec();
3528 qemu_wait_io_event(cur_cpu);
3529 }
3530
3531 return NULL;
3532}
3533
3534void qemu_cpu_kick(void *_env)
3535{
3536 CPUState *env = _env;
3537 qemu_cond_broadcast(env->halt_cond);
3538 if (kvm_enabled())
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003539 qemu_thread_signal(env->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003540}
3541
Glauber Costae5bc2012009-09-28 15:27:44 -03003542int qemu_cpu_self(void *_env)
aliguorid6dc3d42009-04-24 18:04:07 +00003543{
Glauber Costae5bc2012009-09-28 15:27:44 -03003544 CPUState *env = _env;
3545 QemuThread this;
3546
3547 qemu_thread_self(&this);
3548
3549 return qemu_thread_equal(&this, env->thread);
aliguorid6dc3d42009-04-24 18:04:07 +00003550}
3551
3552static void cpu_signal(int sig)
3553{
3554 if (cpu_single_env)
3555 cpu_exit(cpu_single_env);
3556}
3557
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003558static void tcg_block_io_signals(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003559{
3560 sigset_t set;
3561 struct sigaction sigact;
3562
3563 sigemptyset(&set);
3564 sigaddset(&set, SIGUSR2);
3565 sigaddset(&set, SIGIO);
3566 sigaddset(&set, SIGALRM);
Marcelo Tosattiadc8c962010-02-17 20:14:40 -02003567 sigaddset(&set, SIGCHLD);
aliguorid6dc3d42009-04-24 18:04:07 +00003568 pthread_sigmask(SIG_BLOCK, &set, NULL);
3569
3570 sigemptyset(&set);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003571 sigaddset(&set, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003572 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3573
3574 memset(&sigact, 0, sizeof(sigact));
3575 sigact.sa_handler = cpu_signal;
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003576 sigaction(SIG_IPI, &sigact, NULL);
3577}
3578
3579static void dummy_signal(int sig)
3580{
3581}
3582
3583static void kvm_block_io_signals(CPUState *env)
3584{
3585 int r;
3586 sigset_t set;
3587 struct sigaction sigact;
3588
3589 sigemptyset(&set);
3590 sigaddset(&set, SIGUSR2);
3591 sigaddset(&set, SIGIO);
3592 sigaddset(&set, SIGALRM);
3593 sigaddset(&set, SIGCHLD);
3594 sigaddset(&set, SIG_IPI);
3595 pthread_sigmask(SIG_BLOCK, &set, NULL);
3596
3597 pthread_sigmask(SIG_BLOCK, NULL, &set);
3598 sigdelset(&set, SIG_IPI);
3599
3600 memset(&sigact, 0, sizeof(sigact));
3601 sigact.sa_handler = dummy_signal;
3602 sigaction(SIG_IPI, &sigact, NULL);
3603
3604 r = kvm_set_signal_mask(env, &set);
3605 if (r) {
3606 fprintf(stderr, "kvm_set_signal_mask: %s\n", strerror(r));
3607 exit(1);
3608 }
aliguorid6dc3d42009-04-24 18:04:07 +00003609}
3610
3611static void unblock_io_signals(void)
3612{
3613 sigset_t set;
3614
3615 sigemptyset(&set);
3616 sigaddset(&set, SIGUSR2);
3617 sigaddset(&set, SIGIO);
3618 sigaddset(&set, SIGALRM);
3619 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
3620
3621 sigemptyset(&set);
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003622 sigaddset(&set, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003623 pthread_sigmask(SIG_BLOCK, &set, NULL);
3624}
3625
3626static void qemu_signal_lock(unsigned int msecs)
3627{
3628 qemu_mutex_lock(&qemu_fair_mutex);
3629
3630 while (qemu_mutex_trylock(&qemu_global_mutex)) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003631 qemu_thread_signal(tcg_cpu_thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003632 if (!qemu_mutex_timedlock(&qemu_global_mutex, msecs))
3633 break;
3634 }
3635 qemu_mutex_unlock(&qemu_fair_mutex);
3636}
3637
Glauber Costad549db52009-10-07 16:38:03 -03003638void qemu_mutex_lock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003639{
3640 if (kvm_enabled()) {
3641 qemu_mutex_lock(&qemu_fair_mutex);
3642 qemu_mutex_lock(&qemu_global_mutex);
3643 qemu_mutex_unlock(&qemu_fair_mutex);
3644 } else
3645 qemu_signal_lock(100);
3646}
3647
Glauber Costad549db52009-10-07 16:38:03 -03003648void qemu_mutex_unlock_iothread(void)
aliguorid6dc3d42009-04-24 18:04:07 +00003649{
3650 qemu_mutex_unlock(&qemu_global_mutex);
3651}
3652
3653static int all_vcpus_paused(void)
3654{
3655 CPUState *penv = first_cpu;
3656
3657 while (penv) {
3658 if (!penv->stopped)
3659 return 0;
3660 penv = (CPUState *)penv->next_cpu;
3661 }
3662
3663 return 1;
3664}
3665
3666static void pause_all_vcpus(void)
3667{
3668 CPUState *penv = first_cpu;
3669
3670 while (penv) {
3671 penv->stop = 1;
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003672 qemu_thread_signal(penv->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003673 qemu_cpu_kick(penv);
3674 penv = (CPUState *)penv->next_cpu;
3675 }
3676
3677 while (!all_vcpus_paused()) {
3678 qemu_cond_timedwait(&qemu_pause_cond, &qemu_global_mutex, 100);
3679 penv = first_cpu;
3680 while (penv) {
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003681 qemu_thread_signal(penv->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003682 penv = (CPUState *)penv->next_cpu;
3683 }
3684 }
3685}
3686
3687static void resume_all_vcpus(void)
3688{
3689 CPUState *penv = first_cpu;
3690
3691 while (penv) {
3692 penv->stop = 0;
3693 penv->stopped = 0;
Marcelo Tosatticc84de92010-02-17 20:14:42 -02003694 qemu_thread_signal(penv->thread, SIG_IPI);
aliguorid6dc3d42009-04-24 18:04:07 +00003695 qemu_cpu_kick(penv);
3696 penv = (CPUState *)penv->next_cpu;
3697 }
3698}
3699
3700static void tcg_init_vcpu(void *_env)
3701{
3702 CPUState *env = _env;
3703 /* share a single thread for all cpus with TCG */
3704 if (!tcg_cpu_thread) {
3705 env->thread = qemu_mallocz(sizeof(QemuThread));
3706 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3707 qemu_cond_init(env->halt_cond);
3708 qemu_thread_create(env->thread, tcg_cpu_thread_fn, env);
3709 while (env->created == 0)
3710 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3711 tcg_cpu_thread = env->thread;
3712 tcg_halt_cond = env->halt_cond;
3713 } else {
3714 env->thread = tcg_cpu_thread;
3715 env->halt_cond = tcg_halt_cond;
3716 }
3717}
3718
3719static void kvm_start_vcpu(CPUState *env)
3720{
aliguorid6dc3d42009-04-24 18:04:07 +00003721 env->thread = qemu_mallocz(sizeof(QemuThread));
3722 env->halt_cond = qemu_mallocz(sizeof(QemuCond));
3723 qemu_cond_init(env->halt_cond);
3724 qemu_thread_create(env->thread, kvm_cpu_thread_fn, env);
3725 while (env->created == 0)
3726 qemu_cond_timedwait(&qemu_cpu_cond, &qemu_global_mutex, 100);
3727}
3728
3729void qemu_init_vcpu(void *_env)
3730{
3731 CPUState *env = _env;
3732
Jiri Denemark3f7638e2010-01-05 17:26:34 +01003733 env->nr_cores = smp_cores;
3734 env->nr_threads = smp_threads;
aliguorid6dc3d42009-04-24 18:04:07 +00003735 if (kvm_enabled())
3736 kvm_start_vcpu(env);
3737 else
3738 tcg_init_vcpu(env);
3739}
3740
3741void qemu_notify_event(void)
3742{
3743 qemu_event_increment();
3744}
3745
3746void vm_stop(int reason)
3747{
3748 QemuThread me;
3749 qemu_thread_self(&me);
3750
3751 if (!qemu_thread_equal(&me, &io_thread)) {
3752 qemu_system_vmstop_request(reason);
3753 /*
3754 * FIXME: should not return to device code in case
3755 * vm_stop() has been requested.
3756 */
3757 if (cpu_single_env) {
3758 cpu_exit(cpu_single_env);
3759 cpu_single_env->stop = 1;
3760 }
3761 return;
3762 }
3763 do_vm_stop(reason);
3764}
3765
3766#endif
3767
3768
ths877cf882007-04-18 18:11:47 +00003769#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003770static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003771{
3772 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003773 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003774
bellardf3311102006-04-12 20:21:17 +00003775
3776 /* XXX: need to suppress polling by better using win32 events */
3777 ret = 0;
3778 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3779 ret |= pe->func(pe->opaque);
3780 }
thse6b1e552007-04-18 17:56:02 +00003781 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003782 int err;
3783 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003784
aliguori56f3a5d2008-10-31 18:07:17 +00003785 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003786 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3787 if (w->func[ret - WAIT_OBJECT_0])
3788 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003789
ths5fafdf22007-09-16 21:08:06 +00003790 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003791 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003792
thse6b1e552007-04-18 17:56:02 +00003793 /* Check if event is signaled */
3794 ret2 = WaitForSingleObject(w->events[i], 0);
3795 if(ret2 == WAIT_OBJECT_0) {
3796 if (w->func[i])
3797 w->func[i](w->opaque[i]);
3798 } else if (ret2 == WAIT_TIMEOUT) {
3799 } else {
3800 err = GetLastError();
3801 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003802 }
3803 }
bellarda18e5242006-06-25 17:18:27 +00003804 } else if (ret == WAIT_TIMEOUT) {
3805 } else {
3806 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003807 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003808 }
bellardf3311102006-04-12 20:21:17 +00003809 }
aliguori56f3a5d2008-10-31 18:07:17 +00003810
3811 *timeout = 0;
3812}
3813#else
blueswir169d64512008-12-07 19:30:18 +00003814static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003815{
3816}
bellardfd1dff42006-02-01 21:29:26 +00003817#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003818
3819void main_loop_wait(int timeout)
3820{
3821 IOHandlerRecord *ioh;
3822 fd_set rfds, wfds, xfds;
3823 int ret, nfds;
3824 struct timeval tv;
3825
3826 qemu_bh_update_timeout(&timeout);
3827
3828 host_main_loop_wait(&timeout);
3829
bellardfd1dff42006-02-01 21:29:26 +00003830 /* poll any events */
3831 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003832 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003833 FD_ZERO(&rfds);
3834 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003835 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003836 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003837 if (ioh->deleted)
3838 continue;
bellardfd1dff42006-02-01 21:29:26 +00003839 if (ioh->fd_read &&
3840 (!ioh->fd_read_poll ||
3841 ioh->fd_read_poll(ioh->opaque) != 0)) {
3842 FD_SET(ioh->fd, &rfds);
3843 if (ioh->fd > nfds)
3844 nfds = ioh->fd;
3845 }
3846 if (ioh->fd_write) {
3847 FD_SET(ioh->fd, &wfds);
3848 if (ioh->fd > nfds)
3849 nfds = ioh->fd;
3850 }
3851 }
ths3b46e622007-09-17 08:09:54 +00003852
aliguori56f3a5d2008-10-31 18:07:17 +00003853 tv.tv_sec = timeout / 1000;
3854 tv.tv_usec = (timeout % 1000) * 1000;
3855
Jan Kiszkad918f232009-06-24 14:42:30 +02003856 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3857
aliguori48708522009-04-24 18:03:49 +00003858 qemu_mutex_unlock_iothread();
bellarde0356492006-05-01 13:33:02 +00003859 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
aliguori48708522009-04-24 18:03:49 +00003860 qemu_mutex_lock_iothread();
bellardfd1dff42006-02-01 21:29:26 +00003861 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003862 IOHandlerRecord **pioh;
3863
3864 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003865 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003866 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003867 }
ths6ab43fd2007-08-25 01:34:19 +00003868 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003869 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003870 }
3871 }
thscafffd42007-02-28 21:59:44 +00003872
3873 /* remove deleted IO handlers */
3874 pioh = &first_io_handler;
3875 while (*pioh) {
3876 ioh = *pioh;
3877 if (ioh->deleted) {
3878 *pioh = ioh->next;
3879 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003880 } else
thscafffd42007-02-28 21:59:44 +00003881 pioh = &ioh->next;
3882 }
bellardfd1dff42006-02-01 21:29:26 +00003883 }
Jan Kiszkad918f232009-06-24 14:42:30 +02003884
3885 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
bellardc20709a2004-04-21 23:27:19 +00003886
aliguori50317c72009-04-24 18:03:29 +00003887 /* rearm timer, if not periodic */
3888 if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
3889 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
3890 qemu_rearm_alarm_timer(alarm_timer);
3891 }
3892
aliguori357c6922008-11-25 17:26:09 +00003893 /* vm time timers */
aliguorid6dc3d42009-04-24 18:04:07 +00003894 if (vm_running) {
3895 if (!cur_cpu || likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003896 qemu_run_timers(&active_timers[QEMU_CLOCK_VIRTUAL],
3897 qemu_get_clock(vm_clock));
aliguorid6dc3d42009-04-24 18:04:07 +00003898 }
aliguori357c6922008-11-25 17:26:09 +00003899
3900 /* real time timers */
Jan Kiszka0fdddf82009-09-15 13:36:04 +02003901 qemu_run_timers(&active_timers[QEMU_CLOCK_REALTIME],
aliguori357c6922008-11-25 17:26:09 +00003902 qemu_get_clock(rt_clock));
3903
Jan Kiszka21d5d122009-09-15 13:36:04 +02003904 qemu_run_timers(&active_timers[QEMU_CLOCK_HOST],
3905 qemu_get_clock(host_clock));
3906
pbrook423f0742007-05-23 00:06:54 +00003907 /* Check bottom-halves last in case any of the earlier events triggered
3908 them. */
3909 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003910
bellard5905b2e2004-08-01 21:53:26 +00003911}
3912
aliguori43b96852009-04-24 18:03:33 +00003913static int qemu_cpu_exec(CPUState *env)
bellard5905b2e2004-08-01 21:53:26 +00003914{
aliguori43b96852009-04-24 18:03:33 +00003915 int ret;
bellard89bfc102006-02-08 22:46:31 +00003916#ifdef CONFIG_PROFILER
3917 int64_t ti;
3918#endif
aliguori43b96852009-04-24 18:03:33 +00003919
3920#ifdef CONFIG_PROFILER
3921 ti = profile_getclock();
3922#endif
3923 if (use_icount) {
3924 int64_t count;
3925 int decr;
3926 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3927 env->icount_decr.u16.low = 0;
3928 env->icount_extra = 0;
3929 count = qemu_next_deadline();
3930 count = (count + (1 << icount_time_shift) - 1)
3931 >> icount_time_shift;
3932 qemu_icount += count;
3933 decr = (count > 0xffff) ? 0xffff : count;
3934 count -= decr;
3935 env->icount_decr.u16.low = decr;
3936 env->icount_extra = count;
3937 }
3938 ret = cpu_exec(env);
3939#ifdef CONFIG_PROFILER
3940 qemu_time += profile_getclock() - ti;
3941#endif
3942 if (use_icount) {
3943 /* Fold pending instructions back into the
3944 instruction counter, and clear the interrupt flag. */
3945 qemu_icount -= (env->icount_decr.u16.low
3946 + env->icount_extra);
3947 env->icount_decr.u32 = 0;
3948 env->icount_extra = 0;
3949 }
3950 return ret;
3951}
3952
aliguorie6e35b12009-04-24 18:03:57 +00003953static void tcg_cpu_exec(void)
3954{
aliguorid6dc3d42009-04-24 18:04:07 +00003955 int ret = 0;
aliguorie6e35b12009-04-24 18:03:57 +00003956
3957 if (next_cpu == NULL)
3958 next_cpu = first_cpu;
3959 for (; next_cpu != NULL; next_cpu = next_cpu->next_cpu) {
3960 CPUState *env = cur_cpu = next_cpu;
3961
aliguorie6e35b12009-04-24 18:03:57 +00003962 if (timer_alarm_pending) {
3963 timer_alarm_pending = 0;
3964 break;
3965 }
aliguorid6dc3d42009-04-24 18:04:07 +00003966 if (cpu_can_run(env))
3967 ret = qemu_cpu_exec(env);
Marcelo Tosattic37cc7b2010-02-09 12:49:04 -02003968 else if (env->stop)
3969 break;
3970
aliguorie6e35b12009-04-24 18:03:57 +00003971 if (ret == EXCP_DEBUG) {
3972 gdb_set_stop_cpu(env);
3973 debug_requested = 1;
3974 break;
3975 }
3976 }
3977}
3978
aliguori43b96852009-04-24 18:03:33 +00003979static int cpu_has_work(CPUState *env)
3980{
aliguorid6dc3d42009-04-24 18:04:07 +00003981 if (env->stop)
3982 return 1;
3983 if (env->stopped)
3984 return 0;
aliguori43b96852009-04-24 18:03:33 +00003985 if (!env->halted)
3986 return 1;
3987 if (qemu_cpu_has_work(env))
3988 return 1;
3989 return 0;
3990}
3991
3992static int tcg_has_work(void)
3993{
bellard6a00d602005-11-21 23:25:50 +00003994 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003995
aliguori43b96852009-04-24 18:03:33 +00003996 for (env = first_cpu; env != NULL; env = env->next_cpu)
3997 if (cpu_has_work(env))
3998 return 1;
3999 return 0;
4000}
bellard15a76442005-11-23 21:01:03 +00004001
aliguori43b96852009-04-24 18:03:33 +00004002static int qemu_calculate_timeout(void)
4003{
Luiz Capitulinob3198202009-06-09 18:24:57 -03004004#ifndef CONFIG_IOTHREAD
aliguori43b96852009-04-24 18:03:33 +00004005 int timeout;
bellard15a76442005-11-23 21:01:03 +00004006
aliguori43b96852009-04-24 18:03:33 +00004007 if (!vm_running)
4008 timeout = 5000;
4009 else if (tcg_has_work())
4010 timeout = 0;
4011 else if (!use_icount)
4012 timeout = 5000;
4013 else {
4014 /* XXX: use timeout computed from timers */
4015 int64_t add;
4016 int64_t delta;
4017 /* Advance virtual time to the next event. */
4018 if (use_icount == 1) {
4019 /* When not using an adaptive execution frequency
4020 we tend to get badly out of sync with real time,
4021 so just delay for a reasonable amount of time. */
4022 delta = 0;
bellard5905b2e2004-08-01 21:53:26 +00004023 } else {
aliguori43b96852009-04-24 18:03:33 +00004024 delta = cpu_get_icount() - cpu_get_clock();
bellard5905b2e2004-08-01 21:53:26 +00004025 }
aliguori43b96852009-04-24 18:03:33 +00004026 if (delta > 0) {
4027 /* If virtual time is ahead of real time then just
4028 wait for IO. */
4029 timeout = (delta / 1000000) + 1;
4030 } else {
4031 /* Wait for either IO to occur or the next
4032 timer event. */
4033 add = qemu_next_deadline();
4034 /* We advance the timer before checking for IO.
4035 Limit the amount we advance so that early IO
4036 activity won't get the guest too far ahead. */
4037 if (add > 10000000)
4038 add = 10000000;
4039 delta += add;
4040 add = (add + (1 << icount_time_shift) - 1)
4041 >> icount_time_shift;
4042 qemu_icount += add;
4043 timeout = delta / 1000000;
4044 if (timeout < 0)
4045 timeout = 0;
4046 }
bellardb4608c02003-06-27 17:34:32 +00004047 }
aliguori43b96852009-04-24 18:03:33 +00004048
4049 return timeout;
Luiz Capitulinob3198202009-06-09 18:24:57 -03004050#else /* CONFIG_IOTHREAD */
4051 return 1000;
4052#endif
aliguori43b96852009-04-24 18:03:33 +00004053}
4054
4055static int vm_can_run(void)
4056{
4057 if (powerdown_requested)
4058 return 0;
4059 if (reset_requested)
4060 return 0;
4061 if (shutdown_requested)
4062 return 0;
aliguorie5689022009-04-24 18:03:54 +00004063 if (debug_requested)
4064 return 0;
aliguori43b96852009-04-24 18:03:33 +00004065 return 1;
4066}
4067
Blue Swirld9c32312009-08-09 08:42:19 +00004068qemu_irq qemu_system_powerdown;
4069
aliguori43b96852009-04-24 18:03:33 +00004070static void main_loop(void)
4071{
aliguori6e29f5d2009-04-24 18:04:02 +00004072 int r;
aliguori43b96852009-04-24 18:03:33 +00004073
aliguorid6dc3d42009-04-24 18:04:07 +00004074#ifdef CONFIG_IOTHREAD
4075 qemu_system_ready = 1;
4076 qemu_cond_broadcast(&qemu_system_cond);
4077#endif
4078
aliguori6e29f5d2009-04-24 18:04:02 +00004079 for (;;) {
aliguorie6e35b12009-04-24 18:03:57 +00004080 do {
4081#ifdef CONFIG_PROFILER
4082 int64_t ti;
4083#endif
aliguorid6dc3d42009-04-24 18:04:07 +00004084#ifndef CONFIG_IOTHREAD
aliguorie6e35b12009-04-24 18:03:57 +00004085 tcg_cpu_exec();
aliguorid6dc3d42009-04-24 18:04:07 +00004086#endif
aliguori43b96852009-04-24 18:03:33 +00004087#ifdef CONFIG_PROFILER
4088 ti = profile_getclock();
4089#endif
4090 main_loop_wait(qemu_calculate_timeout());
4091#ifdef CONFIG_PROFILER
4092 dev_time += profile_getclock() - ti;
4093#endif
aliguorie5689022009-04-24 18:03:54 +00004094 } while (vm_can_run());
aliguori43b96852009-04-24 18:03:33 +00004095
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004096 if (qemu_debug_requested()) {
aliguori43b96852009-04-24 18:03:33 +00004097 vm_stop(EXCP_DEBUG);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004098 }
aliguori43b96852009-04-24 18:03:33 +00004099 if (qemu_shutdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004100 monitor_protocol_event(QEVENT_SHUTDOWN, NULL);
aliguori43b96852009-04-24 18:03:33 +00004101 if (no_shutdown) {
4102 vm_stop(0);
4103 no_shutdown = 0;
4104 } else
4105 break;
4106 }
aliguorid6dc3d42009-04-24 18:04:07 +00004107 if (qemu_reset_requested()) {
4108 pause_all_vcpus();
aliguori43b96852009-04-24 18:03:33 +00004109 qemu_system_reset();
aliguorid6dc3d42009-04-24 18:04:07 +00004110 resume_all_vcpus();
4111 }
Blue Swirld9c32312009-08-09 08:42:19 +00004112 if (qemu_powerdown_requested()) {
Blue Swirl242cd002009-12-04 18:05:45 +00004113 monitor_protocol_event(QEVENT_POWERDOWN, NULL);
Blue Swirld9c32312009-08-09 08:42:19 +00004114 qemu_irq_raise(qemu_system_powerdown);
4115 }
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004116 if ((r = qemu_vmstop_requested())) {
aliguori6e29f5d2009-04-24 18:04:02 +00004117 vm_stop(r);
Luiz Capitulinob1a15e72009-11-26 22:59:04 -02004118 }
aliguori43b96852009-04-24 18:03:33 +00004119 }
aliguorid6dc3d42009-04-24 18:04:07 +00004120 pause_all_vcpus();
bellardb4608c02003-06-27 17:34:32 +00004121}
4122
pbrook9bd7e6d2009-04-07 22:58:45 +00004123static void version(void)
4124{
pbrook4a19f1e2009-04-07 23:17:49 +00004125 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n");
pbrook9bd7e6d2009-04-07 22:58:45 +00004126}
4127
ths15f82202007-06-29 23:26:08 +00004128static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004129{
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01004130 const char *options_help =
blueswir15824d652009-03-28 06:44:27 +00004131#define DEF(option, opt_arg, opt_enum, opt_help) \
4132 opt_help
4133#define DEFHEADING(text) stringify(text) "\n"
4134#include "qemu-options.h"
4135#undef DEF
4136#undef DEFHEADING
4137#undef GEN_DOCS
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01004138 ;
4139 version();
4140 printf("usage: %s [options] [disk_image]\n"
malc3f020d72010-02-08 12:04:56 +03004141 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01004142 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4143 "\n"
4144 "%s\n"
malc3f020d72010-02-08 12:04:56 +03004145 "During emulation, the following keys are useful:\n"
4146 "ctrl-alt-f toggle full screen\n"
4147 "ctrl-alt-n switch to virtual console 'n'\n"
4148 "ctrl-alt toggle mouse and keyboard grab\n"
4149 "\n"
Paolo Bonzinie8105eb2010-02-04 16:49:59 +01004150 "When using -nographic, press 'ctrl-a h' to get some help.\n",
4151 "qemu",
4152 options_help);
ths15f82202007-06-29 23:26:08 +00004153 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004154}
4155
bellardcd6f1162004-05-13 22:02:20 +00004156#define HAS_ARG 0x0001
4157
4158enum {
blueswir15824d652009-03-28 06:44:27 +00004159#define DEF(option, opt_arg, opt_enum, opt_help) \
4160 opt_enum,
4161#define DEFHEADING(text)
4162#include "qemu-options.h"
4163#undef DEF
4164#undef DEFHEADING
4165#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004166};
4167
4168typedef struct QEMUOption {
4169 const char *name;
4170 int flags;
4171 int index;
4172} QEMUOption;
4173
blueswir1dbed7e42008-10-01 19:38:09 +00004174static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004175 { "h", 0, QEMU_OPTION_h },
blueswir15824d652009-03-28 06:44:27 +00004176#define DEF(option, opt_arg, opt_enum, opt_help) \
4177 { option, opt_arg, opt_enum },
4178#define DEFHEADING(text)
4179#include "qemu-options.h"
4180#undef DEF
4181#undef DEFHEADING
4182#undef GEN_DOCS
bellardcd6f1162004-05-13 22:02:20 +00004183 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004184};
4185
bellard1d14ffa2005-10-30 18:58:22 +00004186#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004187struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004188#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004189#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004190 {
4191 "pcspk",
4192 "PC speaker",
4193 0,
4194 1,
4195 { .init_isa = pcspk_audio_init }
4196 },
4197#endif
malc4c9b53e2009-01-09 10:46:34 +00004198
4199#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004200 {
4201 "sb16",
4202 "Creative Sound Blaster 16",
4203 0,
4204 1,
4205 { .init_isa = SB16_init }
4206 },
malc4c9b53e2009-01-09 10:46:34 +00004207#endif
bellard6a36d842005-12-18 20:34:32 +00004208
malccc53d262008-06-13 10:48:22 +00004209#ifdef CONFIG_CS4231A
4210 {
4211 "cs4231a",
4212 "CS4231A",
4213 0,
4214 1,
4215 { .init_isa = cs4231a_init }
4216 },
4217#endif
4218
bellard6a36d842005-12-18 20:34:32 +00004219#ifdef CONFIG_ADLIB
4220 {
4221 "adlib",
4222#ifdef HAS_YMF262
4223 "Yamaha YMF262 (OPL3)",
4224#else
4225 "Yamaha YM3812 (OPL2)",
4226#endif
4227 0,
4228 1,
4229 { .init_isa = Adlib_init }
4230 },
4231#endif
4232
4233#ifdef CONFIG_GUS
4234 {
4235 "gus",
4236 "Gravis Ultrasound GF1",
4237 0,
4238 1,
4239 { .init_isa = GUS_init }
4240 },
4241#endif
4242
malc4c9b53e2009-01-09 10:46:34 +00004243#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004244 {
4245 "ac97",
4246 "Intel 82801AA AC97 Audio",
4247 0,
4248 0,
4249 { .init_pci = ac97_init }
4250 },
malc4c9b53e2009-01-09 10:46:34 +00004251#endif
balroge5c9a132008-01-14 04:27:55 +00004252
malc4c9b53e2009-01-09 10:46:34 +00004253#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004254 {
4255 "es1370",
4256 "ENSONIQ AudioPCI ES1370",
4257 0,
4258 0,
4259 { .init_pci = es1370_init }
4260 },
balrogb00052e2007-04-30 02:22:06 +00004261#endif
bellard6a36d842005-12-18 20:34:32 +00004262
malc4c9b53e2009-01-09 10:46:34 +00004263#endif /* HAS_AUDIO_CHOICE */
4264
bellard6a36d842005-12-18 20:34:32 +00004265 { NULL, NULL, 0, 0, { NULL } }
4266};
4267
bellard1d14ffa2005-10-30 18:58:22 +00004268static void select_soundhw (const char *optarg)
4269{
bellard6a36d842005-12-18 20:34:32 +00004270 struct soundhw *c;
4271
bellard1d14ffa2005-10-30 18:58:22 +00004272 if (*optarg == '?') {
4273 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004274
bellard1d14ffa2005-10-30 18:58:22 +00004275 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004276 for (c = soundhw; c->name; ++c) {
4277 printf ("%-11s %s\n", c->name, c->descr);
4278 }
4279 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004280 exit (*optarg != '?');
4281 }
4282 else {
bellard6a36d842005-12-18 20:34:32 +00004283 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004284 const char *p;
4285 char *e;
4286 int bad_card = 0;
4287
bellard6a36d842005-12-18 20:34:32 +00004288 if (!strcmp (optarg, "all")) {
4289 for (c = soundhw; c->name; ++c) {
4290 c->enabled = 1;
4291 }
4292 return;
4293 }
bellard1d14ffa2005-10-30 18:58:22 +00004294
bellard6a36d842005-12-18 20:34:32 +00004295 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004296 while (*p) {
4297 e = strchr (p, ',');
4298 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004299
4300 for (c = soundhw; c->name; ++c) {
malcb3d6fb42009-09-06 06:49:03 +04004301 if (!strncmp (c->name, p, l) && !c->name[l]) {
bellard6a36d842005-12-18 20:34:32 +00004302 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004303 break;
4304 }
4305 }
bellard6a36d842005-12-18 20:34:32 +00004306
4307 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004308 if (l > 80) {
4309 fprintf (stderr,
4310 "Unknown sound card name (too big to show)\n");
4311 }
4312 else {
4313 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4314 (int) l, p);
4315 }
4316 bad_card = 1;
4317 }
4318 p += l + (e != NULL);
4319 }
4320
4321 if (bad_card)
4322 goto show_valid_cards;
4323 }
4324}
4325#endif
4326
malc3893c122008-09-28 00:42:05 +00004327static void select_vgahw (const char *p)
4328{
4329 const char *opts;
4330
Gerd Hoffmann64465292009-12-08 13:11:45 +01004331 default_vga = 0;
Zachary Amsden86176752009-07-30 00:15:02 -10004332 vga_interface_type = VGA_NONE;
malc3893c122008-09-28 00:42:05 +00004333 if (strstart(p, "std", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004334 vga_interface_type = VGA_STD;
malc3893c122008-09-28 00:42:05 +00004335 } else if (strstart(p, "cirrus", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004336 vga_interface_type = VGA_CIRRUS;
malc3893c122008-09-28 00:42:05 +00004337 } else if (strstart(p, "vmware", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004338 vga_interface_type = VGA_VMWARE;
aliguori94909d92009-04-22 15:19:53 +00004339 } else if (strstart(p, "xenfb", &opts)) {
Zachary Amsden86176752009-07-30 00:15:02 -10004340 vga_interface_type = VGA_XENFB;
aliguori28b85ed2009-04-22 15:19:48 +00004341 } else if (!strstart(p, "none", &opts)) {
malc3893c122008-09-28 00:42:05 +00004342 invalid_vga:
4343 fprintf(stderr, "Unknown vga type: %s\n", p);
4344 exit(1);
4345 }
malccb5a7aa2008-09-28 00:42:12 +00004346 while (*opts) {
4347 const char *nextopt;
4348
4349 if (strstart(opts, ",retrace=", &nextopt)) {
4350 opts = nextopt;
4351 if (strstart(opts, "dumb", &nextopt))
4352 vga_retrace_method = VGA_RETRACE_DUMB;
4353 else if (strstart(opts, "precise", &nextopt))
4354 vga_retrace_method = VGA_RETRACE_PRECISE;
4355 else goto invalid_vga;
4356 } else goto invalid_vga;
4357 opts = nextopt;
4358 }
malc3893c122008-09-28 00:42:05 +00004359}
4360
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004361#ifdef TARGET_I386
4362static int balloon_parse(const char *arg)
4363{
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004364 QemuOpts *opts;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004365
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004366 if (strcmp(arg, "none") == 0) {
4367 return 0;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004368 }
Gerd Hoffmann382f0742009-08-14 10:34:22 +02004369
4370 if (!strncmp(arg, "virtio", 6)) {
4371 if (arg[6] == ',') {
4372 /* have params -> parse them */
4373 opts = qemu_opts_parse(&qemu_device_opts, arg+7, NULL);
4374 if (!opts)
4375 return -1;
4376 } else {
4377 /* create empty opts */
4378 opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4379 }
4380 qemu_opt_set(opts, "driver", "virtio-balloon-pci");
4381 return 0;
4382 }
4383
4384 return -1;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02004385}
4386#endif
4387
bellard3587d7e2006-06-26 20:03:44 +00004388#ifdef _WIN32
4389static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4390{
4391 exit(STATUS_CONTROL_C_EXIT);
4392 return TRUE;
4393}
4394#endif
4395
aliguoric4be29f2009-04-17 18:58:14 +00004396int qemu_uuid_parse(const char *str, uint8_t *uuid)
blueswir18fcb1b92008-09-18 18:29:08 +00004397{
4398 int ret;
4399
4400 if(strlen(str) != 36)
4401 return -1;
4402
4403 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4404 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4405 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4406
4407 if(ret != 16)
4408 return -1;
4409
aliguorib6f6e3d2009-04-17 18:59:56 +00004410#ifdef TARGET_I386
4411 smbios_add_field(1, offsetof(struct smbios_type_1, uuid), 16, uuid);
4412#endif
4413
blueswir18fcb1b92008-09-18 18:29:08 +00004414 return 0;
4415}
4416
aliguori5b08fc12008-08-21 20:08:03 +00004417#ifndef _WIN32
4418
4419static void termsig_handler(int signal)
4420{
4421 qemu_system_shutdown_request();
4422}
4423
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004424static void sigchld_handler(int signal)
4425{
4426 waitpid(-1, NULL, WNOHANG);
4427}
4428
4429static void sighandler_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004430{
4431 struct sigaction act;
4432
4433 memset(&act, 0, sizeof(act));
4434 act.sa_handler = termsig_handler;
4435 sigaction(SIGINT, &act, NULL);
4436 sigaction(SIGHUP, &act, NULL);
4437 sigaction(SIGTERM, &act, NULL);
Jan Kiszka7c3370d2009-05-08 12:34:17 +02004438
4439 act.sa_handler = sigchld_handler;
4440 act.sa_flags = SA_NOCLDSTOP;
4441 sigaction(SIGCHLD, &act, NULL);
aliguori5b08fc12008-08-21 20:08:03 +00004442}
4443
4444#endif
4445
Paul Brook5cea8592009-05-30 00:52:44 +01004446#ifdef _WIN32
4447/* Look for support files in the same directory as the executable. */
4448static char *find_datadir(const char *argv0)
4449{
4450 char *p;
4451 char buf[MAX_PATH];
4452 DWORD len;
4453
4454 len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
4455 if (len == 0) {
Blue Swirlc5947802009-06-09 20:51:21 +03004456 return NULL;
Paul Brook5cea8592009-05-30 00:52:44 +01004457 }
4458
4459 buf[len] = 0;
4460 p = buf + len - 1;
4461 while (p != buf && *p != '\\')
4462 p--;
4463 *p = 0;
4464 if (access(buf, R_OK) == 0) {
4465 return qemu_strdup(buf);
4466 }
4467 return NULL;
4468}
4469#else /* !_WIN32 */
4470
4471/* Find a likely location for support files using the location of the binary.
4472 For installed binaries this will be "$bindir/../share/qemu". When
4473 running from the build tree this will be "$bindir/../pc-bios". */
4474#define SHARE_SUFFIX "/share/qemu"
4475#define BUILD_SUFFIX "/pc-bios"
4476static char *find_datadir(const char *argv0)
4477{
4478 char *dir;
4479 char *p = NULL;
4480 char *res;
Paul Brook5cea8592009-05-30 00:52:44 +01004481 char buf[PATH_MAX];
Blue Swirl3a417592009-06-09 19:12:21 +00004482 size_t max_len;
Paul Brook5cea8592009-05-30 00:52:44 +01004483
4484#if defined(__linux__)
4485 {
4486 int len;
4487 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
4488 if (len > 0) {
4489 buf[len] = 0;
4490 p = buf;
4491 }
4492 }
4493#elif defined(__FreeBSD__)
4494 {
4495 int len;
4496 len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
4497 if (len > 0) {
4498 buf[len] = 0;
4499 p = buf;
4500 }
4501 }
4502#endif
4503 /* If we don't have any way of figuring out the actual executable
4504 location then try argv[0]. */
4505 if (!p) {
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004506 p = realpath(argv0, buf);
Paul Brook5cea8592009-05-30 00:52:44 +01004507 if (!p) {
4508 return NULL;
4509 }
4510 }
4511 dir = dirname(p);
4512 dir = dirname(dir);
4513
Blue Swirl3a417592009-06-09 19:12:21 +00004514 max_len = strlen(dir) +
4515 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
4516 res = qemu_mallocz(max_len);
4517 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004518 if (access(res, R_OK)) {
Blue Swirl3a417592009-06-09 19:12:21 +00004519 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
Paul Brook5cea8592009-05-30 00:52:44 +01004520 if (access(res, R_OK)) {
4521 qemu_free(res);
4522 res = NULL;
4523 }
4524 }
Jean-Christophe DUBOIS4d224192009-09-02 23:59:02 +02004525
Paul Brook5cea8592009-05-30 00:52:44 +01004526 return res;
4527}
4528#undef SHARE_SUFFIX
4529#undef BUILD_SUFFIX
4530#endif
4531
4532char *qemu_find_file(int type, const char *name)
4533{
4534 int len;
4535 const char *subdir;
4536 char *buf;
4537
4538 /* If name contains path separators then try it as a straight path. */
4539 if ((strchr(name, '/') || strchr(name, '\\'))
4540 && access(name, R_OK) == 0) {
Jean-Christophe DUBOIS73ffc802009-09-02 23:59:06 +02004541 return qemu_strdup(name);
Paul Brook5cea8592009-05-30 00:52:44 +01004542 }
4543 switch (type) {
4544 case QEMU_FILE_TYPE_BIOS:
4545 subdir = "";
4546 break;
4547 case QEMU_FILE_TYPE_KEYMAP:
4548 subdir = "keymaps/";
4549 break;
4550 default:
4551 abort();
4552 }
4553 len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
4554 buf = qemu_mallocz(len);
Blue Swirl3a417592009-06-09 19:12:21 +00004555 snprintf(buf, len, "%s/%s%s", data_dir, subdir, name);
Paul Brook5cea8592009-05-30 00:52:44 +01004556 if (access(buf, R_OK)) {
4557 qemu_free(buf);
4558 return NULL;
4559 }
4560 return buf;
4561}
4562
Markus Armbrusterff952ba2010-01-29 19:48:57 +01004563static int device_help_func(QemuOpts *opts, void *opaque)
4564{
4565 return qdev_device_help(opts);
4566}
4567
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004568static int device_init_func(QemuOpts *opts, void *opaque)
4569{
4570 DeviceState *dev;
4571
4572 dev = qdev_device_add(opts);
4573 if (!dev)
4574 return -1;
4575 return 0;
4576}
4577
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01004578static int chardev_init_func(QemuOpts *opts, void *opaque)
4579{
4580 CharDriverState *chr;
4581
4582 chr = qemu_chr_open_opts(opts, NULL);
4583 if (!chr)
4584 return -1;
4585 return 0;
4586}
4587
Gerd Hoffmann88589342009-12-08 13:11:50 +01004588static int mon_init_func(QemuOpts *opts, void *opaque)
4589{
4590 CharDriverState *chr;
4591 const char *chardev;
4592 const char *mode;
4593 int flags;
4594
4595 mode = qemu_opt_get(opts, "mode");
4596 if (mode == NULL) {
4597 mode = "readline";
4598 }
4599 if (strcmp(mode, "readline") == 0) {
4600 flags = MONITOR_USE_READLINE;
4601 } else if (strcmp(mode, "control") == 0) {
4602 flags = MONITOR_USE_CONTROL;
4603 } else {
4604 fprintf(stderr, "unknown monitor mode \"%s\"\n", mode);
4605 exit(1);
4606 }
4607
4608 if (qemu_opt_get_bool(opts, "default", 0))
4609 flags |= MONITOR_IS_DEFAULT;
4610
4611 chardev = qemu_opt_get(opts, "chardev");
4612 chr = qemu_chr_find(chardev);
4613 if (chr == NULL) {
4614 fprintf(stderr, "chardev \"%s\" not found\n", chardev);
4615 exit(1);
4616 }
4617
4618 monitor_init(chr, flags);
4619 return 0;
4620}
4621
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004622static void monitor_parse(const char *optarg, const char *mode)
Gerd Hoffmann88589342009-12-08 13:11:50 +01004623{
4624 static int monitor_device_index = 0;
4625 QemuOpts *opts;
4626 const char *p;
4627 char label[32];
4628 int def = 0;
4629
4630 if (strstart(optarg, "chardev:", &p)) {
4631 snprintf(label, sizeof(label), "%s", p);
4632 } else {
4633 if (monitor_device_index) {
4634 snprintf(label, sizeof(label), "monitor%d",
4635 monitor_device_index);
4636 } else {
4637 snprintf(label, sizeof(label), "monitor");
4638 def = 1;
4639 }
4640 opts = qemu_chr_parse_compat(label, optarg);
4641 if (!opts) {
4642 fprintf(stderr, "parse error: %s\n", optarg);
4643 exit(1);
4644 }
4645 }
4646
4647 opts = qemu_opts_create(&qemu_mon_opts, label, 1);
4648 if (!opts) {
4649 fprintf(stderr, "duplicate chardev: %s\n", label);
4650 exit(1);
4651 }
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01004652 qemu_opt_set(opts, "mode", mode);
Gerd Hoffmann88589342009-12-08 13:11:50 +01004653 qemu_opt_set(opts, "chardev", label);
4654 if (def)
4655 qemu_opt_set(opts, "default", "on");
4656 monitor_device_index++;
4657}
4658
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004659struct device_config {
4660 enum {
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004661 DEV_USB, /* -usbdevice */
4662 DEV_BT, /* -bt */
4663 DEV_SERIAL, /* -serial */
4664 DEV_PARALLEL, /* -parallel */
4665 DEV_VIRTCON, /* -virtioconsole */
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004666 DEV_DEBUGCON, /* -debugcon */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004667 } type;
4668 const char *cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004669 QTAILQ_ENTRY(device_config) next;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004670};
Blue Swirl72cf2d42009-09-12 07:36:22 +00004671QTAILQ_HEAD(, device_config) device_configs = QTAILQ_HEAD_INITIALIZER(device_configs);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004672
4673static void add_device_config(int type, const char *cmdline)
4674{
4675 struct device_config *conf;
4676
4677 conf = qemu_mallocz(sizeof(*conf));
4678 conf->type = type;
4679 conf->cmdline = cmdline;
Blue Swirl72cf2d42009-09-12 07:36:22 +00004680 QTAILQ_INSERT_TAIL(&device_configs, conf, next);
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004681}
4682
4683static int foreach_device_config(int type, int (*func)(const char *cmdline))
4684{
4685 struct device_config *conf;
4686 int rc;
4687
Blue Swirl72cf2d42009-09-12 07:36:22 +00004688 QTAILQ_FOREACH(conf, &device_configs, next) {
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02004689 if (conf->type != type)
4690 continue;
4691 rc = func(conf->cmdline);
4692 if (0 != rc)
4693 return rc;
4694 }
4695 return 0;
4696}
4697
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01004698static int serial_parse(const char *devname)
4699{
4700 static int index = 0;
4701 char label[32];
4702
4703 if (strcmp(devname, "none") == 0)
4704 return 0;
4705 if (index == MAX_SERIAL_PORTS) {
4706 fprintf(stderr, "qemu: too many serial ports\n");
4707 exit(1);
4708 }
4709 snprintf(label, sizeof(label), "serial%d", index);
4710 serial_hds[index] = qemu_chr_open(label, devname, NULL);
4711 if (!serial_hds[index]) {
4712 fprintf(stderr, "qemu: could not open serial device '%s': %s\n",
4713 devname, strerror(errno));
4714 return -1;
4715 }
4716 index++;
4717 return 0;
4718}
4719
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01004720static int parallel_parse(const char *devname)
4721{
4722 static int index = 0;
4723 char label[32];
4724
4725 if (strcmp(devname, "none") == 0)
4726 return 0;
4727 if (index == MAX_PARALLEL_PORTS) {
4728 fprintf(stderr, "qemu: too many parallel ports\n");
4729 exit(1);
4730 }
4731 snprintf(label, sizeof(label), "parallel%d", index);
4732 parallel_hds[index] = qemu_chr_open(label, devname, NULL);
4733 if (!parallel_hds[index]) {
4734 fprintf(stderr, "qemu: could not open parallel device '%s': %s\n",
4735 devname, strerror(errno));
4736 return -1;
4737 }
4738 index++;
4739 return 0;
4740}
4741
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004742static int virtcon_parse(const char *devname)
4743{
4744 static int index = 0;
4745 char label[32];
Amit Shah392ecf52010-01-21 16:19:23 +05304746 QemuOpts *bus_opts, *dev_opts;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004747
4748 if (strcmp(devname, "none") == 0)
4749 return 0;
4750 if (index == MAX_VIRTIO_CONSOLES) {
4751 fprintf(stderr, "qemu: too many virtio consoles\n");
4752 exit(1);
4753 }
Amit Shah392ecf52010-01-21 16:19:23 +05304754
4755 bus_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4756 qemu_opt_set(bus_opts, "driver", "virtio-serial");
4757
4758 dev_opts = qemu_opts_create(&qemu_device_opts, NULL, 0);
4759 qemu_opt_set(dev_opts, "driver", "virtconsole");
4760
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004761 snprintf(label, sizeof(label), "virtcon%d", index);
4762 virtcon_hds[index] = qemu_chr_open(label, devname, NULL);
4763 if (!virtcon_hds[index]) {
4764 fprintf(stderr, "qemu: could not open virtio console '%s': %s\n",
4765 devname, strerror(errno));
4766 return -1;
4767 }
Amit Shah392ecf52010-01-21 16:19:23 +05304768 qemu_opt_set(dev_opts, "chardev", label);
4769
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01004770 index++;
4771 return 0;
4772}
4773
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08004774static int debugcon_parse(const char *devname)
4775{
4776 QemuOpts *opts;
4777
4778 if (!qemu_chr_open("debugcon", devname, NULL)) {
4779 exit(1);
4780 }
4781 opts = qemu_opts_create(&qemu_device_opts, "debugcon", 1);
4782 if (!opts) {
4783 fprintf(stderr, "qemu: already have a debugcon device\n");
4784 exit(1);
4785 }
4786 qemu_opt_set(opts, "driver", "isa-debugcon");
4787 qemu_opt_set(opts, "chardev", "debugcon");
4788 return 0;
4789}
4790
Anthony Liguori6530a972010-01-22 09:18:06 -06004791static const QEMUOption *lookup_opt(int argc, char **argv,
4792 const char **poptarg, int *poptind)
4793{
4794 const QEMUOption *popt;
4795 int optind = *poptind;
4796 char *r = argv[optind];
4797 const char *optarg;
4798
4799 optind++;
4800 /* Treat --foo the same as -foo. */
4801 if (r[1] == '-')
4802 r++;
4803 popt = qemu_options;
4804 for(;;) {
4805 if (!popt->name) {
4806 fprintf(stderr, "%s: invalid option -- '%s'\n",
4807 argv[0], r);
4808 exit(1);
4809 }
4810 if (!strcmp(popt->name, r + 1))
4811 break;
4812 popt++;
4813 }
4814 if (popt->flags & HAS_ARG) {
4815 if (optind >= argc) {
4816 fprintf(stderr, "%s: option '%s' requires an argument\n",
4817 argv[0], r);
4818 exit(1);
4819 }
4820 optarg = argv[optind++];
4821 } else {
4822 optarg = NULL;
4823 }
4824
4825 *poptarg = optarg;
4826 *poptind = optind;
4827
4828 return popt;
4829}
4830
malc902b3d52008-12-10 19:18:40 +00004831int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004832{
aliguori59030a82009-04-05 18:43:41 +00004833 const char *gdbstub_dev = NULL;
j_mayer28c5af52007-11-11 01:50:45 +00004834 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004835 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004836 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004837 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004838 const char *kernel_filename, *kernel_cmdline;
Anthony Liguori195325a2009-10-30 12:42:29 -05004839 char boot_devices[33] = "cad"; /* default to HD->floppy->CD-ROM */
aliguori3023f332009-01-16 19:04:14 +00004840 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004841 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004842 int cyls, heads, secs, translation;
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02004843 QemuOpts *hda_opts = NULL, *opts;
bellardcd6f1162004-05-13 22:02:20 +00004844 int optind;
Anthony Liguori6530a972010-01-22 09:18:06 -06004845 const char *optarg;
bellardd63d3072004-10-03 13:29:03 +00004846 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004847 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004848 const char *cpu_model;
blueswir1b9e82a52009-04-05 18:03:31 +00004849#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00004850 int fds[2];
blueswir1b9e82a52009-04-05 18:03:31 +00004851#endif
bellard26a5f132008-05-28 12:30:31 +00004852 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004853 const char *pid_file = NULL;
aliguori5bb79102008-10-13 03:12:02 +00004854 const char *incoming = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004855#ifndef _WIN32
aliguori54042bc2009-02-27 22:16:47 +00004856 int fd = 0;
4857 struct passwd *pwd = NULL;
aliguori08585322009-02-27 22:09:45 +00004858 const char *chroot_dir = NULL;
4859 const char *run_as = NULL;
blueswir1b9e82a52009-04-05 18:03:31 +00004860#endif
aliguori268a3622009-04-21 22:30:27 +00004861 CPUState *env;
Anthony Liguori993fbfd2009-05-21 16:54:00 -05004862 int show_vnc_port = 0;
Anthony Liguori292444c2010-01-21 10:57:58 -06004863 int defconfig = 1;
bellard0bd48852005-11-11 00:00:47 +00004864
Jan Kiszka68752042009-09-15 13:36:04 +02004865 init_clocks();
4866
Gerd Hoffmannac7531e2009-08-14 10:36:06 +02004867 qemu_errors_to_file(stderr);
malc902b3d52008-12-10 19:18:40 +00004868 qemu_cache_utils_init(envp);
4869
Blue Swirl72cf2d42009-09-12 07:36:22 +00004870 QLIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004871#ifndef _WIN32
4872 {
4873 struct sigaction act;
4874 sigfillset(&act.sa_mask);
4875 act.sa_flags = 0;
4876 act.sa_handler = SIG_IGN;
4877 sigaction(SIGPIPE, &act, NULL);
4878 }
bellard3587d7e2006-06-26 20:03:44 +00004879#else
4880 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004881 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4882 QEMU to run on a single CPU */
4883 {
4884 HANDLE h;
4885 DWORD mask, smask;
4886 int i;
4887 h = GetCurrentProcess();
4888 if (GetProcessAffinityMask(h, &mask, &smask)) {
4889 for(i = 0; i < 32; i++) {
4890 if (mask & (1 << i))
4891 break;
4892 }
4893 if (i != 32) {
4894 mask = 1 << i;
4895 SetProcessAffinityMask(h, mask);
4896 }
4897 }
4898 }
bellard67b915a2004-03-31 23:37:16 +00004899#endif
bellardbe995c22006-06-25 16:25:21 +00004900
Anthony Liguorif80f9ec2009-05-20 18:38:09 -05004901 module_call_init(MODULE_INIT_MACHINE);
Anthony Liguori0c257432009-05-21 20:41:01 -05004902 machine = find_default_machine();
j_mayer94fc95c2007-03-05 19:44:02 +00004903 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004904 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004905 ram_size = 0;
bellard33e39632003-07-06 17:15:21 +00004906 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004907 kernel_filename = NULL;
4908 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004909 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004910 translation = BIOS_ATA_TRANSLATION_AUTO;
bellardc4b1fcc2004-03-14 21:44:30 +00004911
aliguori268a3622009-04-21 22:30:27 +00004912 for (i = 0; i < MAX_NODES; i++) {
4913 node_mem[i] = 0;
4914 node_cpumask[i] = 0;
4915 }
4916
aliguori268a3622009-04-21 22:30:27 +00004917 nb_numa_nodes = 0;
bellard7c9d8e02005-11-15 22:16:05 +00004918 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004919
bellard26a5f132008-05-28 12:30:31 +00004920 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004921 autostart= 1;
4922
Anthony Liguori292444c2010-01-21 10:57:58 -06004923 /* first pass of option parsing */
4924 optind = 1;
4925 while (optind < argc) {
4926 if (argv[optind][0] != '-') {
4927 /* disk image */
Anthony Liguori28e68d62010-01-27 10:46:00 -06004928 optind++;
Anthony Liguori292444c2010-01-21 10:57:58 -06004929 continue;
4930 } else {
4931 const QEMUOption *popt;
4932
4933 popt = lookup_opt(argc, argv, &optarg, &optind);
4934 switch (popt->index) {
4935 case QEMU_OPTION_nodefconfig:
4936 defconfig=0;
4937 break;
4938 }
4939 }
4940 }
4941
4942 if (defconfig) {
4943 FILE *fp;
4944 fp = fopen(CONFIG_QEMU_CONFDIR "/qemu.conf", "r");
4945 if (fp) {
4946 if (qemu_config_parse(fp) != 0) {
4947 exit(1);
4948 }
4949 fclose(fp);
4950 }
4951
4952 fp = fopen(CONFIG_QEMU_CONFDIR "/target-" TARGET_ARCH ".conf", "r");
4953 if (fp) {
4954 if (qemu_config_parse(fp) != 0) {
4955 exit(1);
4956 }
4957 fclose(fp);
4958 }
4959 }
john cooperb5ec5ce2010-02-20 11:14:59 -06004960#if defined(cpudef_setup)
4961 cpudef_setup(); /* parse cpu definitions in target config file */
4962#endif
Anthony Liguori292444c2010-01-21 10:57:58 -06004963
4964 /* second pass of option parsing */
bellardcd6f1162004-05-13 22:02:20 +00004965 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004966 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004967 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004968 break;
Anthony Liguori6530a972010-01-22 09:18:06 -06004969 if (argv[optind][0] != '-') {
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02004970 hda_opts = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004971 } else {
4972 const QEMUOption *popt;
4973
Anthony Liguori6530a972010-01-22 09:18:06 -06004974 popt = lookup_opt(argc, argv, &optarg, &optind);
bellardcd6f1162004-05-13 22:02:20 +00004975 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004976 case QEMU_OPTION_M:
4977 machine = find_machine(optarg);
4978 if (!machine) {
4979 QEMUMachine *m;
4980 printf("Supported machines are:\n");
4981 for(m = first_machine; m != NULL; m = m->next) {
Mark McLoughlin3f6599e2009-07-22 10:02:50 +01004982 if (m->alias)
4983 printf("%-10s %s (alias of %s)\n",
4984 m->alias, m->desc, m->name);
bellardcc1daa42005-06-05 14:49:17 +00004985 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004986 m->name, m->desc,
Anthony Liguori0c257432009-05-21 20:41:01 -05004987 m->is_default ? " (default)" : "");
bellardcc1daa42005-06-05 14:49:17 +00004988 }
ths15f82202007-06-29 23:26:08 +00004989 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004990 }
4991 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004992 case QEMU_OPTION_cpu:
4993 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004994 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004995/* XXX: implement xxx_cpu_list for targets that still miss it */
john cooperb5ec5ce2010-02-20 11:14:59 -06004996#if defined(cpu_list_id)
4997 cpu_list_id(stdout, &fprintf, optarg);
4998#elif defined(cpu_list)
4999 cpu_list(stdout, &fprintf); /* deprecated */
j_mayer94fc95c2007-03-05 19:44:02 +00005000#endif
ths15f82202007-06-29 23:26:08 +00005001 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005002 } else {
5003 cpu_model = optarg;
5004 }
5005 break;
bellardcd6f1162004-05-13 22:02:20 +00005006 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005007 initrd_filename = optarg;
5008 break;
bellardcd6f1162004-05-13 22:02:20 +00005009 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005010 if (cyls == 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005011 hda_opts = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005012 else
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005013 hda_opts = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005014 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005015 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005016 translation == BIOS_ATA_TRANSLATION_LBA ?
5017 ",trans=lba" :
5018 translation == BIOS_ATA_TRANSLATION_NONE ?
5019 ",trans=none" : "");
5020 break;
bellardcd6f1162004-05-13 22:02:20 +00005021 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005022 case QEMU_OPTION_hdc:
5023 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005024 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005025 break;
thse4bcb142007-12-02 04:51:10 +00005026 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005027 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005028 break;
Gerd Hoffmannd058fe02009-07-31 12:25:36 +02005029 case QEMU_OPTION_set:
5030 if (qemu_set_option(optarg) != 0)
5031 exit(1);
5032 break;
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005033 case QEMU_OPTION_global:
5034 if (qemu_global_option(optarg) != 0)
5035 exit(1);
5036 break;
balrog3e3d5812007-04-30 02:09:25 +00005037 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005038 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005039 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005040 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005041 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005042 break;
j_mayer86f55662007-04-24 06:52:59 +00005043 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005044 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005045 break;
bellardcd6f1162004-05-13 22:02:20 +00005046 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005047 snapshot = 1;
5048 break;
bellardcd6f1162004-05-13 22:02:20 +00005049 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005050 {
bellard330d0412003-07-26 18:11:40 +00005051 const char *p;
5052 p = optarg;
5053 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005054 if (cyls < 1 || cyls > 16383)
5055 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005056 if (*p != ',')
5057 goto chs_fail;
5058 p++;
5059 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005060 if (heads < 1 || heads > 16)
5061 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005062 if (*p != ',')
5063 goto chs_fail;
5064 p++;
5065 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005066 if (secs < 1 || secs > 63)
5067 goto chs_fail;
5068 if (*p == ',') {
5069 p++;
5070 if (!strcmp(p, "none"))
5071 translation = BIOS_ATA_TRANSLATION_NONE;
5072 else if (!strcmp(p, "lba"))
5073 translation = BIOS_ATA_TRANSLATION_LBA;
5074 else if (!strcmp(p, "auto"))
5075 translation = BIOS_ATA_TRANSLATION_AUTO;
5076 else
5077 goto chs_fail;
5078 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005079 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005080 fprintf(stderr, "qemu: invalid physical CHS format\n");
5081 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005082 }
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005083 if (hda_opts != NULL) {
5084 char num[16];
5085 snprintf(num, sizeof(num), "%d", cyls);
5086 qemu_opt_set(hda_opts, "cyls", num);
5087 snprintf(num, sizeof(num), "%d", heads);
5088 qemu_opt_set(hda_opts, "heads", num);
5089 snprintf(num, sizeof(num), "%d", secs);
5090 qemu_opt_set(hda_opts, "secs", num);
5091 if (translation == BIOS_ATA_TRANSLATION_LBA)
5092 qemu_opt_set(hda_opts, "trans", "lba");
5093 if (translation == BIOS_ATA_TRANSLATION_NONE)
5094 qemu_opt_set(hda_opts, "trans", "none");
5095 }
bellard330d0412003-07-26 18:11:40 +00005096 }
5097 break;
aliguori268a3622009-04-21 22:30:27 +00005098 case QEMU_OPTION_numa:
5099 if (nb_numa_nodes >= MAX_NODES) {
5100 fprintf(stderr, "qemu: too many NUMA nodes\n");
5101 exit(1);
5102 }
5103 numa_add(optarg);
5104 break;
bellardcd6f1162004-05-13 22:02:20 +00005105 case QEMU_OPTION_nographic:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005106 display_type = DT_NOGRAPHIC;
bellarda20dd502003-09-30 21:07:02 +00005107 break;
balrog4d3b6f62008-02-10 16:33:14 +00005108#ifdef CONFIG_CURSES
5109 case QEMU_OPTION_curses:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005110 display_type = DT_CURSES;
balrog4d3b6f62008-02-10 16:33:14 +00005111 break;
5112#endif
balroga171fe32007-04-30 01:48:07 +00005113 case QEMU_OPTION_portrait:
5114 graphic_rotate = 1;
5115 break;
bellardcd6f1162004-05-13 22:02:20 +00005116 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005117 kernel_filename = optarg;
5118 break;
bellardcd6f1162004-05-13 22:02:20 +00005119 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005120 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005121 break;
bellardcd6f1162004-05-13 22:02:20 +00005122 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005123 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005124 break;
bellardcd6f1162004-05-13 22:02:20 +00005125 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005126 {
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005127 static const char * const params[] = {
Jan Kiszka95387492009-07-02 00:19:02 +02005128 "order", "once", "menu", NULL
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005129 };
5130 char buf[sizeof(boot_devices)];
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005131 char *standard_boot_devices;
Jan Kiszkaef3adf62009-07-02 00:19:02 +02005132 int legacy = 0;
5133
5134 if (!strchr(optarg, '=')) {
5135 legacy = 1;
5136 pstrcpy(buf, sizeof(buf), optarg);
5137 } else if (check_params(buf, sizeof(buf), params, optarg) < 0) {
5138 fprintf(stderr,
5139 "qemu: unknown boot parameter '%s' in '%s'\n",
5140 buf, optarg);
5141 exit(1);
5142 }
5143
5144 if (legacy ||
5145 get_param_value(buf, sizeof(buf), "order", optarg)) {
5146 boot_devices_bitmap = parse_bootdevices(buf);
5147 pstrcpy(boot_devices, sizeof(boot_devices), buf);
j_mayer28c5af52007-11-11 01:50:45 +00005148 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005149 if (!legacy) {
5150 if (get_param_value(buf, sizeof(buf),
5151 "once", optarg)) {
5152 boot_devices_bitmap |= parse_bootdevices(buf);
5153 standard_boot_devices = qemu_strdup(boot_devices);
5154 pstrcpy(boot_devices, sizeof(boot_devices), buf);
5155 qemu_register_reset(restore_boot_devices,
5156 standard_boot_devices);
5157 }
Jan Kiszka95387492009-07-02 00:19:02 +02005158 if (get_param_value(buf, sizeof(buf),
5159 "menu", optarg)) {
5160 if (!strcmp(buf, "on")) {
5161 boot_menu = 1;
5162 } else if (!strcmp(buf, "off")) {
5163 boot_menu = 0;
5164 } else {
5165 fprintf(stderr,
5166 "qemu: invalid option value '%s'\n",
5167 buf);
5168 exit(1);
5169 }
5170 }
Jan Kiszkae0f084b2009-07-02 00:19:02 +02005171 }
bellard36b486b2003-11-11 13:36:08 +00005172 }
5173 break;
bellardcd6f1162004-05-13 22:02:20 +00005174 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005175 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005176 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005177 break;
bellard52ca8d62006-06-14 16:03:05 +00005178#ifdef TARGET_I386
5179 case QEMU_OPTION_no_fd_bootchk:
5180 fd_bootchk = 0;
5181 break;
5182#endif
Mark McLoughlina1ea4582009-10-08 19:58:26 +01005183 case QEMU_OPTION_netdev:
5184 if (net_client_parse(&qemu_netdev_opts, optarg) == -1) {
5185 exit(1);
5186 }
5187 break;
bellard7c9d8e02005-11-15 22:16:05 +00005188 case QEMU_OPTION_net:
Mark McLoughlin7f161aa2009-10-08 19:58:25 +01005189 if (net_client_parse(&qemu_net_opts, optarg) == -1) {
bellardc4b1fcc2004-03-14 21:44:30 +00005190 exit(1);
5191 }
bellard702c6512004-04-02 21:21:32 +00005192 break;
bellardc7f74642004-08-24 21:57:12 +00005193#ifdef CONFIG_SLIRP
5194 case QEMU_OPTION_tftp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005195 legacy_tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005196 break;
ths47d5d012007-02-20 00:05:08 +00005197 case QEMU_OPTION_bootp:
Jan Kiszkaad196a92009-06-24 14:42:28 +02005198 legacy_bootp_filename = optarg;
ths47d5d012007-02-20 00:05:08 +00005199 break;
bellardc94c8d62004-09-13 21:37:34 +00005200#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005201 case QEMU_OPTION_smb:
Markus Armbruster07527062009-10-06 12:16:57 +01005202 if (net_slirp_smb(optarg) < 0)
5203 exit(1);
bellard9d728e82004-09-05 23:09:03 +00005204 break;
bellardc94c8d62004-09-13 21:37:34 +00005205#endif
bellard9bf05442004-08-25 22:12:49 +00005206 case QEMU_OPTION_redir:
Markus Armbruster07527062009-10-06 12:16:57 +01005207 if (net_slirp_redir(optarg) < 0)
5208 exit(1);
bellard9bf05442004-08-25 22:12:49 +00005209 break;
bellardc7f74642004-08-24 21:57:12 +00005210#endif
balrogdc72ac12008-11-09 00:04:26 +00005211 case QEMU_OPTION_bt:
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005212 add_device_config(DEV_BT, optarg);
balrogdc72ac12008-11-09 00:04:26 +00005213 break;
bellard1d14ffa2005-10-30 18:58:22 +00005214#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005215 case QEMU_OPTION_audio_help:
5216 AUD_help ();
5217 exit (0);
5218 break;
5219 case QEMU_OPTION_soundhw:
5220 select_soundhw (optarg);
5221 break;
5222#endif
bellardcd6f1162004-05-13 22:02:20 +00005223 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005224 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005225 break;
pbrook9bd7e6d2009-04-07 22:58:45 +00005226 case QEMU_OPTION_version:
5227 version();
5228 exit(0);
5229 break;
aurel3200f82b82008-04-27 21:12:55 +00005230 case QEMU_OPTION_m: {
5231 uint64_t value;
5232 char *ptr;
5233
5234 value = strtoul(optarg, &ptr, 10);
5235 switch (*ptr) {
5236 case 0: case 'M': case 'm':
5237 value <<= 20;
5238 break;
5239 case 'G': case 'g':
5240 value <<= 30;
5241 break;
5242 default:
5243 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005244 exit(1);
5245 }
aurel3200f82b82008-04-27 21:12:55 +00005246
5247 /* On 32-bit hosts, QEMU is limited by virtual address space */
Anthony Liguori4a1418e2009-08-10 17:07:24 -05005248 if (value > (2047 << 20) && HOST_LONG_BITS == 32) {
aurel3200f82b82008-04-27 21:12:55 +00005249 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5250 exit(1);
5251 }
Anthony Liguoric227f092009-10-01 16:12:16 -05005252 if (value != (uint64_t)(ram_addr_t)value) {
aurel3200f82b82008-04-27 21:12:55 +00005253 fprintf(stderr, "qemu: ram size too large\n");
5254 exit(1);
5255 }
5256 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005257 break;
aurel3200f82b82008-04-27 21:12:55 +00005258 }
Marcelo Tosattic9027602010-03-01 20:25:08 -03005259 case QEMU_OPTION_mempath:
5260 mem_path = optarg;
5261 break;
5262#ifdef MAP_POPULATE
5263 case QEMU_OPTION_mem_prealloc:
5264 mem_prealloc = 1;
5265 break;
5266#endif
bellardcd6f1162004-05-13 22:02:20 +00005267 case QEMU_OPTION_d:
5268 {
5269 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005270 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005271
bellardcd6f1162004-05-13 22:02:20 +00005272 mask = cpu_str_to_log_mask(optarg);
5273 if (!mask) {
5274 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005275 for(item = cpu_log_items; item->mask != 0; item++) {
5276 printf("%-10s %s\n", item->name, item->help);
5277 }
5278 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005279 }
5280 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005281 }
bellardcd6f1162004-05-13 22:02:20 +00005282 break;
bellardcd6f1162004-05-13 22:02:20 +00005283 case QEMU_OPTION_s:
aliguori59030a82009-04-05 18:43:41 +00005284 gdbstub_dev = "tcp::" DEFAULT_GDBSTUB_PORT;
bellardcd6f1162004-05-13 22:02:20 +00005285 break;
aliguori59030a82009-04-05 18:43:41 +00005286 case QEMU_OPTION_gdb:
5287 gdbstub_dev = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005288 break;
bellardcd6f1162004-05-13 22:02:20 +00005289 case QEMU_OPTION_L:
Paul Brook5cea8592009-05-30 00:52:44 +01005290 data_dir = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005291 break;
j_mayer1192dad2007-10-05 13:08:35 +00005292 case QEMU_OPTION_bios:
5293 bios_name = optarg;
5294 break;
aurel321b530a62009-04-05 20:08:59 +00005295 case QEMU_OPTION_singlestep:
5296 singlestep = 1;
5297 break;
bellardcd6f1162004-05-13 22:02:20 +00005298 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005299 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005300 break;
bellard3d11d0e2004-12-12 16:56:30 +00005301 case QEMU_OPTION_k:
5302 keyboard_layout = optarg;
5303 break;
bellardee22c2f2004-06-03 12:49:50 +00005304 case QEMU_OPTION_localtime:
5305 rtc_utc = 0;
5306 break;
malc3893c122008-09-28 00:42:05 +00005307 case QEMU_OPTION_vga:
5308 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005309 break;
blueswir15824d652009-03-28 06:44:27 +00005310#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005311 case QEMU_OPTION_g:
5312 {
5313 const char *p;
5314 int w, h, depth;
5315 p = optarg;
5316 w = strtol(p, (char **)&p, 10);
5317 if (w <= 0) {
5318 graphic_error:
5319 fprintf(stderr, "qemu: invalid resolution or depth\n");
5320 exit(1);
5321 }
5322 if (*p != 'x')
5323 goto graphic_error;
5324 p++;
5325 h = strtol(p, (char **)&p, 10);
5326 if (h <= 0)
5327 goto graphic_error;
5328 if (*p == 'x') {
5329 p++;
5330 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005331 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005332 depth != 24 && depth != 32)
5333 goto graphic_error;
5334 } else if (*p == '\0') {
5335 depth = graphic_depth;
5336 } else {
5337 goto graphic_error;
5338 }
ths3b46e622007-09-17 08:09:54 +00005339
bellarde9b137c2004-06-21 16:46:10 +00005340 graphic_width = w;
5341 graphic_height = h;
5342 graphic_depth = depth;
5343 }
5344 break;
blueswir15824d652009-03-28 06:44:27 +00005345#endif
ths20d8a3e2007-02-18 17:04:49 +00005346 case QEMU_OPTION_echr:
5347 {
5348 char *r;
5349 term_escape_char = strtol(optarg, &r, 0);
5350 if (r == optarg)
5351 printf("Bad argument to echr\n");
5352 break;
5353 }
bellard82c643f2004-07-14 17:28:13 +00005354 case QEMU_OPTION_monitor:
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01005355 monitor_parse(optarg, "readline");
5356 default_monitor = 0;
5357 break;
5358 case QEMU_OPTION_qmp:
5359 monitor_parse(optarg, "control");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005360 default_monitor = 0;
bellard82c643f2004-07-14 17:28:13 +00005361 break;
Gerd Hoffmann22a0e042009-12-08 13:11:51 +01005362 case QEMU_OPTION_mon:
5363 opts = qemu_opts_parse(&qemu_mon_opts, optarg, "chardev");
5364 if (!opts) {
5365 fprintf(stderr, "parse error: %s\n", optarg);
5366 exit(1);
5367 }
5368 default_monitor = 0;
5369 break;
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005370 case QEMU_OPTION_chardev:
5371 opts = qemu_opts_parse(&qemu_chardev_opts, optarg, "backend");
5372 if (!opts) {
5373 fprintf(stderr, "parse error: %s\n", optarg);
5374 exit(1);
5375 }
Gerd Hoffmann191bc012009-09-10 10:58:35 +02005376 break;
bellard82c643f2004-07-14 17:28:13 +00005377 case QEMU_OPTION_serial:
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005378 add_device_config(DEV_SERIAL, optarg);
5379 default_serial = 0;
bellard82c643f2004-07-14 17:28:13 +00005380 break;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005381 case QEMU_OPTION_watchdog:
Markus Armbruster09aaa162009-08-21 10:31:34 +02005382 if (watchdog) {
5383 fprintf(stderr,
5384 "qemu: only one watchdog option may be given\n");
5385 return 1;
5386 }
5387 watchdog = optarg;
Richard W.M. Jones9dd986c2009-04-25 13:56:19 +01005388 break;
5389 case QEMU_OPTION_watchdog_action:
5390 if (select_watchdog_action(optarg) == -1) {
5391 fprintf(stderr, "Unknown -watchdog-action parameter\n");
5392 exit(1);
5393 }
5394 break;
aliguori51ecf132009-01-15 20:06:40 +00005395 case QEMU_OPTION_virtiocon:
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01005396 add_device_config(DEV_VIRTCON, optarg);
5397 default_virtcon = 0;
aliguori51ecf132009-01-15 20:06:40 +00005398 break;
bellard6508fe52005-01-15 12:02:56 +00005399 case QEMU_OPTION_parallel:
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005400 add_device_config(DEV_PARALLEL, optarg);
5401 default_parallel = 0;
bellard6508fe52005-01-15 12:02:56 +00005402 break;
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08005403 case QEMU_OPTION_debugcon:
5404 add_device_config(DEV_DEBUGCON, optarg);
5405 break;
bellardd63d3072004-10-03 13:29:03 +00005406 case QEMU_OPTION_loadvm:
5407 loadvm = optarg;
5408 break;
5409 case QEMU_OPTION_full_screen:
5410 full_screen = 1;
5411 break;
ths667acca2006-12-11 02:08:05 +00005412#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005413 case QEMU_OPTION_no_frame:
5414 no_frame = 1;
5415 break;
ths3780e192007-06-21 21:08:02 +00005416 case QEMU_OPTION_alt_grab:
5417 alt_grab = 1;
5418 break;
Dustin Kirkland0ca9f8a2009-09-17 15:48:04 -05005419 case QEMU_OPTION_ctrl_grab:
5420 ctrl_grab = 1;
5421 break;
ths667acca2006-12-11 02:08:05 +00005422 case QEMU_OPTION_no_quit:
5423 no_quit = 1;
5424 break;
aliguori7d957bd2009-01-15 22:14:11 +00005425 case QEMU_OPTION_sdl:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005426 display_type = DT_SDL;
aliguori7d957bd2009-01-15 22:14:11 +00005427 break;
ths667acca2006-12-11 02:08:05 +00005428#endif
bellardf7cce892004-12-08 22:21:25 +00005429 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005430 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005431 break;
bellarda09db212005-04-30 16:10:35 +00005432#ifdef TARGET_I386
5433 case QEMU_OPTION_win2k_hack:
5434 win2k_install_hack = 1;
5435 break;
aliguori73822ec2009-01-15 20:11:34 +00005436 case QEMU_OPTION_rtc_td_hack:
5437 rtc_td_hack = 1;
5438 break;
aliguori8a92ea22009-02-27 20:12:36 +00005439 case QEMU_OPTION_acpitable:
5440 if(acpi_table_add(optarg) < 0) {
5441 fprintf(stderr, "Wrong acpi table provided\n");
5442 exit(1);
5443 }
5444 break;
aliguorib6f6e3d2009-04-17 18:59:56 +00005445 case QEMU_OPTION_smbios:
5446 if(smbios_entry_add(optarg) < 0) {
5447 fprintf(stderr, "Wrong smbios provided\n");
5448 exit(1);
5449 }
5450 break;
bellarda09db212005-04-30 16:10:35 +00005451#endif
aliguori7ba1e612008-11-05 16:04:33 +00005452#ifdef CONFIG_KVM
5453 case QEMU_OPTION_enable_kvm:
5454 kvm_allowed = 1;
aliguori7ba1e612008-11-05 16:04:33 +00005455 break;
5456#endif
bellardbb36d472005-11-05 14:22:28 +00005457 case QEMU_OPTION_usb:
5458 usb_enabled = 1;
5459 break;
bellarda594cfb2005-11-06 16:13:29 +00005460 case QEMU_OPTION_usbdevice:
5461 usb_enabled = 1;
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005462 add_device_config(DEV_USB, optarg);
5463 break;
5464 case QEMU_OPTION_device:
Mark McLoughlinb386bec2009-10-06 12:17:01 +01005465 if (!qemu_opts_parse(&qemu_device_opts, optarg, "driver")) {
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005466 exit(1);
5467 }
bellarda594cfb2005-11-06 16:13:29 +00005468 break;
bellard6a00d602005-11-21 23:25:50 +00005469 case QEMU_OPTION_smp:
Andre Przywaradc6b1c02009-08-19 15:42:40 +02005470 smp_parse(optarg);
aliguorib2097002008-10-07 20:39:39 +00005471 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005472 fprintf(stderr, "Invalid number of CPUs\n");
5473 exit(1);
5474 }
Jes Sorensen6be68d72009-07-23 17:03:42 +02005475 if (max_cpus < smp_cpus) {
5476 fprintf(stderr, "maxcpus must be equal to or greater than "
5477 "smp\n");
5478 exit(1);
5479 }
5480 if (max_cpus > 255) {
5481 fprintf(stderr, "Unsupported number of maxcpus\n");
5482 exit(1);
5483 }
bellard6a00d602005-11-21 23:25:50 +00005484 break;
bellard24236862006-04-30 21:28:36 +00005485 case QEMU_OPTION_vnc:
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005486 display_type = DT_VNC;
ths73fc9742006-12-22 02:09:07 +00005487 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005488 break;
blueswir15824d652009-03-28 06:44:27 +00005489#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00005490 case QEMU_OPTION_no_acpi:
5491 acpi_enabled = 0;
5492 break;
aliguori16b29ae2008-12-17 23:28:44 +00005493 case QEMU_OPTION_no_hpet:
5494 no_hpet = 1;
5495 break;
Markus Armbruster7d4c3d52009-06-26 19:15:14 +02005496 case QEMU_OPTION_balloon:
5497 if (balloon_parse(optarg) < 0) {
5498 fprintf(stderr, "Unknown -balloon argument %s\n", optarg);
5499 exit(1);
5500 }
Eduardo Habkostdf97b922009-06-10 16:34:08 -03005501 break;
blueswir15824d652009-03-28 06:44:27 +00005502#endif
bellardd1beab82006-10-02 19:44:22 +00005503 case QEMU_OPTION_no_reboot:
5504 no_reboot = 1;
5505 break;
aurel32b2f76162008-04-11 21:35:52 +00005506 case QEMU_OPTION_no_shutdown:
5507 no_shutdown = 1;
5508 break;
balrog9467cd42007-05-01 01:34:14 +00005509 case QEMU_OPTION_show_cursor:
5510 cursor_hide = 0;
5511 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005512 case QEMU_OPTION_uuid:
5513 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5514 fprintf(stderr, "Fail to parse UUID string."
5515 " Wrong format.\n");
5516 exit(1);
5517 }
5518 break;
blueswir15824d652009-03-28 06:44:27 +00005519#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005520 case QEMU_OPTION_daemonize:
5521 daemonize = 1;
5522 break;
blueswir15824d652009-03-28 06:44:27 +00005523#endif
ths9ae02552007-01-05 17:39:04 +00005524 case QEMU_OPTION_option_rom:
5525 if (nb_option_roms >= MAX_OPTION_ROMS) {
5526 fprintf(stderr, "Too many option ROMs\n");
5527 exit(1);
5528 }
5529 option_rom[nb_option_roms] = optarg;
5530 nb_option_roms++;
5531 break;
blueswir15824d652009-03-28 06:44:27 +00005532#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005533 case QEMU_OPTION_semihosting:
5534 semihosting_enabled = 1;
5535 break;
blueswir15824d652009-03-28 06:44:27 +00005536#endif
thsc35734b2007-03-19 15:17:08 +00005537 case QEMU_OPTION_name:
Andi Kleen18894652009-07-02 09:34:17 +02005538 qemu_name = qemu_strdup(optarg);
5539 {
5540 char *p = strchr(qemu_name, ',');
5541 if (p != NULL) {
5542 *p++ = 0;
5543 if (strncmp(p, "process=", 8)) {
5544 fprintf(stderr, "Unknown subargument %s to -name", p);
5545 exit(1);
5546 }
5547 p += 8;
5548 set_proc_name(p);
5549 }
5550 }
thsc35734b2007-03-19 15:17:08 +00005551 break;
blueswir195efd112008-12-24 20:26:14 +00005552#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005553 case QEMU_OPTION_prom_env:
5554 if (nb_prom_envs >= MAX_PROM_ENVS) {
5555 fprintf(stderr, "Too many prom variables\n");
5556 exit(1);
5557 }
5558 prom_envs[nb_prom_envs] = optarg;
5559 nb_prom_envs++;
5560 break;
5561#endif
balrog2b8f2d42007-07-27 22:08:46 +00005562#ifdef TARGET_ARM
5563 case QEMU_OPTION_old_param:
5564 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005565 break;
balrog2b8f2d42007-07-27 22:08:46 +00005566#endif
thsf3dcfad2007-08-24 01:26:02 +00005567 case QEMU_OPTION_clock:
5568 configure_alarms(optarg);
5569 break;
bellard7e0af5d02007-11-07 16:24:33 +00005570 case QEMU_OPTION_startdate:
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005571 configure_rtc_date_offset(optarg, 1);
5572 break;
5573 case QEMU_OPTION_rtc:
5574 opts = qemu_opts_parse(&qemu_rtc_opts, optarg, NULL);
5575 if (!opts) {
5576 fprintf(stderr, "parse error: %s\n", optarg);
5577 exit(1);
bellard7e0af5d02007-11-07 16:24:33 +00005578 }
Jan Kiszka1ed2fc12009-09-15 13:36:04 +02005579 configure_rtc(opts);
bellard7e0af5d02007-11-07 16:24:33 +00005580 break;
bellard26a5f132008-05-28 12:30:31 +00005581 case QEMU_OPTION_tb_size:
5582 tb_size = strtol(optarg, NULL, 0);
5583 if (tb_size < 0)
5584 tb_size = 0;
5585 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005586 case QEMU_OPTION_icount:
5587 use_icount = 1;
5588 if (strcmp(optarg, "auto") == 0) {
5589 icount_time_shift = -1;
5590 } else {
5591 icount_time_shift = strtol(optarg, NULL, 0);
5592 }
5593 break;
aliguori5bb79102008-10-13 03:12:02 +00005594 case QEMU_OPTION_incoming:
5595 incoming = optarg;
5596 break;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005597 case QEMU_OPTION_nodefaults:
5598 default_serial = 0;
5599 default_parallel = 0;
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01005600 default_virtcon = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005601 default_monitor = 0;
5602 default_vga = 0;
Gerd Hoffmanncb4522c2009-12-08 13:11:47 +01005603 default_net = 0;
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005604 default_floppy = 0;
5605 default_cdrom = 0;
5606 default_sdcard = 0;
Gerd Hoffmannd8c208d2009-12-08 13:11:46 +01005607 break;
blueswir15824d652009-03-28 06:44:27 +00005608#ifndef _WIN32
aliguori08585322009-02-27 22:09:45 +00005609 case QEMU_OPTION_chroot:
5610 chroot_dir = optarg;
5611 break;
5612 case QEMU_OPTION_runas:
5613 run_as = optarg;
5614 break;
blueswir15824d652009-03-28 06:44:27 +00005615#endif
aliguorie37630c2009-04-22 15:19:10 +00005616#ifdef CONFIG_XEN
5617 case QEMU_OPTION_xen_domid:
5618 xen_domid = atoi(optarg);
5619 break;
5620 case QEMU_OPTION_xen_create:
5621 xen_mode = XEN_CREATE;
5622 break;
5623 case QEMU_OPTION_xen_attach:
5624 xen_mode = XEN_ATTACH;
5625 break;
5626#endif
Gerd Hoffmann715a6642009-10-14 10:39:28 +02005627 case QEMU_OPTION_readconfig:
5628 {
5629 FILE *fp;
5630 fp = fopen(optarg, "r");
5631 if (fp == NULL) {
5632 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5633 exit(1);
5634 }
5635 if (qemu_config_parse(fp) != 0) {
5636 exit(1);
5637 }
5638 fclose(fp);
5639 break;
5640 }
5641 case QEMU_OPTION_writeconfig:
5642 {
5643 FILE *fp;
5644 if (strcmp(optarg, "-") == 0) {
5645 fp = stdout;
5646 } else {
5647 fp = fopen(optarg, "w");
5648 if (fp == NULL) {
5649 fprintf(stderr, "open %s: %s\n", optarg, strerror(errno));
5650 exit(1);
5651 }
5652 }
5653 qemu_config_write(fp);
5654 fclose(fp);
5655 break;
5656 }
bellardcd6f1162004-05-13 22:02:20 +00005657 }
bellard0824d6f2003-06-24 13:42:40 +00005658 }
5659 }
bellard330d0412003-07-26 18:11:40 +00005660
Paul Brook5cea8592009-05-30 00:52:44 +01005661 /* If no data_dir is specified then try to find it relative to the
5662 executable path. */
5663 if (!data_dir) {
5664 data_dir = find_datadir(argv[0]);
5665 }
5666 /* If all else fails use the install patch specified when building. */
5667 if (!data_dir) {
5668 data_dir = CONFIG_QEMU_SHAREDIR;
5669 }
5670
Jes Sorensen6be68d72009-07-23 17:03:42 +02005671 /*
5672 * Default to max_cpus = smp_cpus, in case the user doesn't
5673 * specify a max_cpus value.
5674 */
5675 if (!max_cpus)
5676 max_cpus = smp_cpus;
5677
balrog3d878ca2008-10-28 10:59:59 +00005678 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005679 if (smp_cpus > machine->max_cpus) {
5680 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5681 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5682 machine->max_cpus);
5683 exit(1);
5684 }
5685
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005686 qemu_opts_foreach(&qemu_device_opts, default_driver_check, NULL, 0);
Gerd Hoffmannd8bcbab2009-12-16 14:25:40 +01005687 qemu_opts_foreach(&qemu_global_opts, default_driver_check, NULL, 0);
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005688
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005689 if (machine->no_serial) {
5690 default_serial = 0;
5691 }
5692 if (machine->no_parallel) {
5693 default_parallel = 0;
5694 }
5695 if (!machine->use_virtcon) {
5696 default_virtcon = 0;
5697 }
5698 if (machine->no_vga) {
5699 default_vga = 0;
5700 }
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005701 if (machine->no_floppy) {
5702 default_floppy = 0;
5703 }
5704 if (machine->no_cdrom) {
5705 default_cdrom = 0;
5706 }
5707 if (machine->no_sdcard) {
5708 default_sdcard = 0;
5709 }
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005710
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005711 if (display_type == DT_NOGRAPHIC) {
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005712 if (default_parallel)
5713 add_device_config(DEV_PARALLEL, "null");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005714 if (default_serial && default_monitor) {
5715 add_device_config(DEV_SERIAL, "mon:stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005716 } else if (default_virtcon && default_monitor) {
5717 add_device_config(DEV_VIRTCON, "mon:stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005718 } else {
5719 if (default_serial)
5720 add_device_config(DEV_SERIAL, "stdio");
Gerd Hoffmann986c5f72009-12-08 13:11:54 +01005721 if (default_virtcon)
5722 add_device_config(DEV_VIRTCON, "stdio");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005723 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01005724 monitor_parse("stdio", "readline");
Gerd Hoffmanne1c09172009-12-08 13:11:44 +01005725 }
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005726 } else {
5727 if (default_serial)
5728 add_device_config(DEV_SERIAL, "vc:80Cx24C");
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005729 if (default_parallel)
5730 add_device_config(DEV_PARALLEL, "vc:80Cx24C");
Gerd Hoffmannabdeed02009-12-08 13:11:43 +01005731 if (default_monitor)
Gerd Hoffmann6ca55822009-12-08 13:11:52 +01005732 monitor_parse("vc:80Cx24C", "readline");
Alexander Graf38536da2009-12-17 13:06:08 +01005733 if (default_virtcon)
5734 add_device_config(DEV_VIRTCON, "vc:80Cx24C");
aliguoribc0129d2008-08-01 15:12:34 +00005735 }
Gerd Hoffmann64465292009-12-08 13:11:45 +01005736 if (default_vga)
5737 vga_interface_type = VGA_CIRRUS;
aliguoribc0129d2008-08-01 15:12:34 +00005738
Gerd Hoffmann1a688d32009-12-08 13:11:36 +01005739 if (qemu_opts_foreach(&qemu_chardev_opts, chardev_init_func, NULL, 1) != 0)
5740 exit(1);
5741
ths71e3ceb2006-12-22 02:11:31 +00005742#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005743 if (daemonize) {
5744 pid_t pid;
5745
5746 if (pipe(fds) == -1)
5747 exit(1);
5748
5749 pid = fork();
5750 if (pid > 0) {
5751 uint8_t status;
5752 ssize_t len;
5753
5754 close(fds[1]);
5755
5756 again:
ths93815bc2007-03-19 15:58:31 +00005757 len = read(fds[0], &status, 1);
5758 if (len == -1 && (errno == EINTR))
5759 goto again;
5760
5761 if (len != 1)
5762 exit(1);
5763 else if (status == 1) {
Justin M. Forbes850810d2009-10-01 09:42:56 -05005764 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005765 exit(1);
5766 } else
5767 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005768 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005769 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005770
Kevin Wolf40ff6d72009-12-02 12:24:42 +01005771 close(fds[0]);
5772 qemu_set_cloexec(fds[1]);
5773
ths71e3ceb2006-12-22 02:11:31 +00005774 setsid();
5775
5776 pid = fork();
5777 if (pid > 0)
5778 exit(0);
5779 else if (pid < 0)
5780 exit(1);
5781
5782 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005783
5784 signal(SIGTSTP, SIG_IGN);
5785 signal(SIGTTOU, SIG_IGN);
5786 signal(SIGTTIN, SIG_IGN);
5787 }
Juha Riihimäki099fe232009-12-03 15:56:03 +02005788#endif
ths71e3ceb2006-12-22 02:11:31 +00005789
thsaa26bb22007-03-25 21:33:06 +00005790 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
Juha Riihimäki099fe232009-12-03 15:56:03 +02005791#ifndef _WIN32
ths93815bc2007-03-19 15:58:31 +00005792 if (daemonize) {
5793 uint8_t status = 1;
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01005794 if (write(fds[1], &status, 1) != 1) {
5795 perror("daemonize. Writing to pipe\n");
5796 }
ths93815bc2007-03-19 15:58:31 +00005797 } else
Juha Riihimäki099fe232009-12-03 15:56:03 +02005798#endif
Justin M. Forbes850810d2009-10-01 09:42:56 -05005799 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
ths93815bc2007-03-19 15:58:31 +00005800 exit(1);
5801 }
5802
Marcelo Tosatti214910a2009-09-18 02:41:23 -03005803 if (kvm_enabled()) {
5804 int ret;
5805
5806 ret = kvm_init(smp_cpus);
5807 if (ret < 0) {
5808 fprintf(stderr, "failed to initialize KVM\n");
5809 exit(1);
5810 }
5811 }
5812
aliguori3fcf7b62009-04-24 18:03:25 +00005813 if (qemu_init_main_loop()) {
5814 fprintf(stderr, "qemu_init_main_loop failed\n");
5815 exit(1);
5816 }
bellarda20dd502003-09-30 21:07:02 +00005817 linux_boot = (kernel_filename != NULL);
balrog6c41b272007-11-17 12:12:29 +00005818
thsf8d39c02008-07-03 10:01:15 +00005819 if (!linux_boot && *kernel_cmdline != '\0') {
5820 fprintf(stderr, "-append only allowed with -kernel option\n");
5821 exit(1);
5822 }
5823
5824 if (!linux_boot && initrd_filename != NULL) {
5825 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5826 exit(1);
5827 }
5828
Filip Navarabf65f532009-07-27 10:02:04 -05005829#ifndef _WIN32
5830 /* Win32 doesn't support line-buffering and requires size >= 2 */
bellardb118d612003-06-30 23:36:21 +00005831 setvbuf(stdout, NULL, _IOLBF, 0);
Filip Navarabf65f532009-07-27 10:02:04 -05005832#endif
ths3b46e622007-09-17 08:09:54 +00005833
aliguori7183b4b2008-11-05 20:40:18 +00005834 if (init_timer_alarm() < 0) {
5835 fprintf(stderr, "could not initialize alarm timer\n");
5836 exit(1);
5837 }
pbrook2e70f6e2008-06-29 01:03:05 +00005838 if (use_icount && icount_time_shift < 0) {
5839 use_icount = 2;
5840 /* 125MIPS seems a reasonable initial guess at the guest speed.
5841 It will be corrected fairly quickly anyway. */
5842 icount_time_shift = 3;
5843 init_icount_adjust();
5844 }
pbrook634fce92006-07-15 17:40:09 +00005845
bellardfd1dff42006-02-01 21:29:26 +00005846#ifdef _WIN32
5847 socket_init();
5848#endif
5849
Mark McLoughlindc1c9fe2009-10-06 12:17:16 +01005850 if (net_init_clients() < 0) {
5851 exit(1);
bellard702c6512004-04-02 21:21:32 +00005852 }
bellardf1510b22003-06-25 00:07:40 +00005853
Glauber Costa406c8df2009-06-17 09:05:30 -04005854 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
5855 net_set_boot_mask(net_boot);
5856
balrogdc72ac12008-11-09 00:04:26 +00005857 /* init the bluetooth world */
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005858 if (foreach_device_config(DEV_BT, bt_parse))
5859 exit(1);
balrogdc72ac12008-11-09 00:04:26 +00005860
bellard0824d6f2003-06-24 13:42:40 +00005861 /* init the memory */
pbrook94a6b542009-04-11 17:15:54 +00005862 if (ram_size == 0)
5863 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
balrog7fb4fdc2008-04-24 17:59:27 +00005864
bellard26a5f132008-05-28 12:30:31 +00005865 /* init the dynamic translator */
5866 cpu_exec_init_all(tb_size * 1024 * 1024);
5867
Markus Armbrustereb852012009-10-27 18:41:44 +01005868 bdrv_init_with_whitelist();
thse4bcb142007-12-02 04:51:10 +00005869
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005870 blk_mig_init();
5871
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005872 if (default_cdrom) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01005873 /* we always create the cdrom drive, even if no disk is there */
5874 drive_add(NULL, CDROM_ALIAS);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005875 }
thse4bcb142007-12-02 04:51:10 +00005876
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005877 if (default_floppy) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01005878 /* we always create at least one floppy */
5879 drive_add(NULL, FD_ALIAS, 0);
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005880 }
bellardc4b1fcc2004-03-14 21:44:30 +00005881
Gerd Hoffmannac33f8f2009-12-16 14:25:39 +01005882 if (default_sdcard) {
Gerd Hoffmannaa40fc92009-12-08 13:11:48 +01005883 /* we always create one sd slot, even if no card is in it */
5884 drive_add(NULL, SD_ALIAS);
5885 }
balrog9d413d12007-12-04 00:10:34 +00005886
ths96d30e42007-01-07 20:42:14 +00005887 /* open the virtual block devices */
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005888 if (snapshot)
Gerd Hoffmann7282a032009-07-31 12:25:35 +02005889 qemu_opts_foreach(&qemu_drive_opts, drive_enable_snapshot, NULL, 0);
5890 if (qemu_opts_foreach(&qemu_drive_opts, drive_init_func, machine, 1) != 0)
Gerd Hoffmann9dfd7c72009-07-22 16:43:04 +02005891 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005892
Juan Quintela2faf58c2009-09-10 03:04:28 +02005893 vmstate_register(0, &vmstate_timers ,&timers_state);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02005894 register_savevm_live("ram", 0, 3, NULL, ram_save_live, NULL,
5895 ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005896
aliguori268a3622009-04-21 22:30:27 +00005897 if (nb_numa_nodes > 0) {
5898 int i;
5899
5900 if (nb_numa_nodes > smp_cpus) {
5901 nb_numa_nodes = smp_cpus;
5902 }
5903
5904 /* If no memory size if given for any node, assume the default case
5905 * and distribute the available memory equally across all nodes
5906 */
5907 for (i = 0; i < nb_numa_nodes; i++) {
5908 if (node_mem[i] != 0)
5909 break;
5910 }
5911 if (i == nb_numa_nodes) {
5912 uint64_t usedmem = 0;
5913
5914 /* On Linux, the each node's border has to be 8MB aligned,
5915 * the final node gets the rest.
5916 */
5917 for (i = 0; i < nb_numa_nodes - 1; i++) {
5918 node_mem[i] = (ram_size / nb_numa_nodes) & ~((1 << 23UL) - 1);
5919 usedmem += node_mem[i];
5920 }
5921 node_mem[i] = ram_size - usedmem;
5922 }
5923
5924 for (i = 0; i < nb_numa_nodes; i++) {
5925 if (node_cpumask[i] != 0)
5926 break;
5927 }
5928 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5929 * must cope with this anyway, because there are BIOSes out there in
5930 * real machines which also use this scheme.
5931 */
5932 if (i == nb_numa_nodes) {
5933 for (i = 0; i < smp_cpus; i++) {
5934 node_cpumask[i % nb_numa_nodes] |= 1 << i;
5935 }
5936 }
5937 }
5938
Gerd Hoffmann998bbd72009-12-08 13:11:41 +01005939 if (foreach_device_config(DEV_SERIAL, serial_parse) < 0)
5940 exit(1);
Gerd Hoffmann6a5e8b02009-12-08 13:11:42 +01005941 if (foreach_device_config(DEV_PARALLEL, parallel_parse) < 0)
5942 exit(1);
Gerd Hoffmannaee1b932009-12-08 13:11:53 +01005943 if (foreach_device_config(DEV_VIRTCON, virtcon_parse) < 0)
5944 exit(1);
H. Peter Anvinc9f398e2009-12-29 13:51:36 -08005945 if (foreach_device_config(DEV_DEBUGCON, debugcon_parse) < 0)
5946 exit(1);
aliguori2796dae2009-01-16 20:23:27 +00005947
Paul Brookaae94602009-05-14 22:35:06 +01005948 module_call_init(MODULE_INIT_DEVICE);
5949
Markus Armbrusterff952ba2010-01-29 19:48:57 +01005950 if (qemu_opts_foreach(&qemu_device_opts, device_help_func, NULL, 0) != 0)
5951 exit(0);
5952
Markus Armbruster09aaa162009-08-21 10:31:34 +02005953 if (watchdog) {
5954 i = select_watchdog(watchdog);
5955 if (i > 0)
5956 exit (i == 1 ? 1 : 0);
5957 }
5958
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005959 if (machine->compat_props) {
Gerd Hoffmann458fb672009-12-08 13:11:33 +01005960 qdev_prop_register_global_list(machine->compat_props);
Gerd Hoffmannb6b61142009-07-15 13:48:21 +02005961 }
Gerd Hoffmannd0fef6f2009-12-08 13:11:34 +01005962 qemu_add_globals();
5963
Paul Brookfbe1b592009-05-13 17:56:25 +01005964 machine->init(ram_size, boot_devices,
aliguori3023f332009-01-16 19:04:14 +00005965 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5966
Jan Kiszkaea375f92010-03-01 19:10:30 +01005967 cpu_synchronize_all_post_init();
aliguori268a3622009-04-21 22:30:27 +00005968
Juan Quintela67b3b712009-08-28 19:25:15 +02005969#ifndef _WIN32
5970 /* must be after terminal init, SDL library changes signal handlers */
5971 sighandler_setup();
5972#endif
5973
aliguori268a3622009-04-21 22:30:27 +00005974 for (env = first_cpu; env != NULL; env = env->next_cpu) {
5975 for (i = 0; i < nb_numa_nodes; i++) {
5976 if (node_cpumask[i] & (1 << env->cpu_index)) {
5977 env->numa_node = i;
5978 }
5979 }
5980 }
5981
aliguori6f338c32009-02-11 15:21:54 +00005982 current_machine = machine;
5983
aliguori3023f332009-01-16 19:04:14 +00005984 /* init USB devices */
5985 if (usb_enabled) {
Markus Armbruster07527062009-10-06 12:16:57 +01005986 if (foreach_device_config(DEV_USB, usb_parse) < 0)
5987 exit(1);
aliguori3023f332009-01-16 19:04:14 +00005988 }
5989
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005990 /* init generic devices */
Gerd Hoffmannf31d07d2009-07-31 12:25:37 +02005991 if (qemu_opts_foreach(&qemu_device_opts, device_init_func, NULL, 1) != 0)
Gerd Hoffmannbd3c9482009-07-15 13:59:26 +02005992 exit(1);
5993
Markus Armbruster668680f2010-02-11 14:44:58 +01005994 net_check_clients();
5995
aliguori3023f332009-01-16 19:04:14 +00005996 /* just use the first displaystate for the moment */
Paolo Bonzinib473df62010-02-11 00:29:55 +01005997 ds = get_displaystate();
Anthony Liguori993fbfd2009-05-21 16:54:00 -05005998
5999 if (display_type == DT_DEFAULT) {
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006000#if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006001 display_type = DT_SDL;
6002#else
6003 display_type = DT_VNC;
6004 vnc_display = "localhost:0,to=99";
6005 show_vnc_port = 1;
6006#endif
6007 }
6008
6009
6010 switch (display_type) {
6011 case DT_NOGRAPHIC:
6012 break;
6013#if defined(CONFIG_CURSES)
6014 case DT_CURSES:
6015 curses_display_init(ds, full_screen);
6016 break;
6017#endif
bellard5b0753e2005-03-01 21:37:28 +00006018#if defined(CONFIG_SDL)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006019 case DT_SDL:
6020 sdl_display_init(ds, full_screen, no_frame);
6021 break;
bellard5b0753e2005-03-01 21:37:28 +00006022#elif defined(CONFIG_COCOA)
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006023 case DT_SDL:
6024 cocoa_display_init(ds, full_screen);
6025 break;
bellard313aa562003-08-10 21:52:11 +00006026#endif
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006027 case DT_VNC:
6028 vnc_display_init(ds);
6029 if (vnc_display_open(ds, vnc_display) < 0)
6030 exit(1);
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006031
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006032 if (show_vnc_port) {
6033 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds));
Anthony Liguorif92f8af2009-05-20 13:01:02 -05006034 }
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006035 break;
6036 default:
6037 break;
bellard313aa562003-08-10 21:52:11 +00006038 }
aliguori7d957bd2009-01-15 22:14:11 +00006039 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00006040
aliguori3023f332009-01-16 19:04:14 +00006041 dcl = ds->listeners;
6042 while (dcl != NULL) {
6043 if (dcl->dpy_refresh != NULL) {
6044 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
6045 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00006046 }
aliguori3023f332009-01-16 19:04:14 +00006047 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00006048 }
aliguori3023f332009-01-16 19:04:14 +00006049
Anthony Liguori993fbfd2009-05-21 16:54:00 -05006050 if (display_type == DT_NOGRAPHIC || display_type == DT_VNC) {
blueswir19043b622009-01-21 19:28:13 +00006051 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
6052 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
6053 }
6054
Paolo Bonzinib473df62010-02-11 00:29:55 +01006055 text_consoles_set_display(ds);
aliguori2796dae2009-01-16 20:23:27 +00006056
Gerd Hoffmann88589342009-12-08 13:11:50 +01006057 if (qemu_opts_foreach(&qemu_mon_opts, mon_init_func, NULL, 1) != 0)
6058 exit(1);
bellard82c643f2004-07-14 17:28:13 +00006059
aliguori59030a82009-04-05 18:43:41 +00006060 if (gdbstub_dev && gdbserver_start(gdbstub_dev) < 0) {
6061 fprintf(stderr, "qemu: could not open gdbserver on device '%s'\n",
6062 gdbstub_dev);
6063 exit(1);
balrog45669e02007-07-02 13:20:17 +00006064 }
balrog45669e02007-07-02 13:20:17 +00006065
Gerd Hoffmann3418bd22009-09-25 21:42:41 +02006066 qdev_machine_creation_done();
6067
Gerd Hoffmann15ff7702009-12-14 16:07:35 +01006068 if (rom_load_all() != 0) {
6069 fprintf(stderr, "rom loading failed\n");
6070 exit(1);
6071 }
Gerd Hoffmann45a50b12009-10-01 16:42:33 +02006072
Juan Quintela504c2942009-11-12 00:39:13 +01006073 qemu_system_reset();
Juan Quintela05f24012009-08-20 19:42:22 +02006074 if (loadvm) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01006075 if (load_vmstate(loadvm) < 0) {
Juan Quintela05f24012009-08-20 19:42:22 +02006076 autostart = 0;
6077 }
6078 }
bellardd63d3072004-10-03 13:29:03 +00006079
Glauber Costa2bb8c102009-07-24 16:20:23 -04006080 if (incoming) {
aliguori5bb79102008-10-13 03:12:02 +00006081 qemu_start_incoming_migration(incoming);
Avi Kivity6b99dad2009-08-09 14:39:20 +03006082 } else if (autostart) {
aliguoric0f4ce72009-03-05 23:01:01 +00006083 vm_start();
Avi Kivity6b99dad2009-08-09 14:39:20 +03006084 }
thsffd843b2006-12-21 19:46:43 +00006085
blueswir1b9e82a52009-04-05 18:03:31 +00006086#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006087 if (daemonize) {
6088 uint8_t status = 0;
6089 ssize_t len;
ths71e3ceb2006-12-22 02:11:31 +00006090
6091 again1:
6092 len = write(fds[1], &status, 1);
6093 if (len == -1 && (errno == EINTR))
6094 goto again1;
6095
6096 if (len != 1)
6097 exit(1);
6098
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01006099 if (chdir("/")) {
6100 perror("not able to chdir to /");
6101 exit(1);
6102 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01006103 TFR(fd = qemu_open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006104 if (fd == -1)
6105 exit(1);
aliguori08585322009-02-27 22:09:45 +00006106 }
ths71e3ceb2006-12-22 02:11:31 +00006107
aliguori08585322009-02-27 22:09:45 +00006108 if (run_as) {
6109 pwd = getpwnam(run_as);
6110 if (!pwd) {
6111 fprintf(stderr, "User \"%s\" doesn't exist\n", run_as);
6112 exit(1);
6113 }
6114 }
ths71e3ceb2006-12-22 02:11:31 +00006115
aliguori08585322009-02-27 22:09:45 +00006116 if (chroot_dir) {
6117 if (chroot(chroot_dir) < 0) {
6118 fprintf(stderr, "chroot failed\n");
6119 exit(1);
6120 }
Kirill A. Shutemovdc330e22010-01-20 00:56:18 +01006121 if (chdir("/")) {
6122 perror("not able to chdir to /");
6123 exit(1);
6124 }
aliguori08585322009-02-27 22:09:45 +00006125 }
6126
6127 if (run_as) {
6128 if (setgid(pwd->pw_gid) < 0) {
6129 fprintf(stderr, "Failed to setgid(%d)\n", pwd->pw_gid);
6130 exit(1);
6131 }
6132 if (setuid(pwd->pw_uid) < 0) {
6133 fprintf(stderr, "Failed to setuid(%d)\n", pwd->pw_uid);
6134 exit(1);
6135 }
6136 if (setuid(0) != -1) {
6137 fprintf(stderr, "Dropping privileges failed\n");
6138 exit(1);
6139 }
6140 }
aliguori08585322009-02-27 22:09:45 +00006141
6142 if (daemonize) {
6143 dup2(fd, 0);
6144 dup2(fd, 1);
6145 dup2(fd, 2);
6146
6147 close(fd);
ths71e3ceb2006-12-22 02:11:31 +00006148 }
blueswir1b9e82a52009-04-05 18:03:31 +00006149#endif
ths71e3ceb2006-12-22 02:11:31 +00006150
bellard8a7ddc32004-03-31 19:00:16 +00006151 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006152 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006153 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006154
bellard0824d6f2003-06-24 13:42:40 +00006155 return 0;
6156}