4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
34 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
50 #include <linux/if_tun.h>
53 #include <linux/rtc.h>
57 #if defined(CONFIG_SLIRP)
63 #include <sys/timeb.h>
65 #define getopt_long_only getopt_long
66 #define memalign(align, size) malloc(size)
73 #endif /* CONFIG_SDL */
77 #define main qemu_main
78 #endif /* CONFIG_COCOA */
86 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
88 //#define DEBUG_UNUSED_IOPORT
89 //#define DEBUG_IOPORT
91 #if !defined(CONFIG_SOFTMMU)
92 #define PHYS_RAM_MAX_SIZE (256 * 1024 * 1024)
94 #define PHYS_RAM_MAX_SIZE (2047 * 1024 * 1024)
98 #define DEFAULT_RAM_SIZE 144
100 #define DEFAULT_RAM_SIZE 128
103 #define GUI_REFRESH_INTERVAL 30
105 /* XXX: use a two level table to limit memory usage */
106 #define MAX_IOPORTS 65536
108 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
109 char phys_ram_file
[1024];
110 CPUState
*global_env
;
111 CPUState
*cpu_single_env
;
112 void *ioport_opaque
[MAX_IOPORTS
];
113 IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
114 IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
115 BlockDriverState
*bs_table
[MAX_DISKS
], *fd_table
[MAX_FD
];
118 static DisplayState display_state
;
120 const char* keyboard_layout
= NULL
;
121 int64_t ticks_per_sec
;
122 int boot_device
= 'c';
124 static char network_script
[1024];
125 int pit_min_timer_count
= 0;
127 NetDriverState nd_table
[MAX_NICS
];
128 QEMUTimer
*gui_timer
;
130 int audio_enabled
= 0;
131 int sb16_enabled
= 1;
132 int adlib_enabled
= 1;
135 int prep_enabled
= 0;
137 int cirrus_vga_enabled
= 1;
139 int graphic_width
= 1024;
140 int graphic_height
= 768;
142 int graphic_width
= 800;
143 int graphic_height
= 600;
145 int graphic_depth
= 15;
147 TextConsole
*vga_console
;
148 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
149 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
151 /***********************************************************/
152 /* x86 ISA bus support */
154 target_phys_addr_t isa_mem_base
= 0;
156 uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
158 #ifdef DEBUG_UNUSED_IOPORT
159 fprintf(stderr
, "inb: port=0x%04x\n", address
);
164 void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
166 #ifdef DEBUG_UNUSED_IOPORT
167 fprintf(stderr
, "outb: port=0x%04x data=0x%02x\n", address
, data
);
171 /* default is to make two byte accesses */
172 uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
175 data
= ioport_read_table
[0][address
](ioport_opaque
[address
], address
);
176 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
177 data
|= ioport_read_table
[0][address
](ioport_opaque
[address
], address
) << 8;
181 void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
183 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, data
& 0xff);
184 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
185 ioport_write_table
[0][address
](ioport_opaque
[address
], address
, (data
>> 8) & 0xff);
188 uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
190 #ifdef DEBUG_UNUSED_IOPORT
191 fprintf(stderr
, "inl: port=0x%04x\n", address
);
196 void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
198 #ifdef DEBUG_UNUSED_IOPORT
199 fprintf(stderr
, "outl: port=0x%04x data=0x%02x\n", address
, data
);
203 void init_ioports(void)
207 for(i
= 0; i
< MAX_IOPORTS
; i
++) {
208 ioport_read_table
[0][i
] = default_ioport_readb
;
209 ioport_write_table
[0][i
] = default_ioport_writeb
;
210 ioport_read_table
[1][i
] = default_ioport_readw
;
211 ioport_write_table
[1][i
] = default_ioport_writew
;
212 ioport_read_table
[2][i
] = default_ioport_readl
;
213 ioport_write_table
[2][i
] = default_ioport_writel
;
217 /* size is the word size in byte */
218 int register_ioport_read(int start
, int length
, int size
,
219 IOPortReadFunc
*func
, void *opaque
)
225 } else if (size
== 2) {
227 } else if (size
== 4) {
230 hw_error("register_ioport_read: invalid size");
233 for(i
= start
; i
< start
+ length
; i
+= size
) {
234 ioport_read_table
[bsize
][i
] = func
;
235 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
236 hw_error("register_ioport_read: invalid opaque");
237 ioport_opaque
[i
] = opaque
;
242 /* size is the word size in byte */
243 int register_ioport_write(int start
, int length
, int size
,
244 IOPortWriteFunc
*func
, void *opaque
)
250 } else if (size
== 2) {
252 } else if (size
== 4) {
255 hw_error("register_ioport_write: invalid size");
258 for(i
= start
; i
< start
+ length
; i
+= size
) {
259 ioport_write_table
[bsize
][i
] = func
;
260 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
261 hw_error("register_ioport_read: invalid opaque");
262 ioport_opaque
[i
] = opaque
;
267 void isa_unassign_ioport(int start
, int length
)
271 for(i
= start
; i
< start
+ length
; i
++) {
272 ioport_read_table
[0][i
] = default_ioport_readb
;
273 ioport_read_table
[1][i
] = default_ioport_readw
;
274 ioport_read_table
[2][i
] = default_ioport_readl
;
276 ioport_write_table
[0][i
] = default_ioport_writeb
;
277 ioport_write_table
[1][i
] = default_ioport_writew
;
278 ioport_write_table
[2][i
] = default_ioport_writel
;
282 /***********************************************************/
284 void pstrcpy(char *buf
, int buf_size
, const char *str
)
294 if (c
== 0 || q
>= buf
+ buf_size
- 1)
301 /* strcat and truncate. */
302 char *pstrcat(char *buf
, int buf_size
, const char *s
)
307 pstrcpy(buf
+ len
, buf_size
- len
, s
);
311 int strstart(const char *str
, const char *val
, const char **ptr
)
327 /* return the size or -1 if error */
328 int get_image_size(const char *filename
)
331 fd
= open(filename
, O_RDONLY
| O_BINARY
);
334 size
= lseek(fd
, 0, SEEK_END
);
339 /* return the size or -1 if error */
340 int load_image(const char *filename
, uint8_t *addr
)
343 fd
= open(filename
, O_RDONLY
| O_BINARY
);
346 size
= lseek(fd
, 0, SEEK_END
);
347 lseek(fd
, 0, SEEK_SET
);
348 if (read(fd
, addr
, size
) != size
) {
356 void cpu_outb(CPUState
*env
, int addr
, int val
)
359 if (loglevel
& CPU_LOG_IOPORT
)
360 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
362 ioport_write_table
[0][addr
](ioport_opaque
[addr
], addr
, val
);
365 void cpu_outw(CPUState
*env
, int addr
, int val
)
368 if (loglevel
& CPU_LOG_IOPORT
)
369 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
371 ioport_write_table
[1][addr
](ioport_opaque
[addr
], addr
, val
);
374 void cpu_outl(CPUState
*env
, int addr
, int val
)
377 if (loglevel
& CPU_LOG_IOPORT
)
378 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
380 ioport_write_table
[2][addr
](ioport_opaque
[addr
], addr
, val
);
383 int cpu_inb(CPUState
*env
, int addr
)
386 val
= ioport_read_table
[0][addr
](ioport_opaque
[addr
], addr
);
388 if (loglevel
& CPU_LOG_IOPORT
)
389 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
394 int cpu_inw(CPUState
*env
, int addr
)
397 val
= ioport_read_table
[1][addr
](ioport_opaque
[addr
], addr
);
399 if (loglevel
& CPU_LOG_IOPORT
)
400 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
405 int cpu_inl(CPUState
*env
, int addr
)
408 val
= ioport_read_table
[2][addr
](ioport_opaque
[addr
], addr
);
410 if (loglevel
& CPU_LOG_IOPORT
)
411 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
416 /***********************************************************/
417 void hw_error(const char *fmt
, ...)
422 fprintf(stderr
, "qemu: hardware error: ");
423 vfprintf(stderr
, fmt
, ap
);
424 fprintf(stderr
, "\n");
426 cpu_dump_state(global_env
, stderr
, fprintf
, X86_DUMP_FPU
| X86_DUMP_CCOP
);
428 cpu_dump_state(global_env
, stderr
, fprintf
, 0);
434 /***********************************************************/
437 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
438 static void *qemu_put_kbd_event_opaque
;
439 static QEMUPutMouseEvent
*qemu_put_mouse_event
;
440 static void *qemu_put_mouse_event_opaque
;
442 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
444 qemu_put_kbd_event_opaque
= opaque
;
445 qemu_put_kbd_event
= func
;
448 void qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
, void *opaque
)
450 qemu_put_mouse_event_opaque
= opaque
;
451 qemu_put_mouse_event
= func
;
454 void kbd_put_keycode(int keycode
)
456 if (qemu_put_kbd_event
) {
457 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
461 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
463 if (qemu_put_mouse_event
) {
464 qemu_put_mouse_event(qemu_put_mouse_event_opaque
,
465 dx
, dy
, dz
, buttons_state
);
469 /***********************************************************/
472 #if defined(__powerpc__)
474 static inline uint32_t get_tbl(void)
477 asm volatile("mftb %0" : "=r" (tbl
));
481 static inline uint32_t get_tbu(void)
484 asm volatile("mftbu %0" : "=r" (tbl
));
488 int64_t cpu_get_real_ticks(void)
491 /* NOTE: we test if wrapping has occurred */
497 return ((int64_t)h
<< 32) | l
;
500 #elif defined(__i386__)
502 int64_t cpu_get_real_ticks(void)
505 asm volatile ("rdtsc" : "=A" (val
));
509 #elif defined(__x86_64__)
511 int64_t cpu_get_real_ticks(void)
515 asm volatile("rdtsc" : "=a" (low
), "=d" (high
));
523 #error unsupported CPU
526 static int64_t cpu_ticks_offset
;
527 static int cpu_ticks_enabled
;
529 static inline int64_t cpu_get_ticks(void)
531 if (!cpu_ticks_enabled
) {
532 return cpu_ticks_offset
;
534 return cpu_get_real_ticks() + cpu_ticks_offset
;
538 /* enable cpu_get_ticks() */
539 void cpu_enable_ticks(void)
541 if (!cpu_ticks_enabled
) {
542 cpu_ticks_offset
-= cpu_get_real_ticks();
543 cpu_ticks_enabled
= 1;
547 /* disable cpu_get_ticks() : the clock is stopped. You must not call
548 cpu_get_ticks() after that. */
549 void cpu_disable_ticks(void)
551 if (cpu_ticks_enabled
) {
552 cpu_ticks_offset
= cpu_get_ticks();
553 cpu_ticks_enabled
= 0;
557 static int64_t get_clock(void)
562 return ((int64_t)tb
.time
* 1000 + (int64_t)tb
.millitm
) * 1000;
565 gettimeofday(&tv
, NULL
);
566 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
570 void cpu_calibrate_ticks(void)
575 ticks
= cpu_get_real_ticks();
581 usec
= get_clock() - usec
;
582 ticks
= cpu_get_real_ticks() - ticks
;
583 ticks_per_sec
= (ticks
* 1000000LL + (usec
>> 1)) / usec
;
586 /* compute with 96 bit intermediate result: (a*b)/c */
587 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
592 #ifdef WORDS_BIGENDIAN
602 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
603 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
606 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
610 #define QEMU_TIMER_REALTIME 0
611 #define QEMU_TIMER_VIRTUAL 1
615 /* XXX: add frequency */
623 struct QEMUTimer
*next
;
629 static QEMUTimer
*active_timers
[2];
631 static MMRESULT timerID
;
633 /* frequency of the times() clock tick */
634 static int timer_freq
;
637 QEMUClock
*qemu_new_clock(int type
)
640 clock
= qemu_mallocz(sizeof(QEMUClock
));
647 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
651 ts
= qemu_mallocz(sizeof(QEMUTimer
));
658 void qemu_free_timer(QEMUTimer
*ts
)
663 /* stop a timer, but do not dealloc it */
664 void qemu_del_timer(QEMUTimer
*ts
)
668 /* NOTE: this code must be signal safe because
669 qemu_timer_expired() can be called from a signal. */
670 pt
= &active_timers
[ts
->clock
->type
];
683 /* modify the current timer so that it will be fired when current_time
684 >= expire_time. The corresponding callback will be called. */
685 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
691 /* add the timer in the sorted list */
692 /* NOTE: this code must be signal safe because
693 qemu_timer_expired() can be called from a signal. */
694 pt
= &active_timers
[ts
->clock
->type
];
699 if (t
->expire_time
> expire_time
)
703 ts
->expire_time
= expire_time
;
708 int qemu_timer_pending(QEMUTimer
*ts
)
711 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
718 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
722 return (timer_head
->expire_time
<= current_time
);
725 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
731 if (!ts
|| ts
->expire_time
> current_time
)
733 /* remove timer from the list before calling the callback */
734 *ptimer_head
= ts
->next
;
737 /* run the callback (the timer list can be modified) */
742 int64_t qemu_get_clock(QEMUClock
*clock
)
744 switch(clock
->type
) {
745 case QEMU_TIMER_REALTIME
:
747 return GetTickCount();
752 /* Note that using gettimeofday() is not a good solution
753 for timers because its value change when the date is
755 if (timer_freq
== 100) {
756 return times(&tp
) * 10;
758 return ((int64_t)times(&tp
) * 1000) / timer_freq
;
763 case QEMU_TIMER_VIRTUAL
:
764 return cpu_get_ticks();
769 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
771 uint64_t expire_time
;
773 if (qemu_timer_pending(ts
)) {
774 expire_time
= ts
->expire_time
;
778 qemu_put_be64(f
, expire_time
);
781 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
783 uint64_t expire_time
;
785 expire_time
= qemu_get_be64(f
);
786 if (expire_time
!= -1) {
787 qemu_mod_timer(ts
, expire_time
);
793 static void timer_save(QEMUFile
*f
, void *opaque
)
795 if (cpu_ticks_enabled
) {
796 hw_error("cannot save state if virtual timers are running");
798 qemu_put_be64s(f
, &cpu_ticks_offset
);
799 qemu_put_be64s(f
, &ticks_per_sec
);
802 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
806 if (cpu_ticks_enabled
) {
809 qemu_get_be64s(f
, &cpu_ticks_offset
);
810 qemu_get_be64s(f
, &ticks_per_sec
);
815 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
816 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
818 static void host_alarm_handler(int host_signum
)
822 #define DISP_FREQ 1000
824 static int64_t delta_min
= INT64_MAX
;
825 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
827 ti
= qemu_get_clock(vm_clock
);
828 if (last_clock
!= 0) {
829 delta
= ti
- last_clock
;
830 if (delta
< delta_min
)
832 if (delta
> delta_max
)
835 if (++count
== DISP_FREQ
) {
836 printf("timer: min=%lld us max=%lld us avg=%lld us avg_freq=%0.3f Hz\n",
837 muldiv64(delta_min
, 1000000, ticks_per_sec
),
838 muldiv64(delta_max
, 1000000, ticks_per_sec
),
839 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
840 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
842 delta_min
= INT64_MAX
;
850 if (qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
851 qemu_get_clock(vm_clock
)) ||
852 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
853 qemu_get_clock(rt_clock
))) {
854 /* stop the cpu because a timer occured */
855 cpu_interrupt(global_env
, CPU_INTERRUPT_EXIT
);
861 #if defined(__linux__)
863 #define RTC_FREQ 1024
867 static int start_rtc_timer(void)
869 rtc_fd
= open("/dev/rtc", O_RDONLY
);
872 if (ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
873 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
874 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
875 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
878 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
883 pit_min_timer_count
= PIT_FREQ
/ RTC_FREQ
;
889 static int start_rtc_timer(void)
894 #endif /* !defined(__linux__) */
896 #endif /* !defined(_WIN32) */
898 static void init_timers(void)
900 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
901 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
906 timerID
= timeSetEvent(10, // interval (ms)
908 host_alarm_handler
, // function
909 (DWORD
)&count
, // user parameter
910 TIME_PERIODIC
| TIME_CALLBACK_FUNCTION
);
912 perror("failed timer alarm");
916 pit_min_timer_count
= ((uint64_t)10000 * PIT_FREQ
) / 1000000;
919 struct sigaction act
;
920 struct itimerval itv
;
922 /* get times() syscall frequency */
923 timer_freq
= sysconf(_SC_CLK_TCK
);
926 sigfillset(&act
.sa_mask
);
928 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
929 act
.sa_flags
|= SA_ONSTACK
;
931 act
.sa_handler
= host_alarm_handler
;
932 sigaction(SIGALRM
, &act
, NULL
);
934 itv
.it_interval
.tv_sec
= 0;
935 itv
.it_interval
.tv_usec
= 999; /* for i386 kernel 2.6 to get 1 ms */
936 itv
.it_value
.tv_sec
= 0;
937 itv
.it_value
.tv_usec
= 10 * 1000;
938 setitimer(ITIMER_REAL
, &itv
, NULL
);
939 /* we probe the tick duration of the kernel to inform the user if
940 the emulated kernel requested a too high timer frequency */
941 getitimer(ITIMER_REAL
, &itv
);
943 #if defined(__linux__)
944 if (itv
.it_interval
.tv_usec
> 1000) {
945 /* try to use /dev/rtc to have a faster timer */
946 if (start_rtc_timer() < 0)
949 itv
.it_interval
.tv_sec
= 0;
950 itv
.it_interval
.tv_usec
= 0;
951 itv
.it_value
.tv_sec
= 0;
952 itv
.it_value
.tv_usec
= 0;
953 setitimer(ITIMER_REAL
, &itv
, NULL
);
956 sigaction(SIGIO
, &act
, NULL
);
957 fcntl(rtc_fd
, F_SETFL
, O_ASYNC
);
958 fcntl(rtc_fd
, F_SETOWN
, getpid());
960 #endif /* defined(__linux__) */
963 pit_min_timer_count
= ((uint64_t)itv
.it_interval
.tv_usec
*
970 void quit_timers(void)
973 timeKillEvent(timerID
);
977 /***********************************************************/
978 /* character device */
980 int qemu_chr_write(CharDriverState
*s
, const uint8_t *buf
, int len
)
982 return s
->chr_write(s
, buf
, len
);
985 void qemu_chr_printf(CharDriverState
*s
, const char *fmt
, ...)
990 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
991 qemu_chr_write(s
, buf
, strlen(buf
));
995 void qemu_chr_send_event(CharDriverState
*s
, int event
)
997 if (s
->chr_send_event
)
998 s
->chr_send_event(s
, event
);
1001 void qemu_chr_add_read_handler(CharDriverState
*s
,
1002 IOCanRWHandler
*fd_can_read
,
1003 IOReadHandler
*fd_read
, void *opaque
)
1005 s
->chr_add_read_handler(s
, fd_can_read
, fd_read
, opaque
);
1008 void qemu_chr_add_event_handler(CharDriverState
*s
, IOEventHandler
*chr_event
)
1010 s
->chr_event
= chr_event
;
1013 static int null_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1018 static void null_chr_add_read_handler(CharDriverState
*chr
,
1019 IOCanRWHandler
*fd_can_read
,
1020 IOReadHandler
*fd_read
, void *opaque
)
1024 CharDriverState
*qemu_chr_open_null(void)
1026 CharDriverState
*chr
;
1028 chr
= qemu_mallocz(sizeof(CharDriverState
));
1031 chr
->chr_write
= null_chr_write
;
1032 chr
->chr_add_read_handler
= null_chr_add_read_handler
;
1040 /* for nographic stdio only */
1041 IOCanRWHandler
*fd_can_read
;
1042 IOReadHandler
*fd_read
;
1046 #define STDIO_MAX_CLIENTS 2
1048 static int stdio_nb_clients
;
1049 static CharDriverState
*stdio_clients
[STDIO_MAX_CLIENTS
];
1051 static int unix_write(int fd
, const uint8_t *buf
, int len1
)
1057 ret
= write(fd
, buf
, len
);
1059 if (errno
!= EINTR
&& errno
!= EAGAIN
)
1061 } else if (ret
== 0) {
1071 static int fd_chr_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1073 FDCharDriver
*s
= chr
->opaque
;
1074 return unix_write(s
->fd_out
, buf
, len
);
1077 static void fd_chr_add_read_handler(CharDriverState
*chr
,
1078 IOCanRWHandler
*fd_can_read
,
1079 IOReadHandler
*fd_read
, void *opaque
)
1081 FDCharDriver
*s
= chr
->opaque
;
1083 if (nographic
&& s
->fd_in
== 0) {
1084 s
->fd_can_read
= fd_can_read
;
1085 s
->fd_read
= fd_read
;
1086 s
->fd_opaque
= opaque
;
1088 qemu_add_fd_read_handler(s
->fd_in
, fd_can_read
, fd_read
, opaque
);
1092 /* open a character device to a unix fd */
1093 CharDriverState
*qemu_chr_open_fd(int fd_in
, int fd_out
)
1095 CharDriverState
*chr
;
1098 chr
= qemu_mallocz(sizeof(CharDriverState
));
1101 s
= qemu_mallocz(sizeof(FDCharDriver
));
1109 chr
->chr_write
= fd_chr_write
;
1110 chr
->chr_add_read_handler
= fd_chr_add_read_handler
;
1114 /* for STDIO, we handle the case where several clients use it
1117 #define TERM_ESCAPE 0x01 /* ctrl-a is used for escape */
1119 static int term_got_escape
, client_index
;
1121 void term_print_help(void)
1124 "C-a h print this help\n"
1125 "C-a x exit emulator\n"
1126 "C-a s save disk data back to file (if -snapshot)\n"
1127 "C-a b send break (magic sysrq)\n"
1128 "C-a c switch between console and monitor\n"
1129 "C-a C-a send C-a\n"
1133 /* called when a char is received */
1134 static void stdio_received_byte(int ch
)
1136 if (term_got_escape
) {
1137 term_got_escape
= 0;
1148 for (i
= 0; i
< MAX_DISKS
; i
++) {
1150 bdrv_commit(bs_table
[i
]);
1155 if (client_index
< stdio_nb_clients
) {
1156 CharDriverState
*chr
;
1159 chr
= stdio_clients
[client_index
];
1161 chr
->chr_event(s
->fd_opaque
, CHR_EVENT_BREAK
);
1166 if (client_index
>= stdio_nb_clients
)
1168 if (client_index
== 0) {
1169 /* send a new line in the monitor to get the prompt */
1177 } else if (ch
== TERM_ESCAPE
) {
1178 term_got_escape
= 1;
1181 if (client_index
< stdio_nb_clients
) {
1183 CharDriverState
*chr
;
1186 chr
= stdio_clients
[client_index
];
1189 /* XXX: should queue the char if the device is not
1191 if (s
->fd_can_read(s
->fd_opaque
) > 0)
1192 s
->fd_read(s
->fd_opaque
, buf
, 1);
1197 static int stdio_can_read(void *opaque
)
1199 /* XXX: not strictly correct */
1203 static void stdio_read(void *opaque
, const uint8_t *buf
, int size
)
1206 for(i
= 0; i
< size
; i
++)
1207 stdio_received_byte(buf
[i
]);
1210 /* init terminal so that we can grab keys */
1211 static struct termios oldtty
;
1212 static int old_fd0_flags
;
1214 static void term_exit(void)
1216 tcsetattr (0, TCSANOW
, &oldtty
);
1217 fcntl(0, F_SETFL
, old_fd0_flags
);
1220 static void term_init(void)
1224 tcgetattr (0, &tty
);
1226 old_fd0_flags
= fcntl(0, F_GETFL
);
1228 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
1229 |INLCR
|IGNCR
|ICRNL
|IXON
);
1230 tty
.c_oflag
|= OPOST
;
1231 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
1232 /* if graphical mode, we allow Ctrl-C handling */
1234 tty
.c_lflag
&= ~ISIG
;
1235 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
1238 tty
.c_cc
[VTIME
] = 0;
1240 tcsetattr (0, TCSANOW
, &tty
);
1244 fcntl(0, F_SETFL
, O_NONBLOCK
);
1247 CharDriverState
*qemu_chr_open_stdio(void)
1249 CharDriverState
*chr
;
1252 if (stdio_nb_clients
>= STDIO_MAX_CLIENTS
)
1254 chr
= qemu_chr_open_fd(0, 1);
1255 if (stdio_nb_clients
== 0)
1256 qemu_add_fd_read_handler(0, stdio_can_read
, stdio_read
, NULL
);
1257 client_index
= stdio_nb_clients
;
1259 if (stdio_nb_clients
!= 0)
1261 chr
= qemu_chr_open_fd(0, 1);
1263 stdio_clients
[stdio_nb_clients
++] = chr
;
1264 if (stdio_nb_clients
== 1) {
1265 /* set the terminal in raw mode */
1271 #if defined(__linux__)
1272 CharDriverState
*qemu_chr_open_pty(void)
1274 char slave_name
[1024];
1275 int master_fd
, slave_fd
;
1277 /* Not satisfying */
1278 if (openpty(&master_fd
, &slave_fd
, slave_name
, NULL
, NULL
) < 0) {
1281 fprintf(stderr
, "char device redirected to %s\n", slave_name
);
1282 return qemu_chr_open_fd(master_fd
, master_fd
);
1285 CharDriverState
*qemu_chr_open_pty(void)
1291 #endif /* !defined(_WIN32) */
1293 CharDriverState
*qemu_chr_open(const char *filename
)
1295 if (!strcmp(filename
, "vc")) {
1296 return text_console_init(&display_state
);
1297 } else if (!strcmp(filename
, "null")) {
1298 return qemu_chr_open_null();
1301 if (!strcmp(filename
, "pty")) {
1302 return qemu_chr_open_pty();
1303 } else if (!strcmp(filename
, "stdio")) {
1304 return qemu_chr_open_stdio();
1312 /***********************************************************/
1313 /* Linux network device redirectors */
1315 void hex_dump(FILE *f
, const uint8_t *buf
, int size
)
1319 for(i
=0;i
<size
;i
+=16) {
1323 fprintf(f
, "%08x ", i
);
1326 fprintf(f
, " %02x", buf
[i
+j
]);
1331 for(j
=0;j
<len
;j
++) {
1333 if (c
< ' ' || c
> '~')
1335 fprintf(f
, "%c", c
);
1341 void qemu_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1343 nd
->send_packet(nd
, buf
, size
);
1346 void qemu_add_read_packet(NetDriverState
*nd
, IOCanRWHandler
*fd_can_read
,
1347 IOReadHandler
*fd_read
, void *opaque
)
1349 nd
->add_read_packet(nd
, fd_can_read
, fd_read
, opaque
);
1352 /* dummy network adapter */
1354 static void dummy_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1358 static void dummy_add_read_packet(NetDriverState
*nd
,
1359 IOCanRWHandler
*fd_can_read
,
1360 IOReadHandler
*fd_read
, void *opaque
)
1364 static int net_dummy_init(NetDriverState
*nd
)
1366 nd
->send_packet
= dummy_send_packet
;
1367 nd
->add_read_packet
= dummy_add_read_packet
;
1368 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "dummy");
1372 #if defined(CONFIG_SLIRP)
1374 /* slirp network adapter */
1376 static void *slirp_fd_opaque
;
1377 static IOCanRWHandler
*slirp_fd_can_read
;
1378 static IOReadHandler
*slirp_fd_read
;
1379 static int slirp_inited
;
1381 int slirp_can_output(void)
1383 return slirp_fd_can_read(slirp_fd_opaque
);
1386 void slirp_output(const uint8_t *pkt
, int pkt_len
)
1389 printf("output:\n");
1390 hex_dump(stdout
, pkt
, pkt_len
);
1392 slirp_fd_read(slirp_fd_opaque
, pkt
, pkt_len
);
1395 static void slirp_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1399 hex_dump(stdout
, buf
, size
);
1401 slirp_input(buf
, size
);
1404 static void slirp_add_read_packet(NetDriverState
*nd
,
1405 IOCanRWHandler
*fd_can_read
,
1406 IOReadHandler
*fd_read
, void *opaque
)
1408 slirp_fd_opaque
= opaque
;
1409 slirp_fd_can_read
= fd_can_read
;
1410 slirp_fd_read
= fd_read
;
1413 static int net_slirp_init(NetDriverState
*nd
)
1415 if (!slirp_inited
) {
1419 nd
->send_packet
= slirp_send_packet
;
1420 nd
->add_read_packet
= slirp_add_read_packet
;
1421 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "slirp");
1425 static int get_str_sep(char *buf
, int buf_size
, const char **pp
, int sep
)
1430 p1
= strchr(p
, sep
);
1436 if (len
> buf_size
- 1)
1438 memcpy(buf
, p
, len
);
1445 static void net_slirp_redir(const char *redir_str
)
1450 struct in_addr guest_addr
;
1451 int host_port
, guest_port
;
1453 if (!slirp_inited
) {
1459 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1461 if (!strcmp(buf
, "tcp")) {
1463 } else if (!strcmp(buf
, "udp")) {
1469 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1471 host_port
= strtol(buf
, &r
, 0);
1475 if (get_str_sep(buf
, sizeof(buf
), &p
, ':') < 0)
1477 if (buf
[0] == '\0') {
1478 pstrcpy(buf
, sizeof(buf
), "10.0.2.15");
1480 if (!inet_aton(buf
, &guest_addr
))
1483 guest_port
= strtol(p
, &r
, 0);
1487 if (slirp_redir(is_udp
, host_port
, guest_addr
, guest_port
) < 0) {
1488 fprintf(stderr
, "qemu: could not set up redirection\n");
1493 fprintf(stderr
, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
1501 static void smb_exit(void)
1505 char filename
[1024];
1507 /* erase all the files in the directory */
1508 d
= opendir(smb_dir
);
1513 if (strcmp(de
->d_name
, ".") != 0 &&
1514 strcmp(de
->d_name
, "..") != 0) {
1515 snprintf(filename
, sizeof(filename
), "%s/%s",
1516 smb_dir
, de
->d_name
);
1524 /* automatic user mode samba server configuration */
1525 void net_slirp_smb(const char *exported_dir
)
1527 char smb_conf
[1024];
1528 char smb_cmdline
[1024];
1531 if (!slirp_inited
) {
1536 /* XXX: better tmp dir construction */
1537 snprintf(smb_dir
, sizeof(smb_dir
), "/tmp/qemu-smb.%d", getpid());
1538 if (mkdir(smb_dir
, 0700) < 0) {
1539 fprintf(stderr
, "qemu: could not create samba server dir '%s'\n", smb_dir
);
1542 snprintf(smb_conf
, sizeof(smb_conf
), "%s/%s", smb_dir
, "smb.conf");
1544 f
= fopen(smb_conf
, "w");
1546 fprintf(stderr
, "qemu: could not create samba server configuration file '%s'\n", smb_conf
);
1553 "socket address=127.0.0.1\n"
1554 "pid directory=%s\n"
1555 "lock directory=%s\n"
1556 "log file=%s/log.smbd\n"
1557 "smb passwd file=%s/smbpasswd\n"
1558 "security = share\n"
1573 snprintf(smb_cmdline
, sizeof(smb_cmdline
), "/usr/sbin/smbd -s %s",
1576 slirp_add_exec(0, smb_cmdline
, 4, 139);
1579 #endif /* !defined(_WIN32) */
1581 #endif /* CONFIG_SLIRP */
1583 #if !defined(_WIN32)
1585 static int tun_open(char *ifname
, int ifname_size
)
1591 fd
= open("/dev/tap", O_RDWR
);
1593 fprintf(stderr
, "warning: could not open /dev/tap: no virtual network emulation\n");
1598 dev
= devname(s
.st_rdev
, S_IFCHR
);
1599 pstrcpy(ifname
, ifname_size
, dev
);
1601 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1605 static int tun_open(char *ifname
, int ifname_size
)
1610 fd
= open("/dev/net/tun", O_RDWR
);
1612 fprintf(stderr
, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1615 memset(&ifr
, 0, sizeof(ifr
));
1616 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
1617 pstrcpy(ifr
.ifr_name
, IFNAMSIZ
, "tun%d");
1618 ret
= ioctl(fd
, TUNSETIFF
, (void *) &ifr
);
1620 fprintf(stderr
, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1624 printf("Connected to host network interface: %s\n", ifr
.ifr_name
);
1625 pstrcpy(ifname
, ifname_size
, ifr
.ifr_name
);
1626 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1631 static void tun_send_packet(NetDriverState
*nd
, const uint8_t *buf
, int size
)
1633 write(nd
->fd
, buf
, size
);
1636 static void tun_add_read_packet(NetDriverState
*nd
,
1637 IOCanRWHandler
*fd_can_read
,
1638 IOReadHandler
*fd_read
, void *opaque
)
1640 qemu_add_fd_read_handler(nd
->fd
, fd_can_read
, fd_read
, opaque
);
1643 static int net_tun_init(NetDriverState
*nd
)
1649 nd
->fd
= tun_open(nd
->ifname
, sizeof(nd
->ifname
));
1653 /* try to launch network init script */
1658 *parg
++ = network_script
;
1659 *parg
++ = nd
->ifname
;
1661 execv(network_script
, args
);
1664 while (waitpid(pid
, &status
, 0) != pid
);
1665 if (!WIFEXITED(status
) ||
1666 WEXITSTATUS(status
) != 0) {
1667 fprintf(stderr
, "%s: could not launch network script\n",
1671 nd
->send_packet
= tun_send_packet
;
1672 nd
->add_read_packet
= tun_add_read_packet
;
1676 static int net_fd_init(NetDriverState
*nd
, int fd
)
1679 nd
->send_packet
= tun_send_packet
;
1680 nd
->add_read_packet
= tun_add_read_packet
;
1681 pstrcpy(nd
->ifname
, sizeof(nd
->ifname
), "tunfd");
1685 #endif /* !_WIN32 */
1687 /***********************************************************/
1690 static char *pid_filename
;
1692 /* Remove PID file. Called on normal exit */
1694 static void remove_pidfile(void)
1696 unlink (pid_filename
);
1699 static void create_pidfile(const char *filename
)
1701 struct stat pidstat
;
1704 /* Try to write our PID to the named file */
1705 if (stat(filename
, &pidstat
) < 0) {
1706 if (errno
== ENOENT
) {
1707 if ((f
= fopen (filename
, "w")) == NULL
) {
1708 perror("Opening pidfile");
1711 fprintf(f
, "%d\n", getpid());
1713 pid_filename
= qemu_strdup(filename
);
1714 if (!pid_filename
) {
1715 fprintf(stderr
, "Could not save PID filename");
1718 atexit(remove_pidfile
);
1721 fprintf(stderr
, "%s already exists. Remove it and try again.\n",
1727 /***********************************************************/
1730 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
1734 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
1738 static void dumb_refresh(DisplayState
*ds
)
1740 vga_update_display();
1743 void dumb_display_init(DisplayState
*ds
)
1748 ds
->dpy_update
= dumb_update
;
1749 ds
->dpy_resize
= dumb_resize
;
1750 ds
->dpy_refresh
= dumb_refresh
;
1753 #if !defined(CONFIG_SOFTMMU)
1754 /***********************************************************/
1755 /* cpu signal handler */
1756 static void host_segv_handler(int host_signum
, siginfo_t
*info
,
1759 if (cpu_signal_handler(host_signum
, info
, puc
))
1761 if (stdio_nb_clients
> 0)
1767 /***********************************************************/
1770 #define MAX_IO_HANDLERS 64
1772 typedef struct IOHandlerRecord
{
1774 IOCanRWHandler
*fd_can_read
;
1775 IOReadHandler
*fd_read
;
1777 /* temporary data */
1780 struct IOHandlerRecord
*next
;
1783 static IOHandlerRecord
*first_io_handler
;
1785 int qemu_add_fd_read_handler(int fd
, IOCanRWHandler
*fd_can_read
,
1786 IOReadHandler
*fd_read
, void *opaque
)
1788 IOHandlerRecord
*ioh
;
1790 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
1794 ioh
->fd_can_read
= fd_can_read
;
1795 ioh
->fd_read
= fd_read
;
1796 ioh
->opaque
= opaque
;
1797 ioh
->next
= first_io_handler
;
1798 first_io_handler
= ioh
;
1802 void qemu_del_fd_read_handler(int fd
)
1804 IOHandlerRecord
**pioh
, *ioh
;
1806 pioh
= &first_io_handler
;
1811 if (ioh
->fd
== fd
) {
1819 /***********************************************************/
1820 /* savevm/loadvm support */
1822 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
1824 fwrite(buf
, 1, size
, f
);
1827 void qemu_put_byte(QEMUFile
*f
, int v
)
1832 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
1834 qemu_put_byte(f
, v
>> 8);
1835 qemu_put_byte(f
, v
);
1838 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
1840 qemu_put_byte(f
, v
>> 24);
1841 qemu_put_byte(f
, v
>> 16);
1842 qemu_put_byte(f
, v
>> 8);
1843 qemu_put_byte(f
, v
);
1846 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
1848 qemu_put_be32(f
, v
>> 32);
1849 qemu_put_be32(f
, v
);
1852 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
1854 return fread(buf
, 1, size
, f
);
1857 int qemu_get_byte(QEMUFile
*f
)
1867 unsigned int qemu_get_be16(QEMUFile
*f
)
1870 v
= qemu_get_byte(f
) << 8;
1871 v
|= qemu_get_byte(f
);
1875 unsigned int qemu_get_be32(QEMUFile
*f
)
1878 v
= qemu_get_byte(f
) << 24;
1879 v
|= qemu_get_byte(f
) << 16;
1880 v
|= qemu_get_byte(f
) << 8;
1881 v
|= qemu_get_byte(f
);
1885 uint64_t qemu_get_be64(QEMUFile
*f
)
1888 v
= (uint64_t)qemu_get_be32(f
) << 32;
1889 v
|= qemu_get_be32(f
);
1893 int64_t qemu_ftell(QEMUFile
*f
)
1898 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
1900 if (fseek(f
, pos
, whence
) < 0)
1905 typedef struct SaveStateEntry
{
1909 SaveStateHandler
*save_state
;
1910 LoadStateHandler
*load_state
;
1912 struct SaveStateEntry
*next
;
1915 static SaveStateEntry
*first_se
;
1917 int register_savevm(const char *idstr
,
1920 SaveStateHandler
*save_state
,
1921 LoadStateHandler
*load_state
,
1924 SaveStateEntry
*se
, **pse
;
1926 se
= qemu_malloc(sizeof(SaveStateEntry
));
1929 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1930 se
->instance_id
= instance_id
;
1931 se
->version_id
= version_id
;
1932 se
->save_state
= save_state
;
1933 se
->load_state
= load_state
;
1934 se
->opaque
= opaque
;
1937 /* add at the end of list */
1939 while (*pse
!= NULL
)
1940 pse
= &(*pse
)->next
;
1945 #define QEMU_VM_FILE_MAGIC 0x5145564d
1946 #define QEMU_VM_FILE_VERSION 0x00000001
1948 int qemu_savevm(const char *filename
)
1952 int len
, len_pos
, cur_pos
, saved_vm_running
, ret
;
1954 saved_vm_running
= vm_running
;
1957 f
= fopen(filename
, "wb");
1963 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1964 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1966 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
1968 len
= strlen(se
->idstr
);
1969 qemu_put_byte(f
, len
);
1970 qemu_put_buffer(f
, se
->idstr
, len
);
1972 qemu_put_be32(f
, se
->instance_id
);
1973 qemu_put_be32(f
, se
->version_id
);
1975 /* record size: filled later */
1977 qemu_put_be32(f
, 0);
1979 se
->save_state(f
, se
->opaque
);
1981 /* fill record size */
1983 len
= ftell(f
) - len_pos
- 4;
1984 fseek(f
, len_pos
, SEEK_SET
);
1985 qemu_put_be32(f
, len
);
1986 fseek(f
, cur_pos
, SEEK_SET
);
1992 if (saved_vm_running
)
1997 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
2001 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
2002 if (!strcmp(se
->idstr
, idstr
) &&
2003 instance_id
== se
->instance_id
)
2009 int qemu_loadvm(const char *filename
)
2013 int len
, cur_pos
, ret
, instance_id
, record_len
, version_id
;
2014 int saved_vm_running
;
2018 saved_vm_running
= vm_running
;
2021 f
= fopen(filename
, "rb");
2027 v
= qemu_get_be32(f
);
2028 if (v
!= QEMU_VM_FILE_MAGIC
)
2030 v
= qemu_get_be32(f
);
2031 if (v
!= QEMU_VM_FILE_VERSION
) {
2038 #if defined (DO_TB_FLUSH)
2039 tb_flush(global_env
);
2041 len
= qemu_get_byte(f
);
2044 qemu_get_buffer(f
, idstr
, len
);
2046 instance_id
= qemu_get_be32(f
);
2047 version_id
= qemu_get_be32(f
);
2048 record_len
= qemu_get_be32(f
);
2050 printf("idstr=%s instance=0x%x version=%d len=%d\n",
2051 idstr
, instance_id
, version_id
, record_len
);
2054 se
= find_se(idstr
, instance_id
);
2056 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
2057 instance_id
, idstr
);
2059 ret
= se
->load_state(f
, se
->opaque
, version_id
);
2061 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2062 instance_id
, idstr
);
2065 /* always seek to exact end of record */
2066 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
2071 if (saved_vm_running
)
2076 /***********************************************************/
2077 /* cpu save/restore */
2079 #if defined(TARGET_I386)
2081 static void cpu_put_seg(QEMUFile
*f
, SegmentCache
*dt
)
2083 qemu_put_be32(f
, dt
->selector
);
2084 qemu_put_betl(f
, dt
->base
);
2085 qemu_put_be32(f
, dt
->limit
);
2086 qemu_put_be32(f
, dt
->flags
);
2089 static void cpu_get_seg(QEMUFile
*f
, SegmentCache
*dt
)
2091 dt
->selector
= qemu_get_be32(f
);
2092 dt
->base
= qemu_get_betl(f
);
2093 dt
->limit
= qemu_get_be32(f
);
2094 dt
->flags
= qemu_get_be32(f
);
2097 void cpu_save(QEMUFile
*f
, void *opaque
)
2099 CPUState
*env
= opaque
;
2100 uint16_t fptag
, fpus
, fpuc
, fpregs_format
;
2104 for(i
= 0; i
< CPU_NB_REGS
; i
++)
2105 qemu_put_betls(f
, &env
->regs
[i
]);
2106 qemu_put_betls(f
, &env
->eip
);
2107 qemu_put_betls(f
, &env
->eflags
);
2108 hflags
= env
->hflags
; /* XXX: suppress most of the redundant hflags */
2109 qemu_put_be32s(f
, &hflags
);
2113 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2115 for(i
= 0; i
< 8; i
++) {
2116 fptag
|= ((!env
->fptags
[i
]) << i
);
2119 qemu_put_be16s(f
, &fpuc
);
2120 qemu_put_be16s(f
, &fpus
);
2121 qemu_put_be16s(f
, &fptag
);
2123 #ifdef USE_X86LDOUBLE
2128 qemu_put_be16s(f
, &fpregs_format
);
2130 for(i
= 0; i
< 8; i
++) {
2131 #ifdef USE_X86LDOUBLE
2135 /* we save the real CPU data (in case of MMX usage only 'mant'
2136 contains the MMX register */
2137 cpu_get_fp80(&mant
, &exp
, env
->fpregs
[i
].d
);
2138 qemu_put_be64(f
, mant
);
2139 qemu_put_be16(f
, exp
);
2142 /* if we use doubles for float emulation, we save the doubles to
2143 avoid losing information in case of MMX usage. It can give
2144 problems if the image is restored on a CPU where long
2145 doubles are used instead. */
2146 qemu_put_be64(f
, env
->fpregs
[i
].mmx
.MMX_Q(0));
2150 for(i
= 0; i
< 6; i
++)
2151 cpu_put_seg(f
, &env
->segs
[i
]);
2152 cpu_put_seg(f
, &env
->ldt
);
2153 cpu_put_seg(f
, &env
->tr
);
2154 cpu_put_seg(f
, &env
->gdt
);
2155 cpu_put_seg(f
, &env
->idt
);
2157 qemu_put_be32s(f
, &env
->sysenter_cs
);
2158 qemu_put_be32s(f
, &env
->sysenter_esp
);
2159 qemu_put_be32s(f
, &env
->sysenter_eip
);
2161 qemu_put_betls(f
, &env
->cr
[0]);
2162 qemu_put_betls(f
, &env
->cr
[2]);
2163 qemu_put_betls(f
, &env
->cr
[3]);
2164 qemu_put_betls(f
, &env
->cr
[4]);
2166 for(i
= 0; i
< 8; i
++)
2167 qemu_put_betls(f
, &env
->dr
[i
]);
2170 qemu_put_be32s(f
, &env
->a20_mask
);
2173 qemu_put_be32s(f
, &env
->mxcsr
);
2174 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
2175 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
2176 qemu_put_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
2179 #ifdef TARGET_X86_64
2180 qemu_put_be64s(f
, &env
->efer
);
2181 qemu_put_be64s(f
, &env
->star
);
2182 qemu_put_be64s(f
, &env
->lstar
);
2183 qemu_put_be64s(f
, &env
->cstar
);
2184 qemu_put_be64s(f
, &env
->fmask
);
2185 qemu_put_be64s(f
, &env
->kernelgsbase
);
2189 #ifdef USE_X86LDOUBLE
2190 /* XXX: add that in a FPU generic layer */
2191 union x86_longdouble
{
2196 #define MANTD1(fp) (fp & ((1LL << 52) - 1))
2197 #define EXPBIAS1 1023
2198 #define EXPD1(fp) ((fp >> 52) & 0x7FF)
2199 #define SIGND1(fp) ((fp >> 32) & 0x80000000)
2201 static void fp64_to_fp80(union x86_longdouble
*p
, uint64_t temp
)
2205 p
->mant
= (MANTD1(temp
) << 11) | (1LL << 63);
2206 /* exponent + sign */
2207 e
= EXPD1(temp
) - EXPBIAS1
+ 16383;
2208 e
|= SIGND1(temp
) >> 16;
2213 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2215 CPUState
*env
= opaque
;
2218 uint16_t fpus
, fpuc
, fptag
, fpregs_format
;
2220 if (version_id
!= 3)
2222 for(i
= 0; i
< CPU_NB_REGS
; i
++)
2223 qemu_get_betls(f
, &env
->regs
[i
]);
2224 qemu_get_betls(f
, &env
->eip
);
2225 qemu_get_betls(f
, &env
->eflags
);
2226 qemu_get_be32s(f
, &hflags
);
2228 qemu_get_be16s(f
, &fpuc
);
2229 qemu_get_be16s(f
, &fpus
);
2230 qemu_get_be16s(f
, &fptag
);
2231 qemu_get_be16s(f
, &fpregs_format
);
2233 /* NOTE: we cannot always restore the FPU state if the image come
2234 from a host with a different 'USE_X86LDOUBLE' define. We guess
2235 if we are in an MMX state to restore correctly in that case. */
2236 guess_mmx
= ((fptag
== 0xff) && (fpus
& 0x3800) == 0);
2237 for(i
= 0; i
< 8; i
++) {
2241 switch(fpregs_format
) {
2243 mant
= qemu_get_be64(f
);
2244 exp
= qemu_get_be16(f
);
2245 #ifdef USE_X86LDOUBLE
2246 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
2248 /* difficult case */
2250 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
2252 env
->fpregs
[i
].d
= cpu_set_fp80(mant
, exp
);
2256 mant
= qemu_get_be64(f
);
2257 #ifdef USE_X86LDOUBLE
2259 union x86_longdouble
*p
;
2260 /* difficult case */
2261 p
= (void *)&env
->fpregs
[i
];
2266 fp64_to_fp80(p
, mant
);
2270 env
->fpregs
[i
].mmx
.MMX_Q(0) = mant
;
2279 /* XXX: restore FPU round state */
2280 env
->fpstt
= (fpus
>> 11) & 7;
2281 env
->fpus
= fpus
& ~0x3800;
2283 for(i
= 0; i
< 8; i
++) {
2284 env
->fptags
[i
] = (fptag
>> i
) & 1;
2287 for(i
= 0; i
< 6; i
++)
2288 cpu_get_seg(f
, &env
->segs
[i
]);
2289 cpu_get_seg(f
, &env
->ldt
);
2290 cpu_get_seg(f
, &env
->tr
);
2291 cpu_get_seg(f
, &env
->gdt
);
2292 cpu_get_seg(f
, &env
->idt
);
2294 qemu_get_be32s(f
, &env
->sysenter_cs
);
2295 qemu_get_be32s(f
, &env
->sysenter_esp
);
2296 qemu_get_be32s(f
, &env
->sysenter_eip
);
2298 qemu_get_betls(f
, &env
->cr
[0]);
2299 qemu_get_betls(f
, &env
->cr
[2]);
2300 qemu_get_betls(f
, &env
->cr
[3]);
2301 qemu_get_betls(f
, &env
->cr
[4]);
2303 for(i
= 0; i
< 8; i
++)
2304 qemu_get_betls(f
, &env
->dr
[i
]);
2307 qemu_get_be32s(f
, &env
->a20_mask
);
2309 qemu_get_be32s(f
, &env
->mxcsr
);
2310 for(i
= 0; i
< CPU_NB_REGS
; i
++) {
2311 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(0));
2312 qemu_get_be64s(f
, &env
->xmm_regs
[i
].XMM_Q(1));
2315 #ifdef TARGET_X86_64
2316 qemu_get_be64s(f
, &env
->efer
);
2317 qemu_get_be64s(f
, &env
->star
);
2318 qemu_get_be64s(f
, &env
->lstar
);
2319 qemu_get_be64s(f
, &env
->cstar
);
2320 qemu_get_be64s(f
, &env
->fmask
);
2321 qemu_get_be64s(f
, &env
->kernelgsbase
);
2324 /* XXX: compute hflags from scratch, except for CPL and IIF */
2325 env
->hflags
= hflags
;
2330 #elif defined(TARGET_PPC)
2331 void cpu_save(QEMUFile
*f
, void *opaque
)
2335 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2339 #elif defined(TARGET_SPARC)
2340 void cpu_save(QEMUFile
*f
, void *opaque
)
2342 CPUState
*env
= opaque
;
2346 for(i
= 0; i
< 8; i
++)
2347 qemu_put_betls(f
, &env
->gregs
[i
]);
2348 for(i
= 0; i
< NWINDOWS
* 16; i
++)
2349 qemu_put_betls(f
, &env
->regbase
[i
]);
2352 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
2358 qemu_put_betl(f
, u
.i
);
2361 qemu_put_betls(f
, &env
->pc
);
2362 qemu_put_betls(f
, &env
->npc
);
2363 qemu_put_betls(f
, &env
->y
);
2365 qemu_put_be32(f
, tmp
);
2366 qemu_put_be32s(f
, &env
->fsr
);
2367 qemu_put_be32s(f
, &env
->wim
);
2368 qemu_put_be32s(f
, &env
->tbr
);
2370 for(i
= 0; i
< 16; i
++)
2371 qemu_put_be32s(f
, &env
->mmuregs
[i
]);
2374 int cpu_load(QEMUFile
*f
, void *opaque
, int version_id
)
2376 CPUState
*env
= opaque
;
2380 for(i
= 0; i
< 8; i
++)
2381 qemu_get_betls(f
, &env
->gregs
[i
]);
2382 for(i
= 0; i
< NWINDOWS
* 16; i
++)
2383 qemu_get_betls(f
, &env
->regbase
[i
]);
2386 for(i
= 0; i
< TARGET_FPREGS
; i
++) {
2391 u
.i
= qemu_get_betl(f
);
2395 qemu_get_betls(f
, &env
->pc
);
2396 qemu_get_betls(f
, &env
->npc
);
2397 qemu_get_betls(f
, &env
->y
);
2398 tmp
= qemu_get_be32(f
);
2399 env
->cwp
= 0; /* needed to ensure that the wrapping registers are
2400 correctly updated */
2402 qemu_get_be32s(f
, &env
->fsr
);
2403 qemu_get_be32s(f
, &env
->wim
);
2404 qemu_get_be32s(f
, &env
->tbr
);
2406 for(i
= 0; i
< 16; i
++)
2407 qemu_get_be32s(f
, &env
->mmuregs
[i
]);
2414 #warning No CPU save/restore functions
2418 /***********************************************************/
2419 /* ram save/restore */
2421 /* we just avoid storing empty pages */
2422 static void ram_put_page(QEMUFile
*f
, const uint8_t *buf
, int len
)
2427 for(i
= 1; i
< len
; i
++) {
2431 qemu_put_byte(f
, 1);
2432 qemu_put_byte(f
, v
);
2435 qemu_put_byte(f
, 0);
2436 qemu_put_buffer(f
, buf
, len
);
2439 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2443 v
= qemu_get_byte(f
);
2446 if (qemu_get_buffer(f
, buf
, len
) != len
)
2450 v
= qemu_get_byte(f
);
2451 memset(buf
, v
, len
);
2459 static void ram_save(QEMUFile
*f
, void *opaque
)
2462 qemu_put_be32(f
, phys_ram_size
);
2463 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2464 ram_put_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2468 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2472 if (version_id
!= 1)
2474 if (qemu_get_be32(f
) != phys_ram_size
)
2476 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2477 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2484 /***********************************************************/
2485 /* main execution loop */
2487 void gui_update(void *opaque
)
2489 display_state
.dpy_refresh(&display_state
);
2490 qemu_mod_timer(gui_timer
, GUI_REFRESH_INTERVAL
+ qemu_get_clock(rt_clock
));
2493 /* XXX: support several handlers */
2494 VMStopHandler
*vm_stop_cb
;
2495 VMStopHandler
*vm_stop_opaque
;
2497 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
2500 vm_stop_opaque
= opaque
;
2504 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
2517 void vm_stop(int reason
)
2520 cpu_disable_ticks();
2524 vm_stop_cb(vm_stop_opaque
, reason
);
2530 /* reset/shutdown handler */
2532 typedef struct QEMUResetEntry
{
2533 QEMUResetHandler
*func
;
2535 struct QEMUResetEntry
*next
;
2538 static QEMUResetEntry
*first_reset_entry
;
2539 static int reset_requested
;
2540 static int shutdown_requested
;
2542 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
2544 QEMUResetEntry
**pre
, *re
;
2546 pre
= &first_reset_entry
;
2547 while (*pre
!= NULL
)
2548 pre
= &(*pre
)->next
;
2549 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
2551 re
->opaque
= opaque
;
2556 void qemu_system_reset(void)
2560 /* reset all devices */
2561 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
2562 re
->func(re
->opaque
);
2566 void qemu_system_reset_request(void)
2568 reset_requested
= 1;
2569 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
2572 void qemu_system_shutdown_request(void)
2574 shutdown_requested
= 1;
2575 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
2578 static void main_cpu_reset(void *opaque
)
2580 #if defined(TARGET_I386) || defined(TARGET_SPARC)
2581 CPUState
*env
= opaque
;
2586 void main_loop_wait(int timeout
)
2589 struct pollfd ufds
[MAX_IO_HANDLERS
+ 1], *pf
;
2590 IOHandlerRecord
*ioh
, *ioh_next
;
2600 /* poll any events */
2601 /* XXX: separate device handlers from system ones */
2603 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2604 if (!ioh
->fd_can_read
) {
2607 pf
->events
= POLLIN
;
2611 max_size
= ioh
->fd_can_read(ioh
->opaque
);
2613 if (max_size
> sizeof(buf
))
2614 max_size
= sizeof(buf
);
2616 pf
->events
= POLLIN
;
2623 ioh
->max_size
= max_size
;
2626 ret
= poll(ufds
, pf
- ufds
, timeout
);
2628 /* XXX: better handling of removal */
2629 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh_next
) {
2630 ioh_next
= ioh
->next
;
2633 if (pf
->revents
& POLLIN
) {
2634 if (ioh
->max_size
== 0) {
2635 /* just a read event */
2636 ioh
->fd_read(ioh
->opaque
, NULL
, 0);
2638 n
= read(ioh
->fd
, buf
, ioh
->max_size
);
2640 ioh
->fd_read(ioh
->opaque
, buf
, n
);
2641 } else if (errno
!= EAGAIN
) {
2642 ioh
->fd_read(ioh
->opaque
, NULL
, -errno
);
2649 #endif /* !defined(_WIN32) */
2650 #if defined(CONFIG_SLIRP)
2651 /* XXX: merge with poll() */
2653 fd_set rfds
, wfds
, xfds
;
2661 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
2664 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
2666 slirp_select_poll(&rfds
, &wfds
, &xfds
);
2672 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
2673 qemu_get_clock(vm_clock
));
2674 /* run dma transfers, if any */
2678 /* real time timers */
2679 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
2680 qemu_get_clock(rt_clock
));
2686 CPUState
*env
= global_env
;
2690 ret
= cpu_exec(env
);
2691 if (shutdown_requested
) {
2692 ret
= EXCP_INTERRUPT
;
2695 if (reset_requested
) {
2696 reset_requested
= 0;
2697 qemu_system_reset();
2698 ret
= EXCP_INTERRUPT
;
2700 if (ret
== EXCP_DEBUG
) {
2701 vm_stop(EXCP_DEBUG
);
2703 /* if hlt instruction, we wait until the next IRQ */
2704 /* XXX: use timeout computed from timers */
2705 if (ret
== EXCP_HLT
)
2712 main_loop_wait(timeout
);
2714 cpu_disable_ticks();
2720 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2004 Fabrice Bellard\n"
2721 "usage: %s [options] [disk_image]\n"
2723 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
2725 "Standard options:\n"
2726 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
2727 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
2728 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
2729 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
2730 "-boot [a|c|d] boot on floppy (a), hard disk (c) or CD-ROM (d)\n"
2731 "-snapshot write to temporary files instead of disk image files\n"
2732 "-m megs set virtual RAM size to megs MB [default=%d]\n"
2733 "-nographic disable graphical output and redirect serial I/Os to console\n"
2735 "-k language use keyboard layout (for example \"fr\" for French)\n"
2737 "-enable-audio enable audio support\n"
2738 "-localtime set the real time clock to local time [default=utc]\n"
2739 "-full-screen start in full screen\n"
2741 "-prep Simulate a PREP system (default is PowerMAC)\n"
2743 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
2744 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
2747 "Network options:\n"
2748 "-nics n simulate 'n' network cards [default=1]\n"
2749 "-macaddr addr set the mac address of the first interface\n"
2750 "-n script set tap/tun network init script [default=%s]\n"
2751 "-tun-fd fd use this fd as already opened tap/tun interface\n"
2753 "-user-net use user mode network stack [default if no tap/tun script]\n"
2754 "-tftp prefix allow tftp access to files starting with prefix [-user-net]\n"
2756 "-smb dir allow SMB access to files in 'dir' [-user-net]\n"
2758 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
2759 " redirect TCP or UDP connections from host to guest [-user-net]\n"
2761 "-dummy-net use dummy network stack\n"
2763 "Linux boot specific:\n"
2764 "-kernel bzImage use 'bzImage' as kernel image\n"
2765 "-append cmdline use 'cmdline' as kernel command line\n"
2766 "-initrd file use 'file' as initial ram disk\n"
2768 "Debug/Expert options:\n"
2769 "-monitor dev redirect the monitor to char device 'dev'\n"
2770 "-serial dev redirect the serial port to char device 'dev'\n"
2771 "-parallel dev redirect the parallel port to char device 'dev'\n"
2772 "-pidfile file Write PID to 'file'\n"
2773 "-S freeze CPU at startup (use 'c' to start execution)\n"
2774 "-s wait gdb connection to port %d\n"
2775 "-p port change gdb connection port\n"
2776 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
2777 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
2778 " translation (t=none or lba) (usually qemu can guess them)\n"
2779 "-L path set the directory for the BIOS and VGA BIOS\n"
2781 "-no-kqemu disable KQEMU kernel module usage\n"
2783 #ifdef USE_CODE_COPY
2784 "-no-code-copy disable code copy acceleration\n"
2787 "-isa simulate an ISA-only system (default is PCI system)\n"
2788 "-std-vga simulate a standard VGA card with VESA Bochs Extensions\n"
2789 " (default is CL-GD5446 PCI VGA)\n"
2791 "-loadvm file start right away with a saved state (loadvm in monitor)\n"
2793 "During emulation, the following keys are useful:\n"
2794 "ctrl-alt-f toggle full screen\n"
2795 "ctrl-alt-n switch to virtual console 'n'\n"
2796 "ctrl-alt toggle mouse and keyboard grab\n"
2798 "When using -nographic, press 'ctrl-a h' to get some help.\n"
2800 #ifdef CONFIG_SOFTMMU
2806 DEFAULT_NETWORK_SCRIPT
,
2807 DEFAULT_GDBSTUB_PORT
,
2809 #ifndef CONFIG_SOFTMMU
2811 "NOTE: this version of QEMU is faster but it needs slightly patched OSes to\n"
2812 "work. Please use the 'qemu' executable to have a more accurate (but slower)\n"
2818 #define HAS_ARG 0x0001
2831 QEMU_OPTION_snapshot
,
2833 QEMU_OPTION_nographic
,
2834 QEMU_OPTION_enable_audio
,
2837 QEMU_OPTION_macaddr
,
2840 QEMU_OPTION_user_net
,
2844 QEMU_OPTION_dummy_net
,
2856 QEMU_OPTION_no_code_copy
,
2861 QEMU_OPTION_localtime
,
2862 QEMU_OPTION_cirrusvga
,
2864 QEMU_OPTION_std_vga
,
2865 QEMU_OPTION_monitor
,
2867 QEMU_OPTION_parallel
,
2869 QEMU_OPTION_full_screen
,
2870 QEMU_OPTION_pidfile
,
2871 QEMU_OPTION_no_kqemu
,
2874 typedef struct QEMUOption
{
2880 const QEMUOption qemu_options
[] = {
2881 { "h", 0, QEMU_OPTION_h
},
2883 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
2884 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
2885 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
2886 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
2887 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
2888 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
2889 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
2890 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
2891 { "snapshot", 0, QEMU_OPTION_snapshot
},
2892 { "m", HAS_ARG
, QEMU_OPTION_m
},
2893 { "nographic", 0, QEMU_OPTION_nographic
},
2894 { "k", HAS_ARG
, QEMU_OPTION_k
},
2895 { "enable-audio", 0, QEMU_OPTION_enable_audio
},
2897 { "nics", HAS_ARG
, QEMU_OPTION_nics
},
2898 { "macaddr", HAS_ARG
, QEMU_OPTION_macaddr
},
2899 { "n", HAS_ARG
, QEMU_OPTION_n
},
2900 { "tun-fd", HAS_ARG
, QEMU_OPTION_tun_fd
},
2902 { "user-net", 0, QEMU_OPTION_user_net
},
2903 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
2905 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
2907 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
2909 { "dummy-net", 0, QEMU_OPTION_dummy_net
},
2911 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
2912 { "append", HAS_ARG
, QEMU_OPTION_append
},
2913 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
2915 { "S", 0, QEMU_OPTION_S
},
2916 { "s", 0, QEMU_OPTION_s
},
2917 { "p", HAS_ARG
, QEMU_OPTION_p
},
2918 { "d", HAS_ARG
, QEMU_OPTION_d
},
2919 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
2920 { "L", HAS_ARG
, QEMU_OPTION_L
},
2921 { "no-code-copy", 0, QEMU_OPTION_no_code_copy
},
2923 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
2926 { "prep", 0, QEMU_OPTION_prep
},
2928 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
2929 { "g", 1, QEMU_OPTION_g
},
2931 { "localtime", 0, QEMU_OPTION_localtime
},
2932 { "isa", 0, QEMU_OPTION_isa
},
2933 { "std-vga", 0, QEMU_OPTION_std_vga
},
2934 { "monitor", 1, QEMU_OPTION_monitor
},
2935 { "serial", 1, QEMU_OPTION_serial
},
2936 { "parallel", 1, QEMU_OPTION_parallel
},
2937 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
2938 { "full-screen", 0, QEMU_OPTION_full_screen
},
2939 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
2941 /* temporary options */
2942 { "pci", 0, QEMU_OPTION_pci
},
2943 { "cirrusvga", 0, QEMU_OPTION_cirrusvga
},
2947 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
2949 /* this stack is only used during signal handling */
2950 #define SIGNAL_STACK_SIZE 32768
2952 static uint8_t *signal_stack
;
2956 /* password input */
2958 static BlockDriverState
*get_bdrv(int index
)
2960 BlockDriverState
*bs
;
2963 bs
= bs_table
[index
];
2964 } else if (index
< 6) {
2965 bs
= fd_table
[index
- 4];
2972 static void read_passwords(void)
2974 BlockDriverState
*bs
;
2978 for(i
= 0; i
< 6; i
++) {
2980 if (bs
&& bdrv_is_encrypted(bs
)) {
2981 term_printf("%s is encrypted.\n", bdrv_get_device_name(bs
));
2982 for(j
= 0; j
< 3; j
++) {
2983 monitor_readline("Password: ",
2984 1, password
, sizeof(password
));
2985 if (bdrv_set_key(bs
, password
) == 0)
2987 term_printf("invalid password\n");
2993 #define NET_IF_TUN 0
2994 #define NET_IF_USER 1
2995 #define NET_IF_DUMMY 2
2997 int main(int argc
, char **argv
)
2999 #ifdef CONFIG_GDBSTUB
3000 int use_gdbstub
, gdbstub_port
;
3003 int snapshot
, linux_boot
;
3005 const char *initrd_filename
;
3006 const char *hd_filename
[MAX_DISKS
], *fd_filename
[MAX_FD
];
3007 const char *kernel_filename
, *kernel_cmdline
;
3008 DisplayState
*ds
= &display_state
;
3009 int cyls
, heads
, secs
, translation
;
3010 int start_emulation
= 1;
3012 int net_if_type
, nb_tun_fds
, tun_fds
[MAX_NICS
];
3014 const char *r
, *optarg
;
3015 CharDriverState
*monitor_hd
;
3016 char monitor_device
[128];
3017 char serial_devices
[MAX_SERIAL_PORTS
][128];
3018 int serial_device_index
;
3019 char parallel_devices
[MAX_PARALLEL_PORTS
][128];
3020 int parallel_device_index
;
3021 const char *loadvm
= NULL
;
3023 #if !defined(CONFIG_SOFTMMU)
3024 /* we never want that malloc() uses mmap() */
3025 mallopt(M_MMAP_THRESHOLD
, 4096 * 1024);
3027 initrd_filename
= NULL
;
3028 for(i
= 0; i
< MAX_FD
; i
++)
3029 fd_filename
[i
] = NULL
;
3030 for(i
= 0; i
< MAX_DISKS
; i
++)
3031 hd_filename
[i
] = NULL
;
3032 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
3033 vga_ram_size
= VGA_RAM_SIZE
;
3034 bios_size
= BIOS_SIZE
;
3035 pstrcpy(network_script
, sizeof(network_script
), DEFAULT_NETWORK_SCRIPT
);
3036 #ifdef CONFIG_GDBSTUB
3038 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
3042 kernel_filename
= NULL
;
3043 kernel_cmdline
= "";
3045 cyls
= heads
= secs
= 0;
3046 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3047 pstrcpy(monitor_device
, sizeof(monitor_device
), "vc");
3049 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "vc");
3050 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
3051 serial_devices
[i
][0] = '\0';
3052 serial_device_index
= 0;
3054 pstrcpy(parallel_devices
[0], sizeof(parallel_devices
[0]), "vc");
3055 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
3056 parallel_devices
[i
][0] = '\0';
3057 parallel_device_index
= 0;
3062 /* default mac address of the first network interface */
3076 hd_filename
[0] = argv
[optind
++];
3078 const QEMUOption
*popt
;
3081 popt
= qemu_options
;
3084 fprintf(stderr
, "%s: invalid option -- '%s'\n",
3088 if (!strcmp(popt
->name
, r
+ 1))
3092 if (popt
->flags
& HAS_ARG
) {
3093 if (optind
>= argc
) {
3094 fprintf(stderr
, "%s: option '%s' requires an argument\n",
3098 optarg
= argv
[optind
++];
3103 switch(popt
->index
) {
3104 case QEMU_OPTION_initrd
:
3105 initrd_filename
= optarg
;
3107 case QEMU_OPTION_hda
:
3108 hd_filename
[0] = optarg
;
3110 case QEMU_OPTION_hdb
:
3111 hd_filename
[1] = optarg
;
3113 case QEMU_OPTION_snapshot
:
3116 case QEMU_OPTION_hdachs
:
3120 cyls
= strtol(p
, (char **)&p
, 0);
3121 if (cyls
< 1 || cyls
> 16383)
3126 heads
= strtol(p
, (char **)&p
, 0);
3127 if (heads
< 1 || heads
> 16)
3132 secs
= strtol(p
, (char **)&p
, 0);
3133 if (secs
< 1 || secs
> 63)
3137 if (!strcmp(p
, "none"))
3138 translation
= BIOS_ATA_TRANSLATION_NONE
;
3139 else if (!strcmp(p
, "lba"))
3140 translation
= BIOS_ATA_TRANSLATION_LBA
;
3141 else if (!strcmp(p
, "auto"))
3142 translation
= BIOS_ATA_TRANSLATION_AUTO
;
3145 } else if (*p
!= '\0') {
3147 fprintf(stderr
, "qemu: invalid physical CHS format\n");
3152 case QEMU_OPTION_nographic
:
3153 pstrcpy(monitor_device
, sizeof(monitor_device
), "stdio");
3154 pstrcpy(serial_devices
[0], sizeof(serial_devices
[0]), "stdio");
3157 case QEMU_OPTION_kernel
:
3158 kernel_filename
= optarg
;
3160 case QEMU_OPTION_append
:
3161 kernel_cmdline
= optarg
;
3163 case QEMU_OPTION_tun_fd
:
3167 net_if_type
= NET_IF_TUN
;
3168 if (nb_tun_fds
< MAX_NICS
) {
3169 fd
= strtol(optarg
, (char **)&p
, 0);
3171 fprintf(stderr
, "qemu: invalid fd for network interface %d\n", nb_tun_fds
);
3174 tun_fds
[nb_tun_fds
++] = fd
;
3178 case QEMU_OPTION_hdc
:
3179 hd_filename
[2] = optarg
;
3182 case QEMU_OPTION_hdd
:
3183 hd_filename
[3] = optarg
;
3185 case QEMU_OPTION_cdrom
:
3186 hd_filename
[2] = optarg
;
3189 case QEMU_OPTION_boot
:
3190 boot_device
= optarg
[0];
3191 if (boot_device
!= 'a' &&
3194 boot_device
!= 'n' &&
3196 boot_device
!= 'c' && boot_device
!= 'd') {
3197 fprintf(stderr
, "qemu: invalid boot device '%c'\n", boot_device
);
3201 case QEMU_OPTION_fda
:
3202 fd_filename
[0] = optarg
;
3204 case QEMU_OPTION_fdb
:
3205 fd_filename
[1] = optarg
;
3207 case QEMU_OPTION_no_code_copy
:
3208 code_copy_enabled
= 0;
3210 case QEMU_OPTION_nics
:
3211 nb_nics
= atoi(optarg
);
3212 if (nb_nics
< 0 || nb_nics
> MAX_NICS
) {
3213 fprintf(stderr
, "qemu: invalid number of network interfaces\n");
3217 case QEMU_OPTION_macaddr
:
3222 for(i
= 0; i
< 6; i
++) {
3223 macaddr
[i
] = strtol(p
, (char **)&p
, 16);
3230 fprintf(stderr
, "qemu: invalid syntax for ethernet address\n");
3239 case QEMU_OPTION_tftp
:
3240 tftp_prefix
= optarg
;
3243 case QEMU_OPTION_smb
:
3244 net_slirp_smb(optarg
);
3247 case QEMU_OPTION_user_net
:
3248 net_if_type
= NET_IF_USER
;
3250 case QEMU_OPTION_redir
:
3251 net_slirp_redir(optarg
);
3254 case QEMU_OPTION_dummy_net
:
3255 net_if_type
= NET_IF_DUMMY
;
3257 case QEMU_OPTION_enable_audio
:
3264 ram_size
= atoi(optarg
) * 1024 * 1024;
3267 if (ram_size
> PHYS_RAM_MAX_SIZE
) {
3268 fprintf(stderr
, "qemu: at most %d MB RAM can be simulated\n",
3269 PHYS_RAM_MAX_SIZE
/ (1024 * 1024));
3278 mask
= cpu_str_to_log_mask(optarg
);
3280 printf("Log items (comma separated):\n");
3281 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
3282 printf("%-10s %s\n", item
->name
, item
->help
);
3290 pstrcpy(network_script
, sizeof(network_script
), optarg
);
3292 #ifdef CONFIG_GDBSTUB
3297 gdbstub_port
= atoi(optarg
);
3304 start_emulation
= 0;
3306 case QEMU_OPTION_pci
:
3309 case QEMU_OPTION_isa
:
3312 case QEMU_OPTION_prep
:
3316 keyboard_layout
= optarg
;
3318 case QEMU_OPTION_localtime
:
3321 case QEMU_OPTION_cirrusvga
:
3322 cirrus_vga_enabled
= 1;
3324 case QEMU_OPTION_std_vga
:
3325 cirrus_vga_enabled
= 0;
3332 w
= strtol(p
, (char **)&p
, 10);
3335 fprintf(stderr
, "qemu: invalid resolution or depth\n");
3341 h
= strtol(p
, (char **)&p
, 10);
3346 depth
= strtol(p
, (char **)&p
, 10);
3347 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
3348 depth
!= 24 && depth
!= 32)
3350 } else if (*p
== '\0') {
3351 depth
= graphic_depth
;
3358 graphic_depth
= depth
;
3361 case QEMU_OPTION_monitor
:
3362 pstrcpy(monitor_device
, sizeof(monitor_device
), optarg
);
3364 case QEMU_OPTION_serial
:
3365 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
3366 fprintf(stderr
, "qemu: too many serial ports\n");
3369 pstrcpy(serial_devices
[serial_device_index
],
3370 sizeof(serial_devices
[0]), optarg
);
3371 serial_device_index
++;
3373 case QEMU_OPTION_parallel
:
3374 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
3375 fprintf(stderr
, "qemu: too many parallel ports\n");
3378 pstrcpy(parallel_devices
[parallel_device_index
],
3379 sizeof(parallel_devices
[0]), optarg
);
3380 parallel_device_index
++;
3382 case QEMU_OPTION_loadvm
:
3385 case QEMU_OPTION_full_screen
:
3388 case QEMU_OPTION_pidfile
:
3389 create_pidfile(optarg
);
3392 case QEMU_OPTION_no_kqemu
:
3400 linux_boot
= (kernel_filename
!= NULL
);
3402 if (!linux_boot
&& hd_filename
[0] == '\0' && hd_filename
[2] == '\0' &&
3403 fd_filename
[0] == '\0')
3406 /* boot to cd by default if no hard disk */
3407 if (hd_filename
[0] == '\0' && boot_device
== 'c') {
3408 if (fd_filename
[0] != '\0')
3414 #if !defined(CONFIG_SOFTMMU)
3415 /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
3417 static uint8_t stdout_buf
[4096];
3418 setvbuf(stdout
, stdout_buf
, _IOLBF
, sizeof(stdout_buf
));
3421 setvbuf(stdout
, NULL
, _IOLBF
, 0);
3424 /* init host network redirectors */
3425 if (net_if_type
== -1) {
3426 net_if_type
= NET_IF_TUN
;
3427 #if defined(CONFIG_SLIRP)
3428 if (access(network_script
, R_OK
) < 0) {
3429 net_if_type
= NET_IF_USER
;
3434 for(i
= 0; i
< nb_nics
; i
++) {
3435 NetDriverState
*nd
= &nd_table
[i
];
3437 /* init virtual mac address */
3438 nd
->macaddr
[0] = macaddr
[0];
3439 nd
->macaddr
[1] = macaddr
[1];
3440 nd
->macaddr
[2] = macaddr
[2];
3441 nd
->macaddr
[3] = macaddr
[3];
3442 nd
->macaddr
[4] = macaddr
[4];
3443 nd
->macaddr
[5] = macaddr
[5] + i
;
3444 switch(net_if_type
) {
3445 #if defined(CONFIG_SLIRP)
3450 #if !defined(_WIN32)
3452 if (i
< nb_tun_fds
) {
3453 net_fd_init(nd
, tun_fds
[i
]);
3455 if (net_tun_init(nd
) < 0)
3467 /* init the memory */
3468 phys_ram_size
= ram_size
+ vga_ram_size
+ bios_size
;
3470 #ifdef CONFIG_SOFTMMU
3471 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
3472 if (!phys_ram_base
) {
3473 fprintf(stderr
, "Could not allocate physical memory\n");
3477 /* as we must map the same page at several addresses, we must use
3482 tmpdir
= getenv("QEMU_TMPDIR");
3485 snprintf(phys_ram_file
, sizeof(phys_ram_file
), "%s/vlXXXXXX", tmpdir
);
3486 if (mkstemp(phys_ram_file
) < 0) {
3487 fprintf(stderr
, "Could not create temporary memory file '%s'\n",
3491 phys_ram_fd
= open(phys_ram_file
, O_CREAT
| O_TRUNC
| O_RDWR
, 0600);
3492 if (phys_ram_fd
< 0) {
3493 fprintf(stderr
, "Could not open temporary memory file '%s'\n",
3497 ftruncate(phys_ram_fd
, phys_ram_size
);
3498 unlink(phys_ram_file
);
3499 phys_ram_base
= mmap(get_mmap_addr(phys_ram_size
),
3501 PROT_WRITE
| PROT_READ
, MAP_SHARED
| MAP_FIXED
,
3503 if (phys_ram_base
== MAP_FAILED
) {
3504 fprintf(stderr
, "Could not map physical memory\n");
3510 /* we always create the cdrom drive, even if no disk is there */
3513 bs_table
[2] = bdrv_new("cdrom");
3514 bdrv_set_type_hint(bs_table
[2], BDRV_TYPE_CDROM
);
3517 /* open the virtual block devices */
3518 for(i
= 0; i
< MAX_DISKS
; i
++) {
3519 if (hd_filename
[i
]) {
3522 snprintf(buf
, sizeof(buf
), "hd%c", i
+ 'a');
3523 bs_table
[i
] = bdrv_new(buf
);
3525 if (bdrv_open(bs_table
[i
], hd_filename
[i
], snapshot
) < 0) {
3526 fprintf(stderr
, "qemu: could not open hard disk image '%s'\n",
3530 if (i
== 0 && cyls
!= 0) {
3531 bdrv_set_geometry_hint(bs_table
[i
], cyls
, heads
, secs
);
3532 bdrv_set_translation_hint(bs_table
[i
], translation
);
3537 /* we always create at least one floppy disk */
3538 fd_table
[0] = bdrv_new("fda");
3539 bdrv_set_type_hint(fd_table
[0], BDRV_TYPE_FLOPPY
);
3541 for(i
= 0; i
< MAX_FD
; i
++) {
3542 if (fd_filename
[i
]) {
3545 snprintf(buf
, sizeof(buf
), "fd%c", i
+ 'a');
3546 fd_table
[i
] = bdrv_new(buf
);
3547 bdrv_set_type_hint(fd_table
[i
], BDRV_TYPE_FLOPPY
);
3549 if (fd_filename
[i
] != '\0') {
3550 if (bdrv_open(fd_table
[i
], fd_filename
[i
], snapshot
) < 0) {
3551 fprintf(stderr
, "qemu: could not open floppy disk image '%s'\n",
3559 /* init CPU state */
3562 cpu_single_env
= env
;
3564 register_savevm("timer", 0, 1, timer_save
, timer_load
, env
);
3565 register_savevm("cpu", 0, 3, cpu_save
, cpu_load
, env
);
3566 register_savevm("ram", 0, 1, ram_save
, ram_load
, NULL
);
3567 qemu_register_reset(main_cpu_reset
, global_env
);
3570 cpu_calibrate_ticks();
3574 dumb_display_init(ds
);
3576 #if defined(CONFIG_SDL)
3577 sdl_display_init(ds
, full_screen
);
3578 #elif defined(CONFIG_COCOA)
3579 cocoa_display_init(ds
, full_screen
);
3581 dumb_display_init(ds
);
3585 vga_console
= graphic_console_init(ds
);
3587 monitor_hd
= qemu_chr_open(monitor_device
);
3589 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
3592 monitor_init(monitor_hd
, !nographic
);
3594 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
3595 if (serial_devices
[i
][0] != '\0') {
3596 serial_hds
[i
] = qemu_chr_open(serial_devices
[i
]);
3597 if (!serial_hds
[i
]) {
3598 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
3602 if (!strcmp(serial_devices
[i
], "vc"))
3603 qemu_chr_printf(serial_hds
[i
], "serial%d console\n", i
);
3607 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
3608 if (parallel_devices
[i
][0] != '\0') {
3609 parallel_hds
[i
] = qemu_chr_open(parallel_devices
[i
]);
3610 if (!parallel_hds
[i
]) {
3611 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
3612 parallel_devices
[i
]);
3615 if (!strcmp(parallel_devices
[i
], "vc"))
3616 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\n", i
);
3620 /* setup cpu signal handlers for MMU / self modifying code handling */
3621 #if !defined(CONFIG_SOFTMMU)
3623 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3626 signal_stack
= memalign(16, SIGNAL_STACK_SIZE
);
3627 stk
.ss_sp
= signal_stack
;
3628 stk
.ss_size
= SIGNAL_STACK_SIZE
;
3631 if (sigaltstack(&stk
, NULL
) < 0) {
3632 perror("sigaltstack");
3638 struct sigaction act
;
3640 sigfillset(&act
.sa_mask
);
3641 act
.sa_flags
= SA_SIGINFO
;
3642 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3643 act
.sa_flags
|= SA_ONSTACK
;
3645 act
.sa_sigaction
= host_segv_handler
;
3646 sigaction(SIGSEGV
, &act
, NULL
);
3647 sigaction(SIGBUS
, &act
, NULL
);
3648 #if defined (TARGET_I386) && defined(USE_CODE_COPY)
3649 sigaction(SIGFPE
, &act
, NULL
);
3656 struct sigaction act
;
3657 sigfillset(&act
.sa_mask
);
3659 act
.sa_handler
= SIG_IGN
;
3660 sigaction(SIGPIPE
, &act
, NULL
);
3665 #if defined(TARGET_I386)
3666 pc_init(ram_size
, vga_ram_size
, boot_device
,
3667 ds
, fd_filename
, snapshot
,
3668 kernel_filename
, kernel_cmdline
, initrd_filename
);
3669 #elif defined(TARGET_PPC)
3670 ppc_init(ram_size
, vga_ram_size
, boot_device
,
3671 ds
, fd_filename
, snapshot
,
3672 kernel_filename
, kernel_cmdline
, initrd_filename
);
3673 #elif defined(TARGET_SPARC)
3674 sun4m_init(ram_size
, vga_ram_size
, boot_device
,
3675 ds
, fd_filename
, snapshot
,
3676 kernel_filename
, kernel_cmdline
, initrd_filename
);
3679 gui_timer
= qemu_new_timer(rt_clock
, gui_update
, NULL
);
3680 qemu_mod_timer(gui_timer
, qemu_get_clock(rt_clock
));
3682 #ifdef CONFIG_GDBSTUB
3684 if (gdbserver_start(gdbstub_port
) < 0) {
3685 fprintf(stderr
, "Could not open gdbserver socket on port %d\n",
3689 printf("Waiting gdb connection on port %d\n", gdbstub_port
);
3694 qemu_loadvm(loadvm
);
3697 /* XXX: simplify init */
3699 if (start_emulation
) {