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
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
46 #include <linux/if_tun.h>
49 #include <linux/rtc.h>
53 #if defined(CONFIG_SLIRP)
59 #include <sys/timeb.h>
61 #define getopt_long_only getopt_long
62 #define memalign(align, size) malloc(size)
66 #if defined(__linux__)
67 /* SDL use the pthreads and they modify sigaction. We don't
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)
73 extern void __sigaction();
74 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
76 #endif /* __linux__ */
77 #endif /* CONFIG_SDL */
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)
93 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
97 #define DEFAULT_RAM_SIZE 144
99 #define DEFAULT_RAM_SIZE 32
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];
117 static DisplayState display_state;
119 int64_t ticks_per_sec;
120 int boot_device = 'c';
122 static char network_script[1024];
123 int pit_min_timer_count = 0;
125 NetDriverState nd_table[MAX_NICS];
126 SerialState *serial_console;
127 QEMUTimer *gui_timer;
129 int audio_enabled = 0;
131 int prep_enabled = 0;
134 /***********************************************************/
135 /* x86 ISA bus support */
137 target_phys_addr_t isa_mem_base = 0;
139 uint32_t default_ioport_readb(void *opaque, uint32_t address)
141 #ifdef DEBUG_UNUSED_IOPORT
142 fprintf(stderr, "inb: port=0x%04x\n", address);
147 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
149 #ifdef DEBUG_UNUSED_IOPORT
150 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
154 /* default is to make two byte accesses */
155 uint32_t default_ioport_readw(void *opaque, uint32_t address)
158 data = ioport_read_table[0][address](ioport_opaque[address], address);
159 address = (address + 1) & (MAX_IOPORTS - 1);
160 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
164 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
166 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
167 address = (address + 1) & (MAX_IOPORTS - 1);
168 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
171 uint32_t default_ioport_readl(void *opaque, uint32_t address)
173 #ifdef DEBUG_UNUSED_IOPORT
174 fprintf(stderr, "inl: port=0x%04x\n", address);
179 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
181 #ifdef DEBUG_UNUSED_IOPORT
182 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
186 void init_ioports(void)
190 for(i = 0; i < MAX_IOPORTS; i++) {
191 ioport_read_table[0][i] = default_ioport_readb;
192 ioport_write_table[0][i] = default_ioport_writeb;
193 ioport_read_table[1][i] = default_ioport_readw;
194 ioport_write_table[1][i] = default_ioport_writew;
195 ioport_read_table[2][i] = default_ioport_readl;
196 ioport_write_table[2][i] = default_ioport_writel;
200 /* size is the word size in byte */
201 int register_ioport_read(int start, int length, int size,
202 IOPortReadFunc *func, void *opaque)
208 } else if (size == 2) {
210 } else if (size == 4) {
213 hw_error("register_ioport_read: invalid size");
216 for(i = start; i < start + length; i += size) {
217 ioport_read_table[bsize][i] = func;
218 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
219 hw_error("register_ioport_read: invalid opaque");
220 ioport_opaque[i] = opaque;
225 /* size is the word size in byte */
226 int register_ioport_write(int start, int length, int size,
227 IOPortWriteFunc *func, void *opaque)
233 } else if (size == 2) {
235 } else if (size == 4) {
238 hw_error("register_ioport_write: invalid size");
241 for(i = start; i < start + length; i += size) {
242 ioport_write_table[bsize][i] = func;
243 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
244 hw_error("register_ioport_read: invalid opaque");
245 ioport_opaque[i] = opaque;
250 void isa_unassign_ioport(int start, int length)
254 for(i = start; i < start + length; i++) {
255 ioport_read_table[0][i] = default_ioport_readb;
256 ioport_read_table[1][i] = default_ioport_readw;
257 ioport_read_table[2][i] = default_ioport_readl;
259 ioport_write_table[0][i] = default_ioport_writeb;
260 ioport_write_table[1][i] = default_ioport_writew;
261 ioport_write_table[2][i] = default_ioport_writel;
265 void pstrcpy(char *buf, int buf_size, const char *str)
275 if (c == 0 || q >= buf + buf_size - 1)
282 /* strcat and truncate. */
283 char *pstrcat(char *buf, int buf_size, const char *s)
288 pstrcpy(buf + len, buf_size - len, s);
292 /* return the size or -1 if error */
293 int load_image(const char *filename, uint8_t *addr)
296 fd = open(filename, O_RDONLY | O_BINARY);
299 size = lseek(fd, 0, SEEK_END);
300 lseek(fd, 0, SEEK_SET);
301 if (read(fd, addr, size) != size) {
309 void cpu_outb(CPUState *env, int addr, int val)
312 if (loglevel & CPU_LOG_IOPORT)
313 fprintf(logfile, "outb: %04x %02x\n", addr, val);
315 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
318 void cpu_outw(CPUState *env, int addr, int val)
321 if (loglevel & CPU_LOG_IOPORT)
322 fprintf(logfile, "outw: %04x %04x\n", addr, val);
324 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
327 void cpu_outl(CPUState *env, int addr, int val)
330 if (loglevel & CPU_LOG_IOPORT)
331 fprintf(logfile, "outl: %04x %08x\n", addr, val);
333 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
336 int cpu_inb(CPUState *env, int addr)
339 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
341 if (loglevel & CPU_LOG_IOPORT)
342 fprintf(logfile, "inb : %04x %02x\n", addr, val);
347 int cpu_inw(CPUState *env, int addr)
350 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
352 if (loglevel & CPU_LOG_IOPORT)
353 fprintf(logfile, "inw : %04x %04x\n", addr, val);
358 int cpu_inl(CPUState *env, int addr)
361 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
363 if (loglevel & CPU_LOG_IOPORT)
364 fprintf(logfile, "inl : %04x %08x\n", addr, val);
369 /***********************************************************/
370 void hw_error(const char *fmt, ...)
375 fprintf(stderr, "qemu: hardware error: ");
376 vfprintf(stderr, fmt, ap);
377 fprintf(stderr, "\n");
379 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
381 cpu_dump_state(global_env, stderr, 0);
387 /***********************************************************/
390 #if defined(__powerpc__)
392 static inline uint32_t get_tbl(void)
395 asm volatile("mftb %0" : "=r" (tbl));
399 static inline uint32_t get_tbu(void)
402 asm volatile("mftbu %0" : "=r" (tbl));
406 int64_t cpu_get_real_ticks(void)
409 /* NOTE: we test if wrapping has occurred */
415 return ((int64_t)h << 32) | l;
418 #elif defined(__i386__)
420 int64_t cpu_get_real_ticks(void)
423 asm volatile ("rdtsc" : "=A" (val));
427 #elif defined(__x86_64__)
429 int64_t cpu_get_real_ticks(void)
433 asm volatile("rdtsc" : "=a" (low), "=d" (high));
441 #error unsupported CPU
444 static int64_t cpu_ticks_offset;
445 static int cpu_ticks_enabled;
447 static inline int64_t cpu_get_ticks(void)
449 if (!cpu_ticks_enabled) {
450 return cpu_ticks_offset;
452 return cpu_get_real_ticks() + cpu_ticks_offset;
456 /* enable cpu_get_ticks() */
457 void cpu_enable_ticks(void)
459 if (!cpu_ticks_enabled) {
460 cpu_ticks_offset -= cpu_get_real_ticks();
461 cpu_ticks_enabled = 1;
465 /* disable cpu_get_ticks() : the clock is stopped. You must not call
466 cpu_get_ticks() after that. */
467 void cpu_disable_ticks(void)
469 if (cpu_ticks_enabled) {
470 cpu_ticks_offset = cpu_get_ticks();
471 cpu_ticks_enabled = 0;
475 static int64_t get_clock(void)
480 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
483 gettimeofday(&tv, NULL);
484 return tv.tv_sec * 1000000LL + tv.tv_usec;
488 void cpu_calibrate_ticks(void)
493 ticks = cpu_get_real_ticks();
499 usec = get_clock() - usec;
500 ticks = cpu_get_real_ticks() - ticks;
501 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
504 /* compute with 96 bit intermediate result: (a*b)/c */
505 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
510 #ifdef WORDS_BIGENDIAN
520 rl = (uint64_t)u.l.low * (uint64_t)b;
521 rh = (uint64_t)u.l.high * (uint64_t)b;
524 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
528 #define QEMU_TIMER_REALTIME 0
529 #define QEMU_TIMER_VIRTUAL 1
533 /* XXX: add frequency */
541 struct QEMUTimer *next;
547 static QEMUTimer *active_timers[2];
549 static MMRESULT timerID;
551 /* frequency of the times() clock tick */
552 static int timer_freq;
555 QEMUClock *qemu_new_clock(int type)
558 clock = qemu_mallocz(sizeof(QEMUClock));
565 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
569 ts = qemu_mallocz(sizeof(QEMUTimer));
576 void qemu_free_timer(QEMUTimer *ts)
581 /* stop a timer, but do not dealloc it */
582 void qemu_del_timer(QEMUTimer *ts)
586 /* NOTE: this code must be signal safe because
587 qemu_timer_expired() can be called from a signal. */
588 pt = &active_timers[ts->clock->type];
601 /* modify the current timer so that it will be fired when current_time
602 >= expire_time. The corresponding callback will be called. */
603 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
609 /* add the timer in the sorted list */
610 /* NOTE: this code must be signal safe because
611 qemu_timer_expired() can be called from a signal. */
612 pt = &active_timers[ts->clock->type];
617 if (t->expire_time > expire_time)
621 ts->expire_time = expire_time;
626 int qemu_timer_pending(QEMUTimer *ts)
629 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
636 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
640 return (timer_head->expire_time <= current_time);
643 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
649 if (ts->expire_time > current_time)
651 /* remove timer from the list before calling the callback */
652 *ptimer_head = ts->next;
655 /* run the callback (the timer list can be modified) */
660 int64_t qemu_get_clock(QEMUClock *clock)
662 switch(clock->type) {
663 case QEMU_TIMER_REALTIME:
665 return GetTickCount();
670 /* Note that using gettimeofday() is not a good solution
671 for timers because its value change when the date is
673 if (timer_freq == 100) {
674 return times(&tp) * 10;
676 return ((int64_t)times(&tp) * 1000) / timer_freq;
681 case QEMU_TIMER_VIRTUAL:
682 return cpu_get_ticks();
687 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
689 uint64_t expire_time;
691 if (qemu_timer_pending(ts)) {
692 expire_time = ts->expire_time;
696 qemu_put_be64(f, expire_time);
699 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
701 uint64_t expire_time;
703 expire_time = qemu_get_be64(f);
704 if (expire_time != -1) {
705 qemu_mod_timer(ts, expire_time);
711 static void timer_save(QEMUFile *f, void *opaque)
713 if (cpu_ticks_enabled) {
714 hw_error("cannot save state if virtual timers are running");
716 qemu_put_be64s(f, &cpu_ticks_offset);
717 qemu_put_be64s(f, &ticks_per_sec);
720 static int timer_load(QEMUFile *f, void *opaque, int version_id)
724 if (cpu_ticks_enabled) {
727 qemu_get_be64s(f, &cpu_ticks_offset);
728 qemu_get_be64s(f, &ticks_per_sec);
733 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
734 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
736 static void host_alarm_handler(int host_signum)
739 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
740 qemu_get_clock(vm_clock)) ||
741 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
742 qemu_get_clock(rt_clock))) {
743 /* stop the cpu because a timer occured */
744 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
750 #if defined(__linux__)
752 #define RTC_FREQ 1024
756 static int start_rtc_timer(void)
758 rtc_fd = open("/dev/rtc", O_RDONLY);
761 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
762 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
763 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
764 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
767 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
772 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
778 static int start_rtc_timer(void)
783 #endif /* !defined(__linux__) */
785 #endif /* !defined(_WIN32) */
787 static void init_timers(void)
789 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
790 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
795 timerID = timeSetEvent(10, // interval (ms)
797 host_alarm_handler, // function
798 (DWORD)&count, // user parameter
799 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
801 perror("failed timer alarm");
805 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
808 struct sigaction act;
809 struct itimerval itv;
811 /* get times() syscall frequency */
812 timer_freq = sysconf(_SC_CLK_TCK);
815 sigfillset(&act.sa_mask);
817 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
818 act.sa_flags |= SA_ONSTACK;
820 act.sa_handler = host_alarm_handler;
821 sigaction(SIGALRM, &act, NULL);
823 itv.it_interval.tv_sec = 0;
824 itv.it_interval.tv_usec = 1000;
825 itv.it_value.tv_sec = 0;
826 itv.it_value.tv_usec = 10 * 1000;
827 setitimer(ITIMER_REAL, &itv, NULL);
828 /* we probe the tick duration of the kernel to inform the user if
829 the emulated kernel requested a too high timer frequency */
830 getitimer(ITIMER_REAL, &itv);
832 if (itv.it_interval.tv_usec > 1000) {
833 /* try to use /dev/rtc to have a faster timer */
834 if (start_rtc_timer() < 0)
837 itv.it_interval.tv_sec = 0;
838 itv.it_interval.tv_usec = 0;
839 itv.it_value.tv_sec = 0;
840 itv.it_value.tv_usec = 0;
841 setitimer(ITIMER_REAL, &itv, NULL);
844 sigaction(SIGIO, &act, NULL);
845 fcntl(rtc_fd, F_SETFL, O_ASYNC);
846 fcntl(rtc_fd, F_SETOWN, getpid());
849 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
856 void quit_timers(void)
859 timeKillEvent(timerID);
863 /***********************************************************/
868 int serial_open_device(void)
875 int serial_open_device(void)
877 char slave_name[1024];
878 int master_fd, slave_fd;
880 if (serial_console == NULL && nographic) {
881 /* use console for serial port */
886 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
887 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
890 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
900 /***********************************************************/
901 /* Linux network device redirectors */
903 void hex_dump(FILE *f, const uint8_t *buf, int size)
907 for(i=0;i<size;i+=16) {
911 fprintf(f, "%08x ", i);
914 fprintf(f, " %02x", buf[i+j]);
921 if (c < ' ' || c > '~')
929 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
931 nd->send_packet(nd, buf, size);
934 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
935 IOReadHandler *fd_read, void *opaque)
937 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
940 /* dummy network adapter */
942 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
946 static void dummy_add_read_packet(NetDriverState *nd,
947 IOCanRWHandler *fd_can_read,
948 IOReadHandler *fd_read, void *opaque)
952 static int net_dummy_init(NetDriverState *nd)
954 nd->send_packet = dummy_send_packet;
955 nd->add_read_packet = dummy_add_read_packet;
956 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
960 #if defined(CONFIG_SLIRP)
962 /* slirp network adapter */
964 static void *slirp_fd_opaque;
965 static IOCanRWHandler *slirp_fd_can_read;
966 static IOReadHandler *slirp_fd_read;
967 static int slirp_inited;
969 int slirp_can_output(void)
971 return slirp_fd_can_read(slirp_fd_opaque);
974 void slirp_output(const uint8_t *pkt, int pkt_len)
978 hex_dump(stdout, pkt, pkt_len);
980 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
983 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
987 hex_dump(stdout, buf, size);
989 slirp_input(buf, size);
992 static void slirp_add_read_packet(NetDriverState *nd,
993 IOCanRWHandler *fd_can_read,
994 IOReadHandler *fd_read, void *opaque)
996 slirp_fd_opaque = opaque;
997 slirp_fd_can_read = fd_can_read;
998 slirp_fd_read = fd_read;
1001 static int net_slirp_init(NetDriverState *nd)
1003 if (!slirp_inited) {
1007 nd->send_packet = slirp_send_packet;
1008 nd->add_read_packet = slirp_add_read_packet;
1009 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1013 #endif /* CONFIG_SLIRP */
1015 #if !defined(_WIN32)
1017 static int tun_open(char *ifname, int ifname_size)
1023 fd = open("/dev/tap", O_RDWR);
1025 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1030 dev = devname(s.st_rdev, S_IFCHR);
1031 pstrcpy(ifname, ifname_size, dev);
1033 fcntl(fd, F_SETFL, O_NONBLOCK);
1037 static int tun_open(char *ifname, int ifname_size)
1042 fd = open("/dev/net/tun", O_RDWR);
1044 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1047 memset(&ifr, 0, sizeof(ifr));
1048 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1049 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1050 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1052 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1056 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1057 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1058 fcntl(fd, F_SETFL, O_NONBLOCK);
1063 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1065 write(nd->fd, buf, size);
1068 static void tun_add_read_packet(NetDriverState *nd,
1069 IOCanRWHandler *fd_can_read,
1070 IOReadHandler *fd_read, void *opaque)
1072 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1075 static int net_tun_init(NetDriverState *nd)
1081 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1085 /* try to launch network init script */
1090 *parg++ = network_script;
1091 *parg++ = nd->ifname;
1093 execv(network_script, args);
1096 while (waitpid(pid, &status, 0) != pid);
1097 if (!WIFEXITED(status) ||
1098 WEXITSTATUS(status) != 0) {
1099 fprintf(stderr, "%s: could not launch network script\n",
1103 nd->send_packet = tun_send_packet;
1104 nd->add_read_packet = tun_add_read_packet;
1108 static int net_fd_init(NetDriverState *nd, int fd)
1111 nd->send_packet = tun_send_packet;
1112 nd->add_read_packet = tun_add_read_packet;
1113 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1117 #endif /* !_WIN32 */
1119 /***********************************************************/
1124 static void term_exit(void)
1128 static void term_init(void)
1134 /* init terminal so that we can grab keys */
1135 static struct termios oldtty;
1137 static void term_exit(void)
1139 tcsetattr (0, TCSANOW, &oldtty);
1142 static void term_init(void)
1146 tcgetattr (0, &tty);
1149 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1150 |INLCR|IGNCR|ICRNL|IXON);
1151 tty.c_oflag |= OPOST;
1152 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1153 /* if graphical mode, we allow Ctrl-C handling */
1155 tty.c_lflag &= ~ISIG;
1156 tty.c_cflag &= ~(CSIZE|PARENB);
1159 tty.c_cc[VTIME] = 0;
1161 tcsetattr (0, TCSANOW, &tty);
1165 fcntl(0, F_SETFL, O_NONBLOCK);
1170 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1174 static void dumb_resize(DisplayState *ds, int w, int h)
1178 static void dumb_refresh(DisplayState *ds)
1180 vga_update_display();
1183 void dumb_display_init(DisplayState *ds)
1188 ds->dpy_update = dumb_update;
1189 ds->dpy_resize = dumb_resize;
1190 ds->dpy_refresh = dumb_refresh;
1193 #if !defined(CONFIG_SOFTMMU)
1194 /***********************************************************/
1195 /* cpu signal handler */
1196 static void host_segv_handler(int host_signum, siginfo_t *info,
1199 if (cpu_signal_handler(host_signum, info, puc))
1206 /***********************************************************/
1209 #define MAX_IO_HANDLERS 64
1211 typedef struct IOHandlerRecord {
1213 IOCanRWHandler *fd_can_read;
1214 IOReadHandler *fd_read;
1216 /* temporary data */
1219 struct IOHandlerRecord *next;
1222 static IOHandlerRecord *first_io_handler;
1224 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1225 IOReadHandler *fd_read, void *opaque)
1227 IOHandlerRecord *ioh;
1229 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1233 ioh->fd_can_read = fd_can_read;
1234 ioh->fd_read = fd_read;
1235 ioh->opaque = opaque;
1236 ioh->next = first_io_handler;
1237 first_io_handler = ioh;
1241 void qemu_del_fd_read_handler(int fd)
1243 IOHandlerRecord **pioh, *ioh;
1245 pioh = &first_io_handler;
1250 if (ioh->fd == fd) {
1258 /***********************************************************/
1259 /* savevm/loadvm support */
1261 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1263 fwrite(buf, 1, size, f);
1266 void qemu_put_byte(QEMUFile *f, int v)
1271 void qemu_put_be16(QEMUFile *f, unsigned int v)
1273 qemu_put_byte(f, v >> 8);
1274 qemu_put_byte(f, v);
1277 void qemu_put_be32(QEMUFile *f, unsigned int v)
1279 qemu_put_byte(f, v >> 24);
1280 qemu_put_byte(f, v >> 16);
1281 qemu_put_byte(f, v >> 8);
1282 qemu_put_byte(f, v);
1285 void qemu_put_be64(QEMUFile *f, uint64_t v)
1287 qemu_put_be32(f, v >> 32);
1288 qemu_put_be32(f, v);
1291 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1293 return fread(buf, 1, size, f);
1296 int qemu_get_byte(QEMUFile *f)
1306 unsigned int qemu_get_be16(QEMUFile *f)
1309 v = qemu_get_byte(f) << 8;
1310 v |= qemu_get_byte(f);
1314 unsigned int qemu_get_be32(QEMUFile *f)
1317 v = qemu_get_byte(f) << 24;
1318 v |= qemu_get_byte(f) << 16;
1319 v |= qemu_get_byte(f) << 8;
1320 v |= qemu_get_byte(f);
1324 uint64_t qemu_get_be64(QEMUFile *f)
1327 v = (uint64_t)qemu_get_be32(f) << 32;
1328 v |= qemu_get_be32(f);
1332 int64_t qemu_ftell(QEMUFile *f)
1337 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1339 if (fseek(f, pos, whence) < 0)
1344 typedef struct SaveStateEntry {
1348 SaveStateHandler *save_state;
1349 LoadStateHandler *load_state;
1351 struct SaveStateEntry *next;
1354 static SaveStateEntry *first_se;
1356 int register_savevm(const char *idstr,
1359 SaveStateHandler *save_state,
1360 LoadStateHandler *load_state,
1363 SaveStateEntry *se, **pse;
1365 se = qemu_malloc(sizeof(SaveStateEntry));
1368 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1369 se->instance_id = instance_id;
1370 se->version_id = version_id;
1371 se->save_state = save_state;
1372 se->load_state = load_state;
1373 se->opaque = opaque;
1376 /* add at the end of list */
1378 while (*pse != NULL)
1379 pse = &(*pse)->next;
1384 #define QEMU_VM_FILE_MAGIC 0x5145564d
1385 #define QEMU_VM_FILE_VERSION 0x00000001
1387 int qemu_savevm(const char *filename)
1391 int len, len_pos, cur_pos, saved_vm_running, ret;
1393 saved_vm_running = vm_running;
1396 f = fopen(filename, "wb");
1402 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1403 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1405 for(se = first_se; se != NULL; se = se->next) {
1407 len = strlen(se->idstr);
1408 qemu_put_byte(f, len);
1409 qemu_put_buffer(f, se->idstr, len);
1411 qemu_put_be32(f, se->instance_id);
1412 qemu_put_be32(f, se->version_id);
1414 /* record size: filled later */
1416 qemu_put_be32(f, 0);
1418 se->save_state(f, se->opaque);
1420 /* fill record size */
1422 len = ftell(f) - len_pos - 4;
1423 fseek(f, len_pos, SEEK_SET);
1424 qemu_put_be32(f, len);
1425 fseek(f, cur_pos, SEEK_SET);
1431 if (saved_vm_running)
1436 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1440 for(se = first_se; se != NULL; se = se->next) {
1441 if (!strcmp(se->idstr, idstr) &&
1442 instance_id == se->instance_id)
1448 int qemu_loadvm(const char *filename)
1452 int len, cur_pos, ret, instance_id, record_len, version_id;
1453 int saved_vm_running;
1457 saved_vm_running = vm_running;
1460 f = fopen(filename, "rb");
1466 v = qemu_get_be32(f);
1467 if (v != QEMU_VM_FILE_MAGIC)
1469 v = qemu_get_be32(f);
1470 if (v != QEMU_VM_FILE_VERSION) {
1477 #if defined (DO_TB_FLUSH)
1478 tb_flush(global_env);
1480 len = qemu_get_byte(f);
1483 qemu_get_buffer(f, idstr, len);
1485 instance_id = qemu_get_be32(f);
1486 version_id = qemu_get_be32(f);
1487 record_len = qemu_get_be32(f);
1489 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1490 idstr, instance_id, version_id, record_len);
1493 se = find_se(idstr, instance_id);
1495 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1496 instance_id, idstr);
1498 ret = se->load_state(f, se->opaque, version_id);
1500 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1501 instance_id, idstr);
1504 /* always seek to exact end of record */
1505 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1510 if (saved_vm_running)
1515 /***********************************************************/
1516 /* cpu save/restore */
1518 #if defined(TARGET_I386)
1520 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1522 qemu_put_be32(f, (uint32_t)dt->base);
1523 qemu_put_be32(f, dt->limit);
1524 qemu_put_be32(f, dt->flags);
1527 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1529 dt->base = (uint8_t *)qemu_get_be32(f);
1530 dt->limit = qemu_get_be32(f);
1531 dt->flags = qemu_get_be32(f);
1534 void cpu_save(QEMUFile *f, void *opaque)
1536 CPUState *env = opaque;
1537 uint16_t fptag, fpus, fpuc;
1541 for(i = 0; i < 8; i++)
1542 qemu_put_be32s(f, &env->regs[i]);
1543 qemu_put_be32s(f, &env->eip);
1544 qemu_put_be32s(f, &env->eflags);
1545 qemu_put_be32s(f, &env->eflags);
1546 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1547 qemu_put_be32s(f, &hflags);
1551 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1553 for (i=7; i>=0; i--) {
1555 if (env->fptags[i]) {
1560 qemu_put_be16s(f, &fpuc);
1561 qemu_put_be16s(f, &fpus);
1562 qemu_put_be16s(f, &fptag);
1564 for(i = 0; i < 8; i++) {
1567 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1568 qemu_put_be64(f, mant);
1569 qemu_put_be16(f, exp);
1572 for(i = 0; i < 6; i++)
1573 cpu_put_seg(f, &env->segs[i]);
1574 cpu_put_seg(f, &env->ldt);
1575 cpu_put_seg(f, &env->tr);
1576 cpu_put_seg(f, &env->gdt);
1577 cpu_put_seg(f, &env->idt);
1579 qemu_put_be32s(f, &env->sysenter_cs);
1580 qemu_put_be32s(f, &env->sysenter_esp);
1581 qemu_put_be32s(f, &env->sysenter_eip);
1583 qemu_put_be32s(f, &env->cr[0]);
1584 qemu_put_be32s(f, &env->cr[2]);
1585 qemu_put_be32s(f, &env->cr[3]);
1586 qemu_put_be32s(f, &env->cr[4]);
1588 for(i = 0; i < 8; i++)
1589 qemu_put_be32s(f, &env->dr[i]);
1592 qemu_put_be32s(f, &env->a20_mask);
1595 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1597 CPUState *env = opaque;
1600 uint16_t fpus, fpuc, fptag;
1602 if (version_id != 1)
1604 for(i = 0; i < 8; i++)
1605 qemu_get_be32s(f, &env->regs[i]);
1606 qemu_get_be32s(f, &env->eip);
1607 qemu_get_be32s(f, &env->eflags);
1608 qemu_get_be32s(f, &env->eflags);
1609 qemu_get_be32s(f, &hflags);
1611 qemu_get_be16s(f, &fpuc);
1612 qemu_get_be16s(f, &fpus);
1613 qemu_get_be16s(f, &fptag);
1615 for(i = 0; i < 8; i++) {
1618 mant = qemu_get_be64(f);
1619 exp = qemu_get_be16(f);
1620 env->fpregs[i] = cpu_set_fp80(mant, exp);
1624 env->fpstt = (fpus >> 11) & 7;
1625 env->fpus = fpus & ~0x3800;
1626 for(i = 0; i < 8; i++) {
1627 env->fptags[i] = ((fptag & 3) == 3);
1631 for(i = 0; i < 6; i++)
1632 cpu_get_seg(f, &env->segs[i]);
1633 cpu_get_seg(f, &env->ldt);
1634 cpu_get_seg(f, &env->tr);
1635 cpu_get_seg(f, &env->gdt);
1636 cpu_get_seg(f, &env->idt);
1638 qemu_get_be32s(f, &env->sysenter_cs);
1639 qemu_get_be32s(f, &env->sysenter_esp);
1640 qemu_get_be32s(f, &env->sysenter_eip);
1642 qemu_get_be32s(f, &env->cr[0]);
1643 qemu_get_be32s(f, &env->cr[2]);
1644 qemu_get_be32s(f, &env->cr[3]);
1645 qemu_get_be32s(f, &env->cr[4]);
1647 for(i = 0; i < 8; i++)
1648 qemu_get_be32s(f, &env->dr[i]);
1651 qemu_get_be32s(f, &env->a20_mask);
1653 /* XXX: compute hflags from scratch, except for CPL and IIF */
1654 env->hflags = hflags;
1659 #elif defined(TARGET_PPC)
1660 void cpu_save(QEMUFile *f, void *opaque)
1664 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1670 #warning No CPU save/restore functions
1674 /***********************************************************/
1675 /* ram save/restore */
1677 /* we just avoid storing empty pages */
1678 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1683 for(i = 1; i < len; i++) {
1687 qemu_put_byte(f, 1);
1688 qemu_put_byte(f, v);
1691 qemu_put_byte(f, 0);
1692 qemu_put_buffer(f, buf, len);
1695 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1699 v = qemu_get_byte(f);
1702 if (qemu_get_buffer(f, buf, len) != len)
1706 v = qemu_get_byte(f);
1707 memset(buf, v, len);
1715 static void ram_save(QEMUFile *f, void *opaque)
1718 qemu_put_be32(f, phys_ram_size);
1719 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1720 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1724 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1728 if (version_id != 1)
1730 if (qemu_get_be32(f) != phys_ram_size)
1732 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1733 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1740 /***********************************************************/
1741 /* main execution loop */
1743 void gui_update(void *opaque)
1745 display_state.dpy_refresh(&display_state);
1746 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1749 /* XXX: support several handlers */
1750 VMStopHandler *vm_stop_cb;
1751 VMStopHandler *vm_stop_opaque;
1753 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1756 vm_stop_opaque = opaque;
1760 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1773 void vm_stop(int reason)
1776 cpu_disable_ticks();
1780 vm_stop_cb(vm_stop_opaque, reason);
1789 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1790 IOHandlerRecord *ioh, *ioh_next;
1795 CPUState *env = global_env;
1799 ret = cpu_exec(env);
1800 if (reset_requested) {
1801 ret = EXCP_INTERRUPT;
1804 if (ret == EXCP_DEBUG) {
1805 vm_stop(EXCP_DEBUG);
1807 /* if hlt instruction, we wait until the next IRQ */
1808 /* XXX: use timeout computed from timers */
1809 if (ret == EXCP_HLT)
1822 /* poll any events */
1823 /* XXX: separate device handlers from system ones */
1825 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1826 if (!ioh->fd_can_read) {
1829 pf->events = POLLIN;
1833 max_size = ioh->fd_can_read(ioh->opaque);
1835 if (max_size > sizeof(buf))
1836 max_size = sizeof(buf);
1838 pf->events = POLLIN;
1845 ioh->max_size = max_size;
1848 ret = poll(ufds, pf - ufds, timeout);
1850 /* XXX: better handling of removal */
1851 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
1852 ioh_next = ioh->next;
1855 if (pf->revents & POLLIN) {
1856 if (ioh->max_size == 0) {
1857 /* just a read event */
1858 ioh->fd_read(ioh->opaque, NULL, 0);
1860 n = read(ioh->fd, buf, ioh->max_size);
1862 ioh->fd_read(ioh->opaque, buf, n);
1863 } else if (errno != EAGAIN) {
1864 ioh->fd_read(ioh->opaque, NULL, -errno);
1872 #if defined(CONFIG_SLIRP)
1873 /* XXX: merge with poll() */
1875 fd_set rfds, wfds, xfds;
1883 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
1886 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
1888 slirp_select_poll(&rfds, &wfds, &xfds);
1896 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
1897 qemu_get_clock(vm_clock));
1899 if (audio_enabled) {
1900 /* XXX: add explicit timer */
1904 /* run dma transfers, if any */
1908 /* real time timers */
1909 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
1910 qemu_get_clock(rt_clock));
1912 cpu_disable_ticks();
1918 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
1919 "usage: %s [options] [disk_image]\n"
1921 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1923 "Standard options:\n"
1924 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1925 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1926 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1927 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1928 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1929 "-snapshot write to temporary files instead of disk image files\n"
1930 "-m megs set virtual RAM size to megs MB [default=%d]\n"
1931 "-nographic disable graphical output and redirect serial I/Os to console\n"
1932 "-enable-audio enable audio support\n"
1934 "Network options:\n"
1935 "-nics n simulate 'n' network cards [default=1]\n"
1936 "-macaddr addr set the mac address of the first interface\n"
1937 "-n script set tap/tun network init script [default=%s]\n"
1938 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1940 "-user-net use user mode network stack [default if no tap/tun script]\n"
1942 "-dummy-net use dummy network stack\n"
1944 "Linux boot specific:\n"
1945 "-kernel bzImage use 'bzImage' as kernel image\n"
1946 "-append cmdline use 'cmdline' as kernel command line\n"
1947 "-initrd file use 'file' as initial ram disk\n"
1949 "Debug/Expert options:\n"
1950 "-S freeze CPU at startup (use 'c' to start execution)\n"
1951 "-s wait gdb connection to port %d\n"
1952 "-p port change gdb connection port\n"
1953 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1954 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1955 "-L path set the directory for the BIOS and VGA BIOS\n"
1956 #ifdef USE_CODE_COPY
1957 "-no-code-copy disable code copy acceleration\n"
1961 "During emulation, use C-a h to get terminal commands:\n",
1962 #ifdef CONFIG_SOFTMMU
1968 DEFAULT_NETWORK_SCRIPT,
1969 DEFAULT_GDBSTUB_PORT,
1972 #ifndef CONFIG_SOFTMMU
1974 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1975 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1981 #define HAS_ARG 0x0001
1994 QEMU_OPTION_snapshot,
1996 QEMU_OPTION_nographic,
1997 QEMU_OPTION_enable_audio,
2000 QEMU_OPTION_macaddr,
2003 QEMU_OPTION_user_net,
2004 QEMU_OPTION_dummy_net,
2016 QEMU_OPTION_no_code_copy,
2019 QEMU_OPTION_localtime,
2022 typedef struct QEMUOption {
2028 const QEMUOption qemu_options[] = {
2029 { "h", 0, QEMU_OPTION_h },
2031 { "fda", HAS_ARG, QEMU_OPTION_fda },
2032 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2033 { "hda", HAS_ARG, QEMU_OPTION_hda },
2034 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2035 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2036 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2037 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2038 { "boot", HAS_ARG, QEMU_OPTION_boot },
2039 { "snapshot", 0, QEMU_OPTION_snapshot },
2040 { "m", HAS_ARG, QEMU_OPTION_m },
2041 { "nographic", 0, QEMU_OPTION_nographic },
2042 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2044 { "nics", HAS_ARG, QEMU_OPTION_nics},
2045 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2046 { "n", HAS_ARG, QEMU_OPTION_n },
2047 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2049 { "user-net", 0, QEMU_OPTION_user_net },
2051 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2053 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2054 { "append", HAS_ARG, QEMU_OPTION_append },
2055 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2057 { "S", 0, QEMU_OPTION_S },
2058 { "s", 0, QEMU_OPTION_s },
2059 { "p", HAS_ARG, QEMU_OPTION_p },
2060 { "d", HAS_ARG, QEMU_OPTION_d },
2061 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2062 { "L", HAS_ARG, QEMU_OPTION_L },
2063 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2065 /* temporary options */
2066 { "pci", 0, QEMU_OPTION_pci },
2068 { "prep", 0, QEMU_OPTION_prep },
2070 { "localtime", 0, QEMU_OPTION_localtime },
2074 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2076 /* this stack is only used during signal handling */
2077 #define SIGNAL_STACK_SIZE 32768
2079 static uint8_t *signal_stack;
2083 #define NET_IF_TUN 0
2084 #define NET_IF_USER 1
2085 #define NET_IF_DUMMY 2
2087 int main(int argc, char **argv)
2089 #ifdef CONFIG_GDBSTUB
2090 int use_gdbstub, gdbstub_port;
2093 int snapshot, linux_boot;
2095 const char *initrd_filename;
2096 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2097 const char *kernel_filename, *kernel_cmdline;
2098 DisplayState *ds = &display_state;
2099 int cyls, heads, secs;
2100 int start_emulation = 1;
2102 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2104 const char *r, *optarg;
2106 #if !defined(CONFIG_SOFTMMU)
2107 /* we never want that malloc() uses mmap() */
2108 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2110 initrd_filename = NULL;
2111 for(i = 0; i < MAX_FD; i++)
2112 fd_filename[i] = NULL;
2113 for(i = 0; i < MAX_DISKS; i++)
2114 hd_filename[i] = NULL;
2115 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2116 vga_ram_size = VGA_RAM_SIZE;
2117 bios_size = BIOS_SIZE;
2118 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2119 #ifdef CONFIG_GDBSTUB
2121 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2125 kernel_filename = NULL;
2126 kernel_cmdline = "";
2128 cyls = heads = secs = 0;
2133 /* default mac address of the first network interface */
2147 hd_filename[0] = argv[optind++];
2149 const QEMUOption *popt;
2152 popt = qemu_options;
2155 fprintf(stderr, "%s: invalid option -- '%s'\n",
2159 if (!strcmp(popt->name, r + 1))
2163 if (popt->flags & HAS_ARG) {
2164 if (optind >= argc) {
2165 fprintf(stderr, "%s: option '%s' requires an argument\n",
2169 optarg = argv[optind++];
2174 switch(popt->index) {
2175 case QEMU_OPTION_initrd:
2176 initrd_filename = optarg;
2178 case QEMU_OPTION_hda:
2179 hd_filename[0] = optarg;
2181 case QEMU_OPTION_hdb:
2182 hd_filename[1] = optarg;
2184 case QEMU_OPTION_snapshot:
2187 case QEMU_OPTION_hdachs:
2191 cyls = strtol(p, (char **)&p, 0);
2195 heads = strtol(p, (char **)&p, 0);
2199 secs = strtol(p, (char **)&p, 0);
2206 case QEMU_OPTION_nographic:
2209 case QEMU_OPTION_kernel:
2210 kernel_filename = optarg;
2212 case QEMU_OPTION_append:
2213 kernel_cmdline = optarg;
2215 case QEMU_OPTION_tun_fd:
2219 net_if_type = NET_IF_TUN;
2220 if (nb_tun_fds < MAX_NICS) {
2221 fd = strtol(optarg, (char **)&p, 0);
2223 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2226 tun_fds[nb_tun_fds++] = fd;
2230 case QEMU_OPTION_hdc:
2231 hd_filename[2] = optarg;
2234 case QEMU_OPTION_hdd:
2235 hd_filename[3] = optarg;
2237 case QEMU_OPTION_cdrom:
2238 hd_filename[2] = optarg;
2241 case QEMU_OPTION_boot:
2242 boot_device = optarg[0];
2243 if (boot_device != 'a' && boot_device != 'b' &&
2244 boot_device != 'c' && boot_device != 'd') {
2245 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2249 case QEMU_OPTION_fda:
2250 fd_filename[0] = optarg;
2252 case QEMU_OPTION_fdb:
2253 fd_filename[1] = optarg;
2255 case QEMU_OPTION_no_code_copy:
2256 code_copy_enabled = 0;
2258 case QEMU_OPTION_nics:
2259 nb_nics = atoi(optarg);
2260 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2261 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2265 case QEMU_OPTION_macaddr:
2270 for(i = 0; i < 6; i++) {
2271 macaddr[i] = strtol(p, (char **)&p, 16);
2278 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2286 case QEMU_OPTION_user_net:
2287 net_if_type = NET_IF_USER;
2289 case QEMU_OPTION_dummy_net:
2290 net_if_type = NET_IF_DUMMY;
2292 case QEMU_OPTION_enable_audio:
2299 ram_size = atoi(optarg) * 1024 * 1024;
2302 if (ram_size > PHYS_RAM_MAX_SIZE) {
2303 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2304 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2313 mask = cpu_str_to_log_mask(optarg);
2315 printf("Log items (comma separated):\n");
2316 for(item = cpu_log_items; item->mask != 0; item++) {
2317 printf("%-10s %s\n", item->name, item->help);
2325 pstrcpy(network_script, sizeof(network_script), optarg);
2327 #ifdef CONFIG_GDBSTUB
2332 gdbstub_port = atoi(optarg);
2339 start_emulation = 0;
2341 case QEMU_OPTION_pci:
2344 case QEMU_OPTION_prep:
2347 case QEMU_OPTION_localtime:
2354 linux_boot = (kernel_filename != NULL);
2356 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2357 fd_filename[0] == '\0')
2360 /* boot to cd by default if no hard disk */
2361 if (hd_filename[0] == '\0' && boot_device == 'c') {
2362 if (fd_filename[0] != '\0')
2368 #if !defined(CONFIG_SOFTMMU)
2369 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2371 static uint8_t stdout_buf[4096];
2372 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2375 setvbuf(stdout, NULL, _IOLBF, 0);
2378 /* init host network redirectors */
2379 if (net_if_type == -1) {
2380 net_if_type = NET_IF_TUN;
2381 #if defined(CONFIG_SLIRP)
2382 if (access(network_script, R_OK) < 0) {
2383 net_if_type = NET_IF_USER;
2388 for(i = 0; i < nb_nics; i++) {
2389 NetDriverState *nd = &nd_table[i];
2391 /* init virtual mac address */
2392 nd->macaddr[0] = macaddr[0];
2393 nd->macaddr[1] = macaddr[1];
2394 nd->macaddr[2] = macaddr[2];
2395 nd->macaddr[3] = macaddr[3];
2396 nd->macaddr[4] = macaddr[4];
2397 nd->macaddr[5] = macaddr[5] + i;
2398 switch(net_if_type) {
2399 #if defined(CONFIG_SLIRP)
2404 #if !defined(_WIN32)
2406 if (i < nb_tun_fds) {
2407 net_fd_init(nd, tun_fds[i]);
2409 if (net_tun_init(nd) < 0)
2421 /* init the memory */
2422 phys_ram_size = ram_size + vga_ram_size + bios_size;
2424 #ifdef CONFIG_SOFTMMU
2426 /* mallocs are always aligned on BSD. */
2427 phys_ram_base = malloc(phys_ram_size);
2429 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2431 if (!phys_ram_base) {
2432 fprintf(stderr, "Could not allocate physical memory\n");
2436 /* as we must map the same page at several addresses, we must use
2441 tmpdir = getenv("QEMU_TMPDIR");
2444 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2445 if (mkstemp(phys_ram_file) < 0) {
2446 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2450 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2451 if (phys_ram_fd < 0) {
2452 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2456 ftruncate(phys_ram_fd, phys_ram_size);
2457 unlink(phys_ram_file);
2458 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2460 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2462 if (phys_ram_base == MAP_FAILED) {
2463 fprintf(stderr, "Could not map physical memory\n");
2469 /* we always create the cdrom drive, even if no disk is there */
2471 bs_table[2] = bdrv_new("cdrom");
2472 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2475 /* open the virtual block devices */
2476 for(i = 0; i < MAX_DISKS; i++) {
2477 if (hd_filename[i]) {
2480 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2481 bs_table[i] = bdrv_new(buf);
2483 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2484 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2488 if (i == 0 && cyls != 0)
2489 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2493 /* we always create at least one floppy disk */
2494 fd_table[0] = bdrv_new("fda");
2495 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2497 for(i = 0; i < MAX_FD; i++) {
2498 if (fd_filename[i]) {
2501 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2502 fd_table[i] = bdrv_new(buf);
2503 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2505 if (fd_filename[i] != '\0') {
2506 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2507 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2515 /* init CPU state */
2518 cpu_single_env = env;
2520 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2521 register_savevm("cpu", 0, 1, cpu_save, cpu_load, env);
2522 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2525 cpu_calibrate_ticks();
2529 dumb_display_init(ds);
2532 sdl_display_init(ds);
2534 dumb_display_init(ds);
2538 /* setup cpu signal handlers for MMU / self modifying code handling */
2539 #if !defined(CONFIG_SOFTMMU)
2541 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2544 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2545 stk.ss_sp = signal_stack;
2546 stk.ss_size = SIGNAL_STACK_SIZE;
2549 if (sigaltstack(&stk, NULL) < 0) {
2550 perror("sigaltstack");
2556 struct sigaction act;
2558 sigfillset(&act.sa_mask);
2559 act.sa_flags = SA_SIGINFO;
2560 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2561 act.sa_flags |= SA_ONSTACK;
2563 act.sa_sigaction = host_segv_handler;
2564 sigaction(SIGSEGV, &act, NULL);
2565 sigaction(SIGBUS, &act, NULL);
2566 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2567 sigaction(SIGFPE, &act, NULL);
2574 struct sigaction act;
2575 sigfillset(&act.sa_mask);
2577 act.sa_handler = SIG_IGN;
2578 sigaction(SIGPIPE, &act, NULL);
2583 #if defined(TARGET_I386)
2584 pc_init(ram_size, vga_ram_size, boot_device,
2585 ds, fd_filename, snapshot,
2586 kernel_filename, kernel_cmdline, initrd_filename);
2587 #elif defined(TARGET_PPC)
2588 ppc_init(ram_size, vga_ram_size, boot_device,
2589 ds, fd_filename, snapshot,
2590 kernel_filename, kernel_cmdline, initrd_filename);
2593 /* launched after the device init so that it can display or not a
2597 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2598 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2600 #ifdef CONFIG_GDBSTUB
2602 if (gdbserver_start(gdbstub_port) < 0) {
2603 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2607 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2611 if (start_emulation)