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