root / vl.c @ d64477af
History | View | Annotate | Download (58.6 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 <getopt.h> |
27 |
#include <unistd.h> |
28 |
#include <fcntl.h> |
29 |
#include <signal.h> |
30 |
#include <time.h> |
31 |
#include <malloc.h> |
32 |
#include <errno.h> |
33 |
#include <sys/time.h> |
34 |
|
35 |
#ifndef _WIN32
|
36 |
#include <sys/times.h> |
37 |
#include <sys/wait.h> |
38 |
#include <pty.h> |
39 |
#include <termios.h> |
40 |
#include <sys/poll.h> |
41 |
#include <sys/mman.h> |
42 |
#include <sys/ioctl.h> |
43 |
#include <sys/socket.h> |
44 |
#include <linux/if.h> |
45 |
#include <linux/if_tun.h> |
46 |
#endif
|
47 |
|
48 |
#if defined(CONFIG_SLIRP)
|
49 |
#include "libslirp.h" |
50 |
#endif
|
51 |
|
52 |
#ifdef _WIN32
|
53 |
#include <sys/timeb.h> |
54 |
#include <windows.h> |
55 |
#define getopt_long_only getopt_long
|
56 |
#define memalign(align, size) malloc(size)
|
57 |
#endif
|
58 |
|
59 |
#ifdef CONFIG_SDL
|
60 |
/* SDL use the pthreads and they modify sigaction. We don't
|
61 |
want that. */
|
62 |
#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2) |
63 |
extern void __libc_sigaction(); |
64 |
#define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
|
65 |
#else
|
66 |
extern void __sigaction(); |
67 |
#define sigaction(sig, act, oact) __sigaction(sig, act, oact)
|
68 |
#endif
|
69 |
#endif /* CONFIG_SDL */ |
70 |
|
71 |
#include "disas.h" |
72 |
|
73 |
#include "exec-all.h" |
74 |
|
75 |
//#define DO_TB_FLUSH
|
76 |
|
77 |
#define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup" |
78 |
|
79 |
//#define DEBUG_UNUSED_IOPORT
|
80 |
|
81 |
#if !defined(CONFIG_SOFTMMU)
|
82 |
#define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024) |
83 |
#else
|
84 |
#define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024) |
85 |
#endif
|
86 |
|
87 |
/* in ms */
|
88 |
#define GUI_REFRESH_INTERVAL 30 |
89 |
|
90 |
/* XXX: use a two level table to limit memory usage */
|
91 |
#define MAX_IOPORTS 65536 |
92 |
|
93 |
const char *bios_dir = CONFIG_QEMU_SHAREDIR; |
94 |
char phys_ram_file[1024]; |
95 |
CPUState *global_env; |
96 |
CPUState *cpu_single_env; |
97 |
void *ioport_opaque[MAX_IOPORTS];
|
98 |
IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
|
99 |
IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
|
100 |
BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD]; |
101 |
int vga_ram_size;
|
102 |
static DisplayState display_state;
|
103 |
int nographic;
|
104 |
int64_t ticks_per_sec; |
105 |
int boot_device = 'c'; |
106 |
static int ram_size; |
107 |
static char network_script[1024]; |
108 |
int pit_min_timer_count = 0; |
109 |
int nb_nics;
|
110 |
NetDriverState nd_table[MAX_NICS]; |
111 |
SerialState *serial_console; |
112 |
QEMUTimer *gui_timer; |
113 |
int vm_running;
|
114 |
|
115 |
/***********************************************************/
|
116 |
/* x86 io ports */
|
117 |
|
118 |
uint32_t default_ioport_readb(void *opaque, uint32_t address)
|
119 |
{ |
120 |
#ifdef DEBUG_UNUSED_IOPORT
|
121 |
fprintf(stderr, "inb: port=0x%04x\n", address);
|
122 |
#endif
|
123 |
return 0xff; |
124 |
} |
125 |
|
126 |
void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data) |
127 |
{ |
128 |
#ifdef DEBUG_UNUSED_IOPORT
|
129 |
fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
|
130 |
#endif
|
131 |
} |
132 |
|
133 |
/* default is to make two byte accesses */
|
134 |
uint32_t default_ioport_readw(void *opaque, uint32_t address)
|
135 |
{ |
136 |
uint32_t data; |
137 |
data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address); |
138 |
data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8; |
139 |
return data;
|
140 |
} |
141 |
|
142 |
void default_ioport_writew(void *opaque, uint32_t address, uint32_t data) |
143 |
{ |
144 |
ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff); |
145 |
ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff); |
146 |
} |
147 |
|
148 |
uint32_t default_ioport_readl(void *opaque, uint32_t address)
|
149 |
{ |
150 |
#ifdef DEBUG_UNUSED_IOPORT
|
151 |
fprintf(stderr, "inl: port=0x%04x\n", address);
|
152 |
#endif
|
153 |
return 0xffffffff; |
154 |
} |
155 |
|
156 |
void default_ioport_writel(void *opaque, uint32_t address, uint32_t data) |
157 |
{ |
158 |
#ifdef DEBUG_UNUSED_IOPORT
|
159 |
fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
|
160 |
#endif
|
161 |
} |
162 |
|
163 |
void init_ioports(void) |
164 |
{ |
165 |
int i;
|
166 |
|
167 |
for(i = 0; i < MAX_IOPORTS; i++) { |
168 |
ioport_read_table[0][i] = default_ioport_readb;
|
169 |
ioport_write_table[0][i] = default_ioport_writeb;
|
170 |
ioport_read_table[1][i] = default_ioport_readw;
|
171 |
ioport_write_table[1][i] = default_ioport_writew;
|
172 |
ioport_read_table[2][i] = default_ioport_readl;
|
173 |
ioport_write_table[2][i] = default_ioport_writel;
|
174 |
} |
175 |
} |
176 |
|
177 |
/* size is the word size in byte */
|
178 |
int register_ioport_read(int start, int length, int size, |
179 |
IOPortReadFunc *func, void *opaque)
|
180 |
{ |
181 |
int i, bsize;
|
182 |
|
183 |
if (size == 1) { |
184 |
bsize = 0;
|
185 |
} else if (size == 2) { |
186 |
bsize = 1;
|
187 |
} else if (size == 4) { |
188 |
bsize = 2;
|
189 |
} else {
|
190 |
hw_error("register_ioport_read: invalid size");
|
191 |
return -1; |
192 |
} |
193 |
for(i = start; i < start + length; i += size) {
|
194 |
ioport_read_table[bsize][i] = func; |
195 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
196 |
hw_error("register_ioport_read: invalid opaque");
|
197 |
ioport_opaque[i] = opaque; |
198 |
} |
199 |
return 0; |
200 |
} |
201 |
|
202 |
/* size is the word size in byte */
|
203 |
int register_ioport_write(int start, int length, int size, |
204 |
IOPortWriteFunc *func, void *opaque)
|
205 |
{ |
206 |
int i, bsize;
|
207 |
|
208 |
if (size == 1) { |
209 |
bsize = 0;
|
210 |
} else if (size == 2) { |
211 |
bsize = 1;
|
212 |
} else if (size == 4) { |
213 |
bsize = 2;
|
214 |
} else {
|
215 |
hw_error("register_ioport_write: invalid size");
|
216 |
return -1; |
217 |
} |
218 |
for(i = start; i < start + length; i += size) {
|
219 |
ioport_write_table[bsize][i] = func; |
220 |
if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque) |
221 |
hw_error("register_ioport_read: invalid opaque");
|
222 |
ioport_opaque[i] = opaque; |
223 |
} |
224 |
return 0; |
225 |
} |
226 |
|
227 |
void pstrcpy(char *buf, int buf_size, const char *str) |
228 |
{ |
229 |
int c;
|
230 |
char *q = buf;
|
231 |
|
232 |
if (buf_size <= 0) |
233 |
return;
|
234 |
|
235 |
for(;;) {
|
236 |
c = *str++; |
237 |
if (c == 0 || q >= buf + buf_size - 1) |
238 |
break;
|
239 |
*q++ = c; |
240 |
} |
241 |
*q = '\0';
|
242 |
} |
243 |
|
244 |
/* strcat and truncate. */
|
245 |
char *pstrcat(char *buf, int buf_size, const char *s) |
246 |
{ |
247 |
int len;
|
248 |
len = strlen(buf); |
249 |
if (len < buf_size)
|
250 |
pstrcpy(buf + len, buf_size - len, s); |
251 |
return buf;
|
252 |
} |
253 |
|
254 |
/* return the size or -1 if error */
|
255 |
int load_image(const char *filename, uint8_t *addr) |
256 |
{ |
257 |
int fd, size;
|
258 |
fd = open(filename, O_RDONLY | O_BINARY); |
259 |
if (fd < 0) |
260 |
return -1; |
261 |
size = lseek(fd, 0, SEEK_END);
|
262 |
lseek(fd, 0, SEEK_SET);
|
263 |
if (read(fd, addr, size) != size) {
|
264 |
close(fd); |
265 |
return -1; |
266 |
} |
267 |
close(fd); |
268 |
return size;
|
269 |
} |
270 |
|
271 |
void cpu_outb(CPUState *env, int addr, int val) |
272 |
{ |
273 |
addr &= (MAX_IOPORTS - 1);
|
274 |
ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
|
275 |
} |
276 |
|
277 |
void cpu_outw(CPUState *env, int addr, int val) |
278 |
{ |
279 |
addr &= (MAX_IOPORTS - 1);
|
280 |
ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
|
281 |
} |
282 |
|
283 |
void cpu_outl(CPUState *env, int addr, int val) |
284 |
{ |
285 |
addr &= (MAX_IOPORTS - 1);
|
286 |
ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
|
287 |
} |
288 |
|
289 |
int cpu_inb(CPUState *env, int addr) |
290 |
{ |
291 |
addr &= (MAX_IOPORTS - 1);
|
292 |
return ioport_read_table[0][addr](ioport_opaque[addr], addr); |
293 |
} |
294 |
|
295 |
int cpu_inw(CPUState *env, int addr) |
296 |
{ |
297 |
addr &= (MAX_IOPORTS - 1);
|
298 |
return ioport_read_table[1][addr](ioport_opaque[addr], addr); |
299 |
} |
300 |
|
301 |
int cpu_inl(CPUState *env, int addr) |
302 |
{ |
303 |
addr &= (MAX_IOPORTS - 1);
|
304 |
return ioport_read_table[2][addr](ioport_opaque[addr], addr); |
305 |
} |
306 |
|
307 |
/***********************************************************/
|
308 |
void hw_error(const char *fmt, ...) |
309 |
{ |
310 |
va_list ap; |
311 |
|
312 |
va_start(ap, fmt); |
313 |
fprintf(stderr, "qemu: hardware error: ");
|
314 |
vfprintf(stderr, fmt, ap); |
315 |
fprintf(stderr, "\n");
|
316 |
#ifdef TARGET_I386
|
317 |
cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP); |
318 |
#else
|
319 |
cpu_dump_state(global_env, stderr, 0);
|
320 |
#endif
|
321 |
va_end(ap); |
322 |
abort(); |
323 |
} |
324 |
|
325 |
/***********************************************************/
|
326 |
/* timers */
|
327 |
|
328 |
#if defined(__powerpc__)
|
329 |
|
330 |
static inline uint32_t get_tbl(void) |
331 |
{ |
332 |
uint32_t tbl; |
333 |
asm volatile("mftb %0" : "=r" (tbl)); |
334 |
return tbl;
|
335 |
} |
336 |
|
337 |
static inline uint32_t get_tbu(void) |
338 |
{ |
339 |
uint32_t tbl; |
340 |
asm volatile("mftbu %0" : "=r" (tbl)); |
341 |
return tbl;
|
342 |
} |
343 |
|
344 |
int64_t cpu_get_real_ticks(void)
|
345 |
{ |
346 |
uint32_t l, h, h1; |
347 |
/* NOTE: we test if wrapping has occurred */
|
348 |
do {
|
349 |
h = get_tbu(); |
350 |
l = get_tbl(); |
351 |
h1 = get_tbu(); |
352 |
} while (h != h1);
|
353 |
return ((int64_t)h << 32) | l; |
354 |
} |
355 |
|
356 |
#elif defined(__i386__)
|
357 |
|
358 |
int64_t cpu_get_real_ticks(void)
|
359 |
{ |
360 |
int64_t val; |
361 |
asm volatile ("rdtsc" : "=A" (val)); |
362 |
return val;
|
363 |
} |
364 |
|
365 |
#else
|
366 |
#error unsupported CPU
|
367 |
#endif
|
368 |
|
369 |
static int64_t cpu_ticks_offset;
|
370 |
static int cpu_ticks_enabled; |
371 |
|
372 |
static inline int64_t cpu_get_ticks(void) |
373 |
{ |
374 |
if (!cpu_ticks_enabled) {
|
375 |
return cpu_ticks_offset;
|
376 |
} else {
|
377 |
return cpu_get_real_ticks() + cpu_ticks_offset;
|
378 |
} |
379 |
} |
380 |
|
381 |
/* enable cpu_get_ticks() */
|
382 |
void cpu_enable_ticks(void) |
383 |
{ |
384 |
if (!cpu_ticks_enabled) {
|
385 |
cpu_ticks_offset -= cpu_get_real_ticks(); |
386 |
cpu_ticks_enabled = 1;
|
387 |
} |
388 |
} |
389 |
|
390 |
/* disable cpu_get_ticks() : the clock is stopped. You must not call
|
391 |
cpu_get_ticks() after that. */
|
392 |
void cpu_disable_ticks(void) |
393 |
{ |
394 |
if (cpu_ticks_enabled) {
|
395 |
cpu_ticks_offset = cpu_get_ticks(); |
396 |
cpu_ticks_enabled = 0;
|
397 |
} |
398 |
} |
399 |
|
400 |
static int64_t get_clock(void) |
401 |
{ |
402 |
#ifdef _WIN32
|
403 |
struct _timeb tb;
|
404 |
_ftime(&tb); |
405 |
return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000; |
406 |
#else
|
407 |
struct timeval tv;
|
408 |
gettimeofday(&tv, NULL);
|
409 |
return tv.tv_sec * 1000000LL + tv.tv_usec; |
410 |
#endif
|
411 |
} |
412 |
|
413 |
void cpu_calibrate_ticks(void) |
414 |
{ |
415 |
int64_t usec, ticks; |
416 |
|
417 |
usec = get_clock(); |
418 |
ticks = cpu_get_real_ticks(); |
419 |
#ifdef _WIN32
|
420 |
Sleep(50);
|
421 |
#else
|
422 |
usleep(50 * 1000); |
423 |
#endif
|
424 |
usec = get_clock() - usec; |
425 |
ticks = cpu_get_real_ticks() - ticks; |
426 |
ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec; |
427 |
} |
428 |
|
429 |
/* compute with 96 bit intermediate result: (a*b)/c */
|
430 |
uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c) |
431 |
{ |
432 |
union {
|
433 |
uint64_t ll; |
434 |
struct {
|
435 |
#ifdef WORDS_BIGENDIAN
|
436 |
uint32_t high, low; |
437 |
#else
|
438 |
uint32_t low, high; |
439 |
#endif
|
440 |
} l; |
441 |
} u, res; |
442 |
uint64_t rl, rh; |
443 |
|
444 |
u.ll = a; |
445 |
rl = (uint64_t)u.l.low * (uint64_t)b; |
446 |
rh = (uint64_t)u.l.high * (uint64_t)b; |
447 |
rh += (rl >> 32);
|
448 |
res.l.high = rh / c; |
449 |
res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c; |
450 |
return res.ll;
|
451 |
} |
452 |
|
453 |
#define QEMU_TIMER_REALTIME 0 |
454 |
#define QEMU_TIMER_VIRTUAL 1 |
455 |
|
456 |
struct QEMUClock {
|
457 |
int type;
|
458 |
/* XXX: add frequency */
|
459 |
}; |
460 |
|
461 |
struct QEMUTimer {
|
462 |
QEMUClock *clock; |
463 |
int64_t expire_time; |
464 |
QEMUTimerCB *cb; |
465 |
void *opaque;
|
466 |
struct QEMUTimer *next;
|
467 |
}; |
468 |
|
469 |
QEMUClock *rt_clock; |
470 |
QEMUClock *vm_clock; |
471 |
|
472 |
static QEMUTimer *active_timers[2]; |
473 |
#ifdef _WIN32
|
474 |
static MMRESULT timerID;
|
475 |
#else
|
476 |
/* frequency of the times() clock tick */
|
477 |
static int timer_freq; |
478 |
#endif
|
479 |
|
480 |
QEMUClock *qemu_new_clock(int type)
|
481 |
{ |
482 |
QEMUClock *clock; |
483 |
clock = qemu_mallocz(sizeof(QEMUClock));
|
484 |
if (!clock)
|
485 |
return NULL; |
486 |
clock->type = type; |
487 |
return clock;
|
488 |
} |
489 |
|
490 |
QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
|
491 |
{ |
492 |
QEMUTimer *ts; |
493 |
|
494 |
ts = qemu_mallocz(sizeof(QEMUTimer));
|
495 |
ts->clock = clock; |
496 |
ts->cb = cb; |
497 |
ts->opaque = opaque; |
498 |
return ts;
|
499 |
} |
500 |
|
501 |
void qemu_free_timer(QEMUTimer *ts)
|
502 |
{ |
503 |
qemu_free(ts); |
504 |
} |
505 |
|
506 |
/* stop a timer, but do not dealloc it */
|
507 |
void qemu_del_timer(QEMUTimer *ts)
|
508 |
{ |
509 |
QEMUTimer **pt, *t; |
510 |
|
511 |
/* NOTE: this code must be signal safe because
|
512 |
qemu_timer_expired() can be called from a signal. */
|
513 |
pt = &active_timers[ts->clock->type]; |
514 |
for(;;) {
|
515 |
t = *pt; |
516 |
if (!t)
|
517 |
break;
|
518 |
if (t == ts) {
|
519 |
*pt = t->next; |
520 |
break;
|
521 |
} |
522 |
pt = &t->next; |
523 |
} |
524 |
} |
525 |
|
526 |
/* modify the current timer so that it will be fired when current_time
|
527 |
>= expire_time. The corresponding callback will be called. */
|
528 |
void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
|
529 |
{ |
530 |
QEMUTimer **pt, *t; |
531 |
|
532 |
qemu_del_timer(ts); |
533 |
|
534 |
/* add the timer in the sorted list */
|
535 |
/* NOTE: this code must be signal safe because
|
536 |
qemu_timer_expired() can be called from a signal. */
|
537 |
pt = &active_timers[ts->clock->type]; |
538 |
for(;;) {
|
539 |
t = *pt; |
540 |
if (!t)
|
541 |
break;
|
542 |
if (t->expire_time > expire_time)
|
543 |
break;
|
544 |
pt = &t->next; |
545 |
} |
546 |
ts->expire_time = expire_time; |
547 |
ts->next = *pt; |
548 |
*pt = ts; |
549 |
} |
550 |
|
551 |
int qemu_timer_pending(QEMUTimer *ts)
|
552 |
{ |
553 |
QEMUTimer *t; |
554 |
for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) { |
555 |
if (t == ts)
|
556 |
return 1; |
557 |
} |
558 |
return 0; |
559 |
} |
560 |
|
561 |
static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time) |
562 |
{ |
563 |
if (!timer_head)
|
564 |
return 0; |
565 |
return (timer_head->expire_time <= current_time);
|
566 |
} |
567 |
|
568 |
static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time) |
569 |
{ |
570 |
QEMUTimer *ts; |
571 |
|
572 |
for(;;) {
|
573 |
ts = *ptimer_head; |
574 |
if (ts->expire_time > current_time)
|
575 |
break;
|
576 |
/* remove timer from the list before calling the callback */
|
577 |
*ptimer_head = ts->next; |
578 |
ts->next = NULL;
|
579 |
|
580 |
/* run the callback (the timer list can be modified) */
|
581 |
ts->cb(ts->opaque); |
582 |
} |
583 |
} |
584 |
|
585 |
int64_t qemu_get_clock(QEMUClock *clock) |
586 |
{ |
587 |
switch(clock->type) {
|
588 |
case QEMU_TIMER_REALTIME:
|
589 |
#ifdef _WIN32
|
590 |
return GetTickCount();
|
591 |
#else
|
592 |
/* XXX: portability among Linux hosts */
|
593 |
if (timer_freq == 100) { |
594 |
return times(NULL) * 10; |
595 |
} else {
|
596 |
return ((int64_t)times(NULL) * 1000) / timer_freq; |
597 |
} |
598 |
#endif
|
599 |
default:
|
600 |
case QEMU_TIMER_VIRTUAL:
|
601 |
return cpu_get_ticks();
|
602 |
} |
603 |
} |
604 |
|
605 |
/* save a timer */
|
606 |
void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
|
607 |
{ |
608 |
uint64_t expire_time; |
609 |
|
610 |
if (qemu_timer_pending(ts)) {
|
611 |
expire_time = ts->expire_time; |
612 |
} else {
|
613 |
expire_time = -1;
|
614 |
} |
615 |
qemu_put_be64(f, expire_time); |
616 |
} |
617 |
|
618 |
void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
|
619 |
{ |
620 |
uint64_t expire_time; |
621 |
|
622 |
expire_time = qemu_get_be64(f); |
623 |
if (expire_time != -1) { |
624 |
qemu_mod_timer(ts, expire_time); |
625 |
} else {
|
626 |
qemu_del_timer(ts); |
627 |
} |
628 |
} |
629 |
|
630 |
static void timer_save(QEMUFile *f, void *opaque) |
631 |
{ |
632 |
if (cpu_ticks_enabled) {
|
633 |
hw_error("cannot save state if virtual timers are running");
|
634 |
} |
635 |
qemu_put_be64s(f, &cpu_ticks_offset); |
636 |
qemu_put_be64s(f, &ticks_per_sec); |
637 |
} |
638 |
|
639 |
static int timer_load(QEMUFile *f, void *opaque, int version_id) |
640 |
{ |
641 |
if (version_id != 1) |
642 |
return -EINVAL;
|
643 |
if (cpu_ticks_enabled) {
|
644 |
return -EINVAL;
|
645 |
} |
646 |
qemu_get_be64s(f, &cpu_ticks_offset); |
647 |
qemu_get_be64s(f, &ticks_per_sec); |
648 |
return 0; |
649 |
} |
650 |
|
651 |
#ifdef _WIN32
|
652 |
void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
|
653 |
DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) |
654 |
#else
|
655 |
static void host_alarm_handler(int host_signum) |
656 |
#endif
|
657 |
{ |
658 |
if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
|
659 |
qemu_get_clock(vm_clock)) || |
660 |
qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME], |
661 |
qemu_get_clock(rt_clock))) { |
662 |
/* stop the cpu because a timer occured */
|
663 |
cpu_interrupt(global_env, CPU_INTERRUPT_EXIT); |
664 |
} |
665 |
} |
666 |
|
667 |
static void init_timers(void) |
668 |
{ |
669 |
rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME); |
670 |
vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL); |
671 |
|
672 |
#ifdef _WIN32
|
673 |
{ |
674 |
int count=0; |
675 |
timerID = timeSetEvent(10, // interval (ms) |
676 |
0, // resolution |
677 |
host_alarm_handler, // function
|
678 |
(DWORD)&count, // user parameter
|
679 |
TIME_PERIODIC | TIME_CALLBACK_FUNCTION); |
680 |
if( !timerID ) {
|
681 |
perror("failed timer alarm");
|
682 |
exit(1);
|
683 |
} |
684 |
} |
685 |
pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000; |
686 |
#else
|
687 |
{ |
688 |
struct sigaction act;
|
689 |
struct itimerval itv;
|
690 |
|
691 |
/* get times() syscall frequency */
|
692 |
timer_freq = sysconf(_SC_CLK_TCK); |
693 |
|
694 |
/* timer signal */
|
695 |
sigfillset(&act.sa_mask); |
696 |
act.sa_flags = 0;
|
697 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
698 |
act.sa_flags |= SA_ONSTACK; |
699 |
#endif
|
700 |
act.sa_handler = host_alarm_handler; |
701 |
sigaction(SIGALRM, &act, NULL);
|
702 |
|
703 |
itv.it_interval.tv_sec = 0;
|
704 |
itv.it_interval.tv_usec = 1000;
|
705 |
itv.it_value.tv_sec = 0;
|
706 |
itv.it_value.tv_usec = 10 * 1000; |
707 |
setitimer(ITIMER_REAL, &itv, NULL);
|
708 |
/* we probe the tick duration of the kernel to inform the user if
|
709 |
the emulated kernel requested a too high timer frequency */
|
710 |
getitimer(ITIMER_REAL, &itv); |
711 |
pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) / |
712 |
1000000;
|
713 |
} |
714 |
#endif
|
715 |
} |
716 |
|
717 |
void quit_timers(void) |
718 |
{ |
719 |
#ifdef _WIN32
|
720 |
timeKillEvent(timerID); |
721 |
#endif
|
722 |
} |
723 |
|
724 |
/***********************************************************/
|
725 |
/* serial device */
|
726 |
|
727 |
#ifdef _WIN32
|
728 |
|
729 |
int serial_open_device(void) |
730 |
{ |
731 |
return -1; |
732 |
} |
733 |
|
734 |
#else
|
735 |
|
736 |
int serial_open_device(void) |
737 |
{ |
738 |
char slave_name[1024]; |
739 |
int master_fd, slave_fd;
|
740 |
|
741 |
if (serial_console == NULL && nographic) { |
742 |
/* use console for serial port */
|
743 |
return 0; |
744 |
} else {
|
745 |
if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) { |
746 |
fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
|
747 |
return -1; |
748 |
} |
749 |
fprintf(stderr, "Serial port redirected to %s\n", slave_name);
|
750 |
return master_fd;
|
751 |
} |
752 |
} |
753 |
|
754 |
#endif
|
755 |
|
756 |
/***********************************************************/
|
757 |
/* Linux network device redirectors */
|
758 |
|
759 |
void hex_dump(FILE *f, const uint8_t *buf, int size) |
760 |
{ |
761 |
int len, i, j, c;
|
762 |
|
763 |
for(i=0;i<size;i+=16) { |
764 |
len = size - i; |
765 |
if (len > 16) |
766 |
len = 16;
|
767 |
fprintf(f, "%08x ", i);
|
768 |
for(j=0;j<16;j++) { |
769 |
if (j < len)
|
770 |
fprintf(f, " %02x", buf[i+j]);
|
771 |
else
|
772 |
fprintf(f, " ");
|
773 |
} |
774 |
fprintf(f, " ");
|
775 |
for(j=0;j<len;j++) { |
776 |
c = buf[i+j]; |
777 |
if (c < ' ' || c > '~') |
778 |
c = '.';
|
779 |
fprintf(f, "%c", c);
|
780 |
} |
781 |
fprintf(f, "\n");
|
782 |
} |
783 |
} |
784 |
|
785 |
void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
786 |
{ |
787 |
nd->send_packet(nd, buf, size); |
788 |
} |
789 |
|
790 |
void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
|
791 |
IOReadHandler *fd_read, void *opaque)
|
792 |
{ |
793 |
nd->add_read_packet(nd, fd_can_read, fd_read, opaque); |
794 |
} |
795 |
|
796 |
/* dummy network adapter */
|
797 |
|
798 |
static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
799 |
{ |
800 |
} |
801 |
|
802 |
static void dummy_add_read_packet(NetDriverState *nd, |
803 |
IOCanRWHandler *fd_can_read, |
804 |
IOReadHandler *fd_read, void *opaque)
|
805 |
{ |
806 |
} |
807 |
|
808 |
static int net_dummy_init(NetDriverState *nd) |
809 |
{ |
810 |
nd->send_packet = dummy_send_packet; |
811 |
nd->add_read_packet = dummy_add_read_packet; |
812 |
pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy"); |
813 |
return 0; |
814 |
} |
815 |
|
816 |
#if defined(CONFIG_SLIRP)
|
817 |
|
818 |
/* slirp network adapter */
|
819 |
|
820 |
static void *slirp_fd_opaque; |
821 |
static IOCanRWHandler *slirp_fd_can_read;
|
822 |
static IOReadHandler *slirp_fd_read;
|
823 |
static int slirp_inited; |
824 |
|
825 |
int slirp_can_output(void) |
826 |
{ |
827 |
return slirp_fd_can_read(slirp_fd_opaque);
|
828 |
} |
829 |
|
830 |
void slirp_output(const uint8_t *pkt, int pkt_len) |
831 |
{ |
832 |
#if 0
|
833 |
printf("output:\n");
|
834 |
hex_dump(stdout, pkt, pkt_len);
|
835 |
#endif
|
836 |
slirp_fd_read(slirp_fd_opaque, pkt, pkt_len); |
837 |
} |
838 |
|
839 |
static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
840 |
{ |
841 |
#if 0
|
842 |
printf("input:\n");
|
843 |
hex_dump(stdout, buf, size);
|
844 |
#endif
|
845 |
slirp_input(buf, size); |
846 |
} |
847 |
|
848 |
static void slirp_add_read_packet(NetDriverState *nd, |
849 |
IOCanRWHandler *fd_can_read, |
850 |
IOReadHandler *fd_read, void *opaque)
|
851 |
{ |
852 |
slirp_fd_opaque = opaque; |
853 |
slirp_fd_can_read = fd_can_read; |
854 |
slirp_fd_read = fd_read; |
855 |
} |
856 |
|
857 |
static int net_slirp_init(NetDriverState *nd) |
858 |
{ |
859 |
if (!slirp_inited) {
|
860 |
slirp_inited = 1;
|
861 |
slirp_init(); |
862 |
} |
863 |
nd->send_packet = slirp_send_packet; |
864 |
nd->add_read_packet = slirp_add_read_packet; |
865 |
pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp"); |
866 |
return 0; |
867 |
} |
868 |
|
869 |
#endif /* CONFIG_SLIRP */ |
870 |
|
871 |
#if !defined(_WIN32)
|
872 |
|
873 |
static int tun_open(char *ifname, int ifname_size) |
874 |
{ |
875 |
struct ifreq ifr;
|
876 |
int fd, ret;
|
877 |
|
878 |
fd = open("/dev/net/tun", O_RDWR);
|
879 |
if (fd < 0) { |
880 |
fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
|
881 |
return -1; |
882 |
} |
883 |
memset(&ifr, 0, sizeof(ifr)); |
884 |
ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
885 |
pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
|
886 |
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
|
887 |
if (ret != 0) { |
888 |
fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
|
889 |
close(fd); |
890 |
return -1; |
891 |
} |
892 |
printf("Connected to host network interface: %s\n", ifr.ifr_name);
|
893 |
pstrcpy(ifname, ifname_size, ifr.ifr_name); |
894 |
fcntl(fd, F_SETFL, O_NONBLOCK); |
895 |
return fd;
|
896 |
} |
897 |
|
898 |
static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size) |
899 |
{ |
900 |
write(nd->fd, buf, size); |
901 |
} |
902 |
|
903 |
static void tun_add_read_packet(NetDriverState *nd, |
904 |
IOCanRWHandler *fd_can_read, |
905 |
IOReadHandler *fd_read, void *opaque)
|
906 |
{ |
907 |
qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque); |
908 |
} |
909 |
|
910 |
static int net_tun_init(NetDriverState *nd) |
911 |
{ |
912 |
int pid, status;
|
913 |
char *args[3]; |
914 |
char **parg;
|
915 |
|
916 |
nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
|
917 |
if (nd->fd < 0) |
918 |
return -1; |
919 |
|
920 |
/* try to launch network init script */
|
921 |
pid = fork(); |
922 |
if (pid >= 0) { |
923 |
if (pid == 0) { |
924 |
parg = args; |
925 |
*parg++ = network_script; |
926 |
*parg++ = nd->ifname; |
927 |
*parg++ = NULL;
|
928 |
execv(network_script, args); |
929 |
exit(1);
|
930 |
} |
931 |
while (waitpid(pid, &status, 0) != pid); |
932 |
if (!WIFEXITED(status) ||
|
933 |
WEXITSTATUS(status) != 0) {
|
934 |
fprintf(stderr, "%s: could not launch network script\n",
|
935 |
network_script); |
936 |
} |
937 |
} |
938 |
nd->send_packet = tun_send_packet; |
939 |
nd->add_read_packet = tun_add_read_packet; |
940 |
return 0; |
941 |
} |
942 |
|
943 |
static int net_fd_init(NetDriverState *nd, int fd) |
944 |
{ |
945 |
nd->fd = fd; |
946 |
nd->send_packet = tun_send_packet; |
947 |
nd->add_read_packet = tun_add_read_packet; |
948 |
pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd"); |
949 |
return 0; |
950 |
} |
951 |
|
952 |
#endif /* !_WIN32 */ |
953 |
|
954 |
/***********************************************************/
|
955 |
/* dumb display */
|
956 |
|
957 |
#ifdef _WIN32
|
958 |
|
959 |
static void term_exit(void) |
960 |
{ |
961 |
} |
962 |
|
963 |
static void term_init(void) |
964 |
{ |
965 |
} |
966 |
|
967 |
#else
|
968 |
|
969 |
/* init terminal so that we can grab keys */
|
970 |
static struct termios oldtty; |
971 |
|
972 |
static void term_exit(void) |
973 |
{ |
974 |
tcsetattr (0, TCSANOW, &oldtty);
|
975 |
} |
976 |
|
977 |
static void term_init(void) |
978 |
{ |
979 |
struct termios tty;
|
980 |
|
981 |
tcgetattr (0, &tty);
|
982 |
oldtty = tty; |
983 |
|
984 |
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
985 |
|INLCR|IGNCR|ICRNL|IXON); |
986 |
tty.c_oflag |= OPOST; |
987 |
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
988 |
/* if graphical mode, we allow Ctrl-C handling */
|
989 |
if (nographic)
|
990 |
tty.c_lflag &= ~ISIG; |
991 |
tty.c_cflag &= ~(CSIZE|PARENB); |
992 |
tty.c_cflag |= CS8; |
993 |
tty.c_cc[VMIN] = 1;
|
994 |
tty.c_cc[VTIME] = 0;
|
995 |
|
996 |
tcsetattr (0, TCSANOW, &tty);
|
997 |
|
998 |
atexit(term_exit); |
999 |
|
1000 |
fcntl(0, F_SETFL, O_NONBLOCK);
|
1001 |
} |
1002 |
|
1003 |
#endif
|
1004 |
|
1005 |
static void dumb_update(DisplayState *ds, int x, int y, int w, int h) |
1006 |
{ |
1007 |
} |
1008 |
|
1009 |
static void dumb_resize(DisplayState *ds, int w, int h) |
1010 |
{ |
1011 |
} |
1012 |
|
1013 |
static void dumb_refresh(DisplayState *ds) |
1014 |
{ |
1015 |
vga_update_display(); |
1016 |
} |
1017 |
|
1018 |
void dumb_display_init(DisplayState *ds)
|
1019 |
{ |
1020 |
ds->data = NULL;
|
1021 |
ds->linesize = 0;
|
1022 |
ds->depth = 0;
|
1023 |
ds->dpy_update = dumb_update; |
1024 |
ds->dpy_resize = dumb_resize; |
1025 |
ds->dpy_refresh = dumb_refresh; |
1026 |
} |
1027 |
|
1028 |
#if !defined(CONFIG_SOFTMMU)
|
1029 |
/***********************************************************/
|
1030 |
/* cpu signal handler */
|
1031 |
static void host_segv_handler(int host_signum, siginfo_t *info, |
1032 |
void *puc)
|
1033 |
{ |
1034 |
if (cpu_signal_handler(host_signum, info, puc))
|
1035 |
return;
|
1036 |
term_exit(); |
1037 |
abort(); |
1038 |
} |
1039 |
#endif
|
1040 |
|
1041 |
/***********************************************************/
|
1042 |
/* I/O handling */
|
1043 |
|
1044 |
#define MAX_IO_HANDLERS 64 |
1045 |
|
1046 |
typedef struct IOHandlerRecord { |
1047 |
int fd;
|
1048 |
IOCanRWHandler *fd_can_read; |
1049 |
IOReadHandler *fd_read; |
1050 |
void *opaque;
|
1051 |
/* temporary data */
|
1052 |
struct pollfd *ufd;
|
1053 |
int max_size;
|
1054 |
struct IOHandlerRecord *next;
|
1055 |
} IOHandlerRecord; |
1056 |
|
1057 |
static IOHandlerRecord *first_io_handler;
|
1058 |
|
1059 |
int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read, |
1060 |
IOReadHandler *fd_read, void *opaque)
|
1061 |
{ |
1062 |
IOHandlerRecord *ioh; |
1063 |
|
1064 |
ioh = qemu_mallocz(sizeof(IOHandlerRecord));
|
1065 |
if (!ioh)
|
1066 |
return -1; |
1067 |
ioh->fd = fd; |
1068 |
ioh->fd_can_read = fd_can_read; |
1069 |
ioh->fd_read = fd_read; |
1070 |
ioh->opaque = opaque; |
1071 |
ioh->next = first_io_handler; |
1072 |
first_io_handler = ioh; |
1073 |
return 0; |
1074 |
} |
1075 |
|
1076 |
void qemu_del_fd_read_handler(int fd) |
1077 |
{ |
1078 |
IOHandlerRecord **pioh, *ioh; |
1079 |
|
1080 |
pioh = &first_io_handler; |
1081 |
for(;;) {
|
1082 |
ioh = *pioh; |
1083 |
if (ioh == NULL) |
1084 |
break;
|
1085 |
if (ioh->fd == fd) {
|
1086 |
*pioh = ioh->next; |
1087 |
break;
|
1088 |
} |
1089 |
pioh = &ioh->next; |
1090 |
} |
1091 |
} |
1092 |
|
1093 |
/***********************************************************/
|
1094 |
/* savevm/loadvm support */
|
1095 |
|
1096 |
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size) |
1097 |
{ |
1098 |
fwrite(buf, 1, size, f);
|
1099 |
} |
1100 |
|
1101 |
void qemu_put_byte(QEMUFile *f, int v) |
1102 |
{ |
1103 |
fputc(v, f); |
1104 |
} |
1105 |
|
1106 |
void qemu_put_be16(QEMUFile *f, unsigned int v) |
1107 |
{ |
1108 |
qemu_put_byte(f, v >> 8);
|
1109 |
qemu_put_byte(f, v); |
1110 |
} |
1111 |
|
1112 |
void qemu_put_be32(QEMUFile *f, unsigned int v) |
1113 |
{ |
1114 |
qemu_put_byte(f, v >> 24);
|
1115 |
qemu_put_byte(f, v >> 16);
|
1116 |
qemu_put_byte(f, v >> 8);
|
1117 |
qemu_put_byte(f, v); |
1118 |
} |
1119 |
|
1120 |
void qemu_put_be64(QEMUFile *f, uint64_t v)
|
1121 |
{ |
1122 |
qemu_put_be32(f, v >> 32);
|
1123 |
qemu_put_be32(f, v); |
1124 |
} |
1125 |
|
1126 |
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size) |
1127 |
{ |
1128 |
return fread(buf, 1, size, f); |
1129 |
} |
1130 |
|
1131 |
int qemu_get_byte(QEMUFile *f)
|
1132 |
{ |
1133 |
int v;
|
1134 |
v = fgetc(f); |
1135 |
if (v == EOF) |
1136 |
return 0; |
1137 |
else
|
1138 |
return v;
|
1139 |
} |
1140 |
|
1141 |
unsigned int qemu_get_be16(QEMUFile *f) |
1142 |
{ |
1143 |
unsigned int v; |
1144 |
v = qemu_get_byte(f) << 8;
|
1145 |
v |= qemu_get_byte(f); |
1146 |
return v;
|
1147 |
} |
1148 |
|
1149 |
unsigned int qemu_get_be32(QEMUFile *f) |
1150 |
{ |
1151 |
unsigned int v; |
1152 |
v = qemu_get_byte(f) << 24;
|
1153 |
v |= qemu_get_byte(f) << 16;
|
1154 |
v |= qemu_get_byte(f) << 8;
|
1155 |
v |= qemu_get_byte(f); |
1156 |
return v;
|
1157 |
} |
1158 |
|
1159 |
uint64_t qemu_get_be64(QEMUFile *f) |
1160 |
{ |
1161 |
uint64_t v; |
1162 |
v = (uint64_t)qemu_get_be32(f) << 32;
|
1163 |
v |= qemu_get_be32(f); |
1164 |
return v;
|
1165 |
} |
1166 |
|
1167 |
int64_t qemu_ftell(QEMUFile *f) |
1168 |
{ |
1169 |
return ftell(f);
|
1170 |
} |
1171 |
|
1172 |
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
|
1173 |
{ |
1174 |
if (fseek(f, pos, whence) < 0) |
1175 |
return -1; |
1176 |
return ftell(f);
|
1177 |
} |
1178 |
|
1179 |
typedef struct SaveStateEntry { |
1180 |
char idstr[256]; |
1181 |
int instance_id;
|
1182 |
int version_id;
|
1183 |
SaveStateHandler *save_state; |
1184 |
LoadStateHandler *load_state; |
1185 |
void *opaque;
|
1186 |
struct SaveStateEntry *next;
|
1187 |
} SaveStateEntry; |
1188 |
|
1189 |
static SaveStateEntry *first_se;
|
1190 |
|
1191 |
int register_savevm(const char *idstr, |
1192 |
int instance_id,
|
1193 |
int version_id,
|
1194 |
SaveStateHandler *save_state, |
1195 |
LoadStateHandler *load_state, |
1196 |
void *opaque)
|
1197 |
{ |
1198 |
SaveStateEntry *se, **pse; |
1199 |
|
1200 |
se = qemu_malloc(sizeof(SaveStateEntry));
|
1201 |
if (!se)
|
1202 |
return -1; |
1203 |
pstrcpy(se->idstr, sizeof(se->idstr), idstr);
|
1204 |
se->instance_id = instance_id; |
1205 |
se->version_id = version_id; |
1206 |
se->save_state = save_state; |
1207 |
se->load_state = load_state; |
1208 |
se->opaque = opaque; |
1209 |
se->next = NULL;
|
1210 |
|
1211 |
/* add at the end of list */
|
1212 |
pse = &first_se; |
1213 |
while (*pse != NULL) |
1214 |
pse = &(*pse)->next; |
1215 |
*pse = se; |
1216 |
return 0; |
1217 |
} |
1218 |
|
1219 |
#define QEMU_VM_FILE_MAGIC 0x5145564d |
1220 |
#define QEMU_VM_FILE_VERSION 0x00000001 |
1221 |
|
1222 |
int qemu_savevm(const char *filename) |
1223 |
{ |
1224 |
SaveStateEntry *se; |
1225 |
QEMUFile *f; |
1226 |
int len, len_pos, cur_pos, saved_vm_running, ret;
|
1227 |
|
1228 |
saved_vm_running = vm_running; |
1229 |
vm_stop(0);
|
1230 |
|
1231 |
f = fopen(filename, "wb");
|
1232 |
if (!f) {
|
1233 |
ret = -1;
|
1234 |
goto the_end;
|
1235 |
} |
1236 |
|
1237 |
qemu_put_be32(f, QEMU_VM_FILE_MAGIC); |
1238 |
qemu_put_be32(f, QEMU_VM_FILE_VERSION); |
1239 |
|
1240 |
for(se = first_se; se != NULL; se = se->next) { |
1241 |
/* ID string */
|
1242 |
len = strlen(se->idstr); |
1243 |
qemu_put_byte(f, len); |
1244 |
qemu_put_buffer(f, se->idstr, len); |
1245 |
|
1246 |
qemu_put_be32(f, se->instance_id); |
1247 |
qemu_put_be32(f, se->version_id); |
1248 |
|
1249 |
/* record size: filled later */
|
1250 |
len_pos = ftell(f); |
1251 |
qemu_put_be32(f, 0);
|
1252 |
|
1253 |
se->save_state(f, se->opaque); |
1254 |
|
1255 |
/* fill record size */
|
1256 |
cur_pos = ftell(f); |
1257 |
len = ftell(f) - len_pos - 4;
|
1258 |
fseek(f, len_pos, SEEK_SET); |
1259 |
qemu_put_be32(f, len); |
1260 |
fseek(f, cur_pos, SEEK_SET); |
1261 |
} |
1262 |
|
1263 |
fclose(f); |
1264 |
ret = 0;
|
1265 |
the_end:
|
1266 |
if (saved_vm_running)
|
1267 |
vm_start(); |
1268 |
return ret;
|
1269 |
} |
1270 |
|
1271 |
static SaveStateEntry *find_se(const char *idstr, int instance_id) |
1272 |
{ |
1273 |
SaveStateEntry *se; |
1274 |
|
1275 |
for(se = first_se; se != NULL; se = se->next) { |
1276 |
if (!strcmp(se->idstr, idstr) &&
|
1277 |
instance_id == se->instance_id) |
1278 |
return se;
|
1279 |
} |
1280 |
return NULL; |
1281 |
} |
1282 |
|
1283 |
int qemu_loadvm(const char *filename) |
1284 |
{ |
1285 |
SaveStateEntry *se; |
1286 |
QEMUFile *f; |
1287 |
int len, cur_pos, ret, instance_id, record_len, version_id;
|
1288 |
int saved_vm_running;
|
1289 |
unsigned int v; |
1290 |
char idstr[256]; |
1291 |
|
1292 |
saved_vm_running = vm_running; |
1293 |
vm_stop(0);
|
1294 |
|
1295 |
f = fopen(filename, "rb");
|
1296 |
if (!f) {
|
1297 |
ret = -1;
|
1298 |
goto the_end;
|
1299 |
} |
1300 |
|
1301 |
v = qemu_get_be32(f); |
1302 |
if (v != QEMU_VM_FILE_MAGIC)
|
1303 |
goto fail;
|
1304 |
v = qemu_get_be32(f); |
1305 |
if (v != QEMU_VM_FILE_VERSION) {
|
1306 |
fail:
|
1307 |
fclose(f); |
1308 |
ret = -1;
|
1309 |
goto the_end;
|
1310 |
} |
1311 |
for(;;) {
|
1312 |
#if defined (DO_TB_FLUSH)
|
1313 |
tb_flush(); |
1314 |
#endif
|
1315 |
len = qemu_get_byte(f); |
1316 |
if (feof(f))
|
1317 |
break;
|
1318 |
qemu_get_buffer(f, idstr, len); |
1319 |
idstr[len] = '\0';
|
1320 |
instance_id = qemu_get_be32(f); |
1321 |
version_id = qemu_get_be32(f); |
1322 |
record_len = qemu_get_be32(f); |
1323 |
#if 0
|
1324 |
printf("idstr=%s instance=0x%x version=%d len=%d\n",
|
1325 |
idstr, instance_id, version_id, record_len);
|
1326 |
#endif
|
1327 |
cur_pos = ftell(f); |
1328 |
se = find_se(idstr, instance_id); |
1329 |
if (!se) {
|
1330 |
fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
|
1331 |
instance_id, idstr); |
1332 |
} else {
|
1333 |
ret = se->load_state(f, se->opaque, version_id); |
1334 |
if (ret < 0) { |
1335 |
fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
|
1336 |
instance_id, idstr); |
1337 |
} |
1338 |
} |
1339 |
/* always seek to exact end of record */
|
1340 |
qemu_fseek(f, cur_pos + record_len, SEEK_SET); |
1341 |
} |
1342 |
fclose(f); |
1343 |
ret = 0;
|
1344 |
the_end:
|
1345 |
if (saved_vm_running)
|
1346 |
vm_start(); |
1347 |
return ret;
|
1348 |
} |
1349 |
|
1350 |
/***********************************************************/
|
1351 |
/* cpu save/restore */
|
1352 |
|
1353 |
#if defined(TARGET_I386)
|
1354 |
|
1355 |
static void cpu_put_seg(QEMUFile *f, SegmentCache *dt) |
1356 |
{ |
1357 |
qemu_put_be32(f, (uint32_t)dt->base); |
1358 |
qemu_put_be32(f, dt->limit); |
1359 |
qemu_put_be32(f, dt->flags); |
1360 |
} |
1361 |
|
1362 |
static void cpu_get_seg(QEMUFile *f, SegmentCache *dt) |
1363 |
{ |
1364 |
dt->base = (uint8_t *)qemu_get_be32(f); |
1365 |
dt->limit = qemu_get_be32(f); |
1366 |
dt->flags = qemu_get_be32(f); |
1367 |
} |
1368 |
|
1369 |
void cpu_save(QEMUFile *f, void *opaque) |
1370 |
{ |
1371 |
CPUState *env = opaque; |
1372 |
uint16_t fptag, fpus, fpuc; |
1373 |
uint32_t hflags; |
1374 |
int i;
|
1375 |
|
1376 |
for(i = 0; i < 8; i++) |
1377 |
qemu_put_be32s(f, &env->regs[i]); |
1378 |
qemu_put_be32s(f, &env->eip); |
1379 |
qemu_put_be32s(f, &env->eflags); |
1380 |
qemu_put_be32s(f, &env->eflags); |
1381 |
hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
|
1382 |
qemu_put_be32s(f, &hflags); |
1383 |
|
1384 |
/* FPU */
|
1385 |
fpuc = env->fpuc; |
1386 |
fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11; |
1387 |
fptag = 0;
|
1388 |
for (i=7; i>=0; i--) { |
1389 |
fptag <<= 2;
|
1390 |
if (env->fptags[i]) {
|
1391 |
fptag |= 3;
|
1392 |
} |
1393 |
} |
1394 |
|
1395 |
qemu_put_be16s(f, &fpuc); |
1396 |
qemu_put_be16s(f, &fpus); |
1397 |
qemu_put_be16s(f, &fptag); |
1398 |
|
1399 |
for(i = 0; i < 8; i++) { |
1400 |
uint64_t mant; |
1401 |
uint16_t exp; |
1402 |
cpu_get_fp80(&mant, &exp, env->fpregs[i]); |
1403 |
qemu_put_be64(f, mant); |
1404 |
qemu_put_be16(f, exp); |
1405 |
} |
1406 |
|
1407 |
for(i = 0; i < 6; i++) |
1408 |
cpu_put_seg(f, &env->segs[i]); |
1409 |
cpu_put_seg(f, &env->ldt); |
1410 |
cpu_put_seg(f, &env->tr); |
1411 |
cpu_put_seg(f, &env->gdt); |
1412 |
cpu_put_seg(f, &env->idt); |
1413 |
|
1414 |
qemu_put_be32s(f, &env->sysenter_cs); |
1415 |
qemu_put_be32s(f, &env->sysenter_esp); |
1416 |
qemu_put_be32s(f, &env->sysenter_eip); |
1417 |
|
1418 |
qemu_put_be32s(f, &env->cr[0]);
|
1419 |
qemu_put_be32s(f, &env->cr[2]);
|
1420 |
qemu_put_be32s(f, &env->cr[3]);
|
1421 |
qemu_put_be32s(f, &env->cr[4]);
|
1422 |
|
1423 |
for(i = 0; i < 8; i++) |
1424 |
qemu_put_be32s(f, &env->dr[i]); |
1425 |
|
1426 |
/* MMU */
|
1427 |
qemu_put_be32s(f, &env->a20_mask); |
1428 |
} |
1429 |
|
1430 |
int cpu_load(QEMUFile *f, void *opaque, int version_id) |
1431 |
{ |
1432 |
CPUState *env = opaque; |
1433 |
int i;
|
1434 |
uint32_t hflags; |
1435 |
uint16_t fpus, fpuc, fptag; |
1436 |
|
1437 |
if (version_id != 1) |
1438 |
return -EINVAL;
|
1439 |
for(i = 0; i < 8; i++) |
1440 |
qemu_get_be32s(f, &env->regs[i]); |
1441 |
qemu_get_be32s(f, &env->eip); |
1442 |
qemu_get_be32s(f, &env->eflags); |
1443 |
qemu_get_be32s(f, &env->eflags); |
1444 |
qemu_get_be32s(f, &hflags); |
1445 |
|
1446 |
qemu_get_be16s(f, &fpuc); |
1447 |
qemu_get_be16s(f, &fpus); |
1448 |
qemu_get_be16s(f, &fptag); |
1449 |
|
1450 |
for(i = 0; i < 8; i++) { |
1451 |
uint64_t mant; |
1452 |
uint16_t exp; |
1453 |
mant = qemu_get_be64(f); |
1454 |
exp = qemu_get_be16(f); |
1455 |
env->fpregs[i] = cpu_set_fp80(mant, exp); |
1456 |
} |
1457 |
|
1458 |
env->fpuc = fpuc; |
1459 |
env->fpstt = (fpus >> 11) & 7; |
1460 |
env->fpus = fpus & ~0x3800;
|
1461 |
for(i = 0; i < 8; i++) { |
1462 |
env->fptags[i] = ((fptag & 3) == 3); |
1463 |
fptag >>= 2;
|
1464 |
} |
1465 |
|
1466 |
for(i = 0; i < 6; i++) |
1467 |
cpu_get_seg(f, &env->segs[i]); |
1468 |
cpu_get_seg(f, &env->ldt); |
1469 |
cpu_get_seg(f, &env->tr); |
1470 |
cpu_get_seg(f, &env->gdt); |
1471 |
cpu_get_seg(f, &env->idt); |
1472 |
|
1473 |
qemu_get_be32s(f, &env->sysenter_cs); |
1474 |
qemu_get_be32s(f, &env->sysenter_esp); |
1475 |
qemu_get_be32s(f, &env->sysenter_eip); |
1476 |
|
1477 |
qemu_get_be32s(f, &env->cr[0]);
|
1478 |
qemu_get_be32s(f, &env->cr[2]);
|
1479 |
qemu_get_be32s(f, &env->cr[3]);
|
1480 |
qemu_get_be32s(f, &env->cr[4]);
|
1481 |
|
1482 |
for(i = 0; i < 8; i++) |
1483 |
qemu_get_be32s(f, &env->dr[i]); |
1484 |
|
1485 |
/* MMU */
|
1486 |
qemu_get_be32s(f, &env->a20_mask); |
1487 |
|
1488 |
/* XXX: compute hflags from scratch, except for CPL and IIF */
|
1489 |
env->hflags = hflags; |
1490 |
tlb_flush(env, 1);
|
1491 |
return 0; |
1492 |
} |
1493 |
|
1494 |
#elif defined(TARGET_PPC)
|
1495 |
void cpu_save(QEMUFile *f, void *opaque) |
1496 |
{ |
1497 |
} |
1498 |
|
1499 |
int cpu_load(QEMUFile *f, void *opaque, int version_id) |
1500 |
{ |
1501 |
return 0; |
1502 |
} |
1503 |
#else
|
1504 |
|
1505 |
#warning No CPU save/restore functions
|
1506 |
|
1507 |
#endif
|
1508 |
|
1509 |
/***********************************************************/
|
1510 |
/* ram save/restore */
|
1511 |
|
1512 |
/* we just avoid storing empty pages */
|
1513 |
static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len) |
1514 |
{ |
1515 |
int i, v;
|
1516 |
|
1517 |
v = buf[0];
|
1518 |
for(i = 1; i < len; i++) { |
1519 |
if (buf[i] != v)
|
1520 |
goto normal_save;
|
1521 |
} |
1522 |
qemu_put_byte(f, 1);
|
1523 |
qemu_put_byte(f, v); |
1524 |
return;
|
1525 |
normal_save:
|
1526 |
qemu_put_byte(f, 0);
|
1527 |
qemu_put_buffer(f, buf, len); |
1528 |
} |
1529 |
|
1530 |
static int ram_get_page(QEMUFile *f, uint8_t *buf, int len) |
1531 |
{ |
1532 |
int v;
|
1533 |
|
1534 |
v = qemu_get_byte(f); |
1535 |
switch(v) {
|
1536 |
case 0: |
1537 |
if (qemu_get_buffer(f, buf, len) != len)
|
1538 |
return -EIO;
|
1539 |
break;
|
1540 |
case 1: |
1541 |
v = qemu_get_byte(f); |
1542 |
memset(buf, v, len); |
1543 |
break;
|
1544 |
default:
|
1545 |
return -EINVAL;
|
1546 |
} |
1547 |
return 0; |
1548 |
} |
1549 |
|
1550 |
static void ram_save(QEMUFile *f, void *opaque) |
1551 |
{ |
1552 |
int i;
|
1553 |
qemu_put_be32(f, phys_ram_size); |
1554 |
for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
1555 |
ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
1556 |
} |
1557 |
} |
1558 |
|
1559 |
static int ram_load(QEMUFile *f, void *opaque, int version_id) |
1560 |
{ |
1561 |
int i, ret;
|
1562 |
|
1563 |
if (version_id != 1) |
1564 |
return -EINVAL;
|
1565 |
if (qemu_get_be32(f) != phys_ram_size)
|
1566 |
return -EINVAL;
|
1567 |
for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) { |
1568 |
ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE); |
1569 |
if (ret)
|
1570 |
return ret;
|
1571 |
} |
1572 |
return 0; |
1573 |
} |
1574 |
|
1575 |
/***********************************************************/
|
1576 |
/* main execution loop */
|
1577 |
|
1578 |
void gui_update(void *opaque) |
1579 |
{ |
1580 |
display_state.dpy_refresh(&display_state); |
1581 |
qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock)); |
1582 |
} |
1583 |
|
1584 |
/* XXX: support several handlers */
|
1585 |
VMStopHandler *vm_stop_cb; |
1586 |
VMStopHandler *vm_stop_opaque; |
1587 |
|
1588 |
int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque) |
1589 |
{ |
1590 |
vm_stop_cb = cb; |
1591 |
vm_stop_opaque = opaque; |
1592 |
return 0; |
1593 |
} |
1594 |
|
1595 |
void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque) |
1596 |
{ |
1597 |
vm_stop_cb = NULL;
|
1598 |
} |
1599 |
|
1600 |
void vm_start(void) |
1601 |
{ |
1602 |
if (!vm_running) {
|
1603 |
cpu_enable_ticks(); |
1604 |
vm_running = 1;
|
1605 |
} |
1606 |
} |
1607 |
|
1608 |
void vm_stop(int reason) |
1609 |
{ |
1610 |
if (vm_running) {
|
1611 |
cpu_disable_ticks(); |
1612 |
vm_running = 0;
|
1613 |
if (reason != 0) { |
1614 |
if (vm_stop_cb) {
|
1615 |
vm_stop_cb(vm_stop_opaque, reason); |
1616 |
} |
1617 |
} |
1618 |
} |
1619 |
} |
1620 |
|
1621 |
int main_loop(void) |
1622 |
{ |
1623 |
#ifndef _WIN32
|
1624 |
struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf; |
1625 |
IOHandlerRecord *ioh, *ioh_next; |
1626 |
uint8_t buf[4096];
|
1627 |
int n, max_size;
|
1628 |
#endif
|
1629 |
int ret, timeout;
|
1630 |
CPUState *env = global_env; |
1631 |
|
1632 |
for(;;) {
|
1633 |
if (vm_running) {
|
1634 |
ret = cpu_exec(env); |
1635 |
if (reset_requested) {
|
1636 |
ret = EXCP_INTERRUPT; |
1637 |
break;
|
1638 |
} |
1639 |
if (ret == EXCP_DEBUG) {
|
1640 |
vm_stop(EXCP_DEBUG); |
1641 |
} |
1642 |
/* if hlt instruction, we wait until the next IRQ */
|
1643 |
/* XXX: use timeout computed from timers */
|
1644 |
if (ret == EXCP_HLT)
|
1645 |
timeout = 10;
|
1646 |
else
|
1647 |
timeout = 0;
|
1648 |
} else {
|
1649 |
timeout = 10;
|
1650 |
} |
1651 |
|
1652 |
#ifdef _WIN32
|
1653 |
if (timeout > 0) |
1654 |
Sleep(timeout); |
1655 |
#else
|
1656 |
|
1657 |
/* poll any events */
|
1658 |
/* XXX: separate device handlers from system ones */
|
1659 |
pf = ufds; |
1660 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) { |
1661 |
if (!ioh->fd_can_read) {
|
1662 |
max_size = 0;
|
1663 |
pf->fd = ioh->fd; |
1664 |
pf->events = POLLIN; |
1665 |
ioh->ufd = pf; |
1666 |
pf++; |
1667 |
} else {
|
1668 |
max_size = ioh->fd_can_read(ioh->opaque); |
1669 |
if (max_size > 0) { |
1670 |
if (max_size > sizeof(buf)) |
1671 |
max_size = sizeof(buf);
|
1672 |
pf->fd = ioh->fd; |
1673 |
pf->events = POLLIN; |
1674 |
ioh->ufd = pf; |
1675 |
pf++; |
1676 |
} else {
|
1677 |
ioh->ufd = NULL;
|
1678 |
} |
1679 |
} |
1680 |
ioh->max_size = max_size; |
1681 |
} |
1682 |
|
1683 |
ret = poll(ufds, pf - ufds, timeout); |
1684 |
if (ret > 0) { |
1685 |
/* XXX: better handling of removal */
|
1686 |
for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) { |
1687 |
ioh_next = ioh->next; |
1688 |
pf = ioh->ufd; |
1689 |
if (pf) {
|
1690 |
if (pf->revents & POLLIN) {
|
1691 |
if (ioh->max_size == 0) { |
1692 |
/* just a read event */
|
1693 |
ioh->fd_read(ioh->opaque, NULL, 0); |
1694 |
} else {
|
1695 |
n = read(ioh->fd, buf, ioh->max_size); |
1696 |
if (n >= 0) { |
1697 |
ioh->fd_read(ioh->opaque, buf, n); |
1698 |
} else if (errno != -EAGAIN) { |
1699 |
ioh->fd_read(ioh->opaque, NULL, -errno);
|
1700 |
} |
1701 |
} |
1702 |
} |
1703 |
} |
1704 |
} |
1705 |
} |
1706 |
|
1707 |
#if defined(CONFIG_SLIRP)
|
1708 |
/* XXX: merge with poll() */
|
1709 |
if (slirp_inited) {
|
1710 |
fd_set rfds, wfds, xfds; |
1711 |
int nfds;
|
1712 |
struct timeval tv;
|
1713 |
|
1714 |
nfds = -1;
|
1715 |
FD_ZERO(&rfds); |
1716 |
FD_ZERO(&wfds); |
1717 |
FD_ZERO(&xfds); |
1718 |
slirp_select_fill(&nfds, &rfds, &wfds, &xfds); |
1719 |
tv.tv_sec = 0;
|
1720 |
tv.tv_usec = 0;
|
1721 |
ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
|
1722 |
if (ret >= 0) { |
1723 |
slirp_select_poll(&rfds, &wfds, &xfds); |
1724 |
} |
1725 |
} |
1726 |
#endif
|
1727 |
|
1728 |
#endif
|
1729 |
|
1730 |
if (vm_running) {
|
1731 |
qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL], |
1732 |
qemu_get_clock(vm_clock)); |
1733 |
|
1734 |
/* XXX: add explicit timer */
|
1735 |
SB16_run(); |
1736 |
|
1737 |
/* run dma transfers, if any */
|
1738 |
DMA_run(); |
1739 |
} |
1740 |
|
1741 |
/* real time timers */
|
1742 |
qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME], |
1743 |
qemu_get_clock(rt_clock)); |
1744 |
} |
1745 |
cpu_disable_ticks(); |
1746 |
return ret;
|
1747 |
} |
1748 |
|
1749 |
void help(void) |
1750 |
{ |
1751 |
printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003 Fabrice Bellard\n" |
1752 |
"usage: %s [options] [disk_image]\n"
|
1753 |
"\n"
|
1754 |
"'disk_image' is a raw hard image image for IDE hard disk 0\n"
|
1755 |
"\n"
|
1756 |
"Standard options:\n"
|
1757 |
"-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
|
1758 |
"-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
|
1759 |
"-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
|
1760 |
"-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
|
1761 |
"-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
|
1762 |
"-snapshot write to temporary files instead of disk image files\n"
|
1763 |
"-m megs set virtual RAM size to megs MB\n"
|
1764 |
"-nographic disable graphical output and redirect serial I/Os to console\n"
|
1765 |
"\n"
|
1766 |
"Network options:\n"
|
1767 |
"-nics n simulate 'n' network cards [default=1]\n"
|
1768 |
"-macaddr addr set the mac address of the first interface\n"
|
1769 |
"-n script set tap/tun network init script [default=%s]\n"
|
1770 |
"-tun-fd fd use this fd as already opened tap/tun interface\n"
|
1771 |
#ifdef CONFIG_SLIRP
|
1772 |
"-user-net use user mode network stack [default if no tap/tun script]\n"
|
1773 |
#endif
|
1774 |
"-dummy-net use dummy network stack\n"
|
1775 |
"\n"
|
1776 |
"Linux boot specific:\n"
|
1777 |
"-kernel bzImage use 'bzImage' as kernel image\n"
|
1778 |
"-append cmdline use 'cmdline' as kernel command line\n"
|
1779 |
"-initrd file use 'file' as initial ram disk\n"
|
1780 |
"\n"
|
1781 |
"Debug/Expert options:\n"
|
1782 |
"-s wait gdb connection to port %d\n"
|
1783 |
"-p port change gdb connection port\n"
|
1784 |
"-d item1,... output log to %s (use -d ? for a list of log items)\n"
|
1785 |
"-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
|
1786 |
"-L path set the directory for the BIOS and VGA BIOS\n"
|
1787 |
#ifdef USE_CODE_COPY
|
1788 |
"-no-code-copy disable code copy acceleration\n"
|
1789 |
#endif
|
1790 |
|
1791 |
"\n"
|
1792 |
"During emulation, use C-a h to get terminal commands:\n",
|
1793 |
#ifdef CONFIG_SOFTMMU
|
1794 |
"qemu",
|
1795 |
#else
|
1796 |
"qemu-fast",
|
1797 |
#endif
|
1798 |
DEFAULT_NETWORK_SCRIPT, |
1799 |
DEFAULT_GDBSTUB_PORT, |
1800 |
"/tmp/qemu.log");
|
1801 |
term_print_help(); |
1802 |
#ifndef CONFIG_SOFTMMU
|
1803 |
printf("\n"
|
1804 |
"NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
|
1805 |
"work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
|
1806 |
"PC emulation.\n");
|
1807 |
#endif
|
1808 |
exit(1);
|
1809 |
} |
1810 |
|
1811 |
struct option long_options[] = {
|
1812 |
{ "initrd", 1, NULL, 0, }, |
1813 |
{ "hda", 1, NULL, 0, }, |
1814 |
{ "hdb", 1, NULL, 0, }, |
1815 |
{ "snapshot", 0, NULL, 0, }, |
1816 |
{ "hdachs", 1, NULL, 0, }, |
1817 |
{ "nographic", 0, NULL, 0, }, |
1818 |
{ "kernel", 1, NULL, 0, }, |
1819 |
{ "append", 1, NULL, 0, }, |
1820 |
{ "tun-fd", 1, NULL, 0, }, |
1821 |
{ "hdc", 1, NULL, 0, }, |
1822 |
{ "hdd", 1, NULL, 0, }, |
1823 |
{ "cdrom", 1, NULL, 0, }, |
1824 |
{ "boot", 1, NULL, 0, }, |
1825 |
{ "fda", 1, NULL, 0, }, |
1826 |
{ "fdb", 1, NULL, 0, }, |
1827 |
{ "no-code-copy", 0, NULL, 0 }, |
1828 |
{ "nics", 1, NULL, 0 }, |
1829 |
{ "macaddr", 1, NULL, 0 }, |
1830 |
{ "user-net", 0, NULL, 0 }, |
1831 |
{ "dummy-net", 0, NULL, 0 }, |
1832 |
{ NULL, 0, NULL, 0 }, |
1833 |
}; |
1834 |
|
1835 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
1836 |
|
1837 |
/* this stack is only used during signal handling */
|
1838 |
#define SIGNAL_STACK_SIZE 32768 |
1839 |
|
1840 |
static uint8_t *signal_stack;
|
1841 |
|
1842 |
#endif
|
1843 |
|
1844 |
#define NET_IF_TUN 0 |
1845 |
#define NET_IF_USER 1 |
1846 |
#define NET_IF_DUMMY 2 |
1847 |
|
1848 |
int main(int argc, char **argv) |
1849 |
{ |
1850 |
#ifdef CONFIG_GDBSTUB
|
1851 |
int use_gdbstub, gdbstub_port;
|
1852 |
#endif
|
1853 |
int c, i, long_index, has_cdrom;
|
1854 |
int snapshot, linux_boot;
|
1855 |
CPUState *env; |
1856 |
const char *initrd_filename; |
1857 |
const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD]; |
1858 |
const char *kernel_filename, *kernel_cmdline; |
1859 |
DisplayState *ds = &display_state; |
1860 |
int cyls, heads, secs;
|
1861 |
int start_emulation = 1; |
1862 |
uint8_t macaddr[6];
|
1863 |
int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
|
1864 |
|
1865 |
#if !defined(CONFIG_SOFTMMU)
|
1866 |
/* we never want that malloc() uses mmap() */
|
1867 |
mallopt(M_MMAP_THRESHOLD, 4096 * 1024); |
1868 |
#endif
|
1869 |
initrd_filename = NULL;
|
1870 |
for(i = 0; i < MAX_FD; i++) |
1871 |
fd_filename[i] = NULL;
|
1872 |
for(i = 0; i < MAX_DISKS; i++) |
1873 |
hd_filename[i] = NULL;
|
1874 |
ram_size = 32 * 1024 * 1024; |
1875 |
vga_ram_size = VGA_RAM_SIZE; |
1876 |
pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
|
1877 |
#ifdef CONFIG_GDBSTUB
|
1878 |
use_gdbstub = 0;
|
1879 |
gdbstub_port = DEFAULT_GDBSTUB_PORT; |
1880 |
#endif
|
1881 |
snapshot = 0;
|
1882 |
nographic = 0;
|
1883 |
kernel_filename = NULL;
|
1884 |
kernel_cmdline = "";
|
1885 |
has_cdrom = 1;
|
1886 |
cyls = heads = secs = 0;
|
1887 |
|
1888 |
nb_tun_fds = 0;
|
1889 |
net_if_type = -1;
|
1890 |
nb_nics = 1;
|
1891 |
/* default mac address of the first network interface */
|
1892 |
macaddr[0] = 0x52; |
1893 |
macaddr[1] = 0x54; |
1894 |
macaddr[2] = 0x00; |
1895 |
macaddr[3] = 0x12; |
1896 |
macaddr[4] = 0x34; |
1897 |
macaddr[5] = 0x56; |
1898 |
|
1899 |
|
1900 |
for(;;) {
|
1901 |
c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index);
|
1902 |
if (c == -1) |
1903 |
break;
|
1904 |
switch(c) {
|
1905 |
case 0: |
1906 |
switch(long_index) {
|
1907 |
case 0: |
1908 |
initrd_filename = optarg; |
1909 |
break;
|
1910 |
case 1: |
1911 |
hd_filename[0] = optarg;
|
1912 |
break;
|
1913 |
case 2: |
1914 |
hd_filename[1] = optarg;
|
1915 |
break;
|
1916 |
case 3: |
1917 |
snapshot = 1;
|
1918 |
break;
|
1919 |
case 4: |
1920 |
{ |
1921 |
const char *p; |
1922 |
p = optarg; |
1923 |
cyls = strtol(p, (char **)&p, 0); |
1924 |
if (*p != ',') |
1925 |
goto chs_fail;
|
1926 |
p++; |
1927 |
heads = strtol(p, (char **)&p, 0); |
1928 |
if (*p != ',') |
1929 |
goto chs_fail;
|
1930 |
p++; |
1931 |
secs = strtol(p, (char **)&p, 0); |
1932 |
if (*p != '\0') { |
1933 |
chs_fail:
|
1934 |
cyls = 0;
|
1935 |
} |
1936 |
} |
1937 |
break;
|
1938 |
case 5: |
1939 |
nographic = 1;
|
1940 |
break;
|
1941 |
case 6: |
1942 |
kernel_filename = optarg; |
1943 |
break;
|
1944 |
case 7: |
1945 |
kernel_cmdline = optarg; |
1946 |
break;
|
1947 |
case 8: |
1948 |
{ |
1949 |
const char *p; |
1950 |
int fd;
|
1951 |
if (nb_tun_fds < MAX_NICS) {
|
1952 |
fd = strtol(optarg, (char **)&p, 0); |
1953 |
if (*p != '\0') { |
1954 |
fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
|
1955 |
exit(1);
|
1956 |
} |
1957 |
tun_fds[nb_tun_fds++] = fd; |
1958 |
} |
1959 |
} |
1960 |
break;
|
1961 |
case 9: |
1962 |
hd_filename[2] = optarg;
|
1963 |
has_cdrom = 0;
|
1964 |
break;
|
1965 |
case 10: |
1966 |
hd_filename[3] = optarg;
|
1967 |
break;
|
1968 |
case 11: |
1969 |
hd_filename[2] = optarg;
|
1970 |
has_cdrom = 1;
|
1971 |
break;
|
1972 |
case 12: |
1973 |
boot_device = optarg[0];
|
1974 |
if (boot_device != 'a' && boot_device != 'b' && |
1975 |
boot_device != 'c' && boot_device != 'd') { |
1976 |
fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
|
1977 |
exit(1);
|
1978 |
} |
1979 |
break;
|
1980 |
case 13: |
1981 |
fd_filename[0] = optarg;
|
1982 |
break;
|
1983 |
case 14: |
1984 |
fd_filename[1] = optarg;
|
1985 |
break;
|
1986 |
case 15: |
1987 |
code_copy_enabled = 0;
|
1988 |
break;
|
1989 |
case 16: |
1990 |
nb_nics = atoi(optarg); |
1991 |
if (nb_nics < 1 || nb_nics > MAX_NICS) { |
1992 |
fprintf(stderr, "qemu: invalid number of network interfaces\n");
|
1993 |
exit(1);
|
1994 |
} |
1995 |
break;
|
1996 |
case 17: |
1997 |
{ |
1998 |
const char *p; |
1999 |
int i;
|
2000 |
p = optarg; |
2001 |
for(i = 0; i < 6; i++) { |
2002 |
macaddr[i] = strtol(p, (char **)&p, 16); |
2003 |
if (i == 5) { |
2004 |
if (*p != '\0') |
2005 |
goto macaddr_error;
|
2006 |
} else {
|
2007 |
if (*p != ':') { |
2008 |
macaddr_error:
|
2009 |
fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
|
2010 |
exit(1);
|
2011 |
} |
2012 |
p++; |
2013 |
} |
2014 |
} |
2015 |
} |
2016 |
break;
|
2017 |
case 18: |
2018 |
net_if_type = NET_IF_USER; |
2019 |
break;
|
2020 |
case 19: |
2021 |
net_if_type = NET_IF_DUMMY; |
2022 |
break;
|
2023 |
} |
2024 |
break;
|
2025 |
case 'h': |
2026 |
help(); |
2027 |
break;
|
2028 |
case 'm': |
2029 |
ram_size = atoi(optarg) * 1024 * 1024; |
2030 |
if (ram_size <= 0) |
2031 |
help(); |
2032 |
if (ram_size > PHYS_RAM_MAX_SIZE) {
|
2033 |
fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
|
2034 |
PHYS_RAM_MAX_SIZE / (1024 * 1024)); |
2035 |
exit(1);
|
2036 |
} |
2037 |
break;
|
2038 |
case 'd': |
2039 |
{ |
2040 |
int mask;
|
2041 |
CPULogItem *item; |
2042 |
|
2043 |
mask = cpu_str_to_log_mask(optarg); |
2044 |
if (!mask) {
|
2045 |
printf("Log items (comma separated):\n");
|
2046 |
for(item = cpu_log_items; item->mask != 0; item++) { |
2047 |
printf("%-10s %s\n", item->name, item->help);
|
2048 |
} |
2049 |
exit(1);
|
2050 |
} |
2051 |
cpu_set_log(mask); |
2052 |
} |
2053 |
break;
|
2054 |
case 'n': |
2055 |
pstrcpy(network_script, sizeof(network_script), optarg);
|
2056 |
break;
|
2057 |
#ifdef CONFIG_GDBSTUB
|
2058 |
case 's': |
2059 |
use_gdbstub = 1;
|
2060 |
break;
|
2061 |
case 'p': |
2062 |
gdbstub_port = atoi(optarg); |
2063 |
break;
|
2064 |
#endif
|
2065 |
case 'L': |
2066 |
bios_dir = optarg; |
2067 |
break;
|
2068 |
case 'S': |
2069 |
start_emulation = 0;
|
2070 |
break;
|
2071 |
} |
2072 |
} |
2073 |
|
2074 |
if (optind < argc) {
|
2075 |
hd_filename[0] = argv[optind++];
|
2076 |
} |
2077 |
|
2078 |
linux_boot = (kernel_filename != NULL);
|
2079 |
|
2080 |
if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' && |
2081 |
fd_filename[0] == '\0') |
2082 |
help(); |
2083 |
|
2084 |
/* boot to cd by default if no hard disk */
|
2085 |
if (hd_filename[0] == '\0' && boot_device == 'c') { |
2086 |
if (fd_filename[0] != '\0') |
2087 |
boot_device = 'a';
|
2088 |
else
|
2089 |
boot_device = 'd';
|
2090 |
} |
2091 |
|
2092 |
#if !defined(CONFIG_SOFTMMU)
|
2093 |
/* must avoid mmap() usage of glibc by setting a buffer "by hand" */
|
2094 |
{ |
2095 |
static uint8_t stdout_buf[4096]; |
2096 |
setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
|
2097 |
} |
2098 |
#else
|
2099 |
setvbuf(stdout, NULL, _IOLBF, 0); |
2100 |
#endif
|
2101 |
|
2102 |
/* init host network redirectors */
|
2103 |
if (net_if_type == -1) { |
2104 |
net_if_type = NET_IF_TUN; |
2105 |
#if defined(CONFIG_SLIRP)
|
2106 |
if (access(network_script, R_OK) < 0) { |
2107 |
net_if_type = NET_IF_USER; |
2108 |
} |
2109 |
#endif
|
2110 |
} |
2111 |
|
2112 |
for(i = 0; i < nb_nics; i++) { |
2113 |
NetDriverState *nd = &nd_table[i]; |
2114 |
nd->index = i; |
2115 |
/* init virtual mac address */
|
2116 |
nd->macaddr[0] = macaddr[0]; |
2117 |
nd->macaddr[1] = macaddr[1]; |
2118 |
nd->macaddr[2] = macaddr[2]; |
2119 |
nd->macaddr[3] = macaddr[3]; |
2120 |
nd->macaddr[4] = macaddr[4]; |
2121 |
nd->macaddr[5] = macaddr[5] + i; |
2122 |
switch(net_if_type) {
|
2123 |
#if defined(CONFIG_SLIRP)
|
2124 |
case NET_IF_USER:
|
2125 |
net_slirp_init(nd); |
2126 |
break;
|
2127 |
#endif
|
2128 |
#if !defined(_WIN32)
|
2129 |
case NET_IF_TUN:
|
2130 |
if (i < nb_tun_fds) {
|
2131 |
net_fd_init(nd, tun_fds[i]); |
2132 |
} else {
|
2133 |
net_tun_init(nd); |
2134 |
} |
2135 |
break;
|
2136 |
#endif
|
2137 |
case NET_IF_DUMMY:
|
2138 |
default:
|
2139 |
net_dummy_init(nd); |
2140 |
break;
|
2141 |
} |
2142 |
} |
2143 |
|
2144 |
/* init the memory */
|
2145 |
phys_ram_size = ram_size + vga_ram_size; |
2146 |
|
2147 |
#ifdef CONFIG_SOFTMMU
|
2148 |
phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size); |
2149 |
if (!phys_ram_base) {
|
2150 |
fprintf(stderr, "Could not allocate physical memory\n");
|
2151 |
exit(1);
|
2152 |
} |
2153 |
#else
|
2154 |
/* as we must map the same page at several addresses, we must use
|
2155 |
a fd */
|
2156 |
{ |
2157 |
const char *tmpdir; |
2158 |
|
2159 |
tmpdir = getenv("QEMU_TMPDIR");
|
2160 |
if (!tmpdir)
|
2161 |
tmpdir = "/tmp";
|
2162 |
snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir); |
2163 |
if (mkstemp(phys_ram_file) < 0) { |
2164 |
fprintf(stderr, "Could not create temporary memory file '%s'\n",
|
2165 |
phys_ram_file); |
2166 |
exit(1);
|
2167 |
} |
2168 |
phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
|
2169 |
if (phys_ram_fd < 0) { |
2170 |
fprintf(stderr, "Could not open temporary memory file '%s'\n",
|
2171 |
phys_ram_file); |
2172 |
exit(1);
|
2173 |
} |
2174 |
ftruncate(phys_ram_fd, phys_ram_size); |
2175 |
unlink(phys_ram_file); |
2176 |
phys_ram_base = mmap(get_mmap_addr(phys_ram_size), |
2177 |
phys_ram_size, |
2178 |
PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED, |
2179 |
phys_ram_fd, 0);
|
2180 |
if (phys_ram_base == MAP_FAILED) {
|
2181 |
fprintf(stderr, "Could not map physical memory\n");
|
2182 |
exit(1);
|
2183 |
} |
2184 |
} |
2185 |
#endif
|
2186 |
|
2187 |
/* we always create the cdrom drive, even if no disk is there */
|
2188 |
if (has_cdrom) {
|
2189 |
bs_table[2] = bdrv_new("cdrom"); |
2190 |
bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
|
2191 |
} |
2192 |
|
2193 |
/* open the virtual block devices */
|
2194 |
for(i = 0; i < MAX_DISKS; i++) { |
2195 |
if (hd_filename[i]) {
|
2196 |
if (!bs_table[i]) {
|
2197 |
char buf[64]; |
2198 |
snprintf(buf, sizeof(buf), "hd%c", i + 'a'); |
2199 |
bs_table[i] = bdrv_new(buf); |
2200 |
} |
2201 |
if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) { |
2202 |
fprintf(stderr, "qemu: could not open hard disk image '%s\n",
|
2203 |
hd_filename[i]); |
2204 |
exit(1);
|
2205 |
} |
2206 |
if (i == 0 && cyls != 0) |
2207 |
bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs); |
2208 |
} |
2209 |
} |
2210 |
|
2211 |
/* we always create at least one floppy disk */
|
2212 |
fd_table[0] = bdrv_new("fda"); |
2213 |
bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
|
2214 |
|
2215 |
for(i = 0; i < MAX_FD; i++) { |
2216 |
if (fd_filename[i]) {
|
2217 |
if (!fd_table[i]) {
|
2218 |
char buf[64]; |
2219 |
snprintf(buf, sizeof(buf), "fd%c", i + 'a'); |
2220 |
fd_table[i] = bdrv_new(buf); |
2221 |
bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY); |
2222 |
} |
2223 |
if (fd_filename[i] != '\0') { |
2224 |
if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) { |
2225 |
fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
|
2226 |
fd_filename[i]); |
2227 |
exit(1);
|
2228 |
} |
2229 |
} |
2230 |
} |
2231 |
} |
2232 |
|
2233 |
/* init CPU state */
|
2234 |
env = cpu_init(); |
2235 |
global_env = env; |
2236 |
cpu_single_env = env; |
2237 |
|
2238 |
register_savevm("timer", 0, 1, timer_save, timer_load, env); |
2239 |
register_savevm("cpu", 0, 1, cpu_save, cpu_load, env); |
2240 |
register_savevm("ram", 0, 1, ram_save, ram_load, NULL); |
2241 |
|
2242 |
init_ioports(); |
2243 |
cpu_calibrate_ticks(); |
2244 |
|
2245 |
/* terminal init */
|
2246 |
if (nographic) {
|
2247 |
dumb_display_init(ds); |
2248 |
} else {
|
2249 |
#ifdef CONFIG_SDL
|
2250 |
sdl_display_init(ds); |
2251 |
#else
|
2252 |
dumb_display_init(ds); |
2253 |
#endif
|
2254 |
} |
2255 |
|
2256 |
/* setup cpu signal handlers for MMU / self modifying code handling */
|
2257 |
#if !defined(CONFIG_SOFTMMU)
|
2258 |
|
2259 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
2260 |
{ |
2261 |
stack_t stk; |
2262 |
signal_stack = memalign(16, SIGNAL_STACK_SIZE);
|
2263 |
stk.ss_sp = signal_stack; |
2264 |
stk.ss_size = SIGNAL_STACK_SIZE; |
2265 |
stk.ss_flags = 0;
|
2266 |
|
2267 |
if (sigaltstack(&stk, NULL) < 0) { |
2268 |
perror("sigaltstack");
|
2269 |
exit(1);
|
2270 |
} |
2271 |
} |
2272 |
#endif
|
2273 |
{ |
2274 |
struct sigaction act;
|
2275 |
|
2276 |
sigfillset(&act.sa_mask); |
2277 |
act.sa_flags = SA_SIGINFO; |
2278 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
2279 |
act.sa_flags |= SA_ONSTACK; |
2280 |
#endif
|
2281 |
act.sa_sigaction = host_segv_handler; |
2282 |
sigaction(SIGSEGV, &act, NULL);
|
2283 |
sigaction(SIGBUS, &act, NULL);
|
2284 |
#if defined (TARGET_I386) && defined(USE_CODE_COPY)
|
2285 |
sigaction(SIGFPE, &act, NULL);
|
2286 |
#endif
|
2287 |
} |
2288 |
#endif
|
2289 |
|
2290 |
#ifndef _WIN32
|
2291 |
{ |
2292 |
struct sigaction act;
|
2293 |
sigfillset(&act.sa_mask); |
2294 |
act.sa_flags = 0;
|
2295 |
act.sa_handler = SIG_IGN; |
2296 |
sigaction(SIGPIPE, &act, NULL);
|
2297 |
} |
2298 |
#endif
|
2299 |
init_timers(); |
2300 |
|
2301 |
#if defined(TARGET_I386)
|
2302 |
pc_init(ram_size, vga_ram_size, boot_device, |
2303 |
ds, fd_filename, snapshot, |
2304 |
kernel_filename, kernel_cmdline, initrd_filename); |
2305 |
#elif defined(TARGET_PPC)
|
2306 |
ppc_init(ram_size, vga_ram_size, boot_device, |
2307 |
ds, fd_filename, snapshot, |
2308 |
kernel_filename, kernel_cmdline, initrd_filename); |
2309 |
#endif
|
2310 |
|
2311 |
/* launched after the device init so that it can display or not a
|
2312 |
banner */
|
2313 |
monitor_init(); |
2314 |
|
2315 |
gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
|
2316 |
qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock)); |
2317 |
|
2318 |
#ifdef CONFIG_GDBSTUB
|
2319 |
if (use_gdbstub) {
|
2320 |
if (gdbserver_start(gdbstub_port) < 0) { |
2321 |
fprintf(stderr, "Could not open gdbserver socket on port %d\n",
|
2322 |
gdbstub_port); |
2323 |
exit(1);
|
2324 |
} else {
|
2325 |
printf("Waiting gdb connection on port %d\n", gdbstub_port);
|
2326 |
} |
2327 |
} else
|
2328 |
#endif
|
2329 |
if (start_emulation)
|
2330 |
{ |
2331 |
vm_start(); |
2332 |
} |
2333 |
term_init(); |
2334 |
main_loop(); |
2335 |
quit_timers(); |
2336 |
return 0; |
2337 |
} |