blob: 04485fe65feb8ce39dc86ceb1b51140a6112d79e [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"
39#include "block.h"
40#include "audio/audio.h"
aliguori5bb79102008-10-13 03:12:02 +000041#include "migration.h"
aliguori7ba1e612008-11-05 16:04:33 +000042#include "kvm.h"
bellard67b915a2004-03-31 23:37:16 +000043
bellard0824d6f2003-06-24 13:42:40 +000044#include <unistd.h>
bellard0824d6f2003-06-24 13:42:40 +000045#include <fcntl.h>
46#include <signal.h>
47#include <time.h>
bellard0824d6f2003-06-24 13:42:40 +000048#include <errno.h>
bellard67b915a2004-03-31 23:37:16 +000049#include <sys/time.h>
bellardc88676f2006-08-06 13:36:11 +000050#include <zlib.h>
bellard67b915a2004-03-31 23:37:16 +000051
52#ifndef _WIN32
53#include <sys/times.h>
bellardf1510b22003-06-25 00:07:40 +000054#include <sys/wait.h>
bellard67b915a2004-03-31 23:37:16 +000055#include <termios.h>
bellard67b915a2004-03-31 23:37:16 +000056#include <sys/mman.h>
bellardf1510b22003-06-25 00:07:40 +000057#include <sys/ioctl.h>
blueswir124646c72008-11-07 16:55:48 +000058#include <sys/resource.h>
bellardf1510b22003-06-25 00:07:40 +000059#include <sys/socket.h>
bellardc94c8d62004-09-13 21:37:34 +000060#include <netinet/in.h>
blueswir124646c72008-11-07 16:55:48 +000061#include <net/if.h>
62#if defined(__NetBSD__)
63#include <net/if_tap.h>
64#endif
65#ifdef __linux__
66#include <linux/if_tun.h>
67#endif
68#include <arpa/inet.h>
bellard9d728e82004-09-05 23:09:03 +000069#include <dirent.h>
bellard7c9d8e02005-11-15 22:16:05 +000070#include <netdb.h>
thscb4b9762007-09-13 12:39:35 +000071#include <sys/select.h>
bellard7d3505c2004-05-12 19:32:15 +000072#ifdef _BSD
73#include <sys/stat.h>
blueswir124646c72008-11-07 16:55:48 +000074#ifdef __FreeBSD__
bellard7d3505c2004-05-12 19:32:15 +000075#include <libutil.h>
blueswir124646c72008-11-07 16:55:48 +000076#else
77#include <util.h>
blueswir1128ab2f2008-08-15 18:33:42 +000078#endif
ths5c40d2b2007-06-23 16:03:36 +000079#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
80#include <freebsd/stdlib.h>
bellard7d3505c2004-05-12 19:32:15 +000081#else
blueswir1223f0d72008-09-30 18:12:18 +000082#ifdef __linux__
bellard7d3505c2004-05-12 19:32:15 +000083#include <pty.h>
84#include <malloc.h>
bellardfd872592004-05-12 19:11:15 +000085#include <linux/rtc.h>
thsbd494f42007-09-16 20:03:23 +000086
87/* For the benefit of older linux systems which don't supply it,
88 we use a local copy of hpet.h. */
89/* #include <linux/hpet.h> */
90#include "hpet.h"
91
bellarde57a8c02005-11-10 23:58:52 +000092#include <linux/ppdev.h>
ths5867c882007-02-17 23:44:43 +000093#include <linux/parport.h>
blueswir1223f0d72008-09-30 18:12:18 +000094#endif
95#ifdef __sun__
thsd5d10bc2007-02-17 22:54:49 +000096#include <sys/stat.h>
97#include <sys/ethernet.h>
98#include <sys/sockio.h>
thsd5d10bc2007-02-17 22:54:49 +000099#include <netinet/arp.h>
100#include <netinet/in.h>
101#include <netinet/in_systm.h>
102#include <netinet/ip.h>
103#include <netinet/ip_icmp.h> // must come after ip.h
104#include <netinet/udp.h>
105#include <netinet/tcp.h>
106#include <net/if.h>
107#include <syslog.h>
108#include <stropts.h>
bellard67b915a2004-03-31 23:37:16 +0000109#endif
bellard7d3505c2004-05-12 19:32:15 +0000110#endif
bellardec530c82006-04-25 22:36:06 +0000111#endif
bellard67b915a2004-03-31 23:37:16 +0000112
aliguori03ff3ca2008-09-15 15:51:35 +0000113#include "qemu_socket.h"
114
bellardc20709a2004-04-21 23:27:19 +0000115#if defined(CONFIG_SLIRP)
116#include "libslirp.h"
117#endif
118
blueswir19892fbf2008-08-24 10:34:20 +0000119#if defined(__OpenBSD__)
120#include <util.h>
121#endif
122
ths8a16d272008-07-19 09:56:24 +0000123#if defined(CONFIG_VDE)
124#include <libvdeplug.h>
125#endif
126
bellard67b915a2004-03-31 23:37:16 +0000127#ifdef _WIN32
bellard7d3505c2004-05-12 19:32:15 +0000128#include <malloc.h>
bellard67b915a2004-03-31 23:37:16 +0000129#include <sys/timeb.h>
ths4fddf622007-12-17 04:42:29 +0000130#include <mmsystem.h>
bellard67b915a2004-03-31 23:37:16 +0000131#define getopt_long_only getopt_long
132#define memalign(align, size) malloc(size)
133#endif
134
bellard73332e52004-04-04 20:22:28 +0000135#ifdef CONFIG_SDL
bellard96bcd4f2004-07-10 16:26:15 +0000136#ifdef __APPLE__
bellard83fb7ad2004-07-05 21:25:26 +0000137#include <SDL/SDL.h>
bellard96bcd4f2004-07-10 16:26:15 +0000138#endif
bellard73332e52004-04-04 20:22:28 +0000139#endif /* CONFIG_SDL */
bellard0824d6f2003-06-24 13:42:40 +0000140
bellard5b0753e2005-03-01 21:37:28 +0000141#ifdef CONFIG_COCOA
142#undef main
143#define main qemu_main
144#endif /* CONFIG_COCOA */
145
bellard0824d6f2003-06-24 13:42:40 +0000146#include "disas.h"
bellardfc01f7e2003-06-30 10:03:06 +0000147
bellard8a7ddc32004-03-31 19:00:16 +0000148#include "exec-all.h"
bellard0824d6f2003-06-24 13:42:40 +0000149
bellard5a671352003-10-01 00:13:48 +0000150#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
thsb46a8902007-10-21 23:20:45 +0000151#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
pbrooka14d6c82006-12-23 15:37:33 +0000152#ifdef __sun__
153#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
154#else
155#define SMBD_COMMAND "/usr/sbin/smbd"
156#endif
bellardf1510b22003-06-25 00:07:40 +0000157
bellard0824d6f2003-06-24 13:42:40 +0000158//#define DEBUG_UNUSED_IOPORT
bellardfd872592004-05-12 19:11:15 +0000159//#define DEBUG_IOPORT
blueswir19dc63a12008-10-04 07:25:46 +0000160//#define DEBUG_NET
161//#define DEBUG_SLIRP
bellard330d0412003-07-26 18:11:40 +0000162
bellard77d4bc32004-05-26 22:13:53 +0000163#ifdef TARGET_PPC
164#define DEFAULT_RAM_SIZE 144
165#else
bellard1bfe8562004-07-08 21:17:50 +0000166#define DEFAULT_RAM_SIZE 128
bellard77d4bc32004-05-26 22:13:53 +0000167#endif
bellard313aa562003-08-10 21:52:11 +0000168
pbrook0d92ed32006-05-21 16:30:15 +0000169/* Max number of USB devices that can be specified on the commandline. */
170#define MAX_USB_CMDLINE 8
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;
bellardfaea38e2006-08-05 21:31:00 +0000184/* point to the block driver where the snapshots are managed */
blueswir1dbed7e42008-10-01 19:38:09 +0000185static BlockDriverState *bs_snapshots;
186static int vga_ram_size;
malccb5a7aa2008-09-28 00:42:12 +0000187enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
aliguori0e82f342008-10-31 18:44:40 +0000188DisplayState display_state;
bellarda20dd502003-09-30 21:07:02 +0000189int nographic;
blueswir1dbed7e42008-10-01 19:38:09 +0000190static int curses;
bellard3d11d0e2004-12-12 16:56:30 +0000191const char* keyboard_layout = NULL;
bellard313aa562003-08-10 21:52:11 +0000192int64_t ticks_per_sec;
aurel3200f82b82008-04-27 21:12:55 +0000193ram_addr_t ram_size;
bellardc4b1fcc2004-03-14 21:44:30 +0000194int nb_nics;
bellard7c9d8e02005-11-15 22:16:05 +0000195NICInfo nd_table[MAX_NICS];
bellard8a7ddc32004-03-31 19:00:16 +0000196int vm_running;
balrogf6503052008-02-17 11:42:19 +0000197static int rtc_utc = 1;
198static int rtc_date_offset = -1; /* -1 means no change */
bellard1bfe8562004-07-08 21:17:50 +0000199int cirrus_vga_enabled = 1;
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;
211static int no_frame = 0;
ths667acca2006-12-11 02:08:05 +0000212int no_quit = 0;
bellard8d11df92004-08-24 21:13:40 +0000213CharDriverState *serial_hds[MAX_SERIAL_PORTS];
bellard6508fe52005-01-15 12:02:56 +0000214CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
bellarda09db212005-04-30 16:10:35 +0000215#ifdef TARGET_I386
216int win2k_install_hack = 0;
217#endif
bellardbb36d472005-11-05 14:22:28 +0000218int usb_enabled = 0;
bellard6a00d602005-11-21 23:25:50 +0000219int smp_cpus = 1;
ths73fc9742006-12-22 02:09:07 +0000220const char *vnc_display;
bellard6515b202006-05-03 22:02:44 +0000221int acpi_enabled = 1;
bellard52ca8d62006-06-14 16:03:05 +0000222int fd_bootchk = 1;
bellardd1beab82006-10-02 19:44:22 +0000223int no_reboot = 0;
aurel32b2f76162008-04-11 21:35:52 +0000224int no_shutdown = 0;
balrog9467cd42007-05-01 01:34:14 +0000225int cursor_hide = 1;
balroga171fe32007-04-30 01:48:07 +0000226int graphic_rotate = 0;
ths71e3ceb2006-12-22 02:11:31 +0000227int daemonize = 0;
ths9ae02552007-01-05 17:39:04 +0000228const char *option_rom[MAX_OPTION_ROMS];
229int nb_option_roms;
pbrook8e716212007-01-20 17:12:09 +0000230int semihosting_enabled = 0;
balrog2b8f2d42007-07-27 22:08:46 +0000231#ifdef TARGET_ARM
232int old_param = 0;
233#endif
thsc35734b2007-03-19 15:17:08 +0000234const char *qemu_name;
ths3780e192007-06-21 21:08:02 +0000235int alt_grab = 0;
blueswir166508602007-05-01 14:16:52 +0000236#ifdef TARGET_SPARC
237unsigned int nb_prom_envs = 0;
238const char *prom_envs[MAX_PROM_ENVS];
239#endif
blueswir1dbed7e42008-10-01 19:38:09 +0000240static int nb_drives_opt;
241static struct drive_opt {
balrog609497a2008-01-14 02:56:53 +0000242 const char *file;
243 char opt[1024];
244} drives_opt[MAX_DRIVES];
bellard0824d6f2003-06-24 13:42:40 +0000245
balrogee5605e2007-12-03 03:01:40 +0000246static CPUState *cur_cpu;
247static CPUState *next_cpu;
balrog76ea08f2007-12-16 11:48:54 +0000248static int event_pending = 1;
thsbf20dc02008-06-30 17:22:19 +0000249/* Conversion factor from emulated instructions to virtual clock ticks. */
pbrook2e70f6e2008-06-29 01:03:05 +0000250static int icount_time_shift;
thsbf20dc02008-06-30 17:22:19 +0000251/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
pbrook2e70f6e2008-06-29 01:03:05 +0000252#define MAX_ICOUNT_SHIFT 10
253/* Compensate for varying guest execution speed. */
254static int64_t qemu_icount_bias;
blueswir1dbed7e42008-10-01 19:38:09 +0000255static QEMUTimer *icount_rt_timer;
256static QEMUTimer *icount_vm_timer;
balrogee5605e2007-12-03 03:01:40 +0000257
blueswir18fcb1b92008-09-18 18:29:08 +0000258uint8_t qemu_uuid[16];
259
bellard0824d6f2003-06-24 13:42:40 +0000260/***********************************************************/
bellard26aa7d72004-04-28 22:26:05 +0000261/* x86 ISA bus support */
262
263target_phys_addr_t isa_mem_base = 0;
bellard3de388f2005-07-02 18:11:44 +0000264PicState2 *isa_pic;
bellard0824d6f2003-06-24 13:42:40 +0000265
aliguori477e3ed2008-07-23 15:19:59 +0000266static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
267static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
268
269static uint32_t ioport_read(int index, uint32_t address)
270{
271 static IOPortReadFunc *default_func[3] = {
272 default_ioport_readb,
273 default_ioport_readw,
274 default_ioport_readl
275 };
276 IOPortReadFunc *func = ioport_read_table[index][address];
277 if (!func)
278 func = default_func[index];
279 return func(ioport_opaque[address], address);
280}
281
282static void ioport_write(int index, uint32_t address, uint32_t data)
283{
284 static IOPortWriteFunc *default_func[3] = {
285 default_ioport_writeb,
286 default_ioport_writew,
287 default_ioport_writel
288 };
289 IOPortWriteFunc *func = ioport_write_table[index][address];
290 if (!func)
291 func = default_func[index];
292 func(ioport_opaque[address], address, data);
293}
294
pbrook9596ebb2007-11-18 01:44:38 +0000295static uint32_t default_ioport_readb(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000296{
297#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000298 fprintf(stderr, "unused inb: port=0x%04x\n", address);
bellard0824d6f2003-06-24 13:42:40 +0000299#endif
bellardfc01f7e2003-06-30 10:03:06 +0000300 return 0xff;
bellard0824d6f2003-06-24 13:42:40 +0000301}
302
pbrook9596ebb2007-11-18 01:44:38 +0000303static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000304{
305#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000306 fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
bellard0824d6f2003-06-24 13:42:40 +0000307#endif
308}
309
310/* default is to make two byte accesses */
pbrook9596ebb2007-11-18 01:44:38 +0000311static uint32_t default_ioport_readw(void *opaque, uint32_t address)
bellard0824d6f2003-06-24 13:42:40 +0000312{
313 uint32_t data;
aliguori477e3ed2008-07-23 15:19:59 +0000314 data = ioport_read(0, address);
bellarddb45c292004-05-12 19:50:26 +0000315 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000316 data |= ioport_read(0, address) << 8;
bellard0824d6f2003-06-24 13:42:40 +0000317 return data;
318}
319
pbrook9596ebb2007-11-18 01:44:38 +0000320static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
bellard0824d6f2003-06-24 13:42:40 +0000321{
aliguori477e3ed2008-07-23 15:19:59 +0000322 ioport_write(0, address, data & 0xff);
bellarddb45c292004-05-12 19:50:26 +0000323 address = (address + 1) & (MAX_IOPORTS - 1);
aliguori477e3ed2008-07-23 15:19:59 +0000324 ioport_write(0, address, (data >> 8) & 0xff);
bellardfc01f7e2003-06-30 10:03:06 +0000325}
326
pbrook9596ebb2007-11-18 01:44:38 +0000327static uint32_t default_ioport_readl(void *opaque, uint32_t address)
bellardfc01f7e2003-06-30 10:03:06 +0000328{
329#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000330 fprintf(stderr, "unused inl: port=0x%04x\n", address);
bellardfc01f7e2003-06-30 10:03:06 +0000331#endif
332 return 0xffffffff;
333}
334
pbrook9596ebb2007-11-18 01:44:38 +0000335static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
bellardfc01f7e2003-06-30 10:03:06 +0000336{
337#ifdef DEBUG_UNUSED_IOPORT
ths1196be32007-03-17 15:17:58 +0000338 fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
bellardfc01f7e2003-06-30 10:03:06 +0000339#endif
bellard0824d6f2003-06-24 13:42:40 +0000340}
341
bellardfc01f7e2003-06-30 10:03:06 +0000342/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000343int register_ioport_read(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000344 IOPortReadFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000345{
bellardfc01f7e2003-06-30 10:03:06 +0000346 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000347
bellardc4b1fcc2004-03-14 21:44:30 +0000348 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000349 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000350 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000351 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000352 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000353 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000354 } else {
balrog88fdf562008-04-26 21:11:22 +0000355 hw_error("register_ioport_read: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000356 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000357 }
358 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000359 ioport_read_table[bsize][i] = func;
bellardc4b1fcc2004-03-14 21:44:30 +0000360 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
balrog88fdf562008-04-26 21:11:22 +0000361 hw_error("register_ioport_read: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000362 ioport_opaque[i] = opaque;
363 }
bellard0824d6f2003-06-24 13:42:40 +0000364 return 0;
365}
366
bellardfc01f7e2003-06-30 10:03:06 +0000367/* size is the word size in byte */
ths5fafdf22007-09-16 21:08:06 +0000368int register_ioport_write(int start, int length, int size,
bellardc4b1fcc2004-03-14 21:44:30 +0000369 IOPortWriteFunc *func, void *opaque)
bellard0824d6f2003-06-24 13:42:40 +0000370{
bellardfc01f7e2003-06-30 10:03:06 +0000371 int i, bsize;
bellard0824d6f2003-06-24 13:42:40 +0000372
bellardc4b1fcc2004-03-14 21:44:30 +0000373 if (size == 1) {
bellardfc01f7e2003-06-30 10:03:06 +0000374 bsize = 0;
bellardc4b1fcc2004-03-14 21:44:30 +0000375 } else if (size == 2) {
bellardfc01f7e2003-06-30 10:03:06 +0000376 bsize = 1;
bellardc4b1fcc2004-03-14 21:44:30 +0000377 } else if (size == 4) {
bellardfc01f7e2003-06-30 10:03:06 +0000378 bsize = 2;
bellardc4b1fcc2004-03-14 21:44:30 +0000379 } else {
balrog88fdf562008-04-26 21:11:22 +0000380 hw_error("register_ioport_write: invalid size");
bellardfc01f7e2003-06-30 10:03:06 +0000381 return -1;
bellardc4b1fcc2004-03-14 21:44:30 +0000382 }
383 for(i = start; i < start + length; i += size) {
bellardfc01f7e2003-06-30 10:03:06 +0000384 ioport_write_table[bsize][i] = func;
balrog88fdf562008-04-26 21:11:22 +0000385 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
386 hw_error("register_ioport_write: invalid opaque");
bellardc4b1fcc2004-03-14 21:44:30 +0000387 ioport_opaque[i] = opaque;
388 }
bellardf1510b22003-06-25 00:07:40 +0000389 return 0;
390}
391
bellard69b91032004-05-18 23:05:28 +0000392void isa_unassign_ioport(int start, int length)
393{
394 int i;
395
396 for(i = start; i < start + length; i++) {
397 ioport_read_table[0][i] = default_ioport_readb;
398 ioport_read_table[1][i] = default_ioport_readw;
399 ioport_read_table[2][i] = default_ioport_readl;
400
401 ioport_write_table[0][i] = default_ioport_writeb;
402 ioport_write_table[1][i] = default_ioport_writew;
403 ioport_write_table[2][i] = default_ioport_writel;
404 }
405}
406
bellard20f32282005-01-03 23:36:21 +0000407/***********************************************************/
408
bellardc45886d2004-01-05 00:02:06 +0000409void cpu_outb(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000410{
bellardfd872592004-05-12 19:11:15 +0000411#ifdef DEBUG_IOPORT
412 if (loglevel & CPU_LOG_IOPORT)
413 fprintf(logfile, "outb: %04x %02x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000414#endif
aliguori477e3ed2008-07-23 15:19:59 +0000415 ioport_write(0, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000416#ifdef USE_KQEMU
417 if (env)
418 env->last_io_time = cpu_get_time_fast();
419#endif
bellard0824d6f2003-06-24 13:42:40 +0000420}
421
bellardc45886d2004-01-05 00:02:06 +0000422void cpu_outw(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000423{
bellardfd872592004-05-12 19:11:15 +0000424#ifdef DEBUG_IOPORT
425 if (loglevel & CPU_LOG_IOPORT)
426 fprintf(logfile, "outw: %04x %04x\n", addr, val);
ths3b46e622007-09-17 08:09:54 +0000427#endif
aliguori477e3ed2008-07-23 15:19:59 +0000428 ioport_write(1, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000429#ifdef USE_KQEMU
430 if (env)
431 env->last_io_time = cpu_get_time_fast();
432#endif
bellard0824d6f2003-06-24 13:42:40 +0000433}
434
bellardc45886d2004-01-05 00:02:06 +0000435void cpu_outl(CPUState *env, int addr, int val)
bellard0824d6f2003-06-24 13:42:40 +0000436{
bellardfd872592004-05-12 19:11:15 +0000437#ifdef DEBUG_IOPORT
438 if (loglevel & CPU_LOG_IOPORT)
439 fprintf(logfile, "outl: %04x %08x\n", addr, val);
440#endif
aliguori477e3ed2008-07-23 15:19:59 +0000441 ioport_write(2, addr, val);
bellard89bfc102006-02-08 22:46:31 +0000442#ifdef USE_KQEMU
443 if (env)
444 env->last_io_time = cpu_get_time_fast();
445#endif
bellard0824d6f2003-06-24 13:42:40 +0000446}
447
bellardc45886d2004-01-05 00:02:06 +0000448int cpu_inb(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000449{
bellardfd872592004-05-12 19:11:15 +0000450 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000451 val = ioport_read(0, addr);
bellardfd872592004-05-12 19:11:15 +0000452#ifdef DEBUG_IOPORT
453 if (loglevel & CPU_LOG_IOPORT)
454 fprintf(logfile, "inb : %04x %02x\n", addr, val);
455#endif
bellard89bfc102006-02-08 22:46:31 +0000456#ifdef USE_KQEMU
457 if (env)
458 env->last_io_time = cpu_get_time_fast();
459#endif
bellardfd872592004-05-12 19:11:15 +0000460 return val;
bellard0824d6f2003-06-24 13:42:40 +0000461}
462
bellardc45886d2004-01-05 00:02:06 +0000463int cpu_inw(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000464{
bellardfd872592004-05-12 19:11:15 +0000465 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000466 val = ioport_read(1, addr);
bellardfd872592004-05-12 19:11:15 +0000467#ifdef DEBUG_IOPORT
468 if (loglevel & CPU_LOG_IOPORT)
469 fprintf(logfile, "inw : %04x %04x\n", addr, val);
470#endif
bellard89bfc102006-02-08 22:46:31 +0000471#ifdef USE_KQEMU
472 if (env)
473 env->last_io_time = cpu_get_time_fast();
474#endif
bellardfd872592004-05-12 19:11:15 +0000475 return val;
bellard0824d6f2003-06-24 13:42:40 +0000476}
477
bellardc45886d2004-01-05 00:02:06 +0000478int cpu_inl(CPUState *env, int addr)
bellard0824d6f2003-06-24 13:42:40 +0000479{
bellardfd872592004-05-12 19:11:15 +0000480 int val;
aliguori477e3ed2008-07-23 15:19:59 +0000481 val = ioport_read(2, addr);
bellardfd872592004-05-12 19:11:15 +0000482#ifdef DEBUG_IOPORT
483 if (loglevel & CPU_LOG_IOPORT)
484 fprintf(logfile, "inl : %04x %08x\n", addr, val);
485#endif
bellard89bfc102006-02-08 22:46:31 +0000486#ifdef USE_KQEMU
487 if (env)
488 env->last_io_time = cpu_get_time_fast();
489#endif
bellardfd872592004-05-12 19:11:15 +0000490 return val;
bellard0824d6f2003-06-24 13:42:40 +0000491}
492
493/***********************************************************/
bellard0824d6f2003-06-24 13:42:40 +0000494void hw_error(const char *fmt, ...)
495{
496 va_list ap;
bellard6a00d602005-11-21 23:25:50 +0000497 CPUState *env;
bellard0824d6f2003-06-24 13:42:40 +0000498
499 va_start(ap, fmt);
500 fprintf(stderr, "qemu: hardware error: ");
501 vfprintf(stderr, fmt, ap);
502 fprintf(stderr, "\n");
bellard6a00d602005-11-21 23:25:50 +0000503 for(env = first_cpu; env != NULL; env = env->next_cpu) {
504 fprintf(stderr, "CPU #%d:\n", env->cpu_index);
bellard0824d6f2003-06-24 13:42:40 +0000505#ifdef TARGET_I386
bellard6a00d602005-11-21 23:25:50 +0000506 cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
bellardc45886d2004-01-05 00:02:06 +0000507#else
bellard6a00d602005-11-21 23:25:50 +0000508 cpu_dump_state(env, stderr, fprintf, 0);
bellard0824d6f2003-06-24 13:42:40 +0000509#endif
bellard6a00d602005-11-21 23:25:50 +0000510 }
bellard0824d6f2003-06-24 13:42:40 +0000511 va_end(ap);
512 abort();
513}
514
bellard8a7ddc32004-03-31 19:00:16 +0000515/***********************************************************/
bellard63066f42004-06-03 18:45:02 +0000516/* keyboard/mouse */
517
518static QEMUPutKBDEvent *qemu_put_kbd_event;
519static void *qemu_put_kbd_event_opaque;
ths455204e2007-01-05 16:42:13 +0000520static QEMUPutMouseEntry *qemu_put_mouse_event_head;
521static QEMUPutMouseEntry *qemu_put_mouse_event_current;
bellard63066f42004-06-03 18:45:02 +0000522
523void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
524{
525 qemu_put_kbd_event_opaque = opaque;
526 qemu_put_kbd_event = func;
527}
528
ths455204e2007-01-05 16:42:13 +0000529QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
530 void *opaque, int absolute,
531 const char *name)
bellard63066f42004-06-03 18:45:02 +0000532{
ths455204e2007-01-05 16:42:13 +0000533 QEMUPutMouseEntry *s, *cursor;
534
535 s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
536 if (!s)
537 return NULL;
538
539 s->qemu_put_mouse_event = func;
540 s->qemu_put_mouse_event_opaque = opaque;
541 s->qemu_put_mouse_event_absolute = absolute;
542 s->qemu_put_mouse_event_name = qemu_strdup(name);
543 s->next = NULL;
544
545 if (!qemu_put_mouse_event_head) {
546 qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
547 return s;
548 }
549
550 cursor = qemu_put_mouse_event_head;
551 while (cursor->next != NULL)
552 cursor = cursor->next;
553
554 cursor->next = s;
555 qemu_put_mouse_event_current = s;
556
557 return s;
558}
559
560void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
561{
562 QEMUPutMouseEntry *prev = NULL, *cursor;
563
564 if (!qemu_put_mouse_event_head || entry == NULL)
565 return;
566
567 cursor = qemu_put_mouse_event_head;
568 while (cursor != NULL && cursor != entry) {
569 prev = cursor;
570 cursor = cursor->next;
571 }
572
573 if (cursor == NULL) // does not exist or list empty
574 return;
575 else if (prev == NULL) { // entry is head
576 qemu_put_mouse_event_head = cursor->next;
577 if (qemu_put_mouse_event_current == entry)
578 qemu_put_mouse_event_current = cursor->next;
579 qemu_free(entry->qemu_put_mouse_event_name);
580 qemu_free(entry);
581 return;
582 }
583
584 prev->next = entry->next;
585
586 if (qemu_put_mouse_event_current == entry)
587 qemu_put_mouse_event_current = prev;
588
589 qemu_free(entry->qemu_put_mouse_event_name);
590 qemu_free(entry);
bellard63066f42004-06-03 18:45:02 +0000591}
592
593void kbd_put_keycode(int keycode)
594{
595 if (qemu_put_kbd_event) {
596 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
597 }
598}
599
600void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
601{
ths455204e2007-01-05 16:42:13 +0000602 QEMUPutMouseEvent *mouse_event;
603 void *mouse_event_opaque;
balroga171fe32007-04-30 01:48:07 +0000604 int width;
ths455204e2007-01-05 16:42:13 +0000605
606 if (!qemu_put_mouse_event_current) {
607 return;
608 }
609
610 mouse_event =
611 qemu_put_mouse_event_current->qemu_put_mouse_event;
612 mouse_event_opaque =
613 qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
614
615 if (mouse_event) {
balroga171fe32007-04-30 01:48:07 +0000616 if (graphic_rotate) {
617 if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
618 width = 0x7fff;
619 else
aurel32b94ed572008-03-10 19:34:27 +0000620 width = graphic_width - 1;
balroga171fe32007-04-30 01:48:07 +0000621 mouse_event(mouse_event_opaque,
622 width - dy, dx, dz, buttons_state);
623 } else
624 mouse_event(mouse_event_opaque,
625 dx, dy, dz, buttons_state);
bellard63066f42004-06-03 18:45:02 +0000626 }
627}
628
bellard09b26c52006-04-12 21:09:08 +0000629int kbd_mouse_is_absolute(void)
630{
ths455204e2007-01-05 16:42:13 +0000631 if (!qemu_put_mouse_event_current)
632 return 0;
633
634 return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
635}
636
637void do_info_mice(void)
638{
639 QEMUPutMouseEntry *cursor;
640 int index = 0;
641
642 if (!qemu_put_mouse_event_head) {
643 term_printf("No mouse devices connected\n");
644 return;
645 }
646
647 term_printf("Mouse devices available:\n");
648 cursor = qemu_put_mouse_event_head;
649 while (cursor != NULL) {
650 term_printf("%c Mouse #%d: %s\n",
651 (cursor == qemu_put_mouse_event_current ? '*' : ' '),
652 index, cursor->qemu_put_mouse_event_name);
653 index++;
654 cursor = cursor->next;
655 }
656}
657
658void do_mouse_set(int index)
659{
660 QEMUPutMouseEntry *cursor;
661 int i = 0;
662
663 if (!qemu_put_mouse_event_head) {
664 term_printf("No mouse devices connected\n");
665 return;
666 }
667
668 cursor = qemu_put_mouse_event_head;
669 while (cursor != NULL && index != i) {
670 i++;
671 cursor = cursor->next;
672 }
673
674 if (cursor != NULL)
675 qemu_put_mouse_event_current = cursor;
676 else
677 term_printf("Mouse at given index not found\n");
bellard09b26c52006-04-12 21:09:08 +0000678}
679
bellard87858c82003-06-27 12:01:39 +0000680/* compute with 96 bit intermediate result: (a*b)/c */
bellard80cabfa2004-03-14 12:20:30 +0000681uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
bellard87858c82003-06-27 12:01:39 +0000682{
683 union {
684 uint64_t ll;
685 struct {
686#ifdef WORDS_BIGENDIAN
687 uint32_t high, low;
688#else
689 uint32_t low, high;
ths3b46e622007-09-17 08:09:54 +0000690#endif
bellard87858c82003-06-27 12:01:39 +0000691 } l;
692 } u, res;
693 uint64_t rl, rh;
694
695 u.ll = a;
696 rl = (uint64_t)u.l.low * (uint64_t)b;
697 rh = (uint64_t)u.l.high * (uint64_t)b;
698 rh += (rl >> 32);
699 res.l.high = rh / c;
700 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
701 return res.ll;
702}
703
bellard1dce7c32006-07-13 23:20:22 +0000704/***********************************************************/
705/* real time host monotonic timer */
706
707#define QEMU_TIMER_BASE 1000000000LL
708
709#ifdef WIN32
710
711static int64_t clock_freq;
712
713static void init_get_clock(void)
714{
bellarda8e5ac32006-07-14 09:36:13 +0000715 LARGE_INTEGER freq;
716 int ret;
bellard1dce7c32006-07-13 23:20:22 +0000717 ret = QueryPerformanceFrequency(&freq);
718 if (ret == 0) {
719 fprintf(stderr, "Could not calibrate ticks\n");
720 exit(1);
721 }
722 clock_freq = freq.QuadPart;
723}
724
725static int64_t get_clock(void)
726{
727 LARGE_INTEGER ti;
728 QueryPerformanceCounter(&ti);
729 return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
730}
731
732#else
733
734static int use_rt_clock;
735
736static void init_get_clock(void)
737{
738 use_rt_clock = 0;
aurel3260759372008-10-13 21:08:25 +0000739#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000740 {
741 struct timespec ts;
742 if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
743 use_rt_clock = 1;
744 }
745 }
746#endif
747}
748
749static int64_t get_clock(void)
750{
aurel3260759372008-10-13 21:08:25 +0000751#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
bellard1dce7c32006-07-13 23:20:22 +0000752 if (use_rt_clock) {
753 struct timespec ts;
754 clock_gettime(CLOCK_MONOTONIC, &ts);
755 return ts.tv_sec * 1000000000LL + ts.tv_nsec;
ths5fafdf22007-09-16 21:08:06 +0000756 } else
bellard1dce7c32006-07-13 23:20:22 +0000757#endif
758 {
759 /* XXX: using gettimeofday leads to problems if the date
760 changes, so it should be avoided. */
761 struct timeval tv;
762 gettimeofday(&tv, NULL);
763 return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
764 }
765}
bellard1dce7c32006-07-13 23:20:22 +0000766#endif
767
pbrook2e70f6e2008-06-29 01:03:05 +0000768/* Return the virtual CPU time, based on the instruction counter. */
769static int64_t cpu_get_icount(void)
770{
771 int64_t icount;
772 CPUState *env = cpu_single_env;;
773 icount = qemu_icount;
774 if (env) {
775 if (!can_do_io(env))
776 fprintf(stderr, "Bad clock read\n");
777 icount -= (env->icount_decr.u16.low + env->icount_extra);
778 }
779 return qemu_icount_bias + (icount << icount_time_shift);
780}
781
bellard1dce7c32006-07-13 23:20:22 +0000782/***********************************************************/
783/* guest cycle counter */
784
785static int64_t cpu_ticks_prev;
786static int64_t cpu_ticks_offset;
787static int64_t cpu_clock_offset;
788static int cpu_ticks_enabled;
789
790/* return the host CPU cycle counter and handle stop/restart */
791int64_t cpu_get_ticks(void)
792{
pbrook2e70f6e2008-06-29 01:03:05 +0000793 if (use_icount) {
794 return cpu_get_icount();
795 }
bellard1dce7c32006-07-13 23:20:22 +0000796 if (!cpu_ticks_enabled) {
797 return cpu_ticks_offset;
798 } else {
799 int64_t ticks;
800 ticks = cpu_get_real_ticks();
801 if (cpu_ticks_prev > ticks) {
802 /* Note: non increasing ticks may happen if the host uses
803 software suspend */
804 cpu_ticks_offset += cpu_ticks_prev - ticks;
805 }
806 cpu_ticks_prev = ticks;
807 return ticks + cpu_ticks_offset;
808 }
809}
810
811/* return the host CPU monotonic timer and handle stop/restart */
812static int64_t cpu_get_clock(void)
813{
814 int64_t ti;
815 if (!cpu_ticks_enabled) {
816 return cpu_clock_offset;
817 } else {
818 ti = get_clock();
819 return ti + cpu_clock_offset;
820 }
821}
822
823/* enable cpu_get_ticks() */
824void cpu_enable_ticks(void)
825{
826 if (!cpu_ticks_enabled) {
827 cpu_ticks_offset -= cpu_get_real_ticks();
828 cpu_clock_offset -= get_clock();
829 cpu_ticks_enabled = 1;
830 }
831}
832
833/* disable cpu_get_ticks() : the clock is stopped. You must not call
834 cpu_get_ticks() after that. */
835void cpu_disable_ticks(void)
836{
837 if (cpu_ticks_enabled) {
838 cpu_ticks_offset = cpu_get_ticks();
839 cpu_clock_offset = cpu_get_clock();
840 cpu_ticks_enabled = 0;
841 }
842}
843
844/***********************************************************/
845/* timers */
ths5fafdf22007-09-16 21:08:06 +0000846
bellard8a7ddc32004-03-31 19:00:16 +0000847#define QEMU_TIMER_REALTIME 0
848#define QEMU_TIMER_VIRTUAL 1
849
850struct QEMUClock {
851 int type;
852 /* XXX: add frequency */
853};
854
855struct QEMUTimer {
856 QEMUClock *clock;
857 int64_t expire_time;
858 QEMUTimerCB *cb;
859 void *opaque;
860 struct QEMUTimer *next;
861};
862
thsc8994012007-08-19 21:56:03 +0000863struct qemu_alarm_timer {
864 char const *name;
thsefe75412007-08-24 01:36:32 +0000865 unsigned int flags;
thsc8994012007-08-19 21:56:03 +0000866
867 int (*start)(struct qemu_alarm_timer *t);
868 void (*stop)(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000869 void (*rearm)(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000870 void *priv;
871};
872
thsefe75412007-08-24 01:36:32 +0000873#define ALARM_FLAG_DYNTICKS 0x1
balrogd5d08332008-01-05 19:41:47 +0000874#define ALARM_FLAG_EXPIRED 0x2
thsefe75412007-08-24 01:36:32 +0000875
876static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
877{
878 return t->flags & ALARM_FLAG_DYNTICKS;
879}
880
881static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
882{
883 if (!alarm_has_dynticks(t))
884 return;
885
886 t->rearm(t);
887}
888
889/* TODO: MIN_TIMER_REARM_US should be optimized */
890#define MIN_TIMER_REARM_US 250
891
thsc8994012007-08-19 21:56:03 +0000892static struct qemu_alarm_timer *alarm_timer;
aliguorif49e58d2008-11-05 21:22:34 +0000893#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +0000894static int alarm_timer_rfd, alarm_timer_wfd;
aliguorif49e58d2008-11-05 21:22:34 +0000895#endif
thsc8994012007-08-19 21:56:03 +0000896
897#ifdef _WIN32
898
899struct qemu_alarm_win32 {
900 MMRESULT timerId;
901 HANDLE host_alarm;
902 unsigned int period;
903} alarm_win32_data = {0, NULL, -1};
904
905static int win32_start_timer(struct qemu_alarm_timer *t);
906static void win32_stop_timer(struct qemu_alarm_timer *t);
thsefe75412007-08-24 01:36:32 +0000907static void win32_rearm_timer(struct qemu_alarm_timer *t);
thsc8994012007-08-19 21:56:03 +0000908
909#else
910
911static int unix_start_timer(struct qemu_alarm_timer *t);
912static void unix_stop_timer(struct qemu_alarm_timer *t);
913
ths231c6582007-08-26 17:29:15 +0000914#ifdef __linux__
915
thsefe75412007-08-24 01:36:32 +0000916static int dynticks_start_timer(struct qemu_alarm_timer *t);
917static void dynticks_stop_timer(struct qemu_alarm_timer *t);
918static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
919
thsc40ec5a2007-08-19 22:09:40 +0000920static int hpet_start_timer(struct qemu_alarm_timer *t);
921static void hpet_stop_timer(struct qemu_alarm_timer *t);
922
thsc8994012007-08-19 21:56:03 +0000923static int rtc_start_timer(struct qemu_alarm_timer *t);
924static void rtc_stop_timer(struct qemu_alarm_timer *t);
925
thsefe75412007-08-24 01:36:32 +0000926#endif /* __linux__ */
thsc8994012007-08-19 21:56:03 +0000927
928#endif /* _WIN32 */
929
pbrook2e70f6e2008-06-29 01:03:05 +0000930/* Correlation between real and virtual time is always going to be
thsbf20dc02008-06-30 17:22:19 +0000931 fairly approximate, so ignore small variation.
pbrook2e70f6e2008-06-29 01:03:05 +0000932 When the guest is idle real and virtual time will be aligned in
933 the IO wait loop. */
934#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
935
936static void icount_adjust(void)
937{
938 int64_t cur_time;
939 int64_t cur_icount;
940 int64_t delta;
941 static int64_t last_delta;
942 /* If the VM is not running, then do nothing. */
943 if (!vm_running)
944 return;
945
946 cur_time = cpu_get_clock();
947 cur_icount = qemu_get_clock(vm_clock);
948 delta = cur_icount - cur_time;
949 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
950 if (delta > 0
951 && last_delta + ICOUNT_WOBBLE < delta * 2
952 && icount_time_shift > 0) {
953 /* The guest is getting too far ahead. Slow time down. */
954 icount_time_shift--;
955 }
956 if (delta < 0
957 && last_delta - ICOUNT_WOBBLE > delta * 2
958 && icount_time_shift < MAX_ICOUNT_SHIFT) {
959 /* The guest is getting too far behind. Speed time up. */
960 icount_time_shift++;
961 }
962 last_delta = delta;
963 qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
964}
965
966static void icount_adjust_rt(void * opaque)
967{
968 qemu_mod_timer(icount_rt_timer,
969 qemu_get_clock(rt_clock) + 1000);
970 icount_adjust();
971}
972
973static void icount_adjust_vm(void * opaque)
974{
975 qemu_mod_timer(icount_vm_timer,
976 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
977 icount_adjust();
978}
979
980static void init_icount_adjust(void)
981{
982 /* Have both realtime and virtual time triggers for speed adjustment.
983 The realtime trigger catches emulated time passing too slowly,
984 the virtual time trigger catches emulated time passing too fast.
985 Realtime triggers occur even when idle, so use them less frequently
986 than VM triggers. */
987 icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
988 qemu_mod_timer(icount_rt_timer,
989 qemu_get_clock(rt_clock) + 1000);
990 icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
991 qemu_mod_timer(icount_vm_timer,
992 qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
993}
994
thsc8994012007-08-19 21:56:03 +0000995static struct qemu_alarm_timer alarm_timers[] = {
thsefe75412007-08-24 01:36:32 +0000996#ifndef _WIN32
ths231c6582007-08-26 17:29:15 +0000997#ifdef __linux__
thsefe75412007-08-24 01:36:32 +0000998 {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
999 dynticks_stop_timer, dynticks_rearm_timer, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001000 /* HPET - if available - is preferred */
thsefe75412007-08-24 01:36:32 +00001001 {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
thsc40ec5a2007-08-19 22:09:40 +00001002 /* ...otherwise try RTC */
thsefe75412007-08-24 01:36:32 +00001003 {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001004#endif
thsefe75412007-08-24 01:36:32 +00001005 {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
thsc8994012007-08-19 21:56:03 +00001006#else
thsefe75412007-08-24 01:36:32 +00001007 {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1008 win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1009 {"win32", 0, win32_start_timer,
1010 win32_stop_timer, NULL, &alarm_win32_data},
thsc8994012007-08-19 21:56:03 +00001011#endif
1012 {NULL, }
1013};
1014
blueswir13f47aa82008-03-09 06:59:01 +00001015static void show_available_alarms(void)
thsf3dcfad2007-08-24 01:26:02 +00001016{
1017 int i;
1018
1019 printf("Available alarm timers, in order of precedence:\n");
1020 for (i = 0; alarm_timers[i].name; i++)
1021 printf("%s\n", alarm_timers[i].name);
1022}
1023
1024static void configure_alarms(char const *opt)
1025{
1026 int i;
1027 int cur = 0;
1028 int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1029 char *arg;
1030 char *name;
pbrook2e70f6e2008-06-29 01:03:05 +00001031 struct qemu_alarm_timer tmp;
thsf3dcfad2007-08-24 01:26:02 +00001032
aurel323adda042008-03-09 23:43:49 +00001033 if (!strcmp(opt, "?")) {
thsf3dcfad2007-08-24 01:26:02 +00001034 show_available_alarms();
1035 exit(0);
1036 }
1037
1038 arg = strdup(opt);
1039
1040 /* Reorder the array */
1041 name = strtok(arg, ",");
1042 while (name) {
balroge2b577e2007-09-17 21:25:20 +00001043 for (i = 0; i < count && alarm_timers[i].name; i++) {
thsf3dcfad2007-08-24 01:26:02 +00001044 if (!strcmp(alarm_timers[i].name, name))
1045 break;
1046 }
1047
1048 if (i == count) {
1049 fprintf(stderr, "Unknown clock %s\n", name);
1050 goto next;
1051 }
1052
1053 if (i < cur)
1054 /* Ignore */
1055 goto next;
1056
1057 /* Swap */
1058 tmp = alarm_timers[i];
1059 alarm_timers[i] = alarm_timers[cur];
1060 alarm_timers[cur] = tmp;
1061
1062 cur++;
1063next:
1064 name = strtok(NULL, ",");
1065 }
1066
1067 free(arg);
1068
1069 if (cur) {
pbrook2e70f6e2008-06-29 01:03:05 +00001070 /* Disable remaining timers */
thsf3dcfad2007-08-24 01:26:02 +00001071 for (i = cur; i < count; i++)
1072 alarm_timers[i].name = NULL;
aurel323adda042008-03-09 23:43:49 +00001073 } else {
1074 show_available_alarms();
1075 exit(1);
thsf3dcfad2007-08-24 01:26:02 +00001076 }
thsf3dcfad2007-08-24 01:26:02 +00001077}
1078
bellard8a7ddc32004-03-31 19:00:16 +00001079QEMUClock *rt_clock;
1080QEMUClock *vm_clock;
1081
1082static QEMUTimer *active_timers[2];
bellard8a7ddc32004-03-31 19:00:16 +00001083
pbrook9596ebb2007-11-18 01:44:38 +00001084static QEMUClock *qemu_new_clock(int type)
bellard8a7ddc32004-03-31 19:00:16 +00001085{
1086 QEMUClock *clock;
1087 clock = qemu_mallocz(sizeof(QEMUClock));
1088 if (!clock)
1089 return NULL;
1090 clock->type = type;
1091 return clock;
1092}
1093
1094QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1095{
1096 QEMUTimer *ts;
1097
1098 ts = qemu_mallocz(sizeof(QEMUTimer));
1099 ts->clock = clock;
1100 ts->cb = cb;
1101 ts->opaque = opaque;
1102 return ts;
1103}
1104
1105void qemu_free_timer(QEMUTimer *ts)
1106{
1107 qemu_free(ts);
1108}
1109
1110/* stop a timer, but do not dealloc it */
1111void qemu_del_timer(QEMUTimer *ts)
1112{
1113 QEMUTimer **pt, *t;
1114
1115 /* NOTE: this code must be signal safe because
1116 qemu_timer_expired() can be called from a signal. */
1117 pt = &active_timers[ts->clock->type];
1118 for(;;) {
1119 t = *pt;
1120 if (!t)
1121 break;
1122 if (t == ts) {
1123 *pt = t->next;
1124 break;
1125 }
1126 pt = &t->next;
1127 }
1128}
1129
1130/* modify the current timer so that it will be fired when current_time
1131 >= expire_time. The corresponding callback will be called. */
1132void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1133{
1134 QEMUTimer **pt, *t;
1135
1136 qemu_del_timer(ts);
1137
1138 /* add the timer in the sorted list */
1139 /* NOTE: this code must be signal safe because
1140 qemu_timer_expired() can be called from a signal. */
1141 pt = &active_timers[ts->clock->type];
1142 for(;;) {
1143 t = *pt;
1144 if (!t)
1145 break;
ths5fafdf22007-09-16 21:08:06 +00001146 if (t->expire_time > expire_time)
bellard8a7ddc32004-03-31 19:00:16 +00001147 break;
1148 pt = &t->next;
1149 }
1150 ts->expire_time = expire_time;
1151 ts->next = *pt;
1152 *pt = ts;
balrogd5d08332008-01-05 19:41:47 +00001153
1154 /* Rearm if necessary */
pbrook2e70f6e2008-06-29 01:03:05 +00001155 if (pt == &active_timers[ts->clock->type]) {
1156 if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1157 qemu_rearm_alarm_timer(alarm_timer);
1158 }
1159 /* Interrupt execution to force deadline recalculation. */
1160 if (use_icount && cpu_single_env) {
1161 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1162 }
1163 }
bellard8a7ddc32004-03-31 19:00:16 +00001164}
1165
1166int qemu_timer_pending(QEMUTimer *ts)
1167{
1168 QEMUTimer *t;
1169 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1170 if (t == ts)
1171 return 1;
1172 }
1173 return 0;
1174}
1175
1176static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1177{
1178 if (!timer_head)
1179 return 0;
1180 return (timer_head->expire_time <= current_time);
1181}
1182
1183static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1184{
1185 QEMUTimer *ts;
ths3b46e622007-09-17 08:09:54 +00001186
bellard8a7ddc32004-03-31 19:00:16 +00001187 for(;;) {
1188 ts = *ptimer_head;
bellarde95c8d52004-09-30 22:22:08 +00001189 if (!ts || ts->expire_time > current_time)
bellard8a7ddc32004-03-31 19:00:16 +00001190 break;
1191 /* remove timer from the list before calling the callback */
1192 *ptimer_head = ts->next;
1193 ts->next = NULL;
ths3b46e622007-09-17 08:09:54 +00001194
bellard8a7ddc32004-03-31 19:00:16 +00001195 /* run the callback (the timer list can be modified) */
1196 ts->cb(ts->opaque);
1197 }
1198}
1199
1200int64_t qemu_get_clock(QEMUClock *clock)
1201{
1202 switch(clock->type) {
1203 case QEMU_TIMER_REALTIME:
bellard1dce7c32006-07-13 23:20:22 +00001204 return get_clock() / 1000000;
bellard8a7ddc32004-03-31 19:00:16 +00001205 default:
1206 case QEMU_TIMER_VIRTUAL:
pbrook2e70f6e2008-06-29 01:03:05 +00001207 if (use_icount) {
1208 return cpu_get_icount();
1209 } else {
1210 return cpu_get_clock();
1211 }
bellard8a7ddc32004-03-31 19:00:16 +00001212 }
1213}
1214
bellard1dce7c32006-07-13 23:20:22 +00001215static void init_timers(void)
1216{
1217 init_get_clock();
1218 ticks_per_sec = QEMU_TIMER_BASE;
1219 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1220 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1221}
1222
bellard8a7ddc32004-03-31 19:00:16 +00001223/* save a timer */
1224void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1225{
1226 uint64_t expire_time;
1227
1228 if (qemu_timer_pending(ts)) {
1229 expire_time = ts->expire_time;
1230 } else {
1231 expire_time = -1;
1232 }
1233 qemu_put_be64(f, expire_time);
1234}
1235
1236void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1237{
1238 uint64_t expire_time;
1239
1240 expire_time = qemu_get_be64(f);
1241 if (expire_time != -1) {
1242 qemu_mod_timer(ts, expire_time);
1243 } else {
1244 qemu_del_timer(ts);
1245 }
1246}
1247
1248static void timer_save(QEMUFile *f, void *opaque)
1249{
1250 if (cpu_ticks_enabled) {
1251 hw_error("cannot save state if virtual timers are running");
1252 }
thsbee8d682007-12-16 23:41:11 +00001253 qemu_put_be64(f, cpu_ticks_offset);
1254 qemu_put_be64(f, ticks_per_sec);
1255 qemu_put_be64(f, cpu_clock_offset);
bellard8a7ddc32004-03-31 19:00:16 +00001256}
1257
1258static int timer_load(QEMUFile *f, void *opaque, int version_id)
1259{
bellardc88676f2006-08-06 13:36:11 +00001260 if (version_id != 1 && version_id != 2)
bellard8a7ddc32004-03-31 19:00:16 +00001261 return -EINVAL;
1262 if (cpu_ticks_enabled) {
1263 return -EINVAL;
1264 }
thsbee8d682007-12-16 23:41:11 +00001265 cpu_ticks_offset=qemu_get_be64(f);
1266 ticks_per_sec=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001267 if (version_id == 2) {
thsbee8d682007-12-16 23:41:11 +00001268 cpu_clock_offset=qemu_get_be64(f);
bellardc88676f2006-08-06 13:36:11 +00001269 }
bellard8a7ddc32004-03-31 19:00:16 +00001270 return 0;
1271}
1272
bellard67b915a2004-03-31 23:37:16 +00001273#ifdef _WIN32
ths5fafdf22007-09-16 21:08:06 +00001274void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
bellard67b915a2004-03-31 23:37:16 +00001275 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1276#else
bellard8a7ddc32004-03-31 19:00:16 +00001277static void host_alarm_handler(int host_signum)
bellard67b915a2004-03-31 23:37:16 +00001278#endif
bellard8a7ddc32004-03-31 19:00:16 +00001279{
bellard02ba45c2004-06-25 14:46:23 +00001280#if 0
1281#define DISP_FREQ 1000
1282 {
1283 static int64_t delta_min = INT64_MAX;
1284 static int64_t delta_max, delta_cum, last_clock, delta, ti;
1285 static int count;
1286 ti = qemu_get_clock(vm_clock);
1287 if (last_clock != 0) {
1288 delta = ti - last_clock;
1289 if (delta < delta_min)
1290 delta_min = delta;
1291 if (delta > delta_max)
1292 delta_max = delta;
1293 delta_cum += delta;
1294 if (++count == DISP_FREQ) {
bellard26a76462006-06-25 18:15:32 +00001295 printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
bellard02ba45c2004-06-25 14:46:23 +00001296 muldiv64(delta_min, 1000000, ticks_per_sec),
1297 muldiv64(delta_max, 1000000, ticks_per_sec),
1298 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1299 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1300 count = 0;
1301 delta_min = INT64_MAX;
1302 delta_max = 0;
1303 delta_cum = 0;
1304 }
1305 }
1306 last_clock = ti;
1307 }
1308#endif
thsefe75412007-08-24 01:36:32 +00001309 if (alarm_has_dynticks(alarm_timer) ||
pbrook2e70f6e2008-06-29 01:03:05 +00001310 (!use_icount &&
1311 qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1312 qemu_get_clock(vm_clock))) ||
bellard8a7ddc32004-03-31 19:00:16 +00001313 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1314 qemu_get_clock(rt_clock))) {
aliguoric96f1a42008-11-05 20:29:45 +00001315 CPUState *env = next_cpu;
aliguoric96f1a42008-11-05 20:29:45 +00001316
bellard06d9f2f2006-05-01 13:23:04 +00001317#ifdef _WIN32
thsc8994012007-08-19 21:56:03 +00001318 struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1319 SetEvent(data->host_alarm);
aliguorif49e58d2008-11-05 21:22:34 +00001320#else
1321 static const char byte = 0;
aliguoric96f1a42008-11-05 20:29:45 +00001322 write(alarm_timer_wfd, &byte, sizeof(byte));
aliguorif49e58d2008-11-05 21:22:34 +00001323#endif
balrogd5d08332008-01-05 19:41:47 +00001324 alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1325
balrog4f8eb8d2007-12-16 12:39:38 +00001326 if (env) {
1327 /* stop the currently executing cpu because a timer occured */
1328 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
bellarda332e112005-09-03 17:55:47 +00001329#ifdef USE_KQEMU
balrog4f8eb8d2007-12-16 12:39:38 +00001330 if (env->kqemu_enabled) {
1331 kqemu_cpu_interrupt(env);
1332 }
balrogee5605e2007-12-03 03:01:40 +00001333#endif
balrog4f8eb8d2007-12-16 12:39:38 +00001334 }
balrogee5605e2007-12-03 03:01:40 +00001335 event_pending = 1;
bellard8a7ddc32004-03-31 19:00:16 +00001336 }
1337}
1338
pbrook2e70f6e2008-06-29 01:03:05 +00001339static int64_t qemu_next_deadline(void)
thsefe75412007-08-24 01:36:32 +00001340{
pbrook2e70f6e2008-06-29 01:03:05 +00001341 int64_t delta;
thsefe75412007-08-24 01:36:32 +00001342
1343 if (active_timers[QEMU_TIMER_VIRTUAL]) {
pbrook2e70f6e2008-06-29 01:03:05 +00001344 delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1345 qemu_get_clock(vm_clock);
1346 } else {
1347 /* To avoid problems with overflow limit this to 2^32. */
1348 delta = INT32_MAX;
thsefe75412007-08-24 01:36:32 +00001349 }
1350
pbrook2e70f6e2008-06-29 01:03:05 +00001351 if (delta < 0)
1352 delta = 0;
thsefe75412007-08-24 01:36:32 +00001353
pbrook2e70f6e2008-06-29 01:03:05 +00001354 return delta;
1355}
1356
blueswir18632fb92008-09-14 13:59:34 +00001357#if defined(__linux__) || defined(_WIN32)
pbrook2e70f6e2008-06-29 01:03:05 +00001358static uint64_t qemu_next_deadline_dyntick(void)
1359{
1360 int64_t delta;
1361 int64_t rtdelta;
1362
1363 if (use_icount)
1364 delta = INT32_MAX;
1365 else
1366 delta = (qemu_next_deadline() + 999) / 1000;
1367
1368 if (active_timers[QEMU_TIMER_REALTIME]) {
1369 rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1370 qemu_get_clock(rt_clock))*1000;
1371 if (rtdelta < delta)
1372 delta = rtdelta;
1373 }
1374
1375 if (delta < MIN_TIMER_REARM_US)
1376 delta = MIN_TIMER_REARM_US;
1377
1378 return delta;
thsefe75412007-08-24 01:36:32 +00001379}
blueswir18632fb92008-09-14 13:59:34 +00001380#endif
thsefe75412007-08-24 01:36:32 +00001381
bellardfd872592004-05-12 19:11:15 +00001382#ifndef _WIN32
1383
aliguori7183b4b2008-11-05 20:40:18 +00001384/* Sets a specific flag */
1385static int fcntl_setfl(int fd, int flag)
1386{
1387 int flags;
1388
1389 flags = fcntl(fd, F_GETFL);
1390 if (flags == -1)
1391 return -errno;
1392
1393 if (fcntl(fd, F_SETFL, flags | flag) == -1)
1394 return -errno;
1395
1396 return 0;
1397}
1398
bellard829309c2004-05-20 13:20:12 +00001399#if defined(__linux__)
1400
bellardfd872592004-05-12 19:11:15 +00001401#define RTC_FREQ 1024
1402
thsc8994012007-08-19 21:56:03 +00001403static void enable_sigio_timer(int fd)
bellardfd872592004-05-12 19:11:15 +00001404{
thsc8994012007-08-19 21:56:03 +00001405 struct sigaction act;
1406
1407 /* timer signal */
1408 sigfillset(&act.sa_mask);
1409 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001410 act.sa_handler = host_alarm_handler;
1411
1412 sigaction(SIGIO, &act, NULL);
aliguori7183b4b2008-11-05 20:40:18 +00001413 fcntl_setfl(fd, O_ASYNC);
thsc8994012007-08-19 21:56:03 +00001414 fcntl(fd, F_SETOWN, getpid());
1415}
1416
thsc40ec5a2007-08-19 22:09:40 +00001417static int hpet_start_timer(struct qemu_alarm_timer *t)
1418{
1419 struct hpet_info info;
1420 int r, fd;
1421
1422 fd = open("/dev/hpet", O_RDONLY);
1423 if (fd < 0)
1424 return -1;
1425
1426 /* Set frequency */
1427 r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1428 if (r < 0) {
1429 fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1430 "error, but for better emulation accuracy type:\n"
1431 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1432 goto fail;
1433 }
1434
1435 /* Check capabilities */
1436 r = ioctl(fd, HPET_INFO, &info);
1437 if (r < 0)
1438 goto fail;
1439
1440 /* Enable periodic mode */
1441 r = ioctl(fd, HPET_EPI, 0);
1442 if (info.hi_flags && (r < 0))
1443 goto fail;
1444
1445 /* Enable interrupt */
1446 r = ioctl(fd, HPET_IE_ON, 0);
1447 if (r < 0)
1448 goto fail;
1449
1450 enable_sigio_timer(fd);
pbrookfcdc2122007-08-23 20:22:22 +00001451 t->priv = (void *)(long)fd;
thsc40ec5a2007-08-19 22:09:40 +00001452
1453 return 0;
1454fail:
1455 close(fd);
1456 return -1;
1457}
1458
1459static void hpet_stop_timer(struct qemu_alarm_timer *t)
1460{
pbrookfcdc2122007-08-23 20:22:22 +00001461 int fd = (long)t->priv;
thsc40ec5a2007-08-19 22:09:40 +00001462
1463 close(fd);
1464}
1465
thsc8994012007-08-19 21:56:03 +00001466static int rtc_start_timer(struct qemu_alarm_timer *t)
1467{
1468 int rtc_fd;
balrogb5a23ad2008-02-03 03:45:47 +00001469 unsigned long current_rtc_freq = 0;
thsc8994012007-08-19 21:56:03 +00001470
balrogaeb30be2007-07-02 15:03:13 +00001471 TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
bellardfd872592004-05-12 19:11:15 +00001472 if (rtc_fd < 0)
1473 return -1;
balrogb5a23ad2008-02-03 03:45:47 +00001474 ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1475 if (current_rtc_freq != RTC_FREQ &&
1476 ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
bellardfd872592004-05-12 19:11:15 +00001477 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1478 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1479 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1480 goto fail;
1481 }
1482 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1483 fail:
1484 close(rtc_fd);
1485 return -1;
1486 }
thsc8994012007-08-19 21:56:03 +00001487
1488 enable_sigio_timer(rtc_fd);
1489
pbrookfcdc2122007-08-23 20:22:22 +00001490 t->priv = (void *)(long)rtc_fd;
thsc8994012007-08-19 21:56:03 +00001491
bellardfd872592004-05-12 19:11:15 +00001492 return 0;
1493}
1494
thsc8994012007-08-19 21:56:03 +00001495static void rtc_stop_timer(struct qemu_alarm_timer *t)
bellard829309c2004-05-20 13:20:12 +00001496{
pbrookfcdc2122007-08-23 20:22:22 +00001497 int rtc_fd = (long)t->priv;
thsc8994012007-08-19 21:56:03 +00001498
1499 close(rtc_fd);
bellard829309c2004-05-20 13:20:12 +00001500}
1501
thsefe75412007-08-24 01:36:32 +00001502static int dynticks_start_timer(struct qemu_alarm_timer *t)
1503{
1504 struct sigevent ev;
1505 timer_t host_timer;
1506 struct sigaction act;
1507
1508 sigfillset(&act.sa_mask);
1509 act.sa_flags = 0;
thsefe75412007-08-24 01:36:32 +00001510 act.sa_handler = host_alarm_handler;
1511
1512 sigaction(SIGALRM, &act, NULL);
1513
1514 ev.sigev_value.sival_int = 0;
1515 ev.sigev_notify = SIGEV_SIGNAL;
1516 ev.sigev_signo = SIGALRM;
1517
1518 if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1519 perror("timer_create");
1520
1521 /* disable dynticks */
1522 fprintf(stderr, "Dynamic Ticks disabled\n");
1523
1524 return -1;
1525 }
1526
1527 t->priv = (void *)host_timer;
1528
1529 return 0;
1530}
1531
1532static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1533{
1534 timer_t host_timer = (timer_t)t->priv;
1535
1536 timer_delete(host_timer);
1537}
1538
1539static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1540{
1541 timer_t host_timer = (timer_t)t->priv;
1542 struct itimerspec timeout;
1543 int64_t nearest_delta_us = INT64_MAX;
1544 int64_t current_us;
1545
1546 if (!active_timers[QEMU_TIMER_REALTIME] &&
1547 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001548 return;
thsefe75412007-08-24 01:36:32 +00001549
pbrook2e70f6e2008-06-29 01:03:05 +00001550 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001551
1552 /* check whether a timer is already running */
1553 if (timer_gettime(host_timer, &timeout)) {
1554 perror("gettime");
1555 fprintf(stderr, "Internal timer error: aborting\n");
1556 exit(1);
1557 }
1558 current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1559 if (current_us && current_us <= nearest_delta_us)
1560 return;
1561
1562 timeout.it_interval.tv_sec = 0;
1563 timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1564 timeout.it_value.tv_sec = nearest_delta_us / 1000000;
1565 timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1566 if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1567 perror("settime");
1568 fprintf(stderr, "Internal timer error: aborting\n");
1569 exit(1);
1570 }
1571}
1572
ths70744b32007-08-26 17:31:30 +00001573#endif /* defined(__linux__) */
ths231c6582007-08-26 17:29:15 +00001574
thsc8994012007-08-19 21:56:03 +00001575static int unix_start_timer(struct qemu_alarm_timer *t)
1576{
1577 struct sigaction act;
1578 struct itimerval itv;
1579 int err;
1580
1581 /* timer signal */
1582 sigfillset(&act.sa_mask);
1583 act.sa_flags = 0;
thsc8994012007-08-19 21:56:03 +00001584 act.sa_handler = host_alarm_handler;
1585
1586 sigaction(SIGALRM, &act, NULL);
1587
1588 itv.it_interval.tv_sec = 0;
1589 /* for i386 kernel 2.6 to get 1 ms */
1590 itv.it_interval.tv_usec = 999;
1591 itv.it_value.tv_sec = 0;
1592 itv.it_value.tv_usec = 10 * 1000;
1593
1594 err = setitimer(ITIMER_REAL, &itv, NULL);
1595 if (err)
1596 return -1;
1597
1598 return 0;
1599}
1600
1601static void unix_stop_timer(struct qemu_alarm_timer *t)
1602{
1603 struct itimerval itv;
1604
1605 memset(&itv, 0, sizeof(itv));
1606 setitimer(ITIMER_REAL, &itv, NULL);
1607}
1608
bellard829309c2004-05-20 13:20:12 +00001609#endif /* !defined(_WIN32) */
bellardfd872592004-05-12 19:11:15 +00001610
aliguorif49e58d2008-11-05 21:22:34 +00001611static void try_to_rearm_timer(void *opaque)
1612{
1613 struct qemu_alarm_timer *t = opaque;
1614#ifndef _WIN32
1615 ssize_t len;
1616
1617 /* Drain the notify pipe */
1618 do {
1619 char buffer[512];
1620 len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1621 } while ((len == -1 && errno == EINTR) || len > 0);
1622#endif
1623
1624 /* vm time timers */
1625 if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
1626 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1627 qemu_get_clock(vm_clock));
1628
1629 /* real time timers */
1630 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1631 qemu_get_clock(rt_clock));
1632
1633 if (t->flags & ALARM_FLAG_EXPIRED) {
1634 alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1635 qemu_rearm_alarm_timer(alarm_timer);
1636 }
1637}
1638
thsc8994012007-08-19 21:56:03 +00001639#ifdef _WIN32
1640
1641static int win32_start_timer(struct qemu_alarm_timer *t)
1642{
1643 TIMECAPS tc;
1644 struct qemu_alarm_win32 *data = t->priv;
thsefe75412007-08-24 01:36:32 +00001645 UINT flags;
thsc8994012007-08-19 21:56:03 +00001646
1647 data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1648 if (!data->host_alarm) {
1649 perror("Failed CreateEvent");
thsc396a7f2007-08-20 15:42:22 +00001650 return -1;
thsc8994012007-08-19 21:56:03 +00001651 }
1652
1653 memset(&tc, 0, sizeof(tc));
1654 timeGetDevCaps(&tc, sizeof(tc));
1655
1656 if (data->period < tc.wPeriodMin)
1657 data->period = tc.wPeriodMin;
1658
1659 timeBeginPeriod(data->period);
1660
thsefe75412007-08-24 01:36:32 +00001661 flags = TIME_CALLBACK_FUNCTION;
1662 if (alarm_has_dynticks(t))
1663 flags |= TIME_ONESHOT;
1664 else
1665 flags |= TIME_PERIODIC;
1666
thsc8994012007-08-19 21:56:03 +00001667 data->timerId = timeSetEvent(1, // interval (ms)
1668 data->period, // resolution
1669 host_alarm_handler, // function
1670 (DWORD)t, // parameter
thsefe75412007-08-24 01:36:32 +00001671 flags);
thsc8994012007-08-19 21:56:03 +00001672
1673 if (!data->timerId) {
1674 perror("Failed to initialize win32 alarm timer");
1675
1676 timeEndPeriod(data->period);
1677 CloseHandle(data->host_alarm);
1678 return -1;
1679 }
1680
aliguorif49e58d2008-11-05 21:22:34 +00001681 qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
thsc8994012007-08-19 21:56:03 +00001682
1683 return 0;
1684}
1685
1686static void win32_stop_timer(struct qemu_alarm_timer *t)
1687{
1688 struct qemu_alarm_win32 *data = t->priv;
1689
1690 timeKillEvent(data->timerId);
1691 timeEndPeriod(data->period);
1692
1693 CloseHandle(data->host_alarm);
1694}
1695
thsefe75412007-08-24 01:36:32 +00001696static void win32_rearm_timer(struct qemu_alarm_timer *t)
1697{
1698 struct qemu_alarm_win32 *data = t->priv;
1699 uint64_t nearest_delta_us;
1700
1701 if (!active_timers[QEMU_TIMER_REALTIME] &&
1702 !active_timers[QEMU_TIMER_VIRTUAL])
balrogd5d08332008-01-05 19:41:47 +00001703 return;
thsefe75412007-08-24 01:36:32 +00001704
pbrook2e70f6e2008-06-29 01:03:05 +00001705 nearest_delta_us = qemu_next_deadline_dyntick();
thsefe75412007-08-24 01:36:32 +00001706 nearest_delta_us /= 1000;
1707
1708 timeKillEvent(data->timerId);
1709
1710 data->timerId = timeSetEvent(1,
1711 data->period,
1712 host_alarm_handler,
1713 (DWORD)t,
1714 TIME_ONESHOT | TIME_PERIODIC);
1715
1716 if (!data->timerId) {
1717 perror("Failed to re-arm win32 alarm timer");
1718
1719 timeEndPeriod(data->period);
1720 CloseHandle(data->host_alarm);
1721 exit(1);
1722 }
1723}
1724
thsc8994012007-08-19 21:56:03 +00001725#endif /* _WIN32 */
1726
aliguori7183b4b2008-11-05 20:40:18 +00001727static int init_timer_alarm(void)
bellard8a7ddc32004-03-31 19:00:16 +00001728{
blueswir1223f0d72008-09-30 18:12:18 +00001729 struct qemu_alarm_timer *t = NULL;
thsc8994012007-08-19 21:56:03 +00001730 int i, err = -1;
aliguorif49e58d2008-11-05 21:22:34 +00001731
1732#ifndef _WIN32
aliguoric96f1a42008-11-05 20:29:45 +00001733 int fds[2];
1734
aliguori7183b4b2008-11-05 20:40:18 +00001735 err = pipe(fds);
1736 if (err == -1)
1737 return -errno;
1738
1739 err = fcntl_setfl(fds[0], O_NONBLOCK);
1740 if (err < 0)
1741 goto fail;
1742
1743 err = fcntl_setfl(fds[1], O_NONBLOCK);
1744 if (err < 0)
1745 goto fail;
1746
aliguoric96f1a42008-11-05 20:29:45 +00001747 alarm_timer_rfd = fds[0];
1748 alarm_timer_wfd = fds[1];
aliguorif49e58d2008-11-05 21:22:34 +00001749#endif
bellard06d9f2f2006-05-01 13:23:04 +00001750
thsc8994012007-08-19 21:56:03 +00001751 for (i = 0; alarm_timers[i].name; i++) {
1752 t = &alarm_timers[i];
1753
thsc8994012007-08-19 21:56:03 +00001754 err = t->start(t);
1755 if (!err)
1756 break;
bellard67b915a2004-03-31 23:37:16 +00001757 }
bellardfd872592004-05-12 19:11:15 +00001758
thsc8994012007-08-19 21:56:03 +00001759 if (err) {
aliguori7183b4b2008-11-05 20:40:18 +00001760 err = -ENOENT;
1761 goto fail;
bellard67b915a2004-03-31 23:37:16 +00001762 }
thsc8994012007-08-19 21:56:03 +00001763
aliguorif49e58d2008-11-05 21:22:34 +00001764#ifndef _WIN32
aliguori6abfbd72008-11-05 20:49:37 +00001765 qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1766 try_to_rearm_timer, NULL, t);
aliguorif49e58d2008-11-05 21:22:34 +00001767#endif
aliguori6abfbd72008-11-05 20:49:37 +00001768
thsc8994012007-08-19 21:56:03 +00001769 alarm_timer = t;
aliguori7183b4b2008-11-05 20:40:18 +00001770
aliguori6abfbd72008-11-05 20:49:37 +00001771 return 0;
aliguori7183b4b2008-11-05 20:40:18 +00001772
1773fail:
aliguorif49e58d2008-11-05 21:22:34 +00001774#ifndef _WIN32
aliguori7183b4b2008-11-05 20:40:18 +00001775 close(fds[0]);
1776 close(fds[1]);
aliguorif49e58d2008-11-05 21:22:34 +00001777#endif
aliguori7183b4b2008-11-05 20:40:18 +00001778 return err;
bellard8a7ddc32004-03-31 19:00:16 +00001779}
1780
pbrook9596ebb2007-11-18 01:44:38 +00001781static void quit_timers(void)
bellard40c3bac2004-04-04 12:56:28 +00001782{
thsc8994012007-08-19 21:56:03 +00001783 alarm_timer->stop(alarm_timer);
1784 alarm_timer = NULL;
bellard40c3bac2004-04-04 12:56:28 +00001785}
1786
bellardc4b1fcc2004-03-14 21:44:30 +00001787/***********************************************************/
balrogf6503052008-02-17 11:42:19 +00001788/* host time/date access */
1789void qemu_get_timedate(struct tm *tm, int offset)
1790{
1791 time_t ti;
1792 struct tm *ret;
1793
1794 time(&ti);
1795 ti += offset;
1796 if (rtc_date_offset == -1) {
1797 if (rtc_utc)
1798 ret = gmtime(&ti);
1799 else
1800 ret = localtime(&ti);
1801 } else {
1802 ti -= rtc_date_offset;
1803 ret = gmtime(&ti);
1804 }
1805
1806 memcpy(tm, ret, sizeof(struct tm));
1807}
1808
1809int qemu_timedate_diff(struct tm *tm)
1810{
1811 time_t seconds;
1812
1813 if (rtc_date_offset == -1)
1814 if (rtc_utc)
1815 seconds = mktimegm(tm);
1816 else
1817 seconds = mktime(tm);
1818 else
1819 seconds = mktimegm(tm) + rtc_date_offset;
1820
1821 return seconds - time(NULL);
1822}
1823
bellardfd1dff42006-02-01 21:29:26 +00001824#ifdef _WIN32
bellardfd1dff42006-02-01 21:29:26 +00001825static void socket_cleanup(void)
1826{
1827 WSACleanup();
1828}
bellard82c643f2004-07-14 17:28:13 +00001829
bellardfd1dff42006-02-01 21:29:26 +00001830static int socket_init(void)
1831{
1832 WSADATA Data;
1833 int ret, err;
1834
1835 ret = WSAStartup(MAKEWORD(2,2), &Data);
1836 if (ret != 0) {
1837 err = WSAGetLastError();
1838 fprintf(stderr, "WSAStartup: %d\n", err);
1839 return -1;
1840 }
1841 atexit(socket_cleanup);
1842 return 0;
1843}
aurel3264b7b732008-05-05 10:05:31 +00001844#endif
1845
aliguori63a01ef2008-10-31 19:10:00 +00001846const char *get_opt_name(char *buf, int buf_size, const char *p)
thse4bcb142007-12-02 04:51:10 +00001847{
1848 char *q;
thse4bcb142007-12-02 04:51:10 +00001849
balrog609497a2008-01-14 02:56:53 +00001850 q = buf;
1851 while (*p != '\0' && *p != '=') {
1852 if (q && (q - buf) < buf_size - 1)
1853 *q++ = *p;
1854 p++;
1855 }
1856 if (q)
1857 *q = '\0';
1858
1859 return p;
1860}
1861
aliguori63a01ef2008-10-31 19:10:00 +00001862const char *get_opt_value(char *buf, int buf_size, const char *p)
balrog609497a2008-01-14 02:56:53 +00001863{
1864 char *q;
1865
thse4bcb142007-12-02 04:51:10 +00001866 q = buf;
1867 while (*p != '\0') {
balrog609497a2008-01-14 02:56:53 +00001868 if (*p == ',') {
1869 if (*(p + 1) != ',')
thse4bcb142007-12-02 04:51:10 +00001870 break;
thse4bcb142007-12-02 04:51:10 +00001871 p++;
balrog609497a2008-01-14 02:56:53 +00001872 }
thse4bcb142007-12-02 04:51:10 +00001873 if (q && (q - buf) < buf_size - 1)
1874 *q++ = *p;
1875 p++;
1876 }
1877 if (q)
1878 *q = '\0';
1879
1880 return p;
1881}
1882
aliguori63a01ef2008-10-31 19:10:00 +00001883int get_param_value(char *buf, int buf_size,
1884 const char *tag, const char *str)
bellard7c9d8e02005-11-15 22:16:05 +00001885{
1886 const char *p;
bellard7c9d8e02005-11-15 22:16:05 +00001887 char option[128];
1888
1889 p = str;
1890 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001891 p = get_opt_name(option, sizeof(option), p);
bellard7c9d8e02005-11-15 22:16:05 +00001892 if (*p != '=')
1893 break;
1894 p++;
1895 if (!strcmp(tag, option)) {
balrog609497a2008-01-14 02:56:53 +00001896 (void)get_opt_value(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001897 return strlen(buf);
bellard7c9d8e02005-11-15 22:16:05 +00001898 } else {
balrog609497a2008-01-14 02:56:53 +00001899 p = get_opt_value(NULL, 0, p);
bellard7c9d8e02005-11-15 22:16:05 +00001900 }
1901 if (*p != ',')
1902 break;
1903 p++;
1904 }
1905 return 0;
1906}
1907
aliguori63a01ef2008-10-31 19:10:00 +00001908int check_params(char *buf, int buf_size,
1909 const char * const *params, const char *str)
thse4bcb142007-12-02 04:51:10 +00001910{
1911 const char *p;
1912 int i;
1913
1914 p = str;
1915 for(;;) {
balrog609497a2008-01-14 02:56:53 +00001916 p = get_opt_name(buf, buf_size, p);
thse4bcb142007-12-02 04:51:10 +00001917 if (*p != '=')
1918 return -1;
1919 p++;
1920 for(i = 0; params[i] != NULL; i++)
1921 if (!strcmp(params[i], buf))
1922 break;
1923 if (params[i] == NULL)
1924 return -1;
balrog609497a2008-01-14 02:56:53 +00001925 p = get_opt_value(NULL, 0, p);
thse4bcb142007-12-02 04:51:10 +00001926 if (*p != ',')
1927 break;
1928 p++;
1929 }
1930 return 0;
1931}
1932
balrog1ae26a12008-09-28 23:19:47 +00001933/***********************************************************/
1934/* Bluetooth support */
1935static int nb_hcis;
1936static int cur_hci;
1937static struct HCIInfo *hci_table[MAX_NICS];
blueswir140ea94a2008-10-25 11:26:41 +00001938#if 0
balrog1ae26a12008-09-28 23:19:47 +00001939static struct bt_vlan_s {
1940 struct bt_scatternet_s net;
1941 int id;
1942 struct bt_vlan_s *next;
1943} *first_bt_vlan;
1944
1945/* find or alloc a new bluetooth "VLAN" */
blueswir1674bb262008-09-30 18:18:27 +00001946static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
balrog1ae26a12008-09-28 23:19:47 +00001947{
1948 struct bt_vlan_s **pvlan, *vlan;
1949 for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1950 if (vlan->id == id)
1951 return &vlan->net;
1952 }
1953 vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1954 vlan->id = id;
1955 pvlan = &first_bt_vlan;
1956 while (*pvlan != NULL)
1957 pvlan = &(*pvlan)->next;
1958 *pvlan = vlan;
1959 return &vlan->net;
1960}
blueswir140ea94a2008-10-25 11:26:41 +00001961#endif
balrog1ae26a12008-09-28 23:19:47 +00001962
1963static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1964{
1965}
1966
1967static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1968{
1969 return -ENOTSUP;
1970}
1971
1972static struct HCIInfo null_hci = {
1973 .cmd_send = null_hci_send,
1974 .sco_send = null_hci_send,
1975 .acl_send = null_hci_send,
1976 .bdaddr_set = null_hci_addr_set,
1977};
1978
1979struct HCIInfo *qemu_next_hci(void)
1980{
1981 if (cur_hci == nb_hcis)
1982 return &null_hci;
1983
1984 return hci_table[cur_hci++];
1985}
1986
1987/***********************************************************/
1988/* QEMU Block devices */
1989
balrog609497a2008-01-14 02:56:53 +00001990#define HD_ALIAS "index=%d,media=disk"
thse4bcb142007-12-02 04:51:10 +00001991#ifdef TARGET_PPC
1992#define CDROM_ALIAS "index=1,media=cdrom"
1993#else
1994#define CDROM_ALIAS "index=2,media=cdrom"
1995#endif
1996#define FD_ALIAS "index=%d,if=floppy"
balrog609497a2008-01-14 02:56:53 +00001997#define PFLASH_ALIAS "if=pflash"
1998#define MTD_ALIAS "if=mtd"
balrog9d413d12007-12-04 00:10:34 +00001999#define SD_ALIAS "index=0,if=sd"
thse4bcb142007-12-02 04:51:10 +00002000
balrog609497a2008-01-14 02:56:53 +00002001static int drive_add(const char *file, const char *fmt, ...)
thse4bcb142007-12-02 04:51:10 +00002002{
2003 va_list ap;
2004
2005 if (nb_drives_opt >= MAX_DRIVES) {
2006 fprintf(stderr, "qemu: too many drives\n");
2007 exit(1);
2008 }
2009
balrog609497a2008-01-14 02:56:53 +00002010 drives_opt[nb_drives_opt].file = file;
thse4bcb142007-12-02 04:51:10 +00002011 va_start(ap, fmt);
balrog609497a2008-01-14 02:56:53 +00002012 vsnprintf(drives_opt[nb_drives_opt].opt,
2013 sizeof(drives_opt[0].opt), fmt, ap);
thse4bcb142007-12-02 04:51:10 +00002014 va_end(ap);
2015
2016 return nb_drives_opt++;
2017}
2018
thsf60d39b2007-12-17 03:55:57 +00002019int drive_get_index(BlockInterfaceType type, int bus, int unit)
thse4bcb142007-12-02 04:51:10 +00002020{
2021 int index;
2022
2023 /* seek interface, bus and unit */
2024
2025 for (index = 0; index < nb_drives; index++)
thsf60d39b2007-12-17 03:55:57 +00002026 if (drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002027 drives_table[index].bus == bus &&
2028 drives_table[index].unit == unit)
2029 return index;
2030
2031 return -1;
2032}
2033
thsf60d39b2007-12-17 03:55:57 +00002034int drive_get_max_bus(BlockInterfaceType type)
thse4bcb142007-12-02 04:51:10 +00002035{
2036 int max_bus;
2037 int index;
2038
2039 max_bus = -1;
2040 for (index = 0; index < nb_drives; index++) {
thsf60d39b2007-12-17 03:55:57 +00002041 if(drives_table[index].type == type &&
thse4bcb142007-12-02 04:51:10 +00002042 drives_table[index].bus > max_bus)
2043 max_bus = drives_table[index].bus;
2044 }
2045 return max_bus;
2046}
2047
aurel32a1620fa2008-04-29 05:58:01 +00002048static void bdrv_format_print(void *opaque, const char *name)
2049{
2050 fprintf(stderr, " %s", name);
2051}
2052
balrog609497a2008-01-14 02:56:53 +00002053static int drive_init(struct drive_opt *arg, int snapshot,
2054 QEMUMachine *machine)
thse4bcb142007-12-02 04:51:10 +00002055{
2056 char buf[128];
2057 char file[1024];
balrogc8522bd2007-12-06 22:11:20 +00002058 char devname[128];
2059 const char *mediastr = "";
thsf60d39b2007-12-17 03:55:57 +00002060 BlockInterfaceType type;
thse4bcb142007-12-02 04:51:10 +00002061 enum { MEDIA_DISK, MEDIA_CDROM } media;
2062 int bus_id, unit_id;
2063 int cyls, heads, secs, translation;
2064 BlockDriverState *bdrv;
aurel321e72d3b2008-04-28 20:26:45 +00002065 BlockDriver *drv = NULL;
thse4bcb142007-12-02 04:51:10 +00002066 int max_devs;
2067 int index;
balrog33f00272007-12-24 14:33:24 +00002068 int cache;
2069 int bdrv_flags;
balrog609497a2008-01-14 02:56:53 +00002070 char *str = arg->opt;
blueswir17ccfb2e2008-09-14 06:45:34 +00002071 static const char * const params[] = { "bus", "unit", "if", "index",
2072 "cyls", "heads", "secs", "trans",
2073 "media", "snapshot", "file",
2074 "cache", "format", NULL };
thse4bcb142007-12-02 04:51:10 +00002075
2076 if (check_params(buf, sizeof(buf), params, str) < 0) {
balrogff993632008-02-10 13:21:25 +00002077 fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
thse4bcb142007-12-02 04:51:10 +00002078 buf, str);
2079 return -1;
2080 }
2081
2082 file[0] = 0;
2083 cyls = heads = secs = 0;
2084 bus_id = 0;
2085 unit_id = -1;
2086 translation = BIOS_ATA_TRANSLATION_AUTO;
2087 index = -1;
balrog33f00272007-12-24 14:33:24 +00002088 cache = 1;
thse4bcb142007-12-02 04:51:10 +00002089
blueswir1c9b1ae22008-09-28 18:55:17 +00002090 if (machine->use_scsi) {
thsf60d39b2007-12-17 03:55:57 +00002091 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002092 max_devs = MAX_SCSI_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002093 pstrcpy(devname, sizeof(devname), "scsi");
thse4bcb142007-12-02 04:51:10 +00002094 } else {
thsf60d39b2007-12-17 03:55:57 +00002095 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002096 max_devs = MAX_IDE_DEVS;
blueswir1363a37d2008-08-21 17:58:08 +00002097 pstrcpy(devname, sizeof(devname), "ide");
thse4bcb142007-12-02 04:51:10 +00002098 }
2099 media = MEDIA_DISK;
2100
2101 /* extract parameters */
2102
2103 if (get_param_value(buf, sizeof(buf), "bus", str)) {
2104 bus_id = strtol(buf, NULL, 0);
2105 if (bus_id < 0) {
2106 fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2107 return -1;
2108 }
2109 }
2110
2111 if (get_param_value(buf, sizeof(buf), "unit", str)) {
2112 unit_id = strtol(buf, NULL, 0);
2113 if (unit_id < 0) {
2114 fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2115 return -1;
2116 }
2117 }
2118
2119 if (get_param_value(buf, sizeof(buf), "if", str)) {
bellardae45d362008-06-11 09:44:44 +00002120 pstrcpy(devname, sizeof(devname), buf);
thse4bcb142007-12-02 04:51:10 +00002121 if (!strcmp(buf, "ide")) {
thsf60d39b2007-12-17 03:55:57 +00002122 type = IF_IDE;
thse4bcb142007-12-02 04:51:10 +00002123 max_devs = MAX_IDE_DEVS;
2124 } else if (!strcmp(buf, "scsi")) {
thsf60d39b2007-12-17 03:55:57 +00002125 type = IF_SCSI;
thse4bcb142007-12-02 04:51:10 +00002126 max_devs = MAX_SCSI_DEVS;
2127 } else if (!strcmp(buf, "floppy")) {
thsf60d39b2007-12-17 03:55:57 +00002128 type = IF_FLOPPY;
thse4bcb142007-12-02 04:51:10 +00002129 max_devs = 0;
2130 } else if (!strcmp(buf, "pflash")) {
thsf60d39b2007-12-17 03:55:57 +00002131 type = IF_PFLASH;
thse4bcb142007-12-02 04:51:10 +00002132 max_devs = 0;
2133 } else if (!strcmp(buf, "mtd")) {
thsf60d39b2007-12-17 03:55:57 +00002134 type = IF_MTD;
thse4bcb142007-12-02 04:51:10 +00002135 max_devs = 0;
2136 } else if (!strcmp(buf, "sd")) {
thsf60d39b2007-12-17 03:55:57 +00002137 type = IF_SD;
thse4bcb142007-12-02 04:51:10 +00002138 max_devs = 0;
2139 } else {
2140 fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2141 return -1;
2142 }
2143 }
2144
2145 if (get_param_value(buf, sizeof(buf), "index", str)) {
2146 index = strtol(buf, NULL, 0);
2147 if (index < 0) {
2148 fprintf(stderr, "qemu: '%s' invalid index\n", str);
2149 return -1;
2150 }
2151 }
2152
2153 if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2154 cyls = strtol(buf, NULL, 0);
2155 }
2156
2157 if (get_param_value(buf, sizeof(buf), "heads", str)) {
2158 heads = strtol(buf, NULL, 0);
2159 }
2160
2161 if (get_param_value(buf, sizeof(buf), "secs", str)) {
2162 secs = strtol(buf, NULL, 0);
2163 }
2164
2165 if (cyls || heads || secs) {
2166 if (cyls < 1 || cyls > 16383) {
2167 fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2168 return -1;
2169 }
2170 if (heads < 1 || heads > 16) {
2171 fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2172 return -1;
2173 }
2174 if (secs < 1 || secs > 63) {
2175 fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2176 return -1;
2177 }
2178 }
2179
2180 if (get_param_value(buf, sizeof(buf), "trans", str)) {
2181 if (!cyls) {
2182 fprintf(stderr,
2183 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2184 str);
2185 return -1;
2186 }
2187 if (!strcmp(buf, "none"))
2188 translation = BIOS_ATA_TRANSLATION_NONE;
2189 else if (!strcmp(buf, "lba"))
2190 translation = BIOS_ATA_TRANSLATION_LBA;
2191 else if (!strcmp(buf, "auto"))
2192 translation = BIOS_ATA_TRANSLATION_AUTO;
2193 else {
2194 fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2195 return -1;
2196 }
2197 }
2198
2199 if (get_param_value(buf, sizeof(buf), "media", str)) {
2200 if (!strcmp(buf, "disk")) {
2201 media = MEDIA_DISK;
2202 } else if (!strcmp(buf, "cdrom")) {
2203 if (cyls || secs || heads) {
2204 fprintf(stderr,
2205 "qemu: '%s' invalid physical CHS format\n", str);
2206 return -1;
2207 }
2208 media = MEDIA_CDROM;
2209 } else {
2210 fprintf(stderr, "qemu: '%s' invalid media\n", str);
2211 return -1;
2212 }
2213 }
2214
2215 if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2216 if (!strcmp(buf, "on"))
2217 snapshot = 1;
2218 else if (!strcmp(buf, "off"))
2219 snapshot = 0;
2220 else {
2221 fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2222 return -1;
2223 }
2224 }
2225
balrog33f00272007-12-24 14:33:24 +00002226 if (get_param_value(buf, sizeof(buf), "cache", str)) {
aliguori9f7965c2008-10-14 14:42:54 +00002227 if (!strcmp(buf, "off") || !strcmp(buf, "none"))
balrog33f00272007-12-24 14:33:24 +00002228 cache = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002229 else if (!strcmp(buf, "writethrough"))
balrog33f00272007-12-24 14:33:24 +00002230 cache = 1;
aliguori9f7965c2008-10-14 14:42:54 +00002231 else if (!strcmp(buf, "writeback"))
2232 cache = 2;
balrog33f00272007-12-24 14:33:24 +00002233 else {
2234 fprintf(stderr, "qemu: invalid cache option\n");
2235 return -1;
2236 }
2237 }
2238
aurel321e72d3b2008-04-28 20:26:45 +00002239 if (get_param_value(buf, sizeof(buf), "format", str)) {
aurel32a1620fa2008-04-29 05:58:01 +00002240 if (strcmp(buf, "?") == 0) {
2241 fprintf(stderr, "qemu: Supported formats:");
2242 bdrv_iterate_format(bdrv_format_print, NULL);
2243 fprintf(stderr, "\n");
2244 return -1;
2245 }
aurel321e72d3b2008-04-28 20:26:45 +00002246 drv = bdrv_find_format(buf);
2247 if (!drv) {
2248 fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2249 return -1;
2250 }
2251 }
2252
balrog609497a2008-01-14 02:56:53 +00002253 if (arg->file == NULL)
2254 get_param_value(file, sizeof(file), "file", str);
2255 else
2256 pstrcpy(file, sizeof(file), arg->file);
thse4bcb142007-12-02 04:51:10 +00002257
2258 /* compute bus and unit according index */
2259
2260 if (index != -1) {
2261 if (bus_id != 0 || unit_id != -1) {
2262 fprintf(stderr,
2263 "qemu: '%s' index cannot be used with bus and unit\n", str);
2264 return -1;
2265 }
2266 if (max_devs == 0)
2267 {
2268 unit_id = index;
2269 bus_id = 0;
2270 } else {
2271 unit_id = index % max_devs;
2272 bus_id = index / max_devs;
2273 }
2274 }
2275
2276 /* if user doesn't specify a unit_id,
2277 * try to find the first free
2278 */
2279
2280 if (unit_id == -1) {
2281 unit_id = 0;
thsf60d39b2007-12-17 03:55:57 +00002282 while (drive_get_index(type, bus_id, unit_id) != -1) {
thse4bcb142007-12-02 04:51:10 +00002283 unit_id++;
2284 if (max_devs && unit_id >= max_devs) {
2285 unit_id -= max_devs;
2286 bus_id++;
2287 }
2288 }
2289 }
2290
2291 /* check unit id */
2292
2293 if (max_devs && unit_id >= max_devs) {
2294 fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2295 str, unit_id, max_devs - 1);
2296 return -1;
2297 }
2298
2299 /*
2300 * ignore multiple definitions
2301 */
2302
thsf60d39b2007-12-17 03:55:57 +00002303 if (drive_get_index(type, bus_id, unit_id) != -1)
thse4bcb142007-12-02 04:51:10 +00002304 return 0;
2305
2306 /* init */
2307
thsf60d39b2007-12-17 03:55:57 +00002308 if (type == IF_IDE || type == IF_SCSI)
balrogc8522bd2007-12-06 22:11:20 +00002309 mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
balroge6198a72007-12-24 13:58:47 +00002310 if (max_devs)
2311 snprintf(buf, sizeof(buf), "%s%i%s%i",
2312 devname, bus_id, mediastr, unit_id);
2313 else
2314 snprintf(buf, sizeof(buf), "%s%s%i",
2315 devname, mediastr, unit_id);
thse4bcb142007-12-02 04:51:10 +00002316 bdrv = bdrv_new(buf);
2317 drives_table[nb_drives].bdrv = bdrv;
thsf60d39b2007-12-17 03:55:57 +00002318 drives_table[nb_drives].type = type;
thse4bcb142007-12-02 04:51:10 +00002319 drives_table[nb_drives].bus = bus_id;
2320 drives_table[nb_drives].unit = unit_id;
2321 nb_drives++;
2322
thsf60d39b2007-12-17 03:55:57 +00002323 switch(type) {
thse4bcb142007-12-02 04:51:10 +00002324 case IF_IDE:
2325 case IF_SCSI:
2326 switch(media) {
2327 case MEDIA_DISK:
2328 if (cyls != 0) {
2329 bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2330 bdrv_set_translation_hint(bdrv, translation);
2331 }
2332 break;
2333 case MEDIA_CDROM:
2334 bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2335 break;
2336 }
2337 break;
2338 case IF_SD:
2339 /* FIXME: This isn't really a floppy, but it's a reasonable
2340 approximation. */
2341 case IF_FLOPPY:
2342 bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2343 break;
2344 case IF_PFLASH:
2345 case IF_MTD:
2346 break;
2347 }
2348 if (!file[0])
2349 return 0;
balrog33f00272007-12-24 14:33:24 +00002350 bdrv_flags = 0;
aliguori9f7965c2008-10-14 14:42:54 +00002351 if (snapshot) {
balrog33f00272007-12-24 14:33:24 +00002352 bdrv_flags |= BDRV_O_SNAPSHOT;
aliguori9f7965c2008-10-14 14:42:54 +00002353 cache = 2; /* always use write-back with snapshot */
2354 }
2355 if (cache == 0) /* no caching */
2356 bdrv_flags |= BDRV_O_NOCACHE;
2357 else if (cache == 2) /* write-back */
2358 bdrv_flags |= BDRV_O_CACHE_WB;
aliguori83ab7952008-08-19 14:44:22 +00002359 if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
thse4bcb142007-12-02 04:51:10 +00002360 fprintf(stderr, "qemu: could not open disk image %s\n",
2361 file);
2362 return -1;
2363 }
2364 return 0;
2365}
2366
bellard330d0412003-07-26 18:11:40 +00002367/***********************************************************/
bellarda594cfb2005-11-06 16:13:29 +00002368/* USB devices */
2369
pbrook0d92ed32006-05-21 16:30:15 +00002370static USBPort *used_usb_ports;
2371static USBPort *free_usb_ports;
2372
2373/* ??? Maybe change this to register a hub to keep track of the topology. */
2374void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2375 usb_attachfn attach)
2376{
2377 port->opaque = opaque;
2378 port->index = index;
2379 port->attach = attach;
2380 port->next = free_usb_ports;
2381 free_usb_ports = port;
2382}
2383
aliguori4b096fc2008-08-21 19:28:55 +00002384int usb_device_add_dev(USBDevice *dev)
2385{
2386 USBPort *port;
2387
2388 /* Find a USB port to add the device to. */
2389 port = free_usb_ports;
2390 if (!port->next) {
2391 USBDevice *hub;
2392
2393 /* Create a new hub and chain it on. */
2394 free_usb_ports = NULL;
2395 port->next = used_usb_ports;
2396 used_usb_ports = port;
2397
2398 hub = usb_hub_init(VM_USB_HUB_SIZE);
2399 usb_attach(port, hub);
2400 port = free_usb_ports;
2401 }
2402
2403 free_usb_ports = port->next;
2404 port->next = used_usb_ports;
2405 used_usb_ports = port;
2406 usb_attach(port, dev);
2407 return 0;
2408}
2409
bellarda594cfb2005-11-06 16:13:29 +00002410static int usb_device_add(const char *devname)
2411{
2412 const char *p;
2413 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002414
pbrook0d92ed32006-05-21 16:30:15 +00002415 if (!free_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002416 return -1;
2417
2418 if (strstart(devname, "host:", &p)) {
2419 dev = usb_host_device_open(p);
bellarda594cfb2005-11-06 16:13:29 +00002420 } else if (!strcmp(devname, "mouse")) {
2421 dev = usb_mouse_init();
bellard09b26c52006-04-12 21:09:08 +00002422 } else if (!strcmp(devname, "tablet")) {
balrog47b2d332007-06-22 08:16:00 +00002423 dev = usb_tablet_init();
2424 } else if (!strcmp(devname, "keyboard")) {
2425 dev = usb_keyboard_init();
pbrook2e5d83b2006-05-25 23:58:51 +00002426 } else if (strstart(devname, "disk:", &p)) {
2427 dev = usb_msd_init(p);
balrogf6d2a312007-06-10 19:21:04 +00002428 } else if (!strcmp(devname, "wacom-tablet")) {
2429 dev = usb_wacom_init();
balroga7954212008-01-14 03:41:02 +00002430 } else if (strstart(devname, "serial:", &p)) {
2431 dev = usb_serial_init(p);
aurel322e4d9fb2008-04-08 06:01:02 +00002432#ifdef CONFIG_BRLAPI
2433 } else if (!strcmp(devname, "braille")) {
2434 dev = usb_baum_init();
2435#endif
balrog6c9f8862008-07-17 20:47:13 +00002436 } else if (strstart(devname, "net:", &p)) {
balrog9ad97e62008-07-29 13:16:31 +00002437 int nic = nb_nics;
balrog6c9f8862008-07-17 20:47:13 +00002438
balrog9ad97e62008-07-29 13:16:31 +00002439 if (net_client_init("nic", p) < 0)
balrog6c9f8862008-07-17 20:47:13 +00002440 return -1;
balrog9ad97e62008-07-29 13:16:31 +00002441 nd_table[nic].model = "usb";
2442 dev = usb_net_init(&nd_table[nic]);
bellarda594cfb2005-11-06 16:13:29 +00002443 } else {
2444 return -1;
2445 }
pbrook0d92ed32006-05-21 16:30:15 +00002446 if (!dev)
2447 return -1;
2448
aliguori4b096fc2008-08-21 19:28:55 +00002449 return usb_device_add_dev(dev);
bellarda594cfb2005-11-06 16:13:29 +00002450}
2451
aliguori1f3870a2008-08-21 19:27:48 +00002452int usb_device_del_addr(int bus_num, int addr)
bellarda594cfb2005-11-06 16:13:29 +00002453{
pbrook0d92ed32006-05-21 16:30:15 +00002454 USBPort *port;
2455 USBPort **lastp;
bellard059809e2006-07-19 18:06:15 +00002456 USBDevice *dev;
bellarda594cfb2005-11-06 16:13:29 +00002457
pbrook0d92ed32006-05-21 16:30:15 +00002458 if (!used_usb_ports)
bellarda594cfb2005-11-06 16:13:29 +00002459 return -1;
2460
bellarda594cfb2005-11-06 16:13:29 +00002461 if (bus_num != 0)
2462 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002463
2464 lastp = &used_usb_ports;
2465 port = used_usb_ports;
2466 while (port && port->dev->addr != addr) {
2467 lastp = &port->next;
2468 port = port->next;
bellarda594cfb2005-11-06 16:13:29 +00002469 }
pbrook0d92ed32006-05-21 16:30:15 +00002470
2471 if (!port)
bellarda594cfb2005-11-06 16:13:29 +00002472 return -1;
pbrook0d92ed32006-05-21 16:30:15 +00002473
bellard059809e2006-07-19 18:06:15 +00002474 dev = port->dev;
pbrook0d92ed32006-05-21 16:30:15 +00002475 *lastp = port->next;
2476 usb_attach(port, NULL);
bellard059809e2006-07-19 18:06:15 +00002477 dev->handle_destroy(dev);
pbrook0d92ed32006-05-21 16:30:15 +00002478 port->next = free_usb_ports;
2479 free_usb_ports = port;
bellarda594cfb2005-11-06 16:13:29 +00002480 return 0;
2481}
2482
aliguori1f3870a2008-08-21 19:27:48 +00002483static int usb_device_del(const char *devname)
2484{
2485 int bus_num, addr;
2486 const char *p;
2487
aliguori5d0c5752008-09-14 01:07:41 +00002488 if (strstart(devname, "host:", &p))
2489 return usb_host_device_close(p);
2490
aliguori1f3870a2008-08-21 19:27:48 +00002491 if (!used_usb_ports)
2492 return -1;
2493
2494 p = strchr(devname, '.');
2495 if (!p)
2496 return -1;
2497 bus_num = strtoul(devname, NULL, 0);
2498 addr = strtoul(p + 1, NULL, 0);
2499
2500 return usb_device_del_addr(bus_num, addr);
2501}
2502
bellarda594cfb2005-11-06 16:13:29 +00002503void do_usb_add(const char *devname)
2504{
aliguori4b096fc2008-08-21 19:28:55 +00002505 usb_device_add(devname);
bellarda594cfb2005-11-06 16:13:29 +00002506}
2507
2508void do_usb_del(const char *devname)
2509{
aliguori4b096fc2008-08-21 19:28:55 +00002510 usb_device_del(devname);
bellarda594cfb2005-11-06 16:13:29 +00002511}
2512
2513void usb_info(void)
2514{
2515 USBDevice *dev;
pbrook0d92ed32006-05-21 16:30:15 +00002516 USBPort *port;
bellarda594cfb2005-11-06 16:13:29 +00002517 const char *speed_str;
2518
pbrook0d92ed32006-05-21 16:30:15 +00002519 if (!usb_enabled) {
bellarda594cfb2005-11-06 16:13:29 +00002520 term_printf("USB support not enabled\n");
2521 return;
2522 }
2523
pbrook0d92ed32006-05-21 16:30:15 +00002524 for (port = used_usb_ports; port; port = port->next) {
2525 dev = port->dev;
2526 if (!dev)
2527 continue;
2528 switch(dev->speed) {
ths5fafdf22007-09-16 21:08:06 +00002529 case USB_SPEED_LOW:
2530 speed_str = "1.5";
pbrook0d92ed32006-05-21 16:30:15 +00002531 break;
ths5fafdf22007-09-16 21:08:06 +00002532 case USB_SPEED_FULL:
2533 speed_str = "12";
pbrook0d92ed32006-05-21 16:30:15 +00002534 break;
ths5fafdf22007-09-16 21:08:06 +00002535 case USB_SPEED_HIGH:
2536 speed_str = "480";
pbrook0d92ed32006-05-21 16:30:15 +00002537 break;
2538 default:
ths5fafdf22007-09-16 21:08:06 +00002539 speed_str = "?";
pbrook0d92ed32006-05-21 16:30:15 +00002540 break;
bellarda594cfb2005-11-06 16:13:29 +00002541 }
ths5fafdf22007-09-16 21:08:06 +00002542 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
bellard1f6e24e2006-06-26 21:00:51 +00002543 0, dev->addr, speed_str, dev->devname);
bellarda594cfb2005-11-06 16:13:29 +00002544 }
2545}
2546
bellardf7cce892004-12-08 22:21:25 +00002547/***********************************************************/
balrog201a51f2007-04-30 00:51:09 +00002548/* PCMCIA/Cardbus */
2549
2550static struct pcmcia_socket_entry_s {
2551 struct pcmcia_socket_s *socket;
2552 struct pcmcia_socket_entry_s *next;
2553} *pcmcia_sockets = 0;
2554
2555void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2556{
2557 struct pcmcia_socket_entry_s *entry;
2558
2559 entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2560 entry->socket = socket;
2561 entry->next = pcmcia_sockets;
2562 pcmcia_sockets = entry;
2563}
2564
2565void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2566{
2567 struct pcmcia_socket_entry_s *entry, **ptr;
2568
2569 ptr = &pcmcia_sockets;
2570 for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2571 if (entry->socket == socket) {
2572 *ptr = entry->next;
2573 qemu_free(entry);
2574 }
2575}
2576
2577void pcmcia_info(void)
2578{
2579 struct pcmcia_socket_entry_s *iter;
2580 if (!pcmcia_sockets)
2581 term_printf("No PCMCIA sockets\n");
2582
2583 for (iter = pcmcia_sockets; iter; iter = iter->next)
2584 term_printf("%s: %s\n", iter->socket->slot_string,
2585 iter->socket->attached ? iter->socket->card_string :
2586 "Empty");
2587}
2588
2589/***********************************************************/
ths2ff89792007-06-21 23:34:19 +00002590/* dumb display */
2591
2592static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2593{
2594}
2595
2596static void dumb_resize(DisplayState *ds, int w, int h)
2597{
2598}
2599
ths2ff89792007-06-21 23:34:19 +00002600static void dumb_display_init(DisplayState *ds)
2601{
2602 ds->data = NULL;
2603 ds->linesize = 0;
2604 ds->depth = 0;
2605 ds->dpy_update = dumb_update;
2606 ds->dpy_resize = dumb_resize;
aliguori2ad1a432008-10-31 20:34:40 +00002607 ds->dpy_refresh = NULL;
2608 ds->gui_timer_interval = 0;
aliguoribcfad702008-08-21 20:08:55 +00002609 ds->idle = 1;
ths2ff89792007-06-21 23:34:19 +00002610}
2611
2612/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002613/* I/O handling */
bellard0824d6f2003-06-24 13:42:40 +00002614
bellardc4b1fcc2004-03-14 21:44:30 +00002615#define MAX_IO_HANDLERS 64
2616
2617typedef struct IOHandlerRecord {
2618 int fd;
bellard7c9d8e02005-11-15 22:16:05 +00002619 IOCanRWHandler *fd_read_poll;
2620 IOHandler *fd_read;
2621 IOHandler *fd_write;
thscafffd42007-02-28 21:59:44 +00002622 int deleted;
bellardc4b1fcc2004-03-14 21:44:30 +00002623 void *opaque;
2624 /* temporary data */
2625 struct pollfd *ufd;
bellard8a7ddc32004-03-31 19:00:16 +00002626 struct IOHandlerRecord *next;
bellardc4b1fcc2004-03-14 21:44:30 +00002627} IOHandlerRecord;
2628
bellard8a7ddc32004-03-31 19:00:16 +00002629static IOHandlerRecord *first_io_handler;
bellardc4b1fcc2004-03-14 21:44:30 +00002630
bellard7c9d8e02005-11-15 22:16:05 +00002631/* XXX: fd_read_poll should be suppressed, but an API change is
2632 necessary in the character devices to suppress fd_can_read(). */
ths5fafdf22007-09-16 21:08:06 +00002633int qemu_set_fd_handler2(int fd,
2634 IOCanRWHandler *fd_read_poll,
2635 IOHandler *fd_read,
2636 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002637 void *opaque)
bellardb4608c02003-06-27 17:34:32 +00002638{
bellard8a7ddc32004-03-31 19:00:16 +00002639 IOHandlerRecord **pioh, *ioh;
2640
bellard7c9d8e02005-11-15 22:16:05 +00002641 if (!fd_read && !fd_write) {
2642 pioh = &first_io_handler;
2643 for(;;) {
2644 ioh = *pioh;
2645 if (ioh == NULL)
2646 break;
2647 if (ioh->fd == fd) {
thscafffd42007-02-28 21:59:44 +00002648 ioh->deleted = 1;
bellard7c9d8e02005-11-15 22:16:05 +00002649 break;
2650 }
2651 pioh = &ioh->next;
bellard8a7ddc32004-03-31 19:00:16 +00002652 }
bellard7c9d8e02005-11-15 22:16:05 +00002653 } else {
2654 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2655 if (ioh->fd == fd)
2656 goto found;
2657 }
2658 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2659 if (!ioh)
2660 return -1;
2661 ioh->next = first_io_handler;
2662 first_io_handler = ioh;
2663 found:
2664 ioh->fd = fd;
2665 ioh->fd_read_poll = fd_read_poll;
2666 ioh->fd_read = fd_read;
2667 ioh->fd_write = fd_write;
2668 ioh->opaque = opaque;
thscafffd42007-02-28 21:59:44 +00002669 ioh->deleted = 0;
bellard8a7ddc32004-03-31 19:00:16 +00002670 }
bellard7c9d8e02005-11-15 22:16:05 +00002671 return 0;
2672}
2673
ths5fafdf22007-09-16 21:08:06 +00002674int qemu_set_fd_handler(int fd,
2675 IOHandler *fd_read,
2676 IOHandler *fd_write,
bellard7c9d8e02005-11-15 22:16:05 +00002677 void *opaque)
2678{
2679 return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
bellardb4608c02003-06-27 17:34:32 +00002680}
2681
aliguori56f3a5d2008-10-31 18:07:17 +00002682#ifdef _WIN32
bellard8a7ddc32004-03-31 19:00:16 +00002683/***********************************************************/
bellardf3311102006-04-12 20:21:17 +00002684/* Polling handling */
2685
2686typedef struct PollingEntry {
2687 PollingFunc *func;
2688 void *opaque;
2689 struct PollingEntry *next;
2690} PollingEntry;
2691
2692static PollingEntry *first_polling_entry;
2693
2694int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2695{
2696 PollingEntry **ppe, *pe;
2697 pe = qemu_mallocz(sizeof(PollingEntry));
2698 if (!pe)
2699 return -1;
2700 pe->func = func;
2701 pe->opaque = opaque;
2702 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2703 *ppe = pe;
2704 return 0;
2705}
2706
2707void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2708{
2709 PollingEntry **ppe, *pe;
2710 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2711 pe = *ppe;
2712 if (pe->func == func && pe->opaque == opaque) {
2713 *ppe = pe->next;
2714 qemu_free(pe);
2715 break;
2716 }
2717 }
2718}
2719
bellarda18e5242006-06-25 17:18:27 +00002720/***********************************************************/
2721/* Wait objects support */
2722typedef struct WaitObjects {
2723 int num;
2724 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2725 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2726 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2727} WaitObjects;
2728
2729static WaitObjects wait_objects = {0};
ths3b46e622007-09-17 08:09:54 +00002730
bellarda18e5242006-06-25 17:18:27 +00002731int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2732{
2733 WaitObjects *w = &wait_objects;
2734
2735 if (w->num >= MAXIMUM_WAIT_OBJECTS)
2736 return -1;
2737 w->events[w->num] = handle;
2738 w->func[w->num] = func;
2739 w->opaque[w->num] = opaque;
2740 w->num++;
2741 return 0;
2742}
2743
2744void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2745{
2746 int i, found;
2747 WaitObjects *w = &wait_objects;
2748
2749 found = 0;
2750 for (i = 0; i < w->num; i++) {
2751 if (w->events[i] == handle)
2752 found = 1;
2753 if (found) {
2754 w->events[i] = w->events[i + 1];
2755 w->func[i] = w->func[i + 1];
2756 w->opaque[i] = w->opaque[i + 1];
ths3b46e622007-09-17 08:09:54 +00002757 }
bellarda18e5242006-06-25 17:18:27 +00002758 }
2759 if (found)
2760 w->num--;
2761}
2762#endif
2763
aliguori210f41b2008-10-13 03:13:12 +00002764#define SELF_ANNOUNCE_ROUNDS 5
2765#define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
2766//#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
2767#define EXPERIMENTAL_MAGIC 0xf1f23f4f
2768
2769static int announce_self_create(uint8_t *buf,
2770 uint8_t *mac_addr)
2771{
2772 uint32_t magic = EXPERIMENTAL_MAGIC;
2773 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
2774
2775 /* FIXME: should we send a different packet (arp/rarp/ping)? */
2776
2777 memset(buf, 0xff, 6); /* h_dst */
2778 memcpy(buf + 6, mac_addr, 6); /* h_src */
2779 memcpy(buf + 12, &proto, 2); /* h_proto */
2780 memcpy(buf + 14, &magic, 4); /* magic */
2781
2782 return 18; /* len */
2783}
2784
2785void qemu_announce_self(void)
2786{
2787 int i, j, len;
2788 VLANState *vlan;
2789 VLANClientState *vc;
2790 uint8_t buf[256];
2791
2792 for (i = 0; i < nb_nics; i++) {
2793 len = announce_self_create(buf, nd_table[i].macaddr);
2794 vlan = nd_table[i].vlan;
2795 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
aliguori17e90972008-10-24 14:11:41 +00002796 for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++)
2797 vc->fd_read(vc->opaque, buf, len);
aliguori210f41b2008-10-13 03:13:12 +00002798 }
2799 }
2800}
2801
bellardf3311102006-04-12 20:21:17 +00002802/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00002803/* savevm/loadvm support */
bellardb4608c02003-06-27 17:34:32 +00002804
bellardfaea38e2006-08-05 21:31:00 +00002805#define IO_BUF_SIZE 32768
2806
2807struct QEMUFile {
aliguori5dafc532008-09-29 16:25:16 +00002808 QEMUFilePutBufferFunc *put_buffer;
2809 QEMUFileGetBufferFunc *get_buffer;
2810 QEMUFileCloseFunc *close;
2811 QEMUFileRateLimit *rate_limit;
2812 void *opaque;
aliguori871d2f02008-10-13 03:07:56 +00002813 int is_write;
aliguori5dafc532008-09-29 16:25:16 +00002814
bellardfaea38e2006-08-05 21:31:00 +00002815 int64_t buf_offset; /* start of buffer when writing, end of buffer
2816 when reading */
2817 int buf_index;
2818 int buf_size; /* 0 when writing */
2819 uint8_t buf[IO_BUF_SIZE];
aliguori871d2f02008-10-13 03:07:56 +00002820
2821 int has_error;
bellardfaea38e2006-08-05 21:31:00 +00002822};
2823
aliguoric1d36662008-10-24 21:55:17 +00002824typedef struct QEMUFileSocket
aliguori5dafc532008-09-29 16:25:16 +00002825{
2826 int fd;
2827 QEMUFile *file;
aliguoric1d36662008-10-24 21:55:17 +00002828} QEMUFileSocket;
aliguori5dafc532008-09-29 16:25:16 +00002829
aliguoric1d36662008-10-24 21:55:17 +00002830static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguori5dafc532008-09-29 16:25:16 +00002831{
aliguoric1d36662008-10-24 21:55:17 +00002832 QEMUFileSocket *s = opaque;
aliguori5dafc532008-09-29 16:25:16 +00002833 ssize_t len;
2834
2835 do {
aliguoric1d36662008-10-24 21:55:17 +00002836 len = recv(s->fd, buf, size, 0);
2837 } while (len == -1 && socket_error() == EINTR);
aliguori5dafc532008-09-29 16:25:16 +00002838
2839 if (len == -1)
aliguoric1d36662008-10-24 21:55:17 +00002840 len = -socket_error();
aliguori5dafc532008-09-29 16:25:16 +00002841
2842 return len;
2843}
2844
aliguoric1d36662008-10-24 21:55:17 +00002845static int socket_close(void *opaque)
aliguori5dafc532008-09-29 16:25:16 +00002846{
aliguoric1d36662008-10-24 21:55:17 +00002847 QEMUFileSocket *s = opaque;
aliguori5dafc532008-09-29 16:25:16 +00002848 qemu_free(s);
2849 return 0;
2850}
2851
aliguoric1d36662008-10-24 21:55:17 +00002852QEMUFile *qemu_fopen_socket(int fd)
aliguori5dafc532008-09-29 16:25:16 +00002853{
aliguoric1d36662008-10-24 21:55:17 +00002854 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
aliguori5dafc532008-09-29 16:25:16 +00002855
2856 if (s == NULL)
2857 return NULL;
2858
2859 s->fd = fd;
aliguoric1d36662008-10-24 21:55:17 +00002860 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
aliguori5dafc532008-09-29 16:25:16 +00002861 return s->file;
2862}
2863
2864typedef struct QEMUFileStdio
2865{
2866 FILE *outfile;
2867} QEMUFileStdio;
2868
aliguori871d2f02008-10-13 03:07:56 +00002869static int file_put_buffer(void *opaque, const uint8_t *buf,
aliguori5dafc532008-09-29 16:25:16 +00002870 int64_t pos, int size)
2871{
2872 QEMUFileStdio *s = opaque;
2873 fseek(s->outfile, pos, SEEK_SET);
2874 fwrite(buf, 1, size, s->outfile);
aliguori871d2f02008-10-13 03:07:56 +00002875 return size;
aliguori5dafc532008-09-29 16:25:16 +00002876}
2877
2878static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
2879{
2880 QEMUFileStdio *s = opaque;
2881 fseek(s->outfile, pos, SEEK_SET);
2882 return fread(buf, 1, size, s->outfile);
2883}
2884
2885static int file_close(void *opaque)
2886{
2887 QEMUFileStdio *s = opaque;
2888 fclose(s->outfile);
2889 qemu_free(s);
2890 return 0;
2891}
2892
bellardfaea38e2006-08-05 21:31:00 +00002893QEMUFile *qemu_fopen(const char *filename, const char *mode)
2894{
aliguori5dafc532008-09-29 16:25:16 +00002895 QEMUFileStdio *s;
bellardfaea38e2006-08-05 21:31:00 +00002896
aliguori5dafc532008-09-29 16:25:16 +00002897 s = qemu_mallocz(sizeof(QEMUFileStdio));
2898 if (!s)
bellardfaea38e2006-08-05 21:31:00 +00002899 return NULL;
aliguori5dafc532008-09-29 16:25:16 +00002900
2901 s->outfile = fopen(filename, mode);
2902 if (!s->outfile)
bellardfaea38e2006-08-05 21:31:00 +00002903 goto fail;
aliguori5dafc532008-09-29 16:25:16 +00002904
2905 if (!strcmp(mode, "wb"))
2906 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
2907 else if (!strcmp(mode, "rb"))
2908 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
2909
2910fail:
2911 if (s->outfile)
2912 fclose(s->outfile);
2913 qemu_free(s);
bellardfaea38e2006-08-05 21:31:00 +00002914 return NULL;
2915}
2916
aliguori5dafc532008-09-29 16:25:16 +00002917typedef struct QEMUFileBdrv
2918{
2919 BlockDriverState *bs;
2920 int64_t base_offset;
2921} QEMUFileBdrv;
2922
aliguori871d2f02008-10-13 03:07:56 +00002923static int bdrv_put_buffer(void *opaque, const uint8_t *buf,
2924 int64_t pos, int size)
aliguori5dafc532008-09-29 16:25:16 +00002925{
2926 QEMUFileBdrv *s = opaque;
2927 bdrv_pwrite(s->bs, s->base_offset + pos, buf, size);
aliguori871d2f02008-10-13 03:07:56 +00002928 return size;
aliguori5dafc532008-09-29 16:25:16 +00002929}
2930
2931static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
2932{
2933 QEMUFileBdrv *s = opaque;
2934 return bdrv_pread(s->bs, s->base_offset + pos, buf, size);
2935}
2936
2937static int bdrv_fclose(void *opaque)
2938{
2939 QEMUFileBdrv *s = opaque;
2940 qemu_free(s);
2941 return 0;
2942}
2943
blueswir1674bb262008-09-30 18:18:27 +00002944static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
aliguori5dafc532008-09-29 16:25:16 +00002945{
2946 QEMUFileBdrv *s;
2947
2948 s = qemu_mallocz(sizeof(QEMUFileBdrv));
2949 if (!s)
2950 return NULL;
2951
2952 s->bs = bs;
2953 s->base_offset = offset;
2954
2955 if (is_writable)
2956 return qemu_fopen_ops(s, bdrv_put_buffer, NULL, bdrv_fclose, NULL);
2957
2958 return qemu_fopen_ops(s, NULL, bdrv_get_buffer, bdrv_fclose, NULL);
2959}
2960
2961QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
2962 QEMUFileGetBufferFunc *get_buffer,
2963 QEMUFileCloseFunc *close,
2964 QEMUFileRateLimit *rate_limit)
bellardfaea38e2006-08-05 21:31:00 +00002965{
2966 QEMUFile *f;
2967
2968 f = qemu_mallocz(sizeof(QEMUFile));
2969 if (!f)
2970 return NULL;
aliguori5dafc532008-09-29 16:25:16 +00002971
2972 f->opaque = opaque;
2973 f->put_buffer = put_buffer;
2974 f->get_buffer = get_buffer;
2975 f->close = close;
2976 f->rate_limit = rate_limit;
aliguori871d2f02008-10-13 03:07:56 +00002977 f->is_write = 0;
aliguori5dafc532008-09-29 16:25:16 +00002978
bellardfaea38e2006-08-05 21:31:00 +00002979 return f;
2980}
2981
aliguori871d2f02008-10-13 03:07:56 +00002982int qemu_file_has_error(QEMUFile *f)
2983{
2984 return f->has_error;
2985}
2986
bellardfaea38e2006-08-05 21:31:00 +00002987void qemu_fflush(QEMUFile *f)
2988{
aliguori5dafc532008-09-29 16:25:16 +00002989 if (!f->put_buffer)
bellardfaea38e2006-08-05 21:31:00 +00002990 return;
aliguori5dafc532008-09-29 16:25:16 +00002991
aliguori871d2f02008-10-13 03:07:56 +00002992 if (f->is_write && f->buf_index > 0) {
2993 int len;
2994
2995 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
2996 if (len > 0)
2997 f->buf_offset += f->buf_index;
2998 else
2999 f->has_error = 1;
bellardfaea38e2006-08-05 21:31:00 +00003000 f->buf_index = 0;
3001 }
3002}
3003
3004static void qemu_fill_buffer(QEMUFile *f)
3005{
3006 int len;
3007
aliguori5dafc532008-09-29 16:25:16 +00003008 if (!f->get_buffer)
bellardfaea38e2006-08-05 21:31:00 +00003009 return;
aliguori5dafc532008-09-29 16:25:16 +00003010
aliguori871d2f02008-10-13 03:07:56 +00003011 if (f->is_write)
3012 abort();
aliguori5dafc532008-09-29 16:25:16 +00003013
aliguori871d2f02008-10-13 03:07:56 +00003014 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
3015 if (len > 0) {
3016 f->buf_index = 0;
3017 f->buf_size = len;
3018 f->buf_offset += len;
3019 } else if (len != -EAGAIN)
3020 f->has_error = 1;
bellardfaea38e2006-08-05 21:31:00 +00003021}
3022
aliguori5dafc532008-09-29 16:25:16 +00003023int qemu_fclose(QEMUFile *f)
bellardfaea38e2006-08-05 21:31:00 +00003024{
aliguori5dafc532008-09-29 16:25:16 +00003025 int ret = 0;
3026 qemu_fflush(f);
3027 if (f->close)
3028 ret = f->close(f->opaque);
bellardfaea38e2006-08-05 21:31:00 +00003029 qemu_free(f);
aliguori5dafc532008-09-29 16:25:16 +00003030 return ret;
3031}
3032
3033void qemu_file_put_notify(QEMUFile *f)
3034{
3035 f->put_buffer(f->opaque, NULL, 0, 0);
bellardfaea38e2006-08-05 21:31:00 +00003036}
3037
blueswir12ca83a82008-09-25 20:24:19 +00003038void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
bellard8a7ddc32004-03-31 19:00:16 +00003039{
blueswir12ca83a82008-09-25 20:24:19 +00003040 int l;
aliguori871d2f02008-10-13 03:07:56 +00003041
3042 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
3043 fprintf(stderr,
3044 "Attempted to write to buffer while read buffer is not empty\n");
3045 abort();
3046 }
3047
3048 while (!f->has_error && size > 0) {
bellardfaea38e2006-08-05 21:31:00 +00003049 l = IO_BUF_SIZE - f->buf_index;
3050 if (l > size)
3051 l = size;
3052 memcpy(f->buf + f->buf_index, buf, l);
aliguori871d2f02008-10-13 03:07:56 +00003053 f->is_write = 1;
bellardfaea38e2006-08-05 21:31:00 +00003054 f->buf_index += l;
3055 buf += l;
3056 size -= l;
3057 if (f->buf_index >= IO_BUF_SIZE)
3058 qemu_fflush(f);
3059 }
bellard8a7ddc32004-03-31 19:00:16 +00003060}
3061
blueswir12ca83a82008-09-25 20:24:19 +00003062void qemu_put_byte(QEMUFile *f, int v)
bellard8a7ddc32004-03-31 19:00:16 +00003063{
aliguori871d2f02008-10-13 03:07:56 +00003064 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
3065 fprintf(stderr,
3066 "Attempted to write to buffer while read buffer is not empty\n");
3067 abort();
3068 }
3069
bellardfaea38e2006-08-05 21:31:00 +00003070 f->buf[f->buf_index++] = v;
aliguori871d2f02008-10-13 03:07:56 +00003071 f->is_write = 1;
bellardfaea38e2006-08-05 21:31:00 +00003072 if (f->buf_index >= IO_BUF_SIZE)
3073 qemu_fflush(f);
3074}
3075
blueswir12ca83a82008-09-25 20:24:19 +00003076int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
bellardfaea38e2006-08-05 21:31:00 +00003077{
blueswir12ca83a82008-09-25 20:24:19 +00003078 int size, l;
bellardfaea38e2006-08-05 21:31:00 +00003079
aliguori871d2f02008-10-13 03:07:56 +00003080 if (f->is_write)
3081 abort();
3082
bellardfaea38e2006-08-05 21:31:00 +00003083 size = size1;
3084 while (size > 0) {
3085 l = f->buf_size - f->buf_index;
3086 if (l == 0) {
3087 qemu_fill_buffer(f);
3088 l = f->buf_size - f->buf_index;
3089 if (l == 0)
3090 break;
3091 }
3092 if (l > size)
3093 l = size;
3094 memcpy(buf, f->buf + f->buf_index, l);
3095 f->buf_index += l;
3096 buf += l;
3097 size -= l;
3098 }
3099 return size1 - size;
3100}
3101
blueswir12ca83a82008-09-25 20:24:19 +00003102int qemu_get_byte(QEMUFile *f)
bellardfaea38e2006-08-05 21:31:00 +00003103{
aliguori871d2f02008-10-13 03:07:56 +00003104 if (f->is_write)
3105 abort();
3106
bellardfaea38e2006-08-05 21:31:00 +00003107 if (f->buf_index >= f->buf_size) {
3108 qemu_fill_buffer(f);
3109 if (f->buf_index >= f->buf_size)
3110 return 0;
3111 }
3112 return f->buf[f->buf_index++];
3113}
3114
3115int64_t qemu_ftell(QEMUFile *f)
3116{
3117 return f->buf_offset - f->buf_size + f->buf_index;
3118}
3119
3120int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
3121{
3122 if (whence == SEEK_SET) {
3123 /* nothing to do */
3124 } else if (whence == SEEK_CUR) {
3125 pos += qemu_ftell(f);
3126 } else {
3127 /* SEEK_END not supported */
3128 return -1;
3129 }
aliguori5dafc532008-09-29 16:25:16 +00003130 if (f->put_buffer) {
bellardfaea38e2006-08-05 21:31:00 +00003131 qemu_fflush(f);
3132 f->buf_offset = pos;
3133 } else {
3134 f->buf_offset = pos;
3135 f->buf_index = 0;
3136 f->buf_size = 0;
3137 }
3138 return pos;
bellard8a7ddc32004-03-31 19:00:16 +00003139}
3140
aliguori5dafc532008-09-29 16:25:16 +00003141int qemu_file_rate_limit(QEMUFile *f)
3142{
3143 if (f->rate_limit)
3144 return f->rate_limit(f->opaque);
3145
3146 return 0;
3147}
3148
blueswir12ca83a82008-09-25 20:24:19 +00003149void qemu_put_be16(QEMUFile *f, unsigned int v)
bellard8a7ddc32004-03-31 19:00:16 +00003150{
3151 qemu_put_byte(f, v >> 8);
3152 qemu_put_byte(f, v);
3153}
3154
blueswir12ca83a82008-09-25 20:24:19 +00003155void qemu_put_be32(QEMUFile *f, unsigned int v)
bellard8a7ddc32004-03-31 19:00:16 +00003156{
3157 qemu_put_byte(f, v >> 24);
3158 qemu_put_byte(f, v >> 16);
3159 qemu_put_byte(f, v >> 8);
3160 qemu_put_byte(f, v);
3161}
3162
3163void qemu_put_be64(QEMUFile *f, uint64_t v)
3164{
3165 qemu_put_be32(f, v >> 32);
3166 qemu_put_be32(f, v);
3167}
3168
blueswir12ca83a82008-09-25 20:24:19 +00003169unsigned int qemu_get_be16(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00003170{
blueswir12ca83a82008-09-25 20:24:19 +00003171 unsigned int v;
bellard8a7ddc32004-03-31 19:00:16 +00003172 v = qemu_get_byte(f) << 8;
3173 v |= qemu_get_byte(f);
3174 return v;
3175}
3176
blueswir12ca83a82008-09-25 20:24:19 +00003177unsigned int qemu_get_be32(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00003178{
blueswir12ca83a82008-09-25 20:24:19 +00003179 unsigned int v;
bellard8a7ddc32004-03-31 19:00:16 +00003180 v = qemu_get_byte(f) << 24;
3181 v |= qemu_get_byte(f) << 16;
3182 v |= qemu_get_byte(f) << 8;
3183 v |= qemu_get_byte(f);
3184 return v;
3185}
3186
3187uint64_t qemu_get_be64(QEMUFile *f)
3188{
3189 uint64_t v;
3190 v = (uint64_t)qemu_get_be32(f) << 32;
3191 v |= qemu_get_be32(f);
3192 return v;
3193}
3194
bellard8a7ddc32004-03-31 19:00:16 +00003195typedef struct SaveStateEntry {
3196 char idstr[256];
3197 int instance_id;
3198 int version_id;
aliguori9366f412008-10-06 14:53:52 +00003199 int section_id;
3200 SaveLiveStateHandler *save_live_state;
bellard8a7ddc32004-03-31 19:00:16 +00003201 SaveStateHandler *save_state;
3202 LoadStateHandler *load_state;
3203 void *opaque;
3204 struct SaveStateEntry *next;
3205} SaveStateEntry;
3206
3207static SaveStateEntry *first_se;
3208
pbrook18be5182008-07-01 21:31:54 +00003209/* TODO: Individual devices generally have very little idea about the rest
ths18fdb1c2008-07-18 18:02:34 +00003210 of the system, so instance_id should be removed/replaced.
3211 Meanwhile pass -1 as instance_id if you do not already have a clearly
3212 distinguishing id for all instances of your device class. */
aliguori9366f412008-10-06 14:53:52 +00003213int register_savevm_live(const char *idstr,
3214 int instance_id,
3215 int version_id,
3216 SaveLiveStateHandler *save_live_state,
3217 SaveStateHandler *save_state,
3218 LoadStateHandler *load_state,
3219 void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003220{
3221 SaveStateEntry *se, **pse;
aliguori9366f412008-10-06 14:53:52 +00003222 static int global_section_id;
bellard8a7ddc32004-03-31 19:00:16 +00003223
3224 se = qemu_malloc(sizeof(SaveStateEntry));
3225 if (!se)
3226 return -1;
3227 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
pbrook18be5182008-07-01 21:31:54 +00003228 se->instance_id = (instance_id == -1) ? 0 : instance_id;
bellard8a7ddc32004-03-31 19:00:16 +00003229 se->version_id = version_id;
aliguori9366f412008-10-06 14:53:52 +00003230 se->section_id = global_section_id++;
3231 se->save_live_state = save_live_state;
bellard8a7ddc32004-03-31 19:00:16 +00003232 se->save_state = save_state;
3233 se->load_state = load_state;
3234 se->opaque = opaque;
3235 se->next = NULL;
3236
3237 /* add at the end of list */
3238 pse = &first_se;
pbrook18be5182008-07-01 21:31:54 +00003239 while (*pse != NULL) {
3240 if (instance_id == -1
3241 && strcmp(se->idstr, (*pse)->idstr) == 0
3242 && se->instance_id <= (*pse)->instance_id)
3243 se->instance_id = (*pse)->instance_id + 1;
bellard8a7ddc32004-03-31 19:00:16 +00003244 pse = &(*pse)->next;
pbrook18be5182008-07-01 21:31:54 +00003245 }
bellard8a7ddc32004-03-31 19:00:16 +00003246 *pse = se;
3247 return 0;
3248}
3249
aliguori9366f412008-10-06 14:53:52 +00003250int register_savevm(const char *idstr,
3251 int instance_id,
3252 int version_id,
3253 SaveStateHandler *save_state,
3254 LoadStateHandler *load_state,
3255 void *opaque)
3256{
3257 return register_savevm_live(idstr, instance_id, version_id,
3258 NULL, save_state, load_state, opaque);
3259}
bellard8a7ddc32004-03-31 19:00:16 +00003260
aliguori9366f412008-10-06 14:53:52 +00003261#define QEMU_VM_FILE_MAGIC 0x5145564d
3262#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
3263#define QEMU_VM_FILE_VERSION 0x00000003
3264
3265#define QEMU_VM_EOF 0x00
3266#define QEMU_VM_SECTION_START 0x01
3267#define QEMU_VM_SECTION_PART 0x02
3268#define QEMU_VM_SECTION_END 0x03
3269#define QEMU_VM_SECTION_FULL 0x04
3270
3271int qemu_savevm_state_begin(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00003272{
3273 SaveStateEntry *se;
bellard313aa562003-08-10 21:52:11 +00003274
bellard8a7ddc32004-03-31 19:00:16 +00003275 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
3276 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
3277
aliguori9366f412008-10-06 14:53:52 +00003278 for (se = first_se; se != NULL; se = se->next) {
3279 int len;
3280
3281 if (se->save_live_state == NULL)
3282 continue;
3283
3284 /* Section type */
3285 qemu_put_byte(f, QEMU_VM_SECTION_START);
3286 qemu_put_be32(f, se->section_id);
aurel32d978c022008-06-18 22:10:21 +00003287
bellard8a7ddc32004-03-31 19:00:16 +00003288 /* ID string */
3289 len = strlen(se->idstr);
3290 qemu_put_byte(f, len);
thsffe8ab82007-12-16 03:16:05 +00003291 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
bellard8a7ddc32004-03-31 19:00:16 +00003292
3293 qemu_put_be32(f, se->instance_id);
3294 qemu_put_be32(f, se->version_id);
3295
aliguori9366f412008-10-06 14:53:52 +00003296 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
bellard8a7ddc32004-03-31 19:00:16 +00003297 }
3298
aliguori871d2f02008-10-13 03:07:56 +00003299 if (qemu_file_has_error(f))
3300 return -EIO;
3301
aliguori9366f412008-10-06 14:53:52 +00003302 return 0;
3303}
3304
3305int qemu_savevm_state_iterate(QEMUFile *f)
3306{
3307 SaveStateEntry *se;
aliguori475e4272008-10-06 20:21:51 +00003308 int ret = 1;
aliguori9366f412008-10-06 14:53:52 +00003309
3310 for (se = first_se; se != NULL; se = se->next) {
3311 if (se->save_live_state == NULL)
3312 continue;
3313
3314 /* Section type */
3315 qemu_put_byte(f, QEMU_VM_SECTION_PART);
3316 qemu_put_be32(f, se->section_id);
3317
aliguori475e4272008-10-06 20:21:51 +00003318 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
aliguori9366f412008-10-06 14:53:52 +00003319 }
3320
3321 if (ret)
3322 return 1;
3323
aliguori871d2f02008-10-13 03:07:56 +00003324 if (qemu_file_has_error(f))
3325 return -EIO;
3326
aliguori9366f412008-10-06 14:53:52 +00003327 return 0;
3328}
3329
3330int qemu_savevm_state_complete(QEMUFile *f)
3331{
3332 SaveStateEntry *se;
3333
3334 for (se = first_se; se != NULL; se = se->next) {
3335 if (se->save_live_state == NULL)
3336 continue;
3337
3338 /* Section type */
3339 qemu_put_byte(f, QEMU_VM_SECTION_END);
3340 qemu_put_be32(f, se->section_id);
3341
3342 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
3343 }
3344
3345 for(se = first_se; se != NULL; se = se->next) {
3346 int len;
3347
3348 if (se->save_state == NULL)
3349 continue;
3350
3351 /* Section type */
3352 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
3353 qemu_put_be32(f, se->section_id);
3354
3355 /* ID string */
3356 len = strlen(se->idstr);
3357 qemu_put_byte(f, len);
3358 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
3359
3360 qemu_put_be32(f, se->instance_id);
3361 qemu_put_be32(f, se->version_id);
3362
3363 se->save_state(f, se->opaque);
3364 }
3365
3366 qemu_put_byte(f, QEMU_VM_EOF);
3367
aliguori871d2f02008-10-13 03:07:56 +00003368 if (qemu_file_has_error(f))
3369 return -EIO;
3370
aliguori9366f412008-10-06 14:53:52 +00003371 return 0;
3372}
3373
3374int qemu_savevm_state(QEMUFile *f)
3375{
3376 int saved_vm_running;
3377 int ret;
3378
3379 saved_vm_running = vm_running;
3380 vm_stop(0);
3381
aliguori5bb79102008-10-13 03:12:02 +00003382 bdrv_flush_all();
3383
aliguori9366f412008-10-06 14:53:52 +00003384 ret = qemu_savevm_state_begin(f);
3385 if (ret < 0)
3386 goto out;
3387
3388 do {
3389 ret = qemu_savevm_state_iterate(f);
3390 if (ret < 0)
3391 goto out;
aliguori475e4272008-10-06 20:21:51 +00003392 } while (ret == 0);
aliguori9366f412008-10-06 14:53:52 +00003393
3394 ret = qemu_savevm_state_complete(f);
3395
3396out:
aliguori871d2f02008-10-13 03:07:56 +00003397 if (qemu_file_has_error(f))
3398 ret = -EIO;
3399
3400 if (!ret && saved_vm_running)
aliguori9366f412008-10-06 14:53:52 +00003401 vm_start();
aliguori871d2f02008-10-13 03:07:56 +00003402
bellard8a7ddc32004-03-31 19:00:16 +00003403 return ret;
3404}
3405
3406static SaveStateEntry *find_se(const char *idstr, int instance_id)
3407{
3408 SaveStateEntry *se;
3409
3410 for(se = first_se; se != NULL; se = se->next) {
ths5fafdf22007-09-16 21:08:06 +00003411 if (!strcmp(se->idstr, idstr) &&
bellard8a7ddc32004-03-31 19:00:16 +00003412 instance_id == se->instance_id)
3413 return se;
3414 }
3415 return NULL;
3416}
3417
aliguori9366f412008-10-06 14:53:52 +00003418typedef struct LoadStateEntry {
3419 SaveStateEntry *se;
3420 int section_id;
3421 int version_id;
3422 struct LoadStateEntry *next;
3423} LoadStateEntry;
3424
3425static int qemu_loadvm_state_v2(QEMUFile *f)
bellard8a7ddc32004-03-31 19:00:16 +00003426{
3427 SaveStateEntry *se;
bellardfaea38e2006-08-05 21:31:00 +00003428 int len, ret, instance_id, record_len, version_id;
3429 int64_t total_len, end_pos, cur_pos;
bellard8a7ddc32004-03-31 19:00:16 +00003430 char idstr[256];
ths3b46e622007-09-17 08:09:54 +00003431
bellardfaea38e2006-08-05 21:31:00 +00003432 total_len = qemu_get_be64(f);
3433 end_pos = total_len + qemu_ftell(f);
bellardb4608c02003-06-27 17:34:32 +00003434 for(;;) {
bellardfaea38e2006-08-05 21:31:00 +00003435 if (qemu_ftell(f) >= end_pos)
bellard8a7ddc32004-03-31 19:00:16 +00003436 break;
bellardfaea38e2006-08-05 21:31:00 +00003437 len = qemu_get_byte(f);
thsffe8ab82007-12-16 03:16:05 +00003438 qemu_get_buffer(f, (uint8_t *)idstr, len);
bellard8a7ddc32004-03-31 19:00:16 +00003439 idstr[len] = '\0';
3440 instance_id = qemu_get_be32(f);
3441 version_id = qemu_get_be32(f);
3442 record_len = qemu_get_be32(f);
bellardfaea38e2006-08-05 21:31:00 +00003443 cur_pos = qemu_ftell(f);
bellard8a7ddc32004-03-31 19:00:16 +00003444 se = find_se(idstr, instance_id);
3445 if (!se) {
ths5fafdf22007-09-16 21:08:06 +00003446 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
bellard8a7ddc32004-03-31 19:00:16 +00003447 instance_id, idstr);
3448 } else {
3449 ret = se->load_state(f, se->opaque, version_id);
3450 if (ret < 0) {
ths5fafdf22007-09-16 21:08:06 +00003451 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
bellard8a7ddc32004-03-31 19:00:16 +00003452 instance_id, idstr);
3453 }
bellard34865132003-10-05 14:28:56 +00003454 }
bellard8a7ddc32004-03-31 19:00:16 +00003455 /* always seek to exact end of record */
3456 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
3457 }
aliguori871d2f02008-10-13 03:07:56 +00003458
3459 if (qemu_file_has_error(f))
3460 return -EIO;
3461
aliguori9366f412008-10-06 14:53:52 +00003462 return 0;
3463}
3464
3465int qemu_loadvm_state(QEMUFile *f)
3466{
3467 LoadStateEntry *first_le = NULL;
3468 uint8_t section_type;
3469 unsigned int v;
3470 int ret;
3471
3472 v = qemu_get_be32(f);
3473 if (v != QEMU_VM_FILE_MAGIC)
3474 return -EINVAL;
3475
3476 v = qemu_get_be32(f);
3477 if (v == QEMU_VM_FILE_VERSION_COMPAT)
3478 return qemu_loadvm_state_v2(f);
3479 if (v != QEMU_VM_FILE_VERSION)
3480 return -ENOTSUP;
3481
3482 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
3483 uint32_t instance_id, version_id, section_id;
3484 LoadStateEntry *le;
3485 SaveStateEntry *se;
3486 char idstr[257];
3487 int len;
3488
3489 switch (section_type) {
3490 case QEMU_VM_SECTION_START:
3491 case QEMU_VM_SECTION_FULL:
3492 /* Read section start */
3493 section_id = qemu_get_be32(f);
3494 len = qemu_get_byte(f);
3495 qemu_get_buffer(f, (uint8_t *)idstr, len);
3496 idstr[len] = 0;
3497 instance_id = qemu_get_be32(f);
3498 version_id = qemu_get_be32(f);
3499
3500 /* Find savevm section */
3501 se = find_se(idstr, instance_id);
3502 if (se == NULL) {
3503 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
3504 ret = -EINVAL;
3505 goto out;
3506 }
3507
3508 /* Validate version */
3509 if (version_id > se->version_id) {
3510 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
3511 version_id, idstr, se->version_id);
3512 ret = -EINVAL;
3513 goto out;
3514 }
3515
3516 /* Add entry */
3517 le = qemu_mallocz(sizeof(*le));
3518 if (le == NULL) {
3519 ret = -ENOMEM;
3520 goto out;
3521 }
3522
3523 le->se = se;
3524 le->section_id = section_id;
3525 le->version_id = version_id;
3526 le->next = first_le;
3527 first_le = le;
3528
3529 le->se->load_state(f, le->se->opaque, le->version_id);
3530 break;
3531 case QEMU_VM_SECTION_PART:
3532 case QEMU_VM_SECTION_END:
3533 section_id = qemu_get_be32(f);
3534
3535 for (le = first_le; le && le->section_id != section_id; le = le->next);
3536 if (le == NULL) {
3537 fprintf(stderr, "Unknown savevm section %d\n", section_id);
3538 ret = -EINVAL;
3539 goto out;
3540 }
3541
3542 le->se->load_state(f, le->se->opaque, le->version_id);
3543 break;
3544 default:
3545 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
3546 ret = -EINVAL;
3547 goto out;
3548 }
3549 }
3550
bellard8a7ddc32004-03-31 19:00:16 +00003551 ret = 0;
aliguori9366f412008-10-06 14:53:52 +00003552
3553out:
3554 while (first_le) {
3555 LoadStateEntry *le = first_le;
3556 first_le = first_le->next;
3557 qemu_free(le);
3558 }
3559
aliguori871d2f02008-10-13 03:07:56 +00003560 if (qemu_file_has_error(f))
3561 ret = -EIO;
3562
bellardfaea38e2006-08-05 21:31:00 +00003563 return ret;
3564}
3565
3566/* device can contain snapshots */
3567static int bdrv_can_snapshot(BlockDriverState *bs)
3568{
3569 return (bs &&
3570 !bdrv_is_removable(bs) &&
3571 !bdrv_is_read_only(bs));
3572}
3573
3574/* device must be snapshots in order to have a reliable snapshot */
3575static int bdrv_has_snapshot(BlockDriverState *bs)
3576{
3577 return (bs &&
3578 !bdrv_is_removable(bs) &&
3579 !bdrv_is_read_only(bs));
3580}
3581
3582static BlockDriverState *get_bs_snapshots(void)
3583{
3584 BlockDriverState *bs;
3585 int i;
3586
3587 if (bs_snapshots)
3588 return bs_snapshots;
thse4bcb142007-12-02 04:51:10 +00003589 for(i = 0; i <= nb_drives; i++) {
3590 bs = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00003591 if (bdrv_can_snapshot(bs))
3592 goto ok;
3593 }
3594 return NULL;
3595 ok:
3596 bs_snapshots = bs;
3597 return bs;
3598}
3599
3600static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
3601 const char *name)
3602{
3603 QEMUSnapshotInfo *sn_tab, *sn;
3604 int nb_sns, i, ret;
ths3b46e622007-09-17 08:09:54 +00003605
bellardfaea38e2006-08-05 21:31:00 +00003606 ret = -ENOENT;
3607 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
3608 if (nb_sns < 0)
3609 return ret;
3610 for(i = 0; i < nb_sns; i++) {
3611 sn = &sn_tab[i];
3612 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
3613 *sn_info = *sn;
3614 ret = 0;
3615 break;
3616 }
3617 }
3618 qemu_free(sn_tab);
3619 return ret;
3620}
3621
3622void do_savevm(const char *name)
3623{
3624 BlockDriverState *bs, *bs1;
3625 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
3626 int must_delete, ret, i;
3627 BlockDriverInfo bdi1, *bdi = &bdi1;
3628 QEMUFile *f;
3629 int saved_vm_running;
bellard4c279bd2006-08-17 09:43:50 +00003630#ifdef _WIN32
3631 struct _timeb tb;
3632#else
bellardfaea38e2006-08-05 21:31:00 +00003633 struct timeval tv;
bellard4c279bd2006-08-17 09:43:50 +00003634#endif
bellardfaea38e2006-08-05 21:31:00 +00003635
3636 bs = get_bs_snapshots();
3637 if (!bs) {
3638 term_printf("No block device can accept snapshots\n");
3639 return;
3640 }
3641
pbrook6192bc32006-09-03 12:08:37 +00003642 /* ??? Should this occur after vm_stop? */
3643 qemu_aio_flush();
3644
bellardfaea38e2006-08-05 21:31:00 +00003645 saved_vm_running = vm_running;
3646 vm_stop(0);
ths3b46e622007-09-17 08:09:54 +00003647
bellardfaea38e2006-08-05 21:31:00 +00003648 must_delete = 0;
3649 if (name) {
3650 ret = bdrv_snapshot_find(bs, old_sn, name);
3651 if (ret >= 0) {
3652 must_delete = 1;
3653 }
3654 }
3655 memset(sn, 0, sizeof(*sn));
3656 if (must_delete) {
3657 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
3658 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
3659 } else {
3660 if (name)
3661 pstrcpy(sn->name, sizeof(sn->name), name);
3662 }
3663
3664 /* fill auxiliary fields */
bellard4c279bd2006-08-17 09:43:50 +00003665#ifdef _WIN32
3666 _ftime(&tb);
3667 sn->date_sec = tb.time;
3668 sn->date_nsec = tb.millitm * 1000000;
3669#else
bellardfaea38e2006-08-05 21:31:00 +00003670 gettimeofday(&tv, NULL);
3671 sn->date_sec = tv.tv_sec;
3672 sn->date_nsec = tv.tv_usec * 1000;
bellard4c279bd2006-08-17 09:43:50 +00003673#endif
bellardfaea38e2006-08-05 21:31:00 +00003674 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
ths3b46e622007-09-17 08:09:54 +00003675
bellardfaea38e2006-08-05 21:31:00 +00003676 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
3677 term_printf("Device %s does not support VM state snapshots\n",
3678 bdrv_get_device_name(bs));
3679 goto the_end;
3680 }
ths3b46e622007-09-17 08:09:54 +00003681
bellardfaea38e2006-08-05 21:31:00 +00003682 /* save the VM state */
3683 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
3684 if (!f) {
3685 term_printf("Could not open VM state file\n");
3686 goto the_end;
3687 }
3688 ret = qemu_savevm_state(f);
3689 sn->vm_state_size = qemu_ftell(f);
3690 qemu_fclose(f);
3691 if (ret < 0) {
3692 term_printf("Error %d while writing VM\n", ret);
3693 goto the_end;
3694 }
ths3b46e622007-09-17 08:09:54 +00003695
bellardfaea38e2006-08-05 21:31:00 +00003696 /* create the snapshots */
3697
thse4bcb142007-12-02 04:51:10 +00003698 for(i = 0; i < nb_drives; i++) {
3699 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00003700 if (bdrv_has_snapshot(bs1)) {
3701 if (must_delete) {
3702 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
3703 if (ret < 0) {
3704 term_printf("Error while deleting snapshot on '%s'\n",
3705 bdrv_get_device_name(bs1));
3706 }
3707 }
3708 ret = bdrv_snapshot_create(bs1, sn);
3709 if (ret < 0) {
3710 term_printf("Error while creating snapshot on '%s'\n",
3711 bdrv_get_device_name(bs1));
3712 }
3713 }
3714 }
3715
3716 the_end:
bellard8a7ddc32004-03-31 19:00:16 +00003717 if (saved_vm_running)
3718 vm_start();
bellardfaea38e2006-08-05 21:31:00 +00003719}
3720
3721void do_loadvm(const char *name)
3722{
3723 BlockDriverState *bs, *bs1;
3724 BlockDriverInfo bdi1, *bdi = &bdi1;
3725 QEMUFile *f;
3726 int i, ret;
3727 int saved_vm_running;
3728
3729 bs = get_bs_snapshots();
3730 if (!bs) {
3731 term_printf("No block device supports snapshots\n");
3732 return;
3733 }
ths3b46e622007-09-17 08:09:54 +00003734
pbrook6192bc32006-09-03 12:08:37 +00003735 /* Flush all IO requests so they don't interfere with the new state. */
3736 qemu_aio_flush();
3737
bellardfaea38e2006-08-05 21:31:00 +00003738 saved_vm_running = vm_running;
3739 vm_stop(0);
3740
thse4bcb142007-12-02 04:51:10 +00003741 for(i = 0; i <= nb_drives; i++) {
3742 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00003743 if (bdrv_has_snapshot(bs1)) {
3744 ret = bdrv_snapshot_goto(bs1, name);
3745 if (ret < 0) {
3746 if (bs != bs1)
3747 term_printf("Warning: ");
3748 switch(ret) {
3749 case -ENOTSUP:
3750 term_printf("Snapshots not supported on device '%s'\n",
3751 bdrv_get_device_name(bs1));
3752 break;
3753 case -ENOENT:
3754 term_printf("Could not find snapshot '%s' on device '%s'\n",
3755 name, bdrv_get_device_name(bs1));
3756 break;
3757 default:
3758 term_printf("Error %d while activating snapshot on '%s'\n",
3759 ret, bdrv_get_device_name(bs1));
3760 break;
3761 }
3762 /* fatal on snapshot block device */
3763 if (bs == bs1)
3764 goto the_end;
3765 }
3766 }
3767 }
3768
3769 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
3770 term_printf("Device %s does not support VM state snapshots\n",
3771 bdrv_get_device_name(bs));
3772 return;
3773 }
ths3b46e622007-09-17 08:09:54 +00003774
bellardfaea38e2006-08-05 21:31:00 +00003775 /* restore the VM state */
3776 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
3777 if (!f) {
3778 term_printf("Could not open VM state file\n");
3779 goto the_end;
3780 }
3781 ret = qemu_loadvm_state(f);
3782 qemu_fclose(f);
3783 if (ret < 0) {
3784 term_printf("Error %d while loading VM state\n", ret);
3785 }
3786 the_end:
3787 if (saved_vm_running)
3788 vm_start();
3789}
3790
3791void do_delvm(const char *name)
3792{
3793 BlockDriverState *bs, *bs1;
3794 int i, ret;
3795
3796 bs = get_bs_snapshots();
3797 if (!bs) {
3798 term_printf("No block device supports snapshots\n");
3799 return;
3800 }
ths3b46e622007-09-17 08:09:54 +00003801
thse4bcb142007-12-02 04:51:10 +00003802 for(i = 0; i <= nb_drives; i++) {
3803 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00003804 if (bdrv_has_snapshot(bs1)) {
3805 ret = bdrv_snapshot_delete(bs1, name);
3806 if (ret < 0) {
3807 if (ret == -ENOTSUP)
3808 term_printf("Snapshots not supported on device '%s'\n",
3809 bdrv_get_device_name(bs1));
3810 else
3811 term_printf("Error %d while deleting snapshot on '%s'\n",
3812 ret, bdrv_get_device_name(bs1));
3813 }
3814 }
3815 }
3816}
3817
3818void do_info_snapshots(void)
3819{
3820 BlockDriverState *bs, *bs1;
3821 QEMUSnapshotInfo *sn_tab, *sn;
3822 int nb_sns, i;
3823 char buf[256];
3824
3825 bs = get_bs_snapshots();
3826 if (!bs) {
3827 term_printf("No available block device supports snapshots\n");
3828 return;
3829 }
3830 term_printf("Snapshot devices:");
thse4bcb142007-12-02 04:51:10 +00003831 for(i = 0; i <= nb_drives; i++) {
3832 bs1 = drives_table[i].bdrv;
bellardfaea38e2006-08-05 21:31:00 +00003833 if (bdrv_has_snapshot(bs1)) {
3834 if (bs == bs1)
3835 term_printf(" %s", bdrv_get_device_name(bs1));
3836 }
3837 }
3838 term_printf("\n");
3839
3840 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
3841 if (nb_sns < 0) {
3842 term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
3843 return;
3844 }
3845 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
3846 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
3847 for(i = 0; i < nb_sns; i++) {
3848 sn = &sn_tab[i];
3849 term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
3850 }
3851 qemu_free(sn_tab);
bellard8a7ddc32004-03-31 19:00:16 +00003852}
3853
3854/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00003855/* ram save/restore */
3856
bellard8a7ddc32004-03-31 19:00:16 +00003857static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3858{
3859 int v;
3860
3861 v = qemu_get_byte(f);
3862 switch(v) {
3863 case 0:
3864 if (qemu_get_buffer(f, buf, len) != len)
3865 return -EIO;
3866 break;
3867 case 1:
3868 v = qemu_get_byte(f);
3869 memset(buf, v, len);
3870 break;
3871 default:
3872 return -EINVAL;
3873 }
aliguori871d2f02008-10-13 03:07:56 +00003874
3875 if (qemu_file_has_error(f))
3876 return -EIO;
3877
bellard8a7ddc32004-03-31 19:00:16 +00003878 return 0;
3879}
3880
bellardc88676f2006-08-06 13:36:11 +00003881static int ram_load_v1(QEMUFile *f, void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00003882{
aurel3200f82b82008-04-27 21:12:55 +00003883 int ret;
3884 ram_addr_t i;
bellard8a7ddc32004-03-31 19:00:16 +00003885
bellard8a7ddc32004-03-31 19:00:16 +00003886 if (qemu_get_be32(f) != phys_ram_size)
3887 return -EINVAL;
3888 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3889 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3890 if (ret)
3891 return ret;
3892 }
3893 return 0;
3894}
3895
bellardc88676f2006-08-06 13:36:11 +00003896#define BDRV_HASH_BLOCK_SIZE 1024
3897#define IOBUF_SIZE 4096
3898#define RAM_CBLOCK_MAGIC 0xfabe
3899
bellardc88676f2006-08-06 13:36:11 +00003900typedef struct RamDecompressState {
3901 z_stream zstream;
3902 QEMUFile *f;
3903 uint8_t buf[IOBUF_SIZE];
3904} RamDecompressState;
3905
3906static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3907{
3908 int ret;
3909 memset(s, 0, sizeof(*s));
3910 s->f = f;
3911 ret = inflateInit(&s->zstream);
3912 if (ret != Z_OK)
3913 return -1;
3914 return 0;
3915}
3916
3917static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3918{
3919 int ret, clen;
3920
3921 s->zstream.avail_out = len;
3922 s->zstream.next_out = buf;
3923 while (s->zstream.avail_out > 0) {
3924 if (s->zstream.avail_in == 0) {
3925 if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3926 return -1;
3927 clen = qemu_get_be16(s->f);
3928 if (clen > IOBUF_SIZE)
3929 return -1;
3930 qemu_get_buffer(s->f, s->buf, clen);
3931 s->zstream.avail_in = clen;
3932 s->zstream.next_in = s->buf;
3933 }
3934 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3935 if (ret != Z_OK && ret != Z_STREAM_END) {
3936 return -1;
3937 }
3938 }
3939 return 0;
3940}
3941
3942static void ram_decompress_close(RamDecompressState *s)
3943{
3944 inflateEnd(&s->zstream);
3945}
3946
aliguori475e4272008-10-06 20:21:51 +00003947#define RAM_SAVE_FLAG_FULL 0x01
3948#define RAM_SAVE_FLAG_COMPRESS 0x02
3949#define RAM_SAVE_FLAG_MEM_SIZE 0x04
3950#define RAM_SAVE_FLAG_PAGE 0x08
3951#define RAM_SAVE_FLAG_EOS 0x10
3952
3953static int is_dup_page(uint8_t *page, uint8_t ch)
bellardc88676f2006-08-06 13:36:11 +00003954{
aliguori475e4272008-10-06 20:21:51 +00003955 uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3956 uint32_t *array = (uint32_t *)page;
3957 int i;
ths3b46e622007-09-17 08:09:54 +00003958
aliguori475e4272008-10-06 20:21:51 +00003959 for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3960 if (array[i] != val)
3961 return 0;
bellardc88676f2006-08-06 13:36:11 +00003962 }
aliguori475e4272008-10-06 20:21:51 +00003963
3964 return 1;
bellardc88676f2006-08-06 13:36:11 +00003965}
3966
aliguori475e4272008-10-06 20:21:51 +00003967static int ram_save_block(QEMUFile *f)
3968{
3969 static ram_addr_t current_addr = 0;
3970 ram_addr_t saved_addr = current_addr;
3971 ram_addr_t addr = 0;
3972 int found = 0;
3973
3974 while (addr < phys_ram_size) {
3975 if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3976 uint8_t ch;
3977
3978 cpu_physical_memory_reset_dirty(current_addr,
3979 current_addr + TARGET_PAGE_SIZE,
3980 MIGRATION_DIRTY_FLAG);
3981
3982 ch = *(phys_ram_base + current_addr);
3983
3984 if (is_dup_page(phys_ram_base + current_addr, ch)) {
3985 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3986 qemu_put_byte(f, ch);
3987 } else {
3988 qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3989 qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3990 }
3991
3992 found = 1;
3993 break;
3994 }
3995 addr += TARGET_PAGE_SIZE;
3996 current_addr = (saved_addr + addr) % phys_ram_size;
3997 }
3998
3999 return found;
4000}
4001
4002static ram_addr_t ram_save_threshold = 10;
4003
4004static ram_addr_t ram_save_remaining(void)
4005{
4006 ram_addr_t addr;
4007 ram_addr_t count = 0;
4008
4009 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
4010 if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
4011 count++;
4012 }
4013
4014 return count;
4015}
4016
4017static int ram_save_live(QEMUFile *f, int stage, void *opaque)
4018{
4019 ram_addr_t addr;
4020
4021 if (stage == 1) {
4022 /* Make sure all dirty bits are set */
4023 for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
4024 if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
4025 cpu_physical_memory_set_dirty(addr);
4026 }
4027
4028 /* Enable dirty memory tracking */
4029 cpu_physical_memory_set_dirty_tracking(1);
4030
4031 qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
4032 }
4033
4034 while (!qemu_file_rate_limit(f)) {
4035 int ret;
4036
4037 ret = ram_save_block(f);
4038 if (ret == 0) /* no more blocks */
4039 break;
4040 }
4041
4042 /* try transferring iterative blocks of memory */
4043
4044 if (stage == 3) {
4045 cpu_physical_memory_set_dirty_tracking(0);
4046
4047 /* flush all remaining blocks regardless of rate limiting */
4048 while (ram_save_block(f) != 0);
4049 }
4050
4051 qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
4052
4053 return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
4054}
4055
4056static int ram_load_dead(QEMUFile *f, void *opaque)
bellardc88676f2006-08-06 13:36:11 +00004057{
4058 RamDecompressState s1, *s = &s1;
4059 uint8_t buf[10];
aurel3200f82b82008-04-27 21:12:55 +00004060 ram_addr_t i;
bellardc88676f2006-08-06 13:36:11 +00004061
bellardc88676f2006-08-06 13:36:11 +00004062 if (ram_decompress_open(s, f) < 0)
4063 return -EINVAL;
4064 for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
4065 if (ram_decompress_buf(s, buf, 1) < 0) {
4066 fprintf(stderr, "Error while reading ram block header\n");
4067 goto error;
4068 }
4069 if (buf[0] == 0) {
4070 if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
aurel3200f82b82008-04-27 21:12:55 +00004071 fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
bellardc88676f2006-08-06 13:36:11 +00004072 goto error;
4073 }
aliguori475e4272008-10-06 20:21:51 +00004074 } else {
bellardc88676f2006-08-06 13:36:11 +00004075 error:
4076 printf("Error block header\n");
4077 return -EINVAL;
4078 }
4079 }
4080 ram_decompress_close(s);
aliguori475e4272008-10-06 20:21:51 +00004081
4082 return 0;
4083}
4084
4085static int ram_load(QEMUFile *f, void *opaque, int version_id)
4086{
4087 ram_addr_t addr;
4088 int flags;
4089
4090 if (version_id == 1)
4091 return ram_load_v1(f, opaque);
4092
4093 if (version_id == 2) {
4094 if (qemu_get_be32(f) != phys_ram_size)
4095 return -EINVAL;
4096 return ram_load_dead(f, opaque);
4097 }
4098
4099 if (version_id != 3)
4100 return -EINVAL;
4101
4102 do {
4103 addr = qemu_get_be64(f);
4104
4105 flags = addr & ~TARGET_PAGE_MASK;
4106 addr &= TARGET_PAGE_MASK;
4107
4108 if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
4109 if (addr != phys_ram_size)
4110 return -EINVAL;
4111 }
4112
4113 if (flags & RAM_SAVE_FLAG_FULL) {
4114 if (ram_load_dead(f, opaque) < 0)
4115 return -EINVAL;
4116 }
4117
4118 if (flags & RAM_SAVE_FLAG_COMPRESS) {
4119 uint8_t ch = qemu_get_byte(f);
4120 memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
4121 } else if (flags & RAM_SAVE_FLAG_PAGE)
4122 qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
4123 } while (!(flags & RAM_SAVE_FLAG_EOS));
4124
bellardc88676f2006-08-06 13:36:11 +00004125 return 0;
4126}
4127
aliguori9e472e12008-10-08 19:50:24 +00004128void qemu_service_io(void)
4129{
4130 CPUState *env = cpu_single_env;
4131 if (env) {
4132 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
4133#ifdef USE_KQEMU
4134 if (env->kqemu_enabled) {
4135 kqemu_cpu_interrupt(env);
4136 }
4137#endif
4138 }
4139}
4140
bellard8a7ddc32004-03-31 19:00:16 +00004141/***********************************************************/
bellard83f64092006-08-01 16:21:11 +00004142/* bottom halves (can be seen as timers which expire ASAP) */
4143
4144struct QEMUBH {
4145 QEMUBHFunc *cb;
4146 void *opaque;
4147 int scheduled;
aliguori1b435b12008-10-31 17:24:21 +00004148 int idle;
4149 int deleted;
bellard83f64092006-08-01 16:21:11 +00004150 QEMUBH *next;
4151};
4152
4153static QEMUBH *first_bh = NULL;
4154
4155QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
4156{
4157 QEMUBH *bh;
4158 bh = qemu_mallocz(sizeof(QEMUBH));
4159 if (!bh)
4160 return NULL;
4161 bh->cb = cb;
4162 bh->opaque = opaque;
aliguori1b435b12008-10-31 17:24:21 +00004163 bh->next = first_bh;
4164 first_bh = bh;
bellard83f64092006-08-01 16:21:11 +00004165 return bh;
4166}
4167
bellard6eb57332006-08-06 09:51:25 +00004168int qemu_bh_poll(void)
bellard83f64092006-08-01 16:21:11 +00004169{
aliguori1b435b12008-10-31 17:24:21 +00004170 QEMUBH *bh, **bhp;
bellard6eb57332006-08-06 09:51:25 +00004171 int ret;
bellard83f64092006-08-01 16:21:11 +00004172
bellard6eb57332006-08-06 09:51:25 +00004173 ret = 0;
aliguori1b435b12008-10-31 17:24:21 +00004174 for (bh = first_bh; bh; bh = bh->next) {
4175 if (!bh->deleted && bh->scheduled) {
4176 bh->scheduled = 0;
4177 if (!bh->idle)
4178 ret = 1;
4179 bh->idle = 0;
4180 bh->cb(bh->opaque);
4181 }
bellard83f64092006-08-01 16:21:11 +00004182 }
aliguori1b435b12008-10-31 17:24:21 +00004183
4184 /* remove deleted bhs */
4185 bhp = &first_bh;
4186 while (*bhp) {
4187 bh = *bhp;
4188 if (bh->deleted) {
4189 *bhp = bh->next;
4190 qemu_free(bh);
4191 } else
4192 bhp = &bh->next;
4193 }
4194
bellard6eb57332006-08-06 09:51:25 +00004195 return ret;
bellard83f64092006-08-01 16:21:11 +00004196}
4197
aliguori1b435b12008-10-31 17:24:21 +00004198void qemu_bh_schedule_idle(QEMUBH *bh)
4199{
4200 if (bh->scheduled)
4201 return;
4202 bh->scheduled = 1;
4203 bh->idle = 1;
4204}
4205
bellard83f64092006-08-01 16:21:11 +00004206void qemu_bh_schedule(QEMUBH *bh)
4207{
4208 CPUState *env = cpu_single_env;
4209 if (bh->scheduled)
4210 return;
4211 bh->scheduled = 1;
aliguori1b435b12008-10-31 17:24:21 +00004212 bh->idle = 0;
bellard83f64092006-08-01 16:21:11 +00004213 /* stop the currently executing CPU to execute the BH ASAP */
4214 if (env) {
4215 cpu_interrupt(env, CPU_INTERRUPT_EXIT);
4216 }
4217}
4218
4219void qemu_bh_cancel(QEMUBH *bh)
4220{
aliguori1b435b12008-10-31 17:24:21 +00004221 bh->scheduled = 0;
bellard83f64092006-08-01 16:21:11 +00004222}
4223
4224void qemu_bh_delete(QEMUBH *bh)
4225{
aliguori1b435b12008-10-31 17:24:21 +00004226 bh->scheduled = 0;
4227 bh->deleted = 1;
bellard83f64092006-08-01 16:21:11 +00004228}
4229
aliguori56f3a5d2008-10-31 18:07:17 +00004230static void qemu_bh_update_timeout(int *timeout)
4231{
4232 QEMUBH *bh;
4233
4234 for (bh = first_bh; bh; bh = bh->next) {
4235 if (!bh->deleted && bh->scheduled) {
4236 if (bh->idle) {
4237 /* idle bottom halves will be polled at least
4238 * every 10ms */
4239 *timeout = MIN(10, *timeout);
4240 } else {
4241 /* non-idle bottom halves will be executed
4242 * immediately */
4243 *timeout = 0;
4244 break;
4245 }
4246 }
4247 }
4248}
4249
bellard83f64092006-08-01 16:21:11 +00004250/***********************************************************/
bellardcc1daa42005-06-05 14:49:17 +00004251/* machine registration */
4252
blueswir1bdaf78e2008-10-04 07:24:27 +00004253static QEMUMachine *first_machine = NULL;
bellardcc1daa42005-06-05 14:49:17 +00004254
4255int qemu_register_machine(QEMUMachine *m)
4256{
4257 QEMUMachine **pm;
4258 pm = &first_machine;
4259 while (*pm != NULL)
4260 pm = &(*pm)->next;
4261 m->next = NULL;
4262 *pm = m;
4263 return 0;
4264}
4265
pbrook9596ebb2007-11-18 01:44:38 +00004266static QEMUMachine *find_machine(const char *name)
bellardcc1daa42005-06-05 14:49:17 +00004267{
4268 QEMUMachine *m;
4269
4270 for(m = first_machine; m != NULL; m = m->next) {
4271 if (!strcmp(m->name, name))
4272 return m;
4273 }
4274 return NULL;
4275}
4276
4277/***********************************************************/
bellard8a7ddc32004-03-31 19:00:16 +00004278/* main execution loop */
4279
pbrook9596ebb2007-11-18 01:44:38 +00004280static void gui_update(void *opaque)
bellard8a7ddc32004-03-31 19:00:16 +00004281{
ths740733b2007-06-08 01:57:56 +00004282 DisplayState *ds = opaque;
4283 ds->dpy_refresh(ds);
aurel32f442e082008-03-13 19:20:33 +00004284 qemu_mod_timer(ds->gui_timer,
4285 (ds->gui_timer_interval ?
4286 ds->gui_timer_interval :
4287 GUI_REFRESH_INTERVAL)
4288 + qemu_get_clock(rt_clock));
bellard8a7ddc32004-03-31 19:00:16 +00004289}
4290
bellard0bd48852005-11-11 00:00:47 +00004291struct vm_change_state_entry {
4292 VMChangeStateHandler *cb;
4293 void *opaque;
4294 LIST_ENTRY (vm_change_state_entry) entries;
4295};
4296
4297static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
4298
4299VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
4300 void *opaque)
4301{
4302 VMChangeStateEntry *e;
4303
4304 e = qemu_mallocz(sizeof (*e));
4305 if (!e)
4306 return NULL;
4307
4308 e->cb = cb;
4309 e->opaque = opaque;
4310 LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
4311 return e;
4312}
4313
4314void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
4315{
4316 LIST_REMOVE (e, entries);
4317 qemu_free (e);
4318}
4319
4320static void vm_state_notify(int running)
4321{
4322 VMChangeStateEntry *e;
4323
4324 for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
4325 e->cb(e->opaque, running);
4326 }
4327}
4328
bellard8a7ddc32004-03-31 19:00:16 +00004329/* XXX: support several handlers */
bellard0bd48852005-11-11 00:00:47 +00004330static VMStopHandler *vm_stop_cb;
4331static void *vm_stop_opaque;
bellard8a7ddc32004-03-31 19:00:16 +00004332
4333int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
4334{
4335 vm_stop_cb = cb;
4336 vm_stop_opaque = opaque;
4337 return 0;
4338}
4339
4340void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
4341{
4342 vm_stop_cb = NULL;
4343}
4344
4345void vm_start(void)
4346{
4347 if (!vm_running) {
4348 cpu_enable_ticks();
4349 vm_running = 1;
bellard0bd48852005-11-11 00:00:47 +00004350 vm_state_notify(1);
thsefe75412007-08-24 01:36:32 +00004351 qemu_rearm_alarm_timer(alarm_timer);
bellard8a7ddc32004-03-31 19:00:16 +00004352 }
4353}
4354
ths5fafdf22007-09-16 21:08:06 +00004355void vm_stop(int reason)
bellard8a7ddc32004-03-31 19:00:16 +00004356{
4357 if (vm_running) {
4358 cpu_disable_ticks();
4359 vm_running = 0;
4360 if (reason != 0) {
4361 if (vm_stop_cb) {
4362 vm_stop_cb(vm_stop_opaque, reason);
4363 }
4364 }
bellard0bd48852005-11-11 00:00:47 +00004365 vm_state_notify(0);
bellard8a7ddc32004-03-31 19:00:16 +00004366 }
4367}
4368
bellardbb0c6722004-06-20 12:37:32 +00004369/* reset/shutdown handler */
4370
4371typedef struct QEMUResetEntry {
4372 QEMUResetHandler *func;
4373 void *opaque;
4374 struct QEMUResetEntry *next;
4375} QEMUResetEntry;
4376
4377static QEMUResetEntry *first_reset_entry;
4378static int reset_requested;
4379static int shutdown_requested;
bellard34751872005-07-02 14:31:34 +00004380static int powerdown_requested;
bellardbb0c6722004-06-20 12:37:32 +00004381
aurel32cf7a2fe2008-03-18 06:53:05 +00004382int qemu_shutdown_requested(void)
4383{
4384 int r = shutdown_requested;
4385 shutdown_requested = 0;
4386 return r;
4387}
4388
4389int qemu_reset_requested(void)
4390{
4391 int r = reset_requested;
4392 reset_requested = 0;
4393 return r;
4394}
4395
4396int qemu_powerdown_requested(void)
4397{
4398 int r = powerdown_requested;
4399 powerdown_requested = 0;
4400 return r;
4401}
4402
bellardbb0c6722004-06-20 12:37:32 +00004403void qemu_register_reset(QEMUResetHandler *func, void *opaque)
4404{
4405 QEMUResetEntry **pre, *re;
4406
4407 pre = &first_reset_entry;
4408 while (*pre != NULL)
4409 pre = &(*pre)->next;
4410 re = qemu_mallocz(sizeof(QEMUResetEntry));
4411 re->func = func;
4412 re->opaque = opaque;
4413 re->next = NULL;
4414 *pre = re;
4415}
4416
aurel32cf7a2fe2008-03-18 06:53:05 +00004417void qemu_system_reset(void)
bellardbb0c6722004-06-20 12:37:32 +00004418{
4419 QEMUResetEntry *re;
4420
4421 /* reset all devices */
4422 for(re = first_reset_entry; re != NULL; re = re->next) {
4423 re->func(re->opaque);
4424 }
4425}
4426
4427void qemu_system_reset_request(void)
4428{
bellardd1beab82006-10-02 19:44:22 +00004429 if (no_reboot) {
4430 shutdown_requested = 1;
4431 } else {
4432 reset_requested = 1;
4433 }
bellard6a00d602005-11-21 23:25:50 +00004434 if (cpu_single_env)
4435 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00004436}
4437
4438void qemu_system_shutdown_request(void)
4439{
4440 shutdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00004441 if (cpu_single_env)
4442 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00004443}
4444
bellard34751872005-07-02 14:31:34 +00004445void qemu_system_powerdown_request(void)
4446{
4447 powerdown_requested = 1;
bellard6a00d602005-11-21 23:25:50 +00004448 if (cpu_single_env)
4449 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
bellardbb0c6722004-06-20 12:37:32 +00004450}
4451
ths877cf882007-04-18 18:11:47 +00004452#ifdef _WIN32
aliguori56f3a5d2008-10-31 18:07:17 +00004453void host_main_loop_wait(int *timeout)
4454{
4455 int ret, ret2, i;
bellardf3311102006-04-12 20:21:17 +00004456 PollingEntry *pe;
bellardc4b1fcc2004-03-14 21:44:30 +00004457
bellardf3311102006-04-12 20:21:17 +00004458
4459 /* XXX: need to suppress polling by better using win32 events */
4460 ret = 0;
4461 for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4462 ret |= pe->func(pe->opaque);
4463 }
thse6b1e552007-04-18 17:56:02 +00004464 if (ret == 0) {
bellarda18e5242006-06-25 17:18:27 +00004465 int err;
4466 WaitObjects *w = &wait_objects;
ths3b46e622007-09-17 08:09:54 +00004467
aliguori56f3a5d2008-10-31 18:07:17 +00004468 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
bellarda18e5242006-06-25 17:18:27 +00004469 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4470 if (w->func[ret - WAIT_OBJECT_0])
4471 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
ths3b46e622007-09-17 08:09:54 +00004472
ths5fafdf22007-09-16 21:08:06 +00004473 /* Check for additional signaled events */
thse6b1e552007-04-18 17:56:02 +00004474 for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
ths3b46e622007-09-17 08:09:54 +00004475
thse6b1e552007-04-18 17:56:02 +00004476 /* Check if event is signaled */
4477 ret2 = WaitForSingleObject(w->events[i], 0);
4478 if(ret2 == WAIT_OBJECT_0) {
4479 if (w->func[i])
4480 w->func[i](w->opaque[i]);
4481 } else if (ret2 == WAIT_TIMEOUT) {
4482 } else {
4483 err = GetLastError();
4484 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
ths3b46e622007-09-17 08:09:54 +00004485 }
4486 }
bellarda18e5242006-06-25 17:18:27 +00004487 } else if (ret == WAIT_TIMEOUT) {
4488 } else {
4489 err = GetLastError();
thse6b1e552007-04-18 17:56:02 +00004490 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
bellarda18e5242006-06-25 17:18:27 +00004491 }
bellardf3311102006-04-12 20:21:17 +00004492 }
aliguori56f3a5d2008-10-31 18:07:17 +00004493
4494 *timeout = 0;
4495}
4496#else
4497void host_main_loop_wait(int *timeout)
4498{
4499}
bellardfd1dff42006-02-01 21:29:26 +00004500#endif
aliguori56f3a5d2008-10-31 18:07:17 +00004501
4502void main_loop_wait(int timeout)
4503{
4504 IOHandlerRecord *ioh;
4505 fd_set rfds, wfds, xfds;
4506 int ret, nfds;
4507 struct timeval tv;
4508
4509 qemu_bh_update_timeout(&timeout);
4510
4511 host_main_loop_wait(&timeout);
4512
bellardfd1dff42006-02-01 21:29:26 +00004513 /* poll any events */
4514 /* XXX: separate device handlers from system ones */
aliguori6abfbd72008-11-05 20:49:37 +00004515 nfds = -1;
bellardfd1dff42006-02-01 21:29:26 +00004516 FD_ZERO(&rfds);
4517 FD_ZERO(&wfds);
bellarde0356492006-05-01 13:33:02 +00004518 FD_ZERO(&xfds);
bellardfd1dff42006-02-01 21:29:26 +00004519 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
thscafffd42007-02-28 21:59:44 +00004520 if (ioh->deleted)
4521 continue;
bellardfd1dff42006-02-01 21:29:26 +00004522 if (ioh->fd_read &&
4523 (!ioh->fd_read_poll ||
4524 ioh->fd_read_poll(ioh->opaque) != 0)) {
4525 FD_SET(ioh->fd, &rfds);
4526 if (ioh->fd > nfds)
4527 nfds = ioh->fd;
4528 }
4529 if (ioh->fd_write) {
4530 FD_SET(ioh->fd, &wfds);
4531 if (ioh->fd > nfds)
4532 nfds = ioh->fd;
4533 }
4534 }
ths3b46e622007-09-17 08:09:54 +00004535
aliguori56f3a5d2008-10-31 18:07:17 +00004536 tv.tv_sec = timeout / 1000;
4537 tv.tv_usec = (timeout % 1000) * 1000;
4538
bellarde0356492006-05-01 13:33:02 +00004539#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00004540 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00004541 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4542 }
4543#endif
4544 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
bellardfd1dff42006-02-01 21:29:26 +00004545 if (ret > 0) {
thscafffd42007-02-28 21:59:44 +00004546 IOHandlerRecord **pioh;
4547
4548 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
ths6ab43fd2007-08-25 01:34:19 +00004549 if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004550 ioh->fd_read(ioh->opaque);
bellardc4b1fcc2004-03-14 21:44:30 +00004551 }
ths6ab43fd2007-08-25 01:34:19 +00004552 if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
bellardfd1dff42006-02-01 21:29:26 +00004553 ioh->fd_write(ioh->opaque);
bellardb4608c02003-06-27 17:34:32 +00004554 }
4555 }
thscafffd42007-02-28 21:59:44 +00004556
4557 /* remove deleted IO handlers */
4558 pioh = &first_io_handler;
4559 while (*pioh) {
4560 ioh = *pioh;
4561 if (ioh->deleted) {
4562 *pioh = ioh->next;
4563 qemu_free(ioh);
ths5fafdf22007-09-16 21:08:06 +00004564 } else
thscafffd42007-02-28 21:59:44 +00004565 pioh = &ioh->next;
4566 }
bellardfd1dff42006-02-01 21:29:26 +00004567 }
bellarde0356492006-05-01 13:33:02 +00004568#if defined(CONFIG_SLIRP)
aliguori63a01ef2008-10-31 19:10:00 +00004569 if (slirp_is_inited()) {
bellarde0356492006-05-01 13:33:02 +00004570 if (ret < 0) {
4571 FD_ZERO(&rfds);
4572 FD_ZERO(&wfds);
4573 FD_ZERO(&xfds);
4574 }
4575 slirp_select_poll(&rfds, &wfds, &xfds);
4576 }
4577#endif
bellardc20709a2004-04-21 23:27:19 +00004578
pbrook423f0742007-05-23 00:06:54 +00004579 /* Check bottom-halves last in case any of the earlier events triggered
4580 them. */
4581 qemu_bh_poll();
ths3b46e622007-09-17 08:09:54 +00004582
bellard5905b2e2004-08-01 21:53:26 +00004583}
4584
pbrook9596ebb2007-11-18 01:44:38 +00004585static int main_loop(void)
bellard5905b2e2004-08-01 21:53:26 +00004586{
4587 int ret, timeout;
bellard89bfc102006-02-08 22:46:31 +00004588#ifdef CONFIG_PROFILER
4589 int64_t ti;
4590#endif
bellard6a00d602005-11-21 23:25:50 +00004591 CPUState *env;
bellard5905b2e2004-08-01 21:53:26 +00004592
bellard6a00d602005-11-21 23:25:50 +00004593 cur_cpu = first_cpu;
balrogee5605e2007-12-03 03:01:40 +00004594 next_cpu = cur_cpu->next_cpu ?: first_cpu;
bellard5905b2e2004-08-01 21:53:26 +00004595 for(;;) {
4596 if (vm_running) {
bellard15a76442005-11-23 21:01:03 +00004597
bellard15a76442005-11-23 21:01:03 +00004598 for(;;) {
4599 /* get next cpu */
balrogee5605e2007-12-03 03:01:40 +00004600 env = next_cpu;
bellard89bfc102006-02-08 22:46:31 +00004601#ifdef CONFIG_PROFILER
4602 ti = profile_getclock();
4603#endif
pbrook2e70f6e2008-06-29 01:03:05 +00004604 if (use_icount) {
4605 int64_t count;
4606 int decr;
4607 qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4608 env->icount_decr.u16.low = 0;
4609 env->icount_extra = 0;
4610 count = qemu_next_deadline();
4611 count = (count + (1 << icount_time_shift) - 1)
4612 >> icount_time_shift;
4613 qemu_icount += count;
4614 decr = (count > 0xffff) ? 0xffff : count;
4615 count -= decr;
4616 env->icount_decr.u16.low = decr;
4617 env->icount_extra = count;
4618 }
bellard6a00d602005-11-21 23:25:50 +00004619 ret = cpu_exec(env);
bellard89bfc102006-02-08 22:46:31 +00004620#ifdef CONFIG_PROFILER
4621 qemu_time += profile_getclock() - ti;
4622#endif
pbrook2e70f6e2008-06-29 01:03:05 +00004623 if (use_icount) {
4624 /* Fold pending instructions back into the
4625 instruction counter, and clear the interrupt flag. */
4626 qemu_icount -= (env->icount_decr.u16.low
4627 + env->icount_extra);
4628 env->icount_decr.u32 = 0;
4629 env->icount_extra = 0;
4630 }
balrogee5605e2007-12-03 03:01:40 +00004631 next_cpu = env->next_cpu ?: first_cpu;
aurel3295b01002008-04-04 17:16:35 +00004632 if (event_pending && likely(ret != EXCP_DEBUG)) {
balrogee5605e2007-12-03 03:01:40 +00004633 ret = EXCP_INTERRUPT;
4634 event_pending = 0;
4635 break;
4636 }
pbrookbd967e02007-03-11 18:54:57 +00004637 if (ret == EXCP_HLT) {
4638 /* Give the next CPU a chance to run. */
4639 cur_cpu = env;
4640 continue;
4641 }
bellard15a76442005-11-23 21:01:03 +00004642 if (ret != EXCP_HALTED)
4643 break;
4644 /* all CPUs are halted ? */
pbrookbd967e02007-03-11 18:54:57 +00004645 if (env == cur_cpu)
bellard15a76442005-11-23 21:01:03 +00004646 break;
bellard15a76442005-11-23 21:01:03 +00004647 }
4648 cur_cpu = env;
4649
bellard5905b2e2004-08-01 21:53:26 +00004650 if (shutdown_requested) {
bellard34751872005-07-02 14:31:34 +00004651 ret = EXCP_INTERRUPT;
aurel32b2f76162008-04-11 21:35:52 +00004652 if (no_shutdown) {
4653 vm_stop(0);
4654 no_shutdown = 0;
4655 }
4656 else
4657 break;
bellard5905b2e2004-08-01 21:53:26 +00004658 }
4659 if (reset_requested) {
4660 reset_requested = 0;
4661 qemu_system_reset();
bellard34751872005-07-02 14:31:34 +00004662 ret = EXCP_INTERRUPT;
4663 }
4664 if (powerdown_requested) {
4665 powerdown_requested = 0;
4666 qemu_system_powerdown();
4667 ret = EXCP_INTERRUPT;
bellard5905b2e2004-08-01 21:53:26 +00004668 }
aurel3295b01002008-04-04 17:16:35 +00004669 if (unlikely(ret == EXCP_DEBUG)) {
bellard5905b2e2004-08-01 21:53:26 +00004670 vm_stop(EXCP_DEBUG);
4671 }
pbrookbd967e02007-03-11 18:54:57 +00004672 /* If all cpus are halted then wait until the next IRQ */
bellard5905b2e2004-08-01 21:53:26 +00004673 /* XXX: use timeout computed from timers */
pbrook2e70f6e2008-06-29 01:03:05 +00004674 if (ret == EXCP_HALTED) {
4675 if (use_icount) {
4676 int64_t add;
4677 int64_t delta;
4678 /* Advance virtual time to the next event. */
4679 if (use_icount == 1) {
4680 /* When not using an adaptive execution frequency
4681 we tend to get badly out of sync with real time,
thsbf20dc02008-06-30 17:22:19 +00004682 so just delay for a reasonable amount of time. */
pbrook2e70f6e2008-06-29 01:03:05 +00004683 delta = 0;
4684 } else {
4685 delta = cpu_get_icount() - cpu_get_clock();
4686 }
4687 if (delta > 0) {
4688 /* If virtual time is ahead of real time then just
4689 wait for IO. */
4690 timeout = (delta / 1000000) + 1;
4691 } else {
4692 /* Wait for either IO to occur or the next
4693 timer event. */
4694 add = qemu_next_deadline();
4695 /* We advance the timer before checking for IO.
4696 Limit the amount we advance so that early IO
4697 activity won't get the guest too far ahead. */
4698 if (add > 10000000)
4699 add = 10000000;
4700 delta += add;
4701 add = (add + (1 << icount_time_shift) - 1)
4702 >> icount_time_shift;
4703 qemu_icount += add;
4704 timeout = delta / 1000000;
4705 if (timeout < 0)
4706 timeout = 0;
4707 }
4708 } else {
aliguori0a1af392008-10-31 18:40:25 +00004709 timeout = 5000;
pbrook2e70f6e2008-06-29 01:03:05 +00004710 }
4711 } else {
bellard5905b2e2004-08-01 21:53:26 +00004712 timeout = 0;
pbrook2e70f6e2008-06-29 01:03:05 +00004713 }
bellard5905b2e2004-08-01 21:53:26 +00004714 } else {
blueswir198448f52008-09-30 18:16:09 +00004715 if (shutdown_requested) {
4716 ret = EXCP_INTERRUPT;
aliguori5b08fc12008-08-21 20:08:03 +00004717 break;
blueswir198448f52008-09-30 18:16:09 +00004718 }
aliguori0a1af392008-10-31 18:40:25 +00004719 timeout = 5000;
bellard5905b2e2004-08-01 21:53:26 +00004720 }
bellard89bfc102006-02-08 22:46:31 +00004721#ifdef CONFIG_PROFILER
4722 ti = profile_getclock();
4723#endif
bellard5905b2e2004-08-01 21:53:26 +00004724 main_loop_wait(timeout);
bellard89bfc102006-02-08 22:46:31 +00004725#ifdef CONFIG_PROFILER
4726 dev_time += profile_getclock() - ti;
4727#endif
bellardb4608c02003-06-27 17:34:32 +00004728 }
bellard34865132003-10-05 14:28:56 +00004729 cpu_disable_ticks();
4730 return ret;
bellardb4608c02003-06-27 17:34:32 +00004731}
4732
ths15f82202007-06-29 23:26:08 +00004733static void help(int exitcode)
bellard0824d6f2003-06-24 13:42:40 +00004734{
bellard68d0f702008-01-06 17:21:48 +00004735 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
bellard0db63472003-10-27 21:37:46 +00004736 "usage: %s [options] [disk_image]\n"
bellard0824d6f2003-06-24 13:42:40 +00004737 "\n"
bellarda20dd502003-09-30 21:07:02 +00004738 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
bellardfc01f7e2003-06-30 10:03:06 +00004739 "\n"
bellarda20dd502003-09-30 21:07:02 +00004740 "Standard options:\n"
bellardcc1daa42005-06-05 14:49:17 +00004741 "-M machine select emulated machine (-M ? for list)\n"
pbrook5adb4832007-03-08 03:15:18 +00004742 "-cpu cpu select CPU (-cpu ? for list)\n"
bellardc45886d2004-01-05 00:02:06 +00004743 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
bellard36b486b2003-11-11 13:36:08 +00004744 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4745 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
bellardc4b1fcc2004-03-14 21:44:30 +00004746 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
aurel32a1620fa2008-04-29 05:58:01 +00004747 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4748 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
aliguori9f7965c2008-10-14 14:42:54 +00004749 " [,cache=writethrough|writeback|none][,format=f]\n"
thse4bcb142007-12-02 04:51:10 +00004750 " use 'file' as a drive image\n"
balrog3e3d5812007-04-30 02:09:25 +00004751 "-mtdblock file use 'file' as on-board Flash memory image\n"
pbrooka1bb27b2007-04-06 16:49:48 +00004752 "-sd file use 'file' as SecureDigital card image\n"
j_mayer86f55662007-04-24 06:52:59 +00004753 "-pflash file use 'file' as a parallel flash image\n"
thseec85c22007-01-05 17:41:07 +00004754 "-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 +00004755 "-snapshot write to temporary files instead of disk image files\n"
4756#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00004757 "-no-frame open SDL window without a frame and window decorations\n"
ths3780e192007-06-21 21:08:02 +00004758 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
ths667acca2006-12-11 02:08:05 +00004759 "-no-quit disable SDL window close capability\n"
4760#endif
bellard52ca8d62006-06-14 16:03:05 +00004761#ifdef TARGET_I386
4762 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4763#endif
bellarda00bad72004-05-22 21:39:06 +00004764 "-m megs set virtual RAM size to megs MB [default=%d]\n"
bellard91fc2112005-12-18 19:09:37 +00004765 "-smp n set the number of CPUs to 'n' [default=1]\n"
bellardc4b1fcc2004-03-14 21:44:30 +00004766 "-nographic disable graphical output and redirect serial I/Os to console\n"
balroga171fe32007-04-30 01:48:07 +00004767 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
bellard4ca00742004-12-12 22:20:04 +00004768#ifndef _WIN32
ths667acca2006-12-11 02:08:05 +00004769 "-k language use keyboard layout (for example \"fr\" for French)\n"
bellard4ca00742004-12-12 22:20:04 +00004770#endif
bellard1d14ffa2005-10-30 18:58:22 +00004771#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004772 "-audio-help print list of audio drivers and their options\n"
bellardc0fe3822005-11-05 18:55:28 +00004773 "-soundhw c1,... enable audio support\n"
4774 " and only specified sound cards (comma separated list)\n"
4775 " use -soundhw ? to get the list of supported cards\n"
bellard6a36d842005-12-18 20:34:32 +00004776 " use -soundhw all to enable all of them\n"
bellard1d14ffa2005-10-30 18:58:22 +00004777#endif
malc3893c122008-09-28 00:42:05 +00004778 "-vga [std|cirrus|vmware]\n"
4779 " select video card type\n"
bellard89980282004-06-03 14:04:03 +00004780 "-localtime set the real time clock to local time [default=utc]\n"
bellardd63d3072004-10-03 13:29:03 +00004781 "-full-screen start in full screen\n"
bellarda09db212005-04-30 16:10:35 +00004782#ifdef TARGET_I386
4783 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4784#endif
bellardb389dbf2005-11-06 16:49:55 +00004785 "-usb enable the USB driver (will be the default soon)\n"
4786 "-usbdevice name add the host or guest USB device 'name'\n"
bellard6f7e9ae2005-03-13 09:43:36 +00004787#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4788 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
bellardbb0c6722004-06-20 12:37:32 +00004789#endif
thsc35734b2007-03-19 15:17:08 +00004790 "-name string set the name of the guest\n"
blueswir18fcb1b92008-09-18 18:29:08 +00004791 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
bellarda20dd502003-09-30 21:07:02 +00004792 "\n"
bellardc4b1fcc2004-03-14 21:44:30 +00004793 "Network options:\n"
pbrooka41b2ff2006-02-05 04:14:41 +00004794 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
bellard7c9d8e02005-11-15 22:16:05 +00004795 " create a new Network Interface Card and connect it to VLAN 'n'\n"
bellardc20709a2004-04-21 23:27:19 +00004796#ifdef CONFIG_SLIRP
pbrook115defd2006-04-16 11:06:58 +00004797 "-net user[,vlan=n][,hostname=host]\n"
4798 " connect the user mode network stack to VLAN 'n' and send\n"
4799 " hostname 'host' to DHCP clients\n"
bellard7c9d8e02005-11-15 22:16:05 +00004800#endif
bellard7fb843f2006-02-01 23:06:55 +00004801#ifdef _WIN32
4802 "-net tap[,vlan=n],ifname=name\n"
4803 " connect the host TAP network interface to VLAN 'n'\n"
4804#else
thsb46a8902007-10-21 23:20:45 +00004805 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4806 " connect the host TAP network interface to VLAN 'n' and use the\n"
4807 " network scripts 'file' (default=%s)\n"
4808 " and 'dfile' (default=%s);\n"
4809 " use '[down]script=no' to disable script execution;\n"
bellard7c9d8e02005-11-15 22:16:05 +00004810 " use 'fd=h' to connect to an already opened TAP interface\n"
bellard7fb843f2006-02-01 23:06:55 +00004811#endif
bellard6a00d602005-11-21 23:25:50 +00004812 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
bellard7c9d8e02005-11-15 22:16:05 +00004813 " connect the vlan 'n' to another VLAN using a socket connection\n"
bellard3d830452005-12-18 16:36:49 +00004814 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4815 " connect the vlan 'n' to multicast maddr and port\n"
ths8a16d272008-07-19 09:56:24 +00004816#ifdef CONFIG_VDE
4817 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4818 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4819 " on host and listening for incoming connections on 'socketpath'.\n"
4820 " Use group 'groupname' and mode 'octalmode' to change default\n"
4821 " ownership and permissions for communication port.\n"
4822#endif
bellard7c9d8e02005-11-15 22:16:05 +00004823 "-net none use it alone to have zero network devices; if no -net option\n"
4824 " is provided, the default is '-net nic -net user'\n"
4825 "\n"
4826#ifdef CONFIG_SLIRP
ths0db11372007-02-20 00:12:07 +00004827 "-tftp dir allow tftp access to files in dir [-net user]\n"
ths47d5d012007-02-20 00:05:08 +00004828 "-bootp file advertise file in BOOTP replies\n"
bellard7c9d8e02005-11-15 22:16:05 +00004829#ifndef _WIN32
4830 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
bellardc94c8d62004-09-13 21:37:34 +00004831#endif
bellard9bf05442004-08-25 22:12:49 +00004832 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
bellard7c9d8e02005-11-15 22:16:05 +00004833 " redirect TCP or UDP connections from host to guest [-net user]\n"
bellardc20709a2004-04-21 23:27:19 +00004834#endif
bellardc4b1fcc2004-03-14 21:44:30 +00004835 "\n"
4836 "Linux boot specific:\n"
bellarda20dd502003-09-30 21:07:02 +00004837 "-kernel bzImage use 'bzImage' as kernel image\n"
4838 "-append cmdline use 'cmdline' as kernel command line\n"
4839 "-initrd file use 'file' as initial ram disk\n"
bellardfc01f7e2003-06-30 10:03:06 +00004840 "\n"
bellard330d0412003-07-26 18:11:40 +00004841 "Debug/Expert options:\n"
bellard82c643f2004-07-14 17:28:13 +00004842 "-monitor dev redirect the monitor to char device 'dev'\n"
4843 "-serial dev redirect the serial port to char device 'dev'\n"
bellard6508fe52005-01-15 12:02:56 +00004844 "-parallel dev redirect the parallel port to char device 'dev'\n"
bellardf7cce892004-12-08 22:21:25 +00004845 "-pidfile file Write PID to 'file'\n"
bellardcd6f1162004-05-13 22:02:20 +00004846 "-S freeze CPU at startup (use 'c' to start execution)\n"
pbrookcfc34752007-02-22 01:48:01 +00004847 "-s wait gdb connection to port\n"
4848 "-p port set gdb connection port [default=%s]\n"
bellardf193c792004-03-21 17:06:25 +00004849 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
bellard46d47672004-11-16 01:45:27 +00004850 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4851 " translation (t=none or lba) (usually qemu can guess them)\n"
bellard87b47352006-08-17 17:22:54 +00004852 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
bellardd993e022005-02-10 22:00:06 +00004853#ifdef USE_KQEMU
bellard6515b202006-05-03 22:02:44 +00004854 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
bellardd993e022005-02-10 22:00:06 +00004855 "-no-kqemu disable KQEMU kernel module usage\n"
4856#endif
aliguori7ba1e612008-11-05 16:04:33 +00004857#ifdef CONFIG_KVM
4858 "-enable-kvm enable KVM full virtualization support\n"
4859#endif
bellardbb0c6722004-06-20 12:37:32 +00004860#ifdef TARGET_I386
bellard6515b202006-05-03 22:02:44 +00004861 "-no-acpi disable ACPI\n"
bellardbb0c6722004-06-20 12:37:32 +00004862#endif
balrog4d3b6f62008-02-10 16:33:14 +00004863#ifdef CONFIG_CURSES
4864 "-curses use a curses/ncurses interface instead of SDL\n"
4865#endif
bellardd1beab82006-10-02 19:44:22 +00004866 "-no-reboot exit instead of rebooting\n"
aurel32b2f76162008-04-11 21:35:52 +00004867 "-no-shutdown stop before shutdown\n"
aurel32a8080002008-05-10 23:28:26 +00004868 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
bellard24236862006-04-30 21:28:36 +00004869 "-vnc display start a VNC server on display\n"
ths71e3ceb2006-12-22 02:11:31 +00004870#ifndef _WIN32
4871 "-daemonize daemonize QEMU after initializing\n"
4872#endif
ths9ae02552007-01-05 17:39:04 +00004873 "-option-rom rom load a file, rom, into the option ROM space\n"
blueswir166508602007-05-01 14:16:52 +00004874#ifdef TARGET_SPARC
4875 "-prom-env variable=value set OpenBIOS nvram variables\n"
4876#endif
thsf3dcfad2007-08-24 01:26:02 +00004877 "-clock force the use of the given methods for timer alarm.\n"
aurel323adda042008-03-09 23:43:49 +00004878 " To see what timers are available use -clock ?\n"
bellardbce61842008-02-01 22:18:51 +00004879 "-startdate select initial date of the clock\n"
pbrook2e70f6e2008-06-29 01:03:05 +00004880 "-icount [N|auto]\n"
pbrookdd5d6fe2008-06-29 10:43:16 +00004881 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
bellard0824d6f2003-06-24 13:42:40 +00004882 "\n"
bellard82c643f2004-07-14 17:28:13 +00004883 "During emulation, the following keys are useful:\n"
bellard032a8c92004-10-09 22:56:44 +00004884 "ctrl-alt-f toggle full screen\n"
4885 "ctrl-alt-n switch to virtual console 'n'\n"
4886 "ctrl-alt toggle mouse and keyboard grab\n"
bellard82c643f2004-07-14 17:28:13 +00004887 "\n"
4888 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4889 ,
bellard0db63472003-10-27 21:37:46 +00004890 "qemu",
bellarda00bad72004-05-22 21:39:06 +00004891 DEFAULT_RAM_SIZE,
bellard7c9d8e02005-11-15 22:16:05 +00004892#ifndef _WIN32
bellarda00bad72004-05-22 21:39:06 +00004893 DEFAULT_NETWORK_SCRIPT,
thsb46a8902007-10-21 23:20:45 +00004894 DEFAULT_NETWORK_DOWN_SCRIPT,
bellard7c9d8e02005-11-15 22:16:05 +00004895#endif
bellard6e44ba72004-01-18 21:56:49 +00004896 DEFAULT_GDBSTUB_PORT,
bellardbce61842008-02-01 22:18:51 +00004897 "/tmp/qemu.log");
ths15f82202007-06-29 23:26:08 +00004898 exit(exitcode);
bellard0824d6f2003-06-24 13:42:40 +00004899}
4900
bellardcd6f1162004-05-13 22:02:20 +00004901#define HAS_ARG 0x0001
4902
4903enum {
4904 QEMU_OPTION_h,
4905
bellardcc1daa42005-06-05 14:49:17 +00004906 QEMU_OPTION_M,
j_mayer94fc95c2007-03-05 19:44:02 +00004907 QEMU_OPTION_cpu,
bellardcd6f1162004-05-13 22:02:20 +00004908 QEMU_OPTION_fda,
4909 QEMU_OPTION_fdb,
4910 QEMU_OPTION_hda,
4911 QEMU_OPTION_hdb,
4912 QEMU_OPTION_hdc,
4913 QEMU_OPTION_hdd,
thse4bcb142007-12-02 04:51:10 +00004914 QEMU_OPTION_drive,
bellardcd6f1162004-05-13 22:02:20 +00004915 QEMU_OPTION_cdrom,
balrog3e3d5812007-04-30 02:09:25 +00004916 QEMU_OPTION_mtdblock,
pbrooka1bb27b2007-04-06 16:49:48 +00004917 QEMU_OPTION_sd,
j_mayer86f55662007-04-24 06:52:59 +00004918 QEMU_OPTION_pflash,
bellardcd6f1162004-05-13 22:02:20 +00004919 QEMU_OPTION_boot,
4920 QEMU_OPTION_snapshot,
bellard52ca8d62006-06-14 16:03:05 +00004921#ifdef TARGET_I386
4922 QEMU_OPTION_no_fd_bootchk,
4923#endif
bellardcd6f1162004-05-13 22:02:20 +00004924 QEMU_OPTION_m,
4925 QEMU_OPTION_nographic,
balroga171fe32007-04-30 01:48:07 +00004926 QEMU_OPTION_portrait,
bellard1d14ffa2005-10-30 18:58:22 +00004927#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00004928 QEMU_OPTION_audio_help,
4929 QEMU_OPTION_soundhw,
4930#endif
bellardcd6f1162004-05-13 22:02:20 +00004931
bellard7c9d8e02005-11-15 22:16:05 +00004932 QEMU_OPTION_net,
bellardc7f74642004-08-24 21:57:12 +00004933 QEMU_OPTION_tftp,
ths47d5d012007-02-20 00:05:08 +00004934 QEMU_OPTION_bootp,
bellard9d728e82004-09-05 23:09:03 +00004935 QEMU_OPTION_smb,
bellard9bf05442004-08-25 22:12:49 +00004936 QEMU_OPTION_redir,
bellardcd6f1162004-05-13 22:02:20 +00004937
4938 QEMU_OPTION_kernel,
4939 QEMU_OPTION_append,
4940 QEMU_OPTION_initrd,
4941
4942 QEMU_OPTION_S,
4943 QEMU_OPTION_s,
4944 QEMU_OPTION_p,
4945 QEMU_OPTION_d,
4946 QEMU_OPTION_hdachs,
4947 QEMU_OPTION_L,
j_mayer1192dad2007-10-05 13:08:35 +00004948 QEMU_OPTION_bios,
bellard3d11d0e2004-12-12 16:56:30 +00004949 QEMU_OPTION_k,
bellardee22c2f2004-06-03 12:49:50 +00004950 QEMU_OPTION_localtime,
bellarde9b137c2004-06-21 16:46:10 +00004951 QEMU_OPTION_g,
malc3893c122008-09-28 00:42:05 +00004952 QEMU_OPTION_vga,
ths20d8a3e2007-02-18 17:04:49 +00004953 QEMU_OPTION_echr,
bellard82c643f2004-07-14 17:28:13 +00004954 QEMU_OPTION_monitor,
4955 QEMU_OPTION_serial,
bellard6508fe52005-01-15 12:02:56 +00004956 QEMU_OPTION_parallel,
bellardd63d3072004-10-03 13:29:03 +00004957 QEMU_OPTION_loadvm,
4958 QEMU_OPTION_full_screen,
ths43523e92007-02-18 18:19:32 +00004959 QEMU_OPTION_no_frame,
ths3780e192007-06-21 21:08:02 +00004960 QEMU_OPTION_alt_grab,
ths667acca2006-12-11 02:08:05 +00004961 QEMU_OPTION_no_quit,
bellardf7cce892004-12-08 22:21:25 +00004962 QEMU_OPTION_pidfile,
bellardd993e022005-02-10 22:00:06 +00004963 QEMU_OPTION_no_kqemu,
bellard89bfc102006-02-08 22:46:31 +00004964 QEMU_OPTION_kernel_kqemu,
aliguori7ba1e612008-11-05 16:04:33 +00004965 QEMU_OPTION_enable_kvm,
bellarda09db212005-04-30 16:10:35 +00004966 QEMU_OPTION_win2k_hack,
bellardbb36d472005-11-05 14:22:28 +00004967 QEMU_OPTION_usb,
bellarda594cfb2005-11-06 16:13:29 +00004968 QEMU_OPTION_usbdevice,
bellard6a00d602005-11-21 23:25:50 +00004969 QEMU_OPTION_smp,
bellard24236862006-04-30 21:28:36 +00004970 QEMU_OPTION_vnc,
bellard6515b202006-05-03 22:02:44 +00004971 QEMU_OPTION_no_acpi,
balrog4d3b6f62008-02-10 16:33:14 +00004972 QEMU_OPTION_curses,
bellardd1beab82006-10-02 19:44:22 +00004973 QEMU_OPTION_no_reboot,
aurel32b2f76162008-04-11 21:35:52 +00004974 QEMU_OPTION_no_shutdown,
balrog9467cd42007-05-01 01:34:14 +00004975 QEMU_OPTION_show_cursor,
ths71e3ceb2006-12-22 02:11:31 +00004976 QEMU_OPTION_daemonize,
ths9ae02552007-01-05 17:39:04 +00004977 QEMU_OPTION_option_rom,
thsc35734b2007-03-19 15:17:08 +00004978 QEMU_OPTION_semihosting,
4979 QEMU_OPTION_name,
blueswir166508602007-05-01 14:16:52 +00004980 QEMU_OPTION_prom_env,
balrog2b8f2d42007-07-27 22:08:46 +00004981 QEMU_OPTION_old_param,
thsf3dcfad2007-08-24 01:26:02 +00004982 QEMU_OPTION_clock,
bellard7e0af5d02007-11-07 16:24:33 +00004983 QEMU_OPTION_startdate,
bellard26a5f132008-05-28 12:30:31 +00004984 QEMU_OPTION_tb_size,
pbrook2e70f6e2008-06-29 01:03:05 +00004985 QEMU_OPTION_icount,
blueswir18fcb1b92008-09-18 18:29:08 +00004986 QEMU_OPTION_uuid,
aliguori5bb79102008-10-13 03:12:02 +00004987 QEMU_OPTION_incoming,
bellardcd6f1162004-05-13 22:02:20 +00004988};
4989
4990typedef struct QEMUOption {
4991 const char *name;
4992 int flags;
4993 int index;
4994} QEMUOption;
4995
blueswir1dbed7e42008-10-01 19:38:09 +00004996static const QEMUOption qemu_options[] = {
bellardcd6f1162004-05-13 22:02:20 +00004997 { "h", 0, QEMU_OPTION_h },
pbrook64423fb2007-01-27 17:11:41 +00004998 { "help", 0, QEMU_OPTION_h },
bellardcd6f1162004-05-13 22:02:20 +00004999
bellardcc1daa42005-06-05 14:49:17 +00005000 { "M", HAS_ARG, QEMU_OPTION_M },
j_mayer94fc95c2007-03-05 19:44:02 +00005001 { "cpu", HAS_ARG, QEMU_OPTION_cpu },
bellardcd6f1162004-05-13 22:02:20 +00005002 { "fda", HAS_ARG, QEMU_OPTION_fda },
5003 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
5004 { "hda", HAS_ARG, QEMU_OPTION_hda },
5005 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
5006 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
5007 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
thse4bcb142007-12-02 04:51:10 +00005008 { "drive", HAS_ARG, QEMU_OPTION_drive },
bellardcd6f1162004-05-13 22:02:20 +00005009 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
balrog3e3d5812007-04-30 02:09:25 +00005010 { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
pbrooka1bb27b2007-04-06 16:49:48 +00005011 { "sd", HAS_ARG, QEMU_OPTION_sd },
j_mayer86f55662007-04-24 06:52:59 +00005012 { "pflash", HAS_ARG, QEMU_OPTION_pflash },
bellardcd6f1162004-05-13 22:02:20 +00005013 { "boot", HAS_ARG, QEMU_OPTION_boot },
5014 { "snapshot", 0, QEMU_OPTION_snapshot },
bellard52ca8d62006-06-14 16:03:05 +00005015#ifdef TARGET_I386
5016 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
5017#endif
bellardcd6f1162004-05-13 22:02:20 +00005018 { "m", HAS_ARG, QEMU_OPTION_m },
5019 { "nographic", 0, QEMU_OPTION_nographic },
balroga171fe32007-04-30 01:48:07 +00005020 { "portrait", 0, QEMU_OPTION_portrait },
bellard3d11d0e2004-12-12 16:56:30 +00005021 { "k", HAS_ARG, QEMU_OPTION_k },
bellard1d14ffa2005-10-30 18:58:22 +00005022#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005023 { "audio-help", 0, QEMU_OPTION_audio_help },
5024 { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
5025#endif
bellardcd6f1162004-05-13 22:02:20 +00005026
bellard7c9d8e02005-11-15 22:16:05 +00005027 { "net", HAS_ARG, QEMU_OPTION_net},
bellard158156d2004-05-17 21:13:42 +00005028#ifdef CONFIG_SLIRP
bellardc7f74642004-08-24 21:57:12 +00005029 { "tftp", HAS_ARG, QEMU_OPTION_tftp },
ths47d5d012007-02-20 00:05:08 +00005030 { "bootp", HAS_ARG, QEMU_OPTION_bootp },
bellardc94c8d62004-09-13 21:37:34 +00005031#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005032 { "smb", HAS_ARG, QEMU_OPTION_smb },
bellardc94c8d62004-09-13 21:37:34 +00005033#endif
bellard9bf05442004-08-25 22:12:49 +00005034 { "redir", HAS_ARG, QEMU_OPTION_redir },
bellard158156d2004-05-17 21:13:42 +00005035#endif
bellardcd6f1162004-05-13 22:02:20 +00005036
5037 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
5038 { "append", HAS_ARG, QEMU_OPTION_append },
5039 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
5040
5041 { "S", 0, QEMU_OPTION_S },
5042 { "s", 0, QEMU_OPTION_s },
5043 { "p", HAS_ARG, QEMU_OPTION_p },
5044 { "d", HAS_ARG, QEMU_OPTION_d },
5045 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
5046 { "L", HAS_ARG, QEMU_OPTION_L },
j_mayer1192dad2007-10-05 13:08:35 +00005047 { "bios", HAS_ARG, QEMU_OPTION_bios },
bellardd993e022005-02-10 22:00:06 +00005048#ifdef USE_KQEMU
5049 { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
bellard89bfc102006-02-08 22:46:31 +00005050 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
bellardd993e022005-02-10 22:00:06 +00005051#endif
aliguori7ba1e612008-11-05 16:04:33 +00005052#ifdef CONFIG_KVM
5053 { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
5054#endif
bellard6f7e9ae2005-03-13 09:43:36 +00005055#if defined(TARGET_PPC) || defined(TARGET_SPARC)
bellarde9b137c2004-06-21 16:46:10 +00005056 { "g", 1, QEMU_OPTION_g },
bellard77d4bc32004-05-26 22:13:53 +00005057#endif
bellardee22c2f2004-06-03 12:49:50 +00005058 { "localtime", 0, QEMU_OPTION_localtime },
malc3893c122008-09-28 00:42:05 +00005059 { "vga", HAS_ARG, QEMU_OPTION_vga },
balrog8b6e0722007-06-22 08:23:44 +00005060 { "echr", HAS_ARG, QEMU_OPTION_echr },
5061 { "monitor", HAS_ARG, QEMU_OPTION_monitor },
5062 { "serial", HAS_ARG, QEMU_OPTION_serial },
5063 { "parallel", HAS_ARG, QEMU_OPTION_parallel },
bellardd63d3072004-10-03 13:29:03 +00005064 { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
5065 { "full-screen", 0, QEMU_OPTION_full_screen },
ths667acca2006-12-11 02:08:05 +00005066#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005067 { "no-frame", 0, QEMU_OPTION_no_frame },
ths3780e192007-06-21 21:08:02 +00005068 { "alt-grab", 0, QEMU_OPTION_alt_grab },
ths667acca2006-12-11 02:08:05 +00005069 { "no-quit", 0, QEMU_OPTION_no_quit },
5070#endif
bellardf7cce892004-12-08 22:21:25 +00005071 { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
bellarda09db212005-04-30 16:10:35 +00005072 { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
bellarda594cfb2005-11-06 16:13:29 +00005073 { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
bellard6a00d602005-11-21 23:25:50 +00005074 { "smp", HAS_ARG, QEMU_OPTION_smp },
bellard24236862006-04-30 21:28:36 +00005075 { "vnc", HAS_ARG, QEMU_OPTION_vnc },
balrog4d3b6f62008-02-10 16:33:14 +00005076#ifdef CONFIG_CURSES
5077 { "curses", 0, QEMU_OPTION_curses },
5078#endif
blueswir18fcb1b92008-09-18 18:29:08 +00005079 { "uuid", HAS_ARG, QEMU_OPTION_uuid },
ths96d30e42007-01-07 20:42:14 +00005080
bellard1f042752004-06-05 13:46:47 +00005081 /* temporary options */
bellarda594cfb2005-11-06 16:13:29 +00005082 { "usb", 0, QEMU_OPTION_usb },
bellard6515b202006-05-03 22:02:44 +00005083 { "no-acpi", 0, QEMU_OPTION_no_acpi },
bellardd1beab82006-10-02 19:44:22 +00005084 { "no-reboot", 0, QEMU_OPTION_no_reboot },
aurel32b2f76162008-04-11 21:35:52 +00005085 { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
balrog9467cd42007-05-01 01:34:14 +00005086 { "show-cursor", 0, QEMU_OPTION_show_cursor },
ths71e3ceb2006-12-22 02:11:31 +00005087 { "daemonize", 0, QEMU_OPTION_daemonize },
ths9ae02552007-01-05 17:39:04 +00005088 { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
pbrooka87295e2007-05-26 15:09:38 +00005089#if defined(TARGET_ARM) || defined(TARGET_M68K)
pbrook8e716212007-01-20 17:12:09 +00005090 { "semihosting", 0, QEMU_OPTION_semihosting },
5091#endif
thsc35734b2007-03-19 15:17:08 +00005092 { "name", HAS_ARG, QEMU_OPTION_name },
blueswir166508602007-05-01 14:16:52 +00005093#if defined(TARGET_SPARC)
5094 { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
5095#endif
balrog2b8f2d42007-07-27 22:08:46 +00005096#if defined(TARGET_ARM)
5097 { "old-param", 0, QEMU_OPTION_old_param },
5098#endif
thsf3dcfad2007-08-24 01:26:02 +00005099 { "clock", HAS_ARG, QEMU_OPTION_clock },
bellard7e0af5d02007-11-07 16:24:33 +00005100 { "startdate", HAS_ARG, QEMU_OPTION_startdate },
bellard26a5f132008-05-28 12:30:31 +00005101 { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
pbrook2e70f6e2008-06-29 01:03:05 +00005102 { "icount", HAS_ARG, QEMU_OPTION_icount },
aliguori5bb79102008-10-13 03:12:02 +00005103 { "incoming", HAS_ARG, QEMU_OPTION_incoming },
bellardcd6f1162004-05-13 22:02:20 +00005104 { NULL },
bellardfc01f7e2003-06-30 10:03:06 +00005105};
5106
bellard5905b2e2004-08-01 21:53:26 +00005107/* password input */
5108
balrog2bac6012007-04-30 01:34:31 +00005109int qemu_key_check(BlockDriverState *bs, const char *name)
5110{
5111 char password[256];
5112 int i;
5113
5114 if (!bdrv_is_encrypted(bs))
5115 return 0;
5116
5117 term_printf("%s is encrypted.\n", name);
5118 for(i = 0; i < 3; i++) {
5119 monitor_readline("Password: ", 1, password, sizeof(password));
5120 if (bdrv_set_key(bs, password) == 0)
5121 return 0;
5122 term_printf("invalid password\n");
5123 }
5124 return -EPERM;
5125}
5126
aliguori83ab7952008-08-19 14:44:22 +00005127static BlockDriverState *get_bdrv(int index)
5128{
5129 if (index > nb_drives)
5130 return NULL;
5131 return drives_table[index].bdrv;
5132}
5133
bellard5905b2e2004-08-01 21:53:26 +00005134static void read_passwords(void)
5135{
5136 BlockDriverState *bs;
balrog2bac6012007-04-30 01:34:31 +00005137 int i;
bellard5905b2e2004-08-01 21:53:26 +00005138
aliguori83ab7952008-08-19 14:44:22 +00005139 for(i = 0; i < 6; i++) {
5140 bs = get_bdrv(i);
5141 if (bs)
5142 qemu_key_check(bs, bdrv_get_device_name(bs));
bellard5905b2e2004-08-01 21:53:26 +00005143 }
5144}
5145
bellard1d14ffa2005-10-30 18:58:22 +00005146#ifdef HAS_AUDIO
bellard6a36d842005-12-18 20:34:32 +00005147struct soundhw soundhw[] = {
balrogb00052e2007-04-30 02:22:06 +00005148#ifdef HAS_AUDIO_CHOICE
aurel324ce7ff62008-04-07 19:47:14 +00005149#if defined(TARGET_I386) || defined(TARGET_MIPS)
bellardfd06c372006-04-24 21:58:30 +00005150 {
5151 "pcspk",
5152 "PC speaker",
5153 0,
5154 1,
5155 { .init_isa = pcspk_audio_init }
5156 },
5157#endif
bellard6a36d842005-12-18 20:34:32 +00005158 {
5159 "sb16",
5160 "Creative Sound Blaster 16",
5161 0,
5162 1,
5163 { .init_isa = SB16_init }
5164 },
5165
malccc53d262008-06-13 10:48:22 +00005166#ifdef CONFIG_CS4231A
5167 {
5168 "cs4231a",
5169 "CS4231A",
5170 0,
5171 1,
5172 { .init_isa = cs4231a_init }
5173 },
5174#endif
5175
bellard6a36d842005-12-18 20:34:32 +00005176#ifdef CONFIG_ADLIB
5177 {
5178 "adlib",
5179#ifdef HAS_YMF262
5180 "Yamaha YMF262 (OPL3)",
5181#else
5182 "Yamaha YM3812 (OPL2)",
5183#endif
5184 0,
5185 1,
5186 { .init_isa = Adlib_init }
5187 },
5188#endif
5189
5190#ifdef CONFIG_GUS
5191 {
5192 "gus",
5193 "Gravis Ultrasound GF1",
5194 0,
5195 1,
5196 { .init_isa = GUS_init }
5197 },
5198#endif
5199
balroge5c9a132008-01-14 04:27:55 +00005200#ifdef CONFIG_AC97
5201 {
5202 "ac97",
5203 "Intel 82801AA AC97 Audio",
5204 0,
5205 0,
5206 { .init_pci = ac97_init }
5207 },
5208#endif
5209
bellard6a36d842005-12-18 20:34:32 +00005210 {
5211 "es1370",
5212 "ENSONIQ AudioPCI ES1370",
5213 0,
5214 0,
5215 { .init_pci = es1370_init }
5216 },
balrogb00052e2007-04-30 02:22:06 +00005217#endif
bellard6a36d842005-12-18 20:34:32 +00005218
5219 { NULL, NULL, 0, 0, { NULL } }
5220};
5221
bellard1d14ffa2005-10-30 18:58:22 +00005222static void select_soundhw (const char *optarg)
5223{
bellard6a36d842005-12-18 20:34:32 +00005224 struct soundhw *c;
5225
bellard1d14ffa2005-10-30 18:58:22 +00005226 if (*optarg == '?') {
5227 show_valid_cards:
bellard6a36d842005-12-18 20:34:32 +00005228
bellard1d14ffa2005-10-30 18:58:22 +00005229 printf ("Valid sound card names (comma separated):\n");
bellard6a36d842005-12-18 20:34:32 +00005230 for (c = soundhw; c->name; ++c) {
5231 printf ("%-11s %s\n", c->name, c->descr);
5232 }
5233 printf ("\n-soundhw all will enable all of the above\n");
bellard1d14ffa2005-10-30 18:58:22 +00005234 exit (*optarg != '?');
5235 }
5236 else {
bellard6a36d842005-12-18 20:34:32 +00005237 size_t l;
bellard1d14ffa2005-10-30 18:58:22 +00005238 const char *p;
5239 char *e;
5240 int bad_card = 0;
5241
bellard6a36d842005-12-18 20:34:32 +00005242 if (!strcmp (optarg, "all")) {
5243 for (c = soundhw; c->name; ++c) {
5244 c->enabled = 1;
5245 }
5246 return;
5247 }
bellard1d14ffa2005-10-30 18:58:22 +00005248
bellard6a36d842005-12-18 20:34:32 +00005249 p = optarg;
bellard1d14ffa2005-10-30 18:58:22 +00005250 while (*p) {
5251 e = strchr (p, ',');
5252 l = !e ? strlen (p) : (size_t) (e - p);
bellard6a36d842005-12-18 20:34:32 +00005253
5254 for (c = soundhw; c->name; ++c) {
5255 if (!strncmp (c->name, p, l)) {
5256 c->enabled = 1;
bellard1d14ffa2005-10-30 18:58:22 +00005257 break;
5258 }
5259 }
bellard6a36d842005-12-18 20:34:32 +00005260
5261 if (!c->name) {
bellard1d14ffa2005-10-30 18:58:22 +00005262 if (l > 80) {
5263 fprintf (stderr,
5264 "Unknown sound card name (too big to show)\n");
5265 }
5266 else {
5267 fprintf (stderr, "Unknown sound card name `%.*s'\n",
5268 (int) l, p);
5269 }
5270 bad_card = 1;
5271 }
5272 p += l + (e != NULL);
5273 }
5274
5275 if (bad_card)
5276 goto show_valid_cards;
5277 }
5278}
5279#endif
5280
malc3893c122008-09-28 00:42:05 +00005281static void select_vgahw (const char *p)
5282{
5283 const char *opts;
5284
5285 if (strstart(p, "std", &opts)) {
5286 cirrus_vga_enabled = 0;
5287 vmsvga_enabled = 0;
5288 } else if (strstart(p, "cirrus", &opts)) {
5289 cirrus_vga_enabled = 1;
5290 vmsvga_enabled = 0;
5291 } else if (strstart(p, "vmware", &opts)) {
5292 cirrus_vga_enabled = 0;
5293 vmsvga_enabled = 1;
5294 } else {
5295 invalid_vga:
5296 fprintf(stderr, "Unknown vga type: %s\n", p);
5297 exit(1);
5298 }
malccb5a7aa2008-09-28 00:42:12 +00005299 while (*opts) {
5300 const char *nextopt;
5301
5302 if (strstart(opts, ",retrace=", &nextopt)) {
5303 opts = nextopt;
5304 if (strstart(opts, "dumb", &nextopt))
5305 vga_retrace_method = VGA_RETRACE_DUMB;
5306 else if (strstart(opts, "precise", &nextopt))
5307 vga_retrace_method = VGA_RETRACE_PRECISE;
5308 else goto invalid_vga;
5309 } else goto invalid_vga;
5310 opts = nextopt;
5311 }
malc3893c122008-09-28 00:42:05 +00005312}
5313
bellard3587d7e2006-06-26 20:03:44 +00005314#ifdef _WIN32
5315static BOOL WINAPI qemu_ctrl_handler(DWORD type)
5316{
5317 exit(STATUS_CONTROL_C_EXIT);
5318 return TRUE;
5319}
5320#endif
5321
blueswir18fcb1b92008-09-18 18:29:08 +00005322static int qemu_uuid_parse(const char *str, uint8_t *uuid)
5323{
5324 int ret;
5325
5326 if(strlen(str) != 36)
5327 return -1;
5328
5329 ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
5330 &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
5331 &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
5332
5333 if(ret != 16)
5334 return -1;
5335
5336 return 0;
5337}
5338
bellard7c9d8e02005-11-15 22:16:05 +00005339#define MAX_NET_CLIENTS 32
bellardc20709a2004-04-21 23:27:19 +00005340
aliguori5b08fc12008-08-21 20:08:03 +00005341#ifndef _WIN32
5342
5343static void termsig_handler(int signal)
5344{
5345 qemu_system_shutdown_request();
5346}
5347
blueswir16f9e3802008-09-09 18:56:59 +00005348static void termsig_setup(void)
aliguori5b08fc12008-08-21 20:08:03 +00005349{
5350 struct sigaction act;
5351
5352 memset(&act, 0, sizeof(act));
5353 act.sa_handler = termsig_handler;
5354 sigaction(SIGINT, &act, NULL);
5355 sigaction(SIGHUP, &act, NULL);
5356 sigaction(SIGTERM, &act, NULL);
5357}
5358
5359#endif
5360
bellard0824d6f2003-06-24 13:42:40 +00005361int main(int argc, char **argv)
5362{
bellard67b915a2004-03-31 23:37:16 +00005363#ifdef CONFIG_GDBSTUB
pbrookcfc34752007-02-22 01:48:01 +00005364 int use_gdbstub;
5365 const char *gdbstub_port;
bellard67b915a2004-03-31 23:37:16 +00005366#endif
j_mayer28c5af52007-11-11 01:50:45 +00005367 uint32_t boot_devices_bitmap = 0;
thse4bcb142007-12-02 04:51:10 +00005368 int i;
j_mayer28c5af52007-11-11 01:50:45 +00005369 int snapshot, linux_boot, net_boot;
bellard7f7f9872003-10-30 01:11:23 +00005370 const char *initrd_filename;
bellarda20dd502003-09-30 21:07:02 +00005371 const char *kernel_filename, *kernel_cmdline;
j_mayer28c5af52007-11-11 01:50:45 +00005372 const char *boot_devices = "";
bellard313aa562003-08-10 21:52:11 +00005373 DisplayState *ds = &display_state;
bellard46d47672004-11-16 01:45:27 +00005374 int cyls, heads, secs, translation;
pbrookfd5f3932008-03-26 20:55:43 +00005375 const char *net_clients[MAX_NET_CLIENTS];
bellard7c9d8e02005-11-15 22:16:05 +00005376 int nb_net_clients;
thse4bcb142007-12-02 04:51:10 +00005377 int hda_index;
bellardcd6f1162004-05-13 22:02:20 +00005378 int optind;
5379 const char *r, *optarg;
bellard82c643f2004-07-14 17:28:13 +00005380 CharDriverState *monitor_hd;
pbrookfd5f3932008-03-26 20:55:43 +00005381 const char *monitor_device;
5382 const char *serial_devices[MAX_SERIAL_PORTS];
bellard8d11df92004-08-24 21:13:40 +00005383 int serial_device_index;
pbrookfd5f3932008-03-26 20:55:43 +00005384 const char *parallel_devices[MAX_PARALLEL_PORTS];
bellard6508fe52005-01-15 12:02:56 +00005385 int parallel_device_index;
bellardd63d3072004-10-03 13:29:03 +00005386 const char *loadvm = NULL;
bellardcc1daa42005-06-05 14:49:17 +00005387 QEMUMachine *machine;
j_mayer94fc95c2007-03-05 19:44:02 +00005388 const char *cpu_model;
pbrookfd5f3932008-03-26 20:55:43 +00005389 const char *usb_devices[MAX_USB_CMDLINE];
bellarda594cfb2005-11-06 16:13:29 +00005390 int usb_devices_index;
ths71e3ceb2006-12-22 02:11:31 +00005391 int fds[2];
bellard26a5f132008-05-28 12:30:31 +00005392 int tb_size;
ths93815bc2007-03-19 15:58:31 +00005393 const char *pid_file = NULL;
blueswir141bd6392008-10-05 09:56:21 +00005394 int autostart;
aliguori5bb79102008-10-13 03:12:02 +00005395 const char *incoming = NULL;
bellard0bd48852005-11-11 00:00:47 +00005396
5397 LIST_INIT (&vm_change_state_head);
bellardbe995c22006-06-25 16:25:21 +00005398#ifndef _WIN32
5399 {
5400 struct sigaction act;
5401 sigfillset(&act.sa_mask);
5402 act.sa_flags = 0;
5403 act.sa_handler = SIG_IGN;
5404 sigaction(SIGPIPE, &act, NULL);
5405 }
bellard3587d7e2006-06-26 20:03:44 +00005406#else
5407 SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
bellarda8e5ac32006-07-14 09:36:13 +00005408 /* Note: cpu_interrupt() is currently not SMP safe, so we force
5409 QEMU to run on a single CPU */
5410 {
5411 HANDLE h;
5412 DWORD mask, smask;
5413 int i;
5414 h = GetCurrentProcess();
5415 if (GetProcessAffinityMask(h, &mask, &smask)) {
5416 for(i = 0; i < 32; i++) {
5417 if (mask & (1 << i))
5418 break;
5419 }
5420 if (i != 32) {
5421 mask = 1 << i;
5422 SetProcessAffinityMask(h, mask);
5423 }
5424 }
5425 }
bellard67b915a2004-03-31 23:37:16 +00005426#endif
bellardbe995c22006-06-25 16:25:21 +00005427
bellardcc1daa42005-06-05 14:49:17 +00005428 register_machines();
5429 machine = first_machine;
j_mayer94fc95c2007-03-05 19:44:02 +00005430 cpu_model = NULL;
bellardfc01f7e2003-06-30 10:03:06 +00005431 initrd_filename = NULL;
aurel324fc5d072008-04-27 21:39:40 +00005432 ram_size = 0;
bellard313aa562003-08-10 21:52:11 +00005433 vga_ram_size = VGA_RAM_SIZE;
bellard67b915a2004-03-31 23:37:16 +00005434#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00005435 use_gdbstub = 0;
bellardc636bb62007-02-05 20:46:05 +00005436 gdbstub_port = DEFAULT_GDBSTUB_PORT;
bellard67b915a2004-03-31 23:37:16 +00005437#endif
bellard33e39632003-07-06 17:15:21 +00005438 snapshot = 0;
bellarda20dd502003-09-30 21:07:02 +00005439 nographic = 0;
balrog4d3b6f62008-02-10 16:33:14 +00005440 curses = 0;
bellarda20dd502003-09-30 21:07:02 +00005441 kernel_filename = NULL;
5442 kernel_cmdline = "";
bellardc4b1fcc2004-03-14 21:44:30 +00005443 cyls = heads = secs = 0;
bellard46d47672004-11-16 01:45:27 +00005444 translation = BIOS_ATA_TRANSLATION_AUTO;
pbrookc60e08d2008-07-01 16:24:38 +00005445 monitor_device = "vc";
bellardc4b1fcc2004-03-14 21:44:30 +00005446
aurel32c75a8232008-05-04 00:50:34 +00005447 serial_devices[0] = "vc:80Cx24C";
bellard8d11df92004-08-24 21:13:40 +00005448 for(i = 1; i < MAX_SERIAL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00005449 serial_devices[i] = NULL;
bellard8d11df92004-08-24 21:13:40 +00005450 serial_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00005451
aurel32c75a8232008-05-04 00:50:34 +00005452 parallel_devices[0] = "vc:640x480";
bellard6508fe52005-01-15 12:02:56 +00005453 for(i = 1; i < MAX_PARALLEL_PORTS; i++)
pbrookfd5f3932008-03-26 20:55:43 +00005454 parallel_devices[i] = NULL;
bellard6508fe52005-01-15 12:02:56 +00005455 parallel_device_index = 0;
ths3b46e622007-09-17 08:09:54 +00005456
bellarda594cfb2005-11-06 16:13:29 +00005457 usb_devices_index = 0;
ths3b46e622007-09-17 08:09:54 +00005458
bellard7c9d8e02005-11-15 22:16:05 +00005459 nb_net_clients = 0;
thse4bcb142007-12-02 04:51:10 +00005460 nb_drives = 0;
5461 nb_drives_opt = 0;
5462 hda_index = -1;
bellard7c9d8e02005-11-15 22:16:05 +00005463
5464 nb_nics = 0;
ths3b46e622007-09-17 08:09:54 +00005465
bellard26a5f132008-05-28 12:30:31 +00005466 tb_size = 0;
blueswir141bd6392008-10-05 09:56:21 +00005467 autostart= 1;
5468
bellardcd6f1162004-05-13 22:02:20 +00005469 optind = 1;
bellard0824d6f2003-06-24 13:42:40 +00005470 for(;;) {
bellardcd6f1162004-05-13 22:02:20 +00005471 if (optind >= argc)
bellard0824d6f2003-06-24 13:42:40 +00005472 break;
bellardcd6f1162004-05-13 22:02:20 +00005473 r = argv[optind];
5474 if (r[0] != '-') {
balrog609497a2008-01-14 02:56:53 +00005475 hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
bellardcd6f1162004-05-13 22:02:20 +00005476 } else {
5477 const QEMUOption *popt;
5478
5479 optind++;
pbrookdff5efc2007-01-27 17:19:39 +00005480 /* Treat --foo the same as -foo. */
5481 if (r[1] == '-')
5482 r++;
bellardcd6f1162004-05-13 22:02:20 +00005483 popt = qemu_options;
5484 for(;;) {
5485 if (!popt->name) {
ths5fafdf22007-09-16 21:08:06 +00005486 fprintf(stderr, "%s: invalid option -- '%s'\n",
bellardcd6f1162004-05-13 22:02:20 +00005487 argv[0], r);
5488 exit(1);
5489 }
5490 if (!strcmp(popt->name, r + 1))
5491 break;
5492 popt++;
5493 }
5494 if (popt->flags & HAS_ARG) {
5495 if (optind >= argc) {
5496 fprintf(stderr, "%s: option '%s' requires an argument\n",
5497 argv[0], r);
5498 exit(1);
5499 }
5500 optarg = argv[optind++];
5501 } else {
5502 optarg = NULL;
5503 }
5504
5505 switch(popt->index) {
bellardcc1daa42005-06-05 14:49:17 +00005506 case QEMU_OPTION_M:
5507 machine = find_machine(optarg);
5508 if (!machine) {
5509 QEMUMachine *m;
5510 printf("Supported machines are:\n");
5511 for(m = first_machine; m != NULL; m = m->next) {
5512 printf("%-10s %s%s\n",
ths5fafdf22007-09-16 21:08:06 +00005513 m->name, m->desc,
bellardcc1daa42005-06-05 14:49:17 +00005514 m == first_machine ? " (default)" : "");
5515 }
ths15f82202007-06-29 23:26:08 +00005516 exit(*optarg != '?');
bellardcc1daa42005-06-05 14:49:17 +00005517 }
5518 break;
j_mayer94fc95c2007-03-05 19:44:02 +00005519 case QEMU_OPTION_cpu:
5520 /* hw initialization will check this */
ths15f82202007-06-29 23:26:08 +00005521 if (*optarg == '?') {
j_mayerc732abe2007-10-12 06:47:46 +00005522/* XXX: implement xxx_cpu_list for targets that still miss it */
5523#if defined(cpu_list)
5524 cpu_list(stdout, &fprintf);
j_mayer94fc95c2007-03-05 19:44:02 +00005525#endif
ths15f82202007-06-29 23:26:08 +00005526 exit(0);
j_mayer94fc95c2007-03-05 19:44:02 +00005527 } else {
5528 cpu_model = optarg;
5529 }
5530 break;
bellardcd6f1162004-05-13 22:02:20 +00005531 case QEMU_OPTION_initrd:
bellardfc01f7e2003-06-30 10:03:06 +00005532 initrd_filename = optarg;
5533 break;
bellardcd6f1162004-05-13 22:02:20 +00005534 case QEMU_OPTION_hda:
thse4bcb142007-12-02 04:51:10 +00005535 if (cyls == 0)
balrog609497a2008-01-14 02:56:53 +00005536 hda_index = drive_add(optarg, HD_ALIAS, 0);
thse4bcb142007-12-02 04:51:10 +00005537 else
balrog609497a2008-01-14 02:56:53 +00005538 hda_index = drive_add(optarg, HD_ALIAS
thse4bcb142007-12-02 04:51:10 +00005539 ",cyls=%d,heads=%d,secs=%d%s",
balrog609497a2008-01-14 02:56:53 +00005540 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005541 translation == BIOS_ATA_TRANSLATION_LBA ?
5542 ",trans=lba" :
5543 translation == BIOS_ATA_TRANSLATION_NONE ?
5544 ",trans=none" : "");
5545 break;
bellardcd6f1162004-05-13 22:02:20 +00005546 case QEMU_OPTION_hdb:
bellardcc1daa42005-06-05 14:49:17 +00005547 case QEMU_OPTION_hdc:
5548 case QEMU_OPTION_hdd:
balrog609497a2008-01-14 02:56:53 +00005549 drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
bellardfc01f7e2003-06-30 10:03:06 +00005550 break;
thse4bcb142007-12-02 04:51:10 +00005551 case QEMU_OPTION_drive:
balrog609497a2008-01-14 02:56:53 +00005552 drive_add(NULL, "%s", optarg);
thse4bcb142007-12-02 04:51:10 +00005553 break;
balrog3e3d5812007-04-30 02:09:25 +00005554 case QEMU_OPTION_mtdblock:
balrog609497a2008-01-14 02:56:53 +00005555 drive_add(optarg, MTD_ALIAS);
balrog3e3d5812007-04-30 02:09:25 +00005556 break;
pbrooka1bb27b2007-04-06 16:49:48 +00005557 case QEMU_OPTION_sd:
balrog609497a2008-01-14 02:56:53 +00005558 drive_add(optarg, SD_ALIAS);
pbrooka1bb27b2007-04-06 16:49:48 +00005559 break;
j_mayer86f55662007-04-24 06:52:59 +00005560 case QEMU_OPTION_pflash:
balrog609497a2008-01-14 02:56:53 +00005561 drive_add(optarg, PFLASH_ALIAS);
j_mayer86f55662007-04-24 06:52:59 +00005562 break;
bellardcd6f1162004-05-13 22:02:20 +00005563 case QEMU_OPTION_snapshot:
bellard33e39632003-07-06 17:15:21 +00005564 snapshot = 1;
5565 break;
bellardcd6f1162004-05-13 22:02:20 +00005566 case QEMU_OPTION_hdachs:
bellard330d0412003-07-26 18:11:40 +00005567 {
bellard330d0412003-07-26 18:11:40 +00005568 const char *p;
5569 p = optarg;
5570 cyls = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005571 if (cyls < 1 || cyls > 16383)
5572 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005573 if (*p != ',')
5574 goto chs_fail;
5575 p++;
5576 heads = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005577 if (heads < 1 || heads > 16)
5578 goto chs_fail;
bellard330d0412003-07-26 18:11:40 +00005579 if (*p != ',')
5580 goto chs_fail;
5581 p++;
5582 secs = strtol(p, (char **)&p, 0);
bellard46d47672004-11-16 01:45:27 +00005583 if (secs < 1 || secs > 63)
5584 goto chs_fail;
5585 if (*p == ',') {
5586 p++;
5587 if (!strcmp(p, "none"))
5588 translation = BIOS_ATA_TRANSLATION_NONE;
5589 else if (!strcmp(p, "lba"))
5590 translation = BIOS_ATA_TRANSLATION_LBA;
5591 else if (!strcmp(p, "auto"))
5592 translation = BIOS_ATA_TRANSLATION_AUTO;
5593 else
5594 goto chs_fail;
5595 } else if (*p != '\0') {
bellardc4b1fcc2004-03-14 21:44:30 +00005596 chs_fail:
bellard46d47672004-11-16 01:45:27 +00005597 fprintf(stderr, "qemu: invalid physical CHS format\n");
5598 exit(1);
bellardc4b1fcc2004-03-14 21:44:30 +00005599 }
thse4bcb142007-12-02 04:51:10 +00005600 if (hda_index != -1)
balrog609497a2008-01-14 02:56:53 +00005601 snprintf(drives_opt[hda_index].opt,
5602 sizeof(drives_opt[hda_index].opt),
5603 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5604 0, cyls, heads, secs,
thse4bcb142007-12-02 04:51:10 +00005605 translation == BIOS_ATA_TRANSLATION_LBA ?
5606 ",trans=lba" :
5607 translation == BIOS_ATA_TRANSLATION_NONE ?
5608 ",trans=none" : "");
bellard330d0412003-07-26 18:11:40 +00005609 }
5610 break;
bellardcd6f1162004-05-13 22:02:20 +00005611 case QEMU_OPTION_nographic:
bellarda20dd502003-09-30 21:07:02 +00005612 nographic = 1;
5613 break;
balrog4d3b6f62008-02-10 16:33:14 +00005614#ifdef CONFIG_CURSES
5615 case QEMU_OPTION_curses:
5616 curses = 1;
5617 break;
5618#endif
balroga171fe32007-04-30 01:48:07 +00005619 case QEMU_OPTION_portrait:
5620 graphic_rotate = 1;
5621 break;
bellardcd6f1162004-05-13 22:02:20 +00005622 case QEMU_OPTION_kernel:
bellarda20dd502003-09-30 21:07:02 +00005623 kernel_filename = optarg;
5624 break;
bellardcd6f1162004-05-13 22:02:20 +00005625 case QEMU_OPTION_append:
bellarda20dd502003-09-30 21:07:02 +00005626 kernel_cmdline = optarg;
bellard313aa562003-08-10 21:52:11 +00005627 break;
bellardcd6f1162004-05-13 22:02:20 +00005628 case QEMU_OPTION_cdrom:
balrog609497a2008-01-14 02:56:53 +00005629 drive_add(optarg, CDROM_ALIAS);
bellard36b486b2003-11-11 13:36:08 +00005630 break;
bellardcd6f1162004-05-13 22:02:20 +00005631 case QEMU_OPTION_boot:
j_mayer28c5af52007-11-11 01:50:45 +00005632 boot_devices = optarg;
5633 /* We just do some generic consistency checks */
5634 {
5635 /* Could easily be extended to 64 devices if needed */
ths60fe76f2007-12-16 03:02:09 +00005636 const char *p;
j_mayer28c5af52007-11-11 01:50:45 +00005637
5638 boot_devices_bitmap = 0;
5639 for (p = boot_devices; *p != '\0'; p++) {
5640 /* Allowed boot devices are:
5641 * a b : floppy disk drives
5642 * c ... f : IDE disk drives
5643 * g ... m : machine implementation dependant drives
5644 * n ... p : network devices
5645 * It's up to each machine implementation to check
5646 * if the given boot devices match the actual hardware
5647 * implementation and firmware features.
5648 */
5649 if (*p < 'a' || *p > 'q') {
5650 fprintf(stderr, "Invalid boot device '%c'\n", *p);
5651 exit(1);
5652 }
5653 if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5654 fprintf(stderr,
5655 "Boot device '%c' was given twice\n",*p);
5656 exit(1);
5657 }
5658 boot_devices_bitmap |= 1 << (*p - 'a');
5659 }
bellard36b486b2003-11-11 13:36:08 +00005660 }
5661 break;
bellardcd6f1162004-05-13 22:02:20 +00005662 case QEMU_OPTION_fda:
bellardcd6f1162004-05-13 22:02:20 +00005663 case QEMU_OPTION_fdb:
balrog609497a2008-01-14 02:56:53 +00005664 drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
bellardc45886d2004-01-05 00:02:06 +00005665 break;
bellard52ca8d62006-06-14 16:03:05 +00005666#ifdef TARGET_I386
5667 case QEMU_OPTION_no_fd_bootchk:
5668 fd_bootchk = 0;
5669 break;
5670#endif
bellard7c9d8e02005-11-15 22:16:05 +00005671 case QEMU_OPTION_net:
5672 if (nb_net_clients >= MAX_NET_CLIENTS) {
5673 fprintf(stderr, "qemu: too many network clients\n");
bellardc4b1fcc2004-03-14 21:44:30 +00005674 exit(1);
5675 }
pbrookfd5f3932008-03-26 20:55:43 +00005676 net_clients[nb_net_clients] = optarg;
bellard7c9d8e02005-11-15 22:16:05 +00005677 nb_net_clients++;
bellard702c6512004-04-02 21:21:32 +00005678 break;
bellardc7f74642004-08-24 21:57:12 +00005679#ifdef CONFIG_SLIRP
5680 case QEMU_OPTION_tftp:
bellardc7f74642004-08-24 21:57:12 +00005681 tftp_prefix = optarg;
bellard9bf05442004-08-25 22:12:49 +00005682 break;
ths47d5d012007-02-20 00:05:08 +00005683 case QEMU_OPTION_bootp:
5684 bootp_filename = optarg;
5685 break;
bellardc94c8d62004-09-13 21:37:34 +00005686#ifndef _WIN32
bellard9d728e82004-09-05 23:09:03 +00005687 case QEMU_OPTION_smb:
5688 net_slirp_smb(optarg);
5689 break;
bellardc94c8d62004-09-13 21:37:34 +00005690#endif
bellard9bf05442004-08-25 22:12:49 +00005691 case QEMU_OPTION_redir:
ths3b46e622007-09-17 08:09:54 +00005692 net_slirp_redir(optarg);
bellard9bf05442004-08-25 22:12:49 +00005693 break;
bellardc7f74642004-08-24 21:57:12 +00005694#endif
bellard1d14ffa2005-10-30 18:58:22 +00005695#ifdef HAS_AUDIO
bellard1d14ffa2005-10-30 18:58:22 +00005696 case QEMU_OPTION_audio_help:
5697 AUD_help ();
5698 exit (0);
5699 break;
5700 case QEMU_OPTION_soundhw:
5701 select_soundhw (optarg);
5702 break;
5703#endif
bellardcd6f1162004-05-13 22:02:20 +00005704 case QEMU_OPTION_h:
ths15f82202007-06-29 23:26:08 +00005705 help(0);
bellardcd6f1162004-05-13 22:02:20 +00005706 break;
aurel3200f82b82008-04-27 21:12:55 +00005707 case QEMU_OPTION_m: {
5708 uint64_t value;
5709 char *ptr;
5710
5711 value = strtoul(optarg, &ptr, 10);
5712 switch (*ptr) {
5713 case 0: case 'M': case 'm':
5714 value <<= 20;
5715 break;
5716 case 'G': case 'g':
5717 value <<= 30;
5718 break;
5719 default:
5720 fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
bellardcd6f1162004-05-13 22:02:20 +00005721 exit(1);
5722 }
aurel3200f82b82008-04-27 21:12:55 +00005723
5724 /* On 32-bit hosts, QEMU is limited by virtual address space */
5725 if (value > (2047 << 20)
5726#ifndef USE_KQEMU
5727 && HOST_LONG_BITS == 32
5728#endif
5729 ) {
5730 fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5731 exit(1);
5732 }
5733 if (value != (uint64_t)(ram_addr_t)value) {
5734 fprintf(stderr, "qemu: ram size too large\n");
5735 exit(1);
5736 }
5737 ram_size = value;
bellardcd6f1162004-05-13 22:02:20 +00005738 break;
aurel3200f82b82008-04-27 21:12:55 +00005739 }
bellardcd6f1162004-05-13 22:02:20 +00005740 case QEMU_OPTION_d:
5741 {
5742 int mask;
blueswir1c7cd6a32008-10-02 18:27:46 +00005743 const CPULogItem *item;
ths3b46e622007-09-17 08:09:54 +00005744
bellardcd6f1162004-05-13 22:02:20 +00005745 mask = cpu_str_to_log_mask(optarg);
5746 if (!mask) {
5747 printf("Log items (comma separated):\n");
bellardf193c792004-03-21 17:06:25 +00005748 for(item = cpu_log_items; item->mask != 0; item++) {
5749 printf("%-10s %s\n", item->name, item->help);
5750 }
5751 exit(1);
bellardcd6f1162004-05-13 22:02:20 +00005752 }
5753 cpu_set_log(mask);
bellardf193c792004-03-21 17:06:25 +00005754 }
bellardcd6f1162004-05-13 22:02:20 +00005755 break;
bellard67b915a2004-03-31 23:37:16 +00005756#ifdef CONFIG_GDBSTUB
bellardcd6f1162004-05-13 22:02:20 +00005757 case QEMU_OPTION_s:
5758 use_gdbstub = 1;
5759 break;
5760 case QEMU_OPTION_p:
pbrookcfc34752007-02-22 01:48:01 +00005761 gdbstub_port = optarg;
bellardcd6f1162004-05-13 22:02:20 +00005762 break;
bellard67b915a2004-03-31 23:37:16 +00005763#endif
bellardcd6f1162004-05-13 22:02:20 +00005764 case QEMU_OPTION_L:
5765 bios_dir = optarg;
5766 break;
j_mayer1192dad2007-10-05 13:08:35 +00005767 case QEMU_OPTION_bios:
5768 bios_name = optarg;
5769 break;
bellardcd6f1162004-05-13 22:02:20 +00005770 case QEMU_OPTION_S:
pbrook3c07f8e2007-01-21 16:47:01 +00005771 autostart = 0;
bellardcd6f1162004-05-13 22:02:20 +00005772 break;
bellard3d11d0e2004-12-12 16:56:30 +00005773 case QEMU_OPTION_k:
5774 keyboard_layout = optarg;
5775 break;
bellardee22c2f2004-06-03 12:49:50 +00005776 case QEMU_OPTION_localtime:
5777 rtc_utc = 0;
5778 break;
malc3893c122008-09-28 00:42:05 +00005779 case QEMU_OPTION_vga:
5780 select_vgahw (optarg);
bellard1bfe8562004-07-08 21:17:50 +00005781 break;
bellarde9b137c2004-06-21 16:46:10 +00005782 case QEMU_OPTION_g:
5783 {
5784 const char *p;
5785 int w, h, depth;
5786 p = optarg;
5787 w = strtol(p, (char **)&p, 10);
5788 if (w <= 0) {
5789 graphic_error:
5790 fprintf(stderr, "qemu: invalid resolution or depth\n");
5791 exit(1);
5792 }
5793 if (*p != 'x')
5794 goto graphic_error;
5795 p++;
5796 h = strtol(p, (char **)&p, 10);
5797 if (h <= 0)
5798 goto graphic_error;
5799 if (*p == 'x') {
5800 p++;
5801 depth = strtol(p, (char **)&p, 10);
ths5fafdf22007-09-16 21:08:06 +00005802 if (depth != 8 && depth != 15 && depth != 16 &&
bellarde9b137c2004-06-21 16:46:10 +00005803 depth != 24 && depth != 32)
5804 goto graphic_error;
5805 } else if (*p == '\0') {
5806 depth = graphic_depth;
5807 } else {
5808 goto graphic_error;
5809 }
ths3b46e622007-09-17 08:09:54 +00005810
bellarde9b137c2004-06-21 16:46:10 +00005811 graphic_width = w;
5812 graphic_height = h;
5813 graphic_depth = depth;
5814 }
5815 break;
ths20d8a3e2007-02-18 17:04:49 +00005816 case QEMU_OPTION_echr:
5817 {
5818 char *r;
5819 term_escape_char = strtol(optarg, &r, 0);
5820 if (r == optarg)
5821 printf("Bad argument to echr\n");
5822 break;
5823 }
bellard82c643f2004-07-14 17:28:13 +00005824 case QEMU_OPTION_monitor:
pbrookfd5f3932008-03-26 20:55:43 +00005825 monitor_device = optarg;
bellard82c643f2004-07-14 17:28:13 +00005826 break;
5827 case QEMU_OPTION_serial:
bellard8d11df92004-08-24 21:13:40 +00005828 if (serial_device_index >= MAX_SERIAL_PORTS) {
5829 fprintf(stderr, "qemu: too many serial ports\n");
5830 exit(1);
5831 }
pbrookfd5f3932008-03-26 20:55:43 +00005832 serial_devices[serial_device_index] = optarg;
bellard8d11df92004-08-24 21:13:40 +00005833 serial_device_index++;
bellard82c643f2004-07-14 17:28:13 +00005834 break;
bellard6508fe52005-01-15 12:02:56 +00005835 case QEMU_OPTION_parallel:
5836 if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5837 fprintf(stderr, "qemu: too many parallel ports\n");
5838 exit(1);
5839 }
pbrookfd5f3932008-03-26 20:55:43 +00005840 parallel_devices[parallel_device_index] = optarg;
bellard6508fe52005-01-15 12:02:56 +00005841 parallel_device_index++;
5842 break;
bellardd63d3072004-10-03 13:29:03 +00005843 case QEMU_OPTION_loadvm:
5844 loadvm = optarg;
5845 break;
5846 case QEMU_OPTION_full_screen:
5847 full_screen = 1;
5848 break;
ths667acca2006-12-11 02:08:05 +00005849#ifdef CONFIG_SDL
ths43523e92007-02-18 18:19:32 +00005850 case QEMU_OPTION_no_frame:
5851 no_frame = 1;
5852 break;
ths3780e192007-06-21 21:08:02 +00005853 case QEMU_OPTION_alt_grab:
5854 alt_grab = 1;
5855 break;
ths667acca2006-12-11 02:08:05 +00005856 case QEMU_OPTION_no_quit:
5857 no_quit = 1;
5858 break;
5859#endif
bellardf7cce892004-12-08 22:21:25 +00005860 case QEMU_OPTION_pidfile:
ths93815bc2007-03-19 15:58:31 +00005861 pid_file = optarg;
bellardf7cce892004-12-08 22:21:25 +00005862 break;
bellarda09db212005-04-30 16:10:35 +00005863#ifdef TARGET_I386
5864 case QEMU_OPTION_win2k_hack:
5865 win2k_install_hack = 1;
5866 break;
5867#endif
bellardd993e022005-02-10 22:00:06 +00005868#ifdef USE_KQEMU
5869 case QEMU_OPTION_no_kqemu:
5870 kqemu_allowed = 0;
5871 break;
bellard89bfc102006-02-08 22:46:31 +00005872 case QEMU_OPTION_kernel_kqemu:
5873 kqemu_allowed = 2;
5874 break;
bellardd993e022005-02-10 22:00:06 +00005875#endif
aliguori7ba1e612008-11-05 16:04:33 +00005876#ifdef CONFIG_KVM
5877 case QEMU_OPTION_enable_kvm:
5878 kvm_allowed = 1;
5879#ifdef USE_KQEMU
5880 kqemu_allowed = 0;
5881#endif
5882 break;
5883#endif
bellardbb36d472005-11-05 14:22:28 +00005884 case QEMU_OPTION_usb:
5885 usb_enabled = 1;
5886 break;
bellarda594cfb2005-11-06 16:13:29 +00005887 case QEMU_OPTION_usbdevice:
5888 usb_enabled = 1;
pbrook0d92ed32006-05-21 16:30:15 +00005889 if (usb_devices_index >= MAX_USB_CMDLINE) {
bellarda594cfb2005-11-06 16:13:29 +00005890 fprintf(stderr, "Too many USB devices\n");
5891 exit(1);
5892 }
pbrookfd5f3932008-03-26 20:55:43 +00005893 usb_devices[usb_devices_index] = optarg;
bellarda594cfb2005-11-06 16:13:29 +00005894 usb_devices_index++;
5895 break;
bellard6a00d602005-11-21 23:25:50 +00005896 case QEMU_OPTION_smp:
5897 smp_cpus = atoi(optarg);
aliguorib2097002008-10-07 20:39:39 +00005898 if (smp_cpus < 1) {
bellard6a00d602005-11-21 23:25:50 +00005899 fprintf(stderr, "Invalid number of CPUs\n");
5900 exit(1);
5901 }
5902 break;
bellard24236862006-04-30 21:28:36 +00005903 case QEMU_OPTION_vnc:
ths73fc9742006-12-22 02:09:07 +00005904 vnc_display = optarg;
bellard24236862006-04-30 21:28:36 +00005905 break;
bellard6515b202006-05-03 22:02:44 +00005906 case QEMU_OPTION_no_acpi:
5907 acpi_enabled = 0;
5908 break;
bellardd1beab82006-10-02 19:44:22 +00005909 case QEMU_OPTION_no_reboot:
5910 no_reboot = 1;
5911 break;
aurel32b2f76162008-04-11 21:35:52 +00005912 case QEMU_OPTION_no_shutdown:
5913 no_shutdown = 1;
5914 break;
balrog9467cd42007-05-01 01:34:14 +00005915 case QEMU_OPTION_show_cursor:
5916 cursor_hide = 0;
5917 break;
blueswir18fcb1b92008-09-18 18:29:08 +00005918 case QEMU_OPTION_uuid:
5919 if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5920 fprintf(stderr, "Fail to parse UUID string."
5921 " Wrong format.\n");
5922 exit(1);
5923 }
5924 break;
ths71e3ceb2006-12-22 02:11:31 +00005925 case QEMU_OPTION_daemonize:
5926 daemonize = 1;
5927 break;
ths9ae02552007-01-05 17:39:04 +00005928 case QEMU_OPTION_option_rom:
5929 if (nb_option_roms >= MAX_OPTION_ROMS) {
5930 fprintf(stderr, "Too many option ROMs\n");
5931 exit(1);
5932 }
5933 option_rom[nb_option_roms] = optarg;
5934 nb_option_roms++;
5935 break;
pbrook8e716212007-01-20 17:12:09 +00005936 case QEMU_OPTION_semihosting:
5937 semihosting_enabled = 1;
5938 break;
thsc35734b2007-03-19 15:17:08 +00005939 case QEMU_OPTION_name:
5940 qemu_name = optarg;
5941 break;
blueswir166508602007-05-01 14:16:52 +00005942#ifdef TARGET_SPARC
5943 case QEMU_OPTION_prom_env:
5944 if (nb_prom_envs >= MAX_PROM_ENVS) {
5945 fprintf(stderr, "Too many prom variables\n");
5946 exit(1);
5947 }
5948 prom_envs[nb_prom_envs] = optarg;
5949 nb_prom_envs++;
5950 break;
5951#endif
balrog2b8f2d42007-07-27 22:08:46 +00005952#ifdef TARGET_ARM
5953 case QEMU_OPTION_old_param:
5954 old_param = 1;
ths05ebd532008-01-08 19:32:16 +00005955 break;
balrog2b8f2d42007-07-27 22:08:46 +00005956#endif
thsf3dcfad2007-08-24 01:26:02 +00005957 case QEMU_OPTION_clock:
5958 configure_alarms(optarg);
5959 break;
bellard7e0af5d02007-11-07 16:24:33 +00005960 case QEMU_OPTION_startdate:
5961 {
5962 struct tm tm;
balrogf6503052008-02-17 11:42:19 +00005963 time_t rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005964 if (!strcmp(optarg, "now")) {
balrogf6503052008-02-17 11:42:19 +00005965 rtc_date_offset = -1;
bellard7e0af5d02007-11-07 16:24:33 +00005966 } else {
5967 if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5968 &tm.tm_year,
5969 &tm.tm_mon,
5970 &tm.tm_mday,
5971 &tm.tm_hour,
5972 &tm.tm_min,
5973 &tm.tm_sec) == 6) {
5974 /* OK */
5975 } else if (sscanf(optarg, "%d-%d-%d",
5976 &tm.tm_year,
5977 &tm.tm_mon,
5978 &tm.tm_mday) == 3) {
5979 tm.tm_hour = 0;
5980 tm.tm_min = 0;
5981 tm.tm_sec = 0;
5982 } else {
5983 goto date_fail;
5984 }
5985 tm.tm_year -= 1900;
5986 tm.tm_mon--;
bellard3c6b2082007-11-10 19:36:39 +00005987 rtc_start_date = mktimegm(&tm);
bellard7e0af5d02007-11-07 16:24:33 +00005988 if (rtc_start_date == -1) {
5989 date_fail:
5990 fprintf(stderr, "Invalid date format. Valid format are:\n"
5991 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5992 exit(1);
5993 }
balrogf6503052008-02-17 11:42:19 +00005994 rtc_date_offset = time(NULL) - rtc_start_date;
bellard7e0af5d02007-11-07 16:24:33 +00005995 }
5996 }
5997 break;
bellard26a5f132008-05-28 12:30:31 +00005998 case QEMU_OPTION_tb_size:
5999 tb_size = strtol(optarg, NULL, 0);
6000 if (tb_size < 0)
6001 tb_size = 0;
6002 break;
pbrook2e70f6e2008-06-29 01:03:05 +00006003 case QEMU_OPTION_icount:
6004 use_icount = 1;
6005 if (strcmp(optarg, "auto") == 0) {
6006 icount_time_shift = -1;
6007 } else {
6008 icount_time_shift = strtol(optarg, NULL, 0);
6009 }
6010 break;
aliguori5bb79102008-10-13 03:12:02 +00006011 case QEMU_OPTION_incoming:
6012 incoming = optarg;
6013 break;
bellardcd6f1162004-05-13 22:02:20 +00006014 }
bellard0824d6f2003-06-24 13:42:40 +00006015 }
6016 }
bellard330d0412003-07-26 18:11:40 +00006017
aliguori7ba1e612008-11-05 16:04:33 +00006018#if defined(CONFIG_KVM) && defined(USE_KQEMU)
6019 if (kvm_allowed && kqemu_allowed) {
6020 fprintf(stderr,
6021 "You can not enable both KVM and kqemu at the same time\n");
6022 exit(1);
6023 }
6024#endif
6025
balrog3d878ca2008-10-28 10:59:59 +00006026 machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
aliguorib2097002008-10-07 20:39:39 +00006027 if (smp_cpus > machine->max_cpus) {
6028 fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
6029 "supported by machine `%s' (%d)\n", smp_cpus, machine->name,
6030 machine->max_cpus);
6031 exit(1);
6032 }
6033
aliguoribc0129d2008-08-01 15:12:34 +00006034 if (nographic) {
6035 if (serial_device_index == 0)
6036 serial_devices[0] = "stdio";
6037 if (parallel_device_index == 0)
6038 parallel_devices[0] = "null";
6039 if (strncmp(monitor_device, "vc", 2) == 0)
6040 monitor_device = "stdio";
6041 }
6042
ths71e3ceb2006-12-22 02:11:31 +00006043#ifndef _WIN32
ths71e3ceb2006-12-22 02:11:31 +00006044 if (daemonize) {
6045 pid_t pid;
6046
6047 if (pipe(fds) == -1)
6048 exit(1);
6049
6050 pid = fork();
6051 if (pid > 0) {
6052 uint8_t status;
6053 ssize_t len;
6054
6055 close(fds[1]);
6056
6057 again:
ths93815bc2007-03-19 15:58:31 +00006058 len = read(fds[0], &status, 1);
6059 if (len == -1 && (errno == EINTR))
6060 goto again;
6061
6062 if (len != 1)
6063 exit(1);
6064 else if (status == 1) {
6065 fprintf(stderr, "Could not acquire pidfile\n");
6066 exit(1);
6067 } else
6068 exit(0);
ths71e3ceb2006-12-22 02:11:31 +00006069 } else if (pid < 0)
ths93815bc2007-03-19 15:58:31 +00006070 exit(1);
ths71e3ceb2006-12-22 02:11:31 +00006071
6072 setsid();
6073
6074 pid = fork();
6075 if (pid > 0)
6076 exit(0);
6077 else if (pid < 0)
6078 exit(1);
6079
6080 umask(027);
ths71e3ceb2006-12-22 02:11:31 +00006081
6082 signal(SIGTSTP, SIG_IGN);
6083 signal(SIGTTOU, SIG_IGN);
6084 signal(SIGTTIN, SIG_IGN);
6085 }
6086#endif
6087
thsaa26bb22007-03-25 21:33:06 +00006088 if (pid_file && qemu_create_pidfile(pid_file) != 0) {
ths93815bc2007-03-19 15:58:31 +00006089 if (daemonize) {
6090 uint8_t status = 1;
6091 write(fds[1], &status, 1);
6092 } else
6093 fprintf(stderr, "Could not acquire pid file\n");
6094 exit(1);
6095 }
6096
bellardff3fbb32006-01-08 10:53:14 +00006097#ifdef USE_KQEMU
6098 if (smp_cpus > 1)
6099 kqemu_allowed = 0;
6100#endif
bellarda20dd502003-09-30 21:07:02 +00006101 linux_boot = (kernel_filename != NULL);
balrog7317b8c2007-11-18 02:09:36 +00006102 net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
balrog6c41b272007-11-17 12:12:29 +00006103
j_mayer28c5af52007-11-11 01:50:45 +00006104 if (!linux_boot && net_boot == 0 &&
blueswir1f88e4b92008-08-12 15:58:35 +00006105 !machine->nodisk_ok && nb_drives_opt == 0)
ths15f82202007-06-29 23:26:08 +00006106 help(1);
bellard0824d6f2003-06-24 13:42:40 +00006107
thsf8d39c02008-07-03 10:01:15 +00006108 if (!linux_boot && *kernel_cmdline != '\0') {
6109 fprintf(stderr, "-append only allowed with -kernel option\n");
6110 exit(1);
6111 }
6112
6113 if (!linux_boot && initrd_filename != NULL) {
6114 fprintf(stderr, "-initrd only allowed with -kernel option\n");
6115 exit(1);
6116 }
6117
ths96d30e42007-01-07 20:42:14 +00006118 /* boot to floppy or the default cd if no hard disk defined yet */
j_mayer28c5af52007-11-11 01:50:45 +00006119 if (!boot_devices[0]) {
thse4bcb142007-12-02 04:51:10 +00006120 boot_devices = "cad";
ths96d30e42007-01-07 20:42:14 +00006121 }
bellardb118d612003-06-30 23:36:21 +00006122 setvbuf(stdout, NULL, _IOLBF, 0);
ths3b46e622007-09-17 08:09:54 +00006123
pbrook634fce92006-07-15 17:40:09 +00006124 init_timers();
aliguori7183b4b2008-11-05 20:40:18 +00006125 if (init_timer_alarm() < 0) {
6126 fprintf(stderr, "could not initialize alarm timer\n");
6127 exit(1);
6128 }
pbrook2e70f6e2008-06-29 01:03:05 +00006129 if (use_icount && icount_time_shift < 0) {
6130 use_icount = 2;
6131 /* 125MIPS seems a reasonable initial guess at the guest speed.
6132 It will be corrected fairly quickly anyway. */
6133 icount_time_shift = 3;
6134 init_icount_adjust();
6135 }
pbrook634fce92006-07-15 17:40:09 +00006136
bellardfd1dff42006-02-01 21:29:26 +00006137#ifdef _WIN32
6138 socket_init();
6139#endif
6140
bellard7c9d8e02005-11-15 22:16:05 +00006141 /* init network clients */
6142 if (nb_net_clients == 0) {
6143 /* if no clients, we use a default config */
aliguorif441b282008-08-28 20:05:14 +00006144 net_clients[nb_net_clients++] = "nic";
6145#ifdef CONFIG_SLIRP
6146 net_clients[nb_net_clients++] = "user";
6147#endif
bellardc20709a2004-04-21 23:27:19 +00006148 }
6149
bellard7c9d8e02005-11-15 22:16:05 +00006150 for(i = 0;i < nb_net_clients; i++) {
balrog9ad97e62008-07-29 13:16:31 +00006151 if (net_client_parse(net_clients[i]) < 0)
bellard7c9d8e02005-11-15 22:16:05 +00006152 exit(1);
bellard702c6512004-04-02 21:21:32 +00006153 }
aliguori63a01ef2008-10-31 19:10:00 +00006154 net_client_check();
bellardf1510b22003-06-25 00:07:40 +00006155
thseec85c22007-01-05 17:41:07 +00006156#ifdef TARGET_I386
balroged494d82007-12-11 23:23:52 +00006157 /* XXX: this should be moved in the PC machine instantiation code */
j_mayer28c5af52007-11-11 01:50:45 +00006158 if (net_boot != 0) {
6159 int netroms = 0;
6160 for (i = 0; i < nb_nics && i < 4; i++) {
thseec85c22007-01-05 17:41:07 +00006161 const char *model = nd_table[i].model;
6162 char buf[1024];
j_mayer28c5af52007-11-11 01:50:45 +00006163 if (net_boot & (1 << i)) {
6164 if (model == NULL)
6165 model = "ne2k_pci";
6166 snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
6167 if (get_image_size(buf) > 0) {
6168 if (nb_option_roms >= MAX_OPTION_ROMS) {
6169 fprintf(stderr, "Too many option ROMs\n");
6170 exit(1);
6171 }
6172 option_rom[nb_option_roms] = strdup(buf);
6173 nb_option_roms++;
6174 netroms++;
6175 }
6176 }
thseec85c22007-01-05 17:41:07 +00006177 }
j_mayer28c5af52007-11-11 01:50:45 +00006178 if (netroms == 0) {
thseec85c22007-01-05 17:41:07 +00006179 fprintf(stderr, "No valid PXE rom found for network device\n");
6180 exit(1);
6181 }
thseec85c22007-01-05 17:41:07 +00006182 }
6183#endif
6184
bellard0824d6f2003-06-24 13:42:40 +00006185 /* init the memory */
balrog7fb4fdc2008-04-24 17:59:27 +00006186 phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
6187
6188 if (machine->ram_require & RAMSIZE_FIXED) {
6189 if (ram_size > 0) {
6190 if (ram_size < phys_ram_size) {
aurel32cd940062008-04-28 20:26:54 +00006191 fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
6192 machine->name, (unsigned long long) phys_ram_size);
balrog7fb4fdc2008-04-24 17:59:27 +00006193 exit(-1);
6194 }
6195
6196 phys_ram_size = ram_size;
6197 } else
6198 ram_size = phys_ram_size;
6199 } else {
aurel324fc5d072008-04-27 21:39:40 +00006200 if (ram_size == 0)
balrog7fb4fdc2008-04-24 17:59:27 +00006201 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
6202
6203 phys_ram_size += ram_size;
6204 }
ths9ae02552007-01-05 17:39:04 +00006205
bellardd993e022005-02-10 22:00:06 +00006206 phys_ram_base = qemu_vmalloc(phys_ram_size);
bellard7f7f9872003-10-30 01:11:23 +00006207 if (!phys_ram_base) {
6208 fprintf(stderr, "Could not allocate physical memory\n");
bellard0824d6f2003-06-24 13:42:40 +00006209 exit(1);
6210 }
6211
bellard26a5f132008-05-28 12:30:31 +00006212 /* init the dynamic translator */
6213 cpu_exec_init_all(tb_size * 1024 * 1024);
6214
bellard5905b2e2004-08-01 21:53:26 +00006215 bdrv_init();
thse4bcb142007-12-02 04:51:10 +00006216
6217 /* we always create the cdrom drive, even if no disk is there */
6218
6219 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00006220 drive_add(NULL, CDROM_ALIAS);
thse4bcb142007-12-02 04:51:10 +00006221
balrog9d413d12007-12-04 00:10:34 +00006222 /* we always create at least one floppy */
thse4bcb142007-12-02 04:51:10 +00006223
6224 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00006225 drive_add(NULL, FD_ALIAS, 0);
bellardc4b1fcc2004-03-14 21:44:30 +00006226
balrog9d413d12007-12-04 00:10:34 +00006227 /* we always create one sd slot, even if no card is in it */
6228
6229 if (nb_drives_opt < MAX_DRIVES)
balrog609497a2008-01-14 02:56:53 +00006230 drive_add(NULL, SD_ALIAS);
balrog9d413d12007-12-04 00:10:34 +00006231
ths96d30e42007-01-07 20:42:14 +00006232 /* open the virtual block devices */
bellardc4b1fcc2004-03-14 21:44:30 +00006233
thse4bcb142007-12-02 04:51:10 +00006234 for(i = 0; i < nb_drives_opt; i++)
balrog609497a2008-01-14 02:56:53 +00006235 if (drive_init(&drives_opt[i], snapshot, machine) == -1)
thse4bcb142007-12-02 04:51:10 +00006236 exit(1);
balrog3e3d5812007-04-30 02:09:25 +00006237
bellardc88676f2006-08-06 13:36:11 +00006238 register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
aliguori475e4272008-10-06 20:21:51 +00006239 register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
bellard8a7ddc32004-03-31 19:00:16 +00006240
bellard313aa562003-08-10 21:52:11 +00006241 /* terminal init */
ths740733b2007-06-08 01:57:56 +00006242 memset(&display_state, 0, sizeof(display_state));
bellarda20dd502003-09-30 21:07:02 +00006243 if (nographic) {
balrog4d3b6f62008-02-10 16:33:14 +00006244 if (curses) {
6245 fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
6246 exit(1);
6247 }
ths2ff89792007-06-21 23:34:19 +00006248 /* nearly nothing to do */
6249 dumb_display_init(ds);
ths73fc9742006-12-22 02:09:07 +00006250 } else if (vnc_display != NULL) {
ths71cab5c2007-08-25 01:35:38 +00006251 vnc_display_init(ds);
6252 if (vnc_display_open(ds, vnc_display) < 0)
6253 exit(1);
balrog4d3b6f62008-02-10 16:33:14 +00006254 } else
6255#if defined(CONFIG_CURSES)
6256 if (curses) {
6257 curses_display_init(ds, full_screen);
6258 } else
6259#endif
6260 {
bellard5b0753e2005-03-01 21:37:28 +00006261#if defined(CONFIG_SDL)
ths43523e92007-02-18 18:19:32 +00006262 sdl_display_init(ds, full_screen, no_frame);
bellard5b0753e2005-03-01 21:37:28 +00006263#elif defined(CONFIG_COCOA)
6264 cocoa_display_init(ds, full_screen);
pbrook67276f52007-11-15 19:04:08 +00006265#else
6266 dumb_display_init(ds);
bellard313aa562003-08-10 21:52:11 +00006267#endif
6268 }
bellard0824d6f2003-06-24 13:42:40 +00006269
aliguori5b08fc12008-08-21 20:08:03 +00006270#ifndef _WIN32
6271 /* must be after terminal init, SDL library changes signal handlers */
6272 termsig_setup();
6273#endif
6274
ths20d8a3e2007-02-18 17:04:49 +00006275 /* Maintain compatibility with multiple stdio monitors */
6276 if (!strcmp(monitor_device,"stdio")) {
6277 for (i = 0; i < MAX_SERIAL_PORTS; i++) {
pbrookfd5f3932008-03-26 20:55:43 +00006278 const char *devname = serial_devices[i];
6279 if (devname && !strcmp(devname,"mon:stdio")) {
6280 monitor_device = NULL;
ths20d8a3e2007-02-18 17:04:49 +00006281 break;
pbrookfd5f3932008-03-26 20:55:43 +00006282 } else if (devname && !strcmp(devname,"stdio")) {
6283 monitor_device = NULL;
6284 serial_devices[i] = "mon:stdio";
ths20d8a3e2007-02-18 17:04:49 +00006285 break;
6286 }
6287 }
bellard82c643f2004-07-14 17:28:13 +00006288 }
pbrookfd5f3932008-03-26 20:55:43 +00006289 if (monitor_device) {
aliguori5ccfae12008-10-31 17:31:29 +00006290 monitor_hd = qemu_chr_open("monitor", monitor_device);
ths20d8a3e2007-02-18 17:04:49 +00006291 if (!monitor_hd) {
6292 fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
6293 exit(1);
6294 }
6295 monitor_init(monitor_hd, !nographic);
6296 }
bellard82c643f2004-07-14 17:28:13 +00006297
bellard8d11df92004-08-24 21:13:40 +00006298 for(i = 0; i < MAX_SERIAL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006299 const char *devname = serial_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006300 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00006301 char label[32];
6302 snprintf(label, sizeof(label), "serial%d", i);
6303 serial_hds[i] = qemu_chr_open(label, devname);
bellard8d11df92004-08-24 21:13:40 +00006304 if (!serial_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00006305 fprintf(stderr, "qemu: could not open serial device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00006306 devname);
bellard8d11df92004-08-24 21:13:40 +00006307 exit(1);
6308 }
thsaf3a9032007-07-11 23:14:59 +00006309 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006310 qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
bellard8d11df92004-08-24 21:13:40 +00006311 }
bellard82c643f2004-07-14 17:28:13 +00006312 }
bellard82c643f2004-07-14 17:28:13 +00006313
bellard6508fe52005-01-15 12:02:56 +00006314 for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
bellardc03b0f02006-09-03 14:10:53 +00006315 const char *devname = parallel_devices[i];
pbrookfd5f3932008-03-26 20:55:43 +00006316 if (devname && strcmp(devname, "none")) {
aliguori5ccfae12008-10-31 17:31:29 +00006317 char label[32];
6318 snprintf(label, sizeof(label), "parallel%d", i);
6319 parallel_hds[i] = qemu_chr_open(label, devname);
bellard6508fe52005-01-15 12:02:56 +00006320 if (!parallel_hds[i]) {
ths5fafdf22007-09-16 21:08:06 +00006321 fprintf(stderr, "qemu: could not open parallel device '%s'\n",
bellardc03b0f02006-09-03 14:10:53 +00006322 devname);
bellard6508fe52005-01-15 12:02:56 +00006323 exit(1);
6324 }
thsaf3a9032007-07-11 23:14:59 +00006325 if (strstart(devname, "vc", 0))
bellard7ba12602006-07-14 20:26:42 +00006326 qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
bellard6508fe52005-01-15 12:02:56 +00006327 }
6328 }
6329
aliguori7ba1e612008-11-05 16:04:33 +00006330 if (kvm_enabled()) {
6331 int ret;
6332
6333 ret = kvm_init(smp_cpus);
6334 if (ret < 0) {
6335 fprintf(stderr, "failed to initialize KVM\n");
6336 exit(1);
6337 }
6338 }
6339
blueswir1b881c2c2007-11-18 08:46:58 +00006340 machine->init(ram_size, vga_ram_size, boot_devices, ds,
j_mayer94fc95c2007-03-05 19:44:02 +00006341 kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
bellard73332e52004-04-04 20:22:28 +00006342
pbrook0d92ed32006-05-21 16:30:15 +00006343 /* init USB devices */
6344 if (usb_enabled) {
6345 for(i = 0; i < usb_devices_index; i++) {
6346 if (usb_device_add(usb_devices[i]) < 0) {
6347 fprintf(stderr, "Warning: could not add USB device %s\n",
6348 usb_devices[i]);
6349 }
6350 }
6351 }
6352
ths740733b2007-06-08 01:57:56 +00006353 if (display_state.dpy_refresh) {
6354 display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
6355 qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
6356 }
bellard7f7f9872003-10-30 01:11:23 +00006357
bellard67b915a2004-03-31 23:37:16 +00006358#ifdef CONFIG_GDBSTUB
bellardb4608c02003-06-27 17:34:32 +00006359 if (use_gdbstub) {
bellardc636bb62007-02-05 20:46:05 +00006360 /* XXX: use standard host:port notation and modify options
6361 accordingly. */
pbrookcfc34752007-02-22 01:48:01 +00006362 if (gdbserver_start(gdbstub_port) < 0) {
6363 fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
bellardc636bb62007-02-05 20:46:05 +00006364 gdbstub_port);
bellard8a7ddc32004-03-31 19:00:16 +00006365 exit(1);
bellard8a7ddc32004-03-31 19:00:16 +00006366 }
balrog45669e02007-07-02 13:20:17 +00006367 }
bellard67b915a2004-03-31 23:37:16 +00006368#endif
balrog45669e02007-07-02 13:20:17 +00006369
bellardd63d3072004-10-03 13:29:03 +00006370 if (loadvm)
bellardfaea38e2006-08-05 21:31:00 +00006371 do_loadvm(loadvm);
bellardd63d3072004-10-03 13:29:03 +00006372
aliguori5bb79102008-10-13 03:12:02 +00006373 if (incoming) {
6374 autostart = 0; /* fixme how to deal with -daemonize */
6375 qemu_start_incoming_migration(incoming);
6376 }
6377
bellard67b915a2004-03-31 23:37:16 +00006378 {
bellard5905b2e2004-08-01 21:53:26 +00006379 /* XXX: simplify init */
aliguori83ab7952008-08-19 14:44:22 +00006380 read_passwords();
pbrook3c07f8e2007-01-21 16:47:01 +00006381 if (autostart) {
bellard5905b2e2004-08-01 21:53:26 +00006382 vm_start();
6383 }
bellard0824d6f2003-06-24 13:42:40 +00006384 }
thsffd843b2006-12-21 19:46:43 +00006385
ths71e3ceb2006-12-22 02:11:31 +00006386 if (daemonize) {
6387 uint8_t status = 0;
6388 ssize_t len;
6389 int fd;
6390
6391 again1:
6392 len = write(fds[1], &status, 1);
6393 if (len == -1 && (errno == EINTR))
6394 goto again1;
6395
6396 if (len != 1)
6397 exit(1);
6398
aliguoribd54b862008-07-23 00:58:33 +00006399 chdir("/");
balrogaeb30be2007-07-02 15:03:13 +00006400 TFR(fd = open("/dev/null", O_RDWR));
ths71e3ceb2006-12-22 02:11:31 +00006401 if (fd == -1)
6402 exit(1);
6403
6404 dup2(fd, 0);
6405 dup2(fd, 1);
6406 dup2(fd, 2);
6407
6408 close(fd);
6409 }
6410
bellard8a7ddc32004-03-31 19:00:16 +00006411 main_loop();
bellard40c3bac2004-04-04 12:56:28 +00006412 quit_timers();
aliguori63a01ef2008-10-31 19:10:00 +00006413 net_cleanup();
thsb46a8902007-10-21 23:20:45 +00006414
bellard0824d6f2003-06-24 13:42:40 +00006415 return 0;
6416}