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)
60 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
62 //#define DEBUG_UNUSED_IOPORT
64 #if !defined(CONFIG_SOFTMMU)
65 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
67 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
71 #define GUI_REFRESH_INTERVAL 30
73 /* XXX: use a two level table to limit memory usage */
74 #define MAX_IOPORTS 65536
76 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
77 char phys_ram_file
[1024];
79 CPUState
*cpu_single_env
;
80 void *ioport_opaque
[MAX_IOPORTS
];
81 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
82 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
83 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
85 static DisplayState display_state
;
87 int64_t ticks_per_sec
;
88 int boot_device
= 'c';
90 static char network_script
[1024];
91 int pit_min_timer_count
= 0;
93 NetDriverState nd_table
[MAX_NICS
];
94 SerialState
*serial_console
;
98 /***********************************************************/
101 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
103 #ifdef DEBUG_UNUSED_IOPORT
104 fprintf(stderr
, "inb: port=0x%04x\n", address
);
109 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
111 #ifdef DEBUG_UNUSED_IOPORT
112 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
116 /* default is to make two byte accesses */
117 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
120 data
= ioport_read_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
);
121 data
|= ioport_read_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1) << 8;
125 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
127 ioport_write_table
[0][address
& (MAX_IOPORTS
- 1)](opaque
, address
, data
& 0xff);
128 ioport_write_table
[0][(address
+ 1) & (MAX_IOPORTS
- 1)](opaque
, address
+ 1, (data
>> 8) & 0xff);
131 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
133 #ifdef DEBUG_UNUSED_IOPORT
134 fprintf(stderr
, "inl: port=0x%04x\n", address
);
139 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
141 #ifdef DEBUG_UNUSED_IOPORT
142 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
146 void init_ioports(void)
150 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
151 ioport_read_table
[0][i
] = default_ioport_readb
;
152 ioport_write_table
[0][i
] = default_ioport_writeb
;
153 ioport_read_table
[1][i
] = default_ioport_readw
;
154 ioport_write_table
[1][i
] = default_ioport_writew
;
155 ioport_read_table
[2][i
] = default_ioport_readl
;
156 ioport_write_table
[2][i
] = default_ioport_writel
;
160 /* size is the word size in byte */
161 int register_ioport_read(int start
, int length
, int size
,
162 IOPortReadFunc
*func
, void *opaque
)
168 } else if (size
== 2) {
170 } else if (size
== 4) {
173 hw_error("register_ioport_read: invalid size");
176 for(i
= start
; i
< start
+ length
; i
+= size
) {
177 ioport_read_table
[bsize
][i
] = func
;
178 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
179 hw_error("register_ioport_read: invalid opaque");
180 ioport_opaque
[i
] = opaque
;
185 /* size is the word size in byte */
186 int register_ioport_write(int start
, int length
, int size
,
187 IOPortWriteFunc
*func
, void *opaque
)
193 } else if (size
== 2) {
195 } else if (size
== 4) {
198 hw_error("register_ioport_write: invalid size");
201 for(i
= start
; i
< start
+ length
; i
+= size
) {
202 ioport_write_table
[bsize
][i
] = func
;
203 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
204 hw_error("register_ioport_read: invalid opaque");
205 ioport_opaque
[i
] = opaque
;
210 void pstrcpy(char *buf
, int buf_size
, const char *str
)
220 if (c
== 0 || q
>= buf
+ buf_size
- 1)
227 /* strcat and truncate. */
228 char *pstrcat(char *buf
, int buf_size
, const char *s
)
233 pstrcpy(buf
+ len
, buf_size
- len
, s
);
237 /* return the size or -1 if error */
238 int load_image(const char *filename
, uint8_t *addr
)
241 fd
= open(filename
, O_RDONLY
);
244 size
= lseek(fd
, 0, SEEK_END
);
245 lseek(fd
, 0, SEEK_SET
);
246 if (read(fd
, addr
, size
) != size
) {
254 void cpu_outb(CPUState
*env
, int addr
, int val
)
256 addr
&= (MAX_IOPORTS
- 1);
257 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
260 void cpu_outw(CPUState
*env
, int addr
, int val
)
262 addr
&= (MAX_IOPORTS
- 1);
263 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
266 void cpu_outl(CPUState
*env
, int addr
, int val
)
268 addr
&= (MAX_IOPORTS
- 1);
269 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
272 int cpu_inb(CPUState
*env
, int addr
)
274 addr
&= (MAX_IOPORTS
- 1);
275 return ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
278 int cpu_inw(CPUState
*env
, int addr
)
280 addr
&= (MAX_IOPORTS
- 1);
281 return ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
284 int cpu_inl(CPUState
*env
, int addr
)
286 addr
&= (MAX_IOPORTS
- 1);
287 return ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
290 /***********************************************************/
291 void hw_error(const char *fmt
, ...)
296 fprintf(stderr
, "qemu: hardware error: ");
297 vfprintf(stderr
, fmt
, ap
);
298 fprintf(stderr
, "\n");
300 cpu_x86_dump_state(global_env
, stderr
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
302 cpu_dump_state(global_env
, stderr
, 0);
308 /***********************************************************/
311 #if defined(__powerpc__)
313 static inline uint32_t get_tbl(void)
316 asm volatile("mftb %0" : "=r" (tbl
));
320 static inline uint32_t get_tbu(void)
323 asm volatile("mftbu %0" : "=r" (tbl
));
327 int64_t cpu_get_real_ticks(void)
330 /* NOTE: we test if wrapping has occurred */
336 return ((int64_t)h
<< 32) | l
;
339 #elif defined(__i386__)
341 int64_t cpu_get_real_ticks(void)
344 asm volatile ("rdtsc" : "=A" (val
));
349 #error unsupported CPU
352 static int64_t cpu_ticks_offset
;
353 static int cpu_ticks_enabled
;
355 static inline int64_t cpu_get_ticks(void)
357 if (!cpu_ticks_enabled
) {
358 return cpu_ticks_offset
;
360 return cpu_get_real_ticks() + cpu_ticks_offset
;
364 /* enable cpu_get_ticks() */
365 void cpu_enable_ticks(void)
367 if (!cpu_ticks_enabled
) {
368 cpu_ticks_offset
-= cpu_get_real_ticks();
369 cpu_ticks_enabled
= 1;
373 /* disable cpu_get_ticks() : the clock is stopped. You must not call
374 cpu_get_ticks() after that. */
375 void cpu_disable_ticks(void)
377 if (cpu_ticks_enabled
) {
378 cpu_ticks_offset
= cpu_get_ticks();
379 cpu_ticks_enabled
= 0;
383 static int64_t get_clock(void)
388 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
391 gettimeofday(&tv
, NULL
);
392 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
396 void cpu_calibrate_ticks(void)
401 ticks
= cpu_get_real_ticks();
407 usec
= get_clock() - usec
;
408 ticks
= cpu_get_real_ticks() - ticks
;
409 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
412 /* compute with 96 bit intermediate result: (a*b)/c */
413 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
418 #ifdef WORDS_BIGENDIAN
428 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
429 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
432 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
436 #define QEMU_TIMER_REALTIME 0
437 #define QEMU_TIMER_VIRTUAL 1
441 /* XXX: add frequency */
449 struct QEMUTimer
*next
;
455 static QEMUTimer
*active_timers
[2];
457 /* frequency of the times() clock tick */
458 static int timer_freq
;
461 QEMUClock
*qemu_new_clock(int type
)
464 clock
= qemu_mallocz(sizeof(QEMUClock
));
471 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
475 ts
= qemu_mallocz(sizeof(QEMUTimer
));
482 void qemu_free_timer(QEMUTimer
*ts
)
487 /* stop a timer, but do not dealloc it */
488 void qemu_del_timer(QEMUTimer
*ts
)
492 /* NOTE: this code must be signal safe because
493 qemu_timer_expired() can be called from a signal. */
494 pt
= &active_timers
[ts
->clock
->type
];
507 /* modify the current timer so that it will be fired when current_time
508 >= expire_time. The corresponding callback will be called. */
509 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
515 /* add the timer in the sorted list */
516 /* NOTE: this code must be signal safe because
517 qemu_timer_expired() can be called from a signal. */
518 pt
= &active_timers
[ts
->clock
->type
];
523 if (t
->expire_time
> expire_time
)
527 ts
->expire_time
= expire_time
;
532 int qemu_timer_pending(QEMUTimer
*ts
)
535 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
542 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
546 return (timer_head
->expire_time
<= current_time
);
549 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
555 if (ts
->expire_time
> current_time
)
557 /* remove timer from the list before calling the callback */
558 *ptimer_head
= ts
->next
;
561 /* run the callback (the timer list can be modified) */
566 int64_t qemu_get_clock(QEMUClock
*clock
)
568 switch(clock
->type
) {
569 case QEMU_TIMER_REALTIME
:
571 return GetTickCount();
573 /* XXX: portability among Linux hosts */
574 if (timer_freq
== 100) {
575 return times(NULL
) * 10;
577 return ((int64_t)times(NULL
) * 1000) / timer_freq
;
581 case QEMU_TIMER_VIRTUAL
:
582 return cpu_get_ticks();
587 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
589 uint64_t expire_time
;
591 if (qemu_timer_pending(ts
)) {
592 expire_time
= ts
->expire_time
;
596 qemu_put_be64(f
, expire_time
);
599 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
601 uint64_t expire_time
;
603 expire_time
= qemu_get_be64(f
);
604 if (expire_time
!= -1) {
605 qemu_mod_timer(ts
, expire_time
);
611 static void timer_save(QEMUFile
*f
, void *opaque
)
613 if (cpu_ticks_enabled
) {
614 hw_error("cannot save state if virtual timers are running");
616 qemu_put_be64s(f
, &cpu_ticks_offset
);
617 qemu_put_be64s(f
, &ticks_per_sec
);
620 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
624 if (cpu_ticks_enabled
) {
627 qemu_get_be64s(f
, &cpu_ticks_offset
);
628 qemu_get_be64s(f
, &ticks_per_sec
);
633 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
634 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
636 static void host_alarm_handler(int host_signum
)
639 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
640 qemu_get_clock(vm_clock
)) ||
641 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
642 qemu_get_clock(rt_clock
))) {
643 /* stop the cpu because a timer occured */
644 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
648 static void init_timers(void)
650 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
651 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
656 MMRESULT timerID
= timeSetEvent(10, // interval (ms)
658 host_alarm_handler
, // function
659 (DWORD
)&count
, // user parameter
660 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
662 perror("failed timer alarm");
666 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
669 struct sigaction act
;
670 struct itimerval itv
;
672 /* get times() syscall frequency */
673 timer_freq
= sysconf(_SC_CLK_TCK
);
676 sigfillset(&act
.sa_mask
);
678 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
679 act
.sa_flags
|= SA_ONSTACK
;
681 act
.sa_handler
= host_alarm_handler
;
682 sigaction(SIGALRM
, &act
, NULL
);
684 itv
.it_interval
.tv_sec
= 0;
685 itv
.it_interval
.tv_usec
= 1000;
686 itv
.it_value
.tv_sec
= 0;
687 itv
.it_value
.tv_usec
= 10 * 1000;
688 setitimer(ITIMER_REAL
, &itv
, NULL
);
689 /* we probe the tick duration of the kernel to inform the user if
690 the emulated kernel requested a too high timer frequency */
691 getitimer(ITIMER_REAL
, &itv
);
692 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
* PIT_FREQ
) /
698 /***********************************************************/
703 int serial_open_device(void)
710 int serial_open_device(void)
712 char slave_name
[1024];
713 int master_fd
, slave_fd
;
715 if (serial_console
== NULL
&& nographic
) {
716 /* use console for serial port */
719 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
720 fprintf(stderr
, "warning: could not create pseudo terminal for serial port\n");
723 fprintf(stderr
, "Serial port redirected to %s\n", slave_name
);
730 /***********************************************************/
731 /* Linux network device redirector */
735 static int net_init(void)
740 void net_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
746 static int tun_open(char *ifname
, int ifname_size
)
751 fd
= open("/dev/net/tun", O_RDWR
);
753 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
756 memset(&ifr
, 0, sizeof(ifr
));
757 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
758 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
759 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
761 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
765 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
766 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
767 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
771 static int net_init(void)
773 int pid
, status
, launch_script
, i
;
775 char *args
[MAX_NICS
+ 2];
779 for(i
= 0; i
< nb_nics
; i
++) {
782 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
789 /* try to launch network init script */
794 *parg
++ = network_script
;
795 for(i
= 0; i
< nb_nics
; i
++) {
798 *parg
++ = nd
->ifname
;
802 execv(network_script
, args
);
805 while (waitpid(pid
, &status
, 0) != pid
);
806 if (!WIFEXITED(status
) ||
807 WEXITSTATUS(status
) != 0) {
808 fprintf(stderr
, "%s: could not launch network script\n",
816 void net_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
819 printf("NE2000: sending packet size=%d\n", size
);
821 write(nd
->fd
, buf
, size
);
826 /***********************************************************/
831 static void term_exit(void)
835 static void term_init(void)
841 /* init terminal so that we can grab keys */
842 static struct termios oldtty
;
844 static void term_exit(void)
846 tcsetattr (0, TCSANOW
, &oldtty
);
849 static void term_init(void)
856 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
857 |INLCR
|IGNCR
|ICRNL
|IXON
);
858 tty
.c_oflag
|= OPOST
;
859 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
860 /* if graphical mode, we allow Ctrl-C handling */
862 tty
.c_lflag
&= ~ISIG
;
863 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
868 tcsetattr (0, TCSANOW
, &tty
);
872 fcntl(0, F_SETFL
, O_NONBLOCK
);
877 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
881 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
885 static void dumb_refresh(DisplayState
*ds
)
887 vga_update_display();
890 void dumb_display_init(DisplayState
*ds
)
895 ds
->dpy_update
= dumb_update
;
896 ds
->dpy_resize
= dumb_resize
;
897 ds
->dpy_refresh
= dumb_refresh
;
900 #if !defined(CONFIG_SOFTMMU)
901 /***********************************************************/
902 /* cpu signal handler */
903 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
906 if (cpu_signal_handler(host_signum
, info
, puc
))
913 /***********************************************************/
916 #define MAX_IO_HANDLERS 64
918 typedef struct IOHandlerRecord
{
920 IOCanRWHandler
*fd_can_read
;
921 IOReadHandler
*fd_read
;
926 struct IOHandlerRecord
*next
;
929 static IOHandlerRecord
*first_io_handler
;
931 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
932 IOReadHandler
*fd_read
, void *opaque
)
934 IOHandlerRecord
*ioh
;
936 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
940 ioh
->fd_can_read
= fd_can_read
;
941 ioh
->fd_read
= fd_read
;
942 ioh
->opaque
= opaque
;
943 ioh
->next
= first_io_handler
;
944 first_io_handler
= ioh
;
948 void qemu_del_fd_read_handler(int fd
)
950 IOHandlerRecord
**pioh
, *ioh
;
952 pioh
= &first_io_handler
;
965 /***********************************************************/
966 /* savevm/loadvm support */
968 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
970 fwrite(buf
, 1, size
, f
);
973 void qemu_put_byte(QEMUFile
*f
, int v
)
978 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
980 qemu_put_byte(f
, v
>> 8);
984 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
986 qemu_put_byte(f
, v
>> 24);
987 qemu_put_byte(f
, v
>> 16);
988 qemu_put_byte(f
, v
>> 8);
992 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
994 qemu_put_be32(f
, v
>> 32);
998 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1000 return fread(buf
, 1, size
, f
);
1003 int qemu_get_byte(QEMUFile
*f
)
1013 unsigned int qemu_get_be16(QEMUFile
*f
)
1016 v
= qemu_get_byte(f
) << 8;
1017 v
|= qemu_get_byte(f
);
1021 unsigned int qemu_get_be32(QEMUFile
*f
)
1024 v
= qemu_get_byte(f
) << 24;
1025 v
|= qemu_get_byte(f
) << 16;
1026 v
|= qemu_get_byte(f
) << 8;
1027 v
|= qemu_get_byte(f
);
1031 uint64_t qemu_get_be64(QEMUFile
*f
)
1034 v
= (uint64_t)qemu_get_be32(f
) << 32;
1035 v
|= qemu_get_be32(f
);
1039 int64_t qemu_ftell(QEMUFile
*f
)
1044 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1046 if (fseek(f
, pos
, whence
) < 0)
1051 typedef struct SaveStateEntry
{
1055 SaveStateHandler
*save_state
;
1056 LoadStateHandler
*load_state
;
1058 struct SaveStateEntry
*next
;
1061 static SaveStateEntry
*first_se
;
1063 int register_savevm(const char *idstr
,
1066 SaveStateHandler
*save_state
,
1067 LoadStateHandler
*load_state
,
1070 SaveStateEntry
*se
, **pse
;
1072 se
= qemu_malloc(sizeof(SaveStateEntry
));
1075 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1076 se
->instance_id
= instance_id
;
1077 se
->version_id
= version_id
;
1078 se
->save_state
= save_state
;
1079 se
->load_state
= load_state
;
1080 se
->opaque
= opaque
;
1083 /* add at the end of list */
1085 while (*pse
!= NULL
)
1086 pse
= &(*pse
)->next
;
1091 #define QEMU_VM_FILE_MAGIC 0x5145564d
1092 #define QEMU_VM_FILE_VERSION 0x00000001
1094 int qemu_savevm(const char *filename
)
1098 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1100 saved_vm_running
= vm_running
;
1103 f
= fopen(filename
, "wb");
1109 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1110 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1112 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1114 len
= strlen(se
->idstr
);
1115 qemu_put_byte(f
, len
);
1116 qemu_put_buffer(f
, se
->idstr
, len
);
1118 qemu_put_be32(f
, se
->instance_id
);
1119 qemu_put_be32(f
, se
->version_id
);
1121 /* record size: filled later */
1123 qemu_put_be32(f
, 0);
1125 se
->save_state(f
, se
->opaque
);
1127 /* fill record size */
1129 len
= ftell(f
) - len_pos
- 4;
1130 fseek(f
, len_pos
, SEEK_SET
);
1131 qemu_put_be32(f
, len
);
1132 fseek(f
, cur_pos
, SEEK_SET
);
1138 if (saved_vm_running
)
1143 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1147 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1148 if (!strcmp(se
->idstr
, idstr
) &&
1149 instance_id
== se
->instance_id
)
1155 int qemu_loadvm(const char *filename
)
1159 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
1160 int saved_vm_running
;
1164 saved_vm_running
= vm_running
;
1167 f
= fopen(filename
, "rb");
1173 v
= qemu_get_be32(f
);
1174 if (v
!= QEMU_VM_FILE_MAGIC
)
1176 v
= qemu_get_be32(f
);
1177 if (v
!= QEMU_VM_FILE_VERSION
) {
1184 len
= qemu_get_byte(f
);
1187 qemu_get_buffer(f
, idstr
, len
);
1189 instance_id
= qemu_get_be32(f
);
1190 version_id
= qemu_get_be32(f
);
1191 record_len
= qemu_get_be32(f
);
1193 printf("idstr=%s instance=0x%x version=%d len=%d\n",
1194 idstr
, instance_id
, version_id
, record_len
);
1197 se
= find_se(idstr
, instance_id
);
1199 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1200 instance_id
, idstr
);
1202 ret
= se
->load_state(f
, se
->opaque
, version_id
);
1204 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1205 instance_id
, idstr
);
1208 /* always seek to exact end of record */
1209 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
1214 if (saved_vm_running
)
1219 /***********************************************************/
1220 /* cpu save/restore */
1222 #if defined(TARGET_I386)
1224 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
1226 qemu_put_be32(f
, (uint32_t)dt
->base
);
1227 qemu_put_be32(f
, dt
->limit
);
1228 qemu_put_be32(f
, dt
->flags
);
1231 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
1233 dt
->base
= (uint8_t *)qemu_get_be32(f
);
1234 dt
->limit
= qemu_get_be32(f
);
1235 dt
->flags
= qemu_get_be32(f
);
1238 void cpu_save(QEMUFile
*f
, void *opaque
)
1240 CPUState
*env
= opaque
;
1241 uint16_t fptag
, fpus
, fpuc
;
1245 for(i
= 0; i
< 8; i
++)
1246 qemu_put_be32s(f
, &env
->regs
[i
]);
1247 qemu_put_be32s(f
, &env
->eip
);
1248 qemu_put_be32s(f
, &env
->eflags
);
1249 qemu_put_be32s(f
, &env
->eflags
);
1250 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
1251 qemu_put_be32s(f
, &hflags
);
1255 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
1257 for (i
=7; i
>=0; i
--) {
1259 if (env
->fptags
[i
]) {
1264 qemu_put_be16s(f
, &fpuc
);
1265 qemu_put_be16s(f
, &fpus
);
1266 qemu_put_be16s(f
, &fptag
);
1268 for(i
= 0; i
< 8; i
++) {
1271 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
]);
1272 qemu_put_be64(f
, mant
);
1273 qemu_put_be16(f
, exp
);
1276 for(i
= 0; i
< 6; i
++)
1277 cpu_put_seg(f
, &env
->segs
[i
]);
1278 cpu_put_seg(f
, &env
->ldt
);
1279 cpu_put_seg(f
, &env
->tr
);
1280 cpu_put_seg(f
, &env
->gdt
);
1281 cpu_put_seg(f
, &env
->idt
);
1283 qemu_put_be32s(f
, &env
->sysenter_cs
);
1284 qemu_put_be32s(f
, &env
->sysenter_esp
);
1285 qemu_put_be32s(f
, &env
->sysenter_eip
);
1287 qemu_put_be32s(f
, &env
->cr
[0]);
1288 qemu_put_be32s(f
, &env
->cr
[2]);
1289 qemu_put_be32s(f
, &env
->cr
[3]);
1290 qemu_put_be32s(f
, &env
->cr
[4]);
1292 for(i
= 0; i
< 8; i
++)
1293 qemu_put_be32s(f
, &env
->dr
[i
]);
1296 qemu_put_be32s(f
, &env
->a20_mask
);
1299 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
1301 CPUState
*env
= opaque
;
1304 uint16_t fpus
, fpuc
, fptag
;
1306 if (version_id
!= 1)
1308 for(i
= 0; i
< 8; i
++)
1309 qemu_get_be32s(f
, &env
->regs
[i
]);
1310 qemu_get_be32s(f
, &env
->eip
);
1311 qemu_get_be32s(f
, &env
->eflags
);
1312 qemu_get_be32s(f
, &env
->eflags
);
1313 qemu_get_be32s(f
, &hflags
);
1315 qemu_get_be16s(f
, &fpuc
);
1316 qemu_get_be16s(f
, &fpus
);
1317 qemu_get_be16s(f
, &fptag
);
1319 for(i
= 0; i
< 8; i
++) {
1322 mant
= qemu_get_be64(f
);
1323 exp
= qemu_get_be16(f
);
1324 env
->fpregs
[i
] = cpu_set_fp80(mant
, exp
);
1328 env
->fpstt
= (fpus
>> 11) & 7;
1329 env
->fpus
= fpus
& ~0x3800;
1330 for(i
= 0; i
< 8; i
++) {
1331 env
->fptags
[i
] = ((fptag
& 3) == 3);
1335 for(i
= 0; i
< 6; i
++)
1336 cpu_get_seg(f
, &env
->segs
[i
]);
1337 cpu_get_seg(f
, &env
->ldt
);
1338 cpu_get_seg(f
, &env
->tr
);
1339 cpu_get_seg(f
, &env
->gdt
);
1340 cpu_get_seg(f
, &env
->idt
);
1342 qemu_get_be32s(f
, &env
->sysenter_cs
);
1343 qemu_get_be32s(f
, &env
->sysenter_esp
);
1344 qemu_get_be32s(f
, &env
->sysenter_eip
);
1346 qemu_get_be32s(f
, &env
->cr
[0]);
1347 qemu_get_be32s(f
, &env
->cr
[2]);
1348 qemu_get_be32s(f
, &env
->cr
[3]);
1349 qemu_get_be32s(f
, &env
->cr
[4]);
1351 for(i
= 0; i
< 8; i
++)
1352 qemu_get_be32s(f
, &env
->dr
[i
]);
1355 qemu_get_be32s(f
, &env
->a20_mask
);
1357 /* XXX: compute hflags from scratch, except for CPL and IIF */
1358 env
->hflags
= hflags
;
1365 #warning No CPU save/restore functions
1369 /***********************************************************/
1370 /* ram save/restore */
1372 /* we just avoid storing empty pages */
1373 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
1378 for(i
= 1; i
< len
; i
++) {
1382 qemu_put_byte(f
, 1);
1383 qemu_put_byte(f
, v
);
1386 qemu_put_byte(f
, 0);
1387 qemu_put_buffer(f
, buf
, len
);
1390 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
1394 v
= qemu_get_byte(f
);
1397 if (qemu_get_buffer(f
, buf
, len
) != len
)
1401 v
= qemu_get_byte(f
);
1402 memset(buf
, v
, len
);
1410 static void ram_save(QEMUFile
*f
, void *opaque
)
1413 qemu_put_be32(f
, phys_ram_size
);
1414 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1415 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1419 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
1423 if (version_id
!= 1)
1425 if (qemu_get_be32(f
) != phys_ram_size
)
1427 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
1428 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
1435 /***********************************************************/
1436 /* main execution loop */
1438 void gui_update(void *opaque
)
1440 display_state
.dpy_refresh(&display_state
);
1441 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
1444 /* XXX: support several handlers */
1445 VMStopHandler
*vm_stop_cb
;
1446 VMStopHandler
*vm_stop_opaque
;
1448 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1451 vm_stop_opaque
= opaque
;
1455 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
1468 void vm_stop(int reason
)
1471 cpu_disable_ticks();
1475 vm_stop_cb(vm_stop_opaque
, reason
);
1484 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
1485 IOHandlerRecord
*ioh
, *ioh_next
;
1490 CPUState
*env
= global_env
;
1494 ret
= cpu_exec(env
);
1495 if (reset_requested
) {
1496 ret
= EXCP_INTERRUPT
;
1499 if (ret
== EXCP_DEBUG
) {
1500 vm_stop(EXCP_DEBUG
);
1502 /* if hlt instruction, we wait until the next IRQ */
1503 /* XXX: use timeout computed from timers */
1504 if (ret
== EXCP_HLT
)
1513 /* poll any events */
1514 /* XXX: separate device handlers from system ones */
1516 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
1517 if (!ioh
->fd_can_read
) {
1520 pf
->events
= POLLIN
;
1524 max_size
= ioh
->fd_can_read(ioh
->opaque
);
1526 if (max_size
> sizeof(buf
))
1527 max_size
= sizeof(buf
);
1529 pf
->events
= POLLIN
;
1536 ioh
->max_size
= max_size
;
1539 ret
= poll(ufds
, pf
- ufds
, timeout
);
1541 /* XXX: better handling of removal */
1542 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
1543 ioh_next
= ioh
->next
;
1546 if (pf
->revents
& POLLIN
) {
1547 if (ioh
->max_size
== 0) {
1548 /* just a read event */
1549 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
1551 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
1553 ioh
->fd_read(ioh
->opaque
, buf
, n
);
1554 } else if (errno
!= -EAGAIN
) {
1555 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
1565 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1566 qemu_get_clock(vm_clock
));
1568 /* XXX: add explicit timer */
1571 /* run dma transfers, if any */
1575 /* real time timers */
1576 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1577 qemu_get_clock(rt_clock
));
1579 cpu_disable_ticks();
1585 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003 Fabrice Bellard\n"
1586 "usage: %s [options] [disk_image]\n"
1588 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
1590 "Standard options:\n"
1591 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
1592 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
1593 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
1594 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
1595 "-boot [a|b|c|d] boot on floppy (a, b), hard disk (c) or CD-ROM (d)\n"
1596 "-snapshot write to temporary files instead of disk image files\n"
1597 "-m megs set virtual RAM size to megs MB\n"
1598 "-nographic disable graphical output and redirect serial I/Os to console\n"
1600 "Network options:\n"
1601 "-n script set network init script [default=%s]\n"
1602 "-nics n simulate 'n' network interfaces [default=1]\n"
1603 "-tun-fd fd0[,...] use these fds as already opened tap/tun interfaces\n"
1605 "Linux boot specific:\n"
1606 "-kernel bzImage use 'bzImage' as kernel image\n"
1607 "-append cmdline use 'cmdline' as kernel command line\n"
1608 "-initrd file use 'file' as initial ram disk\n"
1610 "Debug/Expert options:\n"
1611 "-s wait gdb connection to port %d\n"
1612 "-p port change gdb connection port\n"
1613 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
1614 "-hdachs c,h,s force hard disk 0 geometry (usually qemu can guess it)\n"
1615 "-L path set the directory for the BIOS and VGA BIOS\n"
1616 #ifdef USE_CODE_COPY
1617 "-no-code-copy disable code copy acceleration\n"
1621 "During emulation, use C-a h to get terminal commands:\n",
1622 #ifdef CONFIG_SOFTMMU
1627 DEFAULT_NETWORK_SCRIPT
,
1628 DEFAULT_GDBSTUB_PORT
,
1631 #ifndef CONFIG_SOFTMMU
1633 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
1634 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
1640 struct option long_options
[] = {
1641 { "initrd", 1, NULL
, 0, },
1642 { "hda", 1, NULL
, 0, },
1643 { "hdb", 1, NULL
, 0, },
1644 { "snapshot", 0, NULL
, 0, },
1645 { "hdachs", 1, NULL
, 0, },
1646 { "nographic", 0, NULL
, 0, },
1647 { "kernel", 1, NULL
, 0, },
1648 { "append", 1, NULL
, 0, },
1649 { "tun-fd", 1, NULL
, 0, },
1650 { "hdc", 1, NULL
, 0, },
1651 { "hdd", 1, NULL
, 0, },
1652 { "cdrom", 1, NULL
, 0, },
1653 { "boot", 1, NULL
, 0, },
1654 { "fda", 1, NULL
, 0, },
1655 { "fdb", 1, NULL
, 0, },
1656 { "no-code-copy", 0, NULL
, 0 },
1657 { "nics", 1, NULL
, 0 },
1658 { NULL
, 0, NULL
, 0 },
1662 /* SDL use the pthreads and they modify sigaction. We don't
1664 #if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 2)
1665 extern void __libc_sigaction();
1666 #define sigaction(sig, act, oact) __libc_sigaction(sig, act, oact)
1668 extern void __sigaction();
1669 #define sigaction(sig, act, oact) __sigaction(sig, act, oact)
1671 #endif /* CONFIG_SDL */
1673 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
1675 /* this stack is only used during signal handling */
1676 #define SIGNAL_STACK_SIZE 32768
1678 static uint8_t *signal_stack
;
1682 int main(int argc
, char **argv
)
1684 #ifdef CONFIG_GDBSTUB
1685 int use_gdbstub
, gdbstub_port
;
1687 int c
, i
, long_index
, has_cdrom
;
1688 int snapshot
, linux_boot
;
1690 const char *initrd_filename
;
1691 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
1692 const char *kernel_filename
, *kernel_cmdline
;
1693 DisplayState
*ds
= &display_state
;
1694 int cyls
, heads
, secs
;
1696 #if !defined(CONFIG_SOFTMMU)
1697 /* we never want that malloc() uses mmap() */
1698 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
1700 initrd_filename
= NULL
;
1701 for(i
= 0; i
< MAX_FD
; i
++)
1702 fd_filename
[i
] = NULL
;
1703 for(i
= 0; i
< MAX_DISKS
; i
++)
1704 hd_filename
[i
] = NULL
;
1705 ram_size
= 32 * 1024 * 1024;
1706 vga_ram_size
= VGA_RAM_SIZE
;
1707 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
1708 #ifdef CONFIG_GDBSTUB
1710 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
1714 kernel_filename
= NULL
;
1715 kernel_cmdline
= "";
1717 cyls
= heads
= secs
= 0;
1720 for(i
= 0; i
< MAX_NICS
; i
++) {
1721 NetDriverState
*nd
= &nd_table
[i
];
1723 /* init virtual mac address */
1724 nd
->macaddr
[0] = 0x52;
1725 nd
->macaddr
[1] = 0x54;
1726 nd
->macaddr
[2] = 0x00;
1727 nd
->macaddr
[3] = 0x12;
1728 nd
->macaddr
[4] = 0x34;
1729 nd
->macaddr
[5] = 0x56 + i
;
1733 c
= getopt_long_only(argc
, argv
, "hm:d:n:sp:L:", long_options
, &long_index
);
1738 switch(long_index
) {
1740 initrd_filename
= optarg
;
1743 hd_filename
[0] = optarg
;
1746 hd_filename
[1] = optarg
;
1755 cyls
= strtol(p
, (char **)&p
, 0);
1759 heads
= strtol(p
, (char **)&p
, 0);
1763 secs
= strtol(p
, (char **)&p
, 0);
1774 kernel_filename
= optarg
;
1777 kernel_cmdline
= optarg
;
1786 fd
= strtol(p
, (char **)&p
, 0);
1787 nd_table
[nb_nics
].fd
= fd
;
1788 snprintf(nd_table
[nb_nics
].ifname
,
1789 sizeof(nd_table
[nb_nics
].ifname
),
1794 } else if (*p
!= '\0') {
1795 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_nics
);
1804 hd_filename
[2] = optarg
;
1808 hd_filename
[3] = optarg
;
1811 hd_filename
[2] = optarg
;
1815 boot_device
= optarg
[0];
1816 if (boot_device
!= 'a' && boot_device
!= 'b' &&
1817 boot_device
!= 'c' && boot_device
!= 'd') {
1818 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
1823 fd_filename
[0] = optarg
;
1826 fd_filename
[1] = optarg
;
1829 code_copy_enabled
= 0;
1832 nb_nics
= atoi(optarg
);
1833 if (nb_nics
< 1 || nb_nics
> MAX_NICS
) {
1834 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
1844 ram_size
= atoi(optarg
) * 1024 * 1024;
1847 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
1848 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
1849 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
1858 mask
= cpu_str_to_log_mask(optarg
);
1860 printf("Log items (comma separated):\n");
1861 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
1862 printf("%-10s %s\n", item
->name
, item
->help
);
1870 pstrcpy(network_script
, sizeof(network_script
), optarg
);
1872 #ifdef CONFIG_GDBSTUB
1877 gdbstub_port
= atoi(optarg
);
1886 if (optind
< argc
) {
1887 hd_filename
[0] = argv
[optind
++];
1890 linux_boot
= (kernel_filename
!= NULL
);
1892 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
1893 fd_filename
[0] == '\0')
1896 /* boot to cd by default if no hard disk */
1897 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
1898 if (fd_filename
[0] != '\0')
1904 #if !defined(CONFIG_SOFTMMU)
1905 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1907 static uint8_t stdout_buf
[4096];
1908 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
1911 setvbuf(stdout
, NULL
, _IOLBF
, 0);
1914 /* init host network redirectors */
1917 /* init the memory */
1918 phys_ram_size
= ram_size
+ vga_ram_size
;
1920 #ifdef CONFIG_SOFTMMU
1921 phys_ram_base
= memalign(TARGET_PAGE_SIZE
, phys_ram_size
);
1922 if (!phys_ram_base
) {
1923 fprintf(stderr
, "Could not allocate physical memory\n");
1927 /* as we must map the same page at several addresses, we must use
1932 tmpdir
= getenv("QEMU_TMPDIR");
1935 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
1936 if (mkstemp(phys_ram_file
) < 0) {
1937 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
1941 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
1942 if (phys_ram_fd
< 0) {
1943 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
1947 ftruncate(phys_ram_fd
, phys_ram_size
);
1948 unlink(phys_ram_file
);
1949 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
1951 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
1953 if (phys_ram_base
== MAP_FAILED
) {
1954 fprintf(stderr
, "Could not map physical memory\n");
1960 /* we always create the cdrom drive, even if no disk is there */
1962 bs_table
[2] = bdrv_new("cdrom");
1963 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
1966 /* open the virtual block devices */
1967 for(i
= 0; i
< MAX_DISKS
; i
++) {
1968 if (hd_filename
[i
]) {
1971 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
1972 bs_table
[i
] = bdrv_new(buf
);
1974 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
1975 fprintf(stderr
, "qemu: could not open hard disk image '%s\n",
1979 if (i
== 0 && cyls
!= 0)
1980 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
1984 /* we always create at least one floppy disk */
1985 fd_table
[0] = bdrv_new("fda");
1986 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
1988 for(i
= 0; i
< MAX_FD
; i
++) {
1989 if (fd_filename
[i
]) {
1992 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
1993 fd_table
[i
] = bdrv_new(buf
);
1994 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
1996 if (fd_filename
[i
] != '\0') {
1997 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
1998 fprintf(stderr
, "qemu: could not open floppy disk image '%s\n",
2008 /* init CPU state */
2011 cpu_single_env
= env
;
2013 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
2014 register_savevm("cpu", 0, 1, cpu_save
, cpu_load
, env
);
2015 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
2018 cpu_calibrate_ticks();
2022 dumb_display_init(ds
);
2025 sdl_display_init(ds
);
2027 dumb_display_init(ds
);
2031 #if defined(TARGET_I386)
2032 pc_init(ram_size
, vga_ram_size
, boot_device
,
2033 ds
, fd_filename
, snapshot
,
2034 kernel_filename
, kernel_cmdline
, initrd_filename
);
2035 #elif defined(TARGET_PPC)
2039 /* launched after the device init so that it can display or not a
2043 /* setup cpu signal handlers for MMU / self modifying code handling */
2044 #if !defined(CONFIG_SOFTMMU)
2046 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2049 signal_stack
= malloc(SIGNAL_STACK_SIZE
);
2050 stk
.ss_sp
= signal_stack
;
2051 stk
.ss_size
= SIGNAL_STACK_SIZE
;
2054 if (sigaltstack(&stk
, NULL
) < 0) {
2055 perror("sigaltstack");
2061 struct sigaction act
;
2063 sigfillset(&act
.sa_mask
);
2064 act
.sa_flags
= SA_SIGINFO
;
2065 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2066 act
.sa_flags
|= SA_ONSTACK
;
2068 act
.sa_sigaction
= host_segv_handler
;
2069 sigaction(SIGSEGV
, &act
, NULL
);
2070 sigaction(SIGBUS
, &act
, NULL
);
2071 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2072 sigaction(SIGFPE
, &act
, NULL
);
2079 struct sigaction act
;
2080 sigfillset(&act
.sa_mask
);
2082 act
.sa_handler
= SIG_IGN
;
2083 sigaction(SIGPIPE
, &act
, NULL
);
2087 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
2088 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
2090 #ifdef CONFIG_GDBSTUB
2092 if (gdbserver_start(gdbstub_port
) < 0) {
2093 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
2097 printf("Waiting gdb connection on port %d\n", gdbstub_port
);