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
36 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/socket.h>
45 #include <linux/if_tun.h>
48 #if defined(CONFIG_SLIRP)
53 #include <sys/timeb.h>
55 #define getopt_long_only getopt_long
56 #define memalign(align, size) malloc(size)
60 /* SDL use the pthreads and they modify sigaction. We don't
62 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
63 extern void __libc_sigaction();
64 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
66 extern void __sigaction();
67 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
69 #endif /* CONFIG_SDL */
77 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
79 //#define DEBUG_UNUSED_IOPORT
81 #if !defined(CONFIG_SOFTMMU)
82 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
84 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
88 #define GUI_REFRESH_INTERVAL 30
90 /* XXX: use a two level table to limit memory usage */
91 #define MAX_IOPORTS 65536
93 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
94 char phys_ram_file
[1024];
96 CPUState
*cpu_single_env
;
97 void *ioport_opaque
[MAX_IOPORTS
];
98 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
99 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
100 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
102 static DisplayState display_state
;
104 int64_t ticks_per_sec
;
105 int boot_device
= 'c';
107 static char network_script
[1024];
108 int pit_min_timer_count
= 0;
110 NetDriverState nd_table
[MAX_NICS
];
111 SerialState
*serial_console
;
112 QEMUTimer
*gui_timer
;
115 /***********************************************************/
118 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
120 #ifdef DEBUG_UNUSED_IOPORT
121 fprintf(stderr
, "inb: port=0x%04x\n", address
);
126 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
128 #ifdef DEBUG_UNUSED_IOPORT
129 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
133 /* default is to make two byte accesses */
134 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
137 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
);
138 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1) << 8;
142 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
144 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
, data
& 0xff);
145 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1, (data
>> 8) & 0xff);
148 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
150 #ifdef DEBUG_UNUSED_IOPORT
151 fprintf(stderr
, "inl: port=0x%04x\n", address
);
156 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
158 #ifdef DEBUG_UNUSED_IOPORT
159 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
163 void init_ioports(void)
167 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
168 ioport_read_table
[0][i
] = default_ioport_readb
;
169 ioport_write_table
[0][i
] = default_ioport_writeb
;
170 ioport_read_table
[1][i
] = default_ioport_readw
;
171 ioport_write_table
[1][i
] = default_ioport_writew
;
172 ioport_read_table
[2][i
] = default_ioport_readl
;
173 ioport_write_table
[2][i
] = default_ioport_writel
;
177 /* size is the word size in byte */
178 int register_ioport_read(int start
, int length
, int size
,
179 IOPortReadFunc
*func
, void *opaque
)
185 } else if (size
== 2) {
187 } else if (size
== 4) {
190 hw_error("register_ioport_read: invalid size");
193 for(i
= start
; i
< start
+ length
; i
+= size
) {
194 ioport_read_table
[bsize
][i
] = func
;
195 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
196 hw_error("register_ioport_read: invalid opaque");
197 ioport_opaque
[i
] = opaque
;
202 /* size is the word size in byte */
203 int register_ioport_write(int start
, int length
, int size
,
204 IOPortWriteFunc
*func
, void *opaque
)
210 } else if (size
== 2) {
212 } else if (size
== 4) {
215 hw_error("register_ioport_write: invalid size");
218 for(i
= start
; i
< start
+ length
; i
+= size
) {
219 ioport_write_table
[bsize
][i
] = func
;
220 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
221 hw_error("register_ioport_read: invalid opaque");
222 ioport_opaque
[i
] = opaque
;
227 void pstrcpy(char *buf
, int buf_size
, const char *str
)
237 if (c
== 0 || q
>= buf
+ buf_size
- 1)
244 /* strcat and truncate. */
245 char *pstrcat(char *buf
, int buf_size
, const char *s
)
250 pstrcpy(buf
+ len
, buf_size
- len
, s
);
254 /* return the size or -1 if error */
255 int load_image(const char *filename
, uint8_t *addr
)
258 fd
= open(filename
, O_RDONLY
| O_BINARY
);
261 size
= lseek(fd
, 0, SEEK_END
);
262 lseek(fd
, 0, SEEK_SET
);
263 if (read(fd
, addr
, size
) != size
) {
271 void cpu_outb(CPUState
*env
, int addr
, int val
)
273 addr
&= (MAX_IOPORTS
- 1);
274 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
277 void cpu_outw(CPUState
*env
, int addr
, int val
)
279 addr
&= (MAX_IOPORTS
- 1);
280 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
283 void cpu_outl(CPUState
*env
, int addr
, int val
)
285 addr
&= (MAX_IOPORTS
- 1);
286 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
289 int cpu_inb(CPUState
*env
, int addr
)
291 addr
&= (MAX_IOPORTS
- 1);
292 return ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
295 int cpu_inw(CPUState
*env
, int addr
)
297 addr
&= (MAX_IOPORTS
- 1);
298 return ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
301 int cpu_inl(CPUState
*env
, int addr
)
303 addr
&= (MAX_IOPORTS
- 1);
304 return ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
307 /***********************************************************/
308 void hw_error(const char *fmt
, ...)
313 fprintf(stderr
, "qemu: hardware error: ");
314 vfprintf(stderr
, fmt
, ap
);
315 fprintf(stderr
, "\n");
317 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
319 cpu_dump_state(global_env
, stderr
, 0);
325 /***********************************************************/
328 #if defined(__powerpc__)
330 static inline uint32_t get_tbl(void)
333 asm volatile("mftb %0" : "=r" (tbl
));
337 static inline uint32_t get_tbu(void)
340 asm volatile("mftbu %0" : "=r" (tbl
));
344 int64_t cpu_get_real_ticks(void)
347 /* NOTE: we test if wrapping has occurred */
353 return ((int64_t)h
<< 32) | l
;
356 #elif defined(__i386__)
358 int64_t cpu_get_real_ticks(void)
361 asm volatile ("rdtsc" : "=A" (val
));
365 #elif defined(__x86_64__)
367 int64_t cpu_get_real_ticks(void)
371 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
379 #error unsupported CPU
382 static int64_t cpu_ticks_offset
;
383 static int cpu_ticks_enabled
;
385 static inline int64_t cpu_get_ticks(void)
387 if (!cpu_ticks_enabled
) {
388 return cpu_ticks_offset
;
390 return cpu_get_real_ticks() + cpu_ticks_offset
;
394 /* enable cpu_get_ticks() */
395 void cpu_enable_ticks(void)
397 if (!cpu_ticks_enabled
) {
398 cpu_ticks_offset
-= cpu_get_real_ticks();
399 cpu_ticks_enabled
= 1;
403 /* disable cpu_get_ticks() : the clock is stopped. You must not call
404 cpu_get_ticks() after that. */
405 void cpu_disable_ticks(void)
407 if (cpu_ticks_enabled
) {
408 cpu_ticks_offset
= cpu_get_ticks();
409 cpu_ticks_enabled
= 0;
413 static int64_t get_clock(void)
418 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
421 gettimeofday(&tv
, NULL
);
422 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
426 void cpu_calibrate_ticks(void)
431 ticks
= cpu_get_real_ticks();
437 usec
= get_clock() - usec
;
438 ticks
= cpu_get_real_ticks() - ticks
;
439 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
442 /* compute with 96 bit intermediate result: (a*b)/c */
443 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
448 #ifdef WORDS_BIGENDIAN
458 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
459 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
462 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
466 #define QEMU_TIMER_REALTIME 0
467 #define QEMU_TIMER_VIRTUAL 1
471 /* XXX: add frequency */
479 struct QEMUTimer
*next
;
485 static QEMUTimer
*active_timers
[2];
487 static MMRESULT timerID
;
489 /* frequency of the times() clock tick */
490 static int timer_freq
;
493 QEMUClock
*qemu_new_clock(int type
)
496 clock
= qemu_mallocz(sizeof(QEMUClock
));
503 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
507 ts
= qemu_mallocz(sizeof(QEMUTimer
));
514 void qemu_free_timer(QEMUTimer
*ts
)
519 /* stop a timer, but do not dealloc it */
520 void qemu_del_timer(QEMUTimer
*ts
)
524 /* NOTE: this code must be signal safe because
525 qemu_timer_expired() can be called from a signal. */
526 pt
= &active_timers
[ts
->clock
->type
];
539 /* modify the current timer so that it will be fired when current_time
540 >= expire_time. The corresponding callback will be called. */
541 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
547 /* add the timer in the sorted list */
548 /* NOTE: this code must be signal safe because
549 qemu_timer_expired() can be called from a signal. */
550 pt
= &active_timers
[ts
->clock
->type
];
555 if (t
->expire_time
> expire_time
)
559 ts
->expire_time
= expire_time
;
564 int qemu_timer_pending(QEMUTimer
*ts
)
567 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
574 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
578 return (timer_head
->expire_time
<= current_time
);
581 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
587 if (ts
->expire_time
> current_time
)
589 /* remove timer from the list before calling the callback */
590 *ptimer_head
= ts
->next
;
593 /* run the callback (the timer list can be modified) */
598 int64_t qemu_get_clock(QEMUClock
*clock
)
600 switch(clock
->type
) {
601 case QEMU_TIMER_REALTIME
:
603 return GetTickCount();
605 /* XXX: portability among Linux hosts */
606 if (timer_freq
== 100) {
607 return times(NULL
) * 10;
609 return ((int64_t)times(NULL
) * 1000) / timer_freq
;
613 case QEMU_TIMER_VIRTUAL
:
614 return cpu_get_ticks();
619 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
621 uint64_t expire_time
;
623 if (qemu_timer_pending(ts
)) {
624 expire_time
= ts
->expire_time
;
628 qemu_put_be64(f
, expire_time
);
631 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
633 uint64_t expire_time
;
635 expire_time
= qemu_get_be64(f
);
636 if (expire_time
!= -1) {
637 qemu_mod_timer(ts
, expire_time
);
643 static void timer_save(QEMUFile
*f
, void *opaque
)
645 if (cpu_ticks_enabled
) {
646 hw_error("cannot save state if virtual timers are running");
648 qemu_put_be64s(f
, &cpu_ticks_offset
);
649 qemu_put_be64s(f
, &ticks_per_sec
);
652 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
656 if (cpu_ticks_enabled
) {
659 qemu_get_be64s(f
, &cpu_ticks_offset
);
660 qemu_get_be64s(f
, &ticks_per_sec
);
665 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
666 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
668 static void host_alarm_handler(int host_signum
)
671 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
672 qemu_get_clock(vm_clock
)) ||
673 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
674 qemu_get_clock(rt_clock
))) {
675 /* stop the cpu because a timer occured */
676 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
680 static void init_timers(void)
682 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
683 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
688 timerID
= timeSetEvent(10, // interval (ms)
690 host_alarm_handler
, // function
691 (DWORD
)&count
, // user parameter
692 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
694 perror("failed timer alarm");
698 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
701 struct sigaction act
;
702 struct itimerval itv
;
704 /* get times() syscall frequency */
705 timer_freq
= sysconf(_SC_CLK_TCK
);
708 sigfillset(&act
.sa_mask
);
710 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
711 act
.sa_flags
|= SA_ONSTACK
;
713 act
.sa_handler
= host_alarm_handler
;
714 sigaction(SIGALRM
, &act
, NULL
);
716 itv
.it_interval
.tv_sec
= 0;
717 itv
.it_interval
.tv_usec
= 1000;
718 itv
.it_value
.tv_sec
= 0;
719 itv
.it_value
.tv_usec
= 10 * 1000;
720 setitimer(ITIMER_REAL
, &itv
, NULL
);
721 /* we probe the tick duration of the kernel to inform the user if
722 the emulated kernel requested a too high timer frequency */
723 getitimer(ITIMER_REAL
, &itv
);
724 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
730 void quit_timers(void)
733 timeKillEvent(timerID
);
737 /***********************************************************/
742 int serial_open_device(void)
749 int serial_open_device(void)
751 char slave_name
[1024];
752 int master_fd
, slave_fd
;
754 if (serial_console
== NULL
&& nographic
) {
755 /* use console for serial port */
758 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
759 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
762 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
769 /***********************************************************/
770 /* Linux network device redirectors */
772 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
776 for(i
=0;i
<size
;i
+=16) {
780 fprintf(f
, "%08x ", i
);
783 fprintf(f
, " %02x", buf
[i
+j
]);
790 if (c
< ' ' || c
> '~')
798 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
800 nd
->send_packet(nd
, buf
, size
);
803 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
804 IOReadHandler
*fd_read
, void *opaque
)
806 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
809 /* dummy network adapter */
811 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
815 static void dummy_add_read_packet(NetDriverState
*nd
,
816 IOCanRWHandler
*fd_can_read
,
817 IOReadHandler
*fd_read
, void *opaque
)
821 static int net_dummy_init(NetDriverState
*nd
)
823 nd
->send_packet
= dummy_send_packet
;
824 nd
->add_read_packet
= dummy_add_read_packet
;
825 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
829 #if defined(CONFIG_SLIRP)
831 /* slirp network adapter */
833 static void *slirp_fd_opaque
;
834 static IOCanRWHandler
*slirp_fd_can_read
;
835 static IOReadHandler
*slirp_fd_read
;
836 static int slirp_inited
;
838 int slirp_can_output(void)
840 return slirp_fd_can_read(slirp_fd_opaque
);
843 void slirp_output(const uint8_t *pkt
, int pkt_len
)
847 hex_dump(stdout
, pkt
, pkt_len
);
849 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
852 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
856 hex_dump(stdout
, buf
, size
);
858 slirp_input(buf
, size
);
861 static void slirp_add_read_packet(NetDriverState
*nd
,
862 IOCanRWHandler
*fd_can_read
,
863 IOReadHandler
*fd_read
, void *opaque
)
865 slirp_fd_opaque
= opaque
;
866 slirp_fd_can_read
= fd_can_read
;
867 slirp_fd_read
= fd_read
;
870 static int net_slirp_init(NetDriverState
*nd
)
876 nd
->send_packet
= slirp_send_packet
;
877 nd
->add_read_packet
= slirp_add_read_packet
;
878 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
882 #endif /* CONFIG_SLIRP */
886 static int tun_open(char *ifname
, int ifname_size
)
891 fd
= open("/dev/net/tun", O_RDWR
);
893 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
896 memset(&ifr
, 0, sizeof(ifr
));
897 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
898 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
899 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
901 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
905 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
906 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
907 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
911 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
913 write(nd
->fd
, buf
, size
);
916 static void tun_add_read_packet(NetDriverState
*nd
,
917 IOCanRWHandler
*fd_can_read
,
918 IOReadHandler
*fd_read
, void *opaque
)
920 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
923 static int net_tun_init(NetDriverState
*nd
)
929 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
933 /* try to launch network init script */
938 *parg
++ = network_script
;
939 *parg
++ = nd
->ifname
;
941 execv(network_script
, args
);
944 while (waitpid(pid
, &status
, 0) != pid
);
945 if (!WIFEXITED(status
) ||
946 WEXITSTATUS(status
) != 0) {
947 fprintf(stderr
, "%s: could not launch network script\n",
951 nd
->send_packet
= tun_send_packet
;
952 nd
->add_read_packet
= tun_add_read_packet
;
956 static int net_fd_init(NetDriverState
*nd
, int fd
)
959 nd
->send_packet
= tun_send_packet
;
960 nd
->add_read_packet
= tun_add_read_packet
;
961 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
967 /***********************************************************/
972 static void term_exit(void)
976 static void term_init(void)
982 /* init terminal so that we can grab keys */
983 static struct termios oldtty
;
985 static void term_exit(void)
987 tcsetattr (0, TCSANOW
, &oldtty
);
990 static void term_init(void)
997 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
998 |INLCR
|IGNCR
|ICRNL
|IXON
);
999 tty
.c_oflag
|= OPOST
;
1000 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1001 /* if graphical mode, we allow Ctrl-C handling */
1003 tty
.c_lflag
&= ~ISIG
;
1004 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1007 tty
.c_cc
[VTIME
] = 0;
1009 tcsetattr (0, TCSANOW
, &tty
);
1013 fcntl(0, F_SETFL
, O_NONBLOCK
);
1018 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1022 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1026 static void dumb_refresh(DisplayState
*ds
)
1028 vga_update_display();
1031 void dumb_display_init(DisplayState
*ds
)
1036 ds
->dpy_update
= dumb_update
;
1037 ds
->dpy_resize
= dumb_resize
;
1038 ds
->dpy_refresh
= dumb_refresh
;
1041 #if !defined(CONFIG_SOFTMMU)
1042 /***********************************************************/
1043 /* cpu signal handler */
1044 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1047 if (cpu_signal_handler(host_signum
, info
, puc
))
1054 /***********************************************************/
1057 #define MAX_IO_HANDLERS 64
1059 typedef struct IOHandlerRecord
{
1061 IOCanRWHandler
*fd_can_read
;
1062 IOReadHandler
*fd_read
;
1064 /* temporary data */
1067 struct IOHandlerRecord
*next
;
1070 static IOHandlerRecord
*first_io_handler
;
1072 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1073 IOReadHandler
*fd_read
, void *opaque
)
1075 IOHandlerRecord
*ioh
;
1077 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1081 ioh
->fd_can_read
= fd_can_read
;
1082 ioh
->fd_read
= fd_read
;
1083 ioh
->opaque
= opaque
;
1084 ioh
->next
= first_io_handler
;
1085 first_io_handler
= ioh
;
1089 void qemu_del_fd_read_handler(int fd
)
1091 IOHandlerRecord
**pioh
, *ioh
;
1093 pioh
= &first_io_handler
;
1098 if (ioh
->fd
== fd
) {
1106 /***********************************************************/
1107 /* savevm/loadvm support */
1109 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1111 fwrite(buf
, 1, size
, f
);
1114 void qemu_put_byte(QEMUFile
*f
, int v
)
1119 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1121 qemu_put_byte(f
, v
>> 8);
1122 qemu_put_byte(f
, v
);
1125 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1127 qemu_put_byte(f
, v
>> 24);
1128 qemu_put_byte(f
, v
>> 16);
1129 qemu_put_byte(f
, v
>> 8);
1130 qemu_put_byte(f
, v
);
1133 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1135 qemu_put_be32(f
, v
>> 32);
1136 qemu_put_be32(f
, v
);
1139 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1141 return fread(buf
, 1, size
, f
);
1144 int qemu_get_byte(QEMUFile
*f
)
1154 unsigned int qemu_get_be16(QEMUFile
*f
)
1157 v
= qemu_get_byte(f
) << 8;
1158 v
|= qemu_get_byte(f
);
1162 unsigned int qemu_get_be32(QEMUFile
*f
)
1165 v
= qemu_get_byte(f
) << 24;
1166 v
|= qemu_get_byte(f
) << 16;
1167 v
|= qemu_get_byte(f
) << 8;
1168 v
|= qemu_get_byte(f
);
1172 uint64_t qemu_get_be64(QEMUFile
*f
)
1175 v
= (uint64_t)qemu_get_be32(f
) << 32;
1176 v
|= qemu_get_be32(f
);
1180 int64_t qemu_ftell(QEMUFile
*f
)
1185 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1187 if (fseek(f
, pos
, whence
) < 0)
1192 typedef struct SaveStateEntry
{
1196 SaveStateHandler
*save_state
;
1197 LoadStateHandler
*load_state
;
1199 struct SaveStateEntry
*next
;
1202 static SaveStateEntry
*first_se
;
1204 int register_savevm(const char *idstr
,
1207 SaveStateHandler
*save_state
,
1208 LoadStateHandler
*load_state
,
1211 SaveStateEntry
*se
, **pse
;
1213 se
= qemu_malloc(sizeof(SaveStateEntry
));
1216 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1217 se
->instance_id
= instance_id
;
1218 se
->version_id
= version_id
;
1219 se
->save_state
= save_state
;
1220 se
->load_state
= load_state
;
1221 se
->opaque
= opaque
;
1224 /* add at the end of list */
1226 while (*pse
!= NULL
)
1227 pse
= &(*pse
)->next
;
1232 #define QEMU_VM_FILE_MAGIC 0x5145564d
1233 #define QEMU_VM_FILE_VERSION 0x00000001
1235 int qemu_savevm(const char *filename
)
1239 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1241 saved_vm_running
= vm_running
;
1244 f
= fopen(filename
, "wb");
1250 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1251 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1253 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1255 len
= strlen(se
->idstr
);
1256 qemu_put_byte(f
, len
);
1257 qemu_put_buffer(f
, se
->idstr
, len
);
1259 qemu_put_be32(f
, se
->instance_id
);
1260 qemu_put_be32(f
, se
->version_id
);
1262 /* record size: filled later */
1264 qemu_put_be32(f
, 0);
1266 se
->save_state(f
, se
->opaque
);
1268 /* fill record size */
1270 len
= ftell(f
) - len_pos
- 4;
1271 fseek(f
, len_pos
, SEEK_SET
);
1272 qemu_put_be32(f
, len
);
1273 fseek(f
, cur_pos
, SEEK_SET
);
1279 if (saved_vm_running
)
1284 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1288 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1289 if (!strcmp(se
->idstr
, idstr
) &&
1290 instance_id
== se
->instance_id
)
1296 int qemu_loadvm(const char *filename
)
1300 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1301 int saved_vm_running
;
1305 saved_vm_running
= vm_running
;
1308 f
= fopen(filename
, "rb");
1314 v
= qemu_get_be32(f
);
1315 if (v
!= QEMU_VM_FILE_MAGIC
)
1317 v
= qemu_get_be32(f
);
1318 if (v
!= QEMU_VM_FILE_VERSION
) {
1325 #if defined (DO_TB_FLUSH)
1326 tb_flush(global_env
);
1328 len
= qemu_get_byte(f
);
1331 qemu_get_buffer(f
, idstr
, len
);
1333 instance_id
= qemu_get_be32(f
);
1334 version_id
= qemu_get_be32(f
);
1335 record_len
= qemu_get_be32(f
);
1337 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1338 idstr
, instance_id
, version_id
, record_len
);
1341 se
= find_se(idstr
, instance_id
);
1343 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1344 instance_id
, idstr
);
1346 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1348 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1349 instance_id
, idstr
);
1352 /* always seek to exact end of record */
1353 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1358 if (saved_vm_running
)
1363 /***********************************************************/
1364 /* cpu save/restore */
1366 #if defined(TARGET_I386)
1368 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1370 qemu_put_be32(f
, (uint32_t)dt
->base
);
1371 qemu_put_be32(f
, dt
->limit
);
1372 qemu_put_be32(f
, dt
->flags
);
1375 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1377 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1378 dt
->limit
= qemu_get_be32(f
);
1379 dt
->flags
= qemu_get_be32(f
);
1382 void cpu_save(QEMUFile
*f
, void *opaque
)
1384 CPUState
*env
= opaque
;
1385 uint16_t fptag
, fpus
, fpuc
;
1389 for(i
= 0; i
< 8; i
++)
1390 qemu_put_be32s(f
, &env
->regs
[i
]);
1391 qemu_put_be32s(f
, &env
->eip
);
1392 qemu_put_be32s(f
, &env
->eflags
);
1393 qemu_put_be32s(f
, &env
->eflags
);
1394 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1395 qemu_put_be32s(f
, &hflags
);
1399 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1401 for (i
=7; i
>=0; i
--) {
1403 if (env
->fptags
[i
]) {
1408 qemu_put_be16s(f
, &fpuc
);
1409 qemu_put_be16s(f
, &fpus
);
1410 qemu_put_be16s(f
, &fptag
);
1412 for(i
= 0; i
< 8; i
++) {
1415 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1416 qemu_put_be64(f
, mant
);
1417 qemu_put_be16(f
, exp
);
1420 for(i
= 0; i
< 6; i
++)
1421 cpu_put_seg(f
, &env
->segs
[i
]);
1422 cpu_put_seg(f
, &env
->ldt
);
1423 cpu_put_seg(f
, &env
->tr
);
1424 cpu_put_seg(f
, &env
->gdt
);
1425 cpu_put_seg(f
, &env
->idt
);
1427 qemu_put_be32s(f
, &env
->sysenter_cs
);
1428 qemu_put_be32s(f
, &env
->sysenter_esp
);
1429 qemu_put_be32s(f
, &env
->sysenter_eip
);
1431 qemu_put_be32s(f
, &env
->cr
[0]);
1432 qemu_put_be32s(f
, &env
->cr
[2]);
1433 qemu_put_be32s(f
, &env
->cr
[3]);
1434 qemu_put_be32s(f
, &env
->cr
[4]);
1436 for(i
= 0; i
< 8; i
++)
1437 qemu_put_be32s(f
, &env
->dr
[i
]);
1440 qemu_put_be32s(f
, &env
->a20_mask
);
1443 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1445 CPUState
*env
= opaque
;
1448 uint16_t fpus
, fpuc
, fptag
;
1450 if (version_id
!= 1)
1452 for(i
= 0; i
< 8; i
++)
1453 qemu_get_be32s(f
, &env
->regs
[i
]);
1454 qemu_get_be32s(f
, &env
->eip
);
1455 qemu_get_be32s(f
, &env
->eflags
);
1456 qemu_get_be32s(f
, &env
->eflags
);
1457 qemu_get_be32s(f
, &hflags
);
1459 qemu_get_be16s(f
, &fpuc
);
1460 qemu_get_be16s(f
, &fpus
);
1461 qemu_get_be16s(f
, &fptag
);
1463 for(i
= 0; i
< 8; i
++) {
1466 mant
= qemu_get_be64(f
);
1467 exp
= qemu_get_be16(f
);
1468 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1472 env
->fpstt
= (fpus
>> 11) & 7;
1473 env
->fpus
= fpus
& ~0x3800;
1474 for(i
= 0; i
< 8; i
++) {
1475 env
->fptags
[i
] = ((fptag
& 3) == 3);
1479 for(i
= 0; i
< 6; i
++)
1480 cpu_get_seg(f
, &env
->segs
[i
]);
1481 cpu_get_seg(f
, &env
->ldt
);
1482 cpu_get_seg(f
, &env
->tr
);
1483 cpu_get_seg(f
, &env
->gdt
);
1484 cpu_get_seg(f
, &env
->idt
);
1486 qemu_get_be32s(f
, &env
->sysenter_cs
);
1487 qemu_get_be32s(f
, &env
->sysenter_esp
);
1488 qemu_get_be32s(f
, &env
->sysenter_eip
);
1490 qemu_get_be32s(f
, &env
->cr
[0]);
1491 qemu_get_be32s(f
, &env
->cr
[2]);
1492 qemu_get_be32s(f
, &env
->cr
[3]);
1493 qemu_get_be32s(f
, &env
->cr
[4]);
1495 for(i
= 0; i
< 8; i
++)
1496 qemu_get_be32s(f
, &env
->dr
[i
]);
1499 qemu_get_be32s(f
, &env
->a20_mask
);
1501 /* XXX: compute hflags from scratch, except for CPL and IIF */
1502 env
->hflags
= hflags
;
1507 #elif defined(TARGET_PPC)
1508 void cpu_save(QEMUFile
*f
, void *opaque
)
1512 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1518 #warning No CPU save/restore functions
1522 /***********************************************************/
1523 /* ram save/restore */
1525 /* we just avoid storing empty pages */
1526 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1531 for(i
= 1; i
< len
; i
++) {
1535 qemu_put_byte(f
, 1);
1536 qemu_put_byte(f
, v
);
1539 qemu_put_byte(f
, 0);
1540 qemu_put_buffer(f
, buf
, len
);
1543 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1547 v
= qemu_get_byte(f
);
1550 if (qemu_get_buffer(f
, buf
, len
) != len
)
1554 v
= qemu_get_byte(f
);
1555 memset(buf
, v
, len
);
1563 static void ram_save(QEMUFile
*f
, void *opaque
)
1566 qemu_put_be32(f
, phys_ram_size
);
1567 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1568 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1572 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1576 if (version_id
!= 1)
1578 if (qemu_get_be32(f
) != phys_ram_size
)
1580 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1581 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1588 /***********************************************************/
1589 /* main execution loop */
1591 void gui_update(void *opaque
)
1593 display_state
.dpy_refresh(&display_state
);
1594 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1597 /* XXX: support several handlers */
1598 VMStopHandler
*vm_stop_cb
;
1599 VMStopHandler
*vm_stop_opaque
;
1601 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1604 vm_stop_opaque
= opaque
;
1608 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1621 void vm_stop(int reason
)
1624 cpu_disable_ticks();
1628 vm_stop_cb(vm_stop_opaque
, reason
);
1637 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1638 IOHandlerRecord
*ioh
, *ioh_next
;
1643 CPUState
*env
= global_env
;
1647 ret
= cpu_exec(env
);
1648 if (reset_requested
) {
1649 ret
= EXCP_INTERRUPT
;
1652 if (ret
== EXCP_DEBUG
) {
1653 vm_stop(EXCP_DEBUG
);
1655 /* if hlt instruction, we wait until the next IRQ */
1656 /* XXX: use timeout computed from timers */
1657 if (ret
== EXCP_HLT
)
1670 /* poll any events */
1671 /* XXX: separate device handlers from system ones */
1673 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1674 if (!ioh
->fd_can_read
) {
1677 pf
->events
= POLLIN
;
1681 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1683 if (max_size
> sizeof(buf
))
1684 max_size
= sizeof(buf
);
1686 pf
->events
= POLLIN
;
1693 ioh
->max_size
= max_size
;
1696 ret
= poll(ufds
, pf
- ufds
, timeout
);
1698 /* XXX: better handling of removal */
1699 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1700 ioh_next
= ioh
->next
;
1703 if (pf
->revents
& POLLIN
) {
1704 if (ioh
->max_size
== 0) {
1705 /* just a read event */
1706 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1708 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1710 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1711 } else if (errno
!= -EAGAIN
) {
1712 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1720 #if defined(CONFIG_SLIRP)
1721 /* XXX: merge with poll() */
1723 fd_set rfds
, wfds
, xfds
;
1731 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
1734 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
1736 slirp_select_poll(&rfds
, &wfds
, &xfds
);
1744 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1745 qemu_get_clock(vm_clock
));
1747 /* XXX: add explicit timer */
1750 /* run dma transfers, if any */
1754 /* real time timers */
1755 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1756 qemu_get_clock(rt_clock
));
1758 cpu_disable_ticks();
1764 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1765 "usage: %s [options] [disk_image]\n"
1767 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1769 "Standard options:\n"
1770 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1771 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1772 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1773 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1774 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1775 "-snapshot write to temporary files instead of disk image files\n"
1776 "-m megs set virtual RAM size to megs MB\n"
1777 "-nographic disable graphical output and redirect serial I/Os to console\n"
1779 "Network options:\n"
1780 "-nics n simulate 'n' network cards [default=1]\n"
1781 "-macaddr addr set the mac address of the first interface\n"
1782 "-n script set tap/tun network init script [default=%s]\n"
1783 "-tun-fd fd use this fd as already opened tap/tun interface\n"
1785 "-user-net use user mode network stack [default if no tap/tun script]\n"
1787 "-dummy-net use dummy network stack\n"
1789 "Linux boot specific:\n"
1790 "-kernel bzImage use 'bzImage' as kernel image\n"
1791 "-append cmdline use 'cmdline' as kernel command line\n"
1792 "-initrd file use 'file' as initial ram disk\n"
1794 "Debug/Expert options:\n"
1795 "-s wait gdb connection to port %d\n"
1796 "-p port change gdb connection port\n"
1797 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1798 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1799 "-L path set the directory for the BIOS and VGA BIOS\n"
1800 #ifdef USE_CODE_COPY
1801 "-no-code-copy disable code copy acceleration\n"
1805 "During emulation, use C-a h to get terminal commands:\n",
1806 #ifdef CONFIG_SOFTMMU
1811 DEFAULT_NETWORK_SCRIPT
,
1812 DEFAULT_GDBSTUB_PORT
,
1815 #ifndef CONFIG_SOFTMMU
1817 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1818 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1824 struct option long_options
[] = {
1825 { "initrd", 1, NULL
, 0, },
1826 { "hda", 1, NULL
, 0, },
1827 { "hdb", 1, NULL
, 0, },
1828 { "snapshot", 0, NULL
, 0, },
1829 { "hdachs", 1, NULL
, 0, },
1830 { "nographic", 0, NULL
, 0, },
1831 { "kernel", 1, NULL
, 0, },
1832 { "append", 1, NULL
, 0, },
1833 { "tun-fd", 1, NULL
, 0, },
1834 { "hdc", 1, NULL
, 0, },
1835 { "hdd", 1, NULL
, 0, },
1836 { "cdrom", 1, NULL
, 0, },
1837 { "boot", 1, NULL
, 0, },
1838 { "fda", 1, NULL
, 0, },
1839 { "fdb", 1, NULL
, 0, },
1840 { "no-code-copy", 0, NULL
, 0 },
1841 { "nics", 1, NULL
, 0 },
1842 { "macaddr", 1, NULL
, 0 },
1843 { "user-net", 0, NULL
, 0 },
1844 { "dummy-net", 0, NULL
, 0 },
1845 { NULL
, 0, NULL
, 0 },
1848 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1850 /* this stack is only used during signal handling */
1851 #define SIGNAL_STACK_SIZE 32768
1853 static uint8_t *signal_stack
;
1857 #define NET_IF_TUN 0
1858 #define NET_IF_USER 1
1859 #define NET_IF_DUMMY 2
1861 int main(int argc
, char **argv
)
1863 #ifdef CONFIG_GDBSTUB
1864 int use_gdbstub
, gdbstub_port
;
1866 int c
, i
, long_index
, has_cdrom
;
1867 int snapshot
, linux_boot
;
1869 const char *initrd_filename
;
1870 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
1871 const char *kernel_filename
, *kernel_cmdline
;
1872 DisplayState
*ds
= &display_state
;
1873 int cyls
, heads
, secs
;
1874 int start_emulation
= 1;
1876 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
1878 #if !defined(CONFIG_SOFTMMU)
1879 /* we never want that malloc() uses mmap() */
1880 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
1882 initrd_filename
= NULL
;
1883 for(i
= 0; i
< MAX_FD
; i
++)
1884 fd_filename
[i
] = NULL
;
1885 for(i
= 0; i
< MAX_DISKS
; i
++)
1886 hd_filename
[i
] = NULL
;
1887 ram_size
= 32 * 1024 * 1024;
1888 vga_ram_size
= VGA_RAM_SIZE
;
1889 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
1890 #ifdef CONFIG_GDBSTUB
1892 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
1896 kernel_filename
= NULL
;
1897 kernel_cmdline
= "";
1899 cyls
= heads
= secs
= 0;
1904 /* default mac address of the first network interface */
1914 c
= getopt_long_only(argc
, argv
, "hm:d:n:sp:L:S", long_options
, &long_index
);
1919 switch(long_index
) {
1921 initrd_filename
= optarg
;
1924 hd_filename
[0] = optarg
;
1927 hd_filename
[1] = optarg
;
1936 cyls
= strtol(p
, (char **)&p
, 0);
1940 heads
= strtol(p
, (char **)&p
, 0);
1944 secs
= strtol(p
, (char **)&p
, 0);
1955 kernel_filename
= optarg
;
1958 kernel_cmdline
= optarg
;
1964 if (nb_tun_fds
< MAX_NICS
) {
1965 fd
= strtol(optarg
, (char **)&p
, 0);
1967 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
1970 tun_fds
[nb_tun_fds
++] = fd
;
1975 hd_filename
[2] = optarg
;
1979 hd_filename
[3] = optarg
;
1982 hd_filename
[2] = optarg
;
1986 boot_device
= optarg
[0];
1987 if (boot_device
!= 'a' && boot_device
!= 'b' &&
1988 boot_device
!= 'c' && boot_device
!= 'd') {
1989 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
1994 fd_filename
[0] = optarg
;
1997 fd_filename
[1] = optarg
;
2000 code_copy_enabled
= 0;
2003 nb_nics
= atoi(optarg
);
2004 if (nb_nics
< 1 || nb_nics
> MAX_NICS
) {
2005 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
2014 for(i
= 0; i
< 6; i
++) {
2015 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
2022 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
2031 net_if_type
= NET_IF_USER
;
2034 net_if_type
= NET_IF_DUMMY
;
2042 ram_size
= atoi(optarg
) * 1024 * 1024;
2045 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
2046 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
2047 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
2056 mask
= cpu_str_to_log_mask(optarg
);
2058 printf("Log items (comma separated):\n");
2059 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
2060 printf("%-10s %s\n", item
->name
, item
->help
);
2068 pstrcpy(network_script
, sizeof(network_script
), optarg
);
2070 #ifdef CONFIG_GDBSTUB
2075 gdbstub_port
= atoi(optarg
);
2082 start_emulation
= 0;
2087 if (optind
< argc
) {
2088 hd_filename
[0] = argv
[optind
++];
2091 linux_boot
= (kernel_filename
!= NULL
);
2093 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
2094 fd_filename
[0] == '\0')
2097 /* boot to cd by default if no hard disk */
2098 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
2099 if (fd_filename
[0] != '\0')
2105 #if !defined(CONFIG_SOFTMMU)
2106 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
2108 static uint8_t stdout_buf
[4096];
2109 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
2112 setvbuf(stdout
, NULL
, _IOLBF
, 0);
2115 /* init host network redirectors */
2116 if (net_if_type
== -1) {
2117 net_if_type
= NET_IF_TUN
;
2118 #if defined(CONFIG_SLIRP)
2119 if (access(network_script
, R_OK
) < 0) {
2120 net_if_type
= NET_IF_USER
;
2125 for(i
= 0; i
< nb_nics
; i
++) {
2126 NetDriverState
*nd
= &nd_table
[i
];
2128 /* init virtual mac address */
2129 nd
->macaddr
[0] = macaddr
[0];
2130 nd
->macaddr
[1] = macaddr
[1];
2131 nd
->macaddr
[2] = macaddr
[2];
2132 nd
->macaddr
[3] = macaddr
[3];
2133 nd
->macaddr
[4] = macaddr
[4];
2134 nd
->macaddr
[5] = macaddr
[5] + i
;
2135 switch(net_if_type
) {
2136 #if defined(CONFIG_SLIRP)
2141 #if !defined(_WIN32)
2143 if (i
< nb_tun_fds
) {
2144 net_fd_init(nd
, tun_fds
[i
]);
2146 if (net_tun_init(nd
) < 0)
2158 /* init the memory */
2159 phys_ram_size
= ram_size
+ vga_ram_size
;
2161 #ifdef CONFIG_SOFTMMU
2162 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
2163 if (!phys_ram_base
) {
2164 fprintf(stderr
, "Could not allocate physical memory\n");
2168 /* as we must map the same page at several addresses, we must use
2173 tmpdir
= getenv("QEMU_TMPDIR");
2176 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
2177 if (mkstemp(phys_ram_file
) < 0) {
2178 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2182 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2183 if (phys_ram_fd
< 0) {
2184 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2188 ftruncate(phys_ram_fd
, phys_ram_size
);
2189 unlink(phys_ram_file
);
2190 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2192 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2194 if (phys_ram_base
== MAP_FAILED
) {
2195 fprintf(stderr
, "Could not map physical memory\n");
2201 /* we always create the cdrom drive, even if no disk is there */
2203 bs_table
[2] = bdrv_new("cdrom");
2204 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2207 /* open the virtual block devices */
2208 for(i
= 0; i
< MAX_DISKS
; i
++) {
2209 if (hd_filename
[i
]) {
2212 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2213 bs_table
[i
] = bdrv_new(buf
);
2215 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2216 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2220 if (i
== 0 && cyls
!= 0)
2221 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2225 /* we always create at least one floppy disk */
2226 fd_table
[0] = bdrv_new("fda");
2227 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2229 for(i
= 0; i
< MAX_FD
; i
++) {
2230 if (fd_filename
[i
]) {
2233 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2234 fd_table
[i
] = bdrv_new(buf
);
2235 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2237 if (fd_filename
[i
] != '\0') {
2238 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2239 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
2247 /* init CPU state */
2250 cpu_single_env
= env
;
2252 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2253 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2254 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2257 cpu_calibrate_ticks();
2261 dumb_display_init(ds
);
2264 sdl_display_init(ds
);
2266 dumb_display_init(ds
);
2270 /* setup cpu signal handlers for MMU / self modifying code handling */
2271 #if !defined(CONFIG_SOFTMMU)
2273 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2276 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2277 stk
.ss_sp
= signal_stack
;
2278 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2281 if (sigaltstack(&stk
, NULL
) < 0) {
2282 perror("sigaltstack");
2288 struct sigaction act
;
2290 sigfillset(&act
.sa_mask
);
2291 act
.sa_flags
= SA_SIGINFO
;
2292 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2293 act
.sa_flags
|= SA_ONSTACK
;
2295 act
.sa_sigaction
= host_segv_handler
;
2296 sigaction(SIGSEGV
, &act
, NULL
);
2297 sigaction(SIGBUS
, &act
, NULL
);
2298 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2299 sigaction(SIGFPE
, &act
, NULL
);
2306 struct sigaction act
;
2307 sigfillset(&act
.sa_mask
);
2309 act
.sa_handler
= SIG_IGN
;
2310 sigaction(SIGPIPE
, &act
, NULL
);
2315 #if defined(TARGET_I386)
2316 pc_init(ram_size
, vga_ram_size
, boot_device
,
2317 ds
, fd_filename
, snapshot
,
2318 kernel_filename
, kernel_cmdline
, initrd_filename
);
2319 #elif defined(TARGET_PPC)
2320 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2321 ds
, fd_filename
, snapshot
,
2322 kernel_filename
, kernel_cmdline
, initrd_filename
);
2325 /* launched after the device init so that it can display or not a
2329 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2330 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2332 #ifdef CONFIG_GDBSTUB
2334 if (gdbserver_start(gdbstub_port
) < 0) {
2335 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2339 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2343 if (start_emulation
)