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 */
73 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
75 //#define DEBUG_UNUSED_IOPORT
77 #if !defined(CONFIG_SOFTMMU)
78 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
80 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
84 #define GUI_REFRESH_INTERVAL 30
86 /* XXX: use a two level table to limit memory usage */
87 #define MAX_IOPORTS 65536
89 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
90 char phys_ram_file
[1024];
92 CPUState
*cpu_single_env
;
93 void *ioport_opaque
[MAX_IOPORTS
];
94 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
95 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
96 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
98 static DisplayState display_state
;
100 int64_t ticks_per_sec
;
101 int boot_device
= 'c';
103 static char network_script
[1024];
104 int pit_min_timer_count
= 0;
106 NetDriverState nd_table
[MAX_NICS
];
107 SerialState
*serial_console
;
108 QEMUTimer
*gui_timer
;
111 /***********************************************************/
114 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
116 #ifdef DEBUG_UNUSED_IOPORT
117 fprintf(stderr
, "inb: port=0x%04x\n", address
);
122 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
124 #ifdef DEBUG_UNUSED_IOPORT
125 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
129 /* default is to make two byte accesses */
130 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
133 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
);
134 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1) << 8;
138 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
140 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
, data
& 0xff);
141 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1, (data
>> 8) & 0xff);
144 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
146 #ifdef DEBUG_UNUSED_IOPORT
147 fprintf(stderr
, "inl: port=0x%04x\n", address
);
152 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
154 #ifdef DEBUG_UNUSED_IOPORT
155 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
159 void init_ioports(void)
163 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
164 ioport_read_table
[0][i
] = default_ioport_readb
;
165 ioport_write_table
[0][i
] = default_ioport_writeb
;
166 ioport_read_table
[1][i
] = default_ioport_readw
;
167 ioport_write_table
[1][i
] = default_ioport_writew
;
168 ioport_read_table
[2][i
] = default_ioport_readl
;
169 ioport_write_table
[2][i
] = default_ioport_writel
;
173 /* size is the word size in byte */
174 int register_ioport_read(int start
, int length
, int size
,
175 IOPortReadFunc
*func
, void *opaque
)
181 } else if (size
== 2) {
183 } else if (size
== 4) {
186 hw_error("register_ioport_read: invalid size");
189 for(i
= start
; i
< start
+ length
; i
+= size
) {
190 ioport_read_table
[bsize
][i
] = func
;
191 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
192 hw_error("register_ioport_read: invalid opaque");
193 ioport_opaque
[i
] = opaque
;
198 /* size is the word size in byte */
199 int register_ioport_write(int start
, int length
, int size
,
200 IOPortWriteFunc
*func
, void *opaque
)
206 } else if (size
== 2) {
208 } else if (size
== 4) {
211 hw_error("register_ioport_write: invalid size");
214 for(i
= start
; i
< start
+ length
; i
+= size
) {
215 ioport_write_table
[bsize
][i
] = func
;
216 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
217 hw_error("register_ioport_read: invalid opaque");
218 ioport_opaque
[i
] = opaque
;
223 void pstrcpy(char *buf
, int buf_size
, const char *str
)
233 if (c
== 0 || q
>= buf
+ buf_size
- 1)
240 /* strcat and truncate. */
241 char *pstrcat(char *buf
, int buf_size
, const char *s
)
246 pstrcpy(buf
+ len
, buf_size
- len
, s
);
250 /* return the size or -1 if error */
251 int load_image(const char *filename
, uint8_t *addr
)
254 fd
= open(filename
, O_RDONLY
| O_BINARY
);
257 size
= lseek(fd
, 0, SEEK_END
);
258 lseek(fd
, 0, SEEK_SET
);
259 if (read(fd
, addr
, size
) != size
) {
267 void cpu_outb(CPUState
*env
, int addr
, int val
)
269 addr
&= (MAX_IOPORTS
- 1);
270 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
273 void cpu_outw(CPUState
*env
, int addr
, int val
)
275 addr
&= (MAX_IOPORTS
- 1);
276 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
279 void cpu_outl(CPUState
*env
, int addr
, int val
)
281 addr
&= (MAX_IOPORTS
- 1);
282 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
285 int cpu_inb(CPUState
*env
, int addr
)
287 addr
&= (MAX_IOPORTS
- 1);
288 return ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
291 int cpu_inw(CPUState
*env
, int addr
)
293 addr
&= (MAX_IOPORTS
- 1);
294 return ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
297 int cpu_inl(CPUState
*env
, int addr
)
299 addr
&= (MAX_IOPORTS
- 1);
300 return ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
303 /***********************************************************/
304 void hw_error(const char *fmt
, ...)
309 fprintf(stderr
, "qemu: hardware error: ");
310 vfprintf(stderr
, fmt
, ap
);
311 fprintf(stderr
, "\n");
313 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
315 cpu_dump_state(global_env
, stderr
, 0);
321 /***********************************************************/
324 #if defined(__powerpc__)
326 static inline uint32_t get_tbl(void)
329 asm volatile("mftb %0" : "=r" (tbl
));
333 static inline uint32_t get_tbu(void)
336 asm volatile("mftbu %0" : "=r" (tbl
));
340 int64_t cpu_get_real_ticks(void)
343 /* NOTE: we test if wrapping has occurred */
349 return ((int64_t)h
<< 32) | l
;
352 #elif defined(__i386__)
354 int64_t cpu_get_real_ticks(void)
357 asm volatile ("rdtsc" : "=A" (val
));
362 #error unsupported CPU
365 static int64_t cpu_ticks_offset
;
366 static int cpu_ticks_enabled
;
368 static inline int64_t cpu_get_ticks(void)
370 if (!cpu_ticks_enabled
) {
371 return cpu_ticks_offset
;
373 return cpu_get_real_ticks() + cpu_ticks_offset
;
377 /* enable cpu_get_ticks() */
378 void cpu_enable_ticks(void)
380 if (!cpu_ticks_enabled
) {
381 cpu_ticks_offset
-= cpu_get_real_ticks();
382 cpu_ticks_enabled
= 1;
386 /* disable cpu_get_ticks() : the clock is stopped. You must not call
387 cpu_get_ticks() after that. */
388 void cpu_disable_ticks(void)
390 if (cpu_ticks_enabled
) {
391 cpu_ticks_offset
= cpu_get_ticks();
392 cpu_ticks_enabled
= 0;
396 static int64_t get_clock(void)
401 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
404 gettimeofday(&tv
, NULL
);
405 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
409 void cpu_calibrate_ticks(void)
414 ticks
= cpu_get_real_ticks();
420 usec
= get_clock() - usec
;
421 ticks
= cpu_get_real_ticks() - ticks
;
422 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
425 /* compute with 96 bit intermediate result: (a*b)/c */
426 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
431 #ifdef WORDS_BIGENDIAN
441 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
442 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
445 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
449 #define QEMU_TIMER_REALTIME 0
450 #define QEMU_TIMER_VIRTUAL 1
454 /* XXX: add frequency */
462 struct QEMUTimer
*next
;
468 static QEMUTimer
*active_timers
[2];
470 static MMRESULT timerID
;
472 /* frequency of the times() clock tick */
473 static int timer_freq
;
476 QEMUClock
*qemu_new_clock(int type
)
479 clock
= qemu_mallocz(sizeof(QEMUClock
));
486 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
490 ts
= qemu_mallocz(sizeof(QEMUTimer
));
497 void qemu_free_timer(QEMUTimer
*ts
)
502 /* stop a timer, but do not dealloc it */
503 void qemu_del_timer(QEMUTimer
*ts
)
507 /* NOTE: this code must be signal safe because
508 qemu_timer_expired() can be called from a signal. */
509 pt
= &active_timers
[ts
->clock
->type
];
522 /* modify the current timer so that it will be fired when current_time
523 >= expire_time. The corresponding callback will be called. */
524 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
530 /* add the timer in the sorted list */
531 /* NOTE: this code must be signal safe because
532 qemu_timer_expired() can be called from a signal. */
533 pt
= &active_timers
[ts
->clock
->type
];
538 if (t
->expire_time
> expire_time
)
542 ts
->expire_time
= expire_time
;
547 int qemu_timer_pending(QEMUTimer
*ts
)
550 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
557 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
561 return (timer_head
->expire_time
<= current_time
);
564 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
570 if (ts
->expire_time
> current_time
)
572 /* remove timer from the list before calling the callback */
573 *ptimer_head
= ts
->next
;
576 /* run the callback (the timer list can be modified) */
581 int64_t qemu_get_clock(QEMUClock
*clock
)
583 switch(clock
->type
) {
584 case QEMU_TIMER_REALTIME
:
586 return GetTickCount();
588 /* XXX: portability among Linux hosts */
589 if (timer_freq
== 100) {
590 return times(NULL
) * 10;
592 return ((int64_t)times(NULL
) * 1000) / timer_freq
;
596 case QEMU_TIMER_VIRTUAL
:
597 return cpu_get_ticks();
602 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
604 uint64_t expire_time
;
606 if (qemu_timer_pending(ts
)) {
607 expire_time
= ts
->expire_time
;
611 qemu_put_be64(f
, expire_time
);
614 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
616 uint64_t expire_time
;
618 expire_time
= qemu_get_be64(f
);
619 if (expire_time
!= -1) {
620 qemu_mod_timer(ts
, expire_time
);
626 static void timer_save(QEMUFile
*f
, void *opaque
)
628 if (cpu_ticks_enabled
) {
629 hw_error("cannot save state if virtual timers are running");
631 qemu_put_be64s(f
, &cpu_ticks_offset
);
632 qemu_put_be64s(f
, &ticks_per_sec
);
635 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
639 if (cpu_ticks_enabled
) {
642 qemu_get_be64s(f
, &cpu_ticks_offset
);
643 qemu_get_be64s(f
, &ticks_per_sec
);
648 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
649 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
651 static void host_alarm_handler(int host_signum
)
654 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
655 qemu_get_clock(vm_clock
)) ||
656 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
657 qemu_get_clock(rt_clock
))) {
658 /* stop the cpu because a timer occured */
659 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
663 static void init_timers(void)
665 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
666 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
671 timerID
= timeSetEvent(10, // interval (ms)
673 host_alarm_handler
, // function
674 (DWORD
)&count
, // user parameter
675 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
677 perror("failed timer alarm");
681 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
684 struct sigaction act
;
685 struct itimerval itv
;
687 /* get times() syscall frequency */
688 timer_freq
= sysconf(_SC_CLK_TCK
);
691 sigfillset(&act
.sa_mask
);
693 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
694 act
.sa_flags
|= SA_ONSTACK
;
696 act
.sa_handler
= host_alarm_handler
;
697 sigaction(SIGALRM
, &act
, NULL
);
699 itv
.it_interval
.tv_sec
= 0;
700 itv
.it_interval
.tv_usec
= 1000;
701 itv
.it_value
.tv_sec
= 0;
702 itv
.it_value
.tv_usec
= 10 * 1000;
703 setitimer(ITIMER_REAL
, &itv
, NULL
);
704 /* we probe the tick duration of the kernel to inform the user if
705 the emulated kernel requested a too high timer frequency */
706 getitimer(ITIMER_REAL
, &itv
);
707 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
713 void quit_timers(void)
716 timeKillEvent(timerID
);
720 /***********************************************************/
725 int serial_open_device(void)
732 int serial_open_device(void)
734 char slave_name
[1024];
735 int master_fd
, slave_fd
;
737 if (serial_console
== NULL
&& nographic
) {
738 /* use console for serial port */
741 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
742 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
745 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
752 /***********************************************************/
753 /* Linux network device redirector */
757 static int net_init(void)
762 void net_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
768 static int tun_open(char *ifname
, int ifname_size
)
773 fd
= open("/dev/net/tun", O_RDWR
);
775 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
778 memset(&ifr
, 0, sizeof(ifr
));
779 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
780 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
781 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
783 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
787 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
788 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
789 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
793 static int net_init(void)
795 int pid
, status
, launch_script
, i
;
797 char *args
[MAX_NICS
+ 2];
801 for(i
= 0; i
< nb_nics
; i
++) {
804 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
811 /* try to launch network init script */
816 *parg
++ = network_script
;
817 for(i
= 0; i
< nb_nics
; i
++) {
820 *parg
++ = nd
->ifname
;
824 execv(network_script
, args
);
827 while (waitpid(pid
, &status
, 0) != pid
);
828 if (!WIFEXITED(status
) ||
829 WEXITSTATUS(status
) != 0) {
830 fprintf(stderr
, "%s: could not launch network script\n",
838 void net_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
841 printf("NE2000: sending packet size=%d\n", size
);
843 write(nd
->fd
, buf
, size
);
848 /***********************************************************/
853 static void term_exit(void)
857 static void term_init(void)
863 /* init terminal so that we can grab keys */
864 static struct termios oldtty
;
866 static void term_exit(void)
868 tcsetattr (0, TCSANOW
, &oldtty
);
871 static void term_init(void)
878 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
879 |INLCR
|IGNCR
|ICRNL
|IXON
);
880 tty
.c_oflag
|= OPOST
;
881 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
882 /* if graphical mode, we allow Ctrl-C handling */
884 tty
.c_lflag
&= ~ISIG
;
885 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
890 tcsetattr (0, TCSANOW
, &tty
);
894 fcntl(0, F_SETFL
, O_NONBLOCK
);
899 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
903 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
907 static void dumb_refresh(DisplayState
*ds
)
909 vga_update_display();
912 void dumb_display_init(DisplayState
*ds
)
917 ds
->dpy_update
= dumb_update
;
918 ds
->dpy_resize
= dumb_resize
;
919 ds
->dpy_refresh
= dumb_refresh
;
922 #if !defined(CONFIG_SOFTMMU)
923 /***********************************************************/
924 /* cpu signal handler */
925 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
928 if (cpu_signal_handler(host_signum
, info
, puc
))
935 /***********************************************************/
938 #define MAX_IO_HANDLERS 64
940 typedef struct IOHandlerRecord
{
942 IOCanRWHandler
*fd_can_read
;
943 IOReadHandler
*fd_read
;
948 struct IOHandlerRecord
*next
;
951 static IOHandlerRecord
*first_io_handler
;
953 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
954 IOReadHandler
*fd_read
, void *opaque
)
956 IOHandlerRecord
*ioh
;
958 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
962 ioh
->fd_can_read
= fd_can_read
;
963 ioh
->fd_read
= fd_read
;
964 ioh
->opaque
= opaque
;
965 ioh
->next
= first_io_handler
;
966 first_io_handler
= ioh
;
970 void qemu_del_fd_read_handler(int fd
)
972 IOHandlerRecord
**pioh
, *ioh
;
974 pioh
= &first_io_handler
;
987 /***********************************************************/
988 /* savevm/loadvm support */
990 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
992 fwrite(buf
, 1, size
, f
);
995 void qemu_put_byte(QEMUFile
*f
, int v
)
1000 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1002 qemu_put_byte(f
, v
>> 8);
1003 qemu_put_byte(f
, v
);
1006 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1008 qemu_put_byte(f
, v
>> 24);
1009 qemu_put_byte(f
, v
>> 16);
1010 qemu_put_byte(f
, v
>> 8);
1011 qemu_put_byte(f
, v
);
1014 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1016 qemu_put_be32(f
, v
>> 32);
1017 qemu_put_be32(f
, v
);
1020 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1022 return fread(buf
, 1, size
, f
);
1025 int qemu_get_byte(QEMUFile
*f
)
1035 unsigned int qemu_get_be16(QEMUFile
*f
)
1038 v
= qemu_get_byte(f
) << 8;
1039 v
|= qemu_get_byte(f
);
1043 unsigned int qemu_get_be32(QEMUFile
*f
)
1046 v
= qemu_get_byte(f
) << 24;
1047 v
|= qemu_get_byte(f
) << 16;
1048 v
|= qemu_get_byte(f
) << 8;
1049 v
|= qemu_get_byte(f
);
1053 uint64_t qemu_get_be64(QEMUFile
*f
)
1056 v
= (uint64_t)qemu_get_be32(f
) << 32;
1057 v
|= qemu_get_be32(f
);
1061 int64_t qemu_ftell(QEMUFile
*f
)
1066 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1068 if (fseek(f
, pos
, whence
) < 0)
1073 typedef struct SaveStateEntry
{
1077 SaveStateHandler
*save_state
;
1078 LoadStateHandler
*load_state
;
1080 struct SaveStateEntry
*next
;
1083 static SaveStateEntry
*first_se
;
1085 int register_savevm(const char *idstr
,
1088 SaveStateHandler
*save_state
,
1089 LoadStateHandler
*load_state
,
1092 SaveStateEntry
*se
, **pse
;
1094 se
= qemu_malloc(sizeof(SaveStateEntry
));
1097 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1098 se
->instance_id
= instance_id
;
1099 se
->version_id
= version_id
;
1100 se
->save_state
= save_state
;
1101 se
->load_state
= load_state
;
1102 se
->opaque
= opaque
;
1105 /* add at the end of list */
1107 while (*pse
!= NULL
)
1108 pse
= &(*pse
)->next
;
1113 #define QEMU_VM_FILE_MAGIC 0x5145564d
1114 #define QEMU_VM_FILE_VERSION 0x00000001
1116 int qemu_savevm(const char *filename
)
1120 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1122 saved_vm_running
= vm_running
;
1125 f
= fopen(filename
, "wb");
1131 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1132 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1134 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1136 len
= strlen(se
->idstr
);
1137 qemu_put_byte(f
, len
);
1138 qemu_put_buffer(f
, se
->idstr
, len
);
1140 qemu_put_be32(f
, se
->instance_id
);
1141 qemu_put_be32(f
, se
->version_id
);
1143 /* record size: filled later */
1145 qemu_put_be32(f
, 0);
1147 se
->save_state(f
, se
->opaque
);
1149 /* fill record size */
1151 len
= ftell(f
) - len_pos
- 4;
1152 fseek(f
, len_pos
, SEEK_SET
);
1153 qemu_put_be32(f
, len
);
1154 fseek(f
, cur_pos
, SEEK_SET
);
1160 if (saved_vm_running
)
1165 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1169 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1170 if (!strcmp(se
->idstr
, idstr
) &&
1171 instance_id
== se
->instance_id
)
1177 int qemu_loadvm(const char *filename
)
1181 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1182 int saved_vm_running
;
1186 saved_vm_running
= vm_running
;
1189 f
= fopen(filename
, "rb");
1195 v
= qemu_get_be32(f
);
1196 if (v
!= QEMU_VM_FILE_MAGIC
)
1198 v
= qemu_get_be32(f
);
1199 if (v
!= QEMU_VM_FILE_VERSION
) {
1206 #if defined (DO_TB_FLUSH)
1209 len
= qemu_get_byte(f
);
1212 qemu_get_buffer(f
, idstr
, len
);
1214 instance_id
= qemu_get_be32(f
);
1215 version_id
= qemu_get_be32(f
);
1216 record_len
= qemu_get_be32(f
);
1218 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1219 idstr
, instance_id
, version_id
, record_len
);
1222 se
= find_se(idstr
, instance_id
);
1224 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1225 instance_id
, idstr
);
1227 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1229 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1230 instance_id
, idstr
);
1233 /* always seek to exact end of record */
1234 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1239 if (saved_vm_running
)
1244 /***********************************************************/
1245 /* cpu save/restore */
1247 #if defined(TARGET_I386)
1249 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1251 qemu_put_be32(f
, (uint32_t)dt
->base
);
1252 qemu_put_be32(f
, dt
->limit
);
1253 qemu_put_be32(f
, dt
->flags
);
1256 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1258 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1259 dt
->limit
= qemu_get_be32(f
);
1260 dt
->flags
= qemu_get_be32(f
);
1263 void cpu_save(QEMUFile
*f
, void *opaque
)
1265 CPUState
*env
= opaque
;
1266 uint16_t fptag
, fpus
, fpuc
;
1270 for(i
= 0; i
< 8; i
++)
1271 qemu_put_be32s(f
, &env
->regs
[i
]);
1272 qemu_put_be32s(f
, &env
->eip
);
1273 qemu_put_be32s(f
, &env
->eflags
);
1274 qemu_put_be32s(f
, &env
->eflags
);
1275 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1276 qemu_put_be32s(f
, &hflags
);
1280 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1282 for (i
=7; i
>=0; i
--) {
1284 if (env
->fptags
[i
]) {
1289 qemu_put_be16s(f
, &fpuc
);
1290 qemu_put_be16s(f
, &fpus
);
1291 qemu_put_be16s(f
, &fptag
);
1293 for(i
= 0; i
< 8; i
++) {
1296 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1297 qemu_put_be64(f
, mant
);
1298 qemu_put_be16(f
, exp
);
1301 for(i
= 0; i
< 6; i
++)
1302 cpu_put_seg(f
, &env
->segs
[i
]);
1303 cpu_put_seg(f
, &env
->ldt
);
1304 cpu_put_seg(f
, &env
->tr
);
1305 cpu_put_seg(f
, &env
->gdt
);
1306 cpu_put_seg(f
, &env
->idt
);
1308 qemu_put_be32s(f
, &env
->sysenter_cs
);
1309 qemu_put_be32s(f
, &env
->sysenter_esp
);
1310 qemu_put_be32s(f
, &env
->sysenter_eip
);
1312 qemu_put_be32s(f
, &env
->cr
[0]);
1313 qemu_put_be32s(f
, &env
->cr
[2]);
1314 qemu_put_be32s(f
, &env
->cr
[3]);
1315 qemu_put_be32s(f
, &env
->cr
[4]);
1317 for(i
= 0; i
< 8; i
++)
1318 qemu_put_be32s(f
, &env
->dr
[i
]);
1321 qemu_put_be32s(f
, &env
->a20_mask
);
1324 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1326 CPUState
*env
= opaque
;
1329 uint16_t fpus
, fpuc
, fptag
;
1331 if (version_id
!= 1)
1333 for(i
= 0; i
< 8; i
++)
1334 qemu_get_be32s(f
, &env
->regs
[i
]);
1335 qemu_get_be32s(f
, &env
->eip
);
1336 qemu_get_be32s(f
, &env
->eflags
);
1337 qemu_get_be32s(f
, &env
->eflags
);
1338 qemu_get_be32s(f
, &hflags
);
1340 qemu_get_be16s(f
, &fpuc
);
1341 qemu_get_be16s(f
, &fpus
);
1342 qemu_get_be16s(f
, &fptag
);
1344 for(i
= 0; i
< 8; i
++) {
1347 mant
= qemu_get_be64(f
);
1348 exp
= qemu_get_be16(f
);
1349 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1353 env
->fpstt
= (fpus
>> 11) & 7;
1354 env
->fpus
= fpus
& ~0x3800;
1355 for(i
= 0; i
< 8; i
++) {
1356 env
->fptags
[i
] = ((fptag
& 3) == 3);
1360 for(i
= 0; i
< 6; i
++)
1361 cpu_get_seg(f
, &env
->segs
[i
]);
1362 cpu_get_seg(f
, &env
->ldt
);
1363 cpu_get_seg(f
, &env
->tr
);
1364 cpu_get_seg(f
, &env
->gdt
);
1365 cpu_get_seg(f
, &env
->idt
);
1367 qemu_get_be32s(f
, &env
->sysenter_cs
);
1368 qemu_get_be32s(f
, &env
->sysenter_esp
);
1369 qemu_get_be32s(f
, &env
->sysenter_eip
);
1371 qemu_get_be32s(f
, &env
->cr
[0]);
1372 qemu_get_be32s(f
, &env
->cr
[2]);
1373 qemu_get_be32s(f
, &env
->cr
[3]);
1374 qemu_get_be32s(f
, &env
->cr
[4]);
1376 for(i
= 0; i
< 8; i
++)
1377 qemu_get_be32s(f
, &env
->dr
[i
]);
1380 qemu_get_be32s(f
, &env
->a20_mask
);
1382 /* XXX: compute hflags from scratch, except for CPL and IIF */
1383 env
->hflags
= hflags
;
1388 #elif defined(TARGET_PPC)
1389 void cpu_save(QEMUFile
*f
, void *opaque
)
1393 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1399 #warning No CPU save/restore functions
1403 /***********************************************************/
1404 /* ram save/restore */
1406 /* we just avoid storing empty pages */
1407 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1412 for(i
= 1; i
< len
; i
++) {
1416 qemu_put_byte(f
, 1);
1417 qemu_put_byte(f
, v
);
1420 qemu_put_byte(f
, 0);
1421 qemu_put_buffer(f
, buf
, len
);
1424 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1428 v
= qemu_get_byte(f
);
1431 if (qemu_get_buffer(f
, buf
, len
) != len
)
1435 v
= qemu_get_byte(f
);
1436 memset(buf
, v
, len
);
1444 static void ram_save(QEMUFile
*f
, void *opaque
)
1447 qemu_put_be32(f
, phys_ram_size
);
1448 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1449 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1453 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1457 if (version_id
!= 1)
1459 if (qemu_get_be32(f
) != phys_ram_size
)
1461 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1462 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1469 /***********************************************************/
1470 /* main execution loop */
1472 void gui_update(void *opaque
)
1474 display_state
.dpy_refresh(&display_state
);
1475 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1478 /* XXX: support several handlers */
1479 VMStopHandler
*vm_stop_cb
;
1480 VMStopHandler
*vm_stop_opaque
;
1482 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1485 vm_stop_opaque
= opaque
;
1489 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1502 void vm_stop(int reason
)
1505 cpu_disable_ticks();
1509 vm_stop_cb(vm_stop_opaque
, reason
);
1518 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1519 IOHandlerRecord
*ioh
, *ioh_next
;
1524 CPUState
*env
= global_env
;
1528 ret
= cpu_exec(env
);
1529 if (reset_requested
) {
1530 ret
= EXCP_INTERRUPT
;
1533 if (ret
== EXCP_DEBUG
) {
1534 vm_stop(EXCP_DEBUG
);
1536 /* if hlt instruction, we wait until the next IRQ */
1537 /* XXX: use timeout computed from timers */
1538 if (ret
== EXCP_HLT
)
1551 /* poll any events */
1552 /* XXX: separate device handlers from system ones */
1554 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1555 if (!ioh
->fd_can_read
) {
1558 pf
->events
= POLLIN
;
1562 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1564 if (max_size
> sizeof(buf
))
1565 max_size
= sizeof(buf
);
1567 pf
->events
= POLLIN
;
1574 ioh
->max_size
= max_size
;
1577 ret
= poll(ufds
, pf
- ufds
, timeout
);
1579 /* XXX: better handling of removal */
1580 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1581 ioh_next
= ioh
->next
;
1584 if (pf
->revents
& POLLIN
) {
1585 if (ioh
->max_size
== 0) {
1586 /* just a read event */
1587 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1589 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1591 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1592 } else if (errno
!= -EAGAIN
) {
1593 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1603 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1604 qemu_get_clock(vm_clock
));
1606 /* XXX: add explicit timer */
1609 /* run dma transfers, if any */
1613 /* real time timers */
1614 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1615 qemu_get_clock(rt_clock
));
1617 cpu_disable_ticks();
1623 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1624 "usage: %s [options] [disk_image]\n"
1626 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1628 "Standard options:\n"
1629 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1630 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1631 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1632 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1633 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1634 "-snapshot write to temporary files instead of disk image files\n"
1635 "-m megs set virtual RAM size to megs MB\n"
1636 "-nographic disable graphical output and redirect serial I/Os to console\n"
1638 "Network options:\n"
1639 "-n script set network init script [default=%s]\n"
1640 "-nics n simulate 'n' network interfaces [default=1]\n"
1641 "-macaddr addr set the mac address of the first interface\n"
1642 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
1644 "Linux boot specific:\n"
1645 "-kernel bzImage use 'bzImage' as kernel image\n"
1646 "-append cmdline use 'cmdline' as kernel command line\n"
1647 "-initrd file use 'file' as initial ram disk\n"
1649 "Debug/Expert options:\n"
1650 "-s wait gdb connection to port %d\n"
1651 "-p port change gdb connection port\n"
1652 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1653 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1654 "-L path set the directory for the BIOS and VGA BIOS\n"
1655 #ifdef USE_CODE_COPY
1656 "-no-code-copy disable code copy acceleration\n"
1660 "During emulation, use C-a h to get terminal commands:\n",
1661 #ifdef CONFIG_SOFTMMU
1666 DEFAULT_NETWORK_SCRIPT
,
1667 DEFAULT_GDBSTUB_PORT
,
1670 #ifndef CONFIG_SOFTMMU
1672 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1673 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1679 struct option long_options
[] = {
1680 { "initrd", 1, NULL
, 0, },
1681 { "hda", 1, NULL
, 0, },
1682 { "hdb", 1, NULL
, 0, },
1683 { "snapshot", 0, NULL
, 0, },
1684 { "hdachs", 1, NULL
, 0, },
1685 { "nographic", 0, NULL
, 0, },
1686 { "kernel", 1, NULL
, 0, },
1687 { "append", 1, NULL
, 0, },
1688 { "tun-fd", 1, NULL
, 0, },
1689 { "hdc", 1, NULL
, 0, },
1690 { "hdd", 1, NULL
, 0, },
1691 { "cdrom", 1, NULL
, 0, },
1692 { "boot", 1, NULL
, 0, },
1693 { "fda", 1, NULL
, 0, },
1694 { "fdb", 1, NULL
, 0, },
1695 { "no-code-copy", 0, NULL
, 0 },
1696 { "nics", 1, NULL
, 0 },
1697 { "macaddr", 1, NULL
, 0 },
1698 { NULL
, 0, NULL
, 0 },
1701 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1703 /* this stack is only used during signal handling */
1704 #define SIGNAL_STACK_SIZE 32768
1706 static uint8_t *signal_stack
;
1710 int main(int argc
, char **argv
)
1712 #ifdef CONFIG_GDBSTUB
1713 int use_gdbstub
, gdbstub_port
;
1715 int c
, i
, long_index
, has_cdrom
;
1716 int snapshot
, linux_boot
;
1718 const char *initrd_filename
;
1719 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
1720 const char *kernel_filename
, *kernel_cmdline
;
1721 DisplayState
*ds
= &display_state
;
1722 int cyls
, heads
, secs
;
1723 int start_emulation
= 1;
1726 #if !defined(CONFIG_SOFTMMU)
1727 /* we never want that malloc() uses mmap() */
1728 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
1730 initrd_filename
= NULL
;
1731 for(i
= 0; i
< MAX_FD
; i
++)
1732 fd_filename
[i
] = NULL
;
1733 for(i
= 0; i
< MAX_DISKS
; i
++)
1734 hd_filename
[i
] = NULL
;
1735 ram_size
= 32 * 1024 * 1024;
1736 vga_ram_size
= VGA_RAM_SIZE
;
1737 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
1738 #ifdef CONFIG_GDBSTUB
1740 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
1744 kernel_filename
= NULL
;
1745 kernel_cmdline
= "";
1747 cyls
= heads
= secs
= 0;
1750 /* default mac address of the first network interface */
1758 for(i
= 0; i
< MAX_NICS
; i
++)
1759 nd_table
[i
].fd
= -1;
1762 c
= getopt_long_only(argc
, argv
, "hm:d:n:sp:L:S", long_options
, &long_index
);
1767 switch(long_index
) {
1769 initrd_filename
= optarg
;
1772 hd_filename
[0] = optarg
;
1775 hd_filename
[1] = optarg
;
1784 cyls
= strtol(p
, (char **)&p
, 0);
1788 heads
= strtol(p
, (char **)&p
, 0);
1792 secs
= strtol(p
, (char **)&p
, 0);
1803 kernel_filename
= optarg
;
1806 kernel_cmdline
= optarg
;
1815 fd
= strtol(p
, (char **)&p
, 0);
1816 nd_table
[nb_nics
].fd
= fd
;
1817 snprintf(nd_table
[nb_nics
].ifname
,
1818 sizeof(nd_table
[nb_nics
].ifname
),
1823 } else if (*p
!= '\0') {
1824 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_nics
);
1833 hd_filename
[2] = optarg
;
1837 hd_filename
[3] = optarg
;
1840 hd_filename
[2] = optarg
;
1844 boot_device
= optarg
[0];
1845 if (boot_device
!= 'a' && boot_device
!= 'b' &&
1846 boot_device
!= 'c' && boot_device
!= 'd') {
1847 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
1852 fd_filename
[0] = optarg
;
1855 fd_filename
[1] = optarg
;
1858 code_copy_enabled
= 0;
1861 nb_nics
= atoi(optarg
);
1862 if (nb_nics
< 1 || nb_nics
> MAX_NICS
) {
1863 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
1872 for(i
= 0; i
< 6; i
++) {
1873 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
1880 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
1894 ram_size
= atoi(optarg
) * 1024 * 1024;
1897 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
1898 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
1899 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
1908 mask
= cpu_str_to_log_mask(optarg
);
1910 printf("Log items (comma separated):\n");
1911 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1912 printf("%-10s %s\n", item
->name
, item
->help
);
1920 pstrcpy(network_script
, sizeof(network_script
), optarg
);
1922 #ifdef CONFIG_GDBSTUB
1927 gdbstub_port
= atoi(optarg
);
1934 start_emulation
= 0;
1939 if (optind
< argc
) {
1940 hd_filename
[0] = argv
[optind
++];
1943 linux_boot
= (kernel_filename
!= NULL
);
1945 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
1946 fd_filename
[0] == '\0')
1949 /* boot to cd by default if no hard disk */
1950 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
1951 if (fd_filename
[0] != '\0')
1957 #if !defined(CONFIG_SOFTMMU)
1958 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1960 static uint8_t stdout_buf
[4096];
1961 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
1964 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1967 /* init host network redirectors */
1968 for(i
= 0; i
< MAX_NICS
; i
++) {
1969 NetDriverState
*nd
= &nd_table
[i
];
1970 /* init virtual mac address */
1971 nd
->macaddr
[0] = macaddr
[0];
1972 nd
->macaddr
[1] = macaddr
[1];
1973 nd
->macaddr
[2] = macaddr
[2];
1974 nd
->macaddr
[3] = macaddr
[3];
1975 nd
->macaddr
[4] = macaddr
[4];
1976 nd
->macaddr
[5] = macaddr
[5] + i
;
1980 /* init the memory */
1981 phys_ram_size
= ram_size
+ vga_ram_size
;
1983 #ifdef CONFIG_SOFTMMU
1984 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
1985 if (!phys_ram_base
) {
1986 fprintf(stderr
, "Could not allocate physical memory\n");
1990 /* as we must map the same page at several addresses, we must use
1995 tmpdir
= getenv("QEMU_TMPDIR");
1998 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
1999 if (mkstemp(phys_ram_file
) < 0) {
2000 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
2004 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
2005 if (phys_ram_fd
< 0) {
2006 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
2010 ftruncate(phys_ram_fd
, phys_ram_size
);
2011 unlink(phys_ram_file
);
2012 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
2014 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
2016 if (phys_ram_base
== MAP_FAILED
) {
2017 fprintf(stderr
, "Could not map physical memory\n");
2023 /* we always create the cdrom drive, even if no disk is there */
2025 bs_table
[2] = bdrv_new("cdrom");
2026 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
2029 /* open the virtual block devices */
2030 for(i
= 0; i
< MAX_DISKS
; i
++) {
2031 if (hd_filename
[i
]) {
2034 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
2035 bs_table
[i
] = bdrv_new(buf
);
2037 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
2038 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
2042 if (i
== 0 && cyls
!= 0)
2043 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
2047 /* we always create at least one floppy disk */
2048 fd_table
[0] = bdrv_new("fda");
2049 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
2051 for(i
= 0; i
< MAX_FD
; i
++) {
2052 if (fd_filename
[i
]) {
2055 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
2056 fd_table
[i
] = bdrv_new(buf
);
2057 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
2059 if (fd_filename
[i
] != '\0') {
2060 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
2061 fprintf(stderr
, "qemu: could not open floppy disk image '%s\n",
2069 /* init CPU state */
2072 cpu_single_env
= env
;
2074 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2075 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2076 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2079 cpu_calibrate_ticks();
2083 dumb_display_init(ds
);
2086 sdl_display_init(ds
);
2088 dumb_display_init(ds
);
2092 /* setup cpu signal handlers for MMU / self modifying code handling */
2093 #if !defined(CONFIG_SOFTMMU)
2095 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2098 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
2099 stk
.ss_sp
= signal_stack
;
2100 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2103 if (sigaltstack(&stk
, NULL
) < 0) {
2104 perror("sigaltstack");
2110 struct sigaction act
;
2112 sigfillset(&act
.sa_mask
);
2113 act
.sa_flags
= SA_SIGINFO
;
2114 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2115 act
.sa_flags
|= SA_ONSTACK
;
2117 act
.sa_sigaction
= host_segv_handler
;
2118 sigaction(SIGSEGV
, &act
, NULL
);
2119 sigaction(SIGBUS
, &act
, NULL
);
2120 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2121 sigaction(SIGFPE
, &act
, NULL
);
2128 struct sigaction act
;
2129 sigfillset(&act
.sa_mask
);
2131 act
.sa_handler
= SIG_IGN
;
2132 sigaction(SIGPIPE
, &act
, NULL
);
2137 #if defined(TARGET_I386)
2138 pc_init(ram_size
, vga_ram_size
, boot_device
,
2139 ds
, fd_filename
, snapshot
,
2140 kernel_filename
, kernel_cmdline
, initrd_filename
);
2141 #elif defined(TARGET_PPC)
2142 ppc_init(ram_size
, vga_ram_size
, boot_device
,
2143 ds
, fd_filename
, snapshot
,
2144 kernel_filename
, kernel_cmdline
, initrd_filename
);
2147 /* launched after the device init so that it can display or not a
2151 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2152 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2154 #ifdef CONFIG_GDBSTUB
2156 if (gdbserver_start(gdbstub_port
) < 0) {
2157 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2161 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
2165 if (start_emulation
)