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>
49 #include <sys/timeb.h>
51 #define getopt_long_only getopt_long
52 #define memalign(align, size) malloc(size)
56 /* SDL use the pthreads and they modify sigaction. We don't
58 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
59 extern void __libc_sigaction();
60 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
62 extern void __sigaction();
63 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
65 #endif /* CONFIG_SDL */
71 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
73 //#define DEBUG_UNUSED_IOPORT
75 #if !defined(CONFIG_SOFTMMU)
76 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
78 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
82 #define GUI_REFRESH_INTERVAL 30
84 /* XXX: use a two level table to limit memory usage */
85 #define MAX_IOPORTS 65536
87 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
88 char phys_ram_file
[1024];
90 CPUState
*cpu_single_env
;
91 void *ioport_opaque
[MAX_IOPORTS
];
92 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
93 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
94 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
96 static DisplayState display_state
;
98 int64_t ticks_per_sec
;
99 int boot_device
= 'c';
101 static char network_script
[1024];
102 int pit_min_timer_count
= 0;
104 NetDriverState nd_table
[MAX_NICS
];
105 SerialState
*serial_console
;
106 QEMUTimer
*gui_timer
;
109 /***********************************************************/
112 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
114 #ifdef DEBUG_UNUSED_IOPORT
115 fprintf(stderr
, "inb: port=0x%04x\n", address
);
120 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
122 #ifdef DEBUG_UNUSED_IOPORT
123 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
127 /* default is to make two byte accesses */
128 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
131 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
);
132 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1) << 8;
136 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
138 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
, data
& 0xff);
139 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1, (data
>> 8) & 0xff);
142 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
144 #ifdef DEBUG_UNUSED_IOPORT
145 fprintf(stderr
, "inl: port=0x%04x\n", address
);
150 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
152 #ifdef DEBUG_UNUSED_IOPORT
153 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
157 void init_ioports(void)
161 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
162 ioport_read_table
[0][i
] = default_ioport_readb
;
163 ioport_write_table
[0][i
] = default_ioport_writeb
;
164 ioport_read_table
[1][i
] = default_ioport_readw
;
165 ioport_write_table
[1][i
] = default_ioport_writew
;
166 ioport_read_table
[2][i
] = default_ioport_readl
;
167 ioport_write_table
[2][i
] = default_ioport_writel
;
171 /* size is the word size in byte */
172 int register_ioport_read(int start
, int length
, int size
,
173 IOPortReadFunc
*func
, void *opaque
)
179 } else if (size
== 2) {
181 } else if (size
== 4) {
184 hw_error("register_ioport_read: invalid size");
187 for(i
= start
; i
< start
+ length
; i
+= size
) {
188 ioport_read_table
[bsize
][i
] = func
;
189 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
190 hw_error("register_ioport_read: invalid opaque");
191 ioport_opaque
[i
] = opaque
;
196 /* size is the word size in byte */
197 int register_ioport_write(int start
, int length
, int size
,
198 IOPortWriteFunc
*func
, void *opaque
)
204 } else if (size
== 2) {
206 } else if (size
== 4) {
209 hw_error("register_ioport_write: invalid size");
212 for(i
= start
; i
< start
+ length
; i
+= size
) {
213 ioport_write_table
[bsize
][i
] = func
;
214 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
215 hw_error("register_ioport_read: invalid opaque");
216 ioport_opaque
[i
] = opaque
;
221 void pstrcpy(char *buf
, int buf_size
, const char *str
)
231 if (c
== 0 || q
>= buf
+ buf_size
- 1)
238 /* strcat and truncate. */
239 char *pstrcat(char *buf
, int buf_size
, const char *s
)
244 pstrcpy(buf
+ len
, buf_size
- len
, s
);
248 /* return the size or -1 if error */
249 int load_image(const char *filename
, uint8_t *addr
)
252 fd
= open(filename
, O_RDONLY
| O_BINARY
);
255 size
= lseek(fd
, 0, SEEK_END
);
256 lseek(fd
, 0, SEEK_SET
);
257 if (read(fd
, addr
, size
) != size
) {
265 void cpu_outb(CPUState
*env
, int addr
, int val
)
267 addr
&= (MAX_IOPORTS
- 1);
268 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
271 void cpu_outw(CPUState
*env
, int addr
, int val
)
273 addr
&= (MAX_IOPORTS
- 1);
274 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
277 void cpu_outl(CPUState
*env
, int addr
, int val
)
279 addr
&= (MAX_IOPORTS
- 1);
280 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
283 int cpu_inb(CPUState
*env
, int addr
)
285 addr
&= (MAX_IOPORTS
- 1);
286 return ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
289 int cpu_inw(CPUState
*env
, int addr
)
291 addr
&= (MAX_IOPORTS
- 1);
292 return ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
295 int cpu_inl(CPUState
*env
, int addr
)
297 addr
&= (MAX_IOPORTS
- 1);
298 return ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
301 /***********************************************************/
302 void hw_error(const char *fmt
, ...)
307 fprintf(stderr
, "qemu: hardware error: ");
308 vfprintf(stderr
, fmt
, ap
);
309 fprintf(stderr
, "\n");
311 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
313 cpu_dump_state(global_env
, stderr
, 0);
319 /***********************************************************/
322 #if defined(__powerpc__)
324 static inline uint32_t get_tbl(void)
327 asm volatile("mftb %0" : "=r" (tbl
));
331 static inline uint32_t get_tbu(void)
334 asm volatile("mftbu %0" : "=r" (tbl
));
338 int64_t cpu_get_real_ticks(void)
341 /* NOTE: we test if wrapping has occurred */
347 return ((int64_t)h
<< 32) | l
;
350 #elif defined(__i386__)
352 int64_t cpu_get_real_ticks(void)
355 asm volatile ("rdtsc" : "=A" (val
));
360 #error unsupported CPU
363 static int64_t cpu_ticks_offset
;
364 static int cpu_ticks_enabled
;
366 static inline int64_t cpu_get_ticks(void)
368 if (!cpu_ticks_enabled
) {
369 return cpu_ticks_offset
;
371 return cpu_get_real_ticks() + cpu_ticks_offset
;
375 /* enable cpu_get_ticks() */
376 void cpu_enable_ticks(void)
378 if (!cpu_ticks_enabled
) {
379 cpu_ticks_offset
-= cpu_get_real_ticks();
380 cpu_ticks_enabled
= 1;
384 /* disable cpu_get_ticks() : the clock is stopped. You must not call
385 cpu_get_ticks() after that. */
386 void cpu_disable_ticks(void)
388 if (cpu_ticks_enabled
) {
389 cpu_ticks_offset
= cpu_get_ticks();
390 cpu_ticks_enabled
= 0;
394 static int64_t get_clock(void)
399 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
402 gettimeofday(&tv
, NULL
);
403 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
407 void cpu_calibrate_ticks(void)
412 ticks
= cpu_get_real_ticks();
418 usec
= get_clock() - usec
;
419 ticks
= cpu_get_real_ticks() - ticks
;
420 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
423 /* compute with 96 bit intermediate result: (a*b)/c */
424 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
429 #ifdef WORDS_BIGENDIAN
439 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
440 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
443 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
447 #define QEMU_TIMER_REALTIME 0
448 #define QEMU_TIMER_VIRTUAL 1
452 /* XXX: add frequency */
460 struct QEMUTimer
*next
;
466 static QEMUTimer
*active_timers
[2];
468 static MMRESULT timerID
;
470 /* frequency of the times() clock tick */
471 static int timer_freq
;
474 QEMUClock
*qemu_new_clock(int type
)
477 clock
= qemu_mallocz(sizeof(QEMUClock
));
484 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
488 ts
= qemu_mallocz(sizeof(QEMUTimer
));
495 void qemu_free_timer(QEMUTimer
*ts
)
500 /* stop a timer, but do not dealloc it */
501 void qemu_del_timer(QEMUTimer
*ts
)
505 /* NOTE: this code must be signal safe because
506 qemu_timer_expired() can be called from a signal. */
507 pt
= &active_timers
[ts
->clock
->type
];
520 /* modify the current timer so that it will be fired when current_time
521 >= expire_time. The corresponding callback will be called. */
522 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
528 /* add the timer in the sorted list */
529 /* NOTE: this code must be signal safe because
530 qemu_timer_expired() can be called from a signal. */
531 pt
= &active_timers
[ts
->clock
->type
];
536 if (t
->expire_time
> expire_time
)
540 ts
->expire_time
= expire_time
;
545 int qemu_timer_pending(QEMUTimer
*ts
)
548 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
555 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
559 return (timer_head
->expire_time
<= current_time
);
562 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
568 if (ts
->expire_time
> current_time
)
570 /* remove timer from the list before calling the callback */
571 *ptimer_head
= ts
->next
;
574 /* run the callback (the timer list can be modified) */
579 int64_t qemu_get_clock(QEMUClock
*clock
)
581 switch(clock
->type
) {
582 case QEMU_TIMER_REALTIME
:
584 return GetTickCount();
586 /* XXX: portability among Linux hosts */
587 if (timer_freq
== 100) {
588 return times(NULL
) * 10;
590 return ((int64_t)times(NULL
) * 1000) / timer_freq
;
594 case QEMU_TIMER_VIRTUAL
:
595 return cpu_get_ticks();
600 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
602 uint64_t expire_time
;
604 if (qemu_timer_pending(ts
)) {
605 expire_time
= ts
->expire_time
;
609 qemu_put_be64(f
, expire_time
);
612 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
614 uint64_t expire_time
;
616 expire_time
= qemu_get_be64(f
);
617 if (expire_time
!= -1) {
618 qemu_mod_timer(ts
, expire_time
);
624 static void timer_save(QEMUFile
*f
, void *opaque
)
626 if (cpu_ticks_enabled
) {
627 hw_error("cannot save state if virtual timers are running");
629 qemu_put_be64s(f
, &cpu_ticks_offset
);
630 qemu_put_be64s(f
, &ticks_per_sec
);
633 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
637 if (cpu_ticks_enabled
) {
640 qemu_get_be64s(f
, &cpu_ticks_offset
);
641 qemu_get_be64s(f
, &ticks_per_sec
);
646 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
647 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
649 static void host_alarm_handler(int host_signum
)
652 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
653 qemu_get_clock(vm_clock
)) ||
654 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
655 qemu_get_clock(rt_clock
))) {
656 /* stop the cpu because a timer occured */
657 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
661 static void init_timers(void)
663 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
664 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
669 timerID
= timeSetEvent(10, // interval (ms)
671 host_alarm_handler
, // function
672 (DWORD
)&count
, // user parameter
673 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
675 perror("failed timer alarm");
679 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
682 struct sigaction act
;
683 struct itimerval itv
;
685 /* get times() syscall frequency */
686 timer_freq
= sysconf(_SC_CLK_TCK
);
689 sigfillset(&act
.sa_mask
);
691 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
692 act
.sa_flags
|= SA_ONSTACK
;
694 act
.sa_handler
= host_alarm_handler
;
695 sigaction(SIGALRM
, &act
, NULL
);
697 itv
.it_interval
.tv_sec
= 0;
698 itv
.it_interval
.tv_usec
= 1000;
699 itv
.it_value
.tv_sec
= 0;
700 itv
.it_value
.tv_usec
= 10 * 1000;
701 setitimer(ITIMER_REAL
, &itv
, NULL
);
702 /* we probe the tick duration of the kernel to inform the user if
703 the emulated kernel requested a too high timer frequency */
704 getitimer(ITIMER_REAL
, &itv
);
705 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
711 void quit_timers(void)
714 timeKillEvent(timerID
);
718 /***********************************************************/
723 int serial_open_device(void)
730 int serial_open_device(void)
732 char slave_name
[1024];
733 int master_fd
, slave_fd
;
735 if (serial_console
== NULL
&& nographic
) {
736 /* use console for serial port */
739 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
740 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
743 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
750 /***********************************************************/
751 /* Linux network device redirector */
755 static int net_init(void)
760 void net_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
766 static int tun_open(char *ifname
, int ifname_size
)
771 fd
= open("/dev/net/tun", O_RDWR
);
773 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
776 memset(&ifr
, 0, sizeof(ifr
));
777 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
778 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
779 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
781 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
785 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
786 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
787 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
791 static int net_init(void)
793 int pid
, status
, launch_script
, i
;
795 char *args
[MAX_NICS
+ 2];
799 for(i
= 0; i
< nb_nics
; i
++) {
802 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
809 /* try to launch network init script */
814 *parg
++ = network_script
;
815 for(i
= 0; i
< nb_nics
; i
++) {
818 *parg
++ = nd
->ifname
;
822 execv(network_script
, args
);
825 while (waitpid(pid
, &status
, 0) != pid
);
826 if (!WIFEXITED(status
) ||
827 WEXITSTATUS(status
) != 0) {
828 fprintf(stderr
, "%s: could not launch network script\n",
836 void net_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
839 printf("NE2000: sending packet size=%d\n", size
);
841 write(nd
->fd
, buf
, size
);
846 /***********************************************************/
851 static void term_exit(void)
855 static void term_init(void)
861 /* init terminal so that we can grab keys */
862 static struct termios oldtty
;
864 static void term_exit(void)
866 tcsetattr (0, TCSANOW
, &oldtty
);
869 static void term_init(void)
876 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
877 |INLCR
|IGNCR
|ICRNL
|IXON
);
878 tty
.c_oflag
|= OPOST
;
879 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
880 /* if graphical mode, we allow Ctrl-C handling */
882 tty
.c_lflag
&= ~ISIG
;
883 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
888 tcsetattr (0, TCSANOW
, &tty
);
892 fcntl(0, F_SETFL
, O_NONBLOCK
);
897 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
901 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
905 static void dumb_refresh(DisplayState
*ds
)
907 vga_update_display();
910 void dumb_display_init(DisplayState
*ds
)
915 ds
->dpy_update
= dumb_update
;
916 ds
->dpy_resize
= dumb_resize
;
917 ds
->dpy_refresh
= dumb_refresh
;
920 #if !defined(CONFIG_SOFTMMU)
921 /***********************************************************/
922 /* cpu signal handler */
923 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
926 if (cpu_signal_handler(host_signum
, info
, puc
))
933 /***********************************************************/
936 #define MAX_IO_HANDLERS 64
938 typedef struct IOHandlerRecord
{
940 IOCanRWHandler
*fd_can_read
;
941 IOReadHandler
*fd_read
;
946 struct IOHandlerRecord
*next
;
949 static IOHandlerRecord
*first_io_handler
;
951 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
952 IOReadHandler
*fd_read
, void *opaque
)
954 IOHandlerRecord
*ioh
;
956 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
960 ioh
->fd_can_read
= fd_can_read
;
961 ioh
->fd_read
= fd_read
;
962 ioh
->opaque
= opaque
;
963 ioh
->next
= first_io_handler
;
964 first_io_handler
= ioh
;
968 void qemu_del_fd_read_handler(int fd
)
970 IOHandlerRecord
**pioh
, *ioh
;
972 pioh
= &first_io_handler
;
985 /***********************************************************/
986 /* savevm/loadvm support */
988 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
990 fwrite(buf
, 1, size
, f
);
993 void qemu_put_byte(QEMUFile
*f
, int v
)
998 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1000 qemu_put_byte(f
, v
>> 8);
1001 qemu_put_byte(f
, v
);
1004 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1006 qemu_put_byte(f
, v
>> 24);
1007 qemu_put_byte(f
, v
>> 16);
1008 qemu_put_byte(f
, v
>> 8);
1009 qemu_put_byte(f
, v
);
1012 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1014 qemu_put_be32(f
, v
>> 32);
1015 qemu_put_be32(f
, v
);
1018 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1020 return fread(buf
, 1, size
, f
);
1023 int qemu_get_byte(QEMUFile
*f
)
1033 unsigned int qemu_get_be16(QEMUFile
*f
)
1036 v
= qemu_get_byte(f
) << 8;
1037 v
|= qemu_get_byte(f
);
1041 unsigned int qemu_get_be32(QEMUFile
*f
)
1044 v
= qemu_get_byte(f
) << 24;
1045 v
|= qemu_get_byte(f
) << 16;
1046 v
|= qemu_get_byte(f
) << 8;
1047 v
|= qemu_get_byte(f
);
1051 uint64_t qemu_get_be64(QEMUFile
*f
)
1054 v
= (uint64_t)qemu_get_be32(f
) << 32;
1055 v
|= qemu_get_be32(f
);
1059 int64_t qemu_ftell(QEMUFile
*f
)
1064 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1066 if (fseek(f
, pos
, whence
) < 0)
1071 typedef struct SaveStateEntry
{
1075 SaveStateHandler
*save_state
;
1076 LoadStateHandler
*load_state
;
1078 struct SaveStateEntry
*next
;
1081 static SaveStateEntry
*first_se
;
1083 int register_savevm(const char *idstr
,
1086 SaveStateHandler
*save_state
,
1087 LoadStateHandler
*load_state
,
1090 SaveStateEntry
*se
, **pse
;
1092 se
= qemu_malloc(sizeof(SaveStateEntry
));
1095 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1096 se
->instance_id
= instance_id
;
1097 se
->version_id
= version_id
;
1098 se
->save_state
= save_state
;
1099 se
->load_state
= load_state
;
1100 se
->opaque
= opaque
;
1103 /* add at the end of list */
1105 while (*pse
!= NULL
)
1106 pse
= &(*pse
)->next
;
1111 #define QEMU_VM_FILE_MAGIC 0x5145564d
1112 #define QEMU_VM_FILE_VERSION 0x00000001
1114 int qemu_savevm(const char *filename
)
1118 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1120 saved_vm_running
= vm_running
;
1123 f
= fopen(filename
, "wb");
1129 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1130 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1132 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1134 len
= strlen(se
->idstr
);
1135 qemu_put_byte(f
, len
);
1136 qemu_put_buffer(f
, se
->idstr
, len
);
1138 qemu_put_be32(f
, se
->instance_id
);
1139 qemu_put_be32(f
, se
->version_id
);
1141 /* record size: filled later */
1143 qemu_put_be32(f
, 0);
1145 se
->save_state(f
, se
->opaque
);
1147 /* fill record size */
1149 len
= ftell(f
) - len_pos
- 4;
1150 fseek(f
, len_pos
, SEEK_SET
);
1151 qemu_put_be32(f
, len
);
1152 fseek(f
, cur_pos
, SEEK_SET
);
1158 if (saved_vm_running
)
1163 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1167 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1168 if (!strcmp(se
->idstr
, idstr
) &&
1169 instance_id
== se
->instance_id
)
1175 int qemu_loadvm(const char *filename
)
1179 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1180 int saved_vm_running
;
1184 saved_vm_running
= vm_running
;
1187 f
= fopen(filename
, "rb");
1193 v
= qemu_get_be32(f
);
1194 if (v
!= QEMU_VM_FILE_MAGIC
)
1196 v
= qemu_get_be32(f
);
1197 if (v
!= QEMU_VM_FILE_VERSION
) {
1204 len
= qemu_get_byte(f
);
1207 qemu_get_buffer(f
, idstr
, len
);
1209 instance_id
= qemu_get_be32(f
);
1210 version_id
= qemu_get_be32(f
);
1211 record_len
= qemu_get_be32(f
);
1213 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1214 idstr
, instance_id
, version_id
, record_len
);
1217 se
= find_se(idstr
, instance_id
);
1219 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1220 instance_id
, idstr
);
1222 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1224 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1225 instance_id
, idstr
);
1228 /* always seek to exact end of record */
1229 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1234 if (saved_vm_running
)
1239 /***********************************************************/
1240 /* cpu save/restore */
1242 #if defined(TARGET_I386)
1244 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1246 qemu_put_be32(f
, (uint32_t)dt
->base
);
1247 qemu_put_be32(f
, dt
->limit
);
1248 qemu_put_be32(f
, dt
->flags
);
1251 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1253 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1254 dt
->limit
= qemu_get_be32(f
);
1255 dt
->flags
= qemu_get_be32(f
);
1258 void cpu_save(QEMUFile
*f
, void *opaque
)
1260 CPUState
*env
= opaque
;
1261 uint16_t fptag
, fpus
, fpuc
;
1265 for(i
= 0; i
< 8; i
++)
1266 qemu_put_be32s(f
, &env
->regs
[i
]);
1267 qemu_put_be32s(f
, &env
->eip
);
1268 qemu_put_be32s(f
, &env
->eflags
);
1269 qemu_put_be32s(f
, &env
->eflags
);
1270 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1271 qemu_put_be32s(f
, &hflags
);
1275 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1277 for (i
=7; i
>=0; i
--) {
1279 if (env
->fptags
[i
]) {
1284 qemu_put_be16s(f
, &fpuc
);
1285 qemu_put_be16s(f
, &fpus
);
1286 qemu_put_be16s(f
, &fptag
);
1288 for(i
= 0; i
< 8; i
++) {
1291 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1292 qemu_put_be64(f
, mant
);
1293 qemu_put_be16(f
, exp
);
1296 for(i
= 0; i
< 6; i
++)
1297 cpu_put_seg(f
, &env
->segs
[i
]);
1298 cpu_put_seg(f
, &env
->ldt
);
1299 cpu_put_seg(f
, &env
->tr
);
1300 cpu_put_seg(f
, &env
->gdt
);
1301 cpu_put_seg(f
, &env
->idt
);
1303 qemu_put_be32s(f
, &env
->sysenter_cs
);
1304 qemu_put_be32s(f
, &env
->sysenter_esp
);
1305 qemu_put_be32s(f
, &env
->sysenter_eip
);
1307 qemu_put_be32s(f
, &env
->cr
[0]);
1308 qemu_put_be32s(f
, &env
->cr
[2]);
1309 qemu_put_be32s(f
, &env
->cr
[3]);
1310 qemu_put_be32s(f
, &env
->cr
[4]);
1312 for(i
= 0; i
< 8; i
++)
1313 qemu_put_be32s(f
, &env
->dr
[i
]);
1316 qemu_put_be32s(f
, &env
->a20_mask
);
1319 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1321 CPUState
*env
= opaque
;
1324 uint16_t fpus
, fpuc
, fptag
;
1326 if (version_id
!= 1)
1328 for(i
= 0; i
< 8; i
++)
1329 qemu_get_be32s(f
, &env
->regs
[i
]);
1330 qemu_get_be32s(f
, &env
->eip
);
1331 qemu_get_be32s(f
, &env
->eflags
);
1332 qemu_get_be32s(f
, &env
->eflags
);
1333 qemu_get_be32s(f
, &hflags
);
1335 qemu_get_be16s(f
, &fpuc
);
1336 qemu_get_be16s(f
, &fpus
);
1337 qemu_get_be16s(f
, &fptag
);
1339 for(i
= 0; i
< 8; i
++) {
1342 mant
= qemu_get_be64(f
);
1343 exp
= qemu_get_be16(f
);
1344 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1348 env
->fpstt
= (fpus
>> 11) & 7;
1349 env
->fpus
= fpus
& ~0x3800;
1350 for(i
= 0; i
< 8; i
++) {
1351 env
->fptags
[i
] = ((fptag
& 3) == 3);
1355 for(i
= 0; i
< 6; i
++)
1356 cpu_get_seg(f
, &env
->segs
[i
]);
1357 cpu_get_seg(f
, &env
->ldt
);
1358 cpu_get_seg(f
, &env
->tr
);
1359 cpu_get_seg(f
, &env
->gdt
);
1360 cpu_get_seg(f
, &env
->idt
);
1362 qemu_get_be32s(f
, &env
->sysenter_cs
);
1363 qemu_get_be32s(f
, &env
->sysenter_esp
);
1364 qemu_get_be32s(f
, &env
->sysenter_eip
);
1366 qemu_get_be32s(f
, &env
->cr
[0]);
1367 qemu_get_be32s(f
, &env
->cr
[2]);
1368 qemu_get_be32s(f
, &env
->cr
[3]);
1369 qemu_get_be32s(f
, &env
->cr
[4]);
1371 for(i
= 0; i
< 8; i
++)
1372 qemu_get_be32s(f
, &env
->dr
[i
]);
1375 qemu_get_be32s(f
, &env
->a20_mask
);
1377 /* XXX: compute hflags from scratch, except for CPL and IIF */
1378 env
->hflags
= hflags
;
1385 #warning No CPU save/restore functions
1389 /***********************************************************/
1390 /* ram save/restore */
1392 /* we just avoid storing empty pages */
1393 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1398 for(i
= 1; i
< len
; i
++) {
1402 qemu_put_byte(f
, 1);
1403 qemu_put_byte(f
, v
);
1406 qemu_put_byte(f
, 0);
1407 qemu_put_buffer(f
, buf
, len
);
1410 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1414 v
= qemu_get_byte(f
);
1417 if (qemu_get_buffer(f
, buf
, len
) != len
)
1421 v
= qemu_get_byte(f
);
1422 memset(buf
, v
, len
);
1430 static void ram_save(QEMUFile
*f
, void *opaque
)
1433 qemu_put_be32(f
, phys_ram_size
);
1434 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1435 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1439 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1443 if (version_id
!= 1)
1445 if (qemu_get_be32(f
) != phys_ram_size
)
1447 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1448 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1455 /***********************************************************/
1456 /* main execution loop */
1458 void gui_update(void *opaque
)
1460 display_state
.dpy_refresh(&display_state
);
1461 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1464 /* XXX: support several handlers */
1465 VMStopHandler
*vm_stop_cb
;
1466 VMStopHandler
*vm_stop_opaque
;
1468 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1471 vm_stop_opaque
= opaque
;
1475 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1488 void vm_stop(int reason
)
1491 cpu_disable_ticks();
1495 vm_stop_cb(vm_stop_opaque
, reason
);
1504 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1505 IOHandlerRecord
*ioh
, *ioh_next
;
1510 CPUState
*env
= global_env
;
1514 ret
= cpu_exec(env
);
1515 if (reset_requested
) {
1516 ret
= EXCP_INTERRUPT
;
1519 if (ret
== EXCP_DEBUG
) {
1520 vm_stop(EXCP_DEBUG
);
1522 /* if hlt instruction, we wait until the next IRQ */
1523 /* XXX: use timeout computed from timers */
1524 if (ret
== EXCP_HLT
)
1537 /* poll any events */
1538 /* XXX: separate device handlers from system ones */
1540 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1541 if (!ioh
->fd_can_read
) {
1544 pf
->events
= POLLIN
;
1548 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1550 if (max_size
> sizeof(buf
))
1551 max_size
= sizeof(buf
);
1553 pf
->events
= POLLIN
;
1560 ioh
->max_size
= max_size
;
1563 ret
= poll(ufds
, pf
- ufds
, timeout
);
1565 /* XXX: better handling of removal */
1566 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1567 ioh_next
= ioh
->next
;
1570 if (pf
->revents
& POLLIN
) {
1571 if (ioh
->max_size
== 0) {
1572 /* just a read event */
1573 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1575 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1577 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1578 } else if (errno
!= -EAGAIN
) {
1579 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1589 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1590 qemu_get_clock(vm_clock
));
1592 /* XXX: add explicit timer */
1595 /* run dma transfers, if any */
1599 /* real time timers */
1600 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1601 qemu_get_clock(rt_clock
));
1603 cpu_disable_ticks();
1609 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1610 "usage: %s [options] [disk_image]\n"
1612 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1614 "Standard options:\n"
1615 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1616 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1617 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1618 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1619 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1620 "-snapshot write to temporary files instead of disk image files\n"
1621 "-m megs set virtual RAM size to megs MB\n"
1622 "-nographic disable graphical output and redirect serial I/Os to console\n"
1624 "Network options:\n"
1625 "-n script set network init script [default=%s]\n"
1626 "-nics n simulate 'n' network interfaces [default=1]\n"
1627 "-macaddr addr set the mac address of the first interface\n"
1628 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
1630 "Linux boot specific:\n"
1631 "-kernel bzImage use 'bzImage' as kernel image\n"
1632 "-append cmdline use 'cmdline' as kernel command line\n"
1633 "-initrd file use 'file' as initial ram disk\n"
1635 "Debug/Expert options:\n"
1636 "-s wait gdb connection to port %d\n"
1637 "-p port change gdb connection port\n"
1638 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1639 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1640 "-L path set the directory for the BIOS and VGA BIOS\n"
1641 #ifdef USE_CODE_COPY
1642 "-no-code-copy disable code copy acceleration\n"
1646 "During emulation, use C-a h to get terminal commands:\n",
1647 #ifdef CONFIG_SOFTMMU
1652 DEFAULT_NETWORK_SCRIPT
,
1653 DEFAULT_GDBSTUB_PORT
,
1656 #ifndef CONFIG_SOFTMMU
1658 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1659 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1665 struct option long_options
[] = {
1666 { "initrd", 1, NULL
, 0, },
1667 { "hda", 1, NULL
, 0, },
1668 { "hdb", 1, NULL
, 0, },
1669 { "snapshot", 0, NULL
, 0, },
1670 { "hdachs", 1, NULL
, 0, },
1671 { "nographic", 0, NULL
, 0, },
1672 { "kernel", 1, NULL
, 0, },
1673 { "append", 1, NULL
, 0, },
1674 { "tun-fd", 1, NULL
, 0, },
1675 { "hdc", 1, NULL
, 0, },
1676 { "hdd", 1, NULL
, 0, },
1677 { "cdrom", 1, NULL
, 0, },
1678 { "boot", 1, NULL
, 0, },
1679 { "fda", 1, NULL
, 0, },
1680 { "fdb", 1, NULL
, 0, },
1681 { "no-code-copy", 0, NULL
, 0 },
1682 { "nics", 1, NULL
, 0 },
1683 { "macaddr", 1, NULL
, 0 },
1684 { NULL
, 0, NULL
, 0 },
1687 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1689 /* this stack is only used during signal handling */
1690 #define SIGNAL_STACK_SIZE 32768
1692 static uint8_t *signal_stack
;
1696 int main(int argc
, char **argv
)
1698 #ifdef CONFIG_GDBSTUB
1699 int use_gdbstub
, gdbstub_port
;
1701 int c
, i
, long_index
, has_cdrom
;
1702 int snapshot
, linux_boot
;
1704 const char *initrd_filename
;
1705 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
1706 const char *kernel_filename
, *kernel_cmdline
;
1707 DisplayState
*ds
= &display_state
;
1708 int cyls
, heads
, secs
;
1711 #if !defined(CONFIG_SOFTMMU)
1712 /* we never want that malloc() uses mmap() */
1713 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
1715 initrd_filename
= NULL
;
1716 for(i
= 0; i
< MAX_FD
; i
++)
1717 fd_filename
[i
] = NULL
;
1718 for(i
= 0; i
< MAX_DISKS
; i
++)
1719 hd_filename
[i
] = NULL
;
1720 ram_size
= 32 * 1024 * 1024;
1721 vga_ram_size
= VGA_RAM_SIZE
;
1722 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
1723 #ifdef CONFIG_GDBSTUB
1725 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
1729 kernel_filename
= NULL
;
1730 kernel_cmdline
= "";
1732 cyls
= heads
= secs
= 0;
1735 /* default mac address of the first network interface */
1743 for(i
= 0; i
< MAX_NICS
; i
++)
1744 nd_table
[i
].fd
= -1;
1747 c
= getopt_long_only(argc
, argv
, "hm:d:n:sp:L:", long_options
, &long_index
);
1752 switch(long_index
) {
1754 initrd_filename
= optarg
;
1757 hd_filename
[0] = optarg
;
1760 hd_filename
[1] = optarg
;
1769 cyls
= strtol(p
, (char **)&p
, 0);
1773 heads
= strtol(p
, (char **)&p
, 0);
1777 secs
= strtol(p
, (char **)&p
, 0);
1788 kernel_filename
= optarg
;
1791 kernel_cmdline
= optarg
;
1800 fd
= strtol(p
, (char **)&p
, 0);
1801 nd_table
[nb_nics
].fd
= fd
;
1802 snprintf(nd_table
[nb_nics
].ifname
,
1803 sizeof(nd_table
[nb_nics
].ifname
),
1808 } else if (*p
!= '\0') {
1809 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_nics
);
1818 hd_filename
[2] = optarg
;
1822 hd_filename
[3] = optarg
;
1825 hd_filename
[2] = optarg
;
1829 boot_device
= optarg
[0];
1830 if (boot_device
!= 'a' && boot_device
!= 'b' &&
1831 boot_device
!= 'c' && boot_device
!= 'd') {
1832 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
1837 fd_filename
[0] = optarg
;
1840 fd_filename
[1] = optarg
;
1843 code_copy_enabled
= 0;
1846 nb_nics
= atoi(optarg
);
1847 if (nb_nics
< 1 || nb_nics
> MAX_NICS
) {
1848 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
1857 for(i
= 0; i
< 6; i
++) {
1858 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
1865 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
1879 ram_size
= atoi(optarg
) * 1024 * 1024;
1882 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
1883 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
1884 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
1893 mask
= cpu_str_to_log_mask(optarg
);
1895 printf("Log items (comma separated):\n");
1896 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1897 printf("%-10s %s\n", item
->name
, item
->help
);
1905 pstrcpy(network_script
, sizeof(network_script
), optarg
);
1907 #ifdef CONFIG_GDBSTUB
1912 gdbstub_port
= atoi(optarg
);
1921 if (optind
< argc
) {
1922 hd_filename
[0] = argv
[optind
++];
1925 linux_boot
= (kernel_filename
!= NULL
);
1927 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
1928 fd_filename
[0] == '\0')
1931 /* boot to cd by default if no hard disk */
1932 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
1933 if (fd_filename
[0] != '\0')
1939 #if !defined(CONFIG_SOFTMMU)
1940 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1942 static uint8_t stdout_buf
[4096];
1943 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
1946 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1949 /* init host network redirectors */
1950 for(i
= 0; i
< MAX_NICS
; i
++) {
1951 NetDriverState
*nd
= &nd_table
[i
];
1952 /* init virtual mac address */
1953 nd
->macaddr
[0] = macaddr
[0];
1954 nd
->macaddr
[1] = macaddr
[1];
1955 nd
->macaddr
[2] = macaddr
[2];
1956 nd
->macaddr
[3] = macaddr
[3];
1957 nd
->macaddr
[4] = macaddr
[4];
1958 nd
->macaddr
[5] = macaddr
[5] + i
;
1962 /* init the memory */
1963 phys_ram_size
= ram_size
+ vga_ram_size
;
1965 #ifdef CONFIG_SOFTMMU
1966 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
1967 if (!phys_ram_base
) {
1968 fprintf(stderr
, "Could not allocate physical memory\n");
1972 /* as we must map the same page at several addresses, we must use
1977 tmpdir
= getenv("QEMU_TMPDIR");
1980 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
1981 if (mkstemp(phys_ram_file
) < 0) {
1982 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
1986 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
1987 if (phys_ram_fd
< 0) {
1988 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
1992 ftruncate(phys_ram_fd
, phys_ram_size
);
1993 unlink(phys_ram_file
);
1994 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
1996 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
1998 if (phys_ram_base
== MAP_FAILED
) {
1999 fprintf(stderr
, "Could not map physical memory\n");
2005 /* we always create the cdrom drive, even if no disk is there */
2007 bs_table
[2] = bdrv_new("cdrom");
2008 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2011 /* open the virtual block devices */
2012 for(i
= 0; i
< MAX_DISKS
; i
++) {
2013 if (hd_filename
[i
]) {
2016 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2017 bs_table
[i
] = bdrv_new(buf
);
2019 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2020 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2024 if (i
== 0 && cyls
!= 0)
2025 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2029 /* we always create at least one floppy disk */
2030 fd_table
[0] = bdrv_new("fda");
2031 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2033 for(i
= 0; i
< MAX_FD
; i
++) {
2034 if (fd_filename
[i
]) {
2037 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2038 fd_table
[i
] = bdrv_new(buf
);
2039 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2041 if (fd_filename
[i
] != '\0') {
2042 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2043 fprintf(stderr
, "qemu: could not open floppy disk image '%s\n",
2051 /* init CPU state */
2054 cpu_single_env
= env
;
2056 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2057 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2058 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2061 cpu_calibrate_ticks();
2065 dumb_display_init(ds
);
2068 sdl_display_init(ds
);
2070 dumb_display_init(ds
);
2074 /* setup cpu signal handlers for MMU / self modifying code handling */
2075 #if !defined(CONFIG_SOFTMMU)
2077 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2080 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2081 stk
.ss_sp
= signal_stack
;
2082 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2085 if (sigaltstack(&stk
, NULL
) < 0) {
2086 perror("sigaltstack");
2092 struct sigaction act
;
2094 sigfillset(&act
.sa_mask
);
2095 act
.sa_flags
= SA_SIGINFO
;
2096 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2097 act
.sa_flags
|= SA_ONSTACK
;
2099 act
.sa_sigaction
= host_segv_handler
;
2100 sigaction(SIGSEGV
, &act
, NULL
);
2101 sigaction(SIGBUS
, &act
, NULL
);
2102 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2103 sigaction(SIGFPE
, &act
, NULL
);
2110 struct sigaction act
;
2111 sigfillset(&act
.sa_mask
);
2113 act
.sa_handler
= SIG_IGN
;
2114 sigaction(SIGPIPE
, &act
, NULL
);
2119 #if defined(TARGET_I386)
2120 pc_init(ram_size
, vga_ram_size
, boot_device
,
2121 ds
, fd_filename
, snapshot
,
2122 kernel_filename
, kernel_cmdline
, initrd_filename
);
2123 #elif defined(TARGET_PPC)
2127 /* launched after the device init so that it can display or not a
2131 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2132 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2134 #ifdef CONFIG_GDBSTUB
2136 if (gdbserver_start(gdbstub_port
) < 0) {
2137 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2141 printf("Waiting gdb connection on port %d\n", gdbstub_port
);