blob: 5f237d0a06843b85da12c86c94e9207ee600f045 [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 */
pbrook87ecb682007-11-17 17:14:51 +000024#include "hw/hw.h"
25#include "hw/boards.h"
26#include "hw/usb.h"
27#include "hw/pcmcia.h"
28#include "hw/pc.h"
pbrook87ecb682007-11-17 17:14:51 +000029#include "hw/audiodev.h"
30#include "hw/isa.h"
aurel322e4d9fb2008-04-08 06:01:02 +000031#include "hw/baum.h"
balrog1ae26a12008-09-28 23:19:47 +000032#include "hw/bt.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "net.h"
34#include "console.h"
35#include "sysemu.h"
36#include "gdbstub.h"
37#include "qemu-timer.h"
38#include "qemu-char.h"
malc902b3d52008-12-10 19:18:40 +000039#include "cache-utils.h"
pbrook87ecb682007-11-17 17:14:51 +000040#include "block.h"
41#include "audio/audio.h"
aliguori5bb79102008-10-13 03:12:02 +000042#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000043#include "kvm.h"
aliguoridf751fa2008-12-04 20:19:35 +000044#include "balloon.h"
bellard67b915a2004-03-31 23:37:16 +000045
bellard0824d6f2003-06-24 13:42:40 +000046#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000047#include <fcntl.h>
48#include <signal.h>
49#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000050#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000051#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000052#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000053
54#ifndef _WIN32
55#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000056#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000057#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000058#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000059#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000060#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000061#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000062#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000063#include <net/if.h>
64#if defined(__NetBSD__)
65#include <net/if_tap.h>
66#endif
67#ifdef __linux__
68#include <linux/if_tun.h>
69#endif
70#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000071#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000072#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000073#include <sys/select.h>
bellard7d3505c2004-05-12 19:32:15 +000074#ifdef _BSD
75#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000076#ifdef __FreeBSD__
bellard7d3505c2004-05-12 19:32:15 +000077#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000078#else
79#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000080#endif
ths5c40d2b2007-06-23 16:03:36 +000081#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000083#else
blueswir1223f0d72008-09-30 18:12:18 +000084#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000085#include <pty.h>
86#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000087#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000088
89/* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91/* #include <linux/hpet.h> */
92#include "hpet.h"
93
bellarde57a8c02005-11-10 23:58:52 +000094#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000095#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000096#endif
97#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000098#include <sys/stat.h>
99#include <sys/ethernet.h>
100#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +0000101#include <netinet/arp.h>
102#include <netinet/in.h>
103#include <netinet/in_systm.h>
104#include <netinet/ip.h>
105#include <netinet/ip_icmp.h> // must come after ip.h
106#include <netinet/udp.h>
107#include <netinet/tcp.h>
108#include <net/if.h>
109#include <syslog.h>
110#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +0000111#endif
bellard7d3505c2004-05-12 19:32:15 +0000112#endif
bellardec530c82006-04-25 22:36:06 +0000113#endif
bellard67b915a2004-03-31 23:37:16 +0000114
aliguori03ff3ca2008-09-15 15:51:35 +0000115#include "qemu_socket.h"
116
bellardc20709a2004-04-21 23:27:19 +0000117#if defined(CONFIG_SLIRP)
118#include "libslirp.h"
119#endif
120
blueswir19892fbf2008-08-24 10:34:20 +0000121#if defined(__OpenBSD__)
122#include <util.h>
123#endif
124
ths8a16d272008-07-19 09:56:24 +0000125#if defined(CONFIG_VDE)
126#include <libvdeplug.h>
127#endif
128
bellard67b915a2004-03-31 23:37:16 +0000129#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +0000130#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000131#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000132#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000133#define getopt_long_only getopt_long
134#define memalign(align, size) malloc(size)
135#endif
136
bellard73332e52004-04-04 20:22:28 +0000137#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000138#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000139#include <SDL/SDL.h>
bellard96bcd4f2004-07-10 16:26:15 +0000140#endif
bellard73332e52004-04-04 20:22:28 +0000141#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000142
bellard5b0753e2005-03-01 21:37:28 +0000143#ifdef CONFIG_COCOA
144#undef main
145#define main qemu_main
146#endif /* CONFIG_COCOA */
147
bellard0824d6f2003-06-24 13:42:40 +0000148#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000149
bellard8a7ddc32004-03-31 19:00:16 +0000150#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000151
bellard0824d6f2003-06-24 13:42:40 +0000152//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000153//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000154//#define DEBUG_NET
155//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000156
aliguorid12d51d2009-01-15 21:48:06 +0000157
158#ifdef DEBUG_IOPORT
aliguori93fcfe32009-01-15 22:34:14 +0000159# define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
aliguorid12d51d2009-01-15 21:48:06 +0000160#else
161# define LOG_IOPORT(...) do { } while (0)
162#endif
163
bellard1bfe8562004-07-08 21:17:50 +0000164#define DEFAULT_RAM_SIZE 128
bellard313aa562003-08-10 21:52:11 +0000165
pbrook0d92ed32006-05-21 16:30:15 +0000166/* Max number of USB devices that can be specified on the commandline. */
167#define MAX_USB_CMDLINE 8
168
balrogdc72ac12008-11-09 00:04:26 +0000169/* Max number of bluetooth switches on the commandline. */
170#define MAX_BT_CMDLINE 10
171
bellard7dea1da2003-11-16 15:59:30 +0000172/* XXX: use a two level table to limit memory usage */
173#define MAX_IOPORTS 65536
bellard0824d6f2003-06-24 13:42:40 +0000174
bellard80cabfa2004-03-14 12:20:30 +0000175const char *bios_dir = CONFIG_QEMU_SHAREDIR;
j_mayer1192dad2007-10-05 13:08:35 +0000176const char *bios_name = NULL;
blueswir1dbed7e42008-10-01 19:38:09 +0000177static void *ioport_opaque[MAX_IOPORTS];
178static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
179static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
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 */
thse4bcb142007-12-02 04:51:10 +0000182DriveInfo drives_table[MAX_DRIVES+1];
183int nb_drives;
blueswir1dbed7e42008-10-01 19:38:09 +0000184static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000185enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori3023f332009-01-16 19:04:14 +0000186static DisplayState *display_state;
bellarda20dd502003-09-30 21:07:02 +0000187int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000188static int curses;
aliguori7d957bd2009-01-15 22:14:11 +0000189static int sdl;
bellard3d11d0e2004-12-12 16:56:30 +0000190const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000191int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000192ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000193int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000194NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000195int vm_running;
balrogf6503052008-02-17 11:42:19 +0000196static int rtc_utc = 1;
197static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000198int cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +0000199int std_vga_enabled = 0;
thsd34cab92007-04-02 01:10:46 +0000200int vmsvga_enabled = 0;
bellardd8272202005-04-06 20:32:23 +0000201#ifdef TARGET_SPARC
202int graphic_width = 1024;
203int graphic_height = 768;
blueswir1eee0b832007-04-21 19:45:49 +0000204int graphic_depth = 8;
bellardd8272202005-04-06 20:32:23 +0000205#else
bellard1bfe8562004-07-08 21:17:50 +0000206int graphic_width = 800;
207int graphic_height = 600;
bellarde9b137c2004-06-21 16:46:10 +0000208int graphic_depth = 15;
blueswir1eee0b832007-04-21 19:45:49 +0000209#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000210static int full_screen = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000211#ifdef CONFIG_SDL
blueswir1dbed7e42008-10-01 19:38:09 +0000212static int no_frame = 0;
blueswir1634a21f2008-11-16 11:34:07 +0000213#endif
ths667acca2006-12-11 02:08:05 +0000214int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000215CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000216CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
aliguori9ede2fd2009-01-15 20:05:25 +0000217CharDriverState *virtcon_hds[MAX_VIRTIO_CONSOLES];
bellarda09db212005-04-30 16:10:35 +0000218#ifdef TARGET_I386
219int win2k_install_hack = 0;
aliguori73822ec2009-01-15 20:11:34 +0000220int rtc_td_hack = 0;
bellarda09db212005-04-30 16:10:35 +0000221#endif
bellardbb36d472005-11-05 14:22:28 +0000222int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000223int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000224const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000225int acpi_enabled = 1;
aliguori16b29ae2008-12-17 23:28:44 +0000226int no_hpet = 0;
bellard52ca8d62006-06-14 16:03:05 +0000227int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000228int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000229int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000230int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000231int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000232int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000233const char *option_rom[MAX_OPTION_ROMS];
234int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000235int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000236#ifdef TARGET_ARM
237int old_param = 0;
238#endif
thsc35734b2007-03-19 15:17:08 +0000239const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000240int alt_grab = 0;
blueswir195efd112008-12-24 20:26:14 +0000241#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +0000242unsigned int nb_prom_envs = 0;
243const char *prom_envs[MAX_PROM_ENVS];
244#endif
aliguoriec691c82009-02-11 15:20:37 +0000245int nb_drives_opt;
246struct drive_opt drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000247
balrogee5605e2007-12-03 03:01:40 +0000248static CPUState *cur_cpu;
249static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000250static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000251/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000252static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000253/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000254#define MAX_ICOUNT_SHIFT 10
255/* Compensate for varying guest execution speed. */
256static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000257static QEMUTimer *icount_rt_timer;
258static QEMUTimer *icount_vm_timer;
blueswir19043b622009-01-21 19:28:13 +0000259static QEMUTimer *nographic_timer;
balrogee5605e2007-12-03 03:01:40 +0000260
blueswir18fcb1b92008-09-18 18:29:08 +0000261uint8_t qemu_uuid[16];
262
bellard0824d6f2003-06-24 13:42:40 +0000263/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000264/* x86 ISA bus support */
265
266target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000267PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000268
aliguori477e3ed2008-07-23 15:19:59 +0000269static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
270static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
271
272static uint32_t ioport_read(int index, uint32_t address)
273{
274 static IOPortReadFunc *default_func[3] = {
275 default_ioport_readb,
276 default_ioport_readw,
277 default_ioport_readl
278 };
279 IOPortReadFunc *func = ioport_read_table[index][address];
280 if (!func)
281 func = default_func[index];
282 return func(ioport_opaque[address], address);
283}
284
285static void ioport_write(int index, uint32_t address, uint32_t data)
286{
287 static IOPortWriteFunc *default_func[3] = {
288 default_ioport_writeb,
289 default_ioport_writew,
290 default_ioport_writel
291 };
292 IOPortWriteFunc *func = ioport_write_table[index][address];
293 if (!func)
294 func = default_func[index];
295 func(ioport_opaque[address], address, data);
296}
297
pbrook9596ebb2007-11-18 01:44:38 +0000298static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000299{
300#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000301 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000302#endif
bellardfc01f7e2003-06-30 10:03:06 +0000303 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000304}
305
pbrook9596ebb2007-11-18 01:44:38 +0000306static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000307{
308#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000309 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000310#endif
311}
312
313/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000314static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000315{
316 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000317 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000318 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000319 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000320 return data;
321}
322
pbrook9596ebb2007-11-18 01:44:38 +0000323static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000324{
aliguori477e3ed2008-07-23 15:19:59 +0000325 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000326 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000327 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000328}
329
pbrook9596ebb2007-11-18 01:44:38 +0000330static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000331{
332#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000333 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000334#endif
335 return 0xffffffff;
336}
337
pbrook9596ebb2007-11-18 01:44:38 +0000338static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000339{
340#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000341 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000342#endif
bellard0824d6f2003-06-24 13:42:40 +0000343}
344
bellardfc01f7e2003-06-30 10:03:06 +0000345/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000346int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000347 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000348{
bellardfc01f7e2003-06-30 10:03:06 +0000349 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000350
bellardc4b1fcc2004-03-14 21:44:30 +0000351 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000352 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000353 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000354 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000355 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000356 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000357 } else {
balrog88fdf562008-04-26 21:11:22 +0000358 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000359 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000360 }
361 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000362 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000363 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000364 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000365 ioport_opaque[i] = opaque;
366 }
bellard0824d6f2003-06-24 13:42:40 +0000367 return 0;
368}
369
bellardfc01f7e2003-06-30 10:03:06 +0000370/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000371int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000372 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000373{
bellardfc01f7e2003-06-30 10:03:06 +0000374 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000375
bellardc4b1fcc2004-03-14 21:44:30 +0000376 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000377 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000378 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000379 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000380 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000381 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000382 } else {
balrog88fdf562008-04-26 21:11:22 +0000383 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000384 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000385 }
386 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000387 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000388 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
389 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000390 ioport_opaque[i] = opaque;
391 }
bellardf1510b22003-06-25 00:07:40 +0000392 return 0;
393}
394
bellard69b91032004-05-18 23:05:28 +0000395void isa_unassign_ioport(int start, int length)
396{
397 int i;
398
399 for(i = start; i < start + length; i++) {
400 ioport_read_table[0][i] = default_ioport_readb;
401 ioport_read_table[1][i] = default_ioport_readw;
402 ioport_read_table[2][i] = default_ioport_readl;
403
404 ioport_write_table[0][i] = default_ioport_writeb;
405 ioport_write_table[1][i] = default_ioport_writew;
406 ioport_write_table[2][i] = default_ioport_writel;
aliguoria7607f72009-02-11 15:21:29 +0000407
408 ioport_opaque[i] = NULL;
bellard69b91032004-05-18 23:05:28 +0000409 }
410}
411
bellard20f32282005-01-03 23:36:21 +0000412/***********************************************************/
413
bellardc45886d2004-01-05 00:02:06 +0000414void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000415{
aliguorid12d51d2009-01-15 21:48:06 +0000416 LOG_IOPORT("outb: %04x %02x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000417 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000418#ifdef USE_KQEMU
419 if (env)
420 env->last_io_time = cpu_get_time_fast();
421#endif
bellard0824d6f2003-06-24 13:42:40 +0000422}
423
bellardc45886d2004-01-05 00:02:06 +0000424void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000425{
aliguorid12d51d2009-01-15 21:48:06 +0000426 LOG_IOPORT("outw: %04x %04x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000427 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000428#ifdef USE_KQEMU
429 if (env)
430 env->last_io_time = cpu_get_time_fast();
431#endif
bellard0824d6f2003-06-24 13:42:40 +0000432}
433
bellardc45886d2004-01-05 00:02:06 +0000434void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000435{
aliguorid12d51d2009-01-15 21:48:06 +0000436 LOG_IOPORT("outl: %04x %08x\n", addr, val);
aliguori477e3ed2008-07-23 15:19:59 +0000437 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000438#ifdef USE_KQEMU
439 if (env)
440 env->last_io_time = cpu_get_time_fast();
441#endif
bellard0824d6f2003-06-24 13:42:40 +0000442}
443
bellardc45886d2004-01-05 00:02:06 +0000444int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000445{
bellardfd872592004-05-12 19:11:15 +0000446 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000447 val = ioport_read(0, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000448 LOG_IOPORT("inb : %04x %02x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000449#ifdef USE_KQEMU
450 if (env)
451 env->last_io_time = cpu_get_time_fast();
452#endif
bellardfd872592004-05-12 19:11:15 +0000453 return val;
bellard0824d6f2003-06-24 13:42:40 +0000454}
455
bellardc45886d2004-01-05 00:02:06 +0000456int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000457{
bellardfd872592004-05-12 19:11:15 +0000458 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000459 val = ioport_read(1, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000460 LOG_IOPORT("inw : %04x %04x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000461#ifdef USE_KQEMU
462 if (env)
463 env->last_io_time = cpu_get_time_fast();
464#endif
bellardfd872592004-05-12 19:11:15 +0000465 return val;
bellard0824d6f2003-06-24 13:42:40 +0000466}
467
bellardc45886d2004-01-05 00:02:06 +0000468int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000469{
bellardfd872592004-05-12 19:11:15 +0000470 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000471 val = ioport_read(2, addr);
aliguorid12d51d2009-01-15 21:48:06 +0000472 LOG_IOPORT("inl : %04x %08x\n", addr, val);
bellard89bfc102006-02-08 22:46:31 +0000473#ifdef USE_KQEMU
474 if (env)
475 env->last_io_time = cpu_get_time_fast();
476#endif
bellardfd872592004-05-12 19:11:15 +0000477 return val;
bellard0824d6f2003-06-24 13:42:40 +0000478}
479
480/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000481void hw_error(const char *fmt, ...)
482{
483 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000484 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000485
486 va_start(ap, fmt);
487 fprintf(stderr, "qemu: hardware error: ");
488 vfprintf(stderr, fmt, ap);
489 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000490 for(env = first_cpu; env != NULL; env = env->next_cpu) {
491 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000492#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000493 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000494#else
bellard6a00d602005-11-21 23:25:50 +0000495 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000496#endif
bellard6a00d602005-11-21 23:25:50 +0000497 }
bellard0824d6f2003-06-24 13:42:40 +0000498 va_end(ap);
499 abort();
500}
aliguoridf751fa2008-12-04 20:19:35 +0000501
502/***************/
503/* ballooning */
504
505static QEMUBalloonEvent *qemu_balloon_event;
506void *qemu_balloon_event_opaque;
507
508void qemu_add_balloon_handler(QEMUBalloonEvent *func, void *opaque)
509{
510 qemu_balloon_event = func;
511 qemu_balloon_event_opaque = opaque;
512}
513
514void qemu_balloon(ram_addr_t target)
515{
516 if (qemu_balloon_event)
517 qemu_balloon_event(qemu_balloon_event_opaque, target);
518}
519
520ram_addr_t qemu_balloon_status(void)
521{
522 if (qemu_balloon_event)
523 return qemu_balloon_event(qemu_balloon_event_opaque, 0);
524 return 0;
525}
bellard0824d6f2003-06-24 13:42:40 +0000526
bellard8a7ddc32004-03-31 19:00:16 +0000527/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000528/* keyboard/mouse */
529
530static QEMUPutKBDEvent *qemu_put_kbd_event;
531static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000532static QEMUPutMouseEntry *qemu_put_mouse_event_head;
533static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000534
535void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
536{
537 qemu_put_kbd_event_opaque = opaque;
538 qemu_put_kbd_event = func;
539}
540
ths455204e2007-01-05 16:42:13 +0000541QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
542 void *opaque, int absolute,
543 const char *name)
bellard63066f42004-06-03 18:45:02 +0000544{
ths455204e2007-01-05 16:42:13 +0000545 QEMUPutMouseEntry *s, *cursor;
546
547 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
ths455204e2007-01-05 16:42:13 +0000548
549 s->qemu_put_mouse_event = func;
550 s->qemu_put_mouse_event_opaque = opaque;
551 s->qemu_put_mouse_event_absolute = absolute;
552 s->qemu_put_mouse_event_name = qemu_strdup(name);
553 s->next = NULL;
554
555 if (!qemu_put_mouse_event_head) {
556 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
557 return s;
558 }
559
560 cursor = qemu_put_mouse_event_head;
561 while (cursor->next != NULL)
562 cursor = cursor->next;
563
564 cursor->next = s;
565 qemu_put_mouse_event_current = s;
566
567 return s;
568}
569
570void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
571{
572 QEMUPutMouseEntry *prev = NULL, *cursor;
573
574 if (!qemu_put_mouse_event_head || entry == NULL)
575 return;
576
577 cursor = qemu_put_mouse_event_head;
578 while (cursor != NULL && cursor != entry) {
579 prev = cursor;
580 cursor = cursor->next;
581 }
582
583 if (cursor == NULL) // does not exist or list empty
584 return;
585 else if (prev == NULL) { // entry is head
586 qemu_put_mouse_event_head = cursor->next;
587 if (qemu_put_mouse_event_current == entry)
588 qemu_put_mouse_event_current = cursor->next;
589 qemu_free(entry->qemu_put_mouse_event_name);
590 qemu_free(entry);
591 return;
592 }
593
594 prev->next = entry->next;
595
596 if (qemu_put_mouse_event_current == entry)
597 qemu_put_mouse_event_current = prev;
598
599 qemu_free(entry->qemu_put_mouse_event_name);
600 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000601}
602
603void kbd_put_keycode(int keycode)
604{
605 if (qemu_put_kbd_event) {
606 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
607 }
608}
609
610void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
611{
ths455204e2007-01-05 16:42:13 +0000612 QEMUPutMouseEvent *mouse_event;
613 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000614 int width;
ths455204e2007-01-05 16:42:13 +0000615
616 if (!qemu_put_mouse_event_current) {
617 return;
618 }
619
620 mouse_event =
621 qemu_put_mouse_event_current->qemu_put_mouse_event;
622 mouse_event_opaque =
623 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
624
625 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000626 if (graphic_rotate) {
627 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
628 width = 0x7fff;
629 else
aurel32b94ed572008-03-10 19:34:27 +0000630 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000631 mouse_event(mouse_event_opaque,
632 width - dy, dx, dz, buttons_state);
633 } else
634 mouse_event(mouse_event_opaque,
635 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000636 }
637}
638
bellard09b26c52006-04-12 21:09:08 +0000639int kbd_mouse_is_absolute(void)
640{
ths455204e2007-01-05 16:42:13 +0000641 if (!qemu_put_mouse_event_current)
642 return 0;
643
644 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
645}
646
647void do_info_mice(void)
648{
649 QEMUPutMouseEntry *cursor;
650 int index = 0;
651
652 if (!qemu_put_mouse_event_head) {
653 term_printf("No mouse devices connected\n");
654 return;
655 }
656
657 term_printf("Mouse devices available:\n");
658 cursor = qemu_put_mouse_event_head;
659 while (cursor != NULL) {
660 term_printf("%c Mouse #%d: %s\n",
661 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
662 index, cursor->qemu_put_mouse_event_name);
663 index++;
664 cursor = cursor->next;
665 }
666}
667
668void do_mouse_set(int index)
669{
670 QEMUPutMouseEntry *cursor;
671 int i = 0;
672
673 if (!qemu_put_mouse_event_head) {
674 term_printf("No mouse devices connected\n");
675 return;
676 }
677
678 cursor = qemu_put_mouse_event_head;
679 while (cursor != NULL && index != i) {
680 i++;
681 cursor = cursor->next;
682 }
683
684 if (cursor != NULL)
685 qemu_put_mouse_event_current = cursor;
686 else
687 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000688}
689
bellard87858c82003-06-27 12:01:39 +0000690/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000691uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000692{
693 union {
694 uint64_t ll;
695 struct {
696#ifdef WORDS_BIGENDIAN
697 uint32_t high, low;
698#else
699 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000700#endif
bellard87858c82003-06-27 12:01:39 +0000701 } l;
702 } u, res;
703 uint64_t rl, rh;
704
705 u.ll = a;
706 rl = (uint64_t)u.l.low * (uint64_t)b;
707 rh = (uint64_t)u.l.high * (uint64_t)b;
708 rh += (rl >> 32);
709 res.l.high = rh / c;
710 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
711 return res.ll;
712}
713
bellard1dce7c32006-07-13 23:20:22 +0000714/***********************************************************/
715/* real time host monotonic timer */
716
717#define QEMU_TIMER_BASE 1000000000LL
718
719#ifdef WIN32
720
721static int64_t clock_freq;
722
723static void init_get_clock(void)
724{
bellarda8e5ac32006-07-14 09:36:13 +0000725 LARGE_INTEGER freq;
726 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000727 ret = QueryPerformanceFrequency(&freq);
728 if (ret == 0) {
729 fprintf(stderr, "Could not calibrate ticks\n");
730 exit(1);
731 }
732 clock_freq = freq.QuadPart;
733}
734
735static int64_t get_clock(void)
736{
737 LARGE_INTEGER ti;
738 QueryPerformanceCounter(&ti);
739 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
740}
741
742#else
743
744static int use_rt_clock;
745
746static void init_get_clock(void)
747{
748 use_rt_clock = 0;
aurel3260759372008-10-13 21:08:25 +0000749#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000750 {
751 struct timespec ts;
752 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
753 use_rt_clock = 1;
754 }
755 }
756#endif
757}
758
759static int64_t get_clock(void)
760{
aurel3260759372008-10-13 21:08:25 +0000761#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000762 if (use_rt_clock) {
763 struct timespec ts;
764 clock_gettime(CLOCK_MONOTONIC, &ts);
765 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000766 } else
bellard1dce7c32006-07-13 23:20:22 +0000767#endif
768 {
769 /* XXX: using gettimeofday leads to problems if the date
770 changes, so it should be avoided. */
771 struct timeval tv;
772 gettimeofday(&tv, NULL);
773 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
774 }
775}
bellard1dce7c32006-07-13 23:20:22 +0000776#endif
777
pbrook2e70f6e2008-06-29 01:03:05 +0000778/* Return the virtual CPU time, based on the instruction counter. */
779static int64_t cpu_get_icount(void)
780{
781 int64_t icount;
782 CPUState *env = cpu_single_env;;
783 icount = qemu_icount;
784 if (env) {
785 if (!can_do_io(env))
786 fprintf(stderr, "Bad clock read\n");
787 icount -= (env->icount_decr.u16.low + env->icount_extra);
788 }
789 return qemu_icount_bias + (icount << icount_time_shift);
790}
791
bellard1dce7c32006-07-13 23:20:22 +0000792/***********************************************************/
793/* guest cycle counter */
794
795static int64_t cpu_ticks_prev;
796static int64_t cpu_ticks_offset;
797static int64_t cpu_clock_offset;
798static int cpu_ticks_enabled;
799
800/* return the host CPU cycle counter and handle stop/restart */
801int64_t cpu_get_ticks(void)
802{
pbrook2e70f6e2008-06-29 01:03:05 +0000803 if (use_icount) {
804 return cpu_get_icount();
805 }
bellard1dce7c32006-07-13 23:20:22 +0000806 if (!cpu_ticks_enabled) {
807 return cpu_ticks_offset;
808 } else {
809 int64_t ticks;
810 ticks = cpu_get_real_ticks();
811 if (cpu_ticks_prev > ticks) {
812 /* Note: non increasing ticks may happen if the host uses
813 software suspend */
814 cpu_ticks_offset += cpu_ticks_prev - ticks;
815 }
816 cpu_ticks_prev = ticks;
817 return ticks + cpu_ticks_offset;
818 }
819}
820
821/* return the host CPU monotonic timer and handle stop/restart */
822static int64_t cpu_get_clock(void)
823{
824 int64_t ti;
825 if (!cpu_ticks_enabled) {
826 return cpu_clock_offset;
827 } else {
828 ti = get_clock();
829 return ti + cpu_clock_offset;
830 }
831}
832
833/* enable cpu_get_ticks() */
834void cpu_enable_ticks(void)
835{
836 if (!cpu_ticks_enabled) {
837 cpu_ticks_offset -= cpu_get_real_ticks();
838 cpu_clock_offset -= get_clock();
839 cpu_ticks_enabled = 1;
840 }
841}
842
843/* disable cpu_get_ticks() : the clock is stopped. You must not call
844 cpu_get_ticks() after that. */
845void cpu_disable_ticks(void)
846{
847 if (cpu_ticks_enabled) {
848 cpu_ticks_offset = cpu_get_ticks();
849 cpu_clock_offset = cpu_get_clock();
850 cpu_ticks_enabled = 0;
851 }
852}
853
854/***********************************************************/
855/* timers */
ths5fafdf22007-09-16 21:08:06 +0000856
bellard8a7ddc32004-03-31 19:00:16 +0000857#define QEMU_TIMER_REALTIME 0
858#define QEMU_TIMER_VIRTUAL 1
859
860struct QEMUClock {
861 int type;
862 /* XXX: add frequency */
863};
864
865struct QEMUTimer {
866 QEMUClock *clock;
867 int64_t expire_time;
868 QEMUTimerCB *cb;
869 void *opaque;
870 struct QEMUTimer *next;
871};
872
thsc8994012007-08-19 21:56:03 +0000873struct qemu_alarm_timer {
874 char const *name;
thsefe75412007-08-24 01:36:32 +0000875 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000876
877 int (*start)(struct qemu_alarm_timer *t);
878 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000879 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000880 void *priv;
881};
882
thsefe75412007-08-24 01:36:32 +0000883#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000884#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000885
886static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
887{
888 return t->flags & ALARM_FLAG_DYNTICKS;
889}
890
891static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
892{
893 if (!alarm_has_dynticks(t))
894 return;
895
896 t->rearm(t);
897}
898
899/* TODO: MIN_TIMER_REARM_US should be optimized */
900#define MIN_TIMER_REARM_US 250
901
thsc8994012007-08-19 21:56:03 +0000902static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000903#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000904static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000905#endif
thsc8994012007-08-19 21:56:03 +0000906
907#ifdef _WIN32
908
909struct qemu_alarm_win32 {
910 MMRESULT timerId;
911 HANDLE host_alarm;
912 unsigned int period;
913} alarm_win32_data = {0, NULL, -1};
914
915static int win32_start_timer(struct qemu_alarm_timer *t);
916static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000917static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000918
919#else
920
921static int unix_start_timer(struct qemu_alarm_timer *t);
922static void unix_stop_timer(struct qemu_alarm_timer *t);
923
ths231c6582007-08-26 17:29:15 +0000924#ifdef __linux__
925
thsefe75412007-08-24 01:36:32 +0000926static int dynticks_start_timer(struct qemu_alarm_timer *t);
927static void dynticks_stop_timer(struct qemu_alarm_timer *t);
928static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
929
thsc40ec5a2007-08-19 22:09:40 +0000930static int hpet_start_timer(struct qemu_alarm_timer *t);
931static void hpet_stop_timer(struct qemu_alarm_timer *t);
932
thsc8994012007-08-19 21:56:03 +0000933static int rtc_start_timer(struct qemu_alarm_timer *t);
934static void rtc_stop_timer(struct qemu_alarm_timer *t);
935
thsefe75412007-08-24 01:36:32 +0000936#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000937
938#endif /* _WIN32 */
939
pbrook2e70f6e2008-06-29 01:03:05 +0000940/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000941 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000942 When the guest is idle real and virtual time will be aligned in
943 the IO wait loop. */
944#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
945
946static void icount_adjust(void)
947{
948 int64_t cur_time;
949 int64_t cur_icount;
950 int64_t delta;
951 static int64_t last_delta;
952 /* If the VM is not running, then do nothing. */
953 if (!vm_running)
954 return;
955
956 cur_time = cpu_get_clock();
957 cur_icount = qemu_get_clock(vm_clock);
958 delta = cur_icount - cur_time;
959 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
960 if (delta > 0
961 && last_delta + ICOUNT_WOBBLE < delta * 2
962 && icount_time_shift > 0) {
963 /* The guest is getting too far ahead. Slow time down. */
964 icount_time_shift--;
965 }
966 if (delta < 0
967 && last_delta - ICOUNT_WOBBLE > delta * 2
968 && icount_time_shift < MAX_ICOUNT_SHIFT) {
969 /* The guest is getting too far behind. Speed time up. */
970 icount_time_shift++;
971 }
972 last_delta = delta;
973 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
974}
975
976static void icount_adjust_rt(void * opaque)
977{
978 qemu_mod_timer(icount_rt_timer,
979 qemu_get_clock(rt_clock) + 1000);
980 icount_adjust();
981}
982
983static void icount_adjust_vm(void * opaque)
984{
985 qemu_mod_timer(icount_vm_timer,
986 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
987 icount_adjust();
988}
989
990static void init_icount_adjust(void)
991{
992 /* Have both realtime and virtual time triggers for speed adjustment.
993 The realtime trigger catches emulated time passing too slowly,
994 the virtual time trigger catches emulated time passing too fast.
995 Realtime triggers occur even when idle, so use them less frequently
996 than VM triggers. */
997 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
998 qemu_mod_timer(icount_rt_timer,
999 qemu_get_clock(rt_clock) + 1000);
1000 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
1001 qemu_mod_timer(icount_vm_timer,
1002 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
1003}
1004
thsc8994012007-08-19 21:56:03 +00001005static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +00001006#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +00001007#ifdef __linux__
thsefe75412007-08-24 01:36:32 +00001008 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1009 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001010 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001011 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001012 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001013 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001014#endif
thsefe75412007-08-24 01:36:32 +00001015 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001016#else
thsefe75412007-08-24 01:36:32 +00001017 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1018 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1019 {"win32", 0, win32_start_timer,
1020 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001021#endif
1022 {NULL, }
1023};
1024
blueswir13f47aa82008-03-09 06:59:01 +00001025static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001026{
1027 int i;
1028
1029 printf("Available alarm timers, in order of precedence:\n");
1030 for (i = 0; alarm_timers[i].name; i++)
1031 printf("%s\n", alarm_timers[i].name);
1032}
1033
1034static void configure_alarms(char const *opt)
1035{
1036 int i;
1037 int cur = 0;
malcb1503cd2008-12-22 20:33:55 +00001038 int count = ARRAY_SIZE(alarm_timers) - 1;
thsf3dcfad2007-08-24 01:26:02 +00001039 char *arg;
1040 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001041 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001042
aurel323adda042008-03-09 23:43:49 +00001043 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001044 show_available_alarms();
1045 exit(0);
1046 }
1047
1048 arg = strdup(opt);
1049
1050 /* Reorder the array */
1051 name = strtok(arg, ",");
1052 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001053 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001054 if (!strcmp(alarm_timers[i].name, name))
1055 break;
1056 }
1057
1058 if (i == count) {
1059 fprintf(stderr, "Unknown clock %s\n", name);
1060 goto next;
1061 }
1062
1063 if (i < cur)
1064 /* Ignore */
1065 goto next;
1066
1067 /* Swap */
1068 tmp = alarm_timers[i];
1069 alarm_timers[i] = alarm_timers[cur];
1070 alarm_timers[cur] = tmp;
1071
1072 cur++;
1073next:
1074 name = strtok(NULL, ",");
1075 }
1076
1077 free(arg);
1078
1079 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001080 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001081 for (i = cur; i < count; i++)
1082 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001083 } else {
1084 show_available_alarms();
1085 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001086 }
thsf3dcfad2007-08-24 01:26:02 +00001087}
1088
bellard8a7ddc32004-03-31 19:00:16 +00001089QEMUClock *rt_clock;
1090QEMUClock *vm_clock;
1091
1092static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001093
pbrook9596ebb2007-11-18 01:44:38 +00001094static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001095{
1096 QEMUClock *clock;
1097 clock = qemu_mallocz(sizeof(QEMUClock));
bellard8a7ddc32004-03-31 19:00:16 +00001098 clock->type = type;
1099 return clock;
1100}
1101
1102QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1103{
1104 QEMUTimer *ts;
1105
1106 ts = qemu_mallocz(sizeof(QEMUTimer));
1107 ts->clock = clock;
1108 ts->cb = cb;
1109 ts->opaque = opaque;
1110 return ts;
1111}
1112
1113void qemu_free_timer(QEMUTimer *ts)
1114{
1115 qemu_free(ts);
1116}
1117
1118/* stop a timer, but do not dealloc it */
1119void qemu_del_timer(QEMUTimer *ts)
1120{
1121 QEMUTimer **pt, *t;
1122
1123 /* NOTE: this code must be signal safe because
1124 qemu_timer_expired() can be called from a signal. */
1125 pt = &active_timers[ts->clock->type];
1126 for(;;) {
1127 t = *pt;
1128 if (!t)
1129 break;
1130 if (t == ts) {
1131 *pt = t->next;
1132 break;
1133 }
1134 pt = &t->next;
1135 }
1136}
1137
1138/* modify the current timer so that it will be fired when current_time
1139 >= expire_time. The corresponding callback will be called. */
1140void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1141{
1142 QEMUTimer **pt, *t;
1143
1144 qemu_del_timer(ts);
1145
1146 /* add the timer in the sorted list */
1147 /* NOTE: this code must be signal safe because
1148 qemu_timer_expired() can be called from a signal. */
1149 pt = &active_timers[ts->clock->type];
1150 for(;;) {
1151 t = *pt;
1152 if (!t)
1153 break;
ths5fafdf22007-09-16 21:08:06 +00001154 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001155 break;
1156 pt = &t->next;
1157 }
1158 ts->expire_time = expire_time;
1159 ts->next = *pt;
1160 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001161
1162 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001163 if (pt == &active_timers[ts->clock->type]) {
1164 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1165 qemu_rearm_alarm_timer(alarm_timer);
1166 }
1167 /* Interrupt execution to force deadline recalculation. */
1168 if (use_icount && cpu_single_env) {
1169 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1170 }
1171 }
bellard8a7ddc32004-03-31 19:00:16 +00001172}
1173
1174int qemu_timer_pending(QEMUTimer *ts)
1175{
1176 QEMUTimer *t;
1177 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1178 if (t == ts)
1179 return 1;
1180 }
1181 return 0;
1182}
1183
1184static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1185{
1186 if (!timer_head)
1187 return 0;
1188 return (timer_head->expire_time <= current_time);
1189}
1190
1191static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1192{
1193 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001194
bellard8a7ddc32004-03-31 19:00:16 +00001195 for(;;) {
1196 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001197 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001198 break;
1199 /* remove timer from the list before calling the callback */
1200 *ptimer_head = ts->next;
1201 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001202
bellard8a7ddc32004-03-31 19:00:16 +00001203 /* run the callback (the timer list can be modified) */
1204 ts->cb(ts->opaque);
1205 }
1206}
1207
1208int64_t qemu_get_clock(QEMUClock *clock)
1209{
1210 switch(clock->type) {
1211 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001212 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001213 default:
1214 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001215 if (use_icount) {
1216 return cpu_get_icount();
1217 } else {
1218 return cpu_get_clock();
1219 }
bellard8a7ddc32004-03-31 19:00:16 +00001220 }
1221}
1222
bellard1dce7c32006-07-13 23:20:22 +00001223static void init_timers(void)
1224{
1225 init_get_clock();
1226 ticks_per_sec = QEMU_TIMER_BASE;
1227 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1228 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1229}
1230
bellard8a7ddc32004-03-31 19:00:16 +00001231/* save a timer */
1232void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1233{
1234 uint64_t expire_time;
1235
1236 if (qemu_timer_pending(ts)) {
1237 expire_time = ts->expire_time;
1238 } else {
1239 expire_time = -1;
1240 }
1241 qemu_put_be64(f, expire_time);
1242}
1243
1244void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1245{
1246 uint64_t expire_time;
1247
1248 expire_time = qemu_get_be64(f);
1249 if (expire_time != -1) {
1250 qemu_mod_timer(ts, expire_time);
1251 } else {
1252 qemu_del_timer(ts);
1253 }
1254}
1255
1256static void timer_save(QEMUFile *f, void *opaque)
1257{
1258 if (cpu_ticks_enabled) {
1259 hw_error("cannot save state if virtual timers are running");
1260 }
thsbee8d682007-12-16 23:41:11 +00001261 qemu_put_be64(f, cpu_ticks_offset);
1262 qemu_put_be64(f, ticks_per_sec);
1263 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001264}
1265
1266static int timer_load(QEMUFile *f, void *opaque, int version_id)
1267{
bellardc88676f2006-08-06 13:36:11 +00001268 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001269 return -EINVAL;
1270 if (cpu_ticks_enabled) {
1271 return -EINVAL;
1272 }
thsbee8d682007-12-16 23:41:11 +00001273 cpu_ticks_offset=qemu_get_be64(f);
1274 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001275 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001276 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001277 }
bellard8a7ddc32004-03-31 19:00:16 +00001278 return 0;
1279}
1280
bellard67b915a2004-03-31 23:37:16 +00001281#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001282void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001283 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1284#else
bellard8a7ddc32004-03-31 19:00:16 +00001285static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001286#endif
bellard8a7ddc32004-03-31 19:00:16 +00001287{
bellard02ba45c2004-06-25 14:46:23 +00001288#if 0
1289#define DISP_FREQ 1000
1290 {
1291 static int64_t delta_min = INT64_MAX;
1292 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1293 static int count;
1294 ti = qemu_get_clock(vm_clock);
1295 if (last_clock != 0) {
1296 delta = ti - last_clock;
1297 if (delta < delta_min)
1298 delta_min = delta;
1299 if (delta > delta_max)
1300 delta_max = delta;
1301 delta_cum += delta;
1302 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001303 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001304 muldiv64(delta_min, 1000000, ticks_per_sec),
1305 muldiv64(delta_max, 1000000, ticks_per_sec),
1306 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1307 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1308 count = 0;
1309 delta_min = INT64_MAX;
1310 delta_max = 0;
1311 delta_cum = 0;
1312 }
1313 }
1314 last_clock = ti;
1315 }
1316#endif
thsefe75412007-08-24 01:36:32 +00001317 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001318 (!use_icount &&
1319 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1320 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001321 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1322 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001323 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001324
bellard06d9f2f2006-05-01 13:23:04 +00001325#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001326 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1327 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001328#else
1329 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001330 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001331#endif
balrogd5d08332008-01-05 19:41:47 +00001332 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1333
balrog4f8eb8d2007-12-16 12:39:38 +00001334 if (env) {
1335 /* stop the currently executing cpu because a timer occured */
1336 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001337#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001338 if (env->kqemu_enabled) {
1339 kqemu_cpu_interrupt(env);
1340 }
balrogee5605e2007-12-03 03:01:40 +00001341#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001342 }
balrogee5605e2007-12-03 03:01:40 +00001343 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001344 }
1345}
1346
pbrook2e70f6e2008-06-29 01:03:05 +00001347static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001348{
pbrook2e70f6e2008-06-29 01:03:05 +00001349 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001350
1351 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001352 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1353 qemu_get_clock(vm_clock);
1354 } else {
1355 /* To avoid problems with overflow limit this to 2^32. */
1356 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001357 }
1358
pbrook2e70f6e2008-06-29 01:03:05 +00001359 if (delta < 0)
1360 delta = 0;
thsefe75412007-08-24 01:36:32 +00001361
pbrook2e70f6e2008-06-29 01:03:05 +00001362 return delta;
1363}
1364
blueswir18632fb92008-09-14 13:59:34 +00001365#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001366static uint64_t qemu_next_deadline_dyntick(void)
1367{
1368 int64_t delta;
1369 int64_t rtdelta;
1370
1371 if (use_icount)
1372 delta = INT32_MAX;
1373 else
1374 delta = (qemu_next_deadline() + 999) / 1000;
1375
1376 if (active_timers[QEMU_TIMER_REALTIME]) {
1377 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1378 qemu_get_clock(rt_clock))*1000;
1379 if (rtdelta < delta)
1380 delta = rtdelta;
1381 }
1382
1383 if (delta < MIN_TIMER_REARM_US)
1384 delta = MIN_TIMER_REARM_US;
1385
1386 return delta;
thsefe75412007-08-24 01:36:32 +00001387}
blueswir18632fb92008-09-14 13:59:34 +00001388#endif
thsefe75412007-08-24 01:36:32 +00001389
bellardfd872592004-05-12 19:11:15 +00001390#ifndef _WIN32
1391
aliguori7183b4b2008-11-05 20:40:18 +00001392/* Sets a specific flag */
1393static int fcntl_setfl(int fd, int flag)
1394{
1395 int flags;
1396
1397 flags = fcntl(fd, F_GETFL);
1398 if (flags == -1)
1399 return -errno;
1400
1401 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1402 return -errno;
1403
1404 return 0;
1405}
1406
bellard829309c2004-05-20 13:20:12 +00001407#if defined(__linux__)
1408
bellardfd872592004-05-12 19:11:15 +00001409#define RTC_FREQ 1024
1410
aurel32de9a95f2008-11-11 13:41:01 +00001411static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001412{
thsc8994012007-08-19 21:56:03 +00001413 struct sigaction act;
1414
1415 /* timer signal */
1416 sigfillset(&act.sa_mask);
1417 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001418 act.sa_handler = host_alarm_handler;
1419
1420 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001421 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001422 fcntl(fd, F_SETOWN, getpid());
1423}
1424
thsc40ec5a2007-08-19 22:09:40 +00001425static int hpet_start_timer(struct qemu_alarm_timer *t)
1426{
1427 struct hpet_info info;
1428 int r, fd;
1429
1430 fd = open("/dev/hpet", O_RDONLY);
1431 if (fd < 0)
1432 return -1;
1433
1434 /* Set frequency */
1435 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1436 if (r < 0) {
1437 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1438 "error, but for better emulation accuracy type:\n"
1439 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1440 goto fail;
1441 }
1442
1443 /* Check capabilities */
1444 r = ioctl(fd, HPET_INFO, &info);
1445 if (r < 0)
1446 goto fail;
1447
1448 /* Enable periodic mode */
1449 r = ioctl(fd, HPET_EPI, 0);
1450 if (info.hi_flags && (r < 0))
1451 goto fail;
1452
1453 /* Enable interrupt */
1454 r = ioctl(fd, HPET_IE_ON, 0);
1455 if (r < 0)
1456 goto fail;
1457
1458 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001459 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001460
1461 return 0;
1462fail:
1463 close(fd);
1464 return -1;
1465}
1466
1467static void hpet_stop_timer(struct qemu_alarm_timer *t)
1468{
pbrookfcdc2122007-08-23 20:22:22 +00001469 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001470
1471 close(fd);
1472}
1473
thsc8994012007-08-19 21:56:03 +00001474static int rtc_start_timer(struct qemu_alarm_timer *t)
1475{
1476 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001477 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001478
balrogaeb30be2007-07-02 15:03:13 +00001479 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001480 if (rtc_fd < 0)
1481 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001482 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1483 if (current_rtc_freq != RTC_FREQ &&
1484 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001485 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1486 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1487 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1488 goto fail;
1489 }
1490 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1491 fail:
1492 close(rtc_fd);
1493 return -1;
1494 }
thsc8994012007-08-19 21:56:03 +00001495
1496 enable_sigio_timer(rtc_fd);
1497
pbrookfcdc2122007-08-23 20:22:22 +00001498 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001499
bellardfd872592004-05-12 19:11:15 +00001500 return 0;
1501}
1502
thsc8994012007-08-19 21:56:03 +00001503static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001504{
pbrookfcdc2122007-08-23 20:22:22 +00001505 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001506
1507 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001508}
1509
thsefe75412007-08-24 01:36:32 +00001510static int dynticks_start_timer(struct qemu_alarm_timer *t)
1511{
1512 struct sigevent ev;
1513 timer_t host_timer;
1514 struct sigaction act;
1515
1516 sigfillset(&act.sa_mask);
1517 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001518 act.sa_handler = host_alarm_handler;
1519
1520 sigaction(SIGALRM, &act, NULL);
1521
1522 ev.sigev_value.sival_int = 0;
1523 ev.sigev_notify = SIGEV_SIGNAL;
1524 ev.sigev_signo = SIGALRM;
1525
1526 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1527 perror("timer_create");
1528
1529 /* disable dynticks */
1530 fprintf(stderr, "Dynamic Ticks disabled\n");
1531
1532 return -1;
1533 }
1534
blueswir10399bfe2008-11-16 11:37:18 +00001535 t->priv = (void *)(long)host_timer;
thsefe75412007-08-24 01:36:32 +00001536
1537 return 0;
1538}
1539
1540static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1541{
blueswir10399bfe2008-11-16 11:37:18 +00001542 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001543
1544 timer_delete(host_timer);
1545}
1546
1547static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1548{
blueswir10399bfe2008-11-16 11:37:18 +00001549 timer_t host_timer = (timer_t)(long)t->priv;
thsefe75412007-08-24 01:36:32 +00001550 struct itimerspec timeout;
1551 int64_t nearest_delta_us = INT64_MAX;
1552 int64_t current_us;
1553
1554 if (!active_timers[QEMU_TIMER_REALTIME] &&
1555 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001556 return;
thsefe75412007-08-24 01:36:32 +00001557
pbrook2e70f6e2008-06-29 01:03:05 +00001558 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001559
1560 /* check whether a timer is already running */
1561 if (timer_gettime(host_timer, &timeout)) {
1562 perror("gettime");
1563 fprintf(stderr, "Internal timer error: aborting\n");
1564 exit(1);
1565 }
1566 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1567 if (current_us && current_us <= nearest_delta_us)
1568 return;
1569
1570 timeout.it_interval.tv_sec = 0;
1571 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1572 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1573 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1574 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1575 perror("settime");
1576 fprintf(stderr, "Internal timer error: aborting\n");
1577 exit(1);
1578 }
1579}
1580
ths70744b32007-08-26 17:31:30 +00001581#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001582
thsc8994012007-08-19 21:56:03 +00001583static int unix_start_timer(struct qemu_alarm_timer *t)
1584{
1585 struct sigaction act;
1586 struct itimerval itv;
1587 int err;
1588
1589 /* timer signal */
1590 sigfillset(&act.sa_mask);
1591 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001592 act.sa_handler = host_alarm_handler;
1593
1594 sigaction(SIGALRM, &act, NULL);
1595
1596 itv.it_interval.tv_sec = 0;
1597 /* for i386 kernel 2.6 to get 1 ms */
1598 itv.it_interval.tv_usec = 999;
1599 itv.it_value.tv_sec = 0;
1600 itv.it_value.tv_usec = 10 * 1000;
1601
1602 err = setitimer(ITIMER_REAL, &itv, NULL);
1603 if (err)
1604 return -1;
1605
1606 return 0;
1607}
1608
1609static void unix_stop_timer(struct qemu_alarm_timer *t)
1610{
1611 struct itimerval itv;
1612
1613 memset(&itv, 0, sizeof(itv));
1614 setitimer(ITIMER_REAL, &itv, NULL);
1615}
1616
bellard829309c2004-05-20 13:20:12 +00001617#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001618
aliguorif49e58d2008-11-05 21:22:34 +00001619static void try_to_rearm_timer(void *opaque)
1620{
1621 struct qemu_alarm_timer *t = opaque;
1622#ifndef _WIN32
1623 ssize_t len;
1624
1625 /* Drain the notify pipe */
1626 do {
1627 char buffer[512];
1628 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1629 } while ((len == -1 && errno == EINTR) || len > 0);
1630#endif
1631
aliguorif49e58d2008-11-05 21:22:34 +00001632 if (t->flags & ALARM_FLAG_EXPIRED) {
1633 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1634 qemu_rearm_alarm_timer(alarm_timer);
1635 }
1636}
1637
thsc8994012007-08-19 21:56:03 +00001638#ifdef _WIN32
1639
1640static int win32_start_timer(struct qemu_alarm_timer *t)
1641{
1642 TIMECAPS tc;
1643 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001644 UINT flags;
thsc8994012007-08-19 21:56:03 +00001645
1646 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1647 if (!data->host_alarm) {
1648 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001649 return -1;
thsc8994012007-08-19 21:56:03 +00001650 }
1651
1652 memset(&tc, 0, sizeof(tc));
1653 timeGetDevCaps(&tc, sizeof(tc));
1654
1655 if (data->period < tc.wPeriodMin)
1656 data->period = tc.wPeriodMin;
1657
1658 timeBeginPeriod(data->period);
1659
thsefe75412007-08-24 01:36:32 +00001660 flags = TIME_CALLBACK_FUNCTION;
1661 if (alarm_has_dynticks(t))
1662 flags |= TIME_ONESHOT;
1663 else
1664 flags |= TIME_PERIODIC;
1665
thsc8994012007-08-19 21:56:03 +00001666 data->timerId = timeSetEvent(1, // interval (ms)
1667 data->period, // resolution
1668 host_alarm_handler, // function
1669 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001670 flags);
thsc8994012007-08-19 21:56:03 +00001671
1672 if (!data->timerId) {
1673 perror("Failed to initialize win32 alarm timer");
1674
1675 timeEndPeriod(data->period);
1676 CloseHandle(data->host_alarm);
1677 return -1;
1678 }
1679
aliguorif49e58d2008-11-05 21:22:34 +00001680 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001681
1682 return 0;
1683}
1684
1685static void win32_stop_timer(struct qemu_alarm_timer *t)
1686{
1687 struct qemu_alarm_win32 *data = t->priv;
1688
1689 timeKillEvent(data->timerId);
1690 timeEndPeriod(data->period);
1691
1692 CloseHandle(data->host_alarm);
1693}
1694
thsefe75412007-08-24 01:36:32 +00001695static void win32_rearm_timer(struct qemu_alarm_timer *t)
1696{
1697 struct qemu_alarm_win32 *data = t->priv;
1698 uint64_t nearest_delta_us;
1699
1700 if (!active_timers[QEMU_TIMER_REALTIME] &&
1701 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001702 return;
thsefe75412007-08-24 01:36:32 +00001703
pbrook2e70f6e2008-06-29 01:03:05 +00001704 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001705 nearest_delta_us /= 1000;
1706
1707 timeKillEvent(data->timerId);
1708
1709 data->timerId = timeSetEvent(1,
1710 data->period,
1711 host_alarm_handler,
1712 (DWORD)t,
1713 TIME_ONESHOT | TIME_PERIODIC);
1714
1715 if (!data->timerId) {
1716 perror("Failed to re-arm win32 alarm timer");
1717
1718 timeEndPeriod(data->period);
1719 CloseHandle(data->host_alarm);
1720 exit(1);
1721 }
1722}
1723
thsc8994012007-08-19 21:56:03 +00001724#endif /* _WIN32 */
1725
aliguori7183b4b2008-11-05 20:40:18 +00001726static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001727{
blueswir1223f0d72008-09-30 18:12:18 +00001728 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001729 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001730
1731#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001732 int fds[2];
1733
aliguori7183b4b2008-11-05 20:40:18 +00001734 err = pipe(fds);
1735 if (err == -1)
1736 return -errno;
1737
1738 err = fcntl_setfl(fds[0], O_NONBLOCK);
1739 if (err < 0)
1740 goto fail;
1741
1742 err = fcntl_setfl(fds[1], O_NONBLOCK);
1743 if (err < 0)
1744 goto fail;
1745
aliguoric96f1a42008-11-05 20:29:45 +00001746 alarm_timer_rfd = fds[0];
1747 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001748#endif
bellard06d9f2f2006-05-01 13:23:04 +00001749
thsc8994012007-08-19 21:56:03 +00001750 for (i = 0; alarm_timers[i].name; i++) {
1751 t = &alarm_timers[i];
1752
thsc8994012007-08-19 21:56:03 +00001753 err = t->start(t);
1754 if (!err)
1755 break;
bellard67b915a2004-03-31 23:37:16 +00001756 }
bellardfd872592004-05-12 19:11:15 +00001757
thsc8994012007-08-19 21:56:03 +00001758 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001759 err = -ENOENT;
1760 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001761 }
thsc8994012007-08-19 21:56:03 +00001762
aliguorif49e58d2008-11-05 21:22:34 +00001763#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001764 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1765 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001766#endif
aliguori6abfbd72008-11-05 20:49:37 +00001767
thsc8994012007-08-19 21:56:03 +00001768 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001769
aliguori6abfbd72008-11-05 20:49:37 +00001770 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001771
1772fail:
aliguorif49e58d2008-11-05 21:22:34 +00001773#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001774 close(fds[0]);
1775 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001776#endif
aliguori7183b4b2008-11-05 20:40:18 +00001777 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001778}
1779
pbrook9596ebb2007-11-18 01:44:38 +00001780static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001781{
thsc8994012007-08-19 21:56:03 +00001782 alarm_timer->stop(alarm_timer);
1783 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001784}
1785
bellardc4b1fcc2004-03-14 21:44:30 +00001786/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001787/* host time/date access */
1788void qemu_get_timedate(struct tm *tm, int offset)
1789{
1790 time_t ti;
1791 struct tm *ret;
1792
1793 time(&ti);
1794 ti += offset;
1795 if (rtc_date_offset == -1) {
1796 if (rtc_utc)
1797 ret = gmtime(&ti);
1798 else
1799 ret = localtime(&ti);
1800 } else {
1801 ti -= rtc_date_offset;
1802 ret = gmtime(&ti);
1803 }
1804
1805 memcpy(tm, ret, sizeof(struct tm));
1806}
1807
1808int qemu_timedate_diff(struct tm *tm)
1809{
1810 time_t seconds;
1811
1812 if (rtc_date_offset == -1)
1813 if (rtc_utc)
1814 seconds = mktimegm(tm);
1815 else
1816 seconds = mktime(tm);
1817 else
1818 seconds = mktimegm(tm) + rtc_date_offset;
1819
1820 return seconds - time(NULL);
1821}
1822
bellardfd1dff42006-02-01 21:29:26 +00001823#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001824static void socket_cleanup(void)
1825{
1826 WSACleanup();
1827}
bellard82c643f2004-07-14 17:28:13 +00001828
bellardfd1dff42006-02-01 21:29:26 +00001829static int socket_init(void)
1830{
1831 WSADATA Data;
1832 int ret, err;
1833
1834 ret = WSAStartup(MAKEWORD(2,2), &Data);
1835 if (ret != 0) {
1836 err = WSAGetLastError();
1837 fprintf(stderr, "WSAStartup: %d\n", err);
1838 return -1;
1839 }
1840 atexit(socket_cleanup);
1841 return 0;
1842}
aurel3264b7b732008-05-05 10:05:31 +00001843#endif
1844
aliguori63a01ef2008-10-31 19:10:00 +00001845const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001846{
1847 char *q;
thse4bcb142007-12-02 04:51:10 +00001848
balrog609497a2008-01-14 02:56:53 +00001849 q = buf;
1850 while (*p != '\0' && *p != '=') {
1851 if (q && (q - buf) < buf_size - 1)
1852 *q++ = *p;
1853 p++;
1854 }
1855 if (q)
1856 *q = '\0';
1857
1858 return p;
1859}
1860
aliguori63a01ef2008-10-31 19:10:00 +00001861const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001862{
1863 char *q;
1864
thse4bcb142007-12-02 04:51:10 +00001865 q = buf;
1866 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001867 if (*p == ',') {
1868 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001869 break;
thse4bcb142007-12-02 04:51:10 +00001870 p++;
balrog609497a2008-01-14 02:56:53 +00001871 }
thse4bcb142007-12-02 04:51:10 +00001872 if (q && (q - buf) < buf_size - 1)
1873 *q++ = *p;
1874 p++;
1875 }
1876 if (q)
1877 *q = '\0';
1878
1879 return p;
1880}
1881
aliguori63a01ef2008-10-31 19:10:00 +00001882int get_param_value(char *buf, int buf_size,
1883 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001884{
1885 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001886 char option[128];
1887
1888 p = str;
1889 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001890 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001891 if (*p != '=')
1892 break;
1893 p++;
1894 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001895 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001896 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001897 } else {
balrog609497a2008-01-14 02:56:53 +00001898 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001899 }
1900 if (*p != ',')
1901 break;
1902 p++;
1903 }
1904 return 0;
1905}
1906
aliguori63a01ef2008-10-31 19:10:00 +00001907int check_params(char *buf, int buf_size,
1908 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001909{
1910 const char *p;
1911 int i;
1912
1913 p = str;
1914 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001915 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001916 if (*p != '=')
1917 return -1;
1918 p++;
1919 for(i = 0; params[i] != NULL; i++)
1920 if (!strcmp(params[i], buf))
1921 break;
1922 if (params[i] == NULL)
1923 return -1;
balrog609497a2008-01-14 02:56:53 +00001924 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001925 if (*p != ',')
1926 break;
1927 p++;
1928 }
1929 return 0;
1930}
1931
balrog1ae26a12008-09-28 23:19:47 +00001932/***********************************************************/
1933/* Bluetooth support */
1934static int nb_hcis;
1935static int cur_hci;
1936static struct HCIInfo *hci_table[MAX_NICS];
balrogdc72ac12008-11-09 00:04:26 +00001937
balrog1ae26a12008-09-28 23:19:47 +00001938static struct bt_vlan_s {
1939 struct bt_scatternet_s net;
1940 int id;
1941 struct bt_vlan_s *next;
1942} *first_bt_vlan;
1943
1944/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001945static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001946{
1947 struct bt_vlan_s **pvlan, *vlan;
1948 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1949 if (vlan->id == id)
1950 return &vlan->net;
1951 }
1952 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1953 vlan->id = id;
1954 pvlan = &first_bt_vlan;
1955 while (*pvlan != NULL)
1956 pvlan = &(*pvlan)->next;
1957 *pvlan = vlan;
1958 return &vlan->net;
1959}
1960
1961static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1962{
1963}
1964
1965static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1966{
1967 return -ENOTSUP;
1968}
1969
1970static struct HCIInfo null_hci = {
1971 .cmd_send = null_hci_send,
1972 .sco_send = null_hci_send,
1973 .acl_send = null_hci_send,
1974 .bdaddr_set = null_hci_addr_set,
1975};
1976
1977struct HCIInfo *qemu_next_hci(void)
1978{
1979 if (cur_hci == nb_hcis)
1980 return &null_hci;
1981
1982 return hci_table[cur_hci++];
1983}
1984
balrogdc72ac12008-11-09 00:04:26 +00001985static struct HCIInfo *hci_init(const char *str)
1986{
1987 char *endp;
1988 struct bt_scatternet_s *vlan = 0;
1989
1990 if (!strcmp(str, "null"))
1991 /* null */
1992 return &null_hci;
1993 else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1994 /* host[:hciN] */
1995 return bt_host_hci(str[4] ? str + 5 : "hci0");
1996 else if (!strncmp(str, "hci", 3)) {
1997 /* hci[,vlan=n] */
1998 if (str[3]) {
1999 if (!strncmp(str + 3, ",vlan=", 6)) {
2000 vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2001 if (*endp)
2002 vlan = 0;
2003 }
2004 } else
2005 vlan = qemu_find_bt_vlan(0);
2006 if (vlan)
2007 return bt_new_hci(vlan);
2008 }
2009
2010 fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2011
2012 return 0;
2013}
2014
2015static int bt_hci_parse(const char *str)
2016{
2017 struct HCIInfo *hci;
2018 bdaddr_t bdaddr;
2019
2020 if (nb_hcis >= MAX_NICS) {
2021 fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2022 return -1;
2023 }
2024
2025 hci = hci_init(str);
2026 if (!hci)
2027 return -1;
2028
2029 bdaddr.b[0] = 0x52;
2030 bdaddr.b[1] = 0x54;
2031 bdaddr.b[2] = 0x00;
2032 bdaddr.b[3] = 0x12;
2033 bdaddr.b[4] = 0x34;
2034 bdaddr.b[5] = 0x56 + nb_hcis;
2035 hci->bdaddr_set(hci, bdaddr.b);
2036
2037 hci_table[nb_hcis++] = hci;
2038
2039 return 0;
2040}
2041
2042static void bt_vhci_add(int vlan_id)
2043{
2044 struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2045
2046 if (!vlan->slave)
2047 fprintf(stderr, "qemu: warning: adding a VHCI to "
2048 "an empty scatternet %i\n", vlan_id);
2049
2050 bt_vhci_init(bt_new_hci(vlan));
2051}
2052
2053static struct bt_device_s *bt_device_add(const char *opt)
2054{
2055 struct bt_scatternet_s *vlan;
2056 int vlan_id = 0;
2057 char *endp = strstr(opt, ",vlan=");
2058 int len = (endp ? endp - opt : strlen(opt)) + 1;
2059 char devname[10];
2060
2061 pstrcpy(devname, MIN(sizeof(devname), len), opt);
2062
2063 if (endp) {
2064 vlan_id = strtol(endp + 6, &endp, 0);
2065 if (*endp) {
2066 fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2067 return 0;
2068 }
2069 }
2070
2071 vlan = qemu_find_bt_vlan(vlan_id);
2072
2073 if (!vlan->slave)
2074 fprintf(stderr, "qemu: warning: adding a slave device to "
2075 "an empty scatternet %i\n", vlan_id);
2076
2077 if (!strcmp(devname, "keyboard"))
2078 return bt_keyboard_init(vlan);
2079
2080 fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2081 return 0;
2082}
2083
2084static int bt_parse(const char *opt)
2085{
2086 const char *endp, *p;
2087 int vlan;
2088
2089 if (strstart(opt, "hci", &endp)) {
2090 if (!*endp || *endp == ',') {
2091 if (*endp)
2092 if (!strstart(endp, ",vlan=", 0))
2093 opt = endp + 1;
2094
2095 return bt_hci_parse(opt);
2096 }
2097 } else if (strstart(opt, "vhci", &endp)) {
2098 if (!*endp || *endp == ',') {
2099 if (*endp) {
2100 if (strstart(endp, ",vlan=", &p)) {
2101 vlan = strtol(p, (char **) &endp, 0);
2102 if (*endp) {
2103 fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2104 return 1;
2105 }
2106 } else {
2107 fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2108 return 1;
2109 }
2110 } else
2111 vlan = 0;
2112
2113 bt_vhci_add(vlan);
2114 return 0;
2115 }
2116 } else if (strstart(opt, "device:", &endp))
2117 return !bt_device_add(endp);
2118
2119 fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2120 return 1;
2121}
2122
balrog1ae26a12008-09-28 23:19:47 +00002123/***********************************************************/
2124/* QEMU Block devices */
2125
balrog609497a2008-01-14 02:56:53 +00002126#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00002127#ifdef TARGET_PPC
2128#define CDROM_ALIAS "index=1,media=cdrom"
2129#else
2130#define CDROM_ALIAS "index=2,media=cdrom"
2131#endif
2132#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00002133#define PFLASH_ALIAS "if=pflash"
2134#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00002135#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002136
aliguori7d5aca92009-02-11 15:19:58 +00002137static int drive_opt_get_free_idx(void)
2138{
2139 int index;
2140
2141 for (index = 0; index < MAX_DRIVES; index++)
2142 if (!drives_opt[index].used) {
2143 drives_opt[index].used = 1;
2144 return index;
2145 }
2146
2147 return -1;
2148}
2149
2150static int drive_get_free_idx(void)
2151{
2152 int index;
2153
2154 for (index = 0; index < MAX_DRIVES; index++)
2155 if (!drives_table[index].used) {
2156 drives_table[index].used = 1;
2157 return index;
2158 }
2159
2160 return -1;
2161}
2162
aliguori4d73cd32009-02-11 15:20:46 +00002163int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002164{
2165 va_list ap;
aliguori7d5aca92009-02-11 15:19:58 +00002166 int index = drive_opt_get_free_idx();
thse4bcb142007-12-02 04:51:10 +00002167
aliguori7d5aca92009-02-11 15:19:58 +00002168 if (nb_drives_opt >= MAX_DRIVES || index == -1) {
thse4bcb142007-12-02 04:51:10 +00002169 fprintf(stderr, "qemu: too many drives\n");
aliguori4d73cd32009-02-11 15:20:46 +00002170 return -1;
thse4bcb142007-12-02 04:51:10 +00002171 }
2172
aliguori7d5aca92009-02-11 15:19:58 +00002173 drives_opt[index].file = file;
thse4bcb142007-12-02 04:51:10 +00002174 va_start(ap, fmt);
aliguori7d5aca92009-02-11 15:19:58 +00002175 vsnprintf(drives_opt[index].opt,
balrog609497a2008-01-14 02:56:53 +00002176 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002177 va_end(ap);
2178
aliguori7d5aca92009-02-11 15:19:58 +00002179 nb_drives_opt++;
2180 return index;
thse4bcb142007-12-02 04:51:10 +00002181}
2182
aliguorib01b1112009-02-11 15:20:20 +00002183void drive_remove(int index)
2184{
2185 drives_opt[index].used = 0;
2186 nb_drives_opt--;
2187}
2188
thsf60d39b2007-12-17 03:55:57 +00002189int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002190{
2191 int index;
2192
2193 /* seek interface, bus and unit */
2194
aliguori7d5aca92009-02-11 15:19:58 +00002195 for (index = 0; index < MAX_DRIVES; index++)
thsf60d39b2007-12-17 03:55:57 +00002196 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002197 drives_table[index].bus == bus &&
aliguori7d5aca92009-02-11 15:19:58 +00002198 drives_table[index].unit == unit &&
2199 drives_table[index].used)
thse4bcb142007-12-02 04:51:10 +00002200 return index;
2201
2202 return -1;
2203}
2204
thsf60d39b2007-12-17 03:55:57 +00002205int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002206{
2207 int max_bus;
2208 int index;
2209
2210 max_bus = -1;
2211 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002212 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002213 drives_table[index].bus > max_bus)
2214 max_bus = drives_table[index].bus;
2215 }
2216 return max_bus;
2217}
2218
aliguorifa879c62009-01-07 17:32:33 +00002219const char *drive_get_serial(BlockDriverState *bdrv)
2220{
2221 int index;
2222
2223 for (index = 0; index < nb_drives; index++)
2224 if (drives_table[index].bdrv == bdrv)
2225 return drives_table[index].serial;
2226
2227 return "\0";
2228}
2229
aliguori428c5702009-01-21 18:59:04 +00002230BlockInterfaceErrorAction drive_get_onerror(BlockDriverState *bdrv)
2231{
2232 int index;
2233
2234 for (index = 0; index < nb_drives; index++)
2235 if (drives_table[index].bdrv == bdrv)
2236 return drives_table[index].onerror;
2237
2238 return BLOCK_ERR_REPORT;
2239}
2240
aurel32a1620fa2008-04-29 05:58:01 +00002241static void bdrv_format_print(void *opaque, const char *name)
2242{
2243 fprintf(stderr, " %s", name);
2244}
2245
aliguorib01b1112009-02-11 15:20:20 +00002246void drive_uninit(BlockDriverState *bdrv)
2247{
2248 int i;
2249
2250 for (i = 0; i < MAX_DRIVES; i++)
2251 if (drives_table[i].bdrv == bdrv) {
2252 drives_table[i].bdrv = NULL;
2253 drives_table[i].used = 0;
2254 drive_remove(drives_table[i].drive_opt_idx);
2255 nb_drives--;
2256 break;
2257 }
2258}
2259
aliguori4d73cd32009-02-11 15:20:46 +00002260int drive_init(struct drive_opt *arg, int snapshot, void *opaque)
thse4bcb142007-12-02 04:51:10 +00002261{
2262 char buf[128];
2263 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002264 char devname[128];
aliguorifa879c62009-01-07 17:32:33 +00002265 char serial[21];
balrogc8522bd2007-12-06 22:11:20 +00002266 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002267 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002268 enum { MEDIA_DISK, MEDIA_CDROM } media;
2269 int bus_id, unit_id;
2270 int cyls, heads, secs, translation;
2271 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002272 BlockDriver *drv = NULL;
aliguori4d73cd32009-02-11 15:20:46 +00002273 QEMUMachine *machine = opaque;
thse4bcb142007-12-02 04:51:10 +00002274 int max_devs;
2275 int index;
balrog33f00272007-12-24 14:33:24 +00002276 int cache;
aliguori428c5702009-01-21 18:59:04 +00002277 int bdrv_flags, onerror;
aliguori7d5aca92009-02-11 15:19:58 +00002278 int drives_table_idx;
balrog609497a2008-01-14 02:56:53 +00002279 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002280 static const char * const params[] = { "bus", "unit", "if", "index",
2281 "cyls", "heads", "secs", "trans",
2282 "media", "snapshot", "file",
aliguori428c5702009-01-21 18:59:04 +00002283 "cache", "format", "serial", "werror",
2284 NULL };
thse4bcb142007-12-02 04:51:10 +00002285
2286 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002287 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002288 buf, str);
2289 return -1;
2290 }
2291
2292 file[0] = 0;
2293 cyls = heads = secs = 0;
2294 bus_id = 0;
2295 unit_id = -1;
2296 translation = BIOS_ATA_TRANSLATION_AUTO;
2297 index = -1;
aliguori4dc822d2008-12-04 21:39:21 +00002298 cache = 3;
thse4bcb142007-12-02 04:51:10 +00002299
blueswir1c9b1ae22008-09-28 18:55:17 +00002300 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002301 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002302 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002303 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002304 } else {
thsf60d39b2007-12-17 03:55:57 +00002305 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002306 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002307 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002308 }
2309 media = MEDIA_DISK;
2310
2311 /* extract parameters */
2312
2313 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2314 bus_id = strtol(buf, NULL, 0);
2315 if (bus_id < 0) {
2316 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2317 return -1;
2318 }
2319 }
2320
2321 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2322 unit_id = strtol(buf, NULL, 0);
2323 if (unit_id < 0) {
2324 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2325 return -1;
2326 }
2327 }
2328
2329 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002330 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002331 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002332 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002333 max_devs = MAX_IDE_DEVS;
2334 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002335 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002336 max_devs = MAX_SCSI_DEVS;
2337 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002338 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002339 max_devs = 0;
2340 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002341 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002342 max_devs = 0;
2343 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002344 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002345 max_devs = 0;
2346 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002347 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002348 max_devs = 0;
aliguori6e02c382008-12-04 19:52:44 +00002349 } else if (!strcmp(buf, "virtio")) {
2350 type = IF_VIRTIO;
2351 max_devs = 0;
2352 } else {
thse4bcb142007-12-02 04:51:10 +00002353 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2354 return -1;
2355 }
2356 }
2357
2358 if (get_param_value(buf, sizeof(buf), "index", str)) {
2359 index = strtol(buf, NULL, 0);
2360 if (index < 0) {
2361 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2362 return -1;
2363 }
2364 }
2365
2366 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2367 cyls = strtol(buf, NULL, 0);
2368 }
2369
2370 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2371 heads = strtol(buf, NULL, 0);
2372 }
2373
2374 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2375 secs = strtol(buf, NULL, 0);
2376 }
2377
2378 if (cyls || heads || secs) {
2379 if (cyls < 1 || cyls > 16383) {
2380 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2381 return -1;
2382 }
2383 if (heads < 1 || heads > 16) {
2384 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2385 return -1;
2386 }
2387 if (secs < 1 || secs > 63) {
2388 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2389 return -1;
2390 }
2391 }
2392
2393 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2394 if (!cyls) {
2395 fprintf(stderr,
2396 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2397 str);
2398 return -1;
2399 }
2400 if (!strcmp(buf, "none"))
2401 translation = BIOS_ATA_TRANSLATION_NONE;
2402 else if (!strcmp(buf, "lba"))
2403 translation = BIOS_ATA_TRANSLATION_LBA;
2404 else if (!strcmp(buf, "auto"))
2405 translation = BIOS_ATA_TRANSLATION_AUTO;
2406 else {
2407 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2408 return -1;
2409 }
2410 }
2411
2412 if (get_param_value(buf, sizeof(buf), "media", str)) {
2413 if (!strcmp(buf, "disk")) {
2414 media = MEDIA_DISK;
2415 } else if (!strcmp(buf, "cdrom")) {
2416 if (cyls || secs || heads) {
2417 fprintf(stderr,
2418 "qemu: '%s' invalid physical CHS format\n", str);
2419 return -1;
2420 }
2421 media = MEDIA_CDROM;
2422 } else {
2423 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2424 return -1;
2425 }
2426 }
2427
2428 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2429 if (!strcmp(buf, "on"))
2430 snapshot = 1;
2431 else if (!strcmp(buf, "off"))
2432 snapshot = 0;
2433 else {
2434 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2435 return -1;
2436 }
2437 }
2438
balrog33f00272007-12-24 14:33:24 +00002439 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002440 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002441 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002442 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002443 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002444 else if (!strcmp(buf, "writeback"))
2445 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002446 else {
2447 fprintf(stderr, "qemu: invalid cache option\n");
2448 return -1;
2449 }
2450 }
2451
aurel321e72d3b2008-04-28 20:26:45 +00002452 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002453 if (strcmp(buf, "?") == 0) {
2454 fprintf(stderr, "qemu: Supported formats:");
2455 bdrv_iterate_format(bdrv_format_print, NULL);
2456 fprintf(stderr, "\n");
2457 return -1;
2458 }
aurel321e72d3b2008-04-28 20:26:45 +00002459 drv = bdrv_find_format(buf);
2460 if (!drv) {
2461 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2462 return -1;
2463 }
2464 }
2465
balrog609497a2008-01-14 02:56:53 +00002466 if (arg->file == NULL)
2467 get_param_value(file, sizeof(file), "file", str);
2468 else
2469 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002470
aliguorifa879c62009-01-07 17:32:33 +00002471 if (!get_param_value(serial, sizeof(serial), "serial", str))
2472 memset(serial, 0, sizeof(serial));
2473
aliguori428c5702009-01-21 18:59:04 +00002474 onerror = BLOCK_ERR_REPORT;
2475 if (get_param_value(buf, sizeof(serial), "werror", str)) {
aliguori869a5c62009-01-22 19:52:25 +00002476 if (type != IF_IDE && type != IF_SCSI && type != IF_VIRTIO) {
aliguoriea8a5d72009-01-22 19:52:21 +00002477 fprintf(stderr, "werror is no supported by this format\n");
aliguori428c5702009-01-21 18:59:04 +00002478 return -1;
2479 }
2480 if (!strcmp(buf, "ignore"))
2481 onerror = BLOCK_ERR_IGNORE;
2482 else if (!strcmp(buf, "enospc"))
2483 onerror = BLOCK_ERR_STOP_ENOSPC;
2484 else if (!strcmp(buf, "stop"))
2485 onerror = BLOCK_ERR_STOP_ANY;
2486 else if (!strcmp(buf, "report"))
2487 onerror = BLOCK_ERR_REPORT;
2488 else {
2489 fprintf(stderr, "qemu: '%s' invalid write error action\n", buf);
2490 return -1;
2491 }
2492 }
2493
thse4bcb142007-12-02 04:51:10 +00002494 /* compute bus and unit according index */
2495
2496 if (index != -1) {
2497 if (bus_id != 0 || unit_id != -1) {
2498 fprintf(stderr,
2499 "qemu: '%s' index cannot be used with bus and unit\n", str);
2500 return -1;
2501 }
2502 if (max_devs == 0)
2503 {
2504 unit_id = index;
2505 bus_id = 0;
2506 } else {
2507 unit_id = index % max_devs;
2508 bus_id = index / max_devs;
2509 }
2510 }
2511
2512 /* if user doesn't specify a unit_id,
2513 * try to find the first free
2514 */
2515
2516 if (unit_id == -1) {
2517 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002518 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002519 unit_id++;
2520 if (max_devs && unit_id >= max_devs) {
2521 unit_id -= max_devs;
2522 bus_id++;
2523 }
2524 }
2525 }
2526
2527 /* check unit id */
2528
2529 if (max_devs && unit_id >= max_devs) {
2530 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2531 str, unit_id, max_devs - 1);
2532 return -1;
2533 }
2534
2535 /*
2536 * ignore multiple definitions
2537 */
2538
thsf60d39b2007-12-17 03:55:57 +00002539 if (drive_get_index(type, bus_id, unit_id) != -1)
aliguori4d73cd32009-02-11 15:20:46 +00002540 return -2;
thse4bcb142007-12-02 04:51:10 +00002541
2542 /* init */
2543
thsf60d39b2007-12-17 03:55:57 +00002544 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002545 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002546 if (max_devs)
2547 snprintf(buf, sizeof(buf), "%s%i%s%i",
2548 devname, bus_id, mediastr, unit_id);
2549 else
2550 snprintf(buf, sizeof(buf), "%s%s%i",
2551 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002552 bdrv = bdrv_new(buf);
aliguori7d5aca92009-02-11 15:19:58 +00002553 drives_table_idx = drive_get_free_idx();
2554 drives_table[drives_table_idx].bdrv = bdrv;
2555 drives_table[drives_table_idx].type = type;
2556 drives_table[drives_table_idx].bus = bus_id;
2557 drives_table[drives_table_idx].unit = unit_id;
2558 drives_table[drives_table_idx].onerror = onerror;
aliguorib01b1112009-02-11 15:20:20 +00002559 drives_table[drives_table_idx].drive_opt_idx = arg - drives_opt;
aliguorifa879c62009-01-07 17:32:33 +00002560 strncpy(drives_table[nb_drives].serial, serial, sizeof(serial));
thse4bcb142007-12-02 04:51:10 +00002561 nb_drives++;
2562
thsf60d39b2007-12-17 03:55:57 +00002563 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002564 case IF_IDE:
2565 case IF_SCSI:
2566 switch(media) {
2567 case MEDIA_DISK:
2568 if (cyls != 0) {
2569 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2570 bdrv_set_translation_hint(bdrv, translation);
2571 }
2572 break;
2573 case MEDIA_CDROM:
2574 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2575 break;
2576 }
2577 break;
2578 case IF_SD:
2579 /* FIXME: This isn't really a floppy, but it's a reasonable
2580 approximation. */
2581 case IF_FLOPPY:
2582 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2583 break;
2584 case IF_PFLASH:
2585 case IF_MTD:
aliguori6e02c382008-12-04 19:52:44 +00002586 case IF_VIRTIO:
thse4bcb142007-12-02 04:51:10 +00002587 break;
2588 }
2589 if (!file[0])
aliguori4d73cd32009-02-11 15:20:46 +00002590 return -2;
balrog33f00272007-12-24 14:33:24 +00002591 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002592 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002593 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002594 cache = 2; /* always use write-back with snapshot */
2595 }
2596 if (cache == 0) /* no caching */
2597 bdrv_flags |= BDRV_O_NOCACHE;
2598 else if (cache == 2) /* write-back */
2599 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori4dc822d2008-12-04 21:39:21 +00002600 else if (cache == 3) /* not specified */
2601 bdrv_flags |= BDRV_O_CACHE_DEF;
aliguori83ab7952008-08-19 14:44:22 +00002602 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
thse4bcb142007-12-02 04:51:10 +00002603 fprintf(stderr, "qemu: could not open disk image %s\n",
2604 file);
2605 return -1;
2606 }
aliguori4d73cd32009-02-11 15:20:46 +00002607 return drives_table_idx;
thse4bcb142007-12-02 04:51:10 +00002608}
2609
bellard330d0412003-07-26 18:11:40 +00002610/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002611/* USB devices */
2612
pbrook0d92ed32006-05-21 16:30:15 +00002613static USBPort *used_usb_ports;
2614static USBPort *free_usb_ports;
2615
2616/* ??? Maybe change this to register a hub to keep track of the topology. */
2617void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2618 usb_attachfn attach)
2619{
2620 port->opaque = opaque;
2621 port->index = index;
2622 port->attach = attach;
2623 port->next = free_usb_ports;
2624 free_usb_ports = port;
2625}
2626
aliguori4b096fc2008-08-21 19:28:55 +00002627int usb_device_add_dev(USBDevice *dev)
2628{
2629 USBPort *port;
2630
2631 /* Find a USB port to add the device to. */
2632 port = free_usb_ports;
2633 if (!port->next) {
2634 USBDevice *hub;
2635
2636 /* Create a new hub and chain it on. */
2637 free_usb_ports = NULL;
2638 port->next = used_usb_ports;
2639 used_usb_ports = port;
2640
2641 hub = usb_hub_init(VM_USB_HUB_SIZE);
2642 usb_attach(port, hub);
2643 port = free_usb_ports;
2644 }
2645
2646 free_usb_ports = port->next;
2647 port->next = used_usb_ports;
2648 used_usb_ports = port;
2649 usb_attach(port, dev);
2650 return 0;
2651}
2652
bellarda594cfb2005-11-06 16:13:29 +00002653static int usb_device_add(const char *devname)
2654{
2655 const char *p;
2656 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002657
pbrook0d92ed32006-05-21 16:30:15 +00002658 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002659 return -1;
2660
2661 if (strstart(devname, "host:", &p)) {
2662 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002663 } else if (!strcmp(devname, "mouse")) {
2664 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002665 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002666 dev = usb_tablet_init();
2667 } else if (!strcmp(devname, "keyboard")) {
2668 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002669 } else if (strstart(devname, "disk:", &p)) {
2670 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00002671 } else if (!strcmp(devname, "wacom-tablet")) {
2672 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002673 } else if (strstart(devname, "serial:", &p)) {
2674 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002675#ifdef CONFIG_BRLAPI
2676 } else if (!strcmp(devname, "braille")) {
2677 dev = usb_baum_init();
2678#endif
balrog6c9f8862008-07-17 20:47:13 +00002679 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002680 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002681
balrog9ad97e62008-07-29 13:16:31 +00002682 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002683 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002684 nd_table[nic].model = "usb";
2685 dev = usb_net_init(&nd_table[nic]);
balrogdc72ac12008-11-09 00:04:26 +00002686 } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2687 dev = usb_bt_init(devname[2] ? hci_init(p) :
2688 bt_new_hci(qemu_find_bt_vlan(0)));
bellarda594cfb2005-11-06 16:13:29 +00002689 } else {
2690 return -1;
2691 }
pbrook0d92ed32006-05-21 16:30:15 +00002692 if (!dev)
2693 return -1;
2694
aliguori4b096fc2008-08-21 19:28:55 +00002695 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002696}
2697
aliguori1f3870a2008-08-21 19:27:48 +00002698int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002699{
pbrook0d92ed32006-05-21 16:30:15 +00002700 USBPort *port;
2701 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002702 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002703
pbrook0d92ed32006-05-21 16:30:15 +00002704 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002705 return -1;
2706
bellarda594cfb2005-11-06 16:13:29 +00002707 if (bus_num != 0)
2708 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002709
2710 lastp = &used_usb_ports;
2711 port = used_usb_ports;
2712 while (port && port->dev->addr != addr) {
2713 lastp = &port->next;
2714 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002715 }
pbrook0d92ed32006-05-21 16:30:15 +00002716
2717 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002718 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002719
bellard059809e2006-07-19 18:06:15 +00002720 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002721 *lastp = port->next;
2722 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002723 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002724 port->next = free_usb_ports;
2725 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002726 return 0;
2727}
2728
aliguori1f3870a2008-08-21 19:27:48 +00002729static int usb_device_del(const char *devname)
2730{
2731 int bus_num, addr;
2732 const char *p;
2733
aliguori5d0c5752008-09-14 01:07:41 +00002734 if (strstart(devname, "host:", &p))
2735 return usb_host_device_close(p);
2736
aliguori1f3870a2008-08-21 19:27:48 +00002737 if (!used_usb_ports)
2738 return -1;
2739
2740 p = strchr(devname, '.');
2741 if (!p)
2742 return -1;
2743 bus_num = strtoul(devname, NULL, 0);
2744 addr = strtoul(p + 1, NULL, 0);
2745
2746 return usb_device_del_addr(bus_num, addr);
2747}
2748
bellarda594cfb2005-11-06 16:13:29 +00002749void do_usb_add(const char *devname)
2750{
aliguori4b096fc2008-08-21 19:28:55 +00002751 usb_device_add(devname);
bellarda594cfb2005-11-06 16:13:29 +00002752}
2753
2754void do_usb_del(const char *devname)
2755{
aliguori4b096fc2008-08-21 19:28:55 +00002756 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002757}
2758
2759void usb_info(void)
2760{
2761 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002762 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002763 const char *speed_str;
2764
pbrook0d92ed32006-05-21 16:30:15 +00002765 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00002766 term_printf("USB support not enabled\n");
2767 return;
2768 }
2769
pbrook0d92ed32006-05-21 16:30:15 +00002770 for (port = used_usb_ports; port; port = port->next) {
2771 dev = port->dev;
2772 if (!dev)
2773 continue;
2774 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002775 case USB_SPEED_LOW:
2776 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002777 break;
ths5fafdf22007-09-16 21:08:06 +00002778 case USB_SPEED_FULL:
2779 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002780 break;
ths5fafdf22007-09-16 21:08:06 +00002781 case USB_SPEED_HIGH:
2782 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002783 break;
2784 default:
ths5fafdf22007-09-16 21:08:06 +00002785 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002786 break;
bellarda594cfb2005-11-06 16:13:29 +00002787 }
ths5fafdf22007-09-16 21:08:06 +00002788 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00002789 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002790 }
2791}
2792
bellardf7cce892004-12-08 22:21:25 +00002793/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002794/* PCMCIA/Cardbus */
2795
2796static struct pcmcia_socket_entry_s {
2797 struct pcmcia_socket_s *socket;
2798 struct pcmcia_socket_entry_s *next;
2799} *pcmcia_sockets = 0;
2800
2801void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2802{
2803 struct pcmcia_socket_entry_s *entry;
2804
2805 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2806 entry->socket = socket;
2807 entry->next = pcmcia_sockets;
2808 pcmcia_sockets = entry;
2809}
2810
2811void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2812{
2813 struct pcmcia_socket_entry_s *entry, **ptr;
2814
2815 ptr = &pcmcia_sockets;
2816 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2817 if (entry->socket == socket) {
2818 *ptr = entry->next;
2819 qemu_free(entry);
2820 }
2821}
2822
2823void pcmcia_info(void)
2824{
2825 struct pcmcia_socket_entry_s *iter;
2826 if (!pcmcia_sockets)
2827 term_printf("No PCMCIA sockets\n");
2828
2829 for (iter = pcmcia_sockets; iter; iter = iter->next)
2830 term_printf("%s: %s\n", iter->socket->slot_string,
2831 iter->socket->attached ? iter->socket->card_string :
2832 "Empty");
2833}
2834
2835/***********************************************************/
aliguori3023f332009-01-16 19:04:14 +00002836/* register display */
2837
2838void register_displaystate(DisplayState *ds)
2839{
2840 DisplayState **s;
2841 s = &display_state;
2842 while (*s != NULL)
2843 s = &(*s)->next;
2844 ds->next = NULL;
2845 *s = ds;
2846}
2847
2848DisplayState *get_displaystate(void)
2849{
2850 return display_state;
2851}
2852
ths2ff89792007-06-21 23:34:19 +00002853/* dumb display */
2854
aliguori8f391ab2009-01-19 16:34:10 +00002855static void dumb_display_init(void)
ths2ff89792007-06-21 23:34:19 +00002856{
aliguori8f391ab2009-01-19 16:34:10 +00002857 DisplayState *ds = qemu_mallocz(sizeof(DisplayState));
aliguori7da03b12009-01-21 18:58:51 +00002858 ds->surface = qemu_create_displaysurface(640, 480, 32, 640 * 4);
aliguori8f391ab2009-01-19 16:34:10 +00002859 register_displaystate(ds);
ths2ff89792007-06-21 23:34:19 +00002860}
2861
2862/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002863/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002864
bellardc4b1fcc2004-03-14 21:44:30 +00002865#define MAX_IO_HANDLERS 64
2866
2867typedef struct IOHandlerRecord {
2868 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002869 IOCanRWHandler *fd_read_poll;
2870 IOHandler *fd_read;
2871 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002872 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002873 void *opaque;
2874 /* temporary data */
2875 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002876 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002877} IOHandlerRecord;
2878
bellard8a7ddc32004-03-31 19:00:16 +00002879static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002880
bellard7c9d8e02005-11-15 22:16:05 +00002881/* XXX: fd_read_poll should be suppressed, but an API change is
2882 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002883int qemu_set_fd_handler2(int fd,
2884 IOCanRWHandler *fd_read_poll,
2885 IOHandler *fd_read,
2886 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002887 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002888{
bellard8a7ddc32004-03-31 19:00:16 +00002889 IOHandlerRecord **pioh, *ioh;
2890
bellard7c9d8e02005-11-15 22:16:05 +00002891 if (!fd_read && !fd_write) {
2892 pioh = &first_io_handler;
2893 for(;;) {
2894 ioh = *pioh;
2895 if (ioh == NULL)
2896 break;
2897 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002898 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002899 break;
2900 }
2901 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002902 }
bellard7c9d8e02005-11-15 22:16:05 +00002903 } else {
2904 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2905 if (ioh->fd == fd)
2906 goto found;
2907 }
2908 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
bellard7c9d8e02005-11-15 22:16:05 +00002909 ioh->next = first_io_handler;
2910 first_io_handler = ioh;
2911 found:
2912 ioh->fd = fd;
2913 ioh->fd_read_poll = fd_read_poll;
2914 ioh->fd_read = fd_read;
2915 ioh->fd_write = fd_write;
2916 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002917 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002918 }
bellard7c9d8e02005-11-15 22:16:05 +00002919 return 0;
2920}
2921
ths5fafdf22007-09-16 21:08:06 +00002922int qemu_set_fd_handler(int fd,
2923 IOHandler *fd_read,
2924 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002925 void *opaque)
2926{
2927 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002928}
2929
aliguori56f3a5d2008-10-31 18:07:17 +00002930#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002931/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002932/* Polling handling */
2933
2934typedef struct PollingEntry {
2935 PollingFunc *func;
2936 void *opaque;
2937 struct PollingEntry *next;
2938} PollingEntry;
2939
2940static PollingEntry *first_polling_entry;
2941
2942int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2943{
2944 PollingEntry **ppe, *pe;
2945 pe = qemu_mallocz(sizeof(PollingEntry));
bellardf3311102006-04-12 20:21:17 +00002946 pe->func = func;
2947 pe->opaque = opaque;
2948 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2949 *ppe = pe;
2950 return 0;
2951}
2952
2953void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2954{
2955 PollingEntry **ppe, *pe;
2956 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2957 pe = *ppe;
2958 if (pe->func == func && pe->opaque == opaque) {
2959 *ppe = pe->next;
2960 qemu_free(pe);
2961 break;
2962 }
2963 }
2964}
2965
bellarda18e5242006-06-25 17:18:27 +00002966/***********************************************************/
2967/* Wait objects support */
2968typedef struct WaitObjects {
2969 int num;
2970 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2971 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2972 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2973} WaitObjects;
2974
2975static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002976
bellarda18e5242006-06-25 17:18:27 +00002977int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2978{
2979 WaitObjects *w = &wait_objects;
2980
2981 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2982 return -1;
2983 w->events[w->num] = handle;
2984 w->func[w->num] = func;
2985 w->opaque[w->num] = opaque;
2986 w->num++;
2987 return 0;
2988}
2989
2990void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2991{
2992 int i, found;
2993 WaitObjects *w = &wait_objects;
2994
2995 found = 0;
2996 for (i = 0; i < w->num; i++) {
2997 if (w->events[i] == handle)
2998 found = 1;
2999 if (found) {
3000 w->events[i] = w->events[i + 1];
3001 w->func[i] = w->func[i + 1];
3002 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00003003 }
bellarda18e5242006-06-25 17:18:27 +00003004 }
3005 if (found)
3006 w->num--;
3007}
3008#endif
3009
bellard8a7ddc32004-03-31 19:00:16 +00003010/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003011/* ram save/restore */
3012
bellard8a7ddc32004-03-31 19:00:16 +00003013static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3014{
3015 int v;
3016
3017 v = qemu_get_byte(f);
3018 switch(v) {
3019 case 0:
3020 if (qemu_get_buffer(f, buf, len) != len)
3021 return -EIO;
3022 break;
3023 case 1:
3024 v = qemu_get_byte(f);
3025 memset(buf, v, len);
3026 break;
3027 default:
3028 return -EINVAL;
3029 }
aliguori871d2f02008-10-13 03:07:56 +00003030
3031 if (qemu_file_has_error(f))
3032 return -EIO;
3033
bellard8a7ddc32004-03-31 19:00:16 +00003034 return 0;
3035}
3036
bellardc88676f2006-08-06 13:36:11 +00003037static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003038{
aurel3200f82b82008-04-27 21:12:55 +00003039 int ret;
3040 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003041
bellard8a7ddc32004-03-31 19:00:16 +00003042 if (qemu_get_be32(f) != phys_ram_size)
3043 return -EINVAL;
3044 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3045 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3046 if (ret)
3047 return ret;
3048 }
3049 return 0;
3050}
3051
bellardc88676f2006-08-06 13:36:11 +00003052#define BDRV_HASH_BLOCK_SIZE 1024
3053#define IOBUF_SIZE 4096
3054#define RAM_CBLOCK_MAGIC 0xfabe
3055
bellardc88676f2006-08-06 13:36:11 +00003056typedef struct RamDecompressState {
3057 z_stream zstream;
3058 QEMUFile *f;
3059 uint8_t buf[IOBUF_SIZE];
3060} RamDecompressState;
3061
3062static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3063{
3064 int ret;
3065 memset(s, 0, sizeof(*s));
3066 s->f = f;
3067 ret = inflateInit(&s->zstream);
3068 if (ret != Z_OK)
3069 return -1;
3070 return 0;
3071}
3072
3073static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3074{
3075 int ret, clen;
3076
3077 s->zstream.avail_out = len;
3078 s->zstream.next_out = buf;
3079 while (s->zstream.avail_out > 0) {
3080 if (s->zstream.avail_in == 0) {
3081 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3082 return -1;
3083 clen = qemu_get_be16(s->f);
3084 if (clen > IOBUF_SIZE)
3085 return -1;
3086 qemu_get_buffer(s->f, s->buf, clen);
3087 s->zstream.avail_in = clen;
3088 s->zstream.next_in = s->buf;
3089 }
3090 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3091 if (ret != Z_OK && ret != Z_STREAM_END) {
3092 return -1;
3093 }
3094 }
3095 return 0;
3096}
3097
3098static void ram_decompress_close(RamDecompressState *s)
3099{
3100 inflateEnd(&s->zstream);
3101}
3102
aliguori475e4272008-10-06 20:21:51 +00003103#define RAM_SAVE_FLAG_FULL 0x01
3104#define RAM_SAVE_FLAG_COMPRESS 0x02
3105#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3106#define RAM_SAVE_FLAG_PAGE 0x08
3107#define RAM_SAVE_FLAG_EOS 0x10
3108
3109static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003110{
aliguori475e4272008-10-06 20:21:51 +00003111 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3112 uint32_t *array = (uint32_t *)page;
3113 int i;
ths3b46e622007-09-17 08:09:54 +00003114
aliguori475e4272008-10-06 20:21:51 +00003115 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3116 if (array[i] != val)
3117 return 0;
bellardc88676f2006-08-06 13:36:11 +00003118 }
aliguori475e4272008-10-06 20:21:51 +00003119
3120 return 1;
bellardc88676f2006-08-06 13:36:11 +00003121}
3122
aliguori475e4272008-10-06 20:21:51 +00003123static int ram_save_block(QEMUFile *f)
3124{
3125 static ram_addr_t current_addr = 0;
3126 ram_addr_t saved_addr = current_addr;
3127 ram_addr_t addr = 0;
3128 int found = 0;
3129
3130 while (addr < phys_ram_size) {
3131 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3132 uint8_t ch;
3133
3134 cpu_physical_memory_reset_dirty(current_addr,
3135 current_addr + TARGET_PAGE_SIZE,
3136 MIGRATION_DIRTY_FLAG);
3137
3138 ch = *(phys_ram_base + current_addr);
3139
3140 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3141 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3142 qemu_put_byte(f, ch);
3143 } else {
3144 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3145 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3146 }
3147
3148 found = 1;
3149 break;
3150 }
3151 addr += TARGET_PAGE_SIZE;
3152 current_addr = (saved_addr + addr) % phys_ram_size;
3153 }
3154
3155 return found;
3156}
3157
3158static ram_addr_t ram_save_threshold = 10;
3159
3160static ram_addr_t ram_save_remaining(void)
3161{
3162 ram_addr_t addr;
3163 ram_addr_t count = 0;
3164
3165 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3166 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3167 count++;
3168 }
3169
3170 return count;
3171}
3172
3173static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3174{
3175 ram_addr_t addr;
3176
3177 if (stage == 1) {
3178 /* Make sure all dirty bits are set */
3179 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3180 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3181 cpu_physical_memory_set_dirty(addr);
3182 }
3183
3184 /* Enable dirty memory tracking */
3185 cpu_physical_memory_set_dirty_tracking(1);
3186
3187 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3188 }
3189
3190 while (!qemu_file_rate_limit(f)) {
3191 int ret;
3192
3193 ret = ram_save_block(f);
3194 if (ret == 0) /* no more blocks */
3195 break;
3196 }
3197
3198 /* try transferring iterative blocks of memory */
3199
3200 if (stage == 3) {
3201 cpu_physical_memory_set_dirty_tracking(0);
3202
3203 /* flush all remaining blocks regardless of rate limiting */
3204 while (ram_save_block(f) != 0);
3205 }
3206
3207 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3208
3209 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3210}
3211
3212static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00003213{
3214 RamDecompressState s1, *s = &s1;
3215 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00003216 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00003217
bellardc88676f2006-08-06 13:36:11 +00003218 if (ram_decompress_open(s, f) < 0)
3219 return -EINVAL;
3220 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3221 if (ram_decompress_buf(s, buf, 1) < 0) {
3222 fprintf(stderr, "Error while reading ram block header\n");
3223 goto error;
3224 }
3225 if (buf[0] == 0) {
3226 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00003227 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00003228 goto error;
3229 }
aliguori475e4272008-10-06 20:21:51 +00003230 } else {
bellardc88676f2006-08-06 13:36:11 +00003231 error:
3232 printf("Error block header\n");
3233 return -EINVAL;
3234 }
3235 }
3236 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00003237
3238 return 0;
3239}
3240
3241static int ram_load(QEMUFile *f, void *opaque, int version_id)
3242{
3243 ram_addr_t addr;
3244 int flags;
3245
3246 if (version_id == 1)
3247 return ram_load_v1(f, opaque);
3248
3249 if (version_id == 2) {
3250 if (qemu_get_be32(f) != phys_ram_size)
3251 return -EINVAL;
3252 return ram_load_dead(f, opaque);
3253 }
3254
3255 if (version_id != 3)
3256 return -EINVAL;
3257
3258 do {
3259 addr = qemu_get_be64(f);
3260
3261 flags = addr & ~TARGET_PAGE_MASK;
3262 addr &= TARGET_PAGE_MASK;
3263
3264 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
3265 if (addr != phys_ram_size)
3266 return -EINVAL;
3267 }
3268
3269 if (flags & RAM_SAVE_FLAG_FULL) {
3270 if (ram_load_dead(f, opaque) < 0)
3271 return -EINVAL;
3272 }
3273
3274 if (flags & RAM_SAVE_FLAG_COMPRESS) {
3275 uint8_t ch = qemu_get_byte(f);
3276 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
3277 } else if (flags & RAM_SAVE_FLAG_PAGE)
3278 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
3279 } while (!(flags & RAM_SAVE_FLAG_EOS));
3280
bellardc88676f2006-08-06 13:36:11 +00003281 return 0;
3282}
3283
aliguori9e472e12008-10-08 19:50:24 +00003284void qemu_service_io(void)
3285{
3286 CPUState *env = cpu_single_env;
3287 if (env) {
3288 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3289#ifdef USE_KQEMU
3290 if (env->kqemu_enabled) {
3291 kqemu_cpu_interrupt(env);
3292 }
3293#endif
3294 }
3295}
3296
bellard8a7ddc32004-03-31 19:00:16 +00003297/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00003298/* bottom halves (can be seen as timers which expire ASAP) */
3299
3300struct QEMUBH {
3301 QEMUBHFunc *cb;
3302 void *opaque;
3303 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00003304 int idle;
3305 int deleted;
bellard83f64092006-08-01 16:21:11 +00003306 QEMUBH *next;
3307};
3308
3309static QEMUBH *first_bh = NULL;
3310
3311QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
3312{
3313 QEMUBH *bh;
3314 bh = qemu_mallocz(sizeof(QEMUBH));
bellard83f64092006-08-01 16:21:11 +00003315 bh->cb = cb;
3316 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00003317 bh->next = first_bh;
3318 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00003319 return bh;
3320}
3321
bellard6eb57332006-08-06 09:51:25 +00003322int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00003323{
aliguori1b435b12008-10-31 17:24:21 +00003324 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00003325 int ret;
bellard83f64092006-08-01 16:21:11 +00003326
bellard6eb57332006-08-06 09:51:25 +00003327 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00003328 for (bh = first_bh; bh; bh = bh->next) {
3329 if (!bh->deleted && bh->scheduled) {
3330 bh->scheduled = 0;
3331 if (!bh->idle)
3332 ret = 1;
3333 bh->idle = 0;
3334 bh->cb(bh->opaque);
3335 }
bellard83f64092006-08-01 16:21:11 +00003336 }
aliguori1b435b12008-10-31 17:24:21 +00003337
3338 /* remove deleted bhs */
3339 bhp = &first_bh;
3340 while (*bhp) {
3341 bh = *bhp;
3342 if (bh->deleted) {
3343 *bhp = bh->next;
3344 qemu_free(bh);
3345 } else
3346 bhp = &bh->next;
3347 }
3348
bellard6eb57332006-08-06 09:51:25 +00003349 return ret;
bellard83f64092006-08-01 16:21:11 +00003350}
3351
aliguori1b435b12008-10-31 17:24:21 +00003352void qemu_bh_schedule_idle(QEMUBH *bh)
3353{
3354 if (bh->scheduled)
3355 return;
3356 bh->scheduled = 1;
3357 bh->idle = 1;
3358}
3359
bellard83f64092006-08-01 16:21:11 +00003360void qemu_bh_schedule(QEMUBH *bh)
3361{
3362 CPUState *env = cpu_single_env;
3363 if (bh->scheduled)
3364 return;
3365 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00003366 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00003367 /* stop the currently executing CPU to execute the BH ASAP */
3368 if (env) {
3369 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
3370 }
3371}
3372
3373void qemu_bh_cancel(QEMUBH *bh)
3374{
aliguori1b435b12008-10-31 17:24:21 +00003375 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00003376}
3377
3378void qemu_bh_delete(QEMUBH *bh)
3379{
aliguori1b435b12008-10-31 17:24:21 +00003380 bh->scheduled = 0;
3381 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00003382}
3383
aliguori56f3a5d2008-10-31 18:07:17 +00003384static void qemu_bh_update_timeout(int *timeout)
3385{
3386 QEMUBH *bh;
3387
3388 for (bh = first_bh; bh; bh = bh->next) {
3389 if (!bh->deleted && bh->scheduled) {
3390 if (bh->idle) {
3391 /* idle bottom halves will be polled at least
3392 * every 10ms */
3393 *timeout = MIN(10, *timeout);
3394 } else {
3395 /* non-idle bottom halves will be executed
3396 * immediately */
3397 *timeout = 0;
3398 break;
3399 }
3400 }
3401 }
3402}
3403
bellard83f64092006-08-01 16:21:11 +00003404/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00003405/* machine registration */
3406
blueswir1bdaf78e2008-10-04 07:24:27 +00003407static QEMUMachine *first_machine = NULL;
aliguori6f338c32009-02-11 15:21:54 +00003408QEMUMachine *current_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00003409
3410int qemu_register_machine(QEMUMachine *m)
3411{
3412 QEMUMachine **pm;
3413 pm = &first_machine;
3414 while (*pm != NULL)
3415 pm = &(*pm)->next;
3416 m->next = NULL;
3417 *pm = m;
3418 return 0;
3419}
3420
pbrook9596ebb2007-11-18 01:44:38 +00003421static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00003422{
3423 QEMUMachine *m;
3424
3425 for(m = first_machine; m != NULL; m = m->next) {
3426 if (!strcmp(m->name, name))
3427 return m;
3428 }
3429 return NULL;
3430}
3431
3432/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003433/* main execution loop */
3434
pbrook9596ebb2007-11-18 01:44:38 +00003435static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003436{
aliguori7d957bd2009-01-15 22:14:11 +00003437 uint64_t interval = GUI_REFRESH_INTERVAL;
ths740733b2007-06-08 01:57:56 +00003438 DisplayState *ds = opaque;
aliguori7d957bd2009-01-15 22:14:11 +00003439 DisplayChangeListener *dcl = ds->listeners;
3440
3441 dpy_refresh(ds);
3442
3443 while (dcl != NULL) {
3444 if (dcl->gui_timer_interval &&
3445 dcl->gui_timer_interval < interval)
3446 interval = dcl->gui_timer_interval;
3447 dcl = dcl->next;
3448 }
3449 qemu_mod_timer(ds->gui_timer, interval + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00003450}
3451
blueswir19043b622009-01-21 19:28:13 +00003452static void nographic_update(void *opaque)
3453{
3454 uint64_t interval = GUI_REFRESH_INTERVAL;
3455
3456 qemu_mod_timer(nographic_timer, interval + qemu_get_clock(rt_clock));
3457}
3458
bellard0bd48852005-11-11 00:00:47 +00003459struct vm_change_state_entry {
3460 VMChangeStateHandler *cb;
3461 void *opaque;
3462 LIST_ENTRY (vm_change_state_entry) entries;
3463};
3464
3465static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
3466
3467VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
3468 void *opaque)
3469{
3470 VMChangeStateEntry *e;
3471
3472 e = qemu_mallocz(sizeof (*e));
bellard0bd48852005-11-11 00:00:47 +00003473
3474 e->cb = cb;
3475 e->opaque = opaque;
3476 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
3477 return e;
3478}
3479
3480void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
3481{
3482 LIST_REMOVE (e, entries);
3483 qemu_free (e);
3484}
3485
aliguori9781e042009-01-22 17:15:29 +00003486static void vm_state_notify(int running, int reason)
bellard0bd48852005-11-11 00:00:47 +00003487{
3488 VMChangeStateEntry *e;
3489
3490 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
aliguori9781e042009-01-22 17:15:29 +00003491 e->cb(e->opaque, running, reason);
bellard0bd48852005-11-11 00:00:47 +00003492 }
3493}
3494
bellard8a7ddc32004-03-31 19:00:16 +00003495void vm_start(void)
3496{
3497 if (!vm_running) {
3498 cpu_enable_ticks();
3499 vm_running = 1;
aliguori9781e042009-01-22 17:15:29 +00003500 vm_state_notify(1, 0);
thsefe75412007-08-24 01:36:32 +00003501 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00003502 }
3503}
3504
ths5fafdf22007-09-16 21:08:06 +00003505void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00003506{
3507 if (vm_running) {
3508 cpu_disable_ticks();
3509 vm_running = 0;
aliguori9781e042009-01-22 17:15:29 +00003510 vm_state_notify(0, reason);
bellard8a7ddc32004-03-31 19:00:16 +00003511 }
3512}
3513
bellardbb0c6722004-06-20 12:37:32 +00003514/* reset/shutdown handler */
3515
3516typedef struct QEMUResetEntry {
3517 QEMUResetHandler *func;
3518 void *opaque;
3519 struct QEMUResetEntry *next;
3520} QEMUResetEntry;
3521
3522static QEMUResetEntry *first_reset_entry;
3523static int reset_requested;
3524static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00003525static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00003526
aurel32cf7a2fe2008-03-18 06:53:05 +00003527int qemu_shutdown_requested(void)
3528{
3529 int r = shutdown_requested;
3530 shutdown_requested = 0;
3531 return r;
3532}
3533
3534int qemu_reset_requested(void)
3535{
3536 int r = reset_requested;
3537 reset_requested = 0;
3538 return r;
3539}
3540
3541int qemu_powerdown_requested(void)
3542{
3543 int r = powerdown_requested;
3544 powerdown_requested = 0;
3545 return r;
3546}
3547
bellardbb0c6722004-06-20 12:37:32 +00003548void qemu_register_reset(QEMUResetHandler *func, void *opaque)
3549{
3550 QEMUResetEntry **pre, *re;
3551
3552 pre = &first_reset_entry;
3553 while (*pre != NULL)
3554 pre = &(*pre)->next;
3555 re = qemu_mallocz(sizeof(QEMUResetEntry));
3556 re->func = func;
3557 re->opaque = opaque;
3558 re->next = NULL;
3559 *pre = re;
3560}
3561
aurel32cf7a2fe2008-03-18 06:53:05 +00003562void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00003563{
3564 QEMUResetEntry *re;
3565
3566 /* reset all devices */
3567 for(re = first_reset_entry; re != NULL; re = re->next) {
3568 re->func(re->opaque);
3569 }
3570}
3571
3572void qemu_system_reset_request(void)
3573{
bellardd1beab82006-10-02 19:44:22 +00003574 if (no_reboot) {
3575 shutdown_requested = 1;
3576 } else {
3577 reset_requested = 1;
3578 }
bellard6a00d602005-11-21 23:25:50 +00003579 if (cpu_single_env)
3580 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003581}
3582
3583void qemu_system_shutdown_request(void)
3584{
3585 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003586 if (cpu_single_env)
3587 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003588}
3589
bellard34751872005-07-02 14:31:34 +00003590void qemu_system_powerdown_request(void)
3591{
3592 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00003593 if (cpu_single_env)
3594 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00003595}
3596
ths877cf882007-04-18 18:11:47 +00003597#ifdef _WIN32
blueswir169d64512008-12-07 19:30:18 +00003598static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003599{
3600 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00003601 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00003602
bellardf3311102006-04-12 20:21:17 +00003603
3604 /* XXX: need to suppress polling by better using win32 events */
3605 ret = 0;
3606 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
3607 ret |= pe->func(pe->opaque);
3608 }
thse6b1e552007-04-18 17:56:02 +00003609 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00003610 int err;
3611 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00003612
aliguori56f3a5d2008-10-31 18:07:17 +00003613 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00003614 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
3615 if (w->func[ret - WAIT_OBJECT_0])
3616 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00003617
ths5fafdf22007-09-16 21:08:06 +00003618 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00003619 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00003620
thse6b1e552007-04-18 17:56:02 +00003621 /* Check if event is signaled */
3622 ret2 = WaitForSingleObject(w->events[i], 0);
3623 if(ret2 == WAIT_OBJECT_0) {
3624 if (w->func[i])
3625 w->func[i](w->opaque[i]);
3626 } else if (ret2 == WAIT_TIMEOUT) {
3627 } else {
3628 err = GetLastError();
3629 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00003630 }
3631 }
bellarda18e5242006-06-25 17:18:27 +00003632 } else if (ret == WAIT_TIMEOUT) {
3633 } else {
3634 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00003635 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00003636 }
bellardf3311102006-04-12 20:21:17 +00003637 }
aliguori56f3a5d2008-10-31 18:07:17 +00003638
3639 *timeout = 0;
3640}
3641#else
blueswir169d64512008-12-07 19:30:18 +00003642static void host_main_loop_wait(int *timeout)
aliguori56f3a5d2008-10-31 18:07:17 +00003643{
3644}
bellardfd1dff42006-02-01 21:29:26 +00003645#endif
aliguori56f3a5d2008-10-31 18:07:17 +00003646
3647void main_loop_wait(int timeout)
3648{
3649 IOHandlerRecord *ioh;
3650 fd_set rfds, wfds, xfds;
3651 int ret, nfds;
3652 struct timeval tv;
3653
3654 qemu_bh_update_timeout(&timeout);
3655
3656 host_main_loop_wait(&timeout);
3657
bellardfd1dff42006-02-01 21:29:26 +00003658 /* poll any events */
3659 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00003660 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00003661 FD_ZERO(&rfds);
3662 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00003663 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00003664 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00003665 if (ioh->deleted)
3666 continue;
bellardfd1dff42006-02-01 21:29:26 +00003667 if (ioh->fd_read &&
3668 (!ioh->fd_read_poll ||
3669 ioh->fd_read_poll(ioh->opaque) != 0)) {
3670 FD_SET(ioh->fd, &rfds);
3671 if (ioh->fd > nfds)
3672 nfds = ioh->fd;
3673 }
3674 if (ioh->fd_write) {
3675 FD_SET(ioh->fd, &wfds);
3676 if (ioh->fd > nfds)
3677 nfds = ioh->fd;
3678 }
3679 }
ths3b46e622007-09-17 08:09:54 +00003680
aliguori56f3a5d2008-10-31 18:07:17 +00003681 tv.tv_sec = timeout / 1000;
3682 tv.tv_usec = (timeout % 1000) * 1000;
3683
bellarde0356492006-05-01 13:33:02 +00003684#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003685 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003686 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
3687 }
3688#endif
3689 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00003690 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00003691 IOHandlerRecord **pioh;
3692
3693 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00003694 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003695 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00003696 }
ths6ab43fd2007-08-25 01:34:19 +00003697 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00003698 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00003699 }
3700 }
thscafffd42007-02-28 21:59:44 +00003701
3702 /* remove deleted IO handlers */
3703 pioh = &first_io_handler;
3704 while (*pioh) {
3705 ioh = *pioh;
3706 if (ioh->deleted) {
3707 *pioh = ioh->next;
3708 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00003709 } else
thscafffd42007-02-28 21:59:44 +00003710 pioh = &ioh->next;
3711 }
bellardfd1dff42006-02-01 21:29:26 +00003712 }
bellarde0356492006-05-01 13:33:02 +00003713#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00003714 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00003715 if (ret < 0) {
3716 FD_ZERO(&rfds);
3717 FD_ZERO(&wfds);
3718 FD_ZERO(&xfds);
3719 }
3720 slirp_select_poll(&rfds, &wfds, &xfds);
3721 }
3722#endif
bellardc20709a2004-04-21 23:27:19 +00003723
aliguori357c6922008-11-25 17:26:09 +00003724 /* vm time timers */
3725 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
3726 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
3727 qemu_get_clock(vm_clock));
3728
3729 /* real time timers */
3730 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
3731 qemu_get_clock(rt_clock));
3732
pbrook423f0742007-05-23 00:06:54 +00003733 /* Check bottom-halves last in case any of the earlier events triggered
3734 them. */
3735 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00003736
bellard5905b2e2004-08-01 21:53:26 +00003737}
3738
pbrook9596ebb2007-11-18 01:44:38 +00003739static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00003740{
3741 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00003742#ifdef CONFIG_PROFILER
3743 int64_t ti;
3744#endif
bellard6a00d602005-11-21 23:25:50 +00003745 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00003746
bellard6a00d602005-11-21 23:25:50 +00003747 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00003748 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00003749 for(;;) {
3750 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00003751
bellard15a76442005-11-23 21:01:03 +00003752 for(;;) {
3753 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00003754 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00003755#ifdef CONFIG_PROFILER
3756 ti = profile_getclock();
3757#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003758 if (use_icount) {
3759 int64_t count;
3760 int decr;
3761 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
3762 env->icount_decr.u16.low = 0;
3763 env->icount_extra = 0;
3764 count = qemu_next_deadline();
3765 count = (count + (1 << icount_time_shift) - 1)
3766 >> icount_time_shift;
3767 qemu_icount += count;
3768 decr = (count > 0xffff) ? 0xffff : count;
3769 count -= decr;
3770 env->icount_decr.u16.low = decr;
3771 env->icount_extra = count;
3772 }
bellard6a00d602005-11-21 23:25:50 +00003773 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00003774#ifdef CONFIG_PROFILER
3775 qemu_time += profile_getclock() - ti;
3776#endif
pbrook2e70f6e2008-06-29 01:03:05 +00003777 if (use_icount) {
3778 /* Fold pending instructions back into the
3779 instruction counter, and clear the interrupt flag. */
3780 qemu_icount -= (env->icount_decr.u16.low
3781 + env->icount_extra);
3782 env->icount_decr.u32 = 0;
3783 env->icount_extra = 0;
3784 }
balrogee5605e2007-12-03 03:01:40 +00003785 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00003786 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00003787 ret = EXCP_INTERRUPT;
3788 event_pending = 0;
3789 break;
3790 }
pbrookbd967e02007-03-11 18:54:57 +00003791 if (ret == EXCP_HLT) {
3792 /* Give the next CPU a chance to run. */
3793 cur_cpu = env;
3794 continue;
3795 }
bellard15a76442005-11-23 21:01:03 +00003796 if (ret != EXCP_HALTED)
3797 break;
3798 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00003799 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00003800 break;
bellard15a76442005-11-23 21:01:03 +00003801 }
3802 cur_cpu = env;
3803
bellard5905b2e2004-08-01 21:53:26 +00003804 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00003805 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00003806 if (no_shutdown) {
3807 vm_stop(0);
3808 no_shutdown = 0;
3809 }
3810 else
3811 break;
bellard5905b2e2004-08-01 21:53:26 +00003812 }
3813 if (reset_requested) {
3814 reset_requested = 0;
3815 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00003816 ret = EXCP_INTERRUPT;
3817 }
3818 if (powerdown_requested) {
3819 powerdown_requested = 0;
3820 qemu_system_powerdown();
3821 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00003822 }
aurel3295b01002008-04-04 17:16:35 +00003823 if (unlikely(ret == EXCP_DEBUG)) {
aliguori880a7572008-11-18 20:30:24 +00003824 gdb_set_stop_cpu(cur_cpu);
bellard5905b2e2004-08-01 21:53:26 +00003825 vm_stop(EXCP_DEBUG);
3826 }
pbrookbd967e02007-03-11 18:54:57 +00003827 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00003828 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00003829 if (ret == EXCP_HALTED) {
3830 if (use_icount) {
3831 int64_t add;
3832 int64_t delta;
3833 /* Advance virtual time to the next event. */
3834 if (use_icount == 1) {
3835 /* When not using an adaptive execution frequency
3836 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00003837 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00003838 delta = 0;
3839 } else {
3840 delta = cpu_get_icount() - cpu_get_clock();
3841 }
3842 if (delta > 0) {
3843 /* If virtual time is ahead of real time then just
3844 wait for IO. */
3845 timeout = (delta / 1000000) + 1;
3846 } else {
3847 /* Wait for either IO to occur or the next
3848 timer event. */
3849 add = qemu_next_deadline();
3850 /* We advance the timer before checking for IO.
3851 Limit the amount we advance so that early IO
3852 activity won't get the guest too far ahead. */
3853 if (add > 10000000)
3854 add = 10000000;
3855 delta += add;
3856 add = (add + (1 << icount_time_shift) - 1)
3857 >> icount_time_shift;
3858 qemu_icount += add;
3859 timeout = delta / 1000000;
3860 if (timeout < 0)
3861 timeout = 0;
3862 }
3863 } else {
aliguori0a1af392008-10-31 18:40:25 +00003864 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00003865 }
3866 } else {
bellard5905b2e2004-08-01 21:53:26 +00003867 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00003868 }
bellard5905b2e2004-08-01 21:53:26 +00003869 } else {
blueswir198448f52008-09-30 18:16:09 +00003870 if (shutdown_requested) {
3871 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00003872 break;
blueswir198448f52008-09-30 18:16:09 +00003873 }
aliguori0a1af392008-10-31 18:40:25 +00003874 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00003875 }
bellard89bfc102006-02-08 22:46:31 +00003876#ifdef CONFIG_PROFILER
3877 ti = profile_getclock();
3878#endif
bellard5905b2e2004-08-01 21:53:26 +00003879 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00003880#ifdef CONFIG_PROFILER
3881 dev_time += profile_getclock() - ti;
3882#endif
bellardb4608c02003-06-27 17:34:32 +00003883 }
bellard34865132003-10-05 14:28:56 +00003884 cpu_disable_ticks();
3885 return ret;
bellardb4608c02003-06-27 17:34:32 +00003886}
3887
ths15f82202007-06-29 23:26:08 +00003888static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00003889{
blueswir1d2c639d2009-01-24 18:19:25 +00003890 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3891 and qemu-doc.texi */
bellard68d0f702008-01-06 17:21:48 +00003892 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00003893 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00003894 "\n"
bellarda20dd502003-09-30 21:07:02 +00003895 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00003896 "\n"
bellarda20dd502003-09-30 21:07:02 +00003897 "Standard options:\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003898 "-h or -help display this help and exit\n"
bellardcc1daa42005-06-05 14:49:17 +00003899 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00003900 "-cpu cpu select CPU (-cpu ? for list)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003901 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc45886d2004-01-05 00:02:06 +00003902 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00003903 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3904 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003905 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00003906 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3907 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguorifa879c62009-01-07 17:32:33 +00003908 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
thse4bcb142007-12-02 04:51:10 +00003909 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00003910 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00003911 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00003912 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00003913 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
ths667acca2006-12-11 02:08:05 +00003914 "-snapshot write to temporary files instead of disk image files\n"
bellarda00bad72004-05-22 21:39:06 +00003915 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard4ca00742004-12-12 22:20:04 +00003916#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00003917 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00003918#endif
bellard1d14ffa2005-10-30 18:58:22 +00003919#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00003920 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00003921 "-soundhw c1,... enable audio support\n"
3922 " and only specified sound cards (comma separated list)\n"
3923 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00003924 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00003925#endif
bellardb389dbf2005-11-06 16:49:55 +00003926 "-usb enable the USB driver (will be the default soon)\n"
3927 "-usbdevice name add the host or guest USB device 'name'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003928 "-name string set the name of the guest\n"
3929 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3930 " specify machine UUID\n"
3931 "\n"
3932 "Display options:\n"
3933 "-nographic disable graphical output and redirect serial I/Os to console\n"
3934#ifdef CONFIG_CURSES
3935 "-curses use a curses/ncurses interface instead of SDL\n"
3936#endif
3937#ifdef CONFIG_SDL
3938 "-no-frame open SDL window without a frame and window decorations\n"
3939 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3940 "-no-quit disable SDL window close capability\n"
3941 "-sdl enable SDL\n"
3942#endif
3943 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3944 "-vga [std|cirrus|vmware|none]\n"
3945 " select video card type\n"
3946 "-full-screen start in full screen\n"
bellard6f7e9ae2005-03-13 09:43:36 +00003947#if defined(TARGET_PPC) || defined(TARGET_SPARC)
3948 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00003949#endif
blueswir1d2c639d2009-01-24 18:19:25 +00003950 "-vnc display start a VNC server on display\n"
bellarda20dd502003-09-30 21:07:02 +00003951 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00003952 "Network options:\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003953 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003954 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00003955#ifdef CONFIG_SLIRP
aliguori7a9f6e42009-01-07 17:48:51 +00003956 "-net user[,vlan=n][,name=str][,hostname=host]\n"
pbrook115defd2006-04-16 11:06:58 +00003957 " connect the user mode network stack to VLAN 'n' and send\n"
3958 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00003959#endif
bellard7fb843f2006-02-01 23:06:55 +00003960#ifdef _WIN32
aliguori7a9f6e42009-01-07 17:48:51 +00003961 "-net tap[,vlan=n][,name=str],ifname=name\n"
bellard7fb843f2006-02-01 23:06:55 +00003962 " connect the host TAP network interface to VLAN 'n'\n"
3963#else
aliguori7a9f6e42009-01-07 17:48:51 +00003964 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
thsb46a8902007-10-21 23:20:45 +00003965 " connect the host TAP network interface to VLAN 'n' and use the\n"
3966 " network scripts 'file' (default=%s)\n"
3967 " and 'dfile' (default=%s);\n"
3968 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00003969 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00003970#endif
aliguori7a9f6e42009-01-07 17:48:51 +00003971 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00003972 " connect the vlan 'n' to another VLAN using a socket connection\n"
aliguori7a9f6e42009-01-07 17:48:51 +00003973 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
bellard3d830452005-12-18 16:36:49 +00003974 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00003975#ifdef CONFIG_VDE
aliguori7a9f6e42009-01-07 17:48:51 +00003976 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
ths8a16d272008-07-19 09:56:24 +00003977 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3978 " on host and listening for incoming connections on 'socketpath'.\n"
3979 " Use group 'groupname' and mode 'octalmode' to change default\n"
3980 " ownership and permissions for communication port.\n"
3981#endif
bellard7c9d8e02005-11-15 22:16:05 +00003982 "-net none use it alone to have zero network devices; if no -net option\n"
3983 " is provided, the default is '-net nic -net user'\n"
bellard7c9d8e02005-11-15 22:16:05 +00003984#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00003985 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00003986 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00003987#ifndef _WIN32
3988 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00003989#endif
bellard9bf05442004-08-25 22:12:49 +00003990 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00003991 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00003992#endif
bellardc4b1fcc2004-03-14 21:44:30 +00003993 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +00003994 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
3995 "-bt hci,host[:id]\n"
3996 " use host's HCI with the given name\n"
3997 "-bt hci[,vlan=n]\n"
3998 " emulate a standard HCI in virtual scatternet 'n'\n"
3999 "-bt vhci[,vlan=n]\n"
4000 " add host computer to virtual scatternet 'n' using VHCI\n"
4001 "-bt device:dev[,vlan=n]\n"
4002 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4003 "\n"
4004#ifdef TARGET_I386
4005 "\n"
4006 "i386 target only:\n"
4007 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4008 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4009 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4010 "-no-acpi disable ACPI\n"
4011 "-no-hpet disable HPET\n"
4012#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004013 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00004014 "-kernel bzImage use 'bzImage' as kernel image\n"
4015 "-append cmdline use 'cmdline' as kernel command line\n"
4016 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00004017 "\n"
bellard330d0412003-07-26 18:11:40 +00004018 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00004019 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00004020 "-parallel dev redirect the parallel port to char device 'dev'\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004021 "-monitor dev redirect the monitor to char device 'dev'\n"
4022 "-pidfile file write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00004023 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00004024 "-s wait gdb connection to port\n"
4025 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00004026 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004027 "-hdachs c,h,s[,t]\n"
4028 " force hard disk 0 physical geometry and the optional BIOS\n"
bellard46d47672004-11-16 01:45:27 +00004029 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00004030 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004031 "-bios file set the filename for the BIOS\n"
bellardd993e022005-02-10 22:00:06 +00004032#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00004033 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00004034 "-no-kqemu disable KQEMU kernel module usage\n"
4035#endif
aliguori7ba1e612008-11-05 16:04:33 +00004036#ifdef CONFIG_KVM
4037 "-enable-kvm enable KVM full virtualization support\n"
4038#endif
bellardd1beab82006-10-02 19:44:22 +00004039 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00004040 "-no-shutdown stop before shutdown\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004041 "-loadvm [tag|id]\n"
4042 " start right away with a saved state (loadvm in monitor)\n"
ths71e3ceb2006-12-22 02:11:31 +00004043#ifndef _WIN32
4044 "-daemonize daemonize QEMU after initializing\n"
4045#endif
ths9ae02552007-01-05 17:39:04 +00004046 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004047#if defined(TARGET_SPARC) || defined(TARGET_PPC)
4048 "-prom-env variable=value\n"
4049 " set OpenBIOS nvram variables\n"
blueswir166508602007-05-01 14:16:52 +00004050#endif
thsf3dcfad2007-08-24 01:26:02 +00004051 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00004052 " To see what timers are available use -clock ?\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004053 "-localtime set the real time clock to local time [default=utc]\n"
bellardbce61842008-02-01 22:18:51 +00004054 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00004055 "-icount [N|auto]\n"
blueswir1d2c639d2009-01-24 18:19:25 +00004056 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4057 "-echr chr set terminal escape character instead of ctrl-a\n"
4058 "-virtioconsole c\n"
4059 " set virtio console\n"
4060 "-show-cursor show cursor\n"
4061#if defined(TARGET_ARM) || defined(TARGET_M68K)
4062 "-semihosting semihosting mode\n"
4063#endif
4064#if defined(TARGET_ARM)
4065 "-old-param old param mode\n"
4066#endif
4067 "-tb-size n set TB size\n"
4068 "-incoming p prepare for incoming migration, listen on port p\n"
bellard0824d6f2003-06-24 13:42:40 +00004069 "\n"
bellard82c643f2004-07-14 17:28:13 +00004070 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004071 "ctrl-alt-f toggle full screen\n"
4072 "ctrl-alt-n switch to virtual console 'n'\n"
4073 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004074 "\n"
4075 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4076 ,
bellard0db63472003-10-27 21:37:46 +00004077 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004078 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004079#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004080 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004081 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004082#endif
bellard6e44ba72004-01-18 21:56:49 +00004083 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004084 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004085 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004086}
4087
bellardcd6f1162004-05-13 22:02:20 +00004088#define HAS_ARG 0x0001
4089
4090enum {
blueswir1d2c639d2009-01-24 18:19:25 +00004091 /* Please keep in synch with help, qemu_options[] and
4092 qemu-doc.texi */
4093 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004094 QEMU_OPTION_h,
bellardcc1daa42005-06-05 14:49:17 +00004095 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004096 QEMU_OPTION_cpu,
blueswir1d2c639d2009-01-24 18:19:25 +00004097 QEMU_OPTION_smp,
bellardcd6f1162004-05-13 22:02:20 +00004098 QEMU_OPTION_fda,
4099 QEMU_OPTION_fdb,
4100 QEMU_OPTION_hda,
4101 QEMU_OPTION_hdb,
4102 QEMU_OPTION_hdc,
4103 QEMU_OPTION_hdd,
4104 QEMU_OPTION_cdrom,
blueswir1d2c639d2009-01-24 18:19:25 +00004105 QEMU_OPTION_drive,
balrog3e3d5812007-04-30 02:09:25 +00004106 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004107 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004108 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004109 QEMU_OPTION_boot,
4110 QEMU_OPTION_snapshot,
4111 QEMU_OPTION_m,
blueswir1d2c639d2009-01-24 18:19:25 +00004112 QEMU_OPTION_k,
bellard1d14ffa2005-10-30 18:58:22 +00004113 QEMU_OPTION_audio_help,
4114 QEMU_OPTION_soundhw,
blueswir1d2c639d2009-01-24 18:19:25 +00004115 QEMU_OPTION_usb,
4116 QEMU_OPTION_usbdevice,
4117 QEMU_OPTION_name,
4118 QEMU_OPTION_uuid,
bellardcd6f1162004-05-13 22:02:20 +00004119
blueswir1d2c639d2009-01-24 18:19:25 +00004120 /* Display options: */
4121 QEMU_OPTION_nographic,
4122 QEMU_OPTION_curses,
4123 QEMU_OPTION_no_frame,
4124 QEMU_OPTION_alt_grab,
4125 QEMU_OPTION_no_quit,
4126 QEMU_OPTION_sdl,
4127 QEMU_OPTION_portrait,
4128 QEMU_OPTION_vga,
4129 QEMU_OPTION_full_screen,
4130 QEMU_OPTION_g,
4131 QEMU_OPTION_vnc,
4132
4133 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004134 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004135 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004136 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004137 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004138 QEMU_OPTION_redir,
balrogdc72ac12008-11-09 00:04:26 +00004139 QEMU_OPTION_bt,
bellardcd6f1162004-05-13 22:02:20 +00004140
blueswir1d2c639d2009-01-24 18:19:25 +00004141 /* i386 target only: */
4142 QEMU_OPTION_win2k_hack,
4143 QEMU_OPTION_rtc_td_hack,
4144 QEMU_OPTION_no_fd_bootchk,
4145 QEMU_OPTION_no_acpi,
4146 QEMU_OPTION_no_hpet,
4147
4148 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004149 QEMU_OPTION_kernel,
4150 QEMU_OPTION_append,
4151 QEMU_OPTION_initrd,
4152
blueswir1d2c639d2009-01-24 18:19:25 +00004153 /* Debug/Expert options: */
4154 QEMU_OPTION_serial,
4155 QEMU_OPTION_parallel,
4156 QEMU_OPTION_monitor,
4157 QEMU_OPTION_pidfile,
bellardcd6f1162004-05-13 22:02:20 +00004158 QEMU_OPTION_S,
4159 QEMU_OPTION_s,
4160 QEMU_OPTION_p,
4161 QEMU_OPTION_d,
4162 QEMU_OPTION_hdachs,
4163 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004164 QEMU_OPTION_bios,
bellard89bfc102006-02-08 22:46:31 +00004165 QEMU_OPTION_kernel_kqemu,
blueswir1d2c639d2009-01-24 18:19:25 +00004166 QEMU_OPTION_no_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004167 QEMU_OPTION_enable_kvm,
bellardd1beab82006-10-02 19:44:22 +00004168 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004169 QEMU_OPTION_no_shutdown,
blueswir1d2c639d2009-01-24 18:19:25 +00004170 QEMU_OPTION_loadvm,
ths71e3ceb2006-12-22 02:11:31 +00004171 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004172 QEMU_OPTION_option_rom,
blueswir166508602007-05-01 14:16:52 +00004173 QEMU_OPTION_prom_env,
thsf3dcfad2007-08-24 01:26:02 +00004174 QEMU_OPTION_clock,
blueswir1d2c639d2009-01-24 18:19:25 +00004175 QEMU_OPTION_localtime,
bellard7e0af5d02007-11-07 16:24:33 +00004176 QEMU_OPTION_startdate,
pbrook2e70f6e2008-06-29 01:03:05 +00004177 QEMU_OPTION_icount,
blueswir1d2c639d2009-01-24 18:19:25 +00004178 QEMU_OPTION_echr,
4179 QEMU_OPTION_virtiocon,
4180 QEMU_OPTION_show_cursor,
4181 QEMU_OPTION_semihosting,
4182 QEMU_OPTION_old_param,
4183 QEMU_OPTION_tb_size,
aliguori5bb79102008-10-13 03:12:02 +00004184 QEMU_OPTION_incoming,
bellardcd6f1162004-05-13 22:02:20 +00004185};
4186
4187typedef struct QEMUOption {
4188 const char *name;
4189 int flags;
4190 int index;
4191} QEMUOption;
4192
blueswir1dbed7e42008-10-01 19:38:09 +00004193static const QEMUOption qemu_options[] = {
blueswir1d2c639d2009-01-24 18:19:25 +00004194 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4195 qemu-doc.texi */
4196 /* Standard options: */
bellardcd6f1162004-05-13 22:02:20 +00004197 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004198 { "help", 0, QEMU_OPTION_h },
bellardcc1daa42005-06-05 14:49:17 +00004199 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00004200 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
blueswir1d2c639d2009-01-24 18:19:25 +00004201 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellardcd6f1162004-05-13 22:02:20 +00004202 { "fda", HAS_ARG, QEMU_OPTION_fda },
4203 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4204 { "hda", HAS_ARG, QEMU_OPTION_hda },
4205 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4206 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4207 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
4208 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
blueswir1d2c639d2009-01-24 18:19:25 +00004209 { "drive", HAS_ARG, QEMU_OPTION_drive },
balrog3e3d5812007-04-30 02:09:25 +00004210 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00004211 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00004212 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00004213 { "boot", HAS_ARG, QEMU_OPTION_boot },
4214 { "snapshot", 0, QEMU_OPTION_snapshot },
4215 { "m", HAS_ARG, QEMU_OPTION_m },
blueswir1d2c639d2009-01-24 18:19:25 +00004216#ifndef _WIN32
bellard3d11d0e2004-12-12 16:56:30 +00004217 { "k", HAS_ARG, QEMU_OPTION_k },
blueswir1d2c639d2009-01-24 18:19:25 +00004218#endif
bellard1d14ffa2005-10-30 18:58:22 +00004219#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004220 { "audio-help", 0, QEMU_OPTION_audio_help },
4221 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4222#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004223 { "usb", 0, QEMU_OPTION_usb },
4224 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
4225 { "name", HAS_ARG, QEMU_OPTION_name },
4226 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
bellardcd6f1162004-05-13 22:02:20 +00004227
blueswir1d2c639d2009-01-24 18:19:25 +00004228 /* Display options: */
4229 { "nographic", 0, QEMU_OPTION_nographic },
4230#ifdef CONFIG_CURSES
4231 { "curses", 0, QEMU_OPTION_curses },
4232#endif
4233#ifdef CONFIG_SDL
4234 { "no-frame", 0, QEMU_OPTION_no_frame },
4235 { "alt-grab", 0, QEMU_OPTION_alt_grab },
4236 { "no-quit", 0, QEMU_OPTION_no_quit },
4237 { "sdl", 0, QEMU_OPTION_sdl },
4238#endif
4239 { "portrait", 0, QEMU_OPTION_portrait },
4240 { "vga", HAS_ARG, QEMU_OPTION_vga },
4241 { "full-screen", 0, QEMU_OPTION_full_screen },
4242#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4243 { "g", 1, QEMU_OPTION_g },
4244#endif
4245 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
4246
4247 /* Network options: */
bellard7c9d8e02005-11-15 22:16:05 +00004248 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00004249#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00004250 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00004251 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00004252#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004253 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00004254#endif
bellard9bf05442004-08-25 22:12:49 +00004255 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00004256#endif
balrogdc72ac12008-11-09 00:04:26 +00004257 { "bt", HAS_ARG, QEMU_OPTION_bt },
blueswir1d2c639d2009-01-24 18:19:25 +00004258#ifdef TARGET_I386
4259 /* i386 target only: */
4260 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
4261 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack },
4262 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4263 { "no-acpi", 0, QEMU_OPTION_no_acpi },
4264 { "no-hpet", 0, QEMU_OPTION_no_hpet },
4265#endif
bellardcd6f1162004-05-13 22:02:20 +00004266
blueswir1d2c639d2009-01-24 18:19:25 +00004267 /* Linux boot specific: */
bellardcd6f1162004-05-13 22:02:20 +00004268 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4269 { "append", HAS_ARG, QEMU_OPTION_append },
4270 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4271
blueswir1d2c639d2009-01-24 18:19:25 +00004272 /* Debug/Expert options: */
4273 { "serial", HAS_ARG, QEMU_OPTION_serial },
4274 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
4275 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4276 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellardcd6f1162004-05-13 22:02:20 +00004277 { "S", 0, QEMU_OPTION_S },
4278 { "s", 0, QEMU_OPTION_s },
4279 { "p", HAS_ARG, QEMU_OPTION_p },
4280 { "d", HAS_ARG, QEMU_OPTION_d },
4281 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4282 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00004283 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00004284#ifdef USE_KQEMU
bellard89bfc102006-02-08 22:46:31 +00004285 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
blueswir1d2c639d2009-01-24 18:19:25 +00004286 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellardd993e022005-02-10 22:00:06 +00004287#endif
aliguori7ba1e612008-11-05 16:04:33 +00004288#ifdef CONFIG_KVM
4289 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
4290#endif
bellardd1beab82006-10-02 19:44:22 +00004291 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00004292 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
blueswir1d2c639d2009-01-24 18:19:25 +00004293 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
ths71e3ceb2006-12-22 02:11:31 +00004294 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00004295 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
blueswir195efd112008-12-24 20:26:14 +00004296#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00004297 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
4298#endif
blueswir1d2c639d2009-01-24 18:19:25 +00004299 { "clock", HAS_ARG, QEMU_OPTION_clock },
4300 { "localtime", 0, QEMU_OPTION_localtime },
4301 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
4302 { "icount", HAS_ARG, QEMU_OPTION_icount },
4303 { "echr", HAS_ARG, QEMU_OPTION_echr },
4304 { "virtioconsole", HAS_ARG, QEMU_OPTION_virtiocon },
4305 { "show-cursor", 0, QEMU_OPTION_show_cursor },
4306#if defined(TARGET_ARM) || defined(TARGET_M68K)
4307 { "semihosting", 0, QEMU_OPTION_semihosting },
4308#endif
balrog2b8f2d42007-07-27 22:08:46 +00004309#if defined(TARGET_ARM)
4310 { "old-param", 0, QEMU_OPTION_old_param },
4311#endif
bellard26a5f132008-05-28 12:30:31 +00004312 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
aliguori5bb79102008-10-13 03:12:02 +00004313 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
bellardcd6f1162004-05-13 22:02:20 +00004314 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00004315};
4316
bellard5905b2e2004-08-01 21:53:26 +00004317/* password input */
4318
balrog2bac6012007-04-30 01:34:31 +00004319int qemu_key_check(BlockDriverState *bs, const char *name)
4320{
4321 char password[256];
4322 int i;
4323
4324 if (!bdrv_is_encrypted(bs))
4325 return 0;
4326
4327 term_printf("%s is encrypted.\n", name);
4328 for(i = 0; i < 3; i++) {
4329 monitor_readline("Password: ", 1, password, sizeof(password));
4330 if (bdrv_set_key(bs, password) == 0)
4331 return 0;
4332 term_printf("invalid password\n");
4333 }
4334 return -EPERM;
4335}
4336
aliguori83ab7952008-08-19 14:44:22 +00004337static BlockDriverState *get_bdrv(int index)
4338{
4339 if (index > nb_drives)
4340 return NULL;
4341 return drives_table[index].bdrv;
4342}
4343
bellard5905b2e2004-08-01 21:53:26 +00004344static void read_passwords(void)
4345{
4346 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00004347 int i;
bellard5905b2e2004-08-01 21:53:26 +00004348
aliguori83ab7952008-08-19 14:44:22 +00004349 for(i = 0; i < 6; i++) {
4350 bs = get_bdrv(i);
4351 if (bs)
4352 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00004353 }
4354}
4355
bellard1d14ffa2005-10-30 18:58:22 +00004356#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00004357struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00004358#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00004359#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00004360 {
4361 "pcspk",
4362 "PC speaker",
4363 0,
4364 1,
4365 { .init_isa = pcspk_audio_init }
4366 },
4367#endif
malc4c9b53e2009-01-09 10:46:34 +00004368
4369#ifdef CONFIG_SB16
bellard6a36d842005-12-18 20:34:32 +00004370 {
4371 "sb16",
4372 "Creative Sound Blaster 16",
4373 0,
4374 1,
4375 { .init_isa = SB16_init }
4376 },
malc4c9b53e2009-01-09 10:46:34 +00004377#endif
bellard6a36d842005-12-18 20:34:32 +00004378
malccc53d262008-06-13 10:48:22 +00004379#ifdef CONFIG_CS4231A
4380 {
4381 "cs4231a",
4382 "CS4231A",
4383 0,
4384 1,
4385 { .init_isa = cs4231a_init }
4386 },
4387#endif
4388
bellard6a36d842005-12-18 20:34:32 +00004389#ifdef CONFIG_ADLIB
4390 {
4391 "adlib",
4392#ifdef HAS_YMF262
4393 "Yamaha YMF262 (OPL3)",
4394#else
4395 "Yamaha YM3812 (OPL2)",
4396#endif
4397 0,
4398 1,
4399 { .init_isa = Adlib_init }
4400 },
4401#endif
4402
4403#ifdef CONFIG_GUS
4404 {
4405 "gus",
4406 "Gravis Ultrasound GF1",
4407 0,
4408 1,
4409 { .init_isa = GUS_init }
4410 },
4411#endif
4412
malc4c9b53e2009-01-09 10:46:34 +00004413#ifdef CONFIG_AC97
balroge5c9a132008-01-14 04:27:55 +00004414 {
4415 "ac97",
4416 "Intel 82801AA AC97 Audio",
4417 0,
4418 0,
4419 { .init_pci = ac97_init }
4420 },
malc4c9b53e2009-01-09 10:46:34 +00004421#endif
balroge5c9a132008-01-14 04:27:55 +00004422
malc4c9b53e2009-01-09 10:46:34 +00004423#ifdef CONFIG_ES1370
bellard6a36d842005-12-18 20:34:32 +00004424 {
4425 "es1370",
4426 "ENSONIQ AudioPCI ES1370",
4427 0,
4428 0,
4429 { .init_pci = es1370_init }
4430 },
balrogb00052e2007-04-30 02:22:06 +00004431#endif
bellard6a36d842005-12-18 20:34:32 +00004432
malc4c9b53e2009-01-09 10:46:34 +00004433#endif /* HAS_AUDIO_CHOICE */
4434
bellard6a36d842005-12-18 20:34:32 +00004435 { NULL, NULL, 0, 0, { NULL } }
4436};
4437
bellard1d14ffa2005-10-30 18:58:22 +00004438static void select_soundhw (const char *optarg)
4439{
bellard6a36d842005-12-18 20:34:32 +00004440 struct soundhw *c;
4441
bellard1d14ffa2005-10-30 18:58:22 +00004442 if (*optarg == '?') {
4443 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00004444
bellard1d14ffa2005-10-30 18:58:22 +00004445 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00004446 for (c = soundhw; c->name; ++c) {
4447 printf ("%-11s %s\n", c->name, c->descr);
4448 }
4449 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00004450 exit (*optarg != '?');
4451 }
4452 else {
bellard6a36d842005-12-18 20:34:32 +00004453 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00004454 const char *p;
4455 char *e;
4456 int bad_card = 0;
4457
bellard6a36d842005-12-18 20:34:32 +00004458 if (!strcmp (optarg, "all")) {
4459 for (c = soundhw; c->name; ++c) {
4460 c->enabled = 1;
4461 }
4462 return;
4463 }
bellard1d14ffa2005-10-30 18:58:22 +00004464
bellard6a36d842005-12-18 20:34:32 +00004465 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00004466 while (*p) {
4467 e = strchr (p, ',');
4468 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00004469
4470 for (c = soundhw; c->name; ++c) {
4471 if (!strncmp (c->name, p, l)) {
4472 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00004473 break;
4474 }
4475 }
bellard6a36d842005-12-18 20:34:32 +00004476
4477 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00004478 if (l > 80) {
4479 fprintf (stderr,
4480 "Unknown sound card name (too big to show)\n");
4481 }
4482 else {
4483 fprintf (stderr, "Unknown sound card name `%.*s'\n",
4484 (int) l, p);
4485 }
4486 bad_card = 1;
4487 }
4488 p += l + (e != NULL);
4489 }
4490
4491 if (bad_card)
4492 goto show_valid_cards;
4493 }
4494}
4495#endif
4496
malc3893c122008-09-28 00:42:05 +00004497static void select_vgahw (const char *p)
4498{
4499 const char *opts;
4500
4501 if (strstart(p, "std", &opts)) {
aliguoric2b3b412009-01-15 20:37:28 +00004502 std_vga_enabled = 1;
malc3893c122008-09-28 00:42:05 +00004503 cirrus_vga_enabled = 0;
4504 vmsvga_enabled = 0;
4505 } else if (strstart(p, "cirrus", &opts)) {
4506 cirrus_vga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004507 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004508 vmsvga_enabled = 0;
4509 } else if (strstart(p, "vmware", &opts)) {
4510 cirrus_vga_enabled = 0;
aliguoric2b3b412009-01-15 20:37:28 +00004511 std_vga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004512 vmsvga_enabled = 1;
aliguoric2b3b412009-01-15 20:37:28 +00004513 } else if (strstart(p, "none", &opts)) {
4514 cirrus_vga_enabled = 0;
4515 std_vga_enabled = 0;
4516 vmsvga_enabled = 0;
malc3893c122008-09-28 00:42:05 +00004517 } else {
4518 invalid_vga:
4519 fprintf(stderr, "Unknown vga type: %s\n", p);
4520 exit(1);
4521 }
malccb5a7aa2008-09-28 00:42:12 +00004522 while (*opts) {
4523 const char *nextopt;
4524
4525 if (strstart(opts, ",retrace=", &nextopt)) {
4526 opts = nextopt;
4527 if (strstart(opts, "dumb", &nextopt))
4528 vga_retrace_method = VGA_RETRACE_DUMB;
4529 else if (strstart(opts, "precise", &nextopt))
4530 vga_retrace_method = VGA_RETRACE_PRECISE;
4531 else goto invalid_vga;
4532 } else goto invalid_vga;
4533 opts = nextopt;
4534 }
malc3893c122008-09-28 00:42:05 +00004535}
4536
bellard3587d7e2006-06-26 20:03:44 +00004537#ifdef _WIN32
4538static BOOL WINAPI qemu_ctrl_handler(DWORD type)
4539{
4540 exit(STATUS_CONTROL_C_EXIT);
4541 return TRUE;
4542}
4543#endif
4544
blueswir18fcb1b92008-09-18 18:29:08 +00004545static int qemu_uuid_parse(const char *str, uint8_t *uuid)
4546{
4547 int ret;
4548
4549 if(strlen(str) != 36)
4550 return -1;
4551
4552 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
4553 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
4554 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
4555
4556 if(ret != 16)
4557 return -1;
4558
4559 return 0;
4560}
4561
bellard7c9d8e02005-11-15 22:16:05 +00004562#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00004563
aliguori5b08fc12008-08-21 20:08:03 +00004564#ifndef _WIN32
4565
4566static void termsig_handler(int signal)
4567{
4568 qemu_system_shutdown_request();
4569}
4570
blueswir16f9e3802008-09-09 18:56:59 +00004571static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00004572{
4573 struct sigaction act;
4574
4575 memset(&act, 0, sizeof(act));
4576 act.sa_handler = termsig_handler;
4577 sigaction(SIGINT, &act, NULL);
4578 sigaction(SIGHUP, &act, NULL);
4579 sigaction(SIGTERM, &act, NULL);
4580}
4581
4582#endif
4583
malc902b3d52008-12-10 19:18:40 +00004584int main(int argc, char **argv, char **envp)
bellard0824d6f2003-06-24 13:42:40 +00004585{
bellard67b915a2004-03-31 23:37:16 +00004586#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00004587 int use_gdbstub;
4588 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00004589#endif
j_mayer28c5af52007-11-11 01:50:45 +00004590 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00004591 int i;
j_mayer28c5af52007-11-11 01:50:45 +00004592 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00004593 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00004594 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00004595 const char *boot_devices = "";
aliguori3023f332009-01-16 19:04:14 +00004596 DisplayState *ds;
aliguori7d957bd2009-01-15 22:14:11 +00004597 DisplayChangeListener *dcl;
bellard46d47672004-11-16 01:45:27 +00004598 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00004599 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00004600 int nb_net_clients;
balrogdc72ac12008-11-09 00:04:26 +00004601 const char *bt_opts[MAX_BT_CMDLINE];
4602 int nb_bt_opts;
thse4bcb142007-12-02 04:51:10 +00004603 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00004604 int optind;
4605 const char *r, *optarg;
aliguori4c621802009-01-16 21:48:20 +00004606 CharDriverState *monitor_hd = NULL;
pbrookfd5f3932008-03-26 20:55:43 +00004607 const char *monitor_device;
4608 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00004609 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00004610 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00004611 int parallel_device_index;
aliguori9ede2fd2009-01-15 20:05:25 +00004612 const char *virtio_consoles[MAX_VIRTIO_CONSOLES];
4613 int virtio_console_index;
bellardd63d3072004-10-03 13:29:03 +00004614 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004615 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004616 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00004617 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00004618 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00004619 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00004620 int tb_size;
ths93815bc2007-03-19 15:58:31 +00004621 const char *pid_file = NULL;
blueswir141bd6392008-10-05 09:56:21 +00004622 int autostart;
aliguori5bb79102008-10-13 03:12:02 +00004623 const char *incoming = NULL;
bellard0bd48852005-11-11 00:00:47 +00004624
malc902b3d52008-12-10 19:18:40 +00004625 qemu_cache_utils_init(envp);
4626
bellard0bd48852005-11-11 00:00:47 +00004627 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00004628#ifndef _WIN32
4629 {
4630 struct sigaction act;
4631 sigfillset(&act.sa_mask);
4632 act.sa_flags = 0;
4633 act.sa_handler = SIG_IGN;
4634 sigaction(SIGPIPE, &act, NULL);
4635 }
bellard3587d7e2006-06-26 20:03:44 +00004636#else
4637 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00004638 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4639 QEMU to run on a single CPU */
4640 {
4641 HANDLE h;
4642 DWORD mask, smask;
4643 int i;
4644 h = GetCurrentProcess();
4645 if (GetProcessAffinityMask(h, &mask, &smask)) {
4646 for(i = 0; i < 32; i++) {
4647 if (mask & (1 << i))
4648 break;
4649 }
4650 if (i != 32) {
4651 mask = 1 << i;
4652 SetProcessAffinityMask(h, mask);
4653 }
4654 }
4655 }
bellard67b915a2004-03-31 23:37:16 +00004656#endif
bellardbe995c22006-06-25 16:25:21 +00004657
bellardcc1daa42005-06-05 14:49:17 +00004658 register_machines();
4659 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00004660 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00004661 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00004662 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00004663 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00004664#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00004665 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00004666 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00004667#endif
bellard33e39632003-07-06 17:15:21 +00004668 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00004669 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00004670 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00004671 kernel_filename = NULL;
4672 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00004673 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00004674 translation = BIOS_ATA_TRANSLATION_AUTO;
pbrookc60e08d2008-07-01 16:24:38 +00004675 monitor_device = "vc";
bellardc4b1fcc2004-03-14 21:44:30 +00004676
aurel32c75a8232008-05-04 00:50:34 +00004677 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00004678 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004679 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00004680 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004681
aurel32c75a8232008-05-04 00:50:34 +00004682 parallel_devices[0] = "vc:640x480";
bellard6508fe52005-01-15 12:02:56 +00004683 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00004684 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00004685 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00004686
aliguori9ede2fd2009-01-15 20:05:25 +00004687 virtio_consoles[0] = "vc:80Cx24C";
4688 for(i = 1; i < MAX_VIRTIO_CONSOLES; i++)
4689 virtio_consoles[i] = NULL;
4690 virtio_console_index = 0;
4691
bellarda594cfb2005-11-06 16:13:29 +00004692 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00004693
bellard7c9d8e02005-11-15 22:16:05 +00004694 nb_net_clients = 0;
balrogdc72ac12008-11-09 00:04:26 +00004695 nb_bt_opts = 0;
thse4bcb142007-12-02 04:51:10 +00004696 nb_drives = 0;
4697 nb_drives_opt = 0;
4698 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00004699
4700 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00004701
bellard26a5f132008-05-28 12:30:31 +00004702 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00004703 autostart= 1;
4704
bellardcd6f1162004-05-13 22:02:20 +00004705 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00004706 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00004707 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00004708 break;
bellardcd6f1162004-05-13 22:02:20 +00004709 r = argv[optind];
4710 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00004711 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00004712 } else {
4713 const QEMUOption *popt;
4714
4715 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00004716 /* Treat --foo the same as -foo. */
4717 if (r[1] == '-')
4718 r++;
bellardcd6f1162004-05-13 22:02:20 +00004719 popt = qemu_options;
4720 for(;;) {
4721 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00004722 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00004723 argv[0], r);
4724 exit(1);
4725 }
4726 if (!strcmp(popt->name, r + 1))
4727 break;
4728 popt++;
4729 }
4730 if (popt->flags & HAS_ARG) {
4731 if (optind >= argc) {
4732 fprintf(stderr, "%s: option '%s' requires an argument\n",
4733 argv[0], r);
4734 exit(1);
4735 }
4736 optarg = argv[optind++];
4737 } else {
4738 optarg = NULL;
4739 }
4740
4741 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00004742 case QEMU_OPTION_M:
4743 machine = find_machine(optarg);
4744 if (!machine) {
4745 QEMUMachine *m;
4746 printf("Supported machines are:\n");
4747 for(m = first_machine; m != NULL; m = m->next) {
4748 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00004749 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00004750 m == first_machine ? " (default)" : "");
4751 }
ths15f82202007-06-29 23:26:08 +00004752 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00004753 }
4754 break;
j_mayer94fc95c2007-03-05 19:44:02 +00004755 case QEMU_OPTION_cpu:
4756 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00004757 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00004758/* XXX: implement xxx_cpu_list for targets that still miss it */
4759#if defined(cpu_list)
4760 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00004761#endif
ths15f82202007-06-29 23:26:08 +00004762 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00004763 } else {
4764 cpu_model = optarg;
4765 }
4766 break;
bellardcd6f1162004-05-13 22:02:20 +00004767 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00004768 initrd_filename = optarg;
4769 break;
bellardcd6f1162004-05-13 22:02:20 +00004770 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00004771 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00004772 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00004773 else
balrog609497a2008-01-14 02:56:53 +00004774 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00004775 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00004776 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004777 translation == BIOS_ATA_TRANSLATION_LBA ?
4778 ",trans=lba" :
4779 translation == BIOS_ATA_TRANSLATION_NONE ?
4780 ",trans=none" : "");
4781 break;
bellardcd6f1162004-05-13 22:02:20 +00004782 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00004783 case QEMU_OPTION_hdc:
4784 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00004785 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00004786 break;
thse4bcb142007-12-02 04:51:10 +00004787 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00004788 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00004789 break;
balrog3e3d5812007-04-30 02:09:25 +00004790 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00004791 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00004792 break;
pbrooka1bb27b2007-04-06 16:49:48 +00004793 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00004794 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00004795 break;
j_mayer86f55662007-04-24 06:52:59 +00004796 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00004797 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00004798 break;
bellardcd6f1162004-05-13 22:02:20 +00004799 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00004800 snapshot = 1;
4801 break;
bellardcd6f1162004-05-13 22:02:20 +00004802 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00004803 {
bellard330d0412003-07-26 18:11:40 +00004804 const char *p;
4805 p = optarg;
4806 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004807 if (cyls < 1 || cyls > 16383)
4808 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004809 if (*p != ',')
4810 goto chs_fail;
4811 p++;
4812 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004813 if (heads < 1 || heads > 16)
4814 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00004815 if (*p != ',')
4816 goto chs_fail;
4817 p++;
4818 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00004819 if (secs < 1 || secs > 63)
4820 goto chs_fail;
4821 if (*p == ',') {
4822 p++;
4823 if (!strcmp(p, "none"))
4824 translation = BIOS_ATA_TRANSLATION_NONE;
4825 else if (!strcmp(p, "lba"))
4826 translation = BIOS_ATA_TRANSLATION_LBA;
4827 else if (!strcmp(p, "auto"))
4828 translation = BIOS_ATA_TRANSLATION_AUTO;
4829 else
4830 goto chs_fail;
4831 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00004832 chs_fail:
bellard46d47672004-11-16 01:45:27 +00004833 fprintf(stderr, "qemu: invalid physical CHS format\n");
4834 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00004835 }
thse4bcb142007-12-02 04:51:10 +00004836 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00004837 snprintf(drives_opt[hda_index].opt,
4838 sizeof(drives_opt[hda_index].opt),
4839 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
4840 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00004841 translation == BIOS_ATA_TRANSLATION_LBA ?
4842 ",trans=lba" :
4843 translation == BIOS_ATA_TRANSLATION_NONE ?
4844 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00004845 }
4846 break;
bellardcd6f1162004-05-13 22:02:20 +00004847 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00004848 nographic = 1;
4849 break;
balrog4d3b6f62008-02-10 16:33:14 +00004850#ifdef CONFIG_CURSES
4851 case QEMU_OPTION_curses:
4852 curses = 1;
4853 break;
4854#endif
balroga171fe32007-04-30 01:48:07 +00004855 case QEMU_OPTION_portrait:
4856 graphic_rotate = 1;
4857 break;
bellardcd6f1162004-05-13 22:02:20 +00004858 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00004859 kernel_filename = optarg;
4860 break;
bellardcd6f1162004-05-13 22:02:20 +00004861 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00004862 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00004863 break;
bellardcd6f1162004-05-13 22:02:20 +00004864 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00004865 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00004866 break;
bellardcd6f1162004-05-13 22:02:20 +00004867 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00004868 boot_devices = optarg;
4869 /* We just do some generic consistency checks */
4870 {
4871 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00004872 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00004873
4874 boot_devices_bitmap = 0;
4875 for (p = boot_devices; *p != '\0'; p++) {
4876 /* Allowed boot devices are:
4877 * a b : floppy disk drives
4878 * c ... f : IDE disk drives
4879 * g ... m : machine implementation dependant drives
4880 * n ... p : network devices
4881 * It's up to each machine implementation to check
4882 * if the given boot devices match the actual hardware
4883 * implementation and firmware features.
4884 */
4885 if (*p < 'a' || *p > 'q') {
4886 fprintf(stderr, "Invalid boot device '%c'\n", *p);
4887 exit(1);
4888 }
4889 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
4890 fprintf(stderr,
4891 "Boot device '%c' was given twice\n",*p);
4892 exit(1);
4893 }
4894 boot_devices_bitmap |= 1 << (*p - 'a');
4895 }
bellard36b486b2003-11-11 13:36:08 +00004896 }
4897 break;
bellardcd6f1162004-05-13 22:02:20 +00004898 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00004899 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00004900 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00004901 break;
bellard52ca8d62006-06-14 16:03:05 +00004902#ifdef TARGET_I386
4903 case QEMU_OPTION_no_fd_bootchk:
4904 fd_bootchk = 0;
4905 break;
4906#endif
bellard7c9d8e02005-11-15 22:16:05 +00004907 case QEMU_OPTION_net:
4908 if (nb_net_clients >= MAX_NET_CLIENTS) {
4909 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00004910 exit(1);
4911 }
pbrookfd5f3932008-03-26 20:55:43 +00004912 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00004913 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00004914 break;
bellardc7f74642004-08-24 21:57:12 +00004915#ifdef CONFIG_SLIRP
4916 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00004917 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00004918 break;
ths47d5d012007-02-20 00:05:08 +00004919 case QEMU_OPTION_bootp:
4920 bootp_filename = optarg;
4921 break;
bellardc94c8d62004-09-13 21:37:34 +00004922#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00004923 case QEMU_OPTION_smb:
4924 net_slirp_smb(optarg);
4925 break;
bellardc94c8d62004-09-13 21:37:34 +00004926#endif
bellard9bf05442004-08-25 22:12:49 +00004927 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00004928 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00004929 break;
bellardc7f74642004-08-24 21:57:12 +00004930#endif
balrogdc72ac12008-11-09 00:04:26 +00004931 case QEMU_OPTION_bt:
4932 if (nb_bt_opts >= MAX_BT_CMDLINE) {
4933 fprintf(stderr, "qemu: too many bluetooth options\n");
4934 exit(1);
4935 }
4936 bt_opts[nb_bt_opts++] = optarg;
4937 break;
bellard1d14ffa2005-10-30 18:58:22 +00004938#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004939 case QEMU_OPTION_audio_help:
4940 AUD_help ();
4941 exit (0);
4942 break;
4943 case QEMU_OPTION_soundhw:
4944 select_soundhw (optarg);
4945 break;
4946#endif
bellardcd6f1162004-05-13 22:02:20 +00004947 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00004948 help(0);
bellardcd6f1162004-05-13 22:02:20 +00004949 break;
aurel3200f82b82008-04-27 21:12:55 +00004950 case QEMU_OPTION_m: {
4951 uint64_t value;
4952 char *ptr;
4953
4954 value = strtoul(optarg, &ptr, 10);
4955 switch (*ptr) {
4956 case 0: case 'M': case 'm':
4957 value <<= 20;
4958 break;
4959 case 'G': case 'g':
4960 value <<= 30;
4961 break;
4962 default:
4963 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00004964 exit(1);
4965 }
aurel3200f82b82008-04-27 21:12:55 +00004966
4967 /* On 32-bit hosts, QEMU is limited by virtual address space */
4968 if (value > (2047 << 20)
4969#ifndef USE_KQEMU
4970 && HOST_LONG_BITS == 32
4971#endif
4972 ) {
4973 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
4974 exit(1);
4975 }
4976 if (value != (uint64_t)(ram_addr_t)value) {
4977 fprintf(stderr, "qemu: ram size too large\n");
4978 exit(1);
4979 }
4980 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00004981 break;
aurel3200f82b82008-04-27 21:12:55 +00004982 }
bellardcd6f1162004-05-13 22:02:20 +00004983 case QEMU_OPTION_d:
4984 {
4985 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00004986 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00004987
bellardcd6f1162004-05-13 22:02:20 +00004988 mask = cpu_str_to_log_mask(optarg);
4989 if (!mask) {
4990 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00004991 for(item = cpu_log_items; item->mask != 0; item++) {
4992 printf("%-10s %s\n", item->name, item->help);
4993 }
4994 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00004995 }
4996 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00004997 }
bellardcd6f1162004-05-13 22:02:20 +00004998 break;
bellard67b915a2004-03-31 23:37:16 +00004999#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00005000 case QEMU_OPTION_s:
5001 use_gdbstub = 1;
5002 break;
5003 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00005004 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005005 break;
bellard67b915a2004-03-31 23:37:16 +00005006#endif
bellardcd6f1162004-05-13 22:02:20 +00005007 case QEMU_OPTION_L:
5008 bios_dir = optarg;
5009 break;
j_mayer1192dad2007-10-05 13:08:35 +00005010 case QEMU_OPTION_bios:
5011 bios_name = optarg;
5012 break;
bellardcd6f1162004-05-13 22:02:20 +00005013 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005014 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005015 break;
bellard3d11d0e2004-12-12 16:56:30 +00005016 case QEMU_OPTION_k:
5017 keyboard_layout = optarg;
5018 break;
bellardee22c2f2004-06-03 12:49:50 +00005019 case QEMU_OPTION_localtime:
5020 rtc_utc = 0;
5021 break;
malc3893c122008-09-28 00:42:05 +00005022 case QEMU_OPTION_vga:
5023 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005024 break;
bellarde9b137c2004-06-21 16:46:10 +00005025 case QEMU_OPTION_g:
5026 {
5027 const char *p;
5028 int w, h, depth;
5029 p = optarg;
5030 w = strtol(p, (char **)&p, 10);
5031 if (w <= 0) {
5032 graphic_error:
5033 fprintf(stderr, "qemu: invalid resolution or depth\n");
5034 exit(1);
5035 }
5036 if (*p != 'x')
5037 goto graphic_error;
5038 p++;
5039 h = strtol(p, (char **)&p, 10);
5040 if (h <= 0)
5041 goto graphic_error;
5042 if (*p == 'x') {
5043 p++;
5044 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005045 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005046 depth != 24 && depth != 32)
5047 goto graphic_error;
5048 } else if (*p == '\0') {
5049 depth = graphic_depth;
5050 } else {
5051 goto graphic_error;
5052 }
ths3b46e622007-09-17 08:09:54 +00005053
bellarde9b137c2004-06-21 16:46:10 +00005054 graphic_width = w;
5055 graphic_height = h;
5056 graphic_depth = depth;
5057 }
5058 break;
ths20d8a3e2007-02-18 17:04:49 +00005059 case QEMU_OPTION_echr:
5060 {
5061 char *r;
5062 term_escape_char = strtol(optarg, &r, 0);
5063 if (r == optarg)
5064 printf("Bad argument to echr\n");
5065 break;
5066 }
bellard82c643f2004-07-14 17:28:13 +00005067 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005068 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005069 break;
5070 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005071 if (serial_device_index >= MAX_SERIAL_PORTS) {
5072 fprintf(stderr, "qemu: too many serial ports\n");
5073 exit(1);
5074 }
pbrookfd5f3932008-03-26 20:55:43 +00005075 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005076 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005077 break;
aliguori51ecf132009-01-15 20:06:40 +00005078 case QEMU_OPTION_virtiocon:
5079 if (virtio_console_index >= MAX_VIRTIO_CONSOLES) {
5080 fprintf(stderr, "qemu: too many virtio consoles\n");
5081 exit(1);
5082 }
5083 virtio_consoles[virtio_console_index] = optarg;
5084 virtio_console_index++;
5085 break;
bellard6508fe52005-01-15 12:02:56 +00005086 case QEMU_OPTION_parallel:
5087 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5088 fprintf(stderr, "qemu: too many parallel ports\n");
5089 exit(1);
5090 }
pbrookfd5f3932008-03-26 20:55:43 +00005091 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005092 parallel_device_index++;
5093 break;
bellardd63d3072004-10-03 13:29:03 +00005094 case QEMU_OPTION_loadvm:
5095 loadvm = optarg;
5096 break;
5097 case QEMU_OPTION_full_screen:
5098 full_screen = 1;
5099 break;
ths667acca2006-12-11 02:08:05 +00005100#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005101 case QEMU_OPTION_no_frame:
5102 no_frame = 1;
5103 break;
ths3780e192007-06-21 21:08:02 +00005104 case QEMU_OPTION_alt_grab:
5105 alt_grab = 1;
5106 break;
ths667acca2006-12-11 02:08:05 +00005107 case QEMU_OPTION_no_quit:
5108 no_quit = 1;
5109 break;
aliguori7d957bd2009-01-15 22:14:11 +00005110 case QEMU_OPTION_sdl:
5111 sdl = 1;
5112 break;
ths667acca2006-12-11 02:08:05 +00005113#endif
bellardf7cce892004-12-08 22:21:25 +00005114 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005115 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005116 break;
bellarda09db212005-04-30 16:10:35 +00005117#ifdef TARGET_I386
5118 case QEMU_OPTION_win2k_hack:
5119 win2k_install_hack = 1;
5120 break;
aliguori73822ec2009-01-15 20:11:34 +00005121 case QEMU_OPTION_rtc_td_hack:
5122 rtc_td_hack = 1;
5123 break;
bellarda09db212005-04-30 16:10:35 +00005124#endif
bellardd993e022005-02-10 22:00:06 +00005125#ifdef USE_KQEMU
5126 case QEMU_OPTION_no_kqemu:
5127 kqemu_allowed = 0;
5128 break;
bellard89bfc102006-02-08 22:46:31 +00005129 case QEMU_OPTION_kernel_kqemu:
5130 kqemu_allowed = 2;
5131 break;
bellardd993e022005-02-10 22:00:06 +00005132#endif
aliguori7ba1e612008-11-05 16:04:33 +00005133#ifdef CONFIG_KVM
5134 case QEMU_OPTION_enable_kvm:
5135 kvm_allowed = 1;
5136#ifdef USE_KQEMU
5137 kqemu_allowed = 0;
5138#endif
5139 break;
5140#endif
bellardbb36d472005-11-05 14:22:28 +00005141 case QEMU_OPTION_usb:
5142 usb_enabled = 1;
5143 break;
bellarda594cfb2005-11-06 16:13:29 +00005144 case QEMU_OPTION_usbdevice:
5145 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005146 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005147 fprintf(stderr, "Too many USB devices\n");
5148 exit(1);
5149 }
pbrookfd5f3932008-03-26 20:55:43 +00005150 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005151 usb_devices_index++;
5152 break;
bellard6a00d602005-11-21 23:25:50 +00005153 case QEMU_OPTION_smp:
5154 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005155 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005156 fprintf(stderr, "Invalid number of CPUs\n");
5157 exit(1);
5158 }
5159 break;
bellard24236862006-04-30 21:28:36 +00005160 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005161 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005162 break;
bellard6515b202006-05-03 22:02:44 +00005163 case QEMU_OPTION_no_acpi:
5164 acpi_enabled = 0;
5165 break;
aliguori16b29ae2008-12-17 23:28:44 +00005166 case QEMU_OPTION_no_hpet:
5167 no_hpet = 1;
5168 break;
bellardd1beab82006-10-02 19:44:22 +00005169 case QEMU_OPTION_no_reboot:
5170 no_reboot = 1;
5171 break;
aurel32b2f76162008-04-11 21:35:52 +00005172 case QEMU_OPTION_no_shutdown:
5173 no_shutdown = 1;
5174 break;
balrog9467cd42007-05-01 01:34:14 +00005175 case QEMU_OPTION_show_cursor:
5176 cursor_hide = 0;
5177 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005178 case QEMU_OPTION_uuid:
5179 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5180 fprintf(stderr, "Fail to parse UUID string."
5181 " Wrong format.\n");
5182 exit(1);
5183 }
5184 break;
ths71e3ceb2006-12-22 02:11:31 +00005185 case QEMU_OPTION_daemonize:
5186 daemonize = 1;
5187 break;
ths9ae02552007-01-05 17:39:04 +00005188 case QEMU_OPTION_option_rom:
5189 if (nb_option_roms >= MAX_OPTION_ROMS) {
5190 fprintf(stderr, "Too many option ROMs\n");
5191 exit(1);
5192 }
5193 option_rom[nb_option_roms] = optarg;
5194 nb_option_roms++;
5195 break;
pbrook8e716212007-01-20 17:12:09 +00005196 case QEMU_OPTION_semihosting:
5197 semihosting_enabled = 1;
5198 break;
thsc35734b2007-03-19 15:17:08 +00005199 case QEMU_OPTION_name:
5200 qemu_name = optarg;
5201 break;
blueswir195efd112008-12-24 20:26:14 +00005202#if defined(TARGET_SPARC) || defined(TARGET_PPC)
blueswir166508602007-05-01 14:16:52 +00005203 case QEMU_OPTION_prom_env:
5204 if (nb_prom_envs >= MAX_PROM_ENVS) {
5205 fprintf(stderr, "Too many prom variables\n");
5206 exit(1);
5207 }
5208 prom_envs[nb_prom_envs] = optarg;
5209 nb_prom_envs++;
5210 break;
5211#endif
balrog2b8f2d42007-07-27 22:08:46 +00005212#ifdef TARGET_ARM
5213 case QEMU_OPTION_old_param:
5214 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005215 break;
balrog2b8f2d42007-07-27 22:08:46 +00005216#endif
thsf3dcfad2007-08-24 01:26:02 +00005217 case QEMU_OPTION_clock:
5218 configure_alarms(optarg);
5219 break;
bellard7e0af5d02007-11-07 16:24:33 +00005220 case QEMU_OPTION_startdate:
5221 {
5222 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005223 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005224 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005225 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005226 } else {
5227 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5228 &tm.tm_year,
5229 &tm.tm_mon,
5230 &tm.tm_mday,
5231 &tm.tm_hour,
5232 &tm.tm_min,
5233 &tm.tm_sec) == 6) {
5234 /* OK */
5235 } else if (sscanf(optarg, "%d-%d-%d",
5236 &tm.tm_year,
5237 &tm.tm_mon,
5238 &tm.tm_mday) == 3) {
5239 tm.tm_hour = 0;
5240 tm.tm_min = 0;
5241 tm.tm_sec = 0;
5242 } else {
5243 goto date_fail;
5244 }
5245 tm.tm_year -= 1900;
5246 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005247 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005248 if (rtc_start_date == -1) {
5249 date_fail:
5250 fprintf(stderr, "Invalid date format. Valid format are:\n"
5251 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5252 exit(1);
5253 }
balrogf6503052008-02-17 11:42:19 +00005254 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005255 }
5256 }
5257 break;
bellard26a5f132008-05-28 12:30:31 +00005258 case QEMU_OPTION_tb_size:
5259 tb_size = strtol(optarg, NULL, 0);
5260 if (tb_size < 0)
5261 tb_size = 0;
5262 break;
pbrook2e70f6e2008-06-29 01:03:05 +00005263 case QEMU_OPTION_icount:
5264 use_icount = 1;
5265 if (strcmp(optarg, "auto") == 0) {
5266 icount_time_shift = -1;
5267 } else {
5268 icount_time_shift = strtol(optarg, NULL, 0);
5269 }
5270 break;
aliguori5bb79102008-10-13 03:12:02 +00005271 case QEMU_OPTION_incoming:
5272 incoming = optarg;
5273 break;
bellardcd6f1162004-05-13 22:02:20 +00005274 }
bellard0824d6f2003-06-24 13:42:40 +00005275 }
5276 }
bellard330d0412003-07-26 18:11:40 +00005277
aliguori7ba1e612008-11-05 16:04:33 +00005278#if defined(CONFIG_KVM) && defined(USE_KQEMU)
5279 if (kvm_allowed && kqemu_allowed) {
5280 fprintf(stderr,
5281 "You can not enable both KVM and kqemu at the same time\n");
5282 exit(1);
5283 }
5284#endif
5285
balrog3d878ca2008-10-28 10:59:59 +00005286 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00005287 if (smp_cpus > machine->max_cpus) {
5288 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5289 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
5290 machine->max_cpus);
5291 exit(1);
5292 }
5293
aliguoribc0129d2008-08-01 15:12:34 +00005294 if (nographic) {
5295 if (serial_device_index == 0)
5296 serial_devices[0] = "stdio";
5297 if (parallel_device_index == 0)
5298 parallel_devices[0] = "null";
5299 if (strncmp(monitor_device, "vc", 2) == 0)
5300 monitor_device = "stdio";
aliguori9ede2fd2009-01-15 20:05:25 +00005301 if (virtio_console_index == 0)
5302 virtio_consoles[0] = "null";
aliguoribc0129d2008-08-01 15:12:34 +00005303 }
5304
ths71e3ceb2006-12-22 02:11:31 +00005305#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00005306 if (daemonize) {
5307 pid_t pid;
5308
5309 if (pipe(fds) == -1)
5310 exit(1);
5311
5312 pid = fork();
5313 if (pid > 0) {
5314 uint8_t status;
5315 ssize_t len;
5316
5317 close(fds[1]);
5318
5319 again:
ths93815bc2007-03-19 15:58:31 +00005320 len = read(fds[0], &status, 1);
5321 if (len == -1 && (errno == EINTR))
5322 goto again;
5323
5324 if (len != 1)
5325 exit(1);
5326 else if (status == 1) {
5327 fprintf(stderr, "Could not acquire pidfile\n");
5328 exit(1);
5329 } else
5330 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00005331 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00005332 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00005333
5334 setsid();
5335
5336 pid = fork();
5337 if (pid > 0)
5338 exit(0);
5339 else if (pid < 0)
5340 exit(1);
5341
5342 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00005343
5344 signal(SIGTSTP, SIG_IGN);
5345 signal(SIGTTOU, SIG_IGN);
5346 signal(SIGTTIN, SIG_IGN);
5347 }
5348#endif
5349
thsaa26bb22007-03-25 21:33:06 +00005350 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00005351 if (daemonize) {
5352 uint8_t status = 1;
5353 write(fds[1], &status, 1);
5354 } else
5355 fprintf(stderr, "Could not acquire pid file\n");
5356 exit(1);
5357 }
5358
bellardff3fbb32006-01-08 10:53:14 +00005359#ifdef USE_KQEMU
5360 if (smp_cpus > 1)
5361 kqemu_allowed = 0;
5362#endif
bellarda20dd502003-09-30 21:07:02 +00005363 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00005364 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00005365
j_mayer28c5af52007-11-11 01:50:45 +00005366 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00005367 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00005368 help(1);
bellard0824d6f2003-06-24 13:42:40 +00005369
thsf8d39c02008-07-03 10:01:15 +00005370 if (!linux_boot && *kernel_cmdline != '\0') {
5371 fprintf(stderr, "-append only allowed with -kernel option\n");
5372 exit(1);
5373 }
5374
5375 if (!linux_boot && initrd_filename != NULL) {
5376 fprintf(stderr, "-initrd only allowed with -kernel option\n");
5377 exit(1);
5378 }
5379
ths96d30e42007-01-07 20:42:14 +00005380 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00005381 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00005382 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00005383 }
bellardb118d612003-06-30 23:36:21 +00005384 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00005385
pbrook634fce92006-07-15 17:40:09 +00005386 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00005387 if (init_timer_alarm() < 0) {
5388 fprintf(stderr, "could not initialize alarm timer\n");
5389 exit(1);
5390 }
pbrook2e70f6e2008-06-29 01:03:05 +00005391 if (use_icount && icount_time_shift < 0) {
5392 use_icount = 2;
5393 /* 125MIPS seems a reasonable initial guess at the guest speed.
5394 It will be corrected fairly quickly anyway. */
5395 icount_time_shift = 3;
5396 init_icount_adjust();
5397 }
pbrook634fce92006-07-15 17:40:09 +00005398
bellardfd1dff42006-02-01 21:29:26 +00005399#ifdef _WIN32
5400 socket_init();
5401#endif
5402
bellard7c9d8e02005-11-15 22:16:05 +00005403 /* init network clients */
5404 if (nb_net_clients == 0) {
5405 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00005406 net_clients[nb_net_clients++] = "nic";
5407#ifdef CONFIG_SLIRP
5408 net_clients[nb_net_clients++] = "user";
5409#endif
bellardc20709a2004-04-21 23:27:19 +00005410 }
5411
bellard7c9d8e02005-11-15 22:16:05 +00005412 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00005413 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00005414 exit(1);
bellard702c6512004-04-02 21:21:32 +00005415 }
aliguori63a01ef2008-10-31 19:10:00 +00005416 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00005417
thseec85c22007-01-05 17:41:07 +00005418#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00005419 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00005420 if (net_boot != 0) {
5421 int netroms = 0;
5422 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00005423 const char *model = nd_table[i].model;
5424 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00005425 if (net_boot & (1 << i)) {
5426 if (model == NULL)
5427 model = "ne2k_pci";
5428 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5429 if (get_image_size(buf) > 0) {
5430 if (nb_option_roms >= MAX_OPTION_ROMS) {
5431 fprintf(stderr, "Too many option ROMs\n");
5432 exit(1);
5433 }
5434 option_rom[nb_option_roms] = strdup(buf);
5435 nb_option_roms++;
5436 netroms++;
5437 }
5438 }
thseec85c22007-01-05 17:41:07 +00005439 }
j_mayer28c5af52007-11-11 01:50:45 +00005440 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00005441 fprintf(stderr, "No valid PXE rom found for network device\n");
5442 exit(1);
5443 }
thseec85c22007-01-05 17:41:07 +00005444 }
5445#endif
5446
balrogdc72ac12008-11-09 00:04:26 +00005447 /* init the bluetooth world */
5448 for (i = 0; i < nb_bt_opts; i++)
5449 if (bt_parse(bt_opts[i]))
5450 exit(1);
5451
bellard0824d6f2003-06-24 13:42:40 +00005452 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00005453 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
5454
5455 if (machine->ram_require & RAMSIZE_FIXED) {
5456 if (ram_size > 0) {
5457 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00005458 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
5459 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00005460 exit(-1);
5461 }
5462
5463 phys_ram_size = ram_size;
5464 } else
5465 ram_size = phys_ram_size;
5466 } else {
aurel324fc5d072008-04-27 21:39:40 +00005467 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00005468 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
5469
5470 phys_ram_size += ram_size;
5471 }
ths9ae02552007-01-05 17:39:04 +00005472
bellardd993e022005-02-10 22:00:06 +00005473 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00005474 if (!phys_ram_base) {
5475 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00005476 exit(1);
5477 }
5478
bellard26a5f132008-05-28 12:30:31 +00005479 /* init the dynamic translator */
5480 cpu_exec_init_all(tb_size * 1024 * 1024);
5481
bellard5905b2e2004-08-01 21:53:26 +00005482 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00005483
5484 /* we always create the cdrom drive, even if no disk is there */
5485
5486 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005487 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00005488
balrog9d413d12007-12-04 00:10:34 +00005489 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00005490
5491 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005492 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00005493
balrog9d413d12007-12-04 00:10:34 +00005494 /* we always create one sd slot, even if no card is in it */
5495
5496 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00005497 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00005498
ths96d30e42007-01-07 20:42:14 +00005499 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00005500
thse4bcb142007-12-02 04:51:10 +00005501 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00005502 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00005503 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00005504
bellardc88676f2006-08-06 13:36:11 +00005505 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00005506 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00005507
aliguori3023f332009-01-16 19:04:14 +00005508#ifndef _WIN32
5509 /* must be after terminal init, SDL library changes signal handlers */
5510 termsig_setup();
5511#endif
5512
5513 /* Maintain compatibility with multiple stdio monitors */
5514 if (!strcmp(monitor_device,"stdio")) {
5515 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
5516 const char *devname = serial_devices[i];
5517 if (devname && !strcmp(devname,"mon:stdio")) {
5518 monitor_device = NULL;
5519 break;
5520 } else if (devname && !strcmp(devname,"stdio")) {
5521 monitor_device = NULL;
5522 serial_devices[i] = "mon:stdio";
5523 break;
5524 }
5525 }
5526 }
5527
5528 if (kvm_enabled()) {
5529 int ret;
5530
5531 ret = kvm_init(smp_cpus);
5532 if (ret < 0) {
5533 fprintf(stderr, "failed to initialize KVM\n");
5534 exit(1);
5535 }
5536 }
5537
aliguori4c621802009-01-16 21:48:20 +00005538 if (monitor_device) {
aurel32ceecf1d2009-01-18 14:08:04 +00005539 monitor_hd = qemu_chr_open("monitor", monitor_device, NULL);
aliguori4c621802009-01-16 21:48:20 +00005540 if (!monitor_hd) {
5541 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
5542 exit(1);
5543 }
5544 }
5545
aliguori2796dae2009-01-16 20:23:27 +00005546 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
5547 const char *devname = serial_devices[i];
5548 if (devname && strcmp(devname, "none")) {
5549 char label[32];
5550 snprintf(label, sizeof(label), "serial%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005551 serial_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005552 if (!serial_hds[i]) {
5553 fprintf(stderr, "qemu: could not open serial device '%s'\n",
5554 devname);
5555 exit(1);
5556 }
5557 }
5558 }
5559
5560 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
5561 const char *devname = parallel_devices[i];
5562 if (devname && strcmp(devname, "none")) {
5563 char label[32];
5564 snprintf(label, sizeof(label), "parallel%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005565 parallel_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005566 if (!parallel_hds[i]) {
5567 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
5568 devname);
5569 exit(1);
5570 }
5571 }
5572 }
5573
5574 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5575 const char *devname = virtio_consoles[i];
5576 if (devname && strcmp(devname, "none")) {
5577 char label[32];
5578 snprintf(label, sizeof(label), "virtcon%d", i);
aurel32ceecf1d2009-01-18 14:08:04 +00005579 virtcon_hds[i] = qemu_chr_open(label, devname, NULL);
aliguori2796dae2009-01-16 20:23:27 +00005580 if (!virtcon_hds[i]) {
5581 fprintf(stderr, "qemu: could not open virtio console '%s'\n",
5582 devname);
5583 exit(1);
5584 }
5585 }
5586 }
5587
aliguori3023f332009-01-16 19:04:14 +00005588 machine->init(ram_size, vga_ram_size, boot_devices,
5589 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
5590
aliguori6f338c32009-02-11 15:21:54 +00005591 current_machine = machine;
5592
aliguori3023f332009-01-16 19:04:14 +00005593 /* Set KVM's vcpu state to qemu's initial CPUState. */
5594 if (kvm_enabled()) {
5595 int ret;
5596
5597 ret = kvm_sync_vcpus();
5598 if (ret < 0) {
5599 fprintf(stderr, "failed to initialize vcpus\n");
5600 exit(1);
5601 }
5602 }
5603
5604 /* init USB devices */
5605 if (usb_enabled) {
5606 for(i = 0; i < usb_devices_index; i++) {
5607 if (usb_device_add(usb_devices[i]) < 0) {
5608 fprintf(stderr, "Warning: could not add USB device %s\n",
5609 usb_devices[i]);
5610 }
5611 }
5612 }
5613
aliguori8f391ab2009-01-19 16:34:10 +00005614 if (!display_state)
5615 dumb_display_init();
aliguori3023f332009-01-16 19:04:14 +00005616 /* just use the first displaystate for the moment */
5617 ds = display_state;
bellard313aa562003-08-10 21:52:11 +00005618 /* terminal init */
bellarda20dd502003-09-30 21:07:02 +00005619 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00005620 if (curses) {
5621 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
5622 exit(1);
5623 }
aliguori7d957bd2009-01-15 22:14:11 +00005624 } else {
balrog4d3b6f62008-02-10 16:33:14 +00005625#if defined(CONFIG_CURSES)
aliguori7d957bd2009-01-15 22:14:11 +00005626 if (curses) {
5627 /* At the moment curses cannot be used with other displays */
5628 curses_display_init(ds, full_screen);
5629 } else
balrog4d3b6f62008-02-10 16:33:14 +00005630#endif
aliguori7d957bd2009-01-15 22:14:11 +00005631 {
5632 if (vnc_display != NULL) {
5633 vnc_display_init(ds);
5634 if (vnc_display_open(ds, vnc_display) < 0)
5635 exit(1);
5636 }
bellard5b0753e2005-03-01 21:37:28 +00005637#if defined(CONFIG_SDL)
aliguorid268de02009-01-22 16:18:33 +00005638 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005639 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00005640#elif defined(CONFIG_COCOA)
aliguorid268de02009-01-22 16:18:33 +00005641 if (sdl || !vnc_display)
aliguori7d957bd2009-01-15 22:14:11 +00005642 cocoa_display_init(ds, full_screen);
bellard313aa562003-08-10 21:52:11 +00005643#endif
aliguori7d957bd2009-01-15 22:14:11 +00005644 }
bellard313aa562003-08-10 21:52:11 +00005645 }
aliguori7d957bd2009-01-15 22:14:11 +00005646 dpy_resize(ds);
aliguori5b08fc12008-08-21 20:08:03 +00005647
aliguori3023f332009-01-16 19:04:14 +00005648 dcl = ds->listeners;
5649 while (dcl != NULL) {
5650 if (dcl->dpy_refresh != NULL) {
5651 ds->gui_timer = qemu_new_timer(rt_clock, gui_update, ds);
5652 qemu_mod_timer(ds->gui_timer, qemu_get_clock(rt_clock));
ths20d8a3e2007-02-18 17:04:49 +00005653 }
aliguori3023f332009-01-16 19:04:14 +00005654 dcl = dcl->next;
bellard82c643f2004-07-14 17:28:13 +00005655 }
aliguori3023f332009-01-16 19:04:14 +00005656
blueswir19043b622009-01-21 19:28:13 +00005657 if (nographic || (vnc_display && !sdl)) {
5658 nographic_timer = qemu_new_timer(rt_clock, nographic_update, NULL);
5659 qemu_mod_timer(nographic_timer, qemu_get_clock(rt_clock));
5660 }
5661
aliguori2796dae2009-01-16 20:23:27 +00005662 text_consoles_set_display(display_state);
5663
aliguori4c621802009-01-16 21:48:20 +00005664 if (monitor_device && monitor_hd)
ths20d8a3e2007-02-18 17:04:49 +00005665 monitor_init(monitor_hd, !nographic);
bellard82c643f2004-07-14 17:28:13 +00005666
bellard8d11df92004-08-24 21:13:40 +00005667 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005668 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005669 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005670 char label[32];
5671 snprintf(label, sizeof(label), "serial%d", i);
thsaf3a9032007-07-11 23:14:59 +00005672 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005673 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00005674 }
bellard82c643f2004-07-14 17:28:13 +00005675 }
bellard82c643f2004-07-14 17:28:13 +00005676
bellard6508fe52005-01-15 12:02:56 +00005677 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00005678 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00005679 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00005680 char label[32];
5681 snprintf(label, sizeof(label), "parallel%d", i);
thsaf3a9032007-07-11 23:14:59 +00005682 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00005683 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00005684 }
5685 }
5686
aliguori9ede2fd2009-01-15 20:05:25 +00005687 for(i = 0; i < MAX_VIRTIO_CONSOLES; i++) {
5688 const char *devname = virtio_consoles[i];
aliguori2796dae2009-01-16 20:23:27 +00005689 if (virtcon_hds[i] && devname) {
aliguori9ede2fd2009-01-15 20:05:25 +00005690 char label[32];
5691 snprintf(label, sizeof(label), "virtcon%d", i);
aliguori9ede2fd2009-01-15 20:05:25 +00005692 if (strstart(devname, "vc", 0))
5693 qemu_chr_printf(virtcon_hds[i], "virtio console%d\r\n", i);
5694 }
5695 }
5696
bellard67b915a2004-03-31 23:37:16 +00005697#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005698 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00005699 /* XXX: use standard host:port notation and modify options
5700 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00005701 if (gdbserver_start(gdbstub_port) < 0) {
5702 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00005703 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00005704 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00005705 }
balrog45669e02007-07-02 13:20:17 +00005706 }
bellard67b915a2004-03-31 23:37:16 +00005707#endif
balrog45669e02007-07-02 13:20:17 +00005708
bellardd63d3072004-10-03 13:29:03 +00005709 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00005710 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00005711
aliguori5bb79102008-10-13 03:12:02 +00005712 if (incoming) {
5713 autostart = 0; /* fixme how to deal with -daemonize */
5714 qemu_start_incoming_migration(incoming);
5715 }
5716
bellard67b915a2004-03-31 23:37:16 +00005717 {
bellard5905b2e2004-08-01 21:53:26 +00005718 /* XXX: simplify init */
aliguori83ab7952008-08-19 14:44:22 +00005719 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00005720 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00005721 vm_start();
5722 }
bellard0824d6f2003-06-24 13:42:40 +00005723 }
thsffd843b2006-12-21 19:46:43 +00005724
ths71e3ceb2006-12-22 02:11:31 +00005725 if (daemonize) {
5726 uint8_t status = 0;
5727 ssize_t len;
5728 int fd;
5729
5730 again1:
5731 len = write(fds[1], &status, 1);
5732 if (len == -1 && (errno == EINTR))
5733 goto again1;
5734
5735 if (len != 1)
5736 exit(1);
5737
aliguoribd54b862008-07-23 00:58:33 +00005738 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00005739 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00005740 if (fd == -1)
5741 exit(1);
5742
5743 dup2(fd, 0);
5744 dup2(fd, 1);
5745 dup2(fd, 2);
5746
5747 close(fd);
5748 }
5749
bellard8a7ddc32004-03-31 19:00:16 +00005750 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00005751 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00005752 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00005753
bellard0824d6f2003-06-24 13:42:40 +00005754 return 0;
5755}