Statistics
| Branch: | Revision:

root / vl.c @ 477e3edf

History | View | Annotate | Download (233.6 kB)

1 0824d6fc bellard
/*
2 80cabfad bellard
 * QEMU System Emulator
3 5fafdf24 ths
 *
4 68d0f70e bellard
 * Copyright (c) 2003-2008 Fabrice Bellard
5 5fafdf24 ths
 *
6 1df912cf bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 1df912cf bellard
 * of this software and associated documentation files (the "Software"), to deal
8 1df912cf bellard
 * in the Software without restriction, including without limitation the rights
9 1df912cf bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 1df912cf bellard
 * copies of the Software, and to permit persons to whom the Software is
11 1df912cf bellard
 * furnished to do so, subject to the following conditions:
12 1df912cf bellard
 *
13 1df912cf bellard
 * The above copyright notice and this permission notice shall be included in
14 1df912cf bellard
 * all copies or substantial portions of the Software.
15 1df912cf bellard
 *
16 1df912cf bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 1df912cf bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 1df912cf bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 1df912cf bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 1df912cf bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 1df912cf bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 1df912cf bellard
 * THE SOFTWARE.
23 0824d6fc bellard
 */
24 87ecb68b pbrook
#include "hw/hw.h"
25 87ecb68b pbrook
#include "hw/boards.h"
26 87ecb68b pbrook
#include "hw/usb.h"
27 87ecb68b pbrook
#include "hw/pcmcia.h"
28 87ecb68b pbrook
#include "hw/pc.h"
29 87ecb68b pbrook
#include "hw/audiodev.h"
30 87ecb68b pbrook
#include "hw/isa.h"
31 2e4d9fb1 aurel32
#include "hw/baum.h"
32 87ecb68b pbrook
#include "net.h"
33 87ecb68b pbrook
#include "console.h"
34 87ecb68b pbrook
#include "sysemu.h"
35 87ecb68b pbrook
#include "gdbstub.h"
36 87ecb68b pbrook
#include "qemu-timer.h"
37 87ecb68b pbrook
#include "qemu-char.h"
38 87ecb68b pbrook
#include "block.h"
39 87ecb68b pbrook
#include "audio/audio.h"
40 67b915a5 bellard
41 0824d6fc bellard
#include <unistd.h>
42 0824d6fc bellard
#include <fcntl.h>
43 0824d6fc bellard
#include <signal.h>
44 0824d6fc bellard
#include <time.h>
45 0824d6fc bellard
#include <errno.h>
46 67b915a5 bellard
#include <sys/time.h>
47 c88676f8 bellard
#include <zlib.h>
48 67b915a5 bellard
49 67b915a5 bellard
#ifndef _WIN32
50 67b915a5 bellard
#include <sys/times.h>
51 f1510b2c bellard
#include <sys/wait.h>
52 67b915a5 bellard
#include <termios.h>
53 67b915a5 bellard
#include <sys/poll.h>
54 67b915a5 bellard
#include <sys/mman.h>
55 f1510b2c bellard
#include <sys/ioctl.h>
56 f1510b2c bellard
#include <sys/socket.h>
57 c94c8d64 bellard
#include <netinet/in.h>
58 9d728e8c bellard
#include <dirent.h>
59 7c9d8e07 bellard
#include <netdb.h>
60 cb4b976b ths
#include <sys/select.h>
61 cb4b976b ths
#include <arpa/inet.h>
62 7d3505c5 bellard
#ifdef _BSD
63 7d3505c5 bellard
#include <sys/stat.h>
64 83fb7adf bellard
#ifndef __APPLE__
65 7d3505c5 bellard
#include <libutil.h>
66 83fb7adf bellard
#endif
67 5c40d2bd ths
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68 5c40d2bd ths
#include <freebsd/stdlib.h>
69 7d3505c5 bellard
#else
70 ec530c81 bellard
#ifndef __sun__
71 f1510b2c bellard
#include <linux/if.h>
72 f1510b2c bellard
#include <linux/if_tun.h>
73 7d3505c5 bellard
#include <pty.h>
74 7d3505c5 bellard
#include <malloc.h>
75 fd872598 bellard
#include <linux/rtc.h>
76 bd494f4c ths
77 bd494f4c ths
/* For the benefit of older linux systems which don't supply it,
78 bd494f4c ths
   we use a local copy of hpet.h. */
79 bd494f4c ths
/* #include <linux/hpet.h> */
80 bd494f4c ths
#include "hpet.h"
81 bd494f4c ths
82 e57a8c0e bellard
#include <linux/ppdev.h>
83 5867c88a ths
#include <linux/parport.h>
84 d5d10bc3 ths
#else
85 d5d10bc3 ths
#include <sys/stat.h>
86 d5d10bc3 ths
#include <sys/ethernet.h>
87 d5d10bc3 ths
#include <sys/sockio.h>
88 d5d10bc3 ths
#include <netinet/arp.h>
89 d5d10bc3 ths
#include <netinet/in.h>
90 d5d10bc3 ths
#include <netinet/in_systm.h>
91 d5d10bc3 ths
#include <netinet/ip.h>
92 d5d10bc3 ths
#include <netinet/ip_icmp.h> // must come after ip.h
93 d5d10bc3 ths
#include <netinet/udp.h>
94 d5d10bc3 ths
#include <netinet/tcp.h>
95 d5d10bc3 ths
#include <net/if.h>
96 d5d10bc3 ths
#include <syslog.h>
97 d5d10bc3 ths
#include <stropts.h>
98 67b915a5 bellard
#endif
99 7d3505c5 bellard
#endif
100 cb4b976b ths
#else
101 cb4b976b ths
#include <winsock2.h>
102 cb4b976b ths
int inet_aton(const char *cp, struct in_addr *ia);
103 ec530c81 bellard
#endif
104 67b915a5 bellard
105 c20709aa bellard
#if defined(CONFIG_SLIRP)
106 c20709aa bellard
#include "libslirp.h"
107 c20709aa bellard
#endif
108 c20709aa bellard
109 8a16d273 ths
#if defined(CONFIG_VDE)
110 8a16d273 ths
#include <libvdeplug.h>
111 8a16d273 ths
#endif
112 8a16d273 ths
113 67b915a5 bellard
#ifdef _WIN32
114 7d3505c5 bellard
#include <malloc.h>
115 67b915a5 bellard
#include <sys/timeb.h>
116 4fddf62a ths
#include <mmsystem.h>
117 67b915a5 bellard
#define getopt_long_only getopt_long
118 67b915a5 bellard
#define memalign(align, size) malloc(size)
119 67b915a5 bellard
#endif
120 67b915a5 bellard
121 6ca957f0 bellard
#include "qemu_socket.h"
122 6ca957f0 bellard
123 73332e5c bellard
#ifdef CONFIG_SDL
124 96bcd4f8 bellard
#ifdef __APPLE__
125 83fb7adf bellard
#include <SDL/SDL.h>
126 96bcd4f8 bellard
#endif
127 73332e5c bellard
#endif /* CONFIG_SDL */
128 0824d6fc bellard
129 5b0753e0 bellard
#ifdef CONFIG_COCOA
130 5b0753e0 bellard
#undef main
131 5b0753e0 bellard
#define main qemu_main
132 5b0753e0 bellard
#endif /* CONFIG_COCOA */
133 5b0753e0 bellard
134 0824d6fc bellard
#include "disas.h"
135 fc01f7e7 bellard
136 8a7ddc38 bellard
#include "exec-all.h"
137 0824d6fc bellard
138 5a67135a bellard
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
139 b46a8906 ths
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
140 a14d6c8c pbrook
#ifdef __sun__
141 a14d6c8c pbrook
#define SMBD_COMMAND "/usr/sfw/sbin/smbd"
142 a14d6c8c pbrook
#else
143 a14d6c8c pbrook
#define SMBD_COMMAND "/usr/sbin/smbd"
144 a14d6c8c pbrook
#endif
145 f1510b2c bellard
146 0824d6fc bellard
//#define DEBUG_UNUSED_IOPORT
147 fd872598 bellard
//#define DEBUG_IOPORT
148 330d0414 bellard
149 77d4bc34 bellard
#ifdef TARGET_PPC
150 77d4bc34 bellard
#define DEFAULT_RAM_SIZE 144
151 77d4bc34 bellard
#else
152 1bfe856e bellard
#define DEFAULT_RAM_SIZE 128
153 77d4bc34 bellard
#endif
154 8a7ddc38 bellard
/* in ms */
155 8a7ddc38 bellard
#define GUI_REFRESH_INTERVAL 30
156 313aa567 bellard
157 0d92ed30 pbrook
/* Max number of USB devices that can be specified on the commandline.  */
158 0d92ed30 pbrook
#define MAX_USB_CMDLINE 8
159 0d92ed30 pbrook
160 7dea1da4 bellard
/* XXX: use a two level table to limit memory usage */
161 7dea1da4 bellard
#define MAX_IOPORTS 65536
162 0824d6fc bellard
163 80cabfad bellard
const char *bios_dir = CONFIG_QEMU_SHAREDIR;
164 1192dad8 j_mayer
const char *bios_name = NULL;
165 c4b1fcc0 bellard
void *ioport_opaque[MAX_IOPORTS];
166 fc01f7e7 bellard
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
167 fc01f7e7 bellard
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
168 e4bcb14c ths
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
169 faea38e7 bellard
   to store the VM snapshots */
170 e4bcb14c ths
DriveInfo drives_table[MAX_DRIVES+1];
171 e4bcb14c ths
int nb_drives;
172 faea38e7 bellard
/* point to the block driver where the snapshots are managed */
173 faea38e7 bellard
BlockDriverState *bs_snapshots;
174 313aa567 bellard
int vga_ram_size;
175 313aa567 bellard
static DisplayState display_state;
176 a20dd508 bellard
int nographic;
177 4d3b6f6e balrog
int curses;
178 3d11d0eb bellard
const char* keyboard_layout = NULL;
179 313aa567 bellard
int64_t ticks_per_sec;
180 00f82b8a aurel32
ram_addr_t ram_size;
181 80cabfad bellard
int pit_min_timer_count = 0;
182 c4b1fcc0 bellard
int nb_nics;
183 7c9d8e07 bellard
NICInfo nd_table[MAX_NICS];
184 8a7ddc38 bellard
int vm_running;
185 f6503059 balrog
static int rtc_utc = 1;
186 f6503059 balrog
static int rtc_date_offset = -1; /* -1 means no change */
187 1bfe856e bellard
int cirrus_vga_enabled = 1;
188 d34cab9f ths
int vmsvga_enabled = 0;
189 d827220b bellard
#ifdef TARGET_SPARC
190 d827220b bellard
int graphic_width = 1024;
191 d827220b bellard
int graphic_height = 768;
192 eee0b836 blueswir1
int graphic_depth = 8;
193 d827220b bellard
#else
194 1bfe856e bellard
int graphic_width = 800;
195 1bfe856e bellard
int graphic_height = 600;
196 e9b137c2 bellard
int graphic_depth = 15;
197 eee0b836 blueswir1
#endif
198 d63d307f bellard
int full_screen = 0;
199 43523e93 ths
int no_frame = 0;
200 667accab ths
int no_quit = 0;
201 8d11df9e bellard
CharDriverState *serial_hds[MAX_SERIAL_PORTS];
202 6508fe59 bellard
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
203 a09db21f bellard
#ifdef TARGET_I386
204 a09db21f bellard
int win2k_install_hack = 0;
205 a09db21f bellard
#endif
206 bb36d470 bellard
int usb_enabled = 0;
207 7c9d8e07 bellard
static VLANState *first_vlan;
208 6a00d601 bellard
int smp_cpus = 1;
209 73fc9742 ths
const char *vnc_display;
210 d3e9db93 bellard
#if defined(TARGET_SPARC)
211 ba3c64fb bellard
#define MAX_CPUS 16
212 d3e9db93 bellard
#elif defined(TARGET_I386)
213 d3e9db93 bellard
#define MAX_CPUS 255
214 ba3c64fb bellard
#else
215 d3e9db93 bellard
#define MAX_CPUS 1
216 ba3c64fb bellard
#endif
217 6515b203 bellard
int acpi_enabled = 1;
218 52ca8d6a bellard
int fd_bootchk = 1;
219 d1beab82 bellard
int no_reboot = 0;
220 b2f76161 aurel32
int no_shutdown = 0;
221 9467cd46 balrog
int cursor_hide = 1;
222 a171fe39 balrog
int graphic_rotate = 0;
223 71e3ceb8 ths
int daemonize = 0;
224 9ae02555 ths
const char *option_rom[MAX_OPTION_ROMS];
225 9ae02555 ths
int nb_option_roms;
226 8e71621f pbrook
int semihosting_enabled = 0;
227 3c07f8e8 pbrook
int autostart = 1;
228 2b8f2d41 balrog
#ifdef TARGET_ARM
229 2b8f2d41 balrog
int old_param = 0;
230 2b8f2d41 balrog
#endif
231 c35734b2 ths
const char *qemu_name;
232 3780e197 ths
int alt_grab = 0;
233 66508601 blueswir1
#ifdef TARGET_SPARC
234 66508601 blueswir1
unsigned int nb_prom_envs = 0;
235 66508601 blueswir1
const char *prom_envs[MAX_PROM_ENVS];
236 66508601 blueswir1
#endif
237 e4bcb14c ths
int nb_drives_opt;
238 609497ab balrog
struct drive_opt {
239 609497ab balrog
    const char *file;
240 609497ab balrog
    char opt[1024];
241 609497ab balrog
} drives_opt[MAX_DRIVES];
242 0824d6fc bellard
243 ee5605e5 balrog
static CPUState *cur_cpu;
244 ee5605e5 balrog
static CPUState *next_cpu;
245 76ea08f9 balrog
static int event_pending = 1;
246 bf20dc07 ths
/* Conversion factor from emulated instructions to virtual clock ticks.  */
247 2e70f6ef pbrook
static int icount_time_shift;
248 bf20dc07 ths
/* Arbitrarily pick 1MIPS as the minimum allowable speed.  */
249 2e70f6ef pbrook
#define MAX_ICOUNT_SHIFT 10
250 2e70f6ef pbrook
/* Compensate for varying guest execution speed.  */
251 2e70f6ef pbrook
static int64_t qemu_icount_bias;
252 2e70f6ef pbrook
QEMUTimer *icount_rt_timer;
253 2e70f6ef pbrook
QEMUTimer *icount_vm_timer;
254 ee5605e5 balrog
255 aeb30be6 balrog
#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR)
256 aeb30be6 balrog
257 0824d6fc bellard
/***********************************************************/
258 26aa7d72 bellard
/* x86 ISA bus support */
259 26aa7d72 bellard
260 26aa7d72 bellard
target_phys_addr_t isa_mem_base = 0;
261 3de388f6 bellard
PicState2 *isa_pic;
262 0824d6fc bellard
263 477e3edf aliguori
static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
264 477e3edf aliguori
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
265 477e3edf aliguori
266 477e3edf aliguori
static uint32_t ioport_read(int index, uint32_t address)
267 477e3edf aliguori
{
268 477e3edf aliguori
    static IOPortReadFunc *default_func[3] = {
269 477e3edf aliguori
        default_ioport_readb,
270 477e3edf aliguori
        default_ioport_readw,
271 477e3edf aliguori
        default_ioport_readl
272 477e3edf aliguori
    };
273 477e3edf aliguori
    IOPortReadFunc *func = ioport_read_table[index][address];
274 477e3edf aliguori
    if (!func)
275 477e3edf aliguori
        func = default_func[index];
276 477e3edf aliguori
    return func(ioport_opaque[address], address);
277 477e3edf aliguori
}
278 477e3edf aliguori
279 477e3edf aliguori
static void ioport_write(int index, uint32_t address, uint32_t data)
280 477e3edf aliguori
{
281 477e3edf aliguori
    static IOPortWriteFunc *default_func[3] = {
282 477e3edf aliguori
        default_ioport_writeb,
283 477e3edf aliguori
        default_ioport_writew,
284 477e3edf aliguori
        default_ioport_writel
285 477e3edf aliguori
    };
286 477e3edf aliguori
    IOPortWriteFunc *func = ioport_write_table[index][address];
287 477e3edf aliguori
    if (!func)
288 477e3edf aliguori
        func = default_func[index];
289 477e3edf aliguori
    func(ioport_opaque[address], address, data);
290 477e3edf aliguori
}
291 477e3edf aliguori
292 9596ebb7 pbrook
static uint32_t default_ioport_readb(void *opaque, uint32_t address)
293 0824d6fc bellard
{
294 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
295 1196be37 ths
    fprintf(stderr, "unused inb: port=0x%04x\n", address);
296 0824d6fc bellard
#endif
297 fc01f7e7 bellard
    return 0xff;
298 0824d6fc bellard
}
299 0824d6fc bellard
300 9596ebb7 pbrook
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
301 0824d6fc bellard
{
302 0824d6fc bellard
#ifdef DEBUG_UNUSED_IOPORT
303 1196be37 ths
    fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
304 0824d6fc bellard
#endif
305 0824d6fc bellard
}
306 0824d6fc bellard
307 0824d6fc bellard
/* default is to make two byte accesses */
308 9596ebb7 pbrook
static uint32_t default_ioport_readw(void *opaque, uint32_t address)
309 0824d6fc bellard
{
310 0824d6fc bellard
    uint32_t data;
311 477e3edf aliguori
    data = ioport_read(0, address);
312 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
313 477e3edf aliguori
    data |= ioport_read(0, address) << 8;
314 0824d6fc bellard
    return data;
315 0824d6fc bellard
}
316 0824d6fc bellard
317 9596ebb7 pbrook
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
318 0824d6fc bellard
{
319 477e3edf aliguori
    ioport_write(0, address, data & 0xff);
320 db45c29a bellard
    address = (address + 1) & (MAX_IOPORTS - 1);
321 477e3edf aliguori
    ioport_write(0, address, (data >> 8) & 0xff);
322 0824d6fc bellard
}
323 0824d6fc bellard
324 9596ebb7 pbrook
static uint32_t default_ioport_readl(void *opaque, uint32_t address)
325 0824d6fc bellard
{
326 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
327 1196be37 ths
    fprintf(stderr, "unused inl: port=0x%04x\n", address);
328 fc01f7e7 bellard
#endif
329 fc01f7e7 bellard
    return 0xffffffff;
330 0824d6fc bellard
}
331 0824d6fc bellard
332 9596ebb7 pbrook
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
333 0824d6fc bellard
{
334 fc01f7e7 bellard
#ifdef DEBUG_UNUSED_IOPORT
335 1196be37 ths
    fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
336 fc01f7e7 bellard
#endif
337 0824d6fc bellard
}
338 0824d6fc bellard
339 fc01f7e7 bellard
/* size is the word size in byte */
340 5fafdf24 ths
int register_ioport_read(int start, int length, int size,
341 c4b1fcc0 bellard
                         IOPortReadFunc *func, void *opaque)
342 f1510b2c bellard
{
343 fc01f7e7 bellard
    int i, bsize;
344 f1510b2c bellard
345 c4b1fcc0 bellard
    if (size == 1) {
346 fc01f7e7 bellard
        bsize = 0;
347 c4b1fcc0 bellard
    } else if (size == 2) {
348 fc01f7e7 bellard
        bsize = 1;
349 c4b1fcc0 bellard
    } else if (size == 4) {
350 fc01f7e7 bellard
        bsize = 2;
351 c4b1fcc0 bellard
    } else {
352 88fdf56f balrog
        hw_error("register_ioport_read: invalid size");
353 fc01f7e7 bellard
        return -1;
354 c4b1fcc0 bellard
    }
355 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
356 fc01f7e7 bellard
        ioport_read_table[bsize][i] = func;
357 c4b1fcc0 bellard
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
358 88fdf56f balrog
            hw_error("register_ioport_read: invalid opaque");
359 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
360 c4b1fcc0 bellard
    }
361 f1510b2c bellard
    return 0;
362 f1510b2c bellard
}
363 f1510b2c bellard
364 fc01f7e7 bellard
/* size is the word size in byte */
365 5fafdf24 ths
int register_ioport_write(int start, int length, int size,
366 c4b1fcc0 bellard
                          IOPortWriteFunc *func, void *opaque)
367 f1510b2c bellard
{
368 fc01f7e7 bellard
    int i, bsize;
369 f1510b2c bellard
370 c4b1fcc0 bellard
    if (size == 1) {
371 fc01f7e7 bellard
        bsize = 0;
372 c4b1fcc0 bellard
    } else if (size == 2) {
373 fc01f7e7 bellard
        bsize = 1;
374 c4b1fcc0 bellard
    } else if (size == 4) {
375 fc01f7e7 bellard
        bsize = 2;
376 c4b1fcc0 bellard
    } else {
377 88fdf56f balrog
        hw_error("register_ioport_write: invalid size");
378 fc01f7e7 bellard
        return -1;
379 c4b1fcc0 bellard
    }
380 c4b1fcc0 bellard
    for(i = start; i < start + length; i += size) {
381 fc01f7e7 bellard
        ioport_write_table[bsize][i] = func;
382 88fdf56f balrog
        if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
383 88fdf56f balrog
            hw_error("register_ioport_write: invalid opaque");
384 c4b1fcc0 bellard
        ioport_opaque[i] = opaque;
385 c4b1fcc0 bellard
    }
386 f1510b2c bellard
    return 0;
387 f1510b2c bellard
}
388 f1510b2c bellard
389 69b91039 bellard
void isa_unassign_ioport(int start, int length)
390 69b91039 bellard
{
391 69b91039 bellard
    int i;
392 69b91039 bellard
393 69b91039 bellard
    for(i = start; i < start + length; i++) {
394 69b91039 bellard
        ioport_read_table[0][i] = default_ioport_readb;
395 69b91039 bellard
        ioport_read_table[1][i] = default_ioport_readw;
396 69b91039 bellard
        ioport_read_table[2][i] = default_ioport_readl;
397 69b91039 bellard
398 69b91039 bellard
        ioport_write_table[0][i] = default_ioport_writeb;
399 69b91039 bellard
        ioport_write_table[1][i] = default_ioport_writew;
400 69b91039 bellard
        ioport_write_table[2][i] = default_ioport_writel;
401 69b91039 bellard
    }
402 69b91039 bellard
}
403 69b91039 bellard
404 20f32282 bellard
/***********************************************************/
405 20f32282 bellard
406 c45886db bellard
void cpu_outb(CPUState *env, int addr, int val)
407 0824d6fc bellard
{
408 fd872598 bellard
#ifdef DEBUG_IOPORT
409 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
410 fd872598 bellard
        fprintf(logfile, "outb: %04x %02x\n", addr, val);
411 3b46e624 ths
#endif
412 477e3edf aliguori
    ioport_write(0, addr, val);
413 89bfc105 bellard
#ifdef USE_KQEMU
414 89bfc105 bellard
    if (env)
415 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
416 89bfc105 bellard
#endif
417 0824d6fc bellard
}
418 0824d6fc bellard
419 c45886db bellard
void cpu_outw(CPUState *env, int addr, int val)
420 0824d6fc bellard
{
421 fd872598 bellard
#ifdef DEBUG_IOPORT
422 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
423 fd872598 bellard
        fprintf(logfile, "outw: %04x %04x\n", addr, val);
424 3b46e624 ths
#endif
425 477e3edf aliguori
    ioport_write(1, addr, val);
426 89bfc105 bellard
#ifdef USE_KQEMU
427 89bfc105 bellard
    if (env)
428 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
429 89bfc105 bellard
#endif
430 0824d6fc bellard
}
431 0824d6fc bellard
432 c45886db bellard
void cpu_outl(CPUState *env, int addr, int val)
433 0824d6fc bellard
{
434 fd872598 bellard
#ifdef DEBUG_IOPORT
435 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
436 fd872598 bellard
        fprintf(logfile, "outl: %04x %08x\n", addr, val);
437 fd872598 bellard
#endif
438 477e3edf aliguori
    ioport_write(2, addr, val);
439 89bfc105 bellard
#ifdef USE_KQEMU
440 89bfc105 bellard
    if (env)
441 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
442 89bfc105 bellard
#endif
443 0824d6fc bellard
}
444 0824d6fc bellard
445 c45886db bellard
int cpu_inb(CPUState *env, int addr)
446 0824d6fc bellard
{
447 fd872598 bellard
    int val;
448 477e3edf aliguori
    val = ioport_read(0, addr);
449 fd872598 bellard
#ifdef DEBUG_IOPORT
450 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
451 fd872598 bellard
        fprintf(logfile, "inb : %04x %02x\n", addr, val);
452 fd872598 bellard
#endif
453 89bfc105 bellard
#ifdef USE_KQEMU
454 89bfc105 bellard
    if (env)
455 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
456 89bfc105 bellard
#endif
457 fd872598 bellard
    return val;
458 0824d6fc bellard
}
459 0824d6fc bellard
460 c45886db bellard
int cpu_inw(CPUState *env, int addr)
461 0824d6fc bellard
{
462 fd872598 bellard
    int val;
463 477e3edf aliguori
    val = ioport_read(1, addr);
464 fd872598 bellard
#ifdef DEBUG_IOPORT
465 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
466 fd872598 bellard
        fprintf(logfile, "inw : %04x %04x\n", addr, val);
467 fd872598 bellard
#endif
468 89bfc105 bellard
#ifdef USE_KQEMU
469 89bfc105 bellard
    if (env)
470 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
471 89bfc105 bellard
#endif
472 fd872598 bellard
    return val;
473 0824d6fc bellard
}
474 0824d6fc bellard
475 c45886db bellard
int cpu_inl(CPUState *env, int addr)
476 0824d6fc bellard
{
477 fd872598 bellard
    int val;
478 477e3edf aliguori
    val = ioport_read(2, addr);
479 fd872598 bellard
#ifdef DEBUG_IOPORT
480 fd872598 bellard
    if (loglevel & CPU_LOG_IOPORT)
481 fd872598 bellard
        fprintf(logfile, "inl : %04x %08x\n", addr, val);
482 fd872598 bellard
#endif
483 89bfc105 bellard
#ifdef USE_KQEMU
484 89bfc105 bellard
    if (env)
485 89bfc105 bellard
        env->last_io_time = cpu_get_time_fast();
486 89bfc105 bellard
#endif
487 fd872598 bellard
    return val;
488 0824d6fc bellard
}
489 0824d6fc bellard
490 0824d6fc bellard
/***********************************************************/
491 0824d6fc bellard
void hw_error(const char *fmt, ...)
492 0824d6fc bellard
{
493 0824d6fc bellard
    va_list ap;
494 6a00d601 bellard
    CPUState *env;
495 0824d6fc bellard
496 0824d6fc bellard
    va_start(ap, fmt);
497 0824d6fc bellard
    fprintf(stderr, "qemu: hardware error: ");
498 0824d6fc bellard
    vfprintf(stderr, fmt, ap);
499 0824d6fc bellard
    fprintf(stderr, "\n");
500 6a00d601 bellard
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
501 6a00d601 bellard
        fprintf(stderr, "CPU #%d:\n", env->cpu_index);
502 0824d6fc bellard
#ifdef TARGET_I386
503 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU);
504 c45886db bellard
#else
505 6a00d601 bellard
        cpu_dump_state(env, stderr, fprintf, 0);
506 0824d6fc bellard
#endif
507 6a00d601 bellard
    }
508 0824d6fc bellard
    va_end(ap);
509 0824d6fc bellard
    abort();
510 0824d6fc bellard
}
511 0824d6fc bellard
512 8a7ddc38 bellard
/***********************************************************/
513 63066f4f bellard
/* keyboard/mouse */
514 63066f4f bellard
515 63066f4f bellard
static QEMUPutKBDEvent *qemu_put_kbd_event;
516 63066f4f bellard
static void *qemu_put_kbd_event_opaque;
517 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
518 455204eb ths
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
519 63066f4f bellard
520 63066f4f bellard
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
521 63066f4f bellard
{
522 63066f4f bellard
    qemu_put_kbd_event_opaque = opaque;
523 63066f4f bellard
    qemu_put_kbd_event = func;
524 63066f4f bellard
}
525 63066f4f bellard
526 455204eb ths
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func,
527 455204eb ths
                                                void *opaque, int absolute,
528 455204eb ths
                                                const char *name)
529 63066f4f bellard
{
530 455204eb ths
    QEMUPutMouseEntry *s, *cursor;
531 455204eb ths
532 455204eb ths
    s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
533 455204eb ths
    if (!s)
534 455204eb ths
        return NULL;
535 455204eb ths
536 455204eb ths
    s->qemu_put_mouse_event = func;
537 455204eb ths
    s->qemu_put_mouse_event_opaque = opaque;
538 455204eb ths
    s->qemu_put_mouse_event_absolute = absolute;
539 455204eb ths
    s->qemu_put_mouse_event_name = qemu_strdup(name);
540 455204eb ths
    s->next = NULL;
541 455204eb ths
542 455204eb ths
    if (!qemu_put_mouse_event_head) {
543 455204eb ths
        qemu_put_mouse_event_head = qemu_put_mouse_event_current = s;
544 455204eb ths
        return s;
545 455204eb ths
    }
546 455204eb ths
547 455204eb ths
    cursor = qemu_put_mouse_event_head;
548 455204eb ths
    while (cursor->next != NULL)
549 455204eb ths
        cursor = cursor->next;
550 455204eb ths
551 455204eb ths
    cursor->next = s;
552 455204eb ths
    qemu_put_mouse_event_current = s;
553 455204eb ths
554 455204eb ths
    return s;
555 455204eb ths
}
556 455204eb ths
557 455204eb ths
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
558 455204eb ths
{
559 455204eb ths
    QEMUPutMouseEntry *prev = NULL, *cursor;
560 455204eb ths
561 455204eb ths
    if (!qemu_put_mouse_event_head || entry == NULL)
562 455204eb ths
        return;
563 455204eb ths
564 455204eb ths
    cursor = qemu_put_mouse_event_head;
565 455204eb ths
    while (cursor != NULL && cursor != entry) {
566 455204eb ths
        prev = cursor;
567 455204eb ths
        cursor = cursor->next;
568 455204eb ths
    }
569 455204eb ths
570 455204eb ths
    if (cursor == NULL) // does not exist or list empty
571 455204eb ths
        return;
572 455204eb ths
    else if (prev == NULL) { // entry is head
573 455204eb ths
        qemu_put_mouse_event_head = cursor->next;
574 455204eb ths
        if (qemu_put_mouse_event_current == entry)
575 455204eb ths
            qemu_put_mouse_event_current = cursor->next;
576 455204eb ths
        qemu_free(entry->qemu_put_mouse_event_name);
577 455204eb ths
        qemu_free(entry);
578 455204eb ths
        return;
579 455204eb ths
    }
580 455204eb ths
581 455204eb ths
    prev->next = entry->next;
582 455204eb ths
583 455204eb ths
    if (qemu_put_mouse_event_current == entry)
584 455204eb ths
        qemu_put_mouse_event_current = prev;
585 455204eb ths
586 455204eb ths
    qemu_free(entry->qemu_put_mouse_event_name);
587 455204eb ths
    qemu_free(entry);
588 63066f4f bellard
}
589 63066f4f bellard
590 63066f4f bellard
void kbd_put_keycode(int keycode)
591 63066f4f bellard
{
592 63066f4f bellard
    if (qemu_put_kbd_event) {
593 63066f4f bellard
        qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
594 63066f4f bellard
    }
595 63066f4f bellard
}
596 63066f4f bellard
597 63066f4f bellard
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
598 63066f4f bellard
{
599 455204eb ths
    QEMUPutMouseEvent *mouse_event;
600 455204eb ths
    void *mouse_event_opaque;
601 a171fe39 balrog
    int width;
602 455204eb ths
603 455204eb ths
    if (!qemu_put_mouse_event_current) {
604 455204eb ths
        return;
605 455204eb ths
    }
606 455204eb ths
607 455204eb ths
    mouse_event =
608 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event;
609 455204eb ths
    mouse_event_opaque =
610 455204eb ths
        qemu_put_mouse_event_current->qemu_put_mouse_event_opaque;
611 455204eb ths
612 455204eb ths
    if (mouse_event) {
613 a171fe39 balrog
        if (graphic_rotate) {
614 a171fe39 balrog
            if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
615 a171fe39 balrog
                width = 0x7fff;
616 a171fe39 balrog
            else
617 b94ed577 aurel32
                width = graphic_width - 1;
618 a171fe39 balrog
            mouse_event(mouse_event_opaque,
619 a171fe39 balrog
                                 width - dy, dx, dz, buttons_state);
620 a171fe39 balrog
        } else
621 a171fe39 balrog
            mouse_event(mouse_event_opaque,
622 a171fe39 balrog
                                 dx, dy, dz, buttons_state);
623 63066f4f bellard
    }
624 63066f4f bellard
}
625 63066f4f bellard
626 09b26c5e bellard
int kbd_mouse_is_absolute(void)
627 09b26c5e bellard
{
628 455204eb ths
    if (!qemu_put_mouse_event_current)
629 455204eb ths
        return 0;
630 455204eb ths
631 455204eb ths
    return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
632 455204eb ths
}
633 455204eb ths
634 455204eb ths
void do_info_mice(void)
635 455204eb ths
{
636 455204eb ths
    QEMUPutMouseEntry *cursor;
637 455204eb ths
    int index = 0;
638 455204eb ths
639 455204eb ths
    if (!qemu_put_mouse_event_head) {
640 455204eb ths
        term_printf("No mouse devices connected\n");
641 455204eb ths
        return;
642 455204eb ths
    }
643 455204eb ths
644 455204eb ths
    term_printf("Mouse devices available:\n");
645 455204eb ths
    cursor = qemu_put_mouse_event_head;
646 455204eb ths
    while (cursor != NULL) {
647 455204eb ths
        term_printf("%c Mouse #%d: %s\n",
648 455204eb ths
                    (cursor == qemu_put_mouse_event_current ? '*' : ' '),
649 455204eb ths
                    index, cursor->qemu_put_mouse_event_name);
650 455204eb ths
        index++;
651 455204eb ths
        cursor = cursor->next;
652 455204eb ths
    }
653 455204eb ths
}
654 455204eb ths
655 455204eb ths
void do_mouse_set(int index)
656 455204eb ths
{
657 455204eb ths
    QEMUPutMouseEntry *cursor;
658 455204eb ths
    int i = 0;
659 455204eb ths
660 455204eb ths
    if (!qemu_put_mouse_event_head) {
661 455204eb ths
        term_printf("No mouse devices connected\n");
662 455204eb ths
        return;
663 455204eb ths
    }
664 455204eb ths
665 455204eb ths
    cursor = qemu_put_mouse_event_head;
666 455204eb ths
    while (cursor != NULL && index != i) {
667 455204eb ths
        i++;
668 455204eb ths
        cursor = cursor->next;
669 455204eb ths
    }
670 455204eb ths
671 455204eb ths
    if (cursor != NULL)
672 455204eb ths
        qemu_put_mouse_event_current = cursor;
673 455204eb ths
    else
674 455204eb ths
        term_printf("Mouse at given index not found\n");
675 09b26c5e bellard
}
676 09b26c5e bellard
677 1dce7c3c bellard
/* compute with 96 bit intermediate result: (a*b)/c */
678 1dce7c3c bellard
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
679 0824d6fc bellard
{
680 1dce7c3c bellard
    union {
681 1dce7c3c bellard
        uint64_t ll;
682 1dce7c3c bellard
        struct {
683 1dce7c3c bellard
#ifdef WORDS_BIGENDIAN
684 1dce7c3c bellard
            uint32_t high, low;
685 1dce7c3c bellard
#else
686 1dce7c3c bellard
            uint32_t low, high;
687 3b46e624 ths
#endif
688 1dce7c3c bellard
        } l;
689 1dce7c3c bellard
    } u, res;
690 1dce7c3c bellard
    uint64_t rl, rh;
691 0824d6fc bellard
692 1dce7c3c bellard
    u.ll = a;
693 1dce7c3c bellard
    rl = (uint64_t)u.l.low * (uint64_t)b;
694 1dce7c3c bellard
    rh = (uint64_t)u.l.high * (uint64_t)b;
695 1dce7c3c bellard
    rh += (rl >> 32);
696 1dce7c3c bellard
    res.l.high = rh / c;
697 1dce7c3c bellard
    res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
698 1dce7c3c bellard
    return res.ll;
699 34865134 bellard
}
700 34865134 bellard
701 1dce7c3c bellard
/***********************************************************/
702 1dce7c3c bellard
/* real time host monotonic timer */
703 34865134 bellard
704 1dce7c3c bellard
#define QEMU_TIMER_BASE 1000000000LL
705 34865134 bellard
706 1dce7c3c bellard
#ifdef WIN32
707 0824d6fc bellard
708 1dce7c3c bellard
static int64_t clock_freq;
709 1115dde7 bellard
710 1dce7c3c bellard
static void init_get_clock(void)
711 1115dde7 bellard
{
712 a8e5ac33 bellard
    LARGE_INTEGER freq;
713 a8e5ac33 bellard
    int ret;
714 1dce7c3c bellard
    ret = QueryPerformanceFrequency(&freq);
715 1dce7c3c bellard
    if (ret == 0) {
716 1dce7c3c bellard
        fprintf(stderr, "Could not calibrate ticks\n");
717 1dce7c3c bellard
        exit(1);
718 1dce7c3c bellard
    }
719 1dce7c3c bellard
    clock_freq = freq.QuadPart;
720 1115dde7 bellard
}
721 1115dde7 bellard
722 1dce7c3c bellard
static int64_t get_clock(void)
723 b8076a74 bellard
{
724 1dce7c3c bellard
    LARGE_INTEGER ti;
725 1dce7c3c bellard
    QueryPerformanceCounter(&ti);
726 1dce7c3c bellard
    return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
727 b8076a74 bellard
}
728 b8076a74 bellard
729 1dce7c3c bellard
#else
730 90cb9493 bellard
731 1dce7c3c bellard
static int use_rt_clock;
732 1dce7c3c bellard
733 1dce7c3c bellard
static void init_get_clock(void)
734 90cb9493 bellard
{
735 1dce7c3c bellard
    use_rt_clock = 0;
736 1dce7c3c bellard
#if defined(__linux__)
737 1dce7c3c bellard
    {
738 1dce7c3c bellard
        struct timespec ts;
739 1dce7c3c bellard
        if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
740 1dce7c3c bellard
            use_rt_clock = 1;
741 1dce7c3c bellard
        }
742 1dce7c3c bellard
    }
743 1dce7c3c bellard
#endif
744 90cb9493 bellard
}
745 90cb9493 bellard
746 1dce7c3c bellard
static int64_t get_clock(void)
747 fdbb4691 bellard
{
748 1dce7c3c bellard
#if defined(__linux__)
749 1dce7c3c bellard
    if (use_rt_clock) {
750 1dce7c3c bellard
        struct timespec ts;
751 1dce7c3c bellard
        clock_gettime(CLOCK_MONOTONIC, &ts);
752 1dce7c3c bellard
        return ts.tv_sec * 1000000000LL + ts.tv_nsec;
753 5fafdf24 ths
    } else
754 fdbb4691 bellard
#endif
755 1dce7c3c bellard
    {
756 1dce7c3c bellard
        /* XXX: using gettimeofday leads to problems if the date
757 1dce7c3c bellard
           changes, so it should be avoided. */
758 1dce7c3c bellard
        struct timeval tv;
759 1dce7c3c bellard
        gettimeofday(&tv, NULL);
760 1dce7c3c bellard
        return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000);
761 1dce7c3c bellard
    }
762 fdbb4691 bellard
}
763 34865134 bellard
#endif
764 34865134 bellard
765 2e70f6ef pbrook
/* Return the virtual CPU time, based on the instruction counter.  */
766 2e70f6ef pbrook
static int64_t cpu_get_icount(void)
767 2e70f6ef pbrook
{
768 2e70f6ef pbrook
    int64_t icount;
769 2e70f6ef pbrook
    CPUState *env = cpu_single_env;;
770 2e70f6ef pbrook
    icount = qemu_icount;
771 2e70f6ef pbrook
    if (env) {
772 2e70f6ef pbrook
        if (!can_do_io(env))
773 2e70f6ef pbrook
            fprintf(stderr, "Bad clock read\n");
774 2e70f6ef pbrook
        icount -= (env->icount_decr.u16.low + env->icount_extra);
775 2e70f6ef pbrook
    }
776 2e70f6ef pbrook
    return qemu_icount_bias + (icount << icount_time_shift);
777 2e70f6ef pbrook
}
778 2e70f6ef pbrook
779 1dce7c3c bellard
/***********************************************************/
780 1dce7c3c bellard
/* guest cycle counter */
781 1dce7c3c bellard
782 eade0f19 bellard
static int64_t cpu_ticks_prev;
783 34865134 bellard
static int64_t cpu_ticks_offset;
784 1dce7c3c bellard
static int64_t cpu_clock_offset;
785 8a7ddc38 bellard
static int cpu_ticks_enabled;
786 34865134 bellard
787 1dce7c3c bellard
/* return the host CPU cycle counter and handle stop/restart */
788 1dce7c3c bellard
int64_t cpu_get_ticks(void)
789 34865134 bellard
{
790 2e70f6ef pbrook
    if (use_icount) {
791 2e70f6ef pbrook
        return cpu_get_icount();
792 2e70f6ef pbrook
    }
793 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
794 8a7ddc38 bellard
        return cpu_ticks_offset;
795 8a7ddc38 bellard
    } else {
796 eade0f19 bellard
        int64_t ticks;
797 eade0f19 bellard
        ticks = cpu_get_real_ticks();
798 eade0f19 bellard
        if (cpu_ticks_prev > ticks) {
799 eade0f19 bellard
            /* Note: non increasing ticks may happen if the host uses
800 eade0f19 bellard
               software suspend */
801 eade0f19 bellard
            cpu_ticks_offset += cpu_ticks_prev - ticks;
802 eade0f19 bellard
        }
803 eade0f19 bellard
        cpu_ticks_prev = ticks;
804 eade0f19 bellard
        return ticks + cpu_ticks_offset;
805 8a7ddc38 bellard
    }
806 34865134 bellard
}
807 34865134 bellard
808 1dce7c3c bellard
/* return the host CPU monotonic timer and handle stop/restart */
809 1dce7c3c bellard
static int64_t cpu_get_clock(void)
810 1dce7c3c bellard
{
811 1dce7c3c bellard
    int64_t ti;
812 1dce7c3c bellard
    if (!cpu_ticks_enabled) {
813 1dce7c3c bellard
        return cpu_clock_offset;
814 1dce7c3c bellard
    } else {
815 1dce7c3c bellard
        ti = get_clock();
816 1dce7c3c bellard
        return ti + cpu_clock_offset;
817 1dce7c3c bellard
    }
818 1dce7c3c bellard
}
819 1dce7c3c bellard
820 34865134 bellard
/* enable cpu_get_ticks() */
821 34865134 bellard
void cpu_enable_ticks(void)
822 34865134 bellard
{
823 8a7ddc38 bellard
    if (!cpu_ticks_enabled) {
824 8a7ddc38 bellard
        cpu_ticks_offset -= cpu_get_real_ticks();
825 1dce7c3c bellard
        cpu_clock_offset -= get_clock();
826 8a7ddc38 bellard
        cpu_ticks_enabled = 1;
827 8a7ddc38 bellard
    }
828 34865134 bellard
}
829 34865134 bellard
830 34865134 bellard
/* disable cpu_get_ticks() : the clock is stopped. You must not call
831 34865134 bellard
   cpu_get_ticks() after that.  */
832 34865134 bellard
void cpu_disable_ticks(void)
833 34865134 bellard
{
834 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
835 8a7ddc38 bellard
        cpu_ticks_offset = cpu_get_ticks();
836 1dce7c3c bellard
        cpu_clock_offset = cpu_get_clock();
837 8a7ddc38 bellard
        cpu_ticks_enabled = 0;
838 8a7ddc38 bellard
    }
839 34865134 bellard
}
840 34865134 bellard
841 1dce7c3c bellard
/***********************************************************/
842 1dce7c3c bellard
/* timers */
843 5fafdf24 ths
844 8a7ddc38 bellard
#define QEMU_TIMER_REALTIME 0
845 8a7ddc38 bellard
#define QEMU_TIMER_VIRTUAL  1
846 8a7ddc38 bellard
847 8a7ddc38 bellard
struct QEMUClock {
848 8a7ddc38 bellard
    int type;
849 8a7ddc38 bellard
    /* XXX: add frequency */
850 8a7ddc38 bellard
};
851 8a7ddc38 bellard
852 8a7ddc38 bellard
struct QEMUTimer {
853 8a7ddc38 bellard
    QEMUClock *clock;
854 8a7ddc38 bellard
    int64_t expire_time;
855 8a7ddc38 bellard
    QEMUTimerCB *cb;
856 8a7ddc38 bellard
    void *opaque;
857 8a7ddc38 bellard
    struct QEMUTimer *next;
858 8a7ddc38 bellard
};
859 8a7ddc38 bellard
860 c8994013 ths
struct qemu_alarm_timer {
861 c8994013 ths
    char const *name;
862 efe75411 ths
    unsigned int flags;
863 c8994013 ths
864 c8994013 ths
    int (*start)(struct qemu_alarm_timer *t);
865 c8994013 ths
    void (*stop)(struct qemu_alarm_timer *t);
866 efe75411 ths
    void (*rearm)(struct qemu_alarm_timer *t);
867 c8994013 ths
    void *priv;
868 c8994013 ths
};
869 c8994013 ths
870 efe75411 ths
#define ALARM_FLAG_DYNTICKS  0x1
871 d5d08334 balrog
#define ALARM_FLAG_EXPIRED   0x2
872 efe75411 ths
873 efe75411 ths
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t)
874 efe75411 ths
{
875 efe75411 ths
    return t->flags & ALARM_FLAG_DYNTICKS;
876 efe75411 ths
}
877 efe75411 ths
878 efe75411 ths
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t)
879 efe75411 ths
{
880 efe75411 ths
    if (!alarm_has_dynticks(t))
881 efe75411 ths
        return;
882 efe75411 ths
883 efe75411 ths
    t->rearm(t);
884 efe75411 ths
}
885 efe75411 ths
886 efe75411 ths
/* TODO: MIN_TIMER_REARM_US should be optimized */
887 efe75411 ths
#define MIN_TIMER_REARM_US 250
888 efe75411 ths
889 c8994013 ths
static struct qemu_alarm_timer *alarm_timer;
890 8a7ddc38 bellard
891 40c3bac3 bellard
#ifdef _WIN32
892 c8994013 ths
893 c8994013 ths
struct qemu_alarm_win32 {
894 c8994013 ths
    MMRESULT timerId;
895 c8994013 ths
    HANDLE host_alarm;
896 c8994013 ths
    unsigned int period;
897 c8994013 ths
} alarm_win32_data = {0, NULL, -1};
898 c8994013 ths
899 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t);
900 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t);
901 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t);
902 c8994013 ths
903 40c3bac3 bellard
#else
904 c8994013 ths
905 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t);
906 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t);
907 c8994013 ths
908 231c6586 ths
#ifdef __linux__
909 231c6586 ths
910 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t);
911 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t);
912 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t);
913 efe75411 ths
914 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t);
915 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t);
916 c40ec5a9 ths
917 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t);
918 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t);
919 c8994013 ths
920 efe75411 ths
#endif /* __linux__ */
921 8a7ddc38 bellard
922 c8994013 ths
#endif /* _WIN32 */
923 c8994013 ths
924 2e70f6ef pbrook
/* Correlation between real and virtual time is always going to be
925 bf20dc07 ths
   fairly approximate, so ignore small variation.
926 2e70f6ef pbrook
   When the guest is idle real and virtual time will be aligned in
927 2e70f6ef pbrook
   the IO wait loop.  */
928 2e70f6ef pbrook
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
929 2e70f6ef pbrook
930 2e70f6ef pbrook
static void icount_adjust(void)
931 2e70f6ef pbrook
{
932 2e70f6ef pbrook
    int64_t cur_time;
933 2e70f6ef pbrook
    int64_t cur_icount;
934 2e70f6ef pbrook
    int64_t delta;
935 2e70f6ef pbrook
    static int64_t last_delta;
936 2e70f6ef pbrook
    /* If the VM is not running, then do nothing.  */
937 2e70f6ef pbrook
    if (!vm_running)
938 2e70f6ef pbrook
        return;
939 2e70f6ef pbrook
940 2e70f6ef pbrook
    cur_time = cpu_get_clock();
941 2e70f6ef pbrook
    cur_icount = qemu_get_clock(vm_clock);
942 2e70f6ef pbrook
    delta = cur_icount - cur_time;
943 2e70f6ef pbrook
    /* FIXME: This is a very crude algorithm, somewhat prone to oscillation.  */
944 2e70f6ef pbrook
    if (delta > 0
945 2e70f6ef pbrook
        && last_delta + ICOUNT_WOBBLE < delta * 2
946 2e70f6ef pbrook
        && icount_time_shift > 0) {
947 2e70f6ef pbrook
        /* The guest is getting too far ahead.  Slow time down.  */
948 2e70f6ef pbrook
        icount_time_shift--;
949 2e70f6ef pbrook
    }
950 2e70f6ef pbrook
    if (delta < 0
951 2e70f6ef pbrook
        && last_delta - ICOUNT_WOBBLE > delta * 2
952 2e70f6ef pbrook
        && icount_time_shift < MAX_ICOUNT_SHIFT) {
953 2e70f6ef pbrook
        /* The guest is getting too far behind.  Speed time up.  */
954 2e70f6ef pbrook
        icount_time_shift++;
955 2e70f6ef pbrook
    }
956 2e70f6ef pbrook
    last_delta = delta;
957 2e70f6ef pbrook
    qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift);
958 2e70f6ef pbrook
}
959 2e70f6ef pbrook
960 2e70f6ef pbrook
static void icount_adjust_rt(void * opaque)
961 2e70f6ef pbrook
{
962 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
963 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
964 2e70f6ef pbrook
    icount_adjust();
965 2e70f6ef pbrook
}
966 2e70f6ef pbrook
967 2e70f6ef pbrook
static void icount_adjust_vm(void * opaque)
968 2e70f6ef pbrook
{
969 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
970 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
971 2e70f6ef pbrook
    icount_adjust();
972 2e70f6ef pbrook
}
973 2e70f6ef pbrook
974 2e70f6ef pbrook
static void init_icount_adjust(void)
975 2e70f6ef pbrook
{
976 2e70f6ef pbrook
    /* Have both realtime and virtual time triggers for speed adjustment.
977 2e70f6ef pbrook
       The realtime trigger catches emulated time passing too slowly,
978 2e70f6ef pbrook
       the virtual time trigger catches emulated time passing too fast.
979 2e70f6ef pbrook
       Realtime triggers occur even when idle, so use them less frequently
980 2e70f6ef pbrook
       than VM triggers.  */
981 2e70f6ef pbrook
    icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
982 2e70f6ef pbrook
    qemu_mod_timer(icount_rt_timer,
983 2e70f6ef pbrook
                   qemu_get_clock(rt_clock) + 1000);
984 2e70f6ef pbrook
    icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
985 2e70f6ef pbrook
    qemu_mod_timer(icount_vm_timer,
986 2e70f6ef pbrook
                   qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
987 2e70f6ef pbrook
}
988 2e70f6ef pbrook
989 c8994013 ths
static struct qemu_alarm_timer alarm_timers[] = {
990 efe75411 ths
#ifndef _WIN32
991 231c6586 ths
#ifdef __linux__
992 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
993 efe75411 ths
     dynticks_stop_timer, dynticks_rearm_timer, NULL},
994 c40ec5a9 ths
    /* HPET - if available - is preferred */
995 efe75411 ths
    {"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL},
996 c40ec5a9 ths
    /* ...otherwise try RTC */
997 efe75411 ths
    {"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL},
998 c8994013 ths
#endif
999 efe75411 ths
    {"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL},
1000 c8994013 ths
#else
1001 efe75411 ths
    {"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
1002 efe75411 ths
     win32_stop_timer, win32_rearm_timer, &alarm_win32_data},
1003 efe75411 ths
    {"win32", 0, win32_start_timer,
1004 efe75411 ths
     win32_stop_timer, NULL, &alarm_win32_data},
1005 c8994013 ths
#endif
1006 c8994013 ths
    {NULL, }
1007 c8994013 ths
};
1008 c8994013 ths
1009 3f47aa8c blueswir1
static void show_available_alarms(void)
1010 f3dcfada ths
{
1011 f3dcfada ths
    int i;
1012 f3dcfada ths
1013 f3dcfada ths
    printf("Available alarm timers, in order of precedence:\n");
1014 f3dcfada ths
    for (i = 0; alarm_timers[i].name; i++)
1015 f3dcfada ths
        printf("%s\n", alarm_timers[i].name);
1016 f3dcfada ths
}
1017 f3dcfada ths
1018 f3dcfada ths
static void configure_alarms(char const *opt)
1019 f3dcfada ths
{
1020 f3dcfada ths
    int i;
1021 f3dcfada ths
    int cur = 0;
1022 f3dcfada ths
    int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1;
1023 f3dcfada ths
    char *arg;
1024 f3dcfada ths
    char *name;
1025 2e70f6ef pbrook
    struct qemu_alarm_timer tmp;
1026 f3dcfada ths
1027 3adda04c aurel32
    if (!strcmp(opt, "?")) {
1028 f3dcfada ths
        show_available_alarms();
1029 f3dcfada ths
        exit(0);
1030 f3dcfada ths
    }
1031 f3dcfada ths
1032 f3dcfada ths
    arg = strdup(opt);
1033 f3dcfada ths
1034 f3dcfada ths
    /* Reorder the array */
1035 f3dcfada ths
    name = strtok(arg, ",");
1036 f3dcfada ths
    while (name) {
1037 e2b577e5 balrog
        for (i = 0; i < count && alarm_timers[i].name; i++) {
1038 f3dcfada ths
            if (!strcmp(alarm_timers[i].name, name))
1039 f3dcfada ths
                break;
1040 f3dcfada ths
        }
1041 f3dcfada ths
1042 f3dcfada ths
        if (i == count) {
1043 f3dcfada ths
            fprintf(stderr, "Unknown clock %s\n", name);
1044 f3dcfada ths
            goto next;
1045 f3dcfada ths
        }
1046 f3dcfada ths
1047 f3dcfada ths
        if (i < cur)
1048 f3dcfada ths
            /* Ignore */
1049 f3dcfada ths
            goto next;
1050 f3dcfada ths
1051 f3dcfada ths
        /* Swap */
1052 f3dcfada ths
        tmp = alarm_timers[i];
1053 f3dcfada ths
        alarm_timers[i] = alarm_timers[cur];
1054 f3dcfada ths
        alarm_timers[cur] = tmp;
1055 f3dcfada ths
1056 f3dcfada ths
        cur++;
1057 f3dcfada ths
next:
1058 f3dcfada ths
        name = strtok(NULL, ",");
1059 f3dcfada ths
    }
1060 f3dcfada ths
1061 f3dcfada ths
    free(arg);
1062 f3dcfada ths
1063 f3dcfada ths
    if (cur) {
1064 2e70f6ef pbrook
        /* Disable remaining timers */
1065 f3dcfada ths
        for (i = cur; i < count; i++)
1066 f3dcfada ths
            alarm_timers[i].name = NULL;
1067 3adda04c aurel32
    } else {
1068 3adda04c aurel32
        show_available_alarms();
1069 3adda04c aurel32
        exit(1);
1070 f3dcfada ths
    }
1071 f3dcfada ths
}
1072 f3dcfada ths
1073 c8994013 ths
QEMUClock *rt_clock;
1074 c8994013 ths
QEMUClock *vm_clock;
1075 c8994013 ths
1076 c8994013 ths
static QEMUTimer *active_timers[2];
1077 c8994013 ths
1078 9596ebb7 pbrook
static QEMUClock *qemu_new_clock(int type)
1079 8a7ddc38 bellard
{
1080 8a7ddc38 bellard
    QEMUClock *clock;
1081 8a7ddc38 bellard
    clock = qemu_mallocz(sizeof(QEMUClock));
1082 8a7ddc38 bellard
    if (!clock)
1083 8a7ddc38 bellard
        return NULL;
1084 8a7ddc38 bellard
    clock->type = type;
1085 8a7ddc38 bellard
    return clock;
1086 8a7ddc38 bellard
}
1087 8a7ddc38 bellard
1088 8a7ddc38 bellard
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
1089 8a7ddc38 bellard
{
1090 8a7ddc38 bellard
    QEMUTimer *ts;
1091 8a7ddc38 bellard
1092 8a7ddc38 bellard
    ts = qemu_mallocz(sizeof(QEMUTimer));
1093 8a7ddc38 bellard
    ts->clock = clock;
1094 8a7ddc38 bellard
    ts->cb = cb;
1095 8a7ddc38 bellard
    ts->opaque = opaque;
1096 8a7ddc38 bellard
    return ts;
1097 8a7ddc38 bellard
}
1098 8a7ddc38 bellard
1099 8a7ddc38 bellard
void qemu_free_timer(QEMUTimer *ts)
1100 8a7ddc38 bellard
{
1101 8a7ddc38 bellard
    qemu_free(ts);
1102 8a7ddc38 bellard
}
1103 8a7ddc38 bellard
1104 8a7ddc38 bellard
/* stop a timer, but do not dealloc it */
1105 8a7ddc38 bellard
void qemu_del_timer(QEMUTimer *ts)
1106 8a7ddc38 bellard
{
1107 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1108 8a7ddc38 bellard
1109 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1110 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1111 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1112 8a7ddc38 bellard
    for(;;) {
1113 8a7ddc38 bellard
        t = *pt;
1114 8a7ddc38 bellard
        if (!t)
1115 8a7ddc38 bellard
            break;
1116 8a7ddc38 bellard
        if (t == ts) {
1117 8a7ddc38 bellard
            *pt = t->next;
1118 8a7ddc38 bellard
            break;
1119 8a7ddc38 bellard
        }
1120 8a7ddc38 bellard
        pt = &t->next;
1121 8a7ddc38 bellard
    }
1122 8a7ddc38 bellard
}
1123 8a7ddc38 bellard
1124 8a7ddc38 bellard
/* modify the current timer so that it will be fired when current_time
1125 8a7ddc38 bellard
   >= expire_time. The corresponding callback will be called. */
1126 8a7ddc38 bellard
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
1127 8a7ddc38 bellard
{
1128 8a7ddc38 bellard
    QEMUTimer **pt, *t;
1129 8a7ddc38 bellard
1130 8a7ddc38 bellard
    qemu_del_timer(ts);
1131 8a7ddc38 bellard
1132 8a7ddc38 bellard
    /* add the timer in the sorted list */
1133 8a7ddc38 bellard
    /* NOTE: this code must be signal safe because
1134 8a7ddc38 bellard
       qemu_timer_expired() can be called from a signal. */
1135 8a7ddc38 bellard
    pt = &active_timers[ts->clock->type];
1136 8a7ddc38 bellard
    for(;;) {
1137 8a7ddc38 bellard
        t = *pt;
1138 8a7ddc38 bellard
        if (!t)
1139 8a7ddc38 bellard
            break;
1140 5fafdf24 ths
        if (t->expire_time > expire_time)
1141 8a7ddc38 bellard
            break;
1142 8a7ddc38 bellard
        pt = &t->next;
1143 8a7ddc38 bellard
    }
1144 8a7ddc38 bellard
    ts->expire_time = expire_time;
1145 8a7ddc38 bellard
    ts->next = *pt;
1146 8a7ddc38 bellard
    *pt = ts;
1147 d5d08334 balrog
1148 d5d08334 balrog
    /* Rearm if necessary  */
1149 2e70f6ef pbrook
    if (pt == &active_timers[ts->clock->type]) {
1150 2e70f6ef pbrook
        if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) {
1151 2e70f6ef pbrook
            qemu_rearm_alarm_timer(alarm_timer);
1152 2e70f6ef pbrook
        }
1153 2e70f6ef pbrook
        /* Interrupt execution to force deadline recalculation.  */
1154 2e70f6ef pbrook
        if (use_icount && cpu_single_env) {
1155 2e70f6ef pbrook
            cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1156 2e70f6ef pbrook
        }
1157 2e70f6ef pbrook
    }
1158 8a7ddc38 bellard
}
1159 8a7ddc38 bellard
1160 8a7ddc38 bellard
int qemu_timer_pending(QEMUTimer *ts)
1161 8a7ddc38 bellard
{
1162 8a7ddc38 bellard
    QEMUTimer *t;
1163 8a7ddc38 bellard
    for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
1164 8a7ddc38 bellard
        if (t == ts)
1165 8a7ddc38 bellard
            return 1;
1166 8a7ddc38 bellard
    }
1167 8a7ddc38 bellard
    return 0;
1168 8a7ddc38 bellard
}
1169 8a7ddc38 bellard
1170 8a7ddc38 bellard
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
1171 8a7ddc38 bellard
{
1172 8a7ddc38 bellard
    if (!timer_head)
1173 8a7ddc38 bellard
        return 0;
1174 8a7ddc38 bellard
    return (timer_head->expire_time <= current_time);
1175 8a7ddc38 bellard
}
1176 8a7ddc38 bellard
1177 8a7ddc38 bellard
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
1178 8a7ddc38 bellard
{
1179 8a7ddc38 bellard
    QEMUTimer *ts;
1180 3b46e624 ths
1181 8a7ddc38 bellard
    for(;;) {
1182 8a7ddc38 bellard
        ts = *ptimer_head;
1183 e95c8d51 bellard
        if (!ts || ts->expire_time > current_time)
1184 8a7ddc38 bellard
            break;
1185 8a7ddc38 bellard
        /* remove timer from the list before calling the callback */
1186 8a7ddc38 bellard
        *ptimer_head = ts->next;
1187 8a7ddc38 bellard
        ts->next = NULL;
1188 3b46e624 ths
1189 8a7ddc38 bellard
        /* run the callback (the timer list can be modified) */
1190 8a7ddc38 bellard
        ts->cb(ts->opaque);
1191 8a7ddc38 bellard
    }
1192 8a7ddc38 bellard
}
1193 8a7ddc38 bellard
1194 8a7ddc38 bellard
int64_t qemu_get_clock(QEMUClock *clock)
1195 8a7ddc38 bellard
{
1196 8a7ddc38 bellard
    switch(clock->type) {
1197 8a7ddc38 bellard
    case QEMU_TIMER_REALTIME:
1198 1dce7c3c bellard
        return get_clock() / 1000000;
1199 8a7ddc38 bellard
    default:
1200 8a7ddc38 bellard
    case QEMU_TIMER_VIRTUAL:
1201 2e70f6ef pbrook
        if (use_icount) {
1202 2e70f6ef pbrook
            return cpu_get_icount();
1203 2e70f6ef pbrook
        } else {
1204 2e70f6ef pbrook
            return cpu_get_clock();
1205 2e70f6ef pbrook
        }
1206 8a7ddc38 bellard
    }
1207 8a7ddc38 bellard
}
1208 8a7ddc38 bellard
1209 1dce7c3c bellard
static void init_timers(void)
1210 1dce7c3c bellard
{
1211 1dce7c3c bellard
    init_get_clock();
1212 1dce7c3c bellard
    ticks_per_sec = QEMU_TIMER_BASE;
1213 1dce7c3c bellard
    rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
1214 1dce7c3c bellard
    vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
1215 1dce7c3c bellard
}
1216 1dce7c3c bellard
1217 8a7ddc38 bellard
/* save a timer */
1218 8a7ddc38 bellard
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
1219 8a7ddc38 bellard
{
1220 8a7ddc38 bellard
    uint64_t expire_time;
1221 8a7ddc38 bellard
1222 8a7ddc38 bellard
    if (qemu_timer_pending(ts)) {
1223 8a7ddc38 bellard
        expire_time = ts->expire_time;
1224 8a7ddc38 bellard
    } else {
1225 8a7ddc38 bellard
        expire_time = -1;
1226 8a7ddc38 bellard
    }
1227 8a7ddc38 bellard
    qemu_put_be64(f, expire_time);
1228 8a7ddc38 bellard
}
1229 8a7ddc38 bellard
1230 8a7ddc38 bellard
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
1231 8a7ddc38 bellard
{
1232 8a7ddc38 bellard
    uint64_t expire_time;
1233 8a7ddc38 bellard
1234 8a7ddc38 bellard
    expire_time = qemu_get_be64(f);
1235 8a7ddc38 bellard
    if (expire_time != -1) {
1236 8a7ddc38 bellard
        qemu_mod_timer(ts, expire_time);
1237 8a7ddc38 bellard
    } else {
1238 8a7ddc38 bellard
        qemu_del_timer(ts);
1239 8a7ddc38 bellard
    }
1240 8a7ddc38 bellard
}
1241 8a7ddc38 bellard
1242 8a7ddc38 bellard
static void timer_save(QEMUFile *f, void *opaque)
1243 8a7ddc38 bellard
{
1244 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1245 8a7ddc38 bellard
        hw_error("cannot save state if virtual timers are running");
1246 8a7ddc38 bellard
    }
1247 bee8d684 ths
    qemu_put_be64(f, cpu_ticks_offset);
1248 bee8d684 ths
    qemu_put_be64(f, ticks_per_sec);
1249 bee8d684 ths
    qemu_put_be64(f, cpu_clock_offset);
1250 8a7ddc38 bellard
}
1251 8a7ddc38 bellard
1252 8a7ddc38 bellard
static int timer_load(QEMUFile *f, void *opaque, int version_id)
1253 8a7ddc38 bellard
{
1254 c88676f8 bellard
    if (version_id != 1 && version_id != 2)
1255 8a7ddc38 bellard
        return -EINVAL;
1256 8a7ddc38 bellard
    if (cpu_ticks_enabled) {
1257 8a7ddc38 bellard
        return -EINVAL;
1258 8a7ddc38 bellard
    }
1259 bee8d684 ths
    cpu_ticks_offset=qemu_get_be64(f);
1260 bee8d684 ths
    ticks_per_sec=qemu_get_be64(f);
1261 c88676f8 bellard
    if (version_id == 2) {
1262 bee8d684 ths
        cpu_clock_offset=qemu_get_be64(f);
1263 c88676f8 bellard
    }
1264 8a7ddc38 bellard
    return 0;
1265 8a7ddc38 bellard
}
1266 8a7ddc38 bellard
1267 67b915a5 bellard
#ifdef _WIN32
1268 5fafdf24 ths
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
1269 67b915a5 bellard
                                 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
1270 67b915a5 bellard
#else
1271 8a7ddc38 bellard
static void host_alarm_handler(int host_signum)
1272 67b915a5 bellard
#endif
1273 8a7ddc38 bellard
{
1274 02ba45c5 bellard
#if 0
1275 02ba45c5 bellard
#define DISP_FREQ 1000
1276 02ba45c5 bellard
    {
1277 02ba45c5 bellard
        static int64_t delta_min = INT64_MAX;
1278 02ba45c5 bellard
        static int64_t delta_max, delta_cum, last_clock, delta, ti;
1279 02ba45c5 bellard
        static int count;
1280 02ba45c5 bellard
        ti = qemu_get_clock(vm_clock);
1281 02ba45c5 bellard
        if (last_clock != 0) {
1282 02ba45c5 bellard
            delta = ti - last_clock;
1283 02ba45c5 bellard
            if (delta < delta_min)
1284 02ba45c5 bellard
                delta_min = delta;
1285 02ba45c5 bellard
            if (delta > delta_max)
1286 02ba45c5 bellard
                delta_max = delta;
1287 02ba45c5 bellard
            delta_cum += delta;
1288 02ba45c5 bellard
            if (++count == DISP_FREQ) {
1289 26a76461 bellard
                printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
1290 02ba45c5 bellard
                       muldiv64(delta_min, 1000000, ticks_per_sec),
1291 02ba45c5 bellard
                       muldiv64(delta_max, 1000000, ticks_per_sec),
1292 02ba45c5 bellard
                       muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
1293 02ba45c5 bellard
                       (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
1294 02ba45c5 bellard
                count = 0;
1295 02ba45c5 bellard
                delta_min = INT64_MAX;
1296 02ba45c5 bellard
                delta_max = 0;
1297 02ba45c5 bellard
                delta_cum = 0;
1298 02ba45c5 bellard
            }
1299 02ba45c5 bellard
        }
1300 02ba45c5 bellard
        last_clock = ti;
1301 02ba45c5 bellard
    }
1302 02ba45c5 bellard
#endif
1303 efe75411 ths
    if (alarm_has_dynticks(alarm_timer) ||
1304 2e70f6ef pbrook
        (!use_icount &&
1305 2e70f6ef pbrook
            qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
1306 2e70f6ef pbrook
                               qemu_get_clock(vm_clock))) ||
1307 8a7ddc38 bellard
        qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
1308 8a7ddc38 bellard
                           qemu_get_clock(rt_clock))) {
1309 06d9f2f7 bellard
#ifdef _WIN32
1310 c8994013 ths
        struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv;
1311 c8994013 ths
        SetEvent(data->host_alarm);
1312 06d9f2f7 bellard
#endif
1313 ee5605e5 balrog
        CPUState *env = next_cpu;
1314 ee5605e5 balrog
1315 d5d08334 balrog
        alarm_timer->flags |= ALARM_FLAG_EXPIRED;
1316 d5d08334 balrog
1317 4f8eb8da balrog
        if (env) {
1318 4f8eb8da balrog
            /* stop the currently executing cpu because a timer occured */
1319 4f8eb8da balrog
            cpu_interrupt(env, CPU_INTERRUPT_EXIT);
1320 a332e112 bellard
#ifdef USE_KQEMU
1321 4f8eb8da balrog
            if (env->kqemu_enabled) {
1322 4f8eb8da balrog
                kqemu_cpu_interrupt(env);
1323 4f8eb8da balrog
            }
1324 ee5605e5 balrog
#endif
1325 4f8eb8da balrog
        }
1326 ee5605e5 balrog
        event_pending = 1;
1327 8a7ddc38 bellard
    }
1328 8a7ddc38 bellard
}
1329 8a7ddc38 bellard
1330 2e70f6ef pbrook
static int64_t qemu_next_deadline(void)
1331 efe75411 ths
{
1332 2e70f6ef pbrook
    int64_t delta;
1333 efe75411 ths
1334 efe75411 ths
    if (active_timers[QEMU_TIMER_VIRTUAL]) {
1335 2e70f6ef pbrook
        delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time -
1336 2e70f6ef pbrook
                     qemu_get_clock(vm_clock);
1337 2e70f6ef pbrook
    } else {
1338 2e70f6ef pbrook
        /* To avoid problems with overflow limit this to 2^32.  */
1339 2e70f6ef pbrook
        delta = INT32_MAX;
1340 efe75411 ths
    }
1341 efe75411 ths
1342 2e70f6ef pbrook
    if (delta < 0)
1343 2e70f6ef pbrook
        delta = 0;
1344 efe75411 ths
1345 2e70f6ef pbrook
    return delta;
1346 2e70f6ef pbrook
}
1347 2e70f6ef pbrook
1348 2e70f6ef pbrook
static uint64_t qemu_next_deadline_dyntick(void)
1349 2e70f6ef pbrook
{
1350 2e70f6ef pbrook
    int64_t delta;
1351 2e70f6ef pbrook
    int64_t rtdelta;
1352 2e70f6ef pbrook
1353 2e70f6ef pbrook
    if (use_icount)
1354 2e70f6ef pbrook
        delta = INT32_MAX;
1355 2e70f6ef pbrook
    else
1356 2e70f6ef pbrook
        delta = (qemu_next_deadline() + 999) / 1000;
1357 2e70f6ef pbrook
1358 2e70f6ef pbrook
    if (active_timers[QEMU_TIMER_REALTIME]) {
1359 2e70f6ef pbrook
        rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time -
1360 2e70f6ef pbrook
                 qemu_get_clock(rt_clock))*1000;
1361 2e70f6ef pbrook
        if (rtdelta < delta)
1362 2e70f6ef pbrook
            delta = rtdelta;
1363 2e70f6ef pbrook
    }
1364 2e70f6ef pbrook
1365 2e70f6ef pbrook
    if (delta < MIN_TIMER_REARM_US)
1366 2e70f6ef pbrook
        delta = MIN_TIMER_REARM_US;
1367 2e70f6ef pbrook
1368 2e70f6ef pbrook
    return delta;
1369 efe75411 ths
}
1370 efe75411 ths
1371 fd872598 bellard
#ifndef _WIN32
1372 fd872598 bellard
1373 829309c7 bellard
#if defined(__linux__)
1374 829309c7 bellard
1375 fd872598 bellard
#define RTC_FREQ 1024
1376 fd872598 bellard
1377 c8994013 ths
static void enable_sigio_timer(int fd)
1378 c8994013 ths
{
1379 c8994013 ths
    struct sigaction act;
1380 c8994013 ths
1381 c8994013 ths
    /* timer signal */
1382 c8994013 ths
    sigfillset(&act.sa_mask);
1383 c8994013 ths
    act.sa_flags = 0;
1384 c8994013 ths
    act.sa_handler = host_alarm_handler;
1385 c8994013 ths
1386 c8994013 ths
    sigaction(SIGIO, &act, NULL);
1387 c8994013 ths
    fcntl(fd, F_SETFL, O_ASYNC);
1388 c8994013 ths
    fcntl(fd, F_SETOWN, getpid());
1389 c8994013 ths
}
1390 829309c7 bellard
1391 c40ec5a9 ths
static int hpet_start_timer(struct qemu_alarm_timer *t)
1392 c40ec5a9 ths
{
1393 c40ec5a9 ths
    struct hpet_info info;
1394 c40ec5a9 ths
    int r, fd;
1395 c40ec5a9 ths
1396 c40ec5a9 ths
    fd = open("/dev/hpet", O_RDONLY);
1397 c40ec5a9 ths
    if (fd < 0)
1398 c40ec5a9 ths
        return -1;
1399 c40ec5a9 ths
1400 c40ec5a9 ths
    /* Set frequency */
1401 c40ec5a9 ths
    r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ);
1402 c40ec5a9 ths
    if (r < 0) {
1403 c40ec5a9 ths
        fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1404 c40ec5a9 ths
                "error, but for better emulation accuracy type:\n"
1405 c40ec5a9 ths
                "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1406 c40ec5a9 ths
        goto fail;
1407 c40ec5a9 ths
    }
1408 c40ec5a9 ths
1409 c40ec5a9 ths
    /* Check capabilities */
1410 c40ec5a9 ths
    r = ioctl(fd, HPET_INFO, &info);
1411 c40ec5a9 ths
    if (r < 0)
1412 c40ec5a9 ths
        goto fail;
1413 c40ec5a9 ths
1414 c40ec5a9 ths
    /* Enable periodic mode */
1415 c40ec5a9 ths
    r = ioctl(fd, HPET_EPI, 0);
1416 c40ec5a9 ths
    if (info.hi_flags && (r < 0))
1417 c40ec5a9 ths
        goto fail;
1418 c40ec5a9 ths
1419 c40ec5a9 ths
    /* Enable interrupt */
1420 c40ec5a9 ths
    r = ioctl(fd, HPET_IE_ON, 0);
1421 c40ec5a9 ths
    if (r < 0)
1422 c40ec5a9 ths
        goto fail;
1423 c40ec5a9 ths
1424 c40ec5a9 ths
    enable_sigio_timer(fd);
1425 fcdc2129 pbrook
    t->priv = (void *)(long)fd;
1426 c40ec5a9 ths
1427 c40ec5a9 ths
    return 0;
1428 c40ec5a9 ths
fail:
1429 c40ec5a9 ths
    close(fd);
1430 c40ec5a9 ths
    return -1;
1431 c40ec5a9 ths
}
1432 c40ec5a9 ths
1433 c40ec5a9 ths
static void hpet_stop_timer(struct qemu_alarm_timer *t)
1434 c40ec5a9 ths
{
1435 fcdc2129 pbrook
    int fd = (long)t->priv;
1436 c40ec5a9 ths
1437 c40ec5a9 ths
    close(fd);
1438 c40ec5a9 ths
}
1439 c40ec5a9 ths
1440 c8994013 ths
static int rtc_start_timer(struct qemu_alarm_timer *t)
1441 fd872598 bellard
{
1442 c8994013 ths
    int rtc_fd;
1443 b5a23ad4 balrog
    unsigned long current_rtc_freq = 0;
1444 c8994013 ths
1445 aeb30be6 balrog
    TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
1446 fd872598 bellard
    if (rtc_fd < 0)
1447 fd872598 bellard
        return -1;
1448 b5a23ad4 balrog
    ioctl(rtc_fd, RTC_IRQP_READ, &current_rtc_freq);
1449 b5a23ad4 balrog
    if (current_rtc_freq != RTC_FREQ &&
1450 b5a23ad4 balrog
        ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
1451 fd872598 bellard
        fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1452 fd872598 bellard
                "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1453 fd872598 bellard
                "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1454 fd872598 bellard
        goto fail;
1455 fd872598 bellard
    }
1456 fd872598 bellard
    if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
1457 fd872598 bellard
    fail:
1458 fd872598 bellard
        close(rtc_fd);
1459 fd872598 bellard
        return -1;
1460 fd872598 bellard
    }
1461 c8994013 ths
1462 c8994013 ths
    enable_sigio_timer(rtc_fd);
1463 c8994013 ths
1464 fcdc2129 pbrook
    t->priv = (void *)(long)rtc_fd;
1465 c8994013 ths
1466 fd872598 bellard
    return 0;
1467 fd872598 bellard
}
1468 fd872598 bellard
1469 c8994013 ths
static void rtc_stop_timer(struct qemu_alarm_timer *t)
1470 829309c7 bellard
{
1471 fcdc2129 pbrook
    int rtc_fd = (long)t->priv;
1472 c8994013 ths
1473 c8994013 ths
    close(rtc_fd);
1474 829309c7 bellard
}
1475 829309c7 bellard
1476 efe75411 ths
static int dynticks_start_timer(struct qemu_alarm_timer *t)
1477 efe75411 ths
{
1478 efe75411 ths
    struct sigevent ev;
1479 efe75411 ths
    timer_t host_timer;
1480 efe75411 ths
    struct sigaction act;
1481 efe75411 ths
1482 efe75411 ths
    sigfillset(&act.sa_mask);
1483 efe75411 ths
    act.sa_flags = 0;
1484 efe75411 ths
    act.sa_handler = host_alarm_handler;
1485 efe75411 ths
1486 efe75411 ths
    sigaction(SIGALRM, &act, NULL);
1487 efe75411 ths
1488 efe75411 ths
    ev.sigev_value.sival_int = 0;
1489 efe75411 ths
    ev.sigev_notify = SIGEV_SIGNAL;
1490 efe75411 ths
    ev.sigev_signo = SIGALRM;
1491 efe75411 ths
1492 efe75411 ths
    if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
1493 efe75411 ths
        perror("timer_create");
1494 efe75411 ths
1495 efe75411 ths
        /* disable dynticks */
1496 efe75411 ths
        fprintf(stderr, "Dynamic Ticks disabled\n");
1497 efe75411 ths
1498 efe75411 ths
        return -1;
1499 efe75411 ths
    }
1500 efe75411 ths
1501 efe75411 ths
    t->priv = (void *)host_timer;
1502 efe75411 ths
1503 efe75411 ths
    return 0;
1504 efe75411 ths
}
1505 efe75411 ths
1506 efe75411 ths
static void dynticks_stop_timer(struct qemu_alarm_timer *t)
1507 efe75411 ths
{
1508 efe75411 ths
    timer_t host_timer = (timer_t)t->priv;
1509 efe75411 ths
1510 efe75411 ths
    timer_delete(host_timer);
1511 efe75411 ths
}
1512 efe75411 ths
1513 efe75411 ths
static void dynticks_rearm_timer(struct qemu_alarm_timer *t)
1514 efe75411 ths
{
1515 efe75411 ths
    timer_t host_timer = (timer_t)t->priv;
1516 efe75411 ths
    struct itimerspec timeout;
1517 efe75411 ths
    int64_t nearest_delta_us = INT64_MAX;
1518 efe75411 ths
    int64_t current_us;
1519 efe75411 ths
1520 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1521 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1522 d5d08334 balrog
        return;
1523 efe75411 ths
1524 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1525 efe75411 ths
1526 efe75411 ths
    /* check whether a timer is already running */
1527 efe75411 ths
    if (timer_gettime(host_timer, &timeout)) {
1528 efe75411 ths
        perror("gettime");
1529 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1530 efe75411 ths
        exit(1);
1531 efe75411 ths
    }
1532 efe75411 ths
    current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000;
1533 efe75411 ths
    if (current_us && current_us <= nearest_delta_us)
1534 efe75411 ths
        return;
1535 efe75411 ths
1536 efe75411 ths
    timeout.it_interval.tv_sec = 0;
1537 efe75411 ths
    timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */
1538 efe75411 ths
    timeout.it_value.tv_sec =  nearest_delta_us / 1000000;
1539 efe75411 ths
    timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000;
1540 efe75411 ths
    if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) {
1541 efe75411 ths
        perror("settime");
1542 efe75411 ths
        fprintf(stderr, "Internal timer error: aborting\n");
1543 efe75411 ths
        exit(1);
1544 efe75411 ths
    }
1545 efe75411 ths
}
1546 efe75411 ths
1547 70744b3a ths
#endif /* defined(__linux__) */
1548 231c6586 ths
1549 c8994013 ths
static int unix_start_timer(struct qemu_alarm_timer *t)
1550 c8994013 ths
{
1551 c8994013 ths
    struct sigaction act;
1552 c8994013 ths
    struct itimerval itv;
1553 c8994013 ths
    int err;
1554 c8994013 ths
1555 c8994013 ths
    /* timer signal */
1556 c8994013 ths
    sigfillset(&act.sa_mask);
1557 c8994013 ths
    act.sa_flags = 0;
1558 c8994013 ths
    act.sa_handler = host_alarm_handler;
1559 c8994013 ths
1560 c8994013 ths
    sigaction(SIGALRM, &act, NULL);
1561 c8994013 ths
1562 c8994013 ths
    itv.it_interval.tv_sec = 0;
1563 c8994013 ths
    /* for i386 kernel 2.6 to get 1 ms */
1564 c8994013 ths
    itv.it_interval.tv_usec = 999;
1565 c8994013 ths
    itv.it_value.tv_sec = 0;
1566 c8994013 ths
    itv.it_value.tv_usec = 10 * 1000;
1567 c8994013 ths
1568 c8994013 ths
    err = setitimer(ITIMER_REAL, &itv, NULL);
1569 c8994013 ths
    if (err)
1570 c8994013 ths
        return -1;
1571 c8994013 ths
1572 c8994013 ths
    return 0;
1573 c8994013 ths
}
1574 c8994013 ths
1575 c8994013 ths
static void unix_stop_timer(struct qemu_alarm_timer *t)
1576 c8994013 ths
{
1577 c8994013 ths
    struct itimerval itv;
1578 c8994013 ths
1579 c8994013 ths
    memset(&itv, 0, sizeof(itv));
1580 c8994013 ths
    setitimer(ITIMER_REAL, &itv, NULL);
1581 c8994013 ths
}
1582 c8994013 ths
1583 829309c7 bellard
#endif /* !defined(_WIN32) */
1584 fd872598 bellard
1585 c8994013 ths
#ifdef _WIN32
1586 c8994013 ths
1587 c8994013 ths
static int win32_start_timer(struct qemu_alarm_timer *t)
1588 c8994013 ths
{
1589 c8994013 ths
    TIMECAPS tc;
1590 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1591 efe75411 ths
    UINT flags;
1592 c8994013 ths
1593 c8994013 ths
    data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL);
1594 c8994013 ths
    if (!data->host_alarm) {
1595 c8994013 ths
        perror("Failed CreateEvent");
1596 c396a7f0 ths
        return -1;
1597 c8994013 ths
    }
1598 c8994013 ths
1599 c8994013 ths
    memset(&tc, 0, sizeof(tc));
1600 c8994013 ths
    timeGetDevCaps(&tc, sizeof(tc));
1601 c8994013 ths
1602 c8994013 ths
    if (data->period < tc.wPeriodMin)
1603 c8994013 ths
        data->period = tc.wPeriodMin;
1604 c8994013 ths
1605 c8994013 ths
    timeBeginPeriod(data->period);
1606 c8994013 ths
1607 efe75411 ths
    flags = TIME_CALLBACK_FUNCTION;
1608 efe75411 ths
    if (alarm_has_dynticks(t))
1609 efe75411 ths
        flags |= TIME_ONESHOT;
1610 efe75411 ths
    else
1611 efe75411 ths
        flags |= TIME_PERIODIC;
1612 efe75411 ths
1613 c8994013 ths
    data->timerId = timeSetEvent(1,         // interval (ms)
1614 c8994013 ths
                        data->period,       // resolution
1615 c8994013 ths
                        host_alarm_handler, // function
1616 c8994013 ths
                        (DWORD)t,           // parameter
1617 efe75411 ths
                        flags);
1618 c8994013 ths
1619 c8994013 ths
    if (!data->timerId) {
1620 c8994013 ths
        perror("Failed to initialize win32 alarm timer");
1621 c8994013 ths
1622 c8994013 ths
        timeEndPeriod(data->period);
1623 c8994013 ths
        CloseHandle(data->host_alarm);
1624 c8994013 ths
        return -1;
1625 c8994013 ths
    }
1626 c8994013 ths
1627 c8994013 ths
    qemu_add_wait_object(data->host_alarm, NULL, NULL);
1628 c8994013 ths
1629 c8994013 ths
    return 0;
1630 c8994013 ths
}
1631 c8994013 ths
1632 c8994013 ths
static void win32_stop_timer(struct qemu_alarm_timer *t)
1633 c8994013 ths
{
1634 c8994013 ths
    struct qemu_alarm_win32 *data = t->priv;
1635 c8994013 ths
1636 c8994013 ths
    timeKillEvent(data->timerId);
1637 c8994013 ths
    timeEndPeriod(data->period);
1638 c8994013 ths
1639 c8994013 ths
    CloseHandle(data->host_alarm);
1640 c8994013 ths
}
1641 c8994013 ths
1642 efe75411 ths
static void win32_rearm_timer(struct qemu_alarm_timer *t)
1643 efe75411 ths
{
1644 efe75411 ths
    struct qemu_alarm_win32 *data = t->priv;
1645 efe75411 ths
    uint64_t nearest_delta_us;
1646 efe75411 ths
1647 efe75411 ths
    if (!active_timers[QEMU_TIMER_REALTIME] &&
1648 efe75411 ths
                !active_timers[QEMU_TIMER_VIRTUAL])
1649 d5d08334 balrog
        return;
1650 efe75411 ths
1651 2e70f6ef pbrook
    nearest_delta_us = qemu_next_deadline_dyntick();
1652 efe75411 ths
    nearest_delta_us /= 1000;
1653 efe75411 ths
1654 efe75411 ths
    timeKillEvent(data->timerId);
1655 efe75411 ths
1656 efe75411 ths
    data->timerId = timeSetEvent(1,
1657 efe75411 ths
                        data->period,
1658 efe75411 ths
                        host_alarm_handler,
1659 efe75411 ths
                        (DWORD)t,
1660 efe75411 ths
                        TIME_ONESHOT | TIME_PERIODIC);
1661 efe75411 ths
1662 efe75411 ths
    if (!data->timerId) {
1663 efe75411 ths
        perror("Failed to re-arm win32 alarm timer");
1664 efe75411 ths
1665 efe75411 ths
        timeEndPeriod(data->period);
1666 efe75411 ths
        CloseHandle(data->host_alarm);
1667 efe75411 ths
        exit(1);
1668 efe75411 ths
    }
1669 efe75411 ths
}
1670 efe75411 ths
1671 c8994013 ths
#endif /* _WIN32 */
1672 c8994013 ths
1673 1dce7c3c bellard
static void init_timer_alarm(void)
1674 8a7ddc38 bellard
{
1675 c8994013 ths
    struct qemu_alarm_timer *t;
1676 c8994013 ths
    int i, err = -1;
1677 c8994013 ths
1678 c8994013 ths
    for (i = 0; alarm_timers[i].name; i++) {
1679 c8994013 ths
        t = &alarm_timers[i];
1680 c8994013 ths
1681 c8994013 ths
        err = t->start(t);
1682 c8994013 ths
        if (!err)
1683 c8994013 ths
            break;
1684 67b915a5 bellard
    }
1685 fd872598 bellard
1686 c8994013 ths
    if (err) {
1687 c8994013 ths
        fprintf(stderr, "Unable to find any suitable alarm timer.\n");
1688 c8994013 ths
        fprintf(stderr, "Terminating\n");
1689 c8994013 ths
        exit(1);
1690 67b915a5 bellard
    }
1691 c8994013 ths
1692 c8994013 ths
    alarm_timer = t;
1693 8a7ddc38 bellard
}
1694 8a7ddc38 bellard
1695 9596ebb7 pbrook
static void quit_timers(void)
1696 40c3bac3 bellard
{
1697 c8994013 ths
    alarm_timer->stop(alarm_timer);
1698 c8994013 ths
    alarm_timer = NULL;
1699 40c3bac3 bellard
}
1700 40c3bac3 bellard
1701 c4b1fcc0 bellard
/***********************************************************/
1702 f6503059 balrog
/* host time/date access */
1703 f6503059 balrog
void qemu_get_timedate(struct tm *tm, int offset)
1704 f6503059 balrog
{
1705 f6503059 balrog
    time_t ti;
1706 f6503059 balrog
    struct tm *ret;
1707 f6503059 balrog
1708 f6503059 balrog
    time(&ti);
1709 f6503059 balrog
    ti += offset;
1710 f6503059 balrog
    if (rtc_date_offset == -1) {
1711 f6503059 balrog
        if (rtc_utc)
1712 f6503059 balrog
            ret = gmtime(&ti);
1713 f6503059 balrog
        else
1714 f6503059 balrog
            ret = localtime(&ti);
1715 f6503059 balrog
    } else {
1716 f6503059 balrog
        ti -= rtc_date_offset;
1717 f6503059 balrog
        ret = gmtime(&ti);
1718 f6503059 balrog
    }
1719 f6503059 balrog
1720 f6503059 balrog
    memcpy(tm, ret, sizeof(struct tm));
1721 f6503059 balrog
}
1722 f6503059 balrog
1723 f6503059 balrog
int qemu_timedate_diff(struct tm *tm)
1724 f6503059 balrog
{
1725 f6503059 balrog
    time_t seconds;
1726 f6503059 balrog
1727 f6503059 balrog
    if (rtc_date_offset == -1)
1728 f6503059 balrog
        if (rtc_utc)
1729 f6503059 balrog
            seconds = mktimegm(tm);
1730 f6503059 balrog
        else
1731 f6503059 balrog
            seconds = mktime(tm);
1732 f6503059 balrog
    else
1733 f6503059 balrog
        seconds = mktimegm(tm) + rtc_date_offset;
1734 f6503059 balrog
1735 f6503059 balrog
    return seconds - time(NULL);
1736 f6503059 balrog
}
1737 f6503059 balrog
1738 f6503059 balrog
/***********************************************************/
1739 82c643ff bellard
/* character device */
1740 313aa567 bellard
1741 e5b0bc44 pbrook
static void qemu_chr_event(CharDriverState *s, int event)
1742 e5b0bc44 pbrook
{
1743 e5b0bc44 pbrook
    if (!s->chr_event)
1744 e5b0bc44 pbrook
        return;
1745 e5b0bc44 pbrook
    s->chr_event(s->handler_opaque, event);
1746 e5b0bc44 pbrook
}
1747 e5b0bc44 pbrook
1748 86e94dea ths
static void qemu_chr_reset_bh(void *opaque)
1749 86e94dea ths
{
1750 86e94dea ths
    CharDriverState *s = opaque;
1751 e5b0bc44 pbrook
    qemu_chr_event(s, CHR_EVENT_RESET);
1752 86e94dea ths
    qemu_bh_delete(s->bh);
1753 86e94dea ths
    s->bh = NULL;
1754 86e94dea ths
}
1755 86e94dea ths
1756 86e94dea ths
void qemu_chr_reset(CharDriverState *s)
1757 86e94dea ths
{
1758 86e94dea ths
    if (s->bh == NULL) {
1759 86e94dea ths
        s->bh = qemu_bh_new(qemu_chr_reset_bh, s);
1760 86e94dea ths
        qemu_bh_schedule(s->bh);
1761 86e94dea ths
    }
1762 86e94dea ths
}
1763 86e94dea ths
1764 82c643ff bellard
int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len)
1765 82c643ff bellard
{
1766 82c643ff bellard
    return s->chr_write(s, buf, len);
1767 82c643ff bellard
}
1768 67b915a5 bellard
1769 e57a8c0e bellard
int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg)
1770 f8d179e3 bellard
{
1771 e57a8c0e bellard
    if (!s->chr_ioctl)
1772 e57a8c0e bellard
        return -ENOTSUP;
1773 e57a8c0e bellard
    return s->chr_ioctl(s, cmd, arg);
1774 f8d179e3 bellard
}
1775 f8d179e3 bellard
1776 e5b0bc44 pbrook
int qemu_chr_can_read(CharDriverState *s)
1777 e5b0bc44 pbrook
{
1778 e5b0bc44 pbrook
    if (!s->chr_can_read)
1779 e5b0bc44 pbrook
        return 0;
1780 e5b0bc44 pbrook
    return s->chr_can_read(s->handler_opaque);
1781 e5b0bc44 pbrook
}
1782 e5b0bc44 pbrook
1783 e5b0bc44 pbrook
void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len)
1784 e5b0bc44 pbrook
{
1785 e5b0bc44 pbrook
    s->chr_read(s->handler_opaque, buf, len);
1786 e5b0bc44 pbrook
}
1787 e5b0bc44 pbrook
1788 bd9bdce6 balrog
void qemu_chr_accept_input(CharDriverState *s)
1789 bd9bdce6 balrog
{
1790 bd9bdce6 balrog
    if (s->chr_accept_input)
1791 bd9bdce6 balrog
        s->chr_accept_input(s);
1792 bd9bdce6 balrog
}
1793 e5b0bc44 pbrook
1794 82c643ff bellard
void qemu_chr_printf(CharDriverState *s, const char *fmt, ...)
1795 67b915a5 bellard
{
1796 82c643ff bellard
    char buf[4096];
1797 82c643ff bellard
    va_list ap;
1798 82c643ff bellard
    va_start(ap, fmt);
1799 82c643ff bellard
    vsnprintf(buf, sizeof(buf), fmt, ap);
1800 ffe8ab83 ths
    qemu_chr_write(s, (uint8_t *)buf, strlen(buf));
1801 82c643ff bellard
    va_end(ap);
1802 67b915a5 bellard
}
1803 67b915a5 bellard
1804 5905b2e5 bellard
void qemu_chr_send_event(CharDriverState *s, int event)
1805 5905b2e5 bellard
{
1806 5905b2e5 bellard
    if (s->chr_send_event)
1807 5905b2e5 bellard
        s->chr_send_event(s, event);
1808 5905b2e5 bellard
}
1809 5905b2e5 bellard
1810 5fafdf24 ths
void qemu_chr_add_handlers(CharDriverState *s,
1811 5fafdf24 ths
                           IOCanRWHandler *fd_can_read,
1812 e5b0bc44 pbrook
                           IOReadHandler *fd_read,
1813 e5b0bc44 pbrook
                           IOEventHandler *fd_event,
1814 e5b0bc44 pbrook
                           void *opaque)
1815 82c643ff bellard
{
1816 e5b0bc44 pbrook
    s->chr_can_read = fd_can_read;
1817 e5b0bc44 pbrook
    s->chr_read = fd_read;
1818 e5b0bc44 pbrook
    s->chr_event = fd_event;
1819 e5b0bc44 pbrook
    s->handler_opaque = opaque;
1820 e5b0bc44 pbrook
    if (s->chr_update_read_handler)
1821 e5b0bc44 pbrook
        s->chr_update_read_handler(s);
1822 82c643ff bellard
}
1823 3b46e624 ths
1824 82c643ff bellard
static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1825 313aa567 bellard
{
1826 82c643ff bellard
    return len;
1827 82c643ff bellard
}
1828 82c643ff bellard
1829 52f61fde ths
static CharDriverState *qemu_chr_open_null(void)
1830 82c643ff bellard
{
1831 82c643ff bellard
    CharDriverState *chr;
1832 82c643ff bellard
1833 82c643ff bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
1834 82c643ff bellard
    if (!chr)
1835 82c643ff bellard
        return NULL;
1836 82c643ff bellard
    chr->chr_write = null_chr_write;
1837 82c643ff bellard
    return chr;
1838 82c643ff bellard
}
1839 82c643ff bellard
1840 20d8a3ed ths
/* MUX driver for serial I/O splitting */
1841 20d8a3ed ths
static int term_timestamps;
1842 20d8a3ed ths
static int64_t term_timestamps_start;
1843 9c1de612 ths
#define MAX_MUX 4
1844 bd9bdce6 balrog
#define MUX_BUFFER_SIZE 32        /* Must be a power of 2.  */
1845 bd9bdce6 balrog
#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1)
1846 20d8a3ed ths
typedef struct {
1847 20d8a3ed ths
    IOCanRWHandler *chr_can_read[MAX_MUX];
1848 20d8a3ed ths
    IOReadHandler *chr_read[MAX_MUX];
1849 20d8a3ed ths
    IOEventHandler *chr_event[MAX_MUX];
1850 20d8a3ed ths
    void *ext_opaque[MAX_MUX];
1851 20d8a3ed ths
    CharDriverState *drv;
1852 bd9bdce6 balrog
    unsigned char buffer[MUX_BUFFER_SIZE];
1853 bd9bdce6 balrog
    int prod;
1854 bd9bdce6 balrog
    int cons;
1855 20d8a3ed ths
    int mux_cnt;
1856 20d8a3ed ths
    int term_got_escape;
1857 20d8a3ed ths
    int max_size;
1858 20d8a3ed ths
} MuxDriver;
1859 20d8a3ed ths
1860 20d8a3ed ths
1861 20d8a3ed ths
static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
1862 20d8a3ed ths
{
1863 20d8a3ed ths
    MuxDriver *d = chr->opaque;
1864 20d8a3ed ths
    int ret;
1865 20d8a3ed ths
    if (!term_timestamps) {
1866 20d8a3ed ths
        ret = d->drv->chr_write(d->drv, buf, len);
1867 20d8a3ed ths
    } else {
1868 20d8a3ed ths
        int i;
1869 20d8a3ed ths
1870 20d8a3ed ths
        ret = 0;
1871 20d8a3ed ths
        for(i = 0; i < len; i++) {
1872 20d8a3ed ths
            ret += d->drv->chr_write(d->drv, buf+i, 1);
1873 20d8a3ed ths
            if (buf[i] == '\n') {
1874 20d8a3ed ths
                char buf1[64];
1875 20d8a3ed ths
                int64_t ti;
1876 20d8a3ed ths
                int secs;
1877 20d8a3ed ths
1878 20d8a3ed ths
                ti = get_clock();
1879 20d8a3ed ths
                if (term_timestamps_start == -1)
1880 20d8a3ed ths
                    term_timestamps_start = ti;
1881 20d8a3ed ths
                ti -= term_timestamps_start;
1882 20d8a3ed ths
                secs = ti / 1000000000;
1883 20d8a3ed ths
                snprintf(buf1, sizeof(buf1),
1884 20d8a3ed ths
                         "[%02d:%02d:%02d.%03d] ",
1885 20d8a3ed ths
                         secs / 3600,
1886 20d8a3ed ths
                         (secs / 60) % 60,
1887 20d8a3ed ths
                         secs % 60,
1888 20d8a3ed ths
                         (int)((ti / 1000000) % 1000));
1889 ffe8ab83 ths
                d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1));
1890 20d8a3ed ths
            }
1891 20d8a3ed ths
        }
1892 20d8a3ed ths
    }
1893 20d8a3ed ths
    return ret;
1894 20d8a3ed ths
}
1895 20d8a3ed ths
1896 20d8a3ed ths
static char *mux_help[] = {
1897 20d8a3ed ths
    "% h    print this help\n\r",
1898 20d8a3ed ths
    "% x    exit emulator\n\r",
1899 20d8a3ed ths
    "% s    save disk data back to file (if -snapshot)\n\r",
1900 20d8a3ed ths
    "% t    toggle console timestamps\n\r"
1901 20d8a3ed ths
    "% b    send break (magic sysrq)\n\r",
1902 20d8a3ed ths
    "% c    switch between console and monitor\n\r",
1903 20d8a3ed ths
    "% %  sends %\n\r",
1904 20d8a3ed ths
    NULL
1905 20d8a3ed ths
};
1906 20d8a3ed ths
1907 20d8a3ed ths
static int term_escape_char = 0x01; /* ctrl-a is used for escape */
1908 20d8a3ed ths
static void mux_print_help(CharDriverState *chr)
1909 20d8a3ed ths
{
1910 20d8a3ed ths
    int i, j;
1911 20d8a3ed ths
    char ebuf[15] = "Escape-Char";
1912 20d8a3ed ths
    char cbuf[50] = "\n\r";
1913 20d8a3ed ths
1914 20d8a3ed ths
    if (term_escape_char > 0 && term_escape_char < 26) {
1915 20d8a3ed ths
        sprintf(cbuf,"\n\r");
1916 20d8a3ed ths
        sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a');
1917 20d8a3ed ths
    } else {
1918 ffe8ab83 ths
        sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
1919 ffe8ab83 ths
            term_escape_char);
1920 20d8a3ed ths
    }
1921 ffe8ab83 ths
    chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf));
1922 20d8a3ed ths
    for (i = 0; mux_help[i] != NULL; i++) {
1923 20d8a3ed ths
        for (j=0; mux_help[i][j] != '\0'; j++) {
1924 20d8a3ed ths
            if (mux_help[i][j] == '%')
1925 ffe8ab83 ths
                chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf));
1926 20d8a3ed ths
            else
1927 ffe8ab83 ths
                chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
1928 20d8a3ed ths
        }
1929 20d8a3ed ths
    }
1930 20d8a3ed ths
}
1931 20d8a3ed ths
1932 20d8a3ed ths
static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch)
1933 20d8a3ed ths
{
1934 20d8a3ed ths
    if (d->term_got_escape) {
1935 20d8a3ed ths
        d->term_got_escape = 0;
1936 20d8a3ed ths
        if (ch == term_escape_char)
1937 20d8a3ed ths
            goto send_char;
1938 20d8a3ed ths
        switch(ch) {
1939 20d8a3ed ths
        case '?':
1940 20d8a3ed ths
        case 'h':
1941 20d8a3ed ths
            mux_print_help(chr);
1942 20d8a3ed ths
            break;
1943 20d8a3ed ths
        case 'x':
1944 20d8a3ed ths
            {
1945 20d8a3ed ths
                 char *term =  "QEMU: Terminated\n\r";
1946 ffe8ab83 ths
                 chr->chr_write(chr,(uint8_t *)term,strlen(term));
1947 20d8a3ed ths
                 exit(0);
1948 20d8a3ed ths
                 break;
1949 20d8a3ed ths
            }
1950 20d8a3ed ths
        case 's':
1951 20d8a3ed ths
            {
1952 20d8a3ed ths
                int i;
1953 e4bcb14c ths
                for (i = 0; i < nb_drives; i++) {
1954 e4bcb14c ths
                        bdrv_commit(drives_table[i].bdrv);
1955 20d8a3ed ths
                }
1956 20d8a3ed ths
            }
1957 20d8a3ed ths
            break;
1958 20d8a3ed ths
        case 'b':
1959 36ddb83b balrog
            qemu_chr_event(chr, CHR_EVENT_BREAK);
1960 20d8a3ed ths
            break;
1961 20d8a3ed ths
        case 'c':
1962 20d8a3ed ths
            /* Switch to the next registered device */
1963 20d8a3ed ths
            chr->focus++;
1964 20d8a3ed ths
            if (chr->focus >= d->mux_cnt)
1965 20d8a3ed ths
                chr->focus = 0;
1966 20d8a3ed ths
            break;
1967 20d8a3ed ths
       case 't':
1968 20d8a3ed ths
           term_timestamps = !term_timestamps;
1969 20d8a3ed ths
           term_timestamps_start = -1;
1970 20d8a3ed ths
           break;
1971 20d8a3ed ths
        }
1972 20d8a3ed ths
    } else if (ch == term_escape_char) {
1973 20d8a3ed ths
        d->term_got_escape = 1;
1974 20d8a3ed ths
    } else {
1975 20d8a3ed ths
    send_char:
1976 20d8a3ed ths
        return 1;
1977 20d8a3ed ths
    }
1978 20d8a3ed ths
    return 0;
1979 20d8a3ed ths
}
1980 20d8a3ed ths
1981 bd9bdce6 balrog
static void mux_chr_accept_input(CharDriverState *chr)
1982 bd9bdce6 balrog
{
1983 bd9bdce6 balrog
    int m = chr->focus;
1984 bd9bdce6 balrog
    MuxDriver *d = chr->opaque;
1985 bd9bdce6 balrog
1986 bd9bdce6 balrog
    while (d->prod != d->cons &&
1987 bd9bdce6 balrog
           d->chr_can_read[m] &&
1988 bd9bdce6 balrog
           d->chr_can_read[m](d->ext_opaque[m])) {
1989 bd9bdce6 balrog
        d->chr_read[m](d->ext_opaque[m],
1990 bd9bdce6 balrog
                       &d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
1991 bd9bdce6 balrog
    }
1992 bd9bdce6 balrog
}
1993 bd9bdce6 balrog
1994 20d8a3ed ths
static int mux_chr_can_read(void *opaque)
1995 20d8a3ed ths
{
1996 20d8a3ed ths
    CharDriverState *chr = opaque;
1997 20d8a3ed ths
    MuxDriver *d = chr->opaque;
1998 bd9bdce6 balrog
1999 bd9bdce6 balrog
    if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
2000 bd9bdce6 balrog
        return 1;
2001 20d8a3ed ths
    if (d->chr_can_read[chr->focus])
2002 bd9bdce6 balrog
        return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
2003 20d8a3ed ths
    return 0;
2004 20d8a3ed ths
}
2005 20d8a3ed ths
2006 20d8a3ed ths
static void mux_chr_read(void *opaque, const uint8_t *buf, int size)
2007 20d8a3ed ths
{
2008 20d8a3ed ths
    CharDriverState *chr = opaque;
2009 20d8a3ed ths
    MuxDriver *d = chr->opaque;
2010 bd9bdce6 balrog
    int m = chr->focus;
2011 20d8a3ed ths
    int i;
2012 bd9bdce6 balrog
2013 bd9bdce6 balrog
    mux_chr_accept_input (opaque);
2014 bd9bdce6 balrog
2015 20d8a3ed ths
    for(i = 0; i < size; i++)
2016 bd9bdce6 balrog
        if (mux_proc_byte(chr, d, buf[i])) {
2017 bd9bdce6 balrog
            if (d->prod == d->cons &&
2018 bd9bdce6 balrog
                d->chr_can_read[m] &&
2019 bd9bdce6 balrog
                d->chr_can_read[m](d->ext_opaque[m]))
2020 bd9bdce6 balrog
                d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
2021 bd9bdce6 balrog
            else
2022 bd9bdce6 balrog
                d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i];
2023 bd9bdce6 balrog
        }
2024 20d8a3ed ths
}
2025 20d8a3ed ths
2026 20d8a3ed ths
static void mux_chr_event(void *opaque, int event)
2027 20d8a3ed ths
{
2028 20d8a3ed ths
    CharDriverState *chr = opaque;
2029 20d8a3ed ths
    MuxDriver *d = chr->opaque;
2030 20d8a3ed ths
    int i;
2031 20d8a3ed ths
2032 20d8a3ed ths
    /* Send the event to all registered listeners */
2033 20d8a3ed ths
    for (i = 0; i < d->mux_cnt; i++)
2034 20d8a3ed ths
        if (d->chr_event[i])
2035 20d8a3ed ths
            d->chr_event[i](d->ext_opaque[i], event);
2036 20d8a3ed ths
}
2037 20d8a3ed ths
2038 20d8a3ed ths
static void mux_chr_update_read_handler(CharDriverState *chr)
2039 20d8a3ed ths
{
2040 20d8a3ed ths
    MuxDriver *d = chr->opaque;
2041 20d8a3ed ths
2042 20d8a3ed ths
    if (d->mux_cnt >= MAX_MUX) {
2043 20d8a3ed ths
        fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
2044 20d8a3ed ths
        return;
2045 20d8a3ed ths
    }
2046 20d8a3ed ths
    d->ext_opaque[d->mux_cnt] = chr->handler_opaque;
2047 20d8a3ed ths
    d->chr_can_read[d->mux_cnt] = chr->chr_can_read;
2048 20d8a3ed ths
    d->chr_read[d->mux_cnt] = chr->chr_read;
2049 20d8a3ed ths
    d->chr_event[d->mux_cnt] = chr->chr_event;
2050 20d8a3ed ths
    /* Fix up the real driver with mux routines */
2051 20d8a3ed ths
    if (d->mux_cnt == 0) {
2052 20d8a3ed ths
        qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read,
2053 20d8a3ed ths
                              mux_chr_event, chr);
2054 20d8a3ed ths
    }
2055 20d8a3ed ths
    chr->focus = d->mux_cnt;
2056 20d8a3ed ths
    d->mux_cnt++;
2057 20d8a3ed ths
}
2058 20d8a3ed ths
2059 9596ebb7 pbrook
static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
2060 20d8a3ed ths
{
2061 20d8a3ed ths
    CharDriverState *chr;
2062 20d8a3ed ths
    MuxDriver *d;
2063 20d8a3ed ths
2064 20d8a3ed ths
    chr = qemu_mallocz(sizeof(CharDriverState));
2065 20d8a3ed ths
    if (!chr)
2066 20d8a3ed ths
        return NULL;
2067 20d8a3ed ths
    d = qemu_mallocz(sizeof(MuxDriver));
2068 20d8a3ed ths
    if (!d) {
2069 20d8a3ed ths
        free(chr);
2070 20d8a3ed ths
        return NULL;
2071 20d8a3ed ths
    }
2072 20d8a3ed ths
2073 20d8a3ed ths
    chr->opaque = d;
2074 20d8a3ed ths
    d->drv = drv;
2075 20d8a3ed ths
    chr->focus = -1;
2076 20d8a3ed ths
    chr->chr_write = mux_chr_write;
2077 20d8a3ed ths
    chr->chr_update_read_handler = mux_chr_update_read_handler;
2078 bd9bdce6 balrog
    chr->chr_accept_input = mux_chr_accept_input;
2079 20d8a3ed ths
    return chr;
2080 20d8a3ed ths
}
2081 20d8a3ed ths
2082 20d8a3ed ths
2083 fd1dff4b bellard
#ifdef _WIN32
2084 82c643ff bellard
2085 fd1dff4b bellard
static void socket_cleanup(void)
2086 fd1dff4b bellard
{
2087 fd1dff4b bellard
    WSACleanup();
2088 fd1dff4b bellard
}
2089 82c643ff bellard
2090 fd1dff4b bellard
static int socket_init(void)
2091 fd1dff4b bellard
{
2092 fd1dff4b bellard
    WSADATA Data;
2093 fd1dff4b bellard
    int ret, err;
2094 fd1dff4b bellard
2095 fd1dff4b bellard
    ret = WSAStartup(MAKEWORD(2,2), &Data);
2096 fd1dff4b bellard
    if (ret != 0) {
2097 fd1dff4b bellard
        err = WSAGetLastError();
2098 fd1dff4b bellard
        fprintf(stderr, "WSAStartup: %d\n", err);
2099 fd1dff4b bellard
        return -1;
2100 fd1dff4b bellard
    }
2101 fd1dff4b bellard
    atexit(socket_cleanup);
2102 fd1dff4b bellard
    return 0;
2103 fd1dff4b bellard
}
2104 fd1dff4b bellard
2105 fd1dff4b bellard
static int send_all(int fd, const uint8_t *buf, int len1)
2106 fd1dff4b bellard
{
2107 fd1dff4b bellard
    int ret, len;
2108 3b46e624 ths
2109 fd1dff4b bellard
    len = len1;
2110 fd1dff4b bellard
    while (len > 0) {
2111 fd1dff4b bellard
        ret = send(fd, buf, len, 0);
2112 fd1dff4b bellard
        if (ret < 0) {
2113 fd1dff4b bellard
            int errno;
2114 fd1dff4b bellard
            errno = WSAGetLastError();
2115 fd1dff4b bellard
            if (errno != WSAEWOULDBLOCK) {
2116 fd1dff4b bellard
                return -1;
2117 fd1dff4b bellard
            }
2118 fd1dff4b bellard
        } else if (ret == 0) {
2119 fd1dff4b bellard
            break;
2120 fd1dff4b bellard
        } else {
2121 fd1dff4b bellard
            buf += ret;
2122 fd1dff4b bellard
            len -= ret;
2123 fd1dff4b bellard
        }
2124 fd1dff4b bellard
    }
2125 fd1dff4b bellard
    return len1 - len;
2126 fd1dff4b bellard
}
2127 fd1dff4b bellard
2128 fd1dff4b bellard
void socket_set_nonblock(int fd)
2129 fd1dff4b bellard
{
2130 fd1dff4b bellard
    unsigned long opt = 1;
2131 fd1dff4b bellard
    ioctlsocket(fd, FIONBIO, &opt);
2132 fd1dff4b bellard
}
2133 fd1dff4b bellard
2134 fd1dff4b bellard
#else
2135 fd1dff4b bellard
2136 1d96905d bellard
static int unix_write(int fd, const uint8_t *buf, int len1)
2137 1d96905d bellard
{
2138 1d96905d bellard
    int ret, len;
2139 1d96905d bellard
2140 1d96905d bellard
    len = len1;
2141 1d96905d bellard
    while (len > 0) {
2142 1d96905d bellard
        ret = write(fd, buf, len);
2143 1d96905d bellard
        if (ret < 0) {
2144 1d96905d bellard
            if (errno != EINTR && errno != EAGAIN)
2145 1d96905d bellard
                return -1;
2146 1d96905d bellard
        } else if (ret == 0) {
2147 1d96905d bellard
            break;
2148 1d96905d bellard
        } else {
2149 1d96905d bellard
            buf += ret;
2150 1d96905d bellard
            len -= ret;
2151 1d96905d bellard
        }
2152 1d96905d bellard
    }
2153 1d96905d bellard
    return len1 - len;
2154 1d96905d bellard
}
2155 1d96905d bellard
2156 fd1dff4b bellard
static inline int send_all(int fd, const uint8_t *buf, int len1)
2157 fd1dff4b bellard
{
2158 fd1dff4b bellard
    return unix_write(fd, buf, len1);
2159 fd1dff4b bellard
}
2160 fd1dff4b bellard
2161 fd1dff4b bellard
void socket_set_nonblock(int fd)
2162 fd1dff4b bellard
{
2163 fd58ff9d ths
    int f;
2164 fd58ff9d ths
    f = fcntl(fd, F_GETFL);
2165 fd58ff9d ths
    fcntl(fd, F_SETFL, f | O_NONBLOCK);
2166 fd1dff4b bellard
}
2167 fd1dff4b bellard
#endif /* !_WIN32 */
2168 fd1dff4b bellard
2169 fd1dff4b bellard
#ifndef _WIN32
2170 fd1dff4b bellard
2171 fd1dff4b bellard
typedef struct {
2172 fd1dff4b bellard
    int fd_in, fd_out;
2173 fd1dff4b bellard
    int max_size;
2174 fd1dff4b bellard
} FDCharDriver;
2175 fd1dff4b bellard
2176 20d8a3ed ths
#define STDIO_MAX_CLIENTS 1
2177 20d8a3ed ths
static int stdio_nb_clients = 0;
2178 fd1dff4b bellard
2179 82c643ff bellard
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
2180 82c643ff bellard
{
2181 82c643ff bellard
    FDCharDriver *s = chr->opaque;
2182 1d96905d bellard
    return unix_write(s->fd_out, buf, len);
2183 82c643ff bellard
}
2184 82c643ff bellard
2185 7c9d8e07 bellard
static int fd_chr_read_poll(void *opaque)
2186 7c9d8e07 bellard
{
2187 7c9d8e07 bellard
    CharDriverState *chr = opaque;
2188 7c9d8e07 bellard
    FDCharDriver *s = chr->opaque;
2189 7c9d8e07 bellard
2190 e5b0bc44 pbrook
    s->max_size = qemu_chr_can_read(chr);
2191 7c9d8e07 bellard
    return s->max_size;
2192 7c9d8e07 bellard
}
2193 7c9d8e07 bellard
2194 7c9d8e07 bellard
static void fd_chr_read(void *opaque)
2195 7c9d8e07 bellard
{
2196 7c9d8e07 bellard
    CharDriverState *chr = opaque;
2197 7c9d8e07 bellard
    FDCharDriver *s = chr->opaque;
2198 7c9d8e07 bellard
    int size, len;
2199 7c9d8e07 bellard
    uint8_t buf[1024];
2200 3b46e624 ths
2201 7c9d8e07 bellard
    len = sizeof(buf);
2202 7c9d8e07 bellard
    if (len > s->max_size)
2203 7c9d8e07 bellard
        len = s->max_size;
2204 7c9d8e07 bellard
    if (len == 0)
2205 7c9d8e07 bellard
        return;
2206 7c9d8e07 bellard
    size = read(s->fd_in, buf, len);
2207 188157fe pbrook
    if (size == 0) {
2208 188157fe pbrook
        /* FD has been closed. Remove it from the active list.  */
2209 188157fe pbrook
        qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2210 188157fe pbrook
        return;
2211 188157fe pbrook
    }
2212 7c9d8e07 bellard
    if (size > 0) {
2213 e5b0bc44 pbrook
        qemu_chr_read(chr, buf, size);
2214 7c9d8e07 bellard
    }
2215 7c9d8e07 bellard
}
2216 7c9d8e07 bellard
2217 e5b0bc44 pbrook
static void fd_chr_update_read_handler(CharDriverState *chr)
2218 82c643ff bellard
{
2219 82c643ff bellard
    FDCharDriver *s = chr->opaque;
2220 82c643ff bellard
2221 f8d179e3 bellard
    if (s->fd_in >= 0) {
2222 f8d179e3 bellard
        if (nographic && s->fd_in == 0) {
2223 f8d179e3 bellard
        } else {
2224 5fafdf24 ths
            qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll,
2225 7c9d8e07 bellard
                                 fd_chr_read, NULL, chr);
2226 f8d179e3 bellard
        }
2227 82c643ff bellard
    }
2228 82c643ff bellard
}
2229 82c643ff bellard
2230 a11d070e balrog
static void fd_chr_close(struct CharDriverState *chr)
2231 a11d070e balrog
{
2232 a11d070e balrog
    FDCharDriver *s = chr->opaque;
2233 a11d070e balrog
2234 a11d070e balrog
    if (s->fd_in >= 0) {
2235 a11d070e balrog
        if (nographic && s->fd_in == 0) {
2236 a11d070e balrog
        } else {
2237 a11d070e balrog
            qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL);
2238 a11d070e balrog
        }
2239 a11d070e balrog
    }
2240 a11d070e balrog
2241 a11d070e balrog
    qemu_free(s);
2242 a11d070e balrog
}
2243 a11d070e balrog
2244 82c643ff bellard
/* open a character device to a unix fd */
2245 52f61fde ths
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out)
2246 82c643ff bellard
{
2247 82c643ff bellard
    CharDriverState *chr;
2248 82c643ff bellard
    FDCharDriver *s;
2249 82c643ff bellard
2250 82c643ff bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
2251 82c643ff bellard
    if (!chr)
2252 82c643ff bellard
        return NULL;
2253 82c643ff bellard
    s = qemu_mallocz(sizeof(FDCharDriver));
2254 82c643ff bellard
    if (!s) {
2255 82c643ff bellard
        free(chr);
2256 82c643ff bellard
        return NULL;
2257 82c643ff bellard
    }
2258 82c643ff bellard
    s->fd_in = fd_in;
2259 82c643ff bellard
    s->fd_out = fd_out;
2260 82c643ff bellard
    chr->opaque = s;
2261 82c643ff bellard
    chr->chr_write = fd_chr_write;
2262 e5b0bc44 pbrook
    chr->chr_update_read_handler = fd_chr_update_read_handler;
2263 a11d070e balrog
    chr->chr_close = fd_chr_close;
2264 86e94dea ths
2265 86e94dea ths
    qemu_chr_reset(chr);
2266 86e94dea ths
2267 82c643ff bellard
    return chr;
2268 82c643ff bellard
}
2269 82c643ff bellard
2270 52f61fde ths
static CharDriverState *qemu_chr_open_file_out(const char *file_out)
2271 f8d179e3 bellard
{
2272 f8d179e3 bellard
    int fd_out;
2273 f8d179e3 bellard
2274 aeb30be6 balrog
    TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
2275 f8d179e3 bellard
    if (fd_out < 0)
2276 f8d179e3 bellard
        return NULL;
2277 f8d179e3 bellard
    return qemu_chr_open_fd(-1, fd_out);
2278 f8d179e3 bellard
}
2279 f8d179e3 bellard
2280 52f61fde ths
static CharDriverState *qemu_chr_open_pipe(const char *filename)
2281 f8d179e3 bellard
{
2282 c26c1c4b ths
    int fd_in, fd_out;
2283 c26c1c4b ths
    char filename_in[256], filename_out[256];
2284 c26c1c4b ths
2285 c26c1c4b ths
    snprintf(filename_in, 256, "%s.in", filename);
2286 c26c1c4b ths
    snprintf(filename_out, 256, "%s.out", filename);
2287 aeb30be6 balrog
    TFR(fd_in = open(filename_in, O_RDWR | O_BINARY));
2288 aeb30be6 balrog
    TFR(fd_out = open(filename_out, O_RDWR | O_BINARY));
2289 c26c1c4b ths
    if (fd_in < 0 || fd_out < 0) {
2290 c26c1c4b ths
        if (fd_in >= 0)
2291 c26c1c4b ths
            close(fd_in);
2292 c26c1c4b ths
        if (fd_out >= 0)
2293 c26c1c4b ths
            close(fd_out);
2294 aeb30be6 balrog
        TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY));
2295 c26c1c4b ths
        if (fd_in < 0)
2296 c26c1c4b ths
            return NULL;
2297 c26c1c4b ths
    }
2298 c26c1c4b ths
    return qemu_chr_open_fd(fd_in, fd_out);
2299 f8d179e3 bellard
}
2300 f8d179e3 bellard
2301 f8d179e3 bellard
2302 82c643ff bellard
/* for STDIO, we handle the case where several clients use it
2303 82c643ff bellard
   (nographic mode) */
2304 82c643ff bellard
2305 aa0bc6b6 bellard
#define TERM_FIFO_MAX_SIZE 1
2306 aa0bc6b6 bellard
2307 aa0bc6b6 bellard
static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
2308 1dce7c3c bellard
static int term_fifo_size;
2309 330d0414 bellard
2310 7c9d8e07 bellard
static int stdio_read_poll(void *opaque)
2311 82c643ff bellard
{
2312 20d8a3ed ths
    CharDriverState *chr = opaque;
2313 aa0bc6b6 bellard
2314 20d8a3ed ths
    /* try to flush the queue if needed */
2315 20d8a3ed ths
    if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) {
2316 20d8a3ed ths
        qemu_chr_read(chr, term_fifo, 1);
2317 20d8a3ed ths
        term_fifo_size = 0;
2318 aa0bc6b6 bellard
    }
2319 20d8a3ed ths
    /* see if we can absorb more chars */
2320 20d8a3ed ths
    if (term_fifo_size == 0)
2321 20d8a3ed ths
        return 1;
2322 20d8a3ed ths
    else
2323 20d8a3ed ths
        return 0;
2324 82c643ff bellard
}
2325 82c643ff bellard
2326 7c9d8e07 bellard
static void stdio_read(void *opaque)
2327 82c643ff bellard
{
2328 7c9d8e07 bellard
    int size;
2329 7c9d8e07 bellard
    uint8_t buf[1];
2330 20d8a3ed ths
    CharDriverState *chr = opaque;
2331 20d8a3ed ths
2332 7c9d8e07 bellard
    size = read(0, buf, 1);
2333 519945df pbrook
    if (size == 0) {
2334 519945df pbrook
        /* stdin has been closed. Remove it from the active list.  */
2335 519945df pbrook
        qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2336 519945df pbrook
        return;
2337 519945df pbrook
    }
2338 20d8a3ed ths
    if (size > 0) {
2339 20d8a3ed ths
        if (qemu_chr_can_read(chr) > 0) {
2340 20d8a3ed ths
            qemu_chr_read(chr, buf, 1);
2341 20d8a3ed ths
        } else if (term_fifo_size == 0) {
2342 20d8a3ed ths
            term_fifo[term_fifo_size++] = buf[0];
2343 1dce7c3c bellard
        }
2344 1dce7c3c bellard
    }
2345 1dce7c3c bellard
}
2346 1dce7c3c bellard
2347 8d11df9e bellard
/* init terminal so that we can grab keys */
2348 8d11df9e bellard
static struct termios oldtty;
2349 8d11df9e bellard
static int old_fd0_flags;
2350 a11d070e balrog
static int term_atexit_done;
2351 8d11df9e bellard
2352 8d11df9e bellard
static void term_exit(void)
2353 8d11df9e bellard
{
2354 8d11df9e bellard
    tcsetattr (0, TCSANOW, &oldtty);
2355 8d11df9e bellard
    fcntl(0, F_SETFL, old_fd0_flags);
2356 8d11df9e bellard
}
2357 8d11df9e bellard
2358 8d11df9e bellard
static void term_init(void)
2359 8d11df9e bellard
{
2360 8d11df9e bellard
    struct termios tty;
2361 8d11df9e bellard
2362 8d11df9e bellard
    tcgetattr (0, &tty);
2363 8d11df9e bellard
    oldtty = tty;
2364 8d11df9e bellard
    old_fd0_flags = fcntl(0, F_GETFL);
2365 8d11df9e bellard
2366 8d11df9e bellard
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2367 8d11df9e bellard
                          |INLCR|IGNCR|ICRNL|IXON);
2368 8d11df9e bellard
    tty.c_oflag |= OPOST;
2369 8d11df9e bellard
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
2370 8d11df9e bellard
    /* if graphical mode, we allow Ctrl-C handling */
2371 8d11df9e bellard
    if (nographic)
2372 8d11df9e bellard
        tty.c_lflag &= ~ISIG;
2373 8d11df9e bellard
    tty.c_cflag &= ~(CSIZE|PARENB);
2374 8d11df9e bellard
    tty.c_cflag |= CS8;
2375 8d11df9e bellard
    tty.c_cc[VMIN] = 1;
2376 8d11df9e bellard
    tty.c_cc[VTIME] = 0;
2377 3b46e624 ths
2378 8d11df9e bellard
    tcsetattr (0, TCSANOW, &tty);
2379 8d11df9e bellard
2380 a11d070e balrog
    if (!term_atexit_done++)
2381 a11d070e balrog
        atexit(term_exit);
2382 8d11df9e bellard
2383 8d11df9e bellard
    fcntl(0, F_SETFL, O_NONBLOCK);
2384 8d11df9e bellard
}
2385 8d11df9e bellard
2386 a11d070e balrog
static void qemu_chr_close_stdio(struct CharDriverState *chr)
2387 a11d070e balrog
{
2388 a11d070e balrog
    term_exit();
2389 a11d070e balrog
    stdio_nb_clients--;
2390 a11d070e balrog
    qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL);
2391 a11d070e balrog
    fd_chr_close(chr);
2392 a11d070e balrog
}
2393 a11d070e balrog
2394 52f61fde ths
static CharDriverState *qemu_chr_open_stdio(void)
2395 82c643ff bellard
{
2396 82c643ff bellard
    CharDriverState *chr;
2397 82c643ff bellard
2398 20d8a3ed ths
    if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
2399 20d8a3ed ths
        return NULL;
2400 20d8a3ed ths
    chr = qemu_chr_open_fd(0, 1);
2401 a11d070e balrog
    chr->chr_close = qemu_chr_close_stdio;
2402 20d8a3ed ths
    qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr);
2403 20d8a3ed ths
    stdio_nb_clients++;
2404 20d8a3ed ths
    term_init();
2405 20d8a3ed ths
2406 82c643ff bellard
    return chr;
2407 82c643ff bellard
}
2408 82c643ff bellard
2409 64b7b733 aurel32
#ifdef __sun__
2410 64b7b733 aurel32
/* Once Solaris has openpty(), this is going to be removed. */
2411 64b7b733 aurel32
int openpty(int *amaster, int *aslave, char *name,
2412 64b7b733 aurel32
            struct termios *termp, struct winsize *winp)
2413 64b7b733 aurel32
{
2414 64b7b733 aurel32
        const char *slave;
2415 64b7b733 aurel32
        int mfd = -1, sfd = -1;
2416 64b7b733 aurel32
2417 64b7b733 aurel32
        *amaster = *aslave = -1;
2418 64b7b733 aurel32
2419 64b7b733 aurel32
        mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
2420 64b7b733 aurel32
        if (mfd < 0)
2421 64b7b733 aurel32
                goto err;
2422 64b7b733 aurel32
2423 64b7b733 aurel32
        if (grantpt(mfd) == -1 || unlockpt(mfd) == -1)
2424 64b7b733 aurel32
                goto err;
2425 64b7b733 aurel32
2426 64b7b733 aurel32
        if ((slave = ptsname(mfd)) == NULL)
2427 64b7b733 aurel32
                goto err;
2428 64b7b733 aurel32
2429 64b7b733 aurel32
        if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1)
2430 64b7b733 aurel32
                goto err;
2431 64b7b733 aurel32
2432 64b7b733 aurel32
        if (ioctl(sfd, I_PUSH, "ptem") == -1 ||
2433 64b7b733 aurel32
            (termp != NULL && tcgetattr(sfd, termp) < 0))
2434 64b7b733 aurel32
                goto err;
2435 64b7b733 aurel32
2436 64b7b733 aurel32
        if (amaster)
2437 64b7b733 aurel32
                *amaster = mfd;
2438 64b7b733 aurel32
        if (aslave)
2439 64b7b733 aurel32
                *aslave = sfd;
2440 64b7b733 aurel32
        if (winp)
2441 64b7b733 aurel32
                ioctl(sfd, TIOCSWINSZ, winp);
2442 64b7b733 aurel32
2443 64b7b733 aurel32
        return 0;
2444 64b7b733 aurel32
2445 64b7b733 aurel32
err:
2446 64b7b733 aurel32
        if (sfd != -1)
2447 64b7b733 aurel32
                close(sfd);
2448 64b7b733 aurel32
        close(mfd);
2449 64b7b733 aurel32
        return -1;
2450 64b7b733 aurel32
}
2451 64b7b733 aurel32
2452 64b7b733 aurel32
void cfmakeraw (struct termios *termios_p)
2453 64b7b733 aurel32
{
2454 64b7b733 aurel32
        termios_p->c_iflag &=
2455 64b7b733 aurel32
                ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
2456 64b7b733 aurel32
        termios_p->c_oflag &= ~OPOST;
2457 64b7b733 aurel32
        termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
2458 64b7b733 aurel32
        termios_p->c_cflag &= ~(CSIZE|PARENB);
2459 64b7b733 aurel32
        termios_p->c_cflag |= CS8;
2460 64b7b733 aurel32
2461 64b7b733 aurel32
        termios_p->c_cc[VMIN] = 0;
2462 64b7b733 aurel32
        termios_p->c_cc[VTIME] = 0;
2463 64b7b733 aurel32
}
2464 64b7b733 aurel32
#endif
2465 64b7b733 aurel32
2466 aec62507 ths
#if defined(__linux__) || defined(__sun__)
2467 52f61fde ths
static CharDriverState *qemu_chr_open_pty(void)
2468 82c643ff bellard
{
2469 91fc2119 bellard
    struct termios tty;
2470 82c643ff bellard
    int master_fd, slave_fd;
2471 3b46e624 ths
2472 64b7b733 aurel32
    if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) {
2473 82c643ff bellard
        return NULL;
2474 82c643ff bellard
    }
2475 3b46e624 ths
2476 64b7b733 aurel32
    /* Set raw attributes on the pty. */
2477 64b7b733 aurel32
    cfmakeraw(&tty);
2478 64b7b733 aurel32
    tcsetattr(slave_fd, TCSAFLUSH, &tty);
2479 91fc2119 bellard
2480 64b7b733 aurel32
    fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
2481 82c643ff bellard
    return qemu_chr_open_fd(master_fd, master_fd);
2482 82c643ff bellard
}
2483 f8d179e3 bellard
2484 5fafdf24 ths
static void tty_serial_init(int fd, int speed,
2485 f8d179e3 bellard
                            int parity, int data_bits, int stop_bits)
2486 f8d179e3 bellard
{
2487 f8d179e3 bellard
    struct termios tty;
2488 f8d179e3 bellard
    speed_t spd;
2489 f8d179e3 bellard
2490 e57a8c0e bellard
#if 0
2491 5fafdf24 ths
    printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
2492 e57a8c0e bellard
           speed, parity, data_bits, stop_bits);
2493 e57a8c0e bellard
#endif
2494 e57a8c0e bellard
    tcgetattr (fd, &tty);
2495 f8d179e3 bellard
2496 a7954218 balrog
#define MARGIN 1.1
2497 a7954218 balrog
    if (speed <= 50 * MARGIN)
2498 f8d179e3 bellard
        spd = B50;
2499 a7954218 balrog
    else if (speed <= 75 * MARGIN)
2500 f8d179e3 bellard
        spd = B75;
2501 a7954218 balrog
    else if (speed <= 300 * MARGIN)
2502 f8d179e3 bellard
        spd = B300;
2503 a7954218 balrog
    else if (speed <= 600 * MARGIN)
2504 f8d179e3 bellard
        spd = B600;
2505 a7954218 balrog
    else if (speed <= 1200 * MARGIN)
2506 f8d179e3 bellard
        spd = B1200;
2507 a7954218 balrog
    else if (speed <= 2400 * MARGIN)
2508 f8d179e3 bellard
        spd = B2400;
2509 a7954218 balrog
    else if (speed <= 4800 * MARGIN)
2510 f8d179e3 bellard
        spd = B4800;
2511 a7954218 balrog
    else if (speed <= 9600 * MARGIN)
2512 f8d179e3 bellard
        spd = B9600;
2513 a7954218 balrog
    else if (speed <= 19200 * MARGIN)
2514 f8d179e3 bellard
        spd = B19200;
2515 a7954218 balrog
    else if (speed <= 38400 * MARGIN)
2516 f8d179e3 bellard
        spd = B38400;
2517 a7954218 balrog
    else if (speed <= 57600 * MARGIN)
2518 f8d179e3 bellard
        spd = B57600;
2519 a7954218 balrog
    else if (speed <= 115200 * MARGIN)
2520 a7954218 balrog
        spd = B115200;
2521 a7954218 balrog
    else
2522 f8d179e3 bellard
        spd = B115200;
2523 f8d179e3 bellard
2524 f8d179e3 bellard
    cfsetispeed(&tty, spd);
2525 f8d179e3 bellard
    cfsetospeed(&tty, spd);
2526 f8d179e3 bellard
2527 f8d179e3 bellard
    tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
2528 f8d179e3 bellard
                          |INLCR|IGNCR|ICRNL|IXON);
2529 f8d179e3 bellard
    tty.c_oflag |= OPOST;
2530 f8d179e3 bellard
    tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG);
2531 094eed6c bellard
    tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB);
2532 f8d179e3 bellard
    switch(data_bits) {
2533 f8d179e3 bellard
    default:
2534 f8d179e3 bellard
    case 8:
2535 f8d179e3 bellard
        tty.c_cflag |= CS8;
2536 f8d179e3 bellard
        break;
2537 f8d179e3 bellard
    case 7:
2538 f8d179e3 bellard
        tty.c_cflag |= CS7;
2539 f8d179e3 bellard
        break;
2540 f8d179e3 bellard
    case 6:
2541 f8d179e3 bellard
        tty.c_cflag |= CS6;
2542 f8d179e3 bellard
        break;
2543 f8d179e3 bellard
    case 5:
2544 f8d179e3 bellard
        tty.c_cflag |= CS5;
2545 f8d179e3 bellard
        break;
2546 f8d179e3 bellard
    }
2547 f8d179e3 bellard
    switch(parity) {
2548 f8d179e3 bellard
    default:
2549 f8d179e3 bellard
    case 'N':
2550 f8d179e3 bellard
        break;
2551 f8d179e3 bellard
    case 'E':
2552 f8d179e3 bellard
        tty.c_cflag |= PARENB;
2553 f8d179e3 bellard
        break;
2554 f8d179e3 bellard
    case 'O':
2555 f8d179e3 bellard
        tty.c_cflag |= PARENB | PARODD;
2556 f8d179e3 bellard
        break;
2557 f8d179e3 bellard
    }
2558 094eed6c bellard
    if (stop_bits == 2)
2559 094eed6c bellard
        tty.c_cflag |= CSTOPB;
2560 3b46e624 ths
2561 f8d179e3 bellard
    tcsetattr (fd, TCSANOW, &tty);
2562 f8d179e3 bellard
}
2563 f8d179e3 bellard
2564 e57a8c0e bellard
static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg)
2565 f8d179e3 bellard
{
2566 f8d179e3 bellard
    FDCharDriver *s = chr->opaque;
2567 3b46e624 ths
2568 e57a8c0e bellard
    switch(cmd) {
2569 e57a8c0e bellard
    case CHR_IOCTL_SERIAL_SET_PARAMS:
2570 e57a8c0e bellard
        {
2571 e57a8c0e bellard
            QEMUSerialSetParams *ssp = arg;
2572 5fafdf24 ths
            tty_serial_init(s->fd_in, ssp->speed, ssp->parity,
2573 e57a8c0e bellard
                            ssp->data_bits, ssp->stop_bits);
2574 e57a8c0e bellard
        }
2575 e57a8c0e bellard
        break;
2576 e57a8c0e bellard
    case CHR_IOCTL_SERIAL_SET_BREAK:
2577 e57a8c0e bellard
        {
2578 e57a8c0e bellard
            int enable = *(int *)arg;
2579 e57a8c0e bellard
            if (enable)
2580 e57a8c0e bellard
                tcsendbreak(s->fd_in, 1);
2581 e57a8c0e bellard
        }
2582 e57a8c0e bellard
        break;
2583 e57a8c0e bellard
    default:
2584 e57a8c0e bellard
        return -ENOTSUP;
2585 e57a8c0e bellard
    }
2586 e57a8c0e bellard
    return 0;
2587 f8d179e3 bellard
}
2588 f8d179e3 bellard
2589 52f61fde ths
static CharDriverState *qemu_chr_open_tty(const char *filename)
2590 f8d179e3 bellard
{
2591 f8d179e3 bellard
    CharDriverState *chr;
2592 f8d179e3 bellard
    int fd;
2593 f8d179e3 bellard
2594 aeb30be6 balrog
    TFR(fd = open(filename, O_RDWR | O_NONBLOCK));
2595 f8d179e3 bellard
    tty_serial_init(fd, 115200, 'N', 8, 1);
2596 f8d179e3 bellard
    chr = qemu_chr_open_fd(fd, fd);
2597 aeb30be6 balrog
    if (!chr) {
2598 aeb30be6 balrog
        close(fd);
2599 f8d179e3 bellard
        return NULL;
2600 aeb30be6 balrog
    }
2601 e57a8c0e bellard
    chr->chr_ioctl = tty_serial_ioctl;
2602 86e94dea ths
    qemu_chr_reset(chr);
2603 e57a8c0e bellard
    return chr;
2604 e57a8c0e bellard
}
2605 aec62507 ths
#else  /* ! __linux__ && ! __sun__ */
2606 aec62507 ths
static CharDriverState *qemu_chr_open_pty(void)
2607 aec62507 ths
{
2608 aec62507 ths
    return NULL;
2609 aec62507 ths
}
2610 aec62507 ths
#endif /* __linux__ || __sun__ */
2611 e57a8c0e bellard
2612 aec62507 ths
#if defined(__linux__)
2613 5867c88a ths
typedef struct {
2614 5867c88a ths
    int fd;
2615 5867c88a ths
    int mode;
2616 5867c88a ths
} ParallelCharDriver;
2617 5867c88a ths
2618 5867c88a ths
static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode)
2619 5867c88a ths
{
2620 5867c88a ths
    if (s->mode != mode) {
2621 5867c88a ths
        int m = mode;
2622 5867c88a ths
        if (ioctl(s->fd, PPSETMODE, &m) < 0)
2623 5867c88a ths
            return 0;
2624 5867c88a ths
        s->mode = mode;
2625 5867c88a ths
    }
2626 5867c88a ths
    return 1;
2627 5867c88a ths
}
2628 5867c88a ths
2629 e57a8c0e bellard
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg)
2630 e57a8c0e bellard
{
2631 5867c88a ths
    ParallelCharDriver *drv = chr->opaque;
2632 5867c88a ths
    int fd = drv->fd;
2633 e57a8c0e bellard
    uint8_t b;
2634 e57a8c0e bellard
2635 e57a8c0e bellard
    switch(cmd) {
2636 e57a8c0e bellard
    case CHR_IOCTL_PP_READ_DATA:
2637 e57a8c0e bellard
        if (ioctl(fd, PPRDATA, &b) < 0)
2638 e57a8c0e bellard
            return -ENOTSUP;
2639 e57a8c0e bellard
        *(uint8_t *)arg = b;
2640 e57a8c0e bellard
        break;
2641 e57a8c0e bellard
    case CHR_IOCTL_PP_WRITE_DATA:
2642 e57a8c0e bellard
        b = *(uint8_t *)arg;
2643 e57a8c0e bellard
        if (ioctl(fd, PPWDATA, &b) < 0)
2644 e57a8c0e bellard
            return -ENOTSUP;
2645 e57a8c0e bellard
        break;
2646 e57a8c0e bellard
    case CHR_IOCTL_PP_READ_CONTROL:
2647 e57a8c0e bellard
        if (ioctl(fd, PPRCONTROL, &b) < 0)
2648 e57a8c0e bellard
            return -ENOTSUP;
2649 5867c88a ths
        /* Linux gives only the lowest bits, and no way to know data
2650 5867c88a ths
           direction! For better compatibility set the fixed upper
2651 5867c88a ths
           bits. */
2652 5867c88a ths
        *(uint8_t *)arg = b | 0xc0;
2653 e57a8c0e bellard
        break;
2654 e57a8c0e bellard
    case CHR_IOCTL_PP_WRITE_CONTROL:
2655 e57a8c0e bellard
        b = *(uint8_t *)arg;
2656 e57a8c0e bellard
        if (ioctl(fd, PPWCONTROL, &b) < 0)
2657 e57a8c0e bellard
            return -ENOTSUP;
2658 e57a8c0e bellard
        break;
2659 e57a8c0e bellard
    case CHR_IOCTL_PP_READ_STATUS:
2660 e57a8c0e bellard
        if (ioctl(fd, PPRSTATUS, &b) < 0)
2661 e57a8c0e bellard
            return -ENOTSUP;
2662 e57a8c0e bellard
        *(uint8_t *)arg = b;
2663 e57a8c0e bellard
        break;
2664 5867c88a ths
    case CHR_IOCTL_PP_EPP_READ_ADDR:
2665 5867c88a ths
        if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2666 5867c88a ths
            struct ParallelIOArg *parg = arg;
2667 5867c88a ths
            int n = read(fd, parg->buffer, parg->count);
2668 5867c88a ths
            if (n != parg->count) {
2669 5867c88a ths
                return -EIO;
2670 5867c88a ths
            }
2671 5867c88a ths
        }
2672 5867c88a ths
        break;
2673 5867c88a ths
    case CHR_IOCTL_PP_EPP_READ:
2674 5867c88a ths
        if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2675 5867c88a ths
            struct ParallelIOArg *parg = arg;
2676 5867c88a ths
            int n = read(fd, parg->buffer, parg->count);
2677 5867c88a ths
            if (n != parg->count) {
2678 5867c88a ths
                return -EIO;
2679 5867c88a ths
            }
2680 5867c88a ths
        }
2681 5867c88a ths
        break;
2682 5867c88a ths
    case CHR_IOCTL_PP_EPP_WRITE_ADDR:
2683 5867c88a ths
        if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
2684 5867c88a ths
            struct ParallelIOArg *parg = arg;
2685 5867c88a ths
            int n = write(fd, parg->buffer, parg->count);
2686 5867c88a ths
            if (n != parg->count) {
2687 5867c88a ths
                return -EIO;
2688 5867c88a ths
            }
2689 5867c88a ths
        }
2690 5867c88a ths
        break;
2691 5867c88a ths
    case CHR_IOCTL_PP_EPP_WRITE:
2692 5867c88a ths
        if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
2693 5867c88a ths
            struct ParallelIOArg *parg = arg;
2694 5867c88a ths
            int n = write(fd, parg->buffer, parg->count);
2695 5867c88a ths
            if (n != parg->count) {
2696 5867c88a ths
                return -EIO;
2697 5867c88a ths
            }
2698 5867c88a ths
        }
2699 5867c88a ths
        break;
2700 e57a8c0e bellard
    default:
2701 e57a8c0e bellard
        return -ENOTSUP;
2702 e57a8c0e bellard
    }
2703 e57a8c0e bellard
    return 0;
2704 e57a8c0e bellard
}
2705 e57a8c0e bellard
2706 5867c88a ths
static void pp_close(CharDriverState *chr)
2707 5867c88a ths
{
2708 5867c88a ths
    ParallelCharDriver *drv = chr->opaque;
2709 5867c88a ths
    int fd = drv->fd;
2710 5867c88a ths
2711 5867c88a ths
    pp_hw_mode(drv, IEEE1284_MODE_COMPAT);
2712 5867c88a ths
    ioctl(fd, PPRELEASE);
2713 5867c88a ths
    close(fd);
2714 5867c88a ths
    qemu_free(drv);
2715 5867c88a ths
}
2716 5867c88a ths
2717 52f61fde ths
static CharDriverState *qemu_chr_open_pp(const char *filename)
2718 e57a8c0e bellard
{
2719 e57a8c0e bellard
    CharDriverState *chr;
2720 5867c88a ths
    ParallelCharDriver *drv;
2721 e57a8c0e bellard
    int fd;
2722 e57a8c0e bellard
2723 aeb30be6 balrog
    TFR(fd = open(filename, O_RDWR));
2724 e57a8c0e bellard
    if (fd < 0)
2725 e57a8c0e bellard
        return NULL;
2726 e57a8c0e bellard
2727 e57a8c0e bellard
    if (ioctl(fd, PPCLAIM) < 0) {
2728 e57a8c0e bellard
        close(fd);
2729 e57a8c0e bellard
        return NULL;
2730 e57a8c0e bellard
    }
2731 e57a8c0e bellard
2732 5867c88a ths
    drv = qemu_mallocz(sizeof(ParallelCharDriver));
2733 5867c88a ths
    if (!drv) {
2734 5867c88a ths
        close(fd);
2735 5867c88a ths
        return NULL;
2736 5867c88a ths
    }
2737 5867c88a ths
    drv->fd = fd;
2738 5867c88a ths
    drv->mode = IEEE1284_MODE_COMPAT;
2739 5867c88a ths
2740 e57a8c0e bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
2741 e57a8c0e bellard
    if (!chr) {
2742 5867c88a ths
        qemu_free(drv);
2743 e57a8c0e bellard
        close(fd);
2744 e57a8c0e bellard
        return NULL;
2745 e57a8c0e bellard
    }
2746 e57a8c0e bellard
    chr->chr_write = null_chr_write;
2747 e57a8c0e bellard
    chr->chr_ioctl = pp_ioctl;
2748 5867c88a ths
    chr->chr_close = pp_close;
2749 5867c88a ths
    chr->opaque = drv;
2750 86e94dea ths
2751 86e94dea ths
    qemu_chr_reset(chr);
2752 86e94dea ths
2753 f8d179e3 bellard
    return chr;
2754 f8d179e3 bellard
}
2755 aec62507 ths
#endif /* __linux__ */
2756 f8d179e3 bellard
2757 aec62507 ths
#else /* _WIN32 */
2758 82c643ff bellard
2759 f331110f bellard
typedef struct {
2760 f331110f bellard
    int max_size;
2761 f331110f bellard
    HANDLE hcom, hrecv, hsend;
2762 f331110f bellard
    OVERLAPPED orecv, osend;
2763 f331110f bellard
    BOOL fpipe;
2764 f331110f bellard
    DWORD len;
2765 f331110f bellard
} WinCharState;
2766 f331110f bellard
2767 f331110f bellard
#define NSENDBUF 2048
2768 f331110f bellard
#define NRECVBUF 2048
2769 f331110f bellard
#define MAXCONNECT 1
2770 f331110f bellard
#define NTIMEOUT 5000
2771 f331110f bellard
2772 f331110f bellard
static int win_chr_poll(void *opaque);
2773 f331110f bellard
static int win_chr_pipe_poll(void *opaque);
2774 f331110f bellard
2775 087f4ae0 ths
static void win_chr_close(CharDriverState *chr)
2776 f331110f bellard
{
2777 087f4ae0 ths
    WinCharState *s = chr->opaque;
2778 087f4ae0 ths
2779 f331110f bellard
    if (s->hsend) {
2780 f331110f bellard
        CloseHandle(s->hsend);
2781 f331110f bellard
        s->hsend = NULL;
2782 f331110f bellard
    }
2783 f331110f bellard
    if (s->hrecv) {
2784 f331110f bellard
        CloseHandle(s->hrecv);
2785 f331110f bellard
        s->hrecv = NULL;
2786 f331110f bellard
    }
2787 f331110f bellard
    if (s->hcom) {
2788 f331110f bellard
        CloseHandle(s->hcom);
2789 f331110f bellard
        s->hcom = NULL;
2790 f331110f bellard
    }
2791 f331110f bellard
    if (s->fpipe)
2792 087f4ae0 ths
        qemu_del_polling_cb(win_chr_pipe_poll, chr);
2793 f331110f bellard
    else
2794 087f4ae0 ths
        qemu_del_polling_cb(win_chr_poll, chr);
2795 f331110f bellard
}
2796 f331110f bellard
2797 087f4ae0 ths
static int win_chr_init(CharDriverState *chr, const char *filename)
2798 f331110f bellard
{
2799 f331110f bellard
    WinCharState *s = chr->opaque;
2800 f331110f bellard
    COMMCONFIG comcfg;
2801 f331110f bellard
    COMMTIMEOUTS cto = { 0, 0, 0, 0, 0};
2802 f331110f bellard
    COMSTAT comstat;
2803 f331110f bellard
    DWORD size;
2804 f331110f bellard
    DWORD err;
2805 3b46e624 ths
2806 f331110f bellard
    s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
2807 f331110f bellard
    if (!s->hsend) {
2808 f331110f bellard
        fprintf(stderr, "Failed CreateEvent\n");
2809 f331110f bellard
        goto fail;
2810 f331110f bellard
    }
2811 f331110f bellard
    s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
2812 f331110f bellard
    if (!s->hrecv) {
2813 f331110f bellard
        fprintf(stderr, "Failed CreateEvent\n");
2814 f331110f bellard
        goto fail;
2815 f331110f bellard
    }
2816 f331110f bellard
2817 f331110f bellard
    s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL,
2818 f331110f bellard
                      OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
2819 f331110f bellard
    if (s->hcom == INVALID_HANDLE_VALUE) {
2820 f331110f bellard
        fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
2821 f331110f bellard
        s->hcom = NULL;
2822 f331110f bellard
        goto fail;
2823 f331110f bellard
    }
2824 3b46e624 ths
2825 f331110f bellard
    if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
2826 f331110f bellard
        fprintf(stderr, "Failed SetupComm\n");
2827 f331110f bellard
        goto fail;
2828 f331110f bellard
    }
2829 3b46e624 ths
2830 f331110f bellard
    ZeroMemory(&comcfg, sizeof(COMMCONFIG));
2831 f331110f bellard
    size = sizeof(COMMCONFIG);
2832 f331110f bellard
    GetDefaultCommConfig(filename, &comcfg, &size);
2833 f331110f bellard
    comcfg.dcb.DCBlength = sizeof(DCB);
2834 f331110f bellard
    CommConfigDialog(filename, NULL, &comcfg);
2835 f331110f bellard
2836 f331110f bellard
    if (!SetCommState(s->hcom, &comcfg.dcb)) {
2837 f331110f bellard
        fprintf(stderr, "Failed SetCommState\n");
2838 f331110f bellard
        goto fail;
2839 f331110f bellard
    }
2840 f331110f bellard
2841 f331110f bellard
    if (!SetCommMask(s->hcom, EV_ERR)) {
2842 f331110f bellard
        fprintf(stderr, "Failed SetCommMask\n");
2843 f331110f bellard
        goto fail;
2844 f331110f bellard
    }
2845 f331110f bellard
2846 f331110f bellard
    cto.ReadIntervalTimeout = MAXDWORD;
2847 f331110f bellard
    if (!SetCommTimeouts(s->hcom, &cto)) {
2848 f331110f bellard
        fprintf(stderr, "Failed SetCommTimeouts\n");
2849 f331110f bellard
        goto fail;
2850 f331110f bellard
    }
2851 3b46e624 ths
2852 f331110f bellard
    if (!ClearCommError(s->hcom, &err, &comstat)) {
2853 f331110f bellard
        fprintf(stderr, "Failed ClearCommError\n");
2854 f331110f bellard
        goto fail;
2855 f331110f bellard
    }
2856 087f4ae0 ths
    qemu_add_polling_cb(win_chr_poll, chr);
2857 f331110f bellard
    return 0;
2858 f331110f bellard
2859 f331110f bellard
 fail:
2860 087f4ae0 ths
    win_chr_close(chr);
2861 f331110f bellard
    return -1;
2862 f331110f bellard
}
2863 f331110f bellard
2864 f331110f bellard
static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1)
2865 f331110f bellard
{
2866 f331110f bellard
    WinCharState *s = chr->opaque;
2867 f331110f bellard
    DWORD len, ret, size, err;
2868 f331110f bellard
2869 f331110f bellard
    len = len1;
2870 f331110f bellard
    ZeroMemory(&s->osend, sizeof(s->osend));
2871 f331110f bellard
    s->osend.hEvent = s->hsend;
2872 f331110f bellard
    while (len > 0) {
2873 f331110f bellard
        if (s->hsend)
2874 f331110f bellard
            ret = WriteFile(s->hcom, buf, len, &size, &s->osend);
2875 f331110f bellard
        else
2876 f331110f bellard
            ret = WriteFile(s->hcom, buf, len, &size, NULL);
2877 f331110f bellard
        if (!ret) {
2878 f331110f bellard
            err = GetLastError();
2879 f331110f bellard
            if (err == ERROR_IO_PENDING) {
2880 f331110f bellard
                ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE);
2881 f331110f bellard
                if (ret) {
2882 f331110f bellard
                    buf += size;
2883 f331110f bellard
                    len -= size;
2884 f331110f bellard
                } else {
2885 f331110f bellard
                    break;
2886 f331110f bellard
                }
2887 f331110f bellard
            } else {
2888 f331110f bellard
                break;
2889 f331110f bellard
            }
2890 f331110f bellard
        } else {
2891 f331110f bellard
            buf += size;
2892 f331110f bellard
            len -= size;
2893 f331110f bellard
        }
2894 f331110f bellard
    }
2895 f331110f bellard
    return len1 - len;
2896 f331110f bellard
}
2897 f331110f bellard
2898 087f4ae0 ths
static int win_chr_read_poll(CharDriverState *chr)
2899 f331110f bellard
{
2900 087f4ae0 ths
    WinCharState *s = chr->opaque;
2901 087f4ae0 ths
2902 087f4ae0 ths
    s->max_size = qemu_chr_can_read(chr);
2903 f331110f bellard
    return s->max_size;
2904 f331110f bellard
}
2905 e5b0bc44 pbrook
2906 087f4ae0 ths
static void win_chr_readfile(CharDriverState *chr)
2907 f331110f bellard
{
2908 087f4ae0 ths
    WinCharState *s = chr->opaque;
2909 f331110f bellard
    int ret, err;
2910 f331110f bellard
    uint8_t buf[1024];
2911 f331110f bellard
    DWORD size;
2912 3b46e624 ths
2913 f331110f bellard
    ZeroMemory(&s->orecv, sizeof(s->orecv));
2914 f331110f bellard
    s->orecv.hEvent = s->hrecv;
2915 f331110f bellard
    ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv);
2916 f331110f bellard
    if (!ret) {
2917 f331110f bellard
        err = GetLastError();
2918 f331110f bellard
        if (err == ERROR_IO_PENDING) {
2919 f331110f bellard
            ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE);
2920 f331110f bellard
        }
2921 f331110f bellard
    }
2922 f331110f bellard
2923 f331110f bellard
    if (size > 0) {
2924 087f4ae0 ths
        qemu_chr_read(chr, buf, size);
2925 f331110f bellard
    }
2926 f331110f bellard
}
2927 f331110f bellard
2928 087f4ae0 ths
static void win_chr_read(CharDriverState *chr)
2929 f331110f bellard
{
2930 087f4ae0 ths
    WinCharState *s = chr->opaque;
2931 087f4ae0 ths
2932 f331110f bellard
    if (s->len > s->max_size)
2933 f331110f bellard
        s->len = s->max_size;
2934 f331110f bellard
    if (s->len == 0)
2935 f331110f bellard
        return;
2936 3b46e624 ths
2937 087f4ae0 ths
    win_chr_readfile(chr);
2938 f331110f bellard
}
2939 f331110f bellard
2940 f331110f bellard
static int win_chr_poll(void *opaque)
2941 f331110f bellard
{
2942 087f4ae0 ths
    CharDriverState *chr = opaque;
2943 087f4ae0 ths
    WinCharState *s = chr->opaque;
2944 f331110f bellard
    COMSTAT status;
2945 f331110f bellard
    DWORD comerr;
2946 3b46e624 ths
2947 f331110f bellard
    ClearCommError(s->hcom, &comerr, &status);
2948 f331110f bellard
    if (status.cbInQue > 0) {
2949 f331110f bellard
        s->len = status.cbInQue;
2950 087f4ae0 ths
        win_chr_read_poll(chr);
2951 087f4ae0 ths
        win_chr_read(chr);
2952 f331110f bellard
        return 1;
2953 f331110f bellard
    }
2954 f331110f bellard
    return 0;
2955 f331110f bellard
}
2956 f331110f bellard
2957 52f61fde ths
static CharDriverState *qemu_chr_open_win(const char *filename)
2958 f331110f bellard
{
2959 f331110f bellard
    CharDriverState *chr;
2960 f331110f bellard
    WinCharState *s;
2961 3b46e624 ths
2962 f331110f bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
2963 f331110f bellard
    if (!chr)
2964 f331110f bellard
        return NULL;
2965 f331110f bellard
    s = qemu_mallocz(sizeof(WinCharState));
2966 f331110f bellard
    if (!s) {
2967 f331110f bellard
        free(chr);
2968 f331110f bellard
        return NULL;
2969 f331110f bellard
    }
2970 f331110f bellard
    chr->opaque = s;
2971 f331110f bellard
    chr->chr_write = win_chr_write;
2972 f331110f bellard
    chr->chr_close = win_chr_close;
2973 f331110f bellard
2974 087f4ae0 ths
    if (win_chr_init(chr, filename) < 0) {
2975 f331110f bellard
        free(s);
2976 f331110f bellard
        free(chr);
2977 f331110f bellard
        return NULL;
2978 f331110f bellard
    }
2979 86e94dea ths
    qemu_chr_reset(chr);
2980 f331110f bellard
    return chr;
2981 f331110f bellard
}
2982 f331110f bellard
2983 f331110f bellard
static int win_chr_pipe_poll(void *opaque)
2984 f331110f bellard
{
2985 087f4ae0 ths
    CharDriverState *chr = opaque;
2986 087f4ae0 ths
    WinCharState *s = chr->opaque;
2987 f331110f bellard
    DWORD size;
2988 f331110f bellard
2989 f331110f bellard
    PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL);
2990 f331110f bellard
    if (size > 0) {
2991 f331110f bellard
        s->len = size;
2992 087f4ae0 ths
        win_chr_read_poll(chr);
2993 087f4ae0 ths
        win_chr_read(chr);
2994 f331110f bellard
        return 1;
2995 f331110f bellard
    }
2996 f331110f bellard
    return 0;
2997 f331110f bellard
}
2998 f331110f bellard
2999 087f4ae0 ths
static int win_chr_pipe_init(CharDriverState *chr, const char *filename)
3000 f331110f bellard
{
3001 087f4ae0 ths
    WinCharState *s = chr->opaque;
3002 f331110f bellard
    OVERLAPPED ov;
3003 f331110f bellard
    int ret;
3004 f331110f bellard
    DWORD size;
3005 f331110f bellard
    char openname[256];
3006 3b46e624 ths
3007 f331110f bellard
    s->fpipe = TRUE;
3008 f331110f bellard
3009 f331110f bellard
    s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL);
3010 f331110f bellard
    if (!s->hsend) {
3011 f331110f bellard
        fprintf(stderr, "Failed CreateEvent\n");
3012 f331110f bellard
        goto fail;
3013 f331110f bellard
    }
3014 f331110f bellard
    s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL);
3015 f331110f bellard
    if (!s->hrecv) {
3016 f331110f bellard
        fprintf(stderr, "Failed CreateEvent\n");
3017 f331110f bellard
        goto fail;
3018 f331110f bellard
    }
3019 3b46e624 ths
3020 f331110f bellard
    snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename);
3021 f331110f bellard
    s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
3022 f331110f bellard
                              PIPE_TYPE_BYTE | PIPE_READMODE_BYTE |
3023 f331110f bellard
                              PIPE_WAIT,
3024 f331110f bellard
                              MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
3025 f331110f bellard
    if (s->hcom == INVALID_HANDLE_VALUE) {
3026 f331110f bellard
        fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
3027 f331110f bellard
        s->hcom = NULL;
3028 f331110f bellard
        goto fail;
3029 f331110f bellard
    }
3030 f331110f bellard
3031 f331110f bellard
    ZeroMemory(&ov, sizeof(ov));
3032 f331110f bellard
    ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
3033 f331110f bellard
    ret = ConnectNamedPipe(s->hcom, &ov);
3034 f331110f bellard
    if (ret) {
3035 f331110f bellard
        fprintf(stderr, "Failed ConnectNamedPipe\n");
3036 f331110f bellard
        goto fail;
3037 f331110f bellard
    }
3038 f331110f bellard
3039 f331110f bellard
    ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE);
3040 f331110f bellard
    if (!ret) {
3041 f331110f bellard
        fprintf(stderr, "Failed GetOverlappedResult\n");
3042 f331110f bellard
        if (ov.hEvent) {
3043 f331110f bellard
            CloseHandle(ov.hEvent);
3044 f331110f bellard
            ov.hEvent = NULL;
3045 f331110f bellard
        }
3046 f331110f bellard
        goto fail;
3047 f331110f bellard
    }
3048 f331110f bellard
3049 f331110f bellard
    if (ov.hEvent) {
3050 f331110f bellard
        CloseHandle(ov.hEvent);
3051 f331110f bellard
        ov.hEvent = NULL;
3052 f331110f bellard
    }
3053 087f4ae0 ths
    qemu_add_polling_cb(win_chr_pipe_poll, chr);
3054 f331110f bellard
    return 0;
3055 f331110f bellard
3056 f331110f bellard
 fail:
3057 087f4ae0 ths
    win_chr_close(chr);
3058 f331110f bellard
    return -1;
3059 f331110f bellard
}
3060 f331110f bellard
3061 f331110f bellard
3062 52f61fde ths
static CharDriverState *qemu_chr_open_win_pipe(const char *filename)
3063 f331110f bellard
{
3064 f331110f bellard
    CharDriverState *chr;
3065 f331110f bellard
    WinCharState *s;
3066 f331110f bellard
3067 f331110f bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
3068 f331110f bellard
    if (!chr)
3069 f331110f bellard
        return NULL;
3070 f331110f bellard
    s = qemu_mallocz(sizeof(WinCharState));
3071 f331110f bellard
    if (!s) {
3072 f331110f bellard
        free(chr);
3073 f331110f bellard
        return NULL;
3074 f331110f bellard
    }
3075 f331110f bellard
    chr->opaque = s;
3076 f331110f bellard
    chr->chr_write = win_chr_write;
3077 f331110f bellard
    chr->chr_close = win_chr_close;
3078 3b46e624 ths
3079 087f4ae0 ths
    if (win_chr_pipe_init(chr, filename) < 0) {
3080 f331110f bellard
        free(s);
3081 f331110f bellard
        free(chr);
3082 f331110f bellard
        return NULL;
3083 f331110f bellard
    }
3084 86e94dea ths
    qemu_chr_reset(chr);
3085 f331110f bellard
    return chr;
3086 f331110f bellard
}
3087 f331110f bellard
3088 52f61fde ths
static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
3089 f331110f bellard
{
3090 f331110f bellard
    CharDriverState *chr;
3091 f331110f bellard
    WinCharState *s;
3092 f331110f bellard
3093 f331110f bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
3094 f331110f bellard
    if (!chr)
3095 f331110f bellard
        return NULL;
3096 f331110f bellard
    s = qemu_mallocz(sizeof(WinCharState));
3097 f331110f bellard
    if (!s) {
3098 f331110f bellard
        free(chr);
3099 f331110f bellard
        return NULL;
3100 f331110f bellard
    }
3101 f331110f bellard
    s->hcom = fd_out;
3102 f331110f bellard
    chr->opaque = s;
3103 f331110f bellard
    chr->chr_write = win_chr_write;
3104 86e94dea ths
    qemu_chr_reset(chr);
3105 f331110f bellard
    return chr;
3106 f331110f bellard
}
3107 72d46479 ths
3108 72d46479 ths
static CharDriverState *qemu_chr_open_win_con(const char *filename)
3109 72d46479 ths
{
3110 72d46479 ths
    return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
3111 72d46479 ths
}
3112 72d46479 ths
3113 52f61fde ths
static CharDriverState *qemu_chr_open_win_file_out(const char *file_out)
3114 f331110f bellard
{
3115 f331110f bellard
    HANDLE fd_out;
3116 3b46e624 ths
3117 f331110f bellard
    fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
3118 f331110f bellard
                        OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3119 f331110f bellard
    if (fd_out == INVALID_HANDLE_VALUE)
3120 f331110f bellard
        return NULL;
3121 f331110f bellard
3122 f331110f bellard
    return qemu_chr_open_win_file(fd_out);
3123 f331110f bellard
}
3124 aec62507 ths
#endif /* !_WIN32 */
3125 f331110f bellard
3126 0bab00f3 bellard
/***********************************************************/
3127 0bab00f3 bellard
/* UDP Net console */
3128 0bab00f3 bellard
3129 0bab00f3 bellard
typedef struct {
3130 0bab00f3 bellard
    int fd;
3131 0bab00f3 bellard
    struct sockaddr_in daddr;
3132 60fe76f3 ths
    uint8_t buf[1024];
3133 0bab00f3 bellard
    int bufcnt;
3134 0bab00f3 bellard
    int bufptr;
3135 0bab00f3 bellard
    int max_size;
3136 0bab00f3 bellard
} NetCharDriver;
3137 0bab00f3 bellard
3138 0bab00f3 bellard
static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3139 0bab00f3 bellard
{
3140 0bab00f3 bellard
    NetCharDriver *s = chr->opaque;
3141 0bab00f3 bellard
3142 0bab00f3 bellard
    return sendto(s->fd, buf, len, 0,
3143 0bab00f3 bellard
                  (struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in));
3144 0bab00f3 bellard
}
3145 0bab00f3 bellard
3146 0bab00f3 bellard
static int udp_chr_read_poll(void *opaque)
3147 0bab00f3 bellard
{
3148 0bab00f3 bellard
    CharDriverState *chr = opaque;
3149 0bab00f3 bellard
    NetCharDriver *s = chr->opaque;
3150 0bab00f3 bellard
3151 e5b0bc44 pbrook
    s->max_size = qemu_chr_can_read(chr);
3152 0bab00f3 bellard
3153 0bab00f3 bellard
    /* If there were any stray characters in the queue process them
3154 0bab00f3 bellard
     * first
3155 0bab00f3 bellard
     */
3156 0bab00f3 bellard
    while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3157 e5b0bc44 pbrook
        qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3158 0bab00f3 bellard
        s->bufptr++;
3159 e5b0bc44 pbrook
        s->max_size = qemu_chr_can_read(chr);
3160 0bab00f3 bellard
    }
3161 0bab00f3 bellard
    return s->max_size;
3162 0bab00f3 bellard
}
3163 0bab00f3 bellard
3164 0bab00f3 bellard
static void udp_chr_read(void *opaque)
3165 0bab00f3 bellard
{
3166 0bab00f3 bellard
    CharDriverState *chr = opaque;
3167 0bab00f3 bellard
    NetCharDriver *s = chr->opaque;
3168 0bab00f3 bellard
3169 0bab00f3 bellard
    if (s->max_size == 0)
3170 0bab00f3 bellard
        return;
3171 0bab00f3 bellard
    s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0);
3172 0bab00f3 bellard
    s->bufptr = s->bufcnt;
3173 0bab00f3 bellard
    if (s->bufcnt <= 0)
3174 0bab00f3 bellard
        return;
3175 0bab00f3 bellard
3176 0bab00f3 bellard
    s->bufptr = 0;
3177 0bab00f3 bellard
    while (s->max_size > 0 && s->bufptr < s->bufcnt) {
3178 e5b0bc44 pbrook
        qemu_chr_read(chr, &s->buf[s->bufptr], 1);
3179 0bab00f3 bellard
        s->bufptr++;
3180 e5b0bc44 pbrook
        s->max_size = qemu_chr_can_read(chr);
3181 0bab00f3 bellard
    }
3182 0bab00f3 bellard
}
3183 0bab00f3 bellard
3184 e5b0bc44 pbrook
static void udp_chr_update_read_handler(CharDriverState *chr)
3185 0bab00f3 bellard
{
3186 0bab00f3 bellard
    NetCharDriver *s = chr->opaque;
3187 0bab00f3 bellard
3188 0bab00f3 bellard
    if (s->fd >= 0) {
3189 0bab00f3 bellard
        qemu_set_fd_handler2(s->fd, udp_chr_read_poll,
3190 0bab00f3 bellard
                             udp_chr_read, NULL, chr);
3191 0bab00f3 bellard
    }
3192 0bab00f3 bellard
}
3193 0bab00f3 bellard
3194 0bab00f3 bellard
int parse_host_port(struct sockaddr_in *saddr, const char *str);
3195 52f61fde ths
#ifndef _WIN32
3196 52f61fde ths
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str);
3197 52f61fde ths
#endif
3198 951f1351 bellard
int parse_host_src_port(struct sockaddr_in *haddr,
3199 951f1351 bellard
                        struct sockaddr_in *saddr,
3200 951f1351 bellard
                        const char *str);
3201 0bab00f3 bellard
3202 52f61fde ths
static CharDriverState *qemu_chr_open_udp(const char *def)
3203 0bab00f3 bellard
{
3204 0bab00f3 bellard
    CharDriverState *chr = NULL;
3205 0bab00f3 bellard
    NetCharDriver *s = NULL;
3206 0bab00f3 bellard
    int fd = -1;
3207 951f1351 bellard
    struct sockaddr_in saddr;
3208 0bab00f3 bellard
3209 0bab00f3 bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
3210 0bab00f3 bellard
    if (!chr)
3211 0bab00f3 bellard
        goto return_err;
3212 0bab00f3 bellard
    s = qemu_mallocz(sizeof(NetCharDriver));
3213 0bab00f3 bellard
    if (!s)
3214 0bab00f3 bellard
        goto return_err;
3215 0bab00f3 bellard
3216 0bab00f3 bellard
    fd = socket(PF_INET, SOCK_DGRAM, 0);
3217 0bab00f3 bellard
    if (fd < 0) {
3218 0bab00f3 bellard
        perror("socket(PF_INET, SOCK_DGRAM)");
3219 0bab00f3 bellard
        goto return_err;
3220 0bab00f3 bellard
    }
3221 0bab00f3 bellard
3222 951f1351 bellard
    if (parse_host_src_port(&s->daddr, &saddr, def) < 0) {
3223 951f1351 bellard
        printf("Could not parse: %s\n", def);
3224 951f1351 bellard
        goto return_err;
3225 0bab00f3 bellard
    }
3226 0bab00f3 bellard
3227 951f1351 bellard
    if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
3228 0bab00f3 bellard
    {
3229 0bab00f3 bellard
        perror("bind");
3230 0bab00f3 bellard
        goto return_err;
3231 0bab00f3 bellard
    }
3232 0bab00f3 bellard
3233 0bab00f3 bellard
    s->fd = fd;
3234 0bab00f3 bellard
    s->bufcnt = 0;
3235 0bab00f3 bellard
    s->bufptr = 0;
3236 0bab00f3 bellard
    chr->opaque = s;
3237 0bab00f3 bellard
    chr->chr_write = udp_chr_write;
3238 e5b0bc44 pbrook
    chr->chr_update_read_handler = udp_chr_update_read_handler;
3239 0bab00f3 bellard
    return chr;
3240 0bab00f3 bellard
3241 0bab00f3 bellard
return_err:
3242 0bab00f3 bellard
    if (chr)
3243 0bab00f3 bellard
        free(chr);
3244 0bab00f3 bellard
    if (s)
3245 0bab00f3 bellard
        free(s);
3246 0bab00f3 bellard
    if (fd >= 0)
3247 0bab00f3 bellard
        closesocket(fd);
3248 0bab00f3 bellard
    return NULL;
3249 0bab00f3 bellard
}
3250 0bab00f3 bellard
3251 0bab00f3 bellard
/***********************************************************/
3252 0bab00f3 bellard
/* TCP Net console */
3253 0bab00f3 bellard
3254 0bab00f3 bellard
typedef struct {
3255 0bab00f3 bellard
    int fd, listen_fd;
3256 0bab00f3 bellard
    int connected;
3257 0bab00f3 bellard
    int max_size;
3258 951f1351 bellard
    int do_telnetopt;
3259 e5b0bc44 pbrook
    int do_nodelay;
3260 ffd843bc ths
    int is_unix;
3261 0bab00f3 bellard
} TCPCharDriver;
3262 0bab00f3 bellard
3263 0bab00f3 bellard
static void tcp_chr_accept(void *opaque);
3264 0bab00f3 bellard
3265 0bab00f3 bellard
static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len)
3266 0bab00f3 bellard
{
3267 0bab00f3 bellard
    TCPCharDriver *s = chr->opaque;
3268 0bab00f3 bellard
    if (s->connected) {
3269 0bab00f3 bellard
        return send_all(s->fd, buf, len);
3270 0bab00f3 bellard
    } else {
3271 0bab00f3 bellard
        /* XXX: indicate an error ? */
3272 0bab00f3 bellard
        return len;
3273 0bab00f3 bellard
    }
3274 0bab00f3 bellard
}
3275 0bab00f3 bellard
3276 0bab00f3 bellard
static int tcp_chr_read_poll(void *opaque)
3277 0bab00f3 bellard
{
3278 0bab00f3 bellard
    CharDriverState *chr = opaque;
3279 0bab00f3 bellard
    TCPCharDriver *s = chr->opaque;
3280 0bab00f3 bellard
    if (!s->connected)
3281 0bab00f3 bellard
        return 0;
3282 e5b0bc44 pbrook
    s->max_size = qemu_chr_can_read(chr);
3283 0bab00f3 bellard
    return s->max_size;
3284 0bab00f3 bellard
}
3285 0bab00f3 bellard
3286 951f1351 bellard
#define IAC 255
3287 951f1351 bellard
#define IAC_BREAK 243
3288 951f1351 bellard
static void tcp_chr_process_IAC_bytes(CharDriverState *chr,
3289 951f1351 bellard
                                      TCPCharDriver *s,
3290 60fe76f3 ths
                                      uint8_t *buf, int *size)
3291 951f1351 bellard
{
3292 951f1351 bellard
    /* Handle any telnet client's basic IAC options to satisfy char by
3293 951f1351 bellard
     * char mode with no echo.  All IAC options will be removed from
3294 951f1351 bellard
     * the buf and the do_telnetopt variable will be used to track the
3295 951f1351 bellard
     * state of the width of the IAC information.
3296 951f1351 bellard
     *
3297 951f1351 bellard
     * IAC commands come in sets of 3 bytes with the exception of the
3298 951f1351 bellard
     * "IAC BREAK" command and the double IAC.
3299 951f1351 bellard
     */
3300 951f1351 bellard
3301 951f1351 bellard
    int i;
3302 951f1351 bellard
    int j = 0;
3303 951f1351 bellard
3304 951f1351 bellard
    for (i = 0; i < *size; i++) {
3305 951f1351 bellard
        if (s->do_telnetopt > 1) {
3306 951f1351 bellard
            if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
3307 951f1351 bellard
                /* Double IAC means send an IAC */
3308 951f1351 bellard
                if (j != i)
3309 951f1351 bellard
                    buf[j] = buf[i];
3310 951f1351 bellard
                j++;
3311 951f1351 bellard
                s->do_telnetopt = 1;
3312 951f1351 bellard
            } else {
3313 951f1351 bellard
                if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) {
3314 951f1351 bellard
                    /* Handle IAC break commands by sending a serial break */
3315 e5b0bc44 pbrook
                    qemu_chr_event(chr, CHR_EVENT_BREAK);
3316 951f1351 bellard
                    s->do_telnetopt++;
3317 951f1351 bellard
                }
3318 951f1351 bellard
                s->do_telnetopt++;
3319 951f1351 bellard
            }
3320 951f1351 bellard
            if (s->do_telnetopt >= 4) {
3321 951f1351 bellard
                s->do_telnetopt = 1;
3322 951f1351 bellard
            }
3323 951f1351 bellard
        } else {
3324 951f1351 bellard
            if ((unsigned char)buf[i] == IAC) {
3325 951f1351 bellard
                s->do_telnetopt = 2;
3326 951f1351 bellard
            } else {
3327 951f1351 bellard
                if (j != i)
3328 951f1351 bellard
                    buf[j] = buf[i];
3329 951f1351 bellard
                j++;
3330 951f1351 bellard
            }
3331 951f1351 bellard
        }
3332 951f1351 bellard
    }
3333 951f1351 bellard
    *size = j;
3334 951f1351 bellard
}
3335 951f1351 bellard
3336 0bab00f3 bellard
static void tcp_chr_read(void *opaque)
3337 0bab00f3 bellard
{
3338 0bab00f3 bellard
    CharDriverState *chr = opaque;
3339 0bab00f3 bellard
    TCPCharDriver *s = chr->opaque;
3340 0bab00f3 bellard
    uint8_t buf[1024];
3341 0bab00f3 bellard
    int len, size;
3342 0bab00f3 bellard
3343 0bab00f3 bellard
    if (!s->connected || s->max_size <= 0)
3344 0bab00f3 bellard
        return;
3345 0bab00f3 bellard
    len = sizeof(buf);
3346 0bab00f3 bellard
    if (len > s->max_size)
3347 0bab00f3 bellard
        len = s->max_size;
3348 0bab00f3 bellard
    size = recv(s->fd, buf, len, 0);
3349 0bab00f3 bellard
    if (size == 0) {
3350 0bab00f3 bellard
        /* connection closed */
3351 0bab00f3 bellard
        s->connected = 0;
3352 0bab00f3 bellard
        if (s->listen_fd >= 0) {
3353 0bab00f3 bellard
            qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3354 0bab00f3 bellard
        }
3355 0bab00f3 bellard
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
3356 0bab00f3 bellard
        closesocket(s->fd);
3357 0bab00f3 bellard
        s->fd = -1;
3358 0bab00f3 bellard
    } else if (size > 0) {
3359 951f1351 bellard
        if (s->do_telnetopt)
3360 951f1351 bellard
            tcp_chr_process_IAC_bytes(chr, s, buf, &size);
3361 951f1351 bellard
        if (size > 0)
3362 e5b0bc44 pbrook
            qemu_chr_read(chr, buf, size);
3363 0bab00f3 bellard
    }
3364 0bab00f3 bellard
}
3365 0bab00f3 bellard
3366 0bab00f3 bellard
static void tcp_chr_connect(void *opaque)
3367 0bab00f3 bellard
{
3368 0bab00f3 bellard
    CharDriverState *chr = opaque;
3369 0bab00f3 bellard
    TCPCharDriver *s = chr->opaque;
3370 0bab00f3 bellard
3371 0bab00f3 bellard
    s->connected = 1;
3372 0bab00f3 bellard
    qemu_set_fd_handler2(s->fd, tcp_chr_read_poll,
3373 0bab00f3 bellard
                         tcp_chr_read, NULL, chr);
3374 86e94dea ths
    qemu_chr_reset(chr);
3375 0bab00f3 bellard
}
3376 0bab00f3 bellard
3377 951f1351 bellard
#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c;
3378 951f1351 bellard
static void tcp_chr_telnet_init(int fd)
3379 951f1351 bellard
{
3380 951f1351 bellard
    char buf[3];
3381 951f1351 bellard
    /* Send the telnet negotion to put telnet in binary, no echo, single char mode */
3382 951f1351 bellard
    IACSET(buf, 0xff, 0xfb, 0x01);  /* IAC WILL ECHO */
3383 951f1351 bellard
    send(fd, (char *)buf, 3, 0);
3384 951f1351 bellard
    IACSET(buf, 0xff, 0xfb, 0x03);  /* IAC WILL Suppress go ahead */
3385 951f1351 bellard
    send(fd, (char *)buf, 3, 0);
3386 951f1351 bellard
    IACSET(buf, 0xff, 0xfb, 0x00);  /* IAC WILL Binary */
3387 951f1351 bellard
    send(fd, (char *)buf, 3, 0);
3388 951f1351 bellard
    IACSET(buf, 0xff, 0xfd, 0x00);  /* IAC DO Binary */
3389 951f1351 bellard
    send(fd, (char *)buf, 3, 0);
3390 951f1351 bellard
}
3391 951f1351 bellard
3392 f7499989 pbrook
static void socket_set_nodelay(int fd)
3393 f7499989 pbrook
{
3394 f7499989 pbrook
    int val = 1;
3395 f7499989 pbrook
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
3396 f7499989 pbrook
}
3397 f7499989 pbrook
3398 0bab00f3 bellard
static void tcp_chr_accept(void *opaque)
3399 0bab00f3 bellard
{
3400 0bab00f3 bellard
    CharDriverState *chr = opaque;
3401 0bab00f3 bellard
    TCPCharDriver *s = chr->opaque;
3402 0bab00f3 bellard
    struct sockaddr_in saddr;
3403 ffd843bc ths
#ifndef _WIN32
3404 ffd843bc ths
    struct sockaddr_un uaddr;
3405 ffd843bc ths
#endif
3406 ffd843bc ths
    struct sockaddr *addr;
3407 0bab00f3 bellard
    socklen_t len;
3408 0bab00f3 bellard
    int fd;
3409 0bab00f3 bellard
3410 0bab00f3 bellard
    for(;;) {
3411 ffd843bc ths
#ifndef _WIN32
3412 ffd843bc ths
        if (s->is_unix) {
3413 ffd843bc ths
            len = sizeof(uaddr);
3414 ffd843bc ths
            addr = (struct sockaddr *)&uaddr;
3415 ffd843bc ths
        } else
3416 ffd843bc ths
#endif
3417 ffd843bc ths
        {
3418 ffd843bc ths
            len = sizeof(saddr);
3419 ffd843bc ths
            addr = (struct sockaddr *)&saddr;
3420 ffd843bc ths
        }
3421 ffd843bc ths
        fd = accept(s->listen_fd, addr, &len);
3422 0bab00f3 bellard
        if (fd < 0 && errno != EINTR) {
3423 0bab00f3 bellard
            return;
3424 0bab00f3 bellard
        } else if (fd >= 0) {
3425 951f1351 bellard
            if (s->do_telnetopt)
3426 951f1351 bellard
                tcp_chr_telnet_init(fd);
3427 0bab00f3 bellard
            break;
3428 0bab00f3 bellard
        }
3429 0bab00f3 bellard
    }
3430 0bab00f3 bellard
    socket_set_nonblock(fd);
3431 f7499989 pbrook
    if (s->do_nodelay)
3432 f7499989 pbrook
        socket_set_nodelay(fd);
3433 0bab00f3 bellard
    s->fd = fd;
3434 0bab00f3 bellard
    qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL);
3435 0bab00f3 bellard
    tcp_chr_connect(chr);
3436 0bab00f3 bellard
}
3437 0bab00f3 bellard
3438 0bab00f3 bellard
static void tcp_chr_close(CharDriverState *chr)
3439 0bab00f3 bellard
{
3440 0bab00f3 bellard
    TCPCharDriver *s = chr->opaque;
3441 0bab00f3 bellard
    if (s->fd >= 0)
3442 0bab00f3 bellard
        closesocket(s->fd);
3443 0bab00f3 bellard
    if (s->listen_fd >= 0)
3444 0bab00f3 bellard
        closesocket(s->listen_fd);
3445 0bab00f3 bellard
    qemu_free(s);
3446 0bab00f3 bellard
}
3447 0bab00f3 bellard
3448 5fafdf24 ths
static CharDriverState *qemu_chr_open_tcp(const char *host_str,
3449 ffd843bc ths
                                          int is_telnet,
3450 ffd843bc ths
                                          int is_unix)
3451 0bab00f3 bellard
{
3452 0bab00f3 bellard
    CharDriverState *chr = NULL;
3453 0bab00f3 bellard
    TCPCharDriver *s = NULL;
3454 0bab00f3 bellard
    int fd = -1, ret, err, val;
3455 951f1351 bellard
    int is_listen = 0;
3456 951f1351 bellard
    int is_waitconnect = 1;
3457 f7499989 pbrook
    int do_nodelay = 0;
3458 951f1351 bellard
    const char *ptr;
3459 0bab00f3 bellard
    struct sockaddr_in saddr;
3460 ffd843bc ths
#ifndef _WIN32
3461 ffd843bc ths
    struct sockaddr_un uaddr;
3462 ffd843bc ths
#endif
3463 ffd843bc ths
    struct sockaddr *addr;
3464 ffd843bc ths
    socklen_t addrlen;
3465 0bab00f3 bellard
3466 ffd843bc ths
#ifndef _WIN32
3467 ffd843bc ths
    if (is_unix) {
3468 ffd843bc ths
        addr = (struct sockaddr *)&uaddr;
3469 ffd843bc ths
        addrlen = sizeof(uaddr);
3470 ffd843bc ths
        if (parse_unix_path(&uaddr, host_str) < 0)
3471 ffd843bc ths
            goto fail;
3472 ffd843bc ths
    } else
3473 ffd843bc ths
#endif
3474 ffd843bc ths
    {
3475 ffd843bc ths
        addr = (struct sockaddr *)&saddr;
3476 ffd843bc ths
        addrlen = sizeof(saddr);
3477 ffd843bc ths
        if (parse_host_port(&saddr, host_str) < 0)
3478 ffd843bc ths
            goto fail;
3479 ffd843bc ths
    }
3480 0bab00f3 bellard
3481 951f1351 bellard
    ptr = host_str;
3482 951f1351 bellard
    while((ptr = strchr(ptr,','))) {
3483 951f1351 bellard
        ptr++;
3484 951f1351 bellard
        if (!strncmp(ptr,"server",6)) {
3485 951f1351 bellard
            is_listen = 1;
3486 951f1351 bellard
        } else if (!strncmp(ptr,"nowait",6)) {
3487 951f1351 bellard
            is_waitconnect = 0;
3488 f7499989 pbrook
        } else if (!strncmp(ptr,"nodelay",6)) {
3489 f7499989 pbrook
            do_nodelay = 1;
3490 951f1351 bellard
        } else {
3491 951f1351 bellard
            printf("Unknown option: %s\n", ptr);
3492 951f1351 bellard
            goto fail;
3493 951f1351 bellard
        }
3494 951f1351 bellard
    }
3495 951f1351 bellard
    if (!is_listen)
3496 951f1351 bellard
        is_waitconnect = 0;
3497 951f1351 bellard
3498 0bab00f3 bellard
    chr = qemu_mallocz(sizeof(CharDriverState));
3499 0bab00f3 bellard
    if (!chr)
3500 0bab00f3 bellard
        goto fail;
3501 0bab00f3 bellard
    s = qemu_mallocz(sizeof(TCPCharDriver));
3502 0bab00f3 bellard
    if (!s)
3503 0bab00f3 bellard
        goto fail;
3504 ffd843bc ths
3505 ffd843bc ths
#ifndef _WIN32
3506 ffd843bc ths
    if (is_unix)
3507 ffd843bc ths
        fd = socket(PF_UNIX, SOCK_STREAM, 0);
3508 ffd843bc ths
    else
3509 ffd843bc ths
#endif
3510 ffd843bc ths
        fd = socket(PF_INET, SOCK_STREAM, 0);
3511 5fafdf24 ths
3512 5fafdf24 ths
    if (fd < 0)
3513 0bab00f3 bellard
        goto fail;
3514 951f1351 bellard
3515 951f1351 bellard
    if (!is_waitconnect)
3516 951f1351 bellard
        socket_set_nonblock(fd);
3517 0bab00f3 bellard
3518 0bab00f3 bellard
    s->connected = 0;
3519 0bab00f3 bellard
    s->fd = -1;
3520 0bab00f3 bellard
    s->listen_fd = -1;
3521 ffd843bc ths
    s->is_unix = is_unix;
3522 f7499989 pbrook
    s->do_nodelay = do_nodelay && !is_unix;
3523 ffd843bc ths
3524 ffd843bc ths
    chr->opaque = s;
3525 ffd843bc ths
    chr->chr_write = tcp_chr_write;
3526 ffd843bc ths
    chr->chr_close = tcp_chr_close;
3527 ffd843bc ths
3528 0bab00f3 bellard
    if (is_listen) {
3529 0bab00f3 bellard
        /* allow fast reuse */
3530 ffd843bc ths
#ifndef _WIN32
3531 ffd843bc ths
        if (is_unix) {
3532 ffd843bc ths
            char path[109];
3533 ae45d369 bellard
            pstrcpy(path, sizeof(path), uaddr.sun_path);
3534 ffd843bc ths
            unlink(path);
3535 ffd843bc ths
        } else
3536 ffd843bc ths
#endif
3537 ffd843bc ths
        {
3538 ffd843bc ths
            val = 1;
3539 ffd843bc ths
            setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
3540 ffd843bc ths
        }
3541 3b46e624 ths
3542 ffd843bc ths
        ret = bind(fd, addr, addrlen);
3543 ffd843bc ths
        if (ret < 0)
3544 0bab00f3 bellard
            goto fail;
3545 ffd843bc ths
3546 0bab00f3 bellard
        ret = listen(fd, 0);
3547 0bab00f3 bellard
        if (ret < 0)
3548 0bab00f3 bellard
            goto fail;
3549 ffd843bc ths
3550 0bab00f3 bellard
        s->listen_fd = fd;
3551 0bab00f3 bellard
        qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
3552 951f1351 bellard
        if (is_telnet)
3553 951f1351 bellard
            s->do_telnetopt = 1;
3554 0bab00f3 bellard
    } else {
3555 0bab00f3 bellard
        for(;;) {
3556 ffd843bc ths
            ret = connect(fd, addr, addrlen);
3557 0bab00f3 bellard
            if (ret < 0) {
3558 0bab00f3 bellard
                err = socket_error();
3559 0bab00f3 bellard
                if (err == EINTR || err == EWOULDBLOCK) {
3560 0bab00f3 bellard
                } else if (err == EINPROGRESS) {
3561 0bab00f3 bellard
                    break;
3562 f5b12268 ths
#ifdef _WIN32
3563 f5b12268 ths
                } else if (err == WSAEALREADY) {
3564 f5b12268 ths
                    break;
3565 f5b12268 ths
#endif
3566 0bab00f3 bellard
                } else {
3567 0bab00f3 bellard
                    goto fail;
3568 0bab00f3 bellard
                }
3569 0bab00f3 bellard
            } else {
3570 0bab00f3 bellard
                s->connected = 1;
3571 0bab00f3 bellard
                break;
3572 0bab00f3 bellard
            }
3573 0bab00f3 bellard
        }
3574 0bab00f3 bellard
        s->fd = fd;
3575 f7499989 pbrook
        socket_set_nodelay(fd);
3576 0bab00f3 bellard
        if (s->connected)
3577 0bab00f3 bellard
            tcp_chr_connect(chr);
3578 0bab00f3 bellard
        else
3579 0bab00f3 bellard
            qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
3580 0bab00f3 bellard
    }
3581 3b46e624 ths
3582 951f1351 bellard
    if (is_listen && is_waitconnect) {
3583 951f1351 bellard
        printf("QEMU waiting for connection on: %s\n", host_str);
3584 951f1351 bellard
        tcp_chr_accept(chr);
3585 951f1351 bellard
        socket_set_nonblock(s->listen_fd);
3586 951f1351 bellard
    }
3587 951f1351 bellard
3588 0bab00f3 bellard
    return chr;
3589 0bab00f3 bellard
 fail:
3590 0bab00f3 bellard
    if (fd >= 0)
3591 0bab00f3 bellard
        closesocket(fd);
3592 0bab00f3 bellard
    qemu_free(s);
3593 0bab00f3 bellard
    qemu_free(chr);
3594 0bab00f3 bellard
    return NULL;
3595 0bab00f3 bellard
}
3596 0bab00f3 bellard
3597 82c643ff bellard
CharDriverState *qemu_chr_open(const char *filename)
3598 82c643ff bellard
{
3599 f8d179e3 bellard
    const char *p;
3600 fd1dff4b bellard
3601 82c643ff bellard
    if (!strcmp(filename, "vc")) {
3602 af3a9031 ths
        return text_console_init(&display_state, 0);
3603 af3a9031 ths
    } else if (strstart(filename, "vc:", &p)) {
3604 af3a9031 ths
        return text_console_init(&display_state, p);
3605 82c643ff bellard
    } else if (!strcmp(filename, "null")) {
3606 82c643ff bellard
        return qemu_chr_open_null();
3607 5fafdf24 ths
    } else
3608 0bab00f3 bellard
    if (strstart(filename, "tcp:", &p)) {
3609 ffd843bc ths
        return qemu_chr_open_tcp(p, 0, 0);
3610 0bab00f3 bellard
    } else
3611 951f1351 bellard
    if (strstart(filename, "telnet:", &p)) {
3612 ffd843bc ths
        return qemu_chr_open_tcp(p, 1, 0);
3613 0bab00f3 bellard
    } else
3614 0bab00f3 bellard
    if (strstart(filename, "udp:", &p)) {
3615 0bab00f3 bellard
        return qemu_chr_open_udp(p);
3616 0bab00f3 bellard
    } else
3617 20d8a3ed ths
    if (strstart(filename, "mon:", &p)) {
3618 20d8a3ed ths
        CharDriverState *drv = qemu_chr_open(p);
3619 20d8a3ed ths
        if (drv) {
3620 20d8a3ed ths
            drv = qemu_chr_open_mux(drv);
3621 20d8a3ed ths
            monitor_init(drv, !nographic);
3622 20d8a3ed ths
            return drv;
3623 20d8a3ed ths
        }
3624 20d8a3ed ths
        printf("Unable to open driver: %s\n", p);
3625 20d8a3ed ths
        return 0;
3626 20d8a3ed ths
    } else
3627 7664728b bellard
#ifndef _WIN32
3628 ffd843bc ths
    if (strstart(filename, "unix:", &p)) {
3629 ffd843bc ths
        return qemu_chr_open_tcp(p, 0, 1);
3630 ffd843bc ths
    } else if (strstart(filename, "file:", &p)) {
3631 f8d179e3 bellard
        return qemu_chr_open_file_out(p);
3632 f8d179e3 bellard
    } else if (strstart(filename, "pipe:", &p)) {
3633 f8d179e3 bellard
        return qemu_chr_open_pipe(p);
3634 7664728b bellard
    } else if (!strcmp(filename, "pty")) {
3635 82c643ff bellard
        return qemu_chr_open_pty();
3636 82c643ff bellard
    } else if (!strcmp(filename, "stdio")) {
3637 82c643ff bellard
        return qemu_chr_open_stdio();
3638 5fafdf24 ths
    } else
3639 f8d179e3 bellard
#if defined(__linux__)
3640 e57a8c0e bellard
    if (strstart(filename, "/dev/parport", NULL)) {
3641 e57a8c0e bellard
        return qemu_chr_open_pp(filename);
3642 5fafdf24 ths
    } else
3643 aec62507 ths
#endif
3644 3fda388a ths
#if defined(__linux__) || defined(__sun__)
3645 f8d179e3 bellard
    if (strstart(filename, "/dev/", NULL)) {
3646 f8d179e3 bellard
        return qemu_chr_open_tty(filename);
3647 3fda388a ths
    } else
3648 3fda388a ths
#endif
3649 aec62507 ths
#else /* !_WIN32 */
3650 f331110f bellard
    if (strstart(filename, "COM", NULL)) {
3651 f331110f bellard
        return qemu_chr_open_win(filename);
3652 f331110f bellard
    } else
3653 f331110f bellard
    if (strstart(filename, "pipe:", &p)) {
3654 f331110f bellard
        return qemu_chr_open_win_pipe(p);
3655 f331110f bellard
    } else
3656 72d46479 ths
    if (strstart(filename, "con:", NULL)) {
3657 72d46479 ths
        return qemu_chr_open_win_con(filename);
3658 72d46479 ths
    } else
3659 f331110f bellard
    if (strstart(filename, "file:", &p)) {
3660 f331110f bellard
        return qemu_chr_open_win_file_out(p);
3661 2e4d9fb1 aurel32
    } else
3662 2e4d9fb1 aurel32
#endif
3663 2e4d9fb1 aurel32
#ifdef CONFIG_BRLAPI
3664 2e4d9fb1 aurel32
    if (!strcmp(filename, "braille")) {
3665 2e4d9fb1 aurel32
        return chr_baum_init();
3666 2e4d9fb1 aurel32
    } else
3667 f331110f bellard
#endif
3668 82c643ff bellard
    {
3669 82c643ff bellard
        return NULL;
3670 82c643ff bellard
    }
3671 82c643ff bellard
}
3672 82c643ff bellard
3673 f331110f bellard
void qemu_chr_close(CharDriverState *chr)
3674 f331110f bellard
{
3675 f331110f bellard
    if (chr->chr_close)
3676 f331110f bellard
        chr->chr_close(chr);
3677 a11d070e balrog
    qemu_free(chr);
3678 f331110f bellard
}
3679 f331110f bellard
3680 80cabfad bellard
/***********************************************************/
3681 7c9d8e07 bellard
/* network device redirectors */
3682 330d0414 bellard
3683 3f4afa14 j_mayer
__attribute__ (( unused ))
3684 9596ebb7 pbrook
static void hex_dump(FILE *f, const uint8_t *buf, int size)
3685 c20709aa bellard
{
3686 c20709aa bellard
    int len, i, j, c;
3687 c20709aa bellard
3688 c20709aa bellard
    for(i=0;i<size;i+=16) {
3689 c20709aa bellard
        len = size - i;
3690 c20709aa bellard
        if (len > 16)
3691 c20709aa bellard
            len = 16;
3692 c20709aa bellard
        fprintf(f, "%08x ", i);
3693 c20709aa bellard
        for(j=0;j<16;j++) {
3694 c20709aa bellard
            if (j < len)
3695 c20709aa bellard
                fprintf(f, " %02x", buf[i+j]);
3696 c20709aa bellard
            else
3697 c20709aa bellard
                fprintf(f, "   ");
3698 c20709aa bellard
        }
3699 c20709aa bellard
        fprintf(f, " ");
3700 c20709aa bellard
        for(j=0;j<len;j++) {
3701 c20709aa bellard
            c = buf[i+j];
3702 c20709aa bellard
            if (c < ' ' || c > '~')
3703 c20709aa bellard
                c = '.';
3704 c20709aa bellard
            fprintf(f, "%c", c);
3705 c20709aa bellard
        }
3706 c20709aa bellard
        fprintf(f, "\n");
3707 c20709aa bellard
    }
3708 c20709aa bellard
}
3709 c20709aa bellard
3710 7c9d8e07 bellard
static int parse_macaddr(uint8_t *macaddr, const char *p)
3711 c20709aa bellard
{
3712 7c9d8e07 bellard
    int i;
3713 76ea08f9 balrog
    char *last_char;
3714 76ea08f9 balrog
    long int offset;
3715 76ea08f9 balrog
3716 76ea08f9 balrog
    errno = 0;
3717 76ea08f9 balrog
    offset = strtol(p, &last_char, 0);    
3718 76ea08f9 balrog
    if (0 == errno && '\0' == *last_char &&
3719 76ea08f9 balrog
            offset >= 0 && offset <= 0xFFFFFF) {
3720 76ea08f9 balrog
        macaddr[3] = (offset & 0xFF0000) >> 16;
3721 76ea08f9 balrog
        macaddr[4] = (offset & 0xFF00) >> 8;
3722 76ea08f9 balrog
        macaddr[5] = offset & 0xFF;
3723 76ea08f9 balrog
        return 0;
3724 76ea08f9 balrog
    } else {
3725 76ea08f9 balrog
        for(i = 0; i < 6; i++) {
3726 76ea08f9 balrog
            macaddr[i] = strtol(p, (char **)&p, 16);
3727 76ea08f9 balrog
            if (i == 5) {
3728 76ea08f9 balrog
                if (*p != '\0')
3729 76ea08f9 balrog
                    return -1;
3730 76ea08f9 balrog
            } else {
3731 76ea08f9 balrog
                if (*p != ':' && *p != '-')
3732 76ea08f9 balrog
                    return -1;
3733 76ea08f9 balrog
                p++;
3734 76ea08f9 balrog
            }
3735 7c9d8e07 bellard
        }
3736 76ea08f9 balrog
        return 0;    
3737 7c9d8e07 bellard
    }
3738 76ea08f9 balrog
3739 76ea08f9 balrog
    return -1;
3740 c20709aa bellard
}
3741 67b915a5 bellard
3742 7c9d8e07 bellard
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
3743 67b915a5 bellard
{
3744 7c9d8e07 bellard
    const char *p, *p1;
3745 7c9d8e07 bellard
    int len;
3746 7c9d8e07 bellard
    p = *pp;
3747 7c9d8e07 bellard
    p1 = strchr(p, sep);
3748 7c9d8e07 bellard
    if (!p1)
3749 7c9d8e07 bellard
        return -1;
3750 7c9d8e07 bellard
    len = p1 - p;
3751 7c9d8e07 bellard
    p1++;
3752 7c9d8e07 bellard
    if (buf_size > 0) {
3753 7c9d8e07 bellard
        if (len > buf_size - 1)
3754 7c9d8e07 bellard
            len = buf_size - 1;
3755 7c9d8e07 bellard
        memcpy(buf, p, len);
3756 7c9d8e07 bellard
        buf[len] = '\0';
3757 7c9d8e07 bellard
    }
3758 7c9d8e07 bellard
    *pp = p1;
3759 7c9d8e07 bellard
    return 0;
3760 c20709aa bellard
}
3761 c20709aa bellard
3762 951f1351 bellard
int parse_host_src_port(struct sockaddr_in *haddr,
3763 951f1351 bellard
                        struct sockaddr_in *saddr,
3764 951f1351 bellard
                        const char *input_str)
3765 951f1351 bellard
{
3766 951f1351 bellard
    char *str = strdup(input_str);
3767 951f1351 bellard
    char *host_str = str;
3768 951f1351 bellard
    char *src_str;
3769 951f1351 bellard
    char *ptr;
3770 951f1351 bellard
3771 951f1351 bellard
    /*
3772 951f1351 bellard
     * Chop off any extra arguments at the end of the string which
3773 951f1351 bellard
     * would start with a comma, then fill in the src port information
3774 951f1351 bellard
     * if it was provided else use the "any address" and "any port".
3775 951f1351 bellard
     */
3776 951f1351 bellard
    if ((ptr = strchr(str,',')))
3777 951f1351 bellard
        *ptr = '\0';
3778 951f1351 bellard
3779 951f1351 bellard
    if ((src_str = strchr(input_str,'@'))) {
3780 951f1351 bellard
        *src_str = '\0';
3781 951f1351 bellard
        src_str++;
3782 951f1351 bellard
    }
3783 951f1351 bellard
3784 951f1351 bellard
    if (parse_host_port(haddr, host_str) < 0)
3785 951f1351 bellard
        goto fail;
3786 951f1351 bellard
3787 951f1351 bellard
    if (!src_str || *src_str == '\0')
3788 951f1351 bellard
        src_str = ":0";
3789 951f1351 bellard
3790 951f1351 bellard
    if (parse_host_port(saddr, src_str) < 0)
3791 951f1351 bellard
        goto fail;
3792 951f1351 bellard
3793 951f1351 bellard
    free(str);
3794 951f1351 bellard
    return(0);
3795 951f1351 bellard
3796 951f1351 bellard
fail:
3797 951f1351 bellard
    free(str);
3798 951f1351 bellard
    return -1;
3799 951f1351 bellard
}
3800 951f1351 bellard
3801 7c9d8e07 bellard
int parse_host_port(struct sockaddr_in *saddr, const char *str)
3802 7c9d8e07 bellard
{
3803 7c9d8e07 bellard
    char buf[512];
3804 7c9d8e07 bellard
    struct hostent *he;
3805 7c9d8e07 bellard
    const char *p, *r;
3806 7c9d8e07 bellard
    int port;
3807 7c9d8e07 bellard
3808 7c9d8e07 bellard
    p = str;
3809 7c9d8e07 bellard
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3810 7c9d8e07 bellard
        return -1;
3811 7c9d8e07 bellard
    saddr->sin_family = AF_INET;
3812 7c9d8e07 bellard
    if (buf[0] == '\0') {
3813 7c9d8e07 bellard
        saddr->sin_addr.s_addr = 0;
3814 7c9d8e07 bellard
    } else {
3815 7c9d8e07 bellard
        if (isdigit(buf[0])) {
3816 7c9d8e07 bellard
            if (!inet_aton(buf, &saddr->sin_addr))
3817 7c9d8e07 bellard
                return -1;
3818 7c9d8e07 bellard
        } else {
3819 7c9d8e07 bellard
            if ((he = gethostbyname(buf)) == NULL)
3820 7c9d8e07 bellard
                return - 1;
3821 7c9d8e07 bellard
            saddr->sin_addr = *(struct in_addr *)he->h_addr;
3822 7c9d8e07 bellard
        }
3823 7c9d8e07 bellard
    }
3824 7c9d8e07 bellard
    port = strtol(p, (char **)&r, 0);
3825 7c9d8e07 bellard
    if (r == p)
3826 7c9d8e07 bellard
        return -1;
3827 7c9d8e07 bellard
    saddr->sin_port = htons(port);
3828 7c9d8e07 bellard
    return 0;
3829 7c9d8e07 bellard
}
3830 c20709aa bellard
3831 52f61fde ths
#ifndef _WIN32
3832 52f61fde ths
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
3833 ffd843bc ths
{
3834 ffd843bc ths
    const char *p;
3835 ffd843bc ths
    int len;
3836 ffd843bc ths
3837 ffd843bc ths
    len = MIN(108, strlen(str));
3838 ffd843bc ths
    p = strchr(str, ',');
3839 ffd843bc ths
    if (p)
3840 ffd843bc ths
        len = MIN(len, p - str);
3841 ffd843bc ths
3842 ffd843bc ths
    memset(uaddr, 0, sizeof(*uaddr));
3843 ffd843bc ths
3844 ffd843bc ths
    uaddr->sun_family = AF_UNIX;
3845 ffd843bc ths
    memcpy(uaddr->sun_path, str, len);
3846 ffd843bc ths
3847 ffd843bc ths
    return 0;
3848 ffd843bc ths
}
3849 52f61fde ths
#endif
3850 ffd843bc ths
3851 7c9d8e07 bellard
/* find or alloc a new VLAN */
3852 7c9d8e07 bellard
VLANState *qemu_find_vlan(int id)
3853 c20709aa bellard
{
3854 7c9d8e07 bellard
    VLANState **pvlan, *vlan;
3855 7c9d8e07 bellard
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3856 7c9d8e07 bellard
        if (vlan->id == id)
3857 7c9d8e07 bellard
            return vlan;
3858 7c9d8e07 bellard
    }
3859 7c9d8e07 bellard
    vlan = qemu_mallocz(sizeof(VLANState));
3860 7c9d8e07 bellard
    if (!vlan)
3861 7c9d8e07 bellard
        return NULL;
3862 7c9d8e07 bellard
    vlan->id = id;
3863 7c9d8e07 bellard
    vlan->next = NULL;
3864 7c9d8e07 bellard
    pvlan = &first_vlan;
3865 7c9d8e07 bellard
    while (*pvlan != NULL)
3866 7c9d8e07 bellard
        pvlan = &(*pvlan)->next;
3867 7c9d8e07 bellard
    *pvlan = vlan;
3868 7c9d8e07 bellard
    return vlan;
3869 c20709aa bellard
}
3870 c20709aa bellard
3871 7c9d8e07 bellard
VLANClientState *qemu_new_vlan_client(VLANState *vlan,
3872 d861b05e pbrook
                                      IOReadHandler *fd_read,
3873 d861b05e pbrook
                                      IOCanRWHandler *fd_can_read,
3874 d861b05e pbrook
                                      void *opaque)
3875 c20709aa bellard
{
3876 7c9d8e07 bellard
    VLANClientState *vc, **pvc;
3877 7c9d8e07 bellard
    vc = qemu_mallocz(sizeof(VLANClientState));
3878 7c9d8e07 bellard
    if (!vc)
3879 7c9d8e07 bellard
        return NULL;
3880 7c9d8e07 bellard
    vc->fd_read = fd_read;
3881 d861b05e pbrook
    vc->fd_can_read = fd_can_read;
3882 7c9d8e07 bellard
    vc->opaque = opaque;
3883 7c9d8e07 bellard
    vc->vlan = vlan;
3884 7c9d8e07 bellard
3885 7c9d8e07 bellard
    vc->next = NULL;
3886 7c9d8e07 bellard
    pvc = &vlan->first_client;
3887 7c9d8e07 bellard
    while (*pvc != NULL)
3888 7c9d8e07 bellard
        pvc = &(*pvc)->next;
3889 7c9d8e07 bellard
    *pvc = vc;
3890 7c9d8e07 bellard
    return vc;
3891 c20709aa bellard
}
3892 c20709aa bellard
3893 dcf414d6 balrog
void qemu_del_vlan_client(VLANClientState *vc)
3894 dcf414d6 balrog
{
3895 dcf414d6 balrog
    VLANClientState **pvc = &vc->vlan->first_client;
3896 dcf414d6 balrog
3897 dcf414d6 balrog
    while (*pvc != NULL)
3898 dcf414d6 balrog
        if (*pvc == vc) {
3899 dcf414d6 balrog
            *pvc = vc->next;
3900 dcf414d6 balrog
            free(vc);
3901 dcf414d6 balrog
            break;
3902 dcf414d6 balrog
        } else
3903 dcf414d6 balrog
            pvc = &(*pvc)->next;
3904 dcf414d6 balrog
}
3905 dcf414d6 balrog
3906 d861b05e pbrook
int qemu_can_send_packet(VLANClientState *vc1)
3907 d861b05e pbrook
{
3908 d861b05e pbrook
    VLANState *vlan = vc1->vlan;
3909 d861b05e pbrook
    VLANClientState *vc;
3910 d861b05e pbrook
3911 d861b05e pbrook
    for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3912 d861b05e pbrook
        if (vc != vc1) {
3913 fbd1711d balrog
            if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
3914 fbd1711d balrog
                return 1;
3915 d861b05e pbrook
        }
3916 d861b05e pbrook
    }
3917 fbd1711d balrog
    return 0;
3918 d861b05e pbrook
}
3919 d861b05e pbrook
3920 7c9d8e07 bellard
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
3921 c20709aa bellard
{
3922 7c9d8e07 bellard
    VLANState *vlan = vc1->vlan;
3923 7c9d8e07 bellard
    VLANClientState *vc;
3924 7c9d8e07 bellard
3925 7c9d8e07 bellard
#if 0
3926 7c9d8e07 bellard
    printf("vlan %d send:\n", vlan->id);
3927 7c9d8e07 bellard
    hex_dump(stdout, buf, size);
3928 7c9d8e07 bellard
#endif
3929 7c9d8e07 bellard
    for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
3930 7c9d8e07 bellard
        if (vc != vc1) {
3931 7c9d8e07 bellard
            vc->fd_read(vc->opaque, buf, size);
3932 7c9d8e07 bellard
        }
3933 7c9d8e07 bellard
    }
3934 67b915a5 bellard
}
3935 67b915a5 bellard
3936 c20709aa bellard
#if defined(CONFIG_SLIRP)
3937 c20709aa bellard
3938 c20709aa bellard
/* slirp network adapter */
3939 c20709aa bellard
3940 c20709aa bellard
static int slirp_inited;
3941 7c9d8e07 bellard
static VLANClientState *slirp_vc;
3942 c20709aa bellard
3943 c20709aa bellard
int slirp_can_output(void)
3944 c20709aa bellard
{
3945 3b7f5d47 pbrook
    return !slirp_vc || qemu_can_send_packet(slirp_vc);
3946 c20709aa bellard
}
3947 c20709aa bellard
3948 c20709aa bellard
void slirp_output(const uint8_t *pkt, int pkt_len)
3949 67b915a5 bellard
{
3950 c20709aa bellard
#if 0
3951 7c9d8e07 bellard
    printf("slirp output:\n");
3952 c20709aa bellard
    hex_dump(stdout, pkt, pkt_len);
3953 c20709aa bellard
#endif
3954 3b7f5d47 pbrook
    if (!slirp_vc)
3955 3b7f5d47 pbrook
        return;
3956 7c9d8e07 bellard
    qemu_send_packet(slirp_vc, pkt, pkt_len);
3957 67b915a5 bellard
}
3958 67b915a5 bellard
3959 7c9d8e07 bellard
static void slirp_receive(void *opaque, const uint8_t *buf, int size)
3960 c20709aa bellard
{
3961 c20709aa bellard
#if 0
3962 7c9d8e07 bellard
    printf("slirp input:\n");
3963 c20709aa bellard
    hex_dump(stdout, buf, size);
3964 c20709aa bellard
#endif
3965 c20709aa bellard
    slirp_input(buf, size);
3966 c20709aa bellard
}
3967 c20709aa bellard
3968 7c9d8e07 bellard
static int net_slirp_init(VLANState *vlan)
3969 c20709aa bellard
{
3970 c20709aa bellard
    if (!slirp_inited) {
3971 c20709aa bellard
        slirp_inited = 1;
3972 c20709aa bellard
        slirp_init();
3973 c20709aa bellard
    }
3974 5fafdf24 ths
    slirp_vc = qemu_new_vlan_client(vlan,
3975 d861b05e pbrook
                                    slirp_receive, NULL, NULL);
3976 7c9d8e07 bellard
    snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector");
3977 9bf05444 bellard
    return 0;
3978 9bf05444 bellard
}
3979 9bf05444 bellard
3980 9bf05444 bellard
static void net_slirp_redir(const char *redir_str)
3981 9bf05444 bellard
{
3982 9bf05444 bellard
    int is_udp;
3983 9bf05444 bellard
    char buf[256], *r;
3984 9bf05444 bellard
    const char *p;
3985 9bf05444 bellard
    struct in_addr guest_addr;
3986 9bf05444 bellard
    int host_port, guest_port;
3987 3b46e624 ths
3988 9bf05444 bellard
    if (!slirp_inited) {
3989 9bf05444 bellard
        slirp_inited = 1;
3990 9bf05444 bellard
        slirp_init();
3991 9bf05444 bellard
    }
3992 9bf05444 bellard
3993 9bf05444 bellard
    p = redir_str;
3994 9bf05444 bellard
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
3995 9bf05444 bellard
        goto fail;
3996 9bf05444 bellard
    if (!strcmp(buf, "tcp")) {
3997 9bf05444 bellard
        is_udp = 0;
3998 9bf05444 bellard
    } else if (!strcmp(buf, "udp")) {
3999 9bf05444 bellard
        is_udp = 1;
4000 9bf05444 bellard
    } else {
4001 9bf05444 bellard
        goto fail;
4002 9bf05444 bellard
    }
4003 9bf05444 bellard
4004 9bf05444 bellard
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
4005 9bf05444 bellard
        goto fail;
4006 9bf05444 bellard
    host_port = strtol(buf, &r, 0);
4007 9bf05444 bellard
    if (r == buf)
4008 9bf05444 bellard
        goto fail;
4009 9bf05444 bellard
4010 9bf05444 bellard
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
4011 9bf05444 bellard
        goto fail;
4012 9bf05444 bellard
    if (buf[0] == '\0') {
4013 9bf05444 bellard
        pstrcpy(buf, sizeof(buf), "10.0.2.15");
4014 9bf05444 bellard
    }
4015 9bf05444 bellard
    if (!inet_aton(buf, &guest_addr))
4016 9bf05444 bellard
        goto fail;
4017 3b46e624 ths
4018 9bf05444 bellard
    guest_port = strtol(p, &r, 0);
4019 9bf05444 bellard
    if (r == p)
4020 9bf05444 bellard
        goto fail;
4021 3b46e624 ths
4022 9bf05444 bellard
    if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
4023 9bf05444 bellard
        fprintf(stderr, "qemu: could not set up redirection\n");
4024 9bf05444 bellard
        exit(1);
4025 9bf05444 bellard
    }
4026 9bf05444 bellard
    return;
4027 9bf05444 bellard
 fail:
4028 9bf05444 bellard
    fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
4029 9bf05444 bellard
    exit(1);
4030 9bf05444 bellard
}
4031 3b46e624 ths
4032 c94c8d64 bellard
#ifndef _WIN32
4033 c94c8d64 bellard
4034 9d728e8c bellard
char smb_dir[1024];
4035 9d728e8c bellard
4036 044fae83 balrog
static void erase_dir(char *dir_name)
4037 9d728e8c bellard
{
4038 9d728e8c bellard
    DIR *d;
4039 9d728e8c bellard
    struct dirent *de;
4040 9d728e8c bellard
    char filename[1024];
4041 9d728e8c bellard
4042 9d728e8c bellard
    /* erase all the files in the directory */
4043 044fae83 balrog
    if ((d = opendir(dir_name)) != 0) {
4044 044fae83 balrog
        for(;;) {
4045 044fae83 balrog
            de = readdir(d);
4046 044fae83 balrog
            if (!de)
4047 044fae83 balrog
                break;
4048 044fae83 balrog
            if (strcmp(de->d_name, ".") != 0 &&
4049 044fae83 balrog
                strcmp(de->d_name, "..") != 0) {
4050 044fae83 balrog
                snprintf(filename, sizeof(filename), "%s/%s",
4051 044fae83 balrog
                         smb_dir, de->d_name);
4052 044fae83 balrog
                if (unlink(filename) != 0)  /* is it a directory? */
4053 044fae83 balrog
                    erase_dir(filename);
4054 044fae83 balrog
            }
4055 9d728e8c bellard
        }
4056 044fae83 balrog
        closedir(d);
4057 044fae83 balrog
        rmdir(dir_name);
4058 9d728e8c bellard
    }
4059 044fae83 balrog
}
4060 044fae83 balrog
4061 044fae83 balrog
/* automatic user mode samba server configuration */
4062 044fae83 balrog
static void smb_exit(void)
4063 044fae83 balrog
{
4064 044fae83 balrog
    erase_dir(smb_dir);
4065 9d728e8c bellard
}
4066 9d728e8c bellard
4067 9d728e8c bellard
/* automatic user mode samba server configuration */
4068 9596ebb7 pbrook
static void net_slirp_smb(const char *exported_dir)
4069 9d728e8c bellard
{
4070 9d728e8c bellard
    char smb_conf[1024];
4071 9d728e8c bellard
    char smb_cmdline[1024];
4072 9d728e8c bellard
    FILE *f;
4073 9d728e8c bellard
4074 9d728e8c bellard
    if (!slirp_inited) {
4075 9d728e8c bellard
        slirp_inited = 1;
4076 9d728e8c bellard
        slirp_init();
4077 9d728e8c bellard
    }
4078 9d728e8c bellard
4079 9d728e8c bellard
    /* XXX: better tmp dir construction */
4080 9d728e8c bellard
    snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
4081 9d728e8c bellard
    if (mkdir(smb_dir, 0700) < 0) {
4082 9d728e8c bellard
        fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
4083 9d728e8c bellard
        exit(1);
4084 9d728e8c bellard
    }
4085 9d728e8c bellard
    snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
4086 3b46e624 ths
4087 9d728e8c bellard
    f = fopen(smb_conf, "w");
4088 9d728e8c bellard
    if (!f) {
4089 9d728e8c bellard
        fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
4090 9d728e8c bellard
        exit(1);
4091 9d728e8c bellard
    }
4092 5fafdf24 ths
    fprintf(f,
4093 9d728e8c bellard
            "[global]\n"
4094 157777ef bellard
            "private dir=%s\n"
4095 157777ef bellard
            "smb ports=0\n"
4096 157777ef bellard
            "socket address=127.0.0.1\n"
4097 9d728e8c bellard
            "pid directory=%s\n"
4098 9d728e8c bellard
            "lock directory=%s\n"
4099 9d728e8c bellard
            "log file=%s/log.smbd\n"
4100 9d728e8c bellard
            "smb passwd file=%s/smbpasswd\n"
4101 03ffbb69 bellard
            "security = share\n"
4102 9d728e8c bellard
            "[qemu]\n"
4103 9d728e8c bellard
            "path=%s\n"
4104 9d728e8c bellard
            "read only=no\n"
4105 9d728e8c bellard
            "guest ok=yes\n",
4106 9d728e8c bellard
            smb_dir,
4107 157777ef bellard
            smb_dir,
4108 9d728e8c bellard
            smb_dir,
4109 9d728e8c bellard
            smb_dir,
4110 9d728e8c bellard
            smb_dir,
4111 9d728e8c bellard
            exported_dir
4112 9d728e8c bellard
            );
4113 9d728e8c bellard
    fclose(f);
4114 9d728e8c bellard
    atexit(smb_exit);
4115 9d728e8c bellard
4116 a14d6c8c pbrook
    snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
4117 a14d6c8c pbrook
             SMBD_COMMAND, smb_conf);
4118 3b46e624 ths
4119 9d728e8c bellard
    slirp_add_exec(0, smb_cmdline, 4, 139);
4120 9d728e8c bellard
}
4121 9bf05444 bellard
4122 c94c8d64 bellard
#endif /* !defined(_WIN32) */
4123 31a60e22 blueswir1
void do_info_slirp(void)
4124 31a60e22 blueswir1
{
4125 31a60e22 blueswir1
    slirp_stats();
4126 31a60e22 blueswir1
}
4127 c94c8d64 bellard
4128 c20709aa bellard
#endif /* CONFIG_SLIRP */
4129 c20709aa bellard
4130 c20709aa bellard
#if !defined(_WIN32)
4131 7c9d8e07 bellard
4132 7c9d8e07 bellard
typedef struct TAPState {
4133 7c9d8e07 bellard
    VLANClientState *vc;
4134 7c9d8e07 bellard
    int fd;
4135 b46a8906 ths
    char down_script[1024];
4136 7c9d8e07 bellard
} TAPState;
4137 7c9d8e07 bellard
4138 7c9d8e07 bellard
static void tap_receive(void *opaque, const uint8_t *buf, int size)
4139 7c9d8e07 bellard
{
4140 7c9d8e07 bellard
    TAPState *s = opaque;
4141 7c9d8e07 bellard
    int ret;
4142 7c9d8e07 bellard
    for(;;) {
4143 7c9d8e07 bellard
        ret = write(s->fd, buf, size);
4144 7c9d8e07 bellard
        if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
4145 7c9d8e07 bellard
        } else {
4146 7c9d8e07 bellard
            break;
4147 7c9d8e07 bellard
        }
4148 7c9d8e07 bellard
    }
4149 7c9d8e07 bellard
}
4150 7c9d8e07 bellard
4151 7c9d8e07 bellard
static void tap_send(void *opaque)
4152 7c9d8e07 bellard
{
4153 7c9d8e07 bellard
    TAPState *s = opaque;
4154 7c9d8e07 bellard
    uint8_t buf[4096];
4155 7c9d8e07 bellard
    int size;
4156 7c9d8e07 bellard
4157 d5d10bc3 ths
#ifdef __sun__
4158 d5d10bc3 ths
    struct strbuf sbuf;
4159 d5d10bc3 ths
    int f = 0;
4160 d5d10bc3 ths
    sbuf.maxlen = sizeof(buf);
4161 d5d10bc3 ths
    sbuf.buf = buf;
4162 d5d10bc3 ths
    size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
4163 d5d10bc3 ths
#else
4164 7c9d8e07 bellard
    size = read(s->fd, buf, sizeof(buf));
4165 d5d10bc3 ths
#endif
4166 7c9d8e07 bellard
    if (size > 0) {
4167 7c9d8e07 bellard
        qemu_send_packet(s->vc, buf, size);
4168 7c9d8e07 bellard
    }
4169 7c9d8e07 bellard
}
4170 7c9d8e07 bellard
4171 7c9d8e07 bellard
/* fd support */
4172 7c9d8e07 bellard
4173 7c9d8e07 bellard
static TAPState *net_tap_fd_init(VLANState *vlan, int fd)
4174 7c9d8e07 bellard
{
4175 7c9d8e07 bellard
    TAPState *s;
4176 7c9d8e07 bellard
4177 7c9d8e07 bellard
    s = qemu_mallocz(sizeof(TAPState));
4178 7c9d8e07 bellard
    if (!s)
4179 7c9d8e07 bellard
        return NULL;
4180 7c9d8e07 bellard
    s->fd = fd;
4181 d861b05e pbrook
    s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
4182 7c9d8e07 bellard
    qemu_set_fd_handler(s->fd, tap_send, NULL, s);
4183 7c9d8e07 bellard
    snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd);
4184 7c9d8e07 bellard
    return s;
4185 7c9d8e07 bellard
}
4186 7c9d8e07 bellard
4187 5c40d2bd ths
#if defined (_BSD) || defined (__FreeBSD_kernel__)
4188 7c9d8e07 bellard
static int tap_open(char *ifname, int ifname_size)
4189 7d3505c5 bellard
{
4190 7d3505c5 bellard
    int fd;
4191 7d3505c5 bellard
    char *dev;
4192 7d3505c5 bellard
    struct stat s;
4193 67b915a5 bellard
4194 aeb30be6 balrog
    TFR(fd = open("/dev/tap", O_RDWR));
4195 7d3505c5 bellard
    if (fd < 0) {
4196 7d3505c5 bellard
        fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
4197 7d3505c5 bellard
        return -1;
4198 7d3505c5 bellard
    }
4199 7d3505c5 bellard
4200 7d3505c5 bellard
    fstat(fd, &s);
4201 7d3505c5 bellard
    dev = devname(s.st_rdev, S_IFCHR);
4202 7d3505c5 bellard
    pstrcpy(ifname, ifname_size, dev);
4203 7d3505c5 bellard
4204 7d3505c5 bellard
    fcntl(fd, F_SETFL, O_NONBLOCK);
4205 7d3505c5 bellard
    return fd;
4206 7d3505c5 bellard
}
4207 ec530c81 bellard
#elif defined(__sun__)
4208 d5d10bc3 ths
#define TUNNEWPPA       (('T'<<16) | 0x0001)
4209 5fafdf24 ths
/*
4210 5fafdf24 ths
 * Allocate TAP device, returns opened fd.
4211 d5d10bc3 ths
 * Stores dev name in the first arg(must be large enough).
4212 3b46e624 ths
 */
4213 d5d10bc3 ths
int tap_alloc(char *dev)
4214 d5d10bc3 ths
{
4215 d5d10bc3 ths
    int tap_fd, if_fd, ppa = -1;
4216 d5d10bc3 ths
    static int ip_fd = 0;
4217 d5d10bc3 ths
    char *ptr;
4218 d5d10bc3 ths
4219 d5d10bc3 ths
    static int arp_fd = 0;
4220 d5d10bc3 ths
    int ip_muxid, arp_muxid;
4221 d5d10bc3 ths
    struct strioctl  strioc_if, strioc_ppa;
4222 d5d10bc3 ths
    int link_type = I_PLINK;;
4223 d5d10bc3 ths
    struct lifreq ifr;
4224 d5d10bc3 ths
    char actual_name[32] = "";
4225 d5d10bc3 ths
4226 d5d10bc3 ths
    memset(&ifr, 0x0, sizeof(ifr));
4227 d5d10bc3 ths
4228 d5d10bc3 ths
    if( *dev ){
4229 5fafdf24 ths
       ptr = dev;
4230 5fafdf24 ths
       while( *ptr && !isdigit((int)*ptr) ) ptr++;
4231 d5d10bc3 ths
       ppa = atoi(ptr);
4232 d5d10bc3 ths
    }
4233 d5d10bc3 ths
4234 d5d10bc3 ths
    /* Check if IP device was opened */
4235 d5d10bc3 ths
    if( ip_fd )
4236 d5d10bc3 ths
       close(ip_fd);
4237 d5d10bc3 ths
4238 aeb30be6 balrog
    TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
4239 aeb30be6 balrog
    if (ip_fd < 0) {
4240 d5d10bc3 ths
       syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
4241 d5d10bc3 ths
       return -1;
4242 d5d10bc3 ths
    }
4243 d5d10bc3 ths
4244 aeb30be6 balrog
    TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
4245 aeb30be6 balrog
    if (tap_fd < 0) {
4246 d5d10bc3 ths
       syslog(LOG_ERR, "Can't open /dev/tap");
4247 d5d10bc3 ths
       return -1;
4248 d5d10bc3 ths
    }
4249 d5d10bc3 ths
4250 d5d10bc3 ths
    /* Assign a new PPA and get its unit number. */
4251 d5d10bc3 ths
    strioc_ppa.ic_cmd = TUNNEWPPA;
4252 d5d10bc3 ths
    strioc_ppa.ic_timout = 0;
4253 d5d10bc3 ths
    strioc_ppa.ic_len = sizeof(ppa);
4254 d5d10bc3 ths
    strioc_ppa.ic_dp = (char *)&ppa;
4255 d5d10bc3 ths
    if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
4256 d5d10bc3 ths
       syslog (LOG_ERR, "Can't assign new interface");
4257 d5d10bc3 ths
4258 aeb30be6 balrog
    TFR(if_fd = open("/dev/tap", O_RDWR, 0));
4259 aeb30be6 balrog
    if (if_fd < 0) {
4260 d5d10bc3 ths
       syslog(LOG_ERR, "Can't open /dev/tap (2)");
4261 d5d10bc3 ths
       return -1;
4262 d5d10bc3 ths
    }
4263 d5d10bc3 ths
    if(ioctl(if_fd, I_PUSH, "ip") < 0){
4264 d5d10bc3 ths
       syslog(LOG_ERR, "Can't push IP module");
4265 d5d10bc3 ths
       return -1;
4266 d5d10bc3 ths
    }
4267 d5d10bc3 ths
4268 d5d10bc3 ths
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
4269 d5d10bc3 ths
        syslog(LOG_ERR, "Can't get flags\n");
4270 d5d10bc3 ths
4271 d5d10bc3 ths
    snprintf (actual_name, 32, "tap%d", ppa);
4272 d5d10bc3 ths
    strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4273 d5d10bc3 ths
4274 d5d10bc3 ths
    ifr.lifr_ppa = ppa;
4275 d5d10bc3 ths
    /* Assign ppa according to the unit number returned by tun device */
4276 d5d10bc3 ths
4277 d5d10bc3 ths
    if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
4278 d5d10bc3 ths
        syslog (LOG_ERR, "Can't set PPA %d", ppa);
4279 d5d10bc3 ths
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
4280 d5d10bc3 ths
        syslog (LOG_ERR, "Can't get flags\n");
4281 d5d10bc3 ths
    /* Push arp module to if_fd */
4282 d5d10bc3 ths
    if (ioctl (if_fd, I_PUSH, "arp") < 0)
4283 d5d10bc3 ths
        syslog (LOG_ERR, "Can't push ARP module (2)");
4284 d5d10bc3 ths
4285 d5d10bc3 ths
    /* Push arp module to ip_fd */
4286 d5d10bc3 ths
    if (ioctl (ip_fd, I_POP, NULL) < 0)
4287 d5d10bc3 ths
        syslog (LOG_ERR, "I_POP failed\n");
4288 d5d10bc3 ths
    if (ioctl (ip_fd, I_PUSH, "arp") < 0)
4289 d5d10bc3 ths
        syslog (LOG_ERR, "Can't push ARP module (3)\n");
4290 d5d10bc3 ths
    /* Open arp_fd */
4291 aeb30be6 balrog
    TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
4292 aeb30be6 balrog
    if (arp_fd < 0)
4293 d5d10bc3 ths
       syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
4294 d5d10bc3 ths
4295 d5d10bc3 ths
    /* Set ifname to arp */
4296 d5d10bc3 ths
    strioc_if.ic_cmd = SIOCSLIFNAME;
4297 d5d10bc3 ths
    strioc_if.ic_timout = 0;
4298 d5d10bc3 ths
    strioc_if.ic_len = sizeof(ifr);
4299 d5d10bc3 ths
    strioc_if.ic_dp = (char *)&ifr;
4300 d5d10bc3 ths
    if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
4301 d5d10bc3 ths
        syslog (LOG_ERR, "Can't set ifname to arp\n");
4302 d5d10bc3 ths
    }
4303 d5d10bc3 ths
4304 d5d10bc3 ths
    if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
4305 d5d10bc3 ths
       syslog(LOG_ERR, "Can't link TAP device to IP");
4306 d5d10bc3 ths
       return -1;
4307 d5d10bc3 ths
    }
4308 d5d10bc3 ths
4309 d5d10bc3 ths
    if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
4310 d5d10bc3 ths
        syslog (LOG_ERR, "Can't link TAP device to ARP");
4311 d5d10bc3 ths
4312 d5d10bc3 ths
    close (if_fd);
4313 d5d10bc3 ths
4314 d5d10bc3 ths
    memset(&ifr, 0x0, sizeof(ifr));
4315 d5d10bc3 ths
    strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
4316 d5d10bc3 ths
    ifr.lifr_ip_muxid  = ip_muxid;
4317 d5d10bc3 ths
    ifr.lifr_arp_muxid = arp_muxid;
4318 d5d10bc3 ths
4319 d5d10bc3 ths
    if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
4320 d5d10bc3 ths
    {
4321 d5d10bc3 ths
      ioctl (ip_fd, I_PUNLINK , arp_muxid);
4322 d5d10bc3 ths
      ioctl (ip_fd, I_PUNLINK, ip_muxid);
4323 d5d10bc3 ths
      syslog (LOG_ERR, "Can't set multiplexor id");
4324 d5d10bc3 ths
    }
4325 d5d10bc3 ths
4326 d5d10bc3 ths
    sprintf(dev, "tap%d", ppa);
4327 d5d10bc3 ths
    return tap_fd;
4328 d5d10bc3 ths
}
4329 d5d10bc3 ths
4330 ec530c81 bellard
static int tap_open(char *ifname, int ifname_size)
4331 ec530c81 bellard
{
4332 d5d10bc3 ths
    char  dev[10]="";
4333 d5d10bc3 ths
    int fd;
4334 d5d10bc3 ths
    if( (fd = tap_alloc(dev)) < 0 ){
4335 d5d10bc3 ths
       fprintf(stderr, "Cannot allocate TAP device\n");
4336 d5d10bc3 ths
       return -1;
4337 d5d10bc3 ths
    }
4338 d5d10bc3 ths
    pstrcpy(ifname, ifname_size, dev);
4339 d5d10bc3 ths
    fcntl(fd, F_SETFL, O_NONBLOCK);
4340 d5d10bc3 ths
    return fd;
4341 ec530c81 bellard
}
4342 7d3505c5 bellard
#else
4343 7c9d8e07 bellard
static int tap_open(char *ifname, int ifname_size)
4344 330d0414 bellard
{
4345 80cabfad bellard
    struct ifreq ifr;
4346 c4b1fcc0 bellard
    int fd, ret;
4347 3b46e624 ths
4348 aeb30be6 balrog
    TFR(fd = open("/dev/net/tun", O_RDWR));
4349 80cabfad bellard
    if (fd < 0) {
4350 80cabfad bellard
        fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
4351 80cabfad bellard
        return -1;
4352 330d0414 bellard
    }
4353 80cabfad bellard
    memset(&ifr, 0, sizeof(ifr));
4354 80cabfad bellard
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
4355 7c9d8e07 bellard
    if (ifname[0] != '\0')
4356 7c9d8e07 bellard
        pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
4357 7c9d8e07 bellard
    else
4358 7c9d8e07 bellard
        pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
4359 80cabfad bellard
    ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
4360 80cabfad bellard
    if (ret != 0) {
4361 80cabfad bellard
        fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
4362 80cabfad bellard
        close(fd);
4363 80cabfad bellard
        return -1;
4364 80cabfad bellard
    }
4365 c4b1fcc0 bellard
    pstrcpy(ifname, ifname_size, ifr.ifr_name);
4366 80cabfad bellard
    fcntl(fd, F_SETFL, O_NONBLOCK);
4367 c4b1fcc0 bellard
    return fd;
4368 c4b1fcc0 bellard
}
4369 7d3505c5 bellard
#endif
4370 330d0414 bellard
4371 b46a8906 ths
static int launch_script(const char *setup_script, const char *ifname, int fd)
4372 7c9d8e07 bellard
{
4373 b46a8906 ths
    int pid, status;
4374 7c9d8e07 bellard
    char *args[3];
4375 7c9d8e07 bellard
    char **parg;
4376 7c9d8e07 bellard
4377 b46a8906 ths
        /* try to launch network script */
4378 7c9d8e07 bellard
        pid = fork();
4379 7c9d8e07 bellard
        if (pid >= 0) {
4380 7c9d8e07 bellard
            if (pid == 0) {
4381 50d3eeae ths
                int open_max = sysconf (_SC_OPEN_MAX), i;
4382 50d3eeae ths
                for (i = 0; i < open_max; i++)
4383 50d3eeae ths
                    if (i != STDIN_FILENO &&
4384 50d3eeae ths
                        i != STDOUT_FILENO &&
4385 50d3eeae ths
                        i != STDERR_FILENO &&
4386 50d3eeae ths
                        i != fd)
4387 50d3eeae ths
                        close(i);
4388 50d3eeae ths
4389 7c9d8e07 bellard
                parg = args;
4390 7c9d8e07 bellard
                *parg++ = (char *)setup_script;
4391 b46a8906 ths
                *parg++ = (char *)ifname;
4392 7c9d8e07 bellard
                *parg++ = NULL;
4393 7c9d8e07 bellard
                execv(setup_script, args);
4394 4a38940d bellard
                _exit(1);
4395 7c9d8e07 bellard
            }
4396 7c9d8e07 bellard
            while (waitpid(pid, &status, 0) != pid);
4397 7c9d8e07 bellard
            if (!WIFEXITED(status) ||
4398 7c9d8e07 bellard
                WEXITSTATUS(status) != 0) {
4399 7c9d8e07 bellard
                fprintf(stderr, "%s: could not launch network script\n",
4400 7c9d8e07 bellard
                        setup_script);
4401 7c9d8e07 bellard
                return -1;
4402 7c9d8e07 bellard
            }
4403 7c9d8e07 bellard
        }
4404 b46a8906 ths
    return 0;
4405 b46a8906 ths
}
4406 b46a8906 ths
4407 b46a8906 ths
static int net_tap_init(VLANState *vlan, const char *ifname1,
4408 b46a8906 ths
                        const char *setup_script, const char *down_script)
4409 b46a8906 ths
{
4410 b46a8906 ths
    TAPState *s;
4411 b46a8906 ths
    int fd;
4412 b46a8906 ths
    char ifname[128];
4413 b46a8906 ths
4414 b46a8906 ths
    if (ifname1 != NULL)
4415 b46a8906 ths
        pstrcpy(ifname, sizeof(ifname), ifname1);
4416 b46a8906 ths
    else
4417 b46a8906 ths
        ifname[0] = '\0';
4418 b46a8906 ths
    TFR(fd = tap_open(ifname, sizeof(ifname)));
4419 b46a8906 ths
    if (fd < 0)
4420 b46a8906 ths
        return -1;
4421 b46a8906 ths
4422 b46a8906 ths
    if (!setup_script || !strcmp(setup_script, "no"))
4423 b46a8906 ths
        setup_script = "";
4424 b46a8906 ths
    if (setup_script[0] != '\0') {
4425 b46a8906 ths
        if (launch_script(setup_script, ifname, fd))
4426 b46a8906 ths
            return -1;
4427 7c9d8e07 bellard
    }
4428 7c9d8e07 bellard
    s = net_tap_fd_init(vlan, fd);
4429 7c9d8e07 bellard
    if (!s)
4430 7c9d8e07 bellard
        return -1;
4431 5fafdf24 ths
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4432 7c9d8e07 bellard
             "tap: ifname=%s setup_script=%s", ifname, setup_script);
4433 b46a8906 ths
    if (down_script && strcmp(down_script, "no"))
4434 b46a8906 ths
        snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
4435 7c9d8e07 bellard
    return 0;
4436 7c9d8e07 bellard
}
4437 7c9d8e07 bellard
4438 fd1dff4b bellard
#endif /* !_WIN32 */
4439 fd1dff4b bellard
4440 8a16d273 ths
#if defined(CONFIG_VDE)
4441 8a16d273 ths
typedef struct VDEState {
4442 8a16d273 ths
    VLANClientState *vc;
4443 8a16d273 ths
    VDECONN *vde;
4444 8a16d273 ths
} VDEState;
4445 8a16d273 ths
4446 8a16d273 ths
static void vde_to_qemu(void *opaque)
4447 8a16d273 ths
{
4448 8a16d273 ths
    VDEState *s = opaque;
4449 8a16d273 ths
    uint8_t buf[4096];
4450 8a16d273 ths
    int size;
4451 8a16d273 ths
4452 8a16d273 ths
    size = vde_recv(s->vde, buf, sizeof(buf), 0);
4453 8a16d273 ths
    if (size > 0) {
4454 8a16d273 ths
        qemu_send_packet(s->vc, buf, size);
4455 8a16d273 ths
    }
4456 8a16d273 ths
}
4457 8a16d273 ths
4458 8a16d273 ths
static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
4459 8a16d273 ths
{
4460 8a16d273 ths
    VDEState *s = opaque;
4461 8a16d273 ths
    int ret;
4462 8a16d273 ths
    for(;;) {
4463 8a16d273 ths
        ret = vde_send(s->vde, buf, size, 0);
4464 8a16d273 ths
        if (ret < 0 && errno == EINTR) {
4465 8a16d273 ths
        } else {
4466 8a16d273 ths
            break;
4467 8a16d273 ths
        }
4468 8a16d273 ths
    }
4469 8a16d273 ths
}
4470 8a16d273 ths
4471 8a16d273 ths
static int net_vde_init(VLANState *vlan, const char *sock, int port,
4472 8a16d273 ths
                        const char *group, int mode)
4473 8a16d273 ths
{
4474 8a16d273 ths
    VDEState *s;
4475 8a16d273 ths
    char *init_group = strlen(group) ? (char *)group : NULL;
4476 8a16d273 ths
    char *init_sock = strlen(sock) ? (char *)sock : NULL;
4477 8a16d273 ths
4478 8a16d273 ths
    struct vde_open_args args = {
4479 8a16d273 ths
        .port = port,
4480 8a16d273 ths
        .group = init_group,
4481 8a16d273 ths
        .mode = mode,
4482 8a16d273 ths
    };
4483 8a16d273 ths
4484 8a16d273 ths
    s = qemu_mallocz(sizeof(VDEState));
4485 8a16d273 ths
    if (!s)
4486 8a16d273 ths
        return -1;
4487 8a16d273 ths
    s->vde = vde_open(init_sock, "QEMU", &args);
4488 8a16d273 ths
    if (!s->vde){
4489 8a16d273 ths
        free(s);
4490 8a16d273 ths
        return -1;
4491 8a16d273 ths
    }
4492 8a16d273 ths
    s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
4493 8a16d273 ths
    qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
4494 8a16d273 ths
    snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d",
4495 8a16d273 ths
             sock, vde_datafd(s->vde));
4496 8a16d273 ths
    return 0;
4497 8a16d273 ths
}
4498 8a16d273 ths
#endif
4499 8a16d273 ths
4500 7c9d8e07 bellard
/* network connection */
4501 7c9d8e07 bellard
typedef struct NetSocketState {
4502 7c9d8e07 bellard
    VLANClientState *vc;
4503 7c9d8e07 bellard
    int fd;
4504 7c9d8e07 bellard
    int state; /* 0 = getting length, 1 = getting data */
4505 7c9d8e07 bellard
    int index;
4506 7c9d8e07 bellard
    int packet_len;
4507 7c9d8e07 bellard
    uint8_t buf[4096];
4508 3d830459 bellard
    struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
4509 7c9d8e07 bellard
} NetSocketState;
4510 7c9d8e07 bellard
4511 7c9d8e07 bellard
typedef struct NetSocketListenState {
4512 7c9d8e07 bellard
    VLANState *vlan;
4513 7c9d8e07 bellard
    int fd;
4514 7c9d8e07 bellard
} NetSocketListenState;
4515 7c9d8e07 bellard
4516 7c9d8e07 bellard
/* XXX: we consider we can send the whole packet without blocking */
4517 7c9d8e07 bellard
static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
4518 c20709aa bellard
{
4519 7c9d8e07 bellard
    NetSocketState *s = opaque;
4520 7c9d8e07 bellard
    uint32_t len;
4521 7c9d8e07 bellard
    len = htonl(size);
4522 7c9d8e07 bellard
4523 fd1dff4b bellard
    send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4524 fd1dff4b bellard
    send_all(s->fd, buf, size);
4525 c20709aa bellard
}
4526 c20709aa bellard
4527 3d830459 bellard
static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
4528 3d830459 bellard
{
4529 3d830459 bellard
    NetSocketState *s = opaque;
4530 5fafdf24 ths
    sendto(s->fd, buf, size, 0,
4531 3d830459 bellard
           (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
4532 3d830459 bellard
}
4533 3d830459 bellard
4534 7c9d8e07 bellard
static void net_socket_send(void *opaque)
4535 c4b1fcc0 bellard
{
4536 7c9d8e07 bellard
    NetSocketState *s = opaque;
4537 fd1dff4b bellard
    int l, size, err;
4538 7c9d8e07 bellard
    uint8_t buf1[4096];
4539 7c9d8e07 bellard
    const uint8_t *buf;
4540 7c9d8e07 bellard
4541 fd1dff4b bellard
    size = recv(s->fd, buf1, sizeof(buf1), 0);
4542 fd1dff4b bellard
    if (size < 0) {
4543 fd1dff4b bellard
        err = socket_error();
4544 5fafdf24 ths
        if (err != EWOULDBLOCK)
4545 fd1dff4b bellard
            goto eoc;
4546 fd1dff4b bellard
    } else if (size == 0) {
4547 7c9d8e07 bellard
        /* end of connection */
4548 fd1dff4b bellard
    eoc:
4549 7c9d8e07 bellard
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4550 fd1dff4b bellard
        closesocket(s->fd);
4551 7c9d8e07 bellard
        return;
4552 7c9d8e07 bellard
    }
4553 7c9d8e07 bellard
    buf = buf1;
4554 7c9d8e07 bellard
    while (size > 0) {
4555 7c9d8e07 bellard
        /* reassemble a packet from the network */
4556 7c9d8e07 bellard
        switch(s->state) {
4557 7c9d8e07 bellard
        case 0:
4558 7c9d8e07 bellard
            l = 4 - s->index;
4559 7c9d8e07 bellard
            if (l > size)
4560 7c9d8e07 bellard
                l = size;
4561 7c9d8e07 bellard
            memcpy(s->buf + s->index, buf, l);
4562 7c9d8e07 bellard
            buf += l;
4563 7c9d8e07 bellard
            size -= l;
4564 7c9d8e07 bellard
            s->index += l;
4565 7c9d8e07 bellard
            if (s->index == 4) {
4566 7c9d8e07 bellard
                /* got length */
4567 7c9d8e07 bellard
                s->packet_len = ntohl(*(uint32_t *)s->buf);
4568 7c9d8e07 bellard
                s->index = 0;
4569 7c9d8e07 bellard
                s->state = 1;
4570 7c9d8e07 bellard
            }
4571 7c9d8e07 bellard
            break;
4572 7c9d8e07 bellard
        case 1:
4573 7c9d8e07 bellard
            l = s->packet_len - s->index;
4574 7c9d8e07 bellard
            if (l > size)
4575 7c9d8e07 bellard
                l = size;
4576 7c9d8e07 bellard
            memcpy(s->buf + s->index, buf, l);
4577 7c9d8e07 bellard
            s->index += l;
4578 7c9d8e07 bellard
            buf += l;
4579 7c9d8e07 bellard
            size -= l;
4580 7c9d8e07 bellard
            if (s->index >= s->packet_len) {
4581 7c9d8e07 bellard
                qemu_send_packet(s->vc, s->buf, s->packet_len);
4582 7c9d8e07 bellard
                s->index = 0;
4583 7c9d8e07 bellard
                s->state = 0;
4584 7c9d8e07 bellard
            }
4585 7c9d8e07 bellard
            break;
4586 7c9d8e07 bellard
        }
4587 7c9d8e07 bellard
    }
4588 c20709aa bellard
}
4589 c20709aa bellard
4590 3d830459 bellard
static void net_socket_send_dgram(void *opaque)
4591 3d830459 bellard
{
4592 3d830459 bellard
    NetSocketState *s = opaque;
4593 3d830459 bellard
    int size;
4594 3d830459 bellard
4595 3d830459 bellard
    size = recv(s->fd, s->buf, sizeof(s->buf), 0);
4596 5fafdf24 ths
    if (size < 0)
4597 3d830459 bellard
        return;
4598 3d830459 bellard
    if (size == 0) {
4599 3d830459 bellard
        /* end of connection */
4600 3d830459 bellard
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
4601 3d830459 bellard
        return;
4602 3d830459 bellard
    }
4603 3d830459 bellard
    qemu_send_packet(s->vc, s->buf, size);
4604 3d830459 bellard
}
4605 3d830459 bellard
4606 3d830459 bellard
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
4607 3d830459 bellard
{
4608 3d830459 bellard
    struct ip_mreq imr;
4609 3d830459 bellard
    int fd;
4610 3d830459 bellard
    int val, ret;
4611 3d830459 bellard
    if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
4612 3d830459 bellard
        fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
4613 5fafdf24 ths
                inet_ntoa(mcastaddr->sin_addr),
4614 fd1dff4b bellard
                (int)ntohl(mcastaddr->sin_addr.s_addr));
4615 3d830459 bellard
        return -1;
4616 3d830459 bellard
4617 3d830459 bellard
    }
4618 3d830459 bellard
    fd = socket(PF_INET, SOCK_DGRAM, 0);
4619 3d830459 bellard
    if (fd < 0) {
4620 3d830459 bellard
        perror("socket(PF_INET, SOCK_DGRAM)");
4621 3d830459 bellard
        return -1;
4622 3d830459 bellard
    }
4623 3d830459 bellard
4624 fd1dff4b bellard
    val = 1;
4625 5fafdf24 ths
    ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
4626 fd1dff4b bellard
                   (const char *)&val, sizeof(val));
4627 fd1dff4b bellard
    if (ret < 0) {
4628 fd1dff4b bellard
        perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
4629 fd1dff4b bellard
        goto fail;
4630 fd1dff4b bellard
    }
4631 fd1dff4b bellard
4632 fd1dff4b bellard
    ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
4633 fd1dff4b bellard
    if (ret < 0) {
4634 fd1dff4b bellard
        perror("bind");
4635 fd1dff4b bellard
        goto fail;
4636 fd1dff4b bellard
    }
4637 3b46e624 ths
4638 3d830459 bellard
    /* Add host to multicast group */
4639 3d830459 bellard
    imr.imr_multiaddr = mcastaddr->sin_addr;
4640 3d830459 bellard
    imr.imr_interface.s_addr = htonl(INADDR_ANY);
4641 3d830459 bellard
4642 5fafdf24 ths
    ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
4643 fd1dff4b bellard
                     (const char *)&imr, sizeof(struct ip_mreq));
4644 3d830459 bellard
    if (ret < 0) {
4645 3d830459 bellard
        perror("setsockopt(IP_ADD_MEMBERSHIP)");
4646 3d830459 bellard
        goto fail;
4647 3d830459 bellard
    }
4648 3d830459 bellard
4649 3d830459 bellard
    /* Force mcast msgs to loopback (eg. several QEMUs in same host */
4650 3d830459 bellard
    val = 1;
4651 5fafdf24 ths
    ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
4652 fd1dff4b bellard
                   (const char *)&val, sizeof(val));
4653 3d830459 bellard
    if (ret < 0) {
4654 3d830459 bellard
        perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
4655 3d830459 bellard
        goto fail;
4656 3d830459 bellard
    }
4657 3d830459 bellard
4658 fd1dff4b bellard
    socket_set_nonblock(fd);
4659 3d830459 bellard
    return fd;
4660 3d830459 bellard
fail:
4661 5fafdf24 ths
    if (fd >= 0)
4662 0bab00f3 bellard
        closesocket(fd);
4663 3d830459 bellard
    return -1;
4664 3d830459 bellard
}
4665 3d830459 bellard
4666 5fafdf24 ths
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd,
4667 3d830459 bellard
                                          int is_connected)
4668 3d830459 bellard
{
4669 3d830459 bellard
    struct sockaddr_in saddr;
4670 3d830459 bellard
    int newfd;
4671 3d830459 bellard
    socklen_t saddr_len;
4672 3d830459 bellard
    NetSocketState *s;
4673 3d830459 bellard
4674 3d830459 bellard
    /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
4675 5fafdf24 ths
     * Because this may be "shared" socket from a "master" process, datagrams would be recv()
4676 3d830459 bellard
     * by ONLY ONE process: we must "clone" this dgram socket --jjo
4677 3d830459 bellard
     */
4678 3d830459 bellard
4679 3d830459 bellard
    if (is_connected) {
4680 3d830459 bellard
        if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
4681 3d830459 bellard
            /* must be bound */
4682 3d830459 bellard
            if (saddr.sin_addr.s_addr==0) {
4683 3d830459 bellard
                fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
4684 3d830459 bellard
                        fd);
4685 3d830459 bellard
                return NULL;
4686 3d830459 bellard
            }
4687 3d830459 bellard
            /* clone dgram socket */
4688 3d830459 bellard
            newfd = net_socket_mcast_create(&saddr);
4689 3d830459 bellard
            if (newfd < 0) {
4690 3d830459 bellard
                /* error already reported by net_socket_mcast_create() */
4691 3d830459 bellard
                close(fd);
4692 3d830459 bellard
                return NULL;
4693 3d830459 bellard
            }
4694 3d830459 bellard
            /* clone newfd to fd, close newfd */
4695 3d830459 bellard
            dup2(newfd, fd);
4696 3d830459 bellard
            close(newfd);
4697 5fafdf24 ths
4698 3d830459 bellard
        } else {
4699 3d830459 bellard
            fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
4700 3d830459 bellard
                    fd, strerror(errno));
4701 3d830459 bellard
            return NULL;
4702 3d830459 bellard
        }
4703 3d830459 bellard
    }
4704 3d830459 bellard
4705 3d830459 bellard
    s = qemu_mallocz(sizeof(NetSocketState));
4706 3d830459 bellard
    if (!s)
4707 3d830459 bellard
        return NULL;
4708 3d830459 bellard
    s->fd = fd;
4709 3d830459 bellard
4710 d861b05e pbrook
    s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
4711 3d830459 bellard
    qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
4712 3d830459 bellard
4713 3d830459 bellard
    /* mcast: save bound address as dst */
4714 3d830459 bellard
    if (is_connected) s->dgram_dst=saddr;
4715 3d830459 bellard
4716 3d830459 bellard
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4717 5fafdf24 ths
            "socket: fd=%d (%s mcast=%s:%d)",
4718 3d830459 bellard
            fd, is_connected? "cloned" : "",
4719 3d830459 bellard
            inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4720 3d830459 bellard
    return s;
4721 3d830459 bellard
}
4722 3d830459 bellard
4723 7c9d8e07 bellard
static void net_socket_connect(void *opaque)
4724 c20709aa bellard
{
4725 7c9d8e07 bellard
    NetSocketState *s = opaque;
4726 7c9d8e07 bellard
    qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
4727 7c9d8e07 bellard
}
4728 c4b1fcc0 bellard
4729 5fafdf24 ths
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd,
4730 7c9d8e07 bellard
                                          int is_connected)
4731 7c9d8e07 bellard
{
4732 7c9d8e07 bellard
    NetSocketState *s;
4733 7c9d8e07 bellard
    s = qemu_mallocz(sizeof(NetSocketState));
4734 7c9d8e07 bellard
    if (!s)
4735 7c9d8e07 bellard
        return NULL;
4736 7c9d8e07 bellard
    s->fd = fd;
4737 5fafdf24 ths
    s->vc = qemu_new_vlan_client(vlan,
4738 d861b05e pbrook
                                 net_socket_receive, NULL, s);
4739 7c9d8e07 bellard
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4740 7c9d8e07 bellard
             "socket: fd=%d", fd);
4741 7c9d8e07 bellard
    if (is_connected) {
4742 7c9d8e07 bellard
        net_socket_connect(s);
4743 7c9d8e07 bellard
    } else {
4744 7c9d8e07 bellard
        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
4745 7c9d8e07 bellard
    }
4746 7c9d8e07 bellard
    return s;
4747 7c9d8e07 bellard
}
4748 c4b1fcc0 bellard
4749 5fafdf24 ths
static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd,
4750 3d830459 bellard
                                          int is_connected)
4751 3d830459 bellard
{
4752 3d830459 bellard
    int so_type=-1, optlen=sizeof(so_type);
4753 3d830459 bellard
4754 69b34976 ths
    if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
4755 69b34976 ths
        (socklen_t *)&optlen)< 0) {
4756 931f03ee ths
        fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
4757 3d830459 bellard
        return NULL;
4758 3d830459 bellard
    }
4759 3d830459 bellard
    switch(so_type) {
4760 3d830459 bellard
    case SOCK_DGRAM:
4761 3d830459 bellard
        return net_socket_fd_init_dgram(vlan, fd, is_connected);
4762 3d830459 bellard
    case SOCK_STREAM:
4763 3d830459 bellard
        return net_socket_fd_init_stream(vlan, fd, is_connected);
4764 3d830459 bellard
    default:
4765 3d830459 bellard
        /* who knows ... this could be a eg. a pty, do warn and continue as stream */
4766 3d830459 bellard
        fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
4767 3d830459 bellard
        return net_socket_fd_init_stream(vlan, fd, is_connected);
4768 3d830459 bellard
    }
4769 3d830459 bellard
    return NULL;
4770 3d830459 bellard
}
4771 3d830459 bellard
4772 7c9d8e07 bellard
static void net_socket_accept(void *opaque)
4773 7c9d8e07 bellard
{
4774 3b46e624 ths
    NetSocketListenState *s = opaque;
4775 7c9d8e07 bellard
    NetSocketState *s1;
4776 7c9d8e07 bellard
    struct sockaddr_in saddr;
4777 7c9d8e07 bellard
    socklen_t len;
4778 7c9d8e07 bellard
    int fd;
4779 7c9d8e07 bellard
4780 7c9d8e07 bellard
    for(;;) {
4781 7c9d8e07 bellard
        len = sizeof(saddr);
4782 7c9d8e07 bellard
        fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
4783 7c9d8e07 bellard
        if (fd < 0 && errno != EINTR) {
4784 7c9d8e07 bellard
            return;
4785 7c9d8e07 bellard
        } else if (fd >= 0) {
4786 7c9d8e07 bellard
            break;
4787 80cabfad bellard
        }
4788 330d0414 bellard
    }
4789 5fafdf24 ths
    s1 = net_socket_fd_init(s->vlan, fd, 1);
4790 7c9d8e07 bellard
    if (!s1) {
4791 0bab00f3 bellard
        closesocket(fd);
4792 7c9d8e07 bellard
    } else {
4793 7c9d8e07 bellard
        snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
4794 5fafdf24 ths
                 "socket: connection from %s:%d",
4795 7c9d8e07 bellard
                 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4796 7c9d8e07 bellard
    }
4797 7c9d8e07 bellard
}
4798 7c9d8e07 bellard
4799 7c9d8e07 bellard
static int net_socket_listen_init(VLANState *vlan, const char *host_str)
4800 7c9d8e07 bellard
{
4801 7c9d8e07 bellard
    NetSocketListenState *s;
4802 7c9d8e07 bellard
    int fd, val, ret;
4803 7c9d8e07 bellard
    struct sockaddr_in saddr;
4804 7c9d8e07 bellard
4805 7c9d8e07 bellard
    if (parse_host_port(&saddr, host_str) < 0)
4806 7c9d8e07 bellard
        return -1;
4807 3b46e624 ths
4808 7c9d8e07 bellard
    s = qemu_mallocz(sizeof(NetSocketListenState));
4809 7c9d8e07 bellard
    if (!s)
4810 7c9d8e07 bellard
        return -1;
4811 7c9d8e07 bellard
4812 7c9d8e07 bellard
    fd = socket(PF_INET, SOCK_STREAM, 0);
4813 7c9d8e07 bellard
    if (fd < 0) {
4814 7c9d8e07 bellard
        perror("socket");
4815 7c9d8e07 bellard
        return -1;
4816 7c9d8e07 bellard
    }
4817 fd1dff4b bellard
    socket_set_nonblock(fd);
4818 7c9d8e07 bellard
4819 7c9d8e07 bellard
    /* allow fast reuse */
4820 7c9d8e07 bellard
    val = 1;
4821 fd1dff4b bellard
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
4822 3b46e624 ths
4823 7c9d8e07 bellard
    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4824 7c9d8e07 bellard
    if (ret < 0) {
4825 7c9d8e07 bellard
        perror("bind");
4826 7c9d8e07 bellard
        return -1;
4827 7c9d8e07 bellard
    }
4828 7c9d8e07 bellard
    ret = listen(fd, 0);
4829 7c9d8e07 bellard
    if (ret < 0) {
4830 7c9d8e07 bellard
        perror("listen");
4831 7c9d8e07 bellard
        return -1;
4832 7c9d8e07 bellard
    }
4833 7c9d8e07 bellard
    s->vlan = vlan;
4834 7c9d8e07 bellard
    s->fd = fd;
4835 7c9d8e07 bellard
    qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
4836 80cabfad bellard
    return 0;
4837 330d0414 bellard
}
4838 330d0414 bellard
4839 7c9d8e07 bellard
static int net_socket_connect_init(VLANState *vlan, const char *host_str)
4840 330d0414 bellard
{
4841 7c9d8e07 bellard
    NetSocketState *s;
4842 fd1dff4b bellard
    int fd, connected, ret, err;
4843 7c9d8e07 bellard
    struct sockaddr_in saddr;
4844 7c9d8e07 bellard
4845 7c9d8e07 bellard
    if (parse_host_port(&saddr, host_str) < 0)
4846 7c9d8e07 bellard
        return -1;
4847 7c9d8e07 bellard
4848 7c9d8e07 bellard
    fd = socket(PF_INET, SOCK_STREAM, 0);
4849 7c9d8e07 bellard
    if (fd < 0) {
4850 7c9d8e07 bellard
        perror("socket");
4851 7c9d8e07 bellard
        return -1;
4852 7c9d8e07 bellard
    }
4853 fd1dff4b bellard
    socket_set_nonblock(fd);
4854 7c9d8e07 bellard
4855 7c9d8e07 bellard
    connected = 0;
4856 7c9d8e07 bellard
    for(;;) {
4857 7c9d8e07 bellard
        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
4858 7c9d8e07 bellard
        if (ret < 0) {
4859 fd1dff4b bellard
            err = socket_error();
4860 fd1dff4b bellard
            if (err == EINTR || err == EWOULDBLOCK) {
4861 fd1dff4b bellard
            } else if (err == EINPROGRESS) {
4862 7c9d8e07 bellard
                break;
4863 f5b12268 ths
#ifdef _WIN32
4864 f5b12268 ths
            } else if (err == WSAEALREADY) {
4865 f5b12268 ths
                break;
4866 f5b12268 ths
#endif
4867 7c9d8e07 bellard
            } else {
4868 7c9d8e07 bellard
                perror("connect");
4869 fd1dff4b bellard
                closesocket(fd);
4870 7c9d8e07 bellard
                return -1;
4871 7c9d8e07 bellard
            }
4872 7c9d8e07 bellard
        } else {
4873 7c9d8e07 bellard
            connected = 1;
4874 7c9d8e07 bellard
            break;
4875 7c9d8e07 bellard
        }
4876 7c9d8e07 bellard
    }
4877 7c9d8e07 bellard
    s = net_socket_fd_init(vlan, fd, connected);
4878 7c9d8e07 bellard
    if (!s)
4879 7c9d8e07 bellard
        return -1;
4880 7c9d8e07 bellard
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4881 5fafdf24 ths
             "socket: connect to %s:%d",
4882 7c9d8e07 bellard
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4883 c20709aa bellard
    return 0;
4884 80cabfad bellard
}
4885 330d0414 bellard
4886 3d830459 bellard
static int net_socket_mcast_init(VLANState *vlan, const char *host_str)
4887 3d830459 bellard
{
4888 3d830459 bellard
    NetSocketState *s;
4889 3d830459 bellard
    int fd;
4890 3d830459 bellard
    struct sockaddr_in saddr;
4891 3d830459 bellard
4892 3d830459 bellard
    if (parse_host_port(&saddr, host_str) < 0)
4893 3d830459 bellard
        return -1;
4894 3d830459 bellard
4895 3d830459 bellard
4896 3d830459 bellard
    fd = net_socket_mcast_create(&saddr);
4897 3d830459 bellard
    if (fd < 0)
4898 3d830459 bellard
        return -1;
4899 3d830459 bellard
4900 3d830459 bellard
    s = net_socket_fd_init(vlan, fd, 0);
4901 3d830459 bellard
    if (!s)
4902 3d830459 bellard
        return -1;
4903 3d830459 bellard
4904 3d830459 bellard
    s->dgram_dst = saddr;
4905 3b46e624 ths
4906 3d830459 bellard
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
4907 5fafdf24 ths
             "socket: mcast=%s:%d",
4908 3d830459 bellard
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
4909 3d830459 bellard
    return 0;
4910 3d830459 bellard
4911 3d830459 bellard
}
4912 3d830459 bellard
4913 609497ab balrog
static const char *get_opt_name(char *buf, int buf_size, const char *p)
4914 609497ab balrog
{
4915 609497ab balrog
    char *q;
4916 609497ab balrog
4917 609497ab balrog
    q = buf;
4918 609497ab balrog
    while (*p != '\0' && *p != '=') {
4919 609497ab balrog
        if (q && (q - buf) < buf_size - 1)
4920 609497ab balrog
            *q++ = *p;
4921 609497ab balrog
        p++;
4922 609497ab balrog
    }
4923 609497ab balrog
    if (q)
4924 609497ab balrog
        *q = '\0';
4925 609497ab balrog
4926 609497ab balrog
    return p;
4927 609497ab balrog
}
4928 609497ab balrog
4929 609497ab balrog
static const char *get_opt_value(char *buf, int buf_size, const char *p)
4930 e4bcb14c ths
{
4931 e4bcb14c ths
    char *q;
4932 e4bcb14c ths
4933 e4bcb14c ths
    q = buf;
4934 e4bcb14c ths
    while (*p != '\0') {
4935 609497ab balrog
        if (*p == ',') {
4936 609497ab balrog
            if (*(p + 1) != ',')
4937 e4bcb14c ths
                break;
4938 e4bcb14c ths
            p++;
4939 609497ab balrog
        }
4940 e4bcb14c ths
        if (q && (q - buf) < buf_size - 1)
4941 e4bcb14c ths
            *q++ = *p;
4942 e4bcb14c ths
        p++;
4943 e4bcb14c ths
    }
4944 e4bcb14c ths
    if (q)
4945 e4bcb14c ths
        *q = '\0';
4946 e4bcb14c ths
4947 e4bcb14c ths
    return p;
4948 e4bcb14c ths
}
4949 e4bcb14c ths
4950 7c9d8e07 bellard
static int get_param_value(char *buf, int buf_size,
4951 7c9d8e07 bellard
                           const char *tag, const char *str)
4952 7c9d8e07 bellard
{
4953 7c9d8e07 bellard
    const char *p;
4954 7c9d8e07 bellard
    char option[128];
4955 7c9d8e07 bellard
4956 7c9d8e07 bellard
    p = str;
4957 7c9d8e07 bellard
    for(;;) {
4958 609497ab balrog
        p = get_opt_name(option, sizeof(option), p);
4959 7c9d8e07 bellard
        if (*p != '=')
4960 7c9d8e07 bellard
            break;
4961 7c9d8e07 bellard
        p++;
4962 7c9d8e07 bellard
        if (!strcmp(tag, option)) {
4963 609497ab balrog
            (void)get_opt_value(buf, buf_size, p);
4964 e4bcb14c ths
            return strlen(buf);
4965 7c9d8e07 bellard
        } else {
4966 609497ab balrog
            p = get_opt_value(NULL, 0, p);
4967 7c9d8e07 bellard
        }
4968 7c9d8e07 bellard
        if (*p != ',')
4969 7c9d8e07 bellard
            break;
4970 7c9d8e07 bellard
        p++;
4971 7c9d8e07 bellard
    }
4972 7c9d8e07 bellard
    return 0;
4973 7c9d8e07 bellard
}
4974 7c9d8e07 bellard
4975 e4bcb14c ths
static int check_params(char *buf, int buf_size,
4976 e4bcb14c ths
                        char **params, const char *str)
4977 e4bcb14c ths
{
4978 e4bcb14c ths
    const char *p;
4979 e4bcb14c ths
    int i;
4980 e4bcb14c ths
4981 e4bcb14c ths
    p = str;
4982 e4bcb14c ths
    for(;;) {
4983 609497ab balrog
        p = get_opt_name(buf, buf_size, p);
4984 e4bcb14c ths
        if (*p != '=')
4985 e4bcb14c ths
            return -1;
4986 e4bcb14c ths
        p++;
4987 e4bcb14c ths
        for(i = 0; params[i] != NULL; i++)
4988 e4bcb14c ths
            if (!strcmp(params[i], buf))
4989 e4bcb14c ths
                break;
4990 e4bcb14c ths
        if (params[i] == NULL)
4991 e4bcb14c ths
            return -1;
4992 609497ab balrog
        p = get_opt_value(NULL, 0, p);
4993 e4bcb14c ths
        if (*p != ',')
4994 e4bcb14c ths
            break;
4995 e4bcb14c ths
        p++;
4996 e4bcb14c ths
    }
4997 e4bcb14c ths
    return 0;
4998 e4bcb14c ths
}
4999 e4bcb14c ths
5000 e4bcb14c ths
5001 52f61fde ths
static int net_client_init(const char *str)
5002 7c9d8e07 bellard
{
5003 7c9d8e07 bellard
    const char *p;
5004 7c9d8e07 bellard
    char *q;
5005 7c9d8e07 bellard
    char device[64];
5006 7c9d8e07 bellard
    char buf[1024];
5007 7c9d8e07 bellard
    int vlan_id, ret;
5008 7c9d8e07 bellard
    VLANState *vlan;
5009 7c9d8e07 bellard
5010 7c9d8e07 bellard
    p = str;
5011 7c9d8e07 bellard
    q = device;
5012 7c9d8e07 bellard
    while (*p != '\0' && *p != ',') {
5013 7c9d8e07 bellard
        if ((q - device) < sizeof(device) - 1)
5014 7c9d8e07 bellard
            *q++ = *p;
5015 7c9d8e07 bellard
        p++;
5016 7c9d8e07 bellard
    }
5017 7c9d8e07 bellard
    *q = '\0';
5018 7c9d8e07 bellard
    if (*p == ',')
5019 7c9d8e07 bellard
        p++;
5020 7c9d8e07 bellard
    vlan_id = 0;
5021 7c9d8e07 bellard
    if (get_param_value(buf, sizeof(buf), "vlan", p)) {
5022 7c9d8e07 bellard
        vlan_id = strtol(buf, NULL, 0);
5023 7c9d8e07 bellard
    }
5024 7c9d8e07 bellard
    vlan = qemu_find_vlan(vlan_id);
5025 7c9d8e07 bellard
    if (!vlan) {
5026 7c9d8e07 bellard
        fprintf(stderr, "Could not create vlan %d\n", vlan_id);
5027 7c9d8e07 bellard
        return -1;
5028 7c9d8e07 bellard
    }
5029 7c9d8e07 bellard
    if (!strcmp(device, "nic")) {
5030 7c9d8e07 bellard
        NICInfo *nd;
5031 7c9d8e07 bellard
        uint8_t *macaddr;
5032 7c9d8e07 bellard
5033 7c9d8e07 bellard
        if (nb_nics >= MAX_NICS) {
5034 7c9d8e07 bellard
            fprintf(stderr, "Too Many NICs\n");
5035 7c9d8e07 bellard
            return -1;
5036 7c9d8e07 bellard
        }
5037 7c9d8e07 bellard
        nd = &nd_table[nb_nics];
5038 7c9d8e07 bellard
        macaddr = nd->macaddr;
5039 7c9d8e07 bellard
        macaddr[0] = 0x52;
5040 7c9d8e07 bellard
        macaddr[1] = 0x54;
5041 7c9d8e07 bellard
        macaddr[2] = 0x00;
5042 7c9d8e07 bellard
        macaddr[3] = 0x12;
5043 7c9d8e07 bellard
        macaddr[4] = 0x34;
5044 7c9d8e07 bellard
        macaddr[5] = 0x56 + nb_nics;
5045 7c9d8e07 bellard
5046 7c9d8e07 bellard
        if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
5047 7c9d8e07 bellard
            if (parse_macaddr(macaddr, buf) < 0) {
5048 7c9d8e07 bellard
                fprintf(stderr, "invalid syntax for ethernet address\n");
5049 7c9d8e07 bellard
                return -1;
5050 7c9d8e07 bellard
            }
5051 7c9d8e07 bellard
        }
5052 a41b2ff2 pbrook
        if (get_param_value(buf, sizeof(buf), "model", p)) {
5053 a41b2ff2 pbrook
            nd->model = strdup(buf);
5054 a41b2ff2 pbrook
        }
5055 7c9d8e07 bellard
        nd->vlan = vlan;
5056 7c9d8e07 bellard
        nb_nics++;
5057 833c7174 blueswir1
        vlan->nb_guest_devs++;
5058 7c9d8e07 bellard
        ret = 0;
5059 7c9d8e07 bellard
    } else
5060 7c9d8e07 bellard
    if (!strcmp(device, "none")) {
5061 7c9d8e07 bellard
        /* does nothing. It is needed to signal that no network cards
5062 7c9d8e07 bellard
           are wanted */
5063 7c9d8e07 bellard
        ret = 0;
5064 7c9d8e07 bellard
    } else
5065 7c9d8e07 bellard
#ifdef CONFIG_SLIRP
5066 7c9d8e07 bellard
    if (!strcmp(device, "user")) {
5067 115defd1 pbrook
        if (get_param_value(buf, sizeof(buf), "hostname", p)) {
5068 3f423c9c bellard
            pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
5069 115defd1 pbrook
        }
5070 833c7174 blueswir1
        vlan->nb_host_devs++;
5071 7c9d8e07 bellard
        ret = net_slirp_init(vlan);
5072 7c9d8e07 bellard
    } else
5073 7c9d8e07 bellard
#endif
5074 7fb843f8 bellard
#ifdef _WIN32
5075 7fb843f8 bellard
    if (!strcmp(device, "tap")) {
5076 7fb843f8 bellard
        char ifname[64];
5077 7fb843f8 bellard
        if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
5078 7fb843f8 bellard
            fprintf(stderr, "tap: no interface name\n");
5079 7fb843f8 bellard
            return -1;
5080 7fb843f8 bellard
        }
5081 833c7174 blueswir1
        vlan->nb_host_devs++;
5082 7fb843f8 bellard
        ret = tap_win32_init(vlan, ifname);
5083 7fb843f8 bellard
    } else
5084 7fb843f8 bellard
#else
5085 7c9d8e07 bellard
    if (!strcmp(device, "tap")) {
5086 7c9d8e07 bellard
        char ifname[64];
5087 b46a8906 ths
        char setup_script[1024], down_script[1024];
5088 7c9d8e07 bellard
        int fd;
5089 4f01035f pbrook
        vlan->nb_host_devs++;
5090 7c9d8e07 bellard
        if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
5091 7c9d8e07 bellard
            fd = strtol(buf, NULL, 0);
5092 64538cdf pbrook
            fcntl(fd, F_SETFL, O_NONBLOCK);
5093 7c9d8e07 bellard
            ret = -1;
5094 7c9d8e07 bellard
            if (net_tap_fd_init(vlan, fd))
5095 7c9d8e07 bellard
                ret = 0;
5096 7c9d8e07 bellard
        } else {
5097 bf8c5342 bellard
            if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
5098 bf8c5342 bellard
                ifname[0] = '\0';
5099 bf8c5342 bellard
            }
5100 7c9d8e07 bellard
            if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
5101 7c9d8e07 bellard
                pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
5102 7c9d8e07 bellard
            }
5103 b46a8906 ths
            if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
5104 b46a8906 ths
                pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
5105 b46a8906 ths
            }
5106 b46a8906 ths
            ret = net_tap_init(vlan, ifname, setup_script, down_script);
5107 7c9d8e07 bellard
        }
5108 7c9d8e07 bellard
    } else
5109 fd1dff4b bellard
#endif
5110 7c9d8e07 bellard
    if (!strcmp(device, "socket")) {
5111 7c9d8e07 bellard
        if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
5112 7c9d8e07 bellard
            int fd;
5113 7c9d8e07 bellard
            fd = strtol(buf, NULL, 0);
5114 7c9d8e07 bellard
            ret = -1;
5115 7c9d8e07 bellard
            if (net_socket_fd_init(vlan, fd, 1))
5116 7c9d8e07 bellard
                ret = 0;
5117 7c9d8e07 bellard
        } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
5118 7c9d8e07 bellard
            ret = net_socket_listen_init(vlan, buf);
5119 7c9d8e07 bellard
        } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
5120 7c9d8e07 bellard
            ret = net_socket_connect_init(vlan, buf);
5121 3d830459 bellard
        } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
5122 3d830459 bellard
            ret = net_socket_mcast_init(vlan, buf);
5123 7c9d8e07 bellard
        } else {
5124 7c9d8e07 bellard
            fprintf(stderr, "Unknown socket options: %s\n", p);
5125 7c9d8e07 bellard
            return -1;
5126 7c9d8e07 bellard
        }
5127 833c7174 blueswir1
        vlan->nb_host_devs++;
5128 7c9d8e07 bellard
    } else
5129 8a16d273 ths
#ifdef CONFIG_VDE
5130 8a16d273 ths
    if (!strcmp(device, "vde")) {
5131 8a16d273 ths
        char vde_sock[1024], vde_group[512];
5132 8a16d273 ths
        int vde_port, vde_mode;
5133 8a16d273 ths
        vlan->nb_host_devs++;
5134 8a16d273 ths
        if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
5135 8a16d273 ths
            vde_sock[0] = '\0';
5136 8a16d273 ths
        }
5137 8a16d273 ths
        if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
5138 8a16d273 ths
            vde_port = strtol(buf, NULL, 10);
5139 8a16d273 ths
        } else {
5140 8a16d273 ths
            vde_port = 0;
5141 8a16d273 ths
        }
5142 8a16d273 ths
        if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
5143 8a16d273 ths
            vde_group[0] = '\0';
5144 8a16d273 ths
        }
5145 8a16d273 ths
        if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
5146 8a16d273 ths
            vde_mode = strtol(buf, NULL, 8);
5147 8a16d273 ths
        } else {
5148 8a16d273 ths
            vde_mode = 0700;
5149 8a16d273 ths
        }
5150 8a16d273 ths
        ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode);
5151 8a16d273 ths
    } else
5152 8a16d273 ths
#endif
5153 7c9d8e07 bellard
    {
5154 7c9d8e07 bellard
        fprintf(stderr, "Unknown network device: %s\n", device);
5155 7c9d8e07 bellard
        return -1;
5156 7c9d8e07 bellard
    }
5157 7c9d8e07 bellard
    if (ret < 0) {
5158 7c9d8e07 bellard
        fprintf(stderr, "Could not initialize device '%s'\n", device);
5159 7c9d8e07 bellard
    }
5160 3b46e624 ths
5161 7c9d8e07 bellard
    return ret;
5162 7c9d8e07 bellard
}
5163 7c9d8e07 bellard
5164 7c9d8e07 bellard
void do_info_network(void)
5165 7c9d8e07 bellard
{
5166 7c9d8e07 bellard
    VLANState *vlan;
5167 7c9d8e07 bellard
    VLANClientState *vc;
5168 7c9d8e07 bellard
5169 7c9d8e07 bellard
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
5170 7c9d8e07 bellard
        term_printf("VLAN %d devices:\n", vlan->id);
5171 7c9d8e07 bellard
        for(vc = vlan->first_client; vc != NULL; vc = vc->next)
5172 7c9d8e07 bellard
            term_printf("  %s\n", vc->info_str);
5173 7c9d8e07 bellard
    }
5174 7c9d8e07 bellard
}
5175 42550fde ths
5176 609497ab balrog
#define HD_ALIAS "index=%d,media=disk"
5177 e4bcb14c ths
#ifdef TARGET_PPC
5178 e4bcb14c ths
#define CDROM_ALIAS "index=1,media=cdrom"
5179 e4bcb14c ths
#else
5180 e4bcb14c ths
#define CDROM_ALIAS "index=2,media=cdrom"
5181 e4bcb14c ths
#endif
5182 e4bcb14c ths
#define FD_ALIAS "index=%d,if=floppy"
5183 609497ab balrog
#define PFLASH_ALIAS "if=pflash"
5184 609497ab balrog
#define MTD_ALIAS "if=mtd"
5185 9d413d1d balrog
#define SD_ALIAS "index=0,if=sd"
5186 e4bcb14c ths
5187 609497ab balrog
static int drive_add(const char *file, const char *fmt, ...)
5188 e4bcb14c ths
{
5189 e4bcb14c ths
    va_list ap;
5190 e4bcb14c ths
5191 e4bcb14c ths
    if (nb_drives_opt >= MAX_DRIVES) {
5192 e4bcb14c ths
        fprintf(stderr, "qemu: too many drives\n");
5193 e4bcb14c ths
        exit(1);
5194 e4bcb14c ths
    }
5195 e4bcb14c ths
5196 609497ab balrog
    drives_opt[nb_drives_opt].file = file;
5197 e4bcb14c ths
    va_start(ap, fmt);
5198 609497ab balrog
    vsnprintf(drives_opt[nb_drives_opt].opt,
5199 609497ab balrog
              sizeof(drives_opt[0].opt), fmt, ap);
5200 e4bcb14c ths
    va_end(ap);
5201 e4bcb14c ths
5202 e4bcb14c ths
    return nb_drives_opt++;
5203 e4bcb14c ths
}
5204 e4bcb14c ths
5205 f60d39bc ths
int drive_get_index(BlockInterfaceType type, int bus, int unit)
5206 e4bcb14c ths
{
5207 e4bcb14c ths
    int index;
5208 e4bcb14c ths
5209 e4bcb14c ths
    /* seek interface, bus and unit */
5210 e4bcb14c ths
5211 e4bcb14c ths
    for (index = 0; index < nb_drives; index++)
5212 f60d39bc ths
        if (drives_table[index].type == type &&
5213 e4bcb14c ths
            drives_table[index].bus == bus &&
5214 e4bcb14c ths
            drives_table[index].unit == unit)
5215 e4bcb14c ths
        return index;
5216 e4bcb14c ths
5217 e4bcb14c ths
    return -1;
5218 e4bcb14c ths
}
5219 e4bcb14c ths
5220 f60d39bc ths
int drive_get_max_bus(BlockInterfaceType type)
5221 e4bcb14c ths
{
5222 e4bcb14c ths
    int max_bus;
5223 e4bcb14c ths
    int index;
5224 e4bcb14c ths
5225 e4bcb14c ths
    max_bus = -1;
5226 e4bcb14c ths
    for (index = 0; index < nb_drives; index++) {
5227 f60d39bc ths
        if(drives_table[index].type == type &&
5228 e4bcb14c ths
           drives_table[index].bus > max_bus)
5229 e4bcb14c ths
            max_bus = drives_table[index].bus;
5230 e4bcb14c ths
    }
5231 e4bcb14c ths
    return max_bus;
5232 e4bcb14c ths
}
5233 e4bcb14c ths
5234 a1620fac aurel32
static void bdrv_format_print(void *opaque, const char *name)
5235 a1620fac aurel32
{
5236 a1620fac aurel32
    fprintf(stderr, " %s", name);
5237 a1620fac aurel32
}
5238 a1620fac aurel32
5239 609497ab balrog
static int drive_init(struct drive_opt *arg, int snapshot,
5240 609497ab balrog
                      QEMUMachine *machine)
5241 e4bcb14c ths
{
5242 e4bcb14c ths
    char buf[128];
5243 e4bcb14c ths
    char file[1024];
5244 c8522bdf balrog
    char devname[128];
5245 c8522bdf balrog
    const char *mediastr = "";
5246 f60d39bc ths
    BlockInterfaceType type;
5247 e4bcb14c ths
    enum { MEDIA_DISK, MEDIA_CDROM } media;
5248 e4bcb14c ths
    int bus_id, unit_id;
5249 e4bcb14c ths
    int cyls, heads, secs, translation;
5250 e4bcb14c ths
    BlockDriverState *bdrv;
5251 1e72d3b7 aurel32
    BlockDriver *drv = NULL;
5252 e4bcb14c ths
    int max_devs;
5253 e4bcb14c ths
    int index;
5254 33f00271 balrog
    int cache;
5255 33f00271 balrog
    int bdrv_flags;
5256 609497ab balrog
    char *str = arg->opt;
5257 e4bcb14c ths
    char *params[] = { "bus", "unit", "if", "index", "cyls", "heads",
5258 33f00271 balrog
                       "secs", "trans", "media", "snapshot", "file",
5259 1e72d3b7 aurel32
                       "cache", "format", NULL };
5260 e4bcb14c ths
5261 e4bcb14c ths
    if (check_params(buf, sizeof(buf), params, str) < 0) {
5262 ff993638 balrog
         fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
5263 e4bcb14c ths
                         buf, str);
5264 e4bcb14c ths
         return -1;
5265 e4bcb14c ths
    }
5266 e4bcb14c ths
5267 e4bcb14c ths
    file[0] = 0;
5268 e4bcb14c ths
    cyls = heads = secs = 0;
5269 e4bcb14c ths
    bus_id = 0;
5270 e4bcb14c ths
    unit_id = -1;
5271 e4bcb14c ths
    translation = BIOS_ATA_TRANSLATION_AUTO;
5272 e4bcb14c ths
    index = -1;
5273 33f00271 balrog
    cache = 1;
5274 e4bcb14c ths
5275 e4bcb14c ths
    if (!strcmp(machine->name, "realview") ||
5276 e4bcb14c ths
        !strcmp(machine->name, "SS-5") ||
5277 e4bcb14c ths
        !strcmp(machine->name, "SS-10") ||
5278 e4bcb14c ths
        !strcmp(machine->name, "SS-600MP") ||
5279 e4bcb14c ths
        !strcmp(machine->name, "versatilepb") ||
5280 e4bcb14c ths
        !strcmp(machine->name, "versatileab")) {
5281 f60d39bc ths
        type = IF_SCSI;
5282 e4bcb14c ths
        max_devs = MAX_SCSI_DEVS;
5283 c8522bdf balrog
        strcpy(devname, "scsi");
5284 e4bcb14c ths
    } else {
5285 f60d39bc ths
        type = IF_IDE;
5286 e4bcb14c ths
        max_devs = MAX_IDE_DEVS;
5287 c8522bdf balrog
        strcpy(devname, "ide");
5288 e4bcb14c ths
    }
5289 e4bcb14c ths
    media = MEDIA_DISK;
5290 e4bcb14c ths
5291 e4bcb14c ths
    /* extract parameters */
5292 e4bcb14c ths
5293 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "bus", str)) {
5294 e4bcb14c ths
        bus_id = strtol(buf, NULL, 0);
5295 e4bcb14c ths
        if (bus_id < 0) {
5296 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
5297 e4bcb14c ths
            return -1;
5298 e4bcb14c ths
        }
5299 e4bcb14c ths
    }
5300 e4bcb14c ths
5301 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "unit", str)) {
5302 e4bcb14c ths
        unit_id = strtol(buf, NULL, 0);
5303 e4bcb14c ths
        if (unit_id < 0) {
5304 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
5305 e4bcb14c ths
            return -1;
5306 e4bcb14c ths
        }
5307 e4bcb14c ths
    }
5308 e4bcb14c ths
5309 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "if", str)) {
5310 ae45d369 bellard
        pstrcpy(devname, sizeof(devname), buf);
5311 e4bcb14c ths
        if (!strcmp(buf, "ide")) {
5312 f60d39bc ths
            type = IF_IDE;
5313 e4bcb14c ths
            max_devs = MAX_IDE_DEVS;
5314 e4bcb14c ths
        } else if (!strcmp(buf, "scsi")) {
5315 f60d39bc ths
            type = IF_SCSI;
5316 e4bcb14c ths
            max_devs = MAX_SCSI_DEVS;
5317 e4bcb14c ths
        } else if (!strcmp(buf, "floppy")) {
5318 f60d39bc ths
            type = IF_FLOPPY;
5319 e4bcb14c ths
            max_devs = 0;
5320 e4bcb14c ths
        } else if (!strcmp(buf, "pflash")) {
5321 f60d39bc ths
            type = IF_PFLASH;
5322 e4bcb14c ths
            max_devs = 0;
5323 e4bcb14c ths
        } else if (!strcmp(buf, "mtd")) {
5324 f60d39bc ths
            type = IF_MTD;
5325 e4bcb14c ths
            max_devs = 0;
5326 e4bcb14c ths
        } else if (!strcmp(buf, "sd")) {
5327 f60d39bc ths
            type = IF_SD;
5328 e4bcb14c ths
            max_devs = 0;
5329 e4bcb14c ths
        } else {
5330 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
5331 e4bcb14c ths
            return -1;
5332 e4bcb14c ths
        }
5333 e4bcb14c ths
    }
5334 e4bcb14c ths
5335 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "index", str)) {
5336 e4bcb14c ths
        index = strtol(buf, NULL, 0);
5337 e4bcb14c ths
        if (index < 0) {
5338 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid index\n", str);
5339 e4bcb14c ths
            return -1;
5340 e4bcb14c ths
        }
5341 e4bcb14c ths
    }
5342 e4bcb14c ths
5343 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "cyls", str)) {
5344 e4bcb14c ths
        cyls = strtol(buf, NULL, 0);
5345 e4bcb14c ths
    }
5346 e4bcb14c ths
5347 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "heads", str)) {
5348 e4bcb14c ths
        heads = strtol(buf, NULL, 0);
5349 e4bcb14c ths
    }
5350 e4bcb14c ths
5351 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "secs", str)) {
5352 e4bcb14c ths
        secs = strtol(buf, NULL, 0);
5353 e4bcb14c ths
    }
5354 e4bcb14c ths
5355 e4bcb14c ths
    if (cyls || heads || secs) {
5356 e4bcb14c ths
        if (cyls < 1 || cyls > 16383) {
5357 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
5358 e4bcb14c ths
            return -1;
5359 e4bcb14c ths
        }
5360 e4bcb14c ths
        if (heads < 1 || heads > 16) {
5361 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
5362 e4bcb14c ths
            return -1;
5363 e4bcb14c ths
        }
5364 e4bcb14c ths
        if (secs < 1 || secs > 63) {
5365 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
5366 e4bcb14c ths
            return -1;
5367 e4bcb14c ths
        }
5368 e4bcb14c ths
    }
5369 e4bcb14c ths
5370 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "trans", str)) {
5371 e4bcb14c ths
        if (!cyls) {
5372 e4bcb14c ths
            fprintf(stderr,
5373 e4bcb14c ths
                    "qemu: '%s' trans must be used with cyls,heads and secs\n",
5374 e4bcb14c ths
                    str);
5375 e4bcb14c ths
            return -1;
5376 e4bcb14c ths
        }
5377 e4bcb14c ths
        if (!strcmp(buf, "none"))
5378 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_NONE;
5379 e4bcb14c ths
        else if (!strcmp(buf, "lba"))
5380 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_LBA;
5381 e4bcb14c ths
        else if (!strcmp(buf, "auto"))
5382 e4bcb14c ths
            translation = BIOS_ATA_TRANSLATION_AUTO;
5383 e4bcb14c ths
        else {
5384 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
5385 e4bcb14c ths
            return -1;
5386 e4bcb14c ths
        }
5387 e4bcb14c ths
    }
5388 e4bcb14c ths
5389 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "media", str)) {
5390 e4bcb14c ths
        if (!strcmp(buf, "disk")) {
5391 e4bcb14c ths
            media = MEDIA_DISK;
5392 e4bcb14c ths
        } else if (!strcmp(buf, "cdrom")) {
5393 e4bcb14c ths
            if (cyls || secs || heads) {
5394 e4bcb14c ths
                fprintf(stderr,
5395 e4bcb14c ths
                        "qemu: '%s' invalid physical CHS format\n", str);
5396 e4bcb14c ths
                return -1;
5397 e4bcb14c ths
            }
5398 e4bcb14c ths
            media = MEDIA_CDROM;
5399 e4bcb14c ths
        } else {
5400 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid media\n", str);
5401 e4bcb14c ths
            return -1;
5402 e4bcb14c ths
        }
5403 e4bcb14c ths
    }
5404 e4bcb14c ths
5405 e4bcb14c ths
    if (get_param_value(buf, sizeof(buf), "snapshot", str)) {
5406 e4bcb14c ths
        if (!strcmp(buf, "on"))
5407 e4bcb14c ths
            snapshot = 1;
5408 e4bcb14c ths
        else if (!strcmp(buf, "off"))
5409 e4bcb14c ths
            snapshot = 0;
5410 e4bcb14c ths
        else {
5411 e4bcb14c ths
            fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
5412 e4bcb14c ths
            return -1;
5413 e4bcb14c ths
        }
5414 e4bcb14c ths
    }
5415 e4bcb14c ths
5416 33f00271 balrog
    if (get_param_value(buf, sizeof(buf), "cache", str)) {
5417 33f00271 balrog
        if (!strcmp(buf, "off"))
5418 33f00271 balrog
            cache = 0;
5419 33f00271 balrog
        else if (!strcmp(buf, "on"))
5420 33f00271 balrog
            cache = 1;
5421 33f00271 balrog
        else {
5422 33f00271 balrog
           fprintf(stderr, "qemu: invalid cache option\n");
5423 33f00271 balrog
           return -1;
5424 33f00271 balrog
        }
5425 33f00271 balrog
    }
5426 33f00271 balrog
5427 1e72d3b7 aurel32
    if (get_param_value(buf, sizeof(buf), "format", str)) {
5428 a1620fac aurel32
       if (strcmp(buf, "?") == 0) {
5429 a1620fac aurel32
            fprintf(stderr, "qemu: Supported formats:");
5430 a1620fac aurel32
            bdrv_iterate_format(bdrv_format_print, NULL);
5431 a1620fac aurel32
            fprintf(stderr, "\n");
5432 a1620fac aurel32
            return -1;
5433 a1620fac aurel32
        }
5434 1e72d3b7 aurel32
        drv = bdrv_find_format(buf);
5435 1e72d3b7 aurel32
        if (!drv) {
5436 1e72d3b7 aurel32
            fprintf(stderr, "qemu: '%s' invalid format\n", buf);
5437 1e72d3b7 aurel32
            return -1;
5438 1e72d3b7 aurel32
        }
5439 1e72d3b7 aurel32
    }
5440 1e72d3b7 aurel32
5441 609497ab balrog
    if (arg->file == NULL)
5442 609497ab balrog
        get_param_value(file, sizeof(file), "file", str);
5443 609497ab balrog
    else
5444 609497ab balrog
        pstrcpy(file, sizeof(file), arg->file);
5445 e4bcb14c ths
5446 e4bcb14c ths
    /* compute bus and unit according index */
5447 e4bcb14c ths
5448 e4bcb14c ths
    if (index != -1) {
5449 e4bcb14c ths
        if (bus_id != 0 || unit_id != -1) {
5450 e4bcb14c ths
            fprintf(stderr,
5451 e4bcb14c ths
                    "qemu: '%s' index cannot be used with bus and unit\n", str);
5452 e4bcb14c ths
            return -1;
5453 e4bcb14c ths
        }
5454 e4bcb14c ths
        if (max_devs == 0)
5455 e4bcb14c ths
        {
5456 e4bcb14c ths
            unit_id = index;
5457 e4bcb14c ths
            bus_id = 0;
5458 e4bcb14c ths
        } else {
5459 e4bcb14c ths
            unit_id = index % max_devs;
5460 e4bcb14c ths
            bus_id = index / max_devs;
5461 e4bcb14c ths
        }
5462 e4bcb14c ths
    }
5463 e4bcb14c ths
5464 e4bcb14c ths
    /* if user doesn't specify a unit_id,
5465 e4bcb14c ths
     * try to find the first free
5466 e4bcb14c ths
     */
5467 e4bcb14c ths
5468 e4bcb14c ths
    if (unit_id == -1) {
5469 e4bcb14c ths
       unit_id = 0;
5470 f60d39bc ths
       while (drive_get_index(type, bus_id, unit_id) != -1) {
5471 e4bcb14c ths
           unit_id++;
5472 e4bcb14c ths
           if (max_devs && unit_id >= max_devs) {
5473 e4bcb14c ths
               unit_id -= max_devs;
5474 e4bcb14c ths
               bus_id++;
5475 e4bcb14c ths
           }
5476 e4bcb14c ths
       }
5477 e4bcb14c ths
    }
5478 e4bcb14c ths
5479 e4bcb14c ths
    /* check unit id */
5480 e4bcb14c ths
5481 e4bcb14c ths
    if (max_devs && unit_id >= max_devs) {
5482 e4bcb14c ths
        fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
5483 e4bcb14c ths
                        str, unit_id, max_devs - 1);
5484 e4bcb14c ths
        return -1;
5485 e4bcb14c ths
    }
5486 e4bcb14c ths
5487 e4bcb14c ths
    /*
5488 e4bcb14c ths
     * ignore multiple definitions
5489 e4bcb14c ths
     */
5490 e4bcb14c ths
5491 f60d39bc ths
    if (drive_get_index(type, bus_id, unit_id) != -1)
5492 e4bcb14c ths
        return 0;
5493 e4bcb14c ths
5494 e4bcb14c ths
    /* init */
5495 e4bcb14c ths
5496 f60d39bc ths
    if (type == IF_IDE || type == IF_SCSI)
5497 c8522bdf balrog
        mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd";
5498 e6198a70 balrog
    if (max_devs)
5499 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%i%s%i",
5500 e6198a70 balrog
                 devname, bus_id, mediastr, unit_id);
5501 e6198a70 balrog
    else
5502 e6198a70 balrog
        snprintf(buf, sizeof(buf), "%s%s%i",
5503 e6198a70 balrog
                 devname, mediastr, unit_id);
5504 e4bcb14c ths
    bdrv = bdrv_new(buf);
5505 e4bcb14c ths
    drives_table[nb_drives].bdrv = bdrv;
5506 f60d39bc ths
    drives_table[nb_drives].type = type;
5507 e4bcb14c ths
    drives_table[nb_drives].bus = bus_id;
5508 e4bcb14c ths
    drives_table[nb_drives].unit = unit_id;
5509 e4bcb14c ths
    nb_drives++;
5510 e4bcb14c ths
5511 f60d39bc ths
    switch(type) {
5512 e4bcb14c ths
    case IF_IDE:
5513 e4bcb14c ths
    case IF_SCSI:
5514 e4bcb14c ths
        switch(media) {
5515 e4bcb14c ths
        case MEDIA_DISK:
5516 e4bcb14c ths
            if (cyls != 0) {
5517 e4bcb14c ths
                bdrv_set_geometry_hint(bdrv, cyls, heads, secs);
5518 e4bcb14c ths
                bdrv_set_translation_hint(bdrv, translation);
5519 e4bcb14c ths
            }
5520 e4bcb14c ths
            break;
5521 e4bcb14c ths
        case MEDIA_CDROM:
5522 e4bcb14c ths
            bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM);
5523 e4bcb14c ths
            break;
5524 e4bcb14c ths
        }
5525 e4bcb14c ths
        break;
5526 e4bcb14c ths
    case IF_SD:
5527 e4bcb14c ths
        /* FIXME: This isn't really a floppy, but it's a reasonable
5528 e4bcb14c ths
           approximation.  */
5529 e4bcb14c ths
    case IF_FLOPPY:
5530 e4bcb14c ths
        bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY);
5531 e4bcb14c ths
        break;
5532 e4bcb14c ths
    case IF_PFLASH:
5533 e4bcb14c ths
    case IF_MTD:
5534 e4bcb14c ths
        break;
5535 e4bcb14c ths
    }
5536 e4bcb14c ths
    if (!file[0])
5537 e4bcb14c ths
        return 0;
5538 33f00271 balrog
    bdrv_flags = 0;
5539 33f00271 balrog
    if (snapshot)
5540 33f00271 balrog
        bdrv_flags |= BDRV_O_SNAPSHOT;
5541 33f00271 balrog
    if (!cache)
5542 33f00271 balrog
        bdrv_flags |= BDRV_O_DIRECT;
5543 1e72d3b7 aurel32
    if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) {
5544 e4bcb14c ths
        fprintf(stderr, "qemu: could not open disk image %s\n",
5545 e4bcb14c ths
                        file);
5546 e4bcb14c ths
        return -1;
5547 e4bcb14c ths
    }
5548 e4bcb14c ths
    return 0;
5549 e4bcb14c ths
}
5550 e4bcb14c ths
5551 330d0414 bellard
/***********************************************************/
5552 a594cfbf bellard
/* USB devices */
5553 a594cfbf bellard
5554 0d92ed30 pbrook
static USBPort *used_usb_ports;
5555 0d92ed30 pbrook
static USBPort *free_usb_ports;
5556 0d92ed30 pbrook
5557 0d92ed30 pbrook
/* ??? Maybe change this to register a hub to keep track of the topology.  */
5558 0d92ed30 pbrook
void qemu_register_usb_port(USBPort *port, void *opaque, int index,
5559 0d92ed30 pbrook
                            usb_attachfn attach)
5560 0d92ed30 pbrook
{
5561 0d92ed30 pbrook
    port->opaque = opaque;
5562 0d92ed30 pbrook
    port->index = index;
5563 0d92ed30 pbrook
    port->attach = attach;
5564 0d92ed30 pbrook
    port->next = free_usb_ports;
5565 0d92ed30 pbrook
    free_usb_ports = port;
5566 0d92ed30 pbrook
}
5567 0d92ed30 pbrook
5568 a594cfbf bellard
static int usb_device_add(const char *devname)
5569 a594cfbf bellard
{
5570 a594cfbf bellard
    const char *p;
5571 a594cfbf bellard
    USBDevice *dev;
5572 0d92ed30 pbrook
    USBPort *port;
5573 a594cfbf bellard
5574 0d92ed30 pbrook
    if (!free_usb_ports)
5575 a594cfbf bellard
        return -1;
5576 a594cfbf bellard
5577 a594cfbf bellard
    if (strstart(devname, "host:", &p)) {
5578 a594cfbf bellard
        dev = usb_host_device_open(p);
5579 a594cfbf bellard
    } else if (!strcmp(devname, "mouse")) {
5580 a594cfbf bellard
        dev = usb_mouse_init();
5581 09b26c5e bellard
    } else if (!strcmp(devname, "tablet")) {
5582 47b2d338 balrog
        dev = usb_tablet_init();
5583 47b2d338 balrog
    } else if (!strcmp(devname, "keyboard")) {
5584 47b2d338 balrog
        dev = usb_keyboard_init();
5585 2e5d83bb pbrook
    } else if (strstart(devname, "disk:", &p)) {
5586 2e5d83bb pbrook
        dev = usb_msd_init(p);
5587 f6d2a316 balrog
    } else if (!strcmp(devname, "wacom-tablet")) {
5588 f6d2a316 balrog
        dev = usb_wacom_init();
5589 a7954218 balrog
    } else if (strstart(devname, "serial:", &p)) {
5590 a7954218 balrog
        dev = usb_serial_init(p);
5591 2e4d9fb1 aurel32
#ifdef CONFIG_BRLAPI
5592 2e4d9fb1 aurel32
    } else if (!strcmp(devname, "braille")) {
5593 2e4d9fb1 aurel32
        dev = usb_baum_init();
5594 2e4d9fb1 aurel32
#endif
5595 6c9f886c balrog
    } else if (strstart(devname, "net:", &p)) {
5596 6c9f886c balrog
        int nicidx = strtoul(p, NULL, 0);
5597 6c9f886c balrog
5598 6c9f886c balrog
        if (nicidx >= nb_nics || strcmp(nd_table[nicidx].model, "usb"))
5599 6c9f886c balrog
            return -1;
5600 6c9f886c balrog
        dev = usb_net_init(&nd_table[nicidx]);
5601 a594cfbf bellard
    } else {
5602 a594cfbf bellard
        return -1;
5603 a594cfbf bellard
    }
5604 0d92ed30 pbrook
    if (!dev)
5605 0d92ed30 pbrook
        return -1;
5606 0d92ed30 pbrook
5607 0d92ed30 pbrook
    /* Find a USB port to add the device to.  */
5608 0d92ed30 pbrook
    port = free_usb_ports;
5609 0d92ed30 pbrook
    if (!port->next) {
5610 0d92ed30 pbrook
        USBDevice *hub;
5611 0d92ed30 pbrook
5612 0d92ed30 pbrook
        /* Create a new hub and chain it on.  */
5613 0d92ed30 pbrook
        free_usb_ports = NULL;
5614 0d92ed30 pbrook
        port->next = used_usb_ports;
5615 0d92ed30 pbrook
        used_usb_ports = port;
5616 0d92ed30 pbrook
5617 0d92ed30 pbrook
        hub = usb_hub_init(VM_USB_HUB_SIZE);
5618 0d92ed30 pbrook
        usb_attach(port, hub);
5619 0d92ed30 pbrook
        port = free_usb_ports;
5620 0d92ed30 pbrook
    }
5621 0d92ed30 pbrook
5622 0d92ed30 pbrook
    free_usb_ports = port->next;
5623 0d92ed30 pbrook
    port->next = used_usb_ports;
5624 0d92ed30 pbrook
    used_usb_ports = port;
5625 0d92ed30 pbrook
    usb_attach(port, dev);
5626 a594cfbf bellard
    return 0;
5627 a594cfbf bellard
}
5628 a594cfbf bellard
5629 a594cfbf bellard
static int usb_device_del(const char *devname)
5630 a594cfbf bellard
{
5631 0d92ed30 pbrook
    USBPort *port;
5632 0d92ed30 pbrook
    USBPort **lastp;
5633 059809e4 bellard
    USBDevice *dev;
5634 0d92ed30 pbrook
    int bus_num, addr;
5635 a594cfbf bellard
    const char *p;
5636 a594cfbf bellard
5637 0d92ed30 pbrook
    if (!used_usb_ports)
5638 a594cfbf bellard
        return -1;
5639 a594cfbf bellard
5640 a594cfbf bellard
    p = strchr(devname, '.');
5641 5fafdf24 ths
    if (!p)
5642 a594cfbf bellard
        return -1;
5643 a594cfbf bellard
    bus_num = strtoul(devname, NULL, 0);
5644 a594cfbf bellard
    addr = strtoul(p + 1, NULL, 0);
5645 a594cfbf bellard
    if (bus_num != 0)
5646 a594cfbf bellard
        return -1;
5647 0d92ed30 pbrook
5648 0d92ed30 pbrook
    lastp = &used_usb_ports;
5649 0d92ed30 pbrook
    port = used_usb_ports;
5650 0d92ed30 pbrook
    while (port && port->dev->addr != addr) {
5651 0d92ed30 pbrook
        lastp = &port->next;
5652 0d92ed30 pbrook
        port = port->next;
5653 a594cfbf bellard
    }
5654 0d92ed30 pbrook
5655 0d92ed30 pbrook
    if (!port)
5656 a594cfbf bellard
        return -1;
5657 0d92ed30 pbrook
5658 059809e4 bellard
    dev = port->dev;
5659 0d92ed30 pbrook
    *lastp = port->next;
5660 0d92ed30 pbrook
    usb_attach(port, NULL);
5661 059809e4 bellard
    dev->handle_destroy(dev);
5662 0d92ed30 pbrook
    port->next = free_usb_ports;
5663 0d92ed30 pbrook
    free_usb_ports = port;
5664 a594cfbf bellard
    return 0;
5665 a594cfbf bellard
}
5666 a594cfbf bellard
5667 a594cfbf bellard
void do_usb_add(const char *devname)
5668 a594cfbf bellard
{
5669 a594cfbf bellard
    int ret;
5670 a594cfbf bellard
    ret = usb_device_add(devname);
5671 5fafdf24 ths
    if (ret < 0)
5672 a594cfbf bellard
        term_printf("Could not add USB device '%s'\n", devname);
5673 a594cfbf bellard
}
5674 a594cfbf bellard
5675 a594cfbf bellard
void do_usb_del(const char *devname)
5676 a594cfbf bellard
{
5677 a594cfbf bellard
    int ret;
5678 a594cfbf bellard
    ret = usb_device_del(devname);
5679 5fafdf24 ths
    if (ret < 0)
5680 a594cfbf bellard
        term_printf("Could not remove USB device '%s'\n", devname);
5681 a594cfbf bellard
}
5682 a594cfbf bellard
5683 a594cfbf bellard
void usb_info(void)
5684 a594cfbf bellard
{
5685 a594cfbf bellard
    USBDevice *dev;
5686 0d92ed30 pbrook
    USBPort *port;
5687 a594cfbf bellard
    const char *speed_str;
5688 a594cfbf bellard
5689 0d92ed30 pbrook
    if (!usb_enabled) {
5690 a594cfbf bellard
        term_printf("USB support not enabled\n");
5691 a594cfbf bellard
        return;
5692 a594cfbf bellard
    }
5693 a594cfbf bellard
5694 0d92ed30 pbrook
    for (port = used_usb_ports; port; port = port->next) {
5695 0d92ed30 pbrook
        dev = port->dev;
5696 0d92ed30 pbrook
        if (!dev)
5697 0d92ed30 pbrook
            continue;
5698 0d92ed30 pbrook
        switch(dev->speed) {
5699 5fafdf24 ths
        case USB_SPEED_LOW:
5700 5fafdf24 ths
            speed_str = "1.5";
5701 0d92ed30 pbrook
            break;
5702 5fafdf24 ths
        case USB_SPEED_FULL:
5703 5fafdf24 ths
            speed_str = "12";
5704 0d92ed30 pbrook
            break;
5705 5fafdf24 ths
        case USB_SPEED_HIGH:
5706 5fafdf24 ths
            speed_str = "480";
5707 0d92ed30 pbrook
            break;
5708 0d92ed30 pbrook
        default:
5709 5fafdf24 ths
            speed_str = "?";
5710 0d92ed30 pbrook
            break;
5711 a594cfbf bellard
        }
5712 5fafdf24 ths
        term_printf("  Device %d.%d, Speed %s Mb/s, Product %s\n",
5713 1f6e24e7 bellard
                    0, dev->addr, speed_str, dev->devname);
5714 a594cfbf bellard
    }
5715 a594cfbf bellard
}
5716 a594cfbf bellard
5717 f7cce898 bellard
/***********************************************************/
5718 201a51fc balrog
/* PCMCIA/Cardbus */
5719 201a51fc balrog
5720 201a51fc balrog
static struct pcmcia_socket_entry_s {
5721 201a51fc balrog
    struct pcmcia_socket_s *socket;
5722 201a51fc balrog
    struct pcmcia_socket_entry_s *next;
5723 201a51fc balrog
} *pcmcia_sockets = 0;
5724 201a51fc balrog
5725 201a51fc balrog
void pcmcia_socket_register(struct pcmcia_socket_s *socket)
5726 201a51fc balrog
{
5727 201a51fc balrog
    struct pcmcia_socket_entry_s *entry;
5728 201a51fc balrog
5729 201a51fc balrog
    entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s));
5730 201a51fc balrog
    entry->socket = socket;
5731 201a51fc balrog
    entry->next = pcmcia_sockets;
5732 201a51fc balrog
    pcmcia_sockets = entry;
5733 201a51fc balrog
}
5734 201a51fc balrog
5735 201a51fc balrog
void pcmcia_socket_unregister(struct pcmcia_socket_s *socket)
5736 201a51fc balrog
{
5737 201a51fc balrog
    struct pcmcia_socket_entry_s *entry, **ptr;
5738 201a51fc balrog
5739 201a51fc balrog
    ptr = &pcmcia_sockets;
5740 201a51fc balrog
    for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
5741 201a51fc balrog
        if (entry->socket == socket) {
5742 201a51fc balrog
            *ptr = entry->next;
5743 201a51fc balrog
            qemu_free(entry);
5744 201a51fc balrog
        }
5745 201a51fc balrog
}
5746 201a51fc balrog
5747 201a51fc balrog
void pcmcia_info(void)
5748 201a51fc balrog
{
5749 201a51fc balrog
    struct pcmcia_socket_entry_s *iter;
5750 201a51fc balrog
    if (!pcmcia_sockets)
5751 201a51fc balrog
        term_printf("No PCMCIA sockets\n");
5752 201a51fc balrog
5753 201a51fc balrog
    for (iter = pcmcia_sockets; iter; iter = iter->next)
5754 201a51fc balrog
        term_printf("%s: %s\n", iter->socket->slot_string,
5755 201a51fc balrog
                    iter->socket->attached ? iter->socket->card_string :
5756 201a51fc balrog
                    "Empty");
5757 201a51fc balrog
}
5758 201a51fc balrog
5759 201a51fc balrog
/***********************************************************/
5760 2ff89790 ths
/* dumb display */
5761 2ff89790 ths
5762 2ff89790 ths
static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
5763 2ff89790 ths
{
5764 2ff89790 ths
}
5765 2ff89790 ths
5766 2ff89790 ths
static void dumb_resize(DisplayState *ds, int w, int h)
5767 2ff89790 ths
{
5768 2ff89790 ths
}
5769 2ff89790 ths
5770 2ff89790 ths
static void dumb_refresh(DisplayState *ds)
5771 2ff89790 ths
{
5772 2ff89790 ths
#if defined(CONFIG_SDL)
5773 2ff89790 ths
    vga_hw_update();
5774 2ff89790 ths
#endif
5775 2ff89790 ths
}
5776 2ff89790 ths
5777 2ff89790 ths
static void dumb_display_init(DisplayState *ds)
5778 2ff89790 ths
{
5779 2ff89790 ths
    ds->data = NULL;
5780 2ff89790 ths
    ds->linesize = 0;
5781 2ff89790 ths
    ds->depth = 0;
5782 2ff89790 ths
    ds->dpy_update = dumb_update;
5783 2ff89790 ths
    ds->dpy_resize = dumb_resize;
5784 2ff89790 ths
    ds->dpy_refresh = dumb_refresh;
5785 2ff89790 ths
}
5786 2ff89790 ths
5787 2ff89790 ths
/***********************************************************/
5788 8a7ddc38 bellard
/* I/O handling */
5789 0824d6fc bellard
5790 c4b1fcc0 bellard
#define MAX_IO_HANDLERS 64
5791 c4b1fcc0 bellard
5792 c4b1fcc0 bellard
typedef struct IOHandlerRecord {
5793 c4b1fcc0 bellard
    int fd;
5794 7c9d8e07 bellard
    IOCanRWHandler *fd_read_poll;
5795 7c9d8e07 bellard
    IOHandler *fd_read;
5796 7c9d8e07 bellard
    IOHandler *fd_write;
5797 cafffd40 ths
    int deleted;
5798 c4b1fcc0 bellard
    void *opaque;
5799 c4b1fcc0 bellard
    /* temporary data */
5800 c4b1fcc0 bellard
    struct pollfd *ufd;
5801 8a7ddc38 bellard
    struct IOHandlerRecord *next;
5802 c4b1fcc0 bellard
} IOHandlerRecord;
5803 c4b1fcc0 bellard
5804 8a7ddc38 bellard
static IOHandlerRecord *first_io_handler;
5805 c4b1fcc0 bellard
5806 7c9d8e07 bellard
/* XXX: fd_read_poll should be suppressed, but an API change is
5807 7c9d8e07 bellard
   necessary in the character devices to suppress fd_can_read(). */
5808 5fafdf24 ths
int qemu_set_fd_handler2(int fd,
5809 5fafdf24 ths
                         IOCanRWHandler *fd_read_poll,
5810 5fafdf24 ths
                         IOHandler *fd_read,
5811 5fafdf24 ths
                         IOHandler *fd_write,
5812 7c9d8e07 bellard
                         void *opaque)
5813 c4b1fcc0 bellard
{
5814 7c9d8e07 bellard
    IOHandlerRecord **pioh, *ioh;
5815 c4b1fcc0 bellard
5816 7c9d8e07 bellard
    if (!fd_read && !fd_write) {
5817 7c9d8e07 bellard
        pioh = &first_io_handler;
5818 7c9d8e07 bellard
        for(;;) {
5819 7c9d8e07 bellard
            ioh = *pioh;
5820 7c9d8e07 bellard
            if (ioh == NULL)
5821 7c9d8e07 bellard
                break;
5822 7c9d8e07 bellard
            if (ioh->fd == fd) {
5823 cafffd40 ths
                ioh->deleted = 1;
5824 7c9d8e07 bellard
                break;
5825 7c9d8e07 bellard
            }
5826 7c9d8e07 bellard
            pioh = &ioh->next;
5827 7c9d8e07 bellard
        }
5828 7c9d8e07 bellard
    } else {
5829 7c9d8e07 bellard
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
5830 7c9d8e07 bellard
            if (ioh->fd == fd)
5831 7c9d8e07 bellard
                goto found;
5832 7c9d8e07 bellard
        }
5833 7c9d8e07 bellard
        ioh = qemu_mallocz(sizeof(IOHandlerRecord));
5834 7c9d8e07 bellard
        if (!ioh)
5835 7c9d8e07 bellard
            return -1;
5836 7c9d8e07 bellard
        ioh->next = first_io_handler;
5837 7c9d8e07 bellard
        first_io_handler = ioh;
5838 7c9d8e07 bellard
    found:
5839 7c9d8e07 bellard
        ioh->fd = fd;
5840 7c9d8e07 bellard
        ioh->fd_read_poll = fd_read_poll;
5841 7c9d8e07 bellard
        ioh->fd_read = fd_read;
5842 7c9d8e07 bellard
        ioh->fd_write = fd_write;
5843 7c9d8e07 bellard
        ioh->opaque = opaque;
5844 cafffd40 ths
        ioh->deleted = 0;
5845 7c9d8e07 bellard
    }
5846 c4b1fcc0 bellard
    return 0;
5847 c4b1fcc0 bellard
}
5848 c4b1fcc0 bellard
5849 5fafdf24 ths
int qemu_set_fd_handler(int fd,
5850 5fafdf24 ths
                        IOHandler *fd_read,
5851 5fafdf24 ths
                        IOHandler *fd_write,
5852 7c9d8e07 bellard
                        void *opaque)
5853 8a7ddc38 bellard
{
5854 7c9d8e07 bellard
    return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque);
5855 8a7ddc38 bellard
}
5856 8a7ddc38 bellard
5857 8a7ddc38 bellard
/***********************************************************/
5858 f331110f bellard
/* Polling handling */
5859 f331110f bellard
5860 f331110f bellard
typedef struct PollingEntry {
5861 f331110f bellard
    PollingFunc *func;
5862 f331110f bellard
    void *opaque;
5863 f331110f bellard
    struct PollingEntry *next;
5864 f331110f bellard
} PollingEntry;
5865 f331110f bellard
5866 f331110f bellard
static PollingEntry *first_polling_entry;
5867 f331110f bellard
5868 f331110f bellard
int qemu_add_polling_cb(PollingFunc *func, void *opaque)
5869 f331110f bellard
{
5870 f331110f bellard
    PollingEntry **ppe, *pe;
5871 f331110f bellard
    pe = qemu_mallocz(sizeof(PollingEntry));
5872 f331110f bellard
    if (!pe)
5873 f331110f bellard
        return -1;
5874 f331110f bellard
    pe->func = func;
5875 f331110f bellard
    pe->opaque = opaque;
5876 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
5877 f331110f bellard
    *ppe = pe;
5878 f331110f bellard
    return 0;
5879 f331110f bellard
}
5880 f331110f bellard
5881 f331110f bellard
void qemu_del_polling_cb(PollingFunc *func, void *opaque)
5882 f331110f bellard
{
5883 f331110f bellard
    PollingEntry **ppe, *pe;
5884 f331110f bellard
    for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
5885 f331110f bellard
        pe = *ppe;
5886 f331110f bellard
        if (pe->func == func && pe->opaque == opaque) {
5887 f331110f bellard
            *ppe = pe->next;
5888 f331110f bellard
            qemu_free(pe);
5889 f331110f bellard
            break;
5890 f331110f bellard
        }
5891 f331110f bellard
    }
5892 f331110f bellard
}
5893 f331110f bellard
5894 a18e524a bellard
#ifdef _WIN32
5895 a18e524a bellard
/***********************************************************/
5896 a18e524a bellard
/* Wait objects support */
5897 a18e524a bellard
typedef struct WaitObjects {
5898 a18e524a bellard
    int num;
5899 a18e524a bellard
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
5900 a18e524a bellard
    WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
5901 a18e524a bellard
    void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
5902 a18e524a bellard
} WaitObjects;
5903 a18e524a bellard
5904 a18e524a bellard
static WaitObjects wait_objects = {0};
5905 3b46e624 ths
5906 a18e524a bellard
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5907 a18e524a bellard
{
5908 a18e524a bellard
    WaitObjects *w = &wait_objects;
5909 a18e524a bellard
5910 a18e524a bellard
    if (w->num >= MAXIMUM_WAIT_OBJECTS)
5911 a18e524a bellard
        return -1;
5912 a18e524a bellard
    w->events[w->num] = handle;
5913 a18e524a bellard
    w->func[w->num] = func;
5914 a18e524a bellard
    w->opaque[w->num] = opaque;
5915 a18e524a bellard
    w->num++;
5916 a18e524a bellard
    return 0;
5917 a18e524a bellard
}
5918 a18e524a bellard
5919 a18e524a bellard
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
5920 a18e524a bellard
{
5921 a18e524a bellard
    int i, found;
5922 a18e524a bellard
    WaitObjects *w = &wait_objects;
5923 a18e524a bellard
5924 a18e524a bellard
    found = 0;
5925 a18e524a bellard
    for (i = 0; i < w->num; i++) {
5926 a18e524a bellard
        if (w->events[i] == handle)
5927 a18e524a bellard
            found = 1;
5928 a18e524a bellard
        if (found) {
5929 a18e524a bellard
            w->events[i] = w->events[i + 1];
5930 a18e524a bellard
            w->func[i] = w->func[i + 1];
5931 a18e524a bellard
            w->opaque[i] = w->opaque[i + 1];
5932 3b46e624 ths
        }
5933 a18e524a bellard
    }
5934 a18e524a bellard
    if (found)
5935 a18e524a bellard
        w->num--;
5936 a18e524a bellard
}
5937 a18e524a bellard
#endif
5938 a18e524a bellard
5939 f331110f bellard
/***********************************************************/
5940 8a7ddc38 bellard
/* savevm/loadvm support */
5941 8a7ddc38 bellard
5942 faea38e7 bellard
#define IO_BUF_SIZE 32768
5943 faea38e7 bellard
5944 faea38e7 bellard
struct QEMUFile {
5945 faea38e7 bellard
    FILE *outfile;
5946 faea38e7 bellard
    BlockDriverState *bs;
5947 faea38e7 bellard
    int is_file;
5948 faea38e7 bellard
    int is_writable;
5949 faea38e7 bellard
    int64_t base_offset;
5950 faea38e7 bellard
    int64_t buf_offset; /* start of buffer when writing, end of buffer
5951 faea38e7 bellard
                           when reading */
5952 faea38e7 bellard
    int buf_index;
5953 faea38e7 bellard
    int buf_size; /* 0 when writing */
5954 faea38e7 bellard
    uint8_t buf[IO_BUF_SIZE];
5955 faea38e7 bellard
};
5956 faea38e7 bellard
5957 faea38e7 bellard
QEMUFile *qemu_fopen(const char *filename, const char *mode)
5958 faea38e7 bellard
{
5959 faea38e7 bellard
    QEMUFile *f;
5960 faea38e7 bellard
5961 faea38e7 bellard
    f = qemu_mallocz(sizeof(QEMUFile));
5962 faea38e7 bellard
    if (!f)
5963 faea38e7 bellard
        return NULL;
5964 faea38e7 bellard
    if (!strcmp(mode, "wb")) {
5965 faea38e7 bellard
        f->is_writable = 1;
5966 faea38e7 bellard
    } else if (!strcmp(mode, "rb")) {
5967 faea38e7 bellard
        f->is_writable = 0;
5968 faea38e7 bellard
    } else {
5969 faea38e7 bellard
        goto fail;
5970 faea38e7 bellard
    }
5971 faea38e7 bellard
    f->outfile = fopen(filename, mode);
5972 faea38e7 bellard
    if (!f->outfile)
5973 faea38e7 bellard
        goto fail;
5974 faea38e7 bellard
    f->is_file = 1;
5975 faea38e7 bellard
    return f;
5976 faea38e7 bellard
 fail:
5977 faea38e7 bellard
    if (f->outfile)
5978 faea38e7 bellard
        fclose(f->outfile);
5979 faea38e7 bellard
    qemu_free(f);
5980 faea38e7 bellard
    return NULL;
5981 faea38e7 bellard
}
5982 faea38e7 bellard
5983 9596ebb7 pbrook
static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
5984 faea38e7 bellard
{
5985 faea38e7 bellard
    QEMUFile *f;
5986 faea38e7 bellard
5987 faea38e7 bellard
    f = qemu_mallocz(sizeof(QEMUFile));
5988 faea38e7 bellard
    if (!f)
5989 faea38e7 bellard
        return NULL;
5990 faea38e7 bellard
    f->is_file = 0;
5991 faea38e7 bellard
    f->bs = bs;
5992 faea38e7 bellard
    f->is_writable = is_writable;
5993 faea38e7 bellard
    f->base_offset = offset;
5994 faea38e7 bellard
    return f;
5995 faea38e7 bellard
}
5996 faea38e7 bellard
5997 faea38e7 bellard
void qemu_fflush(QEMUFile *f)
5998 faea38e7 bellard
{
5999 faea38e7 bellard
    if (!f->is_writable)
6000 faea38e7 bellard
        return;
6001 faea38e7 bellard
    if (f->buf_index > 0) {
6002 faea38e7 bellard
        if (f->is_file) {
6003 faea38e7 bellard
            fseek(f->outfile, f->buf_offset, SEEK_SET);
6004 faea38e7 bellard
            fwrite(f->buf, 1, f->buf_index, f->outfile);
6005 faea38e7 bellard
        } else {
6006 5fafdf24 ths
            bdrv_pwrite(f->bs, f->base_offset + f->buf_offset,
6007 faea38e7 bellard
                        f->buf, f->buf_index);
6008 faea38e7 bellard
        }
6009 faea38e7 bellard
        f->buf_offset += f->buf_index;
6010 faea38e7 bellard
        f->buf_index = 0;
6011 faea38e7 bellard
    }
6012 faea38e7 bellard
}
6013 faea38e7 bellard
6014 faea38e7 bellard
static void qemu_fill_buffer(QEMUFile *f)
6015 faea38e7 bellard
{
6016 faea38e7 bellard
    int len;
6017 faea38e7 bellard
6018 faea38e7 bellard
    if (f->is_writable)
6019 faea38e7 bellard
        return;
6020 faea38e7 bellard
    if (f->is_file) {
6021 faea38e7 bellard
        fseek(f->outfile, f->buf_offset, SEEK_SET);
6022 faea38e7 bellard
        len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
6023 faea38e7 bellard
        if (len < 0)
6024 faea38e7 bellard
            len = 0;
6025 faea38e7 bellard
    } else {
6026 5fafdf24 ths
        len = bdrv_pread(f->bs, f->base_offset + f->buf_offset,
6027 faea38e7 bellard
                         f->buf, IO_BUF_SIZE);
6028 faea38e7 bellard
        if (len < 0)
6029 faea38e7 bellard
            len = 0;
6030 faea38e7 bellard
    }
6031 faea38e7 bellard
    f->buf_index = 0;
6032 faea38e7 bellard
    f->buf_size = len;
6033 faea38e7 bellard
    f->buf_offset += len;
6034 faea38e7 bellard
}
6035 faea38e7 bellard
6036 faea38e7 bellard
void qemu_fclose(QEMUFile *f)
6037 faea38e7 bellard
{
6038 faea38e7 bellard
    if (f->is_writable)
6039 faea38e7 bellard
        qemu_fflush(f);
6040 faea38e7 bellard
    if (f->is_file) {
6041 faea38e7 bellard
        fclose(f->outfile);
6042 faea38e7 bellard
    }
6043 faea38e7 bellard
    qemu_free(f);
6044 faea38e7 bellard
}
6045 faea38e7 bellard
6046 8a7ddc38 bellard
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
6047 b4608c04 bellard
{
6048 faea38e7 bellard
    int l;
6049 faea38e7 bellard
    while (size > 0) {
6050 faea38e7 bellard
        l = IO_BUF_SIZE - f->buf_index;
6051 faea38e7 bellard
        if (l > size)
6052 faea38e7 bellard
            l = size;
6053 faea38e7 bellard
        memcpy(f->buf + f->buf_index, buf, l);
6054 faea38e7 bellard
        f->buf_index += l;
6055 faea38e7 bellard
        buf += l;
6056 faea38e7 bellard
        size -= l;
6057 faea38e7 bellard
        if (f->buf_index >= IO_BUF_SIZE)
6058 faea38e7 bellard
            qemu_fflush(f);
6059 faea38e7 bellard
    }
6060 b4608c04 bellard
}
6061 b4608c04 bellard
6062 8a7ddc38 bellard
void qemu_put_byte(QEMUFile *f, int v)
6063 b4608c04 bellard
{
6064 faea38e7 bellard
    f->buf[f->buf_index++] = v;
6065 faea38e7 bellard
    if (f->buf_index >= IO_BUF_SIZE)
6066 faea38e7 bellard
        qemu_fflush(f);
6067 faea38e7 bellard
}
6068 faea38e7 bellard
6069 faea38e7 bellard
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
6070 faea38e7 bellard
{
6071 faea38e7 bellard
    int size, l;
6072 faea38e7 bellard
6073 faea38e7 bellard
    size = size1;
6074 faea38e7 bellard
    while (size > 0) {
6075 faea38e7 bellard
        l = f->buf_size - f->buf_index;
6076 faea38e7 bellard
        if (l == 0) {
6077 faea38e7 bellard
            qemu_fill_buffer(f);
6078 faea38e7 bellard
            l = f->buf_size - f->buf_index;
6079 faea38e7 bellard
            if (l == 0)
6080 faea38e7 bellard
                break;
6081 faea38e7 bellard
        }
6082 faea38e7 bellard
        if (l > size)
6083 faea38e7 bellard
            l = size;
6084 faea38e7 bellard
        memcpy(buf, f->buf + f->buf_index, l);
6085 faea38e7 bellard
        f->buf_index += l;
6086 faea38e7 bellard
        buf += l;
6087 faea38e7 bellard
        size -= l;
6088 faea38e7 bellard
    }
6089 faea38e7 bellard
    return size1 - size;
6090 faea38e7 bellard
}
6091 faea38e7 bellard
6092 faea38e7 bellard
int qemu_get_byte(QEMUFile *f)
6093 faea38e7 bellard
{
6094 faea38e7 bellard
    if (f->buf_index >= f->buf_size) {
6095 faea38e7 bellard
        qemu_fill_buffer(f);
6096 faea38e7 bellard
        if (f->buf_index >= f->buf_size)
6097 faea38e7 bellard
            return 0;
6098 faea38e7 bellard
    }
6099 faea38e7 bellard
    return f->buf[f->buf_index++];
6100 faea38e7 bellard
}
6101 faea38e7 bellard
6102 faea38e7 bellard
int64_t qemu_ftell(QEMUFile *f)
6103 faea38e7 bellard
{
6104 faea38e7 bellard
    return f->buf_offset - f->buf_size + f->buf_index;
6105 faea38e7 bellard
}
6106 faea38e7 bellard
6107 faea38e7 bellard
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
6108 faea38e7 bellard
{
6109 faea38e7 bellard
    if (whence == SEEK_SET) {
6110 faea38e7 bellard
        /* nothing to do */
6111 faea38e7 bellard
    } else if (whence == SEEK_CUR) {
6112 faea38e7 bellard
        pos += qemu_ftell(f);
6113 faea38e7 bellard
    } else {
6114 faea38e7 bellard
        /* SEEK_END not supported */
6115 faea38e7 bellard
        return -1;
6116 faea38e7 bellard
    }
6117 faea38e7 bellard
    if (f->is_writable) {
6118 faea38e7 bellard
        qemu_fflush(f);
6119 faea38e7 bellard
        f->buf_offset = pos;
6120 faea38e7 bellard
    } else {
6121 faea38e7 bellard
        f->buf_offset = pos;
6122 faea38e7 bellard
        f->buf_index = 0;
6123 faea38e7 bellard
        f->buf_size = 0;
6124 faea38e7 bellard
    }
6125 faea38e7 bellard
    return pos;
6126 8a7ddc38 bellard
}
6127 8a7ddc38 bellard
6128 8a7ddc38 bellard
void qemu_put_be16(QEMUFile *f, unsigned int v)
6129 8a7ddc38 bellard
{
6130 8a7ddc38 bellard
    qemu_put_byte(f, v >> 8);
6131 8a7ddc38 bellard
    qemu_put_byte(f, v);
6132 8a7ddc38 bellard
}
6133 8a7ddc38 bellard
6134 8a7ddc38 bellard
void qemu_put_be32(QEMUFile *f, unsigned int v)
6135 8a7ddc38 bellard
{
6136 8a7ddc38 bellard
    qemu_put_byte(f, v >> 24);
6137 8a7ddc38 bellard
    qemu_put_byte(f, v >> 16);
6138 8a7ddc38 bellard
    qemu_put_byte(f, v >> 8);
6139 8a7ddc38 bellard
    qemu_put_byte(f, v);
6140 8a7ddc38 bellard
}
6141 8a7ddc38 bellard
6142 8a7ddc38 bellard
void qemu_put_be64(QEMUFile *f, uint64_t v)
6143 8a7ddc38 bellard
{
6144 8a7ddc38 bellard
    qemu_put_be32(f, v >> 32);
6145 8a7ddc38 bellard
    qemu_put_be32(f, v);
6146 8a7ddc38 bellard
}
6147 8a7ddc38 bellard
6148 8a7ddc38 bellard
unsigned int qemu_get_be16(QEMUFile *f)
6149 8a7ddc38 bellard
{
6150 8a7ddc38 bellard
    unsigned int v;
6151 8a7ddc38 bellard
    v = qemu_get_byte(f) << 8;
6152 8a7ddc38 bellard
    v |= qemu_get_byte(f);
6153 8a7ddc38 bellard
    return v;
6154 8a7ddc38 bellard
}
6155 8a7ddc38 bellard
6156 8a7ddc38 bellard
unsigned int qemu_get_be32(QEMUFile *f)
6157 8a7ddc38 bellard
{
6158 8a7ddc38 bellard
    unsigned int v;
6159 8a7ddc38 bellard
    v = qemu_get_byte(f) << 24;
6160 8a7ddc38 bellard
    v |= qemu_get_byte(f) << 16;
6161 8a7ddc38 bellard
    v |= qemu_get_byte(f) << 8;
6162 8a7ddc38 bellard
    v |= qemu_get_byte(f);
6163 8a7ddc38 bellard
    return v;
6164 8a7ddc38 bellard
}
6165 8a7ddc38 bellard
6166 8a7ddc38 bellard
uint64_t qemu_get_be64(QEMUFile *f)
6167 8a7ddc38 bellard
{
6168 8a7ddc38 bellard
    uint64_t v;
6169 8a7ddc38 bellard
    v = (uint64_t)qemu_get_be32(f) << 32;
6170 8a7ddc38 bellard
    v |= qemu_get_be32(f);
6171 8a7ddc38 bellard
    return v;
6172 8a7ddc38 bellard
}
6173 8a7ddc38 bellard
6174 8a7ddc38 bellard
typedef struct SaveStateEntry {
6175 8a7ddc38 bellard
    char idstr[256];
6176 8a7ddc38 bellard
    int instance_id;
6177 8a7ddc38 bellard
    int version_id;
6178 8a7ddc38 bellard
    SaveStateHandler *save_state;
6179 8a7ddc38 bellard
    LoadStateHandler *load_state;
6180 8a7ddc38 bellard
    void *opaque;
6181 8a7ddc38 bellard
    struct SaveStateEntry *next;
6182 8a7ddc38 bellard
} SaveStateEntry;
6183 b4608c04 bellard
6184 8a7ddc38 bellard
static SaveStateEntry *first_se;
6185 8a7ddc38 bellard
6186 18be5187 pbrook
/* TODO: Individual devices generally have very little idea about the rest
6187 18fdb1c5 ths
   of the system, so instance_id should be removed/replaced.
6188 18fdb1c5 ths
   Meanwhile pass -1 as instance_id if you do not already have a clearly
6189 18fdb1c5 ths
   distinguishing id for all instances of your device class. */
6190 5fafdf24 ths
int register_savevm(const char *idstr,
6191 5fafdf24 ths
                    int instance_id,
6192 8a7ddc38 bellard
                    int version_id,
6193 8a7ddc38 bellard
                    SaveStateHandler *save_state,
6194 8a7ddc38 bellard
                    LoadStateHandler *load_state,
6195 8a7ddc38 bellard
                    void *opaque)
6196 8a7ddc38 bellard
{
6197 8a7ddc38 bellard
    SaveStateEntry *se, **pse;
6198 8a7ddc38 bellard
6199 8a7ddc38 bellard
    se = qemu_malloc(sizeof(SaveStateEntry));
6200 8a7ddc38 bellard
    if (!se)
6201 8a7ddc38 bellard
        return -1;
6202 8a7ddc38 bellard
    pstrcpy(se->idstr, sizeof(se->idstr), idstr);
6203 18be5187 pbrook
    se->instance_id = (instance_id == -1) ? 0 : instance_id;
6204 8a7ddc38 bellard
    se->version_id = version_id;
6205 8a7ddc38 bellard
    se->save_state = save_state;
6206 8a7ddc38 bellard
    se->load_state = load_state;
6207 8a7ddc38 bellard
    se->opaque = opaque;
6208 8a7ddc38 bellard
    se->next = NULL;
6209 8a7ddc38 bellard
6210 8a7ddc38 bellard
    /* add at the end of list */
6211 8a7ddc38 bellard
    pse = &first_se;
6212 18be5187 pbrook
    while (*pse != NULL) {
6213 18be5187 pbrook
        if (instance_id == -1
6214 18be5187 pbrook
                && strcmp(se->idstr, (*pse)->idstr) == 0
6215 18be5187 pbrook
                && se->instance_id <= (*pse)->instance_id)
6216 18be5187 pbrook
            se->instance_id = (*pse)->instance_id + 1;
6217 8a7ddc38 bellard
        pse = &(*pse)->next;
6218 18be5187 pbrook
    }
6219 8a7ddc38 bellard
    *pse = se;
6220 8a7ddc38 bellard
    return 0;
6221 8a7ddc38 bellard
}
6222 8a7ddc38 bellard
6223 8a7ddc38 bellard
#define QEMU_VM_FILE_MAGIC   0x5145564d
6224 faea38e7 bellard
#define QEMU_VM_FILE_VERSION 0x00000002
6225 8a7ddc38 bellard
6226 9596ebb7 pbrook
static int qemu_savevm_state(QEMUFile *f)
6227 8a7ddc38 bellard
{
6228 8a7ddc38 bellard
    SaveStateEntry *se;
6229 faea38e7 bellard
    int len, ret;
6230 faea38e7 bellard
    int64_t cur_pos, len_pos, total_len_pos;
6231 313aa567 bellard
6232 8a7ddc38 bellard
    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
6233 8a7ddc38 bellard
    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
6234 faea38e7 bellard
    total_len_pos = qemu_ftell(f);
6235 faea38e7 bellard
    qemu_put_be64(f, 0); /* total size */
6236 8a7ddc38 bellard
6237 8a7ddc38 bellard
    for(se = first_se; se != NULL; se = se->next) {
6238 d978c02c aurel32
        if (se->save_state == NULL)
6239 d978c02c aurel32
            /* this one has a loader only, for backwards compatibility */
6240 d978c02c aurel32
            continue;
6241 d978c02c aurel32
6242 8a7ddc38 bellard
        /* ID string */
6243 8a7ddc38 bellard
        len = strlen(se->idstr);
6244 8a7ddc38 bellard
        qemu_put_byte(f, len);
6245 ffe8ab83 ths
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
6246 8a7ddc38 bellard
6247 8a7ddc38 bellard
        qemu_put_be32(f, se->instance_id);
6248 8a7ddc38 bellard
        qemu_put_be32(f, se->version_id);
6249 8a7ddc38 bellard
6250 8a7ddc38 bellard
        /* record size: filled later */
6251 faea38e7 bellard
        len_pos = qemu_ftell(f);
6252 8a7ddc38 bellard
        qemu_put_be32(f, 0);
6253 8a7ddc38 bellard
        se->save_state(f, se->opaque);
6254 8a7ddc38 bellard
6255 8a7ddc38 bellard
        /* fill record size */
6256 faea38e7 bellard
        cur_pos = qemu_ftell(f);
6257 faea38e7 bellard
        len = cur_pos - len_pos - 4;
6258 faea38e7 bellard
        qemu_fseek(f, len_pos, SEEK_SET);
6259 8a7ddc38 bellard
        qemu_put_be32(f, len);
6260 faea38e7 bellard
        qemu_fseek(f, cur_pos, SEEK_SET);
6261 8a7ddc38 bellard
    }
6262 faea38e7 bellard
    cur_pos = qemu_ftell(f);
6263 faea38e7 bellard
    qemu_fseek(f, total_len_pos, SEEK_SET);
6264 faea38e7 bellard
    qemu_put_be64(f, cur_pos - total_len_pos - 8);
6265 faea38e7 bellard
    qemu_fseek(f, cur_pos, SEEK_SET);
6266 8a7ddc38 bellard
6267 8a7ddc38 bellard
    ret = 0;
6268 8a7ddc38 bellard
    return ret;
6269 8a7ddc38 bellard
}
6270 8a7ddc38 bellard
6271 8a7ddc38 bellard
static SaveStateEntry *find_se(const char *idstr, int instance_id)
6272 8a7ddc38 bellard
{
6273 8a7ddc38 bellard
    SaveStateEntry *se;
6274 8a7ddc38 bellard
6275 8a7ddc38 bellard
    for(se = first_se; se != NULL; se = se->next) {
6276 5fafdf24 ths
        if (!strcmp(se->idstr, idstr) &&
6277 8a7ddc38 bellard
            instance_id == se->instance_id)
6278 8a7ddc38 bellard
            return se;
6279 8a7ddc38 bellard
    }
6280 8a7ddc38 bellard
    return NULL;
6281 8a7ddc38 bellard
}
6282 8a7ddc38 bellard
6283 9596ebb7 pbrook
static int qemu_loadvm_state(QEMUFile *f)
6284 8a7ddc38 bellard
{
6285 8a7ddc38 bellard
    SaveStateEntry *se;
6286 faea38e7 bellard
    int len, ret, instance_id, record_len, version_id;
6287 faea38e7 bellard
    int64_t total_len, end_pos, cur_pos;
6288 8a7ddc38 bellard
    unsigned int v;
6289 8a7ddc38 bellard
    char idstr[256];
6290 3b46e624 ths
6291 8a7ddc38 bellard
    v = qemu_get_be32(f);
6292 8a7ddc38 bellard
    if (v != QEMU_VM_FILE_MAGIC)
6293 8a7ddc38 bellard
        goto fail;
6294 8a7ddc38 bellard
    v = qemu_get_be32(f);
6295 8a7ddc38 bellard
    if (v != QEMU_VM_FILE_VERSION) {
6296 8a7ddc38 bellard
    fail:
6297 8a7ddc38 bellard
        ret = -1;
6298 8a7ddc38 bellard
        goto the_end;
6299 8a7ddc38 bellard
    }
6300 faea38e7 bellard
    total_len = qemu_get_be64(f);
6301 faea38e7 bellard
    end_pos = total_len + qemu_ftell(f);
6302 b4608c04 bellard
    for(;;) {
6303 faea38e7 bellard
        if (qemu_ftell(f) >= end_pos)
6304 cd4c3e88 bellard
            break;
6305 faea38e7 bellard
        len = qemu_get_byte(f);
6306 ffe8ab83 ths
        qemu_get_buffer(f, (uint8_t *)idstr, len);
6307 8a7ddc38 bellard
        idstr[len] = '\0';
6308 8a7ddc38 bellard
        instance_id = qemu_get_be32(f);
6309 8a7ddc38 bellard
        version_id = qemu_get_be32(f);
6310 8a7ddc38 bellard
        record_len = qemu_get_be32(f);
6311 8a7ddc38 bellard
#if 0
6312 5fafdf24 ths
        printf("idstr=%s instance=0x%x version=%d len=%d\n",
6313 8a7ddc38 bellard
               idstr, instance_id, version_id, record_len);
6314 8a7ddc38 bellard
#endif
6315 faea38e7 bellard
        cur_pos = qemu_ftell(f);
6316 8a7ddc38 bellard
        se = find_se(idstr, instance_id);
6317 8a7ddc38 bellard
        if (!se) {
6318 5fafdf24 ths
            fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
6319 8a7ddc38 bellard
                    instance_id, idstr);
6320 8a7ddc38 bellard
        } else {
6321 8a7ddc38 bellard
            ret = se->load_state(f, se->opaque, version_id);
6322 8a7ddc38 bellard
            if (ret < 0) {
6323 5fafdf24 ths
                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
6324 8a7ddc38 bellard
                        instance_id, idstr);
6325 8a7ddc38 bellard
            }
6326 34865134 bellard
        }
6327 8a7ddc38 bellard
        /* always seek to exact end of record */
6328 8a7ddc38 bellard
        qemu_fseek(f, cur_pos + record_len, SEEK_SET);
6329 8a7ddc38 bellard
    }
6330 8a7ddc38 bellard
    ret = 0;
6331 8a7ddc38 bellard
 the_end:
6332 faea38e7 bellard
    return ret;
6333 faea38e7 bellard
}
6334 faea38e7 bellard
6335 faea38e7 bellard
/* device can contain snapshots */
6336 faea38e7 bellard
static int bdrv_can_snapshot(BlockDriverState *bs)
6337 faea38e7 bellard
{
6338 faea38e7 bellard
    return (bs &&
6339 faea38e7 bellard
            !bdrv_is_removable(bs) &&
6340 faea38e7 bellard
            !bdrv_is_read_only(bs));
6341 faea38e7 bellard
}
6342 faea38e7 bellard
6343 faea38e7 bellard
/* device must be snapshots in order to have a reliable snapshot */
6344 faea38e7 bellard
static int bdrv_has_snapshot(BlockDriverState *bs)
6345 faea38e7 bellard
{
6346 faea38e7 bellard
    return (bs &&
6347 faea38e7 bellard
            !bdrv_is_removable(bs) &&
6348 faea38e7 bellard
            !bdrv_is_read_only(bs));
6349 faea38e7 bellard
}
6350 faea38e7 bellard
6351 faea38e7 bellard
static BlockDriverState *get_bs_snapshots(void)
6352 faea38e7 bellard
{
6353 faea38e7 bellard
    BlockDriverState *bs;
6354 faea38e7 bellard
    int i;
6355 faea38e7 bellard
6356 faea38e7 bellard
    if (bs_snapshots)
6357 faea38e7 bellard
        return bs_snapshots;
6358 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
6359 e4bcb14c ths
        bs = drives_table[i].bdrv;
6360 faea38e7 bellard
        if (bdrv_can_snapshot(bs))
6361 faea38e7 bellard
            goto ok;
6362 faea38e7 bellard
    }
6363 faea38e7 bellard
    return NULL;
6364 faea38e7 bellard
 ok:
6365 faea38e7 bellard
    bs_snapshots = bs;
6366 faea38e7 bellard
    return bs;
6367 faea38e7 bellard
}
6368 faea38e7 bellard
6369 faea38e7 bellard
static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
6370 faea38e7 bellard
                              const char *name)
6371 faea38e7 bellard
{
6372 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
6373 faea38e7 bellard
    int nb_sns, i, ret;
6374 3b46e624 ths
6375 faea38e7 bellard
    ret = -ENOENT;
6376 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
6377 faea38e7 bellard
    if (nb_sns < 0)
6378 faea38e7 bellard
        return ret;
6379 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
6380 faea38e7 bellard
        sn = &sn_tab[i];
6381 faea38e7 bellard
        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
6382 faea38e7 bellard
            *sn_info = *sn;
6383 faea38e7 bellard
            ret = 0;
6384 faea38e7 bellard
            break;
6385 faea38e7 bellard
        }
6386 faea38e7 bellard
    }
6387 faea38e7 bellard
    qemu_free(sn_tab);
6388 faea38e7 bellard
    return ret;
6389 faea38e7 bellard
}
6390 faea38e7 bellard
6391 faea38e7 bellard
void do_savevm(const char *name)
6392 faea38e7 bellard
{
6393 faea38e7 bellard
    BlockDriverState *bs, *bs1;
6394 faea38e7 bellard
    QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
6395 faea38e7 bellard
    int must_delete, ret, i;
6396 faea38e7 bellard
    BlockDriverInfo bdi1, *bdi = &bdi1;
6397 faea38e7 bellard
    QEMUFile *f;
6398 faea38e7 bellard
    int saved_vm_running;
6399 4c279bdf bellard
#ifdef _WIN32
6400 4c279bdf bellard
    struct _timeb tb;
6401 4c279bdf bellard
#else
6402 faea38e7 bellard
    struct timeval tv;
6403 4c279bdf bellard
#endif
6404 faea38e7 bellard
6405 faea38e7 bellard
    bs = get_bs_snapshots();
6406 faea38e7 bellard
    if (!bs) {
6407 faea38e7 bellard
        term_printf("No block device can accept snapshots\n");
6408 faea38e7 bellard
        return;
6409 faea38e7 bellard
    }
6410 faea38e7 bellard
6411 6192bc37 pbrook
    /* ??? Should this occur after vm_stop?  */
6412 6192bc37 pbrook
    qemu_aio_flush();
6413 6192bc37 pbrook
6414 faea38e7 bellard
    saved_vm_running = vm_running;
6415 faea38e7 bellard
    vm_stop(0);
6416 3b46e624 ths
6417 faea38e7 bellard
    must_delete = 0;
6418 faea38e7 bellard
    if (name) {
6419 faea38e7 bellard
        ret = bdrv_snapshot_find(bs, old_sn, name);
6420 faea38e7 bellard
        if (ret >= 0) {
6421 faea38e7 bellard
            must_delete = 1;
6422 faea38e7 bellard
        }
6423 faea38e7 bellard
    }
6424 faea38e7 bellard
    memset(sn, 0, sizeof(*sn));
6425 faea38e7 bellard
    if (must_delete) {
6426 faea38e7 bellard
        pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
6427 faea38e7 bellard
        pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
6428 faea38e7 bellard
    } else {
6429 faea38e7 bellard
        if (name)
6430 faea38e7 bellard
            pstrcpy(sn->name, sizeof(sn->name), name);
6431 faea38e7 bellard
    }
6432 faea38e7 bellard
6433 faea38e7 bellard
    /* fill auxiliary fields */
6434 4c279bdf bellard
#ifdef _WIN32
6435 4c279bdf bellard
    _ftime(&tb);
6436 4c279bdf bellard
    sn->date_sec = tb.time;
6437 4c279bdf bellard
    sn->date_nsec = tb.millitm * 1000000;
6438 4c279bdf bellard
#else
6439 faea38e7 bellard
    gettimeofday(&tv, NULL);
6440 faea38e7 bellard
    sn->date_sec = tv.tv_sec;
6441 faea38e7 bellard
    sn->date_nsec = tv.tv_usec * 1000;
6442 4c279bdf bellard
#endif
6443 faea38e7 bellard
    sn->vm_clock_nsec = qemu_get_clock(vm_clock);
6444 3b46e624 ths
6445 faea38e7 bellard
    if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6446 faea38e7 bellard
        term_printf("Device %s does not support VM state snapshots\n",
6447 faea38e7 bellard
                    bdrv_get_device_name(bs));
6448 faea38e7 bellard
        goto the_end;
6449 faea38e7 bellard
    }
6450 3b46e624 ths
6451 faea38e7 bellard
    /* save the VM state */
6452 faea38e7 bellard
    f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
6453 faea38e7 bellard
    if (!f) {
6454 faea38e7 bellard
        term_printf("Could not open VM state file\n");
6455 faea38e7 bellard
        goto the_end;
6456 faea38e7 bellard
    }
6457 faea38e7 bellard
    ret = qemu_savevm_state(f);
6458 faea38e7 bellard
    sn->vm_state_size = qemu_ftell(f);
6459 faea38e7 bellard
    qemu_fclose(f);
6460 faea38e7 bellard
    if (ret < 0) {
6461 faea38e7 bellard
        term_printf("Error %d while writing VM\n", ret);
6462 faea38e7 bellard
        goto the_end;
6463 faea38e7 bellard
    }
6464 3b46e624 ths
6465 faea38e7 bellard
    /* create the snapshots */
6466 faea38e7 bellard
6467 e4bcb14c ths
    for(i = 0; i < nb_drives; i++) {
6468 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
6469 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
6470 faea38e7 bellard
            if (must_delete) {
6471 faea38e7 bellard
                ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
6472 faea38e7 bellard
                if (ret < 0) {
6473 faea38e7 bellard
                    term_printf("Error while deleting snapshot on '%s'\n",
6474 faea38e7 bellard
                                bdrv_get_device_name(bs1));
6475 faea38e7 bellard
                }
6476 faea38e7 bellard
            }
6477 faea38e7 bellard
            ret = bdrv_snapshot_create(bs1, sn);
6478 faea38e7 bellard
            if (ret < 0) {
6479 faea38e7 bellard
                term_printf("Error while creating snapshot on '%s'\n",
6480 faea38e7 bellard
                            bdrv_get_device_name(bs1));
6481 faea38e7 bellard
            }
6482 faea38e7 bellard
        }
6483 faea38e7 bellard
    }
6484 faea38e7 bellard
6485 faea38e7 bellard
 the_end:
6486 8a7ddc38 bellard
    if (saved_vm_running)
6487 8a7ddc38 bellard
        vm_start();
6488 faea38e7 bellard
}
6489 faea38e7 bellard
6490 faea38e7 bellard
void do_loadvm(const char *name)
6491 faea38e7 bellard
{
6492 faea38e7 bellard
    BlockDriverState *bs, *bs1;
6493 faea38e7 bellard
    BlockDriverInfo bdi1, *bdi = &bdi1;
6494 faea38e7 bellard
    QEMUFile *f;
6495 faea38e7 bellard
    int i, ret;
6496 faea38e7 bellard
    int saved_vm_running;
6497 faea38e7 bellard
6498 faea38e7 bellard
    bs = get_bs_snapshots();
6499 faea38e7 bellard
    if (!bs) {
6500 faea38e7 bellard
        term_printf("No block device supports snapshots\n");
6501 faea38e7 bellard
        return;
6502 faea38e7 bellard
    }
6503 3b46e624 ths
6504 6192bc37 pbrook
    /* Flush all IO requests so they don't interfere with the new state.  */
6505 6192bc37 pbrook
    qemu_aio_flush();
6506 6192bc37 pbrook
6507 faea38e7 bellard
    saved_vm_running = vm_running;
6508 faea38e7 bellard
    vm_stop(0);
6509 faea38e7 bellard
6510 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
6511 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
6512 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
6513 faea38e7 bellard
            ret = bdrv_snapshot_goto(bs1, name);
6514 faea38e7 bellard
            if (ret < 0) {
6515 faea38e7 bellard
                if (bs != bs1)
6516 faea38e7 bellard
                    term_printf("Warning: ");
6517 faea38e7 bellard
                switch(ret) {
6518 faea38e7 bellard
                case -ENOTSUP:
6519 faea38e7 bellard
                    term_printf("Snapshots not supported on device '%s'\n",
6520 faea38e7 bellard
                                bdrv_get_device_name(bs1));
6521 faea38e7 bellard
                    break;
6522 faea38e7 bellard
                case -ENOENT:
6523 faea38e7 bellard
                    term_printf("Could not find snapshot '%s' on device '%s'\n",
6524 faea38e7 bellard
                                name, bdrv_get_device_name(bs1));
6525 faea38e7 bellard
                    break;
6526 faea38e7 bellard
                default:
6527 faea38e7 bellard
                    term_printf("Error %d while activating snapshot on '%s'\n",
6528 faea38e7 bellard
                                ret, bdrv_get_device_name(bs1));
6529 faea38e7 bellard
                    break;
6530 faea38e7 bellard
                }
6531 faea38e7 bellard
                /* fatal on snapshot block device */
6532 faea38e7 bellard
                if (bs == bs1)
6533 faea38e7 bellard
                    goto the_end;
6534 faea38e7 bellard
            }
6535 faea38e7 bellard
        }
6536 faea38e7 bellard
    }
6537 faea38e7 bellard
6538 faea38e7 bellard
    if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
6539 faea38e7 bellard
        term_printf("Device %s does not support VM state snapshots\n",
6540 faea38e7 bellard
                    bdrv_get_device_name(bs));
6541 faea38e7 bellard
        return;
6542 faea38e7 bellard
    }
6543 3b46e624 ths
6544 faea38e7 bellard
    /* restore the VM state */
6545 faea38e7 bellard
    f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
6546 faea38e7 bellard
    if (!f) {
6547 faea38e7 bellard
        term_printf("Could not open VM state file\n");
6548 faea38e7 bellard
        goto the_end;
6549 faea38e7 bellard
    }
6550 faea38e7 bellard
    ret = qemu_loadvm_state(f);
6551 faea38e7 bellard
    qemu_fclose(f);
6552 faea38e7 bellard
    if (ret < 0) {
6553 faea38e7 bellard
        term_printf("Error %d while loading VM state\n", ret);
6554 faea38e7 bellard
    }
6555 faea38e7 bellard
 the_end:
6556 faea38e7 bellard
    if (saved_vm_running)
6557 faea38e7 bellard
        vm_start();
6558 faea38e7 bellard
}
6559 faea38e7 bellard
6560 faea38e7 bellard
void do_delvm(const char *name)
6561 faea38e7 bellard
{
6562 faea38e7 bellard
    BlockDriverState *bs, *bs1;
6563 faea38e7 bellard
    int i, ret;
6564 faea38e7 bellard
6565 faea38e7 bellard
    bs = get_bs_snapshots();
6566 faea38e7 bellard
    if (!bs) {
6567 faea38e7 bellard
        term_printf("No block device supports snapshots\n");
6568 faea38e7 bellard
        return;
6569 faea38e7 bellard
    }
6570 3b46e624 ths
6571 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
6572 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
6573 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
6574 faea38e7 bellard
            ret = bdrv_snapshot_delete(bs1, name);
6575 faea38e7 bellard
            if (ret < 0) {
6576 faea38e7 bellard
                if (ret == -ENOTSUP)
6577 faea38e7 bellard
                    term_printf("Snapshots not supported on device '%s'\n",
6578 faea38e7 bellard
                                bdrv_get_device_name(bs1));
6579 faea38e7 bellard
                else
6580 faea38e7 bellard
                    term_printf("Error %d while deleting snapshot on '%s'\n",
6581 faea38e7 bellard
                                ret, bdrv_get_device_name(bs1));
6582 faea38e7 bellard
            }
6583 faea38e7 bellard
        }
6584 faea38e7 bellard
    }
6585 faea38e7 bellard
}
6586 faea38e7 bellard
6587 faea38e7 bellard
void do_info_snapshots(void)
6588 faea38e7 bellard
{
6589 faea38e7 bellard
    BlockDriverState *bs, *bs1;
6590 faea38e7 bellard
    QEMUSnapshotInfo *sn_tab, *sn;
6591 faea38e7 bellard
    int nb_sns, i;
6592 faea38e7 bellard
    char buf[256];
6593 faea38e7 bellard
6594 faea38e7 bellard
    bs = get_bs_snapshots();
6595 faea38e7 bellard
    if (!bs) {
6596 faea38e7 bellard
        term_printf("No available block device supports snapshots\n");
6597 faea38e7 bellard
        return;
6598 faea38e7 bellard
    }
6599 faea38e7 bellard
    term_printf("Snapshot devices:");
6600 e4bcb14c ths
    for(i = 0; i <= nb_drives; i++) {
6601 e4bcb14c ths
        bs1 = drives_table[i].bdrv;
6602 faea38e7 bellard
        if (bdrv_has_snapshot(bs1)) {
6603 faea38e7 bellard
            if (bs == bs1)
6604 faea38e7 bellard
                term_printf(" %s", bdrv_get_device_name(bs1));
6605 faea38e7 bellard
        }
6606 faea38e7 bellard
    }
6607 faea38e7 bellard
    term_printf("\n");
6608 faea38e7 bellard
6609 faea38e7 bellard
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
6610 faea38e7 bellard
    if (nb_sns < 0) {
6611 faea38e7 bellard
        term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
6612 faea38e7 bellard
        return;
6613 faea38e7 bellard
    }
6614 faea38e7 bellard
    term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
6615 faea38e7 bellard
    term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
6616 faea38e7 bellard
    for(i = 0; i < nb_sns; i++) {
6617 faea38e7 bellard
        sn = &sn_tab[i];
6618 faea38e7 bellard
        term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
6619 faea38e7 bellard
    }
6620 faea38e7 bellard
    qemu_free(sn_tab);
6621 8a7ddc38 bellard
}
6622 8a7ddc38 bellard
6623 8a7ddc38 bellard
/***********************************************************/
6624 8a7ddc38 bellard
/* ram save/restore */
6625 8a7ddc38 bellard
6626 8a7ddc38 bellard
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
6627 8a7ddc38 bellard
{
6628 8a7ddc38 bellard
    int v;
6629 8a7ddc38 bellard
6630 8a7ddc38 bellard
    v = qemu_get_byte(f);
6631 8a7ddc38 bellard
    switch(v) {
6632 8a7ddc38 bellard
    case 0:
6633 8a7ddc38 bellard
        if (qemu_get_buffer(f, buf, len) != len)
6634 8a7ddc38 bellard
            return -EIO;
6635 8a7ddc38 bellard
        break;
6636 8a7ddc38 bellard
    case 1:
6637 8a7ddc38 bellard
        v = qemu_get_byte(f);
6638 8a7ddc38 bellard
        memset(buf, v, len);
6639 8a7ddc38 bellard
        break;
6640 8a7ddc38 bellard
    default:
6641 8a7ddc38 bellard
        return -EINVAL;
6642 8a7ddc38 bellard
    }
6643 8a7ddc38 bellard
    return 0;
6644 8a7ddc38 bellard
}
6645 8a7ddc38 bellard
6646 c88676f8 bellard
static int ram_load_v1(QEMUFile *f, void *opaque)
6647 c88676f8 bellard
{
6648 00f82b8a aurel32
    int ret;
6649 00f82b8a aurel32
    ram_addr_t i;
6650 c88676f8 bellard
6651 c88676f8 bellard
    if (qemu_get_be32(f) != phys_ram_size)
6652 c88676f8 bellard
        return -EINVAL;
6653 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
6654 c88676f8 bellard
        ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
6655 c88676f8 bellard
        if (ret)
6656 c88676f8 bellard
            return ret;
6657 c88676f8 bellard
    }
6658 c88676f8 bellard
    return 0;
6659 c88676f8 bellard
}
6660 c88676f8 bellard
6661 c88676f8 bellard
#define BDRV_HASH_BLOCK_SIZE 1024
6662 c88676f8 bellard
#define IOBUF_SIZE 4096
6663 c88676f8 bellard
#define RAM_CBLOCK_MAGIC 0xfabe
6664 c88676f8 bellard
6665 c88676f8 bellard
typedef struct RamCompressState {
6666 c88676f8 bellard
    z_stream zstream;
6667 c88676f8 bellard
    QEMUFile *f;
6668 c88676f8 bellard
    uint8_t buf[IOBUF_SIZE];
6669 c88676f8 bellard
} RamCompressState;
6670 c88676f8 bellard
6671 c88676f8 bellard
static int ram_compress_open(RamCompressState *s, QEMUFile *f)
6672 c88676f8 bellard
{
6673 c88676f8 bellard
    int ret;
6674 c88676f8 bellard
    memset(s, 0, sizeof(*s));
6675 c88676f8 bellard
    s->f = f;
6676 c88676f8 bellard
    ret = deflateInit2(&s->zstream, 1,
6677 5fafdf24 ths
                       Z_DEFLATED, 15,
6678 c88676f8 bellard
                       9, Z_DEFAULT_STRATEGY);
6679 c88676f8 bellard
    if (ret != Z_OK)
6680 c88676f8 bellard
        return -1;
6681 c88676f8 bellard
    s->zstream.avail_out = IOBUF_SIZE;
6682 c88676f8 bellard
    s->zstream.next_out = s->buf;
6683 c88676f8 bellard
    return 0;
6684 c88676f8 bellard
}
6685 c88676f8 bellard
6686 c88676f8 bellard
static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len)
6687 c88676f8 bellard
{
6688 c88676f8 bellard
    qemu_put_be16(s->f, RAM_CBLOCK_MAGIC);
6689 c88676f8 bellard
    qemu_put_be16(s->f, len);
6690 c88676f8 bellard
    qemu_put_buffer(s->f, buf, len);
6691 c88676f8 bellard
}
6692 c88676f8 bellard
6693 c88676f8 bellard
static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len)
6694 c88676f8 bellard
{
6695 c88676f8 bellard
    int ret;
6696 c88676f8 bellard
6697 c88676f8 bellard
    s->zstream.avail_in = len;
6698 c88676f8 bellard
    s->zstream.next_in = (uint8_t *)buf;
6699 c88676f8 bellard
    while (s->zstream.avail_in > 0) {
6700 c88676f8 bellard
        ret = deflate(&s->zstream, Z_NO_FLUSH);
6701 c88676f8 bellard
        if (ret != Z_OK)
6702 c88676f8 bellard
            return -1;
6703 c88676f8 bellard
        if (s->zstream.avail_out == 0) {
6704 c88676f8 bellard
            ram_put_cblock(s, s->buf, IOBUF_SIZE);
6705 c88676f8 bellard
            s->zstream.avail_out = IOBUF_SIZE;
6706 c88676f8 bellard
            s->zstream.next_out = s->buf;
6707 c88676f8 bellard
        }
6708 c88676f8 bellard
    }
6709 c88676f8 bellard
    return 0;
6710 c88676f8 bellard
}
6711 c88676f8 bellard
6712 c88676f8 bellard
static void ram_compress_close(RamCompressState *s)
6713 c88676f8 bellard
{
6714 c88676f8 bellard
    int len, ret;
6715 c88676f8 bellard
6716 c88676f8 bellard
    /* compress last bytes */
6717 c88676f8 bellard
    for(;;) {
6718 c88676f8 bellard
        ret = deflate(&s->zstream, Z_FINISH);
6719 c88676f8 bellard
        if (ret == Z_OK || ret == Z_STREAM_END) {
6720 c88676f8 bellard
            len = IOBUF_SIZE - s->zstream.avail_out;
6721 c88676f8 bellard
            if (len > 0) {
6722 c88676f8 bellard
                ram_put_cblock(s, s->buf, len);
6723 c88676f8 bellard
            }
6724 c88676f8 bellard
            s->zstream.avail_out = IOBUF_SIZE;
6725 c88676f8 bellard
            s->zstream.next_out = s->buf;
6726 c88676f8 bellard
            if (ret == Z_STREAM_END)
6727 c88676f8 bellard
                break;
6728 c88676f8 bellard
        } else {
6729 c88676f8 bellard
            goto fail;
6730 c88676f8 bellard
        }
6731 c88676f8 bellard
    }
6732 c88676f8 bellard
fail:
6733 c88676f8 bellard
    deflateEnd(&s->zstream);
6734 c88676f8 bellard
}
6735 c88676f8 bellard
6736 c88676f8 bellard
typedef struct RamDecompressState {
6737 c88676f8 bellard
    z_stream zstream;
6738 c88676f8 bellard
    QEMUFile *f;
6739 c88676f8 bellard
    uint8_t buf[IOBUF_SIZE];
6740 c88676f8 bellard
} RamDecompressState;
6741 c88676f8 bellard
6742 c88676f8 bellard
static int ram_decompress_open(RamDecompressState *s, QEMUFile *f)
6743 c88676f8 bellard
{
6744 c88676f8 bellard
    int ret;
6745 c88676f8 bellard
    memset(s, 0, sizeof(*s));
6746 c88676f8 bellard
    s->f = f;
6747 c88676f8 bellard
    ret = inflateInit(&s->zstream);
6748 c88676f8 bellard
    if (ret != Z_OK)
6749 c88676f8 bellard
        return -1;
6750 c88676f8 bellard
    return 0;
6751 c88676f8 bellard
}
6752 c88676f8 bellard
6753 c88676f8 bellard
static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len)
6754 c88676f8 bellard
{
6755 c88676f8 bellard
    int ret, clen;
6756 c88676f8 bellard
6757 c88676f8 bellard
    s->zstream.avail_out = len;
6758 c88676f8 bellard
    s->zstream.next_out = buf;
6759 c88676f8 bellard
    while (s->zstream.avail_out > 0) {
6760 c88676f8 bellard
        if (s->zstream.avail_in == 0) {
6761 c88676f8 bellard
            if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
6762 c88676f8 bellard
                return -1;
6763 c88676f8 bellard
            clen = qemu_get_be16(s->f);
6764 c88676f8 bellard
            if (clen > IOBUF_SIZE)
6765 c88676f8 bellard
                return -1;
6766 c88676f8 bellard
            qemu_get_buffer(s->f, s->buf, clen);
6767 c88676f8 bellard
            s->zstream.avail_in = clen;
6768 c88676f8 bellard
            s->zstream.next_in = s->buf;
6769 c88676f8 bellard
        }
6770 c88676f8 bellard
        ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
6771 c88676f8 bellard
        if (ret != Z_OK && ret != Z_STREAM_END) {
6772 c88676f8 bellard
            return -1;
6773 c88676f8 bellard
        }
6774 c88676f8 bellard
    }
6775 c88676f8 bellard
    return 0;
6776 c88676f8 bellard
}
6777 c88676f8 bellard
6778 c88676f8 bellard
static void ram_decompress_close(RamDecompressState *s)
6779 c88676f8 bellard
{
6780 c88676f8 bellard
    inflateEnd(&s->zstream);
6781 c88676f8 bellard
}
6782 c88676f8 bellard
6783 8a7ddc38 bellard
static void ram_save(QEMUFile *f, void *opaque)
6784 8a7ddc38 bellard
{
6785 00f82b8a aurel32
    ram_addr_t i;
6786 c88676f8 bellard
    RamCompressState s1, *s = &s1;
6787 c88676f8 bellard
    uint8_t buf[10];
6788 3b46e624 ths
6789 8a7ddc38 bellard
    qemu_put_be32(f, phys_ram_size);
6790 c88676f8 bellard
    if (ram_compress_open(s, f) < 0)
6791 c88676f8 bellard
        return;
6792 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6793 c88676f8 bellard
#if 0
6794 c88676f8 bellard
        if (tight_savevm_enabled) {
6795 c88676f8 bellard
            int64_t sector_num;
6796 c88676f8 bellard
            int j;
6797 c88676f8 bellard

6798 c88676f8 bellard
            /* find if the memory block is available on a virtual
6799 c88676f8 bellard
               block device */
6800 c88676f8 bellard
            sector_num = -1;
6801 e4bcb14c ths
            for(j = 0; j < nb_drives; j++) {
6802 e4bcb14c ths
                sector_num = bdrv_hash_find(drives_table[j].bdrv,
6803 e4bcb14c ths
                                            phys_ram_base + i,
6804 e4bcb14c ths
                                            BDRV_HASH_BLOCK_SIZE);
6805 e4bcb14c ths
                if (sector_num >= 0)
6806 e4bcb14c ths
                    break;
6807 c88676f8 bellard
            }
6808 e4bcb14c ths
            if (j == nb_drives)
6809 c88676f8 bellard
                goto normal_compress;
6810 c88676f8 bellard
            buf[0] = 1;
6811 c88676f8 bellard
            buf[1] = j;
6812 c88676f8 bellard
            cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
6813 c88676f8 bellard
            ram_compress_buf(s, buf, 10);
6814 5fafdf24 ths
        } else
6815 c88676f8 bellard
#endif
6816 c88676f8 bellard
        {
6817 c88676f8 bellard
            //        normal_compress:
6818 c88676f8 bellard
            buf[0] = 0;
6819 c88676f8 bellard
            ram_compress_buf(s, buf, 1);
6820 c88676f8 bellard
            ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE);
6821 c88676f8 bellard
        }
6822 8a7ddc38 bellard
    }
6823 c88676f8 bellard
    ram_compress_close(s);
6824 8a7ddc38 bellard
}
6825 8a7ddc38 bellard
6826 8a7ddc38 bellard
static int ram_load(QEMUFile *f, void *opaque, int version_id)
6827 8a7ddc38 bellard
{
6828 c88676f8 bellard
    RamDecompressState s1, *s = &s1;
6829 c88676f8 bellard
    uint8_t buf[10];
6830 00f82b8a aurel32
    ram_addr_t i;
6831 8a7ddc38 bellard
6832 c88676f8 bellard
    if (version_id == 1)
6833 c88676f8 bellard
        return ram_load_v1(f, opaque);
6834 c88676f8 bellard
    if (version_id != 2)
6835 8a7ddc38 bellard
        return -EINVAL;
6836 8a7ddc38 bellard
    if (qemu_get_be32(f) != phys_ram_size)
6837 8a7ddc38 bellard
        return -EINVAL;
6838 c88676f8 bellard
    if (ram_decompress_open(s, f) < 0)
6839 c88676f8 bellard
        return -EINVAL;
6840 c88676f8 bellard
    for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
6841 c88676f8 bellard
        if (ram_decompress_buf(s, buf, 1) < 0) {
6842 c88676f8 bellard
            fprintf(stderr, "Error while reading ram block header\n");
6843 c88676f8 bellard
            goto error;
6844 c88676f8 bellard
        }
6845 c88676f8 bellard
        if (buf[0] == 0) {
6846 c88676f8 bellard
            if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) {
6847 00f82b8a aurel32
                fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
6848 c88676f8 bellard
                goto error;
6849 c88676f8 bellard
            }
6850 5fafdf24 ths
        } else
6851 c88676f8 bellard
#if 0
6852 c88676f8 bellard
        if (buf[0] == 1) {
6853 c88676f8 bellard
            int bs_index;
6854 c88676f8 bellard
            int64_t sector_num;
6855 c88676f8 bellard

6856 c88676f8 bellard
            ram_decompress_buf(s, buf + 1, 9);
6857 c88676f8 bellard
            bs_index = buf[1];
6858 c88676f8 bellard
            sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
6859 e4bcb14c ths
            if (bs_index >= nb_drives) {
6860 c88676f8 bellard
                fprintf(stderr, "Invalid block device index %d\n", bs_index);
6861 c88676f8 bellard
                goto error;
6862 c88676f8 bellard
            }
6863 e4bcb14c ths
            if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
6864 e4bcb14c ths
                          phys_ram_base + i,
6865 c88676f8 bellard
                          BDRV_HASH_BLOCK_SIZE / 512) < 0) {
6866 5fafdf24 ths
                fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
6867 c88676f8 bellard
                        bs_index, sector_num);
6868 c88676f8 bellard
                goto error;
6869 c88676f8 bellard
            }
6870 5fafdf24 ths
        } else
6871 c88676f8 bellard
#endif
6872 c88676f8 bellard
        {
6873 c88676f8 bellard
        error:
6874 c88676f8 bellard
            printf("Error block header\n");
6875 c88676f8 bellard
            return -EINVAL;
6876 c88676f8 bellard
        }
6877 8a7ddc38 bellard
    }
6878 c88676f8 bellard
    ram_decompress_close(s);
6879 8a7ddc38 bellard
    return 0;
6880 8a7ddc38 bellard
}
6881 8a7ddc38 bellard
6882 8a7ddc38 bellard
/***********************************************************/
6883 83f64091 bellard
/* bottom halves (can be seen as timers which expire ASAP) */
6884 83f64091 bellard
6885 83f64091 bellard
struct QEMUBH {
6886 83f64091 bellard
    QEMUBHFunc *cb;
6887 83f64091 bellard
    void *opaque;
6888 83f64091 bellard
    int scheduled;
6889 83f64091 bellard
    QEMUBH *next;
6890 83f64091 bellard
};
6891 83f64091 bellard
6892 83f64091 bellard
static QEMUBH *first_bh = NULL;
6893 83f64091 bellard
6894 83f64091 bellard
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
6895 83f64091 bellard
{
6896 83f64091 bellard
    QEMUBH *bh;
6897 83f64091 bellard
    bh = qemu_mallocz(sizeof(QEMUBH));
6898 83f64091 bellard
    if (!bh)
6899 83f64091 bellard
        return NULL;
6900 83f64091 bellard
    bh->cb = cb;
6901 83f64091 bellard
    bh->opaque = opaque;
6902 83f64091 bellard
    return bh;
6903 83f64091 bellard
}
6904 83f64091 bellard
6905 6eb5733a bellard
int qemu_bh_poll(void)
6906 83f64091 bellard
{
6907 83f64091 bellard
    QEMUBH *bh, **pbh;
6908 6eb5733a bellard
    int ret;
6909 83f64091 bellard
6910 6eb5733a bellard
    ret = 0;
6911 83f64091 bellard
    for(;;) {
6912 83f64091 bellard
        pbh = &first_bh;
6913 83f64091 bellard
        bh = *pbh;
6914 83f64091 bellard
        if (!bh)
6915 83f64091 bellard
            break;
6916 6eb5733a bellard
        ret = 1;
6917 83f64091 bellard
        *pbh = bh->next;
6918 83f64091 bellard
        bh->scheduled = 0;
6919 83f64091 bellard
        bh->cb(bh->opaque);
6920 83f64091 bellard
    }
6921 6eb5733a bellard
    return ret;
6922 83f64091 bellard
}
6923 83f64091 bellard
6924 83f64091 bellard
void qemu_bh_schedule(QEMUBH *bh)
6925 83f64091 bellard
{
6926 83f64091 bellard
    CPUState *env = cpu_single_env;
6927 83f64091 bellard
    if (bh->scheduled)
6928 83f64091 bellard
        return;
6929 83f64091 bellard
    bh->scheduled = 1;
6930 83f64091 bellard
    bh->next = first_bh;
6931 83f64091 bellard
    first_bh = bh;
6932 83f64091 bellard
6933 83f64091 bellard
    /* stop the currently executing CPU to execute the BH ASAP */
6934 83f64091 bellard
    if (env) {
6935 83f64091 bellard
        cpu_interrupt(env, CPU_INTERRUPT_EXIT);
6936 83f64091 bellard
    }
6937 83f64091 bellard
}
6938 83f64091 bellard
6939 83f64091 bellard
void qemu_bh_cancel(QEMUBH *bh)
6940 83f64091 bellard
{
6941 83f64091 bellard
    QEMUBH **pbh;
6942 83f64091 bellard
    if (bh->scheduled) {
6943 83f64091 bellard
        pbh = &first_bh;
6944 83f64091 bellard
        while (*pbh != bh)
6945 83f64091 bellard
            pbh = &(*pbh)->next;
6946 83f64091 bellard
        *pbh = bh->next;
6947 83f64091 bellard
        bh->scheduled = 0;
6948 83f64091 bellard
    }
6949 83f64091 bellard
}
6950 83f64091 bellard
6951 83f64091 bellard
void qemu_bh_delete(QEMUBH *bh)
6952 83f64091 bellard
{
6953 83f64091 bellard
    qemu_bh_cancel(bh);
6954 83f64091 bellard
    qemu_free(bh);
6955 83f64091 bellard
}
6956 83f64091 bellard
6957 83f64091 bellard
/***********************************************************/
6958 cc1daa40 bellard
/* machine registration */
6959 cc1daa40 bellard
6960 cc1daa40 bellard
QEMUMachine *first_machine = NULL;
6961 cc1daa40 bellard
6962 cc1daa40 bellard
int qemu_register_machine(QEMUMachine *m)
6963 cc1daa40 bellard
{
6964 cc1daa40 bellard
    QEMUMachine **pm;
6965 cc1daa40 bellard
    pm = &first_machine;
6966 cc1daa40 bellard
    while (*pm != NULL)
6967 cc1daa40 bellard
        pm = &(*pm)->next;
6968 cc1daa40 bellard
    m->next = NULL;
6969 cc1daa40 bellard
    *pm = m;
6970 cc1daa40 bellard
    return 0;
6971 cc1daa40 bellard
}
6972 cc1daa40 bellard
6973 9596ebb7 pbrook
static QEMUMachine *find_machine(const char *name)
6974 cc1daa40 bellard
{
6975 cc1daa40 bellard
    QEMUMachine *m;
6976 cc1daa40 bellard
6977 cc1daa40 bellard
    for(m = first_machine; m != NULL; m = m->next) {
6978 cc1daa40 bellard
        if (!strcmp(m->name, name))
6979 cc1daa40 bellard
            return m;
6980 cc1daa40 bellard
    }
6981 cc1daa40 bellard
    return NULL;
6982 cc1daa40 bellard
}
6983 cc1daa40 bellard
6984 cc1daa40 bellard
/***********************************************************/
6985 8a7ddc38 bellard
/* main execution loop */
6986 8a7ddc38 bellard
6987 9596ebb7 pbrook
static void gui_update(void *opaque)
6988 8a7ddc38 bellard
{
6989 740733bb ths
    DisplayState *ds = opaque;
6990 740733bb ths
    ds->dpy_refresh(ds);
6991 f442e08b aurel32
    qemu_mod_timer(ds->gui_timer,
6992 f442e08b aurel32
        (ds->gui_timer_interval ?
6993 f442e08b aurel32
            ds->gui_timer_interval :
6994 f442e08b aurel32
            GUI_REFRESH_INTERVAL)
6995 f442e08b aurel32
        + qemu_get_clock(rt_clock));
6996 8a7ddc38 bellard
}
6997 8a7ddc38 bellard
6998 0bd48850 bellard
struct vm_change_state_entry {
6999 0bd48850 bellard
    VMChangeStateHandler *cb;
7000 0bd48850 bellard
    void *opaque;
7001 0bd48850 bellard
    LIST_ENTRY (vm_change_state_entry) entries;
7002 0bd48850 bellard
};
7003 0bd48850 bellard
7004 0bd48850 bellard
static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
7005 0bd48850 bellard
7006 0bd48850 bellard
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb,
7007 0bd48850 bellard
                                                     void *opaque)
7008 0bd48850 bellard
{
7009 0bd48850 bellard
    VMChangeStateEntry *e;
7010 0bd48850 bellard
7011 0bd48850 bellard
    e = qemu_mallocz(sizeof (*e));
7012 0bd48850 bellard
    if (!e)
7013 0bd48850 bellard
        return NULL;
7014 0bd48850 bellard
7015 0bd48850 bellard
    e->cb = cb;
7016 0bd48850 bellard
    e->opaque = opaque;
7017 0bd48850 bellard
    LIST_INSERT_HEAD(&vm_change_state_head, e, entries);
7018 0bd48850 bellard
    return e;
7019 0bd48850 bellard
}
7020 0bd48850 bellard
7021 0bd48850 bellard
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
7022 0bd48850 bellard
{
7023 0bd48850 bellard
    LIST_REMOVE (e, entries);
7024 0bd48850 bellard
    qemu_free (e);
7025 0bd48850 bellard
}
7026 0bd48850 bellard
7027 0bd48850 bellard
static void vm_state_notify(int running)
7028 0bd48850 bellard
{
7029 0bd48850 bellard
    VMChangeStateEntry *e;
7030 0bd48850 bellard
7031 0bd48850 bellard
    for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
7032 0bd48850 bellard
        e->cb(e->opaque, running);
7033 0bd48850 bellard
    }
7034 0bd48850 bellard
}
7035 0bd48850 bellard
7036 8a7ddc38 bellard
/* XXX: support several handlers */
7037 0bd48850 bellard
static VMStopHandler *vm_stop_cb;
7038 0bd48850 bellard
static void *vm_stop_opaque;
7039 8a7ddc38 bellard
7040 8a7ddc38 bellard
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
7041 8a7ddc38 bellard
{
7042 8a7ddc38 bellard
    vm_stop_cb = cb;
7043 8a7ddc38 bellard
    vm_stop_opaque = opaque;
7044 8a7ddc38 bellard
    return 0;
7045 8a7ddc38 bellard
}
7046 8a7ddc38 bellard
7047 8a7ddc38 bellard
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
7048 8a7ddc38 bellard
{
7049 8a7ddc38 bellard
    vm_stop_cb = NULL;
7050 8a7ddc38 bellard
}
7051 8a7ddc38 bellard
7052 8a7ddc38 bellard
void vm_start(void)
7053 8a7ddc38 bellard
{
7054 8a7ddc38 bellard
    if (!vm_running) {
7055 8a7ddc38 bellard
        cpu_enable_ticks();
7056 8a7ddc38 bellard
        vm_running = 1;
7057 0bd48850 bellard
        vm_state_notify(1);
7058 efe75411 ths
        qemu_rearm_alarm_timer(alarm_timer);
7059 8a7ddc38 bellard
    }
7060 8a7ddc38 bellard
}
7061 8a7ddc38 bellard
7062 5fafdf24 ths
void vm_stop(int reason)
7063 8a7ddc38 bellard
{
7064 8a7ddc38 bellard
    if (vm_running) {
7065 8a7ddc38 bellard
        cpu_disable_ticks();
7066 8a7ddc38 bellard
        vm_running = 0;
7067 8a7ddc38 bellard
        if (reason != 0) {
7068 8a7ddc38 bellard
            if (vm_stop_cb) {
7069 8a7ddc38 bellard
                vm_stop_cb(vm_stop_opaque, reason);
7070 8a7ddc38 bellard
            }
7071 34865134 bellard
        }
7072 0bd48850 bellard
        vm_state_notify(0);
7073 8a7ddc38 bellard
    }
7074 8a7ddc38 bellard
}
7075 8a7ddc38 bellard
7076 bb0c6722 bellard
/* reset/shutdown handler */
7077 bb0c6722 bellard
7078 bb0c6722 bellard
typedef struct QEMUResetEntry {
7079 bb0c6722 bellard
    QEMUResetHandler *func;
7080 bb0c6722 bellard
    void *opaque;
7081 bb0c6722 bellard
    struct QEMUResetEntry *next;
7082 bb0c6722 bellard
} QEMUResetEntry;
7083 bb0c6722 bellard
7084 bb0c6722 bellard
static QEMUResetEntry *first_reset_entry;
7085 bb0c6722 bellard
static int reset_requested;
7086 bb0c6722 bellard
static int shutdown_requested;
7087 3475187d bellard
static int powerdown_requested;
7088 bb0c6722 bellard
7089 cf7a2fe2 aurel32
int qemu_shutdown_requested(void)
7090 cf7a2fe2 aurel32
{
7091 cf7a2fe2 aurel32
    int r = shutdown_requested;
7092 cf7a2fe2 aurel32
    shutdown_requested = 0;
7093 cf7a2fe2 aurel32
    return r;
7094 cf7a2fe2 aurel32
}
7095 cf7a2fe2 aurel32
7096 cf7a2fe2 aurel32
int qemu_reset_requested(void)
7097 cf7a2fe2 aurel32
{
7098 cf7a2fe2 aurel32
    int r = reset_requested;
7099 cf7a2fe2 aurel32
    reset_requested = 0;
7100 cf7a2fe2 aurel32
    return r;
7101 cf7a2fe2 aurel32
}
7102 cf7a2fe2 aurel32
7103 cf7a2fe2 aurel32
int qemu_powerdown_requested(void)
7104 cf7a2fe2 aurel32
{
7105 cf7a2fe2 aurel32
    int r = powerdown_requested;
7106 cf7a2fe2 aurel32
    powerdown_requested = 0;
7107 cf7a2fe2 aurel32
    return r;
7108 cf7a2fe2 aurel32
}
7109 cf7a2fe2 aurel32
7110 bb0c6722 bellard
void qemu_register_reset(QEMUResetHandler *func, void *opaque)
7111 bb0c6722 bellard
{
7112 bb0c6722 bellard
    QEMUResetEntry **pre, *re;
7113 bb0c6722 bellard
7114 bb0c6722 bellard
    pre = &first_reset_entry;
7115 bb0c6722 bellard
    while (*pre != NULL)
7116 bb0c6722 bellard
        pre = &(*pre)->next;
7117 bb0c6722 bellard
    re = qemu_mallocz(sizeof(QEMUResetEntry));
7118 bb0c6722 bellard
    re->func = func;
7119 bb0c6722 bellard
    re->opaque = opaque;
7120 bb0c6722 bellard
    re->next = NULL;
7121 bb0c6722 bellard
    *pre = re;
7122 bb0c6722 bellard
}
7123 bb0c6722 bellard
7124 cf7a2fe2 aurel32
void qemu_system_reset(void)
7125 bb0c6722 bellard
{
7126 bb0c6722 bellard
    QEMUResetEntry *re;
7127 bb0c6722 bellard
7128 bb0c6722 bellard
    /* reset all devices */
7129 bb0c6722 bellard
    for(re = first_reset_entry; re != NULL; re = re->next) {
7130 bb0c6722 bellard
        re->func(re->opaque);
7131 bb0c6722 bellard
    }
7132 bb0c6722 bellard
}
7133 bb0c6722 bellard
7134 bb0c6722 bellard
void qemu_system_reset_request(void)
7135 bb0c6722 bellard
{
7136 d1beab82 bellard
    if (no_reboot) {
7137 d1beab82 bellard
        shutdown_requested = 1;
7138 d1beab82 bellard
    } else {
7139 d1beab82 bellard
        reset_requested = 1;
7140 d1beab82 bellard
    }
7141 6a00d601 bellard
    if (cpu_single_env)
7142 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
7143 bb0c6722 bellard
}
7144 bb0c6722 bellard
7145 bb0c6722 bellard
void qemu_system_shutdown_request(void)
7146 bb0c6722 bellard
{
7147 bb0c6722 bellard
    shutdown_requested = 1;
7148 6a00d601 bellard
    if (cpu_single_env)
7149 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
7150 bb0c6722 bellard
}
7151 bb0c6722 bellard
7152 3475187d bellard
void qemu_system_powerdown_request(void)
7153 3475187d bellard
{
7154 3475187d bellard
    powerdown_requested = 1;
7155 6a00d601 bellard
    if (cpu_single_env)
7156 6a00d601 bellard
        cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
7157 bb0c6722 bellard
}
7158 bb0c6722 bellard
7159 5905b2e5 bellard
void main_loop_wait(int timeout)
7160 8a7ddc38 bellard
{
7161 cafffd40 ths
    IOHandlerRecord *ioh;
7162 e035649e bellard
    fd_set rfds, wfds, xfds;
7163 877cf882 ths
    int ret, nfds;
7164 877cf882 ths
#ifdef _WIN32
7165 877cf882 ths
    int ret2, i;
7166 877cf882 ths
#endif
7167 fd1dff4b bellard
    struct timeval tv;
7168 f331110f bellard
    PollingEntry *pe;
7169 f331110f bellard
7170 c4b1fcc0 bellard
7171 f331110f bellard
    /* XXX: need to suppress polling by better using win32 events */
7172 f331110f bellard
    ret = 0;
7173 f331110f bellard
    for(pe = first_polling_entry; pe != NULL; pe = pe->next) {
7174 f331110f bellard
        ret |= pe->func(pe->opaque);
7175 f331110f bellard
    }
7176 38e205a2 bellard
#ifdef _WIN32
7177 e6b1e558 ths
    if (ret == 0) {
7178 a18e524a bellard
        int err;
7179 a18e524a bellard
        WaitObjects *w = &wait_objects;
7180 3b46e624 ths
7181 a18e524a bellard
        ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout);
7182 a18e524a bellard
        if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
7183 a18e524a bellard
            if (w->func[ret - WAIT_OBJECT_0])
7184 a18e524a bellard
                w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
7185 3b46e624 ths
7186 5fafdf24 ths
            /* Check for additional signaled events */
7187 e6b1e558 ths
            for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
7188 3b46e624 ths
7189 e6b1e558 ths
                /* Check if event is signaled */
7190 e6b1e558 ths
                ret2 = WaitForSingleObject(w->events[i], 0);
7191 e6b1e558 ths
                if(ret2 == WAIT_OBJECT_0) {
7192 e6b1e558 ths
                    if (w->func[i])
7193 e6b1e558 ths
                        w->func[i](w->opaque[i]);
7194 e6b1e558 ths
                } else if (ret2 == WAIT_TIMEOUT) {
7195 e6b1e558 ths
                } else {
7196 e6b1e558 ths
                    err = GetLastError();
7197 e6b1e558 ths
                    fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
7198 3b46e624 ths
                }
7199 3b46e624 ths
            }
7200 a18e524a bellard
        } else if (ret == WAIT_TIMEOUT) {
7201 a18e524a bellard
        } else {
7202 a18e524a bellard
            err = GetLastError();
7203 e6b1e558 ths
            fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
7204 a18e524a bellard
        }
7205 f331110f bellard
    }
7206 fd1dff4b bellard
#endif
7207 fd1dff4b bellard
    /* poll any events */
7208 fd1dff4b bellard
    /* XXX: separate device handlers from system ones */
7209 fd1dff4b bellard
    nfds = -1;
7210 fd1dff4b bellard
    FD_ZERO(&rfds);
7211 fd1dff4b bellard
    FD_ZERO(&wfds);
7212 e035649e bellard
    FD_ZERO(&xfds);
7213 fd1dff4b bellard
    for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
7214 cafffd40 ths
        if (ioh->deleted)
7215 cafffd40 ths
            continue;
7216 fd1dff4b bellard
        if (ioh->fd_read &&
7217 fd1dff4b bellard
            (!ioh->fd_read_poll ||
7218 fd1dff4b bellard
             ioh->fd_read_poll(ioh->opaque) != 0)) {
7219 fd1dff4b bellard
            FD_SET(ioh->fd, &rfds);
7220 fd1dff4b bellard
            if (ioh->fd > nfds)
7221 fd1dff4b bellard
                nfds = ioh->fd;
7222 fd1dff4b bellard
        }
7223 fd1dff4b bellard
        if (ioh->fd_write) {
7224 fd1dff4b bellard
            FD_SET(ioh->fd, &wfds);
7225 fd1dff4b bellard
            if (ioh->fd > nfds)
7226 fd1dff4b bellard
                nfds = ioh->fd;
7227 fd1dff4b bellard
        }
7228 fd1dff4b bellard
    }
7229 3b46e624 ths
7230 fd1dff4b bellard
    tv.tv_sec = 0;
7231 fd1dff4b bellard
#ifdef _WIN32
7232 fd1dff4b bellard
    tv.tv_usec = 0;
7233 38e205a2 bellard
#else
7234 fd1dff4b bellard
    tv.tv_usec = timeout * 1000;
7235 fd1dff4b bellard
#endif
7236 e035649e bellard
#if defined(CONFIG_SLIRP)
7237 e035649e bellard
    if (slirp_inited) {
7238 e035649e bellard
        slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
7239 e035649e bellard
    }
7240 e035649e bellard
#endif
7241 e035649e bellard
    ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
7242 fd1dff4b bellard
    if (ret > 0) {
7243 cafffd40 ths
        IOHandlerRecord **pioh;
7244 cafffd40 ths
7245 cafffd40 ths
        for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
7246 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
7247 fd1dff4b bellard
                ioh->fd_read(ioh->opaque);
7248 7c9d8e07 bellard
            }
7249 6ab43fdc ths
            if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
7250 fd1dff4b bellard
                ioh->fd_write(ioh->opaque);
7251 c4b1fcc0 bellard
            }
7252 b4608c04 bellard
        }
7253 cafffd40 ths
7254 cafffd40 ths
        /* remove deleted IO handlers */
7255 cafffd40 ths
        pioh = &first_io_handler;
7256 cafffd40 ths
        while (*pioh) {
7257 cafffd40 ths
            ioh = *pioh;
7258 cafffd40 ths
            if (ioh->deleted) {
7259 cafffd40 ths
                *pioh = ioh->next;
7260 cafffd40 ths
                qemu_free(ioh);
7261 5fafdf24 ths
            } else
7262 cafffd40 ths
                pioh = &ioh->next;
7263 cafffd40 ths
        }
7264 fd1dff4b bellard
    }
7265 c20709aa bellard
#if defined(CONFIG_SLIRP)
7266 fd1dff4b bellard
    if (slirp_inited) {
7267 e035649e bellard
        if (ret < 0) {
7268 e035649e bellard
            FD_ZERO(&rfds);
7269 e035649e bellard
            FD_ZERO(&wfds);
7270 e035649e bellard
            FD_ZERO(&xfds);
7271 c20709aa bellard
        }
7272 e035649e bellard
        slirp_select_poll(&rfds, &wfds, &xfds);
7273 fd1dff4b bellard
    }
7274 c20709aa bellard
#endif
7275 83f64091 bellard
    qemu_aio_poll();
7276 c20709aa bellard
7277 fd1dff4b bellard
    if (vm_running) {
7278 21b20814 edgar_igl
        if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
7279 5fafdf24 ths
        qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
7280 fd1dff4b bellard
                        qemu_get_clock(vm_clock));
7281 fd1dff4b bellard
        /* run dma transfers, if any */
7282 fd1dff4b bellard
        DMA_run();
7283 fd1dff4b bellard
    }
7284 423f0742 pbrook
7285 fd1dff4b bellard
    /* real time timers */
7286 5fafdf24 ths
    qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
7287 fd1dff4b bellard
                    qemu_get_clock(rt_clock));
7288 423f0742 pbrook
7289 d5d08334 balrog
    if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
7290 d5d08334 balrog
        alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED);
7291 d5d08334 balrog
        qemu_rearm_alarm_timer(alarm_timer);
7292 d5d08334 balrog
    }
7293 b99dc0d1 balrog
7294 423f0742 pbrook
    /* Check bottom-halves last in case any of the earlier events triggered
7295 423f0742 pbrook
       them.  */
7296 423f0742 pbrook
    qemu_bh_poll();
7297 3b46e624 ths
7298 5905b2e5 bellard
}
7299 5905b2e5 bellard
7300 9596ebb7 pbrook
static int main_loop(void)
7301 5905b2e5 bellard
{
7302 5905b2e5 bellard
    int ret, timeout;
7303 89bfc105 bellard
#ifdef CONFIG_PROFILER
7304 89bfc105 bellard
    int64_t ti;
7305 89bfc105 bellard
#endif
7306 6a00d601 bellard
    CPUState *env;
7307 5905b2e5 bellard
7308 6a00d601 bellard
    cur_cpu = first_cpu;
7309 ee5605e5 balrog
    next_cpu = cur_cpu->next_cpu ?: first_cpu;
7310 5905b2e5 bellard
    for(;;) {
7311 5905b2e5 bellard
        if (vm_running) {
7312 15a76449 bellard
7313 15a76449 bellard
            for(;;) {
7314 15a76449 bellard
                /* get next cpu */
7315 ee5605e5 balrog
                env = next_cpu;
7316 89bfc105 bellard
#ifdef CONFIG_PROFILER
7317 89bfc105 bellard
                ti = profile_getclock();
7318 89bfc105 bellard
#endif
7319 2e70f6ef pbrook
                if (use_icount) {
7320 2e70f6ef pbrook
                    int64_t count;
7321 2e70f6ef pbrook
                    int decr;
7322 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low + env->icount_extra);
7323 2e70f6ef pbrook
                    env->icount_decr.u16.low = 0;
7324 2e70f6ef pbrook
                    env->icount_extra = 0;
7325 2e70f6ef pbrook
                    count = qemu_next_deadline();
7326 2e70f6ef pbrook
                    count = (count + (1 << icount_time_shift) - 1)
7327 2e70f6ef pbrook
                            >> icount_time_shift;
7328 2e70f6ef pbrook
                    qemu_icount += count;
7329 2e70f6ef pbrook
                    decr = (count > 0xffff) ? 0xffff : count;
7330 2e70f6ef pbrook
                    count -= decr;
7331 2e70f6ef pbrook
                    env->icount_decr.u16.low = decr;
7332 2e70f6ef pbrook
                    env->icount_extra = count;
7333 2e70f6ef pbrook
                }
7334 6a00d601 bellard
                ret = cpu_exec(env);
7335 89bfc105 bellard
#ifdef CONFIG_PROFILER
7336 89bfc105 bellard
                qemu_time += profile_getclock() - ti;
7337 89bfc105 bellard
#endif
7338 2e70f6ef pbrook
                if (use_icount) {
7339 2e70f6ef pbrook
                    /* Fold pending instructions back into the
7340 2e70f6ef pbrook
                       instruction counter, and clear the interrupt flag.  */
7341 2e70f6ef pbrook
                    qemu_icount -= (env->icount_decr.u16.low
7342 2e70f6ef pbrook
                                    + env->icount_extra);
7343 2e70f6ef pbrook
                    env->icount_decr.u32 = 0;
7344 2e70f6ef pbrook
                    env->icount_extra = 0;
7345 2e70f6ef pbrook
                }
7346 ee5605e5 balrog
                next_cpu = env->next_cpu ?: first_cpu;
7347 95b01009 aurel32
                if (event_pending && likely(ret != EXCP_DEBUG)) {
7348 ee5605e5 balrog
                    ret = EXCP_INTERRUPT;
7349 ee5605e5 balrog
                    event_pending = 0;
7350 ee5605e5 balrog
                    break;
7351 ee5605e5 balrog
                }
7352 bd967e05 pbrook
                if (ret == EXCP_HLT) {
7353 bd967e05 pbrook
                    /* Give the next CPU a chance to run.  */
7354 bd967e05 pbrook
                    cur_cpu = env;
7355 bd967e05 pbrook
                    continue;
7356 bd967e05 pbrook
                }
7357 15a76449 bellard
                if (ret != EXCP_HALTED)
7358 15a76449 bellard
                    break;
7359 15a76449 bellard
                /* all CPUs are halted ? */
7360 bd967e05 pbrook
                if (env == cur_cpu)
7361 15a76449 bellard
                    break;
7362 15a76449 bellard
            }
7363 15a76449 bellard
            cur_cpu = env;
7364 15a76449 bellard
7365 5905b2e5 bellard
            if (shutdown_requested) {
7366 3475187d bellard
                ret = EXCP_INTERRUPT;
7367 b2f76161 aurel32
                if (no_shutdown) {
7368 b2f76161 aurel32
                    vm_stop(0);
7369 b2f76161 aurel32
                    no_shutdown = 0;
7370 b2f76161 aurel32
                }
7371 b2f76161 aurel32
                else
7372 b2f76161 aurel32
                    break;
7373 5905b2e5 bellard
            }
7374 5905b2e5 bellard
            if (reset_requested) {
7375 5905b2e5 bellard
                reset_requested = 0;
7376 5905b2e5 bellard
                qemu_system_reset();
7377 3475187d bellard
                ret = EXCP_INTERRUPT;
7378 3475187d bellard
            }
7379 3475187d bellard
            if (powerdown_requested) {
7380 3475187d bellard
                powerdown_requested = 0;
7381 3475187d bellard
                qemu_system_powerdown();
7382 3475187d bellard
                ret = EXCP_INTERRUPT;
7383 5905b2e5 bellard
            }
7384 95b01009 aurel32
            if (unlikely(ret == EXCP_DEBUG)) {
7385 5905b2e5 bellard
                vm_stop(EXCP_DEBUG);
7386 5905b2e5 bellard
            }
7387 bd967e05 pbrook
            /* If all cpus are halted then wait until the next IRQ */
7388 5905b2e5 bellard
            /* XXX: use timeout computed from timers */
7389 2e70f6ef pbrook
            if (ret == EXCP_HALTED) {
7390 2e70f6ef pbrook
                if (use_icount) {
7391 2e70f6ef pbrook
                    int64_t add;
7392 2e70f6ef pbrook
                    int64_t delta;
7393 2e70f6ef pbrook
                    /* Advance virtual time to the next event.  */
7394 2e70f6ef pbrook
                    if (use_icount == 1) {
7395 2e70f6ef pbrook
                        /* When not using an adaptive execution frequency
7396 2e70f6ef pbrook
                           we tend to get badly out of sync with real time,
7397 bf20dc07 ths
                           so just delay for a reasonable amount of time.  */
7398 2e70f6ef pbrook
                        delta = 0;
7399 2e70f6ef pbrook
                    } else {
7400 2e70f6ef pbrook
                        delta = cpu_get_icount() - cpu_get_clock();
7401 2e70f6ef pbrook
                    }
7402 2e70f6ef pbrook
                    if (delta > 0) {
7403 2e70f6ef pbrook
                        /* If virtual time is ahead of real time then just
7404 2e70f6ef pbrook
                           wait for IO.  */
7405 2e70f6ef pbrook
                        timeout = (delta / 1000000) + 1;
7406 2e70f6ef pbrook
                    } else {
7407 2e70f6ef pbrook
                        /* Wait for either IO to occur or the next
7408 2e70f6ef pbrook
                           timer event.  */
7409 2e70f6ef pbrook
                        add = qemu_next_deadline();
7410 2e70f6ef pbrook
                        /* We advance the timer before checking for IO.
7411 2e70f6ef pbrook
                           Limit the amount we advance so that early IO
7412 2e70f6ef pbrook
                           activity won't get the guest too far ahead.  */
7413 2e70f6ef pbrook
                        if (add > 10000000)
7414 2e70f6ef pbrook
                            add = 10000000;
7415 2e70f6ef pbrook
                        delta += add;
7416 2e70f6ef pbrook
                        add = (add + (1 << icount_time_shift) - 1)
7417 2e70f6ef pbrook
                              >> icount_time_shift;
7418 2e70f6ef pbrook
                        qemu_icount += add;
7419 2e70f6ef pbrook
                        timeout = delta / 1000000;
7420 2e70f6ef pbrook
                        if (timeout < 0)
7421 2e70f6ef pbrook
                            timeout = 0;
7422 2e70f6ef pbrook
                    }
7423 2e70f6ef pbrook
                } else {
7424 2e70f6ef pbrook
                    timeout = 10;
7425 2e70f6ef pbrook
                }
7426 2e70f6ef pbrook
            } else {
7427 5905b2e5 bellard
                timeout = 0;
7428 2e70f6ef pbrook
            }
7429 5905b2e5 bellard
        } else {
7430 5905b2e5 bellard
            timeout = 10;
7431 5905b2e5 bellard
        }
7432 89bfc105 bellard
#ifdef CONFIG_PROFILER
7433 89bfc105 bellard
        ti = profile_getclock();
7434 89bfc105 bellard
#endif
7435 5905b2e5 bellard
        main_loop_wait(timeout);
7436 89bfc105 bellard
#ifdef CONFIG_PROFILER
7437 89bfc105 bellard
        dev_time += profile_getclock() - ti;
7438 89bfc105 bellard
#endif
7439 b4608c04 bellard
    }
7440 34865134 bellard
    cpu_disable_ticks();
7441 34865134 bellard
    return ret;
7442 b4608c04 bellard
}
7443 b4608c04 bellard
7444 15f82208 ths
static void help(int exitcode)
7445 0824d6fc bellard
{
7446 68d0f70e bellard
    printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
7447 0db63474 bellard
           "usage: %s [options] [disk_image]\n"
7448 0824d6fc bellard
           "\n"
7449 a20dd508 bellard
           "'disk_image' is a raw hard image image for IDE hard disk 0\n"
7450 fc01f7e7 bellard
           "\n"
7451 a20dd508 bellard
           "Standard options:\n"
7452 cc1daa40 bellard
           "-M machine      select emulated machine (-M ? for list)\n"
7453 5adb4839 pbrook
           "-cpu cpu        select CPU (-cpu ? for list)\n"
7454 c45886db bellard
           "-fda/-fdb file  use 'file' as floppy disk 0/1 image\n"
7455 36b486bb bellard
           "-hda/-hdb file  use 'file' as IDE hard disk 0/1 image\n"
7456 36b486bb bellard
           "-hdc/-hdd file  use 'file' as IDE hard disk 2/3 image\n"
7457 c4b1fcc0 bellard
           "-cdrom file     use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
7458 a1620fac aurel32
           "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
7459 a1620fac aurel32
           "       [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
7460 a1620fac aurel32
           "       [,cache=on|off][,format=f]\n"
7461 e4bcb14c ths
           "                use 'file' as a drive image\n"
7462 3e3d5815 balrog
           "-mtdblock file  use 'file' as on-board Flash memory image\n"
7463 a1bb27b1 pbrook
           "-sd file        use 'file' as SecureDigital card image\n"
7464 86f55663 j_mayer
           "-pflash file    use 'file' as a parallel flash image\n"
7465 eec85c2a ths
           "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
7466 667accab ths
           "-snapshot       write to temporary files instead of disk image files\n"
7467 667accab ths
#ifdef CONFIG_SDL
7468 43523e93 ths
           "-no-frame       open SDL window without a frame and window decorations\n"
7469 3780e197 ths
           "-alt-grab       use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
7470 667accab ths
           "-no-quit        disable SDL window close capability\n"
7471 667accab ths
#endif
7472 52ca8d6a bellard
#ifdef TARGET_I386
7473 52ca8d6a bellard
           "-no-fd-bootchk  disable boot signature checking for floppy disks\n"
7474 52ca8d6a bellard
#endif
7475 a00bad7e bellard
           "-m megs         set virtual RAM size to megs MB [default=%d]\n"
7476 91fc2119 bellard
           "-smp n          set the number of CPUs to 'n' [default=1]\n"
7477 c4b1fcc0 bellard
           "-nographic      disable graphical output and redirect serial I/Os to console\n"
7478 a171fe39 balrog
           "-portrait       rotate graphical output 90 deg left (only PXA LCD)\n"
7479 4ca0074c bellard
#ifndef _WIN32
7480 667accab ths
           "-k language     use keyboard layout (for example \"fr\" for French)\n"
7481 4ca0074c bellard
#endif
7482 1d14ffa9 bellard
#ifdef HAS_AUDIO
7483 1d14ffa9 bellard
           "-audio-help     print list of audio drivers and their options\n"
7484 c0fe3827 bellard
           "-soundhw c1,... enable audio support\n"
7485 c0fe3827 bellard
           "                and only specified sound cards (comma separated list)\n"
7486 c0fe3827 bellard
           "                use -soundhw ? to get the list of supported cards\n"
7487 6a36d84e bellard
           "                use -soundhw all to enable all of them\n"
7488 1d14ffa9 bellard
#endif
7489 89980284 bellard
           "-localtime      set the real time clock to local time [default=utc]\n"
7490 d63d307f bellard
           "-full-screen    start in full screen\n"
7491 a09db21f bellard
#ifdef TARGET_I386
7492 a09db21f bellard
           "-win2k-hack     use it when installing Windows 2000 to avoid a disk full bug\n"
7493 a09db21f bellard
#endif
7494 b389dbfb bellard
           "-usb            enable the USB driver (will be the default soon)\n"
7495 b389dbfb bellard
           "-usbdevice name add the host or guest USB device 'name'\n"
7496 6f7e9aec bellard
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
7497 6f7e9aec bellard
           "-g WxH[xDEPTH]  Set the initial graphical resolution and depth\n"
7498 bb0c6722 bellard
#endif
7499 c35734b2 ths
           "-name string    set the name of the guest\n"
7500 c4b1fcc0 bellard
           "\n"
7501 c4b1fcc0 bellard
           "Network options:\n"
7502 a41b2ff2 pbrook
           "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
7503 7c9d8e07 bellard
           "                create a new Network Interface Card and connect it to VLAN 'n'\n"
7504 c20709aa bellard
#ifdef CONFIG_SLIRP
7505 115defd1 pbrook
           "-net user[,vlan=n][,hostname=host]\n"
7506 115defd1 pbrook
           "                connect the user mode network stack to VLAN 'n' and send\n"
7507 115defd1 pbrook
           "                hostname 'host' to DHCP clients\n"
7508 7c9d8e07 bellard
#endif
7509 7fb843f8 bellard
#ifdef _WIN32
7510 7fb843f8 bellard
           "-net tap[,vlan=n],ifname=name\n"
7511 7fb843f8 bellard
           "                connect the host TAP network interface to VLAN 'n'\n"
7512 7fb843f8 bellard
#else
7513 b46a8906 ths
           "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
7514 b46a8906 ths
           "                connect the host TAP network interface to VLAN 'n' and use the\n"
7515 b46a8906 ths
           "                network scripts 'file' (default=%s)\n"
7516 b46a8906 ths
           "                and 'dfile' (default=%s);\n"
7517 b46a8906 ths
           "                use '[down]script=no' to disable script execution;\n"
7518 7c9d8e07 bellard
           "                use 'fd=h' to connect to an already opened TAP interface\n"
7519 7fb843f8 bellard
#endif
7520 6a00d601 bellard
           "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
7521 7c9d8e07 bellard
           "                connect the vlan 'n' to another VLAN using a socket connection\n"
7522 3d830459 bellard
           "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
7523 3d830459 bellard
           "                connect the vlan 'n' to multicast maddr and port\n"
7524 8a16d273 ths
#ifdef CONFIG_VDE
7525 8a16d273 ths
           "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
7526 8a16d273 ths
           "                connect the vlan 'n' to port 'n' of a vde switch running\n"
7527 8a16d273 ths
           "                on host and listening for incoming connections on 'socketpath'.\n"
7528 8a16d273 ths
           "                Use group 'groupname' and mode 'octalmode' to change default\n"
7529 8a16d273 ths
           "                ownership and permissions for communication port.\n"
7530 8a16d273 ths
#endif
7531 7c9d8e07 bellard
           "-net none       use it alone to have zero network devices; if no -net option\n"
7532 7c9d8e07 bellard
           "                is provided, the default is '-net nic -net user'\n"
7533 7c9d8e07 bellard
           "\n"
7534 7c9d8e07 bellard
#ifdef CONFIG_SLIRP
7535 0db1137d ths
           "-tftp dir       allow tftp access to files in dir [-net user]\n"
7536 47d5d01a ths
           "-bootp file     advertise file in BOOTP replies\n"
7537 7c9d8e07 bellard
#ifndef _WIN32
7538 7c9d8e07 bellard
           "-smb dir        allow SMB access to files in 'dir' [-net user]\n"
7539 c94c8d64 bellard
#endif
7540 9bf05444 bellard
           "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
7541 7c9d8e07 bellard
           "                redirect TCP or UDP connections from host to guest [-net user]\n"
7542 c20709aa bellard
#endif
7543 a20dd508 bellard
           "\n"
7544 c4b1fcc0 bellard
           "Linux boot specific:\n"
7545 a20dd508 bellard
           "-kernel bzImage use 'bzImage' as kernel image\n"
7546 a20dd508 bellard
           "-append cmdline use 'cmdline' as kernel command line\n"
7547 a20dd508 bellard
           "-initrd file    use 'file' as initial ram disk\n"
7548 fc01f7e7 bellard
           "\n"
7549 330d0414 bellard
           "Debug/Expert options:\n"
7550 82c643ff bellard
           "-monitor dev    redirect the monitor to char device 'dev'\n"
7551 82c643ff bellard
           "-serial dev     redirect the serial port to char device 'dev'\n"
7552 6508fe59 bellard
           "-parallel dev   redirect the parallel port to char device 'dev'\n"
7553 f7cce898 bellard
           "-pidfile file   Write PID to 'file'\n"
7554 cd6f1169 bellard
           "-S              freeze CPU at startup (use 'c' to start execution)\n"
7555 cfc3475a pbrook
           "-s              wait gdb connection to port\n"
7556 cfc3475a pbrook
           "-p port         set gdb connection port [default=%s]\n"
7557 f193c797 bellard
           "-d item1,...    output log to %s (use -d ? for a list of log items)\n"
7558 46d4767d bellard
           "-hdachs c,h,s[,t]  force hard disk 0 physical geometry and the optional BIOS\n"
7559 46d4767d bellard
           "                translation (t=none or lba) (usually qemu can guess them)\n"
7560 87b47350 bellard
           "-L path         set the directory for the BIOS, VGA BIOS and keymaps\n"
7561 d993e026 bellard
#ifdef USE_KQEMU
7562 6515b203 bellard
           "-kernel-kqemu   enable KQEMU full virtualization (default is user mode only)\n"
7563 d993e026 bellard
           "-no-kqemu       disable KQEMU kernel module usage\n"
7564 d993e026 bellard
#endif
7565 bb0c6722 bellard
#ifdef TARGET_I386
7566 1bfe856e bellard
           "-std-vga        simulate a standard VGA card with VESA Bochs Extensions\n"
7567 1bfe856e bellard
           "                (default is CL-GD5446 PCI VGA)\n"
7568 6515b203 bellard
           "-no-acpi        disable ACPI\n"
7569 bb0c6722 bellard
#endif
7570 4d3b6f6e balrog
#ifdef CONFIG_CURSES
7571 4d3b6f6e balrog
           "-curses         use a curses/ncurses interface instead of SDL\n"
7572 4d3b6f6e balrog
#endif
7573 d1beab82 bellard
           "-no-reboot      exit instead of rebooting\n"
7574 b2f76161 aurel32
           "-no-shutdown    stop before shutdown\n"
7575 a8080006 aurel32
           "-loadvm [tag|id]  start right away with a saved state (loadvm in monitor)\n"
7576 24236869 bellard
           "-vnc display    start a VNC server on display\n"
7577 71e3ceb8 ths
#ifndef _WIN32
7578 71e3ceb8 ths
           "-daemonize      daemonize QEMU after initializing\n"
7579 71e3ceb8 ths
#endif
7580 9ae02555 ths
           "-option-rom rom load a file, rom, into the option ROM space\n"
7581 66508601 blueswir1
#ifdef TARGET_SPARC
7582 66508601 blueswir1
           "-prom-env variable=value  set OpenBIOS nvram variables\n"
7583 66508601 blueswir1
#endif
7584 f3dcfada ths
           "-clock          force the use of the given methods for timer alarm.\n"
7585 3adda04c aurel32
           "                To see what timers are available use -clock ?\n"
7586 bce61846 bellard
           "-startdate      select initial date of the clock\n"
7587 2e70f6ef pbrook
           "-icount [N|auto]\n"
7588 dd5d6fe9 pbrook
           "                Enable virtual instruction counter with 2^N clock ticks per instruction\n"
7589 0824d6fc bellard
           "\n"
7590 82c643ff bellard
           "During emulation, the following keys are useful:\n"
7591 032a8c9e bellard
           "ctrl-alt-f      toggle full screen\n"
7592 032a8c9e bellard
           "ctrl-alt-n      switch to virtual console 'n'\n"
7593 032a8c9e bellard
           "ctrl-alt        toggle mouse and keyboard grab\n"
7594 82c643ff bellard
           "\n"
7595 82c643ff bellard
           "When using -nographic, press 'ctrl-a h' to get some help.\n"
7596 82c643ff bellard
           ,
7597 0db63474 bellard
           "qemu",
7598 a00bad7e bellard
           DEFAULT_RAM_SIZE,
7599 7c9d8e07 bellard
#ifndef _WIN32
7600 a00bad7e bellard
           DEFAULT_NETWORK_SCRIPT,
7601 b46a8906 ths
           DEFAULT_NETWORK_DOWN_SCRIPT,
7602 7c9d8e07 bellard
#endif
7603 6e44ba7f bellard
           DEFAULT_GDBSTUB_PORT,
7604 bce61846 bellard
           "/tmp/qemu.log");
7605 15f82208 ths
    exit(exitcode);
7606 0824d6fc bellard
}
7607 0824d6fc bellard
7608 cd6f1169 bellard
#define HAS_ARG 0x0001
7609 cd6f1169 bellard
7610 cd6f1169 bellard
enum {
7611 cd6f1169 bellard
    QEMU_OPTION_h,
7612 cd6f1169 bellard
7613 cc1daa40 bellard
    QEMU_OPTION_M,
7614 94fc95cd j_mayer
    QEMU_OPTION_cpu,
7615 cd6f1169 bellard
    QEMU_OPTION_fda,
7616 cd6f1169 bellard
    QEMU_OPTION_fdb,
7617 cd6f1169 bellard
    QEMU_OPTION_hda,
7618 cd6f1169 bellard
    QEMU_OPTION_hdb,
7619 cd6f1169 bellard
    QEMU_OPTION_hdc,
7620 cd6f1169 bellard
    QEMU_OPTION_hdd,
7621 e4bcb14c ths
    QEMU_OPTION_drive,
7622 cd6f1169 bellard
    QEMU_OPTION_cdrom,
7623 3e3d5815 balrog
    QEMU_OPTION_mtdblock,
7624 a1bb27b1 pbrook
    QEMU_OPTION_sd,
7625 86f55663 j_mayer
    QEMU_OPTION_pflash,
7626 cd6f1169 bellard
    QEMU_OPTION_boot,
7627 cd6f1169 bellard
    QEMU_OPTION_snapshot,
7628 52ca8d6a bellard
#ifdef TARGET_I386
7629 52ca8d6a bellard
    QEMU_OPTION_no_fd_bootchk,
7630 52ca8d6a bellard
#endif
7631 cd6f1169 bellard
    QEMU_OPTION_m,
7632 cd6f1169 bellard
    QEMU_OPTION_nographic,
7633 a171fe39 balrog
    QEMU_OPTION_portrait,
7634 1d14ffa9 bellard
#ifdef HAS_AUDIO
7635 1d14ffa9 bellard
    QEMU_OPTION_audio_help,
7636 1d14ffa9 bellard
    QEMU_OPTION_soundhw,
7637 1d14ffa9 bellard
#endif
7638 cd6f1169 bellard
7639 7c9d8e07 bellard
    QEMU_OPTION_net,
7640 c7f74643 bellard
    QEMU_OPTION_tftp,
7641 47d5d01a ths
    QEMU_OPTION_bootp,
7642 9d728e8c bellard
    QEMU_OPTION_smb,
7643 9bf05444 bellard
    QEMU_OPTION_redir,
7644 cd6f1169 bellard
7645 cd6f1169 bellard
    QEMU_OPTION_kernel,
7646 cd6f1169 bellard
    QEMU_OPTION_append,
7647 cd6f1169 bellard
    QEMU_OPTION_initrd,
7648 cd6f1169 bellard
7649 cd6f1169 bellard
    QEMU_OPTION_S,
7650 cd6f1169 bellard
    QEMU_OPTION_s,
7651 cd6f1169 bellard
    QEMU_OPTION_p,
7652 cd6f1169 bellard
    QEMU_OPTION_d,
7653 cd6f1169 bellard
    QEMU_OPTION_hdachs,
7654 cd6f1169 bellard
    QEMU_OPTION_L,
7655 1192dad8 j_mayer
    QEMU_OPTION_bios,
7656 3d11d0eb bellard
    QEMU_OPTION_k,
7657 ee22c2f7 bellard
    QEMU_OPTION_localtime,
7658 1f04275e bellard
    QEMU_OPTION_cirrusvga,
7659 d34cab9f ths
    QEMU_OPTION_vmsvga,
7660 e9b137c2 bellard
    QEMU_OPTION_g,
7661 1bfe856e bellard
    QEMU_OPTION_std_vga,
7662 20d8a3ed ths
    QEMU_OPTION_echr,
7663 82c643ff bellard
    QEMU_OPTION_monitor,
7664 82c643ff bellard
    QEMU_OPTION_serial,
7665 6508fe59 bellard
    QEMU_OPTION_parallel,
7666 d63d307f bellard
    QEMU_OPTION_loadvm,
7667 d63d307f bellard
    QEMU_OPTION_full_screen,
7668 43523e93 ths
    QEMU_OPTION_no_frame,
7669 3780e197 ths
    QEMU_OPTION_alt_grab,
7670 667accab ths
    QEMU_OPTION_no_quit,
7671 f7cce898 bellard
    QEMU_OPTION_pidfile,
7672 d993e026 bellard
    QEMU_OPTION_no_kqemu,
7673 89bfc105 bellard
    QEMU_OPTION_kernel_kqemu,
7674 a09db21f bellard
    QEMU_OPTION_win2k_hack,
7675 bb36d470 bellard
    QEMU_OPTION_usb,
7676 a594cfbf bellard
    QEMU_OPTION_usbdevice,
7677 6a00d601 bellard
    QEMU_OPTION_smp,
7678 24236869 bellard
    QEMU_OPTION_vnc,
7679 6515b203 bellard
    QEMU_OPTION_no_acpi,
7680 4d3b6f6e balrog
    QEMU_OPTION_curses,
7681 d1beab82 bellard
    QEMU_OPTION_no_reboot,
7682 b2f76161 aurel32
    QEMU_OPTION_no_shutdown,
7683 9467cd46 balrog
    QEMU_OPTION_show_cursor,
7684 71e3ceb8 ths
    QEMU_OPTION_daemonize,
7685 9ae02555 ths
    QEMU_OPTION_option_rom,
7686 c35734b2 ths
    QEMU_OPTION_semihosting,
7687 c35734b2 ths
    QEMU_OPTION_name,
7688 66508601 blueswir1
    QEMU_OPTION_prom_env,
7689 2b8f2d41 balrog
    QEMU_OPTION_old_param,
7690 f3dcfada ths
    QEMU_OPTION_clock,
7691 7e0af5d0 bellard
    QEMU_OPTION_startdate,
7692 26a5f13b bellard
    QEMU_OPTION_tb_size,
7693 2e70f6ef pbrook
    QEMU_OPTION_icount,
7694 cd6f1169 bellard
};
7695 cd6f1169 bellard
7696 cd6f1169 bellard
typedef struct QEMUOption {
7697 cd6f1169 bellard
    const char *name;
7698 cd6f1169 bellard
    int flags;
7699 cd6f1169 bellard
    int index;
7700 cd6f1169 bellard
} QEMUOption;
7701 cd6f1169 bellard
7702 cd6f1169 bellard
const QEMUOption qemu_options[] = {
7703 cd6f1169 bellard
    { "h", 0, QEMU_OPTION_h },
7704 64423fb2 pbrook
    { "help", 0, QEMU_OPTION_h },
7705 cd6f1169 bellard
7706 cc1daa40 bellard
    { "M", HAS_ARG, QEMU_OPTION_M },
7707 94fc95cd j_mayer
    { "cpu", HAS_ARG, QEMU_OPTION_cpu },
7708 cd6f1169 bellard
    { "fda", HAS_ARG, QEMU_OPTION_fda },
7709 cd6f1169 bellard
    { "fdb", HAS_ARG, QEMU_OPTION_fdb },
7710 cd6f1169 bellard
    { "hda", HAS_ARG, QEMU_OPTION_hda },
7711 cd6f1169 bellard
    { "hdb", HAS_ARG, QEMU_OPTION_hdb },
7712 cd6f1169 bellard
    { "hdc", HAS_ARG, QEMU_OPTION_hdc },
7713 cd6f1169 bellard
    { "hdd", HAS_ARG, QEMU_OPTION_hdd },
7714 e4bcb14c ths
    { "drive", HAS_ARG, QEMU_OPTION_drive },
7715 cd6f1169 bellard
    { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
7716 3e3d5815 balrog
    { "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
7717 a1bb27b1 pbrook
    { "sd", HAS_ARG, QEMU_OPTION_sd },
7718 86f55663 j_mayer
    { "pflash", HAS_ARG, QEMU_OPTION_pflash },
7719 cd6f1169 bellard
    { "boot", HAS_ARG, QEMU_OPTION_boot },
7720 cd6f1169 bellard
    { "snapshot", 0, QEMU_OPTION_snapshot },
7721 52ca8d6a bellard
#ifdef TARGET_I386
7722 52ca8d6a bellard
    { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk },
7723 52ca8d6a bellard
#endif
7724 cd6f1169 bellard
    { "m", HAS_ARG, QEMU_OPTION_m },
7725 cd6f1169 bellard
    { "nographic", 0, QEMU_OPTION_nographic },
7726 a171fe39 balrog
    { "portrait", 0, QEMU_OPTION_portrait },
7727 3d11d0eb bellard
    { "k", HAS_ARG, QEMU_OPTION_k },
7728 1d14ffa9 bellard
#ifdef HAS_AUDIO
7729 1d14ffa9 bellard
    { "audio-help", 0, QEMU_OPTION_audio_help },
7730 1d14ffa9 bellard
    { "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
7731 1d14ffa9 bellard
#endif
7732 cd6f1169 bellard
7733 7c9d8e07 bellard
    { "net", HAS_ARG, QEMU_OPTION_net},
7734 158156d1 bellard
#ifdef CONFIG_SLIRP
7735 c7f74643 bellard
    { "tftp", HAS_ARG, QEMU_OPTION_tftp },
7736 47d5d01a ths
    { "bootp", HAS_ARG, QEMU_OPTION_bootp },
7737 c94c8d64 bellard
#ifndef _WIN32
7738 9d728e8c bellard
    { "smb", HAS_ARG, QEMU_OPTION_smb },
7739 c94c8d64 bellard
#endif
7740 9bf05444 bellard
    { "redir", HAS_ARG, QEMU_OPTION_redir },
7741 158156d1 bellard
#endif
7742 cd6f1169 bellard
7743 cd6f1169 bellard
    { "kernel", HAS_ARG, QEMU_OPTION_kernel },
7744 cd6f1169 bellard
    { "append", HAS_ARG, QEMU_OPTION_append },
7745 cd6f1169 bellard
    { "initrd", HAS_ARG, QEMU_OPTION_initrd },
7746 cd6f1169 bellard
7747 cd6f1169 bellard
    { "S", 0, QEMU_OPTION_S },
7748 cd6f1169 bellard
    { "s", 0, QEMU_OPTION_s },
7749 cd6f1169 bellard
    { "p", HAS_ARG, QEMU_OPTION_p },
7750 cd6f1169 bellard
    { "d", HAS_ARG, QEMU_OPTION_d },
7751 cd6f1169 bellard
    { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
7752 cd6f1169 bellard
    { "L", HAS_ARG, QEMU_OPTION_L },
7753 1192dad8 j_mayer
    { "bios", HAS_ARG, QEMU_OPTION_bios },
7754 d993e026 bellard
#ifdef USE_KQEMU
7755 d993e026 bellard
    { "no-kqemu", 0, QEMU_OPTION_no_kqemu },
7756 89bfc105 bellard
    { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu },
7757 d993e026 bellard
#endif
7758 6f7e9aec bellard
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
7759 e9b137c2 bellard
    { "g", 1, QEMU_OPTION_g },
7760 77d4bc34 bellard
#endif
7761 ee22c2f7 bellard
    { "localtime", 0, QEMU_OPTION_localtime },
7762 1bfe856e bellard
    { "std-vga", 0, QEMU_OPTION_std_vga },
7763 8b6e0729 balrog
    { "echr", HAS_ARG, QEMU_OPTION_echr },
7764 8b6e0729 balrog
    { "monitor", HAS_ARG, QEMU_OPTION_monitor },
7765 8b6e0729 balrog
    { "serial", HAS_ARG, QEMU_OPTION_serial },
7766 8b6e0729 balrog
    { "parallel", HAS_ARG, QEMU_OPTION_parallel },
7767 d63d307f bellard
    { "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
7768 d63d307f bellard
    { "full-screen", 0, QEMU_OPTION_full_screen },
7769 667accab ths
#ifdef CONFIG_SDL
7770 43523e93 ths
    { "no-frame", 0, QEMU_OPTION_no_frame },
7771 3780e197 ths
    { "alt-grab", 0, QEMU_OPTION_alt_grab },
7772 667accab ths
    { "no-quit", 0, QEMU_OPTION_no_quit },
7773 667accab ths
#endif
7774 f7cce898 bellard
    { "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
7775 a09db21f bellard
    { "win2k-hack", 0, QEMU_OPTION_win2k_hack },
7776 a594cfbf bellard
    { "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
7777 6a00d601 bellard
    { "smp", HAS_ARG, QEMU_OPTION_smp },
7778 24236869 bellard
    { "vnc", HAS_ARG, QEMU_OPTION_vnc },
7779 4d3b6f6e balrog
#ifdef CONFIG_CURSES
7780 4d3b6f6e balrog
    { "curses", 0, QEMU_OPTION_curses },
7781 4d3b6f6e balrog
#endif
7782 96d30e48 ths
7783 1f04275e bellard
    /* temporary options */
7784 a594cfbf bellard
    { "usb", 0, QEMU_OPTION_usb },
7785 1f04275e bellard
    { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
7786 d34cab9f ths
    { "vmwarevga", 0, QEMU_OPTION_vmsvga },
7787 6515b203 bellard
    { "no-acpi", 0, QEMU_OPTION_no_acpi },
7788 d1beab82 bellard
    { "no-reboot", 0, QEMU_OPTION_no_reboot },
7789 b2f76161 aurel32
    { "no-shutdown", 0, QEMU_OPTION_no_shutdown },
7790 9467cd46 balrog
    { "show-cursor", 0, QEMU_OPTION_show_cursor },
7791 71e3ceb8 ths
    { "daemonize", 0, QEMU_OPTION_daemonize },
7792 9ae02555 ths
    { "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
7793 a87295e8 pbrook
#if defined(TARGET_ARM) || defined(TARGET_M68K)
7794 8e71621f pbrook
    { "semihosting", 0, QEMU_OPTION_semihosting },
7795 8e71621f pbrook
#endif
7796 c35734b2 ths
    { "name", HAS_ARG, QEMU_OPTION_name },
7797 66508601 blueswir1
#if defined(TARGET_SPARC)
7798 66508601 blueswir1
    { "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
7799 66508601 blueswir1
#endif
7800 2b8f2d41 balrog
#if defined(TARGET_ARM)
7801 2b8f2d41 balrog
    { "old-param", 0, QEMU_OPTION_old_param },
7802 2b8f2d41 balrog
#endif
7803 f3dcfada ths
    { "clock", HAS_ARG, QEMU_OPTION_clock },
7804 7e0af5d0 bellard
    { "startdate", HAS_ARG, QEMU_OPTION_startdate },
7805 26a5f13b bellard
    { "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
7806 2e70f6ef pbrook
    { "icount", HAS_ARG, QEMU_OPTION_icount },
7807 cd6f1169 bellard
    { NULL },
7808 fc01f7e7 bellard
};
7809 fc01f7e7 bellard
7810 5905b2e5 bellard
/* password input */
7811 5905b2e5 bellard
7812 2bac6019 balrog
int qemu_key_check(BlockDriverState *bs, const char *name)
7813 2bac6019 balrog
{
7814 2bac6019 balrog
    char password[256];
7815 2bac6019 balrog
    int i;
7816 2bac6019 balrog
7817 2bac6019 balrog
    if (!bdrv_is_encrypted(bs))
7818 2bac6019 balrog
        return 0;
7819 2bac6019 balrog
7820 2bac6019 balrog
    term_printf("%s is encrypted.\n", name);
7821 2bac6019 balrog
    for(i = 0; i < 3; i++) {
7822 2bac6019 balrog
        monitor_readline("Password: ", 1, password, sizeof(password));
7823 2bac6019 balrog
        if (bdrv_set_key(bs, password) == 0)
7824 2bac6019 balrog
            return 0;
7825 2bac6019 balrog
        term_printf("invalid password\n");
7826 2bac6019 balrog
    }
7827 2bac6019 balrog
    return -EPERM;
7828 2bac6019 balrog
}
7829 2bac6019 balrog
7830 5905b2e5 bellard
static BlockDriverState *get_bdrv(int index)
7831 5905b2e5 bellard
{
7832 e4bcb14c ths
    if (index > nb_drives)
7833 e4bcb14c ths
        return NULL;
7834 e4bcb14c ths
    return drives_table[index].bdrv;
7835 5905b2e5 bellard
}
7836 5905b2e5 bellard
7837 5905b2e5 bellard
static void read_passwords(void)
7838 5905b2e5 bellard
{
7839 5905b2e5 bellard
    BlockDriverState *bs;
7840 2bac6019 balrog
    int i;
7841 5905b2e5 bellard
7842 5905b2e5 bellard
    for(i = 0; i < 6; i++) {
7843 5905b2e5 bellard
        bs = get_bdrv(i);
7844 2bac6019 balrog
        if (bs)
7845 2bac6019 balrog
            qemu_key_check(bs, bdrv_get_device_name(bs));
7846 5905b2e5 bellard
    }
7847 5905b2e5 bellard
}
7848 5905b2e5 bellard
7849 1d14ffa9 bellard
#ifdef HAS_AUDIO
7850 6a36d84e bellard
struct soundhw soundhw[] = {
7851 b00052e4 balrog
#ifdef HAS_AUDIO_CHOICE
7852 4ce7ff6e aurel32
#if defined(TARGET_I386) || defined(TARGET_MIPS)
7853 fd06c375 bellard
    {
7854 fd06c375 bellard
        "pcspk",
7855 fd06c375 bellard
        "PC speaker",
7856 fd06c375 bellard
        0,
7857 fd06c375 bellard
        1,
7858 fd06c375 bellard
        { .init_isa = pcspk_audio_init }
7859 fd06c375 bellard
    },
7860 fd06c375 bellard
#endif
7861 6a36d84e bellard
    {
7862 6a36d84e bellard
        "sb16",
7863 6a36d84e bellard
        "Creative Sound Blaster 16",
7864 6a36d84e bellard
        0,
7865 6a36d84e bellard
        1,
7866 6a36d84e bellard
        { .init_isa = SB16_init }
7867 6a36d84e bellard
    },
7868 6a36d84e bellard
7869 cc53d26d malc
#ifdef CONFIG_CS4231A
7870 cc53d26d malc
    {
7871 cc53d26d malc
        "cs4231a",
7872 cc53d26d malc
        "CS4231A",
7873 cc53d26d malc
        0,
7874 cc53d26d malc
        1,
7875 cc53d26d malc
        { .init_isa = cs4231a_init }
7876 cc53d26d malc
    },
7877 cc53d26d malc
#endif
7878 cc53d26d malc
7879 1d14ffa9 bellard
#ifdef CONFIG_ADLIB
7880 6a36d84e bellard
    {
7881 6a36d84e bellard
        "adlib",
7882 1d14ffa9 bellard
#ifdef HAS_YMF262
7883 6a36d84e bellard
        "Yamaha YMF262 (OPL3)",
7884 1d14ffa9 bellard
#else
7885 6a36d84e bellard
        "Yamaha YM3812 (OPL2)",
7886 1d14ffa9 bellard
#endif
7887 6a36d84e bellard
        0,
7888 6a36d84e bellard
        1,
7889 6a36d84e bellard
        { .init_isa = Adlib_init }
7890 6a36d84e bellard
    },
7891 1d14ffa9 bellard
#endif
7892 6a36d84e bellard
7893 1d14ffa9 bellard
#ifdef CONFIG_GUS
7894 6a36d84e bellard
    {
7895 6a36d84e bellard
        "gus",
7896 6a36d84e bellard
        "Gravis Ultrasound GF1",
7897 6a36d84e bellard
        0,
7898 6a36d84e bellard
        1,
7899 6a36d84e bellard
        { .init_isa = GUS_init }
7900 6a36d84e bellard
    },
7901 1d14ffa9 bellard
#endif
7902 6a36d84e bellard
7903 e5c9a13e balrog
#ifdef CONFIG_AC97
7904 e5c9a13e balrog
    {
7905 e5c9a13e balrog
        "ac97",
7906 e5c9a13e balrog
        "Intel 82801AA AC97 Audio",
7907 e5c9a13e balrog
        0,
7908 e5c9a13e balrog
        0,
7909 e5c9a13e balrog
        { .init_pci = ac97_init }
7910 e5c9a13e balrog
    },
7911 e5c9a13e balrog
#endif
7912 e5c9a13e balrog
7913 6a36d84e bellard
    {
7914 6a36d84e bellard
        "es1370",
7915 6a36d84e bellard
        "ENSONIQ AudioPCI ES1370",
7916 6a36d84e bellard
        0,
7917 6a36d84e bellard
        0,
7918 6a36d84e bellard
        { .init_pci = es1370_init }
7919 6a36d84e bellard
    },
7920 b00052e4 balrog
#endif
7921 6a36d84e bellard
7922 6a36d84e bellard
    { NULL, NULL, 0, 0, { NULL } }
7923 6a36d84e bellard
};
7924 6a36d84e bellard
7925 6a36d84e bellard
static void select_soundhw (const char *optarg)
7926 6a36d84e bellard
{
7927 6a36d84e bellard
    struct soundhw *c;
7928 6a36d84e bellard
7929 6a36d84e bellard
    if (*optarg == '?') {
7930 6a36d84e bellard
    show_valid_cards:
7931 6a36d84e bellard
7932 6a36d84e bellard
        printf ("Valid sound card names (comma separated):\n");
7933 6a36d84e bellard
        for (c = soundhw; c->name; ++c) {
7934 6a36d84e bellard
            printf ("%-11s %s\n", c->name, c->descr);
7935 6a36d84e bellard
        }
7936 6a36d84e bellard
        printf ("\n-soundhw all will enable all of the above\n");
7937 1d14ffa9 bellard
        exit (*optarg != '?');
7938 1d14ffa9 bellard
    }
7939 1d14ffa9 bellard
    else {
7940 6a36d84e bellard
        size_t l;
7941 1d14ffa9 bellard
        const char *p;
7942 1d14ffa9 bellard
        char *e;
7943 1d14ffa9 bellard
        int bad_card = 0;
7944 1d14ffa9 bellard
7945 6a36d84e bellard
        if (!strcmp (optarg, "all")) {
7946 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
7947 6a36d84e bellard
                c->enabled = 1;
7948 6a36d84e bellard
            }
7949 6a36d84e bellard
            return;
7950 6a36d84e bellard
        }
7951 1d14ffa9 bellard
7952 6a36d84e bellard
        p = optarg;
7953 1d14ffa9 bellard
        while (*p) {
7954 1d14ffa9 bellard
            e = strchr (p, ',');
7955 1d14ffa9 bellard
            l = !e ? strlen (p) : (size_t) (e - p);
7956 6a36d84e bellard
7957 6a36d84e bellard
            for (c = soundhw; c->name; ++c) {
7958 6a36d84e bellard
                if (!strncmp (c->name, p, l)) {
7959 6a36d84e bellard
                    c->enabled = 1;
7960 1d14ffa9 bellard
                    break;
7961 1d14ffa9 bellard
                }
7962 1d14ffa9 bellard
            }
7963 6a36d84e bellard
7964 6a36d84e bellard
            if (!c->name) {
7965 1d14ffa9 bellard
                if (l > 80) {
7966 1d14ffa9 bellard
                    fprintf (stderr,
7967 1d14ffa9 bellard
                             "Unknown sound card name (too big to show)\n");
7968 1d14ffa9 bellard
                }
7969 1d14ffa9 bellard
                else {
7970 1d14ffa9 bellard
                    fprintf (stderr, "Unknown sound card name `%.*s'\n",
7971 1d14ffa9 bellard
                             (int) l, p);
7972 1d14ffa9 bellard
                }
7973 1d14ffa9 bellard
                bad_card = 1;
7974 1d14ffa9 bellard
            }
7975 1d14ffa9 bellard
            p += l + (e != NULL);
7976 1d14ffa9 bellard
        }
7977 1d14ffa9 bellard
7978 1d14ffa9 bellard
        if (bad_card)
7979 1d14ffa9 bellard
            goto show_valid_cards;
7980 1d14ffa9 bellard
    }
7981 1d14ffa9 bellard
}
7982 1d14ffa9 bellard
#endif
7983 1d14ffa9 bellard
7984 3587d7e6 bellard
#ifdef _WIN32
7985 3587d7e6 bellard
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
7986 3587d7e6 bellard
{
7987 3587d7e6 bellard
    exit(STATUS_CONTROL_C_EXIT);
7988 3587d7e6 bellard
    return TRUE;
7989 3587d7e6 bellard
}
7990 3587d7e6 bellard
#endif
7991 3587d7e6 bellard
7992 7c9d8e07 bellard
#define MAX_NET_CLIENTS 32
7993 c20709aa bellard
7994 0824d6fc bellard
int main(int argc, char **argv)
7995 0824d6fc bellard
{
7996 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
7997 cfc3475a pbrook
    int use_gdbstub;
7998 cfc3475a pbrook
    const char *gdbstub_port;
7999 67b915a5 bellard
#endif
8000 28c5af54 j_mayer
    uint32_t boot_devices_bitmap = 0;
8001 e4bcb14c ths
    int i;
8002 28c5af54 j_mayer
    int snapshot, linux_boot, net_boot;
8003 7f7f9873 bellard
    const char *initrd_filename;
8004 a20dd508 bellard
    const char *kernel_filename, *kernel_cmdline;
8005 28c5af54 j_mayer
    const char *boot_devices = "";
8006 313aa567 bellard
    DisplayState *ds = &display_state;
8007 46d4767d bellard
    int cyls, heads, secs, translation;
8008 fd5f393a pbrook
    const char *net_clients[MAX_NET_CLIENTS];
8009 7c9d8e07 bellard
    int nb_net_clients;
8010 e4bcb14c ths
    int hda_index;
8011 cd6f1169 bellard
    int optind;
8012 cd6f1169 bellard
    const char *r, *optarg;
8013 82c643ff bellard
    CharDriverState *monitor_hd;
8014 fd5f393a pbrook
    const char *monitor_device;
8015 fd5f393a pbrook
    const char *serial_devices[MAX_SERIAL_PORTS];
8016 8d11df9e bellard
    int serial_device_index;
8017 fd5f393a pbrook
    const char *parallel_devices[MAX_PARALLEL_PORTS];
8018 6508fe59 bellard
    int parallel_device_index;
8019 d63d307f bellard
    const char *loadvm = NULL;
8020 cc1daa40 bellard
    QEMUMachine *machine;
8021 94fc95cd j_mayer
    const char *cpu_model;
8022 fd5f393a pbrook
    const char *usb_devices[MAX_USB_CMDLINE];
8023 a594cfbf bellard
    int usb_devices_index;
8024 71e3ceb8 ths
    int fds[2];
8025 26a5f13b bellard
    int tb_size;
8026 93815bc2 ths
    const char *pid_file = NULL;
8027 833c7174 blueswir1
    VLANState *vlan;
8028 0bd48850 bellard
8029 0bd48850 bellard
    LIST_INIT (&vm_change_state_head);
8030 be995c27 bellard
#ifndef _WIN32
8031 be995c27 bellard
    {
8032 be995c27 bellard
        struct sigaction act;
8033 be995c27 bellard
        sigfillset(&act.sa_mask);
8034 be995c27 bellard
        act.sa_flags = 0;
8035 be995c27 bellard
        act.sa_handler = SIG_IGN;
8036 be995c27 bellard
        sigaction(SIGPIPE, &act, NULL);
8037 be995c27 bellard
    }
8038 3587d7e6 bellard
#else
8039 3587d7e6 bellard
    SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE);
8040 a8e5ac33 bellard
    /* Note: cpu_interrupt() is currently not SMP safe, so we force
8041 a8e5ac33 bellard
       QEMU to run on a single CPU */
8042 a8e5ac33 bellard
    {
8043 a8e5ac33 bellard
        HANDLE h;
8044 a8e5ac33 bellard
        DWORD mask, smask;
8045 a8e5ac33 bellard
        int i;
8046 a8e5ac33 bellard
        h = GetCurrentProcess();
8047 a8e5ac33 bellard
        if (GetProcessAffinityMask(h, &mask, &smask)) {
8048 a8e5ac33 bellard
            for(i = 0; i < 32; i++) {
8049 a8e5ac33 bellard
                if (mask & (1 << i))
8050 a8e5ac33 bellard
                    break;
8051 a8e5ac33 bellard
            }
8052 a8e5ac33 bellard
            if (i != 32) {
8053 a8e5ac33 bellard
                mask = 1 << i;
8054 a8e5ac33 bellard
                SetProcessAffinityMask(h, mask);
8055 a8e5ac33 bellard
            }
8056 a8e5ac33 bellard
        }
8057 a8e5ac33 bellard
    }
8058 67b915a5 bellard
#endif
8059 be995c27 bellard
8060 cc1daa40 bellard
    register_machines();
8061 cc1daa40 bellard
    machine = first_machine;
8062 94fc95cd j_mayer
    cpu_model = NULL;
8063 fc01f7e7 bellard
    initrd_filename = NULL;
8064 4fc5d071 aurel32
    ram_size = 0;
8065 313aa567 bellard
    vga_ram_size = VGA_RAM_SIZE;
8066 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
8067 b4608c04 bellard
    use_gdbstub = 0;
8068 c636bb66 bellard
    gdbstub_port = DEFAULT_GDBSTUB_PORT;
8069 67b915a5 bellard
#endif
8070 33e3963e bellard
    snapshot = 0;
8071 a20dd508 bellard
    nographic = 0;
8072 4d3b6f6e balrog
    curses = 0;
8073 a20dd508 bellard
    kernel_filename = NULL;
8074 a20dd508 bellard
    kernel_cmdline = "";
8075 c4b1fcc0 bellard
    cyls = heads = secs = 0;
8076 46d4767d bellard
    translation = BIOS_ATA_TRANSLATION_AUTO;
8077 c60e08d9 pbrook
    monitor_device = "vc";
8078 c4b1fcc0 bellard
8079 c75a823c aurel32
    serial_devices[0] = "vc:80Cx24C";
8080 8d11df9e bellard
    for(i = 1; i < MAX_SERIAL_PORTS; i++)
8081 fd5f393a pbrook
        serial_devices[i] = NULL;
8082 8d11df9e bellard
    serial_device_index = 0;
8083 3b46e624 ths
8084 c75a823c aurel32
    parallel_devices[0] = "vc:640x480";
8085 6508fe59 bellard
    for(i = 1; i < MAX_PARALLEL_PORTS; i++)
8086 fd5f393a pbrook
        parallel_devices[i] = NULL;
8087 6508fe59 bellard
    parallel_device_index = 0;
8088 3b46e624 ths
8089 a594cfbf bellard
    usb_devices_index = 0;
8090 3b46e624 ths
8091 7c9d8e07 bellard
    nb_net_clients = 0;
8092 e4bcb14c ths
    nb_drives = 0;
8093 e4bcb14c ths
    nb_drives_opt = 0;
8094 e4bcb14c ths
    hda_index = -1;
8095 7c9d8e07 bellard
8096 7c9d8e07 bellard
    nb_nics = 0;
8097 3b46e624 ths
8098 26a5f13b bellard
    tb_size = 0;
8099 26a5f13b bellard
    
8100 cd6f1169 bellard
    optind = 1;
8101 0824d6fc bellard
    for(;;) {
8102 cd6f1169 bellard
        if (optind >= argc)
8103 0824d6fc bellard
            break;
8104 cd6f1169 bellard
        r = argv[optind];
8105 cd6f1169 bellard
        if (r[0] != '-') {
8106 609497ab balrog
            hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
8107 cd6f1169 bellard
        } else {
8108 cd6f1169 bellard
            const QEMUOption *popt;
8109 cd6f1169 bellard
8110 cd6f1169 bellard
            optind++;
8111 dff5efc8 pbrook
            /* Treat --foo the same as -foo.  */
8112 dff5efc8 pbrook
            if (r[1] == '-')
8113 dff5efc8 pbrook
                r++;
8114 cd6f1169 bellard
            popt = qemu_options;
8115 cd6f1169 bellard
            for(;;) {
8116 cd6f1169 bellard
                if (!popt->name) {
8117 5fafdf24 ths
                    fprintf(stderr, "%s: invalid option -- '%s'\n",
8118 cd6f1169 bellard
                            argv[0], r);
8119 cd6f1169 bellard
                    exit(1);
8120 cd6f1169 bellard
                }
8121 cd6f1169 bellard
                if (!strcmp(popt->name, r + 1))
8122 cd6f1169 bellard
                    break;
8123 cd6f1169 bellard
                popt++;
8124 cd6f1169 bellard
            }
8125 cd6f1169 bellard
            if (popt->flags & HAS_ARG) {
8126 cd6f1169 bellard
                if (optind >= argc) {
8127 cd6f1169 bellard
                    fprintf(stderr, "%s: option '%s' requires an argument\n",
8128 cd6f1169 bellard
                            argv[0], r);
8129 cd6f1169 bellard
                    exit(1);
8130 cd6f1169 bellard
                }
8131 cd6f1169 bellard
                optarg = argv[optind++];
8132 cd6f1169 bellard
            } else {
8133 cd6f1169 bellard
                optarg = NULL;
8134 cd6f1169 bellard
            }
8135 cd6f1169 bellard
8136 cd6f1169 bellard
            switch(popt->index) {
8137 cc1daa40 bellard
            case QEMU_OPTION_M:
8138 cc1daa40 bellard
                machine = find_machine(optarg);
8139 cc1daa40 bellard
                if (!machine) {
8140 cc1daa40 bellard
                    QEMUMachine *m;
8141 cc1daa40 bellard
                    printf("Supported machines are:\n");
8142 cc1daa40 bellard
                    for(m = first_machine; m != NULL; m = m->next) {
8143 cc1daa40 bellard
                        printf("%-10s %s%s\n",
8144 5fafdf24 ths
                               m->name, m->desc,
8145 cc1daa40 bellard
                               m == first_machine ? " (default)" : "");
8146 cc1daa40 bellard
                    }
8147 15f82208 ths
                    exit(*optarg != '?');
8148 cc1daa40 bellard
                }
8149 cc1daa40 bellard
                break;
8150 94fc95cd j_mayer
            case QEMU_OPTION_cpu:
8151 94fc95cd j_mayer
                /* hw initialization will check this */
8152 15f82208 ths
                if (*optarg == '?') {
8153 c732abe2 j_mayer
/* XXX: implement xxx_cpu_list for targets that still miss it */
8154 c732abe2 j_mayer
#if defined(cpu_list)
8155 c732abe2 j_mayer
                    cpu_list(stdout, &fprintf);
8156 94fc95cd j_mayer
#endif
8157 15f82208 ths
                    exit(0);
8158 94fc95cd j_mayer
                } else {
8159 94fc95cd j_mayer
                    cpu_model = optarg;
8160 94fc95cd j_mayer
                }
8161 94fc95cd j_mayer
                break;
8162 cd6f1169 bellard
            case QEMU_OPTION_initrd:
8163 fc01f7e7 bellard
                initrd_filename = optarg;
8164 fc01f7e7 bellard
                break;
8165 cd6f1169 bellard
            case QEMU_OPTION_hda:
8166 e4bcb14c ths
                if (cyls == 0)
8167 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS, 0);
8168 e4bcb14c ths
                else
8169 609497ab balrog
                    hda_index = drive_add(optarg, HD_ALIAS
8170 e4bcb14c ths
                             ",cyls=%d,heads=%d,secs=%d%s",
8171 609497ab balrog
                             0, cyls, heads, secs,
8172 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_LBA ?
8173 e4bcb14c ths
                                 ",trans=lba" :
8174 e4bcb14c ths
                             translation == BIOS_ATA_TRANSLATION_NONE ?
8175 e4bcb14c ths
                                 ",trans=none" : "");
8176 e4bcb14c ths
                 break;
8177 cd6f1169 bellard
            case QEMU_OPTION_hdb:
8178 cc1daa40 bellard
            case QEMU_OPTION_hdc:
8179 cc1daa40 bellard
            case QEMU_OPTION_hdd:
8180 609497ab balrog
                drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda);
8181 fc01f7e7 bellard
                break;
8182 e4bcb14c ths
            case QEMU_OPTION_drive:
8183 609497ab balrog
                drive_add(NULL, "%s", optarg);
8184 e4bcb14c ths
                break;
8185 3e3d5815 balrog
            case QEMU_OPTION_mtdblock:
8186 609497ab balrog
                drive_add(optarg, MTD_ALIAS);
8187 3e3d5815 balrog
                break;
8188 a1bb27b1 pbrook
            case QEMU_OPTION_sd:
8189 609497ab balrog
                drive_add(optarg, SD_ALIAS);
8190 a1bb27b1 pbrook
                break;
8191 86f55663 j_mayer
            case QEMU_OPTION_pflash:
8192 609497ab balrog
                drive_add(optarg, PFLASH_ALIAS);
8193 86f55663 j_mayer
                break;
8194 cd6f1169 bellard
            case QEMU_OPTION_snapshot:
8195 33e3963e bellard
                snapshot = 1;
8196 33e3963e bellard
                break;
8197 cd6f1169 bellard
            case QEMU_OPTION_hdachs:
8198 330d0414 bellard
                {
8199 330d0414 bellard
                    const char *p;
8200 330d0414 bellard
                    p = optarg;
8201 330d0414 bellard
                    cyls = strtol(p, (char **)&p, 0);
8202 46d4767d bellard
                    if (cyls < 1 || cyls > 16383)
8203 46d4767d bellard
                        goto chs_fail;
8204 330d0414 bellard
                    if (*p != ',')
8205 330d0414 bellard
                        goto chs_fail;
8206 330d0414 bellard
                    p++;
8207 330d0414 bellard
                    heads = strtol(p, (char **)&p, 0);
8208 46d4767d bellard
                    if (heads < 1 || heads > 16)
8209 46d4767d bellard
                        goto chs_fail;
8210 330d0414 bellard
                    if (*p != ',')
8211 330d0414 bellard
                        goto chs_fail;
8212 330d0414 bellard
                    p++;
8213 330d0414 bellard
                    secs = strtol(p, (char **)&p, 0);
8214 46d4767d bellard
                    if (secs < 1 || secs > 63)
8215 46d4767d bellard
                        goto chs_fail;
8216 46d4767d bellard
                    if (*p == ',') {
8217 46d4767d bellard
                        p++;
8218 46d4767d bellard
                        if (!strcmp(p, "none"))
8219 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_NONE;
8220 46d4767d bellard
                        else if (!strcmp(p, "lba"))
8221 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_LBA;
8222 46d4767d bellard
                        else if (!strcmp(p, "auto"))
8223 46d4767d bellard
                            translation = BIOS_ATA_TRANSLATION_AUTO;
8224 46d4767d bellard
                        else
8225 46d4767d bellard
                            goto chs_fail;
8226 46d4767d bellard
                    } else if (*p != '\0') {
8227 c4b1fcc0 bellard
                    chs_fail:
8228 46d4767d bellard
                        fprintf(stderr, "qemu: invalid physical CHS format\n");
8229 46d4767d bellard
                        exit(1);
8230 c4b1fcc0 bellard
                    }
8231 e4bcb14c ths
                    if (hda_index != -1)
8232 609497ab balrog
                        snprintf(drives_opt[hda_index].opt,
8233 609497ab balrog
                                 sizeof(drives_opt[hda_index].opt),
8234 609497ab balrog
                                 HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
8235 609497ab balrog
                                 0, cyls, heads, secs,
8236 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_LBA ?
8237 e4bcb14c ths
                                         ",trans=lba" :
8238 e4bcb14c ths
                                 translation == BIOS_ATA_TRANSLATION_NONE ?
8239 e4bcb14c ths
                                     ",trans=none" : "");
8240 330d0414 bellard
                }
8241 330d0414 bellard
                break;
8242 cd6f1169 bellard
            case QEMU_OPTION_nographic:
8243 fd5f393a pbrook
                serial_devices[0] = "stdio";
8244 fd5f393a pbrook
                parallel_devices[0] = "null";
8245 fd5f393a pbrook
                monitor_device = "stdio";
8246 a20dd508 bellard
                nographic = 1;
8247 a20dd508 bellard
                break;
8248 4d3b6f6e balrog
#ifdef CONFIG_CURSES
8249 4d3b6f6e balrog
            case QEMU_OPTION_curses:
8250 4d3b6f6e balrog
                curses = 1;
8251 4d3b6f6e balrog
                break;
8252 4d3b6f6e balrog
#endif
8253 a171fe39 balrog
            case QEMU_OPTION_portrait:
8254 a171fe39 balrog
                graphic_rotate = 1;
8255 a171fe39 balrog
                break;
8256 cd6f1169 bellard
            case QEMU_OPTION_kernel:
8257 a20dd508 bellard
                kernel_filename = optarg;
8258 a20dd508 bellard
                break;
8259 cd6f1169 bellard
            case QEMU_OPTION_append:
8260 a20dd508 bellard
                kernel_cmdline = optarg;
8261 313aa567 bellard
                break;
8262 cd6f1169 bellard
            case QEMU_OPTION_cdrom:
8263 609497ab balrog
                drive_add(optarg, CDROM_ALIAS);
8264 36b486bb bellard
                break;
8265 cd6f1169 bellard
            case QEMU_OPTION_boot:
8266 28c5af54 j_mayer
                boot_devices = optarg;
8267 28c5af54 j_mayer
                /* We just do some generic consistency checks */
8268 28c5af54 j_mayer
                {
8269 28c5af54 j_mayer
                    /* Could easily be extended to 64 devices if needed */
8270 60fe76f3 ths
                    const char *p;
8271 28c5af54 j_mayer
                    
8272 28c5af54 j_mayer
                    boot_devices_bitmap = 0;
8273 28c5af54 j_mayer
                    for (p = boot_devices; *p != '\0'; p++) {
8274 28c5af54 j_mayer
                        /* Allowed boot devices are:
8275 28c5af54 j_mayer
                         * a b     : floppy disk drives
8276 28c5af54 j_mayer
                         * c ... f : IDE disk drives
8277 28c5af54 j_mayer
                         * g ... m : machine implementation dependant drives
8278 28c5af54 j_mayer
                         * n ... p : network devices
8279 28c5af54 j_mayer
                         * It's up to each machine implementation to check
8280 28c5af54 j_mayer
                         * if the given boot devices match the actual hardware
8281 28c5af54 j_mayer
                         * implementation and firmware features.
8282 28c5af54 j_mayer
                         */
8283 28c5af54 j_mayer
                        if (*p < 'a' || *p > 'q') {
8284 28c5af54 j_mayer
                            fprintf(stderr, "Invalid boot device '%c'\n", *p);
8285 28c5af54 j_mayer
                            exit(1);
8286 28c5af54 j_mayer
                        }
8287 28c5af54 j_mayer
                        if (boot_devices_bitmap & (1 << (*p - 'a'))) {
8288 28c5af54 j_mayer
                            fprintf(stderr,
8289 28c5af54 j_mayer
                                    "Boot device '%c' was given twice\n",*p);
8290 28c5af54 j_mayer
                            exit(1);
8291 28c5af54 j_mayer
                        }
8292 28c5af54 j_mayer
                        boot_devices_bitmap |= 1 << (*p - 'a');
8293 28c5af54 j_mayer
                    }
8294 36b486bb bellard
                }
8295 36b486bb bellard
                break;
8296 cd6f1169 bellard
            case QEMU_OPTION_fda:
8297 cd6f1169 bellard
            case QEMU_OPTION_fdb:
8298 609497ab balrog
                drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda);
8299 c45886db bellard
                break;
8300 52ca8d6a bellard
#ifdef TARGET_I386
8301 52ca8d6a bellard
            case QEMU_OPTION_no_fd_bootchk:
8302 52ca8d6a bellard
                fd_bootchk = 0;
8303 52ca8d6a bellard
                break;
8304 52ca8d6a bellard
#endif
8305 7c9d8e07 bellard
            case QEMU_OPTION_net:
8306 7c9d8e07 bellard
                if (nb_net_clients >= MAX_NET_CLIENTS) {
8307 7c9d8e07 bellard
                    fprintf(stderr, "qemu: too many network clients\n");
8308 c4b1fcc0 bellard
                    exit(1);
8309 c4b1fcc0 bellard
                }
8310 fd5f393a pbrook
                net_clients[nb_net_clients] = optarg;
8311 7c9d8e07 bellard
                nb_net_clients++;
8312 702c651c bellard
                break;
8313 c7f74643 bellard
#ifdef CONFIG_SLIRP
8314 c7f74643 bellard
            case QEMU_OPTION_tftp:
8315 c7f74643 bellard
                tftp_prefix = optarg;
8316 9bf05444 bellard
                break;
8317 47d5d01a ths
            case QEMU_OPTION_bootp:
8318 47d5d01a ths
                bootp_filename = optarg;
8319 47d5d01a ths
                break;
8320 c94c8d64 bellard
#ifndef _WIN32
8321 9d728e8c bellard
            case QEMU_OPTION_smb:
8322 9d728e8c bellard
                net_slirp_smb(optarg);
8323 9d728e8c bellard
                break;
8324 c94c8d64 bellard
#endif
8325 9bf05444 bellard
            case QEMU_OPTION_redir:
8326 3b46e624 ths
                net_slirp_redir(optarg);
8327 9bf05444 bellard
                break;
8328 c7f74643 bellard
#endif
8329 1d14ffa9 bellard
#ifdef HAS_AUDIO
8330 1d14ffa9 bellard
            case QEMU_OPTION_audio_help:
8331 1d14ffa9 bellard
                AUD_help ();
8332 1d14ffa9 bellard
                exit (0);
8333 1d14ffa9 bellard
                break;
8334 1d14ffa9 bellard
            case QEMU_OPTION_soundhw:
8335 1d14ffa9 bellard
                select_soundhw (optarg);
8336 1d14ffa9 bellard
                break;
8337 1d14ffa9 bellard
#endif
8338 cd6f1169 bellard
            case QEMU_OPTION_h:
8339 15f82208 ths
                help(0);
8340 cd6f1169 bellard
                break;
8341 00f82b8a aurel32
            case QEMU_OPTION_m: {
8342 00f82b8a aurel32
                uint64_t value;
8343 00f82b8a aurel32
                char *ptr;
8344 00f82b8a aurel32
8345 00f82b8a aurel32
                value = strtoul(optarg, &ptr, 10);
8346 00f82b8a aurel32
                switch (*ptr) {
8347 00f82b8a aurel32
                case 0: case 'M': case 'm':
8348 00f82b8a aurel32
                    value <<= 20;
8349 00f82b8a aurel32
                    break;
8350 00f82b8a aurel32
                case 'G': case 'g':
8351 00f82b8a aurel32
                    value <<= 30;
8352 00f82b8a aurel32
                    break;
8353 00f82b8a aurel32
                default:
8354 00f82b8a aurel32
                    fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
8355 cd6f1169 bellard
                    exit(1);
8356 cd6f1169 bellard
                }
8357 00f82b8a aurel32
8358 00f82b8a aurel32
                /* On 32-bit hosts, QEMU is limited by virtual address space */
8359 00f82b8a aurel32
                if (value > (2047 << 20)
8360 00f82b8a aurel32
#ifndef USE_KQEMU
8361 00f82b8a aurel32
                    && HOST_LONG_BITS == 32
8362 00f82b8a aurel32
#endif
8363 00f82b8a aurel32
                    ) {
8364 00f82b8a aurel32
                    fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
8365 00f82b8a aurel32
                    exit(1);
8366 00f82b8a aurel32
                }
8367 00f82b8a aurel32
                if (value != (uint64_t)(ram_addr_t)value) {
8368 00f82b8a aurel32
                    fprintf(stderr, "qemu: ram size too large\n");
8369 00f82b8a aurel32
                    exit(1);
8370 00f82b8a aurel32
                }
8371 00f82b8a aurel32
                ram_size = value;
8372 cd6f1169 bellard
                break;
8373 00f82b8a aurel32
            }
8374 cd6f1169 bellard
            case QEMU_OPTION_d:
8375 cd6f1169 bellard
                {
8376 cd6f1169 bellard
                    int mask;
8377 cd6f1169 bellard
                    CPULogItem *item;
8378 3b46e624 ths
8379 cd6f1169 bellard
                    mask = cpu_str_to_log_mask(optarg);
8380 cd6f1169 bellard
                    if (!mask) {
8381 cd6f1169 bellard
                        printf("Log items (comma separated):\n");
8382 f193c797 bellard
                    for(item = cpu_log_items; item->mask != 0; item++) {
8383 f193c797 bellard
                        printf("%-10s %s\n", item->name, item->help);
8384 f193c797 bellard
                    }
8385 f193c797 bellard
                    exit(1);
8386 cd6f1169 bellard
                    }
8387 cd6f1169 bellard
                    cpu_set_log(mask);
8388 f193c797 bellard
                }
8389 cd6f1169 bellard
                break;
8390 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
8391 cd6f1169 bellard
            case QEMU_OPTION_s:
8392 cd6f1169 bellard
                use_gdbstub = 1;
8393 cd6f1169 bellard
                break;
8394 cd6f1169 bellard
            case QEMU_OPTION_p:
8395 cfc3475a pbrook
                gdbstub_port = optarg;
8396 cd6f1169 bellard
                break;
8397 67b915a5 bellard
#endif
8398 cd6f1169 bellard
            case QEMU_OPTION_L:
8399 cd6f1169 bellard
                bios_dir = optarg;
8400 cd6f1169 bellard
                break;
8401 1192dad8 j_mayer
            case QEMU_OPTION_bios:
8402 1192dad8 j_mayer
                bios_name = optarg;
8403 1192dad8 j_mayer
                break;
8404 cd6f1169 bellard
            case QEMU_OPTION_S:
8405 3c07f8e8 pbrook
                autostart = 0;
8406 cd6f1169 bellard
                break;
8407 3d11d0eb bellard
            case QEMU_OPTION_k:
8408 3d11d0eb bellard
                keyboard_layout = optarg;
8409 3d11d0eb bellard
                break;
8410 ee22c2f7 bellard
            case QEMU_OPTION_localtime:
8411 ee22c2f7 bellard
                rtc_utc = 0;
8412 ee22c2f7 bellard
                break;
8413 1f04275e bellard
            case QEMU_OPTION_cirrusvga:
8414 1f04275e bellard
                cirrus_vga_enabled = 1;
8415 d34cab9f ths
                vmsvga_enabled = 0;
8416 d34cab9f ths
                break;
8417 d34cab9f ths
            case QEMU_OPTION_vmsvga:
8418 d34cab9f ths
                cirrus_vga_enabled = 0;
8419 d34cab9f ths
                vmsvga_enabled = 1;
8420 1f04275e bellard
                break;
8421 1bfe856e bellard
            case QEMU_OPTION_std_vga:
8422 1bfe856e bellard
                cirrus_vga_enabled = 0;
8423 d34cab9f ths
                vmsvga_enabled = 0;
8424 1bfe856e bellard
                break;
8425 e9b137c2 bellard
            case QEMU_OPTION_g:
8426 e9b137c2 bellard
                {
8427 e9b137c2 bellard
                    const char *p;
8428 e9b137c2 bellard
                    int w, h, depth;
8429 e9b137c2 bellard
                    p = optarg;
8430 e9b137c2 bellard
                    w = strtol(p, (char **)&p, 10);
8431 e9b137c2 bellard
                    if (w <= 0) {
8432 e9b137c2 bellard
                    graphic_error:
8433 e9b137c2 bellard
                        fprintf(stderr, "qemu: invalid resolution or depth\n");
8434 e9b137c2 bellard
                        exit(1);
8435 e9b137c2 bellard
                    }
8436 e9b137c2 bellard
                    if (*p != 'x')
8437 e9b137c2 bellard
                        goto graphic_error;
8438 e9b137c2 bellard
                    p++;
8439 e9b137c2 bellard
                    h = strtol(p, (char **)&p, 10);
8440 e9b137c2 bellard
                    if (h <= 0)
8441 e9b137c2 bellard
                        goto graphic_error;
8442 e9b137c2 bellard
                    if (*p == 'x') {
8443 e9b137c2 bellard
                        p++;
8444 e9b137c2 bellard
                        depth = strtol(p, (char **)&p, 10);
8445 5fafdf24 ths
                        if (depth != 8 && depth != 15 && depth != 16 &&
8446 e9b137c2 bellard
                            depth != 24 && depth != 32)
8447 e9b137c2 bellard
                            goto graphic_error;
8448 e9b137c2 bellard
                    } else if (*p == '\0') {
8449 e9b137c2 bellard
                        depth = graphic_depth;
8450 e9b137c2 bellard
                    } else {
8451 e9b137c2 bellard
                        goto graphic_error;
8452 e9b137c2 bellard
                    }
8453 3b46e624 ths
8454 e9b137c2 bellard
                    graphic_width = w;
8455 e9b137c2 bellard
                    graphic_height = h;
8456 e9b137c2 bellard
                    graphic_depth = depth;
8457 e9b137c2 bellard
                }
8458 e9b137c2 bellard
                break;
8459 20d8a3ed ths
            case QEMU_OPTION_echr:
8460 20d8a3ed ths
                {
8461 20d8a3ed ths
                    char *r;
8462 20d8a3ed ths
                    term_escape_char = strtol(optarg, &r, 0);
8463 20d8a3ed ths
                    if (r == optarg)
8464 20d8a3ed ths
                        printf("Bad argument to echr\n");
8465 20d8a3ed ths
                    break;
8466 20d8a3ed ths
                }
8467 82c643ff bellard
            case QEMU_OPTION_monitor:
8468 fd5f393a pbrook
                monitor_device = optarg;
8469 82c643ff bellard
                break;
8470 82c643ff bellard
            case QEMU_OPTION_serial:
8471 8d11df9e bellard
                if (serial_device_index >= MAX_SERIAL_PORTS) {
8472 8d11df9e bellard
                    fprintf(stderr, "qemu: too many serial ports\n");
8473 8d11df9e bellard
                    exit(1);
8474 8d11df9e bellard
                }
8475 fd5f393a pbrook
                serial_devices[serial_device_index] = optarg;
8476 8d11df9e bellard
                serial_device_index++;
8477 82c643ff bellard
                break;
8478 6508fe59 bellard
            case QEMU_OPTION_parallel:
8479 6508fe59 bellard
                if (parallel_device_index >= MAX_PARALLEL_PORTS) {
8480 6508fe59 bellard
                    fprintf(stderr, "qemu: too many parallel ports\n");
8481 6508fe59 bellard
                    exit(1);
8482 6508fe59 bellard
                }
8483 fd5f393a pbrook
                parallel_devices[parallel_device_index] = optarg;
8484 6508fe59 bellard
                parallel_device_index++;
8485 6508fe59 bellard
                break;
8486 d63d307f bellard
            case QEMU_OPTION_loadvm:
8487 d63d307f bellard
                loadvm = optarg;
8488 d63d307f bellard
                break;
8489 d63d307f bellard
            case QEMU_OPTION_full_screen:
8490 d63d307f bellard
                full_screen = 1;
8491 d63d307f bellard
                break;
8492 667accab ths
#ifdef CONFIG_SDL
8493 43523e93 ths
            case QEMU_OPTION_no_frame:
8494 43523e93 ths
                no_frame = 1;
8495 43523e93 ths
                break;
8496 3780e197 ths
            case QEMU_OPTION_alt_grab:
8497 3780e197 ths
                alt_grab = 1;
8498 3780e197 ths
                break;
8499 667accab ths
            case QEMU_OPTION_no_quit:
8500 667accab ths
                no_quit = 1;
8501 667accab ths
                break;
8502 667accab ths
#endif
8503 f7cce898 bellard
            case QEMU_OPTION_pidfile:
8504 93815bc2 ths
                pid_file = optarg;
8505 f7cce898 bellard
                break;
8506 a09db21f bellard
#ifdef TARGET_I386
8507 a09db21f bellard
            case QEMU_OPTION_win2k_hack:
8508 a09db21f bellard
                win2k_install_hack = 1;
8509 a09db21f bellard
                break;
8510 a09db21f bellard
#endif
8511 d993e026 bellard
#ifdef USE_KQEMU
8512 d993e026 bellard
            case QEMU_OPTION_no_kqemu:
8513 d993e026 bellard
                kqemu_allowed = 0;
8514 d993e026 bellard
                break;
8515 89bfc105 bellard
            case QEMU_OPTION_kernel_kqemu:
8516 89bfc105 bellard
                kqemu_allowed = 2;
8517 89bfc105 bellard
                break;
8518 d993e026 bellard
#endif
8519 bb36d470 bellard
            case QEMU_OPTION_usb:
8520 bb36d470 bellard
                usb_enabled = 1;
8521 bb36d470 bellard
                break;
8522 a594cfbf bellard
            case QEMU_OPTION_usbdevice:
8523 a594cfbf bellard
                usb_enabled = 1;
8524 0d92ed30 pbrook
                if (usb_devices_index >= MAX_USB_CMDLINE) {
8525 a594cfbf bellard
                    fprintf(stderr, "Too many USB devices\n");
8526 a594cfbf bellard
                    exit(1);
8527 a594cfbf bellard
                }
8528 fd5f393a pbrook
                usb_devices[usb_devices_index] = optarg;
8529 a594cfbf bellard
                usb_devices_index++;
8530 a594cfbf bellard
                break;
8531 6a00d601 bellard
            case QEMU_OPTION_smp:
8532 6a00d601 bellard
                smp_cpus = atoi(optarg);
8533 ba3c64fb bellard
                if (smp_cpus < 1 || smp_cpus > MAX_CPUS) {
8534 6a00d601 bellard
                    fprintf(stderr, "Invalid number of CPUs\n");
8535 6a00d601 bellard
                    exit(1);
8536 6a00d601 bellard
                }
8537 6a00d601 bellard
                break;
8538 24236869 bellard
            case QEMU_OPTION_vnc:
8539 73fc9742 ths
                vnc_display = optarg;
8540 24236869 bellard
                break;
8541 6515b203 bellard
            case QEMU_OPTION_no_acpi:
8542 6515b203 bellard
                acpi_enabled = 0;
8543 6515b203 bellard
                break;
8544 d1beab82 bellard
            case QEMU_OPTION_no_reboot:
8545 d1beab82 bellard
                no_reboot = 1;
8546 d1beab82 bellard
                break;
8547 b2f76161 aurel32
            case QEMU_OPTION_no_shutdown:
8548 b2f76161 aurel32
                no_shutdown = 1;
8549 b2f76161 aurel32
                break;
8550 9467cd46 balrog
            case QEMU_OPTION_show_cursor:
8551 9467cd46 balrog
                cursor_hide = 0;
8552 9467cd46 balrog
                break;
8553 71e3ceb8 ths
            case QEMU_OPTION_daemonize:
8554 71e3ceb8 ths
                daemonize = 1;
8555 71e3ceb8 ths
                break;
8556 9ae02555 ths
            case QEMU_OPTION_option_rom:
8557 9ae02555 ths
                if (nb_option_roms >= MAX_OPTION_ROMS) {
8558 9ae02555 ths
                    fprintf(stderr, "Too many option ROMs\n");
8559 9ae02555 ths
                    exit(1);
8560 9ae02555 ths
                }
8561 9ae02555 ths
                option_rom[nb_option_roms] = optarg;
8562 9ae02555 ths
                nb_option_roms++;
8563 9ae02555 ths
                break;
8564 8e71621f pbrook
            case QEMU_OPTION_semihosting:
8565 8e71621f pbrook
                semihosting_enabled = 1;
8566 8e71621f pbrook
                break;
8567 c35734b2 ths
            case QEMU_OPTION_name:
8568 c35734b2 ths
                qemu_name = optarg;
8569 c35734b2 ths
                break;
8570 66508601 blueswir1
#ifdef TARGET_SPARC
8571 66508601 blueswir1
            case QEMU_OPTION_prom_env:
8572 66508601 blueswir1
                if (nb_prom_envs >= MAX_PROM_ENVS) {
8573 66508601 blueswir1
                    fprintf(stderr, "Too many prom variables\n");
8574 66508601 blueswir1
                    exit(1);
8575 66508601 blueswir1
                }
8576 66508601 blueswir1
                prom_envs[nb_prom_envs] = optarg;
8577 66508601 blueswir1
                nb_prom_envs++;
8578 66508601 blueswir1
                break;
8579 66508601 blueswir1
#endif
8580 2b8f2d41 balrog
#ifdef TARGET_ARM
8581 2b8f2d41 balrog
            case QEMU_OPTION_old_param:
8582 2b8f2d41 balrog
                old_param = 1;
8583 05ebd537 ths
                break;
8584 2b8f2d41 balrog
#endif
8585 f3dcfada ths
            case QEMU_OPTION_clock:
8586 f3dcfada ths
                configure_alarms(optarg);
8587 f3dcfada ths
                break;
8588 7e0af5d0 bellard
            case QEMU_OPTION_startdate:
8589 7e0af5d0 bellard
                {
8590 7e0af5d0 bellard
                    struct tm tm;
8591 f6503059 balrog
                    time_t rtc_start_date;
8592 7e0af5d0 bellard
                    if (!strcmp(optarg, "now")) {
8593 f6503059 balrog
                        rtc_date_offset = -1;
8594 7e0af5d0 bellard
                    } else {
8595 7e0af5d0 bellard
                        if (sscanf(optarg, "%d-%d-%dT%d:%d:%d",
8596 7e0af5d0 bellard
                               &tm.tm_year,
8597 7e0af5d0 bellard
                               &tm.tm_mon,
8598 7e0af5d0 bellard
                               &tm.tm_mday,
8599 7e0af5d0 bellard
                               &tm.tm_hour,
8600 7e0af5d0 bellard
                               &tm.tm_min,
8601 7e0af5d0 bellard
                               &tm.tm_sec) == 6) {
8602 7e0af5d0 bellard
                            /* OK */
8603 7e0af5d0 bellard
                        } else if (sscanf(optarg, "%d-%d-%d",
8604 7e0af5d0 bellard
                                          &tm.tm_year,
8605 7e0af5d0 bellard
                                          &tm.tm_mon,
8606 7e0af5d0 bellard
                                          &tm.tm_mday) == 3) {
8607 7e0af5d0 bellard
                            tm.tm_hour = 0;
8608 7e0af5d0 bellard
                            tm.tm_min = 0;
8609 7e0af5d0 bellard
                            tm.tm_sec = 0;
8610 7e0af5d0 bellard
                        } else {
8611 7e0af5d0 bellard
                            goto date_fail;
8612 7e0af5d0 bellard
                        }
8613 7e0af5d0 bellard
                        tm.tm_year -= 1900;
8614 7e0af5d0 bellard
                        tm.tm_mon--;
8615 3c6b2088 bellard
                        rtc_start_date = mktimegm(&tm);
8616 7e0af5d0 bellard
                        if (rtc_start_date == -1) {
8617 7e0af5d0 bellard
                        date_fail:
8618 7e0af5d0 bellard
                            fprintf(stderr, "Invalid date format. Valid format are:\n"
8619 7e0af5d0 bellard
                                    "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
8620 7e0af5d0 bellard
                            exit(1);
8621 7e0af5d0 bellard
                        }
8622 f6503059 balrog
                        rtc_date_offset = time(NULL) - rtc_start_date;
8623 7e0af5d0 bellard
                    }
8624 7e0af5d0 bellard
                }
8625 7e0af5d0 bellard
                break;
8626 26a5f13b bellard
            case QEMU_OPTION_tb_size:
8627 26a5f13b bellard
                tb_size = strtol(optarg, NULL, 0);
8628 26a5f13b bellard
                if (tb_size < 0)
8629 26a5f13b bellard
                    tb_size = 0;
8630 26a5f13b bellard
                break;
8631 2e70f6ef pbrook
            case QEMU_OPTION_icount:
8632 2e70f6ef pbrook
                use_icount = 1;
8633 2e70f6ef pbrook
                if (strcmp(optarg, "auto") == 0) {
8634 2e70f6ef pbrook
                    icount_time_shift = -1;
8635 2e70f6ef pbrook
                } else {
8636 2e70f6ef pbrook
                    icount_time_shift = strtol(optarg, NULL, 0);
8637 2e70f6ef pbrook
                }
8638 2e70f6ef pbrook
                break;
8639 cd6f1169 bellard
            }
8640 0824d6fc bellard
        }
8641 0824d6fc bellard
    }
8642 330d0414 bellard
8643 71e3ceb8 ths
#ifndef _WIN32
8644 71e3ceb8 ths
    if (daemonize) {
8645 71e3ceb8 ths
        pid_t pid;
8646 71e3ceb8 ths
8647 71e3ceb8 ths
        if (pipe(fds) == -1)
8648 71e3ceb8 ths
            exit(1);
8649 71e3ceb8 ths
8650 71e3ceb8 ths
        pid = fork();
8651 71e3ceb8 ths
        if (pid > 0) {
8652 71e3ceb8 ths
            uint8_t status;
8653 71e3ceb8 ths
            ssize_t len;
8654 71e3ceb8 ths
8655 71e3ceb8 ths
            close(fds[1]);
8656 71e3ceb8 ths
8657 71e3ceb8 ths
        again:
8658 93815bc2 ths
            len = read(fds[0], &status, 1);
8659 93815bc2 ths
            if (len == -1 && (errno == EINTR))
8660 93815bc2 ths
                goto again;
8661 93815bc2 ths
8662 93815bc2 ths
            if (len != 1)
8663 93815bc2 ths
                exit(1);
8664 93815bc2 ths
            else if (status == 1) {
8665 93815bc2 ths
                fprintf(stderr, "Could not acquire pidfile\n");
8666 93815bc2 ths
                exit(1);
8667 93815bc2 ths
            } else
8668 93815bc2 ths
                exit(0);
8669 71e3ceb8 ths
        } else if (pid < 0)
8670 93815bc2 ths
            exit(1);
8671 71e3ceb8 ths
8672 71e3ceb8 ths
        setsid();
8673 71e3ceb8 ths
8674 71e3ceb8 ths
        pid = fork();
8675 71e3ceb8 ths
        if (pid > 0)
8676 71e3ceb8 ths
            exit(0);
8677 71e3ceb8 ths
        else if (pid < 0)
8678 71e3ceb8 ths
            exit(1);
8679 71e3ceb8 ths
8680 71e3ceb8 ths
        umask(027);
8681 71e3ceb8 ths
8682 71e3ceb8 ths
        signal(SIGTSTP, SIG_IGN);
8683 71e3ceb8 ths
        signal(SIGTTOU, SIG_IGN);
8684 71e3ceb8 ths
        signal(SIGTTIN, SIG_IGN);
8685 71e3ceb8 ths
    }
8686 71e3ceb8 ths
#endif
8687 71e3ceb8 ths
8688 aa26bb2d ths
    if (pid_file && qemu_create_pidfile(pid_file) != 0) {
8689 93815bc2 ths
        if (daemonize) {
8690 93815bc2 ths
            uint8_t status = 1;
8691 93815bc2 ths
            write(fds[1], &status, 1);
8692 93815bc2 ths
        } else
8693 93815bc2 ths
            fprintf(stderr, "Could not acquire pid file\n");
8694 93815bc2 ths
        exit(1);
8695 93815bc2 ths
    }
8696 93815bc2 ths
8697 ff3fbb30 bellard
#ifdef USE_KQEMU
8698 ff3fbb30 bellard
    if (smp_cpus > 1)
8699 ff3fbb30 bellard
        kqemu_allowed = 0;
8700 ff3fbb30 bellard
#endif
8701 a20dd508 bellard
    linux_boot = (kernel_filename != NULL);
8702 7317b8ca balrog
    net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF;
8703 6c41b272 balrog
8704 28c5af54 j_mayer
    /* XXX: this should not be: some embedded targets just have flash */
8705 28c5af54 j_mayer
    if (!linux_boot && net_boot == 0 &&
8706 e4bcb14c ths
        nb_drives_opt == 0)
8707 15f82208 ths
        help(1);
8708 0824d6fc bellard
8709 f8d39c01 ths
    if (!linux_boot && *kernel_cmdline != '\0') {
8710 f8d39c01 ths
        fprintf(stderr, "-append only allowed with -kernel option\n");
8711 f8d39c01 ths
        exit(1);
8712 f8d39c01 ths
    }
8713 f8d39c01 ths
8714 f8d39c01 ths
    if (!linux_boot && initrd_filename != NULL) {
8715 f8d39c01 ths
        fprintf(stderr, "-initrd only allowed with -kernel option\n");
8716 f8d39c01 ths
        exit(1);
8717 f8d39c01 ths
    }
8718 f8d39c01 ths
8719 96d30e48 ths
    /* boot to floppy or the default cd if no hard disk defined yet */
8720 28c5af54 j_mayer
    if (!boot_devices[0]) {
8721 e4bcb14c ths
        boot_devices = "cad";
8722 96d30e48 ths
    }
8723 b118d61e bellard
    setvbuf(stdout, NULL, _IOLBF, 0);
8724 3b46e624 ths
8725 634fce96 pbrook
    init_timers();
8726 634fce96 pbrook
    init_timer_alarm();
8727 83f64091 bellard
    qemu_aio_init();
8728 2e70f6ef pbrook
    if (use_icount && icount_time_shift < 0) {
8729 2e70f6ef pbrook
        use_icount = 2;
8730 2e70f6ef pbrook
        /* 125MIPS seems a reasonable initial guess at the guest speed.
8731 2e70f6ef pbrook
           It will be corrected fairly quickly anyway.  */
8732 2e70f6ef pbrook
        icount_time_shift = 3;
8733 2e70f6ef pbrook
        init_icount_adjust();
8734 2e70f6ef pbrook
    }
8735 634fce96 pbrook
8736 fd1dff4b bellard
#ifdef _WIN32
8737 fd1dff4b bellard
    socket_init();
8738 fd1dff4b bellard
#endif
8739 fd1dff4b bellard
8740 7c9d8e07 bellard
    /* init network clients */
8741 7c9d8e07 bellard
    if (nb_net_clients == 0) {
8742 7c9d8e07 bellard
        /* if no clients, we use a default config */
8743 fd5f393a pbrook
        net_clients[0] = "nic";
8744 fd5f393a pbrook
        net_clients[1] = "user";
8745 7c9d8e07 bellard
        nb_net_clients = 2;
8746 c20709aa bellard
    }
8747 c20709aa bellard
8748 7c9d8e07 bellard
    for(i = 0;i < nb_net_clients; i++) {
8749 7c9d8e07 bellard
        if (net_client_init(net_clients[i]) < 0)
8750 7c9d8e07 bellard
            exit(1);
8751 702c651c bellard
    }
8752 833c7174 blueswir1
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
8753 833c7174 blueswir1
        if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
8754 833c7174 blueswir1
            continue;
8755 833c7174 blueswir1
        if (vlan->nb_guest_devs == 0) {
8756 833c7174 blueswir1
            fprintf(stderr, "Invalid vlan (%d) with no nics\n", vlan->id);
8757 833c7174 blueswir1
            exit(1);
8758 833c7174 blueswir1
        }
8759 833c7174 blueswir1
        if (vlan->nb_host_devs == 0)
8760 833c7174 blueswir1
            fprintf(stderr,
8761 833c7174 blueswir1
                    "Warning: vlan %d is not connected to host network\n",
8762 833c7174 blueswir1
                    vlan->id);
8763 833c7174 blueswir1
    }
8764 f1510b2c bellard
8765 eec85c2a ths
#ifdef TARGET_I386
8766 ed494d87 balrog
    /* XXX: this should be moved in the PC machine instantiation code */
8767 28c5af54 j_mayer
    if (net_boot != 0) {
8768 28c5af54 j_mayer
        int netroms = 0;
8769 28c5af54 j_mayer
        for (i = 0; i < nb_nics && i < 4; i++) {
8770 eec85c2a ths
            const char *model = nd_table[i].model;
8771 eec85c2a ths
            char buf[1024];
8772 28c5af54 j_mayer
            if (net_boot & (1 << i)) {
8773 28c5af54 j_mayer
                if (model == NULL)
8774 28c5af54 j_mayer
                    model = "ne2k_pci";
8775 28c5af54 j_mayer
                snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
8776 28c5af54 j_mayer
                if (get_image_size(buf) > 0) {
8777 28c5af54 j_mayer
                    if (nb_option_roms >= MAX_OPTION_ROMS) {
8778 28c5af54 j_mayer
                        fprintf(stderr, "Too many option ROMs\n");
8779 28c5af54 j_mayer
                        exit(1);
8780 28c5af54 j_mayer
                    }
8781 28c5af54 j_mayer
                    option_rom[nb_option_roms] = strdup(buf);
8782 28c5af54 j_mayer
                    nb_option_roms++;
8783 28c5af54 j_mayer
                    netroms++;
8784 28c5af54 j_mayer
                }
8785 28c5af54 j_mayer
            }
8786 eec85c2a ths
        }
8787 28c5af54 j_mayer
        if (netroms == 0) {
8788 eec85c2a ths
            fprintf(stderr, "No valid PXE rom found for network device\n");
8789 eec85c2a ths
            exit(1);
8790 eec85c2a ths
        }
8791 eec85c2a ths
    }
8792 eec85c2a ths
#endif
8793 eec85c2a ths
8794 0824d6fc bellard
    /* init the memory */
8795 7fb4fdcf balrog
    phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED;
8796 7fb4fdcf balrog
8797 7fb4fdcf balrog
    if (machine->ram_require & RAMSIZE_FIXED) {
8798 7fb4fdcf balrog
        if (ram_size > 0) {
8799 7fb4fdcf balrog
            if (ram_size < phys_ram_size) {
8800 cd940061 aurel32
                fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
8801 cd940061 aurel32
                                machine->name, (unsigned long long) phys_ram_size);
8802 7fb4fdcf balrog
                exit(-1);
8803 7fb4fdcf balrog
            }
8804 7fb4fdcf balrog
8805 7fb4fdcf balrog
            phys_ram_size = ram_size;
8806 7fb4fdcf balrog
        } else
8807 7fb4fdcf balrog
            ram_size = phys_ram_size;
8808 7fb4fdcf balrog
    } else {
8809 4fc5d071 aurel32
        if (ram_size == 0)
8810 7fb4fdcf balrog
            ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
8811 7fb4fdcf balrog
8812 7fb4fdcf balrog
        phys_ram_size += ram_size;
8813 7fb4fdcf balrog
    }
8814 9ae02555 ths
8815 d993e026 bellard
    phys_ram_base = qemu_vmalloc(phys_ram_size);
8816 7f7f9873 bellard
    if (!phys_ram_base) {
8817 7f7f9873 bellard
        fprintf(stderr, "Could not allocate physical memory\n");
8818 0824d6fc bellard
        exit(1);
8819 0824d6fc bellard
    }
8820 0824d6fc bellard
8821 26a5f13b bellard
    /* init the dynamic translator */
8822 26a5f13b bellard
    cpu_exec_init_all(tb_size * 1024 * 1024);
8823 26a5f13b bellard
8824 5905b2e5 bellard
    bdrv_init();
8825 c4b1fcc0 bellard
8826 e4bcb14c ths
    /* we always create the cdrom drive, even if no disk is there */
8827 c4b1fcc0 bellard
8828 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
8829 609497ab balrog
        drive_add(NULL, CDROM_ALIAS);
8830 c4b1fcc0 bellard
8831 9d413d1d balrog
    /* we always create at least one floppy */
8832 33e3963e bellard
8833 e4bcb14c ths
    if (nb_drives_opt < MAX_DRIVES)
8834 609497ab balrog
        drive_add(NULL, FD_ALIAS, 0);
8835 86f55663 j_mayer
8836 9d413d1d balrog
    /* we always create one sd slot, even if no card is in it */
8837 9d413d1d balrog
8838 9d413d1d balrog
    if (nb_drives_opt < MAX_DRIVES)
8839 609497ab balrog
        drive_add(NULL, SD_ALIAS);
8840 9d413d1d balrog
8841 e4bcb14c ths
    /* open the virtual block devices */
8842 e4bcb14c ths
8843 e4bcb14c ths
    for(i = 0; i < nb_drives_opt; i++)
8844 609497ab balrog
        if (drive_init(&drives_opt[i], snapshot, machine) == -1)
8845 e4bcb14c ths
            exit(1);
8846 3e3d5815 balrog
8847 c88676f8 bellard
    register_savevm("timer", 0, 2, timer_save, timer_load, NULL);
8848 c88676f8 bellard
    register_savevm("ram", 0, 2, ram_save, ram_load, NULL);
8849 8a7ddc38 bellard
8850 313aa567 bellard
    /* terminal init */
8851 740733bb ths
    memset(&display_state, 0, sizeof(display_state));
8852 a20dd508 bellard
    if (nographic) {
8853 4d3b6f6e balrog
        if (curses) {
8854 4d3b6f6e balrog
            fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
8855 4d3b6f6e balrog
            exit(1);
8856 4d3b6f6e balrog
        }
8857 2ff89790 ths
        /* nearly nothing to do */
8858 2ff89790 ths
        dumb_display_init(ds);
8859 73fc9742 ths
    } else if (vnc_display != NULL) {
8860 71cab5ca ths
        vnc_display_init(ds);
8861 71cab5ca ths
        if (vnc_display_open(ds, vnc_display) < 0)
8862 71cab5ca ths
            exit(1);
8863 4d3b6f6e balrog
    } else
8864 4d3b6f6e balrog
#if defined(CONFIG_CURSES)
8865 4d3b6f6e balrog
    if (curses) {
8866 4d3b6f6e balrog
        curses_display_init(ds, full_screen);
8867 4d3b6f6e balrog
    } else
8868 4d3b6f6e balrog
#endif
8869 4d3b6f6e balrog
    {
8870 5b0753e0 bellard
#if defined(CONFIG_SDL)
8871 43523e93 ths
        sdl_display_init(ds, full_screen, no_frame);
8872 5b0753e0 bellard
#elif defined(CONFIG_COCOA)
8873 5b0753e0 bellard
        cocoa_display_init(ds, full_screen);
8874 67276f53 pbrook
#else
8875 67276f53 pbrook
        dumb_display_init(ds);
8876 313aa567 bellard
#endif
8877 313aa567 bellard
    }
8878 0824d6fc bellard
8879 20d8a3ed ths
    /* Maintain compatibility with multiple stdio monitors */
8880 20d8a3ed ths
    if (!strcmp(monitor_device,"stdio")) {
8881 20d8a3ed ths
        for (i = 0; i < MAX_SERIAL_PORTS; i++) {
8882 fd5f393a pbrook
            const char *devname = serial_devices[i];
8883 fd5f393a pbrook
            if (devname && !strcmp(devname,"mon:stdio")) {
8884 fd5f393a pbrook
                monitor_device = NULL;
8885 20d8a3ed ths
                break;
8886 fd5f393a pbrook
            } else if (devname && !strcmp(devname,"stdio")) {
8887 fd5f393a pbrook
                monitor_device = NULL;
8888 fd5f393a pbrook
                serial_devices[i] = "mon:stdio";
8889 20d8a3ed ths
                break;
8890 20d8a3ed ths
            }
8891 20d8a3ed ths
        }
8892 20d8a3ed ths
    }
8893 fd5f393a pbrook
    if (monitor_device) {
8894 20d8a3ed ths
        monitor_hd = qemu_chr_open(monitor_device);
8895 20d8a3ed ths
        if (!monitor_hd) {
8896 20d8a3ed ths
            fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
8897 20d8a3ed ths
            exit(1);
8898 20d8a3ed ths
        }
8899 20d8a3ed ths
        monitor_init(monitor_hd, !nographic);
8900 82c643ff bellard
    }
8901 82c643ff bellard
8902 8d11df9e bellard
    for(i = 0; i < MAX_SERIAL_PORTS; i++) {
8903 c03b0f0f bellard
        const char *devname = serial_devices[i];
8904 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
8905 c03b0f0f bellard
            serial_hds[i] = qemu_chr_open(devname);
8906 8d11df9e bellard
            if (!serial_hds[i]) {
8907 5fafdf24 ths
                fprintf(stderr, "qemu: could not open serial device '%s'\n",
8908 c03b0f0f bellard
                        devname);
8909 8d11df9e bellard
                exit(1);
8910 8d11df9e bellard
            }
8911 af3a9031 ths
            if (strstart(devname, "vc", 0))
8912 7ba1260a bellard
                qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
8913 8d11df9e bellard
        }
8914 82c643ff bellard
    }
8915 82c643ff bellard
8916 6508fe59 bellard
    for(i = 0; i < MAX_PARALLEL_PORTS; i++) {
8917 c03b0f0f bellard
        const char *devname = parallel_devices[i];
8918 fd5f393a pbrook
        if (devname && strcmp(devname, "none")) {
8919 c03b0f0f bellard
            parallel_hds[i] = qemu_chr_open(devname);
8920 6508fe59 bellard
            if (!parallel_hds[i]) {
8921 5fafdf24 ths
                fprintf(stderr, "qemu: could not open parallel device '%s'\n",
8922 c03b0f0f bellard
                        devname);
8923 6508fe59 bellard
                exit(1);
8924 6508fe59 bellard
            }
8925 af3a9031 ths
            if (strstart(devname, "vc", 0))
8926 7ba1260a bellard
                qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
8927 6508fe59 bellard
        }
8928 6508fe59 bellard
    }
8929 6508fe59 bellard
8930 b881c2c6 blueswir1
    machine->init(ram_size, vga_ram_size, boot_devices, ds,
8931 94fc95cd j_mayer
                  kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
8932 73332e5c bellard
8933 0d92ed30 pbrook
    /* init USB devices */
8934 0d92ed30 pbrook
    if (usb_enabled) {
8935 0d92ed30 pbrook
        for(i = 0; i < usb_devices_index; i++) {
8936 0d92ed30 pbrook
            if (usb_device_add(usb_devices[i]) < 0) {
8937 0d92ed30 pbrook
                fprintf(stderr, "Warning: could not add USB device %s\n",
8938 0d92ed30 pbrook
                        usb_devices[i]);
8939 0d92ed30 pbrook
            }
8940 0d92ed30 pbrook
        }
8941 0d92ed30 pbrook
    }
8942 0d92ed30 pbrook
8943 740733bb ths
    if (display_state.dpy_refresh) {
8944 740733bb ths
        display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state);
8945 740733bb ths
        qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
8946 740733bb ths
    }
8947 7f7f9873 bellard
8948 67b915a5 bellard
#ifdef CONFIG_GDBSTUB
8949 b4608c04 bellard
    if (use_gdbstub) {
8950 c636bb66 bellard
        /* XXX: use standard host:port notation and modify options
8951 c636bb66 bellard
           accordingly. */
8952 cfc3475a pbrook
        if (gdbserver_start(gdbstub_port) < 0) {
8953 cfc3475a pbrook
            fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
8954 c636bb66 bellard
                    gdbstub_port);
8955 8a7ddc38 bellard
            exit(1);
8956 8a7ddc38 bellard
        }
8957 45669e00 balrog
    }
8958 67b915a5 bellard
#endif
8959 45669e00 balrog
8960 d63d307f bellard
    if (loadvm)
8961 faea38e7 bellard
        do_loadvm(loadvm);
8962 d63d307f bellard
8963 67b915a5 bellard
    {
8964 5905b2e5 bellard
        /* XXX: simplify init */
8965 5905b2e5 bellard
        read_passwords();
8966 3c07f8e8 pbrook
        if (autostart) {
8967 5905b2e5 bellard
            vm_start();
8968 5905b2e5 bellard
        }
8969 0824d6fc bellard
    }
8970 ffd843bc ths
8971 71e3ceb8 ths
    if (daemonize) {
8972 71e3ceb8 ths
        uint8_t status = 0;
8973 71e3ceb8 ths
        ssize_t len;
8974 71e3ceb8 ths
        int fd;
8975 71e3ceb8 ths
8976 71e3ceb8 ths
    again1:
8977 71e3ceb8 ths
        len = write(fds[1], &status, 1);
8978 71e3ceb8 ths
        if (len == -1 && (errno == EINTR))
8979 71e3ceb8 ths
            goto again1;
8980 71e3ceb8 ths
8981 71e3ceb8 ths
        if (len != 1)
8982 71e3ceb8 ths
            exit(1);
8983 71e3ceb8 ths
8984 bd54b863 aliguori
        chdir("/");
8985 aeb30be6 balrog
        TFR(fd = open("/dev/null", O_RDWR));
8986 71e3ceb8 ths
        if (fd == -1)
8987 71e3ceb8 ths
            exit(1);
8988 71e3ceb8 ths
8989 71e3ceb8 ths
        dup2(fd, 0);
8990 71e3ceb8 ths
        dup2(fd, 1);
8991 71e3ceb8 ths
        dup2(fd, 2);
8992 71e3ceb8 ths
8993 71e3ceb8 ths
        close(fd);
8994 71e3ceb8 ths
    }
8995 71e3ceb8 ths
8996 8a7ddc38 bellard
    main_loop();
8997 40c3bac3 bellard
    quit_timers();
8998 b46a8906 ths
8999 7d294b61 ths
#if !defined(_WIN32)
9000 b46a8906 ths
    /* close network clients */
9001 b46a8906 ths
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
9002 b46a8906 ths
        VLANClientState *vc;
9003 b46a8906 ths
9004 7d294b61 ths
        for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
9005 b46a8906 ths
            if (vc->fd_read == tap_receive) {
9006 b46a8906 ths
                char ifname[64];
9007 b46a8906 ths
                TAPState *s = vc->opaque;
9008 b46a8906 ths
9009 b46a8906 ths
                if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 &&
9010 b46a8906 ths
                    s->down_script[0])
9011 b46a8906 ths
                    launch_script(s->down_script, ifname, s->fd);
9012 b46a8906 ths
            }
9013 8a16d273 ths
#if defined(CONFIG_VDE)
9014 8a16d273 ths
            if (vc->fd_read == vde_from_qemu) {
9015 8a16d273 ths
                VDEState *s = vc->opaque;
9016 8a16d273 ths
                vde_close(s->vde);
9017 8a16d273 ths
            }
9018 8a16d273 ths
#endif
9019 4fddf62a ths
        }
9020 7d294b61 ths
    }
9021 7d294b61 ths
#endif
9022 0824d6fc bellard
    return 0;
9023 0824d6fc bellard
}