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)
71 #if defined(__linux__)
72 /* SDL use the pthreads and they modify sigaction. We don't
74 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
75 extern void __libc_sigaction();
76 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
78 extern void __sigaction();
79 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
81 #endif /* __linux__ */
82 #endif /* CONFIG_SDL */
90 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
92 //#define DEBUG_UNUSED_IOPORT
93 //#define DEBUG_IOPORT
95 #if !defined(CONFIG_SOFTMMU)
96 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
98 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
102 #define DEFAULT_RAM_SIZE 144
104 #define DEFAULT_RAM_SIZE 128
107 #define GUI_REFRESH_INTERVAL 30
109 /* XXX: use a two level table to limit memory usage */
110 #define MAX_IOPORTS 65536
112 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
113 char phys_ram_file
[1024];
114 CPUState
*global_env
;
115 CPUState
*cpu_single_env
;
116 void *ioport_opaque
[MAX_IOPORTS
];
117 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
118 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
119 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
122 static DisplayState display_state
;
124 int64_t ticks_per_sec
;
125 int boot_device
= 'c';
127 static char network_script
[1024];
128 int pit_min_timer_count
= 0;
130 NetDriverState nd_table
[MAX_NICS
];
131 SerialState
*serial_console
;
132 QEMUTimer
*gui_timer
;
134 int audio_enabled
= 0;
136 int prep_enabled
= 0;
138 int cirrus_vga_enabled
= 1;
139 int graphic_width
= 800;
140 int graphic_height
= 600;
141 int graphic_depth
= 15;
143 /***********************************************************/
144 /* x86 ISA bus support */
146 target_phys_addr_t isa_mem_base
= 0;
148 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr
, "inb: port=0x%04x\n", address
);
156 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
158 #ifdef DEBUG_UNUSED_IOPORT
159 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
163 /* default is to make two byte accesses */
164 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
167 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
168 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
169 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
173 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
175 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
176 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
177 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
180 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
182 #ifdef DEBUG_UNUSED_IOPORT
183 fprintf(stderr
, "inl: port=0x%04x\n", address
);
188 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
190 #ifdef DEBUG_UNUSED_IOPORT
191 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
195 void init_ioports(void)
199 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
200 ioport_read_table
[0][i
] = default_ioport_readb
;
201 ioport_write_table
[0][i
] = default_ioport_writeb
;
202 ioport_read_table
[1][i
] = default_ioport_readw
;
203 ioport_write_table
[1][i
] = default_ioport_writew
;
204 ioport_read_table
[2][i
] = default_ioport_readl
;
205 ioport_write_table
[2][i
] = default_ioport_writel
;
209 /* size is the word size in byte */
210 int register_ioport_read(int start
, int length
, int size
,
211 IOPortReadFunc
*func
, void *opaque
)
217 } else if (size
== 2) {
219 } else if (size
== 4) {
222 hw_error("register_ioport_read: invalid size");
225 for(i
= start
; i
< start
+ length
; i
+= size
) {
226 ioport_read_table
[bsize
][i
] = func
;
227 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
228 hw_error("register_ioport_read: invalid opaque");
229 ioport_opaque
[i
] = opaque
;
234 /* size is the word size in byte */
235 int register_ioport_write(int start
, int length
, int size
,
236 IOPortWriteFunc
*func
, void *opaque
)
242 } else if (size
== 2) {
244 } else if (size
== 4) {
247 hw_error("register_ioport_write: invalid size");
250 for(i
= start
; i
< start
+ length
; i
+= size
) {
251 ioport_write_table
[bsize
][i
] = func
;
252 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
253 hw_error("register_ioport_read: invalid opaque");
254 ioport_opaque
[i
] = opaque
;
259 void isa_unassign_ioport(int start
, int length
)
263 for(i
= start
; i
< start
+ length
; i
++) {
264 ioport_read_table
[0][i
] = default_ioport_readb
;
265 ioport_read_table
[1][i
] = default_ioport_readw
;
266 ioport_read_table
[2][i
] = default_ioport_readl
;
268 ioport_write_table
[0][i
] = default_ioport_writeb
;
269 ioport_write_table
[1][i
] = default_ioport_writew
;
270 ioport_write_table
[2][i
] = default_ioport_writel
;
274 void pstrcpy(char *buf
, int buf_size
, const char *str
)
284 if (c
== 0 || q
>= buf
+ buf_size
- 1)
291 /* strcat and truncate. */
292 char *pstrcat(char *buf
, int buf_size
, const char *s
)
297 pstrcpy(buf
+ len
, buf_size
- len
, s
);
301 /* return the size or -1 if error */
302 int get_image_size(const char *filename
)
305 fd
= open(filename
, O_RDONLY
| O_BINARY
);
308 size
= lseek(fd
, 0, SEEK_END
);
313 /* return the size or -1 if error */
314 int load_image(const char *filename
, uint8_t *addr
)
317 fd
= open(filename
, O_RDONLY
| O_BINARY
);
320 size
= lseek(fd
, 0, SEEK_END
);
321 lseek(fd
, 0, SEEK_SET
);
322 if (read(fd
, addr
, size
) != size
) {
330 void cpu_outb(CPUState
*env
, int addr
, int val
)
333 if (loglevel
& CPU_LOG_IOPORT
)
334 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
336 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
339 void cpu_outw(CPUState
*env
, int addr
, int val
)
342 if (loglevel
& CPU_LOG_IOPORT
)
343 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
345 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
348 void cpu_outl(CPUState
*env
, int addr
, int val
)
351 if (loglevel
& CPU_LOG_IOPORT
)
352 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
354 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
357 int cpu_inb(CPUState
*env
, int addr
)
360 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
362 if (loglevel
& CPU_LOG_IOPORT
)
363 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
368 int cpu_inw(CPUState
*env
, int addr
)
371 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
373 if (loglevel
& CPU_LOG_IOPORT
)
374 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
379 int cpu_inl(CPUState
*env
, int addr
)
382 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
384 if (loglevel
& CPU_LOG_IOPORT
)
385 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
390 /***********************************************************/
391 void hw_error(const char *fmt
, ...)
396 fprintf(stderr
, "qemu: hardware error: ");
397 vfprintf(stderr
, fmt
, ap
);
398 fprintf(stderr
, "\n");
400 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
402 cpu_dump_state(global_env
, stderr
, 0);
408 /***********************************************************/
411 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
412 static void *qemu_put_kbd_event_opaque
;
413 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
414 static void *qemu_put_mouse_event_opaque
;
416 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
418 qemu_put_kbd_event_opaque
= opaque
;
419 qemu_put_kbd_event
= func
;
422 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
)
424 qemu_put_mouse_event_opaque
= opaque
;
425 qemu_put_mouse_event
= func
;
428 void kbd_put_keycode(int keycode
)
430 if (qemu_put_kbd_event
) {
431 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
435 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
437 if (qemu_put_mouse_event
) {
438 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
439 dx
, dy
, dz
, buttons_state
);
443 /***********************************************************/
446 #if defined(__powerpc__)
448 static inline uint32_t get_tbl(void)
451 asm volatile("mftb %0" : "=r" (tbl
));
455 static inline uint32_t get_tbu(void)
458 asm volatile("mftbu %0" : "=r" (tbl
));
462 int64_t cpu_get_real_ticks(void)
465 /* NOTE: we test if wrapping has occurred */
471 return ((int64_t)h
<< 32) | l
;
474 #elif defined(__i386__)
476 int64_t cpu_get_real_ticks(void)
479 asm volatile ("rdtsc" : "=A" (val
));
483 #elif defined(__x86_64__)
485 int64_t cpu_get_real_ticks(void)
489 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
497 #error unsupported CPU
500 static int64_t cpu_ticks_offset
;
501 static int cpu_ticks_enabled
;
503 static inline int64_t cpu_get_ticks(void)
505 if (!cpu_ticks_enabled
) {
506 return cpu_ticks_offset
;
508 return cpu_get_real_ticks() + cpu_ticks_offset
;
512 /* enable cpu_get_ticks() */
513 void cpu_enable_ticks(void)
515 if (!cpu_ticks_enabled
) {
516 cpu_ticks_offset
-= cpu_get_real_ticks();
517 cpu_ticks_enabled
= 1;
521 /* disable cpu_get_ticks() : the clock is stopped. You must not call
522 cpu_get_ticks() after that. */
523 void cpu_disable_ticks(void)
525 if (cpu_ticks_enabled
) {
526 cpu_ticks_offset
= cpu_get_ticks();
527 cpu_ticks_enabled
= 0;
531 static int64_t get_clock(void)
536 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
539 gettimeofday(&tv
, NULL
);
540 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
544 void cpu_calibrate_ticks(void)
549 ticks
= cpu_get_real_ticks();
555 usec
= get_clock() - usec
;
556 ticks
= cpu_get_real_ticks() - ticks
;
557 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
560 /* compute with 96 bit intermediate result: (a*b)/c */
561 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
566 #ifdef WORDS_BIGENDIAN
576 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
577 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
580 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
584 #define QEMU_TIMER_REALTIME 0
585 #define QEMU_TIMER_VIRTUAL 1
589 /* XXX: add frequency */
597 struct QEMUTimer
*next
;
603 static QEMUTimer
*active_timers
[2];
605 static MMRESULT timerID
;
607 /* frequency of the times() clock tick */
608 static int timer_freq
;
611 QEMUClock
*qemu_new_clock(int type
)
614 clock
= qemu_mallocz(sizeof(QEMUClock
));
621 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
625 ts
= qemu_mallocz(sizeof(QEMUTimer
));
632 void qemu_free_timer(QEMUTimer
*ts
)
637 /* stop a timer, but do not dealloc it */
638 void qemu_del_timer(QEMUTimer
*ts
)
642 /* NOTE: this code must be signal safe because
643 qemu_timer_expired() can be called from a signal. */
644 pt
= &active_timers
[ts
->clock
->type
];
657 /* modify the current timer so that it will be fired when current_time
658 >= expire_time. The corresponding callback will be called. */
659 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
665 /* add the timer in the sorted list */
666 /* NOTE: this code must be signal safe because
667 qemu_timer_expired() can be called from a signal. */
668 pt
= &active_timers
[ts
->clock
->type
];
673 if (t
->expire_time
> expire_time
)
677 ts
->expire_time
= expire_time
;
682 int qemu_timer_pending(QEMUTimer
*ts
)
685 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
692 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
696 return (timer_head
->expire_time
<= current_time
);
699 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
705 if (ts
->expire_time
> current_time
)
707 /* remove timer from the list before calling the callback */
708 *ptimer_head
= ts
->next
;
711 /* run the callback (the timer list can be modified) */
716 int64_t qemu_get_clock(QEMUClock
*clock
)
718 switch(clock
->type
) {
719 case QEMU_TIMER_REALTIME
:
721 return GetTickCount();
726 /* Note that using gettimeofday() is not a good solution
727 for timers because its value change when the date is
729 if (timer_freq
== 100) {
730 return times(&tp
) * 10;
732 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
737 case QEMU_TIMER_VIRTUAL
:
738 return cpu_get_ticks();
743 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
745 uint64_t expire_time
;
747 if (qemu_timer_pending(ts
)) {
748 expire_time
= ts
->expire_time
;
752 qemu_put_be64(f
, expire_time
);
755 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
757 uint64_t expire_time
;
759 expire_time
= qemu_get_be64(f
);
760 if (expire_time
!= -1) {
761 qemu_mod_timer(ts
, expire_time
);
767 static void timer_save(QEMUFile
*f
, void *opaque
)
769 if (cpu_ticks_enabled
) {
770 hw_error("cannot save state if virtual timers are running");
772 qemu_put_be64s(f
, &cpu_ticks_offset
);
773 qemu_put_be64s(f
, &ticks_per_sec
);
776 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
780 if (cpu_ticks_enabled
) {
783 qemu_get_be64s(f
, &cpu_ticks_offset
);
784 qemu_get_be64s(f
, &ticks_per_sec
);
789 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
790 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
792 static void host_alarm_handler(int host_signum
)
796 #define DISP_FREQ 1000
798 static int64_t delta_min
= INT64_MAX
;
799 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
801 ti
= qemu_get_clock(vm_clock
);
802 if (last_clock
!= 0) {
803 delta
= ti
- last_clock
;
804 if (delta
< delta_min
)
806 if (delta
> delta_max
)
809 if (++count
== DISP_FREQ
) {
810 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
811 muldiv64(delta_min
, 1000000, ticks_per_sec
),
812 muldiv64(delta_max
, 1000000, ticks_per_sec
),
813 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
814 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
816 delta_min
= INT64_MAX
;
824 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
825 qemu_get_clock(vm_clock
)) ||
826 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
827 qemu_get_clock(rt_clock
))) {
828 /* stop the cpu because a timer occured */
829 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
835 #if defined(__linux__)
837 #define RTC_FREQ 1024
841 static int start_rtc_timer(void)
843 rtc_fd
= open("/dev/rtc", O_RDONLY
);
846 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
847 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
848 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
849 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
852 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
857 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
863 static int start_rtc_timer(void)
868 #endif /* !defined(__linux__) */
870 #endif /* !defined(_WIN32) */
872 static void init_timers(void)
874 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
875 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
880 timerID
= timeSetEvent(10, // interval (ms)
882 host_alarm_handler
, // function
883 (DWORD
)&count
, // user parameter
884 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
886 perror("failed timer alarm");
890 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
893 struct sigaction act
;
894 struct itimerval itv
;
896 /* get times() syscall frequency */
897 timer_freq
= sysconf(_SC_CLK_TCK
);
900 sigfillset(&act
.sa_mask
);
902 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
903 act
.sa_flags
|= SA_ONSTACK
;
905 act
.sa_handler
= host_alarm_handler
;
906 sigaction(SIGALRM
, &act
, NULL
);
908 itv
.it_interval
.tv_sec
= 0;
909 itv
.it_interval
.tv_usec
= 1000;
910 itv
.it_value
.tv_sec
= 0;
911 itv
.it_value
.tv_usec
= 10 * 1000;
912 setitimer(ITIMER_REAL
, &itv
, NULL
);
913 /* we probe the tick duration of the kernel to inform the user if
914 the emulated kernel requested a too high timer frequency */
915 getitimer(ITIMER_REAL
, &itv
);
917 #if defined(__linux__)
918 if (itv
.it_interval
.tv_usec
> 1000) {
919 /* try to use /dev/rtc to have a faster timer */
920 if (start_rtc_timer() < 0)
923 itv
.it_interval
.tv_sec
= 0;
924 itv
.it_interval
.tv_usec
= 0;
925 itv
.it_value
.tv_sec
= 0;
926 itv
.it_value
.tv_usec
= 0;
927 setitimer(ITIMER_REAL
, &itv
, NULL
);
930 sigaction(SIGIO
, &act
, NULL
);
931 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
932 fcntl(rtc_fd
, F_SETOWN
, getpid());
934 #endif /* defined(__linux__) */
937 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
944 void quit_timers(void)
947 timeKillEvent(timerID
);
951 /***********************************************************/
956 int serial_open_device(void)
963 int serial_open_device(void)
965 if (serial_console
== NULL
&& nographic
) {
966 /* use console for serial port */
970 char slave_name
[1024];
971 int master_fd
, slave_fd
;
974 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
975 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
978 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
988 /***********************************************************/
989 /* Linux network device redirectors */
991 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
995 for(i
=0;i
<size
;i
+=16) {
999 fprintf(f
, "%08x ", i
);
1002 fprintf(f
, " %02x", buf
[i
+j
]);
1007 for(j
=0;j
<len
;j
++) {
1009 if (c
< ' ' || c
> '~')
1011 fprintf(f
, "%c", c
);
1017 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1019 nd
->send_packet(nd
, buf
, size
);
1022 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
1023 IOReadHandler
*fd_read
, void *opaque
)
1025 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
1028 /* dummy network adapter */
1030 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1034 static void dummy_add_read_packet(NetDriverState
*nd
,
1035 IOCanRWHandler
*fd_can_read
,
1036 IOReadHandler
*fd_read
, void *opaque
)
1040 static int net_dummy_init(NetDriverState
*nd
)
1042 nd
->send_packet
= dummy_send_packet
;
1043 nd
->add_read_packet
= dummy_add_read_packet
;
1044 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
1048 #if defined(CONFIG_SLIRP)
1050 /* slirp network adapter */
1052 static void *slirp_fd_opaque
;
1053 static IOCanRWHandler
*slirp_fd_can_read
;
1054 static IOReadHandler
*slirp_fd_read
;
1055 static int slirp_inited
;
1057 int slirp_can_output(void)
1059 return slirp_fd_can_read(slirp_fd_opaque
);
1062 void slirp_output(const uint8_t *pkt
, int pkt_len
)
1065 printf("output:\n");
1066 hex_dump(stdout
, pkt
, pkt_len
);
1068 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
1071 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1075 hex_dump(stdout
, buf
, size
);
1077 slirp_input(buf
, size
);
1080 static void slirp_add_read_packet(NetDriverState
*nd
,
1081 IOCanRWHandler
*fd_can_read
,
1082 IOReadHandler
*fd_read
, void *opaque
)
1084 slirp_fd_opaque
= opaque
;
1085 slirp_fd_can_read
= fd_can_read
;
1086 slirp_fd_read
= fd_read
;
1089 static int net_slirp_init(NetDriverState
*nd
)
1091 if (!slirp_inited
) {
1095 nd
->send_packet
= slirp_send_packet
;
1096 nd
->add_read_packet
= slirp_add_read_packet
;
1097 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1101 #endif /* CONFIG_SLIRP */
1103 #if !defined(_WIN32)
1105 static int tun_open(char *ifname
, int ifname_size
)
1111 fd
= open("/dev/tap", O_RDWR
);
1113 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1118 dev
= devname(s
.st_rdev
, S_IFCHR
);
1119 pstrcpy(ifname
, ifname_size
, dev
);
1121 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1125 static int tun_open(char *ifname
, int ifname_size
)
1130 fd
= open("/dev/net/tun", O_RDWR
);
1132 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1135 memset(&ifr
, 0, sizeof(ifr
));
1136 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1137 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1138 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1140 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1144 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1145 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1146 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1151 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1153 write(nd
->fd
, buf
, size
);
1156 static void tun_add_read_packet(NetDriverState
*nd
,
1157 IOCanRWHandler
*fd_can_read
,
1158 IOReadHandler
*fd_read
, void *opaque
)
1160 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1163 static int net_tun_init(NetDriverState
*nd
)
1169 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1173 /* try to launch network init script */
1178 *parg
++ = network_script
;
1179 *parg
++ = nd
->ifname
;
1181 execv(network_script
, args
);
1184 while (waitpid(pid
, &status
, 0) != pid
);
1185 if (!WIFEXITED(status
) ||
1186 WEXITSTATUS(status
) != 0) {
1187 fprintf(stderr
, "%s: could not launch network script\n",
1191 nd
->send_packet
= tun_send_packet
;
1192 nd
->add_read_packet
= tun_add_read_packet
;
1196 static int net_fd_init(NetDriverState
*nd
, int fd
)
1199 nd
->send_packet
= tun_send_packet
;
1200 nd
->add_read_packet
= tun_add_read_packet
;
1201 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1205 #endif /* !_WIN32 */
1207 /***********************************************************/
1212 static void term_exit(void)
1216 static void term_init(void)
1222 /* init terminal so that we can grab keys */
1223 static struct termios oldtty
;
1224 static int old_fd0_flags
;
1226 static void term_exit(void)
1228 tcsetattr (0, TCSANOW
, &oldtty
);
1229 fcntl(0, F_SETFL
, old_fd0_flags
);
1232 static void term_init(void)
1236 tcgetattr (0, &tty
);
1238 old_fd0_flags
= fcntl(0, F_GETFL
);
1240 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1241 |INLCR
|IGNCR
|ICRNL
|IXON
);
1242 tty
.c_oflag
|= OPOST
;
1243 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1244 /* if graphical mode, we allow Ctrl-C handling */
1246 tty
.c_lflag
&= ~ISIG
;
1247 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1250 tty
.c_cc
[VTIME
] = 0;
1252 tcsetattr (0, TCSANOW
, &tty
);
1256 fcntl(0, F_SETFL
, O_NONBLOCK
);
1261 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1265 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1269 static void dumb_refresh(DisplayState
*ds
)
1271 vga_update_display();
1274 void dumb_display_init(DisplayState
*ds
)
1279 ds
->dpy_update
= dumb_update
;
1280 ds
->dpy_resize
= dumb_resize
;
1281 ds
->dpy_refresh
= dumb_refresh
;
1284 #if !defined(CONFIG_SOFTMMU)
1285 /***********************************************************/
1286 /* cpu signal handler */
1287 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1290 if (cpu_signal_handler(host_signum
, info
, puc
))
1297 /***********************************************************/
1300 #define MAX_IO_HANDLERS 64
1302 typedef struct IOHandlerRecord
{
1304 IOCanRWHandler
*fd_can_read
;
1305 IOReadHandler
*fd_read
;
1307 /* temporary data */
1310 struct IOHandlerRecord
*next
;
1313 static IOHandlerRecord
*first_io_handler
;
1315 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1316 IOReadHandler
*fd_read
, void *opaque
)
1318 IOHandlerRecord
*ioh
;
1320 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1324 ioh
->fd_can_read
= fd_can_read
;
1325 ioh
->fd_read
= fd_read
;
1326 ioh
->opaque
= opaque
;
1327 ioh
->next
= first_io_handler
;
1328 first_io_handler
= ioh
;
1332 void qemu_del_fd_read_handler(int fd
)
1334 IOHandlerRecord
**pioh
, *ioh
;
1336 pioh
= &first_io_handler
;
1341 if (ioh
->fd
== fd
) {
1349 /***********************************************************/
1350 /* savevm/loadvm support */
1352 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1354 fwrite(buf
, 1, size
, f
);
1357 void qemu_put_byte(QEMUFile
*f
, int v
)
1362 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1364 qemu_put_byte(f
, v
>> 8);
1365 qemu_put_byte(f
, v
);
1368 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1370 qemu_put_byte(f
, v
>> 24);
1371 qemu_put_byte(f
, v
>> 16);
1372 qemu_put_byte(f
, v
>> 8);
1373 qemu_put_byte(f
, v
);
1376 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1378 qemu_put_be32(f
, v
>> 32);
1379 qemu_put_be32(f
, v
);
1382 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1384 return fread(buf
, 1, size
, f
);
1387 int qemu_get_byte(QEMUFile
*f
)
1397 unsigned int qemu_get_be16(QEMUFile
*f
)
1400 v
= qemu_get_byte(f
) << 8;
1401 v
|= qemu_get_byte(f
);
1405 unsigned int qemu_get_be32(QEMUFile
*f
)
1408 v
= qemu_get_byte(f
) << 24;
1409 v
|= qemu_get_byte(f
) << 16;
1410 v
|= qemu_get_byte(f
) << 8;
1411 v
|= qemu_get_byte(f
);
1415 uint64_t qemu_get_be64(QEMUFile
*f
)
1418 v
= (uint64_t)qemu_get_be32(f
) << 32;
1419 v
|= qemu_get_be32(f
);
1423 int64_t qemu_ftell(QEMUFile
*f
)
1428 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1430 if (fseek(f
, pos
, whence
) < 0)
1435 typedef struct SaveStateEntry
{
1439 SaveStateHandler
*save_state
;
1440 LoadStateHandler
*load_state
;
1442 struct SaveStateEntry
*next
;
1445 static SaveStateEntry
*first_se
;
1447 int register_savevm(const char *idstr
,
1450 SaveStateHandler
*save_state
,
1451 LoadStateHandler
*load_state
,
1454 SaveStateEntry
*se
, **pse
;
1456 se
= qemu_malloc(sizeof(SaveStateEntry
));
1459 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1460 se
->instance_id
= instance_id
;
1461 se
->version_id
= version_id
;
1462 se
->save_state
= save_state
;
1463 se
->load_state
= load_state
;
1464 se
->opaque
= opaque
;
1467 /* add at the end of list */
1469 while (*pse
!= NULL
)
1470 pse
= &(*pse
)->next
;
1475 #define QEMU_VM_FILE_MAGIC 0x5145564d
1476 #define QEMU_VM_FILE_VERSION 0x00000001
1478 int qemu_savevm(const char *filename
)
1482 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1484 saved_vm_running
= vm_running
;
1487 f
= fopen(filename
, "wb");
1493 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1494 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1496 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1498 len
= strlen(se
->idstr
);
1499 qemu_put_byte(f
, len
);
1500 qemu_put_buffer(f
, se
->idstr
, len
);
1502 qemu_put_be32(f
, se
->instance_id
);
1503 qemu_put_be32(f
, se
->version_id
);
1505 /* record size: filled later */
1507 qemu_put_be32(f
, 0);
1509 se
->save_state(f
, se
->opaque
);
1511 /* fill record size */
1513 len
= ftell(f
) - len_pos
- 4;
1514 fseek(f
, len_pos
, SEEK_SET
);
1515 qemu_put_be32(f
, len
);
1516 fseek(f
, cur_pos
, SEEK_SET
);
1522 if (saved_vm_running
)
1527 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1531 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1532 if (!strcmp(se
->idstr
, idstr
) &&
1533 instance_id
== se
->instance_id
)
1539 int qemu_loadvm(const char *filename
)
1543 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1544 int saved_vm_running
;
1548 saved_vm_running
= vm_running
;
1551 f
= fopen(filename
, "rb");
1557 v
= qemu_get_be32(f
);
1558 if (v
!= QEMU_VM_FILE_MAGIC
)
1560 v
= qemu_get_be32(f
);
1561 if (v
!= QEMU_VM_FILE_VERSION
) {
1568 #if defined (DO_TB_FLUSH)
1569 tb_flush(global_env
);
1571 len
= qemu_get_byte(f
);
1574 qemu_get_buffer(f
, idstr
, len
);
1576 instance_id
= qemu_get_be32(f
);
1577 version_id
= qemu_get_be32(f
);
1578 record_len
= qemu_get_be32(f
);
1580 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1581 idstr
, instance_id
, version_id
, record_len
);
1584 se
= find_se(idstr
, instance_id
);
1586 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1587 instance_id
, idstr
);
1589 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1591 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1592 instance_id
, idstr
);
1595 /* always seek to exact end of record */
1596 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1601 if (saved_vm_running
)
1606 /***********************************************************/
1607 /* cpu save/restore */
1609 #if defined(TARGET_I386)
1611 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1613 qemu_put_be32(f
, dt
->selector
);
1614 qemu_put_be32(f
, (uint32_t)dt
->base
);
1615 qemu_put_be32(f
, dt
->limit
);
1616 qemu_put_be32(f
, dt
->flags
);
1619 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1621 dt
->selector
= qemu_get_be32(f
);
1622 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1623 dt
->limit
= qemu_get_be32(f
);
1624 dt
->flags
= qemu_get_be32(f
);
1627 void cpu_save(QEMUFile
*f
, void *opaque
)
1629 CPUState
*env
= opaque
;
1630 uint16_t fptag
, fpus
, fpuc
;
1634 for(i
= 0; i
< 8; i
++)
1635 qemu_put_be32s(f
, &env
->regs
[i
]);
1636 qemu_put_be32s(f
, &env
->eip
);
1637 qemu_put_be32s(f
, &env
->eflags
);
1638 qemu_put_be32s(f
, &env
->eflags
);
1639 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1640 qemu_put_be32s(f
, &hflags
);
1644 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1646 for (i
=7; i
>=0; i
--) {
1648 if (env
->fptags
[i
]) {
1653 qemu_put_be16s(f
, &fpuc
);
1654 qemu_put_be16s(f
, &fpus
);
1655 qemu_put_be16s(f
, &fptag
);
1657 for(i
= 0; i
< 8; i
++) {
1660 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1661 qemu_put_be64(f
, mant
);
1662 qemu_put_be16(f
, exp
);
1665 for(i
= 0; i
< 6; i
++)
1666 cpu_put_seg(f
, &env
->segs
[i
]);
1667 cpu_put_seg(f
, &env
->ldt
);
1668 cpu_put_seg(f
, &env
->tr
);
1669 cpu_put_seg(f
, &env
->gdt
);
1670 cpu_put_seg(f
, &env
->idt
);
1672 qemu_put_be32s(f
, &env
->sysenter_cs
);
1673 qemu_put_be32s(f
, &env
->sysenter_esp
);
1674 qemu_put_be32s(f
, &env
->sysenter_eip
);
1676 qemu_put_be32s(f
, &env
->cr
[0]);
1677 qemu_put_be32s(f
, &env
->cr
[2]);
1678 qemu_put_be32s(f
, &env
->cr
[3]);
1679 qemu_put_be32s(f
, &env
->cr
[4]);
1681 for(i
= 0; i
< 8; i
++)
1682 qemu_put_be32s(f
, &env
->dr
[i
]);
1685 qemu_put_be32s(f
, &env
->a20_mask
);
1688 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1690 CPUState
*env
= opaque
;
1693 uint16_t fpus
, fpuc
, fptag
;
1695 if (version_id
!= 2)
1697 for(i
= 0; i
< 8; i
++)
1698 qemu_get_be32s(f
, &env
->regs
[i
]);
1699 qemu_get_be32s(f
, &env
->eip
);
1700 qemu_get_be32s(f
, &env
->eflags
);
1701 qemu_get_be32s(f
, &env
->eflags
);
1702 qemu_get_be32s(f
, &hflags
);
1704 qemu_get_be16s(f
, &fpuc
);
1705 qemu_get_be16s(f
, &fpus
);
1706 qemu_get_be16s(f
, &fptag
);
1708 for(i
= 0; i
< 8; i
++) {
1711 mant
= qemu_get_be64(f
);
1712 exp
= qemu_get_be16(f
);
1713 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1717 env
->fpstt
= (fpus
>> 11) & 7;
1718 env
->fpus
= fpus
& ~0x3800;
1719 for(i
= 0; i
< 8; i
++) {
1720 env
->fptags
[i
] = ((fptag
& 3) == 3);
1724 for(i
= 0; i
< 6; i
++)
1725 cpu_get_seg(f
, &env
->segs
[i
]);
1726 cpu_get_seg(f
, &env
->ldt
);
1727 cpu_get_seg(f
, &env
->tr
);
1728 cpu_get_seg(f
, &env
->gdt
);
1729 cpu_get_seg(f
, &env
->idt
);
1731 qemu_get_be32s(f
, &env
->sysenter_cs
);
1732 qemu_get_be32s(f
, &env
->sysenter_esp
);
1733 qemu_get_be32s(f
, &env
->sysenter_eip
);
1735 qemu_get_be32s(f
, &env
->cr
[0]);
1736 qemu_get_be32s(f
, &env
->cr
[2]);
1737 qemu_get_be32s(f
, &env
->cr
[3]);
1738 qemu_get_be32s(f
, &env
->cr
[4]);
1740 for(i
= 0; i
< 8; i
++)
1741 qemu_get_be32s(f
, &env
->dr
[i
]);
1744 qemu_get_be32s(f
, &env
->a20_mask
);
1746 /* XXX: compute hflags from scratch, except for CPL and IIF */
1747 env
->hflags
= hflags
;
1752 #elif defined(TARGET_PPC)
1753 void cpu_save(QEMUFile
*f
, void *opaque
)
1757 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1763 #warning No CPU save/restore functions
1767 /***********************************************************/
1768 /* ram save/restore */
1770 /* we just avoid storing empty pages */
1771 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1776 for(i
= 1; i
< len
; i
++) {
1780 qemu_put_byte(f
, 1);
1781 qemu_put_byte(f
, v
);
1784 qemu_put_byte(f
, 0);
1785 qemu_put_buffer(f
, buf
, len
);
1788 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1792 v
= qemu_get_byte(f
);
1795 if (qemu_get_buffer(f
, buf
, len
) != len
)
1799 v
= qemu_get_byte(f
);
1800 memset(buf
, v
, len
);
1808 static void ram_save(QEMUFile
*f
, void *opaque
)
1811 qemu_put_be32(f
, phys_ram_size
);
1812 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1813 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1817 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1821 if (version_id
!= 1)
1823 if (qemu_get_be32(f
) != phys_ram_size
)
1825 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1826 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1833 /***********************************************************/
1834 /* main execution loop */
1836 void gui_update(void *opaque
)
1838 display_state
.dpy_refresh(&display_state
);
1839 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1842 /* XXX: support several handlers */
1843 VMStopHandler
*vm_stop_cb
;
1844 VMStopHandler
*vm_stop_opaque
;
1846 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1849 vm_stop_opaque
= opaque
;
1853 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1866 void vm_stop(int reason
)
1869 cpu_disable_ticks();
1873 vm_stop_cb(vm_stop_opaque
, reason
);
1879 /* reset/shutdown handler */
1881 typedef struct QEMUResetEntry
{
1882 QEMUResetHandler
*func
;
1884 struct QEMUResetEntry
*next
;
1887 static QEMUResetEntry
*first_reset_entry
;
1888 static int reset_requested
;
1889 static int shutdown_requested
;
1891 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1893 QEMUResetEntry
**pre
, *re
;
1895 pre
= &first_reset_entry
;
1896 while (*pre
!= NULL
)
1897 pre
= &(*pre
)->next
;
1898 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1900 re
->opaque
= opaque
;
1905 void qemu_system_reset(void)
1909 /* reset all devices */
1910 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
1911 re
->func(re
->opaque
);
1915 void qemu_system_reset_request(void)
1917 reset_requested
= 1;
1918 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1921 void qemu_system_shutdown_request(void)
1923 shutdown_requested
= 1;
1924 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1927 static void main_cpu_reset(void *opaque
)
1930 CPUState
*env
= opaque
;
1938 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1939 IOHandlerRecord
*ioh
, *ioh_next
;
1944 CPUState
*env
= global_env
;
1948 ret
= cpu_exec(env
);
1949 if (shutdown_requested
) {
1950 ret
= EXCP_INTERRUPT
;
1953 if (reset_requested
) {
1954 reset_requested
= 0;
1955 qemu_system_reset();
1956 ret
= EXCP_INTERRUPT
;
1958 if (ret
== EXCP_DEBUG
) {
1959 vm_stop(EXCP_DEBUG
);
1961 /* if hlt instruction, we wait until the next IRQ */
1962 /* XXX: use timeout computed from timers */
1963 if (ret
== EXCP_HLT
)
1976 /* poll any events */
1977 /* XXX: separate device handlers from system ones */
1979 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1980 if (!ioh
->fd_can_read
) {
1983 pf
->events
= POLLIN
;
1987 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1989 if (max_size
> sizeof(buf
))
1990 max_size
= sizeof(buf
);
1992 pf
->events
= POLLIN
;
1999 ioh
->max_size
= max_size
;
2002 ret
= poll(ufds
, pf
- ufds
, timeout
);
2004 /* XXX: better handling of removal */
2005 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
2006 ioh_next
= ioh
->next
;
2009 if (pf
->revents
& POLLIN
) {
2010 if (ioh
->max_size
== 0) {
2011 /* just a read event */
2012 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
2014 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
2016 ioh
->fd_read(ioh
->opaque
, buf
, n
);
2017 } else if (errno
!= EAGAIN
) {
2018 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
2026 #if defined(CONFIG_SLIRP)
2027 /* XXX: merge with poll() */
2029 fd_set rfds
, wfds
, xfds
;
2037 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2040 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2042 slirp_select_poll(&rfds
, &wfds
, &xfds
);
2050 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
2051 qemu_get_clock(vm_clock
));
2053 if (audio_enabled
) {
2054 /* XXX: add explicit timer */
2058 /* run dma transfers, if any */
2062 /* real time timers */
2063 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
2064 qemu_get_clock(rt_clock
));
2066 cpu_disable_ticks();
2072 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
2073 "usage: %s [options] [disk_image]\n"
2075 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2077 "Standard options:\n"
2078 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2079 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2080 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2081 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2082 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
2083 "-snapshot write to temporary files instead of disk image files\n"
2084 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2085 "-nographic disable graphical output and redirect serial I/Os to console\n"
2086 "-enable-audio enable audio support\n"
2087 "-localtime set the real time clock to local time [default=utc]\n"
2089 "-prep Simulate a PREP system (default is PowerMAC)\n"
2090 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2093 "Network options:\n"
2094 "-nics n simulate 'n' network cards [default=1]\n"
2095 "-macaddr addr set the mac address of the first interface\n"
2096 "-n script set tap/tun network init script [default=%s]\n"
2097 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2099 "-user-net use user mode network stack [default if no tap/tun script]\n"
2101 "-dummy-net use dummy network stack\n"
2103 "Linux boot specific:\n"
2104 "-kernel bzImage use 'bzImage' as kernel image\n"
2105 "-append cmdline use 'cmdline' as kernel command line\n"
2106 "-initrd file use 'file' as initial ram disk\n"
2108 "Debug/Expert options:\n"
2109 "-S freeze CPU at startup (use 'c' to start execution)\n"
2110 "-s wait gdb connection to port %d\n"
2111 "-p port change gdb connection port\n"
2112 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2113 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
2114 "-L path set the directory for the BIOS and VGA BIOS\n"
2115 #ifdef USE_CODE_COPY
2116 "-no-code-copy disable code copy acceleration\n"
2119 "-isa simulate an ISA-only system (default is PCI system)\n"
2120 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2121 " (default is CL-GD5446 PCI VGA)\n"
2124 "During emulation, use C-a h to get terminal commands:\n",
2125 #ifdef CONFIG_SOFTMMU
2131 DEFAULT_NETWORK_SCRIPT
,
2132 DEFAULT_GDBSTUB_PORT
,
2135 #ifndef CONFIG_SOFTMMU
2137 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2138 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2144 #define HAS_ARG 0x0001
2157 QEMU_OPTION_snapshot
,
2159 QEMU_OPTION_nographic
,
2160 QEMU_OPTION_enable_audio
,
2163 QEMU_OPTION_macaddr
,
2166 QEMU_OPTION_user_net
,
2167 QEMU_OPTION_dummy_net
,
2179 QEMU_OPTION_no_code_copy
,
2183 QEMU_OPTION_localtime
,
2184 QEMU_OPTION_cirrusvga
,
2186 QEMU_OPTION_std_vga
,
2189 typedef struct QEMUOption
{
2195 const QEMUOption qemu_options
[] = {
2196 { "h", 0, QEMU_OPTION_h
},
2198 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2199 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2200 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2201 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2202 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2203 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2204 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2205 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2206 { "snapshot", 0, QEMU_OPTION_snapshot
},
2207 { "m", HAS_ARG
, QEMU_OPTION_m
},
2208 { "nographic", 0, QEMU_OPTION_nographic
},
2209 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2211 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2212 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2213 { "n", HAS_ARG
, QEMU_OPTION_n
},
2214 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2216 { "user-net", 0, QEMU_OPTION_user_net
},
2218 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2220 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2221 { "append", HAS_ARG
, QEMU_OPTION_append
},
2222 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2224 { "S", 0, QEMU_OPTION_S
},
2225 { "s", 0, QEMU_OPTION_s
},
2226 { "p", HAS_ARG
, QEMU_OPTION_p
},
2227 { "d", HAS_ARG
, QEMU_OPTION_d
},
2228 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2229 { "L", HAS_ARG
, QEMU_OPTION_L
},
2230 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2232 { "prep", 0, QEMU_OPTION_prep
},
2233 { "g", 1, QEMU_OPTION_g
},
2235 { "localtime", 0, QEMU_OPTION_localtime
},
2236 { "isa", 0, QEMU_OPTION_isa
},
2237 { "std-vga", 0, QEMU_OPTION_std_vga
},
2239 /* temporary options */
2240 { "pci", 0, QEMU_OPTION_pci
},
2241 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
2245 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2247 /* this stack is only used during signal handling */
2248 #define SIGNAL_STACK_SIZE 32768
2250 static uint8_t *signal_stack
;
2254 #define NET_IF_TUN 0
2255 #define NET_IF_USER 1
2256 #define NET_IF_DUMMY 2
2258 int main(int argc
, char **argv
)
2260 #ifdef CONFIG_GDBSTUB
2261 int use_gdbstub
, gdbstub_port
;
2264 int snapshot
, linux_boot
;
2266 const char *initrd_filename
;
2267 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2268 const char *kernel_filename
, *kernel_cmdline
;
2269 DisplayState
*ds
= &display_state
;
2270 int cyls
, heads
, secs
;
2271 int start_emulation
= 1;
2273 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2275 const char *r
, *optarg
;
2277 #if !defined(CONFIG_SOFTMMU)
2278 /* we never want that malloc() uses mmap() */
2279 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2281 initrd_filename
= NULL
;
2282 for(i
= 0; i
< MAX_FD
; i
++)
2283 fd_filename
[i
] = NULL
;
2284 for(i
= 0; i
< MAX_DISKS
; i
++)
2285 hd_filename
[i
] = NULL
;
2286 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2287 vga_ram_size
= VGA_RAM_SIZE
;
2288 bios_size
= BIOS_SIZE
;
2289 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2290 #ifdef CONFIG_GDBSTUB
2292 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2296 kernel_filename
= NULL
;
2297 kernel_cmdline
= "";
2299 cyls
= heads
= secs
= 0;
2304 /* default mac address of the first network interface */
2318 hd_filename
[0] = argv
[optind
++];
2320 const QEMUOption
*popt
;
2323 popt
= qemu_options
;
2326 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2330 if (!strcmp(popt
->name
, r
+ 1))
2334 if (popt
->flags
& HAS_ARG
) {
2335 if (optind
>= argc
) {
2336 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2340 optarg
= argv
[optind
++];
2345 switch(popt
->index
) {
2346 case QEMU_OPTION_initrd
:
2347 initrd_filename
= optarg
;
2349 case QEMU_OPTION_hda
:
2350 hd_filename
[0] = optarg
;
2352 case QEMU_OPTION_hdb
:
2353 hd_filename
[1] = optarg
;
2355 case QEMU_OPTION_snapshot
:
2358 case QEMU_OPTION_hdachs
:
2362 cyls
= strtol(p
, (char **)&p
, 0);
2366 heads
= strtol(p
, (char **)&p
, 0);
2370 secs
= strtol(p
, (char **)&p
, 0);
2377 case QEMU_OPTION_nographic
:
2380 case QEMU_OPTION_kernel
:
2381 kernel_filename
= optarg
;
2383 case QEMU_OPTION_append
:
2384 kernel_cmdline
= optarg
;
2386 case QEMU_OPTION_tun_fd
:
2390 net_if_type
= NET_IF_TUN
;
2391 if (nb_tun_fds
< MAX_NICS
) {
2392 fd
= strtol(optarg
, (char **)&p
, 0);
2394 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2397 tun_fds
[nb_tun_fds
++] = fd
;
2401 case QEMU_OPTION_hdc
:
2402 hd_filename
[2] = optarg
;
2405 case QEMU_OPTION_hdd
:
2406 hd_filename
[3] = optarg
;
2408 case QEMU_OPTION_cdrom
:
2409 hd_filename
[2] = optarg
;
2412 case QEMU_OPTION_boot
:
2413 boot_device
= optarg
[0];
2414 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2415 boot_device
!= 'c' && boot_device
!= 'd') {
2416 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2420 case QEMU_OPTION_fda
:
2421 fd_filename
[0] = optarg
;
2423 case QEMU_OPTION_fdb
:
2424 fd_filename
[1] = optarg
;
2426 case QEMU_OPTION_no_code_copy
:
2427 code_copy_enabled
= 0;
2429 case QEMU_OPTION_nics
:
2430 nb_nics
= atoi(optarg
);
2431 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2432 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2436 case QEMU_OPTION_macaddr
:
2441 for(i
= 0; i
< 6; i
++) {
2442 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2449 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2457 case QEMU_OPTION_user_net
:
2458 net_if_type
= NET_IF_USER
;
2460 case QEMU_OPTION_dummy_net
:
2461 net_if_type
= NET_IF_DUMMY
;
2463 case QEMU_OPTION_enable_audio
:
2470 ram_size
= atoi(optarg
) * 1024 * 1024;
2473 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2474 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2475 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2484 mask
= cpu_str_to_log_mask(optarg
);
2486 printf("Log items (comma separated):\n");
2487 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2488 printf("%-10s %s\n", item
->name
, item
->help
);
2496 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2498 #ifdef CONFIG_GDBSTUB
2503 gdbstub_port
= atoi(optarg
);
2510 start_emulation
= 0;
2512 case QEMU_OPTION_pci
:
2515 case QEMU_OPTION_isa
:
2518 case QEMU_OPTION_prep
:
2521 case QEMU_OPTION_localtime
:
2524 case QEMU_OPTION_cirrusvga
:
2525 cirrus_vga_enabled
= 1;
2527 case QEMU_OPTION_std_vga
:
2528 cirrus_vga_enabled
= 0;
2535 w
= strtol(p
, (char **)&p
, 10);
2538 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2544 h
= strtol(p
, (char **)&p
, 10);
2549 depth
= strtol(p
, (char **)&p
, 10);
2550 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2551 depth
!= 24 && depth
!= 32)
2553 } else if (*p
== '\0') {
2554 depth
= graphic_depth
;
2561 graphic_depth
= depth
;
2568 linux_boot
= (kernel_filename
!= NULL
);
2570 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2571 fd_filename
[0] == '\0')
2574 /* boot to cd by default if no hard disk */
2575 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2576 if (fd_filename
[0] != '\0')
2582 #if !defined(CONFIG_SOFTMMU)
2583 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2585 static uint8_t stdout_buf
[4096];
2586 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2589 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2592 /* init host network redirectors */
2593 if (net_if_type
== -1) {
2594 net_if_type
= NET_IF_TUN
;
2595 #if defined(CONFIG_SLIRP)
2596 if (access(network_script
, R_OK
) < 0) {
2597 net_if_type
= NET_IF_USER
;
2602 for(i
= 0; i
< nb_nics
; i
++) {
2603 NetDriverState
*nd
= &nd_table
[i
];
2605 /* init virtual mac address */
2606 nd
->macaddr
[0] = macaddr
[0];
2607 nd
->macaddr
[1] = macaddr
[1];
2608 nd
->macaddr
[2] = macaddr
[2];
2609 nd
->macaddr
[3] = macaddr
[3];
2610 nd
->macaddr
[4] = macaddr
[4];
2611 nd
->macaddr
[5] = macaddr
[5] + i
;
2612 switch(net_if_type
) {
2613 #if defined(CONFIG_SLIRP)
2618 #if !defined(_WIN32)
2620 if (i
< nb_tun_fds
) {
2621 net_fd_init(nd
, tun_fds
[i
]);
2623 if (net_tun_init(nd
) < 0)
2635 /* init the memory */
2636 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
2638 #ifdef CONFIG_SOFTMMU
2640 /* mallocs are always aligned on BSD. valloc is better for correctness */
2641 phys_ram_base
= valloc(phys_ram_size
);
2643 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2645 if (!phys_ram_base
) {
2646 fprintf(stderr
, "Could not allocate physical memory\n");
2650 /* as we must map the same page at several addresses, we must use
2655 tmpdir
= getenv("QEMU_TMPDIR");
2658 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2659 if (mkstemp(phys_ram_file
) < 0) {
2660 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2664 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2665 if (phys_ram_fd
< 0) {
2666 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2670 ftruncate(phys_ram_fd
, phys_ram_size
);
2671 unlink(phys_ram_file
);
2672 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2674 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2676 if (phys_ram_base
== MAP_FAILED
) {
2677 fprintf(stderr
, "Could not map physical memory\n");
2683 /* we always create the cdrom drive, even if no disk is there */
2685 bs_table
[2] = bdrv_new("cdrom");
2686 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2689 /* open the virtual block devices */
2690 for(i
= 0; i
< MAX_DISKS
; i
++) {
2691 if (hd_filename
[i
]) {
2694 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2695 bs_table
[i
] = bdrv_new(buf
);
2697 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2698 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2702 if (i
== 0 && cyls
!= 0)
2703 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2707 /* we always create at least one floppy disk */
2708 fd_table
[0] = bdrv_new("fda");
2709 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2711 for(i
= 0; i
< MAX_FD
; i
++) {
2712 if (fd_filename
[i
]) {
2715 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2716 fd_table
[i
] = bdrv_new(buf
);
2717 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2719 if (fd_filename
[i
] != '\0') {
2720 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2721 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2729 /* init CPU state */
2732 cpu_single_env
= env
;
2734 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2735 register_savevm("cpu", 0, 2, cpu_save
, cpu_load
, env
);
2736 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2737 qemu_register_reset(main_cpu_reset
, global_env
);
2740 cpu_calibrate_ticks();
2744 dumb_display_init(ds
);
2747 sdl_display_init(ds
);
2749 dumb_display_init(ds
);
2753 /* setup cpu signal handlers for MMU / self modifying code handling */
2754 #if !defined(CONFIG_SOFTMMU)
2756 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2759 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2760 stk
.ss_sp
= signal_stack
;
2761 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2764 if (sigaltstack(&stk
, NULL
) < 0) {
2765 perror("sigaltstack");
2771 struct sigaction act
;
2773 sigfillset(&act
.sa_mask
);
2774 act
.sa_flags
= SA_SIGINFO
;
2775 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2776 act
.sa_flags
|= SA_ONSTACK
;
2778 act
.sa_sigaction
= host_segv_handler
;
2779 sigaction(SIGSEGV
, &act
, NULL
);
2780 sigaction(SIGBUS
, &act
, NULL
);
2781 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2782 sigaction(SIGFPE
, &act
, NULL
);
2789 struct sigaction act
;
2790 sigfillset(&act
.sa_mask
);
2792 act
.sa_handler
= SIG_IGN
;
2793 sigaction(SIGPIPE
, &act
, NULL
);
2798 #if defined(TARGET_I386)
2799 pc_init(ram_size
, vga_ram_size
, boot_device
,
2800 ds
, fd_filename
, snapshot
,
2801 kernel_filename
, kernel_cmdline
, initrd_filename
);
2802 #elif defined(TARGET_PPC)
2803 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2804 ds
, fd_filename
, snapshot
,
2805 kernel_filename
, kernel_cmdline
, initrd_filename
);
2808 /* launched after the device init so that it can display or not a
2812 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2813 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2815 #ifdef CONFIG_GDBSTUB
2817 if (gdbserver_start(gdbstub_port
) < 0) {
2818 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2822 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2826 if (start_emulation
)