4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
46 #include <linux/if_tun.h>
49 #include <linux/rtc.h>
53 #if defined(CONFIG_SLIRP)
59 #include <sys/timeb.h>
61 #define getopt_long_only getopt_long
62 #define memalign(align, size) malloc(size)
66 #if defined(__linux__)
67 /* SDL use the pthreads and they modify sigaction. We don't
69 #if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2))
70 extern void __libc_sigaction();
71 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
73 extern void __sigaction();
74 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
76 #endif /* __linux__ */
77 #endif /* CONFIG_SDL */
85 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
87 //#define DEBUG_UNUSED_IOPORT
88 //#define DEBUG_IOPORT
90 #if !defined(CONFIG_SOFTMMU)
91 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
93 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
97 #define GUI_REFRESH_INTERVAL 30
99 /* XXX: use a two level table to limit memory usage */
100 #define MAX_IOPORTS 65536
102 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
103 char phys_ram_file
[1024];
104 CPUState
*global_env
;
105 CPUState
*cpu_single_env
;
106 void *ioport_opaque
[MAX_IOPORTS
];
107 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
108 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
109 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
111 static DisplayState display_state
;
113 int64_t ticks_per_sec
;
114 int boot_device
= 'c';
116 static char network_script
[1024];
117 int pit_min_timer_count
= 0;
119 NetDriverState nd_table
[MAX_NICS
];
120 SerialState
*serial_console
;
121 QEMUTimer
*gui_timer
;
123 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
](ioport_opaque
[address
], address
);
151 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
152 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
156 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
158 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
159 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
160 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
163 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
165 #ifdef DEBUG_UNUSED_IOPORT
166 fprintf(stderr
, "inl: port=0x%04x\n", address
);
171 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
173 #ifdef DEBUG_UNUSED_IOPORT
174 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
178 void init_ioports(void)
182 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
183 ioport_read_table
[0][i
] = default_ioport_readb
;
184 ioport_write_table
[0][i
] = default_ioport_writeb
;
185 ioport_read_table
[1][i
] = default_ioport_readw
;
186 ioport_write_table
[1][i
] = default_ioport_writew
;
187 ioport_read_table
[2][i
] = default_ioport_readl
;
188 ioport_write_table
[2][i
] = default_ioport_writel
;
192 /* size is the word size in byte */
193 int register_ioport_read(int start
, int length
, int size
,
194 IOPortReadFunc
*func
, void *opaque
)
200 } else if (size
== 2) {
202 } else if (size
== 4) {
205 hw_error("register_ioport_read: invalid size");
208 for(i
= start
; i
< start
+ length
; i
+= size
) {
209 ioport_read_table
[bsize
][i
] = func
;
210 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
211 hw_error("register_ioport_read: invalid opaque");
212 ioport_opaque
[i
] = opaque
;
217 /* size is the word size in byte */
218 int register_ioport_write(int start
, int length
, int size
,
219 IOPortWriteFunc
*func
, void *opaque
)
225 } else if (size
== 2) {
227 } else if (size
== 4) {
230 hw_error("register_ioport_write: invalid size");
233 for(i
= start
; i
< start
+ length
; i
+= size
) {
234 ioport_write_table
[bsize
][i
] = func
;
235 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
236 hw_error("register_ioport_read: invalid opaque");
237 ioport_opaque
[i
] = opaque
;
242 void isa_unassign_ioport(int start
, int length
)
246 for(i
= start
; i
< start
+ length
; i
++) {
247 ioport_read_table
[0][i
] = default_ioport_readb
;
248 ioport_read_table
[1][i
] = default_ioport_readw
;
249 ioport_read_table
[2][i
] = default_ioport_readl
;
251 ioport_write_table
[0][i
] = default_ioport_writeb
;
252 ioport_write_table
[1][i
] = default_ioport_writew
;
253 ioport_write_table
[2][i
] = default_ioport_writel
;
257 void pstrcpy(char *buf
, int buf_size
, const char *str
)
267 if (c
== 0 || q
>= buf
+ buf_size
- 1)
274 /* strcat and truncate. */
275 char *pstrcat(char *buf
, int buf_size
, const char *s
)
280 pstrcpy(buf
+ len
, buf_size
- len
, s
);
284 /* return the size or -1 if error */
285 int load_image(const char *filename
, uint8_t *addr
)
288 fd
= open(filename
, O_RDONLY
| O_BINARY
);
291 size
= lseek(fd
, 0, SEEK_END
);
292 lseek(fd
, 0, SEEK_SET
);
293 if (read(fd
, addr
, size
) != size
) {
301 void cpu_outb(CPUState
*env
, int addr
, int val
)
304 if (loglevel
& CPU_LOG_IOPORT
)
305 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
307 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
310 void cpu_outw(CPUState
*env
, int addr
, int val
)
313 if (loglevel
& CPU_LOG_IOPORT
)
314 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
316 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
319 void cpu_outl(CPUState
*env
, int addr
, int val
)
322 if (loglevel
& CPU_LOG_IOPORT
)
323 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
325 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
328 int cpu_inb(CPUState
*env
, int addr
)
331 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
333 if (loglevel
& CPU_LOG_IOPORT
)
334 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
339 int cpu_inw(CPUState
*env
, int addr
)
342 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
344 if (loglevel
& CPU_LOG_IOPORT
)
345 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
350 int cpu_inl(CPUState
*env
, int addr
)
353 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
355 if (loglevel
& CPU_LOG_IOPORT
)
356 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
361 /***********************************************************/
362 void hw_error(const char *fmt
, ...)
367 fprintf(stderr
, "qemu: hardware error: ");
368 vfprintf(stderr
, fmt
, ap
);
369 fprintf(stderr
, "\n");
371 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
373 cpu_dump_state(global_env
, stderr
, 0);
379 /***********************************************************/
382 #if defined(__powerpc__)
384 static inline uint32_t get_tbl(void)
387 asm volatile("mftb %0" : "=r" (tbl
));
391 static inline uint32_t get_tbu(void)
394 asm volatile("mftbu %0" : "=r" (tbl
));
398 int64_t cpu_get_real_ticks(void)
401 /* NOTE: we test if wrapping has occurred */
407 return ((int64_t)h
<< 32) | l
;
410 #elif defined(__i386__)
412 int64_t cpu_get_real_ticks(void)
415 asm volatile ("rdtsc" : "=A" (val
));
419 #elif defined(__x86_64__)
421 int64_t cpu_get_real_ticks(void)
425 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
433 #error unsupported CPU
436 static int64_t cpu_ticks_offset
;
437 static int cpu_ticks_enabled
;
439 static inline int64_t cpu_get_ticks(void)
441 if (!cpu_ticks_enabled
) {
442 return cpu_ticks_offset
;
444 return cpu_get_real_ticks() + cpu_ticks_offset
;
448 /* enable cpu_get_ticks() */
449 void cpu_enable_ticks(void)
451 if (!cpu_ticks_enabled
) {
452 cpu_ticks_offset
-= cpu_get_real_ticks();
453 cpu_ticks_enabled
= 1;
457 /* disable cpu_get_ticks() : the clock is stopped. You must not call
458 cpu_get_ticks() after that. */
459 void cpu_disable_ticks(void)
461 if (cpu_ticks_enabled
) {
462 cpu_ticks_offset
= cpu_get_ticks();
463 cpu_ticks_enabled
= 0;
467 static int64_t get_clock(void)
472 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
475 gettimeofday(&tv
, NULL
);
476 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
480 void cpu_calibrate_ticks(void)
485 ticks
= cpu_get_real_ticks();
491 usec
= get_clock() - usec
;
492 ticks
= cpu_get_real_ticks() - ticks
;
493 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
496 /* compute with 96 bit intermediate result: (a*b)/c */
497 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
502 #ifdef WORDS_BIGENDIAN
512 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
513 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
516 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
520 #define QEMU_TIMER_REALTIME 0
521 #define QEMU_TIMER_VIRTUAL 1
525 /* XXX: add frequency */
533 struct QEMUTimer
*next
;
539 static QEMUTimer
*active_timers
[2];
541 static MMRESULT timerID
;
543 /* frequency of the times() clock tick */
544 static int timer_freq
;
547 QEMUClock
*qemu_new_clock(int type
)
550 clock
= qemu_mallocz(sizeof(QEMUClock
));
557 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
561 ts
= qemu_mallocz(sizeof(QEMUTimer
));
568 void qemu_free_timer(QEMUTimer
*ts
)
573 /* stop a timer, but do not dealloc it */
574 void qemu_del_timer(QEMUTimer
*ts
)
578 /* NOTE: this code must be signal safe because
579 qemu_timer_expired() can be called from a signal. */
580 pt
= &active_timers
[ts
->clock
->type
];
593 /* modify the current timer so that it will be fired when current_time
594 >= expire_time. The corresponding callback will be called. */
595 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
601 /* add the timer in the sorted list */
602 /* NOTE: this code must be signal safe because
603 qemu_timer_expired() can be called from a signal. */
604 pt
= &active_timers
[ts
->clock
->type
];
609 if (t
->expire_time
> expire_time
)
613 ts
->expire_time
= expire_time
;
618 int qemu_timer_pending(QEMUTimer
*ts
)
621 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
628 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
632 return (timer_head
->expire_time
<= current_time
);
635 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
641 if (ts
->expire_time
> current_time
)
643 /* remove timer from the list before calling the callback */
644 *ptimer_head
= ts
->next
;
647 /* run the callback (the timer list can be modified) */
652 int64_t qemu_get_clock(QEMUClock
*clock
)
654 switch(clock
->type
) {
655 case QEMU_TIMER_REALTIME
:
657 return GetTickCount();
662 /* Note that using gettimeofday() is not a good solution
663 for timers because its value change when the date is
665 if (timer_freq
== 100) {
666 return times(&tp
) * 10;
668 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
673 case QEMU_TIMER_VIRTUAL
:
674 return cpu_get_ticks();
679 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
681 uint64_t expire_time
;
683 if (qemu_timer_pending(ts
)) {
684 expire_time
= ts
->expire_time
;
688 qemu_put_be64(f
, expire_time
);
691 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
693 uint64_t expire_time
;
695 expire_time
= qemu_get_be64(f
);
696 if (expire_time
!= -1) {
697 qemu_mod_timer(ts
, expire_time
);
703 static void timer_save(QEMUFile
*f
, void *opaque
)
705 if (cpu_ticks_enabled
) {
706 hw_error("cannot save state if virtual timers are running");
708 qemu_put_be64s(f
, &cpu_ticks_offset
);
709 qemu_put_be64s(f
, &ticks_per_sec
);
712 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
716 if (cpu_ticks_enabled
) {
719 qemu_get_be64s(f
, &cpu_ticks_offset
);
720 qemu_get_be64s(f
, &ticks_per_sec
);
725 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
726 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
728 static void host_alarm_handler(int host_signum
)
731 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
732 qemu_get_clock(vm_clock
)) ||
733 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
734 qemu_get_clock(rt_clock
))) {
735 /* stop the cpu because a timer occured */
736 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
742 #define RTC_FREQ 1024
746 static int start_rtc_timer(void)
748 rtc_fd
= open("/dev/rtc", O_RDONLY
);
751 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
752 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
753 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
754 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
757 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
762 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
768 static void init_timers(void)
770 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
771 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
776 timerID
= timeSetEvent(10, // interval (ms)
778 host_alarm_handler
, // function
779 (DWORD
)&count
, // user parameter
780 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
782 perror("failed timer alarm");
786 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
789 struct sigaction act
;
790 struct itimerval itv
;
792 /* get times() syscall frequency */
793 timer_freq
= sysconf(_SC_CLK_TCK
);
796 sigfillset(&act
.sa_mask
);
798 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
799 act
.sa_flags
|= SA_ONSTACK
;
801 act
.sa_handler
= host_alarm_handler
;
802 sigaction(SIGALRM
, &act
, NULL
);
804 itv
.it_interval
.tv_sec
= 0;
805 itv
.it_interval
.tv_usec
= 1000;
806 itv
.it_value
.tv_sec
= 0;
807 itv
.it_value
.tv_usec
= 10 * 1000;
808 setitimer(ITIMER_REAL
, &itv
, NULL
);
809 /* we probe the tick duration of the kernel to inform the user if
810 the emulated kernel requested a too high timer frequency */
811 getitimer(ITIMER_REAL
, &itv
);
813 if (itv
.it_interval
.tv_usec
> 1000) {
814 /* try to use /dev/rtc to have a faster timer */
815 if (start_rtc_timer() < 0)
818 itv
.it_interval
.tv_sec
= 0;
819 itv
.it_interval
.tv_usec
= 0;
820 itv
.it_value
.tv_sec
= 0;
821 itv
.it_value
.tv_usec
= 0;
822 setitimer(ITIMER_REAL
, &itv
, NULL
);
825 sigaction(SIGIO
, &act
, NULL
);
826 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
827 fcntl(rtc_fd
, F_SETOWN
, getpid());
830 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
837 void quit_timers(void)
840 timeKillEvent(timerID
);
844 /***********************************************************/
849 int serial_open_device(void)
856 int serial_open_device(void)
858 char slave_name
[1024];
859 int master_fd
, slave_fd
;
861 if (serial_console
== NULL
&& nographic
) {
862 /* use console for serial port */
865 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
866 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
869 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
876 /***********************************************************/
877 /* Linux network device redirectors */
879 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
883 for(i
=0;i
<size
;i
+=16) {
887 fprintf(f
, "%08x ", i
);
890 fprintf(f
, " %02x", buf
[i
+j
]);
897 if (c
< ' ' || c
> '~')
905 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
907 nd
->send_packet(nd
, buf
, size
);
910 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
911 IOReadHandler
*fd_read
, void *opaque
)
913 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
916 /* dummy network adapter */
918 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
922 static void dummy_add_read_packet(NetDriverState
*nd
,
923 IOCanRWHandler
*fd_can_read
,
924 IOReadHandler
*fd_read
, void *opaque
)
928 static int net_dummy_init(NetDriverState
*nd
)
930 nd
->send_packet
= dummy_send_packet
;
931 nd
->add_read_packet
= dummy_add_read_packet
;
932 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
936 #if defined(CONFIG_SLIRP)
938 /* slirp network adapter */
940 static void *slirp_fd_opaque
;
941 static IOCanRWHandler
*slirp_fd_can_read
;
942 static IOReadHandler
*slirp_fd_read
;
943 static int slirp_inited
;
945 int slirp_can_output(void)
947 return slirp_fd_can_read(slirp_fd_opaque
);
950 void slirp_output(const uint8_t *pkt
, int pkt_len
)
954 hex_dump(stdout
, pkt
, pkt_len
);
956 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
959 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
963 hex_dump(stdout
, buf
, size
);
965 slirp_input(buf
, size
);
968 static void slirp_add_read_packet(NetDriverState
*nd
,
969 IOCanRWHandler
*fd_can_read
,
970 IOReadHandler
*fd_read
, void *opaque
)
972 slirp_fd_opaque
= opaque
;
973 slirp_fd_can_read
= fd_can_read
;
974 slirp_fd_read
= fd_read
;
977 static int net_slirp_init(NetDriverState
*nd
)
983 nd
->send_packet
= slirp_send_packet
;
984 nd
->add_read_packet
= slirp_add_read_packet
;
985 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
989 #endif /* CONFIG_SLIRP */
993 static int tun_open(char *ifname
, int ifname_size
)
999 fd
= open("/dev/tap", O_RDWR
);
1001 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1006 dev
= devname(s
.st_rdev
, S_IFCHR
);
1007 pstrcpy(ifname
, ifname_size
, dev
);
1009 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1013 static int tun_open(char *ifname
, int ifname_size
)
1018 fd
= open("/dev/net/tun", O_RDWR
);
1020 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1023 memset(&ifr
, 0, sizeof(ifr
));
1024 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1025 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1026 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1028 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1032 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1033 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1034 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1039 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1041 write(nd
->fd
, buf
, size
);
1044 static void tun_add_read_packet(NetDriverState
*nd
,
1045 IOCanRWHandler
*fd_can_read
,
1046 IOReadHandler
*fd_read
, void *opaque
)
1048 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1051 static int net_tun_init(NetDriverState
*nd
)
1057 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1061 /* try to launch network init script */
1066 *parg
++ = network_script
;
1067 *parg
++ = nd
->ifname
;
1069 execv(network_script
, args
);
1072 while (waitpid(pid
, &status
, 0) != pid
);
1073 if (!WIFEXITED(status
) ||
1074 WEXITSTATUS(status
) != 0) {
1075 fprintf(stderr
, "%s: could not launch network script\n",
1079 nd
->send_packet
= tun_send_packet
;
1080 nd
->add_read_packet
= tun_add_read_packet
;
1084 static int net_fd_init(NetDriverState
*nd
, int fd
)
1087 nd
->send_packet
= tun_send_packet
;
1088 nd
->add_read_packet
= tun_add_read_packet
;
1089 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1093 #endif /* !_WIN32 */
1095 /***********************************************************/
1100 static void term_exit(void)
1104 static void term_init(void)
1110 /* init terminal so that we can grab keys */
1111 static struct termios oldtty
;
1113 static void term_exit(void)
1115 tcsetattr (0, TCSANOW
, &oldtty
);
1118 static void term_init(void)
1122 tcgetattr (0, &tty
);
1125 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1126 |INLCR
|IGNCR
|ICRNL
|IXON
);
1127 tty
.c_oflag
|= OPOST
;
1128 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1129 /* if graphical mode, we allow Ctrl-C handling */
1131 tty
.c_lflag
&= ~ISIG
;
1132 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1135 tty
.c_cc
[VTIME
] = 0;
1137 tcsetattr (0, TCSANOW
, &tty
);
1141 fcntl(0, F_SETFL
, O_NONBLOCK
);
1146 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1150 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1154 static void dumb_refresh(DisplayState
*ds
)
1156 vga_update_display();
1159 void dumb_display_init(DisplayState
*ds
)
1164 ds
->dpy_update
= dumb_update
;
1165 ds
->dpy_resize
= dumb_resize
;
1166 ds
->dpy_refresh
= dumb_refresh
;
1169 #if !defined(CONFIG_SOFTMMU)
1170 /***********************************************************/
1171 /* cpu signal handler */
1172 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1175 if (cpu_signal_handler(host_signum
, info
, puc
))
1182 /***********************************************************/
1185 #define MAX_IO_HANDLERS 64
1187 typedef struct IOHandlerRecord
{
1189 IOCanRWHandler
*fd_can_read
;
1190 IOReadHandler
*fd_read
;
1192 /* temporary data */
1195 struct IOHandlerRecord
*next
;
1198 static IOHandlerRecord
*first_io_handler
;
1200 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1201 IOReadHandler
*fd_read
, void *opaque
)
1203 IOHandlerRecord
*ioh
;
1205 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1209 ioh
->fd_can_read
= fd_can_read
;
1210 ioh
->fd_read
= fd_read
;
1211 ioh
->opaque
= opaque
;
1212 ioh
->next
= first_io_handler
;
1213 first_io_handler
= ioh
;
1217 void qemu_del_fd_read_handler(int fd
)
1219 IOHandlerRecord
**pioh
, *ioh
;
1221 pioh
= &first_io_handler
;
1226 if (ioh
->fd
== fd
) {
1234 /***********************************************************/
1235 /* savevm/loadvm support */
1237 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1239 fwrite(buf
, 1, size
, f
);
1242 void qemu_put_byte(QEMUFile
*f
, int v
)
1247 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1249 qemu_put_byte(f
, v
>> 8);
1250 qemu_put_byte(f
, v
);
1253 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1255 qemu_put_byte(f
, v
>> 24);
1256 qemu_put_byte(f
, v
>> 16);
1257 qemu_put_byte(f
, v
>> 8);
1258 qemu_put_byte(f
, v
);
1261 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1263 qemu_put_be32(f
, v
>> 32);
1264 qemu_put_be32(f
, v
);
1267 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1269 return fread(buf
, 1, size
, f
);
1272 int qemu_get_byte(QEMUFile
*f
)
1282 unsigned int qemu_get_be16(QEMUFile
*f
)
1285 v
= qemu_get_byte(f
) << 8;
1286 v
|= qemu_get_byte(f
);
1290 unsigned int qemu_get_be32(QEMUFile
*f
)
1293 v
= qemu_get_byte(f
) << 24;
1294 v
|= qemu_get_byte(f
) << 16;
1295 v
|= qemu_get_byte(f
) << 8;
1296 v
|= qemu_get_byte(f
);
1300 uint64_t qemu_get_be64(QEMUFile
*f
)
1303 v
= (uint64_t)qemu_get_be32(f
) << 32;
1304 v
|= qemu_get_be32(f
);
1308 int64_t qemu_ftell(QEMUFile
*f
)
1313 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1315 if (fseek(f
, pos
, whence
) < 0)
1320 typedef struct SaveStateEntry
{
1324 SaveStateHandler
*save_state
;
1325 LoadStateHandler
*load_state
;
1327 struct SaveStateEntry
*next
;
1330 static SaveStateEntry
*first_se
;
1332 int register_savevm(const char *idstr
,
1335 SaveStateHandler
*save_state
,
1336 LoadStateHandler
*load_state
,
1339 SaveStateEntry
*se
, **pse
;
1341 se
= qemu_malloc(sizeof(SaveStateEntry
));
1344 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1345 se
->instance_id
= instance_id
;
1346 se
->version_id
= version_id
;
1347 se
->save_state
= save_state
;
1348 se
->load_state
= load_state
;
1349 se
->opaque
= opaque
;
1352 /* add at the end of list */
1354 while (*pse
!= NULL
)
1355 pse
= &(*pse
)->next
;
1360 #define QEMU_VM_FILE_MAGIC 0x5145564d
1361 #define QEMU_VM_FILE_VERSION 0x00000001
1363 int qemu_savevm(const char *filename
)
1367 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1369 saved_vm_running
= vm_running
;
1372 f
= fopen(filename
, "wb");
1378 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1379 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1381 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1383 len
= strlen(se
->idstr
);
1384 qemu_put_byte(f
, len
);
1385 qemu_put_buffer(f
, se
->idstr
, len
);
1387 qemu_put_be32(f
, se
->instance_id
);
1388 qemu_put_be32(f
, se
->version_id
);
1390 /* record size: filled later */
1392 qemu_put_be32(f
, 0);
1394 se
->save_state(f
, se
->opaque
);
1396 /* fill record size */
1398 len
= ftell(f
) - len_pos
- 4;
1399 fseek(f
, len_pos
, SEEK_SET
);
1400 qemu_put_be32(f
, len
);
1401 fseek(f
, cur_pos
, SEEK_SET
);
1407 if (saved_vm_running
)
1412 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1416 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1417 if (!strcmp(se
->idstr
, idstr
) &&
1418 instance_id
== se
->instance_id
)
1424 int qemu_loadvm(const char *filename
)
1428 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1429 int saved_vm_running
;
1433 saved_vm_running
= vm_running
;
1436 f
= fopen(filename
, "rb");
1442 v
= qemu_get_be32(f
);
1443 if (v
!= QEMU_VM_FILE_MAGIC
)
1445 v
= qemu_get_be32(f
);
1446 if (v
!= QEMU_VM_FILE_VERSION
) {
1453 #if defined (DO_TB_FLUSH)
1454 tb_flush(global_env
);
1456 len
= qemu_get_byte(f
);
1459 qemu_get_buffer(f
, idstr
, len
);
1461 instance_id
= qemu_get_be32(f
);
1462 version_id
= qemu_get_be32(f
);
1463 record_len
= qemu_get_be32(f
);
1465 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1466 idstr
, instance_id
, version_id
, record_len
);
1469 se
= find_se(idstr
, instance_id
);
1471 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1472 instance_id
, idstr
);
1474 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1476 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1477 instance_id
, idstr
);
1480 /* always seek to exact end of record */
1481 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1486 if (saved_vm_running
)
1491 /***********************************************************/
1492 /* cpu save/restore */
1494 #if defined(TARGET_I386)
1496 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1498 qemu_put_be32(f
, (uint32_t)dt
->base
);
1499 qemu_put_be32(f
, dt
->limit
);
1500 qemu_put_be32(f
, dt
->flags
);
1503 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1505 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1506 dt
->limit
= qemu_get_be32(f
);
1507 dt
->flags
= qemu_get_be32(f
);
1510 void cpu_save(QEMUFile
*f
, void *opaque
)
1512 CPUState
*env
= opaque
;
1513 uint16_t fptag
, fpus
, fpuc
;
1517 for(i
= 0; i
< 8; i
++)
1518 qemu_put_be32s(f
, &env
->regs
[i
]);
1519 qemu_put_be32s(f
, &env
->eip
);
1520 qemu_put_be32s(f
, &env
->eflags
);
1521 qemu_put_be32s(f
, &env
->eflags
);
1522 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1523 qemu_put_be32s(f
, &hflags
);
1527 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1529 for (i
=7; i
>=0; i
--) {
1531 if (env
->fptags
[i
]) {
1536 qemu_put_be16s(f
, &fpuc
);
1537 qemu_put_be16s(f
, &fpus
);
1538 qemu_put_be16s(f
, &fptag
);
1540 for(i
= 0; i
< 8; i
++) {
1543 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1544 qemu_put_be64(f
, mant
);
1545 qemu_put_be16(f
, exp
);
1548 for(i
= 0; i
< 6; i
++)
1549 cpu_put_seg(f
, &env
->segs
[i
]);
1550 cpu_put_seg(f
, &env
->ldt
);
1551 cpu_put_seg(f
, &env
->tr
);
1552 cpu_put_seg(f
, &env
->gdt
);
1553 cpu_put_seg(f
, &env
->idt
);
1555 qemu_put_be32s(f
, &env
->sysenter_cs
);
1556 qemu_put_be32s(f
, &env
->sysenter_esp
);
1557 qemu_put_be32s(f
, &env
->sysenter_eip
);
1559 qemu_put_be32s(f
, &env
->cr
[0]);
1560 qemu_put_be32s(f
, &env
->cr
[2]);
1561 qemu_put_be32s(f
, &env
->cr
[3]);
1562 qemu_put_be32s(f
, &env
->cr
[4]);
1564 for(i
= 0; i
< 8; i
++)
1565 qemu_put_be32s(f
, &env
->dr
[i
]);
1568 qemu_put_be32s(f
, &env
->a20_mask
);
1571 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1573 CPUState
*env
= opaque
;
1576 uint16_t fpus
, fpuc
, fptag
;
1578 if (version_id
!= 1)
1580 for(i
= 0; i
< 8; i
++)
1581 qemu_get_be32s(f
, &env
->regs
[i
]);
1582 qemu_get_be32s(f
, &env
->eip
);
1583 qemu_get_be32s(f
, &env
->eflags
);
1584 qemu_get_be32s(f
, &env
->eflags
);
1585 qemu_get_be32s(f
, &hflags
);
1587 qemu_get_be16s(f
, &fpuc
);
1588 qemu_get_be16s(f
, &fpus
);
1589 qemu_get_be16s(f
, &fptag
);
1591 for(i
= 0; i
< 8; i
++) {
1594 mant
= qemu_get_be64(f
);
1595 exp
= qemu_get_be16(f
);
1596 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1600 env
->fpstt
= (fpus
>> 11) & 7;
1601 env
->fpus
= fpus
& ~0x3800;
1602 for(i
= 0; i
< 8; i
++) {
1603 env
->fptags
[i
] = ((fptag
& 3) == 3);
1607 for(i
= 0; i
< 6; i
++)
1608 cpu_get_seg(f
, &env
->segs
[i
]);
1609 cpu_get_seg(f
, &env
->ldt
);
1610 cpu_get_seg(f
, &env
->tr
);
1611 cpu_get_seg(f
, &env
->gdt
);
1612 cpu_get_seg(f
, &env
->idt
);
1614 qemu_get_be32s(f
, &env
->sysenter_cs
);
1615 qemu_get_be32s(f
, &env
->sysenter_esp
);
1616 qemu_get_be32s(f
, &env
->sysenter_eip
);
1618 qemu_get_be32s(f
, &env
->cr
[0]);
1619 qemu_get_be32s(f
, &env
->cr
[2]);
1620 qemu_get_be32s(f
, &env
->cr
[3]);
1621 qemu_get_be32s(f
, &env
->cr
[4]);
1623 for(i
= 0; i
< 8; i
++)
1624 qemu_get_be32s(f
, &env
->dr
[i
]);
1627 qemu_get_be32s(f
, &env
->a20_mask
);
1629 /* XXX: compute hflags from scratch, except for CPL and IIF */
1630 env
->hflags
= hflags
;
1635 #elif defined(TARGET_PPC)
1636 void cpu_save(QEMUFile
*f
, void *opaque
)
1640 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1646 #warning No CPU save/restore functions
1650 /***********************************************************/
1651 /* ram save/restore */
1653 /* we just avoid storing empty pages */
1654 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1659 for(i
= 1; i
< len
; i
++) {
1663 qemu_put_byte(f
, 1);
1664 qemu_put_byte(f
, v
);
1667 qemu_put_byte(f
, 0);
1668 qemu_put_buffer(f
, buf
, len
);
1671 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1675 v
= qemu_get_byte(f
);
1678 if (qemu_get_buffer(f
, buf
, len
) != len
)
1682 v
= qemu_get_byte(f
);
1683 memset(buf
, v
, len
);
1691 static void ram_save(QEMUFile
*f
, void *opaque
)
1694 qemu_put_be32(f
, phys_ram_size
);
1695 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1696 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1700 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1704 if (version_id
!= 1)
1706 if (qemu_get_be32(f
) != phys_ram_size
)
1708 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1709 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1716 /***********************************************************/
1717 /* main execution loop */
1719 void gui_update(void *opaque
)
1721 display_state
.dpy_refresh(&display_state
);
1722 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1725 /* XXX: support several handlers */
1726 VMStopHandler
*vm_stop_cb
;
1727 VMStopHandler
*vm_stop_opaque
;
1729 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1732 vm_stop_opaque
= opaque
;
1736 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1749 void vm_stop(int reason
)
1752 cpu_disable_ticks();
1756 vm_stop_cb(vm_stop_opaque
, reason
);
1765 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1766 IOHandlerRecord
*ioh
, *ioh_next
;
1771 CPUState
*env
= global_env
;
1775 ret
= cpu_exec(env
);
1776 if (reset_requested
) {
1777 ret
= EXCP_INTERRUPT
;
1780 if (ret
== EXCP_DEBUG
) {
1781 vm_stop(EXCP_DEBUG
);
1783 /* if hlt instruction, we wait until the next IRQ */
1784 /* XXX: use timeout computed from timers */
1785 if (ret
== EXCP_HLT
)
1798 /* poll any events */
1799 /* XXX: separate device handlers from system ones */
1801 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1802 if (!ioh
->fd_can_read
) {
1805 pf
->events
= POLLIN
;
1809 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1811 if (max_size
> sizeof(buf
))
1812 max_size
= sizeof(buf
);
1814 pf
->events
= POLLIN
;
1821 ioh
->max_size
= max_size
;
1824 ret
= poll(ufds
, pf
- ufds
, timeout
);
1826 /* XXX: better handling of removal */
1827 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1828 ioh_next
= ioh
->next
;
1831 if (pf
->revents
& POLLIN
) {
1832 if (ioh
->max_size
== 0) {
1833 /* just a read event */
1834 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1836 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1838 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1839 } else if (errno
!= EAGAIN
) {
1840 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1848 #if defined(CONFIG_SLIRP)
1849 /* XXX: merge with poll() */
1851 fd_set rfds
, wfds
, xfds
;
1859 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1862 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1864 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1872 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1873 qemu_get_clock(vm_clock
));
1875 if (audio_enabled
) {
1876 /* XXX: add explicit timer */
1880 /* run dma transfers, if any */
1884 /* real time timers */
1885 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1886 qemu_get_clock(rt_clock
));
1888 cpu_disable_ticks();
1894 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
1895 "usage: %s [options] [disk_image]\n"
1897 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1899 "Standard options:\n"
1900 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1901 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1902 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1903 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1904 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1905 "-snapshot write to temporary files instead of disk image files\n"
1906 "-m megs set virtual RAM size to megs MB\n"
1907 "-nographic disable graphical output and redirect serial I/Os to console\n"
1908 "-enable-audio enable audio support\n"
1910 "Network options:\n"
1911 "-nics n simulate 'n' network cards [default=1]\n"
1912 "-macaddr addr set the mac address of the first interface\n"
1913 "-n script set tap/tun network init script [default=%s]\n"
1914 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1916 "-user-net use user mode network stack [default if no tap/tun script]\n"
1918 "-dummy-net use dummy network stack\n"
1920 "Linux boot specific:\n"
1921 "-kernel bzImage use 'bzImage' as kernel image\n"
1922 "-append cmdline use 'cmdline' as kernel command line\n"
1923 "-initrd file use 'file' as initial ram disk\n"
1925 "Debug/Expert options:\n"
1926 "-S freeze CPU at startup (use 'c' to start execution)\n"
1927 "-s wait gdb connection to port %d\n"
1928 "-p port change gdb connection port\n"
1929 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1930 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1931 "-L path set the directory for the BIOS and VGA BIOS\n"
1932 #ifdef USE_CODE_COPY
1933 "-no-code-copy disable code copy acceleration\n"
1937 "During emulation, use C-a h to get terminal commands:\n",
1938 #ifdef CONFIG_SOFTMMU
1943 DEFAULT_NETWORK_SCRIPT
,
1944 DEFAULT_GDBSTUB_PORT
,
1947 #ifndef CONFIG_SOFTMMU
1949 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1950 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1956 #define HAS_ARG 0x0001
1969 QEMU_OPTION_snapshot
,
1971 QEMU_OPTION_nographic
,
1972 QEMU_OPTION_enable_audio
,
1975 QEMU_OPTION_macaddr
,
1978 QEMU_OPTION_user_net
,
1979 QEMU_OPTION_dummy_net
,
1991 QEMU_OPTION_no_code_copy
,
1995 typedef struct QEMUOption
{
2001 const QEMUOption qemu_options
[] = {
2002 { "h", 0, QEMU_OPTION_h
},
2004 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2005 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2006 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2007 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2008 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2009 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2010 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2011 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2012 { "snapshot", 0, QEMU_OPTION_snapshot
},
2013 { "m", HAS_ARG
, QEMU_OPTION_m
},
2014 { "nographic", 0, QEMU_OPTION_nographic
},
2015 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2017 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2018 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2019 { "n", HAS_ARG
, QEMU_OPTION_n
},
2020 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2022 { "user-net", 0, QEMU_OPTION_user_net
},
2024 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2026 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2027 { "append", HAS_ARG
, QEMU_OPTION_append
},
2028 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2030 { "S", 0, QEMU_OPTION_S
},
2031 { "s", 0, QEMU_OPTION_s
},
2032 { "p", HAS_ARG
, QEMU_OPTION_p
},
2033 { "d", HAS_ARG
, QEMU_OPTION_d
},
2034 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2035 { "L", HAS_ARG
, QEMU_OPTION_L
},
2036 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2037 { "pci", 0, QEMU_OPTION_pci
},
2041 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2043 /* this stack is only used during signal handling */
2044 #define SIGNAL_STACK_SIZE 32768
2046 static uint8_t *signal_stack
;
2050 #define NET_IF_TUN 0
2051 #define NET_IF_USER 1
2052 #define NET_IF_DUMMY 2
2054 int main(int argc
, char **argv
)
2056 #ifdef CONFIG_GDBSTUB
2057 int use_gdbstub
, gdbstub_port
;
2060 int snapshot
, linux_boot
;
2062 const char *initrd_filename
;
2063 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2064 const char *kernel_filename
, *kernel_cmdline
;
2065 DisplayState
*ds
= &display_state
;
2066 int cyls
, heads
, secs
;
2067 int start_emulation
= 1;
2069 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2071 const char *r
, *optarg
;
2073 #if !defined(CONFIG_SOFTMMU)
2074 /* we never want that malloc() uses mmap() */
2075 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2077 initrd_filename
= NULL
;
2078 for(i
= 0; i
< MAX_FD
; i
++)
2079 fd_filename
[i
] = NULL
;
2080 for(i
= 0; i
< MAX_DISKS
; i
++)
2081 hd_filename
[i
] = NULL
;
2082 ram_size
= 32 * 1024 * 1024;
2083 vga_ram_size
= VGA_RAM_SIZE
;
2084 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2085 #ifdef CONFIG_GDBSTUB
2087 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2091 kernel_filename
= NULL
;
2092 kernel_cmdline
= "";
2094 cyls
= heads
= secs
= 0;
2099 /* default mac address of the first network interface */
2113 hd_filename
[0] = argv
[optind
++];
2115 const QEMUOption
*popt
;
2118 popt
= qemu_options
;
2121 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2125 if (!strcmp(popt
->name
, r
+ 1))
2129 if (popt
->flags
& HAS_ARG
) {
2130 if (optind
>= argc
) {
2131 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2135 optarg
= argv
[optind
++];
2140 switch(popt
->index
) {
2141 case QEMU_OPTION_initrd
:
2142 initrd_filename
= optarg
;
2144 case QEMU_OPTION_hda
:
2145 hd_filename
[0] = optarg
;
2147 case QEMU_OPTION_hdb
:
2148 hd_filename
[1] = optarg
;
2150 case QEMU_OPTION_snapshot
:
2153 case QEMU_OPTION_hdachs
:
2157 cyls
= strtol(p
, (char **)&p
, 0);
2161 heads
= strtol(p
, (char **)&p
, 0);
2165 secs
= strtol(p
, (char **)&p
, 0);
2172 case QEMU_OPTION_nographic
:
2175 case QEMU_OPTION_kernel
:
2176 kernel_filename
= optarg
;
2178 case QEMU_OPTION_append
:
2179 kernel_cmdline
= optarg
;
2181 case QEMU_OPTION_tun_fd
:
2185 net_if_type
= NET_IF_TUN
;
2186 if (nb_tun_fds
< MAX_NICS
) {
2187 fd
= strtol(optarg
, (char **)&p
, 0);
2189 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2192 tun_fds
[nb_tun_fds
++] = fd
;
2196 case QEMU_OPTION_hdc
:
2197 hd_filename
[2] = optarg
;
2200 case QEMU_OPTION_hdd
:
2201 hd_filename
[3] = optarg
;
2203 case QEMU_OPTION_cdrom
:
2204 hd_filename
[2] = optarg
;
2207 case QEMU_OPTION_boot
:
2208 boot_device
= optarg
[0];
2209 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2210 boot_device
!= 'c' && boot_device
!= 'd') {
2211 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2215 case QEMU_OPTION_fda
:
2216 fd_filename
[0] = optarg
;
2218 case QEMU_OPTION_fdb
:
2219 fd_filename
[1] = optarg
;
2221 case QEMU_OPTION_no_code_copy
:
2222 code_copy_enabled
= 0;
2224 case QEMU_OPTION_nics
:
2225 nb_nics
= atoi(optarg
);
2226 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2227 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2231 case QEMU_OPTION_macaddr
:
2236 for(i
= 0; i
< 6; i
++) {
2237 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2244 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2252 case QEMU_OPTION_user_net
:
2253 net_if_type
= NET_IF_USER
;
2255 case QEMU_OPTION_dummy_net
:
2256 net_if_type
= NET_IF_DUMMY
;
2258 case QEMU_OPTION_enable_audio
:
2265 ram_size
= atoi(optarg
) * 1024 * 1024;
2268 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2269 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2270 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2279 mask
= cpu_str_to_log_mask(optarg
);
2281 printf("Log items (comma separated):\n");
2282 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2283 printf("%-10s %s\n", item
->name
, item
->help
);
2291 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2293 #ifdef CONFIG_GDBSTUB
2298 gdbstub_port
= atoi(optarg
);
2305 start_emulation
= 0;
2307 case QEMU_OPTION_pci
:
2314 linux_boot
= (kernel_filename
!= NULL
);
2316 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2317 fd_filename
[0] == '\0')
2320 /* boot to cd by default if no hard disk */
2321 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2322 if (fd_filename
[0] != '\0')
2328 #if !defined(CONFIG_SOFTMMU)
2329 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2331 static uint8_t stdout_buf
[4096];
2332 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2335 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2338 /* init host network redirectors */
2339 if (net_if_type
== -1) {
2340 net_if_type
= NET_IF_TUN
;
2341 #if defined(CONFIG_SLIRP)
2342 if (access(network_script
, R_OK
) < 0) {
2343 net_if_type
= NET_IF_USER
;
2348 for(i
= 0; i
< nb_nics
; i
++) {
2349 NetDriverState
*nd
= &nd_table
[i
];
2351 /* init virtual mac address */
2352 nd
->macaddr
[0] = macaddr
[0];
2353 nd
->macaddr
[1] = macaddr
[1];
2354 nd
->macaddr
[2] = macaddr
[2];
2355 nd
->macaddr
[3] = macaddr
[3];
2356 nd
->macaddr
[4] = macaddr
[4];
2357 nd
->macaddr
[5] = macaddr
[5] + i
;
2358 switch(net_if_type
) {
2359 #if defined(CONFIG_SLIRP)
2364 #if !defined(_WIN32)
2366 if (i
< nb_tun_fds
) {
2367 net_fd_init(nd
, tun_fds
[i
]);
2369 if (net_tun_init(nd
) < 0)
2381 /* init the memory */
2382 phys_ram_size
= ram_size
+ vga_ram_size
;
2384 #ifdef CONFIG_SOFTMMU
2386 /* mallocs are always aligned on BSD. */
2387 phys_ram_base
= malloc(phys_ram_size
);
2389 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2391 if (!phys_ram_base
) {
2392 fprintf(stderr
, "Could not allocate physical memory\n");
2396 /* as we must map the same page at several addresses, we must use
2401 tmpdir
= getenv("QEMU_TMPDIR");
2404 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2405 if (mkstemp(phys_ram_file
) < 0) {
2406 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2410 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2411 if (phys_ram_fd
< 0) {
2412 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2416 ftruncate(phys_ram_fd
, phys_ram_size
);
2417 unlink(phys_ram_file
);
2418 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2420 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2422 if (phys_ram_base
== MAP_FAILED
) {
2423 fprintf(stderr
, "Could not map physical memory\n");
2429 /* we always create the cdrom drive, even if no disk is there */
2431 bs_table
[2] = bdrv_new("cdrom");
2432 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2435 /* open the virtual block devices */
2436 for(i
= 0; i
< MAX_DISKS
; i
++) {
2437 if (hd_filename
[i
]) {
2440 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2441 bs_table
[i
] = bdrv_new(buf
);
2443 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2444 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2448 if (i
== 0 && cyls
!= 0)
2449 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2453 /* we always create at least one floppy disk */
2454 fd_table
[0] = bdrv_new("fda");
2455 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2457 for(i
= 0; i
< MAX_FD
; i
++) {
2458 if (fd_filename
[i
]) {
2461 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2462 fd_table
[i
] = bdrv_new(buf
);
2463 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2465 if (fd_filename
[i
] != '\0') {
2466 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2467 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2475 /* init CPU state */
2478 cpu_single_env
= env
;
2480 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2481 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2482 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2485 cpu_calibrate_ticks();
2489 dumb_display_init(ds
);
2492 sdl_display_init(ds
);
2494 dumb_display_init(ds
);
2498 /* setup cpu signal handlers for MMU / self modifying code handling */
2499 #if !defined(CONFIG_SOFTMMU)
2501 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2504 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2505 stk
.ss_sp
= signal_stack
;
2506 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2509 if (sigaltstack(&stk
, NULL
) < 0) {
2510 perror("sigaltstack");
2516 struct sigaction act
;
2518 sigfillset(&act
.sa_mask
);
2519 act
.sa_flags
= SA_SIGINFO
;
2520 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2521 act
.sa_flags
|= SA_ONSTACK
;
2523 act
.sa_sigaction
= host_segv_handler
;
2524 sigaction(SIGSEGV
, &act
, NULL
);
2525 sigaction(SIGBUS
, &act
, NULL
);
2526 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2527 sigaction(SIGFPE
, &act
, NULL
);
2534 struct sigaction act
;
2535 sigfillset(&act
.sa_mask
);
2537 act
.sa_handler
= SIG_IGN
;
2538 sigaction(SIGPIPE
, &act
, NULL
);
2543 #if defined(TARGET_I386)
2544 pc_init(ram_size
, vga_ram_size
, boot_device
,
2545 ds
, fd_filename
, snapshot
,
2546 kernel_filename
, kernel_cmdline
, initrd_filename
);
2547 #elif defined(TARGET_PPC)
2548 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2549 ds
, fd_filename
, snapshot
,
2550 kernel_filename
, kernel_cmdline
, initrd_filename
);
2553 /* launched after the device init so that it can display or not a
2557 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2558 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2560 #ifdef CONFIG_GDBSTUB
2562 if (gdbserver_start(gdbstub_port
) < 0) {
2563 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2567 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2571 if (start_emulation
)