partial big endian fixes - change VESA VBE ports for non i386 targets to avoid unalig...
[qemu.git] / vl.c
blob6faf19f0e0acbc70aecdccf35cbe568dde80a207
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "vl.h"
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>
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
53 #if defined(CONFIG_SLIRP)
54 #include "libslirp.h"
55 #endif
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
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 */
79 #include "disas.h"
81 #include "exec-all.h"
83 //#define DO_TB_FLUSH
85 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
87 //#define DEBUG_UNUSED_IOPORT
88 //#define DEBUG_IOPORT
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
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
104 /* XXX: use a two level table to limit memory usage */
105 #define MAX_IOPORTS 65536
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 = 0;
131 int prep_enabled = 0;
133 /***********************************************************/
134 /* x86 ISA bus support */
136 target_phys_addr_t isa_mem_base = 0;
138 uint32_t default_ioport_readb(void *opaque, uint32_t address)
140 #ifdef DEBUG_UNUSED_IOPORT
141 fprintf(stderr, "inb: port=0x%04x\n", address);
142 #endif
143 return 0xff;
146 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
148 #ifdef DEBUG_UNUSED_IOPORT
149 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
150 #endif
153 /* default is to make two byte accesses */
154 uint32_t default_ioport_readw(void *opaque, uint32_t address)
156 uint32_t data;
157 data = ioport_read_table[0][address](ioport_opaque[address], address);
158 address = (address + 1) & (MAX_IOPORTS - 1);
159 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
160 return data;
163 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
165 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
166 address = (address + 1) & (MAX_IOPORTS - 1);
167 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
170 uint32_t default_ioport_readl(void *opaque, uint32_t address)
172 #ifdef DEBUG_UNUSED_IOPORT
173 fprintf(stderr, "inl: port=0x%04x\n", address);
174 #endif
175 return 0xffffffff;
178 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
180 #ifdef DEBUG_UNUSED_IOPORT
181 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
182 #endif
185 void init_ioports(void)
187 int i;
189 for(i = 0; i < MAX_IOPORTS; i++) {
190 ioport_read_table[0][i] = default_ioport_readb;
191 ioport_write_table[0][i] = default_ioport_writeb;
192 ioport_read_table[1][i] = default_ioport_readw;
193 ioport_write_table[1][i] = default_ioport_writew;
194 ioport_read_table[2][i] = default_ioport_readl;
195 ioport_write_table[2][i] = default_ioport_writel;
199 /* size is the word size in byte */
200 int register_ioport_read(int start, int length, int size,
201 IOPortReadFunc *func, void *opaque)
203 int i, bsize;
205 if (size == 1) {
206 bsize = 0;
207 } else if (size == 2) {
208 bsize = 1;
209 } else if (size == 4) {
210 bsize = 2;
211 } else {
212 hw_error("register_ioport_read: invalid size");
213 return -1;
215 for(i = start; i < start + length; i += size) {
216 ioport_read_table[bsize][i] = func;
217 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
218 hw_error("register_ioport_read: invalid opaque");
219 ioport_opaque[i] = opaque;
221 return 0;
224 /* size is the word size in byte */
225 int register_ioport_write(int start, int length, int size,
226 IOPortWriteFunc *func, void *opaque)
228 int i, bsize;
230 if (size == 1) {
231 bsize = 0;
232 } else if (size == 2) {
233 bsize = 1;
234 } else if (size == 4) {
235 bsize = 2;
236 } else {
237 hw_error("register_ioport_write: invalid size");
238 return -1;
240 for(i = start; i < start + length; i += size) {
241 ioport_write_table[bsize][i] = func;
242 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
243 hw_error("register_ioport_read: invalid opaque");
244 ioport_opaque[i] = opaque;
246 return 0;
249 void isa_unassign_ioport(int start, int length)
251 int i;
253 for(i = start; i < start + length; i++) {
254 ioport_read_table[0][i] = default_ioport_readb;
255 ioport_read_table[1][i] = default_ioport_readw;
256 ioport_read_table[2][i] = default_ioport_readl;
258 ioport_write_table[0][i] = default_ioport_writeb;
259 ioport_write_table[1][i] = default_ioport_writew;
260 ioport_write_table[2][i] = default_ioport_writel;
264 void pstrcpy(char *buf, int buf_size, const char *str)
266 int c;
267 char *q = buf;
269 if (buf_size <= 0)
270 return;
272 for(;;) {
273 c = *str++;
274 if (c == 0 || q >= buf + buf_size - 1)
275 break;
276 *q++ = c;
278 *q = '\0';
281 /* strcat and truncate. */
282 char *pstrcat(char *buf, int buf_size, const char *s)
284 int len;
285 len = strlen(buf);
286 if (len < buf_size)
287 pstrcpy(buf + len, buf_size - len, s);
288 return buf;
291 /* return the size or -1 if error */
292 int load_image(const char *filename, uint8_t *addr)
294 int fd, size;
295 fd = open(filename, O_RDONLY | O_BINARY);
296 if (fd < 0)
297 return -1;
298 size = lseek(fd, 0, SEEK_END);
299 lseek(fd, 0, SEEK_SET);
300 if (read(fd, addr, size) != size) {
301 close(fd);
302 return -1;
304 close(fd);
305 return size;
308 void cpu_outb(CPUState *env, int addr, int val)
310 #ifdef DEBUG_IOPORT
311 if (loglevel & CPU_LOG_IOPORT)
312 fprintf(logfile, "outb: %04x %02x\n", addr, val);
313 #endif
314 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
317 void cpu_outw(CPUState *env, int addr, int val)
319 #ifdef DEBUG_IOPORT
320 if (loglevel & CPU_LOG_IOPORT)
321 fprintf(logfile, "outw: %04x %04x\n", addr, val);
322 #endif
323 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
326 void cpu_outl(CPUState *env, int addr, int val)
328 #ifdef DEBUG_IOPORT
329 if (loglevel & CPU_LOG_IOPORT)
330 fprintf(logfile, "outl: %04x %08x\n", addr, val);
331 #endif
332 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
335 int cpu_inb(CPUState *env, int addr)
337 int val;
338 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
339 #ifdef DEBUG_IOPORT
340 if (loglevel & CPU_LOG_IOPORT)
341 fprintf(logfile, "inb : %04x %02x\n", addr, val);
342 #endif
343 return val;
346 int cpu_inw(CPUState *env, int addr)
348 int val;
349 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
350 #ifdef DEBUG_IOPORT
351 if (loglevel & CPU_LOG_IOPORT)
352 fprintf(logfile, "inw : %04x %04x\n", addr, val);
353 #endif
354 return val;
357 int cpu_inl(CPUState *env, int addr)
359 int val;
360 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
361 #ifdef DEBUG_IOPORT
362 if (loglevel & CPU_LOG_IOPORT)
363 fprintf(logfile, "inl : %04x %08x\n", addr, val);
364 #endif
365 return val;
368 /***********************************************************/
369 void hw_error(const char *fmt, ...)
371 va_list ap;
373 va_start(ap, fmt);
374 fprintf(stderr, "qemu: hardware error: ");
375 vfprintf(stderr, fmt, ap);
376 fprintf(stderr, "\n");
377 #ifdef TARGET_I386
378 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
379 #else
380 cpu_dump_state(global_env, stderr, 0);
381 #endif
382 va_end(ap);
383 abort();
386 /***********************************************************/
387 /* timers */
389 #if defined(__powerpc__)
391 static inline uint32_t get_tbl(void)
393 uint32_t tbl;
394 asm volatile("mftb %0" : "=r" (tbl));
395 return tbl;
398 static inline uint32_t get_tbu(void)
400 uint32_t tbl;
401 asm volatile("mftbu %0" : "=r" (tbl));
402 return tbl;
405 int64_t cpu_get_real_ticks(void)
407 uint32_t l, h, h1;
408 /* NOTE: we test if wrapping has occurred */
409 do {
410 h = get_tbu();
411 l = get_tbl();
412 h1 = get_tbu();
413 } while (h != h1);
414 return ((int64_t)h << 32) | l;
417 #elif defined(__i386__)
419 int64_t cpu_get_real_ticks(void)
421 int64_t val;
422 asm volatile ("rdtsc" : "=A" (val));
423 return val;
426 #elif defined(__x86_64__)
428 int64_t cpu_get_real_ticks(void)
430 uint32_t low,high;
431 int64_t val;
432 asm volatile("rdtsc" : "=a" (low), "=d" (high));
433 val = high;
434 val <<= 32;
435 val |= low;
436 return val;
439 #else
440 #error unsupported CPU
441 #endif
443 static int64_t cpu_ticks_offset;
444 static int cpu_ticks_enabled;
446 static inline int64_t cpu_get_ticks(void)
448 if (!cpu_ticks_enabled) {
449 return cpu_ticks_offset;
450 } else {
451 return cpu_get_real_ticks() + cpu_ticks_offset;
455 /* enable cpu_get_ticks() */
456 void cpu_enable_ticks(void)
458 if (!cpu_ticks_enabled) {
459 cpu_ticks_offset -= cpu_get_real_ticks();
460 cpu_ticks_enabled = 1;
464 /* disable cpu_get_ticks() : the clock is stopped. You must not call
465 cpu_get_ticks() after that. */
466 void cpu_disable_ticks(void)
468 if (cpu_ticks_enabled) {
469 cpu_ticks_offset = cpu_get_ticks();
470 cpu_ticks_enabled = 0;
474 static int64_t get_clock(void)
476 #ifdef _WIN32
477 struct _timeb tb;
478 _ftime(&tb);
479 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
480 #else
481 struct timeval tv;
482 gettimeofday(&tv, NULL);
483 return tv.tv_sec * 1000000LL + tv.tv_usec;
484 #endif
487 void cpu_calibrate_ticks(void)
489 int64_t usec, ticks;
491 usec = get_clock();
492 ticks = cpu_get_real_ticks();
493 #ifdef _WIN32
494 Sleep(50);
495 #else
496 usleep(50 * 1000);
497 #endif
498 usec = get_clock() - usec;
499 ticks = cpu_get_real_ticks() - ticks;
500 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
503 /* compute with 96 bit intermediate result: (a*b)/c */
504 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
506 union {
507 uint64_t ll;
508 struct {
509 #ifdef WORDS_BIGENDIAN
510 uint32_t high, low;
511 #else
512 uint32_t low, high;
513 #endif
514 } l;
515 } u, res;
516 uint64_t rl, rh;
518 u.ll = a;
519 rl = (uint64_t)u.l.low * (uint64_t)b;
520 rh = (uint64_t)u.l.high * (uint64_t)b;
521 rh += (rl >> 32);
522 res.l.high = rh / c;
523 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
524 return res.ll;
527 #define QEMU_TIMER_REALTIME 0
528 #define QEMU_TIMER_VIRTUAL 1
530 struct QEMUClock {
531 int type;
532 /* XXX: add frequency */
535 struct QEMUTimer {
536 QEMUClock *clock;
537 int64_t expire_time;
538 QEMUTimerCB *cb;
539 void *opaque;
540 struct QEMUTimer *next;
543 QEMUClock *rt_clock;
544 QEMUClock *vm_clock;
546 static QEMUTimer *active_timers[2];
547 #ifdef _WIN32
548 static MMRESULT timerID;
549 #else
550 /* frequency of the times() clock tick */
551 static int timer_freq;
552 #endif
554 QEMUClock *qemu_new_clock(int type)
556 QEMUClock *clock;
557 clock = qemu_mallocz(sizeof(QEMUClock));
558 if (!clock)
559 return NULL;
560 clock->type = type;
561 return clock;
564 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
566 QEMUTimer *ts;
568 ts = qemu_mallocz(sizeof(QEMUTimer));
569 ts->clock = clock;
570 ts->cb = cb;
571 ts->opaque = opaque;
572 return ts;
575 void qemu_free_timer(QEMUTimer *ts)
577 qemu_free(ts);
580 /* stop a timer, but do not dealloc it */
581 void qemu_del_timer(QEMUTimer *ts)
583 QEMUTimer **pt, *t;
585 /* NOTE: this code must be signal safe because
586 qemu_timer_expired() can be called from a signal. */
587 pt = &active_timers[ts->clock->type];
588 for(;;) {
589 t = *pt;
590 if (!t)
591 break;
592 if (t == ts) {
593 *pt = t->next;
594 break;
596 pt = &t->next;
600 /* modify the current timer so that it will be fired when current_time
601 >= expire_time. The corresponding callback will be called. */
602 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
604 QEMUTimer **pt, *t;
606 qemu_del_timer(ts);
608 /* add the timer in the sorted list */
609 /* NOTE: this code must be signal safe because
610 qemu_timer_expired() can be called from a signal. */
611 pt = &active_timers[ts->clock->type];
612 for(;;) {
613 t = *pt;
614 if (!t)
615 break;
616 if (t->expire_time > expire_time)
617 break;
618 pt = &t->next;
620 ts->expire_time = expire_time;
621 ts->next = *pt;
622 *pt = ts;
625 int qemu_timer_pending(QEMUTimer *ts)
627 QEMUTimer *t;
628 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
629 if (t == ts)
630 return 1;
632 return 0;
635 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
637 if (!timer_head)
638 return 0;
639 return (timer_head->expire_time <= current_time);
642 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
644 QEMUTimer *ts;
646 for(;;) {
647 ts = *ptimer_head;
648 if (ts->expire_time > current_time)
649 break;
650 /* remove timer from the list before calling the callback */
651 *ptimer_head = ts->next;
652 ts->next = NULL;
654 /* run the callback (the timer list can be modified) */
655 ts->cb(ts->opaque);
659 int64_t qemu_get_clock(QEMUClock *clock)
661 switch(clock->type) {
662 case QEMU_TIMER_REALTIME:
663 #ifdef _WIN32
664 return GetTickCount();
665 #else
667 struct tms tp;
669 /* Note that using gettimeofday() is not a good solution
670 for timers because its value change when the date is
671 modified. */
672 if (timer_freq == 100) {
673 return times(&tp) * 10;
674 } else {
675 return ((int64_t)times(&tp) * 1000) / timer_freq;
678 #endif
679 default:
680 case QEMU_TIMER_VIRTUAL:
681 return cpu_get_ticks();
685 /* save a timer */
686 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
688 uint64_t expire_time;
690 if (qemu_timer_pending(ts)) {
691 expire_time = ts->expire_time;
692 } else {
693 expire_time = -1;
695 qemu_put_be64(f, expire_time);
698 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
700 uint64_t expire_time;
702 expire_time = qemu_get_be64(f);
703 if (expire_time != -1) {
704 qemu_mod_timer(ts, expire_time);
705 } else {
706 qemu_del_timer(ts);
710 static void timer_save(QEMUFile *f, void *opaque)
712 if (cpu_ticks_enabled) {
713 hw_error("cannot save state if virtual timers are running");
715 qemu_put_be64s(f, &cpu_ticks_offset);
716 qemu_put_be64s(f, &ticks_per_sec);
719 static int timer_load(QEMUFile *f, void *opaque, int version_id)
721 if (version_id != 1)
722 return -EINVAL;
723 if (cpu_ticks_enabled) {
724 return -EINVAL;
726 qemu_get_be64s(f, &cpu_ticks_offset);
727 qemu_get_be64s(f, &ticks_per_sec);
728 return 0;
731 #ifdef _WIN32
732 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
733 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
734 #else
735 static void host_alarm_handler(int host_signum)
736 #endif
738 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
739 qemu_get_clock(vm_clock)) ||
740 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
741 qemu_get_clock(rt_clock))) {
742 /* stop the cpu because a timer occured */
743 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
747 #ifndef _WIN32
749 #if defined(__linux__)
751 #define RTC_FREQ 1024
753 static int rtc_fd;
755 static int start_rtc_timer(void)
757 rtc_fd = open("/dev/rtc", O_RDONLY);
758 if (rtc_fd < 0)
759 return -1;
760 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
761 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
762 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
763 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
764 goto fail;
766 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
767 fail:
768 close(rtc_fd);
769 return -1;
771 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
772 return 0;
775 #else
777 static int start_rtc_timer(void)
779 return -1;
782 #endif /* !defined(__linux__) */
784 #endif /* !defined(_WIN32) */
786 static void init_timers(void)
788 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
789 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
791 #ifdef _WIN32
793 int count=0;
794 timerID = timeSetEvent(10, // interval (ms)
795 0, // resolution
796 host_alarm_handler, // function
797 (DWORD)&count, // user parameter
798 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
799 if( !timerID ) {
800 perror("failed timer alarm");
801 exit(1);
804 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
805 #else
807 struct sigaction act;
808 struct itimerval itv;
810 /* get times() syscall frequency */
811 timer_freq = sysconf(_SC_CLK_TCK);
813 /* timer signal */
814 sigfillset(&act.sa_mask);
815 act.sa_flags = 0;
816 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
817 act.sa_flags |= SA_ONSTACK;
818 #endif
819 act.sa_handler = host_alarm_handler;
820 sigaction(SIGALRM, &act, NULL);
822 itv.it_interval.tv_sec = 0;
823 itv.it_interval.tv_usec = 1000;
824 itv.it_value.tv_sec = 0;
825 itv.it_value.tv_usec = 10 * 1000;
826 setitimer(ITIMER_REAL, &itv, NULL);
827 /* we probe the tick duration of the kernel to inform the user if
828 the emulated kernel requested a too high timer frequency */
829 getitimer(ITIMER_REAL, &itv);
831 if (itv.it_interval.tv_usec > 1000) {
832 /* try to use /dev/rtc to have a faster timer */
833 if (start_rtc_timer() < 0)
834 goto use_itimer;
835 /* disable itimer */
836 itv.it_interval.tv_sec = 0;
837 itv.it_interval.tv_usec = 0;
838 itv.it_value.tv_sec = 0;
839 itv.it_value.tv_usec = 0;
840 setitimer(ITIMER_REAL, &itv, NULL);
842 /* use the RTC */
843 sigaction(SIGIO, &act, NULL);
844 fcntl(rtc_fd, F_SETFL, O_ASYNC);
845 fcntl(rtc_fd, F_SETOWN, getpid());
846 } else {
847 use_itimer:
848 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
849 PIT_FREQ) / 1000000;
852 #endif
855 void quit_timers(void)
857 #ifdef _WIN32
858 timeKillEvent(timerID);
859 #endif
862 /***********************************************************/
863 /* serial device */
865 #ifdef _WIN32
867 int serial_open_device(void)
869 return -1;
872 #else
874 int serial_open_device(void)
876 char slave_name[1024];
877 int master_fd, slave_fd;
879 if (serial_console == NULL && nographic) {
880 /* use console for serial port */
881 return 0;
882 } else {
883 #if 0
884 /* Not satisfying */
885 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
886 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
887 return -1;
889 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
890 return master_fd;
891 #else
892 return -1;
893 #endif
897 #endif
899 /***********************************************************/
900 /* Linux network device redirectors */
902 void hex_dump(FILE *f, const uint8_t *buf, int size)
904 int len, i, j, c;
906 for(i=0;i<size;i+=16) {
907 len = size - i;
908 if (len > 16)
909 len = 16;
910 fprintf(f, "%08x ", i);
911 for(j=0;j<16;j++) {
912 if (j < len)
913 fprintf(f, " %02x", buf[i+j]);
914 else
915 fprintf(f, " ");
917 fprintf(f, " ");
918 for(j=0;j<len;j++) {
919 c = buf[i+j];
920 if (c < ' ' || c > '~')
921 c = '.';
922 fprintf(f, "%c", c);
924 fprintf(f, "\n");
928 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
930 nd->send_packet(nd, buf, size);
933 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
934 IOReadHandler *fd_read, void *opaque)
936 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
939 /* dummy network adapter */
941 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
945 static void dummy_add_read_packet(NetDriverState *nd,
946 IOCanRWHandler *fd_can_read,
947 IOReadHandler *fd_read, void *opaque)
951 static int net_dummy_init(NetDriverState *nd)
953 nd->send_packet = dummy_send_packet;
954 nd->add_read_packet = dummy_add_read_packet;
955 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
956 return 0;
959 #if defined(CONFIG_SLIRP)
961 /* slirp network adapter */
963 static void *slirp_fd_opaque;
964 static IOCanRWHandler *slirp_fd_can_read;
965 static IOReadHandler *slirp_fd_read;
966 static int slirp_inited;
968 int slirp_can_output(void)
970 return slirp_fd_can_read(slirp_fd_opaque);
973 void slirp_output(const uint8_t *pkt, int pkt_len)
975 #if 0
976 printf("output:\n");
977 hex_dump(stdout, pkt, pkt_len);
978 #endif
979 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
982 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
984 #if 0
985 printf("input:\n");
986 hex_dump(stdout, buf, size);
987 #endif
988 slirp_input(buf, size);
991 static void slirp_add_read_packet(NetDriverState *nd,
992 IOCanRWHandler *fd_can_read,
993 IOReadHandler *fd_read, void *opaque)
995 slirp_fd_opaque = opaque;
996 slirp_fd_can_read = fd_can_read;
997 slirp_fd_read = fd_read;
1000 static int net_slirp_init(NetDriverState *nd)
1002 if (!slirp_inited) {
1003 slirp_inited = 1;
1004 slirp_init();
1006 nd->send_packet = slirp_send_packet;
1007 nd->add_read_packet = slirp_add_read_packet;
1008 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1009 return 0;
1012 #endif /* CONFIG_SLIRP */
1014 #if !defined(_WIN32)
1015 #ifdef _BSD
1016 static int tun_open(char *ifname, int ifname_size)
1018 int fd;
1019 char *dev;
1020 struct stat s;
1022 fd = open("/dev/tap", O_RDWR);
1023 if (fd < 0) {
1024 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1025 return -1;
1028 fstat(fd, &s);
1029 dev = devname(s.st_rdev, S_IFCHR);
1030 pstrcpy(ifname, ifname_size, dev);
1032 fcntl(fd, F_SETFL, O_NONBLOCK);
1033 return fd;
1035 #else
1036 static int tun_open(char *ifname, int ifname_size)
1038 struct ifreq ifr;
1039 int fd, ret;
1041 fd = open("/dev/net/tun", O_RDWR);
1042 if (fd < 0) {
1043 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1044 return -1;
1046 memset(&ifr, 0, sizeof(ifr));
1047 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1048 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1049 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1050 if (ret != 0) {
1051 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1052 close(fd);
1053 return -1;
1055 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1056 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1057 fcntl(fd, F_SETFL, O_NONBLOCK);
1058 return fd;
1060 #endif
1062 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1064 write(nd->fd, buf, size);
1067 static void tun_add_read_packet(NetDriverState *nd,
1068 IOCanRWHandler *fd_can_read,
1069 IOReadHandler *fd_read, void *opaque)
1071 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1074 static int net_tun_init(NetDriverState *nd)
1076 int pid, status;
1077 char *args[3];
1078 char **parg;
1080 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1081 if (nd->fd < 0)
1082 return -1;
1084 /* try to launch network init script */
1085 pid = fork();
1086 if (pid >= 0) {
1087 if (pid == 0) {
1088 parg = args;
1089 *parg++ = network_script;
1090 *parg++ = nd->ifname;
1091 *parg++ = NULL;
1092 execv(network_script, args);
1093 exit(1);
1095 while (waitpid(pid, &status, 0) != pid);
1096 if (!WIFEXITED(status) ||
1097 WEXITSTATUS(status) != 0) {
1098 fprintf(stderr, "%s: could not launch network script\n",
1099 network_script);
1102 nd->send_packet = tun_send_packet;
1103 nd->add_read_packet = tun_add_read_packet;
1104 return 0;
1107 static int net_fd_init(NetDriverState *nd, int fd)
1109 nd->fd = fd;
1110 nd->send_packet = tun_send_packet;
1111 nd->add_read_packet = tun_add_read_packet;
1112 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1113 return 0;
1116 #endif /* !_WIN32 */
1118 /***********************************************************/
1119 /* dumb display */
1121 #ifdef _WIN32
1123 static void term_exit(void)
1127 static void term_init(void)
1131 #else
1133 /* init terminal so that we can grab keys */
1134 static struct termios oldtty;
1136 static void term_exit(void)
1138 tcsetattr (0, TCSANOW, &oldtty);
1141 static void term_init(void)
1143 struct termios tty;
1145 tcgetattr (0, &tty);
1146 oldtty = tty;
1148 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1149 |INLCR|IGNCR|ICRNL|IXON);
1150 tty.c_oflag |= OPOST;
1151 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1152 /* if graphical mode, we allow Ctrl-C handling */
1153 if (nographic)
1154 tty.c_lflag &= ~ISIG;
1155 tty.c_cflag &= ~(CSIZE|PARENB);
1156 tty.c_cflag |= CS8;
1157 tty.c_cc[VMIN] = 1;
1158 tty.c_cc[VTIME] = 0;
1160 tcsetattr (0, TCSANOW, &tty);
1162 atexit(term_exit);
1164 fcntl(0, F_SETFL, O_NONBLOCK);
1167 #endif
1169 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1173 static void dumb_resize(DisplayState *ds, int w, int h)
1177 static void dumb_refresh(DisplayState *ds)
1179 vga_update_display();
1182 void dumb_display_init(DisplayState *ds)
1184 ds->data = NULL;
1185 ds->linesize = 0;
1186 ds->depth = 0;
1187 ds->dpy_update = dumb_update;
1188 ds->dpy_resize = dumb_resize;
1189 ds->dpy_refresh = dumb_refresh;
1192 #if !defined(CONFIG_SOFTMMU)
1193 /***********************************************************/
1194 /* cpu signal handler */
1195 static void host_segv_handler(int host_signum, siginfo_t *info,
1196 void *puc)
1198 if (cpu_signal_handler(host_signum, info, puc))
1199 return;
1200 term_exit();
1201 abort();
1203 #endif
1205 /***********************************************************/
1206 /* I/O handling */
1208 #define MAX_IO_HANDLERS 64
1210 typedef struct IOHandlerRecord {
1211 int fd;
1212 IOCanRWHandler *fd_can_read;
1213 IOReadHandler *fd_read;
1214 void *opaque;
1215 /* temporary data */
1216 struct pollfd *ufd;
1217 int max_size;
1218 struct IOHandlerRecord *next;
1219 } IOHandlerRecord;
1221 static IOHandlerRecord *first_io_handler;
1223 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1224 IOReadHandler *fd_read, void *opaque)
1226 IOHandlerRecord *ioh;
1228 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1229 if (!ioh)
1230 return -1;
1231 ioh->fd = fd;
1232 ioh->fd_can_read = fd_can_read;
1233 ioh->fd_read = fd_read;
1234 ioh->opaque = opaque;
1235 ioh->next = first_io_handler;
1236 first_io_handler = ioh;
1237 return 0;
1240 void qemu_del_fd_read_handler(int fd)
1242 IOHandlerRecord **pioh, *ioh;
1244 pioh = &first_io_handler;
1245 for(;;) {
1246 ioh = *pioh;
1247 if (ioh == NULL)
1248 break;
1249 if (ioh->fd == fd) {
1250 *pioh = ioh->next;
1251 break;
1253 pioh = &ioh->next;
1257 /***********************************************************/
1258 /* savevm/loadvm support */
1260 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1262 fwrite(buf, 1, size, f);
1265 void qemu_put_byte(QEMUFile *f, int v)
1267 fputc(v, f);
1270 void qemu_put_be16(QEMUFile *f, unsigned int v)
1272 qemu_put_byte(f, v >> 8);
1273 qemu_put_byte(f, v);
1276 void qemu_put_be32(QEMUFile *f, unsigned int v)
1278 qemu_put_byte(f, v >> 24);
1279 qemu_put_byte(f, v >> 16);
1280 qemu_put_byte(f, v >> 8);
1281 qemu_put_byte(f, v);
1284 void qemu_put_be64(QEMUFile *f, uint64_t v)
1286 qemu_put_be32(f, v >> 32);
1287 qemu_put_be32(f, v);
1290 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1292 return fread(buf, 1, size, f);
1295 int qemu_get_byte(QEMUFile *f)
1297 int v;
1298 v = fgetc(f);
1299 if (v == EOF)
1300 return 0;
1301 else
1302 return v;
1305 unsigned int qemu_get_be16(QEMUFile *f)
1307 unsigned int v;
1308 v = qemu_get_byte(f) << 8;
1309 v |= qemu_get_byte(f);
1310 return v;
1313 unsigned int qemu_get_be32(QEMUFile *f)
1315 unsigned int v;
1316 v = qemu_get_byte(f) << 24;
1317 v |= qemu_get_byte(f) << 16;
1318 v |= qemu_get_byte(f) << 8;
1319 v |= qemu_get_byte(f);
1320 return v;
1323 uint64_t qemu_get_be64(QEMUFile *f)
1325 uint64_t v;
1326 v = (uint64_t)qemu_get_be32(f) << 32;
1327 v |= qemu_get_be32(f);
1328 return v;
1331 int64_t qemu_ftell(QEMUFile *f)
1333 return ftell(f);
1336 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1338 if (fseek(f, pos, whence) < 0)
1339 return -1;
1340 return ftell(f);
1343 typedef struct SaveStateEntry {
1344 char idstr[256];
1345 int instance_id;
1346 int version_id;
1347 SaveStateHandler *save_state;
1348 LoadStateHandler *load_state;
1349 void *opaque;
1350 struct SaveStateEntry *next;
1351 } SaveStateEntry;
1353 static SaveStateEntry *first_se;
1355 int register_savevm(const char *idstr,
1356 int instance_id,
1357 int version_id,
1358 SaveStateHandler *save_state,
1359 LoadStateHandler *load_state,
1360 void *opaque)
1362 SaveStateEntry *se, **pse;
1364 se = qemu_malloc(sizeof(SaveStateEntry));
1365 if (!se)
1366 return -1;
1367 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1368 se->instance_id = instance_id;
1369 se->version_id = version_id;
1370 se->save_state = save_state;
1371 se->load_state = load_state;
1372 se->opaque = opaque;
1373 se->next = NULL;
1375 /* add at the end of list */
1376 pse = &first_se;
1377 while (*pse != NULL)
1378 pse = &(*pse)->next;
1379 *pse = se;
1380 return 0;
1383 #define QEMU_VM_FILE_MAGIC 0x5145564d
1384 #define QEMU_VM_FILE_VERSION 0x00000001
1386 int qemu_savevm(const char *filename)
1388 SaveStateEntry *se;
1389 QEMUFile *f;
1390 int len, len_pos, cur_pos, saved_vm_running, ret;
1392 saved_vm_running = vm_running;
1393 vm_stop(0);
1395 f = fopen(filename, "wb");
1396 if (!f) {
1397 ret = -1;
1398 goto the_end;
1401 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1402 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1404 for(se = first_se; se != NULL; se = se->next) {
1405 /* ID string */
1406 len = strlen(se->idstr);
1407 qemu_put_byte(f, len);
1408 qemu_put_buffer(f, se->idstr, len);
1410 qemu_put_be32(f, se->instance_id);
1411 qemu_put_be32(f, se->version_id);
1413 /* record size: filled later */
1414 len_pos = ftell(f);
1415 qemu_put_be32(f, 0);
1417 se->save_state(f, se->opaque);
1419 /* fill record size */
1420 cur_pos = ftell(f);
1421 len = ftell(f) - len_pos - 4;
1422 fseek(f, len_pos, SEEK_SET);
1423 qemu_put_be32(f, len);
1424 fseek(f, cur_pos, SEEK_SET);
1427 fclose(f);
1428 ret = 0;
1429 the_end:
1430 if (saved_vm_running)
1431 vm_start();
1432 return ret;
1435 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1437 SaveStateEntry *se;
1439 for(se = first_se; se != NULL; se = se->next) {
1440 if (!strcmp(se->idstr, idstr) &&
1441 instance_id == se->instance_id)
1442 return se;
1444 return NULL;
1447 int qemu_loadvm(const char *filename)
1449 SaveStateEntry *se;
1450 QEMUFile *f;
1451 int len, cur_pos, ret, instance_id, record_len, version_id;
1452 int saved_vm_running;
1453 unsigned int v;
1454 char idstr[256];
1456 saved_vm_running = vm_running;
1457 vm_stop(0);
1459 f = fopen(filename, "rb");
1460 if (!f) {
1461 ret = -1;
1462 goto the_end;
1465 v = qemu_get_be32(f);
1466 if (v != QEMU_VM_FILE_MAGIC)
1467 goto fail;
1468 v = qemu_get_be32(f);
1469 if (v != QEMU_VM_FILE_VERSION) {
1470 fail:
1471 fclose(f);
1472 ret = -1;
1473 goto the_end;
1475 for(;;) {
1476 #if defined (DO_TB_FLUSH)
1477 tb_flush(global_env);
1478 #endif
1479 len = qemu_get_byte(f);
1480 if (feof(f))
1481 break;
1482 qemu_get_buffer(f, idstr, len);
1483 idstr[len] = '\0';
1484 instance_id = qemu_get_be32(f);
1485 version_id = qemu_get_be32(f);
1486 record_len = qemu_get_be32(f);
1487 #if 0
1488 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1489 idstr, instance_id, version_id, record_len);
1490 #endif
1491 cur_pos = ftell(f);
1492 se = find_se(idstr, instance_id);
1493 if (!se) {
1494 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1495 instance_id, idstr);
1496 } else {
1497 ret = se->load_state(f, se->opaque, version_id);
1498 if (ret < 0) {
1499 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1500 instance_id, idstr);
1503 /* always seek to exact end of record */
1504 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1506 fclose(f);
1507 ret = 0;
1508 the_end:
1509 if (saved_vm_running)
1510 vm_start();
1511 return ret;
1514 /***********************************************************/
1515 /* cpu save/restore */
1517 #if defined(TARGET_I386)
1519 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1521 qemu_put_be32(f, (uint32_t)dt->base);
1522 qemu_put_be32(f, dt->limit);
1523 qemu_put_be32(f, dt->flags);
1526 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1528 dt->base = (uint8_t *)qemu_get_be32(f);
1529 dt->limit = qemu_get_be32(f);
1530 dt->flags = qemu_get_be32(f);
1533 void cpu_save(QEMUFile *f, void *opaque)
1535 CPUState *env = opaque;
1536 uint16_t fptag, fpus, fpuc;
1537 uint32_t hflags;
1538 int i;
1540 for(i = 0; i < 8; i++)
1541 qemu_put_be32s(f, &env->regs[i]);
1542 qemu_put_be32s(f, &env->eip);
1543 qemu_put_be32s(f, &env->eflags);
1544 qemu_put_be32s(f, &env->eflags);
1545 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1546 qemu_put_be32s(f, &hflags);
1548 /* FPU */
1549 fpuc = env->fpuc;
1550 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1551 fptag = 0;
1552 for (i=7; i>=0; i--) {
1553 fptag <<= 2;
1554 if (env->fptags[i]) {
1555 fptag |= 3;
1559 qemu_put_be16s(f, &fpuc);
1560 qemu_put_be16s(f, &fpus);
1561 qemu_put_be16s(f, &fptag);
1563 for(i = 0; i < 8; i++) {
1564 uint64_t mant;
1565 uint16_t exp;
1566 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1567 qemu_put_be64(f, mant);
1568 qemu_put_be16(f, exp);
1571 for(i = 0; i < 6; i++)
1572 cpu_put_seg(f, &env->segs[i]);
1573 cpu_put_seg(f, &env->ldt);
1574 cpu_put_seg(f, &env->tr);
1575 cpu_put_seg(f, &env->gdt);
1576 cpu_put_seg(f, &env->idt);
1578 qemu_put_be32s(f, &env->sysenter_cs);
1579 qemu_put_be32s(f, &env->sysenter_esp);
1580 qemu_put_be32s(f, &env->sysenter_eip);
1582 qemu_put_be32s(f, &env->cr[0]);
1583 qemu_put_be32s(f, &env->cr[2]);
1584 qemu_put_be32s(f, &env->cr[3]);
1585 qemu_put_be32s(f, &env->cr[4]);
1587 for(i = 0; i < 8; i++)
1588 qemu_put_be32s(f, &env->dr[i]);
1590 /* MMU */
1591 qemu_put_be32s(f, &env->a20_mask);
1594 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1596 CPUState *env = opaque;
1597 int i;
1598 uint32_t hflags;
1599 uint16_t fpus, fpuc, fptag;
1601 if (version_id != 1)
1602 return -EINVAL;
1603 for(i = 0; i < 8; i++)
1604 qemu_get_be32s(f, &env->regs[i]);
1605 qemu_get_be32s(f, &env->eip);
1606 qemu_get_be32s(f, &env->eflags);
1607 qemu_get_be32s(f, &env->eflags);
1608 qemu_get_be32s(f, &hflags);
1610 qemu_get_be16s(f, &fpuc);
1611 qemu_get_be16s(f, &fpus);
1612 qemu_get_be16s(f, &fptag);
1614 for(i = 0; i < 8; i++) {
1615 uint64_t mant;
1616 uint16_t exp;
1617 mant = qemu_get_be64(f);
1618 exp = qemu_get_be16(f);
1619 env->fpregs[i] = cpu_set_fp80(mant, exp);
1622 env->fpuc = fpuc;
1623 env->fpstt = (fpus >> 11) & 7;
1624 env->fpus = fpus & ~0x3800;
1625 for(i = 0; i < 8; i++) {
1626 env->fptags[i] = ((fptag & 3) == 3);
1627 fptag >>= 2;
1630 for(i = 0; i < 6; i++)
1631 cpu_get_seg(f, &env->segs[i]);
1632 cpu_get_seg(f, &env->ldt);
1633 cpu_get_seg(f, &env->tr);
1634 cpu_get_seg(f, &env->gdt);
1635 cpu_get_seg(f, &env->idt);
1637 qemu_get_be32s(f, &env->sysenter_cs);
1638 qemu_get_be32s(f, &env->sysenter_esp);
1639 qemu_get_be32s(f, &env->sysenter_eip);
1641 qemu_get_be32s(f, &env->cr[0]);
1642 qemu_get_be32s(f, &env->cr[2]);
1643 qemu_get_be32s(f, &env->cr[3]);
1644 qemu_get_be32s(f, &env->cr[4]);
1646 for(i = 0; i < 8; i++)
1647 qemu_get_be32s(f, &env->dr[i]);
1649 /* MMU */
1650 qemu_get_be32s(f, &env->a20_mask);
1652 /* XXX: compute hflags from scratch, except for CPL and IIF */
1653 env->hflags = hflags;
1654 tlb_flush(env, 1);
1655 return 0;
1658 #elif defined(TARGET_PPC)
1659 void cpu_save(QEMUFile *f, void *opaque)
1663 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1665 return 0;
1667 #else
1669 #warning No CPU save/restore functions
1671 #endif
1673 /***********************************************************/
1674 /* ram save/restore */
1676 /* we just avoid storing empty pages */
1677 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1679 int i, v;
1681 v = buf[0];
1682 for(i = 1; i < len; i++) {
1683 if (buf[i] != v)
1684 goto normal_save;
1686 qemu_put_byte(f, 1);
1687 qemu_put_byte(f, v);
1688 return;
1689 normal_save:
1690 qemu_put_byte(f, 0);
1691 qemu_put_buffer(f, buf, len);
1694 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1696 int v;
1698 v = qemu_get_byte(f);
1699 switch(v) {
1700 case 0:
1701 if (qemu_get_buffer(f, buf, len) != len)
1702 return -EIO;
1703 break;
1704 case 1:
1705 v = qemu_get_byte(f);
1706 memset(buf, v, len);
1707 break;
1708 default:
1709 return -EINVAL;
1711 return 0;
1714 static void ram_save(QEMUFile *f, void *opaque)
1716 int i;
1717 qemu_put_be32(f, phys_ram_size);
1718 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1719 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1723 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1725 int i, ret;
1727 if (version_id != 1)
1728 return -EINVAL;
1729 if (qemu_get_be32(f) != phys_ram_size)
1730 return -EINVAL;
1731 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1732 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1733 if (ret)
1734 return ret;
1736 return 0;
1739 /***********************************************************/
1740 /* main execution loop */
1742 void gui_update(void *opaque)
1744 display_state.dpy_refresh(&display_state);
1745 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1748 /* XXX: support several handlers */
1749 VMStopHandler *vm_stop_cb;
1750 VMStopHandler *vm_stop_opaque;
1752 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1754 vm_stop_cb = cb;
1755 vm_stop_opaque = opaque;
1756 return 0;
1759 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1761 vm_stop_cb = NULL;
1764 void vm_start(void)
1766 if (!vm_running) {
1767 cpu_enable_ticks();
1768 vm_running = 1;
1772 void vm_stop(int reason)
1774 if (vm_running) {
1775 cpu_disable_ticks();
1776 vm_running = 0;
1777 if (reason != 0) {
1778 if (vm_stop_cb) {
1779 vm_stop_cb(vm_stop_opaque, reason);
1785 int main_loop(void)
1787 #ifndef _WIN32
1788 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1789 IOHandlerRecord *ioh, *ioh_next;
1790 uint8_t buf[4096];
1791 int n, max_size;
1792 #endif
1793 int ret, timeout;
1794 CPUState *env = global_env;
1796 for(;;) {
1797 if (vm_running) {
1798 ret = cpu_exec(env);
1799 if (reset_requested) {
1800 ret = EXCP_INTERRUPT;
1801 break;
1803 if (ret == EXCP_DEBUG) {
1804 vm_stop(EXCP_DEBUG);
1806 /* if hlt instruction, we wait until the next IRQ */
1807 /* XXX: use timeout computed from timers */
1808 if (ret == EXCP_HLT)
1809 timeout = 10;
1810 else
1811 timeout = 0;
1812 } else {
1813 timeout = 10;
1816 #ifdef _WIN32
1817 if (timeout > 0)
1818 Sleep(timeout);
1819 #else
1821 /* poll any events */
1822 /* XXX: separate device handlers from system ones */
1823 pf = ufds;
1824 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1825 if (!ioh->fd_can_read) {
1826 max_size = 0;
1827 pf->fd = ioh->fd;
1828 pf->events = POLLIN;
1829 ioh->ufd = pf;
1830 pf++;
1831 } else {
1832 max_size = ioh->fd_can_read(ioh->opaque);
1833 if (max_size > 0) {
1834 if (max_size > sizeof(buf))
1835 max_size = sizeof(buf);
1836 pf->fd = ioh->fd;
1837 pf->events = POLLIN;
1838 ioh->ufd = pf;
1839 pf++;
1840 } else {
1841 ioh->ufd = NULL;
1844 ioh->max_size = max_size;
1847 ret = poll(ufds, pf - ufds, timeout);
1848 if (ret > 0) {
1849 /* XXX: better handling of removal */
1850 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1851 ioh_next = ioh->next;
1852 pf = ioh->ufd;
1853 if (pf) {
1854 if (pf->revents & POLLIN) {
1855 if (ioh->max_size == 0) {
1856 /* just a read event */
1857 ioh->fd_read(ioh->opaque, NULL, 0);
1858 } else {
1859 n = read(ioh->fd, buf, ioh->max_size);
1860 if (n >= 0) {
1861 ioh->fd_read(ioh->opaque, buf, n);
1862 } else if (errno != EAGAIN) {
1863 ioh->fd_read(ioh->opaque, NULL, -errno);
1871 #if defined(CONFIG_SLIRP)
1872 /* XXX: merge with poll() */
1873 if (slirp_inited) {
1874 fd_set rfds, wfds, xfds;
1875 int nfds;
1876 struct timeval tv;
1878 nfds = -1;
1879 FD_ZERO(&rfds);
1880 FD_ZERO(&wfds);
1881 FD_ZERO(&xfds);
1882 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1883 tv.tv_sec = 0;
1884 tv.tv_usec = 0;
1885 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1886 if (ret >= 0) {
1887 slirp_select_poll(&rfds, &wfds, &xfds);
1890 #endif
1892 #endif
1894 if (vm_running) {
1895 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1896 qemu_get_clock(vm_clock));
1898 if (audio_enabled) {
1899 /* XXX: add explicit timer */
1900 SB16_run();
1903 /* run dma transfers, if any */
1904 DMA_run();
1907 /* real time timers */
1908 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1909 qemu_get_clock(rt_clock));
1911 cpu_disable_ticks();
1912 return ret;
1915 void help(void)
1917 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
1918 "usage: %s [options] [disk_image]\n"
1919 "\n"
1920 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1921 "\n"
1922 "Standard options:\n"
1923 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1924 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1925 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1926 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1927 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1928 "-snapshot write to temporary files instead of disk image files\n"
1929 "-m megs set virtual RAM size to megs MB [default=%d]\n"
1930 "-nographic disable graphical output and redirect serial I/Os to console\n"
1931 "-enable-audio enable audio support\n"
1932 "\n"
1933 "Network options:\n"
1934 "-nics n simulate 'n' network cards [default=1]\n"
1935 "-macaddr addr set the mac address of the first interface\n"
1936 "-n script set tap/tun network init script [default=%s]\n"
1937 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1938 #ifdef CONFIG_SLIRP
1939 "-user-net use user mode network stack [default if no tap/tun script]\n"
1940 #endif
1941 "-dummy-net use dummy network stack\n"
1942 "\n"
1943 "Linux boot specific:\n"
1944 "-kernel bzImage use 'bzImage' as kernel image\n"
1945 "-append cmdline use 'cmdline' as kernel command line\n"
1946 "-initrd file use 'file' as initial ram disk\n"
1947 "\n"
1948 "Debug/Expert options:\n"
1949 "-S freeze CPU at startup (use 'c' to start execution)\n"
1950 "-s wait gdb connection to port %d\n"
1951 "-p port change gdb connection port\n"
1952 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1953 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1954 "-L path set the directory for the BIOS and VGA BIOS\n"
1955 #ifdef USE_CODE_COPY
1956 "-no-code-copy disable code copy acceleration\n"
1957 #endif
1959 "\n"
1960 "During emulation, use C-a h to get terminal commands:\n",
1961 #ifdef CONFIG_SOFTMMU
1962 "qemu",
1963 #else
1964 "qemu-fast",
1965 #endif
1966 DEFAULT_RAM_SIZE,
1967 DEFAULT_NETWORK_SCRIPT,
1968 DEFAULT_GDBSTUB_PORT,
1969 "/tmp/qemu.log");
1970 term_print_help();
1971 #ifndef CONFIG_SOFTMMU
1972 printf("\n"
1973 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1974 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1975 "PC emulation.\n");
1976 #endif
1977 exit(1);
1980 #define HAS_ARG 0x0001
1982 enum {
1983 QEMU_OPTION_h,
1985 QEMU_OPTION_fda,
1986 QEMU_OPTION_fdb,
1987 QEMU_OPTION_hda,
1988 QEMU_OPTION_hdb,
1989 QEMU_OPTION_hdc,
1990 QEMU_OPTION_hdd,
1991 QEMU_OPTION_cdrom,
1992 QEMU_OPTION_boot,
1993 QEMU_OPTION_snapshot,
1994 QEMU_OPTION_m,
1995 QEMU_OPTION_nographic,
1996 QEMU_OPTION_enable_audio,
1998 QEMU_OPTION_nics,
1999 QEMU_OPTION_macaddr,
2000 QEMU_OPTION_n,
2001 QEMU_OPTION_tun_fd,
2002 QEMU_OPTION_user_net,
2003 QEMU_OPTION_dummy_net,
2005 QEMU_OPTION_kernel,
2006 QEMU_OPTION_append,
2007 QEMU_OPTION_initrd,
2009 QEMU_OPTION_S,
2010 QEMU_OPTION_s,
2011 QEMU_OPTION_p,
2012 QEMU_OPTION_d,
2013 QEMU_OPTION_hdachs,
2014 QEMU_OPTION_L,
2015 QEMU_OPTION_no_code_copy,
2016 QEMU_OPTION_pci,
2017 QEMU_OPTION_prep,
2020 typedef struct QEMUOption {
2021 const char *name;
2022 int flags;
2023 int index;
2024 } QEMUOption;
2026 const QEMUOption qemu_options[] = {
2027 { "h", 0, QEMU_OPTION_h },
2029 { "fda", HAS_ARG, QEMU_OPTION_fda },
2030 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2031 { "hda", HAS_ARG, QEMU_OPTION_hda },
2032 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2033 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2034 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2035 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2036 { "boot", HAS_ARG, QEMU_OPTION_boot },
2037 { "snapshot", 0, QEMU_OPTION_snapshot },
2038 { "m", HAS_ARG, QEMU_OPTION_m },
2039 { "nographic", 0, QEMU_OPTION_nographic },
2040 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2042 { "nics", HAS_ARG, QEMU_OPTION_nics},
2043 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2044 { "n", HAS_ARG, QEMU_OPTION_n },
2045 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2046 #ifdef CONFIG_SLIRP
2047 { "user-net", 0, QEMU_OPTION_user_net },
2048 #endif
2049 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2051 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2052 { "append", HAS_ARG, QEMU_OPTION_append },
2053 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2055 { "S", 0, QEMU_OPTION_S },
2056 { "s", 0, QEMU_OPTION_s },
2057 { "p", HAS_ARG, QEMU_OPTION_p },
2058 { "d", HAS_ARG, QEMU_OPTION_d },
2059 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2060 { "L", HAS_ARG, QEMU_OPTION_L },
2061 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2063 /* temporary options */
2064 { "pci", 0, QEMU_OPTION_pci },
2065 #ifdef TARGET_PPC
2066 { "prep", 0, QEMU_OPTION_prep },
2067 #endif
2068 { NULL },
2071 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2073 /* this stack is only used during signal handling */
2074 #define SIGNAL_STACK_SIZE 32768
2076 static uint8_t *signal_stack;
2078 #endif
2080 #define NET_IF_TUN 0
2081 #define NET_IF_USER 1
2082 #define NET_IF_DUMMY 2
2084 int main(int argc, char **argv)
2086 #ifdef CONFIG_GDBSTUB
2087 int use_gdbstub, gdbstub_port;
2088 #endif
2089 int i, has_cdrom;
2090 int snapshot, linux_boot;
2091 CPUState *env;
2092 const char *initrd_filename;
2093 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2094 const char *kernel_filename, *kernel_cmdline;
2095 DisplayState *ds = &display_state;
2096 int cyls, heads, secs;
2097 int start_emulation = 1;
2098 uint8_t macaddr[6];
2099 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2100 int optind;
2101 const char *r, *optarg;
2103 #if !defined(CONFIG_SOFTMMU)
2104 /* we never want that malloc() uses mmap() */
2105 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2106 #endif
2107 initrd_filename = NULL;
2108 for(i = 0; i < MAX_FD; i++)
2109 fd_filename[i] = NULL;
2110 for(i = 0; i < MAX_DISKS; i++)
2111 hd_filename[i] = NULL;
2112 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2113 vga_ram_size = VGA_RAM_SIZE;
2114 bios_size = BIOS_SIZE;
2115 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2116 #ifdef CONFIG_GDBSTUB
2117 use_gdbstub = 0;
2118 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2119 #endif
2120 snapshot = 0;
2121 nographic = 0;
2122 kernel_filename = NULL;
2123 kernel_cmdline = "";
2124 has_cdrom = 1;
2125 cyls = heads = secs = 0;
2127 nb_tun_fds = 0;
2128 net_if_type = -1;
2129 nb_nics = 1;
2130 /* default mac address of the first network interface */
2131 macaddr[0] = 0x52;
2132 macaddr[1] = 0x54;
2133 macaddr[2] = 0x00;
2134 macaddr[3] = 0x12;
2135 macaddr[4] = 0x34;
2136 macaddr[5] = 0x56;
2138 optind = 1;
2139 for(;;) {
2140 if (optind >= argc)
2141 break;
2142 r = argv[optind];
2143 if (r[0] != '-') {
2144 hd_filename[0] = argv[optind++];
2145 } else {
2146 const QEMUOption *popt;
2148 optind++;
2149 popt = qemu_options;
2150 for(;;) {
2151 if (!popt->name) {
2152 fprintf(stderr, "%s: invalid option -- '%s'\n",
2153 argv[0], r);
2154 exit(1);
2156 if (!strcmp(popt->name, r + 1))
2157 break;
2158 popt++;
2160 if (popt->flags & HAS_ARG) {
2161 if (optind >= argc) {
2162 fprintf(stderr, "%s: option '%s' requires an argument\n",
2163 argv[0], r);
2164 exit(1);
2166 optarg = argv[optind++];
2167 } else {
2168 optarg = NULL;
2171 switch(popt->index) {
2172 case QEMU_OPTION_initrd:
2173 initrd_filename = optarg;
2174 break;
2175 case QEMU_OPTION_hda:
2176 hd_filename[0] = optarg;
2177 break;
2178 case QEMU_OPTION_hdb:
2179 hd_filename[1] = optarg;
2180 break;
2181 case QEMU_OPTION_snapshot:
2182 snapshot = 1;
2183 break;
2184 case QEMU_OPTION_hdachs:
2186 const char *p;
2187 p = optarg;
2188 cyls = strtol(p, (char **)&p, 0);
2189 if (*p != ',')
2190 goto chs_fail;
2191 p++;
2192 heads = strtol(p, (char **)&p, 0);
2193 if (*p != ',')
2194 goto chs_fail;
2195 p++;
2196 secs = strtol(p, (char **)&p, 0);
2197 if (*p != '\0') {
2198 chs_fail:
2199 cyls = 0;
2202 break;
2203 case QEMU_OPTION_nographic:
2204 nographic = 1;
2205 break;
2206 case QEMU_OPTION_kernel:
2207 kernel_filename = optarg;
2208 break;
2209 case QEMU_OPTION_append:
2210 kernel_cmdline = optarg;
2211 break;
2212 case QEMU_OPTION_tun_fd:
2214 const char *p;
2215 int fd;
2216 net_if_type = NET_IF_TUN;
2217 if (nb_tun_fds < MAX_NICS) {
2218 fd = strtol(optarg, (char **)&p, 0);
2219 if (*p != '\0') {
2220 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2221 exit(1);
2223 tun_fds[nb_tun_fds++] = fd;
2226 break;
2227 case QEMU_OPTION_hdc:
2228 hd_filename[2] = optarg;
2229 has_cdrom = 0;
2230 break;
2231 case QEMU_OPTION_hdd:
2232 hd_filename[3] = optarg;
2233 break;
2234 case QEMU_OPTION_cdrom:
2235 hd_filename[2] = optarg;
2236 has_cdrom = 1;
2237 break;
2238 case QEMU_OPTION_boot:
2239 boot_device = optarg[0];
2240 if (boot_device != 'a' && boot_device != 'b' &&
2241 boot_device != 'c' && boot_device != 'd') {
2242 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2243 exit(1);
2245 break;
2246 case QEMU_OPTION_fda:
2247 fd_filename[0] = optarg;
2248 break;
2249 case QEMU_OPTION_fdb:
2250 fd_filename[1] = optarg;
2251 break;
2252 case QEMU_OPTION_no_code_copy:
2253 code_copy_enabled = 0;
2254 break;
2255 case QEMU_OPTION_nics:
2256 nb_nics = atoi(optarg);
2257 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2258 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2259 exit(1);
2261 break;
2262 case QEMU_OPTION_macaddr:
2264 const char *p;
2265 int i;
2266 p = optarg;
2267 for(i = 0; i < 6; i++) {
2268 macaddr[i] = strtol(p, (char **)&p, 16);
2269 if (i == 5) {
2270 if (*p != '\0')
2271 goto macaddr_error;
2272 } else {
2273 if (*p != ':') {
2274 macaddr_error:
2275 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2276 exit(1);
2278 p++;
2282 break;
2283 case QEMU_OPTION_user_net:
2284 net_if_type = NET_IF_USER;
2285 break;
2286 case QEMU_OPTION_dummy_net:
2287 net_if_type = NET_IF_DUMMY;
2288 break;
2289 case QEMU_OPTION_enable_audio:
2290 audio_enabled = 1;
2291 break;
2292 case QEMU_OPTION_h:
2293 help();
2294 break;
2295 case QEMU_OPTION_m:
2296 ram_size = atoi(optarg) * 1024 * 1024;
2297 if (ram_size <= 0)
2298 help();
2299 if (ram_size > PHYS_RAM_MAX_SIZE) {
2300 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2301 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2302 exit(1);
2304 break;
2305 case QEMU_OPTION_d:
2307 int mask;
2308 CPULogItem *item;
2310 mask = cpu_str_to_log_mask(optarg);
2311 if (!mask) {
2312 printf("Log items (comma separated):\n");
2313 for(item = cpu_log_items; item->mask != 0; item++) {
2314 printf("%-10s %s\n", item->name, item->help);
2316 exit(1);
2318 cpu_set_log(mask);
2320 break;
2321 case QEMU_OPTION_n:
2322 pstrcpy(network_script, sizeof(network_script), optarg);
2323 break;
2324 #ifdef CONFIG_GDBSTUB
2325 case QEMU_OPTION_s:
2326 use_gdbstub = 1;
2327 break;
2328 case QEMU_OPTION_p:
2329 gdbstub_port = atoi(optarg);
2330 break;
2331 #endif
2332 case QEMU_OPTION_L:
2333 bios_dir = optarg;
2334 break;
2335 case QEMU_OPTION_S:
2336 start_emulation = 0;
2337 break;
2338 case QEMU_OPTION_pci:
2339 pci_enabled = 1;
2340 break;
2341 case QEMU_OPTION_prep:
2342 prep_enabled = 1;
2343 break;
2348 linux_boot = (kernel_filename != NULL);
2350 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2351 fd_filename[0] == '\0')
2352 help();
2354 /* boot to cd by default if no hard disk */
2355 if (hd_filename[0] == '\0' && boot_device == 'c') {
2356 if (fd_filename[0] != '\0')
2357 boot_device = 'a';
2358 else
2359 boot_device = 'd';
2362 #if !defined(CONFIG_SOFTMMU)
2363 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2365 static uint8_t stdout_buf[4096];
2366 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2368 #else
2369 setvbuf(stdout, NULL, _IOLBF, 0);
2370 #endif
2372 /* init host network redirectors */
2373 if (net_if_type == -1) {
2374 net_if_type = NET_IF_TUN;
2375 #if defined(CONFIG_SLIRP)
2376 if (access(network_script, R_OK) < 0) {
2377 net_if_type = NET_IF_USER;
2379 #endif
2382 for(i = 0; i < nb_nics; i++) {
2383 NetDriverState *nd = &nd_table[i];
2384 nd->index = i;
2385 /* init virtual mac address */
2386 nd->macaddr[0] = macaddr[0];
2387 nd->macaddr[1] = macaddr[1];
2388 nd->macaddr[2] = macaddr[2];
2389 nd->macaddr[3] = macaddr[3];
2390 nd->macaddr[4] = macaddr[4];
2391 nd->macaddr[5] = macaddr[5] + i;
2392 switch(net_if_type) {
2393 #if defined(CONFIG_SLIRP)
2394 case NET_IF_USER:
2395 net_slirp_init(nd);
2396 break;
2397 #endif
2398 #if !defined(_WIN32)
2399 case NET_IF_TUN:
2400 if (i < nb_tun_fds) {
2401 net_fd_init(nd, tun_fds[i]);
2402 } else {
2403 if (net_tun_init(nd) < 0)
2404 net_dummy_init(nd);
2406 break;
2407 #endif
2408 case NET_IF_DUMMY:
2409 default:
2410 net_dummy_init(nd);
2411 break;
2415 /* init the memory */
2416 phys_ram_size = ram_size + vga_ram_size + bios_size;
2418 #ifdef CONFIG_SOFTMMU
2419 #ifdef _BSD
2420 /* mallocs are always aligned on BSD. */
2421 phys_ram_base = malloc(phys_ram_size);
2422 #else
2423 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2424 #endif
2425 if (!phys_ram_base) {
2426 fprintf(stderr, "Could not allocate physical memory\n");
2427 exit(1);
2429 #else
2430 /* as we must map the same page at several addresses, we must use
2431 a fd */
2433 const char *tmpdir;
2435 tmpdir = getenv("QEMU_TMPDIR");
2436 if (!tmpdir)
2437 tmpdir = "/tmp";
2438 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2439 if (mkstemp(phys_ram_file) < 0) {
2440 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2441 phys_ram_file);
2442 exit(1);
2444 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2445 if (phys_ram_fd < 0) {
2446 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2447 phys_ram_file);
2448 exit(1);
2450 ftruncate(phys_ram_fd, phys_ram_size);
2451 unlink(phys_ram_file);
2452 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2453 phys_ram_size,
2454 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2455 phys_ram_fd, 0);
2456 if (phys_ram_base == MAP_FAILED) {
2457 fprintf(stderr, "Could not map physical memory\n");
2458 exit(1);
2461 #endif
2463 /* we always create the cdrom drive, even if no disk is there */
2464 if (has_cdrom) {
2465 bs_table[2] = bdrv_new("cdrom");
2466 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2469 /* open the virtual block devices */
2470 for(i = 0; i < MAX_DISKS; i++) {
2471 if (hd_filename[i]) {
2472 if (!bs_table[i]) {
2473 char buf[64];
2474 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2475 bs_table[i] = bdrv_new(buf);
2477 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2478 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2479 hd_filename[i]);
2480 exit(1);
2482 if (i == 0 && cyls != 0)
2483 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2487 /* we always create at least one floppy disk */
2488 fd_table[0] = bdrv_new("fda");
2489 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2491 for(i = 0; i < MAX_FD; i++) {
2492 if (fd_filename[i]) {
2493 if (!fd_table[i]) {
2494 char buf[64];
2495 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2496 fd_table[i] = bdrv_new(buf);
2497 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2499 if (fd_filename[i] != '\0') {
2500 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2501 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2502 fd_filename[i]);
2503 exit(1);
2509 /* init CPU state */
2510 env = cpu_init();
2511 global_env = env;
2512 cpu_single_env = env;
2514 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2515 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2516 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2518 init_ioports();
2519 cpu_calibrate_ticks();
2521 /* terminal init */
2522 if (nographic) {
2523 dumb_display_init(ds);
2524 } else {
2525 #ifdef CONFIG_SDL
2526 sdl_display_init(ds);
2527 #else
2528 dumb_display_init(ds);
2529 #endif
2532 /* setup cpu signal handlers for MMU / self modifying code handling */
2533 #if !defined(CONFIG_SOFTMMU)
2535 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2537 stack_t stk;
2538 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2539 stk.ss_sp = signal_stack;
2540 stk.ss_size = SIGNAL_STACK_SIZE;
2541 stk.ss_flags = 0;
2543 if (sigaltstack(&stk, NULL) < 0) {
2544 perror("sigaltstack");
2545 exit(1);
2548 #endif
2550 struct sigaction act;
2552 sigfillset(&act.sa_mask);
2553 act.sa_flags = SA_SIGINFO;
2554 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2555 act.sa_flags |= SA_ONSTACK;
2556 #endif
2557 act.sa_sigaction = host_segv_handler;
2558 sigaction(SIGSEGV, &act, NULL);
2559 sigaction(SIGBUS, &act, NULL);
2560 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2561 sigaction(SIGFPE, &act, NULL);
2562 #endif
2564 #endif
2566 #ifndef _WIN32
2568 struct sigaction act;
2569 sigfillset(&act.sa_mask);
2570 act.sa_flags = 0;
2571 act.sa_handler = SIG_IGN;
2572 sigaction(SIGPIPE, &act, NULL);
2574 #endif
2575 init_timers();
2577 #if defined(TARGET_I386)
2578 pc_init(ram_size, vga_ram_size, boot_device,
2579 ds, fd_filename, snapshot,
2580 kernel_filename, kernel_cmdline, initrd_filename);
2581 #elif defined(TARGET_PPC)
2582 ppc_init(ram_size, vga_ram_size, boot_device,
2583 ds, fd_filename, snapshot,
2584 kernel_filename, kernel_cmdline, initrd_filename);
2585 #endif
2587 /* launched after the device init so that it can display or not a
2588 banner */
2589 monitor_init();
2591 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2592 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2594 #ifdef CONFIG_GDBSTUB
2595 if (use_gdbstub) {
2596 if (gdbserver_start(gdbstub_port) < 0) {
2597 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2598 gdbstub_port);
2599 exit(1);
2600 } else {
2601 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2603 } else
2604 #endif
2605 if (start_emulation)
2607 vm_start();
2609 term_init();
2610 main_loop();
2611 quit_timers();
2612 return 0;