allow '-nics 0'
[qemu.git] / vl.c
blob18f957dccf438f5f52498f125483b7ac3475a881
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 <getopt.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <time.h>
31 #include <errno.h>
32 #include <sys/time.h>
34 #ifndef _WIN32
35 #include <sys/times.h>
36 #include <sys/wait.h>
37 #include <termios.h>
38 #include <sys/poll.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #ifdef _BSD
43 #include <sys/stat.h>
44 #include <libutil.h>
45 #else
46 #include <linux/if.h>
47 #include <linux/if_tun.h>
48 #include <pty.h>
49 #include <malloc.h>
50 #include <linux/rtc.h>
51 #endif
52 #endif
54 #if defined(CONFIG_SLIRP)
55 #include "libslirp.h"
56 #endif
58 #ifdef _WIN32
59 #include <malloc.h>
60 #include <sys/timeb.h>
61 #include <windows.h>
62 #define getopt_long_only getopt_long
63 #define memalign(align, size) malloc(size)
64 #endif
66 #ifdef CONFIG_SDL
67 #if defined(__linux__)
68 /* SDL use the pthreads and they modify sigaction. We don't
69 want that. */
70 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
71 extern void __libc_sigaction();
72 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
73 #else
74 extern void __sigaction();
75 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
76 #endif
77 #endif /* __linux__ */
78 #endif /* CONFIG_SDL */
80 #include "disas.h"
82 #include "exec-all.h"
84 //#define DO_TB_FLUSH
86 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
88 //#define DEBUG_UNUSED_IOPORT
89 //#define DEBUG_IOPORT
91 #if !defined(CONFIG_SOFTMMU)
92 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
93 #else
94 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
95 #endif
97 /* in ms */
98 #define GUI_REFRESH_INTERVAL 30
100 /* XXX: use a two level table to limit memory usage */
101 #define MAX_IOPORTS 65536
103 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
104 char phys_ram_file[1024];
105 CPUState *global_env;
106 CPUState *cpu_single_env;
107 void *ioport_opaque[MAX_IOPORTS];
108 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
109 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
110 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
111 int vga_ram_size;
112 static DisplayState display_state;
113 int nographic;
114 int64_t ticks_per_sec;
115 int boot_device = 'c';
116 static int ram_size;
117 static char network_script[1024];
118 int pit_min_timer_count = 0;
119 int nb_nics;
120 NetDriverState nd_table[MAX_NICS];
121 SerialState *serial_console;
122 QEMUTimer *gui_timer;
123 int vm_running;
124 int audio_enabled = 0;
126 /***********************************************************/
127 /* x86 ISA bus support */
129 target_phys_addr_t isa_mem_base = 0;
131 uint32_t default_ioport_readb(void *opaque, uint32_t address)
133 #ifdef DEBUG_UNUSED_IOPORT
134 fprintf(stderr, "inb: port=0x%04x\n", address);
135 #endif
136 return 0xff;
139 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
141 #ifdef DEBUG_UNUSED_IOPORT
142 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
143 #endif
146 /* default is to make two byte accesses */
147 uint32_t default_ioport_readw(void *opaque, uint32_t address)
149 uint32_t data;
150 data = ioport_read_table[0][address](ioport_opaque[address], address);
151 address = (address + 1) & (MAX_IOPORTS - 1);
152 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
153 return data;
156 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
158 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
159 address = (address + 1) & (MAX_IOPORTS - 1);
160 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
163 uint32_t default_ioport_readl(void *opaque, uint32_t address)
165 #ifdef DEBUG_UNUSED_IOPORT
166 fprintf(stderr, "inl: port=0x%04x\n", address);
167 #endif
168 return 0xffffffff;
171 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
173 #ifdef DEBUG_UNUSED_IOPORT
174 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
175 #endif
178 void init_ioports(void)
180 int i;
182 for(i = 0; i < MAX_IOPORTS; i++) {
183 ioport_read_table[0][i] = default_ioport_readb;
184 ioport_write_table[0][i] = default_ioport_writeb;
185 ioport_read_table[1][i] = default_ioport_readw;
186 ioport_write_table[1][i] = default_ioport_writew;
187 ioport_read_table[2][i] = default_ioport_readl;
188 ioport_write_table[2][i] = default_ioport_writel;
192 /* size is the word size in byte */
193 int register_ioport_read(int start, int length, int size,
194 IOPortReadFunc *func, void *opaque)
196 int i, bsize;
198 if (size == 1) {
199 bsize = 0;
200 } else if (size == 2) {
201 bsize = 1;
202 } else if (size == 4) {
203 bsize = 2;
204 } else {
205 hw_error("register_ioport_read: invalid size");
206 return -1;
208 for(i = start; i < start + length; i += size) {
209 ioport_read_table[bsize][i] = func;
210 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
211 hw_error("register_ioport_read: invalid opaque");
212 ioport_opaque[i] = opaque;
214 return 0;
217 /* size is the word size in byte */
218 int register_ioport_write(int start, int length, int size,
219 IOPortWriteFunc *func, void *opaque)
221 int i, bsize;
223 if (size == 1) {
224 bsize = 0;
225 } else if (size == 2) {
226 bsize = 1;
227 } else if (size == 4) {
228 bsize = 2;
229 } else {
230 hw_error("register_ioport_write: invalid size");
231 return -1;
233 for(i = start; i < start + length; i += size) {
234 ioport_write_table[bsize][i] = func;
235 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
236 hw_error("register_ioport_read: invalid opaque");
237 ioport_opaque[i] = opaque;
239 return 0;
242 void pstrcpy(char *buf, int buf_size, const char *str)
244 int c;
245 char *q = buf;
247 if (buf_size <= 0)
248 return;
250 for(;;) {
251 c = *str++;
252 if (c == 0 || q >= buf + buf_size - 1)
253 break;
254 *q++ = c;
256 *q = '\0';
259 /* strcat and truncate. */
260 char *pstrcat(char *buf, int buf_size, const char *s)
262 int len;
263 len = strlen(buf);
264 if (len < buf_size)
265 pstrcpy(buf + len, buf_size - len, s);
266 return buf;
269 /* return the size or -1 if error */
270 int load_image(const char *filename, uint8_t *addr)
272 int fd, size;
273 fd = open(filename, O_RDONLY | O_BINARY);
274 if (fd < 0)
275 return -1;
276 size = lseek(fd, 0, SEEK_END);
277 lseek(fd, 0, SEEK_SET);
278 if (read(fd, addr, size) != size) {
279 close(fd);
280 return -1;
282 close(fd);
283 return size;
286 void cpu_outb(CPUState *env, int addr, int val)
288 #ifdef DEBUG_IOPORT
289 if (loglevel & CPU_LOG_IOPORT)
290 fprintf(logfile, "outb: %04x %02x\n", addr, val);
291 #endif
292 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
295 void cpu_outw(CPUState *env, int addr, int val)
297 #ifdef DEBUG_IOPORT
298 if (loglevel & CPU_LOG_IOPORT)
299 fprintf(logfile, "outw: %04x %04x\n", addr, val);
300 #endif
301 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
304 void cpu_outl(CPUState *env, int addr, int val)
306 #ifdef DEBUG_IOPORT
307 if (loglevel & CPU_LOG_IOPORT)
308 fprintf(logfile, "outl: %04x %08x\n", addr, val);
309 #endif
310 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
313 int cpu_inb(CPUState *env, int addr)
315 int val;
316 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
317 #ifdef DEBUG_IOPORT
318 if (loglevel & CPU_LOG_IOPORT)
319 fprintf(logfile, "inb : %04x %02x\n", addr, val);
320 #endif
321 return val;
324 int cpu_inw(CPUState *env, int addr)
326 int val;
327 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
328 #ifdef DEBUG_IOPORT
329 if (loglevel & CPU_LOG_IOPORT)
330 fprintf(logfile, "inw : %04x %04x\n", addr, val);
331 #endif
332 return val;
335 int cpu_inl(CPUState *env, int addr)
337 int val;
338 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
339 #ifdef DEBUG_IOPORT
340 if (loglevel & CPU_LOG_IOPORT)
341 fprintf(logfile, "inl : %04x %08x\n", addr, val);
342 #endif
343 return val;
346 /***********************************************************/
347 void hw_error(const char *fmt, ...)
349 va_list ap;
351 va_start(ap, fmt);
352 fprintf(stderr, "qemu: hardware error: ");
353 vfprintf(stderr, fmt, ap);
354 fprintf(stderr, "\n");
355 #ifdef TARGET_I386
356 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
357 #else
358 cpu_dump_state(global_env, stderr, 0);
359 #endif
360 va_end(ap);
361 abort();
364 /***********************************************************/
365 /* timers */
367 #if defined(__powerpc__)
369 static inline uint32_t get_tbl(void)
371 uint32_t tbl;
372 asm volatile("mftb %0" : "=r" (tbl));
373 return tbl;
376 static inline uint32_t get_tbu(void)
378 uint32_t tbl;
379 asm volatile("mftbu %0" : "=r" (tbl));
380 return tbl;
383 int64_t cpu_get_real_ticks(void)
385 uint32_t l, h, h1;
386 /* NOTE: we test if wrapping has occurred */
387 do {
388 h = get_tbu();
389 l = get_tbl();
390 h1 = get_tbu();
391 } while (h != h1);
392 return ((int64_t)h << 32) | l;
395 #elif defined(__i386__)
397 int64_t cpu_get_real_ticks(void)
399 int64_t val;
400 asm volatile ("rdtsc" : "=A" (val));
401 return val;
404 #elif defined(__x86_64__)
406 int64_t cpu_get_real_ticks(void)
408 uint32_t low,high;
409 int64_t val;
410 asm volatile("rdtsc" : "=a" (low), "=d" (high));
411 val = high;
412 val <<= 32;
413 val |= low;
414 return val;
417 #else
418 #error unsupported CPU
419 #endif
421 static int64_t cpu_ticks_offset;
422 static int cpu_ticks_enabled;
424 static inline int64_t cpu_get_ticks(void)
426 if (!cpu_ticks_enabled) {
427 return cpu_ticks_offset;
428 } else {
429 return cpu_get_real_ticks() + cpu_ticks_offset;
433 /* enable cpu_get_ticks() */
434 void cpu_enable_ticks(void)
436 if (!cpu_ticks_enabled) {
437 cpu_ticks_offset -= cpu_get_real_ticks();
438 cpu_ticks_enabled = 1;
442 /* disable cpu_get_ticks() : the clock is stopped. You must not call
443 cpu_get_ticks() after that. */
444 void cpu_disable_ticks(void)
446 if (cpu_ticks_enabled) {
447 cpu_ticks_offset = cpu_get_ticks();
448 cpu_ticks_enabled = 0;
452 static int64_t get_clock(void)
454 #ifdef _WIN32
455 struct _timeb tb;
456 _ftime(&tb);
457 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
458 #else
459 struct timeval tv;
460 gettimeofday(&tv, NULL);
461 return tv.tv_sec * 1000000LL + tv.tv_usec;
462 #endif
465 void cpu_calibrate_ticks(void)
467 int64_t usec, ticks;
469 usec = get_clock();
470 ticks = cpu_get_real_ticks();
471 #ifdef _WIN32
472 Sleep(50);
473 #else
474 usleep(50 * 1000);
475 #endif
476 usec = get_clock() - usec;
477 ticks = cpu_get_real_ticks() - ticks;
478 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
481 /* compute with 96 bit intermediate result: (a*b)/c */
482 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
484 union {
485 uint64_t ll;
486 struct {
487 #ifdef WORDS_BIGENDIAN
488 uint32_t high, low;
489 #else
490 uint32_t low, high;
491 #endif
492 } l;
493 } u, res;
494 uint64_t rl, rh;
496 u.ll = a;
497 rl = (uint64_t)u.l.low * (uint64_t)b;
498 rh = (uint64_t)u.l.high * (uint64_t)b;
499 rh += (rl >> 32);
500 res.l.high = rh / c;
501 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
502 return res.ll;
505 #define QEMU_TIMER_REALTIME 0
506 #define QEMU_TIMER_VIRTUAL 1
508 struct QEMUClock {
509 int type;
510 /* XXX: add frequency */
513 struct QEMUTimer {
514 QEMUClock *clock;
515 int64_t expire_time;
516 QEMUTimerCB *cb;
517 void *opaque;
518 struct QEMUTimer *next;
521 QEMUClock *rt_clock;
522 QEMUClock *vm_clock;
524 static QEMUTimer *active_timers[2];
525 #ifdef _WIN32
526 static MMRESULT timerID;
527 #else
528 /* frequency of the times() clock tick */
529 static int timer_freq;
530 #endif
532 QEMUClock *qemu_new_clock(int type)
534 QEMUClock *clock;
535 clock = qemu_mallocz(sizeof(QEMUClock));
536 if (!clock)
537 return NULL;
538 clock->type = type;
539 return clock;
542 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
544 QEMUTimer *ts;
546 ts = qemu_mallocz(sizeof(QEMUTimer));
547 ts->clock = clock;
548 ts->cb = cb;
549 ts->opaque = opaque;
550 return ts;
553 void qemu_free_timer(QEMUTimer *ts)
555 qemu_free(ts);
558 /* stop a timer, but do not dealloc it */
559 void qemu_del_timer(QEMUTimer *ts)
561 QEMUTimer **pt, *t;
563 /* NOTE: this code must be signal safe because
564 qemu_timer_expired() can be called from a signal. */
565 pt = &active_timers[ts->clock->type];
566 for(;;) {
567 t = *pt;
568 if (!t)
569 break;
570 if (t == ts) {
571 *pt = t->next;
572 break;
574 pt = &t->next;
578 /* modify the current timer so that it will be fired when current_time
579 >= expire_time. The corresponding callback will be called. */
580 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
582 QEMUTimer **pt, *t;
584 qemu_del_timer(ts);
586 /* add the timer in the sorted list */
587 /* NOTE: this code must be signal safe because
588 qemu_timer_expired() can be called from a signal. */
589 pt = &active_timers[ts->clock->type];
590 for(;;) {
591 t = *pt;
592 if (!t)
593 break;
594 if (t->expire_time > expire_time)
595 break;
596 pt = &t->next;
598 ts->expire_time = expire_time;
599 ts->next = *pt;
600 *pt = ts;
603 int qemu_timer_pending(QEMUTimer *ts)
605 QEMUTimer *t;
606 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
607 if (t == ts)
608 return 1;
610 return 0;
613 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
615 if (!timer_head)
616 return 0;
617 return (timer_head->expire_time <= current_time);
620 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
622 QEMUTimer *ts;
624 for(;;) {
625 ts = *ptimer_head;
626 if (ts->expire_time > current_time)
627 break;
628 /* remove timer from the list before calling the callback */
629 *ptimer_head = ts->next;
630 ts->next = NULL;
632 /* run the callback (the timer list can be modified) */
633 ts->cb(ts->opaque);
637 int64_t qemu_get_clock(QEMUClock *clock)
639 switch(clock->type) {
640 case QEMU_TIMER_REALTIME:
641 #ifdef _WIN32
642 return GetTickCount();
643 #else
645 struct tms tp;
647 /* Note that using gettimeofday() is not a good solution
648 for timers because its value change when the date is
649 modified. */
650 if (timer_freq == 100) {
651 return times(&tp) * 10;
652 } else {
653 return ((int64_t)times(&tp) * 1000) / timer_freq;
656 #endif
657 default:
658 case QEMU_TIMER_VIRTUAL:
659 return cpu_get_ticks();
663 /* save a timer */
664 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
666 uint64_t expire_time;
668 if (qemu_timer_pending(ts)) {
669 expire_time = ts->expire_time;
670 } else {
671 expire_time = -1;
673 qemu_put_be64(f, expire_time);
676 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
678 uint64_t expire_time;
680 expire_time = qemu_get_be64(f);
681 if (expire_time != -1) {
682 qemu_mod_timer(ts, expire_time);
683 } else {
684 qemu_del_timer(ts);
688 static void timer_save(QEMUFile *f, void *opaque)
690 if (cpu_ticks_enabled) {
691 hw_error("cannot save state if virtual timers are running");
693 qemu_put_be64s(f, &cpu_ticks_offset);
694 qemu_put_be64s(f, &ticks_per_sec);
697 static int timer_load(QEMUFile *f, void *opaque, int version_id)
699 if (version_id != 1)
700 return -EINVAL;
701 if (cpu_ticks_enabled) {
702 return -EINVAL;
704 qemu_get_be64s(f, &cpu_ticks_offset);
705 qemu_get_be64s(f, &ticks_per_sec);
706 return 0;
709 #ifdef _WIN32
710 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
711 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
712 #else
713 static void host_alarm_handler(int host_signum)
714 #endif
716 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
717 qemu_get_clock(vm_clock)) ||
718 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
719 qemu_get_clock(rt_clock))) {
720 /* stop the cpu because a timer occured */
721 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
725 #ifndef _WIN32
727 #define RTC_FREQ 1024
729 static int rtc_fd;
731 static int start_rtc_timer(void)
733 rtc_fd = open("/dev/rtc", O_RDONLY);
734 if (rtc_fd < 0)
735 return -1;
736 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
737 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
738 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
739 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
740 goto fail;
742 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
743 fail:
744 close(rtc_fd);
745 return -1;
747 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
748 return 0;
751 #endif
753 static void init_timers(void)
755 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
756 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
758 #ifdef _WIN32
760 int count=0;
761 timerID = timeSetEvent(10, // interval (ms)
762 0, // resolution
763 host_alarm_handler, // function
764 (DWORD)&count, // user parameter
765 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
766 if( !timerID ) {
767 perror("failed timer alarm");
768 exit(1);
771 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
772 #else
774 struct sigaction act;
775 struct itimerval itv;
777 /* get times() syscall frequency */
778 timer_freq = sysconf(_SC_CLK_TCK);
780 /* timer signal */
781 sigfillset(&act.sa_mask);
782 act.sa_flags = 0;
783 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
784 act.sa_flags |= SA_ONSTACK;
785 #endif
786 act.sa_handler = host_alarm_handler;
787 sigaction(SIGALRM, &act, NULL);
789 itv.it_interval.tv_sec = 0;
790 itv.it_interval.tv_usec = 1000;
791 itv.it_value.tv_sec = 0;
792 itv.it_value.tv_usec = 10 * 1000;
793 setitimer(ITIMER_REAL, &itv, NULL);
794 /* we probe the tick duration of the kernel to inform the user if
795 the emulated kernel requested a too high timer frequency */
796 getitimer(ITIMER_REAL, &itv);
798 if (itv.it_interval.tv_usec > 1000) {
799 /* try to use /dev/rtc to have a faster timer */
800 if (start_rtc_timer() < 0)
801 goto use_itimer;
802 /* disable itimer */
803 itv.it_interval.tv_sec = 0;
804 itv.it_interval.tv_usec = 0;
805 itv.it_value.tv_sec = 0;
806 itv.it_value.tv_usec = 0;
807 setitimer(ITIMER_REAL, &itv, NULL);
809 /* use the RTC */
810 sigaction(SIGIO, &act, NULL);
811 fcntl(rtc_fd, F_SETFL, O_ASYNC);
812 fcntl(rtc_fd, F_SETOWN, getpid());
813 } else {
814 use_itimer:
815 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
816 PIT_FREQ) / 1000000;
819 #endif
822 void quit_timers(void)
824 #ifdef _WIN32
825 timeKillEvent(timerID);
826 #endif
829 /***********************************************************/
830 /* serial device */
832 #ifdef _WIN32
834 int serial_open_device(void)
836 return -1;
839 #else
841 int serial_open_device(void)
843 char slave_name[1024];
844 int master_fd, slave_fd;
846 if (serial_console == NULL && nographic) {
847 /* use console for serial port */
848 return 0;
849 } else {
850 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
851 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
852 return -1;
854 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
855 return master_fd;
859 #endif
861 /***********************************************************/
862 /* Linux network device redirectors */
864 void hex_dump(FILE *f, const uint8_t *buf, int size)
866 int len, i, j, c;
868 for(i=0;i<size;i+=16) {
869 len = size - i;
870 if (len > 16)
871 len = 16;
872 fprintf(f, "%08x ", i);
873 for(j=0;j<16;j++) {
874 if (j < len)
875 fprintf(f, " %02x", buf[i+j]);
876 else
877 fprintf(f, " ");
879 fprintf(f, " ");
880 for(j=0;j<len;j++) {
881 c = buf[i+j];
882 if (c < ' ' || c > '~')
883 c = '.';
884 fprintf(f, "%c", c);
886 fprintf(f, "\n");
890 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
892 nd->send_packet(nd, buf, size);
895 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
896 IOReadHandler *fd_read, void *opaque)
898 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
901 /* dummy network adapter */
903 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
907 static void dummy_add_read_packet(NetDriverState *nd,
908 IOCanRWHandler *fd_can_read,
909 IOReadHandler *fd_read, void *opaque)
913 static int net_dummy_init(NetDriverState *nd)
915 nd->send_packet = dummy_send_packet;
916 nd->add_read_packet = dummy_add_read_packet;
917 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
918 return 0;
921 #if defined(CONFIG_SLIRP)
923 /* slirp network adapter */
925 static void *slirp_fd_opaque;
926 static IOCanRWHandler *slirp_fd_can_read;
927 static IOReadHandler *slirp_fd_read;
928 static int slirp_inited;
930 int slirp_can_output(void)
932 return slirp_fd_can_read(slirp_fd_opaque);
935 void slirp_output(const uint8_t *pkt, int pkt_len)
937 #if 0
938 printf("output:\n");
939 hex_dump(stdout, pkt, pkt_len);
940 #endif
941 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
944 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
946 #if 0
947 printf("input:\n");
948 hex_dump(stdout, buf, size);
949 #endif
950 slirp_input(buf, size);
953 static void slirp_add_read_packet(NetDriverState *nd,
954 IOCanRWHandler *fd_can_read,
955 IOReadHandler *fd_read, void *opaque)
957 slirp_fd_opaque = opaque;
958 slirp_fd_can_read = fd_can_read;
959 slirp_fd_read = fd_read;
962 static int net_slirp_init(NetDriverState *nd)
964 if (!slirp_inited) {
965 slirp_inited = 1;
966 slirp_init();
968 nd->send_packet = slirp_send_packet;
969 nd->add_read_packet = slirp_add_read_packet;
970 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
971 return 0;
974 #endif /* CONFIG_SLIRP */
976 #if !defined(_WIN32)
977 #ifdef _BSD
978 static int tun_open(char *ifname, int ifname_size)
980 int fd;
981 char *dev;
982 struct stat s;
984 fd = open("/dev/tap", O_RDWR);
985 if (fd < 0) {
986 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
987 return -1;
990 fstat(fd, &s);
991 dev = devname(s.st_rdev, S_IFCHR);
992 pstrcpy(ifname, ifname_size, dev);
994 fcntl(fd, F_SETFL, O_NONBLOCK);
995 return fd;
997 #else
998 static int tun_open(char *ifname, int ifname_size)
1000 struct ifreq ifr;
1001 int fd, ret;
1003 fd = open("/dev/net/tun", O_RDWR);
1004 if (fd < 0) {
1005 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1006 return -1;
1008 memset(&ifr, 0, sizeof(ifr));
1009 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1010 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1011 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1012 if (ret != 0) {
1013 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1014 close(fd);
1015 return -1;
1017 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1018 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1019 fcntl(fd, F_SETFL, O_NONBLOCK);
1020 return fd;
1022 #endif
1024 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1026 write(nd->fd, buf, size);
1029 static void tun_add_read_packet(NetDriverState *nd,
1030 IOCanRWHandler *fd_can_read,
1031 IOReadHandler *fd_read, void *opaque)
1033 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1036 static int net_tun_init(NetDriverState *nd)
1038 int pid, status;
1039 char *args[3];
1040 char **parg;
1042 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1043 if (nd->fd < 0)
1044 return -1;
1046 /* try to launch network init script */
1047 pid = fork();
1048 if (pid >= 0) {
1049 if (pid == 0) {
1050 parg = args;
1051 *parg++ = network_script;
1052 *parg++ = nd->ifname;
1053 *parg++ = NULL;
1054 execv(network_script, args);
1055 exit(1);
1057 while (waitpid(pid, &status, 0) != pid);
1058 if (!WIFEXITED(status) ||
1059 WEXITSTATUS(status) != 0) {
1060 fprintf(stderr, "%s: could not launch network script\n",
1061 network_script);
1064 nd->send_packet = tun_send_packet;
1065 nd->add_read_packet = tun_add_read_packet;
1066 return 0;
1069 static int net_fd_init(NetDriverState *nd, int fd)
1071 nd->fd = fd;
1072 nd->send_packet = tun_send_packet;
1073 nd->add_read_packet = tun_add_read_packet;
1074 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1075 return 0;
1078 #endif /* !_WIN32 */
1080 /***********************************************************/
1081 /* dumb display */
1083 #ifdef _WIN32
1085 static void term_exit(void)
1089 static void term_init(void)
1093 #else
1095 /* init terminal so that we can grab keys */
1096 static struct termios oldtty;
1098 static void term_exit(void)
1100 tcsetattr (0, TCSANOW, &oldtty);
1103 static void term_init(void)
1105 struct termios tty;
1107 tcgetattr (0, &tty);
1108 oldtty = tty;
1110 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1111 |INLCR|IGNCR|ICRNL|IXON);
1112 tty.c_oflag |= OPOST;
1113 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1114 /* if graphical mode, we allow Ctrl-C handling */
1115 if (nographic)
1116 tty.c_lflag &= ~ISIG;
1117 tty.c_cflag &= ~(CSIZE|PARENB);
1118 tty.c_cflag |= CS8;
1119 tty.c_cc[VMIN] = 1;
1120 tty.c_cc[VTIME] = 0;
1122 tcsetattr (0, TCSANOW, &tty);
1124 atexit(term_exit);
1126 fcntl(0, F_SETFL, O_NONBLOCK);
1129 #endif
1131 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1135 static void dumb_resize(DisplayState *ds, int w, int h)
1139 static void dumb_refresh(DisplayState *ds)
1141 vga_update_display();
1144 void dumb_display_init(DisplayState *ds)
1146 ds->data = NULL;
1147 ds->linesize = 0;
1148 ds->depth = 0;
1149 ds->dpy_update = dumb_update;
1150 ds->dpy_resize = dumb_resize;
1151 ds->dpy_refresh = dumb_refresh;
1154 #if !defined(CONFIG_SOFTMMU)
1155 /***********************************************************/
1156 /* cpu signal handler */
1157 static void host_segv_handler(int host_signum, siginfo_t *info,
1158 void *puc)
1160 if (cpu_signal_handler(host_signum, info, puc))
1161 return;
1162 term_exit();
1163 abort();
1165 #endif
1167 /***********************************************************/
1168 /* I/O handling */
1170 #define MAX_IO_HANDLERS 64
1172 typedef struct IOHandlerRecord {
1173 int fd;
1174 IOCanRWHandler *fd_can_read;
1175 IOReadHandler *fd_read;
1176 void *opaque;
1177 /* temporary data */
1178 struct pollfd *ufd;
1179 int max_size;
1180 struct IOHandlerRecord *next;
1181 } IOHandlerRecord;
1183 static IOHandlerRecord *first_io_handler;
1185 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1186 IOReadHandler *fd_read, void *opaque)
1188 IOHandlerRecord *ioh;
1190 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1191 if (!ioh)
1192 return -1;
1193 ioh->fd = fd;
1194 ioh->fd_can_read = fd_can_read;
1195 ioh->fd_read = fd_read;
1196 ioh->opaque = opaque;
1197 ioh->next = first_io_handler;
1198 first_io_handler = ioh;
1199 return 0;
1202 void qemu_del_fd_read_handler(int fd)
1204 IOHandlerRecord **pioh, *ioh;
1206 pioh = &first_io_handler;
1207 for(;;) {
1208 ioh = *pioh;
1209 if (ioh == NULL)
1210 break;
1211 if (ioh->fd == fd) {
1212 *pioh = ioh->next;
1213 break;
1215 pioh = &ioh->next;
1219 /***********************************************************/
1220 /* savevm/loadvm support */
1222 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1224 fwrite(buf, 1, size, f);
1227 void qemu_put_byte(QEMUFile *f, int v)
1229 fputc(v, f);
1232 void qemu_put_be16(QEMUFile *f, unsigned int v)
1234 qemu_put_byte(f, v >> 8);
1235 qemu_put_byte(f, v);
1238 void qemu_put_be32(QEMUFile *f, unsigned int v)
1240 qemu_put_byte(f, v >> 24);
1241 qemu_put_byte(f, v >> 16);
1242 qemu_put_byte(f, v >> 8);
1243 qemu_put_byte(f, v);
1246 void qemu_put_be64(QEMUFile *f, uint64_t v)
1248 qemu_put_be32(f, v >> 32);
1249 qemu_put_be32(f, v);
1252 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1254 return fread(buf, 1, size, f);
1257 int qemu_get_byte(QEMUFile *f)
1259 int v;
1260 v = fgetc(f);
1261 if (v == EOF)
1262 return 0;
1263 else
1264 return v;
1267 unsigned int qemu_get_be16(QEMUFile *f)
1269 unsigned int v;
1270 v = qemu_get_byte(f) << 8;
1271 v |= qemu_get_byte(f);
1272 return v;
1275 unsigned int qemu_get_be32(QEMUFile *f)
1277 unsigned int v;
1278 v = qemu_get_byte(f) << 24;
1279 v |= qemu_get_byte(f) << 16;
1280 v |= qemu_get_byte(f) << 8;
1281 v |= qemu_get_byte(f);
1282 return v;
1285 uint64_t qemu_get_be64(QEMUFile *f)
1287 uint64_t v;
1288 v = (uint64_t)qemu_get_be32(f) << 32;
1289 v |= qemu_get_be32(f);
1290 return v;
1293 int64_t qemu_ftell(QEMUFile *f)
1295 return ftell(f);
1298 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1300 if (fseek(f, pos, whence) < 0)
1301 return -1;
1302 return ftell(f);
1305 typedef struct SaveStateEntry {
1306 char idstr[256];
1307 int instance_id;
1308 int version_id;
1309 SaveStateHandler *save_state;
1310 LoadStateHandler *load_state;
1311 void *opaque;
1312 struct SaveStateEntry *next;
1313 } SaveStateEntry;
1315 static SaveStateEntry *first_se;
1317 int register_savevm(const char *idstr,
1318 int instance_id,
1319 int version_id,
1320 SaveStateHandler *save_state,
1321 LoadStateHandler *load_state,
1322 void *opaque)
1324 SaveStateEntry *se, **pse;
1326 se = qemu_malloc(sizeof(SaveStateEntry));
1327 if (!se)
1328 return -1;
1329 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1330 se->instance_id = instance_id;
1331 se->version_id = version_id;
1332 se->save_state = save_state;
1333 se->load_state = load_state;
1334 se->opaque = opaque;
1335 se->next = NULL;
1337 /* add at the end of list */
1338 pse = &first_se;
1339 while (*pse != NULL)
1340 pse = &(*pse)->next;
1341 *pse = se;
1342 return 0;
1345 #define QEMU_VM_FILE_MAGIC 0x5145564d
1346 #define QEMU_VM_FILE_VERSION 0x00000001
1348 int qemu_savevm(const char *filename)
1350 SaveStateEntry *se;
1351 QEMUFile *f;
1352 int len, len_pos, cur_pos, saved_vm_running, ret;
1354 saved_vm_running = vm_running;
1355 vm_stop(0);
1357 f = fopen(filename, "wb");
1358 if (!f) {
1359 ret = -1;
1360 goto the_end;
1363 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1364 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1366 for(se = first_se; se != NULL; se = se->next) {
1367 /* ID string */
1368 len = strlen(se->idstr);
1369 qemu_put_byte(f, len);
1370 qemu_put_buffer(f, se->idstr, len);
1372 qemu_put_be32(f, se->instance_id);
1373 qemu_put_be32(f, se->version_id);
1375 /* record size: filled later */
1376 len_pos = ftell(f);
1377 qemu_put_be32(f, 0);
1379 se->save_state(f, se->opaque);
1381 /* fill record size */
1382 cur_pos = ftell(f);
1383 len = ftell(f) - len_pos - 4;
1384 fseek(f, len_pos, SEEK_SET);
1385 qemu_put_be32(f, len);
1386 fseek(f, cur_pos, SEEK_SET);
1389 fclose(f);
1390 ret = 0;
1391 the_end:
1392 if (saved_vm_running)
1393 vm_start();
1394 return ret;
1397 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1399 SaveStateEntry *se;
1401 for(se = first_se; se != NULL; se = se->next) {
1402 if (!strcmp(se->idstr, idstr) &&
1403 instance_id == se->instance_id)
1404 return se;
1406 return NULL;
1409 int qemu_loadvm(const char *filename)
1411 SaveStateEntry *se;
1412 QEMUFile *f;
1413 int len, cur_pos, ret, instance_id, record_len, version_id;
1414 int saved_vm_running;
1415 unsigned int v;
1416 char idstr[256];
1418 saved_vm_running = vm_running;
1419 vm_stop(0);
1421 f = fopen(filename, "rb");
1422 if (!f) {
1423 ret = -1;
1424 goto the_end;
1427 v = qemu_get_be32(f);
1428 if (v != QEMU_VM_FILE_MAGIC)
1429 goto fail;
1430 v = qemu_get_be32(f);
1431 if (v != QEMU_VM_FILE_VERSION) {
1432 fail:
1433 fclose(f);
1434 ret = -1;
1435 goto the_end;
1437 for(;;) {
1438 #if defined (DO_TB_FLUSH)
1439 tb_flush(global_env);
1440 #endif
1441 len = qemu_get_byte(f);
1442 if (feof(f))
1443 break;
1444 qemu_get_buffer(f, idstr, len);
1445 idstr[len] = '\0';
1446 instance_id = qemu_get_be32(f);
1447 version_id = qemu_get_be32(f);
1448 record_len = qemu_get_be32(f);
1449 #if 0
1450 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1451 idstr, instance_id, version_id, record_len);
1452 #endif
1453 cur_pos = ftell(f);
1454 se = find_se(idstr, instance_id);
1455 if (!se) {
1456 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1457 instance_id, idstr);
1458 } else {
1459 ret = se->load_state(f, se->opaque, version_id);
1460 if (ret < 0) {
1461 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1462 instance_id, idstr);
1465 /* always seek to exact end of record */
1466 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1468 fclose(f);
1469 ret = 0;
1470 the_end:
1471 if (saved_vm_running)
1472 vm_start();
1473 return ret;
1476 /***********************************************************/
1477 /* cpu save/restore */
1479 #if defined(TARGET_I386)
1481 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1483 qemu_put_be32(f, (uint32_t)dt->base);
1484 qemu_put_be32(f, dt->limit);
1485 qemu_put_be32(f, dt->flags);
1488 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1490 dt->base = (uint8_t *)qemu_get_be32(f);
1491 dt->limit = qemu_get_be32(f);
1492 dt->flags = qemu_get_be32(f);
1495 void cpu_save(QEMUFile *f, void *opaque)
1497 CPUState *env = opaque;
1498 uint16_t fptag, fpus, fpuc;
1499 uint32_t hflags;
1500 int i;
1502 for(i = 0; i < 8; i++)
1503 qemu_put_be32s(f, &env->regs[i]);
1504 qemu_put_be32s(f, &env->eip);
1505 qemu_put_be32s(f, &env->eflags);
1506 qemu_put_be32s(f, &env->eflags);
1507 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1508 qemu_put_be32s(f, &hflags);
1510 /* FPU */
1511 fpuc = env->fpuc;
1512 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1513 fptag = 0;
1514 for (i=7; i>=0; i--) {
1515 fptag <<= 2;
1516 if (env->fptags[i]) {
1517 fptag |= 3;
1521 qemu_put_be16s(f, &fpuc);
1522 qemu_put_be16s(f, &fpus);
1523 qemu_put_be16s(f, &fptag);
1525 for(i = 0; i < 8; i++) {
1526 uint64_t mant;
1527 uint16_t exp;
1528 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1529 qemu_put_be64(f, mant);
1530 qemu_put_be16(f, exp);
1533 for(i = 0; i < 6; i++)
1534 cpu_put_seg(f, &env->segs[i]);
1535 cpu_put_seg(f, &env->ldt);
1536 cpu_put_seg(f, &env->tr);
1537 cpu_put_seg(f, &env->gdt);
1538 cpu_put_seg(f, &env->idt);
1540 qemu_put_be32s(f, &env->sysenter_cs);
1541 qemu_put_be32s(f, &env->sysenter_esp);
1542 qemu_put_be32s(f, &env->sysenter_eip);
1544 qemu_put_be32s(f, &env->cr[0]);
1545 qemu_put_be32s(f, &env->cr[2]);
1546 qemu_put_be32s(f, &env->cr[3]);
1547 qemu_put_be32s(f, &env->cr[4]);
1549 for(i = 0; i < 8; i++)
1550 qemu_put_be32s(f, &env->dr[i]);
1552 /* MMU */
1553 qemu_put_be32s(f, &env->a20_mask);
1556 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1558 CPUState *env = opaque;
1559 int i;
1560 uint32_t hflags;
1561 uint16_t fpus, fpuc, fptag;
1563 if (version_id != 1)
1564 return -EINVAL;
1565 for(i = 0; i < 8; i++)
1566 qemu_get_be32s(f, &env->regs[i]);
1567 qemu_get_be32s(f, &env->eip);
1568 qemu_get_be32s(f, &env->eflags);
1569 qemu_get_be32s(f, &env->eflags);
1570 qemu_get_be32s(f, &hflags);
1572 qemu_get_be16s(f, &fpuc);
1573 qemu_get_be16s(f, &fpus);
1574 qemu_get_be16s(f, &fptag);
1576 for(i = 0; i < 8; i++) {
1577 uint64_t mant;
1578 uint16_t exp;
1579 mant = qemu_get_be64(f);
1580 exp = qemu_get_be16(f);
1581 env->fpregs[i] = cpu_set_fp80(mant, exp);
1584 env->fpuc = fpuc;
1585 env->fpstt = (fpus >> 11) & 7;
1586 env->fpus = fpus & ~0x3800;
1587 for(i = 0; i < 8; i++) {
1588 env->fptags[i] = ((fptag & 3) == 3);
1589 fptag >>= 2;
1592 for(i = 0; i < 6; i++)
1593 cpu_get_seg(f, &env->segs[i]);
1594 cpu_get_seg(f, &env->ldt);
1595 cpu_get_seg(f, &env->tr);
1596 cpu_get_seg(f, &env->gdt);
1597 cpu_get_seg(f, &env->idt);
1599 qemu_get_be32s(f, &env->sysenter_cs);
1600 qemu_get_be32s(f, &env->sysenter_esp);
1601 qemu_get_be32s(f, &env->sysenter_eip);
1603 qemu_get_be32s(f, &env->cr[0]);
1604 qemu_get_be32s(f, &env->cr[2]);
1605 qemu_get_be32s(f, &env->cr[3]);
1606 qemu_get_be32s(f, &env->cr[4]);
1608 for(i = 0; i < 8; i++)
1609 qemu_get_be32s(f, &env->dr[i]);
1611 /* MMU */
1612 qemu_get_be32s(f, &env->a20_mask);
1614 /* XXX: compute hflags from scratch, except for CPL and IIF */
1615 env->hflags = hflags;
1616 tlb_flush(env, 1);
1617 return 0;
1620 #elif defined(TARGET_PPC)
1621 void cpu_save(QEMUFile *f, void *opaque)
1625 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1627 return 0;
1629 #else
1631 #warning No CPU save/restore functions
1633 #endif
1635 /***********************************************************/
1636 /* ram save/restore */
1638 /* we just avoid storing empty pages */
1639 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1641 int i, v;
1643 v = buf[0];
1644 for(i = 1; i < len; i++) {
1645 if (buf[i] != v)
1646 goto normal_save;
1648 qemu_put_byte(f, 1);
1649 qemu_put_byte(f, v);
1650 return;
1651 normal_save:
1652 qemu_put_byte(f, 0);
1653 qemu_put_buffer(f, buf, len);
1656 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1658 int v;
1660 v = qemu_get_byte(f);
1661 switch(v) {
1662 case 0:
1663 if (qemu_get_buffer(f, buf, len) != len)
1664 return -EIO;
1665 break;
1666 case 1:
1667 v = qemu_get_byte(f);
1668 memset(buf, v, len);
1669 break;
1670 default:
1671 return -EINVAL;
1673 return 0;
1676 static void ram_save(QEMUFile *f, void *opaque)
1678 int i;
1679 qemu_put_be32(f, phys_ram_size);
1680 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1681 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1685 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1687 int i, ret;
1689 if (version_id != 1)
1690 return -EINVAL;
1691 if (qemu_get_be32(f) != phys_ram_size)
1692 return -EINVAL;
1693 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1694 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1695 if (ret)
1696 return ret;
1698 return 0;
1701 /***********************************************************/
1702 /* main execution loop */
1704 void gui_update(void *opaque)
1706 display_state.dpy_refresh(&display_state);
1707 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1710 /* XXX: support several handlers */
1711 VMStopHandler *vm_stop_cb;
1712 VMStopHandler *vm_stop_opaque;
1714 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1716 vm_stop_cb = cb;
1717 vm_stop_opaque = opaque;
1718 return 0;
1721 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1723 vm_stop_cb = NULL;
1726 void vm_start(void)
1728 if (!vm_running) {
1729 cpu_enable_ticks();
1730 vm_running = 1;
1734 void vm_stop(int reason)
1736 if (vm_running) {
1737 cpu_disable_ticks();
1738 vm_running = 0;
1739 if (reason != 0) {
1740 if (vm_stop_cb) {
1741 vm_stop_cb(vm_stop_opaque, reason);
1747 int main_loop(void)
1749 #ifndef _WIN32
1750 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1751 IOHandlerRecord *ioh, *ioh_next;
1752 uint8_t buf[4096];
1753 int n, max_size;
1754 #endif
1755 int ret, timeout;
1756 CPUState *env = global_env;
1758 for(;;) {
1759 if (vm_running) {
1760 ret = cpu_exec(env);
1761 if (reset_requested) {
1762 ret = EXCP_INTERRUPT;
1763 break;
1765 if (ret == EXCP_DEBUG) {
1766 vm_stop(EXCP_DEBUG);
1768 /* if hlt instruction, we wait until the next IRQ */
1769 /* XXX: use timeout computed from timers */
1770 if (ret == EXCP_HLT)
1771 timeout = 10;
1772 else
1773 timeout = 0;
1774 } else {
1775 timeout = 10;
1778 #ifdef _WIN32
1779 if (timeout > 0)
1780 Sleep(timeout);
1781 #else
1783 /* poll any events */
1784 /* XXX: separate device handlers from system ones */
1785 pf = ufds;
1786 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1787 if (!ioh->fd_can_read) {
1788 max_size = 0;
1789 pf->fd = ioh->fd;
1790 pf->events = POLLIN;
1791 ioh->ufd = pf;
1792 pf++;
1793 } else {
1794 max_size = ioh->fd_can_read(ioh->opaque);
1795 if (max_size > 0) {
1796 if (max_size > sizeof(buf))
1797 max_size = sizeof(buf);
1798 pf->fd = ioh->fd;
1799 pf->events = POLLIN;
1800 ioh->ufd = pf;
1801 pf++;
1802 } else {
1803 ioh->ufd = NULL;
1806 ioh->max_size = max_size;
1809 ret = poll(ufds, pf - ufds, timeout);
1810 if (ret > 0) {
1811 /* XXX: better handling of removal */
1812 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1813 ioh_next = ioh->next;
1814 pf = ioh->ufd;
1815 if (pf) {
1816 if (pf->revents & POLLIN) {
1817 if (ioh->max_size == 0) {
1818 /* just a read event */
1819 ioh->fd_read(ioh->opaque, NULL, 0);
1820 } else {
1821 n = read(ioh->fd, buf, ioh->max_size);
1822 if (n >= 0) {
1823 ioh->fd_read(ioh->opaque, buf, n);
1824 } else if (errno != -EAGAIN) {
1825 ioh->fd_read(ioh->opaque, NULL, -errno);
1833 #if defined(CONFIG_SLIRP)
1834 /* XXX: merge with poll() */
1835 if (slirp_inited) {
1836 fd_set rfds, wfds, xfds;
1837 int nfds;
1838 struct timeval tv;
1840 nfds = -1;
1841 FD_ZERO(&rfds);
1842 FD_ZERO(&wfds);
1843 FD_ZERO(&xfds);
1844 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1845 tv.tv_sec = 0;
1846 tv.tv_usec = 0;
1847 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1848 if (ret >= 0) {
1849 slirp_select_poll(&rfds, &wfds, &xfds);
1852 #endif
1854 #endif
1856 if (vm_running) {
1857 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1858 qemu_get_clock(vm_clock));
1860 if (audio_enabled) {
1861 /* XXX: add explicit timer */
1862 SB16_run();
1865 /* run dma transfers, if any */
1866 DMA_run();
1869 /* real time timers */
1870 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1871 qemu_get_clock(rt_clock));
1873 cpu_disable_ticks();
1874 return ret;
1877 void help(void)
1879 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
1880 "usage: %s [options] [disk_image]\n"
1881 "\n"
1882 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1883 "\n"
1884 "Standard options:\n"
1885 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1886 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1887 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1888 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1889 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1890 "-snapshot write to temporary files instead of disk image files\n"
1891 "-m megs set virtual RAM size to megs MB\n"
1892 "-nographic disable graphical output and redirect serial I/Os to console\n"
1893 "-enable-audio enable audio support\n"
1894 "\n"
1895 "Network options:\n"
1896 "-nics n simulate 'n' network cards [default=1]\n"
1897 "-macaddr addr set the mac address of the first interface\n"
1898 "-n script set tap/tun network init script [default=%s]\n"
1899 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1900 #ifdef CONFIG_SLIRP
1901 "-user-net use user mode network stack [default if no tap/tun script]\n"
1902 #endif
1903 "-dummy-net use dummy network stack\n"
1904 "\n"
1905 "Linux boot specific:\n"
1906 "-kernel bzImage use 'bzImage' as kernel image\n"
1907 "-append cmdline use 'cmdline' as kernel command line\n"
1908 "-initrd file use 'file' as initial ram disk\n"
1909 "\n"
1910 "Debug/Expert options:\n"
1911 "-s wait gdb connection to port %d\n"
1912 "-p port change gdb connection port\n"
1913 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1914 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1915 "-L path set the directory for the BIOS and VGA BIOS\n"
1916 #ifdef USE_CODE_COPY
1917 "-no-code-copy disable code copy acceleration\n"
1918 #endif
1920 "\n"
1921 "During emulation, use C-a h to get terminal commands:\n",
1922 #ifdef CONFIG_SOFTMMU
1923 "qemu",
1924 #else
1925 "qemu-fast",
1926 #endif
1927 DEFAULT_NETWORK_SCRIPT,
1928 DEFAULT_GDBSTUB_PORT,
1929 "/tmp/qemu.log");
1930 term_print_help();
1931 #ifndef CONFIG_SOFTMMU
1932 printf("\n"
1933 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1934 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1935 "PC emulation.\n");
1936 #endif
1937 exit(1);
1940 struct option long_options[] = {
1941 { "initrd", 1, NULL, 0, },
1942 { "hda", 1, NULL, 0, },
1943 { "hdb", 1, NULL, 0, },
1944 { "snapshot", 0, NULL, 0, },
1945 { "hdachs", 1, NULL, 0, },
1946 { "nographic", 0, NULL, 0, },
1947 { "kernel", 1, NULL, 0, },
1948 { "append", 1, NULL, 0, },
1949 { "tun-fd", 1, NULL, 0, },
1950 { "hdc", 1, NULL, 0, },
1951 { "hdd", 1, NULL, 0, },
1952 { "cdrom", 1, NULL, 0, },
1953 { "boot", 1, NULL, 0, },
1954 { "fda", 1, NULL, 0, },
1955 { "fdb", 1, NULL, 0, },
1956 { "no-code-copy", 0, NULL, 0 },
1957 { "nics", 1, NULL, 0 },
1958 { "macaddr", 1, NULL, 0 },
1959 { "user-net", 0, NULL, 0 },
1960 { "dummy-net", 0, NULL, 0 },
1961 { "enable-audio", 0, NULL, 0 },
1962 { NULL, 0, NULL, 0 },
1965 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1967 /* this stack is only used during signal handling */
1968 #define SIGNAL_STACK_SIZE 32768
1970 static uint8_t *signal_stack;
1972 #endif
1974 #define NET_IF_TUN 0
1975 #define NET_IF_USER 1
1976 #define NET_IF_DUMMY 2
1978 int main(int argc, char **argv)
1980 #ifdef CONFIG_GDBSTUB
1981 int use_gdbstub, gdbstub_port;
1982 #endif
1983 int c, i, long_index, has_cdrom;
1984 int snapshot, linux_boot;
1985 CPUState *env;
1986 const char *initrd_filename;
1987 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
1988 const char *kernel_filename, *kernel_cmdline;
1989 DisplayState *ds = &display_state;
1990 int cyls, heads, secs;
1991 int start_emulation = 1;
1992 uint8_t macaddr[6];
1993 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
1995 #if !defined(CONFIG_SOFTMMU)
1996 /* we never want that malloc() uses mmap() */
1997 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
1998 #endif
1999 initrd_filename = NULL;
2000 for(i = 0; i < MAX_FD; i++)
2001 fd_filename[i] = NULL;
2002 for(i = 0; i < MAX_DISKS; i++)
2003 hd_filename[i] = NULL;
2004 ram_size = 32 * 1024 * 1024;
2005 vga_ram_size = VGA_RAM_SIZE;
2006 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2007 #ifdef CONFIG_GDBSTUB
2008 use_gdbstub = 0;
2009 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2010 #endif
2011 snapshot = 0;
2012 nographic = 0;
2013 kernel_filename = NULL;
2014 kernel_cmdline = "";
2015 has_cdrom = 1;
2016 cyls = heads = secs = 0;
2018 nb_tun_fds = 0;
2019 net_if_type = -1;
2020 nb_nics = 1;
2021 /* default mac address of the first network interface */
2022 macaddr[0] = 0x52;
2023 macaddr[1] = 0x54;
2024 macaddr[2] = 0x00;
2025 macaddr[3] = 0x12;
2026 macaddr[4] = 0x34;
2027 macaddr[5] = 0x56;
2030 for(;;) {
2031 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index);
2032 if (c == -1)
2033 break;
2034 switch(c) {
2035 case 0:
2036 switch(long_index) {
2037 case 0:
2038 initrd_filename = optarg;
2039 break;
2040 case 1:
2041 hd_filename[0] = optarg;
2042 break;
2043 case 2:
2044 hd_filename[1] = optarg;
2045 break;
2046 case 3:
2047 snapshot = 1;
2048 break;
2049 case 4:
2051 const char *p;
2052 p = optarg;
2053 cyls = strtol(p, (char **)&p, 0);
2054 if (*p != ',')
2055 goto chs_fail;
2056 p++;
2057 heads = strtol(p, (char **)&p, 0);
2058 if (*p != ',')
2059 goto chs_fail;
2060 p++;
2061 secs = strtol(p, (char **)&p, 0);
2062 if (*p != '\0') {
2063 chs_fail:
2064 cyls = 0;
2067 break;
2068 case 5:
2069 nographic = 1;
2070 break;
2071 case 6:
2072 kernel_filename = optarg;
2073 break;
2074 case 7:
2075 kernel_cmdline = optarg;
2076 break;
2077 case 8:
2079 const char *p;
2080 int fd;
2081 if (nb_tun_fds < MAX_NICS) {
2082 fd = strtol(optarg, (char **)&p, 0);
2083 if (*p != '\0') {
2084 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2085 exit(1);
2087 tun_fds[nb_tun_fds++] = fd;
2090 break;
2091 case 9:
2092 hd_filename[2] = optarg;
2093 has_cdrom = 0;
2094 break;
2095 case 10:
2096 hd_filename[3] = optarg;
2097 break;
2098 case 11:
2099 hd_filename[2] = optarg;
2100 has_cdrom = 1;
2101 break;
2102 case 12:
2103 boot_device = optarg[0];
2104 if (boot_device != 'a' && boot_device != 'b' &&
2105 boot_device != 'c' && boot_device != 'd') {
2106 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2107 exit(1);
2109 break;
2110 case 13:
2111 fd_filename[0] = optarg;
2112 break;
2113 case 14:
2114 fd_filename[1] = optarg;
2115 break;
2116 case 15:
2117 code_copy_enabled = 0;
2118 break;
2119 case 16:
2120 nb_nics = atoi(optarg);
2121 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2122 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2123 exit(1);
2125 break;
2126 case 17:
2128 const char *p;
2129 int i;
2130 p = optarg;
2131 for(i = 0; i < 6; i++) {
2132 macaddr[i] = strtol(p, (char **)&p, 16);
2133 if (i == 5) {
2134 if (*p != '\0')
2135 goto macaddr_error;
2136 } else {
2137 if (*p != ':') {
2138 macaddr_error:
2139 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2140 exit(1);
2142 p++;
2146 break;
2147 case 18:
2148 net_if_type = NET_IF_USER;
2149 break;
2150 case 19:
2151 net_if_type = NET_IF_DUMMY;
2152 break;
2153 case 20:
2154 audio_enabled = 1;
2155 break;
2157 break;
2158 case 'h':
2159 help();
2160 break;
2161 case 'm':
2162 ram_size = atoi(optarg) * 1024 * 1024;
2163 if (ram_size <= 0)
2164 help();
2165 if (ram_size > PHYS_RAM_MAX_SIZE) {
2166 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2167 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2168 exit(1);
2170 break;
2171 case 'd':
2173 int mask;
2174 CPULogItem *item;
2176 mask = cpu_str_to_log_mask(optarg);
2177 if (!mask) {
2178 printf("Log items (comma separated):\n");
2179 for(item = cpu_log_items; item->mask != 0; item++) {
2180 printf("%-10s %s\n", item->name, item->help);
2182 exit(1);
2184 cpu_set_log(mask);
2186 break;
2187 case 'n':
2188 pstrcpy(network_script, sizeof(network_script), optarg);
2189 break;
2190 #ifdef CONFIG_GDBSTUB
2191 case 's':
2192 use_gdbstub = 1;
2193 break;
2194 case 'p':
2195 gdbstub_port = atoi(optarg);
2196 break;
2197 #endif
2198 case 'L':
2199 bios_dir = optarg;
2200 break;
2201 case 'S':
2202 start_emulation = 0;
2203 break;
2207 if (optind < argc) {
2208 hd_filename[0] = argv[optind++];
2211 linux_boot = (kernel_filename != NULL);
2213 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2214 fd_filename[0] == '\0')
2215 help();
2217 /* boot to cd by default if no hard disk */
2218 if (hd_filename[0] == '\0' && boot_device == 'c') {
2219 if (fd_filename[0] != '\0')
2220 boot_device = 'a';
2221 else
2222 boot_device = 'd';
2225 #if !defined(CONFIG_SOFTMMU)
2226 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2228 static uint8_t stdout_buf[4096];
2229 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2231 #else
2232 setvbuf(stdout, NULL, _IOLBF, 0);
2233 #endif
2235 /* init host network redirectors */
2236 if (net_if_type == -1) {
2237 net_if_type = NET_IF_TUN;
2238 #if defined(CONFIG_SLIRP)
2239 if (access(network_script, R_OK) < 0) {
2240 net_if_type = NET_IF_USER;
2242 #endif
2245 for(i = 0; i < nb_nics; i++) {
2246 NetDriverState *nd = &nd_table[i];
2247 nd->index = i;
2248 /* init virtual mac address */
2249 nd->macaddr[0] = macaddr[0];
2250 nd->macaddr[1] = macaddr[1];
2251 nd->macaddr[2] = macaddr[2];
2252 nd->macaddr[3] = macaddr[3];
2253 nd->macaddr[4] = macaddr[4];
2254 nd->macaddr[5] = macaddr[5] + i;
2255 switch(net_if_type) {
2256 #if defined(CONFIG_SLIRP)
2257 case NET_IF_USER:
2258 net_slirp_init(nd);
2259 break;
2260 #endif
2261 #if !defined(_WIN32)
2262 case NET_IF_TUN:
2263 if (i < nb_tun_fds) {
2264 net_fd_init(nd, tun_fds[i]);
2265 } else {
2266 if (net_tun_init(nd) < 0)
2267 net_dummy_init(nd);
2269 break;
2270 #endif
2271 case NET_IF_DUMMY:
2272 default:
2273 net_dummy_init(nd);
2274 break;
2278 /* init the memory */
2279 phys_ram_size = ram_size + vga_ram_size;
2281 #ifdef CONFIG_SOFTMMU
2282 #ifdef _BSD
2283 /* mallocs are always aligned on BSD. */
2284 phys_ram_base = malloc(phys_ram_size);
2285 #else
2286 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2287 #endif
2288 if (!phys_ram_base) {
2289 fprintf(stderr, "Could not allocate physical memory\n");
2290 exit(1);
2292 #else
2293 /* as we must map the same page at several addresses, we must use
2294 a fd */
2296 const char *tmpdir;
2298 tmpdir = getenv("QEMU_TMPDIR");
2299 if (!tmpdir)
2300 tmpdir = "/tmp";
2301 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2302 if (mkstemp(phys_ram_file) < 0) {
2303 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2304 phys_ram_file);
2305 exit(1);
2307 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2308 if (phys_ram_fd < 0) {
2309 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2310 phys_ram_file);
2311 exit(1);
2313 ftruncate(phys_ram_fd, phys_ram_size);
2314 unlink(phys_ram_file);
2315 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2316 phys_ram_size,
2317 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2318 phys_ram_fd, 0);
2319 if (phys_ram_base == MAP_FAILED) {
2320 fprintf(stderr, "Could not map physical memory\n");
2321 exit(1);
2324 #endif
2326 /* we always create the cdrom drive, even if no disk is there */
2327 if (has_cdrom) {
2328 bs_table[2] = bdrv_new("cdrom");
2329 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2332 /* open the virtual block devices */
2333 for(i = 0; i < MAX_DISKS; i++) {
2334 if (hd_filename[i]) {
2335 if (!bs_table[i]) {
2336 char buf[64];
2337 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2338 bs_table[i] = bdrv_new(buf);
2340 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2341 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2342 hd_filename[i]);
2343 exit(1);
2345 if (i == 0 && cyls != 0)
2346 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2350 /* we always create at least one floppy disk */
2351 fd_table[0] = bdrv_new("fda");
2352 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2354 for(i = 0; i < MAX_FD; i++) {
2355 if (fd_filename[i]) {
2356 if (!fd_table[i]) {
2357 char buf[64];
2358 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2359 fd_table[i] = bdrv_new(buf);
2360 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2362 if (fd_filename[i] != '\0') {
2363 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2364 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2365 fd_filename[i]);
2366 exit(1);
2372 /* init CPU state */
2373 env = cpu_init();
2374 global_env = env;
2375 cpu_single_env = env;
2377 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2378 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2379 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2381 init_ioports();
2382 cpu_calibrate_ticks();
2384 /* terminal init */
2385 if (nographic) {
2386 dumb_display_init(ds);
2387 } else {
2388 #ifdef CONFIG_SDL
2389 sdl_display_init(ds);
2390 #else
2391 dumb_display_init(ds);
2392 #endif
2395 /* setup cpu signal handlers for MMU / self modifying code handling */
2396 #if !defined(CONFIG_SOFTMMU)
2398 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2400 stack_t stk;
2401 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2402 stk.ss_sp = signal_stack;
2403 stk.ss_size = SIGNAL_STACK_SIZE;
2404 stk.ss_flags = 0;
2406 if (sigaltstack(&stk, NULL) < 0) {
2407 perror("sigaltstack");
2408 exit(1);
2411 #endif
2413 struct sigaction act;
2415 sigfillset(&act.sa_mask);
2416 act.sa_flags = SA_SIGINFO;
2417 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2418 act.sa_flags |= SA_ONSTACK;
2419 #endif
2420 act.sa_sigaction = host_segv_handler;
2421 sigaction(SIGSEGV, &act, NULL);
2422 sigaction(SIGBUS, &act, NULL);
2423 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2424 sigaction(SIGFPE, &act, NULL);
2425 #endif
2427 #endif
2429 #ifndef _WIN32
2431 struct sigaction act;
2432 sigfillset(&act.sa_mask);
2433 act.sa_flags = 0;
2434 act.sa_handler = SIG_IGN;
2435 sigaction(SIGPIPE, &act, NULL);
2437 #endif
2438 init_timers();
2440 #if defined(TARGET_I386)
2441 pc_init(ram_size, vga_ram_size, boot_device,
2442 ds, fd_filename, snapshot,
2443 kernel_filename, kernel_cmdline, initrd_filename);
2444 #elif defined(TARGET_PPC)
2445 ppc_init(ram_size, vga_ram_size, boot_device,
2446 ds, fd_filename, snapshot,
2447 kernel_filename, kernel_cmdline, initrd_filename);
2448 #endif
2450 /* launched after the device init so that it can display or not a
2451 banner */
2452 monitor_init();
2454 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2455 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2457 #ifdef CONFIG_GDBSTUB
2458 if (use_gdbstub) {
2459 if (gdbserver_start(gdbstub_port) < 0) {
2460 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2461 gdbstub_port);
2462 exit(1);
2463 } else {
2464 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2466 } else
2467 #endif
2468 if (start_emulation)
2470 vm_start();
2472 term_init();
2473 main_loop();
2474 quit_timers();
2475 return 0;