Statistics
| Branch: | Revision:

root / vl.c @ 63a01ef8

History | View | Annotate | Download (165.4 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 67b915a5 bellard
43 0824d6fc bellard
#include <unistd.h>
44 0824d6fc bellard
#include <fcntl.h>
45 0824d6fc bellard
#include <signal.h>
46 0824d6fc bellard
#include <time.h>
47 0824d6fc bellard
#include <errno.h>
48 67b915a5 bellard
#include <sys/time.h>
49 c88676f8 bellard
#include <zlib.h>
50 67b915a5 bellard
51 67b915a5 bellard
#ifndef _WIN32
52 67b915a5 bellard
#include <sys/times.h>
53 f1510b2c bellard
#include <sys/wait.h>
54 67b915a5 bellard
#include <termios.h>
55 67b915a5 bellard
#include <sys/mman.h>
56 f1510b2c bellard
#include <sys/ioctl.h>
57 f1510b2c bellard
#include <sys/socket.h>
58 c94c8d64 bellard
#include <netinet/in.h>
59 9d728e8c bellard
#include <dirent.h>
60 7c9d8e07 bellard
#include <netdb.h>
61 cb4b976b ths
#include <sys/select.h>
62 cb4b976b ths
#include <arpa/inet.h>
63 7d3505c5 bellard
#ifdef _BSD
64 7d3505c5 bellard
#include <sys/stat.h>
65 128ab2ff blueswir1
#if !defined(__APPLE__) && !defined(__OpenBSD__)
66 7d3505c5 bellard
#include <libutil.h>
67 83fb7adf bellard
#endif
68 128ab2ff blueswir1
#ifdef __OpenBSD__
69 128ab2ff blueswir1
#include <net/if.h>
70 128ab2ff blueswir1
#endif
71 5c40d2bd ths
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 5c40d2bd ths
#include <freebsd/stdlib.h>
73 7d3505c5 bellard
#else
74 223f0d72 blueswir1
#ifdef __linux__
75 f1510b2c bellard
#include <linux/if.h>
76 f1510b2c bellard
#include <linux/if_tun.h>
77 7d3505c5 bellard
#include <pty.h>
78 7d3505c5 bellard
#include <malloc.h>
79 fd872598 bellard
#include <linux/rtc.h>
80 bd494f4c ths
81 bd494f4c ths
/* For the benefit of older linux systems which don't supply it,
82 bd494f4c ths
   we use a local copy of hpet.h. */
83 bd494f4c ths
/* #include <linux/hpet.h> */
84 bd494f4c ths
#include "hpet.h"
85 bd494f4c ths
86 e57a8c0e bellard
#include <linux/ppdev.h>
87 5867c88a ths
#include <linux/parport.h>
88 223f0d72 blueswir1
#endif
89 223f0d72 blueswir1
#ifdef __sun__
90 d5d10bc3 ths
#include <sys/stat.h>
91 d5d10bc3 ths
#include <sys/ethernet.h>
92 d5d10bc3 ths
#include <sys/sockio.h>
93 d5d10bc3 ths
#include <netinet/arp.h>
94 d5d10bc3 ths
#include <netinet/in.h>
95 d5d10bc3 ths
#include <netinet/in_systm.h>
96 d5d10bc3 ths
#include <netinet/ip.h>
97 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
98 d5d10bc3 ths
#include <netinet/udp.h>
99 d5d10bc3 ths
#include <netinet/tcp.h>
100 d5d10bc3 ths
#include <net/if.h>
101 d5d10bc3 ths
#include <syslog.h>
102 d5d10bc3 ths
#include <stropts.h>
103 67b915a5 bellard
#endif
104 7d3505c5 bellard
#endif
105 ec530c81 bellard
#endif
106 67b915a5 bellard
107 03ff3ca3 aliguori
#include "qemu_socket.h"
108 03ff3ca3 aliguori
109 c20709aa bellard
#if defined(CONFIG_SLIRP)
110 c20709aa bellard
#include "libslirp.h"
111 c20709aa bellard
#endif
112 c20709aa bellard
113 9892fbfb blueswir1
#if defined(__OpenBSD__)
114 9892fbfb blueswir1
#include <util.h>
115 9892fbfb blueswir1
#endif
116 9892fbfb blueswir1
117 8a16d273 ths
#if defined(CONFIG_VDE)
118 8a16d273 ths
#include <libvdeplug.h>
119 8a16d273 ths
#endif
120 8a16d273 ths
121 67b915a5 bellard
#ifdef _WIN32
122 7d3505c5 bellard
#include <malloc.h>
123 67b915a5 bellard
#include <sys/timeb.h>
124 4fddf62a ths
#include <mmsystem.h>
125 67b915a5 bellard
#define getopt_long_only getopt_long
126 67b915a5 bellard
#define memalign(align, size) malloc(size)
127 67b915a5 bellard
#endif
128 67b915a5 bellard
129 73332e5c bellard
#ifdef CONFIG_SDL
130 96bcd4f8 bellard
#ifdef __APPLE__
131 83fb7adf bellard
#include <SDL/SDL.h>
132 96bcd4f8 bellard
#endif
133 73332e5c bellard
#endif /* CONFIG_SDL */
134 0824d6fc bellard
135 5b0753e0 bellard
#ifdef CONFIG_COCOA
136 5b0753e0 bellard
#undef main
137 5b0753e0 bellard
#define main qemu_main
138 5b0753e0 bellard
#endif /* CONFIG_COCOA */
139 5b0753e0 bellard
140 0824d6fc bellard
#include "disas.h"
141 fc01f7e7 bellard
142 8a7ddc38 bellard
#include "exec-all.h"
143 0824d6fc bellard
144 5a67135a bellard
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
145 b46a8906 ths
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
146 a14d6c8c pbrook
#ifdef __sun__
147 a14d6c8c pbrook
#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
148 a14d6c8c pbrook
#else
149 a14d6c8c pbrook
#define SMBD_COMMAND "/usr/sbin/smbd"
150 a14d6c8c pbrook
#endif
151 f1510b2c bellard
152 0824d6fc bellard
//#define DEBUG_UNUSED_IOPORT
153 fd872598 bellard
//#define DEBUG_IOPORT
154 9dc63a1e blueswir1
//#define DEBUG_NET
155 9dc63a1e blueswir1
//#define DEBUG_SLIRP
156 330d0414 bellard
157 77d4bc34 bellard
#ifdef TARGET_PPC
158 77d4bc34 bellard
#define DEFAULT_RAM_SIZE 144
159 77d4bc34 bellard
#else
160 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
161 77d4bc34 bellard
#endif
162 313aa567 bellard
163 0d92ed30 pbrook
/* Max number of USB devices that can be specified on the commandline.  */
164 0d92ed30 pbrook
#define MAX_USB_CMDLINE 8
165 0d92ed30 pbrook
166 7dea1da4 bellard
/* XXX: use a two level table to limit memory usage */
167 7dea1da4 bellard
#define MAX_IOPORTS 65536
168 0824d6fc bellard
169 80cabfad bellard
const char *bios_dir = CONFIG_QEMU_SHAREDIR;
170 1192dad8 j_mayer
const char *bios_name = NULL;
171 dbed7e40 blueswir1
static void *ioport_opaque[MAX_IOPORTS];
172 dbed7e40 blueswir1
static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
173 dbed7e40 blueswir1
static IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
174 e4bcb14c ths
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
175 faea38e7 bellard
   to store the VM snapshots */
176 e4bcb14c ths
DriveInfo drives_table[MAX_DRIVES+1];
177 e4bcb14c ths
int nb_drives;
178 faea38e7 bellard
/* point to the block driver where the snapshots are managed */
179 dbed7e40 blueswir1
static BlockDriverState *bs_snapshots;
180 dbed7e40 blueswir1
static int vga_ram_size;
181 cb5a7aa8 malc
enum vga_retrace_method vga_retrace_method = VGA_RETRACE_DUMB;
182 0e82f34d aliguori
DisplayState display_state;
183 a20dd508 bellard
int nographic;
184 dbed7e40 blueswir1
static int curses;
185 3d11d0eb bellard
const char* keyboard_layout = NULL;
186 313aa567 bellard
int64_t ticks_per_sec;
187 00f82b8a aurel32
ram_addr_t ram_size;
188 c4b1fcc0 bellard
int nb_nics;
189 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
190 8a7ddc38 bellard
int vm_running;
191 f6503059 balrog
static int rtc_utc = 1;
192 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
193 1bfe856e bellard
int cirrus_vga_enabled = 1;
194 d34cab9f ths
int vmsvga_enabled = 0;
195 d827220b bellard
#ifdef TARGET_SPARC
196 d827220b bellard
int graphic_width = 1024;
197 d827220b bellard
int graphic_height = 768;
198 eee0b836 blueswir1
int graphic_depth = 8;
199 d827220b bellard
#else
200 1bfe856e bellard
int graphic_width = 800;
201 1bfe856e bellard
int graphic_height = 600;
202 e9b137c2 bellard
int graphic_depth = 15;
203 eee0b836 blueswir1
#endif
204 dbed7e40 blueswir1
static int full_screen = 0;
205 dbed7e40 blueswir1
static int no_frame = 0;
206 667accab ths
int no_quit = 0;
207 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
208 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
209 a09db21f bellard
#ifdef TARGET_I386
210 a09db21f bellard
int win2k_install_hack = 0;
211 a09db21f bellard
#endif
212 bb36d470 bellard
int usb_enabled = 0;
213 6a00d601 bellard
int smp_cpus = 1;
214 73fc9742 ths
const char *vnc_display;
215 6515b203 bellard
int acpi_enabled = 1;
216 52ca8d6a bellard
int fd_bootchk = 1;
217 d1beab82 bellard
int no_reboot = 0;
218 b2f76161 aurel32
int no_shutdown = 0;
219 9467cd46 balrog
int cursor_hide = 1;
220 a171fe39 balrog
int graphic_rotate = 0;
221 71e3ceb8 ths
int daemonize = 0;
222 9ae02555 ths
const char *option_rom[MAX_OPTION_ROMS];
223 9ae02555 ths
int nb_option_roms;
224 8e71621f pbrook
int semihosting_enabled = 0;
225 2b8f2d41 balrog
#ifdef TARGET_ARM
226 2b8f2d41 balrog
int old_param = 0;
227 2b8f2d41 balrog
#endif
228 c35734b2 ths
const char *qemu_name;
229 3780e197 ths
int alt_grab = 0;
230 66508601 blueswir1
#ifdef TARGET_SPARC
231 66508601 blueswir1
unsigned int nb_prom_envs = 0;
232 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
233 66508601 blueswir1
#endif
234 dbed7e40 blueswir1
static int nb_drives_opt;
235 dbed7e40 blueswir1
static struct drive_opt {
236 609497ab balrog
    const char *file;
237 609497ab balrog
    char opt[1024];
238 609497ab balrog
} drives_opt[MAX_DRIVES];
239 0824d6fc bellard
240 ee5605e5 balrog
static CPUState *cur_cpu;
241 ee5605e5 balrog
static CPUState *next_cpu;
242 76ea08f9 balrog
static int event_pending = 1;
243 bf20dc07 ths
/* Conversion factor from emulated instructions to virtual clock ticks.  */
244 2e70f6ef pbrook
static int icount_time_shift;
245 bf20dc07 ths
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
246 2e70f6ef pbrook
#define MAX_ICOUNT_SHIFT 10
247 2e70f6ef pbrook
/* Compensate for varying guest execution speed.  */
248 2e70f6ef pbrook
static int64_t qemu_icount_bias;
249 dbed7e40 blueswir1
static QEMUTimer *icount_rt_timer;
250 dbed7e40 blueswir1
static QEMUTimer *icount_vm_timer;
251 ee5605e5 balrog
252 8fcb1b90 blueswir1
uint8_t qemu_uuid[16];
253 8fcb1b90 blueswir1
254 0824d6fc bellard
/***********************************************************/
255 26aa7d72 bellard
/* x86 ISA bus support */
256 26aa7d72 bellard
257 26aa7d72 bellard
target_phys_addr_t isa_mem_base = 0;
258 3de388f6 bellard
PicState2 *isa_pic;
259 0824d6fc bellard
260 477e3edf aliguori
static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
261 477e3edf aliguori
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
262 477e3edf aliguori
263 477e3edf aliguori
static uint32_t ioport_read(int index, uint32_t address)
264 477e3edf aliguori
{
265 477e3edf aliguori
    static IOPortReadFunc *default_func[3] = {
266 477e3edf aliguori
        default_ioport_readb,
267 477e3edf aliguori
        default_ioport_readw,
268 477e3edf aliguori
        default_ioport_readl
269 477e3edf aliguori
    };
270 477e3edf aliguori
    IOPortReadFunc *func = ioport_read_table[index][address];
271 477e3edf aliguori
    if (!func)
272 477e3edf aliguori
        func = default_func[index];
273 477e3edf aliguori
    return func(ioport_opaque[address], address);
274 477e3edf aliguori
}
275 477e3edf aliguori
276 477e3edf aliguori
static void ioport_write(int index, uint32_t address, uint32_t data)
277 477e3edf aliguori
{
278 477e3edf aliguori
    static IOPortWriteFunc *default_func[3] = {
279 477e3edf aliguori
        default_ioport_writeb,
280 477e3edf aliguori
        default_ioport_writew,
281 477e3edf aliguori
        default_ioport_writel
282 477e3edf aliguori
    };
283 477e3edf aliguori
    IOPortWriteFunc *func = ioport_write_table[index][address];
284 477e3edf aliguori
    if (!func)
285 477e3edf aliguori
        func = default_func[index];
286 477e3edf aliguori
    func(ioport_opaque[address], address, data);
287 477e3edf aliguori
}
288 477e3edf aliguori
289 9596ebb7 pbrook
static uint32_t default_ioport_readb(void *opaque, uint32_t address)
290 0824d6fc bellard
{
291 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
292 1196be37 ths
    fprintf(stderr, "unused inb: port=0x%04x\n", address);
293 0824d6fc bellard
#endif
294 fc01f7e7 bellard
    return 0xff;
295 0824d6fc bellard
}
296 0824d6fc bellard
297 9596ebb7 pbrook
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
298 0824d6fc bellard
{
299 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
300 1196be37 ths
    fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
301 0824d6fc bellard
#endif
302 0824d6fc bellard
}
303 0824d6fc bellard
304 0824d6fc bellard
/* default is to make two byte accesses */
305 9596ebb7 pbrook
static uint32_t default_ioport_readw(void *opaque, uint32_t address)
306 0824d6fc bellard
{
307 0824d6fc bellard
    uint32_t data;
308 477e3edf aliguori
    data = ioport_read(0, address);
309 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
310 477e3edf aliguori
    data |= ioport_read(0, address) << 8;
311 0824d6fc bellard
    return data;
312 0824d6fc bellard
}
313 0824d6fc bellard
314 9596ebb7 pbrook
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
315 0824d6fc bellard
{
316 477e3edf aliguori
    ioport_write(0, address, data & 0xff);
317 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
318 477e3edf aliguori
    ioport_write(0, address, (data >> 8) & 0xff);
319 0824d6fc bellard
}
320 0824d6fc bellard
321 9596ebb7 pbrook
static uint32_t default_ioport_readl(void *opaque, uint32_t address)
322 0824d6fc bellard
{
323 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
324 1196be37 ths
    fprintf(stderr, "unused inl: port=0x%04x\n", address);
325 fc01f7e7 bellard
#endif
326 fc01f7e7 bellard
    return 0xffffffff;
327 0824d6fc bellard
}
328 0824d6fc bellard
329 9596ebb7 pbrook
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
330 0824d6fc bellard
{
331 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
332 1196be37 ths
    fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
333 fc01f7e7 bellard
#endif
334 0824d6fc bellard
}
335 0824d6fc bellard
336 fc01f7e7 bellard
/* size is the word size in byte */
337 5fafdf24 ths
int register_ioport_read(int start, int length, int size,
338 c4b1fcc0 bellard
                         IOPortReadFunc *func, void *opaque)
339 f1510b2c bellard
{
340 fc01f7e7 bellard
    int i, bsize;
341 f1510b2c bellard
342 c4b1fcc0 bellard
    if (size == 1) {
343 fc01f7e7 bellard
        bsize = 0;
344 c4b1fcc0 bellard
    } else if (size == 2) {
345 fc01f7e7 bellard
        bsize = 1;
346 c4b1fcc0 bellard
    } else if (size == 4) {
347 fc01f7e7 bellard
        bsize = 2;
348 c4b1fcc0 bellard
    } else {
349 88fdf56f balrog
        hw_error("register_ioport_read: invalid size");
350 fc01f7e7 bellard
        return -1;
351 c4b1fcc0 bellard
    }
352 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
353 fc01f7e7 bellard
        ioport_read_table[bsize][i] = func;
354 c4b1fcc0 bellard
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
355 88fdf56f balrog
            hw_error("register_ioport_read: invalid opaque");
356 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
357 c4b1fcc0 bellard
    }
358 f1510b2c bellard
    return 0;
359 f1510b2c bellard
}
360 f1510b2c bellard
361 fc01f7e7 bellard
/* size is the word size in byte */
362 5fafdf24 ths
int register_ioport_write(int start, int length, int size,
363 c4b1fcc0 bellard
                          IOPortWriteFunc *func, void *opaque)
364 f1510b2c bellard
{
365 fc01f7e7 bellard
    int i, bsize;
366 f1510b2c bellard
367 c4b1fcc0 bellard
    if (size == 1) {
368 fc01f7e7 bellard
        bsize = 0;
369 c4b1fcc0 bellard
    } else if (size == 2) {
370 fc01f7e7 bellard
        bsize = 1;
371 c4b1fcc0 bellard
    } else if (size == 4) {
372 fc01f7e7 bellard
        bsize = 2;
373 c4b1fcc0 bellard
    } else {
374 88fdf56f balrog
        hw_error("register_ioport_write: invalid size");
375 fc01f7e7 bellard
        return -1;
376 c4b1fcc0 bellard
    }
377 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
378 fc01f7e7 bellard
        ioport_write_table[bsize][i] = func;
379 88fdf56f balrog
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
380 88fdf56f balrog
            hw_error("register_ioport_write: invalid opaque");
381 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
382 c4b1fcc0 bellard
    }
383 f1510b2c bellard
    return 0;
384 f1510b2c bellard
}
385 f1510b2c bellard
386 69b91039 bellard
void isa_unassign_ioport(int start, int length)
387 69b91039 bellard
{
388 69b91039 bellard
    int i;
389 69b91039 bellard
390 69b91039 bellard
    for(i = start; i < start + length; i++) {
391 69b91039 bellard
        ioport_read_table[0][i] = default_ioport_readb;
392 69b91039 bellard
        ioport_read_table[1][i] = default_ioport_readw;
393 69b91039 bellard
        ioport_read_table[2][i] = default_ioport_readl;
394 69b91039 bellard
395 69b91039 bellard
        ioport_write_table[0][i] = default_ioport_writeb;
396 69b91039 bellard
        ioport_write_table[1][i] = default_ioport_writew;
397 69b91039 bellard
        ioport_write_table[2][i] = default_ioport_writel;
398 69b91039 bellard
    }
399 69b91039 bellard
}
400 69b91039 bellard
401 20f32282 bellard
/***********************************************************/
402 20f32282 bellard
403 c45886db bellard
void cpu_outb(CPUState *env, int addr, int val)
404 0824d6fc bellard
{
405 fd872598 bellard
#ifdef DEBUG_IOPORT
406 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
407 fd872598 bellard
        fprintf(logfile, "outb: %04x %02x\n", addr, val);
408 3b46e624 ths
#endif
409 477e3edf aliguori
    ioport_write(0, addr, val);
410 89bfc105 bellard
#ifdef USE_KQEMU
411 89bfc105 bellard
    if (env)
412 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
413 89bfc105 bellard
#endif
414 0824d6fc bellard
}
415 0824d6fc bellard
416 c45886db bellard
void cpu_outw(CPUState *env, int addr, int val)
417 0824d6fc bellard
{
418 fd872598 bellard
#ifdef DEBUG_IOPORT
419 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
420 fd872598 bellard
        fprintf(logfile, "outw: %04x %04x\n", addr, val);
421 3b46e624 ths
#endif
422 477e3edf aliguori
    ioport_write(1, addr, val);
423 89bfc105 bellard
#ifdef USE_KQEMU
424 89bfc105 bellard
    if (env)
425 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
426 89bfc105 bellard
#endif
427 0824d6fc bellard
}
428 0824d6fc bellard
429 c45886db bellard
void cpu_outl(CPUState *env, int addr, int val)
430 0824d6fc bellard
{
431 fd872598 bellard
#ifdef DEBUG_IOPORT
432 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
433 fd872598 bellard
        fprintf(logfile, "outl: %04x %08x\n", addr, val);
434 fd872598 bellard
#endif
435 477e3edf aliguori
    ioport_write(2, addr, val);
436 89bfc105 bellard
#ifdef USE_KQEMU
437 89bfc105 bellard
    if (env)
438 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
439 89bfc105 bellard
#endif
440 0824d6fc bellard
}
441 0824d6fc bellard
442 c45886db bellard
int cpu_inb(CPUState *env, int addr)
443 0824d6fc bellard
{
444 fd872598 bellard
    int val;
445 477e3edf aliguori
    val = ioport_read(0, addr);
446 fd872598 bellard
#ifdef DEBUG_IOPORT
447 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
448 fd872598 bellard
        fprintf(logfile, "inb : %04x %02x\n", addr, val);
449 fd872598 bellard
#endif
450 89bfc105 bellard
#ifdef USE_KQEMU
451 89bfc105 bellard
    if (env)
452 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
453 89bfc105 bellard
#endif
454 fd872598 bellard
    return val;
455 0824d6fc bellard
}
456 0824d6fc bellard
457 c45886db bellard
int cpu_inw(CPUState *env, int addr)
458 0824d6fc bellard
{
459 fd872598 bellard
    int val;
460 477e3edf aliguori
    val = ioport_read(1, addr);
461 fd872598 bellard
#ifdef DEBUG_IOPORT
462 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
463 fd872598 bellard
        fprintf(logfile, "inw : %04x %04x\n", addr, val);
464 fd872598 bellard
#endif
465 89bfc105 bellard
#ifdef USE_KQEMU
466 89bfc105 bellard
    if (env)
467 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
468 89bfc105 bellard
#endif
469 fd872598 bellard
    return val;
470 0824d6fc bellard
}
471 0824d6fc bellard
472 c45886db bellard
int cpu_inl(CPUState *env, int addr)
473 0824d6fc bellard
{
474 fd872598 bellard
    int val;
475 477e3edf aliguori
    val = ioport_read(2, addr);
476 fd872598 bellard
#ifdef DEBUG_IOPORT
477 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
478 fd872598 bellard
        fprintf(logfile, "inl : %04x %08x\n", addr, val);
479 fd872598 bellard
#endif
480 89bfc105 bellard
#ifdef USE_KQEMU
481 89bfc105 bellard
    if (env)
482 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
483 89bfc105 bellard
#endif
484 fd872598 bellard
    return val;
485 0824d6fc bellard
}
486 0824d6fc bellard
487 0824d6fc bellard
/***********************************************************/
488 0824d6fc bellard
void hw_error(const char *fmt, ...)
489 0824d6fc bellard
{
490 0824d6fc bellard
    va_list ap;
491 6a00d601 bellard
    CPUState *env;
492 0824d6fc bellard
493 0824d6fc bellard
    va_start(ap, fmt);
494 0824d6fc bellard
    fprintf(stderr, "qemu: hardware error: ");
495 0824d6fc bellard
    vfprintf(stderr, fmt, ap);
496 0824d6fc bellard
    fprintf(stderr, "\n");
497 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
498 6a00d601 bellard
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
499 0824d6fc bellard
#ifdef TARGET_I386
500 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
501 c45886db bellard
#else
502 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, 0);
503 0824d6fc bellard
#endif
504 6a00d601 bellard
    }
505 0824d6fc bellard
    va_end(ap);
506 0824d6fc bellard
    abort();
507 0824d6fc bellard
}
508 0824d6fc bellard
509 8a7ddc38 bellard
/***********************************************************/
510 63066f4f bellard
/* keyboard/mouse */
511 63066f4f bellard
512 63066f4f bellard
static QEMUPutKBDEvent *qemu_put_kbd_event;
513 63066f4f bellard
static void *qemu_put_kbd_event_opaque;
514 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
515 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
516 63066f4f bellard
517 63066f4f bellard
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
518 63066f4f bellard
{
519 63066f4f bellard
    qemu_put_kbd_event_opaque = opaque;
520 63066f4f bellard
    qemu_put_kbd_event = func;
521 63066f4f bellard
}
522 63066f4f bellard
523 455204eb ths
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
524 455204eb ths
                                                void *opaque, int absolute,
525 455204eb ths
                                                const char *name)
526 63066f4f bellard
{
527 455204eb ths
    QEMUPutMouseEntry *s, *cursor;
528 455204eb ths
529 455204eb ths
    s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
530 455204eb ths
    if (!s)
531 455204eb ths
        return NULL;
532 455204eb ths
533 455204eb ths
    s->qemu_put_mouse_event = func;
534 455204eb ths
    s->qemu_put_mouse_event_opaque = opaque;
535 455204eb ths
    s->qemu_put_mouse_event_absolute = absolute;
536 455204eb ths
    s->qemu_put_mouse_event_name = qemu_strdup(name);
537 455204eb ths
    s->next = NULL;
538 455204eb ths
539 455204eb ths
    if (!qemu_put_mouse_event_head) {
540 455204eb ths
        qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
541 455204eb ths
        return s;
542 455204eb ths
    }
543 455204eb ths
544 455204eb ths
    cursor = qemu_put_mouse_event_head;
545 455204eb ths
    while (cursor->next != NULL)
546 455204eb ths
        cursor = cursor->next;
547 455204eb ths
548 455204eb ths
    cursor->next = s;
549 455204eb ths
    qemu_put_mouse_event_current = s;
550 455204eb ths
551 455204eb ths
    return s;
552 455204eb ths
}
553 455204eb ths
554 455204eb ths
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
555 455204eb ths
{
556 455204eb ths
    QEMUPutMouseEntry *prev = NULL, *cursor;
557 455204eb ths
558 455204eb ths
    if (!qemu_put_mouse_event_head || entry == NULL)
559 455204eb ths
        return;
560 455204eb ths
561 455204eb ths
    cursor = qemu_put_mouse_event_head;
562 455204eb ths
    while (cursor != NULL && cursor != entry) {
563 455204eb ths
        prev = cursor;
564 455204eb ths
        cursor = cursor->next;
565 455204eb ths
    }
566 455204eb ths
567 455204eb ths
    if (cursor == NULL) // does not exist or list empty
568 455204eb ths
        return;
569 455204eb ths
    else if (prev == NULL) { // entry is head
570 455204eb ths
        qemu_put_mouse_event_head = cursor->next;
571 455204eb ths
        if (qemu_put_mouse_event_current == entry)
572 455204eb ths
            qemu_put_mouse_event_current = cursor->next;
573 455204eb ths
        qemu_free(entry->qemu_put_mouse_event_name);
574 455204eb ths
        qemu_free(entry);
575 455204eb ths
        return;
576 455204eb ths
    }
577 455204eb ths
578 455204eb ths
    prev->next = entry->next;
579 455204eb ths
580 455204eb ths
    if (qemu_put_mouse_event_current == entry)
581 455204eb ths
        qemu_put_mouse_event_current = prev;
582 455204eb ths
583 455204eb ths
    qemu_free(entry->qemu_put_mouse_event_name);
584 455204eb ths
    qemu_free(entry);
585 63066f4f bellard
}
586 63066f4f bellard
587 63066f4f bellard
void kbd_put_keycode(int keycode)
588 63066f4f bellard
{
589 63066f4f bellard
    if (qemu_put_kbd_event) {
590 63066f4f bellard
        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
591 63066f4f bellard
    }
592 63066f4f bellard
}
593 63066f4f bellard
594 63066f4f bellard
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
595 63066f4f bellard
{
596 455204eb ths
    QEMUPutMouseEvent *mouse_event;
597 455204eb ths
    void *mouse_event_opaque;
598 a171fe39 balrog
    int width;
599 455204eb ths
600 455204eb ths
    if (!qemu_put_mouse_event_current) {
601 455204eb ths
        return;
602 455204eb ths
    }
603 455204eb ths
604 455204eb ths
    mouse_event =
605 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event;
606 455204eb ths
    mouse_event_opaque =
607 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
608 455204eb ths
609 455204eb ths
    if (mouse_event) {
610 a171fe39 balrog
        if (graphic_rotate) {
611 a171fe39 balrog
            if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
612 a171fe39 balrog
                width = 0x7fff;
613 a171fe39 balrog
            else
614 b94ed577 aurel32
                width = graphic_width - 1;
615 a171fe39 balrog
            mouse_event(mouse_event_opaque,
616 a171fe39 balrog
                                 width - dy, dx, dz, buttons_state);
617 a171fe39 balrog
        } else
618 a171fe39 balrog
            mouse_event(mouse_event_opaque,
619 a171fe39 balrog
                                 dx, dy, dz, buttons_state);
620 63066f4f bellard
    }
621 63066f4f bellard
}
622 63066f4f bellard
623 09b26c5e bellard
int kbd_mouse_is_absolute(void)
624 09b26c5e bellard
{
625 455204eb ths
    if (!qemu_put_mouse_event_current)
626 455204eb ths
        return 0;
627 455204eb ths
628 455204eb ths
    return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
629 455204eb ths
}
630 455204eb ths
631 455204eb ths
void do_info_mice(void)
632 455204eb ths
{
633 455204eb ths
    QEMUPutMouseEntry *cursor;
634 455204eb ths
    int index = 0;
635 455204eb ths
636 455204eb ths
    if (!qemu_put_mouse_event_head) {
637 455204eb ths
        term_printf("No mouse devices connected\n");
638 455204eb ths
        return;
639 455204eb ths
    }
640 455204eb ths
641 455204eb ths
    term_printf("Mouse devices available:\n");
642 455204eb ths
    cursor = qemu_put_mouse_event_head;
643 455204eb ths
    while (cursor != NULL) {
644 455204eb ths
        term_printf("%c Mouse #%d: %s\n",
645 455204eb ths
                    (cursor == qemu_put_mouse_event_current ? '*' : ' '),
646 455204eb ths
                    index, cursor->qemu_put_mouse_event_name);
647 455204eb ths
        index++;
648 455204eb ths
        cursor = cursor->next;
649 455204eb ths
    }
650 455204eb ths
}
651 455204eb ths
652 455204eb ths
void do_mouse_set(int index)
653 455204eb ths
{
654 455204eb ths
    QEMUPutMouseEntry *cursor;
655 455204eb ths
    int i = 0;
656 455204eb ths
657 455204eb ths
    if (!qemu_put_mouse_event_head) {
658 455204eb ths
        term_printf("No mouse devices connected\n");
659 455204eb ths
        return;
660 455204eb ths
    }
661 455204eb ths
662 455204eb ths
    cursor = qemu_put_mouse_event_head;
663 455204eb ths
    while (cursor != NULL && index != i) {
664 455204eb ths
        i++;
665 455204eb ths
        cursor = cursor->next;
666 455204eb ths
    }
667 455204eb ths
668 455204eb ths
    if (cursor != NULL)
669 455204eb ths
        qemu_put_mouse_event_current = cursor;
670 455204eb ths
    else
671 455204eb ths
        term_printf("Mouse at given index not found\n");
672 09b26c5e bellard
}
673 09b26c5e bellard
674 1dce7c3c bellard
/* compute with 96 bit intermediate result: (a*b)/c */
675 1dce7c3c bellard
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
676 0824d6fc bellard
{
677 1dce7c3c bellard
    union {
678 1dce7c3c bellard
        uint64_t ll;
679 1dce7c3c bellard
        struct {
680 1dce7c3c bellard
#ifdef WORDS_BIGENDIAN
681 1dce7c3c bellard
            uint32_t high, low;
682 1dce7c3c bellard
#else
683 1dce7c3c bellard
            uint32_t low, high;
684 3b46e624 ths
#endif
685 1dce7c3c bellard
        } l;
686 1dce7c3c bellard
    } u, res;
687 1dce7c3c bellard
    uint64_t rl, rh;
688 0824d6fc bellard
689 1dce7c3c bellard
    u.ll = a;
690 1dce7c3c bellard
    rl = (uint64_t)u.l.low * (uint64_t)b;
691 1dce7c3c bellard
    rh = (uint64_t)u.l.high * (uint64_t)b;
692 1dce7c3c bellard
    rh += (rl >> 32);
693 1dce7c3c bellard
    res.l.high = rh / c;
694 1dce7c3c bellard
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
695 1dce7c3c bellard
    return res.ll;
696 34865134 bellard
}
697 34865134 bellard
698 1dce7c3c bellard
/***********************************************************/
699 1dce7c3c bellard
/* real time host monotonic timer */
700 34865134 bellard
701 1dce7c3c bellard
#define QEMU_TIMER_BASE 1000000000LL
702 34865134 bellard
703 1dce7c3c bellard
#ifdef WIN32
704 0824d6fc bellard
705 1dce7c3c bellard
static int64_t clock_freq;
706 1115dde7 bellard
707 1dce7c3c bellard
static void init_get_clock(void)
708 1115dde7 bellard
{
709 a8e5ac33 bellard
    LARGE_INTEGER freq;
710 a8e5ac33 bellard
    int ret;
711 1dce7c3c bellard
    ret = QueryPerformanceFrequency(&freq);
712 1dce7c3c bellard
    if (ret == 0) {
713 1dce7c3c bellard
        fprintf(stderr, "Could not calibrate ticks\n");
714 1dce7c3c bellard
        exit(1);
715 1dce7c3c bellard
    }
716 1dce7c3c bellard
    clock_freq = freq.QuadPart;
717 1115dde7 bellard
}
718 1115dde7 bellard
719 1dce7c3c bellard
static int64_t get_clock(void)
720 b8076a74 bellard
{
721 1dce7c3c bellard
    LARGE_INTEGER ti;
722 1dce7c3c bellard
    QueryPerformanceCounter(&ti);
723 1dce7c3c bellard
    return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
724 b8076a74 bellard
}
725 b8076a74 bellard
726 1dce7c3c bellard
#else
727 90cb9493 bellard
728 1dce7c3c bellard
static int use_rt_clock;
729 1dce7c3c bellard
730 1dce7c3c bellard
static void init_get_clock(void)
731 90cb9493 bellard
{
732 1dce7c3c bellard
    use_rt_clock = 0;
733 60759371 aurel32
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
734 1dce7c3c bellard
    {
735 1dce7c3c bellard
        struct timespec ts;
736 1dce7c3c bellard
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
737 1dce7c3c bellard
            use_rt_clock = 1;
738 1dce7c3c bellard
        }
739 1dce7c3c bellard
    }
740 1dce7c3c bellard
#endif
741 90cb9493 bellard
}
742 90cb9493 bellard
743 1dce7c3c bellard
static int64_t get_clock(void)
744 fdbb4691 bellard
{
745 60759371 aurel32
#if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
746 1dce7c3c bellard
    if (use_rt_clock) {
747 1dce7c3c bellard
        struct timespec ts;
748 1dce7c3c bellard
        clock_gettime(CLOCK_MONOTONIC, &ts);
749 1dce7c3c bellard
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
750 5fafdf24 ths
    } else
751 fdbb4691 bellard
#endif
752 1dce7c3c bellard
    {
753 1dce7c3c bellard
        /* XXX: using gettimeofday leads to problems if the date
754 1dce7c3c bellard
           changes, so it should be avoided. */
755 1dce7c3c bellard
        struct timeval tv;
756 1dce7c3c bellard
        gettimeofday(&tv, NULL);
757 1dce7c3c bellard
        return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
758 1dce7c3c bellard
    }
759 fdbb4691 bellard
}
760 34865134 bellard
#endif
761 34865134 bellard
762 2e70f6ef pbrook
/* Return the virtual CPU time, based on the instruction counter.  */
763 2e70f6ef pbrook
static int64_t cpu_get_icount(void)
764 2e70f6ef pbrook
{
765 2e70f6ef pbrook
    int64_t icount;
766 2e70f6ef pbrook
    CPUState *env = cpu_single_env;;
767 2e70f6ef pbrook
    icount = qemu_icount;
768 2e70f6ef pbrook
    if (env) {
769 2e70f6ef pbrook
        if (!can_do_io(env))
770 2e70f6ef pbrook
            fprintf(stderr, "Bad clock read\n");
771 2e70f6ef pbrook
        icount -= (env->icount_decr.u16.low + env->icount_extra);
772 2e70f6ef pbrook
    }
773 2e70f6ef pbrook
    return qemu_icount_bias + (icount << icount_time_shift);
774 2e70f6ef pbrook
}
775 2e70f6ef pbrook
776 1dce7c3c bellard
/***********************************************************/
777 1dce7c3c bellard
/* guest cycle counter */
778 1dce7c3c bellard
779 eade0f19 bellard
static int64_t cpu_ticks_prev;
780 34865134 bellard
static int64_t cpu_ticks_offset;
781 1dce7c3c bellard
static int64_t cpu_clock_offset;
782 8a7ddc38 bellard
static int cpu_ticks_enabled;
783 34865134 bellard
784 1dce7c3c bellard
/* return the host CPU cycle counter and handle stop/restart */
785 1dce7c3c bellard
int64_t cpu_get_ticks(void)
786 34865134 bellard
{
787 2e70f6ef pbrook
    if (use_icount) {
788 2e70f6ef pbrook
        return cpu_get_icount();
789 2e70f6ef pbrook
    }
790 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
791 8a7ddc38 bellard
        return cpu_ticks_offset;
792 8a7ddc38 bellard
    } else {
793 eade0f19 bellard
        int64_t ticks;
794 eade0f19 bellard
        ticks = cpu_get_real_ticks();
795 eade0f19 bellard
        if (cpu_ticks_prev > ticks) {
796 eade0f19 bellard
            /* Note: non increasing ticks may happen if the host uses
797 eade0f19 bellard
               software suspend */
798 eade0f19 bellard
            cpu_ticks_offset += cpu_ticks_prev - ticks;
799 eade0f19 bellard
        }
800 eade0f19 bellard
        cpu_ticks_prev = ticks;
801 eade0f19 bellard
        return ticks + cpu_ticks_offset;
802 8a7ddc38 bellard
    }
803 34865134 bellard
}
804 34865134 bellard
805 1dce7c3c bellard
/* return the host CPU monotonic timer and handle stop/restart */
806 1dce7c3c bellard
static int64_t cpu_get_clock(void)
807 1dce7c3c bellard
{
808 1dce7c3c bellard
    int64_t ti;
809 1dce7c3c bellard
    if (!cpu_ticks_enabled) {
810 1dce7c3c bellard
        return cpu_clock_offset;
811 1dce7c3c bellard
    } else {
812 1dce7c3c bellard
        ti = get_clock();
813 1dce7c3c bellard
        return ti + cpu_clock_offset;
814 1dce7c3c bellard
    }
815 1dce7c3c bellard
}
816 1dce7c3c bellard
817 34865134 bellard
/* enable cpu_get_ticks() */
818 34865134 bellard
void cpu_enable_ticks(void)
819 34865134 bellard
{
820 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
821 8a7ddc38 bellard
        cpu_ticks_offset -= cpu_get_real_ticks();
822 1dce7c3c bellard
        cpu_clock_offset -= get_clock();
823 8a7ddc38 bellard
        cpu_ticks_enabled = 1;
824 8a7ddc38 bellard
    }
825 34865134 bellard
}
826 34865134 bellard
827 34865134 bellard
/* disable cpu_get_ticks() : the clock is stopped. You must not call
828 34865134 bellard
   cpu_get_ticks() after that.  */
829 34865134 bellard
void cpu_disable_ticks(void)
830 34865134 bellard
{
831 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
832 8a7ddc38 bellard
        cpu_ticks_offset = cpu_get_ticks();
833 1dce7c3c bellard
        cpu_clock_offset = cpu_get_clock();
834 8a7ddc38 bellard
        cpu_ticks_enabled = 0;
835 8a7ddc38 bellard
    }
836 34865134 bellard
}
837 34865134 bellard
838 1dce7c3c bellard
/***********************************************************/
839 1dce7c3c bellard
/* timers */
840 5fafdf24 ths
841 8a7ddc38 bellard
#define QEMU_TIMER_REALTIME 0
842 8a7ddc38 bellard
#define QEMU_TIMER_VIRTUAL  1
843 8a7ddc38 bellard
844 8a7ddc38 bellard
struct QEMUClock {
845 8a7ddc38 bellard
    int type;
846 8a7ddc38 bellard
    /* XXX: add frequency */
847 8a7ddc38 bellard
};
848 8a7ddc38 bellard
849 8a7ddc38 bellard
struct QEMUTimer {
850 8a7ddc38 bellard
    QEMUClock *clock;
851 8a7ddc38 bellard
    int64_t expire_time;
852 8a7ddc38 bellard
    QEMUTimerCB *cb;
853 8a7ddc38 bellard
    void *opaque;
854 8a7ddc38 bellard
    struct QEMUTimer *next;
855 8a7ddc38 bellard
};
856 8a7ddc38 bellard
857 c8994013 ths
struct qemu_alarm_timer {
858 c8994013 ths
    char const *name;
859 efe75411 ths
    unsigned int flags;
860 c8994013 ths
861 c8994013 ths
    int (*start)(struct qemu_alarm_timer *t);
862 c8994013 ths
    void (*stop)(struct qemu_alarm_timer *t);
863 efe75411 ths
    void (*rearm)(struct qemu_alarm_timer *t);
864 c8994013 ths
    void *priv;
865 c8994013 ths
};
866 c8994013 ths
867 efe75411 ths
#define ALARM_FLAG_DYNTICKS  0x1
868 d5d08334 balrog
#define ALARM_FLAG_EXPIRED   0x2
869 efe75411 ths
870 efe75411 ths
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
871 efe75411 ths
{
872 efe75411 ths
    return t->flags & ALARM_FLAG_DYNTICKS;
873 efe75411 ths
}
874 efe75411 ths
875 efe75411 ths
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
876 efe75411 ths
{
877 efe75411 ths
    if (!alarm_has_dynticks(t))
878 efe75411 ths
        return;
879 efe75411 ths
880 efe75411 ths
    t->rearm(t);
881 efe75411 ths
}
882 efe75411 ths
883 efe75411 ths
/* TODO: MIN_TIMER_REARM_US should be optimized */
884 efe75411 ths
#define MIN_TIMER_REARM_US 250
885 efe75411 ths
886 c8994013 ths
static struct qemu_alarm_timer *alarm_timer;
887 8a7ddc38 bellard
888 40c3bac3 bellard
#ifdef _WIN32
889 c8994013 ths
890 c8994013 ths
struct qemu_alarm_win32 {
891 c8994013 ths
    MMRESULT timerId;
892 c8994013 ths
    HANDLE host_alarm;
893 c8994013 ths
    unsigned int period;
894 c8994013 ths
} alarm_win32_data = {0, NULL, -1};
895 c8994013 ths
896 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t);
897 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t);
898 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t);
899 c8994013 ths
900 40c3bac3 bellard
#else
901 c8994013 ths
902 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t);
903 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t);
904 c8994013 ths
905 231c6586 ths
#ifdef __linux__
906 231c6586 ths
907 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t);
908 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
909 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
910 efe75411 ths
911 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t);
912 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t);
913 c40ec5a9 ths
914 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t);
915 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t);
916 c8994013 ths
917 efe75411 ths
#endif /* __linux__ */
918 8a7ddc38 bellard
919 c8994013 ths
#endif /* _WIN32 */
920 c8994013 ths
921 2e70f6ef pbrook
/* Correlation between real and virtual time is always going to be
922 bf20dc07 ths
   fairly approximate, so ignore small variation.
923 2e70f6ef pbrook
   When the guest is idle real and virtual time will be aligned in
924 2e70f6ef pbrook
   the IO wait loop.  */
925 2e70f6ef pbrook
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
926 2e70f6ef pbrook
927 2e70f6ef pbrook
static void icount_adjust(void)
928 2e70f6ef pbrook
{
929 2e70f6ef pbrook
    int64_t cur_time;
930 2e70f6ef pbrook
    int64_t cur_icount;
931 2e70f6ef pbrook
    int64_t delta;
932 2e70f6ef pbrook
    static int64_t last_delta;
933 2e70f6ef pbrook
    /* If the VM is not running, then do nothing.  */
934 2e70f6ef pbrook
    if (!vm_running)
935 2e70f6ef pbrook
        return;
936 2e70f6ef pbrook
937 2e70f6ef pbrook
    cur_time = cpu_get_clock();
938 2e70f6ef pbrook
    cur_icount = qemu_get_clock(vm_clock);
939 2e70f6ef pbrook
    delta = cur_icount - cur_time;
940 2e70f6ef pbrook
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
941 2e70f6ef pbrook
    if (delta > 0
942 2e70f6ef pbrook
        && last_delta + ICOUNT_WOBBLE < delta * 2
943 2e70f6ef pbrook
        && icount_time_shift > 0) {
944 2e70f6ef pbrook
        /* The guest is getting too far ahead.  Slow time down.  */
945 2e70f6ef pbrook
        icount_time_shift--;
946 2e70f6ef pbrook
    }
947 2e70f6ef pbrook
    if (delta < 0
948 2e70f6ef pbrook
        && last_delta - ICOUNT_WOBBLE > delta * 2
949 2e70f6ef pbrook
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
950 2e70f6ef pbrook
        /* The guest is getting too far behind.  Speed time up.  */
951 2e70f6ef pbrook
        icount_time_shift++;
952 2e70f6ef pbrook
    }
953 2e70f6ef pbrook
    last_delta = delta;
954 2e70f6ef pbrook
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
955 2e70f6ef pbrook
}
956 2e70f6ef pbrook
957 2e70f6ef pbrook
static void icount_adjust_rt(void * opaque)
958 2e70f6ef pbrook
{
959 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
960 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
961 2e70f6ef pbrook
    icount_adjust();
962 2e70f6ef pbrook
}
963 2e70f6ef pbrook
964 2e70f6ef pbrook
static void icount_adjust_vm(void * opaque)
965 2e70f6ef pbrook
{
966 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
967 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
968 2e70f6ef pbrook
    icount_adjust();
969 2e70f6ef pbrook
}
970 2e70f6ef pbrook
971 2e70f6ef pbrook
static void init_icount_adjust(void)
972 2e70f6ef pbrook
{
973 2e70f6ef pbrook
    /* Have both realtime and virtual time triggers for speed adjustment.
974 2e70f6ef pbrook
       The realtime trigger catches emulated time passing too slowly,
975 2e70f6ef pbrook
       the virtual time trigger catches emulated time passing too fast.
976 2e70f6ef pbrook
       Realtime triggers occur even when idle, so use them less frequently
977 2e70f6ef pbrook
       than VM triggers.  */
978 2e70f6ef pbrook
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
979 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
980 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
981 2e70f6ef pbrook
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
982 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
983 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
984 2e70f6ef pbrook
}
985 2e70f6ef pbrook
986 c8994013 ths
static struct qemu_alarm_timer alarm_timers[] = {
987 efe75411 ths
#ifndef _WIN32
988 231c6586 ths
#ifdef __linux__
989 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
990 efe75411 ths
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
991 c40ec5a9 ths
    /* HPET - if available - is preferred */
992 efe75411 ths
    {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
993 c40ec5a9 ths
    /* ...otherwise try RTC */
994 efe75411 ths
    {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
995 c8994013 ths
#endif
996 efe75411 ths
    {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
997 c8994013 ths
#else
998 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
999 efe75411 ths
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1000 efe75411 ths
    {"win32", 0, win32_start_timer,
1001 efe75411 ths
     win32_stop_timer, NULL, &alarm_win32_data},
1002 c8994013 ths
#endif
1003 c8994013 ths
    {NULL, }
1004 c8994013 ths
};
1005 c8994013 ths
1006 3f47aa8c blueswir1
static void show_available_alarms(void)
1007 f3dcfada ths
{
1008 f3dcfada ths
    int i;
1009 f3dcfada ths
1010 f3dcfada ths
    printf("Available alarm timers, in order of precedence:\n");
1011 f3dcfada ths
    for (i = 0; alarm_timers[i].name; i++)
1012 f3dcfada ths
        printf("%s\n", alarm_timers[i].name);
1013 f3dcfada ths
}
1014 f3dcfada ths
1015 f3dcfada ths
static void configure_alarms(char const *opt)
1016 f3dcfada ths
{
1017 f3dcfada ths
    int i;
1018 f3dcfada ths
    int cur = 0;
1019 f3dcfada ths
    int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1020 f3dcfada ths
    char *arg;
1021 f3dcfada ths
    char *name;
1022 2e70f6ef pbrook
    struct qemu_alarm_timer tmp;
1023 f3dcfada ths
1024 3adda04c aurel32
    if (!strcmp(opt, "?")) {
1025 f3dcfada ths
        show_available_alarms();
1026 f3dcfada ths
        exit(0);
1027 f3dcfada ths
    }
1028 f3dcfada ths
1029 f3dcfada ths
    arg = strdup(opt);
1030 f3dcfada ths
1031 f3dcfada ths
    /* Reorder the array */
1032 f3dcfada ths
    name = strtok(arg, ",");
1033 f3dcfada ths
    while (name) {
1034 e2b577e5 balrog
        for (i = 0; i < count && alarm_timers[i].name; i++) {
1035 f3dcfada ths
            if (!strcmp(alarm_timers[i].name, name))
1036 f3dcfada ths
                break;
1037 f3dcfada ths
        }
1038 f3dcfada ths
1039 f3dcfada ths
        if (i == count) {
1040 f3dcfada ths
            fprintf(stderr, "Unknown clock %s\n", name);
1041 f3dcfada ths
            goto next;
1042 f3dcfada ths
        }
1043 f3dcfada ths
1044 f3dcfada ths
        if (i < cur)
1045 f3dcfada ths
            /* Ignore */
1046 f3dcfada ths
            goto next;
1047 f3dcfada ths
1048 f3dcfada ths
        /* Swap */
1049 f3dcfada ths
        tmp = alarm_timers[i];
1050 f3dcfada ths
        alarm_timers[i] = alarm_timers[cur];
1051 f3dcfada ths
        alarm_timers[cur] = tmp;
1052 f3dcfada ths
1053 f3dcfada ths
        cur++;
1054 f3dcfada ths
next:
1055 f3dcfada ths
        name = strtok(NULL, ",");
1056 f3dcfada ths
    }
1057 f3dcfada ths
1058 f3dcfada ths
    free(arg);
1059 f3dcfada ths
1060 f3dcfada ths
    if (cur) {
1061 2e70f6ef pbrook
        /* Disable remaining timers */
1062 f3dcfada ths
        for (i = cur; i < count; i++)
1063 f3dcfada ths
            alarm_timers[i].name = NULL;
1064 3adda04c aurel32
    } else {
1065 3adda04c aurel32
        show_available_alarms();
1066 3adda04c aurel32
        exit(1);
1067 f3dcfada ths
    }
1068 f3dcfada ths
}
1069 f3dcfada ths
1070 c8994013 ths
QEMUClock *rt_clock;
1071 c8994013 ths
QEMUClock *vm_clock;
1072 c8994013 ths
1073 c8994013 ths
static QEMUTimer *active_timers[2];
1074 c8994013 ths
1075 9596ebb7 pbrook
static QEMUClock *qemu_new_clock(int type)
1076 8a7ddc38 bellard
{
1077 8a7ddc38 bellard
    QEMUClock *clock;
1078 8a7ddc38 bellard
    clock = qemu_mallocz(sizeof(QEMUClock));
1079 8a7ddc38 bellard
    if (!clock)
1080 8a7ddc38 bellard
        return NULL;
1081 8a7ddc38 bellard
    clock->type = type;
1082 8a7ddc38 bellard
    return clock;
1083 8a7ddc38 bellard
}
1084 8a7ddc38 bellard
1085 8a7ddc38 bellard
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1086 8a7ddc38 bellard
{
1087 8a7ddc38 bellard
    QEMUTimer *ts;
1088 8a7ddc38 bellard
1089 8a7ddc38 bellard
    ts = qemu_mallocz(sizeof(QEMUTimer));
1090 8a7ddc38 bellard
    ts->clock = clock;
1091 8a7ddc38 bellard
    ts->cb = cb;
1092 8a7ddc38 bellard
    ts->opaque = opaque;
1093 8a7ddc38 bellard
    return ts;
1094 8a7ddc38 bellard
}
1095 8a7ddc38 bellard
1096 8a7ddc38 bellard
void qemu_free_timer(QEMUTimer *ts)
1097 8a7ddc38 bellard
{
1098 8a7ddc38 bellard
    qemu_free(ts);
1099 8a7ddc38 bellard
}
1100 8a7ddc38 bellard
1101 8a7ddc38 bellard
/* stop a timer, but do not dealloc it */
1102 8a7ddc38 bellard
void qemu_del_timer(QEMUTimer *ts)
1103 8a7ddc38 bellard
{
1104 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1105 8a7ddc38 bellard
1106 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1107 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1108 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1109 8a7ddc38 bellard
    for(;;) {
1110 8a7ddc38 bellard
        t = *pt;
1111 8a7ddc38 bellard
        if (!t)
1112 8a7ddc38 bellard
            break;
1113 8a7ddc38 bellard
        if (t == ts) {
1114 8a7ddc38 bellard
            *pt = t->next;
1115 8a7ddc38 bellard
            break;
1116 8a7ddc38 bellard
        }
1117 8a7ddc38 bellard
        pt = &t->next;
1118 8a7ddc38 bellard
    }
1119 8a7ddc38 bellard
}
1120 8a7ddc38 bellard
1121 8a7ddc38 bellard
/* modify the current timer so that it will be fired when current_time
1122 8a7ddc38 bellard
   >= expire_time. The corresponding callback will be called. */
1123 8a7ddc38 bellard
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1124 8a7ddc38 bellard
{
1125 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1126 8a7ddc38 bellard
1127 8a7ddc38 bellard
    qemu_del_timer(ts);
1128 8a7ddc38 bellard
1129 8a7ddc38 bellard
    /* add the timer in the sorted list */
1130 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1131 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1132 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1133 8a7ddc38 bellard
    for(;;) {
1134 8a7ddc38 bellard
        t = *pt;
1135 8a7ddc38 bellard
        if (!t)
1136 8a7ddc38 bellard
            break;
1137 5fafdf24 ths
        if (t->expire_time > expire_time)
1138 8a7ddc38 bellard
            break;
1139 8a7ddc38 bellard
        pt = &t->next;
1140 8a7ddc38 bellard
    }
1141 8a7ddc38 bellard
    ts->expire_time = expire_time;
1142 8a7ddc38 bellard
    ts->next = *pt;
1143 8a7ddc38 bellard
    *pt = ts;
1144 d5d08334 balrog
1145 d5d08334 balrog
    /* Rearm if necessary  */
1146 2e70f6ef pbrook
    if (pt == &active_timers[ts->clock->type]) {
1147 2e70f6ef pbrook
        if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1148 2e70f6ef pbrook
            qemu_rearm_alarm_timer(alarm_timer);
1149 2e70f6ef pbrook
        }
1150 2e70f6ef pbrook
        /* Interrupt execution to force deadline recalculation.  */
1151 2e70f6ef pbrook
        if (use_icount && cpu_single_env) {
1152 2e70f6ef pbrook
            cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1153 2e70f6ef pbrook
        }
1154 2e70f6ef pbrook
    }
1155 8a7ddc38 bellard
}
1156 8a7ddc38 bellard
1157 8a7ddc38 bellard
int qemu_timer_pending(QEMUTimer *ts)
1158 8a7ddc38 bellard
{
1159 8a7ddc38 bellard
    QEMUTimer *t;
1160 8a7ddc38 bellard
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1161 8a7ddc38 bellard
        if (t == ts)
1162 8a7ddc38 bellard
            return 1;
1163 8a7ddc38 bellard
    }
1164 8a7ddc38 bellard
    return 0;
1165 8a7ddc38 bellard
}
1166 8a7ddc38 bellard
1167 8a7ddc38 bellard
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1168 8a7ddc38 bellard
{
1169 8a7ddc38 bellard
    if (!timer_head)
1170 8a7ddc38 bellard
        return 0;
1171 8a7ddc38 bellard
    return (timer_head->expire_time <= current_time);
1172 8a7ddc38 bellard
}
1173 8a7ddc38 bellard
1174 8a7ddc38 bellard
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1175 8a7ddc38 bellard
{
1176 8a7ddc38 bellard
    QEMUTimer *ts;
1177 3b46e624 ths
1178 8a7ddc38 bellard
    for(;;) {
1179 8a7ddc38 bellard
        ts = *ptimer_head;
1180 e95c8d51 bellard
        if (!ts || ts->expire_time > current_time)
1181 8a7ddc38 bellard
            break;
1182 8a7ddc38 bellard
        /* remove timer from the list before calling the callback */
1183 8a7ddc38 bellard
        *ptimer_head = ts->next;
1184 8a7ddc38 bellard
        ts->next = NULL;
1185 3b46e624 ths
1186 8a7ddc38 bellard
        /* run the callback (the timer list can be modified) */
1187 8a7ddc38 bellard
        ts->cb(ts->opaque);
1188 8a7ddc38 bellard
    }
1189 8a7ddc38 bellard
}
1190 8a7ddc38 bellard
1191 8a7ddc38 bellard
int64_t qemu_get_clock(QEMUClock *clock)
1192 8a7ddc38 bellard
{
1193 8a7ddc38 bellard
    switch(clock->type) {
1194 8a7ddc38 bellard
    case QEMU_TIMER_REALTIME:
1195 1dce7c3c bellard
        return get_clock() / 1000000;
1196 8a7ddc38 bellard
    default:
1197 8a7ddc38 bellard
    case QEMU_TIMER_VIRTUAL:
1198 2e70f6ef pbrook
        if (use_icount) {
1199 2e70f6ef pbrook
            return cpu_get_icount();
1200 2e70f6ef pbrook
        } else {
1201 2e70f6ef pbrook
            return cpu_get_clock();
1202 2e70f6ef pbrook
        }
1203 8a7ddc38 bellard
    }
1204 8a7ddc38 bellard
}
1205 8a7ddc38 bellard
1206 1dce7c3c bellard
static void init_timers(void)
1207 1dce7c3c bellard
{
1208 1dce7c3c bellard
    init_get_clock();
1209 1dce7c3c bellard
    ticks_per_sec = QEMU_TIMER_BASE;
1210 1dce7c3c bellard
    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1211 1dce7c3c bellard
    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1212 1dce7c3c bellard
}
1213 1dce7c3c bellard
1214 8a7ddc38 bellard
/* save a timer */
1215 8a7ddc38 bellard
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1216 8a7ddc38 bellard
{
1217 8a7ddc38 bellard
    uint64_t expire_time;
1218 8a7ddc38 bellard
1219 8a7ddc38 bellard
    if (qemu_timer_pending(ts)) {
1220 8a7ddc38 bellard
        expire_time = ts->expire_time;
1221 8a7ddc38 bellard
    } else {
1222 8a7ddc38 bellard
        expire_time = -1;
1223 8a7ddc38 bellard
    }
1224 8a7ddc38 bellard
    qemu_put_be64(f, expire_time);
1225 8a7ddc38 bellard
}
1226 8a7ddc38 bellard
1227 8a7ddc38 bellard
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1228 8a7ddc38 bellard
{
1229 8a7ddc38 bellard
    uint64_t expire_time;
1230 8a7ddc38 bellard
1231 8a7ddc38 bellard
    expire_time = qemu_get_be64(f);
1232 8a7ddc38 bellard
    if (expire_time != -1) {
1233 8a7ddc38 bellard
        qemu_mod_timer(ts, expire_time);
1234 8a7ddc38 bellard
    } else {
1235 8a7ddc38 bellard
        qemu_del_timer(ts);
1236 8a7ddc38 bellard
    }
1237 8a7ddc38 bellard
}
1238 8a7ddc38 bellard
1239 8a7ddc38 bellard
static void timer_save(QEMUFile *f, void *opaque)
1240 8a7ddc38 bellard
{
1241 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1242 8a7ddc38 bellard
        hw_error("cannot save state if virtual timers are running");
1243 8a7ddc38 bellard
    }
1244 bee8d684 ths
    qemu_put_be64(f, cpu_ticks_offset);
1245 bee8d684 ths
    qemu_put_be64(f, ticks_per_sec);
1246 bee8d684 ths
    qemu_put_be64(f, cpu_clock_offset);
1247 8a7ddc38 bellard
}
1248 8a7ddc38 bellard
1249 8a7ddc38 bellard
static int timer_load(QEMUFile *f, void *opaque, int version_id)
1250 8a7ddc38 bellard
{
1251 c88676f8 bellard
    if (version_id != 1 && version_id != 2)
1252 8a7ddc38 bellard
        return -EINVAL;
1253 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1254 8a7ddc38 bellard
        return -EINVAL;
1255 8a7ddc38 bellard
    }
1256 bee8d684 ths
    cpu_ticks_offset=qemu_get_be64(f);
1257 bee8d684 ths
    ticks_per_sec=qemu_get_be64(f);
1258 c88676f8 bellard
    if (version_id == 2) {
1259 bee8d684 ths
        cpu_clock_offset=qemu_get_be64(f);
1260 c88676f8 bellard
    }
1261 8a7ddc38 bellard
    return 0;
1262 8a7ddc38 bellard
}
1263 8a7ddc38 bellard
1264 67b915a5 bellard
#ifdef _WIN32
1265 5fafdf24 ths
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1266 67b915a5 bellard
                                 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1267 67b915a5 bellard
#else
1268 8a7ddc38 bellard
static void host_alarm_handler(int host_signum)
1269 67b915a5 bellard
#endif
1270 8a7ddc38 bellard
{
1271 02ba45c5 bellard
#if 0
1272 02ba45c5 bellard
#define DISP_FREQ 1000
1273 02ba45c5 bellard
    {
1274 02ba45c5 bellard
        static int64_t delta_min = INT64_MAX;
1275 02ba45c5 bellard
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
1276 02ba45c5 bellard
        static int count;
1277 02ba45c5 bellard
        ti = qemu_get_clock(vm_clock);
1278 02ba45c5 bellard
        if (last_clock != 0) {
1279 02ba45c5 bellard
            delta = ti - last_clock;
1280 02ba45c5 bellard
            if (delta < delta_min)
1281 02ba45c5 bellard
                delta_min = delta;
1282 02ba45c5 bellard
            if (delta > delta_max)
1283 02ba45c5 bellard
                delta_max = delta;
1284 02ba45c5 bellard
            delta_cum += delta;
1285 02ba45c5 bellard
            if (++count == DISP_FREQ) {
1286 26a76461 bellard
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1287 02ba45c5 bellard
                       muldiv64(delta_min, 1000000, ticks_per_sec),
1288 02ba45c5 bellard
                       muldiv64(delta_max, 1000000, ticks_per_sec),
1289 02ba45c5 bellard
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1290 02ba45c5 bellard
                       (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1291 02ba45c5 bellard
                count = 0;
1292 02ba45c5 bellard
                delta_min = INT64_MAX;
1293 02ba45c5 bellard
                delta_max = 0;
1294 02ba45c5 bellard
                delta_cum = 0;
1295 02ba45c5 bellard
            }
1296 02ba45c5 bellard
        }
1297 02ba45c5 bellard
        last_clock = ti;
1298 02ba45c5 bellard
    }
1299 02ba45c5 bellard
#endif
1300 efe75411 ths
    if (alarm_has_dynticks(alarm_timer) ||
1301 2e70f6ef pbrook
        (!use_icount &&
1302 2e70f6ef pbrook
            qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1303 2e70f6ef pbrook
                               qemu_get_clock(vm_clock))) ||
1304 8a7ddc38 bellard
        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1305 8a7ddc38 bellard
                           qemu_get_clock(rt_clock))) {
1306 06d9f2f7 bellard
#ifdef _WIN32
1307 c8994013 ths
        struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1308 c8994013 ths
        SetEvent(data->host_alarm);
1309 06d9f2f7 bellard
#endif
1310 ee5605e5 balrog
        CPUState *env = next_cpu;
1311 ee5605e5 balrog
1312 d5d08334 balrog
        alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1313 d5d08334 balrog
1314 4f8eb8da balrog
        if (env) {
1315 4f8eb8da balrog
            /* stop the currently executing cpu because a timer occured */
1316 4f8eb8da balrog
            cpu_interrupt(env, CPU_INTERRUPT_EXIT);
1317 a332e112 bellard
#ifdef USE_KQEMU
1318 4f8eb8da balrog
            if (env->kqemu_enabled) {
1319 4f8eb8da balrog
                kqemu_cpu_interrupt(env);
1320 4f8eb8da balrog
            }
1321 ee5605e5 balrog
#endif
1322 4f8eb8da balrog
        }
1323 ee5605e5 balrog
        event_pending = 1;
1324 8a7ddc38 bellard
    }
1325 8a7ddc38 bellard
}
1326 8a7ddc38 bellard
1327 2e70f6ef pbrook
static int64_t qemu_next_deadline(void)
1328 efe75411 ths
{
1329 2e70f6ef pbrook
    int64_t delta;
1330 efe75411 ths
1331 efe75411 ths
    if (active_timers[QEMU_TIMER_VIRTUAL]) {
1332 2e70f6ef pbrook
        delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1333 2e70f6ef pbrook
                     qemu_get_clock(vm_clock);
1334 2e70f6ef pbrook
    } else {
1335 2e70f6ef pbrook
        /* To avoid problems with overflow limit this to 2^32.  */
1336 2e70f6ef pbrook
        delta = INT32_MAX;
1337 efe75411 ths
    }
1338 efe75411 ths
1339 2e70f6ef pbrook
    if (delta < 0)
1340 2e70f6ef pbrook
        delta = 0;
1341 efe75411 ths
1342 2e70f6ef pbrook
    return delta;
1343 2e70f6ef pbrook
}
1344 2e70f6ef pbrook
1345 8632fb9a blueswir1
#if defined(__linux__) || defined(_WIN32)
1346 2e70f6ef pbrook
static uint64_t qemu_next_deadline_dyntick(void)
1347 2e70f6ef pbrook
{
1348 2e70f6ef pbrook
    int64_t delta;
1349 2e70f6ef pbrook
    int64_t rtdelta;
1350 2e70f6ef pbrook
1351 2e70f6ef pbrook
    if (use_icount)
1352 2e70f6ef pbrook
        delta = INT32_MAX;
1353 2e70f6ef pbrook
    else
1354 2e70f6ef pbrook
        delta = (qemu_next_deadline() + 999) / 1000;
1355 2e70f6ef pbrook
1356 2e70f6ef pbrook
    if (active_timers[QEMU_TIMER_REALTIME]) {
1357 2e70f6ef pbrook
        rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1358 2e70f6ef pbrook
                 qemu_get_clock(rt_clock))*1000;
1359 2e70f6ef pbrook
        if (rtdelta < delta)
1360 2e70f6ef pbrook
            delta = rtdelta;
1361 2e70f6ef pbrook
    }
1362 2e70f6ef pbrook
1363 2e70f6ef pbrook
    if (delta < MIN_TIMER_REARM_US)
1364 2e70f6ef pbrook
        delta = MIN_TIMER_REARM_US;
1365 2e70f6ef pbrook
1366 2e70f6ef pbrook
    return delta;
1367 efe75411 ths
}
1368 8632fb9a blueswir1
#endif
1369 efe75411 ths
1370 fd872598 bellard
#ifndef _WIN32
1371 fd872598 bellard
1372 829309c7 bellard
#if defined(__linux__)
1373 829309c7 bellard
1374 fd872598 bellard
#define RTC_FREQ 1024
1375 fd872598 bellard
1376 c8994013 ths
static void enable_sigio_timer(int fd)
1377 c8994013 ths
{
1378 c8994013 ths
    struct sigaction act;
1379 c8994013 ths
1380 c8994013 ths
    /* timer signal */
1381 c8994013 ths
    sigfillset(&act.sa_mask);
1382 c8994013 ths
    act.sa_flags = 0;
1383 c8994013 ths
    act.sa_handler = host_alarm_handler;
1384 c8994013 ths
1385 c8994013 ths
    sigaction(SIGIO, &act, NULL);
1386 c8994013 ths
    fcntl(fd, F_SETFL, O_ASYNC);
1387 c8994013 ths
    fcntl(fd, F_SETOWN, getpid());
1388 c8994013 ths
}
1389 829309c7 bellard
1390 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t)
1391 c40ec5a9 ths
{
1392 c40ec5a9 ths
    struct hpet_info info;
1393 c40ec5a9 ths
    int r, fd;
1394 c40ec5a9 ths
1395 c40ec5a9 ths
    fd = open("/dev/hpet", O_RDONLY);
1396 c40ec5a9 ths
    if (fd < 0)
1397 c40ec5a9 ths
        return -1;
1398 c40ec5a9 ths
1399 c40ec5a9 ths
    /* Set frequency */
1400 c40ec5a9 ths
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1401 c40ec5a9 ths
    if (r < 0) {
1402 c40ec5a9 ths
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1403 c40ec5a9 ths
                "error, but for better emulation accuracy type:\n"
1404 c40ec5a9 ths
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1405 c40ec5a9 ths
        goto fail;
1406 c40ec5a9 ths
    }
1407 c40ec5a9 ths
1408 c40ec5a9 ths
    /* Check capabilities */
1409 c40ec5a9 ths
    r = ioctl(fd, HPET_INFO, &info);
1410 c40ec5a9 ths
    if (r < 0)
1411 c40ec5a9 ths
        goto fail;
1412 c40ec5a9 ths
1413 c40ec5a9 ths
    /* Enable periodic mode */
1414 c40ec5a9 ths
    r = ioctl(fd, HPET_EPI, 0);
1415 c40ec5a9 ths
    if (info.hi_flags && (r < 0))
1416 c40ec5a9 ths
        goto fail;
1417 c40ec5a9 ths
1418 c40ec5a9 ths
    /* Enable interrupt */
1419 c40ec5a9 ths
    r = ioctl(fd, HPET_IE_ON, 0);
1420 c40ec5a9 ths
    if (r < 0)
1421 c40ec5a9 ths
        goto fail;
1422 c40ec5a9 ths
1423 c40ec5a9 ths
    enable_sigio_timer(fd);
1424 fcdc2129 pbrook
    t->priv = (void *)(long)fd;
1425 c40ec5a9 ths
1426 c40ec5a9 ths
    return 0;
1427 c40ec5a9 ths
fail:
1428 c40ec5a9 ths
    close(fd);
1429 c40ec5a9 ths
    return -1;
1430 c40ec5a9 ths
}
1431 c40ec5a9 ths
1432 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t)
1433 c40ec5a9 ths
{
1434 fcdc2129 pbrook
    int fd = (long)t->priv;
1435 c40ec5a9 ths
1436 c40ec5a9 ths
    close(fd);
1437 c40ec5a9 ths
}
1438 c40ec5a9 ths
1439 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t)
1440 fd872598 bellard
{
1441 c8994013 ths
    int rtc_fd;
1442 b5a23ad4 balrog
    unsigned long current_rtc_freq = 0;
1443 c8994013 ths
1444 aeb30be6 balrog
    TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1445 fd872598 bellard
    if (rtc_fd < 0)
1446 fd872598 bellard
        return -1;
1447 b5a23ad4 balrog
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1448 b5a23ad4 balrog
    if (current_rtc_freq != RTC_FREQ &&
1449 b5a23ad4 balrog
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1450 fd872598 bellard
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1451 fd872598 bellard
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1452 fd872598 bellard
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1453 fd872598 bellard
        goto fail;
1454 fd872598 bellard
    }
1455 fd872598 bellard
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1456 fd872598 bellard
    fail:
1457 fd872598 bellard
        close(rtc_fd);
1458 fd872598 bellard
        return -1;
1459 fd872598 bellard
    }
1460 c8994013 ths
1461 c8994013 ths
    enable_sigio_timer(rtc_fd);
1462 c8994013 ths
1463 fcdc2129 pbrook
    t->priv = (void *)(long)rtc_fd;
1464 c8994013 ths
1465 fd872598 bellard
    return 0;
1466 fd872598 bellard
}
1467 fd872598 bellard
1468 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t)
1469 829309c7 bellard
{
1470 fcdc2129 pbrook
    int rtc_fd = (long)t->priv;
1471 c8994013 ths
1472 c8994013 ths
    close(rtc_fd);
1473 829309c7 bellard
}
1474 829309c7 bellard
1475 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t)
1476 efe75411 ths
{
1477 efe75411 ths
    struct sigevent ev;
1478 efe75411 ths
    timer_t host_timer;
1479 efe75411 ths
    struct sigaction act;
1480 efe75411 ths
1481 efe75411 ths
    sigfillset(&act.sa_mask);
1482 efe75411 ths
    act.sa_flags = 0;
1483 efe75411 ths
    act.sa_handler = host_alarm_handler;
1484 efe75411 ths
1485 efe75411 ths
    sigaction(SIGALRM, &act, NULL);
1486 efe75411 ths
1487 efe75411 ths
    ev.sigev_value.sival_int = 0;
1488 efe75411 ths
    ev.sigev_notify = SIGEV_SIGNAL;
1489 efe75411 ths
    ev.sigev_signo = SIGALRM;
1490 efe75411 ths
1491 efe75411 ths
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1492 efe75411 ths
        perror("timer_create");
1493 efe75411 ths
1494 efe75411 ths
        /* disable dynticks */
1495 efe75411 ths
        fprintf(stderr, "Dynamic Ticks disabled\n");
1496 efe75411 ths
1497 efe75411 ths
        return -1;
1498 efe75411 ths
    }
1499 efe75411 ths
1500 efe75411 ths
    t->priv = (void *)host_timer;
1501 efe75411 ths
1502 efe75411 ths
    return 0;
1503 efe75411 ths
}
1504 efe75411 ths
1505 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1506 efe75411 ths
{
1507 efe75411 ths
    timer_t host_timer = (timer_t)t->priv;
1508 efe75411 ths
1509 efe75411 ths
    timer_delete(host_timer);
1510 efe75411 ths
}
1511 efe75411 ths
1512 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1513 efe75411 ths
{
1514 efe75411 ths
    timer_t host_timer = (timer_t)t->priv;
1515 efe75411 ths
    struct itimerspec timeout;
1516 efe75411 ths
    int64_t nearest_delta_us = INT64_MAX;
1517 efe75411 ths
    int64_t current_us;
1518 efe75411 ths
1519 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1520 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1521 d5d08334 balrog
        return;
1522 efe75411 ths
1523 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1524 efe75411 ths
1525 efe75411 ths
    /* check whether a timer is already running */
1526 efe75411 ths
    if (timer_gettime(host_timer, &timeout)) {
1527 efe75411 ths
        perror("gettime");
1528 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1529 efe75411 ths
        exit(1);
1530 efe75411 ths
    }
1531 efe75411 ths
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1532 efe75411 ths
    if (current_us && current_us <= nearest_delta_us)
1533 efe75411 ths
        return;
1534 efe75411 ths
1535 efe75411 ths
    timeout.it_interval.tv_sec = 0;
1536 efe75411 ths
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1537 efe75411 ths
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1538 efe75411 ths
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1539 efe75411 ths
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1540 efe75411 ths
        perror("settime");
1541 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1542 efe75411 ths
        exit(1);
1543 efe75411 ths
    }
1544 efe75411 ths
}
1545 efe75411 ths
1546 70744b3a ths
#endif /* defined(__linux__) */
1547 231c6586 ths
1548 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t)
1549 c8994013 ths
{
1550 c8994013 ths
    struct sigaction act;
1551 c8994013 ths
    struct itimerval itv;
1552 c8994013 ths
    int err;
1553 c8994013 ths
1554 c8994013 ths
    /* timer signal */
1555 c8994013 ths
    sigfillset(&act.sa_mask);
1556 c8994013 ths
    act.sa_flags = 0;
1557 c8994013 ths
    act.sa_handler = host_alarm_handler;
1558 c8994013 ths
1559 c8994013 ths
    sigaction(SIGALRM, &act, NULL);
1560 c8994013 ths
1561 c8994013 ths
    itv.it_interval.tv_sec = 0;
1562 c8994013 ths
    /* for i386 kernel 2.6 to get 1 ms */
1563 c8994013 ths
    itv.it_interval.tv_usec = 999;
1564 c8994013 ths
    itv.it_value.tv_sec = 0;
1565 c8994013 ths
    itv.it_value.tv_usec = 10 * 1000;
1566 c8994013 ths
1567 c8994013 ths
    err = setitimer(ITIMER_REAL, &itv, NULL);
1568 c8994013 ths
    if (err)
1569 c8994013 ths
        return -1;
1570 c8994013 ths
1571 c8994013 ths
    return 0;
1572 c8994013 ths
}
1573 c8994013 ths
1574 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t)
1575 c8994013 ths
{
1576 c8994013 ths
    struct itimerval itv;
1577 c8994013 ths
1578 c8994013 ths
    memset(&itv, 0, sizeof(itv));
1579 c8994013 ths
    setitimer(ITIMER_REAL, &itv, NULL);
1580 c8994013 ths
}
1581 c8994013 ths
1582 829309c7 bellard
#endif /* !defined(_WIN32) */
1583 fd872598 bellard
1584 c8994013 ths
#ifdef _WIN32
1585 c8994013 ths
1586 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t)
1587 c8994013 ths
{
1588 c8994013 ths
    TIMECAPS tc;
1589 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1590 efe75411 ths
    UINT flags;
1591 c8994013 ths
1592 c8994013 ths
    data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1593 c8994013 ths
    if (!data->host_alarm) {
1594 c8994013 ths
        perror("Failed CreateEvent");
1595 c396a7f0 ths
        return -1;
1596 c8994013 ths
    }
1597 c8994013 ths
1598 c8994013 ths
    memset(&tc, 0, sizeof(tc));
1599 c8994013 ths
    timeGetDevCaps(&tc, sizeof(tc));
1600 c8994013 ths
1601 c8994013 ths
    if (data->period < tc.wPeriodMin)
1602 c8994013 ths
        data->period = tc.wPeriodMin;
1603 c8994013 ths
1604 c8994013 ths
    timeBeginPeriod(data->period);
1605 c8994013 ths
1606 efe75411 ths
    flags = TIME_CALLBACK_FUNCTION;
1607 efe75411 ths
    if (alarm_has_dynticks(t))
1608 efe75411 ths
        flags |= TIME_ONESHOT;
1609 efe75411 ths
    else
1610 efe75411 ths
        flags |= TIME_PERIODIC;
1611 efe75411 ths
1612 c8994013 ths
    data->timerId = timeSetEvent(1,         // interval (ms)
1613 c8994013 ths
                        data->period,       // resolution
1614 c8994013 ths
                        host_alarm_handler, // function
1615 c8994013 ths
                        (DWORD)t,           // parameter
1616 efe75411 ths
                        flags);
1617 c8994013 ths
1618 c8994013 ths
    if (!data->timerId) {
1619 c8994013 ths
        perror("Failed to initialize win32 alarm timer");
1620 c8994013 ths
1621 c8994013 ths
        timeEndPeriod(data->period);
1622 c8994013 ths
        CloseHandle(data->host_alarm);
1623 c8994013 ths
        return -1;
1624 c8994013 ths
    }
1625 c8994013 ths
1626 c8994013 ths
    qemu_add_wait_object(data->host_alarm, NULL, NULL);
1627 c8994013 ths
1628 c8994013 ths
    return 0;
1629 c8994013 ths
}
1630 c8994013 ths
1631 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t)
1632 c8994013 ths
{
1633 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1634 c8994013 ths
1635 c8994013 ths
    timeKillEvent(data->timerId);
1636 c8994013 ths
    timeEndPeriod(data->period);
1637 c8994013 ths
1638 c8994013 ths
    CloseHandle(data->host_alarm);
1639 c8994013 ths
}
1640 c8994013 ths
1641 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1642 efe75411 ths
{
1643 efe75411 ths
    struct qemu_alarm_win32 *data = t->priv;
1644 efe75411 ths
    uint64_t nearest_delta_us;
1645 efe75411 ths
1646 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1647 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1648 d5d08334 balrog
        return;
1649 efe75411 ths
1650 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1651 efe75411 ths
    nearest_delta_us /= 1000;
1652 efe75411 ths
1653 efe75411 ths
    timeKillEvent(data->timerId);
1654 efe75411 ths
1655 efe75411 ths
    data->timerId = timeSetEvent(1,
1656 efe75411 ths
                        data->period,
1657 efe75411 ths
                        host_alarm_handler,
1658 efe75411 ths
                        (DWORD)t,
1659 efe75411 ths
                        TIME_ONESHOT | TIME_PERIODIC);
1660 efe75411 ths
1661 efe75411 ths
    if (!data->timerId) {
1662 efe75411 ths
        perror("Failed to re-arm win32 alarm timer");
1663 efe75411 ths
1664 efe75411 ths
        timeEndPeriod(data->period);
1665 efe75411 ths
        CloseHandle(data->host_alarm);
1666 efe75411 ths
        exit(1);
1667 efe75411 ths
    }
1668 efe75411 ths
}
1669 efe75411 ths
1670 c8994013 ths
#endif /* _WIN32 */
1671 c8994013 ths
1672 1dce7c3c bellard
static void init_timer_alarm(void)
1673 8a7ddc38 bellard
{
1674 223f0d72 blueswir1
    struct qemu_alarm_timer *t = NULL;
1675 c8994013 ths
    int i, err = -1;
1676 c8994013 ths
1677 c8994013 ths
    for (i = 0; alarm_timers[i].name; i++) {
1678 c8994013 ths
        t = &alarm_timers[i];
1679 c8994013 ths
1680 c8994013 ths
        err = t->start(t);
1681 c8994013 ths
        if (!err)
1682 c8994013 ths
            break;
1683 67b915a5 bellard
    }
1684 fd872598 bellard
1685 c8994013 ths
    if (err) {
1686 c8994013 ths
        fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1687 c8994013 ths
        fprintf(stderr, "Terminating\n");
1688 c8994013 ths
        exit(1);
1689 67b915a5 bellard
    }
1690 c8994013 ths
1691 c8994013 ths
    alarm_timer = t;
1692 8a7ddc38 bellard
}
1693 8a7ddc38 bellard
1694 9596ebb7 pbrook
static void quit_timers(void)
1695 40c3bac3 bellard
{
1696 c8994013 ths
    alarm_timer->stop(alarm_timer);
1697 c8994013 ths
    alarm_timer = NULL;
1698 40c3bac3 bellard
}
1699 40c3bac3 bellard
1700 c4b1fcc0 bellard
/***********************************************************/
1701 f6503059 balrog
/* host time/date access */
1702 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
1703 f6503059 balrog
{
1704 f6503059 balrog
    time_t ti;
1705 f6503059 balrog
    struct tm *ret;
1706 f6503059 balrog
1707 f6503059 balrog
    time(&ti);
1708 f6503059 balrog
    ti += offset;
1709 f6503059 balrog
    if (rtc_date_offset == -1) {
1710 f6503059 balrog
        if (rtc_utc)
1711 f6503059 balrog
            ret = gmtime(&ti);
1712 f6503059 balrog
        else
1713 f6503059 balrog
            ret = localtime(&ti);
1714 f6503059 balrog
    } else {
1715 f6503059 balrog
        ti -= rtc_date_offset;
1716 f6503059 balrog
        ret = gmtime(&ti);
1717 f6503059 balrog
    }
1718 f6503059 balrog
1719 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
1720 f6503059 balrog
}
1721 f6503059 balrog
1722 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
1723 f6503059 balrog
{
1724 f6503059 balrog
    time_t seconds;
1725 f6503059 balrog
1726 f6503059 balrog
    if (rtc_date_offset == -1)
1727 f6503059 balrog
        if (rtc_utc)
1728 f6503059 balrog
            seconds = mktimegm(tm);
1729 f6503059 balrog
        else
1730 f6503059 balrog
            seconds = mktime(tm);
1731 f6503059 balrog
    else
1732 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
1733 f6503059 balrog
1734 f6503059 balrog
    return seconds - time(NULL);
1735 f6503059 balrog
}
1736 f6503059 balrog
1737 fd1dff4b bellard
#ifdef _WIN32
1738 fd1dff4b bellard
static void socket_cleanup(void)
1739 fd1dff4b bellard
{
1740 fd1dff4b bellard
    WSACleanup();
1741 fd1dff4b bellard
}
1742 82c643ff bellard
1743 fd1dff4b bellard
static int socket_init(void)
1744 fd1dff4b bellard
{
1745 fd1dff4b bellard
    WSADATA Data;
1746 fd1dff4b bellard
    int ret, err;
1747 fd1dff4b bellard
1748 fd1dff4b bellard
    ret = WSAStartup(MAKEWORD(2,2), &Data);
1749 fd1dff4b bellard
    if (ret != 0) {
1750 fd1dff4b bellard
        err = WSAGetLastError();
1751 fd1dff4b bellard
        fprintf(stderr, "WSAStartup: %d\n", err);
1752 fd1dff4b bellard
        return -1;
1753 fd1dff4b bellard
    }
1754 fd1dff4b bellard
    atexit(socket_cleanup);
1755 fd1dff4b bellard
    return 0;
1756 fd1dff4b bellard
}
1757 64b7b733 aurel32
#endif
1758 64b7b733 aurel32
1759 63a01ef8 aliguori
const char *get_opt_name(char *buf, int buf_size, const char *p)
1760 609497ab balrog
{
1761 609497ab balrog
    char *q;
1762 609497ab balrog
1763 609497ab balrog
    q = buf;
1764 609497ab balrog
    while (*p != '\0' && *p != '=') {
1765 609497ab balrog
        if (q && (q - buf) < buf_size - 1)
1766 609497ab balrog
            *q++ = *p;
1767 609497ab balrog
        p++;
1768 609497ab balrog
    }
1769 609497ab balrog
    if (q)
1770 609497ab balrog
        *q = '\0';
1771 609497ab balrog
1772 609497ab balrog
    return p;
1773 609497ab balrog
}
1774 609497ab balrog
1775 63a01ef8 aliguori
const char *get_opt_value(char *buf, int buf_size, const char *p)
1776 e4bcb14c ths
{
1777 e4bcb14c ths
    char *q;
1778 e4bcb14c ths
1779 e4bcb14c ths
    q = buf;
1780 e4bcb14c ths
    while (*p != '\0') {
1781 609497ab balrog
        if (*p == ',') {
1782 609497ab balrog
            if (*(p + 1) != ',')
1783 e4bcb14c ths
                break;
1784 e4bcb14c ths
            p++;
1785 609497ab balrog
        }
1786 e4bcb14c ths
        if (q && (q - buf) < buf_size - 1)
1787 e4bcb14c ths
            *q++ = *p;
1788 e4bcb14c ths
        p++;
1789 e4bcb14c ths
    }
1790 e4bcb14c ths
    if (q)
1791 e4bcb14c ths
        *q = '\0';
1792 e4bcb14c ths
1793 e4bcb14c ths
    return p;
1794 e4bcb14c ths
}
1795 e4bcb14c ths
1796 63a01ef8 aliguori
int get_param_value(char *buf, int buf_size,
1797 63a01ef8 aliguori
                    const char *tag, const char *str)
1798 7c9d8e07 bellard
{
1799 7c9d8e07 bellard
    const char *p;
1800 7c9d8e07 bellard
    char option[128];
1801 7c9d8e07 bellard
1802 7c9d8e07 bellard
    p = str;
1803 7c9d8e07 bellard
    for(;;) {
1804 609497ab balrog
        p = get_opt_name(option, sizeof(option), p);
1805 7c9d8e07 bellard
        if (*p != '=')
1806 7c9d8e07 bellard
            break;
1807 7c9d8e07 bellard
        p++;
1808 7c9d8e07 bellard
        if (!strcmp(tag, option)) {
1809 609497ab balrog
            (void)get_opt_value(buf, buf_size, p);
1810 e4bcb14c ths
            return strlen(buf);
1811 7c9d8e07 bellard
        } else {
1812 609497ab balrog
            p = get_opt_value(NULL, 0, p);
1813 7c9d8e07 bellard
        }
1814 7c9d8e07 bellard
        if (*p != ',')
1815 7c9d8e07 bellard
            break;
1816 7c9d8e07 bellard
        p++;
1817 7c9d8e07 bellard
    }
1818 7c9d8e07 bellard
    return 0;
1819 7c9d8e07 bellard
}
1820 7c9d8e07 bellard
1821 63a01ef8 aliguori
int check_params(char *buf, int buf_size,
1822 63a01ef8 aliguori
                 const char * const *params, const char *str)
1823 e4bcb14c ths
{
1824 e4bcb14c ths
    const char *p;
1825 e4bcb14c ths
    int i;
1826 e4bcb14c ths
1827 e4bcb14c ths
    p = str;
1828 e4bcb14c ths
    for(;;) {
1829 609497ab balrog
        p = get_opt_name(buf, buf_size, p);
1830 e4bcb14c ths
        if (*p != '=')
1831 e4bcb14c ths
            return -1;
1832 e4bcb14c ths
        p++;
1833 e4bcb14c ths
        for(i = 0; params[i] != NULL; i++)
1834 e4bcb14c ths
            if (!strcmp(params[i], buf))
1835 e4bcb14c ths
                break;
1836 e4bcb14c ths
        if (params[i] == NULL)
1837 e4bcb14c ths
            return -1;
1838 609497ab balrog
        p = get_opt_value(NULL, 0, p);
1839 e4bcb14c ths
        if (*p != ',')
1840 e4bcb14c ths
            break;
1841 e4bcb14c ths
        p++;
1842 e4bcb14c ths
    }
1843 e4bcb14c ths
    return 0;
1844 e4bcb14c ths
}
1845 e4bcb14c ths
1846 1ae26a18 balrog
/***********************************************************/
1847 1ae26a18 balrog
/* Bluetooth support */
1848 1ae26a18 balrog
static int nb_hcis;
1849 1ae26a18 balrog
static int cur_hci;
1850 1ae26a18 balrog
static struct HCIInfo *hci_table[MAX_NICS];
1851 40ea94a5 blueswir1
#if 0
1852 1ae26a18 balrog
static struct bt_vlan_s {
1853 1ae26a18 balrog
    struct bt_scatternet_s net;
1854 1ae26a18 balrog
    int id;
1855 1ae26a18 balrog
    struct bt_vlan_s *next;
1856 1ae26a18 balrog
} *first_bt_vlan;
1857 1ae26a18 balrog

1858 1ae26a18 balrog
/* find or alloc a new bluetooth "VLAN" */
1859 674bb261 blueswir1
static struct bt_scatternet_s *qemu_find_bt_vlan(int id)
1860 1ae26a18 balrog
{
1861 1ae26a18 balrog
    struct bt_vlan_s **pvlan, *vlan;
1862 1ae26a18 balrog
    for (vlan = first_bt_vlan; vlan != NULL; vlan = vlan->next) {
1863 1ae26a18 balrog
        if (vlan->id == id)
1864 1ae26a18 balrog
            return &vlan->net;
1865 1ae26a18 balrog
    }
1866 1ae26a18 balrog
    vlan = qemu_mallocz(sizeof(struct bt_vlan_s));
1867 1ae26a18 balrog
    vlan->id = id;
1868 1ae26a18 balrog
    pvlan = &first_bt_vlan;
1869 1ae26a18 balrog
    while (*pvlan != NULL)
1870 1ae26a18 balrog
        pvlan = &(*pvlan)->next;
1871 1ae26a18 balrog
    *pvlan = vlan;
1872 1ae26a18 balrog
    return &vlan->net;
1873 1ae26a18 balrog
}
1874 40ea94a5 blueswir1
#endif
1875 1ae26a18 balrog
1876 1ae26a18 balrog
static void null_hci_send(struct HCIInfo *hci, const uint8_t *data, int len)
1877 1ae26a18 balrog
{
1878 1ae26a18 balrog
}
1879 1ae26a18 balrog
1880 1ae26a18 balrog
static int null_hci_addr_set(struct HCIInfo *hci, const uint8_t *bd_addr)
1881 1ae26a18 balrog
{
1882 1ae26a18 balrog
    return -ENOTSUP;
1883 1ae26a18 balrog
}
1884 1ae26a18 balrog
1885 1ae26a18 balrog
static struct HCIInfo null_hci = {
1886 1ae26a18 balrog
    .cmd_send = null_hci_send,
1887 1ae26a18 balrog
    .sco_send = null_hci_send,
1888 1ae26a18 balrog
    .acl_send = null_hci_send,
1889 1ae26a18 balrog
    .bdaddr_set = null_hci_addr_set,
1890 1ae26a18 balrog
};
1891 1ae26a18 balrog
1892 1ae26a18 balrog
struct HCIInfo *qemu_next_hci(void)
1893 1ae26a18 balrog
{
1894 1ae26a18 balrog
    if (cur_hci == nb_hcis)
1895 1ae26a18 balrog
        return &null_hci;
1896 1ae26a18 balrog
1897 1ae26a18 balrog
    return hci_table[cur_hci++];
1898 1ae26a18 balrog
}
1899 1ae26a18 balrog
1900 1ae26a18 balrog
/***********************************************************/
1901 1ae26a18 balrog
/* QEMU Block devices */
1902 1ae26a18 balrog
1903 609497ab balrog
#define HD_ALIAS "index=%d,media=disk"
1904 e4bcb14c ths
#ifdef TARGET_PPC
1905 e4bcb14c ths
#define CDROM_ALIAS "index=1,media=cdrom"
1906 e4bcb14c ths
#else
1907 e4bcb14c ths
#define CDROM_ALIAS "index=2,media=cdrom"
1908 e4bcb14c ths
#endif
1909 e4bcb14c ths
#define FD_ALIAS "index=%d,if=floppy"
1910 609497ab balrog
#define PFLASH_ALIAS "if=pflash"
1911 609497ab balrog
#define MTD_ALIAS "if=mtd"
1912 9d413d1d balrog
#define SD_ALIAS "index=0,if=sd"
1913 e4bcb14c ths
1914 609497ab balrog
static int drive_add(const char *file, const char *fmt, ...)
1915 e4bcb14c ths
{
1916 e4bcb14c ths
    va_list ap;
1917 e4bcb14c ths
1918 e4bcb14c ths
    if (nb_drives_opt >= MAX_DRIVES) {
1919 e4bcb14c ths
        fprintf(stderr, "qemu: too many drives\n");
1920 e4bcb14c ths
        exit(1);
1921 e4bcb14c ths
    }
1922 e4bcb14c ths
1923 609497ab balrog
    drives_opt[nb_drives_opt].file = file;
1924 e4bcb14c ths
    va_start(ap, fmt);
1925 609497ab balrog
    vsnprintf(drives_opt[nb_drives_opt].opt,
1926 609497ab balrog
              sizeof(drives_opt[0].opt), fmt, ap);
1927 e4bcb14c ths
    va_end(ap);
1928 e4bcb14c ths
1929 e4bcb14c ths
    return nb_drives_opt++;
1930 e4bcb14c ths
}
1931 e4bcb14c ths
1932 f60d39bc ths
int drive_get_index(BlockInterfaceType type, int bus, int unit)
1933 e4bcb14c ths
{
1934 e4bcb14c ths
    int index;
1935 e4bcb14c ths
1936 e4bcb14c ths
    /* seek interface, bus and unit */
1937 e4bcb14c ths
1938 e4bcb14c ths
    for (index = 0; index < nb_drives; index++)
1939 f60d39bc ths
        if (drives_table[index].type == type &&
1940 e4bcb14c ths
            drives_table[index].bus == bus &&
1941 e4bcb14c ths
            drives_table[index].unit == unit)
1942 e4bcb14c ths
        return index;
1943 e4bcb14c ths
1944 e4bcb14c ths
    return -1;
1945 e4bcb14c ths
}
1946 e4bcb14c ths
1947 f60d39bc ths
int drive_get_max_bus(BlockInterfaceType type)
1948 e4bcb14c ths
{
1949 e4bcb14c ths
    int max_bus;
1950 e4bcb14c ths
    int index;
1951 e4bcb14c ths
1952 e4bcb14c ths
    max_bus = -1;
1953 e4bcb14c ths
    for (index = 0; index < nb_drives; index++) {
1954 f60d39bc ths
        if(drives_table[index].type == type &&
1955 e4bcb14c ths
           drives_table[index].bus > max_bus)
1956 e4bcb14c ths
            max_bus = drives_table[index].bus;
1957 e4bcb14c ths
    }
1958 e4bcb14c ths
    return max_bus;
1959 e4bcb14c ths
}
1960 e4bcb14c ths
1961 a1620fac aurel32
static void bdrv_format_print(void *opaque, const char *name)
1962 a1620fac aurel32
{
1963 a1620fac aurel32
    fprintf(stderr, " %s", name);
1964 a1620fac aurel32
}
1965 a1620fac aurel32
1966 609497ab balrog
static int drive_init(struct drive_opt *arg, int snapshot,
1967 609497ab balrog
                      QEMUMachine *machine)
1968 e4bcb14c ths
{
1969 e4bcb14c ths
    char buf[128];
1970 e4bcb14c ths
    char file[1024];
1971 c8522bdf balrog
    char devname[128];
1972 c8522bdf balrog
    const char *mediastr = "";
1973 f60d39bc ths
    BlockInterfaceType type;
1974 e4bcb14c ths
    enum { MEDIA_DISK, MEDIA_CDROM } media;
1975 e4bcb14c ths
    int bus_id, unit_id;
1976 e4bcb14c ths
    int cyls, heads, secs, translation;
1977 e4bcb14c ths
    BlockDriverState *bdrv;
1978 1e72d3b7 aurel32
    BlockDriver *drv = NULL;
1979 e4bcb14c ths
    int max_devs;
1980 e4bcb14c ths
    int index;
1981 33f00271 balrog
    int cache;
1982 33f00271 balrog
    int bdrv_flags;
1983 609497ab balrog
    char *str = arg->opt;
1984 7ccfb2eb blueswir1
    static const char * const params[] = { "bus", "unit", "if", "index",
1985 7ccfb2eb blueswir1
                                           "cyls", "heads", "secs", "trans",
1986 7ccfb2eb blueswir1
                                           "media", "snapshot", "file",
1987 7ccfb2eb blueswir1
                                           "cache", "format", NULL };
1988 e4bcb14c ths
1989 e4bcb14c ths
    if (check_params(buf, sizeof(buf), params, str) < 0) {
1990 ff993638 balrog
         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
1991 e4bcb14c ths
                         buf, str);
1992 e4bcb14c ths
         return -1;
1993 e4bcb14c ths
    }
1994 e4bcb14c ths
1995 e4bcb14c ths
    file[0] = 0;
1996 e4bcb14c ths
    cyls = heads = secs = 0;
1997 e4bcb14c ths
    bus_id = 0;
1998 e4bcb14c ths
    unit_id = -1;
1999 e4bcb14c ths
    translation = BIOS_ATA_TRANSLATION_AUTO;
2000 e4bcb14c ths
    index = -1;
2001 33f00271 balrog
    cache = 1;
2002 e4bcb14c ths
2003 c9b1ae2c blueswir1
    if (machine->use_scsi) {
2004 f60d39bc ths
        type = IF_SCSI;
2005 e4bcb14c ths
        max_devs = MAX_SCSI_DEVS;
2006 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "scsi");
2007 e4bcb14c ths
    } else {
2008 f60d39bc ths
        type = IF_IDE;
2009 e4bcb14c ths
        max_devs = MAX_IDE_DEVS;
2010 363a37d5 blueswir1
        pstrcpy(devname, sizeof(devname), "ide");
2011 e4bcb14c ths
    }
2012 e4bcb14c ths
    media = MEDIA_DISK;
2013 e4bcb14c ths
2014 e4bcb14c ths
    /* extract parameters */
2015 e4bcb14c ths
2016 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "bus", str)) {
2017 e4bcb14c ths
        bus_id = strtol(buf, NULL, 0);
2018 e4bcb14c ths
        if (bus_id < 0) {
2019 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
2020 e4bcb14c ths
            return -1;
2021 e4bcb14c ths
        }
2022 e4bcb14c ths
    }
2023 e4bcb14c ths
2024 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "unit", str)) {
2025 e4bcb14c ths
        unit_id = strtol(buf, NULL, 0);
2026 e4bcb14c ths
        if (unit_id < 0) {
2027 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
2028 e4bcb14c ths
            return -1;
2029 e4bcb14c ths
        }
2030 e4bcb14c ths
    }
2031 e4bcb14c ths
2032 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "if", str)) {
2033 ae45d369 bellard
        pstrcpy(devname, sizeof(devname), buf);
2034 e4bcb14c ths
        if (!strcmp(buf, "ide")) {
2035 f60d39bc ths
            type = IF_IDE;
2036 e4bcb14c ths
            max_devs = MAX_IDE_DEVS;
2037 e4bcb14c ths
        } else if (!strcmp(buf, "scsi")) {
2038 f60d39bc ths
            type = IF_SCSI;
2039 e4bcb14c ths
            max_devs = MAX_SCSI_DEVS;
2040 e4bcb14c ths
        } else if (!strcmp(buf, "floppy")) {
2041 f60d39bc ths
            type = IF_FLOPPY;
2042 e4bcb14c ths
            max_devs = 0;
2043 e4bcb14c ths
        } else if (!strcmp(buf, "pflash")) {
2044 f60d39bc ths
            type = IF_PFLASH;
2045 e4bcb14c ths
            max_devs = 0;
2046 e4bcb14c ths
        } else if (!strcmp(buf, "mtd")) {
2047 f60d39bc ths
            type = IF_MTD;
2048 e4bcb14c ths
            max_devs = 0;
2049 e4bcb14c ths
        } else if (!strcmp(buf, "sd")) {
2050 f60d39bc ths
            type = IF_SD;
2051 e4bcb14c ths
            max_devs = 0;
2052 e4bcb14c ths
        } else {
2053 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
2054 e4bcb14c ths
            return -1;
2055 e4bcb14c ths
        }
2056 e4bcb14c ths
    }
2057 e4bcb14c ths
2058 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "index", str)) {
2059 e4bcb14c ths
        index = strtol(buf, NULL, 0);
2060 e4bcb14c ths
        if (index < 0) {
2061 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid index\n", str);
2062 e4bcb14c ths
            return -1;
2063 e4bcb14c ths
        }
2064 e4bcb14c ths
    }
2065 e4bcb14c ths
2066 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "cyls", str)) {
2067 e4bcb14c ths
        cyls = strtol(buf, NULL, 0);
2068 e4bcb14c ths
    }
2069 e4bcb14c ths
2070 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "heads", str)) {
2071 e4bcb14c ths
        heads = strtol(buf, NULL, 0);
2072 e4bcb14c ths
    }
2073 e4bcb14c ths
2074 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "secs", str)) {
2075 e4bcb14c ths
        secs = strtol(buf, NULL, 0);
2076 e4bcb14c ths
    }
2077 e4bcb14c ths
2078 e4bcb14c ths
    if (cyls || heads || secs) {
2079 e4bcb14c ths
        if (cyls < 1 || cyls > 16383) {
2080 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
2081 e4bcb14c ths
            return -1;
2082 e4bcb14c ths
        }
2083 e4bcb14c ths
        if (heads < 1 || heads > 16) {
2084 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
2085 e4bcb14c ths
            return -1;
2086 e4bcb14c ths
        }
2087 e4bcb14c ths
        if (secs < 1 || secs > 63) {
2088 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
2089 e4bcb14c ths
            return -1;
2090 e4bcb14c ths
        }
2091 e4bcb14c ths
    }
2092 e4bcb14c ths
2093 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "trans", str)) {
2094 e4bcb14c ths
        if (!cyls) {
2095 e4bcb14c ths
            fprintf(stderr,
2096 e4bcb14c ths
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
2097 e4bcb14c ths
                    str);
2098 e4bcb14c ths
            return -1;
2099 e4bcb14c ths
        }
2100 e4bcb14c ths
        if (!strcmp(buf, "none"))
2101 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_NONE;
2102 e4bcb14c ths
        else if (!strcmp(buf, "lba"))
2103 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_LBA;
2104 e4bcb14c ths
        else if (!strcmp(buf, "auto"))
2105 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_AUTO;
2106 e4bcb14c ths
        else {
2107 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
2108 e4bcb14c ths
            return -1;
2109 e4bcb14c ths
        }
2110 e4bcb14c ths
    }
2111 e4bcb14c ths
2112 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "media", str)) {
2113 e4bcb14c ths
        if (!strcmp(buf, "disk")) {
2114 e4bcb14c ths
            media = MEDIA_DISK;
2115 e4bcb14c ths
        } else if (!strcmp(buf, "cdrom")) {
2116 e4bcb14c ths
            if (cyls || secs || heads) {
2117 e4bcb14c ths
                fprintf(stderr,
2118 e4bcb14c ths
                        "qemu: '%s' invalid physical CHS format\n", str);
2119 e4bcb14c ths
                return -1;
2120 e4bcb14c ths
            }
2121 e4bcb14c ths
            media = MEDIA_CDROM;
2122 e4bcb14c ths
        } else {
2123 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid media\n", str);
2124 e4bcb14c ths
            return -1;
2125 e4bcb14c ths
        }
2126 e4bcb14c ths
    }
2127 e4bcb14c ths
2128 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
2129 e4bcb14c ths
        if (!strcmp(buf, "on"))
2130 e4bcb14c ths
            snapshot = 1;
2131 e4bcb14c ths
        else if (!strcmp(buf, "off"))
2132 e4bcb14c ths
            snapshot = 0;
2133 e4bcb14c ths
        else {
2134 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
2135 e4bcb14c ths
            return -1;
2136 e4bcb14c ths
        }
2137 e4bcb14c ths
    }
2138 e4bcb14c ths
2139 33f00271 balrog
    if (get_param_value(buf, sizeof(buf), "cache", str)) {
2140 9f7965c7 aliguori
        if (!strcmp(buf, "off") || !strcmp(buf, "none"))
2141 33f00271 balrog
            cache = 0;
2142 9f7965c7 aliguori
        else if (!strcmp(buf, "writethrough"))
2143 33f00271 balrog
            cache = 1;
2144 9f7965c7 aliguori
        else if (!strcmp(buf, "writeback"))
2145 9f7965c7 aliguori
            cache = 2;
2146 33f00271 balrog
        else {
2147 33f00271 balrog
           fprintf(stderr, "qemu: invalid cache option\n");
2148 33f00271 balrog
           return -1;
2149 33f00271 balrog
        }
2150 33f00271 balrog
    }
2151 33f00271 balrog
2152 1e72d3b7 aurel32
    if (get_param_value(buf, sizeof(buf), "format", str)) {
2153 a1620fac aurel32
       if (strcmp(buf, "?") == 0) {
2154 a1620fac aurel32
            fprintf(stderr, "qemu: Supported formats:");
2155 a1620fac aurel32
            bdrv_iterate_format(bdrv_format_print, NULL);
2156 a1620fac aurel32
            fprintf(stderr, "\n");
2157 a1620fac aurel32
            return -1;
2158 a1620fac aurel32
        }
2159 1e72d3b7 aurel32
        drv = bdrv_find_format(buf);
2160 1e72d3b7 aurel32
        if (!drv) {
2161 1e72d3b7 aurel32
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
2162 1e72d3b7 aurel32
            return -1;
2163 1e72d3b7 aurel32
        }
2164 1e72d3b7 aurel32
    }
2165 1e72d3b7 aurel32
2166 609497ab balrog
    if (arg->file == NULL)
2167 609497ab balrog
        get_param_value(file, sizeof(file), "file", str);
2168 609497ab balrog
    else
2169 609497ab balrog
        pstrcpy(file, sizeof(file), arg->file);
2170 e4bcb14c ths
2171 e4bcb14c ths
    /* compute bus and unit according index */
2172 e4bcb14c ths
2173 e4bcb14c ths
    if (index != -1) {
2174 e4bcb14c ths
        if (bus_id != 0 || unit_id != -1) {
2175 e4bcb14c ths
            fprintf(stderr,
2176 e4bcb14c ths
                    "qemu: '%s' index cannot be used with bus and unit\n", str);
2177 e4bcb14c ths
            return -1;
2178 e4bcb14c ths
        }
2179 e4bcb14c ths
        if (max_devs == 0)
2180 e4bcb14c ths
        {
2181 e4bcb14c ths
            unit_id = index;
2182 e4bcb14c ths
            bus_id = 0;
2183 e4bcb14c ths
        } else {
2184 e4bcb14c ths
            unit_id = index % max_devs;
2185 e4bcb14c ths
            bus_id = index / max_devs;
2186 e4bcb14c ths
        }
2187 e4bcb14c ths
    }
2188 e4bcb14c ths
2189 e4bcb14c ths
    /* if user doesn't specify a unit_id,
2190 e4bcb14c ths
     * try to find the first free
2191 e4bcb14c ths
     */
2192 e4bcb14c ths
2193 e4bcb14c ths
    if (unit_id == -1) {
2194 e4bcb14c ths
       unit_id = 0;
2195 f60d39bc ths
       while (drive_get_index(type, bus_id, unit_id) != -1) {
2196 e4bcb14c ths
           unit_id++;
2197 e4bcb14c ths
           if (max_devs && unit_id >= max_devs) {
2198 e4bcb14c ths
               unit_id -= max_devs;
2199 e4bcb14c ths
               bus_id++;
2200 e4bcb14c ths
           }
2201 e4bcb14c ths
       }
2202 e4bcb14c ths
    }
2203 e4bcb14c ths
2204 e4bcb14c ths
    /* check unit id */
2205 e4bcb14c ths
2206 e4bcb14c ths
    if (max_devs && unit_id >= max_devs) {
2207 e4bcb14c ths
        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
2208 e4bcb14c ths
                        str, unit_id, max_devs - 1);
2209 e4bcb14c ths
        return -1;
2210 e4bcb14c ths
    }
2211 e4bcb14c ths
2212 e4bcb14c ths
    /*
2213 e4bcb14c ths
     * ignore multiple definitions
2214 e4bcb14c ths
     */
2215 e4bcb14c ths
2216 f60d39bc ths
    if (drive_get_index(type, bus_id, unit_id) != -1)
2217 e4bcb14c ths
        return 0;
2218 e4bcb14c ths
2219 e4bcb14c ths
    /* init */
2220 e4bcb14c ths
2221 f60d39bc ths
    if (type == IF_IDE || type == IF_SCSI)
2222 c8522bdf balrog
        mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
2223 e6198a70 balrog
    if (max_devs)
2224 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%i%s%i",
2225 e6198a70 balrog
                 devname, bus_id, mediastr, unit_id);
2226 e6198a70 balrog
    else
2227 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%s%i",
2228 e6198a70 balrog
                 devname, mediastr, unit_id);
2229 e4bcb14c ths
    bdrv = bdrv_new(buf);
2230 e4bcb14c ths
    drives_table[nb_drives].bdrv = bdrv;
2231 f60d39bc ths
    drives_table[nb_drives].type = type;
2232 e4bcb14c ths
    drives_table[nb_drives].bus = bus_id;
2233 e4bcb14c ths
    drives_table[nb_drives].unit = unit_id;
2234 e4bcb14c ths
    nb_drives++;
2235 e4bcb14c ths
2236 f60d39bc ths
    switch(type) {
2237 e4bcb14c ths
    case IF_IDE:
2238 e4bcb14c ths
    case IF_SCSI:
2239 e4bcb14c ths
        switch(media) {
2240 e4bcb14c ths
        case MEDIA_DISK:
2241 e4bcb14c ths
            if (cyls != 0) {
2242 e4bcb14c ths
                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
2243 e4bcb14c ths
                bdrv_set_translation_hint(bdrv, translation);
2244 e4bcb14c ths
            }
2245 e4bcb14c ths
            break;
2246 e4bcb14c ths
        case MEDIA_CDROM:
2247 e4bcb14c ths
            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
2248 e4bcb14c ths
            break;
2249 e4bcb14c ths
        }
2250 e4bcb14c ths
        break;
2251 e4bcb14c ths
    case IF_SD:
2252 e4bcb14c ths
        /* FIXME: This isn't really a floppy, but it's a reasonable
2253 e4bcb14c ths
           approximation.  */
2254 e4bcb14c ths
    case IF_FLOPPY:
2255 e4bcb14c ths
        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
2256 e4bcb14c ths
        break;
2257 e4bcb14c ths
    case IF_PFLASH:
2258 e4bcb14c ths
    case IF_MTD:
2259 e4bcb14c ths
        break;
2260 e4bcb14c ths
    }
2261 e4bcb14c ths
    if (!file[0])
2262 e4bcb14c ths
        return 0;
2263 33f00271 balrog
    bdrv_flags = 0;
2264 9f7965c7 aliguori
    if (snapshot) {
2265 33f00271 balrog
        bdrv_flags |= BDRV_O_SNAPSHOT;
2266 9f7965c7 aliguori
        cache = 2; /* always use write-back with snapshot */
2267 9f7965c7 aliguori
    }
2268 9f7965c7 aliguori
    if (cache == 0) /* no caching */
2269 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_NOCACHE;
2270 9f7965c7 aliguori
    else if (cache == 2) /* write-back */
2271 9f7965c7 aliguori
        bdrv_flags |= BDRV_O_CACHE_WB;
2272 83ab7950 aliguori
    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
2273 e4bcb14c ths
        fprintf(stderr, "qemu: could not open disk image %s\n",
2274 e4bcb14c ths
                        file);
2275 e4bcb14c ths
        return -1;
2276 e4bcb14c ths
    }
2277 e4bcb14c ths
    return 0;
2278 e4bcb14c ths
}
2279 e4bcb14c ths
2280 330d0414 bellard
/***********************************************************/
2281 a594cfbf bellard
/* USB devices */
2282 a594cfbf bellard
2283 0d92ed30 pbrook
static USBPort *used_usb_ports;
2284 0d92ed30 pbrook
static USBPort *free_usb_ports;
2285 0d92ed30 pbrook
2286 0d92ed30 pbrook
/* ??? Maybe change this to register a hub to keep track of the topology.  */
2287 0d92ed30 pbrook
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
2288 0d92ed30 pbrook
                            usb_attachfn attach)
2289 0d92ed30 pbrook
{
2290 0d92ed30 pbrook
    port->opaque = opaque;
2291 0d92ed30 pbrook
    port->index = index;
2292 0d92ed30 pbrook
    port->attach = attach;
2293 0d92ed30 pbrook
    port->next = free_usb_ports;
2294 0d92ed30 pbrook
    free_usb_ports = port;
2295 0d92ed30 pbrook
}
2296 0d92ed30 pbrook
2297 4b096fc9 aliguori
int usb_device_add_dev(USBDevice *dev)
2298 4b096fc9 aliguori
{
2299 4b096fc9 aliguori
    USBPort *port;
2300 4b096fc9 aliguori
2301 4b096fc9 aliguori
    /* Find a USB port to add the device to.  */
2302 4b096fc9 aliguori
    port = free_usb_ports;
2303 4b096fc9 aliguori
    if (!port->next) {
2304 4b096fc9 aliguori
        USBDevice *hub;
2305 4b096fc9 aliguori
2306 4b096fc9 aliguori
        /* Create a new hub and chain it on.  */
2307 4b096fc9 aliguori
        free_usb_ports = NULL;
2308 4b096fc9 aliguori
        port->next = used_usb_ports;
2309 4b096fc9 aliguori
        used_usb_ports = port;
2310 4b096fc9 aliguori
2311 4b096fc9 aliguori
        hub = usb_hub_init(VM_USB_HUB_SIZE);
2312 4b096fc9 aliguori
        usb_attach(port, hub);
2313 4b096fc9 aliguori
        port = free_usb_ports;
2314 4b096fc9 aliguori
    }
2315 4b096fc9 aliguori
2316 4b096fc9 aliguori
    free_usb_ports = port->next;
2317 4b096fc9 aliguori
    port->next = used_usb_ports;
2318 4b096fc9 aliguori
    used_usb_ports = port;
2319 4b096fc9 aliguori
    usb_attach(port, dev);
2320 4b096fc9 aliguori
    return 0;
2321 4b096fc9 aliguori
}
2322 4b096fc9 aliguori
2323 a594cfbf bellard
static int usb_device_add(const char *devname)
2324 a594cfbf bellard
{
2325 a594cfbf bellard
    const char *p;
2326 a594cfbf bellard
    USBDevice *dev;
2327 a594cfbf bellard
2328 0d92ed30 pbrook
    if (!free_usb_ports)
2329 a594cfbf bellard
        return -1;
2330 a594cfbf bellard
2331 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
2332 a594cfbf bellard
        dev = usb_host_device_open(p);
2333 a594cfbf bellard
    } else if (!strcmp(devname, "mouse")) {
2334 a594cfbf bellard
        dev = usb_mouse_init();
2335 09b26c5e bellard
    } else if (!strcmp(devname, "tablet")) {
2336 47b2d338 balrog
        dev = usb_tablet_init();
2337 47b2d338 balrog
    } else if (!strcmp(devname, "keyboard")) {
2338 47b2d338 balrog
        dev = usb_keyboard_init();
2339 2e5d83bb pbrook
    } else if (strstart(devname, "disk:", &p)) {
2340 2e5d83bb pbrook
        dev = usb_msd_init(p);
2341 f6d2a316 balrog
    } else if (!strcmp(devname, "wacom-tablet")) {
2342 f6d2a316 balrog
        dev = usb_wacom_init();
2343 a7954218 balrog
    } else if (strstart(devname, "serial:", &p)) {
2344 a7954218 balrog
        dev = usb_serial_init(p);
2345 2e4d9fb1 aurel32
#ifdef CONFIG_BRLAPI
2346 2e4d9fb1 aurel32
    } else if (!strcmp(devname, "braille")) {
2347 2e4d9fb1 aurel32
        dev = usb_baum_init();
2348 2e4d9fb1 aurel32
#endif
2349 6c9f886c balrog
    } else if (strstart(devname, "net:", &p)) {
2350 9ad97e65 balrog
        int nic = nb_nics;
2351 6c9f886c balrog
2352 9ad97e65 balrog
        if (net_client_init("nic", p) < 0)
2353 6c9f886c balrog
            return -1;
2354 9ad97e65 balrog
        nd_table[nic].model = "usb";
2355 9ad97e65 balrog
        dev = usb_net_init(&nd_table[nic]);
2356 a594cfbf bellard
    } else {
2357 a594cfbf bellard
        return -1;
2358 a594cfbf bellard
    }
2359 0d92ed30 pbrook
    if (!dev)
2360 0d92ed30 pbrook
        return -1;
2361 0d92ed30 pbrook
2362 4b096fc9 aliguori
    return usb_device_add_dev(dev);
2363 a594cfbf bellard
}
2364 a594cfbf bellard
2365 1f3870ab aliguori
int usb_device_del_addr(int bus_num, int addr)
2366 a594cfbf bellard
{
2367 0d92ed30 pbrook
    USBPort *port;
2368 0d92ed30 pbrook
    USBPort **lastp;
2369 059809e4 bellard
    USBDevice *dev;
2370 a594cfbf bellard
2371 0d92ed30 pbrook
    if (!used_usb_ports)
2372 a594cfbf bellard
        return -1;
2373 a594cfbf bellard
2374 a594cfbf bellard
    if (bus_num != 0)
2375 a594cfbf bellard
        return -1;
2376 0d92ed30 pbrook
2377 0d92ed30 pbrook
    lastp = &used_usb_ports;
2378 0d92ed30 pbrook
    port = used_usb_ports;
2379 0d92ed30 pbrook
    while (port && port->dev->addr != addr) {
2380 0d92ed30 pbrook
        lastp = &port->next;
2381 0d92ed30 pbrook
        port = port->next;
2382 a594cfbf bellard
    }
2383 0d92ed30 pbrook
2384 0d92ed30 pbrook
    if (!port)
2385 a594cfbf bellard
        return -1;
2386 0d92ed30 pbrook
2387 059809e4 bellard
    dev = port->dev;
2388 0d92ed30 pbrook
    *lastp = port->next;
2389 0d92ed30 pbrook
    usb_attach(port, NULL);
2390 059809e4 bellard
    dev->handle_destroy(dev);
2391 0d92ed30 pbrook
    port->next = free_usb_ports;
2392 0d92ed30 pbrook
    free_usb_ports = port;
2393 a594cfbf bellard
    return 0;
2394 a594cfbf bellard
}
2395 a594cfbf bellard
2396 1f3870ab aliguori
static int usb_device_del(const char *devname)
2397 1f3870ab aliguori
{
2398 1f3870ab aliguori
    int bus_num, addr;
2399 1f3870ab aliguori
    const char *p;
2400 1f3870ab aliguori
2401 5d0c5750 aliguori
    if (strstart(devname, "host:", &p))
2402 5d0c5750 aliguori
        return usb_host_device_close(p);
2403 5d0c5750 aliguori
2404 1f3870ab aliguori
    if (!used_usb_ports)
2405 1f3870ab aliguori
        return -1;
2406 1f3870ab aliguori
2407 1f3870ab aliguori
    p = strchr(devname, '.');
2408 1f3870ab aliguori
    if (!p)
2409 1f3870ab aliguori
        return -1;
2410 1f3870ab aliguori
    bus_num = strtoul(devname, NULL, 0);
2411 1f3870ab aliguori
    addr = strtoul(p + 1, NULL, 0);
2412 1f3870ab aliguori
2413 1f3870ab aliguori
    return usb_device_del_addr(bus_num, addr);
2414 1f3870ab aliguori
}
2415 1f3870ab aliguori
2416 a594cfbf bellard
void do_usb_add(const char *devname)
2417 a594cfbf bellard
{
2418 4b096fc9 aliguori
    usb_device_add(devname);
2419 a594cfbf bellard
}
2420 a594cfbf bellard
2421 a594cfbf bellard
void do_usb_del(const char *devname)
2422 a594cfbf bellard
{
2423 4b096fc9 aliguori
    usb_device_del(devname);
2424 a594cfbf bellard
}
2425 a594cfbf bellard
2426 a594cfbf bellard
void usb_info(void)
2427 a594cfbf bellard
{
2428 a594cfbf bellard
    USBDevice *dev;
2429 0d92ed30 pbrook
    USBPort *port;
2430 a594cfbf bellard
    const char *speed_str;
2431 a594cfbf bellard
2432 0d92ed30 pbrook
    if (!usb_enabled) {
2433 a594cfbf bellard
        term_printf("USB support not enabled\n");
2434 a594cfbf bellard
        return;
2435 a594cfbf bellard
    }
2436 a594cfbf bellard
2437 0d92ed30 pbrook
    for (port = used_usb_ports; port; port = port->next) {
2438 0d92ed30 pbrook
        dev = port->dev;
2439 0d92ed30 pbrook
        if (!dev)
2440 0d92ed30 pbrook
            continue;
2441 0d92ed30 pbrook
        switch(dev->speed) {
2442 5fafdf24 ths
        case USB_SPEED_LOW:
2443 5fafdf24 ths
            speed_str = "1.5";
2444 0d92ed30 pbrook
            break;
2445 5fafdf24 ths
        case USB_SPEED_FULL:
2446 5fafdf24 ths
            speed_str = "12";
2447 0d92ed30 pbrook
            break;
2448 5fafdf24 ths
        case USB_SPEED_HIGH:
2449 5fafdf24 ths
            speed_str = "480";
2450 0d92ed30 pbrook
            break;
2451 0d92ed30 pbrook
        default:
2452 5fafdf24 ths
            speed_str = "?";
2453 0d92ed30 pbrook
            break;
2454 a594cfbf bellard
        }
2455 5fafdf24 ths
        term_printf("  Device %d.%d, Speed %s Mb/s, Product %s\n",
2456 1f6e24e7 bellard
                    0, dev->addr, speed_str, dev->devname);
2457 a594cfbf bellard
    }
2458 a594cfbf bellard
}
2459 a594cfbf bellard
2460 f7cce898 bellard
/***********************************************************/
2461 201a51fc balrog
/* PCMCIA/Cardbus */
2462 201a51fc balrog
2463 201a51fc balrog
static struct pcmcia_socket_entry_s {
2464 201a51fc balrog
    struct pcmcia_socket_s *socket;
2465 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
2466 201a51fc balrog
} *pcmcia_sockets = 0;
2467 201a51fc balrog
2468 201a51fc balrog
void pcmcia_socket_register(struct pcmcia_socket_s *socket)
2469 201a51fc balrog
{
2470 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
2471 201a51fc balrog
2472 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
2473 201a51fc balrog
    entry->socket = socket;
2474 201a51fc balrog
    entry->next = pcmcia_sockets;
2475 201a51fc balrog
    pcmcia_sockets = entry;
2476 201a51fc balrog
}
2477 201a51fc balrog
2478 201a51fc balrog
void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
2479 201a51fc balrog
{
2480 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
2481 201a51fc balrog
2482 201a51fc balrog
    ptr = &pcmcia_sockets;
2483 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
2484 201a51fc balrog
        if (entry->socket == socket) {
2485 201a51fc balrog
            *ptr = entry->next;
2486 201a51fc balrog
            qemu_free(entry);
2487 201a51fc balrog
        }
2488 201a51fc balrog
}
2489 201a51fc balrog
2490 201a51fc balrog
void pcmcia_info(void)
2491 201a51fc balrog
{
2492 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
2493 201a51fc balrog
    if (!pcmcia_sockets)
2494 201a51fc balrog
        term_printf("No PCMCIA sockets\n");
2495 201a51fc balrog
2496 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
2497 201a51fc balrog
        term_printf("%s: %s\n", iter->socket->slot_string,
2498 201a51fc balrog
                    iter->socket->attached ? iter->socket->card_string :
2499 201a51fc balrog
                    "Empty");
2500 201a51fc balrog
}
2501 201a51fc balrog
2502 201a51fc balrog
/***********************************************************/
2503 2ff89790 ths
/* dumb display */
2504 2ff89790 ths
2505 2ff89790 ths
static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
2506 2ff89790 ths
{
2507 2ff89790 ths
}
2508 2ff89790 ths
2509 2ff89790 ths
static void dumb_resize(DisplayState *ds, int w, int h)
2510 2ff89790 ths
{
2511 2ff89790 ths
}
2512 2ff89790 ths
2513 2ff89790 ths
static void dumb_refresh(DisplayState *ds)
2514 2ff89790 ths
{
2515 2ff89790 ths
#if defined(CONFIG_SDL)
2516 2ff89790 ths
    vga_hw_update();
2517 2ff89790 ths
#endif
2518 2ff89790 ths
}
2519 2ff89790 ths
2520 2ff89790 ths
static void dumb_display_init(DisplayState *ds)
2521 2ff89790 ths
{
2522 2ff89790 ths
    ds->data = NULL;
2523 2ff89790 ths
    ds->linesize = 0;
2524 2ff89790 ths
    ds->depth = 0;
2525 2ff89790 ths
    ds->dpy_update = dumb_update;
2526 2ff89790 ths
    ds->dpy_resize = dumb_resize;
2527 2ff89790 ths
    ds->dpy_refresh = dumb_refresh;
2528 bcfad70f aliguori
    ds->gui_timer_interval = 500;
2529 bcfad70f aliguori
    ds->idle = 1;
2530 2ff89790 ths
}
2531 2ff89790 ths
2532 2ff89790 ths
/***********************************************************/
2533 8a7ddc38 bellard
/* I/O handling */
2534 0824d6fc bellard
2535 c4b1fcc0 bellard
#define MAX_IO_HANDLERS 64
2536 c4b1fcc0 bellard
2537 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
2538 c4b1fcc0 bellard
    int fd;
2539 7c9d8e07 bellard
    IOCanRWHandler *fd_read_poll;
2540 7c9d8e07 bellard
    IOHandler *fd_read;
2541 7c9d8e07 bellard
    IOHandler *fd_write;
2542 cafffd40 ths
    int deleted;
2543 c4b1fcc0 bellard
    void *opaque;
2544 c4b1fcc0 bellard
    /* temporary data */
2545 c4b1fcc0 bellard
    struct pollfd *ufd;
2546 8a7ddc38 bellard
    struct IOHandlerRecord *next;
2547 c4b1fcc0 bellard
} IOHandlerRecord;
2548 c4b1fcc0 bellard
2549 8a7ddc38 bellard
static IOHandlerRecord *first_io_handler;
2550 c4b1fcc0 bellard
2551 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
2552 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
2553 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
2554 5fafdf24 ths
                         IOCanRWHandler *fd_read_poll,
2555 5fafdf24 ths
                         IOHandler *fd_read,
2556 5fafdf24 ths
                         IOHandler *fd_write,
2557 7c9d8e07 bellard
                         void *opaque)
2558 c4b1fcc0 bellard
{
2559 7c9d8e07 bellard
    IOHandlerRecord **pioh, *ioh;
2560 c4b1fcc0 bellard
2561 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
2562 7c9d8e07 bellard
        pioh = &first_io_handler;
2563 7c9d8e07 bellard
        for(;;) {
2564 7c9d8e07 bellard
            ioh = *pioh;
2565 7c9d8e07 bellard
            if (ioh == NULL)
2566 7c9d8e07 bellard
                break;
2567 7c9d8e07 bellard
            if (ioh->fd == fd) {
2568 cafffd40 ths
                ioh->deleted = 1;
2569 7c9d8e07 bellard
                break;
2570 7c9d8e07 bellard
            }
2571 7c9d8e07 bellard
            pioh = &ioh->next;
2572 7c9d8e07 bellard
        }
2573 7c9d8e07 bellard
    } else {
2574 7c9d8e07 bellard
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
2575 7c9d8e07 bellard
            if (ioh->fd == fd)
2576 7c9d8e07 bellard
                goto found;
2577 7c9d8e07 bellard
        }
2578 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
2579 7c9d8e07 bellard
        if (!ioh)
2580 7c9d8e07 bellard
            return -1;
2581 7c9d8e07 bellard
        ioh->next = first_io_handler;
2582 7c9d8e07 bellard
        first_io_handler = ioh;
2583 7c9d8e07 bellard
    found:
2584 7c9d8e07 bellard
        ioh->fd = fd;
2585 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
2586 7c9d8e07 bellard
        ioh->fd_read = fd_read;
2587 7c9d8e07 bellard
        ioh->fd_write = fd_write;
2588 7c9d8e07 bellard
        ioh->opaque = opaque;
2589 cafffd40 ths
        ioh->deleted = 0;
2590 7c9d8e07 bellard
    }
2591 c4b1fcc0 bellard
    return 0;
2592 c4b1fcc0 bellard
}
2593 c4b1fcc0 bellard
2594 5fafdf24 ths
int qemu_set_fd_handler(int fd,
2595 5fafdf24 ths
                        IOHandler *fd_read,
2596 5fafdf24 ths
                        IOHandler *fd_write,
2597 7c9d8e07 bellard
                        void *opaque)
2598 8a7ddc38 bellard
{
2599 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
2600 8a7ddc38 bellard
}
2601 8a7ddc38 bellard
2602 56f3a5d0 aliguori
#ifdef _WIN32
2603 8a7ddc38 bellard
/***********************************************************/
2604 f331110f bellard
/* Polling handling */
2605 f331110f bellard
2606 f331110f bellard
typedef struct PollingEntry {
2607 f331110f bellard
    PollingFunc *func;
2608 f331110f bellard
    void *opaque;
2609 f331110f bellard
    struct PollingEntry *next;
2610 f331110f bellard
} PollingEntry;
2611 f331110f bellard
2612 f331110f bellard
static PollingEntry *first_polling_entry;
2613 f331110f bellard
2614 f331110f bellard
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
2615 f331110f bellard
{
2616 f331110f bellard
    PollingEntry **ppe, *pe;
2617 f331110f bellard
    pe = qemu_mallocz(sizeof(PollingEntry));
2618 f331110f bellard
    if (!pe)
2619 f331110f bellard
        return -1;
2620 f331110f bellard
    pe->func = func;
2621 f331110f bellard
    pe->opaque = opaque;
2622 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
2623 f331110f bellard
    *ppe = pe;
2624 f331110f bellard
    return 0;
2625 f331110f bellard
}
2626 f331110f bellard
2627 f331110f bellard
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
2628 f331110f bellard
{
2629 f331110f bellard
    PollingEntry **ppe, *pe;
2630 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
2631 f331110f bellard
        pe = *ppe;
2632 f331110f bellard
        if (pe->func == func && pe->opaque == opaque) {
2633 f331110f bellard
            *ppe = pe->next;
2634 f331110f bellard
            qemu_free(pe);
2635 f331110f bellard
            break;
2636 f331110f bellard
        }
2637 f331110f bellard
    }
2638 f331110f bellard
}
2639 f331110f bellard
2640 a18e524a bellard
/***********************************************************/
2641 a18e524a bellard
/* Wait objects support */
2642 a18e524a bellard
typedef struct WaitObjects {
2643 a18e524a bellard
    int num;
2644 a18e524a bellard
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
2645 a18e524a bellard
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
2646 a18e524a bellard
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
2647 a18e524a bellard
} WaitObjects;
2648 a18e524a bellard
2649 a18e524a bellard
static WaitObjects wait_objects = {0};
2650 3b46e624 ths
2651 a18e524a bellard
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2652 a18e524a bellard
{
2653 a18e524a bellard
    WaitObjects *w = &wait_objects;
2654 a18e524a bellard
2655 a18e524a bellard
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
2656 a18e524a bellard
        return -1;
2657 a18e524a bellard
    w->events[w->num] = handle;
2658 a18e524a bellard
    w->func[w->num] = func;
2659 a18e524a bellard
    w->opaque[w->num] = opaque;
2660 a18e524a bellard
    w->num++;
2661 a18e524a bellard
    return 0;
2662 a18e524a bellard
}
2663 a18e524a bellard
2664 a18e524a bellard
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
2665 a18e524a bellard
{
2666 a18e524a bellard
    int i, found;
2667 a18e524a bellard
    WaitObjects *w = &wait_objects;
2668 a18e524a bellard
2669 a18e524a bellard
    found = 0;
2670 a18e524a bellard
    for (i = 0; i < w->num; i++) {
2671 a18e524a bellard
        if (w->events[i] == handle)
2672 a18e524a bellard
            found = 1;
2673 a18e524a bellard
        if (found) {
2674 a18e524a bellard
            w->events[i] = w->events[i + 1];
2675 a18e524a bellard
            w->func[i] = w->func[i + 1];
2676 a18e524a bellard
            w->opaque[i] = w->opaque[i + 1];
2677 3b46e624 ths
        }
2678 a18e524a bellard
    }
2679 a18e524a bellard
    if (found)
2680 a18e524a bellard
        w->num--;
2681 a18e524a bellard
}
2682 a18e524a bellard
#endif
2683 a18e524a bellard
2684 210f41ba aliguori
#define SELF_ANNOUNCE_ROUNDS 5
2685 210f41ba aliguori
#define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
2686 210f41ba aliguori
//#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
2687 210f41ba aliguori
#define EXPERIMENTAL_MAGIC 0xf1f23f4f
2688 210f41ba aliguori
2689 210f41ba aliguori
static int announce_self_create(uint8_t *buf, 
2690 210f41ba aliguori
                                uint8_t *mac_addr)
2691 210f41ba aliguori
{
2692 210f41ba aliguori
    uint32_t magic = EXPERIMENTAL_MAGIC;
2693 210f41ba aliguori
    uint16_t proto = htons(ETH_P_EXPERIMENTAL);
2694 210f41ba aliguori
2695 210f41ba aliguori
    /* FIXME: should we send a different packet (arp/rarp/ping)? */
2696 210f41ba aliguori
2697 210f41ba aliguori
    memset(buf, 0xff, 6);         /* h_dst */
2698 210f41ba aliguori
    memcpy(buf + 6, mac_addr, 6); /* h_src */
2699 210f41ba aliguori
    memcpy(buf + 12, &proto, 2);  /* h_proto */
2700 210f41ba aliguori
    memcpy(buf + 14, &magic, 4);  /* magic */
2701 210f41ba aliguori
2702 210f41ba aliguori
    return 18; /* len */
2703 210f41ba aliguori
}
2704 210f41ba aliguori
2705 210f41ba aliguori
void qemu_announce_self(void)
2706 210f41ba aliguori
{
2707 210f41ba aliguori
    int i, j, len;
2708 210f41ba aliguori
    VLANState *vlan;
2709 210f41ba aliguori
    VLANClientState *vc;
2710 210f41ba aliguori
    uint8_t buf[256];
2711 210f41ba aliguori
2712 210f41ba aliguori
    for (i = 0; i < nb_nics; i++) {
2713 210f41ba aliguori
        len = announce_self_create(buf, nd_table[i].macaddr);
2714 210f41ba aliguori
        vlan = nd_table[i].vlan;
2715 210f41ba aliguori
        for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
2716 17e90973 aliguori
            for (j=0; j < SELF_ANNOUNCE_ROUNDS; j++)
2717 17e90973 aliguori
                vc->fd_read(vc->opaque, buf, len);
2718 210f41ba aliguori
        }
2719 210f41ba aliguori
    }
2720 210f41ba aliguori
}
2721 210f41ba aliguori
2722 f331110f bellard
/***********************************************************/
2723 8a7ddc38 bellard
/* savevm/loadvm support */
2724 8a7ddc38 bellard
2725 faea38e7 bellard
#define IO_BUF_SIZE 32768
2726 faea38e7 bellard
2727 faea38e7 bellard
struct QEMUFile {
2728 5dafc53f aliguori
    QEMUFilePutBufferFunc *put_buffer;
2729 5dafc53f aliguori
    QEMUFileGetBufferFunc *get_buffer;
2730 5dafc53f aliguori
    QEMUFileCloseFunc *close;
2731 5dafc53f aliguori
    QEMUFileRateLimit *rate_limit;
2732 5dafc53f aliguori
    void *opaque;
2733 871d2f07 aliguori
    int is_write;
2734 5dafc53f aliguori
2735 faea38e7 bellard
    int64_t buf_offset; /* start of buffer when writing, end of buffer
2736 faea38e7 bellard
                           when reading */
2737 faea38e7 bellard
    int buf_index;
2738 faea38e7 bellard
    int buf_size; /* 0 when writing */
2739 faea38e7 bellard
    uint8_t buf[IO_BUF_SIZE];
2740 871d2f07 aliguori
2741 871d2f07 aliguori
    int has_error;
2742 faea38e7 bellard
};
2743 faea38e7 bellard
2744 c1d36665 aliguori
typedef struct QEMUFileSocket
2745 5dafc53f aliguori
{
2746 5dafc53f aliguori
    int fd;
2747 5dafc53f aliguori
    QEMUFile *file;
2748 c1d36665 aliguori
} QEMUFileSocket;
2749 5dafc53f aliguori
2750 c1d36665 aliguori
static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
2751 5dafc53f aliguori
{
2752 c1d36665 aliguori
    QEMUFileSocket *s = opaque;
2753 5dafc53f aliguori
    ssize_t len;
2754 5dafc53f aliguori
2755 5dafc53f aliguori
    do {
2756 c1d36665 aliguori
        len = recv(s->fd, buf, size, 0);
2757 c1d36665 aliguori
    } while (len == -1 && socket_error() == EINTR);
2758 5dafc53f aliguori
2759 5dafc53f aliguori
    if (len == -1)
2760 c1d36665 aliguori
        len = -socket_error();
2761 5dafc53f aliguori
2762 5dafc53f aliguori
    return len;
2763 5dafc53f aliguori
}
2764 5dafc53f aliguori
2765 c1d36665 aliguori
static int socket_close(void *opaque)
2766 5dafc53f aliguori
{
2767 c1d36665 aliguori
    QEMUFileSocket *s = opaque;
2768 5dafc53f aliguori
    qemu_free(s);
2769 5dafc53f aliguori
    return 0;
2770 5dafc53f aliguori
}
2771 5dafc53f aliguori
2772 c1d36665 aliguori
QEMUFile *qemu_fopen_socket(int fd)
2773 5dafc53f aliguori
{
2774 c1d36665 aliguori
    QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
2775 5dafc53f aliguori
2776 5dafc53f aliguori
    if (s == NULL)
2777 5dafc53f aliguori
        return NULL;
2778 5dafc53f aliguori
2779 5dafc53f aliguori
    s->fd = fd;
2780 c1d36665 aliguori
    s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL);
2781 5dafc53f aliguori
    return s->file;
2782 5dafc53f aliguori
}
2783 5dafc53f aliguori
2784 5dafc53f aliguori
typedef struct QEMUFileStdio
2785 5dafc53f aliguori
{
2786 5dafc53f aliguori
    FILE *outfile;
2787 5dafc53f aliguori
} QEMUFileStdio;
2788 5dafc53f aliguori
2789 871d2f07 aliguori
static int file_put_buffer(void *opaque, const uint8_t *buf,
2790 5dafc53f aliguori
                            int64_t pos, int size)
2791 5dafc53f aliguori
{
2792 5dafc53f aliguori
    QEMUFileStdio *s = opaque;
2793 5dafc53f aliguori
    fseek(s->outfile, pos, SEEK_SET);
2794 5dafc53f aliguori
    fwrite(buf, 1, size, s->outfile);
2795 871d2f07 aliguori
    return size;
2796 5dafc53f aliguori
}
2797 5dafc53f aliguori
2798 5dafc53f aliguori
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
2799 5dafc53f aliguori
{
2800 5dafc53f aliguori
    QEMUFileStdio *s = opaque;
2801 5dafc53f aliguori
    fseek(s->outfile, pos, SEEK_SET);
2802 5dafc53f aliguori
    return fread(buf, 1, size, s->outfile);
2803 5dafc53f aliguori
}
2804 5dafc53f aliguori
2805 5dafc53f aliguori
static int file_close(void *opaque)
2806 5dafc53f aliguori
{
2807 5dafc53f aliguori
    QEMUFileStdio *s = opaque;
2808 5dafc53f aliguori
    fclose(s->outfile);
2809 5dafc53f aliguori
    qemu_free(s);
2810 5dafc53f aliguori
    return 0;
2811 5dafc53f aliguori
}
2812 5dafc53f aliguori
2813 faea38e7 bellard
QEMUFile *qemu_fopen(const char *filename, const char *mode)
2814 faea38e7 bellard
{
2815 5dafc53f aliguori
    QEMUFileStdio *s;
2816 faea38e7 bellard
2817 5dafc53f aliguori
    s = qemu_mallocz(sizeof(QEMUFileStdio));
2818 5dafc53f aliguori
    if (!s)
2819 faea38e7 bellard
        return NULL;
2820 5dafc53f aliguori
2821 5dafc53f aliguori
    s->outfile = fopen(filename, mode);
2822 5dafc53f aliguori
    if (!s->outfile)
2823 faea38e7 bellard
        goto fail;
2824 5dafc53f aliguori
2825 5dafc53f aliguori
    if (!strcmp(mode, "wb"))
2826 5dafc53f aliguori
        return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL);
2827 5dafc53f aliguori
    else if (!strcmp(mode, "rb"))
2828 5dafc53f aliguori
        return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL);
2829 5dafc53f aliguori
2830 5dafc53f aliguori
fail:
2831 5dafc53f aliguori
    if (s->outfile)
2832 5dafc53f aliguori
        fclose(s->outfile);
2833 5dafc53f aliguori
    qemu_free(s);
2834 faea38e7 bellard
    return NULL;
2835 faea38e7 bellard
}
2836 faea38e7 bellard
2837 5dafc53f aliguori
typedef struct QEMUFileBdrv
2838 5dafc53f aliguori
{
2839 5dafc53f aliguori
    BlockDriverState *bs;
2840 5dafc53f aliguori
    int64_t base_offset;
2841 5dafc53f aliguori
} QEMUFileBdrv;
2842 5dafc53f aliguori
2843 871d2f07 aliguori
static int bdrv_put_buffer(void *opaque, const uint8_t *buf,
2844 871d2f07 aliguori
                           int64_t pos, int size)
2845 5dafc53f aliguori
{
2846 5dafc53f aliguori
    QEMUFileBdrv *s = opaque;
2847 5dafc53f aliguori
    bdrv_pwrite(s->bs, s->base_offset + pos, buf, size);
2848 871d2f07 aliguori
    return size;
2849 5dafc53f aliguori
}
2850 5dafc53f aliguori
2851 5dafc53f aliguori
static int bdrv_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
2852 5dafc53f aliguori
{
2853 5dafc53f aliguori
    QEMUFileBdrv *s = opaque;
2854 5dafc53f aliguori
    return bdrv_pread(s->bs, s->base_offset + pos, buf, size);
2855 5dafc53f aliguori
}
2856 5dafc53f aliguori
2857 5dafc53f aliguori
static int bdrv_fclose(void *opaque)
2858 5dafc53f aliguori
{
2859 5dafc53f aliguori
    QEMUFileBdrv *s = opaque;
2860 5dafc53f aliguori
    qemu_free(s);
2861 5dafc53f aliguori
    return 0;
2862 5dafc53f aliguori
}
2863 5dafc53f aliguori
2864 674bb261 blueswir1
static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
2865 5dafc53f aliguori
{
2866 5dafc53f aliguori
    QEMUFileBdrv *s;
2867 5dafc53f aliguori
2868 5dafc53f aliguori
    s = qemu_mallocz(sizeof(QEMUFileBdrv));
2869 5dafc53f aliguori
    if (!s)
2870 5dafc53f aliguori
        return NULL;
2871 5dafc53f aliguori
2872 5dafc53f aliguori
    s->bs = bs;
2873 5dafc53f aliguori
    s->base_offset = offset;
2874 5dafc53f aliguori
2875 5dafc53f aliguori
    if (is_writable)
2876 5dafc53f aliguori
        return qemu_fopen_ops(s, bdrv_put_buffer, NULL, bdrv_fclose, NULL);
2877 5dafc53f aliguori
2878 5dafc53f aliguori
    return qemu_fopen_ops(s, NULL, bdrv_get_buffer, bdrv_fclose, NULL);
2879 5dafc53f aliguori
}
2880 5dafc53f aliguori
2881 5dafc53f aliguori
QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
2882 5dafc53f aliguori
                         QEMUFileGetBufferFunc *get_buffer,
2883 5dafc53f aliguori
                         QEMUFileCloseFunc *close,
2884 5dafc53f aliguori
                         QEMUFileRateLimit *rate_limit)
2885 faea38e7 bellard
{
2886 faea38e7 bellard
    QEMUFile *f;
2887 faea38e7 bellard
2888 faea38e7 bellard
    f = qemu_mallocz(sizeof(QEMUFile));
2889 faea38e7 bellard
    if (!f)
2890 faea38e7 bellard
        return NULL;
2891 5dafc53f aliguori
2892 5dafc53f aliguori
    f->opaque = opaque;
2893 5dafc53f aliguori
    f->put_buffer = put_buffer;
2894 5dafc53f aliguori
    f->get_buffer = get_buffer;
2895 5dafc53f aliguori
    f->close = close;
2896 5dafc53f aliguori
    f->rate_limit = rate_limit;
2897 871d2f07 aliguori
    f->is_write = 0;
2898 5dafc53f aliguori
2899 faea38e7 bellard
    return f;
2900 faea38e7 bellard
}
2901 faea38e7 bellard
2902 871d2f07 aliguori
int qemu_file_has_error(QEMUFile *f)
2903 871d2f07 aliguori
{
2904 871d2f07 aliguori
    return f->has_error;
2905 871d2f07 aliguori
}
2906 871d2f07 aliguori
2907 faea38e7 bellard
void qemu_fflush(QEMUFile *f)
2908 faea38e7 bellard
{
2909 5dafc53f aliguori
    if (!f->put_buffer)
2910 faea38e7 bellard
        return;
2911 5dafc53f aliguori
2912 871d2f07 aliguori
    if (f->is_write && f->buf_index > 0) {
2913 871d2f07 aliguori
        int len;
2914 871d2f07 aliguori
2915 871d2f07 aliguori
        len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
2916 871d2f07 aliguori
        if (len > 0)
2917 871d2f07 aliguori
            f->buf_offset += f->buf_index;
2918 871d2f07 aliguori
        else
2919 871d2f07 aliguori
            f->has_error = 1;
2920 faea38e7 bellard
        f->buf_index = 0;
2921 faea38e7 bellard
    }
2922 faea38e7 bellard
}
2923 faea38e7 bellard
2924 faea38e7 bellard
static void qemu_fill_buffer(QEMUFile *f)
2925 faea38e7 bellard
{
2926 faea38e7 bellard
    int len;
2927 faea38e7 bellard
2928 5dafc53f aliguori
    if (!f->get_buffer)
2929 faea38e7 bellard
        return;
2930 5dafc53f aliguori
2931 871d2f07 aliguori
    if (f->is_write)
2932 871d2f07 aliguori
        abort();
2933 5dafc53f aliguori
2934 871d2f07 aliguori
    len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
2935 871d2f07 aliguori
    if (len > 0) {
2936 871d2f07 aliguori
        f->buf_index = 0;
2937 871d2f07 aliguori
        f->buf_size = len;
2938 871d2f07 aliguori
        f->buf_offset += len;
2939 871d2f07 aliguori
    } else if (len != -EAGAIN)
2940 871d2f07 aliguori
        f->has_error = 1;
2941 faea38e7 bellard
}
2942 faea38e7 bellard
2943 5dafc53f aliguori
int qemu_fclose(QEMUFile *f)
2944 faea38e7 bellard
{
2945 5dafc53f aliguori
    int ret = 0;
2946 5dafc53f aliguori
    qemu_fflush(f);
2947 5dafc53f aliguori
    if (f->close)
2948 5dafc53f aliguori
        ret = f->close(f->opaque);
2949 faea38e7 bellard
    qemu_free(f);
2950 5dafc53f aliguori
    return ret;
2951 5dafc53f aliguori
}
2952 5dafc53f aliguori
2953 5dafc53f aliguori
void qemu_file_put_notify(QEMUFile *f)
2954 5dafc53f aliguori
{
2955 5dafc53f aliguori
    f->put_buffer(f->opaque, NULL, 0, 0);
2956 faea38e7 bellard
}
2957 faea38e7 bellard
2958 2ca83a8d blueswir1
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
2959 b4608c04 bellard
{
2960 2ca83a8d blueswir1
    int l;
2961 871d2f07 aliguori
2962 871d2f07 aliguori
    if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
2963 871d2f07 aliguori
        fprintf(stderr,
2964 871d2f07 aliguori
                "Attempted to write to buffer while read buffer is not empty\n");
2965 871d2f07 aliguori
        abort();
2966 871d2f07 aliguori
    }
2967 871d2f07 aliguori
2968 871d2f07 aliguori
    while (!f->has_error && size > 0) {
2969 faea38e7 bellard
        l = IO_BUF_SIZE - f->buf_index;
2970 faea38e7 bellard
        if (l > size)
2971 faea38e7 bellard
            l = size;
2972 faea38e7 bellard
        memcpy(f->buf + f->buf_index, buf, l);
2973 871d2f07 aliguori
        f->is_write = 1;
2974 faea38e7 bellard
        f->buf_index += l;
2975 faea38e7 bellard
        buf += l;
2976 faea38e7 bellard
        size -= l;
2977 faea38e7 bellard
        if (f->buf_index >= IO_BUF_SIZE)
2978 faea38e7 bellard
            qemu_fflush(f);
2979 faea38e7 bellard
    }
2980 b4608c04 bellard
}
2981 b4608c04 bellard
2982 2ca83a8d blueswir1
void qemu_put_byte(QEMUFile *f, int v)
2983 b4608c04 bellard
{
2984 871d2f07 aliguori
    if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
2985 871d2f07 aliguori
        fprintf(stderr,
2986 871d2f07 aliguori
                "Attempted to write to buffer while read buffer is not empty\n");
2987 871d2f07 aliguori
        abort();
2988 871d2f07 aliguori
    }
2989 871d2f07 aliguori
2990 faea38e7 bellard
    f->buf[f->buf_index++] = v;
2991 871d2f07 aliguori
    f->is_write = 1;
2992 faea38e7 bellard
    if (f->buf_index >= IO_BUF_SIZE)
2993 faea38e7 bellard
        qemu_fflush(f);
2994 faea38e7 bellard
}
2995 faea38e7 bellard
2996 2ca83a8d blueswir1
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
2997 faea38e7 bellard
{
2998 2ca83a8d blueswir1
    int size, l;
2999 faea38e7 bellard
3000 871d2f07 aliguori
    if (f->is_write)
3001 871d2f07 aliguori
        abort();
3002 871d2f07 aliguori
3003 faea38e7 bellard
    size = size1;
3004 faea38e7 bellard
    while (size > 0) {
3005 faea38e7 bellard
        l = f->buf_size - f->buf_index;
3006 faea38e7 bellard
        if (l == 0) {
3007 faea38e7 bellard
            qemu_fill_buffer(f);
3008 faea38e7 bellard
            l = f->buf_size - f->buf_index;
3009 faea38e7 bellard
            if (l == 0)
3010 faea38e7 bellard
                break;
3011 faea38e7 bellard
        }
3012 faea38e7 bellard
        if (l > size)
3013 faea38e7 bellard
            l = size;
3014 faea38e7 bellard
        memcpy(buf, f->buf + f->buf_index, l);
3015 faea38e7 bellard
        f->buf_index += l;
3016 faea38e7 bellard
        buf += l;
3017 faea38e7 bellard
        size -= l;
3018 faea38e7 bellard
    }
3019 faea38e7 bellard
    return size1 - size;
3020 faea38e7 bellard
}
3021 faea38e7 bellard
3022 2ca83a8d blueswir1
int qemu_get_byte(QEMUFile *f)
3023 faea38e7 bellard
{
3024 871d2f07 aliguori
    if (f->is_write)
3025 871d2f07 aliguori
        abort();
3026 871d2f07 aliguori
3027 faea38e7 bellard
    if (f->buf_index >= f->buf_size) {
3028 faea38e7 bellard
        qemu_fill_buffer(f);
3029 faea38e7 bellard
        if (f->buf_index >= f->buf_size)
3030 faea38e7 bellard
            return 0;
3031 faea38e7 bellard
    }
3032 faea38e7 bellard
    return f->buf[f->buf_index++];
3033 faea38e7 bellard
}
3034 faea38e7 bellard
3035 faea38e7 bellard
int64_t qemu_ftell(QEMUFile *f)
3036 faea38e7 bellard
{
3037 faea38e7 bellard
    return f->buf_offset - f->buf_size + f->buf_index;
3038 faea38e7 bellard
}
3039 faea38e7 bellard
3040 faea38e7 bellard
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
3041 faea38e7 bellard
{
3042 faea38e7 bellard
    if (whence == SEEK_SET) {
3043 faea38e7 bellard
        /* nothing to do */
3044 faea38e7 bellard
    } else if (whence == SEEK_CUR) {
3045 faea38e7 bellard
        pos += qemu_ftell(f);
3046 faea38e7 bellard
    } else {
3047 faea38e7 bellard
        /* SEEK_END not supported */
3048 faea38e7 bellard
        return -1;
3049 faea38e7 bellard
    }
3050 5dafc53f aliguori
    if (f->put_buffer) {
3051 faea38e7 bellard
        qemu_fflush(f);
3052 faea38e7 bellard
        f->buf_offset = pos;
3053 faea38e7 bellard
    } else {
3054 faea38e7 bellard
        f->buf_offset = pos;
3055 faea38e7 bellard
        f->buf_index = 0;
3056 faea38e7 bellard
        f->buf_size = 0;
3057 faea38e7 bellard
    }
3058 faea38e7 bellard
    return pos;
3059 8a7ddc38 bellard
}
3060 8a7ddc38 bellard
3061 5dafc53f aliguori
int qemu_file_rate_limit(QEMUFile *f)
3062 5dafc53f aliguori
{
3063 5dafc53f aliguori
    if (f->rate_limit)
3064 5dafc53f aliguori
        return f->rate_limit(f->opaque);
3065 5dafc53f aliguori
3066 5dafc53f aliguori
    return 0;
3067 5dafc53f aliguori
}
3068 5dafc53f aliguori
3069 2ca83a8d blueswir1
void qemu_put_be16(QEMUFile *f, unsigned int v)
3070 8a7ddc38 bellard
{
3071 8a7ddc38 bellard
    qemu_put_byte(f, v >> 8);
3072 8a7ddc38 bellard
    qemu_put_byte(f, v);
3073 8a7ddc38 bellard
}
3074 8a7ddc38 bellard
3075 2ca83a8d blueswir1
void qemu_put_be32(QEMUFile *f, unsigned int v)
3076 8a7ddc38 bellard
{
3077 8a7ddc38 bellard
    qemu_put_byte(f, v >> 24);
3078 8a7ddc38 bellard
    qemu_put_byte(f, v >> 16);
3079 8a7ddc38 bellard
    qemu_put_byte(f, v >> 8);
3080 8a7ddc38 bellard
    qemu_put_byte(f, v);
3081 8a7ddc38 bellard
}
3082 8a7ddc38 bellard
3083 8a7ddc38 bellard
void qemu_put_be64(QEMUFile *f, uint64_t v)
3084 8a7ddc38 bellard
{
3085 8a7ddc38 bellard
    qemu_put_be32(f, v >> 32);
3086 8a7ddc38 bellard
    qemu_put_be32(f, v);
3087 8a7ddc38 bellard
}
3088 8a7ddc38 bellard
3089 2ca83a8d blueswir1
unsigned int qemu_get_be16(QEMUFile *f)
3090 8a7ddc38 bellard
{
3091 2ca83a8d blueswir1
    unsigned int v;
3092 8a7ddc38 bellard
    v = qemu_get_byte(f) << 8;
3093 8a7ddc38 bellard
    v |= qemu_get_byte(f);
3094 8a7ddc38 bellard
    return v;
3095 8a7ddc38 bellard
}
3096 8a7ddc38 bellard
3097 2ca83a8d blueswir1
unsigned int qemu_get_be32(QEMUFile *f)
3098 8a7ddc38 bellard
{
3099 2ca83a8d blueswir1
    unsigned int v;
3100 8a7ddc38 bellard
    v = qemu_get_byte(f) << 24;
3101 8a7ddc38 bellard
    v |= qemu_get_byte(f) << 16;
3102 8a7ddc38 bellard
    v |= qemu_get_byte(f) << 8;
3103 8a7ddc38 bellard
    v |= qemu_get_byte(f);
3104 8a7ddc38 bellard
    return v;
3105 8a7ddc38 bellard
}
3106 8a7ddc38 bellard
3107 8a7ddc38 bellard
uint64_t qemu_get_be64(QEMUFile *f)
3108 8a7ddc38 bellard
{
3109 8a7ddc38 bellard
    uint64_t v;
3110 8a7ddc38 bellard
    v = (uint64_t)qemu_get_be32(f) << 32;
3111 8a7ddc38 bellard
    v |= qemu_get_be32(f);
3112 8a7ddc38 bellard
    return v;
3113 8a7ddc38 bellard
}
3114 8a7ddc38 bellard
3115 8a7ddc38 bellard
typedef struct SaveStateEntry {
3116 8a7ddc38 bellard
    char idstr[256];
3117 8a7ddc38 bellard
    int instance_id;
3118 8a7ddc38 bellard
    int version_id;
3119 9366f418 aliguori
    int section_id;
3120 9366f418 aliguori
    SaveLiveStateHandler *save_live_state;
3121 8a7ddc38 bellard
    SaveStateHandler *save_state;
3122 8a7ddc38 bellard
    LoadStateHandler *load_state;
3123 8a7ddc38 bellard
    void *opaque;
3124 8a7ddc38 bellard
    struct SaveStateEntry *next;
3125 8a7ddc38 bellard
} SaveStateEntry;
3126 b4608c04 bellard
3127 8a7ddc38 bellard
static SaveStateEntry *first_se;
3128 8a7ddc38 bellard
3129 18be5187 pbrook
/* TODO: Individual devices generally have very little idea about the rest
3130 18fdb1c5 ths
   of the system, so instance_id should be removed/replaced.
3131 18fdb1c5 ths
   Meanwhile pass -1 as instance_id if you do not already have a clearly
3132 18fdb1c5 ths
   distinguishing id for all instances of your device class. */
3133 9366f418 aliguori
int register_savevm_live(const char *idstr,
3134 9366f418 aliguori
                         int instance_id,
3135 9366f418 aliguori
                         int version_id,
3136 9366f418 aliguori
                         SaveLiveStateHandler *save_live_state,
3137 9366f418 aliguori
                         SaveStateHandler *save_state,
3138 9366f418 aliguori
                         LoadStateHandler *load_state,
3139 9366f418 aliguori
                         void *opaque)
3140 8a7ddc38 bellard
{
3141 8a7ddc38 bellard
    SaveStateEntry *se, **pse;
3142 9366f418 aliguori
    static int global_section_id;
3143 8a7ddc38 bellard
3144 8a7ddc38 bellard
    se = qemu_malloc(sizeof(SaveStateEntry));
3145 8a7ddc38 bellard
    if (!se)
3146 8a7ddc38 bellard
        return -1;
3147 8a7ddc38 bellard
    pstrcpy(se->idstr, sizeof(se->idstr), idstr);
3148 18be5187 pbrook
    se->instance_id = (instance_id == -1) ? 0 : instance_id;
3149 8a7ddc38 bellard
    se->version_id = version_id;
3150 9366f418 aliguori
    se->section_id = global_section_id++;
3151 9366f418 aliguori
    se->save_live_state = save_live_state;
3152 8a7ddc38 bellard
    se->save_state = save_state;
3153 8a7ddc38 bellard
    se->load_state = load_state;
3154 8a7ddc38 bellard
    se->opaque = opaque;
3155 8a7ddc38 bellard
    se->next = NULL;
3156 8a7ddc38 bellard
3157 8a7ddc38 bellard
    /* add at the end of list */
3158 8a7ddc38 bellard
    pse = &first_se;
3159 18be5187 pbrook
    while (*pse != NULL) {
3160 18be5187 pbrook
        if (instance_id == -1
3161 18be5187 pbrook
                && strcmp(se->idstr, (*pse)->idstr) == 0
3162 18be5187 pbrook
                && se->instance_id <= (*pse)->instance_id)
3163 18be5187 pbrook
            se->instance_id = (*pse)->instance_id + 1;
3164 8a7ddc38 bellard
        pse = &(*pse)->next;
3165 18be5187 pbrook
    }
3166 8a7ddc38 bellard
    *pse = se;
3167 8a7ddc38 bellard
    return 0;
3168 8a7ddc38 bellard
}
3169 8a7ddc38 bellard
3170 9366f418 aliguori
int register_savevm(const char *idstr,
3171 9366f418 aliguori
                    int instance_id,
3172 9366f418 aliguori
                    int version_id,
3173 9366f418 aliguori
                    SaveStateHandler *save_state,
3174 9366f418 aliguori
                    LoadStateHandler *load_state,
3175 9366f418 aliguori
                    void *opaque)
3176 9366f418 aliguori
{
3177 9366f418 aliguori
    return register_savevm_live(idstr, instance_id, version_id,
3178 9366f418 aliguori
                                NULL, save_state, load_state, opaque);
3179 9366f418 aliguori
}
3180 9366f418 aliguori
3181 9366f418 aliguori
#define QEMU_VM_FILE_MAGIC           0x5145564d
3182 9366f418 aliguori
#define QEMU_VM_FILE_VERSION_COMPAT  0x00000002
3183 9366f418 aliguori
#define QEMU_VM_FILE_VERSION         0x00000003
3184 9366f418 aliguori
3185 9366f418 aliguori
#define QEMU_VM_EOF                  0x00
3186 9366f418 aliguori
#define QEMU_VM_SECTION_START        0x01
3187 9366f418 aliguori
#define QEMU_VM_SECTION_PART         0x02
3188 9366f418 aliguori
#define QEMU_VM_SECTION_END          0x03
3189 9366f418 aliguori
#define QEMU_VM_SECTION_FULL         0x04
3190 8a7ddc38 bellard
3191 9366f418 aliguori
int qemu_savevm_state_begin(QEMUFile *f)
3192 8a7ddc38 bellard
{
3193 8a7ddc38 bellard
    SaveStateEntry *se;
3194 313aa567 bellard
3195 8a7ddc38 bellard
    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
3196 8a7ddc38 bellard
    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
3197 9366f418 aliguori
3198 9366f418 aliguori
    for (se = first_se; se != NULL; se = se->next) {
3199 9366f418 aliguori
        int len;
3200 9366f418 aliguori
3201 9366f418 aliguori
        if (se->save_live_state == NULL)
3202 9366f418 aliguori
            continue;
3203 9366f418 aliguori
3204 9366f418 aliguori
        /* Section type */
3205 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_START);
3206 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3207 9366f418 aliguori
3208 9366f418 aliguori
        /* ID string */
3209 9366f418 aliguori
        len = strlen(se->idstr);
3210 9366f418 aliguori
        qemu_put_byte(f, len);
3211 9366f418 aliguori
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
3212 9366f418 aliguori
3213 9366f418 aliguori
        qemu_put_be32(f, se->instance_id);
3214 9366f418 aliguori
        qemu_put_be32(f, se->version_id);
3215 9366f418 aliguori
3216 9366f418 aliguori
        se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
3217 9366f418 aliguori
    }
3218 9366f418 aliguori
3219 871d2f07 aliguori
    if (qemu_file_has_error(f))
3220 871d2f07 aliguori
        return -EIO;
3221 871d2f07 aliguori
3222 9366f418 aliguori
    return 0;
3223 9366f418 aliguori
}
3224 9366f418 aliguori
3225 9366f418 aliguori
int qemu_savevm_state_iterate(QEMUFile *f)
3226 9366f418 aliguori
{
3227 9366f418 aliguori
    SaveStateEntry *se;
3228 475e4277 aliguori
    int ret = 1;
3229 9366f418 aliguori
3230 9366f418 aliguori
    for (se = first_se; se != NULL; se = se->next) {
3231 9366f418 aliguori
        if (se->save_live_state == NULL)
3232 9366f418 aliguori
            continue;
3233 9366f418 aliguori
3234 9366f418 aliguori
        /* Section type */
3235 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_PART);
3236 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3237 9366f418 aliguori
3238 475e4277 aliguori
        ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
3239 9366f418 aliguori
    }
3240 9366f418 aliguori
3241 9366f418 aliguori
    if (ret)
3242 9366f418 aliguori
        return 1;
3243 9366f418 aliguori
3244 871d2f07 aliguori
    if (qemu_file_has_error(f))
3245 871d2f07 aliguori
        return -EIO;
3246 871d2f07 aliguori
3247 9366f418 aliguori
    return 0;
3248 9366f418 aliguori
}
3249 9366f418 aliguori
3250 9366f418 aliguori
int qemu_savevm_state_complete(QEMUFile *f)
3251 9366f418 aliguori
{
3252 9366f418 aliguori
    SaveStateEntry *se;
3253 9366f418 aliguori
3254 9366f418 aliguori
    for (se = first_se; se != NULL; se = se->next) {
3255 9366f418 aliguori
        if (se->save_live_state == NULL)
3256 9366f418 aliguori
            continue;
3257 9366f418 aliguori
3258 9366f418 aliguori
        /* Section type */
3259 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_END);
3260 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3261 9366f418 aliguori
3262 9366f418 aliguori
        se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
3263 9366f418 aliguori
    }
3264 8a7ddc38 bellard
3265 8a7ddc38 bellard
    for(se = first_se; se != NULL; se = se->next) {
3266 9366f418 aliguori
        int len;
3267 9366f418 aliguori
3268 d978c02c aurel32
        if (se->save_state == NULL)
3269 d978c02c aurel32
            continue;
3270 d978c02c aurel32
3271 9366f418 aliguori
        /* Section type */
3272 9366f418 aliguori
        qemu_put_byte(f, QEMU_VM_SECTION_FULL);
3273 9366f418 aliguori
        qemu_put_be32(f, se->section_id);
3274 9366f418 aliguori
3275 8a7ddc38 bellard
        /* ID string */
3276 8a7ddc38 bellard
        len = strlen(se->idstr);
3277 8a7ddc38 bellard
        qemu_put_byte(f, len);
3278 ffe8ab83 ths
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
3279 8a7ddc38 bellard
3280 8a7ddc38 bellard
        qemu_put_be32(f, se->instance_id);
3281 8a7ddc38 bellard
        qemu_put_be32(f, se->version_id);
3282 8a7ddc38 bellard
3283 8a7ddc38 bellard
        se->save_state(f, se->opaque);
3284 8a7ddc38 bellard
    }
3285 8a7ddc38 bellard
3286 9366f418 aliguori
    qemu_put_byte(f, QEMU_VM_EOF);
3287 9366f418 aliguori
3288 871d2f07 aliguori
    if (qemu_file_has_error(f))
3289 871d2f07 aliguori
        return -EIO;
3290 871d2f07 aliguori
3291 9366f418 aliguori
    return 0;
3292 9366f418 aliguori
}
3293 9366f418 aliguori
3294 9366f418 aliguori
int qemu_savevm_state(QEMUFile *f)
3295 9366f418 aliguori
{
3296 9366f418 aliguori
    int saved_vm_running;
3297 9366f418 aliguori
    int ret;
3298 9366f418 aliguori
3299 9366f418 aliguori
    saved_vm_running = vm_running;
3300 9366f418 aliguori
    vm_stop(0);
3301 9366f418 aliguori
3302 5bb7910a aliguori
    bdrv_flush_all();
3303 5bb7910a aliguori
3304 9366f418 aliguori
    ret = qemu_savevm_state_begin(f);
3305 9366f418 aliguori
    if (ret < 0)
3306 9366f418 aliguori
        goto out;
3307 9366f418 aliguori
3308 9366f418 aliguori
    do {
3309 9366f418 aliguori
        ret = qemu_savevm_state_iterate(f);
3310 9366f418 aliguori
        if (ret < 0)
3311 9366f418 aliguori
            goto out;
3312 475e4277 aliguori
    } while (ret == 0);
3313 9366f418 aliguori
3314 9366f418 aliguori
    ret = qemu_savevm_state_complete(f);
3315 9366f418 aliguori
3316 9366f418 aliguori
out:
3317 871d2f07 aliguori
    if (qemu_file_has_error(f))
3318 871d2f07 aliguori
        ret = -EIO;
3319 871d2f07 aliguori
3320 871d2f07 aliguori
    if (!ret && saved_vm_running)
3321 9366f418 aliguori
        vm_start();
3322 871d2f07 aliguori
3323 8a7ddc38 bellard
    return ret;
3324 8a7ddc38 bellard
}
3325 8a7ddc38 bellard
3326 8a7ddc38 bellard
static SaveStateEntry *find_se(const char *idstr, int instance_id)
3327 8a7ddc38 bellard
{
3328 8a7ddc38 bellard
    SaveStateEntry *se;
3329 8a7ddc38 bellard
3330 8a7ddc38 bellard
    for(se = first_se; se != NULL; se = se->next) {
3331 5fafdf24 ths
        if (!strcmp(se->idstr, idstr) &&
3332 8a7ddc38 bellard
            instance_id == se->instance_id)
3333 8a7ddc38 bellard
            return se;
3334 8a7ddc38 bellard
    }
3335 8a7ddc38 bellard
    return NULL;
3336 8a7ddc38 bellard
}
3337 8a7ddc38 bellard
3338 9366f418 aliguori
typedef struct LoadStateEntry {
3339 9366f418 aliguori
    SaveStateEntry *se;
3340 9366f418 aliguori
    int section_id;
3341 9366f418 aliguori
    int version_id;
3342 9366f418 aliguori
    struct LoadStateEntry *next;
3343 9366f418 aliguori
} LoadStateEntry;
3344 9366f418 aliguori
3345 9366f418 aliguori
static int qemu_loadvm_state_v2(QEMUFile *f)
3346 8a7ddc38 bellard
{
3347 8a7ddc38 bellard
    SaveStateEntry *se;
3348 faea38e7 bellard
    int len, ret, instance_id, record_len, version_id;
3349 faea38e7 bellard
    int64_t total_len, end_pos, cur_pos;
3350 8a7ddc38 bellard
    char idstr[256];
3351 3b46e624 ths
3352 faea38e7 bellard
    total_len = qemu_get_be64(f);
3353 faea38e7 bellard
    end_pos = total_len + qemu_ftell(f);
3354 b4608c04 bellard
    for(;;) {
3355 faea38e7 bellard
        if (qemu_ftell(f) >= end_pos)
3356 cd4c3e88 bellard
            break;
3357 faea38e7 bellard
        len = qemu_get_byte(f);
3358 ffe8ab83 ths
        qemu_get_buffer(f, (uint8_t *)idstr, len);
3359 8a7ddc38 bellard
        idstr[len] = '\0';
3360 8a7ddc38 bellard
        instance_id = qemu_get_be32(f);
3361 8a7ddc38 bellard
        version_id = qemu_get_be32(f);
3362 8a7ddc38 bellard
        record_len = qemu_get_be32(f);
3363 faea38e7 bellard
        cur_pos = qemu_ftell(f);
3364 8a7ddc38 bellard
        se = find_se(idstr, instance_id);
3365 8a7ddc38 bellard
        if (!se) {
3366 5fafdf24 ths
            fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3367 8a7ddc38 bellard
                    instance_id, idstr);
3368 8a7ddc38 bellard
        } else {
3369 8a7ddc38 bellard
            ret = se->load_state(f, se->opaque, version_id);
3370 8a7ddc38 bellard
            if (ret < 0) {
3371 5fafdf24 ths
                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3372 8a7ddc38 bellard
                        instance_id, idstr);
3373 8a7ddc38 bellard
            }
3374 34865134 bellard
        }
3375 8a7ddc38 bellard
        /* always seek to exact end of record */
3376 8a7ddc38 bellard
        qemu_fseek(f, cur_pos + record_len, SEEK_SET);
3377 8a7ddc38 bellard
    }
3378 871d2f07 aliguori
3379 871d2f07 aliguori
    if (qemu_file_has_error(f))
3380 871d2f07 aliguori
        return -EIO;
3381 871d2f07 aliguori
3382 9366f418 aliguori
    return 0;
3383 9366f418 aliguori
}
3384 9366f418 aliguori
3385 9366f418 aliguori
int qemu_loadvm_state(QEMUFile *f)
3386 9366f418 aliguori
{
3387 9366f418 aliguori
    LoadStateEntry *first_le = NULL;
3388 9366f418 aliguori
    uint8_t section_type;
3389 9366f418 aliguori
    unsigned int v;
3390 9366f418 aliguori
    int ret;
3391 9366f418 aliguori
3392 9366f418 aliguori
    v = qemu_get_be32(f);
3393 9366f418 aliguori
    if (v != QEMU_VM_FILE_MAGIC)
3394 9366f418 aliguori
        return -EINVAL;
3395 9366f418 aliguori
3396 9366f418 aliguori
    v = qemu_get_be32(f);
3397 9366f418 aliguori
    if (v == QEMU_VM_FILE_VERSION_COMPAT)
3398 9366f418 aliguori
        return qemu_loadvm_state_v2(f);
3399 9366f418 aliguori
    if (v != QEMU_VM_FILE_VERSION)
3400 9366f418 aliguori
        return -ENOTSUP;
3401 9366f418 aliguori
3402 9366f418 aliguori
    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
3403 9366f418 aliguori
        uint32_t instance_id, version_id, section_id;
3404 9366f418 aliguori
        LoadStateEntry *le;
3405 9366f418 aliguori
        SaveStateEntry *se;
3406 9366f418 aliguori
        char idstr[257];
3407 9366f418 aliguori
        int len;
3408 9366f418 aliguori
3409 9366f418 aliguori
        switch (section_type) {
3410 9366f418 aliguori
        case QEMU_VM_SECTION_START:
3411 9366f418 aliguori
        case QEMU_VM_SECTION_FULL:
3412 9366f418 aliguori
            /* Read section start */
3413 9366f418 aliguori
            section_id = qemu_get_be32(f);
3414 9366f418 aliguori
            len = qemu_get_byte(f);
3415 9366f418 aliguori
            qemu_get_buffer(f, (uint8_t *)idstr, len);
3416 9366f418 aliguori
            idstr[len] = 0;
3417 9366f418 aliguori
            instance_id = qemu_get_be32(f);
3418 9366f418 aliguori
            version_id = qemu_get_be32(f);
3419 9366f418 aliguori
3420 9366f418 aliguori
            /* Find savevm section */
3421 9366f418 aliguori
            se = find_se(idstr, instance_id);
3422 9366f418 aliguori
            if (se == NULL) {
3423 9366f418 aliguori
                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
3424 9366f418 aliguori
                ret = -EINVAL;
3425 9366f418 aliguori
                goto out;
3426 9366f418 aliguori
            }
3427 9366f418 aliguori
3428 9366f418 aliguori
            /* Validate version */
3429 9366f418 aliguori
            if (version_id > se->version_id) {
3430 9366f418 aliguori
                fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
3431 9366f418 aliguori
                        version_id, idstr, se->version_id);
3432 9366f418 aliguori
                ret = -EINVAL;
3433 9366f418 aliguori
                goto out;
3434 9366f418 aliguori
            }
3435 9366f418 aliguori
3436 9366f418 aliguori
            /* Add entry */
3437 9366f418 aliguori
            le = qemu_mallocz(sizeof(*le));
3438 9366f418 aliguori
            if (le == NULL) {
3439 9366f418 aliguori
                ret = -ENOMEM;
3440 9366f418 aliguori
                goto out;
3441 9366f418 aliguori
            }
3442 9366f418 aliguori
3443 9366f418 aliguori
            le->se = se;
3444 9366f418 aliguori
            le->section_id = section_id;
3445 9366f418 aliguori
            le->version_id = version_id;
3446 9366f418 aliguori
            le->next = first_le;
3447 9366f418 aliguori
            first_le = le;
3448 9366f418 aliguori
3449 9366f418 aliguori
            le->se->load_state(f, le->se->opaque, le->version_id);
3450 9366f418 aliguori
            break;
3451 9366f418 aliguori
        case QEMU_VM_SECTION_PART:
3452 9366f418 aliguori
        case QEMU_VM_SECTION_END:
3453 9366f418 aliguori
            section_id = qemu_get_be32(f);
3454 9366f418 aliguori
3455 9366f418 aliguori
            for (le = first_le; le && le->section_id != section_id; le = le->next);
3456 9366f418 aliguori
            if (le == NULL) {
3457 9366f418 aliguori
                fprintf(stderr, "Unknown savevm section %d\n", section_id);
3458 9366f418 aliguori
                ret = -EINVAL;
3459 9366f418 aliguori
                goto out;
3460 9366f418 aliguori
            }
3461 9366f418 aliguori
3462 9366f418 aliguori
            le->se->load_state(f, le->se->opaque, le->version_id);
3463 9366f418 aliguori
            break;
3464 9366f418 aliguori
        default:
3465 9366f418 aliguori
            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
3466 9366f418 aliguori
            ret = -EINVAL;
3467 9366f418 aliguori
            goto out;
3468 9366f418 aliguori
        }
3469 9366f418 aliguori
    }
3470 9366f418 aliguori
3471 8a7ddc38 bellard
    ret = 0;
3472 9366f418 aliguori
3473 9366f418 aliguori
out:
3474 9366f418 aliguori
    while (first_le) {
3475 9366f418 aliguori
        LoadStateEntry *le = first_le;
3476 9366f418 aliguori
        first_le = first_le->next;
3477 9366f418 aliguori
        qemu_free(le);
3478 9366f418 aliguori
    }
3479 9366f418 aliguori
3480 871d2f07 aliguori
    if (qemu_file_has_error(f))
3481 871d2f07 aliguori
        ret = -EIO;
3482 871d2f07 aliguori
3483 faea38e7 bellard
    return ret;
3484 faea38e7 bellard
}
3485 faea38e7 bellard
3486 faea38e7 bellard
/* device can contain snapshots */
3487 faea38e7 bellard
static int bdrv_can_snapshot(BlockDriverState *bs)
3488 faea38e7 bellard
{
3489 faea38e7 bellard
    return (bs &&
3490 faea38e7 bellard
            !bdrv_is_removable(bs) &&
3491 faea38e7 bellard
            !bdrv_is_read_only(bs));
3492 faea38e7 bellard
}
3493 faea38e7 bellard
3494 faea38e7 bellard
/* device must be snapshots in order to have a reliable snapshot */
3495 faea38e7 bellard
static int bdrv_has_snapshot(BlockDriverState *bs)
3496 faea38e7 bellard
{
3497 faea38e7 bellard
    return (bs &&
3498 faea38e7 bellard
            !bdrv_is_removable(bs) &&
3499 faea38e7 bellard
            !bdrv_is_read_only(bs));
3500 faea38e7 bellard
}
3501 faea38e7 bellard
3502 faea38e7 bellard
static BlockDriverState *get_bs_snapshots(void)
3503 faea38e7 bellard
{
3504 faea38e7 bellard
    BlockDriverState *bs;
3505 faea38e7 bellard
    int i;
3506 faea38e7 bellard
3507 faea38e7 bellard
    if (bs_snapshots)
3508 faea38e7 bellard
        return bs_snapshots;
3509 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3510 e4bcb14c ths
        bs = drives_table[i].bdrv;
3511 faea38e7 bellard
        if (bdrv_can_snapshot(bs))
3512 faea38e7 bellard
            goto ok;
3513 faea38e7 bellard
    }
3514 faea38e7 bellard
    return NULL;
3515 faea38e7 bellard
 ok:
3516 faea38e7 bellard
    bs_snapshots = bs;
3517 faea38e7 bellard
    return bs;
3518 faea38e7 bellard
}
3519 faea38e7 bellard
3520 faea38e7 bellard
static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
3521 faea38e7 bellard
                              const char *name)
3522 faea38e7 bellard
{
3523 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
3524 faea38e7 bellard
    int nb_sns, i, ret;
3525 3b46e624 ths
3526 faea38e7 bellard
    ret = -ENOENT;
3527 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
3528 faea38e7 bellard
    if (nb_sns < 0)
3529 faea38e7 bellard
        return ret;
3530 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
3531 faea38e7 bellard
        sn = &sn_tab[i];
3532 faea38e7 bellard
        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
3533 faea38e7 bellard
            *sn_info = *sn;
3534 faea38e7 bellard
            ret = 0;
3535 faea38e7 bellard
            break;
3536 faea38e7 bellard
        }
3537 faea38e7 bellard
    }
3538 faea38e7 bellard
    qemu_free(sn_tab);
3539 faea38e7 bellard
    return ret;
3540 faea38e7 bellard
}
3541 faea38e7 bellard
3542 faea38e7 bellard
void do_savevm(const char *name)
3543 faea38e7 bellard
{
3544 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3545 faea38e7 bellard
    QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
3546 faea38e7 bellard
    int must_delete, ret, i;
3547 faea38e7 bellard
    BlockDriverInfo bdi1, *bdi = &bdi1;
3548 faea38e7 bellard
    QEMUFile *f;
3549 faea38e7 bellard
    int saved_vm_running;
3550 4c279bdf bellard
#ifdef _WIN32
3551 4c279bdf bellard
    struct _timeb tb;
3552 4c279bdf bellard
#else
3553 faea38e7 bellard
    struct timeval tv;
3554 4c279bdf bellard
#endif
3555 faea38e7 bellard
3556 faea38e7 bellard
    bs = get_bs_snapshots();
3557 faea38e7 bellard
    if (!bs) {
3558 faea38e7 bellard
        term_printf("No block device can accept snapshots\n");
3559 faea38e7 bellard
        return;
3560 faea38e7 bellard
    }
3561 faea38e7 bellard
3562 6192bc37 pbrook
    /* ??? Should this occur after vm_stop?  */
3563 6192bc37 pbrook
    qemu_aio_flush();
3564 6192bc37 pbrook
3565 faea38e7 bellard
    saved_vm_running = vm_running;
3566 faea38e7 bellard
    vm_stop(0);
3567 3b46e624 ths
3568 faea38e7 bellard
    must_delete = 0;
3569 faea38e7 bellard
    if (name) {
3570 faea38e7 bellard
        ret = bdrv_snapshot_find(bs, old_sn, name);
3571 faea38e7 bellard
        if (ret >= 0) {
3572 faea38e7 bellard
            must_delete = 1;
3573 faea38e7 bellard
        }
3574 faea38e7 bellard
    }
3575 faea38e7 bellard
    memset(sn, 0, sizeof(*sn));
3576 faea38e7 bellard
    if (must_delete) {
3577 faea38e7 bellard
        pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
3578 faea38e7 bellard
        pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
3579 faea38e7 bellard
    } else {
3580 faea38e7 bellard
        if (name)
3581 faea38e7 bellard
            pstrcpy(sn->name, sizeof(sn->name), name);
3582 faea38e7 bellard
    }
3583 faea38e7 bellard
3584 faea38e7 bellard
    /* fill auxiliary fields */
3585 4c279bdf bellard
#ifdef _WIN32
3586 4c279bdf bellard
    _ftime(&tb);
3587 4c279bdf bellard
    sn->date_sec = tb.time;
3588 4c279bdf bellard
    sn->date_nsec = tb.millitm * 1000000;
3589 4c279bdf bellard
#else
3590 faea38e7 bellard
    gettimeofday(&tv, NULL);
3591 faea38e7 bellard
    sn->date_sec = tv.tv_sec;
3592 faea38e7 bellard
    sn->date_nsec = tv.tv_usec * 1000;
3593 4c279bdf bellard
#endif
3594 faea38e7 bellard
    sn->vm_clock_nsec = qemu_get_clock(vm_clock);
3595 3b46e624 ths
3596 faea38e7 bellard
    if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
3597 faea38e7 bellard
        term_printf("Device %s does not support VM state snapshots\n",
3598 faea38e7 bellard
                    bdrv_get_device_name(bs));
3599 faea38e7 bellard
        goto the_end;
3600 faea38e7 bellard
    }
3601 3b46e624 ths
3602 faea38e7 bellard
    /* save the VM state */
3603 faea38e7 bellard
    f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
3604 faea38e7 bellard
    if (!f) {
3605 faea38e7 bellard
        term_printf("Could not open VM state file\n");
3606 faea38e7 bellard
        goto the_end;
3607 faea38e7 bellard
    }
3608 faea38e7 bellard
    ret = qemu_savevm_state(f);
3609 faea38e7 bellard
    sn->vm_state_size = qemu_ftell(f);
3610 faea38e7 bellard
    qemu_fclose(f);
3611 faea38e7 bellard
    if (ret < 0) {
3612 faea38e7 bellard
        term_printf("Error %d while writing VM\n", ret);
3613 faea38e7 bellard
        goto the_end;
3614 faea38e7 bellard
    }
3615 3b46e624 ths
3616 faea38e7 bellard
    /* create the snapshots */
3617 faea38e7 bellard
3618 e4bcb14c ths
    for(i = 0; i < nb_drives; i++) {
3619 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3620 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3621 faea38e7 bellard
            if (must_delete) {
3622 faea38e7 bellard
                ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
3623 faea38e7 bellard
                if (ret < 0) {
3624 faea38e7 bellard
                    term_printf("Error while deleting snapshot on '%s'\n",
3625 faea38e7 bellard
                                bdrv_get_device_name(bs1));
3626 faea38e7 bellard
                }
3627 faea38e7 bellard
            }
3628 faea38e7 bellard
            ret = bdrv_snapshot_create(bs1, sn);
3629 faea38e7 bellard
            if (ret < 0) {
3630 faea38e7 bellard
                term_printf("Error while creating snapshot on '%s'\n",
3631 faea38e7 bellard
                            bdrv_get_device_name(bs1));
3632 faea38e7 bellard
            }
3633 faea38e7 bellard
        }
3634 faea38e7 bellard
    }
3635 faea38e7 bellard
3636 faea38e7 bellard
 the_end:
3637 8a7ddc38 bellard
    if (saved_vm_running)
3638 8a7ddc38 bellard
        vm_start();
3639 faea38e7 bellard
}
3640 faea38e7 bellard
3641 faea38e7 bellard
void do_loadvm(const char *name)
3642 faea38e7 bellard
{
3643 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3644 faea38e7 bellard
    BlockDriverInfo bdi1, *bdi = &bdi1;
3645 faea38e7 bellard
    QEMUFile *f;
3646 faea38e7 bellard
    int i, ret;
3647 faea38e7 bellard
    int saved_vm_running;
3648 faea38e7 bellard
3649 faea38e7 bellard
    bs = get_bs_snapshots();
3650 faea38e7 bellard
    if (!bs) {
3651 faea38e7 bellard
        term_printf("No block device supports snapshots\n");
3652 faea38e7 bellard
        return;
3653 faea38e7 bellard
    }
3654 3b46e624 ths
3655 6192bc37 pbrook
    /* Flush all IO requests so they don't interfere with the new state.  */
3656 6192bc37 pbrook
    qemu_aio_flush();
3657 6192bc37 pbrook
3658 faea38e7 bellard
    saved_vm_running = vm_running;
3659 faea38e7 bellard
    vm_stop(0);
3660 faea38e7 bellard
3661 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3662 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3663 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3664 faea38e7 bellard
            ret = bdrv_snapshot_goto(bs1, name);
3665 faea38e7 bellard
            if (ret < 0) {
3666 faea38e7 bellard
                if (bs != bs1)
3667 faea38e7 bellard
                    term_printf("Warning: ");
3668 faea38e7 bellard
                switch(ret) {
3669 faea38e7 bellard
                case -ENOTSUP:
3670 faea38e7 bellard
                    term_printf("Snapshots not supported on device '%s'\n",
3671 faea38e7 bellard
                                bdrv_get_device_name(bs1));
3672 faea38e7 bellard
                    break;
3673 faea38e7 bellard
                case -ENOENT:
3674 faea38e7 bellard
                    term_printf("Could not find snapshot '%s' on device '%s'\n",
3675 faea38e7 bellard
                                name, bdrv_get_device_name(bs1));
3676 faea38e7 bellard
                    break;
3677 faea38e7 bellard
                default:
3678 faea38e7 bellard
                    term_printf("Error %d while activating snapshot on '%s'\n",
3679 faea38e7 bellard
                                ret, bdrv_get_device_name(bs1));
3680 faea38e7 bellard
                    break;
3681 faea38e7 bellard
                }
3682 faea38e7 bellard
                /* fatal on snapshot block device */
3683 faea38e7 bellard
                if (bs == bs1)
3684 faea38e7 bellard
                    goto the_end;
3685 faea38e7 bellard
            }
3686 faea38e7 bellard
        }
3687 faea38e7 bellard
    }
3688 faea38e7 bellard
3689 faea38e7 bellard
    if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
3690 faea38e7 bellard
        term_printf("Device %s does not support VM state snapshots\n",
3691 faea38e7 bellard
                    bdrv_get_device_name(bs));
3692 faea38e7 bellard
        return;
3693 faea38e7 bellard
    }
3694 3b46e624 ths
3695 faea38e7 bellard
    /* restore the VM state */
3696 faea38e7 bellard
    f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
3697 faea38e7 bellard
    if (!f) {
3698 faea38e7 bellard
        term_printf("Could not open VM state file\n");
3699 faea38e7 bellard
        goto the_end;
3700 faea38e7 bellard
    }
3701 faea38e7 bellard
    ret = qemu_loadvm_state(f);
3702 faea38e7 bellard
    qemu_fclose(f);
3703 faea38e7 bellard
    if (ret < 0) {
3704 faea38e7 bellard
        term_printf("Error %d while loading VM state\n", ret);
3705 faea38e7 bellard
    }
3706 faea38e7 bellard
 the_end:
3707 faea38e7 bellard
    if (saved_vm_running)
3708 faea38e7 bellard
        vm_start();
3709 faea38e7 bellard
}
3710 faea38e7 bellard
3711 faea38e7 bellard
void do_delvm(const char *name)
3712 faea38e7 bellard
{
3713 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3714 faea38e7 bellard
    int i, ret;
3715 faea38e7 bellard
3716 faea38e7 bellard
    bs = get_bs_snapshots();
3717 faea38e7 bellard
    if (!bs) {
3718 faea38e7 bellard
        term_printf("No block device supports snapshots\n");
3719 faea38e7 bellard
        return;
3720 faea38e7 bellard
    }
3721 3b46e624 ths
3722 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3723 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3724 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3725 faea38e7 bellard
            ret = bdrv_snapshot_delete(bs1, name);
3726 faea38e7 bellard
            if (ret < 0) {
3727 faea38e7 bellard
                if (ret == -ENOTSUP)
3728 faea38e7 bellard
                    term_printf("Snapshots not supported on device '%s'\n",
3729 faea38e7 bellard
                                bdrv_get_device_name(bs1));
3730 faea38e7 bellard
                else
3731 faea38e7 bellard
                    term_printf("Error %d while deleting snapshot on '%s'\n",
3732 faea38e7 bellard
                                ret, bdrv_get_device_name(bs1));
3733 faea38e7 bellard
            }
3734 faea38e7 bellard
        }
3735 faea38e7 bellard
    }
3736 faea38e7 bellard
}
3737 faea38e7 bellard
3738 faea38e7 bellard
void do_info_snapshots(void)
3739 faea38e7 bellard
{
3740 faea38e7 bellard
    BlockDriverState *bs, *bs1;
3741 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
3742 faea38e7 bellard
    int nb_sns, i;
3743 faea38e7 bellard
    char buf[256];
3744 faea38e7 bellard
3745 faea38e7 bellard
    bs = get_bs_snapshots();
3746 faea38e7 bellard
    if (!bs) {
3747 faea38e7 bellard
        term_printf("No available block device supports snapshots\n");
3748 faea38e7 bellard
        return;
3749 faea38e7 bellard
    }
3750 faea38e7 bellard
    term_printf("Snapshot devices:");
3751 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
3752 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
3753 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
3754 faea38e7 bellard
            if (bs == bs1)
3755 faea38e7 bellard
                term_printf(" %s", bdrv_get_device_name(bs1));
3756 faea38e7 bellard
        }
3757 faea38e7 bellard
    }
3758 faea38e7 bellard
    term_printf("\n");
3759 faea38e7 bellard
3760 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
3761 faea38e7 bellard
    if (nb_sns < 0) {
3762 faea38e7 bellard
        term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
3763 faea38e7 bellard
        return;
3764 faea38e7 bellard
    }
3765 faea38e7 bellard
    term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
3766 faea38e7 bellard
    term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
3767 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
3768 faea38e7 bellard
        sn = &sn_tab[i];
3769 faea38e7 bellard
        term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
3770 faea38e7 bellard
    }
3771 faea38e7 bellard
    qemu_free(sn_tab);
3772 8a7ddc38 bellard
}
3773 8a7ddc38 bellard
3774 8a7ddc38 bellard
/***********************************************************/
3775 8a7ddc38 bellard
/* ram save/restore */
3776 8a7ddc38 bellard
3777 8a7ddc38 bellard
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
3778 8a7ddc38 bellard
{
3779 8a7ddc38 bellard
    int v;
3780 8a7ddc38 bellard
3781 8a7ddc38 bellard
    v = qemu_get_byte(f);
3782 8a7ddc38 bellard
    switch(v) {
3783 8a7ddc38 bellard
    case 0:
3784 8a7ddc38 bellard
        if (qemu_get_buffer(f, buf, len) != len)
3785 8a7ddc38 bellard
            return -EIO;
3786 8a7ddc38 bellard
        break;
3787 8a7ddc38 bellard
    case 1:
3788 8a7ddc38 bellard
        v = qemu_get_byte(f);
3789 8a7ddc38 bellard
        memset(buf, v, len);
3790 8a7ddc38 bellard
        break;
3791 8a7ddc38 bellard
    default:
3792 8a7ddc38 bellard
        return -EINVAL;
3793 8a7ddc38 bellard
    }
3794 871d2f07 aliguori
3795 871d2f07 aliguori
    if (qemu_file_has_error(f))
3796 871d2f07 aliguori
        return -EIO;
3797 871d2f07 aliguori
3798 8a7ddc38 bellard
    return 0;
3799 8a7ddc38 bellard
}
3800 8a7ddc38 bellard
3801 c88676f8 bellard
static int ram_load_v1(QEMUFile *f, void *opaque)
3802 c88676f8 bellard
{
3803 00f82b8a aurel32
    int ret;
3804 00f82b8a aurel32
    ram_addr_t i;
3805 c88676f8 bellard
3806 c88676f8 bellard
    if (qemu_get_be32(f) != phys_ram_size)
3807 c88676f8 bellard
        return -EINVAL;
3808 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
3809 c88676f8 bellard
        ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
3810 c88676f8 bellard
        if (ret)
3811 c88676f8 bellard
            return ret;
3812 c88676f8 bellard
    }
3813 c88676f8 bellard
    return 0;
3814 c88676f8 bellard
}
3815 c88676f8 bellard
3816 c88676f8 bellard
#define BDRV_HASH_BLOCK_SIZE 1024
3817 c88676f8 bellard
#define IOBUF_SIZE 4096
3818 c88676f8 bellard
#define RAM_CBLOCK_MAGIC 0xfabe
3819 c88676f8 bellard
3820 c88676f8 bellard
typedef struct RamDecompressState {
3821 c88676f8 bellard
    z_stream zstream;
3822 c88676f8 bellard
    QEMUFile *f;
3823 c88676f8 bellard
    uint8_t buf[IOBUF_SIZE];
3824 c88676f8 bellard
} RamDecompressState;
3825 c88676f8 bellard
3826 c88676f8 bellard
static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
3827 c88676f8 bellard
{
3828 c88676f8 bellard
    int ret;
3829 c88676f8 bellard
    memset(s, 0, sizeof(*s));
3830 c88676f8 bellard
    s->f = f;
3831 c88676f8 bellard
    ret = inflateInit(&s->zstream);
3832 c88676f8 bellard
    if (ret != Z_OK)
3833 c88676f8 bellard
        return -1;
3834 c88676f8 bellard
    return 0;
3835 c88676f8 bellard
}
3836 c88676f8 bellard
3837 c88676f8 bellard
static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
3838 c88676f8 bellard
{
3839 c88676f8 bellard
    int ret, clen;
3840 c88676f8 bellard
3841 c88676f8 bellard
    s->zstream.avail_out = len;
3842 c88676f8 bellard
    s->zstream.next_out = buf;
3843 c88676f8 bellard
    while (s->zstream.avail_out > 0) {
3844 c88676f8 bellard
        if (s->zstream.avail_in == 0) {
3845 c88676f8 bellard
            if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
3846 c88676f8 bellard
                return -1;
3847 c88676f8 bellard
            clen = qemu_get_be16(s->f);
3848 c88676f8 bellard
            if (clen > IOBUF_SIZE)
3849 c88676f8 bellard
                return -1;
3850 c88676f8 bellard
            qemu_get_buffer(s->f, s->buf, clen);
3851 c88676f8 bellard
            s->zstream.avail_in = clen;
3852 c88676f8 bellard
            s->zstream.next_in = s->buf;
3853 c88676f8 bellard
        }
3854 c88676f8 bellard
        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
3855 c88676f8 bellard
        if (ret != Z_OK && ret != Z_STREAM_END) {
3856 c88676f8 bellard
            return -1;
3857 c88676f8 bellard
        }
3858 c88676f8 bellard
    }
3859 c88676f8 bellard
    return 0;
3860 c88676f8 bellard
}
3861 c88676f8 bellard
3862 c88676f8 bellard
static void ram_decompress_close(RamDecompressState *s)
3863 c88676f8 bellard
{
3864 c88676f8 bellard
    inflateEnd(&s->zstream);
3865 c88676f8 bellard
}
3866 c88676f8 bellard
3867 475e4277 aliguori
#define RAM_SAVE_FLAG_FULL        0x01
3868 475e4277 aliguori
#define RAM_SAVE_FLAG_COMPRESS        0x02
3869 475e4277 aliguori
#define RAM_SAVE_FLAG_MEM_SIZE        0x04
3870 475e4277 aliguori
#define RAM_SAVE_FLAG_PAGE        0x08
3871 475e4277 aliguori
#define RAM_SAVE_FLAG_EOS        0x10
3872 475e4277 aliguori
3873 475e4277 aliguori
static int is_dup_page(uint8_t *page, uint8_t ch)
3874 8a7ddc38 bellard
{
3875 475e4277 aliguori
    uint32_t val = ch << 24 | ch << 16 | ch << 8 | ch;
3876 475e4277 aliguori
    uint32_t *array = (uint32_t *)page;
3877 475e4277 aliguori
    int i;
3878 3b46e624 ths
3879 475e4277 aliguori
    for (i = 0; i < (TARGET_PAGE_SIZE / 4); i++) {
3880 475e4277 aliguori
        if (array[i] != val)
3881 475e4277 aliguori
            return 0;
3882 475e4277 aliguori
    }
3883 475e4277 aliguori
3884 475e4277 aliguori
    return 1;
3885 475e4277 aliguori
}
3886 475e4277 aliguori
3887 475e4277 aliguori
static int ram_save_block(QEMUFile *f)
3888 475e4277 aliguori
{
3889 475e4277 aliguori
    static ram_addr_t current_addr = 0;
3890 475e4277 aliguori
    ram_addr_t saved_addr = current_addr;
3891 475e4277 aliguori
    ram_addr_t addr = 0;
3892 475e4277 aliguori
    int found = 0;
3893 475e4277 aliguori
3894 475e4277 aliguori
    while (addr < phys_ram_size) {
3895 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(current_addr, MIGRATION_DIRTY_FLAG)) {
3896 475e4277 aliguori
            uint8_t ch;
3897 475e4277 aliguori
3898 475e4277 aliguori
            cpu_physical_memory_reset_dirty(current_addr,
3899 475e4277 aliguori
                                            current_addr + TARGET_PAGE_SIZE,
3900 475e4277 aliguori
                                            MIGRATION_DIRTY_FLAG);
3901 475e4277 aliguori
3902 475e4277 aliguori
            ch = *(phys_ram_base + current_addr);
3903 475e4277 aliguori
3904 475e4277 aliguori
            if (is_dup_page(phys_ram_base + current_addr, ch)) {
3905 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_COMPRESS);
3906 475e4277 aliguori
                qemu_put_byte(f, ch);
3907 475e4277 aliguori
            } else {
3908 475e4277 aliguori
                qemu_put_be64(f, current_addr | RAM_SAVE_FLAG_PAGE);
3909 475e4277 aliguori
                qemu_put_buffer(f, phys_ram_base + current_addr, TARGET_PAGE_SIZE);
3910 c88676f8 bellard
            }
3911 475e4277 aliguori
3912 475e4277 aliguori
            found = 1;
3913 475e4277 aliguori
            break;
3914 c88676f8 bellard
        }
3915 475e4277 aliguori
        addr += TARGET_PAGE_SIZE;
3916 475e4277 aliguori
        current_addr = (saved_addr + addr) % phys_ram_size;
3917 8a7ddc38 bellard
    }
3918 475e4277 aliguori
3919 475e4277 aliguori
    return found;
3920 8a7ddc38 bellard
}
3921 8a7ddc38 bellard
3922 475e4277 aliguori
static ram_addr_t ram_save_threshold = 10;
3923 475e4277 aliguori
3924 475e4277 aliguori
static ram_addr_t ram_save_remaining(void)
3925 475e4277 aliguori
{
3926 475e4277 aliguori
    ram_addr_t addr;
3927 475e4277 aliguori
    ram_addr_t count = 0;
3928 475e4277 aliguori
3929 475e4277 aliguori
    for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3930 475e4277 aliguori
        if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3931 475e4277 aliguori
            count++;
3932 475e4277 aliguori
    }
3933 475e4277 aliguori
3934 475e4277 aliguori
    return count;
3935 475e4277 aliguori
}
3936 475e4277 aliguori
3937 475e4277 aliguori
static int ram_save_live(QEMUFile *f, int stage, void *opaque)
3938 475e4277 aliguori
{
3939 475e4277 aliguori
    ram_addr_t addr;
3940 475e4277 aliguori
3941 475e4277 aliguori
    if (stage == 1) {
3942 475e4277 aliguori
        /* Make sure all dirty bits are set */
3943 475e4277 aliguori
        for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
3944 475e4277 aliguori
            if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
3945 475e4277 aliguori
                cpu_physical_memory_set_dirty(addr);
3946 475e4277 aliguori
        }
3947 475e4277 aliguori
        
3948 475e4277 aliguori
        /* Enable dirty memory tracking */
3949 475e4277 aliguori
        cpu_physical_memory_set_dirty_tracking(1);
3950 475e4277 aliguori
3951 475e4277 aliguori
        qemu_put_be64(f, phys_ram_size | RAM_SAVE_FLAG_MEM_SIZE);
3952 475e4277 aliguori
    }
3953 475e4277 aliguori
3954 475e4277 aliguori
    while (!qemu_file_rate_limit(f)) {
3955 475e4277 aliguori
        int ret;
3956 475e4277 aliguori
3957 475e4277 aliguori
        ret = ram_save_block(f);
3958 475e4277 aliguori
        if (ret == 0) /* no more blocks */
3959 475e4277 aliguori
            break;
3960 475e4277 aliguori
    }
3961 475e4277 aliguori
3962 475e4277 aliguori
    /* try transferring iterative blocks of memory */
3963 475e4277 aliguori
3964 475e4277 aliguori
    if (stage == 3) {
3965 475e4277 aliguori
        cpu_physical_memory_set_dirty_tracking(0);
3966 475e4277 aliguori
3967 475e4277 aliguori
        /* flush all remaining blocks regardless of rate limiting */
3968 475e4277 aliguori
        while (ram_save_block(f) != 0);
3969 475e4277 aliguori
    }
3970 475e4277 aliguori
3971 475e4277 aliguori
    qemu_put_be64(f, RAM_SAVE_FLAG_EOS);
3972 475e4277 aliguori
3973 475e4277 aliguori
    return (stage == 2) && (ram_save_remaining() < ram_save_threshold);
3974 475e4277 aliguori
}
3975 475e4277 aliguori
3976 475e4277 aliguori
static int ram_load_dead(QEMUFile *f, void *opaque)
3977 8a7ddc38 bellard
{
3978 c88676f8 bellard
    RamDecompressState s1, *s = &s1;
3979 c88676f8 bellard
    uint8_t buf[10];
3980 00f82b8a aurel32
    ram_addr_t i;
3981 8a7ddc38 bellard
3982 c88676f8 bellard
    if (ram_decompress_open(s, f) < 0)
3983 c88676f8 bellard
        return -EINVAL;
3984 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
3985 c88676f8 bellard
        if (ram_decompress_buf(s, buf, 1) < 0) {
3986 c88676f8 bellard
            fprintf(stderr, "Error while reading ram block header\n");
3987 c88676f8 bellard
            goto error;
3988 c88676f8 bellard
        }
3989 c88676f8 bellard
        if (buf[0] == 0) {
3990 c88676f8 bellard
            if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
3991 00f82b8a aurel32
                fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
3992 c88676f8 bellard
                goto error;
3993 c88676f8 bellard
            }
3994 475e4277 aliguori
        } else {
3995 c88676f8 bellard
        error:
3996 c88676f8 bellard
            printf("Error block header\n");
3997 c88676f8 bellard
            return -EINVAL;
3998 c88676f8 bellard
        }
3999 8a7ddc38 bellard
    }
4000 c88676f8 bellard
    ram_decompress_close(s);
4001 475e4277 aliguori
4002 475e4277 aliguori
    return 0;
4003 475e4277 aliguori
}
4004 475e4277 aliguori
4005 475e4277 aliguori
static int ram_load(QEMUFile *f, void *opaque, int version_id)
4006 475e4277 aliguori
{
4007 475e4277 aliguori
    ram_addr_t addr;
4008 475e4277 aliguori
    int flags;
4009 475e4277 aliguori
4010 475e4277 aliguori
    if (version_id == 1)
4011 475e4277 aliguori
        return ram_load_v1(f, opaque);
4012 475e4277 aliguori
4013 475e4277 aliguori
    if (version_id == 2) {
4014 475e4277 aliguori
        if (qemu_get_be32(f) != phys_ram_size)
4015 475e4277 aliguori
            return -EINVAL;
4016 475e4277 aliguori
        return ram_load_dead(f, opaque);
4017 475e4277 aliguori
    }
4018 475e4277 aliguori
4019 475e4277 aliguori
    if (version_id != 3)
4020 475e4277 aliguori
        return -EINVAL;
4021 475e4277 aliguori
4022 475e4277 aliguori
    do {
4023 475e4277 aliguori
        addr = qemu_get_be64(f);
4024 475e4277 aliguori
4025 475e4277 aliguori
        flags = addr & ~TARGET_PAGE_MASK;
4026 475e4277 aliguori
        addr &= TARGET_PAGE_MASK;
4027 475e4277 aliguori
4028 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_MEM_SIZE) {
4029 475e4277 aliguori
            if (addr != phys_ram_size)
4030 475e4277 aliguori
                return -EINVAL;
4031 475e4277 aliguori
        }
4032 475e4277 aliguori
4033 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_FULL) {
4034 475e4277 aliguori
            if (ram_load_dead(f, opaque) < 0)
4035 475e4277 aliguori
                return -EINVAL;
4036 475e4277 aliguori
        }
4037 475e4277 aliguori
        
4038 475e4277 aliguori
        if (flags & RAM_SAVE_FLAG_COMPRESS) {
4039 475e4277 aliguori
            uint8_t ch = qemu_get_byte(f);
4040 475e4277 aliguori
            memset(phys_ram_base + addr, ch, TARGET_PAGE_SIZE);
4041 475e4277 aliguori
        } else if (flags & RAM_SAVE_FLAG_PAGE)
4042 475e4277 aliguori
            qemu_get_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
4043 475e4277 aliguori
    } while (!(flags & RAM_SAVE_FLAG_EOS));
4044 475e4277 aliguori
4045 8a7ddc38 bellard
    return 0;
4046 8a7ddc38 bellard
}
4047 8a7ddc38 bellard
4048 9e472e10 aliguori
void qemu_service_io(void)
4049 9e472e10 aliguori
{
4050 9e472e10 aliguori
    CPUState *env = cpu_single_env;
4051 9e472e10 aliguori
    if (env) {
4052 9e472e10 aliguori
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
4053 9e472e10 aliguori
#ifdef USE_KQEMU
4054 9e472e10 aliguori
        if (env->kqemu_enabled) {
4055 9e472e10 aliguori
            kqemu_cpu_interrupt(env);
4056 9e472e10 aliguori
        }
4057 9e472e10 aliguori
#endif
4058 9e472e10 aliguori
    }
4059 9e472e10 aliguori
}
4060 9e472e10 aliguori
4061 8a7ddc38 bellard
/***********************************************************/
4062 83f64091 bellard
/* bottom halves (can be seen as timers which expire ASAP) */
4063 83f64091 bellard
4064 83f64091 bellard
struct QEMUBH {
4065 83f64091 bellard
    QEMUBHFunc *cb;
4066 83f64091 bellard
    void *opaque;
4067 83f64091 bellard
    int scheduled;
4068 1b435b10 aliguori
    int idle;
4069 1b435b10 aliguori
    int deleted;
4070 83f64091 bellard
    QEMUBH *next;
4071 83f64091 bellard
};
4072 83f64091 bellard
4073 83f64091 bellard
static QEMUBH *first_bh = NULL;
4074 83f64091 bellard
4075 83f64091 bellard
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
4076 83f64091 bellard
{
4077 83f64091 bellard
    QEMUBH *bh;
4078 83f64091 bellard
    bh = qemu_mallocz(sizeof(QEMUBH));
4079 83f64091 bellard
    if (!bh)
4080 83f64091 bellard
        return NULL;
4081 83f64091 bellard
    bh->cb = cb;
4082 83f64091 bellard
    bh->opaque = opaque;
4083 1b435b10 aliguori
    bh->next = first_bh;
4084 1b435b10 aliguori
    first_bh = bh;
4085 83f64091 bellard
    return bh;
4086 83f64091 bellard
}
4087 83f64091 bellard
4088 6eb5733a bellard
int qemu_bh_poll(void)
4089 83f64091 bellard
{
4090 1b435b10 aliguori
    QEMUBH *bh, **bhp;
4091 6eb5733a bellard
    int ret;
4092 83f64091 bellard
4093 6eb5733a bellard
    ret = 0;
4094 1b435b10 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
4095 1b435b10 aliguori
        if (!bh->deleted && bh->scheduled) {
4096 1b435b10 aliguori
            bh->scheduled = 0;
4097 1b435b10 aliguori
            if (!bh->idle)
4098 1b435b10 aliguori
                ret = 1;
4099 1b435b10 aliguori
            bh->idle = 0;
4100 1b435b10 aliguori
            bh->cb(bh->opaque);
4101 1b435b10 aliguori
        }
4102 83f64091 bellard
    }
4103 1b435b10 aliguori
4104 1b435b10 aliguori
    /* remove deleted bhs */
4105 1b435b10 aliguori
    bhp = &first_bh;
4106 1b435b10 aliguori
    while (*bhp) {
4107 1b435b10 aliguori
        bh = *bhp;
4108 1b435b10 aliguori
        if (bh->deleted) {
4109 1b435b10 aliguori
            *bhp = bh->next;
4110 1b435b10 aliguori
            qemu_free(bh);
4111 1b435b10 aliguori
        } else
4112 1b435b10 aliguori
            bhp = &bh->next;
4113 1b435b10 aliguori
    }
4114 1b435b10 aliguori
4115 6eb5733a bellard
    return ret;
4116 83f64091 bellard
}
4117 83f64091 bellard
4118 1b435b10 aliguori
void qemu_bh_schedule_idle(QEMUBH *bh)
4119 1b435b10 aliguori
{
4120 1b435b10 aliguori
    if (bh->scheduled)
4121 1b435b10 aliguori
        return;
4122 1b435b10 aliguori
    bh->scheduled = 1;
4123 1b435b10 aliguori
    bh->idle = 1;
4124 1b435b10 aliguori
}
4125 1b435b10 aliguori
4126 83f64091 bellard
void qemu_bh_schedule(QEMUBH *bh)
4127 83f64091 bellard
{
4128 83f64091 bellard
    CPUState *env = cpu_single_env;
4129 83f64091 bellard
    if (bh->scheduled)
4130 83f64091 bellard
        return;
4131 83f64091 bellard
    bh->scheduled = 1;
4132 1b435b10 aliguori
    bh->idle = 0;
4133 83f64091 bellard
    /* stop the currently executing CPU to execute the BH ASAP */
4134 83f64091 bellard
    if (env) {
4135 83f64091 bellard
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
4136 83f64091 bellard
    }
4137 83f64091 bellard
}
4138 83f64091 bellard
4139 83f64091 bellard
void qemu_bh_cancel(QEMUBH *bh)
4140 83f64091 bellard
{
4141 1b435b10 aliguori
    bh->scheduled = 0;
4142 83f64091 bellard
}
4143 83f64091 bellard
4144 83f64091 bellard
void qemu_bh_delete(QEMUBH *bh)
4145 83f64091 bellard
{
4146 1b435b10 aliguori
    bh->scheduled = 0;
4147 1b435b10 aliguori
    bh->deleted = 1;
4148 83f64091 bellard
}
4149 83f64091 bellard
4150 56f3a5d0 aliguori
static void qemu_bh_update_timeout(int *timeout)
4151 56f3a5d0 aliguori
{
4152 56f3a5d0 aliguori
    QEMUBH *bh;
4153 56f3a5d0 aliguori
4154 56f3a5d0 aliguori
    for (bh = first_bh; bh; bh = bh->next) {
4155 56f3a5d0 aliguori
        if (!bh->deleted && bh->scheduled) {
4156 56f3a5d0 aliguori
            if (bh->idle) {
4157 56f3a5d0 aliguori
                /* idle bottom halves will be polled at least
4158 56f3a5d0 aliguori
                 * every 10ms */
4159 56f3a5d0 aliguori
                *timeout = MIN(10, *timeout);
4160 56f3a5d0 aliguori
            } else {
4161 56f3a5d0 aliguori
                /* non-idle bottom halves will be executed
4162 56f3a5d0 aliguori
                 * immediately */
4163 56f3a5d0 aliguori
                *timeout = 0;
4164 56f3a5d0 aliguori
                break;
4165 56f3a5d0 aliguori
            }
4166 56f3a5d0 aliguori
        }
4167 56f3a5d0 aliguori
    }
4168 56f3a5d0 aliguori
}
4169 56f3a5d0 aliguori
4170 83f64091 bellard
/***********************************************************/
4171 cc1daa40 bellard
/* machine registration */
4172 cc1daa40 bellard
4173 bdaf78e0 blueswir1
static QEMUMachine *first_machine = NULL;
4174 cc1daa40 bellard
4175 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
4176 cc1daa40 bellard
{
4177 cc1daa40 bellard
    QEMUMachine **pm;
4178 cc1daa40 bellard
    pm = &first_machine;
4179 cc1daa40 bellard
    while (*pm != NULL)
4180 cc1daa40 bellard
        pm = &(*pm)->next;
4181 cc1daa40 bellard
    m->next = NULL;
4182 cc1daa40 bellard
    *pm = m;
4183 cc1daa40 bellard
    return 0;
4184 cc1daa40 bellard
}
4185 cc1daa40 bellard
4186 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
4187 cc1daa40 bellard
{
4188 cc1daa40 bellard
    QEMUMachine *m;
4189 cc1daa40 bellard
4190 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
4191 cc1daa40 bellard
        if (!strcmp(m->name, name))
4192 cc1daa40 bellard
            return m;
4193 cc1daa40 bellard
    }
4194 cc1daa40 bellard
    return NULL;
4195 cc1daa40 bellard
}
4196 cc1daa40 bellard
4197 cc1daa40 bellard
/***********************************************************/
4198 8a7ddc38 bellard
/* main execution loop */
4199 8a7ddc38 bellard
4200 9596ebb7 pbrook
static void gui_update(void *opaque)
4201 8a7ddc38 bellard
{
4202 740733bb ths
    DisplayState *ds = opaque;
4203 740733bb ths
    ds->dpy_refresh(ds);
4204 f442e08b aurel32
    qemu_mod_timer(ds->gui_timer,
4205 f442e08b aurel32
        (ds->gui_timer_interval ?
4206 f442e08b aurel32
            ds->gui_timer_interval :
4207 f442e08b aurel32
            GUI_REFRESH_INTERVAL)
4208 f442e08b aurel32
        + qemu_get_clock(rt_clock));
4209 8a7ddc38 bellard
}
4210 8a7ddc38 bellard
4211 0bd48850 bellard
struct vm_change_state_entry {
4212 0bd48850 bellard
    VMChangeStateHandler *cb;
4213 0bd48850 bellard
    void *opaque;
4214 0bd48850 bellard
    LIST_ENTRY (vm_change_state_entry) entries;
4215 0bd48850 bellard
};
4216 0bd48850 bellard
4217 0bd48850 bellard
static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
4218 0bd48850 bellard
4219 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
4220 0bd48850 bellard
                                                     void *opaque)
4221 0bd48850 bellard
{
4222 0bd48850 bellard
    VMChangeStateEntry *e;
4223 0bd48850 bellard
4224 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
4225 0bd48850 bellard
    if (!e)
4226 0bd48850 bellard
        return NULL;
4227 0bd48850 bellard
4228 0bd48850 bellard
    e->cb = cb;
4229 0bd48850 bellard
    e->opaque = opaque;
4230 0bd48850 bellard
    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
4231 0bd48850 bellard
    return e;
4232 0bd48850 bellard
}
4233 0bd48850 bellard
4234 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
4235 0bd48850 bellard
{
4236 0bd48850 bellard
    LIST_REMOVE (e, entries);
4237 0bd48850 bellard
    qemu_free (e);
4238 0bd48850 bellard
}
4239 0bd48850 bellard
4240 0bd48850 bellard
static void vm_state_notify(int running)
4241 0bd48850 bellard
{
4242 0bd48850 bellard
    VMChangeStateEntry *e;
4243 0bd48850 bellard
4244 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
4245 0bd48850 bellard
        e->cb(e->opaque, running);
4246 0bd48850 bellard
    }
4247 0bd48850 bellard
}
4248 0bd48850 bellard
4249 8a7ddc38 bellard
/* XXX: support several handlers */
4250 0bd48850 bellard
static VMStopHandler *vm_stop_cb;
4251 0bd48850 bellard
static void *vm_stop_opaque;
4252 8a7ddc38 bellard
4253 8a7ddc38 bellard
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
4254 8a7ddc38 bellard
{
4255 8a7ddc38 bellard
    vm_stop_cb = cb;
4256 8a7ddc38 bellard
    vm_stop_opaque = opaque;
4257 8a7ddc38 bellard
    return 0;
4258 8a7ddc38 bellard
}
4259 8a7ddc38 bellard
4260 8a7ddc38 bellard
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
4261 8a7ddc38 bellard
{
4262 8a7ddc38 bellard
    vm_stop_cb = NULL;
4263 8a7ddc38 bellard
}
4264 8a7ddc38 bellard
4265 8a7ddc38 bellard
void vm_start(void)
4266 8a7ddc38 bellard
{
4267 8a7ddc38 bellard
    if (!vm_running) {
4268 8a7ddc38 bellard
        cpu_enable_ticks();
4269 8a7ddc38 bellard
        vm_running = 1;
4270 0bd48850 bellard
        vm_state_notify(1);
4271 efe75411 ths
        qemu_rearm_alarm_timer(alarm_timer);
4272 8a7ddc38 bellard
    }
4273 8a7ddc38 bellard
}
4274 8a7ddc38 bellard
4275 5fafdf24 ths
void vm_stop(int reason)
4276 8a7ddc38 bellard
{
4277 8a7ddc38 bellard
    if (vm_running) {
4278 8a7ddc38 bellard
        cpu_disable_ticks();
4279 8a7ddc38 bellard
        vm_running = 0;
4280 8a7ddc38 bellard
        if (reason != 0) {
4281 8a7ddc38 bellard
            if (vm_stop_cb) {
4282 8a7ddc38 bellard
                vm_stop_cb(vm_stop_opaque, reason);
4283 8a7ddc38 bellard
            }
4284 34865134 bellard
        }
4285 0bd48850 bellard
        vm_state_notify(0);
4286 8a7ddc38 bellard
    }
4287 8a7ddc38 bellard
}
4288 8a7ddc38 bellard
4289 bb0c6722 bellard
/* reset/shutdown handler */
4290 bb0c6722 bellard
4291 bb0c6722 bellard
typedef struct QEMUResetEntry {
4292 bb0c6722 bellard
    QEMUResetHandler *func;
4293 bb0c6722 bellard
    void *opaque;
4294 bb0c6722 bellard
    struct QEMUResetEntry *next;
4295 bb0c6722 bellard
} QEMUResetEntry;
4296 bb0c6722 bellard
4297 bb0c6722 bellard
static QEMUResetEntry *first_reset_entry;
4298 bb0c6722 bellard
static int reset_requested;
4299 bb0c6722 bellard
static int shutdown_requested;
4300 3475187d bellard
static int powerdown_requested;
4301 bb0c6722 bellard
4302 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
4303 cf7a2fe2 aurel32
{
4304 cf7a2fe2 aurel32
    int r = shutdown_requested;
4305 cf7a2fe2 aurel32
    shutdown_requested = 0;
4306 cf7a2fe2 aurel32
    return r;
4307 cf7a2fe2 aurel32
}
4308 cf7a2fe2 aurel32
4309 cf7a2fe2 aurel32
int qemu_reset_requested(void)
4310 cf7a2fe2 aurel32
{
4311 cf7a2fe2 aurel32
    int r = reset_requested;
4312 cf7a2fe2 aurel32
    reset_requested = 0;
4313 cf7a2fe2 aurel32
    return r;
4314 cf7a2fe2 aurel32
}
4315 cf7a2fe2 aurel32
4316 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
4317 cf7a2fe2 aurel32
{
4318 cf7a2fe2 aurel32
    int r = powerdown_requested;
4319 cf7a2fe2 aurel32
    powerdown_requested = 0;
4320 cf7a2fe2 aurel32
    return r;
4321 cf7a2fe2 aurel32
}
4322 cf7a2fe2 aurel32
4323 bb0c6722 bellard
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
4324 bb0c6722 bellard
{
4325 bb0c6722 bellard
    QEMUResetEntry **pre, *re;
4326 bb0c6722 bellard
4327 bb0c6722 bellard
    pre = &first_reset_entry;
4328 bb0c6722 bellard
    while (*pre != NULL)
4329 bb0c6722 bellard
        pre = &(*pre)->next;
4330 bb0c6722 bellard
    re = qemu_mallocz(sizeof(QEMUResetEntry));
4331 bb0c6722 bellard
    re->func = func;
4332 bb0c6722 bellard
    re->opaque = opaque;
4333 bb0c6722 bellard
    re->next = NULL;
4334 bb0c6722 bellard
    *pre = re;
4335 bb0c6722 bellard
}
4336 bb0c6722 bellard
4337 cf7a2fe2 aurel32
void qemu_system_reset(void)
4338 bb0c6722 bellard
{
4339 bb0c6722 bellard
    QEMUResetEntry *re;
4340 bb0c6722 bellard
4341 bb0c6722 bellard
    /* reset all devices */
4342 bb0c6722 bellard
    for(re = first_reset_entry; re != NULL; re = re->next) {
4343 bb0c6722 bellard
        re->func(re->opaque);
4344 bb0c6722 bellard
    }
4345 bb0c6722 bellard
}
4346 bb0c6722 bellard
4347 bb0c6722 bellard
void qemu_system_reset_request(void)
4348 bb0c6722 bellard
{
4349 d1beab82 bellard
    if (no_reboot) {
4350 d1beab82 bellard
        shutdown_requested = 1;
4351 d1beab82 bellard
    } else {
4352 d1beab82 bellard
        reset_requested = 1;
4353 d1beab82 bellard
    }
4354 6a00d601 bellard
    if (cpu_single_env)
4355 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4356 bb0c6722 bellard
}
4357 bb0c6722 bellard
4358 bb0c6722 bellard
void qemu_system_shutdown_request(void)
4359 bb0c6722 bellard
{
4360 bb0c6722 bellard
    shutdown_requested = 1;
4361 6a00d601 bellard
    if (cpu_single_env)
4362 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4363 bb0c6722 bellard
}
4364 bb0c6722 bellard
4365 3475187d bellard
void qemu_system_powerdown_request(void)
4366 3475187d bellard
{
4367 3475187d bellard
    powerdown_requested = 1;
4368 6a00d601 bellard
    if (cpu_single_env)
4369 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
4370 bb0c6722 bellard
}
4371 bb0c6722 bellard
4372 877cf882 ths
#ifdef _WIN32
4373 56f3a5d0 aliguori
void host_main_loop_wait(int *timeout)
4374 56f3a5d0 aliguori
{
4375 56f3a5d0 aliguori
    int ret, ret2, i;
4376 f331110f bellard
    PollingEntry *pe;
4377 f331110f bellard
4378 c4b1fcc0 bellard
4379 f331110f bellard
    /* XXX: need to suppress polling by better using win32 events */
4380 f331110f bellard
    ret = 0;
4381 f331110f bellard
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
4382 f331110f bellard
        ret |= pe->func(pe->opaque);
4383 f331110f bellard
    }
4384 e6b1e558 ths
    if (ret == 0) {
4385 a18e524a bellard
        int err;
4386 a18e524a bellard
        WaitObjects *w = &wait_objects;
4387 3b46e624 ths
4388 56f3a5d0 aliguori
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
4389 a18e524a bellard
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
4390 a18e524a bellard
            if (w->func[ret - WAIT_OBJECT_0])
4391 a18e524a bellard
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
4392 3b46e624 ths
4393 5fafdf24 ths
            /* Check for additional signaled events */
4394 e6b1e558 ths
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
4395 3b46e624 ths
4396 e6b1e558 ths
                /* Check if event is signaled */
4397 e6b1e558 ths
                ret2 = WaitForSingleObject(w->events[i], 0);
4398 e6b1e558 ths
                if(ret2 == WAIT_OBJECT_0) {
4399 e6b1e558 ths
                    if (w->func[i])
4400 e6b1e558 ths
                        w->func[i](w->opaque[i]);
4401 e6b1e558 ths
                } else if (ret2 == WAIT_TIMEOUT) {
4402 e6b1e558 ths
                } else {
4403 e6b1e558 ths
                    err = GetLastError();
4404 e6b1e558 ths
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
4405 3b46e624 ths
                }
4406 3b46e624 ths
            }
4407 a18e524a bellard
        } else if (ret == WAIT_TIMEOUT) {
4408 a18e524a bellard
        } else {
4409 a18e524a bellard
            err = GetLastError();
4410 e6b1e558 ths
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
4411 a18e524a bellard
        }
4412 f331110f bellard
    }
4413 56f3a5d0 aliguori
4414 56f3a5d0 aliguori
    *timeout = 0;
4415 56f3a5d0 aliguori
}
4416 56f3a5d0 aliguori
#else
4417 56f3a5d0 aliguori
void host_main_loop_wait(int *timeout)
4418 56f3a5d0 aliguori
{
4419 56f3a5d0 aliguori
}
4420 fd1dff4b bellard
#endif
4421 56f3a5d0 aliguori
4422 56f3a5d0 aliguori
void main_loop_wait(int timeout)
4423 56f3a5d0 aliguori
{
4424 56f3a5d0 aliguori
    IOHandlerRecord *ioh;
4425 56f3a5d0 aliguori
    fd_set rfds, wfds, xfds;
4426 56f3a5d0 aliguori
    int ret, nfds;
4427 56f3a5d0 aliguori
    struct timeval tv;
4428 56f3a5d0 aliguori
4429 56f3a5d0 aliguori
    qemu_bh_update_timeout(&timeout);
4430 56f3a5d0 aliguori
4431 56f3a5d0 aliguori
    host_main_loop_wait(&timeout);
4432 56f3a5d0 aliguori
4433 fd1dff4b bellard
    /* poll any events */
4434 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
4435 fd1dff4b bellard
    nfds = -1;
4436 fd1dff4b bellard
    FD_ZERO(&rfds);
4437 fd1dff4b bellard
    FD_ZERO(&wfds);
4438 e035649e bellard
    FD_ZERO(&xfds);
4439 fd1dff4b bellard
    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4440 cafffd40 ths
        if (ioh->deleted)
4441 cafffd40 ths
            continue;
4442 fd1dff4b bellard
        if (ioh->fd_read &&
4443 fd1dff4b bellard
            (!ioh->fd_read_poll ||
4444 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
4445 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
4446 fd1dff4b bellard
            if (ioh->fd > nfds)
4447 fd1dff4b bellard
                nfds = ioh->fd;
4448 fd1dff4b bellard
        }
4449 fd1dff4b bellard
        if (ioh->fd_write) {
4450 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
4451 fd1dff4b bellard
            if (ioh->fd > nfds)
4452 fd1dff4b bellard
                nfds = ioh->fd;
4453 fd1dff4b bellard
        }
4454 fd1dff4b bellard
    }
4455 3b46e624 ths
4456 56f3a5d0 aliguori
    tv.tv_sec = timeout / 1000;
4457 56f3a5d0 aliguori
    tv.tv_usec = (timeout % 1000) * 1000;
4458 56f3a5d0 aliguori
4459 e035649e bellard
#if defined(CONFIG_SLIRP)
4460 63a01ef8 aliguori
    if (slirp_is_inited()) {
4461 e035649e bellard
        slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
4462 e035649e bellard
    }
4463 e035649e bellard
#endif
4464 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
4465 fd1dff4b bellard
    if (ret > 0) {
4466 cafffd40 ths
        IOHandlerRecord **pioh;
4467 cafffd40 ths
4468 cafffd40 ths
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
4469 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
4470 fd1dff4b bellard
                ioh->fd_read(ioh->opaque);
4471 7c9d8e07 bellard
            }
4472 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
4473 fd1dff4b bellard
                ioh->fd_write(ioh->opaque);
4474 c4b1fcc0 bellard
            }
4475 b4608c04 bellard
        }
4476 cafffd40 ths
4477 cafffd40 ths
        /* remove deleted IO handlers */
4478 cafffd40 ths
        pioh = &first_io_handler;
4479 cafffd40 ths
        while (*pioh) {
4480 cafffd40 ths
            ioh = *pioh;
4481 cafffd40 ths
            if (ioh->deleted) {
4482 cafffd40 ths
                *pioh = ioh->next;
4483 cafffd40 ths
                qemu_free(ioh);
4484 5fafdf24 ths
            } else
4485 cafffd40 ths
                pioh = &ioh->next;
4486 cafffd40 ths
        }
4487 fd1dff4b bellard
    }
4488 c20709aa bellard
#if defined(CONFIG_SLIRP)
4489 63a01ef8 aliguori
    if (slirp_is_inited()) {
4490 e035649e bellard
        if (ret < 0) {
4491 e035649e bellard
            FD_ZERO(&rfds);
4492 e035649e bellard
            FD_ZERO(&wfds);
4493 e035649e bellard
            FD_ZERO(&xfds);
4494 c20709aa bellard
        }
4495 e035649e bellard
        slirp_select_poll(&rfds, &wfds, &xfds);
4496 fd1dff4b bellard
    }
4497 c20709aa bellard
#endif
4498 c20709aa bellard
4499 fd1dff4b bellard
    if (vm_running) {
4500 21b20814 edgar_igl
        if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
4501 5fafdf24 ths
        qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
4502 fd1dff4b bellard
                        qemu_get_clock(vm_clock));
4503 fd1dff4b bellard
    }
4504 423f0742 pbrook
4505 fd1dff4b bellard
    /* real time timers */
4506 5fafdf24 ths
    qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
4507 fd1dff4b bellard
                    qemu_get_clock(rt_clock));
4508 423f0742 pbrook
4509 d5d08334 balrog
    if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
4510 d5d08334 balrog
        alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED);
4511 d5d08334 balrog
        qemu_rearm_alarm_timer(alarm_timer);
4512 d5d08334 balrog
    }
4513 b99dc0d1 balrog
4514 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
4515 423f0742 pbrook
       them.  */
4516 423f0742 pbrook
    qemu_bh_poll();
4517 3b46e624 ths
4518 5905b2e5 bellard
}
4519 5905b2e5 bellard
4520 9596ebb7 pbrook
static int main_loop(void)
4521 5905b2e5 bellard
{
4522 5905b2e5 bellard
    int ret, timeout;
4523 89bfc105 bellard
#ifdef CONFIG_PROFILER
4524 89bfc105 bellard
    int64_t ti;
4525 89bfc105 bellard
#endif
4526 6a00d601 bellard
    CPUState *env;
4527 5905b2e5 bellard
4528 6a00d601 bellard
    cur_cpu = first_cpu;
4529 ee5605e5 balrog
    next_cpu = cur_cpu->next_cpu ?: first_cpu;
4530 5905b2e5 bellard
    for(;;) {
4531 5905b2e5 bellard
        if (vm_running) {
4532 15a76449 bellard
4533 15a76449 bellard
            for(;;) {
4534 15a76449 bellard
                /* get next cpu */
4535 ee5605e5 balrog
                env = next_cpu;
4536 89bfc105 bellard
#ifdef CONFIG_PROFILER
4537 89bfc105 bellard
                ti = profile_getclock();
4538 89bfc105 bellard
#endif
4539 2e70f6ef pbrook
                if (use_icount) {
4540 2e70f6ef pbrook
                    int64_t count;
4541 2e70f6ef pbrook
                    int decr;
4542 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
4543 2e70f6ef pbrook
                    env->icount_decr.u16.low = 0;
4544 2e70f6ef pbrook
                    env->icount_extra = 0;
4545 2e70f6ef pbrook
                    count = qemu_next_deadline();
4546 2e70f6ef pbrook
                    count = (count + (1 << icount_time_shift) - 1)
4547 2e70f6ef pbrook
                            >> icount_time_shift;
4548 2e70f6ef pbrook
                    qemu_icount += count;
4549 2e70f6ef pbrook
                    decr = (count > 0xffff) ? 0xffff : count;
4550 2e70f6ef pbrook
                    count -= decr;
4551 2e70f6ef pbrook
                    env->icount_decr.u16.low = decr;
4552 2e70f6ef pbrook
                    env->icount_extra = count;
4553 2e70f6ef pbrook
                }
4554 6a00d601 bellard
                ret = cpu_exec(env);
4555 89bfc105 bellard
#ifdef CONFIG_PROFILER
4556 89bfc105 bellard
                qemu_time += profile_getclock() - ti;
4557 89bfc105 bellard
#endif
4558 2e70f6ef pbrook
                if (use_icount) {
4559 2e70f6ef pbrook
                    /* Fold pending instructions back into the
4560 2e70f6ef pbrook
                       instruction counter, and clear the interrupt flag.  */
4561 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low
4562 2e70f6ef pbrook
                                    + env->icount_extra);
4563 2e70f6ef pbrook
                    env->icount_decr.u32 = 0;
4564 2e70f6ef pbrook
                    env->icount_extra = 0;
4565 2e70f6ef pbrook
                }
4566 ee5605e5 balrog
                next_cpu = env->next_cpu ?: first_cpu;
4567 95b01009 aurel32
                if (event_pending && likely(ret != EXCP_DEBUG)) {
4568 ee5605e5 balrog
                    ret = EXCP_INTERRUPT;
4569 ee5605e5 balrog
                    event_pending = 0;
4570 ee5605e5 balrog
                    break;
4571 ee5605e5 balrog
                }
4572 bd967e05 pbrook
                if (ret == EXCP_HLT) {
4573 bd967e05 pbrook
                    /* Give the next CPU a chance to run.  */
4574 bd967e05 pbrook
                    cur_cpu = env;
4575 bd967e05 pbrook
                    continue;
4576 bd967e05 pbrook
                }
4577 15a76449 bellard
                if (ret != EXCP_HALTED)
4578 15a76449 bellard
                    break;
4579 15a76449 bellard
                /* all CPUs are halted ? */
4580 bd967e05 pbrook
                if (env == cur_cpu)
4581 15a76449 bellard
                    break;
4582 15a76449 bellard
            }
4583 15a76449 bellard
            cur_cpu = env;
4584 15a76449 bellard
4585 5905b2e5 bellard
            if (shutdown_requested) {
4586 3475187d bellard
                ret = EXCP_INTERRUPT;
4587 b2f76161 aurel32
                if (no_shutdown) {
4588 b2f76161 aurel32
                    vm_stop(0);
4589 b2f76161 aurel32
                    no_shutdown = 0;
4590 b2f76161 aurel32
                }
4591 b2f76161 aurel32
                else
4592 b2f76161 aurel32
                    break;
4593 5905b2e5 bellard
            }
4594 5905b2e5 bellard
            if (reset_requested) {
4595 5905b2e5 bellard
                reset_requested = 0;
4596 5905b2e5 bellard
                qemu_system_reset();
4597 3475187d bellard
                ret = EXCP_INTERRUPT;
4598 3475187d bellard
            }
4599 3475187d bellard
            if (powerdown_requested) {
4600 3475187d bellard
                powerdown_requested = 0;
4601 3475187d bellard
                qemu_system_powerdown();
4602 3475187d bellard
                ret = EXCP_INTERRUPT;
4603 5905b2e5 bellard
            }
4604 95b01009 aurel32
            if (unlikely(ret == EXCP_DEBUG)) {
4605 5905b2e5 bellard
                vm_stop(EXCP_DEBUG);
4606 5905b2e5 bellard
            }
4607 bd967e05 pbrook
            /* If all cpus are halted then wait until the next IRQ */
4608 5905b2e5 bellard
            /* XXX: use timeout computed from timers */
4609 2e70f6ef pbrook
            if (ret == EXCP_HALTED) {
4610 2e70f6ef pbrook
                if (use_icount) {
4611 2e70f6ef pbrook
                    int64_t add;
4612 2e70f6ef pbrook
                    int64_t delta;
4613 2e70f6ef pbrook
                    /* Advance virtual time to the next event.  */
4614 2e70f6ef pbrook
                    if (use_icount == 1) {
4615 2e70f6ef pbrook
                        /* When not using an adaptive execution frequency
4616 2e70f6ef pbrook
                           we tend to get badly out of sync with real time,
4617 bf20dc07 ths
                           so just delay for a reasonable amount of time.  */
4618 2e70f6ef pbrook
                        delta = 0;
4619 2e70f6ef pbrook
                    } else {
4620 2e70f6ef pbrook
                        delta = cpu_get_icount() - cpu_get_clock();
4621 2e70f6ef pbrook
                    }
4622 2e70f6ef pbrook
                    if (delta > 0) {
4623 2e70f6ef pbrook
                        /* If virtual time is ahead of real time then just
4624 2e70f6ef pbrook
                           wait for IO.  */
4625 2e70f6ef pbrook
                        timeout = (delta / 1000000) + 1;
4626 2e70f6ef pbrook
                    } else {
4627 2e70f6ef pbrook
                        /* Wait for either IO to occur or the next
4628 2e70f6ef pbrook
                           timer event.  */
4629 2e70f6ef pbrook
                        add = qemu_next_deadline();
4630 2e70f6ef pbrook
                        /* We advance the timer before checking for IO.
4631 2e70f6ef pbrook
                           Limit the amount we advance so that early IO
4632 2e70f6ef pbrook
                           activity won't get the guest too far ahead.  */
4633 2e70f6ef pbrook
                        if (add > 10000000)
4634 2e70f6ef pbrook
                            add = 10000000;
4635 2e70f6ef pbrook
                        delta += add;
4636 2e70f6ef pbrook
                        add = (add + (1 << icount_time_shift) - 1)
4637 2e70f6ef pbrook
                              >> icount_time_shift;
4638 2e70f6ef pbrook
                        qemu_icount += add;
4639 2e70f6ef pbrook
                        timeout = delta / 1000000;
4640 2e70f6ef pbrook
                        if (timeout < 0)
4641 2e70f6ef pbrook
                            timeout = 0;
4642 2e70f6ef pbrook
                    }
4643 2e70f6ef pbrook
                } else {
4644 0a1af395 aliguori
                    timeout = 5000;
4645 2e70f6ef pbrook
                }
4646 2e70f6ef pbrook
            } else {
4647 5905b2e5 bellard
                timeout = 0;
4648 2e70f6ef pbrook
            }
4649 5905b2e5 bellard
        } else {
4650 98448f58 blueswir1
            if (shutdown_requested) {
4651 98448f58 blueswir1
                ret = EXCP_INTERRUPT;
4652 5b08fc10 aliguori
                break;
4653 98448f58 blueswir1
            }
4654 0a1af395 aliguori
            timeout = 5000;
4655 5905b2e5 bellard
        }
4656 89bfc105 bellard
#ifdef CONFIG_PROFILER
4657 89bfc105 bellard
        ti = profile_getclock();
4658 89bfc105 bellard
#endif
4659 5905b2e5 bellard
        main_loop_wait(timeout);
4660 89bfc105 bellard
#ifdef CONFIG_PROFILER
4661 89bfc105 bellard
        dev_time += profile_getclock() - ti;
4662 89bfc105 bellard
#endif
4663 b4608c04 bellard
    }
4664 34865134 bellard
    cpu_disable_ticks();
4665 34865134 bellard
    return ret;
4666 b4608c04 bellard
}
4667 b4608c04 bellard
4668 15f82208 ths
static void help(int exitcode)
4669 0824d6fc bellard
{
4670 68d0f70e bellard
    printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
4671 0db63474 bellard
           "usage: %s [options] [disk_image]\n"
4672 0824d6fc bellard
           "\n"
4673 a20dd508 bellard
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4674 fc01f7e7 bellard
           "\n"
4675 a20dd508 bellard
           "Standard options:\n"
4676 cc1daa40 bellard
           "-M machine      select emulated machine (-M ? for list)\n"
4677 5adb4839 pbrook
           "-cpu cpu        select CPU (-cpu ? for list)\n"
4678 c45886db bellard
           "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
4679 36b486bb bellard
           "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
4680 36b486bb bellard
           "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
4681 c4b1fcc0 bellard
           "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4682 a1620fac aurel32
           "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4683 a1620fac aurel32
           "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
4684 9f7965c7 aliguori
           "       [,cache=writethrough|writeback|none][,format=f]\n"
4685 e4bcb14c ths
           "                use 'file' as a drive image\n"
4686 3e3d5815 balrog
           "-mtdblock file  use 'file' as on-board Flash memory image\n"
4687 a1bb27b1 pbrook
           "-sd file        use 'file' as SecureDigital card image\n"
4688 86f55663 j_mayer
           "-pflash file    use 'file' as a parallel flash image\n"
4689 eec85c2a ths
           "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4690 667accab ths
           "-snapshot       write to temporary files instead of disk image files\n"
4691 667accab ths
#ifdef CONFIG_SDL
4692 43523e93 ths
           "-no-frame       open SDL window without a frame and window decorations\n"
4693 3780e197 ths
           "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4694 667accab ths
           "-no-quit        disable SDL window close capability\n"
4695 667accab ths
#endif
4696 52ca8d6a bellard
#ifdef TARGET_I386
4697 52ca8d6a bellard
           "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
4698 52ca8d6a bellard
#endif
4699 a00bad7e bellard
           "-m megs         set virtual RAM size to megs MB [default=%d]\n"
4700 91fc2119 bellard
           "-smp n          set the number of CPUs to 'n' [default=1]\n"
4701 c4b1fcc0 bellard
           "-nographic      disable graphical output and redirect serial I/Os to console\n"
4702 a171fe39 balrog
           "-portrait       rotate graphical output 90 deg left (only PXA LCD)\n"
4703 4ca0074c bellard
#ifndef _WIN32
4704 667accab ths
           "-k language     use keyboard layout (for example \"fr\" for French)\n"
4705 4ca0074c bellard
#endif
4706 1d14ffa9 bellard
#ifdef HAS_AUDIO
4707 1d14ffa9 bellard
           "-audio-help     print list of audio drivers and their options\n"
4708 c0fe3827 bellard
           "-soundhw c1,... enable audio support\n"
4709 c0fe3827 bellard
           "                and only specified sound cards (comma separated list)\n"
4710 c0fe3827 bellard
           "                use -soundhw ? to get the list of supported cards\n"
4711 6a36d84e bellard
           "                use -soundhw all to enable all of them\n"
4712 1d14ffa9 bellard
#endif
4713 3893c124 malc
           "-vga [std|cirrus|vmware]\n"
4714 3893c124 malc
           "                select video card type\n"
4715 89980284 bellard
           "-localtime      set the real time clock to local time [default=utc]\n"
4716 d63d307f bellard
           "-full-screen    start in full screen\n"
4717 a09db21f bellard
#ifdef TARGET_I386
4718 a09db21f bellard
           "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
4719 a09db21f bellard
#endif
4720 b389dbfb bellard
           "-usb            enable the USB driver (will be the default soon)\n"
4721 b389dbfb bellard
           "-usbdevice name add the host or guest USB device 'name'\n"
4722 6f7e9aec bellard
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4723 6f7e9aec bellard
           "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
4724 bb0c6722 bellard
#endif
4725 c35734b2 ths
           "-name string    set the name of the guest\n"
4726 8fcb1b90 blueswir1
           "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4727 c4b1fcc0 bellard
           "\n"
4728 c4b1fcc0 bellard
           "Network options:\n"
4729 a41b2ff2 pbrook
           "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4730 7c9d8e07 bellard
           "                create a new Network Interface Card and connect it to VLAN 'n'\n"
4731 c20709aa bellard
#ifdef CONFIG_SLIRP
4732 115defd1 pbrook
           "-net user[,vlan=n][,hostname=host]\n"
4733 115defd1 pbrook
           "                connect the user mode network stack to VLAN 'n' and send\n"
4734 115defd1 pbrook
           "                hostname 'host' to DHCP clients\n"
4735 7c9d8e07 bellard
#endif
4736 7fb843f8 bellard
#ifdef _WIN32
4737 7fb843f8 bellard
           "-net tap[,vlan=n],ifname=name\n"
4738 7fb843f8 bellard
           "                connect the host TAP network interface to VLAN 'n'\n"
4739 7fb843f8 bellard
#else
4740 b46a8906 ths
           "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4741 b46a8906 ths
           "                connect the host TAP network interface to VLAN 'n' and use the\n"
4742 b46a8906 ths
           "                network scripts 'file' (default=%s)\n"
4743 b46a8906 ths
           "                and 'dfile' (default=%s);\n"
4744 b46a8906 ths
           "                use '[down]script=no' to disable script execution;\n"
4745 7c9d8e07 bellard
           "                use 'fd=h' to connect to an already opened TAP interface\n"
4746 7fb843f8 bellard
#endif
4747 6a00d601 bellard
           "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4748 7c9d8e07 bellard
           "                connect the vlan 'n' to another VLAN using a socket connection\n"
4749 3d830459 bellard
           "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4750 3d830459 bellard
           "                connect the vlan 'n' to multicast maddr and port\n"
4751 8a16d273 ths
#ifdef CONFIG_VDE
4752 8a16d273 ths
           "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4753 8a16d273 ths
           "                connect the vlan 'n' to port 'n' of a vde switch running\n"
4754 8a16d273 ths
           "                on host and listening for incoming connections on 'socketpath'.\n"
4755 8a16d273 ths
           "                Use group 'groupname' and mode 'octalmode' to change default\n"
4756 8a16d273 ths
           "                ownership and permissions for communication port.\n"
4757 8a16d273 ths
#endif
4758 7c9d8e07 bellard
           "-net none       use it alone to have zero network devices; if no -net option\n"
4759 7c9d8e07 bellard
           "                is provided, the default is '-net nic -net user'\n"
4760 7c9d8e07 bellard
           "\n"
4761 7c9d8e07 bellard
#ifdef CONFIG_SLIRP
4762 0db1137d ths
           "-tftp dir       allow tftp access to files in dir [-net user]\n"
4763 47d5d01a ths
           "-bootp file     advertise file in BOOTP replies\n"
4764 7c9d8e07 bellard
#ifndef _WIN32
4765 7c9d8e07 bellard
           "-smb dir        allow SMB access to files in 'dir' [-net user]\n"
4766 c94c8d64 bellard
#endif
4767 9bf05444 bellard
           "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4768 7c9d8e07 bellard
           "                redirect TCP or UDP connections from host to guest [-net user]\n"
4769 c20709aa bellard
#endif
4770 a20dd508 bellard
           "\n"
4771 c4b1fcc0 bellard
           "Linux boot specific:\n"
4772 a20dd508 bellard
           "-kernel bzImage use 'bzImage' as kernel image\n"
4773 a20dd508 bellard
           "-append cmdline use 'cmdline' as kernel command line\n"
4774 a20dd508 bellard
           "-initrd file    use 'file' as initial ram disk\n"
4775 fc01f7e7 bellard
           "\n"
4776 330d0414 bellard
           "Debug/Expert options:\n"
4777 82c643ff bellard
           "-monitor dev    redirect the monitor to char device 'dev'\n"
4778 82c643ff bellard
           "-serial dev     redirect the serial port to char device 'dev'\n"
4779 6508fe59 bellard
           "-parallel dev   redirect the parallel port to char device 'dev'\n"
4780 f7cce898 bellard
           "-pidfile file   Write PID to 'file'\n"
4781 cd6f1169 bellard
           "-S              freeze CPU at startup (use 'c' to start execution)\n"
4782 cfc3475a pbrook
           "-s              wait gdb connection to port\n"
4783 cfc3475a pbrook
           "-p port         set gdb connection port [default=%s]\n"
4784 f193c797 bellard
           "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
4785 46d4767d bellard
           "-hdachs c,h,s[,t]  force hard disk 0 physical geometry and the optional BIOS\n"
4786 46d4767d bellard
           "                translation (t=none or lba) (usually qemu can guess them)\n"
4787 87b47350 bellard
           "-L path         set the directory for the BIOS, VGA BIOS and keymaps\n"
4788 d993e026 bellard
#ifdef USE_KQEMU
4789 6515b203 bellard
           "-kernel-kqemu   enable KQEMU full virtualization (default is user mode only)\n"
4790 d993e026 bellard
           "-no-kqemu       disable KQEMU kernel module usage\n"
4791 d993e026 bellard
#endif
4792 bb0c6722 bellard
#ifdef TARGET_I386
4793 6515b203 bellard
           "-no-acpi        disable ACPI\n"
4794 bb0c6722 bellard
#endif
4795 4d3b6f6e balrog
#ifdef CONFIG_CURSES
4796 4d3b6f6e balrog
           "-curses         use a curses/ncurses interface instead of SDL\n"
4797 4d3b6f6e balrog
#endif
4798 d1beab82 bellard
           "-no-reboot      exit instead of rebooting\n"
4799 b2f76161 aurel32
           "-no-shutdown    stop before shutdown\n"
4800 a8080006 aurel32
           "-loadvm [tag|id]  start right away with a saved state (loadvm in monitor)\n"
4801 24236869 bellard
           "-vnc display    start a VNC server on display\n"
4802 71e3ceb8 ths
#ifndef _WIN32
4803 71e3ceb8 ths
           "-daemonize      daemonize QEMU after initializing\n"
4804 71e3ceb8 ths
#endif
4805 9ae02555 ths
           "-option-rom rom load a file, rom, into the option ROM space\n"
4806 66508601 blueswir1
#ifdef TARGET_SPARC
4807 66508601 blueswir1
           "-prom-env variable=value  set OpenBIOS nvram variables\n"
4808 66508601 blueswir1
#endif
4809 f3dcfada ths
           "-clock          force the use of the given methods for timer alarm.\n"
4810 3adda04c aurel32
           "                To see what timers are available use -clock ?\n"
4811 bce61846 bellard
           "-startdate      select initial date of the clock\n"
4812 2e70f6ef pbrook
           "-icount [N|auto]\n"
4813 dd5d6fe9 pbrook
           "                Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4814 0824d6fc bellard
           "\n"
4815 82c643ff bellard
           "During emulation, the following keys are useful:\n"
4816 032a8c9e bellard
           "ctrl-alt-f      toggle full screen\n"
4817 032a8c9e bellard
           "ctrl-alt-n      switch to virtual console 'n'\n"
4818 032a8c9e bellard
           "ctrl-alt        toggle mouse and keyboard grab\n"
4819 82c643ff bellard
           "\n"
4820 82c643ff bellard
           "When using -nographic, press 'ctrl-a h' to get some help.\n"
4821 82c643ff bellard
           ,
4822 0db63474 bellard
           "qemu",
4823 a00bad7e bellard
           DEFAULT_RAM_SIZE,
4824 7c9d8e07 bellard
#ifndef _WIN32
4825 a00bad7e bellard
           DEFAULT_NETWORK_SCRIPT,
4826 b46a8906 ths
           DEFAULT_NETWORK_DOWN_SCRIPT,
4827 7c9d8e07 bellard
#endif
4828 6e44ba7f bellard
           DEFAULT_GDBSTUB_PORT,
4829 bce61846 bellard
           "/tmp/qemu.log");
4830 15f82208 ths
    exit(exitcode);
4831 0824d6fc bellard
}
4832 0824d6fc bellard
4833 cd6f1169 bellard
#define HAS_ARG 0x0001
4834 cd6f1169 bellard
4835 cd6f1169 bellard
enum {
4836 cd6f1169 bellard
    QEMU_OPTION_h,
4837 cd6f1169 bellard
4838 cc1daa40 bellard
    QEMU_OPTION_M,
4839 94fc95cd j_mayer
    QEMU_OPTION_cpu,
4840 cd6f1169 bellard
    QEMU_OPTION_fda,
4841 cd6f1169 bellard
    QEMU_OPTION_fdb,
4842 cd6f1169 bellard
    QEMU_OPTION_hda,
4843 cd6f1169 bellard
    QEMU_OPTION_hdb,
4844 cd6f1169 bellard
    QEMU_OPTION_hdc,
4845 cd6f1169 bellard
    QEMU_OPTION_hdd,
4846 e4bcb14c ths
    QEMU_OPTION_drive,
4847 cd6f1169 bellard
    QEMU_OPTION_cdrom,
4848 3e3d5815 balrog
    QEMU_OPTION_mtdblock,
4849 a1bb27b1 pbrook
    QEMU_OPTION_sd,
4850 86f55663 j_mayer
    QEMU_OPTION_pflash,
4851 cd6f1169 bellard
    QEMU_OPTION_boot,
4852 cd6f1169 bellard
    QEMU_OPTION_snapshot,
4853 52ca8d6a bellard
#ifdef TARGET_I386
4854 52ca8d6a bellard
    QEMU_OPTION_no_fd_bootchk,
4855 52ca8d6a bellard
#endif
4856 cd6f1169 bellard
    QEMU_OPTION_m,
4857 cd6f1169 bellard
    QEMU_OPTION_nographic,
4858 a171fe39 balrog
    QEMU_OPTION_portrait,
4859 1d14ffa9 bellard
#ifdef HAS_AUDIO
4860 1d14ffa9 bellard
    QEMU_OPTION_audio_help,
4861 1d14ffa9 bellard
    QEMU_OPTION_soundhw,
4862 1d14ffa9 bellard
#endif
4863 cd6f1169 bellard
4864 7c9d8e07 bellard
    QEMU_OPTION_net,
4865 c7f74643 bellard
    QEMU_OPTION_tftp,
4866 47d5d01a ths
    QEMU_OPTION_bootp,
4867 9d728e8c bellard
    QEMU_OPTION_smb,
4868 9bf05444 bellard
    QEMU_OPTION_redir,
4869 cd6f1169 bellard
4870 cd6f1169 bellard
    QEMU_OPTION_kernel,
4871 cd6f1169 bellard
    QEMU_OPTION_append,
4872 cd6f1169 bellard
    QEMU_OPTION_initrd,
4873 cd6f1169 bellard
4874 cd6f1169 bellard
    QEMU_OPTION_S,
4875 cd6f1169 bellard
    QEMU_OPTION_s,
4876 cd6f1169 bellard
    QEMU_OPTION_p,
4877 cd6f1169 bellard
    QEMU_OPTION_d,
4878 cd6f1169 bellard
    QEMU_OPTION_hdachs,
4879 cd6f1169 bellard
    QEMU_OPTION_L,
4880 1192dad8 j_mayer
    QEMU_OPTION_bios,
4881 3d11d0eb bellard
    QEMU_OPTION_k,
4882 ee22c2f7 bellard
    QEMU_OPTION_localtime,
4883 e9b137c2 bellard
    QEMU_OPTION_g,
4884 3893c124 malc
    QEMU_OPTION_vga,
4885 20d8a3ed ths
    QEMU_OPTION_echr,
4886 82c643ff bellard
    QEMU_OPTION_monitor,
4887 82c643ff bellard
    QEMU_OPTION_serial,
4888 6508fe59 bellard
    QEMU_OPTION_parallel,
4889 d63d307f bellard
    QEMU_OPTION_loadvm,
4890 d63d307f bellard
    QEMU_OPTION_full_screen,
4891 43523e93 ths
    QEMU_OPTION_no_frame,
4892 3780e197 ths
    QEMU_OPTION_alt_grab,
4893 667accab ths
    QEMU_OPTION_no_quit,
4894 f7cce898 bellard
    QEMU_OPTION_pidfile,
4895 d993e026 bellard
    QEMU_OPTION_no_kqemu,
4896 89bfc105 bellard
    QEMU_OPTION_kernel_kqemu,
4897 a09db21f bellard
    QEMU_OPTION_win2k_hack,
4898 bb36d470 bellard
    QEMU_OPTION_usb,
4899 a594cfbf bellard
    QEMU_OPTION_usbdevice,
4900 6a00d601 bellard
    QEMU_OPTION_smp,
4901 24236869 bellard
    QEMU_OPTION_vnc,
4902 6515b203 bellard
    QEMU_OPTION_no_acpi,
4903 4d3b6f6e balrog
    QEMU_OPTION_curses,
4904 d1beab82 bellard
    QEMU_OPTION_no_reboot,
4905 b2f76161 aurel32
    QEMU_OPTION_no_shutdown,
4906 9467cd46 balrog
    QEMU_OPTION_show_cursor,
4907 71e3ceb8 ths
    QEMU_OPTION_daemonize,
4908 9ae02555 ths
    QEMU_OPTION_option_rom,
4909 c35734b2 ths
    QEMU_OPTION_semihosting,
4910 c35734b2 ths
    QEMU_OPTION_name,
4911 66508601 blueswir1
    QEMU_OPTION_prom_env,
4912 2b8f2d41 balrog
    QEMU_OPTION_old_param,
4913 f3dcfada ths
    QEMU_OPTION_clock,
4914 7e0af5d0 bellard
    QEMU_OPTION_startdate,
4915 26a5f13b bellard
    QEMU_OPTION_tb_size,
4916 2e70f6ef pbrook
    QEMU_OPTION_icount,
4917 8fcb1b90 blueswir1
    QEMU_OPTION_uuid,
4918 5bb7910a aliguori
    QEMU_OPTION_incoming,
4919 cd6f1169 bellard
};
4920 cd6f1169 bellard
4921 cd6f1169 bellard
typedef struct QEMUOption {
4922 cd6f1169 bellard
    const char *name;
4923 cd6f1169 bellard
    int flags;
4924 cd6f1169 bellard
    int index;
4925 cd6f1169 bellard
} QEMUOption;
4926 cd6f1169 bellard
4927 dbed7e40 blueswir1
static const QEMUOption qemu_options[] = {
4928 cd6f1169 bellard
    { "h", 0, QEMU_OPTION_h },
4929 64423fb2 pbrook
    { "help", 0, QEMU_OPTION_h },
4930 cd6f1169 bellard
4931 cc1daa40 bellard
    { "M", HAS_ARG, QEMU_OPTION_M },
4932 94fc95cd j_mayer
    { "cpu", HAS_ARG, QEMU_OPTION_cpu },
4933 cd6f1169 bellard
    { "fda", HAS_ARG, QEMU_OPTION_fda },
4934 cd6f1169 bellard
    { "fdb", HAS_ARG, QEMU_OPTION_fdb },
4935 cd6f1169 bellard
    { "hda", HAS_ARG, QEMU_OPTION_hda },
4936 cd6f1169 bellard
    { "hdb", HAS_ARG, QEMU_OPTION_hdb },
4937 cd6f1169 bellard
    { "hdc", HAS_ARG, QEMU_OPTION_hdc },
4938 cd6f1169 bellard
    { "hdd", HAS_ARG, QEMU_OPTION_hdd },
4939 e4bcb14c ths
    { "drive", HAS_ARG, QEMU_OPTION_drive },
4940 cd6f1169 bellard
    { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
4941 3e3d5815 balrog
    { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
4942 a1bb27b1 pbrook
    { "sd", HAS_ARG, QEMU_OPTION_sd },
4943 86f55663 j_mayer
    { "pflash", HAS_ARG, QEMU_OPTION_pflash },
4944 cd6f1169 bellard
    { "boot", HAS_ARG, QEMU_OPTION_boot },
4945 cd6f1169 bellard
    { "snapshot", 0, QEMU_OPTION_snapshot },
4946 52ca8d6a bellard
#ifdef TARGET_I386
4947 52ca8d6a bellard
    { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
4948 52ca8d6a bellard
#endif
4949 cd6f1169 bellard
    { "m", HAS_ARG, QEMU_OPTION_m },
4950 cd6f1169 bellard
    { "nographic", 0, QEMU_OPTION_nographic },
4951 a171fe39 balrog
    { "portrait", 0, QEMU_OPTION_portrait },
4952 3d11d0eb bellard
    { "k", HAS_ARG, QEMU_OPTION_k },
4953 1d14ffa9 bellard
#ifdef HAS_AUDIO
4954 1d14ffa9 bellard
    { "audio-help", 0, QEMU_OPTION_audio_help },
4955 1d14ffa9 bellard
    { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
4956 1d14ffa9 bellard
#endif
4957 cd6f1169 bellard
4958 7c9d8e07 bellard
    { "net", HAS_ARG, QEMU_OPTION_net},
4959 158156d1 bellard
#ifdef CONFIG_SLIRP
4960 c7f74643 bellard
    { "tftp", HAS_ARG, QEMU_OPTION_tftp },
4961 47d5d01a ths
    { "bootp", HAS_ARG, QEMU_OPTION_bootp },
4962 c94c8d64 bellard
#ifndef _WIN32
4963 9d728e8c bellard
    { "smb", HAS_ARG, QEMU_OPTION_smb },
4964 c94c8d64 bellard
#endif
4965 9bf05444 bellard
    { "redir", HAS_ARG, QEMU_OPTION_redir },
4966 158156d1 bellard
#endif
4967 cd6f1169 bellard
4968 cd6f1169 bellard
    { "kernel", HAS_ARG, QEMU_OPTION_kernel },
4969 cd6f1169 bellard
    { "append", HAS_ARG, QEMU_OPTION_append },
4970 cd6f1169 bellard
    { "initrd", HAS_ARG, QEMU_OPTION_initrd },
4971 cd6f1169 bellard
4972 cd6f1169 bellard
    { "S", 0, QEMU_OPTION_S },
4973 cd6f1169 bellard
    { "s", 0, QEMU_OPTION_s },
4974 cd6f1169 bellard
    { "p", HAS_ARG, QEMU_OPTION_p },
4975 cd6f1169 bellard
    { "d", HAS_ARG, QEMU_OPTION_d },
4976 cd6f1169 bellard
    { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
4977 cd6f1169 bellard
    { "L", HAS_ARG, QEMU_OPTION_L },
4978 1192dad8 j_mayer
    { "bios", HAS_ARG, QEMU_OPTION_bios },
4979 d993e026 bellard
#ifdef USE_KQEMU
4980 d993e026 bellard
    { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
4981 89bfc105 bellard
    { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
4982 d993e026 bellard
#endif
4983 6f7e9aec bellard
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
4984 e9b137c2 bellard
    { "g", 1, QEMU_OPTION_g },
4985 77d4bc34 bellard
#endif
4986 ee22c2f7 bellard
    { "localtime", 0, QEMU_OPTION_localtime },
4987 3893c124 malc
    { "vga", HAS_ARG, QEMU_OPTION_vga },
4988 8b6e0729 balrog
    { "echr", HAS_ARG, QEMU_OPTION_echr },
4989 8b6e0729 balrog
    { "monitor", HAS_ARG, QEMU_OPTION_monitor },
4990 8b6e0729 balrog
    { "serial", HAS_ARG, QEMU_OPTION_serial },
4991 8b6e0729 balrog
    { "parallel", HAS_ARG, QEMU_OPTION_parallel },
4992 d63d307f bellard
    { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
4993 d63d307f bellard
    { "full-screen", 0, QEMU_OPTION_full_screen },
4994 667accab ths
#ifdef CONFIG_SDL
4995 43523e93 ths
    { "no-frame", 0, QEMU_OPTION_no_frame },
4996 3780e197 ths
    { "alt-grab", 0, QEMU_OPTION_alt_grab },
4997 667accab ths
    { "no-quit", 0, QEMU_OPTION_no_quit },
4998 667accab ths
#endif
4999 f7cce898 bellard
    { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
5000 a09db21f bellard
    { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
5001 a594cfbf bellard
    { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
5002 6a00d601 bellard
    { "smp", HAS_ARG, QEMU_OPTION_smp },
5003 24236869 bellard
    { "vnc", HAS_ARG, QEMU_OPTION_vnc },
5004 4d3b6f6e balrog
#ifdef CONFIG_CURSES
5005 4d3b6f6e balrog
    { "curses", 0, QEMU_OPTION_curses },
5006 4d3b6f6e balrog
#endif
5007 8fcb1b90 blueswir1
    { "uuid", HAS_ARG, QEMU_OPTION_uuid },
5008 96d30e48 ths
5009 1f04275e bellard
    /* temporary options */
5010 a594cfbf bellard
    { "usb", 0, QEMU_OPTION_usb },
5011 6515b203 bellard
    { "no-acpi", 0, QEMU_OPTION_no_acpi },
5012 d1beab82 bellard
    { "no-reboot", 0, QEMU_OPTION_no_reboot },
5013 b2f76161 aurel32
    { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
5014 9467cd46 balrog
    { "show-cursor", 0, QEMU_OPTION_show_cursor },
5015 71e3ceb8 ths
    { "daemonize", 0, QEMU_OPTION_daemonize },
5016 9ae02555 ths
    { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
5017 a87295e8 pbrook
#if defined(TARGET_ARM) || defined(TARGET_M68K)
5018 8e71621f pbrook
    { "semihosting", 0, QEMU_OPTION_semihosting },
5019 8e71621f pbrook
#endif
5020 c35734b2 ths
    { "name", HAS_ARG, QEMU_OPTION_name },
5021 66508601 blueswir1
#if defined(TARGET_SPARC)
5022 66508601 blueswir1
    { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
5023 66508601 blueswir1
#endif
5024 2b8f2d41 balrog
#if defined(TARGET_ARM)
5025 2b8f2d41 balrog
    { "old-param", 0, QEMU_OPTION_old_param },
5026 2b8f2d41 balrog
#endif
5027 f3dcfada ths
    { "clock", HAS_ARG, QEMU_OPTION_clock },
5028 7e0af5d0 bellard
    { "startdate", HAS_ARG, QEMU_OPTION_startdate },
5029 26a5f13b bellard
    { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
5030 2e70f6ef pbrook
    { "icount", HAS_ARG, QEMU_OPTION_icount },
5031 5bb7910a aliguori
    { "incoming", HAS_ARG, QEMU_OPTION_incoming },
5032 cd6f1169 bellard
    { NULL },
5033 fc01f7e7 bellard
};
5034 fc01f7e7 bellard
5035 5905b2e5 bellard
/* password input */
5036 5905b2e5 bellard
5037 2bac6019 balrog
int qemu_key_check(BlockDriverState *bs, const char *name)
5038 2bac6019 balrog
{
5039 2bac6019 balrog
    char password[256];
5040 2bac6019 balrog
    int i;
5041 2bac6019 balrog
5042 2bac6019 balrog
    if (!bdrv_is_encrypted(bs))
5043 2bac6019 balrog
        return 0;
5044 2bac6019 balrog
5045 2bac6019 balrog
    term_printf("%s is encrypted.\n", name);
5046 2bac6019 balrog
    for(i = 0; i < 3; i++) {
5047 2bac6019 balrog
        monitor_readline("Password: ", 1, password, sizeof(password));
5048 2bac6019 balrog
        if (bdrv_set_key(bs, password) == 0)
5049 2bac6019 balrog
            return 0;
5050 2bac6019 balrog
        term_printf("invalid password\n");
5051 2bac6019 balrog
    }
5052 2bac6019 balrog
    return -EPERM;
5053 2bac6019 balrog
}
5054 2bac6019 balrog
5055 83ab7950 aliguori
static BlockDriverState *get_bdrv(int index)
5056 83ab7950 aliguori
{
5057 83ab7950 aliguori
    if (index > nb_drives)
5058 83ab7950 aliguori
        return NULL;
5059 83ab7950 aliguori
    return drives_table[index].bdrv;
5060 83ab7950 aliguori
}
5061 83ab7950 aliguori
5062 5905b2e5 bellard
static void read_passwords(void)
5063 5905b2e5 bellard
{
5064 5905b2e5 bellard
    BlockDriverState *bs;
5065 2bac6019 balrog
    int i;
5066 5905b2e5 bellard
5067 83ab7950 aliguori
    for(i = 0; i < 6; i++) {
5068 83ab7950 aliguori
        bs = get_bdrv(i);
5069 83ab7950 aliguori
        if (bs)
5070 83ab7950 aliguori
            qemu_key_check(bs, bdrv_get_device_name(bs));
5071 5905b2e5 bellard
    }
5072 5905b2e5 bellard
}
5073 5905b2e5 bellard
5074 1d14ffa9 bellard
#ifdef HAS_AUDIO
5075 6a36d84e bellard
struct soundhw soundhw[] = {
5076 b00052e4 balrog
#ifdef HAS_AUDIO_CHOICE
5077 4ce7ff6e aurel32
#if defined(TARGET_I386) || defined(TARGET_MIPS)
5078 fd06c375 bellard
    {
5079 fd06c375 bellard
        "pcspk",
5080 fd06c375 bellard
        "PC speaker",
5081 fd06c375 bellard
        0,
5082 fd06c375 bellard
        1,
5083 fd06c375 bellard
        { .init_isa = pcspk_audio_init }
5084 fd06c375 bellard
    },
5085 fd06c375 bellard
#endif
5086 6a36d84e bellard
    {
5087 6a36d84e bellard
        "sb16",
5088 6a36d84e bellard
        "Creative Sound Blaster 16",
5089 6a36d84e bellard
        0,
5090 6a36d84e bellard
        1,
5091 6a36d84e bellard
        { .init_isa = SB16_init }
5092 6a36d84e bellard
    },
5093 6a36d84e bellard
5094 cc53d26d malc
#ifdef CONFIG_CS4231A
5095 cc53d26d malc
    {
5096 cc53d26d malc
        "cs4231a",
5097 cc53d26d malc
        "CS4231A",
5098 cc53d26d malc
        0,
5099 cc53d26d malc
        1,
5100 cc53d26d malc
        { .init_isa = cs4231a_init }
5101 cc53d26d malc
    },
5102 cc53d26d malc
#endif
5103 cc53d26d malc
5104 1d14ffa9 bellard
#ifdef CONFIG_ADLIB
5105 6a36d84e bellard
    {
5106 6a36d84e bellard
        "adlib",
5107 1d14ffa9 bellard
#ifdef HAS_YMF262
5108 6a36d84e bellard
        "Yamaha YMF262 (OPL3)",
5109 1d14ffa9 bellard
#else
5110 6a36d84e bellard
        "Yamaha YM3812 (OPL2)",
5111 1d14ffa9 bellard
#endif
5112 6a36d84e bellard
        0,
5113 6a36d84e bellard
        1,
5114 6a36d84e bellard
        { .init_isa = Adlib_init }
5115 6a36d84e bellard
    },
5116 1d14ffa9 bellard
#endif
5117 6a36d84e bellard
5118 1d14ffa9 bellard
#ifdef CONFIG_GUS
5119 6a36d84e bellard
    {
5120 6a36d84e bellard
        "gus",
5121 6a36d84e bellard
        "Gravis Ultrasound GF1",
5122 6a36d84e bellard
        0,
5123 6a36d84e bellard
        1,
5124 6a36d84e bellard
        { .init_isa = GUS_init }
5125 6a36d84e bellard
    },
5126 1d14ffa9 bellard
#endif
5127 6a36d84e bellard
5128 e5c9a13e balrog
#ifdef CONFIG_AC97
5129 e5c9a13e balrog
    {
5130 e5c9a13e balrog
        "ac97",
5131 e5c9a13e balrog
        "Intel 82801AA AC97 Audio",
5132 e5c9a13e balrog
        0,
5133 e5c9a13e balrog
        0,
5134 e5c9a13e balrog
        { .init_pci = ac97_init }
5135 e5c9a13e balrog
    },
5136 e5c9a13e balrog
#endif
5137 e5c9a13e balrog
5138 6a36d84e bellard
    {
5139 6a36d84e bellard
        "es1370",
5140 6a36d84e bellard
        "ENSONIQ AudioPCI ES1370",
5141 6a36d84e bellard
        0,
5142 6a36d84e bellard
        0,
5143 6a36d84e bellard
        { .init_pci = es1370_init }
5144 6a36d84e bellard
    },
5145 b00052e4 balrog
#endif
5146 6a36d84e bellard
5147 6a36d84e bellard
    { NULL, NULL, 0, 0, { NULL } }
5148 6a36d84e bellard
};
5149 6a36d84e bellard
5150 6a36d84e bellard
static void select_soundhw (const char *optarg)
5151 6a36d84e bellard
{
5152 6a36d84e bellard
    struct soundhw *c;
5153 6a36d84e bellard
5154 6a36d84e bellard
    if (*optarg == '?') {
5155 6a36d84e bellard
    show_valid_cards:
5156 6a36d84e bellard
5157 6a36d84e bellard
        printf ("Valid sound card names (comma separated):\n");
5158 6a36d84e bellard
        for (c = soundhw; c->name; ++c) {
5159 6a36d84e bellard
            printf ("%-11s %s\n", c->name, c->descr);
5160 6a36d84e bellard
        }
5161 6a36d84e bellard
        printf ("\n-soundhw all will enable all of the above\n");
5162 1d14ffa9 bellard
        exit (*optarg != '?');
5163 1d14ffa9 bellard
    }
5164 1d14ffa9 bellard
    else {
5165 6a36d84e bellard
        size_t l;
5166 1d14ffa9 bellard
        const char *p;
5167 1d14ffa9 bellard
        char *e;
5168 1d14ffa9 bellard
        int bad_card = 0;
5169 1d14ffa9 bellard
5170 6a36d84e bellard
        if (!strcmp (optarg, "all")) {
5171 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
5172 6a36d84e bellard
                c->enabled = 1;
5173 6a36d84e bellard
            }
5174 6a36d84e bellard
            return;
5175 6a36d84e bellard
        }
5176 1d14ffa9 bellard
5177 6a36d84e bellard
        p = optarg;
5178 1d14ffa9 bellard
        while (*p) {
5179 1d14ffa9 bellard
            e = strchr (p, ',');
5180 1d14ffa9 bellard
            l = !e ? strlen (p) : (size_t) (e - p);
5181 6a36d84e bellard
5182 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
5183 6a36d84e bellard
                if (!strncmp (c->name, p, l)) {
5184 6a36d84e bellard
                    c->enabled = 1;
5185 1d14ffa9 bellard
                    break;
5186 1d14ffa9 bellard
                }
5187 1d14ffa9 bellard
            }
5188 6a36d84e bellard
5189 6a36d84e bellard
            if (!c->name) {
5190 1d14ffa9 bellard
                if (l > 80) {
5191 1d14ffa9 bellard
                    fprintf (stderr,
5192 1d14ffa9 bellard
                             "Unknown sound card name (too big to show)\n");
5193 1d14ffa9 bellard
                }
5194 1d14ffa9 bellard
                else {
5195 1d14ffa9 bellard
                    fprintf (stderr, "Unknown sound card name `%.*s'\n",
5196 1d14ffa9 bellard
                             (int) l, p);
5197 1d14ffa9 bellard
                }
5198 1d14ffa9 bellard
                bad_card = 1;
5199 1d14ffa9 bellard
            }
5200 1d14ffa9 bellard
            p += l + (e != NULL);
5201 1d14ffa9 bellard
        }
5202 1d14ffa9 bellard
5203 1d14ffa9 bellard
        if (bad_card)
5204 1d14ffa9 bellard
            goto show_valid_cards;
5205 1d14ffa9 bellard
    }
5206 1d14ffa9 bellard
}
5207 1d14ffa9 bellard
#endif
5208 1d14ffa9 bellard
5209 3893c124 malc
static void select_vgahw (const char *p)
5210 3893c124 malc
{
5211 3893c124 malc
    const char *opts;
5212 3893c124 malc
5213 3893c124 malc
    if (strstart(p, "std", &opts)) {
5214 3893c124 malc
        cirrus_vga_enabled = 0;
5215 3893c124 malc
        vmsvga_enabled = 0;
5216 3893c124 malc
    } else if (strstart(p, "cirrus", &opts)) {
5217 3893c124 malc
        cirrus_vga_enabled = 1;
5218 3893c124 malc
        vmsvga_enabled = 0;
5219 3893c124 malc
    } else if (strstart(p, "vmware", &opts)) {
5220 3893c124 malc
        cirrus_vga_enabled = 0;
5221 3893c124 malc
        vmsvga_enabled = 1;
5222 3893c124 malc
    } else {
5223 3893c124 malc
    invalid_vga:
5224 3893c124 malc
        fprintf(stderr, "Unknown vga type: %s\n", p);
5225 3893c124 malc
        exit(1);
5226 3893c124 malc
    }
5227 cb5a7aa8 malc
    while (*opts) {
5228 cb5a7aa8 malc
        const char *nextopt;
5229 cb5a7aa8 malc
5230 cb5a7aa8 malc
        if (strstart(opts, ",retrace=", &nextopt)) {
5231 cb5a7aa8 malc
            opts = nextopt;
5232 cb5a7aa8 malc
            if (strstart(opts, "dumb", &nextopt))
5233 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_DUMB;
5234 cb5a7aa8 malc
            else if (strstart(opts, "precise", &nextopt))
5235 cb5a7aa8 malc
                vga_retrace_method = VGA_RETRACE_PRECISE;
5236 cb5a7aa8 malc
            else goto invalid_vga;
5237 cb5a7aa8 malc
        } else goto invalid_vga;
5238 cb5a7aa8 malc
        opts = nextopt;
5239 cb5a7aa8 malc
    }
5240 3893c124 malc
}
5241 3893c124 malc
5242 3587d7e6 bellard
#ifdef _WIN32
5243 3587d7e6 bellard
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
5244 3587d7e6 bellard
{
5245 3587d7e6 bellard
    exit(STATUS_CONTROL_C_EXIT);
5246 3587d7e6 bellard
    return TRUE;
5247 3587d7e6 bellard
}
5248 3587d7e6 bellard
#endif
5249 3587d7e6 bellard
5250 8fcb1b90 blueswir1
static int qemu_uuid_parse(const char *str, uint8_t *uuid)
5251 8fcb1b90 blueswir1
{
5252 8fcb1b90 blueswir1
    int ret;
5253 8fcb1b90 blueswir1
5254 8fcb1b90 blueswir1
    if(strlen(str) != 36)
5255 8fcb1b90 blueswir1
        return -1;
5256 8fcb1b90 blueswir1
5257 8fcb1b90 blueswir1
    ret = sscanf(str, UUID_FMT, &uuid[0], &uuid[1], &uuid[2], &uuid[3],
5258 8fcb1b90 blueswir1
            &uuid[4], &uuid[5], &uuid[6], &uuid[7], &uuid[8], &uuid[9],
5259 8fcb1b90 blueswir1
            &uuid[10], &uuid[11], &uuid[12], &uuid[13], &uuid[14], &uuid[15]);
5260 8fcb1b90 blueswir1
5261 8fcb1b90 blueswir1
    if(ret != 16)
5262 8fcb1b90 blueswir1
        return -1;
5263 8fcb1b90 blueswir1
5264 8fcb1b90 blueswir1
    return 0;
5265 8fcb1b90 blueswir1
}
5266 8fcb1b90 blueswir1
5267 7c9d8e07 bellard
#define MAX_NET_CLIENTS 32
5268 c20709aa bellard
5269 5b08fc10 aliguori
#ifndef _WIN32
5270 5b08fc10 aliguori
5271 5b08fc10 aliguori
static void termsig_handler(int signal)
5272 5b08fc10 aliguori
{
5273 5b08fc10 aliguori
    qemu_system_shutdown_request();
5274 5b08fc10 aliguori
}
5275 5b08fc10 aliguori
5276 6f9e3801 blueswir1
static void termsig_setup(void)
5277 5b08fc10 aliguori
{
5278 5b08fc10 aliguori
    struct sigaction act;
5279 5b08fc10 aliguori
5280 5b08fc10 aliguori
    memset(&act, 0, sizeof(act));
5281 5b08fc10 aliguori
    act.sa_handler = termsig_handler;
5282 5b08fc10 aliguori
    sigaction(SIGINT,  &act, NULL);
5283 5b08fc10 aliguori
    sigaction(SIGHUP,  &act, NULL);
5284 5b08fc10 aliguori
    sigaction(SIGTERM, &act, NULL);
5285 5b08fc10 aliguori
}
5286 5b08fc10 aliguori
5287 5b08fc10 aliguori
#endif
5288 5b08fc10 aliguori
5289 0824d6fc bellard
int main(int argc, char **argv)
5290 0824d6fc bellard
{
5291 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5292 cfc3475a pbrook
    int use_gdbstub;
5293 cfc3475a pbrook
    const char *gdbstub_port;
5294 67b915a5 bellard
#endif
5295 28c5af54 j_mayer
    uint32_t boot_devices_bitmap = 0;
5296 e4bcb14c ths
    int i;
5297 28c5af54 j_mayer
    int snapshot, linux_boot, net_boot;
5298 7f7f9873 bellard
    const char *initrd_filename;
5299 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
5300 28c5af54 j_mayer
    const char *boot_devices = "";
5301 313aa567 bellard
    DisplayState *ds = &display_state;
5302 46d4767d bellard
    int cyls, heads, secs, translation;
5303 fd5f393a pbrook
    const char *net_clients[MAX_NET_CLIENTS];
5304 7c9d8e07 bellard
    int nb_net_clients;
5305 e4bcb14c ths
    int hda_index;
5306 cd6f1169 bellard
    int optind;
5307 cd6f1169 bellard
    const char *r, *optarg;
5308 82c643ff bellard
    CharDriverState *monitor_hd;
5309 fd5f393a pbrook
    const char *monitor_device;
5310 fd5f393a pbrook
    const char *serial_devices[MAX_SERIAL_PORTS];
5311 8d11df9e bellard
    int serial_device_index;
5312 fd5f393a pbrook
    const char *parallel_devices[MAX_PARALLEL_PORTS];
5313 6508fe59 bellard
    int parallel_device_index;
5314 d63d307f bellard
    const char *loadvm = NULL;
5315 cc1daa40 bellard
    QEMUMachine *machine;
5316 94fc95cd j_mayer
    const char *cpu_model;
5317 fd5f393a pbrook
    const char *usb_devices[MAX_USB_CMDLINE];
5318 a594cfbf bellard
    int usb_devices_index;
5319 71e3ceb8 ths
    int fds[2];
5320 26a5f13b bellard
    int tb_size;
5321 93815bc2 ths
    const char *pid_file = NULL;
5322 41bd639b blueswir1
    int autostart;
5323 5bb7910a aliguori
    const char *incoming = NULL;
5324 0bd48850 bellard
5325 0bd48850 bellard
    LIST_INIT (&vm_change_state_head);
5326 be995c27 bellard
#ifndef _WIN32
5327 be995c27 bellard
    {
5328 be995c27 bellard
        struct sigaction act;
5329 be995c27 bellard
        sigfillset(&act.sa_mask);
5330 be995c27 bellard
        act.sa_flags = 0;
5331 be995c27 bellard
        act.sa_handler = SIG_IGN;
5332 be995c27 bellard
        sigaction(SIGPIPE, &act, NULL);
5333 be995c27 bellard
    }
5334 3587d7e6 bellard
#else
5335 3587d7e6 bellard
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
5336 a8e5ac33 bellard
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
5337 a8e5ac33 bellard
       QEMU to run on a single CPU */
5338 a8e5ac33 bellard
    {
5339 a8e5ac33 bellard
        HANDLE h;
5340 a8e5ac33 bellard
        DWORD mask, smask;
5341 a8e5ac33 bellard
        int i;
5342 a8e5ac33 bellard
        h = GetCurrentProcess();
5343 a8e5ac33 bellard
        if (GetProcessAffinityMask(h, &mask, &smask)) {
5344 a8e5ac33 bellard
            for(i = 0; i < 32; i++) {
5345 a8e5ac33 bellard
                if (mask & (1 << i))
5346 a8e5ac33 bellard
                    break;
5347 a8e5ac33 bellard
            }
5348 a8e5ac33 bellard
            if (i != 32) {
5349 a8e5ac33 bellard
                mask = 1 << i;
5350 a8e5ac33 bellard
                SetProcessAffinityMask(h, mask);
5351 a8e5ac33 bellard
            }
5352 a8e5ac33 bellard
        }
5353 a8e5ac33 bellard
    }
5354 67b915a5 bellard
#endif
5355 be995c27 bellard
5356 cc1daa40 bellard
    register_machines();
5357 cc1daa40 bellard
    machine = first_machine;
5358 94fc95cd j_mayer
    cpu_model = NULL;
5359 fc01f7e7 bellard
    initrd_filename = NULL;
5360 4fc5d071 aurel32
    ram_size = 0;
5361 313aa567 bellard
    vga_ram_size = VGA_RAM_SIZE;
5362 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5363 b4608c04 bellard
    use_gdbstub = 0;
5364 c636bb66 bellard
    gdbstub_port = DEFAULT_GDBSTUB_PORT;
5365 67b915a5 bellard
#endif
5366 33e3963e bellard
    snapshot = 0;
5367 a20dd508 bellard
    nographic = 0;
5368 4d3b6f6e balrog
    curses = 0;
5369 a20dd508 bellard
    kernel_filename = NULL;
5370 a20dd508 bellard
    kernel_cmdline = "";
5371 c4b1fcc0 bellard
    cyls = heads = secs = 0;
5372 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
5373 c60e08d9 pbrook
    monitor_device = "vc";
5374 c4b1fcc0 bellard
5375 c75a823c aurel32
    serial_devices[0] = "vc:80Cx24C";
5376 8d11df9e bellard
    for(i = 1; i < MAX_SERIAL_PORTS; i++)
5377 fd5f393a pbrook
        serial_devices[i] = NULL;
5378 8d11df9e bellard
    serial_device_index = 0;
5379 3b46e624 ths
5380 c75a823c aurel32
    parallel_devices[0] = "vc:640x480";
5381 6508fe59 bellard
    for(i = 1; i < MAX_PARALLEL_PORTS; i++)
5382 fd5f393a pbrook
        parallel_devices[i] = NULL;
5383 6508fe59 bellard
    parallel_device_index = 0;
5384 3b46e624 ths
5385 a594cfbf bellard
    usb_devices_index = 0;
5386 3b46e624 ths
5387 7c9d8e07 bellard
    nb_net_clients = 0;
5388 e4bcb14c ths
    nb_drives = 0;
5389 e4bcb14c ths
    nb_drives_opt = 0;
5390 e4bcb14c ths
    hda_index = -1;
5391 7c9d8e07 bellard
5392 7c9d8e07 bellard
    nb_nics = 0;
5393 3b46e624 ths
5394 26a5f13b bellard
    tb_size = 0;
5395 41bd639b blueswir1
    autostart= 1;
5396 41bd639b blueswir1
5397 cd6f1169 bellard
    optind = 1;
5398 0824d6fc bellard
    for(;;) {
5399 cd6f1169 bellard
        if (optind >= argc)
5400 0824d6fc bellard
            break;
5401 cd6f1169 bellard
        r = argv[optind];
5402 cd6f1169 bellard
        if (r[0] != '-') {
5403 609497ab balrog
            hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
5404 cd6f1169 bellard
        } else {
5405 cd6f1169 bellard
            const QEMUOption *popt;
5406 cd6f1169 bellard
5407 cd6f1169 bellard
            optind++;
5408 dff5efc8 pbrook
            /* Treat --foo the same as -foo.  */
5409 dff5efc8 pbrook
            if (r[1] == '-')
5410 dff5efc8 pbrook
                r++;
5411 cd6f1169 bellard
            popt = qemu_options;
5412 cd6f1169 bellard
            for(;;) {
5413 cd6f1169 bellard
                if (!popt->name) {
5414 5fafdf24 ths
                    fprintf(stderr, "%s: invalid option -- '%s'\n",
5415 cd6f1169 bellard
                            argv[0], r);
5416 cd6f1169 bellard
                    exit(1);
5417 cd6f1169 bellard
                }
5418 cd6f1169 bellard
                if (!strcmp(popt->name, r + 1))
5419 cd6f1169 bellard
                    break;
5420 cd6f1169 bellard
                popt++;
5421 cd6f1169 bellard
            }
5422 cd6f1169 bellard
            if (popt->flags & HAS_ARG) {
5423 cd6f1169 bellard
                if (optind >= argc) {
5424 cd6f1169 bellard
                    fprintf(stderr, "%s: option '%s' requires an argument\n",
5425 cd6f1169 bellard
                            argv[0], r);
5426 cd6f1169 bellard
                    exit(1);
5427 cd6f1169 bellard
                }
5428 cd6f1169 bellard
                optarg = argv[optind++];
5429 cd6f1169 bellard
            } else {
5430 cd6f1169 bellard
                optarg = NULL;
5431 cd6f1169 bellard
            }
5432 cd6f1169 bellard
5433 cd6f1169 bellard
            switch(popt->index) {
5434 cc1daa40 bellard
            case QEMU_OPTION_M:
5435 cc1daa40 bellard
                machine = find_machine(optarg);
5436 cc1daa40 bellard
                if (!machine) {
5437 cc1daa40 bellard
                    QEMUMachine *m;
5438 cc1daa40 bellard
                    printf("Supported machines are:\n");
5439 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
5440 cc1daa40 bellard
                        printf("%-10s %s%s\n",
5441 5fafdf24 ths
                               m->name, m->desc,
5442 cc1daa40 bellard
                               m == first_machine ? " (default)" : "");
5443 cc1daa40 bellard
                    }
5444 15f82208 ths
                    exit(*optarg != '?');
5445 cc1daa40 bellard
                }
5446 cc1daa40 bellard
                break;
5447 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
5448 94fc95cd j_mayer
                /* hw initialization will check this */
5449 15f82208 ths
                if (*optarg == '?') {
5450 c732abe2 j_mayer
/* XXX: implement xxx_cpu_list for targets that still miss it */
5451 c732abe2 j_mayer
#if defined(cpu_list)
5452 c732abe2 j_mayer
                    cpu_list(stdout, &fprintf);
5453 94fc95cd j_mayer
#endif
5454 15f82208 ths
                    exit(0);
5455 94fc95cd j_mayer
                } else {
5456 94fc95cd j_mayer
                    cpu_model = optarg;
5457 94fc95cd j_mayer
                }
5458 94fc95cd j_mayer
                break;
5459 cd6f1169 bellard
            case QEMU_OPTION_initrd:
5460 fc01f7e7 bellard
                initrd_filename = optarg;
5461 fc01f7e7 bellard
                break;
5462 cd6f1169 bellard
            case QEMU_OPTION_hda:
5463 e4bcb14c ths
                if (cyls == 0)
5464 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS, 0);
5465 e4bcb14c ths
                else
5466 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS
5467 e4bcb14c ths
                             ",cyls=%d,heads=%d,secs=%d%s",
5468 609497ab balrog
                             0, cyls, heads, secs,
5469 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_LBA ?
5470 e4bcb14c ths
                                 ",trans=lba" :
5471 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_NONE ?
5472 e4bcb14c ths
                                 ",trans=none" : "");
5473 e4bcb14c ths
                 break;
5474 cd6f1169 bellard
            case QEMU_OPTION_hdb:
5475 cc1daa40 bellard
            case QEMU_OPTION_hdc:
5476 cc1daa40 bellard
            case QEMU_OPTION_hdd:
5477 609497ab balrog
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
5478 fc01f7e7 bellard
                break;
5479 e4bcb14c ths
            case QEMU_OPTION_drive:
5480 609497ab balrog
                drive_add(NULL, "%s", optarg);
5481 e4bcb14c ths
                break;
5482 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
5483 609497ab balrog
                drive_add(optarg, MTD_ALIAS);
5484 3e3d5815 balrog
                break;
5485 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
5486 609497ab balrog
                drive_add(optarg, SD_ALIAS);
5487 a1bb27b1 pbrook
                break;
5488 86f55663 j_mayer
            case QEMU_OPTION_pflash:
5489 609497ab balrog
                drive_add(optarg, PFLASH_ALIAS);
5490 86f55663 j_mayer
                break;
5491 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
5492 33e3963e bellard
                snapshot = 1;
5493 33e3963e bellard
                break;
5494 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
5495 330d0414 bellard
                {
5496 330d0414 bellard
                    const char *p;
5497 330d0414 bellard
                    p = optarg;
5498 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
5499 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
5500 46d4767d bellard
                        goto chs_fail;
5501 330d0414 bellard
                    if (*p != ',')
5502 330d0414 bellard
                        goto chs_fail;
5503 330d0414 bellard
                    p++;
5504 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
5505 46d4767d bellard
                    if (heads < 1 || heads > 16)
5506 46d4767d bellard
                        goto chs_fail;
5507 330d0414 bellard
                    if (*p != ',')
5508 330d0414 bellard
                        goto chs_fail;
5509 330d0414 bellard
                    p++;
5510 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
5511 46d4767d bellard
                    if (secs < 1 || secs > 63)
5512 46d4767d bellard
                        goto chs_fail;
5513 46d4767d bellard
                    if (*p == ',') {
5514 46d4767d bellard
                        p++;
5515 46d4767d bellard
                        if (!strcmp(p, "none"))
5516 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
5517 46d4767d bellard
                        else if (!strcmp(p, "lba"))
5518 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
5519 46d4767d bellard
                        else if (!strcmp(p, "auto"))
5520 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
5521 46d4767d bellard
                        else
5522 46d4767d bellard
                            goto chs_fail;
5523 46d4767d bellard
                    } else if (*p != '\0') {
5524 c4b1fcc0 bellard
                    chs_fail:
5525 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
5526 46d4767d bellard
                        exit(1);
5527 c4b1fcc0 bellard
                    }
5528 e4bcb14c ths
                    if (hda_index != -1)
5529 609497ab balrog
                        snprintf(drives_opt[hda_index].opt,
5530 609497ab balrog
                                 sizeof(drives_opt[hda_index].opt),
5531 609497ab balrog
                                 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
5532 609497ab balrog
                                 0, cyls, heads, secs,
5533 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
5534 e4bcb14c ths
                                         ",trans=lba" :
5535 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
5536 e4bcb14c ths
                                     ",trans=none" : "");
5537 330d0414 bellard
                }
5538 330d0414 bellard
                break;
5539 cd6f1169 bellard
            case QEMU_OPTION_nographic:
5540 a20dd508 bellard
                nographic = 1;
5541 a20dd508 bellard
                break;
5542 4d3b6f6e balrog
#ifdef CONFIG_CURSES
5543 4d3b6f6e balrog
            case QEMU_OPTION_curses:
5544 4d3b6f6e balrog
                curses = 1;
5545 4d3b6f6e balrog
                break;
5546 4d3b6f6e balrog
#endif
5547 a171fe39 balrog
            case QEMU_OPTION_portrait:
5548 a171fe39 balrog
                graphic_rotate = 1;
5549 a171fe39 balrog
                break;
5550 cd6f1169 bellard
            case QEMU_OPTION_kernel:
5551 a20dd508 bellard
                kernel_filename = optarg;
5552 a20dd508 bellard
                break;
5553 cd6f1169 bellard
            case QEMU_OPTION_append:
5554 a20dd508 bellard
                kernel_cmdline = optarg;
5555 313aa567 bellard
                break;
5556 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
5557 609497ab balrog
                drive_add(optarg, CDROM_ALIAS);
5558 36b486bb bellard
                break;
5559 cd6f1169 bellard
            case QEMU_OPTION_boot:
5560 28c5af54 j_mayer
                boot_devices = optarg;
5561 28c5af54 j_mayer
                /* We just do some generic consistency checks */
5562 28c5af54 j_mayer
                {
5563 28c5af54 j_mayer
                    /* Could easily be extended to 64 devices if needed */
5564 60fe76f3 ths
                    const char *p;
5565 28c5af54 j_mayer
                    
5566 28c5af54 j_mayer
                    boot_devices_bitmap = 0;
5567 28c5af54 j_mayer
                    for (p = boot_devices; *p != '\0'; p++) {
5568 28c5af54 j_mayer
                        /* Allowed boot devices are:
5569 28c5af54 j_mayer
                         * a b     : floppy disk drives
5570 28c5af54 j_mayer
                         * c ... f : IDE disk drives
5571 28c5af54 j_mayer
                         * g ... m : machine implementation dependant drives
5572 28c5af54 j_mayer
                         * n ... p : network devices
5573 28c5af54 j_mayer
                         * It's up to each machine implementation to check
5574 28c5af54 j_mayer
                         * if the given boot devices match the actual hardware
5575 28c5af54 j_mayer
                         * implementation and firmware features.
5576 28c5af54 j_mayer
                         */
5577 28c5af54 j_mayer
                        if (*p < 'a' || *p > 'q') {
5578 28c5af54 j_mayer
                            fprintf(stderr, "Invalid boot device '%c'\n", *p);
5579 28c5af54 j_mayer
                            exit(1);
5580 28c5af54 j_mayer
                        }
5581 28c5af54 j_mayer
                        if (boot_devices_bitmap & (1 << (*p - 'a'))) {
5582 28c5af54 j_mayer
                            fprintf(stderr,
5583 28c5af54 j_mayer
                                    "Boot device '%c' was given twice\n",*p);
5584 28c5af54 j_mayer
                            exit(1);
5585 28c5af54 j_mayer
                        }
5586 28c5af54 j_mayer
                        boot_devices_bitmap |= 1 << (*p - 'a');
5587 28c5af54 j_mayer
                    }
5588 36b486bb bellard
                }
5589 36b486bb bellard
                break;
5590 cd6f1169 bellard
            case QEMU_OPTION_fda:
5591 cd6f1169 bellard
            case QEMU_OPTION_fdb:
5592 609497ab balrog
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
5593 c45886db bellard
                break;
5594 52ca8d6a bellard
#ifdef TARGET_I386
5595 52ca8d6a bellard
            case QEMU_OPTION_no_fd_bootchk:
5596 52ca8d6a bellard
                fd_bootchk = 0;
5597 52ca8d6a bellard
                break;
5598 52ca8d6a bellard
#endif
5599 7c9d8e07 bellard
            case QEMU_OPTION_net:
5600 7c9d8e07 bellard
                if (nb_net_clients >= MAX_NET_CLIENTS) {
5601 7c9d8e07 bellard
                    fprintf(stderr, "qemu: too many network clients\n");
5602 c4b1fcc0 bellard
                    exit(1);
5603 c4b1fcc0 bellard
                }
5604 fd5f393a pbrook
                net_clients[nb_net_clients] = optarg;
5605 7c9d8e07 bellard
                nb_net_clients++;
5606 702c651c bellard
                break;
5607 c7f74643 bellard
#ifdef CONFIG_SLIRP
5608 c7f74643 bellard
            case QEMU_OPTION_tftp:
5609 c7f74643 bellard
                tftp_prefix = optarg;
5610 9bf05444 bellard
                break;
5611 47d5d01a ths
            case QEMU_OPTION_bootp:
5612 47d5d01a ths
                bootp_filename = optarg;
5613 47d5d01a ths
                break;
5614 c94c8d64 bellard
#ifndef _WIN32
5615 9d728e8c bellard
            case QEMU_OPTION_smb:
5616 9d728e8c bellard
                net_slirp_smb(optarg);
5617 9d728e8c bellard
                break;
5618 c94c8d64 bellard
#endif
5619 9bf05444 bellard
            case QEMU_OPTION_redir:
5620 3b46e624 ths
                net_slirp_redir(optarg);
5621 9bf05444 bellard
                break;
5622 c7f74643 bellard
#endif
5623 1d14ffa9 bellard
#ifdef HAS_AUDIO
5624 1d14ffa9 bellard
            case QEMU_OPTION_audio_help:
5625 1d14ffa9 bellard
                AUD_help ();
5626 1d14ffa9 bellard
                exit (0);
5627 1d14ffa9 bellard
                break;
5628 1d14ffa9 bellard
            case QEMU_OPTION_soundhw:
5629 1d14ffa9 bellard
                select_soundhw (optarg);
5630 1d14ffa9 bellard
                break;
5631 1d14ffa9 bellard
#endif
5632 cd6f1169 bellard
            case QEMU_OPTION_h:
5633 15f82208 ths
                help(0);
5634 cd6f1169 bellard
                break;
5635 00f82b8a aurel32
            case QEMU_OPTION_m: {
5636 00f82b8a aurel32
                uint64_t value;
5637 00f82b8a aurel32
                char *ptr;
5638 00f82b8a aurel32
5639 00f82b8a aurel32
                value = strtoul(optarg, &ptr, 10);
5640 00f82b8a aurel32
                switch (*ptr) {
5641 00f82b8a aurel32
                case 0: case 'M': case 'm':
5642 00f82b8a aurel32
                    value <<= 20;
5643 00f82b8a aurel32
                    break;
5644 00f82b8a aurel32
                case 'G': case 'g':
5645 00f82b8a aurel32
                    value <<= 30;
5646 00f82b8a aurel32
                    break;
5647 00f82b8a aurel32
                default:
5648 00f82b8a aurel32
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
5649 cd6f1169 bellard
                    exit(1);
5650 cd6f1169 bellard
                }
5651 00f82b8a aurel32
5652 00f82b8a aurel32
                /* On 32-bit hosts, QEMU is limited by virtual address space */
5653 00f82b8a aurel32
                if (value > (2047 << 20)
5654 00f82b8a aurel32
#ifndef USE_KQEMU
5655 00f82b8a aurel32
                    && HOST_LONG_BITS == 32
5656 00f82b8a aurel32
#endif
5657 00f82b8a aurel32
                    ) {
5658 00f82b8a aurel32
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
5659 00f82b8a aurel32
                    exit(1);
5660 00f82b8a aurel32
                }
5661 00f82b8a aurel32
                if (value != (uint64_t)(ram_addr_t)value) {
5662 00f82b8a aurel32
                    fprintf(stderr, "qemu: ram size too large\n");
5663 00f82b8a aurel32
                    exit(1);
5664 00f82b8a aurel32
                }
5665 00f82b8a aurel32
                ram_size = value;
5666 cd6f1169 bellard
                break;
5667 00f82b8a aurel32
            }
5668 cd6f1169 bellard
            case QEMU_OPTION_d:
5669 cd6f1169 bellard
                {
5670 cd6f1169 bellard
                    int mask;
5671 c7cd6a37 blueswir1
                    const CPULogItem *item;
5672 3b46e624 ths
5673 cd6f1169 bellard
                    mask = cpu_str_to_log_mask(optarg);
5674 cd6f1169 bellard
                    if (!mask) {
5675 cd6f1169 bellard
                        printf("Log items (comma separated):\n");
5676 f193c797 bellard
                    for(item = cpu_log_items; item->mask != 0; item++) {
5677 f193c797 bellard
                        printf("%-10s %s\n", item->name, item->help);
5678 f193c797 bellard
                    }
5679 f193c797 bellard
                    exit(1);
5680 cd6f1169 bellard
                    }
5681 cd6f1169 bellard
                    cpu_set_log(mask);
5682 f193c797 bellard
                }
5683 cd6f1169 bellard
                break;
5684 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
5685 cd6f1169 bellard
            case QEMU_OPTION_s:
5686 cd6f1169 bellard
                use_gdbstub = 1;
5687 cd6f1169 bellard
                break;
5688 cd6f1169 bellard
            case QEMU_OPTION_p:
5689 cfc3475a pbrook
                gdbstub_port = optarg;
5690 cd6f1169 bellard
                break;
5691 67b915a5 bellard
#endif
5692 cd6f1169 bellard
            case QEMU_OPTION_L:
5693 cd6f1169 bellard
                bios_dir = optarg;
5694 cd6f1169 bellard
                break;
5695 1192dad8 j_mayer
            case QEMU_OPTION_bios:
5696 1192dad8 j_mayer
                bios_name = optarg;
5697 1192dad8 j_mayer
                break;
5698 cd6f1169 bellard
            case QEMU_OPTION_S:
5699 3c07f8e8 pbrook
                autostart = 0;
5700 cd6f1169 bellard
                break;
5701 3d11d0eb bellard
            case QEMU_OPTION_k:
5702 3d11d0eb bellard
                keyboard_layout = optarg;
5703 3d11d0eb bellard
                break;
5704 ee22c2f7 bellard
            case QEMU_OPTION_localtime:
5705 ee22c2f7 bellard
                rtc_utc = 0;
5706 ee22c2f7 bellard
                break;
5707 3893c124 malc
            case QEMU_OPTION_vga:
5708 3893c124 malc
                select_vgahw (optarg);
5709 1bfe856e bellard
                break;
5710 e9b137c2 bellard
            case QEMU_OPTION_g:
5711 e9b137c2 bellard
                {
5712 e9b137c2 bellard
                    const char *p;
5713 e9b137c2 bellard
                    int w, h, depth;
5714 e9b137c2 bellard
                    p = optarg;
5715 e9b137c2 bellard
                    w = strtol(p, (char **)&p, 10);
5716 e9b137c2 bellard
                    if (w <= 0) {
5717 e9b137c2 bellard
                    graphic_error:
5718 e9b137c2 bellard
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
5719 e9b137c2 bellard
                        exit(1);
5720 e9b137c2 bellard
                    }
5721 e9b137c2 bellard
                    if (*p != 'x')
5722 e9b137c2 bellard
                        goto graphic_error;
5723 e9b137c2 bellard
                    p++;
5724 e9b137c2 bellard
                    h = strtol(p, (char **)&p, 10);
5725 e9b137c2 bellard
                    if (h <= 0)
5726 e9b137c2 bellard
                        goto graphic_error;
5727 e9b137c2 bellard
                    if (*p == 'x') {
5728 e9b137c2 bellard
                        p++;
5729 e9b137c2 bellard
                        depth = strtol(p, (char **)&p, 10);
5730 5fafdf24 ths
                        if (depth != 8 && depth != 15 && depth != 16 &&
5731 e9b137c2 bellard
                            depth != 24 && depth != 32)
5732 e9b137c2 bellard
                            goto graphic_error;
5733 e9b137c2 bellard
                    } else if (*p == '\0') {
5734 e9b137c2 bellard
                        depth = graphic_depth;
5735 e9b137c2 bellard
                    } else {
5736 e9b137c2 bellard
                        goto graphic_error;
5737 e9b137c2 bellard
                    }
5738 3b46e624 ths
5739 e9b137c2 bellard
                    graphic_width = w;
5740 e9b137c2 bellard
                    graphic_height = h;
5741 e9b137c2 bellard
                    graphic_depth = depth;
5742 e9b137c2 bellard
                }
5743 e9b137c2 bellard
                break;
5744 20d8a3ed ths
            case QEMU_OPTION_echr:
5745 20d8a3ed ths
                {
5746 20d8a3ed ths
                    char *r;
5747 20d8a3ed ths
                    term_escape_char = strtol(optarg, &r, 0);
5748 20d8a3ed ths
                    if (r == optarg)
5749 20d8a3ed ths
                        printf("Bad argument to echr\n");
5750 20d8a3ed ths
                    break;
5751 20d8a3ed ths
                }
5752 82c643ff bellard
            case QEMU_OPTION_monitor:
5753 fd5f393a pbrook
                monitor_device = optarg;
5754 82c643ff bellard
                break;
5755 82c643ff bellard
            case QEMU_OPTION_serial:
5756 8d11df9e bellard
                if (serial_device_index >= MAX_SERIAL_PORTS) {
5757 8d11df9e bellard
                    fprintf(stderr, "qemu: too many serial ports\n");
5758 8d11df9e bellard
                    exit(1);
5759 8d11df9e bellard
                }
5760 fd5f393a pbrook
                serial_devices[serial_device_index] = optarg;
5761 8d11df9e bellard
                serial_device_index++;
5762 82c643ff bellard
                break;
5763 6508fe59 bellard
            case QEMU_OPTION_parallel:
5764 6508fe59 bellard
                if (parallel_device_index >= MAX_PARALLEL_PORTS) {
5765 6508fe59 bellard
                    fprintf(stderr, "qemu: too many parallel ports\n");
5766 6508fe59 bellard
                    exit(1);
5767 6508fe59 bellard
                }
5768 fd5f393a pbrook
                parallel_devices[parallel_device_index] = optarg;
5769 6508fe59 bellard
                parallel_device_index++;
5770 6508fe59 bellard
                break;
5771 d63d307f bellard
            case QEMU_OPTION_loadvm:
5772 d63d307f bellard
                loadvm = optarg;
5773 d63d307f bellard
                break;
5774 d63d307f bellard
            case QEMU_OPTION_full_screen:
5775 d63d307f bellard
                full_screen = 1;
5776 d63d307f bellard
                break;
5777 667accab ths
#ifdef CONFIG_SDL
5778 43523e93 ths
            case QEMU_OPTION_no_frame:
5779 43523e93 ths
                no_frame = 1;
5780 43523e93 ths
                break;
5781 3780e197 ths
            case QEMU_OPTION_alt_grab:
5782 3780e197 ths
                alt_grab = 1;
5783 3780e197 ths
                break;
5784 667accab ths
            case QEMU_OPTION_no_quit:
5785 667accab ths
                no_quit = 1;
5786 667accab ths
                break;
5787 667accab ths
#endif
5788 f7cce898 bellard
            case QEMU_OPTION_pidfile:
5789 93815bc2 ths
                pid_file = optarg;
5790 f7cce898 bellard
                break;
5791 a09db21f bellard
#ifdef TARGET_I386
5792 a09db21f bellard
            case QEMU_OPTION_win2k_hack:
5793 a09db21f bellard
                win2k_install_hack = 1;
5794 a09db21f bellard
                break;
5795 a09db21f bellard
#endif
5796 d993e026 bellard
#ifdef USE_KQEMU
5797 d993e026 bellard
            case QEMU_OPTION_no_kqemu:
5798 d993e026 bellard
                kqemu_allowed = 0;
5799 d993e026 bellard
                break;
5800 89bfc105 bellard
            case QEMU_OPTION_kernel_kqemu:
5801 89bfc105 bellard
                kqemu_allowed = 2;
5802 89bfc105 bellard
                break;
5803 d993e026 bellard
#endif
5804 bb36d470 bellard
            case QEMU_OPTION_usb:
5805 bb36d470 bellard
                usb_enabled = 1;
5806 bb36d470 bellard
                break;
5807 a594cfbf bellard
            case QEMU_OPTION_usbdevice:
5808 a594cfbf bellard
                usb_enabled = 1;
5809 0d92ed30 pbrook
                if (usb_devices_index >= MAX_USB_CMDLINE) {
5810 a594cfbf bellard
                    fprintf(stderr, "Too many USB devices\n");
5811 a594cfbf bellard
                    exit(1);
5812 a594cfbf bellard
                }
5813 fd5f393a pbrook
                usb_devices[usb_devices_index] = optarg;
5814 a594cfbf bellard
                usb_devices_index++;
5815 a594cfbf bellard
                break;
5816 6a00d601 bellard
            case QEMU_OPTION_smp:
5817 6a00d601 bellard
                smp_cpus = atoi(optarg);
5818 b2097003 aliguori
                if (smp_cpus < 1) {
5819 6a00d601 bellard
                    fprintf(stderr, "Invalid number of CPUs\n");
5820 6a00d601 bellard
                    exit(1);
5821 6a00d601 bellard
                }
5822 6a00d601 bellard
                break;
5823 24236869 bellard
            case QEMU_OPTION_vnc:
5824 73fc9742 ths
                vnc_display = optarg;
5825 24236869 bellard
                break;
5826 6515b203 bellard
            case QEMU_OPTION_no_acpi:
5827 6515b203 bellard
                acpi_enabled = 0;
5828 6515b203 bellard
                break;
5829 d1beab82 bellard
            case QEMU_OPTION_no_reboot:
5830 d1beab82 bellard
                no_reboot = 1;
5831 d1beab82 bellard
                break;
5832 b2f76161 aurel32
            case QEMU_OPTION_no_shutdown:
5833 b2f76161 aurel32
                no_shutdown = 1;
5834 b2f76161 aurel32
                break;
5835 9467cd46 balrog
            case QEMU_OPTION_show_cursor:
5836 9467cd46 balrog
                cursor_hide = 0;
5837 9467cd46 balrog
                break;
5838 8fcb1b90 blueswir1
            case QEMU_OPTION_uuid:
5839 8fcb1b90 blueswir1
                if(qemu_uuid_parse(optarg, qemu_uuid) < 0) {
5840 8fcb1b90 blueswir1
                    fprintf(stderr, "Fail to parse UUID string."
5841 8fcb1b90 blueswir1
                            " Wrong format.\n");
5842 8fcb1b90 blueswir1
                    exit(1);
5843 8fcb1b90 blueswir1
                }
5844 8fcb1b90 blueswir1
                break;
5845 71e3ceb8 ths
            case QEMU_OPTION_daemonize:
5846 71e3ceb8 ths
                daemonize = 1;
5847 71e3ceb8 ths
                break;
5848 9ae02555 ths
            case QEMU_OPTION_option_rom:
5849 9ae02555 ths
                if (nb_option_roms >= MAX_OPTION_ROMS) {
5850 9ae02555 ths
                    fprintf(stderr, "Too many option ROMs\n");
5851 9ae02555 ths
                    exit(1);
5852 9ae02555 ths
                }
5853 9ae02555 ths
                option_rom[nb_option_roms] = optarg;
5854 9ae02555 ths
                nb_option_roms++;
5855 9ae02555 ths
                break;
5856 8e71621f pbrook
            case QEMU_OPTION_semihosting:
5857 8e71621f pbrook
                semihosting_enabled = 1;
5858 8e71621f pbrook
                break;
5859 c35734b2 ths
            case QEMU_OPTION_name:
5860 c35734b2 ths
                qemu_name = optarg;
5861 c35734b2 ths
                break;
5862 66508601 blueswir1
#ifdef TARGET_SPARC
5863 66508601 blueswir1
            case QEMU_OPTION_prom_env:
5864 66508601 blueswir1
                if (nb_prom_envs >= MAX_PROM_ENVS) {
5865 66508601 blueswir1
                    fprintf(stderr, "Too many prom variables\n");
5866 66508601 blueswir1
                    exit(1);
5867 66508601 blueswir1
                }
5868 66508601 blueswir1
                prom_envs[nb_prom_envs] = optarg;
5869 66508601 blueswir1
                nb_prom_envs++;
5870 66508601 blueswir1
                break;
5871 66508601 blueswir1
#endif
5872 2b8f2d41 balrog
#ifdef TARGET_ARM
5873 2b8f2d41 balrog
            case QEMU_OPTION_old_param:
5874 2b8f2d41 balrog
                old_param = 1;
5875 05ebd537 ths
                break;
5876 2b8f2d41 balrog
#endif
5877 f3dcfada ths
            case QEMU_OPTION_clock:
5878 f3dcfada ths
                configure_alarms(optarg);
5879 f3dcfada ths
                break;
5880 7e0af5d0 bellard
            case QEMU_OPTION_startdate:
5881 7e0af5d0 bellard
                {
5882 7e0af5d0 bellard
                    struct tm tm;
5883 f6503059 balrog
                    time_t rtc_start_date;
5884 7e0af5d0 bellard
                    if (!strcmp(optarg, "now")) {
5885 f6503059 balrog
                        rtc_date_offset = -1;
5886 7e0af5d0 bellard
                    } else {
5887 7e0af5d0 bellard
                        if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
5888 7e0af5d0 bellard
                               &tm.tm_year,
5889 7e0af5d0 bellard
                               &tm.tm_mon,
5890 7e0af5d0 bellard
                               &tm.tm_mday,
5891 7e0af5d0 bellard
                               &tm.tm_hour,
5892 7e0af5d0 bellard
                               &tm.tm_min,
5893 7e0af5d0 bellard
                               &tm.tm_sec) == 6) {
5894 7e0af5d0 bellard
                            /* OK */
5895 7e0af5d0 bellard
                        } else if (sscanf(optarg, "%d-%d-%d",
5896 7e0af5d0 bellard
                                          &tm.tm_year,
5897 7e0af5d0 bellard
                                          &tm.tm_mon,
5898 7e0af5d0 bellard
                                          &tm.tm_mday) == 3) {
5899 7e0af5d0 bellard
                            tm.tm_hour = 0;
5900 7e0af5d0 bellard
                            tm.tm_min = 0;
5901 7e0af5d0 bellard
                            tm.tm_sec = 0;
5902 7e0af5d0 bellard
                        } else {
5903 7e0af5d0 bellard
                            goto date_fail;
5904 7e0af5d0 bellard
                        }
5905 7e0af5d0 bellard
                        tm.tm_year -= 1900;
5906 7e0af5d0 bellard
                        tm.tm_mon--;
5907 3c6b2088 bellard
                        rtc_start_date = mktimegm(&tm);
5908 7e0af5d0 bellard
                        if (rtc_start_date == -1) {
5909 7e0af5d0 bellard
                        date_fail:
5910 7e0af5d0 bellard
                            fprintf(stderr, "Invalid date format. Valid format are:\n"
5911 7e0af5d0 bellard
                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5912 7e0af5d0 bellard
                            exit(1);
5913 7e0af5d0 bellard
                        }
5914 f6503059 balrog
                        rtc_date_offset = time(NULL) - rtc_start_date;
5915 7e0af5d0 bellard
                    }
5916 7e0af5d0 bellard
                }
5917 7e0af5d0 bellard
                break;
5918 26a5f13b bellard
            case QEMU_OPTION_tb_size:
5919 26a5f13b bellard
                tb_size = strtol(optarg, NULL, 0);
5920 26a5f13b bellard
                if (tb_size < 0)
5921 26a5f13b bellard
                    tb_size = 0;
5922 26a5f13b bellard
                break;
5923 2e70f6ef pbrook
            case QEMU_OPTION_icount:
5924 2e70f6ef pbrook
                use_icount = 1;
5925 2e70f6ef pbrook
                if (strcmp(optarg, "auto") == 0) {
5926 2e70f6ef pbrook
                    icount_time_shift = -1;
5927 2e70f6ef pbrook
                } else {
5928 2e70f6ef pbrook
                    icount_time_shift = strtol(optarg, NULL, 0);
5929 2e70f6ef pbrook
                }
5930 2e70f6ef pbrook
                break;
5931 5bb7910a aliguori
            case QEMU_OPTION_incoming:
5932 5bb7910a aliguori
                incoming = optarg;
5933 5bb7910a aliguori
                break;
5934 cd6f1169 bellard
            }
5935 0824d6fc bellard
        }
5936 0824d6fc bellard
    }
5937 330d0414 bellard
5938 3d878caa balrog
    machine->max_cpus = machine->max_cpus ?: 1; /* Default to UP */
5939 b2097003 aliguori
    if (smp_cpus > machine->max_cpus) {
5940 b2097003 aliguori
        fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
5941 b2097003 aliguori
                "supported by machine `%s' (%d)\n", smp_cpus,  machine->name,
5942 b2097003 aliguori
                machine->max_cpus);
5943 b2097003 aliguori
        exit(1);
5944 b2097003 aliguori
    }
5945 b2097003 aliguori
5946 bc0129d9 aliguori
    if (nographic) {
5947 bc0129d9 aliguori
       if (serial_device_index == 0)
5948 bc0129d9 aliguori
           serial_devices[0] = "stdio";
5949 bc0129d9 aliguori
       if (parallel_device_index == 0)
5950 bc0129d9 aliguori
           parallel_devices[0] = "null";
5951 bc0129d9 aliguori
       if (strncmp(monitor_device, "vc", 2) == 0)
5952 bc0129d9 aliguori
           monitor_device = "stdio";
5953 bc0129d9 aliguori
    }
5954 bc0129d9 aliguori
5955 71e3ceb8 ths
#ifndef _WIN32
5956 71e3ceb8 ths
    if (daemonize) {
5957 71e3ceb8 ths
        pid_t pid;
5958 71e3ceb8 ths
5959 71e3ceb8 ths
        if (pipe(fds) == -1)
5960 71e3ceb8 ths
            exit(1);
5961 71e3ceb8 ths
5962 71e3ceb8 ths
        pid = fork();
5963 71e3ceb8 ths
        if (pid > 0) {
5964 71e3ceb8 ths
            uint8_t status;
5965 71e3ceb8 ths
            ssize_t len;
5966 71e3ceb8 ths
5967 71e3ceb8 ths
            close(fds[1]);
5968 71e3ceb8 ths
5969 71e3ceb8 ths
        again:
5970 93815bc2 ths
            len = read(fds[0], &status, 1);
5971 93815bc2 ths
            if (len == -1 && (errno == EINTR))
5972 93815bc2 ths
                goto again;
5973 93815bc2 ths
5974 93815bc2 ths
            if (len != 1)
5975 93815bc2 ths
                exit(1);
5976 93815bc2 ths
            else if (status == 1) {
5977 93815bc2 ths
                fprintf(stderr, "Could not acquire pidfile\n");
5978 93815bc2 ths
                exit(1);
5979 93815bc2 ths
            } else
5980 93815bc2 ths
                exit(0);
5981 71e3ceb8 ths
        } else if (pid < 0)
5982 93815bc2 ths
            exit(1);
5983 71e3ceb8 ths
5984 71e3ceb8 ths
        setsid();
5985 71e3ceb8 ths
5986 71e3ceb8 ths
        pid = fork();
5987 71e3ceb8 ths
        if (pid > 0)
5988 71e3ceb8 ths
            exit(0);
5989 71e3ceb8 ths
        else if (pid < 0)
5990 71e3ceb8 ths
            exit(1);
5991 71e3ceb8 ths
5992 71e3ceb8 ths
        umask(027);
5993 71e3ceb8 ths
5994 71e3ceb8 ths
        signal(SIGTSTP, SIG_IGN);
5995 71e3ceb8 ths
        signal(SIGTTOU, SIG_IGN);
5996 71e3ceb8 ths
        signal(SIGTTIN, SIG_IGN);
5997 71e3ceb8 ths
    }
5998 71e3ceb8 ths
#endif
5999 71e3ceb8 ths
6000 aa26bb2d ths
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
6001 93815bc2 ths
        if (daemonize) {
6002 93815bc2 ths
            uint8_t status = 1;
6003 93815bc2 ths
            write(fds[1], &status, 1);
6004 93815bc2 ths
        } else
6005 93815bc2 ths
            fprintf(stderr, "Could not acquire pid file\n");
6006 93815bc2 ths
        exit(1);
6007 93815bc2 ths
    }
6008 93815bc2 ths
6009 ff3fbb30 bellard
#ifdef USE_KQEMU
6010 ff3fbb30 bellard
    if (smp_cpus > 1)
6011 ff3fbb30 bellard
        kqemu_allowed = 0;
6012 ff3fbb30 bellard
#endif
6013 a20dd508 bellard
    linux_boot = (kernel_filename != NULL);
6014 7317b8ca balrog
    net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
6015 6c41b272 balrog
6016 28c5af54 j_mayer
    if (!linux_boot && net_boot == 0 &&
6017 f88e4b91 blueswir1
        !machine->nodisk_ok && nb_drives_opt == 0)
6018 15f82208 ths
        help(1);
6019 0824d6fc bellard
6020 f8d39c01 ths
    if (!linux_boot && *kernel_cmdline != '\0') {
6021 f8d39c01 ths
        fprintf(stderr, "-append only allowed with -kernel option\n");
6022 f8d39c01 ths
        exit(1);
6023 f8d39c01 ths
    }
6024 f8d39c01 ths
6025 f8d39c01 ths
    if (!linux_boot && initrd_filename != NULL) {
6026 f8d39c01 ths
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
6027 f8d39c01 ths
        exit(1);
6028 f8d39c01 ths
    }
6029 f8d39c01 ths
6030 96d30e48 ths
    /* boot to floppy or the default cd if no hard disk defined yet */
6031 28c5af54 j_mayer
    if (!boot_devices[0]) {
6032 e4bcb14c ths
        boot_devices = "cad";
6033 96d30e48 ths
    }
6034 b118d61e bellard
    setvbuf(stdout, NULL, _IOLBF, 0);
6035 3b46e624 ths
6036 634fce96 pbrook
    init_timers();
6037 634fce96 pbrook
    init_timer_alarm();
6038 2e70f6ef pbrook
    if (use_icount && icount_time_shift < 0) {
6039 2e70f6ef pbrook
        use_icount = 2;
6040 2e70f6ef pbrook
        /* 125MIPS seems a reasonable initial guess at the guest speed.
6041 2e70f6ef pbrook
           It will be corrected fairly quickly anyway.  */
6042 2e70f6ef pbrook
        icount_time_shift = 3;
6043 2e70f6ef pbrook
        init_icount_adjust();
6044 2e70f6ef pbrook
    }
6045 634fce96 pbrook
6046 fd1dff4b bellard
#ifdef _WIN32
6047 fd1dff4b bellard
    socket_init();
6048 fd1dff4b bellard
#endif
6049 fd1dff4b bellard
6050 7c9d8e07 bellard
    /* init network clients */
6051 7c9d8e07 bellard
    if (nb_net_clients == 0) {
6052 7c9d8e07 bellard
        /* if no clients, we use a default config */
6053 f441b28b aliguori
        net_clients[nb_net_clients++] = "nic";
6054 f441b28b aliguori
#ifdef CONFIG_SLIRP
6055 f441b28b aliguori
        net_clients[nb_net_clients++] = "user";
6056 f441b28b aliguori
#endif
6057 c20709aa bellard
    }
6058 c20709aa bellard
6059 7c9d8e07 bellard
    for(i = 0;i < nb_net_clients; i++) {
6060 9ad97e65 balrog
        if (net_client_parse(net_clients[i]) < 0)
6061 7c9d8e07 bellard
            exit(1);
6062 702c651c bellard
    }
6063 63a01ef8 aliguori
    net_client_check();
6064 f1510b2c bellard
6065 eec85c2a ths
#ifdef TARGET_I386
6066 ed494d87 balrog
    /* XXX: this should be moved in the PC machine instantiation code */
6067 28c5af54 j_mayer
    if (net_boot != 0) {
6068 28c5af54 j_mayer
        int netroms = 0;
6069 28c5af54 j_mayer
        for (i = 0; i < nb_nics && i < 4; i++) {
6070 eec85c2a ths
            const char *model = nd_table[i].model;
6071 eec85c2a ths
            char buf[1024];
6072 28c5af54 j_mayer
            if (net_boot & (1 << i)) {
6073 28c5af54 j_mayer
                if (model == NULL)
6074 28c5af54 j_mayer
                    model = "ne2k_pci";
6075 28c5af54 j_mayer
                snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
6076 28c5af54 j_mayer
                if (get_image_size(buf) > 0) {
6077 28c5af54 j_mayer
                    if (nb_option_roms >= MAX_OPTION_ROMS) {
6078 28c5af54 j_mayer
                        fprintf(stderr, "Too many option ROMs\n");
6079 28c5af54 j_mayer
                        exit(1);
6080 28c5af54 j_mayer
                    }
6081 28c5af54 j_mayer
                    option_rom[nb_option_roms] = strdup(buf);
6082 28c5af54 j_mayer
                    nb_option_roms++;
6083 28c5af54 j_mayer
                    netroms++;
6084 28c5af54 j_mayer
                }
6085 28c5af54 j_mayer
            }
6086 eec85c2a ths
        }
6087 28c5af54 j_mayer
        if (netroms == 0) {
6088 eec85c2a ths
            fprintf(stderr, "No valid PXE rom found for network device\n");
6089 eec85c2a ths
            exit(1);
6090 eec85c2a ths
        }
6091 eec85c2a ths
    }
6092 eec85c2a ths
#endif
6093 eec85c2a ths
6094 0824d6fc bellard
    /* init the memory */
6095 7fb4fdcf balrog
    phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
6096 7fb4fdcf balrog
6097 7fb4fdcf balrog
    if (machine->ram_require & RAMSIZE_FIXED) {
6098 7fb4fdcf balrog
        if (ram_size > 0) {
6099 7fb4fdcf balrog
            if (ram_size < phys_ram_size) {
6100 cd940061 aurel32
                fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
6101 cd940061 aurel32
                                machine->name, (unsigned long long) phys_ram_size);
6102 7fb4fdcf balrog
                exit(-1);
6103 7fb4fdcf balrog
            }
6104 7fb4fdcf balrog
6105 7fb4fdcf balrog
            phys_ram_size = ram_size;
6106 7fb4fdcf balrog
        } else
6107 7fb4fdcf balrog
            ram_size = phys_ram_size;
6108 7fb4fdcf balrog
    } else {
6109 4fc5d071 aurel32
        if (ram_size == 0)
6110 7fb4fdcf balrog
            ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
6111 7fb4fdcf balrog
6112 7fb4fdcf balrog
        phys_ram_size += ram_size;
6113 7fb4fdcf balrog
    }
6114 9ae02555 ths
6115 d993e026 bellard
    phys_ram_base = qemu_vmalloc(phys_ram_size);
6116 7f7f9873 bellard
    if (!phys_ram_base) {
6117 7f7f9873 bellard
        fprintf(stderr, "Could not allocate physical memory\n");
6118 0824d6fc bellard
        exit(1);
6119 0824d6fc bellard
    }
6120 0824d6fc bellard
6121 26a5f13b bellard
    /* init the dynamic translator */
6122 26a5f13b bellard
    cpu_exec_init_all(tb_size * 1024 * 1024);
6123 26a5f13b bellard
6124 5905b2e5 bellard
    bdrv_init();
6125 c4b1fcc0 bellard
6126 e4bcb14c ths
    /* we always create the cdrom drive, even if no disk is there */
6127 c4b1fcc0 bellard
6128 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
6129 609497ab balrog
        drive_add(NULL, CDROM_ALIAS);
6130 c4b1fcc0 bellard
6131 9d413d1d balrog
    /* we always create at least one floppy */
6132 33e3963e bellard
6133 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
6134 609497ab balrog
        drive_add(NULL, FD_ALIAS, 0);
6135 86f55663 j_mayer
6136 9d413d1d balrog
    /* we always create one sd slot, even if no card is in it */
6137 9d413d1d balrog
6138 9d413d1d balrog
    if (nb_drives_opt < MAX_DRIVES)
6139 609497ab balrog
        drive_add(NULL, SD_ALIAS);
6140 9d413d1d balrog
6141 e4bcb14c ths
    /* open the virtual block devices */
6142 e4bcb14c ths
6143 e4bcb14c ths
    for(i = 0; i < nb_drives_opt; i++)
6144 609497ab balrog
        if (drive_init(&drives_opt[i], snapshot, machine) == -1)
6145 e4bcb14c ths
            exit(1);
6146 3e3d5815 balrog
6147 c88676f8 bellard
    register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
6148 475e4277 aliguori
    register_savevm_live("ram", 0, 3, ram_save_live, NULL, ram_load, NULL);
6149 8a7ddc38 bellard
6150 313aa567 bellard
    /* terminal init */
6151 740733bb ths
    memset(&display_state, 0, sizeof(display_state));
6152 a20dd508 bellard
    if (nographic) {
6153 4d3b6f6e balrog
        if (curses) {
6154 4d3b6f6e balrog
            fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
6155 4d3b6f6e balrog
            exit(1);
6156 4d3b6f6e balrog
        }
6157 2ff89790 ths
        /* nearly nothing to do */
6158 2ff89790 ths
        dumb_display_init(ds);
6159 73fc9742 ths
    } else if (vnc_display != NULL) {
6160 71cab5ca ths
        vnc_display_init(ds);
6161 71cab5ca ths
        if (vnc_display_open(ds, vnc_display) < 0)
6162 71cab5ca ths
            exit(1);
6163 4d3b6f6e balrog
    } else
6164 4d3b6f6e balrog
#if defined(CONFIG_CURSES)
6165 4d3b6f6e balrog
    if (curses) {
6166 4d3b6f6e balrog
        curses_display_init(ds, full_screen);
6167 4d3b6f6e balrog
    } else
6168 4d3b6f6e balrog
#endif
6169 4d3b6f6e balrog
    {
6170 5b0753e0 bellard
#if defined(CONFIG_SDL)
6171 43523e93 ths
        sdl_display_init(ds, full_screen, no_frame);
6172 5b0753e0 bellard
#elif defined(CONFIG_COCOA)
6173 5b0753e0 bellard
        cocoa_display_init(ds, full_screen);
6174 67276f53 pbrook
#else
6175 67276f53 pbrook
        dumb_display_init(ds);
6176 313aa567 bellard
#endif
6177 313aa567 bellard
    }
6178 0824d6fc bellard
6179 5b08fc10 aliguori
#ifndef _WIN32
6180 5b08fc10 aliguori
    /* must be after terminal init, SDL library changes signal handlers */
6181 5b08fc10 aliguori
    termsig_setup();
6182 5b08fc10 aliguori
#endif
6183 5b08fc10 aliguori
6184 20d8a3ed ths
    /* Maintain compatibility with multiple stdio monitors */
6185 20d8a3ed ths
    if (!strcmp(monitor_device,"stdio")) {
6186 20d8a3ed ths
        for (i = 0; i < MAX_SERIAL_PORTS; i++) {
6187 fd5f393a pbrook
            const char *devname = serial_devices[i];
6188 fd5f393a pbrook
            if (devname && !strcmp(devname,"mon:stdio")) {
6189 fd5f393a pbrook
                monitor_device = NULL;
6190 20d8a3ed ths
                break;
6191 fd5f393a pbrook
            } else if (devname && !strcmp(devname,"stdio")) {
6192 fd5f393a pbrook
                monitor_device = NULL;
6193 fd5f393a pbrook
                serial_devices[i] = "mon:stdio";
6194 20d8a3ed ths
                break;
6195 20d8a3ed ths
            }
6196 20d8a3ed ths
        }
6197 20d8a3ed ths
    }
6198 fd5f393a pbrook
    if (monitor_device) {
6199 5ccfae10 aliguori
        monitor_hd = qemu_chr_open("monitor", monitor_device);
6200 20d8a3ed ths
        if (!monitor_hd) {
6201 20d8a3ed ths
            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
6202 20d8a3ed ths
            exit(1);
6203 20d8a3ed ths
        }
6204 20d8a3ed ths
        monitor_init(monitor_hd, !nographic);
6205 82c643ff bellard
    }
6206 82c643ff bellard
6207 8d11df9e bellard
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
6208 c03b0f0f bellard
        const char *devname = serial_devices[i];
6209 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
6210 5ccfae10 aliguori
            char label[32];
6211 5ccfae10 aliguori
            snprintf(label, sizeof(label), "serial%d", i);
6212 5ccfae10 aliguori
            serial_hds[i] = qemu_chr_open(label, devname);
6213 8d11df9e bellard
            if (!serial_hds[i]) {
6214 5fafdf24 ths
                fprintf(stderr, "qemu: could not open serial device '%s'\n",
6215 c03b0f0f bellard
                        devname);
6216 8d11df9e bellard
                exit(1);
6217 8d11df9e bellard
            }
6218 af3a9031 ths
            if (strstart(devname, "vc", 0))
6219 7ba1260a bellard
                qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
6220 8d11df9e bellard
        }
6221 82c643ff bellard
    }
6222 82c643ff bellard
6223 6508fe59 bellard
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
6224 c03b0f0f bellard
        const char *devname = parallel_devices[i];
6225 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
6226 5ccfae10 aliguori
            char label[32];
6227 5ccfae10 aliguori
            snprintf(label, sizeof(label), "parallel%d", i);
6228 5ccfae10 aliguori
            parallel_hds[i] = qemu_chr_open(label, devname);
6229 6508fe59 bellard
            if (!parallel_hds[i]) {
6230 5fafdf24 ths
                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
6231 c03b0f0f bellard
                        devname);
6232 6508fe59 bellard
                exit(1);
6233 6508fe59 bellard
            }
6234 af3a9031 ths
            if (strstart(devname, "vc", 0))
6235 7ba1260a bellard
                qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
6236 6508fe59 bellard
        }
6237 6508fe59 bellard
    }
6238 6508fe59 bellard
6239 b881c2c6 blueswir1
    machine->init(ram_size, vga_ram_size, boot_devices, ds,
6240 94fc95cd j_mayer
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
6241 73332e5c bellard
6242 0d92ed30 pbrook
    /* init USB devices */
6243 0d92ed30 pbrook
    if (usb_enabled) {
6244 0d92ed30 pbrook
        for(i = 0; i < usb_devices_index; i++) {
6245 0d92ed30 pbrook
            if (usb_device_add(usb_devices[i]) < 0) {
6246 0d92ed30 pbrook
                fprintf(stderr, "Warning: could not add USB device %s\n",
6247 0d92ed30 pbrook
                        usb_devices[i]);
6248 0d92ed30 pbrook
            }
6249 0d92ed30 pbrook
        }
6250 0d92ed30 pbrook
    }
6251 0d92ed30 pbrook
6252 740733bb ths
    if (display_state.dpy_refresh) {
6253 740733bb ths
        display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
6254 740733bb ths
        qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
6255 740733bb ths
    }
6256 7f7f9873 bellard
6257 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
6258 b4608c04 bellard
    if (use_gdbstub) {
6259 c636bb66 bellard
        /* XXX: use standard host:port notation and modify options
6260 c636bb66 bellard
           accordingly. */
6261 cfc3475a pbrook
        if (gdbserver_start(gdbstub_port) < 0) {
6262 cfc3475a pbrook
            fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
6263 c636bb66 bellard
                    gdbstub_port);
6264 8a7ddc38 bellard
            exit(1);
6265 8a7ddc38 bellard
        }
6266 45669e00 balrog
    }
6267 67b915a5 bellard
#endif
6268 45669e00 balrog
6269 d63d307f bellard
    if (loadvm)
6270 faea38e7 bellard
        do_loadvm(loadvm);
6271 d63d307f bellard
6272 5bb7910a aliguori
    if (incoming) {
6273 5bb7910a aliguori
        autostart = 0; /* fixme how to deal with -daemonize */
6274 5bb7910a aliguori
        qemu_start_incoming_migration(incoming);
6275 5bb7910a aliguori
    }
6276 5bb7910a aliguori
6277 67b915a5 bellard
    {
6278 5905b2e5 bellard
        /* XXX: simplify init */
6279 83ab7950 aliguori
        read_passwords();
6280 3c07f8e8 pbrook
        if (autostart) {
6281 5905b2e5 bellard
            vm_start();
6282 5905b2e5 bellard
        }
6283 0824d6fc bellard
    }
6284 ffd843bc ths
6285 71e3ceb8 ths
    if (daemonize) {
6286 71e3ceb8 ths
        uint8_t status = 0;
6287 71e3ceb8 ths
        ssize_t len;
6288 71e3ceb8 ths
        int fd;
6289 71e3ceb8 ths
6290 71e3ceb8 ths
    again1:
6291 71e3ceb8 ths
        len = write(fds[1], &status, 1);
6292 71e3ceb8 ths
        if (len == -1 && (errno == EINTR))
6293 71e3ceb8 ths
            goto again1;
6294 71e3ceb8 ths
6295 71e3ceb8 ths
        if (len != 1)
6296 71e3ceb8 ths
            exit(1);
6297 71e3ceb8 ths
6298 bd54b863 aliguori
        chdir("/");
6299 aeb30be6 balrog
        TFR(fd = open("/dev/null", O_RDWR));
6300 71e3ceb8 ths
        if (fd == -1)
6301 71e3ceb8 ths
            exit(1);
6302 71e3ceb8 ths
6303 71e3ceb8 ths
        dup2(fd, 0);
6304 71e3ceb8 ths
        dup2(fd, 1);
6305 71e3ceb8 ths
        dup2(fd, 2);
6306 71e3ceb8 ths
6307 71e3ceb8 ths
        close(fd);
6308 71e3ceb8 ths
    }
6309 71e3ceb8 ths
6310 8a7ddc38 bellard
    main_loop();
6311 40c3bac3 bellard
    quit_timers();
6312 63a01ef8 aliguori
    net_cleanup();
6313 b46a8906 ths
6314 0824d6fc bellard
    return 0;
6315 0824d6fc bellard
}