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
35 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
47 #include <linux/if_tun.h>
50 #include <linux/rtc.h>
54 #if defined(CONFIG_SLIRP)
60 #include <sys/timeb.h>
62 #define getopt_long_only getopt_long
63 #define memalign(align, size) malloc(size)
67 #if defined(__linux__)
68 /* SDL use the pthreads and they modify sigaction. We don't
70 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
71 extern void __libc_sigaction();
72 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
74 extern void __sigaction();
75 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
77 #endif /* __linux__ */
78 #endif /* CONFIG_SDL */
86 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
88 //#define DEBUG_UNUSED_IOPORT
89 //#define DEBUG_IOPORT
91 #if !defined(CONFIG_SOFTMMU)
92 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
94 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
98 #define GUI_REFRESH_INTERVAL 30
100 /* XXX: use a two level table to limit memory usage */
101 #define MAX_IOPORTS 65536
103 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
104 char phys_ram_file[1024];
105 CPUState *global_env;
106 CPUState *cpu_single_env;
107 void *ioport_opaque[MAX_IOPORTS];
108 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
109 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
110 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
112 static DisplayState display_state;
114 int64_t ticks_per_sec;
115 int boot_device = 'c';
117 static char network_script[1024];
118 int pit_min_timer_count = 0;
120 NetDriverState nd_table[MAX_NICS];
121 SerialState *serial_console;
122 QEMUTimer *gui_timer;
124 int audio_enabled = 0;
126 /***********************************************************/
127 /* x86 ISA bus support */
129 target_phys_addr_t isa_mem_base = 0;
131 uint32_t default_ioport_readb(void *opaque, uint32_t address)
133 #ifdef DEBUG_UNUSED_IOPORT
134 fprintf(stderr, "inb: port=0x%04x\n", address);
139 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
141 #ifdef DEBUG_UNUSED_IOPORT
142 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
146 /* default is to make two byte accesses */
147 uint32_t default_ioport_readw(void *opaque, uint32_t address)
150 data = ioport_read_table[0][address](ioport_opaque[address], address);
151 address = (address + 1) & (MAX_IOPORTS - 1);
152 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
156 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
158 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
159 address = (address + 1) & (MAX_IOPORTS - 1);
160 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
163 uint32_t default_ioport_readl(void *opaque, uint32_t address)
165 #ifdef DEBUG_UNUSED_IOPORT
166 fprintf(stderr, "inl: port=0x%04x\n", address);
171 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
173 #ifdef DEBUG_UNUSED_IOPORT
174 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
178 void init_ioports(void)
182 for(i = 0; i < MAX_IOPORTS; i++) {
183 ioport_read_table[0][i] = default_ioport_readb;
184 ioport_write_table[0][i] = default_ioport_writeb;
185 ioport_read_table[1][i] = default_ioport_readw;
186 ioport_write_table[1][i] = default_ioport_writew;
187 ioport_read_table[2][i] = default_ioport_readl;
188 ioport_write_table[2][i] = default_ioport_writel;
192 /* size is the word size in byte */
193 int register_ioport_read(int start, int length, int size,
194 IOPortReadFunc *func, void *opaque)
200 } else if (size == 2) {
202 } else if (size == 4) {
205 hw_error("register_ioport_read: invalid size");
208 for(i = start; i < start + length; i += size) {
209 ioport_read_table[bsize][i] = func;
210 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
211 hw_error("register_ioport_read: invalid opaque");
212 ioport_opaque[i] = opaque;
217 /* size is the word size in byte */
218 int register_ioport_write(int start, int length, int size,
219 IOPortWriteFunc *func, void *opaque)
225 } else if (size == 2) {
227 } else if (size == 4) {
230 hw_error("register_ioport_write: invalid size");
233 for(i = start; i < start + length; i += size) {
234 ioport_write_table[bsize][i] = func;
235 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
236 hw_error("register_ioport_read: invalid opaque");
237 ioport_opaque[i] = opaque;
242 void pstrcpy(char *buf, int buf_size, const char *str)
252 if (c == 0 || q >= buf + buf_size - 1)
259 /* strcat and truncate. */
260 char *pstrcat(char *buf, int buf_size, const char *s)
265 pstrcpy(buf + len, buf_size - len, s);
269 /* return the size or -1 if error */
270 int load_image(const char *filename, uint8_t *addr)
273 fd = open(filename, O_RDONLY | O_BINARY);
276 size = lseek(fd, 0, SEEK_END);
277 lseek(fd, 0, SEEK_SET);
278 if (read(fd, addr, size) != size) {
286 void cpu_outb(CPUState *env, int addr, int val)
289 if (loglevel & CPU_LOG_IOPORT)
290 fprintf(logfile, "outb: %04x %02x\n", addr, val);
292 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
295 void cpu_outw(CPUState *env, int addr, int val)
298 if (loglevel & CPU_LOG_IOPORT)
299 fprintf(logfile, "outw: %04x %04x\n", addr, val);
301 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
304 void cpu_outl(CPUState *env, int addr, int val)
307 if (loglevel & CPU_LOG_IOPORT)
308 fprintf(logfile, "outl: %04x %08x\n", addr, val);
310 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
313 int cpu_inb(CPUState *env, int addr)
316 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
318 if (loglevel & CPU_LOG_IOPORT)
319 fprintf(logfile, "inb : %04x %02x\n", addr, val);
324 int cpu_inw(CPUState *env, int addr)
327 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
329 if (loglevel & CPU_LOG_IOPORT)
330 fprintf(logfile, "inw : %04x %04x\n", addr, val);
335 int cpu_inl(CPUState *env, int addr)
338 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
340 if (loglevel & CPU_LOG_IOPORT)
341 fprintf(logfile, "inl : %04x %08x\n", addr, val);
346 /***********************************************************/
347 void hw_error(const char *fmt, ...)
352 fprintf(stderr, "qemu: hardware error: ");
353 vfprintf(stderr, fmt, ap);
354 fprintf(stderr, "\n");
356 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
358 cpu_dump_state(global_env, stderr, 0);
364 /***********************************************************/
367 #if defined(__powerpc__)
369 static inline uint32_t get_tbl(void)
372 asm volatile("mftb %0" : "=r" (tbl));
376 static inline uint32_t get_tbu(void)
379 asm volatile("mftbu %0" : "=r" (tbl));
383 int64_t cpu_get_real_ticks(void)
386 /* NOTE: we test if wrapping has occurred */
392 return ((int64_t)h << 32) | l;
395 #elif defined(__i386__)
397 int64_t cpu_get_real_ticks(void)
400 asm volatile ("rdtsc" : "=A" (val));
404 #elif defined(__x86_64__)
406 int64_t cpu_get_real_ticks(void)
410 asm volatile("rdtsc" : "=a" (low), "=d" (high));
418 #error unsupported CPU
421 static int64_t cpu_ticks_offset;
422 static int cpu_ticks_enabled;
424 static inline int64_t cpu_get_ticks(void)
426 if (!cpu_ticks_enabled) {
427 return cpu_ticks_offset;
429 return cpu_get_real_ticks() + cpu_ticks_offset;
433 /* enable cpu_get_ticks() */
434 void cpu_enable_ticks(void)
436 if (!cpu_ticks_enabled) {
437 cpu_ticks_offset -= cpu_get_real_ticks();
438 cpu_ticks_enabled = 1;
442 /* disable cpu_get_ticks() : the clock is stopped. You must not call
443 cpu_get_ticks() after that. */
444 void cpu_disable_ticks(void)
446 if (cpu_ticks_enabled) {
447 cpu_ticks_offset = cpu_get_ticks();
448 cpu_ticks_enabled = 0;
452 static int64_t get_clock(void)
457 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
460 gettimeofday(&tv, NULL);
461 return tv.tv_sec * 1000000LL + tv.tv_usec;
465 void cpu_calibrate_ticks(void)
470 ticks = cpu_get_real_ticks();
476 usec = get_clock() - usec;
477 ticks = cpu_get_real_ticks() - ticks;
478 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
481 /* compute with 96 bit intermediate result: (a*b)/c */
482 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
487 #ifdef WORDS_BIGENDIAN
497 rl = (uint64_t)u.l.low * (uint64_t)b;
498 rh = (uint64_t)u.l.high * (uint64_t)b;
501 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
505 #define QEMU_TIMER_REALTIME 0
506 #define QEMU_TIMER_VIRTUAL 1
510 /* XXX: add frequency */
518 struct QEMUTimer *next;
524 static QEMUTimer *active_timers[2];
526 static MMRESULT timerID;
528 /* frequency of the times() clock tick */
529 static int timer_freq;
532 QEMUClock *qemu_new_clock(int type)
535 clock = qemu_mallocz(sizeof(QEMUClock));
542 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
546 ts = qemu_mallocz(sizeof(QEMUTimer));
553 void qemu_free_timer(QEMUTimer *ts)
558 /* stop a timer, but do not dealloc it */
559 void qemu_del_timer(QEMUTimer *ts)
563 /* NOTE: this code must be signal safe because
564 qemu_timer_expired() can be called from a signal. */
565 pt = &active_timers[ts->clock->type];
578 /* modify the current timer so that it will be fired when current_time
579 >= expire_time. The corresponding callback will be called. */
580 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
586 /* add the timer in the sorted list */
587 /* NOTE: this code must be signal safe because
588 qemu_timer_expired() can be called from a signal. */
589 pt = &active_timers[ts->clock->type];
594 if (t->expire_time > expire_time)
598 ts->expire_time = expire_time;
603 int qemu_timer_pending(QEMUTimer *ts)
606 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
613 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
617 return (timer_head->expire_time <= current_time);
620 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
626 if (ts->expire_time > current_time)
628 /* remove timer from the list before calling the callback */
629 *ptimer_head = ts->next;
632 /* run the callback (the timer list can be modified) */
637 int64_t qemu_get_clock(QEMUClock *clock)
639 switch(clock->type) {
640 case QEMU_TIMER_REALTIME:
642 return GetTickCount();
647 /* Note that using gettimeofday() is not a good solution
648 for timers because its value change when the date is
650 if (timer_freq == 100) {
651 return times(&tp) * 10;
653 return ((int64_t)times(&tp) * 1000) / timer_freq;
658 case QEMU_TIMER_VIRTUAL:
659 return cpu_get_ticks();
664 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
666 uint64_t expire_time;
668 if (qemu_timer_pending(ts)) {
669 expire_time = ts->expire_time;
673 qemu_put_be64(f, expire_time);
676 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
678 uint64_t expire_time;
680 expire_time = qemu_get_be64(f);
681 if (expire_time != -1) {
682 qemu_mod_timer(ts, expire_time);
688 static void timer_save(QEMUFile *f, void *opaque)
690 if (cpu_ticks_enabled) {
691 hw_error("cannot save state if virtual timers are running");
693 qemu_put_be64s(f, &cpu_ticks_offset);
694 qemu_put_be64s(f, &ticks_per_sec);
697 static int timer_load(QEMUFile *f, void *opaque, int version_id)
701 if (cpu_ticks_enabled) {
704 qemu_get_be64s(f, &cpu_ticks_offset);
705 qemu_get_be64s(f, &ticks_per_sec);
710 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
711 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
713 static void host_alarm_handler(int host_signum)
716 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
717 qemu_get_clock(vm_clock)) ||
718 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
719 qemu_get_clock(rt_clock))) {
720 /* stop the cpu because a timer occured */
721 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
727 #define RTC_FREQ 1024
731 static int start_rtc_timer(void)
733 rtc_fd = open("/dev/rtc", O_RDONLY);
736 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
737 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
738 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
739 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
742 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
747 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
753 static void init_timers(void)
755 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
756 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
761 timerID = timeSetEvent(10, // interval (ms)
763 host_alarm_handler, // function
764 (DWORD)&count, // user parameter
765 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
767 perror("failed timer alarm");
771 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
774 struct sigaction act;
775 struct itimerval itv;
777 /* get times() syscall frequency */
778 timer_freq = sysconf(_SC_CLK_TCK);
781 sigfillset(&act.sa_mask);
783 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
784 act.sa_flags |= SA_ONSTACK;
786 act.sa_handler = host_alarm_handler;
787 sigaction(SIGALRM, &act, NULL);
789 itv.it_interval.tv_sec = 0;
790 itv.it_interval.tv_usec = 1000;
791 itv.it_value.tv_sec = 0;
792 itv.it_value.tv_usec = 10 * 1000;
793 setitimer(ITIMER_REAL, &itv, NULL);
794 /* we probe the tick duration of the kernel to inform the user if
795 the emulated kernel requested a too high timer frequency */
796 getitimer(ITIMER_REAL, &itv);
798 if (itv.it_interval.tv_usec > 1000) {
799 /* try to use /dev/rtc to have a faster timer */
800 if (start_rtc_timer() < 0)
803 itv.it_interval.tv_sec = 0;
804 itv.it_interval.tv_usec = 0;
805 itv.it_value.tv_sec = 0;
806 itv.it_value.tv_usec = 0;
807 setitimer(ITIMER_REAL, &itv, NULL);
810 sigaction(SIGIO, &act, NULL);
811 fcntl(rtc_fd, F_SETFL, O_ASYNC);
812 fcntl(rtc_fd, F_SETOWN, getpid());
815 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
822 void quit_timers(void)
825 timeKillEvent(timerID);
829 /***********************************************************/
834 int serial_open_device(void)
841 int serial_open_device(void)
843 char slave_name[1024];
844 int master_fd, slave_fd;
846 if (serial_console == NULL && nographic) {
847 /* use console for serial port */
850 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
851 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
854 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
861 /***********************************************************/
862 /* Linux network device redirectors */
864 void hex_dump(FILE *f, const uint8_t *buf, int size)
868 for(i=0;i<size;i+=16) {
872 fprintf(f, "%08x ", i);
875 fprintf(f, " %02x", buf[i+j]);
882 if (c < ' ' || c > '~')
890 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
892 nd->send_packet(nd, buf, size);
895 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
896 IOReadHandler *fd_read, void *opaque)
898 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
901 /* dummy network adapter */
903 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
907 static void dummy_add_read_packet(NetDriverState *nd,
908 IOCanRWHandler *fd_can_read,
909 IOReadHandler *fd_read, void *opaque)
913 static int net_dummy_init(NetDriverState *nd)
915 nd->send_packet = dummy_send_packet;
916 nd->add_read_packet = dummy_add_read_packet;
917 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
921 #if defined(CONFIG_SLIRP)
923 /* slirp network adapter */
925 static void *slirp_fd_opaque;
926 static IOCanRWHandler *slirp_fd_can_read;
927 static IOReadHandler *slirp_fd_read;
928 static int slirp_inited;
930 int slirp_can_output(void)
932 return slirp_fd_can_read(slirp_fd_opaque);
935 void slirp_output(const uint8_t *pkt, int pkt_len)
939 hex_dump(stdout, pkt, pkt_len);
941 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
944 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
948 hex_dump(stdout, buf, size);
950 slirp_input(buf, size);
953 static void slirp_add_read_packet(NetDriverState *nd,
954 IOCanRWHandler *fd_can_read,
955 IOReadHandler *fd_read, void *opaque)
957 slirp_fd_opaque = opaque;
958 slirp_fd_can_read = fd_can_read;
959 slirp_fd_read = fd_read;
962 static int net_slirp_init(NetDriverState *nd)
968 nd->send_packet = slirp_send_packet;
969 nd->add_read_packet = slirp_add_read_packet;
970 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
974 #endif /* CONFIG_SLIRP */
978 static int tun_open(char *ifname, int ifname_size)
984 fd = open("/dev/tap", O_RDWR);
986 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
991 dev = devname(s.st_rdev, S_IFCHR);
992 pstrcpy(ifname, ifname_size, dev);
994 fcntl(fd, F_SETFL, O_NONBLOCK);
998 static int tun_open(char *ifname, int ifname_size)
1003 fd = open("/dev/net/tun", O_RDWR);
1005 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1008 memset(&ifr, 0, sizeof(ifr));
1009 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1010 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1011 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1013 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1017 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1018 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1019 fcntl(fd, F_SETFL, O_NONBLOCK);
1024 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1026 write(nd->fd, buf, size);
1029 static void tun_add_read_packet(NetDriverState *nd,
1030 IOCanRWHandler *fd_can_read,
1031 IOReadHandler *fd_read, void *opaque)
1033 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1036 static int net_tun_init(NetDriverState *nd)
1042 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1046 /* try to launch network init script */
1051 *parg++ = network_script;
1052 *parg++ = nd->ifname;
1054 execv(network_script, args);
1057 while (waitpid(pid, &status, 0) != pid);
1058 if (!WIFEXITED(status) ||
1059 WEXITSTATUS(status) != 0) {
1060 fprintf(stderr, "%s: could not launch network script\n",
1064 nd->send_packet = tun_send_packet;
1065 nd->add_read_packet = tun_add_read_packet;
1069 static int net_fd_init(NetDriverState *nd, int fd)
1072 nd->send_packet = tun_send_packet;
1073 nd->add_read_packet = tun_add_read_packet;
1074 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1078 #endif /* !_WIN32 */
1080 /***********************************************************/
1085 static void term_exit(void)
1089 static void term_init(void)
1095 /* init terminal so that we can grab keys */
1096 static struct termios oldtty;
1098 static void term_exit(void)
1100 tcsetattr (0, TCSANOW, &oldtty);
1103 static void term_init(void)
1107 tcgetattr (0, &tty);
1110 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1111 |INLCR|IGNCR|ICRNL|IXON);
1112 tty.c_oflag |= OPOST;
1113 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1114 /* if graphical mode, we allow Ctrl-C handling */
1116 tty.c_lflag &= ~ISIG;
1117 tty.c_cflag &= ~(CSIZE|PARENB);
1120 tty.c_cc[VTIME] = 0;
1122 tcsetattr (0, TCSANOW, &tty);
1126 fcntl(0, F_SETFL, O_NONBLOCK);
1131 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1135 static void dumb_resize(DisplayState *ds, int w, int h)
1139 static void dumb_refresh(DisplayState *ds)
1141 vga_update_display();
1144 void dumb_display_init(DisplayState *ds)
1149 ds->dpy_update = dumb_update;
1150 ds->dpy_resize = dumb_resize;
1151 ds->dpy_refresh = dumb_refresh;
1154 #if !defined(CONFIG_SOFTMMU)
1155 /***********************************************************/
1156 /* cpu signal handler */
1157 static void host_segv_handler(int host_signum, siginfo_t *info,
1160 if (cpu_signal_handler(host_signum, info, puc))
1167 /***********************************************************/
1170 #define MAX_IO_HANDLERS 64
1172 typedef struct IOHandlerRecord {
1174 IOCanRWHandler *fd_can_read;
1175 IOReadHandler *fd_read;
1177 /* temporary data */
1180 struct IOHandlerRecord *next;
1183 static IOHandlerRecord *first_io_handler;
1185 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1186 IOReadHandler *fd_read, void *opaque)
1188 IOHandlerRecord *ioh;
1190 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1194 ioh->fd_can_read = fd_can_read;
1195 ioh->fd_read = fd_read;
1196 ioh->opaque = opaque;
1197 ioh->next = first_io_handler;
1198 first_io_handler = ioh;
1202 void qemu_del_fd_read_handler(int fd)
1204 IOHandlerRecord **pioh, *ioh;
1206 pioh = &first_io_handler;
1211 if (ioh->fd == fd) {
1219 /***********************************************************/
1220 /* savevm/loadvm support */
1222 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1224 fwrite(buf, 1, size, f);
1227 void qemu_put_byte(QEMUFile *f, int v)
1232 void qemu_put_be16(QEMUFile *f, unsigned int v)
1234 qemu_put_byte(f, v >> 8);
1235 qemu_put_byte(f, v);
1238 void qemu_put_be32(QEMUFile *f, unsigned int v)
1240 qemu_put_byte(f, v >> 24);
1241 qemu_put_byte(f, v >> 16);
1242 qemu_put_byte(f, v >> 8);
1243 qemu_put_byte(f, v);
1246 void qemu_put_be64(QEMUFile *f, uint64_t v)
1248 qemu_put_be32(f, v >> 32);
1249 qemu_put_be32(f, v);
1252 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1254 return fread(buf, 1, size, f);
1257 int qemu_get_byte(QEMUFile *f)
1267 unsigned int qemu_get_be16(QEMUFile *f)
1270 v = qemu_get_byte(f) << 8;
1271 v |= qemu_get_byte(f);
1275 unsigned int qemu_get_be32(QEMUFile *f)
1278 v = qemu_get_byte(f) << 24;
1279 v |= qemu_get_byte(f) << 16;
1280 v |= qemu_get_byte(f) << 8;
1281 v |= qemu_get_byte(f);
1285 uint64_t qemu_get_be64(QEMUFile *f)
1288 v = (uint64_t)qemu_get_be32(f) << 32;
1289 v |= qemu_get_be32(f);
1293 int64_t qemu_ftell(QEMUFile *f)
1298 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1300 if (fseek(f, pos, whence) < 0)
1305 typedef struct SaveStateEntry {
1309 SaveStateHandler *save_state;
1310 LoadStateHandler *load_state;
1312 struct SaveStateEntry *next;
1315 static SaveStateEntry *first_se;
1317 int register_savevm(const char *idstr,
1320 SaveStateHandler *save_state,
1321 LoadStateHandler *load_state,
1324 SaveStateEntry *se, **pse;
1326 se = qemu_malloc(sizeof(SaveStateEntry));
1329 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1330 se->instance_id = instance_id;
1331 se->version_id = version_id;
1332 se->save_state = save_state;
1333 se->load_state = load_state;
1334 se->opaque = opaque;
1337 /* add at the end of list */
1339 while (*pse != NULL)
1340 pse = &(*pse)->next;
1345 #define QEMU_VM_FILE_MAGIC 0x5145564d
1346 #define QEMU_VM_FILE_VERSION 0x00000001
1348 int qemu_savevm(const char *filename)
1352 int len, len_pos, cur_pos, saved_vm_running, ret;
1354 saved_vm_running = vm_running;
1357 f = fopen(filename, "wb");
1363 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1364 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1366 for(se = first_se; se != NULL; se = se->next) {
1368 len = strlen(se->idstr);
1369 qemu_put_byte(f, len);
1370 qemu_put_buffer(f, se->idstr, len);
1372 qemu_put_be32(f, se->instance_id);
1373 qemu_put_be32(f, se->version_id);
1375 /* record size: filled later */
1377 qemu_put_be32(f, 0);
1379 se->save_state(f, se->opaque);
1381 /* fill record size */
1383 len = ftell(f) - len_pos - 4;
1384 fseek(f, len_pos, SEEK_SET);
1385 qemu_put_be32(f, len);
1386 fseek(f, cur_pos, SEEK_SET);
1392 if (saved_vm_running)
1397 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1401 for(se = first_se; se != NULL; se = se->next) {
1402 if (!strcmp(se->idstr, idstr) &&
1403 instance_id == se->instance_id)
1409 int qemu_loadvm(const char *filename)
1413 int len, cur_pos, ret, instance_id, record_len, version_id;
1414 int saved_vm_running;
1418 saved_vm_running = vm_running;
1421 f = fopen(filename, "rb");
1427 v = qemu_get_be32(f);
1428 if (v != QEMU_VM_FILE_MAGIC)
1430 v = qemu_get_be32(f);
1431 if (v != QEMU_VM_FILE_VERSION) {
1438 #if defined (DO_TB_FLUSH)
1439 tb_flush(global_env);
1441 len = qemu_get_byte(f);
1444 qemu_get_buffer(f, idstr, len);
1446 instance_id = qemu_get_be32(f);
1447 version_id = qemu_get_be32(f);
1448 record_len = qemu_get_be32(f);
1450 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1451 idstr, instance_id, version_id, record_len);
1454 se = find_se(idstr, instance_id);
1456 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1457 instance_id, idstr);
1459 ret = se->load_state(f, se->opaque, version_id);
1461 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1462 instance_id, idstr);
1465 /* always seek to exact end of record */
1466 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1471 if (saved_vm_running)
1476 /***********************************************************/
1477 /* cpu save/restore */
1479 #if defined(TARGET_I386)
1481 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1483 qemu_put_be32(f, (uint32_t)dt->base);
1484 qemu_put_be32(f, dt->limit);
1485 qemu_put_be32(f, dt->flags);
1488 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1490 dt->base = (uint8_t *)qemu_get_be32(f);
1491 dt->limit = qemu_get_be32(f);
1492 dt->flags = qemu_get_be32(f);
1495 void cpu_save(QEMUFile *f, void *opaque)
1497 CPUState *env = opaque;
1498 uint16_t fptag, fpus, fpuc;
1502 for(i = 0; i < 8; i++)
1503 qemu_put_be32s(f, &env->regs[i]);
1504 qemu_put_be32s(f, &env->eip);
1505 qemu_put_be32s(f, &env->eflags);
1506 qemu_put_be32s(f, &env->eflags);
1507 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1508 qemu_put_be32s(f, &hflags);
1512 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1514 for (i=7; i>=0; i--) {
1516 if (env->fptags[i]) {
1521 qemu_put_be16s(f, &fpuc);
1522 qemu_put_be16s(f, &fpus);
1523 qemu_put_be16s(f, &fptag);
1525 for(i = 0; i < 8; i++) {
1528 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1529 qemu_put_be64(f, mant);
1530 qemu_put_be16(f, exp);
1533 for(i = 0; i < 6; i++)
1534 cpu_put_seg(f, &env->segs[i]);
1535 cpu_put_seg(f, &env->ldt);
1536 cpu_put_seg(f, &env->tr);
1537 cpu_put_seg(f, &env->gdt);
1538 cpu_put_seg(f, &env->idt);
1540 qemu_put_be32s(f, &env->sysenter_cs);
1541 qemu_put_be32s(f, &env->sysenter_esp);
1542 qemu_put_be32s(f, &env->sysenter_eip);
1544 qemu_put_be32s(f, &env->cr[0]);
1545 qemu_put_be32s(f, &env->cr[2]);
1546 qemu_put_be32s(f, &env->cr[3]);
1547 qemu_put_be32s(f, &env->cr[4]);
1549 for(i = 0; i < 8; i++)
1550 qemu_put_be32s(f, &env->dr[i]);
1553 qemu_put_be32s(f, &env->a20_mask);
1556 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1558 CPUState *env = opaque;
1561 uint16_t fpus, fpuc, fptag;
1563 if (version_id != 1)
1565 for(i = 0; i < 8; i++)
1566 qemu_get_be32s(f, &env->regs[i]);
1567 qemu_get_be32s(f, &env->eip);
1568 qemu_get_be32s(f, &env->eflags);
1569 qemu_get_be32s(f, &env->eflags);
1570 qemu_get_be32s(f, &hflags);
1572 qemu_get_be16s(f, &fpuc);
1573 qemu_get_be16s(f, &fpus);
1574 qemu_get_be16s(f, &fptag);
1576 for(i = 0; i < 8; i++) {
1579 mant = qemu_get_be64(f);
1580 exp = qemu_get_be16(f);
1581 env->fpregs[i] = cpu_set_fp80(mant, exp);
1585 env->fpstt = (fpus >> 11) & 7;
1586 env->fpus = fpus & ~0x3800;
1587 for(i = 0; i < 8; i++) {
1588 env->fptags[i] = ((fptag & 3) == 3);
1592 for(i = 0; i < 6; i++)
1593 cpu_get_seg(f, &env->segs[i]);
1594 cpu_get_seg(f, &env->ldt);
1595 cpu_get_seg(f, &env->tr);
1596 cpu_get_seg(f, &env->gdt);
1597 cpu_get_seg(f, &env->idt);
1599 qemu_get_be32s(f, &env->sysenter_cs);
1600 qemu_get_be32s(f, &env->sysenter_esp);
1601 qemu_get_be32s(f, &env->sysenter_eip);
1603 qemu_get_be32s(f, &env->cr[0]);
1604 qemu_get_be32s(f, &env->cr[2]);
1605 qemu_get_be32s(f, &env->cr[3]);
1606 qemu_get_be32s(f, &env->cr[4]);
1608 for(i = 0; i < 8; i++)
1609 qemu_get_be32s(f, &env->dr[i]);
1612 qemu_get_be32s(f, &env->a20_mask);
1614 /* XXX: compute hflags from scratch, except for CPL and IIF */
1615 env->hflags = hflags;
1620 #elif defined(TARGET_PPC)
1621 void cpu_save(QEMUFile *f, void *opaque)
1625 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1631 #warning No CPU save/restore functions
1635 /***********************************************************/
1636 /* ram save/restore */
1638 /* we just avoid storing empty pages */
1639 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1644 for(i = 1; i < len; i++) {
1648 qemu_put_byte(f, 1);
1649 qemu_put_byte(f, v);
1652 qemu_put_byte(f, 0);
1653 qemu_put_buffer(f, buf, len);
1656 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1660 v = qemu_get_byte(f);
1663 if (qemu_get_buffer(f, buf, len) != len)
1667 v = qemu_get_byte(f);
1668 memset(buf, v, len);
1676 static void ram_save(QEMUFile *f, void *opaque)
1679 qemu_put_be32(f, phys_ram_size);
1680 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1681 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1685 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1689 if (version_id != 1)
1691 if (qemu_get_be32(f) != phys_ram_size)
1693 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1694 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1701 /***********************************************************/
1702 /* main execution loop */
1704 void gui_update(void *opaque)
1706 display_state.dpy_refresh(&display_state);
1707 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1710 /* XXX: support several handlers */
1711 VMStopHandler *vm_stop_cb;
1712 VMStopHandler *vm_stop_opaque;
1714 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1717 vm_stop_opaque = opaque;
1721 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1734 void vm_stop(int reason)
1737 cpu_disable_ticks();
1741 vm_stop_cb(vm_stop_opaque, reason);
1750 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1751 IOHandlerRecord *ioh, *ioh_next;
1756 CPUState *env = global_env;
1760 ret = cpu_exec(env);
1761 if (reset_requested) {
1762 ret = EXCP_INTERRUPT;
1765 if (ret == EXCP_DEBUG) {
1766 vm_stop(EXCP_DEBUG);
1768 /* if hlt instruction, we wait until the next IRQ */
1769 /* XXX: use timeout computed from timers */
1770 if (ret == EXCP_HLT)
1783 /* poll any events */
1784 /* XXX: separate device handlers from system ones */
1786 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1787 if (!ioh->fd_can_read) {
1790 pf->events = POLLIN;
1794 max_size = ioh->fd_can_read(ioh->opaque);
1796 if (max_size > sizeof(buf))
1797 max_size = sizeof(buf);
1799 pf->events = POLLIN;
1806 ioh->max_size = max_size;
1809 ret = poll(ufds, pf - ufds, timeout);
1811 /* XXX: better handling of removal */
1812 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1813 ioh_next = ioh->next;
1816 if (pf->revents & POLLIN) {
1817 if (ioh->max_size == 0) {
1818 /* just a read event */
1819 ioh->fd_read(ioh->opaque, NULL, 0);
1821 n = read(ioh->fd, buf, ioh->max_size);
1823 ioh->fd_read(ioh->opaque, buf, n);
1824 } else if (errno != -EAGAIN) {
1825 ioh->fd_read(ioh->opaque, NULL, -errno);
1833 #if defined(CONFIG_SLIRP)
1834 /* XXX: merge with poll() */
1836 fd_set rfds, wfds, xfds;
1844 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1847 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1849 slirp_select_poll(&rfds, &wfds, &xfds);
1857 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1858 qemu_get_clock(vm_clock));
1860 if (audio_enabled) {
1861 /* XXX: add explicit timer */
1865 /* run dma transfers, if any */
1869 /* real time timers */
1870 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1871 qemu_get_clock(rt_clock));
1873 cpu_disable_ticks();
1879 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
1880 "usage: %s [options] [disk_image]\n"
1882 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1884 "Standard options:\n"
1885 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1886 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1887 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1888 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1889 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1890 "-snapshot write to temporary files instead of disk image files\n"
1891 "-m megs set virtual RAM size to megs MB\n"
1892 "-nographic disable graphical output and redirect serial I/Os to console\n"
1893 "-enable-audio enable audio support\n"
1895 "Network options:\n"
1896 "-nics n simulate 'n' network cards [default=1]\n"
1897 "-macaddr addr set the mac address of the first interface\n"
1898 "-n script set tap/tun network init script [default=%s]\n"
1899 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1901 "-user-net use user mode network stack [default if no tap/tun script]\n"
1903 "-dummy-net use dummy network stack\n"
1905 "Linux boot specific:\n"
1906 "-kernel bzImage use 'bzImage' as kernel image\n"
1907 "-append cmdline use 'cmdline' as kernel command line\n"
1908 "-initrd file use 'file' as initial ram disk\n"
1910 "Debug/Expert options:\n"
1911 "-s wait gdb connection to port %d\n"
1912 "-p port change gdb connection port\n"
1913 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1914 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1915 "-L path set the directory for the BIOS and VGA BIOS\n"
1916 #ifdef USE_CODE_COPY
1917 "-no-code-copy disable code copy acceleration\n"
1921 "During emulation, use C-a h to get terminal commands:\n",
1922 #ifdef CONFIG_SOFTMMU
1927 DEFAULT_NETWORK_SCRIPT,
1928 DEFAULT_GDBSTUB_PORT,
1931 #ifndef CONFIG_SOFTMMU
1933 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1934 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1940 struct option long_options[] = {
1941 { "initrd", 1, NULL, 0, },
1942 { "hda", 1, NULL, 0, },
1943 { "hdb", 1, NULL, 0, },
1944 { "snapshot", 0, NULL, 0, },
1945 { "hdachs", 1, NULL, 0, },
1946 { "nographic", 0, NULL, 0, },
1947 { "kernel", 1, NULL, 0, },
1948 { "append", 1, NULL, 0, },
1949 { "tun-fd", 1, NULL, 0, },
1950 { "hdc", 1, NULL, 0, },
1951 { "hdd", 1, NULL, 0, },
1952 { "cdrom", 1, NULL, 0, },
1953 { "boot", 1, NULL, 0, },
1954 { "fda", 1, NULL, 0, },
1955 { "fdb", 1, NULL, 0, },
1956 { "no-code-copy", 0, NULL, 0 },
1957 { "nics", 1, NULL, 0 },
1958 { "macaddr", 1, NULL, 0 },
1959 { "user-net", 0, NULL, 0 },
1960 { "dummy-net", 0, NULL, 0 },
1961 { "enable-audio", 0, NULL, 0 },
1962 { NULL, 0, NULL, 0 },
1965 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1967 /* this stack is only used during signal handling */
1968 #define SIGNAL_STACK_SIZE 32768
1970 static uint8_t *signal_stack;
1974 #define NET_IF_TUN 0
1975 #define NET_IF_USER 1
1976 #define NET_IF_DUMMY 2
1978 int main(int argc, char **argv)
1980 #ifdef CONFIG_GDBSTUB
1981 int use_gdbstub, gdbstub_port;
1983 int c, i, long_index, has_cdrom;
1984 int snapshot, linux_boot;
1986 const char *initrd_filename;
1987 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
1988 const char *kernel_filename, *kernel_cmdline;
1989 DisplayState *ds = &display_state;
1990 int cyls, heads, secs;
1991 int start_emulation = 1;
1993 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
1995 #if !defined(CONFIG_SOFTMMU)
1996 /* we never want that malloc() uses mmap() */
1997 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
1999 initrd_filename = NULL;
2000 for(i = 0; i < MAX_FD; i++)
2001 fd_filename[i] = NULL;
2002 for(i = 0; i < MAX_DISKS; i++)
2003 hd_filename[i] = NULL;
2004 ram_size = 32 * 1024 * 1024;
2005 vga_ram_size = VGA_RAM_SIZE;
2006 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2007 #ifdef CONFIG_GDBSTUB
2009 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2013 kernel_filename = NULL;
2014 kernel_cmdline = "";
2016 cyls = heads = secs = 0;
2021 /* default mac address of the first network interface */
2031 c = getopt_long_only(argc, argv, "hm:d:n:sp:L:S", long_options, &long_index);
2036 switch(long_index) {
2038 initrd_filename = optarg;
2041 hd_filename[0] = optarg;
2044 hd_filename[1] = optarg;
2053 cyls = strtol(p, (char **)&p, 0);
2057 heads = strtol(p, (char **)&p, 0);
2061 secs = strtol(p, (char **)&p, 0);
2072 kernel_filename = optarg;
2075 kernel_cmdline = optarg;
2081 if (nb_tun_fds < MAX_NICS) {
2082 fd = strtol(optarg, (char **)&p, 0);
2084 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2087 tun_fds[nb_tun_fds++] = fd;
2092 hd_filename[2] = optarg;
2096 hd_filename[3] = optarg;
2099 hd_filename[2] = optarg;
2103 boot_device = optarg[0];
2104 if (boot_device != 'a' && boot_device != 'b' &&
2105 boot_device != 'c' && boot_device != 'd') {
2106 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2111 fd_filename[0] = optarg;
2114 fd_filename[1] = optarg;
2117 code_copy_enabled = 0;
2120 nb_nics = atoi(optarg);
2121 if (nb_nics < 1 || nb_nics > MAX_NICS) {
2122 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2131 for(i = 0; i < 6; i++) {
2132 macaddr[i] = strtol(p, (char **)&p, 16);
2139 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2148 net_if_type = NET_IF_USER;
2151 net_if_type = NET_IF_DUMMY;
2162 ram_size = atoi(optarg) * 1024 * 1024;
2165 if (ram_size > PHYS_RAM_MAX_SIZE) {
2166 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2167 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2176 mask = cpu_str_to_log_mask(optarg);
2178 printf("Log items (comma separated):\n");
2179 for(item = cpu_log_items; item->mask != 0; item++) {
2180 printf("%-10s %s\n", item->name, item->help);
2188 pstrcpy(network_script, sizeof(network_script), optarg);
2190 #ifdef CONFIG_GDBSTUB
2195 gdbstub_port = atoi(optarg);
2202 start_emulation = 0;
2207 if (optind < argc) {
2208 hd_filename[0] = argv[optind++];
2211 linux_boot = (kernel_filename != NULL);
2213 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2214 fd_filename[0] == '\0')
2217 /* boot to cd by default if no hard disk */
2218 if (hd_filename[0] == '\0' && boot_device == 'c') {
2219 if (fd_filename[0] != '\0')
2225 #if !defined(CONFIG_SOFTMMU)
2226 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2228 static uint8_t stdout_buf[4096];
2229 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2232 setvbuf(stdout, NULL, _IOLBF, 0);
2235 /* init host network redirectors */
2236 if (net_if_type == -1) {
2237 net_if_type = NET_IF_TUN;
2238 #if defined(CONFIG_SLIRP)
2239 if (access(network_script, R_OK) < 0) {
2240 net_if_type = NET_IF_USER;
2245 for(i = 0; i < nb_nics; i++) {
2246 NetDriverState *nd = &nd_table[i];
2248 /* init virtual mac address */
2249 nd->macaddr[0] = macaddr[0];
2250 nd->macaddr[1] = macaddr[1];
2251 nd->macaddr[2] = macaddr[2];
2252 nd->macaddr[3] = macaddr[3];
2253 nd->macaddr[4] = macaddr[4];
2254 nd->macaddr[5] = macaddr[5] + i;
2255 switch(net_if_type) {
2256 #if defined(CONFIG_SLIRP)
2261 #if !defined(_WIN32)
2263 if (i < nb_tun_fds) {
2264 net_fd_init(nd, tun_fds[i]);
2266 if (net_tun_init(nd) < 0)
2278 /* init the memory */
2279 phys_ram_size = ram_size + vga_ram_size;
2281 #ifdef CONFIG_SOFTMMU
2283 /* mallocs are always aligned on BSD. */
2284 phys_ram_base = malloc(phys_ram_size);
2286 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2288 if (!phys_ram_base) {
2289 fprintf(stderr, "Could not allocate physical memory\n");
2293 /* as we must map the same page at several addresses, we must use
2298 tmpdir = getenv("QEMU_TMPDIR");
2301 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2302 if (mkstemp(phys_ram_file) < 0) {
2303 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2307 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2308 if (phys_ram_fd < 0) {
2309 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2313 ftruncate(phys_ram_fd, phys_ram_size);
2314 unlink(phys_ram_file);
2315 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2317 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2319 if (phys_ram_base == MAP_FAILED) {
2320 fprintf(stderr, "Could not map physical memory\n");
2326 /* we always create the cdrom drive, even if no disk is there */
2328 bs_table[2] = bdrv_new("cdrom");
2329 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2332 /* open the virtual block devices */
2333 for(i = 0; i < MAX_DISKS; i++) {
2334 if (hd_filename[i]) {
2337 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2338 bs_table[i] = bdrv_new(buf);
2340 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2341 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2345 if (i == 0 && cyls != 0)
2346 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2350 /* we always create at least one floppy disk */
2351 fd_table[0] = bdrv_new("fda");
2352 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2354 for(i = 0; i < MAX_FD; i++) {
2355 if (fd_filename[i]) {
2358 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2359 fd_table[i] = bdrv_new(buf);
2360 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2362 if (fd_filename[i] != '\0') {
2363 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2364 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2372 /* init CPU state */
2375 cpu_single_env = env;
2377 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2378 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2379 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2382 cpu_calibrate_ticks();
2386 dumb_display_init(ds);
2389 sdl_display_init(ds);
2391 dumb_display_init(ds);
2395 /* setup cpu signal handlers for MMU / self modifying code handling */
2396 #if !defined(CONFIG_SOFTMMU)
2398 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2401 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2402 stk.ss_sp = signal_stack;
2403 stk.ss_size = SIGNAL_STACK_SIZE;
2406 if (sigaltstack(&stk, NULL) < 0) {
2407 perror("sigaltstack");
2413 struct sigaction act;
2415 sigfillset(&act.sa_mask);
2416 act.sa_flags = SA_SIGINFO;
2417 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2418 act.sa_flags |= SA_ONSTACK;
2420 act.sa_sigaction = host_segv_handler;
2421 sigaction(SIGSEGV, &act, NULL);
2422 sigaction(SIGBUS, &act, NULL);
2423 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2424 sigaction(SIGFPE, &act, NULL);
2431 struct sigaction act;
2432 sigfillset(&act.sa_mask);
2434 act.sa_handler = SIG_IGN;
2435 sigaction(SIGPIPE, &act, NULL);
2440 #if defined(TARGET_I386)
2441 pc_init(ram_size, vga_ram_size, boot_device,
2442 ds, fd_filename, snapshot,
2443 kernel_filename, kernel_cmdline, initrd_filename);
2444 #elif defined(TARGET_PPC)
2445 ppc_init(ram_size, vga_ram_size, boot_device,
2446 ds, fd_filename, snapshot,
2447 kernel_filename, kernel_cmdline, initrd_filename);
2450 /* launched after the device init so that it can display or not a
2454 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2455 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2457 #ifdef CONFIG_GDBSTUB
2459 if (gdbserver_start(gdbstub_port) < 0) {
2460 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2464 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2468 if (start_emulation)