Statistics
| Branch: | Revision:

root / vl.c @ 10781c09

History | View | Annotate | Download (145.8 kB)

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