Statistics
| Branch: | Revision:

root / vl.c @ b1503cda

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