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