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