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