root / vl.c @ bf20dc07
History | View | Annotate | Download (229.6 kB)
1 |
/*
|
---|---|
2 |
* QEMU System Emulator
|
3 |
*
|
4 |
* Copyright (c) 2003-2008 Fabrice Bellard
|
5 |
*
|
6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
* of this software and associated documentation files (the "Software"), to deal
|
8 |
* in the Software without restriction, including without limitation the rights
|
9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
* copies of the Software, and to permit persons to whom the Software is
|
11 |
* furnished to do so, subject to the following conditions:
|
12 |
*
|
13 |
* The above copyright notice and this permission notice shall be included in
|
14 |
* all copies or substantial portions of the Software.
|
15 |
*
|
16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
* THE SOFTWARE.
|
23 |
*/
|
24 |
#include "hw/hw.h" |
25 |
#include "hw/boards.h" |
26 |
#include "hw/usb.h" |
27 |
#include "hw/pcmcia.h" |
28 |
#include "hw/pc.h" |
29 |
#include "hw/audiodev.h" |
30 |
#include "hw/isa.h" |
31 |
#include "hw/baum.h" |
32 |
#include "net.h" |
33 |
#include "console.h" |
34 |
#include "sysemu.h" |
35 |
#include "gdbstub.h" |
36 |
#include "qemu-timer.h" |
37 |
#include "qemu-char.h" |
38 |
#include "block.h" |
39 |
#include "audio/audio.h" |
40 |
|
41 |
#include <unistd.h> |
42 |
#include <fcntl.h> |
43 |
#include <signal.h> |
44 |
#include <time.h> |
45 |
#include <errno.h> |
46 |
#include <sys/time.h> |
47 |
#include <zlib.h> |
48 |
|
49 |
#ifndef _WIN32
|
50 |
#include <sys/times.h> |
51 |
#include <sys/wait.h> |
52 |
#include <termios.h> |
53 |
#include <sys/poll.h> |
54 |
#include <sys/mman.h> |
55 |
#include <sys/ioctl.h> |
56 |
#include <sys/socket.h> |
57 |
#include <netinet/in.h> |
58 |
#include <dirent.h> |
59 |
#include <netdb.h> |
60 |
#include <sys/select.h> |
61 |
#include <arpa/inet.h> |
62 |
#ifdef _BSD
|
63 |
#include <sys/stat.h> |
64 |
#ifndef __APPLE__
|
65 |
#include <libutil.h> |
66 |
#endif
|
67 |
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
68 |
#include <freebsd/stdlib.h> |
69 |
#else
|
70 |
#ifndef __sun__
|
71 |
#include <linux/if.h> |
72 |
#include <linux/if_tun.h> |
73 |
#include <pty.h> |
74 |
#include <malloc.h> |
75 |
#include <linux/rtc.h> |
76 |
|
77 |
/* For the benefit of older linux systems which don't supply it,
|
78 |
we use a local copy of hpet.h. */
|
79 |
/* #include <linux/hpet.h> */
|
80 |
#include "hpet.h" |
81 |
|
82 |
#include <linux/ppdev.h> |
83 |
#include <linux/parport.h> |
84 |
#else
|
85 |
#include <sys/stat.h> |
86 |
#include <sys/ethernet.h> |
87 |
#include <sys/sockio.h> |
88 |
#include <netinet/arp.h> |
89 |
#include <netinet/in.h> |
90 |
#include <netinet/in_systm.h> |
91 |
#include <netinet/ip.h> |
92 |
#include <netinet/ip_icmp.h> // must come after ip.h |
93 |
#include <netinet/udp.h> |
94 |
#include <netinet/tcp.h> |
95 |
#include <net/if.h> |
96 |
#include <syslog.h> |
97 |
#include <stropts.h> |
98 |
#endif
|
99 |
#endif
|
100 |
#else
|
101 |
#include <winsock2.h> |
102 |
int inet_aton(const char *cp, struct in_addr *ia); |
103 |
#endif
|
104 |
|
105 |
#if defined(CONFIG_SLIRP)
|
106 |
#include "libslirp.h" |
107 |
#endif
|
108 |
|
109 |
#ifdef _WIN32
|
110 |
#include <malloc.h> |
111 |
#include <sys/timeb.h> |
112 |
#include <mmsystem.h> |
113 |
#define getopt_long_only getopt_long
|
114 |
#define memalign(align, size) malloc(size)
|
115 |
#endif
|
116 |
|
117 |
#include "qemu_socket.h" |
118 |
|
119 |
#ifdef CONFIG_SDL
|
120 |
#ifdef __APPLE__
|
121 |
#include <SDL/SDL.h> |
122 |
#endif
|
123 |
#endif /* CONFIG_SDL */ |
124 |
|
125 |
#ifdef CONFIG_COCOA
|
126 |
#undef main
|
127 |
#define main qemu_main
|
128 |
#endif /* CONFIG_COCOA */ |
129 |
|
130 |
#include "disas.h" |
131 |
|
132 |
#include "exec-all.h" |
133 |
|
134 |
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
135 |
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
136 |
#ifdef __sun__
|
137 |
#define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
138 |
#else
|
139 |
#define SMBD_COMMAND "/usr/sbin/smbd" |
140 |
#endif
|
141 |
|
142 |
//#define DEBUG_UNUSED_IOPORT
|
143 |
//#define DEBUG_IOPORT
|
144 |
|
145 |
#ifdef TARGET_PPC
|
146 |
#define DEFAULT_RAM_SIZE 144 |
147 |
#else
|
148 |
#define DEFAULT_RAM_SIZE 128 |
149 |
#endif
|
150 |
/* in ms */
|
151 |
#define GUI_REFRESH_INTERVAL 30 |
152 |
|
153 |
/* Max number of USB devices that can be specified on the commandline. */
|
154 |
#define MAX_USB_CMDLINE 8 |
155 |
|
156 |
/* XXX: use a two level table to limit memory usage */
|
157 |
#define MAX_IOPORTS 65536 |
158 |
|
159 |
const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
160 |
const char *bios_name = NULL; |
161 |
void *ioport_opaque[MAX_IOPORTS];
|
162 |
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
163 |
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
164 |
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
165 |
to store the VM snapshots */
|
166 |
DriveInfo drives_table[MAX_DRIVES+1];
|
167 |
int nb_drives;
|
168 |
/* point to the block driver where the snapshots are managed */
|
169 |
BlockDriverState *bs_snapshots; |
170 |
int vga_ram_size;
|
171 |
static DisplayState display_state;
|
172 |
int nographic;
|
173 |
int curses;
|
174 |
const char* keyboard_layout = NULL; |
175 |
int64_t ticks_per_sec; |
176 |
ram_addr_t ram_size; |
177 |
int pit_min_timer_count = 0; |
178 |
int nb_nics;
|
179 |
NICInfo nd_table[MAX_NICS]; |
180 |
int vm_running;
|
181 |
static int rtc_utc = 1; |
182 |
static int rtc_date_offset = -1; /* -1 means no change */ |
183 |
int cirrus_vga_enabled = 1; |
184 |
int vmsvga_enabled = 0; |
185 |
#ifdef TARGET_SPARC
|
186 |
int graphic_width = 1024; |
187 |
int graphic_height = 768; |
188 |
int graphic_depth = 8; |
189 |
#else
|
190 |
int graphic_width = 800; |
191 |
int graphic_height = 600; |
192 |
int graphic_depth = 15; |
193 |
#endif
|
194 |
int full_screen = 0; |
195 |
int no_frame = 0; |
196 |
int no_quit = 0; |
197 |
CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
198 |
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
199 |
#ifdef TARGET_I386
|
200 |
int win2k_install_hack = 0; |
201 |
#endif
|
202 |
int usb_enabled = 0; |
203 |
static VLANState *first_vlan;
|
204 |
int smp_cpus = 1; |
205 |
const char *vnc_display; |
206 |
#if defined(TARGET_SPARC)
|
207 |
#define MAX_CPUS 16 |
208 |
#elif defined(TARGET_I386)
|
209 |
#define MAX_CPUS 255 |
210 |
#else
|
211 |
#define MAX_CPUS 1 |
212 |
#endif
|
213 |
int acpi_enabled = 1; |
214 |
int fd_bootchk = 1; |
215 |
int no_reboot = 0; |
216 |
int no_shutdown = 0; |
217 |
int cursor_hide = 1; |
218 |
int graphic_rotate = 0; |
219 |
int daemonize = 0; |
220 |
const char *option_rom[MAX_OPTION_ROMS]; |
221 |
int nb_option_roms;
|
222 |
int semihosting_enabled = 0; |
223 |
int autostart = 1; |
224 |
#ifdef TARGET_ARM
|
225 |
int old_param = 0; |
226 |
#endif
|
227 |
const char *qemu_name; |
228 |
int alt_grab = 0; |
229 |
#ifdef TARGET_SPARC
|
230 |
unsigned int nb_prom_envs = 0; |
231 |
const char *prom_envs[MAX_PROM_ENVS]; |
232 |
#endif
|
233 |
int nb_drives_opt;
|
234 |
struct drive_opt {
|
235 |
const char *file; |
236 |
char opt[1024]; |
237 |
} drives_opt[MAX_DRIVES]; |
238 |
|
239 |
static CPUState *cur_cpu;
|
240 |
static CPUState *next_cpu;
|
241 |
static int event_pending = 1; |
242 |
/* Conversion factor from emulated instructions to virtual clock ticks. */
|
243 |
static int icount_time_shift; |
244 |
/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
245 |
#define MAX_ICOUNT_SHIFT 10 |
246 |
/* Compensate for varying guest execution speed. */
|
247 |
static int64_t qemu_icount_bias;
|
248 |
QEMUTimer *icount_rt_timer; |
249 |
QEMUTimer *icount_vm_timer; |
250 |
|
251 |
#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
252 |
|
253 |
/***********************************************************/
|
254 |
/* x86 ISA bus support */
|
255 |
|
256 |
target_phys_addr_t isa_mem_base = 0;
|
257 |
PicState2 *isa_pic; |
258 |
|
259 |
static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
260 |
{ |
261 |
#ifdef DEBUG_UNUSED_IOPORT
|
262 |
fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
263 |
#endif
|
264 |
return 0xff; |
265 |
} |
266 |
|
267 |
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
268 |
{ |
269 |
#ifdef DEBUG_UNUSED_IOPORT
|
270 |
fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
271 |
#endif
|
272 |
} |
273 |
|
274 |
/* default is to make two byte accesses */
|
275 |
static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
276 |
{ |
277 |
uint32_t data; |
278 |
data = ioport_read_table[0][address](ioport_opaque[address], address);
|
279 |
address = (address + 1) & (MAX_IOPORTS - 1); |
280 |
data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
281 |
return data;
|
282 |
} |
283 |
|
284 |
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
285 |
{ |
286 |
ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
287 |
address = (address + 1) & (MAX_IOPORTS - 1); |
288 |
ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
289 |
} |
290 |
|
291 |
static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
292 |
{ |
293 |
#ifdef DEBUG_UNUSED_IOPORT
|
294 |
fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
295 |
#endif
|
296 |
return 0xffffffff; |
297 |
} |
298 |
|
299 |
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
300 |
{ |
301 |
#ifdef DEBUG_UNUSED_IOPORT
|
302 |
fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
303 |
#endif
|
304 |
} |
305 |
|
306 |
static void init_ioports(void) |
307 |
{ |
308 |
int i;
|
309 |
|
310 |
for(i = 0; i < MAX_IOPORTS; i++) { |
311 |
ioport_read_table[0][i] = default_ioport_readb;
|
312 |
ioport_write_table[0][i] = default_ioport_writeb;
|
313 |
ioport_read_table[1][i] = default_ioport_readw;
|
314 |
ioport_write_table[1][i] = default_ioport_writew;
|
315 |
ioport_read_table[2][i] = default_ioport_readl;
|
316 |
ioport_write_table[2][i] = default_ioport_writel;
|
317 |
} |
318 |
} |
319 |
|
320 |
/* size is the word size in byte */
|
321 |
int register_ioport_read(int start, int length, int size, |
322 |
IOPortReadFunc *func, void *opaque)
|
323 |
{ |
324 |
int i, bsize;
|
325 |
|
326 |
if (size == 1) { |
327 |
bsize = 0;
|
328 |
} else if (size == 2) { |
329 |
bsize = 1;
|
330 |
} else if (size == 4) { |
331 |
bsize = 2;
|
332 |
} else {
|
333 |
hw_error("register_ioport_read: invalid size");
|
334 |
return -1; |
335 |
} |
336 |
for(i = start; i < start + length; i += size) {
|
337 |
ioport_read_table[bsize][i] = func; |
338 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
339 |
hw_error("register_ioport_read: invalid opaque");
|
340 |
ioport_opaque[i] = opaque; |
341 |
} |
342 |
return 0; |
343 |
} |
344 |
|
345 |
/* size is the word size in byte */
|
346 |
int register_ioport_write(int start, int length, int size, |
347 |
IOPortWriteFunc *func, void *opaque)
|
348 |
{ |
349 |
int i, bsize;
|
350 |
|
351 |
if (size == 1) { |
352 |
bsize = 0;
|
353 |
} else if (size == 2) { |
354 |
bsize = 1;
|
355 |
} else if (size == 4) { |
356 |
bsize = 2;
|
357 |
} else {
|
358 |
hw_error("register_ioport_write: invalid size");
|
359 |
return -1; |
360 |
} |
361 |
for(i = start; i < start + length; i += size) {
|
362 |
ioport_write_table[bsize][i] = func; |
363 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
364 |
hw_error("register_ioport_write: invalid opaque");
|
365 |
ioport_opaque[i] = opaque; |
366 |
} |
367 |
return 0; |
368 |
} |
369 |
|
370 |
void isa_unassign_ioport(int start, int length) |
371 |
{ |
372 |
int i;
|
373 |
|
374 |
for(i = start; i < start + length; i++) {
|
375 |
ioport_read_table[0][i] = default_ioport_readb;
|
376 |
ioport_read_table[1][i] = default_ioport_readw;
|
377 |
ioport_read_table[2][i] = default_ioport_readl;
|
378 |
|
379 |
ioport_write_table[0][i] = default_ioport_writeb;
|
380 |
ioport_write_table[1][i] = default_ioport_writew;
|
381 |
ioport_write_table[2][i] = default_ioport_writel;
|
382 |
} |
383 |
} |
384 |
|
385 |
/***********************************************************/
|
386 |
|
387 |
void cpu_outb(CPUState *env, int addr, int val) |
388 |
{ |
389 |
#ifdef DEBUG_IOPORT
|
390 |
if (loglevel & CPU_LOG_IOPORT)
|
391 |
fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
392 |
#endif
|
393 |
ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
|
394 |
#ifdef USE_KQEMU
|
395 |
if (env)
|
396 |
env->last_io_time = cpu_get_time_fast(); |
397 |
#endif
|
398 |
} |
399 |
|
400 |
void cpu_outw(CPUState *env, int addr, int val) |
401 |
{ |
402 |
#ifdef DEBUG_IOPORT
|
403 |
if (loglevel & CPU_LOG_IOPORT)
|
404 |
fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
405 |
#endif
|
406 |
ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
|
407 |
#ifdef USE_KQEMU
|
408 |
if (env)
|
409 |
env->last_io_time = cpu_get_time_fast(); |
410 |
#endif
|
411 |
} |
412 |
|
413 |
void cpu_outl(CPUState *env, int addr, int val) |
414 |
{ |
415 |
#ifdef DEBUG_IOPORT
|
416 |
if (loglevel & CPU_LOG_IOPORT)
|
417 |
fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
418 |
#endif
|
419 |
ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
|
420 |
#ifdef USE_KQEMU
|
421 |
if (env)
|
422 |
env->last_io_time = cpu_get_time_fast(); |
423 |
#endif
|
424 |
} |
425 |
|
426 |
int cpu_inb(CPUState *env, int addr) |
427 |
{ |
428 |
int val;
|
429 |
val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
|
430 |
#ifdef DEBUG_IOPORT
|
431 |
if (loglevel & CPU_LOG_IOPORT)
|
432 |
fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
433 |
#endif
|
434 |
#ifdef USE_KQEMU
|
435 |
if (env)
|
436 |
env->last_io_time = cpu_get_time_fast(); |
437 |
#endif
|
438 |
return val;
|
439 |
} |
440 |
|
441 |
int cpu_inw(CPUState *env, int addr) |
442 |
{ |
443 |
int val;
|
444 |
val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
|
445 |
#ifdef DEBUG_IOPORT
|
446 |
if (loglevel & CPU_LOG_IOPORT)
|
447 |
fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
448 |
#endif
|
449 |
#ifdef USE_KQEMU
|
450 |
if (env)
|
451 |
env->last_io_time = cpu_get_time_fast(); |
452 |
#endif
|
453 |
return val;
|
454 |
} |
455 |
|
456 |
int cpu_inl(CPUState *env, int addr) |
457 |
{ |
458 |
int val;
|
459 |
val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
|
460 |
#ifdef DEBUG_IOPORT
|
461 |
if (loglevel & CPU_LOG_IOPORT)
|
462 |
fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
463 |
#endif
|
464 |
#ifdef USE_KQEMU
|
465 |
if (env)
|
466 |
env->last_io_time = cpu_get_time_fast(); |
467 |
#endif
|
468 |
return val;
|
469 |
} |
470 |
|
471 |
/***********************************************************/
|
472 |
void hw_error(const char *fmt, ...) |
473 |
{ |
474 |
va_list ap; |
475 |
CPUState *env; |
476 |
|
477 |
va_start(ap, fmt); |
478 |
fprintf(stderr, "qemu: hardware error: ");
|
479 |
vfprintf(stderr, fmt, ap); |
480 |
fprintf(stderr, "\n");
|
481 |
for(env = first_cpu; env != NULL; env = env->next_cpu) { |
482 |
fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
483 |
#ifdef TARGET_I386
|
484 |
cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
485 |
#else
|
486 |
cpu_dump_state(env, stderr, fprintf, 0);
|
487 |
#endif
|
488 |
} |
489 |
va_end(ap); |
490 |
abort(); |
491 |
} |
492 |
|
493 |
/***********************************************************/
|
494 |
/* keyboard/mouse */
|
495 |
|
496 |
static QEMUPutKBDEvent *qemu_put_kbd_event;
|
497 |
static void *qemu_put_kbd_event_opaque; |
498 |
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
499 |
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
500 |
|
501 |
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
502 |
{ |
503 |
qemu_put_kbd_event_opaque = opaque; |
504 |
qemu_put_kbd_event = func; |
505 |
} |
506 |
|
507 |
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
508 |
void *opaque, int absolute, |
509 |
const char *name) |
510 |
{ |
511 |
QEMUPutMouseEntry *s, *cursor; |
512 |
|
513 |
s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
514 |
if (!s)
|
515 |
return NULL; |
516 |
|
517 |
s->qemu_put_mouse_event = func; |
518 |
s->qemu_put_mouse_event_opaque = opaque; |
519 |
s->qemu_put_mouse_event_absolute = absolute; |
520 |
s->qemu_put_mouse_event_name = qemu_strdup(name); |
521 |
s->next = NULL;
|
522 |
|
523 |
if (!qemu_put_mouse_event_head) {
|
524 |
qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
525 |
return s;
|
526 |
} |
527 |
|
528 |
cursor = qemu_put_mouse_event_head; |
529 |
while (cursor->next != NULL) |
530 |
cursor = cursor->next; |
531 |
|
532 |
cursor->next = s; |
533 |
qemu_put_mouse_event_current = s; |
534 |
|
535 |
return s;
|
536 |
} |
537 |
|
538 |
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
539 |
{ |
540 |
QEMUPutMouseEntry *prev = NULL, *cursor;
|
541 |
|
542 |
if (!qemu_put_mouse_event_head || entry == NULL) |
543 |
return;
|
544 |
|
545 |
cursor = qemu_put_mouse_event_head; |
546 |
while (cursor != NULL && cursor != entry) { |
547 |
prev = cursor; |
548 |
cursor = cursor->next; |
549 |
} |
550 |
|
551 |
if (cursor == NULL) // does not exist or list empty |
552 |
return;
|
553 |
else if (prev == NULL) { // entry is head |
554 |
qemu_put_mouse_event_head = cursor->next; |
555 |
if (qemu_put_mouse_event_current == entry)
|
556 |
qemu_put_mouse_event_current = cursor->next; |
557 |
qemu_free(entry->qemu_put_mouse_event_name); |
558 |
qemu_free(entry); |
559 |
return;
|
560 |
} |
561 |
|
562 |
prev->next = entry->next; |
563 |
|
564 |
if (qemu_put_mouse_event_current == entry)
|
565 |
qemu_put_mouse_event_current = prev; |
566 |
|
567 |
qemu_free(entry->qemu_put_mouse_event_name); |
568 |
qemu_free(entry); |
569 |
} |
570 |
|
571 |
void kbd_put_keycode(int keycode) |
572 |
{ |
573 |
if (qemu_put_kbd_event) {
|
574 |
qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
575 |
} |
576 |
} |
577 |
|
578 |
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
579 |
{ |
580 |
QEMUPutMouseEvent *mouse_event; |
581 |
void *mouse_event_opaque;
|
582 |
int width;
|
583 |
|
584 |
if (!qemu_put_mouse_event_current) {
|
585 |
return;
|
586 |
} |
587 |
|
588 |
mouse_event = |
589 |
qemu_put_mouse_event_current->qemu_put_mouse_event; |
590 |
mouse_event_opaque = |
591 |
qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
592 |
|
593 |
if (mouse_event) {
|
594 |
if (graphic_rotate) {
|
595 |
if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
596 |
width = 0x7fff;
|
597 |
else
|
598 |
width = graphic_width - 1;
|
599 |
mouse_event(mouse_event_opaque, |
600 |
width - dy, dx, dz, buttons_state); |
601 |
} else
|
602 |
mouse_event(mouse_event_opaque, |
603 |
dx, dy, dz, buttons_state); |
604 |
} |
605 |
} |
606 |
|
607 |
int kbd_mouse_is_absolute(void) |
608 |
{ |
609 |
if (!qemu_put_mouse_event_current)
|
610 |
return 0; |
611 |
|
612 |
return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
613 |
} |
614 |
|
615 |
void do_info_mice(void) |
616 |
{ |
617 |
QEMUPutMouseEntry *cursor; |
618 |
int index = 0; |
619 |
|
620 |
if (!qemu_put_mouse_event_head) {
|
621 |
term_printf("No mouse devices connected\n");
|
622 |
return;
|
623 |
} |
624 |
|
625 |
term_printf("Mouse devices available:\n");
|
626 |
cursor = qemu_put_mouse_event_head; |
627 |
while (cursor != NULL) { |
628 |
term_printf("%c Mouse #%d: %s\n",
|
629 |
(cursor == qemu_put_mouse_event_current ? '*' : ' '), |
630 |
index, cursor->qemu_put_mouse_event_name); |
631 |
index++; |
632 |
cursor = cursor->next; |
633 |
} |
634 |
} |
635 |
|
636 |
void do_mouse_set(int index) |
637 |
{ |
638 |
QEMUPutMouseEntry *cursor; |
639 |
int i = 0; |
640 |
|
641 |
if (!qemu_put_mouse_event_head) {
|
642 |
term_printf("No mouse devices connected\n");
|
643 |
return;
|
644 |
} |
645 |
|
646 |
cursor = qemu_put_mouse_event_head; |
647 |
while (cursor != NULL && index != i) { |
648 |
i++; |
649 |
cursor = cursor->next; |
650 |
} |
651 |
|
652 |
if (cursor != NULL) |
653 |
qemu_put_mouse_event_current = cursor; |
654 |
else
|
655 |
term_printf("Mouse at given index not found\n");
|
656 |
} |
657 |
|
658 |
/* compute with 96 bit intermediate result: (a*b)/c */
|
659 |
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
660 |
{ |
661 |
union {
|
662 |
uint64_t ll; |
663 |
struct {
|
664 |
#ifdef WORDS_BIGENDIAN
|
665 |
uint32_t high, low; |
666 |
#else
|
667 |
uint32_t low, high; |
668 |
#endif
|
669 |
} l; |
670 |
} u, res; |
671 |
uint64_t rl, rh; |
672 |
|
673 |
u.ll = a; |
674 |
rl = (uint64_t)u.l.low * (uint64_t)b; |
675 |
rh = (uint64_t)u.l.high * (uint64_t)b; |
676 |
rh += (rl >> 32);
|
677 |
res.l.high = rh / c; |
678 |
res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
679 |
return res.ll;
|
680 |
} |
681 |
|
682 |
/***********************************************************/
|
683 |
/* real time host monotonic timer */
|
684 |
|
685 |
#define QEMU_TIMER_BASE 1000000000LL |
686 |
|
687 |
#ifdef WIN32
|
688 |
|
689 |
static int64_t clock_freq;
|
690 |
|
691 |
static void init_get_clock(void) |
692 |
{ |
693 |
LARGE_INTEGER freq; |
694 |
int ret;
|
695 |
ret = QueryPerformanceFrequency(&freq); |
696 |
if (ret == 0) { |
697 |
fprintf(stderr, "Could not calibrate ticks\n");
|
698 |
exit(1);
|
699 |
} |
700 |
clock_freq = freq.QuadPart; |
701 |
} |
702 |
|
703 |
static int64_t get_clock(void) |
704 |
{ |
705 |
LARGE_INTEGER ti; |
706 |
QueryPerformanceCounter(&ti); |
707 |
return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
708 |
} |
709 |
|
710 |
#else
|
711 |
|
712 |
static int use_rt_clock; |
713 |
|
714 |
static void init_get_clock(void) |
715 |
{ |
716 |
use_rt_clock = 0;
|
717 |
#if defined(__linux__)
|
718 |
{ |
719 |
struct timespec ts;
|
720 |
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
721 |
use_rt_clock = 1;
|
722 |
} |
723 |
} |
724 |
#endif
|
725 |
} |
726 |
|
727 |
static int64_t get_clock(void) |
728 |
{ |
729 |
#if defined(__linux__)
|
730 |
if (use_rt_clock) {
|
731 |
struct timespec ts;
|
732 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
733 |
return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
734 |
} else
|
735 |
#endif
|
736 |
{ |
737 |
/* XXX: using gettimeofday leads to problems if the date
|
738 |
changes, so it should be avoided. */
|
739 |
struct timeval tv;
|
740 |
gettimeofday(&tv, NULL);
|
741 |
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
742 |
} |
743 |
} |
744 |
#endif
|
745 |
|
746 |
/* Return the virtual CPU time, based on the instruction counter. */
|
747 |
static int64_t cpu_get_icount(void) |
748 |
{ |
749 |
int64_t icount; |
750 |
CPUState *env = cpu_single_env;; |
751 |
icount = qemu_icount; |
752 |
if (env) {
|
753 |
if (!can_do_io(env))
|
754 |
fprintf(stderr, "Bad clock read\n");
|
755 |
icount -= (env->icount_decr.u16.low + env->icount_extra); |
756 |
} |
757 |
return qemu_icount_bias + (icount << icount_time_shift);
|
758 |
} |
759 |
|
760 |
/***********************************************************/
|
761 |
/* guest cycle counter */
|
762 |
|
763 |
static int64_t cpu_ticks_prev;
|
764 |
static int64_t cpu_ticks_offset;
|
765 |
static int64_t cpu_clock_offset;
|
766 |
static int cpu_ticks_enabled; |
767 |
|
768 |
/* return the host CPU cycle counter and handle stop/restart */
|
769 |
int64_t cpu_get_ticks(void)
|
770 |
{ |
771 |
if (use_icount) {
|
772 |
return cpu_get_icount();
|
773 |
} |
774 |
if (!cpu_ticks_enabled) {
|
775 |
return cpu_ticks_offset;
|
776 |
} else {
|
777 |
int64_t ticks; |
778 |
ticks = cpu_get_real_ticks(); |
779 |
if (cpu_ticks_prev > ticks) {
|
780 |
/* Note: non increasing ticks may happen if the host uses
|
781 |
software suspend */
|
782 |
cpu_ticks_offset += cpu_ticks_prev - ticks; |
783 |
} |
784 |
cpu_ticks_prev = ticks; |
785 |
return ticks + cpu_ticks_offset;
|
786 |
} |
787 |
} |
788 |
|
789 |
/* return the host CPU monotonic timer and handle stop/restart */
|
790 |
static int64_t cpu_get_clock(void) |
791 |
{ |
792 |
int64_t ti; |
793 |
if (!cpu_ticks_enabled) {
|
794 |
return cpu_clock_offset;
|
795 |
} else {
|
796 |
ti = get_clock(); |
797 |
return ti + cpu_clock_offset;
|
798 |
} |
799 |
} |
800 |
|
801 |
/* enable cpu_get_ticks() */
|
802 |
void cpu_enable_ticks(void) |
803 |
{ |
804 |
if (!cpu_ticks_enabled) {
|
805 |
cpu_ticks_offset -= cpu_get_real_ticks(); |
806 |
cpu_clock_offset -= get_clock(); |
807 |
cpu_ticks_enabled = 1;
|
808 |
} |
809 |
} |
810 |
|
811 |
/* disable cpu_get_ticks() : the clock is stopped. You must not call
|
812 |
cpu_get_ticks() after that. */
|
813 |
void cpu_disable_ticks(void) |
814 |
{ |
815 |
if (cpu_ticks_enabled) {
|
816 |
cpu_ticks_offset = cpu_get_ticks(); |
817 |
cpu_clock_offset = cpu_get_clock(); |
818 |
cpu_ticks_enabled = 0;
|
819 |
} |
820 |
} |
821 |
|
822 |
/***********************************************************/
|
823 |
/* timers */
|
824 |
|
825 |
#define QEMU_TIMER_REALTIME 0 |
826 |
#define QEMU_TIMER_VIRTUAL 1 |
827 |
|
828 |
struct QEMUClock {
|
829 |
int type;
|
830 |
/* XXX: add frequency */
|
831 |
}; |
832 |
|
833 |
struct QEMUTimer {
|
834 |
QEMUClock *clock; |
835 |
int64_t expire_time; |
836 |
QEMUTimerCB *cb; |
837 |
void *opaque;
|
838 |
struct QEMUTimer *next;
|
839 |
}; |
840 |
|
841 |
struct qemu_alarm_timer {
|
842 |
char const *name; |
843 |
unsigned int flags; |
844 |
|
845 |
int (*start)(struct qemu_alarm_timer *t); |
846 |
void (*stop)(struct qemu_alarm_timer *t); |
847 |
void (*rearm)(struct qemu_alarm_timer *t); |
848 |
void *priv;
|
849 |
}; |
850 |
|
851 |
#define ALARM_FLAG_DYNTICKS 0x1 |
852 |
#define ALARM_FLAG_EXPIRED 0x2 |
853 |
|
854 |
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
855 |
{ |
856 |
return t->flags & ALARM_FLAG_DYNTICKS;
|
857 |
} |
858 |
|
859 |
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
860 |
{ |
861 |
if (!alarm_has_dynticks(t))
|
862 |
return;
|
863 |
|
864 |
t->rearm(t); |
865 |
} |
866 |
|
867 |
/* TODO: MIN_TIMER_REARM_US should be optimized */
|
868 |
#define MIN_TIMER_REARM_US 250 |
869 |
|
870 |
static struct qemu_alarm_timer *alarm_timer; |
871 |
|
872 |
#ifdef _WIN32
|
873 |
|
874 |
struct qemu_alarm_win32 {
|
875 |
MMRESULT timerId; |
876 |
HANDLE host_alarm; |
877 |
unsigned int period; |
878 |
} alarm_win32_data = {0, NULL, -1}; |
879 |
|
880 |
static int win32_start_timer(struct qemu_alarm_timer *t); |
881 |
static void win32_stop_timer(struct qemu_alarm_timer *t); |
882 |
static void win32_rearm_timer(struct qemu_alarm_timer *t); |
883 |
|
884 |
#else
|
885 |
|
886 |
static int unix_start_timer(struct qemu_alarm_timer *t); |
887 |
static void unix_stop_timer(struct qemu_alarm_timer *t); |
888 |
|
889 |
#ifdef __linux__
|
890 |
|
891 |
static int dynticks_start_timer(struct qemu_alarm_timer *t); |
892 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
893 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
894 |
|
895 |
static int hpet_start_timer(struct qemu_alarm_timer *t); |
896 |
static void hpet_stop_timer(struct qemu_alarm_timer *t); |
897 |
|
898 |
static int rtc_start_timer(struct qemu_alarm_timer *t); |
899 |
static void rtc_stop_timer(struct qemu_alarm_timer *t); |
900 |
|
901 |
#endif /* __linux__ */ |
902 |
|
903 |
#endif /* _WIN32 */ |
904 |
|
905 |
/* Correlation between real and virtual time is always going to be
|
906 |
fairly approximate, so ignore small variation.
|
907 |
When the guest is idle real and virtual time will be aligned in
|
908 |
the IO wait loop. */
|
909 |
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
910 |
|
911 |
static void icount_adjust(void) |
912 |
{ |
913 |
int64_t cur_time; |
914 |
int64_t cur_icount; |
915 |
int64_t delta; |
916 |
static int64_t last_delta;
|
917 |
/* If the VM is not running, then do nothing. */
|
918 |
if (!vm_running)
|
919 |
return;
|
920 |
|
921 |
cur_time = cpu_get_clock(); |
922 |
cur_icount = qemu_get_clock(vm_clock); |
923 |
delta = cur_icount - cur_time; |
924 |
/* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
925 |
if (delta > 0 |
926 |
&& last_delta + ICOUNT_WOBBLE < delta * 2
|
927 |
&& icount_time_shift > 0) {
|
928 |
/* The guest is getting too far ahead. Slow time down. */
|
929 |
icount_time_shift--; |
930 |
} |
931 |
if (delta < 0 |
932 |
&& last_delta - ICOUNT_WOBBLE > delta * 2
|
933 |
&& icount_time_shift < MAX_ICOUNT_SHIFT) { |
934 |
/* The guest is getting too far behind. Speed time up. */
|
935 |
icount_time_shift++; |
936 |
} |
937 |
last_delta = delta; |
938 |
qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
939 |
} |
940 |
|
941 |
static void icount_adjust_rt(void * opaque) |
942 |
{ |
943 |
qemu_mod_timer(icount_rt_timer, |
944 |
qemu_get_clock(rt_clock) + 1000);
|
945 |
icount_adjust(); |
946 |
} |
947 |
|
948 |
static void icount_adjust_vm(void * opaque) |
949 |
{ |
950 |
qemu_mod_timer(icount_vm_timer, |
951 |
qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
952 |
icount_adjust(); |
953 |
} |
954 |
|
955 |
static void init_icount_adjust(void) |
956 |
{ |
957 |
/* Have both realtime and virtual time triggers for speed adjustment.
|
958 |
The realtime trigger catches emulated time passing too slowly,
|
959 |
the virtual time trigger catches emulated time passing too fast.
|
960 |
Realtime triggers occur even when idle, so use them less frequently
|
961 |
than VM triggers. */
|
962 |
icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
963 |
qemu_mod_timer(icount_rt_timer, |
964 |
qemu_get_clock(rt_clock) + 1000);
|
965 |
icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
966 |
qemu_mod_timer(icount_vm_timer, |
967 |
qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
968 |
} |
969 |
|
970 |
static struct qemu_alarm_timer alarm_timers[] = { |
971 |
#ifndef _WIN32
|
972 |
#ifdef __linux__
|
973 |
{"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
974 |
dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
975 |
/* HPET - if available - is preferred */
|
976 |
{"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
977 |
/* ...otherwise try RTC */
|
978 |
{"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
979 |
#endif
|
980 |
{"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
981 |
#else
|
982 |
{"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
983 |
win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
984 |
{"win32", 0, win32_start_timer, |
985 |
win32_stop_timer, NULL, &alarm_win32_data},
|
986 |
#endif
|
987 |
{NULL, }
|
988 |
}; |
989 |
|
990 |
static void show_available_alarms(void) |
991 |
{ |
992 |
int i;
|
993 |
|
994 |
printf("Available alarm timers, in order of precedence:\n");
|
995 |
for (i = 0; alarm_timers[i].name; i++) |
996 |
printf("%s\n", alarm_timers[i].name);
|
997 |
} |
998 |
|
999 |
static void configure_alarms(char const *opt) |
1000 |
{ |
1001 |
int i;
|
1002 |
int cur = 0; |
1003 |
int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
1004 |
char *arg;
|
1005 |
char *name;
|
1006 |
struct qemu_alarm_timer tmp;
|
1007 |
|
1008 |
if (!strcmp(opt, "?")) { |
1009 |
show_available_alarms(); |
1010 |
exit(0);
|
1011 |
} |
1012 |
|
1013 |
arg = strdup(opt); |
1014 |
|
1015 |
/* Reorder the array */
|
1016 |
name = strtok(arg, ",");
|
1017 |
while (name) {
|
1018 |
for (i = 0; i < count && alarm_timers[i].name; i++) { |
1019 |
if (!strcmp(alarm_timers[i].name, name))
|
1020 |
break;
|
1021 |
} |
1022 |
|
1023 |
if (i == count) {
|
1024 |
fprintf(stderr, "Unknown clock %s\n", name);
|
1025 |
goto next;
|
1026 |
} |
1027 |
|
1028 |
if (i < cur)
|
1029 |
/* Ignore */
|
1030 |
goto next;
|
1031 |
|
1032 |
/* Swap */
|
1033 |
tmp = alarm_timers[i]; |
1034 |
alarm_timers[i] = alarm_timers[cur]; |
1035 |
alarm_timers[cur] = tmp; |
1036 |
|
1037 |
cur++; |
1038 |
next:
|
1039 |
name = strtok(NULL, ","); |
1040 |
} |
1041 |
|
1042 |
free(arg); |
1043 |
|
1044 |
if (cur) {
|
1045 |
/* Disable remaining timers */
|
1046 |
for (i = cur; i < count; i++)
|
1047 |
alarm_timers[i].name = NULL;
|
1048 |
} else {
|
1049 |
show_available_alarms(); |
1050 |
exit(1);
|
1051 |
} |
1052 |
} |
1053 |
|
1054 |
QEMUClock *rt_clock; |
1055 |
QEMUClock *vm_clock; |
1056 |
|
1057 |
static QEMUTimer *active_timers[2]; |
1058 |
|
1059 |
static QEMUClock *qemu_new_clock(int type) |
1060 |
{ |
1061 |
QEMUClock *clock; |
1062 |
clock = qemu_mallocz(sizeof(QEMUClock));
|
1063 |
if (!clock)
|
1064 |
return NULL; |
1065 |
clock->type = type; |
1066 |
return clock;
|
1067 |
} |
1068 |
|
1069 |
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
1070 |
{ |
1071 |
QEMUTimer *ts; |
1072 |
|
1073 |
ts = qemu_mallocz(sizeof(QEMUTimer));
|
1074 |
ts->clock = clock; |
1075 |
ts->cb = cb; |
1076 |
ts->opaque = opaque; |
1077 |
return ts;
|
1078 |
} |
1079 |
|
1080 |
void qemu_free_timer(QEMUTimer *ts)
|
1081 |
{ |
1082 |
qemu_free(ts); |
1083 |
} |
1084 |
|
1085 |
/* stop a timer, but do not dealloc it */
|
1086 |
void qemu_del_timer(QEMUTimer *ts)
|
1087 |
{ |
1088 |
QEMUTimer **pt, *t; |
1089 |
|
1090 |
/* NOTE: this code must be signal safe because
|
1091 |
qemu_timer_expired() can be called from a signal. */
|
1092 |
pt = &active_timers[ts->clock->type]; |
1093 |
for(;;) {
|
1094 |
t = *pt; |
1095 |
if (!t)
|
1096 |
break;
|
1097 |
if (t == ts) {
|
1098 |
*pt = t->next; |
1099 |
break;
|
1100 |
} |
1101 |
pt = &t->next; |
1102 |
} |
1103 |
} |
1104 |
|
1105 |
/* modify the current timer so that it will be fired when current_time
|
1106 |
>= expire_time. The corresponding callback will be called. */
|
1107 |
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1108 |
{ |
1109 |
QEMUTimer **pt, *t; |
1110 |
|
1111 |
qemu_del_timer(ts); |
1112 |
|
1113 |
/* add the timer in the sorted list */
|
1114 |
/* NOTE: this code must be signal safe because
|
1115 |
qemu_timer_expired() can be called from a signal. */
|
1116 |
pt = &active_timers[ts->clock->type]; |
1117 |
for(;;) {
|
1118 |
t = *pt; |
1119 |
if (!t)
|
1120 |
break;
|
1121 |
if (t->expire_time > expire_time)
|
1122 |
break;
|
1123 |
pt = &t->next; |
1124 |
} |
1125 |
ts->expire_time = expire_time; |
1126 |
ts->next = *pt; |
1127 |
*pt = ts; |
1128 |
|
1129 |
/* Rearm if necessary */
|
1130 |
if (pt == &active_timers[ts->clock->type]) {
|
1131 |
if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
1132 |
qemu_rearm_alarm_timer(alarm_timer); |
1133 |
} |
1134 |
/* Interrupt execution to force deadline recalculation. */
|
1135 |
if (use_icount && cpu_single_env) {
|
1136 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
1137 |
} |
1138 |
} |
1139 |
} |
1140 |
|
1141 |
int qemu_timer_pending(QEMUTimer *ts)
|
1142 |
{ |
1143 |
QEMUTimer *t; |
1144 |
for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1145 |
if (t == ts)
|
1146 |
return 1; |
1147 |
} |
1148 |
return 0; |
1149 |
} |
1150 |
|
1151 |
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1152 |
{ |
1153 |
if (!timer_head)
|
1154 |
return 0; |
1155 |
return (timer_head->expire_time <= current_time);
|
1156 |
} |
1157 |
|
1158 |
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1159 |
{ |
1160 |
QEMUTimer *ts; |
1161 |
|
1162 |
for(;;) {
|
1163 |
ts = *ptimer_head; |
1164 |
if (!ts || ts->expire_time > current_time)
|
1165 |
break;
|
1166 |
/* remove timer from the list before calling the callback */
|
1167 |
*ptimer_head = ts->next; |
1168 |
ts->next = NULL;
|
1169 |
|
1170 |
/* run the callback (the timer list can be modified) */
|
1171 |
ts->cb(ts->opaque); |
1172 |
} |
1173 |
} |
1174 |
|
1175 |
int64_t qemu_get_clock(QEMUClock *clock) |
1176 |
{ |
1177 |
switch(clock->type) {
|
1178 |
case QEMU_TIMER_REALTIME:
|
1179 |
return get_clock() / 1000000; |
1180 |
default:
|
1181 |
case QEMU_TIMER_VIRTUAL:
|
1182 |
if (use_icount) {
|
1183 |
return cpu_get_icount();
|
1184 |
} else {
|
1185 |
return cpu_get_clock();
|
1186 |
} |
1187 |
} |
1188 |
} |
1189 |
|
1190 |
static void init_timers(void) |
1191 |
{ |
1192 |
init_get_clock(); |
1193 |
ticks_per_sec = QEMU_TIMER_BASE; |
1194 |
rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1195 |
vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1196 |
} |
1197 |
|
1198 |
/* save a timer */
|
1199 |
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1200 |
{ |
1201 |
uint64_t expire_time; |
1202 |
|
1203 |
if (qemu_timer_pending(ts)) {
|
1204 |
expire_time = ts->expire_time; |
1205 |
} else {
|
1206 |
expire_time = -1;
|
1207 |
} |
1208 |
qemu_put_be64(f, expire_time); |
1209 |
} |
1210 |
|
1211 |
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1212 |
{ |
1213 |
uint64_t expire_time; |
1214 |
|
1215 |
expire_time = qemu_get_be64(f); |
1216 |
if (expire_time != -1) { |
1217 |
qemu_mod_timer(ts, expire_time); |
1218 |
} else {
|
1219 |
qemu_del_timer(ts); |
1220 |
} |
1221 |
} |
1222 |
|
1223 |
static void timer_save(QEMUFile *f, void *opaque) |
1224 |
{ |
1225 |
if (cpu_ticks_enabled) {
|
1226 |
hw_error("cannot save state if virtual timers are running");
|
1227 |
} |
1228 |
qemu_put_be64(f, cpu_ticks_offset); |
1229 |
qemu_put_be64(f, ticks_per_sec); |
1230 |
qemu_put_be64(f, cpu_clock_offset); |
1231 |
} |
1232 |
|
1233 |
static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1234 |
{ |
1235 |
if (version_id != 1 && version_id != 2) |
1236 |
return -EINVAL;
|
1237 |
if (cpu_ticks_enabled) {
|
1238 |
return -EINVAL;
|
1239 |
} |
1240 |
cpu_ticks_offset=qemu_get_be64(f); |
1241 |
ticks_per_sec=qemu_get_be64(f); |
1242 |
if (version_id == 2) { |
1243 |
cpu_clock_offset=qemu_get_be64(f); |
1244 |
} |
1245 |
return 0; |
1246 |
} |
1247 |
|
1248 |
#ifdef _WIN32
|
1249 |
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1250 |
DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1251 |
#else
|
1252 |
static void host_alarm_handler(int host_signum) |
1253 |
#endif
|
1254 |
{ |
1255 |
#if 0
|
1256 |
#define DISP_FREQ 1000
|
1257 |
{
|
1258 |
static int64_t delta_min = INT64_MAX;
|
1259 |
static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1260 |
static int count;
|
1261 |
ti = qemu_get_clock(vm_clock);
|
1262 |
if (last_clock != 0) {
|
1263 |
delta = ti - last_clock;
|
1264 |
if (delta < delta_min)
|
1265 |
delta_min = delta;
|
1266 |
if (delta > delta_max)
|
1267 |
delta_max = delta;
|
1268 |
delta_cum += delta;
|
1269 |
if (++count == DISP_FREQ) {
|
1270 |
printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1271 |
muldiv64(delta_min, 1000000, ticks_per_sec),
|
1272 |
muldiv64(delta_max, 1000000, ticks_per_sec),
|
1273 |
muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1274 |
(double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1275 |
count = 0;
|
1276 |
delta_min = INT64_MAX;
|
1277 |
delta_max = 0;
|
1278 |
delta_cum = 0;
|
1279 |
}
|
1280 |
}
|
1281 |
last_clock = ti;
|
1282 |
}
|
1283 |
#endif
|
1284 |
if (alarm_has_dynticks(alarm_timer) ||
|
1285 |
(!use_icount && |
1286 |
qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1287 |
qemu_get_clock(vm_clock))) || |
1288 |
qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1289 |
qemu_get_clock(rt_clock))) { |
1290 |
#ifdef _WIN32
|
1291 |
struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1292 |
SetEvent(data->host_alarm); |
1293 |
#endif
|
1294 |
CPUState *env = next_cpu; |
1295 |
|
1296 |
alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1297 |
|
1298 |
if (env) {
|
1299 |
/* stop the currently executing cpu because a timer occured */
|
1300 |
cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1301 |
#ifdef USE_KQEMU
|
1302 |
if (env->kqemu_enabled) {
|
1303 |
kqemu_cpu_interrupt(env); |
1304 |
} |
1305 |
#endif
|
1306 |
} |
1307 |
event_pending = 1;
|
1308 |
} |
1309 |
} |
1310 |
|
1311 |
static int64_t qemu_next_deadline(void) |
1312 |
{ |
1313 |
int64_t delta; |
1314 |
|
1315 |
if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1316 |
delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1317 |
qemu_get_clock(vm_clock); |
1318 |
} else {
|
1319 |
/* To avoid problems with overflow limit this to 2^32. */
|
1320 |
delta = INT32_MAX; |
1321 |
} |
1322 |
|
1323 |
if (delta < 0) |
1324 |
delta = 0;
|
1325 |
|
1326 |
return delta;
|
1327 |
} |
1328 |
|
1329 |
static uint64_t qemu_next_deadline_dyntick(void) |
1330 |
{ |
1331 |
int64_t delta; |
1332 |
int64_t rtdelta; |
1333 |
|
1334 |
if (use_icount)
|
1335 |
delta = INT32_MAX; |
1336 |
else
|
1337 |
delta = (qemu_next_deadline() + 999) / 1000; |
1338 |
|
1339 |
if (active_timers[QEMU_TIMER_REALTIME]) {
|
1340 |
rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1341 |
qemu_get_clock(rt_clock))*1000;
|
1342 |
if (rtdelta < delta)
|
1343 |
delta = rtdelta; |
1344 |
} |
1345 |
|
1346 |
if (delta < MIN_TIMER_REARM_US)
|
1347 |
delta = MIN_TIMER_REARM_US; |
1348 |
|
1349 |
return delta;
|
1350 |
} |
1351 |
|
1352 |
#ifndef _WIN32
|
1353 |
|
1354 |
#if defined(__linux__)
|
1355 |
|
1356 |
#define RTC_FREQ 1024 |
1357 |
|
1358 |
static void enable_sigio_timer(int fd) |
1359 |
{ |
1360 |
struct sigaction act;
|
1361 |
|
1362 |
/* timer signal */
|
1363 |
sigfillset(&act.sa_mask); |
1364 |
act.sa_flags = 0;
|
1365 |
act.sa_handler = host_alarm_handler; |
1366 |
|
1367 |
sigaction(SIGIO, &act, NULL);
|
1368 |
fcntl(fd, F_SETFL, O_ASYNC); |
1369 |
fcntl(fd, F_SETOWN, getpid()); |
1370 |
} |
1371 |
|
1372 |
static int hpet_start_timer(struct qemu_alarm_timer *t) |
1373 |
{ |
1374 |
struct hpet_info info;
|
1375 |
int r, fd;
|
1376 |
|
1377 |
fd = open("/dev/hpet", O_RDONLY);
|
1378 |
if (fd < 0) |
1379 |
return -1; |
1380 |
|
1381 |
/* Set frequency */
|
1382 |
r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1383 |
if (r < 0) { |
1384 |
fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1385 |
"error, but for better emulation accuracy type:\n"
|
1386 |
"'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1387 |
goto fail;
|
1388 |
} |
1389 |
|
1390 |
/* Check capabilities */
|
1391 |
r = ioctl(fd, HPET_INFO, &info); |
1392 |
if (r < 0) |
1393 |
goto fail;
|
1394 |
|
1395 |
/* Enable periodic mode */
|
1396 |
r = ioctl(fd, HPET_EPI, 0);
|
1397 |
if (info.hi_flags && (r < 0)) |
1398 |
goto fail;
|
1399 |
|
1400 |
/* Enable interrupt */
|
1401 |
r = ioctl(fd, HPET_IE_ON, 0);
|
1402 |
if (r < 0) |
1403 |
goto fail;
|
1404 |
|
1405 |
enable_sigio_timer(fd); |
1406 |
t->priv = (void *)(long)fd; |
1407 |
|
1408 |
return 0; |
1409 |
fail:
|
1410 |
close(fd); |
1411 |
return -1; |
1412 |
} |
1413 |
|
1414 |
static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1415 |
{ |
1416 |
int fd = (long)t->priv; |
1417 |
|
1418 |
close(fd); |
1419 |
} |
1420 |
|
1421 |
static int rtc_start_timer(struct qemu_alarm_timer *t) |
1422 |
{ |
1423 |
int rtc_fd;
|
1424 |
unsigned long current_rtc_freq = 0; |
1425 |
|
1426 |
TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1427 |
if (rtc_fd < 0) |
1428 |
return -1; |
1429 |
ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1430 |
if (current_rtc_freq != RTC_FREQ &&
|
1431 |
ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1432 |
fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1433 |
"error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1434 |
"type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1435 |
goto fail;
|
1436 |
} |
1437 |
if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1438 |
fail:
|
1439 |
close(rtc_fd); |
1440 |
return -1; |
1441 |
} |
1442 |
|
1443 |
enable_sigio_timer(rtc_fd); |
1444 |
|
1445 |
t->priv = (void *)(long)rtc_fd; |
1446 |
|
1447 |
return 0; |
1448 |
} |
1449 |
|
1450 |
static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1451 |
{ |
1452 |
int rtc_fd = (long)t->priv; |
1453 |
|
1454 |
close(rtc_fd); |
1455 |
} |
1456 |
|
1457 |
static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1458 |
{ |
1459 |
struct sigevent ev;
|
1460 |
timer_t host_timer; |
1461 |
struct sigaction act;
|
1462 |
|
1463 |
sigfillset(&act.sa_mask); |
1464 |
act.sa_flags = 0;
|
1465 |
act.sa_handler = host_alarm_handler; |
1466 |
|
1467 |
sigaction(SIGALRM, &act, NULL);
|
1468 |
|
1469 |
ev.sigev_value.sival_int = 0;
|
1470 |
ev.sigev_notify = SIGEV_SIGNAL; |
1471 |
ev.sigev_signo = SIGALRM; |
1472 |
|
1473 |
if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1474 |
perror("timer_create");
|
1475 |
|
1476 |
/* disable dynticks */
|
1477 |
fprintf(stderr, "Dynamic Ticks disabled\n");
|
1478 |
|
1479 |
return -1; |
1480 |
} |
1481 |
|
1482 |
t->priv = (void *)host_timer;
|
1483 |
|
1484 |
return 0; |
1485 |
} |
1486 |
|
1487 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1488 |
{ |
1489 |
timer_t host_timer = (timer_t)t->priv; |
1490 |
|
1491 |
timer_delete(host_timer); |
1492 |
} |
1493 |
|
1494 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1495 |
{ |
1496 |
timer_t host_timer = (timer_t)t->priv; |
1497 |
struct itimerspec timeout;
|
1498 |
int64_t nearest_delta_us = INT64_MAX; |
1499 |
int64_t current_us; |
1500 |
|
1501 |
if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1502 |
!active_timers[QEMU_TIMER_VIRTUAL]) |
1503 |
return;
|
1504 |
|
1505 |
nearest_delta_us = qemu_next_deadline_dyntick(); |
1506 |
|
1507 |
/* check whether a timer is already running */
|
1508 |
if (timer_gettime(host_timer, &timeout)) {
|
1509 |
perror("gettime");
|
1510 |
fprintf(stderr, "Internal timer error: aborting\n");
|
1511 |
exit(1);
|
1512 |
} |
1513 |
current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1514 |
if (current_us && current_us <= nearest_delta_us)
|
1515 |
return;
|
1516 |
|
1517 |
timeout.it_interval.tv_sec = 0;
|
1518 |
timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1519 |
timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1520 |
timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1521 |
if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1522 |
perror("settime");
|
1523 |
fprintf(stderr, "Internal timer error: aborting\n");
|
1524 |
exit(1);
|
1525 |
} |
1526 |
} |
1527 |
|
1528 |
#endif /* defined(__linux__) */ |
1529 |
|
1530 |
static int unix_start_timer(struct qemu_alarm_timer *t) |
1531 |
{ |
1532 |
struct sigaction act;
|
1533 |
struct itimerval itv;
|
1534 |
int err;
|
1535 |
|
1536 |
/* timer signal */
|
1537 |
sigfillset(&act.sa_mask); |
1538 |
act.sa_flags = 0;
|
1539 |
act.sa_handler = host_alarm_handler; |
1540 |
|
1541 |
sigaction(SIGALRM, &act, NULL);
|
1542 |
|
1543 |
itv.it_interval.tv_sec = 0;
|
1544 |
/* for i386 kernel 2.6 to get 1 ms */
|
1545 |
itv.it_interval.tv_usec = 999;
|
1546 |
itv.it_value.tv_sec = 0;
|
1547 |
itv.it_value.tv_usec = 10 * 1000; |
1548 |
|
1549 |
err = setitimer(ITIMER_REAL, &itv, NULL);
|
1550 |
if (err)
|
1551 |
return -1; |
1552 |
|
1553 |
return 0; |
1554 |
} |
1555 |
|
1556 |
static void unix_stop_timer(struct qemu_alarm_timer *t) |
1557 |
{ |
1558 |
struct itimerval itv;
|
1559 |
|
1560 |
memset(&itv, 0, sizeof(itv)); |
1561 |
setitimer(ITIMER_REAL, &itv, NULL);
|
1562 |
} |
1563 |
|
1564 |
#endif /* !defined(_WIN32) */ |
1565 |
|
1566 |
#ifdef _WIN32
|
1567 |
|
1568 |
static int win32_start_timer(struct qemu_alarm_timer *t) |
1569 |
{ |
1570 |
TIMECAPS tc; |
1571 |
struct qemu_alarm_win32 *data = t->priv;
|
1572 |
UINT flags; |
1573 |
|
1574 |
data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1575 |
if (!data->host_alarm) {
|
1576 |
perror("Failed CreateEvent");
|
1577 |
return -1; |
1578 |
} |
1579 |
|
1580 |
memset(&tc, 0, sizeof(tc)); |
1581 |
timeGetDevCaps(&tc, sizeof(tc));
|
1582 |
|
1583 |
if (data->period < tc.wPeriodMin)
|
1584 |
data->period = tc.wPeriodMin; |
1585 |
|
1586 |
timeBeginPeriod(data->period); |
1587 |
|
1588 |
flags = TIME_CALLBACK_FUNCTION; |
1589 |
if (alarm_has_dynticks(t))
|
1590 |
flags |= TIME_ONESHOT; |
1591 |
else
|
1592 |
flags |= TIME_PERIODIC; |
1593 |
|
1594 |
data->timerId = timeSetEvent(1, // interval (ms) |
1595 |
data->period, // resolution
|
1596 |
host_alarm_handler, // function
|
1597 |
(DWORD)t, // parameter
|
1598 |
flags); |
1599 |
|
1600 |
if (!data->timerId) {
|
1601 |
perror("Failed to initialize win32 alarm timer");
|
1602 |
|
1603 |
timeEndPeriod(data->period); |
1604 |
CloseHandle(data->host_alarm); |
1605 |
return -1; |
1606 |
} |
1607 |
|
1608 |
qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1609 |
|
1610 |
return 0; |
1611 |
} |
1612 |
|
1613 |
static void win32_stop_timer(struct qemu_alarm_timer *t) |
1614 |
{ |
1615 |
struct qemu_alarm_win32 *data = t->priv;
|
1616 |
|
1617 |
timeKillEvent(data->timerId); |
1618 |
timeEndPeriod(data->period); |
1619 |
|
1620 |
CloseHandle(data->host_alarm); |
1621 |
} |
1622 |
|
1623 |
static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1624 |
{ |
1625 |
struct qemu_alarm_win32 *data = t->priv;
|
1626 |
uint64_t nearest_delta_us; |
1627 |
|
1628 |
if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1629 |
!active_timers[QEMU_TIMER_VIRTUAL]) |
1630 |
return;
|
1631 |
|
1632 |
nearest_delta_us = qemu_next_deadline_dyntick(); |
1633 |
nearest_delta_us /= 1000;
|
1634 |
|
1635 |
timeKillEvent(data->timerId); |
1636 |
|
1637 |
data->timerId = timeSetEvent(1,
|
1638 |
data->period, |
1639 |
host_alarm_handler, |
1640 |
(DWORD)t, |
1641 |
TIME_ONESHOT | TIME_PERIODIC); |
1642 |
|
1643 |
if (!data->timerId) {
|
1644 |
perror("Failed to re-arm win32 alarm timer");
|
1645 |
|
1646 |
timeEndPeriod(data->period); |
1647 |
CloseHandle(data->host_alarm); |
1648 |
exit(1);
|
1649 |
} |
1650 |
} |
1651 |
|
1652 |
#endif /* _WIN32 */ |
1653 |
|
1654 |
static void init_timer_alarm(void) |
1655 |
{ |
1656 |
struct qemu_alarm_timer *t;
|
1657 |
int i, err = -1; |
1658 |
|
1659 |
for (i = 0; alarm_timers[i].name; i++) { |
1660 |
t = &alarm_timers[i]; |
1661 |
|
1662 |
err = t->start(t); |
1663 |
if (!err)
|
1664 |
break;
|
1665 |
} |
1666 |
|
1667 |
if (err) {
|
1668 |
fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1669 |
fprintf(stderr, "Terminating\n");
|
1670 |
exit(1);
|
1671 |
} |
1672 |
|
1673 |
alarm_timer = t; |
1674 |
} |
1675 |
|
1676 |
static void quit_timers(void) |
1677 |
{ |
1678 |
alarm_timer->stop(alarm_timer); |
1679 |
alarm_timer = NULL;
|
1680 |
} |
1681 |
|
1682 |
/***********************************************************/
|
1683 |
/* host time/date access */
|
1684 |
void qemu_get_timedate(struct tm *tm, int offset) |
1685 |
{ |
1686 |
time_t ti; |
1687 |
struct tm *ret;
|
1688 |
|
1689 |
time(&ti); |
1690 |
ti += offset; |
1691 |
if (rtc_date_offset == -1) { |
1692 |
if (rtc_utc)
|
1693 |
ret = gmtime(&ti); |
1694 |
else
|
1695 |
ret = localtime(&ti); |
1696 |
} else {
|
1697 |
ti -= rtc_date_offset; |
1698 |
ret = gmtime(&ti); |
1699 |
} |
1700 |
|
1701 |
memcpy(tm, ret, sizeof(struct tm)); |
1702 |
} |
1703 |
|
1704 |
int qemu_timedate_diff(struct tm *tm) |
1705 |
{ |
1706 |
time_t seconds; |
1707 |
|
1708 |
if (rtc_date_offset == -1) |
1709 |
if (rtc_utc)
|
1710 |
seconds = mktimegm(tm); |
1711 |
else
|
1712 |
seconds = mktime(tm); |
1713 |
else
|
1714 |
seconds = mktimegm(tm) + rtc_date_offset; |
1715 |
|
1716 |
return seconds - time(NULL); |
1717 |
} |
1718 |
|
1719 |
/***********************************************************/
|
1720 |
/* character device */
|
1721 |
|
1722 |
static void qemu_chr_event(CharDriverState *s, int event) |
1723 |
{ |
1724 |
if (!s->chr_event)
|
1725 |
return;
|
1726 |
s->chr_event(s->handler_opaque, event); |
1727 |
} |
1728 |
|
1729 |
static void qemu_chr_reset_bh(void *opaque) |
1730 |
{ |
1731 |
CharDriverState *s = opaque; |
1732 |
qemu_chr_event(s, CHR_EVENT_RESET); |
1733 |
qemu_bh_delete(s->bh); |
1734 |
s->bh = NULL;
|
1735 |
} |
1736 |
|
1737 |
void qemu_chr_reset(CharDriverState *s)
|
1738 |
{ |
1739 |
if (s->bh == NULL) { |
1740 |
s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1741 |
qemu_bh_schedule(s->bh); |
1742 |
} |
1743 |
} |
1744 |
|
1745 |
int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1746 |
{ |
1747 |
return s->chr_write(s, buf, len);
|
1748 |
} |
1749 |
|
1750 |
int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1751 |
{ |
1752 |
if (!s->chr_ioctl)
|
1753 |
return -ENOTSUP;
|
1754 |
return s->chr_ioctl(s, cmd, arg);
|
1755 |
} |
1756 |
|
1757 |
int qemu_chr_can_read(CharDriverState *s)
|
1758 |
{ |
1759 |
if (!s->chr_can_read)
|
1760 |
return 0; |
1761 |
return s->chr_can_read(s->handler_opaque);
|
1762 |
} |
1763 |
|
1764 |
void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1765 |
{ |
1766 |
s->chr_read(s->handler_opaque, buf, len); |
1767 |
} |
1768 |
|
1769 |
void qemu_chr_accept_input(CharDriverState *s)
|
1770 |
{ |
1771 |
if (s->chr_accept_input)
|
1772 |
s->chr_accept_input(s); |
1773 |
} |
1774 |
|
1775 |
void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1776 |
{ |
1777 |
char buf[4096]; |
1778 |
va_list ap; |
1779 |
va_start(ap, fmt); |
1780 |
vsnprintf(buf, sizeof(buf), fmt, ap);
|
1781 |
qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1782 |
va_end(ap); |
1783 |
} |
1784 |
|
1785 |
void qemu_chr_send_event(CharDriverState *s, int event) |
1786 |
{ |
1787 |
if (s->chr_send_event)
|
1788 |
s->chr_send_event(s, event); |
1789 |
} |
1790 |
|
1791 |
void qemu_chr_add_handlers(CharDriverState *s,
|
1792 |
IOCanRWHandler *fd_can_read, |
1793 |
IOReadHandler *fd_read, |
1794 |
IOEventHandler *fd_event, |
1795 |
void *opaque)
|
1796 |
{ |
1797 |
s->chr_can_read = fd_can_read; |
1798 |
s->chr_read = fd_read; |
1799 |
s->chr_event = fd_event; |
1800 |
s->handler_opaque = opaque; |
1801 |
if (s->chr_update_read_handler)
|
1802 |
s->chr_update_read_handler(s); |
1803 |
} |
1804 |
|
1805 |
static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1806 |
{ |
1807 |
return len;
|
1808 |
} |
1809 |
|
1810 |
static CharDriverState *qemu_chr_open_null(void) |
1811 |
{ |
1812 |
CharDriverState *chr; |
1813 |
|
1814 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
1815 |
if (!chr)
|
1816 |
return NULL; |
1817 |
chr->chr_write = null_chr_write; |
1818 |
return chr;
|
1819 |
} |
1820 |
|
1821 |
/* MUX driver for serial I/O splitting */
|
1822 |
static int term_timestamps; |
1823 |
static int64_t term_timestamps_start;
|
1824 |
#define MAX_MUX 4 |
1825 |
#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1826 |
#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1827 |
typedef struct { |
1828 |
IOCanRWHandler *chr_can_read[MAX_MUX]; |
1829 |
IOReadHandler *chr_read[MAX_MUX]; |
1830 |
IOEventHandler *chr_event[MAX_MUX]; |
1831 |
void *ext_opaque[MAX_MUX];
|
1832 |
CharDriverState *drv; |
1833 |
unsigned char buffer[MUX_BUFFER_SIZE]; |
1834 |
int prod;
|
1835 |
int cons;
|
1836 |
int mux_cnt;
|
1837 |
int term_got_escape;
|
1838 |
int max_size;
|
1839 |
} MuxDriver; |
1840 |
|
1841 |
|
1842 |
static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1843 |
{ |
1844 |
MuxDriver *d = chr->opaque; |
1845 |
int ret;
|
1846 |
if (!term_timestamps) {
|
1847 |
ret = d->drv->chr_write(d->drv, buf, len); |
1848 |
} else {
|
1849 |
int i;
|
1850 |
|
1851 |
ret = 0;
|
1852 |
for(i = 0; i < len; i++) { |
1853 |
ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1854 |
if (buf[i] == '\n') { |
1855 |
char buf1[64]; |
1856 |
int64_t ti; |
1857 |
int secs;
|
1858 |
|
1859 |
ti = get_clock(); |
1860 |
if (term_timestamps_start == -1) |
1861 |
term_timestamps_start = ti; |
1862 |
ti -= term_timestamps_start; |
1863 |
secs = ti / 1000000000;
|
1864 |
snprintf(buf1, sizeof(buf1),
|
1865 |
"[%02d:%02d:%02d.%03d] ",
|
1866 |
secs / 3600,
|
1867 |
(secs / 60) % 60, |
1868 |
secs % 60,
|
1869 |
(int)((ti / 1000000) % 1000)); |
1870 |
d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1871 |
} |
1872 |
} |
1873 |
} |
1874 |
return ret;
|
1875 |
} |
1876 |
|
1877 |
static char *mux_help[] = { |
1878 |
"% h print this help\n\r",
|
1879 |
"% x exit emulator\n\r",
|
1880 |
"% s save disk data back to file (if -snapshot)\n\r",
|
1881 |
"% t toggle console timestamps\n\r"
|
1882 |
"% b send break (magic sysrq)\n\r",
|
1883 |
"% c switch between console and monitor\n\r",
|
1884 |
"% % sends %\n\r",
|
1885 |
NULL
|
1886 |
}; |
1887 |
|
1888 |
static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1889 |
static void mux_print_help(CharDriverState *chr) |
1890 |
{ |
1891 |
int i, j;
|
1892 |
char ebuf[15] = "Escape-Char"; |
1893 |
char cbuf[50] = "\n\r"; |
1894 |
|
1895 |
if (term_escape_char > 0 && term_escape_char < 26) { |
1896 |
sprintf(cbuf,"\n\r");
|
1897 |
sprintf(ebuf,"C-%c", term_escape_char - 1 + 'a'); |
1898 |
} else {
|
1899 |
sprintf(cbuf,"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1900 |
term_escape_char); |
1901 |
} |
1902 |
chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1903 |
for (i = 0; mux_help[i] != NULL; i++) { |
1904 |
for (j=0; mux_help[i][j] != '\0'; j++) { |
1905 |
if (mux_help[i][j] == '%') |
1906 |
chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1907 |
else
|
1908 |
chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1909 |
} |
1910 |
} |
1911 |
} |
1912 |
|
1913 |
static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1914 |
{ |
1915 |
if (d->term_got_escape) {
|
1916 |
d->term_got_escape = 0;
|
1917 |
if (ch == term_escape_char)
|
1918 |
goto send_char;
|
1919 |
switch(ch) {
|
1920 |
case '?': |
1921 |
case 'h': |
1922 |
mux_print_help(chr); |
1923 |
break;
|
1924 |
case 'x': |
1925 |
{ |
1926 |
char *term = "QEMU: Terminated\n\r"; |
1927 |
chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1928 |
exit(0);
|
1929 |
break;
|
1930 |
} |
1931 |
case 's': |
1932 |
{ |
1933 |
int i;
|
1934 |
for (i = 0; i < nb_drives; i++) { |
1935 |
bdrv_commit(drives_table[i].bdrv); |
1936 |
} |
1937 |
} |
1938 |
break;
|
1939 |
case 'b': |
1940 |
qemu_chr_event(chr, CHR_EVENT_BREAK); |
1941 |
break;
|
1942 |
case 'c': |
1943 |
/* Switch to the next registered device */
|
1944 |
chr->focus++; |
1945 |
if (chr->focus >= d->mux_cnt)
|
1946 |
chr->focus = 0;
|
1947 |
break;
|
1948 |
case 't': |
1949 |
term_timestamps = !term_timestamps; |
1950 |
term_timestamps_start = -1;
|
1951 |
break;
|
1952 |
} |
1953 |
} else if (ch == term_escape_char) { |
1954 |
d->term_got_escape = 1;
|
1955 |
} else {
|
1956 |
send_char:
|
1957 |
return 1; |
1958 |
} |
1959 |
return 0; |
1960 |
} |
1961 |
|
1962 |
static void mux_chr_accept_input(CharDriverState *chr) |
1963 |
{ |
1964 |
int m = chr->focus;
|
1965 |
MuxDriver *d = chr->opaque; |
1966 |
|
1967 |
while (d->prod != d->cons &&
|
1968 |
d->chr_can_read[m] && |
1969 |
d->chr_can_read[m](d->ext_opaque[m])) { |
1970 |
d->chr_read[m](d->ext_opaque[m], |
1971 |
&d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1972 |
} |
1973 |
} |
1974 |
|
1975 |
static int mux_chr_can_read(void *opaque) |
1976 |
{ |
1977 |
CharDriverState *chr = opaque; |
1978 |
MuxDriver *d = chr->opaque; |
1979 |
|
1980 |
if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
1981 |
return 1; |
1982 |
if (d->chr_can_read[chr->focus])
|
1983 |
return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
1984 |
return 0; |
1985 |
} |
1986 |
|
1987 |
static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
1988 |
{ |
1989 |
CharDriverState *chr = opaque; |
1990 |
MuxDriver *d = chr->opaque; |
1991 |
int m = chr->focus;
|
1992 |
int i;
|
1993 |
|
1994 |
mux_chr_accept_input (opaque); |
1995 |
|
1996 |
for(i = 0; i < size; i++) |
1997 |
if (mux_proc_byte(chr, d, buf[i])) {
|
1998 |
if (d->prod == d->cons &&
|
1999 |
d->chr_can_read[m] && |
2000 |
d->chr_can_read[m](d->ext_opaque[m])) |
2001 |
d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
2002 |
else
|
2003 |
d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
2004 |
} |
2005 |
} |
2006 |
|
2007 |
static void mux_chr_event(void *opaque, int event) |
2008 |
{ |
2009 |
CharDriverState *chr = opaque; |
2010 |
MuxDriver *d = chr->opaque; |
2011 |
int i;
|
2012 |
|
2013 |
/* Send the event to all registered listeners */
|
2014 |
for (i = 0; i < d->mux_cnt; i++) |
2015 |
if (d->chr_event[i])
|
2016 |
d->chr_event[i](d->ext_opaque[i], event); |
2017 |
} |
2018 |
|
2019 |
static void mux_chr_update_read_handler(CharDriverState *chr) |
2020 |
{ |
2021 |
MuxDriver *d = chr->opaque; |
2022 |
|
2023 |
if (d->mux_cnt >= MAX_MUX) {
|
2024 |
fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
2025 |
return;
|
2026 |
} |
2027 |
d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
2028 |
d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
2029 |
d->chr_read[d->mux_cnt] = chr->chr_read; |
2030 |
d->chr_event[d->mux_cnt] = chr->chr_event; |
2031 |
/* Fix up the real driver with mux routines */
|
2032 |
if (d->mux_cnt == 0) { |
2033 |
qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
2034 |
mux_chr_event, chr); |
2035 |
} |
2036 |
chr->focus = d->mux_cnt; |
2037 |
d->mux_cnt++; |
2038 |
} |
2039 |
|
2040 |
static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
2041 |
{ |
2042 |
CharDriverState *chr; |
2043 |
MuxDriver *d; |
2044 |
|
2045 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2046 |
if (!chr)
|
2047 |
return NULL; |
2048 |
d = qemu_mallocz(sizeof(MuxDriver));
|
2049 |
if (!d) {
|
2050 |
free(chr); |
2051 |
return NULL; |
2052 |
} |
2053 |
|
2054 |
chr->opaque = d; |
2055 |
d->drv = drv; |
2056 |
chr->focus = -1;
|
2057 |
chr->chr_write = mux_chr_write; |
2058 |
chr->chr_update_read_handler = mux_chr_update_read_handler; |
2059 |
chr->chr_accept_input = mux_chr_accept_input; |
2060 |
return chr;
|
2061 |
} |
2062 |
|
2063 |
|
2064 |
#ifdef _WIN32
|
2065 |
|
2066 |
static void socket_cleanup(void) |
2067 |
{ |
2068 |
WSACleanup(); |
2069 |
} |
2070 |
|
2071 |
static int socket_init(void) |
2072 |
{ |
2073 |
WSADATA Data; |
2074 |
int ret, err;
|
2075 |
|
2076 |
ret = WSAStartup(MAKEWORD(2,2), &Data); |
2077 |
if (ret != 0) { |
2078 |
err = WSAGetLastError(); |
2079 |
fprintf(stderr, "WSAStartup: %d\n", err);
|
2080 |
return -1; |
2081 |
} |
2082 |
atexit(socket_cleanup); |
2083 |
return 0; |
2084 |
} |
2085 |
|
2086 |
static int send_all(int fd, const uint8_t *buf, int len1) |
2087 |
{ |
2088 |
int ret, len;
|
2089 |
|
2090 |
len = len1; |
2091 |
while (len > 0) { |
2092 |
ret = send(fd, buf, len, 0);
|
2093 |
if (ret < 0) { |
2094 |
int errno;
|
2095 |
errno = WSAGetLastError(); |
2096 |
if (errno != WSAEWOULDBLOCK) {
|
2097 |
return -1; |
2098 |
} |
2099 |
} else if (ret == 0) { |
2100 |
break;
|
2101 |
} else {
|
2102 |
buf += ret; |
2103 |
len -= ret; |
2104 |
} |
2105 |
} |
2106 |
return len1 - len;
|
2107 |
} |
2108 |
|
2109 |
void socket_set_nonblock(int fd) |
2110 |
{ |
2111 |
unsigned long opt = 1; |
2112 |
ioctlsocket(fd, FIONBIO, &opt); |
2113 |
} |
2114 |
|
2115 |
#else
|
2116 |
|
2117 |
static int unix_write(int fd, const uint8_t *buf, int len1) |
2118 |
{ |
2119 |
int ret, len;
|
2120 |
|
2121 |
len = len1; |
2122 |
while (len > 0) { |
2123 |
ret = write(fd, buf, len); |
2124 |
if (ret < 0) { |
2125 |
if (errno != EINTR && errno != EAGAIN)
|
2126 |
return -1; |
2127 |
} else if (ret == 0) { |
2128 |
break;
|
2129 |
} else {
|
2130 |
buf += ret; |
2131 |
len -= ret; |
2132 |
} |
2133 |
} |
2134 |
return len1 - len;
|
2135 |
} |
2136 |
|
2137 |
static inline int send_all(int fd, const uint8_t *buf, int len1) |
2138 |
{ |
2139 |
return unix_write(fd, buf, len1);
|
2140 |
} |
2141 |
|
2142 |
void socket_set_nonblock(int fd) |
2143 |
{ |
2144 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
2145 |
} |
2146 |
#endif /* !_WIN32 */ |
2147 |
|
2148 |
#ifndef _WIN32
|
2149 |
|
2150 |
typedef struct { |
2151 |
int fd_in, fd_out;
|
2152 |
int max_size;
|
2153 |
} FDCharDriver; |
2154 |
|
2155 |
#define STDIO_MAX_CLIENTS 1 |
2156 |
static int stdio_nb_clients = 0; |
2157 |
|
2158 |
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2159 |
{ |
2160 |
FDCharDriver *s = chr->opaque; |
2161 |
return unix_write(s->fd_out, buf, len);
|
2162 |
} |
2163 |
|
2164 |
static int fd_chr_read_poll(void *opaque) |
2165 |
{ |
2166 |
CharDriverState *chr = opaque; |
2167 |
FDCharDriver *s = chr->opaque; |
2168 |
|
2169 |
s->max_size = qemu_chr_can_read(chr); |
2170 |
return s->max_size;
|
2171 |
} |
2172 |
|
2173 |
static void fd_chr_read(void *opaque) |
2174 |
{ |
2175 |
CharDriverState *chr = opaque; |
2176 |
FDCharDriver *s = chr->opaque; |
2177 |
int size, len;
|
2178 |
uint8_t buf[1024];
|
2179 |
|
2180 |
len = sizeof(buf);
|
2181 |
if (len > s->max_size)
|
2182 |
len = s->max_size; |
2183 |
if (len == 0) |
2184 |
return;
|
2185 |
size = read(s->fd_in, buf, len); |
2186 |
if (size == 0) { |
2187 |
/* FD has been closed. Remove it from the active list. */
|
2188 |
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2189 |
return;
|
2190 |
} |
2191 |
if (size > 0) { |
2192 |
qemu_chr_read(chr, buf, size); |
2193 |
} |
2194 |
} |
2195 |
|
2196 |
static void fd_chr_update_read_handler(CharDriverState *chr) |
2197 |
{ |
2198 |
FDCharDriver *s = chr->opaque; |
2199 |
|
2200 |
if (s->fd_in >= 0) { |
2201 |
if (nographic && s->fd_in == 0) { |
2202 |
} else {
|
2203 |
qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2204 |
fd_chr_read, NULL, chr);
|
2205 |
} |
2206 |
} |
2207 |
} |
2208 |
|
2209 |
static void fd_chr_close(struct CharDriverState *chr) |
2210 |
{ |
2211 |
FDCharDriver *s = chr->opaque; |
2212 |
|
2213 |
if (s->fd_in >= 0) { |
2214 |
if (nographic && s->fd_in == 0) { |
2215 |
} else {
|
2216 |
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2217 |
} |
2218 |
} |
2219 |
|
2220 |
qemu_free(s); |
2221 |
} |
2222 |
|
2223 |
/* open a character device to a unix fd */
|
2224 |
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2225 |
{ |
2226 |
CharDriverState *chr; |
2227 |
FDCharDriver *s; |
2228 |
|
2229 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2230 |
if (!chr)
|
2231 |
return NULL; |
2232 |
s = qemu_mallocz(sizeof(FDCharDriver));
|
2233 |
if (!s) {
|
2234 |
free(chr); |
2235 |
return NULL; |
2236 |
} |
2237 |
s->fd_in = fd_in; |
2238 |
s->fd_out = fd_out; |
2239 |
chr->opaque = s; |
2240 |
chr->chr_write = fd_chr_write; |
2241 |
chr->chr_update_read_handler = fd_chr_update_read_handler; |
2242 |
chr->chr_close = fd_chr_close; |
2243 |
|
2244 |
qemu_chr_reset(chr); |
2245 |
|
2246 |
return chr;
|
2247 |
} |
2248 |
|
2249 |
static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2250 |
{ |
2251 |
int fd_out;
|
2252 |
|
2253 |
TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2254 |
if (fd_out < 0) |
2255 |
return NULL; |
2256 |
return qemu_chr_open_fd(-1, fd_out); |
2257 |
} |
2258 |
|
2259 |
static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2260 |
{ |
2261 |
int fd_in, fd_out;
|
2262 |
char filename_in[256], filename_out[256]; |
2263 |
|
2264 |
snprintf(filename_in, 256, "%s.in", filename); |
2265 |
snprintf(filename_out, 256, "%s.out", filename); |
2266 |
TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2267 |
TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2268 |
if (fd_in < 0 || fd_out < 0) { |
2269 |
if (fd_in >= 0) |
2270 |
close(fd_in); |
2271 |
if (fd_out >= 0) |
2272 |
close(fd_out); |
2273 |
TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2274 |
if (fd_in < 0) |
2275 |
return NULL; |
2276 |
} |
2277 |
return qemu_chr_open_fd(fd_in, fd_out);
|
2278 |
} |
2279 |
|
2280 |
|
2281 |
/* for STDIO, we handle the case where several clients use it
|
2282 |
(nographic mode) */
|
2283 |
|
2284 |
#define TERM_FIFO_MAX_SIZE 1 |
2285 |
|
2286 |
static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2287 |
static int term_fifo_size; |
2288 |
|
2289 |
static int stdio_read_poll(void *opaque) |
2290 |
{ |
2291 |
CharDriverState *chr = opaque; |
2292 |
|
2293 |
/* try to flush the queue if needed */
|
2294 |
if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2295 |
qemu_chr_read(chr, term_fifo, 1);
|
2296 |
term_fifo_size = 0;
|
2297 |
} |
2298 |
/* see if we can absorb more chars */
|
2299 |
if (term_fifo_size == 0) |
2300 |
return 1; |
2301 |
else
|
2302 |
return 0; |
2303 |
} |
2304 |
|
2305 |
static void stdio_read(void *opaque) |
2306 |
{ |
2307 |
int size;
|
2308 |
uint8_t buf[1];
|
2309 |
CharDriverState *chr = opaque; |
2310 |
|
2311 |
size = read(0, buf, 1); |
2312 |
if (size == 0) { |
2313 |
/* stdin has been closed. Remove it from the active list. */
|
2314 |
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2315 |
return;
|
2316 |
} |
2317 |
if (size > 0) { |
2318 |
if (qemu_chr_can_read(chr) > 0) { |
2319 |
qemu_chr_read(chr, buf, 1);
|
2320 |
} else if (term_fifo_size == 0) { |
2321 |
term_fifo[term_fifo_size++] = buf[0];
|
2322 |
} |
2323 |
} |
2324 |
} |
2325 |
|
2326 |
/* init terminal so that we can grab keys */
|
2327 |
static struct termios oldtty; |
2328 |
static int old_fd0_flags; |
2329 |
static int term_atexit_done; |
2330 |
|
2331 |
static void term_exit(void) |
2332 |
{ |
2333 |
tcsetattr (0, TCSANOW, &oldtty);
|
2334 |
fcntl(0, F_SETFL, old_fd0_flags);
|
2335 |
} |
2336 |
|
2337 |
static void term_init(void) |
2338 |
{ |
2339 |
struct termios tty;
|
2340 |
|
2341 |
tcgetattr (0, &tty);
|
2342 |
oldtty = tty; |
2343 |
old_fd0_flags = fcntl(0, F_GETFL);
|
2344 |
|
2345 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2346 |
|INLCR|IGNCR|ICRNL|IXON); |
2347 |
tty.c_oflag |= OPOST; |
2348 |
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2349 |
/* if graphical mode, we allow Ctrl-C handling */
|
2350 |
if (nographic)
|
2351 |
tty.c_lflag &= ~ISIG; |
2352 |
tty.c_cflag &= ~(CSIZE|PARENB); |
2353 |
tty.c_cflag |= CS8; |
2354 |
tty.c_cc[VMIN] = 1;
|
2355 |
tty.c_cc[VTIME] = 0;
|
2356 |
|
2357 |
tcsetattr (0, TCSANOW, &tty);
|
2358 |
|
2359 |
if (!term_atexit_done++)
|
2360 |
atexit(term_exit); |
2361 |
|
2362 |
fcntl(0, F_SETFL, O_NONBLOCK);
|
2363 |
} |
2364 |
|
2365 |
static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2366 |
{ |
2367 |
term_exit(); |
2368 |
stdio_nb_clients--; |
2369 |
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2370 |
fd_chr_close(chr); |
2371 |
} |
2372 |
|
2373 |
static CharDriverState *qemu_chr_open_stdio(void) |
2374 |
{ |
2375 |
CharDriverState *chr; |
2376 |
|
2377 |
if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2378 |
return NULL; |
2379 |
chr = qemu_chr_open_fd(0, 1); |
2380 |
chr->chr_close = qemu_chr_close_stdio; |
2381 |
qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2382 |
stdio_nb_clients++; |
2383 |
term_init(); |
2384 |
|
2385 |
return chr;
|
2386 |
} |
2387 |
|
2388 |
#ifdef __sun__
|
2389 |
/* Once Solaris has openpty(), this is going to be removed. */
|
2390 |
int openpty(int *amaster, int *aslave, char *name, |
2391 |
struct termios *termp, struct winsize *winp) |
2392 |
{ |
2393 |
const char *slave; |
2394 |
int mfd = -1, sfd = -1; |
2395 |
|
2396 |
*amaster = *aslave = -1;
|
2397 |
|
2398 |
mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2399 |
if (mfd < 0) |
2400 |
goto err;
|
2401 |
|
2402 |
if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2403 |
goto err;
|
2404 |
|
2405 |
if ((slave = ptsname(mfd)) == NULL) |
2406 |
goto err;
|
2407 |
|
2408 |
if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2409 |
goto err;
|
2410 |
|
2411 |
if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2412 |
(termp != NULL && tcgetattr(sfd, termp) < 0)) |
2413 |
goto err;
|
2414 |
|
2415 |
if (amaster)
|
2416 |
*amaster = mfd; |
2417 |
if (aslave)
|
2418 |
*aslave = sfd; |
2419 |
if (winp)
|
2420 |
ioctl(sfd, TIOCSWINSZ, winp); |
2421 |
|
2422 |
return 0; |
2423 |
|
2424 |
err:
|
2425 |
if (sfd != -1) |
2426 |
close(sfd); |
2427 |
close(mfd); |
2428 |
return -1; |
2429 |
} |
2430 |
|
2431 |
void cfmakeraw (struct termios *termios_p) |
2432 |
{ |
2433 |
termios_p->c_iflag &= |
2434 |
~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2435 |
termios_p->c_oflag &= ~OPOST; |
2436 |
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2437 |
termios_p->c_cflag &= ~(CSIZE|PARENB); |
2438 |
termios_p->c_cflag |= CS8; |
2439 |
|
2440 |
termios_p->c_cc[VMIN] = 0;
|
2441 |
termios_p->c_cc[VTIME] = 0;
|
2442 |
} |
2443 |
#endif
|
2444 |
|
2445 |
#if defined(__linux__) || defined(__sun__)
|
2446 |
static CharDriverState *qemu_chr_open_pty(void) |
2447 |
{ |
2448 |
struct termios tty;
|
2449 |
int master_fd, slave_fd;
|
2450 |
|
2451 |
if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) < 0) { |
2452 |
return NULL; |
2453 |
} |
2454 |
|
2455 |
/* Set raw attributes on the pty. */
|
2456 |
cfmakeraw(&tty); |
2457 |
tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2458 |
|
2459 |
fprintf(stderr, "char device redirected to %s\n", ptsname(master_fd));
|
2460 |
return qemu_chr_open_fd(master_fd, master_fd);
|
2461 |
} |
2462 |
|
2463 |
static void tty_serial_init(int fd, int speed, |
2464 |
int parity, int data_bits, int stop_bits) |
2465 |
{ |
2466 |
struct termios tty;
|
2467 |
speed_t spd; |
2468 |
|
2469 |
#if 0
|
2470 |
printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2471 |
speed, parity, data_bits, stop_bits);
|
2472 |
#endif
|
2473 |
tcgetattr (fd, &tty); |
2474 |
|
2475 |
#define MARGIN 1.1 |
2476 |
if (speed <= 50 * MARGIN) |
2477 |
spd = B50; |
2478 |
else if (speed <= 75 * MARGIN) |
2479 |
spd = B75; |
2480 |
else if (speed <= 300 * MARGIN) |
2481 |
spd = B300; |
2482 |
else if (speed <= 600 * MARGIN) |
2483 |
spd = B600; |
2484 |
else if (speed <= 1200 * MARGIN) |
2485 |
spd = B1200; |
2486 |
else if (speed <= 2400 * MARGIN) |
2487 |
spd = B2400; |
2488 |
else if (speed <= 4800 * MARGIN) |
2489 |
spd = B4800; |
2490 |
else if (speed <= 9600 * MARGIN) |
2491 |
spd = B9600; |
2492 |
else if (speed <= 19200 * MARGIN) |
2493 |
spd = B19200; |
2494 |
else if (speed <= 38400 * MARGIN) |
2495 |
spd = B38400; |
2496 |
else if (speed <= 57600 * MARGIN) |
2497 |
spd = B57600; |
2498 |
else if (speed <= 115200 * MARGIN) |
2499 |
spd = B115200; |
2500 |
else
|
2501 |
spd = B115200; |
2502 |
|
2503 |
cfsetispeed(&tty, spd); |
2504 |
cfsetospeed(&tty, spd); |
2505 |
|
2506 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2507 |
|INLCR|IGNCR|ICRNL|IXON); |
2508 |
tty.c_oflag |= OPOST; |
2509 |
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2510 |
tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2511 |
switch(data_bits) {
|
2512 |
default:
|
2513 |
case 8: |
2514 |
tty.c_cflag |= CS8; |
2515 |
break;
|
2516 |
case 7: |
2517 |
tty.c_cflag |= CS7; |
2518 |
break;
|
2519 |
case 6: |
2520 |
tty.c_cflag |= CS6; |
2521 |
break;
|
2522 |
case 5: |
2523 |
tty.c_cflag |= CS5; |
2524 |
break;
|
2525 |
} |
2526 |
switch(parity) {
|
2527 |
default:
|
2528 |
case 'N': |
2529 |
break;
|
2530 |
case 'E': |
2531 |
tty.c_cflag |= PARENB; |
2532 |
break;
|
2533 |
case 'O': |
2534 |
tty.c_cflag |= PARENB | PARODD; |
2535 |
break;
|
2536 |
} |
2537 |
if (stop_bits == 2) |
2538 |
tty.c_cflag |= CSTOPB; |
2539 |
|
2540 |
tcsetattr (fd, TCSANOW, &tty); |
2541 |
} |
2542 |
|
2543 |
static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2544 |
{ |
2545 |
FDCharDriver *s = chr->opaque; |
2546 |
|
2547 |
switch(cmd) {
|
2548 |
case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2549 |
{ |
2550 |
QEMUSerialSetParams *ssp = arg; |
2551 |
tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2552 |
ssp->data_bits, ssp->stop_bits); |
2553 |
} |
2554 |
break;
|
2555 |
case CHR_IOCTL_SERIAL_SET_BREAK:
|
2556 |
{ |
2557 |
int enable = *(int *)arg; |
2558 |
if (enable)
|
2559 |
tcsendbreak(s->fd_in, 1);
|
2560 |
} |
2561 |
break;
|
2562 |
default:
|
2563 |
return -ENOTSUP;
|
2564 |
} |
2565 |
return 0; |
2566 |
} |
2567 |
|
2568 |
static CharDriverState *qemu_chr_open_tty(const char *filename) |
2569 |
{ |
2570 |
CharDriverState *chr; |
2571 |
int fd;
|
2572 |
|
2573 |
TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2574 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
2575 |
tty_serial_init(fd, 115200, 'N', 8, 1); |
2576 |
chr = qemu_chr_open_fd(fd, fd); |
2577 |
if (!chr) {
|
2578 |
close(fd); |
2579 |
return NULL; |
2580 |
} |
2581 |
chr->chr_ioctl = tty_serial_ioctl; |
2582 |
qemu_chr_reset(chr); |
2583 |
return chr;
|
2584 |
} |
2585 |
#else /* ! __linux__ && ! __sun__ */ |
2586 |
static CharDriverState *qemu_chr_open_pty(void) |
2587 |
{ |
2588 |
return NULL; |
2589 |
} |
2590 |
#endif /* __linux__ || __sun__ */ |
2591 |
|
2592 |
#if defined(__linux__)
|
2593 |
typedef struct { |
2594 |
int fd;
|
2595 |
int mode;
|
2596 |
} ParallelCharDriver; |
2597 |
|
2598 |
static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2599 |
{ |
2600 |
if (s->mode != mode) {
|
2601 |
int m = mode;
|
2602 |
if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2603 |
return 0; |
2604 |
s->mode = mode; |
2605 |
} |
2606 |
return 1; |
2607 |
} |
2608 |
|
2609 |
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2610 |
{ |
2611 |
ParallelCharDriver *drv = chr->opaque; |
2612 |
int fd = drv->fd;
|
2613 |
uint8_t b; |
2614 |
|
2615 |
switch(cmd) {
|
2616 |
case CHR_IOCTL_PP_READ_DATA:
|
2617 |
if (ioctl(fd, PPRDATA, &b) < 0) |
2618 |
return -ENOTSUP;
|
2619 |
*(uint8_t *)arg = b; |
2620 |
break;
|
2621 |
case CHR_IOCTL_PP_WRITE_DATA:
|
2622 |
b = *(uint8_t *)arg; |
2623 |
if (ioctl(fd, PPWDATA, &b) < 0) |
2624 |
return -ENOTSUP;
|
2625 |
break;
|
2626 |
case CHR_IOCTL_PP_READ_CONTROL:
|
2627 |
if (ioctl(fd, PPRCONTROL, &b) < 0) |
2628 |
return -ENOTSUP;
|
2629 |
/* Linux gives only the lowest bits, and no way to know data
|
2630 |
direction! For better compatibility set the fixed upper
|
2631 |
bits. */
|
2632 |
*(uint8_t *)arg = b | 0xc0;
|
2633 |
break;
|
2634 |
case CHR_IOCTL_PP_WRITE_CONTROL:
|
2635 |
b = *(uint8_t *)arg; |
2636 |
if (ioctl(fd, PPWCONTROL, &b) < 0) |
2637 |
return -ENOTSUP;
|
2638 |
break;
|
2639 |
case CHR_IOCTL_PP_READ_STATUS:
|
2640 |
if (ioctl(fd, PPRSTATUS, &b) < 0) |
2641 |
return -ENOTSUP;
|
2642 |
*(uint8_t *)arg = b; |
2643 |
break;
|
2644 |
case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2645 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2646 |
struct ParallelIOArg *parg = arg;
|
2647 |
int n = read(fd, parg->buffer, parg->count);
|
2648 |
if (n != parg->count) {
|
2649 |
return -EIO;
|
2650 |
} |
2651 |
} |
2652 |
break;
|
2653 |
case CHR_IOCTL_PP_EPP_READ:
|
2654 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2655 |
struct ParallelIOArg *parg = arg;
|
2656 |
int n = read(fd, parg->buffer, parg->count);
|
2657 |
if (n != parg->count) {
|
2658 |
return -EIO;
|
2659 |
} |
2660 |
} |
2661 |
break;
|
2662 |
case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2663 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2664 |
struct ParallelIOArg *parg = arg;
|
2665 |
int n = write(fd, parg->buffer, parg->count);
|
2666 |
if (n != parg->count) {
|
2667 |
return -EIO;
|
2668 |
} |
2669 |
} |
2670 |
break;
|
2671 |
case CHR_IOCTL_PP_EPP_WRITE:
|
2672 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2673 |
struct ParallelIOArg *parg = arg;
|
2674 |
int n = write(fd, parg->buffer, parg->count);
|
2675 |
if (n != parg->count) {
|
2676 |
return -EIO;
|
2677 |
} |
2678 |
} |
2679 |
break;
|
2680 |
default:
|
2681 |
return -ENOTSUP;
|
2682 |
} |
2683 |
return 0; |
2684 |
} |
2685 |
|
2686 |
static void pp_close(CharDriverState *chr) |
2687 |
{ |
2688 |
ParallelCharDriver *drv = chr->opaque; |
2689 |
int fd = drv->fd;
|
2690 |
|
2691 |
pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2692 |
ioctl(fd, PPRELEASE); |
2693 |
close(fd); |
2694 |
qemu_free(drv); |
2695 |
} |
2696 |
|
2697 |
static CharDriverState *qemu_chr_open_pp(const char *filename) |
2698 |
{ |
2699 |
CharDriverState *chr; |
2700 |
ParallelCharDriver *drv; |
2701 |
int fd;
|
2702 |
|
2703 |
TFR(fd = open(filename, O_RDWR)); |
2704 |
if (fd < 0) |
2705 |
return NULL; |
2706 |
|
2707 |
if (ioctl(fd, PPCLAIM) < 0) { |
2708 |
close(fd); |
2709 |
return NULL; |
2710 |
} |
2711 |
|
2712 |
drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2713 |
if (!drv) {
|
2714 |
close(fd); |
2715 |
return NULL; |
2716 |
} |
2717 |
drv->fd = fd; |
2718 |
drv->mode = IEEE1284_MODE_COMPAT; |
2719 |
|
2720 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2721 |
if (!chr) {
|
2722 |
qemu_free(drv); |
2723 |
close(fd); |
2724 |
return NULL; |
2725 |
} |
2726 |
chr->chr_write = null_chr_write; |
2727 |
chr->chr_ioctl = pp_ioctl; |
2728 |
chr->chr_close = pp_close; |
2729 |
chr->opaque = drv; |
2730 |
|
2731 |
qemu_chr_reset(chr); |
2732 |
|
2733 |
return chr;
|
2734 |
} |
2735 |
#endif /* __linux__ */ |
2736 |
|
2737 |
#else /* _WIN32 */ |
2738 |
|
2739 |
typedef struct { |
2740 |
int max_size;
|
2741 |
HANDLE hcom, hrecv, hsend; |
2742 |
OVERLAPPED orecv, osend; |
2743 |
BOOL fpipe; |
2744 |
DWORD len; |
2745 |
} WinCharState; |
2746 |
|
2747 |
#define NSENDBUF 2048 |
2748 |
#define NRECVBUF 2048 |
2749 |
#define MAXCONNECT 1 |
2750 |
#define NTIMEOUT 5000 |
2751 |
|
2752 |
static int win_chr_poll(void *opaque); |
2753 |
static int win_chr_pipe_poll(void *opaque); |
2754 |
|
2755 |
static void win_chr_close(CharDriverState *chr) |
2756 |
{ |
2757 |
WinCharState *s = chr->opaque; |
2758 |
|
2759 |
if (s->hsend) {
|
2760 |
CloseHandle(s->hsend); |
2761 |
s->hsend = NULL;
|
2762 |
} |
2763 |
if (s->hrecv) {
|
2764 |
CloseHandle(s->hrecv); |
2765 |
s->hrecv = NULL;
|
2766 |
} |
2767 |
if (s->hcom) {
|
2768 |
CloseHandle(s->hcom); |
2769 |
s->hcom = NULL;
|
2770 |
} |
2771 |
if (s->fpipe)
|
2772 |
qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2773 |
else
|
2774 |
qemu_del_polling_cb(win_chr_poll, chr); |
2775 |
} |
2776 |
|
2777 |
static int win_chr_init(CharDriverState *chr, const char *filename) |
2778 |
{ |
2779 |
WinCharState *s = chr->opaque; |
2780 |
COMMCONFIG comcfg; |
2781 |
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2782 |
COMSTAT comstat; |
2783 |
DWORD size; |
2784 |
DWORD err; |
2785 |
|
2786 |
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2787 |
if (!s->hsend) {
|
2788 |
fprintf(stderr, "Failed CreateEvent\n");
|
2789 |
goto fail;
|
2790 |
} |
2791 |
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2792 |
if (!s->hrecv) {
|
2793 |
fprintf(stderr, "Failed CreateEvent\n");
|
2794 |
goto fail;
|
2795 |
} |
2796 |
|
2797 |
s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
2798 |
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
2799 |
if (s->hcom == INVALID_HANDLE_VALUE) {
|
2800 |
fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
2801 |
s->hcom = NULL;
|
2802 |
goto fail;
|
2803 |
} |
2804 |
|
2805 |
if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
2806 |
fprintf(stderr, "Failed SetupComm\n");
|
2807 |
goto fail;
|
2808 |
} |
2809 |
|
2810 |
ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
2811 |
size = sizeof(COMMCONFIG);
|
2812 |
GetDefaultCommConfig(filename, &comcfg, &size); |
2813 |
comcfg.dcb.DCBlength = sizeof(DCB);
|
2814 |
CommConfigDialog(filename, NULL, &comcfg);
|
2815 |
|
2816 |
if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
2817 |
fprintf(stderr, "Failed SetCommState\n");
|
2818 |
goto fail;
|
2819 |
} |
2820 |
|
2821 |
if (!SetCommMask(s->hcom, EV_ERR)) {
|
2822 |
fprintf(stderr, "Failed SetCommMask\n");
|
2823 |
goto fail;
|
2824 |
} |
2825 |
|
2826 |
cto.ReadIntervalTimeout = MAXDWORD; |
2827 |
if (!SetCommTimeouts(s->hcom, &cto)) {
|
2828 |
fprintf(stderr, "Failed SetCommTimeouts\n");
|
2829 |
goto fail;
|
2830 |
} |
2831 |
|
2832 |
if (!ClearCommError(s->hcom, &err, &comstat)) {
|
2833 |
fprintf(stderr, "Failed ClearCommError\n");
|
2834 |
goto fail;
|
2835 |
} |
2836 |
qemu_add_polling_cb(win_chr_poll, chr); |
2837 |
return 0; |
2838 |
|
2839 |
fail:
|
2840 |
win_chr_close(chr); |
2841 |
return -1; |
2842 |
} |
2843 |
|
2844 |
static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
2845 |
{ |
2846 |
WinCharState *s = chr->opaque; |
2847 |
DWORD len, ret, size, err; |
2848 |
|
2849 |
len = len1; |
2850 |
ZeroMemory(&s->osend, sizeof(s->osend));
|
2851 |
s->osend.hEvent = s->hsend; |
2852 |
while (len > 0) { |
2853 |
if (s->hsend)
|
2854 |
ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
2855 |
else
|
2856 |
ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
2857 |
if (!ret) {
|
2858 |
err = GetLastError(); |
2859 |
if (err == ERROR_IO_PENDING) {
|
2860 |
ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
2861 |
if (ret) {
|
2862 |
buf += size; |
2863 |
len -= size; |
2864 |
} else {
|
2865 |
break;
|
2866 |
} |
2867 |
} else {
|
2868 |
break;
|
2869 |
} |
2870 |
} else {
|
2871 |
buf += size; |
2872 |
len -= size; |
2873 |
} |
2874 |
} |
2875 |
return len1 - len;
|
2876 |
} |
2877 |
|
2878 |
static int win_chr_read_poll(CharDriverState *chr) |
2879 |
{ |
2880 |
WinCharState *s = chr->opaque; |
2881 |
|
2882 |
s->max_size = qemu_chr_can_read(chr); |
2883 |
return s->max_size;
|
2884 |
} |
2885 |
|
2886 |
static void win_chr_readfile(CharDriverState *chr) |
2887 |
{ |
2888 |
WinCharState *s = chr->opaque; |
2889 |
int ret, err;
|
2890 |
uint8_t buf[1024];
|
2891 |
DWORD size; |
2892 |
|
2893 |
ZeroMemory(&s->orecv, sizeof(s->orecv));
|
2894 |
s->orecv.hEvent = s->hrecv; |
2895 |
ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
2896 |
if (!ret) {
|
2897 |
err = GetLastError(); |
2898 |
if (err == ERROR_IO_PENDING) {
|
2899 |
ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
2900 |
} |
2901 |
} |
2902 |
|
2903 |
if (size > 0) { |
2904 |
qemu_chr_read(chr, buf, size); |
2905 |
} |
2906 |
} |
2907 |
|
2908 |
static void win_chr_read(CharDriverState *chr) |
2909 |
{ |
2910 |
WinCharState *s = chr->opaque; |
2911 |
|
2912 |
if (s->len > s->max_size)
|
2913 |
s->len = s->max_size; |
2914 |
if (s->len == 0) |
2915 |
return;
|
2916 |
|
2917 |
win_chr_readfile(chr); |
2918 |
} |
2919 |
|
2920 |
static int win_chr_poll(void *opaque) |
2921 |
{ |
2922 |
CharDriverState *chr = opaque; |
2923 |
WinCharState *s = chr->opaque; |
2924 |
COMSTAT status; |
2925 |
DWORD comerr; |
2926 |
|
2927 |
ClearCommError(s->hcom, &comerr, &status); |
2928 |
if (status.cbInQue > 0) { |
2929 |
s->len = status.cbInQue; |
2930 |
win_chr_read_poll(chr); |
2931 |
win_chr_read(chr); |
2932 |
return 1; |
2933 |
} |
2934 |
return 0; |
2935 |
} |
2936 |
|
2937 |
static CharDriverState *qemu_chr_open_win(const char *filename) |
2938 |
{ |
2939 |
CharDriverState *chr; |
2940 |
WinCharState *s; |
2941 |
|
2942 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2943 |
if (!chr)
|
2944 |
return NULL; |
2945 |
s = qemu_mallocz(sizeof(WinCharState));
|
2946 |
if (!s) {
|
2947 |
free(chr); |
2948 |
return NULL; |
2949 |
} |
2950 |
chr->opaque = s; |
2951 |
chr->chr_write = win_chr_write; |
2952 |
chr->chr_close = win_chr_close; |
2953 |
|
2954 |
if (win_chr_init(chr, filename) < 0) { |
2955 |
free(s); |
2956 |
free(chr); |
2957 |
return NULL; |
2958 |
} |
2959 |
qemu_chr_reset(chr); |
2960 |
return chr;
|
2961 |
} |
2962 |
|
2963 |
static int win_chr_pipe_poll(void *opaque) |
2964 |
{ |
2965 |
CharDriverState *chr = opaque; |
2966 |
WinCharState *s = chr->opaque; |
2967 |
DWORD size; |
2968 |
|
2969 |
PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
2970 |
if (size > 0) { |
2971 |
s->len = size; |
2972 |
win_chr_read_poll(chr); |
2973 |
win_chr_read(chr); |
2974 |
return 1; |
2975 |
} |
2976 |
return 0; |
2977 |
} |
2978 |
|
2979 |
static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
2980 |
{ |
2981 |
WinCharState *s = chr->opaque; |
2982 |
OVERLAPPED ov; |
2983 |
int ret;
|
2984 |
DWORD size; |
2985 |
char openname[256]; |
2986 |
|
2987 |
s->fpipe = TRUE; |
2988 |
|
2989 |
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2990 |
if (!s->hsend) {
|
2991 |
fprintf(stderr, "Failed CreateEvent\n");
|
2992 |
goto fail;
|
2993 |
} |
2994 |
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
2995 |
if (!s->hrecv) {
|
2996 |
fprintf(stderr, "Failed CreateEvent\n");
|
2997 |
goto fail;
|
2998 |
} |
2999 |
|
3000 |
snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
3001 |
s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
3002 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
3003 |
PIPE_WAIT, |
3004 |
MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
3005 |
if (s->hcom == INVALID_HANDLE_VALUE) {
|
3006 |
fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
3007 |
s->hcom = NULL;
|
3008 |
goto fail;
|
3009 |
} |
3010 |
|
3011 |
ZeroMemory(&ov, sizeof(ov));
|
3012 |
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
3013 |
ret = ConnectNamedPipe(s->hcom, &ov); |
3014 |
if (ret) {
|
3015 |
fprintf(stderr, "Failed ConnectNamedPipe\n");
|
3016 |
goto fail;
|
3017 |
} |
3018 |
|
3019 |
ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
3020 |
if (!ret) {
|
3021 |
fprintf(stderr, "Failed GetOverlappedResult\n");
|
3022 |
if (ov.hEvent) {
|
3023 |
CloseHandle(ov.hEvent); |
3024 |
ov.hEvent = NULL;
|
3025 |
} |
3026 |
goto fail;
|
3027 |
} |
3028 |
|
3029 |
if (ov.hEvent) {
|
3030 |
CloseHandle(ov.hEvent); |
3031 |
ov.hEvent = NULL;
|
3032 |
} |
3033 |
qemu_add_polling_cb(win_chr_pipe_poll, chr); |
3034 |
return 0; |
3035 |
|
3036 |
fail:
|
3037 |
win_chr_close(chr); |
3038 |
return -1; |
3039 |
} |
3040 |
|
3041 |
|
3042 |
static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
3043 |
{ |
3044 |
CharDriverState *chr; |
3045 |
WinCharState *s; |
3046 |
|
3047 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3048 |
if (!chr)
|
3049 |
return NULL; |
3050 |
s = qemu_mallocz(sizeof(WinCharState));
|
3051 |
if (!s) {
|
3052 |
free(chr); |
3053 |
return NULL; |
3054 |
} |
3055 |
chr->opaque = s; |
3056 |
chr->chr_write = win_chr_write; |
3057 |
chr->chr_close = win_chr_close; |
3058 |
|
3059 |
if (win_chr_pipe_init(chr, filename) < 0) { |
3060 |
free(s); |
3061 |
free(chr); |
3062 |
return NULL; |
3063 |
} |
3064 |
qemu_chr_reset(chr); |
3065 |
return chr;
|
3066 |
} |
3067 |
|
3068 |
static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
3069 |
{ |
3070 |
CharDriverState *chr; |
3071 |
WinCharState *s; |
3072 |
|
3073 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3074 |
if (!chr)
|
3075 |
return NULL; |
3076 |
s = qemu_mallocz(sizeof(WinCharState));
|
3077 |
if (!s) {
|
3078 |
free(chr); |
3079 |
return NULL; |
3080 |
} |
3081 |
s->hcom = fd_out; |
3082 |
chr->opaque = s; |
3083 |
chr->chr_write = win_chr_write; |
3084 |
qemu_chr_reset(chr); |
3085 |
return chr;
|
3086 |
} |
3087 |
|
3088 |
static CharDriverState *qemu_chr_open_win_con(const char *filename) |
3089 |
{ |
3090 |
return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
3091 |
} |
3092 |
|
3093 |
static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
3094 |
{ |
3095 |
HANDLE fd_out; |
3096 |
|
3097 |
fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
3098 |
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
3099 |
if (fd_out == INVALID_HANDLE_VALUE)
|
3100 |
return NULL; |
3101 |
|
3102 |
return qemu_chr_open_win_file(fd_out);
|
3103 |
} |
3104 |
#endif /* !_WIN32 */ |
3105 |
|
3106 |
/***********************************************************/
|
3107 |
/* UDP Net console */
|
3108 |
|
3109 |
typedef struct { |
3110 |
int fd;
|
3111 |
struct sockaddr_in daddr;
|
3112 |
uint8_t buf[1024];
|
3113 |
int bufcnt;
|
3114 |
int bufptr;
|
3115 |
int max_size;
|
3116 |
} NetCharDriver; |
3117 |
|
3118 |
static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3119 |
{ |
3120 |
NetCharDriver *s = chr->opaque; |
3121 |
|
3122 |
return sendto(s->fd, buf, len, 0, |
3123 |
(struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3124 |
} |
3125 |
|
3126 |
static int udp_chr_read_poll(void *opaque) |
3127 |
{ |
3128 |
CharDriverState *chr = opaque; |
3129 |
NetCharDriver *s = chr->opaque; |
3130 |
|
3131 |
s->max_size = qemu_chr_can_read(chr); |
3132 |
|
3133 |
/* If there were any stray characters in the queue process them
|
3134 |
* first
|
3135 |
*/
|
3136 |
while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3137 |
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3138 |
s->bufptr++; |
3139 |
s->max_size = qemu_chr_can_read(chr); |
3140 |
} |
3141 |
return s->max_size;
|
3142 |
} |
3143 |
|
3144 |
static void udp_chr_read(void *opaque) |
3145 |
{ |
3146 |
CharDriverState *chr = opaque; |
3147 |
NetCharDriver *s = chr->opaque; |
3148 |
|
3149 |
if (s->max_size == 0) |
3150 |
return;
|
3151 |
s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3152 |
s->bufptr = s->bufcnt; |
3153 |
if (s->bufcnt <= 0) |
3154 |
return;
|
3155 |
|
3156 |
s->bufptr = 0;
|
3157 |
while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3158 |
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3159 |
s->bufptr++; |
3160 |
s->max_size = qemu_chr_can_read(chr); |
3161 |
} |
3162 |
} |
3163 |
|
3164 |
static void udp_chr_update_read_handler(CharDriverState *chr) |
3165 |
{ |
3166 |
NetCharDriver *s = chr->opaque; |
3167 |
|
3168 |
if (s->fd >= 0) { |
3169 |
qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3170 |
udp_chr_read, NULL, chr);
|
3171 |
} |
3172 |
} |
3173 |
|
3174 |
int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3175 |
#ifndef _WIN32
|
3176 |
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3177 |
#endif
|
3178 |
int parse_host_src_port(struct sockaddr_in *haddr, |
3179 |
struct sockaddr_in *saddr,
|
3180 |
const char *str); |
3181 |
|
3182 |
static CharDriverState *qemu_chr_open_udp(const char *def) |
3183 |
{ |
3184 |
CharDriverState *chr = NULL;
|
3185 |
NetCharDriver *s = NULL;
|
3186 |
int fd = -1; |
3187 |
struct sockaddr_in saddr;
|
3188 |
|
3189 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3190 |
if (!chr)
|
3191 |
goto return_err;
|
3192 |
s = qemu_mallocz(sizeof(NetCharDriver));
|
3193 |
if (!s)
|
3194 |
goto return_err;
|
3195 |
|
3196 |
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3197 |
if (fd < 0) { |
3198 |
perror("socket(PF_INET, SOCK_DGRAM)");
|
3199 |
goto return_err;
|
3200 |
} |
3201 |
|
3202 |
if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3203 |
printf("Could not parse: %s\n", def);
|
3204 |
goto return_err;
|
3205 |
} |
3206 |
|
3207 |
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3208 |
{ |
3209 |
perror("bind");
|
3210 |
goto return_err;
|
3211 |
} |
3212 |
|
3213 |
s->fd = fd; |
3214 |
s->bufcnt = 0;
|
3215 |
s->bufptr = 0;
|
3216 |
chr->opaque = s; |
3217 |
chr->chr_write = udp_chr_write; |
3218 |
chr->chr_update_read_handler = udp_chr_update_read_handler; |
3219 |
return chr;
|
3220 |
|
3221 |
return_err:
|
3222 |
if (chr)
|
3223 |
free(chr); |
3224 |
if (s)
|
3225 |
free(s); |
3226 |
if (fd >= 0) |
3227 |
closesocket(fd); |
3228 |
return NULL; |
3229 |
} |
3230 |
|
3231 |
/***********************************************************/
|
3232 |
/* TCP Net console */
|
3233 |
|
3234 |
typedef struct { |
3235 |
int fd, listen_fd;
|
3236 |
int connected;
|
3237 |
int max_size;
|
3238 |
int do_telnetopt;
|
3239 |
int do_nodelay;
|
3240 |
int is_unix;
|
3241 |
} TCPCharDriver; |
3242 |
|
3243 |
static void tcp_chr_accept(void *opaque); |
3244 |
|
3245 |
static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3246 |
{ |
3247 |
TCPCharDriver *s = chr->opaque; |
3248 |
if (s->connected) {
|
3249 |
return send_all(s->fd, buf, len);
|
3250 |
} else {
|
3251 |
/* XXX: indicate an error ? */
|
3252 |
return len;
|
3253 |
} |
3254 |
} |
3255 |
|
3256 |
static int tcp_chr_read_poll(void *opaque) |
3257 |
{ |
3258 |
CharDriverState *chr = opaque; |
3259 |
TCPCharDriver *s = chr->opaque; |
3260 |
if (!s->connected)
|
3261 |
return 0; |
3262 |
s->max_size = qemu_chr_can_read(chr); |
3263 |
return s->max_size;
|
3264 |
} |
3265 |
|
3266 |
#define IAC 255 |
3267 |
#define IAC_BREAK 243 |
3268 |
static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3269 |
TCPCharDriver *s, |
3270 |
uint8_t *buf, int *size)
|
3271 |
{ |
3272 |
/* Handle any telnet client's basic IAC options to satisfy char by
|
3273 |
* char mode with no echo. All IAC options will be removed from
|
3274 |
* the buf and the do_telnetopt variable will be used to track the
|
3275 |
* state of the width of the IAC information.
|
3276 |
*
|
3277 |
* IAC commands come in sets of 3 bytes with the exception of the
|
3278 |
* "IAC BREAK" command and the double IAC.
|
3279 |
*/
|
3280 |
|
3281 |
int i;
|
3282 |
int j = 0; |
3283 |
|
3284 |
for (i = 0; i < *size; i++) { |
3285 |
if (s->do_telnetopt > 1) { |
3286 |
if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3287 |
/* Double IAC means send an IAC */
|
3288 |
if (j != i)
|
3289 |
buf[j] = buf[i]; |
3290 |
j++; |
3291 |
s->do_telnetopt = 1;
|
3292 |
} else {
|
3293 |
if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3294 |
/* Handle IAC break commands by sending a serial break */
|
3295 |
qemu_chr_event(chr, CHR_EVENT_BREAK); |
3296 |
s->do_telnetopt++; |
3297 |
} |
3298 |
s->do_telnetopt++; |
3299 |
} |
3300 |
if (s->do_telnetopt >= 4) { |
3301 |
s->do_telnetopt = 1;
|
3302 |
} |
3303 |
} else {
|
3304 |
if ((unsigned char)buf[i] == IAC) { |
3305 |
s->do_telnetopt = 2;
|
3306 |
} else {
|
3307 |
if (j != i)
|
3308 |
buf[j] = buf[i]; |
3309 |
j++; |
3310 |
} |
3311 |
} |
3312 |
} |
3313 |
*size = j; |
3314 |
} |
3315 |
|
3316 |
static void tcp_chr_read(void *opaque) |
3317 |
{ |
3318 |
CharDriverState *chr = opaque; |
3319 |
TCPCharDriver *s = chr->opaque; |
3320 |
uint8_t buf[1024];
|
3321 |
int len, size;
|
3322 |
|
3323 |
if (!s->connected || s->max_size <= 0) |
3324 |
return;
|
3325 |
len = sizeof(buf);
|
3326 |
if (len > s->max_size)
|
3327 |
len = s->max_size; |
3328 |
size = recv(s->fd, buf, len, 0);
|
3329 |
if (size == 0) { |
3330 |
/* connection closed */
|
3331 |
s->connected = 0;
|
3332 |
if (s->listen_fd >= 0) { |
3333 |
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3334 |
} |
3335 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3336 |
closesocket(s->fd); |
3337 |
s->fd = -1;
|
3338 |
} else if (size > 0) { |
3339 |
if (s->do_telnetopt)
|
3340 |
tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3341 |
if (size > 0) |
3342 |
qemu_chr_read(chr, buf, size); |
3343 |
} |
3344 |
} |
3345 |
|
3346 |
static void tcp_chr_connect(void *opaque) |
3347 |
{ |
3348 |
CharDriverState *chr = opaque; |
3349 |
TCPCharDriver *s = chr->opaque; |
3350 |
|
3351 |
s->connected = 1;
|
3352 |
qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3353 |
tcp_chr_read, NULL, chr);
|
3354 |
qemu_chr_reset(chr); |
3355 |
} |
3356 |
|
3357 |
#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3358 |
static void tcp_chr_telnet_init(int fd) |
3359 |
{ |
3360 |
char buf[3]; |
3361 |
/* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3362 |
IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3363 |
send(fd, (char *)buf, 3, 0); |
3364 |
IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3365 |
send(fd, (char *)buf, 3, 0); |
3366 |
IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3367 |
send(fd, (char *)buf, 3, 0); |
3368 |
IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3369 |
send(fd, (char *)buf, 3, 0); |
3370 |
} |
3371 |
|
3372 |
static void socket_set_nodelay(int fd) |
3373 |
{ |
3374 |
int val = 1; |
3375 |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3376 |
} |
3377 |
|
3378 |
static void tcp_chr_accept(void *opaque) |
3379 |
{ |
3380 |
CharDriverState *chr = opaque; |
3381 |
TCPCharDriver *s = chr->opaque; |
3382 |
struct sockaddr_in saddr;
|
3383 |
#ifndef _WIN32
|
3384 |
struct sockaddr_un uaddr;
|
3385 |
#endif
|
3386 |
struct sockaddr *addr;
|
3387 |
socklen_t len; |
3388 |
int fd;
|
3389 |
|
3390 |
for(;;) {
|
3391 |
#ifndef _WIN32
|
3392 |
if (s->is_unix) {
|
3393 |
len = sizeof(uaddr);
|
3394 |
addr = (struct sockaddr *)&uaddr;
|
3395 |
} else
|
3396 |
#endif
|
3397 |
{ |
3398 |
len = sizeof(saddr);
|
3399 |
addr = (struct sockaddr *)&saddr;
|
3400 |
} |
3401 |
fd = accept(s->listen_fd, addr, &len); |
3402 |
if (fd < 0 && errno != EINTR) { |
3403 |
return;
|
3404 |
} else if (fd >= 0) { |
3405 |
if (s->do_telnetopt)
|
3406 |
tcp_chr_telnet_init(fd); |
3407 |
break;
|
3408 |
} |
3409 |
} |
3410 |
socket_set_nonblock(fd); |
3411 |
if (s->do_nodelay)
|
3412 |
socket_set_nodelay(fd); |
3413 |
s->fd = fd; |
3414 |
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3415 |
tcp_chr_connect(chr); |
3416 |
} |
3417 |
|
3418 |
static void tcp_chr_close(CharDriverState *chr) |
3419 |
{ |
3420 |
TCPCharDriver *s = chr->opaque; |
3421 |
if (s->fd >= 0) |
3422 |
closesocket(s->fd); |
3423 |
if (s->listen_fd >= 0) |
3424 |
closesocket(s->listen_fd); |
3425 |
qemu_free(s); |
3426 |
} |
3427 |
|
3428 |
static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3429 |
int is_telnet,
|
3430 |
int is_unix)
|
3431 |
{ |
3432 |
CharDriverState *chr = NULL;
|
3433 |
TCPCharDriver *s = NULL;
|
3434 |
int fd = -1, ret, err, val; |
3435 |
int is_listen = 0; |
3436 |
int is_waitconnect = 1; |
3437 |
int do_nodelay = 0; |
3438 |
const char *ptr; |
3439 |
struct sockaddr_in saddr;
|
3440 |
#ifndef _WIN32
|
3441 |
struct sockaddr_un uaddr;
|
3442 |
#endif
|
3443 |
struct sockaddr *addr;
|
3444 |
socklen_t addrlen; |
3445 |
|
3446 |
#ifndef _WIN32
|
3447 |
if (is_unix) {
|
3448 |
addr = (struct sockaddr *)&uaddr;
|
3449 |
addrlen = sizeof(uaddr);
|
3450 |
if (parse_unix_path(&uaddr, host_str) < 0) |
3451 |
goto fail;
|
3452 |
} else
|
3453 |
#endif
|
3454 |
{ |
3455 |
addr = (struct sockaddr *)&saddr;
|
3456 |
addrlen = sizeof(saddr);
|
3457 |
if (parse_host_port(&saddr, host_str) < 0) |
3458 |
goto fail;
|
3459 |
} |
3460 |
|
3461 |
ptr = host_str; |
3462 |
while((ptr = strchr(ptr,','))) { |
3463 |
ptr++; |
3464 |
if (!strncmp(ptr,"server",6)) { |
3465 |
is_listen = 1;
|
3466 |
} else if (!strncmp(ptr,"nowait",6)) { |
3467 |
is_waitconnect = 0;
|
3468 |
} else if (!strncmp(ptr,"nodelay",6)) { |
3469 |
do_nodelay = 1;
|
3470 |
} else {
|
3471 |
printf("Unknown option: %s\n", ptr);
|
3472 |
goto fail;
|
3473 |
} |
3474 |
} |
3475 |
if (!is_listen)
|
3476 |
is_waitconnect = 0;
|
3477 |
|
3478 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3479 |
if (!chr)
|
3480 |
goto fail;
|
3481 |
s = qemu_mallocz(sizeof(TCPCharDriver));
|
3482 |
if (!s)
|
3483 |
goto fail;
|
3484 |
|
3485 |
#ifndef _WIN32
|
3486 |
if (is_unix)
|
3487 |
fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3488 |
else
|
3489 |
#endif
|
3490 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
3491 |
|
3492 |
if (fd < 0) |
3493 |
goto fail;
|
3494 |
|
3495 |
if (!is_waitconnect)
|
3496 |
socket_set_nonblock(fd); |
3497 |
|
3498 |
s->connected = 0;
|
3499 |
s->fd = -1;
|
3500 |
s->listen_fd = -1;
|
3501 |
s->is_unix = is_unix; |
3502 |
s->do_nodelay = do_nodelay && !is_unix; |
3503 |
|
3504 |
chr->opaque = s; |
3505 |
chr->chr_write = tcp_chr_write; |
3506 |
chr->chr_close = tcp_chr_close; |
3507 |
|
3508 |
if (is_listen) {
|
3509 |
/* allow fast reuse */
|
3510 |
#ifndef _WIN32
|
3511 |
if (is_unix) {
|
3512 |
char path[109]; |
3513 |
pstrcpy(path, sizeof(path), uaddr.sun_path);
|
3514 |
unlink(path); |
3515 |
} else
|
3516 |
#endif
|
3517 |
{ |
3518 |
val = 1;
|
3519 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3520 |
} |
3521 |
|
3522 |
ret = bind(fd, addr, addrlen); |
3523 |
if (ret < 0) |
3524 |
goto fail;
|
3525 |
|
3526 |
ret = listen(fd, 0);
|
3527 |
if (ret < 0) |
3528 |
goto fail;
|
3529 |
|
3530 |
s->listen_fd = fd; |
3531 |
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3532 |
if (is_telnet)
|
3533 |
s->do_telnetopt = 1;
|
3534 |
} else {
|
3535 |
for(;;) {
|
3536 |
ret = connect(fd, addr, addrlen); |
3537 |
if (ret < 0) { |
3538 |
err = socket_error(); |
3539 |
if (err == EINTR || err == EWOULDBLOCK) {
|
3540 |
} else if (err == EINPROGRESS) { |
3541 |
break;
|
3542 |
#ifdef _WIN32
|
3543 |
} else if (err == WSAEALREADY) { |
3544 |
break;
|
3545 |
#endif
|
3546 |
} else {
|
3547 |
goto fail;
|
3548 |
} |
3549 |
} else {
|
3550 |
s->connected = 1;
|
3551 |
break;
|
3552 |
} |
3553 |
} |
3554 |
s->fd = fd; |
3555 |
socket_set_nodelay(fd); |
3556 |
if (s->connected)
|
3557 |
tcp_chr_connect(chr); |
3558 |
else
|
3559 |
qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3560 |
} |
3561 |
|
3562 |
if (is_listen && is_waitconnect) {
|
3563 |
printf("QEMU waiting for connection on: %s\n", host_str);
|
3564 |
tcp_chr_accept(chr); |
3565 |
socket_set_nonblock(s->listen_fd); |
3566 |
} |
3567 |
|
3568 |
return chr;
|
3569 |
fail:
|
3570 |
if (fd >= 0) |
3571 |
closesocket(fd); |
3572 |
qemu_free(s); |
3573 |
qemu_free(chr); |
3574 |
return NULL; |
3575 |
} |
3576 |
|
3577 |
CharDriverState *qemu_chr_open(const char *filename) |
3578 |
{ |
3579 |
const char *p; |
3580 |
|
3581 |
if (!strcmp(filename, "vc")) { |
3582 |
return text_console_init(&display_state, 0); |
3583 |
} else if (strstart(filename, "vc:", &p)) { |
3584 |
return text_console_init(&display_state, p);
|
3585 |
} else if (!strcmp(filename, "null")) { |
3586 |
return qemu_chr_open_null();
|
3587 |
} else
|
3588 |
if (strstart(filename, "tcp:", &p)) { |
3589 |
return qemu_chr_open_tcp(p, 0, 0); |
3590 |
} else
|
3591 |
if (strstart(filename, "telnet:", &p)) { |
3592 |
return qemu_chr_open_tcp(p, 1, 0); |
3593 |
} else
|
3594 |
if (strstart(filename, "udp:", &p)) { |
3595 |
return qemu_chr_open_udp(p);
|
3596 |
} else
|
3597 |
if (strstart(filename, "mon:", &p)) { |
3598 |
CharDriverState *drv = qemu_chr_open(p); |
3599 |
if (drv) {
|
3600 |
drv = qemu_chr_open_mux(drv); |
3601 |
monitor_init(drv, !nographic); |
3602 |
return drv;
|
3603 |
} |
3604 |
printf("Unable to open driver: %s\n", p);
|
3605 |
return 0; |
3606 |
} else
|
3607 |
#ifndef _WIN32
|
3608 |
if (strstart(filename, "unix:", &p)) { |
3609 |
return qemu_chr_open_tcp(p, 0, 1); |
3610 |
} else if (strstart(filename, "file:", &p)) { |
3611 |
return qemu_chr_open_file_out(p);
|
3612 |
} else if (strstart(filename, "pipe:", &p)) { |
3613 |
return qemu_chr_open_pipe(p);
|
3614 |
} else if (!strcmp(filename, "pty")) { |
3615 |
return qemu_chr_open_pty();
|
3616 |
} else if (!strcmp(filename, "stdio")) { |
3617 |
return qemu_chr_open_stdio();
|
3618 |
} else
|
3619 |
#if defined(__linux__)
|
3620 |
if (strstart(filename, "/dev/parport", NULL)) { |
3621 |
return qemu_chr_open_pp(filename);
|
3622 |
} else
|
3623 |
#endif
|
3624 |
#if defined(__linux__) || defined(__sun__)
|
3625 |
if (strstart(filename, "/dev/", NULL)) { |
3626 |
return qemu_chr_open_tty(filename);
|
3627 |
} else
|
3628 |
#endif
|
3629 |
#else /* !_WIN32 */ |
3630 |
if (strstart(filename, "COM", NULL)) { |
3631 |
return qemu_chr_open_win(filename);
|
3632 |
} else
|
3633 |
if (strstart(filename, "pipe:", &p)) { |
3634 |
return qemu_chr_open_win_pipe(p);
|
3635 |
} else
|
3636 |
if (strstart(filename, "con:", NULL)) { |
3637 |
return qemu_chr_open_win_con(filename);
|
3638 |
} else
|
3639 |
if (strstart(filename, "file:", &p)) { |
3640 |
return qemu_chr_open_win_file_out(p);
|
3641 |
} else
|
3642 |
#endif
|
3643 |
#ifdef CONFIG_BRLAPI
|
3644 |
if (!strcmp(filename, "braille")) { |
3645 |
return chr_baum_init();
|
3646 |
} else
|
3647 |
#endif
|
3648 |
{ |
3649 |
return NULL; |
3650 |
} |
3651 |
} |
3652 |
|
3653 |
void qemu_chr_close(CharDriverState *chr)
|
3654 |
{ |
3655 |
if (chr->chr_close)
|
3656 |
chr->chr_close(chr); |
3657 |
qemu_free(chr); |
3658 |
} |
3659 |
|
3660 |
/***********************************************************/
|
3661 |
/* network device redirectors */
|
3662 |
|
3663 |
__attribute__ (( unused )) |
3664 |
static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3665 |
{ |
3666 |
int len, i, j, c;
|
3667 |
|
3668 |
for(i=0;i<size;i+=16) { |
3669 |
len = size - i; |
3670 |
if (len > 16) |
3671 |
len = 16;
|
3672 |
fprintf(f, "%08x ", i);
|
3673 |
for(j=0;j<16;j++) { |
3674 |
if (j < len)
|
3675 |
fprintf(f, " %02x", buf[i+j]);
|
3676 |
else
|
3677 |
fprintf(f, " ");
|
3678 |
} |
3679 |
fprintf(f, " ");
|
3680 |
for(j=0;j<len;j++) { |
3681 |
c = buf[i+j]; |
3682 |
if (c < ' ' || c > '~') |
3683 |
c = '.';
|
3684 |
fprintf(f, "%c", c);
|
3685 |
} |
3686 |
fprintf(f, "\n");
|
3687 |
} |
3688 |
} |
3689 |
|
3690 |
static int parse_macaddr(uint8_t *macaddr, const char *p) |
3691 |
{ |
3692 |
int i;
|
3693 |
char *last_char;
|
3694 |
long int offset; |
3695 |
|
3696 |
errno = 0;
|
3697 |
offset = strtol(p, &last_char, 0);
|
3698 |
if (0 == errno && '\0' == *last_char && |
3699 |
offset >= 0 && offset <= 0xFFFFFF) { |
3700 |
macaddr[3] = (offset & 0xFF0000) >> 16; |
3701 |
macaddr[4] = (offset & 0xFF00) >> 8; |
3702 |
macaddr[5] = offset & 0xFF; |
3703 |
return 0; |
3704 |
} else {
|
3705 |
for(i = 0; i < 6; i++) { |
3706 |
macaddr[i] = strtol(p, (char **)&p, 16); |
3707 |
if (i == 5) { |
3708 |
if (*p != '\0') |
3709 |
return -1; |
3710 |
} else {
|
3711 |
if (*p != ':' && *p != '-') |
3712 |
return -1; |
3713 |
p++; |
3714 |
} |
3715 |
} |
3716 |
return 0; |
3717 |
} |
3718 |
|
3719 |
return -1; |
3720 |
} |
3721 |
|
3722 |
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3723 |
{ |
3724 |
const char *p, *p1; |
3725 |
int len;
|
3726 |
p = *pp; |
3727 |
p1 = strchr(p, sep); |
3728 |
if (!p1)
|
3729 |
return -1; |
3730 |
len = p1 - p; |
3731 |
p1++; |
3732 |
if (buf_size > 0) { |
3733 |
if (len > buf_size - 1) |
3734 |
len = buf_size - 1;
|
3735 |
memcpy(buf, p, len); |
3736 |
buf[len] = '\0';
|
3737 |
} |
3738 |
*pp = p1; |
3739 |
return 0; |
3740 |
} |
3741 |
|
3742 |
int parse_host_src_port(struct sockaddr_in *haddr, |
3743 |
struct sockaddr_in *saddr,
|
3744 |
const char *input_str) |
3745 |
{ |
3746 |
char *str = strdup(input_str);
|
3747 |
char *host_str = str;
|
3748 |
char *src_str;
|
3749 |
char *ptr;
|
3750 |
|
3751 |
/*
|
3752 |
* Chop off any extra arguments at the end of the string which
|
3753 |
* would start with a comma, then fill in the src port information
|
3754 |
* if it was provided else use the "any address" and "any port".
|
3755 |
*/
|
3756 |
if ((ptr = strchr(str,','))) |
3757 |
*ptr = '\0';
|
3758 |
|
3759 |
if ((src_str = strchr(input_str,'@'))) { |
3760 |
*src_str = '\0';
|
3761 |
src_str++; |
3762 |
} |
3763 |
|
3764 |
if (parse_host_port(haddr, host_str) < 0) |
3765 |
goto fail;
|
3766 |
|
3767 |
if (!src_str || *src_str == '\0') |
3768 |
src_str = ":0";
|
3769 |
|
3770 |
if (parse_host_port(saddr, src_str) < 0) |
3771 |
goto fail;
|
3772 |
|
3773 |
free(str); |
3774 |
return(0); |
3775 |
|
3776 |
fail:
|
3777 |
free(str); |
3778 |
return -1; |
3779 |
} |
3780 |
|
3781 |
int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3782 |
{ |
3783 |
char buf[512]; |
3784 |
struct hostent *he;
|
3785 |
const char *p, *r; |
3786 |
int port;
|
3787 |
|
3788 |
p = str; |
3789 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3790 |
return -1; |
3791 |
saddr->sin_family = AF_INET; |
3792 |
if (buf[0] == '\0') { |
3793 |
saddr->sin_addr.s_addr = 0;
|
3794 |
} else {
|
3795 |
if (isdigit(buf[0])) { |
3796 |
if (!inet_aton(buf, &saddr->sin_addr))
|
3797 |
return -1; |
3798 |
} else {
|
3799 |
if ((he = gethostbyname(buf)) == NULL) |
3800 |
return - 1; |
3801 |
saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
3802 |
} |
3803 |
} |
3804 |
port = strtol(p, (char **)&r, 0); |
3805 |
if (r == p)
|
3806 |
return -1; |
3807 |
saddr->sin_port = htons(port); |
3808 |
return 0; |
3809 |
} |
3810 |
|
3811 |
#ifndef _WIN32
|
3812 |
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
3813 |
{ |
3814 |
const char *p; |
3815 |
int len;
|
3816 |
|
3817 |
len = MIN(108, strlen(str));
|
3818 |
p = strchr(str, ',');
|
3819 |
if (p)
|
3820 |
len = MIN(len, p - str); |
3821 |
|
3822 |
memset(uaddr, 0, sizeof(*uaddr)); |
3823 |
|
3824 |
uaddr->sun_family = AF_UNIX; |
3825 |
memcpy(uaddr->sun_path, str, len); |
3826 |
|
3827 |
return 0; |
3828 |
} |
3829 |
#endif
|
3830 |
|
3831 |
/* find or alloc a new VLAN */
|
3832 |
VLANState *qemu_find_vlan(int id)
|
3833 |
{ |
3834 |
VLANState **pvlan, *vlan; |
3835 |
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
3836 |
if (vlan->id == id)
|
3837 |
return vlan;
|
3838 |
} |
3839 |
vlan = qemu_mallocz(sizeof(VLANState));
|
3840 |
if (!vlan)
|
3841 |
return NULL; |
3842 |
vlan->id = id; |
3843 |
vlan->next = NULL;
|
3844 |
pvlan = &first_vlan; |
3845 |
while (*pvlan != NULL) |
3846 |
pvlan = &(*pvlan)->next; |
3847 |
*pvlan = vlan; |
3848 |
return vlan;
|
3849 |
} |
3850 |
|
3851 |
VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
3852 |
IOReadHandler *fd_read, |
3853 |
IOCanRWHandler *fd_can_read, |
3854 |
void *opaque)
|
3855 |
{ |
3856 |
VLANClientState *vc, **pvc; |
3857 |
vc = qemu_mallocz(sizeof(VLANClientState));
|
3858 |
if (!vc)
|
3859 |
return NULL; |
3860 |
vc->fd_read = fd_read; |
3861 |
vc->fd_can_read = fd_can_read; |
3862 |
vc->opaque = opaque; |
3863 |
vc->vlan = vlan; |
3864 |
|
3865 |
vc->next = NULL;
|
3866 |
pvc = &vlan->first_client; |
3867 |
while (*pvc != NULL) |
3868 |
pvc = &(*pvc)->next; |
3869 |
*pvc = vc; |
3870 |
return vc;
|
3871 |
} |
3872 |
|
3873 |
int qemu_can_send_packet(VLANClientState *vc1)
|
3874 |
{ |
3875 |
VLANState *vlan = vc1->vlan; |
3876 |
VLANClientState *vc; |
3877 |
|
3878 |
for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
3879 |
if (vc != vc1) {
|
3880 |
if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
3881 |
return 1; |
3882 |
} |
3883 |
} |
3884 |
return 0; |
3885 |
} |
3886 |
|
3887 |
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
3888 |
{ |
3889 |
VLANState *vlan = vc1->vlan; |
3890 |
VLANClientState *vc; |
3891 |
|
3892 |
#if 0
|
3893 |
printf("vlan %d send:\n", vlan->id);
|
3894 |
hex_dump(stdout, buf, size);
|
3895 |
#endif
|
3896 |
for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
3897 |
if (vc != vc1) {
|
3898 |
vc->fd_read(vc->opaque, buf, size); |
3899 |
} |
3900 |
} |
3901 |
} |
3902 |
|
3903 |
#if defined(CONFIG_SLIRP)
|
3904 |
|
3905 |
/* slirp network adapter */
|
3906 |
|
3907 |
static int slirp_inited; |
3908 |
static VLANClientState *slirp_vc;
|
3909 |
|
3910 |
int slirp_can_output(void) |
3911 |
{ |
3912 |
return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
3913 |
} |
3914 |
|
3915 |
void slirp_output(const uint8_t *pkt, int pkt_len) |
3916 |
{ |
3917 |
#if 0
|
3918 |
printf("slirp output:\n");
|
3919 |
hex_dump(stdout, pkt, pkt_len);
|
3920 |
#endif
|
3921 |
if (!slirp_vc)
|
3922 |
return;
|
3923 |
qemu_send_packet(slirp_vc, pkt, pkt_len); |
3924 |
} |
3925 |
|
3926 |
static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
3927 |
{ |
3928 |
#if 0
|
3929 |
printf("slirp input:\n");
|
3930 |
hex_dump(stdout, buf, size);
|
3931 |
#endif
|
3932 |
slirp_input(buf, size); |
3933 |
} |
3934 |
|
3935 |
static int net_slirp_init(VLANState *vlan) |
3936 |
{ |
3937 |
if (!slirp_inited) {
|
3938 |
slirp_inited = 1;
|
3939 |
slirp_init(); |
3940 |
} |
3941 |
slirp_vc = qemu_new_vlan_client(vlan, |
3942 |
slirp_receive, NULL, NULL); |
3943 |
snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
3944 |
return 0; |
3945 |
} |
3946 |
|
3947 |
static void net_slirp_redir(const char *redir_str) |
3948 |
{ |
3949 |
int is_udp;
|
3950 |
char buf[256], *r; |
3951 |
const char *p; |
3952 |
struct in_addr guest_addr;
|
3953 |
int host_port, guest_port;
|
3954 |
|
3955 |
if (!slirp_inited) {
|
3956 |
slirp_inited = 1;
|
3957 |
slirp_init(); |
3958 |
} |
3959 |
|
3960 |
p = redir_str; |
3961 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3962 |
goto fail;
|
3963 |
if (!strcmp(buf, "tcp")) { |
3964 |
is_udp = 0;
|
3965 |
} else if (!strcmp(buf, "udp")) { |
3966 |
is_udp = 1;
|
3967 |
} else {
|
3968 |
goto fail;
|
3969 |
} |
3970 |
|
3971 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3972 |
goto fail;
|
3973 |
host_port = strtol(buf, &r, 0);
|
3974 |
if (r == buf)
|
3975 |
goto fail;
|
3976 |
|
3977 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
3978 |
goto fail;
|
3979 |
if (buf[0] == '\0') { |
3980 |
pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
3981 |
} |
3982 |
if (!inet_aton(buf, &guest_addr))
|
3983 |
goto fail;
|
3984 |
|
3985 |
guest_port = strtol(p, &r, 0);
|
3986 |
if (r == p)
|
3987 |
goto fail;
|
3988 |
|
3989 |
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
3990 |
fprintf(stderr, "qemu: could not set up redirection\n");
|
3991 |
exit(1);
|
3992 |
} |
3993 |
return;
|
3994 |
fail:
|
3995 |
fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
3996 |
exit(1);
|
3997 |
} |
3998 |
|
3999 |
#ifndef _WIN32
|
4000 |
|
4001 |
char smb_dir[1024]; |
4002 |
|
4003 |
static void erase_dir(char *dir_name) |
4004 |
{ |
4005 |
DIR *d; |
4006 |
struct dirent *de;
|
4007 |
char filename[1024]; |
4008 |
|
4009 |
/* erase all the files in the directory */
|
4010 |
if ((d = opendir(dir_name)) != 0) { |
4011 |
for(;;) {
|
4012 |
de = readdir(d); |
4013 |
if (!de)
|
4014 |
break;
|
4015 |
if (strcmp(de->d_name, ".") != 0 && |
4016 |
strcmp(de->d_name, "..") != 0) { |
4017 |
snprintf(filename, sizeof(filename), "%s/%s", |
4018 |
smb_dir, de->d_name); |
4019 |
if (unlink(filename) != 0) /* is it a directory? */ |
4020 |
erase_dir(filename); |
4021 |
} |
4022 |
} |
4023 |
closedir(d); |
4024 |
rmdir(dir_name); |
4025 |
} |
4026 |
} |
4027 |
|
4028 |
/* automatic user mode samba server configuration */
|
4029 |
static void smb_exit(void) |
4030 |
{ |
4031 |
erase_dir(smb_dir); |
4032 |
} |
4033 |
|
4034 |
/* automatic user mode samba server configuration */
|
4035 |
static void net_slirp_smb(const char *exported_dir) |
4036 |
{ |
4037 |
char smb_conf[1024]; |
4038 |
char smb_cmdline[1024]; |
4039 |
FILE *f; |
4040 |
|
4041 |
if (!slirp_inited) {
|
4042 |
slirp_inited = 1;
|
4043 |
slirp_init(); |
4044 |
} |
4045 |
|
4046 |
/* XXX: better tmp dir construction */
|
4047 |
snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
4048 |
if (mkdir(smb_dir, 0700) < 0) { |
4049 |
fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
4050 |
exit(1);
|
4051 |
} |
4052 |
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
4053 |
|
4054 |
f = fopen(smb_conf, "w");
|
4055 |
if (!f) {
|
4056 |
fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
4057 |
exit(1);
|
4058 |
} |
4059 |
fprintf(f, |
4060 |
"[global]\n"
|
4061 |
"private dir=%s\n"
|
4062 |
"smb ports=0\n"
|
4063 |
"socket address=127.0.0.1\n"
|
4064 |
"pid directory=%s\n"
|
4065 |
"lock directory=%s\n"
|
4066 |
"log file=%s/log.smbd\n"
|
4067 |
"smb passwd file=%s/smbpasswd\n"
|
4068 |
"security = share\n"
|
4069 |
"[qemu]\n"
|
4070 |
"path=%s\n"
|
4071 |
"read only=no\n"
|
4072 |
"guest ok=yes\n",
|
4073 |
smb_dir, |
4074 |
smb_dir, |
4075 |
smb_dir, |
4076 |
smb_dir, |
4077 |
smb_dir, |
4078 |
exported_dir |
4079 |
); |
4080 |
fclose(f); |
4081 |
atexit(smb_exit); |
4082 |
|
4083 |
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
4084 |
SMBD_COMMAND, smb_conf); |
4085 |
|
4086 |
slirp_add_exec(0, smb_cmdline, 4, 139); |
4087 |
} |
4088 |
|
4089 |
#endif /* !defined(_WIN32) */ |
4090 |
void do_info_slirp(void) |
4091 |
{ |
4092 |
slirp_stats(); |
4093 |
} |
4094 |
|
4095 |
#endif /* CONFIG_SLIRP */ |
4096 |
|
4097 |
#if !defined(_WIN32)
|
4098 |
|
4099 |
typedef struct TAPState { |
4100 |
VLANClientState *vc; |
4101 |
int fd;
|
4102 |
char down_script[1024]; |
4103 |
} TAPState; |
4104 |
|
4105 |
static void tap_receive(void *opaque, const uint8_t *buf, int size) |
4106 |
{ |
4107 |
TAPState *s = opaque; |
4108 |
int ret;
|
4109 |
for(;;) {
|
4110 |
ret = write(s->fd, buf, size); |
4111 |
if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
4112 |
} else {
|
4113 |
break;
|
4114 |
} |
4115 |
} |
4116 |
} |
4117 |
|
4118 |
static void tap_send(void *opaque) |
4119 |
{ |
4120 |
TAPState *s = opaque; |
4121 |
uint8_t buf[4096];
|
4122 |
int size;
|
4123 |
|
4124 |
#ifdef __sun__
|
4125 |
struct strbuf sbuf;
|
4126 |
int f = 0; |
4127 |
sbuf.maxlen = sizeof(buf);
|
4128 |
sbuf.buf = buf; |
4129 |
size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4130 |
#else
|
4131 |
size = read(s->fd, buf, sizeof(buf));
|
4132 |
#endif
|
4133 |
if (size > 0) { |
4134 |
qemu_send_packet(s->vc, buf, size); |
4135 |
} |
4136 |
} |
4137 |
|
4138 |
/* fd support */
|
4139 |
|
4140 |
static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4141 |
{ |
4142 |
TAPState *s; |
4143 |
|
4144 |
s = qemu_mallocz(sizeof(TAPState));
|
4145 |
if (!s)
|
4146 |
return NULL; |
4147 |
s->fd = fd; |
4148 |
s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4149 |
qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4150 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4151 |
return s;
|
4152 |
} |
4153 |
|
4154 |
#if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4155 |
static int tap_open(char *ifname, int ifname_size) |
4156 |
{ |
4157 |
int fd;
|
4158 |
char *dev;
|
4159 |
struct stat s;
|
4160 |
|
4161 |
TFR(fd = open("/dev/tap", O_RDWR));
|
4162 |
if (fd < 0) { |
4163 |
fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4164 |
return -1; |
4165 |
} |
4166 |
|
4167 |
fstat(fd, &s); |
4168 |
dev = devname(s.st_rdev, S_IFCHR); |
4169 |
pstrcpy(ifname, ifname_size, dev); |
4170 |
|
4171 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
4172 |
return fd;
|
4173 |
} |
4174 |
#elif defined(__sun__)
|
4175 |
#define TUNNEWPPA (('T'<<16) | 0x0001) |
4176 |
/*
|
4177 |
* Allocate TAP device, returns opened fd.
|
4178 |
* Stores dev name in the first arg(must be large enough).
|
4179 |
*/
|
4180 |
int tap_alloc(char *dev) |
4181 |
{ |
4182 |
int tap_fd, if_fd, ppa = -1; |
4183 |
static int ip_fd = 0; |
4184 |
char *ptr;
|
4185 |
|
4186 |
static int arp_fd = 0; |
4187 |
int ip_muxid, arp_muxid;
|
4188 |
struct strioctl strioc_if, strioc_ppa;
|
4189 |
int link_type = I_PLINK;;
|
4190 |
struct lifreq ifr;
|
4191 |
char actual_name[32] = ""; |
4192 |
|
4193 |
memset(&ifr, 0x0, sizeof(ifr)); |
4194 |
|
4195 |
if( *dev ){
|
4196 |
ptr = dev; |
4197 |
while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4198 |
ppa = atoi(ptr); |
4199 |
} |
4200 |
|
4201 |
/* Check if IP device was opened */
|
4202 |
if( ip_fd )
|
4203 |
close(ip_fd); |
4204 |
|
4205 |
TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4206 |
if (ip_fd < 0) { |
4207 |
syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4208 |
return -1; |
4209 |
} |
4210 |
|
4211 |
TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4212 |
if (tap_fd < 0) { |
4213 |
syslog(LOG_ERR, "Can't open /dev/tap");
|
4214 |
return -1; |
4215 |
} |
4216 |
|
4217 |
/* Assign a new PPA and get its unit number. */
|
4218 |
strioc_ppa.ic_cmd = TUNNEWPPA; |
4219 |
strioc_ppa.ic_timout = 0;
|
4220 |
strioc_ppa.ic_len = sizeof(ppa);
|
4221 |
strioc_ppa.ic_dp = (char *)&ppa;
|
4222 |
if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4223 |
syslog (LOG_ERR, "Can't assign new interface");
|
4224 |
|
4225 |
TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4226 |
if (if_fd < 0) { |
4227 |
syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4228 |
return -1; |
4229 |
} |
4230 |
if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4231 |
syslog(LOG_ERR, "Can't push IP module");
|
4232 |
return -1; |
4233 |
} |
4234 |
|
4235 |
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4236 |
syslog(LOG_ERR, "Can't get flags\n");
|
4237 |
|
4238 |
snprintf (actual_name, 32, "tap%d", ppa); |
4239 |
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4240 |
|
4241 |
ifr.lifr_ppa = ppa; |
4242 |
/* Assign ppa according to the unit number returned by tun device */
|
4243 |
|
4244 |
if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4245 |
syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4246 |
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4247 |
syslog (LOG_ERR, "Can't get flags\n");
|
4248 |
/* Push arp module to if_fd */
|
4249 |
if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4250 |
syslog (LOG_ERR, "Can't push ARP module (2)");
|
4251 |
|
4252 |
/* Push arp module to ip_fd */
|
4253 |
if (ioctl (ip_fd, I_POP, NULL) < 0) |
4254 |
syslog (LOG_ERR, "I_POP failed\n");
|
4255 |
if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4256 |
syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4257 |
/* Open arp_fd */
|
4258 |
TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4259 |
if (arp_fd < 0) |
4260 |
syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4261 |
|
4262 |
/* Set ifname to arp */
|
4263 |
strioc_if.ic_cmd = SIOCSLIFNAME; |
4264 |
strioc_if.ic_timout = 0;
|
4265 |
strioc_if.ic_len = sizeof(ifr);
|
4266 |
strioc_if.ic_dp = (char *)𝔦
|
4267 |
if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4268 |
syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4269 |
} |
4270 |
|
4271 |
if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4272 |
syslog(LOG_ERR, "Can't link TAP device to IP");
|
4273 |
return -1; |
4274 |
} |
4275 |
|
4276 |
if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4277 |
syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4278 |
|
4279 |
close (if_fd); |
4280 |
|
4281 |
memset(&ifr, 0x0, sizeof(ifr)); |
4282 |
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4283 |
ifr.lifr_ip_muxid = ip_muxid; |
4284 |
ifr.lifr_arp_muxid = arp_muxid; |
4285 |
|
4286 |
if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4287 |
{ |
4288 |
ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4289 |
ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4290 |
syslog (LOG_ERR, "Can't set multiplexor id");
|
4291 |
} |
4292 |
|
4293 |
sprintf(dev, "tap%d", ppa);
|
4294 |
return tap_fd;
|
4295 |
} |
4296 |
|
4297 |
static int tap_open(char *ifname, int ifname_size) |
4298 |
{ |
4299 |
char dev[10]=""; |
4300 |
int fd;
|
4301 |
if( (fd = tap_alloc(dev)) < 0 ){ |
4302 |
fprintf(stderr, "Cannot allocate TAP device\n");
|
4303 |
return -1; |
4304 |
} |
4305 |
pstrcpy(ifname, ifname_size, dev); |
4306 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
4307 |
return fd;
|
4308 |
} |
4309 |
#else
|
4310 |
static int tap_open(char *ifname, int ifname_size) |
4311 |
{ |
4312 |
struct ifreq ifr;
|
4313 |
int fd, ret;
|
4314 |
|
4315 |
TFR(fd = open("/dev/net/tun", O_RDWR));
|
4316 |
if (fd < 0) { |
4317 |
fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4318 |
return -1; |
4319 |
} |
4320 |
memset(&ifr, 0, sizeof(ifr)); |
4321 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4322 |
if (ifname[0] != '\0') |
4323 |
pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4324 |
else
|
4325 |
pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4326 |
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4327 |
if (ret != 0) { |
4328 |
fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4329 |
close(fd); |
4330 |
return -1; |
4331 |
} |
4332 |
pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4333 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
4334 |
return fd;
|
4335 |
} |
4336 |
#endif
|
4337 |
|
4338 |
static int launch_script(const char *setup_script, const char *ifname, int fd) |
4339 |
{ |
4340 |
int pid, status;
|
4341 |
char *args[3]; |
4342 |
char **parg;
|
4343 |
|
4344 |
/* try to launch network script */
|
4345 |
pid = fork(); |
4346 |
if (pid >= 0) { |
4347 |
if (pid == 0) { |
4348 |
int open_max = sysconf (_SC_OPEN_MAX), i;
|
4349 |
for (i = 0; i < open_max; i++) |
4350 |
if (i != STDIN_FILENO &&
|
4351 |
i != STDOUT_FILENO && |
4352 |
i != STDERR_FILENO && |
4353 |
i != fd) |
4354 |
close(i); |
4355 |
|
4356 |
parg = args; |
4357 |
*parg++ = (char *)setup_script;
|
4358 |
*parg++ = (char *)ifname;
|
4359 |
*parg++ = NULL;
|
4360 |
execv(setup_script, args); |
4361 |
_exit(1);
|
4362 |
} |
4363 |
while (waitpid(pid, &status, 0) != pid); |
4364 |
if (!WIFEXITED(status) ||
|
4365 |
WEXITSTATUS(status) != 0) {
|
4366 |
fprintf(stderr, "%s: could not launch network script\n",
|
4367 |
setup_script); |
4368 |
return -1; |
4369 |
} |
4370 |
} |
4371 |
return 0; |
4372 |
} |
4373 |
|
4374 |
static int net_tap_init(VLANState *vlan, const char *ifname1, |
4375 |
const char *setup_script, const char *down_script) |
4376 |
{ |
4377 |
TAPState *s; |
4378 |
int fd;
|
4379 |
char ifname[128]; |
4380 |
|
4381 |
if (ifname1 != NULL) |
4382 |
pstrcpy(ifname, sizeof(ifname), ifname1);
|
4383 |
else
|
4384 |
ifname[0] = '\0'; |
4385 |
TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4386 |
if (fd < 0) |
4387 |
return -1; |
4388 |
|
4389 |
if (!setup_script || !strcmp(setup_script, "no")) |
4390 |
setup_script = "";
|
4391 |
if (setup_script[0] != '\0') { |
4392 |
if (launch_script(setup_script, ifname, fd))
|
4393 |
return -1; |
4394 |
} |
4395 |
s = net_tap_fd_init(vlan, fd); |
4396 |
if (!s)
|
4397 |
return -1; |
4398 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4399 |
"tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4400 |
if (down_script && strcmp(down_script, "no")) |
4401 |
snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4402 |
return 0; |
4403 |
} |
4404 |
|
4405 |
#endif /* !_WIN32 */ |
4406 |
|
4407 |
/* network connection */
|
4408 |
typedef struct NetSocketState { |
4409 |
VLANClientState *vc; |
4410 |
int fd;
|
4411 |
int state; /* 0 = getting length, 1 = getting data */ |
4412 |
int index;
|
4413 |
int packet_len;
|
4414 |
uint8_t buf[4096];
|
4415 |
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4416 |
} NetSocketState; |
4417 |
|
4418 |
typedef struct NetSocketListenState { |
4419 |
VLANState *vlan; |
4420 |
int fd;
|
4421 |
} NetSocketListenState; |
4422 |
|
4423 |
/* XXX: we consider we can send the whole packet without blocking */
|
4424 |
static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4425 |
{ |
4426 |
NetSocketState *s = opaque; |
4427 |
uint32_t len; |
4428 |
len = htonl(size); |
4429 |
|
4430 |
send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4431 |
send_all(s->fd, buf, size); |
4432 |
} |
4433 |
|
4434 |
static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4435 |
{ |
4436 |
NetSocketState *s = opaque; |
4437 |
sendto(s->fd, buf, size, 0,
|
4438 |
(struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4439 |
} |
4440 |
|
4441 |
static void net_socket_send(void *opaque) |
4442 |
{ |
4443 |
NetSocketState *s = opaque; |
4444 |
int l, size, err;
|
4445 |
uint8_t buf1[4096];
|
4446 |
const uint8_t *buf;
|
4447 |
|
4448 |
size = recv(s->fd, buf1, sizeof(buf1), 0); |
4449 |
if (size < 0) { |
4450 |
err = socket_error(); |
4451 |
if (err != EWOULDBLOCK)
|
4452 |
goto eoc;
|
4453 |
} else if (size == 0) { |
4454 |
/* end of connection */
|
4455 |
eoc:
|
4456 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4457 |
closesocket(s->fd); |
4458 |
return;
|
4459 |
} |
4460 |
buf = buf1; |
4461 |
while (size > 0) { |
4462 |
/* reassemble a packet from the network */
|
4463 |
switch(s->state) {
|
4464 |
case 0: |
4465 |
l = 4 - s->index;
|
4466 |
if (l > size)
|
4467 |
l = size; |
4468 |
memcpy(s->buf + s->index, buf, l); |
4469 |
buf += l; |
4470 |
size -= l; |
4471 |
s->index += l; |
4472 |
if (s->index == 4) { |
4473 |
/* got length */
|
4474 |
s->packet_len = ntohl(*(uint32_t *)s->buf); |
4475 |
s->index = 0;
|
4476 |
s->state = 1;
|
4477 |
} |
4478 |
break;
|
4479 |
case 1: |
4480 |
l = s->packet_len - s->index; |
4481 |
if (l > size)
|
4482 |
l = size; |
4483 |
memcpy(s->buf + s->index, buf, l); |
4484 |
s->index += l; |
4485 |
buf += l; |
4486 |
size -= l; |
4487 |
if (s->index >= s->packet_len) {
|
4488 |
qemu_send_packet(s->vc, s->buf, s->packet_len); |
4489 |
s->index = 0;
|
4490 |
s->state = 0;
|
4491 |
} |
4492 |
break;
|
4493 |
} |
4494 |
} |
4495 |
} |
4496 |
|
4497 |
static void net_socket_send_dgram(void *opaque) |
4498 |
{ |
4499 |
NetSocketState *s = opaque; |
4500 |
int size;
|
4501 |
|
4502 |
size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4503 |
if (size < 0) |
4504 |
return;
|
4505 |
if (size == 0) { |
4506 |
/* end of connection */
|
4507 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4508 |
return;
|
4509 |
} |
4510 |
qemu_send_packet(s->vc, s->buf, size); |
4511 |
} |
4512 |
|
4513 |
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4514 |
{ |
4515 |
struct ip_mreq imr;
|
4516 |
int fd;
|
4517 |
int val, ret;
|
4518 |
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4519 |
fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4520 |
inet_ntoa(mcastaddr->sin_addr), |
4521 |
(int)ntohl(mcastaddr->sin_addr.s_addr));
|
4522 |
return -1; |
4523 |
|
4524 |
} |
4525 |
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4526 |
if (fd < 0) { |
4527 |
perror("socket(PF_INET, SOCK_DGRAM)");
|
4528 |
return -1; |
4529 |
} |
4530 |
|
4531 |
val = 1;
|
4532 |
ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4533 |
(const char *)&val, sizeof(val)); |
4534 |
if (ret < 0) { |
4535 |
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4536 |
goto fail;
|
4537 |
} |
4538 |
|
4539 |
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4540 |
if (ret < 0) { |
4541 |
perror("bind");
|
4542 |
goto fail;
|
4543 |
} |
4544 |
|
4545 |
/* Add host to multicast group */
|
4546 |
imr.imr_multiaddr = mcastaddr->sin_addr; |
4547 |
imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4548 |
|
4549 |
ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4550 |
(const char *)&imr, sizeof(struct ip_mreq)); |
4551 |
if (ret < 0) { |
4552 |
perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4553 |
goto fail;
|
4554 |
} |
4555 |
|
4556 |
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4557 |
val = 1;
|
4558 |
ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4559 |
(const char *)&val, sizeof(val)); |
4560 |
if (ret < 0) { |
4561 |
perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4562 |
goto fail;
|
4563 |
} |
4564 |
|
4565 |
socket_set_nonblock(fd); |
4566 |
return fd;
|
4567 |
fail:
|
4568 |
if (fd >= 0) |
4569 |
closesocket(fd); |
4570 |
return -1; |
4571 |
} |
4572 |
|
4573 |
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4574 |
int is_connected)
|
4575 |
{ |
4576 |
struct sockaddr_in saddr;
|
4577 |
int newfd;
|
4578 |
socklen_t saddr_len; |
4579 |
NetSocketState *s; |
4580 |
|
4581 |
/* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4582 |
* Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4583 |
* by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4584 |
*/
|
4585 |
|
4586 |
if (is_connected) {
|
4587 |
if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4588 |
/* must be bound */
|
4589 |
if (saddr.sin_addr.s_addr==0) { |
4590 |
fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4591 |
fd); |
4592 |
return NULL; |
4593 |
} |
4594 |
/* clone dgram socket */
|
4595 |
newfd = net_socket_mcast_create(&saddr); |
4596 |
if (newfd < 0) { |
4597 |
/* error already reported by net_socket_mcast_create() */
|
4598 |
close(fd); |
4599 |
return NULL; |
4600 |
} |
4601 |
/* clone newfd to fd, close newfd */
|
4602 |
dup2(newfd, fd); |
4603 |
close(newfd); |
4604 |
|
4605 |
} else {
|
4606 |
fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4607 |
fd, strerror(errno)); |
4608 |
return NULL; |
4609 |
} |
4610 |
} |
4611 |
|
4612 |
s = qemu_mallocz(sizeof(NetSocketState));
|
4613 |
if (!s)
|
4614 |
return NULL; |
4615 |
s->fd = fd; |
4616 |
|
4617 |
s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4618 |
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4619 |
|
4620 |
/* mcast: save bound address as dst */
|
4621 |
if (is_connected) s->dgram_dst=saddr;
|
4622 |
|
4623 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4624 |
"socket: fd=%d (%s mcast=%s:%d)",
|
4625 |
fd, is_connected? "cloned" : "", |
4626 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4627 |
return s;
|
4628 |
} |
4629 |
|
4630 |
static void net_socket_connect(void *opaque) |
4631 |
{ |
4632 |
NetSocketState *s = opaque; |
4633 |
qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4634 |
} |
4635 |
|
4636 |
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4637 |
int is_connected)
|
4638 |
{ |
4639 |
NetSocketState *s; |
4640 |
s = qemu_mallocz(sizeof(NetSocketState));
|
4641 |
if (!s)
|
4642 |
return NULL; |
4643 |
s->fd = fd; |
4644 |
s->vc = qemu_new_vlan_client(vlan, |
4645 |
net_socket_receive, NULL, s);
|
4646 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4647 |
"socket: fd=%d", fd);
|
4648 |
if (is_connected) {
|
4649 |
net_socket_connect(s); |
4650 |
} else {
|
4651 |
qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4652 |
} |
4653 |
return s;
|
4654 |
} |
4655 |
|
4656 |
static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4657 |
int is_connected)
|
4658 |
{ |
4659 |
int so_type=-1, optlen=sizeof(so_type); |
4660 |
|
4661 |
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4662 |
(socklen_t *)&optlen)< 0) {
|
4663 |
fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4664 |
return NULL; |
4665 |
} |
4666 |
switch(so_type) {
|
4667 |
case SOCK_DGRAM:
|
4668 |
return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4669 |
case SOCK_STREAM:
|
4670 |
return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4671 |
default:
|
4672 |
/* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4673 |
fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4674 |
return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4675 |
} |
4676 |
return NULL; |
4677 |
} |
4678 |
|
4679 |
static void net_socket_accept(void *opaque) |
4680 |
{ |
4681 |
NetSocketListenState *s = opaque; |
4682 |
NetSocketState *s1; |
4683 |
struct sockaddr_in saddr;
|
4684 |
socklen_t len; |
4685 |
int fd;
|
4686 |
|
4687 |
for(;;) {
|
4688 |
len = sizeof(saddr);
|
4689 |
fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4690 |
if (fd < 0 && errno != EINTR) { |
4691 |
return;
|
4692 |
} else if (fd >= 0) { |
4693 |
break;
|
4694 |
} |
4695 |
} |
4696 |
s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4697 |
if (!s1) {
|
4698 |
closesocket(fd); |
4699 |
} else {
|
4700 |
snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4701 |
"socket: connection from %s:%d",
|
4702 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4703 |
} |
4704 |
} |
4705 |
|
4706 |
static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4707 |
{ |
4708 |
NetSocketListenState *s; |
4709 |
int fd, val, ret;
|
4710 |
struct sockaddr_in saddr;
|
4711 |
|
4712 |
if (parse_host_port(&saddr, host_str) < 0) |
4713 |
return -1; |
4714 |
|
4715 |
s = qemu_mallocz(sizeof(NetSocketListenState));
|
4716 |
if (!s)
|
4717 |
return -1; |
4718 |
|
4719 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
4720 |
if (fd < 0) { |
4721 |
perror("socket");
|
4722 |
return -1; |
4723 |
} |
4724 |
socket_set_nonblock(fd); |
4725 |
|
4726 |
/* allow fast reuse */
|
4727 |
val = 1;
|
4728 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
4729 |
|
4730 |
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4731 |
if (ret < 0) { |
4732 |
perror("bind");
|
4733 |
return -1; |
4734 |
} |
4735 |
ret = listen(fd, 0);
|
4736 |
if (ret < 0) { |
4737 |
perror("listen");
|
4738 |
return -1; |
4739 |
} |
4740 |
s->vlan = vlan; |
4741 |
s->fd = fd; |
4742 |
qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
4743 |
return 0; |
4744 |
} |
4745 |
|
4746 |
static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
4747 |
{ |
4748 |
NetSocketState *s; |
4749 |
int fd, connected, ret, err;
|
4750 |
struct sockaddr_in saddr;
|
4751 |
|
4752 |
if (parse_host_port(&saddr, host_str) < 0) |
4753 |
return -1; |
4754 |
|
4755 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
4756 |
if (fd < 0) { |
4757 |
perror("socket");
|
4758 |
return -1; |
4759 |
} |
4760 |
socket_set_nonblock(fd); |
4761 |
|
4762 |
connected = 0;
|
4763 |
for(;;) {
|
4764 |
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
4765 |
if (ret < 0) { |
4766 |
err = socket_error(); |
4767 |
if (err == EINTR || err == EWOULDBLOCK) {
|
4768 |
} else if (err == EINPROGRESS) { |
4769 |
break;
|
4770 |
#ifdef _WIN32
|
4771 |
} else if (err == WSAEALREADY) { |
4772 |
break;
|
4773 |
#endif
|
4774 |
} else {
|
4775 |
perror("connect");
|
4776 |
closesocket(fd); |
4777 |
return -1; |
4778 |
} |
4779 |
} else {
|
4780 |
connected = 1;
|
4781 |
break;
|
4782 |
} |
4783 |
} |
4784 |
s = net_socket_fd_init(vlan, fd, connected); |
4785 |
if (!s)
|
4786 |
return -1; |
4787 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4788 |
"socket: connect to %s:%d",
|
4789 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4790 |
return 0; |
4791 |
} |
4792 |
|
4793 |
static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
4794 |
{ |
4795 |
NetSocketState *s; |
4796 |
int fd;
|
4797 |
struct sockaddr_in saddr;
|
4798 |
|
4799 |
if (parse_host_port(&saddr, host_str) < 0) |
4800 |
return -1; |
4801 |
|
4802 |
|
4803 |
fd = net_socket_mcast_create(&saddr); |
4804 |
if (fd < 0) |
4805 |
return -1; |
4806 |
|
4807 |
s = net_socket_fd_init(vlan, fd, 0);
|
4808 |
if (!s)
|
4809 |
return -1; |
4810 |
|
4811 |
s->dgram_dst = saddr; |
4812 |
|
4813 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4814 |
"socket: mcast=%s:%d",
|
4815 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4816 |
return 0; |
4817 |
|
4818 |
} |
4819 |
|
4820 |
static const char *get_opt_name(char *buf, int buf_size, const char *p) |
4821 |
{ |
4822 |
char *q;
|
4823 |
|
4824 |
q = buf; |
4825 |
while (*p != '\0' && *p != '=') { |
4826 |
if (q && (q - buf) < buf_size - 1) |
4827 |
*q++ = *p; |
4828 |
p++; |
4829 |
} |
4830 |
if (q)
|
4831 |
*q = '\0';
|
4832 |
|
4833 |
return p;
|
4834 |
} |
4835 |
|
4836 |
static const char *get_opt_value(char *buf, int buf_size, const char *p) |
4837 |
{ |
4838 |
char *q;
|
4839 |
|
4840 |
q = buf; |
4841 |
while (*p != '\0') { |
4842 |
if (*p == ',') { |
4843 |
if (*(p + 1) != ',') |
4844 |
break;
|
4845 |
p++; |
4846 |
} |
4847 |
if (q && (q - buf) < buf_size - 1) |
4848 |
*q++ = *p; |
4849 |
p++; |
4850 |
} |
4851 |
if (q)
|
4852 |
*q = '\0';
|
4853 |
|
4854 |
return p;
|
4855 |
} |
4856 |
|
4857 |
static int get_param_value(char *buf, int buf_size, |
4858 |
const char *tag, const char *str) |
4859 |
{ |
4860 |
const char *p; |
4861 |
char option[128]; |
4862 |
|
4863 |
p = str; |
4864 |
for(;;) {
|
4865 |
p = get_opt_name(option, sizeof(option), p);
|
4866 |
if (*p != '=') |
4867 |
break;
|
4868 |
p++; |
4869 |
if (!strcmp(tag, option)) {
|
4870 |
(void)get_opt_value(buf, buf_size, p);
|
4871 |
return strlen(buf);
|
4872 |
} else {
|
4873 |
p = get_opt_value(NULL, 0, p); |
4874 |
} |
4875 |
if (*p != ',') |
4876 |
break;
|
4877 |
p++; |
4878 |
} |
4879 |
return 0; |
4880 |
} |
4881 |
|
4882 |
static int check_params(char *buf, int buf_size, |
4883 |
char **params, const char *str) |
4884 |
{ |
4885 |
const char *p; |
4886 |
int i;
|
4887 |
|
4888 |
p = str; |
4889 |
for(;;) {
|
4890 |
p = get_opt_name(buf, buf_size, p); |
4891 |
if (*p != '=') |
4892 |
return -1; |
4893 |
p++; |
4894 |
for(i = 0; params[i] != NULL; i++) |
4895 |
if (!strcmp(params[i], buf))
|
4896 |
break;
|
4897 |
if (params[i] == NULL) |
4898 |
return -1; |
4899 |
p = get_opt_value(NULL, 0, p); |
4900 |
if (*p != ',') |
4901 |
break;
|
4902 |
p++; |
4903 |
} |
4904 |
return 0; |
4905 |
} |
4906 |
|
4907 |
|
4908 |
static int net_client_init(const char *str) |
4909 |
{ |
4910 |
const char *p; |
4911 |
char *q;
|
4912 |
char device[64]; |
4913 |
char buf[1024]; |
4914 |
int vlan_id, ret;
|
4915 |
VLANState *vlan; |
4916 |
|
4917 |
p = str; |
4918 |
q = device; |
4919 |
while (*p != '\0' && *p != ',') { |
4920 |
if ((q - device) < sizeof(device) - 1) |
4921 |
*q++ = *p; |
4922 |
p++; |
4923 |
} |
4924 |
*q = '\0';
|
4925 |
if (*p == ',') |
4926 |
p++; |
4927 |
vlan_id = 0;
|
4928 |
if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
4929 |
vlan_id = strtol(buf, NULL, 0); |
4930 |
} |
4931 |
vlan = qemu_find_vlan(vlan_id); |
4932 |
if (!vlan) {
|
4933 |
fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
4934 |
return -1; |
4935 |
} |
4936 |
if (!strcmp(device, "nic")) { |
4937 |
NICInfo *nd; |
4938 |
uint8_t *macaddr; |
4939 |
|
4940 |
if (nb_nics >= MAX_NICS) {
|
4941 |
fprintf(stderr, "Too Many NICs\n");
|
4942 |
return -1; |
4943 |
} |
4944 |
nd = &nd_table[nb_nics]; |
4945 |
macaddr = nd->macaddr; |
4946 |
macaddr[0] = 0x52; |
4947 |
macaddr[1] = 0x54; |
4948 |
macaddr[2] = 0x00; |
4949 |
macaddr[3] = 0x12; |
4950 |
macaddr[4] = 0x34; |
4951 |
macaddr[5] = 0x56 + nb_nics; |
4952 |
|
4953 |
if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
4954 |
if (parse_macaddr(macaddr, buf) < 0) { |
4955 |
fprintf(stderr, "invalid syntax for ethernet address\n");
|
4956 |
return -1; |
4957 |
} |
4958 |
} |
4959 |
if (get_param_value(buf, sizeof(buf), "model", p)) { |
4960 |
nd->model = strdup(buf); |
4961 |
} |
4962 |
nd->vlan = vlan; |
4963 |
nb_nics++; |
4964 |
vlan->nb_guest_devs++; |
4965 |
ret = 0;
|
4966 |
} else
|
4967 |
if (!strcmp(device, "none")) { |
4968 |
/* does nothing. It is needed to signal that no network cards
|
4969 |
are wanted */
|
4970 |
ret = 0;
|
4971 |
} else
|
4972 |
#ifdef CONFIG_SLIRP
|
4973 |
if (!strcmp(device, "user")) { |