IDE1 init fix
[qemu.git] / vl.c
blobcdebcc2035d8f596deb96d5bf17ee93e0db1bddd
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;
132 int rtc_utc = 1;
133 int cirrus_vga_enabled = 0;
135 /***********************************************************/
136 /* x86 ISA bus support */
138 target_phys_addr_t isa_mem_base = 0;
140 uint32_t default_ioport_readb(void *opaque, uint32_t address)
142 #ifdef DEBUG_UNUSED_IOPORT
143 fprintf(stderr, "inb: port=0x%04x\n", address);
144 #endif
145 return 0xff;
148 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
152 #endif
155 /* default is to make two byte accesses */
156 uint32_t default_ioport_readw(void *opaque, uint32_t address)
158 uint32_t data;
159 data = ioport_read_table[0][address](ioport_opaque[address], address);
160 address = (address + 1) & (MAX_IOPORTS - 1);
161 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
162 return data;
165 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
167 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
168 address = (address + 1) & (MAX_IOPORTS - 1);
169 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
172 uint32_t default_ioport_readl(void *opaque, uint32_t address)
174 #ifdef DEBUG_UNUSED_IOPORT
175 fprintf(stderr, "inl: port=0x%04x\n", address);
176 #endif
177 return 0xffffffff;
180 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
182 #ifdef DEBUG_UNUSED_IOPORT
183 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
184 #endif
187 void init_ioports(void)
189 int i;
191 for(i = 0; i < MAX_IOPORTS; i++) {
192 ioport_read_table[0][i] = default_ioport_readb;
193 ioport_write_table[0][i] = default_ioport_writeb;
194 ioport_read_table[1][i] = default_ioport_readw;
195 ioport_write_table[1][i] = default_ioport_writew;
196 ioport_read_table[2][i] = default_ioport_readl;
197 ioport_write_table[2][i] = default_ioport_writel;
201 /* size is the word size in byte */
202 int register_ioport_read(int start, int length, int size,
203 IOPortReadFunc *func, void *opaque)
205 int i, bsize;
207 if (size == 1) {
208 bsize = 0;
209 } else if (size == 2) {
210 bsize = 1;
211 } else if (size == 4) {
212 bsize = 2;
213 } else {
214 hw_error("register_ioport_read: invalid size");
215 return -1;
217 for(i = start; i < start + length; i += size) {
218 ioport_read_table[bsize][i] = func;
219 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
220 hw_error("register_ioport_read: invalid opaque");
221 ioport_opaque[i] = opaque;
223 return 0;
226 /* size is the word size in byte */
227 int register_ioport_write(int start, int length, int size,
228 IOPortWriteFunc *func, void *opaque)
230 int i, bsize;
232 if (size == 1) {
233 bsize = 0;
234 } else if (size == 2) {
235 bsize = 1;
236 } else if (size == 4) {
237 bsize = 2;
238 } else {
239 hw_error("register_ioport_write: invalid size");
240 return -1;
242 for(i = start; i < start + length; i += size) {
243 ioport_write_table[bsize][i] = func;
244 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
245 hw_error("register_ioport_read: invalid opaque");
246 ioport_opaque[i] = opaque;
248 return 0;
251 void isa_unassign_ioport(int start, int length)
253 int i;
255 for(i = start; i < start + length; i++) {
256 ioport_read_table[0][i] = default_ioport_readb;
257 ioport_read_table[1][i] = default_ioport_readw;
258 ioport_read_table[2][i] = default_ioport_readl;
260 ioport_write_table[0][i] = default_ioport_writeb;
261 ioport_write_table[1][i] = default_ioport_writew;
262 ioport_write_table[2][i] = default_ioport_writel;
266 void pstrcpy(char *buf, int buf_size, const char *str)
268 int c;
269 char *q = buf;
271 if (buf_size <= 0)
272 return;
274 for(;;) {
275 c = *str++;
276 if (c == 0 || q >= buf + buf_size - 1)
277 break;
278 *q++ = c;
280 *q = '\0';
283 /* strcat and truncate. */
284 char *pstrcat(char *buf, int buf_size, const char *s)
286 int len;
287 len = strlen(buf);
288 if (len < buf_size)
289 pstrcpy(buf + len, buf_size - len, s);
290 return buf;
293 /* return the size or -1 if error */
294 int load_image(const char *filename, uint8_t *addr)
296 int fd, size;
297 fd = open(filename, O_RDONLY | O_BINARY);
298 if (fd < 0)
299 return -1;
300 size = lseek(fd, 0, SEEK_END);
301 lseek(fd, 0, SEEK_SET);
302 if (read(fd, addr, size) != size) {
303 close(fd);
304 return -1;
306 close(fd);
307 return size;
310 void cpu_outb(CPUState *env, int addr, int val)
312 #ifdef DEBUG_IOPORT
313 if (loglevel & CPU_LOG_IOPORT)
314 fprintf(logfile, "outb: %04x %02x\n", addr, val);
315 #endif
316 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
319 void cpu_outw(CPUState *env, int addr, int val)
321 #ifdef DEBUG_IOPORT
322 if (loglevel & CPU_LOG_IOPORT)
323 fprintf(logfile, "outw: %04x %04x\n", addr, val);
324 #endif
325 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
328 void cpu_outl(CPUState *env, int addr, int val)
330 #ifdef DEBUG_IOPORT
331 if (loglevel & CPU_LOG_IOPORT)
332 fprintf(logfile, "outl: %04x %08x\n", addr, val);
333 #endif
334 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
337 int cpu_inb(CPUState *env, int addr)
339 int val;
340 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
341 #ifdef DEBUG_IOPORT
342 if (loglevel & CPU_LOG_IOPORT)
343 fprintf(logfile, "inb : %04x %02x\n", addr, val);
344 #endif
345 return val;
348 int cpu_inw(CPUState *env, int addr)
350 int val;
351 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
352 #ifdef DEBUG_IOPORT
353 if (loglevel & CPU_LOG_IOPORT)
354 fprintf(logfile, "inw : %04x %04x\n", addr, val);
355 #endif
356 return val;
359 int cpu_inl(CPUState *env, int addr)
361 int val;
362 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
363 #ifdef DEBUG_IOPORT
364 if (loglevel & CPU_LOG_IOPORT)
365 fprintf(logfile, "inl : %04x %08x\n", addr, val);
366 #endif
367 return val;
370 /***********************************************************/
371 void hw_error(const char *fmt, ...)
373 va_list ap;
375 va_start(ap, fmt);
376 fprintf(stderr, "qemu: hardware error: ");
377 vfprintf(stderr, fmt, ap);
378 fprintf(stderr, "\n");
379 #ifdef TARGET_I386
380 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
381 #else
382 cpu_dump_state(global_env, stderr, 0);
383 #endif
384 va_end(ap);
385 abort();
388 /***********************************************************/
389 /* keyboard/mouse */
391 static QEMUPutKBDEvent *qemu_put_kbd_event;
392 static void *qemu_put_kbd_event_opaque;
393 static QEMUPutMouseEvent *qemu_put_mouse_event;
394 static void *qemu_put_mouse_event_opaque;
396 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
398 qemu_put_kbd_event_opaque = opaque;
399 qemu_put_kbd_event = func;
402 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
404 qemu_put_mouse_event_opaque = opaque;
405 qemu_put_mouse_event = func;
408 void kbd_put_keycode(int keycode)
410 if (qemu_put_kbd_event) {
411 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
415 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
417 if (qemu_put_mouse_event) {
418 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
419 dx, dy, dz, buttons_state);
423 /***********************************************************/
424 /* timers */
426 #if defined(__powerpc__)
428 static inline uint32_t get_tbl(void)
430 uint32_t tbl;
431 asm volatile("mftb %0" : "=r" (tbl));
432 return tbl;
435 static inline uint32_t get_tbu(void)
437 uint32_t tbl;
438 asm volatile("mftbu %0" : "=r" (tbl));
439 return tbl;
442 int64_t cpu_get_real_ticks(void)
444 uint32_t l, h, h1;
445 /* NOTE: we test if wrapping has occurred */
446 do {
447 h = get_tbu();
448 l = get_tbl();
449 h1 = get_tbu();
450 } while (h != h1);
451 return ((int64_t)h << 32) | l;
454 #elif defined(__i386__)
456 int64_t cpu_get_real_ticks(void)
458 int64_t val;
459 asm volatile ("rdtsc" : "=A" (val));
460 return val;
463 #elif defined(__x86_64__)
465 int64_t cpu_get_real_ticks(void)
467 uint32_t low,high;
468 int64_t val;
469 asm volatile("rdtsc" : "=a" (low), "=d" (high));
470 val = high;
471 val <<= 32;
472 val |= low;
473 return val;
476 #else
477 #error unsupported CPU
478 #endif
480 static int64_t cpu_ticks_offset;
481 static int cpu_ticks_enabled;
483 static inline int64_t cpu_get_ticks(void)
485 if (!cpu_ticks_enabled) {
486 return cpu_ticks_offset;
487 } else {
488 return cpu_get_real_ticks() + cpu_ticks_offset;
492 /* enable cpu_get_ticks() */
493 void cpu_enable_ticks(void)
495 if (!cpu_ticks_enabled) {
496 cpu_ticks_offset -= cpu_get_real_ticks();
497 cpu_ticks_enabled = 1;
501 /* disable cpu_get_ticks() : the clock is stopped. You must not call
502 cpu_get_ticks() after that. */
503 void cpu_disable_ticks(void)
505 if (cpu_ticks_enabled) {
506 cpu_ticks_offset = cpu_get_ticks();
507 cpu_ticks_enabled = 0;
511 static int64_t get_clock(void)
513 #ifdef _WIN32
514 struct _timeb tb;
515 _ftime(&tb);
516 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
517 #else
518 struct timeval tv;
519 gettimeofday(&tv, NULL);
520 return tv.tv_sec * 1000000LL + tv.tv_usec;
521 #endif
524 void cpu_calibrate_ticks(void)
526 int64_t usec, ticks;
528 usec = get_clock();
529 ticks = cpu_get_real_ticks();
530 #ifdef _WIN32
531 Sleep(50);
532 #else
533 usleep(50 * 1000);
534 #endif
535 usec = get_clock() - usec;
536 ticks = cpu_get_real_ticks() - ticks;
537 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
540 /* compute with 96 bit intermediate result: (a*b)/c */
541 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
543 union {
544 uint64_t ll;
545 struct {
546 #ifdef WORDS_BIGENDIAN
547 uint32_t high, low;
548 #else
549 uint32_t low, high;
550 #endif
551 } l;
552 } u, res;
553 uint64_t rl, rh;
555 u.ll = a;
556 rl = (uint64_t)u.l.low * (uint64_t)b;
557 rh = (uint64_t)u.l.high * (uint64_t)b;
558 rh += (rl >> 32);
559 res.l.high = rh / c;
560 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
561 return res.ll;
564 #define QEMU_TIMER_REALTIME 0
565 #define QEMU_TIMER_VIRTUAL 1
567 struct QEMUClock {
568 int type;
569 /* XXX: add frequency */
572 struct QEMUTimer {
573 QEMUClock *clock;
574 int64_t expire_time;
575 QEMUTimerCB *cb;
576 void *opaque;
577 struct QEMUTimer *next;
580 QEMUClock *rt_clock;
581 QEMUClock *vm_clock;
583 static QEMUTimer *active_timers[2];
584 #ifdef _WIN32
585 static MMRESULT timerID;
586 #else
587 /* frequency of the times() clock tick */
588 static int timer_freq;
589 #endif
591 QEMUClock *qemu_new_clock(int type)
593 QEMUClock *clock;
594 clock = qemu_mallocz(sizeof(QEMUClock));
595 if (!clock)
596 return NULL;
597 clock->type = type;
598 return clock;
601 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
603 QEMUTimer *ts;
605 ts = qemu_mallocz(sizeof(QEMUTimer));
606 ts->clock = clock;
607 ts->cb = cb;
608 ts->opaque = opaque;
609 return ts;
612 void qemu_free_timer(QEMUTimer *ts)
614 qemu_free(ts);
617 /* stop a timer, but do not dealloc it */
618 void qemu_del_timer(QEMUTimer *ts)
620 QEMUTimer **pt, *t;
622 /* NOTE: this code must be signal safe because
623 qemu_timer_expired() can be called from a signal. */
624 pt = &active_timers[ts->clock->type];
625 for(;;) {
626 t = *pt;
627 if (!t)
628 break;
629 if (t == ts) {
630 *pt = t->next;
631 break;
633 pt = &t->next;
637 /* modify the current timer so that it will be fired when current_time
638 >= expire_time. The corresponding callback will be called. */
639 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
641 QEMUTimer **pt, *t;
643 qemu_del_timer(ts);
645 /* add the timer in the sorted list */
646 /* NOTE: this code must be signal safe because
647 qemu_timer_expired() can be called from a signal. */
648 pt = &active_timers[ts->clock->type];
649 for(;;) {
650 t = *pt;
651 if (!t)
652 break;
653 if (t->expire_time > expire_time)
654 break;
655 pt = &t->next;
657 ts->expire_time = expire_time;
658 ts->next = *pt;
659 *pt = ts;
662 int qemu_timer_pending(QEMUTimer *ts)
664 QEMUTimer *t;
665 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
666 if (t == ts)
667 return 1;
669 return 0;
672 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
674 if (!timer_head)
675 return 0;
676 return (timer_head->expire_time <= current_time);
679 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
681 QEMUTimer *ts;
683 for(;;) {
684 ts = *ptimer_head;
685 if (ts->expire_time > current_time)
686 break;
687 /* remove timer from the list before calling the callback */
688 *ptimer_head = ts->next;
689 ts->next = NULL;
691 /* run the callback (the timer list can be modified) */
692 ts->cb(ts->opaque);
696 int64_t qemu_get_clock(QEMUClock *clock)
698 switch(clock->type) {
699 case QEMU_TIMER_REALTIME:
700 #ifdef _WIN32
701 return GetTickCount();
702 #else
704 struct tms tp;
706 /* Note that using gettimeofday() is not a good solution
707 for timers because its value change when the date is
708 modified. */
709 if (timer_freq == 100) {
710 return times(&tp) * 10;
711 } else {
712 return ((int64_t)times(&tp) * 1000) / timer_freq;
715 #endif
716 default:
717 case QEMU_TIMER_VIRTUAL:
718 return cpu_get_ticks();
722 /* save a timer */
723 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
725 uint64_t expire_time;
727 if (qemu_timer_pending(ts)) {
728 expire_time = ts->expire_time;
729 } else {
730 expire_time = -1;
732 qemu_put_be64(f, expire_time);
735 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
737 uint64_t expire_time;
739 expire_time = qemu_get_be64(f);
740 if (expire_time != -1) {
741 qemu_mod_timer(ts, expire_time);
742 } else {
743 qemu_del_timer(ts);
747 static void timer_save(QEMUFile *f, void *opaque)
749 if (cpu_ticks_enabled) {
750 hw_error("cannot save state if virtual timers are running");
752 qemu_put_be64s(f, &cpu_ticks_offset);
753 qemu_put_be64s(f, &ticks_per_sec);
756 static int timer_load(QEMUFile *f, void *opaque, int version_id)
758 if (version_id != 1)
759 return -EINVAL;
760 if (cpu_ticks_enabled) {
761 return -EINVAL;
763 qemu_get_be64s(f, &cpu_ticks_offset);
764 qemu_get_be64s(f, &ticks_per_sec);
765 return 0;
768 #ifdef _WIN32
769 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
770 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
771 #else
772 static void host_alarm_handler(int host_signum)
773 #endif
775 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
776 qemu_get_clock(vm_clock)) ||
777 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
778 qemu_get_clock(rt_clock))) {
779 /* stop the cpu because a timer occured */
780 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
784 #ifndef _WIN32
786 #if defined(__linux__)
788 #define RTC_FREQ 1024
790 static int rtc_fd;
792 static int start_rtc_timer(void)
794 rtc_fd = open("/dev/rtc", O_RDONLY);
795 if (rtc_fd < 0)
796 return -1;
797 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
798 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
799 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
800 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
801 goto fail;
803 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
804 fail:
805 close(rtc_fd);
806 return -1;
808 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
809 return 0;
812 #else
814 static int start_rtc_timer(void)
816 return -1;
819 #endif /* !defined(__linux__) */
821 #endif /* !defined(_WIN32) */
823 static void init_timers(void)
825 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
826 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
828 #ifdef _WIN32
830 int count=0;
831 timerID = timeSetEvent(10, // interval (ms)
832 0, // resolution
833 host_alarm_handler, // function
834 (DWORD)&count, // user parameter
835 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
836 if( !timerID ) {
837 perror("failed timer alarm");
838 exit(1);
841 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
842 #else
844 struct sigaction act;
845 struct itimerval itv;
847 /* get times() syscall frequency */
848 timer_freq = sysconf(_SC_CLK_TCK);
850 /* timer signal */
851 sigfillset(&act.sa_mask);
852 act.sa_flags = 0;
853 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
854 act.sa_flags |= SA_ONSTACK;
855 #endif
856 act.sa_handler = host_alarm_handler;
857 sigaction(SIGALRM, &act, NULL);
859 itv.it_interval.tv_sec = 0;
860 itv.it_interval.tv_usec = 1000;
861 itv.it_value.tv_sec = 0;
862 itv.it_value.tv_usec = 10 * 1000;
863 setitimer(ITIMER_REAL, &itv, NULL);
864 /* we probe the tick duration of the kernel to inform the user if
865 the emulated kernel requested a too high timer frequency */
866 getitimer(ITIMER_REAL, &itv);
868 if (itv.it_interval.tv_usec > 1000) {
869 /* try to use /dev/rtc to have a faster timer */
870 if (start_rtc_timer() < 0)
871 goto use_itimer;
872 /* disable itimer */
873 itv.it_interval.tv_sec = 0;
874 itv.it_interval.tv_usec = 0;
875 itv.it_value.tv_sec = 0;
876 itv.it_value.tv_usec = 0;
877 setitimer(ITIMER_REAL, &itv, NULL);
879 /* use the RTC */
880 sigaction(SIGIO, &act, NULL);
881 fcntl(rtc_fd, F_SETFL, O_ASYNC);
882 fcntl(rtc_fd, F_SETOWN, getpid());
883 } else {
884 use_itimer:
885 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
886 PIT_FREQ) / 1000000;
889 #endif
892 void quit_timers(void)
894 #ifdef _WIN32
895 timeKillEvent(timerID);
896 #endif
899 /***********************************************************/
900 /* serial device */
902 #ifdef _WIN32
904 int serial_open_device(void)
906 return -1;
909 #else
911 int serial_open_device(void)
913 char slave_name[1024];
914 int master_fd, slave_fd;
916 if (serial_console == NULL && nographic) {
917 /* use console for serial port */
918 return 0;
919 } else {
920 #if 0
921 /* Not satisfying */
922 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
923 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
924 return -1;
926 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
927 return master_fd;
928 #else
929 return -1;
930 #endif
934 #endif
936 /***********************************************************/
937 /* Linux network device redirectors */
939 void hex_dump(FILE *f, const uint8_t *buf, int size)
941 int len, i, j, c;
943 for(i=0;i<size;i+=16) {
944 len = size - i;
945 if (len > 16)
946 len = 16;
947 fprintf(f, "%08x ", i);
948 for(j=0;j<16;j++) {
949 if (j < len)
950 fprintf(f, " %02x", buf[i+j]);
951 else
952 fprintf(f, " ");
954 fprintf(f, " ");
955 for(j=0;j<len;j++) {
956 c = buf[i+j];
957 if (c < ' ' || c > '~')
958 c = '.';
959 fprintf(f, "%c", c);
961 fprintf(f, "\n");
965 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
967 nd->send_packet(nd, buf, size);
970 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
971 IOReadHandler *fd_read, void *opaque)
973 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
976 /* dummy network adapter */
978 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
982 static void dummy_add_read_packet(NetDriverState *nd,
983 IOCanRWHandler *fd_can_read,
984 IOReadHandler *fd_read, void *opaque)
988 static int net_dummy_init(NetDriverState *nd)
990 nd->send_packet = dummy_send_packet;
991 nd->add_read_packet = dummy_add_read_packet;
992 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
993 return 0;
996 #if defined(CONFIG_SLIRP)
998 /* slirp network adapter */
1000 static void *slirp_fd_opaque;
1001 static IOCanRWHandler *slirp_fd_can_read;
1002 static IOReadHandler *slirp_fd_read;
1003 static int slirp_inited;
1005 int slirp_can_output(void)
1007 return slirp_fd_can_read(slirp_fd_opaque);
1010 void slirp_output(const uint8_t *pkt, int pkt_len)
1012 #if 0
1013 printf("output:\n");
1014 hex_dump(stdout, pkt, pkt_len);
1015 #endif
1016 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1019 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1021 #if 0
1022 printf("input:\n");
1023 hex_dump(stdout, buf, size);
1024 #endif
1025 slirp_input(buf, size);
1028 static void slirp_add_read_packet(NetDriverState *nd,
1029 IOCanRWHandler *fd_can_read,
1030 IOReadHandler *fd_read, void *opaque)
1032 slirp_fd_opaque = opaque;
1033 slirp_fd_can_read = fd_can_read;
1034 slirp_fd_read = fd_read;
1037 static int net_slirp_init(NetDriverState *nd)
1039 if (!slirp_inited) {
1040 slirp_inited = 1;
1041 slirp_init();
1043 nd->send_packet = slirp_send_packet;
1044 nd->add_read_packet = slirp_add_read_packet;
1045 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1046 return 0;
1049 #endif /* CONFIG_SLIRP */
1051 #if !defined(_WIN32)
1052 #ifdef _BSD
1053 static int tun_open(char *ifname, int ifname_size)
1055 int fd;
1056 char *dev;
1057 struct stat s;
1059 fd = open("/dev/tap", O_RDWR);
1060 if (fd < 0) {
1061 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1062 return -1;
1065 fstat(fd, &s);
1066 dev = devname(s.st_rdev, S_IFCHR);
1067 pstrcpy(ifname, ifname_size, dev);
1069 fcntl(fd, F_SETFL, O_NONBLOCK);
1070 return fd;
1072 #else
1073 static int tun_open(char *ifname, int ifname_size)
1075 struct ifreq ifr;
1076 int fd, ret;
1078 fd = open("/dev/net/tun", O_RDWR);
1079 if (fd < 0) {
1080 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1081 return -1;
1083 memset(&ifr, 0, sizeof(ifr));
1084 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1085 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1086 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1087 if (ret != 0) {
1088 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1089 close(fd);
1090 return -1;
1092 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1093 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1094 fcntl(fd, F_SETFL, O_NONBLOCK);
1095 return fd;
1097 #endif
1099 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1101 write(nd->fd, buf, size);
1104 static void tun_add_read_packet(NetDriverState *nd,
1105 IOCanRWHandler *fd_can_read,
1106 IOReadHandler *fd_read, void *opaque)
1108 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1111 static int net_tun_init(NetDriverState *nd)
1113 int pid, status;
1114 char *args[3];
1115 char **parg;
1117 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1118 if (nd->fd < 0)
1119 return -1;
1121 /* try to launch network init script */
1122 pid = fork();
1123 if (pid >= 0) {
1124 if (pid == 0) {
1125 parg = args;
1126 *parg++ = network_script;
1127 *parg++ = nd->ifname;
1128 *parg++ = NULL;
1129 execv(network_script, args);
1130 exit(1);
1132 while (waitpid(pid, &status, 0) != pid);
1133 if (!WIFEXITED(status) ||
1134 WEXITSTATUS(status) != 0) {
1135 fprintf(stderr, "%s: could not launch network script\n",
1136 network_script);
1139 nd->send_packet = tun_send_packet;
1140 nd->add_read_packet = tun_add_read_packet;
1141 return 0;
1144 static int net_fd_init(NetDriverState *nd, int fd)
1146 nd->fd = fd;
1147 nd->send_packet = tun_send_packet;
1148 nd->add_read_packet = tun_add_read_packet;
1149 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1150 return 0;
1153 #endif /* !_WIN32 */
1155 /***********************************************************/
1156 /* dumb display */
1158 #ifdef _WIN32
1160 static void term_exit(void)
1164 static void term_init(void)
1168 #else
1170 /* init terminal so that we can grab keys */
1171 static struct termios oldtty;
1173 static void term_exit(void)
1175 tcsetattr (0, TCSANOW, &oldtty);
1178 static void term_init(void)
1180 struct termios tty;
1182 tcgetattr (0, &tty);
1183 oldtty = tty;
1185 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1186 |INLCR|IGNCR|ICRNL|IXON);
1187 tty.c_oflag |= OPOST;
1188 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1189 /* if graphical mode, we allow Ctrl-C handling */
1190 if (nographic)
1191 tty.c_lflag &= ~ISIG;
1192 tty.c_cflag &= ~(CSIZE|PARENB);
1193 tty.c_cflag |= CS8;
1194 tty.c_cc[VMIN] = 1;
1195 tty.c_cc[VTIME] = 0;
1197 tcsetattr (0, TCSANOW, &tty);
1199 atexit(term_exit);
1201 fcntl(0, F_SETFL, O_NONBLOCK);
1204 #endif
1206 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1210 static void dumb_resize(DisplayState *ds, int w, int h)
1214 static void dumb_refresh(DisplayState *ds)
1216 vga_update_display();
1219 void dumb_display_init(DisplayState *ds)
1221 ds->data = NULL;
1222 ds->linesize = 0;
1223 ds->depth = 0;
1224 ds->dpy_update = dumb_update;
1225 ds->dpy_resize = dumb_resize;
1226 ds->dpy_refresh = dumb_refresh;
1229 #if !defined(CONFIG_SOFTMMU)
1230 /***********************************************************/
1231 /* cpu signal handler */
1232 static void host_segv_handler(int host_signum, siginfo_t *info,
1233 void *puc)
1235 if (cpu_signal_handler(host_signum, info, puc))
1236 return;
1237 term_exit();
1238 abort();
1240 #endif
1242 /***********************************************************/
1243 /* I/O handling */
1245 #define MAX_IO_HANDLERS 64
1247 typedef struct IOHandlerRecord {
1248 int fd;
1249 IOCanRWHandler *fd_can_read;
1250 IOReadHandler *fd_read;
1251 void *opaque;
1252 /* temporary data */
1253 struct pollfd *ufd;
1254 int max_size;
1255 struct IOHandlerRecord *next;
1256 } IOHandlerRecord;
1258 static IOHandlerRecord *first_io_handler;
1260 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1261 IOReadHandler *fd_read, void *opaque)
1263 IOHandlerRecord *ioh;
1265 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1266 if (!ioh)
1267 return -1;
1268 ioh->fd = fd;
1269 ioh->fd_can_read = fd_can_read;
1270 ioh->fd_read = fd_read;
1271 ioh->opaque = opaque;
1272 ioh->next = first_io_handler;
1273 first_io_handler = ioh;
1274 return 0;
1277 void qemu_del_fd_read_handler(int fd)
1279 IOHandlerRecord **pioh, *ioh;
1281 pioh = &first_io_handler;
1282 for(;;) {
1283 ioh = *pioh;
1284 if (ioh == NULL)
1285 break;
1286 if (ioh->fd == fd) {
1287 *pioh = ioh->next;
1288 break;
1290 pioh = &ioh->next;
1294 /***********************************************************/
1295 /* savevm/loadvm support */
1297 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1299 fwrite(buf, 1, size, f);
1302 void qemu_put_byte(QEMUFile *f, int v)
1304 fputc(v, f);
1307 void qemu_put_be16(QEMUFile *f, unsigned int v)
1309 qemu_put_byte(f, v >> 8);
1310 qemu_put_byte(f, v);
1313 void qemu_put_be32(QEMUFile *f, unsigned int v)
1315 qemu_put_byte(f, v >> 24);
1316 qemu_put_byte(f, v >> 16);
1317 qemu_put_byte(f, v >> 8);
1318 qemu_put_byte(f, v);
1321 void qemu_put_be64(QEMUFile *f, uint64_t v)
1323 qemu_put_be32(f, v >> 32);
1324 qemu_put_be32(f, v);
1327 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1329 return fread(buf, 1, size, f);
1332 int qemu_get_byte(QEMUFile *f)
1334 int v;
1335 v = fgetc(f);
1336 if (v == EOF)
1337 return 0;
1338 else
1339 return v;
1342 unsigned int qemu_get_be16(QEMUFile *f)
1344 unsigned int v;
1345 v = qemu_get_byte(f) << 8;
1346 v |= qemu_get_byte(f);
1347 return v;
1350 unsigned int qemu_get_be32(QEMUFile *f)
1352 unsigned int v;
1353 v = qemu_get_byte(f) << 24;
1354 v |= qemu_get_byte(f) << 16;
1355 v |= qemu_get_byte(f) << 8;
1356 v |= qemu_get_byte(f);
1357 return v;
1360 uint64_t qemu_get_be64(QEMUFile *f)
1362 uint64_t v;
1363 v = (uint64_t)qemu_get_be32(f) << 32;
1364 v |= qemu_get_be32(f);
1365 return v;
1368 int64_t qemu_ftell(QEMUFile *f)
1370 return ftell(f);
1373 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1375 if (fseek(f, pos, whence) < 0)
1376 return -1;
1377 return ftell(f);
1380 typedef struct SaveStateEntry {
1381 char idstr[256];
1382 int instance_id;
1383 int version_id;
1384 SaveStateHandler *save_state;
1385 LoadStateHandler *load_state;
1386 void *opaque;
1387 struct SaveStateEntry *next;
1388 } SaveStateEntry;
1390 static SaveStateEntry *first_se;
1392 int register_savevm(const char *idstr,
1393 int instance_id,
1394 int version_id,
1395 SaveStateHandler *save_state,
1396 LoadStateHandler *load_state,
1397 void *opaque)
1399 SaveStateEntry *se, **pse;
1401 se = qemu_malloc(sizeof(SaveStateEntry));
1402 if (!se)
1403 return -1;
1404 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1405 se->instance_id = instance_id;
1406 se->version_id = version_id;
1407 se->save_state = save_state;
1408 se->load_state = load_state;
1409 se->opaque = opaque;
1410 se->next = NULL;
1412 /* add at the end of list */
1413 pse = &first_se;
1414 while (*pse != NULL)
1415 pse = &(*pse)->next;
1416 *pse = se;
1417 return 0;
1420 #define QEMU_VM_FILE_MAGIC 0x5145564d
1421 #define QEMU_VM_FILE_VERSION 0x00000001
1423 int qemu_savevm(const char *filename)
1425 SaveStateEntry *se;
1426 QEMUFile *f;
1427 int len, len_pos, cur_pos, saved_vm_running, ret;
1429 saved_vm_running = vm_running;
1430 vm_stop(0);
1432 f = fopen(filename, "wb");
1433 if (!f) {
1434 ret = -1;
1435 goto the_end;
1438 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1439 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1441 for(se = first_se; se != NULL; se = se->next) {
1442 /* ID string */
1443 len = strlen(se->idstr);
1444 qemu_put_byte(f, len);
1445 qemu_put_buffer(f, se->idstr, len);
1447 qemu_put_be32(f, se->instance_id);
1448 qemu_put_be32(f, se->version_id);
1450 /* record size: filled later */
1451 len_pos = ftell(f);
1452 qemu_put_be32(f, 0);
1454 se->save_state(f, se->opaque);
1456 /* fill record size */
1457 cur_pos = ftell(f);
1458 len = ftell(f) - len_pos - 4;
1459 fseek(f, len_pos, SEEK_SET);
1460 qemu_put_be32(f, len);
1461 fseek(f, cur_pos, SEEK_SET);
1464 fclose(f);
1465 ret = 0;
1466 the_end:
1467 if (saved_vm_running)
1468 vm_start();
1469 return ret;
1472 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1474 SaveStateEntry *se;
1476 for(se = first_se; se != NULL; se = se->next) {
1477 if (!strcmp(se->idstr, idstr) &&
1478 instance_id == se->instance_id)
1479 return se;
1481 return NULL;
1484 int qemu_loadvm(const char *filename)
1486 SaveStateEntry *se;
1487 QEMUFile *f;
1488 int len, cur_pos, ret, instance_id, record_len, version_id;
1489 int saved_vm_running;
1490 unsigned int v;
1491 char idstr[256];
1493 saved_vm_running = vm_running;
1494 vm_stop(0);
1496 f = fopen(filename, "rb");
1497 if (!f) {
1498 ret = -1;
1499 goto the_end;
1502 v = qemu_get_be32(f);
1503 if (v != QEMU_VM_FILE_MAGIC)
1504 goto fail;
1505 v = qemu_get_be32(f);
1506 if (v != QEMU_VM_FILE_VERSION) {
1507 fail:
1508 fclose(f);
1509 ret = -1;
1510 goto the_end;
1512 for(;;) {
1513 #if defined (DO_TB_FLUSH)
1514 tb_flush(global_env);
1515 #endif
1516 len = qemu_get_byte(f);
1517 if (feof(f))
1518 break;
1519 qemu_get_buffer(f, idstr, len);
1520 idstr[len] = '\0';
1521 instance_id = qemu_get_be32(f);
1522 version_id = qemu_get_be32(f);
1523 record_len = qemu_get_be32(f);
1524 #if 0
1525 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1526 idstr, instance_id, version_id, record_len);
1527 #endif
1528 cur_pos = ftell(f);
1529 se = find_se(idstr, instance_id);
1530 if (!se) {
1531 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1532 instance_id, idstr);
1533 } else {
1534 ret = se->load_state(f, se->opaque, version_id);
1535 if (ret < 0) {
1536 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1537 instance_id, idstr);
1540 /* always seek to exact end of record */
1541 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1543 fclose(f);
1544 ret = 0;
1545 the_end:
1546 if (saved_vm_running)
1547 vm_start();
1548 return ret;
1551 /***********************************************************/
1552 /* cpu save/restore */
1554 #if defined(TARGET_I386)
1556 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1558 qemu_put_be32(f, (uint32_t)dt->base);
1559 qemu_put_be32(f, dt->limit);
1560 qemu_put_be32(f, dt->flags);
1563 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1565 dt->base = (uint8_t *)qemu_get_be32(f);
1566 dt->limit = qemu_get_be32(f);
1567 dt->flags = qemu_get_be32(f);
1570 void cpu_save(QEMUFile *f, void *opaque)
1572 CPUState *env = opaque;
1573 uint16_t fptag, fpus, fpuc;
1574 uint32_t hflags;
1575 int i;
1577 for(i = 0; i < 8; i++)
1578 qemu_put_be32s(f, &env->regs[i]);
1579 qemu_put_be32s(f, &env->eip);
1580 qemu_put_be32s(f, &env->eflags);
1581 qemu_put_be32s(f, &env->eflags);
1582 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1583 qemu_put_be32s(f, &hflags);
1585 /* FPU */
1586 fpuc = env->fpuc;
1587 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1588 fptag = 0;
1589 for (i=7; i>=0; i--) {
1590 fptag <<= 2;
1591 if (env->fptags[i]) {
1592 fptag |= 3;
1596 qemu_put_be16s(f, &fpuc);
1597 qemu_put_be16s(f, &fpus);
1598 qemu_put_be16s(f, &fptag);
1600 for(i = 0; i < 8; i++) {
1601 uint64_t mant;
1602 uint16_t exp;
1603 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1604 qemu_put_be64(f, mant);
1605 qemu_put_be16(f, exp);
1608 for(i = 0; i < 6; i++)
1609 cpu_put_seg(f, &env->segs[i]);
1610 cpu_put_seg(f, &env->ldt);
1611 cpu_put_seg(f, &env->tr);
1612 cpu_put_seg(f, &env->gdt);
1613 cpu_put_seg(f, &env->idt);
1615 qemu_put_be32s(f, &env->sysenter_cs);
1616 qemu_put_be32s(f, &env->sysenter_esp);
1617 qemu_put_be32s(f, &env->sysenter_eip);
1619 qemu_put_be32s(f, &env->cr[0]);
1620 qemu_put_be32s(f, &env->cr[2]);
1621 qemu_put_be32s(f, &env->cr[3]);
1622 qemu_put_be32s(f, &env->cr[4]);
1624 for(i = 0; i < 8; i++)
1625 qemu_put_be32s(f, &env->dr[i]);
1627 /* MMU */
1628 qemu_put_be32s(f, &env->a20_mask);
1631 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1633 CPUState *env = opaque;
1634 int i;
1635 uint32_t hflags;
1636 uint16_t fpus, fpuc, fptag;
1638 if (version_id != 1)
1639 return -EINVAL;
1640 for(i = 0; i < 8; i++)
1641 qemu_get_be32s(f, &env->regs[i]);
1642 qemu_get_be32s(f, &env->eip);
1643 qemu_get_be32s(f, &env->eflags);
1644 qemu_get_be32s(f, &env->eflags);
1645 qemu_get_be32s(f, &hflags);
1647 qemu_get_be16s(f, &fpuc);
1648 qemu_get_be16s(f, &fpus);
1649 qemu_get_be16s(f, &fptag);
1651 for(i = 0; i < 8; i++) {
1652 uint64_t mant;
1653 uint16_t exp;
1654 mant = qemu_get_be64(f);
1655 exp = qemu_get_be16(f);
1656 env->fpregs[i] = cpu_set_fp80(mant, exp);
1659 env->fpuc = fpuc;
1660 env->fpstt = (fpus >> 11) & 7;
1661 env->fpus = fpus & ~0x3800;
1662 for(i = 0; i < 8; i++) {
1663 env->fptags[i] = ((fptag & 3) == 3);
1664 fptag >>= 2;
1667 for(i = 0; i < 6; i++)
1668 cpu_get_seg(f, &env->segs[i]);
1669 cpu_get_seg(f, &env->ldt);
1670 cpu_get_seg(f, &env->tr);
1671 cpu_get_seg(f, &env->gdt);
1672 cpu_get_seg(f, &env->idt);
1674 qemu_get_be32s(f, &env->sysenter_cs);
1675 qemu_get_be32s(f, &env->sysenter_esp);
1676 qemu_get_be32s(f, &env->sysenter_eip);
1678 qemu_get_be32s(f, &env->cr[0]);
1679 qemu_get_be32s(f, &env->cr[2]);
1680 qemu_get_be32s(f, &env->cr[3]);
1681 qemu_get_be32s(f, &env->cr[4]);
1683 for(i = 0; i < 8; i++)
1684 qemu_get_be32s(f, &env->dr[i]);
1686 /* MMU */
1687 qemu_get_be32s(f, &env->a20_mask);
1689 /* XXX: compute hflags from scratch, except for CPL and IIF */
1690 env->hflags = hflags;
1691 tlb_flush(env, 1);
1692 return 0;
1695 #elif defined(TARGET_PPC)
1696 void cpu_save(QEMUFile *f, void *opaque)
1700 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1702 return 0;
1704 #else
1706 #warning No CPU save/restore functions
1708 #endif
1710 /***********************************************************/
1711 /* ram save/restore */
1713 /* we just avoid storing empty pages */
1714 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1716 int i, v;
1718 v = buf[0];
1719 for(i = 1; i < len; i++) {
1720 if (buf[i] != v)
1721 goto normal_save;
1723 qemu_put_byte(f, 1);
1724 qemu_put_byte(f, v);
1725 return;
1726 normal_save:
1727 qemu_put_byte(f, 0);
1728 qemu_put_buffer(f, buf, len);
1731 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1733 int v;
1735 v = qemu_get_byte(f);
1736 switch(v) {
1737 case 0:
1738 if (qemu_get_buffer(f, buf, len) != len)
1739 return -EIO;
1740 break;
1741 case 1:
1742 v = qemu_get_byte(f);
1743 memset(buf, v, len);
1744 break;
1745 default:
1746 return -EINVAL;
1748 return 0;
1751 static void ram_save(QEMUFile *f, void *opaque)
1753 int i;
1754 qemu_put_be32(f, phys_ram_size);
1755 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1756 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1760 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1762 int i, ret;
1764 if (version_id != 1)
1765 return -EINVAL;
1766 if (qemu_get_be32(f) != phys_ram_size)
1767 return -EINVAL;
1768 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1769 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1770 if (ret)
1771 return ret;
1773 return 0;
1776 /***********************************************************/
1777 /* main execution loop */
1779 void gui_update(void *opaque)
1781 display_state.dpy_refresh(&display_state);
1782 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1785 /* XXX: support several handlers */
1786 VMStopHandler *vm_stop_cb;
1787 VMStopHandler *vm_stop_opaque;
1789 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1791 vm_stop_cb = cb;
1792 vm_stop_opaque = opaque;
1793 return 0;
1796 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1798 vm_stop_cb = NULL;
1801 void vm_start(void)
1803 if (!vm_running) {
1804 cpu_enable_ticks();
1805 vm_running = 1;
1809 void vm_stop(int reason)
1811 if (vm_running) {
1812 cpu_disable_ticks();
1813 vm_running = 0;
1814 if (reason != 0) {
1815 if (vm_stop_cb) {
1816 vm_stop_cb(vm_stop_opaque, reason);
1822 int main_loop(void)
1824 #ifndef _WIN32
1825 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1826 IOHandlerRecord *ioh, *ioh_next;
1827 uint8_t buf[4096];
1828 int n, max_size;
1829 #endif
1830 int ret, timeout;
1831 CPUState *env = global_env;
1833 for(;;) {
1834 if (vm_running) {
1835 ret = cpu_exec(env);
1836 if (reset_requested) {
1837 ret = EXCP_INTERRUPT;
1838 break;
1840 if (ret == EXCP_DEBUG) {
1841 vm_stop(EXCP_DEBUG);
1843 /* if hlt instruction, we wait until the next IRQ */
1844 /* XXX: use timeout computed from timers */
1845 if (ret == EXCP_HLT)
1846 timeout = 10;
1847 else
1848 timeout = 0;
1849 } else {
1850 timeout = 10;
1853 #ifdef _WIN32
1854 if (timeout > 0)
1855 Sleep(timeout);
1856 #else
1858 /* poll any events */
1859 /* XXX: separate device handlers from system ones */
1860 pf = ufds;
1861 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1862 if (!ioh->fd_can_read) {
1863 max_size = 0;
1864 pf->fd = ioh->fd;
1865 pf->events = POLLIN;
1866 ioh->ufd = pf;
1867 pf++;
1868 } else {
1869 max_size = ioh->fd_can_read(ioh->opaque);
1870 if (max_size > 0) {
1871 if (max_size > sizeof(buf))
1872 max_size = sizeof(buf);
1873 pf->fd = ioh->fd;
1874 pf->events = POLLIN;
1875 ioh->ufd = pf;
1876 pf++;
1877 } else {
1878 ioh->ufd = NULL;
1881 ioh->max_size = max_size;
1884 ret = poll(ufds, pf - ufds, timeout);
1885 if (ret > 0) {
1886 /* XXX: better handling of removal */
1887 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1888 ioh_next = ioh->next;
1889 pf = ioh->ufd;
1890 if (pf) {
1891 if (pf->revents & POLLIN) {
1892 if (ioh->max_size == 0) {
1893 /* just a read event */
1894 ioh->fd_read(ioh->opaque, NULL, 0);
1895 } else {
1896 n = read(ioh->fd, buf, ioh->max_size);
1897 if (n >= 0) {
1898 ioh->fd_read(ioh->opaque, buf, n);
1899 } else if (errno != EAGAIN) {
1900 ioh->fd_read(ioh->opaque, NULL, -errno);
1908 #if defined(CONFIG_SLIRP)
1909 /* XXX: merge with poll() */
1910 if (slirp_inited) {
1911 fd_set rfds, wfds, xfds;
1912 int nfds;
1913 struct timeval tv;
1915 nfds = -1;
1916 FD_ZERO(&rfds);
1917 FD_ZERO(&wfds);
1918 FD_ZERO(&xfds);
1919 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1920 tv.tv_sec = 0;
1921 tv.tv_usec = 0;
1922 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1923 if (ret >= 0) {
1924 slirp_select_poll(&rfds, &wfds, &xfds);
1927 #endif
1929 #endif
1931 if (vm_running) {
1932 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1933 qemu_get_clock(vm_clock));
1935 if (audio_enabled) {
1936 /* XXX: add explicit timer */
1937 SB16_run();
1940 /* run dma transfers, if any */
1941 DMA_run();
1944 /* real time timers */
1945 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1946 qemu_get_clock(rt_clock));
1948 cpu_disable_ticks();
1949 return ret;
1952 void help(void)
1954 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
1955 "usage: %s [options] [disk_image]\n"
1956 "\n"
1957 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1958 "\n"
1959 "Standard options:\n"
1960 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1961 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1962 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1963 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1964 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1965 "-snapshot write to temporary files instead of disk image files\n"
1966 "-m megs set virtual RAM size to megs MB [default=%d]\n"
1967 "-nographic disable graphical output and redirect serial I/Os to console\n"
1968 "-enable-audio enable audio support\n"
1969 "-localtime set the real time clock to local time [default=utc]\n"
1970 "\n"
1971 "Network options:\n"
1972 "-nics n simulate 'n' network cards [default=1]\n"
1973 "-macaddr addr set the mac address of the first interface\n"
1974 "-n script set tap/tun network init script [default=%s]\n"
1975 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1976 #ifdef CONFIG_SLIRP
1977 "-user-net use user mode network stack [default if no tap/tun script]\n"
1978 #endif
1979 "-dummy-net use dummy network stack\n"
1980 "\n"
1981 "Linux boot specific:\n"
1982 "-kernel bzImage use 'bzImage' as kernel image\n"
1983 "-append cmdline use 'cmdline' as kernel command line\n"
1984 "-initrd file use 'file' as initial ram disk\n"
1985 "\n"
1986 "Debug/Expert options:\n"
1987 "-S freeze CPU at startup (use 'c' to start execution)\n"
1988 "-s wait gdb connection to port %d\n"
1989 "-p port change gdb connection port\n"
1990 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1991 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1992 "-L path set the directory for the BIOS and VGA BIOS\n"
1993 #ifdef USE_CODE_COPY
1994 "-no-code-copy disable code copy acceleration\n"
1995 #endif
1997 "\n"
1998 "During emulation, use C-a h to get terminal commands:\n",
1999 #ifdef CONFIG_SOFTMMU
2000 "qemu",
2001 #else
2002 "qemu-fast",
2003 #endif
2004 DEFAULT_RAM_SIZE,
2005 DEFAULT_NETWORK_SCRIPT,
2006 DEFAULT_GDBSTUB_PORT,
2007 "/tmp/qemu.log");
2008 term_print_help();
2009 #ifndef CONFIG_SOFTMMU
2010 printf("\n"
2011 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2012 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2013 "PC emulation.\n");
2014 #endif
2015 exit(1);
2018 #define HAS_ARG 0x0001
2020 enum {
2021 QEMU_OPTION_h,
2023 QEMU_OPTION_fda,
2024 QEMU_OPTION_fdb,
2025 QEMU_OPTION_hda,
2026 QEMU_OPTION_hdb,
2027 QEMU_OPTION_hdc,
2028 QEMU_OPTION_hdd,
2029 QEMU_OPTION_cdrom,
2030 QEMU_OPTION_boot,
2031 QEMU_OPTION_snapshot,
2032 QEMU_OPTION_m,
2033 QEMU_OPTION_nographic,
2034 QEMU_OPTION_enable_audio,
2036 QEMU_OPTION_nics,
2037 QEMU_OPTION_macaddr,
2038 QEMU_OPTION_n,
2039 QEMU_OPTION_tun_fd,
2040 QEMU_OPTION_user_net,
2041 QEMU_OPTION_dummy_net,
2043 QEMU_OPTION_kernel,
2044 QEMU_OPTION_append,
2045 QEMU_OPTION_initrd,
2047 QEMU_OPTION_S,
2048 QEMU_OPTION_s,
2049 QEMU_OPTION_p,
2050 QEMU_OPTION_d,
2051 QEMU_OPTION_hdachs,
2052 QEMU_OPTION_L,
2053 QEMU_OPTION_no_code_copy,
2054 QEMU_OPTION_pci,
2055 QEMU_OPTION_prep,
2056 QEMU_OPTION_localtime,
2057 QEMU_OPTION_cirrusvga,
2060 typedef struct QEMUOption {
2061 const char *name;
2062 int flags;
2063 int index;
2064 } QEMUOption;
2066 const QEMUOption qemu_options[] = {
2067 { "h", 0, QEMU_OPTION_h },
2069 { "fda", HAS_ARG, QEMU_OPTION_fda },
2070 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2071 { "hda", HAS_ARG, QEMU_OPTION_hda },
2072 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2073 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2074 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2075 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2076 { "boot", HAS_ARG, QEMU_OPTION_boot },
2077 { "snapshot", 0, QEMU_OPTION_snapshot },
2078 { "m", HAS_ARG, QEMU_OPTION_m },
2079 { "nographic", 0, QEMU_OPTION_nographic },
2080 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2082 { "nics", HAS_ARG, QEMU_OPTION_nics},
2083 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2084 { "n", HAS_ARG, QEMU_OPTION_n },
2085 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2086 #ifdef CONFIG_SLIRP
2087 { "user-net", 0, QEMU_OPTION_user_net },
2088 #endif
2089 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2091 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2092 { "append", HAS_ARG, QEMU_OPTION_append },
2093 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2095 { "S", 0, QEMU_OPTION_S },
2096 { "s", 0, QEMU_OPTION_s },
2097 { "p", HAS_ARG, QEMU_OPTION_p },
2098 { "d", HAS_ARG, QEMU_OPTION_d },
2099 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2100 { "L", HAS_ARG, QEMU_OPTION_L },
2101 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2102 #ifdef TARGET_PPC
2103 { "prep", 0, QEMU_OPTION_prep },
2104 #endif
2105 { "localtime", 0, QEMU_OPTION_localtime },
2107 /* temporary options */
2108 { "pci", 0, QEMU_OPTION_pci },
2109 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2110 { NULL },
2113 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2115 /* this stack is only used during signal handling */
2116 #define SIGNAL_STACK_SIZE 32768
2118 static uint8_t *signal_stack;
2120 #endif
2122 #define NET_IF_TUN 0
2123 #define NET_IF_USER 1
2124 #define NET_IF_DUMMY 2
2126 int main(int argc, char **argv)
2128 #ifdef CONFIG_GDBSTUB
2129 int use_gdbstub, gdbstub_port;
2130 #endif
2131 int i, has_cdrom;
2132 int snapshot, linux_boot;
2133 CPUState *env;
2134 const char *initrd_filename;
2135 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2136 const char *kernel_filename, *kernel_cmdline;
2137 DisplayState *ds = &display_state;
2138 int cyls, heads, secs;
2139 int start_emulation = 1;
2140 uint8_t macaddr[6];
2141 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2142 int optind;
2143 const char *r, *optarg;
2145 #if !defined(CONFIG_SOFTMMU)
2146 /* we never want that malloc() uses mmap() */
2147 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2148 #endif
2149 initrd_filename = NULL;
2150 for(i = 0; i < MAX_FD; i++)
2151 fd_filename[i] = NULL;
2152 for(i = 0; i < MAX_DISKS; i++)
2153 hd_filename[i] = NULL;
2154 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2155 vga_ram_size = VGA_RAM_SIZE;
2156 bios_size = BIOS_SIZE;
2157 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2158 #ifdef CONFIG_GDBSTUB
2159 use_gdbstub = 0;
2160 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2161 #endif
2162 snapshot = 0;
2163 nographic = 0;
2164 kernel_filename = NULL;
2165 kernel_cmdline = "";
2166 has_cdrom = 1;
2167 cyls = heads = secs = 0;
2169 nb_tun_fds = 0;
2170 net_if_type = -1;
2171 nb_nics = 1;
2172 /* default mac address of the first network interface */
2173 macaddr[0] = 0x52;
2174 macaddr[1] = 0x54;
2175 macaddr[2] = 0x00;
2176 macaddr[3] = 0x12;
2177 macaddr[4] = 0x34;
2178 macaddr[5] = 0x56;
2180 optind = 1;
2181 for(;;) {
2182 if (optind >= argc)
2183 break;
2184 r = argv[optind];
2185 if (r[0] != '-') {
2186 hd_filename[0] = argv[optind++];
2187 } else {
2188 const QEMUOption *popt;
2190 optind++;
2191 popt = qemu_options;
2192 for(;;) {
2193 if (!popt->name) {
2194 fprintf(stderr, "%s: invalid option -- '%s'\n",
2195 argv[0], r);
2196 exit(1);
2198 if (!strcmp(popt->name, r + 1))
2199 break;
2200 popt++;
2202 if (popt->flags & HAS_ARG) {
2203 if (optind >= argc) {
2204 fprintf(stderr, "%s: option '%s' requires an argument\n",
2205 argv[0], r);
2206 exit(1);
2208 optarg = argv[optind++];
2209 } else {
2210 optarg = NULL;
2213 switch(popt->index) {
2214 case QEMU_OPTION_initrd:
2215 initrd_filename = optarg;
2216 break;
2217 case QEMU_OPTION_hda:
2218 hd_filename[0] = optarg;
2219 break;
2220 case QEMU_OPTION_hdb:
2221 hd_filename[1] = optarg;
2222 break;
2223 case QEMU_OPTION_snapshot:
2224 snapshot = 1;
2225 break;
2226 case QEMU_OPTION_hdachs:
2228 const char *p;
2229 p = optarg;
2230 cyls = strtol(p, (char **)&p, 0);
2231 if (*p != ',')
2232 goto chs_fail;
2233 p++;
2234 heads = strtol(p, (char **)&p, 0);
2235 if (*p != ',')
2236 goto chs_fail;
2237 p++;
2238 secs = strtol(p, (char **)&p, 0);
2239 if (*p != '\0') {
2240 chs_fail:
2241 cyls = 0;
2244 break;
2245 case QEMU_OPTION_nographic:
2246 nographic = 1;
2247 break;
2248 case QEMU_OPTION_kernel:
2249 kernel_filename = optarg;
2250 break;
2251 case QEMU_OPTION_append:
2252 kernel_cmdline = optarg;
2253 break;
2254 case QEMU_OPTION_tun_fd:
2256 const char *p;
2257 int fd;
2258 net_if_type = NET_IF_TUN;
2259 if (nb_tun_fds < MAX_NICS) {
2260 fd = strtol(optarg, (char **)&p, 0);
2261 if (*p != '\0') {
2262 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2263 exit(1);
2265 tun_fds[nb_tun_fds++] = fd;
2268 break;
2269 case QEMU_OPTION_hdc:
2270 hd_filename[2] = optarg;
2271 has_cdrom = 0;
2272 break;
2273 case QEMU_OPTION_hdd:
2274 hd_filename[3] = optarg;
2275 break;
2276 case QEMU_OPTION_cdrom:
2277 hd_filename[2] = optarg;
2278 has_cdrom = 1;
2279 break;
2280 case QEMU_OPTION_boot:
2281 boot_device = optarg[0];
2282 if (boot_device != 'a' && boot_device != 'b' &&
2283 boot_device != 'c' && boot_device != 'd') {
2284 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2285 exit(1);
2287 break;
2288 case QEMU_OPTION_fda:
2289 fd_filename[0] = optarg;
2290 break;
2291 case QEMU_OPTION_fdb:
2292 fd_filename[1] = optarg;
2293 break;
2294 case QEMU_OPTION_no_code_copy:
2295 code_copy_enabled = 0;
2296 break;
2297 case QEMU_OPTION_nics:
2298 nb_nics = atoi(optarg);
2299 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2300 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2301 exit(1);
2303 break;
2304 case QEMU_OPTION_macaddr:
2306 const char *p;
2307 int i;
2308 p = optarg;
2309 for(i = 0; i < 6; i++) {
2310 macaddr[i] = strtol(p, (char **)&p, 16);
2311 if (i == 5) {
2312 if (*p != '\0')
2313 goto macaddr_error;
2314 } else {
2315 if (*p != ':') {
2316 macaddr_error:
2317 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2318 exit(1);
2320 p++;
2324 break;
2325 case QEMU_OPTION_user_net:
2326 net_if_type = NET_IF_USER;
2327 break;
2328 case QEMU_OPTION_dummy_net:
2329 net_if_type = NET_IF_DUMMY;
2330 break;
2331 case QEMU_OPTION_enable_audio:
2332 audio_enabled = 1;
2333 break;
2334 case QEMU_OPTION_h:
2335 help();
2336 break;
2337 case QEMU_OPTION_m:
2338 ram_size = atoi(optarg) * 1024 * 1024;
2339 if (ram_size <= 0)
2340 help();
2341 if (ram_size > PHYS_RAM_MAX_SIZE) {
2342 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2343 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2344 exit(1);
2346 break;
2347 case QEMU_OPTION_d:
2349 int mask;
2350 CPULogItem *item;
2352 mask = cpu_str_to_log_mask(optarg);
2353 if (!mask) {
2354 printf("Log items (comma separated):\n");
2355 for(item = cpu_log_items; item->mask != 0; item++) {
2356 printf("%-10s %s\n", item->name, item->help);
2358 exit(1);
2360 cpu_set_log(mask);
2362 break;
2363 case QEMU_OPTION_n:
2364 pstrcpy(network_script, sizeof(network_script), optarg);
2365 break;
2366 #ifdef CONFIG_GDBSTUB
2367 case QEMU_OPTION_s:
2368 use_gdbstub = 1;
2369 break;
2370 case QEMU_OPTION_p:
2371 gdbstub_port = atoi(optarg);
2372 break;
2373 #endif
2374 case QEMU_OPTION_L:
2375 bios_dir = optarg;
2376 break;
2377 case QEMU_OPTION_S:
2378 start_emulation = 0;
2379 break;
2380 case QEMU_OPTION_pci:
2381 pci_enabled = 1;
2382 break;
2383 case QEMU_OPTION_prep:
2384 prep_enabled = 1;
2385 break;
2386 case QEMU_OPTION_localtime:
2387 rtc_utc = 0;
2388 break;
2389 case QEMU_OPTION_cirrusvga:
2390 cirrus_vga_enabled = 1;
2391 break;
2396 linux_boot = (kernel_filename != NULL);
2398 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2399 fd_filename[0] == '\0')
2400 help();
2402 /* boot to cd by default if no hard disk */
2403 if (hd_filename[0] == '\0' && boot_device == 'c') {
2404 if (fd_filename[0] != '\0')
2405 boot_device = 'a';
2406 else
2407 boot_device = 'd';
2410 #if !defined(CONFIG_SOFTMMU)
2411 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2413 static uint8_t stdout_buf[4096];
2414 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2416 #else
2417 setvbuf(stdout, NULL, _IOLBF, 0);
2418 #endif
2420 /* init host network redirectors */
2421 if (net_if_type == -1) {
2422 net_if_type = NET_IF_TUN;
2423 #if defined(CONFIG_SLIRP)
2424 if (access(network_script, R_OK) < 0) {
2425 net_if_type = NET_IF_USER;
2427 #endif
2430 for(i = 0; i < nb_nics; i++) {
2431 NetDriverState *nd = &nd_table[i];
2432 nd->index = i;
2433 /* init virtual mac address */
2434 nd->macaddr[0] = macaddr[0];
2435 nd->macaddr[1] = macaddr[1];
2436 nd->macaddr[2] = macaddr[2];
2437 nd->macaddr[3] = macaddr[3];
2438 nd->macaddr[4] = macaddr[4];
2439 nd->macaddr[5] = macaddr[5] + i;
2440 switch(net_if_type) {
2441 #if defined(CONFIG_SLIRP)
2442 case NET_IF_USER:
2443 net_slirp_init(nd);
2444 break;
2445 #endif
2446 #if !defined(_WIN32)
2447 case NET_IF_TUN:
2448 if (i < nb_tun_fds) {
2449 net_fd_init(nd, tun_fds[i]);
2450 } else {
2451 if (net_tun_init(nd) < 0)
2452 net_dummy_init(nd);
2454 break;
2455 #endif
2456 case NET_IF_DUMMY:
2457 default:
2458 net_dummy_init(nd);
2459 break;
2463 /* init the memory */
2464 phys_ram_size = ram_size + vga_ram_size + bios_size;
2466 #ifdef CONFIG_SOFTMMU
2467 #ifdef _BSD
2468 /* mallocs are always aligned on BSD. */
2469 phys_ram_base = malloc(phys_ram_size);
2470 #else
2471 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2472 #endif
2473 if (!phys_ram_base) {
2474 fprintf(stderr, "Could not allocate physical memory\n");
2475 exit(1);
2477 #else
2478 /* as we must map the same page at several addresses, we must use
2479 a fd */
2481 const char *tmpdir;
2483 tmpdir = getenv("QEMU_TMPDIR");
2484 if (!tmpdir)
2485 tmpdir = "/tmp";
2486 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2487 if (mkstemp(phys_ram_file) < 0) {
2488 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2489 phys_ram_file);
2490 exit(1);
2492 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2493 if (phys_ram_fd < 0) {
2494 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2495 phys_ram_file);
2496 exit(1);
2498 ftruncate(phys_ram_fd, phys_ram_size);
2499 unlink(phys_ram_file);
2500 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2501 phys_ram_size,
2502 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2503 phys_ram_fd, 0);
2504 if (phys_ram_base == MAP_FAILED) {
2505 fprintf(stderr, "Could not map physical memory\n");
2506 exit(1);
2509 #endif
2511 /* we always create the cdrom drive, even if no disk is there */
2512 if (has_cdrom) {
2513 bs_table[2] = bdrv_new("cdrom");
2514 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2517 /* open the virtual block devices */
2518 for(i = 0; i < MAX_DISKS; i++) {
2519 if (hd_filename[i]) {
2520 if (!bs_table[i]) {
2521 char buf[64];
2522 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2523 bs_table[i] = bdrv_new(buf);
2525 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2526 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2527 hd_filename[i]);
2528 exit(1);
2530 if (i == 0 && cyls != 0)
2531 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2535 /* we always create at least one floppy disk */
2536 fd_table[0] = bdrv_new("fda");
2537 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2539 for(i = 0; i < MAX_FD; i++) {
2540 if (fd_filename[i]) {
2541 if (!fd_table[i]) {
2542 char buf[64];
2543 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2544 fd_table[i] = bdrv_new(buf);
2545 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2547 if (fd_filename[i] != '\0') {
2548 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2549 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2550 fd_filename[i]);
2551 exit(1);
2557 /* init CPU state */
2558 env = cpu_init();
2559 global_env = env;
2560 cpu_single_env = env;
2562 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2563 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2564 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2566 init_ioports();
2567 cpu_calibrate_ticks();
2569 /* terminal init */
2570 if (nographic) {
2571 dumb_display_init(ds);
2572 } else {
2573 #ifdef CONFIG_SDL
2574 sdl_display_init(ds);
2575 #else
2576 dumb_display_init(ds);
2577 #endif
2580 /* setup cpu signal handlers for MMU / self modifying code handling */
2581 #if !defined(CONFIG_SOFTMMU)
2583 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2585 stack_t stk;
2586 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2587 stk.ss_sp = signal_stack;
2588 stk.ss_size = SIGNAL_STACK_SIZE;
2589 stk.ss_flags = 0;
2591 if (sigaltstack(&stk, NULL) < 0) {
2592 perror("sigaltstack");
2593 exit(1);
2596 #endif
2598 struct sigaction act;
2600 sigfillset(&act.sa_mask);
2601 act.sa_flags = SA_SIGINFO;
2602 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2603 act.sa_flags |= SA_ONSTACK;
2604 #endif
2605 act.sa_sigaction = host_segv_handler;
2606 sigaction(SIGSEGV, &act, NULL);
2607 sigaction(SIGBUS, &act, NULL);
2608 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2609 sigaction(SIGFPE, &act, NULL);
2610 #endif
2612 #endif
2614 #ifndef _WIN32
2616 struct sigaction act;
2617 sigfillset(&act.sa_mask);
2618 act.sa_flags = 0;
2619 act.sa_handler = SIG_IGN;
2620 sigaction(SIGPIPE, &act, NULL);
2622 #endif
2623 init_timers();
2625 #if defined(TARGET_I386)
2626 pc_init(ram_size, vga_ram_size, boot_device,
2627 ds, fd_filename, snapshot,
2628 kernel_filename, kernel_cmdline, initrd_filename);
2629 #elif defined(TARGET_PPC)
2630 ppc_init(ram_size, vga_ram_size, boot_device,
2631 ds, fd_filename, snapshot,
2632 kernel_filename, kernel_cmdline, initrd_filename);
2633 #endif
2635 /* launched after the device init so that it can display or not a
2636 banner */
2637 monitor_init();
2639 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2640 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2642 #ifdef CONFIG_GDBSTUB
2643 if (use_gdbstub) {
2644 if (gdbserver_start(gdbstub_port) < 0) {
2645 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2646 gdbstub_port);
2647 exit(1);
2648 } else {
2649 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2651 } else
2652 #endif
2653 if (start_emulation)
2655 vm_start();
2657 term_init();
2658 main_loop();
2659 quit_timers();
2660 return 0;