4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
46 #include <linux/if_tun.h>
49 #include <linux/rtc.h>
53 #if defined(CONFIG_SLIRP)
59 #include <sys/timeb.h>
61 #define getopt_long_only getopt_long
62 #define memalign(align, size) malloc(size)
66 #if defined(__linux__)
67 /* SDL use the pthreads and they modify sigaction. We don't
69 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
70 extern void __libc_sigaction();
71 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
73 extern void __sigaction();
74 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
76 #endif /* __linux__ */
77 #endif /* CONFIG_SDL */
85 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
87 //#define DEBUG_UNUSED_IOPORT
88 //#define DEBUG_IOPORT
90 #if !defined(CONFIG_SOFTMMU)
91 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
93 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
97 #define DEFAULT_RAM_SIZE 144
99 #define DEFAULT_RAM_SIZE 32
102 #define GUI_REFRESH_INTERVAL 30
104 /* XXX: use a two level table to limit memory usage */
105 #define MAX_IOPORTS 65536
107 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
108 char phys_ram_file
[1024];
109 CPUState
*global_env
;
110 CPUState
*cpu_single_env
;
111 void *ioport_opaque
[MAX_IOPORTS
];
112 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
113 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
114 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
117 static DisplayState display_state
;
119 int64_t ticks_per_sec
;
120 int boot_device
= 'c';
122 static char network_script
[1024];
123 int pit_min_timer_count
= 0;
125 NetDriverState nd_table
[MAX_NICS
];
126 SerialState
*serial_console
;
127 QEMUTimer
*gui_timer
;
129 int audio_enabled
= 0;
131 int prep_enabled
= 0;
133 int cirrus_vga_enabled
= 0;
134 int graphic_width
= 640;
135 int graphic_height
= 480;
136 int graphic_depth
= 15;
138 /***********************************************************/
139 /* x86 ISA bus support */
141 target_phys_addr_t isa_mem_base
= 0;
143 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
145 #ifdef DEBUG_UNUSED_IOPORT
146 fprintf(stderr
, "inb: port=0x%04x\n", address
);
151 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
153 #ifdef DEBUG_UNUSED_IOPORT
154 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
158 /* default is to make two byte accesses */
159 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
162 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
163 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
164 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
168 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
170 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
171 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
172 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
175 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
177 #ifdef DEBUG_UNUSED_IOPORT
178 fprintf(stderr
, "inl: port=0x%04x\n", address
);
183 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
185 #ifdef DEBUG_UNUSED_IOPORT
186 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
190 void init_ioports(void)
194 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
195 ioport_read_table
[0][i
] = default_ioport_readb
;
196 ioport_write_table
[0][i
] = default_ioport_writeb
;
197 ioport_read_table
[1][i
] = default_ioport_readw
;
198 ioport_write_table
[1][i
] = default_ioport_writew
;
199 ioport_read_table
[2][i
] = default_ioport_readl
;
200 ioport_write_table
[2][i
] = default_ioport_writel
;
204 /* size is the word size in byte */
205 int register_ioport_read(int start
, int length
, int size
,
206 IOPortReadFunc
*func
, void *opaque
)
212 } else if (size
== 2) {
214 } else if (size
== 4) {
217 hw_error("register_ioport_read: invalid size");
220 for(i
= start
; i
< start
+ length
; i
+= size
) {
221 ioport_read_table
[bsize
][i
] = func
;
222 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
223 hw_error("register_ioport_read: invalid opaque");
224 ioport_opaque
[i
] = opaque
;
229 /* size is the word size in byte */
230 int register_ioport_write(int start
, int length
, int size
,
231 IOPortWriteFunc
*func
, void *opaque
)
237 } else if (size
== 2) {
239 } else if (size
== 4) {
242 hw_error("register_ioport_write: invalid size");
245 for(i
= start
; i
< start
+ length
; i
+= size
) {
246 ioport_write_table
[bsize
][i
] = func
;
247 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
248 hw_error("register_ioport_read: invalid opaque");
249 ioport_opaque
[i
] = opaque
;
254 void isa_unassign_ioport(int start
, int length
)
258 for(i
= start
; i
< start
+ length
; i
++) {
259 ioport_read_table
[0][i
] = default_ioport_readb
;
260 ioport_read_table
[1][i
] = default_ioport_readw
;
261 ioport_read_table
[2][i
] = default_ioport_readl
;
263 ioport_write_table
[0][i
] = default_ioport_writeb
;
264 ioport_write_table
[1][i
] = default_ioport_writew
;
265 ioport_write_table
[2][i
] = default_ioport_writel
;
269 void pstrcpy(char *buf
, int buf_size
, const char *str
)
279 if (c
== 0 || q
>= buf
+ buf_size
- 1)
286 /* strcat and truncate. */
287 char *pstrcat(char *buf
, int buf_size
, const char *s
)
292 pstrcpy(buf
+ len
, buf_size
- len
, s
);
296 /* return the size or -1 if error */
297 int get_image_size(const char *filename
)
300 fd
= open(filename
, O_RDONLY
| O_BINARY
);
303 size
= lseek(fd
, 0, SEEK_END
);
308 /* return the size or -1 if error */
309 int load_image(const char *filename
, uint8_t *addr
)
312 fd
= open(filename
, O_RDONLY
| O_BINARY
);
315 size
= lseek(fd
, 0, SEEK_END
);
316 lseek(fd
, 0, SEEK_SET
);
317 if (read(fd
, addr
, size
) != size
) {
325 void cpu_outb(CPUState
*env
, int addr
, int val
)
328 if (loglevel
& CPU_LOG_IOPORT
)
329 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
331 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
334 void cpu_outw(CPUState
*env
, int addr
, int val
)
337 if (loglevel
& CPU_LOG_IOPORT
)
338 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
340 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
343 void cpu_outl(CPUState
*env
, int addr
, int val
)
346 if (loglevel
& CPU_LOG_IOPORT
)
347 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
349 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
352 int cpu_inb(CPUState
*env
, int addr
)
355 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
357 if (loglevel
& CPU_LOG_IOPORT
)
358 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
363 int cpu_inw(CPUState
*env
, int addr
)
366 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
368 if (loglevel
& CPU_LOG_IOPORT
)
369 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
374 int cpu_inl(CPUState
*env
, int addr
)
377 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
379 if (loglevel
& CPU_LOG_IOPORT
)
380 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
385 /***********************************************************/
386 void hw_error(const char *fmt
, ...)
391 fprintf(stderr
, "qemu: hardware error: ");
392 vfprintf(stderr
, fmt
, ap
);
393 fprintf(stderr
, "\n");
395 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
397 cpu_dump_state(global_env
, stderr
, 0);
403 /***********************************************************/
406 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
407 static void *qemu_put_kbd_event_opaque
;
408 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
409 static void *qemu_put_mouse_event_opaque
;
411 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
413 qemu_put_kbd_event_opaque
= opaque
;
414 qemu_put_kbd_event
= func
;
417 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
)
419 qemu_put_mouse_event_opaque
= opaque
;
420 qemu_put_mouse_event
= func
;
423 void kbd_put_keycode(int keycode
)
425 if (qemu_put_kbd_event
) {
426 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
430 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
432 if (qemu_put_mouse_event
) {
433 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
434 dx
, dy
, dz
, buttons_state
);
438 /***********************************************************/
441 #if defined(__powerpc__)
443 static inline uint32_t get_tbl(void)
446 asm volatile("mftb %0" : "=r" (tbl
));
450 static inline uint32_t get_tbu(void)
453 asm volatile("mftbu %0" : "=r" (tbl
));
457 int64_t cpu_get_real_ticks(void)
460 /* NOTE: we test if wrapping has occurred */
466 return ((int64_t)h
<< 32) | l
;
469 #elif defined(__i386__)
471 int64_t cpu_get_real_ticks(void)
474 asm volatile ("rdtsc" : "=A" (val
));
478 #elif defined(__x86_64__)
480 int64_t cpu_get_real_ticks(void)
484 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
492 #error unsupported CPU
495 static int64_t cpu_ticks_offset
;
496 static int cpu_ticks_enabled
;
498 static inline int64_t cpu_get_ticks(void)
500 if (!cpu_ticks_enabled
) {
501 return cpu_ticks_offset
;
503 return cpu_get_real_ticks() + cpu_ticks_offset
;
507 /* enable cpu_get_ticks() */
508 void cpu_enable_ticks(void)
510 if (!cpu_ticks_enabled
) {
511 cpu_ticks_offset
-= cpu_get_real_ticks();
512 cpu_ticks_enabled
= 1;
516 /* disable cpu_get_ticks() : the clock is stopped. You must not call
517 cpu_get_ticks() after that. */
518 void cpu_disable_ticks(void)
520 if (cpu_ticks_enabled
) {
521 cpu_ticks_offset
= cpu_get_ticks();
522 cpu_ticks_enabled
= 0;
526 static int64_t get_clock(void)
531 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
534 gettimeofday(&tv
, NULL
);
535 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
539 void cpu_calibrate_ticks(void)
544 ticks
= cpu_get_real_ticks();
550 usec
= get_clock() - usec
;
551 ticks
= cpu_get_real_ticks() - ticks
;
552 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
555 /* compute with 96 bit intermediate result: (a*b)/c */
556 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
561 #ifdef WORDS_BIGENDIAN
571 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
572 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
575 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
579 #define QEMU_TIMER_REALTIME 0
580 #define QEMU_TIMER_VIRTUAL 1
584 /* XXX: add frequency */
592 struct QEMUTimer
*next
;
598 static QEMUTimer
*active_timers
[2];
600 static MMRESULT timerID
;
602 /* frequency of the times() clock tick */
603 static int timer_freq
;
606 QEMUClock
*qemu_new_clock(int type
)
609 clock
= qemu_mallocz(sizeof(QEMUClock
));
616 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
620 ts
= qemu_mallocz(sizeof(QEMUTimer
));
627 void qemu_free_timer(QEMUTimer
*ts
)
632 /* stop a timer, but do not dealloc it */
633 void qemu_del_timer(QEMUTimer
*ts
)
637 /* NOTE: this code must be signal safe because
638 qemu_timer_expired() can be called from a signal. */
639 pt
= &active_timers
[ts
->clock
->type
];
652 /* modify the current timer so that it will be fired when current_time
653 >= expire_time. The corresponding callback will be called. */
654 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
660 /* add the timer in the sorted list */
661 /* NOTE: this code must be signal safe because
662 qemu_timer_expired() can be called from a signal. */
663 pt
= &active_timers
[ts
->clock
->type
];
668 if (t
->expire_time
> expire_time
)
672 ts
->expire_time
= expire_time
;
677 int qemu_timer_pending(QEMUTimer
*ts
)
680 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
687 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
691 return (timer_head
->expire_time
<= current_time
);
694 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
700 if (ts
->expire_time
> current_time
)
702 /* remove timer from the list before calling the callback */
703 *ptimer_head
= ts
->next
;
706 /* run the callback (the timer list can be modified) */
711 int64_t qemu_get_clock(QEMUClock
*clock
)
713 switch(clock
->type
) {
714 case QEMU_TIMER_REALTIME
:
716 return GetTickCount();
721 /* Note that using gettimeofday() is not a good solution
722 for timers because its value change when the date is
724 if (timer_freq
== 100) {
725 return times(&tp
) * 10;
727 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
732 case QEMU_TIMER_VIRTUAL
:
733 return cpu_get_ticks();
738 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
740 uint64_t expire_time
;
742 if (qemu_timer_pending(ts
)) {
743 expire_time
= ts
->expire_time
;
747 qemu_put_be64(f
, expire_time
);
750 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
752 uint64_t expire_time
;
754 expire_time
= qemu_get_be64(f
);
755 if (expire_time
!= -1) {
756 qemu_mod_timer(ts
, expire_time
);
762 static void timer_save(QEMUFile
*f
, void *opaque
)
764 if (cpu_ticks_enabled
) {
765 hw_error("cannot save state if virtual timers are running");
767 qemu_put_be64s(f
, &cpu_ticks_offset
);
768 qemu_put_be64s(f
, &ticks_per_sec
);
771 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
775 if (cpu_ticks_enabled
) {
778 qemu_get_be64s(f
, &cpu_ticks_offset
);
779 qemu_get_be64s(f
, &ticks_per_sec
);
784 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
785 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
787 static void host_alarm_handler(int host_signum
)
791 #define DISP_FREQ 1000
793 static int64_t delta_min
= INT64_MAX
;
794 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
796 ti
= qemu_get_clock(vm_clock
);
797 if (last_clock
!= 0) {
798 delta
= ti
- last_clock
;
799 if (delta
< delta_min
)
801 if (delta
> delta_max
)
804 if (++count
== DISP_FREQ
) {
805 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
806 muldiv64(delta_min
, 1000000, ticks_per_sec
),
807 muldiv64(delta_max
, 1000000, ticks_per_sec
),
808 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
809 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
811 delta_min
= INT64_MAX
;
819 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
820 qemu_get_clock(vm_clock
)) ||
821 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
822 qemu_get_clock(rt_clock
))) {
823 /* stop the cpu because a timer occured */
824 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
830 #if defined(__linux__)
832 #define RTC_FREQ 1024
836 static int start_rtc_timer(void)
838 rtc_fd
= open("/dev/rtc", O_RDONLY
);
841 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
842 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
843 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
844 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
847 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
852 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
858 static int start_rtc_timer(void)
863 #endif /* !defined(__linux__) */
865 #endif /* !defined(_WIN32) */
867 static void init_timers(void)
869 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
870 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
875 timerID
= timeSetEvent(10, // interval (ms)
877 host_alarm_handler
, // function
878 (DWORD
)&count
, // user parameter
879 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
881 perror("failed timer alarm");
885 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
888 struct sigaction act
;
889 struct itimerval itv
;
891 /* get times() syscall frequency */
892 timer_freq
= sysconf(_SC_CLK_TCK
);
895 sigfillset(&act
.sa_mask
);
897 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
898 act
.sa_flags
|= SA_ONSTACK
;
900 act
.sa_handler
= host_alarm_handler
;
901 sigaction(SIGALRM
, &act
, NULL
);
903 itv
.it_interval
.tv_sec
= 0;
904 itv
.it_interval
.tv_usec
= 1000;
905 itv
.it_value
.tv_sec
= 0;
906 itv
.it_value
.tv_usec
= 10 * 1000;
907 setitimer(ITIMER_REAL
, &itv
, NULL
);
908 /* we probe the tick duration of the kernel to inform the user if
909 the emulated kernel requested a too high timer frequency */
910 getitimer(ITIMER_REAL
, &itv
);
912 if (itv
.it_interval
.tv_usec
> 1000) {
913 /* try to use /dev/rtc to have a faster timer */
914 if (start_rtc_timer() < 0)
917 itv
.it_interval
.tv_sec
= 0;
918 itv
.it_interval
.tv_usec
= 0;
919 itv
.it_value
.tv_sec
= 0;
920 itv
.it_value
.tv_usec
= 0;
921 setitimer(ITIMER_REAL
, &itv
, NULL
);
924 sigaction(SIGRTMIN
, &act
, NULL
);
925 fcntl(rtc_fd
, F_SETSIG
, SIGRTMIN
);
926 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
927 fcntl(rtc_fd
, F_SETOWN
, getpid());
930 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
937 void quit_timers(void)
940 timeKillEvent(timerID
);
944 /***********************************************************/
949 int serial_open_device(void)
956 int serial_open_device(void)
958 char slave_name
[1024];
959 int master_fd
, slave_fd
;
961 if (serial_console
== NULL
&& nographic
) {
962 /* use console for serial port */
967 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
968 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
971 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
981 /***********************************************************/
982 /* Linux network device redirectors */
984 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
988 for(i
=0;i
<size
;i
+=16) {
992 fprintf(f
, "%08x ", i
);
995 fprintf(f
, " %02x", buf
[i
+j
]);
1000 for(j
=0;j
<len
;j
++) {
1002 if (c
< ' ' || c
> '~')
1004 fprintf(f
, "%c", c
);
1010 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1012 nd
->send_packet(nd
, buf
, size
);
1015 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
1016 IOReadHandler
*fd_read
, void *opaque
)
1018 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
1021 /* dummy network adapter */
1023 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1027 static void dummy_add_read_packet(NetDriverState
*nd
,
1028 IOCanRWHandler
*fd_can_read
,
1029 IOReadHandler
*fd_read
, void *opaque
)
1033 static int net_dummy_init(NetDriverState
*nd
)
1035 nd
->send_packet
= dummy_send_packet
;
1036 nd
->add_read_packet
= dummy_add_read_packet
;
1037 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
1041 #if defined(CONFIG_SLIRP)
1043 /* slirp network adapter */
1045 static void *slirp_fd_opaque
;
1046 static IOCanRWHandler
*slirp_fd_can_read
;
1047 static IOReadHandler
*slirp_fd_read
;
1048 static int slirp_inited
;
1050 int slirp_can_output(void)
1052 return slirp_fd_can_read(slirp_fd_opaque
);
1055 void slirp_output(const uint8_t *pkt
, int pkt_len
)
1058 printf("output:\n");
1059 hex_dump(stdout
, pkt
, pkt_len
);
1061 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
1064 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1068 hex_dump(stdout
, buf
, size
);
1070 slirp_input(buf
, size
);
1073 static void slirp_add_read_packet(NetDriverState
*nd
,
1074 IOCanRWHandler
*fd_can_read
,
1075 IOReadHandler
*fd_read
, void *opaque
)
1077 slirp_fd_opaque
= opaque
;
1078 slirp_fd_can_read
= fd_can_read
;
1079 slirp_fd_read
= fd_read
;
1082 static int net_slirp_init(NetDriverState
*nd
)
1084 if (!slirp_inited
) {
1088 nd
->send_packet
= slirp_send_packet
;
1089 nd
->add_read_packet
= slirp_add_read_packet
;
1090 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1094 #endif /* CONFIG_SLIRP */
1096 #if !defined(_WIN32)
1098 static int tun_open(char *ifname
, int ifname_size
)
1104 fd
= open("/dev/tap", O_RDWR
);
1106 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1111 dev
= devname(s
.st_rdev
, S_IFCHR
);
1112 pstrcpy(ifname
, ifname_size
, dev
);
1114 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1118 static int tun_open(char *ifname
, int ifname_size
)
1123 fd
= open("/dev/net/tun", O_RDWR
);
1125 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1128 memset(&ifr
, 0, sizeof(ifr
));
1129 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1130 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1131 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1133 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1137 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1138 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1139 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1144 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1146 write(nd
->fd
, buf
, size
);
1149 static void tun_add_read_packet(NetDriverState
*nd
,
1150 IOCanRWHandler
*fd_can_read
,
1151 IOReadHandler
*fd_read
, void *opaque
)
1153 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1156 static int net_tun_init(NetDriverState
*nd
)
1162 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1166 /* try to launch network init script */
1171 *parg
++ = network_script
;
1172 *parg
++ = nd
->ifname
;
1174 execv(network_script
, args
);
1177 while (waitpid(pid
, &status
, 0) != pid
);
1178 if (!WIFEXITED(status
) ||
1179 WEXITSTATUS(status
) != 0) {
1180 fprintf(stderr
, "%s: could not launch network script\n",
1184 nd
->send_packet
= tun_send_packet
;
1185 nd
->add_read_packet
= tun_add_read_packet
;
1189 static int net_fd_init(NetDriverState
*nd
, int fd
)
1192 nd
->send_packet
= tun_send_packet
;
1193 nd
->add_read_packet
= tun_add_read_packet
;
1194 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1198 #endif /* !_WIN32 */
1200 /***********************************************************/
1205 static void term_exit(void)
1209 static void term_init(void)
1215 /* init terminal so that we can grab keys */
1216 static struct termios oldtty
;
1217 static int old_fd0_flags
;
1219 static void term_exit(void)
1221 tcsetattr (0, TCSANOW
, &oldtty
);
1222 fcntl(0, F_SETFL
, old_fd0_flags
);
1225 static void term_init(void)
1229 tcgetattr (0, &tty
);
1231 old_fd0_flags
= fcntl(0, F_GETFL
);
1233 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1234 |INLCR
|IGNCR
|ICRNL
|IXON
);
1235 tty
.c_oflag
|= OPOST
;
1236 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1237 /* if graphical mode, we allow Ctrl-C handling */
1239 tty
.c_lflag
&= ~ISIG
;
1240 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1243 tty
.c_cc
[VTIME
] = 0;
1245 tcsetattr (0, TCSANOW
, &tty
);
1249 fcntl(0, F_SETFL
, O_NONBLOCK
);
1254 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1258 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1262 static void dumb_refresh(DisplayState
*ds
)
1264 vga_update_display();
1267 void dumb_display_init(DisplayState
*ds
)
1272 ds
->dpy_update
= dumb_update
;
1273 ds
->dpy_resize
= dumb_resize
;
1274 ds
->dpy_refresh
= dumb_refresh
;
1277 #if !defined(CONFIG_SOFTMMU)
1278 /***********************************************************/
1279 /* cpu signal handler */
1280 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1283 if (cpu_signal_handler(host_signum
, info
, puc
))
1290 /***********************************************************/
1293 #define MAX_IO_HANDLERS 64
1295 typedef struct IOHandlerRecord
{
1297 IOCanRWHandler
*fd_can_read
;
1298 IOReadHandler
*fd_read
;
1300 /* temporary data */
1303 struct IOHandlerRecord
*next
;
1306 static IOHandlerRecord
*first_io_handler
;
1308 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1309 IOReadHandler
*fd_read
, void *opaque
)
1311 IOHandlerRecord
*ioh
;
1313 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1317 ioh
->fd_can_read
= fd_can_read
;
1318 ioh
->fd_read
= fd_read
;
1319 ioh
->opaque
= opaque
;
1320 ioh
->next
= first_io_handler
;
1321 first_io_handler
= ioh
;
1325 void qemu_del_fd_read_handler(int fd
)
1327 IOHandlerRecord
**pioh
, *ioh
;
1329 pioh
= &first_io_handler
;
1334 if (ioh
->fd
== fd
) {
1342 /***********************************************************/
1343 /* savevm/loadvm support */
1345 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1347 fwrite(buf
, 1, size
, f
);
1350 void qemu_put_byte(QEMUFile
*f
, int v
)
1355 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1357 qemu_put_byte(f
, v
>> 8);
1358 qemu_put_byte(f
, v
);
1361 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1363 qemu_put_byte(f
, v
>> 24);
1364 qemu_put_byte(f
, v
>> 16);
1365 qemu_put_byte(f
, v
>> 8);
1366 qemu_put_byte(f
, v
);
1369 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1371 qemu_put_be32(f
, v
>> 32);
1372 qemu_put_be32(f
, v
);
1375 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1377 return fread(buf
, 1, size
, f
);
1380 int qemu_get_byte(QEMUFile
*f
)
1390 unsigned int qemu_get_be16(QEMUFile
*f
)
1393 v
= qemu_get_byte(f
) << 8;
1394 v
|= qemu_get_byte(f
);
1398 unsigned int qemu_get_be32(QEMUFile
*f
)
1401 v
= qemu_get_byte(f
) << 24;
1402 v
|= qemu_get_byte(f
) << 16;
1403 v
|= qemu_get_byte(f
) << 8;
1404 v
|= qemu_get_byte(f
);
1408 uint64_t qemu_get_be64(QEMUFile
*f
)
1411 v
= (uint64_t)qemu_get_be32(f
) << 32;
1412 v
|= qemu_get_be32(f
);
1416 int64_t qemu_ftell(QEMUFile
*f
)
1421 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1423 if (fseek(f
, pos
, whence
) < 0)
1428 typedef struct SaveStateEntry
{
1432 SaveStateHandler
*save_state
;
1433 LoadStateHandler
*load_state
;
1435 struct SaveStateEntry
*next
;
1438 static SaveStateEntry
*first_se
;
1440 int register_savevm(const char *idstr
,
1443 SaveStateHandler
*save_state
,
1444 LoadStateHandler
*load_state
,
1447 SaveStateEntry
*se
, **pse
;
1449 se
= qemu_malloc(sizeof(SaveStateEntry
));
1452 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1453 se
->instance_id
= instance_id
;
1454 se
->version_id
= version_id
;
1455 se
->save_state
= save_state
;
1456 se
->load_state
= load_state
;
1457 se
->opaque
= opaque
;
1460 /* add at the end of list */
1462 while (*pse
!= NULL
)
1463 pse
= &(*pse
)->next
;
1468 #define QEMU_VM_FILE_MAGIC 0x5145564d
1469 #define QEMU_VM_FILE_VERSION 0x00000001
1471 int qemu_savevm(const char *filename
)
1475 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1477 saved_vm_running
= vm_running
;
1480 f
= fopen(filename
, "wb");
1486 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1487 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1489 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1491 len
= strlen(se
->idstr
);
1492 qemu_put_byte(f
, len
);
1493 qemu_put_buffer(f
, se
->idstr
, len
);
1495 qemu_put_be32(f
, se
->instance_id
);
1496 qemu_put_be32(f
, se
->version_id
);
1498 /* record size: filled later */
1500 qemu_put_be32(f
, 0);
1502 se
->save_state(f
, se
->opaque
);
1504 /* fill record size */
1506 len
= ftell(f
) - len_pos
- 4;
1507 fseek(f
, len_pos
, SEEK_SET
);
1508 qemu_put_be32(f
, len
);
1509 fseek(f
, cur_pos
, SEEK_SET
);
1515 if (saved_vm_running
)
1520 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1524 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1525 if (!strcmp(se
->idstr
, idstr
) &&
1526 instance_id
== se
->instance_id
)
1532 int qemu_loadvm(const char *filename
)
1536 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1537 int saved_vm_running
;
1541 saved_vm_running
= vm_running
;
1544 f
= fopen(filename
, "rb");
1550 v
= qemu_get_be32(f
);
1551 if (v
!= QEMU_VM_FILE_MAGIC
)
1553 v
= qemu_get_be32(f
);
1554 if (v
!= QEMU_VM_FILE_VERSION
) {
1561 #if defined (DO_TB_FLUSH)
1562 tb_flush(global_env
);
1564 len
= qemu_get_byte(f
);
1567 qemu_get_buffer(f
, idstr
, len
);
1569 instance_id
= qemu_get_be32(f
);
1570 version_id
= qemu_get_be32(f
);
1571 record_len
= qemu_get_be32(f
);
1573 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1574 idstr
, instance_id
, version_id
, record_len
);
1577 se
= find_se(idstr
, instance_id
);
1579 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1580 instance_id
, idstr
);
1582 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1584 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1585 instance_id
, idstr
);
1588 /* always seek to exact end of record */
1589 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1594 if (saved_vm_running
)
1599 /***********************************************************/
1600 /* cpu save/restore */
1602 #if defined(TARGET_I386)
1604 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1606 qemu_put_be32(f
, dt
->selector
);
1607 qemu_put_be32(f
, (uint32_t)dt
->base
);
1608 qemu_put_be32(f
, dt
->limit
);
1609 qemu_put_be32(f
, dt
->flags
);
1612 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1614 dt
->selector
= qemu_get_be32(f
);
1615 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1616 dt
->limit
= qemu_get_be32(f
);
1617 dt
->flags
= qemu_get_be32(f
);
1620 void cpu_save(QEMUFile
*f
, void *opaque
)
1622 CPUState
*env
= opaque
;
1623 uint16_t fptag
, fpus
, fpuc
;
1627 for(i
= 0; i
< 8; i
++)
1628 qemu_put_be32s(f
, &env
->regs
[i
]);
1629 qemu_put_be32s(f
, &env
->eip
);
1630 qemu_put_be32s(f
, &env
->eflags
);
1631 qemu_put_be32s(f
, &env
->eflags
);
1632 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1633 qemu_put_be32s(f
, &hflags
);
1637 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1639 for (i
=7; i
>=0; i
--) {
1641 if (env
->fptags
[i
]) {
1646 qemu_put_be16s(f
, &fpuc
);
1647 qemu_put_be16s(f
, &fpus
);
1648 qemu_put_be16s(f
, &fptag
);
1650 for(i
= 0; i
< 8; i
++) {
1653 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1654 qemu_put_be64(f
, mant
);
1655 qemu_put_be16(f
, exp
);
1658 for(i
= 0; i
< 6; i
++)
1659 cpu_put_seg(f
, &env
->segs
[i
]);
1660 cpu_put_seg(f
, &env
->ldt
);
1661 cpu_put_seg(f
, &env
->tr
);
1662 cpu_put_seg(f
, &env
->gdt
);
1663 cpu_put_seg(f
, &env
->idt
);
1665 qemu_put_be32s(f
, &env
->sysenter_cs
);
1666 qemu_put_be32s(f
, &env
->sysenter_esp
);
1667 qemu_put_be32s(f
, &env
->sysenter_eip
);
1669 qemu_put_be32s(f
, &env
->cr
[0]);
1670 qemu_put_be32s(f
, &env
->cr
[2]);
1671 qemu_put_be32s(f
, &env
->cr
[3]);
1672 qemu_put_be32s(f
, &env
->cr
[4]);
1674 for(i
= 0; i
< 8; i
++)
1675 qemu_put_be32s(f
, &env
->dr
[i
]);
1678 qemu_put_be32s(f
, &env
->a20_mask
);
1681 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1683 CPUState
*env
= opaque
;
1686 uint16_t fpus
, fpuc
, fptag
;
1688 if (version_id
!= 2)
1690 for(i
= 0; i
< 8; i
++)
1691 qemu_get_be32s(f
, &env
->regs
[i
]);
1692 qemu_get_be32s(f
, &env
->eip
);
1693 qemu_get_be32s(f
, &env
->eflags
);
1694 qemu_get_be32s(f
, &env
->eflags
);
1695 qemu_get_be32s(f
, &hflags
);
1697 qemu_get_be16s(f
, &fpuc
);
1698 qemu_get_be16s(f
, &fpus
);
1699 qemu_get_be16s(f
, &fptag
);
1701 for(i
= 0; i
< 8; i
++) {
1704 mant
= qemu_get_be64(f
);
1705 exp
= qemu_get_be16(f
);
1706 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1710 env
->fpstt
= (fpus
>> 11) & 7;
1711 env
->fpus
= fpus
& ~0x3800;
1712 for(i
= 0; i
< 8; i
++) {
1713 env
->fptags
[i
] = ((fptag
& 3) == 3);
1717 for(i
= 0; i
< 6; i
++)
1718 cpu_get_seg(f
, &env
->segs
[i
]);
1719 cpu_get_seg(f
, &env
->ldt
);
1720 cpu_get_seg(f
, &env
->tr
);
1721 cpu_get_seg(f
, &env
->gdt
);
1722 cpu_get_seg(f
, &env
->idt
);
1724 qemu_get_be32s(f
, &env
->sysenter_cs
);
1725 qemu_get_be32s(f
, &env
->sysenter_esp
);
1726 qemu_get_be32s(f
, &env
->sysenter_eip
);
1728 qemu_get_be32s(f
, &env
->cr
[0]);
1729 qemu_get_be32s(f
, &env
->cr
[2]);
1730 qemu_get_be32s(f
, &env
->cr
[3]);
1731 qemu_get_be32s(f
, &env
->cr
[4]);
1733 for(i
= 0; i
< 8; i
++)
1734 qemu_get_be32s(f
, &env
->dr
[i
]);
1737 qemu_get_be32s(f
, &env
->a20_mask
);
1739 /* XXX: compute hflags from scratch, except for CPL and IIF */
1740 env
->hflags
= hflags
;
1745 #elif defined(TARGET_PPC)
1746 void cpu_save(QEMUFile
*f
, void *opaque
)
1750 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1756 #warning No CPU save/restore functions
1760 /***********************************************************/
1761 /* ram save/restore */
1763 /* we just avoid storing empty pages */
1764 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1769 for(i
= 1; i
< len
; i
++) {
1773 qemu_put_byte(f
, 1);
1774 qemu_put_byte(f
, v
);
1777 qemu_put_byte(f
, 0);
1778 qemu_put_buffer(f
, buf
, len
);
1781 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1785 v
= qemu_get_byte(f
);
1788 if (qemu_get_buffer(f
, buf
, len
) != len
)
1792 v
= qemu_get_byte(f
);
1793 memset(buf
, v
, len
);
1801 static void ram_save(QEMUFile
*f
, void *opaque
)
1804 qemu_put_be32(f
, phys_ram_size
);
1805 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1806 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1810 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1814 if (version_id
!= 1)
1816 if (qemu_get_be32(f
) != phys_ram_size
)
1818 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1819 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1826 /***********************************************************/
1827 /* main execution loop */
1829 void gui_update(void *opaque
)
1831 display_state
.dpy_refresh(&display_state
);
1832 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1835 /* XXX: support several handlers */
1836 VMStopHandler
*vm_stop_cb
;
1837 VMStopHandler
*vm_stop_opaque
;
1839 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1842 vm_stop_opaque
= opaque
;
1846 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1859 void vm_stop(int reason
)
1862 cpu_disable_ticks();
1866 vm_stop_cb(vm_stop_opaque
, reason
);
1872 /* reset/shutdown handler */
1874 typedef struct QEMUResetEntry
{
1875 QEMUResetHandler
*func
;
1877 struct QEMUResetEntry
*next
;
1880 static QEMUResetEntry
*first_reset_entry
;
1881 static int reset_requested
;
1882 static int shutdown_requested
;
1884 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
1886 QEMUResetEntry
**pre
, *re
;
1888 pre
= &first_reset_entry
;
1889 while (*pre
!= NULL
)
1890 pre
= &(*pre
)->next
;
1891 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
1893 re
->opaque
= opaque
;
1898 void qemu_system_reset(void)
1902 /* reset all devices */
1903 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
1904 re
->func(re
->opaque
);
1908 void qemu_system_reset_request(void)
1910 reset_requested
= 1;
1911 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1914 void qemu_system_shutdown_request(void)
1916 shutdown_requested
= 1;
1917 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1920 static void main_cpu_reset(void *opaque
)
1923 CPUState
*env
= opaque
;
1931 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1932 IOHandlerRecord
*ioh
, *ioh_next
;
1937 CPUState
*env
= global_env
;
1941 ret
= cpu_exec(env
);
1942 if (shutdown_requested
) {
1943 ret
= EXCP_INTERRUPT
;
1946 if (reset_requested
) {
1947 reset_requested
= 0;
1948 qemu_system_reset();
1949 ret
= EXCP_INTERRUPT
;
1951 if (ret
== EXCP_DEBUG
) {
1952 vm_stop(EXCP_DEBUG
);
1954 /* if hlt instruction, we wait until the next IRQ */
1955 /* XXX: use timeout computed from timers */
1956 if (ret
== EXCP_HLT
)
1969 /* poll any events */
1970 /* XXX: separate device handlers from system ones */
1972 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1973 if (!ioh
->fd_can_read
) {
1976 pf
->events
= POLLIN
;
1980 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1982 if (max_size
> sizeof(buf
))
1983 max_size
= sizeof(buf
);
1985 pf
->events
= POLLIN
;
1992 ioh
->max_size
= max_size
;
1995 ret
= poll(ufds
, pf
- ufds
, timeout
);
1997 /* XXX: better handling of removal */
1998 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1999 ioh_next
= ioh
->next
;
2002 if (pf
->revents
& POLLIN
) {
2003 if (ioh
->max_size
== 0) {
2004 /* just a read event */
2005 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
2007 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
2009 ioh
->fd_read(ioh
->opaque
, buf
, n
);
2010 } else if (errno
!= EAGAIN
) {
2011 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
2019 #if defined(CONFIG_SLIRP)
2020 /* XXX: merge with poll() */
2022 fd_set rfds
, wfds
, xfds
;
2030 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2033 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2035 slirp_select_poll(&rfds
, &wfds
, &xfds
);
2043 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
2044 qemu_get_clock(vm_clock
));
2046 if (audio_enabled
) {
2047 /* XXX: add explicit timer */
2051 /* run dma transfers, if any */
2055 /* real time timers */
2056 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
2057 qemu_get_clock(rt_clock
));
2059 cpu_disable_ticks();
2065 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
2066 "usage: %s [options] [disk_image]\n"
2068 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2070 "Standard options:\n"
2071 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2072 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2073 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2074 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2075 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
2076 "-snapshot write to temporary files instead of disk image files\n"
2077 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2078 "-nographic disable graphical output and redirect serial I/Os to console\n"
2079 "-enable-audio enable audio support\n"
2080 "-localtime set the real time clock to local time [default=utc]\n"
2082 "-prep Simulate a PREP system (default is PowerMAC)\n"
2083 "-g WxH[xDEPTH] Set the initial VGA graphic mode\n"
2086 "Network options:\n"
2087 "-nics n simulate 'n' network cards [default=1]\n"
2088 "-macaddr addr set the mac address of the first interface\n"
2089 "-n script set tap/tun network init script [default=%s]\n"
2090 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2092 "-user-net use user mode network stack [default if no tap/tun script]\n"
2094 "-dummy-net use dummy network stack\n"
2096 "Linux boot specific:\n"
2097 "-kernel bzImage use 'bzImage' as kernel image\n"
2098 "-append cmdline use 'cmdline' as kernel command line\n"
2099 "-initrd file use 'file' as initial ram disk\n"
2101 "Debug/Expert options:\n"
2102 "-S freeze CPU at startup (use 'c' to start execution)\n"
2103 "-s wait gdb connection to port %d\n"
2104 "-p port change gdb connection port\n"
2105 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2106 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
2107 "-L path set the directory for the BIOS and VGA BIOS\n"
2108 #ifdef USE_CODE_COPY
2109 "-no-code-copy disable code copy acceleration\n"
2112 "-isa simulate an ISA-only system (default is PCI system)\n"
2115 "During emulation, use C-a h to get terminal commands:\n",
2116 #ifdef CONFIG_SOFTMMU
2122 DEFAULT_NETWORK_SCRIPT
,
2123 DEFAULT_GDBSTUB_PORT
,
2126 #ifndef CONFIG_SOFTMMU
2128 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2129 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2135 #define HAS_ARG 0x0001
2148 QEMU_OPTION_snapshot
,
2150 QEMU_OPTION_nographic
,
2151 QEMU_OPTION_enable_audio
,
2154 QEMU_OPTION_macaddr
,
2157 QEMU_OPTION_user_net
,
2158 QEMU_OPTION_dummy_net
,
2170 QEMU_OPTION_no_code_copy
,
2174 QEMU_OPTION_localtime
,
2175 QEMU_OPTION_cirrusvga
,
2179 typedef struct QEMUOption
{
2185 const QEMUOption qemu_options
[] = {
2186 { "h", 0, QEMU_OPTION_h
},
2188 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2189 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2190 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2191 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2192 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2193 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2194 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2195 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2196 { "snapshot", 0, QEMU_OPTION_snapshot
},
2197 { "m", HAS_ARG
, QEMU_OPTION_m
},
2198 { "nographic", 0, QEMU_OPTION_nographic
},
2199 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2201 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2202 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2203 { "n", HAS_ARG
, QEMU_OPTION_n
},
2204 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2206 { "user-net", 0, QEMU_OPTION_user_net
},
2208 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2210 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2211 { "append", HAS_ARG
, QEMU_OPTION_append
},
2212 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2214 { "S", 0, QEMU_OPTION_S
},
2215 { "s", 0, QEMU_OPTION_s
},
2216 { "p", HAS_ARG
, QEMU_OPTION_p
},
2217 { "d", HAS_ARG
, QEMU_OPTION_d
},
2218 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2219 { "L", HAS_ARG
, QEMU_OPTION_L
},
2220 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2222 { "prep", 0, QEMU_OPTION_prep
},
2223 { "g", 1, QEMU_OPTION_g
},
2225 { "localtime", 0, QEMU_OPTION_localtime
},
2226 { "isa", 0, QEMU_OPTION_isa
},
2228 /* temporary options */
2229 { "pci", 0, QEMU_OPTION_pci
},
2230 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
2234 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2236 /* this stack is only used during signal handling */
2237 #define SIGNAL_STACK_SIZE 32768
2239 static uint8_t *signal_stack
;
2243 #define NET_IF_TUN 0
2244 #define NET_IF_USER 1
2245 #define NET_IF_DUMMY 2
2247 int main(int argc
, char **argv
)
2249 #ifdef CONFIG_GDBSTUB
2250 int use_gdbstub
, gdbstub_port
;
2253 int snapshot
, linux_boot
;
2255 const char *initrd_filename
;
2256 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2257 const char *kernel_filename
, *kernel_cmdline
;
2258 DisplayState
*ds
= &display_state
;
2259 int cyls
, heads
, secs
;
2260 int start_emulation
= 1;
2262 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2264 const char *r
, *optarg
;
2266 #if !defined(CONFIG_SOFTMMU)
2267 /* we never want that malloc() uses mmap() */
2268 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2270 initrd_filename
= NULL
;
2271 for(i
= 0; i
< MAX_FD
; i
++)
2272 fd_filename
[i
] = NULL
;
2273 for(i
= 0; i
< MAX_DISKS
; i
++)
2274 hd_filename
[i
] = NULL
;
2275 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2276 vga_ram_size
= VGA_RAM_SIZE
;
2277 bios_size
= BIOS_SIZE
;
2278 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2279 #ifdef CONFIG_GDBSTUB
2281 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2285 kernel_filename
= NULL
;
2286 kernel_cmdline
= "";
2288 cyls
= heads
= secs
= 0;
2293 /* default mac address of the first network interface */
2307 hd_filename
[0] = argv
[optind
++];
2309 const QEMUOption
*popt
;
2312 popt
= qemu_options
;
2315 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2319 if (!strcmp(popt
->name
, r
+ 1))
2323 if (popt
->flags
& HAS_ARG
) {
2324 if (optind
>= argc
) {
2325 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2329 optarg
= argv
[optind
++];
2334 switch(popt
->index
) {
2335 case QEMU_OPTION_initrd
:
2336 initrd_filename
= optarg
;
2338 case QEMU_OPTION_hda
:
2339 hd_filename
[0] = optarg
;
2341 case QEMU_OPTION_hdb
:
2342 hd_filename
[1] = optarg
;
2344 case QEMU_OPTION_snapshot
:
2347 case QEMU_OPTION_hdachs
:
2351 cyls
= strtol(p
, (char **)&p
, 0);
2355 heads
= strtol(p
, (char **)&p
, 0);
2359 secs
= strtol(p
, (char **)&p
, 0);
2366 case QEMU_OPTION_nographic
:
2369 case QEMU_OPTION_kernel
:
2370 kernel_filename
= optarg
;
2372 case QEMU_OPTION_append
:
2373 kernel_cmdline
= optarg
;
2375 case QEMU_OPTION_tun_fd
:
2379 net_if_type
= NET_IF_TUN
;
2380 if (nb_tun_fds
< MAX_NICS
) {
2381 fd
= strtol(optarg
, (char **)&p
, 0);
2383 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2386 tun_fds
[nb_tun_fds
++] = fd
;
2390 case QEMU_OPTION_hdc
:
2391 hd_filename
[2] = optarg
;
2394 case QEMU_OPTION_hdd
:
2395 hd_filename
[3] = optarg
;
2397 case QEMU_OPTION_cdrom
:
2398 hd_filename
[2] = optarg
;
2401 case QEMU_OPTION_boot
:
2402 boot_device
= optarg
[0];
2403 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2404 boot_device
!= 'c' && boot_device
!= 'd') {
2405 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2409 case QEMU_OPTION_fda
:
2410 fd_filename
[0] = optarg
;
2412 case QEMU_OPTION_fdb
:
2413 fd_filename
[1] = optarg
;
2415 case QEMU_OPTION_no_code_copy
:
2416 code_copy_enabled
= 0;
2418 case QEMU_OPTION_nics
:
2419 nb_nics
= atoi(optarg
);
2420 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2421 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2425 case QEMU_OPTION_macaddr
:
2430 for(i
= 0; i
< 6; i
++) {
2431 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2438 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2446 case QEMU_OPTION_user_net
:
2447 net_if_type
= NET_IF_USER
;
2449 case QEMU_OPTION_dummy_net
:
2450 net_if_type
= NET_IF_DUMMY
;
2452 case QEMU_OPTION_enable_audio
:
2459 ram_size
= atoi(optarg
) * 1024 * 1024;
2462 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2463 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2464 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2473 mask
= cpu_str_to_log_mask(optarg
);
2475 printf("Log items (comma separated):\n");
2476 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2477 printf("%-10s %s\n", item
->name
, item
->help
);
2485 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2487 #ifdef CONFIG_GDBSTUB
2492 gdbstub_port
= atoi(optarg
);
2499 start_emulation
= 0;
2501 case QEMU_OPTION_pci
:
2504 case QEMU_OPTION_isa
:
2507 case QEMU_OPTION_prep
:
2510 case QEMU_OPTION_localtime
:
2513 case QEMU_OPTION_cirrusvga
:
2514 cirrus_vga_enabled
= 1;
2521 w
= strtol(p
, (char **)&p
, 10);
2524 fprintf(stderr
, "qemu: invalid resolution or depth\n");
2530 h
= strtol(p
, (char **)&p
, 10);
2535 depth
= strtol(p
, (char **)&p
, 10);
2536 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
2537 depth
!= 24 && depth
!= 32)
2539 } else if (*p
== '\0') {
2540 depth
= graphic_depth
;
2547 graphic_depth
= depth
;
2554 linux_boot
= (kernel_filename
!= NULL
);
2556 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2557 fd_filename
[0] == '\0')
2560 /* boot to cd by default if no hard disk */
2561 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2562 if (fd_filename
[0] != '\0')
2568 #if !defined(CONFIG_SOFTMMU)
2569 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2571 static uint8_t stdout_buf
[4096];
2572 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2575 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2578 /* init host network redirectors */
2579 if (net_if_type
== -1) {
2580 net_if_type
= NET_IF_TUN
;
2581 #if defined(CONFIG_SLIRP)
2582 if (access(network_script
, R_OK
) < 0) {
2583 net_if_type
= NET_IF_USER
;
2588 for(i
= 0; i
< nb_nics
; i
++) {
2589 NetDriverState
*nd
= &nd_table
[i
];
2591 /* init virtual mac address */
2592 nd
->macaddr
[0] = macaddr
[0];
2593 nd
->macaddr
[1] = macaddr
[1];
2594 nd
->macaddr
[2] = macaddr
[2];
2595 nd
->macaddr
[3] = macaddr
[3];
2596 nd
->macaddr
[4] = macaddr
[4];
2597 nd
->macaddr
[5] = macaddr
[5] + i
;
2598 switch(net_if_type
) {
2599 #if defined(CONFIG_SLIRP)
2604 #if !defined(_WIN32)
2606 if (i
< nb_tun_fds
) {
2607 net_fd_init(nd
, tun_fds
[i
]);
2609 if (net_tun_init(nd
) < 0)
2621 /* init the memory */
2622 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
2624 #ifdef CONFIG_SOFTMMU
2626 /* mallocs are always aligned on BSD. */
2627 phys_ram_base
= malloc(phys_ram_size
);
2629 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2631 if (!phys_ram_base
) {
2632 fprintf(stderr
, "Could not allocate physical memory\n");
2636 /* as we must map the same page at several addresses, we must use
2641 tmpdir
= getenv("QEMU_TMPDIR");
2644 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2645 if (mkstemp(phys_ram_file
) < 0) {
2646 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2650 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2651 if (phys_ram_fd
< 0) {
2652 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2656 ftruncate(phys_ram_fd
, phys_ram_size
);
2657 unlink(phys_ram_file
);
2658 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2660 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2662 if (phys_ram_base
== MAP_FAILED
) {
2663 fprintf(stderr
, "Could not map physical memory\n");
2669 /* we always create the cdrom drive, even if no disk is there */
2671 bs_table
[2] = bdrv_new("cdrom");
2672 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2675 /* open the virtual block devices */
2676 for(i
= 0; i
< MAX_DISKS
; i
++) {
2677 if (hd_filename
[i
]) {
2680 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2681 bs_table
[i
] = bdrv_new(buf
);
2683 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2684 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2688 if (i
== 0 && cyls
!= 0)
2689 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2693 /* we always create at least one floppy disk */
2694 fd_table
[0] = bdrv_new("fda");
2695 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2697 for(i
= 0; i
< MAX_FD
; i
++) {
2698 if (fd_filename
[i
]) {
2701 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2702 fd_table
[i
] = bdrv_new(buf
);
2703 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2705 if (fd_filename
[i
] != '\0') {
2706 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2707 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2715 /* init CPU state */
2718 cpu_single_env
= env
;
2720 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2721 register_savevm("cpu", 0, 2, cpu_save
, cpu_load
, env
);
2722 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2723 qemu_register_reset(main_cpu_reset
, global_env
);
2726 cpu_calibrate_ticks();
2730 dumb_display_init(ds
);
2733 sdl_display_init(ds
);
2735 dumb_display_init(ds
);
2739 /* setup cpu signal handlers for MMU / self modifying code handling */
2740 #if !defined(CONFIG_SOFTMMU)
2742 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2745 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2746 stk
.ss_sp
= signal_stack
;
2747 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2750 if (sigaltstack(&stk
, NULL
) < 0) {
2751 perror("sigaltstack");
2757 struct sigaction act
;
2759 sigfillset(&act
.sa_mask
);
2760 act
.sa_flags
= SA_SIGINFO
;
2761 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2762 act
.sa_flags
|= SA_ONSTACK
;
2764 act
.sa_sigaction
= host_segv_handler
;
2765 sigaction(SIGSEGV
, &act
, NULL
);
2766 sigaction(SIGBUS
, &act
, NULL
);
2767 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2768 sigaction(SIGFPE
, &act
, NULL
);
2775 struct sigaction act
;
2776 sigfillset(&act
.sa_mask
);
2778 act
.sa_handler
= SIG_IGN
;
2779 sigaction(SIGPIPE
, &act
, NULL
);
2784 #if defined(TARGET_I386)
2785 pc_init(ram_size
, vga_ram_size
, boot_device
,
2786 ds
, fd_filename
, snapshot
,
2787 kernel_filename
, kernel_cmdline
, initrd_filename
);
2788 #elif defined(TARGET_PPC)
2789 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2790 ds
, fd_filename
, snapshot
,
2791 kernel_filename
, kernel_cmdline
, initrd_filename
);
2794 /* launched after the device init so that it can display or not a
2798 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2799 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2801 #ifdef CONFIG_GDBSTUB
2803 if (gdbserver_start(gdbstub_port
) < 0) {
2804 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2808 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2812 if (start_emulation
)