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