root / vl.c @ d79284e0
History | View | Annotate | Download (96.2 kB)
1 |
/*
|
---|---|
2 |
* QEMU System Emulator
|
3 |
*
|
4 |
* Copyright (c) 2003-2004 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 |
|
33 |
#ifndef _WIN32
|
34 |
#include <sys/times.h> |
35 |
#include <sys/wait.h> |
36 |
#include <termios.h> |
37 |
#include <sys/poll.h> |
38 |
#include <sys/mman.h> |
39 |
#include <sys/ioctl.h> |
40 |
#include <sys/socket.h> |
41 |
#include <netinet/in.h> |
42 |
#include <dirent.h> |
43 |
#ifdef _BSD
|
44 |
#include <sys/stat.h> |
45 |
#ifndef __APPLE__
|
46 |
#include <libutil.h> |
47 |
#endif
|
48 |
#else
|
49 |
#include <linux/if.h> |
50 |
#include <linux/if_tun.h> |
51 |
#include <pty.h> |
52 |
#include <malloc.h> |
53 |
#include <linux/rtc.h> |
54 |
#endif
|
55 |
#endif
|
56 |
|
57 |
#if defined(CONFIG_SLIRP)
|
58 |
#include "libslirp.h" |
59 |
#endif
|
60 |
|
61 |
#ifdef _WIN32
|
62 |
#include <malloc.h> |
63 |
#include <sys/timeb.h> |
64 |
#include <windows.h> |
65 |
#define getopt_long_only getopt_long
|
66 |
#define memalign(align, size) malloc(size)
|
67 |
#endif
|
68 |
|
69 |
#ifdef CONFIG_SDL
|
70 |
#ifdef __APPLE__
|
71 |
#include <SDL/SDL.h> |
72 |
#endif
|
73 |
#endif /* CONFIG_SDL */ |
74 |
|
75 |
#include "disas.h" |
76 |
|
77 |
#include "exec-all.h" |
78 |
|
79 |
//#define DO_TB_FLUSH
|
80 |
|
81 |
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
82 |
|
83 |
//#define DEBUG_UNUSED_IOPORT
|
84 |
//#define DEBUG_IOPORT
|
85 |
|
86 |
#if !defined(CONFIG_SOFTMMU)
|
87 |
#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) |
88 |
#else
|
89 |
#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
90 |
#endif
|
91 |
|
92 |
#ifdef TARGET_PPC
|
93 |
#define DEFAULT_RAM_SIZE 144 |
94 |
#else
|
95 |
#define DEFAULT_RAM_SIZE 128 |
96 |
#endif
|
97 |
/* in ms */
|
98 |
#define GUI_REFRESH_INTERVAL 30 |
99 |
|
100 |
/* XXX: use a two level table to limit memory usage */
|
101 |
#define MAX_IOPORTS 65536 |
102 |
|
103 |
const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
104 |
char phys_ram_file[1024]; |
105 |
CPUState *global_env; |
106 |
CPUState *cpu_single_env; |
107 |
void *ioport_opaque[MAX_IOPORTS];
|
108 |
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
109 |
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
110 |
BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD]; |
111 |
int vga_ram_size;
|
112 |
int bios_size;
|
113 |
static DisplayState display_state;
|
114 |
int nographic;
|
115 |
const char* keyboard_layout = NULL; |
116 |
int64_t ticks_per_sec; |
117 |
int boot_device = 'c'; |
118 |
int ram_size;
|
119 |
static char network_script[1024]; |
120 |
int pit_min_timer_count = 0; |
121 |
int nb_nics;
|
122 |
NetDriverState nd_table[MAX_NICS]; |
123 |
QEMUTimer *gui_timer; |
124 |
int vm_running;
|
125 |
int audio_enabled = 0; |
126 |
int sb16_enabled = 1; |
127 |
int adlib_enabled = 1; |
128 |
int gus_enabled = 1; |
129 |
int pci_enabled = 1; |
130 |
int prep_enabled = 0; |
131 |
int rtc_utc = 1; |
132 |
int cirrus_vga_enabled = 1; |
133 |
int graphic_width = 800; |
134 |
int graphic_height = 600; |
135 |
int graphic_depth = 15; |
136 |
int full_screen = 0; |
137 |
TextConsole *vga_console; |
138 |
CharDriverState *serial_hds[MAX_SERIAL_PORTS]; |
139 |
CharDriverState *parallel_hds[MAX_PARALLEL_PORTS]; |
140 |
|
141 |
/***********************************************************/
|
142 |
/* x86 ISA bus support */
|
143 |
|
144 |
target_phys_addr_t isa_mem_base = 0;
|
145 |
|
146 |
uint32_t default_ioport_readb(void *opaque, uint32_t address)
|
147 |
{ |
148 |
#ifdef DEBUG_UNUSED_IOPORT
|
149 |
fprintf(stderr, "inb: port=0x%04x\n", address);
|
150 |
#endif
|
151 |
return 0xff; |
152 |
} |
153 |
|
154 |
void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
155 |
{ |
156 |
#ifdef DEBUG_UNUSED_IOPORT
|
157 |
fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
|
158 |
#endif
|
159 |
} |
160 |
|
161 |
/* default is to make two byte accesses */
|
162 |
uint32_t default_ioport_readw(void *opaque, uint32_t address)
|
163 |
{ |
164 |
uint32_t data; |
165 |
data = ioport_read_table[0][address](ioport_opaque[address], address);
|
166 |
address = (address + 1) & (MAX_IOPORTS - 1); |
167 |
data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8; |
168 |
return data;
|
169 |
} |
170 |
|
171 |
void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
172 |
{ |
173 |
ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff); |
174 |
address = (address + 1) & (MAX_IOPORTS - 1); |
175 |
ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff); |
176 |
} |
177 |
|
178 |
uint32_t default_ioport_readl(void *opaque, uint32_t address)
|
179 |
{ |
180 |
#ifdef DEBUG_UNUSED_IOPORT
|
181 |
fprintf(stderr, "inl: port=0x%04x\n", address);
|
182 |
#endif
|
183 |
return 0xffffffff; |
184 |
} |
185 |
|
186 |
void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
187 |
{ |
188 |
#ifdef DEBUG_UNUSED_IOPORT
|
189 |
fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
|
190 |
#endif
|
191 |
} |
192 |
|
193 |
void init_ioports(void) |
194 |
{ |
195 |
int i;
|
196 |
|
197 |
for(i = 0; i < MAX_IOPORTS; i++) { |
198 |
ioport_read_table[0][i] = default_ioport_readb;
|
199 |
ioport_write_table[0][i] = default_ioport_writeb;
|
200 |
ioport_read_table[1][i] = default_ioport_readw;
|
201 |
ioport_write_table[1][i] = default_ioport_writew;
|
202 |
ioport_read_table[2][i] = default_ioport_readl;
|
203 |
ioport_write_table[2][i] = default_ioport_writel;
|
204 |
} |
205 |
} |
206 |
|
207 |
/* size is the word size in byte */
|
208 |
int register_ioport_read(int start, int length, int size, |
209 |
IOPortReadFunc *func, void *opaque)
|
210 |
{ |
211 |
int i, bsize;
|
212 |
|
213 |
if (size == 1) { |
214 |
bsize = 0;
|
215 |
} else if (size == 2) { |
216 |
bsize = 1;
|
217 |
} else if (size == 4) { |
218 |
bsize = 2;
|
219 |
} else {
|
220 |
hw_error("register_ioport_read: invalid size");
|
221 |
return -1; |
222 |
} |
223 |
for(i = start; i < start + length; i += size) {
|
224 |
ioport_read_table[bsize][i] = func; |
225 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
226 |
hw_error("register_ioport_read: invalid opaque");
|
227 |
ioport_opaque[i] = opaque; |
228 |
} |
229 |
return 0; |
230 |
} |
231 |
|
232 |
/* size is the word size in byte */
|
233 |
int register_ioport_write(int start, int length, int size, |
234 |
IOPortWriteFunc *func, void *opaque)
|
235 |
{ |
236 |
int i, bsize;
|
237 |
|
238 |
if (size == 1) { |
239 |
bsize = 0;
|
240 |
} else if (size == 2) { |
241 |
bsize = 1;
|
242 |
} else if (size == 4) { |
243 |
bsize = 2;
|
244 |
} else {
|
245 |
hw_error("register_ioport_write: invalid size");
|
246 |
return -1; |
247 |
} |
248 |
for(i = start; i < start + length; i += size) {
|
249 |
ioport_write_table[bsize][i] = func; |
250 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
251 |
hw_error("register_ioport_read: invalid opaque");
|
252 |
ioport_opaque[i] = opaque; |
253 |
} |
254 |
return 0; |
255 |
} |
256 |
|
257 |
void isa_unassign_ioport(int start, int length) |
258 |
{ |
259 |
int i;
|
260 |
|
261 |
for(i = start; i < start + length; i++) {
|
262 |
ioport_read_table[0][i] = default_ioport_readb;
|
263 |
ioport_read_table[1][i] = default_ioport_readw;
|
264 |
ioport_read_table[2][i] = default_ioport_readl;
|
265 |
|
266 |
ioport_write_table[0][i] = default_ioport_writeb;
|
267 |
ioport_write_table[1][i] = default_ioport_writew;
|
268 |
ioport_write_table[2][i] = default_ioport_writel;
|
269 |
} |
270 |
} |
271 |
|
272 |
/***********************************************************/
|
273 |
|
274 |
void pstrcpy(char *buf, int buf_size, const char *str) |
275 |
{ |
276 |
int c;
|
277 |
char *q = buf;
|
278 |
|
279 |
if (buf_size <= 0) |
280 |
return;
|
281 |
|
282 |
for(;;) {
|
283 |
c = *str++; |
284 |
if (c == 0 || q >= buf + buf_size - 1) |
285 |
break;
|
286 |
*q++ = c; |
287 |
} |
288 |
*q = '\0';
|
289 |
} |
290 |
|
291 |
/* strcat and truncate. */
|
292 |
char *pstrcat(char *buf, int buf_size, const char *s) |
293 |
{ |
294 |
int len;
|
295 |
len = strlen(buf); |
296 |
if (len < buf_size)
|
297 |
pstrcpy(buf + len, buf_size - len, s); |
298 |
return buf;
|
299 |
} |
300 |
|
301 |
int strstart(const char *str, const char *val, const char **ptr) |
302 |
{ |
303 |
const char *p, *q; |
304 |
p = str; |
305 |
q = val; |
306 |
while (*q != '\0') { |
307 |
if (*p != *q)
|
308 |
return 0; |
309 |
p++; |
310 |
q++; |
311 |
} |
312 |
if (ptr)
|
313 |
*ptr = p; |
314 |
return 1; |
315 |
} |
316 |
|
317 |
/* return the size or -1 if error */
|
318 |
int get_image_size(const char *filename) |
319 |
{ |
320 |
int fd, size;
|
321 |
fd = open(filename, O_RDONLY | O_BINARY); |
322 |
if (fd < 0) |
323 |
return -1; |
324 |
size = lseek(fd, 0, SEEK_END);
|
325 |
close(fd); |
326 |
return size;
|
327 |
} |
328 |
|
329 |
/* return the size or -1 if error */
|
330 |
int load_image(const char *filename, uint8_t *addr) |
331 |
{ |
332 |
int fd, size;
|
333 |
fd = open(filename, O_RDONLY | O_BINARY); |
334 |
if (fd < 0) |
335 |
return -1; |
336 |
size = lseek(fd, 0, SEEK_END);
|
337 |
lseek(fd, 0, SEEK_SET);
|
338 |
if (read(fd, addr, size) != size) {
|
339 |
close(fd); |
340 |
return -1; |
341 |
} |
342 |
close(fd); |
343 |
return size;
|
344 |
} |
345 |
|
346 |
void cpu_outb(CPUState *env, int addr, int val) |
347 |
{ |
348 |
#ifdef DEBUG_IOPORT
|
349 |
if (loglevel & CPU_LOG_IOPORT)
|
350 |
fprintf(logfile, "outb: %04x %02x\n", addr, val);
|
351 |
#endif
|
352 |
ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
|
353 |
} |
354 |
|
355 |
void cpu_outw(CPUState *env, int addr, int val) |
356 |
{ |
357 |
#ifdef DEBUG_IOPORT
|
358 |
if (loglevel & CPU_LOG_IOPORT)
|
359 |
fprintf(logfile, "outw: %04x %04x\n", addr, val);
|
360 |
#endif
|
361 |
ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
|
362 |
} |
363 |
|
364 |
void cpu_outl(CPUState *env, int addr, int val) |
365 |
{ |
366 |
#ifdef DEBUG_IOPORT
|
367 |
if (loglevel & CPU_LOG_IOPORT)
|
368 |
fprintf(logfile, "outl: %04x %08x\n", addr, val);
|
369 |
#endif
|
370 |
ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
|
371 |
} |
372 |
|
373 |
int cpu_inb(CPUState *env, int addr) |
374 |
{ |
375 |
int val;
|
376 |
val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
|
377 |
#ifdef DEBUG_IOPORT
|
378 |
if (loglevel & CPU_LOG_IOPORT)
|
379 |
fprintf(logfile, "inb : %04x %02x\n", addr, val);
|
380 |
#endif
|
381 |
return val;
|
382 |
} |
383 |
|
384 |
int cpu_inw(CPUState *env, int addr) |
385 |
{ |
386 |
int val;
|
387 |
val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
|
388 |
#ifdef DEBUG_IOPORT
|
389 |
if (loglevel & CPU_LOG_IOPORT)
|
390 |
fprintf(logfile, "inw : %04x %04x\n", addr, val);
|
391 |
#endif
|
392 |
return val;
|
393 |
} |
394 |
|
395 |
int cpu_inl(CPUState *env, int addr) |
396 |
{ |
397 |
int val;
|
398 |
val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
|
399 |
#ifdef DEBUG_IOPORT
|
400 |
if (loglevel & CPU_LOG_IOPORT)
|
401 |
fprintf(logfile, "inl : %04x %08x\n", addr, val);
|
402 |
#endif
|
403 |
return val;
|
404 |
} |
405 |
|
406 |
/***********************************************************/
|
407 |
void hw_error(const char *fmt, ...) |
408 |
{ |
409 |
va_list ap; |
410 |
|
411 |
va_start(ap, fmt); |
412 |
fprintf(stderr, "qemu: hardware error: ");
|
413 |
vfprintf(stderr, fmt, ap); |
414 |
fprintf(stderr, "\n");
|
415 |
#ifdef TARGET_I386
|
416 |
cpu_dump_state(global_env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP); |
417 |
#else
|
418 |
cpu_dump_state(global_env, stderr, fprintf, 0);
|
419 |
#endif
|
420 |
va_end(ap); |
421 |
abort(); |
422 |
} |
423 |
|
424 |
/***********************************************************/
|
425 |
/* keyboard/mouse */
|
426 |
|
427 |
static QEMUPutKBDEvent *qemu_put_kbd_event;
|
428 |
static void *qemu_put_kbd_event_opaque; |
429 |
static QEMUPutMouseEvent *qemu_put_mouse_event;
|
430 |
static void *qemu_put_mouse_event_opaque; |
431 |
|
432 |
void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque) |
433 |
{ |
434 |
qemu_put_kbd_event_opaque = opaque; |
435 |
qemu_put_kbd_event = func; |
436 |
} |
437 |
|
438 |
void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque) |
439 |
{ |
440 |
qemu_put_mouse_event_opaque = opaque; |
441 |
qemu_put_mouse_event = func; |
442 |
} |
443 |
|
444 |
void kbd_put_keycode(int keycode) |
445 |
{ |
446 |
if (qemu_put_kbd_event) {
|
447 |
qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode); |
448 |
} |
449 |
} |
450 |
|
451 |
void kbd_mouse_event(int dx, int dy, int dz, int buttons_state) |
452 |
{ |
453 |
if (qemu_put_mouse_event) {
|
454 |
qemu_put_mouse_event(qemu_put_mouse_event_opaque, |
455 |
dx, dy, dz, buttons_state); |
456 |
} |
457 |
} |
458 |
|
459 |
/***********************************************************/
|
460 |
/* timers */
|
461 |
|
462 |
#if defined(__powerpc__)
|
463 |
|
464 |
static inline uint32_t get_tbl(void) |
465 |
{ |
466 |
uint32_t tbl; |
467 |
asm volatile("mftb %0" : "=r" (tbl)); |
468 |
return tbl;
|
469 |
} |
470 |
|
471 |
static inline uint32_t get_tbu(void) |
472 |
{ |
473 |
uint32_t tbl; |
474 |
asm volatile("mftbu %0" : "=r" (tbl)); |
475 |
return tbl;
|
476 |
} |
477 |
|
478 |
int64_t cpu_get_real_ticks(void)
|
479 |
{ |
480 |
uint32_t l, h, h1; |
481 |
/* NOTE: we test if wrapping has occurred */
|
482 |
do {
|
483 |
h = get_tbu(); |
484 |
l = get_tbl(); |
485 |
h1 = get_tbu(); |
486 |
} while (h != h1);
|
487 |
return ((int64_t)h << 32) | l; |
488 |
} |
489 |
|
490 |
#elif defined(__i386__)
|
491 |
|
492 |
int64_t cpu_get_real_ticks(void)
|
493 |
{ |
494 |
int64_t val; |
495 |
asm volatile ("rdtsc" : "=A" (val)); |
496 |
return val;
|
497 |
} |
498 |
|
499 |
#elif defined(__x86_64__)
|
500 |
|
501 |
int64_t cpu_get_real_ticks(void)
|
502 |
{ |
503 |
uint32_t low,high; |
504 |
int64_t val; |
505 |
asm volatile("rdtsc" : "=a" (low), "=d" (high)); |
506 |
val = high; |
507 |
val <<= 32;
|
508 |
val |= low; |
509 |
return val;
|
510 |
} |
511 |
|
512 |
#else
|
513 |
#error unsupported CPU
|
514 |
#endif
|
515 |
|
516 |
static int64_t cpu_ticks_offset;
|
517 |
static int cpu_ticks_enabled; |
518 |
|
519 |
static inline int64_t cpu_get_ticks(void) |
520 |
{ |
521 |
if (!cpu_ticks_enabled) {
|
522 |
return cpu_ticks_offset;
|
523 |
} else {
|
524 |
return cpu_get_real_ticks() + cpu_ticks_offset;
|
525 |
} |
526 |
} |
527 |
|
528 |
/* enable cpu_get_ticks() */
|
529 |
void cpu_enable_ticks(void) |
530 |
{ |
531 |
if (!cpu_ticks_enabled) {
|
532 |
cpu_ticks_offset -= cpu_get_real_ticks(); |
533 |
cpu_ticks_enabled = 1;
|
534 |
} |
535 |
} |
536 |
|
537 |
/* disable cpu_get_ticks() : the clock is stopped. You must not call
|
538 |
cpu_get_ticks() after that. */
|
539 |
void cpu_disable_ticks(void) |
540 |
{ |
541 |
if (cpu_ticks_enabled) {
|
542 |
cpu_ticks_offset = cpu_get_ticks(); |
543 |
cpu_ticks_enabled = 0;
|
544 |
} |
545 |
} |
546 |
|
547 |
static int64_t get_clock(void) |
548 |
{ |
549 |
#ifdef _WIN32
|
550 |
struct _timeb tb;
|
551 |
_ftime(&tb); |
552 |
return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000; |
553 |
#else
|
554 |
struct timeval tv;
|
555 |
gettimeofday(&tv, NULL);
|
556 |
return tv.tv_sec * 1000000LL + tv.tv_usec; |
557 |
#endif
|
558 |
} |
559 |
|
560 |
void cpu_calibrate_ticks(void) |
561 |
{ |
562 |
int64_t usec, ticks; |
563 |
|
564 |
usec = get_clock(); |
565 |
ticks = cpu_get_real_ticks(); |
566 |
#ifdef _WIN32
|
567 |
Sleep(50);
|
568 |
#else
|
569 |
usleep(50 * 1000); |
570 |
#endif
|
571 |
usec = get_clock() - usec; |
572 |
ticks = cpu_get_real_ticks() - ticks; |
573 |
ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; |
574 |
} |
575 |
|
576 |
/* compute with 96 bit intermediate result: (a*b)/c */
|
577 |
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
578 |
{ |
579 |
union {
|
580 |
uint64_t ll; |
581 |
struct {
|
582 |
#ifdef WORDS_BIGENDIAN
|
583 |
uint32_t high, low; |
584 |
#else
|
585 |
uint32_t low, high; |
586 |
#endif
|
587 |
} l; |
588 |
} u, res; |
589 |
uint64_t rl, rh; |
590 |
|
591 |
u.ll = a; |
592 |
rl = (uint64_t)u.l.low * (uint64_t)b; |
593 |
rh = (uint64_t)u.l.high * (uint64_t)b; |
594 |
rh += (rl >> 32);
|
595 |
res.l.high = rh / c; |
596 |
res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
597 |
return res.ll;
|
598 |
} |
599 |
|
600 |
#define QEMU_TIMER_REALTIME 0 |
601 |
#define QEMU_TIMER_VIRTUAL 1 |
602 |
|
603 |
struct QEMUClock {
|
604 |
int type;
|
605 |
/* XXX: add frequency */
|
606 |
}; |
607 |
|
608 |
struct QEMUTimer {
|
609 |
QEMUClock *clock; |
610 |
int64_t expire_time; |
611 |
QEMUTimerCB *cb; |
612 |
void *opaque;
|
613 |
struct QEMUTimer *next;
|
614 |
}; |
615 |
|
616 |
QEMUClock *rt_clock; |
617 |
QEMUClock *vm_clock; |
618 |
|
619 |
static QEMUTimer *active_timers[2]; |
620 |
#ifdef _WIN32
|
621 |
static MMRESULT timerID;
|
622 |
#else
|
623 |
/* frequency of the times() clock tick */
|
624 |
static int timer_freq; |
625 |
#endif
|
626 |
|
627 |
QEMUClock *qemu_new_clock(int type)
|
628 |
{ |
629 |
QEMUClock *clock; |
630 |
clock = qemu_mallocz(sizeof(QEMUClock));
|
631 |
if (!clock)
|
632 |
return NULL; |
633 |
clock->type = type; |
634 |
return clock;
|
635 |
} |
636 |
|
637 |
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
638 |
{ |
639 |
QEMUTimer *ts; |
640 |
|
641 |
ts = qemu_mallocz(sizeof(QEMUTimer));
|
642 |
ts->clock = clock; |
643 |
ts->cb = cb; |
644 |
ts->opaque = opaque; |
645 |
return ts;
|
646 |
} |
647 |
|
648 |
void qemu_free_timer(QEMUTimer *ts)
|
649 |
{ |
650 |
qemu_free(ts); |
651 |
} |
652 |
|
653 |
/* stop a timer, but do not dealloc it */
|
654 |
void qemu_del_timer(QEMUTimer *ts)
|
655 |
{ |
656 |
QEMUTimer **pt, *t; |
657 |
|
658 |
/* NOTE: this code must be signal safe because
|
659 |
qemu_timer_expired() can be called from a signal. */
|
660 |
pt = &active_timers[ts->clock->type]; |
661 |
for(;;) {
|
662 |
t = *pt; |
663 |
if (!t)
|
664 |
break;
|
665 |
if (t == ts) {
|
666 |
*pt = t->next; |
667 |
break;
|
668 |
} |
669 |
pt = &t->next; |
670 |
} |
671 |
} |
672 |
|
673 |
/* modify the current timer so that it will be fired when current_time
|
674 |
>= expire_time. The corresponding callback will be called. */
|
675 |
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
676 |
{ |
677 |
QEMUTimer **pt, *t; |
678 |
|
679 |
qemu_del_timer(ts); |
680 |
|
681 |
/* add the timer in the sorted list */
|
682 |
/* NOTE: this code must be signal safe because
|
683 |
qemu_timer_expired() can be called from a signal. */
|
684 |
pt = &active_timers[ts->clock->type]; |
685 |
for(;;) {
|
686 |
t = *pt; |
687 |
if (!t)
|
688 |
break;
|
689 |
if (t->expire_time > expire_time)
|
690 |
break;
|
691 |
pt = &t->next; |
692 |
} |
693 |
ts->expire_time = expire_time; |
694 |
ts->next = *pt; |
695 |
*pt = ts; |
696 |
} |
697 |
|
698 |
int qemu_timer_pending(QEMUTimer *ts)
|
699 |
{ |
700 |
QEMUTimer *t; |
701 |
for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
702 |
if (t == ts)
|
703 |
return 1; |
704 |
} |
705 |
return 0; |
706 |
} |
707 |
|
708 |
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
709 |
{ |
710 |
if (!timer_head)
|
711 |
return 0; |
712 |
return (timer_head->expire_time <= current_time);
|
713 |
} |
714 |
|
715 |
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
716 |
{ |
717 |
QEMUTimer *ts; |
718 |
|
719 |
for(;;) {
|
720 |
ts = *ptimer_head; |
721 |
if (!ts || ts->expire_time > current_time)
|
722 |
break;
|
723 |
/* remove timer from the list before calling the callback */
|
724 |
*ptimer_head = ts->next; |
725 |
ts->next = NULL;
|
726 |
|
727 |
/* run the callback (the timer list can be modified) */
|
728 |
ts->cb(ts->opaque); |
729 |
} |
730 |
} |
731 |
|
732 |
int64_t qemu_get_clock(QEMUClock *clock) |
733 |
{ |
734 |
switch(clock->type) {
|
735 |
case QEMU_TIMER_REALTIME:
|
736 |
#ifdef _WIN32
|
737 |
return GetTickCount();
|
738 |
#else
|
739 |
{ |
740 |
struct tms tp;
|
741 |
|
742 |
/* Note that using gettimeofday() is not a good solution
|
743 |
for timers because its value change when the date is
|
744 |
modified. */
|
745 |
if (timer_freq == 100) { |
746 |
return times(&tp) * 10; |
747 |
} else {
|
748 |
return ((int64_t)times(&tp) * 1000) / timer_freq; |
749 |
} |
750 |
} |
751 |
#endif
|
752 |
default:
|
753 |
case QEMU_TIMER_VIRTUAL:
|
754 |
return cpu_get_ticks();
|
755 |
} |
756 |
} |
757 |
|
758 |
/* save a timer */
|
759 |
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
760 |
{ |
761 |
uint64_t expire_time; |
762 |
|
763 |
if (qemu_timer_pending(ts)) {
|
764 |
expire_time = ts->expire_time; |
765 |
} else {
|
766 |
expire_time = -1;
|
767 |
} |
768 |
qemu_put_be64(f, expire_time); |
769 |
} |
770 |
|
771 |
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
772 |
{ |
773 |
uint64_t expire_time; |
774 |
|
775 |
expire_time = qemu_get_be64(f); |
776 |
if (expire_time != -1) { |
777 |
qemu_mod_timer(ts, expire_time); |
778 |
} else {
|
779 |
qemu_del_timer(ts); |
780 |
} |
781 |
} |
782 |
|
783 |
static void timer_save(QEMUFile *f, void *opaque) |
784 |
{ |
785 |
if (cpu_ticks_enabled) {
|
786 |
hw_error("cannot save state if virtual timers are running");
|
787 |
} |
788 |
qemu_put_be64s(f, &cpu_ticks_offset); |
789 |
qemu_put_be64s(f, &ticks_per_sec); |
790 |
} |
791 |
|
792 |
static int timer_load(QEMUFile *f, void *opaque, int version_id) |
793 |
{ |
794 |
if (version_id != 1) |
795 |
return -EINVAL;
|
796 |
if (cpu_ticks_enabled) {
|
797 |
return -EINVAL;
|
798 |
} |
799 |
qemu_get_be64s(f, &cpu_ticks_offset); |
800 |
qemu_get_be64s(f, &ticks_per_sec); |
801 |
return 0; |
802 |
} |
803 |
|
804 |
#ifdef _WIN32
|
805 |
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
806 |
DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
807 |
#else
|
808 |
static void host_alarm_handler(int host_signum) |
809 |
#endif
|
810 |
{ |
811 |
#if 0
|
812 |
#define DISP_FREQ 1000
|
813 |
{
|
814 |
static int64_t delta_min = INT64_MAX;
|
815 |
static int64_t delta_max, delta_cum, last_clock, delta, ti;
|
816 |
static int count;
|
817 |
ti = qemu_get_clock(vm_clock);
|
818 |
if (last_clock != 0) {
|
819 |
delta = ti - last_clock;
|
820 |
if (delta < delta_min)
|
821 |
delta_min = delta;
|
822 |
if (delta > delta_max)
|
823 |
delta_max = delta;
|
824 |
delta_cum += delta;
|
825 |
if (++count == DISP_FREQ) {
|
826 |
printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
|
827 |
muldiv64(delta_min, 1000000, ticks_per_sec),
|
828 |
muldiv64(delta_max, 1000000, ticks_per_sec),
|
829 |
muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
|
830 |
(double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
|
831 |
count = 0;
|
832 |
delta_min = INT64_MAX;
|
833 |
delta_max = 0;
|
834 |
delta_cum = 0;
|
835 |
}
|
836 |
}
|
837 |
last_clock = ti;
|
838 |
}
|
839 |
#endif
|
840 |
if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
|
841 |
qemu_get_clock(vm_clock)) || |
842 |
qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
843 |
qemu_get_clock(rt_clock))) { |
844 |
/* stop the cpu because a timer occured */
|
845 |
cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); |
846 |
} |
847 |
} |
848 |
|
849 |
#ifndef _WIN32
|
850 |
|
851 |
#if defined(__linux__)
|
852 |
|
853 |
#define RTC_FREQ 1024 |
854 |
|
855 |
static int rtc_fd; |
856 |
|
857 |
static int start_rtc_timer(void) |
858 |
{ |
859 |
rtc_fd = open("/dev/rtc", O_RDONLY);
|
860 |
if (rtc_fd < 0) |
861 |
return -1; |
862 |
if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) { |
863 |
fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
|
864 |
"error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
|
865 |
"type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
|
866 |
goto fail;
|
867 |
} |
868 |
if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) { |
869 |
fail:
|
870 |
close(rtc_fd); |
871 |
return -1; |
872 |
} |
873 |
pit_min_timer_count = PIT_FREQ / RTC_FREQ; |
874 |
return 0; |
875 |
} |
876 |
|
877 |
#else
|
878 |
|
879 |
static int start_rtc_timer(void) |
880 |
{ |
881 |
return -1; |
882 |
} |
883 |
|
884 |
#endif /* !defined(__linux__) */ |
885 |
|
886 |
#endif /* !defined(_WIN32) */ |
887 |
|
888 |
static void init_timers(void) |
889 |
{ |
890 |
rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
891 |
vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
892 |
|
893 |
#ifdef _WIN32
|
894 |
{ |
895 |
int count=0; |
896 |
timerID = timeSetEvent(10, // interval (ms) |
897 |
0, // resolution |
898 |
host_alarm_handler, // function
|
899 |
(DWORD)&count, // user parameter
|
900 |
TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
901 |
if( !timerID ) {
|
902 |
perror("failed timer alarm");
|
903 |
exit(1);
|
904 |
} |
905 |
} |
906 |
pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
907 |
#else
|
908 |
{ |
909 |
struct sigaction act;
|
910 |
struct itimerval itv;
|
911 |
|
912 |
/* get times() syscall frequency */
|
913 |
timer_freq = sysconf(_SC_CLK_TCK); |
914 |
|
915 |
/* timer signal */
|
916 |
sigfillset(&act.sa_mask); |
917 |
act.sa_flags = 0;
|
918 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
919 |
act.sa_flags |= SA_ONSTACK; |
920 |
#endif
|
921 |
act.sa_handler = host_alarm_handler; |
922 |
sigaction(SIGALRM, &act, NULL);
|
923 |
|
924 |
itv.it_interval.tv_sec = 0;
|
925 |
itv.it_interval.tv_usec = 999; /* for i386 kernel 2.6 to get 1 ms */ |
926 |
itv.it_value.tv_sec = 0;
|
927 |
itv.it_value.tv_usec = 10 * 1000; |
928 |
setitimer(ITIMER_REAL, &itv, NULL);
|
929 |
/* we probe the tick duration of the kernel to inform the user if
|
930 |
the emulated kernel requested a too high timer frequency */
|
931 |
getitimer(ITIMER_REAL, &itv); |
932 |
|
933 |
#if defined(__linux__)
|
934 |
if (itv.it_interval.tv_usec > 1000) { |
935 |
/* try to use /dev/rtc to have a faster timer */
|
936 |
if (start_rtc_timer() < 0) |
937 |
goto use_itimer;
|
938 |
/* disable itimer */
|
939 |
itv.it_interval.tv_sec = 0;
|
940 |
itv.it_interval.tv_usec = 0;
|
941 |
itv.it_value.tv_sec = 0;
|
942 |
itv.it_value.tv_usec = 0;
|
943 |
setitimer(ITIMER_REAL, &itv, NULL);
|
944 |
|
945 |
/* use the RTC */
|
946 |
sigaction(SIGIO, &act, NULL);
|
947 |
fcntl(rtc_fd, F_SETFL, O_ASYNC); |
948 |
fcntl(rtc_fd, F_SETOWN, getpid()); |
949 |
} else
|
950 |
#endif /* defined(__linux__) */ |
951 |
{ |
952 |
use_itimer:
|
953 |
pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * |
954 |
PIT_FREQ) / 1000000;
|
955 |
} |
956 |
} |
957 |
#endif
|
958 |
} |
959 |
|
960 |
void quit_timers(void) |
961 |
{ |
962 |
#ifdef _WIN32
|
963 |
timeKillEvent(timerID); |
964 |
#endif
|
965 |
} |
966 |
|
967 |
/***********************************************************/
|
968 |
/* character device */
|
969 |
|
970 |
int qemu_chr_write(CharDriverState *s, const uint8_t *buf, int len) |
971 |
{ |
972 |
return s->chr_write(s, buf, len);
|
973 |
} |
974 |
|
975 |
void qemu_chr_printf(CharDriverState *s, const char *fmt, ...) |
976 |
{ |
977 |
char buf[4096]; |
978 |
va_list ap; |
979 |
va_start(ap, fmt); |
980 |
vsnprintf(buf, sizeof(buf), fmt, ap);
|
981 |
qemu_chr_write(s, buf, strlen(buf)); |
982 |
va_end(ap); |
983 |
} |
984 |
|
985 |
void qemu_chr_send_event(CharDriverState *s, int event) |
986 |
{ |
987 |
if (s->chr_send_event)
|
988 |
s->chr_send_event(s, event); |
989 |
} |
990 |
|
991 |
void qemu_chr_add_read_handler(CharDriverState *s,
|
992 |
IOCanRWHandler *fd_can_read, |
993 |
IOReadHandler *fd_read, void *opaque)
|
994 |
{ |
995 |
s->chr_add_read_handler(s, fd_can_read, fd_read, opaque); |
996 |
} |
997 |
|
998 |
void qemu_chr_add_event_handler(CharDriverState *s, IOEventHandler *chr_event)
|
999 |
{ |
1000 |
s->chr_event = chr_event; |
1001 |
} |
1002 |
|
1003 |
static int null_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1004 |
{ |
1005 |
return len;
|
1006 |
} |
1007 |
|
1008 |
static void null_chr_add_read_handler(CharDriverState *chr, |
1009 |
IOCanRWHandler *fd_can_read, |
1010 |
IOReadHandler *fd_read, void *opaque)
|
1011 |
{ |
1012 |
} |
1013 |
|
1014 |
CharDriverState *qemu_chr_open_null(void)
|
1015 |
{ |
1016 |
CharDriverState *chr; |
1017 |
|
1018 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
1019 |
if (!chr)
|
1020 |
return NULL; |
1021 |
chr->chr_write = null_chr_write; |
1022 |
chr->chr_add_read_handler = null_chr_add_read_handler; |
1023 |
return chr;
|
1024 |
} |
1025 |
|
1026 |
#ifndef _WIN32
|
1027 |
|
1028 |
typedef struct { |
1029 |
int fd_in, fd_out;
|
1030 |
/* for nographic stdio only */
|
1031 |
IOCanRWHandler *fd_can_read; |
1032 |
IOReadHandler *fd_read; |
1033 |
void *fd_opaque;
|
1034 |
} FDCharDriver; |
1035 |
|
1036 |
#define STDIO_MAX_CLIENTS 2 |
1037 |
|
1038 |
static int stdio_nb_clients; |
1039 |
static CharDriverState *stdio_clients[STDIO_MAX_CLIENTS];
|
1040 |
|
1041 |
static int unix_write(int fd, const uint8_t *buf, int len1) |
1042 |
{ |
1043 |
int ret, len;
|
1044 |
|
1045 |
len = len1; |
1046 |
while (len > 0) { |
1047 |
ret = write(fd, buf, len); |
1048 |
if (ret < 0) { |
1049 |
if (errno != EINTR && errno != EAGAIN)
|
1050 |
return -1; |
1051 |
} else if (ret == 0) { |
1052 |
break;
|
1053 |
} else {
|
1054 |
buf += ret; |
1055 |
len -= ret; |
1056 |
} |
1057 |
} |
1058 |
return len1 - len;
|
1059 |
} |
1060 |
|
1061 |
static int fd_chr_write(CharDriverState *chr, const uint8_t *buf, int len) |
1062 |
{ |
1063 |
FDCharDriver *s = chr->opaque; |
1064 |
return unix_write(s->fd_out, buf, len);
|
1065 |
} |
1066 |
|
1067 |
static void fd_chr_add_read_handler(CharDriverState *chr, |
1068 |
IOCanRWHandler *fd_can_read, |
1069 |
IOReadHandler *fd_read, void *opaque)
|
1070 |
{ |
1071 |
FDCharDriver *s = chr->opaque; |
1072 |
|
1073 |
if (nographic && s->fd_in == 0) { |
1074 |
s->fd_can_read = fd_can_read; |
1075 |
s->fd_read = fd_read; |
1076 |
s->fd_opaque = opaque; |
1077 |
} else {
|
1078 |
qemu_add_fd_read_handler(s->fd_in, fd_can_read, fd_read, opaque); |
1079 |
} |
1080 |
} |
1081 |
|
1082 |
/* open a character device to a unix fd */
|
1083 |
CharDriverState *qemu_chr_open_fd(int fd_in, int fd_out) |
1084 |
{ |
1085 |
CharDriverState *chr; |
1086 |
FDCharDriver *s; |
1087 |
|
1088 |
chr = qemu_mallocz(sizeof(CharDriverState));
|
1089 |
if (!chr)
|
1090 |
return NULL; |
1091 |
s = qemu_mallocz(sizeof(FDCharDriver));
|
1092 |
if (!s) {
|
1093 |
free(chr); |
1094 |
return NULL; |
1095 |
} |
1096 |
s->fd_in = fd_in; |
1097 |
s->fd_out = fd_out; |
1098 |
chr->opaque = s; |
1099 |
chr->chr_write = fd_chr_write; |
1100 |
chr->chr_add_read_handler = fd_chr_add_read_handler; |
1101 |
return chr;
|
1102 |
} |
1103 |
|
1104 |
/* for STDIO, we handle the case where several clients use it
|
1105 |
(nographic mode) */
|
1106 |
|
1107 |
#define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */ |
1108 |
|
1109 |
static int term_got_escape, client_index; |
1110 |
|
1111 |
void term_print_help(void) |
1112 |
{ |
1113 |
printf("\n"
|
1114 |
"C-a h print this help\n"
|
1115 |
"C-a x exit emulator\n"
|
1116 |
"C-a s save disk data back to file (if -snapshot)\n"
|
1117 |
"C-a b send break (magic sysrq)\n"
|
1118 |
"C-a c switch between console and monitor\n"
|
1119 |
"C-a C-a send C-a\n"
|
1120 |
); |
1121 |
} |
1122 |
|
1123 |
/* called when a char is received */
|
1124 |
static void stdio_received_byte(int ch) |
1125 |
{ |
1126 |
if (term_got_escape) {
|
1127 |
term_got_escape = 0;
|
1128 |
switch(ch) {
|
1129 |
case 'h': |
1130 |
term_print_help(); |
1131 |
break;
|
1132 |
case 'x': |
1133 |
exit(0);
|
1134 |
break;
|
1135 |
case 's': |
1136 |
{ |
1137 |
int i;
|
1138 |
for (i = 0; i < MAX_DISKS; i++) { |
1139 |
if (bs_table[i])
|
1140 |
bdrv_commit(bs_table[i]); |
1141 |
} |
1142 |
} |
1143 |
break;
|
1144 |
case 'b': |
1145 |
if (client_index < stdio_nb_clients) {
|
1146 |
CharDriverState *chr; |
1147 |
FDCharDriver *s; |
1148 |
|
1149 |
chr = stdio_clients[client_index]; |
1150 |
s = chr->opaque; |
1151 |
chr->chr_event(s->fd_opaque, CHR_EVENT_BREAK); |
1152 |
} |
1153 |
break;
|
1154 |
case 'c': |
1155 |
client_index++; |
1156 |
if (client_index >= stdio_nb_clients)
|
1157 |
client_index = 0;
|
1158 |
if (client_index == 0) { |
1159 |
/* send a new line in the monitor to get the prompt */
|
1160 |
ch = '\r';
|
1161 |
goto send_char;
|
1162 |
} |
1163 |
break;
|
1164 |
case TERM_ESCAPE:
|
1165 |
goto send_char;
|
1166 |
} |
1167 |
} else if (ch == TERM_ESCAPE) { |
1168 |
term_got_escape = 1;
|
1169 |
} else {
|
1170 |
send_char:
|
1171 |
if (client_index < stdio_nb_clients) {
|
1172 |
uint8_t buf[1];
|
1173 |
CharDriverState *chr; |
1174 |
FDCharDriver *s; |
1175 |
|
1176 |
chr = stdio_clients[client_index]; |
1177 |
s = chr->opaque; |
1178 |
buf[0] = ch;
|
1179 |
/* XXX: should queue the char if the device is not
|
1180 |
ready */
|
1181 |
if (s->fd_can_read(s->fd_opaque) > 0) |
1182 |
s->fd_read(s->fd_opaque, buf, 1);
|
1183 |
} |
1184 |
} |
1185 |
} |
1186 |
|
1187 |
static int stdio_can_read(void *opaque) |
1188 |
{ |
1189 |
/* XXX: not strictly correct */
|
1190 |
return 1; |
1191 |
} |
1192 |
|
1193 |
static void stdio_read(void *opaque, const uint8_t *buf, int size) |
1194 |
{ |
1195 |
int i;
|
1196 |
for(i = 0; i < size; i++) |
1197 |
stdio_received_byte(buf[i]); |
1198 |
} |
1199 |
|
1200 |
/* init terminal so that we can grab keys */
|
1201 |
static struct termios oldtty; |
1202 |
static int old_fd0_flags; |
1203 |
|
1204 |
static void term_exit(void) |
1205 |
{ |
1206 |
tcsetattr (0, TCSANOW, &oldtty);
|
1207 |
fcntl(0, F_SETFL, old_fd0_flags);
|
1208 |
} |
1209 |
|
1210 |
static void term_init(void) |
1211 |
{ |
1212 |
struct termios tty;
|
1213 |
|
1214 |
tcgetattr (0, &tty);
|
1215 |
oldtty = tty; |
1216 |
old_fd0_flags = fcntl(0, F_GETFL);
|
1217 |
|
1218 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
1219 |
|INLCR|IGNCR|ICRNL|IXON); |
1220 |
tty.c_oflag |= OPOST; |
1221 |
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
1222 |
/* if graphical mode, we allow Ctrl-C handling */
|
1223 |
if (nographic)
|
1224 |
tty.c_lflag &= ~ISIG; |
1225 |
tty.c_cflag &= ~(CSIZE|PARENB); |
1226 |
tty.c_cflag |= CS8; |
1227 |
tty.c_cc[VMIN] = 1;
|
1228 |
tty.c_cc[VTIME] = 0;
|
1229 |
|
1230 |
tcsetattr (0, TCSANOW, &tty);
|
1231 |
|
1232 |
atexit(term_exit); |
1233 |
|
1234 |
fcntl(0, F_SETFL, O_NONBLOCK);
|
1235 |
} |
1236 |
|
1237 |
CharDriverState *qemu_chr_open_stdio(void)
|
1238 |
{ |
1239 |
CharDriverState *chr; |
1240 |
|
1241 |
if (nographic) {
|
1242 |
if (stdio_nb_clients >= STDIO_MAX_CLIENTS)
|
1243 |
return NULL; |
1244 |
chr = qemu_chr_open_fd(0, 1); |
1245 |
if (stdio_nb_clients == 0) |
1246 |
qemu_add_fd_read_handler(0, stdio_can_read, stdio_read, NULL); |
1247 |
client_index = stdio_nb_clients; |
1248 |
} else {
|
1249 |
if (stdio_nb_clients != 0) |
1250 |
return NULL; |
1251 |
chr = qemu_chr_open_fd(0, 1); |
1252 |
} |
1253 |
stdio_clients[stdio_nb_clients++] = chr; |
1254 |
if (stdio_nb_clients == 1) { |
1255 |
/* set the terminal in raw mode */
|
1256 |
term_init(); |
1257 |
} |
1258 |
return chr;
|
1259 |
} |
1260 |
|
1261 |
#if defined(__linux__)
|
1262 |
CharDriverState *qemu_chr_open_pty(void)
|
1263 |
{ |
1264 |
char slave_name[1024]; |
1265 |
int master_fd, slave_fd;
|
1266 |
|
1267 |
/* Not satisfying */
|
1268 |
if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
1269 |
return NULL; |
1270 |
} |
1271 |
fprintf(stderr, "char device redirected to %s\n", slave_name);
|
1272 |
return qemu_chr_open_fd(master_fd, master_fd);
|
1273 |
} |
1274 |
#else
|
1275 |
CharDriverState *qemu_chr_open_pty(void)
|
1276 |
{ |
1277 |
return NULL; |
1278 |
} |
1279 |
#endif
|
1280 |
|
1281 |
#endif /* !defined(_WIN32) */ |
1282 |
|
1283 |
CharDriverState *qemu_chr_open(const char *filename) |
1284 |
{ |
1285 |
if (!strcmp(filename, "vc")) { |
1286 |
return text_console_init(&display_state);
|
1287 |
} else if (!strcmp(filename, "null")) { |
1288 |
return qemu_chr_open_null();
|
1289 |
} else
|
1290 |
#ifndef _WIN32
|
1291 |
if (!strcmp(filename, "pty")) { |
1292 |
return qemu_chr_open_pty();
|
1293 |
} else if (!strcmp(filename, "stdio")) { |
1294 |
return qemu_chr_open_stdio();
|
1295 |
} else
|
1296 |
#endif
|
1297 |
{ |
1298 |
return NULL; |
1299 |
} |
1300 |
} |
1301 |
|
1302 |
/***********************************************************/
|
1303 |
/* Linux network device redirectors */
|
1304 |
|
1305 |
void hex_dump(FILE *f, const uint8_t *buf, int size) |
1306 |
{ |
1307 |
int len, i, j, c;
|
1308 |
|
1309 |
for(i=0;i<size;i+=16) { |
1310 |
len = size - i; |
1311 |
if (len > 16) |
1312 |
len = 16;
|
1313 |
fprintf(f, "%08x ", i);
|
1314 |
for(j=0;j<16;j++) { |
1315 |
if (j < len)
|
1316 |
fprintf(f, " %02x", buf[i+j]);
|
1317 |
else
|
1318 |
fprintf(f, " ");
|
1319 |
} |
1320 |
fprintf(f, " ");
|
1321 |
for(j=0;j<len;j++) { |
1322 |
c = buf[i+j]; |
1323 |
if (c < ' ' || c > '~') |
1324 |
c = '.';
|
1325 |
fprintf(f, "%c", c);
|
1326 |
} |
1327 |
fprintf(f, "\n");
|
1328 |
} |
1329 |
} |
1330 |
|
1331 |
void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
1332 |
{ |
1333 |
nd->send_packet(nd, buf, size); |
1334 |
} |
1335 |
|
1336 |
void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
|
1337 |
IOReadHandler *fd_read, void *opaque)
|
1338 |
{ |
1339 |
nd->add_read_packet(nd, fd_can_read, fd_read, opaque); |
1340 |
} |
1341 |
|
1342 |
/* dummy network adapter */
|
1343 |
|
1344 |
static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
1345 |
{ |
1346 |
} |
1347 |
|
1348 |
static void dummy_add_read_packet(NetDriverState *nd, |
1349 |
IOCanRWHandler *fd_can_read, |
1350 |
IOReadHandler *fd_read, void *opaque)
|
1351 |
{ |
1352 |
} |
1353 |
|
1354 |
static int net_dummy_init(NetDriverState *nd) |
1355 |
{ |
1356 |
nd->send_packet = dummy_send_packet; |
1357 |
nd->add_read_packet = dummy_add_read_packet; |
1358 |
pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy"); |
1359 |
return 0; |
1360 |
} |
1361 |
|
1362 |
#if defined(CONFIG_SLIRP)
|
1363 |
|
1364 |
/* slirp network adapter */
|
1365 |
|
1366 |
static void *slirp_fd_opaque; |
1367 |
static IOCanRWHandler *slirp_fd_can_read;
|
1368 |
static IOReadHandler *slirp_fd_read;
|
1369 |
static int slirp_inited; |
1370 |
|
1371 |
int slirp_can_output(void) |
1372 |
{ |
1373 |
return slirp_fd_can_read(slirp_fd_opaque);
|
1374 |
} |
1375 |
|
1376 |
void slirp_output(const uint8_t *pkt, int pkt_len) |
1377 |
{ |
1378 |
#if 0
|
1379 |
printf("output:\n");
|
1380 |
hex_dump(stdout, pkt, pkt_len);
|
1381 |
#endif
|
1382 |
slirp_fd_read(slirp_fd_opaque, pkt, pkt_len); |
1383 |
} |
1384 |
|
1385 |
static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
1386 |
{ |
1387 |
#if 0
|
1388 |
printf("input:\n");
|
1389 |
hex_dump(stdout, buf, size);
|
1390 |
#endif
|
1391 |
slirp_input(buf, size); |
1392 |
} |
1393 |
|
1394 |
static void slirp_add_read_packet(NetDriverState *nd, |
1395 |
IOCanRWHandler *fd_can_read, |
1396 |
IOReadHandler *fd_read, void *opaque)
|
1397 |
{ |
1398 |
slirp_fd_opaque = opaque; |
1399 |
slirp_fd_can_read = fd_can_read; |
1400 |
slirp_fd_read = fd_read; |
1401 |
} |
1402 |
|
1403 |
static int net_slirp_init(NetDriverState *nd) |
1404 |
{ |
1405 |
if (!slirp_inited) {
|
1406 |
slirp_inited = 1;
|
1407 |
slirp_init(); |
1408 |
} |
1409 |
nd->send_packet = slirp_send_packet; |
1410 |
nd->add_read_packet = slirp_add_read_packet; |
1411 |
pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp"); |
1412 |
return 0; |
1413 |
} |
1414 |
|
1415 |
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
1416 |
{ |
1417 |
const char *p, *p1; |
1418 |
int len;
|
1419 |
p = *pp; |
1420 |
p1 = strchr(p, sep); |
1421 |
if (!p1)
|
1422 |
return -1; |
1423 |
len = p1 - p; |
1424 |
p1++; |
1425 |
if (buf_size > 0) { |
1426 |
if (len > buf_size - 1) |
1427 |
len = buf_size - 1;
|
1428 |
memcpy(buf, p, len); |
1429 |
buf[len] = '\0';
|
1430 |
} |
1431 |
*pp = p1; |
1432 |
return 0; |
1433 |
} |
1434 |
|
1435 |
static void net_slirp_redir(const char *redir_str) |
1436 |
{ |
1437 |
int is_udp;
|
1438 |
char buf[256], *r; |
1439 |
const char *p; |
1440 |
struct in_addr guest_addr;
|
1441 |
int host_port, guest_port;
|
1442 |
|
1443 |
if (!slirp_inited) {
|
1444 |
slirp_inited = 1;
|
1445 |
slirp_init(); |
1446 |
} |
1447 |
|
1448 |
p = redir_str; |
1449 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
1450 |
goto fail;
|
1451 |
if (!strcmp(buf, "tcp")) { |
1452 |
is_udp = 0;
|
1453 |
} else if (!strcmp(buf, "udp")) { |
1454 |
is_udp = 1;
|
1455 |
} else {
|
1456 |
goto fail;
|
1457 |
} |
1458 |
|
1459 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
1460 |
goto fail;
|
1461 |
host_port = strtol(buf, &r, 0);
|
1462 |
if (r == buf)
|
1463 |
goto fail;
|
1464 |
|
1465 |
if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
1466 |
goto fail;
|
1467 |
if (buf[0] == '\0') { |
1468 |
pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
1469 |
} |
1470 |
if (!inet_aton(buf, &guest_addr))
|
1471 |
goto fail;
|
1472 |
|
1473 |
guest_port = strtol(p, &r, 0);
|
1474 |
if (r == p)
|
1475 |
goto fail;
|
1476 |
|
1477 |
if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
1478 |
fprintf(stderr, "qemu: could not set up redirection\n");
|
1479 |
exit(1);
|
1480 |
} |
1481 |
return;
|
1482 |
fail:
|
1483 |
fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
|
1484 |
exit(1);
|
1485 |
} |
1486 |
|
1487 |
#ifndef _WIN32
|
1488 |
|
1489 |
char smb_dir[1024]; |
1490 |
|
1491 |
static void smb_exit(void) |
1492 |
{ |
1493 |
DIR *d; |
1494 |
struct dirent *de;
|
1495 |
char filename[1024]; |
1496 |
|
1497 |
/* erase all the files in the directory */
|
1498 |
d = opendir(smb_dir); |
1499 |
for(;;) {
|
1500 |
de = readdir(d); |
1501 |
if (!de)
|
1502 |
break;
|
1503 |
if (strcmp(de->d_name, ".") != 0 && |
1504 |
strcmp(de->d_name, "..") != 0) { |
1505 |
snprintf(filename, sizeof(filename), "%s/%s", |
1506 |
smb_dir, de->d_name); |
1507 |
unlink(filename); |
1508 |
} |
1509 |
} |
1510 |
closedir(d); |
1511 |
rmdir(smb_dir); |
1512 |
} |
1513 |
|
1514 |
/* automatic user mode samba server configuration */
|
1515 |
void net_slirp_smb(const char *exported_dir) |
1516 |
{ |
1517 |
char smb_conf[1024]; |
1518 |
char smb_cmdline[1024]; |
1519 |
FILE *f; |
1520 |
|
1521 |
if (!slirp_inited) {
|
1522 |
slirp_inited = 1;
|
1523 |
slirp_init(); |
1524 |
} |
1525 |
|
1526 |
/* XXX: better tmp dir construction */
|
1527 |
snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
1528 |
if (mkdir(smb_dir, 0700) < 0) { |
1529 |
fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
|
1530 |
exit(1);
|
1531 |
} |
1532 |
snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
1533 |
|
1534 |
f = fopen(smb_conf, "w");
|
1535 |
if (!f) {
|
1536 |
fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
|
1537 |
exit(1);
|
1538 |
} |
1539 |
fprintf(f, |
1540 |
"[global]\n"
|
1541 |
"pid directory=%s\n"
|
1542 |
"lock directory=%s\n"
|
1543 |
"log file=%s/log.smbd\n"
|
1544 |
"smb passwd file=%s/smbpasswd\n"
|
1545 |
"security = share\n"
|
1546 |
"[qemu]\n"
|
1547 |
"path=%s\n"
|
1548 |
"read only=no\n"
|
1549 |
"guest ok=yes\n",
|
1550 |
smb_dir, |
1551 |
smb_dir, |
1552 |
smb_dir, |
1553 |
smb_dir, |
1554 |
exported_dir |
1555 |
); |
1556 |
fclose(f); |
1557 |
atexit(smb_exit); |
1558 |
|
1559 |
snprintf(smb_cmdline, sizeof(smb_cmdline), "/usr/sbin/smbd -s %s", |
1560 |
smb_conf); |
1561 |
|
1562 |
slirp_add_exec(0, smb_cmdline, 4, 139); |
1563 |
} |
1564 |
|
1565 |
#endif /* !defined(_WIN32) */ |
1566 |
|
1567 |
#endif /* CONFIG_SLIRP */ |
1568 |
|
1569 |
#if !defined(_WIN32)
|
1570 |
#ifdef _BSD
|
1571 |
static int tun_open(char *ifname, int ifname_size) |
1572 |
{ |
1573 |
int fd;
|
1574 |
char *dev;
|
1575 |
struct stat s;
|
1576 |
|
1577 |
fd = open("/dev/tap", O_RDWR);
|
1578 |
if (fd < 0) { |
1579 |
fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
|
1580 |
return -1; |
1581 |
} |
1582 |
|
1583 |
fstat(fd, &s); |
1584 |
dev = devname(s.st_rdev, S_IFCHR); |
1585 |
pstrcpy(ifname, ifname_size, dev); |
1586 |
|
1587 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
1588 |
return fd;
|
1589 |
} |
1590 |
#else
|
1591 |
static int tun_open(char *ifname, int ifname_size) |
1592 |
{ |
1593 |
struct ifreq ifr;
|
1594 |
int fd, ret;
|
1595 |
|
1596 |
fd = open("/dev/net/tun", O_RDWR);
|
1597 |
if (fd < 0) { |
1598 |
fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
1599 |
return -1; |
1600 |
} |
1601 |
memset(&ifr, 0, sizeof(ifr)); |
1602 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
1603 |
pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
|
1604 |
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
1605 |
if (ret != 0) { |
1606 |
fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
1607 |
close(fd); |
1608 |
return -1; |
1609 |
} |
1610 |
printf("Connected to host network interface: %s\n", ifr.ifr_name);
|
1611 |
pstrcpy(ifname, ifname_size, ifr.ifr_name); |
1612 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
1613 |
return fd;
|
1614 |
} |
1615 |
#endif
|
1616 |
|
1617 |
static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
1618 |
{ |
1619 |
write(nd->fd, buf, size); |
1620 |
} |
1621 |
|
1622 |
static void tun_add_read_packet(NetDriverState *nd, |
1623 |
IOCanRWHandler *fd_can_read, |
1624 |
IOReadHandler *fd_read, void *opaque)
|
1625 |
{ |
1626 |
qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque); |
1627 |
} |
1628 |
|
1629 |
static int net_tun_init(NetDriverState *nd) |
1630 |
{ |
1631 |
int pid, status;
|
1632 |
char *args[3]; |
1633 |
char **parg;
|
1634 |
|
1635 |
nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
|
1636 |
if (nd->fd < 0) |
1637 |
return -1; |
1638 |
|
1639 |
/* try to launch network init script */
|
1640 |
pid = fork(); |
1641 |
if (pid >= 0) { |
1642 |
if (pid == 0) { |
1643 |
parg = args; |
1644 |
*parg++ = network_script; |
1645 |
*parg++ = nd->ifname; |
1646 |
*parg++ = NULL;
|
1647 |
execv(network_script, args); |
1648 |
exit(1);
|
1649 |
} |
1650 |
while (waitpid(pid, &status, 0) != pid); |
1651 |
if (!WIFEXITED(status) ||
|
1652 |
WEXITSTATUS(status) != 0) {
|
1653 |
fprintf(stderr, "%s: could not launch network script\n",
|
1654 |
network_script); |
1655 |
} |
1656 |
} |
1657 |
nd->send_packet = tun_send_packet; |
1658 |
nd->add_read_packet = tun_add_read_packet; |
1659 |
return 0; |
1660 |
} |
1661 |
|
1662 |
static int net_fd_init(NetDriverState *nd, int fd) |
1663 |
{ |
1664 |
nd->fd = fd; |
1665 |
nd->send_packet = tun_send_packet; |
1666 |
nd->add_read_packet = tun_add_read_packet; |
1667 |
pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd"); |
1668 |
return 0; |
1669 |
} |
1670 |
|
1671 |
#endif /* !_WIN32 */ |
1672 |
|
1673 |
/***********************************************************/
|
1674 |
/* pid file */
|
1675 |
|
1676 |
static char *pid_filename; |
1677 |
|
1678 |
/* Remove PID file. Called on normal exit */
|
1679 |
|
1680 |
static void remove_pidfile(void) |
1681 |
{ |
1682 |
unlink (pid_filename); |
1683 |
} |
1684 |
|
1685 |
static void create_pidfile(const char *filename) |
1686 |
{ |
1687 |
struct stat pidstat;
|
1688 |
FILE *f; |
1689 |
|
1690 |
/* Try to write our PID to the named file */
|
1691 |
if (stat(filename, &pidstat) < 0) { |
1692 |
if (errno == ENOENT) {
|
1693 |
if ((f = fopen (filename, "w")) == NULL) { |
1694 |
perror("Opening pidfile");
|
1695 |
exit(1);
|
1696 |
} |
1697 |
fprintf(f, "%d\n", getpid());
|
1698 |
fclose(f); |
1699 |
pid_filename = qemu_strdup(filename); |
1700 |
if (!pid_filename) {
|
1701 |
fprintf(stderr, "Could not save PID filename");
|
1702 |
exit(1);
|
1703 |
} |
1704 |
atexit(remove_pidfile); |
1705 |
} |
1706 |
} else {
|
1707 |
fprintf(stderr, "%s already exists. Remove it and try again.\n",
|
1708 |
filename); |
1709 |
exit(1);
|
1710 |
} |
1711 |
} |
1712 |
|
1713 |
/***********************************************************/
|
1714 |
/* dumb display */
|
1715 |
|
1716 |
static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
1717 |
{ |
1718 |
} |
1719 |
|
1720 |
static void dumb_resize(DisplayState *ds, int w, int h) |
1721 |
{ |
1722 |
} |
1723 |
|
1724 |
static void dumb_refresh(DisplayState *ds) |
1725 |
{ |
1726 |
vga_update_display(); |
1727 |
} |
1728 |
|
1729 |
void dumb_display_init(DisplayState *ds)
|
1730 |
{ |
1731 |
ds->data = NULL;
|
1732 |
ds->linesize = 0;
|
1733 |
ds->depth = 0;
|
1734 |
ds->dpy_update = dumb_update; |
1735 |
ds->dpy_resize = dumb_resize; |
1736 |
ds->dpy_refresh = dumb_refresh; |
1737 |
} |
1738 |
|
1739 |
#if !defined(CONFIG_SOFTMMU)
|
1740 |
/***********************************************************/
|
1741 |
/* cpu signal handler */
|
1742 |
static void host_segv_handler(int host_signum, siginfo_t *info, |
1743 |
void *puc)
|
1744 |
{ |
1745 |
if (cpu_signal_handler(host_signum, info, puc))
|
1746 |
return;
|
1747 |
if (stdio_nb_clients > 0) |
1748 |
term_exit(); |
1749 |
abort(); |
1750 |
} |
1751 |
#endif
|
1752 |
|
1753 |
/***********************************************************/
|
1754 |
/* I/O handling */
|
1755 |
|
1756 |
#define MAX_IO_HANDLERS 64 |
1757 |
|
1758 |
typedef struct IOHandlerRecord { |
1759 |
int fd;
|
1760 |
IOCanRWHandler *fd_can_read; |
1761 |
IOReadHandler *fd_read; |
1762 |
void *opaque;
|
1763 |
/* temporary data */
|
1764 |
struct pollfd *ufd;
|
1765 |
int max_size;
|
1766 |
struct IOHandlerRecord *next;
|
1767 |
} IOHandlerRecord; |
1768 |
|
1769 |
static IOHandlerRecord *first_io_handler;
|
1770 |
|
1771 |
int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, |
1772 |
IOReadHandler *fd_read, void *opaque)
|
1773 |
{ |
1774 |
IOHandlerRecord *ioh; |
1775 |
|
1776 |
ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
1777 |
if (!ioh)
|
1778 |
return -1; |
1779 |
ioh->fd = fd; |
1780 |
ioh->fd_can_read = fd_can_read; |
1781 |
ioh->fd_read = fd_read; |
1782 |
ioh->opaque = opaque; |
1783 |
ioh->next = first_io_handler; |
1784 |
first_io_handler = ioh; |
1785 |
return 0; |
1786 |
} |
1787 |
|
1788 |
void qemu_del_fd_read_handler(int fd) |
1789 |
{ |
1790 |
IOHandlerRecord **pioh, *ioh; |
1791 |
|
1792 |
pioh = &first_io_handler; |
1793 |
for(;;) {
|
1794 |
ioh = *pioh; |
1795 |
if (ioh == NULL) |
1796 |
break;
|
1797 |
if (ioh->fd == fd) {
|
1798 |
*pioh = ioh->next; |
1799 |
break;
|
1800 |
} |
1801 |
pioh = &ioh->next; |
1802 |
} |
1803 |
} |
1804 |
|
1805 |
/***********************************************************/
|
1806 |
/* savevm/loadvm support */
|
1807 |
|
1808 |
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
1809 |
{ |
1810 |
fwrite(buf, 1, size, f);
|
1811 |
} |
1812 |
|
1813 |
void qemu_put_byte(QEMUFile *f, int v) |
1814 |
{ |
1815 |
fputc(v, f); |
1816 |
} |
1817 |
|
1818 |
void qemu_put_be16(QEMUFile *f, unsigned int v) |
1819 |
{ |
1820 |
qemu_put_byte(f, v >> 8);
|
1821 |
qemu_put_byte(f, v); |
1822 |
} |
1823 |
|
1824 |
void qemu_put_be32(QEMUFile *f, unsigned int v) |
1825 |
{ |
1826 |
qemu_put_byte(f, v >> 24);
|
1827 |
qemu_put_byte(f, v >> 16);
|
1828 |
qemu_put_byte(f, v >> 8);
|
1829 |
qemu_put_byte(f, v); |
1830 |
} |
1831 |
|
1832 |
void qemu_put_be64(QEMUFile *f, uint64_t v)
|
1833 |
{ |
1834 |
qemu_put_be32(f, v >> 32);
|
1835 |
qemu_put_be32(f, v); |
1836 |
} |
1837 |
|
1838 |
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) |
1839 |
{ |
1840 |
return fread(buf, 1, size, f); |
1841 |
} |
1842 |
|
1843 |
int qemu_get_byte(QEMUFile *f)
|
1844 |
{ |
1845 |
int v;
|
1846 |
v = fgetc(f); |
1847 |
if (v == EOF) |
1848 |
return 0; |
1849 |
else
|
1850 |
return v;
|
1851 |
} |
1852 |
|
1853 |
unsigned int qemu_get_be16(QEMUFile *f) |
1854 |
{ |
1855 |
unsigned int v; |
1856 |
v = qemu_get_byte(f) << 8;
|
1857 |
v |= qemu_get_byte(f); |
1858 |
return v;
|
1859 |
} |
1860 |
|
1861 |
unsigned int qemu_get_be32(QEMUFile *f) |
1862 |
{ |
1863 |
unsigned int v; |
1864 |
v = qemu_get_byte(f) << 24;
|
1865 |
v |= qemu_get_byte(f) << 16;
|
1866 |
v |= qemu_get_byte(f) << 8;
|
1867 |
v |= qemu_get_byte(f); |
1868 |
return v;
|
1869 |
} |
1870 |
|
1871 |
uint64_t qemu_get_be64(QEMUFile *f) |
1872 |
{ |
1873 |
uint64_t v; |
1874 |
v = (uint64_t)qemu_get_be32(f) << 32;
|
1875 |
v |= qemu_get_be32(f); |
1876 |
return v;
|
1877 |
} |
1878 |
|
1879 |
int64_t qemu_ftell(QEMUFile *f) |
1880 |
{ |
1881 |
return ftell(f);
|
1882 |
} |
1883 |
|
1884 |
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
1885 |
{ |
1886 |
if (fseek(f, pos, whence) < 0) |
1887 |
return -1; |
1888 |
return ftell(f);
|
1889 |
} |
1890 |
|
1891 |
typedef struct SaveStateEntry { |
1892 |
char idstr[256]; |
1893 |
int instance_id;
|
1894 |
int version_id;
|
1895 |
SaveStateHandler *save_state; |
1896 |
LoadStateHandler *load_state; |
1897 |
void *opaque;
|
1898 |
struct SaveStateEntry *next;
|
1899 |
} SaveStateEntry; |
1900 |
|
1901 |
static SaveStateEntry *first_se;
|
1902 |
|
1903 |
int register_savevm(const char *idstr, |
1904 |
int instance_id,
|
1905 |
int version_id,
|
1906 |
SaveStateHandler *save_state, |
1907 |
LoadStateHandler *load_state, |
1908 |
void *opaque)
|
1909 |
{ |
1910 |
SaveStateEntry *se, **pse; |
1911 |
|
1912 |
se = qemu_malloc(sizeof(SaveStateEntry));
|
1913 |
if (!se)
|
1914 |
return -1; |
1915 |
pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
1916 |
se->instance_id = instance_id; |
1917 |
se->version_id = version_id; |
1918 |
se->save_state = save_state; |
1919 |
se->load_state = load_state; |
1920 |
se->opaque = opaque; |
1921 |
se->next = NULL;
|
1922 |
|
1923 |
/* add at the end of list */
|
1924 |
pse = &first_se; |
1925 |
while (*pse != NULL) |
1926 |
pse = &(*pse)->next; |
1927 |
*pse = se; |
1928 |
return 0; |
1929 |
} |
1930 |
|
1931 |
#define QEMU_VM_FILE_MAGIC 0x5145564d |
1932 |
#define QEMU_VM_FILE_VERSION 0x00000001 |
1933 |
|
1934 |
int qemu_savevm(const char *filename) |
1935 |
{ |
1936 |
SaveStateEntry *se; |
1937 |
QEMUFile *f; |
1938 |
int len, len_pos, cur_pos, saved_vm_running, ret;
|
1939 |
|
1940 |
saved_vm_running = vm_running; |
1941 |
vm_stop(0);
|
1942 |
|
1943 |
f = fopen(filename, "wb");
|
1944 |
if (!f) {
|
1945 |
ret = -1;
|
1946 |
goto the_end;
|
1947 |
} |
1948 |
|
1949 |
qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
1950 |
qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
1951 |
|
1952 |
for(se = first_se; se != NULL; se = se->next) { |
1953 |
/* ID string */
|
1954 |
len = strlen(se->idstr); |
1955 |
qemu_put_byte(f, len); |
1956 |
qemu_put_buffer(f, se->idstr, len); |
1957 |
|
1958 |
qemu_put_be32(f, se->instance_id); |
1959 |
qemu_put_be32(f, se->version_id); |
1960 |
|
1961 |
/* record size: filled later */
|
1962 |
len_pos = ftell(f); |
1963 |
qemu_put_be32(f, 0);
|
1964 |
|
1965 |
se->save_state(f, se->opaque); |
1966 |
|
1967 |
/* fill record size */
|
1968 |
cur_pos = ftell(f); |
1969 |
len = ftell(f) - len_pos - 4;
|
1970 |
fseek(f, len_pos, SEEK_SET); |
1971 |
qemu_put_be32(f, len); |
1972 |
fseek(f, cur_pos, SEEK_SET); |
1973 |
} |
1974 |
|
1975 |
fclose(f); |
1976 |
ret = 0;
|
1977 |
the_end:
|
1978 |
if (saved_vm_running)
|
1979 |
vm_start(); |
1980 |
return ret;
|
1981 |
} |
1982 |
|
1983 |
static SaveStateEntry *find_se(const char *idstr, int instance_id) |
1984 |
{ |
1985 |
SaveStateEntry *se; |
1986 |
|
1987 |
for(se = first_se; se != NULL; se = se->next) { |
1988 |
if (!strcmp(se->idstr, idstr) &&
|
1989 |
instance_id == se->instance_id) |
1990 |
return se;
|
1991 |
} |
1992 |
return NULL; |
1993 |
} |
1994 |
|
1995 |
int qemu_loadvm(const char *filename) |
1996 |
{ |
1997 |
SaveStateEntry *se; |
1998 |
QEMUFile *f; |
1999 |
int len, cur_pos, ret, instance_id, record_len, version_id;
|
2000 |
int saved_vm_running;
|
2001 |
unsigned int v; |
2002 |
char idstr[256]; |
2003 |
|
2004 |
saved_vm_running = vm_running; |
2005 |
vm_stop(0);
|
2006 |
|
2007 |
f = fopen(filename, "rb");
|
2008 |
if (!f) {
|
2009 |
ret = -1;
|
2010 |
goto the_end;
|
2011 |
} |
2012 |
|
2013 |
v = qemu_get_be32(f); |
2014 |
if (v != QEMU_VM_FILE_MAGIC)
|
2015 |
goto fail;
|
2016 |
v = qemu_get_be32(f); |
2017 |
if (v != QEMU_VM_FILE_VERSION) {
|
2018 |
fail:
|
2019 |
fclose(f); |
2020 |
ret = -1;
|
2021 |
goto the_end;
|
2022 |
} |
2023 |
for(;;) {
|
2024 |
#if defined (DO_TB_FLUSH)
|
2025 |
tb_flush(global_env); |
2026 |
#endif
|
2027 |
len = qemu_get_byte(f); |
2028 |
if (feof(f))
|
2029 |
break;
|
2030 |
qemu_get_buffer(f, idstr, len); |
2031 |
idstr[len] = '\0';
|
2032 |
instance_id = qemu_get_be32(f); |
2033 |
version_id = qemu_get_be32(f); |
2034 |
record_len = qemu_get_be32(f); |
2035 |
#if 0
|
2036 |
printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
2037 |
idstr, instance_id, version_id, record_len);
|
2038 |
#endif
|
2039 |
cur_pos = ftell(f); |
2040 |
se = find_se(idstr, instance_id); |
2041 |
if (!se) {
|
2042 |
fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
2043 |
instance_id, idstr); |
2044 |
} else {
|
2045 |
ret = se->load_state(f, se->opaque, version_id); |
2046 |
if (ret < 0) { |
2047 |
fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
2048 |
instance_id, idstr); |
2049 |
} |
2050 |
} |
2051 |
/* always seek to exact end of record */
|
2052 |
qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
2053 |
} |
2054 |
fclose(f); |
2055 |
ret = 0;
|
2056 |
the_end:
|
2057 |
if (saved_vm_running)
|
2058 |
vm_start(); |
2059 |
return ret;
|
2060 |
} |
2061 |
|
2062 |
/***********************************************************/
|
2063 |
/* cpu save/restore */
|
2064 |
|
2065 |
#if defined(TARGET_I386)
|
2066 |
|
2067 |
static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
2068 |
{ |
2069 |
qemu_put_be32(f, dt->selector); |
2070 |
qemu_put_betl(f, dt->base); |
2071 |
qemu_put_be32(f, dt->limit); |
2072 |
qemu_put_be32(f, dt->flags); |
2073 |
} |
2074 |
|
2075 |
static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
2076 |
{ |
2077 |
dt->selector = qemu_get_be32(f); |
2078 |
dt->base = qemu_get_betl(f); |
2079 |
dt->limit = qemu_get_be32(f); |
2080 |
dt->flags = qemu_get_be32(f); |
2081 |
} |
2082 |
|
2083 |
void cpu_save(QEMUFile *f, void *opaque) |
2084 |
{ |
2085 |
CPUState *env = opaque; |
2086 |
uint16_t fptag, fpus, fpuc, fpregs_format; |
2087 |
uint32_t hflags; |
2088 |
int i;
|
2089 |
|
2090 |
for(i = 0; i < CPU_NB_REGS; i++) |
2091 |
qemu_put_betls(f, &env->regs[i]); |
2092 |
qemu_put_betls(f, &env->eip); |
2093 |
qemu_put_betls(f, &env->eflags); |
2094 |
hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
|
2095 |
qemu_put_be32s(f, &hflags); |
2096 |
|
2097 |
/* FPU */
|
2098 |
fpuc = env->fpuc; |
2099 |
fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
2100 |
fptag = 0;
|
2101 |
for(i = 0; i < 8; i++) { |
2102 |
fptag |= ((!env->fptags[i]) << i); |
2103 |
} |
2104 |
|
2105 |
qemu_put_be16s(f, &fpuc); |
2106 |
qemu_put_be16s(f, &fpus); |
2107 |
qemu_put_be16s(f, &fptag); |
2108 |
|
2109 |
#ifdef USE_X86LDOUBLE
|
2110 |
fpregs_format = 0;
|
2111 |
#else
|
2112 |
fpregs_format = 1;
|
2113 |
#endif
|
2114 |
qemu_put_be16s(f, &fpregs_format); |
2115 |
|
2116 |
for(i = 0; i < 8; i++) { |
2117 |
#ifdef USE_X86LDOUBLE
|
2118 |
{ |
2119 |
uint64_t mant; |
2120 |
uint16_t exp; |
2121 |
/* we save the real CPU data (in case of MMX usage only 'mant'
|
2122 |
contains the MMX register */
|
2123 |
cpu_get_fp80(&mant, &exp, env->fpregs[i].d); |
2124 |
qemu_put_be64(f, mant); |
2125 |
qemu_put_be16(f, exp); |
2126 |
} |
2127 |
#else
|
2128 |
/* if we use doubles for float emulation, we save the doubles to
|
2129 |
avoid losing information in case of MMX usage. It can give
|
2130 |
problems if the image is restored on a CPU where long
|
2131 |
doubles are used instead. */
|
2132 |
qemu_put_be64(f, env->fpregs[i].mmx.MMX_Q(0));
|
2133 |
#endif
|
2134 |
} |
2135 |
|
2136 |
for(i = 0; i < 6; i++) |
2137 |
cpu_put_seg(f, &env->segs[i]); |
2138 |
cpu_put_seg(f, &env->ldt); |
2139 |
cpu_put_seg(f, &env->tr); |
2140 |
cpu_put_seg(f, &env->gdt); |
2141 |
cpu_put_seg(f, &env->idt); |
2142 |
|
2143 |
qemu_put_be32s(f, &env->sysenter_cs); |
2144 |
qemu_put_be32s(f, &env->sysenter_esp); |
2145 |
qemu_put_be32s(f, &env->sysenter_eip); |
2146 |
|
2147 |
qemu_put_betls(f, &env->cr[0]);
|
2148 |
qemu_put_betls(f, &env->cr[2]);
|
2149 |
qemu_put_betls(f, &env->cr[3]);
|
2150 |
qemu_put_betls(f, &env->cr[4]);
|
2151 |
|
2152 |
for(i = 0; i < 8; i++) |
2153 |
qemu_put_betls(f, &env->dr[i]); |
2154 |
|
2155 |
/* MMU */
|
2156 |
qemu_put_be32s(f, &env->a20_mask); |
2157 |
|
2158 |
/* XMM */
|
2159 |
qemu_put_be32s(f, &env->mxcsr); |
2160 |
for(i = 0; i < CPU_NB_REGS; i++) { |
2161 |
qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(0));
|
2162 |
qemu_put_be64s(f, &env->xmm_regs[i].XMM_Q(1));
|
2163 |
} |
2164 |
|
2165 |
#ifdef TARGET_X86_64
|
2166 |
qemu_put_be64s(f, &env->efer); |
2167 |
qemu_put_be64s(f, &env->star); |
2168 |
qemu_put_be64s(f, &env->lstar); |
2169 |
qemu_put_be64s(f, &env->cstar); |
2170 |
qemu_put_be64s(f, &env->fmask); |
2171 |
qemu_put_be64s(f, &env->kernelgsbase); |
2172 |
#endif
|
2173 |
} |
2174 |
|
2175 |
#ifdef USE_X86LDOUBLE
|
2176 |
/* XXX: add that in a FPU generic layer */
|
2177 |
union x86_longdouble {
|
2178 |
uint64_t mant; |
2179 |
uint16_t exp; |
2180 |
}; |
2181 |
|
2182 |
#define MANTD1(fp) (fp & ((1LL << 52) - 1)) |
2183 |
#define EXPBIAS1 1023 |
2184 |
#define EXPD1(fp) ((fp >> 52) & 0x7FF) |
2185 |
#define SIGND1(fp) ((fp >> 32) & 0x80000000) |
2186 |
|
2187 |
static void fp64_to_fp80(union x86_longdouble *p, uint64_t temp) |
2188 |
{ |
2189 |
int e;
|
2190 |
/* mantissa */
|
2191 |
p->mant = (MANTD1(temp) << 11) | (1LL << 63); |
2192 |
/* exponent + sign */
|
2193 |
e = EXPD1(temp) - EXPBIAS1 + 16383;
|
2194 |
e |= SIGND1(temp) >> 16;
|
2195 |
p->exp = e; |
2196 |
} |
2197 |
#endif
|
2198 |
|
2199 |
int cpu_load(QEMUFile *f, void *opaque, int version_id) |
2200 |
{ |
2201 |
CPUState *env = opaque; |
2202 |
int i, guess_mmx;
|
2203 |
uint32_t hflags; |
2204 |
uint16_t fpus, fpuc, fptag, fpregs_format; |
2205 |
|
2206 |
if (version_id != 3) |
2207 |
return -EINVAL;
|
2208 |
for(i = 0; i < CPU_NB_REGS; i++) |
2209 |
qemu_get_betls(f, &env->regs[i]); |
2210 |
qemu_get_betls(f, &env->eip); |
2211 |
qemu_get_betls(f, &env->eflags); |
2212 |
qemu_get_be32s(f, &hflags); |
2213 |
|
2214 |
qemu_get_be16s(f, &fpuc); |
2215 |
qemu_get_be16s(f, &fpus); |
2216 |
qemu_get_be16s(f, &fptag); |
2217 |
qemu_get_be16s(f, &fpregs_format); |
2218 |
|
2219 |
/* NOTE: we cannot always restore the FPU state if the image come
|
2220 |
from a host with a different 'USE_X86LDOUBLE' define. We guess
|
2221 |
if we are in an MMX state to restore correctly in that case. */
|
2222 |
guess_mmx = ((fptag == 0xff) && (fpus & 0x3800) == 0); |
2223 |
for(i = 0; i < 8; i++) { |
2224 |
uint64_t mant; |
2225 |
uint16_t exp; |
2226 |
|
2227 |
switch(fpregs_format) {
|
2228 |
case 0: |
2229 |
mant = qemu_get_be64(f); |
2230 |
exp = qemu_get_be16(f); |
2231 |
#ifdef USE_X86LDOUBLE
|
2232 |
env->fpregs[i].d = cpu_set_fp80(mant, exp); |
2233 |
#else
|
2234 |
/* difficult case */
|
2235 |
if (guess_mmx)
|
2236 |
env->fpregs[i].mmx.MMX_Q(0) = mant;
|
2237 |
else
|
2238 |
env->fpregs[i].d = cpu_set_fp80(mant, exp); |
2239 |
#endif
|
2240 |
break;
|
2241 |
case 1: |
2242 |
mant = qemu_get_be64(f); |
2243 |
#ifdef USE_X86LDOUBLE
|
2244 |
{ |
2245 |
union x86_longdouble *p;
|
2246 |
/* difficult case */
|
2247 |
p = (void *)&env->fpregs[i];
|
2248 |
if (guess_mmx) {
|
2249 |
p->mant = mant; |
2250 |
p->exp = 0xffff;
|
2251 |
} else {
|
2252 |
fp64_to_fp80(p, mant); |
2253 |
} |
2254 |
} |
2255 |
#else
|
2256 |
env->fpregs[i].mmx.MMX_Q(0) = mant;
|
2257 |
#endif
|
2258 |
break;
|
2259 |
default:
|
2260 |
return -EINVAL;
|
2261 |
} |
2262 |
} |
2263 |
|
2264 |
env->fpuc = fpuc; |
2265 |
env->fpstt = (fpus >> 11) & 7; |
2266 |
env->fpus = fpus & ~0x3800;
|
2267 |
fptag ^= 0xff;
|
2268 |
for(i = 0; i < 8; i++) { |
2269 |
env->fptags[i] = (fptag >> i) & 1;
|
2270 |
} |
2271 |
|
2272 |
for(i = 0; i < 6; i++) |
2273 |
cpu_get_seg(f, &env->segs[i]); |
2274 |
cpu_get_seg(f, &env->ldt); |
2275 |
cpu_get_seg(f, &env->tr); |
2276 |
cpu_get_seg(f, &env->gdt); |
2277 |
cpu_get_seg(f, &env->idt); |
2278 |
|
2279 |
qemu_get_be32s(f, &env->sysenter_cs); |
2280 |
qemu_get_be32s(f, &env->sysenter_esp); |
2281 |
qemu_get_be32s(f, &env->sysenter_eip); |
2282 |
|
2283 |
qemu_get_betls(f, &env->cr[0]);
|
2284 |
qemu_get_betls(f, &env->cr[2]);
|
2285 |
qemu_get_betls(f, &env->cr[3]);
|
2286 |
qemu_get_betls(f, &env->cr[4]);
|
2287 |
|
2288 |
for(i = 0; i < 8; i++) |
2289 |
qemu_get_betls(f, &env->dr[i]); |
2290 |
|
2291 |
/* MMU */
|
2292 |
qemu_get_be32s(f, &env->a20_mask); |
2293 |
|
2294 |
qemu_get_be32s(f, &env->mxcsr); |
2295 |
for(i = 0; i < CPU_NB_REGS; i++) { |
2296 |
qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(0));
|
2297 |
qemu_get_be64s(f, &env->xmm_regs[i].XMM_Q(1));
|
2298 |
} |
2299 |
|
2300 |
#ifdef TARGET_X86_64
|
2301 |
qemu_get_be64s(f, &env->efer); |
2302 |
qemu_get_be64s(f, &env->star); |
2303 |
qemu_get_be64s(f, &env->lstar); |
2304 |
qemu_get_be64s(f, &env->cstar); |
2305 |
qemu_get_be64s(f, &env->fmask); |
2306 |
qemu_get_be64s(f, &env->kernelgsbase); |
2307 |
#endif
|
2308 |
|
2309 |
/* XXX: compute hflags from scratch, except for CPL and IIF */
|
2310 |
env->hflags = hflags; |
2311 |
tlb_flush(env, 1);
|
2312 |
return 0; |
2313 |
} |
2314 |
|
2315 |
#elif defined(TARGET_PPC)
|
2316 |
void cpu_save(QEMUFile *f, void *opaque) |
2317 |
{ |
2318 |
} |
2319 |
|
2320 |
int cpu_load(QEMUFile *f, void *opaque, int version_id) |
2321 |
{ |
2322 |
return 0; |
2323 |
} |
2324 |
#elif defined(TARGET_SPARC)
|
2325 |
void cpu_save(QEMUFile *f, void *opaque) |
2326 |
{ |
2327 |
CPUState *env = opaque; |
2328 |
int i;
|
2329 |
uint32_t tmp; |
2330 |
|
2331 |
for(i = 1; i < 8; i++) |
2332 |
qemu_put_be32s(f, &env->gregs[i]); |
2333 |
tmp = env->regwptr - env->regbase; |
2334 |
qemu_put_be32s(f, &tmp); |
2335 |
for(i = 1; i < NWINDOWS * 16 + 8; i++) |
2336 |
qemu_put_be32s(f, &env->regbase[i]); |
2337 |
|
2338 |
/* FPU */
|
2339 |
for(i = 0; i < 32; i++) { |
2340 |
uint64_t mant; |
2341 |
uint16_t exp; |
2342 |
cpu_get_fp64(&mant, &exp, env->fpr[i]); |
2343 |
qemu_put_be64(f, mant); |
2344 |
qemu_put_be16(f, exp); |
2345 |
} |
2346 |
qemu_put_be32s(f, &env->pc); |
2347 |
qemu_put_be32s(f, &env->npc); |
2348 |
qemu_put_be32s(f, &env->y); |
2349 |
tmp = GET_PSR(env); |
2350 |
qemu_put_be32s(f, &tmp); |
2351 |
qemu_put_be32s(f, &env->fsr); |
2352 |
qemu_put_be32s(f, &env->cwp); |
2353 |
qemu_put_be32s(f, &env->wim); |
2354 |
qemu_put_be32s(f, &env->tbr); |
2355 |
/* MMU */
|
2356 |
for(i = 0; i < 16; i++) |
2357 |
qemu_put_be32s(f, &env->mmuregs[i]); |
2358 |
} |
2359 |
|
2360 |
int cpu_load(QEMUFile *f, void *opaque, int version_id) |
2361 |
{ |
2362 |
CPUState *env = opaque; |
2363 |
int i;
|
2364 |
uint32_t tmp; |
2365 |
|
2366 |
for(i = 1; i < 8; i++) |
2367 |
qemu_get_be32s(f, &env->gregs[i]); |
2368 |
qemu_get_be32s(f, &tmp); |
2369 |
env->regwptr = env->regbase + tmp; |
2370 |
for(i = 1; i < NWINDOWS * 16 + 8; i++) |
2371 |
qemu_get_be32s(f, &env->regbase[i]); |
2372 |
|
2373 |
/* FPU */
|
2374 |
for(i = 0; i < 32; i++) { |
2375 |
uint64_t mant; |
2376 |
uint16_t exp; |
2377 |
|
2378 |
qemu_get_be64s(f, &mant); |
2379 |
qemu_get_be16s(f, &exp); |
2380 |
env->fpr[i] = cpu_put_fp64(mant, exp); |
2381 |
} |
2382 |
qemu_get_be32s(f, &env->pc); |
2383 |
qemu_get_be32s(f, &env->npc); |
2384 |
qemu_get_be32s(f, &env->y); |
2385 |
qemu_get_be32s(f, &tmp); |
2386 |
PUT_PSR(env, tmp); |
2387 |
qemu_get_be32s(f, &env->fsr); |
2388 |
qemu_get_be32s(f, &env->cwp); |
2389 |
qemu_get_be32s(f, &env->wim); |
2390 |
qemu_get_be32s(f, &env->tbr); |
2391 |
/* MMU */
|
2392 |
for(i = 0; i < 16; i++) |
2393 |
qemu_get_be32s(f, &env->mmuregs[i]); |
2394 |
tlb_flush(env, 1);
|
2395 |
return 0; |
2396 |
} |
2397 |
#else
|
2398 |
|
2399 |
#warning No CPU save/restore functions
|
2400 |
|
2401 |
#endif
|
2402 |
|
2403 |
/***********************************************************/
|
2404 |
/* ram save/restore */
|
2405 |
|
2406 |
/* we just avoid storing empty pages */
|
2407 |
static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len) |
2408 |
{ |
2409 |
int i, v;
|
2410 |
|
2411 |
v = buf[0];
|
2412 |
for(i = 1; i < len; i++) { |
2413 |
if (buf[i] != v)
|
2414 |
goto normal_save;
|
2415 |
} |
2416 |
qemu_put_byte(f, 1);
|
2417 |
qemu_put_byte(f, v); |
2418 |
return;
|
2419 |
normal_save:
|
2420 |
qemu_put_byte(f, 0);
|
2421 |
qemu_put_buffer(f, buf, len); |
2422 |
} |
2423 |
|
2424 |
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
2425 |
{ |
2426 |
int v;
|
2427 |
|
2428 |
v = qemu_get_byte(f); |
2429 |
switch(v) {
|
2430 |
case 0: |
2431 |
if (qemu_get_buffer(f, buf, len) != len)
|
2432 |
return -EIO;
|
2433 |
break;
|
2434 |
case 1: |
2435 |
v = qemu_get_byte(f); |
2436 |
memset(buf, v, len); |
2437 |
break;
|
2438 |
default:
|
2439 |
return -EINVAL;
|
2440 |
} |
2441 |
return 0; |
2442 |
} |
2443 |
|
2444 |
static void ram_save(QEMUFile *f, void *opaque) |
2445 |
{ |
2446 |
int i;
|
2447 |
qemu_put_be32(f, phys_ram_size); |
2448 |
for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
2449 |
ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
2450 |
} |
2451 |
} |
2452 |
|
2453 |
static int ram_load(QEMUFile *f, void *opaque, int version_id) |
2454 |
{ |
2455 |
int i, ret;
|
2456 |
|
2457 |
if (version_id != 1) |
2458 |
return -EINVAL;
|
2459 |
if (qemu_get_be32(f) != phys_ram_size)
|
2460 |
return -EINVAL;
|
2461 |
for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
2462 |
ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
2463 |
if (ret)
|
2464 |
return ret;
|
2465 |
} |
2466 |
return 0; |
2467 |
} |
2468 |
|
2469 |
/***********************************************************/
|
2470 |
/* main execution loop */
|
2471 |
|
2472 |
void gui_update(void *opaque) |
2473 |
{ |
2474 |
display_state.dpy_refresh(&display_state); |
2475 |
qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
2476 |
} |
2477 |
|
2478 |
/* XXX: support several handlers */
|
2479 |
VMStopHandler *vm_stop_cb; |
2480 |
VMStopHandler *vm_stop_opaque; |
2481 |
|
2482 |
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
2483 |
{ |
2484 |
vm_stop_cb = cb; |
2485 |
vm_stop_opaque = opaque; |
2486 |
return 0; |
2487 |
} |
2488 |
|
2489 |
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
2490 |
{ |
2491 |
vm_stop_cb = NULL;
|
2492 |
} |
2493 |
|
2494 |
void vm_start(void) |
2495 |
{ |
2496 |
if (!vm_running) {
|
2497 |
cpu_enable_ticks(); |
2498 |
vm_running = 1;
|
2499 |
} |
2500 |
} |
2501 |
|
2502 |
void vm_stop(int reason) |
2503 |
{ |
2504 |
if (vm_running) {
|
2505 |
cpu_disable_ticks(); |
2506 |
vm_running = 0;
|
2507 |
if (reason != 0) { |
2508 |
if (vm_stop_cb) {
|
2509 |
vm_stop_cb(vm_stop_opaque, reason); |
2510 |
} |
2511 |
} |
2512 |
} |
2513 |
} |
2514 |
|
2515 |
/* reset/shutdown handler */
|
2516 |
|
2517 |
typedef struct QEMUResetEntry { |
2518 |
QEMUResetHandler *func; |
2519 |
void *opaque;
|
2520 |
struct QEMUResetEntry *next;
|
2521 |
} QEMUResetEntry; |
2522 |
|
2523 |
static QEMUResetEntry *first_reset_entry;
|
2524 |
static int reset_requested; |
2525 |
static int shutdown_requested; |
2526 |
|
2527 |
void qemu_register_reset(QEMUResetHandler *func, void *opaque) |
2528 |
{ |
2529 |
QEMUResetEntry **pre, *re; |
2530 |
|
2531 |
pre = &first_reset_entry; |
2532 |
while (*pre != NULL) |
2533 |
pre = &(*pre)->next; |
2534 |
re = qemu_mallocz(sizeof(QEMUResetEntry));
|
2535 |
re->func = func; |
2536 |
re->opaque = opaque; |
2537 |
re->next = NULL;
|
2538 |
*pre = re; |
2539 |
} |
2540 |
|
2541 |
void qemu_system_reset(void) |
2542 |
{ |
2543 |
QEMUResetEntry *re; |
2544 |
|
2545 |
/* reset all devices */
|
2546 |
for(re = first_reset_entry; re != NULL; re = re->next) { |
2547 |
re->func(re->opaque); |
2548 |
} |
2549 |
} |
2550 |
|
2551 |
void qemu_system_reset_request(void) |
2552 |
{ |
2553 |
reset_requested = 1;
|
2554 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
2555 |
} |
2556 |
|
2557 |
void qemu_system_shutdown_request(void) |
2558 |
{ |
2559 |
shutdown_requested = 1;
|
2560 |
cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT); |
2561 |
} |
2562 |
|
2563 |
static void main_cpu_reset(void *opaque) |
2564 |
{ |
2565 |
#if defined(TARGET_I386) || defined(TARGET_SPARC)
|
2566 |
CPUState *env = opaque; |
2567 |
cpu_reset(env); |
2568 |
#endif
|
2569 |
} |
2570 |
|
2571 |
void main_loop_wait(int timeout) |
2572 |
{ |
2573 |
#ifndef _WIN32
|
2574 |
struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf; |
2575 |
IOHandlerRecord *ioh, *ioh_next; |
2576 |
uint8_t buf[4096];
|
2577 |
int n, max_size;
|
2578 |
#endif
|
2579 |
int ret;
|
2580 |
|
2581 |
#ifdef _WIN32
|
2582 |
if (timeout > 0) |
2583 |
Sleep(timeout); |
2584 |
#else
|
2585 |
/* poll any events */
|
2586 |
/* XXX: separate device handlers from system ones */
|
2587 |
pf = ufds; |
2588 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
2589 |
if (!ioh->fd_can_read) {
|
2590 |
max_size = 0;
|
2591 |
pf->fd = ioh->fd; |
2592 |
pf->events = POLLIN; |
2593 |
ioh->ufd = pf; |
2594 |
pf++; |
2595 |
} else {
|
2596 |
max_size = ioh->fd_can_read(ioh->opaque); |
2597 |
if (max_size > 0) { |
2598 |
if (max_size > sizeof(buf)) |
2599 |
max_size = sizeof(buf);
|
2600 |
pf->fd = ioh->fd; |
2601 |
pf->events = POLLIN; |
2602 |
ioh->ufd = pf; |
2603 |
pf++; |
2604 |
} else {
|
2605 |
ioh->ufd = NULL;
|
2606 |
} |
2607 |
} |
2608 |
ioh->max_size = max_size; |
2609 |
} |
2610 |
|
2611 |
ret = poll(ufds, pf - ufds, timeout); |
2612 |
if (ret > 0) { |
2613 |
/* XXX: better handling of removal */
|
2614 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { |
2615 |
ioh_next = ioh->next; |
2616 |
pf = ioh->ufd; |
2617 |
if (pf) {
|
2618 |
if (pf->revents & POLLIN) {
|
2619 |
if (ioh->max_size == 0) { |
2620 |
/* just a read event */
|
2621 |
ioh->fd_read(ioh->opaque, NULL, 0); |
2622 |
} else {
|
2623 |
n = read(ioh->fd, buf, ioh->max_size); |
2624 |
if (n >= 0) { |
2625 |
ioh->fd_read(ioh->opaque, buf, n); |
2626 |
} else if (errno != EAGAIN) { |
2627 |
ioh->fd_read(ioh->opaque, NULL, -errno);
|
2628 |
} |
2629 |
} |
2630 |
} |
2631 |
} |
2632 |
} |
2633 |
} |
2634 |
#endif /* !defined(_WIN32) */ |
2635 |
#if defined(CONFIG_SLIRP)
|
2636 |
/* XXX: merge with poll() */
|
2637 |
if (slirp_inited) {
|
2638 |
fd_set rfds, wfds, xfds; |
2639 |
int nfds;
|
2640 |
struct timeval tv;
|
2641 |
|
2642 |
nfds = -1;
|
2643 |
FD_ZERO(&rfds); |
2644 |
FD_ZERO(&wfds); |
2645 |
FD_ZERO(&xfds); |
2646 |
slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
2647 |
tv.tv_sec = 0;
|
2648 |
tv.tv_usec = 0;
|
2649 |
ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
2650 |
if (ret >= 0) { |
2651 |
slirp_select_poll(&rfds, &wfds, &xfds); |
2652 |
} |
2653 |
} |
2654 |
#endif
|
2655 |
|
2656 |
if (vm_running) {
|
2657 |
qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
2658 |
qemu_get_clock(vm_clock)); |
2659 |
/* run dma transfers, if any */
|
2660 |
DMA_run(); |
2661 |
} |
2662 |
|
2663 |
/* real time timers */
|
2664 |
qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
2665 |
qemu_get_clock(rt_clock)); |
2666 |
} |
2667 |
|
2668 |
int main_loop(void) |
2669 |
{ |
2670 |
int ret, timeout;
|
2671 |
CPUState *env = global_env; |
2672 |
|
2673 |
for(;;) {
|
2674 |
if (vm_running) {
|
2675 |
ret = cpu_exec(env); |
2676 |
if (shutdown_requested) {
|
2677 |
ret = EXCP_INTERRUPT; |
2678 |
break;
|
2679 |
} |
2680 |
if (reset_requested) {
|
2681 |
reset_requested = 0;
|
2682 |
qemu_system_reset(); |
2683 |
ret = EXCP_INTERRUPT; |
2684 |
} |
2685 |
if (ret == EXCP_DEBUG) {
|
2686 |
vm_stop(EXCP_DEBUG); |
2687 |
} |
2688 |
/* if hlt instruction, we wait until the next IRQ */
|
2689 |
/* XXX: use timeout computed from timers */
|
2690 |
if (ret == EXCP_HLT)
|
2691 |
timeout = 10;
|
2692 |
else
|
2693 |
timeout = 0;
|
2694 |
} else {
|
2695 |
timeout = 10;
|
2696 |
} |
2697 |
main_loop_wait(timeout); |
2698 |
} |
2699 |
cpu_disable_ticks(); |
2700 |
return ret;
|
2701 |
} |
2702 |
|
2703 |
void help(void) |
2704 |
{ |
2705 |
printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n" |
2706 |
"usage: %s [options] [disk_image]\n"
|
2707 |
"\n"
|
2708 |
"'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
2709 |
"\n"
|
2710 |
"Standard options:\n"
|
2711 |
"-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
2712 |
"-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
2713 |
"-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
2714 |
"-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
2715 |
"-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
|
2716 |
"-snapshot write to temporary files instead of disk image files\n"
|
2717 |
"-m megs set virtual RAM size to megs MB [default=%d]\n"
|
2718 |
"-nographic disable graphical output and redirect serial I/Os to console\n"
|
2719 |
#ifndef _WIN32
|
2720 |
"-k language use keyboard layout (for example \"fr\" for French)\n"
|
2721 |
#endif
|
2722 |
"-enable-audio enable audio support\n"
|
2723 |
"-localtime set the real time clock to local time [default=utc]\n"
|
2724 |
"-full-screen start in full screen\n"
|
2725 |
#ifdef TARGET_PPC
|
2726 |
"-prep Simulate a PREP system (default is PowerMAC)\n"
|
2727 |
"-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
|
2728 |
#endif
|
2729 |
"\n"
|
2730 |
"Network options:\n"
|
2731 |
"-nics n simulate 'n' network cards [default=1]\n"
|
2732 |
"-macaddr addr set the mac address of the first interface\n"
|
2733 |
"-n script set tap/tun network init script [default=%s]\n"
|
2734 |
"-tun-fd fd use this fd as already opened tap/tun interface\n"
|
2735 |
#ifdef CONFIG_SLIRP
|
2736 |
"-user-net use user mode network stack [default if no tap/tun script]\n"
|
2737 |
"-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
|
2738 |
#ifndef _WIN32
|
2739 |
"-smb dir allow SMB access to files in 'dir' [-user-net]\n"
|
2740 |
#endif
|
2741 |
"-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
|
2742 |
" redirect TCP or UDP connections from host to guest [-user-net]\n"
|
2743 |
#endif
|
2744 |
"-dummy-net use dummy network stack\n"
|
2745 |
"\n"
|
2746 |
"Linux boot specific:\n"
|
2747 |
"-kernel bzImage use 'bzImage' as kernel image\n"
|
2748 |
"-append cmdline use 'cmdline' as kernel command line\n"
|
2749 |
"-initrd file use 'file' as initial ram disk\n"
|
2750 |
"\n"
|
2751 |
"Debug/Expert options:\n"
|
2752 |
"-monitor dev redirect the monitor to char device 'dev'\n"
|
2753 |
"-serial dev redirect the serial port to char device 'dev'\n"
|
2754 |
"-parallel dev redirect the parallel port to char device 'dev'\n"
|
2755 |
"-pidfile file Write PID to 'file'\n"
|
2756 |
"-S freeze CPU at startup (use 'c' to start execution)\n"
|
2757 |
"-s wait gdb connection to port %d\n"
|
2758 |
"-p port change gdb connection port\n"
|
2759 |
"-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
2760 |
"-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
|
2761 |
" translation (t=none or lba) (usually qemu can guess them)\n"
|
2762 |
"-L path set the directory for the BIOS and VGA BIOS\n"
|
2763 |
#ifdef USE_CODE_COPY
|
2764 |
"-no-code-copy disable code copy acceleration\n"
|
2765 |
#endif
|
2766 |
#ifdef TARGET_I386
|
2767 |
"-isa simulate an ISA-only system (default is PCI system)\n"
|
2768 |
"-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
|
2769 |
" (default is CL-GD5446 PCI VGA)\n"
|
2770 |
#endif
|
2771 |
"-loadvm file start right away with a saved state (loadvm in monitor)\n"
|
2772 |
"\n"
|
2773 |
"During emulation, the following keys are useful:\n"
|
2774 |
"ctrl-alt-f toggle full screen\n"
|
2775 |
"ctrl-alt-n switch to virtual console 'n'\n"
|
2776 |
"ctrl-alt toggle mouse and keyboard grab\n"
|
2777 |
"\n"
|
2778 |
"When using -nographic, press 'ctrl-a h' to get some help.\n"
|
2779 |
, |
2780 |
#ifdef CONFIG_SOFTMMU
|
2781 |
"qemu",
|
2782 |
#else
|
2783 |
"qemu-fast",
|
2784 |
#endif
|
2785 |
DEFAULT_RAM_SIZE, |
2786 |
DEFAULT_NETWORK_SCRIPT, |
2787 |
DEFAULT_GDBSTUB_PORT, |
2788 |
"/tmp/qemu.log");
|
2789 |
#ifndef CONFIG_SOFTMMU
|
2790 |
printf("\n"
|
2791 |
"NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
|
2792 |
"work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
|
2793 |
"PC emulation.\n");
|
2794 |
#endif
|
2795 |
exit(1);
|
2796 |
} |
2797 |
|
2798 |
#define HAS_ARG 0x0001 |
2799 |
|
2800 |
enum {
|
2801 |
QEMU_OPTION_h, |
2802 |
|
2803 |
QEMU_OPTION_fda, |
2804 |
QEMU_OPTION_fdb, |
2805 |
QEMU_OPTION_hda, |
2806 |
QEMU_OPTION_hdb, |
2807 |
QEMU_OPTION_hdc, |
2808 |
QEMU_OPTION_hdd, |
2809 |
QEMU_OPTION_cdrom, |
2810 |
QEMU_OPTION_boot, |
2811 |
QEMU_OPTION_snapshot, |
2812 |
QEMU_OPTION_m, |
2813 |
QEMU_OPTION_nographic, |
2814 |
QEMU_OPTION_enable_audio, |
2815 |
|
2816 |
QEMU_OPTION_nics, |
2817 |
QEMU_OPTION_macaddr, |
2818 |
QEMU_OPTION_n, |
2819 |
QEMU_OPTION_tun_fd, |
2820 |
QEMU_OPTION_user_net, |
2821 |
QEMU_OPTION_tftp, |
2822 |
QEMU_OPTION_smb, |
2823 |
QEMU_OPTION_redir, |
2824 |
QEMU_OPTION_dummy_net, |
2825 |
|
2826 |
QEMU_OPTION_kernel, |
2827 |
QEMU_OPTION_append, |
2828 |
QEMU_OPTION_initrd, |
2829 |
|
2830 |
QEMU_OPTION_S, |
2831 |
QEMU_OPTION_s, |
2832 |
QEMU_OPTION_p, |
2833 |
QEMU_OPTION_d, |
2834 |
QEMU_OPTION_hdachs, |
2835 |
QEMU_OPTION_L, |
2836 |
QEMU_OPTION_no_code_copy, |
2837 |
QEMU_OPTION_pci, |
2838 |
QEMU_OPTION_isa, |
2839 |
QEMU_OPTION_prep, |
2840 |
QEMU_OPTION_k, |
2841 |
QEMU_OPTION_localtime, |
2842 |
QEMU_OPTION_cirrusvga, |
2843 |
QEMU_OPTION_g, |
2844 |
QEMU_OPTION_std_vga, |
2845 |
QEMU_OPTION_monitor, |
2846 |
QEMU_OPTION_serial, |
2847 |
QEMU_OPTION_parallel, |
2848 |
QEMU_OPTION_loadvm, |
2849 |
QEMU_OPTION_full_screen, |
2850 |
QEMU_OPTION_pidfile, |
2851 |
}; |
2852 |
|
2853 |
typedef struct QEMUOption { |
2854 |
const char *name; |
2855 |
int flags;
|
2856 |
int index;
|
2857 |
} QEMUOption; |
2858 |
|
2859 |
const QEMUOption qemu_options[] = {
|
2860 |
{ "h", 0, QEMU_OPTION_h }, |
2861 |
|
2862 |
{ "fda", HAS_ARG, QEMU_OPTION_fda },
|
2863 |
{ "fdb", HAS_ARG, QEMU_OPTION_fdb },
|
2864 |
{ "hda", HAS_ARG, QEMU_OPTION_hda },
|
2865 |
{ "hdb", HAS_ARG, QEMU_OPTION_hdb },
|
2866 |
{ "hdc", HAS_ARG, QEMU_OPTION_hdc },
|
2867 |
{ "hdd", HAS_ARG, QEMU_OPTION_hdd },
|
2868 |
{ "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
|
2869 |
{ "boot", HAS_ARG, QEMU_OPTION_boot },
|
2870 |
{ "snapshot", 0, QEMU_OPTION_snapshot }, |
2871 |
{ "m", HAS_ARG, QEMU_OPTION_m },
|
2872 |
{ "nographic", 0, QEMU_OPTION_nographic }, |
2873 |
{ "k", HAS_ARG, QEMU_OPTION_k },
|
2874 |
{ "enable-audio", 0, QEMU_OPTION_enable_audio }, |
2875 |
|
2876 |
{ "nics", HAS_ARG, QEMU_OPTION_nics},
|
2877 |
{ "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
|
2878 |
{ "n", HAS_ARG, QEMU_OPTION_n },
|
2879 |
{ "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
|
2880 |
#ifdef CONFIG_SLIRP
|
2881 |
{ "user-net", 0, QEMU_OPTION_user_net }, |
2882 |
{ "tftp", HAS_ARG, QEMU_OPTION_tftp },
|
2883 |
#ifndef _WIN32
|
2884 |
{ "smb", HAS_ARG, QEMU_OPTION_smb },
|
2885 |
#endif
|
2886 |
{ "redir", HAS_ARG, QEMU_OPTION_redir },
|
2887 |
#endif
|
2888 |
{ "dummy-net", 0, QEMU_OPTION_dummy_net }, |
2889 |
|
2890 |
{ "kernel", HAS_ARG, QEMU_OPTION_kernel },
|
2891 |
{ "append", HAS_ARG, QEMU_OPTION_append },
|
2892 |
{ "initrd", HAS_ARG, QEMU_OPTION_initrd },
|
2893 |
|
2894 |
{ "S", 0, QEMU_OPTION_S }, |
2895 |
{ "s", 0, QEMU_OPTION_s }, |
2896 |
{ "p", HAS_ARG, QEMU_OPTION_p },
|
2897 |
{ "d", HAS_ARG, QEMU_OPTION_d },
|
2898 |
{ "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
|
2899 |
{ "L", HAS_ARG, QEMU_OPTION_L },
|
2900 |
{ "no-code-copy", 0, QEMU_OPTION_no_code_copy }, |
2901 |
#ifdef TARGET_PPC
|
2902 |
{ "prep", 0, QEMU_OPTION_prep }, |
2903 |
{ "g", 1, QEMU_OPTION_g }, |
2904 |
#endif
|
2905 |
{ "localtime", 0, QEMU_OPTION_localtime }, |
2906 |
{ "isa", 0, QEMU_OPTION_isa }, |
2907 |
{ "std-vga", 0, QEMU_OPTION_std_vga }, |
2908 |
{ "monitor", 1, QEMU_OPTION_monitor }, |
2909 |
{ "serial", 1, QEMU_OPTION_serial }, |
2910 |
{ "parallel", 1, QEMU_OPTION_parallel }, |
2911 |
{ "loadvm", HAS_ARG, QEMU_OPTION_loadvm },
|
2912 |
{ "full-screen", 0, QEMU_OPTION_full_screen }, |
2913 |
{ "pidfile", HAS_ARG, QEMU_OPTION_pidfile },
|
2914 |
|
2915 |
/* temporary options */
|
2916 |
{ "pci", 0, QEMU_OPTION_pci }, |
2917 |
{ "cirrusvga", 0, QEMU_OPTION_cirrusvga }, |
2918 |
{ NULL },
|
2919 |
}; |
2920 |
|
2921 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
2922 |
|
2923 |
/* this stack is only used during signal handling */
|
2924 |
#define SIGNAL_STACK_SIZE 32768 |
2925 |
|
2926 |
static uint8_t *signal_stack;
|
2927 |
|
2928 |
#endif
|
2929 |
|
2930 |
/* password input */
|
2931 |
|
2932 |
static BlockDriverState *get_bdrv(int index) |
2933 |
{ |
2934 |
BlockDriverState *bs; |
2935 |
|
2936 |
if (index < 4) { |
2937 |
bs = bs_table[index]; |
2938 |
} else if (index < 6) { |
2939 |
bs = fd_table[index - 4];
|
2940 |
} else {
|
2941 |
bs = NULL;
|
2942 |
} |
2943 |
return bs;
|
2944 |
} |
2945 |
|
2946 |
static void read_passwords(void) |
2947 |
{ |
2948 |
BlockDriverState *bs; |
2949 |
int i, j;
|
2950 |
char password[256]; |
2951 |
|
2952 |
for(i = 0; i < 6; i++) { |
2953 |
bs = get_bdrv(i); |
2954 |
if (bs && bdrv_is_encrypted(bs)) {
|
2955 |
term_printf("%s is encrypted.\n", bdrv_get_device_name(bs));
|
2956 |
for(j = 0; j < 3; j++) { |
2957 |
monitor_readline("Password: ",
|
2958 |
1, password, sizeof(password)); |
2959 |
if (bdrv_set_key(bs, password) == 0) |
2960 |
break;
|
2961 |
term_printf("invalid password\n");
|
2962 |
} |
2963 |
} |
2964 |
} |
2965 |
} |
2966 |
|
2967 |
#define NET_IF_TUN 0 |
2968 |
#define NET_IF_USER 1 |
2969 |
#define NET_IF_DUMMY 2 |
2970 |
|
2971 |
int main(int argc, char **argv) |
2972 |
{ |
2973 |
#ifdef CONFIG_GDBSTUB
|
2974 |
int use_gdbstub, gdbstub_port;
|
2975 |
#endif
|
2976 |
int i, has_cdrom;
|
2977 |
int snapshot, linux_boot;
|
2978 |
CPUState *env; |
2979 |
const char *initrd_filename; |
2980 |
const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
2981 |
const char *kernel_filename, *kernel_cmdline; |
2982 |
DisplayState *ds = &display_state; |
2983 |
int cyls, heads, secs, translation;
|
2984 |
int start_emulation = 1; |
2985 |
uint8_t macaddr[6];
|
2986 |
int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
|
2987 |
int optind;
|
2988 |
const char *r, *optarg; |
2989 |
CharDriverState *monitor_hd; |
2990 |
char monitor_device[128]; |
2991 |
char serial_devices[MAX_SERIAL_PORTS][128]; |
2992 |
int serial_device_index;
|
2993 |
char parallel_devices[MAX_PARALLEL_PORTS][128]; |
2994 |
int parallel_device_index;
|
2995 |
const char *loadvm = NULL; |
2996 |
|
2997 |
#if !defined(CONFIG_SOFTMMU)
|
2998 |
/* we never want that malloc() uses mmap() */
|
2999 |
mallopt(M_MMAP_THRESHOLD, 4096 * 1024); |
3000 |
#endif
|
3001 |
initrd_filename = NULL;
|
3002 |
for(i = 0; i < MAX_FD; i++) |
3003 |
fd_filename[i] = NULL;
|
3004 |
for(i = 0; i < MAX_DISKS; i++) |
3005 |
hd_filename[i] = NULL;
|
3006 |
ram_size = DEFAULT_RAM_SIZE * 1024 * 1024; |
3007 |
vga_ram_size = VGA_RAM_SIZE; |
3008 |
bios_size = BIOS_SIZE; |
3009 |
pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
|
3010 |
#ifdef CONFIG_GDBSTUB
|
3011 |
use_gdbstub = 0;
|
3012 |
gdbstub_port = DEFAULT_GDBSTUB_PORT; |
3013 |
#endif
|
3014 |
snapshot = 0;
|
3015 |
nographic = 0;
|
3016 |
kernel_filename = NULL;
|
3017 |
kernel_cmdline = "";
|
3018 |
has_cdrom = 1;
|
3019 |
cyls = heads = secs = 0;
|
3020 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
3021 |
pstrcpy(monitor_device, sizeof(monitor_device), "vc"); |
3022 |
|
3023 |
pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "vc"); |
3024 |
for(i = 1; i < MAX_SERIAL_PORTS; i++) |
3025 |
serial_devices[i][0] = '\0'; |
3026 |
serial_device_index = 0;
|
3027 |
|
3028 |
pstrcpy(parallel_devices[0], sizeof(parallel_devices[0]), "vc"); |
3029 |
for(i = 1; i < MAX_PARALLEL_PORTS; i++) |
3030 |
parallel_devices[i][0] = '\0'; |
3031 |
parallel_device_index = 0;
|
3032 |
|
3033 |
nb_tun_fds = 0;
|
3034 |
net_if_type = -1;
|
3035 |
nb_nics = 1;
|
3036 |
/* default mac address of the first network interface */
|
3037 |
macaddr[0] = 0x52; |
3038 |
macaddr[1] = 0x54; |
3039 |
macaddr[2] = 0x00; |
3040 |
macaddr[3] = 0x12; |
3041 |
macaddr[4] = 0x34; |
3042 |
macaddr[5] = 0x56; |
3043 |
|
3044 |
optind = 1;
|
3045 |
for(;;) {
|
3046 |
if (optind >= argc)
|
3047 |
break;
|
3048 |
r = argv[optind]; |
3049 |
if (r[0] != '-') { |
3050 |
hd_filename[0] = argv[optind++];
|
3051 |
} else {
|
3052 |
const QEMUOption *popt;
|
3053 |
|
3054 |
optind++; |
3055 |
popt = qemu_options; |
3056 |
for(;;) {
|
3057 |
if (!popt->name) {
|
3058 |
fprintf(stderr, "%s: invalid option -- '%s'\n",
|
3059 |
argv[0], r);
|
3060 |
exit(1);
|
3061 |
} |
3062 |
if (!strcmp(popt->name, r + 1)) |
3063 |
break;
|
3064 |
popt++; |
3065 |
} |
3066 |
if (popt->flags & HAS_ARG) {
|
3067 |
if (optind >= argc) {
|
3068 |
fprintf(stderr, "%s: option '%s' requires an argument\n",
|
3069 |
argv[0], r);
|
3070 |
exit(1);
|
3071 |
} |
3072 |
optarg = argv[optind++]; |
3073 |
} else {
|
3074 |
optarg = NULL;
|
3075 |
} |
3076 |
|
3077 |
switch(popt->index) {
|
3078 |
case QEMU_OPTION_initrd:
|
3079 |
initrd_filename = optarg; |
3080 |
break;
|
3081 |
case QEMU_OPTION_hda:
|
3082 |
hd_filename[0] = optarg;
|
3083 |
break;
|
3084 |
case QEMU_OPTION_hdb:
|
3085 |
hd_filename[1] = optarg;
|
3086 |
break;
|
3087 |
case QEMU_OPTION_snapshot:
|
3088 |
snapshot = 1;
|
3089 |
break;
|
3090 |
case QEMU_OPTION_hdachs:
|
3091 |
{ |
3092 |
const char *p; |
3093 |
p = optarg; |
3094 |
cyls = strtol(p, (char **)&p, 0); |
3095 |
if (cyls < 1 || cyls > 16383) |
3096 |
goto chs_fail;
|
3097 |
if (*p != ',') |
3098 |
goto chs_fail;
|
3099 |
p++; |
3100 |
heads = strtol(p, (char **)&p, 0); |
3101 |
if (heads < 1 || heads > 16) |
3102 |
goto chs_fail;
|
3103 |
if (*p != ',') |
3104 |
goto chs_fail;
|
3105 |
p++; |
3106 |
secs = strtol(p, (char **)&p, 0); |
3107 |
if (secs < 1 || secs > 63) |
3108 |
goto chs_fail;
|
3109 |
if (*p == ',') { |
3110 |
p++; |
3111 |
if (!strcmp(p, "none")) |
3112 |
translation = BIOS_ATA_TRANSLATION_NONE; |
3113 |
else if (!strcmp(p, "lba")) |
3114 |
translation = BIOS_ATA_TRANSLATION_LBA; |
3115 |
else if (!strcmp(p, "auto")) |
3116 |
translation = BIOS_ATA_TRANSLATION_AUTO; |
3117 |
else
|
3118 |
goto chs_fail;
|
3119 |
} else if (*p != '\0') { |
3120 |
chs_fail:
|
3121 |
fprintf(stderr, "qemu: invalid physical CHS format\n");
|
3122 |
exit(1);
|
3123 |
} |
3124 |
} |
3125 |
break;
|
3126 |
case QEMU_OPTION_nographic:
|
3127 |
pstrcpy(monitor_device, sizeof(monitor_device), "stdio"); |
3128 |
pstrcpy(serial_devices[0], sizeof(serial_devices[0]), "stdio"); |
3129 |
nographic = 1;
|
3130 |
break;
|
3131 |
case QEMU_OPTION_kernel:
|
3132 |
kernel_filename = optarg; |
3133 |
break;
|
3134 |
case QEMU_OPTION_append:
|
3135 |
kernel_cmdline = optarg; |
3136 |
break;
|
3137 |
case QEMU_OPTION_tun_fd:
|
3138 |
{ |
3139 |
const char *p; |
3140 |
int fd;
|
3141 |
net_if_type = NET_IF_TUN; |
3142 |
if (nb_tun_fds < MAX_NICS) {
|
3143 |
fd = strtol(optarg, (char **)&p, 0); |
3144 |
if (*p != '\0') { |
3145 |
fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
|
3146 |
exit(1);
|
3147 |
} |
3148 |
tun_fds[nb_tun_fds++] = fd; |
3149 |
} |
3150 |
} |
3151 |
break;
|
3152 |
case QEMU_OPTION_hdc:
|
3153 |
hd_filename[2] = optarg;
|
3154 |
has_cdrom = 0;
|
3155 |
break;
|
3156 |
case QEMU_OPTION_hdd:
|
3157 |
hd_filename[3] = optarg;
|
3158 |
break;
|
3159 |
case QEMU_OPTION_cdrom:
|
3160 |
hd_filename[2] = optarg;
|
3161 |
has_cdrom = 1;
|
3162 |
break;
|
3163 |
case QEMU_OPTION_boot:
|
3164 |
boot_device = optarg[0];
|
3165 |
if (boot_device != 'a' && |
3166 |
boot_device != 'c' && boot_device != 'd') { |
3167 |
fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
|
3168 |
exit(1);
|
3169 |
} |
3170 |
break;
|
3171 |
case QEMU_OPTION_fda:
|
3172 |
fd_filename[0] = optarg;
|
3173 |
break;
|
3174 |
case QEMU_OPTION_fdb:
|
3175 |
fd_filename[1] = optarg;
|
3176 |
break;
|
3177 |
case QEMU_OPTION_no_code_copy:
|
3178 |
code_copy_enabled = 0;
|
3179 |
break;
|
3180 |
case QEMU_OPTION_nics:
|
3181 |
nb_nics = atoi(optarg); |
3182 |
if (nb_nics < 0 || nb_nics > MAX_NICS) { |
3183 |
fprintf(stderr, "qemu: invalid number of network interfaces\n");
|
3184 |
exit(1);
|
3185 |
} |
3186 |
break;
|
3187 |
case QEMU_OPTION_macaddr:
|
3188 |
{ |
3189 |
const char *p; |
3190 |
int i;
|
3191 |
p = optarg; |
3192 |
for(i = 0; i < 6; i++) { |
3193 |
macaddr[i] = strtol(p, (char **)&p, 16); |
3194 |
if (i == 5) { |
3195 |
if (*p != '\0') |
3196 |
goto macaddr_error;
|
3197 |
} else {
|
3198 |
if (*p != ':') { |
3199 |
macaddr_error:
|
3200 |
fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
|
3201 |
exit(1);
|
3202 |
} |
3203 |
p++; |
3204 |
} |
3205 |
} |
3206 |
} |
3207 |
break;
|
3208 |
#ifdef CONFIG_SLIRP
|
3209 |
case QEMU_OPTION_tftp:
|
3210 |
tftp_prefix = optarg; |
3211 |
break;
|
3212 |
#ifndef _WIN32
|
3213 |
case QEMU_OPTION_smb:
|
3214 |
net_slirp_smb(optarg); |
3215 |
break;
|
3216 |
#endif
|
3217 |
case QEMU_OPTION_user_net:
|
3218 |
net_if_type = NET_IF_USER; |
3219 |
break;
|
3220 |
case QEMU_OPTION_redir:
|
3221 |
net_slirp_redir(optarg); |
3222 |
break;
|
3223 |
#endif
|
3224 |
case QEMU_OPTION_dummy_net:
|
3225 |
net_if_type = NET_IF_DUMMY; |
3226 |
break;
|
3227 |
case QEMU_OPTION_enable_audio:
|
3228 |
audio_enabled = 1;
|
3229 |
break;
|
3230 |
case QEMU_OPTION_h:
|
3231 |
help(); |
3232 |
break;
|
3233 |
case QEMU_OPTION_m:
|
3234 |
ram_size = atoi(optarg) * 1024 * 1024; |
3235 |
if (ram_size <= 0) |
3236 |
help(); |
3237 |
if (ram_size > PHYS_RAM_MAX_SIZE) {
|
3238 |
fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
|
3239 |
PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
3240 |
exit(1);
|
3241 |
} |
3242 |
break;
|
3243 |
case QEMU_OPTION_d:
|
3244 |
{ |
3245 |
int mask;
|
3246 |
CPULogItem *item; |
3247 |
|
3248 |
mask = cpu_str_to_log_mask(optarg); |
3249 |
if (!mask) {
|
3250 |
printf("Log items (comma separated):\n");
|
3251 |
for(item = cpu_log_items; item->mask != 0; item++) { |
3252 |
printf("%-10s %s\n", item->name, item->help);
|
3253 |
} |
3254 |
exit(1);
|
3255 |
} |
3256 |
cpu_set_log(mask); |
3257 |
} |
3258 |
break;
|
3259 |
case QEMU_OPTION_n:
|
3260 |
pstrcpy(network_script, sizeof(network_script), optarg);
|
3261 |
break;
|
3262 |
#ifdef CONFIG_GDBSTUB
|
3263 |
case QEMU_OPTION_s:
|
3264 |
use_gdbstub = 1;
|
3265 |
break;
|
3266 |
case QEMU_OPTION_p:
|
3267 |
gdbstub_port = atoi(optarg); |
3268 |
break;
|
3269 |
#endif
|
3270 |
case QEMU_OPTION_L:
|
3271 |
bios_dir = optarg; |
3272 |
break;
|
3273 |
case QEMU_OPTION_S:
|
3274 |
start_emulation = 0;
|
3275 |
break;
|
3276 |
case QEMU_OPTION_pci:
|
3277 |
pci_enabled = 1;
|
3278 |
break;
|
3279 |
case QEMU_OPTION_isa:
|
3280 |
pci_enabled = 0;
|
3281 |
break;
|
3282 |
case QEMU_OPTION_prep:
|
3283 |
prep_enabled = 1;
|
3284 |
break;
|
3285 |
case QEMU_OPTION_k:
|
3286 |
keyboard_layout = optarg; |
3287 |
break;
|
3288 |
case QEMU_OPTION_localtime:
|
3289 |
rtc_utc = 0;
|
3290 |
break;
|
3291 |
case QEMU_OPTION_cirrusvga:
|
3292 |
cirrus_vga_enabled = 1;
|
3293 |
break;
|
3294 |
case QEMU_OPTION_std_vga:
|
3295 |
cirrus_vga_enabled = 0;
|
3296 |
break;
|
3297 |
case QEMU_OPTION_g:
|
3298 |
{ |
3299 |
const char *p; |
3300 |
int w, h, depth;
|
3301 |
p = optarg; |
3302 |
w = strtol(p, (char **)&p, 10); |
3303 |
if (w <= 0) { |
3304 |
graphic_error:
|
3305 |
fprintf(stderr, "qemu: invalid resolution or depth\n");
|
3306 |
exit(1);
|
3307 |
} |
3308 |
if (*p != 'x') |
3309 |
goto graphic_error;
|
3310 |
p++; |
3311 |
h = strtol(p, (char **)&p, 10); |
3312 |
if (h <= 0) |
3313 |
goto graphic_error;
|
3314 |
if (*p == 'x') { |
3315 |
p++; |
3316 |
depth = strtol(p, (char **)&p, 10); |
3317 |
if (depth != 8 && depth != 15 && depth != 16 && |
3318 |
depth != 24 && depth != 32) |
3319 |
goto graphic_error;
|
3320 |
} else if (*p == '\0') { |
3321 |
depth = graphic_depth; |
3322 |
} else {
|
3323 |
goto graphic_error;
|
3324 |
} |
3325 |
|
3326 |
graphic_width = w; |
3327 |
graphic_height = h; |
3328 |
graphic_depth = depth; |
3329 |
} |
3330 |
break;
|
3331 |
case QEMU_OPTION_monitor:
|
3332 |
pstrcpy(monitor_device, sizeof(monitor_device), optarg);
|
3333 |
break;
|
3334 |
case QEMU_OPTION_serial:
|
3335 |
if (serial_device_index >= MAX_SERIAL_PORTS) {
|
3336 |
fprintf(stderr, "qemu: too many serial ports\n");
|
3337 |
exit(1);
|
3338 |
} |
3339 |
pstrcpy(serial_devices[serial_device_index], |
3340 |
sizeof(serial_devices[0]), optarg); |
3341 |
serial_device_index++; |
3342 |
break;
|
3343 |
case QEMU_OPTION_parallel:
|
3344 |
if (parallel_device_index >= MAX_PARALLEL_PORTS) {
|
3345 |
fprintf(stderr, "qemu: too many parallel ports\n");
|
3346 |
exit(1);
|
3347 |
} |
3348 |
pstrcpy(parallel_devices[parallel_device_index], |
3349 |
sizeof(parallel_devices[0]), optarg); |
3350 |
parallel_device_index++; |
3351 |
break;
|
3352 |
case QEMU_OPTION_loadvm:
|
3353 |
loadvm = optarg; |
3354 |
break;
|
3355 |
case QEMU_OPTION_full_screen:
|
3356 |
full_screen = 1;
|
3357 |
break;
|
3358 |
case QEMU_OPTION_pidfile:
|
3359 |
create_pidfile(optarg); |
3360 |
break;
|
3361 |
} |
3362 |
} |
3363 |
} |
3364 |
|
3365 |
linux_boot = (kernel_filename != NULL);
|
3366 |
|
3367 |
if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' && |
3368 |
fd_filename[0] == '\0') |
3369 |
help(); |
3370 |
|
3371 |
/* boot to cd by default if no hard disk */
|
3372 |
if (hd_filename[0] == '\0' && boot_device == 'c') { |
3373 |
if (fd_filename[0] != '\0') |
3374 |
boot_device = 'a';
|
3375 |
else
|
3376 |
boot_device = 'd';
|
3377 |
} |
3378 |
|
3379 |
#if !defined(CONFIG_SOFTMMU)
|
3380 |
/* must avoid mmap() usage of glibc by setting a buffer "by hand" */
|
3381 |
{ |
3382 |
static uint8_t stdout_buf[4096]; |
3383 |
setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
|
3384 |
} |
3385 |
#else
|
3386 |
setvbuf(stdout, NULL, _IOLBF, 0); |
3387 |
#endif
|
3388 |
|
3389 |
/* init host network redirectors */
|
3390 |
if (net_if_type == -1) { |
3391 |
net_if_type = NET_IF_TUN; |
3392 |
#if defined(CONFIG_SLIRP)
|
3393 |
if (access(network_script, R_OK) < 0) { |
3394 |
net_if_type = NET_IF_USER; |
3395 |
} |
3396 |
#endif
|
3397 |
} |
3398 |
|
3399 |
for(i = 0; i < nb_nics; i++) { |
3400 |
NetDriverState *nd = &nd_table[i]; |
3401 |
nd->index = i; |
3402 |
/* init virtual mac address */
|
3403 |
nd->macaddr[0] = macaddr[0]; |
3404 |
nd->macaddr[1] = macaddr[1]; |
3405 |
nd->macaddr[2] = macaddr[2]; |
3406 |
nd->macaddr[3] = macaddr[3]; |
3407 |
nd->macaddr[4] = macaddr[4]; |
3408 |
nd->macaddr[5] = macaddr[5] + i; |
3409 |
switch(net_if_type) {
|
3410 |
#if defined(CONFIG_SLIRP)
|
3411 |
case NET_IF_USER:
|
3412 |
net_slirp_init(nd); |
3413 |
break;
|
3414 |
#endif
|
3415 |
#if !defined(_WIN32)
|
3416 |
case NET_IF_TUN:
|
3417 |
if (i < nb_tun_fds) {
|
3418 |
net_fd_init(nd, tun_fds[i]); |
3419 |
} else {
|
3420 |
if (net_tun_init(nd) < 0) |
3421 |
net_dummy_init(nd); |
3422 |
} |
3423 |
break;
|
3424 |
#endif
|
3425 |
case NET_IF_DUMMY:
|
3426 |
default:
|
3427 |
net_dummy_init(nd); |
3428 |
break;
|
3429 |
} |
3430 |
} |
3431 |
|
3432 |
/* init the memory */
|
3433 |
phys_ram_size = ram_size + vga_ram_size + bios_size; |
3434 |
|
3435 |
#ifdef CONFIG_SOFTMMU
|
3436 |
#ifdef _BSD
|
3437 |
/* mallocs are always aligned on BSD. valloc is better for correctness */
|
3438 |
phys_ram_base = valloc(phys_ram_size); |
3439 |
#else
|
3440 |
phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size); |
3441 |
#endif
|
3442 |
if (!phys_ram_base) {
|
3443 |
fprintf(stderr, "Could not allocate physical memory\n");
|
3444 |
exit(1);
|
3445 |
} |
3446 |
#else
|
3447 |
/* as we must map the same page at several addresses, we must use
|
3448 |
a fd */
|
3449 |
{ |
3450 |
const char *tmpdir; |
3451 |
|
3452 |
tmpdir = getenv("QEMU_TMPDIR");
|
3453 |
if (!tmpdir)
|
3454 |
tmpdir = "/tmp";
|
3455 |
snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); |
3456 |
if (mkstemp(phys_ram_file) < 0) { |
3457 |
fprintf(stderr, "Could not create temporary memory file '%s'\n",
|
3458 |
phys_ram_file); |
3459 |
exit(1);
|
3460 |
} |
3461 |
phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
|
3462 |
if (phys_ram_fd < 0) { |
3463 |
fprintf(stderr, "Could not open temporary memory file '%s'\n",
|
3464 |
phys_ram_file); |
3465 |
exit(1);
|
3466 |
} |
3467 |
ftruncate(phys_ram_fd, phys_ram_size); |
3468 |
unlink(phys_ram_file); |
3469 |
phys_ram_base = mmap(get_mmap_addr(phys_ram_size), |
3470 |
phys_ram_size, |
3471 |
PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, |
3472 |
phys_ram_fd, 0);
|
3473 |
if (phys_ram_base == MAP_FAILED) {
|
3474 |
fprintf(stderr, "Could not map physical memory\n");
|
3475 |
exit(1);
|
3476 |
} |
3477 |
} |
3478 |
#endif
|
3479 |
|
3480 |
/* we always create the cdrom drive, even if no disk is there */
|
3481 |
bdrv_init(); |
3482 |
if (has_cdrom) {
|
3483 |
bs_table[2] = bdrv_new("cdrom"); |
3484 |
bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
|
3485 |
} |
3486 |
|
3487 |
/* open the virtual block devices */
|
3488 |
for(i = 0; i < MAX_DISKS; i++) { |
3489 |
if (hd_filename[i]) {
|
3490 |
if (!bs_table[i]) {
|
3491 |
char buf[64]; |
3492 |
snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
3493 |
bs_table[i] = bdrv_new(buf); |
3494 |
} |
3495 |
if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) { |
3496 |
fprintf(stderr, "qemu: could not open hard disk image '%s'\n",
|
3497 |
hd_filename[i]); |
3498 |
exit(1);
|
3499 |
} |
3500 |
if (i == 0 && cyls != 0) { |
3501 |
bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
3502 |
bdrv_set_translation_hint(bs_table[i], translation); |
3503 |
} |
3504 |
} |
3505 |
} |
3506 |
|
3507 |
/* we always create at least one floppy disk */
|
3508 |
fd_table[0] = bdrv_new("fda"); |
3509 |
bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
|
3510 |
|
3511 |
for(i = 0; i < MAX_FD; i++) { |
3512 |
if (fd_filename[i]) {
|
3513 |
if (!fd_table[i]) {
|
3514 |
char buf[64]; |
3515 |
snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
3516 |
fd_table[i] = bdrv_new(buf); |
3517 |
bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
3518 |
} |
3519 |
if (fd_filename[i] != '\0') { |
3520 |
if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) { |
3521 |
fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
|
3522 |
fd_filename[i]); |
3523 |
exit(1);
|
3524 |
} |
3525 |
} |
3526 |
} |
3527 |
} |
3528 |
|
3529 |
/* init CPU state */
|
3530 |
env = cpu_init(); |
3531 |
global_env = env; |
3532 |
cpu_single_env = env; |
3533 |
|
3534 |
register_savevm("timer", 0, 1, timer_save, timer_load, env); |
3535 |
register_savevm("cpu", 0, 3, cpu_save, cpu_load, env); |
3536 |
register_savevm("ram", 0, 1, ram_save, ram_load, NULL); |
3537 |
qemu_register_reset(main_cpu_reset, global_env); |
3538 |
|
3539 |
init_ioports(); |
3540 |
cpu_calibrate_ticks(); |
3541 |
|
3542 |
/* terminal init */
|
3543 |
if (nographic) {
|
3544 |
dumb_display_init(ds); |
3545 |
} else {
|
3546 |
#ifdef CONFIG_SDL
|
3547 |
sdl_display_init(ds, full_screen); |
3548 |
#else
|
3549 |
dumb_display_init(ds); |
3550 |
#endif
|
3551 |
} |
3552 |
|
3553 |
vga_console = graphic_console_init(ds); |
3554 |
|
3555 |
monitor_hd = qemu_chr_open(monitor_device); |
3556 |
if (!monitor_hd) {
|
3557 |
fprintf(stderr, "qemu: could not open monitor device '%s'\n", monitor_device);
|
3558 |
exit(1);
|
3559 |
} |
3560 |
monitor_init(monitor_hd, !nographic); |
3561 |
|
3562 |
for(i = 0; i < MAX_SERIAL_PORTS; i++) { |
3563 |
if (serial_devices[i][0] != '\0') { |
3564 |
serial_hds[i] = qemu_chr_open(serial_devices[i]); |
3565 |
if (!serial_hds[i]) {
|
3566 |
fprintf(stderr, "qemu: could not open serial device '%s'\n",
|
3567 |
serial_devices[i]); |
3568 |
exit(1);
|
3569 |
} |
3570 |
if (!strcmp(serial_devices[i], "vc")) |
3571 |
qemu_chr_printf(serial_hds[i], "serial%d console\n", i);
|
3572 |
} |
3573 |
} |
3574 |
|
3575 |
for(i = 0; i < MAX_PARALLEL_PORTS; i++) { |
3576 |
if (parallel_devices[i][0] != '\0') { |
3577 |
parallel_hds[i] = qemu_chr_open(parallel_devices[i]); |
3578 |
if (!parallel_hds[i]) {
|
3579 |
fprintf(stderr, "qemu: could not open parallel device '%s'\n",
|
3580 |
parallel_devices[i]); |
3581 |
exit(1);
|
3582 |
} |
3583 |
if (!strcmp(parallel_devices[i], "vc")) |
3584 |
qemu_chr_printf(parallel_hds[i], "parallel%d console\n", i);
|
3585 |
} |
3586 |
} |
3587 |
|
3588 |
/* setup cpu signal handlers for MMU / self modifying code handling */
|
3589 |
#if !defined(CONFIG_SOFTMMU)
|
3590 |
|
3591 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
3592 |
{ |
3593 |
stack_t stk; |
3594 |
signal_stack = memalign(16, SIGNAL_STACK_SIZE);
|
3595 |
stk.ss_sp = signal_stack; |
3596 |
stk.ss_size = SIGNAL_STACK_SIZE; |
3597 |
stk.ss_flags = 0;
|
3598 |
|
3599 |
if (sigaltstack(&stk, NULL) < 0) { |
3600 |
perror("sigaltstack");
|
3601 |
exit(1);
|
3602 |
} |
3603 |
} |
3604 |
#endif
|
3605 |
{ |
3606 |
struct sigaction act;
|
3607 |
|
3608 |
sigfillset(&act.sa_mask); |
3609 |
act.sa_flags = SA_SIGINFO; |
3610 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
3611 |
act.sa_flags |= SA_ONSTACK; |
3612 |
#endif
|
3613 |
act.sa_sigaction = host_segv_handler; |
3614 |
sigaction(SIGSEGV, &act, NULL);
|
3615 |
sigaction(SIGBUS, &act, NULL);
|
3616 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
3617 |
sigaction(SIGFPE, &act, NULL);
|
3618 |
#endif
|
3619 |
} |
3620 |
#endif
|
3621 |
|
3622 |
#ifndef _WIN32
|
3623 |
{ |
3624 |
struct sigaction act;
|
3625 |
sigfillset(&act.sa_mask); |
3626 |
act.sa_flags = 0;
|
3627 |
act.sa_handler = SIG_IGN; |
3628 |
sigaction(SIGPIPE, &act, NULL);
|
3629 |
} |
3630 |
#endif
|
3631 |
init_timers(); |
3632 |
|
3633 |
#if defined(TARGET_I386)
|
3634 |
pc_init(ram_size, vga_ram_size, boot_device, |
3635 |
ds, fd_filename, snapshot, |
3636 |
kernel_filename, kernel_cmdline, initrd_filename); |
3637 |
#elif defined(TARGET_PPC)
|
3638 |
ppc_init(ram_size, vga_ram_size, boot_device, |
3639 |
ds, fd_filename, snapshot, |
3640 |
kernel_filename, kernel_cmdline, initrd_filename); |
3641 |
#elif defined(TARGET_SPARC)
|
3642 |
sun4m_init(ram_size, vga_ram_size, boot_device, |
3643 |
ds, fd_filename, snapshot, |
3644 |
kernel_filename, kernel_cmdline, initrd_filename); |
3645 |
#endif
|
3646 |
|
3647 |
gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
|
3648 |
qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
3649 |
|
3650 |
#ifdef CONFIG_GDBSTUB
|
3651 |
if (use_gdbstub) {
|
3652 |
if (gdbserver_start(gdbstub_port) < 0) { |
3653 |
fprintf(stderr, "Could not open gdbserver socket on port %d\n",
|
3654 |
gdbstub_port); |
3655 |
exit(1);
|
3656 |
} else {
|
3657 |
printf("Waiting gdb connection on port %d\n", gdbstub_port);
|
3658 |
} |
3659 |
} else
|
3660 |
#endif
|
3661 |
if (loadvm)
|
3662 |
qemu_loadvm(loadvm); |
3663 |
|
3664 |
{ |
3665 |
/* XXX: simplify init */
|
3666 |
read_passwords(); |
3667 |
if (start_emulation) {
|
3668 |
vm_start(); |
3669 |
} |
3670 |
} |
3671 |
main_loop(); |
3672 |
quit_timers(); |
3673 |
return 0; |
3674 |
} |