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;
125 /***********************************************************/
126 /* x86 ISA bus support */
128 target_phys_addr_t isa_mem_base
= 0;
130 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
132 #ifdef DEBUG_UNUSED_IOPORT
133 fprintf(stderr
, "inb: port=0x%04x\n", address
);
138 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
140 #ifdef DEBUG_UNUSED_IOPORT
141 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
145 /* default is to make two byte accesses */
146 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
149 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
150 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
151 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
155 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
157 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
158 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
159 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
162 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
164 #ifdef DEBUG_UNUSED_IOPORT
165 fprintf(stderr
, "inl: port=0x%04x\n", address
);
170 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
172 #ifdef DEBUG_UNUSED_IOPORT
173 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
177 void init_ioports(void)
181 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
182 ioport_read_table
[0][i
] = default_ioport_readb
;
183 ioport_write_table
[0][i
] = default_ioport_writeb
;
184 ioport_read_table
[1][i
] = default_ioport_readw
;
185 ioport_write_table
[1][i
] = default_ioport_writew
;
186 ioport_read_table
[2][i
] = default_ioport_readl
;
187 ioport_write_table
[2][i
] = default_ioport_writel
;
191 /* size is the word size in byte */
192 int register_ioport_read(int start
, int length
, int size
,
193 IOPortReadFunc
*func
, void *opaque
)
199 } else if (size
== 2) {
201 } else if (size
== 4) {
204 hw_error("register_ioport_read: invalid size");
207 for(i
= start
; i
< start
+ length
; i
+= size
) {
208 ioport_read_table
[bsize
][i
] = func
;
209 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
210 hw_error("register_ioport_read: invalid opaque");
211 ioport_opaque
[i
] = opaque
;
216 /* size is the word size in byte */
217 int register_ioport_write(int start
, int length
, int size
,
218 IOPortWriteFunc
*func
, void *opaque
)
224 } else if (size
== 2) {
226 } else if (size
== 4) {
229 hw_error("register_ioport_write: invalid size");
232 for(i
= start
; i
< start
+ length
; i
+= size
) {
233 ioport_write_table
[bsize
][i
] = func
;
234 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
235 hw_error("register_ioport_read: invalid opaque");
236 ioport_opaque
[i
] = opaque
;
241 void pstrcpy(char *buf
, int buf_size
, const char *str
)
251 if (c
== 0 || q
>= buf
+ buf_size
- 1)
258 /* strcat and truncate. */
259 char *pstrcat(char *buf
, int buf_size
, const char *s
)
264 pstrcpy(buf
+ len
, buf_size
- len
, s
);
268 /* return the size or -1 if error */
269 int load_image(const char *filename
, uint8_t *addr
)
272 fd
= open(filename
, O_RDONLY
| O_BINARY
);
275 size
= lseek(fd
, 0, SEEK_END
);
276 lseek(fd
, 0, SEEK_SET
);
277 if (read(fd
, addr
, size
) != size
) {
285 void cpu_outb(CPUState
*env
, int addr
, int val
)
288 if (loglevel
& CPU_LOG_IOPORT
)
289 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
291 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
294 void cpu_outw(CPUState
*env
, int addr
, int val
)
297 if (loglevel
& CPU_LOG_IOPORT
)
298 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
300 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
303 void cpu_outl(CPUState
*env
, int addr
, int val
)
306 if (loglevel
& CPU_LOG_IOPORT
)
307 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
309 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
312 int cpu_inb(CPUState
*env
, int addr
)
315 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
317 if (loglevel
& CPU_LOG_IOPORT
)
318 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
323 int cpu_inw(CPUState
*env
, int addr
)
326 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
328 if (loglevel
& CPU_LOG_IOPORT
)
329 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
334 int cpu_inl(CPUState
*env
, int addr
)
337 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
339 if (loglevel
& CPU_LOG_IOPORT
)
340 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
345 /***********************************************************/
346 void hw_error(const char *fmt
, ...)
351 fprintf(stderr
, "qemu: hardware error: ");
352 vfprintf(stderr
, fmt
, ap
);
353 fprintf(stderr
, "\n");
355 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
357 cpu_dump_state(global_env
, stderr
, 0);
363 /***********************************************************/
366 #if defined(__powerpc__)
368 static inline uint32_t get_tbl(void)
371 asm volatile("mftb %0" : "=r" (tbl
));
375 static inline uint32_t get_tbu(void)
378 asm volatile("mftbu %0" : "=r" (tbl
));
382 int64_t cpu_get_real_ticks(void)
385 /* NOTE: we test if wrapping has occurred */
391 return ((int64_t)h
<< 32) | l
;
394 #elif defined(__i386__)
396 int64_t cpu_get_real_ticks(void)
399 asm volatile ("rdtsc" : "=A" (val
));
403 #elif defined(__x86_64__)
405 int64_t cpu_get_real_ticks(void)
409 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
417 #error unsupported CPU
420 static int64_t cpu_ticks_offset
;
421 static int cpu_ticks_enabled
;
423 static inline int64_t cpu_get_ticks(void)
425 if (!cpu_ticks_enabled
) {
426 return cpu_ticks_offset
;
428 return cpu_get_real_ticks() + cpu_ticks_offset
;
432 /* enable cpu_get_ticks() */
433 void cpu_enable_ticks(void)
435 if (!cpu_ticks_enabled
) {
436 cpu_ticks_offset
-= cpu_get_real_ticks();
437 cpu_ticks_enabled
= 1;
441 /* disable cpu_get_ticks() : the clock is stopped. You must not call
442 cpu_get_ticks() after that. */
443 void cpu_disable_ticks(void)
445 if (cpu_ticks_enabled
) {
446 cpu_ticks_offset
= cpu_get_ticks();
447 cpu_ticks_enabled
= 0;
451 static int64_t get_clock(void)
456 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
459 gettimeofday(&tv
, NULL
);
460 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
464 void cpu_calibrate_ticks(void)
469 ticks
= cpu_get_real_ticks();
475 usec
= get_clock() - usec
;
476 ticks
= cpu_get_real_ticks() - ticks
;
477 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
480 /* compute with 96 bit intermediate result: (a*b)/c */
481 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
486 #ifdef WORDS_BIGENDIAN
496 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
497 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
500 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
504 #define QEMU_TIMER_REALTIME 0
505 #define QEMU_TIMER_VIRTUAL 1
509 /* XXX: add frequency */
517 struct QEMUTimer
*next
;
523 static QEMUTimer
*active_timers
[2];
525 static MMRESULT timerID
;
527 /* frequency of the times() clock tick */
528 static int timer_freq
;
531 QEMUClock
*qemu_new_clock(int type
)
534 clock
= qemu_mallocz(sizeof(QEMUClock
));
541 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
545 ts
= qemu_mallocz(sizeof(QEMUTimer
));
552 void qemu_free_timer(QEMUTimer
*ts
)
557 /* stop a timer, but do not dealloc it */
558 void qemu_del_timer(QEMUTimer
*ts
)
562 /* NOTE: this code must be signal safe because
563 qemu_timer_expired() can be called from a signal. */
564 pt
= &active_timers
[ts
->clock
->type
];
577 /* modify the current timer so that it will be fired when current_time
578 >= expire_time. The corresponding callback will be called. */
579 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
585 /* add the timer in the sorted list */
586 /* NOTE: this code must be signal safe because
587 qemu_timer_expired() can be called from a signal. */
588 pt
= &active_timers
[ts
->clock
->type
];
593 if (t
->expire_time
> expire_time
)
597 ts
->expire_time
= expire_time
;
602 int qemu_timer_pending(QEMUTimer
*ts
)
605 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
612 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
616 return (timer_head
->expire_time
<= current_time
);
619 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
625 if (ts
->expire_time
> current_time
)
627 /* remove timer from the list before calling the callback */
628 *ptimer_head
= ts
->next
;
631 /* run the callback (the timer list can be modified) */
636 int64_t qemu_get_clock(QEMUClock
*clock
)
638 switch(clock
->type
) {
639 case QEMU_TIMER_REALTIME
:
641 return GetTickCount();
646 /* Note that using gettimeofday() is not a good solution
647 for timers because its value change when the date is
649 if (timer_freq
== 100) {
650 return times(&tp
) * 10;
652 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
657 case QEMU_TIMER_VIRTUAL
:
658 return cpu_get_ticks();
663 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
665 uint64_t expire_time
;
667 if (qemu_timer_pending(ts
)) {
668 expire_time
= ts
->expire_time
;
672 qemu_put_be64(f
, expire_time
);
675 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
677 uint64_t expire_time
;
679 expire_time
= qemu_get_be64(f
);
680 if (expire_time
!= -1) {
681 qemu_mod_timer(ts
, expire_time
);
687 static void timer_save(QEMUFile
*f
, void *opaque
)
689 if (cpu_ticks_enabled
) {
690 hw_error("cannot save state if virtual timers are running");
692 qemu_put_be64s(f
, &cpu_ticks_offset
);
693 qemu_put_be64s(f
, &ticks_per_sec
);
696 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
700 if (cpu_ticks_enabled
) {
703 qemu_get_be64s(f
, &cpu_ticks_offset
);
704 qemu_get_be64s(f
, &ticks_per_sec
);
709 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
710 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
712 static void host_alarm_handler(int host_signum
)
715 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
716 qemu_get_clock(vm_clock
)) ||
717 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
718 qemu_get_clock(rt_clock
))) {
719 /* stop the cpu because a timer occured */
720 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
726 #define RTC_FREQ 1024
730 static int start_rtc_timer(void)
732 rtc_fd
= open("/dev/rtc", O_RDONLY
);
735 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
736 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
737 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
738 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
741 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
746 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
752 static void init_timers(void)
754 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
755 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
760 timerID
= timeSetEvent(10, // interval (ms)
762 host_alarm_handler
, // function
763 (DWORD
)&count
, // user parameter
764 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
766 perror("failed timer alarm");
770 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
773 struct sigaction act
;
774 struct itimerval itv
;
776 /* get times() syscall frequency */
777 timer_freq
= sysconf(_SC_CLK_TCK
);
780 sigfillset(&act
.sa_mask
);
782 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
783 act
.sa_flags
|= SA_ONSTACK
;
785 act
.sa_handler
= host_alarm_handler
;
786 sigaction(SIGALRM
, &act
, NULL
);
788 itv
.it_interval
.tv_sec
= 0;
789 itv
.it_interval
.tv_usec
= 1000;
790 itv
.it_value
.tv_sec
= 0;
791 itv
.it_value
.tv_usec
= 10 * 1000;
792 setitimer(ITIMER_REAL
, &itv
, NULL
);
793 /* we probe the tick duration of the kernel to inform the user if
794 the emulated kernel requested a too high timer frequency */
795 getitimer(ITIMER_REAL
, &itv
);
797 if (itv
.it_interval
.tv_usec
> 1000) {
798 /* try to use /dev/rtc to have a faster timer */
799 if (start_rtc_timer() < 0)
802 itv
.it_interval
.tv_sec
= 0;
803 itv
.it_interval
.tv_usec
= 0;
804 itv
.it_value
.tv_sec
= 0;
805 itv
.it_value
.tv_usec
= 0;
806 setitimer(ITIMER_REAL
, &itv
, NULL
);
809 sigaction(SIGIO
, &act
, NULL
);
810 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
811 fcntl(rtc_fd
, F_SETOWN
, getpid());
814 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
821 void quit_timers(void)
824 timeKillEvent(timerID
);
828 /***********************************************************/
833 int serial_open_device(void)
840 int serial_open_device(void)
842 char slave_name
[1024];
843 int master_fd
, slave_fd
;
845 if (serial_console
== NULL
&& nographic
) {
846 /* use console for serial port */
849 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
850 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
853 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
860 /***********************************************************/
861 /* Linux network device redirectors */
863 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
867 for(i
=0;i
<size
;i
+=16) {
871 fprintf(f
, "%08x ", i
);
874 fprintf(f
, " %02x", buf
[i
+j
]);
881 if (c
< ' ' || c
> '~')
889 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
891 nd
->send_packet(nd
, buf
, size
);
894 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
895 IOReadHandler
*fd_read
, void *opaque
)
897 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
900 /* dummy network adapter */
902 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
906 static void dummy_add_read_packet(NetDriverState
*nd
,
907 IOCanRWHandler
*fd_can_read
,
908 IOReadHandler
*fd_read
, void *opaque
)
912 static int net_dummy_init(NetDriverState
*nd
)
914 nd
->send_packet
= dummy_send_packet
;
915 nd
->add_read_packet
= dummy_add_read_packet
;
916 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
920 #if defined(CONFIG_SLIRP)
922 /* slirp network adapter */
924 static void *slirp_fd_opaque
;
925 static IOCanRWHandler
*slirp_fd_can_read
;
926 static IOReadHandler
*slirp_fd_read
;
927 static int slirp_inited
;
929 int slirp_can_output(void)
931 return slirp_fd_can_read(slirp_fd_opaque
);
934 void slirp_output(const uint8_t *pkt
, int pkt_len
)
938 hex_dump(stdout
, pkt
, pkt_len
);
940 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
943 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
947 hex_dump(stdout
, buf
, size
);
949 slirp_input(buf
, size
);
952 static void slirp_add_read_packet(NetDriverState
*nd
,
953 IOCanRWHandler
*fd_can_read
,
954 IOReadHandler
*fd_read
, void *opaque
)
956 slirp_fd_opaque
= opaque
;
957 slirp_fd_can_read
= fd_can_read
;
958 slirp_fd_read
= fd_read
;
961 static int net_slirp_init(NetDriverState
*nd
)
967 nd
->send_packet
= slirp_send_packet
;
968 nd
->add_read_packet
= slirp_add_read_packet
;
969 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
973 #endif /* CONFIG_SLIRP */
977 static int tun_open(char *ifname
, int ifname_size
)
983 fd
= open("/dev/tap", O_RDWR
);
985 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
990 dev
= devname(s
.st_rdev
, S_IFCHR
);
991 pstrcpy(ifname
, ifname_size
, dev
);
993 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
997 static int tun_open(char *ifname
, int ifname_size
)
1002 fd
= open("/dev/net/tun", O_RDWR
);
1004 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1007 memset(&ifr
, 0, sizeof(ifr
));
1008 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1009 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1010 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1012 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1016 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1017 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1018 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1023 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1025 write(nd
->fd
, buf
, size
);
1028 static void tun_add_read_packet(NetDriverState
*nd
,
1029 IOCanRWHandler
*fd_can_read
,
1030 IOReadHandler
*fd_read
, void *opaque
)
1032 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1035 static int net_tun_init(NetDriverState
*nd
)
1041 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1045 /* try to launch network init script */
1050 *parg
++ = network_script
;
1051 *parg
++ = nd
->ifname
;
1053 execv(network_script
, args
);
1056 while (waitpid(pid
, &status
, 0) != pid
);
1057 if (!WIFEXITED(status
) ||
1058 WEXITSTATUS(status
) != 0) {
1059 fprintf(stderr
, "%s: could not launch network script\n",
1063 nd
->send_packet
= tun_send_packet
;
1064 nd
->add_read_packet
= tun_add_read_packet
;
1068 static int net_fd_init(NetDriverState
*nd
, int fd
)
1071 nd
->send_packet
= tun_send_packet
;
1072 nd
->add_read_packet
= tun_add_read_packet
;
1073 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1077 #endif /* !_WIN32 */
1079 /***********************************************************/
1084 static void term_exit(void)
1088 static void term_init(void)
1094 /* init terminal so that we can grab keys */
1095 static struct termios oldtty
;
1097 static void term_exit(void)
1099 tcsetattr (0, TCSANOW
, &oldtty
);
1102 static void term_init(void)
1106 tcgetattr (0, &tty
);
1109 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1110 |INLCR
|IGNCR
|ICRNL
|IXON
);
1111 tty
.c_oflag
|= OPOST
;
1112 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1113 /* if graphical mode, we allow Ctrl-C handling */
1115 tty
.c_lflag
&= ~ISIG
;
1116 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1119 tty
.c_cc
[VTIME
] = 0;
1121 tcsetattr (0, TCSANOW
, &tty
);
1125 fcntl(0, F_SETFL
, O_NONBLOCK
);
1130 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1134 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1138 static void dumb_refresh(DisplayState
*ds
)
1140 vga_update_display();
1143 void dumb_display_init(DisplayState
*ds
)
1148 ds
->dpy_update
= dumb_update
;
1149 ds
->dpy_resize
= dumb_resize
;
1150 ds
->dpy_refresh
= dumb_refresh
;
1153 #if !defined(CONFIG_SOFTMMU)
1154 /***********************************************************/
1155 /* cpu signal handler */
1156 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1159 if (cpu_signal_handler(host_signum
, info
, puc
))
1166 /***********************************************************/
1169 #define MAX_IO_HANDLERS 64
1171 typedef struct IOHandlerRecord
{
1173 IOCanRWHandler
*fd_can_read
;
1174 IOReadHandler
*fd_read
;
1176 /* temporary data */
1179 struct IOHandlerRecord
*next
;
1182 static IOHandlerRecord
*first_io_handler
;
1184 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1185 IOReadHandler
*fd_read
, void *opaque
)
1187 IOHandlerRecord
*ioh
;
1189 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1193 ioh
->fd_can_read
= fd_can_read
;
1194 ioh
->fd_read
= fd_read
;
1195 ioh
->opaque
= opaque
;
1196 ioh
->next
= first_io_handler
;
1197 first_io_handler
= ioh
;
1201 void qemu_del_fd_read_handler(int fd
)
1203 IOHandlerRecord
**pioh
, *ioh
;
1205 pioh
= &first_io_handler
;
1210 if (ioh
->fd
== fd
) {
1218 /***********************************************************/
1219 /* savevm/loadvm support */
1221 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1223 fwrite(buf
, 1, size
, f
);
1226 void qemu_put_byte(QEMUFile
*f
, int v
)
1231 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1233 qemu_put_byte(f
, v
>> 8);
1234 qemu_put_byte(f
, v
);
1237 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1239 qemu_put_byte(f
, v
>> 24);
1240 qemu_put_byte(f
, v
>> 16);
1241 qemu_put_byte(f
, v
>> 8);
1242 qemu_put_byte(f
, v
);
1245 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1247 qemu_put_be32(f
, v
>> 32);
1248 qemu_put_be32(f
, v
);
1251 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1253 return fread(buf
, 1, size
, f
);
1256 int qemu_get_byte(QEMUFile
*f
)
1266 unsigned int qemu_get_be16(QEMUFile
*f
)
1269 v
= qemu_get_byte(f
) << 8;
1270 v
|= qemu_get_byte(f
);
1274 unsigned int qemu_get_be32(QEMUFile
*f
)
1277 v
= qemu_get_byte(f
) << 24;
1278 v
|= qemu_get_byte(f
) << 16;
1279 v
|= qemu_get_byte(f
) << 8;
1280 v
|= qemu_get_byte(f
);
1284 uint64_t qemu_get_be64(QEMUFile
*f
)
1287 v
= (uint64_t)qemu_get_be32(f
) << 32;
1288 v
|= qemu_get_be32(f
);
1292 int64_t qemu_ftell(QEMUFile
*f
)
1297 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1299 if (fseek(f
, pos
, whence
) < 0)
1304 typedef struct SaveStateEntry
{
1308 SaveStateHandler
*save_state
;
1309 LoadStateHandler
*load_state
;
1311 struct SaveStateEntry
*next
;
1314 static SaveStateEntry
*first_se
;
1316 int register_savevm(const char *idstr
,
1319 SaveStateHandler
*save_state
,
1320 LoadStateHandler
*load_state
,
1323 SaveStateEntry
*se
, **pse
;
1325 se
= qemu_malloc(sizeof(SaveStateEntry
));
1328 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1329 se
->instance_id
= instance_id
;
1330 se
->version_id
= version_id
;
1331 se
->save_state
= save_state
;
1332 se
->load_state
= load_state
;
1333 se
->opaque
= opaque
;
1336 /* add at the end of list */
1338 while (*pse
!= NULL
)
1339 pse
= &(*pse
)->next
;
1344 #define QEMU_VM_FILE_MAGIC 0x5145564d
1345 #define QEMU_VM_FILE_VERSION 0x00000001
1347 int qemu_savevm(const char *filename
)
1351 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1353 saved_vm_running
= vm_running
;
1356 f
= fopen(filename
, "wb");
1362 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1363 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1365 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1367 len
= strlen(se
->idstr
);
1368 qemu_put_byte(f
, len
);
1369 qemu_put_buffer(f
, se
->idstr
, len
);
1371 qemu_put_be32(f
, se
->instance_id
);
1372 qemu_put_be32(f
, se
->version_id
);
1374 /* record size: filled later */
1376 qemu_put_be32(f
, 0);
1378 se
->save_state(f
, se
->opaque
);
1380 /* fill record size */
1382 len
= ftell(f
) - len_pos
- 4;
1383 fseek(f
, len_pos
, SEEK_SET
);
1384 qemu_put_be32(f
, len
);
1385 fseek(f
, cur_pos
, SEEK_SET
);
1391 if (saved_vm_running
)
1396 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1400 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1401 if (!strcmp(se
->idstr
, idstr
) &&
1402 instance_id
== se
->instance_id
)
1408 int qemu_loadvm(const char *filename
)
1412 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1413 int saved_vm_running
;
1417 saved_vm_running
= vm_running
;
1420 f
= fopen(filename
, "rb");
1426 v
= qemu_get_be32(f
);
1427 if (v
!= QEMU_VM_FILE_MAGIC
)
1429 v
= qemu_get_be32(f
);
1430 if (v
!= QEMU_VM_FILE_VERSION
) {
1437 #if defined (DO_TB_FLUSH)
1438 tb_flush(global_env
);
1440 len
= qemu_get_byte(f
);
1443 qemu_get_buffer(f
, idstr
, len
);
1445 instance_id
= qemu_get_be32(f
);
1446 version_id
= qemu_get_be32(f
);
1447 record_len
= qemu_get_be32(f
);
1449 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1450 idstr
, instance_id
, version_id
, record_len
);
1453 se
= find_se(idstr
, instance_id
);
1455 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1456 instance_id
, idstr
);
1458 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1460 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1461 instance_id
, idstr
);
1464 /* always seek to exact end of record */
1465 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1470 if (saved_vm_running
)
1475 /***********************************************************/
1476 /* cpu save/restore */
1478 #if defined(TARGET_I386)
1480 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1482 qemu_put_be32(f
, (uint32_t)dt
->base
);
1483 qemu_put_be32(f
, dt
->limit
);
1484 qemu_put_be32(f
, dt
->flags
);
1487 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1489 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1490 dt
->limit
= qemu_get_be32(f
);
1491 dt
->flags
= qemu_get_be32(f
);
1494 void cpu_save(QEMUFile
*f
, void *opaque
)
1496 CPUState
*env
= opaque
;
1497 uint16_t fptag
, fpus
, fpuc
;
1501 for(i
= 0; i
< 8; i
++)
1502 qemu_put_be32s(f
, &env
->regs
[i
]);
1503 qemu_put_be32s(f
, &env
->eip
);
1504 qemu_put_be32s(f
, &env
->eflags
);
1505 qemu_put_be32s(f
, &env
->eflags
);
1506 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1507 qemu_put_be32s(f
, &hflags
);
1511 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1513 for (i
=7; i
>=0; i
--) {
1515 if (env
->fptags
[i
]) {
1520 qemu_put_be16s(f
, &fpuc
);
1521 qemu_put_be16s(f
, &fpus
);
1522 qemu_put_be16s(f
, &fptag
);
1524 for(i
= 0; i
< 8; i
++) {
1527 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1528 qemu_put_be64(f
, mant
);
1529 qemu_put_be16(f
, exp
);
1532 for(i
= 0; i
< 6; i
++)
1533 cpu_put_seg(f
, &env
->segs
[i
]);
1534 cpu_put_seg(f
, &env
->ldt
);
1535 cpu_put_seg(f
, &env
->tr
);
1536 cpu_put_seg(f
, &env
->gdt
);
1537 cpu_put_seg(f
, &env
->idt
);
1539 qemu_put_be32s(f
, &env
->sysenter_cs
);
1540 qemu_put_be32s(f
, &env
->sysenter_esp
);
1541 qemu_put_be32s(f
, &env
->sysenter_eip
);
1543 qemu_put_be32s(f
, &env
->cr
[0]);
1544 qemu_put_be32s(f
, &env
->cr
[2]);
1545 qemu_put_be32s(f
, &env
->cr
[3]);
1546 qemu_put_be32s(f
, &env
->cr
[4]);
1548 for(i
= 0; i
< 8; i
++)
1549 qemu_put_be32s(f
, &env
->dr
[i
]);
1552 qemu_put_be32s(f
, &env
->a20_mask
);
1555 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1557 CPUState
*env
= opaque
;
1560 uint16_t fpus
, fpuc
, fptag
;
1562 if (version_id
!= 1)
1564 for(i
= 0; i
< 8; i
++)
1565 qemu_get_be32s(f
, &env
->regs
[i
]);
1566 qemu_get_be32s(f
, &env
->eip
);
1567 qemu_get_be32s(f
, &env
->eflags
);
1568 qemu_get_be32s(f
, &env
->eflags
);
1569 qemu_get_be32s(f
, &hflags
);
1571 qemu_get_be16s(f
, &fpuc
);
1572 qemu_get_be16s(f
, &fpus
);
1573 qemu_get_be16s(f
, &fptag
);
1575 for(i
= 0; i
< 8; i
++) {
1578 mant
= qemu_get_be64(f
);
1579 exp
= qemu_get_be16(f
);
1580 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1584 env
->fpstt
= (fpus
>> 11) & 7;
1585 env
->fpus
= fpus
& ~0x3800;
1586 for(i
= 0; i
< 8; i
++) {
1587 env
->fptags
[i
] = ((fptag
& 3) == 3);
1591 for(i
= 0; i
< 6; i
++)
1592 cpu_get_seg(f
, &env
->segs
[i
]);
1593 cpu_get_seg(f
, &env
->ldt
);
1594 cpu_get_seg(f
, &env
->tr
);
1595 cpu_get_seg(f
, &env
->gdt
);
1596 cpu_get_seg(f
, &env
->idt
);
1598 qemu_get_be32s(f
, &env
->sysenter_cs
);
1599 qemu_get_be32s(f
, &env
->sysenter_esp
);
1600 qemu_get_be32s(f
, &env
->sysenter_eip
);
1602 qemu_get_be32s(f
, &env
->cr
[0]);
1603 qemu_get_be32s(f
, &env
->cr
[2]);
1604 qemu_get_be32s(f
, &env
->cr
[3]);
1605 qemu_get_be32s(f
, &env
->cr
[4]);
1607 for(i
= 0; i
< 8; i
++)
1608 qemu_get_be32s(f
, &env
->dr
[i
]);
1611 qemu_get_be32s(f
, &env
->a20_mask
);
1613 /* XXX: compute hflags from scratch, except for CPL and IIF */
1614 env
->hflags
= hflags
;
1619 #elif defined(TARGET_PPC)
1620 void cpu_save(QEMUFile
*f
, void *opaque
)
1624 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1630 #warning No CPU save/restore functions
1634 /***********************************************************/
1635 /* ram save/restore */
1637 /* we just avoid storing empty pages */
1638 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1643 for(i
= 1; i
< len
; i
++) {
1647 qemu_put_byte(f
, 1);
1648 qemu_put_byte(f
, v
);
1651 qemu_put_byte(f
, 0);
1652 qemu_put_buffer(f
, buf
, len
);
1655 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1659 v
= qemu_get_byte(f
);
1662 if (qemu_get_buffer(f
, buf
, len
) != len
)
1666 v
= qemu_get_byte(f
);
1667 memset(buf
, v
, len
);
1675 static void ram_save(QEMUFile
*f
, void *opaque
)
1678 qemu_put_be32(f
, phys_ram_size
);
1679 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1680 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1684 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1688 if (version_id
!= 1)
1690 if (qemu_get_be32(f
) != phys_ram_size
)
1692 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1693 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1700 /***********************************************************/
1701 /* main execution loop */
1703 void gui_update(void *opaque
)
1705 display_state
.dpy_refresh(&display_state
);
1706 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1709 /* XXX: support several handlers */
1710 VMStopHandler
*vm_stop_cb
;
1711 VMStopHandler
*vm_stop_opaque
;
1713 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1716 vm_stop_opaque
= opaque
;
1720 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1733 void vm_stop(int reason
)
1736 cpu_disable_ticks();
1740 vm_stop_cb(vm_stop_opaque
, reason
);
1749 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1750 IOHandlerRecord
*ioh
, *ioh_next
;
1755 CPUState
*env
= global_env
;
1759 ret
= cpu_exec(env
);
1760 if (reset_requested
) {
1761 ret
= EXCP_INTERRUPT
;
1764 if (ret
== EXCP_DEBUG
) {
1765 vm_stop(EXCP_DEBUG
);
1767 /* if hlt instruction, we wait until the next IRQ */
1768 /* XXX: use timeout computed from timers */
1769 if (ret
== EXCP_HLT
)
1782 /* poll any events */
1783 /* XXX: separate device handlers from system ones */
1785 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1786 if (!ioh
->fd_can_read
) {
1789 pf
->events
= POLLIN
;
1793 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1795 if (max_size
> sizeof(buf
))
1796 max_size
= sizeof(buf
);
1798 pf
->events
= POLLIN
;
1805 ioh
->max_size
= max_size
;
1808 ret
= poll(ufds
, pf
- ufds
, timeout
);
1810 /* XXX: better handling of removal */
1811 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1812 ioh_next
= ioh
->next
;
1815 if (pf
->revents
& POLLIN
) {
1816 if (ioh
->max_size
== 0) {
1817 /* just a read event */
1818 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1820 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1822 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1823 } else if (errno
!= -EAGAIN
) {
1824 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1832 #if defined(CONFIG_SLIRP)
1833 /* XXX: merge with poll() */
1835 fd_set rfds
, wfds
, xfds
;
1843 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1846 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1848 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1856 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1857 qemu_get_clock(vm_clock
));
1859 if (audio_enabled
) {
1860 /* XXX: add explicit timer */
1864 /* run dma transfers, if any */
1868 /* real time timers */
1869 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1870 qemu_get_clock(rt_clock
));
1872 cpu_disable_ticks();
1878 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
1879 "usage: %s [options] [disk_image]\n"
1881 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1883 "Standard options:\n"
1884 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1885 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1886 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1887 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1888 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1889 "-snapshot write to temporary files instead of disk image files\n"
1890 "-m megs set virtual RAM size to megs MB\n"
1891 "-nographic disable graphical output and redirect serial I/Os to console\n"
1892 "-enable-audio enable audio support\n"
1894 "Network options:\n"
1895 "-nics n simulate 'n' network cards [default=1]\n"
1896 "-macaddr addr set the mac address of the first interface\n"
1897 "-n script set tap/tun network init script [default=%s]\n"
1898 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1900 "-user-net use user mode network stack [default if no tap/tun script]\n"
1902 "-dummy-net use dummy network stack\n"
1904 "Linux boot specific:\n"
1905 "-kernel bzImage use 'bzImage' as kernel image\n"
1906 "-append cmdline use 'cmdline' as kernel command line\n"
1907 "-initrd file use 'file' as initial ram disk\n"
1909 "Debug/Expert options:\n"
1910 "-S freeze CPU at startup (use 'c' to start execution)\n"
1911 "-s wait gdb connection to port %d\n"
1912 "-p port change gdb connection port\n"
1913 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1914 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1915 "-L path set the directory for the BIOS and VGA BIOS\n"
1916 #ifdef USE_CODE_COPY
1917 "-no-code-copy disable code copy acceleration\n"
1921 "During emulation, use C-a h to get terminal commands:\n",
1922 #ifdef CONFIG_SOFTMMU
1927 DEFAULT_NETWORK_SCRIPT
,
1928 DEFAULT_GDBSTUB_PORT
,
1931 #ifndef CONFIG_SOFTMMU
1933 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1934 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1940 #define HAS_ARG 0x0001
1953 QEMU_OPTION_snapshot
,
1955 QEMU_OPTION_nographic
,
1956 QEMU_OPTION_enable_audio
,
1959 QEMU_OPTION_macaddr
,
1962 QEMU_OPTION_user_net
,
1963 QEMU_OPTION_dummy_net
,
1975 QEMU_OPTION_no_code_copy
,
1978 typedef struct QEMUOption
{
1984 const QEMUOption qemu_options
[] = {
1985 { "h", 0, QEMU_OPTION_h
},
1987 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
1988 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
1989 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
1990 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
1991 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
1992 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
1993 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
1994 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
1995 { "snapshot", 0, QEMU_OPTION_snapshot
},
1996 { "m", HAS_ARG
, QEMU_OPTION_m
},
1997 { "nographic", 0, QEMU_OPTION_nographic
},
1998 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2000 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2001 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2002 { "n", HAS_ARG
, QEMU_OPTION_d
},
2003 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2004 { "user-net", 0, QEMU_OPTION_user_net
},
2005 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2007 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2008 { "append", HAS_ARG
, QEMU_OPTION_append
},
2009 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2011 { "S", 0, QEMU_OPTION_S
},
2012 { "s", 0, QEMU_OPTION_s
},
2013 { "p", HAS_ARG
, QEMU_OPTION_p
},
2014 { "d", HAS_ARG
, QEMU_OPTION_d
},
2015 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2016 { "L", HAS_ARG
, QEMU_OPTION_L
},
2017 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2021 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2023 /* this stack is only used during signal handling */
2024 #define SIGNAL_STACK_SIZE 32768
2026 static uint8_t *signal_stack
;
2030 #define NET_IF_TUN 0
2031 #define NET_IF_USER 1
2032 #define NET_IF_DUMMY 2
2034 int main(int argc
, char **argv
)
2036 #ifdef CONFIG_GDBSTUB
2037 int use_gdbstub
, gdbstub_port
;
2040 int snapshot
, linux_boot
;
2042 const char *initrd_filename
;
2043 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
2044 const char *kernel_filename
, *kernel_cmdline
;
2045 DisplayState
*ds
= &display_state
;
2046 int cyls
, heads
, secs
;
2047 int start_emulation
= 1;
2049 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
2051 const char *r
, *optarg
;
2053 #if !defined(CONFIG_SOFTMMU)
2054 /* we never want that malloc() uses mmap() */
2055 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
2057 initrd_filename
= NULL
;
2058 for(i
= 0; i
< MAX_FD
; i
++)
2059 fd_filename
[i
] = NULL
;
2060 for(i
= 0; i
< MAX_DISKS
; i
++)
2061 hd_filename
[i
] = NULL
;
2062 ram_size
= 32 * 1024 * 1024;
2063 vga_ram_size
= VGA_RAM_SIZE
;
2064 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
2065 #ifdef CONFIG_GDBSTUB
2067 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
2071 kernel_filename
= NULL
;
2072 kernel_cmdline
= "";
2074 cyls
= heads
= secs
= 0;
2079 /* default mac address of the first network interface */
2093 hd_filename
[0] = argv
[optind
++];
2095 const QEMUOption
*popt
;
2098 popt
= qemu_options
;
2101 fprintf(stderr
, "%s: invalid option -- '%s'\n",
2105 if (!strcmp(popt
->name
, r
+ 1))
2109 if (popt
->flags
& HAS_ARG
) {
2110 if (optind
>= argc
) {
2111 fprintf(stderr
, "%s: option '%s' requires an argument\n",
2115 optarg
= argv
[optind
++];
2120 switch(popt
->index
) {
2121 case QEMU_OPTION_initrd
:
2122 initrd_filename
= optarg
;
2124 case QEMU_OPTION_hda
:
2125 hd_filename
[0] = optarg
;
2127 case QEMU_OPTION_hdb
:
2128 hd_filename
[1] = optarg
;
2130 case QEMU_OPTION_snapshot
:
2133 case QEMU_OPTION_hdachs
:
2137 cyls
= strtol(p
, (char **)&p
, 0);
2141 heads
= strtol(p
, (char **)&p
, 0);
2145 secs
= strtol(p
, (char **)&p
, 0);
2152 case QEMU_OPTION_nographic
:
2155 case QEMU_OPTION_kernel
:
2156 kernel_filename
= optarg
;
2158 case QEMU_OPTION_append
:
2159 kernel_cmdline
= optarg
;
2161 case QEMU_OPTION_tun_fd
:
2165 net_if_type
= NET_IF_TUN
;
2166 if (nb_tun_fds
< MAX_NICS
) {
2167 fd
= strtol(optarg
, (char **)&p
, 0);
2169 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
2172 tun_fds
[nb_tun_fds
++] = fd
;
2176 case QEMU_OPTION_hdc
:
2177 hd_filename
[2] = optarg
;
2180 case QEMU_OPTION_hdd
:
2181 hd_filename
[3] = optarg
;
2183 case QEMU_OPTION_cdrom
:
2184 hd_filename
[2] = optarg
;
2187 case QEMU_OPTION_boot
:
2188 boot_device
= optarg
[0];
2189 if (boot_device
!= 'a' && boot_device
!= 'b' &&
2190 boot_device
!= 'c' && boot_device
!= 'd') {
2191 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
2195 case QEMU_OPTION_fda
:
2196 fd_filename
[0] = optarg
;
2198 case QEMU_OPTION_fdb
:
2199 fd_filename
[1] = optarg
;
2201 case QEMU_OPTION_no_code_copy
:
2202 code_copy_enabled
= 0;
2204 case QEMU_OPTION_nics
:
2205 nb_nics
= atoi(optarg
);
2206 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
2207 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2211 case QEMU_OPTION_macaddr
:
2216 for(i
= 0; i
< 6; i
++) {
2217 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2224 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2232 case QEMU_OPTION_user_net
:
2233 net_if_type
= NET_IF_USER
;
2235 case QEMU_OPTION_dummy_net
:
2236 net_if_type
= NET_IF_DUMMY
;
2238 case QEMU_OPTION_enable_audio
:
2245 ram_size
= atoi(optarg
) * 1024 * 1024;
2248 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2249 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2250 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2259 mask
= cpu_str_to_log_mask(optarg
);
2261 printf("Log items (comma separated):\n");
2262 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2263 printf("%-10s %s\n", item
->name
, item
->help
);
2271 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2273 #ifdef CONFIG_GDBSTUB
2278 gdbstub_port
= atoi(optarg
);
2285 start_emulation
= 0;
2291 linux_boot
= (kernel_filename
!= NULL
);
2293 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2294 fd_filename
[0] == '\0')
2297 /* boot to cd by default if no hard disk */
2298 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2299 if (fd_filename
[0] != '\0')
2305 #if !defined(CONFIG_SOFTMMU)
2306 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2308 static uint8_t stdout_buf
[4096];
2309 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2312 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2315 /* init host network redirectors */
2316 if (net_if_type
== -1) {
2317 net_if_type
= NET_IF_TUN
;
2318 #if defined(CONFIG_SLIRP)
2319 if (access(network_script
, R_OK
) < 0) {
2320 net_if_type
= NET_IF_USER
;
2325 for(i
= 0; i
< nb_nics
; i
++) {
2326 NetDriverState
*nd
= &nd_table
[i
];
2328 /* init virtual mac address */
2329 nd
->macaddr
[0] = macaddr
[0];
2330 nd
->macaddr
[1] = macaddr
[1];
2331 nd
->macaddr
[2] = macaddr
[2];
2332 nd
->macaddr
[3] = macaddr
[3];
2333 nd
->macaddr
[4] = macaddr
[4];
2334 nd
->macaddr
[5] = macaddr
[5] + i
;
2335 switch(net_if_type
) {
2336 #if defined(CONFIG_SLIRP)
2341 #if !defined(_WIN32)
2343 if (i
< nb_tun_fds
) {
2344 net_fd_init(nd
, tun_fds
[i
]);
2346 if (net_tun_init(nd
) < 0)
2358 /* init the memory */
2359 phys_ram_size
= ram_size
+ vga_ram_size
;
2361 #ifdef CONFIG_SOFTMMU
2363 /* mallocs are always aligned on BSD. */
2364 phys_ram_base
= malloc(phys_ram_size
);
2366 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2368 if (!phys_ram_base
) {
2369 fprintf(stderr
, "Could not allocate physical memory\n");
2373 /* as we must map the same page at several addresses, we must use
2378 tmpdir
= getenv("QEMU_TMPDIR");
2381 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2382 if (mkstemp(phys_ram_file
) < 0) {
2383 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2387 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2388 if (phys_ram_fd
< 0) {
2389 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2393 ftruncate(phys_ram_fd
, phys_ram_size
);
2394 unlink(phys_ram_file
);
2395 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2397 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2399 if (phys_ram_base
== MAP_FAILED
) {
2400 fprintf(stderr
, "Could not map physical memory\n");
2406 /* we always create the cdrom drive, even if no disk is there */
2408 bs_table
[2] = bdrv_new("cdrom");
2409 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2412 /* open the virtual block devices */
2413 for(i
= 0; i
< MAX_DISKS
; i
++) {
2414 if (hd_filename
[i
]) {
2417 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2418 bs_table
[i
] = bdrv_new(buf
);
2420 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2421 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2425 if (i
== 0 && cyls
!= 0)
2426 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2430 /* we always create at least one floppy disk */
2431 fd_table
[0] = bdrv_new("fda");
2432 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2434 for(i
= 0; i
< MAX_FD
; i
++) {
2435 if (fd_filename
[i
]) {
2438 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2439 fd_table
[i
] = bdrv_new(buf
);
2440 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2442 if (fd_filename
[i
] != '\0') {
2443 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2444 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2452 /* init CPU state */
2455 cpu_single_env
= env
;
2457 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2458 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2459 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2462 cpu_calibrate_ticks();
2466 dumb_display_init(ds
);
2469 sdl_display_init(ds
);
2471 dumb_display_init(ds
);
2475 /* setup cpu signal handlers for MMU / self modifying code handling */
2476 #if !defined(CONFIG_SOFTMMU)
2478 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2481 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2482 stk
.ss_sp
= signal_stack
;
2483 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2486 if (sigaltstack(&stk
, NULL
) < 0) {
2487 perror("sigaltstack");
2493 struct sigaction act
;
2495 sigfillset(&act
.sa_mask
);
2496 act
.sa_flags
= SA_SIGINFO
;
2497 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2498 act
.sa_flags
|= SA_ONSTACK
;
2500 act
.sa_sigaction
= host_segv_handler
;
2501 sigaction(SIGSEGV
, &act
, NULL
);
2502 sigaction(SIGBUS
, &act
, NULL
);
2503 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2504 sigaction(SIGFPE
, &act
, NULL
);
2511 struct sigaction act
;
2512 sigfillset(&act
.sa_mask
);
2514 act
.sa_handler
= SIG_IGN
;
2515 sigaction(SIGPIPE
, &act
, NULL
);
2520 #if defined(TARGET_I386)
2521 pc_init(ram_size
, vga_ram_size
, boot_device
,
2522 ds
, fd_filename
, snapshot
,
2523 kernel_filename
, kernel_cmdline
, initrd_filename
);
2524 #elif defined(TARGET_PPC)
2525 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2526 ds
, fd_filename
, snapshot
,
2527 kernel_filename
, kernel_cmdline
, initrd_filename
);
2530 /* launched after the device init so that it can display or not a
2534 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2535 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2537 #ifdef CONFIG_GDBSTUB
2539 if (gdbserver_start(gdbstub_port
) < 0) {
2540 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2544 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2548 if (start_emulation
)