root / vl.c @ ed23fbd9
History | View | Annotate | Download (239.5 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 |
#if !defined(__APPLE__) && !defined(__OpenBSD__)
|
65 |
#include <libutil.h> |
66 |
#endif
|
67 |
#ifdef __OpenBSD__
|
68 |
#include <net/if.h> |
69 |
#endif
|
70 |
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
|
71 |
#include <freebsd/stdlib.h> |
72 |
#else
|
73 |
#ifndef __sun__
|
74 |
#include <linux/if.h> |
75 |
#include <linux/if_tun.h> |
76 |
#include <pty.h> |
77 |
#include <malloc.h> |
78 |
#include <linux/rtc.h> |
79 |
|
80 |
/* For the benefit of older linux systems which don't supply it,
|
81 |
we use a local copy of hpet.h. */
|
82 |
/* #include <linux/hpet.h> */
|
83 |
#include "hpet.h" |
84 |
|
85 |
#include <linux/ppdev.h> |
86 |
#include <linux/parport.h> |
87 |
#else
|
88 |
#include <sys/stat.h> |
89 |
#include <sys/ethernet.h> |
90 |
#include <sys/sockio.h> |
91 |
#include <netinet/arp.h> |
92 |
#include <netinet/in.h> |
93 |
#include <netinet/in_systm.h> |
94 |
#include <netinet/ip.h> |
95 |
#include <netinet/ip_icmp.h> // must come after ip.h |
96 |
#include <netinet/udp.h> |
97 |
#include <netinet/tcp.h> |
98 |
#include <net/if.h> |
99 |
#include <syslog.h> |
100 |
#include <stropts.h> |
101 |
#endif
|
102 |
#endif
|
103 |
#else
|
104 |
#include <winsock2.h> |
105 |
int inet_aton(const char *cp, struct in_addr *ia); |
106 |
#endif
|
107 |
|
108 |
#if defined(CONFIG_SLIRP)
|
109 |
#include "libslirp.h" |
110 |
#endif
|
111 |
|
112 |
#if defined(__OpenBSD__)
|
113 |
#include <util.h> |
114 |
#endif
|
115 |
|
116 |
#if defined(CONFIG_VDE)
|
117 |
#include <libvdeplug.h> |
118 |
#endif
|
119 |
|
120 |
#ifdef _WIN32
|
121 |
#include <malloc.h> |
122 |
#include <sys/timeb.h> |
123 |
#include <mmsystem.h> |
124 |
#define getopt_long_only getopt_long
|
125 |
#define memalign(align, size) malloc(size)
|
126 |
#endif
|
127 |
|
128 |
#include "qemu_socket.h" |
129 |
|
130 |
#ifdef CONFIG_SDL
|
131 |
#ifdef __APPLE__
|
132 |
#include <SDL/SDL.h> |
133 |
#endif
|
134 |
#endif /* CONFIG_SDL */ |
135 |
|
136 |
#ifdef CONFIG_COCOA
|
137 |
#undef main
|
138 |
#define main qemu_main
|
139 |
#endif /* CONFIG_COCOA */ |
140 |
|
141 |
#include "disas.h" |
142 |
|
143 |
#include "exec-all.h" |
144 |
|
145 |
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
146 |
#define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown" |
147 |
#ifdef __sun__
|
148 |
#define SMBD_COMMAND "/usr/sfw/sbin/smbd" |
149 |
#else
|
150 |
#define SMBD_COMMAND "/usr/sbin/smbd" |
151 |
#endif
|
152 |
|
153 |
//#define DEBUG_UNUSED_IOPORT
|
154 |
//#define DEBUG_IOPORT
|
155 |
|
156 |
#ifdef TARGET_PPC
|
157 |
#define DEFAULT_RAM_SIZE 144 |
158 |
#else
|
159 |
#define DEFAULT_RAM_SIZE 128 |
160 |
#endif
|
161 |
|
162 |
/* Max number of USB devices that can be specified on the commandline. */
|
163 |
#define MAX_USB_CMDLINE 8 |
164 |
|
165 |
/* XXX: use a two level table to limit memory usage */
|
166 |
#define MAX_IOPORTS 65536 |
167 |
|
168 |
const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
169 |
const char *bios_name = NULL; |
170 |
void *ioport_opaque[MAX_IOPORTS];
|
171 |
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
172 |
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
173 |
/* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
|
174 |
to store the VM snapshots */
|
175 |
DriveInfo drives_table[MAX_DRIVES+1];
|
176 |
int nb_drives;
|
177 |
/* point to the block driver where the snapshots are managed */
|
178 |
BlockDriverState *bs_snapshots; |
179 |
int vga_ram_size;
|
180 |
static DisplayState display_state;
|
181 |
int nographic;
|
182 |
int curses;
|
183 |
const char* keyboard_layout = NULL; |
184 |
int64_t ticks_per_sec; |
185 |
ram_addr_t ram_size; |
186 |
int pit_min_timer_count = 0; |
187 |
int nb_nics;
|
188 |
NICInfo nd_table[MAX_NICS]; |
189 |
int vm_running;
|
190 |
static int rtc_utc = 1; |
191 |
static int rtc_date_offset = -1; /* -1 means no change */ |
192 |
int cirrus_vga_enabled = 1; |
193 |
int vmsvga_enabled = 0; |
194 |
#ifdef TARGET_SPARC
|
195 |
int graphic_width = 1024; |
196 |
int graphic_height = 768; |
197 |
int graphic_depth = 8; |
198 |
#else
|
199 |
int graphic_width = 800; |
200 |
int graphic_height = 600; |
201 |
int graphic_depth = 15; |
202 |
#endif
|
203 |
int full_screen = 0; |
204 |
int no_frame = 0; |
205 |
int no_quit = 0; |
206 |
CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
207 |
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
208 |
#ifdef TARGET_I386
|
209 |
int win2k_install_hack = 0; |
210 |
#endif
|
211 |
int usb_enabled = 0; |
212 |
static VLANState *first_vlan;
|
213 |
int smp_cpus = 1; |
214 |
const char *vnc_display; |
215 |
#if defined(TARGET_SPARC)
|
216 |
#define MAX_CPUS 16 |
217 |
#elif defined(TARGET_I386)
|
218 |
#define MAX_CPUS 255 |
219 |
#else
|
220 |
#define MAX_CPUS 1 |
221 |
#endif
|
222 |
int acpi_enabled = 1; |
223 |
int fd_bootchk = 1; |
224 |
int no_reboot = 0; |
225 |
int no_shutdown = 0; |
226 |
int cursor_hide = 1; |
227 |
int graphic_rotate = 0; |
228 |
int daemonize = 0; |
229 |
const char *option_rom[MAX_OPTION_ROMS]; |
230 |
int nb_option_roms;
|
231 |
int semihosting_enabled = 0; |
232 |
int autostart = 1; |
233 |
#ifdef TARGET_ARM
|
234 |
int old_param = 0; |
235 |
#endif
|
236 |
const char *qemu_name; |
237 |
int alt_grab = 0; |
238 |
#ifdef TARGET_SPARC
|
239 |
unsigned int nb_prom_envs = 0; |
240 |
const char *prom_envs[MAX_PROM_ENVS]; |
241 |
#endif
|
242 |
int nb_drives_opt;
|
243 |
struct drive_opt {
|
244 |
const char *file; |
245 |
char opt[1024]; |
246 |
} drives_opt[MAX_DRIVES]; |
247 |
|
248 |
static CPUState *cur_cpu;
|
249 |
static CPUState *next_cpu;
|
250 |
static int event_pending = 1; |
251 |
/* Conversion factor from emulated instructions to virtual clock ticks. */
|
252 |
static int icount_time_shift; |
253 |
/* Arbitrarily pick 1MIPS as the minimum allowable speed. */
|
254 |
#define MAX_ICOUNT_SHIFT 10 |
255 |
/* Compensate for varying guest execution speed. */
|
256 |
static int64_t qemu_icount_bias;
|
257 |
QEMUTimer *icount_rt_timer; |
258 |
QEMUTimer *icount_vm_timer; |
259 |
|
260 |
#define TFR(expr) do { if ((expr) != -1) break; } while (errno == EINTR) |
261 |
|
262 |
/***********************************************************/
|
263 |
/* x86 ISA bus support */
|
264 |
|
265 |
target_phys_addr_t isa_mem_base = 0;
|
266 |
PicState2 *isa_pic; |
267 |
|
268 |
static IOPortReadFunc default_ioport_readb, default_ioport_readw, default_ioport_readl;
|
269 |
static IOPortWriteFunc default_ioport_writeb, default_ioport_writew, default_ioport_writel;
|
270 |
|
271 |
static uint32_t ioport_read(int index, uint32_t address) |
272 |
{ |
273 |
static IOPortReadFunc *default_func[3] = { |
274 |
default_ioport_readb, |
275 |
default_ioport_readw, |
276 |
default_ioport_readl |
277 |
}; |
278 |
IOPortReadFunc *func = ioport_read_table[index][address]; |
279 |
if (!func)
|
280 |
func = default_func[index]; |
281 |
return func(ioport_opaque[address], address);
|
282 |
} |
283 |
|
284 |
static void ioport_write(int index, uint32_t address, uint32_t data) |
285 |
{ |
286 |
static IOPortWriteFunc *default_func[3] = { |
287 |
default_ioport_writeb, |
288 |
default_ioport_writew, |
289 |
default_ioport_writel |
290 |
}; |
291 |
IOPortWriteFunc *func = ioport_write_table[index][address]; |
292 |
if (!func)
|
293 |
func = default_func[index]; |
294 |
func(ioport_opaque[address], address, data); |
295 |
} |
296 |
|
297 |
static uint32_t default_ioport_readb(void *opaque, uint32_t address) |
298 |
{ |
299 |
#ifdef DEBUG_UNUSED_IOPORT
|
300 |
fprintf(stderr, "unused inb: port=0x%04x\n", address);
|
301 |
#endif
|
302 |
return 0xff; |
303 |
} |
304 |
|
305 |
static void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
306 |
{ |
307 |
#ifdef DEBUG_UNUSED_IOPORT
|
308 |
fprintf(stderr, "unused outb: port=0x%04x data=0x%02x\n", address, data);
|
309 |
#endif
|
310 |
} |
311 |
|
312 |
/* default is to make two byte accesses */
|
313 |
static uint32_t default_ioport_readw(void *opaque, uint32_t address) |
314 |
{ |
315 |
uint32_t data; |
316 |
data = ioport_read(0, address);
|
317 |
address = (address + 1) & (MAX_IOPORTS - 1); |
318 |
data |= ioport_read(0, address) << 8; |
319 |
return data;
|
320 |
} |
321 |
|
322 |
static void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
323 |
{ |
324 |
ioport_write(0, address, data & 0xff); |
325 |
address = (address + 1) & (MAX_IOPORTS - 1); |
326 |
ioport_write(0, address, (data >> 8) & 0xff); |
327 |
} |
328 |
|
329 |
static uint32_t default_ioport_readl(void *opaque, uint32_t address) |
330 |
{ |
331 |
#ifdef DEBUG_UNUSED_IOPORT
|
332 |
fprintf(stderr, "unused inl: port=0x%04x\n", address);
|
333 |
#endif
|
334 |
return 0xffffffff; |
335 |
} |
336 |
|
337 |
static void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
338 |
{ |
339 |
#ifdef DEBUG_UNUSED_IOPORT
|
340 |
fprintf(stderr, "unused outl: port=0x%04x data=0x%02x\n", address, data);
|
341 |
#endif
|
342 |
} |
343 |
|
344 |
/* size is the word size in byte */
|
345 |
int register_ioport_read(int start, int length, int size, |
346 |
IOPortReadFunc *func, void *opaque)
|
347 |
{ |
348 |
int i, bsize;
|
349 |
|
350 |
if (size == 1) { |
351 |
bsize = 0;
|
352 |
} else if (size == 2) { |
353 |
bsize = 1;
|
354 |
} else if (size == 4) { |
355 |
bsize = 2;
|
356 |
} else {
|
357 |
hw_error("register_ioport_read: invalid size");
|
358 |
return -1; |
359 |
} |
360 |
for(i = start; i < start + length; i += size) {
|
361 |
ioport_read_table[bsize][i] = func; |
362 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
363 |
hw_error("register_ioport_read: invalid opaque");
|
364 |
ioport_opaque[i] = opaque; |
365 |
} |
366 |
return 0; |
367 |
} |
368 |
|
369 |
/* size is the word size in byte */
|
370 |
int register_ioport_write(int start, int length, int size, |
371 |
IOPortWriteFunc *func, void *opaque)
|
372 |
{ |
373 |
int i, bsize;
|
374 |
|
375 |
if (size == 1) { |
376 |
bsize = 0;
|
377 |
} else if (size == 2) { |
378 |
bsize = 1;
|
379 |
} else if (size == 4) { |
380 |
bsize = 2;
|
381 |
} else {
|
382 |
hw_error("register_ioport_write: invalid size");
|
383 |
return -1; |
384 |
} |
385 |
for(i = start; i < start + length; i += size) {
|
386 |
ioport_write_table[bsize][i] = func; |
387 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
388 |
hw_error("register_ioport_write: invalid opaque");
|
389 |
ioport_opaque[i] = opaque; |
390 |
} |
391 |
return 0; |
392 |
} |
393 |
|
394 |
void isa_unassign_ioport(int start, int length) |
395 |
{ |
396 |
int i;
|
397 |
|
398 |
for(i = start; i < start + length; i++) {
|
399 |
ioport_read_table[0][i] = default_ioport_readb;
|
400 |
ioport_read_table[1][i] = default_ioport_readw;
|
401 |
ioport_read_table[2][i] = default_ioport_readl;
|
402 |
|
403 |
ioport_write_table[0][i] = default_ioport_writeb;
|
404 |
ioport_write_table[1][i] = default_ioport_writew;
|
405 |
ioport_write_table[2][i] = default_ioport_writel;
|
406 |
} |
407 |
} |
408 |
|
409 |
/***********************************************************/
|
410 |
|
411 |
void cpu_outb(CPUState *env, int addr, int val) |
412 |
{ |
413 |
#ifdef DEBUG_IOPORT
|
414 |
if (loglevel & CPU_LOG_IOPORT)
|
415 |
fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
416 |
#endif
|
417 |
ioport_write(0, addr, val);
|
418 |
#ifdef USE_KQEMU
|
419 |
if (env)
|
420 |
env->last_io_time = cpu_get_time_fast(); |
421 |
#endif
|
422 |
} |
423 |
|
424 |
void cpu_outw(CPUState *env, int addr, int val) |
425 |
{ |
426 |
#ifdef DEBUG_IOPORT
|
427 |
if (loglevel & CPU_LOG_IOPORT)
|
428 |
fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
429 |
#endif
|
430 |
ioport_write(1, addr, val);
|
431 |
#ifdef USE_KQEMU
|
432 |
if (env)
|
433 |
env->last_io_time = cpu_get_time_fast(); |
434 |
#endif
|
435 |
} |
436 |
|
437 |
void cpu_outl(CPUState *env, int addr, int val) |
438 |
{ |
439 |
#ifdef DEBUG_IOPORT
|
440 |
if (loglevel & CPU_LOG_IOPORT)
|
441 |
fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
442 |
#endif
|
443 |
ioport_write(2, addr, val);
|
444 |
#ifdef USE_KQEMU
|
445 |
if (env)
|
446 |
env->last_io_time = cpu_get_time_fast(); |
447 |
#endif
|
448 |
} |
449 |
|
450 |
int cpu_inb(CPUState *env, int addr) |
451 |
{ |
452 |
int val;
|
453 |
val = ioport_read(0, addr);
|
454 |
#ifdef DEBUG_IOPORT
|
455 |
if (loglevel & CPU_LOG_IOPORT)
|
456 |
fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
457 |
#endif
|
458 |
#ifdef USE_KQEMU
|
459 |
if (env)
|
460 |
env->last_io_time = cpu_get_time_fast(); |
461 |
#endif
|
462 |
return val;
|
463 |
} |
464 |
|
465 |
int cpu_inw(CPUState *env, int addr) |
466 |
{ |
467 |
int val;
|
468 |
val = ioport_read(1, addr);
|
469 |
#ifdef DEBUG_IOPORT
|
470 |
if (loglevel & CPU_LOG_IOPORT)
|
471 |
fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
472 |
#endif
|
473 |
#ifdef USE_KQEMU
|
474 |
if (env)
|
475 |
env->last_io_time = cpu_get_time_fast(); |
476 |
#endif
|
477 |
return val;
|
478 |
} |
479 |
|
480 |
int cpu_inl(CPUState *env, int addr) |
481 |
{ |
482 |
int val;
|
483 |
val = ioport_read(2, addr);
|
484 |
#ifdef DEBUG_IOPORT
|
485 |
if (loglevel & CPU_LOG_IOPORT)
|
486 |
fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
487 |
#endif
|
488 |
#ifdef USE_KQEMU
|
489 |
if (env)
|
490 |
env->last_io_time = cpu_get_time_fast(); |
491 |
#endif
|
492 |
return val;
|
493 |
} |
494 |
|
495 |
/***********************************************************/
|
496 |
void hw_error(const char *fmt, ...) |
497 |
{ |
498 |
va_list ap; |
499 |
CPUState *env; |
500 |
|
501 |
va_start(ap, fmt); |
502 |
fprintf(stderr, "qemu: hardware error: ");
|
503 |
vfprintf(stderr, fmt, ap); |
504 |
fprintf(stderr, "\n");
|
505 |
for(env = first_cpu; env != NULL; env = env->next_cpu) { |
506 |
fprintf(stderr, "CPU #%d:\n", env->cpu_index);
|
507 |
#ifdef TARGET_I386
|
508 |
cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU); |
509 |
#else
|
510 |
cpu_dump_state(env, stderr, fprintf, 0);
|
511 |
#endif
|
512 |
} |
513 |
va_end(ap); |
514 |
abort(); |
515 |
} |
516 |
|
517 |
/***********************************************************/
|
518 |
/* keyboard/mouse */
|
519 |
|
520 |
static QEMUPutKBDEvent *qemu_put_kbd_event;
|
521 |
static void *qemu_put_kbd_event_opaque; |
522 |
static QEMUPutMouseEntry *qemu_put_mouse_event_head;
|
523 |
static QEMUPutMouseEntry *qemu_put_mouse_event_current;
|
524 |
|
525 |
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
526 |
{ |
527 |
qemu_put_kbd_event_opaque = opaque; |
528 |
qemu_put_kbd_event = func; |
529 |
} |
530 |
|
531 |
QEMUPutMouseEntry *qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, |
532 |
void *opaque, int absolute, |
533 |
const char *name) |
534 |
{ |
535 |
QEMUPutMouseEntry *s, *cursor; |
536 |
|
537 |
s = qemu_mallocz(sizeof(QEMUPutMouseEntry));
|
538 |
if (!s)
|
539 |
return NULL; |
540 |
|
541 |
s->qemu_put_mouse_event = func; |
542 |
s->qemu_put_mouse_event_opaque = opaque; |
543 |
s->qemu_put_mouse_event_absolute = absolute; |
544 |
s->qemu_put_mouse_event_name = qemu_strdup(name); |
545 |
s->next = NULL;
|
546 |
|
547 |
if (!qemu_put_mouse_event_head) {
|
548 |
qemu_put_mouse_event_head = qemu_put_mouse_event_current = s; |
549 |
return s;
|
550 |
} |
551 |
|
552 |
cursor = qemu_put_mouse_event_head; |
553 |
while (cursor->next != NULL) |
554 |
cursor = cursor->next; |
555 |
|
556 |
cursor->next = s; |
557 |
qemu_put_mouse_event_current = s; |
558 |
|
559 |
return s;
|
560 |
} |
561 |
|
562 |
void qemu_remove_mouse_event_handler(QEMUPutMouseEntry *entry)
|
563 |
{ |
564 |
QEMUPutMouseEntry *prev = NULL, *cursor;
|
565 |
|
566 |
if (!qemu_put_mouse_event_head || entry == NULL) |
567 |
return;
|
568 |
|
569 |
cursor = qemu_put_mouse_event_head; |
570 |
while (cursor != NULL && cursor != entry) { |
571 |
prev = cursor; |
572 |
cursor = cursor->next; |
573 |
} |
574 |
|
575 |
if (cursor == NULL) // does not exist or list empty |
576 |
return;
|
577 |
else if (prev == NULL) { // entry is head |
578 |
qemu_put_mouse_event_head = cursor->next; |
579 |
if (qemu_put_mouse_event_current == entry)
|
580 |
qemu_put_mouse_event_current = cursor->next; |
581 |
qemu_free(entry->qemu_put_mouse_event_name); |
582 |
qemu_free(entry); |
583 |
return;
|
584 |
} |
585 |
|
586 |
prev->next = entry->next; |
587 |
|
588 |
if (qemu_put_mouse_event_current == entry)
|
589 |
qemu_put_mouse_event_current = prev; |
590 |
|
591 |
qemu_free(entry->qemu_put_mouse_event_name); |
592 |
qemu_free(entry); |
593 |
} |
594 |
|
595 |
void kbd_put_keycode(int keycode) |
596 |
{ |
597 |
if (qemu_put_kbd_event) {
|
598 |
qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
599 |
} |
600 |
} |
601 |
|
602 |
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
603 |
{ |
604 |
QEMUPutMouseEvent *mouse_event; |
605 |
void *mouse_event_opaque;
|
606 |
int width;
|
607 |
|
608 |
if (!qemu_put_mouse_event_current) {
|
609 |
return;
|
610 |
} |
611 |
|
612 |
mouse_event = |
613 |
qemu_put_mouse_event_current->qemu_put_mouse_event; |
614 |
mouse_event_opaque = |
615 |
qemu_put_mouse_event_current->qemu_put_mouse_event_opaque; |
616 |
|
617 |
if (mouse_event) {
|
618 |
if (graphic_rotate) {
|
619 |
if (qemu_put_mouse_event_current->qemu_put_mouse_event_absolute)
|
620 |
width = 0x7fff;
|
621 |
else
|
622 |
width = graphic_width - 1;
|
623 |
mouse_event(mouse_event_opaque, |
624 |
width - dy, dx, dz, buttons_state); |
625 |
} else
|
626 |
mouse_event(mouse_event_opaque, |
627 |
dx, dy, dz, buttons_state); |
628 |
} |
629 |
} |
630 |
|
631 |
int kbd_mouse_is_absolute(void) |
632 |
{ |
633 |
if (!qemu_put_mouse_event_current)
|
634 |
return 0; |
635 |
|
636 |
return qemu_put_mouse_event_current->qemu_put_mouse_event_absolute;
|
637 |
} |
638 |
|
639 |
void do_info_mice(void) |
640 |
{ |
641 |
QEMUPutMouseEntry *cursor; |
642 |
int index = 0; |
643 |
|
644 |
if (!qemu_put_mouse_event_head) {
|
645 |
term_printf("No mouse devices connected\n");
|
646 |
return;
|
647 |
} |
648 |
|
649 |
term_printf("Mouse devices available:\n");
|
650 |
cursor = qemu_put_mouse_event_head; |
651 |
while (cursor != NULL) { |
652 |
term_printf("%c Mouse #%d: %s\n",
|
653 |
(cursor == qemu_put_mouse_event_current ? '*' : ' '), |
654 |
index, cursor->qemu_put_mouse_event_name); |
655 |
index++; |
656 |
cursor = cursor->next; |
657 |
} |
658 |
} |
659 |
|
660 |
void do_mouse_set(int index) |
661 |
{ |
662 |
QEMUPutMouseEntry *cursor; |
663 |
int i = 0; |
664 |
|
665 |
if (!qemu_put_mouse_event_head) {
|
666 |
term_printf("No mouse devices connected\n");
|
667 |
return;
|
668 |
} |
669 |
|
670 |
cursor = qemu_put_mouse_event_head; |
671 |
while (cursor != NULL && index != i) { |
672 |
i++; |
673 |
cursor = cursor->next; |
674 |
} |
675 |
|
676 |
if (cursor != NULL) |
677 |
qemu_put_mouse_event_current = cursor; |
678 |
else
|
679 |
term_printf("Mouse at given index not found\n");
|
680 |
} |
681 |
|
682 |
/* compute with 96 bit intermediate result: (a*b)/c */
|
683 |
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
684 |
{ |
685 |
union {
|
686 |
uint64_t ll; |
687 |
struct {
|
688 |
#ifdef WORDS_BIGENDIAN
|
689 |
uint32_t high, low; |
690 |
#else
|
691 |
uint32_t low, high; |
692 |
#endif
|
693 |
} l; |
694 |
} u, res; |
695 |
uint64_t rl, rh; |
696 |
|
697 |
u.ll = a; |
698 |
rl = (uint64_t)u.l.low * (uint64_t)b; |
699 |
rh = (uint64_t)u.l.high * (uint64_t)b; |
700 |
rh += (rl >> 32);
|
701 |
res.l.high = rh / c; |
702 |
res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
703 |
return res.ll;
|
704 |
} |
705 |
|
706 |
/***********************************************************/
|
707 |
/* real time host monotonic timer */
|
708 |
|
709 |
#define QEMU_TIMER_BASE 1000000000LL |
710 |
|
711 |
#ifdef WIN32
|
712 |
|
713 |
static int64_t clock_freq;
|
714 |
|
715 |
static void init_get_clock(void) |
716 |
{ |
717 |
LARGE_INTEGER freq; |
718 |
int ret;
|
719 |
ret = QueryPerformanceFrequency(&freq); |
720 |
if (ret == 0) { |
721 |
fprintf(stderr, "Could not calibrate ticks\n");
|
722 |
exit(1);
|
723 |
} |
724 |
clock_freq = freq.QuadPart; |
725 |
} |
726 |
|
727 |
static int64_t get_clock(void) |
728 |
{ |
729 |
LARGE_INTEGER ti; |
730 |
QueryPerformanceCounter(&ti); |
731 |
return muldiv64(ti.QuadPart, QEMU_TIMER_BASE, clock_freq);
|
732 |
} |
733 |
|
734 |
#else
|
735 |
|
736 |
static int use_rt_clock; |
737 |
|
738 |
static void init_get_clock(void) |
739 |
{ |
740 |
use_rt_clock = 0;
|
741 |
#if defined(__linux__)
|
742 |
{ |
743 |
struct timespec ts;
|
744 |
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) { |
745 |
use_rt_clock = 1;
|
746 |
} |
747 |
} |
748 |
#endif
|
749 |
} |
750 |
|
751 |
static int64_t get_clock(void) |
752 |
{ |
753 |
#if defined(__linux__)
|
754 |
if (use_rt_clock) {
|
755 |
struct timespec ts;
|
756 |
clock_gettime(CLOCK_MONOTONIC, &ts); |
757 |
return ts.tv_sec * 1000000000LL + ts.tv_nsec; |
758 |
} else
|
759 |
#endif
|
760 |
{ |
761 |
/* XXX: using gettimeofday leads to problems if the date
|
762 |
changes, so it should be avoided. */
|
763 |
struct timeval tv;
|
764 |
gettimeofday(&tv, NULL);
|
765 |
return tv.tv_sec * 1000000000LL + (tv.tv_usec * 1000); |
766 |
} |
767 |
} |
768 |
#endif
|
769 |
|
770 |
/* Return the virtual CPU time, based on the instruction counter. */
|
771 |
static int64_t cpu_get_icount(void) |
772 |
{ |
773 |
int64_t icount; |
774 |
CPUState *env = cpu_single_env;; |
775 |
icount = qemu_icount; |
776 |
if (env) {
|
777 |
if (!can_do_io(env))
|
778 |
fprintf(stderr, "Bad clock read\n");
|
779 |
icount -= (env->icount_decr.u16.low + env->icount_extra); |
780 |
} |
781 |
return qemu_icount_bias + (icount << icount_time_shift);
|
782 |
} |
783 |
|
784 |
/***********************************************************/
|
785 |
/* guest cycle counter */
|
786 |
|
787 |
static int64_t cpu_ticks_prev;
|
788 |
static int64_t cpu_ticks_offset;
|
789 |
static int64_t cpu_clock_offset;
|
790 |
static int cpu_ticks_enabled; |
791 |
|
792 |
/* return the host CPU cycle counter and handle stop/restart */
|
793 |
int64_t cpu_get_ticks(void)
|
794 |
{ |
795 |
if (use_icount) {
|
796 |
return cpu_get_icount();
|
797 |
} |
798 |
if (!cpu_ticks_enabled) {
|
799 |
return cpu_ticks_offset;
|
800 |
} else {
|
801 |
int64_t ticks; |
802 |
ticks = cpu_get_real_ticks(); |
803 |
if (cpu_ticks_prev > ticks) {
|
804 |
/* Note: non increasing ticks may happen if the host uses
|
805 |
software suspend */
|
806 |
cpu_ticks_offset += cpu_ticks_prev - ticks; |
807 |
} |
808 |
cpu_ticks_prev = ticks; |
809 |
return ticks + cpu_ticks_offset;
|
810 |
} |
811 |
} |
812 |
|
813 |
/* return the host CPU monotonic timer and handle stop/restart */
|
814 |
static int64_t cpu_get_clock(void) |
815 |
{ |
816 |
int64_t ti; |
817 |
if (!cpu_ticks_enabled) {
|
818 |
return cpu_clock_offset;
|
819 |
} else {
|
820 |
ti = get_clock(); |
821 |
return ti + cpu_clock_offset;
|
822 |
} |
823 |
} |
824 |
|
825 |
/* enable cpu_get_ticks() */
|
826 |
void cpu_enable_ticks(void) |
827 |
{ |
828 |
if (!cpu_ticks_enabled) {
|
829 |
cpu_ticks_offset -= cpu_get_real_ticks(); |
830 |
cpu_clock_offset -= get_clock(); |
831 |
cpu_ticks_enabled = 1;
|
832 |
} |
833 |
} |
834 |
|
835 |
/* disable cpu_get_ticks() : the clock is stopped. You must not call
|
836 |
cpu_get_ticks() after that. */
|
837 |
void cpu_disable_ticks(void) |
838 |
{ |
839 |
if (cpu_ticks_enabled) {
|
840 |
cpu_ticks_offset = cpu_get_ticks(); |
841 |
cpu_clock_offset = cpu_get_clock(); |
842 |
cpu_ticks_enabled = 0;
|
843 |
} |
844 |
} |
845 |
|
846 |
/***********************************************************/
|
847 |
/* timers */
|
848 |
|
849 |
#define QEMU_TIMER_REALTIME 0 |
850 |
#define QEMU_TIMER_VIRTUAL 1 |
851 |
|
852 |
struct QEMUClock {
|
853 |
int type;
|
854 |
/* XXX: add frequency */
|
855 |
}; |
856 |
|
857 |
struct QEMUTimer {
|
858 |
QEMUClock *clock; |
859 |
int64_t expire_time; |
860 |
QEMUTimerCB *cb; |
861 |
void *opaque;
|
862 |
struct QEMUTimer *next;
|
863 |
}; |
864 |
|
865 |
struct qemu_alarm_timer {
|
866 |
char const *name; |
867 |
unsigned int flags; |
868 |
|
869 |
int (*start)(struct qemu_alarm_timer *t); |
870 |
void (*stop)(struct qemu_alarm_timer *t); |
871 |
void (*rearm)(struct qemu_alarm_timer *t); |
872 |
void *priv;
|
873 |
}; |
874 |
|
875 |
#define ALARM_FLAG_DYNTICKS 0x1 |
876 |
#define ALARM_FLAG_EXPIRED 0x2 |
877 |
|
878 |
static inline int alarm_has_dynticks(struct qemu_alarm_timer *t) |
879 |
{ |
880 |
return t->flags & ALARM_FLAG_DYNTICKS;
|
881 |
} |
882 |
|
883 |
static void qemu_rearm_alarm_timer(struct qemu_alarm_timer *t) |
884 |
{ |
885 |
if (!alarm_has_dynticks(t))
|
886 |
return;
|
887 |
|
888 |
t->rearm(t); |
889 |
} |
890 |
|
891 |
/* TODO: MIN_TIMER_REARM_US should be optimized */
|
892 |
#define MIN_TIMER_REARM_US 250 |
893 |
|
894 |
static struct qemu_alarm_timer *alarm_timer; |
895 |
|
896 |
#ifdef _WIN32
|
897 |
|
898 |
struct qemu_alarm_win32 {
|
899 |
MMRESULT timerId; |
900 |
HANDLE host_alarm; |
901 |
unsigned int period; |
902 |
} alarm_win32_data = {0, NULL, -1}; |
903 |
|
904 |
static int win32_start_timer(struct qemu_alarm_timer *t); |
905 |
static void win32_stop_timer(struct qemu_alarm_timer *t); |
906 |
static void win32_rearm_timer(struct qemu_alarm_timer *t); |
907 |
|
908 |
#else
|
909 |
|
910 |
static int unix_start_timer(struct qemu_alarm_timer *t); |
911 |
static void unix_stop_timer(struct qemu_alarm_timer *t); |
912 |
|
913 |
#ifdef __linux__
|
914 |
|
915 |
static int dynticks_start_timer(struct qemu_alarm_timer *t); |
916 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t); |
917 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t); |
918 |
|
919 |
static int hpet_start_timer(struct qemu_alarm_timer *t); |
920 |
static void hpet_stop_timer(struct qemu_alarm_timer *t); |
921 |
|
922 |
static int rtc_start_timer(struct qemu_alarm_timer *t); |
923 |
static void rtc_stop_timer(struct qemu_alarm_timer *t); |
924 |
|
925 |
#endif /* __linux__ */ |
926 |
|
927 |
#endif /* _WIN32 */ |
928 |
|
929 |
/* Correlation between real and virtual time is always going to be
|
930 |
fairly approximate, so ignore small variation.
|
931 |
When the guest is idle real and virtual time will be aligned in
|
932 |
the IO wait loop. */
|
933 |
#define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10) |
934 |
|
935 |
static void icount_adjust(void) |
936 |
{ |
937 |
int64_t cur_time; |
938 |
int64_t cur_icount; |
939 |
int64_t delta; |
940 |
static int64_t last_delta;
|
941 |
/* If the VM is not running, then do nothing. */
|
942 |
if (!vm_running)
|
943 |
return;
|
944 |
|
945 |
cur_time = cpu_get_clock(); |
946 |
cur_icount = qemu_get_clock(vm_clock); |
947 |
delta = cur_icount - cur_time; |
948 |
/* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
|
949 |
if (delta > 0 |
950 |
&& last_delta + ICOUNT_WOBBLE < delta * 2
|
951 |
&& icount_time_shift > 0) {
|
952 |
/* The guest is getting too far ahead. Slow time down. */
|
953 |
icount_time_shift--; |
954 |
} |
955 |
if (delta < 0 |
956 |
&& last_delta - ICOUNT_WOBBLE > delta * 2
|
957 |
&& icount_time_shift < MAX_ICOUNT_SHIFT) { |
958 |
/* The guest is getting too far behind. Speed time up. */
|
959 |
icount_time_shift++; |
960 |
} |
961 |
last_delta = delta; |
962 |
qemu_icount_bias = cur_icount - (qemu_icount << icount_time_shift); |
963 |
} |
964 |
|
965 |
static void icount_adjust_rt(void * opaque) |
966 |
{ |
967 |
qemu_mod_timer(icount_rt_timer, |
968 |
qemu_get_clock(rt_clock) + 1000);
|
969 |
icount_adjust(); |
970 |
} |
971 |
|
972 |
static void icount_adjust_vm(void * opaque) |
973 |
{ |
974 |
qemu_mod_timer(icount_vm_timer, |
975 |
qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
976 |
icount_adjust(); |
977 |
} |
978 |
|
979 |
static void init_icount_adjust(void) |
980 |
{ |
981 |
/* Have both realtime and virtual time triggers for speed adjustment.
|
982 |
The realtime trigger catches emulated time passing too slowly,
|
983 |
the virtual time trigger catches emulated time passing too fast.
|
984 |
Realtime triggers occur even when idle, so use them less frequently
|
985 |
than VM triggers. */
|
986 |
icount_rt_timer = qemu_new_timer(rt_clock, icount_adjust_rt, NULL);
|
987 |
qemu_mod_timer(icount_rt_timer, |
988 |
qemu_get_clock(rt_clock) + 1000);
|
989 |
icount_vm_timer = qemu_new_timer(vm_clock, icount_adjust_vm, NULL);
|
990 |
qemu_mod_timer(icount_vm_timer, |
991 |
qemu_get_clock(vm_clock) + QEMU_TIMER_BASE / 10);
|
992 |
} |
993 |
|
994 |
static struct qemu_alarm_timer alarm_timers[] = { |
995 |
#ifndef _WIN32
|
996 |
#ifdef __linux__
|
997 |
{"dynticks", ALARM_FLAG_DYNTICKS, dynticks_start_timer,
|
998 |
dynticks_stop_timer, dynticks_rearm_timer, NULL},
|
999 |
/* HPET - if available - is preferred */
|
1000 |
{"hpet", 0, hpet_start_timer, hpet_stop_timer, NULL, NULL}, |
1001 |
/* ...otherwise try RTC */
|
1002 |
{"rtc", 0, rtc_start_timer, rtc_stop_timer, NULL, NULL}, |
1003 |
#endif
|
1004 |
{"unix", 0, unix_start_timer, unix_stop_timer, NULL, NULL}, |
1005 |
#else
|
1006 |
{"dynticks", ALARM_FLAG_DYNTICKS, win32_start_timer,
|
1007 |
win32_stop_timer, win32_rearm_timer, &alarm_win32_data}, |
1008 |
{"win32", 0, win32_start_timer, |
1009 |
win32_stop_timer, NULL, &alarm_win32_data},
|
1010 |
#endif
|
1011 |
{NULL, }
|
1012 |
}; |
1013 |
|
1014 |
static void show_available_alarms(void) |
1015 |
{ |
1016 |
int i;
|
1017 |
|
1018 |
printf("Available alarm timers, in order of precedence:\n");
|
1019 |
for (i = 0; alarm_timers[i].name; i++) |
1020 |
printf("%s\n", alarm_timers[i].name);
|
1021 |
} |
1022 |
|
1023 |
static void configure_alarms(char const *opt) |
1024 |
{ |
1025 |
int i;
|
1026 |
int cur = 0; |
1027 |
int count = (sizeof(alarm_timers) / sizeof(*alarm_timers)) - 1; |
1028 |
char *arg;
|
1029 |
char *name;
|
1030 |
struct qemu_alarm_timer tmp;
|
1031 |
|
1032 |
if (!strcmp(opt, "?")) { |
1033 |
show_available_alarms(); |
1034 |
exit(0);
|
1035 |
} |
1036 |
|
1037 |
arg = strdup(opt); |
1038 |
|
1039 |
/* Reorder the array */
|
1040 |
name = strtok(arg, ",");
|
1041 |
while (name) {
|
1042 |
for (i = 0; i < count && alarm_timers[i].name; i++) { |
1043 |
if (!strcmp(alarm_timers[i].name, name))
|
1044 |
break;
|
1045 |
} |
1046 |
|
1047 |
if (i == count) {
|
1048 |
fprintf(stderr, "Unknown clock %s\n", name);
|
1049 |
goto next;
|
1050 |
} |
1051 |
|
1052 |
if (i < cur)
|
1053 |
/* Ignore */
|
1054 |
goto next;
|
1055 |
|
1056 |
/* Swap */
|
1057 |
tmp = alarm_timers[i]; |
1058 |
alarm_timers[i] = alarm_timers[cur]; |
1059 |
alarm_timers[cur] = tmp; |
1060 |
|
1061 |
cur++; |
1062 |
next:
|
1063 |
name = strtok(NULL, ","); |
1064 |
} |
1065 |
|
1066 |
free(arg); |
1067 |
|
1068 |
if (cur) {
|
1069 |
/* Disable remaining timers */
|
1070 |
for (i = cur; i < count; i++)
|
1071 |
alarm_timers[i].name = NULL;
|
1072 |
} else {
|
1073 |
show_available_alarms(); |
1074 |
exit(1);
|
1075 |
} |
1076 |
} |
1077 |
|
1078 |
QEMUClock *rt_clock; |
1079 |
QEMUClock *vm_clock; |
1080 |
|
1081 |
static QEMUTimer *active_timers[2]; |
1082 |
|
1083 |
static QEMUClock *qemu_new_clock(int type) |
1084 |
{ |
1085 |
QEMUClock *clock; |
1086 |
clock = qemu_mallocz(sizeof(QEMUClock));
|
1087 |
if (!clock)
|
1088 |
return NULL; |
1089 |
clock->type = type; |
1090 |
return clock;
|
1091 |
} |
1092 |
|
1093 |
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
1094 |
{ |
1095 |
QEMUTimer *ts; |
1096 |
|
1097 |
ts = qemu_mallocz(sizeof(QEMUTimer));
|
1098 |
ts->clock = clock; |
1099 |
ts->cb = cb; |
1100 |
ts->opaque = opaque; |
1101 |
return ts;
|
1102 |
} |
1103 |
|
1104 |
void qemu_free_timer(QEMUTimer *ts)
|
1105 |
{ |
1106 |
qemu_free(ts); |
1107 |
} |
1108 |
|
1109 |
/* stop a timer, but do not dealloc it */
|
1110 |
void qemu_del_timer(QEMUTimer *ts)
|
1111 |
{ |
1112 |
QEMUTimer **pt, *t; |
1113 |
|
1114 |
/* NOTE: this code must be signal safe because
|
1115 |
qemu_timer_expired() can be called from a signal. */
|
1116 |
pt = &active_timers[ts->clock->type]; |
1117 |
for(;;) {
|
1118 |
t = *pt; |
1119 |
if (!t)
|
1120 |
break;
|
1121 |
if (t == ts) {
|
1122 |
*pt = t->next; |
1123 |
break;
|
1124 |
} |
1125 |
pt = &t->next; |
1126 |
} |
1127 |
} |
1128 |
|
1129 |
/* modify the current timer so that it will be fired when current_time
|
1130 |
>= expire_time. The corresponding callback will be called. */
|
1131 |
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
1132 |
{ |
1133 |
QEMUTimer **pt, *t; |
1134 |
|
1135 |
qemu_del_timer(ts); |
1136 |
|
1137 |
/* add the timer in the sorted list */
|
1138 |
/* NOTE: this code must be signal safe because
|
1139 |
qemu_timer_expired() can be called from a signal. */
|
1140 |
pt = &active_timers[ts->clock->type]; |
1141 |
for(;;) {
|
1142 |
t = *pt; |
1143 |
if (!t)
|
1144 |
break;
|
1145 |
if (t->expire_time > expire_time)
|
1146 |
break;
|
1147 |
pt = &t->next; |
1148 |
} |
1149 |
ts->expire_time = expire_time; |
1150 |
ts->next = *pt; |
1151 |
*pt = ts; |
1152 |
|
1153 |
/* Rearm if necessary */
|
1154 |
if (pt == &active_timers[ts->clock->type]) {
|
1155 |
if ((alarm_timer->flags & ALARM_FLAG_EXPIRED) == 0) { |
1156 |
qemu_rearm_alarm_timer(alarm_timer); |
1157 |
} |
1158 |
/* Interrupt execution to force deadline recalculation. */
|
1159 |
if (use_icount && cpu_single_env) {
|
1160 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
1161 |
} |
1162 |
} |
1163 |
} |
1164 |
|
1165 |
int qemu_timer_pending(QEMUTimer *ts)
|
1166 |
{ |
1167 |
QEMUTimer *t; |
1168 |
for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
1169 |
if (t == ts)
|
1170 |
return 1; |
1171 |
} |
1172 |
return 0; |
1173 |
} |
1174 |
|
1175 |
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
1176 |
{ |
1177 |
if (!timer_head)
|
1178 |
return 0; |
1179 |
return (timer_head->expire_time <= current_time);
|
1180 |
} |
1181 |
|
1182 |
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
1183 |
{ |
1184 |
QEMUTimer *ts; |
1185 |
|
1186 |
for(;;) {
|
1187 |
ts = *ptimer_head; |
1188 |
if (!ts || ts->expire_time > current_time)
|
1189 |
break;
|
1190 |
/* remove timer from the list before calling the callback */
|
1191 |
*ptimer_head = ts->next; |
1192 |
ts->next = NULL;
|
1193 |
|
1194 |
/* run the callback (the timer list can be modified) */
|
1195 |
ts->cb(ts->opaque); |
1196 |
} |
1197 |
} |
1198 |
|
1199 |
int64_t qemu_get_clock(QEMUClock *clock) |
1200 |
{ |
1201 |
switch(clock->type) {
|
1202 |
case QEMU_TIMER_REALTIME:
|
1203 |
return get_clock() / 1000000; |
1204 |
default:
|
1205 |
case QEMU_TIMER_VIRTUAL:
|
1206 |
if (use_icount) {
|
1207 |
return cpu_get_icount();
|
1208 |
} else {
|
1209 |
return cpu_get_clock();
|
1210 |
} |
1211 |
} |
1212 |
} |
1213 |
|
1214 |
static void init_timers(void) |
1215 |
{ |
1216 |
init_get_clock(); |
1217 |
ticks_per_sec = QEMU_TIMER_BASE; |
1218 |
rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
1219 |
vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
1220 |
} |
1221 |
|
1222 |
/* save a timer */
|
1223 |
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
1224 |
{ |
1225 |
uint64_t expire_time; |
1226 |
|
1227 |
if (qemu_timer_pending(ts)) {
|
1228 |
expire_time = ts->expire_time; |
1229 |
} else {
|
1230 |
expire_time = -1;
|
1231 |
} |
1232 |
qemu_put_be64(f, expire_time); |
1233 |
} |
1234 |
|
1235 |
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
1236 |
{ |
1237 |
uint64_t expire_time; |
1238 |
|
1239 |
expire_time = qemu_get_be64(f); |
1240 |
if (expire_time != -1) { |
1241 |
qemu_mod_timer(ts, expire_time); |
1242 |
} else {
|
1243 |
qemu_del_timer(ts); |
1244 |
} |
1245 |
} |
1246 |
|
1247 |
static void timer_save(QEMUFile *f, void *opaque) |
1248 |
{ |
1249 |
if (cpu_ticks_enabled) {
|
1250 |
hw_error("cannot save state if virtual timers are running");
|
1251 |
} |
1252 |
qemu_put_be64(f, cpu_ticks_offset); |
1253 |
qemu_put_be64(f, ticks_per_sec); |
1254 |
qemu_put_be64(f, cpu_clock_offset); |
1255 |
} |
1256 |
|
1257 |
static int timer_load(QEMUFile *f, void *opaque, int version_id) |
1258 |
{ |
1259 |
if (version_id != 1 && version_id != 2) |
1260 |
return -EINVAL;
|
1261 |
if (cpu_ticks_enabled) {
|
1262 |
return -EINVAL;
|
1263 |
} |
1264 |
cpu_ticks_offset=qemu_get_be64(f); |
1265 |
ticks_per_sec=qemu_get_be64(f); |
1266 |
if (version_id == 2) { |
1267 |
cpu_clock_offset=qemu_get_be64(f); |
1268 |
} |
1269 |
return 0; |
1270 |
} |
1271 |
|
1272 |
#ifdef _WIN32
|
1273 |
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
1274 |
DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
1275 |
#else
|
1276 |
static void host_alarm_handler(int host_signum) |
1277 |
#endif
|
1278 |
{ |
1279 |
#if 0
|
1280 |
#define DISP_FREQ 1000
|
1281 |
{
|
1282 |
static int64_t delta_min = INT64_MAX;
|
1283 |
static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
1284 |
static int count;
|
1285 |
ti = qemu_get_clock(vm_clock);
|
1286 |
if (last_clock != 0) {
|
1287 |
delta = ti - last_clock;
|
1288 |
if (delta < delta_min)
|
1289 |
delta_min = delta;
|
1290 |
if (delta > delta_max)
|
1291 |
delta_max = delta;
|
1292 |
delta_cum += delta;
|
1293 |
if (++count == DISP_FREQ) {
|
1294 |
printf("timer: min=%" PRId64 " us max=%" PRId64 " us avg=%" PRId64 " us avg_freq=%0.3f Hz\n",
|
1295 |
muldiv64(delta_min, 1000000, ticks_per_sec),
|
1296 |
muldiv64(delta_max, 1000000, ticks_per_sec),
|
1297 |
muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
1298 |
(double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
1299 |
count = 0;
|
1300 |
delta_min = INT64_MAX;
|
1301 |
delta_max = 0;
|
1302 |
delta_cum = 0;
|
1303 |
}
|
1304 |
}
|
1305 |
last_clock = ti;
|
1306 |
}
|
1307 |
#endif
|
1308 |
if (alarm_has_dynticks(alarm_timer) ||
|
1309 |
(!use_icount && |
1310 |
qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL], |
1311 |
qemu_get_clock(vm_clock))) || |
1312 |
qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
1313 |
qemu_get_clock(rt_clock))) { |
1314 |
#ifdef _WIN32
|
1315 |
struct qemu_alarm_win32 *data = ((struct qemu_alarm_timer*)dwUser)->priv; |
1316 |
SetEvent(data->host_alarm); |
1317 |
#endif
|
1318 |
CPUState *env = next_cpu; |
1319 |
|
1320 |
alarm_timer->flags |= ALARM_FLAG_EXPIRED; |
1321 |
|
1322 |
if (env) {
|
1323 |
/* stop the currently executing cpu because a timer occured */
|
1324 |
cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
1325 |
#ifdef USE_KQEMU
|
1326 |
if (env->kqemu_enabled) {
|
1327 |
kqemu_cpu_interrupt(env); |
1328 |
} |
1329 |
#endif
|
1330 |
} |
1331 |
event_pending = 1;
|
1332 |
} |
1333 |
} |
1334 |
|
1335 |
static int64_t qemu_next_deadline(void) |
1336 |
{ |
1337 |
int64_t delta; |
1338 |
|
1339 |
if (active_timers[QEMU_TIMER_VIRTUAL]) {
|
1340 |
delta = active_timers[QEMU_TIMER_VIRTUAL]->expire_time - |
1341 |
qemu_get_clock(vm_clock); |
1342 |
} else {
|
1343 |
/* To avoid problems with overflow limit this to 2^32. */
|
1344 |
delta = INT32_MAX; |
1345 |
} |
1346 |
|
1347 |
if (delta < 0) |
1348 |
delta = 0;
|
1349 |
|
1350 |
return delta;
|
1351 |
} |
1352 |
|
1353 |
static uint64_t qemu_next_deadline_dyntick(void) |
1354 |
{ |
1355 |
int64_t delta; |
1356 |
int64_t rtdelta; |
1357 |
|
1358 |
if (use_icount)
|
1359 |
delta = INT32_MAX; |
1360 |
else
|
1361 |
delta = (qemu_next_deadline() + 999) / 1000; |
1362 |
|
1363 |
if (active_timers[QEMU_TIMER_REALTIME]) {
|
1364 |
rtdelta = (active_timers[QEMU_TIMER_REALTIME]->expire_time - |
1365 |
qemu_get_clock(rt_clock))*1000;
|
1366 |
if (rtdelta < delta)
|
1367 |
delta = rtdelta; |
1368 |
} |
1369 |
|
1370 |
if (delta < MIN_TIMER_REARM_US)
|
1371 |
delta = MIN_TIMER_REARM_US; |
1372 |
|
1373 |
return delta;
|
1374 |
} |
1375 |
|
1376 |
#ifndef _WIN32
|
1377 |
|
1378 |
#if defined(__linux__)
|
1379 |
|
1380 |
#define RTC_FREQ 1024 |
1381 |
|
1382 |
static void enable_sigio_timer(int fd) |
1383 |
{ |
1384 |
struct sigaction act;
|
1385 |
|
1386 |
/* timer signal */
|
1387 |
sigfillset(&act.sa_mask); |
1388 |
act.sa_flags = 0;
|
1389 |
act.sa_handler = host_alarm_handler; |
1390 |
|
1391 |
sigaction(SIGIO, &act, NULL);
|
1392 |
fcntl(fd, F_SETFL, O_ASYNC); |
1393 |
fcntl(fd, F_SETOWN, getpid()); |
1394 |
} |
1395 |
|
1396 |
static int hpet_start_timer(struct qemu_alarm_timer *t) |
1397 |
{ |
1398 |
struct hpet_info info;
|
1399 |
int r, fd;
|
1400 |
|
1401 |
fd = open("/dev/hpet", O_RDONLY);
|
1402 |
if (fd < 0) |
1403 |
return -1; |
1404 |
|
1405 |
/* Set frequency */
|
1406 |
r = ioctl(fd, HPET_IRQFREQ, RTC_FREQ); |
1407 |
if (r < 0) { |
1408 |
fprintf(stderr, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
|
1409 |
"error, but for better emulation accuracy type:\n"
|
1410 |
"'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
|
1411 |
goto fail;
|
1412 |
} |
1413 |
|
1414 |
/* Check capabilities */
|
1415 |
r = ioctl(fd, HPET_INFO, &info); |
1416 |
if (r < 0) |
1417 |
goto fail;
|
1418 |
|
1419 |
/* Enable periodic mode */
|
1420 |
r = ioctl(fd, HPET_EPI, 0);
|
1421 |
if (info.hi_flags && (r < 0)) |
1422 |
goto fail;
|
1423 |
|
1424 |
/* Enable interrupt */
|
1425 |
r = ioctl(fd, HPET_IE_ON, 0);
|
1426 |
if (r < 0) |
1427 |
goto fail;
|
1428 |
|
1429 |
enable_sigio_timer(fd); |
1430 |
t->priv = (void *)(long)fd; |
1431 |
|
1432 |
return 0; |
1433 |
fail:
|
1434 |
close(fd); |
1435 |
return -1; |
1436 |
} |
1437 |
|
1438 |
static void hpet_stop_timer(struct qemu_alarm_timer *t) |
1439 |
{ |
1440 |
int fd = (long)t->priv; |
1441 |
|
1442 |
close(fd); |
1443 |
} |
1444 |
|
1445 |
static int rtc_start_timer(struct qemu_alarm_timer *t) |
1446 |
{ |
1447 |
int rtc_fd;
|
1448 |
unsigned long current_rtc_freq = 0; |
1449 |
|
1450 |
TFR(rtc_fd = open("/dev/rtc", O_RDONLY));
|
1451 |
if (rtc_fd < 0) |
1452 |
return -1; |
1453 |
ioctl(rtc_fd, RTC_IRQP_READ, ¤t_rtc_freq); |
1454 |
if (current_rtc_freq != RTC_FREQ &&
|
1455 |
ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
|
1456 |
fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
1457 |
"error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
1458 |
"type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
1459 |
goto fail;
|
1460 |
} |
1461 |
if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
1462 |
fail:
|
1463 |
close(rtc_fd); |
1464 |
return -1; |
1465 |
} |
1466 |
|
1467 |
enable_sigio_timer(rtc_fd); |
1468 |
|
1469 |
t->priv = (void *)(long)rtc_fd; |
1470 |
|
1471 |
return 0; |
1472 |
} |
1473 |
|
1474 |
static void rtc_stop_timer(struct qemu_alarm_timer *t) |
1475 |
{ |
1476 |
int rtc_fd = (long)t->priv; |
1477 |
|
1478 |
close(rtc_fd); |
1479 |
} |
1480 |
|
1481 |
static int dynticks_start_timer(struct qemu_alarm_timer *t) |
1482 |
{ |
1483 |
struct sigevent ev;
|
1484 |
timer_t host_timer; |
1485 |
struct sigaction act;
|
1486 |
|
1487 |
sigfillset(&act.sa_mask); |
1488 |
act.sa_flags = 0;
|
1489 |
act.sa_handler = host_alarm_handler; |
1490 |
|
1491 |
sigaction(SIGALRM, &act, NULL);
|
1492 |
|
1493 |
ev.sigev_value.sival_int = 0;
|
1494 |
ev.sigev_notify = SIGEV_SIGNAL; |
1495 |
ev.sigev_signo = SIGALRM; |
1496 |
|
1497 |
if (timer_create(CLOCK_REALTIME, &ev, &host_timer)) {
|
1498 |
perror("timer_create");
|
1499 |
|
1500 |
/* disable dynticks */
|
1501 |
fprintf(stderr, "Dynamic Ticks disabled\n");
|
1502 |
|
1503 |
return -1; |
1504 |
} |
1505 |
|
1506 |
t->priv = (void *)host_timer;
|
1507 |
|
1508 |
return 0; |
1509 |
} |
1510 |
|
1511 |
static void dynticks_stop_timer(struct qemu_alarm_timer *t) |
1512 |
{ |
1513 |
timer_t host_timer = (timer_t)t->priv; |
1514 |
|
1515 |
timer_delete(host_timer); |
1516 |
} |
1517 |
|
1518 |
static void dynticks_rearm_timer(struct qemu_alarm_timer *t) |
1519 |
{ |
1520 |
timer_t host_timer = (timer_t)t->priv; |
1521 |
struct itimerspec timeout;
|
1522 |
int64_t nearest_delta_us = INT64_MAX; |
1523 |
int64_t current_us; |
1524 |
|
1525 |
if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1526 |
!active_timers[QEMU_TIMER_VIRTUAL]) |
1527 |
return;
|
1528 |
|
1529 |
nearest_delta_us = qemu_next_deadline_dyntick(); |
1530 |
|
1531 |
/* check whether a timer is already running */
|
1532 |
if (timer_gettime(host_timer, &timeout)) {
|
1533 |
perror("gettime");
|
1534 |
fprintf(stderr, "Internal timer error: aborting\n");
|
1535 |
exit(1);
|
1536 |
} |
1537 |
current_us = timeout.it_value.tv_sec * 1000000 + timeout.it_value.tv_nsec/1000; |
1538 |
if (current_us && current_us <= nearest_delta_us)
|
1539 |
return;
|
1540 |
|
1541 |
timeout.it_interval.tv_sec = 0;
|
1542 |
timeout.it_interval.tv_nsec = 0; /* 0 for one-shot timer */ |
1543 |
timeout.it_value.tv_sec = nearest_delta_us / 1000000;
|
1544 |
timeout.it_value.tv_nsec = (nearest_delta_us % 1000000) * 1000; |
1545 |
if (timer_settime(host_timer, 0 /* RELATIVE */, &timeout, NULL)) { |
1546 |
perror("settime");
|
1547 |
fprintf(stderr, "Internal timer error: aborting\n");
|
1548 |
exit(1);
|
1549 |
} |
1550 |
} |
1551 |
|
1552 |
#endif /* defined(__linux__) */ |
1553 |
|
1554 |
static int unix_start_timer(struct qemu_alarm_timer *t) |
1555 |
{ |
1556 |
struct sigaction act;
|
1557 |
struct itimerval itv;
|
1558 |
int err;
|
1559 |
|
1560 |
/* timer signal */
|
1561 |
sigfillset(&act.sa_mask); |
1562 |
act.sa_flags = 0;
|
1563 |
act.sa_handler = host_alarm_handler; |
1564 |
|
1565 |
sigaction(SIGALRM, &act, NULL);
|
1566 |
|
1567 |
itv.it_interval.tv_sec = 0;
|
1568 |
/* for i386 kernel 2.6 to get 1 ms */
|
1569 |
itv.it_interval.tv_usec = 999;
|
1570 |
itv.it_value.tv_sec = 0;
|
1571 |
itv.it_value.tv_usec = 10 * 1000; |
1572 |
|
1573 |
err = setitimer(ITIMER_REAL, &itv, NULL);
|
1574 |
if (err)
|
1575 |
return -1; |
1576 |
|
1577 |
return 0; |
1578 |
} |
1579 |
|
1580 |
static void unix_stop_timer(struct qemu_alarm_timer *t) |
1581 |
{ |
1582 |
struct itimerval itv;
|
1583 |
|
1584 |
memset(&itv, 0, sizeof(itv)); |
1585 |
setitimer(ITIMER_REAL, &itv, NULL);
|
1586 |
} |
1587 |
|
1588 |
#endif /* !defined(_WIN32) */ |
1589 |
|
1590 |
#ifdef _WIN32
|
1591 |
|
1592 |
static int win32_start_timer(struct qemu_alarm_timer *t) |
1593 |
{ |
1594 |
TIMECAPS tc; |
1595 |
struct qemu_alarm_win32 *data = t->priv;
|
1596 |
UINT flags; |
1597 |
|
1598 |
data->host_alarm = CreateEvent(NULL, FALSE, FALSE, NULL); |
1599 |
if (!data->host_alarm) {
|
1600 |
perror("Failed CreateEvent");
|
1601 |
return -1; |
1602 |
} |
1603 |
|
1604 |
memset(&tc, 0, sizeof(tc)); |
1605 |
timeGetDevCaps(&tc, sizeof(tc));
|
1606 |
|
1607 |
if (data->period < tc.wPeriodMin)
|
1608 |
data->period = tc.wPeriodMin; |
1609 |
|
1610 |
timeBeginPeriod(data->period); |
1611 |
|
1612 |
flags = TIME_CALLBACK_FUNCTION; |
1613 |
if (alarm_has_dynticks(t))
|
1614 |
flags |= TIME_ONESHOT; |
1615 |
else
|
1616 |
flags |= TIME_PERIODIC; |
1617 |
|
1618 |
data->timerId = timeSetEvent(1, // interval (ms) |
1619 |
data->period, // resolution
|
1620 |
host_alarm_handler, // function
|
1621 |
(DWORD)t, // parameter
|
1622 |
flags); |
1623 |
|
1624 |
if (!data->timerId) {
|
1625 |
perror("Failed to initialize win32 alarm timer");
|
1626 |
|
1627 |
timeEndPeriod(data->period); |
1628 |
CloseHandle(data->host_alarm); |
1629 |
return -1; |
1630 |
} |
1631 |
|
1632 |
qemu_add_wait_object(data->host_alarm, NULL, NULL); |
1633 |
|
1634 |
return 0; |
1635 |
} |
1636 |
|
1637 |
static void win32_stop_timer(struct qemu_alarm_timer *t) |
1638 |
{ |
1639 |
struct qemu_alarm_win32 *data = t->priv;
|
1640 |
|
1641 |
timeKillEvent(data->timerId); |
1642 |
timeEndPeriod(data->period); |
1643 |
|
1644 |
CloseHandle(data->host_alarm); |
1645 |
} |
1646 |
|
1647 |
static void win32_rearm_timer(struct qemu_alarm_timer *t) |
1648 |
{ |
1649 |
struct qemu_alarm_win32 *data = t->priv;
|
1650 |
uint64_t nearest_delta_us; |
1651 |
|
1652 |
if (!active_timers[QEMU_TIMER_REALTIME] &&
|
1653 |
!active_timers[QEMU_TIMER_VIRTUAL]) |
1654 |
return;
|
1655 |
|
1656 |
nearest_delta_us = qemu_next_deadline_dyntick(); |
1657 |
nearest_delta_us /= 1000;
|
1658 |
|
1659 |
timeKillEvent(data->timerId); |
1660 |
|
1661 |
data->timerId = timeSetEvent(1,
|
1662 |
data->period, |
1663 |
host_alarm_handler, |
1664 |
(DWORD)t, |
1665 |
TIME_ONESHOT | TIME_PERIODIC); |
1666 |
|
1667 |
if (!data->timerId) {
|
1668 |
perror("Failed to re-arm win32 alarm timer");
|
1669 |
|
1670 |
timeEndPeriod(data->period); |
1671 |
CloseHandle(data->host_alarm); |
1672 |
exit(1);
|
1673 |
} |
1674 |
} |
1675 |
|
1676 |
#endif /* _WIN32 */ |
1677 |
|
1678 |
static void init_timer_alarm(void) |
1679 |
{ |
1680 |
struct qemu_alarm_timer *t;
|
1681 |
int i, err = -1; |
1682 |
|
1683 |
for (i = 0; alarm_timers[i].name; i++) { |
1684 |
t = &alarm_timers[i]; |
1685 |
|
1686 |
err = t->start(t); |
1687 |
if (!err)
|
1688 |
break;
|
1689 |
} |
1690 |
|
1691 |
if (err) {
|
1692 |
fprintf(stderr, "Unable to find any suitable alarm timer.\n");
|
1693 |
fprintf(stderr, "Terminating\n");
|
1694 |
exit(1);
|
1695 |
} |
1696 |
|
1697 |
alarm_timer = t; |
1698 |
} |
1699 |
|
1700 |
static void quit_timers(void) |
1701 |
{ |
1702 |
alarm_timer->stop(alarm_timer); |
1703 |
alarm_timer = NULL;
|
1704 |
} |
1705 |
|
1706 |
/***********************************************************/
|
1707 |
/* host time/date access */
|
1708 |
void qemu_get_timedate(struct tm *tm, int offset) |
1709 |
{ |
1710 |
time_t ti; |
1711 |
struct tm *ret;
|
1712 |
|
1713 |
time(&ti); |
1714 |
ti += offset; |
1715 |
if (rtc_date_offset == -1) { |
1716 |
if (rtc_utc)
|
1717 |
ret = gmtime(&ti); |
1718 |
else
|
1719 |
ret = localtime(&ti); |
1720 |
} else {
|
1721 |
ti -= rtc_date_offset; |
1722 |
ret = gmtime(&ti); |
1723 |
} |
1724 |
|
1725 |
memcpy(tm, ret, sizeof(struct tm)); |
1726 |
} |
1727 |
|
1728 |
int qemu_timedate_diff(struct tm *tm) |
1729 |
{ |
1730 |
time_t seconds; |
1731 |
|
1732 |
if (rtc_date_offset == -1) |
1733 |
if (rtc_utc)
|
1734 |
seconds = mktimegm(tm); |
1735 |
else
|
1736 |
seconds = mktime(tm); |
1737 |
else
|
1738 |
seconds = mktimegm(tm) + rtc_date_offset; |
1739 |
|
1740 |
return seconds - time(NULL); |
1741 |
} |
1742 |
|
1743 |
/***********************************************************/
|
1744 |
/* character device */
|
1745 |
|
1746 |
static void qemu_chr_event(CharDriverState *s, int event) |
1747 |
{ |
1748 |
if (!s->chr_event)
|
1749 |
return;
|
1750 |
s->chr_event(s->handler_opaque, event); |
1751 |
} |
1752 |
|
1753 |
static void qemu_chr_reset_bh(void *opaque) |
1754 |
{ |
1755 |
CharDriverState *s = opaque; |
1756 |
qemu_chr_event(s, CHR_EVENT_RESET); |
1757 |
qemu_bh_delete(s->bh); |
1758 |
s->bh = NULL;
|
1759 |
} |
1760 |
|
1761 |
void qemu_chr_reset(CharDriverState *s)
|
1762 |
{ |
1763 |
if (s->bh == NULL) { |
1764 |
s->bh = qemu_bh_new(qemu_chr_reset_bh, s); |
1765 |
qemu_bh_schedule(s->bh); |
1766 |
} |
1767 |
} |
1768 |
|
1769 |
int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
1770 |
{ |
1771 |
return s->chr_write(s, buf, len);
|
1772 |
} |
1773 |
|
1774 |
int qemu_chr_ioctl(CharDriverState *s, int cmd, void *arg) |
1775 |
{ |
1776 |
if (!s->chr_ioctl)
|
1777 |
return -ENOTSUP;
|
1778 |
return s->chr_ioctl(s, cmd, arg);
|
1779 |
} |
1780 |
|
1781 |
int qemu_chr_can_read(CharDriverState *s)
|
1782 |
{ |
1783 |
if (!s->chr_can_read)
|
1784 |
return 0; |
1785 |
return s->chr_can_read(s->handler_opaque);
|
1786 |
} |
1787 |
|
1788 |
void qemu_chr_read(CharDriverState *s, uint8_t *buf, int len) |
1789 |
{ |
1790 |
s->chr_read(s->handler_opaque, buf, len); |
1791 |
} |
1792 |
|
1793 |
void qemu_chr_accept_input(CharDriverState *s)
|
1794 |
{ |
1795 |
if (s->chr_accept_input)
|
1796 |
s->chr_accept_input(s); |
1797 |
} |
1798 |
|
1799 |
void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
1800 |
{ |
1801 |
char buf[4096]; |
1802 |
va_list ap; |
1803 |
va_start(ap, fmt); |
1804 |
vsnprintf(buf, sizeof(buf), fmt, ap);
|
1805 |
qemu_chr_write(s, (uint8_t *)buf, strlen(buf)); |
1806 |
va_end(ap); |
1807 |
} |
1808 |
|
1809 |
void qemu_chr_send_event(CharDriverState *s, int event) |
1810 |
{ |
1811 |
if (s->chr_send_event)
|
1812 |
s->chr_send_event(s, event); |
1813 |
} |
1814 |
|
1815 |
void qemu_chr_add_handlers(CharDriverState *s,
|
1816 |
IOCanRWHandler *fd_can_read, |
1817 |
IOReadHandler *fd_read, |
1818 |
IOEventHandler *fd_event, |
1819 |
void *opaque)
|
1820 |
{ |
1821 |
s->chr_can_read = fd_can_read; |
1822 |
s->chr_read = fd_read; |
1823 |
s->chr_event = fd_event; |
1824 |
s->handler_opaque = opaque; |
1825 |
if (s->chr_update_read_handler)
|
1826 |
s->chr_update_read_handler(s); |
1827 |
} |
1828 |
|
1829 |
static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1830 |
{ |
1831 |
return len;
|
1832 |
} |
1833 |
|
1834 |
static CharDriverState *qemu_chr_open_null(void) |
1835 |
{ |
1836 |
CharDriverState *chr; |
1837 |
|
1838 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
1839 |
if (!chr)
|
1840 |
return NULL; |
1841 |
chr->chr_write = null_chr_write; |
1842 |
return chr;
|
1843 |
} |
1844 |
|
1845 |
/* MUX driver for serial I/O splitting */
|
1846 |
static int term_timestamps; |
1847 |
static int64_t term_timestamps_start;
|
1848 |
#define MAX_MUX 4 |
1849 |
#define MUX_BUFFER_SIZE 32 /* Must be a power of 2. */ |
1850 |
#define MUX_BUFFER_MASK (MUX_BUFFER_SIZE - 1) |
1851 |
typedef struct { |
1852 |
IOCanRWHandler *chr_can_read[MAX_MUX]; |
1853 |
IOReadHandler *chr_read[MAX_MUX]; |
1854 |
IOEventHandler *chr_event[MAX_MUX]; |
1855 |
void *ext_opaque[MAX_MUX];
|
1856 |
CharDriverState *drv; |
1857 |
unsigned char buffer[MUX_BUFFER_SIZE]; |
1858 |
int prod;
|
1859 |
int cons;
|
1860 |
int mux_cnt;
|
1861 |
int term_got_escape;
|
1862 |
int max_size;
|
1863 |
} MuxDriver; |
1864 |
|
1865 |
|
1866 |
static int mux_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1867 |
{ |
1868 |
MuxDriver *d = chr->opaque; |
1869 |
int ret;
|
1870 |
if (!term_timestamps) {
|
1871 |
ret = d->drv->chr_write(d->drv, buf, len); |
1872 |
} else {
|
1873 |
int i;
|
1874 |
|
1875 |
ret = 0;
|
1876 |
for(i = 0; i < len; i++) { |
1877 |
ret += d->drv->chr_write(d->drv, buf+i, 1);
|
1878 |
if (buf[i] == '\n') { |
1879 |
char buf1[64]; |
1880 |
int64_t ti; |
1881 |
int secs;
|
1882 |
|
1883 |
ti = get_clock(); |
1884 |
if (term_timestamps_start == -1) |
1885 |
term_timestamps_start = ti; |
1886 |
ti -= term_timestamps_start; |
1887 |
secs = ti / 1000000000;
|
1888 |
snprintf(buf1, sizeof(buf1),
|
1889 |
"[%02d:%02d:%02d.%03d] ",
|
1890 |
secs / 3600,
|
1891 |
(secs / 60) % 60, |
1892 |
secs % 60,
|
1893 |
(int)((ti / 1000000) % 1000)); |
1894 |
d->drv->chr_write(d->drv, (uint8_t *)buf1, strlen(buf1)); |
1895 |
} |
1896 |
} |
1897 |
} |
1898 |
return ret;
|
1899 |
} |
1900 |
|
1901 |
static char *mux_help[] = { |
1902 |
"% h print this help\n\r",
|
1903 |
"% x exit emulator\n\r",
|
1904 |
"% s save disk data back to file (if -snapshot)\n\r",
|
1905 |
"% t toggle console timestamps\n\r"
|
1906 |
"% b send break (magic sysrq)\n\r",
|
1907 |
"% c switch between console and monitor\n\r",
|
1908 |
"% % sends %\n\r",
|
1909 |
NULL
|
1910 |
}; |
1911 |
|
1912 |
static int term_escape_char = 0x01; /* ctrl-a is used for escape */ |
1913 |
static void mux_print_help(CharDriverState *chr) |
1914 |
{ |
1915 |
int i, j;
|
1916 |
char ebuf[15] = "Escape-Char"; |
1917 |
char cbuf[50] = "\n\r"; |
1918 |
|
1919 |
if (term_escape_char > 0 && term_escape_char < 26) { |
1920 |
snprintf(cbuf, sizeof(cbuf), "\n\r"); |
1921 |
snprintf(ebuf, sizeof(ebuf), "C-%c", term_escape_char - 1 + 'a'); |
1922 |
} else {
|
1923 |
snprintf(cbuf, sizeof(cbuf),
|
1924 |
"\n\rEscape-Char set to Ascii: 0x%02x\n\r\n\r",
|
1925 |
term_escape_char); |
1926 |
} |
1927 |
chr->chr_write(chr, (uint8_t *)cbuf, strlen(cbuf)); |
1928 |
for (i = 0; mux_help[i] != NULL; i++) { |
1929 |
for (j=0; mux_help[i][j] != '\0'; j++) { |
1930 |
if (mux_help[i][j] == '%') |
1931 |
chr->chr_write(chr, (uint8_t *)ebuf, strlen(ebuf)); |
1932 |
else
|
1933 |
chr->chr_write(chr, (uint8_t *)&mux_help[i][j], 1);
|
1934 |
} |
1935 |
} |
1936 |
} |
1937 |
|
1938 |
static int mux_proc_byte(CharDriverState *chr, MuxDriver *d, int ch) |
1939 |
{ |
1940 |
if (d->term_got_escape) {
|
1941 |
d->term_got_escape = 0;
|
1942 |
if (ch == term_escape_char)
|
1943 |
goto send_char;
|
1944 |
switch(ch) {
|
1945 |
case '?': |
1946 |
case 'h': |
1947 |
mux_print_help(chr); |
1948 |
break;
|
1949 |
case 'x': |
1950 |
{ |
1951 |
char *term = "QEMU: Terminated\n\r"; |
1952 |
chr->chr_write(chr,(uint8_t *)term,strlen(term)); |
1953 |
exit(0);
|
1954 |
break;
|
1955 |
} |
1956 |
case 's': |
1957 |
{ |
1958 |
int i;
|
1959 |
for (i = 0; i < nb_drives; i++) { |
1960 |
bdrv_commit(drives_table[i].bdrv); |
1961 |
} |
1962 |
} |
1963 |
break;
|
1964 |
case 'b': |
1965 |
qemu_chr_event(chr, CHR_EVENT_BREAK); |
1966 |
break;
|
1967 |
case 'c': |
1968 |
/* Switch to the next registered device */
|
1969 |
chr->focus++; |
1970 |
if (chr->focus >= d->mux_cnt)
|
1971 |
chr->focus = 0;
|
1972 |
break;
|
1973 |
case 't': |
1974 |
term_timestamps = !term_timestamps; |
1975 |
term_timestamps_start = -1;
|
1976 |
break;
|
1977 |
} |
1978 |
} else if (ch == term_escape_char) { |
1979 |
d->term_got_escape = 1;
|
1980 |
} else {
|
1981 |
send_char:
|
1982 |
return 1; |
1983 |
} |
1984 |
return 0; |
1985 |
} |
1986 |
|
1987 |
static void mux_chr_accept_input(CharDriverState *chr) |
1988 |
{ |
1989 |
int m = chr->focus;
|
1990 |
MuxDriver *d = chr->opaque; |
1991 |
|
1992 |
while (d->prod != d->cons &&
|
1993 |
d->chr_can_read[m] && |
1994 |
d->chr_can_read[m](d->ext_opaque[m])) { |
1995 |
d->chr_read[m](d->ext_opaque[m], |
1996 |
&d->buffer[d->cons++ & MUX_BUFFER_MASK], 1);
|
1997 |
} |
1998 |
} |
1999 |
|
2000 |
static int mux_chr_can_read(void *opaque) |
2001 |
{ |
2002 |
CharDriverState *chr = opaque; |
2003 |
MuxDriver *d = chr->opaque; |
2004 |
|
2005 |
if ((d->prod - d->cons) < MUX_BUFFER_SIZE)
|
2006 |
return 1; |
2007 |
if (d->chr_can_read[chr->focus])
|
2008 |
return d->chr_can_read[chr->focus](d->ext_opaque[chr->focus]);
|
2009 |
return 0; |
2010 |
} |
2011 |
|
2012 |
static void mux_chr_read(void *opaque, const uint8_t *buf, int size) |
2013 |
{ |
2014 |
CharDriverState *chr = opaque; |
2015 |
MuxDriver *d = chr->opaque; |
2016 |
int m = chr->focus;
|
2017 |
int i;
|
2018 |
|
2019 |
mux_chr_accept_input (opaque); |
2020 |
|
2021 |
for(i = 0; i < size; i++) |
2022 |
if (mux_proc_byte(chr, d, buf[i])) {
|
2023 |
if (d->prod == d->cons &&
|
2024 |
d->chr_can_read[m] && |
2025 |
d->chr_can_read[m](d->ext_opaque[m])) |
2026 |
d->chr_read[m](d->ext_opaque[m], &buf[i], 1);
|
2027 |
else
|
2028 |
d->buffer[d->prod++ & MUX_BUFFER_MASK] = buf[i]; |
2029 |
} |
2030 |
} |
2031 |
|
2032 |
static void mux_chr_event(void *opaque, int event) |
2033 |
{ |
2034 |
CharDriverState *chr = opaque; |
2035 |
MuxDriver *d = chr->opaque; |
2036 |
int i;
|
2037 |
|
2038 |
/* Send the event to all registered listeners */
|
2039 |
for (i = 0; i < d->mux_cnt; i++) |
2040 |
if (d->chr_event[i])
|
2041 |
d->chr_event[i](d->ext_opaque[i], event); |
2042 |
} |
2043 |
|
2044 |
static void mux_chr_update_read_handler(CharDriverState *chr) |
2045 |
{ |
2046 |
MuxDriver *d = chr->opaque; |
2047 |
|
2048 |
if (d->mux_cnt >= MAX_MUX) {
|
2049 |
fprintf(stderr, "Cannot add I/O handlers, MUX array is full\n");
|
2050 |
return;
|
2051 |
} |
2052 |
d->ext_opaque[d->mux_cnt] = chr->handler_opaque; |
2053 |
d->chr_can_read[d->mux_cnt] = chr->chr_can_read; |
2054 |
d->chr_read[d->mux_cnt] = chr->chr_read; |
2055 |
d->chr_event[d->mux_cnt] = chr->chr_event; |
2056 |
/* Fix up the real driver with mux routines */
|
2057 |
if (d->mux_cnt == 0) { |
2058 |
qemu_chr_add_handlers(d->drv, mux_chr_can_read, mux_chr_read, |
2059 |
mux_chr_event, chr); |
2060 |
} |
2061 |
chr->focus = d->mux_cnt; |
2062 |
d->mux_cnt++; |
2063 |
} |
2064 |
|
2065 |
static CharDriverState *qemu_chr_open_mux(CharDriverState *drv)
|
2066 |
{ |
2067 |
CharDriverState *chr; |
2068 |
MuxDriver *d; |
2069 |
|
2070 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2071 |
if (!chr)
|
2072 |
return NULL; |
2073 |
d = qemu_mallocz(sizeof(MuxDriver));
|
2074 |
if (!d) {
|
2075 |
free(chr); |
2076 |
return NULL; |
2077 |
} |
2078 |
|
2079 |
chr->opaque = d; |
2080 |
d->drv = drv; |
2081 |
chr->focus = -1;
|
2082 |
chr->chr_write = mux_chr_write; |
2083 |
chr->chr_update_read_handler = mux_chr_update_read_handler; |
2084 |
chr->chr_accept_input = mux_chr_accept_input; |
2085 |
return chr;
|
2086 |
} |
2087 |
|
2088 |
|
2089 |
#ifdef _WIN32
|
2090 |
|
2091 |
static void socket_cleanup(void) |
2092 |
{ |
2093 |
WSACleanup(); |
2094 |
} |
2095 |
|
2096 |
static int socket_init(void) |
2097 |
{ |
2098 |
WSADATA Data; |
2099 |
int ret, err;
|
2100 |
|
2101 |
ret = WSAStartup(MAKEWORD(2,2), &Data); |
2102 |
if (ret != 0) { |
2103 |
err = WSAGetLastError(); |
2104 |
fprintf(stderr, "WSAStartup: %d\n", err);
|
2105 |
return -1; |
2106 |
} |
2107 |
atexit(socket_cleanup); |
2108 |
return 0; |
2109 |
} |
2110 |
|
2111 |
static int send_all(int fd, const uint8_t *buf, int len1) |
2112 |
{ |
2113 |
int ret, len;
|
2114 |
|
2115 |
len = len1; |
2116 |
while (len > 0) { |
2117 |
ret = send(fd, buf, len, 0);
|
2118 |
if (ret < 0) { |
2119 |
int errno;
|
2120 |
errno = WSAGetLastError(); |
2121 |
if (errno != WSAEWOULDBLOCK) {
|
2122 |
return -1; |
2123 |
} |
2124 |
} else if (ret == 0) { |
2125 |
break;
|
2126 |
} else {
|
2127 |
buf += ret; |
2128 |
len -= ret; |
2129 |
} |
2130 |
} |
2131 |
return len1 - len;
|
2132 |
} |
2133 |
|
2134 |
void socket_set_nonblock(int fd) |
2135 |
{ |
2136 |
unsigned long opt = 1; |
2137 |
ioctlsocket(fd, FIONBIO, &opt); |
2138 |
} |
2139 |
|
2140 |
#else
|
2141 |
|
2142 |
static int unix_write(int fd, const uint8_t *buf, int len1) |
2143 |
{ |
2144 |
int ret, len;
|
2145 |
|
2146 |
len = len1; |
2147 |
while (len > 0) { |
2148 |
ret = write(fd, buf, len); |
2149 |
if (ret < 0) { |
2150 |
if (errno != EINTR && errno != EAGAIN)
|
2151 |
return -1; |
2152 |
} else if (ret == 0) { |
2153 |
break;
|
2154 |
} else {
|
2155 |
buf += ret; |
2156 |
len -= ret; |
2157 |
} |
2158 |
} |
2159 |
return len1 - len;
|
2160 |
} |
2161 |
|
2162 |
static inline int send_all(int fd, const uint8_t *buf, int len1) |
2163 |
{ |
2164 |
return unix_write(fd, buf, len1);
|
2165 |
} |
2166 |
|
2167 |
void socket_set_nonblock(int fd) |
2168 |
{ |
2169 |
int f;
|
2170 |
f = fcntl(fd, F_GETFL); |
2171 |
fcntl(fd, F_SETFL, f | O_NONBLOCK); |
2172 |
} |
2173 |
#endif /* !_WIN32 */ |
2174 |
|
2175 |
#ifndef _WIN32
|
2176 |
|
2177 |
typedef struct { |
2178 |
int fd_in, fd_out;
|
2179 |
int max_size;
|
2180 |
} FDCharDriver; |
2181 |
|
2182 |
#define STDIO_MAX_CLIENTS 1 |
2183 |
static int stdio_nb_clients = 0; |
2184 |
|
2185 |
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2186 |
{ |
2187 |
FDCharDriver *s = chr->opaque; |
2188 |
return unix_write(s->fd_out, buf, len);
|
2189 |
} |
2190 |
|
2191 |
static int fd_chr_read_poll(void *opaque) |
2192 |
{ |
2193 |
CharDriverState *chr = opaque; |
2194 |
FDCharDriver *s = chr->opaque; |
2195 |
|
2196 |
s->max_size = qemu_chr_can_read(chr); |
2197 |
return s->max_size;
|
2198 |
} |
2199 |
|
2200 |
static void fd_chr_read(void *opaque) |
2201 |
{ |
2202 |
CharDriverState *chr = opaque; |
2203 |
FDCharDriver *s = chr->opaque; |
2204 |
int size, len;
|
2205 |
uint8_t buf[1024];
|
2206 |
|
2207 |
len = sizeof(buf);
|
2208 |
if (len > s->max_size)
|
2209 |
len = s->max_size; |
2210 |
if (len == 0) |
2211 |
return;
|
2212 |
size = read(s->fd_in, buf, len); |
2213 |
if (size == 0) { |
2214 |
/* FD has been closed. Remove it from the active list. */
|
2215 |
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2216 |
return;
|
2217 |
} |
2218 |
if (size > 0) { |
2219 |
qemu_chr_read(chr, buf, size); |
2220 |
} |
2221 |
} |
2222 |
|
2223 |
static void fd_chr_update_read_handler(CharDriverState *chr) |
2224 |
{ |
2225 |
FDCharDriver *s = chr->opaque; |
2226 |
|
2227 |
if (s->fd_in >= 0) { |
2228 |
if (nographic && s->fd_in == 0) { |
2229 |
} else {
|
2230 |
qemu_set_fd_handler2(s->fd_in, fd_chr_read_poll, |
2231 |
fd_chr_read, NULL, chr);
|
2232 |
} |
2233 |
} |
2234 |
} |
2235 |
|
2236 |
static void fd_chr_close(struct CharDriverState *chr) |
2237 |
{ |
2238 |
FDCharDriver *s = chr->opaque; |
2239 |
|
2240 |
if (s->fd_in >= 0) { |
2241 |
if (nographic && s->fd_in == 0) { |
2242 |
} else {
|
2243 |
qemu_set_fd_handler2(s->fd_in, NULL, NULL, NULL, NULL); |
2244 |
} |
2245 |
} |
2246 |
|
2247 |
qemu_free(s); |
2248 |
} |
2249 |
|
2250 |
/* open a character device to a unix fd */
|
2251 |
static CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
2252 |
{ |
2253 |
CharDriverState *chr; |
2254 |
FDCharDriver *s; |
2255 |
|
2256 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2257 |
if (!chr)
|
2258 |
return NULL; |
2259 |
s = qemu_mallocz(sizeof(FDCharDriver));
|
2260 |
if (!s) {
|
2261 |
free(chr); |
2262 |
return NULL; |
2263 |
} |
2264 |
s->fd_in = fd_in; |
2265 |
s->fd_out = fd_out; |
2266 |
chr->opaque = s; |
2267 |
chr->chr_write = fd_chr_write; |
2268 |
chr->chr_update_read_handler = fd_chr_update_read_handler; |
2269 |
chr->chr_close = fd_chr_close; |
2270 |
|
2271 |
qemu_chr_reset(chr); |
2272 |
|
2273 |
return chr;
|
2274 |
} |
2275 |
|
2276 |
static CharDriverState *qemu_chr_open_file_out(const char *file_out) |
2277 |
{ |
2278 |
int fd_out;
|
2279 |
|
2280 |
TFR(fd_out = open(file_out, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, 0666));
|
2281 |
if (fd_out < 0) |
2282 |
return NULL; |
2283 |
return qemu_chr_open_fd(-1, fd_out); |
2284 |
} |
2285 |
|
2286 |
static CharDriverState *qemu_chr_open_pipe(const char *filename) |
2287 |
{ |
2288 |
int fd_in, fd_out;
|
2289 |
char filename_in[256], filename_out[256]; |
2290 |
|
2291 |
snprintf(filename_in, 256, "%s.in", filename); |
2292 |
snprintf(filename_out, 256, "%s.out", filename); |
2293 |
TFR(fd_in = open(filename_in, O_RDWR | O_BINARY)); |
2294 |
TFR(fd_out = open(filename_out, O_RDWR | O_BINARY)); |
2295 |
if (fd_in < 0 || fd_out < 0) { |
2296 |
if (fd_in >= 0) |
2297 |
close(fd_in); |
2298 |
if (fd_out >= 0) |
2299 |
close(fd_out); |
2300 |
TFR(fd_in = fd_out = open(filename, O_RDWR | O_BINARY)); |
2301 |
if (fd_in < 0) |
2302 |
return NULL; |
2303 |
} |
2304 |
return qemu_chr_open_fd(fd_in, fd_out);
|
2305 |
} |
2306 |
|
2307 |
|
2308 |
/* for STDIO, we handle the case where several clients use it
|
2309 |
(nographic mode) */
|
2310 |
|
2311 |
#define TERM_FIFO_MAX_SIZE 1 |
2312 |
|
2313 |
static uint8_t term_fifo[TERM_FIFO_MAX_SIZE];
|
2314 |
static int term_fifo_size; |
2315 |
|
2316 |
static int stdio_read_poll(void *opaque) |
2317 |
{ |
2318 |
CharDriverState *chr = opaque; |
2319 |
|
2320 |
/* try to flush the queue if needed */
|
2321 |
if (term_fifo_size != 0 && qemu_chr_can_read(chr) > 0) { |
2322 |
qemu_chr_read(chr, term_fifo, 1);
|
2323 |
term_fifo_size = 0;
|
2324 |
} |
2325 |
/* see if we can absorb more chars */
|
2326 |
if (term_fifo_size == 0) |
2327 |
return 1; |
2328 |
else
|
2329 |
return 0; |
2330 |
} |
2331 |
|
2332 |
static void stdio_read(void *opaque) |
2333 |
{ |
2334 |
int size;
|
2335 |
uint8_t buf[1];
|
2336 |
CharDriverState *chr = opaque; |
2337 |
|
2338 |
size = read(0, buf, 1); |
2339 |
if (size == 0) { |
2340 |
/* stdin has been closed. Remove it from the active list. */
|
2341 |
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2342 |
return;
|
2343 |
} |
2344 |
if (size > 0) { |
2345 |
if (qemu_chr_can_read(chr) > 0) { |
2346 |
qemu_chr_read(chr, buf, 1);
|
2347 |
} else if (term_fifo_size == 0) { |
2348 |
term_fifo[term_fifo_size++] = buf[0];
|
2349 |
} |
2350 |
} |
2351 |
} |
2352 |
|
2353 |
/* init terminal so that we can grab keys */
|
2354 |
static struct termios oldtty; |
2355 |
static int old_fd0_flags; |
2356 |
static int term_atexit_done; |
2357 |
|
2358 |
static void term_exit(void) |
2359 |
{ |
2360 |
tcsetattr (0, TCSANOW, &oldtty);
|
2361 |
fcntl(0, F_SETFL, old_fd0_flags);
|
2362 |
} |
2363 |
|
2364 |
static void term_init(void) |
2365 |
{ |
2366 |
struct termios tty;
|
2367 |
|
2368 |
tcgetattr (0, &tty);
|
2369 |
oldtty = tty; |
2370 |
old_fd0_flags = fcntl(0, F_GETFL);
|
2371 |
|
2372 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2373 |
|INLCR|IGNCR|ICRNL|IXON); |
2374 |
tty.c_oflag |= OPOST; |
2375 |
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
2376 |
/* if graphical mode, we allow Ctrl-C handling */
|
2377 |
if (nographic)
|
2378 |
tty.c_lflag &= ~ISIG; |
2379 |
tty.c_cflag &= ~(CSIZE|PARENB); |
2380 |
tty.c_cflag |= CS8; |
2381 |
tty.c_cc[VMIN] = 1;
|
2382 |
tty.c_cc[VTIME] = 0;
|
2383 |
|
2384 |
tcsetattr (0, TCSANOW, &tty);
|
2385 |
|
2386 |
if (!term_atexit_done++)
|
2387 |
atexit(term_exit); |
2388 |
|
2389 |
fcntl(0, F_SETFL, O_NONBLOCK);
|
2390 |
} |
2391 |
|
2392 |
static void qemu_chr_close_stdio(struct CharDriverState *chr) |
2393 |
{ |
2394 |
term_exit(); |
2395 |
stdio_nb_clients--; |
2396 |
qemu_set_fd_handler2(0, NULL, NULL, NULL, NULL); |
2397 |
fd_chr_close(chr); |
2398 |
} |
2399 |
|
2400 |
static CharDriverState *qemu_chr_open_stdio(void) |
2401 |
{ |
2402 |
CharDriverState *chr; |
2403 |
|
2404 |
if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
2405 |
return NULL; |
2406 |
chr = qemu_chr_open_fd(0, 1); |
2407 |
chr->chr_close = qemu_chr_close_stdio; |
2408 |
qemu_set_fd_handler2(0, stdio_read_poll, stdio_read, NULL, chr); |
2409 |
stdio_nb_clients++; |
2410 |
term_init(); |
2411 |
|
2412 |
return chr;
|
2413 |
} |
2414 |
|
2415 |
#ifdef __sun__
|
2416 |
/* Once Solaris has openpty(), this is going to be removed. */
|
2417 |
int openpty(int *amaster, int *aslave, char *name, |
2418 |
struct termios *termp, struct winsize *winp) |
2419 |
{ |
2420 |
const char *slave; |
2421 |
int mfd = -1, sfd = -1; |
2422 |
|
2423 |
*amaster = *aslave = -1;
|
2424 |
|
2425 |
mfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
|
2426 |
if (mfd < 0) |
2427 |
goto err;
|
2428 |
|
2429 |
if (grantpt(mfd) == -1 || unlockpt(mfd) == -1) |
2430 |
goto err;
|
2431 |
|
2432 |
if ((slave = ptsname(mfd)) == NULL) |
2433 |
goto err;
|
2434 |
|
2435 |
if ((sfd = open(slave, O_RDONLY | O_NOCTTY)) == -1) |
2436 |
goto err;
|
2437 |
|
2438 |
if (ioctl(sfd, I_PUSH, "ptem") == -1 || |
2439 |
(termp != NULL && tcgetattr(sfd, termp) < 0)) |
2440 |
goto err;
|
2441 |
|
2442 |
if (amaster)
|
2443 |
*amaster = mfd; |
2444 |
if (aslave)
|
2445 |
*aslave = sfd; |
2446 |
if (winp)
|
2447 |
ioctl(sfd, TIOCSWINSZ, winp); |
2448 |
|
2449 |
return 0; |
2450 |
|
2451 |
err:
|
2452 |
if (sfd != -1) |
2453 |
close(sfd); |
2454 |
close(mfd); |
2455 |
return -1; |
2456 |
} |
2457 |
|
2458 |
void cfmakeraw (struct termios *termios_p) |
2459 |
{ |
2460 |
termios_p->c_iflag &= |
2461 |
~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); |
2462 |
termios_p->c_oflag &= ~OPOST; |
2463 |
termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); |
2464 |
termios_p->c_cflag &= ~(CSIZE|PARENB); |
2465 |
termios_p->c_cflag |= CS8; |
2466 |
|
2467 |
termios_p->c_cc[VMIN] = 0;
|
2468 |
termios_p->c_cc[VTIME] = 0;
|
2469 |
} |
2470 |
#endif
|
2471 |
|
2472 |
#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
2473 |
|| defined(__NetBSD__) || defined(__OpenBSD__) |
2474 |
|
2475 |
typedef struct { |
2476 |
int fd;
|
2477 |
int connected;
|
2478 |
int polling;
|
2479 |
int read_bytes;
|
2480 |
QEMUTimer *timer; |
2481 |
} PtyCharDriver; |
2482 |
|
2483 |
static void pty_chr_update_read_handler(CharDriverState *chr); |
2484 |
static void pty_chr_state(CharDriverState *chr, int connected); |
2485 |
|
2486 |
static int pty_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
2487 |
{ |
2488 |
PtyCharDriver *s = chr->opaque; |
2489 |
|
2490 |
if (!s->connected) {
|
2491 |
/* guest sends data, check for (re-)connect */
|
2492 |
pty_chr_update_read_handler(chr); |
2493 |
return 0; |
2494 |
} |
2495 |
return unix_write(s->fd, buf, len);
|
2496 |
} |
2497 |
|
2498 |
static int pty_chr_read_poll(void *opaque) |
2499 |
{ |
2500 |
CharDriverState *chr = opaque; |
2501 |
PtyCharDriver *s = chr->opaque; |
2502 |
|
2503 |
s->read_bytes = qemu_chr_can_read(chr); |
2504 |
return s->read_bytes;
|
2505 |
} |
2506 |
|
2507 |
static void pty_chr_read(void *opaque) |
2508 |
{ |
2509 |
CharDriverState *chr = opaque; |
2510 |
PtyCharDriver *s = chr->opaque; |
2511 |
int size, len;
|
2512 |
uint8_t buf[1024];
|
2513 |
|
2514 |
len = sizeof(buf);
|
2515 |
if (len > s->read_bytes)
|
2516 |
len = s->read_bytes; |
2517 |
if (len == 0) |
2518 |
return;
|
2519 |
size = read(s->fd, buf, len); |
2520 |
if ((size == -1 && errno == EIO) || |
2521 |
(size == 0)) {
|
2522 |
pty_chr_state(chr, 0);
|
2523 |
return;
|
2524 |
} |
2525 |
if (size > 0) { |
2526 |
pty_chr_state(chr, 1);
|
2527 |
qemu_chr_read(chr, buf, size); |
2528 |
} |
2529 |
} |
2530 |
|
2531 |
static void pty_chr_update_read_handler(CharDriverState *chr) |
2532 |
{ |
2533 |
PtyCharDriver *s = chr->opaque; |
2534 |
|
2535 |
qemu_set_fd_handler2(s->fd, pty_chr_read_poll, |
2536 |
pty_chr_read, NULL, chr);
|
2537 |
s->polling = 1;
|
2538 |
/*
|
2539 |
* Short timeout here: just need wait long enougth that qemu makes
|
2540 |
* it through the poll loop once. When reconnected we want a
|
2541 |
* short timeout so we notice it almost instantly. Otherwise
|
2542 |
* read() gives us -EIO instantly, making pty_chr_state() reset the
|
2543 |
* timeout to the normal (much longer) poll interval before the
|
2544 |
* timer triggers.
|
2545 |
*/
|
2546 |
qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 10);
|
2547 |
} |
2548 |
|
2549 |
static void pty_chr_state(CharDriverState *chr, int connected) |
2550 |
{ |
2551 |
PtyCharDriver *s = chr->opaque; |
2552 |
|
2553 |
if (!connected) {
|
2554 |
qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2555 |
s->connected = 0;
|
2556 |
s->polling = 0;
|
2557 |
/* (re-)connect poll interval for idle guests: once per second.
|
2558 |
* We check more frequently in case the guests sends data to
|
2559 |
* the virtual device linked to our pty. */
|
2560 |
qemu_mod_timer(s->timer, qemu_get_clock(rt_clock) + 1000);
|
2561 |
} else {
|
2562 |
if (!s->connected)
|
2563 |
qemu_chr_reset(chr); |
2564 |
s->connected = 1;
|
2565 |
} |
2566 |
} |
2567 |
|
2568 |
static void pty_chr_timer(void *opaque) |
2569 |
{ |
2570 |
struct CharDriverState *chr = opaque;
|
2571 |
PtyCharDriver *s = chr->opaque; |
2572 |
|
2573 |
if (s->connected)
|
2574 |
return;
|
2575 |
if (s->polling) {
|
2576 |
/* If we arrive here without polling being cleared due
|
2577 |
* read returning -EIO, then we are (re-)connected */
|
2578 |
pty_chr_state(chr, 1);
|
2579 |
return;
|
2580 |
} |
2581 |
|
2582 |
/* Next poll ... */
|
2583 |
pty_chr_update_read_handler(chr); |
2584 |
} |
2585 |
|
2586 |
static void pty_chr_close(struct CharDriverState *chr) |
2587 |
{ |
2588 |
PtyCharDriver *s = chr->opaque; |
2589 |
|
2590 |
qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL); |
2591 |
close(s->fd); |
2592 |
qemu_free(s); |
2593 |
} |
2594 |
|
2595 |
static CharDriverState *qemu_chr_open_pty(void) |
2596 |
{ |
2597 |
CharDriverState *chr; |
2598 |
PtyCharDriver *s; |
2599 |
struct termios tty;
|
2600 |
int slave_fd;
|
2601 |
#if defined(__OpenBSD__)
|
2602 |
char pty_name[PATH_MAX];
|
2603 |
#define q_ptsname(x) pty_name
|
2604 |
#else
|
2605 |
char *pty_name = NULL; |
2606 |
#define q_ptsname(x) ptsname(x)
|
2607 |
#endif
|
2608 |
|
2609 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2610 |
if (!chr)
|
2611 |
return NULL; |
2612 |
s = qemu_mallocz(sizeof(PtyCharDriver));
|
2613 |
if (!s) {
|
2614 |
qemu_free(chr); |
2615 |
return NULL; |
2616 |
} |
2617 |
|
2618 |
if (openpty(&s->fd, &slave_fd, pty_name, NULL, NULL) < 0) { |
2619 |
return NULL; |
2620 |
} |
2621 |
|
2622 |
/* Set raw attributes on the pty. */
|
2623 |
cfmakeraw(&tty); |
2624 |
tcsetattr(slave_fd, TCSAFLUSH, &tty); |
2625 |
close(slave_fd); |
2626 |
|
2627 |
fprintf(stderr, "char device redirected to %s\n", q_ptsname(s->fd));
|
2628 |
|
2629 |
chr->opaque = s; |
2630 |
chr->chr_write = pty_chr_write; |
2631 |
chr->chr_update_read_handler = pty_chr_update_read_handler; |
2632 |
chr->chr_close = pty_chr_close; |
2633 |
|
2634 |
s->timer = qemu_new_timer(rt_clock, pty_chr_timer, chr); |
2635 |
|
2636 |
return chr;
|
2637 |
} |
2638 |
|
2639 |
static void tty_serial_init(int fd, int speed, |
2640 |
int parity, int data_bits, int stop_bits) |
2641 |
{ |
2642 |
struct termios tty;
|
2643 |
speed_t spd; |
2644 |
|
2645 |
#if 0
|
2646 |
printf("tty_serial_init: speed=%d parity=%c data=%d stop=%d\n",
|
2647 |
speed, parity, data_bits, stop_bits);
|
2648 |
#endif
|
2649 |
tcgetattr (fd, &tty); |
2650 |
|
2651 |
#define MARGIN 1.1 |
2652 |
if (speed <= 50 * MARGIN) |
2653 |
spd = B50; |
2654 |
else if (speed <= 75 * MARGIN) |
2655 |
spd = B75; |
2656 |
else if (speed <= 300 * MARGIN) |
2657 |
spd = B300; |
2658 |
else if (speed <= 600 * MARGIN) |
2659 |
spd = B600; |
2660 |
else if (speed <= 1200 * MARGIN) |
2661 |
spd = B1200; |
2662 |
else if (speed <= 2400 * MARGIN) |
2663 |
spd = B2400; |
2664 |
else if (speed <= 4800 * MARGIN) |
2665 |
spd = B4800; |
2666 |
else if (speed <= 9600 * MARGIN) |
2667 |
spd = B9600; |
2668 |
else if (speed <= 19200 * MARGIN) |
2669 |
spd = B19200; |
2670 |
else if (speed <= 38400 * MARGIN) |
2671 |
spd = B38400; |
2672 |
else if (speed <= 57600 * MARGIN) |
2673 |
spd = B57600; |
2674 |
else if (speed <= 115200 * MARGIN) |
2675 |
spd = B115200; |
2676 |
else
|
2677 |
spd = B115200; |
2678 |
|
2679 |
cfsetispeed(&tty, spd); |
2680 |
cfsetospeed(&tty, spd); |
2681 |
|
2682 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
2683 |
|INLCR|IGNCR|ICRNL|IXON); |
2684 |
tty.c_oflag |= OPOST; |
2685 |
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN|ISIG); |
2686 |
tty.c_cflag &= ~(CSIZE|PARENB|PARODD|CRTSCTS|CSTOPB); |
2687 |
switch(data_bits) {
|
2688 |
default:
|
2689 |
case 8: |
2690 |
tty.c_cflag |= CS8; |
2691 |
break;
|
2692 |
case 7: |
2693 |
tty.c_cflag |= CS7; |
2694 |
break;
|
2695 |
case 6: |
2696 |
tty.c_cflag |= CS6; |
2697 |
break;
|
2698 |
case 5: |
2699 |
tty.c_cflag |= CS5; |
2700 |
break;
|
2701 |
} |
2702 |
switch(parity) {
|
2703 |
default:
|
2704 |
case 'N': |
2705 |
break;
|
2706 |
case 'E': |
2707 |
tty.c_cflag |= PARENB; |
2708 |
break;
|
2709 |
case 'O': |
2710 |
tty.c_cflag |= PARENB | PARODD; |
2711 |
break;
|
2712 |
} |
2713 |
if (stop_bits == 2) |
2714 |
tty.c_cflag |= CSTOPB; |
2715 |
|
2716 |
tcsetattr (fd, TCSANOW, &tty); |
2717 |
} |
2718 |
|
2719 |
static int tty_serial_ioctl(CharDriverState *chr, int cmd, void *arg) |
2720 |
{ |
2721 |
FDCharDriver *s = chr->opaque; |
2722 |
|
2723 |
switch(cmd) {
|
2724 |
case CHR_IOCTL_SERIAL_SET_PARAMS:
|
2725 |
{ |
2726 |
QEMUSerialSetParams *ssp = arg; |
2727 |
tty_serial_init(s->fd_in, ssp->speed, ssp->parity, |
2728 |
ssp->data_bits, ssp->stop_bits); |
2729 |
} |
2730 |
break;
|
2731 |
case CHR_IOCTL_SERIAL_SET_BREAK:
|
2732 |
{ |
2733 |
int enable = *(int *)arg; |
2734 |
if (enable)
|
2735 |
tcsendbreak(s->fd_in, 1);
|
2736 |
} |
2737 |
break;
|
2738 |
case CHR_IOCTL_SERIAL_GET_TIOCM:
|
2739 |
{ |
2740 |
int sarg = 0; |
2741 |
int *targ = (int *)arg; |
2742 |
ioctl(s->fd_in, TIOCMGET, &sarg); |
2743 |
*targ = 0;
|
2744 |
if (sarg | TIOCM_CTS)
|
2745 |
*targ |= CHR_TIOCM_CTS; |
2746 |
if (sarg | TIOCM_CAR)
|
2747 |
*targ |= CHR_TIOCM_CAR; |
2748 |
if (sarg | TIOCM_DSR)
|
2749 |
*targ |= CHR_TIOCM_DSR; |
2750 |
if (sarg | TIOCM_RI)
|
2751 |
*targ |= CHR_TIOCM_RI; |
2752 |
if (sarg | TIOCM_DTR)
|
2753 |
*targ |= CHR_TIOCM_DTR; |
2754 |
if (sarg | TIOCM_RTS)
|
2755 |
*targ |= CHR_TIOCM_RTS; |
2756 |
} |
2757 |
break;
|
2758 |
case CHR_IOCTL_SERIAL_SET_TIOCM:
|
2759 |
{ |
2760 |
int sarg = *(int *)arg; |
2761 |
int targ = 0; |
2762 |
if (sarg | CHR_TIOCM_DTR)
|
2763 |
targ |= TIOCM_DTR; |
2764 |
if (sarg | CHR_TIOCM_RTS)
|
2765 |
targ |= TIOCM_RTS; |
2766 |
ioctl(s->fd_in, TIOCMSET, &targ); |
2767 |
} |
2768 |
break;
|
2769 |
default:
|
2770 |
return -ENOTSUP;
|
2771 |
} |
2772 |
return 0; |
2773 |
} |
2774 |
|
2775 |
static CharDriverState *qemu_chr_open_tty(const char *filename) |
2776 |
{ |
2777 |
CharDriverState *chr; |
2778 |
int fd;
|
2779 |
|
2780 |
TFR(fd = open(filename, O_RDWR | O_NONBLOCK)); |
2781 |
tty_serial_init(fd, 115200, 'N', 8, 1); |
2782 |
chr = qemu_chr_open_fd(fd, fd); |
2783 |
if (!chr) {
|
2784 |
close(fd); |
2785 |
return NULL; |
2786 |
} |
2787 |
chr->chr_ioctl = tty_serial_ioctl; |
2788 |
qemu_chr_reset(chr); |
2789 |
return chr;
|
2790 |
} |
2791 |
#else /* ! __linux__ && ! __sun__ */ |
2792 |
static CharDriverState *qemu_chr_open_pty(void) |
2793 |
{ |
2794 |
return NULL; |
2795 |
} |
2796 |
#endif /* __linux__ || __sun__ */ |
2797 |
|
2798 |
#if defined(__linux__)
|
2799 |
typedef struct { |
2800 |
int fd;
|
2801 |
int mode;
|
2802 |
} ParallelCharDriver; |
2803 |
|
2804 |
static int pp_hw_mode(ParallelCharDriver *s, uint16_t mode) |
2805 |
{ |
2806 |
if (s->mode != mode) {
|
2807 |
int m = mode;
|
2808 |
if (ioctl(s->fd, PPSETMODE, &m) < 0) |
2809 |
return 0; |
2810 |
s->mode = mode; |
2811 |
} |
2812 |
return 1; |
2813 |
} |
2814 |
|
2815 |
static int pp_ioctl(CharDriverState *chr, int cmd, void *arg) |
2816 |
{ |
2817 |
ParallelCharDriver *drv = chr->opaque; |
2818 |
int fd = drv->fd;
|
2819 |
uint8_t b; |
2820 |
|
2821 |
switch(cmd) {
|
2822 |
case CHR_IOCTL_PP_READ_DATA:
|
2823 |
if (ioctl(fd, PPRDATA, &b) < 0) |
2824 |
return -ENOTSUP;
|
2825 |
*(uint8_t *)arg = b; |
2826 |
break;
|
2827 |
case CHR_IOCTL_PP_WRITE_DATA:
|
2828 |
b = *(uint8_t *)arg; |
2829 |
if (ioctl(fd, PPWDATA, &b) < 0) |
2830 |
return -ENOTSUP;
|
2831 |
break;
|
2832 |
case CHR_IOCTL_PP_READ_CONTROL:
|
2833 |
if (ioctl(fd, PPRCONTROL, &b) < 0) |
2834 |
return -ENOTSUP;
|
2835 |
/* Linux gives only the lowest bits, and no way to know data
|
2836 |
direction! For better compatibility set the fixed upper
|
2837 |
bits. */
|
2838 |
*(uint8_t *)arg = b | 0xc0;
|
2839 |
break;
|
2840 |
case CHR_IOCTL_PP_WRITE_CONTROL:
|
2841 |
b = *(uint8_t *)arg; |
2842 |
if (ioctl(fd, PPWCONTROL, &b) < 0) |
2843 |
return -ENOTSUP;
|
2844 |
break;
|
2845 |
case CHR_IOCTL_PP_READ_STATUS:
|
2846 |
if (ioctl(fd, PPRSTATUS, &b) < 0) |
2847 |
return -ENOTSUP;
|
2848 |
*(uint8_t *)arg = b; |
2849 |
break;
|
2850 |
case CHR_IOCTL_PP_DATA_DIR:
|
2851 |
if (ioctl(fd, PPDATADIR, (int *)arg) < 0) |
2852 |
return -ENOTSUP;
|
2853 |
break;
|
2854 |
case CHR_IOCTL_PP_EPP_READ_ADDR:
|
2855 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2856 |
struct ParallelIOArg *parg = arg;
|
2857 |
int n = read(fd, parg->buffer, parg->count);
|
2858 |
if (n != parg->count) {
|
2859 |
return -EIO;
|
2860 |
} |
2861 |
} |
2862 |
break;
|
2863 |
case CHR_IOCTL_PP_EPP_READ:
|
2864 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2865 |
struct ParallelIOArg *parg = arg;
|
2866 |
int n = read(fd, parg->buffer, parg->count);
|
2867 |
if (n != parg->count) {
|
2868 |
return -EIO;
|
2869 |
} |
2870 |
} |
2871 |
break;
|
2872 |
case CHR_IOCTL_PP_EPP_WRITE_ADDR:
|
2873 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP|IEEE1284_ADDR)) {
|
2874 |
struct ParallelIOArg *parg = arg;
|
2875 |
int n = write(fd, parg->buffer, parg->count);
|
2876 |
if (n != parg->count) {
|
2877 |
return -EIO;
|
2878 |
} |
2879 |
} |
2880 |
break;
|
2881 |
case CHR_IOCTL_PP_EPP_WRITE:
|
2882 |
if (pp_hw_mode(drv, IEEE1284_MODE_EPP)) {
|
2883 |
struct ParallelIOArg *parg = arg;
|
2884 |
int n = write(fd, parg->buffer, parg->count);
|
2885 |
if (n != parg->count) {
|
2886 |
return -EIO;
|
2887 |
} |
2888 |
} |
2889 |
break;
|
2890 |
default:
|
2891 |
return -ENOTSUP;
|
2892 |
} |
2893 |
return 0; |
2894 |
} |
2895 |
|
2896 |
static void pp_close(CharDriverState *chr) |
2897 |
{ |
2898 |
ParallelCharDriver *drv = chr->opaque; |
2899 |
int fd = drv->fd;
|
2900 |
|
2901 |
pp_hw_mode(drv, IEEE1284_MODE_COMPAT); |
2902 |
ioctl(fd, PPRELEASE); |
2903 |
close(fd); |
2904 |
qemu_free(drv); |
2905 |
} |
2906 |
|
2907 |
static CharDriverState *qemu_chr_open_pp(const char *filename) |
2908 |
{ |
2909 |
CharDriverState *chr; |
2910 |
ParallelCharDriver *drv; |
2911 |
int fd;
|
2912 |
|
2913 |
TFR(fd = open(filename, O_RDWR)); |
2914 |
if (fd < 0) |
2915 |
return NULL; |
2916 |
|
2917 |
if (ioctl(fd, PPCLAIM) < 0) { |
2918 |
close(fd); |
2919 |
return NULL; |
2920 |
} |
2921 |
|
2922 |
drv = qemu_mallocz(sizeof(ParallelCharDriver));
|
2923 |
if (!drv) {
|
2924 |
close(fd); |
2925 |
return NULL; |
2926 |
} |
2927 |
drv->fd = fd; |
2928 |
drv->mode = IEEE1284_MODE_COMPAT; |
2929 |
|
2930 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
2931 |
if (!chr) {
|
2932 |
qemu_free(drv); |
2933 |
close(fd); |
2934 |
return NULL; |
2935 |
} |
2936 |
chr->chr_write = null_chr_write; |
2937 |
chr->chr_ioctl = pp_ioctl; |
2938 |
chr->chr_close = pp_close; |
2939 |
chr->opaque = drv; |
2940 |
|
2941 |
qemu_chr_reset(chr); |
2942 |
|
2943 |
return chr;
|
2944 |
} |
2945 |
#endif /* __linux__ */ |
2946 |
|
2947 |
#else /* _WIN32 */ |
2948 |
|
2949 |
typedef struct { |
2950 |
int max_size;
|
2951 |
HANDLE hcom, hrecv, hsend; |
2952 |
OVERLAPPED orecv, osend; |
2953 |
BOOL fpipe; |
2954 |
DWORD len; |
2955 |
} WinCharState; |
2956 |
|
2957 |
#define NSENDBUF 2048 |
2958 |
#define NRECVBUF 2048 |
2959 |
#define MAXCONNECT 1 |
2960 |
#define NTIMEOUT 5000 |
2961 |
|
2962 |
static int win_chr_poll(void *opaque); |
2963 |
static int win_chr_pipe_poll(void *opaque); |
2964 |
|
2965 |
static void win_chr_close(CharDriverState *chr) |
2966 |
{ |
2967 |
WinCharState *s = chr->opaque; |
2968 |
|
2969 |
if (s->hsend) {
|
2970 |
CloseHandle(s->hsend); |
2971 |
s->hsend = NULL;
|
2972 |
} |
2973 |
if (s->hrecv) {
|
2974 |
CloseHandle(s->hrecv); |
2975 |
s->hrecv = NULL;
|
2976 |
} |
2977 |
if (s->hcom) {
|
2978 |
CloseHandle(s->hcom); |
2979 |
s->hcom = NULL;
|
2980 |
} |
2981 |
if (s->fpipe)
|
2982 |
qemu_del_polling_cb(win_chr_pipe_poll, chr); |
2983 |
else
|
2984 |
qemu_del_polling_cb(win_chr_poll, chr); |
2985 |
} |
2986 |
|
2987 |
static int win_chr_init(CharDriverState *chr, const char *filename) |
2988 |
{ |
2989 |
WinCharState *s = chr->opaque; |
2990 |
COMMCONFIG comcfg; |
2991 |
COMMTIMEOUTS cto = { 0, 0, 0, 0, 0}; |
2992 |
COMSTAT comstat; |
2993 |
DWORD size; |
2994 |
DWORD err; |
2995 |
|
2996 |
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
2997 |
if (!s->hsend) {
|
2998 |
fprintf(stderr, "Failed CreateEvent\n");
|
2999 |
goto fail;
|
3000 |
} |
3001 |
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
3002 |
if (!s->hrecv) {
|
3003 |
fprintf(stderr, "Failed CreateEvent\n");
|
3004 |
goto fail;
|
3005 |
} |
3006 |
|
3007 |
s->hcom = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, |
3008 |
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
|
3009 |
if (s->hcom == INVALID_HANDLE_VALUE) {
|
3010 |
fprintf(stderr, "Failed CreateFile (%lu)\n", GetLastError());
|
3011 |
s->hcom = NULL;
|
3012 |
goto fail;
|
3013 |
} |
3014 |
|
3015 |
if (!SetupComm(s->hcom, NRECVBUF, NSENDBUF)) {
|
3016 |
fprintf(stderr, "Failed SetupComm\n");
|
3017 |
goto fail;
|
3018 |
} |
3019 |
|
3020 |
ZeroMemory(&comcfg, sizeof(COMMCONFIG));
|
3021 |
size = sizeof(COMMCONFIG);
|
3022 |
GetDefaultCommConfig(filename, &comcfg, &size); |
3023 |
comcfg.dcb.DCBlength = sizeof(DCB);
|
3024 |
CommConfigDialog(filename, NULL, &comcfg);
|
3025 |
|
3026 |
if (!SetCommState(s->hcom, &comcfg.dcb)) {
|
3027 |
fprintf(stderr, "Failed SetCommState\n");
|
3028 |
goto fail;
|
3029 |
} |
3030 |
|
3031 |
if (!SetCommMask(s->hcom, EV_ERR)) {
|
3032 |
fprintf(stderr, "Failed SetCommMask\n");
|
3033 |
goto fail;
|
3034 |
} |
3035 |
|
3036 |
cto.ReadIntervalTimeout = MAXDWORD; |
3037 |
if (!SetCommTimeouts(s->hcom, &cto)) {
|
3038 |
fprintf(stderr, "Failed SetCommTimeouts\n");
|
3039 |
goto fail;
|
3040 |
} |
3041 |
|
3042 |
if (!ClearCommError(s->hcom, &err, &comstat)) {
|
3043 |
fprintf(stderr, "Failed ClearCommError\n");
|
3044 |
goto fail;
|
3045 |
} |
3046 |
qemu_add_polling_cb(win_chr_poll, chr); |
3047 |
return 0; |
3048 |
|
3049 |
fail:
|
3050 |
win_chr_close(chr); |
3051 |
return -1; |
3052 |
} |
3053 |
|
3054 |
static int win_chr_write(CharDriverState *chr, const uint8_t *buf, int len1) |
3055 |
{ |
3056 |
WinCharState *s = chr->opaque; |
3057 |
DWORD len, ret, size, err; |
3058 |
|
3059 |
len = len1; |
3060 |
ZeroMemory(&s->osend, sizeof(s->osend));
|
3061 |
s->osend.hEvent = s->hsend; |
3062 |
while (len > 0) { |
3063 |
if (s->hsend)
|
3064 |
ret = WriteFile(s->hcom, buf, len, &size, &s->osend); |
3065 |
else
|
3066 |
ret = WriteFile(s->hcom, buf, len, &size, NULL);
|
3067 |
if (!ret) {
|
3068 |
err = GetLastError(); |
3069 |
if (err == ERROR_IO_PENDING) {
|
3070 |
ret = GetOverlappedResult(s->hcom, &s->osend, &size, TRUE); |
3071 |
if (ret) {
|
3072 |
buf += size; |
3073 |
len -= size; |
3074 |
} else {
|
3075 |
break;
|
3076 |
} |
3077 |
} else {
|
3078 |
break;
|
3079 |
} |
3080 |
} else {
|
3081 |
buf += size; |
3082 |
len -= size; |
3083 |
} |
3084 |
} |
3085 |
return len1 - len;
|
3086 |
} |
3087 |
|
3088 |
static int win_chr_read_poll(CharDriverState *chr) |
3089 |
{ |
3090 |
WinCharState *s = chr->opaque; |
3091 |
|
3092 |
s->max_size = qemu_chr_can_read(chr); |
3093 |
return s->max_size;
|
3094 |
} |
3095 |
|
3096 |
static void win_chr_readfile(CharDriverState *chr) |
3097 |
{ |
3098 |
WinCharState *s = chr->opaque; |
3099 |
int ret, err;
|
3100 |
uint8_t buf[1024];
|
3101 |
DWORD size; |
3102 |
|
3103 |
ZeroMemory(&s->orecv, sizeof(s->orecv));
|
3104 |
s->orecv.hEvent = s->hrecv; |
3105 |
ret = ReadFile(s->hcom, buf, s->len, &size, &s->orecv); |
3106 |
if (!ret) {
|
3107 |
err = GetLastError(); |
3108 |
if (err == ERROR_IO_PENDING) {
|
3109 |
ret = GetOverlappedResult(s->hcom, &s->orecv, &size, TRUE); |
3110 |
} |
3111 |
} |
3112 |
|
3113 |
if (size > 0) { |
3114 |
qemu_chr_read(chr, buf, size); |
3115 |
} |
3116 |
} |
3117 |
|
3118 |
static void win_chr_read(CharDriverState *chr) |
3119 |
{ |
3120 |
WinCharState *s = chr->opaque; |
3121 |
|
3122 |
if (s->len > s->max_size)
|
3123 |
s->len = s->max_size; |
3124 |
if (s->len == 0) |
3125 |
return;
|
3126 |
|
3127 |
win_chr_readfile(chr); |
3128 |
} |
3129 |
|
3130 |
static int win_chr_poll(void *opaque) |
3131 |
{ |
3132 |
CharDriverState *chr = opaque; |
3133 |
WinCharState *s = chr->opaque; |
3134 |
COMSTAT status; |
3135 |
DWORD comerr; |
3136 |
|
3137 |
ClearCommError(s->hcom, &comerr, &status); |
3138 |
if (status.cbInQue > 0) { |
3139 |
s->len = status.cbInQue; |
3140 |
win_chr_read_poll(chr); |
3141 |
win_chr_read(chr); |
3142 |
return 1; |
3143 |
} |
3144 |
return 0; |
3145 |
} |
3146 |
|
3147 |
static CharDriverState *qemu_chr_open_win(const char *filename) |
3148 |
{ |
3149 |
CharDriverState *chr; |
3150 |
WinCharState *s; |
3151 |
|
3152 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3153 |
if (!chr)
|
3154 |
return NULL; |
3155 |
s = qemu_mallocz(sizeof(WinCharState));
|
3156 |
if (!s) {
|
3157 |
free(chr); |
3158 |
return NULL; |
3159 |
} |
3160 |
chr->opaque = s; |
3161 |
chr->chr_write = win_chr_write; |
3162 |
chr->chr_close = win_chr_close; |
3163 |
|
3164 |
if (win_chr_init(chr, filename) < 0) { |
3165 |
free(s); |
3166 |
free(chr); |
3167 |
return NULL; |
3168 |
} |
3169 |
qemu_chr_reset(chr); |
3170 |
return chr;
|
3171 |
} |
3172 |
|
3173 |
static int win_chr_pipe_poll(void *opaque) |
3174 |
{ |
3175 |
CharDriverState *chr = opaque; |
3176 |
WinCharState *s = chr->opaque; |
3177 |
DWORD size; |
3178 |
|
3179 |
PeekNamedPipe(s->hcom, NULL, 0, NULL, &size, NULL); |
3180 |
if (size > 0) { |
3181 |
s->len = size; |
3182 |
win_chr_read_poll(chr); |
3183 |
win_chr_read(chr); |
3184 |
return 1; |
3185 |
} |
3186 |
return 0; |
3187 |
} |
3188 |
|
3189 |
static int win_chr_pipe_init(CharDriverState *chr, const char *filename) |
3190 |
{ |
3191 |
WinCharState *s = chr->opaque; |
3192 |
OVERLAPPED ov; |
3193 |
int ret;
|
3194 |
DWORD size; |
3195 |
char openname[256]; |
3196 |
|
3197 |
s->fpipe = TRUE; |
3198 |
|
3199 |
s->hsend = CreateEvent(NULL, TRUE, FALSE, NULL); |
3200 |
if (!s->hsend) {
|
3201 |
fprintf(stderr, "Failed CreateEvent\n");
|
3202 |
goto fail;
|
3203 |
} |
3204 |
s->hrecv = CreateEvent(NULL, TRUE, FALSE, NULL); |
3205 |
if (!s->hrecv) {
|
3206 |
fprintf(stderr, "Failed CreateEvent\n");
|
3207 |
goto fail;
|
3208 |
} |
3209 |
|
3210 |
snprintf(openname, sizeof(openname), "\\\\.\\pipe\\%s", filename); |
3211 |
s->hcom = CreateNamedPipe(openname, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, |
3212 |
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | |
3213 |
PIPE_WAIT, |
3214 |
MAXCONNECT, NSENDBUF, NRECVBUF, NTIMEOUT, NULL);
|
3215 |
if (s->hcom == INVALID_HANDLE_VALUE) {
|
3216 |
fprintf(stderr, "Failed CreateNamedPipe (%lu)\n", GetLastError());
|
3217 |
s->hcom = NULL;
|
3218 |
goto fail;
|
3219 |
} |
3220 |
|
3221 |
ZeroMemory(&ov, sizeof(ov));
|
3222 |
ov.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
3223 |
ret = ConnectNamedPipe(s->hcom, &ov); |
3224 |
if (ret) {
|
3225 |
fprintf(stderr, "Failed ConnectNamedPipe\n");
|
3226 |
goto fail;
|
3227 |
} |
3228 |
|
3229 |
ret = GetOverlappedResult(s->hcom, &ov, &size, TRUE); |
3230 |
if (!ret) {
|
3231 |
fprintf(stderr, "Failed GetOverlappedResult\n");
|
3232 |
if (ov.hEvent) {
|
3233 |
CloseHandle(ov.hEvent); |
3234 |
ov.hEvent = NULL;
|
3235 |
} |
3236 |
goto fail;
|
3237 |
} |
3238 |
|
3239 |
if (ov.hEvent) {
|
3240 |
CloseHandle(ov.hEvent); |
3241 |
ov.hEvent = NULL;
|
3242 |
} |
3243 |
qemu_add_polling_cb(win_chr_pipe_poll, chr); |
3244 |
return 0; |
3245 |
|
3246 |
fail:
|
3247 |
win_chr_close(chr); |
3248 |
return -1; |
3249 |
} |
3250 |
|
3251 |
|
3252 |
static CharDriverState *qemu_chr_open_win_pipe(const char *filename) |
3253 |
{ |
3254 |
CharDriverState *chr; |
3255 |
WinCharState *s; |
3256 |
|
3257 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3258 |
if (!chr)
|
3259 |
return NULL; |
3260 |
s = qemu_mallocz(sizeof(WinCharState));
|
3261 |
if (!s) {
|
3262 |
free(chr); |
3263 |
return NULL; |
3264 |
} |
3265 |
chr->opaque = s; |
3266 |
chr->chr_write = win_chr_write; |
3267 |
chr->chr_close = win_chr_close; |
3268 |
|
3269 |
if (win_chr_pipe_init(chr, filename) < 0) { |
3270 |
free(s); |
3271 |
free(chr); |
3272 |
return NULL; |
3273 |
} |
3274 |
qemu_chr_reset(chr); |
3275 |
return chr;
|
3276 |
} |
3277 |
|
3278 |
static CharDriverState *qemu_chr_open_win_file(HANDLE fd_out)
|
3279 |
{ |
3280 |
CharDriverState *chr; |
3281 |
WinCharState *s; |
3282 |
|
3283 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3284 |
if (!chr)
|
3285 |
return NULL; |
3286 |
s = qemu_mallocz(sizeof(WinCharState));
|
3287 |
if (!s) {
|
3288 |
free(chr); |
3289 |
return NULL; |
3290 |
} |
3291 |
s->hcom = fd_out; |
3292 |
chr->opaque = s; |
3293 |
chr->chr_write = win_chr_write; |
3294 |
qemu_chr_reset(chr); |
3295 |
return chr;
|
3296 |
} |
3297 |
|
3298 |
static CharDriverState *qemu_chr_open_win_con(const char *filename) |
3299 |
{ |
3300 |
return qemu_chr_open_win_file(GetStdHandle(STD_OUTPUT_HANDLE));
|
3301 |
} |
3302 |
|
3303 |
static CharDriverState *qemu_chr_open_win_file_out(const char *file_out) |
3304 |
{ |
3305 |
HANDLE fd_out; |
3306 |
|
3307 |
fd_out = CreateFile(file_out, GENERIC_WRITE, FILE_SHARE_READ, NULL,
|
3308 |
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
|
3309 |
if (fd_out == INVALID_HANDLE_VALUE)
|
3310 |
return NULL; |
3311 |
|
3312 |
return qemu_chr_open_win_file(fd_out);
|
3313 |
} |
3314 |
#endif /* !_WIN32 */ |
3315 |
|
3316 |
/***********************************************************/
|
3317 |
/* UDP Net console */
|
3318 |
|
3319 |
typedef struct { |
3320 |
int fd;
|
3321 |
struct sockaddr_in daddr;
|
3322 |
uint8_t buf[1024];
|
3323 |
int bufcnt;
|
3324 |
int bufptr;
|
3325 |
int max_size;
|
3326 |
} NetCharDriver; |
3327 |
|
3328 |
static int udp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3329 |
{ |
3330 |
NetCharDriver *s = chr->opaque; |
3331 |
|
3332 |
return sendto(s->fd, buf, len, 0, |
3333 |
(struct sockaddr *)&s->daddr, sizeof(struct sockaddr_in)); |
3334 |
} |
3335 |
|
3336 |
static int udp_chr_read_poll(void *opaque) |
3337 |
{ |
3338 |
CharDriverState *chr = opaque; |
3339 |
NetCharDriver *s = chr->opaque; |
3340 |
|
3341 |
s->max_size = qemu_chr_can_read(chr); |
3342 |
|
3343 |
/* If there were any stray characters in the queue process them
|
3344 |
* first
|
3345 |
*/
|
3346 |
while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3347 |
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3348 |
s->bufptr++; |
3349 |
s->max_size = qemu_chr_can_read(chr); |
3350 |
} |
3351 |
return s->max_size;
|
3352 |
} |
3353 |
|
3354 |
static void udp_chr_read(void *opaque) |
3355 |
{ |
3356 |
CharDriverState *chr = opaque; |
3357 |
NetCharDriver *s = chr->opaque; |
3358 |
|
3359 |
if (s->max_size == 0) |
3360 |
return;
|
3361 |
s->bufcnt = recv(s->fd, s->buf, sizeof(s->buf), 0); |
3362 |
s->bufptr = s->bufcnt; |
3363 |
if (s->bufcnt <= 0) |
3364 |
return;
|
3365 |
|
3366 |
s->bufptr = 0;
|
3367 |
while (s->max_size > 0 && s->bufptr < s->bufcnt) { |
3368 |
qemu_chr_read(chr, &s->buf[s->bufptr], 1);
|
3369 |
s->bufptr++; |
3370 |
s->max_size = qemu_chr_can_read(chr); |
3371 |
} |
3372 |
} |
3373 |
|
3374 |
static void udp_chr_update_read_handler(CharDriverState *chr) |
3375 |
{ |
3376 |
NetCharDriver *s = chr->opaque; |
3377 |
|
3378 |
if (s->fd >= 0) { |
3379 |
qemu_set_fd_handler2(s->fd, udp_chr_read_poll, |
3380 |
udp_chr_read, NULL, chr);
|
3381 |
} |
3382 |
} |
3383 |
|
3384 |
int parse_host_port(struct sockaddr_in *saddr, const char *str); |
3385 |
#ifndef _WIN32
|
3386 |
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str); |
3387 |
#endif
|
3388 |
int parse_host_src_port(struct sockaddr_in *haddr, |
3389 |
struct sockaddr_in *saddr,
|
3390 |
const char *str); |
3391 |
|
3392 |
static CharDriverState *qemu_chr_open_udp(const char *def) |
3393 |
{ |
3394 |
CharDriverState *chr = NULL;
|
3395 |
NetCharDriver *s = NULL;
|
3396 |
int fd = -1; |
3397 |
struct sockaddr_in saddr;
|
3398 |
|
3399 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3400 |
if (!chr)
|
3401 |
goto return_err;
|
3402 |
s = qemu_mallocz(sizeof(NetCharDriver));
|
3403 |
if (!s)
|
3404 |
goto return_err;
|
3405 |
|
3406 |
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
3407 |
if (fd < 0) { |
3408 |
perror("socket(PF_INET, SOCK_DGRAM)");
|
3409 |
goto return_err;
|
3410 |
} |
3411 |
|
3412 |
if (parse_host_src_port(&s->daddr, &saddr, def) < 0) { |
3413 |
printf("Could not parse: %s\n", def);
|
3414 |
goto return_err;
|
3415 |
} |
3416 |
|
3417 |
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) |
3418 |
{ |
3419 |
perror("bind");
|
3420 |
goto return_err;
|
3421 |
} |
3422 |
|
3423 |
s->fd = fd; |
3424 |
s->bufcnt = 0;
|
3425 |
s->bufptr = 0;
|
3426 |
chr->opaque = s; |
3427 |
chr->chr_write = udp_chr_write; |
3428 |
chr->chr_update_read_handler = udp_chr_update_read_handler; |
3429 |
return chr;
|
3430 |
|
3431 |
return_err:
|
3432 |
if (chr)
|
3433 |
free(chr); |
3434 |
if (s)
|
3435 |
free(s); |
3436 |
if (fd >= 0) |
3437 |
closesocket(fd); |
3438 |
return NULL; |
3439 |
} |
3440 |
|
3441 |
/***********************************************************/
|
3442 |
/* TCP Net console */
|
3443 |
|
3444 |
typedef struct { |
3445 |
int fd, listen_fd;
|
3446 |
int connected;
|
3447 |
int max_size;
|
3448 |
int do_telnetopt;
|
3449 |
int do_nodelay;
|
3450 |
int is_unix;
|
3451 |
} TCPCharDriver; |
3452 |
|
3453 |
static void tcp_chr_accept(void *opaque); |
3454 |
|
3455 |
static int tcp_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
3456 |
{ |
3457 |
TCPCharDriver *s = chr->opaque; |
3458 |
if (s->connected) {
|
3459 |
return send_all(s->fd, buf, len);
|
3460 |
} else {
|
3461 |
/* XXX: indicate an error ? */
|
3462 |
return len;
|
3463 |
} |
3464 |
} |
3465 |
|
3466 |
static int tcp_chr_read_poll(void *opaque) |
3467 |
{ |
3468 |
CharDriverState *chr = opaque; |
3469 |
TCPCharDriver *s = chr->opaque; |
3470 |
if (!s->connected)
|
3471 |
return 0; |
3472 |
s->max_size = qemu_chr_can_read(chr); |
3473 |
return s->max_size;
|
3474 |
} |
3475 |
|
3476 |
#define IAC 255 |
3477 |
#define IAC_BREAK 243 |
3478 |
static void tcp_chr_process_IAC_bytes(CharDriverState *chr, |
3479 |
TCPCharDriver *s, |
3480 |
uint8_t *buf, int *size)
|
3481 |
{ |
3482 |
/* Handle any telnet client's basic IAC options to satisfy char by
|
3483 |
* char mode with no echo. All IAC options will be removed from
|
3484 |
* the buf and the do_telnetopt variable will be used to track the
|
3485 |
* state of the width of the IAC information.
|
3486 |
*
|
3487 |
* IAC commands come in sets of 3 bytes with the exception of the
|
3488 |
* "IAC BREAK" command and the double IAC.
|
3489 |
*/
|
3490 |
|
3491 |
int i;
|
3492 |
int j = 0; |
3493 |
|
3494 |
for (i = 0; i < *size; i++) { |
3495 |
if (s->do_telnetopt > 1) { |
3496 |
if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) { |
3497 |
/* Double IAC means send an IAC */
|
3498 |
if (j != i)
|
3499 |
buf[j] = buf[i]; |
3500 |
j++; |
3501 |
s->do_telnetopt = 1;
|
3502 |
} else {
|
3503 |
if ((unsigned char)buf[i] == IAC_BREAK && s->do_telnetopt == 2) { |
3504 |
/* Handle IAC break commands by sending a serial break */
|
3505 |
qemu_chr_event(chr, CHR_EVENT_BREAK); |
3506 |
s->do_telnetopt++; |
3507 |
} |
3508 |
s->do_telnetopt++; |
3509 |
} |
3510 |
if (s->do_telnetopt >= 4) { |
3511 |
s->do_telnetopt = 1;
|
3512 |
} |
3513 |
} else {
|
3514 |
if ((unsigned char)buf[i] == IAC) { |
3515 |
s->do_telnetopt = 2;
|
3516 |
} else {
|
3517 |
if (j != i)
|
3518 |
buf[j] = buf[i]; |
3519 |
j++; |
3520 |
} |
3521 |
} |
3522 |
} |
3523 |
*size = j; |
3524 |
} |
3525 |
|
3526 |
static void tcp_chr_read(void *opaque) |
3527 |
{ |
3528 |
CharDriverState *chr = opaque; |
3529 |
TCPCharDriver *s = chr->opaque; |
3530 |
uint8_t buf[1024];
|
3531 |
int len, size;
|
3532 |
|
3533 |
if (!s->connected || s->max_size <= 0) |
3534 |
return;
|
3535 |
len = sizeof(buf);
|
3536 |
if (len > s->max_size)
|
3537 |
len = s->max_size; |
3538 |
size = recv(s->fd, buf, len, 0);
|
3539 |
if (size == 0) { |
3540 |
/* connection closed */
|
3541 |
s->connected = 0;
|
3542 |
if (s->listen_fd >= 0) { |
3543 |
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3544 |
} |
3545 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
3546 |
closesocket(s->fd); |
3547 |
s->fd = -1;
|
3548 |
} else if (size > 0) { |
3549 |
if (s->do_telnetopt)
|
3550 |
tcp_chr_process_IAC_bytes(chr, s, buf, &size); |
3551 |
if (size > 0) |
3552 |
qemu_chr_read(chr, buf, size); |
3553 |
} |
3554 |
} |
3555 |
|
3556 |
static void tcp_chr_connect(void *opaque) |
3557 |
{ |
3558 |
CharDriverState *chr = opaque; |
3559 |
TCPCharDriver *s = chr->opaque; |
3560 |
|
3561 |
s->connected = 1;
|
3562 |
qemu_set_fd_handler2(s->fd, tcp_chr_read_poll, |
3563 |
tcp_chr_read, NULL, chr);
|
3564 |
qemu_chr_reset(chr); |
3565 |
} |
3566 |
|
3567 |
#define IACSET(x,a,b,c) x[0] = a; x[1] = b; x[2] = c; |
3568 |
static void tcp_chr_telnet_init(int fd) |
3569 |
{ |
3570 |
char buf[3]; |
3571 |
/* Send the telnet negotion to put telnet in binary, no echo, single char mode */
|
3572 |
IACSET(buf, 0xff, 0xfb, 0x01); /* IAC WILL ECHO */ |
3573 |
send(fd, (char *)buf, 3, 0); |
3574 |
IACSET(buf, 0xff, 0xfb, 0x03); /* IAC WILL Suppress go ahead */ |
3575 |
send(fd, (char *)buf, 3, 0); |
3576 |
IACSET(buf, 0xff, 0xfb, 0x00); /* IAC WILL Binary */ |
3577 |
send(fd, (char *)buf, 3, 0); |
3578 |
IACSET(buf, 0xff, 0xfd, 0x00); /* IAC DO Binary */ |
3579 |
send(fd, (char *)buf, 3, 0); |
3580 |
} |
3581 |
|
3582 |
static void socket_set_nodelay(int fd) |
3583 |
{ |
3584 |
int val = 1; |
3585 |
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val)); |
3586 |
} |
3587 |
|
3588 |
static void tcp_chr_accept(void *opaque) |
3589 |
{ |
3590 |
CharDriverState *chr = opaque; |
3591 |
TCPCharDriver *s = chr->opaque; |
3592 |
struct sockaddr_in saddr;
|
3593 |
#ifndef _WIN32
|
3594 |
struct sockaddr_un uaddr;
|
3595 |
#endif
|
3596 |
struct sockaddr *addr;
|
3597 |
socklen_t len; |
3598 |
int fd;
|
3599 |
|
3600 |
for(;;) {
|
3601 |
#ifndef _WIN32
|
3602 |
if (s->is_unix) {
|
3603 |
len = sizeof(uaddr);
|
3604 |
addr = (struct sockaddr *)&uaddr;
|
3605 |
} else
|
3606 |
#endif
|
3607 |
{ |
3608 |
len = sizeof(saddr);
|
3609 |
addr = (struct sockaddr *)&saddr;
|
3610 |
} |
3611 |
fd = accept(s->listen_fd, addr, &len); |
3612 |
if (fd < 0 && errno != EINTR) { |
3613 |
return;
|
3614 |
} else if (fd >= 0) { |
3615 |
if (s->do_telnetopt)
|
3616 |
tcp_chr_telnet_init(fd); |
3617 |
break;
|
3618 |
} |
3619 |
} |
3620 |
socket_set_nonblock(fd); |
3621 |
if (s->do_nodelay)
|
3622 |
socket_set_nodelay(fd); |
3623 |
s->fd = fd; |
3624 |
qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); |
3625 |
tcp_chr_connect(chr); |
3626 |
} |
3627 |
|
3628 |
static void tcp_chr_close(CharDriverState *chr) |
3629 |
{ |
3630 |
TCPCharDriver *s = chr->opaque; |
3631 |
if (s->fd >= 0) |
3632 |
closesocket(s->fd); |
3633 |
if (s->listen_fd >= 0) |
3634 |
closesocket(s->listen_fd); |
3635 |
qemu_free(s); |
3636 |
} |
3637 |
|
3638 |
static CharDriverState *qemu_chr_open_tcp(const char *host_str, |
3639 |
int is_telnet,
|
3640 |
int is_unix)
|
3641 |
{ |
3642 |
CharDriverState *chr = NULL;
|
3643 |
TCPCharDriver *s = NULL;
|
3644 |
int fd = -1, ret, err, val; |
3645 |
int is_listen = 0; |
3646 |
int is_waitconnect = 1; |
3647 |
int do_nodelay = 0; |
3648 |
const char *ptr; |
3649 |
struct sockaddr_in saddr;
|
3650 |
#ifndef _WIN32
|
3651 |
struct sockaddr_un uaddr;
|
3652 |
#endif
|
3653 |
struct sockaddr *addr;
|
3654 |
socklen_t addrlen; |
3655 |
|
3656 |
#ifndef _WIN32
|
3657 |
if (is_unix) {
|
3658 |
addr = (struct sockaddr *)&uaddr;
|
3659 |
addrlen = sizeof(uaddr);
|
3660 |
if (parse_unix_path(&uaddr, host_str) < 0) |
3661 |
goto fail;
|
3662 |
} else
|
3663 |
#endif
|
3664 |
{ |
3665 |
addr = (struct sockaddr *)&saddr;
|
3666 |
addrlen = sizeof(saddr);
|
3667 |
if (parse_host_port(&saddr, host_str) < 0) |
3668 |
goto fail;
|
3669 |
} |
3670 |
|
3671 |
ptr = host_str; |
3672 |
while((ptr = strchr(ptr,','))) { |
3673 |
ptr++; |
3674 |
if (!strncmp(ptr,"server",6)) { |
3675 |
is_listen = 1;
|
3676 |
} else if (!strncmp(ptr,"nowait",6)) { |
3677 |
is_waitconnect = 0;
|
3678 |
} else if (!strncmp(ptr,"nodelay",6)) { |
3679 |
do_nodelay = 1;
|
3680 |
} else {
|
3681 |
printf("Unknown option: %s\n", ptr);
|
3682 |
goto fail;
|
3683 |
} |
3684 |
} |
3685 |
if (!is_listen)
|
3686 |
is_waitconnect = 0;
|
3687 |
|
3688 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
3689 |
if (!chr)
|
3690 |
goto fail;
|
3691 |
s = qemu_mallocz(sizeof(TCPCharDriver));
|
3692 |
if (!s)
|
3693 |
goto fail;
|
3694 |
|
3695 |
#ifndef _WIN32
|
3696 |
if (is_unix)
|
3697 |
fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
3698 |
else
|
3699 |
#endif
|
3700 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
3701 |
|
3702 |
if (fd < 0) |
3703 |
goto fail;
|
3704 |
|
3705 |
if (!is_waitconnect)
|
3706 |
socket_set_nonblock(fd); |
3707 |
|
3708 |
s->connected = 0;
|
3709 |
s->fd = -1;
|
3710 |
s->listen_fd = -1;
|
3711 |
s->is_unix = is_unix; |
3712 |
s->do_nodelay = do_nodelay && !is_unix; |
3713 |
|
3714 |
chr->opaque = s; |
3715 |
chr->chr_write = tcp_chr_write; |
3716 |
chr->chr_close = tcp_chr_close; |
3717 |
|
3718 |
if (is_listen) {
|
3719 |
/* allow fast reuse */
|
3720 |
#ifndef _WIN32
|
3721 |
if (is_unix) {
|
3722 |
char path[109]; |
3723 |
pstrcpy(path, sizeof(path), uaddr.sun_path);
|
3724 |
unlink(path); |
3725 |
} else
|
3726 |
#endif
|
3727 |
{ |
3728 |
val = 1;
|
3729 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
3730 |
} |
3731 |
|
3732 |
ret = bind(fd, addr, addrlen); |
3733 |
if (ret < 0) |
3734 |
goto fail;
|
3735 |
|
3736 |
ret = listen(fd, 0);
|
3737 |
if (ret < 0) |
3738 |
goto fail;
|
3739 |
|
3740 |
s->listen_fd = fd; |
3741 |
qemu_set_fd_handler(s->listen_fd, tcp_chr_accept, NULL, chr);
|
3742 |
if (is_telnet)
|
3743 |
s->do_telnetopt = 1;
|
3744 |
} else {
|
3745 |
for(;;) {
|
3746 |
ret = connect(fd, addr, addrlen); |
3747 |
if (ret < 0) { |
3748 |
err = socket_error(); |
3749 |
if (err == EINTR || err == EWOULDBLOCK) {
|
3750 |
} else if (err == EINPROGRESS) { |
3751 |
break;
|
3752 |
#ifdef _WIN32
|
3753 |
} else if (err == WSAEALREADY) { |
3754 |
break;
|
3755 |
#endif
|
3756 |
} else {
|
3757 |
goto fail;
|
3758 |
} |
3759 |
} else {
|
3760 |
s->connected = 1;
|
3761 |
break;
|
3762 |
} |
3763 |
} |
3764 |
s->fd = fd; |
3765 |
socket_set_nodelay(fd); |
3766 |
if (s->connected)
|
3767 |
tcp_chr_connect(chr); |
3768 |
else
|
3769 |
qemu_set_fd_handler(s->fd, NULL, tcp_chr_connect, chr);
|
3770 |
} |
3771 |
|
3772 |
if (is_listen && is_waitconnect) {
|
3773 |
printf("QEMU waiting for connection on: %s\n", host_str);
|
3774 |
tcp_chr_accept(chr); |
3775 |
socket_set_nonblock(s->listen_fd); |
3776 |
} |
3777 |
|
3778 |
return chr;
|
3779 |
fail:
|
3780 |
if (fd >= 0) |
3781 |
closesocket(fd); |
3782 |
qemu_free(s); |
3783 |
qemu_free(chr); |
3784 |
return NULL; |
3785 |
} |
3786 |
|
3787 |
CharDriverState *qemu_chr_open(const char *filename) |
3788 |
{ |
3789 |
const char *p; |
3790 |
|
3791 |
if (!strcmp(filename, "vc")) { |
3792 |
return text_console_init(&display_state, 0); |
3793 |
} else if (strstart(filename, "vc:", &p)) { |
3794 |
return text_console_init(&display_state, p);
|
3795 |
} else if (!strcmp(filename, "null")) { |
3796 |
return qemu_chr_open_null();
|
3797 |
} else
|
3798 |
if (strstart(filename, "tcp:", &p)) { |
3799 |
return qemu_chr_open_tcp(p, 0, 0); |
3800 |
} else
|
3801 |
if (strstart(filename, "telnet:", &p)) { |
3802 |
return qemu_chr_open_tcp(p, 1, 0); |
3803 |
} else
|
3804 |
if (strstart(filename, "udp:", &p)) { |
3805 |
return qemu_chr_open_udp(p);
|
3806 |
} else
|
3807 |
if (strstart(filename, "mon:", &p)) { |
3808 |
CharDriverState *drv = qemu_chr_open(p); |
3809 |
if (drv) {
|
3810 |
drv = qemu_chr_open_mux(drv); |
3811 |
monitor_init(drv, !nographic); |
3812 |
return drv;
|
3813 |
} |
3814 |
printf("Unable to open driver: %s\n", p);
|
3815 |
return 0; |
3816 |
} else
|
3817 |
#ifndef _WIN32
|
3818 |
if (strstart(filename, "unix:", &p)) { |
3819 |
return qemu_chr_open_tcp(p, 0, 1); |
3820 |
} else if (strstart(filename, "file:", &p)) { |
3821 |
return qemu_chr_open_file_out(p);
|
3822 |
} else if (strstart(filename, "pipe:", &p)) { |
3823 |
return qemu_chr_open_pipe(p);
|
3824 |
} else if (!strcmp(filename, "pty")) { |
3825 |
return qemu_chr_open_pty();
|
3826 |
} else if (!strcmp(filename, "stdio")) { |
3827 |
return qemu_chr_open_stdio();
|
3828 |
} else
|
3829 |
#if defined(__linux__)
|
3830 |
if (strstart(filename, "/dev/parport", NULL)) { |
3831 |
return qemu_chr_open_pp(filename);
|
3832 |
} else
|
3833 |
#endif
|
3834 |
#if defined(__linux__) || defined(__sun__) || defined(__FreeBSD__) \
|
3835 |
|| defined(__NetBSD__) || defined(__OpenBSD__) |
3836 |
if (strstart(filename, "/dev/", NULL)) { |
3837 |
return qemu_chr_open_tty(filename);
|
3838 |
} else
|
3839 |
#endif
|
3840 |
#else /* !_WIN32 */ |
3841 |
if (strstart(filename, "COM", NULL)) { |
3842 |
return qemu_chr_open_win(filename);
|
3843 |
} else
|
3844 |
if (strstart(filename, "pipe:", &p)) { |
3845 |
return qemu_chr_open_win_pipe(p);
|
3846 |
} else
|
3847 |
if (strstart(filename, "con:", NULL)) { |
3848 |
return qemu_chr_open_win_con(filename);
|
3849 |
} else
|
3850 |
if (strstart(filename, "file:", &p)) { |
3851 |
return qemu_chr_open_win_file_out(p);
|
3852 |
} else
|
3853 |
#endif
|
3854 |
#ifdef CONFIG_BRLAPI
|
3855 |
if (!strcmp(filename, "braille")) { |
3856 |
return chr_baum_init();
|
3857 |
} else
|
3858 |
#endif
|
3859 |
{ |
3860 |
return NULL; |
3861 |
} |
3862 |
} |
3863 |
|
3864 |
void qemu_chr_close(CharDriverState *chr)
|
3865 |
{ |
3866 |
if (chr->chr_close)
|
3867 |
chr->chr_close(chr); |
3868 |
qemu_free(chr); |
3869 |
} |
3870 |
|
3871 |
/***********************************************************/
|
3872 |
/* network device redirectors */
|
3873 |
|
3874 |
__attribute__ (( unused )) |
3875 |
static void hex_dump(FILE *f, const uint8_t *buf, int size) |
3876 |
{ |
3877 |
int len, i, j, c;
|
3878 |
|
3879 |
for(i=0;i<size;i+=16) { |
3880 |
len = size - i; |
3881 |
if (len > 16) |
3882 |
len = 16;
|
3883 |
fprintf(f, "%08x ", i);
|
3884 |
for(j=0;j<16;j++) { |
3885 |
if (j < len)
|
3886 |
fprintf(f, " %02x", buf[i+j]);
|
3887 |
else
|
3888 |
fprintf(f, " ");
|
3889 |
} |
3890 |
fprintf(f, " ");
|
3891 |
for(j=0;j<len;j++) { |
3892 |
c = buf[i+j]; |
3893 |
if (c < ' ' || c > '~') |
3894 |
c = '.';
|
3895 |
fprintf(f, "%c", c);
|
3896 |
} |
3897 |
fprintf(f, "\n");
|
3898 |
} |
3899 |
} |
3900 |
|
3901 |
static int parse_macaddr(uint8_t *macaddr, const char *p) |
3902 |
{ |
3903 |
int i;
|
3904 |
char *last_char;
|
3905 |
long int offset; |
3906 |
|
3907 |
errno = 0;
|
3908 |
offset = strtol(p, &last_char, 0);
|
3909 |
if (0 == errno && '\0' == *last_char && |
3910 |
offset >= 0 && offset <= 0xFFFFFF) { |
3911 |
macaddr[3] = (offset & 0xFF0000) >> 16; |
3912 |
macaddr[4] = (offset & 0xFF00) >> 8; |
3913 |
macaddr[5] = offset & 0xFF; |
3914 |
return 0; |
3915 |
} else {
|
3916 |
for(i = 0; i < 6; i++) { |
3917 |
macaddr[i] = strtol(p, (char **)&p, 16); |
3918 |
if (i == 5) { |
3919 |
if (*p != '\0') |
3920 |
return -1; |
3921 |
} else {
|
3922 |
if (*p != ':' && *p != '-') |
3923 |
return -1; |
3924 |
p++; |
3925 |
} |
3926 |
} |
3927 |
return 0; |
3928 |
} |
3929 |
|
3930 |
return -1; |
3931 |
} |
3932 |
|
3933 |
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
3934 |
{ |
3935 |
const char *p, *p1; |
3936 |
int len;
|
3937 |
p = *pp; |
3938 |
p1 = strchr(p, sep); |
3939 |
if (!p1)
|
3940 |
return -1; |
3941 |
len = p1 - p; |
3942 |
p1++; |
3943 |
if (buf_size > 0) { |
3944 |
if (len > buf_size - 1) |
3945 |
len = buf_size - 1;
|
3946 |
memcpy(buf, p, len); |
3947 |
buf[len] = '\0';
|
3948 |
} |
3949 |
*pp = p1; |
3950 |
return 0; |
3951 |
} |
3952 |
|
3953 |
int parse_host_src_port(struct sockaddr_in *haddr, |
3954 |
struct sockaddr_in *saddr,
|
3955 |
const char *input_str) |
3956 |
{ |
3957 |
char *str = strdup(input_str);
|
3958 |
char *host_str = str;
|
3959 |
char *src_str;
|
3960 |
char *ptr;
|
3961 |
|
3962 |
/*
|
3963 |
* Chop off any extra arguments at the end of the string which
|
3964 |
* would start with a comma, then fill in the src port information
|
3965 |
* if it was provided else use the "any address" and "any port".
|
3966 |
*/
|
3967 |
if ((ptr = strchr(str,','))) |
3968 |
*ptr = '\0';
|
3969 |
|
3970 |
if ((src_str = strchr(input_str,'@'))) { |
3971 |
*src_str = '\0';
|
3972 |
src_str++; |
3973 |
} |
3974 |
|
3975 |
if (parse_host_port(haddr, host_str) < 0) |
3976 |
goto fail;
|
3977 |
|
3978 |
if (!src_str || *src_str == '\0') |
3979 |
src_str = ":0";
|
3980 |
|
3981 |
if (parse_host_port(saddr, src_str) < 0) |
3982 |
goto fail;
|
3983 |
|
3984 |
free(str); |
3985 |
return(0); |
3986 |
|
3987 |
fail:
|
3988 |
free(str); |
3989 |
return -1; |
3990 |
} |
3991 |
|
3992 |
int parse_host_port(struct sockaddr_in *saddr, const char *str) |
3993 |
{ |
3994 |
char buf[512]; |
3995 |
struct hostent *he;
|
3996 |
const char *p, *r; |
3997 |
int port;
|
3998 |
|
3999 |
p = str; |
4000 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4001 |
return -1; |
4002 |
saddr->sin_family = AF_INET; |
4003 |
if (buf[0] == '\0') { |
4004 |
saddr->sin_addr.s_addr = 0;
|
4005 |
} else {
|
4006 |
if (isdigit(buf[0])) { |
4007 |
if (!inet_aton(buf, &saddr->sin_addr))
|
4008 |
return -1; |
4009 |
} else {
|
4010 |
if ((he = gethostbyname(buf)) == NULL) |
4011 |
return - 1; |
4012 |
saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
4013 |
} |
4014 |
} |
4015 |
port = strtol(p, (char **)&r, 0); |
4016 |
if (r == p)
|
4017 |
return -1; |
4018 |
saddr->sin_port = htons(port); |
4019 |
return 0; |
4020 |
} |
4021 |
|
4022 |
#ifndef _WIN32
|
4023 |
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
4024 |
{ |
4025 |
const char *p; |
4026 |
int len;
|
4027 |
|
4028 |
len = MIN(108, strlen(str));
|
4029 |
p = strchr(str, ',');
|
4030 |
if (p)
|
4031 |
len = MIN(len, p - str); |
4032 |
|
4033 |
memset(uaddr, 0, sizeof(*uaddr)); |
4034 |
|
4035 |
uaddr->sun_family = AF_UNIX; |
4036 |
memcpy(uaddr->sun_path, str, len); |
4037 |
|
4038 |
return 0; |
4039 |
} |
4040 |
#endif
|
4041 |
|
4042 |
/* find or alloc a new VLAN */
|
4043 |
VLANState *qemu_find_vlan(int id)
|
4044 |
{ |
4045 |
VLANState **pvlan, *vlan; |
4046 |
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
4047 |
if (vlan->id == id)
|
4048 |
return vlan;
|
4049 |
} |
4050 |
vlan = qemu_mallocz(sizeof(VLANState));
|
4051 |
if (!vlan)
|
4052 |
return NULL; |
4053 |
vlan->id = id; |
4054 |
vlan->next = NULL;
|
4055 |
pvlan = &first_vlan; |
4056 |
while (*pvlan != NULL) |
4057 |
pvlan = &(*pvlan)->next; |
4058 |
*pvlan = vlan; |
4059 |
return vlan;
|
4060 |
} |
4061 |
|
4062 |
VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
4063 |
IOReadHandler *fd_read, |
4064 |
IOCanRWHandler *fd_can_read, |
4065 |
void *opaque)
|
4066 |
{ |
4067 |
VLANClientState *vc, **pvc; |
4068 |
vc = qemu_mallocz(sizeof(VLANClientState));
|
4069 |
if (!vc)
|
4070 |
return NULL; |
4071 |
vc->fd_read = fd_read; |
4072 |
vc->fd_can_read = fd_can_read; |
4073 |
vc->opaque = opaque; |
4074 |
vc->vlan = vlan; |
4075 |
|
4076 |
vc->next = NULL;
|
4077 |
pvc = &vlan->first_client; |
4078 |
while (*pvc != NULL) |
4079 |
pvc = &(*pvc)->next; |
4080 |
*pvc = vc; |
4081 |
return vc;
|
4082 |
} |
4083 |
|
4084 |
void qemu_del_vlan_client(VLANClientState *vc)
|
4085 |
{ |
4086 |
VLANClientState **pvc = &vc->vlan->first_client; |
4087 |
|
4088 |
while (*pvc != NULL) |
4089 |
if (*pvc == vc) {
|
4090 |
*pvc = vc->next; |
4091 |
free(vc); |
4092 |
break;
|
4093 |
} else
|
4094 |
pvc = &(*pvc)->next; |
4095 |
} |
4096 |
|
4097 |
int qemu_can_send_packet(VLANClientState *vc1)
|
4098 |
{ |
4099 |
VLANState *vlan = vc1->vlan; |
4100 |
VLANClientState *vc; |
4101 |
|
4102 |
for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4103 |
if (vc != vc1) {
|
4104 |
if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
|
4105 |
return 1; |
4106 |
} |
4107 |
} |
4108 |
return 0; |
4109 |
} |
4110 |
|
4111 |
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
4112 |
{ |
4113 |
VLANState *vlan = vc1->vlan; |
4114 |
VLANClientState *vc; |
4115 |
|
4116 |
#if 0
|
4117 |
printf("vlan %d send:\n", vlan->id);
|
4118 |
hex_dump(stdout, buf, size);
|
4119 |
#endif
|
4120 |
for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
4121 |
if (vc != vc1) {
|
4122 |
vc->fd_read(vc->opaque, buf, size); |
4123 |
} |
4124 |
} |
4125 |
} |
4126 |
|
4127 |
#if defined(CONFIG_SLIRP)
|
4128 |
|
4129 |
/* slirp network adapter */
|
4130 |
|
4131 |
static int slirp_inited; |
4132 |
static VLANClientState *slirp_vc;
|
4133 |
|
4134 |
int slirp_can_output(void) |
4135 |
{ |
4136 |
return !slirp_vc || qemu_can_send_packet(slirp_vc);
|
4137 |
} |
4138 |
|
4139 |
void slirp_output(const uint8_t *pkt, int pkt_len) |
4140 |
{ |
4141 |
#if 0
|
4142 |
printf("slirp output:\n");
|
4143 |
hex_dump(stdout, pkt, pkt_len);
|
4144 |
#endif
|
4145 |
if (!slirp_vc)
|
4146 |
return;
|
4147 |
qemu_send_packet(slirp_vc, pkt, pkt_len); |
4148 |
} |
4149 |
|
4150 |
static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
4151 |
{ |
4152 |
#if 0
|
4153 |
printf("slirp input:\n");
|
4154 |
hex_dump(stdout, buf, size);
|
4155 |
#endif
|
4156 |
slirp_input(buf, size); |
4157 |
} |
4158 |
|
4159 |
static int net_slirp_init(VLANState *vlan) |
4160 |
{ |
4161 |
if (!slirp_inited) {
|
4162 |
slirp_inited = 1;
|
4163 |
slirp_init(); |
4164 |
} |
4165 |
slirp_vc = qemu_new_vlan_client(vlan, |
4166 |
slirp_receive, NULL, NULL); |
4167 |
snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
4168 |
return 0; |
4169 |
} |
4170 |
|
4171 |
static void net_slirp_redir(const char *redir_str) |
4172 |
{ |
4173 |
int is_udp;
|
4174 |
char buf[256], *r; |
4175 |
const char *p; |
4176 |
struct in_addr guest_addr;
|
4177 |
int host_port, guest_port;
|
4178 |
|
4179 |
if (!slirp_inited) {
|
4180 |
slirp_inited = 1;
|
4181 |
slirp_init(); |
4182 |
} |
4183 |
|
4184 |
p = redir_str; |
4185 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4186 |
goto fail;
|
4187 |
if (!strcmp(buf, "tcp")) { |
4188 |
is_udp = 0;
|
4189 |
} else if (!strcmp(buf, "udp")) { |
4190 |
is_udp = 1;
|
4191 |
} else {
|
4192 |
goto fail;
|
4193 |
} |
4194 |
|
4195 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4196 |
goto fail;
|
4197 |
host_port = strtol(buf, &r, 0);
|
4198 |
if (r == buf)
|
4199 |
goto fail;
|
4200 |
|
4201 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
4202 |
goto fail;
|
4203 |
if (buf[0] == '\0') { |
4204 |
pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
4205 |
} |
4206 |
if (!inet_aton(buf, &guest_addr))
|
4207 |
goto fail;
|
4208 |
|
4209 |
guest_port = strtol(p, &r, 0);
|
4210 |
if (r == p)
|
4211 |
goto fail;
|
4212 |
|
4213 |
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
4214 |
fprintf(stderr, "qemu: could not set up redirection\n");
|
4215 |
exit(1);
|
4216 |
} |
4217 |
return;
|
4218 |
fail:
|
4219 |
fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
4220 |
exit(1);
|
4221 |
} |
4222 |
|
4223 |
#ifndef _WIN32
|
4224 |
|
4225 |
char smb_dir[1024]; |
4226 |
|
4227 |
static void erase_dir(char *dir_name) |
4228 |
{ |
4229 |
DIR *d; |
4230 |
struct dirent *de;
|
4231 |
char filename[1024]; |
4232 |
|
4233 |
/* erase all the files in the directory */
|
4234 |
if ((d = opendir(dir_name)) != 0) { |
4235 |
for(;;) {
|
4236 |
de = readdir(d); |
4237 |
if (!de)
|
4238 |
break;
|
4239 |
if (strcmp(de->d_name, ".") != 0 && |
4240 |
strcmp(de->d_name, "..") != 0) { |
4241 |
snprintf(filename, sizeof(filename), "%s/%s", |
4242 |
smb_dir, de->d_name); |
4243 |
if (unlink(filename) != 0) /* is it a directory? */ |
4244 |
erase_dir(filename); |
4245 |
} |
4246 |
} |
4247 |
closedir(d); |
4248 |
rmdir(dir_name); |
4249 |
} |
4250 |
} |
4251 |
|
4252 |
/* automatic user mode samba server configuration */
|
4253 |
static void smb_exit(void) |
4254 |
{ |
4255 |
erase_dir(smb_dir); |
4256 |
} |
4257 |
|
4258 |
/* automatic user mode samba server configuration */
|
4259 |
static void net_slirp_smb(const char *exported_dir) |
4260 |
{ |
4261 |
char smb_conf[1024]; |
4262 |
char smb_cmdline[1024]; |
4263 |
FILE *f; |
4264 |
|
4265 |
if (!slirp_inited) {
|
4266 |
slirp_inited = 1;
|
4267 |
slirp_init(); |
4268 |
} |
4269 |
|
4270 |
/* XXX: better tmp dir construction */
|
4271 |
snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
4272 |
if (mkdir(smb_dir, 0700) < 0) { |
4273 |
fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
4274 |
exit(1);
|
4275 |
} |
4276 |
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
4277 |
|
4278 |
f = fopen(smb_conf, "w");
|
4279 |
if (!f) {
|
4280 |
fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
4281 |
exit(1);
|
4282 |
} |
4283 |
fprintf(f, |
4284 |
"[global]\n"
|
4285 |
"private dir=%s\n"
|
4286 |
"smb ports=0\n"
|
4287 |
"socket address=127.0.0.1\n"
|
4288 |
"pid directory=%s\n"
|
4289 |
"lock directory=%s\n"
|
4290 |
"log file=%s/log.smbd\n"
|
4291 |
"smb passwd file=%s/smbpasswd\n"
|
4292 |
"security = share\n"
|
4293 |
"[qemu]\n"
|
4294 |
"path=%s\n"
|
4295 |
"read only=no\n"
|
4296 |
"guest ok=yes\n",
|
4297 |
smb_dir, |
4298 |
smb_dir, |
4299 |
smb_dir, |
4300 |
smb_dir, |
4301 |
smb_dir, |
4302 |
exported_dir |
4303 |
); |
4304 |
fclose(f); |
4305 |
atexit(smb_exit); |
4306 |
|
4307 |
snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
4308 |
SMBD_COMMAND, smb_conf); |
4309 |
|
4310 |
slirp_add_exec(0, smb_cmdline, 4, 139); |
4311 |
} |
4312 |
|
4313 |
#endif /* !defined(_WIN32) */ |
4314 |
void do_info_slirp(void) |
4315 |
{ |
4316 |
slirp_stats(); |
4317 |
} |
4318 |
|
4319 |
#endif /* CONFIG_SLIRP */ |
4320 |
|
4321 |
#if !defined(_WIN32)
|
4322 |
|
4323 |
typedef struct TAPState { |
4324 |
VLANClientState *vc; |
4325 |
int fd;
|
4326 |
char down_script[1024]; |
4327 |
} TAPState; |
4328 |
|
4329 |
static void tap_receive(void *opaque, const uint8_t *buf, int size) |
4330 |
{ |
4331 |
TAPState *s = opaque; |
4332 |
int ret;
|
4333 |
for(;;) {
|
4334 |
ret = write(s->fd, buf, size); |
4335 |
if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
4336 |
} else {
|
4337 |
break;
|
4338 |
} |
4339 |
} |
4340 |
} |
4341 |
|
4342 |
static void tap_send(void *opaque) |
4343 |
{ |
4344 |
TAPState *s = opaque; |
4345 |
uint8_t buf[4096];
|
4346 |
int size;
|
4347 |
|
4348 |
#ifdef __sun__
|
4349 |
struct strbuf sbuf;
|
4350 |
int f = 0; |
4351 |
sbuf.maxlen = sizeof(buf);
|
4352 |
sbuf.buf = buf; |
4353 |
size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
4354 |
#else
|
4355 |
size = read(s->fd, buf, sizeof(buf));
|
4356 |
#endif
|
4357 |
if (size > 0) { |
4358 |
qemu_send_packet(s->vc, buf, size); |
4359 |
} |
4360 |
} |
4361 |
|
4362 |
/* fd support */
|
4363 |
|
4364 |
static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
4365 |
{ |
4366 |
TAPState *s; |
4367 |
|
4368 |
s = qemu_mallocz(sizeof(TAPState));
|
4369 |
if (!s)
|
4370 |
return NULL; |
4371 |
s->fd = fd; |
4372 |
s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s);
|
4373 |
qemu_set_fd_handler(s->fd, tap_send, NULL, s);
|
4374 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
4375 |
return s;
|
4376 |
} |
4377 |
|
4378 |
#if defined (_BSD) || defined (__FreeBSD_kernel__)
|
4379 |
static int tap_open(char *ifname, int ifname_size) |
4380 |
{ |
4381 |
int fd;
|
4382 |
char *dev;
|
4383 |
struct stat s;
|
4384 |
|
4385 |
TFR(fd = open("/dev/tap", O_RDWR));
|
4386 |
if (fd < 0) { |
4387 |
fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
4388 |
return -1; |
4389 |
} |
4390 |
|
4391 |
fstat(fd, &s); |
4392 |
dev = devname(s.st_rdev, S_IFCHR); |
4393 |
pstrcpy(ifname, ifname_size, dev); |
4394 |
|
4395 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
4396 |
return fd;
|
4397 |
} |
4398 |
#elif defined(__sun__)
|
4399 |
#define TUNNEWPPA (('T'<<16) | 0x0001) |
4400 |
/*
|
4401 |
* Allocate TAP device, returns opened fd.
|
4402 |
* Stores dev name in the first arg(must be large enough).
|
4403 |
*/
|
4404 |
int tap_alloc(char *dev, size_t dev_size) |
4405 |
{ |
4406 |
int tap_fd, if_fd, ppa = -1; |
4407 |
static int ip_fd = 0; |
4408 |
char *ptr;
|
4409 |
|
4410 |
static int arp_fd = 0; |
4411 |
int ip_muxid, arp_muxid;
|
4412 |
struct strioctl strioc_if, strioc_ppa;
|
4413 |
int link_type = I_PLINK;;
|
4414 |
struct lifreq ifr;
|
4415 |
char actual_name[32] = ""; |
4416 |
|
4417 |
memset(&ifr, 0x0, sizeof(ifr)); |
4418 |
|
4419 |
if( *dev ){
|
4420 |
ptr = dev; |
4421 |
while( *ptr && !isdigit((int)*ptr) ) ptr++; |
4422 |
ppa = atoi(ptr); |
4423 |
} |
4424 |
|
4425 |
/* Check if IP device was opened */
|
4426 |
if( ip_fd )
|
4427 |
close(ip_fd); |
4428 |
|
4429 |
TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
4430 |
if (ip_fd < 0) { |
4431 |
syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
|
4432 |
return -1; |
4433 |
} |
4434 |
|
4435 |
TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
4436 |
if (tap_fd < 0) { |
4437 |
syslog(LOG_ERR, "Can't open /dev/tap");
|
4438 |
return -1; |
4439 |
} |
4440 |
|
4441 |
/* Assign a new PPA and get its unit number. */
|
4442 |
strioc_ppa.ic_cmd = TUNNEWPPA; |
4443 |
strioc_ppa.ic_timout = 0;
|
4444 |
strioc_ppa.ic_len = sizeof(ppa);
|
4445 |
strioc_ppa.ic_dp = (char *)&ppa;
|
4446 |
if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
4447 |
syslog (LOG_ERR, "Can't assign new interface");
|
4448 |
|
4449 |
TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
4450 |
if (if_fd < 0) { |
4451 |
syslog(LOG_ERR, "Can't open /dev/tap (2)");
|
4452 |
return -1; |
4453 |
} |
4454 |
if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
4455 |
syslog(LOG_ERR, "Can't push IP module");
|
4456 |
return -1; |
4457 |
} |
4458 |
|
4459 |
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
4460 |
syslog(LOG_ERR, "Can't get flags\n");
|
4461 |
|
4462 |
snprintf (actual_name, 32, "tap%d", ppa); |
4463 |
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4464 |
|
4465 |
ifr.lifr_ppa = ppa; |
4466 |
/* Assign ppa according to the unit number returned by tun device */
|
4467 |
|
4468 |
if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
4469 |
syslog (LOG_ERR, "Can't set PPA %d", ppa);
|
4470 |
if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
4471 |
syslog (LOG_ERR, "Can't get flags\n");
|
4472 |
/* Push arp module to if_fd */
|
4473 |
if (ioctl (if_fd, I_PUSH, "arp") < 0) |
4474 |
syslog (LOG_ERR, "Can't push ARP module (2)");
|
4475 |
|
4476 |
/* Push arp module to ip_fd */
|
4477 |
if (ioctl (ip_fd, I_POP, NULL) < 0) |
4478 |
syslog (LOG_ERR, "I_POP failed\n");
|
4479 |
if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
4480 |
syslog (LOG_ERR, "Can't push ARP module (3)\n");
|
4481 |
/* Open arp_fd */
|
4482 |
TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
4483 |
if (arp_fd < 0) |
4484 |
syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
4485 |
|
4486 |
/* Set ifname to arp */
|
4487 |
strioc_if.ic_cmd = SIOCSLIFNAME; |
4488 |
strioc_if.ic_timout = 0;
|
4489 |
strioc_if.ic_len = sizeof(ifr);
|
4490 |
strioc_if.ic_dp = (char *)𝔦
|
4491 |
if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
4492 |
syslog (LOG_ERR, "Can't set ifname to arp\n");
|
4493 |
} |
4494 |
|
4495 |
if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
4496 |
syslog(LOG_ERR, "Can't link TAP device to IP");
|
4497 |
return -1; |
4498 |
} |
4499 |
|
4500 |
if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
4501 |
syslog (LOG_ERR, "Can't link TAP device to ARP");
|
4502 |
|
4503 |
close (if_fd); |
4504 |
|
4505 |
memset(&ifr, 0x0, sizeof(ifr)); |
4506 |
strncpy (ifr.lifr_name, actual_name, sizeof (ifr.lifr_name));
|
4507 |
ifr.lifr_ip_muxid = ip_muxid; |
4508 |
ifr.lifr_arp_muxid = arp_muxid; |
4509 |
|
4510 |
if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
4511 |
{ |
4512 |
ioctl (ip_fd, I_PUNLINK , arp_muxid); |
4513 |
ioctl (ip_fd, I_PUNLINK, ip_muxid); |
4514 |
syslog (LOG_ERR, "Can't set multiplexor id");
|
4515 |
} |
4516 |
|
4517 |
snprintf(dev, dev_size, "tap%d", ppa);
|
4518 |
return tap_fd;
|
4519 |
} |
4520 |
|
4521 |
static int tap_open(char *ifname, int ifname_size) |
4522 |
{ |
4523 |
char dev[10]=""; |
4524 |
int fd;
|
4525 |
if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ |
4526 |
fprintf(stderr, "Cannot allocate TAP device\n");
|
4527 |
return -1; |
4528 |
} |
4529 |
pstrcpy(ifname, ifname_size, dev); |
4530 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
4531 |
return fd;
|
4532 |
} |
4533 |
#else
|
4534 |
static int tap_open(char *ifname, int ifname_size) |
4535 |
{ |
4536 |
struct ifreq ifr;
|
4537 |
int fd, ret;
|
4538 |
|
4539 |
TFR(fd = open("/dev/net/tun", O_RDWR));
|
4540 |
if (fd < 0) { |
4541 |
fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
4542 |
return -1; |
4543 |
} |
4544 |
memset(&ifr, 0, sizeof(ifr)); |
4545 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
4546 |
if (ifname[0] != '\0') |
4547 |
pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
4548 |
else
|
4549 |
pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
|
4550 |
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
4551 |
if (ret != 0) { |
4552 |
fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
4553 |
close(fd); |
4554 |
return -1; |
4555 |
} |
4556 |
pstrcpy(ifname, ifname_size, ifr.ifr_name); |
4557 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
4558 |
return fd;
|
4559 |
} |
4560 |
#endif
|
4561 |
|
4562 |
static int launch_script(const char *setup_script, const char *ifname, int fd) |
4563 |
{ |
4564 |
int pid, status;
|
4565 |
char *args[3]; |
4566 |
char **parg;
|
4567 |
|
4568 |
/* try to launch network script */
|
4569 |
pid = fork(); |
4570 |
if (pid >= 0) { |
4571 |
if (pid == 0) { |
4572 |
int open_max = sysconf (_SC_OPEN_MAX), i;
|
4573 |
for (i = 0; i < open_max; i++) |
4574 |
if (i != STDIN_FILENO &&
|
4575 |
i != STDOUT_FILENO && |
4576 |
i != STDERR_FILENO && |
4577 |
i != fd) |
4578 |
close(i); |
4579 |
|
4580 |
parg = args; |
4581 |
*parg++ = (char *)setup_script;
|
4582 |
*parg++ = (char *)ifname;
|
4583 |
*parg++ = NULL;
|
4584 |
execv(setup_script, args); |
4585 |
_exit(1);
|
4586 |
} |
4587 |
while (waitpid(pid, &status, 0) != pid); |
4588 |
if (!WIFEXITED(status) ||
|
4589 |
WEXITSTATUS(status) != 0) {
|
4590 |
fprintf(stderr, "%s: could not launch network script\n",
|
4591 |
setup_script); |
4592 |
return -1; |
4593 |
} |
4594 |
} |
4595 |
return 0; |
4596 |
} |
4597 |
|
4598 |
static int net_tap_init(VLANState *vlan, const char *ifname1, |
4599 |
const char *setup_script, const char *down_script) |
4600 |
{ |
4601 |
TAPState *s; |
4602 |
int fd;
|
4603 |
char ifname[128]; |
4604 |
|
4605 |
if (ifname1 != NULL) |
4606 |
pstrcpy(ifname, sizeof(ifname), ifname1);
|
4607 |
else
|
4608 |
ifname[0] = '\0'; |
4609 |
TFR(fd = tap_open(ifname, sizeof(ifname)));
|
4610 |
if (fd < 0) |
4611 |
return -1; |
4612 |
|
4613 |
if (!setup_script || !strcmp(setup_script, "no")) |
4614 |
setup_script = "";
|
4615 |
if (setup_script[0] != '\0') { |
4616 |
if (launch_script(setup_script, ifname, fd))
|
4617 |
return -1; |
4618 |
} |
4619 |
s = net_tap_fd_init(vlan, fd); |
4620 |
if (!s)
|
4621 |
return -1; |
4622 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4623 |
"tap: ifname=%s setup_script=%s", ifname, setup_script);
|
4624 |
if (down_script && strcmp(down_script, "no")) |
4625 |
snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
4626 |
return 0; |
4627 |
} |
4628 |
|
4629 |
#endif /* !_WIN32 */ |
4630 |
|
4631 |
#if defined(CONFIG_VDE)
|
4632 |
typedef struct VDEState { |
4633 |
VLANClientState *vc; |
4634 |
VDECONN *vde; |
4635 |
} VDEState; |
4636 |
|
4637 |
static void vde_to_qemu(void *opaque) |
4638 |
{ |
4639 |
VDEState *s = opaque; |
4640 |
uint8_t buf[4096];
|
4641 |
int size;
|
4642 |
|
4643 |
size = vde_recv(s->vde, buf, sizeof(buf), 0); |
4644 |
if (size > 0) { |
4645 |
qemu_send_packet(s->vc, buf, size); |
4646 |
} |
4647 |
} |
4648 |
|
4649 |
static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) |
4650 |
{ |
4651 |
VDEState *s = opaque; |
4652 |
int ret;
|
4653 |
for(;;) {
|
4654 |
ret = vde_send(s->vde, buf, size, 0);
|
4655 |
if (ret < 0 && errno == EINTR) { |
4656 |
} else {
|
4657 |
break;
|
4658 |
} |
4659 |
} |
4660 |
} |
4661 |
|
4662 |
static int net_vde_init(VLANState *vlan, const char *sock, int port, |
4663 |
const char *group, int mode) |
4664 |
{ |
4665 |
VDEState *s; |
4666 |
char *init_group = strlen(group) ? (char *)group : NULL; |
4667 |
char *init_sock = strlen(sock) ? (char *)sock : NULL; |
4668 |
|
4669 |
struct vde_open_args args = {
|
4670 |
.port = port, |
4671 |
.group = init_group, |
4672 |
.mode = mode, |
4673 |
}; |
4674 |
|
4675 |
s = qemu_mallocz(sizeof(VDEState));
|
4676 |
if (!s)
|
4677 |
return -1; |
4678 |
s->vde = vde_open(init_sock, "QEMU", &args);
|
4679 |
if (!s->vde){
|
4680 |
free(s); |
4681 |
return -1; |
4682 |
} |
4683 |
s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s);
|
4684 |
qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
|
4685 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d", |
4686 |
sock, vde_datafd(s->vde)); |
4687 |
return 0; |
4688 |
} |
4689 |
#endif
|
4690 |
|
4691 |
/* network connection */
|
4692 |
typedef struct NetSocketState { |
4693 |
VLANClientState *vc; |
4694 |
int fd;
|
4695 |
int state; /* 0 = getting length, 1 = getting data */ |
4696 |
int index;
|
4697 |
int packet_len;
|
4698 |
uint8_t buf[4096];
|
4699 |
struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
4700 |
} NetSocketState; |
4701 |
|
4702 |
typedef struct NetSocketListenState { |
4703 |
VLANState *vlan; |
4704 |
int fd;
|
4705 |
} NetSocketListenState; |
4706 |
|
4707 |
/* XXX: we consider we can send the whole packet without blocking */
|
4708 |
static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
4709 |
{ |
4710 |
NetSocketState *s = opaque; |
4711 |
uint32_t len; |
4712 |
len = htonl(size); |
4713 |
|
4714 |
send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
4715 |
send_all(s->fd, buf, size); |
4716 |
} |
4717 |
|
4718 |
static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
4719 |
{ |
4720 |
NetSocketState *s = opaque; |
4721 |
sendto(s->fd, buf, size, 0,
|
4722 |
(struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
4723 |
} |
4724 |
|
4725 |
static void net_socket_send(void *opaque) |
4726 |
{ |
4727 |
NetSocketState *s = opaque; |
4728 |
int l, size, err;
|
4729 |
uint8_t buf1[4096];
|
4730 |
const uint8_t *buf;
|
4731 |
|
4732 |
size = recv(s->fd, buf1, sizeof(buf1), 0); |
4733 |
if (size < 0) { |
4734 |
err = socket_error(); |
4735 |
if (err != EWOULDBLOCK)
|
4736 |
goto eoc;
|
4737 |
} else if (size == 0) { |
4738 |
/* end of connection */
|
4739 |
eoc:
|
4740 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4741 |
closesocket(s->fd); |
4742 |
return;
|
4743 |
} |
4744 |
buf = buf1; |
4745 |
while (size > 0) { |
4746 |
/* reassemble a packet from the network */
|
4747 |
switch(s->state) {
|
4748 |
case 0: |
4749 |
l = 4 - s->index;
|
4750 |
if (l > size)
|
4751 |
l = size; |
4752 |
memcpy(s->buf + s->index, buf, l); |
4753 |
buf += l; |
4754 |
size -= l; |
4755 |
s->index += l; |
4756 |
if (s->index == 4) { |
4757 |
/* got length */
|
4758 |
s->packet_len = ntohl(*(uint32_t *)s->buf); |
4759 |
s->index = 0;
|
4760 |
s->state = 1;
|
4761 |
} |
4762 |
break;
|
4763 |
case 1: |
4764 |
l = s->packet_len - s->index; |
4765 |
if (l > size)
|
4766 |
l = size; |
4767 |
memcpy(s->buf + s->index, buf, l); |
4768 |
s->index += l; |
4769 |
buf += l; |
4770 |
size -= l; |
4771 |
if (s->index >= s->packet_len) {
|
4772 |
qemu_send_packet(s->vc, s->buf, s->packet_len); |
4773 |
s->index = 0;
|
4774 |
s->state = 0;
|
4775 |
} |
4776 |
break;
|
4777 |
} |
4778 |
} |
4779 |
} |
4780 |
|
4781 |
static void net_socket_send_dgram(void *opaque) |
4782 |
{ |
4783 |
NetSocketState *s = opaque; |
4784 |
int size;
|
4785 |
|
4786 |
size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
4787 |
if (size < 0) |
4788 |
return;
|
4789 |
if (size == 0) { |
4790 |
/* end of connection */
|
4791 |
qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
4792 |
return;
|
4793 |
} |
4794 |
qemu_send_packet(s->vc, s->buf, size); |
4795 |
} |
4796 |
|
4797 |
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
4798 |
{ |
4799 |
struct ip_mreq imr;
|
4800 |
int fd;
|
4801 |
int val, ret;
|
4802 |
if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
|
4803 |
fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
|
4804 |
inet_ntoa(mcastaddr->sin_addr), |
4805 |
(int)ntohl(mcastaddr->sin_addr.s_addr));
|
4806 |
return -1; |
4807 |
|
4808 |
} |
4809 |
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
4810 |
if (fd < 0) { |
4811 |
perror("socket(PF_INET, SOCK_DGRAM)");
|
4812 |
return -1; |
4813 |
} |
4814 |
|
4815 |
val = 1;
|
4816 |
ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
4817 |
(const char *)&val, sizeof(val)); |
4818 |
if (ret < 0) { |
4819 |
perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
|
4820 |
goto fail;
|
4821 |
} |
4822 |
|
4823 |
ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
4824 |
if (ret < 0) { |
4825 |
perror("bind");
|
4826 |
goto fail;
|
4827 |
} |
4828 |
|
4829 |
/* Add host to multicast group */
|
4830 |
imr.imr_multiaddr = mcastaddr->sin_addr; |
4831 |
imr.imr_interface.s_addr = htonl(INADDR_ANY); |
4832 |
|
4833 |
ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
4834 |
(const char *)&imr, sizeof(struct ip_mreq)); |
4835 |
if (ret < 0) { |
4836 |
perror("setsockopt(IP_ADD_MEMBERSHIP)");
|
4837 |
goto fail;
|
4838 |
} |
4839 |
|
4840 |
/* Force mcast msgs to loopback (eg. several QEMUs in same host */
|
4841 |
val = 1;
|
4842 |
ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
4843 |
(const char *)&val, sizeof(val)); |
4844 |
if (ret < 0) { |
4845 |
perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
|
4846 |
goto fail;
|
4847 |
} |
4848 |
|
4849 |
socket_set_nonblock(fd); |
4850 |
return fd;
|
4851 |
fail:
|
4852 |
if (fd >= 0) |
4853 |
closesocket(fd); |
4854 |
return -1; |
4855 |
} |
4856 |
|
4857 |
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
4858 |
int is_connected)
|
4859 |
{ |
4860 |
struct sockaddr_in saddr;
|
4861 |
int newfd;
|
4862 |
socklen_t saddr_len; |
4863 |
NetSocketState *s; |
4864 |
|
4865 |
/* fd passed: multicast: "learn" dgram_dst address from bound address and save it
|
4866 |
* Because this may be "shared" socket from a "master" process, datagrams would be recv()
|
4867 |
* by ONLY ONE process: we must "clone" this dgram socket --jjo
|
4868 |
*/
|
4869 |
|
4870 |
if (is_connected) {
|
4871 |
if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
4872 |
/* must be bound */
|
4873 |
if (saddr.sin_addr.s_addr==0) { |
4874 |
fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
|
4875 |
fd); |
4876 |
return NULL; |
4877 |
} |
4878 |
/* clone dgram socket */
|
4879 |
newfd = net_socket_mcast_create(&saddr); |
4880 |
if (newfd < 0) { |
4881 |
/* error already reported by net_socket_mcast_create() */
|
4882 |
close(fd); |
4883 |
return NULL; |
4884 |
} |
4885 |
/* clone newfd to fd, close newfd */
|
4886 |
dup2(newfd, fd); |
4887 |
close(newfd); |
4888 |
|
4889 |
} else {
|
4890 |
fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
|
4891 |
fd, strerror(errno)); |
4892 |
return NULL; |
4893 |
} |
4894 |
} |
4895 |
|
4896 |
s = qemu_mallocz(sizeof(NetSocketState));
|
4897 |
if (!s)
|
4898 |
return NULL; |
4899 |
s->fd = fd; |
4900 |
|
4901 |
s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s);
|
4902 |
qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
|
4903 |
|
4904 |
/* mcast: save bound address as dst */
|
4905 |
if (is_connected) s->dgram_dst=saddr;
|
4906 |
|
4907 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4908 |
"socket: fd=%d (%s mcast=%s:%d)",
|
4909 |
fd, is_connected? "cloned" : "", |
4910 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4911 |
return s;
|
4912 |
} |
4913 |
|
4914 |
static void net_socket_connect(void *opaque) |
4915 |
{ |
4916 |
NetSocketState *s = opaque; |
4917 |
qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
|
4918 |
} |
4919 |
|
4920 |
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
4921 |
int is_connected)
|
4922 |
{ |
4923 |
NetSocketState *s; |
4924 |
s = qemu_mallocz(sizeof(NetSocketState));
|
4925 |
if (!s)
|
4926 |
return NULL; |
4927 |
s->fd = fd; |
4928 |
s->vc = qemu_new_vlan_client(vlan, |
4929 |
net_socket_receive, NULL, s);
|
4930 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
4931 |
"socket: fd=%d", fd);
|
4932 |
if (is_connected) {
|
4933 |
net_socket_connect(s); |
4934 |
} else {
|
4935 |
qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
|
4936 |
} |
4937 |
return s;
|
4938 |
} |
4939 |
|
4940 |
static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
4941 |
int is_connected)
|
4942 |
{ |
4943 |
int so_type=-1, optlen=sizeof(so_type); |
4944 |
|
4945 |
if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
4946 |
(socklen_t *)&optlen)< 0) {
|
4947 |
fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
|
4948 |
return NULL; |
4949 |
} |
4950 |
switch(so_type) {
|
4951 |
case SOCK_DGRAM:
|
4952 |
return net_socket_fd_init_dgram(vlan, fd, is_connected);
|
4953 |
case SOCK_STREAM:
|
4954 |
return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4955 |
default:
|
4956 |
/* who knows ... this could be a eg. a pty, do warn and continue as stream */
|
4957 |
fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
|
4958 |
return net_socket_fd_init_stream(vlan, fd, is_connected);
|
4959 |
} |
4960 |
return NULL; |
4961 |
} |
4962 |
|
4963 |
static void net_socket_accept(void *opaque) |
4964 |
{ |
4965 |
NetSocketListenState *s = opaque; |
4966 |
NetSocketState *s1; |
4967 |
struct sockaddr_in saddr;
|
4968 |
socklen_t len; |
4969 |
int fd;
|
4970 |
|
4971 |
for(;;) {
|
4972 |
len = sizeof(saddr);
|
4973 |
fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
|
4974 |
if (fd < 0 && errno != EINTR) { |
4975 |
return;
|
4976 |
} else if (fd >= 0) { |
4977 |
break;
|
4978 |
} |
4979 |
} |
4980 |
s1 = net_socket_fd_init(s->vlan, fd, 1);
|
4981 |
if (!s1) {
|
4982 |
closesocket(fd); |
4983 |
} else {
|
4984 |
snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
|
4985 |
"socket: connection from %s:%d",
|
4986 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
4987 |
} |
4988 |
} |
4989 |
|
4990 |
static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
4991 |
{ |
4992 |
NetSocketListenState *s; |
4993 |
int fd, val, ret;
|
4994 |
struct sockaddr_in saddr;
|
4995 |
|
4996 |
if (parse_host_port(&saddr, host_str) < 0) |
4997 |
return -1; |
4998 |
|
4999 |
s = qemu_mallocz(sizeof(NetSocketListenState));
|
5000 |
if (!s)
|
5001 |
return -1; |
5002 |
|
5003 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
5004 |
if (fd < 0) { |
5005 |
perror("socket");
|
5006 |
return -1; |
5007 |
} |
5008 |
socket_set_nonblock(fd); |
5009 |
|
5010 |
/* allow fast reuse */
|
5011 |
val = 1;
|
5012 |
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
5013 |
|
5014 |
ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
5015 |
if (ret < 0) { |
5016 |
perror("bind");
|
5017 |
return -1; |
5018 |
} |
5019 |
ret = listen(fd, 0);
|
5020 |
if (ret < 0) { |
5021 |
perror("listen");
|
5022 |
return -1; |
5023 |
} |
5024 |
s->vlan = vlan; |
5025 |
s->fd = fd; |
5026 |
qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
|
5027 |
return 0; |
5028 |
} |
5029 |
|
5030 |
static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
5031 |
{ |
5032 |
NetSocketState *s; |
5033 |
int fd, connected, ret, err;
|
5034 |
struct sockaddr_in saddr;
|
5035 |
|
5036 |
if (parse_host_port(&saddr, host_str) < 0) |
5037 |
return -1; |
5038 |
|
5039 |
fd = socket(PF_INET, SOCK_STREAM, 0);
|
5040 |
if (fd < 0) { |
5041 |
perror("socket");
|
5042 |
return -1; |
5043 |
} |
5044 |
socket_set_nonblock(fd); |
5045 |
|
5046 |
connected = 0;
|
5047 |
for(;;) {
|
5048 |
ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
5049 |
if (ret < 0) { |
5050 |
err = socket_error(); |
5051 |
if (err == EINTR || err == EWOULDBLOCK) {
|
5052 |
} else if (err == EINPROGRESS) { |
5053 |
break;
|
5054 |
#ifdef _WIN32
|
5055 |
} else if (err == WSAEALREADY) { |
5056 |
break;
|
5057 |
#endif
|
5058 |
} else {
|
5059 |
perror("connect");
|
5060 |
closesocket(fd); |
5061 |
return -1; |
5062 |
} |
5063 |
} else {
|
5064 |
connected = 1;
|
5065 |
break;
|
5066 |
} |
5067 |
} |
5068 |
s = net_socket_fd_init(vlan, fd, connected); |
5069 |
if (!s)
|
5070 |
return -1; |
5071 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5072 |
"socket: connect to %s:%d",
|
5073 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5074 |
return 0; |
5075 |
} |
5076 |
|
5077 |
static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
5078 |
{ |
5079 |
NetSocketState *s; |
5080 |
int fd;
|
5081 |
struct sockaddr_in saddr;
|
5082 |
|
5083 |
if (parse_host_port(&saddr, host_str) < 0) |
5084 |
return -1; |
5085 |
|
5086 |
|
5087 |
fd = net_socket_mcast_create(&saddr); |
5088 |
if (fd < 0) |
5089 |
return -1; |
5090 |
|
5091 |
s = net_socket_fd_init(vlan, fd, 0);
|
5092 |
if (!s)
|
5093 |
return -1; |
5094 |
|
5095 |
s->dgram_dst = saddr; |
5096 |
|
5097 |
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
|
5098 |
"socket: mcast=%s:%d",
|
5099 |
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
5100 |
return 0; |
5101 |
|
5102 |
} |
5103 |
|
5104 |
static const char *get_opt_name(char *buf, int buf_size, const char *p) |
5105 |
{ |
5106 |
char *q;
|
5107 |
|
5108 |
q = buf; |
5109 |
while (*p != '\0' && *p != '=') { |
5110 |
if (q && (q - buf) < buf_size - 1) |
5111 |
*q++ = *p; |
5112 |
p++; |
5113 |
} |
5114 |
if (q)
|
5115 |
*q = '\0';
|
5116 |
|
5117 |
return p;
|
5118 |
} |
5119 |
|
5120 |
static const char *get_opt_value(char *buf, int buf_size, const char *p) |
5121 |
{ |
5122 |
char *q;
|
5123 |
|
5124 |
q = buf; |
5125 |
while (*p != '\0') { |
5126 |
if (*p == ',') { |
5127 |
if (*(p + 1) != ',') |
5128 |
break;
|
5129 |
p++; |
5130 |
} |
5131 |
if (q && (q - buf) < buf_size - 1) |
5132 |
*q++ = *p; |
5133 |
p++; |
5134 |
} |
5135 |
if (q)
|
5136 |
*q = '\0';
|
5137 |
|
5138 |
return p;
|
5139 |
} |
5140 |
|
5141 |
static int get_param_value(char *buf, int buf_size, |
5142 |
const char *tag, const char *str) |
5143 |
{ |
5144 |
const char *p; |
5145 |
char option[128]; |
5146 |
|
5147 |
p = str; |
5148 |
for(;;) {
|
5149 |
p = get_opt_name(option, sizeof(option), p);
|
5150 |
if (*p != '=') |
5151 |
break;
|
5152 |
p++; |
5153 |
if (!strcmp(tag, option)) {
|
5154 |
(void)get_opt_value(buf, buf_size, p);
|
5155 |
return strlen(buf);
|
5156 |
} else {
|
5157 |
p = get_opt_value(NULL, 0, p); |
5158 |
} |
5159 |
if (*p != ',') |
5160 |
break;
|
5161 |
p++; |
5162 |
} |
5163 |
return 0; |
5164 |
} |
5165 |
|
5166 |
static int check_params(char *buf, int buf_size, |
5167 |
char **params, const char *str) |
5168 |
{ |
5169 |
const char *p; |
5170 |
int i;
|
5171 |
|
5172 |
p = str; |
5173 |
for(;;) {
|
5174 |
p = get_opt_name(buf, buf_size, p); |
5175 |
if (*p != '=') |
5176 |
return -1; |
5177 |
p++; |
5178 |
for(i = 0; params[i] != NULL; i++) |
5179 |
if (!strcmp(params[i], buf))
|
5180 |
break;
|
5181 |
if (params[i] == NULL) |
5182 |
return -1; |
5183 |
p = get_opt_value(NULL, 0, p); |
5184 |
if (*p != ',') |
5185 |
break;
|
5186 |
p++; |
5187 |
} |
5188 |
return 0; |
5189 |
} |
5190 |
|
5191 |
static int net_client_init(const char *device, const char *p) |
5192 |
{ |
5193 |
char buf[1024]; |
5194 |
int vlan_id, ret;
|
5195 |
VLANState *vlan; |
5196 |
|
5197 |
vlan_id = 0;
|
5198 |
if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
5199 |
vlan_id = strtol(buf, NULL, 0); |
5200 |
} |
5201 |
vlan = qemu_find_vlan(vlan_id); |
5202 |
if (!vlan) {
|
5203 |
fprintf(stderr, "Could not create vlan %d\n", vlan_id);
|
5204 |
return -1; |
5205 |
} |
5206 |
if (!strcmp(device, "nic")) { |
5207 |
NICInfo *nd; |
5208 |
uint8_t *macaddr; |
5209 |
|
5210 |
if (nb_nics >= MAX_NICS) {
|
5211 |
fprintf(stderr, "Too Many NICs\n");
|
5212 |
return -1; |
5213 |
} |
5214 |
nd = &nd_table[nb_nics]; |
5215 |
macaddr = nd->macaddr; |
5216 |
macaddr[0] = 0x52; |
5217 |
macaddr[1] = 0x54; |
5218 |
macaddr[2] = 0x00; |
5219 |
macaddr[3] = 0x12; |
5220 |
macaddr[4] = 0x34; |
5221 |
macaddr[5] = 0x56 + nb_nics; |
5222 |
|
5223 |
if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
5224 |
if (parse_macaddr(macaddr, buf) < 0) { |
5225 |
fprintf(stderr, "invalid syntax for ethernet address\n");
|
5226 |
return -1; |
5227 |
} |
5228 |
} |
5229 |
if (get_param_value(buf, sizeof(buf), "model", p)) { |
5230 |
nd->model = strdup(buf); |
5231 |
} |
5232 |
nd->vlan = vlan; |
5233 |
nb_nics++; |
5234 |
vlan->nb_guest_devs++; |
5235 |
ret = 0;
|
5236 |
} else
|
5237 |
if (!strcmp(device, "none")) { |
5238 |
/* does nothing. It is needed to signal that no network cards
|
5239 |
are wanted */
|
5240 |
ret = 0;
|
5241 |
} else
|
5242 |
#ifdef CONFIG_SLIRP
|
5243 |
if (!strcmp(device, "user")) { |
5244 |
if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
5245 |
pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
|
5246 |
} |
5247 |
vlan->nb_host_devs++; |
5248 |
ret = net_slirp_init(vlan); |
5249 |
} else
|
5250 |
#endif
|
5251 |
#ifdef _WIN32
|
5252 |
if (!strcmp(device, "tap")) { |
5253 |
char ifname[64]; |
5254 |
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5255 |
fprintf(stderr, "tap: no interface name\n");
|
5256 |
return -1; |
5257 |
} |
5258 |
vlan->nb_host_devs++; |
5259 |
ret = tap_win32_init(vlan, ifname); |
5260 |
} else
|
5261 |
#else
|
5262 |
if (!strcmp(device, "tap")) { |
5263 |
char ifname[64]; |
5264 |
char setup_script[1024], down_script[1024]; |
5265 |
int fd;
|
5266 |
vlan->nb_host_devs++; |
5267 |
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5268 |
fd = strtol(buf, NULL, 0); |
5269 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
5270 |
ret = -1;
|
5271 |
if (net_tap_fd_init(vlan, fd))
|
5272 |
ret = 0;
|
5273 |
} else {
|
5274 |
if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
5275 |
ifname[0] = '\0'; |
5276 |
} |
5277 |
if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
5278 |
pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
|
5279 |
} |
5280 |
if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
5281 |
pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
|
5282 |
} |
5283 |
ret = net_tap_init(vlan, ifname, setup_script, down_script); |
5284 |
} |
5285 |
} else
|
5286 |
#endif
|
5287 |
if (!strcmp(device, "socket")) { |
5288 |
if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
5289 |
int fd;
|
5290 |
fd = strtol(buf, NULL, 0); |
5291 |
ret = -1;
|
5292 |
if (net_socket_fd_init(vlan, fd, 1)) |
5293 |
ret = 0;
|
5294 |
} else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
5295 |
ret = net_socket_listen_init(vlan, buf); |
5296 |
} else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
5297 |
ret = net_socket_connect_init(vlan, buf); |
5298 |
} else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
5299 |
ret = net_socket_mcast_init(vlan, buf); |
5300 |
} else {
|
5301 |
fprintf(stderr, "Unknown socket options: %s\n", p);
|
5302 |
return -1; |
5303 |
} |
5304 |
vlan->nb_host_devs++; |
5305 |
} else
|
5306 |
#ifdef CONFIG_VDE
|
5307 |
if (!strcmp(device, "vde")) { |
5308 |
char vde_sock[1024], vde_group[512]; |
5309 |
int vde_port, vde_mode;
|
5310 |
vlan->nb_host_devs++; |
5311 |
if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { |
5312 |
vde_sock[0] = '\0'; |
5313 |
} |
5314 |
if (get_param_value(buf, sizeof(buf), "port", p) > 0) { |
5315 |
vde_port = strtol(buf, NULL, 10); |
5316 |
} else {
|
5317 |
vde_port = 0;
|
5318 |
} |
5319 |
if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) { |
5320 |
vde_group[0] = '\0'; |
5321 |
} |
5322 |
if (get_param_value(buf, sizeof(buf), "mode", p) > 0) { |
5323 |
vde_mode = strtol(buf, NULL, 8); |
5324 |
} else {
|
5325 |
vde_mode = 0700;
|
5326 |
} |
5327 |
ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode); |
5328 |
} else
|
5329 |
#endif
|
5330 |
{ |
5331 |
fprintf(stderr, "Unknown network device: %s\n", device);
|
5332 |
return -1; |
5333 |
} |
5334 |
if (ret < 0) { |
5335 |
fprintf(stderr, "Could not initialize device '%s'\n", device);
|
5336 |
} |
5337 |
|
5338 |
return ret;
|
5339 |
} |
5340 |
|
5341 |
static int net_client_parse(const char *str) |
5342 |
{ |
5343 |
const char *p; |
5344 |
char *q;
|
5345 |
char device[64]; |
5346 |
|
5347 |
p = str; |
5348 |
q = device; |
5349 |
while (*p != '\0' && *p != ',') { |
5350 |
if ((q - device) < sizeof(device) - 1) |
5351 |
*q++ = *p; |
5352 |
p++; |
5353 |
} |
5354 |
*q = '\0';
|
5355 |
if (*p == ',') |
5356 |
p++; |
5357 |
|
5358 |
return net_client_init(device, p);
|
5359 |
} |
5360 |
|
5361 |
void do_info_network(void) |
5362 |
{ |
5363 |
VLANState *vlan; |
5364 |
VLANClientState *vc; |
5365 |
|
5366 |
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
5367 |
term_printf("VLAN %d devices:\n", vlan->id);
|
5368 |
for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
5369 |
term_printf(" %s\n", vc->info_str);
|
5370 |
} |
5371 |
} |
5372 |
|
5373 |
#define HD_ALIAS "index=%d,media=disk" |
5374 |
#ifdef TARGET_PPC
|
5375 |
#define CDROM_ALIAS "index=1,media=cdrom" |
5376 |
#else
|
5377 |
#define CDROM_ALIAS "index=2,media=cdrom" |
5378 |
#endif
|
5379 |
#define FD_ALIAS "index=%d,if=floppy" |
5380 |
#define PFLASH_ALIAS "if=pflash" |
5381 |
#define MTD_ALIAS "if=mtd" |
5382 |
#define SD_ALIAS "index=0,if=sd" |
5383 |
|
5384 |
static int drive_add(const char *file, const char *fmt, ...) |
5385 |
{ |
5386 |
va_list ap; |
5387 |
|
5388 |
if (nb_drives_opt >= MAX_DRIVES) {
|
5389 |
fprintf(stderr, "qemu: too many drives\n");
|
5390 |
exit(1);
|
5391 |
} |
5392 |
|
5393 |
drives_opt[nb_drives_opt].file = file; |
5394 |
va_start(ap, fmt); |
5395 |
vsnprintf(drives_opt[nb_drives_opt].opt, |
5396 |
sizeof(drives_opt[0].opt), fmt, ap); |
5397 |
va_end(ap); |
5398 |
|
5399 |
return nb_drives_opt++;
|
5400 |
} |
5401 |
|
5402 |
int drive_get_index(BlockInterfaceType type, int bus, int unit) |
5403 |
{ |
5404 |
int index;
|
5405 |
|
5406 |
/* seek interface, bus and unit */
|
5407 |
|
5408 |
for (index = 0; index < nb_drives; index++) |
5409 |
if (drives_table[index].type == type &&
|
5410 |
drives_table[index].bus == bus && |
5411 |
drives_table[index].unit == unit) |
5412 |
return index;
|
5413 |
|
5414 |
return -1; |
5415 |
} |
5416 |
|
5417 |
int drive_get_max_bus(BlockInterfaceType type)
|
5418 |
{ |
5419 |
int max_bus;
|
5420 |
int index;
|
5421 |
|
5422 |
max_bus = -1;
|
5423 |
for (index = 0; index < nb_drives; index++) { |
5424 |
if(drives_table[index].type == type &&
|
5425 |
drives_table[index].bus > max_bus) |
5426 |
max_bus = drives_table[index].bus; |
5427 |
} |
5428 |
return max_bus;
|
5429 |
} |
5430 |
|
5431 |
static void bdrv_format_print(void *opaque, const char *name) |
5432 |
{ |
5433 |
fprintf(stderr, " %s", name);
|
5434 |
} |
5435 |
|
5436 |
static int drive_init(struct drive_opt *arg, int snapshot, |
5437 |
QEMUMachine *machine) |
5438 |
{ |
5439 |
char buf[128]; |
5440 |
char file[1024]; |
5441 |
char devname[128]; |
5442 |
const char *mediastr = ""; |
5443 |
BlockInterfaceType type; |
5444 |
enum { MEDIA_DISK, MEDIA_CDROM } media;
|
5445 |
int bus_id, unit_id;
|
5446 |
int cyls, heads, secs, translation;
|
5447 |
BlockDriverState *bdrv; |
5448 |
BlockDriver *drv = NULL;
|
5449 |
int max_devs;
|
5450 |
int index;
|
5451 |
int cache;
|
5452 |
int bdrv_flags;
|
5453 |
char *str = arg->opt;
|
5454 |
char *params[] = { "bus", "unit", "if", "index", "cyls", "heads", |
5455 |
"secs", "trans", "media", "snapshot", "file", |
5456 |
"cache", "format", NULL }; |
5457 |
|
5458 |
if (check_params(buf, sizeof(buf), params, str) < 0) { |
5459 |
fprintf(stderr, "qemu: unknown parameter '%s' in '%s'\n",
|
5460 |
buf, str); |
5461 |
return -1; |
5462 |
} |
5463 |
|
5464 |
file[0] = 0; |
5465 |
cyls = heads = secs = 0;
|
5466 |
bus_id = 0;
|
5467 |
unit_id = -1;
|
5468 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
5469 |
index = -1;
|
5470 |
cache = 1;
|
5471 |
|
5472 |
if (!strcmp(machine->name, "realview") || |
5473 |
!strcmp(machine->name, "SS-5") ||
|
5474 |
!strcmp(machine->name, "SS-10") ||
|
5475 |
!strcmp(machine->name, "SS-600MP") ||
|
5476 |
!strcmp(machine->name, "versatilepb") ||
|
5477 |
!strcmp(machine->name, "versatileab")) {
|
5478 |
type = IF_SCSI; |
5479 |
max_devs = MAX_SCSI_DEVS; |
5480 |
pstrcpy(devname, sizeof(devname), "scsi"); |
5481 |
} else {
|
5482 |
type = IF_IDE; |
5483 |
max_devs = MAX_IDE_DEVS; |
5484 |
pstrcpy(devname, sizeof(devname), "ide"); |
5485 |
} |
5486 |
media = MEDIA_DISK; |
5487 |
|
5488 |
/* extract parameters */
|
5489 |
|
5490 |
if (get_param_value(buf, sizeof(buf), "bus", str)) { |
5491 |
bus_id = strtol(buf, NULL, 0); |
5492 |
if (bus_id < 0) { |
5493 |
fprintf(stderr, "qemu: '%s' invalid bus id\n", str);
|
5494 |
return -1; |
5495 |
} |
5496 |
} |
5497 |
|
5498 |
if (get_param_value(buf, sizeof(buf), "unit", str)) { |
5499 |
unit_id = strtol(buf, NULL, 0); |
5500 |
if (unit_id < 0) { |
5501 |
fprintf(stderr, "qemu: '%s' invalid unit id\n", str);
|
5502 |
return -1; |
5503 |
} |
5504 |
} |
5505 |
|
5506 |
if (get_param_value(buf, sizeof(buf), "if", str)) { |
5507 |
pstrcpy(devname, sizeof(devname), buf);
|
5508 |
if (!strcmp(buf, "ide")) { |
5509 |
type = IF_IDE; |
5510 |
max_devs = MAX_IDE_DEVS; |
5511 |
} else if (!strcmp(buf, "scsi")) { |
5512 |
type = IF_SCSI; |
5513 |
max_devs = MAX_SCSI_DEVS; |
5514 |
} else if (!strcmp(buf, "floppy")) { |
5515 |
type = IF_FLOPPY; |
5516 |
max_devs = 0;
|
5517 |
} else if (!strcmp(buf, "pflash")) { |
5518 |
type = IF_PFLASH; |
5519 |
max_devs = 0;
|
5520 |
} else if (!strcmp(buf, "mtd")) { |
5521 |
type = IF_MTD; |
5522 |
max_devs = 0;
|
5523 |
} else if (!strcmp(buf, "sd")) { |
5524 |
type = IF_SD; |
5525 |
max_devs = 0;
|
5526 |
} else {
|
5527 |
fprintf(stderr, "qemu: '%s' unsupported bus type '%s'\n", str, buf);
|
5528 |
return -1; |
5529 |
} |
5530 |
} |
5531 |
|
5532 |
if (get_param_value(buf, sizeof(buf), "index", str)) { |
5533 |
index = strtol(buf, NULL, 0); |
5534 |
if (index < 0) { |
5535 |
fprintf(stderr, "qemu: '%s' invalid index\n", str);
|
5536 |
return -1; |
5537 |
} |
5538 |
} |
5539 |
|
5540 |
if (get_param_value(buf, sizeof(buf), "cyls", str)) { |
5541 |
cyls = strtol(buf, NULL, 0); |
5542 |
} |
5543 |
|
5544 |
if (get_param_value(buf, sizeof(buf), "heads", str)) { |
5545 |
heads = strtol(buf, NULL, 0); |
5546 |
} |
5547 |
|
5548 |
if (get_param_value(buf, sizeof(buf), "secs", str)) { |
5549 |
secs = strtol(buf, NULL, 0); |
5550 |
} |
5551 |
|
5552 |
if (cyls || heads || secs) {
|
5553 |
if (cyls < 1 || cyls > 16383) { |
5554 |
fprintf(stderr, "qemu: '%s' invalid physical cyls number\n", str);
|
5555 |
return -1; |
5556 |
} |
5557 |
if (heads < 1 || heads > 16) { |
5558 |
fprintf(stderr, "qemu: '%s' invalid physical heads number\n", str);
|
5559 |
return -1; |
5560 |
} |
5561 |
if (secs < 1 || secs > 63) { |
5562 |
fprintf(stderr, "qemu: '%s' invalid physical secs number\n", str);
|
5563 |
return -1; |
5564 |
} |
5565 |
} |
5566 |
|
5567 |
if (get_param_value(buf, sizeof(buf), "trans", str)) { |
5568 |
if (!cyls) {
|
5569 |
fprintf(stderr, |
5570 |
"qemu: '%s' trans must be used with cyls,heads and secs\n",
|
5571 |
str); |
5572 |
return -1; |
5573 |
} |
5574 |
if (!strcmp(buf, "none")) |
5575 |
translation = BIOS_ATA_TRANSLATION_NONE; |
5576 |
else if (!strcmp(buf, "lba")) |
5577 |
translation = BIOS_ATA_TRANSLATION_LBA; |
5578 |
else if (!strcmp(buf, "auto")) |
5579 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
5580 |
else {
|
5581 |
fprintf(stderr, "qemu: '%s' invalid translation type\n", str);
|
5582 |
return -1; |
5583 |
} |
5584 |
} |
5585 |
|
5586 |
if (get_param_value(buf, sizeof(buf), "media", str)) { |
5587 |
if (!strcmp(buf, "disk")) { |
5588 |
media = MEDIA_DISK; |
5589 |
} else if (!strcmp(buf, "cdrom")) { |
5590 |
if (cyls || secs || heads) {
|
5591 |
fprintf(stderr, |
5592 |
"qemu: '%s' invalid physical CHS format\n", str);
|
5593 |
return -1; |
5594 |
} |
5595 |
media = MEDIA_CDROM; |
5596 |
} else {
|
5597 |
fprintf(stderr, "qemu: '%s' invalid media\n", str);
|
5598 |
return -1; |
5599 |
} |
5600 |
} |
5601 |
|
5602 |
if (get_param_value(buf, sizeof(buf), "snapshot", str)) { |
5603 |
if (!strcmp(buf, "on")) |
5604 |
snapshot = 1;
|
5605 |
else if (!strcmp(buf, "off")) |
5606 |
snapshot = 0;
|
5607 |
else {
|
5608 |
fprintf(stderr, "qemu: '%s' invalid snapshot option\n", str);
|
5609 |
return -1; |
5610 |
} |
5611 |
} |
5612 |
|
5613 |
if (get_param_value(buf, sizeof(buf), "cache", str)) { |
5614 |
if (!strcmp(buf, "off")) |
5615 |
cache = 0;
|
5616 |
else if (!strcmp(buf, "on")) |
5617 |
cache = 1;
|
5618 |
else {
|
5619 |
fprintf(stderr, "qemu: invalid cache option\n");
|
5620 |
return -1; |
5621 |
} |
5622 |
} |
5623 |
|
5624 |
if (get_param_value(buf, sizeof(buf), "format", str)) { |
5625 |
if (strcmp(buf, "?") == 0) { |
5626 |
fprintf(stderr, "qemu: Supported formats:");
|
5627 |
bdrv_iterate_format(bdrv_format_print, NULL);
|
5628 |
fprintf(stderr, "\n");
|
5629 |
return -1; |
5630 |
} |
5631 |
drv = bdrv_find_format(buf); |
5632 |
if (!drv) {
|
5633 |
fprintf(stderr, "qemu: '%s' invalid format\n", buf);
|
5634 |
return -1; |
5635 |
} |
5636 |
} |
5637 |
|
5638 |
if (arg->file == NULL) |
5639 |
get_param_value(file, sizeof(file), "file", str); |
5640 |
else
|
5641 |
pstrcpy(file, sizeof(file), arg->file);
|
5642 |
|
5643 |
/* compute bus and unit according index */
|
5644 |
|
5645 |
if (index != -1) { |
5646 |
if (bus_id != 0 || unit_id != -1) { |
5647 |
fprintf(stderr, |
5648 |
"qemu: '%s' index cannot be used with bus and unit\n", str);
|
5649 |
return -1; |
5650 |
} |
5651 |
if (max_devs == 0) |
5652 |
{ |
5653 |
unit_id = index; |
5654 |
bus_id = 0;
|
5655 |
} else {
|
5656 |
unit_id = index % max_devs; |
5657 |
bus_id = index / max_devs; |
5658 |
} |
5659 |
} |
5660 |
|
5661 |
/* if user doesn't specify a unit_id,
|
5662 |
* try to find the first free
|
5663 |
*/
|
5664 |
|
5665 |
if (unit_id == -1) { |
5666 |
unit_id = 0;
|
5667 |
while (drive_get_index(type, bus_id, unit_id) != -1) { |
5668 |
unit_id++; |
5669 |
if (max_devs && unit_id >= max_devs) {
|
5670 |
unit_id -= max_devs; |
5671 |
bus_id++; |
5672 |
} |
5673 |
} |
5674 |
} |
5675 |
|
5676 |
/* check unit id */
|
5677 |
|
5678 |
if (max_devs && unit_id >= max_devs) {
|
5679 |
fprintf(stderr, "qemu: '%s' unit %d too big (max is %d)\n",
|
5680 |
str, unit_id, max_devs - 1);
|
5681 |
return -1; |
5682 |
} |
5683 |
|
5684 |
/*
|
5685 |
* ignore multiple definitions
|
5686 |
*/
|
5687 |
|
5688 |
if (drive_get_index(type, bus_id, unit_id) != -1) |
5689 |
return 0; |
5690 |
|
5691 |
/* init */
|
5692 |
|
5693 |
if (type == IF_IDE || type == IF_SCSI)
|
5694 |
mediastr = (media == MEDIA_CDROM) ? "-cd" : "-hd"; |
5695 |
if (max_devs)
|
5696 |
snprintf(buf, sizeof(buf), "%s%i%s%i", |
5697 |
devname, bus_id, mediastr, unit_id); |
5698 |
else
|
5699 |
snprintf(buf, sizeof(buf), "%s%s%i", |
5700 |
devname, mediastr, unit_id); |
5701 |
bdrv = bdrv_new(buf); |
5702 |
drives_table[nb_drives].bdrv = bdrv; |
5703 |
drives_table[nb_drives].type = type; |
5704 |
drives_table[nb_drives].bus = bus_id; |
5705 |
drives_table[nb_drives].unit = unit_id; |
5706 |
nb_drives++; |
5707 |
|
5708 |
switch(type) {
|
5709 |
case IF_IDE:
|
5710 |
case IF_SCSI:
|
5711 |
switch(media) {
|
5712 |
case MEDIA_DISK:
|
5713 |
if (cyls != 0) { |
5714 |
bdrv_set_geometry_hint(bdrv, cyls, heads, secs); |
5715 |
bdrv_set_translation_hint(bdrv, translation); |
5716 |
} |
5717 |
break;
|
5718 |
case MEDIA_CDROM:
|
5719 |
bdrv_set_type_hint(bdrv, BDRV_TYPE_CDROM); |
5720 |
break;
|
5721 |
} |
5722 |
break;
|
5723 |
case IF_SD:
|
5724 |
/* FIXME: This isn't really a floppy, but it's a reasonable
|
5725 |
approximation. */
|
5726 |
case IF_FLOPPY:
|
5727 |
bdrv_set_type_hint(bdrv, BDRV_TYPE_FLOPPY); |
5728 |
break;
|
5729 |
case IF_PFLASH:
|
5730 |
case IF_MTD:
|
5731 |
break;
|
5732 |
} |
5733 |
if (!file[0]) |
5734 |
return 0; |
5735 |
bdrv_flags = 0;
|
5736 |
if (snapshot)
|
5737 |
bdrv_flags |= BDRV_O_SNAPSHOT; |
5738 |
if (!cache)
|
5739 |
bdrv_flags |= BDRV_O_DIRECT; |
5740 |
if (bdrv_open2(bdrv, file, bdrv_flags, drv) < 0 || qemu_key_check(bdrv, file)) { |
5741 |
fprintf(stderr, "qemu: could not open disk image %s\n",
|
5742 |
file); |
5743 |
return -1; |
5744 |
} |
5745 |
return 0; |
5746 |
} |
5747 |
|
5748 |
/***********************************************************/
|
5749 |
/* USB devices */
|
5750 |
|
5751 |
static USBPort *used_usb_ports;
|
5752 |
static USBPort *free_usb_ports;
|
5753 |
|
5754 |
/* ??? Maybe change this to register a hub to keep track of the topology. */
|
5755 |
void qemu_register_usb_port(USBPort *port, void *opaque, int index, |
5756 |
usb_attachfn attach) |
5757 |
{ |
5758 |
port->opaque = opaque; |
5759 |
port->index = index; |
5760 |
port->attach = attach; |
5761 |
port->next = free_usb_ports; |
5762 |
free_usb_ports = port; |
5763 |
} |
5764 |
|
5765 |
int usb_device_add_dev(USBDevice *dev)
|
5766 |
{ |
5767 |
USBPort *port; |
5768 |
|
5769 |
/* Find a USB port to add the device to. */
|
5770 |
port = free_usb_ports; |
5771 |
if (!port->next) {
|
5772 |
USBDevice *hub; |
5773 |
|
5774 |
/* Create a new hub and chain it on. */
|
5775 |
free_usb_ports = NULL;
|
5776 |
port->next = used_usb_ports; |
5777 |
used_usb_ports = port; |
5778 |
|
5779 |
hub = usb_hub_init(VM_USB_HUB_SIZE); |
5780 |
usb_attach(port, hub); |
5781 |
port = free_usb_ports; |
5782 |
} |
5783 |
|
5784 |
free_usb_ports = port->next; |
5785 |
port->next = used_usb_ports; |
5786 |
used_usb_ports = port; |
5787 |
usb_attach(port, dev); |
5788 |
return 0; |
5789 |
} |
5790 |
|
5791 |
static int usb_device_add(const char *devname) |
5792 |
{ |
5793 |
const char *p; |
5794 |
USBDevice *dev; |
5795 |
USBPort *port; |
5796 |
|
5797 |
if (!free_usb_ports)
|
5798 |
return -1; |
5799 |
|
5800 |
if (strstart(devname, "host:", &p)) { |
5801 |
dev = usb_host_device_open(p); |
5802 |
} else if (!strcmp(devname, "mouse")) { |
5803 |
dev = usb_mouse_init(); |
5804 |
} else if (!strcmp(devname, "tablet")) { |
5805 |
dev = usb_tablet_init(); |
5806 |
} else if (!strcmp(devname, "keyboard")) { |
5807 |
dev = usb_keyboard_init(); |
5808 |
} else if (strstart(devname, "disk:", &p)) { |
5809 |
dev = usb_msd_init(p); |
5810 |
} else if (!strcmp(devname, "wacom-tablet")) { |
5811 |
dev = usb_wacom_init(); |
5812 |
} else if (strstart(devname, "serial:", &p)) { |
5813 |
dev = usb_serial_init(p); |
5814 |
#ifdef CONFIG_BRLAPI
|
5815 |
} else if (!strcmp(devname, "braille")) { |
5816 |
dev = usb_baum_init(); |
5817 |
#endif
|
5818 |
} else if (strstart(devname, "net:", &p)) { |
5819 |
int nic = nb_nics;
|
5820 |
|
5821 |
if (net_client_init("nic", p) < 0) |
5822 |
return -1; |
5823 |
nd_table[nic].model = "usb";
|
5824 |
dev = usb_net_init(&nd_table[nic]); |
5825 |
} else {
|
5826 |
return -1; |
5827 |
} |
5828 |
if (!dev)
|
5829 |
return -1; |
5830 |
|
5831 |
return usb_device_add_dev(dev);
|
5832 |
} |
5833 |
|
5834 |
int usb_device_del_addr(int bus_num, int addr) |
5835 |
{ |
5836 |
USBPort *port; |
5837 |
USBPort **lastp; |
5838 |
USBDevice *dev; |
5839 |
|
5840 |
if (!used_usb_ports)
|
5841 |
return -1; |
5842 |
|
5843 |
if (bus_num != 0) |
5844 |
return -1; |
5845 |
|
5846 |
lastp = &used_usb_ports; |
5847 |
port = used_usb_ports; |
5848 |
while (port && port->dev->addr != addr) {
|
5849 |
lastp = &port->next; |
5850 |
port = port->next; |
5851 |
} |
5852 |
|
5853 |
if (!port)
|
5854 |
return -1; |
5855 |
|
5856 |
dev = port->dev; |
5857 |
*lastp = port->next; |
5858 |
usb_attach(port, NULL);
|
5859 |
dev->handle_destroy(dev); |
5860 |
port->next = free_usb_ports; |
5861 |
free_usb_ports = port; |
5862 |
return 0; |
5863 |
} |
5864 |
|
5865 |
static int usb_device_del(const char *devname) |
5866 |
{ |
5867 |
int bus_num, addr;
|
5868 |
const char *p; |
5869 |
|
5870 |
if (!used_usb_ports)
|
5871 |
return -1; |
5872 |
|
5873 |
p = strchr(devname, '.');
|
5874 |
if (!p)
|
5875 |
return -1; |
5876 |
bus_num = strtoul(devname, NULL, 0); |
5877 |
addr = strtoul(p + 1, NULL, 0); |
5878 |
|
5879 |
return usb_device_del_addr(bus_num, addr);
|
5880 |
} |
5881 |
|
5882 |
void do_usb_add(const char *devname) |
5883 |
{ |
5884 |
usb_device_add(devname); |
5885 |
} |
5886 |
|
5887 |
void do_usb_del(const char *devname) |
5888 |
{ |
5889 |
usb_device_del(devname); |
5890 |
} |
5891 |
|
5892 |
void usb_info(void) |
5893 |
{ |
5894 |
USBDevice *dev; |
5895 |
USBPort *port; |
5896 |
const char *speed_str; |
5897 |
|
5898 |
if (!usb_enabled) {
|
5899 |
term_printf("USB support not enabled\n");
|
5900 |
return;
|
5901 |
} |
5902 |
|
5903 |
for (port = used_usb_ports; port; port = port->next) {
|
5904 |
dev = port->dev; |
5905 |
if (!dev)
|
5906 |
continue;
|
5907 |
switch(dev->speed) {
|
5908 |
case USB_SPEED_LOW:
|
5909 |
speed_str = "1.5";
|
5910 |
break;
|
5911 |
case USB_SPEED_FULL:
|
5912 |
speed_str = "12";
|
5913 |
break;
|
5914 |
case USB_SPEED_HIGH:
|
5915 |
speed_str = "480";
|
5916 |
break;
|
5917 |
default:
|
5918 |
speed_str = "?";
|
5919 |
break;
|
5920 |
} |
5921 |
term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
|
5922 |
0, dev->addr, speed_str, dev->devname);
|
5923 |
} |
5924 |
} |
5925 |
|
5926 |
/***********************************************************/
|
5927 |
/* PCMCIA/Cardbus */
|
5928 |
|
5929 |
static struct pcmcia_socket_entry_s { |
5930 |
struct pcmcia_socket_s *socket;
|
5931 |
struct pcmcia_socket_entry_s *next;
|
5932 |
} *pcmcia_sockets = 0;
|
5933 |
|
5934 |
void pcmcia_socket_register(struct pcmcia_socket_s *socket) |
5935 |
{ |
5936 |
struct pcmcia_socket_entry_s *entry;
|
5937 |
|
5938 |
entry = qemu_malloc(sizeof(struct pcmcia_socket_entry_s)); |
5939 |
entry->socket = socket; |
5940 |
entry->next = pcmcia_sockets; |
5941 |
pcmcia_sockets = entry; |
5942 |
} |
5943 |
|
5944 |
void pcmcia_socket_unregister(struct pcmcia_socket_s *socket) |
5945 |
{ |
5946 |
struct pcmcia_socket_entry_s *entry, **ptr;
|
5947 |
|
5948 |
ptr = &pcmcia_sockets; |
5949 |
for (entry = *ptr; entry; ptr = &entry->next, entry = *ptr)
|
5950 |
if (entry->socket == socket) {
|
5951 |
*ptr = entry->next; |
5952 |
qemu_free(entry); |
5953 |
} |
5954 |
} |
5955 |
|
5956 |
void pcmcia_info(void) |
5957 |
{ |
5958 |
struct pcmcia_socket_entry_s *iter;
|
5959 |
if (!pcmcia_sockets)
|
5960 |
term_printf("No PCMCIA sockets\n");
|
5961 |
|
5962 |
for (iter = pcmcia_sockets; iter; iter = iter->next)
|
5963 |
term_printf("%s: %s\n", iter->socket->slot_string,
|
5964 |
iter->socket->attached ? iter->socket->card_string : |
5965 |
"Empty");
|
5966 |
} |
5967 |
|
5968 |
/***********************************************************/
|
5969 |
/* dumb display */
|
5970 |
|
5971 |
static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
5972 |
{ |
5973 |
} |
5974 |
|
5975 |
static void dumb_resize(DisplayState *ds, int w, int h) |
5976 |
{ |
5977 |
} |
5978 |
|
5979 |
static void dumb_refresh(DisplayState *ds) |
5980 |
{ |
5981 |
#if defined(CONFIG_SDL)
|
5982 |
vga_hw_update(); |
5983 |
#endif
|
5984 |
} |
5985 |
|
5986 |
static void dumb_display_init(DisplayState *ds) |
5987 |
{ |
5988 |
ds->data = NULL;
|
5989 |
ds->linesize = 0;
|
5990 |
ds->depth = 0;
|
5991 |
ds->dpy_update = dumb_update; |
5992 |
ds->dpy_resize = dumb_resize; |
5993 |
ds->dpy_refresh = dumb_refresh; |
5994 |
ds->gui_timer_interval = 500;
|
5995 |
ds->idle = 1;
|
5996 |
} |
5997 |
|
5998 |
/***********************************************************/
|
5999 |
/* I/O handling */
|
6000 |
|
6001 |
#define MAX_IO_HANDLERS 64 |
6002 |
|
6003 |
typedef struct IOHandlerRecord { |
6004 |
int fd;
|
6005 |
IOCanRWHandler *fd_read_poll; |
6006 |
IOHandler *fd_read; |
6007 |
IOHandler *fd_write; |
6008 |
int deleted;
|
6009 |
void *opaque;
|
6010 |
/* temporary data */
|
6011 |
struct pollfd *ufd;
|
6012 |
struct IOHandlerRecord *next;
|
6013 |
} IOHandlerRecord; |
6014 |
|
6015 |
static IOHandlerRecord *first_io_handler;
|
6016 |
|
6017 |
/* XXX: fd_read_poll should be suppressed, but an API change is
|
6018 |
necessary in the character devices to suppress fd_can_read(). */
|
6019 |
int qemu_set_fd_handler2(int fd, |
6020 |
IOCanRWHandler *fd_read_poll, |
6021 |
IOHandler *fd_read, |
6022 |
IOHandler *fd_write, |
6023 |
void *opaque)
|
6024 |
{ |
6025 |
IOHandlerRecord **pioh, *ioh; |
6026 |
|
6027 |
if (!fd_read && !fd_write) {
|
6028 |
pioh = &first_io_handler; |
6029 |
for(;;) {
|
6030 |
ioh = *pioh; |
6031 |
if (ioh == NULL) |
6032 |
break;
|
6033 |
if (ioh->fd == fd) {
|
6034 |
ioh->deleted = 1;
|
6035 |
break;
|
6036 |
} |
6037 |
pioh = &ioh->next; |
6038 |
} |
6039 |
} else {
|
6040 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
6041 |
if (ioh->fd == fd)
|
6042 |
goto found;
|
6043 |
} |
6044 |
ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
6045 |
if (!ioh)
|
6046 |
return -1; |
6047 |
ioh->next = first_io_handler; |
6048 |
first_io_handler = ioh; |
6049 |
found:
|
6050 |
ioh->fd = fd; |
6051 |
ioh->fd_read_poll = fd_read_poll; |
6052 |
ioh->fd_read = fd_read; |
6053 |
ioh->fd_write = fd_write; |
6054 |
ioh->opaque = opaque; |
6055 |
ioh->deleted = 0;
|
6056 |
} |
6057 |
return 0; |
6058 |
} |
6059 |
|
6060 |
int qemu_set_fd_handler(int fd, |
6061 |
IOHandler *fd_read, |
6062 |
IOHandler *fd_write, |
6063 |
void *opaque)
|
6064 |
{ |
6065 |
return qemu_set_fd_handler2(fd, NULL, fd_read, fd_write, opaque); |
6066 |
} |
6067 |
|
6068 |
/***********************************************************/
|
6069 |
/* Polling handling */
|
6070 |
|
6071 |
typedef struct PollingEntry { |
6072 |
PollingFunc *func; |
6073 |
void *opaque;
|
6074 |
struct PollingEntry *next;
|
6075 |
} PollingEntry; |
6076 |
|
6077 |
static PollingEntry *first_polling_entry;
|
6078 |
|
6079 |
int qemu_add_polling_cb(PollingFunc *func, void *opaque) |
6080 |
{ |
6081 |
PollingEntry **ppe, *pe; |
6082 |
pe = qemu_mallocz(sizeof(PollingEntry));
|
6083 |
if (!pe)
|
6084 |
return -1; |
6085 |
pe->func = func; |
6086 |
pe->opaque = opaque; |
6087 |
for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next); |
6088 |
*ppe = pe; |
6089 |
return 0; |
6090 |
} |
6091 |
|
6092 |
void qemu_del_polling_cb(PollingFunc *func, void *opaque) |
6093 |
{ |
6094 |
PollingEntry **ppe, *pe; |
6095 |
for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) { |
6096 |
pe = *ppe; |
6097 |
if (pe->func == func && pe->opaque == opaque) {
|
6098 |
*ppe = pe->next; |
6099 |
qemu_free(pe); |
6100 |
break;
|
6101 |
} |
6102 |
} |
6103 |
} |
6104 |
|
6105 |
#ifdef _WIN32
|
6106 |
/***********************************************************/
|
6107 |
/* Wait objects support */
|
6108 |
typedef struct WaitObjects { |
6109 |
int num;
|
6110 |
HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
6111 |
WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
|
6112 |
void *opaque[MAXIMUM_WAIT_OBJECTS + 1]; |
6113 |
} WaitObjects; |
6114 |
|
6115 |
static WaitObjects wait_objects = {0}; |
6116 |
|
6117 |
int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6118 |
{ |
6119 |
WaitObjects *w = &wait_objects; |
6120 |
|
6121 |
if (w->num >= MAXIMUM_WAIT_OBJECTS)
|
6122 |
return -1; |
6123 |
w->events[w->num] = handle; |
6124 |
w->func[w->num] = func; |
6125 |
w->opaque[w->num] = opaque; |
6126 |
w->num++; |
6127 |
return 0; |
6128 |
} |
6129 |
|
6130 |
void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque) |
6131 |
{ |
6132 |
int i, found;
|
6133 |
WaitObjects *w = &wait_objects; |
6134 |
|
6135 |
found = 0;
|
6136 |
for (i = 0; i < w->num; i++) { |
6137 |
if (w->events[i] == handle)
|
6138 |
found = 1;
|
6139 |
if (found) {
|
6140 |
w->events[i] = w->events[i + 1];
|
6141 |
w->func[i] = w->func[i + 1];
|
6142 |
w->opaque[i] = w->opaque[i + 1];
|
6143 |
} |
6144 |
} |
6145 |
if (found)
|
6146 |
w->num--; |
6147 |
} |
6148 |
#endif
|
6149 |
|
6150 |
/***********************************************************/
|
6151 |
/* savevm/loadvm support */
|
6152 |
|
6153 |
#define IO_BUF_SIZE 32768 |
6154 |
|
6155 |
struct QEMUFile {
|
6156 |
FILE *outfile; |
6157 |
BlockDriverState *bs; |
6158 |
int is_file;
|
6159 |
int is_writable;
|
6160 |
int64_t base_offset; |
6161 |
int64_t buf_offset; /* start of buffer when writing, end of buffer
|
6162 |
when reading */
|
6163 |
int buf_index;
|
6164 |
int buf_size; /* 0 when writing */ |
6165 |
uint8_t buf[IO_BUF_SIZE]; |
6166 |
}; |
6167 |
|
6168 |
QEMUFile *qemu_fopen(const char *filename, const char *mode) |
6169 |
{ |
6170 |
QEMUFile *f; |
6171 |
|
6172 |
f = qemu_mallocz(sizeof(QEMUFile));
|
6173 |
if (!f)
|
6174 |
return NULL; |
6175 |
if (!strcmp(mode, "wb")) { |
6176 |
f->is_writable = 1;
|
6177 |
} else if (!strcmp(mode, "rb")) { |
6178 |
f->is_writable = 0;
|
6179 |
} else {
|
6180 |
goto fail;
|
6181 |
} |
6182 |
f->outfile = fopen(filename, mode); |
6183 |
if (!f->outfile)
|
6184 |
goto fail;
|
6185 |
f->is_file = 1;
|
6186 |
return f;
|
6187 |
fail:
|
6188 |
if (f->outfile)
|
6189 |
fclose(f->outfile); |
6190 |
qemu_free(f); |
6191 |
return NULL; |
6192 |
} |
6193 |
|
6194 |
static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable) |
6195 |
{ |
6196 |
QEMUFile *f; |
6197 |
|
6198 |
f = qemu_mallocz(sizeof(QEMUFile));
|
6199 |
if (!f)
|
6200 |
return NULL; |
6201 |
f->is_file = 0;
|
6202 |
f->bs = bs; |
6203 |
f->is_writable = is_writable; |
6204 |
f->base_offset = offset; |
6205 |
return f;
|
6206 |
} |
6207 |
|
6208 |
void qemu_fflush(QEMUFile *f)
|
6209 |
{ |
6210 |
if (!f->is_writable)
|
6211 |
return;
|
6212 |
if (f->buf_index > 0) { |
6213 |
if (f->is_file) {
|
6214 |
fseek(f->outfile, f->buf_offset, SEEK_SET); |
6215 |
fwrite(f->buf, 1, f->buf_index, f->outfile);
|
6216 |
} else {
|
6217 |
bdrv_pwrite(f->bs, f->base_offset + f->buf_offset, |
6218 |
f->buf, f->buf_index); |
6219 |
} |
6220 |
f->buf_offset += f->buf_index; |
6221 |
f->buf_index = 0;
|
6222 |
} |
6223 |
} |
6224 |
|
6225 |
static void qemu_fill_buffer(QEMUFile *f) |
6226 |
{ |
6227 |
int len;
|
6228 |
|
6229 |
if (f->is_writable)
|
6230 |
return;
|
6231 |
if (f->is_file) {
|
6232 |
fseek(f->outfile, f->buf_offset, SEEK_SET); |
6233 |
len = fread(f->buf, 1, IO_BUF_SIZE, f->outfile);
|
6234 |
if (len < 0) |
6235 |
len = 0;
|
6236 |
} else {
|
6237 |
len = bdrv_pread(f->bs, f->base_offset + f->buf_offset, |
6238 |
f->buf, IO_BUF_SIZE); |
6239 |
if (len < 0) |
6240 |
len = 0;
|
6241 |
} |
6242 |
f->buf_index = 0;
|
6243 |
f->buf_size = len; |
6244 |
f->buf_offset += len; |
6245 |
} |
6246 |
|
6247 |
void qemu_fclose(QEMUFile *f)
|
6248 |
{ |
6249 |
if (f->is_writable)
|
6250 |
qemu_fflush(f); |
6251 |
if (f->is_file) {
|
6252 |
fclose(f->outfile); |
6253 |
} |
6254 |
qemu_free(f); |
6255 |
} |
6256 |
|
6257 |
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
6258 |
{ |
6259 |
int l;
|
6260 |
while (size > 0) { |
6261 |
l = IO_BUF_SIZE - f->buf_index; |
6262 |
if (l > size)
|
6263 |
l = size; |
6264 |
memcpy(f->buf + f->buf_index, buf, l); |
6265 |
f->buf_index += l; |
6266 |
buf += l; |
6267 |
size -= l; |
6268 |
if (f->buf_index >= IO_BUF_SIZE)
|
6269 |
qemu_fflush(f); |
6270 |
} |
6271 |
} |
6272 |
|
6273 |
void qemu_put_byte(QEMUFile *f, int v) |
6274 |
{ |
6275 |
f->buf[f->buf_index++] = v; |
6276 |
if (f->buf_index >= IO_BUF_SIZE)
|
6277 |
qemu_fflush(f); |
6278 |
} |
6279 |
|
6280 |
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1) |
6281 |
{ |
6282 |
int size, l;
|
6283 |
|
6284 |
size = size1; |
6285 |
while (size > 0) { |
6286 |
l = f->buf_size - f->buf_index; |
6287 |
if (l == 0) { |
6288 |
qemu_fill_buffer(f); |
6289 |
l = f->buf_size - f->buf_index; |
6290 |
if (l == 0) |
6291 |
break;
|
6292 |
} |
6293 |
if (l > size)
|
6294 |
l = size; |
6295 |
memcpy(buf, f->buf + f->buf_index, l); |
6296 |
f->buf_index += l; |
6297 |
buf += l; |
6298 |
size -= l; |
6299 |
} |
6300 |
return size1 - size;
|
6301 |
} |
6302 |
|
6303 |
int qemu_get_byte(QEMUFile *f)
|
6304 |
{ |
6305 |
if (f->buf_index >= f->buf_size) {
|
6306 |
qemu_fill_buffer(f); |
6307 |
if (f->buf_index >= f->buf_size)
|
6308 |
return 0; |
6309 |
} |
6310 |
return f->buf[f->buf_index++];
|
6311 |
} |
6312 |
|
6313 |
int64_t qemu_ftell(QEMUFile *f) |
6314 |
{ |
6315 |
return f->buf_offset - f->buf_size + f->buf_index;
|
6316 |
} |
6317 |
|
6318 |
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
6319 |
{ |
6320 |
if (whence == SEEK_SET) {
|
6321 |
/* nothing to do */
|
6322 |
} else if (whence == SEEK_CUR) { |
6323 |
pos += qemu_ftell(f); |
6324 |
} else {
|
6325 |
/* SEEK_END not supported */
|
6326 |
return -1; |
6327 |
} |
6328 |
if (f->is_writable) {
|
6329 |
qemu_fflush(f); |
6330 |
f->buf_offset = pos; |
6331 |
} else {
|
6332 |
f->buf_offset = pos; |
6333 |
f->buf_index = 0;
|
6334 |
f->buf_size = 0;
|
6335 |
} |
6336 |
return pos;
|
6337 |
} |
6338 |
|
6339 |
void qemu_put_be16(QEMUFile *f, unsigned int v) |
6340 |
{ |
6341 |
qemu_put_byte(f, v >> 8);
|
6342 |
qemu_put_byte(f, v); |
6343 |
} |
6344 |
|
6345 |
void qemu_put_be32(QEMUFile *f, unsigned int v) |
6346 |
{ |
6347 |
qemu_put_byte(f, v >> 24);
|
6348 |
qemu_put_byte(f, v >> 16);
|
6349 |
qemu_put_byte(f, v >> 8);
|
6350 |
qemu_put_byte(f, v); |
6351 |
} |
6352 |
|
6353 |
void qemu_put_be64(QEMUFile *f, uint64_t v)
|
6354 |
{ |
6355 |
qemu_put_be32(f, v >> 32);
|
6356 |
qemu_put_be32(f, v); |
6357 |
} |
6358 |
|
6359 |
unsigned int qemu_get_be16(QEMUFile *f) |
6360 |
{ |
6361 |
unsigned int v; |
6362 |
v = qemu_get_byte(f) << 8;
|
6363 |
v |= qemu_get_byte(f); |
6364 |
return v;
|
6365 |
} |
6366 |
|
6367 |
unsigned int qemu_get_be32(QEMUFile *f) |
6368 |
{ |
6369 |
unsigned int v; |
6370 |
v = qemu_get_byte(f) << 24;
|
6371 |
v |= qemu_get_byte(f) << 16;
|
6372 |
v |= qemu_get_byte(f) << 8;
|
6373 |
v |= qemu_get_byte(f); |
6374 |
return v;
|
6375 |
} |
6376 |
|
6377 |
uint64_t qemu_get_be64(QEMUFile *f) |
6378 |
{ |
6379 |
uint64_t v; |
6380 |
v = (uint64_t)qemu_get_be32(f) << 32;
|
6381 |
v |= qemu_get_be32(f); |
6382 |
return v;
|
6383 |
} |
6384 |
|
6385 |
typedef struct SaveStateEntry { |
6386 |
char idstr[256]; |
6387 |
int instance_id;
|
6388 |
int version_id;
|
6389 |
SaveStateHandler *save_state; |
6390 |
LoadStateHandler *load_state; |
6391 |
void *opaque;
|
6392 |
struct SaveStateEntry *next;
|
6393 |
} SaveStateEntry; |
6394 |
|
6395 |
static SaveStateEntry *first_se;
|
6396 |
|
6397 |
/* TODO: Individual devices generally have very little idea about the rest
|
6398 |
of the system, so instance_id should be removed/replaced.
|
6399 |
Meanwhile pass -1 as instance_id if you do not already have a clearly
|
6400 |
distinguishing id for all instances of your device class. */
|
6401 |
int register_savevm(const char *idstr, |
6402 |
int instance_id,
|
6403 |
int version_id,
|
6404 |
SaveStateHandler *save_state, |
6405 |
LoadStateHandler *load_state, |
6406 |
void *opaque)
|
6407 |
{ |
6408 |
SaveStateEntry *se, **pse; |
6409 |
|
6410 |
se = qemu_malloc(sizeof(SaveStateEntry));
|
6411 |
if (!se)
|
6412 |
return -1; |
6413 |
pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
6414 |
se->instance_id = (instance_id == -1) ? 0 : instance_id; |
6415 |
se->version_id = version_id; |
6416 |
se->save_state = save_state; |
6417 |
se->load_state = load_state; |
6418 |
se->opaque = opaque; |
6419 |
se->next = NULL;
|
6420 |
|
6421 |
/* add at the end of list */
|
6422 |
pse = &first_se; |
6423 |
while (*pse != NULL) { |
6424 |
if (instance_id == -1 |
6425 |
&& strcmp(se->idstr, (*pse)->idstr) == 0
|
6426 |
&& se->instance_id <= (*pse)->instance_id) |
6427 |
se->instance_id = (*pse)->instance_id + 1;
|
6428 |
pse = &(*pse)->next; |
6429 |
} |
6430 |
*pse = se; |
6431 |
return 0; |
6432 |
} |
6433 |
|
6434 |
#define QEMU_VM_FILE_MAGIC 0x5145564d |
6435 |
#define QEMU_VM_FILE_VERSION 0x00000002 |
6436 |
|
6437 |
static int qemu_savevm_state(QEMUFile *f) |
6438 |
{ |
6439 |
SaveStateEntry *se; |
6440 |
int len, ret;
|
6441 |
int64_t cur_pos, len_pos, total_len_pos; |
6442 |
|
6443 |
qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
6444 |
qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
6445 |
total_len_pos = qemu_ftell(f); |
6446 |
qemu_put_be64(f, 0); /* total size */ |
6447 |
|
6448 |
for(se = first_se; se != NULL; se = se->next) { |
6449 |
if (se->save_state == NULL) |
6450 |
/* this one has a loader only, for backwards compatibility */
|
6451 |
continue;
|
6452 |
|
6453 |
/* ID string */
|
6454 |
len = strlen(se->idstr); |
6455 |
qemu_put_byte(f, len); |
6456 |
qemu_put_buffer(f, (uint8_t *)se->idstr, len); |
6457 |
|
6458 |
qemu_put_be32(f, se->instance_id); |
6459 |
qemu_put_be32(f, se->version_id); |
6460 |
|
6461 |
/* record size: filled later */
|
6462 |
len_pos = qemu_ftell(f); |
6463 |
qemu_put_be32(f, 0);
|
6464 |
se->save_state(f, se->opaque); |
6465 |
|
6466 |
/* fill record size */
|
6467 |
cur_pos = qemu_ftell(f); |
6468 |
len = cur_pos - len_pos - 4;
|
6469 |
qemu_fseek(f, len_pos, SEEK_SET); |
6470 |
qemu_put_be32(f, len); |
6471 |
qemu_fseek(f, cur_pos, SEEK_SET); |
6472 |
} |
6473 |
cur_pos = qemu_ftell(f); |
6474 |
qemu_fseek(f, total_len_pos, SEEK_SET); |
6475 |
qemu_put_be64(f, cur_pos - total_len_pos - 8);
|
6476 |
qemu_fseek(f, cur_pos, SEEK_SET); |
6477 |
|
6478 |
ret = 0;
|
6479 |
return ret;
|
6480 |
} |
6481 |
|
6482 |
static SaveStateEntry *find_se(const char *idstr, int instance_id) |
6483 |
{ |
6484 |
SaveStateEntry *se; |
6485 |
|
6486 |
for(se = first_se; se != NULL; se = se->next) { |
6487 |
if (!strcmp(se->idstr, idstr) &&
|
6488 |
instance_id == se->instance_id) |
6489 |
return se;
|
6490 |
} |
6491 |
return NULL; |
6492 |
} |
6493 |
|
6494 |
static int qemu_loadvm_state(QEMUFile *f) |
6495 |
{ |
6496 |
SaveStateEntry *se; |
6497 |
int len, ret, instance_id, record_len, version_id;
|
6498 |
int64_t total_len, end_pos, cur_pos; |
6499 |
unsigned int v; |
6500 |
char idstr[256]; |
6501 |
|
6502 |
v = qemu_get_be32(f); |
6503 |
if (v != QEMU_VM_FILE_MAGIC)
|
6504 |
goto fail;
|
6505 |
v = qemu_get_be32(f); |
6506 |
if (v != QEMU_VM_FILE_VERSION) {
|
6507 |
fail:
|
6508 |
ret = -1;
|
6509 |
goto the_end;
|
6510 |
} |
6511 |
total_len = qemu_get_be64(f); |
6512 |
end_pos = total_len + qemu_ftell(f); |
6513 |
for(;;) {
|
6514 |
if (qemu_ftell(f) >= end_pos)
|
6515 |
break;
|
6516 |
len = qemu_get_byte(f); |
6517 |
qemu_get_buffer(f, (uint8_t *)idstr, len); |
6518 |
idstr[len] = '\0';
|
6519 |
instance_id = qemu_get_be32(f); |
6520 |
version_id = qemu_get_be32(f); |
6521 |
record_len = qemu_get_be32(f); |
6522 |
#if 0
|
6523 |
printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
6524 |
idstr, instance_id, version_id, record_len);
|
6525 |
#endif
|
6526 |
cur_pos = qemu_ftell(f); |
6527 |
se = find_se(idstr, instance_id); |
6528 |
if (!se) {
|
6529 |
fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
6530 |
instance_id, idstr); |
6531 |
} else {
|
6532 |
ret = se->load_state(f, se->opaque, version_id); |
6533 |
if (ret < 0) { |
6534 |
fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
6535 |
instance_id, idstr); |
6536 |
} |
6537 |
} |
6538 |
/* always seek to exact end of record */
|
6539 |
qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
6540 |
} |
6541 |
ret = 0;
|
6542 |
the_end:
|
6543 |
return ret;
|
6544 |
} |
6545 |
|
6546 |
/* device can contain snapshots */
|
6547 |
static int bdrv_can_snapshot(BlockDriverState *bs) |
6548 |
{ |
6549 |
return (bs &&
|
6550 |
!bdrv_is_removable(bs) && |
6551 |
!bdrv_is_read_only(bs)); |
6552 |
} |
6553 |
|
6554 |
/* device must be snapshots in order to have a reliable snapshot */
|
6555 |
static int bdrv_has_snapshot(BlockDriverState *bs) |
6556 |
{ |
6557 |
return (bs &&
|
6558 |
!bdrv_is_removable(bs) && |
6559 |
!bdrv_is_read_only(bs)); |
6560 |
} |
6561 |
|
6562 |
static BlockDriverState *get_bs_snapshots(void) |
6563 |
{ |
6564 |
BlockDriverState *bs; |
6565 |
int i;
|
6566 |
|
6567 |
if (bs_snapshots)
|
6568 |
return bs_snapshots;
|
6569 |
for(i = 0; i <= nb_drives; i++) { |
6570 |
bs = drives_table[i].bdrv; |
6571 |
if (bdrv_can_snapshot(bs))
|
6572 |
goto ok;
|
6573 |
} |
6574 |
return NULL; |
6575 |
ok:
|
6576 |
bs_snapshots = bs; |
6577 |
return bs;
|
6578 |
} |
6579 |
|
6580 |
static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info, |
6581 |
const char *name) |
6582 |
{ |
6583 |
QEMUSnapshotInfo *sn_tab, *sn; |
6584 |
int nb_sns, i, ret;
|
6585 |
|
6586 |
ret = -ENOENT; |
6587 |
nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6588 |
if (nb_sns < 0) |
6589 |
return ret;
|
6590 |
for(i = 0; i < nb_sns; i++) { |
6591 |
sn = &sn_tab[i]; |
6592 |
if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
|
6593 |
*sn_info = *sn; |
6594 |
ret = 0;
|
6595 |
break;
|
6596 |
} |
6597 |
} |
6598 |
qemu_free(sn_tab); |
6599 |
return ret;
|
6600 |
} |
6601 |
|
6602 |
void do_savevm(const char *name) |
6603 |
{ |
6604 |
BlockDriverState *bs, *bs1; |
6605 |
QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1; |
6606 |
int must_delete, ret, i;
|
6607 |
BlockDriverInfo bdi1, *bdi = &bdi1; |
6608 |
QEMUFile *f; |
6609 |
int saved_vm_running;
|
6610 |
#ifdef _WIN32
|
6611 |
struct _timeb tb;
|
6612 |
#else
|
6613 |
struct timeval tv;
|
6614 |
#endif
|
6615 |
|
6616 |
bs = get_bs_snapshots(); |
6617 |
if (!bs) {
|
6618 |
term_printf("No block device can accept snapshots\n");
|
6619 |
return;
|
6620 |
} |
6621 |
|
6622 |
/* ??? Should this occur after vm_stop? */
|
6623 |
qemu_aio_flush(); |
6624 |
|
6625 |
saved_vm_running = vm_running; |
6626 |
vm_stop(0);
|
6627 |
|
6628 |
must_delete = 0;
|
6629 |
if (name) {
|
6630 |
ret = bdrv_snapshot_find(bs, old_sn, name); |
6631 |
if (ret >= 0) { |
6632 |
must_delete = 1;
|
6633 |
} |
6634 |
} |
6635 |
memset(sn, 0, sizeof(*sn)); |
6636 |
if (must_delete) {
|
6637 |
pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
|
6638 |
pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
|
6639 |
} else {
|
6640 |
if (name)
|
6641 |
pstrcpy(sn->name, sizeof(sn->name), name);
|
6642 |
} |
6643 |
|
6644 |
/* fill auxiliary fields */
|
6645 |
#ifdef _WIN32
|
6646 |
_ftime(&tb); |
6647 |
sn->date_sec = tb.time; |
6648 |
sn->date_nsec = tb.millitm * 1000000;
|
6649 |
#else
|
6650 |
gettimeofday(&tv, NULL);
|
6651 |
sn->date_sec = tv.tv_sec; |
6652 |
sn->date_nsec = tv.tv_usec * 1000;
|
6653 |
#endif
|
6654 |
sn->vm_clock_nsec = qemu_get_clock(vm_clock); |
6655 |
|
6656 |
if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6657 |
term_printf("Device %s does not support VM state snapshots\n",
|
6658 |
bdrv_get_device_name(bs)); |
6659 |
goto the_end;
|
6660 |
} |
6661 |
|
6662 |
/* save the VM state */
|
6663 |
f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
|
6664 |
if (!f) {
|
6665 |
term_printf("Could not open VM state file\n");
|
6666 |
goto the_end;
|
6667 |
} |
6668 |
ret = qemu_savevm_state(f); |
6669 |
sn->vm_state_size = qemu_ftell(f); |
6670 |
qemu_fclose(f); |
6671 |
if (ret < 0) { |
6672 |
term_printf("Error %d while writing VM\n", ret);
|
6673 |
goto the_end;
|
6674 |
} |
6675 |
|
6676 |
/* create the snapshots */
|
6677 |
|
6678 |
for(i = 0; i < nb_drives; i++) { |
6679 |
bs1 = drives_table[i].bdrv; |
6680 |
if (bdrv_has_snapshot(bs1)) {
|
6681 |
if (must_delete) {
|
6682 |
ret = bdrv_snapshot_delete(bs1, old_sn->id_str); |
6683 |
if (ret < 0) { |
6684 |
term_printf("Error while deleting snapshot on '%s'\n",
|
6685 |
bdrv_get_device_name(bs1)); |
6686 |
} |
6687 |
} |
6688 |
ret = bdrv_snapshot_create(bs1, sn); |
6689 |
if (ret < 0) { |
6690 |
term_printf("Error while creating snapshot on '%s'\n",
|
6691 |
bdrv_get_device_name(bs1)); |
6692 |
} |
6693 |
} |
6694 |
} |
6695 |
|
6696 |
the_end:
|
6697 |
if (saved_vm_running)
|
6698 |
vm_start(); |
6699 |
} |
6700 |
|
6701 |
void do_loadvm(const char *name) |
6702 |
{ |
6703 |
BlockDriverState *bs, *bs1; |
6704 |
BlockDriverInfo bdi1, *bdi = &bdi1; |
6705 |
QEMUFile *f; |
6706 |
int i, ret;
|
6707 |
int saved_vm_running;
|
6708 |
|
6709 |
bs = get_bs_snapshots(); |
6710 |
if (!bs) {
|
6711 |
term_printf("No block device supports snapshots\n");
|
6712 |
return;
|
6713 |
} |
6714 |
|
6715 |
/* Flush all IO requests so they don't interfere with the new state. */
|
6716 |
qemu_aio_flush(); |
6717 |
|
6718 |
saved_vm_running = vm_running; |
6719 |
vm_stop(0);
|
6720 |
|
6721 |
for(i = 0; i <= nb_drives; i++) { |
6722 |
bs1 = drives_table[i].bdrv; |
6723 |
if (bdrv_has_snapshot(bs1)) {
|
6724 |
ret = bdrv_snapshot_goto(bs1, name); |
6725 |
if (ret < 0) { |
6726 |
if (bs != bs1)
|
6727 |
term_printf("Warning: ");
|
6728 |
switch(ret) {
|
6729 |
case -ENOTSUP:
|
6730 |
term_printf("Snapshots not supported on device '%s'\n",
|
6731 |
bdrv_get_device_name(bs1)); |
6732 |
break;
|
6733 |
case -ENOENT:
|
6734 |
term_printf("Could not find snapshot '%s' on device '%s'\n",
|
6735 |
name, bdrv_get_device_name(bs1)); |
6736 |
break;
|
6737 |
default:
|
6738 |
term_printf("Error %d while activating snapshot on '%s'\n",
|
6739 |
ret, bdrv_get_device_name(bs1)); |
6740 |
break;
|
6741 |
} |
6742 |
/* fatal on snapshot block device */
|
6743 |
if (bs == bs1)
|
6744 |
goto the_end;
|
6745 |
} |
6746 |
} |
6747 |
} |
6748 |
|
6749 |
if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) { |
6750 |
term_printf("Device %s does not support VM state snapshots\n",
|
6751 |
bdrv_get_device_name(bs)); |
6752 |
return;
|
6753 |
} |
6754 |
|
6755 |
/* restore the VM state */
|
6756 |
f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
|
6757 |
if (!f) {
|
6758 |
term_printf("Could not open VM state file\n");
|
6759 |
goto the_end;
|
6760 |
} |
6761 |
ret = qemu_loadvm_state(f); |
6762 |
qemu_fclose(f); |
6763 |
if (ret < 0) { |
6764 |
term_printf("Error %d while loading VM state\n", ret);
|
6765 |
} |
6766 |
the_end:
|
6767 |
if (saved_vm_running)
|
6768 |
vm_start(); |
6769 |
} |
6770 |
|
6771 |
void do_delvm(const char *name) |
6772 |
{ |
6773 |
BlockDriverState *bs, *bs1; |
6774 |
int i, ret;
|
6775 |
|
6776 |
bs = get_bs_snapshots(); |
6777 |
if (!bs) {
|
6778 |
term_printf("No block device supports snapshots\n");
|
6779 |
return;
|
6780 |
} |
6781 |
|
6782 |
for(i = 0; i <= nb_drives; i++) { |
6783 |
bs1 = drives_table[i].bdrv; |
6784 |
if (bdrv_has_snapshot(bs1)) {
|
6785 |
ret = bdrv_snapshot_delete(bs1, name); |
6786 |
if (ret < 0) { |
6787 |
if (ret == -ENOTSUP)
|
6788 |
term_printf("Snapshots not supported on device '%s'\n",
|
6789 |
bdrv_get_device_name(bs1)); |
6790 |
else
|
6791 |
term_printf("Error %d while deleting snapshot on '%s'\n",
|
6792 |
ret, bdrv_get_device_name(bs1)); |
6793 |
} |
6794 |
} |
6795 |
} |
6796 |
} |
6797 |
|
6798 |
void do_info_snapshots(void) |
6799 |
{ |
6800 |
BlockDriverState *bs, *bs1; |
6801 |
QEMUSnapshotInfo *sn_tab, *sn; |
6802 |
int nb_sns, i;
|
6803 |
char buf[256]; |
6804 |
|
6805 |
bs = get_bs_snapshots(); |
6806 |
if (!bs) {
|
6807 |
term_printf("No available block device supports snapshots\n");
|
6808 |
return;
|
6809 |
} |
6810 |
term_printf("Snapshot devices:");
|
6811 |
for(i = 0; i <= nb_drives; i++) { |
6812 |
bs1 = drives_table[i].bdrv; |
6813 |
if (bdrv_has_snapshot(bs1)) {
|
6814 |
if (bs == bs1)
|
6815 |
term_printf(" %s", bdrv_get_device_name(bs1));
|
6816 |
} |
6817 |
} |
6818 |
term_printf("\n");
|
6819 |
|
6820 |
nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
6821 |
if (nb_sns < 0) { |
6822 |
term_printf("bdrv_snapshot_list: error %d\n", nb_sns);
|
6823 |
return;
|
6824 |
} |
6825 |
term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs));
|
6826 |
term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
6827 |
for(i = 0; i < nb_sns; i++) { |
6828 |
sn = &sn_tab[i]; |
6829 |
term_printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
6830 |
} |
6831 |
qemu_free(sn_tab); |
6832 |
} |
6833 |
|
6834 |
/***********************************************************/
|
6835 |
/* ram save/restore */
|
6836 |
|
6837 |
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
6838 |
{ |
6839 |
int v;
|
6840 |
|
6841 |
v = qemu_get_byte(f); |
6842 |
switch(v) {
|
6843 |
case 0: |
6844 |
if (qemu_get_buffer(f, buf, len) != len)
|
6845 |
return -EIO;
|
6846 |
break;
|
6847 |
case 1: |
6848 |
v = qemu_get_byte(f); |
6849 |
memset(buf, v, len); |
6850 |
break;
|
6851 |
default:
|
6852 |
return -EINVAL;
|
6853 |
} |
6854 |
return 0; |
6855 |
} |
6856 |
|
6857 |
static int ram_load_v1(QEMUFile *f, void *opaque) |
6858 |
{ |
6859 |
int ret;
|
6860 |
ram_addr_t i; |
6861 |
|
6862 |
if (qemu_get_be32(f) != phys_ram_size)
|
6863 |
return -EINVAL;
|
6864 |
for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
6865 |
ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
6866 |
if (ret)
|
6867 |
return ret;
|
6868 |
} |
6869 |
return 0; |
6870 |
} |
6871 |
|
6872 |
#define BDRV_HASH_BLOCK_SIZE 1024 |
6873 |
#define IOBUF_SIZE 4096 |
6874 |
#define RAM_CBLOCK_MAGIC 0xfabe |
6875 |
|
6876 |
typedef struct RamCompressState { |
6877 |
z_stream zstream; |
6878 |
QEMUFile *f; |
6879 |
uint8_t buf[IOBUF_SIZE]; |
6880 |
} RamCompressState; |
6881 |
|
6882 |
static int ram_compress_open(RamCompressState *s, QEMUFile *f) |
6883 |
{ |
6884 |
int ret;
|
6885 |
memset(s, 0, sizeof(*s)); |
6886 |
s->f = f; |
6887 |
ret = deflateInit2(&s->zstream, 1,
|
6888 |
Z_DEFLATED, 15,
|
6889 |
9, Z_DEFAULT_STRATEGY);
|
6890 |
if (ret != Z_OK)
|
6891 |
return -1; |
6892 |
s->zstream.avail_out = IOBUF_SIZE; |
6893 |
s->zstream.next_out = s->buf; |
6894 |
return 0; |
6895 |
} |
6896 |
|
6897 |
static void ram_put_cblock(RamCompressState *s, const uint8_t *buf, int len) |
6898 |
{ |
6899 |
qemu_put_be16(s->f, RAM_CBLOCK_MAGIC); |
6900 |
qemu_put_be16(s->f, len); |
6901 |
qemu_put_buffer(s->f, buf, len); |
6902 |
} |
6903 |
|
6904 |
static int ram_compress_buf(RamCompressState *s, const uint8_t *buf, int len) |
6905 |
{ |
6906 |
int ret;
|
6907 |
|
6908 |
s->zstream.avail_in = len; |
6909 |
s->zstream.next_in = (uint8_t *)buf; |
6910 |
while (s->zstream.avail_in > 0) { |
6911 |
ret = deflate(&s->zstream, Z_NO_FLUSH); |
6912 |
if (ret != Z_OK)
|
6913 |
return -1; |
6914 |
if (s->zstream.avail_out == 0) { |
6915 |
ram_put_cblock(s, s->buf, IOBUF_SIZE); |
6916 |
s->zstream.avail_out = IOBUF_SIZE; |
6917 |
s->zstream.next_out = s->buf; |
6918 |
} |
6919 |
} |
6920 |
return 0; |
6921 |
} |
6922 |
|
6923 |
static void ram_compress_close(RamCompressState *s) |
6924 |
{ |
6925 |
int len, ret;
|
6926 |
|
6927 |
/* compress last bytes */
|
6928 |
for(;;) {
|
6929 |
ret = deflate(&s->zstream, Z_FINISH); |
6930 |
if (ret == Z_OK || ret == Z_STREAM_END) {
|
6931 |
len = IOBUF_SIZE - s->zstream.avail_out; |
6932 |
if (len > 0) { |
6933 |
ram_put_cblock(s, s->buf, len); |
6934 |
} |
6935 |
s->zstream.avail_out = IOBUF_SIZE; |
6936 |
s->zstream.next_out = s->buf; |
6937 |
if (ret == Z_STREAM_END)
|
6938 |
break;
|
6939 |
} else {
|
6940 |
goto fail;
|
6941 |
} |
6942 |
} |
6943 |
fail:
|
6944 |
deflateEnd(&s->zstream); |
6945 |
} |
6946 |
|
6947 |
typedef struct RamDecompressState { |
6948 |
z_stream zstream; |
6949 |
QEMUFile *f; |
6950 |
uint8_t buf[IOBUF_SIZE]; |
6951 |
} RamDecompressState; |
6952 |
|
6953 |
static int ram_decompress_open(RamDecompressState *s, QEMUFile *f) |
6954 |
{ |
6955 |
int ret;
|
6956 |
memset(s, 0, sizeof(*s)); |
6957 |
s->f = f; |
6958 |
ret = inflateInit(&s->zstream); |
6959 |
if (ret != Z_OK)
|
6960 |
return -1; |
6961 |
return 0; |
6962 |
} |
6963 |
|
6964 |
static int ram_decompress_buf(RamDecompressState *s, uint8_t *buf, int len) |
6965 |
{ |
6966 |
int ret, clen;
|
6967 |
|
6968 |
s->zstream.avail_out = len; |
6969 |
s->zstream.next_out = buf; |
6970 |
while (s->zstream.avail_out > 0) { |
6971 |
if (s->zstream.avail_in == 0) { |
6972 |
if (qemu_get_be16(s->f) != RAM_CBLOCK_MAGIC)
|
6973 |
return -1; |
6974 |
clen = qemu_get_be16(s->f); |
6975 |
if (clen > IOBUF_SIZE)
|
6976 |
return -1; |
6977 |
qemu_get_buffer(s->f, s->buf, clen); |
6978 |
s->zstream.avail_in = clen; |
6979 |
s->zstream.next_in = s->buf; |
6980 |
} |
6981 |
ret = inflate(&s->zstream, Z_PARTIAL_FLUSH); |
6982 |
if (ret != Z_OK && ret != Z_STREAM_END) {
|
6983 |
return -1; |
6984 |
} |
6985 |
} |
6986 |
return 0; |
6987 |
} |
6988 |
|
6989 |
static void ram_decompress_close(RamDecompressState *s) |
6990 |
{ |
6991 |
inflateEnd(&s->zstream); |
6992 |
} |
6993 |
|
6994 |
static void ram_save(QEMUFile *f, void *opaque) |
6995 |
{ |
6996 |
ram_addr_t i; |
6997 |
RamCompressState s1, *s = &s1; |
6998 |
uint8_t buf[10];
|
6999 |
|
7000 |
qemu_put_be32(f, phys_ram_size); |
7001 |
if (ram_compress_open(s, f) < 0) |
7002 |
return;
|
7003 |
for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
7004 |
#if 0
|
7005 |
if (tight_savevm_enabled) {
|
7006 |
int64_t sector_num;
|
7007 |
int j;
|
7008 |
|
7009 |
/* find if the memory block is available on a virtual
|
7010 |
block device */
|
7011 |
sector_num = -1;
|
7012 |
for(j = 0; j < nb_drives; j++) {
|
7013 |
sector_num = bdrv_hash_find(drives_table[j].bdrv,
|
7014 |
phys_ram_base + i,
|
7015 |
BDRV_HASH_BLOCK_SIZE);
|
7016 |
if (sector_num >= 0)
|
7017 |
break;
|
7018 |
}
|
7019 |
if (j == nb_drives)
|
7020 |
goto normal_compress;
|
7021 |
buf[0] = 1;
|
7022 |
buf[1] = j;
|
7023 |
cpu_to_be64wu((uint64_t *)(buf + 2), sector_num);
|
7024 |
ram_compress_buf(s, buf, 10);
|
7025 |
} else
|
7026 |
#endif
|
7027 |
{ |
7028 |
// normal_compress:
|
7029 |
buf[0] = 0; |
7030 |
ram_compress_buf(s, buf, 1);
|
7031 |
ram_compress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE); |
7032 |
} |
7033 |
} |
7034 |
ram_compress_close(s); |
7035 |
} |
7036 |
|
7037 |
static int ram_load(QEMUFile *f, void *opaque, int version_id) |
7038 |
{ |
7039 |
RamDecompressState s1, *s = &s1; |
7040 |
uint8_t buf[10];
|
7041 |
ram_addr_t i; |
7042 |
|
7043 |
if (version_id == 1) |
7044 |
return ram_load_v1(f, opaque);
|
7045 |
if (version_id != 2) |
7046 |
return -EINVAL;
|
7047 |
if (qemu_get_be32(f) != phys_ram_size)
|
7048 |
return -EINVAL;
|
7049 |
if (ram_decompress_open(s, f) < 0) |
7050 |
return -EINVAL;
|
7051 |
for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) { |
7052 |
if (ram_decompress_buf(s, buf, 1) < 0) { |
7053 |
fprintf(stderr, "Error while reading ram block header\n");
|
7054 |
goto error;
|
7055 |
} |
7056 |
if (buf[0] == 0) { |
7057 |
if (ram_decompress_buf(s, phys_ram_base + i, BDRV_HASH_BLOCK_SIZE) < 0) { |
7058 |
fprintf(stderr, "Error while reading ram block address=0x%08" PRIx64, (uint64_t)i);
|
7059 |
goto error;
|
7060 |
} |
7061 |
} else
|
7062 |
#if 0
|
7063 |
if (buf[0] == 1) {
|
7064 |
int bs_index;
|
7065 |
int64_t sector_num;
|
7066 |
|
7067 |
ram_decompress_buf(s, buf + 1, 9);
|
7068 |
bs_index = buf[1];
|
7069 |
sector_num = be64_to_cpupu((const uint64_t *)(buf + 2));
|
7070 |
if (bs_index >= nb_drives) {
|
7071 |
fprintf(stderr, "Invalid block device index %d\n", bs_index);
|
7072 |
goto error;
|
7073 |
}
|
7074 |
if (bdrv_read(drives_table[bs_index].bdrv, sector_num,
|
7075 |
phys_ram_base + i,
|
7076 |
BDRV_HASH_BLOCK_SIZE / 512) < 0) {
|
7077 |
fprintf(stderr, "Error while reading sector %d:%" PRId64 "\n",
|
7078 |
bs_index, sector_num);
|
7079 |
goto error;
|
7080 |
}
|
7081 |
} else
|
7082 |
#endif
|
7083 |
{ |
7084 |
error:
|
7085 |
printf("Error block header\n");
|
7086 |
return -EINVAL;
|
7087 |
} |
7088 |
} |
7089 |
ram_decompress_close(s); |
7090 |
return 0; |
7091 |
} |
7092 |
|
7093 |
/***********************************************************/
|
7094 |
/* bottom halves (can be seen as timers which expire ASAP) */
|
7095 |
|
7096 |
struct QEMUBH {
|
7097 |
QEMUBHFunc *cb; |
7098 |
void *opaque;
|
7099 |
int scheduled;
|
7100 |
QEMUBH *next; |
7101 |
}; |
7102 |
|
7103 |
static QEMUBH *first_bh = NULL; |
7104 |
|
7105 |
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
7106 |
{ |
7107 |
QEMUBH *bh; |
7108 |
bh = qemu_mallocz(sizeof(QEMUBH));
|
7109 |
if (!bh)
|
7110 |
return NULL; |
7111 |
bh->cb = cb; |
7112 |
bh->opaque = opaque; |
7113 |
return bh;
|
7114 |
} |
7115 |
|
7116 |
int qemu_bh_poll(void) |
7117 |
{ |
7118 |
QEMUBH *bh, **pbh; |
7119 |
int ret;
|
7120 |
|
7121 |
ret = 0;
|
7122 |
for(;;) {
|
7123 |
pbh = &first_bh; |
7124 |
bh = *pbh; |
7125 |
if (!bh)
|
7126 |
break;
|
7127 |
ret = 1;
|
7128 |
*pbh = bh->next; |
7129 |
bh->scheduled = 0;
|
7130 |
bh->cb(bh->opaque); |
7131 |
} |
7132 |
return ret;
|
7133 |
} |
7134 |
|
7135 |
void qemu_bh_schedule(QEMUBH *bh)
|
7136 |
{ |
7137 |
CPUState *env = cpu_single_env; |
7138 |
if (bh->scheduled)
|
7139 |
return;
|
7140 |
bh->scheduled = 1;
|
7141 |
bh->next = first_bh; |
7142 |
first_bh = bh; |
7143 |
|
7144 |
/* stop the currently executing CPU to execute the BH ASAP */
|
7145 |
if (env) {
|
7146 |
cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
7147 |
} |
7148 |
} |
7149 |
|
7150 |
void qemu_bh_cancel(QEMUBH *bh)
|
7151 |
{ |
7152 |
QEMUBH **pbh; |
7153 |
if (bh->scheduled) {
|
7154 |
pbh = &first_bh; |
7155 |
while (*pbh != bh)
|
7156 |
pbh = &(*pbh)->next; |
7157 |
*pbh = bh->next; |
7158 |
bh->scheduled = 0;
|
7159 |
} |
7160 |
} |
7161 |
|
7162 |
void qemu_bh_delete(QEMUBH *bh)
|
7163 |
{ |
7164 |
qemu_bh_cancel(bh); |
7165 |
qemu_free(bh); |
7166 |
} |
7167 |
|
7168 |
/***********************************************************/
|
7169 |
/* machine registration */
|
7170 |
|
7171 |
QEMUMachine *first_machine = NULL;
|
7172 |
|
7173 |
int qemu_register_machine(QEMUMachine *m)
|
7174 |
{ |
7175 |
QEMUMachine **pm; |
7176 |
pm = &first_machine; |
7177 |
while (*pm != NULL) |
7178 |
pm = &(*pm)->next; |
7179 |
m->next = NULL;
|
7180 |
*pm = m; |
7181 |
return 0; |
7182 |
} |
7183 |
|
7184 |
static QEMUMachine *find_machine(const char *name) |
7185 |
{ |
7186 |
QEMUMachine *m; |
7187 |
|
7188 |
for(m = first_machine; m != NULL; m = m->next) { |
7189 |
if (!strcmp(m->name, name))
|
7190 |
return m;
|
7191 |
} |
7192 |
return NULL; |
7193 |
} |
7194 |
|
7195 |
/***********************************************************/
|
7196 |
/* main execution loop */
|
7197 |
|
7198 |
static void gui_update(void *opaque) |
7199 |
{ |
7200 |
DisplayState *ds = opaque; |
7201 |
ds->dpy_refresh(ds); |
7202 |
qemu_mod_timer(ds->gui_timer, |
7203 |
(ds->gui_timer_interval ? |
7204 |
ds->gui_timer_interval : |
7205 |
GUI_REFRESH_INTERVAL) |
7206 |
+ qemu_get_clock(rt_clock)); |
7207 |
} |
7208 |
|
7209 |
struct vm_change_state_entry {
|
7210 |
VMChangeStateHandler *cb; |
7211 |
void *opaque;
|
7212 |
LIST_ENTRY (vm_change_state_entry) entries; |
7213 |
}; |
7214 |
|
7215 |
static LIST_HEAD(vm_change_state_head, vm_change_state_entry) vm_change_state_head;
|
7216 |
|
7217 |
VMChangeStateEntry *qemu_add_vm_change_state_handler(VMChangeStateHandler *cb, |
7218 |
void *opaque)
|
7219 |
{ |
7220 |
VMChangeStateEntry *e; |
7221 |
|
7222 |
e = qemu_mallocz(sizeof (*e));
|
7223 |
if (!e)
|
7224 |
return NULL; |
7225 |
|
7226 |
e->cb = cb; |
7227 |
e->opaque = opaque; |
7228 |
LIST_INSERT_HEAD(&vm_change_state_head, e, entries); |
7229 |
return e;
|
7230 |
} |
7231 |
|
7232 |
void qemu_del_vm_change_state_handler(VMChangeStateEntry *e)
|
7233 |
{ |
7234 |
LIST_REMOVE (e, entries); |
7235 |
qemu_free (e); |
7236 |
} |
7237 |
|
7238 |
static void vm_state_notify(int running) |
7239 |
{ |
7240 |
VMChangeStateEntry *e; |
7241 |
|
7242 |
for (e = vm_change_state_head.lh_first; e; e = e->entries.le_next) {
|
7243 |
e->cb(e->opaque, running); |
7244 |
} |
7245 |
} |
7246 |
|
7247 |
/* XXX: support several handlers */
|
7248 |
static VMStopHandler *vm_stop_cb;
|
7249 |
static void *vm_stop_opaque; |
7250 |
|
7251 |
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7252 |
{ |
7253 |
vm_stop_cb = cb; |
7254 |
vm_stop_opaque = opaque; |
7255 |
return 0; |
7256 |
} |
7257 |
|
7258 |
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
7259 |
{ |
7260 |
vm_stop_cb = NULL;
|
7261 |
} |
7262 |
|
7263 |
void vm_start(void) |
7264 |
{ |
7265 |
if (!vm_running) {
|
7266 |
cpu_enable_ticks(); |
7267 |
vm_running = 1;
|
7268 |
vm_state_notify(1);
|
7269 |
qemu_rearm_alarm_timer(alarm_timer); |
7270 |
} |
7271 |
} |
7272 |
|
7273 |
void vm_stop(int reason) |
7274 |
{ |
7275 |
if (vm_running) {
|
7276 |
cpu_disable_ticks(); |
7277 |
vm_running = 0;
|
7278 |
if (reason != 0) { |
7279 |
if (vm_stop_cb) {
|
7280 |
vm_stop_cb(vm_stop_opaque, reason); |
7281 |
} |
7282 |
} |
7283 |
vm_state_notify(0);
|
7284 |
} |
7285 |
} |
7286 |
|
7287 |
/* reset/shutdown handler */
|
7288 |
|
7289 |
typedef struct QEMUResetEntry { |
7290 |
QEMUResetHandler *func; |
7291 |
void *opaque;
|
7292 |
struct QEMUResetEntry *next;
|
7293 |
} QEMUResetEntry; |
7294 |
|
7295 |
static QEMUResetEntry *first_reset_entry;
|
7296 |
static int reset_requested; |
7297 |
static int shutdown_requested; |
7298 |
static int powerdown_requested; |
7299 |
|
7300 |
int qemu_shutdown_requested(void) |
7301 |
{ |
7302 |
int r = shutdown_requested;
|
7303 |
shutdown_requested = 0;
|
7304 |
return r;
|
7305 |
} |
7306 |
|
7307 |
int qemu_reset_requested(void) |
7308 |
{ |
7309 |
int r = reset_requested;
|
7310 |
reset_requested = 0;
|
7311 |
return r;
|
7312 |
} |
7313 |
|
7314 |
int qemu_powerdown_requested(void) |
7315 |
{ |
7316 |
int r = powerdown_requested;
|
7317 |
powerdown_requested = 0;
|
7318 |
return r;
|
7319 |
} |
7320 |
|
7321 |
void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
7322 |
{ |
7323 |
QEMUResetEntry **pre, *re; |
7324 |
|
7325 |
pre = &first_reset_entry; |
7326 |
while (*pre != NULL) |
7327 |
pre = &(*pre)->next; |
7328 |
re = qemu_mallocz(sizeof(QEMUResetEntry));
|
7329 |
re->func = func; |
7330 |
re->opaque = opaque; |
7331 |
re->next = NULL;
|
7332 |
*pre = re; |
7333 |
} |
7334 |
|
7335 |
void qemu_system_reset(void) |
7336 |
{ |
7337 |
QEMUResetEntry *re; |
7338 |
|
7339 |
/* reset all devices */
|
7340 |
for(re = first_reset_entry; re != NULL; re = re->next) { |
7341 |
re->func(re->opaque); |
7342 |
} |
7343 |
} |
7344 |
|
7345 |
void qemu_system_reset_request(void) |
7346 |
{ |
7347 |
if (no_reboot) {
|
7348 |
shutdown_requested = 1;
|
7349 |
} else {
|
7350 |
reset_requested = 1;
|
7351 |
} |
7352 |
if (cpu_single_env)
|
7353 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7354 |
} |
7355 |
|
7356 |
void qemu_system_shutdown_request(void) |
7357 |
{ |
7358 |
shutdown_requested = 1;
|
7359 |
if (cpu_single_env)
|
7360 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7361 |
} |
7362 |
|
7363 |
void qemu_system_powerdown_request(void) |
7364 |
{ |
7365 |
powerdown_requested = 1;
|
7366 |
if (cpu_single_env)
|
7367 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
7368 |
} |
7369 |
|
7370 |
void main_loop_wait(int timeout) |
7371 |
{ |
7372 |
IOHandlerRecord *ioh; |
7373 |
fd_set rfds, wfds, xfds; |
7374 |
int ret, nfds;
|
7375 |
#ifdef _WIN32
|
7376 |
int ret2, i;
|
7377 |
#endif
|
7378 |
struct timeval tv;
|
7379 |
PollingEntry *pe; |
7380 |
|
7381 |
|
7382 |
/* XXX: need to suppress polling by better using win32 events */
|
7383 |
ret = 0;
|
7384 |
for(pe = first_polling_entry; pe != NULL; pe = pe->next) { |
7385 |
ret |= pe->func(pe->opaque); |
7386 |
} |
7387 |
#ifdef _WIN32
|
7388 |
if (ret == 0) { |
7389 |
int err;
|
7390 |
WaitObjects *w = &wait_objects; |
7391 |
|
7392 |
ret = WaitForMultipleObjects(w->num, w->events, FALSE, timeout); |
7393 |
if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) { |
7394 |
if (w->func[ret - WAIT_OBJECT_0])
|
7395 |
w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]); |
7396 |
|
7397 |
/* Check for additional signaled events */
|
7398 |
for(i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) { |
7399 |
|
7400 |
/* Check if event is signaled */
|
7401 |
ret2 = WaitForSingleObject(w->events[i], 0);
|
7402 |
if(ret2 == WAIT_OBJECT_0) {
|
7403 |
if (w->func[i])
|
7404 |
w->func[i](w->opaque[i]); |
7405 |
} else if (ret2 == WAIT_TIMEOUT) { |
7406 |
} else {
|
7407 |
err = GetLastError(); |
7408 |
fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
|
7409 |
} |
7410 |
} |
7411 |
} else if (ret == WAIT_TIMEOUT) { |
7412 |
} else {
|
7413 |
err = GetLastError(); |
7414 |
fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
|
7415 |
} |
7416 |
} |
7417 |
#endif
|
7418 |
/* poll any events */
|
7419 |
/* XXX: separate device handlers from system ones */
|
7420 |
nfds = -1;
|
7421 |
FD_ZERO(&rfds); |
7422 |
FD_ZERO(&wfds); |
7423 |
FD_ZERO(&xfds); |
7424 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7425 |
if (ioh->deleted)
|
7426 |
continue;
|
7427 |
if (ioh->fd_read &&
|
7428 |
(!ioh->fd_read_poll || |
7429 |
ioh->fd_read_poll(ioh->opaque) != 0)) {
|
7430 |
FD_SET(ioh->fd, &rfds); |
7431 |
if (ioh->fd > nfds)
|
7432 |
nfds = ioh->fd; |
7433 |
} |
7434 |
if (ioh->fd_write) {
|
7435 |
FD_SET(ioh->fd, &wfds); |
7436 |
if (ioh->fd > nfds)
|
7437 |
nfds = ioh->fd; |
7438 |
} |
7439 |
} |
7440 |
|
7441 |
tv.tv_sec = 0;
|
7442 |
#ifdef _WIN32
|
7443 |
tv.tv_usec = 0;
|
7444 |
#else
|
7445 |
tv.tv_usec = timeout * 1000;
|
7446 |
#endif
|
7447 |
#if defined(CONFIG_SLIRP)
|
7448 |
if (slirp_inited) {
|
7449 |
slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
7450 |
} |
7451 |
#endif
|
7452 |
ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
7453 |
if (ret > 0) { |
7454 |
IOHandlerRecord **pioh; |
7455 |
|
7456 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
7457 |
if (!ioh->deleted && ioh->fd_read && FD_ISSET(ioh->fd, &rfds)) {
|
7458 |
ioh->fd_read(ioh->opaque); |
7459 |
} |
7460 |
if (!ioh->deleted && ioh->fd_write && FD_ISSET(ioh->fd, &wfds)) {
|
7461 |
ioh->fd_write(ioh->opaque); |
7462 |
} |
7463 |
} |
7464 |
|
7465 |
/* remove deleted IO handlers */
|
7466 |
pioh = &first_io_handler; |
7467 |
while (*pioh) {
|
7468 |
ioh = *pioh; |
7469 |
if (ioh->deleted) {
|
7470 |
*pioh = ioh->next; |
7471 |
qemu_free(ioh); |
7472 |
} else
|
7473 |
pioh = &ioh->next; |
7474 |
} |
7475 |
} |
7476 |
#if defined(CONFIG_SLIRP)
|
7477 |
if (slirp_inited) {
|
7478 |
if (ret < 0) { |
7479 |
FD_ZERO(&rfds); |
7480 |
FD_ZERO(&wfds); |
7481 |
FD_ZERO(&xfds); |
7482 |
} |
7483 |
slirp_select_poll(&rfds, &wfds, &xfds); |
7484 |
} |
7485 |
#endif
|
7486 |
qemu_aio_poll(); |
7487 |
|
7488 |
if (vm_running) {
|
7489 |
if (likely(!(cur_cpu->singlestep_enabled & SSTEP_NOTIMER)))
|
7490 |
qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
7491 |
qemu_get_clock(vm_clock)); |
7492 |
/* run dma transfers, if any */
|
7493 |
DMA_run(); |
7494 |
} |
7495 |
|
7496 |
/* real time timers */
|
7497 |
qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
7498 |
qemu_get_clock(rt_clock)); |
7499 |
|
7500 |
if (alarm_timer->flags & ALARM_FLAG_EXPIRED) {
|
7501 |
alarm_timer->flags &= ~(ALARM_FLAG_EXPIRED); |
7502 |
qemu_rearm_alarm_timer(alarm_timer); |
7503 |
} |
7504 |
|
7505 |
/* Check bottom-halves last in case any of the earlier events triggered
|
7506 |
them. */
|
7507 |
qemu_bh_poll(); |
7508 |
|
7509 |
} |
7510 |
|
7511 |
static int main_loop(void) |
7512 |
{ |
7513 |
int ret, timeout;
|
7514 |
#ifdef CONFIG_PROFILER
|
7515 |
int64_t ti; |
7516 |
#endif
|
7517 |
CPUState *env; |
7518 |
|
7519 |
cur_cpu = first_cpu; |
7520 |
next_cpu = cur_cpu->next_cpu ?: first_cpu; |
7521 |
for(;;) {
|
7522 |
if (vm_running) {
|
7523 |
|
7524 |
for(;;) {
|
7525 |
/* get next cpu */
|
7526 |
env = next_cpu; |
7527 |
#ifdef CONFIG_PROFILER
|
7528 |
ti = profile_getclock(); |
7529 |
#endif
|
7530 |
if (use_icount) {
|
7531 |
int64_t count; |
7532 |
int decr;
|
7533 |
qemu_icount -= (env->icount_decr.u16.low + env->icount_extra); |
7534 |
env->icount_decr.u16.low = 0;
|
7535 |
env->icount_extra = 0;
|
7536 |
count = qemu_next_deadline(); |
7537 |
count = (count + (1 << icount_time_shift) - 1) |
7538 |
>> icount_time_shift; |
7539 |
qemu_icount += count; |
7540 |
decr = (count > 0xffff) ? 0xffff : count; |
7541 |
count -= decr; |
7542 |
env->icount_decr.u16.low = decr; |
7543 |
env->icount_extra = count; |
7544 |
} |
7545 |
ret = cpu_exec(env); |
7546 |
#ifdef CONFIG_PROFILER
|
7547 |
qemu_time += profile_getclock() - ti; |
7548 |
#endif
|
7549 |
if (use_icount) {
|
7550 |
/* Fold pending instructions back into the
|
7551 |
instruction counter, and clear the interrupt flag. */
|
7552 |
qemu_icount -= (env->icount_decr.u16.low |
7553 |
+ env->icount_extra); |
7554 |
env->icount_decr.u32 = 0;
|
7555 |
env->icount_extra = 0;
|
7556 |
} |
7557 |
next_cpu = env->next_cpu ?: first_cpu; |
7558 |
if (event_pending && likely(ret != EXCP_DEBUG)) {
|
7559 |
ret = EXCP_INTERRUPT; |
7560 |
event_pending = 0;
|
7561 |
break;
|
7562 |
} |
7563 |
if (ret == EXCP_HLT) {
|
7564 |
/* Give the next CPU a chance to run. */
|
7565 |
cur_cpu = env; |
7566 |
continue;
|
7567 |
} |
7568 |
if (ret != EXCP_HALTED)
|
7569 |
break;
|
7570 |
/* all CPUs are halted ? */
|
7571 |
if (env == cur_cpu)
|
7572 |
break;
|
7573 |
} |
7574 |
cur_cpu = env; |
7575 |
|
7576 |
if (shutdown_requested) {
|
7577 |
ret = EXCP_INTERRUPT; |
7578 |
if (no_shutdown) {
|
7579 |
vm_stop(0);
|
7580 |
no_shutdown = 0;
|
7581 |
} |
7582 |
else
|
7583 |
break;
|
7584 |
} |
7585 |
if (reset_requested) {
|
7586 |
reset_requested = 0;
|
7587 |
qemu_system_reset(); |
7588 |
ret = EXCP_INTERRUPT; |
7589 |
} |
7590 |
if (powerdown_requested) {
|
7591 |
powerdown_requested = 0;
|
7592 |
qemu_system_powerdown(); |
7593 |
ret = EXCP_INTERRUPT; |
7594 |
} |
7595 |
if (unlikely(ret == EXCP_DEBUG)) {
|
7596 |
vm_stop(EXCP_DEBUG); |
7597 |
} |
7598 |
/* If all cpus are halted then wait until the next IRQ */
|
7599 |
/* XXX: use timeout computed from timers */
|
7600 |
if (ret == EXCP_HALTED) {
|
7601 |
if (use_icount) {
|
7602 |
int64_t add; |
7603 |
int64_t delta; |
7604 |
/* Advance virtual time to the next event. */
|
7605 |
if (use_icount == 1) { |
7606 |
/* When not using an adaptive execution frequency
|
7607 |
we tend to get badly out of sync with real time,
|
7608 |
so just delay for a reasonable amount of time. */
|
7609 |
delta = 0;
|
7610 |
} else {
|
7611 |
delta = cpu_get_icount() - cpu_get_clock(); |
7612 |
} |
7613 |
if (delta > 0) { |
7614 |
/* If virtual time is ahead of real time then just
|
7615 |
wait for IO. */
|
7616 |
timeout = (delta / 1000000) + 1; |
7617 |
} else {
|
7618 |
/* Wait for either IO to occur or the next
|
7619 |
timer event. */
|
7620 |
add = qemu_next_deadline(); |
7621 |
/* We advance the timer before checking for IO.
|
7622 |
Limit the amount we advance so that early IO
|
7623 |
activity won't get the guest too far ahead. */
|
7624 |
if (add > 10000000) |
7625 |
add = 10000000;
|
7626 |
delta += add; |
7627 |
add = (add + (1 << icount_time_shift) - 1) |
7628 |
>> icount_time_shift; |
7629 |
qemu_icount += add; |
7630 |
timeout = delta / 1000000;
|
7631 |
if (timeout < 0) |
7632 |
timeout = 0;
|
7633 |
} |
7634 |
} else {
|
7635 |
timeout = 10;
|
7636 |
} |
7637 |
} else {
|
7638 |
timeout = 0;
|
7639 |
} |
7640 |
} else {
|
7641 |
if (shutdown_requested)
|
7642 |
break;
|
7643 |
timeout = 10;
|
7644 |
} |
7645 |
#ifdef CONFIG_PROFILER
|
7646 |
ti = profile_getclock(); |
7647 |
#endif
|
7648 |
main_loop_wait(timeout); |
7649 |
#ifdef CONFIG_PROFILER
|
7650 |
dev_time += profile_getclock() - ti; |
7651 |
#endif
|
7652 |
} |
7653 |
cpu_disable_ticks(); |
7654 |
return ret;
|
7655 |
} |
7656 |
|
7657 |
static void help(int exitcode) |
7658 |
{ |
7659 |
printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n" |
7660 |
"usage: %s [options] [disk_image]\n"
|
7661 |
"\n"
|
7662 |
"'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
7663 |
"\n"
|
7664 |
"Standard options:\n"
|
7665 |
"-M machine select emulated machine (-M ? for list)\n"
|
7666 |
"-cpu cpu select CPU (-cpu ? for list)\n"
|
7667 |
"-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
7668 |
"-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
7669 |
"-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
7670 |
"-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
7671 |
"-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
|
7672 |
" [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
|
7673 |
" [,cache=on|off][,format=f]\n"
|
7674 |
" use 'file' as a drive image\n"
|
7675 |
"-mtdblock file use 'file' as on-board Flash memory image\n"
|
7676 |
"-sd file use 'file' as SecureDigital card image\n"
|
7677 |
"-pflash file use 'file' as a parallel flash image\n"
|
7678 |
"-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
|
7679 |
"-snapshot write to temporary files instead of disk image files\n"
|
7680 |
#ifdef CONFIG_SDL
|
7681 |
"-no-frame open SDL window without a frame and window decorations\n"
|
7682 |
"-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
|
7683 |
"-no-quit disable SDL window close capability\n"
|
7684 |
#endif
|
7685 |
#ifdef TARGET_I386
|
7686 |
"-no-fd-bootchk disable boot signature checking for floppy disks\n"
|
7687 |
#endif
|
7688 |
"-m megs set virtual RAM size to megs MB [default=%d]\n"
|
7689 |
"-smp n set the number of CPUs to 'n' [default=1]\n"
|
7690 |
"-nographic disable graphical output and redirect serial I/Os to console\n"
|
7691 |
"-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
|
7692 |
#ifndef _WIN32
|
7693 |
"-k language use keyboard layout (for example \"fr\" for French)\n"
|
7694 |
#endif
|
7695 |
#ifdef HAS_AUDIO
|
7696 |
"-audio-help print list of audio drivers and their options\n"
|
7697 |
"-soundhw c1,... enable audio support\n"
|
7698 |
" and only specified sound cards (comma separated list)\n"
|
7699 |
" use -soundhw ? to get the list of supported cards\n"
|
7700 |
" use -soundhw all to enable all of them\n"
|
7701 |
#endif
|
7702 |
"-localtime set the real time clock to local time [default=utc]\n"
|
7703 |
"-full-screen start in full screen\n"
|
7704 |
#ifdef TARGET_I386
|
7705 |
"-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
|
7706 |
#endif
|
7707 |
"-usb enable the USB driver (will be the default soon)\n"
|
7708 |
"-usbdevice name add the host or guest USB device 'name'\n"
|
7709 |
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7710 |
"-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
|
7711 |
#endif
|
7712 |
"-name string set the name of the guest\n"
|
7713 |
"\n"
|
7714 |
"Network options:\n"
|
7715 |
"-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
|
7716 |
" create a new Network Interface Card and connect it to VLAN 'n'\n"
|
7717 |
#ifdef CONFIG_SLIRP
|
7718 |
"-net user[,vlan=n][,hostname=host]\n"
|
7719 |
" connect the user mode network stack to VLAN 'n' and send\n"
|
7720 |
" hostname 'host' to DHCP clients\n"
|
7721 |
#endif
|
7722 |
#ifdef _WIN32
|
7723 |
"-net tap[,vlan=n],ifname=name\n"
|
7724 |
" connect the host TAP network interface to VLAN 'n'\n"
|
7725 |
#else
|
7726 |
"-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
|
7727 |
" connect the host TAP network interface to VLAN 'n' and use the\n"
|
7728 |
" network scripts 'file' (default=%s)\n"
|
7729 |
" and 'dfile' (default=%s);\n"
|
7730 |
" use '[down]script=no' to disable script execution;\n"
|
7731 |
" use 'fd=h' to connect to an already opened TAP interface\n"
|
7732 |
#endif
|
7733 |
"-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
|
7734 |
" connect the vlan 'n' to another VLAN using a socket connection\n"
|
7735 |
"-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
|
7736 |
" connect the vlan 'n' to multicast maddr and port\n"
|
7737 |
#ifdef CONFIG_VDE
|
7738 |
"-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
|
7739 |
" connect the vlan 'n' to port 'n' of a vde switch running\n"
|
7740 |
" on host and listening for incoming connections on 'socketpath'.\n"
|
7741 |
" Use group 'groupname' and mode 'octalmode' to change default\n"
|
7742 |
" ownership and permissions for communication port.\n"
|
7743 |
#endif
|
7744 |
"-net none use it alone to have zero network devices; if no -net option\n"
|
7745 |
" is provided, the default is '-net nic -net user'\n"
|
7746 |
"\n"
|
7747 |
#ifdef CONFIG_SLIRP
|
7748 |
"-tftp dir allow tftp access to files in dir [-net user]\n"
|
7749 |
"-bootp file advertise file in BOOTP replies\n"
|
7750 |
#ifndef _WIN32
|
7751 |
"-smb dir allow SMB access to files in 'dir' [-net user]\n"
|
7752 |
#endif
|
7753 |
"-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
7754 |
" redirect TCP or UDP connections from host to guest [-net user]\n"
|
7755 |
#endif
|
7756 |
"\n"
|
7757 |
"Linux boot specific:\n"
|
7758 |
"-kernel bzImage use 'bzImage' as kernel image\n"
|
7759 |
"-append cmdline use 'cmdline' as kernel command line\n"
|
7760 |
"-initrd file use 'file' as initial ram disk\n"
|
7761 |
"\n"
|
7762 |
"Debug/Expert options:\n"
|
7763 |
"-monitor dev redirect the monitor to char device 'dev'\n"
|
7764 |
"-serial dev redirect the serial port to char device 'dev'\n"
|
7765 |
"-parallel dev redirect the parallel port to char device 'dev'\n"
|
7766 |
"-pidfile file Write PID to 'file'\n"
|
7767 |
"-S freeze CPU at startup (use 'c' to start execution)\n"
|
7768 |
"-s wait gdb connection to port\n"
|
7769 |
"-p port set gdb connection port [default=%s]\n"
|
7770 |
"-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
7771 |
"-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
7772 |
" translation (t=none or lba) (usually qemu can guess them)\n"
|
7773 |
"-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
|
7774 |
#ifdef USE_KQEMU
|
7775 |
"-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
|
7776 |
"-no-kqemu disable KQEMU kernel module usage\n"
|
7777 |
#endif
|
7778 |
#ifdef TARGET_I386
|
7779 |
"-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
7780 |
" (default is CL-GD5446 PCI VGA)\n"
|
7781 |
"-no-acpi disable ACPI\n"
|
7782 |
#endif
|
7783 |
#ifdef CONFIG_CURSES
|
7784 |
"-curses use a curses/ncurses interface instead of SDL\n"
|
7785 |
#endif
|
7786 |
"-no-reboot exit instead of rebooting\n"
|
7787 |
"-no-shutdown stop before shutdown\n"
|
7788 |
"-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
|
7789 |
"-vnc display start a VNC server on display\n"
|
7790 |
#ifndef _WIN32
|
7791 |
"-daemonize daemonize QEMU after initializing\n"
|
7792 |
#endif
|
7793 |
"-option-rom rom load a file, rom, into the option ROM space\n"
|
7794 |
#ifdef TARGET_SPARC
|
7795 |
"-prom-env variable=value set OpenBIOS nvram variables\n"
|
7796 |
#endif
|
7797 |
"-clock force the use of the given methods for timer alarm.\n"
|
7798 |
" To see what timers are available use -clock ?\n"
|
7799 |
"-startdate select initial date of the clock\n"
|
7800 |
"-icount [N|auto]\n"
|
7801 |
" Enable virtual instruction counter with 2^N clock ticks per instruction\n"
|
7802 |
"\n"
|
7803 |
"During emulation, the following keys are useful:\n"
|
7804 |
"ctrl-alt-f toggle full screen\n"
|
7805 |
"ctrl-alt-n switch to virtual console 'n'\n"
|
7806 |
"ctrl-alt toggle mouse and keyboard grab\n"
|
7807 |
"\n"
|
7808 |
"When using -nographic, press 'ctrl-a h' to get some help.\n"
|
7809 |
, |
7810 |
"qemu",
|
7811 |
DEFAULT_RAM_SIZE, |
7812 |
#ifndef _WIN32
|
7813 |
DEFAULT_NETWORK_SCRIPT, |
7814 |
DEFAULT_NETWORK_DOWN_SCRIPT, |
7815 |
#endif
|
7816 |
DEFAULT_GDBSTUB_PORT, |
7817 |
"/tmp/qemu.log");
|
7818 |
exit(exitcode); |
7819 |
} |
7820 |
|
7821 |
#define HAS_ARG 0x0001 |
7822 |
|
7823 |
enum {
|
7824 |
QEMU_OPTION_h, |
7825 |
|
7826 |
QEMU_OPTION_M, |
7827 |
QEMU_OPTION_cpu, |
7828 |
QEMU_OPTION_fda, |
7829 |
QEMU_OPTION_fdb, |
7830 |
QEMU_OPTION_hda, |
7831 |
QEMU_OPTION_hdb, |
7832 |
QEMU_OPTION_hdc, |
7833 |
QEMU_OPTION_hdd, |
7834 |
QEMU_OPTION_drive, |
7835 |
QEMU_OPTION_cdrom, |
7836 |
QEMU_OPTION_mtdblock, |
7837 |
QEMU_OPTION_sd, |
7838 |
QEMU_OPTION_pflash, |
7839 |
QEMU_OPTION_boot, |
7840 |
QEMU_OPTION_snapshot, |
7841 |
#ifdef TARGET_I386
|
7842 |
QEMU_OPTION_no_fd_bootchk, |
7843 |
#endif
|
7844 |
QEMU_OPTION_m, |
7845 |
QEMU_OPTION_nographic, |
7846 |
QEMU_OPTION_portrait, |
7847 |
#ifdef HAS_AUDIO
|
7848 |
QEMU_OPTION_audio_help, |
7849 |
QEMU_OPTION_soundhw, |
7850 |
#endif
|
7851 |
|
7852 |
QEMU_OPTION_net, |
7853 |
QEMU_OPTION_tftp, |
7854 |
QEMU_OPTION_bootp, |
7855 |
QEMU_OPTION_smb, |
7856 |
QEMU_OPTION_redir, |
7857 |
|
7858 |
QEMU_OPTION_kernel, |
7859 |
QEMU_OPTION_append, |
7860 |
QEMU_OPTION_initrd, |
7861 |
|
7862 |
QEMU_OPTION_S, |
7863 |
QEMU_OPTION_s, |
7864 |
QEMU_OPTION_p, |
7865 |
QEMU_OPTION_d, |
7866 |
QEMU_OPTION_hdachs, |
7867 |
QEMU_OPTION_L, |
7868 |
QEMU_OPTION_bios, |
7869 |
QEMU_OPTION_k, |
7870 |
QEMU_OPTION_localtime, |
7871 |
QEMU_OPTION_cirrusvga, |
7872 |
QEMU_OPTION_vmsvga, |
7873 |
QEMU_OPTION_g, |
7874 |
QEMU_OPTION_std_vga, |
7875 |
QEMU_OPTION_echr, |
7876 |
QEMU_OPTION_monitor, |
7877 |
QEMU_OPTION_serial, |
7878 |
QEMU_OPTION_parallel, |
7879 |
QEMU_OPTION_loadvm, |
7880 |
QEMU_OPTION_full_screen, |
7881 |
QEMU_OPTION_no_frame, |
7882 |
QEMU_OPTION_alt_grab, |
7883 |
QEMU_OPTION_no_quit, |
7884 |
QEMU_OPTION_pidfile, |
7885 |
QEMU_OPTION_no_kqemu, |
7886 |
QEMU_OPTION_kernel_kqemu, |
7887 |
QEMU_OPTION_win2k_hack, |
7888 |
QEMU_OPTION_usb, |
7889 |
QEMU_OPTION_usbdevice, |
7890 |
QEMU_OPTION_smp, |
7891 |
QEMU_OPTION_vnc, |
7892 |
QEMU_OPTION_no_acpi, |
7893 |
QEMU_OPTION_curses, |
7894 |
QEMU_OPTION_no_reboot, |
7895 |
QEMU_OPTION_no_shutdown, |
7896 |
QEMU_OPTION_show_cursor, |
7897 |
QEMU_OPTION_daemonize, |
7898 |
QEMU_OPTION_option_rom, |
7899 |
QEMU_OPTION_semihosting, |
7900 |
QEMU_OPTION_name, |
7901 |
QEMU_OPTION_prom_env, |
7902 |
QEMU_OPTION_old_param, |
7903 |
QEMU_OPTION_clock, |
7904 |
QEMU_OPTION_startdate, |
7905 |
QEMU_OPTION_tb_size, |
7906 |
QEMU_OPTION_icount, |
7907 |
}; |
7908 |
|
7909 |
typedef struct QEMUOption { |
7910 |
const char *name; |
7911 |
int flags;
|
7912 |
int index;
|
7913 |
} QEMUOption; |
7914 |
|
7915 |
const QEMUOption qemu_options[] = {
|
7916 |
{ "h", 0, QEMU_OPTION_h }, |
7917 |
{ "help", 0, QEMU_OPTION_h }, |
7918 |
|
7919 |
{ "M", HAS_ARG, QEMU_OPTION_M },
|
7920 |
{ "cpu", HAS_ARG, QEMU_OPTION_cpu },
|
7921 |
{ "fda", HAS_ARG, QEMU_OPTION_fda },
|
7922 |
{ "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
7923 |
{ "hda", HAS_ARG, QEMU_OPTION_hda },
|
7924 |
{ "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
7925 |
{ "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
7926 |
{ "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
7927 |
{ "drive", HAS_ARG, QEMU_OPTION_drive },
|
7928 |
{ "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
7929 |
{ "mtdblock", HAS_ARG, QEMU_OPTION_mtdblock },
|
7930 |
{ "sd", HAS_ARG, QEMU_OPTION_sd },
|
7931 |
{ "pflash", HAS_ARG, QEMU_OPTION_pflash },
|
7932 |
{ "boot", HAS_ARG, QEMU_OPTION_boot },
|
7933 |
{ "snapshot", 0, QEMU_OPTION_snapshot }, |
7934 |
#ifdef TARGET_I386
|
7935 |
{ "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk }, |
7936 |
#endif
|
7937 |
{ "m", HAS_ARG, QEMU_OPTION_m },
|
7938 |
{ "nographic", 0, QEMU_OPTION_nographic }, |
7939 |
{ "portrait", 0, QEMU_OPTION_portrait }, |
7940 |
{ "k", HAS_ARG, QEMU_OPTION_k },
|
7941 |
#ifdef HAS_AUDIO
|
7942 |
{ "audio-help", 0, QEMU_OPTION_audio_help }, |
7943 |
{ "soundhw", HAS_ARG, QEMU_OPTION_soundhw },
|
7944 |
#endif
|
7945 |
|
7946 |
{ "net", HAS_ARG, QEMU_OPTION_net},
|
7947 |
#ifdef CONFIG_SLIRP
|
7948 |
{ "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
7949 |
{ "bootp", HAS_ARG, QEMU_OPTION_bootp },
|
7950 |
#ifndef _WIN32
|
7951 |
{ "smb", HAS_ARG, QEMU_OPTION_smb },
|
7952 |
#endif
|
7953 |
{ "redir", HAS_ARG, QEMU_OPTION_redir },
|
7954 |
#endif
|
7955 |
|
7956 |
{ "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
7957 |
{ "append", HAS_ARG, QEMU_OPTION_append },
|
7958 |
{ "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
7959 |
|
7960 |
{ "S", 0, QEMU_OPTION_S }, |
7961 |
{ "s", 0, QEMU_OPTION_s }, |
7962 |
{ "p", HAS_ARG, QEMU_OPTION_p },
|
7963 |
{ "d", HAS_ARG, QEMU_OPTION_d },
|
7964 |
{ "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
7965 |
{ "L", HAS_ARG, QEMU_OPTION_L },
|
7966 |
{ "bios", HAS_ARG, QEMU_OPTION_bios },
|
7967 |
#ifdef USE_KQEMU
|
7968 |
{ "no-kqemu", 0, QEMU_OPTION_no_kqemu }, |
7969 |
{ "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, |
7970 |
#endif
|
7971 |
#if defined(TARGET_PPC) || defined(TARGET_SPARC)
|
7972 |
{ "g", 1, QEMU_OPTION_g }, |
7973 |
#endif
|
7974 |
{ "localtime", 0, QEMU_OPTION_localtime }, |
7975 |
{ "std-vga", 0, QEMU_OPTION_std_vga }, |
7976 |
{ "echr", HAS_ARG, QEMU_OPTION_echr },
|
7977 |
{ "monitor", HAS_ARG, QEMU_OPTION_monitor },
|
7978 |
{ "serial", HAS_ARG, QEMU_OPTION_serial },
|
7979 |
{ "parallel", HAS_ARG, QEMU_OPTION_parallel },
|
7980 |
{ "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
7981 |
{ "full-screen", 0, QEMU_OPTION_full_screen }, |
7982 |
#ifdef CONFIG_SDL
|
7983 |
{ "no-frame", 0, QEMU_OPTION_no_frame }, |
7984 |
{ "alt-grab", 0, QEMU_OPTION_alt_grab }, |
7985 |
{ "no-quit", 0, QEMU_OPTION_no_quit }, |
7986 |
#endif
|
7987 |
{ "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
7988 |
{ "win2k-hack", 0, QEMU_OPTION_win2k_hack }, |
7989 |
{ "usbdevice", HAS_ARG, QEMU_OPTION_usbdevice },
|
7990 |
{ "smp", HAS_ARG, QEMU_OPTION_smp },
|
7991 |
{ "vnc", HAS_ARG, QEMU_OPTION_vnc },
|
7992 |
#ifdef CONFIG_CURSES
|
7993 |
{ "curses", 0, QEMU_OPTION_curses }, |
7994 |
#endif
|
7995 |
|
7996 |
/* temporary options */
|
7997 |
{ "usb", 0, QEMU_OPTION_usb }, |
7998 |
{ "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
7999 |
{ "vmwarevga", 0, QEMU_OPTION_vmsvga }, |
8000 |
{ "no-acpi", 0, QEMU_OPTION_no_acpi }, |
8001 |
{ "no-reboot", 0, QEMU_OPTION_no_reboot }, |
8002 |
{ "no-shutdown", 0, QEMU_OPTION_no_shutdown }, |
8003 |
{ "show-cursor", 0, QEMU_OPTION_show_cursor }, |
8004 |
{ "daemonize", 0, QEMU_OPTION_daemonize }, |
8005 |
{ "option-rom", HAS_ARG, QEMU_OPTION_option_rom },
|
8006 |
#if defined(TARGET_ARM) || defined(TARGET_M68K)
|
8007 |
{ "semihosting", 0, QEMU_OPTION_semihosting }, |
8008 |
#endif
|
8009 |
{ "name", HAS_ARG, QEMU_OPTION_name },
|
8010 |
#if defined(TARGET_SPARC)
|
8011 |
{ "prom-env", HAS_ARG, QEMU_OPTION_prom_env },
|
8012 |
#endif
|
8013 |
#if defined(TARGET_ARM)
|
8014 |
{ "old-param", 0, QEMU_OPTION_old_param }, |
8015 |
#endif
|
8016 |
{ "clock", HAS_ARG, QEMU_OPTION_clock },
|
8017 |
{ "startdate", HAS_ARG, QEMU_OPTION_startdate },
|
8018 |
{ "tb-size", HAS_ARG, QEMU_OPTION_tb_size },
|
8019 |
{ "icount", HAS_ARG, QEMU_OPTION_icount },
|
8020 |
{ NULL },
|
8021 |
}; |
8022 |
|
8023 |
/* password input */
|
8024 |
|
8025 |
int qemu_key_check(BlockDriverState *bs, const char *name) |
8026 |
{ |
8027 |
char password[256]; |
8028 |
int i;
|
8029 |
|
8030 |
if (!bdrv_is_encrypted(bs))
|
8031 |
return 0; |
8032 |
|
8033 |
term_printf("%s is encrypted.\n", name);
|
8034 |
for(i = 0; i < 3; i++) { |
8035 |
monitor_readline("Password: ", 1, password, sizeof(password)); |
8036 |
if (bdrv_set_key(bs, password) == 0) |
8037 |
return 0; |
8038 |
term_printf("invalid password\n");
|
8039 |
} |
8040 |
return -EPERM;
|
8041 |
} |
8042 |
|
8043 |
static BlockDriverState *get_bdrv(int index) |
8044 |
{ |
8045 |
if (index > nb_drives)
|
8046 |
return NULL; |
8047 |
return drives_table[index].bdrv;
|
8048 |
} |
8049 |
|
8050 |
static void read_passwords(void) |
8051 |
{ |
8052 |
BlockDriverState *bs; |
8053 |
int i;
|
8054 |
|
8055 |
for(i = 0; i < 6; i++) { |
8056 |
bs = get_bdrv(i); |
8057 |
if (bs)
|
8058 |
qemu_key_check(bs, bdrv_get_device_name(bs)); |
8059 |
} |
8060 |
} |
8061 |
|
8062 |
#ifdef HAS_AUDIO
|
8063 |
struct soundhw soundhw[] = {
|
8064 |
#ifdef HAS_AUDIO_CHOICE
|
8065 |
#if defined(TARGET_I386) || defined(TARGET_MIPS)
|
8066 |
{ |
8067 |
"pcspk",
|
8068 |
"PC speaker",
|
8069 |
0,
|
8070 |
1,
|
8071 |
{ .init_isa = pcspk_audio_init } |
8072 |
}, |
8073 |
#endif
|
8074 |
{ |
8075 |
"sb16",
|
8076 |
"Creative Sound Blaster 16",
|
8077 |
0,
|
8078 |
1,
|
8079 |
{ .init_isa = SB16_init } |
8080 |
}, |
8081 |
|
8082 |
#ifdef CONFIG_CS4231A
|
8083 |
{ |
8084 |
"cs4231a",
|
8085 |
"CS4231A",
|
8086 |
0,
|
8087 |
1,
|
8088 |
{ .init_isa = cs4231a_init } |
8089 |
}, |
8090 |
#endif
|
8091 |
|
8092 |
#ifdef CONFIG_ADLIB
|
8093 |
{ |
8094 |
"adlib",
|
8095 |
#ifdef HAS_YMF262
|
8096 |
"Yamaha YMF262 (OPL3)",
|
8097 |
#else
|
8098 |
"Yamaha YM3812 (OPL2)",
|
8099 |
#endif
|
8100 |
0,
|
8101 |
1,
|
8102 |
{ .init_isa = Adlib_init } |
8103 |
}, |
8104 |
#endif
|
8105 |
|
8106 |
#ifdef CONFIG_GUS
|
8107 |
{ |
8108 |
"gus",
|
8109 |
"Gravis Ultrasound GF1",
|
8110 |
0,
|
8111 |
1,
|
8112 |
{ .init_isa = GUS_init } |
8113 |
}, |
8114 |
#endif
|
8115 |
|
8116 |
#ifdef CONFIG_AC97
|
8117 |
{ |
8118 |
"ac97",
|
8119 |
"Intel 82801AA AC97 Audio",
|
8120 |
0,
|
8121 |
0,
|
8122 |
{ .init_pci = ac97_init } |
8123 |
}, |
8124 |
#endif
|
8125 |
|
8126 |
{ |
8127 |
"es1370",
|
8128 |
"ENSONIQ AudioPCI ES1370",
|
8129 |
0,
|
8130 |
0,
|
8131 |
{ .init_pci = es1370_init } |
8132 |
}, |
8133 |
#endif
|
8134 |
|
8135 |
{ NULL, NULL, 0, 0, { NULL } } |
8136 |
}; |
8137 |
|
8138 |
static void select_soundhw (const char *optarg) |
8139 |
{ |
8140 |
struct soundhw *c;
|
8141 |
|
8142 |
if (*optarg == '?') { |
8143 |
show_valid_cards:
|
8144 |
|
8145 |
printf ("Valid sound card names (comma separated):\n");
|
8146 |
for (c = soundhw; c->name; ++c) {
|
8147 |
printf ("%-11s %s\n", c->name, c->descr);
|
8148 |
} |
8149 |
printf ("\n-soundhw all will enable all of the above\n");
|
8150 |
exit (*optarg != '?');
|
8151 |
} |
8152 |
else {
|
8153 |
size_t l; |
8154 |
const char *p; |
8155 |
char *e;
|
8156 |
int bad_card = 0; |
8157 |
|
8158 |
if (!strcmp (optarg, "all")) { |
8159 |
for (c = soundhw; c->name; ++c) {
|
8160 |
c->enabled = 1;
|
8161 |
} |
8162 |
return;
|
8163 |
} |
8164 |
|
8165 |
p = optarg; |
8166 |
while (*p) {
|
8167 |
e = strchr (p, ',');
|
8168 |
l = !e ? strlen (p) : (size_t) (e - p); |
8169 |
|
8170 |
for (c = soundhw; c->name; ++c) {
|
8171 |
if (!strncmp (c->name, p, l)) {
|
8172 |
c->enabled = 1;
|
8173 |
break;
|
8174 |
} |
8175 |
} |
8176 |
|
8177 |
if (!c->name) {
|
8178 |
if (l > 80) { |
8179 |
fprintf (stderr, |
8180 |
"Unknown sound card name (too big to show)\n");
|
8181 |
} |
8182 |
else {
|
8183 |
fprintf (stderr, "Unknown sound card name `%.*s'\n",
|
8184 |
(int) l, p);
|
8185 |
} |
8186 |
bad_card = 1;
|
8187 |
} |
8188 |
p += l + (e != NULL);
|
8189 |
} |
8190 |
|
8191 |
if (bad_card)
|
8192 |
goto show_valid_cards;
|
8193 |
} |
8194 |
} |
8195 |
#endif
|
8196 |
|
8197 |
#ifdef _WIN32
|
8198 |
static BOOL WINAPI qemu_ctrl_handler(DWORD type)
|
8199 |
{ |
8200 |
exit(STATUS_CONTROL_C_EXIT); |
8201 |
return TRUE;
|
8202 |
} |
8203 |
#endif
|
8204 |
|
8205 |
#define MAX_NET_CLIENTS 32 |
8206 |
|
8207 |
#ifndef _WIN32
|
8208 |
|
8209 |
static void termsig_handler(int signal) |
8210 |
{ |
8211 |
qemu_system_shutdown_request(); |
8212 |
} |
8213 |
|
8214 |
void termsig_setup(void) |
8215 |
{ |
8216 |
struct sigaction act;
|
8217 |
|
8218 |
memset(&act, 0, sizeof(act)); |
8219 |
act.sa_handler = termsig_handler; |
8220 |
sigaction(SIGINT, &act, NULL);
|
8221 |
sigaction(SIGHUP, &act, NULL);
|
8222 |
sigaction(SIGTERM, &act, NULL);
|
8223 |
} |
8224 |
|
8225 |
#endif
|
8226 |
|
8227 |
int main(int argc, char **argv) |
8228 |
{ |
8229 |
#ifdef CONFIG_GDBSTUB
|
8230 |
int use_gdbstub;
|
8231 |
const char *gdbstub_port; |
8232 |
#endif
|
8233 |
uint32_t boot_devices_bitmap = 0;
|
8234 |
int i;
|
8235 |
int snapshot, linux_boot, net_boot;
|
8236 |
const char *initrd_filename; |
8237 |
const char *kernel_filename, *kernel_cmdline; |
8238 |
const char *boot_devices = ""; |
8239 |
DisplayState *ds = &display_state; |
8240 |
int cyls, heads, secs, translation;
|
8241 |
const char *net_clients[MAX_NET_CLIENTS]; |
8242 |
int nb_net_clients;
|
8243 |
int hda_index;
|
8244 |
int optind;
|
8245 |
const char *r, *optarg; |
8246 |
CharDriverState *monitor_hd; |
8247 |
const char *monitor_device; |
8248 |
const char *serial_devices[MAX_SERIAL_PORTS]; |
8249 |
int serial_device_index;
|
8250 |
const char *parallel_devices[MAX_PARALLEL_PORTS]; |
8251 |
int parallel_device_index;
|
8252 |
const char *loadvm = NULL; |
8253 |
QEMUMachine *machine; |
8254 |
const char *cpu_model; |
8255 |
const char *usb_devices[MAX_USB_CMDLINE]; |
8256 |
int usb_devices_index;
|
8257 |
int fds[2]; |
8258 |
int tb_size;
|
8259 |
const char *pid_file = NULL; |
8260 |
VLANState *vlan; |
8261 |
|
8262 |
LIST_INIT (&vm_change_state_head); |
8263 |
#ifndef _WIN32
|
8264 |
{ |
8265 |
struct sigaction act;
|
8266 |
sigfillset(&act.sa_mask); |
8267 |
act.sa_flags = 0;
|
8268 |
act.sa_handler = SIG_IGN; |
8269 |
sigaction(SIGPIPE, &act, NULL);
|
8270 |
} |
8271 |
#else
|
8272 |
SetConsoleCtrlHandler(qemu_ctrl_handler, TRUE); |
8273 |
/* Note: cpu_interrupt() is currently not SMP safe, so we force
|
8274 |
QEMU to run on a single CPU */
|
8275 |
{ |
8276 |
HANDLE h; |
8277 |
DWORD mask, smask; |
8278 |
int i;
|
8279 |
h = GetCurrentProcess(); |
8280 |
if (GetProcessAffinityMask(h, &mask, &smask)) {
|
8281 |
for(i = 0; i < 32; i++) { |
8282 |
if (mask & (1 << i)) |
8283 |
break;
|
8284 |
} |
8285 |
if (i != 32) { |
8286 |
mask = 1 << i;
|
8287 |
SetProcessAffinityMask(h, mask); |
8288 |
} |
8289 |
} |
8290 |
} |
8291 |
#endif
|
8292 |
|
8293 |
register_machines(); |
8294 |
machine = first_machine; |
8295 |
cpu_model = NULL;
|
8296 |
initrd_filename = NULL;
|
8297 |
ram_size = 0;
|
8298 |
vga_ram_size = VGA_RAM_SIZE; |
8299 |
#ifdef CONFIG_GDBSTUB
|
8300 |
use_gdbstub = 0;
|
8301 |
gdbstub_port = DEFAULT_GDBSTUB_PORT; |
8302 |
#endif
|
8303 |
snapshot = 0;
|
8304 |
nographic = 0;
|
8305 |
curses = 0;
|
8306 |
kernel_filename = NULL;
|
8307 |
kernel_cmdline = "";
|
8308 |
cyls = heads = secs = 0;
|
8309 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
8310 |
monitor_device = "vc";
|
8311 |
|
8312 |
serial_devices[0] = "vc:80Cx24C"; |
8313 |
for(i = 1; i < MAX_SERIAL_PORTS; i++) |
8314 |
serial_devices[i] = NULL;
|
8315 |
serial_device_index = 0;
|
8316 |
|
8317 |
parallel_devices[0] = "vc:640x480"; |
8318 |
for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
8319 |
parallel_devices[i] = NULL;
|
8320 |
parallel_device_index = 0;
|
8321 |
|
8322 |
usb_devices_index = 0;
|
8323 |
|
8324 |
nb_net_clients = 0;
|
8325 |
nb_drives = 0;
|
8326 |
nb_drives_opt = 0;
|
8327 |
hda_index = -1;
|
8328 |
|
8329 |
nb_nics = 0;
|
8330 |
|
8331 |
tb_size = 0;
|
8332 |
|
8333 |
optind = 1;
|
8334 |
for(;;) {
|
8335 |
if (optind >= argc)
|
8336 |
break;
|
8337 |
r = argv[optind]; |
8338 |
if (r[0] != '-') { |
8339 |
hda_index = drive_add(argv[optind++], HD_ALIAS, 0);
|
8340 |
} else {
|
8341 |
const QEMUOption *popt;
|
8342 |
|
8343 |
optind++; |
8344 |
/* Treat --foo the same as -foo. */
|
8345 |
if (r[1] == '-') |
8346 |
r++; |
8347 |
popt = qemu_options; |
8348 |
for(;;) {
|
8349 |
if (!popt->name) {
|
8350 |
fprintf(stderr, "%s: invalid option -- '%s'\n",
|
8351 |
argv[0], r);
|
8352 |
exit(1);
|
8353 |
} |
8354 |
if (!strcmp(popt->name, r + 1)) |
8355 |
break;
|
8356 |
popt++; |
8357 |
} |
8358 |
if (popt->flags & HAS_ARG) {
|
8359 |
if (optind >= argc) {
|
8360 |
fprintf(stderr, "%s: option '%s' requires an argument\n",
|
8361 |
argv[0], r);
|
8362 |
exit(1);
|
8363 |
} |
8364 |
optarg = argv[optind++]; |
8365 |
} else {
|
8366 |
optarg = NULL;
|
8367 |
} |
8368 |
|
8369 |
switch(popt->index) {
|
8370 |
case QEMU_OPTION_M:
|
8371 |
machine = find_machine(optarg); |
8372 |
if (!machine) {
|
8373 |
QEMUMachine *m; |
8374 |
printf("Supported machines are:\n");
|
8375 |
for(m = first_machine; m != NULL; m = m->next) { |
8376 |
printf("%-10s %s%s\n",
|
8377 |
m->name, m->desc, |
8378 |
m == first_machine ? " (default)" : ""); |
8379 |
} |
8380 |
exit(*optarg != '?');
|
8381 |
} |
8382 |
break;
|
8383 |
case QEMU_OPTION_cpu:
|
8384 |
/* hw initialization will check this */
|
8385 |
if (*optarg == '?') { |
8386 |
/* XXX: implement xxx_cpu_list for targets that still miss it */
|
8387 |
#if defined(cpu_list)
|
8388 |
cpu_list(stdout, &fprintf); |
8389 |
#endif
|
8390 |
exit(0);
|
8391 |
} else {
|
8392 |
cpu_model = optarg; |
8393 |
} |
8394 |
break;
|
8395 |
case QEMU_OPTION_initrd:
|
8396 |
initrd_filename = optarg; |
8397 |
break;
|
8398 |
case QEMU_OPTION_hda:
|
8399 |
if (cyls == 0) |
8400 |
hda_index = drive_add(optarg, HD_ALIAS, 0);
|
8401 |
else
|
8402 |
hda_index = drive_add(optarg, HD_ALIAS |
8403 |
",cyls=%d,heads=%d,secs=%d%s",
|
8404 |
0, cyls, heads, secs,
|
8405 |
translation == BIOS_ATA_TRANSLATION_LBA ? |
8406 |
",trans=lba" :
|
8407 |
translation == BIOS_ATA_TRANSLATION_NONE ? |
8408 |
",trans=none" : ""); |
8409 |
break;
|
8410 |
case QEMU_OPTION_hdb:
|
8411 |
case QEMU_OPTION_hdc:
|
8412 |
case QEMU_OPTION_hdd:
|
8413 |
drive_add(optarg, HD_ALIAS, popt->index - QEMU_OPTION_hda); |
8414 |
break;
|
8415 |
case QEMU_OPTION_drive:
|
8416 |
drive_add(NULL, "%s", optarg); |
8417 |
break;
|
8418 |
case QEMU_OPTION_mtdblock:
|
8419 |
drive_add(optarg, MTD_ALIAS); |
8420 |
break;
|
8421 |
case QEMU_OPTION_sd:
|
8422 |
drive_add(optarg, SD_ALIAS); |
8423 |
break;
|
8424 |
case QEMU_OPTION_pflash:
|
8425 |
drive_add(optarg, PFLASH_ALIAS); |
8426 |
break;
|
8427 |
case QEMU_OPTION_snapshot:
|
8428 |
snapshot = 1;
|
8429 |
break;
|
8430 |
case QEMU_OPTION_hdachs:
|
8431 |
{ |
8432 |
const char *p; |
8433 |
p = optarg; |
8434 |
cyls = strtol(p, (char **)&p, 0); |
8435 |
if (cyls < 1 || cyls > 16383) |
8436 |
goto chs_fail;
|
8437 |
if (*p != ',') |
8438 |
goto chs_fail;
|
8439 |
p++; |
8440 |
heads = strtol(p, (char **)&p, 0); |
8441 |
if (heads < 1 || heads > 16) |
8442 |
goto chs_fail;
|
8443 |
if (*p != ',') |
8444 |
goto chs_fail;
|
8445 |
p++; |
8446 |
secs = strtol(p, (char **)&p, 0); |
8447 |
if (secs < 1 || secs > 63) |
8448 |
goto chs_fail;
|
8449 |
if (*p == ',') { |
8450 |
p++; |
8451 |
if (!strcmp(p, "none")) |
8452 |
translation = BIOS_ATA_TRANSLATION_NONE; |
8453 |
else if (!strcmp(p, "lba")) |
8454 |
translation = BIOS_ATA_TRANSLATION_LBA; |
8455 |
else if (!strcmp(p, "auto")) |
8456 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
8457 |
else
|
8458 |
goto chs_fail;
|
8459 |
} else if (*p != '\0') { |
8460 |
chs_fail:
|
8461 |
fprintf(stderr, "qemu: invalid physical CHS format\n");
|
8462 |
exit(1);
|
8463 |
} |
8464 |
if (hda_index != -1) |
8465 |
snprintf(drives_opt[hda_index].opt, |
8466 |
sizeof(drives_opt[hda_index].opt),
|
8467 |
HD_ALIAS ",cyls=%d,heads=%d,secs=%d%s",
|
8468 |
0, cyls, heads, secs,
|
8469 |
translation == BIOS_ATA_TRANSLATION_LBA ? |
8470 |
",trans=lba" :
|
8471 |
translation == BIOS_ATA_TRANSLATION_NONE ? |
8472 |
",trans=none" : ""); |
8473 |
} |
8474 |
break;
|
8475 |
case QEMU_OPTION_nographic:
|
8476 |
nographic = 1;
|
8477 |
break;
|
8478 |
#ifdef CONFIG_CURSES
|
8479 |
case QEMU_OPTION_curses:
|
8480 |
curses = 1;
|
8481 |
break;
|
8482 |
#endif
|
8483 |
case QEMU_OPTION_portrait:
|
8484 |
graphic_rotate = 1;
|
8485 |
break;
|
8486 |
case QEMU_OPTION_kernel:
|
8487 |
kernel_filename = optarg; |
8488 |
break;
|
8489 |
case QEMU_OPTION_append:
|
8490 |
kernel_cmdline = optarg; |
8491 |
break;
|
8492 |
case QEMU_OPTION_cdrom:
|
8493 |
drive_add(optarg, CDROM_ALIAS); |
8494 |
break;
|
8495 |
case QEMU_OPTION_boot:
|
8496 |
boot_devices = optarg; |
8497 |
/* We just do some generic consistency checks */
|
8498 |
{ |
8499 |
/* Could easily be extended to 64 devices if needed */
|
8500 |
const char *p; |
8501 |
|
8502 |
boot_devices_bitmap = 0;
|
8503 |
for (p = boot_devices; *p != '\0'; p++) { |
8504 |
/* Allowed boot devices are:
|
8505 |
* a b : floppy disk drives
|
8506 |
* c ... f : IDE disk drives
|
8507 |
* g ... m : machine implementation dependant drives
|
8508 |
* n ... p : network devices
|
8509 |
* It's up to each machine implementation to check
|
8510 |
* if the given boot devices match the actual hardware
|
8511 |
* implementation and firmware features.
|
8512 |
*/
|
8513 |
if (*p < 'a' || *p > 'q') { |
8514 |
fprintf(stderr, "Invalid boot device '%c'\n", *p);
|
8515 |
exit(1);
|
8516 |
} |
8517 |
if (boot_devices_bitmap & (1 << (*p - 'a'))) { |
8518 |
fprintf(stderr, |
8519 |
"Boot device '%c' was given twice\n",*p);
|
8520 |
exit(1);
|
8521 |
} |
8522 |
boot_devices_bitmap |= 1 << (*p - 'a'); |
8523 |
} |
8524 |
} |
8525 |
break;
|
8526 |
case QEMU_OPTION_fda:
|
8527 |
case QEMU_OPTION_fdb:
|
8528 |
drive_add(optarg, FD_ALIAS, popt->index - QEMU_OPTION_fda); |
8529 |
break;
|
8530 |
#ifdef TARGET_I386
|
8531 |
case QEMU_OPTION_no_fd_bootchk:
|
8532 |
fd_bootchk = 0;
|
8533 |
break;
|
8534 |
#endif
|
8535 |
case QEMU_OPTION_net:
|
8536 |
if (nb_net_clients >= MAX_NET_CLIENTS) {
|
8537 |
fprintf(stderr, "qemu: too many network clients\n");
|
8538 |
exit(1);
|
8539 |
} |
8540 |
net_clients[nb_net_clients] = optarg; |
8541 |
nb_net_clients++; |
8542 |
break;
|
8543 |
#ifdef CONFIG_SLIRP
|
8544 |
case QEMU_OPTION_tftp:
|
8545 |
tftp_prefix = optarg; |
8546 |
break;
|
8547 |
case QEMU_OPTION_bootp:
|
8548 |
bootp_filename = optarg; |
8549 |
break;
|
8550 |
#ifndef _WIN32
|
8551 |
case QEMU_OPTION_smb:
|
8552 |
net_slirp_smb(optarg); |
8553 |
break;
|
8554 |
#endif
|
8555 |
case QEMU_OPTION_redir:
|
8556 |
net_slirp_redir(optarg); |
8557 |
break;
|
8558 |
#endif
|
8559 |
#ifdef HAS_AUDIO
|
8560 |
case QEMU_OPTION_audio_help:
|
8561 |
AUD_help (); |
8562 |
exit (0);
|
8563 |
break;
|
8564 |
case QEMU_OPTION_soundhw:
|
8565 |
select_soundhw (optarg); |
8566 |
break;
|
8567 |
#endif
|
8568 |
case QEMU_OPTION_h:
|
8569 |
help(0);
|
8570 |
break;
|
8571 |
case QEMU_OPTION_m: {
|
8572 |
uint64_t value; |
8573 |
char *ptr;
|
8574 |
|
8575 |
value = strtoul(optarg, &ptr, 10);
|
8576 |
switch (*ptr) {
|
8577 |
case 0: case 'M': case 'm': |
8578 |
value <<= 20;
|
8579 |
break;
|
8580 |
case 'G': case 'g': |
8581 |
value <<= 30;
|
8582 |
break;
|
8583 |
default:
|
8584 |
fprintf(stderr, "qemu: invalid ram size: %s\n", optarg);
|
8585 |
exit(1);
|
8586 |
} |
8587 |
|
8588 |
/* On 32-bit hosts, QEMU is limited by virtual address space */
|
8589 |
if (value > (2047 << 20) |
8590 |
#ifndef USE_KQEMU
|
8591 |
&& HOST_LONG_BITS == 32
|
8592 |
#endif
|
8593 |
) { |
8594 |
fprintf(stderr, "qemu: at most 2047 MB RAM can be simulated\n");
|
8595 |
exit(1);
|
8596 |
} |
8597 |
if (value != (uint64_t)(ram_addr_t)value) {
|
8598 |
fprintf(stderr, "qemu: ram size too large\n");
|
8599 |
exit(1);
|
8600 |
} |
8601 |
ram_size = value; |
8602 |
break;
|
8603 |
} |
8604 |
case QEMU_OPTION_d:
|
8605 |
{ |
8606 |
int mask;
|
8607 |
CPULogItem *item; |
8608 |
|
8609 |
mask = cpu_str_to_log_mask(optarg); |
8610 |
if (!mask) {
|
8611 |
printf("Log items (comma separated):\n");
|
8612 |
for(item = cpu_log_items; item->mask != 0; item++) { |
8613 |
printf("%-10s %s\n", item->name, item->help);
|
8614 |
} |
8615 |
exit(1);
|
8616 |
} |
8617 |
cpu_set_log(mask); |
8618 |
} |
8619 |
break;
|
8620 |
#ifdef CONFIG_GDBSTUB
|
8621 |
case QEMU_OPTION_s:
|
8622 |
use_gdbstub = 1;
|
8623 |
break;
|
8624 |
case QEMU_OPTION_p:
|
8625 |
gdbstub_port = optarg; |
8626 |
break;
|
8627 |
#endif
|
8628 |
case QEMU_OPTION_L:
|
8629 |
bios_dir = optarg; |
8630 |
break;
|
8631 |
case QEMU_OPTION_bios:
|
8632 |
bios_name = optarg; |
8633 |
break;
|
8634 |
case QEMU_OPTION_S:
|
8635 |
autostart = 0;
|
8636 |
break;
|
8637 |
case QEMU_OPTION_k:
|
8638 |
keyboard_layout = optarg; |
8639 |
break;
|
8640 |
case QEMU_OPTION_localtime:
|
8641 |
rtc_utc = 0;
|
8642 |
break;
|
8643 |
case QEMU_OPTION_cirrusvga:
|
8644 |
cirrus_vga_enabled = 1;
|
8645 |
vmsvga_enabled = 0;
|
8646 |
break;
|
8647 |
case QEMU_OPTION_vmsvga:
|
8648 |
cirrus_vga_enabled = 0;
|
8649 |
vmsvga_enabled = 1;
|
8650 |
break;
|
8651 |
case QEMU_OPTION_std_vga:
|
8652 |
cirrus_vga_enabled = 0;
|
8653 |
vmsvga_enabled = 0;
|
8654 |
break;
|
8655 |
case QEMU_OPTION_g:
|
8656 |
{ |
8657 |
const char *p; |
8658 |
int w, h, depth;
|
8659 |
p = optarg; |
8660 |
w = strtol(p, (char **)&p, 10); |
8661 |
if (w <= 0) { |
8662 |
graphic_error:
|
8663 |
fprintf(stderr, "qemu: invalid resolution or depth\n");
|
8664 |
exit(1);
|
8665 |
} |
8666 |
if (*p != 'x') |
8667 |
goto graphic_error;
|
8668 |
p++; |
8669 |
h = strtol(p, (char **)&p, 10); |
8670 |
if (h <= 0) |
8671 |
goto graphic_error;
|
8672 |
if (*p == 'x') { |
8673 |
p++; |
8674 |
depth = strtol(p, (char **)&p, 10); |
8675 |
if (depth != 8 && depth != 15 && depth != 16 && |
8676 |
depth != 24 && depth != 32) |
8677 |
goto graphic_error;
|
8678 |
} else if (*p == '\0') { |
8679 |
depth = graphic_depth; |
8680 |
} else {
|
8681 |
goto graphic_error;
|
8682 |
} |
8683 |
|
8684 |
graphic_width = w; |
8685 |
graphic_height = h; |
8686 |
graphic_depth = depth; |
8687 |
} |
8688 |
break;
|
8689 |
case QEMU_OPTION_echr:
|
8690 |
{ |
8691 |
char *r;
|
8692 |
term_escape_char = strtol(optarg, &r, 0);
|
8693 |
if (r == optarg)
|
8694 |
printf("Bad argument to echr\n");
|
8695 |
break;
|
8696 |
} |
8697 |
case QEMU_OPTION_monitor:
|
8698 |
monitor_device = optarg; |
8699 |
break;
|
8700 |
case QEMU_OPTION_serial:
|
8701 |
if (serial_device_index >= MAX_SERIAL_PORTS) {
|
8702 |
fprintf(stderr, "qemu: too many serial ports\n");
|
8703 |
exit(1);
|
8704 |
} |
8705 |
serial_devices[serial_device_index] = optarg; |
8706 |
serial_device_index++; |
8707 |
break;
|
8708 |
case QEMU_OPTION_parallel:
|
8709 |
if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
8710 |
fprintf(stderr, "qemu: too many parallel ports\n");
|
8711 |
exit(1);
|
8712 |
} |
8713 |
parallel_devices[parallel_device_index] = optarg; |
8714 |
parallel_device_index++; |
8715 |
break;
|
8716 |
case QEMU_OPTION_loadvm:
|
8717 |
loadvm = optarg; |
8718 |
break;
|
8719 |
case QEMU_OPTION_full_screen:
|
8720 |
full_screen = 1;
|
8721 |
break;
|
8722 |
#ifdef CONFIG_SDL
|
8723 |
case QEMU_OPTION_no_frame:
|
8724 |
no_frame = 1;
|
8725 |
break;
|
8726 |
case QEMU_OPTION_alt_grab:
|
8727 |
alt_grab = 1;
|
8728 |
break;
|
8729 |
case QEMU_OPTION_no_quit:
|
8730 |
no_quit = 1;
|
8731 |
break;
|
8732 |
#endif
|
8733 |
case QEMU_OPTION_pidfile:
|
8734 |
pid_file = optarg; |
8735 |
break;
|
8736 |
#ifdef TARGET_I386
|
8737 |
case QEMU_OPTION_win2k_hack:
|
8738 |
win2k_install_hack = 1;
|
8739 |
break;
|
8740 |
#endif
|
8741 |
#ifdef USE_KQEMU
|
8742 |
case QEMU_OPTION_no_kqemu:
|
8743 |
kqemu_allowed = 0;
|
8744 |
break;
|
8745 |
case QEMU_OPTION_kernel_kqemu:
|
8746 |
kqemu_allowed = 2;
|
8747 |
break;
|
8748 |
#endif
|
8749 |
case QEMU_OPTION_usb:
|
8750 |
usb_enabled = 1;
|
8751 |
break;
|
8752 |
case QEMU_OPTION_usbdevice:
|
8753 |
usb_enabled = 1;
|
8754 |
if (usb_devices_index >= MAX_USB_CMDLINE) {
|
8755 |
fprintf(stderr, "Too many USB devices\n");
|
8756 |
exit(1);
|
8757 |
} |
8758 |
usb_devices[usb_devices_index] = optarg; |
8759 |
usb_devices_index++; |
8760 |
break;
|
8761 |
case QEMU_OPTION_smp:
|
8762 |
smp_cpus = atoi(optarg); |
8763 |
if (smp_cpus < 1 || smp_cpus > MAX_CPUS) { |
8764 |
fprintf(stderr, "Invalid number of CPUs\n");
|
8765 |
exit(1);
|
8766 |
} |
8767 |
break;
|
8768 |
case QEMU_OPTION_vnc:
|
8769 |
vnc_display = optarg; |
8770 |
break;
|
8771 |
case QEMU_OPTION_no_acpi:
|
8772 |
acpi_enabled = 0;
|
8773 |
break;
|
8774 |
case QEMU_OPTION_no_reboot:
|
8775 |
no_reboot = 1;
|
8776 |
break;
|
8777 |
case QEMU_OPTION_no_shutdown:
|
8778 |
no_shutdown = 1;
|
8779 |
break;
|
8780 |
case QEMU_OPTION_show_cursor:
|
8781 |
cursor_hide = 0;
|
8782 |
break;
|
8783 |
case QEMU_OPTION_daemonize:
|
8784 |
daemonize = 1;
|
8785 |
break;
|
8786 |
case QEMU_OPTION_option_rom:
|
8787 |
if (nb_option_roms >= MAX_OPTION_ROMS) {
|
8788 |
fprintf(stderr, "Too many option ROMs\n");
|
8789 |
exit(1);
|
8790 |
} |
8791 |
option_rom[nb_option_roms] = optarg; |
8792 |
nb_option_roms++; |
8793 |
break;
|
8794 |
case QEMU_OPTION_semihosting:
|
8795 |
semihosting_enabled = 1;
|
8796 |
break;
|
8797 |
case QEMU_OPTION_name:
|
8798 |
qemu_name = optarg; |
8799 |
break;
|
8800 |
#ifdef TARGET_SPARC
|
8801 |
case QEMU_OPTION_prom_env:
|
8802 |
if (nb_prom_envs >= MAX_PROM_ENVS) {
|
8803 |
fprintf(stderr, "Too many prom variables\n");
|
8804 |
exit(1);
|
8805 |
} |
8806 |
prom_envs[nb_prom_envs] = optarg; |
8807 |
nb_prom_envs++; |
8808 |
break;
|
8809 |
#endif
|
8810 |
#ifdef TARGET_ARM
|
8811 |
case QEMU_OPTION_old_param:
|
8812 |
old_param = 1;
|
8813 |
break;
|
8814 |
#endif
|
8815 |
case QEMU_OPTION_clock:
|
8816 |
configure_alarms(optarg); |
8817 |
break;
|
8818 |
case QEMU_OPTION_startdate:
|
8819 |
{ |
8820 |
struct tm tm;
|
8821 |
time_t rtc_start_date; |
8822 |
if (!strcmp(optarg, "now")) { |
8823 |
rtc_date_offset = -1;
|
8824 |
} else {
|
8825 |
if (sscanf(optarg, "%d-%d-%dT%d:%d:%d", |
8826 |
&tm.tm_year, |
8827 |
&tm.tm_mon, |
8828 |
&tm.tm_mday, |
8829 |
&tm.tm_hour, |
8830 |
&tm.tm_min, |
8831 |
&tm.tm_sec) == 6) {
|
8832 |
/* OK */
|
8833 |
} else if (sscanf(optarg, "%d-%d-%d", |
8834 |
&tm.tm_year, |
8835 |
&tm.tm_mon, |
8836 |
&tm.tm_mday) == 3) {
|
8837 |
tm.tm_hour = 0;
|
8838 |
tm.tm_min = 0;
|
8839 |
tm.tm_sec = 0;
|
8840 |
} else {
|
8841 |
goto date_fail;
|
8842 |
} |
8843 |
tm.tm_year -= 1900;
|
8844 |
tm.tm_mon--; |
8845 |
rtc_start_date = mktimegm(&tm); |
8846 |
if (rtc_start_date == -1) { |
8847 |
date_fail:
|
8848 |
fprintf(stderr, "Invalid date format. Valid format are:\n"
|
8849 |
"'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
|
8850 |
exit(1);
|
8851 |
} |
8852 |
rtc_date_offset = time(NULL) - rtc_start_date;
|
8853 |
} |
8854 |
} |
8855 |
break;
|
8856 |
case QEMU_OPTION_tb_size:
|
8857 |
tb_size = strtol(optarg, NULL, 0); |
8858 |
if (tb_size < 0) |
8859 |
tb_size = 0;
|
8860 |
break;
|
8861 |
case QEMU_OPTION_icount:
|
8862 |
use_icount = 1;
|
8863 |
if (strcmp(optarg, "auto") == 0) { |
8864 |
icount_time_shift = -1;
|
8865 |
} else {
|
8866 |
icount_time_shift = strtol(optarg, NULL, 0); |
8867 |
} |
8868 |
break;
|
8869 |
} |
8870 |
} |
8871 |
} |
8872 |
|
8873 |
if (nographic) {
|
8874 |
if (serial_device_index == 0) |
8875 |
serial_devices[0] = "stdio"; |
8876 |
if (parallel_device_index == 0) |
8877 |
parallel_devices[0] = "null"; |
8878 |
if (strncmp(monitor_device, "vc", 2) == 0) |
8879 |
monitor_device = "stdio";
|
8880 |
} |
8881 |
|
8882 |
#ifndef _WIN32
|
8883 |
if (daemonize) {
|
8884 |
pid_t pid; |
8885 |
|
8886 |
if (pipe(fds) == -1) |
8887 |
exit(1);
|
8888 |
|
8889 |
pid = fork(); |
8890 |
if (pid > 0) { |
8891 |
uint8_t status; |
8892 |
ssize_t len; |
8893 |
|
8894 |
close(fds[1]);
|
8895 |
|
8896 |
again:
|
8897 |
len = read(fds[0], &status, 1); |
8898 |
if (len == -1 && (errno == EINTR)) |
8899 |
goto again;
|
8900 |
|
8901 |
if (len != 1) |
8902 |
exit(1);
|
8903 |
else if (status == 1) { |
8904 |
fprintf(stderr, "Could not acquire pidfile\n");
|
8905 |
exit(1);
|
8906 |
} else
|
8907 |
exit(0);
|
8908 |
} else if (pid < 0) |
8909 |
exit(1);
|
8910 |
|
8911 |
setsid(); |
8912 |
|
8913 |
pid = fork(); |
8914 |
if (pid > 0) |
8915 |
exit(0);
|
8916 |
else if (pid < 0) |
8917 |
exit(1);
|
8918 |
|
8919 |
umask(027);
|
8920 |
|
8921 |
signal(SIGTSTP, SIG_IGN); |
8922 |
signal(SIGTTOU, SIG_IGN); |
8923 |
signal(SIGTTIN, SIG_IGN); |
8924 |
} |
8925 |
#endif
|
8926 |
|
8927 |
if (pid_file && qemu_create_pidfile(pid_file) != 0) { |
8928 |
if (daemonize) {
|
8929 |
uint8_t status = 1;
|
8930 |
write(fds[1], &status, 1); |
8931 |
} else
|
8932 |
fprintf(stderr, "Could not acquire pid file\n");
|
8933 |
exit(1);
|
8934 |
} |
8935 |
|
8936 |
#ifdef USE_KQEMU
|
8937 |
if (smp_cpus > 1) |
8938 |
kqemu_allowed = 0;
|
8939 |
#endif
|
8940 |
linux_boot = (kernel_filename != NULL);
|
8941 |
net_boot = (boot_devices_bitmap >> ('n' - 'a')) & 0xF; |
8942 |
|
8943 |
if (!linux_boot && net_boot == 0 && |
8944 |
!machine->nodisk_ok && nb_drives_opt == 0)
|
8945 |
help(1);
|
8946 |
|
8947 |
if (!linux_boot && *kernel_cmdline != '\0') { |
8948 |
fprintf(stderr, "-append only allowed with -kernel option\n");
|
8949 |
exit(1);
|
8950 |
} |
8951 |
|
8952 |
if (!linux_boot && initrd_filename != NULL) { |
8953 |
fprintf(stderr, "-initrd only allowed with -kernel option\n");
|
8954 |
exit(1);
|
8955 |
} |
8956 |
|
8957 |
/* boot to floppy or the default cd if no hard disk defined yet */
|
8958 |
if (!boot_devices[0]) { |
8959 |
boot_devices = "cad";
|
8960 |
} |
8961 |
setvbuf(stdout, NULL, _IOLBF, 0); |
8962 |
|
8963 |
init_timers(); |
8964 |
init_timer_alarm(); |
8965 |
qemu_aio_init(); |
8966 |
if (use_icount && icount_time_shift < 0) { |
8967 |
use_icount = 2;
|
8968 |
/* 125MIPS seems a reasonable initial guess at the guest speed.
|
8969 |
It will be corrected fairly quickly anyway. */
|
8970 |
icount_time_shift = 3;
|
8971 |
init_icount_adjust(); |
8972 |
} |
8973 |
|
8974 |
#ifdef _WIN32
|
8975 |
socket_init(); |
8976 |
#endif
|
8977 |
|
8978 |
/* init network clients */
|
8979 |
if (nb_net_clients == 0) { |
8980 |
/* if no clients, we use a default config */
|
8981 |
net_clients[nb_net_clients++] = "nic";
|
8982 |
#ifdef CONFIG_SLIRP
|
8983 |
net_clients[nb_net_clients++] = "user";
|
8984 |
#endif
|
8985 |
} |
8986 |
|
8987 |
for(i = 0;i < nb_net_clients; i++) { |
8988 |
if (net_client_parse(net_clients[i]) < 0) |
8989 |
exit(1);
|
8990 |
} |
8991 |
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
8992 |
if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
8993 |
continue;
|
8994 |
if (vlan->nb_guest_devs == 0) |
8995 |
fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
8996 |
if (vlan->nb_host_devs == 0) |
8997 |
fprintf(stderr, |
8998 |
"Warning: vlan %d is not connected to host network\n",
|
8999 |
vlan->id); |
9000 |
} |
9001 |
|
9002 |
#ifdef TARGET_I386
|
9003 |
/* XXX: this should be moved in the PC machine instantiation code */
|
9004 |
if (net_boot != 0) { |
9005 |
int netroms = 0; |
9006 |
for (i = 0; i < nb_nics && i < 4; i++) { |
9007 |
const char *model = nd_table[i].model; |
9008 |
char buf[1024]; |
9009 |
if (net_boot & (1 << i)) { |
9010 |
if (model == NULL) |
9011 |
model = "ne2k_pci";
|
9012 |
snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model); |
9013 |
if (get_image_size(buf) > 0) { |
9014 |
if (nb_option_roms >= MAX_OPTION_ROMS) {
|
9015 |
fprintf(stderr, "Too many option ROMs\n");
|
9016 |
exit(1);
|
9017 |
} |
9018 |
option_rom[nb_option_roms] = strdup(buf); |
9019 |
nb_option_roms++; |
9020 |
netroms++; |
9021 |
} |
9022 |
} |
9023 |
} |
9024 |
if (netroms == 0) { |
9025 |
fprintf(stderr, "No valid PXE rom found for network device\n");
|
9026 |
exit(1);
|
9027 |
} |
9028 |
} |
9029 |
#endif
|
9030 |
|
9031 |
/* init the memory */
|
9032 |
phys_ram_size = machine->ram_require & ~RAMSIZE_FIXED; |
9033 |
|
9034 |
if (machine->ram_require & RAMSIZE_FIXED) {
|
9035 |
if (ram_size > 0) { |
9036 |
if (ram_size < phys_ram_size) {
|
9037 |
fprintf(stderr, "Machine `%s' requires %llu bytes of memory\n",
|
9038 |
machine->name, (unsigned long long) phys_ram_size); |
9039 |
exit(-1);
|
9040 |
} |
9041 |
|
9042 |
phys_ram_size = ram_size; |
9043 |
} else
|
9044 |
ram_size = phys_ram_size; |
9045 |
} else {
|
9046 |
if (ram_size == 0) |
9047 |
ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
9048 |
|
9049 |
phys_ram_size += ram_size; |
9050 |
} |
9051 |
|
9052 |
phys_ram_base = qemu_vmalloc(phys_ram_size); |
9053 |
if (!phys_ram_base) {
|
9054 |
fprintf(stderr, "Could not allocate physical memory\n");
|
9055 |
exit(1);
|
9056 |
} |
9057 |
|
9058 |
/* init the dynamic translator */
|
9059 |
cpu_exec_init_all(tb_size * 1024 * 1024); |
9060 |
|
9061 |
bdrv_init(); |
9062 |
|
9063 |
/* we always create the cdrom drive, even if no disk is there */
|
9064 |
|
9065 |
if (nb_drives_opt < MAX_DRIVES)
|
9066 |
drive_add(NULL, CDROM_ALIAS);
|
9067 |
|
9068 |
/* we always create at least one floppy */
|
9069 |
|
9070 |
if (nb_drives_opt < MAX_DRIVES)
|
9071 |
drive_add(NULL, FD_ALIAS, 0); |
9072 |
|
9073 |
/* we always create one sd slot, even if no card is in it */
|
9074 |
|
9075 |
if (nb_drives_opt < MAX_DRIVES)
|
9076 |
drive_add(NULL, SD_ALIAS);
|
9077 |
|
9078 |
/* open the virtual block devices */
|
9079 |
|
9080 |
for(i = 0; i < nb_drives_opt; i++) |
9081 |
if (drive_init(&drives_opt[i], snapshot, machine) == -1) |
9082 |
exit(1);
|
9083 |
|
9084 |
register_savevm("timer", 0, 2, timer_save, timer_load, NULL); |
9085 |
register_savevm("ram", 0, 2, ram_save, ram_load, NULL); |
9086 |
|
9087 |
/* terminal init */
|
9088 |
memset(&display_state, 0, sizeof(display_state)); |
9089 |
if (nographic) {
|
9090 |
if (curses) {
|
9091 |
fprintf(stderr, "fatal: -nographic can't be used with -curses\n");
|
9092 |
exit(1);
|
9093 |
} |
9094 |
/* nearly nothing to do */
|
9095 |
dumb_display_init(ds); |
9096 |
} else if (vnc_display != NULL) { |
9097 |
vnc_display_init(ds); |
9098 |
if (vnc_display_open(ds, vnc_display) < 0) |
9099 |
exit(1);
|
9100 |
} else
|
9101 |
#if defined(CONFIG_CURSES)
|
9102 |
if (curses) {
|
9103 |
curses_display_init(ds, full_screen); |
9104 |
} else
|
9105 |
#endif
|
9106 |
{ |
9107 |
#if defined(CONFIG_SDL)
|
9108 |
sdl_display_init(ds, full_screen, no_frame); |
9109 |
#elif defined(CONFIG_COCOA)
|
9110 |
cocoa_display_init(ds, full_screen); |
9111 |
#else
|
9112 |
dumb_display_init(ds); |
9113 |
#endif
|
9114 |
} |
9115 |
|
9116 |
#ifndef _WIN32
|
9117 |
/* must be after terminal init, SDL library changes signal handlers */
|
9118 |
termsig_setup(); |
9119 |
#endif
|
9120 |
|
9121 |
/* Maintain compatibility with multiple stdio monitors */
|
9122 |
if (!strcmp(monitor_device,"stdio")) { |
9123 |
for (i = 0; i < MAX_SERIAL_PORTS; i++) { |
9124 |
const char *devname = serial_devices[i]; |
9125 |
if (devname && !strcmp(devname,"mon:stdio")) { |
9126 |
monitor_device = NULL;
|
9127 |
break;
|
9128 |
} else if (devname && !strcmp(devname,"stdio")) { |
9129 |
monitor_device = NULL;
|
9130 |
serial_devices[i] = "mon:stdio";
|
9131 |
break;
|
9132 |
} |
9133 |
} |
9134 |
} |
9135 |
if (monitor_device) {
|
9136 |
monitor_hd = qemu_chr_open(monitor_device); |
9137 |
if (!monitor_hd) {
|
9138 |
fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
9139 |
exit(1);
|
9140 |
} |
9141 |
monitor_init(monitor_hd, !nographic); |
9142 |
} |
9143 |
|
9144 |
for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
9145 |
const char *devname = serial_devices[i]; |
9146 |
if (devname && strcmp(devname, "none")) { |
9147 |
serial_hds[i] = qemu_chr_open(devname); |
9148 |
if (!serial_hds[i]) {
|
9149 |
fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
9150 |
devname); |
9151 |
exit(1);
|
9152 |
} |
9153 |
if (strstart(devname, "vc", 0)) |
9154 |
qemu_chr_printf(serial_hds[i], "serial%d console\r\n", i);
|
9155 |
} |
9156 |
} |
9157 |
|
9158 |
for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
9159 |
const char *devname = parallel_devices[i]; |
9160 |
if (devname && strcmp(devname, "none")) { |
9161 |
parallel_hds[i] = qemu_chr_open(devname); |
9162 |
if (!parallel_hds[i]) {
|
9163 |
fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
9164 |
devname); |
9165 |
exit(1);
|
9166 |
} |
9167 |
if (strstart(devname, "vc", 0)) |
9168 |
qemu_chr_printf(parallel_hds[i], "parallel%d console\r\n", i);
|
9169 |
} |
9170 |
} |
9171 |
|
9172 |
machine->init(ram_size, vga_ram_size, boot_devices, ds, |
9173 |
kernel_filename, kernel_cmdline, initrd_filename, cpu_model); |
9174 |
|
9175 |
/* init USB devices */
|
9176 |
if (usb_enabled) {
|
9177 |
for(i = 0; i < usb_devices_index; i++) { |
9178 |
if (usb_device_add(usb_devices[i]) < 0) { |
9179 |
fprintf(stderr, "Warning: could not add USB device %s\n",
|
9180 |
usb_devices[i]); |
9181 |
} |
9182 |
} |
9183 |
} |
9184 |
|
9185 |
if (display_state.dpy_refresh) {
|
9186 |
display_state.gui_timer = qemu_new_timer(rt_clock, gui_update, &display_state); |
9187 |
qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock)); |
9188 |
} |
9189 |
|
9190 |
#ifdef CONFIG_GDBSTUB
|
9191 |
if (use_gdbstub) {
|
9192 |
/* XXX: use standard host:port notation and modify options
|
9193 |
accordingly. */
|
9194 |
if (gdbserver_start(gdbstub_port) < 0) { |
9195 |
fprintf(stderr, "qemu: could not open gdbstub device on port '%s'\n",
|
9196 |
gdbstub_port); |
9197 |
exit(1);
|
9198 |
} |
9199 |
} |
9200 |
#endif
|
9201 |
|
9202 |
if (loadvm)
|
9203 |
do_loadvm(loadvm); |
9204 |
|
9205 |
{ |
9206 |
/* XXX: simplify init */
|
9207 |
read_passwords(); |
9208 |
if (autostart) {
|
9209 |
vm_start(); |
9210 |
} |
9211 |
} |
9212 |
|
9213 |
if (daemonize) {
|
9214 |
uint8_t status = 0;
|
9215 |
ssize_t len; |
9216 |
int fd;
|
9217 |
|
9218 |
again1:
|
9219 |
len = write(fds[1], &status, 1); |
9220 |
if (len == -1 && (errno == EINTR)) |
9221 |
goto again1;
|
9222 |
|
9223 |
if (len != 1) |
9224 |
exit(1);
|
9225 |
|
9226 |
chdir("/");
|
9227 |
TFR(fd = open("/dev/null", O_RDWR));
|
9228 |
if (fd == -1) |
9229 |
exit(1);
|
9230 |
|
9231 |
dup2(fd, 0);
|
9232 |
dup2(fd, 1);
|
9233 |
dup2(fd, 2);
|
9234 |
|
9235 |
close(fd); |
9236 |
} |
9237 |
|
9238 |
main_loop(); |
9239 |
quit_timers(); |
9240 |
|
9241 |
#if !defined(_WIN32)
|
9242 |
/* close network clients */
|
9243 |
for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
9244 |
VLANClientState *vc; |
9245 |
|
9246 |
for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
9247 |
if (vc->fd_read == tap_receive) {
|
9248 |
char ifname[64]; |
9249 |
TAPState *s = vc->opaque; |
9250 |
|
9251 |
if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
9252 |
s->down_script[0])
|
9253 |
launch_script(s->down_script, ifname, s->fd); |
9254 |
} |
9255 |
#if defined(CONFIG_VDE)
|
9256 |
if (vc->fd_read == vde_from_qemu) {
|
9257 |
VDEState *s = vc->opaque; |
9258 |
vde_close(s->vde); |
9259 |
} |
9260 |
#endif
|
9261 |
} |
9262 |
} |
9263 |
#endif
|
9264 |
return 0; |
9265 |
} |