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
35 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
47 #include <linux/if_tun.h>
50 #include <linux/rtc.h>
54 #if defined(CONFIG_SLIRP)
60 #include <sys/timeb.h>
62 #define getopt_long_only getopt_long
63 #define memalign(align, size) malloc(size)
67 #if defined(__linux__)
68 /* SDL use the pthreads and they modify sigaction. We don't
70 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
71 extern void __libc_sigaction();
72 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
74 extern void __sigaction();
75 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
77 #endif /* __linux__ */
78 #endif /* CONFIG_SDL */
86 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
88 //#define DEBUG_UNUSED_IOPORT
89 //#define DEBUG_IOPORT
91 #if !defined(CONFIG_SOFTMMU)
92 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
94 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
98 #define GUI_REFRESH_INTERVAL 30
100 /* XXX: use a two level table to limit memory usage */
101 #define MAX_IOPORTS 65536
103 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
104 char phys_ram_file
[1024];
105 CPUState
*global_env
;
106 CPUState
*cpu_single_env
;
107 void *ioport_opaque
[MAX_IOPORTS
];
108 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
109 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
110 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
112 static DisplayState display_state
;
114 int64_t ticks_per_sec
;
115 int boot_device
= 'c';
117 static char network_script
[1024];
118 int pit_min_timer_count
= 0;
120 NetDriverState nd_table
[MAX_NICS
];
121 SerialState
*serial_console
;
122 QEMUTimer
*gui_timer
;
124 int audio_enabled
= 0;
126 /***********************************************************/
127 /* x86 ISA bus support */
129 target_phys_addr_t isa_mem_base
= 0;
131 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
133 #ifdef DEBUG_UNUSED_IOPORT
134 fprintf(stderr
, "inb: port=0x%04x\n", address
);
139 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
141 #ifdef DEBUG_UNUSED_IOPORT
142 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
146 /* default is to make two byte accesses */
147 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
150 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
);
151 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1) << 8;
155 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
157 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
, data
& 0xff);
158 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1, (data
>> 8) & 0xff);
161 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
163 #ifdef DEBUG_UNUSED_IOPORT
164 fprintf(stderr
, "inl: port=0x%04x\n", address
);
169 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
171 #ifdef DEBUG_UNUSED_IOPORT
172 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
176 void init_ioports(void)
180 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
181 ioport_read_table
[0][i
] = default_ioport_readb
;
182 ioport_write_table
[0][i
] = default_ioport_writeb
;
183 ioport_read_table
[1][i
] = default_ioport_readw
;
184 ioport_write_table
[1][i
] = default_ioport_writew
;
185 ioport_read_table
[2][i
] = default_ioport_readl
;
186 ioport_write_table
[2][i
] = default_ioport_writel
;
190 /* size is the word size in byte */
191 int register_ioport_read(int start
, int length
, int size
,
192 IOPortReadFunc
*func
, void *opaque
)
198 } else if (size
== 2) {
200 } else if (size
== 4) {
203 hw_error("register_ioport_read: invalid size");
206 for(i
= start
; i
< start
+ length
; i
+= size
) {
207 ioport_read_table
[bsize
][i
] = func
;
208 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
209 hw_error("register_ioport_read: invalid opaque");
210 ioport_opaque
[i
] = opaque
;
215 /* size is the word size in byte */
216 int register_ioport_write(int start
, int length
, int size
,
217 IOPortWriteFunc
*func
, void *opaque
)
223 } else if (size
== 2) {
225 } else if (size
== 4) {
228 hw_error("register_ioport_write: invalid size");
231 for(i
= start
; i
< start
+ length
; i
+= size
) {
232 ioport_write_table
[bsize
][i
] = func
;
233 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
234 hw_error("register_ioport_read: invalid opaque");
235 ioport_opaque
[i
] = opaque
;
240 void pstrcpy(char *buf
, int buf_size
, const char *str
)
250 if (c
== 0 || q
>= buf
+ buf_size
- 1)
257 /* strcat and truncate. */
258 char *pstrcat(char *buf
, int buf_size
, const char *s
)
263 pstrcpy(buf
+ len
, buf_size
- len
, s
);
267 /* return the size or -1 if error */
268 int load_image(const char *filename
, uint8_t *addr
)
271 fd
= open(filename
, O_RDONLY
| O_BINARY
);
274 size
= lseek(fd
, 0, SEEK_END
);
275 lseek(fd
, 0, SEEK_SET
);
276 if (read(fd
, addr
, size
) != size
) {
284 void cpu_outb(CPUState
*env
, int addr
, int val
)
286 addr
&= (MAX_IOPORTS
- 1);
288 if (loglevel
& CPU_LOG_IOPORT
)
289 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
291 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
294 void cpu_outw(CPUState
*env
, int addr
, int val
)
296 addr
&= (MAX_IOPORTS
- 1);
298 if (loglevel
& CPU_LOG_IOPORT
)
299 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
301 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
304 void cpu_outl(CPUState
*env
, int addr
, int val
)
306 addr
&= (MAX_IOPORTS
- 1);
308 if (loglevel
& CPU_LOG_IOPORT
)
309 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
311 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
314 int cpu_inb(CPUState
*env
, int addr
)
317 addr
&= (MAX_IOPORTS
- 1);
318 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
320 if (loglevel
& CPU_LOG_IOPORT
)
321 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
326 int cpu_inw(CPUState
*env
, int addr
)
329 addr
&= (MAX_IOPORTS
- 1);
330 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
332 if (loglevel
& CPU_LOG_IOPORT
)
333 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
338 int cpu_inl(CPUState
*env
, int addr
)
341 addr
&= (MAX_IOPORTS
- 1);
342 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
344 if (loglevel
& CPU_LOG_IOPORT
)
345 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
350 /***********************************************************/
351 void hw_error(const char *fmt
, ...)
356 fprintf(stderr
, "qemu: hardware error: ");
357 vfprintf(stderr
, fmt
, ap
);
358 fprintf(stderr
, "\n");
360 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
362 cpu_dump_state(global_env
, stderr
, 0);
368 /***********************************************************/
371 #if defined(__powerpc__)
373 static inline uint32_t get_tbl(void)
376 asm volatile("mftb %0" : "=r" (tbl
));
380 static inline uint32_t get_tbu(void)
383 asm volatile("mftbu %0" : "=r" (tbl
));
387 int64_t cpu_get_real_ticks(void)
390 /* NOTE: we test if wrapping has occurred */
396 return ((int64_t)h
<< 32) | l
;
399 #elif defined(__i386__)
401 int64_t cpu_get_real_ticks(void)
404 asm volatile ("rdtsc" : "=A" (val
));
408 #elif defined(__x86_64__)
410 int64_t cpu_get_real_ticks(void)
414 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
422 #error unsupported CPU
425 static int64_t cpu_ticks_offset
;
426 static int cpu_ticks_enabled
;
428 static inline int64_t cpu_get_ticks(void)
430 if (!cpu_ticks_enabled
) {
431 return cpu_ticks_offset
;
433 return cpu_get_real_ticks() + cpu_ticks_offset
;
437 /* enable cpu_get_ticks() */
438 void cpu_enable_ticks(void)
440 if (!cpu_ticks_enabled
) {
441 cpu_ticks_offset
-= cpu_get_real_ticks();
442 cpu_ticks_enabled
= 1;
446 /* disable cpu_get_ticks() : the clock is stopped. You must not call
447 cpu_get_ticks() after that. */
448 void cpu_disable_ticks(void)
450 if (cpu_ticks_enabled
) {
451 cpu_ticks_offset
= cpu_get_ticks();
452 cpu_ticks_enabled
= 0;
456 static int64_t get_clock(void)
461 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
464 gettimeofday(&tv
, NULL
);
465 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
469 void cpu_calibrate_ticks(void)
474 ticks
= cpu_get_real_ticks();
480 usec
= get_clock() - usec
;
481 ticks
= cpu_get_real_ticks() - ticks
;
482 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
485 /* compute with 96 bit intermediate result: (a*b)/c */
486 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
491 #ifdef WORDS_BIGENDIAN
501 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
502 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
505 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
509 #define QEMU_TIMER_REALTIME 0
510 #define QEMU_TIMER_VIRTUAL 1
514 /* XXX: add frequency */
522 struct QEMUTimer
*next
;
528 static QEMUTimer
*active_timers
[2];
530 static MMRESULT timerID
;
532 /* frequency of the times() clock tick */
533 static int timer_freq
;
536 QEMUClock
*qemu_new_clock(int type
)
539 clock
= qemu_mallocz(sizeof(QEMUClock
));
546 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
550 ts
= qemu_mallocz(sizeof(QEMUTimer
));
557 void qemu_free_timer(QEMUTimer
*ts
)
562 /* stop a timer, but do not dealloc it */
563 void qemu_del_timer(QEMUTimer
*ts
)
567 /* NOTE: this code must be signal safe because
568 qemu_timer_expired() can be called from a signal. */
569 pt
= &active_timers
[ts
->clock
->type
];
582 /* modify the current timer so that it will be fired when current_time
583 >= expire_time. The corresponding callback will be called. */
584 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
590 /* add the timer in the sorted list */
591 /* NOTE: this code must be signal safe because
592 qemu_timer_expired() can be called from a signal. */
593 pt
= &active_timers
[ts
->clock
->type
];
598 if (t
->expire_time
> expire_time
)
602 ts
->expire_time
= expire_time
;
607 int qemu_timer_pending(QEMUTimer
*ts
)
610 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
617 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
621 return (timer_head
->expire_time
<= current_time
);
624 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
630 if (ts
->expire_time
> current_time
)
632 /* remove timer from the list before calling the callback */
633 *ptimer_head
= ts
->next
;
636 /* run the callback (the timer list can be modified) */
641 int64_t qemu_get_clock(QEMUClock
*clock
)
643 switch(clock
->type
) {
644 case QEMU_TIMER_REALTIME
:
646 return GetTickCount();
651 /* Note that using gettimeofday() is not a good solution
652 for timers because its value change when the date is
654 if (timer_freq
== 100) {
655 return times(&tp
) * 10;
657 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
662 case QEMU_TIMER_VIRTUAL
:
663 return cpu_get_ticks();
668 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
670 uint64_t expire_time
;
672 if (qemu_timer_pending(ts
)) {
673 expire_time
= ts
->expire_time
;
677 qemu_put_be64(f
, expire_time
);
680 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
682 uint64_t expire_time
;
684 expire_time
= qemu_get_be64(f
);
685 if (expire_time
!= -1) {
686 qemu_mod_timer(ts
, expire_time
);
692 static void timer_save(QEMUFile
*f
, void *opaque
)
694 if (cpu_ticks_enabled
) {
695 hw_error("cannot save state if virtual timers are running");
697 qemu_put_be64s(f
, &cpu_ticks_offset
);
698 qemu_put_be64s(f
, &ticks_per_sec
);
701 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
705 if (cpu_ticks_enabled
) {
708 qemu_get_be64s(f
, &cpu_ticks_offset
);
709 qemu_get_be64s(f
, &ticks_per_sec
);
714 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
715 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
717 static void host_alarm_handler(int host_signum
)
720 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
721 qemu_get_clock(vm_clock
)) ||
722 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
723 qemu_get_clock(rt_clock
))) {
724 /* stop the cpu because a timer occured */
725 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
731 #define RTC_FREQ 1024
735 static int start_rtc_timer(void)
737 rtc_fd
= open("/dev/rtc", O_RDONLY
);
740 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
741 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
742 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
743 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
746 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
751 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
757 static void init_timers(void)
759 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
760 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
765 timerID
= timeSetEvent(10, // interval (ms)
767 host_alarm_handler
, // function
768 (DWORD
)&count
, // user parameter
769 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
771 perror("failed timer alarm");
775 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
778 struct sigaction act
;
779 struct itimerval itv
;
781 /* get times() syscall frequency */
782 timer_freq
= sysconf(_SC_CLK_TCK
);
785 sigfillset(&act
.sa_mask
);
787 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
788 act
.sa_flags
|= SA_ONSTACK
;
790 act
.sa_handler
= host_alarm_handler
;
791 sigaction(SIGALRM
, &act
, NULL
);
793 itv
.it_interval
.tv_sec
= 0;
794 itv
.it_interval
.tv_usec
= 1000;
795 itv
.it_value
.tv_sec
= 0;
796 itv
.it_value
.tv_usec
= 10 * 1000;
797 setitimer(ITIMER_REAL
, &itv
, NULL
);
798 /* we probe the tick duration of the kernel to inform the user if
799 the emulated kernel requested a too high timer frequency */
800 getitimer(ITIMER_REAL
, &itv
);
802 if (itv
.it_interval
.tv_usec
> 1000) {
803 /* try to use /dev/rtc to have a faster timer */
804 if (start_rtc_timer() < 0)
807 itv
.it_interval
.tv_sec
= 0;
808 itv
.it_interval
.tv_usec
= 0;
809 itv
.it_value
.tv_sec
= 0;
810 itv
.it_value
.tv_usec
= 0;
811 setitimer(ITIMER_REAL
, &itv
, NULL
);
814 sigaction(SIGIO
, &act
, NULL
);
815 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
816 fcntl(rtc_fd
, F_SETOWN
, getpid());
819 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
826 void quit_timers(void)
829 timeKillEvent(timerID
);
833 /***********************************************************/
838 int serial_open_device(void)
845 int serial_open_device(void)
847 char slave_name
[1024];
848 int master_fd
, slave_fd
;
850 if (serial_console
== NULL
&& nographic
) {
851 /* use console for serial port */
854 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
855 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
858 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
865 /***********************************************************/
866 /* Linux network device redirectors */
868 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
872 for(i
=0;i
<size
;i
+=16) {
876 fprintf(f
, "%08x ", i
);
879 fprintf(f
, " %02x", buf
[i
+j
]);
886 if (c
< ' ' || c
> '~')
894 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
896 nd
->send_packet(nd
, buf
, size
);
899 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
900 IOReadHandler
*fd_read
, void *opaque
)
902 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
905 /* dummy network adapter */
907 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
911 static void dummy_add_read_packet(NetDriverState
*nd
,
912 IOCanRWHandler
*fd_can_read
,
913 IOReadHandler
*fd_read
, void *opaque
)
917 static int net_dummy_init(NetDriverState
*nd
)
919 nd
->send_packet
= dummy_send_packet
;
920 nd
->add_read_packet
= dummy_add_read_packet
;
921 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
925 #if defined(CONFIG_SLIRP)
927 /* slirp network adapter */
929 static void *slirp_fd_opaque
;
930 static IOCanRWHandler
*slirp_fd_can_read
;
931 static IOReadHandler
*slirp_fd_read
;
932 static int slirp_inited
;
934 int slirp_can_output(void)
936 return slirp_fd_can_read(slirp_fd_opaque
);
939 void slirp_output(const uint8_t *pkt
, int pkt_len
)
943 hex_dump(stdout
, pkt
, pkt_len
);
945 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
948 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
952 hex_dump(stdout
, buf
, size
);
954 slirp_input(buf
, size
);
957 static void slirp_add_read_packet(NetDriverState
*nd
,
958 IOCanRWHandler
*fd_can_read
,
959 IOReadHandler
*fd_read
, void *opaque
)
961 slirp_fd_opaque
= opaque
;
962 slirp_fd_can_read
= fd_can_read
;
963 slirp_fd_read
= fd_read
;
966 static int net_slirp_init(NetDriverState
*nd
)
972 nd
->send_packet
= slirp_send_packet
;
973 nd
->add_read_packet
= slirp_add_read_packet
;
974 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
978 #endif /* CONFIG_SLIRP */
982 static int tun_open(char *ifname
, int ifname_size
)
988 fd
= open("/dev/tap", O_RDWR
);
990 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
995 dev
= devname(s
.st_rdev
, S_IFCHR
);
996 pstrcpy(ifname
, ifname_size
, dev
);
998 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1002 static int tun_open(char *ifname
, int ifname_size
)
1007 fd
= open("/dev/net/tun", O_RDWR
);
1009 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1012 memset(&ifr
, 0, sizeof(ifr
));
1013 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1014 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1015 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1017 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1021 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1022 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1023 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1028 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1030 write(nd
->fd
, buf
, size
);
1033 static void tun_add_read_packet(NetDriverState
*nd
,
1034 IOCanRWHandler
*fd_can_read
,
1035 IOReadHandler
*fd_read
, void *opaque
)
1037 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1040 static int net_tun_init(NetDriverState
*nd
)
1046 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1050 /* try to launch network init script */
1055 *parg
++ = network_script
;
1056 *parg
++ = nd
->ifname
;
1058 execv(network_script
, args
);
1061 while (waitpid(pid
, &status
, 0) != pid
);
1062 if (!WIFEXITED(status
) ||
1063 WEXITSTATUS(status
) != 0) {
1064 fprintf(stderr
, "%s: could not launch network script\n",
1068 nd
->send_packet
= tun_send_packet
;
1069 nd
->add_read_packet
= tun_add_read_packet
;
1073 static int net_fd_init(NetDriverState
*nd
, int fd
)
1076 nd
->send_packet
= tun_send_packet
;
1077 nd
->add_read_packet
= tun_add_read_packet
;
1078 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1082 #endif /* !_WIN32 */
1084 /***********************************************************/
1089 static void term_exit(void)
1093 static void term_init(void)
1099 /* init terminal so that we can grab keys */
1100 static struct termios oldtty
;
1102 static void term_exit(void)
1104 tcsetattr (0, TCSANOW
, &oldtty
);
1107 static void term_init(void)
1111 tcgetattr (0, &tty
);
1114 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1115 |INLCR
|IGNCR
|ICRNL
|IXON
);
1116 tty
.c_oflag
|= OPOST
;
1117 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1118 /* if graphical mode, we allow Ctrl-C handling */
1120 tty
.c_lflag
&= ~ISIG
;
1121 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1124 tty
.c_cc
[VTIME
] = 0;
1126 tcsetattr (0, TCSANOW
, &tty
);
1130 fcntl(0, F_SETFL
, O_NONBLOCK
);
1135 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1139 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1143 static void dumb_refresh(DisplayState
*ds
)
1145 vga_update_display();
1148 void dumb_display_init(DisplayState
*ds
)
1153 ds
->dpy_update
= dumb_update
;
1154 ds
->dpy_resize
= dumb_resize
;
1155 ds
->dpy_refresh
= dumb_refresh
;
1158 #if !defined(CONFIG_SOFTMMU)
1159 /***********************************************************/
1160 /* cpu signal handler */
1161 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1164 if (cpu_signal_handler(host_signum
, info
, puc
))
1171 /***********************************************************/
1174 #define MAX_IO_HANDLERS 64
1176 typedef struct IOHandlerRecord
{
1178 IOCanRWHandler
*fd_can_read
;
1179 IOReadHandler
*fd_read
;
1181 /* temporary data */
1184 struct IOHandlerRecord
*next
;
1187 static IOHandlerRecord
*first_io_handler
;
1189 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1190 IOReadHandler
*fd_read
, void *opaque
)
1192 IOHandlerRecord
*ioh
;
1194 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1198 ioh
->fd_can_read
= fd_can_read
;
1199 ioh
->fd_read
= fd_read
;
1200 ioh
->opaque
= opaque
;
1201 ioh
->next
= first_io_handler
;
1202 first_io_handler
= ioh
;
1206 void qemu_del_fd_read_handler(int fd
)
1208 IOHandlerRecord
**pioh
, *ioh
;
1210 pioh
= &first_io_handler
;
1215 if (ioh
->fd
== fd
) {
1223 /***********************************************************/
1224 /* savevm/loadvm support */
1226 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1228 fwrite(buf
, 1, size
, f
);
1231 void qemu_put_byte(QEMUFile
*f
, int v
)
1236 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1238 qemu_put_byte(f
, v
>> 8);
1239 qemu_put_byte(f
, v
);
1242 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1244 qemu_put_byte(f
, v
>> 24);
1245 qemu_put_byte(f
, v
>> 16);
1246 qemu_put_byte(f
, v
>> 8);
1247 qemu_put_byte(f
, v
);
1250 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1252 qemu_put_be32(f
, v
>> 32);
1253 qemu_put_be32(f
, v
);
1256 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1258 return fread(buf
, 1, size
, f
);
1261 int qemu_get_byte(QEMUFile
*f
)
1271 unsigned int qemu_get_be16(QEMUFile
*f
)
1274 v
= qemu_get_byte(f
) << 8;
1275 v
|= qemu_get_byte(f
);
1279 unsigned int qemu_get_be32(QEMUFile
*f
)
1282 v
= qemu_get_byte(f
) << 24;
1283 v
|= qemu_get_byte(f
) << 16;
1284 v
|= qemu_get_byte(f
) << 8;
1285 v
|= qemu_get_byte(f
);
1289 uint64_t qemu_get_be64(QEMUFile
*f
)
1292 v
= (uint64_t)qemu_get_be32(f
) << 32;
1293 v
|= qemu_get_be32(f
);
1297 int64_t qemu_ftell(QEMUFile
*f
)
1302 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1304 if (fseek(f
, pos
, whence
) < 0)
1309 typedef struct SaveStateEntry
{
1313 SaveStateHandler
*save_state
;
1314 LoadStateHandler
*load_state
;
1316 struct SaveStateEntry
*next
;
1319 static SaveStateEntry
*first_se
;
1321 int register_savevm(const char *idstr
,
1324 SaveStateHandler
*save_state
,
1325 LoadStateHandler
*load_state
,
1328 SaveStateEntry
*se
, **pse
;
1330 se
= qemu_malloc(sizeof(SaveStateEntry
));
1333 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1334 se
->instance_id
= instance_id
;
1335 se
->version_id
= version_id
;
1336 se
->save_state
= save_state
;
1337 se
->load_state
= load_state
;
1338 se
->opaque
= opaque
;
1341 /* add at the end of list */
1343 while (*pse
!= NULL
)
1344 pse
= &(*pse
)->next
;
1349 #define QEMU_VM_FILE_MAGIC 0x5145564d
1350 #define QEMU_VM_FILE_VERSION 0x00000001
1352 int qemu_savevm(const char *filename
)
1356 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1358 saved_vm_running
= vm_running
;
1361 f
= fopen(filename
, "wb");
1367 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1368 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1370 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1372 len
= strlen(se
->idstr
);
1373 qemu_put_byte(f
, len
);
1374 qemu_put_buffer(f
, se
->idstr
, len
);
1376 qemu_put_be32(f
, se
->instance_id
);
1377 qemu_put_be32(f
, se
->version_id
);
1379 /* record size: filled later */
1381 qemu_put_be32(f
, 0);
1383 se
->save_state(f
, se
->opaque
);
1385 /* fill record size */
1387 len
= ftell(f
) - len_pos
- 4;
1388 fseek(f
, len_pos
, SEEK_SET
);
1389 qemu_put_be32(f
, len
);
1390 fseek(f
, cur_pos
, SEEK_SET
);
1396 if (saved_vm_running
)
1401 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1405 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1406 if (!strcmp(se
->idstr
, idstr
) &&
1407 instance_id
== se
->instance_id
)
1413 int qemu_loadvm(const char *filename
)
1417 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1418 int saved_vm_running
;
1422 saved_vm_running
= vm_running
;
1425 f
= fopen(filename
, "rb");
1431 v
= qemu_get_be32(f
);
1432 if (v
!= QEMU_VM_FILE_MAGIC
)
1434 v
= qemu_get_be32(f
);
1435 if (v
!= QEMU_VM_FILE_VERSION
) {
1442 #if defined (DO_TB_FLUSH)
1443 tb_flush(global_env
);
1445 len
= qemu_get_byte(f
);
1448 qemu_get_buffer(f
, idstr
, len
);
1450 instance_id
= qemu_get_be32(f
);
1451 version_id
= qemu_get_be32(f
);
1452 record_len
= qemu_get_be32(f
);
1454 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1455 idstr
, instance_id
, version_id
, record_len
);
1458 se
= find_se(idstr
, instance_id
);
1460 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1461 instance_id
, idstr
);
1463 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1465 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1466 instance_id
, idstr
);
1469 /* always seek to exact end of record */
1470 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1475 if (saved_vm_running
)
1480 /***********************************************************/
1481 /* cpu save/restore */
1483 #if defined(TARGET_I386)
1485 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1487 qemu_put_be32(f
, (uint32_t)dt
->base
);
1488 qemu_put_be32(f
, dt
->limit
);
1489 qemu_put_be32(f
, dt
->flags
);
1492 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1494 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1495 dt
->limit
= qemu_get_be32(f
);
1496 dt
->flags
= qemu_get_be32(f
);
1499 void cpu_save(QEMUFile
*f
, void *opaque
)
1501 CPUState
*env
= opaque
;
1502 uint16_t fptag
, fpus
, fpuc
;
1506 for(i
= 0; i
< 8; i
++)
1507 qemu_put_be32s(f
, &env
->regs
[i
]);
1508 qemu_put_be32s(f
, &env
->eip
);
1509 qemu_put_be32s(f
, &env
->eflags
);
1510 qemu_put_be32s(f
, &env
->eflags
);
1511 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1512 qemu_put_be32s(f
, &hflags
);
1516 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1518 for (i
=7; i
>=0; i
--) {
1520 if (env
->fptags
[i
]) {
1525 qemu_put_be16s(f
, &fpuc
);
1526 qemu_put_be16s(f
, &fpus
);
1527 qemu_put_be16s(f
, &fptag
);
1529 for(i
= 0; i
< 8; i
++) {
1532 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1533 qemu_put_be64(f
, mant
);
1534 qemu_put_be16(f
, exp
);
1537 for(i
= 0; i
< 6; i
++)
1538 cpu_put_seg(f
, &env
->segs
[i
]);
1539 cpu_put_seg(f
, &env
->ldt
);
1540 cpu_put_seg(f
, &env
->tr
);
1541 cpu_put_seg(f
, &env
->gdt
);
1542 cpu_put_seg(f
, &env
->idt
);
1544 qemu_put_be32s(f
, &env
->sysenter_cs
);
1545 qemu_put_be32s(f
, &env
->sysenter_esp
);
1546 qemu_put_be32s(f
, &env
->sysenter_eip
);
1548 qemu_put_be32s(f
, &env
->cr
[0]);
1549 qemu_put_be32s(f
, &env
->cr
[2]);
1550 qemu_put_be32s(f
, &env
->cr
[3]);
1551 qemu_put_be32s(f
, &env
->cr
[4]);
1553 for(i
= 0; i
< 8; i
++)
1554 qemu_put_be32s(f
, &env
->dr
[i
]);
1557 qemu_put_be32s(f
, &env
->a20_mask
);
1560 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1562 CPUState
*env
= opaque
;
1565 uint16_t fpus
, fpuc
, fptag
;
1567 if (version_id
!= 1)
1569 for(i
= 0; i
< 8; i
++)
1570 qemu_get_be32s(f
, &env
->regs
[i
]);
1571 qemu_get_be32s(f
, &env
->eip
);
1572 qemu_get_be32s(f
, &env
->eflags
);
1573 qemu_get_be32s(f
, &env
->eflags
);
1574 qemu_get_be32s(f
, &hflags
);
1576 qemu_get_be16s(f
, &fpuc
);
1577 qemu_get_be16s(f
, &fpus
);
1578 qemu_get_be16s(f
, &fptag
);
1580 for(i
= 0; i
< 8; i
++) {
1583 mant
= qemu_get_be64(f
);
1584 exp
= qemu_get_be16(f
);
1585 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1589 env
->fpstt
= (fpus
>> 11) & 7;
1590 env
->fpus
= fpus
& ~0x3800;
1591 for(i
= 0; i
< 8; i
++) {
1592 env
->fptags
[i
] = ((fptag
& 3) == 3);
1596 for(i
= 0; i
< 6; i
++)
1597 cpu_get_seg(f
, &env
->segs
[i
]);
1598 cpu_get_seg(f
, &env
->ldt
);
1599 cpu_get_seg(f
, &env
->tr
);
1600 cpu_get_seg(f
, &env
->gdt
);
1601 cpu_get_seg(f
, &env
->idt
);
1603 qemu_get_be32s(f
, &env
->sysenter_cs
);
1604 qemu_get_be32s(f
, &env
->sysenter_esp
);
1605 qemu_get_be32s(f
, &env
->sysenter_eip
);
1607 qemu_get_be32s(f
, &env
->cr
[0]);
1608 qemu_get_be32s(f
, &env
->cr
[2]);
1609 qemu_get_be32s(f
, &env
->cr
[3]);
1610 qemu_get_be32s(f
, &env
->cr
[4]);
1612 for(i
= 0; i
< 8; i
++)
1613 qemu_get_be32s(f
, &env
->dr
[i
]);
1616 qemu_get_be32s(f
, &env
->a20_mask
);
1618 /* XXX: compute hflags from scratch, except for CPL and IIF */
1619 env
->hflags
= hflags
;
1624 #elif defined(TARGET_PPC)
1625 void cpu_save(QEMUFile
*f
, void *opaque
)
1629 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1635 #warning No CPU save/restore functions
1639 /***********************************************************/
1640 /* ram save/restore */
1642 /* we just avoid storing empty pages */
1643 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1648 for(i
= 1; i
< len
; i
++) {
1652 qemu_put_byte(f
, 1);
1653 qemu_put_byte(f
, v
);
1656 qemu_put_byte(f
, 0);
1657 qemu_put_buffer(f
, buf
, len
);
1660 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1664 v
= qemu_get_byte(f
);
1667 if (qemu_get_buffer(f
, buf
, len
) != len
)
1671 v
= qemu_get_byte(f
);
1672 memset(buf
, v
, len
);
1680 static void ram_save(QEMUFile
*f
, void *opaque
)
1683 qemu_put_be32(f
, phys_ram_size
);
1684 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1685 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1689 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1693 if (version_id
!= 1)
1695 if (qemu_get_be32(f
) != phys_ram_size
)
1697 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1698 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1705 /***********************************************************/
1706 /* main execution loop */
1708 void gui_update(void *opaque
)
1710 display_state
.dpy_refresh(&display_state
);
1711 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1714 /* XXX: support several handlers */
1715 VMStopHandler
*vm_stop_cb
;
1716 VMStopHandler
*vm_stop_opaque
;
1718 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1721 vm_stop_opaque
= opaque
;
1725 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1738 void vm_stop(int reason
)
1741 cpu_disable_ticks();
1745 vm_stop_cb(vm_stop_opaque
, reason
);
1754 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1755 IOHandlerRecord
*ioh
, *ioh_next
;
1760 CPUState
*env
= global_env
;
1764 ret
= cpu_exec(env
);
1765 if (reset_requested
) {
1766 ret
= EXCP_INTERRUPT
;
1769 if (ret
== EXCP_DEBUG
) {
1770 vm_stop(EXCP_DEBUG
);
1772 /* if hlt instruction, we wait until the next IRQ */
1773 /* XXX: use timeout computed from timers */
1774 if (ret
== EXCP_HLT
)
1787 /* poll any events */
1788 /* XXX: separate device handlers from system ones */
1790 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1791 if (!ioh
->fd_can_read
) {
1794 pf
->events
= POLLIN
;
1798 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1800 if (max_size
> sizeof(buf
))
1801 max_size
= sizeof(buf
);
1803 pf
->events
= POLLIN
;
1810 ioh
->max_size
= max_size
;
1813 ret
= poll(ufds
, pf
- ufds
, timeout
);
1815 /* XXX: better handling of removal */
1816 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1817 ioh_next
= ioh
->next
;
1820 if (pf
->revents
& POLLIN
) {
1821 if (ioh
->max_size
== 0) {
1822 /* just a read event */
1823 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1825 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1827 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1828 } else if (errno
!= -EAGAIN
) {
1829 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1837 #if defined(CONFIG_SLIRP)
1838 /* XXX: merge with poll() */
1840 fd_set rfds
, wfds
, xfds
;
1848 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1851 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1853 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1861 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1862 qemu_get_clock(vm_clock
));
1864 if (audio_enabled
) {
1865 /* XXX: add explicit timer */
1869 /* run dma transfers, if any */
1873 /* real time timers */
1874 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1875 qemu_get_clock(rt_clock
));
1877 cpu_disable_ticks();
1883 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
1884 "usage: %s [options] [disk_image]\n"
1886 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1888 "Standard options:\n"
1889 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1890 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1891 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1892 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1893 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1894 "-snapshot write to temporary files instead of disk image files\n"
1895 "-m megs set virtual RAM size to megs MB\n"
1896 "-nographic disable graphical output and redirect serial I/Os to console\n"
1897 "-enable-audio enable audio support\n"
1899 "Network options:\n"
1900 "-nics n simulate 'n' network cards [default=1]\n"
1901 "-macaddr addr set the mac address of the first interface\n"
1902 "-n script set tap/tun network init script [default=%s]\n"
1903 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1905 "-user-net use user mode network stack [default if no tap/tun script]\n"
1907 "-dummy-net use dummy network stack\n"
1909 "Linux boot specific:\n"
1910 "-kernel bzImage use 'bzImage' as kernel image\n"
1911 "-append cmdline use 'cmdline' as kernel command line\n"
1912 "-initrd file use 'file' as initial ram disk\n"
1914 "Debug/Expert options:\n"
1915 "-s wait gdb connection to port %d\n"
1916 "-p port change gdb connection port\n"
1917 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1918 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1919 "-L path set the directory for the BIOS and VGA BIOS\n"
1920 #ifdef USE_CODE_COPY
1921 "-no-code-copy disable code copy acceleration\n"
1925 "During emulation, use C-a h to get terminal commands:\n",
1926 #ifdef CONFIG_SOFTMMU
1931 DEFAULT_NETWORK_SCRIPT
,
1932 DEFAULT_GDBSTUB_PORT
,
1935 #ifndef CONFIG_SOFTMMU
1937 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1938 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1944 struct option long_options
[] = {
1945 { "initrd", 1, NULL
, 0, },
1946 { "hda", 1, NULL
, 0, },
1947 { "hdb", 1, NULL
, 0, },
1948 { "snapshot", 0, NULL
, 0, },
1949 { "hdachs", 1, NULL
, 0, },
1950 { "nographic", 0, NULL
, 0, },
1951 { "kernel", 1, NULL
, 0, },
1952 { "append", 1, NULL
, 0, },
1953 { "tun-fd", 1, NULL
, 0, },
1954 { "hdc", 1, NULL
, 0, },
1955 { "hdd", 1, NULL
, 0, },
1956 { "cdrom", 1, NULL
, 0, },
1957 { "boot", 1, NULL
, 0, },
1958 { "fda", 1, NULL
, 0, },
1959 { "fdb", 1, NULL
, 0, },
1960 { "no-code-copy", 0, NULL
, 0 },
1961 { "nics", 1, NULL
, 0 },
1962 { "macaddr", 1, NULL
, 0 },
1963 { "user-net", 0, NULL
, 0 },
1964 { "dummy-net", 0, NULL
, 0 },
1965 { "enable-audio", 0, NULL
, 0 },
1966 { NULL
, 0, NULL
, 0 },
1969 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1971 /* this stack is only used during signal handling */
1972 #define SIGNAL_STACK_SIZE 32768
1974 static uint8_t *signal_stack
;
1978 #define NET_IF_TUN 0
1979 #define NET_IF_USER 1
1980 #define NET_IF_DUMMY 2
1982 int main(int argc
, char **argv
)
1984 #ifdef CONFIG_GDBSTUB
1985 int use_gdbstub
, gdbstub_port
;
1987 int c
, i
, long_index
, has_cdrom
;
1988 int snapshot
, linux_boot
;
1990 const char *initrd_filename
;
1991 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
1992 const char *kernel_filename
, *kernel_cmdline
;
1993 DisplayState
*ds
= &display_state
;
1994 int cyls
, heads
, secs
;
1995 int start_emulation
= 1;
1997 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
1999 #if !defined(CONFIG_SOFTMMU)
2000 /* we never want that malloc() uses mmap() */
2001 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2003 initrd_filename
= NULL
;
2004 for(i
= 0; i
< MAX_FD
; i
++)
2005 fd_filename
[i
] = NULL
;
2006 for(i
= 0; i
< MAX_DISKS
; i
++)
2007 hd_filename
[i
] = NULL
;
2008 ram_size
= 32 * 1024 * 1024;
2009 vga_ram_size
= VGA_RAM_SIZE
;
2010 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2011 #ifdef CONFIG_GDBSTUB
2013 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2017 kernel_filename
= NULL
;
2018 kernel_cmdline
= "";
2020 cyls
= heads
= secs
= 0;
2025 /* default mac address of the first network interface */
2035 c
= getopt_long_only(argc
, argv
, "hm:d:n:sp:L:S", long_options
, &long_index
);
2040 switch(long_index
) {
2042 initrd_filename
= optarg
;
2045 hd_filename
[0] = optarg
;
2048 hd_filename
[1] = optarg
;
2057 cyls
= strtol(p
, (char **)&p
, 0);
2061 heads
= strtol(p
, (char **)&p
, 0);
2065 secs
= strtol(p
, (char **)&p
, 0);
2076 kernel_filename
= optarg
;
2079 kernel_cmdline
= optarg
;
2085 if (nb_tun_fds
< MAX_NICS
) {
2086 fd
= strtol(optarg
, (char **)&p
, 0);
2088 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2091 tun_fds
[nb_tun_fds
++] = fd
;
2096 hd_filename
[2] = optarg
;
2100 hd_filename
[3] = optarg
;
2103 hd_filename
[2] = optarg
;
2107 boot_device
= optarg
[0];
2108 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2109 boot_device
!= 'c' && boot_device
!= 'd') {
2110 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2115 fd_filename
[0] = optarg
;
2118 fd_filename
[1] = optarg
;
2121 code_copy_enabled
= 0;
2124 nb_nics
= atoi(optarg
);
2125 if (nb_nics
< 1 || nb_nics
> MAX_NICS
) {
2126 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2135 for(i
= 0; i
< 6; i
++) {
2136 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2143 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2152 net_if_type
= NET_IF_USER
;
2155 net_if_type
= NET_IF_DUMMY
;
2166 ram_size
= atoi(optarg
) * 1024 * 1024;
2169 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2170 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2171 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2180 mask
= cpu_str_to_log_mask(optarg
);
2182 printf("Log items (comma separated):\n");
2183 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2184 printf("%-10s %s\n", item
->name
, item
->help
);
2192 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2194 #ifdef CONFIG_GDBSTUB
2199 gdbstub_port
= atoi(optarg
);
2206 start_emulation
= 0;
2211 if (optind
< argc
) {
2212 hd_filename
[0] = argv
[optind
++];
2215 linux_boot
= (kernel_filename
!= NULL
);
2217 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2218 fd_filename
[0] == '\0')
2221 /* boot to cd by default if no hard disk */
2222 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2223 if (fd_filename
[0] != '\0')
2229 #if !defined(CONFIG_SOFTMMU)
2230 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2232 static uint8_t stdout_buf
[4096];
2233 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2236 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2239 /* init host network redirectors */
2240 if (net_if_type
== -1) {
2241 net_if_type
= NET_IF_TUN
;
2242 #if defined(CONFIG_SLIRP)
2243 if (access(network_script
, R_OK
) < 0) {
2244 net_if_type
= NET_IF_USER
;
2249 for(i
= 0; i
< nb_nics
; i
++) {
2250 NetDriverState
*nd
= &nd_table
[i
];
2252 /* init virtual mac address */
2253 nd
->macaddr
[0] = macaddr
[0];
2254 nd
->macaddr
[1] = macaddr
[1];
2255 nd
->macaddr
[2] = macaddr
[2];
2256 nd
->macaddr
[3] = macaddr
[3];
2257 nd
->macaddr
[4] = macaddr
[4];
2258 nd
->macaddr
[5] = macaddr
[5] + i
;
2259 switch(net_if_type
) {
2260 #if defined(CONFIG_SLIRP)
2265 #if !defined(_WIN32)
2267 if (i
< nb_tun_fds
) {
2268 net_fd_init(nd
, tun_fds
[i
]);
2270 if (net_tun_init(nd
) < 0)
2282 /* init the memory */
2283 phys_ram_size
= ram_size
+ vga_ram_size
;
2285 #ifdef CONFIG_SOFTMMU
2287 /* mallocs are always aligned on BSD. */
2288 phys_ram_base
= malloc(phys_ram_size
);
2290 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2292 if (!phys_ram_base
) {
2293 fprintf(stderr
, "Could not allocate physical memory\n");
2297 /* as we must map the same page at several addresses, we must use
2302 tmpdir
= getenv("QEMU_TMPDIR");
2305 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2306 if (mkstemp(phys_ram_file
) < 0) {
2307 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2311 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2312 if (phys_ram_fd
< 0) {
2313 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2317 ftruncate(phys_ram_fd
, phys_ram_size
);
2318 unlink(phys_ram_file
);
2319 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2321 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2323 if (phys_ram_base
== MAP_FAILED
) {
2324 fprintf(stderr
, "Could not map physical memory\n");
2330 /* we always create the cdrom drive, even if no disk is there */
2332 bs_table
[2] = bdrv_new("cdrom");
2333 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2336 /* open the virtual block devices */
2337 for(i
= 0; i
< MAX_DISKS
; i
++) {
2338 if (hd_filename
[i
]) {
2341 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2342 bs_table
[i
] = bdrv_new(buf
);
2344 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2345 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2349 if (i
== 0 && cyls
!= 0)
2350 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2354 /* we always create at least one floppy disk */
2355 fd_table
[0] = bdrv_new("fda");
2356 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2358 for(i
= 0; i
< MAX_FD
; i
++) {
2359 if (fd_filename
[i
]) {
2362 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2363 fd_table
[i
] = bdrv_new(buf
);
2364 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2366 if (fd_filename
[i
] != '\0') {
2367 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2368 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2376 /* init CPU state */
2379 cpu_single_env
= env
;
2381 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2382 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2383 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2386 cpu_calibrate_ticks();
2390 dumb_display_init(ds
);
2393 sdl_display_init(ds
);
2395 dumb_display_init(ds
);
2399 /* setup cpu signal handlers for MMU / self modifying code handling */
2400 #if !defined(CONFIG_SOFTMMU)
2402 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2405 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2406 stk
.ss_sp
= signal_stack
;
2407 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2410 if (sigaltstack(&stk
, NULL
) < 0) {
2411 perror("sigaltstack");
2417 struct sigaction act
;
2419 sigfillset(&act
.sa_mask
);
2420 act
.sa_flags
= SA_SIGINFO
;
2421 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2422 act
.sa_flags
|= SA_ONSTACK
;
2424 act
.sa_sigaction
= host_segv_handler
;
2425 sigaction(SIGSEGV
, &act
, NULL
);
2426 sigaction(SIGBUS
, &act
, NULL
);
2427 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2428 sigaction(SIGFPE
, &act
, NULL
);
2435 struct sigaction act
;
2436 sigfillset(&act
.sa_mask
);
2438 act
.sa_handler
= SIG_IGN
;
2439 sigaction(SIGPIPE
, &act
, NULL
);
2444 #if defined(TARGET_I386)
2445 pc_init(ram_size
, vga_ram_size
, boot_device
,
2446 ds
, fd_filename
, snapshot
,
2447 kernel_filename
, kernel_cmdline
, initrd_filename
);
2448 #elif defined(TARGET_PPC)
2449 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2450 ds
, fd_filename
, snapshot
,
2451 kernel_filename
, kernel_cmdline
, initrd_filename
);
2454 /* launched after the device init so that it can display or not a
2458 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2459 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2461 #ifdef CONFIG_GDBSTUB
2463 if (gdbserver_start(gdbstub_port
) < 0) {
2464 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2468 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2472 if (start_emulation
)