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