Statistics
| Branch: | Revision:

root / vl.c @ 3587f82a

History | View | Annotate | Download (172.3 kB)

1 0824d6fc bellard
/*
2 80cabfad bellard
 * QEMU System Emulator
3 5fafdf24 ths
 *
4 68d0f70e bellard
 * Copyright (c) 2003-2008 Fabrice Bellard
5 5fafdf24 ths
 *
6 1df912cf bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1df912cf bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1df912cf bellard
 * in the Software without restriction, including without limitation the rights
9 1df912cf bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1df912cf bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1df912cf bellard
 * furnished to do so, subject to the following conditions:
12 1df912cf bellard
 *
13 1df912cf bellard
 * The above copyright notice and this permission notice shall be included in
14 1df912cf bellard
 * all copies or substantial portions of the Software.
15 1df912cf bellard
 *
16 1df912cf bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1df912cf bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1df912cf bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1df912cf bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1df912cf bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1df912cf bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1df912cf bellard
 * THE SOFTWARE.
23 0824d6fc bellard
 */
24 87ecb68b pbrook
#include "hw/hw.h"
25 87ecb68b pbrook
#include "hw/boards.h"
26 87ecb68b pbrook
#include "hw/usb.h"
27 87ecb68b pbrook
#include "hw/pcmcia.h"
28 87ecb68b pbrook
#include "hw/pc.h"
29 87ecb68b pbrook
#include "hw/audiodev.h"
30 87ecb68b pbrook
#include "hw/isa.h"
31 2e4d9fb1 aurel32
#include "hw/baum.h"
32 1ae26a18 balrog
#include "hw/bt.h"
33 87ecb68b pbrook
#include "net.h"
34 87ecb68b pbrook
#include "console.h"
35 87ecb68b pbrook
#include "sysemu.h"
36 87ecb68b pbrook
#include "gdbstub.h"
37 87ecb68b pbrook
#include "qemu-timer.h"
38 87ecb68b pbrook
#include "qemu-char.h"
39 87ecb68b pbrook
#include "block.h"
40 87ecb68b pbrook
#include "audio/audio.h"
41 5bb7910a aliguori
#include "migration.h"
42 7ba1e619 aliguori
#include "kvm.h"
43 67b915a5 bellard
44 0824d6fc bellard
#include <unistd.h>
45 0824d6fc bellard
#include <fcntl.h>
46 0824d6fc bellard
#include <signal.h>
47 0824d6fc bellard
#include <time.h>
48 0824d6fc bellard
#include <errno.h>
49 67b915a5 bellard
#include <sys/time.h>
50 c88676f8 bellard
#include <zlib.h>
51 67b915a5 bellard
52 67b915a5 bellard
#ifndef _WIN32
53 67b915a5 bellard
#include <sys/times.h>
54 f1510b2c bellard
#include <sys/wait.h>
55 67b915a5 bellard
#include <termios.h>
56 67b915a5 bellard
#include <sys/mman.h>
57 f1510b2c bellard
#include <sys/ioctl.h>
58 24646c7e blueswir1
#include <sys/resource.h>
59 f1510b2c bellard
#include <sys/socket.h>
60 c94c8d64 bellard
#include <netinet/in.h>
61 24646c7e blueswir1
#include <net/if.h>
62 24646c7e blueswir1
#if defined(__NetBSD__)
63 24646c7e blueswir1
#include <net/if_tap.h>
64 24646c7e blueswir1
#endif
65 24646c7e blueswir1
#ifdef __linux__
66 24646c7e blueswir1
#include <linux/if_tun.h>
67 24646c7e blueswir1
#endif
68 24646c7e blueswir1
#include <arpa/inet.h>
69 9d728e8c bellard
#include <dirent.h>
70 7c9d8e07 bellard
#include <netdb.h>
71 cb4b976b ths
#include <sys/select.h>
72 7d3505c5 bellard
#ifdef _BSD
73 7d3505c5 bellard
#include <sys/stat.h>
74 24646c7e blueswir1
#ifdef __FreeBSD__
75 7d3505c5 bellard
#include <libutil.h>
76 24646c7e blueswir1
#else
77 24646c7e blueswir1
#include <util.h>
78 128ab2ff blueswir1
#endif
79 5c40d2bd ths
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
80 5c40d2bd ths
#include <freebsd/stdlib.h>
81 7d3505c5 bellard
#else
82 223f0d72 blueswir1
#ifdef __linux__
83 7d3505c5 bellard
#include <pty.h>
84 7d3505c5 bellard
#include <malloc.h>
85 fd872598 bellard
#include <linux/rtc.h>
86 bd494f4c ths
87 bd494f4c ths
/* For the benefit of older linux systems which don't supply it,
88 bd494f4c ths
   we use a local copy of hpet.h. */
89 bd494f4c ths
/* #include <linux/hpet.h> */
90 bd494f4c ths
#include "hpet.h"
91 bd494f4c ths
92 e57a8c0e bellard
#include <linux/ppdev.h>
93 5867c88a ths
#include <linux/parport.h>
94 223f0d72 blueswir1
#endif
95 223f0d72 blueswir1
#ifdef __sun__
96 d5d10bc3 ths
#include <sys/stat.h>
97 d5d10bc3 ths
#include <sys/ethernet.h>
98 d5d10bc3 ths
#include <sys/sockio.h>
99 d5d10bc3 ths
#include <netinet/arp.h>
100 d5d10bc3 ths
#include <netinet/in.h>
101 d5d10bc3 ths
#include <netinet/in_systm.h>
102 d5d10bc3 ths
#include <netinet/ip.h>
103 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
104 d5d10bc3 ths
#include <netinet/udp.h>
105 d5d10bc3 ths
#include <netinet/tcp.h>
106 d5d10bc3 ths
#include <net/if.h>
107 d5d10bc3 ths
#include <syslog.h>
108 d5d10bc3 ths
#include <stropts.h>
109 67b915a5 bellard
#endif
110 7d3505c5 bellard
#endif
111 ec530c81 bellard
#endif
112 67b915a5 bellard
113 03ff3ca3 aliguori
#include "qemu_socket.h"
114 03ff3ca3 aliguori
115 c20709aa bellard
#if defined(CONFIG_SLIRP)
116 c20709aa bellard
#include "libslirp.h"
117 c20709aa bellard
#endif
118 c20709aa bellard
119 9892fbfb blueswir1
#if defined(__OpenBSD__)
120 9892fbfb blueswir1
#include <util.h>
121 9892fbfb blueswir1
#endif
122 9892fbfb blueswir1
123 8a16d273 ths
#if defined(CONFIG_VDE)
124 8a16d273 ths
#include <libvdeplug.h>
125 8a16d273 ths
#endif
126 8a16d273 ths
127 67b915a5 bellard
#ifdef _WIN32
128 7d3505c5 bellard
#include <malloc.h>
129 67b915a5 bellard
#include <sys/timeb.h>
130 4fddf62a ths
#include <mmsystem.h>
131 67b915a5 bellard
#define getopt_long_only getopt_long
132 67b915a5 bellard
#define memalign(align, size) malloc(size)
133 67b915a5 bellard
#endif
134 67b915a5 bellard
135 73332e5c bellard
#ifdef CONFIG_SDL
136 96bcd4f8 bellard
#ifdef __APPLE__
137 83fb7adf bellard
#include <SDL/SDL.h>
138 96bcd4f8 bellard
#endif
139 73332e5c bellard
#endif /* CONFIG_SDL */
140 0824d6fc bellard
141 5b0753e0 bellard
#ifdef CONFIG_COCOA
142 5b0753e0 bellard
#undef main
143 5b0753e0 bellard
#define main qemu_main
144 5b0753e0 bellard
#endif /* CONFIG_COCOA */
145 5b0753e0 bellard
146 0824d6fc bellard
#include "disas.h"
147 fc01f7e7 bellard
148 8a7ddc38 bellard
#include "exec-all.h"
149 0824d6fc bellard
150 5a67135a bellard
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
151 b46a8906 ths
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
152 a14d6c8c pbrook
#ifdef __sun__
153 a14d6c8c pbrook
#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
154 a14d6c8c pbrook
#else
155 a14d6c8c pbrook
#define SMBD_COMMAND "/usr/sbin/smbd"
156 a14d6c8c pbrook
#endif
157 f1510b2c bellard
158 0824d6fc bellard
//#define DEBUG_UNUSED_IOPORT
159 fd872598 bellard
//#define DEBUG_IOPORT
160 9dc63a1e blueswir1
//#define DEBUG_NET
161 9dc63a1e blueswir1
//#define DEBUG_SLIRP
162 330d0414 bellard
163 77d4bc34 bellard
#ifdef TARGET_PPC
164 77d4bc34 bellard
#define DEFAULT_RAM_SIZE 144
165 77d4bc34 bellard
#else
166 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
167 77d4bc34 bellard
#endif
168 313aa567 bellard
169 0d92ed30 pbrook
/* Max number of USB devices that can be specified on the commandline.  */
170 0d92ed30 pbrook
#define MAX_USB_CMDLINE 8
171 0d92ed30 pbrook
172 dc72ac14 balrog
/* Max number of bluetooth switches on the commandline.  */
173 dc72ac14 balrog
#define MAX_BT_CMDLINE 10
174 dc72ac14 balrog
175 7dea1da4 bellard
/* XXX: use a two level table to limit memory usage */
176 7dea1da4 bellard
#define MAX_IOPORTS 65536
177 0824d6fc bellard
178 80cabfad bellard
const char *bios_dir = CONFIG_QEMU_SHAREDIR;
179 1192dad8 j_mayer
const char *bios_name = NULL;
180 dbed7e40 blueswir1
static void *ioport_opaque[MAX_IOPORTS];
181 dbed7e40 blueswir1
static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
182 dbed7e40 blueswir1
static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
183 e4bcb14c ths
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
184 faea38e7 bellard
   to store the VM snapshots */
185 e4bcb14c ths
DriveInfo drives_table[MAX_DRIVES+1];
186 e4bcb14c ths
int nb_drives;
187 faea38e7 bellard
/* point to the block driver where the snapshots are managed */
188 dbed7e40 blueswir1
static BlockDriverState *bs_snapshots;
189 dbed7e40 blueswir1
static int vga_ram_size;
190 cb5a7aa8 malc
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
191 0e82f34d aliguori
DisplayState display_state;
192 a20dd508 bellard
int nographic;
193 dbed7e40 blueswir1
static int curses;
194 3d11d0eb bellard
const char* keyboard_layout = NULL;
195 313aa567 bellard
int64_t ticks_per_sec;
196 00f82b8a aurel32
ram_addr_t ram_size;
197 c4b1fcc0 bellard
int nb_nics;
198 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
199 8a7ddc38 bellard
int vm_running;
200 f6503059 balrog
static int rtc_utc = 1;
201 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
202 1bfe856e bellard
int cirrus_vga_enabled = 1;
203 d34cab9f ths
int vmsvga_enabled = 0;
204 d827220b bellard
#ifdef TARGET_SPARC
205 d827220b bellard
int graphic_width = 1024;
206 d827220b bellard
int graphic_height = 768;
207 eee0b836 blueswir1
int graphic_depth = 8;
208 d827220b bellard
#else
209 1bfe856e bellard
int graphic_width = 800;
210 1bfe856e bellard
int graphic_height = 600;
211 e9b137c2 bellard
int graphic_depth = 15;
212 eee0b836 blueswir1
#endif
213 dbed7e40 blueswir1
static int full_screen = 0;
214 dbed7e40 blueswir1
static int no_frame = 0;
215 667accab ths
int no_quit = 0;
216 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
217 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
218 a09db21f bellard
#ifdef TARGET_I386
219 a09db21f bellard
int win2k_install_hack = 0;
220 a09db21f bellard
#endif
221 bb36d470 bellard
int usb_enabled = 0;
222 6a00d601 bellard
int smp_cpus = 1;
223 73fc9742 ths
const char *vnc_display;
224 6515b203 bellard
int acpi_enabled = 1;
225 52ca8d6a bellard
int fd_bootchk = 1;
226 d1beab82 bellard
int no_reboot = 0;
227 b2f76161 aurel32
int no_shutdown = 0;
228 9467cd46 balrog
int cursor_hide = 1;
229 a171fe39 balrog
int graphic_rotate = 0;
230 71e3ceb8 ths
int daemonize = 0;
231 9ae02555 ths
const char *option_rom[MAX_OPTION_ROMS];
232 9ae02555 ths
int nb_option_roms;
233 8e71621f pbrook
int semihosting_enabled = 0;
234 2b8f2d41 balrog
#ifdef TARGET_ARM
235 2b8f2d41 balrog
int old_param = 0;
236 2b8f2d41 balrog
#endif
237 c35734b2 ths
const char *qemu_name;
238 3780e197 ths
int alt_grab = 0;
239 66508601 blueswir1
#ifdef TARGET_SPARC
240 66508601 blueswir1
unsigned int nb_prom_envs = 0;
241 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
242 66508601 blueswir1
#endif
243 dbed7e40 blueswir1
static int nb_drives_opt;
244 dbed7e40 blueswir1
static struct drive_opt {
245 609497ab balrog
    const char *file;
246 609497ab balrog
    char opt[1024];
247 609497ab balrog
} drives_opt[MAX_DRIVES];
248 0824d6fc bellard
249 ee5605e5 balrog
static CPUState *cur_cpu;
250 ee5605e5 balrog
static CPUState *next_cpu;
251 76ea08f9 balrog
static int event_pending = 1;
252 bf20dc07 ths
/* Conversion factor from emulated instructions to virtual clock ticks.  */
253 2e70f6ef pbrook
static int icount_time_shift;
254 bf20dc07 ths
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
255 2e70f6ef pbrook
#define MAX_ICOUNT_SHIFT 10
256 2e70f6ef pbrook
/* Compensate for varying guest execution speed.  */
257 2e70f6ef pbrook
static int64_t qemu_icount_bias;
258 dbed7e40 blueswir1
static QEMUTimer *icount_rt_timer;
259 dbed7e40 blueswir1
static QEMUTimer *icount_vm_timer;
260 ee5605e5 balrog
261 8fcb1b90 blueswir1
uint8_t qemu_uuid[16];
262 8fcb1b90 blueswir1
263 0824d6fc bellard
/***********************************************************/
264 26aa7d72 bellard
/* x86 ISA bus support */
265 26aa7d72 bellard
266 26aa7d72 bellard
target_phys_addr_t isa_mem_base = 0;
267 3de388f6 bellard
PicState2 *isa_pic;
268 0824d6fc bellard
269 477e3edf aliguori
static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
270 477e3edf aliguori
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
271 477e3edf aliguori
272 477e3edf aliguori
static uint32_t ioport_read(int index, uint32_t address)
273 477e3edf aliguori
{
274 477e3edf aliguori
    static IOPortReadFunc *default_func[3] = {
275 477e3edf aliguori
        default_ioport_readb,
276 477e3edf aliguori
        default_ioport_readw,
277 477e3edf aliguori
        default_ioport_readl
278 477e3edf aliguori
    };
279 477e3edf aliguori
    IOPortReadFunc *func = ioport_read_table[index][address];
280 477e3edf aliguori
    if (!func)
281 477e3edf aliguori
        func = default_func[index];
282 477e3edf aliguori
    return func(ioport_opaque[address], address);
283 477e3edf aliguori
}
284 477e3edf aliguori
285 477e3edf aliguori
static void ioport_write(int index, uint32_t address, uint32_t data)
286 477e3edf aliguori
{
287 477e3edf aliguori
    static IOPortWriteFunc *default_func[3] = {
288 477e3edf aliguori
        default_ioport_writeb,
289 477e3edf aliguori
        default_ioport_writew,
290 477e3edf aliguori
        default_ioport_writel
291 477e3edf aliguori
    };
292 477e3edf aliguori
    IOPortWriteFunc *func = ioport_write_table[index][address];
293 477e3edf aliguori
    if (!func)
294 477e3edf aliguori
        func = default_func[index];
295 477e3edf aliguori
    func(ioport_opaque[address], address, data);
296 477e3edf aliguori
}
297 477e3edf aliguori
298 9596ebb7 pbrook
static uint32_t default_ioport_readb(void *opaque, uint32_t address)
299 0824d6fc bellard
{
300 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
301 1196be37 ths
    fprintf(stderr, "unused inb: port=0x%04x\n", address);
302 0824d6fc bellard
#endif
303 fc01f7e7 bellard
    return 0xff;
304 0824d6fc bellard
}
305 0824d6fc bellard
306 9596ebb7 pbrook
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
307 0824d6fc bellard
{
308 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
309 1196be37 ths
    fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
310 0824d6fc bellard
#endif
311 0824d6fc bellard
}
312 0824d6fc bellard
313 0824d6fc bellard
/* default is to make two byte accesses */
314 9596ebb7 pbrook
static uint32_t default_ioport_readw(void *opaque, uint32_t address)
315 0824d6fc bellard
{
316 0824d6fc bellard
    uint32_t data;
317 477e3edf aliguori
    data = ioport_read(0, address);
318 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
319 477e3edf aliguori
    data |= ioport_read(0, address) << 8;
320 0824d6fc bellard
    return data;
321 0824d6fc bellard
}
322 0824d6fc bellard
323 9596ebb7 pbrook
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
324 0824d6fc bellard
{
325 477e3edf aliguori
    ioport_write(0, address, data & 0xff);
326 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
327 477e3edf aliguori
    ioport_write(0, address, (data >> 8) & 0xff);
328 0824d6fc bellard
}
329 0824d6fc bellard
330 9596ebb7 pbrook
static uint32_t default_ioport_readl(void *opaque, uint32_t address)
331 0824d6fc bellard
{
332 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
333 1196be37 ths
    fprintf(stderr, "unused inl: port=0x%04x\n", address);
334 fc01f7e7 bellard
#endif
335 fc01f7e7 bellard
    return 0xffffffff;
336 0824d6fc bellard
}
337 0824d6fc bellard
338 9596ebb7 pbrook
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
339 0824d6fc bellard
{
340 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
341 1196be37 ths
    fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
342 fc01f7e7 bellard
#endif
343 0824d6fc bellard
}
344 0824d6fc bellard
345 fc01f7e7 bellard
/* size is the word size in byte */
346 5fafdf24 ths
int register_ioport_read(int start, int length, int size,
347 c4b1fcc0 bellard
                         IOPortReadFunc *func, void *opaque)
348 f1510b2c bellard
{
349 fc01f7e7 bellard
    int i, bsize;
350 f1510b2c bellard
351 c4b1fcc0 bellard
    if (size == 1) {
352 fc01f7e7 bellard
        bsize = 0;
353 c4b1fcc0 bellard
    } else if (size == 2) {
354 fc01f7e7 bellard
        bsize = 1;
355 c4b1fcc0 bellard
    } else if (size == 4) {
356 fc01f7e7 bellard
        bsize = 2;
357 c4b1fcc0 bellard
    } else {
358 88fdf56f balrog
        hw_error("register_ioport_read: invalid size");
359 fc01f7e7 bellard
        return -1;
360 c4b1fcc0 bellard
    }
361 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
362 fc01f7e7 bellard
        ioport_read_table[bsize][i] = func;
363 c4b1fcc0 bellard
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
364 88fdf56f balrog
            hw_error("register_ioport_read: invalid opaque");
365 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
366 c4b1fcc0 bellard
    }
367 f1510b2c bellard
    return 0;
368 f1510b2c bellard
}
369 f1510b2c bellard
370 fc01f7e7 bellard
/* size is the word size in byte */
371 5fafdf24 ths
int register_ioport_write(int start, int length, int size,
372 c4b1fcc0 bellard
                          IOPortWriteFunc *func, void *opaque)
373 f1510b2c bellard
{
374 fc01f7e7 bellard
    int i, bsize;
375 f1510b2c bellard
376 c4b1fcc0 bellard
    if (size == 1) {
377 fc01f7e7 bellard
        bsize = 0;
378 c4b1fcc0 bellard
    } else if (size == 2) {
379 fc01f7e7 bellard
        bsize = 1;
380 c4b1fcc0 bellard
    } else if (size == 4) {
381 fc01f7e7 bellard
        bsize = 2;
382 c4b1fcc0 bellard
    } else {
383 88fdf56f balrog
        hw_error("register_ioport_write: invalid size");
384 fc01f7e7 bellard
        return -1;
385 c4b1fcc0 bellard
    }
386 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
387 fc01f7e7 bellard
        ioport_write_table[bsize][i] = func;
388 88fdf56f balrog
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
389 88fdf56f balrog
            hw_error("register_ioport_write: invalid opaque");
390 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
391 c4b1fcc0 bellard
    }
392 f1510b2c bellard
    return 0;
393 f1510b2c bellard
}
394 f1510b2c bellard
395 69b91039 bellard
void isa_unassign_ioport(int start, int length)
396 69b91039 bellard
{
397 69b91039 bellard
    int i;
398 69b91039 bellard
399 69b91039 bellard
    for(i = start; i < start + length; i++) {
400 69b91039 bellard
        ioport_read_table[0][i] = default_ioport_readb;
401 69b91039 bellard
        ioport_read_table[1][i] = default_ioport_readw;
402 69b91039 bellard
        ioport_read_table[2][i] = default_ioport_readl;
403 69b91039 bellard
404 69b91039 bellard
        ioport_write_table[0][i] = default_ioport_writeb;
405 69b91039 bellard
        ioport_write_table[1][i] = default_ioport_writew;
406 69b91039 bellard
        ioport_write_table[2][i] = default_ioport_writel;
407 69b91039 bellard
    }
408 69b91039 bellard
}
409 69b91039 bellard
410 20f32282 bellard
/***********************************************************/
411 20f32282 bellard
412 c45886db bellard
void cpu_outb(CPUState *env, int addr, int val)
413 0824d6fc bellard
{
414 fd872598 bellard
#ifdef DEBUG_IOPORT
415 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
416 fd872598 bellard
        fprintf(logfile, "outb: %04x %02x\n", addr, val);
417 3b46e624 ths
#endif
418 477e3edf aliguori
    ioport_write(0, addr, val);
419 89bfc105 bellard
#ifdef USE_KQEMU
420 89bfc105 bellard
    if (env)
421 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
422 89bfc105 bellard
#endif
423 0824d6fc bellard
}
424 0824d6fc bellard
425 c45886db bellard
void cpu_outw(CPUState *env, int addr, int val)
426 0824d6fc bellard
{
427 fd872598 bellard
#ifdef DEBUG_IOPORT
428 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
429 fd872598 bellard
        fprintf(logfile, "outw: %04x %04x\n", addr, val);
430 3b46e624 ths
#endif
431 477e3edf aliguori
    ioport_write(1, addr, val);
432 89bfc105 bellard
#ifdef USE_KQEMU
433 89bfc105 bellard
    if (env)
434 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
435 89bfc105 bellard
#endif
436 0824d6fc bellard
}
437 0824d6fc bellard
438 c45886db bellard
void cpu_outl(CPUState *env, int addr, int val)
439 0824d6fc bellard
{
440 fd872598 bellard
#ifdef DEBUG_IOPORT
441 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
442 fd872598 bellard
        fprintf(logfile, "outl: %04x %08x\n", addr, val);
443 fd872598 bellard
#endif
444 477e3edf aliguori
    ioport_write(2, addr, val);
445 89bfc105 bellard
#ifdef USE_KQEMU
446 89bfc105 bellard
    if (env)
447 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
448 89bfc105 bellard
#endif
449 0824d6fc bellard
}
450 0824d6fc bellard
451 c45886db bellard
int cpu_inb(CPUState *env, int addr)
452 0824d6fc bellard
{
453 fd872598 bellard
    int val;
454 477e3edf aliguori
    val = ioport_read(0, addr);
455 fd872598 bellard
#ifdef DEBUG_IOPORT
456 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
457 fd872598 bellard
        fprintf(logfile, "inb : %04x %02x\n", addr, val);
458 fd872598 bellard
#endif
459 89bfc105 bellard
#ifdef USE_KQEMU
460 89bfc105 bellard
    if (env)
461 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
462 89bfc105 bellard
#endif
463 fd872598 bellard
    return val;
464 0824d6fc bellard
}
465 0824d6fc bellard
466 c45886db bellard
int cpu_inw(CPUState *env, int addr)
467 0824d6fc bellard
{
468 fd872598 bellard
    int val;
469 477e3edf aliguori
    val = ioport_read(1, addr);
470 fd872598 bellard
#ifdef DEBUG_IOPORT
471 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
472 fd872598 bellard
        fprintf(logfile, "inw : %04x %04x\n", addr, val);
473 fd872598 bellard
#endif
474 89bfc105 bellard
#ifdef USE_KQEMU
475 89bfc105 bellard
    if (env)
476 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
477 89bfc105 bellard
#endif
478 fd872598 bellard
    return val;
479 0824d6fc bellard
}
480 0824d6fc bellard
481 c45886db bellard
int cpu_inl(CPUState *env, int addr)
482 0824d6fc bellard
{
483 fd872598 bellard
    int val;
484 477e3edf aliguori
    val = ioport_read(2, addr);
485 fd872598 bellard
#ifdef DEBUG_IOPORT
486 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
487 fd872598 bellard
        fprintf(logfile, "inl : %04x %08x\n", addr, val);
488 fd872598 bellard
#endif
489 89bfc105 bellard
#ifdef USE_KQEMU
490 89bfc105 bellard
    if (env)
491 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
492 89bfc105 bellard
#endif
493 fd872598 bellard
    return val;
494 0824d6fc bellard
}
495 0824d6fc bellard
496 0824d6fc bellard
/***********************************************************/
497 0824d6fc bellard
void hw_error(const char *fmt, ...)
498 0824d6fc bellard
{
499 0824d6fc bellard
    va_list ap;
500 6a00d601 bellard
    CPUState *env;
501 0824d6fc bellard
502 0824d6fc bellard
    va_start(ap, fmt);
503 0824d6fc bellard
    fprintf(stderr, "qemu: hardware error: ");
504 0824d6fc bellard
    vfprintf(stderr, fmt, ap);
505 0824d6fc bellard
    fprintf(stderr, "\n");
506 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
507 6a00d601 bellard
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
508 0824d6fc bellard
#ifdef TARGET_I386
509 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
510 c45886db bellard
#else
511 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, 0);
512 0824d6fc bellard
#endif
513 6a00d601 bellard
    }
514 0824d6fc bellard
    va_end(ap);
515 0824d6fc bellard
    abort();
516 0824d6fc bellard
}
517 0824d6fc bellard
518 8a7ddc38 bellard
/***********************************************************/
519 63066f4f bellard
/* keyboard/mouse */
520 63066f4f bellard
521 63066f4f bellard
static QEMUPutKBDEvent *qemu_put_kbd_event;
522 63066f4f bellard
static void *qemu_put_kbd_event_opaque;
523 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
524 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
525 63066f4f bellard
526 63066f4f bellard
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
527 63066f4f bellard
{
528 63066f4f bellard
    qemu_put_kbd_event_opaque = opaque;
529 63066f4f bellard
    qemu_put_kbd_event = func;
530 63066f4f bellard
}
531 63066f4f bellard
532 455204eb ths
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
533 455204eb ths
                                                void *opaque, int absolute,
534 455204eb ths
                                                const char *name)
535 63066f4f bellard
{
536 455204eb ths
    QEMUPutMouseEntry *s, *cursor;
537 455204eb ths
538 455204eb ths
    s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
539 455204eb ths
    if (!s)
540 455204eb ths
        return NULL;
541 455204eb ths
542 455204eb ths
    s->qemu_put_mouse_event = func;
543 455204eb ths
    s->qemu_put_mouse_event_opaque = opaque;
544 455204eb ths
    s->qemu_put_mouse_event_absolute = absolute;
545 455204eb ths
    s->qemu_put_mouse_event_name = qemu_strdup(name);
546 455204eb ths
    s->next = NULL;
547 455204eb ths
548 455204eb ths
    if (!qemu_put_mouse_event_head) {
549 455204eb ths
        qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
550 455204eb ths
        return s;
551 455204eb ths
    }
552 455204eb ths
553 455204eb ths
    cursor = qemu_put_mouse_event_head;
554 455204eb ths
    while (cursor->next != NULL)
555 455204eb ths
        cursor = cursor->next;
556 455204eb ths
557 455204eb ths
    cursor->next = s;
558 455204eb ths
    qemu_put_mouse_event_current = s;
559 455204eb ths
560 455204eb ths
    return s;
561 455204eb ths
}
562 455204eb ths
563 455204eb ths
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
564 455204eb ths
{
565 455204eb ths
    QEMUPutMouseEntry *prev = NULL, *cursor;
566 455204eb ths
567 455204eb ths
    if (!qemu_put_mouse_event_head || entry == NULL)
568 455204eb ths
        return;
569 455204eb ths
570 455204eb ths
    cursor = qemu_put_mouse_event_head;
571 455204eb ths
    while (cursor != NULL && cursor != entry) {
572 455204eb ths
        prev = cursor;
573 455204eb ths
        cursor = cursor->next;
574 455204eb ths
    }
575 455204eb ths
576 455204eb ths
    if (cursor == NULL) // does not exist or list empty
577 455204eb ths
        return;
578 455204eb ths
    else if (prev == NULL) { // entry is head
579 455204eb ths
        qemu_put_mouse_event_head = cursor->next;
580 455204eb ths
        if (qemu_put_mouse_event_current == entry)
581 455204eb ths
            qemu_put_mouse_event_current = cursor->next;
582 455204eb ths
        qemu_free(entry->qemu_put_mouse_event_name);
583 455204eb ths
        qemu_free(entry);
584 455204eb ths
        return;
585 455204eb ths
    }
586 455204eb ths
587 455204eb ths
    prev->next = entry->next;
588 455204eb ths
589 455204eb ths
    if (qemu_put_mouse_event_current == entry)
590 455204eb ths
        qemu_put_mouse_event_current = prev;
591 455204eb ths
592 455204eb ths
    qemu_free(entry->qemu_put_mouse_event_name);
593 455204eb ths
    qemu_free(entry);
594 63066f4f bellard
}
595 63066f4f bellard
596 63066f4f bellard
void kbd_put_keycode(int keycode)
597 63066f4f bellard
{
598 63066f4f bellard
    if (qemu_put_kbd_event) {
599 63066f4f bellard
        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
600 63066f4f bellard
    }
601 63066f4f bellard
}
602 63066f4f bellard
603 63066f4f bellard
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
604 63066f4f bellard
{
605 455204eb ths
    QEMUPutMouseEvent *mouse_event;
606 455204eb ths
    void *mouse_event_opaque;
607 a171fe39 balrog
    int width;
608 455204eb ths
609 455204eb ths
    if (!qemu_put_mouse_event_current) {
610 455204eb ths
        return;
611 455204eb ths
    }
612 455204eb ths
613 455204eb ths
    mouse_event =
614 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event;
615 455204eb ths
    mouse_event_opaque =
616 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
617 455204eb ths
618 455204eb ths
    if (mouse_event) {
619 a171fe39 balrog
        if (graphic_rotate) {
620 a171fe39 balrog
            if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
621 a171fe39 balrog
                width = 0x7fff;
622 a171fe39 balrog
            else
623 b94ed577 aurel32
                width = graphic_width - 1;
624 a171fe39 balrog
            mouse_event(mouse_event_opaque,
625 a171fe39 balrog
                                 width - dy, dx, dz, buttons_state);
626 a171fe39 balrog
        } else
627 a171fe39 balrog
            mouse_event(mouse_event_opaque,
628 a171fe39 balrog
                                 dx, dy, dz, buttons_state);
629 63066f4f bellard
    }
630 63066f4f bellard
}
631 63066f4f bellard
632 09b26c5e bellard
int kbd_mouse_is_absolute(void)
633 09b26c5e bellard
{
634 455204eb ths
    if (!qemu_put_mouse_event_current)
635 455204eb ths
        return 0;
636 455204eb ths
637 455204eb ths
    return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
638 455204eb ths
}
639 455204eb ths
640 455204eb ths
void do_info_mice(void)
641 455204eb ths
{
642 455204eb ths
    QEMUPutMouseEntry *cursor;
643 455204eb ths
    int index = 0;
644 455204eb ths
645 455204eb ths
    if (!qemu_put_mouse_event_head) {
646 455204eb ths
        term_printf("No mouse devices connected\n");
647 455204eb ths
        return;
648 455204eb ths
    }
649 455204eb ths
650 455204eb ths
    term_printf("Mouse devices available:\n");
651 455204eb ths
    cursor = qemu_put_mouse_event_head;
652 455204eb ths
    while (cursor != NULL) {
653 455204eb ths
        term_printf("%c Mouse #%d: %s\n",
654 455204eb ths
                    (cursor == qemu_put_mouse_event_current ? '*' : ' '),
655 455204eb ths
                    index, cursor->qemu_put_mouse_event_name);
656 455204eb ths
        index++;
657 455204eb ths
        cursor = cursor->next;
658 455204eb ths
    }
659 455204eb ths
}
660 455204eb ths
661 455204eb ths
void do_mouse_set(int index)
662 455204eb ths
{
663 455204eb ths
    QEMUPutMouseEntry *cursor;
664 455204eb ths
    int i = 0;
665 455204eb ths
666 455204eb ths
    if (!qemu_put_mouse_event_head) {
667 455204eb ths
        term_printf("No mouse devices connected\n");
668 455204eb ths
        return;
669 455204eb ths
    }
670 455204eb ths
671 455204eb ths
    cursor = qemu_put_mouse_event_head;
672 455204eb ths
    while (cursor != NULL && index != i) {
673 455204eb ths
        i++;
674 455204eb ths
        cursor = cursor->next;
675 455204eb ths
    }
676 455204eb ths
677 455204eb ths
    if (cursor != NULL)
678 455204eb ths
        qemu_put_mouse_event_current = cursor;
679 455204eb ths
    else
680 455204eb ths
        term_printf("Mouse at given index not found\n");
681 09b26c5e bellard
}
682 09b26c5e bellard
683 1dce7c3c bellard
/* compute with 96 bit intermediate result: (a*b)/c */
684 1dce7c3c bellard
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
685 0824d6fc bellard
{
686 1dce7c3c bellard
    union {
687 1dce7c3c bellard
        uint64_t ll;
688 1dce7c3c bellard
        struct {
689 1dce7c3c bellard
#ifdef WORDS_BIGENDIAN
690 1dce7c3c bellard
            uint32_t high, low;
691 1dce7c3c bellard
#else
692 1dce7c3c bellard
            uint32_t low, high;
693 3b46e624 ths
#endif
694 1dce7c3c bellard
        } l;
695 1dce7c3c bellard
    } u, res;
696 1dce7c3c bellard
    uint64_t rl, rh;
697 0824d6fc bellard
698 1dce7c3c bellard
    u.ll = a;
699 1dce7c3c bellard
    rl = (uint64_t)u.l.low * (uint64_t)b;
700 1dce7c3c bellard
    rh = (uint64_t)u.l.high * (uint64_t)b;
701 1dce7c3c bellard
    rh += (rl >> 32);
702 1dce7c3c bellard
    res.l.high = rh / c;
703 1dce7c3c bellard
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
704 1dce7c3c bellard
    return res.ll;
705 34865134 bellard
}
706 34865134 bellard
707 1dce7c3c bellard
/***********************************************************/
708 1dce7c3c bellard
/* real time host monotonic timer */
709 34865134 bellard
710 1dce7c3c bellard
#define QEMU_TIMER_BASE 1000000000LL
711 34865134 bellard
712 1dce7c3c bellard
#ifdef WIN32
713 0824d6fc bellard
714 1dce7c3c bellard
static int64_t clock_freq;
715 1115dde7 bellard
716 1dce7c3c bellard
static void init_get_clock(void)
717 1115dde7 bellard
{
718 a8e5ac33 bellard
    LARGE_INTEGER freq;
719 a8e5ac33 bellard
    int ret;
720 1dce7c3c bellard
    ret = QueryPerformanceFrequency(&freq);
721 1dce7c3c bellard
    if (ret == 0) {
722 1dce7c3c bellard
        fprintf(stderr, "Could not calibrate ticks\n");
723 1dce7c3c bellard
        exit(1);
724 1dce7c3c bellard
    }
725 1dce7c3c bellard
    clock_freq = freq.QuadPart;
726 1115dde7 bellard
}
727 1115dde7 bellard
728 1dce7c3c bellard
static int64_t get_clock(void)
729 b8076a74 bellard
{
730 1dce7c3c bellard
    LARGE_INTEGER ti;
731 1dce7c3c bellard
    QueryPerformanceCounter(&ti);
732 1dce7c3c bellard
    return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
733 b8076a74 bellard
}
734 b8076a74 bellard
735 1dce7c3c bellard
#else
736 90cb9493 bellard
737 1dce7c3c bellard
static int use_rt_clock;
738 1dce7c3c bellard
739 1dce7c3c bellard
static void init_get_clock(void)
740 90cb9493 bellard
{
741 1dce7c3c bellard
    use_rt_clock = 0;
742 60759371 aurel32
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
743 1dce7c3c bellard
    {
744 1dce7c3c bellard
        struct timespec ts;
745 1dce7c3c bellard
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
746 1dce7c3c bellard
            use_rt_clock = 1;
747 1dce7c3c bellard
        }
748 1dce7c3c bellard
    }
749 1dce7c3c bellard
#endif
750 90cb9493 bellard
}
751 90cb9493 bellard
752 1dce7c3c bellard
static int64_t get_clock(void)
753 fdbb4691 bellard
{
754 60759371 aurel32
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
755 1dce7c3c bellard
    if (use_rt_clock) {
756 1dce7c3c bellard
        struct timespec ts;
757 1dce7c3c bellard
        clock_gettime(CLOCK_MONOTONIC, &ts);
758 1dce7c3c bellard
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
759 5fafdf24 ths
    } else
760 fdbb4691 bellard
#endif
761 1dce7c3c bellard
    {
762 1dce7c3c bellard
        /* XXX: using gettimeofday leads to problems if the date
763 1dce7c3c bellard
           changes, so it should be avoided. */
764 1dce7c3c bellard
        struct timeval tv;
765 1dce7c3c bellard
        gettimeofday(&tv, NULL);
766 1dce7c3c bellard
        return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
767 1dce7c3c bellard
    }
768 fdbb4691 bellard
}
769 34865134 bellard
#endif
770 34865134 bellard
771 2e70f6ef pbrook
/* Return the virtual CPU time, based on the instruction counter.  */
772 2e70f6ef pbrook
static int64_t cpu_get_icount(void)
773 2e70f6ef pbrook
{
774 2e70f6ef pbrook
    int64_t icount;
775 2e70f6ef pbrook
    CPUState *env = cpu_single_env;;
776 2e70f6ef pbrook
    icount = qemu_icount;
777 2e70f6ef pbrook
    if (env) {
778 2e70f6ef pbrook
        if (!can_do_io(env))
779 2e70f6ef pbrook
            fprintf(stderr, "Bad clock read\n");
780 2e70f6ef pbrook
        icount -= (env->icount_decr.u16.low + env->icount_extra);
781 2e70f6ef pbrook
    }
782 2e70f6ef pbrook
    return qemu_icount_bias + (icount << icount_time_shift);
783 2e70f6ef pbrook
}
784 2e70f6ef pbrook
785 1dce7c3c bellard
/***********************************************************/
786 1dce7c3c bellard
/* guest cycle counter */
787 1dce7c3c bellard
788 eade0f19 bellard
static int64_t cpu_ticks_prev;
789 34865134 bellard
static int64_t cpu_ticks_offset;
790 1dce7c3c bellard
static int64_t cpu_clock_offset;
791 8a7ddc38 bellard
static int cpu_ticks_enabled;
792 34865134 bellard
793 1dce7c3c bellard
/* return the host CPU cycle counter and handle stop/restart */
794 1dce7c3c bellard
int64_t cpu_get_ticks(void)
795 34865134 bellard
{
796 2e70f6ef pbrook
    if (use_icount) {
797 2e70f6ef pbrook
        return cpu_get_icount();
798 2e70f6ef pbrook
    }
799 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
800 8a7ddc38 bellard
        return cpu_ticks_offset;
801 8a7ddc38 bellard
    } else {
802 eade0f19 bellard
        int64_t ticks;
803 eade0f19 bellard
        ticks = cpu_get_real_ticks();
804 eade0f19 bellard
        if (cpu_ticks_prev > ticks) {
805 eade0f19 bellard
            /* Note: non increasing ticks may happen if the host uses
806 eade0f19 bellard
               software suspend */
807 eade0f19 bellard
            cpu_ticks_offset += cpu_ticks_prev - ticks;
808 eade0f19 bellard
        }
809 eade0f19 bellard
        cpu_ticks_prev = ticks;
810 eade0f19 bellard
        return ticks + cpu_ticks_offset;
811 8a7ddc38 bellard
    }
812 34865134 bellard
}
813 34865134 bellard
814 1dce7c3c bellard
/* return the host CPU monotonic timer and handle stop/restart */
815 1dce7c3c bellard
static int64_t cpu_get_clock(void)
816 1dce7c3c bellard
{
817 1dce7c3c bellard
    int64_t ti;
818 1dce7c3c bellard
    if (!cpu_ticks_enabled) {
819 1dce7c3c bellard
        return cpu_clock_offset;
820 1dce7c3c bellard
    } else {
821 1dce7c3c bellard
        ti = get_clock();
822 1dce7c3c bellard
        return ti + cpu_clock_offset;
823 1dce7c3c bellard
    }
824 1dce7c3c bellard
}
825 1dce7c3c bellard
826 34865134 bellard
/* enable cpu_get_ticks() */
827 34865134 bellard
void cpu_enable_ticks(void)
828 34865134 bellard
{
829 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
830 8a7ddc38 bellard
        cpu_ticks_offset -= cpu_get_real_ticks();
831 1dce7c3c bellard
        cpu_clock_offset -= get_clock();
832 8a7ddc38 bellard
        cpu_ticks_enabled = 1;
833 8a7ddc38 bellard
    }
834 34865134 bellard
}
835 34865134 bellard
836 34865134 bellard
/* disable cpu_get_ticks() : the clock is stopped. You must not call
837 34865134 bellard
   cpu_get_ticks() after that.  */
838 34865134 bellard
void cpu_disable_ticks(void)
839 34865134 bellard
{
840 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
841 8a7ddc38 bellard
        cpu_ticks_offset = cpu_get_ticks();
842 1dce7c3c bellard
        cpu_clock_offset = cpu_get_clock();
843 8a7ddc38 bellard
        cpu_ticks_enabled = 0;
844 8a7ddc38 bellard
    }
845 34865134 bellard
}
846 34865134 bellard
847 1dce7c3c bellard
/***********************************************************/
848 1dce7c3c bellard
/* timers */
849 5fafdf24 ths
850 8a7ddc38 bellard
#define QEMU_TIMER_REALTIME 0
851 8a7ddc38 bellard
#define QEMU_TIMER_VIRTUAL  1
852 8a7ddc38 bellard
853 8a7ddc38 bellard
struct QEMUClock {
854 8a7ddc38 bellard
    int type;
855 8a7ddc38 bellard
    /* XXX: add frequency */
856 8a7ddc38 bellard
};
857 8a7ddc38 bellard
858 8a7ddc38 bellard
struct QEMUTimer {
859 8a7ddc38 bellard
    QEMUClock *clock;
860 8a7ddc38 bellard
    int64_t expire_time;
861 8a7ddc38 bellard
    QEMUTimerCB *cb;
862 8a7ddc38 bellard
    void *opaque;
863 8a7ddc38 bellard
    struct QEMUTimer *next;
864 8a7ddc38 bellard
};
865 8a7ddc38 bellard
866 c8994013 ths
struct qemu_alarm_timer {
867 c8994013 ths
    char const *name;
868 efe75411 ths
    unsigned int flags;
869 c8994013 ths
870 c8994013 ths
    int (*start)(struct qemu_alarm_timer *t);
871 c8994013 ths
    void (*stop)(struct qemu_alarm_timer *t);
872 efe75411 ths
    void (*rearm)(struct qemu_alarm_timer *t);
873 c8994013 ths
    void *priv;
874 c8994013 ths
};
875 c8994013 ths
876 efe75411 ths
#define ALARM_FLAG_DYNTICKS  0x1
877 d5d08334 balrog
#define ALARM_FLAG_EXPIRED   0x2
878 efe75411 ths
879 efe75411 ths
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
880 efe75411 ths
{
881 efe75411 ths
    return t->flags & ALARM_FLAG_DYNTICKS;
882 efe75411 ths
}
883 efe75411 ths
884 efe75411 ths
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
885 efe75411 ths
{
886 efe75411 ths
    if (!alarm_has_dynticks(t))
887 efe75411 ths
        return;
888 efe75411 ths
889 efe75411 ths
    t->rearm(t);
890 efe75411 ths
}
891 efe75411 ths
892 efe75411 ths
/* TODO: MIN_TIMER_REARM_US should be optimized */
893 efe75411 ths
#define MIN_TIMER_REARM_US 250
894 efe75411 ths
895 c8994013 ths
static struct qemu_alarm_timer *alarm_timer;
896 f49e58dc aliguori
#ifndef _WIN32
897 c96f1a48 aliguori
static int alarm_timer_rfd, alarm_timer_wfd;
898 f49e58dc aliguori
#endif
899 8a7ddc38 bellard
900 40c3bac3 bellard
#ifdef _WIN32
901 c8994013 ths
902 c8994013 ths
struct qemu_alarm_win32 {
903 c8994013 ths
    MMRESULT timerId;
904 c8994013 ths
    HANDLE host_alarm;
905 c8994013 ths
    unsigned int period;
906 c8994013 ths
} alarm_win32_data = {0, NULL, -1};
907 c8994013 ths
908 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t);
909 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t);
910 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t);
911 c8994013 ths
912 40c3bac3 bellard
#else
913 c8994013 ths
914 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t);
915 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t);
916 c8994013 ths
917 231c6586 ths
#ifdef __linux__
918 231c6586 ths
919 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t);
920 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
921 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
922 efe75411 ths
923 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t);
924 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t);
925 c40ec5a9 ths
926 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t);
927 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t);
928 c8994013 ths
929 efe75411 ths
#endif /* __linux__ */
930 8a7ddc38 bellard
931 c8994013 ths
#endif /* _WIN32 */
932 c8994013 ths
933 2e70f6ef pbrook
/* Correlation between real and virtual time is always going to be
934 bf20dc07 ths
   fairly approximate, so ignore small variation.
935 2e70f6ef pbrook
   When the guest is idle real and virtual time will be aligned in
936 2e70f6ef pbrook
   the IO wait loop.  */
937 2e70f6ef pbrook
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
938 2e70f6ef pbrook
939 2e70f6ef pbrook
static void icount_adjust(void)
940 2e70f6ef pbrook
{
941 2e70f6ef pbrook
    int64_t cur_time;
942 2e70f6ef pbrook
    int64_t cur_icount;
943 2e70f6ef pbrook
    int64_t delta;
944 2e70f6ef pbrook
    static int64_t last_delta;
945 2e70f6ef pbrook
    /* If the VM is not running, then do nothing.  */
946 2e70f6ef pbrook
    if (!vm_running)
947 2e70f6ef pbrook
        return;
948 2e70f6ef pbrook
949 2e70f6ef pbrook
    cur_time = cpu_get_clock();
950 2e70f6ef pbrook
    cur_icount = qemu_get_clock(vm_clock);
951 2e70f6ef pbrook
    delta = cur_icount - cur_time;
952 2e70f6ef pbrook
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
953 2e70f6ef pbrook
    if (delta > 0
954 2e70f6ef pbrook
        && last_delta + ICOUNT_WOBBLE < delta * 2
955 2e70f6ef pbrook
        && icount_time_shift > 0) {
956 2e70f6ef pbrook
        /* The guest is getting too far ahead.  Slow time down.  */
957 2e70f6ef pbrook
        icount_time_shift--;
958 2e70f6ef pbrook
    }
959 2e70f6ef pbrook
    if (delta < 0
960 2e70f6ef pbrook
        && last_delta - ICOUNT_WOBBLE > delta * 2
961 2e70f6ef pbrook
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
962 2e70f6ef pbrook
        /* The guest is getting too far behind.  Speed time up.  */
963 2e70f6ef pbrook
        icount_time_shift++;
964 2e70f6ef pbrook
    }
965 2e70f6ef pbrook
    last_delta = delta;
966 2e70f6ef pbrook
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
967 2e70f6ef pbrook
}
968 2e70f6ef pbrook
969 2e70f6ef pbrook
static void icount_adjust_rt(void * opaque)
970 2e70f6ef pbrook
{
971 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
972 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
973 2e70f6ef pbrook
    icount_adjust();
974 2e70f6ef pbrook
}
975 2e70f6ef pbrook
976 2e70f6ef pbrook
static void icount_adjust_vm(void * opaque)
977 2e70f6ef pbrook
{
978 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
979 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
980 2e70f6ef pbrook
    icount_adjust();
981 2e70f6ef pbrook
}
982 2e70f6ef pbrook
983 2e70f6ef pbrook
static void init_icount_adjust(void)
984 2e70f6ef pbrook
{
985 2e70f6ef pbrook
    /* Have both realtime and virtual time triggers for speed adjustment.
986 2e70f6ef pbrook
       The realtime trigger catches emulated time passing too slowly,
987 2e70f6ef pbrook
       the virtual time trigger catches emulated time passing too fast.
988 2e70f6ef pbrook
       Realtime triggers occur even when idle, so use them less frequently
989 2e70f6ef pbrook
       than VM triggers.  */
990 2e70f6ef pbrook
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
991 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
992 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
993 2e70f6ef pbrook
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
994 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
995 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
996 2e70f6ef pbrook
}
997 2e70f6ef pbrook
998 c8994013 ths
static struct qemu_alarm_timer alarm_timers[] = {
999 efe75411 ths
#ifndef _WIN32
1000 231c6586 ths
#ifdef __linux__
1001 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
1002 efe75411 ths
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
1003 c40ec5a9 ths
    /* HPET - if available - is preferred */
1004 efe75411 ths
    {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
1005 c40ec5a9 ths
    /* ...otherwise try RTC */
1006 efe75411 ths
    {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
1007 c8994013 ths
#endif
1008 efe75411 ths
    {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
1009 c8994013 ths
#else
1010 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1011 efe75411 ths
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1012 efe75411 ths
    {"win32", 0, win32_start_timer,
1013 efe75411 ths
     win32_stop_timer, NULL, &alarm_win32_data},
1014 c8994013 ths
#endif
1015 c8994013 ths
    {NULL, }
1016 c8994013 ths
};
1017 c8994013 ths
1018 3f47aa8c blueswir1
static void show_available_alarms(void)
1019 f3dcfada ths
{
1020 f3dcfada ths
    int i;
1021 f3dcfada ths
1022 f3dcfada ths
    printf("Available alarm timers, in order of precedence:\n");
1023 f3dcfada ths
    for (i = 0; alarm_timers[i].name; i++)
1024 f3dcfada ths
        printf("%s\n", alarm_timers[i].name);
1025 f3dcfada ths
}
1026 f3dcfada ths
1027 f3dcfada ths
static void configure_alarms(char const *opt)
1028 f3dcfada ths
{
1029 f3dcfada ths
    int i;
1030 f3dcfada ths
    int cur = 0;
1031 f3dcfada ths
    int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1032 f3dcfada ths
    char *arg;
1033 f3dcfada ths
    char *name;
1034 2e70f6ef pbrook
    struct qemu_alarm_timer tmp;
1035 f3dcfada ths
1036 3adda04c aurel32
    if (!strcmp(opt, "?")) {
1037 f3dcfada ths
        show_available_alarms();
1038 f3dcfada ths
        exit(0);
1039 f3dcfada ths
    }
1040 f3dcfada ths
1041 f3dcfada ths
    arg = strdup(opt);
1042 f3dcfada ths
1043 f3dcfada ths
    /* Reorder the array */
1044 f3dcfada ths
    name = strtok(arg, ",");
1045 f3dcfada ths
    while (name) {
1046 e2b577e5 balrog
        for (i = 0; i < count && alarm_timers[i].name; i++) {
1047 f3dcfada ths
            if (!strcmp(alarm_timers[i].name, name))
1048 f3dcfada ths
                break;
1049 f3dcfada ths
        }
1050 f3dcfada ths
1051 f3dcfada ths
        if (i == count) {
1052 f3dcfada ths
            fprintf(stderr, "Unknown clock %s\n", name);
1053 f3dcfada ths
            goto next;
1054 f3dcfada ths
        }
1055 f3dcfada ths
1056 f3dcfada ths
        if (i < cur)
1057 f3dcfada ths
            /* Ignore */
1058 f3dcfada ths
            goto next;
1059 f3dcfada ths
1060 f3dcfada ths
        /* Swap */
1061 f3dcfada ths
        tmp = alarm_timers[i];
1062 f3dcfada ths
        alarm_timers[i] = alarm_timers[cur];
1063 f3dcfada ths
        alarm_timers[cur] = tmp;
1064 f3dcfada ths
1065 f3dcfada ths
        cur++;
1066 f3dcfada ths
next:
1067 f3dcfada ths
        name = strtok(NULL, ",");
1068 f3dcfada ths
    }
1069 f3dcfada ths
1070 f3dcfada ths
    free(arg);
1071 f3dcfada ths
1072 f3dcfada ths
    if (cur) {
1073 2e70f6ef pbrook
        /* Disable remaining timers */
1074 f3dcfada ths
        for (i = cur; i < count; i++)
1075 f3dcfada ths
            alarm_timers[i].name = NULL;
1076 3adda04c aurel32
    } else {
1077 3adda04c aurel32
        show_available_alarms();
1078 3adda04c aurel32
        exit(1);
1079 f3dcfada ths
    }
1080 f3dcfada ths
}
1081 f3dcfada ths
1082 c8994013 ths
QEMUClock *rt_clock;
1083 c8994013 ths
QEMUClock *vm_clock;
1084 c8994013 ths
1085 c8994013 ths
static QEMUTimer *active_timers[2];
1086 c8994013 ths
1087 9596ebb7 pbrook
static QEMUClock *qemu_new_clock(int type)
1088 8a7ddc38 bellard
{
1089 8a7ddc38 bellard
    QEMUClock *clock;
1090 8a7ddc38 bellard
    clock = qemu_mallocz(sizeof(QEMUClock));
1091 8a7ddc38 bellard
    if (!clock)
1092 8a7ddc38 bellard
        return NULL;
1093 8a7ddc38 bellard
    clock->type = type;
1094 8a7ddc38 bellard
    return clock;
1095 8a7ddc38 bellard
}
1096 8a7ddc38 bellard
1097 8a7ddc38 bellard
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1098 8a7ddc38 bellard
{
1099 8a7ddc38 bellard
    QEMUTimer *ts;
1100 8a7ddc38 bellard
1101 8a7ddc38 bellard
    ts = qemu_mallocz(sizeof(QEMUTimer));
1102 8a7ddc38 bellard
    ts->clock = clock;
1103 8a7ddc38 bellard
    ts->cb = cb;
1104 8a7ddc38 bellard
    ts->opaque = opaque;
1105 8a7ddc38 bellard
    return ts;
1106 8a7ddc38 bellard
}
1107 8a7ddc38 bellard
1108 8a7ddc38 bellard
void qemu_free_timer(QEMUTimer *ts)
1109 8a7ddc38 bellard
{
1110 8a7ddc38 bellard
    qemu_free(ts);
1111 8a7ddc38 bellard
}
1112 8a7ddc38 bellard
1113 8a7ddc38 bellard
/* stop a timer, but do not dealloc it */
1114 8a7ddc38 bellard
void qemu_del_timer(QEMUTimer *ts)
1115 8a7ddc38 bellard
{
1116 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1117 8a7ddc38 bellard
1118 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1119 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1120 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1121 8a7ddc38 bellard
    for(;;) {
1122 8a7ddc38 bellard
        t = *pt;
1123 8a7ddc38 bellard
        if (!t)
1124 8a7ddc38 bellard
            break;
1125 8a7ddc38 bellard
        if (t == ts) {
1126 8a7ddc38 bellard
            *pt = t->next;
1127 8a7ddc38 bellard
            break;
1128 8a7ddc38 bellard
        }
1129 8a7ddc38 bellard
        pt = &t->next;
1130 8a7ddc38 bellard
    }
1131 8a7ddc38 bellard
}
1132 8a7ddc38 bellard
1133 8a7ddc38 bellard
/* modify the current timer so that it will be fired when current_time
1134 8a7ddc38 bellard
   >= expire_time. The corresponding callback will be called. */
1135 8a7ddc38 bellard
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1136 8a7ddc38 bellard
{
1137 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1138 8a7ddc38 bellard
1139 8a7ddc38 bellard
    qemu_del_timer(ts);
1140 8a7ddc38 bellard
1141 8a7ddc38 bellard
    /* add the timer in the sorted list */
1142 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1143 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1144 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1145 8a7ddc38 bellard
    for(;;) {
1146 8a7ddc38 bellard
        t = *pt;
1147 8a7ddc38 bellard
        if (!t)
1148 8a7ddc38 bellard
            break;
1149 5fafdf24 ths
        if (t->expire_time > expire_time)
1150 8a7ddc38 bellard
            break;
1151 8a7ddc38 bellard
        pt = &t->next;
1152 8a7ddc38 bellard
    }
1153 8a7ddc38 bellard
    ts->expire_time = expire_time;
1154 8a7ddc38 bellard
    ts->next = *pt;
1155 8a7ddc38 bellard
    *pt = ts;
1156 d5d08334 balrog
1157 d5d08334 balrog
    /* Rearm if necessary  */
1158 2e70f6ef pbrook
    if (pt == &active_timers[ts->clock->type]) {
1159 2e70f6ef pbrook
        if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1160 2e70f6ef pbrook
            qemu_rearm_alarm_timer(alarm_timer);
1161 2e70f6ef pbrook
        }
1162 2e70f6ef pbrook
        /* Interrupt execution to force deadline recalculation.  */
1163 2e70f6ef pbrook
        if (use_icount && cpu_single_env) {
1164 2e70f6ef pbrook
            cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1165 2e70f6ef pbrook
        }
1166 2e70f6ef pbrook
    }
1167 8a7ddc38 bellard
}
1168 8a7ddc38 bellard
1169 8a7ddc38 bellard
int qemu_timer_pending(QEMUTimer *ts)
1170 8a7ddc38 bellard
{
1171 8a7ddc38 bellard
    QEMUTimer *t;
1172 8a7ddc38 bellard
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1173 8a7ddc38 bellard
        if (t == ts)
1174 8a7ddc38 bellard
            return 1;
1175 8a7ddc38 bellard
    }
1176 8a7ddc38 bellard
    return 0;
1177 8a7ddc38 bellard
}
1178 8a7ddc38 bellard
1179 8a7ddc38 bellard
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1180 8a7ddc38 bellard
{
1181 8a7ddc38 bellard
    if (!timer_head)
1182 8a7ddc38 bellard
        return 0;
1183 8a7ddc38 bellard
    return (timer_head->expire_time <= current_time);
1184 8a7ddc38 bellard
}
1185 8a7ddc38 bellard
1186 8a7ddc38 bellard
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1187 8a7ddc38 bellard
{
1188 8a7ddc38 bellard
    QEMUTimer *ts;
1189 3b46e624 ths
1190 8a7ddc38 bellard
    for(;;) {
1191 8a7ddc38 bellard
        ts = *ptimer_head;
1192 e95c8d51 bellard
        if (!ts || ts->expire_time > current_time)
1193 8a7ddc38 bellard
            break;
1194 8a7ddc38 bellard
        /* remove timer from the list before calling the callback */
1195 8a7ddc38 bellard
        *ptimer_head = ts->next;
1196 8a7ddc38 bellard
        ts->next = NULL;
1197 3b46e624 ths
1198 8a7ddc38 bellard
        /* run the callback (the timer list can be modified) */
1199 8a7ddc38 bellard
        ts->cb(ts->opaque);
1200 8a7ddc38 bellard
    }
1201 8a7ddc38 bellard
}
1202 8a7ddc38 bellard
1203 8a7ddc38 bellard
int64_t qemu_get_clock(QEMUClock *clock)
1204 8a7ddc38 bellard
{
1205 8a7ddc38 bellard
    switch(clock->type) {
1206 8a7ddc38 bellard
    case QEMU_TIMER_REALTIME:
1207 1dce7c3c bellard
        return get_clock() / 1000000;
1208 8a7ddc38 bellard
    default:
1209 8a7ddc38 bellard
    case QEMU_TIMER_VIRTUAL:
1210 2e70f6ef pbrook
        if (use_icount) {
1211 2e70f6ef pbrook
            return cpu_get_icount();
1212 2e70f6ef pbrook
        } else {
1213 2e70f6ef pbrook
            return cpu_get_clock();
1214 2e70f6ef pbrook
        }
1215 8a7ddc38 bellard
    }
1216 8a7ddc38 bellard
}
1217 8a7ddc38 bellard
1218 1dce7c3c bellard
static void init_timers(void)
1219 1dce7c3c bellard
{
1220 1dce7c3c bellard
    init_get_clock();
1221 1dce7c3c bellard
    ticks_per_sec = QEMU_TIMER_BASE;
1222 1dce7c3c bellard
    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1223 1dce7c3c bellard
    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1224 1dce7c3c bellard
}
1225 1dce7c3c bellard
1226 8a7ddc38 bellard
/* save a timer */
1227 8a7ddc38 bellard
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1228 8a7ddc38 bellard
{
1229 8a7ddc38 bellard
    uint64_t expire_time;
1230 8a7ddc38 bellard
1231 8a7ddc38 bellard
    if (qemu_timer_pending(ts)) {
1232 8a7ddc38 bellard
        expire_time = ts->expire_time;
1233 8a7ddc38 bellard
    } else {
1234 8a7ddc38 bellard
        expire_time = -1;
1235 8a7ddc38 bellard
    }
1236 8a7ddc38 bellard
    qemu_put_be64(f, expire_time);
1237 8a7ddc38 bellard
}
1238 8a7ddc38 bellard
1239 8a7ddc38 bellard
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1240 8a7ddc38 bellard
{
1241 8a7ddc38 bellard
    uint64_t expire_time;
1242 8a7ddc38 bellard
1243 8a7ddc38 bellard
    expire_time = qemu_get_be64(f);
1244 8a7ddc38 bellard
    if (expire_time != -1) {
1245 8a7ddc38 bellard
        qemu_mod_timer(ts, expire_time);
1246 8a7ddc38 bellard
    } else {
1247 8a7ddc38 bellard
        qemu_del_timer(ts);
1248 8a7ddc38 bellard
    }
1249 8a7ddc38 bellard
}
1250 8a7ddc38 bellard
1251 8a7ddc38 bellard
static void timer_save(QEMUFile *f, void *opaque)
1252 8a7ddc38 bellard
{
1253 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1254 8a7ddc38 bellard
        hw_error("cannot save state if virtual timers are running");
1255 8a7ddc38 bellard
    }
1256 bee8d684 ths
    qemu_put_be64(f, cpu_ticks_offset);
1257 bee8d684 ths
    qemu_put_be64(f, ticks_per_sec);
1258 bee8d684 ths
    qemu_put_be64(f, cpu_clock_offset);
1259 8a7ddc38 bellard
}
1260 8a7ddc38 bellard
1261 8a7ddc38 bellard
static int timer_load(QEMUFile *f, void *opaque, int version_id)
1262 8a7ddc38 bellard
{
1263 c88676f8 bellard
    if (version_id != 1 && version_id != 2)
1264 8a7ddc38 bellard
        return -EINVAL;
1265 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1266 8a7ddc38 bellard
        return -EINVAL;
1267 8a7ddc38 bellard
    }
1268 bee8d684 ths
    cpu_ticks_offset=qemu_get_be64(f);
1269 bee8d684 ths
    ticks_per_sec=qemu_get_be64(f);
1270 c88676f8 bellard
    if (version_id == 2) {
1271 bee8d684 ths
        cpu_clock_offset=qemu_get_be64(f);
1272 c88676f8 bellard
    }
1273 8a7ddc38 bellard
    return 0;
1274 8a7ddc38 bellard
}
1275 8a7ddc38 bellard
1276 67b915a5 bellard
#ifdef _WIN32
1277 5fafdf24 ths
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1278 67b915a5 bellard
                                 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1279 67b915a5 bellard
#else
1280 8a7ddc38 bellard
static void host_alarm_handler(int host_signum)
1281 67b915a5 bellard
#endif
1282 8a7ddc38 bellard
{
1283 02ba45c5 bellard
#if 0
1284 02ba45c5 bellard
#define DISP_FREQ 1000
1285 02ba45c5 bellard
    {
1286 02ba45c5 bellard
        static int64_t delta_min = INT64_MAX;
1287 02ba45c5 bellard
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
1288 02ba45c5 bellard
        static int count;
1289 02ba45c5 bellard
        ti = qemu_get_clock(vm_clock);
1290 02ba45c5 bellard
        if (last_clock != 0) {
1291 02ba45c5 bellard
            delta = ti - last_clock;
1292 02ba45c5 bellard
            if (delta < delta_min)
1293 02ba45c5 bellard
                delta_min = delta;
1294 02ba45c5 bellard
            if (delta > delta_max)
1295 02ba45c5 bellard
                delta_max = delta;
1296 02ba45c5 bellard
            delta_cum += delta;
1297 02ba45c5 bellard
            if (++count == DISP_FREQ) {
1298 26a76461 bellard
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1299 02ba45c5 bellard
                       muldiv64(delta_min, 1000000, ticks_per_sec),
1300 02ba45c5 bellard
                       muldiv64(delta_max, 1000000, ticks_per_sec),
1301 02ba45c5 bellard
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1302 02ba45c5 bellard
                       (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1303 02ba45c5 bellard
                count = 0;
1304 02ba45c5 bellard
                delta_min = INT64_MAX;
1305 02ba45c5 bellard
                delta_max = 0;
1306 02ba45c5 bellard
                delta_cum = 0;
1307 02ba45c5 bellard
            }
1308 02ba45c5 bellard
        }
1309 02ba45c5 bellard
        last_clock = ti;
1310 02ba45c5 bellard
    }
1311 02ba45c5 bellard
#endif
1312 efe75411 ths
    if (alarm_has_dynticks(alarm_timer) ||
1313 2e70f6ef pbrook
        (!use_icount &&
1314 2e70f6ef pbrook
            qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1315 2e70f6ef pbrook
                               qemu_get_clock(vm_clock))) ||
1316 8a7ddc38 bellard
        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1317 8a7ddc38 bellard
                           qemu_get_clock(rt_clock))) {
1318 c96f1a48 aliguori
        CPUState *env = next_cpu;
1319 c96f1a48 aliguori
1320 06d9f2f7 bellard
#ifdef _WIN32
1321 c8994013 ths
        struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1322 c8994013 ths
        SetEvent(data->host_alarm);
1323 f49e58dc aliguori
#else
1324 f49e58dc aliguori
        static const char byte = 0;
1325 c96f1a48 aliguori
        write(alarm_timer_wfd, &byte, sizeof(byte));
1326 f49e58dc aliguori
#endif
1327 d5d08334 balrog
        alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1328 d5d08334 balrog
1329 4f8eb8da balrog
        if (env) {
1330 4f8eb8da balrog
            /* stop the currently executing cpu because a timer occured */
1331 4f8eb8da balrog
            cpu_interrupt(env, CPU_INTERRUPT_EXIT);
1332 a332e112 bellard
#ifdef USE_KQEMU
1333 4f8eb8da balrog
            if (env->kqemu_enabled) {
1334 4f8eb8da balrog
                kqemu_cpu_interrupt(env);
1335 4f8eb8da balrog
            }
1336 ee5605e5 balrog
#endif
1337 4f8eb8da balrog
        }
1338 ee5605e5 balrog
        event_pending = 1;
1339 8a7ddc38 bellard
    }
1340 8a7ddc38 bellard
}
1341 8a7ddc38 bellard
1342 2e70f6ef pbrook
static int64_t qemu_next_deadline(void)
1343 efe75411 ths
{
1344 2e70f6ef pbrook
    int64_t delta;
1345 efe75411 ths
1346 efe75411 ths
    if (active_timers[QEMU_TIMER_VIRTUAL]) {
1347 2e70f6ef pbrook
        delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1348 2e70f6ef pbrook
                     qemu_get_clock(vm_clock);
1349 2e70f6ef pbrook
    } else {
1350 2e70f6ef pbrook
        /* To avoid problems with overflow limit this to 2^32.  */
1351 2e70f6ef pbrook
        delta = INT32_MAX;
1352 efe75411 ths
    }
1353 efe75411 ths
1354 2e70f6ef pbrook
    if (delta < 0)
1355 2e70f6ef pbrook
        delta = 0;
1356 efe75411 ths
1357 2e70f6ef pbrook
    return delta;
1358 2e70f6ef pbrook
}
1359 2e70f6ef pbrook
1360 8632fb9a blueswir1
#if defined(__linux__) || defined(_WIN32)
1361 2e70f6ef pbrook
static uint64_t qemu_next_deadline_dyntick(void)
1362 2e70f6ef pbrook
{
1363 2e70f6ef pbrook
    int64_t delta;
1364 2e70f6ef pbrook
    int64_t rtdelta;
1365 2e70f6ef pbrook
1366 2e70f6ef pbrook
    if (use_icount)
1367 2e70f6ef pbrook
        delta = INT32_MAX;
1368 2e70f6ef pbrook
    else
1369 2e70f6ef pbrook
        delta = (qemu_next_deadline() + 999) / 1000;
1370 2e70f6ef pbrook
1371 2e70f6ef pbrook
    if (active_timers[QEMU_TIMER_REALTIME]) {
1372 2e70f6ef pbrook
        rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1373 2e70f6ef pbrook
                 qemu_get_clock(rt_clock))*1000;
1374 2e70f6ef pbrook
        if (rtdelta < delta)
1375 2e70f6ef pbrook
            delta = rtdelta;
1376 2e70f6ef pbrook
    }
1377 2e70f6ef pbrook
1378 2e70f6ef pbrook
    if (delta < MIN_TIMER_REARM_US)
1379 2e70f6ef pbrook
        delta = MIN_TIMER_REARM_US;
1380 2e70f6ef pbrook
1381 2e70f6ef pbrook
    return delta;
1382 efe75411 ths
}
1383 8632fb9a blueswir1
#endif
1384 efe75411 ths
1385 fd872598 bellard
#ifndef _WIN32
1386 fd872598 bellard
1387 7183b4b4 aliguori
/* Sets a specific flag */
1388 7183b4b4 aliguori
static int fcntl_setfl(int fd, int flag)
1389 7183b4b4 aliguori
{
1390 7183b4b4 aliguori
    int flags;
1391 7183b4b4 aliguori
1392 7183b4b4 aliguori
    flags = fcntl(fd, F_GETFL);
1393 7183b4b4 aliguori
    if (flags == -1)
1394 7183b4b4 aliguori
        return -errno;
1395 7183b4b4 aliguori
1396 7183b4b4 aliguori
    if (fcntl(fd, F_SETFL, flags | flag) == -1)
1397 7183b4b4 aliguori
        return -errno;
1398 7183b4b4 aliguori
1399 7183b4b4 aliguori
    return 0;
1400 7183b4b4 aliguori
}
1401 7183b4b4 aliguori
1402 829309c7 bellard
#if defined(__linux__)
1403 829309c7 bellard
1404 fd872598 bellard
#define RTC_FREQ 1024
1405 fd872598 bellard
1406 3587f82a aurel32
void enable_sigio_timer(int fd)
1407 c8994013 ths
{
1408 c8994013 ths
    struct sigaction act;
1409 c8994013 ths
1410 c8994013 ths
    /* timer signal */
1411 c8994013 ths
    sigfillset(&act.sa_mask);
1412 c8994013 ths
    act.sa_flags = 0;
1413 c8994013 ths
    act.sa_handler = host_alarm_handler;
1414 c8994013 ths
1415 c8994013 ths
    sigaction(SIGIO, &act, NULL);
1416 7183b4b4 aliguori
    fcntl_setfl(fd, O_ASYNC);
1417 c8994013 ths
    fcntl(fd, F_SETOWN, getpid());
1418 c8994013 ths
}
1419 829309c7 bellard
1420 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t)
1421 c40ec5a9 ths
{
1422 c40ec5a9 ths
    struct hpet_info info;
1423 c40ec5a9 ths
    int r, fd;
1424 c40ec5a9 ths
1425 c40ec5a9 ths
    fd = open("/dev/hpet", O_RDONLY);
1426 c40ec5a9 ths
    if (fd < 0)
1427 c40ec5a9 ths
        return -1;
1428 c40ec5a9 ths
1429 c40ec5a9 ths
    /* Set frequency */
1430 c40ec5a9 ths
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1431 c40ec5a9 ths
    if (r < 0) {
1432 c40ec5a9 ths
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1433 c40ec5a9 ths
                "error, but for better emulation accuracy type:\n"
1434 c40ec5a9 ths
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1435 c40ec5a9 ths
        goto fail;
1436 c40ec5a9 ths
    }
1437 c40ec5a9 ths
1438 c40ec5a9 ths
    /* Check capabilities */
1439 c40ec5a9 ths
    r = ioctl(fd, HPET_INFO, &info);
1440 c40ec5a9 ths
    if (r < 0)
1441 c40ec5a9 ths
        goto fail;
1442 c40ec5a9 ths
1443 c40ec5a9 ths
    /* Enable periodic mode */
1444 c40ec5a9 ths
    r = ioctl(fd, HPET_EPI, 0);
1445 c40ec5a9 ths
    if (info.hi_flags && (r < 0))
1446 c40ec5a9 ths
        goto fail;
1447 c40ec5a9 ths
1448 c40ec5a9 ths
    /* Enable interrupt */
1449 c40ec5a9 ths
    r = ioctl(fd, HPET_IE_ON, 0);
1450 c40ec5a9 ths
    if (r < 0)
1451 c40ec5a9 ths
        goto fail;
1452 c40ec5a9 ths
1453 c40ec5a9 ths
    enable_sigio_timer(fd);
1454 fcdc2129 pbrook
    t->priv = (void *)(long)fd;
1455 c40ec5a9 ths
1456 c40ec5a9 ths
    return 0;
1457 c40ec5a9 ths
fail:
1458 c40ec5a9 ths
    close(fd);
1459 c40ec5a9 ths
    return -1;
1460 c40ec5a9 ths
}
1461 c40ec5a9 ths
1462 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t)
1463 c40ec5a9 ths
{
1464 fcdc2129 pbrook
    int fd = (long)t->priv;
1465 c40ec5a9 ths
1466 c40ec5a9 ths
    close(fd);
1467 c40ec5a9 ths
}
1468 c40ec5a9 ths
1469 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t)
1470 fd872598 bellard
{
1471 c8994013 ths
    int rtc_fd;
1472 b5a23ad4 balrog
    unsigned long current_rtc_freq = 0;
1473 c8994013 ths
1474 aeb30be6 balrog
    TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1475 fd872598 bellard
    if (rtc_fd < 0)
1476 fd872598 bellard
        return -1;
1477 b5a23ad4 balrog
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1478 b5a23ad4 balrog
    if (current_rtc_freq != RTC_FREQ &&
1479 b5a23ad4 balrog
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1480 fd872598 bellard
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1481 fd872598 bellard
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1482 fd872598 bellard
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1483 fd872598 bellard
        goto fail;
1484 fd872598 bellard
    }
1485 fd872598 bellard
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1486 fd872598 bellard
    fail:
1487 fd872598 bellard
        close(rtc_fd);
1488 fd872598 bellard
        return -1;
1489 fd872598 bellard
    }
1490 c8994013 ths
1491 c8994013 ths
    enable_sigio_timer(rtc_fd);
1492 c8994013 ths
1493 fcdc2129 pbrook
    t->priv = (void *)(long)rtc_fd;
1494 c8994013 ths
1495 fd872598 bellard
    return 0;
1496 fd872598 bellard
}
1497 fd872598 bellard
1498 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t)
1499 829309c7 bellard
{
1500 fcdc2129 pbrook
    int rtc_fd = (long)t->priv;
1501 c8994013 ths
1502 c8994013 ths
    close(rtc_fd);
1503 829309c7 bellard
}
1504 829309c7 bellard
1505 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t)
1506 efe75411 ths
{
1507 efe75411 ths
    struct sigevent ev;
1508 efe75411 ths
    timer_t host_timer;
1509 efe75411 ths
    struct sigaction act;
1510 efe75411 ths
1511 efe75411 ths
    sigfillset(&act.sa_mask);
1512 efe75411 ths
    act.sa_flags = 0;
1513 efe75411 ths
    act.sa_handler = host_alarm_handler;
1514 efe75411 ths
1515 efe75411 ths
    sigaction(SIGALRM, &act, NULL);
1516 efe75411 ths
1517 efe75411 ths
    ev.sigev_value.sival_int = 0;
1518 efe75411 ths
    ev.sigev_notify = SIGEV_SIGNAL;
1519 efe75411 ths
    ev.sigev_signo = SIGALRM;
1520 efe75411 ths
1521 efe75411 ths
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1522 efe75411 ths
        perror("timer_create");
1523 efe75411 ths
1524 efe75411 ths
        /* disable dynticks */
1525 efe75411 ths
        fprintf(stderr, "Dynamic Ticks disabled\n");
1526 efe75411 ths
1527 efe75411 ths
        return -1;
1528 efe75411 ths
    }
1529 efe75411 ths
1530 efe75411 ths
    t->priv = (void *)host_timer;
1531 efe75411 ths
1532 efe75411 ths
    return 0;
1533 efe75411 ths
}
1534 efe75411 ths
1535 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1536 efe75411 ths
{
1537 efe75411 ths
    timer_t host_timer = (timer_t)t->priv;
1538 efe75411 ths
1539 efe75411 ths
    timer_delete(host_timer);
1540 efe75411 ths
}
1541 efe75411 ths
1542 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1543 efe75411 ths
{
1544 efe75411 ths
    timer_t host_timer = (timer_t)t->priv;
1545 efe75411 ths
    struct itimerspec timeout;
1546 efe75411 ths
    int64_t nearest_delta_us = INT64_MAX;
1547 efe75411 ths
    int64_t current_us;
1548 efe75411 ths
1549 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1550 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1551 d5d08334 balrog
        return;
1552 efe75411 ths
1553 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1554 efe75411 ths
1555 efe75411 ths
    /* check whether a timer is already running */
1556 efe75411 ths
    if (timer_gettime(host_timer, &timeout)) {
1557 efe75411 ths
        perror("gettime");
1558 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1559 efe75411 ths
        exit(1);
1560 efe75411 ths
    }
1561 efe75411 ths
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1562 efe75411 ths
    if (current_us && current_us <= nearest_delta_us)
1563 efe75411 ths
        return;
1564 efe75411 ths
1565 efe75411 ths
    timeout.it_interval.tv_sec = 0;
1566 efe75411 ths
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1567 efe75411 ths
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1568 efe75411 ths
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1569 efe75411 ths
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1570 efe75411 ths
        perror("settime");
1571 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1572 efe75411 ths
        exit(1);
1573 efe75411 ths
    }
1574 efe75411 ths
}
1575 efe75411 ths
1576 70744b3a ths
#endif /* defined(__linux__) */
1577 231c6586 ths
1578 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t)
1579 c8994013 ths
{
1580 c8994013 ths
    struct sigaction act;
1581 c8994013 ths
    struct itimerval itv;
1582 c8994013 ths
    int err;
1583 c8994013 ths
1584 c8994013 ths
    /* timer signal */
1585 c8994013 ths
    sigfillset(&act.sa_mask);
1586 c8994013 ths
    act.sa_flags = 0;
1587 c8994013 ths
    act.sa_handler = host_alarm_handler;
1588 c8994013 ths
1589 c8994013 ths
    sigaction(SIGALRM, &act, NULL);
1590 c8994013 ths
1591 c8994013 ths
    itv.it_interval.tv_sec = 0;
1592 c8994013 ths
    /* for i386 kernel 2.6 to get 1 ms */
1593 c8994013 ths
    itv.it_interval.tv_usec = 999;
1594 c8994013 ths
    itv.it_value.tv_sec = 0;
1595 c8994013 ths
    itv.it_value.tv_usec = 10 * 1000;
1596 c8994013 ths
1597 c8994013 ths
    err = setitimer(ITIMER_REAL, &itv, NULL);
1598 c8994013 ths
    if (err)
1599 c8994013 ths
        return -1;
1600 c8994013 ths
1601 c8994013 ths
    return 0;
1602 c8994013 ths
}
1603 c8994013 ths
1604 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t)
1605 c8994013 ths
{
1606 c8994013 ths
    struct itimerval itv;
1607 c8994013 ths
1608 c8994013 ths
    memset(&itv, 0, sizeof(itv));
1609 c8994013 ths
    setitimer(ITIMER_REAL, &itv, NULL);
1610 c8994013 ths
}
1611 c8994013 ths
1612 829309c7 bellard
#endif /* !defined(_WIN32) */
1613 fd872598 bellard
1614 f49e58dc aliguori
static void try_to_rearm_timer(void *opaque)
1615 f49e58dc aliguori
{
1616 f49e58dc aliguori
    struct qemu_alarm_timer *t = opaque;
1617 f49e58dc aliguori
#ifndef _WIN32
1618 f49e58dc aliguori
    ssize_t len;
1619 f49e58dc aliguori
1620 f49e58dc aliguori
    /* Drain the notify pipe */
1621 f49e58dc aliguori
    do {
1622 f49e58dc aliguori
        char buffer[512];
1623 f49e58dc aliguori
        len = read(alarm_timer_rfd, buffer, sizeof(buffer));
1624 f49e58dc aliguori
    } while ((len == -1 && errno == EINTR) || len > 0);
1625 f49e58dc aliguori
#endif
1626 f49e58dc aliguori
1627 f49e58dc aliguori
    /* vm time timers */
1628 f49e58dc aliguori
    if (vm_running && likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
1629 f49e58dc aliguori
        qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1630 f49e58dc aliguori
                        qemu_get_clock(vm_clock));
1631 f49e58dc aliguori
1632 f49e58dc aliguori
    /* real time timers */
1633 f49e58dc aliguori
    qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1634 f49e58dc aliguori
                    qemu_get_clock(rt_clock));
1635 f49e58dc aliguori
1636 f49e58dc aliguori
    if (t->flags & ALARM_FLAG_EXPIRED) {
1637 f49e58dc aliguori
        alarm_timer->flags &= ~ALARM_FLAG_EXPIRED;
1638 f49e58dc aliguori
        qemu_rearm_alarm_timer(alarm_timer);
1639 f49e58dc aliguori
    }
1640 f49e58dc aliguori
}
1641 f49e58dc aliguori
1642 c8994013 ths
#ifdef _WIN32
1643 c8994013 ths
1644 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t)
1645 c8994013 ths
{
1646 c8994013 ths
    TIMECAPS tc;
1647 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1648 efe75411 ths
    UINT flags;
1649 c8994013 ths
1650 c8994013 ths
    data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1651 c8994013 ths
    if (!data->host_alarm) {
1652 c8994013 ths
        perror("Failed CreateEvent");
1653 c396a7f0 ths
        return -1;
1654 c8994013 ths
    }
1655 c8994013 ths
1656 c8994013 ths
    memset(&tc, 0, sizeof(tc));
1657 c8994013 ths
    timeGetDevCaps(&tc, sizeof(tc));
1658 c8994013 ths
1659 c8994013 ths
    if (data->period < tc.wPeriodMin)
1660 c8994013 ths
        data->period = tc.wPeriodMin;
1661 c8994013 ths
1662 c8994013 ths
    timeBeginPeriod(data->period);
1663 c8994013 ths
1664 efe75411 ths
    flags = TIME_CALLBACK_FUNCTION;
1665 efe75411 ths
    if (alarm_has_dynticks(t))
1666 efe75411 ths
        flags |= TIME_ONESHOT;
1667 efe75411 ths
    else
1668 efe75411 ths
        flags |= TIME_PERIODIC;
1669 efe75411 ths
1670 c8994013 ths
    data->timerId = timeSetEvent(1,         // interval (ms)
1671 c8994013 ths
                        data->period,       // resolution
1672 c8994013 ths
                        host_alarm_handler, // function
1673 c8994013 ths
                        (DWORD)t,           // parameter
1674 efe75411 ths
                        flags);
1675 c8994013 ths
1676 c8994013 ths
    if (!data->timerId) {
1677 c8994013 ths
        perror("Failed to initialize win32 alarm timer");
1678 c8994013 ths
1679 c8994013 ths
        timeEndPeriod(data->period);
1680 c8994013 ths
        CloseHandle(data->host_alarm);
1681 c8994013 ths
        return -1;
1682 c8994013 ths
    }
1683 c8994013 ths
1684 f49e58dc aliguori
    qemu_add_wait_object(data->host_alarm, try_to_rearm_timer, t);
1685 c8994013 ths
1686 c8994013 ths
    return 0;
1687 c8994013 ths
}
1688 c8994013 ths
1689 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t)
1690 c8994013 ths
{
1691 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1692 c8994013 ths
1693 c8994013 ths
    timeKillEvent(data->timerId);
1694 c8994013 ths
    timeEndPeriod(data->period);
1695 c8994013 ths
1696 c8994013 ths
    CloseHandle(data->host_alarm);
1697 c8994013 ths
}
1698 c8994013 ths
1699 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1700 efe75411 ths
{
1701 efe75411 ths
    struct qemu_alarm_win32 *data = t->priv;
1702 efe75411 ths
    uint64_t nearest_delta_us;
1703 efe75411 ths
1704 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1705 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1706 d5d08334 balrog
        return;
1707 efe75411 ths
1708 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1709 efe75411 ths
    nearest_delta_us /= 1000;
1710 efe75411 ths
1711 efe75411 ths
    timeKillEvent(data->timerId);
1712 efe75411 ths
1713 efe75411 ths
    data->timerId = timeSetEvent(1,
1714 efe75411 ths
                        data->period,
1715 efe75411 ths
                        host_alarm_handler,
1716 efe75411 ths
                        (DWORD)t,
1717 efe75411 ths
                        TIME_ONESHOT | TIME_PERIODIC);
1718 efe75411 ths
1719 efe75411 ths
    if (!data->timerId) {
1720 efe75411 ths
        perror("Failed to re-arm win32 alarm timer");
1721 efe75411 ths
1722 efe75411 ths
        timeEndPeriod(data->period);
1723 efe75411 ths
        CloseHandle(data->host_alarm);
1724 efe75411 ths
        exit(1);
1725 efe75411 ths
    }
1726 efe75411 ths
}
1727 efe75411 ths
1728 c8994013 ths
#endif /* _WIN32 */
1729 c8994013 ths
1730 7183b4b4 aliguori
static int init_timer_alarm(void)
1731 8a7ddc38 bellard
{
1732 223f0d72 blueswir1
    struct qemu_alarm_timer *t = NULL;
1733 c8994013 ths
    int i, err = -1;
1734 f49e58dc aliguori
1735 f49e58dc aliguori
#ifndef _WIN32
1736 c96f1a48 aliguori
    int fds[2];
1737 c96f1a48 aliguori
1738 7183b4b4 aliguori
    err = pipe(fds);
1739 7183b4b4 aliguori
    if (err == -1)
1740 7183b4b4 aliguori
        return -errno;
1741 7183b4b4 aliguori
1742 7183b4b4 aliguori
    err = fcntl_setfl(fds[0], O_NONBLOCK);
1743 7183b4b4 aliguori
    if (err < 0)
1744 7183b4b4 aliguori
        goto fail;
1745 7183b4b4 aliguori
1746 7183b4b4 aliguori
    err = fcntl_setfl(fds[1], O_NONBLOCK);
1747 7183b4b4 aliguori
    if (err < 0)
1748 7183b4b4 aliguori
        goto fail;
1749 7183b4b4 aliguori
1750 c96f1a48 aliguori
    alarm_timer_rfd = fds[0];
1751 c96f1a48 aliguori
    alarm_timer_wfd = fds[1];
1752 f49e58dc aliguori
#endif
1753 c8994013 ths
1754 c8994013 ths
    for (i = 0; alarm_timers[i].name; i++) {
1755 c8994013 ths
        t = &alarm_timers[i];
1756 c8994013 ths
1757 c8994013 ths
        err = t->start(t);
1758 c8994013 ths
        if (!err)
1759 c8994013 ths
            break;
1760 67b915a5 bellard
    }
1761 fd872598 bellard
1762 c8994013 ths
    if (err) {
1763 7183b4b4 aliguori
        err = -ENOENT;
1764 7183b4b4 aliguori
        goto fail;
1765 67b915a5 bellard
    }
1766 c8994013 ths
1767 f49e58dc aliguori
#ifndef _WIN32
1768 6abfbd79 aliguori
    qemu_set_fd_handler2(alarm_timer_rfd, NULL,
1769 6abfbd79 aliguori
                         try_to_rearm_timer, NULL, t);
1770 f49e58dc aliguori
#endif
1771 6abfbd79 aliguori
1772 c8994013 ths
    alarm_timer = t;
1773 7183b4b4 aliguori
1774 6abfbd79 aliguori
    return 0;
1775 7183b4b4 aliguori
1776 7183b4b4 aliguori
fail:
1777 f49e58dc aliguori
#ifndef _WIN32
1778 7183b4b4 aliguori
    close(fds[0]);
1779 7183b4b4 aliguori
    close(fds[1]);
1780 f49e58dc aliguori
#endif
1781 7183b4b4 aliguori
    return err;
1782 8a7ddc38 bellard
}
1783 8a7ddc38 bellard
1784 9596ebb7 pbrook
static void quit_timers(void)
1785 40c3bac3 bellard
{
1786 c8994013 ths
    alarm_timer->stop(alarm_timer);
1787 c8994013 ths
    alarm_timer = NULL;
1788 40c3bac3 bellard
}
1789 40c3bac3 bellard
1790 c4b1fcc0 bellard
/***********************************************************/
1791 f6503059 balrog
/* host time/date access */
1792 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
1793 f6503059 balrog
{
1794 f6503059 balrog
    time_t ti;
1795 f6503059 balrog
    struct tm *ret;
1796 f6503059 balrog
1797 f6503059 balrog
    time(&ti);
1798 f6503059 balrog
    ti += offset;
1799 f6503059 balrog
    if (rtc_date_offset == -1) {
1800 f6503059 balrog
        if (rtc_utc)
1801 f6503059 balrog
            ret = gmtime(&ti);
1802 f6503059 balrog
        else
1803 f6503059 balrog
            ret = localtime(&ti);
1804 f6503059 balrog
    } else {
1805 f6503059 balrog
        ti -= rtc_date_offset;
1806 f6503059 balrog
        ret = gmtime(&ti);
1807 f6503059 balrog
    }
1808 f6503059 balrog
1809 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
1810 f6503059 balrog
}
1811 f6503059 balrog
1812 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
1813 f6503059 balrog
{
1814 f6503059 balrog
    time_t seconds;
1815 f6503059 balrog
1816 f6503059 balrog
    if (rtc_date_offset == -1)
1817 f6503059 balrog
        if (rtc_utc)
1818 f6503059 balrog
            seconds = mktimegm(tm);
1819 f6503059 balrog
        else
1820 f6503059 balrog
            seconds = mktime(tm);
1821 f6503059 balrog
    else
1822 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
1823 f6503059 balrog
1824 f6503059 balrog
    return seconds - time(NULL);
1825 f6503059 balrog
}
1826 f6503059 balrog
1827 fd1dff4b bellard
#ifdef _WIN32
1828 fd1dff4b bellard
static void socket_cleanup(void)
1829 fd1dff4b bellard
{
1830 fd1dff4b bellard
    WSACleanup();
1831 fd1dff4b bellard
}
1832 82c643ff bellard
1833 fd1dff4b bellard
static int socket_init(void)
1834 fd1dff4b bellard
{
1835 fd1dff4b bellard
    WSADATA Data;
1836 fd1dff4b bellard
    int ret, err;
1837 fd1dff4b bellard
1838 fd1dff4b bellard
    ret = WSAStartup(MAKEWORD(2,2), &Data);
1839 fd1dff4b bellard
    if (ret != 0) {
1840 fd1dff4b bellard
        err = WSAGetLastError();
1841 fd1dff4b bellard
        fprintf(stderr, "WSAStartup: %d\n", err);
1842 fd1dff4b bellard
        return -1;
1843 fd1dff4b bellard
    }
1844 fd1dff4b bellard
    atexit(socket_cleanup);
1845 fd1dff4b bellard
    return 0;
1846 fd1dff4b bellard
}
1847 64b7b733 aurel32
#endif
1848 64b7b733 aurel32
1849 63a01ef8 aliguori
const char *get_opt_name(char *buf, int buf_size, const char *p)
1850 609497ab balrog
{
1851 609497ab balrog
    char *q;
1852 609497ab balrog
1853 609497ab balrog
    q = buf;
1854 609497ab balrog
    while (*p != '\0' && *p != '=') {
1855 609497ab balrog
        if (q && (q - buf) < buf_size - 1)
1856 609497ab balrog
            *q++ = *p;
1857 609497ab balrog
        p++;
1858 609497ab balrog
    }
1859 609497ab balrog
    if (q)
1860 609497ab balrog
        *q = '\0';
1861 609497ab balrog
1862 609497ab balrog
    return p;
1863 609497ab balrog
}
1864 609497ab balrog
1865 63a01ef8 aliguori
const char *get_opt_value(char *buf, int buf_size, const char *p)
1866 e4bcb14c ths
{
1867 e4bcb14c ths
    char *q;
1868 e4bcb14c ths
1869 e4bcb14c ths
    q = buf;
1870 e4bcb14c ths
    while (*p != '\0') {
1871 609497ab balrog
        if (*p == ',') {
1872 609497ab balrog
            if (*(p + 1) != ',')
1873 e4bcb14c ths
                break;
1874 e4bcb14c ths
            p++;
1875 609497ab balrog
        }
1876 e4bcb14c ths
        if (q && (q - buf) < buf_size - 1)
1877 e4bcb14c ths
            *q++ = *p;
1878 e4bcb14c ths
        p++;
1879 e4bcb14c ths
    }
1880 e4bcb14c ths
    if (q)
1881 e4bcb14c ths
        *q = '\0';
1882 e4bcb14c ths
1883 e4bcb14c ths
    return p;
1884 e4bcb14c ths
}
1885 e4bcb14c ths
1886 63a01ef8 aliguori
int get_param_value(char *buf, int buf_size,
1887 63a01ef8 aliguori
                    const char *tag, const char *str)
1888 7c9d8e07 bellard
{
1889 7c9d8e07 bellard
    const char *p;
1890 7c9d8e07 bellard
    char option[128];
1891 7c9d8e07 bellard
1892 7c9d8e07 bellard
    p = str;
1893 7c9d8e07 bellard
    for(;;) {
1894 609497ab balrog
        p = get_opt_name(option, sizeof(option), p);
1895 7c9d8e07 bellard
        if (*p != '=')
1896 7c9d8e07 bellard
            break;
1897 7c9d8e07 bellard
        p++;
1898 7c9d8e07 bellard
        if (!strcmp(tag, option)) {
1899 609497ab balrog
            (void)get_opt_value(buf, buf_size, p);
1900 e4bcb14c ths
            return strlen(buf);
1901 7c9d8e07 bellard
        } else {
1902 609497ab balrog
            p = get_opt_value(NULL, 0, p);
1903 7c9d8e07 bellard
        }
1904 7c9d8e07 bellard
        if (*p != ',')
1905 7c9d8e07 bellard
            break;
1906 7c9d8e07 bellard
        p++;
1907 7c9d8e07 bellard
    }
1908 7c9d8e07 bellard
    return 0;
1909 7c9d8e07 bellard
}
1910 7c9d8e07 bellard
1911 63a01ef8 aliguori
int check_params(char *buf, int buf_size,
1912 63a01ef8 aliguori
                 const char * const *params, const char *str)
1913 e4bcb14c ths
{
1914 e4bcb14c ths
    const char *p;
1915 e4bcb14c ths
    int i;
1916 e4bcb14c ths
1917 e4bcb14c ths
    p = str;
1918 e4bcb14c ths
    for(;;) {
1919 609497ab balrog
        p = get_opt_name(buf, buf_size, p);
1920 e4bcb14c ths
        if (*p != '=')
1921 e4bcb14c ths
            return -1;
1922 e4bcb14c ths
        p++;
1923 e4bcb14c ths
        for(i = 0; params[i] != NULL; i++)
1924 e4bcb14c ths
            if (!strcmp(params[i], buf))
1925 e4bcb14c ths
                break;
1926 e4bcb14c ths
        if (params[i] == NULL)
1927 e4bcb14c ths
            return -1;
1928 609497ab balrog
        p = get_opt_value(NULL, 0, p);
1929 e4bcb14c ths
        if (*p != ',')
1930 e4bcb14c ths
            break;
1931 e4bcb14c ths
        p++;
1932 e4bcb14c ths
    }
1933 e4bcb14c ths
    return 0;
1934 e4bcb14c ths
}
1935 e4bcb14c ths
1936 1ae26a18 balrog
/***********************************************************/
1937 1ae26a18 balrog
/* Bluetooth support */
1938 1ae26a18 balrog
static int nb_hcis;
1939 1ae26a18 balrog
static int cur_hci;
1940 1ae26a18 balrog
static struct HCIInfo *hci_table[MAX_NICS];
1941 dc72ac14 balrog
1942 1ae26a18 balrog
static struct bt_vlan_s {
1943 1ae26a18 balrog
    struct bt_scatternet_s net;
1944 1ae26a18 balrog
    int id;
1945 1ae26a18 balrog
    struct bt_vlan_s *next;
1946 1ae26a18 balrog
} *first_bt_vlan;
1947 1ae26a18 balrog
1948 1ae26a18 balrog
/* find or alloc a new bluetooth "VLAN" */
1949 674bb261 blueswir1
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1950 1ae26a18 balrog
{
1951 1ae26a18 balrog
    struct bt_vlan_s **pvlan, *vlan;
1952 1ae26a18 balrog
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1953 1ae26a18 balrog
        if (vlan->id == id)
1954 1ae26a18 balrog
            return &vlan->net;
1955 1ae26a18 balrog
    }
1956 1ae26a18 balrog
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1957 1ae26a18 balrog
    vlan->id = id;
1958 1ae26a18 balrog
    pvlan = &first_bt_vlan;
1959 1ae26a18 balrog
    while (*pvlan != NULL)
1960 1ae26a18 balrog
        pvlan = &(*pvlan)->next;
1961 1ae26a18 balrog
    *pvlan = vlan;
1962 1ae26a18 balrog
    return &vlan->net;
1963 1ae26a18 balrog
}
1964 1ae26a18 balrog
1965 1ae26a18 balrog
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1966 1ae26a18 balrog
{
1967 1ae26a18 balrog
}
1968 1ae26a18 balrog
1969 1ae26a18 balrog
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1970 1ae26a18 balrog
{
1971 1ae26a18 balrog
    return -ENOTSUP;
1972 1ae26a18 balrog
}
1973 1ae26a18 balrog
1974 1ae26a18 balrog
static struct HCIInfo null_hci = {
1975 1ae26a18 balrog
    .cmd_send = null_hci_send,
1976 1ae26a18 balrog
    .sco_send = null_hci_send,
1977 1ae26a18 balrog
    .acl_send = null_hci_send,
1978 1ae26a18 balrog
    .bdaddr_set = null_hci_addr_set,
1979 1ae26a18 balrog
};
1980 1ae26a18 balrog
1981 1ae26a18 balrog
struct HCIInfo *qemu_next_hci(void)
1982 1ae26a18 balrog
{
1983 1ae26a18 balrog
    if (cur_hci == nb_hcis)
1984 1ae26a18 balrog
        return &null_hci;
1985 1ae26a18 balrog
1986 1ae26a18 balrog
    return hci_table[cur_hci++];
1987 1ae26a18 balrog
}
1988 1ae26a18 balrog
1989 dc72ac14 balrog
static struct HCIInfo *hci_init(const char *str)
1990 dc72ac14 balrog
{
1991 dc72ac14 balrog
    char *endp;
1992 dc72ac14 balrog
    struct bt_scatternet_s *vlan = 0;
1993 dc72ac14 balrog
1994 dc72ac14 balrog
    if (!strcmp(str, "null"))
1995 dc72ac14 balrog
        /* null */
1996 dc72ac14 balrog
        return &null_hci;
1997 dc72ac14 balrog
    else if (!strncmp(str, "host", 4) && (str[4] == '\0' || str[4] == ':'))
1998 dc72ac14 balrog
        /* host[:hciN] */
1999 dc72ac14 balrog
        return bt_host_hci(str[4] ? str + 5 : "hci0");
2000 dc72ac14 balrog
    else if (!strncmp(str, "hci", 3)) {
2001 dc72ac14 balrog
        /* hci[,vlan=n] */
2002 dc72ac14 balrog
        if (str[3]) {
2003 dc72ac14 balrog
            if (!strncmp(str + 3, ",vlan=", 6)) {
2004 dc72ac14 balrog
                vlan = qemu_find_bt_vlan(strtol(str + 9, &endp, 0));
2005 dc72ac14 balrog
                if (*endp)
2006 dc72ac14 balrog
                    vlan = 0;
2007 dc72ac14 balrog
            }
2008 dc72ac14 balrog
        } else
2009 dc72ac14 balrog
            vlan = qemu_find_bt_vlan(0);
2010 dc72ac14 balrog
        if (vlan)
2011 dc72ac14 balrog
           return bt_new_hci(vlan);
2012 dc72ac14 balrog
    }
2013 dc72ac14 balrog
2014 dc72ac14 balrog
    fprintf(stderr, "qemu: Unknown bluetooth HCI `%s'.\n", str);
2015 dc72ac14 balrog
2016 dc72ac14 balrog
    return 0;
2017 dc72ac14 balrog
}
2018 dc72ac14 balrog
2019 dc72ac14 balrog
static int bt_hci_parse(const char *str)
2020 dc72ac14 balrog
{
2021 dc72ac14 balrog
    struct HCIInfo *hci;
2022 dc72ac14 balrog
    bdaddr_t bdaddr;
2023 dc72ac14 balrog
2024 dc72ac14 balrog
    if (nb_hcis >= MAX_NICS) {
2025 dc72ac14 balrog
        fprintf(stderr, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS);
2026 dc72ac14 balrog
        return -1;
2027 dc72ac14 balrog
    }
2028 dc72ac14 balrog
2029 dc72ac14 balrog
    hci = hci_init(str);
2030 dc72ac14 balrog
    if (!hci)
2031 dc72ac14 balrog
        return -1;
2032 dc72ac14 balrog
2033 dc72ac14 balrog
    bdaddr.b[0] = 0x52;
2034 dc72ac14 balrog
    bdaddr.b[1] = 0x54;
2035 dc72ac14 balrog
    bdaddr.b[2] = 0x00;
2036 dc72ac14 balrog
    bdaddr.b[3] = 0x12;
2037 dc72ac14 balrog
    bdaddr.b[4] = 0x34;
2038 dc72ac14 balrog
    bdaddr.b[5] = 0x56 + nb_hcis;
2039 dc72ac14 balrog
    hci->bdaddr_set(hci, bdaddr.b);
2040 dc72ac14 balrog
2041 dc72ac14 balrog
    hci_table[nb_hcis++] = hci;
2042 dc72ac14 balrog
2043 dc72ac14 balrog
    return 0;
2044 dc72ac14 balrog
}
2045 dc72ac14 balrog
2046 dc72ac14 balrog
static void bt_vhci_add(int vlan_id)
2047 dc72ac14 balrog
{
2048 dc72ac14 balrog
    struct bt_scatternet_s *vlan = qemu_find_bt_vlan(vlan_id);
2049 dc72ac14 balrog
2050 dc72ac14 balrog
    if (!vlan->slave)
2051 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a VHCI to "
2052 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
2053 dc72ac14 balrog
2054 dc72ac14 balrog
    bt_vhci_init(bt_new_hci(vlan));
2055 dc72ac14 balrog
}
2056 dc72ac14 balrog
2057 dc72ac14 balrog
static struct bt_device_s *bt_device_add(const char *opt)
2058 dc72ac14 balrog
{
2059 dc72ac14 balrog
    struct bt_scatternet_s *vlan;
2060 dc72ac14 balrog
    int vlan_id = 0;
2061 dc72ac14 balrog
    char *endp = strstr(opt, ",vlan=");
2062 dc72ac14 balrog
    int len = (endp ? endp - opt : strlen(opt)) + 1;
2063 dc72ac14 balrog
    char devname[10];
2064 dc72ac14 balrog
2065 dc72ac14 balrog
    pstrcpy(devname, MIN(sizeof(devname), len), opt);
2066 dc72ac14 balrog
2067 dc72ac14 balrog
    if (endp) {
2068 dc72ac14 balrog
        vlan_id = strtol(endp + 6, &endp, 0);
2069 dc72ac14 balrog
        if (*endp) {
2070 dc72ac14 balrog
            fprintf(stderr, "qemu: unrecognised bluetooth vlan Id\n");
2071 dc72ac14 balrog
            return 0;
2072 dc72ac14 balrog
        }
2073 dc72ac14 balrog
    }
2074 dc72ac14 balrog
2075 dc72ac14 balrog
    vlan = qemu_find_bt_vlan(vlan_id);
2076 dc72ac14 balrog
2077 dc72ac14 balrog
    if (!vlan->slave)
2078 dc72ac14 balrog
        fprintf(stderr, "qemu: warning: adding a slave device to "
2079 dc72ac14 balrog
                        "an empty scatternet %i\n", vlan_id);
2080 dc72ac14 balrog
2081 dc72ac14 balrog
    if (!strcmp(devname, "keyboard"))
2082 dc72ac14 balrog
        return bt_keyboard_init(vlan);
2083 dc72ac14 balrog
2084 dc72ac14 balrog
    fprintf(stderr, "qemu: unsupported bluetooth device `%s'\n", devname);
2085 dc72ac14 balrog
    return 0;
2086 dc72ac14 balrog
}
2087 dc72ac14 balrog
2088 dc72ac14 balrog
static int bt_parse(const char *opt)
2089 dc72ac14 balrog
{
2090 dc72ac14 balrog
    const char *endp, *p;
2091 dc72ac14 balrog
    int vlan;
2092 dc72ac14 balrog
2093 dc72ac14 balrog
    if (strstart(opt, "hci", &endp)) {
2094 dc72ac14 balrog
        if (!*endp || *endp == ',') {
2095 dc72ac14 balrog
            if (*endp)
2096 dc72ac14 balrog
                if (!strstart(endp, ",vlan=", 0))
2097 dc72ac14 balrog
                    opt = endp + 1;
2098 dc72ac14 balrog
2099 dc72ac14 balrog
            return bt_hci_parse(opt);
2100 dc72ac14 balrog
       }
2101 dc72ac14 balrog
    } else if (strstart(opt, "vhci", &endp)) {
2102 dc72ac14 balrog
        if (!*endp || *endp == ',') {
2103 dc72ac14 balrog
            if (*endp) {
2104 dc72ac14 balrog
                if (strstart(endp, ",vlan=", &p)) {
2105 dc72ac14 balrog
                    vlan = strtol(p, (char **) &endp, 0);
2106 dc72ac14 balrog
                    if (*endp) {
2107 dc72ac14 balrog
                        fprintf(stderr, "qemu: bad scatternet '%s'\n", p);
2108 dc72ac14 balrog
                        return 1;
2109 dc72ac14 balrog
                    }
2110 dc72ac14 balrog
                } else {
2111 dc72ac14 balrog
                    fprintf(stderr, "qemu: bad parameter '%s'\n", endp + 1);
2112 dc72ac14 balrog
                    return 1;
2113 dc72ac14 balrog
                }
2114 dc72ac14 balrog
            } else
2115 dc72ac14 balrog
                vlan = 0;
2116 dc72ac14 balrog
2117 dc72ac14 balrog
            bt_vhci_add(vlan);
2118 dc72ac14 balrog
            return 0;
2119 dc72ac14 balrog
        }
2120 dc72ac14 balrog
    } else if (strstart(opt, "device:", &endp))
2121 dc72ac14 balrog
        return !bt_device_add(endp);
2122 dc72ac14 balrog
2123 dc72ac14 balrog
    fprintf(stderr, "qemu: bad bluetooth parameter '%s'\n", opt);
2124 dc72ac14 balrog
    return 1;
2125 dc72ac14 balrog
}
2126 dc72ac14 balrog
2127 1ae26a18 balrog
/***********************************************************/
2128 1ae26a18 balrog
/* QEMU Block devices */
2129 1ae26a18 balrog
2130 609497ab balrog
#define HD_ALIAS "index=%d,media=disk"
2131 e4bcb14c ths
#ifdef TARGET_PPC
2132 e4bcb14c ths
#define CDROM_ALIAS "index=1,media=cdrom"
2133 e4bcb14c ths
#else
2134 e4bcb14c ths
#define CDROM_ALIAS "index=2,media=cdrom"
2135 e4bcb14c ths
#endif
2136 e4bcb14c ths
#define FD_ALIAS "index=%d,if=floppy"
2137 609497ab balrog
#define PFLASH_ALIAS "if=pflash"
2138 609497ab balrog
#define MTD_ALIAS "if=mtd"
2139 9d413d1d balrog
#define SD_ALIAS "index=0,if=sd"
2140 e4bcb14c ths
2141 609497ab balrog
static int drive_add(const char *file, const char *fmt, ...)
2142 e4bcb14c ths
{
2143 e4bcb14c ths
    va_list ap;
2144 e4bcb14c ths
2145 e4bcb14c ths
    if (nb_drives_opt >= MAX_DRIVES) {
2146 e4bcb14c ths
        fprintf(stderr, "qemu: too many drives\n");
2147 e4bcb14c ths
        exit(1);
2148 e4bcb14c ths
    }
2149 e4bcb14c ths
2150 609497ab balrog
    drives_opt[nb_drives_opt].file = file;
2151 e4bcb14c ths
    va_start(ap, fmt);
2152 609497ab balrog
    vsnprintf(drives_opt[nb_drives_opt].opt,
2153 609497ab balrog
              sizeof(drives_opt[0].opt), fmt, ap);
2154 e4bcb14c ths
    va_end(ap);
2155 e4bcb14c ths
2156 e4bcb14c ths
    return nb_drives_opt++;
2157 e4bcb14c ths
}
2158 e4bcb14c ths
2159 f60d39bc ths
int drive_get_index(BlockInterfaceType type, int bus, int unit)
2160 e4bcb14c ths
{
2161 e4bcb14c ths
    int index;
2162 e4bcb14c ths
2163 e4bcb14c ths
    /* seek interface, bus and unit */
2164 e4bcb14c ths
2165 e4bcb14c ths
    for (index = 0; index < nb_drives; index++)
2166 f60d39bc ths
        if (drives_table[index].type == type &&
2167 e4bcb14c ths
            drives_table[index].bus == bus &&
2168 e4bcb14c ths
            drives_table[index].unit == unit)
2169 e4bcb14c ths
        return index;
2170 e4bcb14c ths
2171 e4bcb14c ths
    return -1;
2172 e4bcb14c ths
}
2173 e4bcb14c ths
2174 f60d39bc ths
int drive_get_max_bus(BlockInterfaceType type)
2175 e4bcb14c ths
{
2176 e4bcb14c ths
    int max_bus;
2177 e4bcb14c ths
    int index;
2178 e4bcb14c ths
2179 e4bcb14c ths
    max_bus = -1;
2180 e4bcb14c ths
    for (index = 0; index < nb_drives; index++) {
2181 f60d39bc ths
        if(drives_table[index].type == type &&
2182 e4bcb14c ths
           drives_table[index].bus > max_bus)
2183 e4bcb14c ths
            max_bus = drives_table[index].bus;
2184 e4bcb14c ths
    }
2185 e4bcb14c ths
    return max_bus;
2186 e4bcb14c ths
}
2187 e4bcb14c ths
2188 a1620fac aurel32
static void bdrv_format_print(void *opaque, const char *name)
2189 a1620fac aurel32
{
2190 a1620fac aurel32
    fprintf(stderr, " %s", name);
2191 a1620fac aurel32
}
2192 a1620fac aurel32
2193 609497ab balrog
static int drive_init(struct drive_opt *arg, int snapshot,
2194 609497ab balrog
                      QEMUMachine *machine)
2195 e4bcb14c ths
{
2196 e4bcb14c ths
    char buf[128];
2197 e4bcb14c ths
    char file[1024];
2198 c8522bdf balrog
    char devname[128];
2199 c8522bdf balrog
    const char *mediastr = "";
2200 f60d39bc ths
    BlockInterfaceType type;
2201 e4bcb14c ths
    enum { MEDIA_DISK, MEDIA_CDROM } media;
2202 e4bcb14c ths
    int bus_id, unit_id;
2203 e4bcb14c ths
    int cyls, heads, secs, translation;
2204 e4bcb14c ths
    BlockDriverState *bdrv;
2205 1e72d3b7 aurel32
    BlockDriver *drv = NULL;
2206 e4bcb14c ths
    int max_devs;
2207 e4bcb14c ths
    int index;
2208 33f00271 balrog
    int cache;
2209 33f00271 balrog
    int bdrv_flags;
2210 609497ab balrog
    char *str = arg->opt;
2211 7ccfb2eb blueswir1
    static const char * const params[] = { "bus", "unit", "if", "index",
2212 7ccfb2eb blueswir1
                                           "cyls", "heads", "secs", "trans",
2213 7ccfb2eb blueswir1
                                           "media", "snapshot", "file",
2214 7ccfb2eb blueswir1
                                           "cache", "format", NULL };
2215 e4bcb14c ths
2216 e4bcb14c ths
    if (check_params(buf, sizeof(buf), params, str) < 0) {
2217 ff993638 balrog
         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
2218 e4bcb14c ths
                         buf, str);
2219 e4bcb14c ths
         return -1;
2220 e4bcb14c ths
    }
2221 e4bcb14c ths
2222 e4bcb14c ths
    file[0] = 0;
2223 e4bcb14c ths
    cyls = heads = secs = 0;
2224 e4bcb14c ths
    bus_id = 0;
2225 e4bcb14c ths
    unit_id = -1;
2226 e4bcb14c ths
    translation = BIOS_ATA_TRANSLATION_AUTO;
2227 e4bcb14c ths
    index = -1;
2228 33f00271 balrog
    cache = 1;
2229 e4bcb14c ths
2230 c9b1ae2c blueswir1
    if (machine->use_scsi) {
2231 f60d39bc ths
        type = IF_SCSI;
2232 e4bcb14c ths
        max_devs = MAX_SCSI_DEVS;
2233 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "scsi");
2234 e4bcb14c ths
    } else {
2235 f60d39bc ths
        type = IF_IDE;
2236 e4bcb14c ths
        max_devs = MAX_IDE_DEVS;
2237 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "ide");
2238 e4bcb14c ths
    }
2239 e4bcb14c ths
    media = MEDIA_DISK;
2240 e4bcb14c ths
2241 e4bcb14c ths
    /* extract parameters */
2242 e4bcb14c ths
2243 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "bus", str)) {
2244 e4bcb14c ths
        bus_id = strtol(buf, NULL, 0);
2245 e4bcb14c ths
        if (bus_id < 0) {
2246 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2247 e4bcb14c ths
            return -1;
2248 e4bcb14c ths
        }
2249 e4bcb14c ths
    }
2250 e4bcb14c ths
2251 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "unit", str)) {
2252 e4bcb14c ths
        unit_id = strtol(buf, NULL, 0);
2253 e4bcb14c ths
        if (unit_id < 0) {
2254 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2255 e4bcb14c ths
            return -1;
2256 e4bcb14c ths
        }
2257 e4bcb14c ths
    }
2258 e4bcb14c ths
2259 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "if", str)) {
2260 ae45d369 bellard
        pstrcpy(devname, sizeof(devname), buf);
2261 e4bcb14c ths
        if (!strcmp(buf, "ide")) {
2262 f60d39bc ths
            type = IF_IDE;
2263 e4bcb14c ths
            max_devs = MAX_IDE_DEVS;
2264 e4bcb14c ths
        } else if (!strcmp(buf, "scsi")) {
2265 f60d39bc ths
            type = IF_SCSI;
2266 e4bcb14c ths
            max_devs = MAX_SCSI_DEVS;
2267 e4bcb14c ths
        } else if (!strcmp(buf, "floppy")) {
2268 f60d39bc ths
            type = IF_FLOPPY;
2269 e4bcb14c ths
            max_devs = 0;
2270 e4bcb14c ths
        } else if (!strcmp(buf, "pflash")) {
2271 f60d39bc ths
            type = IF_PFLASH;
2272 e4bcb14c ths
            max_devs = 0;
2273 e4bcb14c ths
        } else if (!strcmp(buf, "mtd")) {
2274 f60d39bc ths
            type = IF_MTD;
2275 e4bcb14c ths
            max_devs = 0;
2276 e4bcb14c ths
        } else if (!strcmp(buf, "sd")) {
2277 f60d39bc ths
            type = IF_SD;
2278 e4bcb14c ths
            max_devs = 0;
2279 e4bcb14c ths
        } else {
2280 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2281 e4bcb14c ths
            return -1;
2282 e4bcb14c ths
        }
2283 e4bcb14c ths
    }
2284 e4bcb14c ths
2285 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "index", str)) {
2286 e4bcb14c ths
        index = strtol(buf, NULL, 0);
2287 e4bcb14c ths
        if (index < 0) {
2288 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid index\n", str);
2289 e4bcb14c ths
            return -1;
2290 e4bcb14c ths
        }
2291 e4bcb14c ths
    }
2292 e4bcb14c ths
2293 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2294 e4bcb14c ths
        cyls = strtol(buf, NULL, 0);
2295 e4bcb14c ths
    }
2296 e4bcb14c ths
2297 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "heads", str)) {
2298 e4bcb14c ths
        heads = strtol(buf, NULL, 0);
2299 e4bcb14c ths
    }
2300 e4bcb14c ths
2301 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "secs", str)) {
2302 e4bcb14c ths
        secs = strtol(buf, NULL, 0);
2303 e4bcb14c ths
    }
2304 e4bcb14c ths
2305 e4bcb14c ths
    if (cyls || heads || secs) {
2306 e4bcb14c ths
        if (cyls < 1 || cyls > 16383) {
2307 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2308 e4bcb14c ths
            return -1;
2309 e4bcb14c ths
        }
2310 e4bcb14c ths
        if (heads < 1 || heads > 16) {
2311 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2312 e4bcb14c ths
            return -1;
2313 e4bcb14c ths
        }
2314 e4bcb14c ths
        if (secs < 1 || secs > 63) {
2315 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2316 e4bcb14c ths
            return -1;
2317 e4bcb14c ths
        }
2318 e4bcb14c ths
    }
2319 e4bcb14c ths
2320 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "trans", str)) {
2321 e4bcb14c ths
        if (!cyls) {
2322 e4bcb14c ths
            fprintf(stderr,
2323 e4bcb14c ths
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
2324 e4bcb14c ths
                    str);
2325 e4bcb14c ths
            return -1;
2326 e4bcb14c ths
        }
2327 e4bcb14c ths
        if (!strcmp(buf, "none"))
2328 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_NONE;
2329 e4bcb14c ths
        else if (!strcmp(buf, "lba"))
2330 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_LBA;
2331 e4bcb14c ths
        else if (!strcmp(buf, "auto"))
2332 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_AUTO;
2333 e4bcb14c ths
        else {
2334 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2335 e4bcb14c ths
            return -1;
2336 e4bcb14c ths
        }
2337 e4bcb14c ths
    }
2338 e4bcb14c ths
2339 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "media", str)) {
2340 e4bcb14c ths
        if (!strcmp(buf, "disk")) {
2341 e4bcb14c ths
            media = MEDIA_DISK;
2342 e4bcb14c ths
        } else if (!strcmp(buf, "cdrom")) {
2343 e4bcb14c ths
            if (cyls || secs || heads) {
2344 e4bcb14c ths
                fprintf(stderr,
2345 e4bcb14c ths
                        "qemu: '%s' invalid physical CHS format\n", str);
2346 e4bcb14c ths
                return -1;
2347 e4bcb14c ths
            }
2348 e4bcb14c ths
            media = MEDIA_CDROM;
2349 e4bcb14c ths
        } else {
2350 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid media\n", str);
2351 e4bcb14c ths
            return -1;
2352 e4bcb14c ths
        }
2353 e4bcb14c ths
    }
2354 e4bcb14c ths
2355 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2356 e4bcb14c ths
        if (!strcmp(buf, "on"))
2357 e4bcb14c ths
            snapshot = 1;
2358 e4bcb14c ths
        else if (!strcmp(buf, "off"))
2359 e4bcb14c ths
            snapshot = 0;
2360 e4bcb14c ths
        else {
2361 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2362 e4bcb14c ths
            return -1;
2363 e4bcb14c ths
        }
2364 e4bcb14c ths
    }
2365 e4bcb14c ths
2366 33f00271 balrog
    if (get_param_value(buf, sizeof(buf), "cache", str)) {
2367 9f7965c7 aliguori
        if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2368 33f00271 balrog
            cache = 0;
2369 9f7965c7 aliguori
        else if (!strcmp(buf, "writethrough"))
2370 33f00271 balrog
            cache = 1;
2371 9f7965c7 aliguori
        else if (!strcmp(buf, "writeback"))
2372 9f7965c7 aliguori
            cache = 2;
2373 33f00271 balrog
        else {
2374 33f00271 balrog
           fprintf(stderr, "qemu: invalid cache option\n");
2375 33f00271 balrog
           return -1;
2376 33f00271 balrog
        }
2377 33f00271 balrog
    }
2378 33f00271 balrog
2379 1e72d3b7 aurel32
    if (get_param_value(buf, sizeof(buf), "format", str)) {
2380 a1620fac aurel32
       if (strcmp(buf, "?") == 0) {
2381 a1620fac aurel32
            fprintf(stderr, "qemu: Supported formats:");
2382 a1620fac aurel32
            bdrv_iterate_format(bdrv_format_print, NULL);
2383 a1620fac aurel32
            fprintf(stderr, "\n");
2384 a1620fac aurel32
            return -1;
2385 a1620fac aurel32
        }
2386 1e72d3b7 aurel32
        drv = bdrv_find_format(buf);
2387 1e72d3b7 aurel32
        if (!drv) {
2388 1e72d3b7 aurel32
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2389 1e72d3b7 aurel32
            return -1;
2390 1e72d3b7 aurel32
        }
2391 1e72d3b7 aurel32
    }
2392 1e72d3b7 aurel32
2393 609497ab balrog
    if (arg->file == NULL)
2394 609497ab balrog
        get_param_value(file, sizeof(file), "file", str);
2395 609497ab balrog
    else
2396 609497ab balrog
        pstrcpy(file, sizeof(file), arg->file);
2397 e4bcb14c ths
2398 e4bcb14c ths
    /* compute bus and unit according index */
2399 e4bcb14c ths
2400 e4bcb14c ths
    if (index != -1) {
2401 e4bcb14c ths
        if (bus_id != 0 || unit_id != -1) {
2402 e4bcb14c ths
            fprintf(stderr,
2403 e4bcb14c ths
                    "qemu: '%s' index cannot be used with bus and unit\n", str);
2404 e4bcb14c ths
            return -1;
2405 e4bcb14c ths
        }
2406 e4bcb14c ths
        if (max_devs == 0)
2407 e4bcb14c ths
        {
2408 e4bcb14c ths
            unit_id = index;
2409 e4bcb14c ths
            bus_id = 0;
2410 e4bcb14c ths
        } else {
2411 e4bcb14c ths
            unit_id = index % max_devs;
2412 e4bcb14c ths
            bus_id = index / max_devs;
2413 e4bcb14c ths
        }
2414 e4bcb14c ths
    }
2415 e4bcb14c ths
2416 e4bcb14c ths
    /* if user doesn't specify a unit_id,
2417 e4bcb14c ths
     * try to find the first free
2418 e4bcb14c ths
     */
2419 e4bcb14c ths
2420 e4bcb14c ths
    if (unit_id == -1) {
2421 e4bcb14c ths
       unit_id = 0;
2422 f60d39bc ths
       while (drive_get_index(type, bus_id, unit_id) != -1) {
2423 e4bcb14c ths
           unit_id++;
2424 e4bcb14c ths
           if (max_devs && unit_id >= max_devs) {
2425 e4bcb14c ths
               unit_id -= max_devs;
2426 e4bcb14c ths
               bus_id++;
2427 e4bcb14c ths
           }
2428 e4bcb14c ths
       }
2429 e4bcb14c ths
    }
2430 e4bcb14c ths
2431 e4bcb14c ths
    /* check unit id */
2432 e4bcb14c ths
2433 e4bcb14c ths
    if (max_devs && unit_id >= max_devs) {
2434 e4bcb14c ths
        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2435 e4bcb14c ths
                        str, unit_id, max_devs - 1);
2436 e4bcb14c ths
        return -1;
2437 e4bcb14c ths
    }
2438 e4bcb14c ths
2439 e4bcb14c ths
    /*
2440 e4bcb14c ths
     * ignore multiple definitions
2441 e4bcb14c ths
     */
2442 e4bcb14c ths
2443 f60d39bc ths
    if (drive_get_index(type, bus_id, unit_id) != -1)
2444 e4bcb14c ths
        return 0;
2445 e4bcb14c ths
2446 e4bcb14c ths
    /* init */
2447 e4bcb14c ths
2448 f60d39bc ths
    if (type == IF_IDE || type == IF_SCSI)
2449 c8522bdf balrog
        mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2450 e6198a70 balrog
    if (max_devs)
2451 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%i%s%i",
2452 e6198a70 balrog
                 devname, bus_id, mediastr, unit_id);
2453 e6198a70 balrog
    else
2454 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%s%i",
2455 e6198a70 balrog
                 devname, mediastr, unit_id);
2456 e4bcb14c ths
    bdrv = bdrv_new(buf);
2457 e4bcb14c ths
    drives_table[nb_drives].bdrv = bdrv;
2458 f60d39bc ths
    drives_table[nb_drives].type = type;
2459 e4bcb14c ths
    drives_table[nb_drives].bus = bus_id;
2460 e4bcb14c ths
    drives_table[nb_drives].unit = unit_id;
2461 e4bcb14c ths
    nb_drives++;
2462 e4bcb14c ths
2463 f60d39bc ths
    switch(type) {
2464 e4bcb14c ths
    case IF_IDE:
2465 e4bcb14c ths
    case IF_SCSI:
2466 e4bcb14c ths
        switch(media) {
2467 e4bcb14c ths
        case MEDIA_DISK:
2468 e4bcb14c ths
            if (cyls != 0) {
2469 e4bcb14c ths
                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2470 e4bcb14c ths
                bdrv_set_translation_hint(bdrv, translation);
2471 e4bcb14c ths
            }
2472 e4bcb14c ths
            break;
2473 e4bcb14c ths
        case MEDIA_CDROM:
2474 e4bcb14c ths
            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2475 e4bcb14c ths
            break;
2476 e4bcb14c ths
        }
2477 e4bcb14c ths
        break;
2478 e4bcb14c ths
    case IF_SD:
2479 e4bcb14c ths
        /* FIXME: This isn't really a floppy, but it's a reasonable
2480 e4bcb14c ths
           approximation.  */
2481 e4bcb14c ths
    case IF_FLOPPY:
2482 e4bcb14c ths
        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2483 e4bcb14c ths
        break;
2484 e4bcb14c ths
    case IF_PFLASH:
2485 e4bcb14c ths
    case IF_MTD:
2486 e4bcb14c ths
        break;
2487 e4bcb14c ths
    }
2488 e4bcb14c ths
    if (!file[0])
2489 e4bcb14c ths
        return 0;
2490 33f00271 balrog
    bdrv_flags = 0;
2491 9f7965c7 aliguori
    if (snapshot) {
2492 33f00271 balrog
        bdrv_flags |= BDRV_O_SNAPSHOT;
2493 9f7965c7 aliguori
        cache = 2; /* always use write-back with snapshot */
2494 9f7965c7 aliguori
    }
2495 9f7965c7 aliguori
    if (cache == 0) /* no caching */
2496 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_NOCACHE;
2497 9f7965c7 aliguori
    else if (cache == 2) /* write-back */
2498 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_CACHE_WB;
2499 83ab7950 aliguori
    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
2500 e4bcb14c ths
        fprintf(stderr, "qemu: could not open disk image %s\n",
2501 e4bcb14c ths
                        file);
2502 e4bcb14c ths
        return -1;
2503 e4bcb14c ths
    }
2504 e4bcb14c ths
    return 0;
2505 e4bcb14c ths
}
2506 e4bcb14c ths
2507 330d0414 bellard
/***********************************************************/
2508 a594cfbf bellard
/* USB devices */
2509 a594cfbf bellard
2510 0d92ed30 pbrook
static USBPort *used_usb_ports;
2511 0d92ed30 pbrook
static USBPort *free_usb_ports;
2512 0d92ed30 pbrook
2513 0d92ed30 pbrook
/* ??? Maybe change this to register a hub to keep track of the topology.  */
2514 0d92ed30 pbrook
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2515 0d92ed30 pbrook
                            usb_attachfn attach)
2516 0d92ed30 pbrook
{
2517 0d92ed30 pbrook
    port->opaque = opaque;
2518 0d92ed30 pbrook
    port->index = index;
2519 0d92ed30 pbrook
    port->attach = attach;
2520 0d92ed30 pbrook
    port->next = free_usb_ports;
2521 0d92ed30 pbrook
    free_usb_ports = port;
2522 0d92ed30 pbrook
}
2523 0d92ed30 pbrook
2524 4b096fc9 aliguori
int usb_device_add_dev(USBDevice *dev)
2525 4b096fc9 aliguori
{
2526 4b096fc9 aliguori
    USBPort *port;
2527 4b096fc9 aliguori
2528 4b096fc9 aliguori
    /* Find a USB port to add the device to.  */
2529 4b096fc9 aliguori
    port = free_usb_ports;
2530 4b096fc9 aliguori
    if (!port->next) {
2531 4b096fc9 aliguori
        USBDevice *hub;
2532 4b096fc9 aliguori
2533 4b096fc9 aliguori
        /* Create a new hub and chain it on.  */
2534 4b096fc9 aliguori
        free_usb_ports = NULL;
2535 4b096fc9 aliguori
        port->next = used_usb_ports;
2536 4b096fc9 aliguori
        used_usb_ports = port;
2537 4b096fc9 aliguori
2538 4b096fc9 aliguori
        hub = usb_hub_init(VM_USB_HUB_SIZE);
2539 4b096fc9 aliguori
        usb_attach(port, hub);
2540 4b096fc9 aliguori
        port = free_usb_ports;
2541 4b096fc9 aliguori
    }
2542 4b096fc9 aliguori
2543 4b096fc9 aliguori
    free_usb_ports = port->next;
2544 4b096fc9 aliguori
    port->next = used_usb_ports;
2545 4b096fc9 aliguori
    used_usb_ports = port;
2546 4b096fc9 aliguori
    usb_attach(port, dev);
2547 4b096fc9 aliguori
    return 0;
2548 4b096fc9 aliguori
}
2549 4b096fc9 aliguori
2550 a594cfbf bellard
static int usb_device_add(const char *devname)
2551 a594cfbf bellard
{
2552 a594cfbf bellard
    const char *p;
2553 a594cfbf bellard
    USBDevice *dev;
2554 a594cfbf bellard
2555 0d92ed30 pbrook
    if (!free_usb_ports)
2556 a594cfbf bellard
        return -1;
2557 a594cfbf bellard
2558 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
2559 a594cfbf bellard
        dev = usb_host_device_open(p);
2560 a594cfbf bellard
    } else if (!strcmp(devname, "mouse")) {
2561 a594cfbf bellard
        dev = usb_mouse_init();
2562 09b26c5e bellard
    } else if (!strcmp(devname, "tablet")) {
2563 47b2d338 balrog
        dev = usb_tablet_init();
2564 47b2d338 balrog
    } else if (!strcmp(devname, "keyboard")) {
2565 47b2d338 balrog
        dev = usb_keyboard_init();
2566 2e5d83bb pbrook
    } else if (strstart(devname, "disk:", &p)) {
2567 2e5d83bb pbrook
        dev = usb_msd_init(p);
2568 f6d2a316 balrog
    } else if (!strcmp(devname, "wacom-tablet")) {
2569 f6d2a316 balrog
        dev = usb_wacom_init();
2570 a7954218 balrog
    } else if (strstart(devname, "serial:", &p)) {
2571 a7954218 balrog
        dev = usb_serial_init(p);
2572 2e4d9fb1 aurel32
#ifdef CONFIG_BRLAPI
2573 2e4d9fb1 aurel32
    } else if (!strcmp(devname, "braille")) {
2574 2e4d9fb1 aurel32
        dev = usb_baum_init();
2575 2e4d9fb1 aurel32
#endif
2576 6c9f886c balrog
    } else if (strstart(devname, "net:", &p)) {
2577 9ad97e65 balrog
        int nic = nb_nics;
2578 6c9f886c balrog
2579 9ad97e65 balrog
        if (net_client_init("nic", p) < 0)
2580 6c9f886c balrog
            return -1;
2581 9ad97e65 balrog
        nd_table[nic].model = "usb";
2582 9ad97e65 balrog
        dev = usb_net_init(&nd_table[nic]);
2583 dc72ac14 balrog
    } else if (!strcmp(devname, "bt") || strstart(devname, "bt:", &p)) {
2584 dc72ac14 balrog
        dev = usb_bt_init(devname[2] ? hci_init(p) :
2585 dc72ac14 balrog
                        bt_new_hci(qemu_find_bt_vlan(0)));
2586 a594cfbf bellard
    } else {
2587 a594cfbf bellard
        return -1;
2588 a594cfbf bellard
    }
2589 0d92ed30 pbrook
    if (!dev)
2590 0d92ed30 pbrook
        return -1;
2591 0d92ed30 pbrook
2592 4b096fc9 aliguori
    return usb_device_add_dev(dev);
2593 a594cfbf bellard
}
2594 a594cfbf bellard
2595 1f3870ab aliguori
int usb_device_del_addr(int bus_num, int addr)
2596 a594cfbf bellard
{
2597 0d92ed30 pbrook
    USBPort *port;
2598 0d92ed30 pbrook
    USBPort **lastp;
2599 059809e4 bellard
    USBDevice *dev;
2600 a594cfbf bellard
2601 0d92ed30 pbrook
    if (!used_usb_ports)
2602 a594cfbf bellard
        return -1;
2603 a594cfbf bellard
2604 a594cfbf bellard
    if (bus_num != 0)
2605 a594cfbf bellard
        return -1;
2606 0d92ed30 pbrook
2607 0d92ed30 pbrook
    lastp = &used_usb_ports;
2608 0d92ed30 pbrook
    port = used_usb_ports;
2609 0d92ed30 pbrook
    while (port && port->dev->addr != addr) {
2610 0d92ed30 pbrook
        lastp = &port->next;
2611 0d92ed30 pbrook
        port = port->next;
2612 a594cfbf bellard
    }
2613 0d92ed30 pbrook
2614 0d92ed30 pbrook
    if (!port)
2615 a594cfbf bellard
        return -1;
2616 0d92ed30 pbrook
2617 059809e4 bellard
    dev = port->dev;
2618 0d92ed30 pbrook
    *lastp = port->next;
2619 0d92ed30 pbrook
    usb_attach(port, NULL);
2620 059809e4 bellard
    dev->handle_destroy(dev);
2621 0d92ed30 pbrook
    port->next = free_usb_ports;
2622 0d92ed30 pbrook
    free_usb_ports = port;
2623 a594cfbf bellard
    return 0;
2624 a594cfbf bellard
}
2625 a594cfbf bellard
2626 1f3870ab aliguori
static int usb_device_del(const char *devname)
2627 1f3870ab aliguori
{
2628 1f3870ab aliguori
    int bus_num, addr;
2629 1f3870ab aliguori
    const char *p;
2630 1f3870ab aliguori
2631 5d0c5750 aliguori
    if (strstart(devname, "host:", &p))
2632 5d0c5750 aliguori
        return usb_host_device_close(p);
2633 5d0c5750 aliguori
2634 1f3870ab aliguori
    if (!used_usb_ports)
2635 1f3870ab aliguori
        return -1;
2636 1f3870ab aliguori
2637 1f3870ab aliguori
    p = strchr(devname, '.');
2638 1f3870ab aliguori
    if (!p)
2639 1f3870ab aliguori
        return -1;
2640 1f3870ab aliguori
    bus_num = strtoul(devname, NULL, 0);
2641 1f3870ab aliguori
    addr = strtoul(p + 1, NULL, 0);
2642 1f3870ab aliguori
2643 1f3870ab aliguori
    return usb_device_del_addr(bus_num, addr);
2644 1f3870ab aliguori
}
2645 1f3870ab aliguori
2646 a594cfbf bellard
void do_usb_add(const char *devname)
2647 a594cfbf bellard
{
2648 4b096fc9 aliguori
    usb_device_add(devname);
2649 a594cfbf bellard
}
2650 a594cfbf bellard
2651 a594cfbf bellard
void do_usb_del(const char *devname)
2652 a594cfbf bellard
{
2653 4b096fc9 aliguori
    usb_device_del(devname);
2654 a594cfbf bellard
}
2655 a594cfbf bellard
2656 a594cfbf bellard
void usb_info(void)
2657 a594cfbf bellard
{
2658 a594cfbf bellard
    USBDevice *dev;
2659 0d92ed30 pbrook
    USBPort *port;
2660 a594cfbf bellard
    const char *speed_str;
2661 a594cfbf bellard
2662 0d92ed30 pbrook
    if (!usb_enabled) {
2663 a594cfbf bellard
        term_printf("USB support not enabled\n");
2664 a594cfbf bellard
        return;
2665 a594cfbf bellard
    }
2666 a594cfbf bellard
2667 0d92ed30 pbrook
    for (port = used_usb_ports; port; port = port->next) {
2668 0d92ed30 pbrook
        dev = port->dev;
2669 0d92ed30 pbrook
        if (!dev)
2670 0d92ed30 pbrook
            continue;
2671 0d92ed30 pbrook
        switch(dev->speed) {
2672 5fafdf24 ths
        case USB_SPEED_LOW:
2673 5fafdf24 ths
            speed_str = "1.5";
2674 0d92ed30 pbrook
            break;
2675 5fafdf24 ths
        case USB_SPEED_FULL:
2676 5fafdf24 ths
            speed_str = "12";
2677 0d92ed30 pbrook
            break;
2678 5fafdf24 ths
        case USB_SPEED_HIGH:
2679 5fafdf24 ths
            speed_str = "480";
2680 0d92ed30 pbrook
            break;
2681 0d92ed30 pbrook
        default:
2682 5fafdf24 ths
            speed_str = "?";
2683 0d92ed30 pbrook
            break;
2684 a594cfbf bellard
        }
2685 5fafdf24 ths
        term_printf("  Device %d.%d, Speed %s Mb/s, Product %s\n",
2686 1f6e24e7 bellard
                    0, dev->addr, speed_str, dev->devname);
2687 a594cfbf bellard
    }
2688 a594cfbf bellard
}
2689 a594cfbf bellard
2690 f7cce898 bellard
/***********************************************************/
2691 201a51fc balrog
/* PCMCIA/Cardbus */
2692 201a51fc balrog
2693 201a51fc balrog
static struct pcmcia_socket_entry_s {
2694 201a51fc balrog
    struct pcmcia_socket_s *socket;
2695 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
2696 201a51fc balrog
} *pcmcia_sockets = 0;
2697 201a51fc balrog
2698 201a51fc balrog
void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2699 201a51fc balrog
{
2700 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
2701 201a51fc balrog
2702 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2703 201a51fc balrog
    entry->socket = socket;
2704 201a51fc balrog
    entry->next = pcmcia_sockets;
2705 201a51fc balrog
    pcmcia_sockets = entry;
2706 201a51fc balrog
}
2707 201a51fc balrog
2708 201a51fc balrog
void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2709 201a51fc balrog
{
2710 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
2711 201a51fc balrog
2712 201a51fc balrog
    ptr = &pcmcia_sockets;
2713 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2714 201a51fc balrog
        if (entry->socket == socket) {
2715 201a51fc balrog
            *ptr = entry->next;
2716 201a51fc balrog
            qemu_free(entry);
2717 201a51fc balrog
        }
2718 201a51fc balrog
}
2719 201a51fc balrog
2720 201a51fc balrog
void pcmcia_info(void)
2721 201a51fc balrog
{
2722 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
2723 201a51fc balrog
    if (!pcmcia_sockets)
2724 201a51fc balrog
        term_printf("No PCMCIA sockets\n");
2725 201a51fc balrog
2726 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
2727 201a51fc balrog
        term_printf("%s: %s\n", iter->socket->slot_string,
2728 201a51fc balrog
                    iter->socket->attached ? iter->socket->card_string :
2729 201a51fc balrog
                    "Empty");
2730 201a51fc balrog
}
2731 201a51fc balrog
2732 201a51fc balrog
/***********************************************************/
2733 2ff89790 ths
/* dumb display */
2734 2ff89790 ths
2735 2ff89790 ths
static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2736 2ff89790 ths
{
2737 2ff89790 ths
}
2738 2ff89790 ths
2739 2ff89790 ths
static void dumb_resize(DisplayState *ds, int w, int h)
2740 2ff89790 ths
{
2741 2ff89790 ths
}
2742 2ff89790 ths
2743 2ff89790 ths
static void dumb_display_init(DisplayState *ds)
2744 2ff89790 ths
{
2745 2ff89790 ths
    ds->data = NULL;
2746 2ff89790 ths
    ds->linesize = 0;
2747 2ff89790 ths
    ds->depth = 0;
2748 2ff89790 ths
    ds->dpy_update = dumb_update;
2749 2ff89790 ths
    ds->dpy_resize = dumb_resize;
2750 2ad1a437 aliguori
    ds->dpy_refresh = NULL;
2751 2ad1a437 aliguori
    ds->gui_timer_interval = 0;
2752 bcfad70f aliguori
    ds->idle = 1;
2753 2ff89790 ths
}
2754 2ff89790 ths
2755 2ff89790 ths
/***********************************************************/
2756 8a7ddc38 bellard
/* I/O handling */
2757 0824d6fc bellard
2758 c4b1fcc0 bellard
#define MAX_IO_HANDLERS 64
2759 c4b1fcc0 bellard
2760 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
2761 c4b1fcc0 bellard
    int fd;
2762 7c9d8e07 bellard
    IOCanRWHandler *fd_read_poll;
2763 7c9d8e07 bellard
    IOHandler *fd_read;
2764 7c9d8e07 bellard
    IOHandler *fd_write;
2765 cafffd40 ths
    int deleted;
2766 c4b1fcc0 bellard
    void *opaque;
2767 c4b1fcc0 bellard
    /* temporary data */
2768 c4b1fcc0 bellard
    struct pollfd *ufd;
2769 8a7ddc38 bellard
    struct IOHandlerRecord *next;
2770 c4b1fcc0 bellard
} IOHandlerRecord;
2771 c4b1fcc0 bellard
2772 8a7ddc38 bellard
static IOHandlerRecord *first_io_handler;
2773 c4b1fcc0 bellard
2774 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
2775 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
2776 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
2777 5fafdf24 ths
                         IOCanRWHandler *fd_read_poll,
2778 5fafdf24 ths
                         IOHandler *fd_read,
2779 5fafdf24 ths
                         IOHandler *fd_write,
2780 7c9d8e07 bellard
                         void *opaque)
2781 c4b1fcc0 bellard
{
2782 7c9d8e07 bellard
    IOHandlerRecord **pioh, *ioh;
2783 c4b1fcc0 bellard
2784 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
2785 7c9d8e07 bellard
        pioh = &first_io_handler;
2786 7c9d8e07 bellard
        for(;;) {
2787 7c9d8e07 bellard
            ioh = *pioh;
2788 7c9d8e07 bellard
            if (ioh == NULL)
2789 7c9d8e07 bellard
                break;
2790 7c9d8e07 bellard
            if (ioh->fd == fd) {
2791 cafffd40 ths
                ioh->deleted = 1;
2792 7c9d8e07 bellard
                break;
2793 7c9d8e07 bellard
            }
2794 7c9d8e07 bellard
            pioh = &ioh->next;
2795 7c9d8e07 bellard
        }
2796 7c9d8e07 bellard
    } else {
2797 7c9d8e07 bellard
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2798 7c9d8e07 bellard
            if (ioh->fd == fd)
2799 7c9d8e07 bellard
                goto found;
2800 7c9d8e07 bellard
        }
2801 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2802 7c9d8e07 bellard
        if (!ioh)
2803 7c9d8e07 bellard
            return -1;
2804 7c9d8e07 bellard
        ioh->next = first_io_handler;
2805 7c9d8e07 bellard
        first_io_handler = ioh;
2806 7c9d8e07 bellard
    found:
2807 7c9d8e07 bellard
        ioh->fd = fd;
2808 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
2809 7c9d8e07 bellard
        ioh->fd_read = fd_read;
2810 7c9d8e07 bellard
        ioh->fd_write = fd_write;
2811 7c9d8e07 bellard
        ioh->opaque = opaque;
2812 cafffd40 ths
        ioh->deleted = 0;
2813 7c9d8e07 bellard
    }
2814 c4b1fcc0 bellard
    return 0;
2815 c4b1fcc0 bellard
}
2816 c4b1fcc0 bellard
2817 5fafdf24 ths
int qemu_set_fd_handler(int fd,
2818 5fafdf24 ths
                        IOHandler *fd_read,
2819 5fafdf24 ths
                        IOHandler *fd_write,
2820 7c9d8e07 bellard
                        void *opaque)
2821 8a7ddc38 bellard
{
2822 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2823 8a7ddc38 bellard
}
2824 8a7ddc38 bellard
2825 56f3a5d0 aliguori
#ifdef _WIN32
2826 8a7ddc38 bellard
/***********************************************************/
2827 f331110f bellard
/* Polling handling */
2828 f331110f bellard
2829 f331110f bellard
typedef struct PollingEntry {
2830 f331110f bellard
    PollingFunc *func;
2831 f331110f bellard
    void *opaque;
2832 f331110f bellard
    struct PollingEntry *next;
2833 f331110f bellard
} PollingEntry;
2834 f331110f bellard
2835 f331110f bellard
static PollingEntry *first_polling_entry;
2836 f331110f bellard
2837 f331110f bellard
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2838 f331110f bellard
{
2839 f331110f bellard
    PollingEntry **ppe, *pe;
2840 f331110f bellard
    pe = qemu_mallocz(sizeof(PollingEntry));
2841 f331110f bellard
    if (!pe)
2842 f331110f bellard
        return -1;
2843 f331110f bellard
    pe->func = func;
2844 f331110f bellard
    pe->opaque = opaque;
2845 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2846 f331110f bellard
    *ppe = pe;
2847 f331110f bellard
    return 0;
2848 f331110f bellard
}
2849 f331110f bellard
2850 f331110f bellard
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2851 f331110f bellard
{
2852 f331110f bellard
    PollingEntry **ppe, *pe;
2853 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2854 f331110f bellard
        pe = *ppe;
2855 f331110f bellard
        if (pe->func == func && pe->opaque == opaque) {
2856 f331110f bellard
            *ppe = pe->next;
2857 f331110f bellard
            qemu_free(pe);
2858 f331110f bellard
            break;
2859 f331110f bellard
        }
2860 f331110f bellard
    }
2861 f331110f bellard
}
2862 f331110f bellard
2863 a18e524a bellard
/***********************************************************/
2864 a18e524a bellard
/* Wait objects support */
2865 a18e524a bellard
typedef struct WaitObjects {
2866 a18e524a bellard
    int num;
2867 a18e524a bellard
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2868 a18e524a bellard
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2869 a18e524a bellard
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2870 a18e524a bellard
} WaitObjects;
2871 a18e524a bellard
2872 a18e524a bellard
static WaitObjects wait_objects = {0};
2873 3b46e624 ths
2874 a18e524a bellard
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2875 a18e524a bellard
{
2876 a18e524a bellard
    WaitObjects *w = &wait_objects;
2877 a18e524a bellard
2878 a18e524a bellard
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
2879 a18e524a bellard
        return -1;
2880 a18e524a bellard
    w->events[w->num] = handle;
2881 a18e524a bellard
    w->func[w->num] = func;
2882 a18e524a bellard
    w->opaque[w->num] = opaque;
2883 a18e524a bellard
    w->num++;
2884 a18e524a bellard
    return 0;
2885 a18e524a bellard
}
2886 a18e524a bellard
2887 a18e524a bellard
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2888 a18e524a bellard
{
2889 a18e524a bellard
    int i, found;
2890 a18e524a bellard
    WaitObjects *w = &wait_objects;
2891 a18e524a bellard
2892 a18e524a bellard
    found = 0;
2893 a18e524a bellard
    for (i = 0; i < w->num; i++) {
2894 a18e524a bellard
        if (w->events[i] == handle)
2895 a18e524a bellard
            found = 1;
2896 a18e524a bellard
        if (found) {
2897 a18e524a bellard
            w->events[i] = w->events[i + 1];
2898 a18e524a bellard
            w->func[i] = w->func[i + 1];
2899 a18e524a bellard
            w->opaque[i] = w->opaque[i + 1];
2900 3b46e624 ths
        }
2901 a18e524a bellard
    }
2902 a18e524a bellard
    if (found)
2903 a18e524a bellard
        w->num--;
2904 a18e524a bellard
}
2905 a18e524a bellard
#endif
2906 a18e524a bellard
2907 210f41ba aliguori
#define SELF_ANNOUNCE_ROUNDS 5
2908 210f41ba aliguori
#define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
2909 210f41ba aliguori
//#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
2910 210f41ba aliguori
#define EXPERIMENTAL_MAGIC 0xf1f23f4f
2911 210f41ba aliguori
2912 210f41ba aliguori
static int announce_self_create(uint8_t *buf, 
2913 210f41ba aliguori
                                uint8_t *mac_addr)
2914 210f41ba aliguori
{
2915 210f41ba aliguori
    uint32_t magic = EXPERIMENTAL_MAGIC;
2916 210f41ba aliguori
    uint16_t proto = htons(ETH_P_EXPERIMENTAL);
2917 210f41ba aliguori
2918 210f41ba aliguori
    /* FIXME: should we send a different packet (arp/rarp/ping)? */
2919 210f41ba aliguori
2920 210f41ba aliguori
    memset(buf, 0xff, 6);         /* h_dst */
2921 210f41ba aliguori
    memcpy(buf + 6, mac_addr, 6); /* h_src */
2922 210f41ba aliguori
    memcpy(buf + 12, &proto, 2);  /* h_proto */
2923 210f41ba aliguori
    memcpy(buf + 14, &magic, 4);  /* magic */
2924 210f41ba aliguori
2925 210f41ba aliguori
    return 18; /* len */
2926 210f41ba aliguori
}
2927 210f41ba aliguori
2928 210f41ba aliguori
void qemu_announce_self(void)
2929 210f41ba aliguori
{
2930 210f41ba aliguori
    int i, j, len;
2931 210f41ba aliguori
    VLANState *vlan;
2932 210f41ba aliguori
    VLANClientState *vc;
2933 210f41ba aliguori
    uint8_t buf[256];
2934 210f41ba aliguori
2935 210f41ba aliguori
    for (i = 0; i < nb_nics; i++) {
2936 210f41ba aliguori
        len = announce_self_create(buf, nd_table[i].macaddr);
2937 210f41ba aliguori
        vlan = nd_table[i].vlan;
2938 210f41ba aliguori
        for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
2939 17e90973 aliguori
            for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++)
2940 17e90973 aliguori
                vc->fd_read(vc->opaque, buf, len);
2941 210f41ba aliguori
        }
2942 210f41ba aliguori
    }
2943 210f41ba aliguori
}
2944 210f41ba aliguori
2945 f331110f bellard
/***********************************************************/
2946 8a7ddc38 bellard
/* savevm/loadvm support */
2947 8a7ddc38 bellard
2948 faea38e7 bellard
#define IO_BUF_SIZE 32768
2949 faea38e7 bellard
2950 faea38e7 bellard
struct QEMUFile {
2951 5dafc53f aliguori
    QEMUFilePutBufferFunc *put_buffer;
2952 5dafc53f aliguori
    QEMUFileGetBufferFunc *get_buffer;
2953 5dafc53f aliguori
    QEMUFileCloseFunc *close;
2954 5dafc53f aliguori
    QEMUFileRateLimit *rate_limit;
2955 5dafc53f aliguori
    void *opaque;
2956 871d2f07 aliguori
    int is_write;
2957 5dafc53f aliguori
2958 faea38e7 bellard
    int64_t buf_offset; /* start of buffer when writing, end of buffer
2959 faea38e7 bellard
                           when reading */
2960 faea38e7 bellard
    int buf_index;
2961 faea38e7 bellard
    int buf_size; /* 0 when writing */
2962 faea38e7 bellard
    uint8_t buf[IO_BUF_SIZE];
2963 871d2f07 aliguori
2964 871d2f07 aliguori
    int has_error;
2965 faea38e7 bellard
};
2966 faea38e7 bellard
2967 c1d36665 aliguori
typedef struct QEMUFileSocket
2968 5dafc53f aliguori
{
2969 5dafc53f aliguori
    int fd;
2970 5dafc53f aliguori
    QEMUFile *file;
2971 c1d36665 aliguori
} QEMUFileSocket;
2972 5dafc53f aliguori
2973 c1d36665 aliguori
static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
2974 5dafc53f aliguori
{
2975 c1d36665 aliguori
    QEMUFileSocket *s = opaque;
2976 5dafc53f aliguori
    ssize_t len;
2977 5dafc53f aliguori
2978 5dafc53f aliguori
    do {
2979 c1d36665 aliguori
        len = recv(s->fd, buf, size, 0);
2980 c1d36665 aliguori
    } while (len == -1 && socket_error() == EINTR);
2981 5dafc53f aliguori
2982 5dafc53f aliguori
    if (len == -1)
2983 c1d36665 aliguori
        len = -socket_error();
2984 5dafc53f aliguori
2985 5dafc53f aliguori
    return len;
2986 5dafc53f aliguori
}
2987 5dafc53f aliguori
2988 c1d36665 aliguori
static int socket_close(void *opaque)
2989 5dafc53f aliguori
{
2990 c1d36665 aliguori
    QEMUFileSocket *s = opaque;
2991 5dafc53f aliguori
    qemu_free(s);
2992 5dafc53f aliguori
    return 0;
2993 5dafc53f aliguori
}
2994 5dafc53f aliguori
2995 c1d36665 aliguori
QEMUFile *qemu_fopen_socket(int fd)
2996 5dafc53f aliguori
{
2997 c1d36665 aliguori
    QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
2998 5dafc53f aliguori
2999 5dafc53f aliguori
    if (s == NULL)
3000 5dafc53f aliguori
        return NULL;
3001 5dafc53f aliguori
3002 5dafc53f aliguori
    s->fd = fd;
3003 c1d36665 aliguori
    s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
3004 5dafc53f aliguori
    return s->file;
3005 5dafc53f aliguori
}
3006 5dafc53f aliguori
3007 5dafc53f aliguori
typedef struct QEMUFileStdio
3008 5dafc53f aliguori
{
3009 5dafc53f aliguori
    FILE *outfile;
3010 5dafc53f aliguori
} QEMUFileStdio;
3011 5dafc53f aliguori
3012 871d2f07 aliguori
static int file_put_buffer(void *opaque, const uint8_t *buf,
3013 5dafc53f aliguori
                            int64_t pos, int size)
3014 5dafc53f aliguori
{
3015 5dafc53f aliguori
    QEMUFileStdio *s = opaque;
3016 5dafc53f aliguori
    fseek(s->outfile, pos, SEEK_SET);
3017 5dafc53f aliguori
    fwrite(buf, 1, size, s->outfile);
3018 871d2f07 aliguori
    return size;
3019 5dafc53f aliguori
}
3020 5dafc53f aliguori
3021 5dafc53f aliguori
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
3022 5dafc53f aliguori
{
3023 5dafc53f aliguori
    QEMUFileStdio *s = opaque;
3024 5dafc53f aliguori
    fseek(s->outfile, pos, SEEK_SET);
3025 5dafc53f aliguori
    return fread(buf, 1, size, s->outfile);
3026 5dafc53f aliguori
}
3027 5dafc53f aliguori
3028 5dafc53f aliguori
static int file_close(void *opaque)
3029 5dafc53f aliguori
{
3030 5dafc53f aliguori
    QEMUFileStdio *s = opaque;
3031 5dafc53f aliguori
    fclose(s->outfile);
3032 5dafc53f aliguori
    qemu_free(s);
3033 5dafc53f aliguori
    return 0;
3034 5dafc53f aliguori
}
3035 5dafc53f aliguori
3036 faea38e7 bellard
QEMUFile *qemu_fopen(const char *filename, const char *mode)
3037 faea38e7 bellard
{
3038 5dafc53f aliguori
    QEMUFileStdio *s;
3039 faea38e7 bellard
3040 5dafc53f aliguori
    s = qemu_mallocz(sizeof(QEMUFileStdio));
3041 5dafc53f aliguori
    if (!s)
3042 faea38e7 bellard
        return NULL;
3043 5dafc53f aliguori
3044 5dafc53f aliguori
    s->outfile = fopen(filename, mode);
3045 5dafc53f aliguori
    if (!s->outfile)
3046 faea38e7 bellard
        goto fail;
3047 5dafc53f aliguori
3048 5dafc53f aliguori
    if (!strcmp(mode, "wb"))
3049 5dafc53f aliguori
        return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
3050 5dafc53f aliguori
    else if (!strcmp(mode, "rb"))
3051 5dafc53f aliguori
        return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
3052 5dafc53f aliguori
3053 5dafc53f aliguori
fail:
3054 5dafc53f aliguori
    if (s->outfile)
3055 5dafc53f aliguori
        fclose(s->outfile);
3056 5dafc53f aliguori
    qemu_free(s);
3057 faea38e7 bellard
    return NULL;
3058 faea38e7 bellard
}
3059 faea38e7 bellard
3060 5dafc53f aliguori
typedef struct QEMUFileBdrv
3061 5dafc53f aliguori
{
3062 5dafc53f aliguori
    BlockDriverState *bs;
3063 5dafc53f aliguori
    int64_t base_offset;
3064 5dafc53f aliguori
} QEMUFileBdrv;
3065 5dafc53f aliguori
3066 871d2f07 aliguori
static int bdrv_put_buffer(void *opaque, const uint8_t *buf,
3067 871d2f07 aliguori
                           int64_t pos, int size)
3068 5dafc53f aliguori
{
3069 5dafc53f aliguori
    QEMUFileBdrv *s = opaque;
3070 5dafc53f aliguori
    bdrv_pwrite(s->bs, s->base_offset + pos, buf, size);
3071 871d2f07 aliguori
    return size;
3072 5dafc53f aliguori
}
3073 5dafc53f aliguori
3074 5dafc53f aliguori
static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
3075 5dafc53f aliguori
{
3076 5dafc53f aliguori
    QEMUFileBdrv *s = opaque;
3077 5dafc53f aliguori
    return bdrv_pread(s->bs, s->base_offset + pos, buf, size);
3078 5dafc53f aliguori
}
3079 5dafc53f aliguori
3080 5dafc53f aliguori
static int bdrv_fclose(void *opaque)
3081 5dafc53f aliguori
{
3082 5dafc53f aliguori
    QEMUFileBdrv *s = opaque;
3083 5dafc53f aliguori
    qemu_free(s);
3084 5dafc53f aliguori
    return 0;
3085 5dafc53f aliguori
}
3086 5dafc53f aliguori
3087 674bb261 blueswir1
static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
3088 5dafc53f aliguori
{
3089 5dafc53f aliguori
    QEMUFileBdrv *s;
3090 5dafc53f aliguori
3091 5dafc53f aliguori
    s = qemu_mallocz(sizeof(QEMUFileBdrv));
3092 5dafc53f aliguori
    if (!s)
3093 5dafc53f aliguori
        return NULL;
3094 5dafc53f aliguori
3095 5dafc53f aliguori
    s->bs = bs;
3096 5dafc53f aliguori
    s->base_offset = offset;
3097 5dafc53f aliguori
3098 5dafc53f aliguori
    if (is_writable)
3099 5dafc53f aliguori
        return qemu_fopen_ops(s, bdrv_put_buffer, NULL, bdrv_fclose, NULL);
3100 5dafc53f aliguori
3101 5dafc53f aliguori
    return qemu_fopen_ops(s, NULL, bdrv_get_buffer, bdrv_fclose, NULL);
3102 5dafc53f aliguori
}
3103 5dafc53f aliguori
3104 5dafc53f aliguori
QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
3105 5dafc53f aliguori
                         QEMUFileGetBufferFunc *get_buffer,
3106 5dafc53f aliguori
                         QEMUFileCloseFunc *close,
3107 5dafc53f aliguori
                         QEMUFileRateLimit *rate_limit)
3108 faea38e7 bellard
{
3109 faea38e7 bellard
    QEMUFile *f;
3110 faea38e7 bellard
3111 faea38e7 bellard
    f = qemu_mallocz(sizeof(QEMUFile));
3112 faea38e7 bellard
    if (!f)
3113 faea38e7 bellard
        return NULL;
3114 5dafc53f aliguori
3115 5dafc53f aliguori
    f->opaque = opaque;
3116 5dafc53f aliguori
    f->put_buffer = put_buffer;
3117 5dafc53f aliguori
    f->get_buffer = get_buffer;
3118 5dafc53f aliguori
    f->close = close;
3119 5dafc53f aliguori
    f->rate_limit = rate_limit;
3120 871d2f07 aliguori
    f->is_write = 0;
3121 5dafc53f aliguori
3122 faea38e7 bellard
    return f;
3123 faea38e7 bellard
}
3124 faea38e7 bellard
3125 871d2f07 aliguori
int qemu_file_has_error(QEMUFile *f)
3126 871d2f07 aliguori
{
3127 871d2f07 aliguori
    return f->has_error;
3128 871d2f07 aliguori
}
3129 871d2f07 aliguori
3130 faea38e7 bellard
void qemu_fflush(QEMUFile *f)
3131 faea38e7 bellard
{
3132 5dafc53f aliguori
    if (!f->put_buffer)
3133 faea38e7 bellard
        return;
3134 5dafc53f aliguori
3135 871d2f07 aliguori
    if (f->is_write && f->buf_index > 0) {
3136 871d2f07 aliguori
        int len;
3137 871d2f07 aliguori
3138 871d2f07 aliguori
        len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
3139 871d2f07 aliguori
        if (len > 0)
3140 871d2f07 aliguori
            f->buf_offset += f->buf_index;
3141 871d2f07 aliguori
        else
3142 871d2f07 aliguori
            f->has_error = 1;
3143 faea38e7 bellard
        f->buf_index = 0;
3144 faea38e7 bellard
    }
3145 faea38e7 bellard
}
3146 faea38e7 bellard
3147 faea38e7 bellard
static void qemu_fill_buffer(QEMUFile *f)
3148 faea38e7 bellard
{
3149 faea38e7 bellard
    int len;
3150 faea38e7 bellard
3151 5dafc53f aliguori
    if (!f->get_buffer)
3152 faea38e7 bellard
        return;
3153 5dafc53f aliguori
3154 871d2f07 aliguori
    if (f->is_write)
3155 871d2f07 aliguori
        abort();
3156 5dafc53f aliguori
3157 871d2f07 aliguori
    len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
3158 871d2f07 aliguori
    if (len > 0) {
3159 871d2f07 aliguori
        f->buf_index = 0;
3160 871d2f07 aliguori
        f->buf_size = len;
3161 871d2f07 aliguori
        f->buf_offset += len;
3162 871d2f07 aliguori
    } else if (len != -EAGAIN)
3163 871d2f07 aliguori
        f->has_error = 1;
3164 faea38e7 bellard
}
3165 faea38e7 bellard
3166 5dafc53f aliguori
int qemu_fclose(QEMUFile *f)
3167 faea38e7 bellard
{
3168 5dafc53f aliguori
    int ret = 0;
3169 5dafc53f aliguori
    qemu_fflush(f);
3170 5dafc53f aliguori
    if (f->close)
3171 5dafc53f aliguori
        ret = f->close(f->opaque);
3172 faea38e7 bellard
    qemu_free(f);
3173 5dafc53f aliguori
    return ret;
3174 5dafc53f aliguori
}
3175 5dafc53f aliguori
3176 5dafc53f aliguori
void qemu_file_put_notify(QEMUFile *f)
3177 5dafc53f aliguori
{
3178 5dafc53f aliguori
    f->put_buffer(f->opaque, NULL, 0, 0);
3179 faea38e7 bellard
}
3180 faea38e7 bellard
3181 2ca83a8d blueswir1
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
3182 b4608c04 bellard
{
3183 2ca83a8d blueswir1
    int l;
3184 871d2f07 aliguori
3185 871d2f07 aliguori
    if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
3186 871d2f07 aliguori
        fprintf(stderr,
3187 871d2f07 aliguori
                "Attempted to write to buffer while read buffer is not empty\n");
3188 871d2f07 aliguori
        abort();
3189 871d2f07 aliguori
    }
3190 871d2f07 aliguori
3191 871d2f07 aliguori
    while (!f->has_error && size > 0) {
3192 faea38e7 bellard
        l = IO_BUF_SIZE - f->buf_index;
3193 faea38e7 bellard
        if (l > size)
3194 faea38e7 bellard
            l = size;
3195 faea38e7 bellard
        memcpy(f->buf + f->buf_index, buf, l);
3196 871d2f07 aliguori
        f->is_write = 1;
3197 faea38e7 bellard
        f->buf_index += l;
3198 faea38e7 bellard
        buf += l;
3199 faea38e7 bellard
        size -= l;
3200 faea38e7 bellard
        if (f->buf_index >= IO_BUF_SIZE)
3201 faea38e7 bellard
            qemu_fflush(f);
3202 faea38e7 bellard
    }
3203 b4608c04 bellard
}
3204 b4608c04 bellard
3205 2ca83a8d blueswir1
void qemu_put_byte(QEMUFile *f, int v)
3206 b4608c04 bellard
{
3207 871d2f07 aliguori
    if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
3208 871d2f07 aliguori
        fprintf(stderr,
3209 871d2f07 aliguori
                "Attempted to write to buffer while read buffer is not empty\n");
3210 871d2f07 aliguori
        abort();
3211 871d2f07 aliguori
    }
3212 871d2f07 aliguori
3213 faea38e7 bellard
    f->buf[f->buf_index++] = v;
3214 871d2f07 aliguori
    f->is_write = 1;
3215 faea38e7 bellard
    if (f->buf_index >= IO_BUF_SIZE)
3216 faea38e7 bellard
        qemu_fflush(f);
3217 faea38e7 bellard
}
3218 faea38e7 bellard
3219 2ca83a8d blueswir1
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
3220 faea38e7 bellard
{
3221 2ca83a8d blueswir1
    int size, l;
3222 faea38e7 bellard
3223 871d2f07 aliguori
    if (f->is_write)
3224 871d2f07 aliguori
        abort();
3225 871d2f07 aliguori
3226 faea38e7 bellard
    size = size1;
3227 faea38e7 bellard
    while (size > 0) {
3228 faea38e7 bellard
        l = f->buf_size - f->buf_index;
3229 faea38e7 bellard
        if (l == 0) {
3230 faea38e7 bellard
            qemu_fill_buffer(f);
3231 faea38e7 bellard
            l = f->buf_size - f->buf_index;
3232 faea38e7 bellard
            if (l == 0)
3233 faea38e7 bellard
                break;
3234 faea38e7 bellard
        }
3235 faea38e7 bellard
        if (l > size)
3236 faea38e7 bellard
            l = size;
3237 faea38e7 bellard
        memcpy(buf, f->buf + f->buf_index, l);
3238 faea38e7 bellard
        f->buf_index += l;
3239 faea38e7 bellard
        buf += l;
3240 faea38e7 bellard
        size -= l;
3241 faea38e7 bellard
    }
3242 faea38e7 bellard
    return size1 - size;
3243 faea38e7 bellard
}
3244 faea38e7 bellard
3245 2ca83a8d blueswir1
int qemu_get_byte(QEMUFile *f)
3246 faea38e7 bellard
{
3247 871d2f07 aliguori
    if (f->is_write)
3248 871d2f07 aliguori
        abort();
3249 871d2f07 aliguori
3250 faea38e7 bellard
    if (f->buf_index >= f->buf_size) {
3251 faea38e7 bellard
        qemu_fill_buffer(f);
3252 faea38e7 bellard
        if (f->buf_index >= f->buf_size)
3253 faea38e7 bellard
            return 0;
3254 faea38e7 bellard
    }
3255 faea38e7 bellard
    return f->buf[f->buf_index++];
3256 faea38e7 bellard
}
3257 faea38e7 bellard
3258 faea38e7 bellard
int64_t qemu_ftell(QEMUFile *f)
3259 faea38e7 bellard
{
3260 faea38e7 bellard
    return f->buf_offset - f->buf_size + f->buf_index;
3261 faea38e7 bellard
}
3262 faea38e7 bellard
3263 faea38e7 bellard
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
3264 faea38e7 bellard
{
3265 faea38e7 bellard
    if (whence == SEEK_SET) {
3266 faea38e7 bellard
        /* nothing to do */
3267 faea38e7 bellard
    } else if (whence == SEEK_CUR) {
3268 faea38e7 bellard
        pos += qemu_ftell(f);
3269 faea38e7 bellard
    } else {
3270 faea38e7 bellard
        /* SEEK_END not supported */
3271 faea38e7 bellard
        return -1;
3272 faea38e7 bellard
    }
3273 5dafc53f aliguori
    if (f->put_buffer) {
3274 faea38e7 bellard
        qemu_fflush(f);
3275 faea38e7 bellard
        f->buf_offset = pos;
3276 faea38e7 bellard
    } else {
3277 faea38e7 bellard
        f->buf_offset = pos;
3278 faea38e7 bellard
        f->buf_index = 0;
3279 faea38e7 bellard
        f->buf_size = 0;
3280 faea38e7 bellard
    }
3281 faea38e7 bellard
    return pos;
3282 8a7ddc38 bellard
}
3283 8a7ddc38 bellard
3284 5dafc53f aliguori
int qemu_file_rate_limit(QEMUFile *f)
3285 5dafc53f aliguori
{
3286 5dafc53f aliguori
    if (f->rate_limit)
3287 5dafc53f aliguori
        return f->rate_limit(f->opaque);
3288 5dafc53f aliguori
3289 5dafc53f aliguori
    return 0;
3290 5dafc53f aliguori
}
3291 5dafc53f aliguori
3292 2ca83a8d blueswir1
void qemu_put_be16(QEMUFile *f, unsigned int v)
3293 8a7ddc38 bellard
{
3294 8a7ddc38 bellard
    qemu_put_byte(f, v >> 8);
3295 8a7ddc38 bellard
    qemu_put_byte(f, v);
3296 8a7ddc38 bellard
}
3297 8a7ddc38 bellard
3298 2ca83a8d blueswir1
void qemu_put_be32(QEMUFile *f, unsigned int v)
3299 8a7ddc38 bellard
{
3300 8a7ddc38 bellard
    qemu_put_byte(f, v >> 24);
3301 8a7ddc38 bellard
    qemu_put_byte(f, v >> 16);
3302 8a7ddc38 bellard
    qemu_put_byte(f, v >> 8);
3303 8a7ddc38 bellard
    qemu_put_byte(f, v);
3304 8a7ddc38 bellard
}
3305 8a7ddc38 bellard
3306 8a7ddc38 bellard
void qemu_put_be64(QEMUFile *f, uint64_t v)
3307 8a7ddc38 bellard
{
3308 8a7ddc38 bellard
    qemu_put_be32(f, v >> 32);
3309 8a7ddc38 bellard
    qemu_put_be32(f, v);
3310 8a7ddc38 bellard
}
3311 8a7ddc38 bellard
3312 2ca83a8d blueswir1
unsigned int qemu_get_be16(QEMUFile *f)
3313 8a7ddc38 bellard
{
3314 2ca83a8d blueswir1
    unsigned int v;
3315 8a7ddc38 bellard
    v = qemu_get_byte(f) << 8;
3316 8a7ddc38 bellard
    v |= qemu_get_byte(f);
3317 8a7ddc38 bellard
    return v;
3318 8a7ddc38 bellard
}
3319 8a7ddc38 bellard
3320 2ca83a8d blueswir1
unsigned int qemu_get_be32(QEMUFile *f)
3321 8a7ddc38 bellard
{
3322 2ca83a8d blueswir1
    unsigned int v;
3323 8a7ddc38 bellard
    v = qemu_get_byte(f) << 24;
3324 8a7ddc38 bellard
    v |= qemu_get_byte(f) << 16;
3325 8a7ddc38 bellard
    v |= qemu_get_byte(f) << 8;
3326 8a7ddc38 bellard
    v |= qemu_get_byte(f);
3327 8a7ddc38 bellard
    return v;
3328 8a7ddc38 bellard
}
3329 8a7ddc38 bellard
3330 8a7ddc38 bellard
uint64_t qemu_get_be64(QEMUFile *f)
3331 8a7ddc38 bellard
{
3332 8a7ddc38 bellard
    uint64_t v;
3333 8a7ddc38 bellard
    v = (uint64_t)qemu_get_be32(f) << 32;
3334 8a7ddc38 bellard
    v |= qemu_get_be32(f);
3335 8a7ddc38 bellard
    return v;
3336 8a7ddc38 bellard
}
3337 8a7ddc38 bellard
3338 8a7ddc38 bellard
typedef struct SaveStateEntry {
3339 8a7ddc38 bellard
    char idstr[256];
3340 8a7ddc38 bellard
    int instance_id;
3341 8a7ddc38 bellard
    int version_id;
3342 9366f418 aliguori
    int section_id;
3343 9366f418 aliguori
    SaveLiveStateHandler *save_live_state;
3344 8a7ddc38 bellard
    SaveStateHandler *save_state;
3345 8a7ddc38 bellard
    LoadStateHandler *load_state;
3346 8a7ddc38 bellard
    void *opaque;
3347 8a7ddc38 bellard
    struct SaveStateEntry *next;
3348 8a7ddc38 bellard
} SaveStateEntry;
3349 b4608c04 bellard
3350 8a7ddc38 bellard
static SaveStateEntry *first_se;
3351 8a7ddc38 bellard
3352 18be5187 pbrook
/* TODO: Individual devices generally have very little idea about the rest
3353 18fdb1c5 ths
   of the system, so instance_id should be removed/replaced.
3354 18fdb1c5 ths
   Meanwhile pass -1 as instance_id if you do not already have a clearly
3355 18fdb1c5 ths
   distinguishing id for all instances of your device class. */
3356 9366f418 aliguori
int register_savevm_live(const char *idstr,
3357 9366f418 aliguori
                         int instance_id,
3358 9366f418 aliguori
                         int version_id,
3359 9366f418 aliguori
                         SaveLiveStateHandler *save_live_state,
3360 9366f418 aliguori
                         SaveStateHandler *save_state,
3361 9366f418 aliguori
                         LoadStateHandler *load_state,
3362 9366f418 aliguori
                         void *opaque)
3363 8a7ddc38 bellard
{
3364 8a7ddc38 bellard
    SaveStateEntry *se, **pse;
3365 9366f418 aliguori
    static int global_section_id;
3366 8a7ddc38 bellard
3367 8a7ddc38 bellard
    se = qemu_malloc(sizeof(SaveStateEntry));
3368 8a7ddc38 bellard
    if (!se)
3369 8a7ddc38 bellard
        return -1;
3370 8a7ddc38 bellard
    pstrcpy(se->idstr, sizeof(se->idstr), idstr);
3371 18be5187 pbrook
    se->instance_id = (instance_id == -1) ? 0 : instance_id;
3372 8a7ddc38 bellard
    se->version_id = version_id;
3373 9366f418 aliguori
    se->section_id = global_section_id++;
3374 9366f418 aliguori
    se->save_live_state = save_live_state;
3375 8a7ddc38 bellard
    se->save_state = save_state;
3376 8a7ddc38 bellard
    se->load_state = load_state;
3377 8a7ddc38 bellard
    se->opaque = opaque;
3378 8a7ddc38 bellard
    se->next = NULL;
3379 8a7ddc38 bellard
3380 8a7ddc38 bellard
    /* add at the end of list */
3381 8a7ddc38 bellard
    pse = &first_se;
3382 18be5187 pbrook
    while (*pse != NULL) {
3383 18be5187 pbrook
        if (instance_id == -1
3384 18be5187 pbrook
                && strcmp(se->idstr, (*pse)->idstr) == 0
3385 18be5187 pbrook
                && se->instance_id <= (*pse)->instance_id)
3386 18be5187 pbrook
            se->instance_id = (*pse)->instance_id + 1;
3387 8a7ddc38 bellard
        pse = &(*pse)->next;
3388 18be5187 pbrook
    }
3389 8a7ddc38 bellard
    *pse = se;
3390 8a7ddc38 bellard
    return 0;
3391 8a7ddc38 bellard
}
3392 8a7ddc38 bellard
3393 9366f418 aliguori
int register_savevm(const char *idstr,
3394 9366f418 aliguori
                    int instance_id,
3395 9366f418 aliguori
                    int version_id,
3396 9366f418 aliguori
                    SaveStateHandler *save_state,
3397 9366f418 aliguori
                    LoadStateHandler *load_state,
3398 9366f418 aliguori
                    void *opaque)
3399 9366f418 aliguori
{
3400 9366f418 aliguori
    return register_savevm_live(idstr, instance_id, version_id,
3401 9366f418 aliguori
                                NULL, save_state, load_state, opaque);
3402 9366f418 aliguori
}
3403 9366f418 aliguori
3404 9366f418 aliguori
#define QEMU_VM_FILE_MAGIC           0x5145564d
3405 9366f418 aliguori
#define QEMU_VM_FILE_VERSION_COMPAT  0x00000002
3406 9366f418 aliguori
#define QEMU_VM_FILE_VERSION         0x00000003
3407 9366f418 aliguori
3408 9366f418 aliguori
#define QEMU_VM_EOF                  0x00
3409 9366f418 aliguori
#define QEMU_VM_SECTION_START        0x01
3410 9366f418 aliguori
#define QEMU_VM_SECTION_PART         0x02
3411 9366f418 aliguori
#define QEMU_VM_SECTION_END          0x03
3412 9366f418 aliguori
#define QEMU_VM_SECTION_FULL         0x04
3413 8a7ddc38 bellard
3414 9366f418 aliguori
int qemu_savevm_state_begin(QEMUFile *f)
3415 8a7ddc38 bellard
{
3416 8a7ddc38 bellard
    SaveStateEntry *se;
3417 313aa567 bellard
3418 8a7ddc38 bellard
    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
3419 8a7ddc38 bellard
    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
3420 9366f418 aliguori
3421 9366f418 aliguori
    for (se = first_se; se != NULL; se = se->next) {
3422 9366f418 aliguori
        int len;
3423 9366f418 aliguori
3424 9366f418 aliguori
        if (se->save_live_state == NULL)
3425 9366f418 aliguori
            continue;
3426 9366f418 aliguori
3427 9366f418 aliguori
        /* Section type */
3428 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_START);
3429 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3430 9366f418 aliguori
3431 9366f418 aliguori
        /* ID string */
3432 9366f418 aliguori
        len = strlen(se->idstr);
3433 9366f418 aliguori
        qemu_put_byte(f, len);
3434 9366f418 aliguori
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
3435 9366f418 aliguori
3436 9366f418 aliguori
        qemu_put_be32(f, se->instance_id);
3437 9366f418 aliguori
        qemu_put_be32(f, se->version_id);
3438 9366f418 aliguori
3439 9366f418 aliguori
        se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
3440 9366f418 aliguori
    }
3441 9366f418 aliguori
3442 871d2f07 aliguori
    if (qemu_file_has_error(f))
3443 871d2f07 aliguori
        return -EIO;
3444 871d2f07 aliguori
3445 9366f418 aliguori
    return 0;
3446 9366f418 aliguori
}
3447 9366f418 aliguori
3448 9366f418 aliguori
int qemu_savevm_state_iterate(QEMUFile *f)
3449 9366f418 aliguori
{
3450 9366f418 aliguori
    SaveStateEntry *se;
3451 475e4277 aliguori
    int ret = 1;
3452 9366f418 aliguori
3453 9366f418 aliguori
    for (se = first_se; se != NULL; se = se->next) {
3454 9366f418 aliguori
        if (se->save_live_state == NULL)
3455 9366f418 aliguori
            continue;
3456 9366f418 aliguori
3457 9366f418 aliguori
        /* Section type */
3458 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_PART);
3459 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3460 9366f418 aliguori
3461 475e4277 aliguori
        ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
3462 9366f418 aliguori
    }
3463 9366f418 aliguori
3464 9366f418 aliguori
    if (ret)
3465 9366f418 aliguori
        return 1;
3466 9366f418 aliguori
3467 871d2f07 aliguori
    if (qemu_file_has_error(f))
3468 871d2f07 aliguori
        return -EIO;
3469 871d2f07 aliguori
3470 9366f418 aliguori
    return 0;
3471 9366f418 aliguori
}
3472 9366f418 aliguori
3473 9366f418 aliguori
int qemu_savevm_state_complete(QEMUFile *f)
3474 9366f418 aliguori
{
3475 9366f418 aliguori
    SaveStateEntry *se;
3476 9366f418 aliguori
3477 9366f418 aliguori
    for (se = first_se; se != NULL; se = se->next) {
3478 9366f418 aliguori
        if (se->save_live_state == NULL)
3479 9366f418 aliguori
            continue;
3480 9366f418 aliguori
3481 9366f418 aliguori
        /* Section type */
3482 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_END);
3483 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3484 9366f418 aliguori
3485 9366f418 aliguori
        se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
3486 9366f418 aliguori
    }
3487 8a7ddc38 bellard
3488 8a7ddc38 bellard
    for(se = first_se; se != NULL; se = se->next) {
3489 9366f418 aliguori
        int len;
3490 9366f418 aliguori
3491 d978c02c aurel32
        if (se->save_state == NULL)
3492 d978c02c aurel32
            continue;
3493 d978c02c aurel32
3494 9366f418 aliguori
        /* Section type */
3495 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_FULL);
3496 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3497 9366f418 aliguori
3498 8a7ddc38 bellard
        /* ID string */
3499 8a7ddc38 bellard
        len = strlen(se->idstr);
3500 8a7ddc38 bellard
        qemu_put_byte(f, len);
3501 ffe8ab83 ths
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
3502 8a7ddc38 bellard
3503 8a7ddc38 bellard
        qemu_put_be32(f, se->instance_id);
3504 8a7ddc38 bellard
        qemu_put_be32(f, se->version_id);
3505 8a7ddc38 bellard
3506 8a7ddc38 bellard
        se->save_state(f, se->opaque);
3507 8a7ddc38 bellard
    }
3508 8a7ddc38 bellard
3509 9366f418 aliguori
    qemu_put_byte(f, QEMU_VM_EOF);
3510 9366f418 aliguori
3511 871d2f07 aliguori
    if (qemu_file_has_error(f))
3512 871d2f07 aliguori
        return -EIO;
3513 871d2f07 aliguori
3514 9366f418 aliguori
    return 0;
3515 9366f418 aliguori
}
3516 9366f418 aliguori
3517 9366f418 aliguori
int qemu_savevm_state(QEMUFile *f)
3518 9366f418 aliguori
{
3519 9366f418 aliguori
    int saved_vm_running;
3520 9366f418 aliguori
    int ret;
3521 9366f418 aliguori
3522 9366f418 aliguori
    saved_vm_running = vm_running;
3523 9366f418 aliguori
    vm_stop(0);
3524 9366f418 aliguori
3525 5bb7910a aliguori
    bdrv_flush_all();
3526 5bb7910a aliguori
3527 9366f418 aliguori
    ret = qemu_savevm_state_begin(f);
3528 9366f418 aliguori
    if (ret < 0)
3529 9366f418 aliguori
        goto out;
3530 9366f418 aliguori
3531 9366f418 aliguori
    do {
3532 9366f418 aliguori
        ret = qemu_savevm_state_iterate(f);
3533 9366f418 aliguori
        if (ret < 0)
3534 9366f418 aliguori
            goto out;
3535 475e4277 aliguori
    } while (ret == 0);
3536 9366f418 aliguori
3537 9366f418 aliguori
    ret = qemu_savevm_state_complete(f);
3538 9366f418 aliguori
3539 9366f418 aliguori
out:
3540 871d2f07 aliguori
    if (qemu_file_has_error(f))
3541 871d2f07 aliguori
        ret = -EIO;
3542 871d2f07 aliguori
3543 871d2f07 aliguori
    if (!ret && saved_vm_running)
3544 9366f418 aliguori
        vm_start();
3545 871d2f07 aliguori
3546 8a7ddc38 bellard
    return ret;
3547 8a7ddc38 bellard
}
3548 8a7ddc38 bellard
3549 8a7ddc38 bellard
static SaveStateEntry *find_se(const char *idstr, int instance_id)
3550 8a7ddc38 bellard
{
3551 8a7ddc38 bellard
    SaveStateEntry *se;
3552 8a7ddc38 bellard
3553 8a7ddc38 bellard
    for(se = first_se; se != NULL; se = se->next) {
3554 5fafdf24 ths
        if (!strcmp(se->idstr, idstr) &&
3555 8a7ddc38 bellard
            instance_id == se->instance_id)
3556 8a7ddc38 bellard
            return se;
3557 8a7ddc38 bellard
    }
3558 8a7ddc38 bellard
    return NULL;
3559 8a7ddc38 bellard
}
3560 8a7ddc38 bellard
3561 9366f418 aliguori
typedef struct LoadStateEntry {
3562 9366f418 aliguori
    SaveStateEntry *se;
3563 9366f418 aliguori
    int section_id;
3564 9366f418 aliguori
    int version_id;
3565 9366f418 aliguori
    struct LoadStateEntry *next;
3566 9366f418 aliguori
} LoadStateEntry;
3567 9366f418 aliguori
3568 9366f418 aliguori
static int qemu_loadvm_state_v2(QEMUFile *f)
3569 8a7ddc38 bellard
{
3570 8a7ddc38 bellard
    SaveStateEntry *se;
3571 faea38e7 bellard
    int len, ret, instance_id, record_len, version_id;
3572 faea38e7 bellard
    int64_t total_len, end_pos, cur_pos;
3573 8a7ddc38 bellard
    char idstr[256];
3574 3b46e624 ths
3575 faea38e7 bellard
    total_len = qemu_get_be64(f);
3576 faea38e7 bellard
    end_pos = total_len + qemu_ftell(f);
3577 b4608c04 bellard
    for(;;) {
3578 faea38e7 bellard
        if (qemu_ftell(f) >= end_pos)
3579 cd4c3e88 bellard
            break;
3580 faea38e7 bellard
        len = qemu_get_byte(f);
3581 ffe8ab83 ths
        qemu_get_buffer(f, (uint8_t *)idstr, len);
3582 8a7ddc38 bellard
        idstr[len] = '\0';
3583 8a7ddc38 bellard
        instance_id = qemu_get_be32(f);
3584 8a7ddc38 bellard
        version_id = qemu_get_be32(f);
3585 8a7ddc38 bellard
        record_len = qemu_get_be32(f);
3586 faea38e7 bellard
        cur_pos = qemu_ftell(f);
3587 8a7ddc38 bellard
        se = find_se(idstr, instance_id);
3588 8a7ddc38 bellard
        if (!se) {
3589 5fafdf24 ths
            fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3590 8a7ddc38 bellard
                    instance_id, idstr);
3591 8a7ddc38 bellard
        } else {
3592 8a7ddc38 bellard
            ret = se->load_state(f, se->opaque, version_id);
3593 8a7ddc38 bellard
            if (ret < 0) {
3594 5fafdf24 ths
                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3595 8a7ddc38 bellard
                        instance_id, idstr);
3596 8a7ddc38 bellard
            }
3597 34865134 bellard
        }
3598 8a7ddc38 bellard
        /* always seek to exact end of record */
3599 8a7ddc38 bellard
        qemu_fseek(f, cur_pos + record_len, SEEK_SET);
3600 8a7ddc38 bellard
    }
3601 871d2f07 aliguori
3602 871d2f07 aliguori
    if (qemu_file_has_error(f))
3603 871d2f07 aliguori
        return -EIO;
3604 871d2f07 aliguori
3605 9366f418 aliguori
    return 0;
3606 9366f418 aliguori
}
3607 9366f418 aliguori
3608 9366f418 aliguori
int qemu_loadvm_state(QEMUFile *f)
3609 9366f418 aliguori
{
3610 9366f418 aliguori
    LoadStateEntry *first_le = NULL;
3611 9366f418 aliguori
    uint8_t section_type;
3612 9366f418 aliguori
    unsigned int v;
3613 9366f418 aliguori
    int ret;
3614 9366f418 aliguori
3615 9366f418 aliguori
    v = qemu_get_be32(f);
3616 9366f418 aliguori
    if (v != QEMU_VM_FILE_MAGIC)
3617 9366f418 aliguori
        return -EINVAL;
3618 9366f418 aliguori
3619 9366f418 aliguori
    v = qemu_get_be32(f);
3620 9366f418 aliguori
    if (v == QEMU_VM_FILE_VERSION_COMPAT)
3621 9366f418 aliguori
        return qemu_loadvm_state_v2(f);
3622 9366f418 aliguori
    if (v != QEMU_VM_FILE_VERSION)
3623 9366f418 aliguori
        return -ENOTSUP;
3624 9366f418 aliguori
3625 9366f418 aliguori
    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
3626 9366f418 aliguori
        uint32_t instance_id, version_id, section_id;
3627 9366f418 aliguori
        LoadStateEntry *le;
3628 9366f418 aliguori
        SaveStateEntry *se;
3629 9366f418 aliguori
        char idstr[257];
3630 9366f418 aliguori
        int len;
3631 9366f418 aliguori
3632 9366f418 aliguori
        switch (section_type) {
3633 9366f418 aliguori
        case QEMU_VM_SECTION_START:
3634 9366f418 aliguori
        case QEMU_VM_SECTION_FULL:
3635 9366f418 aliguori
            /* Read section start */
3636 9366f418 aliguori
            section_id = qemu_get_be32(f);
3637 9366f418 aliguori
            len = qemu_get_byte(f);
3638 9366f418 aliguori
            qemu_get_buffer(f, (uint8_t *)idstr, len);
3639 9366f418 aliguori
            idstr[len] = 0;
3640 9366f418 aliguori
            instance_id = qemu_get_be32(f);
3641 9366f418 aliguori
            version_id = qemu_get_be32(f);
3642 9366f418 aliguori
3643 9366f418 aliguori
            /* Find savevm section */
3644 9366f418 aliguori
            se = find_se(idstr, instance_id);
3645 9366f418 aliguori
            if (se == NULL) {
3646 9366f418 aliguori
                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
3647 9366f418 aliguori
                ret = -EINVAL;
3648 9366f418 aliguori
                goto out;
3649 9366f418 aliguori
            }
3650 9366f418 aliguori
3651 9366f418 aliguori
            /* Validate version */
3652 9366f418 aliguori
            if (version_id > se->version_id) {
3653 9366f418 aliguori
                fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
3654 9366f418 aliguori
                        version_id, idstr, se->version_id);
3655 9366f418 aliguori
                ret = -EINVAL;
3656 9366f418 aliguori
                goto out;
3657 9366f418 aliguori
            }
3658 9366f418 aliguori
3659 9366f418 aliguori
            /* Add entry */
3660 9366f418 aliguori
            le = qemu_mallocz(sizeof(*le));
3661 9366f418 aliguori
            if (le == NULL) {
3662 9366f418 aliguori
                ret = -ENOMEM;
3663 9366f418 aliguori
                goto out;
3664 9366f418 aliguori
            }
3665 9366f418 aliguori
3666 9366f418 aliguori
            le->se = se;
3667 9366f418 aliguori
            le->section_id = section_id;
3668 9366f418 aliguori
            le->version_id = version_id;
3669 9366f418 aliguori
            le->next = first_le;
3670 9366f418 aliguori
            first_le = le;
3671 9366f418 aliguori
3672 9366f418 aliguori
            le->se->load_state(f, le->se->opaque, le->version_id);
3673 9366f418 aliguori
            break;
3674 9366f418 aliguori
        case QEMU_VM_SECTION_PART:
3675 9366f418 aliguori
        case QEMU_VM_SECTION_END:
3676 9366f418 aliguori
            section_id = qemu_get_be32(f);
3677 9366f418 aliguori
3678 9366f418 aliguori
            for (le = first_le; le && le->section_id != section_id; le = le->next);
3679 9366f418 aliguori
            if (le == NULL) {
3680 9366f418 aliguori
                fprintf(stderr, "Unknown savevm section %d\n", section_id);
3681 9366f418 aliguori
                ret = -EINVAL;
3682 9366f418 aliguori
                goto out;
3683 9366f418 aliguori
            }
3684 9366f418 aliguori
3685 9366f418 aliguori
            le->se->load_state(f, le->se->opaque, le->version_id);
3686 9366f418 aliguori
            break;
3687 9366f418 aliguori
        default:
3688 9366f418 aliguori
            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
3689 9366f418 aliguori
            ret = -EINVAL;
3690 9366f418 aliguori
            goto out;
3691 9366f418 aliguori
        }
3692 9366f418 aliguori
    }
3693 9366f418 aliguori
3694 8a7ddc38 bellard
    ret = 0;
3695 9366f418 aliguori
3696 9366f418 aliguori
out:
3697 9366f418 aliguori
    while (first_le) {
3698 9366f418 aliguori
        LoadStateEntry *le = first_le;
3699 9366f418 aliguori
        first_le = first_le->next;
3700 9366f418 aliguori
        qemu_free(le);
3701 9366f418 aliguori
    }
3702 9366f418 aliguori
3703 871d2f07 aliguori
    if (qemu_file_has_error(f))
3704 871d2f07 aliguori
        ret = -EIO;
3705 871d2f07 aliguori
3706 faea38e7 bellard
    return ret;
3707 faea38e7 bellard
}
3708 faea38e7 bellard
3709 faea38e7 bellard
/* device can contain snapshots */
3710 faea38e7 bellard
static int bdrv_can_snapshot(BlockDriverState *bs)
3711 faea38e7 bellard
{
3712 faea38e7 bellard
    return (bs &&
3713 faea38e7 bellard
            !bdrv_is_removable(bs) &&
3714 faea38e7 bellard
            !bdrv_is_read_only(bs));
3715 faea38e7 bellard
}
3716 faea38e7 bellard
3717 faea38e7 bellard
/* device must be snapshots in order to have a reliable snapshot */
3718 faea38e7 bellard
static int bdrv_has_snapshot(BlockDriverState *bs)
3719 faea38e7 bellard
{
3720 faea38e7 bellard
    return (bs &&
3721 faea38e7 bellard
            !bdrv_is_removable(bs) &&
3722 faea38e7 bellard
            !bdrv_is_read_only(bs));
3723 faea38e7 bellard
}
3724 faea38e7 bellard
3725 faea38e7 bellard
static BlockDriverState *get_bs_snapshots(void)
3726 faea38e7 bellard
{
3727 faea38e7 bellard
    BlockDriverState *bs;
3728 faea38e7 bellard
    int i;
3729 faea38e7 bellard
3730 faea38e7 bellard
    if (bs_snapshots)
3731 faea38e7 bellard
        return bs_snapshots;
3732 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3733 e4bcb14c ths
        bs = drives_table[i].bdrv;
3734 faea38e7 bellard
        if (bdrv_can_snapshot(bs))
3735 faea38e7 bellard
            goto ok;
3736 faea38e7 bellard
    }
3737 faea38e7 bellard
    return NULL;
3738 faea38e7 bellard
 ok:
3739 faea38e7 bellard
    bs_snapshots = bs;
3740 faea38e7 bellard
    return bs;
3741 faea38e7 bellard
}
3742 faea38e7 bellard
3743 faea38e7 bellard
static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
3744 faea38e7 bellard
                              const char *name)
3745 faea38e7 bellard
{
3746 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
3747 faea38e7 bellard
    int nb_sns, i, ret;
3748 3b46e624 ths
3749 faea38e7 bellard
    ret = -ENOENT;
3750 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
3751 faea38e7 bellard
    if (nb_sns < 0)
3752 faea38e7 bellard
        return ret;
3753 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
3754 faea38e7 bellard
        sn = &sn_tab[i];
3755 faea38e7 bellard
        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
3756 faea38e7 bellard
            *sn_info = *sn;
3757 faea38e7 bellard
            ret = 0;
3758 faea38e7 bellard
            break;
3759 faea38e7 bellard
        }
3760 faea38e7 bellard
    }
3761 faea38e7 bellard
    qemu_free(sn_tab);
3762 faea38e7 bellard
    return ret;
3763 faea38e7 bellard
}
3764 faea38e7 bellard
3765 faea38e7 bellard
void do_savevm(const char *name)
3766 faea38e7 bellard
{
3767 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3768 faea38e7 bellard
    QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
3769 faea38e7 bellard
    int must_delete, ret, i;
3770 faea38e7 bellard
    BlockDriverInfo bdi1, *bdi = &bdi1;
3771 faea38e7 bellard
    QEMUFile *f;
3772 faea38e7 bellard
    int saved_vm_running;
3773 4c279bdf bellard
#ifdef _WIN32
3774 4c279bdf bellard
    struct _timeb tb;
3775 4c279bdf bellard
#else
3776 faea38e7 bellard
    struct timeval tv;
3777 4c279bdf bellard
#endif
3778 faea38e7 bellard
3779 faea38e7 bellard
    bs = get_bs_snapshots();
3780 faea38e7 bellard
    if (!bs) {
3781 faea38e7 bellard
        term_printf("No block device can accept snapshots\n");
3782 faea38e7 bellard
        return;
3783 faea38e7 bellard
    }
3784 faea38e7 bellard
3785 6192bc37 pbrook
    /* ??? Should this occur after vm_stop?  */
3786 6192bc37 pbrook
    qemu_aio_flush();
3787 6192bc37 pbrook
3788 faea38e7 bellard
    saved_vm_running = vm_running;
3789 faea38e7 bellard
    vm_stop(0);
3790 3b46e624 ths
3791 faea38e7 bellard
    must_delete = 0;
3792 faea38e7 bellard
    if (name) {
3793 faea38e7 bellard
        ret = bdrv_snapshot_find(bs, old_sn, name);
3794 faea38e7 bellard
        if (ret >= 0) {
3795 faea38e7 bellard
            must_delete = 1;
3796 faea38e7 bellard
        }
3797 faea38e7 bellard
    }
3798 faea38e7 bellard
    memset(sn, 0, sizeof(*sn));
3799 faea38e7 bellard
    if (must_delete) {
3800 faea38e7 bellard
        pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
3801 faea38e7 bellard
        pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
3802 faea38e7 bellard
    } else {
3803 faea38e7 bellard
        if (name)
3804 faea38e7 bellard
            pstrcpy(sn->name, sizeof(sn->name), name);
3805 faea38e7 bellard
    }
3806 faea38e7 bellard
3807 faea38e7 bellard
    /* fill auxiliary fields */
3808 4c279bdf bellard
#ifdef _WIN32
3809 4c279bdf bellard
    _ftime(&tb);
3810 4c279bdf bellard
    sn->date_sec = tb.time;
3811 4c279bdf bellard
    sn->date_nsec = tb.millitm * 1000000;
3812 4c279bdf bellard
#else
3813 faea38e7 bellard
    gettimeofday(&tv, NULL);
3814 faea38e7 bellard
    sn->date_sec = tv.tv_sec;
3815 faea38e7 bellard
    sn->date_nsec = tv.tv_usec * 1000;
3816 4c279bdf bellard
#endif
3817 faea38e7 bellard
    sn->vm_clock_nsec = qemu_get_clock(vm_clock);
3818 3b46e624 ths
3819 faea38e7 bellard
    if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
3820 faea38e7 bellard
        term_printf("Device %s does not support VM state snapshots\n",
3821 faea38e7 bellard
                    bdrv_get_device_name(bs));
3822 faea38e7 bellard
        goto the_end;
3823 faea38e7 bellard
    }
3824 3b46e624 ths
3825 faea38e7 bellard
    /* save the VM state */
3826 faea38e7 bellard
    f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
3827 faea38e7 bellard
    if (!f) {
3828 faea38e7 bellard
        term_printf("Could not open VM state file\n");
3829 faea38e7 bellard
        goto the_end;
3830 faea38e7 bellard
    }
3831 faea38e7 bellard
    ret = qemu_savevm_state(f);
3832 faea38e7 bellard
    sn->vm_state_size = qemu_ftell(f);
3833 faea38e7 bellard
    qemu_fclose(f);
3834 faea38e7 bellard
    if (ret < 0) {
3835 faea38e7 bellard
        term_printf("Error %d while writing VM\n", ret);
3836 faea38e7 bellard
        goto the_end;
3837 faea38e7 bellard
    }
3838 3b46e624 ths
3839 faea38e7 bellard
    /* create the snapshots */
3840 faea38e7 bellard
3841 e4bcb14c ths
    for(i = 0; i < nb_drives; i++) {
3842 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3843 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3844 faea38e7 bellard
            if (must_delete) {
3845 faea38e7 bellard
                ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
3846 faea38e7 bellard
                if (ret < 0) {
3847 faea38e7 bellard
                    term_printf("Error while deleting snapshot on '%s'\n",
3848 faea38e7 bellard
                                bdrv_get_device_name(bs1));
3849 faea38e7 bellard
                }
3850 faea38e7 bellard
            }
3851 faea38e7 bellard
            ret = bdrv_snapshot_create(bs1, sn);
3852 faea38e7 bellard
            if (ret < 0) {
3853 faea38e7 bellard
                term_printf("Error while creating snapshot on '%s'\n",
3854 faea38e7 bellard
                            bdrv_get_device_name(bs1));
3855 faea38e7 bellard
            }
3856 faea38e7 bellard
        }
3857 faea38e7 bellard
    }
3858 faea38e7 bellard
3859 faea38e7 bellard
 the_end:
3860 8a7ddc38 bellard
    if (saved_vm_running)
3861 8a7ddc38 bellard
        vm_start();
3862 faea38e7 bellard
}
3863 faea38e7 bellard
3864 faea38e7 bellard
void do_loadvm(const char *name)
3865 faea38e7 bellard
{
3866 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3867 faea38e7 bellard
    BlockDriverInfo bdi1, *bdi = &bdi1;
3868 faea38e7 bellard
    QEMUFile *f;
3869 faea38e7 bellard
    int i, ret;
3870 faea38e7 bellard
    int saved_vm_running;
3871 faea38e7 bellard
3872 faea38e7 bellard
    bs = get_bs_snapshots();
3873 faea38e7 bellard
    if (!bs) {
3874 faea38e7 bellard
        term_printf("No block device supports snapshots\n");
3875 faea38e7 bellard
        return;
3876 faea38e7 bellard
    }
3877 3b46e624 ths
3878 6192bc37 pbrook
    /* Flush all IO requests so they don't interfere with the new state.  */
3879 6192bc37 pbrook
    qemu_aio_flush();
3880 6192bc37 pbrook
3881 faea38e7 bellard
    saved_vm_running = vm_running;
3882 faea38e7 bellard
    vm_stop(0);
3883 faea38e7 bellard
3884 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3885 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3886 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3887 faea38e7 bellard
            ret = bdrv_snapshot_goto(bs1, name);
3888 faea38e7 bellard
            if (ret < 0) {
3889 faea38e7 bellard
                if (bs != bs1)
3890 faea38e7 bellard
                    term_printf("Warning: ");
3891 faea38e7 bellard
                switch(ret) {
3892 faea38e7 bellard
                case -ENOTSUP:
3893 faea38e7 bellard
                    term_printf("Snapshots not supported on device '%s'\n",
3894 faea38e7 bellard
                                bdrv_get_device_name(bs1));
3895 faea38e7 bellard
                    break;
3896 faea38e7 bellard
                case -ENOENT:
3897 faea38e7 bellard
                    term_printf("Could not find snapshot '%s' on device '%s'\n",
3898 faea38e7 bellard
                                name, bdrv_get_device_name(bs1));
3899 faea38e7 bellard
                    break;
3900 faea38e7 bellard
                default:
3901 faea38e7 bellard
                    term_printf("Error %d while activating snapshot on '%s'\n",
3902 faea38e7 bellard
                                ret, bdrv_get_device_name(bs1));
3903 faea38e7 bellard
                    break;
3904 faea38e7 bellard
                }
3905 faea38e7 bellard
                /* fatal on snapshot block device */
3906 faea38e7 bellard
                if (bs == bs1)
3907 faea38e7 bellard
                    goto the_end;
3908 faea38e7 bellard
            }
3909 faea38e7 bellard
        }
3910 faea38e7 bellard
    }
3911 faea38e7 bellard
3912 faea38e7 bellard
    if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
3913 faea38e7 bellard
        term_printf("Device %s does not support VM state snapshots\n",
3914 faea38e7 bellard
                    bdrv_get_device_name(bs));
3915 faea38e7 bellard
        return;
3916 faea38e7 bellard
    }
3917 3b46e624 ths
3918 faea38e7 bellard
    /* restore the VM state */
3919 faea38e7 bellard
    f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
3920 faea38e7 bellard
    if (!f) {
3921 faea38e7 bellard
        term_printf("Could not open VM state file\n");
3922 faea38e7 bellard
        goto the_end;
3923 faea38e7 bellard
    }
3924 faea38e7 bellard
    ret = qemu_loadvm_state(f);
3925 faea38e7 bellard
    qemu_fclose(f);
3926 faea38e7 bellard
    if (ret < 0) {
3927 faea38e7 bellard
        term_printf("Error %d while loading VM state\n", ret);
3928 faea38e7 bellard
    }
3929 faea38e7 bellard
 the_end:
3930 faea38e7 bellard
    if (saved_vm_running)
3931 faea38e7 bellard
        vm_start();
3932 faea38e7 bellard
}
3933 faea38e7 bellard
3934 faea38e7 bellard
void do_delvm(const char *name)
3935 faea38e7 bellard
{
3936 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3937 faea38e7 bellard
    int i, ret;
3938 faea38e7 bellard
3939 faea38e7 bellard
    bs = get_bs_snapshots();
3940 faea38e7 bellard
    if (!bs) {
3941 faea38e7 bellard
        term_printf("No block device supports snapshots\n");
3942 faea38e7 bellard
        return;
3943 faea38e7 bellard
    }
3944 3b46e624 ths
3945 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3946 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3947 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3948 faea38e7 bellard
            ret = bdrv_snapshot_delete(bs1, name);
3949 faea38e7 bellard
            if (ret < 0) {
3950 faea38e7 bellard
                if (ret == -ENOTSUP)
3951 faea38e7 bellard
                    term_printf("Snapshots not supported on device '%s'\n",
3952 faea38e7 bellard
                                bdrv_get_device_name(bs1));
3953 faea38e7 bellard
                else
3954 faea38e7 bellard
                    term_printf("Error %d while deleting snapshot on '%s'\n",
3955 faea38e7 bellard
                                ret, bdrv_get_device_name(bs1));
3956 faea38e7 bellard
            }
3957 faea38e7 bellard
        }
3958 faea38e7 bellard
    }
3959 faea38e7 bellard
}
3960 faea38e7 bellard
3961 faea38e7 bellard
void do_info_snapshots(void)
3962 faea38e7 bellard
{
3963 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3964 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
3965 faea38e7 bellard
    int nb_sns, i;
3966 faea38e7 bellard
    char buf[256];
3967 faea38e7 bellard
3968 faea38e7 bellard
    bs = get_bs_snapshots();
3969 faea38e7 bellard
    if (!bs) {
3970 faea38e7 bellard
        term_printf("No available block device supports snapshots\n");
3971 faea38e7 bellard
        return;
3972 faea38e7 bellard
    }
3973 faea38e7 bellard
    term_printf("Snapshot devices:");
3974 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3975 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3976 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3977 faea38e7 bellard
            if (bs == bs1)
3978 faea38e7 bellard
                term_printf(" %s", bdrv_get_device_name(bs1));
3979 faea38e7 bellard
        }
3980 faea38e7 bellard
    }
3981 faea38e7 bellard
    term_printf("\n");
3982 faea38e7 bellard
3983 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
3984 faea38e7 bellard
    if (nb_sns < 0) {
3985 faea38e7 bellard
        term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
3986 faea38e7 bellard
        return;
3987 faea38e7 bellard
    }
3988 faea38e7 bellard
    term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
3989 faea38e7 bellard
    term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
3990 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
3991 faea38e7 bellard
        sn = &sn_tab[i];
3992 faea38e7 bellard
        term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
3993 faea38e7 bellard
    }
3994 faea38e7 bellard
    qemu_free(sn_tab);
3995 8a7ddc38 bellard
}
3996 8a7ddc38 bellard
3997 8a7ddc38 bellard
/***********************************************************/
3998 8a7ddc38 bellard
/* ram save/restore */
3999 8a7ddc38 bellard
4000 8a7ddc38 bellard
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
4001 8a7ddc38 bellard
{
4002 8a7ddc38 bellard
    int v;
4003 8a7ddc38 bellard
4004 8a7ddc38 bellard
    v = qemu_get_byte(f);
4005 8a7ddc38 bellard
    switch(v) {
4006 8a7ddc38 bellard
    case 0:
4007 8a7ddc38 bellard
        if (qemu_get_buffer(f, buf, len) != len)
4008 8a7ddc38 bellard
            return -EIO;
4009 8a7ddc38 bellard
        break;
4010 8a7ddc38 bellard
    case 1:
4011 8a7ddc38 bellard
        v = qemu_get_byte(f);
4012 8a7ddc38 bellard
        memset(buf, v, len);
4013 8a7ddc38 bellard
        break;
4014 8a7ddc38 bellard
    default:
4015 8a7ddc38 bellard
        return -EINVAL;
4016 8a7ddc38 bellard
    }
4017 871d2f07 aliguori
4018 871d2f07 aliguori
    if (qemu_file_has_error(f))
4019 871d2f07 aliguori
        return -EIO;
4020 871d2f07 aliguori
4021 8a7ddc38 bellard
    return 0;
4022 8a7ddc38 bellard
}
4023 8a7ddc38 bellard
4024 c88676f8 bellard
static int ram_load_v1(QEMUFile *f, void *opaque)
4025 c88676f8 bellard
{
4026 00f82b8a aurel32
    int ret;
4027 00f82b8a aurel32
    ram_addr_t i;
4028 c88676f8 bellard
4029 c88676f8 bellard
    if (qemu_get_be32(f) != phys_ram_size)
4030 c88676f8 bellard
        return -EINVAL;
4031 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
4032 c88676f8 bellard
        ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
4033 c88676f8 bellard
        if (ret)
4034 c88676f8 bellard
            return ret;
4035 c88676f8 bellard
    }
4036 c88676f8 bellard
    return 0;
4037 c88676f8 bellard
}
4038 c88676f8 bellard
4039 c88676f8 bellard
#define BDRV_HASH_BLOCK_SIZE 1024
4040 c88676f8 bellard
#define IOBUF_SIZE 4096
4041 c88676f8 bellard
#define RAM_CBLOCK_MAGIC 0xfabe
4042 c88676f8 bellard
4043 c88676f8 bellard
typedef struct RamDecompressState {
4044 c88676f8 bellard
    z_stream zstream;
4045 c88676f8 bellard
    QEMUFile *f;
4046 c88676f8 bellard
    uint8_t buf[IOBUF_SIZE];
4047 c88676f8 bellard
} RamDecompressState;
4048 c88676f8 bellard
4049 c88676f8 bellard
static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
4050 c88676f8 bellard
{
4051 c88676f8 bellard
    int ret;
4052 c88676f8 bellard
    memset(s, 0, sizeof(*s));
4053 c88676f8 bellard
    s->f = f;
4054 c88676f8 bellard
    ret = inflateInit(&s->zstream);
4055 c88676f8 bellard
    if (ret != Z_OK)
4056 c88676f8 bellard
        return -1;
4057 c88676f8 bellard
    return 0;
4058 c88676f8 bellard
}
4059 c88676f8 bellard
4060 c88676f8 bellard
static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
4061 c88676f8 bellard
{
4062 c88676f8 bellard
    int ret, clen;
4063 c88676f8 bellard
4064 c88676f8 bellard
    s->zstream.avail_out = len;
4065 c88676f8 bellard
    s->zstream.next_out = buf;
4066 c88676f8 bellard
    while (s->zstream.avail_out > 0) {
4067 c88676f8 bellard
        if (s->zstream.avail_in == 0) {
4068 c88676f8 bellard
            if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
4069 c88676f8 bellard
                return -1;
4070 c88676f8 bellard
            clen = qemu_get_be16(s->f);
4071 c88676f8 bellard
            if (clen > IOBUF_SIZE)
4072 c88676f8 bellard
                return -1;
4073 c88676f8 bellard
            qemu_get_buffer(s->f, s->buf, clen);
4074 c88676f8 bellard
            s->zstream.avail_in = clen;
4075 c88676f8 bellard
            s->zstream.next_in = s->buf;
4076 c88676f8 bellard
        }
4077 c88676f8 bellard
        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
4078 c88676f8 bellard
        if (ret != Z_OK && ret != Z_STREAM_END) {
4079 c88676f8 bellard
            return -1;
4080 c88676f8 bellard
        }
4081 c88676f8 bellard
    }
4082 c88676f8 bellard
    return 0;
4083 c88676f8 bellard
}
4084 c88676f8 bellard
4085 c88676f8 bellard
static void ram_decompress_close(RamDecompressState *s)
4086 c88676f8 bellard
{
4087 c88676f8 bellard
    inflateEnd(&s->zstream);
4088 c88676f8 bellard
}
4089 c88676f8 bellard
4090 475e4277 aliguori
#define RAM_SAVE_FLAG_FULL        0x01
4091 475e4277 aliguori
#define RAM_SAVE_FLAG_COMPRESS        0x02
4092 475e4277 aliguori
#define RAM_SAVE_FLAG_MEM_SIZE        0x04
4093 475e4277 aliguori
#define RAM_SAVE_FLAG_PAGE        0x08
4094 475e4277 aliguori
#define RAM_SAVE_FLAG_EOS        0x10
4095 475e4277 aliguori
4096 475e4277 aliguori
static int is_dup_page(uint8_t *page, uint8_t ch)
4097 8a7ddc38 bellard
{
4098 475e4277 aliguori
    uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
4099 475e4277 aliguori
    uint32_t *array = (uint32_t *)page;
4100 475e4277 aliguori
    int i;
4101 3b46e624 ths
4102 475e4277 aliguori
    for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
4103 475e4277 aliguori
        if (array[i] != val)
4104 475e4277 aliguori
            return 0;
4105 475e4277 aliguori
    }
4106 475e4277 aliguori
4107 475e4277 aliguori
    return 1;
4108 475e4277 aliguori
}
4109 475e4277 aliguori
4110 475e4277 aliguori
static int ram_save_block(QEMUFile *f)
4111 475e4277 aliguori
{
4112 475e4277 aliguori
    static ram_addr_t current_addr = 0;
4113 475e4277 aliguori
    ram_addr_t saved_addr = current_addr;
4114 475e4277 aliguori
    ram_addr_t addr = 0;
4115 475e4277 aliguori
    int found = 0;
4116 475e4277 aliguori
4117 475e4277 aliguori
    while (addr < phys_ram_size) {
4118 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
4119 475e4277 aliguori
            uint8_t ch;
4120 475e4277 aliguori
4121 475e4277 aliguori
            cpu_physical_memory_reset_dirty(current_addr,
4122 475e4277 aliguori
                                            current_addr + TARGET_PAGE_SIZE,
4123 475e4277 aliguori
                                            MIGRATION_DIRTY_FLAG);
4124 475e4277 aliguori
4125 475e4277 aliguori
            ch = *(phys_ram_base + current_addr);
4126 475e4277 aliguori
4127 475e4277 aliguori
            if (is_dup_page(phys_ram_base + current_addr, ch)) {
4128 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
4129 475e4277 aliguori
                qemu_put_byte(f, ch);
4130 475e4277 aliguori
            } else {
4131 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
4132 475e4277 aliguori
                qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
4133 c88676f8 bellard
            }
4134 475e4277 aliguori
4135 475e4277 aliguori
            found = 1;
4136 475e4277 aliguori
            break;
4137 c88676f8 bellard
        }
4138 475e4277 aliguori
        addr += TARGET_PAGE_SIZE;
4139 475e4277 aliguori
        current_addr = (saved_addr + addr) % phys_ram_size;
4140 8a7ddc38 bellard
    }
4141 475e4277 aliguori
4142 475e4277 aliguori
    return found;
4143 8a7ddc38 bellard
}
4144 8a7ddc38 bellard
4145 475e4277 aliguori
static ram_addr_t ram_save_threshold = 10;
4146 475e4277 aliguori
4147 475e4277 aliguori
static ram_addr_t ram_save_remaining(void)
4148 475e4277 aliguori
{
4149 475e4277 aliguori
    ram_addr_t addr;
4150 475e4277 aliguori
    ram_addr_t count = 0;
4151 475e4277 aliguori
4152 475e4277 aliguori
    for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
4153 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
4154 475e4277 aliguori
            count++;
4155 475e4277 aliguori
    }
4156 475e4277 aliguori
4157 475e4277 aliguori
    return count;
4158 475e4277 aliguori
}
4159 475e4277 aliguori
4160 475e4277 aliguori
static int ram_save_live(QEMUFile *f, int stage, void *opaque)
4161 475e4277 aliguori
{
4162 475e4277 aliguori
    ram_addr_t addr;
4163 475e4277 aliguori
4164 475e4277 aliguori
    if (stage == 1) {
4165 475e4277 aliguori
        /* Make sure all dirty bits are set */
4166 475e4277 aliguori
        for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
4167 475e4277 aliguori
            if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
4168 475e4277 aliguori
                cpu_physical_memory_set_dirty(addr);
4169 475e4277 aliguori
        }
4170 475e4277 aliguori
        
4171 475e4277 aliguori
        /* Enable dirty memory tracking */
4172 475e4277 aliguori
        cpu_physical_memory_set_dirty_tracking(1);
4173 475e4277 aliguori
4174 475e4277 aliguori
        qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
4175 475e4277 aliguori
    }
4176 475e4277 aliguori
4177 475e4277 aliguori
    while (!qemu_file_rate_limit(f)) {
4178 475e4277 aliguori
        int ret;
4179 475e4277 aliguori
4180 475e4277 aliguori
        ret = ram_save_block(f);
4181 475e4277 aliguori
        if (ret == 0) /* no more blocks */
4182 475e4277 aliguori
            break;
4183 475e4277 aliguori
    }
4184 475e4277 aliguori
4185 475e4277 aliguori
    /* try transferring iterative blocks of memory */
4186 475e4277 aliguori
4187 475e4277 aliguori
    if (stage == 3) {
4188 475e4277 aliguori
        cpu_physical_memory_set_dirty_tracking(0);
4189 475e4277 aliguori
4190 475e4277 aliguori
        /* flush all remaining blocks regardless of rate limiting */
4191 475e4277 aliguori
        while (ram_save_block(f) != 0);
4192 475e4277 aliguori
    }
4193 475e4277 aliguori
4194 475e4277 aliguori
    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
4195 475e4277 aliguori
4196 475e4277 aliguori
    return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
4197 475e4277 aliguori
}
4198 475e4277 aliguori
4199 475e4277 aliguori
static int ram_load_dead(QEMUFile *f, void *opaque)
4200 8a7ddc38 bellard
{
4201 c88676f8 bellard
    RamDecompressState s1, *s = &s1;
4202 c88676f8 bellard
    uint8_t buf[10];
4203 00f82b8a aurel32
    ram_addr_t i;
4204 8a7ddc38 bellard
4205 c88676f8 bellard
    if (ram_decompress_open(s, f) < 0)
4206 c88676f8 bellard
        return -EINVAL;
4207 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
4208 c88676f8 bellard
        if (ram_decompress_buf(s, buf, 1) < 0) {
4209 c88676f8 bellard
            fprintf(stderr, "Error while reading ram block header\n");
4210 c88676f8 bellard
            goto error;
4211 c88676f8 bellard
        }
4212 c88676f8 bellard
        if (buf[0] == 0) {
4213 c88676f8 bellard
            if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
4214 00f82b8a aurel32
                fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
4215 c88676f8 bellard
                goto error;
4216 c88676f8 bellard
            }
4217 475e4277 aliguori
        } else {
4218 c88676f8 bellard
        error:
4219 c88676f8 bellard
            printf("Error block header\n");
4220 c88676f8 bellard
            return -EINVAL;
4221 c88676f8 bellard
        }
4222 8a7ddc38 bellard
    }
4223 c88676f8 bellard
    ram_decompress_close(s);
4224 475e4277 aliguori
4225 475e4277 aliguori
    return 0;
4226 475e4277 aliguori
}
4227 475e4277 aliguori
4228 475e4277 aliguori
static int ram_load(QEMUFile *f, void *opaque, int version_id)
4229 475e4277 aliguori
{
4230 475e4277 aliguori
    ram_addr_t addr;
4231 475e4277 aliguori
    int flags;
4232 475e4277 aliguori
4233 475e4277 aliguori
    if (version_id == 1)
4234 475e4277 aliguori
        return ram_load_v1(f, opaque);
4235 475e4277 aliguori
4236 475e4277 aliguori
    if (version_id == 2) {
4237 475e4277 aliguori
        if (qemu_get_be32(f) != phys_ram_size)
4238 475e4277 aliguori
            return -EINVAL;
4239 475e4277 aliguori
        return ram_load_dead(f, opaque);
4240 475e4277 aliguori
    }
4241 475e4277 aliguori
4242 475e4277 aliguori
    if (version_id != 3)
4243 475e4277 aliguori
        return -EINVAL;
4244 475e4277 aliguori
4245 475e4277 aliguori
    do {
4246 475e4277 aliguori
        addr = qemu_get_be64(f);
4247 475e4277 aliguori
4248 475e4277 aliguori
        flags = addr & ~TARGET_PAGE_MASK;
4249 475e4277 aliguori
        addr &= TARGET_PAGE_MASK;
4250 475e4277 aliguori
4251 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
4252 475e4277 aliguori
            if (addr != phys_ram_size)
4253 475e4277 aliguori
                return -EINVAL;
4254 475e4277 aliguori
        }
4255 475e4277 aliguori
4256 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_FULL) {
4257 475e4277 aliguori
            if (ram_load_dead(f, opaque) < 0)
4258 475e4277 aliguori
                return -EINVAL;
4259 475e4277 aliguori
        }
4260 475e4277 aliguori
        
4261 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_COMPRESS) {
4262 475e4277 aliguori
            uint8_t ch = qemu_get_byte(f);
4263 475e4277 aliguori
            memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
4264 475e4277 aliguori
        } else if (flags & RAM_SAVE_FLAG_PAGE)
4265 475e4277 aliguori
            qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
4266 475e4277 aliguori
    } while (!(flags & RAM_SAVE_FLAG_EOS));
4267 475e4277 aliguori
4268 8a7ddc38 bellard
    return 0;
4269 8a7ddc38 bellard
}
4270 8a7ddc38 bellard
4271 9e472e10 aliguori
void qemu_service_io(void)
4272 9e472e10 aliguori
{
4273 9e472e10 aliguori
    CPUState *env = cpu_single_env;
4274 9e472e10 aliguori
    if (env) {
4275 9e472e10 aliguori
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
4276 9e472e10 aliguori
#ifdef USE_KQEMU
4277 9e472e10 aliguori
        if (env->kqemu_enabled) {
4278 9e472e10 aliguori
            kqemu_cpu_interrupt(env);
4279 9e472e10 aliguori
        }
4280 9e472e10 aliguori
#endif
4281 9e472e10 aliguori
    }
4282 9e472e10 aliguori
}
4283 9e472e10 aliguori
4284 8a7ddc38 bellard
/***********************************************************/
4285 83f64091 bellard
/* bottom halves (can be seen as timers which expire ASAP) */
4286 83f64091 bellard
4287 83f64091 bellard
struct QEMUBH {
4288 83f64091 bellard
    QEMUBHFunc *cb;
4289 83f64091 bellard
    void *opaque;
4290 83f64091 bellard
    int scheduled;
4291 1b435b10 aliguori
    int idle;
4292 1b435b10 aliguori
    int deleted;
4293 83f64091 bellard
    QEMUBH *next;
4294 83f64091 bellard
};
4295 83f64091 bellard
4296 83f64091 bellard
static QEMUBH *first_bh = NULL;
4297 83f64091 bellard
4298 83f64091 bellard
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
4299 83f64091 bellard
{
4300 83f64091 bellard
    QEMUBH *bh;
4301 83f64091 bellard
    bh = qemu_mallocz(sizeof(QEMUBH));
4302 83f64091 bellard
    if (!bh)
4303 83f64091 bellard
        return NULL;
4304 83f64091 bellard
    bh->cb = cb;
4305 83f64091 bellard
    bh->opaque = opaque;
4306 1b435b10 aliguori
    bh->next = first_bh;
4307 1b435b10 aliguori
    first_bh = bh;
4308 83f64091 bellard
    return bh;
4309 83f64091 bellard
}
4310 83f64091 bellard
4311 6eb5733a bellard
int qemu_bh_poll(void)
4312 83f64091 bellard
{
4313 1b435b10 aliguori
    QEMUBH *bh, **bhp;
4314 6eb5733a bellard
    int ret;
4315 83f64091 bellard
4316 6eb5733a bellard
    ret = 0;
4317 1b435b10 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
4318 1b435b10 aliguori
        if (!bh->deleted && bh->scheduled) {
4319 1b435b10 aliguori
            bh->scheduled = 0;
4320 1b435b10 aliguori
            if (!bh->idle)
4321 1b435b10 aliguori
                ret = 1;
4322 1b435b10 aliguori
            bh->idle = 0;
4323 1b435b10 aliguori
            bh->cb(bh->opaque);
4324 1b435b10 aliguori
        }
4325 83f64091 bellard
    }
4326 1b435b10 aliguori
4327 1b435b10 aliguori
    /* remove deleted bhs */
4328 1b435b10 aliguori
    bhp = &first_bh;
4329 1b435b10 aliguori
    while (*bhp) {
4330 1b435b10 aliguori
        bh = *bhp;
4331 1b435b10 aliguori
        if (bh->deleted) {
4332 1b435b10 aliguori
            *bhp = bh->next;
4333 1b435b10 aliguori
            qemu_free(bh);
4334 1b435b10 aliguori
        } else
4335 1b435b10 aliguori
            bhp = &bh->next;
4336 1b435b10 aliguori
    }
4337 1b435b10 aliguori
4338 6eb5733a bellard
    return ret;
4339 83f64091 bellard
}
4340 83f64091 bellard
4341 1b435b10 aliguori
void qemu_bh_schedule_idle(QEMUBH *bh)
4342 1b435b10 aliguori
{
4343 1b435b10 aliguori
    if (bh->scheduled)
4344 1b435b10 aliguori
        return;
4345 1b435b10 aliguori
    bh->scheduled = 1;
4346 1b435b10 aliguori
    bh->idle = 1;
4347 1b435b10 aliguori
}
4348 1b435b10 aliguori
4349 83f64091 bellard
void qemu_bh_schedule(QEMUBH *bh)
4350 83f64091 bellard
{
4351 83f64091 bellard
    CPUState *env = cpu_single_env;
4352 83f64091 bellard
    if (bh->scheduled)
4353 83f64091 bellard
        return;
4354 83f64091 bellard
    bh->scheduled = 1;
4355 1b435b10 aliguori
    bh->idle = 0;
4356 83f64091 bellard
    /* stop the currently executing CPU to execute the BH ASAP */
4357 83f64091 bellard
    if (env) {
4358 83f64091 bellard
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
4359 83f64091 bellard
    }
4360 83f64091 bellard
}
4361 83f64091 bellard
4362 83f64091 bellard
void qemu_bh_cancel(QEMUBH *bh)
4363 83f64091 bellard
{
4364 1b435b10 aliguori
    bh->scheduled = 0;
4365 83f64091 bellard
}
4366 83f64091 bellard
4367 83f64091 bellard
void qemu_bh_delete(QEMUBH *bh)
4368 83f64091 bellard
{
4369 1b435b10 aliguori
    bh->scheduled = 0;
4370 1b435b10 aliguori
    bh->deleted = 1;
4371 83f64091 bellard
}
4372 83f64091 bellard
4373 56f3a5d0 aliguori
static void qemu_bh_update_timeout(int *timeout)
4374 56f3a5d0 aliguori
{
4375 56f3a5d0 aliguori
    QEMUBH *bh;
4376 56f3a5d0 aliguori
4377 56f3a5d0 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
4378 56f3a5d0 aliguori
        if (!bh->deleted && bh->scheduled) {
4379 56f3a5d0 aliguori
            if (bh->idle) {
4380 56f3a5d0 aliguori
                /* idle bottom halves will be polled at least
4381 56f3a5d0 aliguori
                 * every 10ms */
4382 56f3a5d0 aliguori
                *timeout = MIN(10, *timeout);
4383 56f3a5d0 aliguori
            } else {
4384 56f3a5d0 aliguori
                /* non-idle bottom halves will be executed
4385 56f3a5d0 aliguori
                 * immediately */
4386 56f3a5d0 aliguori
                *timeout = 0;
4387 56f3a5d0 aliguori
                break;
4388 56f3a5d0 aliguori
            }
4389 56f3a5d0 aliguori
        }
4390 56f3a5d0 aliguori
    }
4391 56f3a5d0 aliguori
}
4392 56f3a5d0 aliguori
4393 83f64091 bellard
/***********************************************************/
4394 cc1daa40 bellard
/* machine registration */
4395 cc1daa40 bellard
4396 bdaf78e0 blueswir1
static QEMUMachine *first_machine = NULL;
4397 cc1daa40 bellard
4398 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
4399 cc1daa40 bellard
{
4400 cc1daa40 bellard
    QEMUMachine **pm;
4401 cc1daa40 bellard
    pm = &first_machine;
4402 cc1daa40 bellard
    while (*pm != NULL)
4403 cc1daa40 bellard
        pm = &(*pm)->next;
4404 cc1daa40 bellard
    m->next = NULL;
4405 cc1daa40 bellard
    *pm = m;
4406 cc1daa40 bellard
    return 0;
4407 cc1daa40 bellard
}
4408 cc1daa40 bellard
4409 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
4410 cc1daa40 bellard
{
4411 cc1daa40 bellard
    QEMUMachine *m;
4412 cc1daa40 bellard
4413 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
4414 cc1daa40 bellard
        if (!strcmp(m->name, name))
4415 cc1daa40 bellard
            return m;
4416 cc1daa40 bellard
    }
4417 cc1daa40 bellard
    return NULL;
4418 cc1daa40 bellard
}
4419 cc1daa40 bellard
4420 cc1daa40 bellard
/***********************************************************/
4421 8a7ddc38 bellard
/* main execution loop */
4422 8a7ddc38 bellard
4423 9596ebb7 pbrook
static void gui_update(void *opaque)
4424 8a7ddc38 bellard
{
4425 740733bb ths
    DisplayState *ds = opaque;
4426 740733bb ths
    ds->dpy_refresh(ds);
4427 f442e08b aurel32
    qemu_mod_timer(ds->gui_timer,
4428 f442e08b aurel32
        (ds->gui_timer_interval ?
4429 f442e08b aurel32
            ds->gui_timer_interval :
4430 f442e08b aurel32
            GUI_REFRESH_INTERVAL)
4431 f442e08b aurel32
        + qemu_get_clock(rt_clock));
4432 8a7ddc38 bellard
}
4433 8a7ddc38 bellard
4434 0bd48850 bellard
struct vm_change_state_entry {
4435 0bd48850 bellard
    VMChangeStateHandler *cb;
4436 0bd48850 bellard
    void *opaque;
4437 0bd48850 bellard
    LIST_ENTRY (vm_change_state_entry) entries;
4438 0bd48850 bellard
};
4439 0bd48850 bellard
4440 0bd48850 bellard
static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
4441 0bd48850 bellard
4442 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
4443 0bd48850 bellard
                                                     void *opaque)
4444 0bd48850 bellard
{
4445 0bd48850 bellard
    VMChangeStateEntry *e;
4446 0bd48850 bellard
4447 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
4448 0bd48850 bellard
    if (!e)
4449 0bd48850 bellard
        return NULL;
4450 0bd48850 bellard
4451 0bd48850 bellard
    e->cb = cb;
4452 0bd48850 bellard
    e->opaque = opaque;
4453 0bd48850 bellard
    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
4454 0bd48850 bellard
    return e;
4455 0bd48850 bellard
}
4456 0bd48850 bellard
4457 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
4458 0bd48850 bellard
{
4459 0bd48850 bellard
    LIST_REMOVE (e, entries);
4460 0bd48850 bellard
    qemu_free (e);
4461 0bd48850 bellard
}
4462 0bd48850 bellard
4463 0bd48850 bellard
static void vm_state_notify(int running)
4464 0bd48850 bellard
{
4465 0bd48850 bellard
    VMChangeStateEntry *e;
4466 0bd48850 bellard
4467 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
4468 0bd48850 bellard
        e->cb(e->opaque, running);
4469 0bd48850 bellard
    }
4470 0bd48850 bellard
}
4471 0bd48850 bellard
4472 8a7ddc38 bellard
/* XXX: support several handlers */
4473 0bd48850 bellard
static VMStopHandler *vm_stop_cb;
4474 0bd48850 bellard
static void *vm_stop_opaque;
4475 8a7ddc38 bellard
4476 8a7ddc38 bellard
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
4477 8a7ddc38 bellard
{
4478 8a7ddc38 bellard
    vm_stop_cb = cb;
4479 8a7ddc38 bellard
    vm_stop_opaque = opaque;
4480 8a7ddc38 bellard
    return 0;
4481 8a7ddc38 bellard
}
4482 8a7ddc38 bellard
4483 8a7ddc38 bellard
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
4484 8a7ddc38 bellard
{
4485 8a7ddc38 bellard
    vm_stop_cb = NULL;
4486 8a7ddc38 bellard
}
4487 8a7ddc38 bellard
4488 8a7ddc38 bellard
void vm_start(void)
4489 8a7ddc38 bellard
{
4490 8a7ddc38 bellard
    if (!vm_running) {
4491 8a7ddc38 bellard
        cpu_enable_ticks();
4492 8a7ddc38 bellard
        vm_running = 1;
4493 0bd48850 bellard
        vm_state_notify(1);
4494 efe75411 ths
        qemu_rearm_alarm_timer(alarm_timer);
4495 8a7ddc38 bellard
    }
4496 8a7ddc38 bellard
}
4497 8a7ddc38 bellard
4498 5fafdf24 ths
void vm_stop(int reason)
4499 8a7ddc38 bellard
{
4500 8a7ddc38 bellard
    if (vm_running) {
4501 8a7ddc38 bellard
        cpu_disable_ticks();
4502 8a7ddc38 bellard
        vm_running = 0;
4503 8a7ddc38 bellard
        if (reason != 0) {
4504 8a7ddc38 bellard
            if (vm_stop_cb) {
4505 8a7ddc38 bellard
                vm_stop_cb(vm_stop_opaque, reason);
4506 8a7ddc38 bellard
            }
4507 34865134 bellard
        }
4508 0bd48850 bellard
        vm_state_notify(0);
4509 8a7ddc38 bellard
    }
4510 8a7ddc38 bellard
}
4511 8a7ddc38 bellard
4512 bb0c6722 bellard
/* reset/shutdown handler */
4513 bb0c6722 bellard
4514 bb0c6722 bellard
typedef struct QEMUResetEntry {
4515 bb0c6722 bellard
    QEMUResetHandler *func;
4516 bb0c6722 bellard
    void *opaque;
4517 bb0c6722 bellard
    struct QEMUResetEntry *next;
4518 bb0c6722 bellard
} QEMUResetEntry;
4519 bb0c6722 bellard
4520 bb0c6722 bellard
static QEMUResetEntry *first_reset_entry;
4521 bb0c6722 bellard
static int reset_requested;
4522 bb0c6722 bellard
static int shutdown_requested;
4523 3475187d bellard
static int powerdown_requested;
4524 bb0c6722 bellard
4525 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
4526 cf7a2fe2 aurel32
{
4527 cf7a2fe2 aurel32
    int r = shutdown_requested;
4528 cf7a2fe2 aurel32
    shutdown_requested = 0;
4529 cf7a2fe2 aurel32
    return r;
4530 cf7a2fe2 aurel32
}
4531 cf7a2fe2 aurel32
4532 cf7a2fe2 aurel32
int qemu_reset_requested(void)
4533 cf7a2fe2 aurel32
{
4534 cf7a2fe2 aurel32
    int r = reset_requested;
4535 cf7a2fe2 aurel32
    reset_requested = 0;
4536 cf7a2fe2 aurel32
    return r;
4537 cf7a2fe2 aurel32
}
4538 cf7a2fe2 aurel32
4539 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
4540 cf7a2fe2 aurel32
{
4541 cf7a2fe2 aurel32
    int r = powerdown_requested;
4542 cf7a2fe2 aurel32
    powerdown_requested = 0;
4543 cf7a2fe2 aurel32
    return r;
4544 cf7a2fe2 aurel32
}
4545 cf7a2fe2 aurel32
4546 bb0c6722 bellard
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
4547 bb0c6722 bellard
{
4548 bb0c6722 bellard
    QEMUResetEntry **pre, *re;
4549 bb0c6722 bellard
4550 bb0c6722 bellard
    pre = &first_reset_entry;
4551 bb0c6722 bellard
    while (*pre != NULL)
4552 bb0c6722 bellard
        pre = &(*pre)->next;
4553 bb0c6722 bellard
    re = qemu_mallocz(sizeof(QEMUResetEntry));
4554 bb0c6722 bellard
    re->func = func;
4555 bb0c6722 bellard
    re->opaque = opaque;
4556 bb0c6722 bellard
    re->next = NULL;
4557 bb0c6722 bellard
    *pre = re;
4558 bb0c6722 bellard
}
4559 bb0c6722 bellard
4560 cf7a2fe2 aurel32
void qemu_system_reset(void)
4561 bb0c6722 bellard
{
4562 bb0c6722 bellard
    QEMUResetEntry *re;
4563 bb0c6722 bellard
4564 bb0c6722 bellard
    /* reset all devices */
4565 bb0c6722 bellard
    for(re = first_reset_entry; re != NULL; re = re->next) {
4566 bb0c6722 bellard
        re->func(re->opaque);
4567 bb0c6722 bellard
    }
4568 bb0c6722 bellard
}
4569 bb0c6722 bellard
4570 bb0c6722 bellard
void qemu_system_reset_request(void)
4571 bb0c6722 bellard
{
4572 d1beab82 bellard
    if (no_reboot) {
4573 d1beab82 bellard
        shutdown_requested = 1;
4574 d1beab82 bellard
    } else {
4575 d1beab82 bellard
        reset_requested = 1;
4576 d1beab82 bellard
    }
4577 6a00d601 bellard
    if (cpu_single_env)
4578 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4579 bb0c6722 bellard
}
4580 bb0c6722 bellard
4581 bb0c6722 bellard
void qemu_system_shutdown_request(void)
4582 bb0c6722 bellard
{
4583 bb0c6722 bellard
    shutdown_requested = 1;
4584 6a00d601 bellard
    if (cpu_single_env)
4585 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4586 bb0c6722 bellard
}
4587 bb0c6722 bellard
4588 3475187d bellard
void qemu_system_powerdown_request(void)
4589 3475187d bellard
{
4590 3475187d bellard
    powerdown_requested = 1;
4591 6a00d601 bellard
    if (cpu_single_env)
4592 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4593 bb0c6722 bellard
}
4594 bb0c6722 bellard
4595 877cf882 ths
#ifdef _WIN32
4596 56f3a5d0 aliguori
void host_main_loop_wait(int *timeout)
4597 56f3a5d0 aliguori
{
4598 56f3a5d0 aliguori
    int ret, ret2, i;
4599 f331110f bellard
    PollingEntry *pe;
4600 f331110f bellard
4601 c4b1fcc0 bellard
4602 f331110f bellard
    /* XXX: need to suppress polling by better using win32 events */
4603 f331110f bellard
    ret = 0;
4604 f331110f bellard
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4605 f331110f bellard
        ret |= pe->func(pe->opaque);
4606 f331110f bellard
    }
4607 e6b1e558 ths
    if (ret == 0) {
4608 a18e524a bellard
        int err;
4609 a18e524a bellard
        WaitObjects *w = &wait_objects;
4610 3b46e624 ths
4611 56f3a5d0 aliguori
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
4612 a18e524a bellard
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4613 a18e524a bellard
            if (w->func[ret - WAIT_OBJECT_0])
4614 a18e524a bellard
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
4615 3b46e624 ths
4616 5fafdf24 ths
            /* Check for additional signaled events */
4617 e6b1e558 ths
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
4618 3b46e624 ths
4619 e6b1e558 ths
                /* Check if event is signaled */
4620 e6b1e558 ths
                ret2 = WaitForSingleObject(w->events[i], 0);
4621 e6b1e558 ths
                if(ret2 == WAIT_OBJECT_0) {
4622 e6b1e558 ths
                    if (w->func[i])
4623 e6b1e558 ths
                        w->func[i](w->opaque[i]);
4624 e6b1e558 ths
                } else if (ret2 == WAIT_TIMEOUT) {
4625 e6b1e558 ths
                } else {
4626 e6b1e558 ths
                    err = GetLastError();
4627 e6b1e558 ths
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
4628 3b46e624 ths
                }
4629 3b46e624 ths
            }
4630 a18e524a bellard
        } else if (ret == WAIT_TIMEOUT) {
4631 a18e524a bellard
        } else {
4632 a18e524a bellard
            err = GetLastError();
4633 e6b1e558 ths
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
4634 a18e524a bellard
        }
4635 f331110f bellard
    }
4636 56f3a5d0 aliguori
4637 56f3a5d0 aliguori
    *timeout = 0;
4638 56f3a5d0 aliguori
}
4639 56f3a5d0 aliguori
#else
4640 56f3a5d0 aliguori
void host_main_loop_wait(int *timeout)
4641 56f3a5d0 aliguori
{
4642 56f3a5d0 aliguori
}
4643 fd1dff4b bellard
#endif
4644 56f3a5d0 aliguori
4645 56f3a5d0 aliguori
void main_loop_wait(int timeout)
4646 56f3a5d0 aliguori
{
4647 56f3a5d0 aliguori
    IOHandlerRecord *ioh;
4648 56f3a5d0 aliguori
    fd_set rfds, wfds, xfds;
4649 56f3a5d0 aliguori
    int ret, nfds;
4650 56f3a5d0 aliguori
    struct timeval tv;
4651 56f3a5d0 aliguori
4652 56f3a5d0 aliguori
    qemu_bh_update_timeout(&timeout);
4653 56f3a5d0 aliguori
4654 56f3a5d0 aliguori
    host_main_loop_wait(&timeout);
4655 56f3a5d0 aliguori
4656 fd1dff4b bellard
    /* poll any events */
4657 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
4658 6abfbd79 aliguori
    nfds = -1;
4659 fd1dff4b bellard
    FD_ZERO(&rfds);
4660 fd1dff4b bellard
    FD_ZERO(&wfds);
4661 e035649e bellard
    FD_ZERO(&xfds);
4662 fd1dff4b bellard
    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4663 cafffd40 ths
        if (ioh->deleted)
4664 cafffd40 ths
            continue;
4665 fd1dff4b bellard
        if (ioh->fd_read &&
4666 fd1dff4b bellard
            (!ioh->fd_read_poll ||
4667 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
4668 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
4669 fd1dff4b bellard
            if (ioh->fd > nfds)
4670 fd1dff4b bellard
                nfds = ioh->fd;
4671 fd1dff4b bellard
        }
4672 fd1dff4b bellard
        if (ioh->fd_write) {
4673 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
4674 fd1dff4b bellard
            if (ioh->fd > nfds)
4675 fd1dff4b bellard
                nfds = ioh->fd;
4676 fd1dff4b bellard
        }
4677 fd1dff4b bellard
    }
4678 3b46e624 ths
4679 56f3a5d0 aliguori
    tv.tv_sec = timeout / 1000;
4680 56f3a5d0 aliguori
    tv.tv_usec = (timeout % 1000) * 1000;
4681 56f3a5d0 aliguori
4682 e035649e bellard
#if defined(CONFIG_SLIRP)
4683 63a01ef8 aliguori
    if (slirp_is_inited()) {
4684 e035649e bellard
        slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4685 e035649e bellard
    }
4686 e035649e bellard
#endif
4687 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4688 fd1dff4b bellard
    if (ret > 0) {
4689 cafffd40 ths
        IOHandlerRecord **pioh;
4690 cafffd40 ths
4691 cafffd40 ths
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4692 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
4693 fd1dff4b bellard
                ioh->fd_read(ioh->opaque);
4694 7c9d8e07 bellard
            }
4695 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
4696 fd1dff4b bellard
                ioh->fd_write(ioh->opaque);
4697 c4b1fcc0 bellard
            }
4698 b4608c04 bellard
        }
4699 cafffd40 ths
4700 cafffd40 ths
        /* remove deleted IO handlers */
4701 cafffd40 ths
        pioh = &first_io_handler;
4702 cafffd40 ths
        while (*pioh) {
4703 cafffd40 ths
            ioh = *pioh;
4704 cafffd40 ths
            if (ioh->deleted) {
4705 cafffd40 ths
                *pioh = ioh->next;
4706 cafffd40 ths
                qemu_free(ioh);
4707 5fafdf24 ths
            } else
4708 cafffd40 ths
                pioh = &ioh->next;
4709 cafffd40 ths
        }
4710 fd1dff4b bellard
    }
4711 c20709aa bellard
#if defined(CONFIG_SLIRP)
4712 63a01ef8 aliguori
    if (slirp_is_inited()) {
4713 e035649e bellard
        if (ret < 0) {
4714 e035649e bellard
            FD_ZERO(&rfds);
4715 e035649e bellard
            FD_ZERO(&wfds);
4716 e035649e bellard
            FD_ZERO(&xfds);
4717 c20709aa bellard
        }
4718 e035649e bellard
        slirp_select_poll(&rfds, &wfds, &xfds);
4719 fd1dff4b bellard
    }
4720 c20709aa bellard
#endif
4721 c20709aa bellard
4722 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
4723 423f0742 pbrook
       them.  */
4724 423f0742 pbrook
    qemu_bh_poll();
4725 3b46e624 ths
4726 5905b2e5 bellard
}
4727 5905b2e5 bellard
4728 9596ebb7 pbrook
static int main_loop(void)
4729 5905b2e5 bellard
{
4730 5905b2e5 bellard
    int ret, timeout;
4731 89bfc105 bellard
#ifdef CONFIG_PROFILER
4732 89bfc105 bellard
    int64_t ti;
4733 89bfc105 bellard
#endif
4734 6a00d601 bellard
    CPUState *env;
4735 5905b2e5 bellard
4736 6a00d601 bellard
    cur_cpu = first_cpu;
4737 ee5605e5 balrog
    next_cpu = cur_cpu->next_cpu ?: first_cpu;
4738 5905b2e5 bellard
    for(;;) {
4739 5905b2e5 bellard
        if (vm_running) {
4740 15a76449 bellard
4741 15a76449 bellard
            for(;;) {
4742 15a76449 bellard
                /* get next cpu */
4743 ee5605e5 balrog
                env = next_cpu;
4744 89bfc105 bellard
#ifdef CONFIG_PROFILER
4745 89bfc105 bellard
                ti = profile_getclock();
4746 89bfc105 bellard
#endif
4747 2e70f6ef pbrook
                if (use_icount) {
4748 2e70f6ef pbrook
                    int64_t count;
4749 2e70f6ef pbrook
                    int decr;
4750 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4751 2e70f6ef pbrook
                    env->icount_decr.u16.low = 0;
4752 2e70f6ef pbrook
                    env->icount_extra = 0;
4753 2e70f6ef pbrook
                    count = qemu_next_deadline();
4754 2e70f6ef pbrook
                    count = (count + (1 << icount_time_shift) - 1)
4755 2e70f6ef pbrook
                            >> icount_time_shift;
4756 2e70f6ef pbrook
                    qemu_icount += count;
4757 2e70f6ef pbrook
                    decr = (count > 0xffff) ? 0xffff : count;
4758 2e70f6ef pbrook
                    count -= decr;
4759 2e70f6ef pbrook
                    env->icount_decr.u16.low = decr;
4760 2e70f6ef pbrook
                    env->icount_extra = count;
4761 2e70f6ef pbrook
                }
4762 6a00d601 bellard
                ret = cpu_exec(env);
4763 89bfc105 bellard
#ifdef CONFIG_PROFILER
4764 89bfc105 bellard
                qemu_time += profile_getclock() - ti;
4765 89bfc105 bellard
#endif
4766 2e70f6ef pbrook
                if (use_icount) {
4767 2e70f6ef pbrook
                    /* Fold pending instructions back into the
4768 2e70f6ef pbrook
                       instruction counter, and clear the interrupt flag.  */
4769 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low
4770 2e70f6ef pbrook
                                    + env->icount_extra);
4771 2e70f6ef pbrook
                    env->icount_decr.u32 = 0;
4772 2e70f6ef pbrook
                    env->icount_extra = 0;
4773 2e70f6ef pbrook
                }
4774 ee5605e5 balrog
                next_cpu = env->next_cpu ?: first_cpu;
4775 95b01009 aurel32
                if (event_pending && likely(ret != EXCP_DEBUG)) {
4776 ee5605e5 balrog
                    ret = EXCP_INTERRUPT;
4777 ee5605e5 balrog
                    event_pending = 0;
4778 ee5605e5 balrog
                    break;
4779 ee5605e5 balrog
                }
4780 bd967e05 pbrook
                if (ret == EXCP_HLT) {
4781 bd967e05 pbrook
                    /* Give the next CPU a chance to run.  */
4782 bd967e05 pbrook
                    cur_cpu = env;
4783 bd967e05 pbrook
                    continue;
4784 bd967e05 pbrook
                }
4785 15a76449 bellard
                if (ret != EXCP_HALTED)
4786 15a76449 bellard
                    break;
4787 15a76449 bellard
                /* all CPUs are halted ? */
4788 bd967e05 pbrook
                if (env == cur_cpu)
4789 15a76449 bellard
                    break;
4790 15a76449 bellard
            }
4791 15a76449 bellard
            cur_cpu = env;
4792 15a76449 bellard
4793 5905b2e5 bellard
            if (shutdown_requested) {
4794 3475187d bellard
                ret = EXCP_INTERRUPT;
4795 b2f76161 aurel32
                if (no_shutdown) {
4796 b2f76161 aurel32
                    vm_stop(0);
4797 b2f76161 aurel32
                    no_shutdown = 0;
4798 b2f76161 aurel32
                }
4799 b2f76161 aurel32
                else
4800 b2f76161 aurel32
                    break;
4801 5905b2e5 bellard
            }
4802 5905b2e5 bellard
            if (reset_requested) {
4803 5905b2e5 bellard
                reset_requested = 0;
4804 5905b2e5 bellard
                qemu_system_reset();
4805 3475187d bellard
                ret = EXCP_INTERRUPT;
4806 3475187d bellard
            }
4807 3475187d bellard
            if (powerdown_requested) {
4808 3475187d bellard
                powerdown_requested = 0;
4809 3475187d bellard
                qemu_system_powerdown();
4810 3475187d bellard
                ret = EXCP_INTERRUPT;
4811 5905b2e5 bellard
            }
4812 95b01009 aurel32
            if (unlikely(ret == EXCP_DEBUG)) {
4813 5905b2e5 bellard
                vm_stop(EXCP_DEBUG);
4814 5905b2e5 bellard
            }
4815 bd967e05 pbrook
            /* If all cpus are halted then wait until the next IRQ */
4816 5905b2e5 bellard
            /* XXX: use timeout computed from timers */
4817 2e70f6ef pbrook
            if (ret == EXCP_HALTED) {
4818 2e70f6ef pbrook
                if (use_icount) {
4819 2e70f6ef pbrook
                    int64_t add;
4820 2e70f6ef pbrook
                    int64_t delta;
4821 2e70f6ef pbrook
                    /* Advance virtual time to the next event.  */
4822 2e70f6ef pbrook
                    if (use_icount == 1) {
4823 2e70f6ef pbrook
                        /* When not using an adaptive execution frequency
4824 2e70f6ef pbrook
                           we tend to get badly out of sync with real time,
4825 bf20dc07 ths
                           so just delay for a reasonable amount of time.  */
4826 2e70f6ef pbrook
                        delta = 0;
4827 2e70f6ef pbrook
                    } else {
4828 2e70f6ef pbrook
                        delta = cpu_get_icount() - cpu_get_clock();
4829 2e70f6ef pbrook
                    }
4830 2e70f6ef pbrook
                    if (delta > 0) {
4831 2e70f6ef pbrook
                        /* If virtual time is ahead of real time then just
4832 2e70f6ef pbrook
                           wait for IO.  */
4833 2e70f6ef pbrook
                        timeout = (delta / 1000000) + 1;
4834 2e70f6ef pbrook
                    } else {
4835 2e70f6ef pbrook
                        /* Wait for either IO to occur or the next
4836 2e70f6ef pbrook
                           timer event.  */
4837 2e70f6ef pbrook
                        add = qemu_next_deadline();
4838 2e70f6ef pbrook
                        /* We advance the timer before checking for IO.
4839 2e70f6ef pbrook
                           Limit the amount we advance so that early IO
4840 2e70f6ef pbrook
                           activity won't get the guest too far ahead.  */
4841 2e70f6ef pbrook
                        if (add > 10000000)
4842 2e70f6ef pbrook
                            add = 10000000;
4843 2e70f6ef pbrook
                        delta += add;
4844 2e70f6ef pbrook
                        add = (add + (1 << icount_time_shift) - 1)
4845 2e70f6ef pbrook
                              >> icount_time_shift;
4846 2e70f6ef pbrook
                        qemu_icount += add;
4847 2e70f6ef pbrook
                        timeout = delta / 1000000;
4848 2e70f6ef pbrook
                        if (timeout < 0)
4849 2e70f6ef pbrook
                            timeout = 0;
4850 2e70f6ef pbrook
                    }
4851 2e70f6ef pbrook
                } else {
4852 0a1af395 aliguori
                    timeout = 5000;
4853 2e70f6ef pbrook
                }
4854 2e70f6ef pbrook
            } else {
4855 5905b2e5 bellard
                timeout = 0;
4856 2e70f6ef pbrook
            }
4857 5905b2e5 bellard
        } else {
4858 98448f58 blueswir1
            if (shutdown_requested) {
4859 98448f58 blueswir1
                ret = EXCP_INTERRUPT;
4860 5b08fc10 aliguori
                break;
4861 98448f58 blueswir1
            }
4862 0a1af395 aliguori
            timeout = 5000;
4863 5905b2e5 bellard
        }
4864 89bfc105 bellard
#ifdef CONFIG_PROFILER
4865 89bfc105 bellard
        ti = profile_getclock();
4866 89bfc105 bellard
#endif
4867 5905b2e5 bellard
        main_loop_wait(timeout);
4868 89bfc105 bellard
#ifdef CONFIG_PROFILER
4869 89bfc105 bellard
        dev_time += profile_getclock() - ti;
4870 89bfc105 bellard
#endif
4871 b4608c04 bellard
    }
4872 34865134 bellard
    cpu_disable_ticks();
4873 34865134 bellard
    return ret;
4874 b4608c04 bellard
}
4875 b4608c04 bellard
4876 15f82208 ths
static void help(int exitcode)
4877 0824d6fc bellard
{
4878 68d0f70e bellard
    printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
4879 0db63474 bellard
           "usage: %s [options] [disk_image]\n"
4880 0824d6fc bellard
           "\n"
4881 a20dd508 bellard
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4882 fc01f7e7 bellard
           "\n"
4883 a20dd508 bellard
           "Standard options:\n"
4884 cc1daa40 bellard
           "-M machine      select emulated machine (-M ? for list)\n"
4885 5adb4839 pbrook
           "-cpu cpu        select CPU (-cpu ? for list)\n"
4886 c45886db bellard
           "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
4887 36b486bb bellard
           "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
4888 36b486bb bellard
           "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
4889 c4b1fcc0 bellard
           "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4890 a1620fac aurel32
           "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4891 a1620fac aurel32
           "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
4892 9f7965c7 aliguori
           "       [,cache=writethrough|writeback|none][,format=f]\n"
4893 e4bcb14c ths
           "                use 'file' as a drive image\n"
4894 3e3d5815 balrog
           "-mtdblock file  use 'file' as on-board Flash memory image\n"
4895 a1bb27b1 pbrook
           "-sd file        use 'file' as SecureDigital card image\n"
4896 86f55663 j_mayer
           "-pflash file    use 'file' as a parallel flash image\n"
4897 eec85c2a ths
           "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4898 667accab ths
           "-snapshot       write to temporary files instead of disk image files\n"
4899 667accab ths
#ifdef CONFIG_SDL
4900 43523e93 ths
           "-no-frame       open SDL window without a frame and window decorations\n"
4901 3780e197 ths
           "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4902 667accab ths
           "-no-quit        disable SDL window close capability\n"
4903 667accab ths
#endif
4904 52ca8d6a bellard
#ifdef TARGET_I386
4905 52ca8d6a bellard
           "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
4906 52ca8d6a bellard
#endif
4907 a00bad7e bellard
           "-m megs         set virtual RAM size to megs MB [default=%d]\n"
4908 91fc2119 bellard
           "-smp n          set the number of CPUs to 'n' [default=1]\n"
4909 c4b1fcc0 bellard
           "-nographic      disable graphical output and redirect serial I/Os to console\n"
4910 a171fe39 balrog
           "-portrait       rotate graphical output 90 deg left (only PXA LCD)\n"
4911 4ca0074c bellard
#ifndef _WIN32
4912 667accab ths
           "-k language     use keyboard layout (for example \"fr\" for French)\n"
4913 4ca0074c bellard
#endif
4914 1d14ffa9 bellard
#ifdef HAS_AUDIO
4915 1d14ffa9 bellard
           "-audio-help     print list of audio drivers and their options\n"
4916 c0fe3827 bellard
           "-soundhw c1,... enable audio support\n"
4917 c0fe3827 bellard
           "                and only specified sound cards (comma separated list)\n"
4918 c0fe3827 bellard
           "                use -soundhw ? to get the list of supported cards\n"
4919 6a36d84e bellard
           "                use -soundhw all to enable all of them\n"
4920 1d14ffa9 bellard
#endif
4921 3893c124 malc
           "-vga [std|cirrus|vmware]\n"
4922 3893c124 malc
           "                select video card type\n"
4923 89980284 bellard
           "-localtime      set the real time clock to local time [default=utc]\n"
4924 d63d307f bellard
           "-full-screen    start in full screen\n"
4925 a09db21f bellard
#ifdef TARGET_I386
4926 a09db21f bellard
           "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
4927 a09db21f bellard
#endif
4928 b389dbfb bellard
           "-usb            enable the USB driver (will be the default soon)\n"
4929 b389dbfb bellard
           "-usbdevice name add the host or guest USB device 'name'\n"
4930 6f7e9aec bellard
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4931 6f7e9aec bellard
           "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
4932 bb0c6722 bellard
#endif
4933 c35734b2 ths
           "-name string    set the name of the guest\n"
4934 8fcb1b90 blueswir1
           "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4935 c4b1fcc0 bellard
           "\n"
4936 c4b1fcc0 bellard
           "Network options:\n"
4937 a41b2ff2 pbrook
           "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4938 7c9d8e07 bellard
           "                create a new Network Interface Card and connect it to VLAN 'n'\n"
4939 c20709aa bellard
#ifdef CONFIG_SLIRP
4940 115defd1 pbrook
           "-net user[,vlan=n][,hostname=host]\n"
4941 115defd1 pbrook
           "                connect the user mode network stack to VLAN 'n' and send\n"
4942 115defd1 pbrook
           "                hostname 'host' to DHCP clients\n"
4943 7c9d8e07 bellard
#endif
4944 7fb843f8 bellard
#ifdef _WIN32
4945 7fb843f8 bellard
           "-net tap[,vlan=n],ifname=name\n"
4946 7fb843f8 bellard
           "                connect the host TAP network interface to VLAN 'n'\n"
4947 7fb843f8 bellard
#else
4948 b46a8906 ths
           "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4949 b46a8906 ths
           "                connect the host TAP network interface to VLAN 'n' and use the\n"
4950 b46a8906 ths
           "                network scripts 'file' (default=%s)\n"
4951 b46a8906 ths
           "                and 'dfile' (default=%s);\n"
4952 b46a8906 ths
           "                use '[down]script=no' to disable script execution;\n"
4953 7c9d8e07 bellard
           "                use 'fd=h' to connect to an already opened TAP interface\n"
4954 7fb843f8 bellard
#endif
4955 6a00d601 bellard
           "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4956 7c9d8e07 bellard
           "                connect the vlan 'n' to another VLAN using a socket connection\n"
4957 3d830459 bellard
           "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4958 3d830459 bellard
           "                connect the vlan 'n' to multicast maddr and port\n"
4959 8a16d273 ths
#ifdef CONFIG_VDE
4960 8a16d273 ths
           "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4961 8a16d273 ths
           "                connect the vlan 'n' to port 'n' of a vde switch running\n"
4962 8a16d273 ths
           "                on host and listening for incoming connections on 'socketpath'.\n"
4963 8a16d273 ths
           "                Use group 'groupname' and mode 'octalmode' to change default\n"
4964 8a16d273 ths
           "                ownership and permissions for communication port.\n"
4965 8a16d273 ths
#endif
4966 7c9d8e07 bellard
           "-net none       use it alone to have zero network devices; if no -net option\n"
4967 7c9d8e07 bellard
           "                is provided, the default is '-net nic -net user'\n"
4968 7c9d8e07 bellard
           "\n"
4969 dc72ac14 balrog
           "-bt hci,null    Dumb bluetooth HCI - doesn't respond to commands\n"
4970 dc72ac14 balrog
           "-bt hci,host[:id]\n"
4971 dc72ac14 balrog
           "                Use host's HCI with the given name\n"
4972 dc72ac14 balrog
           "-bt hci[,vlan=n]\n"
4973 dc72ac14 balrog
           "                Emulate a standard HCI in virtual scatternet 'n'\n"
4974 dc72ac14 balrog
           "-bt vhci[,vlan=n]\n"
4975 dc72ac14 balrog
           "                Add host computer to virtual scatternet 'n' using VHCI\n"
4976 dc72ac14 balrog
           "-bt device:dev[,vlan=n]\n"
4977 dc72ac14 balrog
           "                Emulate a bluetooth device 'dev' in scatternet 'n'\n"
4978 dc72ac14 balrog
           "\n"
4979 7c9d8e07 bellard
#ifdef CONFIG_SLIRP
4980 0db1137d ths
           "-tftp dir       allow tftp access to files in dir [-net user]\n"
4981 47d5d01a ths
           "-bootp file     advertise file in BOOTP replies\n"
4982 7c9d8e07 bellard
#ifndef _WIN32
4983 7c9d8e07 bellard
           "-smb dir        allow SMB access to files in 'dir' [-net user]\n"
4984 c94c8d64 bellard
#endif
4985 9bf05444 bellard
           "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4986 7c9d8e07 bellard
           "                redirect TCP or UDP connections from host to guest [-net user]\n"
4987 c20709aa bellard
#endif
4988 a20dd508 bellard
           "\n"
4989 c4b1fcc0 bellard
           "Linux boot specific:\n"
4990 a20dd508 bellard
           "-kernel bzImage use 'bzImage' as kernel image\n"
4991 a20dd508 bellard
           "-append cmdline use 'cmdline' as kernel command line\n"
4992 a20dd508 bellard
           "-initrd file    use 'file' as initial ram disk\n"
4993 fc01f7e7 bellard
           "\n"
4994 330d0414 bellard
           "Debug/Expert options:\n"
4995 82c643ff bellard
           "-monitor dev    redirect the monitor to char device 'dev'\n"
4996 82c643ff bellard
           "-serial dev     redirect the serial port to char device 'dev'\n"
4997 6508fe59 bellard
           "-parallel dev   redirect the parallel port to char device 'dev'\n"
4998 f7cce898 bellard
           "-pidfile file   Write PID to 'file'\n"
4999 cd6f1169 bellard
           "-S              freeze CPU at startup (use 'c' to start execution)\n"
5000 cfc3475a pbrook
           "-s              wait gdb connection to port\n"
5001 cfc3475a pbrook
           "-p port         set gdb connection port [default=%s]\n"
5002 f193c797 bellard
           "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
5003 46d4767d bellard
           "-hdachs c,h,s[,t]  force hard disk 0 physical geometry and the optional BIOS\n"
5004 46d4767d bellard
           "                translation (t=none or lba) (usually qemu can guess them)\n"
5005 87b47350 bellard
           "-L path         set the directory for the BIOS, VGA BIOS and keymaps\n"
5006 d993e026 bellard
#ifdef USE_KQEMU
5007 6515b203 bellard
           "-kernel-kqemu   enable KQEMU full virtualization (default is user mode only)\n"
5008 d993e026 bellard
           "-no-kqemu       disable KQEMU kernel module usage\n"
5009 d993e026 bellard
#endif
5010 7ba1e619 aliguori
#ifdef CONFIG_KVM
5011 7ba1e619 aliguori
           "-enable-kvm     enable KVM full virtualization support\n"
5012 7ba1e619 aliguori
#endif
5013 bb0c6722 bellard
#ifdef TARGET_I386
5014 6515b203 bellard
           "-no-acpi        disable ACPI\n"
5015 bb0c6722 bellard
#endif
5016 4d3b6f6e balrog
#ifdef CONFIG_CURSES
5017 4d3b6f6e balrog
           "-curses         use a curses/ncurses interface instead of SDL\n"
5018 4d3b6f6e balrog
#endif
5019 d1beab82 bellard
           "-no-reboot      exit instead of rebooting\n"
5020 b2f76161 aurel32
           "-no-shutdown    stop before shutdown\n"
5021 a8080006 aurel32
           "-loadvm [tag|id]  start right away with a saved state (loadvm in monitor)\n"
5022 24236869 bellard
           "-vnc display    start a VNC server on display\n"
5023 71e3ceb8 ths
#ifndef _WIN32
5024 71e3ceb8 ths
           "-daemonize      daemonize QEMU after initializing\n"
5025 71e3ceb8 ths
#endif
5026 9ae02555 ths
           "-option-rom rom load a file, rom, into the option ROM space\n"
5027 66508601 blueswir1
#ifdef TARGET_SPARC
5028 66508601 blueswir1
           "-prom-env variable=value  set OpenBIOS nvram variables\n"
5029 66508601 blueswir1
#endif
5030 f3dcfada ths
           "-clock          force the use of the given methods for timer alarm.\n"
5031 3adda04c aurel32
           "                To see what timers are available use -clock ?\n"
5032 bce61846 bellard
           "-startdate      select initial date of the clock\n"
5033 2e70f6ef pbrook
           "-icount [N|auto]\n"
5034 dd5d6fe9 pbrook
           "                Enable virtual instruction counter with 2^N clock ticks per instruction\n"
5035 0824d6fc bellard
           "\n"
5036 82c643ff bellard
           "During emulation, the following keys are useful:\n"
5037 032a8c9e bellard
           "ctrl-alt-f      toggle full screen\n"
5038 032a8c9e bellard
           "ctrl-alt-n      switch to virtual console 'n'\n"
5039 032a8c9e bellard
           "ctrl-alt        toggle mouse and keyboard grab\n"
5040 82c643ff bellard
           "\n"
5041 82c643ff bellard
           "When using -nographic, press 'ctrl-a h' to get some help.\n"
5042 82c643ff bellard
           ,
5043 0db63474 bellard
           "qemu",
5044 a00bad7e bellard
           DEFAULT_RAM_SIZE,
5045 7c9d8e07 bellard
#ifndef _WIN32
5046 a00bad7e bellard
           DEFAULT_NETWORK_SCRIPT,
5047 b46a8906 ths
           DEFAULT_NETWORK_DOWN_SCRIPT,
5048 7c9d8e07 bellard
#endif
5049 6e44ba7f bellard
           DEFAULT_GDBSTUB_PORT,
5050 bce61846 bellard
           "/tmp/qemu.log");
5051 15f82208 ths
    exit(exitcode);
5052 0824d6fc bellard
}
5053 0824d6fc bellard
5054 cd6f1169 bellard
#define HAS_ARG 0x0001
5055 cd6f1169 bellard
5056 cd6f1169 bellard
enum {
5057 cd6f1169 bellard
    QEMU_OPTION_h,
5058 cd6f1169 bellard
5059 cc1daa40 bellard
    QEMU_OPTION_M,
5060 94fc95cd j_mayer
    QEMU_OPTION_cpu,
5061 cd6f1169 bellard
    QEMU_OPTION_fda,
5062 cd6f1169 bellard
    QEMU_OPTION_fdb,
5063 cd6f1169 bellard
    QEMU_OPTION_hda,
5064 cd6f1169 bellard
    QEMU_OPTION_hdb,
5065 cd6f1169 bellard
    QEMU_OPTION_hdc,
5066 cd6f1169 bellard
    QEMU_OPTION_hdd,
5067 e4bcb14c ths
    QEMU_OPTION_drive,
5068 cd6f1169 bellard
    QEMU_OPTION_cdrom,
5069 3e3d5815 balrog
    QEMU_OPTION_mtdblock,
5070 a1bb27b1 pbrook
    QEMU_OPTION_sd,
5071 86f55663 j_mayer
    QEMU_OPTION_pflash,
5072 cd6f1169 bellard
    QEMU_OPTION_boot,
5073 cd6f1169 bellard
    QEMU_OPTION_snapshot,
5074 52ca8d6a bellard
#ifdef TARGET_I386
5075 52ca8d6a bellard
    QEMU_OPTION_no_fd_bootchk,
5076 52ca8d6a bellard
#endif
5077 cd6f1169 bellard
    QEMU_OPTION_m,
5078 cd6f1169 bellard
    QEMU_OPTION_nographic,
5079 a171fe39 balrog
    QEMU_OPTION_portrait,
5080 1d14ffa9 bellard
#ifdef HAS_AUDIO
5081 1d14ffa9 bellard
    QEMU_OPTION_audio_help,
5082 1d14ffa9 bellard
    QEMU_OPTION_soundhw,
5083 1d14ffa9 bellard
#endif
5084 cd6f1169 bellard
5085 7c9d8e07 bellard
    QEMU_OPTION_net,
5086 c7f74643 bellard
    QEMU_OPTION_tftp,
5087 47d5d01a ths
    QEMU_OPTION_bootp,
5088 9d728e8c bellard
    QEMU_OPTION_smb,
5089 9bf05444 bellard
    QEMU_OPTION_redir,
5090 dc72ac14 balrog
    QEMU_OPTION_bt,
5091 cd6f1169 bellard
5092 cd6f1169 bellard
    QEMU_OPTION_kernel,
5093 cd6f1169 bellard
    QEMU_OPTION_append,
5094 cd6f1169 bellard
    QEMU_OPTION_initrd,
5095 cd6f1169 bellard
5096 cd6f1169 bellard
    QEMU_OPTION_S,
5097 cd6f1169 bellard
    QEMU_OPTION_s,
5098 cd6f1169 bellard
    QEMU_OPTION_p,
5099 cd6f1169 bellard
    QEMU_OPTION_d,
5100 cd6f1169 bellard
    QEMU_OPTION_hdachs,
5101 cd6f1169 bellard
    QEMU_OPTION_L,
5102 1192dad8 j_mayer
    QEMU_OPTION_bios,
5103 3d11d0eb bellard
    QEMU_OPTION_k,
5104 ee22c2f7 bellard
    QEMU_OPTION_localtime,
5105 e9b137c2 bellard
    QEMU_OPTION_g,
5106 3893c124 malc
    QEMU_OPTION_vga,
5107 20d8a3ed ths
    QEMU_OPTION_echr,
5108 82c643ff bellard
    QEMU_OPTION_monitor,
5109 82c643ff bellard
    QEMU_OPTION_serial,
5110 6508fe59 bellard
    QEMU_OPTION_parallel,
5111 d63d307f bellard
    QEMU_OPTION_loadvm,
5112 d63d307f bellard
    QEMU_OPTION_full_screen,
5113 43523e93 ths
    QEMU_OPTION_no_frame,
5114 3780e197 ths
    QEMU_OPTION_alt_grab,
5115 667accab ths
    QEMU_OPTION_no_quit,
5116 f7cce898 bellard
    QEMU_OPTION_pidfile,
5117 d993e026 bellard
    QEMU_OPTION_no_kqemu,
5118 89bfc105 bellard
    QEMU_OPTION_kernel_kqemu,
5119 7ba1e619 aliguori
    QEMU_OPTION_enable_kvm,
5120 a09db21f bellard
    QEMU_OPTION_win2k_hack,
5121 bb36d470 bellard
    QEMU_OPTION_usb,
5122 a594cfbf bellard
    QEMU_OPTION_usbdevice,
5123 6a00d601 bellard
    QEMU_OPTION_smp,
5124 24236869 bellard
    QEMU_OPTION_vnc,
5125 6515b203 bellard
    QEMU_OPTION_no_acpi,
5126 4d3b6f6e balrog
    QEMU_OPTION_curses,
5127 d1beab82 bellard
    QEMU_OPTION_no_reboot,
5128 b2f76161 aurel32
    QEMU_OPTION_no_shutdown,
5129 9467cd46 balrog
    QEMU_OPTION_show_cursor,
5130 71e3ceb8 ths
    QEMU_OPTION_daemonize,
5131 9ae02555 ths
    QEMU_OPTION_option_rom,
5132 c35734b2 ths
    QEMU_OPTION_semihosting,
5133 c35734b2 ths
    QEMU_OPTION_name,
5134 66508601 blueswir1
    QEMU_OPTION_prom_env,
5135 2b8f2d41 balrog
    QEMU_OPTION_old_param,
5136 f3dcfada ths
    QEMU_OPTION_clock,
5137 7e0af5d0 bellard
    QEMU_OPTION_startdate,
5138 26a5f13b bellard
    QEMU_OPTION_tb_size,
5139 2e70f6ef pbrook
    QEMU_OPTION_icount,
5140 8fcb1b90 blueswir1
    QEMU_OPTION_uuid,
5141 5bb7910a aliguori
    QEMU_OPTION_incoming,
5142 cd6f1169 bellard
};
5143 cd6f1169 bellard
5144 cd6f1169 bellard
typedef struct QEMUOption {
5145 cd6f1169 bellard
    const char *name;
5146 cd6f1169 bellard
    int flags;
5147 cd6f1169 bellard
    int index;
5148 cd6f1169 bellard
} QEMUOption;
5149 cd6f1169 bellard
5150 dbed7e40 blueswir1
static const QEMUOption qemu_options[] = {
5151 cd6f1169 bellard
    { "h", 0, QEMU_OPTION_h },
5152 64423fb2 pbrook
    { "help", 0, QEMU_OPTION_h },
5153 cd6f1169 bellard
5154 cc1daa40 bellard
    { "M", HAS_ARG, QEMU_OPTION_M },
5155 94fc95cd j_mayer
    { "cpu", HAS_ARG, QEMU_OPTION_cpu },
5156 cd6f1169 bellard
    { "fda", HAS_ARG, QEMU_OPTION_fda },
5157 cd6f1169 bellard
    { "fdb", HAS_ARG, QEMU_OPTION_fdb },
5158 cd6f1169 bellard
    { "hda", HAS_ARG, QEMU_OPTION_hda },
5159 cd6f1169 bellard
    { "hdb", HAS_ARG, QEMU_OPTION_hdb },
5160 cd6f1169 bellard
    { "hdc", HAS_ARG, QEMU_OPTION_hdc },
5161 cd6f1169 bellard
    { "hdd", HAS_ARG, QEMU_OPTION_hdd },
5162 e4bcb14c ths
    { "drive", HAS_ARG, QEMU_OPTION_drive },
5163 cd6f1169 bellard
    { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
5164 3e3d5815 balrog
    { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
5165 a1bb27b1 pbrook
    { "sd", HAS_ARG, QEMU_OPTION_sd },
5166 86f55663 j_mayer
    { "pflash", HAS_ARG, QEMU_OPTION_pflash },
5167 cd6f1169 bellard
    { "boot", HAS_ARG, QEMU_OPTION_boot },
5168 cd6f1169 bellard
    { "snapshot", 0, QEMU_OPTION_snapshot },
5169 52ca8d6a bellard
#ifdef TARGET_I386
5170 52ca8d6a bellard
    { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
5171 52ca8d6a bellard
#endif
5172 cd6f1169 bellard
    { "m", HAS_ARG, QEMU_OPTION_m },
5173 cd6f1169 bellard
    { "nographic", 0, QEMU_OPTION_nographic },
5174 a171fe39 balrog
    { "portrait", 0, QEMU_OPTION_portrait },
5175 3d11d0eb bellard
    { "k", HAS_ARG, QEMU_OPTION_k },
5176 1d14ffa9 bellard
#ifdef HAS_AUDIO
5177 1d14ffa9 bellard
    { "audio-help", 0, QEMU_OPTION_audio_help },
5178 1d14ffa9 bellard
    { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
5179 1d14ffa9 bellard
#endif
5180 cd6f1169 bellard
5181 7c9d8e07 bellard
    { "net", HAS_ARG, QEMU_OPTION_net},
5182 158156d1 bellard
#ifdef CONFIG_SLIRP
5183 c7f74643 bellard
    { "tftp", HAS_ARG, QEMU_OPTION_tftp },
5184 47d5d01a ths
    { "bootp", HAS_ARG, QEMU_OPTION_bootp },
5185 c94c8d64 bellard
#ifndef _WIN32
5186 9d728e8c bellard
    { "smb", HAS_ARG, QEMU_OPTION_smb },
5187 c94c8d64 bellard
#endif
5188 9bf05444 bellard
    { "redir", HAS_ARG, QEMU_OPTION_redir },
5189 158156d1 bellard
#endif
5190 dc72ac14 balrog
    { "bt", HAS_ARG, QEMU_OPTION_bt },
5191 cd6f1169 bellard
5192 cd6f1169 bellard
    { "kernel", HAS_ARG, QEMU_OPTION_kernel },
5193 cd6f1169 bellard
    { "append", HAS_ARG, QEMU_OPTION_append },
5194 cd6f1169 bellard
    { "initrd", HAS_ARG, QEMU_OPTION_initrd },
5195 cd6f1169 bellard
5196 cd6f1169 bellard
    { "S", 0, QEMU_OPTION_S },
5197 cd6f1169 bellard
    { "s", 0, QEMU_OPTION_s },
5198 cd6f1169 bellard
    { "p", HAS_ARG, QEMU_OPTION_p },
5199 cd6f1169 bellard
    { "d", HAS_ARG, QEMU_OPTION_d },
5200 cd6f1169 bellard
    { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
5201 cd6f1169 bellard
    { "L", HAS_ARG, QEMU_OPTION_L },
5202 1192dad8 j_mayer
    { "bios", HAS_ARG, QEMU_OPTION_bios },
5203 d993e026 bellard
#ifdef USE_KQEMU
5204 d993e026 bellard
    { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
5205 89bfc105 bellard
    { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
5206 d993e026 bellard
#endif
5207 7ba1e619 aliguori
#ifdef CONFIG_KVM
5208 7ba1e619 aliguori
    { "enable-kvm", 0, QEMU_OPTION_enable_kvm },
5209 7ba1e619 aliguori
#endif
5210 6f7e9aec bellard
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
5211 e9b137c2 bellard
    { "g", 1, QEMU_OPTION_g },
5212 77d4bc34 bellard
#endif
5213 ee22c2f7 bellard
    { "localtime", 0, QEMU_OPTION_localtime },
5214 3893c124 malc
    { "vga", HAS_ARG, QEMU_OPTION_vga },
5215 8b6e0729 balrog
    { "echr", HAS_ARG, QEMU_OPTION_echr },
5216 8b6e0729 balrog
    { "monitor", HAS_ARG, QEMU_OPTION_monitor },
5217 8b6e0729 balrog
    { "serial", HAS_ARG, QEMU_OPTION_serial },
5218 8b6e0729 balrog
    { "parallel", HAS_ARG, QEMU_OPTION_parallel },
5219 d63d307f bellard
    { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
5220 d63d307f bellard
    { "full-screen", 0, QEMU_OPTION_full_screen },
5221 667accab ths
#ifdef CONFIG_SDL
5222 43523e93 ths
    { "no-frame", 0, QEMU_OPTION_no_frame },
5223 3780e197 ths
    { "alt-grab", 0, QEMU_OPTION_alt_grab },
5224 667accab ths
    { "no-quit", 0, QEMU_OPTION_no_quit },
5225 667accab ths
#endif
5226 f7cce898 bellard
    { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
5227 a09db21f bellard
    { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
5228 a594cfbf bellard
    { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
5229 6a00d601 bellard
    { "smp", HAS_ARG, QEMU_OPTION_smp },
5230 24236869 bellard
    { "vnc", HAS_ARG, QEMU_OPTION_vnc },
5231 4d3b6f6e balrog
#ifdef CONFIG_CURSES
5232 4d3b6f6e balrog
    { "curses", 0, QEMU_OPTION_curses },
5233 4d3b6f6e balrog
#endif
5234 8fcb1b90 blueswir1
    { "uuid", HAS_ARG, QEMU_OPTION_uuid },
5235 96d30e48 ths
5236 1f04275e bellard
    /* temporary options */
5237 a594cfbf bellard
    { "usb", 0, QEMU_OPTION_usb },
5238 6515b203 bellard
    { "no-acpi", 0, QEMU_OPTION_no_acpi },
5239 d1beab82 bellard
    { "no-reboot", 0, QEMU_OPTION_no_reboot },
5240 b2f76161 aurel32
    { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
5241 9467cd46 balrog
    { "show-cursor", 0, QEMU_OPTION_show_cursor },
5242 71e3ceb8 ths
    { "daemonize", 0, QEMU_OPTION_daemonize },
5243 9ae02555 ths
    { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
5244 a87295e8 pbrook
#if defined(TARGET_ARM) || defined(TARGET_M68K)
5245 8e71621f pbrook
    { "semihosting", 0, QEMU_OPTION_semihosting },
5246 8e71621f pbrook
#endif
5247 c35734b2 ths
    { "name", HAS_ARG, QEMU_OPTION_name },
5248 66508601 blueswir1
#if defined(TARGET_SPARC)
5249 66508601 blueswir1
    { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
5250 66508601 blueswir1
#endif
5251 2b8f2d41 balrog
#if defined(TARGET_ARM)
5252 2b8f2d41 balrog
    { "old-param", 0, QEMU_OPTION_old_param },
5253 2b8f2d41 balrog
#endif
5254 f3dcfada ths
    { "clock", HAS_ARG, QEMU_OPTION_clock },
5255 7e0af5d0 bellard
    { "startdate", HAS_ARG, QEMU_OPTION_startdate },
5256 26a5f13b bellard
    { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
5257 2e70f6ef pbrook
    { "icount", HAS_ARG, QEMU_OPTION_icount },
5258 5bb7910a aliguori
    { "incoming", HAS_ARG, QEMU_OPTION_incoming },
5259 cd6f1169 bellard
    { NULL },
5260 fc01f7e7 bellard
};
5261 fc01f7e7 bellard
5262 5905b2e5 bellard
/* password input */
5263 5905b2e5 bellard
5264 2bac6019 balrog
int qemu_key_check(BlockDriverState *bs, const char *name)
5265 2bac6019 balrog
{
5266 2bac6019 balrog
    char password[256];
5267 2bac6019 balrog
    int i;
5268 2bac6019 balrog
5269 2bac6019 balrog
    if (!bdrv_is_encrypted(bs))
5270 2bac6019 balrog
        return 0;
5271 2bac6019 balrog
5272 2bac6019 balrog
    term_printf("%s is encrypted.\n", name);
5273 2bac6019 balrog
    for(i = 0; i < 3; i++) {
5274 2bac6019 balrog
        monitor_readline("Password: ", 1, password, sizeof(password));
5275 2bac6019 balrog
        if (bdrv_set_key(bs, password) == 0)
5276 2bac6019 balrog
            return 0;
5277 2bac6019 balrog
        term_printf("invalid password\n");
5278 2bac6019 balrog
    }
5279 2bac6019 balrog
    return -EPERM;
5280 2bac6019 balrog
}
5281 2bac6019 balrog
5282 83ab7950 aliguori
static BlockDriverState *get_bdrv(int index)
5283 83ab7950 aliguori
{
5284 83ab7950 aliguori
    if (index > nb_drives)
5285 83ab7950 aliguori
        return NULL;
5286 83ab7950 aliguori
    return drives_table[index].bdrv;
5287 83ab7950 aliguori
}
5288 83ab7950 aliguori
5289 5905b2e5 bellard
static void read_passwords(void)
5290 5905b2e5 bellard
{
5291 5905b2e5 bellard
    BlockDriverState *bs;
5292 2bac6019 balrog
    int i;
5293 5905b2e5 bellard
5294 83ab7950 aliguori
    for(i = 0; i < 6; i++) {
5295 83ab7950 aliguori
        bs = get_bdrv(i);
5296 83ab7950 aliguori
        if (bs)
5297 83ab7950 aliguori
            qemu_key_check(bs, bdrv_get_device_name(bs));
5298 5905b2e5 bellard
    }
5299 5905b2e5 bellard
}
5300 5905b2e5 bellard
5301 1d14ffa9 bellard
#ifdef HAS_AUDIO
5302 6a36d84e bellard
struct soundhw soundhw[] = {
5303 b00052e4 balrog
#ifdef HAS_AUDIO_CHOICE
5304 4ce7ff6e aurel32
#if defined(TARGET_I386) || defined(TARGET_MIPS)
5305 fd06c375 bellard
    {
5306 fd06c375 bellard
        "pcspk",
5307 fd06c375 bellard
        "PC speaker",
5308 fd06c375 bellard
        0,
5309 fd06c375 bellard
        1,
5310 fd06c375 bellard
        { .init_isa = pcspk_audio_init }
5311 fd06c375 bellard
    },
5312 fd06c375 bellard
#endif
5313 6a36d84e bellard
    {
5314 6a36d84e bellard
        "sb16",
5315 6a36d84e bellard
        "Creative Sound Blaster 16",
5316 6a36d84e bellard
        0,
5317 6a36d84e bellard
        1,
5318 6a36d84e bellard
        { .init_isa = SB16_init }
5319 6a36d84e bellard
    },
5320 6a36d84e bellard
5321 cc53d26d malc
#ifdef CONFIG_CS4231A
5322 cc53d26d malc
    {
5323 cc53d26d malc
        "cs4231a",
5324 cc53d26d malc
        "CS4231A",
5325 cc53d26d malc
        0,
5326 cc53d26d malc
        1,
5327 cc53d26d malc
        { .init_isa = cs4231a_init }
5328 cc53d26d malc
    },
5329 cc53d26d malc
#endif
5330 cc53d26d malc
5331 1d14ffa9 bellard
#ifdef CONFIG_ADLIB
5332 6a36d84e bellard
    {
5333 6a36d84e bellard
        "adlib",
5334 1d14ffa9 bellard
#ifdef HAS_YMF262
5335 6a36d84e bellard
        "Yamaha YMF262 (OPL3)",
5336 1d14ffa9 bellard
#else
5337 6a36d84e bellard
        "Yamaha YM3812 (OPL2)",
5338 1d14ffa9 bellard
#endif
5339 6a36d84e bellard
        0,
5340 6a36d84e bellard
        1,
5341 6a36d84e bellard
        { .init_isa = Adlib_init }
5342 6a36d84e bellard
    },
5343 1d14ffa9 bellard
#endif
5344 6a36d84e bellard
5345 1d14ffa9 bellard
#ifdef CONFIG_GUS
5346 6a36d84e bellard
    {
5347 6a36d84e bellard
        "gus",
5348 6a36d84e bellard
        "Gravis Ultrasound GF1",
5349 6a36d84e bellard
        0,
5350 6a36d84e bellard
        1,
5351 6a36d84e bellard
        { .init_isa = GUS_init }
5352 6a36d84e bellard
    },
5353 1d14ffa9 bellard
#endif
5354 6a36d84e bellard
5355 e5c9a13e balrog
#ifdef CONFIG_AC97
5356 e5c9a13e balrog
    {
5357 e5c9a13e balrog
        "ac97",
5358 e5c9a13e balrog
        "Intel 82801AA AC97 Audio",
5359 e5c9a13e balrog
        0,
5360 e5c9a13e balrog
        0,
5361 e5c9a13e balrog
        { .init_pci = ac97_init }
5362 e5c9a13e balrog
    },
5363 e5c9a13e balrog
#endif
5364 e5c9a13e balrog
5365 6a36d84e bellard
    {
5366 6a36d84e bellard
        "es1370",
5367 6a36d84e bellard
        "ENSONIQ AudioPCI ES1370",
5368 6a36d84e bellard
        0,
5369 6a36d84e bellard
        0,
5370 6a36d84e bellard
        { .init_pci = es1370_init }
5371 6a36d84e bellard
    },
5372 b00052e4 balrog
#endif
5373 6a36d84e bellard
5374 6a36d84e bellard
    { NULL, NULL, 0, 0, { NULL } }
5375 6a36d84e bellard
};
5376 6a36d84e bellard
5377 6a36d84e bellard
static void select_soundhw (const char *optarg)
5378 6a36d84e bellard
{
5379 6a36d84e bellard
    struct soundhw *c;
5380 6a36d84e bellard
5381 6a36d84e bellard
    if (*optarg == '?') {
5382 6a36d84e bellard
    show_valid_cards:
5383 6a36d84e bellard
5384 6a36d84e bellard
        printf ("Valid sound card names (comma separated):\n");
5385 6a36d84e bellard
        for (c = soundhw; c->name; ++c) {
5386 6a36d84e bellard
            printf ("%-11s %s\n", c->name, c->descr);
5387 6a36d84e bellard
        }
5388 6a36d84e bellard
        printf ("\n-soundhw all will enable all of the above\n");
5389 1d14ffa9 bellard
        exit (*optarg != '?');
5390 1d14ffa9 bellard
    }
5391 1d14ffa9 bellard
    else {
5392 6a36d84e bellard
        size_t l;
5393 1d14ffa9 bellard
        const char *p;
5394 1d14ffa9 bellard
        char *e;
5395 1d14ffa9 bellard
        int bad_card = 0;
5396 1d14ffa9 bellard
5397 6a36d84e bellard
        if (!strcmp (optarg, "all")) {
5398 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
5399 6a36d84e bellard
                c->enabled = 1;
5400 6a36d84e bellard
            }
5401 6a36d84e bellard
            return;
5402 6a36d84e bellard
        }
5403 1d14ffa9 bellard
5404 6a36d84e bellard
        p = optarg;
5405 1d14ffa9 bellard
        while (*p) {
5406 1d14ffa9 bellard
            e = strchr (p, ',');
5407 1d14ffa9 bellard
            l = !e ? strlen (p) : (size_t) (e - p);
5408 6a36d84e bellard
5409 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
5410 6a36d84e bellard
                if (!strncmp (c->name, p, l)) {
5411 6a36d84e bellard
                    c->enabled = 1;
5412 1d14ffa9 bellard
                    break;
5413 1d14ffa9 bellard
                }
5414 1d14ffa9 bellard
            }
5415 6a36d84e bellard
5416 6a36d84e bellard
            if (!c->name) {
5417 1d14ffa9 bellard
                if (l > 80) {
5418 1d14ffa9 bellard
                    fprintf (stderr,
5419 1d14ffa9 bellard
                             "Unknown sound card name (too big to show)\n");
5420 1d14ffa9 bellard
                }
5421 1d14ffa9 bellard
                else {
5422 1d14ffa9 bellard
                    fprintf (stderr, "Unknown sound card name `%.*s'\n",
5423 1d14ffa9 bellard
                             (int) l, p);
5424 1d14ffa9 bellard
                }
5425 1d14ffa9 bellard
                bad_card = 1;
5426 1d14ffa9 bellard
            }
5427 1d14ffa9 bellard
            p += l + (e != NULL);
5428 1d14ffa9 bellard
        }
5429 1d14ffa9 bellard
5430 1d14ffa9 bellard
        if (bad_card)
5431 1d14ffa9 bellard
            goto show_valid_cards;
5432 1d14ffa9 bellard
    }
5433 1d14ffa9 bellard
}
5434 1d14ffa9 bellard
#endif
5435 1d14ffa9 bellard
5436 3893c124 malc
static void select_vgahw (const char *p)
5437 3893c124 malc
{
5438 3893c124 malc
    const char *opts;
5439 3893c124 malc
5440 3893c124 malc
    if (strstart(p, "std", &opts)) {
5441 3893c124 malc
        cirrus_vga_enabled = 0;
5442 3893c124 malc
        vmsvga_enabled = 0;
5443 3893c124 malc
    } else if (strstart(p, "cirrus", &opts)) {
5444 3893c124 malc
        cirrus_vga_enabled = 1;
5445 3893c124 malc
        vmsvga_enabled = 0;
5446 3893c124 malc
    } else if (strstart(p, "vmware", &opts)) {
5447 3893c124 malc
        cirrus_vga_enabled = 0;
5448 3893c124 malc
        vmsvga_enabled = 1;
5449 3893c124 malc
    } else {
5450 3893c124 malc
    invalid_vga:
5451 3893c124 malc
        fprintf(stderr, "Unknown vga type: %s\n", p);
5452 3893c124 malc
        exit(1);
5453 3893c124 malc
    }
5454 cb5a7aa8 malc
    while (*opts) {
5455 cb5a7aa8 malc
        const char *nextopt;
5456 cb5a7aa8 malc
5457 cb5a7aa8 malc
        if (strstart(opts, ",retrace=", &nextopt)) {
5458 cb5a7aa8 malc
            opts = nextopt;
5459 cb5a7aa8 malc
            if (strstart(opts, "dumb", &nextopt))
5460 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_DUMB;
5461 cb5a7aa8 malc
            else if (strstart(opts, "precise", &nextopt))
5462 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_PRECISE;
5463 cb5a7aa8 malc
            else goto invalid_vga;
5464 cb5a7aa8 malc
        } else goto invalid_vga;
5465 cb5a7aa8 malc
        opts = nextopt;
5466 cb5a7aa8 malc
    }
5467 3893c124 malc
}
5468 3893c124 malc
5469 3587d7e6 bellard
#ifdef _WIN32
5470 3587d7e6 bellard
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
5471 3587d7e6 bellard
{
5472 3587d7e6 bellard
    exit(STATUS_CONTROL_C_EXIT);
5473 3587d7e6 bellard
    return TRUE;
5474 3587d7e6 bellard
}
5475 3587d7e6 bellard
#endif
5476 3587d7e6 bellard
5477 8fcb1b90 blueswir1
static int qemu_uuid_parse(const char *str, uint8_t *uuid)
5478 8fcb1b90 blueswir1
{
5479 8fcb1b90 blueswir1
    int ret;
5480 8fcb1b90 blueswir1
5481 8fcb1b90 blueswir1
    if(strlen(str) != 36)
5482 8fcb1b90 blueswir1
        return -1;
5483 8fcb1b90 blueswir1
5484 8fcb1b90 blueswir1
    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
5485 8fcb1b90 blueswir1
            &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
5486 8fcb1b90 blueswir1
            &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
5487 8fcb1b90 blueswir1
5488 8fcb1b90 blueswir1
    if(ret != 16)
5489 8fcb1b90 blueswir1
        return -1;
5490 8fcb1b90 blueswir1
5491 8fcb1b90 blueswir1
    return 0;
5492 8fcb1b90 blueswir1
}
5493 8fcb1b90 blueswir1
5494 7c9d8e07 bellard
#define MAX_NET_CLIENTS 32
5495 c20709aa bellard
5496 5b08fc10 aliguori
#ifndef _WIN32
5497 5b08fc10 aliguori
5498 5b08fc10 aliguori
static void termsig_handler(int signal)
5499 5b08fc10 aliguori
{
5500 5b08fc10 aliguori
    qemu_system_shutdown_request();
5501 5b08fc10 aliguori
}
5502 5b08fc10 aliguori
5503 6f9e3801 blueswir1
static void termsig_setup(void)
5504 5b08fc10 aliguori
{
5505 5b08fc10 aliguori
    struct sigaction act;
5506 5b08fc10 aliguori
5507 5b08fc10 aliguori
    memset(&act, 0, sizeof(act));
5508 5b08fc10 aliguori
    act.sa_handler = termsig_handler;
5509 5b08fc10 aliguori
    sigaction(SIGINT,  &act, NULL);
5510 5b08fc10 aliguori
    sigaction(SIGHUP,  &act, NULL);
5511 5b08fc10 aliguori
    sigaction(SIGTERM, &act, NULL);
5512 5b08fc10 aliguori
}
5513 5b08fc10 aliguori
5514 5b08fc10 aliguori
#endif
5515 5b08fc10 aliguori
5516 0824d6fc bellard
int main(int argc, char **argv)
5517 0824d6fc bellard
{
5518 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5519 cfc3475a pbrook
    int use_gdbstub;
5520 cfc3475a pbrook
    const char *gdbstub_port;
5521 67b915a5 bellard
#endif
5522 28c5af54 j_mayer
    uint32_t boot_devices_bitmap = 0;
5523 e4bcb14c ths
    int i;
5524 28c5af54 j_mayer
    int snapshot, linux_boot, net_boot;
5525 7f7f9873 bellard
    const char *initrd_filename;
5526 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
5527 28c5af54 j_mayer
    const char *boot_devices = "";
5528 313aa567 bellard
    DisplayState *ds = &display_state;
5529 46d4767d bellard
    int cyls, heads, secs, translation;
5530 fd5f393a pbrook
    const char *net_clients[MAX_NET_CLIENTS];
5531 7c9d8e07 bellard
    int nb_net_clients;
5532 dc72ac14 balrog
    const char *bt_opts[MAX_BT_CMDLINE];
5533 dc72ac14 balrog
    int nb_bt_opts;
5534 e4bcb14c ths
    int hda_index;
5535 cd6f1169 bellard
    int optind;
5536 cd6f1169 bellard
    const char *r, *optarg;
5537 82c643ff bellard
    CharDriverState *monitor_hd;
5538 fd5f393a pbrook
    const char *monitor_device;
5539 fd5f393a pbrook
    const char *serial_devices[MAX_SERIAL_PORTS];
5540 8d11df9e bellard
    int serial_device_index;
5541 fd5f393a pbrook
    const char *parallel_devices[MAX_PARALLEL_PORTS];
5542 6508fe59 bellard
    int parallel_device_index;
5543 d63d307f bellard
    const char *loadvm = NULL;
5544 cc1daa40 bellard
    QEMUMachine *machine;
5545 94fc95cd j_mayer
    const char *cpu_model;
5546 fd5f393a pbrook
    const char *usb_devices[MAX_USB_CMDLINE];
5547 a594cfbf bellard
    int usb_devices_index;
5548 71e3ceb8 ths
    int fds[2];
5549 26a5f13b bellard
    int tb_size;
5550 93815bc2 ths
    const char *pid_file = NULL;
5551 41bd639b blueswir1
    int autostart;
5552 5bb7910a aliguori
    const char *incoming = NULL;
5553 0bd48850 bellard
5554 0bd48850 bellard
    LIST_INIT (&vm_change_state_head);
5555 be995c27 bellard
#ifndef _WIN32
5556 be995c27 bellard
    {
5557 be995c27 bellard
        struct sigaction act;
5558 be995c27 bellard
        sigfillset(&act.sa_mask);
5559 be995c27 bellard
        act.sa_flags = 0;
5560 be995c27 bellard
        act.sa_handler = SIG_IGN;
5561 be995c27 bellard
        sigaction(SIGPIPE, &act, NULL);
5562 be995c27 bellard
    }
5563 3587d7e6 bellard
#else
5564 3587d7e6 bellard
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
5565 a8e5ac33 bellard
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
5566 a8e5ac33 bellard
       QEMU to run on a single CPU */
5567 a8e5ac33 bellard
    {
5568 a8e5ac33 bellard
        HANDLE h;
5569 a8e5ac33 bellard
        DWORD mask, smask;
5570 a8e5ac33 bellard
        int i;
5571 a8e5ac33 bellard
        h = GetCurrentProcess();
5572 a8e5ac33 bellard
        if (GetProcessAffinityMask(h, &mask, &smask)) {
5573 a8e5ac33 bellard
            for(i = 0; i < 32; i++) {
5574 a8e5ac33 bellard
                if (mask & (1 << i))
5575 a8e5ac33 bellard
                    break;
5576 a8e5ac33 bellard
            }
5577 a8e5ac33 bellard
            if (i != 32) {
5578 a8e5ac33 bellard
                mask = 1 << i;
5579 a8e5ac33 bellard
                SetProcessAffinityMask(h, mask);
5580 a8e5ac33 bellard
            }
5581 a8e5ac33 bellard
        }
5582 a8e5ac33 bellard
    }
5583 67b915a5 bellard
#endif
5584 be995c27 bellard
5585 cc1daa40 bellard
    register_machines();
5586 cc1daa40 bellard
    machine = first_machine;
5587 94fc95cd j_mayer
    cpu_model = NULL;
5588 fc01f7e7 bellard
    initrd_filename = NULL;
5589 4fc5d071 aurel32
    ram_size = 0;
5590 313aa567 bellard
    vga_ram_size = VGA_RAM_SIZE;
5591 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5592 b4608c04 bellard
    use_gdbstub = 0;
5593 c636bb66 bellard
    gdbstub_port = DEFAULT_GDBSTUB_PORT;
5594 67b915a5 bellard
#endif
5595 33e3963e bellard
    snapshot = 0;
5596 a20dd508 bellard
    nographic = 0;
5597 4d3b6f6e balrog
    curses = 0;
5598 a20dd508 bellard
    kernel_filename = NULL;
5599 a20dd508 bellard
    kernel_cmdline = "";
5600 c4b1fcc0 bellard
    cyls = heads = secs = 0;
5601 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
5602 c60e08d9 pbrook
    monitor_device = "vc";
5603 c4b1fcc0 bellard
5604 c75a823c aurel32
    serial_devices[0] = "vc:80Cx24C";
5605 8d11df9e bellard
    for(i = 1; i < MAX_SERIAL_PORTS; i++)
5606 fd5f393a pbrook
        serial_devices[i] = NULL;
5607 8d11df9e bellard
    serial_device_index = 0;
5608 3b46e624 ths
5609 c75a823c aurel32
    parallel_devices[0] = "vc:640x480";
5610 6508fe59 bellard
    for(i = 1; i < MAX_PARALLEL_PORTS; i++)
5611 fd5f393a pbrook
        parallel_devices[i] = NULL;
5612 6508fe59 bellard
    parallel_device_index = 0;
5613 3b46e624 ths
5614 a594cfbf bellard
    usb_devices_index = 0;
5615 3b46e624 ths
5616 7c9d8e07 bellard
    nb_net_clients = 0;
5617 dc72ac14 balrog
    nb_bt_opts = 0;
5618 e4bcb14c ths
    nb_drives = 0;
5619 e4bcb14c ths
    nb_drives_opt = 0;
5620 e4bcb14c ths
    hda_index = -1;
5621 7c9d8e07 bellard
5622 7c9d8e07 bellard
    nb_nics = 0;
5623 3b46e624 ths
5624 26a5f13b bellard
    tb_size = 0;
5625 41bd639b blueswir1
    autostart= 1;
5626 41bd639b blueswir1
5627 cd6f1169 bellard
    optind = 1;
5628 0824d6fc bellard
    for(;;) {
5629 cd6f1169 bellard
        if (optind >= argc)
5630 0824d6fc bellard
            break;
5631 cd6f1169 bellard
        r = argv[optind];
5632 cd6f1169 bellard
        if (r[0] != '-') {
5633 609497ab balrog
            hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
5634 cd6f1169 bellard
        } else {
5635 cd6f1169 bellard
            const QEMUOption *popt;
5636 cd6f1169 bellard
5637 cd6f1169 bellard
            optind++;
5638 dff5efc8 pbrook
            /* Treat --foo the same as -foo.  */
5639 dff5efc8 pbrook
            if (r[1] == '-')
5640 dff5efc8 pbrook
                r++;
5641 cd6f1169 bellard
            popt = qemu_options;
5642 cd6f1169 bellard
            for(;;) {
5643 cd6f1169 bellard
                if (!popt->name) {
5644 5fafdf24 ths
                    fprintf(stderr, "%s: invalid option -- '%s'\n",
5645 cd6f1169 bellard
                            argv[0], r);
5646 cd6f1169 bellard
                    exit(1);
5647 cd6f1169 bellard
                }
5648 cd6f1169 bellard
                if (!strcmp(popt->name, r + 1))
5649 cd6f1169 bellard
                    break;
5650 cd6f1169 bellard
                popt++;
5651 cd6f1169 bellard
            }
5652 cd6f1169 bellard
            if (popt->flags & HAS_ARG) {
5653 cd6f1169 bellard
                if (optind >= argc) {
5654 cd6f1169 bellard
                    fprintf(stderr, "%s: option '%s' requires an argument\n",
5655 cd6f1169 bellard
                            argv[0], r);
5656 cd6f1169 bellard
                    exit(1);
5657 cd6f1169 bellard
                }
5658 cd6f1169 bellard
                optarg = argv[optind++];
5659 cd6f1169 bellard
            } else {
5660 cd6f1169 bellard
                optarg = NULL;
5661 cd6f1169 bellard
            }
5662 cd6f1169 bellard
5663 cd6f1169 bellard
            switch(popt->index) {
5664 cc1daa40 bellard
            case QEMU_OPTION_M:
5665 cc1daa40 bellard
                machine = find_machine(optarg);
5666 cc1daa40 bellard
                if (!machine) {
5667 cc1daa40 bellard
                    QEMUMachine *m;
5668 cc1daa40 bellard
                    printf("Supported machines are:\n");
5669 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
5670 cc1daa40 bellard
                        printf("%-10s %s%s\n",
5671 5fafdf24 ths
                               m->name, m->desc,
5672 cc1daa40 bellard
                               m == first_machine ? " (default)" : "");
5673 cc1daa40 bellard
                    }
5674 15f82208 ths
                    exit(*optarg != '?');
5675 cc1daa40 bellard
                }
5676 cc1daa40 bellard
                break;
5677 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
5678 94fc95cd j_mayer
                /* hw initialization will check this */
5679 15f82208 ths
                if (*optarg == '?') {
5680 c732abe2 j_mayer
/* XXX: implement xxx_cpu_list for targets that still miss it */
5681 c732abe2 j_mayer
#if defined(cpu_list)
5682 c732abe2 j_mayer
                    cpu_list(stdout, &fprintf);
5683 94fc95cd j_mayer
#endif
5684 15f82208 ths
                    exit(0);
5685 94fc95cd j_mayer
                } else {
5686 94fc95cd j_mayer
                    cpu_model = optarg;
5687 94fc95cd j_mayer
                }
5688 94fc95cd j_mayer
                break;
5689 cd6f1169 bellard
            case QEMU_OPTION_initrd:
5690 fc01f7e7 bellard
                initrd_filename = optarg;
5691 fc01f7e7 bellard
                break;
5692 cd6f1169 bellard
            case QEMU_OPTION_hda:
5693 e4bcb14c ths
                if (cyls == 0)
5694 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS, 0);
5695 e4bcb14c ths
                else
5696 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS
5697 e4bcb14c ths
                             ",cyls=%d,heads=%d,secs=%d%s",
5698 609497ab balrog
                             0, cyls, heads, secs,
5699 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_LBA ?
5700 e4bcb14c ths
                                 ",trans=lba" :
5701 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_NONE ?
5702 e4bcb14c ths
                                 ",trans=none" : "");
5703 e4bcb14c ths
                 break;
5704 cd6f1169 bellard
            case QEMU_OPTION_hdb:
5705 cc1daa40 bellard
            case QEMU_OPTION_hdc:
5706 cc1daa40 bellard
            case QEMU_OPTION_hdd:
5707 609497ab balrog
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
5708 fc01f7e7 bellard
                break;
5709 e4bcb14c ths
            case QEMU_OPTION_drive:
5710 609497ab balrog
                drive_add(NULL, "%s", optarg);
5711 e4bcb14c ths
                break;
5712 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
5713 609497ab balrog
                drive_add(optarg, MTD_ALIAS);
5714 3e3d5815 balrog
                break;
5715 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
5716 609497ab balrog
                drive_add(optarg, SD_ALIAS);
5717 a1bb27b1 pbrook
                break;
5718 86f55663 j_mayer
            case QEMU_OPTION_pflash:
5719 609497ab balrog
                drive_add(optarg, PFLASH_ALIAS);
5720 86f55663 j_mayer
                break;
5721 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
5722 33e3963e bellard
                snapshot = 1;
5723 33e3963e bellard
                break;
5724 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
5725 330d0414 bellard
                {
5726 330d0414 bellard
                    const char *p;
5727 330d0414 bellard
                    p = optarg;
5728 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
5729 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
5730 46d4767d bellard
                        goto chs_fail;
5731 330d0414 bellard
                    if (*p != ',')
5732 330d0414 bellard
                        goto chs_fail;
5733 330d0414 bellard
                    p++;
5734 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
5735 46d4767d bellard
                    if (heads < 1 || heads > 16)
5736 46d4767d bellard
                        goto chs_fail;
5737 330d0414 bellard
                    if (*p != ',')
5738 330d0414 bellard
                        goto chs_fail;
5739 330d0414 bellard
                    p++;
5740 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
5741 46d4767d bellard
                    if (secs < 1 || secs > 63)
5742 46d4767d bellard
                        goto chs_fail;
5743 46d4767d bellard
                    if (*p == ',') {
5744 46d4767d bellard
                        p++;
5745 46d4767d bellard
                        if (!strcmp(p, "none"))
5746 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
5747 46d4767d bellard
                        else if (!strcmp(p, "lba"))
5748 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
5749 46d4767d bellard
                        else if (!strcmp(p, "auto"))
5750 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
5751 46d4767d bellard
                        else
5752 46d4767d bellard
                            goto chs_fail;
5753 46d4767d bellard
                    } else if (*p != '\0') {
5754 c4b1fcc0 bellard
                    chs_fail:
5755 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
5756 46d4767d bellard
                        exit(1);
5757 c4b1fcc0 bellard
                    }
5758 e4bcb14c ths
                    if (hda_index != -1)
5759 609497ab balrog
                        snprintf(drives_opt[hda_index].opt,
5760 609497ab balrog
                                 sizeof(drives_opt[hda_index].opt),
5761 609497ab balrog
                                 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5762 609497ab balrog
                                 0, cyls, heads, secs,
5763 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
5764 e4bcb14c ths
                                         ",trans=lba" :
5765 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
5766 e4bcb14c ths
                                     ",trans=none" : "");
5767 330d0414 bellard
                }
5768 330d0414 bellard
                break;
5769 cd6f1169 bellard
            case QEMU_OPTION_nographic:
5770 a20dd508 bellard
                nographic = 1;
5771 a20dd508 bellard
                break;
5772 4d3b6f6e balrog
#ifdef CONFIG_CURSES
5773 4d3b6f6e balrog
            case QEMU_OPTION_curses:
5774 4d3b6f6e balrog
                curses = 1;
5775 4d3b6f6e balrog
                break;
5776 4d3b6f6e balrog
#endif
5777 a171fe39 balrog
            case QEMU_OPTION_portrait:
5778 a171fe39 balrog
                graphic_rotate = 1;
5779 a171fe39 balrog
                break;
5780 cd6f1169 bellard
            case QEMU_OPTION_kernel:
5781 a20dd508 bellard
                kernel_filename = optarg;
5782 a20dd508 bellard
                break;
5783 cd6f1169 bellard
            case QEMU_OPTION_append:
5784 a20dd508 bellard
                kernel_cmdline = optarg;
5785 313aa567 bellard
                break;
5786 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
5787 609497ab balrog
                drive_add(optarg, CDROM_ALIAS);
5788 36b486bb bellard
                break;
5789 cd6f1169 bellard
            case QEMU_OPTION_boot:
5790 28c5af54 j_mayer
                boot_devices = optarg;
5791 28c5af54 j_mayer
                /* We just do some generic consistency checks */
5792 28c5af54 j_mayer
                {
5793 28c5af54 j_mayer
                    /* Could easily be extended to 64 devices if needed */
5794 60fe76f3 ths
                    const char *p;
5795 28c5af54 j_mayer
                    
5796 28c5af54 j_mayer
                    boot_devices_bitmap = 0;
5797 28c5af54 j_mayer
                    for (p = boot_devices; *p != '\0'; p++) {
5798 28c5af54 j_mayer
                        /* Allowed boot devices are:
5799 28c5af54 j_mayer
                         * a b     : floppy disk drives
5800 28c5af54 j_mayer
                         * c ... f : IDE disk drives
5801 28c5af54 j_mayer
                         * g ... m : machine implementation dependant drives
5802 28c5af54 j_mayer
                         * n ... p : network devices
5803 28c5af54 j_mayer
                         * It's up to each machine implementation to check
5804 28c5af54 j_mayer
                         * if the given boot devices match the actual hardware
5805 28c5af54 j_mayer
                         * implementation and firmware features.
5806 28c5af54 j_mayer
                         */
5807 28c5af54 j_mayer
                        if (*p < 'a' || *p > 'q') {
5808 28c5af54 j_mayer
                            fprintf(stderr, "Invalid boot device '%c'\n", *p);
5809 28c5af54 j_mayer
                            exit(1);
5810 28c5af54 j_mayer
                        }
5811 28c5af54 j_mayer
                        if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5812 28c5af54 j_mayer
                            fprintf(stderr,
5813 28c5af54 j_mayer
                                    "Boot device '%c' was given twice\n",*p);
5814 28c5af54 j_mayer
                            exit(1);
5815 28c5af54 j_mayer
                        }
5816 28c5af54 j_mayer
                        boot_devices_bitmap |= 1 << (*p - 'a');
5817 28c5af54 j_mayer
                    }
5818 36b486bb bellard
                }
5819 36b486bb bellard
                break;
5820 cd6f1169 bellard
            case QEMU_OPTION_fda:
5821 cd6f1169 bellard
            case QEMU_OPTION_fdb:
5822 609497ab balrog
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
5823 c45886db bellard
                break;
5824 52ca8d6a bellard
#ifdef TARGET_I386
5825 52ca8d6a bellard
            case QEMU_OPTION_no_fd_bootchk:
5826 52ca8d6a bellard
                fd_bootchk = 0;
5827 52ca8d6a bellard
                break;
5828 52ca8d6a bellard
#endif
5829 7c9d8e07 bellard
            case QEMU_OPTION_net:
5830 7c9d8e07 bellard
                if (nb_net_clients >= MAX_NET_CLIENTS) {
5831 7c9d8e07 bellard
                    fprintf(stderr, "qemu: too many network clients\n");
5832 c4b1fcc0 bellard
                    exit(1);
5833 c4b1fcc0 bellard
                }
5834 fd5f393a pbrook
                net_clients[nb_net_clients] = optarg;
5835 7c9d8e07 bellard
                nb_net_clients++;
5836 702c651c bellard
                break;
5837 c7f74643 bellard
#ifdef CONFIG_SLIRP
5838 c7f74643 bellard
            case QEMU_OPTION_tftp:
5839 c7f74643 bellard
                tftp_prefix = optarg;
5840 9bf05444 bellard
                break;
5841 47d5d01a ths
            case QEMU_OPTION_bootp:
5842 47d5d01a ths
                bootp_filename = optarg;
5843 47d5d01a ths
                break;
5844 c94c8d64 bellard
#ifndef _WIN32
5845 9d728e8c bellard
            case QEMU_OPTION_smb:
5846 9d728e8c bellard
                net_slirp_smb(optarg);
5847 9d728e8c bellard
                break;
5848 c94c8d64 bellard
#endif
5849 9bf05444 bellard
            case QEMU_OPTION_redir:
5850 3b46e624 ths
                net_slirp_redir(optarg);
5851 9bf05444 bellard
                break;
5852 c7f74643 bellard
#endif
5853 dc72ac14 balrog
            case QEMU_OPTION_bt:
5854 dc72ac14 balrog
                if (nb_bt_opts >= MAX_BT_CMDLINE) {
5855 dc72ac14 balrog
                    fprintf(stderr, "qemu: too many bluetooth options\n");
5856 dc72ac14 balrog
                    exit(1);
5857 dc72ac14 balrog
                }
5858 dc72ac14 balrog
                bt_opts[nb_bt_opts++] = optarg;
5859 dc72ac14 balrog
                break;
5860 1d14ffa9 bellard
#ifdef HAS_AUDIO
5861 1d14ffa9 bellard
            case QEMU_OPTION_audio_help:
5862 1d14ffa9 bellard
                AUD_help ();
5863 1d14ffa9 bellard
                exit (0);
5864 1d14ffa9 bellard
                break;
5865 1d14ffa9 bellard
            case QEMU_OPTION_soundhw:
5866 1d14ffa9 bellard
                select_soundhw (optarg);
5867 1d14ffa9 bellard
                break;
5868 1d14ffa9 bellard
#endif
5869 cd6f1169 bellard
            case QEMU_OPTION_h:
5870 15f82208 ths
                help(0);
5871 cd6f1169 bellard
                break;
5872 00f82b8a aurel32
            case QEMU_OPTION_m: {
5873 00f82b8a aurel32
                uint64_t value;
5874 00f82b8a aurel32
                char *ptr;
5875 00f82b8a aurel32
5876 00f82b8a aurel32
                value = strtoul(optarg, &ptr, 10);
5877 00f82b8a aurel32
                switch (*ptr) {
5878 00f82b8a aurel32
                case 0: case 'M': case 'm':
5879 00f82b8a aurel32
                    value <<= 20;
5880 00f82b8a aurel32
                    break;
5881 00f82b8a aurel32
                case 'G': case 'g':
5882 00f82b8a aurel32
                    value <<= 30;
5883 00f82b8a aurel32
                    break;
5884 00f82b8a aurel32
                default:
5885 00f82b8a aurel32
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
5886 cd6f1169 bellard
                    exit(1);
5887 cd6f1169 bellard
                }
5888 00f82b8a aurel32
5889 00f82b8a aurel32
                /* On 32-bit hosts, QEMU is limited by virtual address space */
5890 00f82b8a aurel32
                if (value > (2047 << 20)
5891 00f82b8a aurel32
#ifndef USE_KQEMU
5892 00f82b8a aurel32
                    && HOST_LONG_BITS == 32
5893 00f82b8a aurel32
#endif
5894 00f82b8a aurel32
                    ) {
5895 00f82b8a aurel32
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5896 00f82b8a aurel32
                    exit(1);
5897 00f82b8a aurel32
                }
5898 00f82b8a aurel32
                if (value != (uint64_t)(ram_addr_t)value) {
5899 00f82b8a aurel32
                    fprintf(stderr, "qemu: ram size too large\n");
5900 00f82b8a aurel32
                    exit(1);
5901 00f82b8a aurel32
                }
5902 00f82b8a aurel32
                ram_size = value;
5903 cd6f1169 bellard
                break;
5904 00f82b8a aurel32
            }
5905 cd6f1169 bellard
            case QEMU_OPTION_d:
5906 cd6f1169 bellard
                {
5907 cd6f1169 bellard
                    int mask;
5908 c7cd6a37 blueswir1
                    const CPULogItem *item;
5909 3b46e624 ths
5910 cd6f1169 bellard
                    mask = cpu_str_to_log_mask(optarg);
5911 cd6f1169 bellard
                    if (!mask) {
5912 cd6f1169 bellard
                        printf("Log items (comma separated):\n");
5913 f193c797 bellard
                    for(item = cpu_log_items; item->mask != 0; item++) {
5914 f193c797 bellard
                        printf("%-10s %s\n", item->name, item->help);
5915 f193c797 bellard
                    }
5916 f193c797 bellard
                    exit(1);
5917 cd6f1169 bellard
                    }
5918 cd6f1169 bellard
                    cpu_set_log(mask);
5919 f193c797 bellard
                }
5920 cd6f1169 bellard
                break;
5921 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5922 cd6f1169 bellard
            case QEMU_OPTION_s:
5923 cd6f1169 bellard
                use_gdbstub = 1;
5924 cd6f1169 bellard
                break;
5925 cd6f1169 bellard
            case QEMU_OPTION_p:
5926 cfc3475a pbrook
                gdbstub_port = optarg;
5927 cd6f1169 bellard
                break;
5928 67b915a5 bellard
#endif
5929 cd6f1169 bellard
            case QEMU_OPTION_L:
5930 cd6f1169 bellard
                bios_dir = optarg;
5931 cd6f1169 bellard
                break;
5932 1192dad8 j_mayer
            case QEMU_OPTION_bios:
5933 1192dad8 j_mayer
                bios_name = optarg;
5934 1192dad8 j_mayer
                break;
5935 cd6f1169 bellard
            case QEMU_OPTION_S:
5936 3c07f8e8 pbrook
                autostart = 0;
5937 cd6f1169 bellard
                break;
5938 3d11d0eb bellard
            case QEMU_OPTION_k:
5939 3d11d0eb bellard
                keyboard_layout = optarg;
5940 3d11d0eb bellard
                break;
5941 ee22c2f7 bellard
            case QEMU_OPTION_localtime:
5942 ee22c2f7 bellard
                rtc_utc = 0;
5943 ee22c2f7 bellard
                break;
5944 3893c124 malc
            case QEMU_OPTION_vga:
5945 3893c124 malc
                select_vgahw (optarg);
5946 1bfe856e bellard
                break;
5947 e9b137c2 bellard
            case QEMU_OPTION_g:
5948 e9b137c2 bellard
                {
5949 e9b137c2 bellard
                    const char *p;
5950 e9b137c2 bellard
                    int w, h, depth;
5951 e9b137c2 bellard
                    p = optarg;
5952 e9b137c2 bellard
                    w = strtol(p, (char **)&p, 10);
5953 e9b137c2 bellard
                    if (w <= 0) {
5954 e9b137c2 bellard
                    graphic_error:
5955 e9b137c2 bellard
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
5956 e9b137c2 bellard
                        exit(1);
5957 e9b137c2 bellard
                    }
5958 e9b137c2 bellard
                    if (*p != 'x')
5959 e9b137c2 bellard
                        goto graphic_error;
5960 e9b137c2 bellard
                    p++;
5961 e9b137c2 bellard
                    h = strtol(p, (char **)&p, 10);
5962 e9b137c2 bellard
                    if (h <= 0)
5963 e9b137c2 bellard
                        goto graphic_error;
5964 e9b137c2 bellard
                    if (*p == 'x') {
5965 e9b137c2 bellard
                        p++;
5966 e9b137c2 bellard
                        depth = strtol(p, (char **)&p, 10);
5967 5fafdf24 ths
                        if (depth != 8 && depth != 15 && depth != 16 &&
5968 e9b137c2 bellard
                            depth != 24 && depth != 32)
5969 e9b137c2 bellard
                            goto graphic_error;
5970 e9b137c2 bellard
                    } else if (*p == '\0') {
5971 e9b137c2 bellard
                        depth = graphic_depth;
5972 e9b137c2 bellard
                    } else {
5973 e9b137c2 bellard
                        goto graphic_error;
5974 e9b137c2 bellard
                    }
5975 3b46e624 ths
5976 e9b137c2 bellard
                    graphic_width = w;
5977 e9b137c2 bellard
                    graphic_height = h;
5978 e9b137c2 bellard
                    graphic_depth = depth;
5979 e9b137c2 bellard
                }
5980 e9b137c2 bellard
                break;
5981 20d8a3ed ths
            case QEMU_OPTION_echr:
5982 20d8a3ed ths
                {
5983 20d8a3ed ths
                    char *r;
5984 20d8a3ed ths
                    term_escape_char = strtol(optarg, &r, 0);
5985 20d8a3ed ths
                    if (r == optarg)
5986 20d8a3ed ths
                        printf("Bad argument to echr\n");
5987 20d8a3ed ths
                    break;
5988 20d8a3ed ths
                }
5989 82c643ff bellard
            case QEMU_OPTION_monitor:
5990 fd5f393a pbrook
                monitor_device = optarg;
5991 82c643ff bellard
                break;
5992 82c643ff bellard
            case QEMU_OPTION_serial:
5993 8d11df9e bellard
                if (serial_device_index >= MAX_SERIAL_PORTS) {
5994 8d11df9e bellard
                    fprintf(stderr, "qemu: too many serial ports\n");
5995 8d11df9e bellard
                    exit(1);
5996 8d11df9e bellard
                }
5997 fd5f393a pbrook
                serial_devices[serial_device_index] = optarg;
5998 8d11df9e bellard
                serial_device_index++;
5999 82c643ff bellard
                break;
6000 6508fe59 bellard
            case QEMU_OPTION_parallel:
6001 6508fe59 bellard
                if (parallel_device_index >= MAX_PARALLEL_PORTS) {
6002 6508fe59 bellard
                    fprintf(stderr, "qemu: too many parallel ports\n");
6003 6508fe59 bellard
                    exit(1);
6004 6508fe59 bellard
                }
6005 fd5f393a pbrook
                parallel_devices[parallel_device_index] = optarg;
6006 6508fe59 bellard
                parallel_device_index++;
6007 6508fe59 bellard
                break;
6008 d63d307f bellard
            case QEMU_OPTION_loadvm:
6009 d63d307f bellard
                loadvm = optarg;
6010 d63d307f bellard
                break;
6011 d63d307f bellard
            case QEMU_OPTION_full_screen:
6012 d63d307f bellard
                full_screen = 1;
6013 d63d307f bellard
                break;
6014 667accab ths
#ifdef CONFIG_SDL
6015 43523e93 ths
            case QEMU_OPTION_no_frame:
6016 43523e93 ths
                no_frame = 1;
6017 43523e93 ths
                break;
6018 3780e197 ths
            case QEMU_OPTION_alt_grab:
6019 3780e197 ths
                alt_grab = 1;
6020 3780e197 ths
                break;
6021 667accab ths
            case QEMU_OPTION_no_quit:
6022 667accab ths
                no_quit = 1;
6023 667accab ths
                break;
6024 667accab ths
#endif
6025 f7cce898 bellard
            case QEMU_OPTION_pidfile:
6026 93815bc2 ths
                pid_file = optarg;
6027 f7cce898 bellard
                break;
6028 a09db21f bellard
#ifdef TARGET_I386
6029 a09db21f bellard
            case QEMU_OPTION_win2k_hack:
6030 a09db21f bellard
                win2k_install_hack = 1;
6031 a09db21f bellard
                break;
6032 a09db21f bellard
#endif
6033 d993e026 bellard
#ifdef USE_KQEMU
6034 d993e026 bellard
            case QEMU_OPTION_no_kqemu:
6035 d993e026 bellard
                kqemu_allowed = 0;
6036 d993e026 bellard
                break;
6037 89bfc105 bellard
            case QEMU_OPTION_kernel_kqemu:
6038 89bfc105 bellard
                kqemu_allowed = 2;
6039 89bfc105 bellard
                break;
6040 d993e026 bellard
#endif
6041 7ba1e619 aliguori
#ifdef CONFIG_KVM
6042 7ba1e619 aliguori
            case QEMU_OPTION_enable_kvm:
6043 7ba1e619 aliguori
                kvm_allowed = 1;
6044 7ba1e619 aliguori
#ifdef USE_KQEMU
6045 7ba1e619 aliguori
                kqemu_allowed = 0;
6046 7ba1e619 aliguori
#endif
6047 7ba1e619 aliguori
                break;
6048 7ba1e619 aliguori
#endif
6049 bb36d470 bellard
            case QEMU_OPTION_usb:
6050 bb36d470 bellard
                usb_enabled = 1;
6051 bb36d470 bellard
                break;
6052 a594cfbf bellard
            case QEMU_OPTION_usbdevice:
6053 a594cfbf bellard
                usb_enabled = 1;
6054 0d92ed30 pbrook
                if (usb_devices_index >= MAX_USB_CMDLINE) {
6055 a594cfbf bellard
                    fprintf(stderr, "Too many USB devices\n");
6056 a594cfbf bellard
                    exit(1);
6057 a594cfbf bellard
                }
6058 fd5f393a pbrook
                usb_devices[usb_devices_index] = optarg;
6059 a594cfbf bellard
                usb_devices_index++;
6060 a594cfbf bellard
                break;
6061 6a00d601 bellard
            case QEMU_OPTION_smp:
6062 6a00d601 bellard
                smp_cpus = atoi(optarg);
6063 b2097003 aliguori
                if (smp_cpus < 1) {
6064 6a00d601 bellard
                    fprintf(stderr, "Invalid number of CPUs\n");
6065 6a00d601 bellard
                    exit(1);
6066 6a00d601 bellard
                }
6067 6a00d601 bellard
                break;
6068 24236869 bellard
            case QEMU_OPTION_vnc:
6069 73fc9742 ths
                vnc_display = optarg;
6070 24236869 bellard
                break;
6071 6515b203 bellard
            case QEMU_OPTION_no_acpi:
6072 6515b203 bellard
                acpi_enabled = 0;
6073 6515b203 bellard
                break;
6074 d1beab82 bellard
            case QEMU_OPTION_no_reboot:
6075 d1beab82 bellard
                no_reboot = 1;
6076 d1beab82 bellard
                break;
6077 b2f76161 aurel32
            case QEMU_OPTION_no_shutdown:
6078 b2f76161 aurel32
                no_shutdown = 1;
6079 b2f76161 aurel32
                break;
6080 9467cd46 balrog
            case QEMU_OPTION_show_cursor:
6081 9467cd46 balrog
                cursor_hide = 0;
6082 9467cd46 balrog
                break;
6083 8fcb1b90 blueswir1
            case QEMU_OPTION_uuid:
6084 8fcb1b90 blueswir1
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
6085 8fcb1b90 blueswir1
                    fprintf(stderr, "Fail to parse UUID string."
6086 8fcb1b90 blueswir1
                            " Wrong format.\n");
6087 8fcb1b90 blueswir1
                    exit(1);
6088 8fcb1b90 blueswir1
                }
6089 8fcb1b90 blueswir1
                break;
6090 71e3ceb8 ths
            case QEMU_OPTION_daemonize:
6091 71e3ceb8 ths
                daemonize = 1;
6092 71e3ceb8 ths
                break;
6093 9ae02555 ths
            case QEMU_OPTION_option_rom:
6094 9ae02555 ths
                if (nb_option_roms >= MAX_OPTION_ROMS) {
6095 9ae02555 ths
                    fprintf(stderr, "Too many option ROMs\n");
6096 9ae02555 ths
                    exit(1);
6097 9ae02555 ths
                }
6098 9ae02555 ths
                option_rom[nb_option_roms] = optarg;
6099 9ae02555 ths
                nb_option_roms++;
6100 9ae02555 ths
                break;
6101 8e71621f pbrook
            case QEMU_OPTION_semihosting:
6102 8e71621f pbrook
                semihosting_enabled = 1;
6103 8e71621f pbrook
                break;
6104 c35734b2 ths
            case QEMU_OPTION_name:
6105 c35734b2 ths
                qemu_name = optarg;
6106 c35734b2 ths
                break;
6107 66508601 blueswir1
#ifdef TARGET_SPARC
6108 66508601 blueswir1
            case QEMU_OPTION_prom_env:
6109 66508601 blueswir1
                if (nb_prom_envs >= MAX_PROM_ENVS) {
6110 66508601 blueswir1
                    fprintf(stderr, "Too many prom variables\n");
6111 66508601 blueswir1
                    exit(1);
6112 66508601 blueswir1
                }
6113 66508601 blueswir1
                prom_envs[nb_prom_envs] = optarg;
6114 66508601 blueswir1
                nb_prom_envs++;
6115 66508601 blueswir1
                break;
6116 66508601 blueswir1
#endif
6117 2b8f2d41 balrog
#ifdef TARGET_ARM
6118 2b8f2d41 balrog
            case QEMU_OPTION_old_param:
6119 2b8f2d41 balrog
                old_param = 1;
6120 05ebd537 ths
                break;
6121 2b8f2d41 balrog
#endif
6122 f3dcfada ths
            case QEMU_OPTION_clock:
6123 f3dcfada ths
                configure_alarms(optarg);
6124 f3dcfada ths
                break;
6125 7e0af5d0 bellard
            case QEMU_OPTION_startdate:
6126 7e0af5d0 bellard
                {
6127 7e0af5d0 bellard
                    struct tm tm;
6128 f6503059 balrog
                    time_t rtc_start_date;
6129 7e0af5d0 bellard
                    if (!strcmp(optarg, "now")) {
6130 f6503059 balrog
                        rtc_date_offset = -1;
6131 7e0af5d0 bellard
                    } else {
6132 7e0af5d0 bellard
                        if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
6133 7e0af5d0 bellard
                               &tm.tm_year,
6134 7e0af5d0 bellard
                               &tm.tm_mon,
6135 7e0af5d0 bellard
                               &tm.tm_mday,
6136 7e0af5d0 bellard
                               &tm.tm_hour,
6137 7e0af5d0 bellard
                               &tm.tm_min,
6138 7e0af5d0 bellard
                               &tm.tm_sec) == 6) {
6139 7e0af5d0 bellard
                            /* OK */
6140 7e0af5d0 bellard
                        } else if (sscanf(optarg, "%d-%d-%d",
6141 7e0af5d0 bellard
                                          &tm.tm_year,
6142 7e0af5d0 bellard
                                          &tm.tm_mon,
6143 7e0af5d0 bellard
                                          &tm.tm_mday) == 3) {
6144 7e0af5d0 bellard
                            tm.tm_hour = 0;
6145 7e0af5d0 bellard
                            tm.tm_min = 0;
6146 7e0af5d0 bellard
                            tm.tm_sec = 0;
6147 7e0af5d0 bellard
                        } else {
6148 7e0af5d0 bellard
                            goto date_fail;
6149 7e0af5d0 bellard
                        }
6150 7e0af5d0 bellard
                        tm.tm_year -= 1900;
6151 7e0af5d0 bellard
                        tm.tm_mon--;
6152 3c6b2088 bellard
                        rtc_start_date = mktimegm(&tm);
6153 7e0af5d0 bellard
                        if (rtc_start_date == -1) {
6154 7e0af5d0 bellard
                        date_fail:
6155 7e0af5d0 bellard
                            fprintf(stderr, "Invalid date format. Valid format are:\n"
6156 7e0af5d0 bellard
                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
6157 7e0af5d0 bellard
                            exit(1);
6158 7e0af5d0 bellard
                        }
6159 f6503059 balrog
                        rtc_date_offset = time(NULL) - rtc_start_date;
6160 7e0af5d0 bellard
                    }
6161 7e0af5d0 bellard
                }
6162 7e0af5d0 bellard
                break;
6163 26a5f13b bellard
            case QEMU_OPTION_tb_size:
6164 26a5f13b bellard
                tb_size = strtol(optarg, NULL, 0);
6165 26a5f13b bellard
                if (tb_size < 0)
6166 26a5f13b bellard
                    tb_size = 0;
6167 26a5f13b bellard
                break;
6168 2e70f6ef pbrook
            case QEMU_OPTION_icount:
6169 2e70f6ef pbrook
                use_icount = 1;
6170 2e70f6ef pbrook
                if (strcmp(optarg, "auto") == 0) {
6171 2e70f6ef pbrook
                    icount_time_shift = -1;
6172 2e70f6ef pbrook
                } else {
6173 2e70f6ef pbrook
                    icount_time_shift = strtol(optarg, NULL, 0);
6174 2e70f6ef pbrook
                }
6175 2e70f6ef pbrook
                break;
6176 5bb7910a aliguori
            case QEMU_OPTION_incoming:
6177 5bb7910a aliguori
                incoming = optarg;
6178 5bb7910a aliguori
                break;
6179 cd6f1169 bellard
            }
6180 0824d6fc bellard
        }
6181 0824d6fc bellard
    }
6182 330d0414 bellard
6183 7ba1e619 aliguori
#if defined(CONFIG_KVM) && defined(USE_KQEMU)
6184 7ba1e619 aliguori
    if (kvm_allowed && kqemu_allowed) {
6185 7ba1e619 aliguori
        fprintf(stderr,
6186 7ba1e619 aliguori
                "You can not enable both KVM and kqemu at the same time\n");
6187 7ba1e619 aliguori
        exit(1);
6188 7ba1e619 aliguori
    }
6189 7ba1e619 aliguori
#endif
6190 7ba1e619 aliguori
6191 3d878caa balrog
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
6192 b2097003 aliguori
    if (smp_cpus > machine->max_cpus) {
6193 b2097003 aliguori
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
6194 b2097003 aliguori
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
6195 b2097003 aliguori
                machine->max_cpus);
6196 b2097003 aliguori
        exit(1);
6197 b2097003 aliguori
    }
6198 b2097003 aliguori
6199 bc0129d9 aliguori
    if (nographic) {
6200 bc0129d9 aliguori
       if (serial_device_index == 0)
6201 bc0129d9 aliguori
           serial_devices[0] = "stdio";
6202 bc0129d9 aliguori
       if (parallel_device_index == 0)
6203 bc0129d9 aliguori
           parallel_devices[0] = "null";
6204 bc0129d9 aliguori
       if (strncmp(monitor_device, "vc", 2) == 0)
6205 bc0129d9 aliguori
           monitor_device = "stdio";
6206 bc0129d9 aliguori
    }
6207 bc0129d9 aliguori
6208 71e3ceb8 ths
#ifndef _WIN32
6209 71e3ceb8 ths
    if (daemonize) {
6210 71e3ceb8 ths
        pid_t pid;
6211 71e3ceb8 ths
6212 71e3ceb8 ths
        if (pipe(fds) == -1)
6213 71e3ceb8 ths
            exit(1);
6214 71e3ceb8 ths
6215 71e3ceb8 ths
        pid = fork();
6216 71e3ceb8 ths
        if (pid > 0) {
6217 71e3ceb8 ths
            uint8_t status;
6218 71e3ceb8 ths
            ssize_t len;
6219 71e3ceb8 ths
6220 71e3ceb8 ths
            close(fds[1]);
6221 71e3ceb8 ths
6222 71e3ceb8 ths
        again:
6223 93815bc2 ths
            len = read(fds[0], &status, 1);
6224 93815bc2 ths
            if (len == -1 && (errno == EINTR))
6225 93815bc2 ths
                goto again;
6226 93815bc2 ths
6227 93815bc2 ths
            if (len != 1)
6228 93815bc2 ths
                exit(1);
6229 93815bc2 ths
            else if (status == 1) {
6230 93815bc2 ths
                fprintf(stderr, "Could not acquire pidfile\n");
6231 93815bc2 ths
                exit(1);
6232 93815bc2 ths
            } else
6233 93815bc2 ths
                exit(0);
6234 71e3ceb8 ths
        } else if (pid < 0)
6235 93815bc2 ths
            exit(1);
6236 71e3ceb8 ths
6237 71e3ceb8 ths
        setsid();
6238 71e3ceb8 ths
6239 71e3ceb8 ths
        pid = fork();
6240 71e3ceb8 ths
        if (pid > 0)
6241 71e3ceb8 ths
            exit(0);
6242 71e3ceb8 ths
        else if (pid < 0)
6243 71e3ceb8 ths
            exit(1);
6244 71e3ceb8 ths
6245 71e3ceb8 ths
        umask(027);
6246 71e3ceb8 ths
6247 71e3ceb8 ths
        signal(SIGTSTP, SIG_IGN);
6248 71e3ceb8 ths
        signal(SIGTTOU, SIG_IGN);
6249 71e3ceb8 ths
        signal(SIGTTIN, SIG_IGN);
6250 71e3ceb8 ths
    }
6251 71e3ceb8 ths
#endif
6252 71e3ceb8 ths
6253 aa26bb2d ths
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
6254 93815bc2 ths
        if (daemonize) {
6255 93815bc2 ths
            uint8_t status = 1;
6256 93815bc2 ths
            write(fds[1], &status, 1);
6257 93815bc2 ths
        } else
6258 93815bc2 ths
            fprintf(stderr, "Could not acquire pid file\n");
6259 93815bc2 ths
        exit(1);
6260 93815bc2 ths
    }
6261 93815bc2 ths
6262 ff3fbb30 bellard
#ifdef USE_KQEMU
6263 ff3fbb30 bellard
    if (smp_cpus > 1)
6264 ff3fbb30 bellard
        kqemu_allowed = 0;
6265 ff3fbb30 bellard
#endif
6266 a20dd508 bellard
    linux_boot = (kernel_filename != NULL);
6267 7317b8ca balrog
    net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
6268 6c41b272 balrog
6269 28c5af54 j_mayer
    if (!linux_boot && net_boot == 0 &&
6270 f88e4b91 blueswir1
        !machine->nodisk_ok && nb_drives_opt == 0)
6271 15f82208 ths
        help(1);
6272 0824d6fc bellard
6273 f8d39c01 ths
    if (!linux_boot && *kernel_cmdline != '\0') {
6274 f8d39c01 ths
        fprintf(stderr, "-append only allowed with -kernel option\n");
6275 f8d39c01 ths
        exit(1);
6276 f8d39c01 ths
    }
6277 f8d39c01 ths
6278 f8d39c01 ths
    if (!linux_boot && initrd_filename != NULL) {
6279 f8d39c01 ths
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
6280 f8d39c01 ths
        exit(1);
6281 f8d39c01 ths
    }
6282 f8d39c01 ths
6283 96d30e48 ths
    /* boot to floppy or the default cd if no hard disk defined yet */
6284 28c5af54 j_mayer
    if (!boot_devices[0]) {
6285 e4bcb14c ths
        boot_devices = "cad";
6286 96d30e48 ths
    }
6287 b118d61e bellard
    setvbuf(stdout, NULL, _IOLBF, 0);
6288 3b46e624 ths
6289 634fce96 pbrook
    init_timers();
6290 7183b4b4 aliguori
    if (init_timer_alarm() < 0) {
6291 7183b4b4 aliguori
        fprintf(stderr, "could not initialize alarm timer\n");
6292 7183b4b4 aliguori
        exit(1);
6293 7183b4b4 aliguori
    }
6294 2e70f6ef pbrook
    if (use_icount && icount_time_shift < 0) {
6295 2e70f6ef pbrook
        use_icount = 2;
6296 2e70f6ef pbrook
        /* 125MIPS seems a reasonable initial guess at the guest speed.
6297 2e70f6ef pbrook
           It will be corrected fairly quickly anyway.  */
6298 2e70f6ef pbrook
        icount_time_shift = 3;
6299 2e70f6ef pbrook
        init_icount_adjust();
6300 2e70f6ef pbrook
    }
6301 634fce96 pbrook
6302 fd1dff4b bellard
#ifdef _WIN32
6303 fd1dff4b bellard
    socket_init();
6304 fd1dff4b bellard
#endif
6305 fd1dff4b bellard
6306 7c9d8e07 bellard
    /* init network clients */
6307 7c9d8e07 bellard
    if (nb_net_clients == 0) {
6308 7c9d8e07 bellard
        /* if no clients, we use a default config */
6309 f441b28b aliguori
        net_clients[nb_net_clients++] = "nic";
6310 f441b28b aliguori
#ifdef CONFIG_SLIRP
6311 f441b28b aliguori
        net_clients[nb_net_clients++] = "user";
6312 f441b28b aliguori
#endif
6313 c20709aa bellard
    }
6314 c20709aa bellard
6315 7c9d8e07 bellard
    for(i = 0;i < nb_net_clients; i++) {
6316 9ad97e65 balrog
        if (net_client_parse(net_clients[i]) < 0)
6317 7c9d8e07 bellard
            exit(1);
6318 702c651c bellard
    }
6319 63a01ef8 aliguori
    net_client_check();
6320 f1510b2c bellard
6321 eec85c2a ths
#ifdef TARGET_I386
6322 ed494d87 balrog
    /* XXX: this should be moved in the PC machine instantiation code */
6323 28c5af54 j_mayer
    if (net_boot != 0) {
6324 28c5af54 j_mayer
        int netroms = 0;
6325 28c5af54 j_mayer
        for (i = 0; i < nb_nics && i < 4; i++) {
6326 eec85c2a ths
            const char *model = nd_table[i].model;
6327 eec85c2a ths
            char buf[1024];
6328 28c5af54 j_mayer
            if (net_boot & (1 << i)) {
6329 28c5af54 j_mayer
                if (model == NULL)
6330 28c5af54 j_mayer
                    model = "ne2k_pci";
6331 28c5af54 j_mayer
                snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
6332 28c5af54 j_mayer
                if (get_image_size(buf) > 0) {
6333 28c5af54 j_mayer
                    if (nb_option_roms >= MAX_OPTION_ROMS) {
6334 28c5af54 j_mayer
                        fprintf(stderr, "Too many option ROMs\n");
6335 28c5af54 j_mayer
                        exit(1);
6336 28c5af54 j_mayer
                    }
6337 28c5af54 j_mayer
                    option_rom[nb_option_roms] = strdup(buf);
6338 28c5af54 j_mayer
                    nb_option_roms++;
6339 28c5af54 j_mayer
                    netroms++;
6340 28c5af54 j_mayer
                }
6341 28c5af54 j_mayer
            }
6342 eec85c2a ths
        }
6343 28c5af54 j_mayer
        if (netroms == 0) {
6344 eec85c2a ths
            fprintf(stderr, "No valid PXE rom found for network device\n");
6345 eec85c2a ths
            exit(1);
6346 eec85c2a ths
        }
6347 eec85c2a ths
    }
6348 eec85c2a ths
#endif
6349 eec85c2a ths
6350 dc72ac14 balrog
    /* init the bluetooth world */
6351 dc72ac14 balrog
    for (i = 0; i < nb_bt_opts; i++)
6352 dc72ac14 balrog
        if (bt_parse(bt_opts[i]))
6353 dc72ac14 balrog
            exit(1);
6354 dc72ac14 balrog
6355 0824d6fc bellard
    /* init the memory */
6356 7fb4fdcf balrog
    phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
6357 7fb4fdcf balrog
6358 7fb4fdcf balrog
    if (machine->ram_require & RAMSIZE_FIXED) {
6359 7fb4fdcf balrog
        if (ram_size > 0) {
6360 7fb4fdcf balrog
            if (ram_size < phys_ram_size) {
6361 cd940061 aurel32
                fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
6362 cd940061 aurel32
                                machine->name, (unsigned long long) phys_ram_size);
6363 7fb4fdcf balrog
                exit(-1);
6364 7fb4fdcf balrog
            }
6365 7fb4fdcf balrog
6366 7fb4fdcf balrog
            phys_ram_size = ram_size;
6367 7fb4fdcf balrog
        } else
6368 7fb4fdcf balrog
            ram_size = phys_ram_size;
6369 7fb4fdcf balrog
    } else {
6370 4fc5d071 aurel32
        if (ram_size == 0)
6371 7fb4fdcf balrog
            ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
6372 7fb4fdcf balrog
6373 7fb4fdcf balrog
        phys_ram_size += ram_size;
6374 7fb4fdcf balrog
    }
6375 9ae02555 ths
6376 d993e026 bellard
    phys_ram_base = qemu_vmalloc(phys_ram_size);
6377 7f7f9873 bellard
    if (!phys_ram_base) {
6378 7f7f9873 bellard
        fprintf(stderr, "Could not allocate physical memory\n");
6379 0824d6fc bellard
        exit(1);
6380 0824d6fc bellard
    }
6381 0824d6fc bellard
6382 26a5f13b bellard
    /* init the dynamic translator */
6383 26a5f13b bellard
    cpu_exec_init_all(tb_size * 1024 * 1024);
6384 26a5f13b bellard
6385 5905b2e5 bellard
    bdrv_init();
6386 c4b1fcc0 bellard
6387 e4bcb14c ths
    /* we always create the cdrom drive, even if no disk is there */
6388 c4b1fcc0 bellard
6389 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
6390 609497ab balrog
        drive_add(NULL, CDROM_ALIAS);
6391 c4b1fcc0 bellard
6392 9d413d1d balrog
    /* we always create at least one floppy */
6393 33e3963e bellard
6394 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
6395 609497ab balrog
        drive_add(NULL, FD_ALIAS, 0);
6396 86f55663 j_mayer
6397 9d413d1d balrog
    /* we always create one sd slot, even if no card is in it */
6398 9d413d1d balrog
6399 9d413d1d balrog
    if (nb_drives_opt < MAX_DRIVES)
6400 609497ab balrog
        drive_add(NULL, SD_ALIAS);
6401 9d413d1d balrog
6402 e4bcb14c ths
    /* open the virtual block devices */
6403 e4bcb14c ths
6404 e4bcb14c ths
    for(i = 0; i < nb_drives_opt; i++)
6405 609497ab balrog
        if (drive_init(&drives_opt[i], snapshot, machine) == -1)
6406 e4bcb14c ths
            exit(1);
6407 3e3d5815 balrog
6408 c88676f8 bellard
    register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
6409 475e4277 aliguori
    register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
6410 8a7ddc38 bellard
6411 313aa567 bellard
    /* terminal init */
6412 740733bb ths
    memset(&display_state, 0, sizeof(display_state));
6413 a20dd508 bellard
    if (nographic) {
6414 4d3b6f6e balrog
        if (curses) {
6415 4d3b6f6e balrog
            fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
6416 4d3b6f6e balrog
            exit(1);
6417 4d3b6f6e balrog
        }
6418 2ff89790 ths
        /* nearly nothing to do */
6419 2ff89790 ths
        dumb_display_init(ds);
6420 73fc9742 ths
    } else if (vnc_display != NULL) {
6421 71cab5ca ths
        vnc_display_init(ds);
6422 71cab5ca ths
        if (vnc_display_open(ds, vnc_display) < 0)
6423 71cab5ca ths
            exit(1);
6424 4d3b6f6e balrog
    } else
6425 4d3b6f6e balrog
#if defined(CONFIG_CURSES)
6426 4d3b6f6e balrog
    if (curses) {
6427 4d3b6f6e balrog
        curses_display_init(ds, full_screen);
6428 4d3b6f6e balrog
    } else
6429 4d3b6f6e balrog
#endif
6430 4d3b6f6e balrog
    {
6431 5b0753e0 bellard
#if defined(CONFIG_SDL)
6432 43523e93 ths
        sdl_display_init(ds, full_screen, no_frame);
6433 5b0753e0 bellard
#elif defined(CONFIG_COCOA)
6434 5b0753e0 bellard
        cocoa_display_init(ds, full_screen);
6435 67276f53 pbrook
#else
6436 67276f53 pbrook
        dumb_display_init(ds);
6437 313aa567 bellard
#endif
6438 313aa567 bellard
    }
6439 0824d6fc bellard
6440 5b08fc10 aliguori
#ifndef _WIN32
6441 5b08fc10 aliguori
    /* must be after terminal init, SDL library changes signal handlers */
6442 5b08fc10 aliguori
    termsig_setup();
6443 5b08fc10 aliguori
#endif
6444 5b08fc10 aliguori
6445 20d8a3ed ths
    /* Maintain compatibility with multiple stdio monitors */
6446 20d8a3ed ths
    if (!strcmp(monitor_device,"stdio")) {
6447 20d8a3ed ths
        for (i = 0; i < MAX_SERIAL_PORTS; i++) {
6448 fd5f393a pbrook
            const char *devname = serial_devices[i];
6449 fd5f393a pbrook
            if (devname && !strcmp(devname,"mon:stdio")) {
6450 fd5f393a pbrook
                monitor_device = NULL;
6451 20d8a3ed ths
                break;
6452 fd5f393a pbrook
            } else if (devname && !strcmp(devname,"stdio")) {
6453 fd5f393a pbrook
                monitor_device = NULL;
6454 fd5f393a pbrook
                serial_devices[i] = "mon:stdio";
6455 20d8a3ed ths
                break;
6456 20d8a3ed ths
            }
6457 20d8a3ed ths
        }
6458 20d8a3ed ths
    }
6459 fd5f393a pbrook
    if (monitor_device) {
6460 5ccfae10 aliguori
        monitor_hd = qemu_chr_open("monitor", monitor_device);
6461 20d8a3ed ths
        if (!monitor_hd) {
6462 20d8a3ed ths
            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
6463 20d8a3ed ths
            exit(1);
6464 20d8a3ed ths
        }
6465 20d8a3ed ths
        monitor_init(monitor_hd, !nographic);
6466 82c643ff bellard
    }
6467 82c643ff bellard
6468 8d11df9e bellard
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6469 c03b0f0f bellard
        const char *devname = serial_devices[i];
6470 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
6471 5ccfae10 aliguori
            char label[32];
6472 5ccfae10 aliguori
            snprintf(label, sizeof(label), "serial%d", i);
6473 5ccfae10 aliguori
            serial_hds[i] = qemu_chr_open(label, devname);
6474 8d11df9e bellard
            if (!serial_hds[i]) {
6475 5fafdf24 ths
                fprintf(stderr, "qemu: could not open serial device '%s'\n",
6476 c03b0f0f bellard
                        devname);
6477 8d11df9e bellard
                exit(1);
6478 8d11df9e bellard
            }
6479 af3a9031 ths
            if (strstart(devname, "vc", 0))
6480 7ba1260a bellard
                qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
6481 8d11df9e bellard
        }
6482 82c643ff bellard
    }
6483 82c643ff bellard
6484 6508fe59 bellard
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6485 c03b0f0f bellard
        const char *devname = parallel_devices[i];
6486 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
6487 5ccfae10 aliguori
            char label[32];
6488 5ccfae10 aliguori
            snprintf(label, sizeof(label), "parallel%d", i);
6489 5ccfae10 aliguori
            parallel_hds[i] = qemu_chr_open(label, devname);
6490 6508fe59 bellard
            if (!parallel_hds[i]) {
6491 5fafdf24 ths
                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
6492 c03b0f0f bellard
                        devname);
6493 6508fe59 bellard
                exit(1);
6494 6508fe59 bellard
            }
6495 af3a9031 ths
            if (strstart(devname, "vc", 0))
6496 7ba1260a bellard
                qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
6497 6508fe59 bellard
        }
6498 6508fe59 bellard
    }
6499 6508fe59 bellard
6500 7ba1e619 aliguori
    if (kvm_enabled()) {
6501 7ba1e619 aliguori
        int ret;
6502 7ba1e619 aliguori
6503 7ba1e619 aliguori
        ret = kvm_init(smp_cpus);
6504 7ba1e619 aliguori
        if (ret < 0) {
6505 7ba1e619 aliguori
            fprintf(stderr, "failed to initialize KVM\n");
6506 7ba1e619 aliguori
            exit(1);
6507 7ba1e619 aliguori
        }
6508 7ba1e619 aliguori
    }
6509 7ba1e619 aliguori
6510 b881c2c6 blueswir1
    machine->init(ram_size, vga_ram_size, boot_devices, ds,
6511 94fc95cd j_mayer
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6512 73332e5c bellard
6513 0d92ed30 pbrook
    /* init USB devices */
6514 0d92ed30 pbrook
    if (usb_enabled) {
6515 0d92ed30 pbrook
        for(i = 0; i < usb_devices_index; i++) {
6516 0d92ed30 pbrook
            if (usb_device_add(usb_devices[i]) < 0) {
6517 0d92ed30 pbrook
                fprintf(stderr, "Warning: could not add USB device %s\n",
6518 0d92ed30 pbrook
                        usb_devices[i]);
6519 0d92ed30 pbrook
            }
6520 0d92ed30 pbrook
        }
6521 0d92ed30 pbrook
    }
6522 0d92ed30 pbrook
6523 740733bb ths
    if (display_state.dpy_refresh) {
6524 740733bb ths
        display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
6525 740733bb ths
        qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
6526 740733bb ths
    }
6527 7f7f9873 bellard
6528 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
6529 b4608c04 bellard
    if (use_gdbstub) {
6530 c636bb66 bellard
        /* XXX: use standard host:port notation and modify options
6531 c636bb66 bellard
           accordingly. */
6532 cfc3475a pbrook
        if (gdbserver_start(gdbstub_port) < 0) {
6533 cfc3475a pbrook
            fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
6534 c636bb66 bellard
                    gdbstub_port);
6535 8a7ddc38 bellard
            exit(1);
6536 8a7ddc38 bellard
        }
6537 45669e00 balrog
    }
6538 67b915a5 bellard
#endif
6539 45669e00 balrog
6540 d63d307f bellard
    if (loadvm)
6541 faea38e7 bellard
        do_loadvm(loadvm);
6542 d63d307f bellard
6543 5bb7910a aliguori
    if (incoming) {
6544 5bb7910a aliguori
        autostart = 0; /* fixme how to deal with -daemonize */
6545 5bb7910a aliguori
        qemu_start_incoming_migration(incoming);
6546 5bb7910a aliguori
    }
6547 5bb7910a aliguori
6548 67b915a5 bellard
    {
6549 5905b2e5 bellard
        /* XXX: simplify init */
6550 83ab7950 aliguori
        read_passwords();
6551 3c07f8e8 pbrook
        if (autostart) {
6552 5905b2e5 bellard
            vm_start();
6553 5905b2e5 bellard
        }
6554 0824d6fc bellard
    }
6555 ffd843bc ths
6556 71e3ceb8 ths
    if (daemonize) {
6557 71e3ceb8 ths
        uint8_t status = 0;
6558 71e3ceb8 ths
        ssize_t len;
6559 71e3ceb8 ths
        int fd;
6560 71e3ceb8 ths
6561 71e3ceb8 ths
    again1:
6562 71e3ceb8 ths
        len = write(fds[1], &status, 1);
6563 71e3ceb8 ths
        if (len == -1 && (errno == EINTR))
6564 71e3ceb8 ths
            goto again1;
6565 71e3ceb8 ths
6566 71e3ceb8 ths
        if (len != 1)
6567 71e3ceb8 ths
            exit(1);
6568 71e3ceb8 ths
6569 bd54b863 aliguori
        chdir("/");
6570 aeb30be6 balrog
        TFR(fd = open("/dev/null", O_RDWR));
6571 71e3ceb8 ths
        if (fd == -1)
6572 71e3ceb8 ths
            exit(1);
6573 71e3ceb8 ths
6574 71e3ceb8 ths
        dup2(fd, 0);
6575 71e3ceb8 ths
        dup2(fd, 1);
6576 71e3ceb8 ths
        dup2(fd, 2);
6577 71e3ceb8 ths
6578 71e3ceb8 ths
        close(fd);
6579 71e3ceb8 ths
    }
6580 71e3ceb8 ths
6581 8a7ddc38 bellard
    main_loop();
6582 40c3bac3 bellard
    quit_timers();
6583 63a01ef8 aliguori
    net_cleanup();
6584 b46a8906 ths
6585 0824d6fc bellard
    return 0;
6586 0824d6fc bellard
}