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>
48 #include <linux/if_tun.h>
51 #include <linux/rtc.h>
55 #if defined(CONFIG_SLIRP)
61 #include <sys/timeb.h>
63 #define getopt_long_only getopt_long
64 #define memalign(align, size) malloc(size)
69 #if defined(__linux__)
70 /* SDL use the pthreads and they modify sigaction. We don't
72 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
73 extern void __libc_sigaction();
74 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
76 extern void __sigaction();
77 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
79 #endif /* __linux__ */
80 #endif /* CONFIG_SDL */
88 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
90 //#define DEBUG_UNUSED_IOPORT
91 //#define DEBUG_IOPORT
93 #if !defined(CONFIG_SOFTMMU)
94 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
96 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
100 #define DEFAULT_RAM_SIZE 144
102 #define DEFAULT_RAM_SIZE 128
105 #define GUI_REFRESH_INTERVAL 30
107 /* XXX: use a two level table to limit memory usage */
108 #define MAX_IOPORTS 65536
110 const char *bios_dir = CONFIG_QEMU_SHAREDIR;
111 char phys_ram_file[1024];
112 CPUState *global_env;
113 CPUState *cpu_single_env;
114 void *ioport_opaque[MAX_IOPORTS];
115 IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
116 IOPortWriteFunc *ioport_write_table[3][MAX_IOPORTS];
117 BlockDriverState *bs_table[MAX_DISKS], *fd_table[MAX_FD];
120 static DisplayState display_state;
122 int64_t ticks_per_sec;
123 int boot_device = 'c';
125 static char network_script[1024];
126 int pit_min_timer_count = 0;
128 NetDriverState nd_table[MAX_NICS];
129 SerialState *serial_console;
130 QEMUTimer *gui_timer;
132 int audio_enabled = 0;
134 int prep_enabled = 0;
136 int cirrus_vga_enabled = 1;
137 int graphic_width = 800;
138 int graphic_height = 600;
139 int graphic_depth = 15;
141 /***********************************************************/
142 /* x86 ISA bus support */
144 target_phys_addr_t isa_mem_base = 0;
146 uint32_t default_ioport_readb(void *opaque, uint32_t address)
148 #ifdef DEBUG_UNUSED_IOPORT
149 fprintf(stderr, "inb: port=0x%04x\n", address);
154 void default_ioport_writeb(void *opaque, uint32_t address, uint32_t data)
156 #ifdef DEBUG_UNUSED_IOPORT
157 fprintf(stderr, "outb: port=0x%04x data=0x%02x\n", address, data);
161 /* default is to make two byte accesses */
162 uint32_t default_ioport_readw(void *opaque, uint32_t address)
165 data = ioport_read_table[0][address](ioport_opaque[address], address);
166 address = (address + 1) & (MAX_IOPORTS - 1);
167 data |= ioport_read_table[0][address](ioport_opaque[address], address) << 8;
171 void default_ioport_writew(void *opaque, uint32_t address, uint32_t data)
173 ioport_write_table[0][address](ioport_opaque[address], address, data & 0xff);
174 address = (address + 1) & (MAX_IOPORTS - 1);
175 ioport_write_table[0][address](ioport_opaque[address], address, (data >> 8) & 0xff);
178 uint32_t default_ioport_readl(void *opaque, uint32_t address)
180 #ifdef DEBUG_UNUSED_IOPORT
181 fprintf(stderr, "inl: port=0x%04x\n", address);
186 void default_ioport_writel(void *opaque, uint32_t address, uint32_t data)
188 #ifdef DEBUG_UNUSED_IOPORT
189 fprintf(stderr, "outl: port=0x%04x data=0x%02x\n", address, data);
193 void init_ioports(void)
197 for(i = 0; i < MAX_IOPORTS; i++) {
198 ioport_read_table[0][i] = default_ioport_readb;
199 ioport_write_table[0][i] = default_ioport_writeb;
200 ioport_read_table[1][i] = default_ioport_readw;
201 ioport_write_table[1][i] = default_ioport_writew;
202 ioport_read_table[2][i] = default_ioport_readl;
203 ioport_write_table[2][i] = default_ioport_writel;
207 /* size is the word size in byte */
208 int register_ioport_read(int start, int length, int size,
209 IOPortReadFunc *func, void *opaque)
215 } else if (size == 2) {
217 } else if (size == 4) {
220 hw_error("register_ioport_read: invalid size");
223 for(i = start; i < start + length; i += size) {
224 ioport_read_table[bsize][i] = func;
225 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
226 hw_error("register_ioport_read: invalid opaque");
227 ioport_opaque[i] = opaque;
232 /* size is the word size in byte */
233 int register_ioport_write(int start, int length, int size,
234 IOPortWriteFunc *func, void *opaque)
240 } else if (size == 2) {
242 } else if (size == 4) {
245 hw_error("register_ioport_write: invalid size");
248 for(i = start; i < start + length; i += size) {
249 ioport_write_table[bsize][i] = func;
250 if (ioport_opaque[i] != NULL && ioport_opaque[i] != opaque)
251 hw_error("register_ioport_read: invalid opaque");
252 ioport_opaque[i] = opaque;
257 void isa_unassign_ioport(int start, int length)
261 for(i = start; i < start + length; i++) {
262 ioport_read_table[0][i] = default_ioport_readb;
263 ioport_read_table[1][i] = default_ioport_readw;
264 ioport_read_table[2][i] = default_ioport_readl;
266 ioport_write_table[0][i] = default_ioport_writeb;
267 ioport_write_table[1][i] = default_ioport_writew;
268 ioport_write_table[2][i] = default_ioport_writel;
272 void pstrcpy(char *buf, int buf_size, const char *str)
282 if (c == 0 || q >= buf + buf_size - 1)
289 /* strcat and truncate. */
290 char *pstrcat(char *buf, int buf_size, const char *s)
295 pstrcpy(buf + len, buf_size - len, s);
299 /* return the size or -1 if error */
300 int get_image_size(const char *filename)
303 fd = open(filename, O_RDONLY | O_BINARY);
306 size = lseek(fd, 0, SEEK_END);
311 /* return the size or -1 if error */
312 int load_image(const char *filename, uint8_t *addr)
315 fd = open(filename, O_RDONLY | O_BINARY);
318 size = lseek(fd, 0, SEEK_END);
319 lseek(fd, 0, SEEK_SET);
320 if (read(fd, addr, size) != size) {
328 void cpu_outb(CPUState *env, int addr, int val)
331 if (loglevel & CPU_LOG_IOPORT)
332 fprintf(logfile, "outb: %04x %02x\n", addr, val);
334 ioport_write_table[0][addr](ioport_opaque[addr], addr, val);
337 void cpu_outw(CPUState *env, int addr, int val)
340 if (loglevel & CPU_LOG_IOPORT)
341 fprintf(logfile, "outw: %04x %04x\n", addr, val);
343 ioport_write_table[1][addr](ioport_opaque[addr], addr, val);
346 void cpu_outl(CPUState *env, int addr, int val)
349 if (loglevel & CPU_LOG_IOPORT)
350 fprintf(logfile, "outl: %04x %08x\n", addr, val);
352 ioport_write_table[2][addr](ioport_opaque[addr], addr, val);
355 int cpu_inb(CPUState *env, int addr)
358 val = ioport_read_table[0][addr](ioport_opaque[addr], addr);
360 if (loglevel & CPU_LOG_IOPORT)
361 fprintf(logfile, "inb : %04x %02x\n", addr, val);
366 int cpu_inw(CPUState *env, int addr)
369 val = ioport_read_table[1][addr](ioport_opaque[addr], addr);
371 if (loglevel & CPU_LOG_IOPORT)
372 fprintf(logfile, "inw : %04x %04x\n", addr, val);
377 int cpu_inl(CPUState *env, int addr)
380 val = ioport_read_table[2][addr](ioport_opaque[addr], addr);
382 if (loglevel & CPU_LOG_IOPORT)
383 fprintf(logfile, "inl : %04x %08x\n", addr, val);
388 /***********************************************************/
389 void hw_error(const char *fmt, ...)
394 fprintf(stderr, "qemu: hardware error: ");
395 vfprintf(stderr, fmt, ap);
396 fprintf(stderr, "\n");
398 cpu_x86_dump_state(global_env, stderr, X86_DUMP_FPU | X86_DUMP_CCOP);
400 cpu_dump_state(global_env, stderr, 0);
406 /***********************************************************/
409 static QEMUPutKBDEvent *qemu_put_kbd_event;
410 static void *qemu_put_kbd_event_opaque;
411 static QEMUPutMouseEvent *qemu_put_mouse_event;
412 static void *qemu_put_mouse_event_opaque;
414 void qemu_add_kbd_event_handler(QEMUPutKBDEvent *func, void *opaque)
416 qemu_put_kbd_event_opaque = opaque;
417 qemu_put_kbd_event = func;
420 void qemu_add_mouse_event_handler(QEMUPutMouseEvent *func, void *opaque)
422 qemu_put_mouse_event_opaque = opaque;
423 qemu_put_mouse_event = func;
426 void kbd_put_keycode(int keycode)
428 if (qemu_put_kbd_event) {
429 qemu_put_kbd_event(qemu_put_kbd_event_opaque, keycode);
433 void kbd_mouse_event(int dx, int dy, int dz, int buttons_state)
435 if (qemu_put_mouse_event) {
436 qemu_put_mouse_event(qemu_put_mouse_event_opaque,
437 dx, dy, dz, buttons_state);
441 /***********************************************************/
444 #if defined(__powerpc__)
446 static inline uint32_t get_tbl(void)
449 asm volatile("mftb %0" : "=r" (tbl));
453 static inline uint32_t get_tbu(void)
456 asm volatile("mftbu %0" : "=r" (tbl));
460 int64_t cpu_get_real_ticks(void)
463 /* NOTE: we test if wrapping has occurred */
469 return ((int64_t)h << 32) | l;
472 #elif defined(__i386__)
474 int64_t cpu_get_real_ticks(void)
477 asm volatile ("rdtsc" : "=A" (val));
481 #elif defined(__x86_64__)
483 int64_t cpu_get_real_ticks(void)
487 asm volatile("rdtsc" : "=a" (low), "=d" (high));
495 #error unsupported CPU
498 static int64_t cpu_ticks_offset;
499 static int cpu_ticks_enabled;
501 static inline int64_t cpu_get_ticks(void)
503 if (!cpu_ticks_enabled) {
504 return cpu_ticks_offset;
506 return cpu_get_real_ticks() + cpu_ticks_offset;
510 /* enable cpu_get_ticks() */
511 void cpu_enable_ticks(void)
513 if (!cpu_ticks_enabled) {
514 cpu_ticks_offset -= cpu_get_real_ticks();
515 cpu_ticks_enabled = 1;
519 /* disable cpu_get_ticks() : the clock is stopped. You must not call
520 cpu_get_ticks() after that. */
521 void cpu_disable_ticks(void)
523 if (cpu_ticks_enabled) {
524 cpu_ticks_offset = cpu_get_ticks();
525 cpu_ticks_enabled = 0;
529 static int64_t get_clock(void)
534 return ((int64_t)tb.time * 1000 + (int64_t)tb.millitm) * 1000;
537 gettimeofday(&tv, NULL);
538 return tv.tv_sec * 1000000LL + tv.tv_usec;
542 void cpu_calibrate_ticks(void)
547 ticks = cpu_get_real_ticks();
553 usec = get_clock() - usec;
554 ticks = cpu_get_real_ticks() - ticks;
555 ticks_per_sec = (ticks * 1000000LL + (usec >> 1)) / usec;
558 /* compute with 96 bit intermediate result: (a*b)/c */
559 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c)
564 #ifdef WORDS_BIGENDIAN
574 rl = (uint64_t)u.l.low * (uint64_t)b;
575 rh = (uint64_t)u.l.high * (uint64_t)b;
578 res.l.low = (((rh % c) << 32) + (rl & 0xffffffff)) / c;
582 #define QEMU_TIMER_REALTIME 0
583 #define QEMU_TIMER_VIRTUAL 1
587 /* XXX: add frequency */
595 struct QEMUTimer *next;
601 static QEMUTimer *active_timers[2];
603 static MMRESULT timerID;
605 /* frequency of the times() clock tick */
606 static int timer_freq;
609 QEMUClock *qemu_new_clock(int type)
612 clock = qemu_mallocz(sizeof(QEMUClock));
619 QEMUTimer *qemu_new_timer(QEMUClock *clock, QEMUTimerCB *cb, void *opaque)
623 ts = qemu_mallocz(sizeof(QEMUTimer));
630 void qemu_free_timer(QEMUTimer *ts)
635 /* stop a timer, but do not dealloc it */
636 void qemu_del_timer(QEMUTimer *ts)
640 /* NOTE: this code must be signal safe because
641 qemu_timer_expired() can be called from a signal. */
642 pt = &active_timers[ts->clock->type];
655 /* modify the current timer so that it will be fired when current_time
656 >= expire_time. The corresponding callback will be called. */
657 void qemu_mod_timer(QEMUTimer *ts, int64_t expire_time)
663 /* add the timer in the sorted list */
664 /* NOTE: this code must be signal safe because
665 qemu_timer_expired() can be called from a signal. */
666 pt = &active_timers[ts->clock->type];
671 if (t->expire_time > expire_time)
675 ts->expire_time = expire_time;
680 int qemu_timer_pending(QEMUTimer *ts)
683 for(t = active_timers[ts->clock->type]; t != NULL; t = t->next) {
690 static inline int qemu_timer_expired(QEMUTimer *timer_head, int64_t current_time)
694 return (timer_head->expire_time <= current_time);
697 static void qemu_run_timers(QEMUTimer **ptimer_head, int64_t current_time)
703 if (ts->expire_time > current_time)
705 /* remove timer from the list before calling the callback */
706 *ptimer_head = ts->next;
709 /* run the callback (the timer list can be modified) */
714 int64_t qemu_get_clock(QEMUClock *clock)
716 switch(clock->type) {
717 case QEMU_TIMER_REALTIME:
719 return GetTickCount();
724 /* Note that using gettimeofday() is not a good solution
725 for timers because its value change when the date is
727 if (timer_freq == 100) {
728 return times(&tp) * 10;
730 return ((int64_t)times(&tp) * 1000) / timer_freq;
735 case QEMU_TIMER_VIRTUAL:
736 return cpu_get_ticks();
741 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
743 uint64_t expire_time;
745 if (qemu_timer_pending(ts)) {
746 expire_time = ts->expire_time;
750 qemu_put_be64(f, expire_time);
753 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
755 uint64_t expire_time;
757 expire_time = qemu_get_be64(f);
758 if (expire_time != -1) {
759 qemu_mod_timer(ts, expire_time);
765 static void timer_save(QEMUFile *f, void *opaque)
767 if (cpu_ticks_enabled) {
768 hw_error("cannot save state if virtual timers are running");
770 qemu_put_be64s(f, &cpu_ticks_offset);
771 qemu_put_be64s(f, &ticks_per_sec);
774 static int timer_load(QEMUFile *f, void *opaque, int version_id)
778 if (cpu_ticks_enabled) {
781 qemu_get_be64s(f, &cpu_ticks_offset);
782 qemu_get_be64s(f, &ticks_per_sec);
787 void CALLBACK host_alarm_handler(UINT uTimerID, UINT uMsg,
788 DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
790 static void host_alarm_handler(int host_signum)
794 #define DISP_FREQ 1000
796 static int64_t delta_min = INT64_MAX;
797 static int64_t delta_max, delta_cum, last_clock, delta, ti;
799 ti = qemu_get_clock(vm_clock);
800 if (last_clock != 0) {
801 delta = ti - last_clock;
802 if (delta < delta_min)
804 if (delta > delta_max)
807 if (++count == DISP_FREQ) {
808 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
809 muldiv64(delta_min, 1000000, ticks_per_sec),
810 muldiv64(delta_max, 1000000, ticks_per_sec),
811 muldiv64(delta_cum, 1000000 / DISP_FREQ, ticks_per_sec),
812 (double)ticks_per_sec / ((double)delta_cum / DISP_FREQ));
814 delta_min = INT64_MAX;
822 if (qemu_timer_expired(active_timers[QEMU_TIMER_VIRTUAL],
823 qemu_get_clock(vm_clock)) ||
824 qemu_timer_expired(active_timers[QEMU_TIMER_REALTIME],
825 qemu_get_clock(rt_clock))) {
826 /* stop the cpu because a timer occured */
827 cpu_interrupt(global_env, CPU_INTERRUPT_EXIT);
833 #if defined(__linux__)
835 #define RTC_FREQ 1024
839 static int start_rtc_timer(void)
841 rtc_fd = open("/dev/rtc", O_RDONLY);
844 if (ioctl(rtc_fd, RTC_IRQP_SET, RTC_FREQ) < 0) {
845 fprintf(stderr, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
846 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
847 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
850 if (ioctl(rtc_fd, RTC_PIE_ON, 0) < 0) {
855 pit_min_timer_count = PIT_FREQ / RTC_FREQ;
861 static int start_rtc_timer(void)
866 #endif /* !defined(__linux__) */
868 #endif /* !defined(_WIN32) */
870 static void init_timers(void)
872 rt_clock = qemu_new_clock(QEMU_TIMER_REALTIME);
873 vm_clock = qemu_new_clock(QEMU_TIMER_VIRTUAL);
878 timerID = timeSetEvent(10, // interval (ms)
880 host_alarm_handler, // function
881 (DWORD)&count, // user parameter
882 TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
884 perror("failed timer alarm");
888 pit_min_timer_count = ((uint64_t)10000 * PIT_FREQ) / 1000000;
891 struct sigaction act;
892 struct itimerval itv;
894 /* get times() syscall frequency */
895 timer_freq = sysconf(_SC_CLK_TCK);
898 sigfillset(&act.sa_mask);
900 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
901 act.sa_flags |= SA_ONSTACK;
903 act.sa_handler = host_alarm_handler;
904 sigaction(SIGALRM, &act, NULL);
906 itv.it_interval.tv_sec = 0;
907 itv.it_interval.tv_usec = 1000;
908 itv.it_value.tv_sec = 0;
909 itv.it_value.tv_usec = 10 * 1000;
910 setitimer(ITIMER_REAL, &itv, NULL);
911 /* we probe the tick duration of the kernel to inform the user if
912 the emulated kernel requested a too high timer frequency */
913 getitimer(ITIMER_REAL, &itv);
915 #if defined(__linux__)
916 if (itv.it_interval.tv_usec > 1000) {
917 /* try to use /dev/rtc to have a faster timer */
918 if (start_rtc_timer() < 0)
921 itv.it_interval.tv_sec = 0;
922 itv.it_interval.tv_usec = 0;
923 itv.it_value.tv_sec = 0;
924 itv.it_value.tv_usec = 0;
925 setitimer(ITIMER_REAL, &itv, NULL);
928 sigaction(SIGIO, &act, NULL);
929 fcntl(rtc_fd, F_SETFL, O_ASYNC);
930 fcntl(rtc_fd, F_SETOWN, getpid());
932 #endif /* defined(__linux__) */
935 pit_min_timer_count = ((uint64_t)itv.it_interval.tv_usec *
942 void quit_timers(void)
945 timeKillEvent(timerID);
949 /***********************************************************/
954 int serial_open_device(void)
961 int serial_open_device(void)
963 if (serial_console == NULL && nographic) {
964 /* use console for serial port */
968 char slave_name[1024];
969 int master_fd, slave_fd;
972 if (openpty(&master_fd, &slave_fd, slave_name, NULL, NULL) < 0) {
973 fprintf(stderr, "warning: could not create pseudo terminal for serial port\n");
976 fprintf(stderr, "Serial port redirected to %s\n", slave_name);
986 /***********************************************************/
987 /* Linux network device redirectors */
989 void hex_dump(FILE *f, const uint8_t *buf, int size)
993 for(i=0;i<size;i+=16) {
997 fprintf(f, "%08x ", i);
1000 fprintf(f, " %02x", buf[i+j]);
1005 for(j=0;j<len;j++) {
1007 if (c < ' ' || c > '~')
1009 fprintf(f, "%c", c);
1015 void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1017 nd->send_packet(nd, buf, size);
1020 void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
1021 IOReadHandler *fd_read, void *opaque)
1023 nd->add_read_packet(nd, fd_can_read, fd_read, opaque);
1026 /* dummy network adapter */
1028 static void dummy_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1032 static void dummy_add_read_packet(NetDriverState *nd,
1033 IOCanRWHandler *fd_can_read,
1034 IOReadHandler *fd_read, void *opaque)
1038 static int net_dummy_init(NetDriverState *nd)
1040 nd->send_packet = dummy_send_packet;
1041 nd->add_read_packet = dummy_add_read_packet;
1042 pstrcpy(nd->ifname, sizeof(nd->ifname), "dummy");
1046 #if defined(CONFIG_SLIRP)
1048 /* slirp network adapter */
1050 static void *slirp_fd_opaque;
1051 static IOCanRWHandler *slirp_fd_can_read;
1052 static IOReadHandler *slirp_fd_read;
1053 static int slirp_inited;
1055 int slirp_can_output(void)
1057 return slirp_fd_can_read(slirp_fd_opaque);
1060 void slirp_output(const uint8_t *pkt, int pkt_len)
1063 printf("output:\n");
1064 hex_dump(stdout, pkt, pkt_len);
1066 slirp_fd_read(slirp_fd_opaque, pkt, pkt_len);
1069 static void slirp_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1073 hex_dump(stdout, buf, size);
1075 slirp_input(buf, size);
1078 static void slirp_add_read_packet(NetDriverState *nd,
1079 IOCanRWHandler *fd_can_read,
1080 IOReadHandler *fd_read, void *opaque)
1082 slirp_fd_opaque = opaque;
1083 slirp_fd_can_read = fd_can_read;
1084 slirp_fd_read = fd_read;
1087 static int net_slirp_init(NetDriverState *nd)
1089 if (!slirp_inited) {
1093 nd->send_packet = slirp_send_packet;
1094 nd->add_read_packet = slirp_add_read_packet;
1095 pstrcpy(nd->ifname, sizeof(nd->ifname), "slirp");
1099 #endif /* CONFIG_SLIRP */
1101 #if !defined(_WIN32)
1103 static int tun_open(char *ifname, int ifname_size)
1109 fd = open("/dev/tap", O_RDWR);
1111 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1116 dev = devname(s.st_rdev, S_IFCHR);
1117 pstrcpy(ifname, ifname_size, dev);
1119 fcntl(fd, F_SETFL, O_NONBLOCK);
1123 static int tun_open(char *ifname, int ifname_size)
1128 fd = open("/dev/net/tun", O_RDWR);
1130 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1133 memset(&ifr, 0, sizeof(ifr));
1134 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1135 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tun%d");
1136 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1138 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1142 printf("Connected to host network interface: %s\n", ifr.ifr_name);
1143 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1144 fcntl(fd, F_SETFL, O_NONBLOCK);
1149 static void tun_send_packet(NetDriverState *nd, const uint8_t *buf, int size)
1151 write(nd->fd, buf, size);
1154 static void tun_add_read_packet(NetDriverState *nd,
1155 IOCanRWHandler *fd_can_read,
1156 IOReadHandler *fd_read, void *opaque)
1158 qemu_add_fd_read_handler(nd->fd, fd_can_read, fd_read, opaque);
1161 static int net_tun_init(NetDriverState *nd)
1167 nd->fd = tun_open(nd->ifname, sizeof(nd->ifname));
1171 /* try to launch network init script */
1176 *parg++ = network_script;
1177 *parg++ = nd->ifname;
1179 execv(network_script, args);
1182 while (waitpid(pid, &status, 0) != pid);
1183 if (!WIFEXITED(status) ||
1184 WEXITSTATUS(status) != 0) {
1185 fprintf(stderr, "%s: could not launch network script\n",
1189 nd->send_packet = tun_send_packet;
1190 nd->add_read_packet = tun_add_read_packet;
1194 static int net_fd_init(NetDriverState *nd, int fd)
1197 nd->send_packet = tun_send_packet;
1198 nd->add_read_packet = tun_add_read_packet;
1199 pstrcpy(nd->ifname, sizeof(nd->ifname), "tunfd");
1203 #endif /* !_WIN32 */
1205 /***********************************************************/
1210 static void term_exit(void)
1214 static void term_init(void)
1220 /* init terminal so that we can grab keys */
1221 static struct termios oldtty;
1222 static int old_fd0_flags;
1224 static void term_exit(void)
1226 tcsetattr (0, TCSANOW, &oldtty);
1227 fcntl(0, F_SETFL, old_fd0_flags);
1230 static void term_init(void)
1234 tcgetattr (0, &tty);
1236 old_fd0_flags = fcntl(0, F_GETFL);
1238 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
1239 |INLCR|IGNCR|ICRNL|IXON);
1240 tty.c_oflag |= OPOST;
1241 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
1242 /* if graphical mode, we allow Ctrl-C handling */
1244 tty.c_lflag &= ~ISIG;
1245 tty.c_cflag &= ~(CSIZE|PARENB);
1248 tty.c_cc[VTIME] = 0;
1250 tcsetattr (0, TCSANOW, &tty);
1254 fcntl(0, F_SETFL, O_NONBLOCK);
1259 static void dumb_update(DisplayState *ds, int x, int y, int w, int h)
1263 static void dumb_resize(DisplayState *ds, int w, int h)
1267 static void dumb_refresh(DisplayState *ds)
1269 vga_update_display();
1272 void dumb_display_init(DisplayState *ds)
1277 ds->dpy_update = dumb_update;
1278 ds->dpy_resize = dumb_resize;
1279 ds->dpy_refresh = dumb_refresh;
1282 #if !defined(CONFIG_SOFTMMU)
1283 /***********************************************************/
1284 /* cpu signal handler */
1285 static void host_segv_handler(int host_signum, siginfo_t *info,
1288 if (cpu_signal_handler(host_signum, info, puc))
1295 /***********************************************************/
1298 #define MAX_IO_HANDLERS 64
1300 typedef struct IOHandlerRecord {
1302 IOCanRWHandler *fd_can_read;
1303 IOReadHandler *fd_read;
1305 /* temporary data */
1308 struct IOHandlerRecord *next;
1311 static IOHandlerRecord *first_io_handler;
1313 int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
1314 IOReadHandler *fd_read, void *opaque)
1316 IOHandlerRecord *ioh;
1318 ioh = qemu_mallocz(sizeof(IOHandlerRecord));
1322 ioh->fd_can_read = fd_can_read;
1323 ioh->fd_read = fd_read;
1324 ioh->opaque = opaque;
1325 ioh->next = first_io_handler;
1326 first_io_handler = ioh;
1330 void qemu_del_fd_read_handler(int fd)
1332 IOHandlerRecord **pioh, *ioh;
1334 pioh = &first_io_handler;
1339 if (ioh->fd == fd) {
1347 /***********************************************************/
1348 /* savevm/loadvm support */
1350 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
1352 fwrite(buf, 1, size, f);
1355 void qemu_put_byte(QEMUFile *f, int v)
1360 void qemu_put_be16(QEMUFile *f, unsigned int v)
1362 qemu_put_byte(f, v >> 8);
1363 qemu_put_byte(f, v);
1366 void qemu_put_be32(QEMUFile *f, unsigned int v)
1368 qemu_put_byte(f, v >> 24);
1369 qemu_put_byte(f, v >> 16);
1370 qemu_put_byte(f, v >> 8);
1371 qemu_put_byte(f, v);
1374 void qemu_put_be64(QEMUFile *f, uint64_t v)
1376 qemu_put_be32(f, v >> 32);
1377 qemu_put_be32(f, v);
1380 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
1382 return fread(buf, 1, size, f);
1385 int qemu_get_byte(QEMUFile *f)
1395 unsigned int qemu_get_be16(QEMUFile *f)
1398 v = qemu_get_byte(f) << 8;
1399 v |= qemu_get_byte(f);
1403 unsigned int qemu_get_be32(QEMUFile *f)
1406 v = qemu_get_byte(f) << 24;
1407 v |= qemu_get_byte(f) << 16;
1408 v |= qemu_get_byte(f) << 8;
1409 v |= qemu_get_byte(f);
1413 uint64_t qemu_get_be64(QEMUFile *f)
1416 v = (uint64_t)qemu_get_be32(f) << 32;
1417 v |= qemu_get_be32(f);
1421 int64_t qemu_ftell(QEMUFile *f)
1426 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
1428 if (fseek(f, pos, whence) < 0)
1433 typedef struct SaveStateEntry {
1437 SaveStateHandler *save_state;
1438 LoadStateHandler *load_state;
1440 struct SaveStateEntry *next;
1443 static SaveStateEntry *first_se;
1445 int register_savevm(const char *idstr,
1448 SaveStateHandler *save_state,
1449 LoadStateHandler *load_state,
1452 SaveStateEntry *se, **pse;
1454 se = qemu_malloc(sizeof(SaveStateEntry));
1457 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1458 se->instance_id = instance_id;
1459 se->version_id = version_id;
1460 se->save_state = save_state;
1461 se->load_state = load_state;
1462 se->opaque = opaque;
1465 /* add at the end of list */
1467 while (*pse != NULL)
1468 pse = &(*pse)->next;
1473 #define QEMU_VM_FILE_MAGIC 0x5145564d
1474 #define QEMU_VM_FILE_VERSION 0x00000001
1476 int qemu_savevm(const char *filename)
1480 int len, len_pos, cur_pos, saved_vm_running, ret;
1482 saved_vm_running = vm_running;
1485 f = fopen(filename, "wb");
1491 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1492 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1494 for(se = first_se; se != NULL; se = se->next) {
1496 len = strlen(se->idstr);
1497 qemu_put_byte(f, len);
1498 qemu_put_buffer(f, se->idstr, len);
1500 qemu_put_be32(f, se->instance_id);
1501 qemu_put_be32(f, se->version_id);
1503 /* record size: filled later */
1505 qemu_put_be32(f, 0);
1507 se->save_state(f, se->opaque);
1509 /* fill record size */
1511 len = ftell(f) - len_pos - 4;
1512 fseek(f, len_pos, SEEK_SET);
1513 qemu_put_be32(f, len);
1514 fseek(f, cur_pos, SEEK_SET);
1520 if (saved_vm_running)
1525 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1529 for(se = first_se; se != NULL; se = se->next) {
1530 if (!strcmp(se->idstr, idstr) &&
1531 instance_id == se->instance_id)
1537 int qemu_loadvm(const char *filename)
1541 int len, cur_pos, ret, instance_id, record_len, version_id;
1542 int saved_vm_running;
1546 saved_vm_running = vm_running;
1549 f = fopen(filename, "rb");
1555 v = qemu_get_be32(f);
1556 if (v != QEMU_VM_FILE_MAGIC)
1558 v = qemu_get_be32(f);
1559 if (v != QEMU_VM_FILE_VERSION) {
1566 #if defined (DO_TB_FLUSH)
1567 tb_flush(global_env);
1569 len = qemu_get_byte(f);
1572 qemu_get_buffer(f, idstr, len);
1574 instance_id = qemu_get_be32(f);
1575 version_id = qemu_get_be32(f);
1576 record_len = qemu_get_be32(f);
1578 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1579 idstr, instance_id, version_id, record_len);
1582 se = find_se(idstr, instance_id);
1584 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1585 instance_id, idstr);
1587 ret = se->load_state(f, se->opaque, version_id);
1589 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1590 instance_id, idstr);
1593 /* always seek to exact end of record */
1594 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1599 if (saved_vm_running)
1604 /***********************************************************/
1605 /* cpu save/restore */
1607 #if defined(TARGET_I386)
1609 static void cpu_put_seg(QEMUFile *f, SegmentCache *dt)
1611 qemu_put_be32(f, dt->selector);
1612 qemu_put_be32(f, (uint32_t)dt->base);
1613 qemu_put_be32(f, dt->limit);
1614 qemu_put_be32(f, dt->flags);
1617 static void cpu_get_seg(QEMUFile *f, SegmentCache *dt)
1619 dt->selector = qemu_get_be32(f);
1620 dt->base = (uint8_t *)qemu_get_be32(f);
1621 dt->limit = qemu_get_be32(f);
1622 dt->flags = qemu_get_be32(f);
1625 void cpu_save(QEMUFile *f, void *opaque)
1627 CPUState *env = opaque;
1628 uint16_t fptag, fpus, fpuc;
1632 for(i = 0; i < 8; i++)
1633 qemu_put_be32s(f, &env->regs[i]);
1634 qemu_put_be32s(f, &env->eip);
1635 qemu_put_be32s(f, &env->eflags);
1636 qemu_put_be32s(f, &env->eflags);
1637 hflags = env->hflags; /* XXX: suppress most of the redundant hflags */
1638 qemu_put_be32s(f, &hflags);
1642 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
1644 for (i=7; i>=0; i--) {
1646 if (env->fptags[i]) {
1651 qemu_put_be16s(f, &fpuc);
1652 qemu_put_be16s(f, &fpus);
1653 qemu_put_be16s(f, &fptag);
1655 for(i = 0; i < 8; i++) {
1658 cpu_get_fp80(&mant, &exp, env->fpregs[i]);
1659 qemu_put_be64(f, mant);
1660 qemu_put_be16(f, exp);
1663 for(i = 0; i < 6; i++)
1664 cpu_put_seg(f, &env->segs[i]);
1665 cpu_put_seg(f, &env->ldt);
1666 cpu_put_seg(f, &env->tr);
1667 cpu_put_seg(f, &env->gdt);
1668 cpu_put_seg(f, &env->idt);
1670 qemu_put_be32s(f, &env->sysenter_cs);
1671 qemu_put_be32s(f, &env->sysenter_esp);
1672 qemu_put_be32s(f, &env->sysenter_eip);
1674 qemu_put_be32s(f, &env->cr[0]);
1675 qemu_put_be32s(f, &env->cr[2]);
1676 qemu_put_be32s(f, &env->cr[3]);
1677 qemu_put_be32s(f, &env->cr[4]);
1679 for(i = 0; i < 8; i++)
1680 qemu_put_be32s(f, &env->dr[i]);
1683 qemu_put_be32s(f, &env->a20_mask);
1686 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1688 CPUState *env = opaque;
1691 uint16_t fpus, fpuc, fptag;
1693 if (version_id != 2)
1695 for(i = 0; i < 8; i++)
1696 qemu_get_be32s(f, &env->regs[i]);
1697 qemu_get_be32s(f, &env->eip);
1698 qemu_get_be32s(f, &env->eflags);
1699 qemu_get_be32s(f, &env->eflags);
1700 qemu_get_be32s(f, &hflags);
1702 qemu_get_be16s(f, &fpuc);
1703 qemu_get_be16s(f, &fpus);
1704 qemu_get_be16s(f, &fptag);
1706 for(i = 0; i < 8; i++) {
1709 mant = qemu_get_be64(f);
1710 exp = qemu_get_be16(f);
1711 env->fpregs[i] = cpu_set_fp80(mant, exp);
1715 env->fpstt = (fpus >> 11) & 7;
1716 env->fpus = fpus & ~0x3800;
1717 for(i = 0; i < 8; i++) {
1718 env->fptags[i] = ((fptag & 3) == 3);
1722 for(i = 0; i < 6; i++)
1723 cpu_get_seg(f, &env->segs[i]);
1724 cpu_get_seg(f, &env->ldt);
1725 cpu_get_seg(f, &env->tr);
1726 cpu_get_seg(f, &env->gdt);
1727 cpu_get_seg(f, &env->idt);
1729 qemu_get_be32s(f, &env->sysenter_cs);
1730 qemu_get_be32s(f, &env->sysenter_esp);
1731 qemu_get_be32s(f, &env->sysenter_eip);
1733 qemu_get_be32s(f, &env->cr[0]);
1734 qemu_get_be32s(f, &env->cr[2]);
1735 qemu_get_be32s(f, &env->cr[3]);
1736 qemu_get_be32s(f, &env->cr[4]);
1738 for(i = 0; i < 8; i++)
1739 qemu_get_be32s(f, &env->dr[i]);
1742 qemu_get_be32s(f, &env->a20_mask);
1744 /* XXX: compute hflags from scratch, except for CPL and IIF */
1745 env->hflags = hflags;
1750 #elif defined(TARGET_PPC)
1751 void cpu_save(QEMUFile *f, void *opaque)
1755 int cpu_load(QEMUFile *f, void *opaque, int version_id)
1761 #warning No CPU save/restore functions
1765 /***********************************************************/
1766 /* ram save/restore */
1768 /* we just avoid storing empty pages */
1769 static void ram_put_page(QEMUFile *f, const uint8_t *buf, int len)
1774 for(i = 1; i < len; i++) {
1778 qemu_put_byte(f, 1);
1779 qemu_put_byte(f, v);
1782 qemu_put_byte(f, 0);
1783 qemu_put_buffer(f, buf, len);
1786 static int ram_get_page(QEMUFile *f, uint8_t *buf, int len)
1790 v = qemu_get_byte(f);
1793 if (qemu_get_buffer(f, buf, len) != len)
1797 v = qemu_get_byte(f);
1798 memset(buf, v, len);
1806 static void ram_save(QEMUFile *f, void *opaque)
1809 qemu_put_be32(f, phys_ram_size);
1810 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1811 ram_put_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1815 static int ram_load(QEMUFile *f, void *opaque, int version_id)
1819 if (version_id != 1)
1821 if (qemu_get_be32(f) != phys_ram_size)
1823 for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
1824 ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
1831 /***********************************************************/
1832 /* main execution loop */
1834 void gui_update(void *opaque)
1836 display_state.dpy_refresh(&display_state);
1837 qemu_mod_timer(gui_timer, GUI_REFRESH_INTERVAL + qemu_get_clock(rt_clock));
1840 /* XXX: support several handlers */
1841 VMStopHandler *vm_stop_cb;
1842 VMStopHandler *vm_stop_opaque;
1844 int qemu_add_vm_stop_handler(VMStopHandler *cb, void *opaque)
1847 vm_stop_opaque = opaque;
1851 void qemu_del_vm_stop_handler(VMStopHandler *cb, void *opaque)
1864 void vm_stop(int reason)
1867 cpu_disable_ticks();
1871 vm_stop_cb(vm_stop_opaque, reason);
1877 /* reset/shutdown handler */
1879 typedef struct QEMUResetEntry {
1880 QEMUResetHandler *func;
1882 struct QEMUResetEntry *next;
1885 static QEMUResetEntry *first_reset_entry;
1886 static int reset_requested;
1887 static int shutdown_requested;
1889 void qemu_register_reset(QEMUResetHandler *func, void *opaque)
1891 QEMUResetEntry **pre, *re;
1893 pre = &first_reset_entry;
1894 while (*pre != NULL)
1895 pre = &(*pre)->next;
1896 re = qemu_mallocz(sizeof(QEMUResetEntry));
1898 re->opaque = opaque;
1903 void qemu_system_reset(void)
1907 /* reset all devices */
1908 for(re = first_reset_entry; re != NULL; re = re->next) {
1909 re->func(re->opaque);
1913 void qemu_system_reset_request(void)
1915 reset_requested = 1;
1916 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1919 void qemu_system_shutdown_request(void)
1921 shutdown_requested = 1;
1922 cpu_interrupt(cpu_single_env, CPU_INTERRUPT_EXIT);
1925 static void main_cpu_reset(void *opaque)
1928 CPUState *env = opaque;
1936 struct pollfd ufds[MAX_IO_HANDLERS + 1], *pf;
1937 IOHandlerRecord *ioh, *ioh_next;
1942 CPUState *env = global_env;
1946 ret = cpu_exec(env);
1947 if (shutdown_requested) {
1948 ret = EXCP_INTERRUPT;
1951 if (reset_requested) {
1952 reset_requested = 0;
1953 qemu_system_reset();
1954 ret = EXCP_INTERRUPT;
1956 if (ret == EXCP_DEBUG) {
1957 vm_stop(EXCP_DEBUG);
1959 /* if hlt instruction, we wait until the next IRQ */
1960 /* XXX: use timeout computed from timers */
1961 if (ret == EXCP_HLT)
1974 /* poll any events */
1975 /* XXX: separate device handlers from system ones */
1977 for(ioh = first_io_handler; ioh != NULL; ioh = ioh->next) {
1978 if (!ioh->fd_can_read) {
1981 pf->events = POLLIN;
1985 max_size = ioh->fd_can_read(ioh->opaque);
1987 if (max_size > sizeof(buf))
1988 max_size = sizeof(buf);
1990 pf->events = POLLIN;
1997 ioh->max_size = max_size;
2000 ret = poll(ufds, pf - ufds, timeout);
2002 /* XXX: better handling of removal */
2003 for(ioh = first_io_handler; ioh != NULL; ioh = ioh_next) {
2004 ioh_next = ioh->next;
2007 if (pf->revents & POLLIN) {
2008 if (ioh->max_size == 0) {
2009 /* just a read event */
2010 ioh->fd_read(ioh->opaque, NULL, 0);
2012 n = read(ioh->fd, buf, ioh->max_size);
2014 ioh->fd_read(ioh->opaque, buf, n);
2015 } else if (errno != EAGAIN) {
2016 ioh->fd_read(ioh->opaque, NULL, -errno);
2024 #if defined(CONFIG_SLIRP)
2025 /* XXX: merge with poll() */
2027 fd_set rfds, wfds, xfds;
2035 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
2038 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
2040 slirp_select_poll(&rfds, &wfds, &xfds);
2048 qemu_run_timers(&active_timers[QEMU_TIMER_VIRTUAL],
2049 qemu_get_clock(vm_clock));
2051 if (audio_enabled) {
2052 /* XXX: add explicit timer */
2056 /* run dma transfers, if any */
2060 /* real time timers */
2061 qemu_run_timers(&active_timers[QEMU_TIMER_REALTIME],
2062 qemu_get_clock(rt_clock));
2064 cpu_disable_ticks();
2070 printf("QEMU PC emulator version " QEMU_VERSION ", Copyright (c) 2003-2004 Fabrice Bellard\n"
2071 "usage: %s [options] [disk_image]\n"
2073 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2075 "Standard options:\n"
2076 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2077 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2078 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2079 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2080 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
2081 "-snapshot write to temporary files instead of disk image files\n"
2082 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2083 "-nographic disable graphical output and redirect serial I/Os to console\n"
2084 "-enable-audio enable audio support\n"
2085 "-localtime set the real time clock to local time [default=utc]\n"
2087 "-prep Simulate a PREP system (default is PowerMAC)\n"
2088 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2091 "Network options:\n"
2092 "-nics n simulate 'n' network cards [default=1]\n"
2093 "-macaddr addr set the mac address of the first interface\n"
2094 "-n script set tap/tun network init script [default=%s]\n"
2095 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2097 "-user-net use user mode network stack [default if no tap/tun script]\n"
2099 "-dummy-net use dummy network stack\n"
2101 "Linux boot specific:\n"
2102 "-kernel bzImage use 'bzImage' as kernel image\n"
2103 "-append cmdline use 'cmdline' as kernel command line\n"
2104 "-initrd file use 'file' as initial ram disk\n"
2106 "Debug/Expert options:\n"
2107 "-S freeze CPU at startup (use 'c' to start execution)\n"
2108 "-s wait gdb connection to port %d\n"
2109 "-p port change gdb connection port\n"
2110 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2111 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
2112 "-L path set the directory for the BIOS and VGA BIOS\n"
2113 #ifdef USE_CODE_COPY
2114 "-no-code-copy disable code copy acceleration\n"
2117 "-isa simulate an ISA-only system (default is PCI system)\n"
2118 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2119 " (default is CL-GD5446 PCI VGA)\n"
2122 "During emulation, use C-a h to get terminal commands:\n",
2123 #ifdef CONFIG_SOFTMMU
2129 DEFAULT_NETWORK_SCRIPT,
2130 DEFAULT_GDBSTUB_PORT,
2133 #ifndef CONFIG_SOFTMMU
2135 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2136 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2142 #define HAS_ARG 0x0001
2155 QEMU_OPTION_snapshot,
2157 QEMU_OPTION_nographic,
2158 QEMU_OPTION_enable_audio,
2161 QEMU_OPTION_macaddr,
2164 QEMU_OPTION_user_net,
2165 QEMU_OPTION_dummy_net,
2177 QEMU_OPTION_no_code_copy,
2181 QEMU_OPTION_localtime,
2182 QEMU_OPTION_cirrusvga,
2184 QEMU_OPTION_std_vga,
2187 typedef struct QEMUOption {
2193 const QEMUOption qemu_options[] = {
2194 { "h", 0, QEMU_OPTION_h },
2196 { "fda", HAS_ARG, QEMU_OPTION_fda },
2197 { "fdb", HAS_ARG, QEMU_OPTION_fdb },
2198 { "hda", HAS_ARG, QEMU_OPTION_hda },
2199 { "hdb", HAS_ARG, QEMU_OPTION_hdb },
2200 { "hdc", HAS_ARG, QEMU_OPTION_hdc },
2201 { "hdd", HAS_ARG, QEMU_OPTION_hdd },
2202 { "cdrom", HAS_ARG, QEMU_OPTION_cdrom },
2203 { "boot", HAS_ARG, QEMU_OPTION_boot },
2204 { "snapshot", 0, QEMU_OPTION_snapshot },
2205 { "m", HAS_ARG, QEMU_OPTION_m },
2206 { "nographic", 0, QEMU_OPTION_nographic },
2207 { "enable-audio", 0, QEMU_OPTION_enable_audio },
2209 { "nics", HAS_ARG, QEMU_OPTION_nics},
2210 { "macaddr", HAS_ARG, QEMU_OPTION_macaddr},
2211 { "n", HAS_ARG, QEMU_OPTION_n },
2212 { "tun-fd", HAS_ARG, QEMU_OPTION_tun_fd },
2214 { "user-net", 0, QEMU_OPTION_user_net },
2216 { "dummy-net", 0, QEMU_OPTION_dummy_net },
2218 { "kernel", HAS_ARG, QEMU_OPTION_kernel },
2219 { "append", HAS_ARG, QEMU_OPTION_append },
2220 { "initrd", HAS_ARG, QEMU_OPTION_initrd },
2222 { "S", 0, QEMU_OPTION_S },
2223 { "s", 0, QEMU_OPTION_s },
2224 { "p", HAS_ARG, QEMU_OPTION_p },
2225 { "d", HAS_ARG, QEMU_OPTION_d },
2226 { "hdachs", HAS_ARG, QEMU_OPTION_hdachs },
2227 { "L", HAS_ARG, QEMU_OPTION_L },
2228 { "no-code-copy", 0, QEMU_OPTION_no_code_copy },
2230 { "prep", 0, QEMU_OPTION_prep },
2231 { "g", 1, QEMU_OPTION_g },
2233 { "localtime", 0, QEMU_OPTION_localtime },
2234 { "isa", 0, QEMU_OPTION_isa },
2235 { "std-vga", 0, QEMU_OPTION_std_vga },
2237 /* temporary options */
2238 { "pci", 0, QEMU_OPTION_pci },
2239 { "cirrusvga", 0, QEMU_OPTION_cirrusvga },
2243 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2245 /* this stack is only used during signal handling */
2246 #define SIGNAL_STACK_SIZE 32768
2248 static uint8_t *signal_stack;
2252 #define NET_IF_TUN 0
2253 #define NET_IF_USER 1
2254 #define NET_IF_DUMMY 2
2256 int main(int argc, char **argv)
2258 #ifdef CONFIG_GDBSTUB
2259 int use_gdbstub, gdbstub_port;
2262 int snapshot, linux_boot;
2264 const char *initrd_filename;
2265 const char *hd_filename[MAX_DISKS], *fd_filename[MAX_FD];
2266 const char *kernel_filename, *kernel_cmdline;
2267 DisplayState *ds = &display_state;
2268 int cyls, heads, secs;
2269 int start_emulation = 1;
2271 int net_if_type, nb_tun_fds, tun_fds[MAX_NICS];
2273 const char *r, *optarg;
2275 #if !defined(CONFIG_SOFTMMU)
2276 /* we never want that malloc() uses mmap() */
2277 mallopt(M_MMAP_THRESHOLD, 4096 * 1024);
2279 initrd_filename = NULL;
2280 for(i = 0; i < MAX_FD; i++)
2281 fd_filename[i] = NULL;
2282 for(i = 0; i < MAX_DISKS; i++)
2283 hd_filename[i] = NULL;
2284 ram_size = DEFAULT_RAM_SIZE * 1024 * 1024;
2285 vga_ram_size = VGA_RAM_SIZE;
2286 bios_size = BIOS_SIZE;
2287 pstrcpy(network_script, sizeof(network_script), DEFAULT_NETWORK_SCRIPT);
2288 #ifdef CONFIG_GDBSTUB
2290 gdbstub_port = DEFAULT_GDBSTUB_PORT;
2294 kernel_filename = NULL;
2295 kernel_cmdline = "";
2297 cyls = heads = secs = 0;
2302 /* default mac address of the first network interface */
2316 hd_filename[0] = argv[optind++];
2318 const QEMUOption *popt;
2321 popt = qemu_options;
2324 fprintf(stderr, "%s: invalid option -- '%s'\n",
2328 if (!strcmp(popt->name, r + 1))
2332 if (popt->flags & HAS_ARG) {
2333 if (optind >= argc) {
2334 fprintf(stderr, "%s: option '%s' requires an argument\n",
2338 optarg = argv[optind++];
2343 switch(popt->index) {
2344 case QEMU_OPTION_initrd:
2345 initrd_filename = optarg;
2347 case QEMU_OPTION_hda:
2348 hd_filename[0] = optarg;
2350 case QEMU_OPTION_hdb:
2351 hd_filename[1] = optarg;
2353 case QEMU_OPTION_snapshot:
2356 case QEMU_OPTION_hdachs:
2360 cyls = strtol(p, (char **)&p, 0);
2364 heads = strtol(p, (char **)&p, 0);
2368 secs = strtol(p, (char **)&p, 0);
2375 case QEMU_OPTION_nographic:
2378 case QEMU_OPTION_kernel:
2379 kernel_filename = optarg;
2381 case QEMU_OPTION_append:
2382 kernel_cmdline = optarg;
2384 case QEMU_OPTION_tun_fd:
2388 net_if_type = NET_IF_TUN;
2389 if (nb_tun_fds < MAX_NICS) {
2390 fd = strtol(optarg, (char **)&p, 0);
2392 fprintf(stderr, "qemu: invalid fd for network interface %d\n", nb_tun_fds);
2395 tun_fds[nb_tun_fds++] = fd;
2399 case QEMU_OPTION_hdc:
2400 hd_filename[2] = optarg;
2403 case QEMU_OPTION_hdd:
2404 hd_filename[3] = optarg;
2406 case QEMU_OPTION_cdrom:
2407 hd_filename[2] = optarg;
2410 case QEMU_OPTION_boot:
2411 boot_device = optarg[0];
2412 if (boot_device != 'a' && boot_device != 'b' &&
2413 boot_device != 'c' && boot_device != 'd') {
2414 fprintf(stderr, "qemu: invalid boot device '%c'\n", boot_device);
2418 case QEMU_OPTION_fda:
2419 fd_filename[0] = optarg;
2421 case QEMU_OPTION_fdb:
2422 fd_filename[1] = optarg;
2424 case QEMU_OPTION_no_code_copy:
2425 code_copy_enabled = 0;
2427 case QEMU_OPTION_nics:
2428 nb_nics = atoi(optarg);
2429 if (nb_nics < 0 || nb_nics > MAX_NICS) {
2430 fprintf(stderr, "qemu: invalid number of network interfaces\n");
2434 case QEMU_OPTION_macaddr:
2439 for(i = 0; i < 6; i++) {
2440 macaddr[i] = strtol(p, (char **)&p, 16);
2447 fprintf(stderr, "qemu: invalid syntax for ethernet address\n");
2455 case QEMU_OPTION_user_net:
2456 net_if_type = NET_IF_USER;
2458 case QEMU_OPTION_dummy_net:
2459 net_if_type = NET_IF_DUMMY;
2461 case QEMU_OPTION_enable_audio:
2468 ram_size = atoi(optarg) * 1024 * 1024;
2471 if (ram_size > PHYS_RAM_MAX_SIZE) {
2472 fprintf(stderr, "qemu: at most %d MB RAM can be simulated\n",
2473 PHYS_RAM_MAX_SIZE / (1024 * 1024));
2482 mask = cpu_str_to_log_mask(optarg);
2484 printf("Log items (comma separated):\n");
2485 for(item = cpu_log_items; item->mask != 0; item++) {
2486 printf("%-10s %s\n", item->name, item->help);
2494 pstrcpy(network_script, sizeof(network_script), optarg);
2496 #ifdef CONFIG_GDBSTUB
2501 gdbstub_port = atoi(optarg);
2508 start_emulation = 0;
2510 case QEMU_OPTION_pci:
2513 case QEMU_OPTION_isa:
2516 case QEMU_OPTION_prep:
2519 case QEMU_OPTION_localtime:
2522 case QEMU_OPTION_cirrusvga:
2523 cirrus_vga_enabled = 1;
2525 case QEMU_OPTION_std_vga:
2526 cirrus_vga_enabled = 0;
2533 w = strtol(p, (char **)&p, 10);
2536 fprintf(stderr, "qemu: invalid resolution or depth\n");
2542 h = strtol(p, (char **)&p, 10);
2547 depth = strtol(p, (char **)&p, 10);
2548 if (depth != 8 && depth != 15 && depth != 16 &&
2549 depth != 24 && depth != 32)
2551 } else if (*p == '\0') {
2552 depth = graphic_depth;
2559 graphic_depth = depth;
2566 linux_boot = (kernel_filename != NULL);
2568 if (!linux_boot && hd_filename[0] == '\0' && hd_filename[2] == '\0' &&
2569 fd_filename[0] == '\0')
2572 /* boot to cd by default if no hard disk */
2573 if (hd_filename[0] == '\0' && boot_device == 'c') {
2574 if (fd_filename[0] != '\0')
2580 #if !defined(CONFIG_SOFTMMU)
2581 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2583 static uint8_t stdout_buf[4096];
2584 setvbuf(stdout, stdout_buf, _IOLBF, sizeof(stdout_buf));
2587 setvbuf(stdout, NULL, _IOLBF, 0);
2590 /* init host network redirectors */
2591 if (net_if_type == -1) {
2592 net_if_type = NET_IF_TUN;
2593 #if defined(CONFIG_SLIRP)
2594 if (access(network_script, R_OK) < 0) {
2595 net_if_type = NET_IF_USER;
2600 for(i = 0; i < nb_nics; i++) {
2601 NetDriverState *nd = &nd_table[i];
2603 /* init virtual mac address */
2604 nd->macaddr[0] = macaddr[0];
2605 nd->macaddr[1] = macaddr[1];
2606 nd->macaddr[2] = macaddr[2];
2607 nd->macaddr[3] = macaddr[3];
2608 nd->macaddr[4] = macaddr[4];
2609 nd->macaddr[5] = macaddr[5] + i;
2610 switch(net_if_type) {
2611 #if defined(CONFIG_SLIRP)
2616 #if !defined(_WIN32)
2618 if (i < nb_tun_fds) {
2619 net_fd_init(nd, tun_fds[i]);
2621 if (net_tun_init(nd) < 0)
2633 /* init the memory */
2634 phys_ram_size = ram_size + vga_ram_size + bios_size;
2636 #ifdef CONFIG_SOFTMMU
2638 /* mallocs are always aligned on BSD. valloc is better for correctness */
2639 phys_ram_base = valloc(phys_ram_size);
2641 phys_ram_base = memalign(TARGET_PAGE_SIZE, phys_ram_size);
2643 if (!phys_ram_base) {
2644 fprintf(stderr, "Could not allocate physical memory\n");
2648 /* as we must map the same page at several addresses, we must use
2653 tmpdir = getenv("QEMU_TMPDIR");
2656 snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/vlXXXXXX", tmpdir);
2657 if (mkstemp(phys_ram_file) < 0) {
2658 fprintf(stderr, "Could not create temporary memory file '%s'\n",
2662 phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
2663 if (phys_ram_fd < 0) {
2664 fprintf(stderr, "Could not open temporary memory file '%s'\n",
2668 ftruncate(phys_ram_fd, phys_ram_size);
2669 unlink(phys_ram_file);
2670 phys_ram_base = mmap(get_mmap_addr(phys_ram_size),
2672 PROT_WRITE | PROT_READ, MAP_SHARED | MAP_FIXED,
2674 if (phys_ram_base == MAP_FAILED) {
2675 fprintf(stderr, "Could not map physical memory\n");
2681 /* we always create the cdrom drive, even if no disk is there */
2683 bs_table[2] = bdrv_new("cdrom");
2684 bdrv_set_type_hint(bs_table[2], BDRV_TYPE_CDROM);
2687 /* open the virtual block devices */
2688 for(i = 0; i < MAX_DISKS; i++) {
2689 if (hd_filename[i]) {
2692 snprintf(buf, sizeof(buf), "hd%c", i + 'a');
2693 bs_table[i] = bdrv_new(buf);
2695 if (bdrv_open(bs_table[i], hd_filename[i], snapshot) < 0) {
2696 fprintf(stderr, "qemu: could not open hard disk image '%s\n",
2700 if (i == 0 && cyls != 0)
2701 bdrv_set_geometry_hint(bs_table[i], cyls, heads, secs);
2705 /* we always create at least one floppy disk */
2706 fd_table[0] = bdrv_new("fda");
2707 bdrv_set_type_hint(fd_table[0], BDRV_TYPE_FLOPPY);
2709 for(i = 0; i < MAX_FD; i++) {
2710 if (fd_filename[i]) {
2713 snprintf(buf, sizeof(buf), "fd%c", i + 'a');
2714 fd_table[i] = bdrv_new(buf);
2715 bdrv_set_type_hint(fd_table[i], BDRV_TYPE_FLOPPY);
2717 if (fd_filename[i] != '\0') {
2718 if (bdrv_open(fd_table[i], fd_filename[i], snapshot) < 0) {
2719 fprintf(stderr, "qemu: could not open floppy disk image '%s'\n",
2727 /* init CPU state */
2730 cpu_single_env = env;
2732 register_savevm("timer", 0, 1, timer_save, timer_load, env);
2733 register_savevm("cpu", 0, 2, cpu_save, cpu_load, env);
2734 register_savevm("ram", 0, 1, ram_save, ram_load, NULL);
2735 qemu_register_reset(main_cpu_reset, global_env);
2738 cpu_calibrate_ticks();
2742 dumb_display_init(ds);
2745 sdl_display_init(ds);
2747 dumb_display_init(ds);
2751 /* setup cpu signal handlers for MMU / self modifying code handling */
2752 #if !defined(CONFIG_SOFTMMU)
2754 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2757 signal_stack = memalign(16, SIGNAL_STACK_SIZE);
2758 stk.ss_sp = signal_stack;
2759 stk.ss_size = SIGNAL_STACK_SIZE;
2762 if (sigaltstack(&stk, NULL) < 0) {
2763 perror("sigaltstack");
2769 struct sigaction act;
2771 sigfillset(&act.sa_mask);
2772 act.sa_flags = SA_SIGINFO;
2773 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2774 act.sa_flags |= SA_ONSTACK;
2776 act.sa_sigaction = host_segv_handler;
2777 sigaction(SIGSEGV, &act, NULL);
2778 sigaction(SIGBUS, &act, NULL);
2779 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2780 sigaction(SIGFPE, &act, NULL);
2787 struct sigaction act;
2788 sigfillset(&act.sa_mask);
2790 act.sa_handler = SIG_IGN;
2791 sigaction(SIGPIPE, &act, NULL);
2796 #if defined(TARGET_I386)
2797 pc_init(ram_size, vga_ram_size, boot_device,
2798 ds, fd_filename, snapshot,
2799 kernel_filename, kernel_cmdline, initrd_filename);
2800 #elif defined(TARGET_PPC)
2801 ppc_init(ram_size, vga_ram_size, boot_device,
2802 ds, fd_filename, snapshot,
2803 kernel_filename, kernel_cmdline, initrd_filename);
2806 /* launched after the device init so that it can display or not a
2810 gui_timer = qemu_new_timer(rt_clock, gui_update, NULL);
2811 qemu_mod_timer(gui_timer, qemu_get_clock(rt_clock));
2813 #ifdef CONFIG_GDBSTUB
2815 if (gdbserver_start(gdbstub_port) < 0) {
2816 fprintf(stderr, "Could not open gdbserver socket on port %d\n",
2820 printf("Waiting gdb connection on port %d\n", gdbstub_port);
2824 if (start_emulation)