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)
96 #define DEFAULT_RAM_SIZE 32
99 #define GUI_REFRESH_INTERVAL 30
101 /* XXX: use a two level table to limit memory usage */
102 #define MAX_IOPORTS 65536
104 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
105 char phys_ram_file
[1024];
106 CPUState
*global_env
;
107 CPUState
*cpu_single_env
;
108 void *ioport_opaque
[MAX_IOPORTS
];
109 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
110 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
111 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
114 static DisplayState display_state
;
116 int64_t ticks_per_sec
;
117 int boot_device
= 'c';
119 static char network_script
[1024];
120 int pit_min_timer_count
= 0;
122 NetDriverState nd_table
[MAX_NICS
];
123 SerialState
*serial_console
;
124 QEMUTimer
*gui_timer
;
126 int audio_enabled
= 0;
129 /***********************************************************/
130 /* x86 ISA bus support */
132 target_phys_addr_t isa_mem_base
= 0;
134 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
136 #ifdef DEBUG_UNUSED_IOPORT
137 fprintf(stderr
, "inb: port=0x%04x\n", address
);
142 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
144 #ifdef DEBUG_UNUSED_IOPORT
145 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
149 /* default is to make two byte accesses */
150 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
153 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
154 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
155 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
159 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
161 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
162 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
163 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
166 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
168 #ifdef DEBUG_UNUSED_IOPORT
169 fprintf(stderr
, "inl: port=0x%04x\n", address
);
174 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
176 #ifdef DEBUG_UNUSED_IOPORT
177 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
181 void init_ioports(void)
185 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
186 ioport_read_table
[0][i
] = default_ioport_readb
;
187 ioport_write_table
[0][i
] = default_ioport_writeb
;
188 ioport_read_table
[1][i
] = default_ioport_readw
;
189 ioport_write_table
[1][i
] = default_ioport_writew
;
190 ioport_read_table
[2][i
] = default_ioport_readl
;
191 ioport_write_table
[2][i
] = default_ioport_writel
;
195 /* size is the word size in byte */
196 int register_ioport_read(int start
, int length
, int size
,
197 IOPortReadFunc
*func
, void *opaque
)
203 } else if (size
== 2) {
205 } else if (size
== 4) {
208 hw_error("register_ioport_read: invalid size");
211 for(i
= start
; i
< start
+ length
; i
+= size
) {
212 ioport_read_table
[bsize
][i
] = func
;
213 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
214 hw_error("register_ioport_read: invalid opaque");
215 ioport_opaque
[i
] = opaque
;
220 /* size is the word size in byte */
221 int register_ioport_write(int start
, int length
, int size
,
222 IOPortWriteFunc
*func
, void *opaque
)
228 } else if (size
== 2) {
230 } else if (size
== 4) {
233 hw_error("register_ioport_write: invalid size");
236 for(i
= start
; i
< start
+ length
; i
+= size
) {
237 ioport_write_table
[bsize
][i
] = func
;
238 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
239 hw_error("register_ioport_read: invalid opaque");
240 ioport_opaque
[i
] = opaque
;
245 void isa_unassign_ioport(int start
, int length
)
249 for(i
= start
; i
< start
+ length
; i
++) {
250 ioport_read_table
[0][i
] = default_ioport_readb
;
251 ioport_read_table
[1][i
] = default_ioport_readw
;
252 ioport_read_table
[2][i
] = default_ioport_readl
;
254 ioport_write_table
[0][i
] = default_ioport_writeb
;
255 ioport_write_table
[1][i
] = default_ioport_writew
;
256 ioport_write_table
[2][i
] = default_ioport_writel
;
260 void pstrcpy(char *buf
, int buf_size
, const char *str
)
270 if (c
== 0 || q
>= buf
+ buf_size
- 1)
277 /* strcat and truncate. */
278 char *pstrcat(char *buf
, int buf_size
, const char *s
)
283 pstrcpy(buf
+ len
, buf_size
- len
, s
);
287 /* return the size or -1 if error */
288 int load_image(const char *filename
, uint8_t *addr
)
291 fd
= open(filename
, O_RDONLY
| O_BINARY
);
294 size
= lseek(fd
, 0, SEEK_END
);
295 lseek(fd
, 0, SEEK_SET
);
296 if (read(fd
, addr
, size
) != size
) {
304 void cpu_outb(CPUState
*env
, int addr
, int val
)
307 if (loglevel
& CPU_LOG_IOPORT
)
308 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
310 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
313 void cpu_outw(CPUState
*env
, int addr
, int val
)
316 if (loglevel
& CPU_LOG_IOPORT
)
317 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
319 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
322 void cpu_outl(CPUState
*env
, int addr
, int val
)
325 if (loglevel
& CPU_LOG_IOPORT
)
326 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
328 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
331 int cpu_inb(CPUState
*env
, int addr
)
334 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
336 if (loglevel
& CPU_LOG_IOPORT
)
337 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
342 int cpu_inw(CPUState
*env
, int addr
)
345 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
347 if (loglevel
& CPU_LOG_IOPORT
)
348 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
353 int cpu_inl(CPUState
*env
, int addr
)
356 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
358 if (loglevel
& CPU_LOG_IOPORT
)
359 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
364 /***********************************************************/
365 void hw_error(const char *fmt
, ...)
370 fprintf(stderr
, "qemu: hardware error: ");
371 vfprintf(stderr
, fmt
, ap
);
372 fprintf(stderr
, "\n");
374 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
376 cpu_dump_state(global_env
, stderr
, 0);
382 /***********************************************************/
385 #if defined(__powerpc__)
387 static inline uint32_t get_tbl(void)
390 asm volatile("mftb %0" : "=r" (tbl
));
394 static inline uint32_t get_tbu(void)
397 asm volatile("mftbu %0" : "=r" (tbl
));
401 int64_t cpu_get_real_ticks(void)
404 /* NOTE: we test if wrapping has occurred */
410 return ((int64_t)h
<< 32) | l
;
413 #elif defined(__i386__)
415 int64_t cpu_get_real_ticks(void)
418 asm volatile ("rdtsc" : "=A" (val
));
422 #elif defined(__x86_64__)
424 int64_t cpu_get_real_ticks(void)
428 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
436 #error unsupported CPU
439 static int64_t cpu_ticks_offset
;
440 static int cpu_ticks_enabled
;
442 static inline int64_t cpu_get_ticks(void)
444 if (!cpu_ticks_enabled
) {
445 return cpu_ticks_offset
;
447 return cpu_get_real_ticks() + cpu_ticks_offset
;
451 /* enable cpu_get_ticks() */
452 void cpu_enable_ticks(void)
454 if (!cpu_ticks_enabled
) {
455 cpu_ticks_offset
-= cpu_get_real_ticks();
456 cpu_ticks_enabled
= 1;
460 /* disable cpu_get_ticks() : the clock is stopped. You must not call
461 cpu_get_ticks() after that. */
462 void cpu_disable_ticks(void)
464 if (cpu_ticks_enabled
) {
465 cpu_ticks_offset
= cpu_get_ticks();
466 cpu_ticks_enabled
= 0;
470 static int64_t get_clock(void)
475 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
478 gettimeofday(&tv
, NULL
);
479 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
483 void cpu_calibrate_ticks(void)
488 ticks
= cpu_get_real_ticks();
494 usec
= get_clock() - usec
;
495 ticks
= cpu_get_real_ticks() - ticks
;
496 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
499 /* compute with 96 bit intermediate result: (a*b)/c */
500 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
505 #ifdef WORDS_BIGENDIAN
515 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
516 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
519 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
523 #define QEMU_TIMER_REALTIME 0
524 #define QEMU_TIMER_VIRTUAL 1
528 /* XXX: add frequency */
536 struct QEMUTimer
*next
;
542 static QEMUTimer
*active_timers
[2];
544 static MMRESULT timerID
;
546 /* frequency of the times() clock tick */
547 static int timer_freq
;
550 QEMUClock
*qemu_new_clock(int type
)
553 clock
= qemu_mallocz(sizeof(QEMUClock
));
560 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
564 ts
= qemu_mallocz(sizeof(QEMUTimer
));
571 void qemu_free_timer(QEMUTimer
*ts
)
576 /* stop a timer, but do not dealloc it */
577 void qemu_del_timer(QEMUTimer
*ts
)
581 /* NOTE: this code must be signal safe because
582 qemu_timer_expired() can be called from a signal. */
583 pt
= &active_timers
[ts
->clock
->type
];
596 /* modify the current timer so that it will be fired when current_time
597 >= expire_time. The corresponding callback will be called. */
598 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
604 /* add the timer in the sorted list */
605 /* NOTE: this code must be signal safe because
606 qemu_timer_expired() can be called from a signal. */
607 pt
= &active_timers
[ts
->clock
->type
];
612 if (t
->expire_time
> expire_time
)
616 ts
->expire_time
= expire_time
;
621 int qemu_timer_pending(QEMUTimer
*ts
)
624 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
631 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
635 return (timer_head
->expire_time
<= current_time
);
638 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
644 if (ts
->expire_time
> current_time
)
646 /* remove timer from the list before calling the callback */
647 *ptimer_head
= ts
->next
;
650 /* run the callback (the timer list can be modified) */
655 int64_t qemu_get_clock(QEMUClock
*clock
)
657 switch(clock
->type
) {
658 case QEMU_TIMER_REALTIME
:
660 return GetTickCount();
665 /* Note that using gettimeofday() is not a good solution
666 for timers because its value change when the date is
668 if (timer_freq
== 100) {
669 return times(&tp
) * 10;
671 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
676 case QEMU_TIMER_VIRTUAL
:
677 return cpu_get_ticks();
682 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
684 uint64_t expire_time
;
686 if (qemu_timer_pending(ts
)) {
687 expire_time
= ts
->expire_time
;
691 qemu_put_be64(f
, expire_time
);
694 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
696 uint64_t expire_time
;
698 expire_time
= qemu_get_be64(f
);
699 if (expire_time
!= -1) {
700 qemu_mod_timer(ts
, expire_time
);
706 static void timer_save(QEMUFile
*f
, void *opaque
)
708 if (cpu_ticks_enabled
) {
709 hw_error("cannot save state if virtual timers are running");
711 qemu_put_be64s(f
, &cpu_ticks_offset
);
712 qemu_put_be64s(f
, &ticks_per_sec
);
715 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
719 if (cpu_ticks_enabled
) {
722 qemu_get_be64s(f
, &cpu_ticks_offset
);
723 qemu_get_be64s(f
, &ticks_per_sec
);
728 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
729 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
731 static void host_alarm_handler(int host_signum
)
734 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
735 qemu_get_clock(vm_clock
)) ||
736 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
737 qemu_get_clock(rt_clock
))) {
738 /* stop the cpu because a timer occured */
739 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
745 #if defined(__linux__)
747 #define RTC_FREQ 1024
751 static int start_rtc_timer(void)
753 rtc_fd
= open("/dev/rtc", O_RDONLY
);
756 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
757 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
758 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
759 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
762 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
767 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
773 static int start_rtc_timer(void)
778 #endif /* !defined(__linux__) */
780 #endif /* !defined(_WIN32) */
782 static void init_timers(void)
784 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
785 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
790 timerID
= timeSetEvent(10, // interval (ms)
792 host_alarm_handler
, // function
793 (DWORD
)&count
, // user parameter
794 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
796 perror("failed timer alarm");
800 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
803 struct sigaction act
;
804 struct itimerval itv
;
806 /* get times() syscall frequency */
807 timer_freq
= sysconf(_SC_CLK_TCK
);
810 sigfillset(&act
.sa_mask
);
812 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
813 act
.sa_flags
|= SA_ONSTACK
;
815 act
.sa_handler
= host_alarm_handler
;
816 sigaction(SIGALRM
, &act
, NULL
);
818 itv
.it_interval
.tv_sec
= 0;
819 itv
.it_interval
.tv_usec
= 1000;
820 itv
.it_value
.tv_sec
= 0;
821 itv
.it_value
.tv_usec
= 10 * 1000;
822 setitimer(ITIMER_REAL
, &itv
, NULL
);
823 /* we probe the tick duration of the kernel to inform the user if
824 the emulated kernel requested a too high timer frequency */
825 getitimer(ITIMER_REAL
, &itv
);
827 if (itv
.it_interval
.tv_usec
> 1000) {
828 /* try to use /dev/rtc to have a faster timer */
829 if (start_rtc_timer() < 0)
832 itv
.it_interval
.tv_sec
= 0;
833 itv
.it_interval
.tv_usec
= 0;
834 itv
.it_value
.tv_sec
= 0;
835 itv
.it_value
.tv_usec
= 0;
836 setitimer(ITIMER_REAL
, &itv
, NULL
);
839 sigaction(SIGIO
, &act
, NULL
);
840 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
841 fcntl(rtc_fd
, F_SETOWN
, getpid());
844 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
851 void quit_timers(void)
854 timeKillEvent(timerID
);
858 /***********************************************************/
863 int serial_open_device(void)
870 int serial_open_device(void)
872 char slave_name
[1024];
873 int master_fd
, slave_fd
;
875 if (serial_console
== NULL
&& nographic
) {
876 /* use console for serial port */
879 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
880 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
883 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
890 /***********************************************************/
891 /* Linux network device redirectors */
893 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
897 for(i
=0;i
<size
;i
+=16) {
901 fprintf(f
, "%08x ", i
);
904 fprintf(f
, " %02x", buf
[i
+j
]);
911 if (c
< ' ' || c
> '~')
919 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
921 nd
->send_packet(nd
, buf
, size
);
924 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
925 IOReadHandler
*fd_read
, void *opaque
)
927 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
930 /* dummy network adapter */
932 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
936 static void dummy_add_read_packet(NetDriverState
*nd
,
937 IOCanRWHandler
*fd_can_read
,
938 IOReadHandler
*fd_read
, void *opaque
)
942 static int net_dummy_init(NetDriverState
*nd
)
944 nd
->send_packet
= dummy_send_packet
;
945 nd
->add_read_packet
= dummy_add_read_packet
;
946 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
950 #if defined(CONFIG_SLIRP)
952 /* slirp network adapter */
954 static void *slirp_fd_opaque
;
955 static IOCanRWHandler
*slirp_fd_can_read
;
956 static IOReadHandler
*slirp_fd_read
;
957 static int slirp_inited
;
959 int slirp_can_output(void)
961 return slirp_fd_can_read(slirp_fd_opaque
);
964 void slirp_output(const uint8_t *pkt
, int pkt_len
)
968 hex_dump(stdout
, pkt
, pkt_len
);
970 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
973 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
977 hex_dump(stdout
, buf
, size
);
979 slirp_input(buf
, size
);
982 static void slirp_add_read_packet(NetDriverState
*nd
,
983 IOCanRWHandler
*fd_can_read
,
984 IOReadHandler
*fd_read
, void *opaque
)
986 slirp_fd_opaque
= opaque
;
987 slirp_fd_can_read
= fd_can_read
;
988 slirp_fd_read
= fd_read
;
991 static int net_slirp_init(NetDriverState
*nd
)
997 nd
->send_packet
= slirp_send_packet
;
998 nd
->add_read_packet
= slirp_add_read_packet
;
999 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1003 #endif /* CONFIG_SLIRP */
1005 #if !defined(_WIN32)
1007 static int tun_open(char *ifname
, int ifname_size
)
1013 fd
= open("/dev/tap", O_RDWR
);
1015 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1020 dev
= devname(s
.st_rdev
, S_IFCHR
);
1021 pstrcpy(ifname
, ifname_size
, dev
);
1023 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1027 static int tun_open(char *ifname
, int ifname_size
)
1032 fd
= open("/dev/net/tun", O_RDWR
);
1034 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1037 memset(&ifr
, 0, sizeof(ifr
));
1038 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1039 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1040 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1042 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1046 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1047 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1048 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1053 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1055 write(nd
->fd
, buf
, size
);
1058 static void tun_add_read_packet(NetDriverState
*nd
,
1059 IOCanRWHandler
*fd_can_read
,
1060 IOReadHandler
*fd_read
, void *opaque
)
1062 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1065 static int net_tun_init(NetDriverState
*nd
)
1071 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1075 /* try to launch network init script */
1080 *parg
++ = network_script
;
1081 *parg
++ = nd
->ifname
;
1083 execv(network_script
, args
);
1086 while (waitpid(pid
, &status
, 0) != pid
);
1087 if (!WIFEXITED(status
) ||
1088 WEXITSTATUS(status
) != 0) {
1089 fprintf(stderr
, "%s: could not launch network script\n",
1093 nd
->send_packet
= tun_send_packet
;
1094 nd
->add_read_packet
= tun_add_read_packet
;
1098 static int net_fd_init(NetDriverState
*nd
, int fd
)
1101 nd
->send_packet
= tun_send_packet
;
1102 nd
->add_read_packet
= tun_add_read_packet
;
1103 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1107 #endif /* !_WIN32 */
1109 /***********************************************************/
1114 static void term_exit(void)
1118 static void term_init(void)
1124 /* init terminal so that we can grab keys */
1125 static struct termios oldtty
;
1127 static void term_exit(void)
1129 tcsetattr (0, TCSANOW
, &oldtty
);
1132 static void term_init(void)
1136 tcgetattr (0, &tty
);
1139 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1140 |INLCR
|IGNCR
|ICRNL
|IXON
);
1141 tty
.c_oflag
|= OPOST
;
1142 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1143 /* if graphical mode, we allow Ctrl-C handling */
1145 tty
.c_lflag
&= ~ISIG
;
1146 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1149 tty
.c_cc
[VTIME
] = 0;
1151 tcsetattr (0, TCSANOW
, &tty
);
1155 fcntl(0, F_SETFL
, O_NONBLOCK
);
1160 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1164 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1168 static void dumb_refresh(DisplayState
*ds
)
1170 vga_update_display();
1173 void dumb_display_init(DisplayState
*ds
)
1178 ds
->dpy_update
= dumb_update
;
1179 ds
->dpy_resize
= dumb_resize
;
1180 ds
->dpy_refresh
= dumb_refresh
;
1183 #if !defined(CONFIG_SOFTMMU)
1184 /***********************************************************/
1185 /* cpu signal handler */
1186 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1189 if (cpu_signal_handler(host_signum
, info
, puc
))
1196 /***********************************************************/
1199 #define MAX_IO_HANDLERS 64
1201 typedef struct IOHandlerRecord
{
1203 IOCanRWHandler
*fd_can_read
;
1204 IOReadHandler
*fd_read
;
1206 /* temporary data */
1209 struct IOHandlerRecord
*next
;
1212 static IOHandlerRecord
*first_io_handler
;
1214 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1215 IOReadHandler
*fd_read
, void *opaque
)
1217 IOHandlerRecord
*ioh
;
1219 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1223 ioh
->fd_can_read
= fd_can_read
;
1224 ioh
->fd_read
= fd_read
;
1225 ioh
->opaque
= opaque
;
1226 ioh
->next
= first_io_handler
;
1227 first_io_handler
= ioh
;
1231 void qemu_del_fd_read_handler(int fd
)
1233 IOHandlerRecord
**pioh
, *ioh
;
1235 pioh
= &first_io_handler
;
1240 if (ioh
->fd
== fd
) {
1248 /***********************************************************/
1249 /* savevm/loadvm support */
1251 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1253 fwrite(buf
, 1, size
, f
);
1256 void qemu_put_byte(QEMUFile
*f
, int v
)
1261 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1263 qemu_put_byte(f
, v
>> 8);
1264 qemu_put_byte(f
, v
);
1267 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1269 qemu_put_byte(f
, v
>> 24);
1270 qemu_put_byte(f
, v
>> 16);
1271 qemu_put_byte(f
, v
>> 8);
1272 qemu_put_byte(f
, v
);
1275 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1277 qemu_put_be32(f
, v
>> 32);
1278 qemu_put_be32(f
, v
);
1281 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1283 return fread(buf
, 1, size
, f
);
1286 int qemu_get_byte(QEMUFile
*f
)
1296 unsigned int qemu_get_be16(QEMUFile
*f
)
1299 v
= qemu_get_byte(f
) << 8;
1300 v
|= qemu_get_byte(f
);
1304 unsigned int qemu_get_be32(QEMUFile
*f
)
1307 v
= qemu_get_byte(f
) << 24;
1308 v
|= qemu_get_byte(f
) << 16;
1309 v
|= qemu_get_byte(f
) << 8;
1310 v
|= qemu_get_byte(f
);
1314 uint64_t qemu_get_be64(QEMUFile
*f
)
1317 v
= (uint64_t)qemu_get_be32(f
) << 32;
1318 v
|= qemu_get_be32(f
);
1322 int64_t qemu_ftell(QEMUFile
*f
)
1327 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1329 if (fseek(f
, pos
, whence
) < 0)
1334 typedef struct SaveStateEntry
{
1338 SaveStateHandler
*save_state
;
1339 LoadStateHandler
*load_state
;
1341 struct SaveStateEntry
*next
;
1344 static SaveStateEntry
*first_se
;
1346 int register_savevm(const char *idstr
,
1349 SaveStateHandler
*save_state
,
1350 LoadStateHandler
*load_state
,
1353 SaveStateEntry
*se
, **pse
;
1355 se
= qemu_malloc(sizeof(SaveStateEntry
));
1358 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1359 se
->instance_id
= instance_id
;
1360 se
->version_id
= version_id
;
1361 se
->save_state
= save_state
;
1362 se
->load_state
= load_state
;
1363 se
->opaque
= opaque
;
1366 /* add at the end of list */
1368 while (*pse
!= NULL
)
1369 pse
= &(*pse
)->next
;
1374 #define QEMU_VM_FILE_MAGIC 0x5145564d
1375 #define QEMU_VM_FILE_VERSION 0x00000001
1377 int qemu_savevm(const char *filename
)
1381 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1383 saved_vm_running
= vm_running
;
1386 f
= fopen(filename
, "wb");
1392 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1393 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1395 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1397 len
= strlen(se
->idstr
);
1398 qemu_put_byte(f
, len
);
1399 qemu_put_buffer(f
, se
->idstr
, len
);
1401 qemu_put_be32(f
, se
->instance_id
);
1402 qemu_put_be32(f
, se
->version_id
);
1404 /* record size: filled later */
1406 qemu_put_be32(f
, 0);
1408 se
->save_state(f
, se
->opaque
);
1410 /* fill record size */
1412 len
= ftell(f
) - len_pos
- 4;
1413 fseek(f
, len_pos
, SEEK_SET
);
1414 qemu_put_be32(f
, len
);
1415 fseek(f
, cur_pos
, SEEK_SET
);
1421 if (saved_vm_running
)
1426 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1430 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1431 if (!strcmp(se
->idstr
, idstr
) &&
1432 instance_id
== se
->instance_id
)
1438 int qemu_loadvm(const char *filename
)
1442 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1443 int saved_vm_running
;
1447 saved_vm_running
= vm_running
;
1450 f
= fopen(filename
, "rb");
1456 v
= qemu_get_be32(f
);
1457 if (v
!= QEMU_VM_FILE_MAGIC
)
1459 v
= qemu_get_be32(f
);
1460 if (v
!= QEMU_VM_FILE_VERSION
) {
1467 #if defined (DO_TB_FLUSH)
1468 tb_flush(global_env
);
1470 len
= qemu_get_byte(f
);
1473 qemu_get_buffer(f
, idstr
, len
);
1475 instance_id
= qemu_get_be32(f
);
1476 version_id
= qemu_get_be32(f
);
1477 record_len
= qemu_get_be32(f
);
1479 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1480 idstr
, instance_id
, version_id
, record_len
);
1483 se
= find_se(idstr
, instance_id
);
1485 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1486 instance_id
, idstr
);
1488 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1490 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1491 instance_id
, idstr
);
1494 /* always seek to exact end of record */
1495 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1500 if (saved_vm_running
)
1505 /***********************************************************/
1506 /* cpu save/restore */
1508 #if defined(TARGET_I386)
1510 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1512 qemu_put_be32(f
, (uint32_t)dt
->base
);
1513 qemu_put_be32(f
, dt
->limit
);
1514 qemu_put_be32(f
, dt
->flags
);
1517 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1519 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1520 dt
->limit
= qemu_get_be32(f
);
1521 dt
->flags
= qemu_get_be32(f
);
1524 void cpu_save(QEMUFile
*f
, void *opaque
)
1526 CPUState
*env
= opaque
;
1527 uint16_t fptag
, fpus
, fpuc
;
1531 for(i
= 0; i
< 8; i
++)
1532 qemu_put_be32s(f
, &env
->regs
[i
]);
1533 qemu_put_be32s(f
, &env
->eip
);
1534 qemu_put_be32s(f
, &env
->eflags
);
1535 qemu_put_be32s(f
, &env
->eflags
);
1536 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1537 qemu_put_be32s(f
, &hflags
);
1541 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1543 for (i
=7; i
>=0; i
--) {
1545 if (env
->fptags
[i
]) {
1550 qemu_put_be16s(f
, &fpuc
);
1551 qemu_put_be16s(f
, &fpus
);
1552 qemu_put_be16s(f
, &fptag
);
1554 for(i
= 0; i
< 8; i
++) {
1557 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1558 qemu_put_be64(f
, mant
);
1559 qemu_put_be16(f
, exp
);
1562 for(i
= 0; i
< 6; i
++)
1563 cpu_put_seg(f
, &env
->segs
[i
]);
1564 cpu_put_seg(f
, &env
->ldt
);
1565 cpu_put_seg(f
, &env
->tr
);
1566 cpu_put_seg(f
, &env
->gdt
);
1567 cpu_put_seg(f
, &env
->idt
);
1569 qemu_put_be32s(f
, &env
->sysenter_cs
);
1570 qemu_put_be32s(f
, &env
->sysenter_esp
);
1571 qemu_put_be32s(f
, &env
->sysenter_eip
);
1573 qemu_put_be32s(f
, &env
->cr
[0]);
1574 qemu_put_be32s(f
, &env
->cr
[2]);
1575 qemu_put_be32s(f
, &env
->cr
[3]);
1576 qemu_put_be32s(f
, &env
->cr
[4]);
1578 for(i
= 0; i
< 8; i
++)
1579 qemu_put_be32s(f
, &env
->dr
[i
]);
1582 qemu_put_be32s(f
, &env
->a20_mask
);
1585 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1587 CPUState
*env
= opaque
;
1590 uint16_t fpus
, fpuc
, fptag
;
1592 if (version_id
!= 1)
1594 for(i
= 0; i
< 8; i
++)
1595 qemu_get_be32s(f
, &env
->regs
[i
]);
1596 qemu_get_be32s(f
, &env
->eip
);
1597 qemu_get_be32s(f
, &env
->eflags
);
1598 qemu_get_be32s(f
, &env
->eflags
);
1599 qemu_get_be32s(f
, &hflags
);
1601 qemu_get_be16s(f
, &fpuc
);
1602 qemu_get_be16s(f
, &fpus
);
1603 qemu_get_be16s(f
, &fptag
);
1605 for(i
= 0; i
< 8; i
++) {
1608 mant
= qemu_get_be64(f
);
1609 exp
= qemu_get_be16(f
);
1610 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1614 env
->fpstt
= (fpus
>> 11) & 7;
1615 env
->fpus
= fpus
& ~0x3800;
1616 for(i
= 0; i
< 8; i
++) {
1617 env
->fptags
[i
] = ((fptag
& 3) == 3);
1621 for(i
= 0; i
< 6; i
++)
1622 cpu_get_seg(f
, &env
->segs
[i
]);
1623 cpu_get_seg(f
, &env
->ldt
);
1624 cpu_get_seg(f
, &env
->tr
);
1625 cpu_get_seg(f
, &env
->gdt
);
1626 cpu_get_seg(f
, &env
->idt
);
1628 qemu_get_be32s(f
, &env
->sysenter_cs
);
1629 qemu_get_be32s(f
, &env
->sysenter_esp
);
1630 qemu_get_be32s(f
, &env
->sysenter_eip
);
1632 qemu_get_be32s(f
, &env
->cr
[0]);
1633 qemu_get_be32s(f
, &env
->cr
[2]);
1634 qemu_get_be32s(f
, &env
->cr
[3]);
1635 qemu_get_be32s(f
, &env
->cr
[4]);
1637 for(i
= 0; i
< 8; i
++)
1638 qemu_get_be32s(f
, &env
->dr
[i
]);
1641 qemu_get_be32s(f
, &env
->a20_mask
);
1643 /* XXX: compute hflags from scratch, except for CPL and IIF */
1644 env
->hflags
= hflags
;
1649 #elif defined(TARGET_PPC)
1650 void cpu_save(QEMUFile
*f
, void *opaque
)
1654 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1660 #warning No CPU save/restore functions
1664 /***********************************************************/
1665 /* ram save/restore */
1667 /* we just avoid storing empty pages */
1668 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1673 for(i
= 1; i
< len
; i
++) {
1677 qemu_put_byte(f
, 1);
1678 qemu_put_byte(f
, v
);
1681 qemu_put_byte(f
, 0);
1682 qemu_put_buffer(f
, buf
, len
);
1685 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1689 v
= qemu_get_byte(f
);
1692 if (qemu_get_buffer(f
, buf
, len
) != len
)
1696 v
= qemu_get_byte(f
);
1697 memset(buf
, v
, len
);
1705 static void ram_save(QEMUFile
*f
, void *opaque
)
1708 qemu_put_be32(f
, phys_ram_size
);
1709 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1710 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1714 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1718 if (version_id
!= 1)
1720 if (qemu_get_be32(f
) != phys_ram_size
)
1722 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1723 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1730 /***********************************************************/
1731 /* main execution loop */
1733 void gui_update(void *opaque
)
1735 display_state
.dpy_refresh(&display_state
);
1736 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1739 /* XXX: support several handlers */
1740 VMStopHandler
*vm_stop_cb
;
1741 VMStopHandler
*vm_stop_opaque
;
1743 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1746 vm_stop_opaque
= opaque
;
1750 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1763 void vm_stop(int reason
)
1766 cpu_disable_ticks();
1770 vm_stop_cb(vm_stop_opaque
, reason
);
1779 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1780 IOHandlerRecord
*ioh
, *ioh_next
;
1785 CPUState
*env
= global_env
;
1789 ret
= cpu_exec(env
);
1790 if (reset_requested
) {
1791 ret
= EXCP_INTERRUPT
;
1794 if (ret
== EXCP_DEBUG
) {
1795 vm_stop(EXCP_DEBUG
);
1797 /* if hlt instruction, we wait until the next IRQ */
1798 /* XXX: use timeout computed from timers */
1799 if (ret
== EXCP_HLT
)
1812 /* poll any events */
1813 /* XXX: separate device handlers from system ones */
1815 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1816 if (!ioh
->fd_can_read
) {
1819 pf
->events
= POLLIN
;
1823 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1825 if (max_size
> sizeof(buf
))
1826 max_size
= sizeof(buf
);
1828 pf
->events
= POLLIN
;
1835 ioh
->max_size
= max_size
;
1838 ret
= poll(ufds
, pf
- ufds
, timeout
);
1840 /* XXX: better handling of removal */
1841 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1842 ioh_next
= ioh
->next
;
1845 if (pf
->revents
& POLLIN
) {
1846 if (ioh
->max_size
== 0) {
1847 /* just a read event */
1848 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1850 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1852 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1853 } else if (errno
!= EAGAIN
) {
1854 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1862 #if defined(CONFIG_SLIRP)
1863 /* XXX: merge with poll() */
1865 fd_set rfds
, wfds
, xfds
;
1873 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1876 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1878 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1886 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1887 qemu_get_clock(vm_clock
));
1889 if (audio_enabled
) {
1890 /* XXX: add explicit timer */
1894 /* run dma transfers, if any */
1898 /* real time timers */
1899 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1900 qemu_get_clock(rt_clock
));
1902 cpu_disable_ticks();
1908 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
1909 "usage: %s [options] [disk_image]\n"
1911 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1913 "Standard options:\n"
1914 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1915 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1916 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1917 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1918 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1919 "-snapshot write to temporary files instead of disk image files\n"
1920 "-m megs set virtual RAM size to megs MB [default=%d]\n"
1921 "-nographic disable graphical output and redirect serial I/Os to console\n"
1922 "-enable-audio enable audio support\n"
1924 "Network options:\n"
1925 "-nics n simulate 'n' network cards [default=1]\n"
1926 "-macaddr addr set the mac address of the first interface\n"
1927 "-n script set tap/tun network init script [default=%s]\n"
1928 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1930 "-user-net use user mode network stack [default if no tap/tun script]\n"
1932 "-dummy-net use dummy network stack\n"
1934 "Linux boot specific:\n"
1935 "-kernel bzImage use 'bzImage' as kernel image\n"
1936 "-append cmdline use 'cmdline' as kernel command line\n"
1937 "-initrd file use 'file' as initial ram disk\n"
1939 "Debug/Expert options:\n"
1940 "-S freeze CPU at startup (use 'c' to start execution)\n"
1941 "-s wait gdb connection to port %d\n"
1942 "-p port change gdb connection port\n"
1943 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1944 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1945 "-L path set the directory for the BIOS and VGA BIOS\n"
1946 #ifdef USE_CODE_COPY
1947 "-no-code-copy disable code copy acceleration\n"
1951 "During emulation, use C-a h to get terminal commands:\n",
1952 #ifdef CONFIG_SOFTMMU
1958 DEFAULT_NETWORK_SCRIPT
,
1959 DEFAULT_GDBSTUB_PORT
,
1962 #ifndef CONFIG_SOFTMMU
1964 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1965 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1971 #define HAS_ARG 0x0001
1984 QEMU_OPTION_snapshot
,
1986 QEMU_OPTION_nographic
,
1987 QEMU_OPTION_enable_audio
,
1990 QEMU_OPTION_macaddr
,
1993 QEMU_OPTION_user_net
,
1994 QEMU_OPTION_dummy_net
,
2006 QEMU_OPTION_no_code_copy
,
2010 typedef struct QEMUOption
{
2016 const QEMUOption qemu_options
[] = {
2017 { "h", 0, QEMU_OPTION_h
},
2019 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2020 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2021 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2022 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2023 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2024 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2025 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2026 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2027 { "snapshot", 0, QEMU_OPTION_snapshot
},
2028 { "m", HAS_ARG
, QEMU_OPTION_m
},
2029 { "nographic", 0, QEMU_OPTION_nographic
},
2030 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2032 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2033 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2034 { "n", HAS_ARG
, QEMU_OPTION_n
},
2035 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2037 { "user-net", 0, QEMU_OPTION_user_net
},
2039 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2041 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2042 { "append", HAS_ARG
, QEMU_OPTION_append
},
2043 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2045 { "S", 0, QEMU_OPTION_S
},
2046 { "s", 0, QEMU_OPTION_s
},
2047 { "p", HAS_ARG
, QEMU_OPTION_p
},
2048 { "d", HAS_ARG
, QEMU_OPTION_d
},
2049 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2050 { "L", HAS_ARG
, QEMU_OPTION_L
},
2051 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2052 { "pci", 0, QEMU_OPTION_pci
},
2056 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2058 /* this stack is only used during signal handling */
2059 #define SIGNAL_STACK_SIZE 32768
2061 static uint8_t *signal_stack
;
2065 #define NET_IF_TUN 0
2066 #define NET_IF_USER 1
2067 #define NET_IF_DUMMY 2
2069 int main(int argc
, char **argv
)
2071 #ifdef CONFIG_GDBSTUB
2072 int use_gdbstub
, gdbstub_port
;
2075 int snapshot
, linux_boot
;
2077 const char *initrd_filename
;
2078 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2079 const char *kernel_filename
, *kernel_cmdline
;
2080 DisplayState
*ds
= &display_state
;
2081 int cyls
, heads
, secs
;
2082 int start_emulation
= 1;
2084 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2086 const char *r
, *optarg
;
2088 #if !defined(CONFIG_SOFTMMU)
2089 /* we never want that malloc() uses mmap() */
2090 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2092 initrd_filename
= NULL
;
2093 for(i
= 0; i
< MAX_FD
; i
++)
2094 fd_filename
[i
] = NULL
;
2095 for(i
= 0; i
< MAX_DISKS
; i
++)
2096 hd_filename
[i
] = NULL
;
2097 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
2098 vga_ram_size
= VGA_RAM_SIZE
;
2099 bios_size
= BIOS_SIZE
;
2100 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2101 #ifdef CONFIG_GDBSTUB
2103 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2107 kernel_filename
= NULL
;
2108 kernel_cmdline
= "";
2110 cyls
= heads
= secs
= 0;
2115 /* default mac address of the first network interface */
2129 hd_filename
[0] = argv
[optind
++];
2131 const QEMUOption
*popt
;
2134 popt
= qemu_options
;
2137 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2141 if (!strcmp(popt
->name
, r
+ 1))
2145 if (popt
->flags
& HAS_ARG
) {
2146 if (optind
>= argc
) {
2147 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2151 optarg
= argv
[optind
++];
2156 switch(popt
->index
) {
2157 case QEMU_OPTION_initrd
:
2158 initrd_filename
= optarg
;
2160 case QEMU_OPTION_hda
:
2161 hd_filename
[0] = optarg
;
2163 case QEMU_OPTION_hdb
:
2164 hd_filename
[1] = optarg
;
2166 case QEMU_OPTION_snapshot
:
2169 case QEMU_OPTION_hdachs
:
2173 cyls
= strtol(p
, (char **)&p
, 0);
2177 heads
= strtol(p
, (char **)&p
, 0);
2181 secs
= strtol(p
, (char **)&p
, 0);
2188 case QEMU_OPTION_nographic
:
2191 case QEMU_OPTION_kernel
:
2192 kernel_filename
= optarg
;
2194 case QEMU_OPTION_append
:
2195 kernel_cmdline
= optarg
;
2197 case QEMU_OPTION_tun_fd
:
2201 net_if_type
= NET_IF_TUN
;
2202 if (nb_tun_fds
< MAX_NICS
) {
2203 fd
= strtol(optarg
, (char **)&p
, 0);
2205 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2208 tun_fds
[nb_tun_fds
++] = fd
;
2212 case QEMU_OPTION_hdc
:
2213 hd_filename
[2] = optarg
;
2216 case QEMU_OPTION_hdd
:
2217 hd_filename
[3] = optarg
;
2219 case QEMU_OPTION_cdrom
:
2220 hd_filename
[2] = optarg
;
2223 case QEMU_OPTION_boot
:
2224 boot_device
= optarg
[0];
2225 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2226 boot_device
!= 'c' && boot_device
!= 'd') {
2227 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2231 case QEMU_OPTION_fda
:
2232 fd_filename
[0] = optarg
;
2234 case QEMU_OPTION_fdb
:
2235 fd_filename
[1] = optarg
;
2237 case QEMU_OPTION_no_code_copy
:
2238 code_copy_enabled
= 0;
2240 case QEMU_OPTION_nics
:
2241 nb_nics
= atoi(optarg
);
2242 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2243 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2247 case QEMU_OPTION_macaddr
:
2252 for(i
= 0; i
< 6; i
++) {
2253 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2260 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2268 case QEMU_OPTION_user_net
:
2269 net_if_type
= NET_IF_USER
;
2271 case QEMU_OPTION_dummy_net
:
2272 net_if_type
= NET_IF_DUMMY
;
2274 case QEMU_OPTION_enable_audio
:
2281 ram_size
= atoi(optarg
) * 1024 * 1024;
2284 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2285 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2286 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2295 mask
= cpu_str_to_log_mask(optarg
);
2297 printf("Log items (comma separated):\n");
2298 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2299 printf("%-10s %s\n", item
->name
, item
->help
);
2307 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2309 #ifdef CONFIG_GDBSTUB
2314 gdbstub_port
= atoi(optarg
);
2321 start_emulation
= 0;
2323 case QEMU_OPTION_pci
:
2330 linux_boot
= (kernel_filename
!= NULL
);
2332 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2333 fd_filename
[0] == '\0')
2336 /* boot to cd by default if no hard disk */
2337 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2338 if (fd_filename
[0] != '\0')
2344 #if !defined(CONFIG_SOFTMMU)
2345 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2347 static uint8_t stdout_buf
[4096];
2348 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2351 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2354 /* init host network redirectors */
2355 if (net_if_type
== -1) {
2356 net_if_type
= NET_IF_TUN
;
2357 #if defined(CONFIG_SLIRP)
2358 if (access(network_script
, R_OK
) < 0) {
2359 net_if_type
= NET_IF_USER
;
2364 for(i
= 0; i
< nb_nics
; i
++) {
2365 NetDriverState
*nd
= &nd_table
[i
];
2367 /* init virtual mac address */
2368 nd
->macaddr
[0] = macaddr
[0];
2369 nd
->macaddr
[1] = macaddr
[1];
2370 nd
->macaddr
[2] = macaddr
[2];
2371 nd
->macaddr
[3] = macaddr
[3];
2372 nd
->macaddr
[4] = macaddr
[4];
2373 nd
->macaddr
[5] = macaddr
[5] + i
;
2374 switch(net_if_type
) {
2375 #if defined(CONFIG_SLIRP)
2380 #if !defined(_WIN32)
2382 if (i
< nb_tun_fds
) {
2383 net_fd_init(nd
, tun_fds
[i
]);
2385 if (net_tun_init(nd
) < 0)
2397 /* init the memory */
2398 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
2400 #ifdef CONFIG_SOFTMMU
2402 /* mallocs are always aligned on BSD. */
2403 phys_ram_base
= malloc(phys_ram_size
);
2405 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2407 if (!phys_ram_base
) {
2408 fprintf(stderr
, "Could not allocate physical memory\n");
2412 /* as we must map the same page at several addresses, we must use
2417 tmpdir
= getenv("QEMU_TMPDIR");
2420 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2421 if (mkstemp(phys_ram_file
) < 0) {
2422 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2426 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2427 if (phys_ram_fd
< 0) {
2428 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2432 ftruncate(phys_ram_fd
, phys_ram_size
);
2433 unlink(phys_ram_file
);
2434 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2436 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2438 if (phys_ram_base
== MAP_FAILED
) {
2439 fprintf(stderr
, "Could not map physical memory\n");
2445 /* we always create the cdrom drive, even if no disk is there */
2447 bs_table
[2] = bdrv_new("cdrom");
2448 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2451 /* open the virtual block devices */
2452 for(i
= 0; i
< MAX_DISKS
; i
++) {
2453 if (hd_filename
[i
]) {
2456 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2457 bs_table
[i
] = bdrv_new(buf
);
2459 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2460 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2464 if (i
== 0 && cyls
!= 0)
2465 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2469 /* we always create at least one floppy disk */
2470 fd_table
[0] = bdrv_new("fda");
2471 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2473 for(i
= 0; i
< MAX_FD
; i
++) {
2474 if (fd_filename
[i
]) {
2477 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2478 fd_table
[i
] = bdrv_new(buf
);
2479 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2481 if (fd_filename
[i
] != '\0') {
2482 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2483 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2491 /* init CPU state */
2494 cpu_single_env
= env
;
2496 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2497 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2498 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2501 cpu_calibrate_ticks();
2505 dumb_display_init(ds
);
2508 sdl_display_init(ds
);
2510 dumb_display_init(ds
);
2514 /* setup cpu signal handlers for MMU / self modifying code handling */
2515 #if !defined(CONFIG_SOFTMMU)
2517 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2520 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2521 stk
.ss_sp
= signal_stack
;
2522 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2525 if (sigaltstack(&stk
, NULL
) < 0) {
2526 perror("sigaltstack");
2532 struct sigaction act
;
2534 sigfillset(&act
.sa_mask
);
2535 act
.sa_flags
= SA_SIGINFO
;
2536 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2537 act
.sa_flags
|= SA_ONSTACK
;
2539 act
.sa_sigaction
= host_segv_handler
;
2540 sigaction(SIGSEGV
, &act
, NULL
);
2541 sigaction(SIGBUS
, &act
, NULL
);
2542 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2543 sigaction(SIGFPE
, &act
, NULL
);
2550 struct sigaction act
;
2551 sigfillset(&act
.sa_mask
);
2553 act
.sa_handler
= SIG_IGN
;
2554 sigaction(SIGPIPE
, &act
, NULL
);
2559 #if defined(TARGET_I386)
2560 pc_init(ram_size
, vga_ram_size
, boot_device
,
2561 ds
, fd_filename
, snapshot
,
2562 kernel_filename
, kernel_cmdline
, initrd_filename
);
2563 #elif defined(TARGET_PPC)
2564 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2565 ds
, fd_filename
, snapshot
,
2566 kernel_filename
, kernel_cmdline
, initrd_filename
);
2569 /* launched after the device init so that it can display or not a
2573 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2574 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2576 #ifdef CONFIG_GDBSTUB
2578 if (gdbserver_start(gdbstub_port
) < 0) {
2579 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2583 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2587 if (start_emulation
)