4 * Copyright (c) 2003-2008 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
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
40 #include "audio/audio.h"
41 #include "migration.h"
53 #include <sys/times.h>
57 #include <sys/ioctl.h>
58 #include <sys/socket.h>
59 #include <netinet/in.h>
62 #include <sys/select.h>
63 #include <arpa/inet.h>
66 #if !defined(__APPLE__) && !defined(__OpenBSD__)
72 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
73 #include <freebsd/stdlib.h>
77 #include <linux/if_tun.h>
80 #include <linux/rtc.h>
82 /* For the benefit of older linux systems which don't supply it,
83 we use a local copy of hpet.h. */
84 /* #include <linux/hpet.h> */
87 #include <linux/ppdev.h>
88 #include <linux/parport.h>
92 #include <sys/ethernet.h>
93 #include <sys/sockio.h>
94 #include <netinet/arp.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
98 #include <netinet/ip_icmp.h> // must come after ip.h
99 #include <netinet/udp.h>
100 #include <netinet/tcp.h>
108 #include "qemu_socket.h"
110 #if defined(CONFIG_SLIRP)
111 #include "libslirp.h"
114 #if defined(__OpenBSD__)
118 #if defined(CONFIG_VDE)
119 #include <libvdeplug.h>
124 #include <sys/timeb.h>
125 #include <mmsystem.h>
126 #define getopt_long_only getopt_long
127 #define memalign(align, size) malloc(size)
134 #endif /* CONFIG_SDL */
138 #define main qemu_main
139 #endif /* CONFIG_COCOA */
143 #include "exec-all.h"
145 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
146 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
148 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
150 #define SMBD_COMMAND "/usr/sbin/smbd"
153 //#define DEBUG_UNUSED_IOPORT
154 //#define DEBUG_IOPORT
156 //#define DEBUG_SLIRP
159 #define DEFAULT_RAM_SIZE 144
161 #define DEFAULT_RAM_SIZE 128
164 /* Max number of USB devices that can be specified on the commandline. */
165 #define MAX_USB_CMDLINE 8
167 /* XXX: use a two level table to limit memory usage */
168 #define MAX_IOPORTS 65536
170 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
171 const char *bios_name
= NULL
;
172 static void *ioport_opaque
[MAX_IOPORTS
];
173 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
174 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
175 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
176 to store the VM snapshots */
177 DriveInfo drives_table
[MAX_DRIVES
+1];
179 /* point to the block driver where the snapshots are managed */
180 static BlockDriverState
*bs_snapshots
;
181 static int vga_ram_size
;
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 DisplayState display_state
;
186 const char* keyboard_layout
= NULL
;
187 int64_t ticks_per_sec
;
190 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 int cirrus_vga_enabled
= 1;
195 int vmsvga_enabled
= 0;
197 int graphic_width
= 1024;
198 int graphic_height
= 768;
199 int graphic_depth
= 8;
201 int graphic_width
= 800;
202 int graphic_height
= 600;
203 int graphic_depth
= 15;
205 static int full_screen
= 0;
206 static int no_frame
= 0;
208 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
209 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
211 int win2k_install_hack
= 0;
215 const char *vnc_display
;
216 int acpi_enabled
= 1;
221 int graphic_rotate
= 0;
223 const char *option_rom
[MAX_OPTION_ROMS
];
225 int semihosting_enabled
= 0;
229 const char *qemu_name
;
232 unsigned int nb_prom_envs
= 0;
233 const char *prom_envs
[MAX_PROM_ENVS
];
235 static int nb_drives_opt
;
236 static struct drive_opt
{
239 } drives_opt
[MAX_DRIVES
];
241 static CPUState
*cur_cpu
;
242 static CPUState
*next_cpu
;
243 static int event_pending
= 1;
244 /* Conversion factor from emulated instructions to virtual clock ticks. */
245 static int icount_time_shift
;
246 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
247 #define MAX_ICOUNT_SHIFT 10
248 /* Compensate for varying guest execution speed. */
249 static int64_t qemu_icount_bias
;
250 static QEMUTimer
*icount_rt_timer
;
251 static QEMUTimer
*icount_vm_timer
;
253 uint8_t qemu_uuid
[16];
255 /***********************************************************/
256 /* x86 ISA bus support */
258 target_phys_addr_t isa_mem_base
= 0;
261 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
262 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
264 static uint32_t ioport_read(int index
, uint32_t address
)
266 static IOPortReadFunc
*default_func
[3] = {
267 default_ioport_readb
,
268 default_ioport_readw
,
271 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
273 func
= default_func
[index
];
274 return func(ioport_opaque
[address
], address
);
277 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
279 static IOPortWriteFunc
*default_func
[3] = {
280 default_ioport_writeb
,
281 default_ioport_writew
,
282 default_ioport_writel
284 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
286 func
= default_func
[index
];
287 func(ioport_opaque
[address
], address
, data
);
290 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
292 #ifdef DEBUG_UNUSED_IOPORT
293 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
298 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
300 #ifdef DEBUG_UNUSED_IOPORT
301 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
305 /* default is to make two byte accesses */
306 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
309 data
= ioport_read(0, address
);
310 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
311 data
|= ioport_read(0, address
) << 8;
315 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
317 ioport_write(0, address
, data
& 0xff);
318 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
319 ioport_write(0, address
, (data
>> 8) & 0xff);
322 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
324 #ifdef DEBUG_UNUSED_IOPORT
325 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
330 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
332 #ifdef DEBUG_UNUSED_IOPORT
333 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
337 /* size is the word size in byte */
338 int register_ioport_read(int start
, int length
, int size
,
339 IOPortReadFunc
*func
, void *opaque
)
345 } else if (size
== 2) {
347 } else if (size
== 4) {
350 hw_error("register_ioport_read: invalid size");
353 for(i
= start
; i
< start
+ length
; i
+= size
) {
354 ioport_read_table
[bsize
][i
] = func
;
355 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
356 hw_error("register_ioport_read: invalid opaque");
357 ioport_opaque
[i
] = opaque
;
362 /* size is the word size in byte */
363 int register_ioport_write(int start
, int length
, int size
,
364 IOPortWriteFunc
*func
, void *opaque
)
370 } else if (size
== 2) {
372 } else if (size
== 4) {
375 hw_error("register_ioport_write: invalid size");
378 for(i
= start
; i
< start
+ length
; i
+= size
) {
379 ioport_write_table
[bsize
][i
] = func
;
380 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
381 hw_error("register_ioport_write: invalid opaque");
382 ioport_opaque
[i
] = opaque
;
387 void isa_unassign_ioport(int start
, int length
)
391 for(i
= start
; i
< start
+ length
; i
++) {
392 ioport_read_table
[0][i
] = default_ioport_readb
;
393 ioport_read_table
[1][i
] = default_ioport_readw
;
394 ioport_read_table
[2][i
] = default_ioport_readl
;
396 ioport_write_table
[0][i
] = default_ioport_writeb
;
397 ioport_write_table
[1][i
] = default_ioport_writew
;
398 ioport_write_table
[2][i
] = default_ioport_writel
;
402 /***********************************************************/
404 void cpu_outb(CPUState
*env
, int addr
, int val
)
407 if (loglevel
& CPU_LOG_IOPORT
)
408 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
410 ioport_write(0, addr
, val
);
413 env
->last_io_time
= cpu_get_time_fast();
417 void cpu_outw(CPUState
*env
, int addr
, int val
)
420 if (loglevel
& CPU_LOG_IOPORT
)
421 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
423 ioport_write(1, addr
, val
);
426 env
->last_io_time
= cpu_get_time_fast();
430 void cpu_outl(CPUState
*env
, int addr
, int val
)
433 if (loglevel
& CPU_LOG_IOPORT
)
434 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
436 ioport_write(2, addr
, val
);
439 env
->last_io_time
= cpu_get_time_fast();
443 int cpu_inb(CPUState
*env
, int addr
)
446 val
= ioport_read(0, addr
);
448 if (loglevel
& CPU_LOG_IOPORT
)
449 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
453 env
->last_io_time
= cpu_get_time_fast();
458 int cpu_inw(CPUState
*env
, int addr
)
461 val
= ioport_read(1, addr
);
463 if (loglevel
& CPU_LOG_IOPORT
)
464 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
468 env
->last_io_time
= cpu_get_time_fast();
473 int cpu_inl(CPUState
*env
, int addr
)
476 val
= ioport_read(2, addr
);
478 if (loglevel
& CPU_LOG_IOPORT
)
479 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
483 env
->last_io_time
= cpu_get_time_fast();
488 /***********************************************************/
489 void hw_error(const char *fmt
, ...)
495 fprintf(stderr
, "qemu: hardware error: ");
496 vfprintf(stderr
, fmt
, ap
);
497 fprintf(stderr
, "\n");
498 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
499 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
501 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
503 cpu_dump_state(env
, stderr
, fprintf
, 0);
510 /***********************************************************/
513 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
514 static void *qemu_put_kbd_event_opaque
;
515 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
516 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
518 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
520 qemu_put_kbd_event_opaque
= opaque
;
521 qemu_put_kbd_event
= func
;
524 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
525 void *opaque
, int absolute
,
528 QEMUPutMouseEntry
*s
, *cursor
;
530 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
534 s
->qemu_put_mouse_event
= func
;
535 s
->qemu_put_mouse_event_opaque
= opaque
;
536 s
->qemu_put_mouse_event_absolute
= absolute
;
537 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
540 if (!qemu_put_mouse_event_head
) {
541 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
545 cursor
= qemu_put_mouse_event_head
;
546 while (cursor
->next
!= NULL
)
547 cursor
= cursor
->next
;
550 qemu_put_mouse_event_current
= s
;
555 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
557 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
559 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
562 cursor
= qemu_put_mouse_event_head
;
563 while (cursor
!= NULL
&& cursor
!= entry
) {
565 cursor
= cursor
->next
;
568 if (cursor
== NULL
) // does not exist or list empty
570 else if (prev
== NULL
) { // entry is head
571 qemu_put_mouse_event_head
= cursor
->next
;
572 if (qemu_put_mouse_event_current
== entry
)
573 qemu_put_mouse_event_current
= cursor
->next
;
574 qemu_free(entry
->qemu_put_mouse_event_name
);
579 prev
->next
= entry
->next
;
581 if (qemu_put_mouse_event_current
== entry
)
582 qemu_put_mouse_event_current
= prev
;
584 qemu_free(entry
->qemu_put_mouse_event_name
);
588 void kbd_put_keycode(int keycode
)
590 if (qemu_put_kbd_event
) {
591 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
595 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
597 QEMUPutMouseEvent
*mouse_event
;
598 void *mouse_event_opaque
;
601 if (!qemu_put_mouse_event_current
) {
606 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
608 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
611 if (graphic_rotate
) {
612 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
615 width
= graphic_width
- 1;
616 mouse_event(mouse_event_opaque
,
617 width
- dy
, dx
, dz
, buttons_state
);
619 mouse_event(mouse_event_opaque
,
620 dx
, dy
, dz
, buttons_state
);
624 int kbd_mouse_is_absolute(void)
626 if (!qemu_put_mouse_event_current
)
629 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
632 void do_info_mice(void)
634 QEMUPutMouseEntry
*cursor
;
637 if (!qemu_put_mouse_event_head
) {
638 term_printf("No mouse devices connected\n");
642 term_printf("Mouse devices available:\n");
643 cursor
= qemu_put_mouse_event_head
;
644 while (cursor
!= NULL
) {
645 term_printf("%c Mouse #%d: %s\n",
646 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
647 index
, cursor
->qemu_put_mouse_event_name
);
649 cursor
= cursor
->next
;
653 void do_mouse_set(int index
)
655 QEMUPutMouseEntry
*cursor
;
658 if (!qemu_put_mouse_event_head
) {
659 term_printf("No mouse devices connected\n");
663 cursor
= qemu_put_mouse_event_head
;
664 while (cursor
!= NULL
&& index
!= i
) {
666 cursor
= cursor
->next
;
670 qemu_put_mouse_event_current
= cursor
;
672 term_printf("Mouse at given index not found\n");
675 /* compute with 96 bit intermediate result: (a*b)/c */
676 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
681 #ifdef WORDS_BIGENDIAN
691 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
692 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
695 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
699 /***********************************************************/
700 /* real time host monotonic timer */
702 #define QEMU_TIMER_BASE 1000000000LL
706 static int64_t clock_freq
;
708 static void init_get_clock(void)
712 ret
= QueryPerformanceFrequency(&freq
);
714 fprintf(stderr
, "Could not calibrate ticks\n");
717 clock_freq
= freq
.QuadPart
;
720 static int64_t get_clock(void)
723 QueryPerformanceCounter(&ti
);
724 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
729 static int use_rt_clock
;
731 static void init_get_clock(void)
734 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
737 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
744 static int64_t get_clock(void)
746 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
749 clock_gettime(CLOCK_MONOTONIC
, &ts
);
750 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
754 /* XXX: using gettimeofday leads to problems if the date
755 changes, so it should be avoided. */
757 gettimeofday(&tv
, NULL
);
758 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
763 /* Return the virtual CPU time, based on the instruction counter. */
764 static int64_t cpu_get_icount(void)
767 CPUState
*env
= cpu_single_env
;;
768 icount
= qemu_icount
;
771 fprintf(stderr
, "Bad clock read\n");
772 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
774 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
777 /***********************************************************/
778 /* guest cycle counter */
780 static int64_t cpu_ticks_prev
;
781 static int64_t cpu_ticks_offset
;
782 static int64_t cpu_clock_offset
;
783 static int cpu_ticks_enabled
;
785 /* return the host CPU cycle counter and handle stop/restart */
786 int64_t cpu_get_ticks(void)
789 return cpu_get_icount();
791 if (!cpu_ticks_enabled
) {
792 return cpu_ticks_offset
;
795 ticks
= cpu_get_real_ticks();
796 if (cpu_ticks_prev
> ticks
) {
797 /* Note: non increasing ticks may happen if the host uses
799 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
801 cpu_ticks_prev
= ticks
;
802 return ticks
+ cpu_ticks_offset
;
806 /* return the host CPU monotonic timer and handle stop/restart */
807 static int64_t cpu_get_clock(void)
810 if (!cpu_ticks_enabled
) {
811 return cpu_clock_offset
;
814 return ti
+ cpu_clock_offset
;
818 /* enable cpu_get_ticks() */
819 void cpu_enable_ticks(void)
821 if (!cpu_ticks_enabled
) {
822 cpu_ticks_offset
-= cpu_get_real_ticks();
823 cpu_clock_offset
-= get_clock();
824 cpu_ticks_enabled
= 1;
828 /* disable cpu_get_ticks() : the clock is stopped. You must not call
829 cpu_get_ticks() after that. */
830 void cpu_disable_ticks(void)
832 if (cpu_ticks_enabled
) {
833 cpu_ticks_offset
= cpu_get_ticks();
834 cpu_clock_offset
= cpu_get_clock();
835 cpu_ticks_enabled
= 0;
839 /***********************************************************/
842 #define QEMU_TIMER_REALTIME 0
843 #define QEMU_TIMER_VIRTUAL 1
847 /* XXX: add frequency */
855 struct QEMUTimer
*next
;
858 struct qemu_alarm_timer
{
862 int (*start
)(struct qemu_alarm_timer
*t
);
863 void (*stop
)(struct qemu_alarm_timer
*t
);
864 void (*rearm
)(struct qemu_alarm_timer
*t
);
868 #define ALARM_FLAG_DYNTICKS 0x1
869 #define ALARM_FLAG_EXPIRED 0x2
871 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
873 return t
->flags
& ALARM_FLAG_DYNTICKS
;
876 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
878 if (!alarm_has_dynticks(t
))
884 /* TODO: MIN_TIMER_REARM_US should be optimized */
885 #define MIN_TIMER_REARM_US 250
887 static struct qemu_alarm_timer
*alarm_timer
;
891 struct qemu_alarm_win32
{
895 } alarm_win32_data
= {0, NULL
, -1};
897 static int win32_start_timer(struct qemu_alarm_timer
*t
);
898 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
899 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
903 static int unix_start_timer(struct qemu_alarm_timer
*t
);
904 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
908 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
909 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
910 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
912 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
913 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
915 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
916 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
918 #endif /* __linux__ */
922 /* Correlation between real and virtual time is always going to be
923 fairly approximate, so ignore small variation.
924 When the guest is idle real and virtual time will be aligned in
926 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
928 static void icount_adjust(void)
933 static int64_t last_delta
;
934 /* If the VM is not running, then do nothing. */
938 cur_time
= cpu_get_clock();
939 cur_icount
= qemu_get_clock(vm_clock
);
940 delta
= cur_icount
- cur_time
;
941 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
943 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
944 && icount_time_shift
> 0) {
945 /* The guest is getting too far ahead. Slow time down. */
949 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
950 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
951 /* The guest is getting too far behind. Speed time up. */
955 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
958 static void icount_adjust_rt(void * opaque
)
960 qemu_mod_timer(icount_rt_timer
,
961 qemu_get_clock(rt_clock
) + 1000);
965 static void icount_adjust_vm(void * opaque
)
967 qemu_mod_timer(icount_vm_timer
,
968 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
972 static void init_icount_adjust(void)
974 /* Have both realtime and virtual time triggers for speed adjustment.
975 The realtime trigger catches emulated time passing too slowly,
976 the virtual time trigger catches emulated time passing too fast.
977 Realtime triggers occur even when idle, so use them less frequently
979 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
980 qemu_mod_timer(icount_rt_timer
,
981 qemu_get_clock(rt_clock
) + 1000);
982 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
983 qemu_mod_timer(icount_vm_timer
,
984 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
987 static struct qemu_alarm_timer alarm_timers
[] = {
990 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
991 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
992 /* HPET - if available - is preferred */
993 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
994 /* ...otherwise try RTC */
995 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
997 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
999 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1000 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1001 {"win32", 0, win32_start_timer
,
1002 win32_stop_timer
, NULL
, &alarm_win32_data
},
1007 static void show_available_alarms(void)
1011 printf("Available alarm timers, in order of precedence:\n");
1012 for (i
= 0; alarm_timers
[i
].name
; i
++)
1013 printf("%s\n", alarm_timers
[i
].name
);
1016 static void configure_alarms(char const *opt
)
1020 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1023 struct qemu_alarm_timer tmp
;
1025 if (!strcmp(opt
, "?")) {
1026 show_available_alarms();
1032 /* Reorder the array */
1033 name
= strtok(arg
, ",");
1035 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1036 if (!strcmp(alarm_timers
[i
].name
, name
))
1041 fprintf(stderr
, "Unknown clock %s\n", name
);
1050 tmp
= alarm_timers
[i
];
1051 alarm_timers
[i
] = alarm_timers
[cur
];
1052 alarm_timers
[cur
] = tmp
;
1056 name
= strtok(NULL
, ",");
1062 /* Disable remaining timers */
1063 for (i
= cur
; i
< count
; i
++)
1064 alarm_timers
[i
].name
= NULL
;
1066 show_available_alarms();
1071 QEMUClock
*rt_clock
;
1072 QEMUClock
*vm_clock
;
1074 static QEMUTimer
*active_timers
[2];
1076 static QEMUClock
*qemu_new_clock(int type
)
1079 clock
= qemu_mallocz(sizeof(QEMUClock
));
1086 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1090 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1093 ts
->opaque
= opaque
;
1097 void qemu_free_timer(QEMUTimer
*ts
)
1102 /* stop a timer, but do not dealloc it */
1103 void qemu_del_timer(QEMUTimer
*ts
)
1107 /* NOTE: this code must be signal safe because
1108 qemu_timer_expired() can be called from a signal. */
1109 pt
= &active_timers
[ts
->clock
->type
];
1122 /* modify the current timer so that it will be fired when current_time
1123 >= expire_time. The corresponding callback will be called. */
1124 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1130 /* add the timer in the sorted list */
1131 /* NOTE: this code must be signal safe because
1132 qemu_timer_expired() can be called from a signal. */
1133 pt
= &active_timers
[ts
->clock
->type
];
1138 if (t
->expire_time
> expire_time
)
1142 ts
->expire_time
= expire_time
;
1146 /* Rearm if necessary */
1147 if (pt
== &active_timers
[ts
->clock
->type
]) {
1148 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1149 qemu_rearm_alarm_timer(alarm_timer
);
1151 /* Interrupt execution to force deadline recalculation. */
1152 if (use_icount
&& cpu_single_env
) {
1153 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1158 int qemu_timer_pending(QEMUTimer
*ts
)
1161 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1168 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1172 return (timer_head
->expire_time
<= current_time
);
1175 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1181 if (!ts
|| ts
->expire_time
> current_time
)
1183 /* remove timer from the list before calling the callback */
1184 *ptimer_head
= ts
->next
;
1187 /* run the callback (the timer list can be modified) */
1192 int64_t qemu_get_clock(QEMUClock
*clock
)
1194 switch(clock
->type
) {
1195 case QEMU_TIMER_REALTIME
:
1196 return get_clock() / 1000000;
1198 case QEMU_TIMER_VIRTUAL
:
1200 return cpu_get_icount();
1202 return cpu_get_clock();
1207 static void init_timers(void)
1210 ticks_per_sec
= QEMU_TIMER_BASE
;
1211 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1212 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1216 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1218 uint64_t expire_time
;
1220 if (qemu_timer_pending(ts
)) {
1221 expire_time
= ts
->expire_time
;
1225 qemu_put_be64(f
, expire_time
);
1228 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1230 uint64_t expire_time
;
1232 expire_time
= qemu_get_be64(f
);
1233 if (expire_time
!= -1) {
1234 qemu_mod_timer(ts
, expire_time
);
1240 static void timer_save(QEMUFile
*f
, void *opaque
)
1242 if (cpu_ticks_enabled
) {
1243 hw_error("cannot save state if virtual timers are running");
1245 qemu_put_be64(f
, cpu_ticks_offset
);
1246 qemu_put_be64(f
, ticks_per_sec
);
1247 qemu_put_be64(f
, cpu_clock_offset
);
1250 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1252 if (version_id
!= 1 && version_id
!= 2)
1254 if (cpu_ticks_enabled
) {
1257 cpu_ticks_offset
=qemu_get_be64(f
);
1258 ticks_per_sec
=qemu_get_be64(f
);
1259 if (version_id
== 2) {
1260 cpu_clock_offset
=qemu_get_be64(f
);
1266 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1267 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1269 static void host_alarm_handler(int host_signum
)
1273 #define DISP_FREQ 1000
1275 static int64_t delta_min
= INT64_MAX
;
1276 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1278 ti
= qemu_get_clock(vm_clock
);
1279 if (last_clock
!= 0) {
1280 delta
= ti
- last_clock
;
1281 if (delta
< delta_min
)
1283 if (delta
> delta_max
)
1286 if (++count
== DISP_FREQ
) {
1287 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1288 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1289 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1290 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1291 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1293 delta_min
= INT64_MAX
;
1301 if (alarm_has_dynticks(alarm_timer
) ||
1303 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1304 qemu_get_clock(vm_clock
))) ||
1305 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1306 qemu_get_clock(rt_clock
))) {
1308 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1309 SetEvent(data
->host_alarm
);
1311 CPUState
*env
= next_cpu
;
1313 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1316 /* stop the currently executing cpu because a timer occured */
1317 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1319 if (env
->kqemu_enabled
) {
1320 kqemu_cpu_interrupt(env
);
1328 static int64_t qemu_next_deadline(void)
1332 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1333 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1334 qemu_get_clock(vm_clock
);
1336 /* To avoid problems with overflow limit this to 2^32. */
1346 #if defined(__linux__) || defined(_WIN32)
1347 static uint64_t qemu_next_deadline_dyntick(void)
1355 delta
= (qemu_next_deadline() + 999) / 1000;
1357 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1358 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1359 qemu_get_clock(rt_clock
))*1000;
1360 if (rtdelta
< delta
)
1364 if (delta
< MIN_TIMER_REARM_US
)
1365 delta
= MIN_TIMER_REARM_US
;
1373 #if defined(__linux__)
1375 #define RTC_FREQ 1024
1377 static void enable_sigio_timer(int fd
)
1379 struct sigaction act
;
1382 sigfillset(&act
.sa_mask
);
1384 act
.sa_handler
= host_alarm_handler
;
1386 sigaction(SIGIO
, &act
, NULL
);
1387 fcntl(fd
, F_SETFL
, O_ASYNC
);
1388 fcntl(fd
, F_SETOWN
, getpid());
1391 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1393 struct hpet_info info
;
1396 fd
= open("/dev/hpet", O_RDONLY
);
1401 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1403 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1404 "error, but for better emulation accuracy type:\n"
1405 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1409 /* Check capabilities */
1410 r
= ioctl(fd
, HPET_INFO
, &info
);
1414 /* Enable periodic mode */
1415 r
= ioctl(fd
, HPET_EPI
, 0);
1416 if (info
.hi_flags
&& (r
< 0))
1419 /* Enable interrupt */
1420 r
= ioctl(fd
, HPET_IE_ON
, 0);
1424 enable_sigio_timer(fd
);
1425 t
->priv
= (void *)(long)fd
;
1433 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1435 int fd
= (long)t
->priv
;
1440 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1443 unsigned long current_rtc_freq
= 0;
1445 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1448 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1449 if (current_rtc_freq
!= RTC_FREQ
&&
1450 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1451 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1452 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1453 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1456 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1462 enable_sigio_timer(rtc_fd
);
1464 t
->priv
= (void *)(long)rtc_fd
;
1469 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1471 int rtc_fd
= (long)t
->priv
;
1476 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1480 struct sigaction act
;
1482 sigfillset(&act
.sa_mask
);
1484 act
.sa_handler
= host_alarm_handler
;
1486 sigaction(SIGALRM
, &act
, NULL
);
1488 ev
.sigev_value
.sival_int
= 0;
1489 ev
.sigev_notify
= SIGEV_SIGNAL
;
1490 ev
.sigev_signo
= SIGALRM
;
1492 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1493 perror("timer_create");
1495 /* disable dynticks */
1496 fprintf(stderr
, "Dynamic Ticks disabled\n");
1501 t
->priv
= (void *)host_timer
;
1506 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1508 timer_t host_timer
= (timer_t
)t
->priv
;
1510 timer_delete(host_timer
);
1513 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1515 timer_t host_timer
= (timer_t
)t
->priv
;
1516 struct itimerspec timeout
;
1517 int64_t nearest_delta_us
= INT64_MAX
;
1520 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1521 !active_timers
[QEMU_TIMER_VIRTUAL
])
1524 nearest_delta_us
= qemu_next_deadline_dyntick();
1526 /* check whether a timer is already running */
1527 if (timer_gettime(host_timer
, &timeout
)) {
1529 fprintf(stderr
, "Internal timer error: aborting\n");
1532 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1533 if (current_us
&& current_us
<= nearest_delta_us
)
1536 timeout
.it_interval
.tv_sec
= 0;
1537 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1538 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1539 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1540 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1542 fprintf(stderr
, "Internal timer error: aborting\n");
1547 #endif /* defined(__linux__) */
1549 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1551 struct sigaction act
;
1552 struct itimerval itv
;
1556 sigfillset(&act
.sa_mask
);
1558 act
.sa_handler
= host_alarm_handler
;
1560 sigaction(SIGALRM
, &act
, NULL
);
1562 itv
.it_interval
.tv_sec
= 0;
1563 /* for i386 kernel 2.6 to get 1 ms */
1564 itv
.it_interval
.tv_usec
= 999;
1565 itv
.it_value
.tv_sec
= 0;
1566 itv
.it_value
.tv_usec
= 10 * 1000;
1568 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1575 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1577 struct itimerval itv
;
1579 memset(&itv
, 0, sizeof(itv
));
1580 setitimer(ITIMER_REAL
, &itv
, NULL
);
1583 #endif /* !defined(_WIN32) */
1587 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1590 struct qemu_alarm_win32
*data
= t
->priv
;
1593 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1594 if (!data
->host_alarm
) {
1595 perror("Failed CreateEvent");
1599 memset(&tc
, 0, sizeof(tc
));
1600 timeGetDevCaps(&tc
, sizeof(tc
));
1602 if (data
->period
< tc
.wPeriodMin
)
1603 data
->period
= tc
.wPeriodMin
;
1605 timeBeginPeriod(data
->period
);
1607 flags
= TIME_CALLBACK_FUNCTION
;
1608 if (alarm_has_dynticks(t
))
1609 flags
|= TIME_ONESHOT
;
1611 flags
|= TIME_PERIODIC
;
1613 data
->timerId
= timeSetEvent(1, // interval (ms)
1614 data
->period
, // resolution
1615 host_alarm_handler
, // function
1616 (DWORD
)t
, // parameter
1619 if (!data
->timerId
) {
1620 perror("Failed to initialize win32 alarm timer");
1622 timeEndPeriod(data
->period
);
1623 CloseHandle(data
->host_alarm
);
1627 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1632 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1634 struct qemu_alarm_win32
*data
= t
->priv
;
1636 timeKillEvent(data
->timerId
);
1637 timeEndPeriod(data
->period
);
1639 CloseHandle(data
->host_alarm
);
1642 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1644 struct qemu_alarm_win32
*data
= t
->priv
;
1645 uint64_t nearest_delta_us
;
1647 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1648 !active_timers
[QEMU_TIMER_VIRTUAL
])
1651 nearest_delta_us
= qemu_next_deadline_dyntick();
1652 nearest_delta_us
/= 1000;
1654 timeKillEvent(data
->timerId
);
1656 data
->timerId
= timeSetEvent(1,
1660 TIME_ONESHOT
| TIME_PERIODIC
);
1662 if (!data
->timerId
) {
1663 perror("Failed to re-arm win32 alarm timer");
1665 timeEndPeriod(data
->period
);
1666 CloseHandle(data
->host_alarm
);
1673 static void init_timer_alarm(void)
1675 struct qemu_alarm_timer
*t
= NULL
;
1678 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1679 t
= &alarm_timers
[i
];
1687 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1688 fprintf(stderr
, "Terminating\n");
1695 static void quit_timers(void)
1697 alarm_timer
->stop(alarm_timer
);
1701 /***********************************************************/
1702 /* host time/date access */
1703 void qemu_get_timedate(struct tm
*tm
, int offset
)
1710 if (rtc_date_offset
== -1) {
1714 ret
= localtime(&ti
);
1716 ti
-= rtc_date_offset
;
1720 memcpy(tm
, ret
, sizeof(struct tm
));
1723 int qemu_timedate_diff(struct tm
*tm
)
1727 if (rtc_date_offset
== -1)
1729 seconds
= mktimegm(tm
);
1731 seconds
= mktime(tm
);
1733 seconds
= mktimegm(tm
) + rtc_date_offset
;
1735 return seconds
- time(NULL
);
1739 static void socket_cleanup(void)
1744 static int socket_init(void)
1749 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1751 err
= WSAGetLastError();
1752 fprintf(stderr
, "WSAStartup: %d\n", err
);
1755 atexit(socket_cleanup
);
1760 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1765 while (*p
!= '\0' && *p
!= '=') {
1766 if (q
&& (q
- buf
) < buf_size
- 1)
1776 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1781 while (*p
!= '\0') {
1783 if (*(p
+ 1) != ',')
1787 if (q
&& (q
- buf
) < buf_size
- 1)
1797 int get_param_value(char *buf
, int buf_size
,
1798 const char *tag
, const char *str
)
1805 p
= get_opt_name(option
, sizeof(option
), p
);
1809 if (!strcmp(tag
, option
)) {
1810 (void)get_opt_value(buf
, buf_size
, p
);
1813 p
= get_opt_value(NULL
, 0, p
);
1822 int check_params(char *buf
, int buf_size
,
1823 const char * const *params
, const char *str
)
1830 p
= get_opt_name(buf
, buf_size
, p
);
1834 for(i
= 0; params
[i
] != NULL
; i
++)
1835 if (!strcmp(params
[i
], buf
))
1837 if (params
[i
] == NULL
)
1839 p
= get_opt_value(NULL
, 0, p
);
1847 /***********************************************************/
1848 /* Bluetooth support */
1851 static struct HCIInfo
*hci_table
[MAX_NICS
];
1853 static struct bt_vlan_s
{
1854 struct bt_scatternet_s net
;
1856 struct bt_vlan_s
*next
;
1859 /* find or alloc a new bluetooth "VLAN" */
1860 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1862 struct bt_vlan_s
**pvlan
, *vlan
;
1863 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1867 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1869 pvlan
= &first_bt_vlan
;
1870 while (*pvlan
!= NULL
)
1871 pvlan
= &(*pvlan
)->next
;
1877 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1881 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1886 static struct HCIInfo null_hci
= {
1887 .cmd_send
= null_hci_send
,
1888 .sco_send
= null_hci_send
,
1889 .acl_send
= null_hci_send
,
1890 .bdaddr_set
= null_hci_addr_set
,
1893 struct HCIInfo
*qemu_next_hci(void)
1895 if (cur_hci
== nb_hcis
)
1898 return hci_table
[cur_hci
++];
1901 /***********************************************************/
1902 /* QEMU Block devices */
1904 #define HD_ALIAS "index=%d,media=disk"
1906 #define CDROM_ALIAS "index=1,media=cdrom"
1908 #define CDROM_ALIAS "index=2,media=cdrom"
1910 #define FD_ALIAS "index=%d,if=floppy"
1911 #define PFLASH_ALIAS "if=pflash"
1912 #define MTD_ALIAS "if=mtd"
1913 #define SD_ALIAS "index=0,if=sd"
1915 static int drive_add(const char *file
, const char *fmt
, ...)
1919 if (nb_drives_opt
>= MAX_DRIVES
) {
1920 fprintf(stderr
, "qemu: too many drives\n");
1924 drives_opt
[nb_drives_opt
].file
= file
;
1926 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
1927 sizeof(drives_opt
[0].opt
), fmt
, ap
);
1930 return nb_drives_opt
++;
1933 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
1937 /* seek interface, bus and unit */
1939 for (index
= 0; index
< nb_drives
; index
++)
1940 if (drives_table
[index
].type
== type
&&
1941 drives_table
[index
].bus
== bus
&&
1942 drives_table
[index
].unit
== unit
)
1948 int drive_get_max_bus(BlockInterfaceType type
)
1954 for (index
= 0; index
< nb_drives
; index
++) {
1955 if(drives_table
[index
].type
== type
&&
1956 drives_table
[index
].bus
> max_bus
)
1957 max_bus
= drives_table
[index
].bus
;
1962 static void bdrv_format_print(void *opaque
, const char *name
)
1964 fprintf(stderr
, " %s", name
);
1967 static int drive_init(struct drive_opt
*arg
, int snapshot
,
1968 QEMUMachine
*machine
)
1973 const char *mediastr
= "";
1974 BlockInterfaceType type
;
1975 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1976 int bus_id
, unit_id
;
1977 int cyls
, heads
, secs
, translation
;
1978 BlockDriverState
*bdrv
;
1979 BlockDriver
*drv
= NULL
;
1984 char *str
= arg
->opt
;
1985 static const char * const params
[] = { "bus", "unit", "if", "index",
1986 "cyls", "heads", "secs", "trans",
1987 "media", "snapshot", "file",
1988 "cache", "format", NULL
};
1990 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
1991 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
1997 cyls
= heads
= secs
= 0;
2000 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2004 if (machine
->use_scsi
) {
2006 max_devs
= MAX_SCSI_DEVS
;
2007 pstrcpy(devname
, sizeof(devname
), "scsi");
2010 max_devs
= MAX_IDE_DEVS
;
2011 pstrcpy(devname
, sizeof(devname
), "ide");
2015 /* extract parameters */
2017 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2018 bus_id
= strtol(buf
, NULL
, 0);
2020 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2025 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2026 unit_id
= strtol(buf
, NULL
, 0);
2028 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2033 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2034 pstrcpy(devname
, sizeof(devname
), buf
);
2035 if (!strcmp(buf
, "ide")) {
2037 max_devs
= MAX_IDE_DEVS
;
2038 } else if (!strcmp(buf
, "scsi")) {
2040 max_devs
= MAX_SCSI_DEVS
;
2041 } else if (!strcmp(buf
, "floppy")) {
2044 } else if (!strcmp(buf
, "pflash")) {
2047 } else if (!strcmp(buf
, "mtd")) {
2050 } else if (!strcmp(buf
, "sd")) {
2054 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2059 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2060 index
= strtol(buf
, NULL
, 0);
2062 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2067 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2068 cyls
= strtol(buf
, NULL
, 0);
2071 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2072 heads
= strtol(buf
, NULL
, 0);
2075 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2076 secs
= strtol(buf
, NULL
, 0);
2079 if (cyls
|| heads
|| secs
) {
2080 if (cyls
< 1 || cyls
> 16383) {
2081 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2084 if (heads
< 1 || heads
> 16) {
2085 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2088 if (secs
< 1 || secs
> 63) {
2089 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2094 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2097 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2101 if (!strcmp(buf
, "none"))
2102 translation
= BIOS_ATA_TRANSLATION_NONE
;
2103 else if (!strcmp(buf
, "lba"))
2104 translation
= BIOS_ATA_TRANSLATION_LBA
;
2105 else if (!strcmp(buf
, "auto"))
2106 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2108 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2113 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2114 if (!strcmp(buf
, "disk")) {
2116 } else if (!strcmp(buf
, "cdrom")) {
2117 if (cyls
|| secs
|| heads
) {
2119 "qemu: '%s' invalid physical CHS format\n", str
);
2122 media
= MEDIA_CDROM
;
2124 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2129 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2130 if (!strcmp(buf
, "on"))
2132 else if (!strcmp(buf
, "off"))
2135 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2140 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2141 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2143 else if (!strcmp(buf
, "writethrough"))
2145 else if (!strcmp(buf
, "writeback"))
2148 fprintf(stderr
, "qemu: invalid cache option\n");
2153 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2154 if (strcmp(buf
, "?") == 0) {
2155 fprintf(stderr
, "qemu: Supported formats:");
2156 bdrv_iterate_format(bdrv_format_print
, NULL
);
2157 fprintf(stderr
, "\n");
2160 drv
= bdrv_find_format(buf
);
2162 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2167 if (arg
->file
== NULL
)
2168 get_param_value(file
, sizeof(file
), "file", str
);
2170 pstrcpy(file
, sizeof(file
), arg
->file
);
2172 /* compute bus and unit according index */
2175 if (bus_id
!= 0 || unit_id
!= -1) {
2177 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2185 unit_id
= index
% max_devs
;
2186 bus_id
= index
/ max_devs
;
2190 /* if user doesn't specify a unit_id,
2191 * try to find the first free
2194 if (unit_id
== -1) {
2196 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2198 if (max_devs
&& unit_id
>= max_devs
) {
2199 unit_id
-= max_devs
;
2207 if (max_devs
&& unit_id
>= max_devs
) {
2208 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2209 str
, unit_id
, max_devs
- 1);
2214 * ignore multiple definitions
2217 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2222 if (type
== IF_IDE
|| type
== IF_SCSI
)
2223 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2225 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2226 devname
, bus_id
, mediastr
, unit_id
);
2228 snprintf(buf
, sizeof(buf
), "%s%s%i",
2229 devname
, mediastr
, unit_id
);
2230 bdrv
= bdrv_new(buf
);
2231 drives_table
[nb_drives
].bdrv
= bdrv
;
2232 drives_table
[nb_drives
].type
= type
;
2233 drives_table
[nb_drives
].bus
= bus_id
;
2234 drives_table
[nb_drives
].unit
= unit_id
;
2243 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2244 bdrv_set_translation_hint(bdrv
, translation
);
2248 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2253 /* FIXME: This isn't really a floppy, but it's a reasonable
2256 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2266 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2267 cache
= 2; /* always use write-back with snapshot */
2269 if (cache
== 0) /* no caching */
2270 bdrv_flags
|= BDRV_O_NOCACHE
;
2271 else if (cache
== 2) /* write-back */
2272 bdrv_flags
|= BDRV_O_CACHE_WB
;
2273 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2274 fprintf(stderr
, "qemu: could not open disk image %s\n",
2281 /***********************************************************/
2284 static USBPort
*used_usb_ports
;
2285 static USBPort
*free_usb_ports
;
2287 /* ??? Maybe change this to register a hub to keep track of the topology. */
2288 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2289 usb_attachfn attach
)
2291 port
->opaque
= opaque
;
2292 port
->index
= index
;
2293 port
->attach
= attach
;
2294 port
->next
= free_usb_ports
;
2295 free_usb_ports
= port
;
2298 int usb_device_add_dev(USBDevice
*dev
)
2302 /* Find a USB port to add the device to. */
2303 port
= free_usb_ports
;
2307 /* Create a new hub and chain it on. */
2308 free_usb_ports
= NULL
;
2309 port
->next
= used_usb_ports
;
2310 used_usb_ports
= port
;
2312 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2313 usb_attach(port
, hub
);
2314 port
= free_usb_ports
;
2317 free_usb_ports
= port
->next
;
2318 port
->next
= used_usb_ports
;
2319 used_usb_ports
= port
;
2320 usb_attach(port
, dev
);
2324 static int usb_device_add(const char *devname
)
2329 if (!free_usb_ports
)
2332 if (strstart(devname
, "host:", &p
)) {
2333 dev
= usb_host_device_open(p
);
2334 } else if (!strcmp(devname
, "mouse")) {
2335 dev
= usb_mouse_init();
2336 } else if (!strcmp(devname
, "tablet")) {
2337 dev
= usb_tablet_init();
2338 } else if (!strcmp(devname
, "keyboard")) {
2339 dev
= usb_keyboard_init();
2340 } else if (strstart(devname
, "disk:", &p
)) {
2341 dev
= usb_msd_init(p
);
2342 } else if (!strcmp(devname
, "wacom-tablet")) {
2343 dev
= usb_wacom_init();
2344 } else if (strstart(devname
, "serial:", &p
)) {
2345 dev
= usb_serial_init(p
);
2346 #ifdef CONFIG_BRLAPI
2347 } else if (!strcmp(devname
, "braille")) {
2348 dev
= usb_baum_init();
2350 } else if (strstart(devname
, "net:", &p
)) {
2353 if (net_client_init("nic", p
) < 0)
2355 nd_table
[nic
].model
= "usb";
2356 dev
= usb_net_init(&nd_table
[nic
]);
2363 return usb_device_add_dev(dev
);
2366 int usb_device_del_addr(int bus_num
, int addr
)
2372 if (!used_usb_ports
)
2378 lastp
= &used_usb_ports
;
2379 port
= used_usb_ports
;
2380 while (port
&& port
->dev
->addr
!= addr
) {
2381 lastp
= &port
->next
;
2389 *lastp
= port
->next
;
2390 usb_attach(port
, NULL
);
2391 dev
->handle_destroy(dev
);
2392 port
->next
= free_usb_ports
;
2393 free_usb_ports
= port
;
2397 static int usb_device_del(const char *devname
)
2402 if (strstart(devname
, "host:", &p
))
2403 return usb_host_device_close(p
);
2405 if (!used_usb_ports
)
2408 p
= strchr(devname
, '.');
2411 bus_num
= strtoul(devname
, NULL
, 0);
2412 addr
= strtoul(p
+ 1, NULL
, 0);
2414 return usb_device_del_addr(bus_num
, addr
);
2417 void do_usb_add(const char *devname
)
2419 usb_device_add(devname
);
2422 void do_usb_del(const char *devname
)
2424 usb_device_del(devname
);
2431 const char *speed_str
;
2434 term_printf("USB support not enabled\n");
2438 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2442 switch(dev
->speed
) {
2446 case USB_SPEED_FULL
:
2449 case USB_SPEED_HIGH
:
2456 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2457 0, dev
->addr
, speed_str
, dev
->devname
);
2461 /***********************************************************/
2462 /* PCMCIA/Cardbus */
2464 static struct pcmcia_socket_entry_s
{
2465 struct pcmcia_socket_s
*socket
;
2466 struct pcmcia_socket_entry_s
*next
;
2467 } *pcmcia_sockets
= 0;
2469 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2471 struct pcmcia_socket_entry_s
*entry
;
2473 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2474 entry
->socket
= socket
;
2475 entry
->next
= pcmcia_sockets
;
2476 pcmcia_sockets
= entry
;
2479 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2481 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2483 ptr
= &pcmcia_sockets
;
2484 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2485 if (entry
->socket
== socket
) {
2491 void pcmcia_info(void)
2493 struct pcmcia_socket_entry_s
*iter
;
2494 if (!pcmcia_sockets
)
2495 term_printf("No PCMCIA sockets\n");
2497 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2498 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2499 iter
->socket
->attached
? iter
->socket
->card_string
:
2503 /***********************************************************/
2506 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2510 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2514 static void dumb_display_init(DisplayState
*ds
)
2519 ds
->dpy_update
= dumb_update
;
2520 ds
->dpy_resize
= dumb_resize
;
2521 ds
->dpy_refresh
= NULL
;
2522 ds
->gui_timer_interval
= 0;
2526 /***********************************************************/
2529 #define MAX_IO_HANDLERS 64
2531 typedef struct IOHandlerRecord
{
2533 IOCanRWHandler
*fd_read_poll
;
2535 IOHandler
*fd_write
;
2538 /* temporary data */
2540 struct IOHandlerRecord
*next
;
2543 static IOHandlerRecord
*first_io_handler
;
2545 /* XXX: fd_read_poll should be suppressed, but an API change is
2546 necessary in the character devices to suppress fd_can_read(). */
2547 int qemu_set_fd_handler2(int fd
,
2548 IOCanRWHandler
*fd_read_poll
,
2550 IOHandler
*fd_write
,
2553 IOHandlerRecord
**pioh
, *ioh
;
2555 if (!fd_read
&& !fd_write
) {
2556 pioh
= &first_io_handler
;
2561 if (ioh
->fd
== fd
) {
2568 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2572 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2575 ioh
->next
= first_io_handler
;
2576 first_io_handler
= ioh
;
2579 ioh
->fd_read_poll
= fd_read_poll
;
2580 ioh
->fd_read
= fd_read
;
2581 ioh
->fd_write
= fd_write
;
2582 ioh
->opaque
= opaque
;
2588 int qemu_set_fd_handler(int fd
,
2590 IOHandler
*fd_write
,
2593 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2597 /***********************************************************/
2598 /* Polling handling */
2600 typedef struct PollingEntry
{
2603 struct PollingEntry
*next
;
2606 static PollingEntry
*first_polling_entry
;
2608 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2610 PollingEntry
**ppe
, *pe
;
2611 pe
= qemu_mallocz(sizeof(PollingEntry
));
2615 pe
->opaque
= opaque
;
2616 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2621 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2623 PollingEntry
**ppe
, *pe
;
2624 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2626 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2634 /***********************************************************/
2635 /* Wait objects support */
2636 typedef struct WaitObjects
{
2638 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2639 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2640 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2643 static WaitObjects wait_objects
= {0};
2645 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2647 WaitObjects
*w
= &wait_objects
;
2649 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2651 w
->events
[w
->num
] = handle
;
2652 w
->func
[w
->num
] = func
;
2653 w
->opaque
[w
->num
] = opaque
;
2658 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2661 WaitObjects
*w
= &wait_objects
;
2664 for (i
= 0; i
< w
->num
; i
++) {
2665 if (w
->events
[i
] == handle
)
2668 w
->events
[i
] = w
->events
[i
+ 1];
2669 w
->func
[i
] = w
->func
[i
+ 1];
2670 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2678 #define SELF_ANNOUNCE_ROUNDS 5
2679 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
2680 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
2681 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
2683 static int announce_self_create(uint8_t *buf
,
2686 uint32_t magic
= EXPERIMENTAL_MAGIC
;
2687 uint16_t proto
= htons(ETH_P_EXPERIMENTAL
);
2689 /* FIXME: should we send a different packet (arp/rarp/ping)? */
2691 memset(buf
, 0xff, 6); /* h_dst */
2692 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
2693 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
2694 memcpy(buf
+ 14, &magic
, 4); /* magic */
2696 return 18; /* len */
2699 void qemu_announce_self(void)
2703 VLANClientState
*vc
;
2706 for (i
= 0; i
< nb_nics
; i
++) {
2707 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
2708 vlan
= nd_table
[i
].vlan
;
2709 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2710 for (j
=0; j
< SELF_ANNOUNCE_ROUNDS
; j
++)
2711 vc
->fd_read(vc
->opaque
, buf
, len
);
2716 /***********************************************************/
2717 /* savevm/loadvm support */
2719 #define IO_BUF_SIZE 32768
2722 QEMUFilePutBufferFunc
*put_buffer
;
2723 QEMUFileGetBufferFunc
*get_buffer
;
2724 QEMUFileCloseFunc
*close
;
2725 QEMUFileRateLimit
*rate_limit
;
2729 int64_t buf_offset
; /* start of buffer when writing, end of buffer
2732 int buf_size
; /* 0 when writing */
2733 uint8_t buf
[IO_BUF_SIZE
];
2738 typedef struct QEMUFileSocket
2744 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
2746 QEMUFileSocket
*s
= opaque
;
2750 len
= recv(s
->fd
, buf
, size
, 0);
2751 } while (len
== -1 && socket_error() == EINTR
);
2754 len
= -socket_error();
2759 static int socket_close(void *opaque
)
2761 QEMUFileSocket
*s
= opaque
;
2766 QEMUFile
*qemu_fopen_socket(int fd
)
2768 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
2774 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
2778 typedef struct QEMUFileStdio
2783 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
2784 int64_t pos
, int size
)
2786 QEMUFileStdio
*s
= opaque
;
2787 fseek(s
->outfile
, pos
, SEEK_SET
);
2788 fwrite(buf
, 1, size
, s
->outfile
);
2792 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
2794 QEMUFileStdio
*s
= opaque
;
2795 fseek(s
->outfile
, pos
, SEEK_SET
);
2796 return fread(buf
, 1, size
, s
->outfile
);
2799 static int file_close(void *opaque
)
2801 QEMUFileStdio
*s
= opaque
;
2807 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
2811 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
2815 s
->outfile
= fopen(filename
, mode
);
2819 if (!strcmp(mode
, "wb"))
2820 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
2821 else if (!strcmp(mode
, "rb"))
2822 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
2831 typedef struct QEMUFileBdrv
2833 BlockDriverState
*bs
;
2834 int64_t base_offset
;
2837 static int bdrv_put_buffer(void *opaque
, const uint8_t *buf
,
2838 int64_t pos
, int size
)
2840 QEMUFileBdrv
*s
= opaque
;
2841 bdrv_pwrite(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
2845 static int bdrv_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
2847 QEMUFileBdrv
*s
= opaque
;
2848 return bdrv_pread(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
2851 static int bdrv_fclose(void *opaque
)
2853 QEMUFileBdrv
*s
= opaque
;
2858 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
2862 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
2867 s
->base_offset
= offset
;
2870 return qemu_fopen_ops(s
, bdrv_put_buffer
, NULL
, bdrv_fclose
, NULL
);
2872 return qemu_fopen_ops(s
, NULL
, bdrv_get_buffer
, bdrv_fclose
, NULL
);
2875 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
2876 QEMUFileGetBufferFunc
*get_buffer
,
2877 QEMUFileCloseFunc
*close
,
2878 QEMUFileRateLimit
*rate_limit
)
2882 f
= qemu_mallocz(sizeof(QEMUFile
));
2887 f
->put_buffer
= put_buffer
;
2888 f
->get_buffer
= get_buffer
;
2890 f
->rate_limit
= rate_limit
;
2896 int qemu_file_has_error(QEMUFile
*f
)
2898 return f
->has_error
;
2901 void qemu_fflush(QEMUFile
*f
)
2906 if (f
->is_write
&& f
->buf_index
> 0) {
2909 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
2911 f
->buf_offset
+= f
->buf_index
;
2918 static void qemu_fill_buffer(QEMUFile
*f
)
2928 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
2932 f
->buf_offset
+= len
;
2933 } else if (len
!= -EAGAIN
)
2937 int qemu_fclose(QEMUFile
*f
)
2942 ret
= f
->close(f
->opaque
);
2947 void qemu_file_put_notify(QEMUFile
*f
)
2949 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
2952 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
2956 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
2958 "Attempted to write to buffer while read buffer is not empty\n");
2962 while (!f
->has_error
&& size
> 0) {
2963 l
= IO_BUF_SIZE
- f
->buf_index
;
2966 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
2971 if (f
->buf_index
>= IO_BUF_SIZE
)
2976 void qemu_put_byte(QEMUFile
*f
, int v
)
2978 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
2980 "Attempted to write to buffer while read buffer is not empty\n");
2984 f
->buf
[f
->buf_index
++] = v
;
2986 if (f
->buf_index
>= IO_BUF_SIZE
)
2990 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
2999 l
= f
->buf_size
- f
->buf_index
;
3001 qemu_fill_buffer(f
);
3002 l
= f
->buf_size
- f
->buf_index
;
3008 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
3013 return size1
- size
;
3016 int qemu_get_byte(QEMUFile
*f
)
3021 if (f
->buf_index
>= f
->buf_size
) {
3022 qemu_fill_buffer(f
);
3023 if (f
->buf_index
>= f
->buf_size
)
3026 return f
->buf
[f
->buf_index
++];
3029 int64_t qemu_ftell(QEMUFile
*f
)
3031 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
3034 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3036 if (whence
== SEEK_SET
) {
3038 } else if (whence
== SEEK_CUR
) {
3039 pos
+= qemu_ftell(f
);
3041 /* SEEK_END not supported */
3044 if (f
->put_buffer
) {
3046 f
->buf_offset
= pos
;
3048 f
->buf_offset
= pos
;
3055 int qemu_file_rate_limit(QEMUFile
*f
)
3058 return f
->rate_limit(f
->opaque
);
3063 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3065 qemu_put_byte(f
, v
>> 8);
3066 qemu_put_byte(f
, v
);
3069 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3071 qemu_put_byte(f
, v
>> 24);
3072 qemu_put_byte(f
, v
>> 16);
3073 qemu_put_byte(f
, v
>> 8);
3074 qemu_put_byte(f
, v
);
3077 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3079 qemu_put_be32(f
, v
>> 32);
3080 qemu_put_be32(f
, v
);
3083 unsigned int qemu_get_be16(QEMUFile
*f
)
3086 v
= qemu_get_byte(f
) << 8;
3087 v
|= qemu_get_byte(f
);
3091 unsigned int qemu_get_be32(QEMUFile
*f
)
3094 v
= qemu_get_byte(f
) << 24;
3095 v
|= qemu_get_byte(f
) << 16;
3096 v
|= qemu_get_byte(f
) << 8;
3097 v
|= qemu_get_byte(f
);
3101 uint64_t qemu_get_be64(QEMUFile
*f
)
3104 v
= (uint64_t)qemu_get_be32(f
) << 32;
3105 v
|= qemu_get_be32(f
);
3109 typedef struct SaveStateEntry
{
3114 SaveLiveStateHandler
*save_live_state
;
3115 SaveStateHandler
*save_state
;
3116 LoadStateHandler
*load_state
;
3118 struct SaveStateEntry
*next
;
3121 static SaveStateEntry
*first_se
;
3123 /* TODO: Individual devices generally have very little idea about the rest
3124 of the system, so instance_id should be removed/replaced.
3125 Meanwhile pass -1 as instance_id if you do not already have a clearly
3126 distinguishing id for all instances of your device class. */
3127 int register_savevm_live(const char *idstr
,
3130 SaveLiveStateHandler
*save_live_state
,
3131 SaveStateHandler
*save_state
,
3132 LoadStateHandler
*load_state
,
3135 SaveStateEntry
*se
, **pse
;
3136 static int global_section_id
;
3138 se
= qemu_malloc(sizeof(SaveStateEntry
));
3141 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3142 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
3143 se
->version_id
= version_id
;
3144 se
->section_id
= global_section_id
++;
3145 se
->save_live_state
= save_live_state
;
3146 se
->save_state
= save_state
;
3147 se
->load_state
= load_state
;
3148 se
->opaque
= opaque
;
3151 /* add at the end of list */
3153 while (*pse
!= NULL
) {
3154 if (instance_id
== -1
3155 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
3156 && se
->instance_id
<= (*pse
)->instance_id
)
3157 se
->instance_id
= (*pse
)->instance_id
+ 1;
3158 pse
= &(*pse
)->next
;
3164 int register_savevm(const char *idstr
,
3167 SaveStateHandler
*save_state
,
3168 LoadStateHandler
*load_state
,
3171 return register_savevm_live(idstr
, instance_id
, version_id
,
3172 NULL
, save_state
, load_state
, opaque
);
3175 #define QEMU_VM_FILE_MAGIC 0x5145564d
3176 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
3177 #define QEMU_VM_FILE_VERSION 0x00000003
3179 #define QEMU_VM_EOF 0x00
3180 #define QEMU_VM_SECTION_START 0x01
3181 #define QEMU_VM_SECTION_PART 0x02
3182 #define QEMU_VM_SECTION_END 0x03
3183 #define QEMU_VM_SECTION_FULL 0x04
3185 int qemu_savevm_state_begin(QEMUFile
*f
)
3189 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3190 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3192 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
3195 if (se
->save_live_state
== NULL
)
3199 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
3200 qemu_put_be32(f
, se
->section_id
);
3203 len
= strlen(se
->idstr
);
3204 qemu_put_byte(f
, len
);
3205 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
3207 qemu_put_be32(f
, se
->instance_id
);
3208 qemu_put_be32(f
, se
->version_id
);
3210 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
3213 if (qemu_file_has_error(f
))
3219 int qemu_savevm_state_iterate(QEMUFile
*f
)
3224 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
3225 if (se
->save_live_state
== NULL
)
3229 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
3230 qemu_put_be32(f
, se
->section_id
);
3232 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
3238 if (qemu_file_has_error(f
))
3244 int qemu_savevm_state_complete(QEMUFile
*f
)
3248 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
3249 if (se
->save_live_state
== NULL
)
3253 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
3254 qemu_put_be32(f
, se
->section_id
);
3256 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
3259 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3262 if (se
->save_state
== NULL
)
3266 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
3267 qemu_put_be32(f
, se
->section_id
);
3270 len
= strlen(se
->idstr
);
3271 qemu_put_byte(f
, len
);
3272 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
3274 qemu_put_be32(f
, se
->instance_id
);
3275 qemu_put_be32(f
, se
->version_id
);
3277 se
->save_state(f
, se
->opaque
);
3280 qemu_put_byte(f
, QEMU_VM_EOF
);
3282 if (qemu_file_has_error(f
))
3288 int qemu_savevm_state(QEMUFile
*f
)
3290 int saved_vm_running
;
3293 saved_vm_running
= vm_running
;
3298 ret
= qemu_savevm_state_begin(f
);
3303 ret
= qemu_savevm_state_iterate(f
);
3308 ret
= qemu_savevm_state_complete(f
);
3311 if (qemu_file_has_error(f
))
3314 if (!ret
&& saved_vm_running
)
3320 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3324 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3325 if (!strcmp(se
->idstr
, idstr
) &&
3326 instance_id
== se
->instance_id
)
3332 typedef struct LoadStateEntry
{
3336 struct LoadStateEntry
*next
;
3339 static int qemu_loadvm_state_v2(QEMUFile
*f
)
3342 int len
, ret
, instance_id
, record_len
, version_id
;
3343 int64_t total_len
, end_pos
, cur_pos
;
3346 total_len
= qemu_get_be64(f
);
3347 end_pos
= total_len
+ qemu_ftell(f
);
3349 if (qemu_ftell(f
) >= end_pos
)
3351 len
= qemu_get_byte(f
);
3352 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
3354 instance_id
= qemu_get_be32(f
);
3355 version_id
= qemu_get_be32(f
);
3356 record_len
= qemu_get_be32(f
);
3357 cur_pos
= qemu_ftell(f
);
3358 se
= find_se(idstr
, instance_id
);
3360 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3361 instance_id
, idstr
);
3363 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3365 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3366 instance_id
, idstr
);
3369 /* always seek to exact end of record */
3370 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3373 if (qemu_file_has_error(f
))
3379 int qemu_loadvm_state(QEMUFile
*f
)
3381 LoadStateEntry
*first_le
= NULL
;
3382 uint8_t section_type
;
3386 v
= qemu_get_be32(f
);
3387 if (v
!= QEMU_VM_FILE_MAGIC
)
3390 v
= qemu_get_be32(f
);
3391 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
3392 return qemu_loadvm_state_v2(f
);
3393 if (v
!= QEMU_VM_FILE_VERSION
)
3396 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
3397 uint32_t instance_id
, version_id
, section_id
;
3403 switch (section_type
) {
3404 case QEMU_VM_SECTION_START
:
3405 case QEMU_VM_SECTION_FULL
:
3406 /* Read section start */
3407 section_id
= qemu_get_be32(f
);
3408 len
= qemu_get_byte(f
);
3409 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
3411 instance_id
= qemu_get_be32(f
);
3412 version_id
= qemu_get_be32(f
);
3414 /* Find savevm section */
3415 se
= find_se(idstr
, instance_id
);
3417 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
3422 /* Validate version */
3423 if (version_id
> se
->version_id
) {
3424 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
3425 version_id
, idstr
, se
->version_id
);
3431 le
= qemu_mallocz(sizeof(*le
));
3438 le
->section_id
= section_id
;
3439 le
->version_id
= version_id
;
3440 le
->next
= first_le
;
3443 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
3445 case QEMU_VM_SECTION_PART
:
3446 case QEMU_VM_SECTION_END
:
3447 section_id
= qemu_get_be32(f
);
3449 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
3451 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
3456 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
3459 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
3469 LoadStateEntry
*le
= first_le
;
3470 first_le
= first_le
->next
;
3474 if (qemu_file_has_error(f
))
3480 /* device can contain snapshots */
3481 static int bdrv_can_snapshot(BlockDriverState
*bs
)
3484 !bdrv_is_removable(bs
) &&
3485 !bdrv_is_read_only(bs
));
3488 /* device must be snapshots in order to have a reliable snapshot */
3489 static int bdrv_has_snapshot(BlockDriverState
*bs
)
3492 !bdrv_is_removable(bs
) &&
3493 !bdrv_is_read_only(bs
));
3496 static BlockDriverState
*get_bs_snapshots(void)
3498 BlockDriverState
*bs
;
3502 return bs_snapshots
;
3503 for(i
= 0; i
<= nb_drives
; i
++) {
3504 bs
= drives_table
[i
].bdrv
;
3505 if (bdrv_can_snapshot(bs
))
3514 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
3517 QEMUSnapshotInfo
*sn_tab
, *sn
;
3521 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
3524 for(i
= 0; i
< nb_sns
; i
++) {
3526 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
3536 void do_savevm(const char *name
)
3538 BlockDriverState
*bs
, *bs1
;
3539 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
3540 int must_delete
, ret
, i
;
3541 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
3543 int saved_vm_running
;
3550 bs
= get_bs_snapshots();
3552 term_printf("No block device can accept snapshots\n");
3556 /* ??? Should this occur after vm_stop? */
3559 saved_vm_running
= vm_running
;
3564 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
3569 memset(sn
, 0, sizeof(*sn
));
3571 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
3572 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
3575 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
3578 /* fill auxiliary fields */
3581 sn
->date_sec
= tb
.time
;
3582 sn
->date_nsec
= tb
.millitm
* 1000000;
3584 gettimeofday(&tv
, NULL
);
3585 sn
->date_sec
= tv
.tv_sec
;
3586 sn
->date_nsec
= tv
.tv_usec
* 1000;
3588 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
3590 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
3591 term_printf("Device %s does not support VM state snapshots\n",
3592 bdrv_get_device_name(bs
));
3596 /* save the VM state */
3597 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
3599 term_printf("Could not open VM state file\n");
3602 ret
= qemu_savevm_state(f
);
3603 sn
->vm_state_size
= qemu_ftell(f
);
3606 term_printf("Error %d while writing VM\n", ret
);
3610 /* create the snapshots */
3612 for(i
= 0; i
< nb_drives
; i
++) {
3613 bs1
= drives_table
[i
].bdrv
;
3614 if (bdrv_has_snapshot(bs1
)) {
3616 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
3618 term_printf("Error while deleting snapshot on '%s'\n",
3619 bdrv_get_device_name(bs1
));
3622 ret
= bdrv_snapshot_create(bs1
, sn
);
3624 term_printf("Error while creating snapshot on '%s'\n",
3625 bdrv_get_device_name(bs1
));
3631 if (saved_vm_running
)
3635 void do_loadvm(const char *name
)
3637 BlockDriverState
*bs
, *bs1
;
3638 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
3641 int saved_vm_running
;
3643 bs
= get_bs_snapshots();
3645 term_printf("No block device supports snapshots\n");
3649 /* Flush all IO requests so they don't interfere with the new state. */
3652 saved_vm_running
= vm_running
;
3655 for(i
= 0; i
<= nb_drives
; i
++) {
3656 bs1
= drives_table
[i
].bdrv
;
3657 if (bdrv_has_snapshot(bs1
)) {
3658 ret
= bdrv_snapshot_goto(bs1
, name
);
3661 term_printf("Warning: ");
3664 term_printf("Snapshots not supported on device '%s'\n",
3665 bdrv_get_device_name(bs1
));
3668 term_printf("Could not find snapshot '%s' on device '%s'\n",
3669 name
, bdrv_get_device_name(bs1
));
3672 term_printf("Error %d while activating snapshot on '%s'\n",
3673 ret
, bdrv_get_device_name(bs1
));
3676 /* fatal on snapshot block device */
3683 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
3684 term_printf("Device %s does not support VM state snapshots\n",
3685 bdrv_get_device_name(bs
));
3689 /* restore the VM state */
3690 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
3692 term_printf("Could not open VM state file\n");
3695 ret
= qemu_loadvm_state(f
);
3698 term_printf("Error %d while loading VM state\n", ret
);
3701 if (saved_vm_running
)
3705 void do_delvm(const char *name
)
3707 BlockDriverState
*bs
, *bs1
;
3710 bs
= get_bs_snapshots();
3712 term_printf("No block device supports snapshots\n");
3716 for(i
= 0; i
<= nb_drives
; i
++) {
3717 bs1
= drives_table
[i
].bdrv
;
3718 if (bdrv_has_snapshot(bs1
)) {
3719 ret
= bdrv_snapshot_delete(bs1
, name
);
3721 if (ret
== -ENOTSUP
)
3722 term_printf("Snapshots not supported on device '%s'\n",
3723 bdrv_get_device_name(bs1
));
3725 term_printf("Error %d while deleting snapshot on '%s'\n",
3726 ret
, bdrv_get_device_name(bs1
));
3732 void do_info_snapshots(void)
3734 BlockDriverState
*bs
, *bs1
;
3735 QEMUSnapshotInfo
*sn_tab
, *sn
;
3739 bs
= get_bs_snapshots();
3741 term_printf("No available block device supports snapshots\n");
3744 term_printf("Snapshot devices:");
3745 for(i
= 0; i
<= nb_drives
; i
++) {
3746 bs1
= drives_table
[i
].bdrv
;
3747 if (bdrv_has_snapshot(bs1
)) {
3749 term_printf(" %s", bdrv_get_device_name(bs1
));
3754 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
3756 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
3759 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
3760 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
3761 for(i
= 0; i
< nb_sns
; i
++) {
3763 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
3768 /***********************************************************/
3769 /* ram save/restore */
3771 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3775 v
= qemu_get_byte(f
);
3778 if (qemu_get_buffer(f
, buf
, len
) != len
)
3782 v
= qemu_get_byte(f
);
3783 memset(buf
, v
, len
);
3789 if (qemu_file_has_error(f
))
3795 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3800 if (qemu_get_be32(f
) != phys_ram_size
)
3802 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3803 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3810 #define BDRV_HASH_BLOCK_SIZE 1024
3811 #define IOBUF_SIZE 4096
3812 #define RAM_CBLOCK_MAGIC 0xfabe
3814 typedef struct RamDecompressState
{
3817 uint8_t buf
[IOBUF_SIZE
];
3818 } RamDecompressState
;
3820 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3823 memset(s
, 0, sizeof(*s
));
3825 ret
= inflateInit(&s
->zstream
);
3831 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3835 s
->zstream
.avail_out
= len
;
3836 s
->zstream
.next_out
= buf
;
3837 while (s
->zstream
.avail_out
> 0) {
3838 if (s
->zstream
.avail_in
== 0) {
3839 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3841 clen
= qemu_get_be16(s
->f
);
3842 if (clen
> IOBUF_SIZE
)
3844 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3845 s
->zstream
.avail_in
= clen
;
3846 s
->zstream
.next_in
= s
->buf
;
3848 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3849 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3856 static void ram_decompress_close(RamDecompressState
*s
)
3858 inflateEnd(&s
->zstream
);
3861 #define RAM_SAVE_FLAG_FULL 0x01
3862 #define RAM_SAVE_FLAG_COMPRESS 0x02
3863 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3864 #define RAM_SAVE_FLAG_PAGE 0x08
3865 #define RAM_SAVE_FLAG_EOS 0x10
3867 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3869 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3870 uint32_t *array
= (uint32_t *)page
;
3873 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3874 if (array
[i
] != val
)
3881 static int ram_save_block(QEMUFile
*f
)
3883 static ram_addr_t current_addr
= 0;
3884 ram_addr_t saved_addr
= current_addr
;
3885 ram_addr_t addr
= 0;
3888 while (addr
< phys_ram_size
) {
3889 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3892 cpu_physical_memory_reset_dirty(current_addr
,
3893 current_addr
+ TARGET_PAGE_SIZE
,
3894 MIGRATION_DIRTY_FLAG
);
3896 ch
= *(phys_ram_base
+ current_addr
);
3898 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3899 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3900 qemu_put_byte(f
, ch
);
3902 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3903 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3909 addr
+= TARGET_PAGE_SIZE
;
3910 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3916 static ram_addr_t ram_save_threshold
= 10;
3918 static ram_addr_t
ram_save_remaining(void)
3921 ram_addr_t count
= 0;
3923 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3924 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3931 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3936 /* Make sure all dirty bits are set */
3937 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3938 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3939 cpu_physical_memory_set_dirty(addr
);
3942 /* Enable dirty memory tracking */
3943 cpu_physical_memory_set_dirty_tracking(1);
3945 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3948 while (!qemu_file_rate_limit(f
)) {
3951 ret
= ram_save_block(f
);
3952 if (ret
== 0) /* no more blocks */
3956 /* try transferring iterative blocks of memory */
3959 cpu_physical_memory_set_dirty_tracking(0);
3961 /* flush all remaining blocks regardless of rate limiting */
3962 while (ram_save_block(f
) != 0);
3965 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3967 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3970 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3972 RamDecompressState s1
, *s
= &s1
;
3976 if (ram_decompress_open(s
, f
) < 0)
3978 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3979 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3980 fprintf(stderr
, "Error while reading ram block header\n");
3984 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3985 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3990 printf("Error block header\n");
3994 ram_decompress_close(s
);
3999 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4004 if (version_id
== 1)
4005 return ram_load_v1(f
, opaque
);
4007 if (version_id
== 2) {
4008 if (qemu_get_be32(f
) != phys_ram_size
)
4010 return ram_load_dead(f
, opaque
);
4013 if (version_id
!= 3)
4017 addr
= qemu_get_be64(f
);
4019 flags
= addr
& ~TARGET_PAGE_MASK
;
4020 addr
&= TARGET_PAGE_MASK
;
4022 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
4023 if (addr
!= phys_ram_size
)
4027 if (flags
& RAM_SAVE_FLAG_FULL
) {
4028 if (ram_load_dead(f
, opaque
) < 0)
4032 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
4033 uint8_t ch
= qemu_get_byte(f
);
4034 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
4035 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
4036 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
4037 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
4042 void qemu_service_io(void)
4044 CPUState
*env
= cpu_single_env
;
4046 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
4048 if (env
->kqemu_enabled
) {
4049 kqemu_cpu_interrupt(env
);
4055 /***********************************************************/
4056 /* bottom halves (can be seen as timers which expire ASAP) */
4067 static QEMUBH
*first_bh
= NULL
;
4069 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
4072 bh
= qemu_mallocz(sizeof(QEMUBH
));
4076 bh
->opaque
= opaque
;
4077 bh
->next
= first_bh
;
4082 int qemu_bh_poll(void)
4088 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
4089 if (!bh
->deleted
&& bh
->scheduled
) {
4098 /* remove deleted bhs */
4112 void qemu_bh_schedule_idle(QEMUBH
*bh
)
4120 void qemu_bh_schedule(QEMUBH
*bh
)
4122 CPUState
*env
= cpu_single_env
;
4127 /* stop the currently executing CPU to execute the BH ASAP */
4129 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
4133 void qemu_bh_cancel(QEMUBH
*bh
)
4138 void qemu_bh_delete(QEMUBH
*bh
)
4144 static void qemu_bh_update_timeout(int *timeout
)
4148 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
4149 if (!bh
->deleted
&& bh
->scheduled
) {
4151 /* idle bottom halves will be polled at least
4153 *timeout
= MIN(10, *timeout
);
4155 /* non-idle bottom halves will be executed
4164 /***********************************************************/
4165 /* machine registration */
4167 static QEMUMachine
*first_machine
= NULL
;
4169 int qemu_register_machine(QEMUMachine
*m
)
4172 pm
= &first_machine
;
4180 static QEMUMachine
*find_machine(const char *name
)
4184 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4185 if (!strcmp(m
->name
, name
))
4191 /***********************************************************/
4192 /* main execution loop */
4194 static void gui_update(void *opaque
)
4196 DisplayState
*ds
= opaque
;
4197 ds
->dpy_refresh(ds
);
4198 qemu_mod_timer(ds
->gui_timer
,
4199 (ds
->gui_timer_interval
?
4200 ds
->gui_timer_interval
:
4201 GUI_REFRESH_INTERVAL
)
4202 + qemu_get_clock(rt_clock
));
4205 struct vm_change_state_entry
{
4206 VMChangeStateHandler
*cb
;
4208 LIST_ENTRY (vm_change_state_entry
) entries
;
4211 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4213 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4216 VMChangeStateEntry
*e
;
4218 e
= qemu_mallocz(sizeof (*e
));
4224 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4228 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4230 LIST_REMOVE (e
, entries
);
4234 static void vm_state_notify(int running
)
4236 VMChangeStateEntry
*e
;
4238 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4239 e
->cb(e
->opaque
, running
);
4243 /* XXX: support several handlers */
4244 static VMStopHandler
*vm_stop_cb
;
4245 static void *vm_stop_opaque
;
4247 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4250 vm_stop_opaque
= opaque
;
4254 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4265 qemu_rearm_alarm_timer(alarm_timer
);
4269 void vm_stop(int reason
)
4272 cpu_disable_ticks();
4276 vm_stop_cb(vm_stop_opaque
, reason
);
4283 /* reset/shutdown handler */
4285 typedef struct QEMUResetEntry
{
4286 QEMUResetHandler
*func
;
4288 struct QEMUResetEntry
*next
;
4291 static QEMUResetEntry
*first_reset_entry
;
4292 static int reset_requested
;
4293 static int shutdown_requested
;
4294 static int powerdown_requested
;
4296 int qemu_shutdown_requested(void)
4298 int r
= shutdown_requested
;
4299 shutdown_requested
= 0;
4303 int qemu_reset_requested(void)
4305 int r
= reset_requested
;
4306 reset_requested
= 0;
4310 int qemu_powerdown_requested(void)
4312 int r
= powerdown_requested
;
4313 powerdown_requested
= 0;
4317 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4319 QEMUResetEntry
**pre
, *re
;
4321 pre
= &first_reset_entry
;
4322 while (*pre
!= NULL
)
4323 pre
= &(*pre
)->next
;
4324 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4326 re
->opaque
= opaque
;
4331 void qemu_system_reset(void)
4335 /* reset all devices */
4336 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4337 re
->func(re
->opaque
);
4341 void qemu_system_reset_request(void)
4344 shutdown_requested
= 1;
4346 reset_requested
= 1;
4349 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4352 void qemu_system_shutdown_request(void)
4354 shutdown_requested
= 1;
4356 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4359 void qemu_system_powerdown_request(void)
4361 powerdown_requested
= 1;
4363 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4367 void host_main_loop_wait(int *timeout
)
4373 /* XXX: need to suppress polling by better using win32 events */
4375 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4376 ret
|= pe
->func(pe
->opaque
);
4380 WaitObjects
*w
= &wait_objects
;
4382 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4383 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4384 if (w
->func
[ret
- WAIT_OBJECT_0
])
4385 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4387 /* Check for additional signaled events */
4388 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4390 /* Check if event is signaled */
4391 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4392 if(ret2
== WAIT_OBJECT_0
) {
4394 w
->func
[i
](w
->opaque
[i
]);
4395 } else if (ret2
== WAIT_TIMEOUT
) {
4397 err
= GetLastError();
4398 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4401 } else if (ret
== WAIT_TIMEOUT
) {
4403 err
= GetLastError();
4404 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4411 void host_main_loop_wait(int *timeout
)
4416 void main_loop_wait(int timeout
)
4418 IOHandlerRecord
*ioh
;
4419 fd_set rfds
, wfds
, xfds
;
4423 qemu_bh_update_timeout(&timeout
);
4425 host_main_loop_wait(&timeout
);
4427 /* poll any events */
4428 /* XXX: separate device handlers from system ones */
4433 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4437 (!ioh
->fd_read_poll
||
4438 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4439 FD_SET(ioh
->fd
, &rfds
);
4443 if (ioh
->fd_write
) {
4444 FD_SET(ioh
->fd
, &wfds
);
4450 tv
.tv_sec
= timeout
/ 1000;
4451 tv
.tv_usec
= (timeout
% 1000) * 1000;
4453 #if defined(CONFIG_SLIRP)
4454 if (slirp_is_inited()) {
4455 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4458 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4460 IOHandlerRecord
**pioh
;
4462 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4463 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4464 ioh
->fd_read(ioh
->opaque
);
4466 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4467 ioh
->fd_write(ioh
->opaque
);
4471 /* remove deleted IO handlers */
4472 pioh
= &first_io_handler
;
4482 #if defined(CONFIG_SLIRP)
4483 if (slirp_is_inited()) {
4489 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4494 if (likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4495 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4496 qemu_get_clock(vm_clock
));
4499 /* real time timers */
4500 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4501 qemu_get_clock(rt_clock
));
4503 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4504 alarm_timer
->flags
&= ~(ALARM_FLAG_EXPIRED
);
4505 qemu_rearm_alarm_timer(alarm_timer
);
4508 /* Check bottom-halves last in case any of the earlier events triggered
4514 static int main_loop(void)
4517 #ifdef CONFIG_PROFILER
4522 cur_cpu
= first_cpu
;
4523 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
4530 #ifdef CONFIG_PROFILER
4531 ti
= profile_getclock();
4536 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4537 env
->icount_decr
.u16
.low
= 0;
4538 env
->icount_extra
= 0;
4539 count
= qemu_next_deadline();
4540 count
= (count
+ (1 << icount_time_shift
) - 1)
4541 >> icount_time_shift
;
4542 qemu_icount
+= count
;
4543 decr
= (count
> 0xffff) ? 0xffff : count
;
4545 env
->icount_decr
.u16
.low
= decr
;
4546 env
->icount_extra
= count
;
4548 ret
= cpu_exec(env
);
4549 #ifdef CONFIG_PROFILER
4550 qemu_time
+= profile_getclock() - ti
;
4553 /* Fold pending instructions back into the
4554 instruction counter, and clear the interrupt flag. */
4555 qemu_icount
-= (env
->icount_decr
.u16
.low
4556 + env
->icount_extra
);
4557 env
->icount_decr
.u32
= 0;
4558 env
->icount_extra
= 0;
4560 next_cpu
= env
->next_cpu
?: first_cpu
;
4561 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
4562 ret
= EXCP_INTERRUPT
;
4566 if (ret
== EXCP_HLT
) {
4567 /* Give the next CPU a chance to run. */
4571 if (ret
!= EXCP_HALTED
)
4573 /* all CPUs are halted ? */
4579 if (shutdown_requested
) {
4580 ret
= EXCP_INTERRUPT
;
4588 if (reset_requested
) {
4589 reset_requested
= 0;
4590 qemu_system_reset();
4591 ret
= EXCP_INTERRUPT
;
4593 if (powerdown_requested
) {
4594 powerdown_requested
= 0;
4595 qemu_system_powerdown();
4596 ret
= EXCP_INTERRUPT
;
4598 if (unlikely(ret
== EXCP_DEBUG
)) {
4599 vm_stop(EXCP_DEBUG
);
4601 /* If all cpus are halted then wait until the next IRQ */
4602 /* XXX: use timeout computed from timers */
4603 if (ret
== EXCP_HALTED
) {
4607 /* Advance virtual time to the next event. */
4608 if (use_icount
== 1) {
4609 /* When not using an adaptive execution frequency
4610 we tend to get badly out of sync with real time,
4611 so just delay for a reasonable amount of time. */
4614 delta
= cpu_get_icount() - cpu_get_clock();
4617 /* If virtual time is ahead of real time then just
4619 timeout
= (delta
/ 1000000) + 1;
4621 /* Wait for either IO to occur or the next
4623 add
= qemu_next_deadline();
4624 /* We advance the timer before checking for IO.
4625 Limit the amount we advance so that early IO
4626 activity won't get the guest too far ahead. */
4630 add
= (add
+ (1 << icount_time_shift
) - 1)
4631 >> icount_time_shift
;
4633 timeout
= delta
/ 1000000;
4644 if (shutdown_requested
) {
4645 ret
= EXCP_INTERRUPT
;
4650 #ifdef CONFIG_PROFILER
4651 ti
= profile_getclock();
4653 main_loop_wait(timeout
);
4654 #ifdef CONFIG_PROFILER
4655 dev_time
+= profile_getclock() - ti
;
4658 cpu_disable_ticks();
4662 static void help(int exitcode
)
4664 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
4665 "usage: %s [options] [disk_image]\n"
4667 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4669 "Standard options:\n"
4670 "-M machine select emulated machine (-M ? for list)\n"
4671 "-cpu cpu select CPU (-cpu ? for list)\n"
4672 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4673 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4674 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4675 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4676 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4677 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
4678 " [,cache=writethrough|writeback|none][,format=f]\n"
4679 " use 'file' as a drive image\n"
4680 "-mtdblock file use 'file' as on-board Flash memory image\n"
4681 "-sd file use 'file' as SecureDigital card image\n"
4682 "-pflash file use 'file' as a parallel flash image\n"
4683 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4684 "-snapshot write to temporary files instead of disk image files\n"
4686 "-no-frame open SDL window without a frame and window decorations\n"
4687 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4688 "-no-quit disable SDL window close capability\n"
4691 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4693 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4694 "-smp n set the number of CPUs to 'n' [default=1]\n"
4695 "-nographic disable graphical output and redirect serial I/Os to console\n"
4696 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4698 "-k language use keyboard layout (for example \"fr\" for French)\n"
4701 "-audio-help print list of audio drivers and their options\n"
4702 "-soundhw c1,... enable audio support\n"
4703 " and only specified sound cards (comma separated list)\n"
4704 " use -soundhw ? to get the list of supported cards\n"
4705 " use -soundhw all to enable all of them\n"
4707 "-vga [std|cirrus|vmware]\n"
4708 " select video card type\n"
4709 "-localtime set the real time clock to local time [default=utc]\n"
4710 "-full-screen start in full screen\n"
4712 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4714 "-usb enable the USB driver (will be the default soon)\n"
4715 "-usbdevice name add the host or guest USB device 'name'\n"
4716 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4717 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4719 "-name string set the name of the guest\n"
4720 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4722 "Network options:\n"
4723 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4724 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4726 "-net user[,vlan=n][,hostname=host]\n"
4727 " connect the user mode network stack to VLAN 'n' and send\n"
4728 " hostname 'host' to DHCP clients\n"
4731 "-net tap[,vlan=n],ifname=name\n"
4732 " connect the host TAP network interface to VLAN 'n'\n"
4734 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4735 " connect the host TAP network interface to VLAN 'n' and use the\n"
4736 " network scripts 'file' (default=%s)\n"
4737 " and 'dfile' (default=%s);\n"
4738 " use '[down]script=no' to disable script execution;\n"
4739 " use 'fd=h' to connect to an already opened TAP interface\n"
4741 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4742 " connect the vlan 'n' to another VLAN using a socket connection\n"
4743 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4744 " connect the vlan 'n' to multicast maddr and port\n"
4746 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4747 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4748 " on host and listening for incoming connections on 'socketpath'.\n"
4749 " Use group 'groupname' and mode 'octalmode' to change default\n"
4750 " ownership and permissions for communication port.\n"
4752 "-net none use it alone to have zero network devices; if no -net option\n"
4753 " is provided, the default is '-net nic -net user'\n"
4756 "-tftp dir allow tftp access to files in dir [-net user]\n"
4757 "-bootp file advertise file in BOOTP replies\n"
4759 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4761 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4762 " redirect TCP or UDP connections from host to guest [-net user]\n"
4765 "Linux boot specific:\n"
4766 "-kernel bzImage use 'bzImage' as kernel image\n"
4767 "-append cmdline use 'cmdline' as kernel command line\n"
4768 "-initrd file use 'file' as initial ram disk\n"
4770 "Debug/Expert options:\n"
4771 "-monitor dev redirect the monitor to char device 'dev'\n"
4772 "-serial dev redirect the serial port to char device 'dev'\n"
4773 "-parallel dev redirect the parallel port to char device 'dev'\n"
4774 "-pidfile file Write PID to 'file'\n"
4775 "-S freeze CPU at startup (use 'c' to start execution)\n"
4776 "-s wait gdb connection to port\n"
4777 "-p port set gdb connection port [default=%s]\n"
4778 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4779 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4780 " translation (t=none or lba) (usually qemu can guess them)\n"
4781 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4783 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4784 "-no-kqemu disable KQEMU kernel module usage\n"
4787 "-enable-kvm enable KVM full virtualization support\n"
4790 "-no-acpi disable ACPI\n"
4792 #ifdef CONFIG_CURSES
4793 "-curses use a curses/ncurses interface instead of SDL\n"
4795 "-no-reboot exit instead of rebooting\n"
4796 "-no-shutdown stop before shutdown\n"
4797 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4798 "-vnc display start a VNC server on display\n"
4800 "-daemonize daemonize QEMU after initializing\n"
4802 "-option-rom rom load a file, rom, into the option ROM space\n"
4804 "-prom-env variable=value set OpenBIOS nvram variables\n"
4806 "-clock force the use of the given methods for timer alarm.\n"
4807 " To see what timers are available use -clock ?\n"
4808 "-startdate select initial date of the clock\n"
4809 "-icount [N|auto]\n"
4810 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4812 "During emulation, the following keys are useful:\n"
4813 "ctrl-alt-f toggle full screen\n"
4814 "ctrl-alt-n switch to virtual console 'n'\n"
4815 "ctrl-alt toggle mouse and keyboard grab\n"
4817 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4822 DEFAULT_NETWORK_SCRIPT
,
4823 DEFAULT_NETWORK_DOWN_SCRIPT
,
4825 DEFAULT_GDBSTUB_PORT
,
4830 #define HAS_ARG 0x0001
4845 QEMU_OPTION_mtdblock
,
4849 QEMU_OPTION_snapshot
,
4851 QEMU_OPTION_no_fd_bootchk
,
4854 QEMU_OPTION_nographic
,
4855 QEMU_OPTION_portrait
,
4857 QEMU_OPTION_audio_help
,
4858 QEMU_OPTION_soundhw
,
4879 QEMU_OPTION_localtime
,
4883 QEMU_OPTION_monitor
,
4885 QEMU_OPTION_parallel
,
4887 QEMU_OPTION_full_screen
,
4888 QEMU_OPTION_no_frame
,
4889 QEMU_OPTION_alt_grab
,
4890 QEMU_OPTION_no_quit
,
4891 QEMU_OPTION_pidfile
,
4892 QEMU_OPTION_no_kqemu
,
4893 QEMU_OPTION_kernel_kqemu
,
4894 QEMU_OPTION_enable_kvm
,
4895 QEMU_OPTION_win2k_hack
,
4897 QEMU_OPTION_usbdevice
,
4900 QEMU_OPTION_no_acpi
,
4902 QEMU_OPTION_no_reboot
,
4903 QEMU_OPTION_no_shutdown
,
4904 QEMU_OPTION_show_cursor
,
4905 QEMU_OPTION_daemonize
,
4906 QEMU_OPTION_option_rom
,
4907 QEMU_OPTION_semihosting
,
4909 QEMU_OPTION_prom_env
,
4910 QEMU_OPTION_old_param
,
4912 QEMU_OPTION_startdate
,
4913 QEMU_OPTION_tb_size
,
4916 QEMU_OPTION_incoming
,
4919 typedef struct QEMUOption
{
4925 static const QEMUOption qemu_options
[] = {
4926 { "h", 0, QEMU_OPTION_h
},
4927 { "help", 0, QEMU_OPTION_h
},
4929 { "M", HAS_ARG
, QEMU_OPTION_M
},
4930 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4931 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4932 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4933 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4934 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4935 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4936 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4937 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4938 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4939 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4940 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4941 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4942 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4943 { "snapshot", 0, QEMU_OPTION_snapshot
},
4945 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4947 { "m", HAS_ARG
, QEMU_OPTION_m
},
4948 { "nographic", 0, QEMU_OPTION_nographic
},
4949 { "portrait", 0, QEMU_OPTION_portrait
},
4950 { "k", HAS_ARG
, QEMU_OPTION_k
},
4952 { "audio-help", 0, QEMU_OPTION_audio_help
},
4953 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4956 { "net", HAS_ARG
, QEMU_OPTION_net
},
4958 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4959 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4961 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4963 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4966 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4967 { "append", HAS_ARG
, QEMU_OPTION_append
},
4968 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4970 { "S", 0, QEMU_OPTION_S
},
4971 { "s", 0, QEMU_OPTION_s
},
4972 { "p", HAS_ARG
, QEMU_OPTION_p
},
4973 { "d", HAS_ARG
, QEMU_OPTION_d
},
4974 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4975 { "L", HAS_ARG
, QEMU_OPTION_L
},
4976 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4978 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4979 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4982 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4984 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4985 { "g", 1, QEMU_OPTION_g
},
4987 { "localtime", 0, QEMU_OPTION_localtime
},
4988 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4989 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4990 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4991 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4992 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4993 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4994 { "full-screen", 0, QEMU_OPTION_full_screen
},
4996 { "no-frame", 0, QEMU_OPTION_no_frame
},
4997 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4998 { "no-quit", 0, QEMU_OPTION_no_quit
},
5000 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
5001 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
5002 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
5003 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
5004 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
5005 #ifdef CONFIG_CURSES
5006 { "curses", 0, QEMU_OPTION_curses
},
5008 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
5010 /* temporary options */
5011 { "usb", 0, QEMU_OPTION_usb
},
5012 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
5013 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
5014 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
5015 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
5016 { "daemonize", 0, QEMU_OPTION_daemonize
},
5017 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
5018 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5019 { "semihosting", 0, QEMU_OPTION_semihosting
},
5021 { "name", HAS_ARG
, QEMU_OPTION_name
},
5022 #if defined(TARGET_SPARC)
5023 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
5025 #if defined(TARGET_ARM)
5026 { "old-param", 0, QEMU_OPTION_old_param
},
5028 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
5029 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
5030 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
5031 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
5032 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
5036 /* password input */
5038 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
5043 if (!bdrv_is_encrypted(bs
))
5046 term_printf("%s is encrypted.\n", name
);
5047 for(i
= 0; i
< 3; i
++) {
5048 monitor_readline("Password: ", 1, password
, sizeof(password
));
5049 if (bdrv_set_key(bs
, password
) == 0)
5051 term_printf("invalid password\n");
5056 static BlockDriverState
*get_bdrv(int index
)
5058 if (index
> nb_drives
)
5060 return drives_table
[index
].bdrv
;
5063 static void read_passwords(void)
5065 BlockDriverState
*bs
;
5068 for(i
= 0; i
< 6; i
++) {
5071 qemu_key_check(bs
, bdrv_get_device_name(bs
));
5076 struct soundhw soundhw
[] = {
5077 #ifdef HAS_AUDIO_CHOICE
5078 #if defined(TARGET_I386) || defined(TARGET_MIPS)
5084 { .init_isa
= pcspk_audio_init
}
5089 "Creative Sound Blaster 16",
5092 { .init_isa
= SB16_init
}
5095 #ifdef CONFIG_CS4231A
5101 { .init_isa
= cs4231a_init
}
5109 "Yamaha YMF262 (OPL3)",
5111 "Yamaha YM3812 (OPL2)",
5115 { .init_isa
= Adlib_init
}
5122 "Gravis Ultrasound GF1",
5125 { .init_isa
= GUS_init
}
5132 "Intel 82801AA AC97 Audio",
5135 { .init_pci
= ac97_init
}
5141 "ENSONIQ AudioPCI ES1370",
5144 { .init_pci
= es1370_init
}
5148 { NULL
, NULL
, 0, 0, { NULL
} }
5151 static void select_soundhw (const char *optarg
)
5155 if (*optarg
== '?') {
5158 printf ("Valid sound card names (comma separated):\n");
5159 for (c
= soundhw
; c
->name
; ++c
) {
5160 printf ("%-11s %s\n", c
->name
, c
->descr
);
5162 printf ("\n-soundhw all will enable all of the above\n");
5163 exit (*optarg
!= '?');
5171 if (!strcmp (optarg
, "all")) {
5172 for (c
= soundhw
; c
->name
; ++c
) {
5180 e
= strchr (p
, ',');
5181 l
= !e
? strlen (p
) : (size_t) (e
- p
);
5183 for (c
= soundhw
; c
->name
; ++c
) {
5184 if (!strncmp (c
->name
, p
, l
)) {
5193 "Unknown sound card name (too big to show)\n");
5196 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
5201 p
+= l
+ (e
!= NULL
);
5205 goto show_valid_cards
;
5210 static void select_vgahw (const char *p
)
5214 if (strstart(p
, "std", &opts
)) {
5215 cirrus_vga_enabled
= 0;
5217 } else if (strstart(p
, "cirrus", &opts
)) {
5218 cirrus_vga_enabled
= 1;
5220 } else if (strstart(p
, "vmware", &opts
)) {
5221 cirrus_vga_enabled
= 0;
5225 fprintf(stderr
, "Unknown vga type: %s\n", p
);
5229 const char *nextopt
;
5231 if (strstart(opts
, ",retrace=", &nextopt
)) {
5233 if (strstart(opts
, "dumb", &nextopt
))
5234 vga_retrace_method
= VGA_RETRACE_DUMB
;
5235 else if (strstart(opts
, "precise", &nextopt
))
5236 vga_retrace_method
= VGA_RETRACE_PRECISE
;
5237 else goto invalid_vga
;
5238 } else goto invalid_vga
;
5244 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
5246 exit(STATUS_CONTROL_C_EXIT
);
5251 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
5255 if(strlen(str
) != 36)
5258 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
5259 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
5260 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
5268 #define MAX_NET_CLIENTS 32
5272 static void termsig_handler(int signal
)
5274 qemu_system_shutdown_request();
5277 static void termsig_setup(void)
5279 struct sigaction act
;
5281 memset(&act
, 0, sizeof(act
));
5282 act
.sa_handler
= termsig_handler
;
5283 sigaction(SIGINT
, &act
, NULL
);
5284 sigaction(SIGHUP
, &act
, NULL
);
5285 sigaction(SIGTERM
, &act
, NULL
);
5290 int main(int argc
, char **argv
)
5292 #ifdef CONFIG_GDBSTUB
5294 const char *gdbstub_port
;
5296 uint32_t boot_devices_bitmap
= 0;
5298 int snapshot
, linux_boot
, net_boot
;
5299 const char *initrd_filename
;
5300 const char *kernel_filename
, *kernel_cmdline
;
5301 const char *boot_devices
= "";
5302 DisplayState
*ds
= &display_state
;
5303 int cyls
, heads
, secs
, translation
;
5304 const char *net_clients
[MAX_NET_CLIENTS
];
5308 const char *r
, *optarg
;
5309 CharDriverState
*monitor_hd
;
5310 const char *monitor_device
;
5311 const char *serial_devices
[MAX_SERIAL_PORTS
];
5312 int serial_device_index
;
5313 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
5314 int parallel_device_index
;
5315 const char *loadvm
= NULL
;
5316 QEMUMachine
*machine
;
5317 const char *cpu_model
;
5318 const char *usb_devices
[MAX_USB_CMDLINE
];
5319 int usb_devices_index
;
5322 const char *pid_file
= NULL
;
5324 const char *incoming
= NULL
;
5326 LIST_INIT (&vm_change_state_head
);
5329 struct sigaction act
;
5330 sigfillset(&act
.sa_mask
);
5332 act
.sa_handler
= SIG_IGN
;
5333 sigaction(SIGPIPE
, &act
, NULL
);
5336 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
5337 /* Note: cpu_interrupt() is currently not SMP safe, so we force
5338 QEMU to run on a single CPU */
5343 h
= GetCurrentProcess();
5344 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
5345 for(i
= 0; i
< 32; i
++) {
5346 if (mask
& (1 << i
))
5351 SetProcessAffinityMask(h
, mask
);
5357 register_machines();
5358 machine
= first_machine
;
5360 initrd_filename
= NULL
;
5362 vga_ram_size
= VGA_RAM_SIZE
;
5363 #ifdef CONFIG_GDBSTUB
5365 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5370 kernel_filename
= NULL
;
5371 kernel_cmdline
= "";
5372 cyls
= heads
= secs
= 0;
5373 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5374 monitor_device
= "vc";
5376 serial_devices
[0] = "vc:80Cx24C";
5377 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5378 serial_devices
[i
] = NULL
;
5379 serial_device_index
= 0;
5381 parallel_devices
[0] = "vc:640x480";
5382 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5383 parallel_devices
[i
] = NULL
;
5384 parallel_device_index
= 0;
5386 usb_devices_index
= 0;
5404 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
5406 const QEMUOption
*popt
;
5409 /* Treat --foo the same as -foo. */
5412 popt
= qemu_options
;
5415 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5419 if (!strcmp(popt
->name
, r
+ 1))
5423 if (popt
->flags
& HAS_ARG
) {
5424 if (optind
>= argc
) {
5425 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5429 optarg
= argv
[optind
++];
5434 switch(popt
->index
) {
5436 machine
= find_machine(optarg
);
5439 printf("Supported machines are:\n");
5440 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5441 printf("%-10s %s%s\n",
5443 m
== first_machine
? " (default)" : "");
5445 exit(*optarg
!= '?');
5448 case QEMU_OPTION_cpu
:
5449 /* hw initialization will check this */
5450 if (*optarg
== '?') {
5451 /* XXX: implement xxx_cpu_list for targets that still miss it */
5452 #if defined(cpu_list)
5453 cpu_list(stdout
, &fprintf
);
5460 case QEMU_OPTION_initrd
:
5461 initrd_filename
= optarg
;
5463 case QEMU_OPTION_hda
:
5465 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5467 hda_index
= drive_add(optarg
, HD_ALIAS
5468 ",cyls=%d,heads=%d,secs=%d%s",
5469 0, cyls
, heads
, secs
,
5470 translation
== BIOS_ATA_TRANSLATION_LBA
?
5472 translation
== BIOS_ATA_TRANSLATION_NONE
?
5473 ",trans=none" : "");
5475 case QEMU_OPTION_hdb
:
5476 case QEMU_OPTION_hdc
:
5477 case QEMU_OPTION_hdd
:
5478 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5480 case QEMU_OPTION_drive
:
5481 drive_add(NULL
, "%s", optarg
);
5483 case QEMU_OPTION_mtdblock
:
5484 drive_add(optarg
, MTD_ALIAS
);
5486 case QEMU_OPTION_sd
:
5487 drive_add(optarg
, SD_ALIAS
);
5489 case QEMU_OPTION_pflash
:
5490 drive_add(optarg
, PFLASH_ALIAS
);
5492 case QEMU_OPTION_snapshot
:
5495 case QEMU_OPTION_hdachs
:
5499 cyls
= strtol(p
, (char **)&p
, 0);
5500 if (cyls
< 1 || cyls
> 16383)
5505 heads
= strtol(p
, (char **)&p
, 0);
5506 if (heads
< 1 || heads
> 16)
5511 secs
= strtol(p
, (char **)&p
, 0);
5512 if (secs
< 1 || secs
> 63)
5516 if (!strcmp(p
, "none"))
5517 translation
= BIOS_ATA_TRANSLATION_NONE
;
5518 else if (!strcmp(p
, "lba"))
5519 translation
= BIOS_ATA_TRANSLATION_LBA
;
5520 else if (!strcmp(p
, "auto"))
5521 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5524 } else if (*p
!= '\0') {
5526 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5529 if (hda_index
!= -1)
5530 snprintf(drives_opt
[hda_index
].opt
,
5531 sizeof(drives_opt
[hda_index
].opt
),
5532 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5533 0, cyls
, heads
, secs
,
5534 translation
== BIOS_ATA_TRANSLATION_LBA
?
5536 translation
== BIOS_ATA_TRANSLATION_NONE
?
5537 ",trans=none" : "");
5540 case QEMU_OPTION_nographic
:
5543 #ifdef CONFIG_CURSES
5544 case QEMU_OPTION_curses
:
5548 case QEMU_OPTION_portrait
:
5551 case QEMU_OPTION_kernel
:
5552 kernel_filename
= optarg
;
5554 case QEMU_OPTION_append
:
5555 kernel_cmdline
= optarg
;
5557 case QEMU_OPTION_cdrom
:
5558 drive_add(optarg
, CDROM_ALIAS
);
5560 case QEMU_OPTION_boot
:
5561 boot_devices
= optarg
;
5562 /* We just do some generic consistency checks */
5564 /* Could easily be extended to 64 devices if needed */
5567 boot_devices_bitmap
= 0;
5568 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5569 /* Allowed boot devices are:
5570 * a b : floppy disk drives
5571 * c ... f : IDE disk drives
5572 * g ... m : machine implementation dependant drives
5573 * n ... p : network devices
5574 * It's up to each machine implementation to check
5575 * if the given boot devices match the actual hardware
5576 * implementation and firmware features.
5578 if (*p
< 'a' || *p
> 'q') {
5579 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5582 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5584 "Boot device '%c' was given twice\n",*p
);
5587 boot_devices_bitmap
|= 1 << (*p
- 'a');
5591 case QEMU_OPTION_fda
:
5592 case QEMU_OPTION_fdb
:
5593 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5596 case QEMU_OPTION_no_fd_bootchk
:
5600 case QEMU_OPTION_net
:
5601 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5602 fprintf(stderr
, "qemu: too many network clients\n");
5605 net_clients
[nb_net_clients
] = optarg
;
5609 case QEMU_OPTION_tftp
:
5610 tftp_prefix
= optarg
;
5612 case QEMU_OPTION_bootp
:
5613 bootp_filename
= optarg
;
5616 case QEMU_OPTION_smb
:
5617 net_slirp_smb(optarg
);
5620 case QEMU_OPTION_redir
:
5621 net_slirp_redir(optarg
);
5625 case QEMU_OPTION_audio_help
:
5629 case QEMU_OPTION_soundhw
:
5630 select_soundhw (optarg
);
5636 case QEMU_OPTION_m
: {
5640 value
= strtoul(optarg
, &ptr
, 10);
5642 case 0: case 'M': case 'm':
5649 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5653 /* On 32-bit hosts, QEMU is limited by virtual address space */
5654 if (value
> (2047 << 20)
5656 && HOST_LONG_BITS
== 32
5659 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5662 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5663 fprintf(stderr
, "qemu: ram size too large\n");
5672 const CPULogItem
*item
;
5674 mask
= cpu_str_to_log_mask(optarg
);
5676 printf("Log items (comma separated):\n");
5677 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5678 printf("%-10s %s\n", item
->name
, item
->help
);
5685 #ifdef CONFIG_GDBSTUB
5690 gdbstub_port
= optarg
;
5696 case QEMU_OPTION_bios
:
5703 keyboard_layout
= optarg
;
5705 case QEMU_OPTION_localtime
:
5708 case QEMU_OPTION_vga
:
5709 select_vgahw (optarg
);
5716 w
= strtol(p
, (char **)&p
, 10);
5719 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5725 h
= strtol(p
, (char **)&p
, 10);
5730 depth
= strtol(p
, (char **)&p
, 10);
5731 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5732 depth
!= 24 && depth
!= 32)
5734 } else if (*p
== '\0') {
5735 depth
= graphic_depth
;
5742 graphic_depth
= depth
;
5745 case QEMU_OPTION_echr
:
5748 term_escape_char
= strtol(optarg
, &r
, 0);
5750 printf("Bad argument to echr\n");
5753 case QEMU_OPTION_monitor
:
5754 monitor_device
= optarg
;
5756 case QEMU_OPTION_serial
:
5757 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5758 fprintf(stderr
, "qemu: too many serial ports\n");
5761 serial_devices
[serial_device_index
] = optarg
;
5762 serial_device_index
++;
5764 case QEMU_OPTION_parallel
:
5765 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5766 fprintf(stderr
, "qemu: too many parallel ports\n");
5769 parallel_devices
[parallel_device_index
] = optarg
;
5770 parallel_device_index
++;
5772 case QEMU_OPTION_loadvm
:
5775 case QEMU_OPTION_full_screen
:
5779 case QEMU_OPTION_no_frame
:
5782 case QEMU_OPTION_alt_grab
:
5785 case QEMU_OPTION_no_quit
:
5789 case QEMU_OPTION_pidfile
:
5793 case QEMU_OPTION_win2k_hack
:
5794 win2k_install_hack
= 1;
5798 case QEMU_OPTION_no_kqemu
:
5801 case QEMU_OPTION_kernel_kqemu
:
5806 case QEMU_OPTION_enable_kvm
:
5813 case QEMU_OPTION_usb
:
5816 case QEMU_OPTION_usbdevice
:
5818 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5819 fprintf(stderr
, "Too many USB devices\n");
5822 usb_devices
[usb_devices_index
] = optarg
;
5823 usb_devices_index
++;
5825 case QEMU_OPTION_smp
:
5826 smp_cpus
= atoi(optarg
);
5828 fprintf(stderr
, "Invalid number of CPUs\n");
5832 case QEMU_OPTION_vnc
:
5833 vnc_display
= optarg
;
5835 case QEMU_OPTION_no_acpi
:
5838 case QEMU_OPTION_no_reboot
:
5841 case QEMU_OPTION_no_shutdown
:
5844 case QEMU_OPTION_show_cursor
:
5847 case QEMU_OPTION_uuid
:
5848 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5849 fprintf(stderr
, "Fail to parse UUID string."
5850 " Wrong format.\n");
5854 case QEMU_OPTION_daemonize
:
5857 case QEMU_OPTION_option_rom
:
5858 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5859 fprintf(stderr
, "Too many option ROMs\n");
5862 option_rom
[nb_option_roms
] = optarg
;
5865 case QEMU_OPTION_semihosting
:
5866 semihosting_enabled
= 1;
5868 case QEMU_OPTION_name
:
5872 case QEMU_OPTION_prom_env
:
5873 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5874 fprintf(stderr
, "Too many prom variables\n");
5877 prom_envs
[nb_prom_envs
] = optarg
;
5882 case QEMU_OPTION_old_param
:
5886 case QEMU_OPTION_clock
:
5887 configure_alarms(optarg
);
5889 case QEMU_OPTION_startdate
:
5892 time_t rtc_start_date
;
5893 if (!strcmp(optarg
, "now")) {
5894 rtc_date_offset
= -1;
5896 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5904 } else if (sscanf(optarg
, "%d-%d-%d",
5907 &tm
.tm_mday
) == 3) {
5916 rtc_start_date
= mktimegm(&tm
);
5917 if (rtc_start_date
== -1) {
5919 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5920 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5923 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5927 case QEMU_OPTION_tb_size
:
5928 tb_size
= strtol(optarg
, NULL
, 0);
5932 case QEMU_OPTION_icount
:
5934 if (strcmp(optarg
, "auto") == 0) {
5935 icount_time_shift
= -1;
5937 icount_time_shift
= strtol(optarg
, NULL
, 0);
5940 case QEMU_OPTION_incoming
:
5947 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5948 if (kvm_allowed
&& kqemu_allowed
) {
5950 "You can not enable both KVM and kqemu at the same time\n");
5955 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5956 if (smp_cpus
> machine
->max_cpus
) {
5957 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5958 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5964 if (serial_device_index
== 0)
5965 serial_devices
[0] = "stdio";
5966 if (parallel_device_index
== 0)
5967 parallel_devices
[0] = "null";
5968 if (strncmp(monitor_device
, "vc", 2) == 0)
5969 monitor_device
= "stdio";
5976 if (pipe(fds
) == -1)
5987 len
= read(fds
[0], &status
, 1);
5988 if (len
== -1 && (errno
== EINTR
))
5993 else if (status
== 1) {
5994 fprintf(stderr
, "Could not acquire pidfile\n");
6011 signal(SIGTSTP
, SIG_IGN
);
6012 signal(SIGTTOU
, SIG_IGN
);
6013 signal(SIGTTIN
, SIG_IGN
);
6017 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
6020 write(fds
[1], &status
, 1);
6022 fprintf(stderr
, "Could not acquire pid file\n");
6030 linux_boot
= (kernel_filename
!= NULL
);
6031 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
6033 if (!linux_boot
&& net_boot
== 0 &&
6034 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
6037 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
6038 fprintf(stderr
, "-append only allowed with -kernel option\n");
6042 if (!linux_boot
&& initrd_filename
!= NULL
) {
6043 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
6047 /* boot to floppy or the default cd if no hard disk defined yet */
6048 if (!boot_devices
[0]) {
6049 boot_devices
= "cad";
6051 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6055 if (use_icount
&& icount_time_shift
< 0) {
6057 /* 125MIPS seems a reasonable initial guess at the guest speed.
6058 It will be corrected fairly quickly anyway. */
6059 icount_time_shift
= 3;
6060 init_icount_adjust();
6067 /* init network clients */
6068 if (nb_net_clients
== 0) {
6069 /* if no clients, we use a default config */
6070 net_clients
[nb_net_clients
++] = "nic";
6072 net_clients
[nb_net_clients
++] = "user";
6076 for(i
= 0;i
< nb_net_clients
; i
++) {
6077 if (net_client_parse(net_clients
[i
]) < 0)
6083 /* XXX: this should be moved in the PC machine instantiation code */
6084 if (net_boot
!= 0) {
6086 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
6087 const char *model
= nd_table
[i
].model
;
6089 if (net_boot
& (1 << i
)) {
6092 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
6093 if (get_image_size(buf
) > 0) {
6094 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
6095 fprintf(stderr
, "Too many option ROMs\n");
6098 option_rom
[nb_option_roms
] = strdup(buf
);
6105 fprintf(stderr
, "No valid PXE rom found for network device\n");
6111 /* init the memory */
6112 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
6114 if (machine
->ram_require
& RAMSIZE_FIXED
) {
6116 if (ram_size
< phys_ram_size
) {
6117 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
6118 machine
->name
, (unsigned long long) phys_ram_size
);
6122 phys_ram_size
= ram_size
;
6124 ram_size
= phys_ram_size
;
6127 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6129 phys_ram_size
+= ram_size
;
6132 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6133 if (!phys_ram_base
) {
6134 fprintf(stderr
, "Could not allocate physical memory\n");
6138 /* init the dynamic translator */
6139 cpu_exec_init_all(tb_size
* 1024 * 1024);
6143 /* we always create the cdrom drive, even if no disk is there */
6145 if (nb_drives_opt
< MAX_DRIVES
)
6146 drive_add(NULL
, CDROM_ALIAS
);
6148 /* we always create at least one floppy */
6150 if (nb_drives_opt
< MAX_DRIVES
)
6151 drive_add(NULL
, FD_ALIAS
, 0);
6153 /* we always create one sd slot, even if no card is in it */
6155 if (nb_drives_opt
< MAX_DRIVES
)
6156 drive_add(NULL
, SD_ALIAS
);
6158 /* open the virtual block devices */
6160 for(i
= 0; i
< nb_drives_opt
; i
++)
6161 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
6164 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
6165 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
6168 memset(&display_state
, 0, sizeof(display_state
));
6171 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
6174 /* nearly nothing to do */
6175 dumb_display_init(ds
);
6176 } else if (vnc_display
!= NULL
) {
6177 vnc_display_init(ds
);
6178 if (vnc_display_open(ds
, vnc_display
) < 0)
6181 #if defined(CONFIG_CURSES)
6183 curses_display_init(ds
, full_screen
);
6187 #if defined(CONFIG_SDL)
6188 sdl_display_init(ds
, full_screen
, no_frame
);
6189 #elif defined(CONFIG_COCOA)
6190 cocoa_display_init(ds
, full_screen
);
6192 dumb_display_init(ds
);
6197 /* must be after terminal init, SDL library changes signal handlers */
6201 /* Maintain compatibility with multiple stdio monitors */
6202 if (!strcmp(monitor_device
,"stdio")) {
6203 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6204 const char *devname
= serial_devices
[i
];
6205 if (devname
&& !strcmp(devname
,"mon:stdio")) {
6206 monitor_device
= NULL
;
6208 } else if (devname
&& !strcmp(devname
,"stdio")) {
6209 monitor_device
= NULL
;
6210 serial_devices
[i
] = "mon:stdio";
6215 if (monitor_device
) {
6216 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
6218 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6221 monitor_init(monitor_hd
, !nographic
);
6224 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6225 const char *devname
= serial_devices
[i
];
6226 if (devname
&& strcmp(devname
, "none")) {
6228 snprintf(label
, sizeof(label
), "serial%d", i
);
6229 serial_hds
[i
] = qemu_chr_open(label
, devname
);
6230 if (!serial_hds
[i
]) {
6231 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6235 if (strstart(devname
, "vc", 0))
6236 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6240 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6241 const char *devname
= parallel_devices
[i
];
6242 if (devname
&& strcmp(devname
, "none")) {
6244 snprintf(label
, sizeof(label
), "parallel%d", i
);
6245 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
6246 if (!parallel_hds
[i
]) {
6247 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6251 if (strstart(devname
, "vc", 0))
6252 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6256 if (kvm_enabled()) {
6259 ret
= kvm_init(smp_cpus
);
6261 fprintf(stderr
, "failed to initialize KVM\n");
6266 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
6267 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6269 /* init USB devices */
6271 for(i
= 0; i
< usb_devices_index
; i
++) {
6272 if (usb_device_add(usb_devices
[i
]) < 0) {
6273 fprintf(stderr
, "Warning: could not add USB device %s\n",
6279 if (display_state
.dpy_refresh
) {
6280 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
6281 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
6284 #ifdef CONFIG_GDBSTUB
6286 /* XXX: use standard host:port notation and modify options
6288 if (gdbserver_start(gdbstub_port
) < 0) {
6289 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
6300 autostart
= 0; /* fixme how to deal with -daemonize */
6301 qemu_start_incoming_migration(incoming
);
6305 /* XXX: simplify init */
6318 len
= write(fds
[1], &status
, 1);
6319 if (len
== -1 && (errno
== EINTR
))
6326 TFR(fd
= open("/dev/null", O_RDWR
));