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 /***********************************************************/
134 /* x86 ISA bus support */
136 target_phys_addr_t isa_mem_base
= 0;
138 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
140 #ifdef DEBUG_UNUSED_IOPORT
141 fprintf(stderr
, "inb: port=0x%04x\n", address
);
146 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
148 #ifdef DEBUG_UNUSED_IOPORT
149 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
153 /* default is to make two byte accesses */
154 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
157 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
158 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
159 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
163 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
165 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
166 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
167 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
170 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
172 #ifdef DEBUG_UNUSED_IOPORT
173 fprintf(stderr
, "inl: port=0x%04x\n", address
);
178 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
180 #ifdef DEBUG_UNUSED_IOPORT
181 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
185 void init_ioports(void)
189 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
190 ioport_read_table
[0][i
] = default_ioport_readb
;
191 ioport_write_table
[0][i
] = default_ioport_writeb
;
192 ioport_read_table
[1][i
] = default_ioport_readw
;
193 ioport_write_table
[1][i
] = default_ioport_writew
;
194 ioport_read_table
[2][i
] = default_ioport_readl
;
195 ioport_write_table
[2][i
] = default_ioport_writel
;
199 /* size is the word size in byte */
200 int register_ioport_read(int start
, int length
, int size
,
201 IOPortReadFunc
*func
, void *opaque
)
207 } else if (size
== 2) {
209 } else if (size
== 4) {
212 hw_error("register_ioport_read: invalid size");
215 for(i
= start
; i
< start
+ length
; i
+= size
) {
216 ioport_read_table
[bsize
][i
] = func
;
217 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
218 hw_error("register_ioport_read: invalid opaque");
219 ioport_opaque
[i
] = opaque
;
224 /* size is the word size in byte */
225 int register_ioport_write(int start
, int length
, int size
,
226 IOPortWriteFunc
*func
, void *opaque
)
232 } else if (size
== 2) {
234 } else if (size
== 4) {
237 hw_error("register_ioport_write: invalid size");
240 for(i
= start
; i
< start
+ length
; i
+= size
) {
241 ioport_write_table
[bsize
][i
] = func
;
242 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
243 hw_error("register_ioport_read: invalid opaque");
244 ioport_opaque
[i
] = opaque
;
249 void isa_unassign_ioport(int start
, int length
)
253 for(i
= start
; i
< start
+ length
; i
++) {
254 ioport_read_table
[0][i
] = default_ioport_readb
;
255 ioport_read_table
[1][i
] = default_ioport_readw
;
256 ioport_read_table
[2][i
] = default_ioport_readl
;
258 ioport_write_table
[0][i
] = default_ioport_writeb
;
259 ioport_write_table
[1][i
] = default_ioport_writew
;
260 ioport_write_table
[2][i
] = default_ioport_writel
;
264 void pstrcpy(char *buf
, int buf_size
, const char *str
)
274 if (c
== 0 || q
>= buf
+ buf_size
- 1)
281 /* strcat and truncate. */
282 char *pstrcat(char *buf
, int buf_size
, const char *s
)
287 pstrcpy(buf
+ len
, buf_size
- len
, s
);
291 /* return the size or -1 if error */
292 int load_image(const char *filename
, uint8_t *addr
)
295 fd
= open(filename
, O_RDONLY
| O_BINARY
);
298 size
= lseek(fd
, 0, SEEK_END
);
299 lseek(fd
, 0, SEEK_SET
);
300 if (read(fd
, addr
, size
) != size
) {
308 void cpu_outb(CPUState
*env
, int addr
, int val
)
311 if (loglevel
& CPU_LOG_IOPORT
)
312 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
314 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
317 void cpu_outw(CPUState
*env
, int addr
, int val
)
320 if (loglevel
& CPU_LOG_IOPORT
)
321 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
323 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
326 void cpu_outl(CPUState
*env
, int addr
, int val
)
329 if (loglevel
& CPU_LOG_IOPORT
)
330 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
332 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
335 int cpu_inb(CPUState
*env
, int addr
)
338 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
340 if (loglevel
& CPU_LOG_IOPORT
)
341 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
346 int cpu_inw(CPUState
*env
, int addr
)
349 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
351 if (loglevel
& CPU_LOG_IOPORT
)
352 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
357 int cpu_inl(CPUState
*env
, int addr
)
360 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
362 if (loglevel
& CPU_LOG_IOPORT
)
363 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
368 /***********************************************************/
369 void hw_error(const char *fmt
, ...)
374 fprintf(stderr
, "qemu: hardware error: ");
375 vfprintf(stderr
, fmt
, ap
);
376 fprintf(stderr
, "\n");
378 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
380 cpu_dump_state(global_env
, stderr
, 0);
386 /***********************************************************/
389 #if defined(__powerpc__)
391 static inline uint32_t get_tbl(void)
394 asm volatile("mftb %0" : "=r" (tbl
));
398 static inline uint32_t get_tbu(void)
401 asm volatile("mftbu %0" : "=r" (tbl
));
405 int64_t cpu_get_real_ticks(void)
408 /* NOTE: we test if wrapping has occurred */
414 return ((int64_t)h
<< 32) | l
;
417 #elif defined(__i386__)
419 int64_t cpu_get_real_ticks(void)
422 asm volatile ("rdtsc" : "=A" (val
));
426 #elif defined(__x86_64__)
428 int64_t cpu_get_real_ticks(void)
432 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
440 #error unsupported CPU
443 static int64_t cpu_ticks_offset
;
444 static int cpu_ticks_enabled
;
446 static inline int64_t cpu_get_ticks(void)
448 if (!cpu_ticks_enabled
) {
449 return cpu_ticks_offset
;
451 return cpu_get_real_ticks() + cpu_ticks_offset
;
455 /* enable cpu_get_ticks() */
456 void cpu_enable_ticks(void)
458 if (!cpu_ticks_enabled
) {
459 cpu_ticks_offset
-= cpu_get_real_ticks();
460 cpu_ticks_enabled
= 1;
464 /* disable cpu_get_ticks() : the clock is stopped. You must not call
465 cpu_get_ticks() after that. */
466 void cpu_disable_ticks(void)
468 if (cpu_ticks_enabled
) {
469 cpu_ticks_offset
= cpu_get_ticks();
470 cpu_ticks_enabled
= 0;
474 static int64_t get_clock(void)
479 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
482 gettimeofday(&tv
, NULL
);
483 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
487 void cpu_calibrate_ticks(void)
492 ticks
= cpu_get_real_ticks();
498 usec
= get_clock() - usec
;
499 ticks
= cpu_get_real_ticks() - ticks
;
500 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
503 /* compute with 96 bit intermediate result: (a*b)/c */
504 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
509 #ifdef WORDS_BIGENDIAN
519 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
520 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
523 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
527 #define QEMU_TIMER_REALTIME 0
528 #define QEMU_TIMER_VIRTUAL 1
532 /* XXX: add frequency */
540 struct QEMUTimer
*next
;
546 static QEMUTimer
*active_timers
[2];
548 static MMRESULT timerID
;
550 /* frequency of the times() clock tick */
551 static int timer_freq
;
554 QEMUClock
*qemu_new_clock(int type
)
557 clock
= qemu_mallocz(sizeof(QEMUClock
));
564 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
568 ts
= qemu_mallocz(sizeof(QEMUTimer
));
575 void qemu_free_timer(QEMUTimer
*ts
)
580 /* stop a timer, but do not dealloc it */
581 void qemu_del_timer(QEMUTimer
*ts
)
585 /* NOTE: this code must be signal safe because
586 qemu_timer_expired() can be called from a signal. */
587 pt
= &active_timers
[ts
->clock
->type
];
600 /* modify the current timer so that it will be fired when current_time
601 >= expire_time. The corresponding callback will be called. */
602 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
608 /* add the timer in the sorted list */
609 /* NOTE: this code must be signal safe because
610 qemu_timer_expired() can be called from a signal. */
611 pt
= &active_timers
[ts
->clock
->type
];
616 if (t
->expire_time
> expire_time
)
620 ts
->expire_time
= expire_time
;
625 int qemu_timer_pending(QEMUTimer
*ts
)
628 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
635 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
639 return (timer_head
->expire_time
<= current_time
);
642 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
648 if (ts
->expire_time
> current_time
)
650 /* remove timer from the list before calling the callback */
651 *ptimer_head
= ts
->next
;
654 /* run the callback (the timer list can be modified) */
659 int64_t qemu_get_clock(QEMUClock
*clock
)
661 switch(clock
->type
) {
662 case QEMU_TIMER_REALTIME
:
664 return GetTickCount();
669 /* Note that using gettimeofday() is not a good solution
670 for timers because its value change when the date is
672 if (timer_freq
== 100) {
673 return times(&tp
) * 10;
675 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
680 case QEMU_TIMER_VIRTUAL
:
681 return cpu_get_ticks();
686 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
688 uint64_t expire_time
;
690 if (qemu_timer_pending(ts
)) {
691 expire_time
= ts
->expire_time
;
695 qemu_put_be64(f
, expire_time
);
698 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
700 uint64_t expire_time
;
702 expire_time
= qemu_get_be64(f
);
703 if (expire_time
!= -1) {
704 qemu_mod_timer(ts
, expire_time
);
710 static void timer_save(QEMUFile
*f
, void *opaque
)
712 if (cpu_ticks_enabled
) {
713 hw_error("cannot save state if virtual timers are running");
715 qemu_put_be64s(f
, &cpu_ticks_offset
);
716 qemu_put_be64s(f
, &ticks_per_sec
);
719 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
723 if (cpu_ticks_enabled
) {
726 qemu_get_be64s(f
, &cpu_ticks_offset
);
727 qemu_get_be64s(f
, &ticks_per_sec
);
732 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
733 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
735 static void host_alarm_handler(int host_signum
)
738 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
739 qemu_get_clock(vm_clock
)) ||
740 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
741 qemu_get_clock(rt_clock
))) {
742 /* stop the cpu because a timer occured */
743 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
749 #if defined(__linux__)
751 #define RTC_FREQ 1024
755 static int start_rtc_timer(void)
757 rtc_fd
= open("/dev/rtc", O_RDONLY
);
760 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
761 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
762 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
763 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
766 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
771 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
777 static int start_rtc_timer(void)
782 #endif /* !defined(__linux__) */
784 #endif /* !defined(_WIN32) */
786 static void init_timers(void)
788 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
789 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
794 timerID
= timeSetEvent(10, // interval (ms)
796 host_alarm_handler
, // function
797 (DWORD
)&count
, // user parameter
798 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
800 perror("failed timer alarm");
804 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
807 struct sigaction act
;
808 struct itimerval itv
;
810 /* get times() syscall frequency */
811 timer_freq
= sysconf(_SC_CLK_TCK
);
814 sigfillset(&act
.sa_mask
);
816 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
817 act
.sa_flags
|= SA_ONSTACK
;
819 act
.sa_handler
= host_alarm_handler
;
820 sigaction(SIGALRM
, &act
, NULL
);
822 itv
.it_interval
.tv_sec
= 0;
823 itv
.it_interval
.tv_usec
= 1000;
824 itv
.it_value
.tv_sec
= 0;
825 itv
.it_value
.tv_usec
= 10 * 1000;
826 setitimer(ITIMER_REAL
, &itv
, NULL
);
827 /* we probe the tick duration of the kernel to inform the user if
828 the emulated kernel requested a too high timer frequency */
829 getitimer(ITIMER_REAL
, &itv
);
831 if (itv
.it_interval
.tv_usec
> 1000) {
832 /* try to use /dev/rtc to have a faster timer */
833 if (start_rtc_timer() < 0)
836 itv
.it_interval
.tv_sec
= 0;
837 itv
.it_interval
.tv_usec
= 0;
838 itv
.it_value
.tv_sec
= 0;
839 itv
.it_value
.tv_usec
= 0;
840 setitimer(ITIMER_REAL
, &itv
, NULL
);
843 sigaction(SIGIO
, &act
, NULL
);
844 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
845 fcntl(rtc_fd
, F_SETOWN
, getpid());
848 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
855 void quit_timers(void)
858 timeKillEvent(timerID
);
862 /***********************************************************/
867 int serial_open_device(void)
874 int serial_open_device(void)
876 char slave_name
[1024];
877 int master_fd
, slave_fd
;
879 if (serial_console
== NULL
&& nographic
) {
880 /* use console for serial port */
885 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
886 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
889 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
899 /***********************************************************/
900 /* Linux network device redirectors */
902 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
906 for(i
=0;i
<size
;i
+=16) {
910 fprintf(f
, "%08x ", i
);
913 fprintf(f
, " %02x", buf
[i
+j
]);
920 if (c
< ' ' || c
> '~')
928 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
930 nd
->send_packet(nd
, buf
, size
);
933 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
934 IOReadHandler
*fd_read
, void *opaque
)
936 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
939 /* dummy network adapter */
941 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
945 static void dummy_add_read_packet(NetDriverState
*nd
,
946 IOCanRWHandler
*fd_can_read
,
947 IOReadHandler
*fd_read
, void *opaque
)
951 static int net_dummy_init(NetDriverState
*nd
)
953 nd
->send_packet
= dummy_send_packet
;
954 nd
->add_read_packet
= dummy_add_read_packet
;
955 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
959 #if defined(CONFIG_SLIRP)
961 /* slirp network adapter */
963 static void *slirp_fd_opaque
;
964 static IOCanRWHandler
*slirp_fd_can_read
;
965 static IOReadHandler
*slirp_fd_read
;
966 static int slirp_inited
;
968 int slirp_can_output(void)
970 return slirp_fd_can_read(slirp_fd_opaque
);
973 void slirp_output(const uint8_t *pkt
, int pkt_len
)
977 hex_dump(stdout
, pkt
, pkt_len
);
979 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
982 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
986 hex_dump(stdout
, buf
, size
);
988 slirp_input(buf
, size
);
991 static void slirp_add_read_packet(NetDriverState
*nd
,
992 IOCanRWHandler
*fd_can_read
,
993 IOReadHandler
*fd_read
, void *opaque
)
995 slirp_fd_opaque
= opaque
;
996 slirp_fd_can_read
= fd_can_read
;
997 slirp_fd_read
= fd_read
;
1000 static int net_slirp_init(NetDriverState
*nd
)
1002 if (!slirp_inited
) {
1006 nd
->send_packet
= slirp_send_packet
;
1007 nd
->add_read_packet
= slirp_add_read_packet
;
1008 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1012 #endif /* CONFIG_SLIRP */
1014 #if !defined(_WIN32)
1016 static int tun_open(char *ifname
, int ifname_size
)
1022 fd
= open("/dev/tap", O_RDWR
);
1024 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1029 dev
= devname(s
.st_rdev
, S_IFCHR
);
1030 pstrcpy(ifname
, ifname_size
, dev
);
1032 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1036 static int tun_open(char *ifname
, int ifname_size
)
1041 fd
= open("/dev/net/tun", O_RDWR
);
1043 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1046 memset(&ifr
, 0, sizeof(ifr
));
1047 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1048 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1049 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1051 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1055 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1056 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1057 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1062 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1064 write(nd
->fd
, buf
, size
);
1067 static void tun_add_read_packet(NetDriverState
*nd
,
1068 IOCanRWHandler
*fd_can_read
,
1069 IOReadHandler
*fd_read
, void *opaque
)
1071 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1074 static int net_tun_init(NetDriverState
*nd
)
1080 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1084 /* try to launch network init script */
1089 *parg
++ = network_script
;
1090 *parg
++ = nd
->ifname
;
1092 execv(network_script
, args
);
1095 while (waitpid(pid
, &status
, 0) != pid
);
1096 if (!WIFEXITED(status
) ||
1097 WEXITSTATUS(status
) != 0) {
1098 fprintf(stderr
, "%s: could not launch network script\n",
1102 nd
->send_packet
= tun_send_packet
;
1103 nd
->add_read_packet
= tun_add_read_packet
;
1107 static int net_fd_init(NetDriverState
*nd
, int fd
)
1110 nd
->send_packet
= tun_send_packet
;
1111 nd
->add_read_packet
= tun_add_read_packet
;
1112 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1116 #endif /* !_WIN32 */
1118 /***********************************************************/
1123 static void term_exit(void)
1127 static void term_init(void)
1133 /* init terminal so that we can grab keys */
1134 static struct termios oldtty
;
1136 static void term_exit(void)
1138 tcsetattr (0, TCSANOW
, &oldtty
);
1141 static void term_init(void)
1145 tcgetattr (0, &tty
);
1148 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1149 |INLCR
|IGNCR
|ICRNL
|IXON
);
1150 tty
.c_oflag
|= OPOST
;
1151 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1152 /* if graphical mode, we allow Ctrl-C handling */
1154 tty
.c_lflag
&= ~ISIG
;
1155 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1158 tty
.c_cc
[VTIME
] = 0;
1160 tcsetattr (0, TCSANOW
, &tty
);
1164 fcntl(0, F_SETFL
, O_NONBLOCK
);
1169 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1173 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1177 static void dumb_refresh(DisplayState
*ds
)
1179 vga_update_display();
1182 void dumb_display_init(DisplayState
*ds
)
1187 ds
->dpy_update
= dumb_update
;
1188 ds
->dpy_resize
= dumb_resize
;
1189 ds
->dpy_refresh
= dumb_refresh
;
1192 #if !defined(CONFIG_SOFTMMU)
1193 /***********************************************************/
1194 /* cpu signal handler */
1195 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1198 if (cpu_signal_handler(host_signum
, info
, puc
))
1205 /***********************************************************/
1208 #define MAX_IO_HANDLERS 64
1210 typedef struct IOHandlerRecord
{
1212 IOCanRWHandler
*fd_can_read
;
1213 IOReadHandler
*fd_read
;
1215 /* temporary data */
1218 struct IOHandlerRecord
*next
;
1221 static IOHandlerRecord
*first_io_handler
;
1223 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1224 IOReadHandler
*fd_read
, void *opaque
)
1226 IOHandlerRecord
*ioh
;
1228 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1232 ioh
->fd_can_read
= fd_can_read
;
1233 ioh
->fd_read
= fd_read
;
1234 ioh
->opaque
= opaque
;
1235 ioh
->next
= first_io_handler
;
1236 first_io_handler
= ioh
;
1240 void qemu_del_fd_read_handler(int fd
)
1242 IOHandlerRecord
**pioh
, *ioh
;
1244 pioh
= &first_io_handler
;
1249 if (ioh
->fd
== fd
) {
1257 /***********************************************************/
1258 /* savevm/loadvm support */
1260 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1262 fwrite(buf
, 1, size
, f
);
1265 void qemu_put_byte(QEMUFile
*f
, int v
)
1270 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1272 qemu_put_byte(f
, v
>> 8);
1273 qemu_put_byte(f
, v
);
1276 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1278 qemu_put_byte(f
, v
>> 24);
1279 qemu_put_byte(f
, v
>> 16);
1280 qemu_put_byte(f
, v
>> 8);
1281 qemu_put_byte(f
, v
);
1284 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1286 qemu_put_be32(f
, v
>> 32);
1287 qemu_put_be32(f
, v
);
1290 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1292 return fread(buf
, 1, size
, f
);
1295 int qemu_get_byte(QEMUFile
*f
)
1305 unsigned int qemu_get_be16(QEMUFile
*f
)
1308 v
= qemu_get_byte(f
) << 8;
1309 v
|= qemu_get_byte(f
);
1313 unsigned int qemu_get_be32(QEMUFile
*f
)
1316 v
= qemu_get_byte(f
) << 24;
1317 v
|= qemu_get_byte(f
) << 16;
1318 v
|= qemu_get_byte(f
) << 8;
1319 v
|= qemu_get_byte(f
);
1323 uint64_t qemu_get_be64(QEMUFile
*f
)
1326 v
= (uint64_t)qemu_get_be32(f
) << 32;
1327 v
|= qemu_get_be32(f
);
1331 int64_t qemu_ftell(QEMUFile
*f
)
1336 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1338 if (fseek(f
, pos
, whence
) < 0)
1343 typedef struct SaveStateEntry
{
1347 SaveStateHandler
*save_state
;
1348 LoadStateHandler
*load_state
;
1350 struct SaveStateEntry
*next
;
1353 static SaveStateEntry
*first_se
;
1355 int register_savevm(const char *idstr
,
1358 SaveStateHandler
*save_state
,
1359 LoadStateHandler
*load_state
,
1362 SaveStateEntry
*se
, **pse
;
1364 se
= qemu_malloc(sizeof(SaveStateEntry
));
1367 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1368 se
->instance_id
= instance_id
;
1369 se
->version_id
= version_id
;
1370 se
->save_state
= save_state
;
1371 se
->load_state
= load_state
;
1372 se
->opaque
= opaque
;
1375 /* add at the end of list */
1377 while (*pse
!= NULL
)
1378 pse
= &(*pse
)->next
;
1383 #define QEMU_VM_FILE_MAGIC 0x5145564d
1384 #define QEMU_VM_FILE_VERSION 0x00000001
1386 int qemu_savevm(const char *filename
)
1390 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1392 saved_vm_running
= vm_running
;
1395 f
= fopen(filename
, "wb");
1401 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1402 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1404 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1406 len
= strlen(se
->idstr
);
1407 qemu_put_byte(f
, len
);
1408 qemu_put_buffer(f
, se
->idstr
, len
);
1410 qemu_put_be32(f
, se
->instance_id
);
1411 qemu_put_be32(f
, se
->version_id
);
1413 /* record size: filled later */
1415 qemu_put_be32(f
, 0);
1417 se
->save_state(f
, se
->opaque
);
1419 /* fill record size */
1421 len
= ftell(f
) - len_pos
- 4;
1422 fseek(f
, len_pos
, SEEK_SET
);
1423 qemu_put_be32(f
, len
);
1424 fseek(f
, cur_pos
, SEEK_SET
);
1430 if (saved_vm_running
)
1435 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1439 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1440 if (!strcmp(se
->idstr
, idstr
) &&
1441 instance_id
== se
->instance_id
)
1447 int qemu_loadvm(const char *filename
)
1451 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1452 int saved_vm_running
;
1456 saved_vm_running
= vm_running
;
1459 f
= fopen(filename
, "rb");
1465 v
= qemu_get_be32(f
);
1466 if (v
!= QEMU_VM_FILE_MAGIC
)
1468 v
= qemu_get_be32(f
);
1469 if (v
!= QEMU_VM_FILE_VERSION
) {
1476 #if defined (DO_TB_FLUSH)
1477 tb_flush(global_env
);
1479 len
= qemu_get_byte(f
);
1482 qemu_get_buffer(f
, idstr
, len
);
1484 instance_id
= qemu_get_be32(f
);
1485 version_id
= qemu_get_be32(f
);
1486 record_len
= qemu_get_be32(f
);
1488 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1489 idstr
, instance_id
, version_id
, record_len
);
1492 se
= find_se(idstr
, instance_id
);
1494 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1495 instance_id
, idstr
);
1497 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1499 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1500 instance_id
, idstr
);
1503 /* always seek to exact end of record */
1504 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1509 if (saved_vm_running
)
1514 /***********************************************************/
1515 /* cpu save/restore */
1517 #if defined(TARGET_I386)
1519 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1521 qemu_put_be32(f
, (uint32_t)dt
->base
);
1522 qemu_put_be32(f
, dt
->limit
);
1523 qemu_put_be32(f
, dt
->flags
);
1526 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1528 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1529 dt
->limit
= qemu_get_be32(f
);
1530 dt
->flags
= qemu_get_be32(f
);
1533 void cpu_save(QEMUFile
*f
, void *opaque
)
1535 CPUState
*env
= opaque
;
1536 uint16_t fptag
, fpus
, fpuc
;
1540 for(i
= 0; i
< 8; i
++)
1541 qemu_put_be32s(f
, &env
->regs
[i
]);
1542 qemu_put_be32s(f
, &env
->eip
);
1543 qemu_put_be32s(f
, &env
->eflags
);
1544 qemu_put_be32s(f
, &env
->eflags
);
1545 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1546 qemu_put_be32s(f
, &hflags
);
1550 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1552 for (i
=7; i
>=0; i
--) {
1554 if (env
->fptags
[i
]) {
1559 qemu_put_be16s(f
, &fpuc
);
1560 qemu_put_be16s(f
, &fpus
);
1561 qemu_put_be16s(f
, &fptag
);
1563 for(i
= 0; i
< 8; i
++) {
1566 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1567 qemu_put_be64(f
, mant
);
1568 qemu_put_be16(f
, exp
);
1571 for(i
= 0; i
< 6; i
++)
1572 cpu_put_seg(f
, &env
->segs
[i
]);
1573 cpu_put_seg(f
, &env
->ldt
);
1574 cpu_put_seg(f
, &env
->tr
);
1575 cpu_put_seg(f
, &env
->gdt
);
1576 cpu_put_seg(f
, &env
->idt
);
1578 qemu_put_be32s(f
, &env
->sysenter_cs
);
1579 qemu_put_be32s(f
, &env
->sysenter_esp
);
1580 qemu_put_be32s(f
, &env
->sysenter_eip
);
1582 qemu_put_be32s(f
, &env
->cr
[0]);
1583 qemu_put_be32s(f
, &env
->cr
[2]);
1584 qemu_put_be32s(f
, &env
->cr
[3]);
1585 qemu_put_be32s(f
, &env
->cr
[4]);
1587 for(i
= 0; i
< 8; i
++)
1588 qemu_put_be32s(f
, &env
->dr
[i
]);
1591 qemu_put_be32s(f
, &env
->a20_mask
);
1594 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1596 CPUState
*env
= opaque
;
1599 uint16_t fpus
, fpuc
, fptag
;
1601 if (version_id
!= 1)
1603 for(i
= 0; i
< 8; i
++)
1604 qemu_get_be32s(f
, &env
->regs
[i
]);
1605 qemu_get_be32s(f
, &env
->eip
);
1606 qemu_get_be32s(f
, &env
->eflags
);
1607 qemu_get_be32s(f
, &env
->eflags
);
1608 qemu_get_be32s(f
, &hflags
);
1610 qemu_get_be16s(f
, &fpuc
);
1611 qemu_get_be16s(f
, &fpus
);
1612 qemu_get_be16s(f
, &fptag
);
1614 for(i
= 0; i
< 8; i
++) {
1617 mant
= qemu_get_be64(f
);
1618 exp
= qemu_get_be16(f
);
1619 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1623 env
->fpstt
= (fpus
>> 11) & 7;
1624 env
->fpus
= fpus
& ~0x3800;
1625 for(i
= 0; i
< 8; i
++) {
1626 env
->fptags
[i
] = ((fptag
& 3) == 3);
1630 for(i
= 0; i
< 6; i
++)
1631 cpu_get_seg(f
, &env
->segs
[i
]);
1632 cpu_get_seg(f
, &env
->ldt
);
1633 cpu_get_seg(f
, &env
->tr
);
1634 cpu_get_seg(f
, &env
->gdt
);
1635 cpu_get_seg(f
, &env
->idt
);
1637 qemu_get_be32s(f
, &env
->sysenter_cs
);
1638 qemu_get_be32s(f
, &env
->sysenter_esp
);
1639 qemu_get_be32s(f
, &env
->sysenter_eip
);
1641 qemu_get_be32s(f
, &env
->cr
[0]);
1642 qemu_get_be32s(f
, &env
->cr
[2]);
1643 qemu_get_be32s(f
, &env
->cr
[3]);
1644 qemu_get_be32s(f
, &env
->cr
[4]);
1646 for(i
= 0; i
< 8; i
++)
1647 qemu_get_be32s(f
, &env
->dr
[i
]);
1650 qemu_get_be32s(f
, &env
->a20_mask
);
1652 /* XXX: compute hflags from scratch, except for CPL and IIF */
1653 env
->hflags
= hflags
;
1658 #elif defined(TARGET_PPC)
1659 void cpu_save(QEMUFile
*f
, void *opaque
)
1663 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1669 #warning No CPU save/restore functions
1673 /***********************************************************/
1674 /* ram save/restore */
1676 /* we just avoid storing empty pages */
1677 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1682 for(i
= 1; i
< len
; i
++) {
1686 qemu_put_byte(f
, 1);
1687 qemu_put_byte(f
, v
);
1690 qemu_put_byte(f
, 0);
1691 qemu_put_buffer(f
, buf
, len
);
1694 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1698 v
= qemu_get_byte(f
);
1701 if (qemu_get_buffer(f
, buf
, len
) != len
)
1705 v
= qemu_get_byte(f
);
1706 memset(buf
, v
, len
);
1714 static void ram_save(QEMUFile
*f
, void *opaque
)
1717 qemu_put_be32(f
, phys_ram_size
);
1718 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1719 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1723 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1727 if (version_id
!= 1)
1729 if (qemu_get_be32(f
) != phys_ram_size
)
1731 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1732 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1739 /***********************************************************/
1740 /* main execution loop */
1742 void gui_update(void *opaque
)
1744 display_state
.dpy_refresh(&display_state
);
1745 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1748 /* XXX: support several handlers */
1749 VMStopHandler
*vm_stop_cb
;
1750 VMStopHandler
*vm_stop_opaque
;
1752 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1755 vm_stop_opaque
= opaque
;
1759 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1772 void vm_stop(int reason
)
1775 cpu_disable_ticks();
1779 vm_stop_cb(vm_stop_opaque
, reason
);
1788 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1789 IOHandlerRecord
*ioh
, *ioh_next
;
1794 CPUState
*env
= global_env
;
1798 ret
= cpu_exec(env
);
1799 if (reset_requested
) {
1800 ret
= EXCP_INTERRUPT
;
1803 if (ret
== EXCP_DEBUG
) {
1804 vm_stop(EXCP_DEBUG
);
1806 /* if hlt instruction, we wait until the next IRQ */
1807 /* XXX: use timeout computed from timers */
1808 if (ret
== EXCP_HLT
)
1821 /* poll any events */
1822 /* XXX: separate device handlers from system ones */
1824 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1825 if (!ioh
->fd_can_read
) {
1828 pf
->events
= POLLIN
;
1832 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1834 if (max_size
> sizeof(buf
))
1835 max_size
= sizeof(buf
);
1837 pf
->events
= POLLIN
;
1844 ioh
->max_size
= max_size
;
1847 ret
= poll(ufds
, pf
- ufds
, timeout
);
1849 /* XXX: better handling of removal */
1850 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1851 ioh_next
= ioh
->next
;
1854 if (pf
->revents
& POLLIN
) {
1855 if (ioh
->max_size
== 0) {
1856 /* just a read event */
1857 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1859 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1861 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1862 } else if (errno
!= EAGAIN
) {
1863 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1871 #if defined(CONFIG_SLIRP)
1872 /* XXX: merge with poll() */
1874 fd_set rfds
, wfds
, xfds
;
1882 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1885 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1887 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1895 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1896 qemu_get_clock(vm_clock
));
1898 if (audio_enabled
) {
1899 /* XXX: add explicit timer */
1903 /* run dma transfers, if any */
1907 /* real time timers */
1908 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1909 qemu_get_clock(rt_clock
));
1911 cpu_disable_ticks();
1917 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
1918 "usage: %s [options] [disk_image]\n"
1920 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1922 "Standard options:\n"
1923 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1924 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1925 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1926 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1927 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1928 "-snapshot write to temporary files instead of disk image files\n"
1929 "-m megs set virtual RAM size to megs MB [default=%d]\n"
1930 "-nographic disable graphical output and redirect serial I/Os to console\n"
1931 "-enable-audio enable audio support\n"
1933 "Network options:\n"
1934 "-nics n simulate 'n' network cards [default=1]\n"
1935 "-macaddr addr set the mac address of the first interface\n"
1936 "-n script set tap/tun network init script [default=%s]\n"
1937 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1939 "-user-net use user mode network stack [default if no tap/tun script]\n"
1941 "-dummy-net use dummy network stack\n"
1943 "Linux boot specific:\n"
1944 "-kernel bzImage use 'bzImage' as kernel image\n"
1945 "-append cmdline use 'cmdline' as kernel command line\n"
1946 "-initrd file use 'file' as initial ram disk\n"
1948 "Debug/Expert options:\n"
1949 "-S freeze CPU at startup (use 'c' to start execution)\n"
1950 "-s wait gdb connection to port %d\n"
1951 "-p port change gdb connection port\n"
1952 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1953 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1954 "-L path set the directory for the BIOS and VGA BIOS\n"
1955 #ifdef USE_CODE_COPY
1956 "-no-code-copy disable code copy acceleration\n"
1960 "During emulation, use C-a h to get terminal commands:\n",
1961 #ifdef CONFIG_SOFTMMU
1967 DEFAULT_NETWORK_SCRIPT
,
1968 DEFAULT_GDBSTUB_PORT
,
1971 #ifndef CONFIG_SOFTMMU
1973 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1974 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1980 #define HAS_ARG 0x0001
1993 QEMU_OPTION_snapshot
,
1995 QEMU_OPTION_nographic
,
1996 QEMU_OPTION_enable_audio
,
1999 QEMU_OPTION_macaddr
,
2002 QEMU_OPTION_user_net
,
2003 QEMU_OPTION_dummy_net
,
2015 QEMU_OPTION_no_code_copy
,
2020 typedef struct QEMUOption
{
2026 const QEMUOption qemu_options
[] = {
2027 { "h", 0, QEMU_OPTION_h
},
2029 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2030 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2031 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2032 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2033 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2034 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2035 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2036 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2037 { "snapshot", 0, QEMU_OPTION_snapshot
},
2038 { "m", HAS_ARG
, QEMU_OPTION_m
},
2039 { "nographic", 0, QEMU_OPTION_nographic
},
2040 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2042 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2043 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2044 { "n", HAS_ARG
, QEMU_OPTION_n
},
2045 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2047 { "user-net", 0, QEMU_OPTION_user_net
},
2049 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2051 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2052 { "append", HAS_ARG
, QEMU_OPTION_append
},
2053 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2055 { "S", 0, QEMU_OPTION_S
},
2056 { "s", 0, QEMU_OPTION_s
},
2057 { "p", HAS_ARG
, QEMU_OPTION_p
},
2058 { "d", HAS_ARG
, QEMU_OPTION_d
},
2059 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2060 { "L", HAS_ARG
, QEMU_OPTION_L
},
2061 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2063 /* temporary options */
2064 { "pci", 0, QEMU_OPTION_pci
},
2066 { "prep", 0, QEMU_OPTION_prep
},
2071 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2073 /* this stack is only used during signal handling */
2074 #define SIGNAL_STACK_SIZE 32768
2076 static uint8_t *signal_stack
;
2080 #define NET_IF_TUN 0
2081 #define NET_IF_USER 1
2082 #define NET_IF_DUMMY 2
2084 int main(int argc
, char **argv
)
2086 #ifdef CONFIG_GDBSTUB
2087 int use_gdbstub
, gdbstub_port
;
2090 int snapshot
, linux_boot
;
2092 const char *initrd_filename
;
2093 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2094 const char *kernel_filename
, *kernel_cmdline
;
2095 DisplayState
*ds
= &display_state
;
2096 int cyls
, heads
, secs
;
2097 int start_emulation
= 1;
2099 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2101 const char *r
, *optarg
;
2103 #if !defined(CONFIG_SOFTMMU)
2104 /* we never want that malloc() uses mmap() */
2105 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2107 initrd_filename
= NULL
;
2108 for(i
= 0; i
< MAX_FD
; i
++)
2109 fd_filename
[i
] = NULL
;
2110 for(i
= 0; i
< MAX_DISKS
; i
++)
2111 hd_filename
[i
] = NULL
;
2112 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2113 vga_ram_size
= VGA_RAM_SIZE
;
2114 bios_size
= BIOS_SIZE
;
2115 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2116 #ifdef CONFIG_GDBSTUB
2118 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2122 kernel_filename
= NULL
;
2123 kernel_cmdline
= "";
2125 cyls
= heads
= secs
= 0;
2130 /* default mac address of the first network interface */
2144 hd_filename
[0] = argv
[optind
++];
2146 const QEMUOption
*popt
;
2149 popt
= qemu_options
;
2152 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2156 if (!strcmp(popt
->name
, r
+ 1))
2160 if (popt
->flags
& HAS_ARG
) {
2161 if (optind
>= argc
) {
2162 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2166 optarg
= argv
[optind
++];
2171 switch(popt
->index
) {
2172 case QEMU_OPTION_initrd
:
2173 initrd_filename
= optarg
;
2175 case QEMU_OPTION_hda
:
2176 hd_filename
[0] = optarg
;
2178 case QEMU_OPTION_hdb
:
2179 hd_filename
[1] = optarg
;
2181 case QEMU_OPTION_snapshot
:
2184 case QEMU_OPTION_hdachs
:
2188 cyls
= strtol(p
, (char **)&p
, 0);
2192 heads
= strtol(p
, (char **)&p
, 0);
2196 secs
= strtol(p
, (char **)&p
, 0);
2203 case QEMU_OPTION_nographic
:
2206 case QEMU_OPTION_kernel
:
2207 kernel_filename
= optarg
;
2209 case QEMU_OPTION_append
:
2210 kernel_cmdline
= optarg
;
2212 case QEMU_OPTION_tun_fd
:
2216 net_if_type
= NET_IF_TUN
;
2217 if (nb_tun_fds
< MAX_NICS
) {
2218 fd
= strtol(optarg
, (char **)&p
, 0);
2220 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2223 tun_fds
[nb_tun_fds
++] = fd
;
2227 case QEMU_OPTION_hdc
:
2228 hd_filename
[2] = optarg
;
2231 case QEMU_OPTION_hdd
:
2232 hd_filename
[3] = optarg
;
2234 case QEMU_OPTION_cdrom
:
2235 hd_filename
[2] = optarg
;
2238 case QEMU_OPTION_boot
:
2239 boot_device
= optarg
[0];
2240 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2241 boot_device
!= 'c' && boot_device
!= 'd') {
2242 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2246 case QEMU_OPTION_fda
:
2247 fd_filename
[0] = optarg
;
2249 case QEMU_OPTION_fdb
:
2250 fd_filename
[1] = optarg
;
2252 case QEMU_OPTION_no_code_copy
:
2253 code_copy_enabled
= 0;
2255 case QEMU_OPTION_nics
:
2256 nb_nics
= atoi(optarg
);
2257 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2258 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2262 case QEMU_OPTION_macaddr
:
2267 for(i
= 0; i
< 6; i
++) {
2268 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2275 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2283 case QEMU_OPTION_user_net
:
2284 net_if_type
= NET_IF_USER
;
2286 case QEMU_OPTION_dummy_net
:
2287 net_if_type
= NET_IF_DUMMY
;
2289 case QEMU_OPTION_enable_audio
:
2296 ram_size
= atoi(optarg
) * 1024 * 1024;
2299 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2300 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2301 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2310 mask
= cpu_str_to_log_mask(optarg
);
2312 printf("Log items (comma separated):\n");
2313 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2314 printf("%-10s %s\n", item
->name
, item
->help
);
2322 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2324 #ifdef CONFIG_GDBSTUB
2329 gdbstub_port
= atoi(optarg
);
2336 start_emulation
= 0;
2338 case QEMU_OPTION_pci
:
2341 case QEMU_OPTION_prep
:
2348 linux_boot
= (kernel_filename
!= NULL
);
2350 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2351 fd_filename
[0] == '\0')
2354 /* boot to cd by default if no hard disk */
2355 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2356 if (fd_filename
[0] != '\0')
2362 #if !defined(CONFIG_SOFTMMU)
2363 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2365 static uint8_t stdout_buf
[4096];
2366 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2369 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2372 /* init host network redirectors */
2373 if (net_if_type
== -1) {
2374 net_if_type
= NET_IF_TUN
;
2375 #if defined(CONFIG_SLIRP)
2376 if (access(network_script
, R_OK
) < 0) {
2377 net_if_type
= NET_IF_USER
;
2382 for(i
= 0; i
< nb_nics
; i
++) {
2383 NetDriverState
*nd
= &nd_table
[i
];
2385 /* init virtual mac address */
2386 nd
->macaddr
[0] = macaddr
[0];
2387 nd
->macaddr
[1] = macaddr
[1];
2388 nd
->macaddr
[2] = macaddr
[2];
2389 nd
->macaddr
[3] = macaddr
[3];
2390 nd
->macaddr
[4] = macaddr
[4];
2391 nd
->macaddr
[5] = macaddr
[5] + i
;
2392 switch(net_if_type
) {
2393 #if defined(CONFIG_SLIRP)
2398 #if !defined(_WIN32)
2400 if (i
< nb_tun_fds
) {
2401 net_fd_init(nd
, tun_fds
[i
]);
2403 if (net_tun_init(nd
) < 0)
2415 /* init the memory */
2416 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
2418 #ifdef CONFIG_SOFTMMU
2420 /* mallocs are always aligned on BSD. */
2421 phys_ram_base
= malloc(phys_ram_size
);
2423 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2425 if (!phys_ram_base
) {
2426 fprintf(stderr
, "Could not allocate physical memory\n");
2430 /* as we must map the same page at several addresses, we must use
2435 tmpdir
= getenv("QEMU_TMPDIR");
2438 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2439 if (mkstemp(phys_ram_file
) < 0) {
2440 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2444 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2445 if (phys_ram_fd
< 0) {
2446 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2450 ftruncate(phys_ram_fd
, phys_ram_size
);
2451 unlink(phys_ram_file
);
2452 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2454 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2456 if (phys_ram_base
== MAP_FAILED
) {
2457 fprintf(stderr
, "Could not map physical memory\n");
2463 /* we always create the cdrom drive, even if no disk is there */
2465 bs_table
[2] = bdrv_new("cdrom");
2466 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2469 /* open the virtual block devices */
2470 for(i
= 0; i
< MAX_DISKS
; i
++) {
2471 if (hd_filename
[i
]) {
2474 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2475 bs_table
[i
] = bdrv_new(buf
);
2477 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2478 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2482 if (i
== 0 && cyls
!= 0)
2483 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2487 /* we always create at least one floppy disk */
2488 fd_table
[0] = bdrv_new("fda");
2489 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2491 for(i
= 0; i
< MAX_FD
; i
++) {
2492 if (fd_filename
[i
]) {
2495 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2496 fd_table
[i
] = bdrv_new(buf
);
2497 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2499 if (fd_filename
[i
] != '\0') {
2500 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2501 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2509 /* init CPU state */
2512 cpu_single_env
= env
;
2514 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2515 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2516 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2519 cpu_calibrate_ticks();
2523 dumb_display_init(ds
);
2526 sdl_display_init(ds
);
2528 dumb_display_init(ds
);
2532 /* setup cpu signal handlers for MMU / self modifying code handling */
2533 #if !defined(CONFIG_SOFTMMU)
2535 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2538 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2539 stk
.ss_sp
= signal_stack
;
2540 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2543 if (sigaltstack(&stk
, NULL
) < 0) {
2544 perror("sigaltstack");
2550 struct sigaction act
;
2552 sigfillset(&act
.sa_mask
);
2553 act
.sa_flags
= SA_SIGINFO
;
2554 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2555 act
.sa_flags
|= SA_ONSTACK
;
2557 act
.sa_sigaction
= host_segv_handler
;
2558 sigaction(SIGSEGV
, &act
, NULL
);
2559 sigaction(SIGBUS
, &act
, NULL
);
2560 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2561 sigaction(SIGFPE
, &act
, NULL
);
2568 struct sigaction act
;
2569 sigfillset(&act
.sa_mask
);
2571 act
.sa_handler
= SIG_IGN
;
2572 sigaction(SIGPIPE
, &act
, NULL
);
2577 #if defined(TARGET_I386)
2578 pc_init(ram_size
, vga_ram_size
, boot_device
,
2579 ds
, fd_filename
, snapshot
,
2580 kernel_filename
, kernel_cmdline
, initrd_filename
);
2581 #elif defined(TARGET_PPC)
2582 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2583 ds
, fd_filename
, snapshot
,
2584 kernel_filename
, kernel_cmdline
, initrd_filename
);
2587 /* launched after the device init so that it can display or not a
2591 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2592 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2594 #ifdef CONFIG_GDBSTUB
2596 if (gdbserver_start(gdbstub_port
) < 0) {
2597 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2601 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2605 if (start_emulation
)