Statistics
| Branch: | Revision:

root / vl.c @ 604457d7

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