4 * Copyright (c) 2003-2004 Fabrice Bellard
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
36 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
45 #include <linux/if_tun.h>
48 #if defined(CONFIG_SLIRP)
53 #include <sys/timeb.h>
55 #define getopt_long_only getopt_long
56 #define memalign(align, size) malloc(size)
60 /* SDL use the pthreads and they modify sigaction. We don't
62 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
63 extern void __libc_sigaction();
64 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
66 extern void __sigaction();
67 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
69 #endif /* CONFIG_SDL */
77 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
79 //#define DEBUG_UNUSED_IOPORT
81 #if !defined(CONFIG_SOFTMMU)
82 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
84 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
88 #define GUI_REFRESH_INTERVAL 30
90 /* XXX: use a two level table to limit memory usage */
91 #define MAX_IOPORTS 65536
93 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
94 char phys_ram_file[1024];
96 CPUState *cpu_single_env;
97 void *ioport_opaque[MAX_IOPORTS];
98 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
99 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
100 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
102 static DisplayState display_state;
104 int64_t ticks_per_sec;
105 int boot_device = 'c';
107 static char network_script[1024];
108 int pit_min_timer_count = 0;
110 NetDriverState nd_table[MAX_NICS];
111 SerialState *serial_console;
112 QEMUTimer *gui_timer;
114 int audio_enabled = 0;
116 /***********************************************************/
117 /* x86 ISA bus support */
119 target_phys_addr_t isa_mem_base = 0;
121 uint32_t default_ioport_readb(void *opaque, uint32_t address)
123 #ifdef DEBUG_UNUSED_IOPORT
124 fprintf(stderr, "inb: port=0x%04x\n", address);
129 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
131 #ifdef DEBUG_UNUSED_IOPORT
132 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
136 /* default is to make two byte accesses */
137 uint32_t default_ioport_readw(void *opaque, uint32_t address)
140 data = ioport_read_table[0][address & (MAX_IOPORTS - 1)](opaque, address);
141 data |= ioport_read_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1) << 8;
145 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
147 ioport_write_table[0][address & (MAX_IOPORTS - 1)](opaque, address, data & 0xff);
148 ioport_write_table[0][(address + 1) & (MAX_IOPORTS - 1)](opaque, address + 1, (data >> 8) & 0xff);
151 uint32_t default_ioport_readl(void *opaque, uint32_t address)
153 #ifdef DEBUG_UNUSED_IOPORT
154 fprintf(stderr, "inl: port=0x%04x\n", address);
159 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
161 #ifdef DEBUG_UNUSED_IOPORT
162 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
166 void init_ioports(void)
170 for(i = 0; i < MAX_IOPORTS; i++) {
171 ioport_read_table[0][i] = default_ioport_readb;
172 ioport_write_table[0][i] = default_ioport_writeb;
173 ioport_read_table[1][i] = default_ioport_readw;
174 ioport_write_table[1][i] = default_ioport_writew;
175 ioport_read_table[2][i] = default_ioport_readl;
176 ioport_write_table[2][i] = default_ioport_writel;
180 /* size is the word size in byte */
181 int register_ioport_read(int start, int length, int size,
182 IOPortReadFunc *func, void *opaque)
188 } else if (size == 2) {
190 } else if (size == 4) {
193 hw_error("register_ioport_read: invalid size");
196 for(i = start; i < start + length; i += size) {
197 ioport_read_table[bsize][i] = func;
198 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
199 hw_error("register_ioport_read: invalid opaque");
200 ioport_opaque[i] = opaque;
205 /* size is the word size in byte */
206 int register_ioport_write(int start, int length, int size,
207 IOPortWriteFunc *func, void *opaque)
213 } else if (size == 2) {
215 } else if (size == 4) {
218 hw_error("register_ioport_write: invalid size");
221 for(i = start; i < start + length; i += size) {
222 ioport_write_table[bsize][i] = func;
223 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
224 hw_error("register_ioport_read: invalid opaque");
225 ioport_opaque[i] = opaque;
230 void pstrcpy(char *buf, int buf_size, const char *str)
240 if (c == 0 || q >= buf + buf_size - 1)
247 /* strcat and truncate. */
248 char *pstrcat(char *buf, int buf_size, const char *s)
253 pstrcpy(buf + len, buf_size - len, s);
257 /* return the size or -1 if error */
258 int load_image(const char *filename, uint8_t *addr)
261 fd = open(filename, O_RDONLY | O_BINARY);
264 size = lseek(fd, 0, SEEK_END);
265 lseek(fd, 0, SEEK_SET);
266 if (read(fd, addr, size) != size) {
274 void cpu_outb(CPUState *env, int addr, int val)
276 addr &= (MAX_IOPORTS - 1);
277 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
280 void cpu_outw(CPUState *env, int addr, int val)
282 addr &= (MAX_IOPORTS - 1);
283 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
286 void cpu_outl(CPUState *env, int addr, int val)
288 addr &= (MAX_IOPORTS - 1);
289 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
292 int cpu_inb(CPUState *env, int addr)
294 addr &= (MAX_IOPORTS - 1);
295 return ioport_read_table[0][addr](ioport_opaque[addr], addr);
298 int cpu_inw(CPUState *env, int addr)
300 addr &= (MAX_IOPORTS - 1);
301 return ioport_read_table[1][addr](ioport_opaque[addr], addr);
304 int cpu_inl(CPUState *env, int addr)
306 addr &= (MAX_IOPORTS - 1);
307 return ioport_read_table[2][addr](ioport_opaque[addr], addr);
310 /***********************************************************/
311 void hw_error(const char *fmt, ...)
316 fprintf(stderr, "qemu: hardware error: ");
317 vfprintf(stderr, fmt, ap);
318 fprintf(stderr, "\n");
320 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
322 cpu_dump_state(global_env, stderr, 0);
328 /***********************************************************/
331 #if defined(__powerpc__)
333 static inline uint32_t get_tbl(void)
336 asm volatile("mftb %0" : "=r" (tbl));
340 static inline uint32_t get_tbu(void)
343 asm volatile("mftbu %0" : "=r" (tbl));
347 int64_t cpu_get_real_ticks(void)
350 /* NOTE: we test if wrapping has occurred */
356 return ((int64_t)h << 32) | l;
359 #elif defined(__i386__)
361 int64_t cpu_get_real_ticks(void)
364 asm volatile ("rdtsc" : "=A" (val));
368 #elif defined(__x86_64__)
370 int64_t cpu_get_real_ticks(void)
374 asm volatile("rdtsc" : "=a" (low), "=d" (high));
382 #error unsupported CPU
385 static int64_t cpu_ticks_offset;
386 static int cpu_ticks_enabled;
388 static inline int64_t cpu_get_ticks(void)
390 if (!cpu_ticks_enabled) {
391 return cpu_ticks_offset;
393 return cpu_get_real_ticks() + cpu_ticks_offset;
397 /* enable cpu_get_ticks() */
398 void cpu_enable_ticks(void)
400 if (!cpu_ticks_enabled) {
401 cpu_ticks_offset -= cpu_get_real_ticks();
402 cpu_ticks_enabled = 1;
406 /* disable cpu_get_ticks() : the clock is stopped. You must not call
407 cpu_get_ticks() after that. */
408 void cpu_disable_ticks(void)
410 if (cpu_ticks_enabled) {
411 cpu_ticks_offset = cpu_get_ticks();
412 cpu_ticks_enabled = 0;
416 static int64_t get_clock(void)
421 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
424 gettimeofday(&tv, NULL);
425 return tv.tv_sec * 1000000LL + tv.tv_usec;
429 void cpu_calibrate_ticks(void)
434 ticks = cpu_get_real_ticks();
440 usec = get_clock() - usec;
441 ticks = cpu_get_real_ticks() - ticks;
442 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
445 /* compute with 96 bit intermediate result: (a*b)/c */
446 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
451 #ifdef WORDS_BIGENDIAN
461 rl = (uint64_t)u.l.low * (uint64_t)b;
462 rh = (uint64_t)u.l.high * (uint64_t)b;
465 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
469 #define QEMU_TIMER_REALTIME 0
470 #define QEMU_TIMER_VIRTUAL 1
474 /* XXX: add frequency */
482 struct QEMUTimer *next;
488 static QEMUTimer *active_timers[2];
490 static MMRESULT timerID;
492 /* frequency of the times() clock tick */
493 static int timer_freq;
496 QEMUClock *qemu_new_clock(int type)
499 clock = qemu_mallocz(sizeof(QEMUClock));
506 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
510 ts = qemu_mallocz(sizeof(QEMUTimer));
517 void qemu_free_timer(QEMUTimer *ts)
522 /* stop a timer, but do not dealloc it */
523 void qemu_del_timer(QEMUTimer *ts)
527 /* NOTE: this code must be signal safe because
528 qemu_timer_expired() can be called from a signal. */
529 pt = &active_timers[ts->clock->type];
542 /* modify the current timer so that it will be fired when current_time
543 >= expire_time. The corresponding callback will be called. */
544 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
550 /* add the timer in the sorted list */
551 /* NOTE: this code must be signal safe because
552 qemu_timer_expired() can be called from a signal. */
553 pt = &active_timers[ts->clock->type];
558 if (t->expire_time > expire_time)
562 ts->expire_time = expire_time;
567 int qemu_timer_pending(QEMUTimer *ts)
570 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
577 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
581 return (timer_head->expire_time <= current_time);
584 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
590 if (ts->expire_time > current_time)
592 /* remove timer from the list before calling the callback */
593 *ptimer_head = ts->next;
596 /* run the callback (the timer list can be modified) */
601 int64_t qemu_get_clock(QEMUClock *clock)
603 switch(clock->type) {
604 case QEMU_TIMER_REALTIME:
606 return GetTickCount();
608 /* XXX: portability among Linux hosts */
609 if (timer_freq == 100) {
610 return times(NULL) * 10;
612 return ((int64_t)times(NULL) * 1000) / timer_freq;
616 case QEMU_TIMER_VIRTUAL:
617 return cpu_get_ticks();
622 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
624 uint64_t expire_time;
626 if (qemu_timer_pending(ts)) {
627 expire_time = ts->expire_time;
631 qemu_put_be64(f, expire_time);
634 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
636 uint64_t expire_time;
638 expire_time = qemu_get_be64(f);
639 if (expire_time != -1) {
640 qemu_mod_timer(ts, expire_time);
646 static void timer_save(QEMUFile *f, void *opaque)
648 if (cpu_ticks_enabled) {
649 hw_error("cannot save state if virtual timers are running");
651 qemu_put_be64s(f, &cpu_ticks_offset);
652 qemu_put_be64s(f, &ticks_per_sec);
655 static int timer_load(QEMUFile *f, void *opaque, int version_id)
659 if (cpu_ticks_enabled) {
662 qemu_get_be64s(f, &cpu_ticks_offset);
663 qemu_get_be64s(f, &ticks_per_sec);
668 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
669 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
671 static void host_alarm_handler(int host_signum)
674 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
675 qemu_get_clock(vm_clock)) ||
676 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
677 qemu_get_clock(rt_clock))) {
678 /* stop the cpu because a timer occured */
679 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
683 static void init_timers(void)
685 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
686 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
691 timerID = timeSetEvent(10, // interval (ms)
693 host_alarm_handler, // function
694 (DWORD)&count, // user parameter
695 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
697 perror("failed timer alarm");
701 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
704 struct sigaction act;
705 struct itimerval itv;
707 /* get times() syscall frequency */
708 timer_freq = sysconf(_SC_CLK_TCK);
711 sigfillset(&act.sa_mask);
713 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
714 act.sa_flags |= SA_ONSTACK;
716 act.sa_handler = host_alarm_handler;
717 sigaction(SIGALRM, &act, NULL);
719 itv.it_interval.tv_sec = 0;
720 itv.it_interval.tv_usec = 1000;
721 itv.it_value.tv_sec = 0;
722 itv.it_value.tv_usec = 10 * 1000;
723 setitimer(ITIMER_REAL, &itv, NULL);
724 /* we probe the tick duration of the kernel to inform the user if
725 the emulated kernel requested a too high timer frequency */
726 getitimer(ITIMER_REAL, &itv);
727 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec * PIT_FREQ) /
733 void quit_timers(void)
736 timeKillEvent(timerID);
740 /***********************************************************/
745 int serial_open_device(void)
752 int serial_open_device(void)
754 char slave_name[1024];
755 int master_fd, slave_fd;
757 if (serial_console == NULL && nographic) {
758 /* use console for serial port */
761 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
762 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
765 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
772 /***********************************************************/
773 /* Linux network device redirectors */
775 void hex_dump(FILE *f, const uint8_t *buf, int size)
779 for(i=0;i<size;i+=16) {
783 fprintf(f, "%08x ", i);
786 fprintf(f, " %02x", buf[i+j]);
793 if (c < ' ' || c > '~')
801 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
803 nd->send_packet(nd, buf, size);
806 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
807 IOReadHandler *fd_read, void *opaque)
809 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
812 /* dummy network adapter */
814 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
818 static void dummy_add_read_packet(NetDriverState *nd,
819 IOCanRWHandler *fd_can_read,
820 IOReadHandler *fd_read, void *opaque)
824 static int net_dummy_init(NetDriverState *nd)
826 nd->send_packet = dummy_send_packet;
827 nd->add_read_packet = dummy_add_read_packet;
828 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
832 #if defined(CONFIG_SLIRP)
834 /* slirp network adapter */
836 static void *slirp_fd_opaque;
837 static IOCanRWHandler *slirp_fd_can_read;
838 static IOReadHandler *slirp_fd_read;
839 static int slirp_inited;
841 int slirp_can_output(void)
843 return slirp_fd_can_read(slirp_fd_opaque);
846 void slirp_output(const uint8_t *pkt, int pkt_len)
850 hex_dump(stdout, pkt, pkt_len);
852 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
855 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
859 hex_dump(stdout, buf, size);
861 slirp_input(buf, size);
864 static void slirp_add_read_packet(NetDriverState *nd,
865 IOCanRWHandler *fd_can_read,
866 IOReadHandler *fd_read, void *opaque)
868 slirp_fd_opaque = opaque;
869 slirp_fd_can_read = fd_can_read;
870 slirp_fd_read = fd_read;
873 static int net_slirp_init(NetDriverState *nd)
879 nd->send_packet = slirp_send_packet;
880 nd->add_read_packet = slirp_add_read_packet;
881 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
885 #endif /* CONFIG_SLIRP */
889 static int tun_open(char *ifname, int ifname_size)
894 fd = open("/dev/net/tun", O_RDWR);
896 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
899 memset(&ifr, 0, sizeof(ifr));
900 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
901 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
902 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
904 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
908 printf("Connected to host network interface: %s\n", ifr.ifr_name);
909 pstrcpy(ifname, ifname_size, ifr.ifr_name);
910 fcntl(fd, F_SETFL, O_NONBLOCK);
914 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
916 write(nd->fd, buf, size);
919 static void tun_add_read_packet(NetDriverState *nd,
920 IOCanRWHandler *fd_can_read,
921 IOReadHandler *fd_read, void *opaque)
923 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
926 static int net_tun_init(NetDriverState *nd)
932 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
936 /* try to launch network init script */
941 *parg++ = network_script;
942 *parg++ = nd->ifname;
944 execv(network_script, args);
947 while (waitpid(pid, &status, 0) != pid);
948 if (!WIFEXITED(status) ||
949 WEXITSTATUS(status) != 0) {
950 fprintf(stderr, "%s: could not launch network script\n",
954 nd->send_packet = tun_send_packet;
955 nd->add_read_packet = tun_add_read_packet;
959 static int net_fd_init(NetDriverState *nd, int fd)
962 nd->send_packet = tun_send_packet;
963 nd->add_read_packet = tun_add_read_packet;
964 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
970 /***********************************************************/
975 static void term_exit(void)
979 static void term_init(void)
985 /* init terminal so that we can grab keys */
986 static struct termios oldtty;
988 static void term_exit(void)
990 tcsetattr (0, TCSANOW, &oldtty);
993 static void term_init(void)
1000 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1001 |INLCR|IGNCR|ICRNL|IXON);
1002 tty.c_oflag |= OPOST;
1003 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1004 /* if graphical mode, we allow Ctrl-C handling */
1006 tty.c_lflag &= ~ISIG;
1007 tty.c_cflag &= ~(CSIZE|PARENB);
1010 tty.c_cc[VTIME] = 0;
1012 tcsetattr (0, TCSANOW, &tty);
1016 fcntl(0, F_SETFL, O_NONBLOCK);
1021 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1025 static void dumb_resize(DisplayState *ds, int w, int h)
1029 static void dumb_refresh(DisplayState *ds)
1031 vga_update_display();
1034 void dumb_display_init(DisplayState *ds)
1039 ds->dpy_update = dumb_update;
1040 ds->dpy_resize = dumb_resize;
1041 ds->dpy_refresh = dumb_refresh;
1044 #if !defined(CONFIG_SOFTMMU)
1045 /***********************************************************/
1046 /* cpu signal handler */
1047 static void host_segv_handler(int host_signum, siginfo_t *info,
1050 if (cpu_signal_handler(host_signum, info, puc))
1057 /***********************************************************/
1060 #define MAX_IO_HANDLERS 64
1062 typedef struct IOHandlerRecord {
1064 IOCanRWHandler *fd_can_read;
1065 IOReadHandler *fd_read;
1067 /* temporary data */
1070 struct IOHandlerRecord *next;
1073 static IOHandlerRecord *first_io_handler;
1075 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1076 IOReadHandler *fd_read, void *opaque)
1078 IOHandlerRecord *ioh;
1080 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1084 ioh->fd_can_read = fd_can_read;
1085 ioh->fd_read = fd_read;
1086 ioh->opaque = opaque;
1087 ioh->next = first_io_handler;
1088 first_io_handler = ioh;
1092 void qemu_del_fd_read_handler(int fd)
1094 IOHandlerRecord **pioh, *ioh;
1096 pioh = &first_io_handler;
1101 if (ioh->fd == fd) {
1109 /***********************************************************/
1110 /* savevm/loadvm support */
1112 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1114 fwrite(buf, 1, size, f);
1117 void qemu_put_byte(QEMUFile *f, int v)
1122 void qemu_put_be16(QEMUFile *f, unsigned int v)
1124 qemu_put_byte(f, v >> 8);
1125 qemu_put_byte(f, v);
1128 void qemu_put_be32(QEMUFile *f, unsigned int v)
1130 qemu_put_byte(f, v >> 24);
1131 qemu_put_byte(f, v >> 16);
1132 qemu_put_byte(f, v >> 8);
1133 qemu_put_byte(f, v);
1136 void qemu_put_be64(QEMUFile *f, uint64_t v)
1138 qemu_put_be32(f, v >> 32);
1139 qemu_put_be32(f, v);
1142 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1144 return fread(buf, 1, size, f);
1147 int qemu_get_byte(QEMUFile *f)
1157 unsigned int qemu_get_be16(QEMUFile *f)
1160 v = qemu_get_byte(f) << 8;
1161 v |= qemu_get_byte(f);
1165 unsigned int qemu_get_be32(QEMUFile *f)
1168 v = qemu_get_byte(f) << 24;
1169 v |= qemu_get_byte(f) << 16;
1170 v |= qemu_get_byte(f) << 8;
1171 v |= qemu_get_byte(f);
1175 uint64_t qemu_get_be64(QEMUFile *f)
1178 v = (uint64_t)qemu_get_be32(f) << 32;
1179 v |= qemu_get_be32(f);
1183 int64_t qemu_ftell(QEMUFile *f)
1188 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1190 if (fseek(f, pos, whence) < 0)
1195 typedef struct SaveStateEntry {
1199 SaveStateHandler *save_state;
1200 LoadStateHandler *load_state;
1202 struct SaveStateEntry *next;
1205 static SaveStateEntry *first_se;
1207 int register_savevm(const char *idstr,
1210 SaveStateHandler *save_state,
1211 LoadStateHandler *load_state,
1214 SaveStateEntry *se, **pse;
1216 se = qemu_malloc(sizeof(SaveStateEntry));
1219 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1220 se->instance_id = instance_id;
1221 se->version_id = version_id;
1222 se->save_state = save_state;
1223 se->load_state = load_state;
1224 se->opaque = opaque;
1227 /* add at the end of list */
1229 while (*pse != NULL)
1230 pse = &(*pse)->next;
1235 #define QEMU_VM_FILE_MAGIC 0x5145564d
1236 #define QEMU_VM_FILE_VERSION 0x00000001
1238 int qemu_savevm(const char *filename)
1242 int len, len_pos, cur_pos, saved_vm_running, ret;
1244 saved_vm_running = vm_running;
1247 f = fopen(filename, "wb");
1253 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1254 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1256 for(se = first_se; se != NULL; se = se->next) {
1258 len = strlen(se->idstr);
1259 qemu_put_byte(f, len);
1260 qemu_put_buffer(f, se->idstr, len);
1262 qemu_put_be32(f, se->instance_id);
1263 qemu_put_be32(f, se->version_id);
1265 /* record size: filled later */
1267 qemu_put_be32(f, 0);
1269 se->save_state(f, se->opaque);
1271 /* fill record size */
1273 len = ftell(f) - len_pos - 4;
1274 fseek(f, len_pos, SEEK_SET);
1275 qemu_put_be32(f, len);
1276 fseek(f, cur_pos, SEEK_SET);
1282 if (saved_vm_running)
1287 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1291 for(se = first_se; se != NULL; se = se->next) {
1292 if (!strcmp(se->idstr, idstr) &&
1293 instance_id == se->instance_id)
1299 int qemu_loadvm(const char *filename)
1303 int len, cur_pos, ret, instance_id, record_len, version_id;
1304 int saved_vm_running;
1308 saved_vm_running = vm_running;
1311 f = fopen(filename, "rb");
1317 v = qemu_get_be32(f);
1318 if (v != QEMU_VM_FILE_MAGIC)
1320 v = qemu_get_be32(f);
1321 if (v != QEMU_VM_FILE_VERSION) {
1328 #if defined (DO_TB_FLUSH)
1329 tb_flush(global_env);
1331 len = qemu_get_byte(f);
1334 qemu_get_buffer(f, idstr, len);
1336 instance_id = qemu_get_be32(f);
1337 version_id = qemu_get_be32(f);
1338 record_len = qemu_get_be32(f);
1340 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1341 idstr, instance_id, version_id, record_len);
1344 se = find_se(idstr, instance_id);
1346 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1347 instance_id, idstr);
1349 ret = se->load_state(f, se->opaque, version_id);
1351 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1352 instance_id, idstr);
1355 /* always seek to exact end of record */
1356 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1361 if (saved_vm_running)
1366 /***********************************************************/
1367 /* cpu save/restore */
1369 #if defined(TARGET_I386)
1371 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1373 qemu_put_be32(f, (uint32_t)dt->base);
1374 qemu_put_be32(f, dt->limit);
1375 qemu_put_be32(f, dt->flags);
1378 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1380 dt->base = (uint8_t *)qemu_get_be32(f);
1381 dt->limit = qemu_get_be32(f);
1382 dt->flags = qemu_get_be32(f);
1385 void cpu_save(QEMUFile *f, void *opaque)
1387 CPUState *env = opaque;
1388 uint16_t fptag, fpus, fpuc;
1392 for(i = 0; i < 8; i++)
1393 qemu_put_be32s(f, &env->regs[i]);
1394 qemu_put_be32s(f, &env->eip);
1395 qemu_put_be32s(f, &env->eflags);
1396 qemu_put_be32s(f, &env->eflags);
1397 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1398 qemu_put_be32s(f, &hflags);
1402 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1404 for (i=7; i>=0; i--) {
1406 if (env->fptags[i]) {
1411 qemu_put_be16s(f, &fpuc);
1412 qemu_put_be16s(f, &fpus);
1413 qemu_put_be16s(f, &fptag);
1415 for(i = 0; i < 8; i++) {
1418 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1419 qemu_put_be64(f, mant);
1420 qemu_put_be16(f, exp);
1423 for(i = 0; i < 6; i++)
1424 cpu_put_seg(f, &env->segs[i]);
1425 cpu_put_seg(f, &env->ldt);
1426 cpu_put_seg(f, &env->tr);
1427 cpu_put_seg(f, &env->gdt);
1428 cpu_put_seg(f, &env->idt);
1430 qemu_put_be32s(f, &env->sysenter_cs);
1431 qemu_put_be32s(f, &env->sysenter_esp);
1432 qemu_put_be32s(f, &env->sysenter_eip);
1434 qemu_put_be32s(f, &env->cr[0]);
1435 qemu_put_be32s(f, &env->cr[2]);
1436 qemu_put_be32s(f, &env->cr[3]);
1437 qemu_put_be32s(f, &env->cr[4]);
1439 for(i = 0; i < 8; i++)
1440 qemu_put_be32s(f, &env->dr[i]);
1443 qemu_put_be32s(f, &env->a20_mask);
1446 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1448 CPUState *env = opaque;
1451 uint16_t fpus, fpuc, fptag;
1453 if (version_id != 1)
1455 for(i = 0; i < 8; i++)
1456 qemu_get_be32s(f, &env->regs[i]);
1457 qemu_get_be32s(f, &env->eip);
1458 qemu_get_be32s(f, &env->eflags);
1459 qemu_get_be32s(f, &env->eflags);
1460 qemu_get_be32s(f, &hflags);
1462 qemu_get_be16s(f, &fpuc);
1463 qemu_get_be16s(f, &fpus);
1464 qemu_get_be16s(f, &fptag);
1466 for(i = 0; i < 8; i++) {
1469 mant = qemu_get_be64(f);
1470 exp = qemu_get_be16(f);
1471 env->fpregs[i] = cpu_set_fp80(mant, exp);
1475 env->fpstt = (fpus >> 11) & 7;
1476 env->fpus = fpus & ~0x3800;
1477 for(i = 0; i < 8; i++) {
1478 env->fptags[i] = ((fptag & 3) == 3);
1482 for(i = 0; i < 6; i++)
1483 cpu_get_seg(f, &env->segs[i]);
1484 cpu_get_seg(f, &env->ldt);
1485 cpu_get_seg(f, &env->tr);
1486 cpu_get_seg(f, &env->gdt);
1487 cpu_get_seg(f, &env->idt);
1489 qemu_get_be32s(f, &env->sysenter_cs);
1490 qemu_get_be32s(f, &env->sysenter_esp);
1491 qemu_get_be32s(f, &env->sysenter_eip);
1493 qemu_get_be32s(f, &env->cr[0]);
1494 qemu_get_be32s(f, &env->cr[2]);
1495 qemu_get_be32s(f, &env->cr[3]);
1496 qemu_get_be32s(f, &env->cr[4]);
1498 for(i = 0; i < 8; i++)
1499 qemu_get_be32s(f, &env->dr[i]);
1502 qemu_get_be32s(f, &env->a20_mask);
1504 /* XXX: compute hflags from scratch, except for CPL and IIF */
1505 env->hflags = hflags;
1510 #elif defined(TARGET_PPC)
1511 void cpu_save(QEMUFile *f, void *opaque)
1515 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1521 #warning No CPU save/restore functions
1525 /***********************************************************/
1526 /* ram save/restore */
1528 /* we just avoid storing empty pages */
1529 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1534 for(i = 1; i < len; i++) {
1538 qemu_put_byte(f, 1);
1539 qemu_put_byte(f, v);
1542 qemu_put_byte(f, 0);
1543 qemu_put_buffer(f, buf, len);
1546 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1550 v = qemu_get_byte(f);
1553 if (qemu_get_buffer(f, buf, len) != len)
1557 v = qemu_get_byte(f);
1558 memset(buf, v, len);
1566 static void ram_save(QEMUFile *f, void *opaque)
1569 qemu_put_be32(f, phys_ram_size);
1570 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1571 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1575 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1579 if (version_id != 1)
1581 if (qemu_get_be32(f) != phys_ram_size)
1583 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1584 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1591 /***********************************************************/
1592 /* main execution loop */
1594 void gui_update(void *opaque)
1596 display_state.dpy_refresh(&display_state);
1597 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1600 /* XXX: support several handlers */
1601 VMStopHandler *vm_stop_cb;
1602 VMStopHandler *vm_stop_opaque;
1604 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1607 vm_stop_opaque = opaque;
1611 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1624 void vm_stop(int reason)
1627 cpu_disable_ticks();
1631 vm_stop_cb(vm_stop_opaque, reason);
1640 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1641 IOHandlerRecord *ioh, *ioh_next;
1646 CPUState *env = global_env;
1650 ret = cpu_exec(env);
1651 if (reset_requested) {
1652 ret = EXCP_INTERRUPT;
1655 if (ret == EXCP_DEBUG) {
1656 vm_stop(EXCP_DEBUG);
1658 /* if hlt instruction, we wait until the next IRQ */
1659 /* XXX: use timeout computed from timers */
1660 if (ret == EXCP_HLT)
1673 /* poll any events */
1674 /* XXX: separate device handlers from system ones */
1676 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1677 if (!ioh->fd_can_read) {
1680 pf->events = POLLIN;
1684 max_size = ioh->fd_can_read(ioh->opaque);
1686 if (max_size > sizeof(buf))
1687 max_size = sizeof(buf);
1689 pf->events = POLLIN;
1696 ioh->max_size = max_size;
1699 ret = poll(ufds, pf - ufds, timeout);
1701 /* XXX: better handling of removal */
1702 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1703 ioh_next = ioh->next;
1706 if (pf->revents & POLLIN) {
1707 if (ioh->max_size == 0) {
1708 /* just a read event */
1709 ioh->fd_read(ioh->opaque, NULL, 0);
1711 n = read(ioh->fd, buf, ioh->max_size);
1713 ioh->fd_read(ioh->opaque, buf, n);
1714 } else if (errno != -EAGAIN) {
1715 ioh->fd_read(ioh->opaque, NULL, -errno);
1723 #if defined(CONFIG_SLIRP)
1724 /* XXX: merge with poll() */
1726 fd_set rfds, wfds, xfds;
1734 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1737 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1739 slirp_select_poll(&rfds, &wfds, &xfds);
1747 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1748 qemu_get_clock(vm_clock));
1750 if (audio_enabled) {
1751 /* XXX: add explicit timer */
1755 /* run dma transfers, if any */
1759 /* real time timers */
1760 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1761 qemu_get_clock(rt_clock));
1763 cpu_disable_ticks();
1769 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
1770 "usage: %s [options] [disk_image]\n"
1772 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1774 "Standard options:\n"
1775 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1776 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1777 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1778 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1779 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1780 "-snapshot write to temporary files instead of disk image files\n"
1781 "-m megs set virtual RAM size to megs MB\n"
1782 "-nographic disable graphical output and redirect serial I/Os to console\n"
1783 "-enable-audio enable audio support\n"
1785 "Network options:\n"
1786 "-nics n simulate 'n' network cards [default=1]\n"
1787 "-macaddr addr set the mac address of the first interface\n"
1788 "-n script set tap/tun network init script [default=%s]\n"
1789 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1791 "-user-net use user mode network stack [default if no tap/tun script]\n"
1793 "-dummy-net use dummy network stack\n"
1795 "Linux boot specific:\n"
1796 "-kernel bzImage use 'bzImage' as kernel image\n"
1797 "-append cmdline use 'cmdline' as kernel command line\n"
1798 "-initrd file use 'file' as initial ram disk\n"
1800 "Debug/Expert options:\n"
1801 "-s wait gdb connection to port %d\n"
1802 "-p port change gdb connection port\n"
1803 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1804 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1805 "-L path set the directory for the BIOS and VGA BIOS\n"
1806 #ifdef USE_CODE_COPY
1807 "-no-code-copy disable code copy acceleration\n"
1811 "During emulation, use C-a h to get terminal commands:\n",
1812 #ifdef CONFIG_SOFTMMU
1817 DEFAULT_NETWORK_SCRIPT,
1818 DEFAULT_GDBSTUB_PORT,
1821 #ifndef CONFIG_SOFTMMU
1823 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1824 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1830 struct option long_options[] = {
1831 { "initrd", 1, NULL, 0, },
1832 { "hda", 1, NULL, 0, },
1833 { "hdb", 1, NULL, 0, },
1834 { "snapshot", 0, NULL, 0, },
1835 { "hdachs", 1, NULL, 0, },
1836 { "nographic", 0, NULL, 0, },
1837 { "kernel", 1, NULL, 0, },
1838 { "append", 1, NULL, 0, },
1839 { "tun-fd", 1, NULL, 0, },
1840 { "hdc", 1, NULL, 0, },
1841 { "hdd", 1, NULL, 0, },
1842 { "cdrom", 1, NULL, 0, },
1843 { "boot", 1, NULL, 0, },
1844 { "fda", 1, NULL, 0, },
1845 { "fdb", 1, NULL, 0, },
1846 { "no-code-copy", 0, NULL, 0 },
1847 { "nics", 1, NULL, 0 },
1848 { "macaddr", 1, NULL, 0 },
1849 { "user-net", 0, NULL, 0 },
1850 { "dummy-net", 0, NULL, 0 },
1851 { "enable-audio", 0, NULL, 0 },
1852 { NULL, 0, NULL, 0 },
1855 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1857 /* this stack is only used during signal handling */
1858 #define SIGNAL_STACK_SIZE 32768
1860 static uint8_t *signal_stack;
1864 #define NET_IF_TUN 0
1865 #define NET_IF_USER 1
1866 #define NET_IF_DUMMY 2
1868 int main(int argc, char **argv)
1870 #ifdef CONFIG_GDBSTUB
1871 int use_gdbstub, gdbstub_port;
1873 int c, i, long_index, has_cdrom;
1874 int snapshot, linux_boot;
1876 const char *initrd_filename;
1877 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
1878 const char *kernel_filename, *kernel_cmdline;
1879 DisplayState *ds = &display_state;
1880 int cyls, heads, secs;
1881 int start_emulation = 1;
1883 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
1885 #if !defined(CONFIG_SOFTMMU)
1886 /* we never want that malloc() uses mmap() */
1887 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
1889 initrd_filename = NULL;
1890 for(i = 0; i < MAX_FD; i++)
1891 fd_filename[i] = NULL;
1892 for(i = 0; i < MAX_DISKS; i++)
1893 hd_filename[i] = NULL;
1894 ram_size = 32 * 1024 * 1024;
1895 vga_ram_size = VGA_RAM_SIZE;
1896 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
1897 #ifdef CONFIG_GDBSTUB
1899 gdbstub_port = DEFAULT_GDBSTUB_PORT;
1903 kernel_filename = NULL;
1904 kernel_cmdline = "";
1906 cyls = heads = secs = 0;
1911 /* default mac address of the first network interface */
1921 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index);
1926 switch(long_index) {
1928 initrd_filename = optarg;
1931 hd_filename[0] = optarg;
1934 hd_filename[1] = optarg;
1943 cyls = strtol(p, (char **)&p, 0);
1947 heads = strtol(p, (char **)&p, 0);
1951 secs = strtol(p, (char **)&p, 0);
1962 kernel_filename = optarg;
1965 kernel_cmdline = optarg;
1971 if (nb_tun_fds < MAX_NICS) {
1972 fd = strtol(optarg, (char **)&p, 0);
1974 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
1977 tun_fds[nb_tun_fds++] = fd;
1982 hd_filename[2] = optarg;
1986 hd_filename[3] = optarg;
1989 hd_filename[2] = optarg;
1993 boot_device = optarg[0];
1994 if (boot_device != 'a' && boot_device != 'b' &&
1995 boot_device != 'c' && boot_device != 'd') {
1996 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2001 fd_filename[0] = optarg;
2004 fd_filename[1] = optarg;
2007 code_copy_enabled = 0;
2010 nb_nics = atoi(optarg);
2011 if (nb_nics < 1 || nb_nics > MAX_NICS) {
2012 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2021 for(i = 0; i < 6; i++) {
2022 macaddr[i] = strtol(p, (char **)&p, 16);
2029 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2038 net_if_type = NET_IF_USER;
2041 net_if_type = NET_IF_DUMMY;
2052 ram_size = atoi(optarg) * 1024 * 1024;
2055 if (ram_size > PHYS_RAM_MAX_SIZE) {
2056 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2057 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2066 mask = cpu_str_to_log_mask(optarg);
2068 printf("Log items (comma separated):\n");
2069 for(item = cpu_log_items; item->mask != 0; item++) {
2070 printf("%-10s %s\n", item->name, item->help);
2078 pstrcpy(network_script, sizeof(network_script), optarg);
2080 #ifdef CONFIG_GDBSTUB
2085 gdbstub_port = atoi(optarg);
2092 start_emulation = 0;
2097 if (optind < argc) {
2098 hd_filename[0] = argv[optind++];
2101 linux_boot = (kernel_filename != NULL);
2103 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2104 fd_filename[0] == '\0')
2107 /* boot to cd by default if no hard disk */
2108 if (hd_filename[0] == '\0' && boot_device == 'c') {
2109 if (fd_filename[0] != '\0')
2115 #if !defined(CONFIG_SOFTMMU)
2116 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2118 static uint8_t stdout_buf[4096];
2119 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2122 setvbuf(stdout, NULL, _IOLBF, 0);
2125 /* init host network redirectors */
2126 if (net_if_type == -1) {
2127 net_if_type = NET_IF_TUN;
2128 #if defined(CONFIG_SLIRP)
2129 if (access(network_script, R_OK) < 0) {
2130 net_if_type = NET_IF_USER;
2135 for(i = 0; i < nb_nics; i++) {
2136 NetDriverState *nd = &nd_table[i];
2138 /* init virtual mac address */
2139 nd->macaddr[0] = macaddr[0];
2140 nd->macaddr[1] = macaddr[1];
2141 nd->macaddr[2] = macaddr[2];
2142 nd->macaddr[3] = macaddr[3];
2143 nd->macaddr[4] = macaddr[4];
2144 nd->macaddr[5] = macaddr[5] + i;
2145 switch(net_if_type) {
2146 #if defined(CONFIG_SLIRP)
2151 #if !defined(_WIN32)
2153 if (i < nb_tun_fds) {
2154 net_fd_init(nd, tun_fds[i]);
2156 if (net_tun_init(nd) < 0)
2168 /* init the memory */
2169 phys_ram_size = ram_size + vga_ram_size;
2171 #ifdef CONFIG_SOFTMMU
2172 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2173 if (!phys_ram_base) {
2174 fprintf(stderr, "Could not allocate physical memory\n");
2178 /* as we must map the same page at several addresses, we must use
2183 tmpdir = getenv("QEMU_TMPDIR");
2186 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2187 if (mkstemp(phys_ram_file) < 0) {
2188 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2192 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2193 if (phys_ram_fd < 0) {
2194 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2198 ftruncate(phys_ram_fd, phys_ram_size);
2199 unlink(phys_ram_file);
2200 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2202 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2204 if (phys_ram_base == MAP_FAILED) {
2205 fprintf(stderr, "Could not map physical memory\n");
2211 /* we always create the cdrom drive, even if no disk is there */
2213 bs_table[2] = bdrv_new("cdrom");
2214 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2217 /* open the virtual block devices */
2218 for(i = 0; i < MAX_DISKS; i++) {
2219 if (hd_filename[i]) {
2222 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2223 bs_table[i] = bdrv_new(buf);
2225 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2226 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2230 if (i == 0 && cyls != 0)
2231 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2235 /* we always create at least one floppy disk */
2236 fd_table[0] = bdrv_new("fda");
2237 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2239 for(i = 0; i < MAX_FD; i++) {
2240 if (fd_filename[i]) {
2243 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2244 fd_table[i] = bdrv_new(buf);
2245 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2247 if (fd_filename[i] != '\0') {
2248 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2249 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2257 /* init CPU state */
2260 cpu_single_env = env;
2262 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2263 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2264 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2267 cpu_calibrate_ticks();
2271 dumb_display_init(ds);
2274 sdl_display_init(ds);
2276 dumb_display_init(ds);
2280 /* setup cpu signal handlers for MMU / self modifying code handling */
2281 #if !defined(CONFIG_SOFTMMU)
2283 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2286 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2287 stk.ss_sp = signal_stack;
2288 stk.ss_size = SIGNAL_STACK_SIZE;
2291 if (sigaltstack(&stk, NULL) < 0) {
2292 perror("sigaltstack");
2298 struct sigaction act;
2300 sigfillset(&act.sa_mask);
2301 act.sa_flags = SA_SIGINFO;
2302 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2303 act.sa_flags |= SA_ONSTACK;
2305 act.sa_sigaction = host_segv_handler;
2306 sigaction(SIGSEGV, &act, NULL);
2307 sigaction(SIGBUS, &act, NULL);
2308 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2309 sigaction(SIGFPE, &act, NULL);
2316 struct sigaction act;
2317 sigfillset(&act.sa_mask);
2319 act.sa_handler = SIG_IGN;
2320 sigaction(SIGPIPE, &act, NULL);
2325 #if defined(TARGET_I386)
2326 pc_init(ram_size, vga_ram_size, boot_device,
2327 ds, fd_filename, snapshot,
2328 kernel_filename, kernel_cmdline, initrd_filename);
2329 #elif defined(TARGET_PPC)
2330 ppc_init(ram_size, vga_ram_size, boot_device,
2331 ds, fd_filename, snapshot,
2332 kernel_filename, kernel_cmdline, initrd_filename);
2335 /* launched after the device init so that it can display or not a
2339 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2340 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2342 #ifdef CONFIG_GDBSTUB
2344 if (gdbserver_start(gdbstub_port) < 0) {
2345 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2349 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2353 if (start_emulation)