Statistics
| Branch: | Revision:

root / vl.c @ 1eec614b

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