Statistics
| Branch: | Revision:

root / vl.c @ d78f3995

History | View | Annotate | Download (156.4 kB)

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