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"
52 #include <sys/times.h>
56 #include <sys/ioctl.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
61 #include <sys/select.h>
62 #include <arpa/inet.h>
65 #if !defined(__APPLE__) && !defined(__OpenBSD__)
71 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
72 #include <freebsd/stdlib.h>
76 #include <linux/if_tun.h>
79 #include <linux/rtc.h>
81 /* For the benefit of older linux systems which don't supply it,
82 we use a local copy of hpet.h. */
83 /* #include <linux/hpet.h> */
86 #include <linux/ppdev.h>
87 #include <linux/parport.h>
91 #include <sys/ethernet.h>
92 #include <sys/sockio.h>
93 #include <netinet/arp.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_icmp.h> // must come after ip.h
98 #include <netinet/udp.h>
99 #include <netinet/tcp.h>
107 #include "qemu_socket.h"
109 #if defined(CONFIG_SLIRP)
110 #include "libslirp.h"
113 #if defined(__OpenBSD__)
117 #if defined(CONFIG_VDE)
118 #include <libvdeplug.h>
123 #include <sys/timeb.h>
124 #include <mmsystem.h>
125 #define getopt_long_only getopt_long
126 #define memalign(align, size) malloc(size)
133 #endif /* CONFIG_SDL */
137 #define main qemu_main
138 #endif /* CONFIG_COCOA */
142 #include "exec-all.h"
144 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
145 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
147 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
149 #define SMBD_COMMAND "/usr/sbin/smbd"
152 //#define DEBUG_UNUSED_IOPORT
153 //#define DEBUG_IOPORT
155 //#define DEBUG_SLIRP
158 #define DEFAULT_RAM_SIZE 144
160 #define DEFAULT_RAM_SIZE 128
163 /* Max number of USB devices that can be specified on the commandline. */
164 #define MAX_USB_CMDLINE 8
166 /* XXX: use a two level table to limit memory usage */
167 #define MAX_IOPORTS 65536
169 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
170 const char *bios_name
= NULL
;
171 static void *ioport_opaque
[MAX_IOPORTS
];
172 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
173 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
174 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
175 to store the VM snapshots */
176 DriveInfo drives_table
[MAX_DRIVES
+1];
178 /* point to the block driver where the snapshots are managed */
179 static BlockDriverState
*bs_snapshots
;
180 static int vga_ram_size
;
181 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
182 DisplayState display_state
;
185 const char* keyboard_layout
= NULL
;
186 int64_t ticks_per_sec
;
189 NICInfo nd_table
[MAX_NICS
];
191 static int rtc_utc
= 1;
192 static int rtc_date_offset
= -1; /* -1 means no change */
193 int cirrus_vga_enabled
= 1;
194 int vmsvga_enabled
= 0;
196 int graphic_width
= 1024;
197 int graphic_height
= 768;
198 int graphic_depth
= 8;
200 int graphic_width
= 800;
201 int graphic_height
= 600;
202 int graphic_depth
= 15;
204 static int full_screen
= 0;
205 static int no_frame
= 0;
207 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
208 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
210 int win2k_install_hack
= 0;
214 const char *vnc_display
;
215 int acpi_enabled
= 1;
220 int graphic_rotate
= 0;
222 const char *option_rom
[MAX_OPTION_ROMS
];
224 int semihosting_enabled
= 0;
228 const char *qemu_name
;
231 unsigned int nb_prom_envs
= 0;
232 const char *prom_envs
[MAX_PROM_ENVS
];
234 static int nb_drives_opt
;
235 static struct drive_opt
{
238 } drives_opt
[MAX_DRIVES
];
240 static CPUState
*cur_cpu
;
241 static CPUState
*next_cpu
;
242 static int event_pending
= 1;
243 /* Conversion factor from emulated instructions to virtual clock ticks. */
244 static int icount_time_shift
;
245 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
246 #define MAX_ICOUNT_SHIFT 10
247 /* Compensate for varying guest execution speed. */
248 static int64_t qemu_icount_bias
;
249 static QEMUTimer
*icount_rt_timer
;
250 static QEMUTimer
*icount_vm_timer
;
252 uint8_t qemu_uuid
[16];
254 /***********************************************************/
255 /* x86 ISA bus support */
257 target_phys_addr_t isa_mem_base
= 0;
260 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
261 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
263 static uint32_t ioport_read(int index
, uint32_t address
)
265 static IOPortReadFunc
*default_func
[3] = {
266 default_ioport_readb
,
267 default_ioport_readw
,
270 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
272 func
= default_func
[index
];
273 return func(ioport_opaque
[address
], address
);
276 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
278 static IOPortWriteFunc
*default_func
[3] = {
279 default_ioport_writeb
,
280 default_ioport_writew
,
281 default_ioport_writel
283 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
285 func
= default_func
[index
];
286 func(ioport_opaque
[address
], address
, data
);
289 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
291 #ifdef DEBUG_UNUSED_IOPORT
292 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
297 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
299 #ifdef DEBUG_UNUSED_IOPORT
300 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
304 /* default is to make two byte accesses */
305 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
308 data
= ioport_read(0, address
);
309 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
310 data
|= ioport_read(0, address
) << 8;
314 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
316 ioport_write(0, address
, data
& 0xff);
317 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
318 ioport_write(0, address
, (data
>> 8) & 0xff);
321 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
323 #ifdef DEBUG_UNUSED_IOPORT
324 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
329 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
331 #ifdef DEBUG_UNUSED_IOPORT
332 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
336 /* size is the word size in byte */
337 int register_ioport_read(int start
, int length
, int size
,
338 IOPortReadFunc
*func
, void *opaque
)
344 } else if (size
== 2) {
346 } else if (size
== 4) {
349 hw_error("register_ioport_read: invalid size");
352 for(i
= start
; i
< start
+ length
; i
+= size
) {
353 ioport_read_table
[bsize
][i
] = func
;
354 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
355 hw_error("register_ioport_read: invalid opaque");
356 ioport_opaque
[i
] = opaque
;
361 /* size is the word size in byte */
362 int register_ioport_write(int start
, int length
, int size
,
363 IOPortWriteFunc
*func
, void *opaque
)
369 } else if (size
== 2) {
371 } else if (size
== 4) {
374 hw_error("register_ioport_write: invalid size");
377 for(i
= start
; i
< start
+ length
; i
+= size
) {
378 ioport_write_table
[bsize
][i
] = func
;
379 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
380 hw_error("register_ioport_write: invalid opaque");
381 ioport_opaque
[i
] = opaque
;
386 void isa_unassign_ioport(int start
, int length
)
390 for(i
= start
; i
< start
+ length
; i
++) {
391 ioport_read_table
[0][i
] = default_ioport_readb
;
392 ioport_read_table
[1][i
] = default_ioport_readw
;
393 ioport_read_table
[2][i
] = default_ioport_readl
;
395 ioport_write_table
[0][i
] = default_ioport_writeb
;
396 ioport_write_table
[1][i
] = default_ioport_writew
;
397 ioport_write_table
[2][i
] = default_ioport_writel
;
401 /***********************************************************/
403 void cpu_outb(CPUState
*env
, int addr
, int val
)
406 if (loglevel
& CPU_LOG_IOPORT
)
407 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
409 ioport_write(0, addr
, val
);
412 env
->last_io_time
= cpu_get_time_fast();
416 void cpu_outw(CPUState
*env
, int addr
, int val
)
419 if (loglevel
& CPU_LOG_IOPORT
)
420 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
422 ioport_write(1, addr
, val
);
425 env
->last_io_time
= cpu_get_time_fast();
429 void cpu_outl(CPUState
*env
, int addr
, int val
)
432 if (loglevel
& CPU_LOG_IOPORT
)
433 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
435 ioport_write(2, addr
, val
);
438 env
->last_io_time
= cpu_get_time_fast();
442 int cpu_inb(CPUState
*env
, int addr
)
445 val
= ioport_read(0, addr
);
447 if (loglevel
& CPU_LOG_IOPORT
)
448 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
452 env
->last_io_time
= cpu_get_time_fast();
457 int cpu_inw(CPUState
*env
, int addr
)
460 val
= ioport_read(1, addr
);
462 if (loglevel
& CPU_LOG_IOPORT
)
463 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
467 env
->last_io_time
= cpu_get_time_fast();
472 int cpu_inl(CPUState
*env
, int addr
)
475 val
= ioport_read(2, addr
);
477 if (loglevel
& CPU_LOG_IOPORT
)
478 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
482 env
->last_io_time
= cpu_get_time_fast();
487 /***********************************************************/
488 void hw_error(const char *fmt
, ...)
494 fprintf(stderr
, "qemu: hardware error: ");
495 vfprintf(stderr
, fmt
, ap
);
496 fprintf(stderr
, "\n");
497 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
498 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
500 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
502 cpu_dump_state(env
, stderr
, fprintf
, 0);
509 /***********************************************************/
512 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
513 static void *qemu_put_kbd_event_opaque
;
514 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
515 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
517 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
519 qemu_put_kbd_event_opaque
= opaque
;
520 qemu_put_kbd_event
= func
;
523 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
524 void *opaque
, int absolute
,
527 QEMUPutMouseEntry
*s
, *cursor
;
529 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
533 s
->qemu_put_mouse_event
= func
;
534 s
->qemu_put_mouse_event_opaque
= opaque
;
535 s
->qemu_put_mouse_event_absolute
= absolute
;
536 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
539 if (!qemu_put_mouse_event_head
) {
540 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
544 cursor
= qemu_put_mouse_event_head
;
545 while (cursor
->next
!= NULL
)
546 cursor
= cursor
->next
;
549 qemu_put_mouse_event_current
= s
;
554 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
556 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
558 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
561 cursor
= qemu_put_mouse_event_head
;
562 while (cursor
!= NULL
&& cursor
!= entry
) {
564 cursor
= cursor
->next
;
567 if (cursor
== NULL
) // does not exist or list empty
569 else if (prev
== NULL
) { // entry is head
570 qemu_put_mouse_event_head
= cursor
->next
;
571 if (qemu_put_mouse_event_current
== entry
)
572 qemu_put_mouse_event_current
= cursor
->next
;
573 qemu_free(entry
->qemu_put_mouse_event_name
);
578 prev
->next
= entry
->next
;
580 if (qemu_put_mouse_event_current
== entry
)
581 qemu_put_mouse_event_current
= prev
;
583 qemu_free(entry
->qemu_put_mouse_event_name
);
587 void kbd_put_keycode(int keycode
)
589 if (qemu_put_kbd_event
) {
590 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
594 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
596 QEMUPutMouseEvent
*mouse_event
;
597 void *mouse_event_opaque
;
600 if (!qemu_put_mouse_event_current
) {
605 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
607 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
610 if (graphic_rotate
) {
611 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
614 width
= graphic_width
- 1;
615 mouse_event(mouse_event_opaque
,
616 width
- dy
, dx
, dz
, buttons_state
);
618 mouse_event(mouse_event_opaque
,
619 dx
, dy
, dz
, buttons_state
);
623 int kbd_mouse_is_absolute(void)
625 if (!qemu_put_mouse_event_current
)
628 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
631 void do_info_mice(void)
633 QEMUPutMouseEntry
*cursor
;
636 if (!qemu_put_mouse_event_head
) {
637 term_printf("No mouse devices connected\n");
641 term_printf("Mouse devices available:\n");
642 cursor
= qemu_put_mouse_event_head
;
643 while (cursor
!= NULL
) {
644 term_printf("%c Mouse #%d: %s\n",
645 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
646 index
, cursor
->qemu_put_mouse_event_name
);
648 cursor
= cursor
->next
;
652 void do_mouse_set(int index
)
654 QEMUPutMouseEntry
*cursor
;
657 if (!qemu_put_mouse_event_head
) {
658 term_printf("No mouse devices connected\n");
662 cursor
= qemu_put_mouse_event_head
;
663 while (cursor
!= NULL
&& index
!= i
) {
665 cursor
= cursor
->next
;
669 qemu_put_mouse_event_current
= cursor
;
671 term_printf("Mouse at given index not found\n");
674 /* compute with 96 bit intermediate result: (a*b)/c */
675 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
680 #ifdef WORDS_BIGENDIAN
690 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
691 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
694 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
698 /***********************************************************/
699 /* real time host monotonic timer */
701 #define QEMU_TIMER_BASE 1000000000LL
705 static int64_t clock_freq
;
707 static void init_get_clock(void)
711 ret
= QueryPerformanceFrequency(&freq
);
713 fprintf(stderr
, "Could not calibrate ticks\n");
716 clock_freq
= freq
.QuadPart
;
719 static int64_t get_clock(void)
722 QueryPerformanceCounter(&ti
);
723 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
728 static int use_rt_clock
;
730 static void init_get_clock(void)
733 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
736 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
743 static int64_t get_clock(void)
745 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
748 clock_gettime(CLOCK_MONOTONIC
, &ts
);
749 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
753 /* XXX: using gettimeofday leads to problems if the date
754 changes, so it should be avoided. */
756 gettimeofday(&tv
, NULL
);
757 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
762 /* Return the virtual CPU time, based on the instruction counter. */
763 static int64_t cpu_get_icount(void)
766 CPUState
*env
= cpu_single_env
;;
767 icount
= qemu_icount
;
770 fprintf(stderr
, "Bad clock read\n");
771 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
773 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
776 /***********************************************************/
777 /* guest cycle counter */
779 static int64_t cpu_ticks_prev
;
780 static int64_t cpu_ticks_offset
;
781 static int64_t cpu_clock_offset
;
782 static int cpu_ticks_enabled
;
784 /* return the host CPU cycle counter and handle stop/restart */
785 int64_t cpu_get_ticks(void)
788 return cpu_get_icount();
790 if (!cpu_ticks_enabled
) {
791 return cpu_ticks_offset
;
794 ticks
= cpu_get_real_ticks();
795 if (cpu_ticks_prev
> ticks
) {
796 /* Note: non increasing ticks may happen if the host uses
798 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
800 cpu_ticks_prev
= ticks
;
801 return ticks
+ cpu_ticks_offset
;
805 /* return the host CPU monotonic timer and handle stop/restart */
806 static int64_t cpu_get_clock(void)
809 if (!cpu_ticks_enabled
) {
810 return cpu_clock_offset
;
813 return ti
+ cpu_clock_offset
;
817 /* enable cpu_get_ticks() */
818 void cpu_enable_ticks(void)
820 if (!cpu_ticks_enabled
) {
821 cpu_ticks_offset
-= cpu_get_real_ticks();
822 cpu_clock_offset
-= get_clock();
823 cpu_ticks_enabled
= 1;
827 /* disable cpu_get_ticks() : the clock is stopped. You must not call
828 cpu_get_ticks() after that. */
829 void cpu_disable_ticks(void)
831 if (cpu_ticks_enabled
) {
832 cpu_ticks_offset
= cpu_get_ticks();
833 cpu_clock_offset
= cpu_get_clock();
834 cpu_ticks_enabled
= 0;
838 /***********************************************************/
841 #define QEMU_TIMER_REALTIME 0
842 #define QEMU_TIMER_VIRTUAL 1
846 /* XXX: add frequency */
854 struct QEMUTimer
*next
;
857 struct qemu_alarm_timer
{
861 int (*start
)(struct qemu_alarm_timer
*t
);
862 void (*stop
)(struct qemu_alarm_timer
*t
);
863 void (*rearm
)(struct qemu_alarm_timer
*t
);
867 #define ALARM_FLAG_DYNTICKS 0x1
868 #define ALARM_FLAG_EXPIRED 0x2
870 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
872 return t
->flags
& ALARM_FLAG_DYNTICKS
;
875 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
877 if (!alarm_has_dynticks(t
))
883 /* TODO: MIN_TIMER_REARM_US should be optimized */
884 #define MIN_TIMER_REARM_US 250
886 static struct qemu_alarm_timer
*alarm_timer
;
890 struct qemu_alarm_win32
{
894 } alarm_win32_data
= {0, NULL
, -1};
896 static int win32_start_timer(struct qemu_alarm_timer
*t
);
897 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
898 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
902 static int unix_start_timer(struct qemu_alarm_timer
*t
);
903 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
907 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
908 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
909 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
911 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
912 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
914 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
915 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
917 #endif /* __linux__ */
921 /* Correlation between real and virtual time is always going to be
922 fairly approximate, so ignore small variation.
923 When the guest is idle real and virtual time will be aligned in
925 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
927 static void icount_adjust(void)
932 static int64_t last_delta
;
933 /* If the VM is not running, then do nothing. */
937 cur_time
= cpu_get_clock();
938 cur_icount
= qemu_get_clock(vm_clock
);
939 delta
= cur_icount
- cur_time
;
940 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
942 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
943 && icount_time_shift
> 0) {
944 /* The guest is getting too far ahead. Slow time down. */
948 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
949 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
950 /* The guest is getting too far behind. Speed time up. */
954 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
957 static void icount_adjust_rt(void * opaque
)
959 qemu_mod_timer(icount_rt_timer
,
960 qemu_get_clock(rt_clock
) + 1000);
964 static void icount_adjust_vm(void * opaque
)
966 qemu_mod_timer(icount_vm_timer
,
967 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
971 static void init_icount_adjust(void)
973 /* Have both realtime and virtual time triggers for speed adjustment.
974 The realtime trigger catches emulated time passing too slowly,
975 the virtual time trigger catches emulated time passing too fast.
976 Realtime triggers occur even when idle, so use them less frequently
978 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
979 qemu_mod_timer(icount_rt_timer
,
980 qemu_get_clock(rt_clock
) + 1000);
981 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
982 qemu_mod_timer(icount_vm_timer
,
983 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
986 static struct qemu_alarm_timer alarm_timers
[] = {
989 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
990 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
991 /* HPET - if available - is preferred */
992 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
993 /* ...otherwise try RTC */
994 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
996 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
998 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
999 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1000 {"win32", 0, win32_start_timer
,
1001 win32_stop_timer
, NULL
, &alarm_win32_data
},
1006 static void show_available_alarms(void)
1010 printf("Available alarm timers, in order of precedence:\n");
1011 for (i
= 0; alarm_timers
[i
].name
; i
++)
1012 printf("%s\n", alarm_timers
[i
].name
);
1015 static void configure_alarms(char const *opt
)
1019 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1022 struct qemu_alarm_timer tmp
;
1024 if (!strcmp(opt
, "?")) {
1025 show_available_alarms();
1031 /* Reorder the array */
1032 name
= strtok(arg
, ",");
1034 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1035 if (!strcmp(alarm_timers
[i
].name
, name
))
1040 fprintf(stderr
, "Unknown clock %s\n", name
);
1049 tmp
= alarm_timers
[i
];
1050 alarm_timers
[i
] = alarm_timers
[cur
];
1051 alarm_timers
[cur
] = tmp
;
1055 name
= strtok(NULL
, ",");
1061 /* Disable remaining timers */
1062 for (i
= cur
; i
< count
; i
++)
1063 alarm_timers
[i
].name
= NULL
;
1065 show_available_alarms();
1070 QEMUClock
*rt_clock
;
1071 QEMUClock
*vm_clock
;
1073 static QEMUTimer
*active_timers
[2];
1075 static QEMUClock
*qemu_new_clock(int type
)
1078 clock
= qemu_mallocz(sizeof(QEMUClock
));
1085 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1089 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1092 ts
->opaque
= opaque
;
1096 void qemu_free_timer(QEMUTimer
*ts
)
1101 /* stop a timer, but do not dealloc it */
1102 void qemu_del_timer(QEMUTimer
*ts
)
1106 /* NOTE: this code must be signal safe because
1107 qemu_timer_expired() can be called from a signal. */
1108 pt
= &active_timers
[ts
->clock
->type
];
1121 /* modify the current timer so that it will be fired when current_time
1122 >= expire_time. The corresponding callback will be called. */
1123 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1129 /* add the timer in the sorted list */
1130 /* NOTE: this code must be signal safe because
1131 qemu_timer_expired() can be called from a signal. */
1132 pt
= &active_timers
[ts
->clock
->type
];
1137 if (t
->expire_time
> expire_time
)
1141 ts
->expire_time
= expire_time
;
1145 /* Rearm if necessary */
1146 if (pt
== &active_timers
[ts
->clock
->type
]) {
1147 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1148 qemu_rearm_alarm_timer(alarm_timer
);
1150 /* Interrupt execution to force deadline recalculation. */
1151 if (use_icount
&& cpu_single_env
) {
1152 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1157 int qemu_timer_pending(QEMUTimer
*ts
)
1160 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1167 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1171 return (timer_head
->expire_time
<= current_time
);
1174 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1180 if (!ts
|| ts
->expire_time
> current_time
)
1182 /* remove timer from the list before calling the callback */
1183 *ptimer_head
= ts
->next
;
1186 /* run the callback (the timer list can be modified) */
1191 int64_t qemu_get_clock(QEMUClock
*clock
)
1193 switch(clock
->type
) {
1194 case QEMU_TIMER_REALTIME
:
1195 return get_clock() / 1000000;
1197 case QEMU_TIMER_VIRTUAL
:
1199 return cpu_get_icount();
1201 return cpu_get_clock();
1206 static void init_timers(void)
1209 ticks_per_sec
= QEMU_TIMER_BASE
;
1210 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1211 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1215 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1217 uint64_t expire_time
;
1219 if (qemu_timer_pending(ts
)) {
1220 expire_time
= ts
->expire_time
;
1224 qemu_put_be64(f
, expire_time
);
1227 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1229 uint64_t expire_time
;
1231 expire_time
= qemu_get_be64(f
);
1232 if (expire_time
!= -1) {
1233 qemu_mod_timer(ts
, expire_time
);
1239 static void timer_save(QEMUFile
*f
, void *opaque
)
1241 if (cpu_ticks_enabled
) {
1242 hw_error("cannot save state if virtual timers are running");
1244 qemu_put_be64(f
, cpu_ticks_offset
);
1245 qemu_put_be64(f
, ticks_per_sec
);
1246 qemu_put_be64(f
, cpu_clock_offset
);
1249 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1251 if (version_id
!= 1 && version_id
!= 2)
1253 if (cpu_ticks_enabled
) {
1256 cpu_ticks_offset
=qemu_get_be64(f
);
1257 ticks_per_sec
=qemu_get_be64(f
);
1258 if (version_id
== 2) {
1259 cpu_clock_offset
=qemu_get_be64(f
);
1265 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1266 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1268 static void host_alarm_handler(int host_signum
)
1272 #define DISP_FREQ 1000
1274 static int64_t delta_min
= INT64_MAX
;
1275 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1277 ti
= qemu_get_clock(vm_clock
);
1278 if (last_clock
!= 0) {
1279 delta
= ti
- last_clock
;
1280 if (delta
< delta_min
)
1282 if (delta
> delta_max
)
1285 if (++count
== DISP_FREQ
) {
1286 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1287 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1288 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1289 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1290 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1292 delta_min
= INT64_MAX
;
1300 if (alarm_has_dynticks(alarm_timer
) ||
1302 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1303 qemu_get_clock(vm_clock
))) ||
1304 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1305 qemu_get_clock(rt_clock
))) {
1307 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1308 SetEvent(data
->host_alarm
);
1310 CPUState
*env
= next_cpu
;
1312 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1315 /* stop the currently executing cpu because a timer occured */
1316 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1318 if (env
->kqemu_enabled
) {
1319 kqemu_cpu_interrupt(env
);
1327 static int64_t qemu_next_deadline(void)
1331 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1332 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1333 qemu_get_clock(vm_clock
);
1335 /* To avoid problems with overflow limit this to 2^32. */
1345 #if defined(__linux__) || defined(_WIN32)
1346 static uint64_t qemu_next_deadline_dyntick(void)
1354 delta
= (qemu_next_deadline() + 999) / 1000;
1356 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1357 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1358 qemu_get_clock(rt_clock
))*1000;
1359 if (rtdelta
< delta
)
1363 if (delta
< MIN_TIMER_REARM_US
)
1364 delta
= MIN_TIMER_REARM_US
;
1372 #if defined(__linux__)
1374 #define RTC_FREQ 1024
1376 static void enable_sigio_timer(int fd
)
1378 struct sigaction act
;
1381 sigfillset(&act
.sa_mask
);
1383 act
.sa_handler
= host_alarm_handler
;
1385 sigaction(SIGIO
, &act
, NULL
);
1386 fcntl(fd
, F_SETFL
, O_ASYNC
);
1387 fcntl(fd
, F_SETOWN
, getpid());
1390 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1392 struct hpet_info info
;
1395 fd
= open("/dev/hpet", O_RDONLY
);
1400 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1402 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1403 "error, but for better emulation accuracy type:\n"
1404 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1408 /* Check capabilities */
1409 r
= ioctl(fd
, HPET_INFO
, &info
);
1413 /* Enable periodic mode */
1414 r
= ioctl(fd
, HPET_EPI
, 0);
1415 if (info
.hi_flags
&& (r
< 0))
1418 /* Enable interrupt */
1419 r
= ioctl(fd
, HPET_IE_ON
, 0);
1423 enable_sigio_timer(fd
);
1424 t
->priv
= (void *)(long)fd
;
1432 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1434 int fd
= (long)t
->priv
;
1439 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1442 unsigned long current_rtc_freq
= 0;
1444 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1447 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1448 if (current_rtc_freq
!= RTC_FREQ
&&
1449 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1450 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1451 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1452 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1455 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1461 enable_sigio_timer(rtc_fd
);
1463 t
->priv
= (void *)(long)rtc_fd
;
1468 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1470 int rtc_fd
= (long)t
->priv
;
1475 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1479 struct sigaction act
;
1481 sigfillset(&act
.sa_mask
);
1483 act
.sa_handler
= host_alarm_handler
;
1485 sigaction(SIGALRM
, &act
, NULL
);
1487 ev
.sigev_value
.sival_int
= 0;
1488 ev
.sigev_notify
= SIGEV_SIGNAL
;
1489 ev
.sigev_signo
= SIGALRM
;
1491 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1492 perror("timer_create");
1494 /* disable dynticks */
1495 fprintf(stderr
, "Dynamic Ticks disabled\n");
1500 t
->priv
= (void *)host_timer
;
1505 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1507 timer_t host_timer
= (timer_t
)t
->priv
;
1509 timer_delete(host_timer
);
1512 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1514 timer_t host_timer
= (timer_t
)t
->priv
;
1515 struct itimerspec timeout
;
1516 int64_t nearest_delta_us
= INT64_MAX
;
1519 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1520 !active_timers
[QEMU_TIMER_VIRTUAL
])
1523 nearest_delta_us
= qemu_next_deadline_dyntick();
1525 /* check whether a timer is already running */
1526 if (timer_gettime(host_timer
, &timeout
)) {
1528 fprintf(stderr
, "Internal timer error: aborting\n");
1531 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1532 if (current_us
&& current_us
<= nearest_delta_us
)
1535 timeout
.it_interval
.tv_sec
= 0;
1536 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1537 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1538 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1539 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1541 fprintf(stderr
, "Internal timer error: aborting\n");
1546 #endif /* defined(__linux__) */
1548 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1550 struct sigaction act
;
1551 struct itimerval itv
;
1555 sigfillset(&act
.sa_mask
);
1557 act
.sa_handler
= host_alarm_handler
;
1559 sigaction(SIGALRM
, &act
, NULL
);
1561 itv
.it_interval
.tv_sec
= 0;
1562 /* for i386 kernel 2.6 to get 1 ms */
1563 itv
.it_interval
.tv_usec
= 999;
1564 itv
.it_value
.tv_sec
= 0;
1565 itv
.it_value
.tv_usec
= 10 * 1000;
1567 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1574 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1576 struct itimerval itv
;
1578 memset(&itv
, 0, sizeof(itv
));
1579 setitimer(ITIMER_REAL
, &itv
, NULL
);
1582 #endif /* !defined(_WIN32) */
1586 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1589 struct qemu_alarm_win32
*data
= t
->priv
;
1592 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1593 if (!data
->host_alarm
) {
1594 perror("Failed CreateEvent");
1598 memset(&tc
, 0, sizeof(tc
));
1599 timeGetDevCaps(&tc
, sizeof(tc
));
1601 if (data
->period
< tc
.wPeriodMin
)
1602 data
->period
= tc
.wPeriodMin
;
1604 timeBeginPeriod(data
->period
);
1606 flags
= TIME_CALLBACK_FUNCTION
;
1607 if (alarm_has_dynticks(t
))
1608 flags
|= TIME_ONESHOT
;
1610 flags
|= TIME_PERIODIC
;
1612 data
->timerId
= timeSetEvent(1, // interval (ms)
1613 data
->period
, // resolution
1614 host_alarm_handler
, // function
1615 (DWORD
)t
, // parameter
1618 if (!data
->timerId
) {
1619 perror("Failed to initialize win32 alarm timer");
1621 timeEndPeriod(data
->period
);
1622 CloseHandle(data
->host_alarm
);
1626 qemu_add_wait_object(data
->host_alarm
, NULL
, NULL
);
1631 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1633 struct qemu_alarm_win32
*data
= t
->priv
;
1635 timeKillEvent(data
->timerId
);
1636 timeEndPeriod(data
->period
);
1638 CloseHandle(data
->host_alarm
);
1641 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1643 struct qemu_alarm_win32
*data
= t
->priv
;
1644 uint64_t nearest_delta_us
;
1646 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1647 !active_timers
[QEMU_TIMER_VIRTUAL
])
1650 nearest_delta_us
= qemu_next_deadline_dyntick();
1651 nearest_delta_us
/= 1000;
1653 timeKillEvent(data
->timerId
);
1655 data
->timerId
= timeSetEvent(1,
1659 TIME_ONESHOT
| TIME_PERIODIC
);
1661 if (!data
->timerId
) {
1662 perror("Failed to re-arm win32 alarm timer");
1664 timeEndPeriod(data
->period
);
1665 CloseHandle(data
->host_alarm
);
1672 static void init_timer_alarm(void)
1674 struct qemu_alarm_timer
*t
= NULL
;
1677 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1678 t
= &alarm_timers
[i
];
1686 fprintf(stderr
, "Unable to find any suitable alarm timer.\n");
1687 fprintf(stderr
, "Terminating\n");
1694 static void quit_timers(void)
1696 alarm_timer
->stop(alarm_timer
);
1700 /***********************************************************/
1701 /* host time/date access */
1702 void qemu_get_timedate(struct tm
*tm
, int offset
)
1709 if (rtc_date_offset
== -1) {
1713 ret
= localtime(&ti
);
1715 ti
-= rtc_date_offset
;
1719 memcpy(tm
, ret
, sizeof(struct tm
));
1722 int qemu_timedate_diff(struct tm
*tm
)
1726 if (rtc_date_offset
== -1)
1728 seconds
= mktimegm(tm
);
1730 seconds
= mktime(tm
);
1732 seconds
= mktimegm(tm
) + rtc_date_offset
;
1734 return seconds
- time(NULL
);
1738 static void socket_cleanup(void)
1743 static int socket_init(void)
1748 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1750 err
= WSAGetLastError();
1751 fprintf(stderr
, "WSAStartup: %d\n", err
);
1754 atexit(socket_cleanup
);
1759 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1764 while (*p
!= '\0' && *p
!= '=') {
1765 if (q
&& (q
- buf
) < buf_size
- 1)
1775 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1780 while (*p
!= '\0') {
1782 if (*(p
+ 1) != ',')
1786 if (q
&& (q
- buf
) < buf_size
- 1)
1796 int get_param_value(char *buf
, int buf_size
,
1797 const char *tag
, const char *str
)
1804 p
= get_opt_name(option
, sizeof(option
), p
);
1808 if (!strcmp(tag
, option
)) {
1809 (void)get_opt_value(buf
, buf_size
, p
);
1812 p
= get_opt_value(NULL
, 0, p
);
1821 int check_params(char *buf
, int buf_size
,
1822 const char * const *params
, const char *str
)
1829 p
= get_opt_name(buf
, buf_size
, p
);
1833 for(i
= 0; params
[i
] != NULL
; i
++)
1834 if (!strcmp(params
[i
], buf
))
1836 if (params
[i
] == NULL
)
1838 p
= get_opt_value(NULL
, 0, p
);
1846 /***********************************************************/
1847 /* Bluetooth support */
1850 static struct HCIInfo
*hci_table
[MAX_NICS
];
1852 static struct bt_vlan_s
{
1853 struct bt_scatternet_s net
;
1855 struct bt_vlan_s
*next
;
1858 /* find or alloc a new bluetooth "VLAN" */
1859 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1861 struct bt_vlan_s
**pvlan
, *vlan
;
1862 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1866 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1868 pvlan
= &first_bt_vlan
;
1869 while (*pvlan
!= NULL
)
1870 pvlan
= &(*pvlan
)->next
;
1876 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1880 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1885 static struct HCIInfo null_hci
= {
1886 .cmd_send
= null_hci_send
,
1887 .sco_send
= null_hci_send
,
1888 .acl_send
= null_hci_send
,
1889 .bdaddr_set
= null_hci_addr_set
,
1892 struct HCIInfo
*qemu_next_hci(void)
1894 if (cur_hci
== nb_hcis
)
1897 return hci_table
[cur_hci
++];
1900 /***********************************************************/
1901 /* QEMU Block devices */
1903 #define HD_ALIAS "index=%d,media=disk"
1905 #define CDROM_ALIAS "index=1,media=cdrom"
1907 #define CDROM_ALIAS "index=2,media=cdrom"
1909 #define FD_ALIAS "index=%d,if=floppy"
1910 #define PFLASH_ALIAS "if=pflash"
1911 #define MTD_ALIAS "if=mtd"
1912 #define SD_ALIAS "index=0,if=sd"
1914 static int drive_add(const char *file
, const char *fmt
, ...)
1918 if (nb_drives_opt
>= MAX_DRIVES
) {
1919 fprintf(stderr
, "qemu: too many drives\n");
1923 drives_opt
[nb_drives_opt
].file
= file
;
1925 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
1926 sizeof(drives_opt
[0].opt
), fmt
, ap
);
1929 return nb_drives_opt
++;
1932 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
1936 /* seek interface, bus and unit */
1938 for (index
= 0; index
< nb_drives
; index
++)
1939 if (drives_table
[index
].type
== type
&&
1940 drives_table
[index
].bus
== bus
&&
1941 drives_table
[index
].unit
== unit
)
1947 int drive_get_max_bus(BlockInterfaceType type
)
1953 for (index
= 0; index
< nb_drives
; index
++) {
1954 if(drives_table
[index
].type
== type
&&
1955 drives_table
[index
].bus
> max_bus
)
1956 max_bus
= drives_table
[index
].bus
;
1961 static void bdrv_format_print(void *opaque
, const char *name
)
1963 fprintf(stderr
, " %s", name
);
1966 static int drive_init(struct drive_opt
*arg
, int snapshot
,
1967 QEMUMachine
*machine
)
1972 const char *mediastr
= "";
1973 BlockInterfaceType type
;
1974 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1975 int bus_id
, unit_id
;
1976 int cyls
, heads
, secs
, translation
;
1977 BlockDriverState
*bdrv
;
1978 BlockDriver
*drv
= NULL
;
1983 char *str
= arg
->opt
;
1984 static const char * const params
[] = { "bus", "unit", "if", "index",
1985 "cyls", "heads", "secs", "trans",
1986 "media", "snapshot", "file",
1987 "cache", "format", NULL
};
1989 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
1990 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
1996 cyls
= heads
= secs
= 0;
1999 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2003 if (machine
->use_scsi
) {
2005 max_devs
= MAX_SCSI_DEVS
;
2006 pstrcpy(devname
, sizeof(devname
), "scsi");
2009 max_devs
= MAX_IDE_DEVS
;
2010 pstrcpy(devname
, sizeof(devname
), "ide");
2014 /* extract parameters */
2016 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2017 bus_id
= strtol(buf
, NULL
, 0);
2019 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2024 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2025 unit_id
= strtol(buf
, NULL
, 0);
2027 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2032 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2033 pstrcpy(devname
, sizeof(devname
), buf
);
2034 if (!strcmp(buf
, "ide")) {
2036 max_devs
= MAX_IDE_DEVS
;
2037 } else if (!strcmp(buf
, "scsi")) {
2039 max_devs
= MAX_SCSI_DEVS
;
2040 } else if (!strcmp(buf
, "floppy")) {
2043 } else if (!strcmp(buf
, "pflash")) {
2046 } else if (!strcmp(buf
, "mtd")) {
2049 } else if (!strcmp(buf
, "sd")) {
2053 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2058 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2059 index
= strtol(buf
, NULL
, 0);
2061 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2066 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2067 cyls
= strtol(buf
, NULL
, 0);
2070 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2071 heads
= strtol(buf
, NULL
, 0);
2074 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2075 secs
= strtol(buf
, NULL
, 0);
2078 if (cyls
|| heads
|| secs
) {
2079 if (cyls
< 1 || cyls
> 16383) {
2080 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2083 if (heads
< 1 || heads
> 16) {
2084 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2087 if (secs
< 1 || secs
> 63) {
2088 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2093 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2096 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2100 if (!strcmp(buf
, "none"))
2101 translation
= BIOS_ATA_TRANSLATION_NONE
;
2102 else if (!strcmp(buf
, "lba"))
2103 translation
= BIOS_ATA_TRANSLATION_LBA
;
2104 else if (!strcmp(buf
, "auto"))
2105 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2107 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2112 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2113 if (!strcmp(buf
, "disk")) {
2115 } else if (!strcmp(buf
, "cdrom")) {
2116 if (cyls
|| secs
|| heads
) {
2118 "qemu: '%s' invalid physical CHS format\n", str
);
2121 media
= MEDIA_CDROM
;
2123 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2128 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2129 if (!strcmp(buf
, "on"))
2131 else if (!strcmp(buf
, "off"))
2134 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2139 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2140 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2142 else if (!strcmp(buf
, "writethrough"))
2144 else if (!strcmp(buf
, "writeback"))
2147 fprintf(stderr
, "qemu: invalid cache option\n");
2152 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2153 if (strcmp(buf
, "?") == 0) {
2154 fprintf(stderr
, "qemu: Supported formats:");
2155 bdrv_iterate_format(bdrv_format_print
, NULL
);
2156 fprintf(stderr
, "\n");
2159 drv
= bdrv_find_format(buf
);
2161 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2166 if (arg
->file
== NULL
)
2167 get_param_value(file
, sizeof(file
), "file", str
);
2169 pstrcpy(file
, sizeof(file
), arg
->file
);
2171 /* compute bus and unit according index */
2174 if (bus_id
!= 0 || unit_id
!= -1) {
2176 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2184 unit_id
= index
% max_devs
;
2185 bus_id
= index
/ max_devs
;
2189 /* if user doesn't specify a unit_id,
2190 * try to find the first free
2193 if (unit_id
== -1) {
2195 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2197 if (max_devs
&& unit_id
>= max_devs
) {
2198 unit_id
-= max_devs
;
2206 if (max_devs
&& unit_id
>= max_devs
) {
2207 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2208 str
, unit_id
, max_devs
- 1);
2213 * ignore multiple definitions
2216 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2221 if (type
== IF_IDE
|| type
== IF_SCSI
)
2222 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2224 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2225 devname
, bus_id
, mediastr
, unit_id
);
2227 snprintf(buf
, sizeof(buf
), "%s%s%i",
2228 devname
, mediastr
, unit_id
);
2229 bdrv
= bdrv_new(buf
);
2230 drives_table
[nb_drives
].bdrv
= bdrv
;
2231 drives_table
[nb_drives
].type
= type
;
2232 drives_table
[nb_drives
].bus
= bus_id
;
2233 drives_table
[nb_drives
].unit
= unit_id
;
2242 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2243 bdrv_set_translation_hint(bdrv
, translation
);
2247 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2252 /* FIXME: This isn't really a floppy, but it's a reasonable
2255 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2265 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2266 cache
= 2; /* always use write-back with snapshot */
2268 if (cache
== 0) /* no caching */
2269 bdrv_flags
|= BDRV_O_NOCACHE
;
2270 else if (cache
== 2) /* write-back */
2271 bdrv_flags
|= BDRV_O_CACHE_WB
;
2272 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2273 fprintf(stderr
, "qemu: could not open disk image %s\n",
2280 /***********************************************************/
2283 static USBPort
*used_usb_ports
;
2284 static USBPort
*free_usb_ports
;
2286 /* ??? Maybe change this to register a hub to keep track of the topology. */
2287 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2288 usb_attachfn attach
)
2290 port
->opaque
= opaque
;
2291 port
->index
= index
;
2292 port
->attach
= attach
;
2293 port
->next
= free_usb_ports
;
2294 free_usb_ports
= port
;
2297 int usb_device_add_dev(USBDevice
*dev
)
2301 /* Find a USB port to add the device to. */
2302 port
= free_usb_ports
;
2306 /* Create a new hub and chain it on. */
2307 free_usb_ports
= NULL
;
2308 port
->next
= used_usb_ports
;
2309 used_usb_ports
= port
;
2311 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2312 usb_attach(port
, hub
);
2313 port
= free_usb_ports
;
2316 free_usb_ports
= port
->next
;
2317 port
->next
= used_usb_ports
;
2318 used_usb_ports
= port
;
2319 usb_attach(port
, dev
);
2323 static int usb_device_add(const char *devname
)
2328 if (!free_usb_ports
)
2331 if (strstart(devname
, "host:", &p
)) {
2332 dev
= usb_host_device_open(p
);
2333 } else if (!strcmp(devname
, "mouse")) {
2334 dev
= usb_mouse_init();
2335 } else if (!strcmp(devname
, "tablet")) {
2336 dev
= usb_tablet_init();
2337 } else if (!strcmp(devname
, "keyboard")) {
2338 dev
= usb_keyboard_init();
2339 } else if (strstart(devname
, "disk:", &p
)) {
2340 dev
= usb_msd_init(p
);
2341 } else if (!strcmp(devname
, "wacom-tablet")) {
2342 dev
= usb_wacom_init();
2343 } else if (strstart(devname
, "serial:", &p
)) {
2344 dev
= usb_serial_init(p
);
2345 #ifdef CONFIG_BRLAPI
2346 } else if (!strcmp(devname
, "braille")) {
2347 dev
= usb_baum_init();
2349 } else if (strstart(devname
, "net:", &p
)) {
2352 if (net_client_init("nic", p
) < 0)
2354 nd_table
[nic
].model
= "usb";
2355 dev
= usb_net_init(&nd_table
[nic
]);
2362 return usb_device_add_dev(dev
);
2365 int usb_device_del_addr(int bus_num
, int addr
)
2371 if (!used_usb_ports
)
2377 lastp
= &used_usb_ports
;
2378 port
= used_usb_ports
;
2379 while (port
&& port
->dev
->addr
!= addr
) {
2380 lastp
= &port
->next
;
2388 *lastp
= port
->next
;
2389 usb_attach(port
, NULL
);
2390 dev
->handle_destroy(dev
);
2391 port
->next
= free_usb_ports
;
2392 free_usb_ports
= port
;
2396 static int usb_device_del(const char *devname
)
2401 if (strstart(devname
, "host:", &p
))
2402 return usb_host_device_close(p
);
2404 if (!used_usb_ports
)
2407 p
= strchr(devname
, '.');
2410 bus_num
= strtoul(devname
, NULL
, 0);
2411 addr
= strtoul(p
+ 1, NULL
, 0);
2413 return usb_device_del_addr(bus_num
, addr
);
2416 void do_usb_add(const char *devname
)
2418 usb_device_add(devname
);
2421 void do_usb_del(const char *devname
)
2423 usb_device_del(devname
);
2430 const char *speed_str
;
2433 term_printf("USB support not enabled\n");
2437 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2441 switch(dev
->speed
) {
2445 case USB_SPEED_FULL
:
2448 case USB_SPEED_HIGH
:
2455 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2456 0, dev
->addr
, speed_str
, dev
->devname
);
2460 /***********************************************************/
2461 /* PCMCIA/Cardbus */
2463 static struct pcmcia_socket_entry_s
{
2464 struct pcmcia_socket_s
*socket
;
2465 struct pcmcia_socket_entry_s
*next
;
2466 } *pcmcia_sockets
= 0;
2468 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2470 struct pcmcia_socket_entry_s
*entry
;
2472 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2473 entry
->socket
= socket
;
2474 entry
->next
= pcmcia_sockets
;
2475 pcmcia_sockets
= entry
;
2478 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2480 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2482 ptr
= &pcmcia_sockets
;
2483 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2484 if (entry
->socket
== socket
) {
2490 void pcmcia_info(void)
2492 struct pcmcia_socket_entry_s
*iter
;
2493 if (!pcmcia_sockets
)
2494 term_printf("No PCMCIA sockets\n");
2496 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2497 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2498 iter
->socket
->attached
? iter
->socket
->card_string
:
2502 /***********************************************************/
2505 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2509 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2513 static void dumb_display_init(DisplayState
*ds
)
2518 ds
->dpy_update
= dumb_update
;
2519 ds
->dpy_resize
= dumb_resize
;
2520 ds
->dpy_refresh
= NULL
;
2521 ds
->gui_timer_interval
= 0;
2525 /***********************************************************/
2528 #define MAX_IO_HANDLERS 64
2530 typedef struct IOHandlerRecord
{
2532 IOCanRWHandler
*fd_read_poll
;
2534 IOHandler
*fd_write
;
2537 /* temporary data */
2539 struct IOHandlerRecord
*next
;
2542 static IOHandlerRecord
*first_io_handler
;
2544 /* XXX: fd_read_poll should be suppressed, but an API change is
2545 necessary in the character devices to suppress fd_can_read(). */
2546 int qemu_set_fd_handler2(int fd
,
2547 IOCanRWHandler
*fd_read_poll
,
2549 IOHandler
*fd_write
,
2552 IOHandlerRecord
**pioh
, *ioh
;
2554 if (!fd_read
&& !fd_write
) {
2555 pioh
= &first_io_handler
;
2560 if (ioh
->fd
== fd
) {
2567 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2571 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2574 ioh
->next
= first_io_handler
;
2575 first_io_handler
= ioh
;
2578 ioh
->fd_read_poll
= fd_read_poll
;
2579 ioh
->fd_read
= fd_read
;
2580 ioh
->fd_write
= fd_write
;
2581 ioh
->opaque
= opaque
;
2587 int qemu_set_fd_handler(int fd
,
2589 IOHandler
*fd_write
,
2592 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2596 /***********************************************************/
2597 /* Polling handling */
2599 typedef struct PollingEntry
{
2602 struct PollingEntry
*next
;
2605 static PollingEntry
*first_polling_entry
;
2607 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2609 PollingEntry
**ppe
, *pe
;
2610 pe
= qemu_mallocz(sizeof(PollingEntry
));
2614 pe
->opaque
= opaque
;
2615 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2620 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2622 PollingEntry
**ppe
, *pe
;
2623 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2625 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2633 /***********************************************************/
2634 /* Wait objects support */
2635 typedef struct WaitObjects
{
2637 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2638 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2639 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2642 static WaitObjects wait_objects
= {0};
2644 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2646 WaitObjects
*w
= &wait_objects
;
2648 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2650 w
->events
[w
->num
] = handle
;
2651 w
->func
[w
->num
] = func
;
2652 w
->opaque
[w
->num
] = opaque
;
2657 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2660 WaitObjects
*w
= &wait_objects
;
2663 for (i
= 0; i
< w
->num
; i
++) {
2664 if (w
->events
[i
] == handle
)
2667 w
->events
[i
] = w
->events
[i
+ 1];
2668 w
->func
[i
] = w
->func
[i
+ 1];
2669 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2677 #define SELF_ANNOUNCE_ROUNDS 5
2678 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
2679 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
2680 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
2682 static int announce_self_create(uint8_t *buf
,
2685 uint32_t magic
= EXPERIMENTAL_MAGIC
;
2686 uint16_t proto
= htons(ETH_P_EXPERIMENTAL
);
2688 /* FIXME: should we send a different packet (arp/rarp/ping)? */
2690 memset(buf
, 0xff, 6); /* h_dst */
2691 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
2692 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
2693 memcpy(buf
+ 14, &magic
, 4); /* magic */
2695 return 18; /* len */
2698 void qemu_announce_self(void)
2702 VLANClientState
*vc
;
2705 for (i
= 0; i
< nb_nics
; i
++) {
2706 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
2707 vlan
= nd_table
[i
].vlan
;
2708 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
2709 for (j
=0; j
< SELF_ANNOUNCE_ROUNDS
; j
++)
2710 vc
->fd_read(vc
->opaque
, buf
, len
);
2715 /***********************************************************/
2716 /* savevm/loadvm support */
2718 #define IO_BUF_SIZE 32768
2721 QEMUFilePutBufferFunc
*put_buffer
;
2722 QEMUFileGetBufferFunc
*get_buffer
;
2723 QEMUFileCloseFunc
*close
;
2724 QEMUFileRateLimit
*rate_limit
;
2728 int64_t buf_offset
; /* start of buffer when writing, end of buffer
2731 int buf_size
; /* 0 when writing */
2732 uint8_t buf
[IO_BUF_SIZE
];
2737 typedef struct QEMUFileSocket
2743 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
2745 QEMUFileSocket
*s
= opaque
;
2749 len
= recv(s
->fd
, buf
, size
, 0);
2750 } while (len
== -1 && socket_error() == EINTR
);
2753 len
= -socket_error();
2758 static int socket_close(void *opaque
)
2760 QEMUFileSocket
*s
= opaque
;
2765 QEMUFile
*qemu_fopen_socket(int fd
)
2767 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
2773 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
);
2777 typedef struct QEMUFileStdio
2782 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
2783 int64_t pos
, int size
)
2785 QEMUFileStdio
*s
= opaque
;
2786 fseek(s
->outfile
, pos
, SEEK_SET
);
2787 fwrite(buf
, 1, size
, s
->outfile
);
2791 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
2793 QEMUFileStdio
*s
= opaque
;
2794 fseek(s
->outfile
, pos
, SEEK_SET
);
2795 return fread(buf
, 1, size
, s
->outfile
);
2798 static int file_close(void *opaque
)
2800 QEMUFileStdio
*s
= opaque
;
2806 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
2810 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
2814 s
->outfile
= fopen(filename
, mode
);
2818 if (!strcmp(mode
, "wb"))
2819 return qemu_fopen_ops(s
, file_put_buffer
, NULL
, file_close
, NULL
);
2820 else if (!strcmp(mode
, "rb"))
2821 return qemu_fopen_ops(s
, NULL
, file_get_buffer
, file_close
, NULL
);
2830 typedef struct QEMUFileBdrv
2832 BlockDriverState
*bs
;
2833 int64_t base_offset
;
2836 static int bdrv_put_buffer(void *opaque
, const uint8_t *buf
,
2837 int64_t pos
, int size
)
2839 QEMUFileBdrv
*s
= opaque
;
2840 bdrv_pwrite(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
2844 static int bdrv_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
2846 QEMUFileBdrv
*s
= opaque
;
2847 return bdrv_pread(s
->bs
, s
->base_offset
+ pos
, buf
, size
);
2850 static int bdrv_fclose(void *opaque
)
2852 QEMUFileBdrv
*s
= opaque
;
2857 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int64_t offset
, int is_writable
)
2861 s
= qemu_mallocz(sizeof(QEMUFileBdrv
));
2866 s
->base_offset
= offset
;
2869 return qemu_fopen_ops(s
, bdrv_put_buffer
, NULL
, bdrv_fclose
, NULL
);
2871 return qemu_fopen_ops(s
, NULL
, bdrv_get_buffer
, bdrv_fclose
, NULL
);
2874 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
2875 QEMUFileGetBufferFunc
*get_buffer
,
2876 QEMUFileCloseFunc
*close
,
2877 QEMUFileRateLimit
*rate_limit
)
2881 f
= qemu_mallocz(sizeof(QEMUFile
));
2886 f
->put_buffer
= put_buffer
;
2887 f
->get_buffer
= get_buffer
;
2889 f
->rate_limit
= rate_limit
;
2895 int qemu_file_has_error(QEMUFile
*f
)
2897 return f
->has_error
;
2900 void qemu_fflush(QEMUFile
*f
)
2905 if (f
->is_write
&& f
->buf_index
> 0) {
2908 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
2910 f
->buf_offset
+= f
->buf_index
;
2917 static void qemu_fill_buffer(QEMUFile
*f
)
2927 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
2931 f
->buf_offset
+= len
;
2932 } else if (len
!= -EAGAIN
)
2936 int qemu_fclose(QEMUFile
*f
)
2941 ret
= f
->close(f
->opaque
);
2946 void qemu_file_put_notify(QEMUFile
*f
)
2948 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
2951 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
2955 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
2957 "Attempted to write to buffer while read buffer is not empty\n");
2961 while (!f
->has_error
&& size
> 0) {
2962 l
= IO_BUF_SIZE
- f
->buf_index
;
2965 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
2970 if (f
->buf_index
>= IO_BUF_SIZE
)
2975 void qemu_put_byte(QEMUFile
*f
, int v
)
2977 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
2979 "Attempted to write to buffer while read buffer is not empty\n");
2983 f
->buf
[f
->buf_index
++] = v
;
2985 if (f
->buf_index
>= IO_BUF_SIZE
)
2989 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
2998 l
= f
->buf_size
- f
->buf_index
;
3000 qemu_fill_buffer(f
);
3001 l
= f
->buf_size
- f
->buf_index
;
3007 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
3012 return size1
- size
;
3015 int qemu_get_byte(QEMUFile
*f
)
3020 if (f
->buf_index
>= f
->buf_size
) {
3021 qemu_fill_buffer(f
);
3022 if (f
->buf_index
>= f
->buf_size
)
3025 return f
->buf
[f
->buf_index
++];
3028 int64_t qemu_ftell(QEMUFile
*f
)
3030 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
3033 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
3035 if (whence
== SEEK_SET
) {
3037 } else if (whence
== SEEK_CUR
) {
3038 pos
+= qemu_ftell(f
);
3040 /* SEEK_END not supported */
3043 if (f
->put_buffer
) {
3045 f
->buf_offset
= pos
;
3047 f
->buf_offset
= pos
;
3054 int qemu_file_rate_limit(QEMUFile
*f
)
3057 return f
->rate_limit(f
->opaque
);
3062 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
3064 qemu_put_byte(f
, v
>> 8);
3065 qemu_put_byte(f
, v
);
3068 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
3070 qemu_put_byte(f
, v
>> 24);
3071 qemu_put_byte(f
, v
>> 16);
3072 qemu_put_byte(f
, v
>> 8);
3073 qemu_put_byte(f
, v
);
3076 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
3078 qemu_put_be32(f
, v
>> 32);
3079 qemu_put_be32(f
, v
);
3082 unsigned int qemu_get_be16(QEMUFile
*f
)
3085 v
= qemu_get_byte(f
) << 8;
3086 v
|= qemu_get_byte(f
);
3090 unsigned int qemu_get_be32(QEMUFile
*f
)
3093 v
= qemu_get_byte(f
) << 24;
3094 v
|= qemu_get_byte(f
) << 16;
3095 v
|= qemu_get_byte(f
) << 8;
3096 v
|= qemu_get_byte(f
);
3100 uint64_t qemu_get_be64(QEMUFile
*f
)
3103 v
= (uint64_t)qemu_get_be32(f
) << 32;
3104 v
|= qemu_get_be32(f
);
3108 typedef struct SaveStateEntry
{
3113 SaveLiveStateHandler
*save_live_state
;
3114 SaveStateHandler
*save_state
;
3115 LoadStateHandler
*load_state
;
3117 struct SaveStateEntry
*next
;
3120 static SaveStateEntry
*first_se
;
3122 /* TODO: Individual devices generally have very little idea about the rest
3123 of the system, so instance_id should be removed/replaced.
3124 Meanwhile pass -1 as instance_id if you do not already have a clearly
3125 distinguishing id for all instances of your device class. */
3126 int register_savevm_live(const char *idstr
,
3129 SaveLiveStateHandler
*save_live_state
,
3130 SaveStateHandler
*save_state
,
3131 LoadStateHandler
*load_state
,
3134 SaveStateEntry
*se
, **pse
;
3135 static int global_section_id
;
3137 se
= qemu_malloc(sizeof(SaveStateEntry
));
3140 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
3141 se
->instance_id
= (instance_id
== -1) ? 0 : instance_id
;
3142 se
->version_id
= version_id
;
3143 se
->section_id
= global_section_id
++;
3144 se
->save_live_state
= save_live_state
;
3145 se
->save_state
= save_state
;
3146 se
->load_state
= load_state
;
3147 se
->opaque
= opaque
;
3150 /* add at the end of list */
3152 while (*pse
!= NULL
) {
3153 if (instance_id
== -1
3154 && strcmp(se
->idstr
, (*pse
)->idstr
) == 0
3155 && se
->instance_id
<= (*pse
)->instance_id
)
3156 se
->instance_id
= (*pse
)->instance_id
+ 1;
3157 pse
= &(*pse
)->next
;
3163 int register_savevm(const char *idstr
,
3166 SaveStateHandler
*save_state
,
3167 LoadStateHandler
*load_state
,
3170 return register_savevm_live(idstr
, instance_id
, version_id
,
3171 NULL
, save_state
, load_state
, opaque
);
3174 #define QEMU_VM_FILE_MAGIC 0x5145564d
3175 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
3176 #define QEMU_VM_FILE_VERSION 0x00000003
3178 #define QEMU_VM_EOF 0x00
3179 #define QEMU_VM_SECTION_START 0x01
3180 #define QEMU_VM_SECTION_PART 0x02
3181 #define QEMU_VM_SECTION_END 0x03
3182 #define QEMU_VM_SECTION_FULL 0x04
3184 int qemu_savevm_state_begin(QEMUFile
*f
)
3188 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
3189 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
3191 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
3194 if (se
->save_live_state
== NULL
)
3198 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
3199 qemu_put_be32(f
, se
->section_id
);
3202 len
= strlen(se
->idstr
);
3203 qemu_put_byte(f
, len
);
3204 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
3206 qemu_put_be32(f
, se
->instance_id
);
3207 qemu_put_be32(f
, se
->version_id
);
3209 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
3212 if (qemu_file_has_error(f
))
3218 int qemu_savevm_state_iterate(QEMUFile
*f
)
3223 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
3224 if (se
->save_live_state
== NULL
)
3228 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
3229 qemu_put_be32(f
, se
->section_id
);
3231 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
3237 if (qemu_file_has_error(f
))
3243 int qemu_savevm_state_complete(QEMUFile
*f
)
3247 for (se
= first_se
; se
!= NULL
; se
= se
->next
) {
3248 if (se
->save_live_state
== NULL
)
3252 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
3253 qemu_put_be32(f
, se
->section_id
);
3255 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
3258 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3261 if (se
->save_state
== NULL
)
3265 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
3266 qemu_put_be32(f
, se
->section_id
);
3269 len
= strlen(se
->idstr
);
3270 qemu_put_byte(f
, len
);
3271 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
3273 qemu_put_be32(f
, se
->instance_id
);
3274 qemu_put_be32(f
, se
->version_id
);
3276 se
->save_state(f
, se
->opaque
);
3279 qemu_put_byte(f
, QEMU_VM_EOF
);
3281 if (qemu_file_has_error(f
))
3287 int qemu_savevm_state(QEMUFile
*f
)
3289 int saved_vm_running
;
3292 saved_vm_running
= vm_running
;
3297 ret
= qemu_savevm_state_begin(f
);
3302 ret
= qemu_savevm_state_iterate(f
);
3307 ret
= qemu_savevm_state_complete(f
);
3310 if (qemu_file_has_error(f
))
3313 if (!ret
&& saved_vm_running
)
3319 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
3323 for(se
= first_se
; se
!= NULL
; se
= se
->next
) {
3324 if (!strcmp(se
->idstr
, idstr
) &&
3325 instance_id
== se
->instance_id
)
3331 typedef struct LoadStateEntry
{
3335 struct LoadStateEntry
*next
;
3338 static int qemu_loadvm_state_v2(QEMUFile
*f
)
3341 int len
, ret
, instance_id
, record_len
, version_id
;
3342 int64_t total_len
, end_pos
, cur_pos
;
3345 total_len
= qemu_get_be64(f
);
3346 end_pos
= total_len
+ qemu_ftell(f
);
3348 if (qemu_ftell(f
) >= end_pos
)
3350 len
= qemu_get_byte(f
);
3351 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
3353 instance_id
= qemu_get_be32(f
);
3354 version_id
= qemu_get_be32(f
);
3355 record_len
= qemu_get_be32(f
);
3356 cur_pos
= qemu_ftell(f
);
3357 se
= find_se(idstr
, instance_id
);
3359 fprintf(stderr
, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
3360 instance_id
, idstr
);
3362 ret
= se
->load_state(f
, se
->opaque
, version_id
);
3364 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
3365 instance_id
, idstr
);
3368 /* always seek to exact end of record */
3369 qemu_fseek(f
, cur_pos
+ record_len
, SEEK_SET
);
3372 if (qemu_file_has_error(f
))
3378 int qemu_loadvm_state(QEMUFile
*f
)
3380 LoadStateEntry
*first_le
= NULL
;
3381 uint8_t section_type
;
3385 v
= qemu_get_be32(f
);
3386 if (v
!= QEMU_VM_FILE_MAGIC
)
3389 v
= qemu_get_be32(f
);
3390 if (v
== QEMU_VM_FILE_VERSION_COMPAT
)
3391 return qemu_loadvm_state_v2(f
);
3392 if (v
!= QEMU_VM_FILE_VERSION
)
3395 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
3396 uint32_t instance_id
, version_id
, section_id
;
3402 switch (section_type
) {
3403 case QEMU_VM_SECTION_START
:
3404 case QEMU_VM_SECTION_FULL
:
3405 /* Read section start */
3406 section_id
= qemu_get_be32(f
);
3407 len
= qemu_get_byte(f
);
3408 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
3410 instance_id
= qemu_get_be32(f
);
3411 version_id
= qemu_get_be32(f
);
3413 /* Find savevm section */
3414 se
= find_se(idstr
, instance_id
);
3416 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
3421 /* Validate version */
3422 if (version_id
> se
->version_id
) {
3423 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
3424 version_id
, idstr
, se
->version_id
);
3430 le
= qemu_mallocz(sizeof(*le
));
3437 le
->section_id
= section_id
;
3438 le
->version_id
= version_id
;
3439 le
->next
= first_le
;
3442 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
3444 case QEMU_VM_SECTION_PART
:
3445 case QEMU_VM_SECTION_END
:
3446 section_id
= qemu_get_be32(f
);
3448 for (le
= first_le
; le
&& le
->section_id
!= section_id
; le
= le
->next
);
3450 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
3455 le
->se
->load_state(f
, le
->se
->opaque
, le
->version_id
);
3458 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
3468 LoadStateEntry
*le
= first_le
;
3469 first_le
= first_le
->next
;
3473 if (qemu_file_has_error(f
))
3479 /* device can contain snapshots */
3480 static int bdrv_can_snapshot(BlockDriverState
*bs
)
3483 !bdrv_is_removable(bs
) &&
3484 !bdrv_is_read_only(bs
));
3487 /* device must be snapshots in order to have a reliable snapshot */
3488 static int bdrv_has_snapshot(BlockDriverState
*bs
)
3491 !bdrv_is_removable(bs
) &&
3492 !bdrv_is_read_only(bs
));
3495 static BlockDriverState
*get_bs_snapshots(void)
3497 BlockDriverState
*bs
;
3501 return bs_snapshots
;
3502 for(i
= 0; i
<= nb_drives
; i
++) {
3503 bs
= drives_table
[i
].bdrv
;
3504 if (bdrv_can_snapshot(bs
))
3513 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
3516 QEMUSnapshotInfo
*sn_tab
, *sn
;
3520 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
3523 for(i
= 0; i
< nb_sns
; i
++) {
3525 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
3535 void do_savevm(const char *name
)
3537 BlockDriverState
*bs
, *bs1
;
3538 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
3539 int must_delete
, ret
, i
;
3540 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
3542 int saved_vm_running
;
3549 bs
= get_bs_snapshots();
3551 term_printf("No block device can accept snapshots\n");
3555 /* ??? Should this occur after vm_stop? */
3558 saved_vm_running
= vm_running
;
3563 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
3568 memset(sn
, 0, sizeof(*sn
));
3570 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
3571 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
3574 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
3577 /* fill auxiliary fields */
3580 sn
->date_sec
= tb
.time
;
3581 sn
->date_nsec
= tb
.millitm
* 1000000;
3583 gettimeofday(&tv
, NULL
);
3584 sn
->date_sec
= tv
.tv_sec
;
3585 sn
->date_nsec
= tv
.tv_usec
* 1000;
3587 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
3589 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
3590 term_printf("Device %s does not support VM state snapshots\n",
3591 bdrv_get_device_name(bs
));
3595 /* save the VM state */
3596 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 1);
3598 term_printf("Could not open VM state file\n");
3601 ret
= qemu_savevm_state(f
);
3602 sn
->vm_state_size
= qemu_ftell(f
);
3605 term_printf("Error %d while writing VM\n", ret
);
3609 /* create the snapshots */
3611 for(i
= 0; i
< nb_drives
; i
++) {
3612 bs1
= drives_table
[i
].bdrv
;
3613 if (bdrv_has_snapshot(bs1
)) {
3615 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
3617 term_printf("Error while deleting snapshot on '%s'\n",
3618 bdrv_get_device_name(bs1
));
3621 ret
= bdrv_snapshot_create(bs1
, sn
);
3623 term_printf("Error while creating snapshot on '%s'\n",
3624 bdrv_get_device_name(bs1
));
3630 if (saved_vm_running
)
3634 void do_loadvm(const char *name
)
3636 BlockDriverState
*bs
, *bs1
;
3637 BlockDriverInfo bdi1
, *bdi
= &bdi1
;
3640 int saved_vm_running
;
3642 bs
= get_bs_snapshots();
3644 term_printf("No block device supports snapshots\n");
3648 /* Flush all IO requests so they don't interfere with the new state. */
3651 saved_vm_running
= vm_running
;
3654 for(i
= 0; i
<= nb_drives
; i
++) {
3655 bs1
= drives_table
[i
].bdrv
;
3656 if (bdrv_has_snapshot(bs1
)) {
3657 ret
= bdrv_snapshot_goto(bs1
, name
);
3660 term_printf("Warning: ");
3663 term_printf("Snapshots not supported on device '%s'\n",
3664 bdrv_get_device_name(bs1
));
3667 term_printf("Could not find snapshot '%s' on device '%s'\n",
3668 name
, bdrv_get_device_name(bs1
));
3671 term_printf("Error %d while activating snapshot on '%s'\n",
3672 ret
, bdrv_get_device_name(bs1
));
3675 /* fatal on snapshot block device */
3682 if (bdrv_get_info(bs
, bdi
) < 0 || bdi
->vm_state_offset
<= 0) {
3683 term_printf("Device %s does not support VM state snapshots\n",
3684 bdrv_get_device_name(bs
));
3688 /* restore the VM state */
3689 f
= qemu_fopen_bdrv(bs
, bdi
->vm_state_offset
, 0);
3691 term_printf("Could not open VM state file\n");
3694 ret
= qemu_loadvm_state(f
);
3697 term_printf("Error %d while loading VM state\n", ret
);
3700 if (saved_vm_running
)
3704 void do_delvm(const char *name
)
3706 BlockDriverState
*bs
, *bs1
;
3709 bs
= get_bs_snapshots();
3711 term_printf("No block device supports snapshots\n");
3715 for(i
= 0; i
<= nb_drives
; i
++) {
3716 bs1
= drives_table
[i
].bdrv
;
3717 if (bdrv_has_snapshot(bs1
)) {
3718 ret
= bdrv_snapshot_delete(bs1
, name
);
3720 if (ret
== -ENOTSUP
)
3721 term_printf("Snapshots not supported on device '%s'\n",
3722 bdrv_get_device_name(bs1
));
3724 term_printf("Error %d while deleting snapshot on '%s'\n",
3725 ret
, bdrv_get_device_name(bs1
));
3731 void do_info_snapshots(void)
3733 BlockDriverState
*bs
, *bs1
;
3734 QEMUSnapshotInfo
*sn_tab
, *sn
;
3738 bs
= get_bs_snapshots();
3740 term_printf("No available block device supports snapshots\n");
3743 term_printf("Snapshot devices:");
3744 for(i
= 0; i
<= nb_drives
; i
++) {
3745 bs1
= drives_table
[i
].bdrv
;
3746 if (bdrv_has_snapshot(bs1
)) {
3748 term_printf(" %s", bdrv_get_device_name(bs1
));
3753 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
3755 term_printf("bdrv_snapshot_list: error %d\n", nb_sns
);
3758 term_printf("Snapshot list (from %s):\n", bdrv_get_device_name(bs
));
3759 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
3760 for(i
= 0; i
< nb_sns
; i
++) {
3762 term_printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
3767 /***********************************************************/
3768 /* ram save/restore */
3770 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3774 v
= qemu_get_byte(f
);
3777 if (qemu_get_buffer(f
, buf
, len
) != len
)
3781 v
= qemu_get_byte(f
);
3782 memset(buf
, v
, len
);
3788 if (qemu_file_has_error(f
))
3794 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3799 if (qemu_get_be32(f
) != phys_ram_size
)
3801 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3802 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3809 #define BDRV_HASH_BLOCK_SIZE 1024
3810 #define IOBUF_SIZE 4096
3811 #define RAM_CBLOCK_MAGIC 0xfabe
3813 typedef struct RamDecompressState
{
3816 uint8_t buf
[IOBUF_SIZE
];
3817 } RamDecompressState
;
3819 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3822 memset(s
, 0, sizeof(*s
));
3824 ret
= inflateInit(&s
->zstream
);
3830 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3834 s
->zstream
.avail_out
= len
;
3835 s
->zstream
.next_out
= buf
;
3836 while (s
->zstream
.avail_out
> 0) {
3837 if (s
->zstream
.avail_in
== 0) {
3838 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3840 clen
= qemu_get_be16(s
->f
);
3841 if (clen
> IOBUF_SIZE
)
3843 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3844 s
->zstream
.avail_in
= clen
;
3845 s
->zstream
.next_in
= s
->buf
;
3847 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3848 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3855 static void ram_decompress_close(RamDecompressState
*s
)
3857 inflateEnd(&s
->zstream
);
3860 #define RAM_SAVE_FLAG_FULL 0x01
3861 #define RAM_SAVE_FLAG_COMPRESS 0x02
3862 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3863 #define RAM_SAVE_FLAG_PAGE 0x08
3864 #define RAM_SAVE_FLAG_EOS 0x10
3866 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3868 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3869 uint32_t *array
= (uint32_t *)page
;
3872 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3873 if (array
[i
] != val
)
3880 static int ram_save_block(QEMUFile
*f
)
3882 static ram_addr_t current_addr
= 0;
3883 ram_addr_t saved_addr
= current_addr
;
3884 ram_addr_t addr
= 0;
3887 while (addr
< phys_ram_size
) {
3888 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3891 cpu_physical_memory_reset_dirty(current_addr
,
3892 current_addr
+ TARGET_PAGE_SIZE
,
3893 MIGRATION_DIRTY_FLAG
);
3895 ch
= *(phys_ram_base
+ current_addr
);
3897 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3898 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3899 qemu_put_byte(f
, ch
);
3901 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3902 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3908 addr
+= TARGET_PAGE_SIZE
;
3909 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3915 static ram_addr_t ram_save_threshold
= 10;
3917 static ram_addr_t
ram_save_remaining(void)
3920 ram_addr_t count
= 0;
3922 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3923 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3930 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3935 /* Make sure all dirty bits are set */
3936 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3937 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3938 cpu_physical_memory_set_dirty(addr
);
3941 /* Enable dirty memory tracking */
3942 cpu_physical_memory_set_dirty_tracking(1);
3944 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3947 while (!qemu_file_rate_limit(f
)) {
3950 ret
= ram_save_block(f
);
3951 if (ret
== 0) /* no more blocks */
3955 /* try transferring iterative blocks of memory */
3958 cpu_physical_memory_set_dirty_tracking(0);
3960 /* flush all remaining blocks regardless of rate limiting */
3961 while (ram_save_block(f
) != 0);
3964 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3966 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3969 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3971 RamDecompressState s1
, *s
= &s1
;
3975 if (ram_decompress_open(s
, f
) < 0)
3977 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3978 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3979 fprintf(stderr
, "Error while reading ram block header\n");
3983 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3984 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3989 printf("Error block header\n");
3993 ram_decompress_close(s
);
3998 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
4003 if (version_id
== 1)
4004 return ram_load_v1(f
, opaque
);
4006 if (version_id
== 2) {
4007 if (qemu_get_be32(f
) != phys_ram_size
)
4009 return ram_load_dead(f
, opaque
);
4012 if (version_id
!= 3)
4016 addr
= qemu_get_be64(f
);
4018 flags
= addr
& ~TARGET_PAGE_MASK
;
4019 addr
&= TARGET_PAGE_MASK
;
4021 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
4022 if (addr
!= phys_ram_size
)
4026 if (flags
& RAM_SAVE_FLAG_FULL
) {
4027 if (ram_load_dead(f
, opaque
) < 0)
4031 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
4032 uint8_t ch
= qemu_get_byte(f
);
4033 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
4034 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
4035 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
4036 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
4041 void qemu_service_io(void)
4043 CPUState
*env
= cpu_single_env
;
4045 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
4047 if (env
->kqemu_enabled
) {
4048 kqemu_cpu_interrupt(env
);
4054 /***********************************************************/
4055 /* bottom halves (can be seen as timers which expire ASAP) */
4066 static QEMUBH
*first_bh
= NULL
;
4068 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
4071 bh
= qemu_mallocz(sizeof(QEMUBH
));
4075 bh
->opaque
= opaque
;
4076 bh
->next
= first_bh
;
4081 int qemu_bh_poll(void)
4087 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
4088 if (!bh
->deleted
&& bh
->scheduled
) {
4097 /* remove deleted bhs */
4111 void qemu_bh_schedule_idle(QEMUBH
*bh
)
4119 void qemu_bh_schedule(QEMUBH
*bh
)
4121 CPUState
*env
= cpu_single_env
;
4126 /* stop the currently executing CPU to execute the BH ASAP */
4128 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
4132 void qemu_bh_cancel(QEMUBH
*bh
)
4137 void qemu_bh_delete(QEMUBH
*bh
)
4143 static void qemu_bh_update_timeout(int *timeout
)
4147 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
4148 if (!bh
->deleted
&& bh
->scheduled
) {
4150 /* idle bottom halves will be polled at least
4152 *timeout
= MIN(10, *timeout
);
4154 /* non-idle bottom halves will be executed
4163 /***********************************************************/
4164 /* machine registration */
4166 static QEMUMachine
*first_machine
= NULL
;
4168 int qemu_register_machine(QEMUMachine
*m
)
4171 pm
= &first_machine
;
4179 static QEMUMachine
*find_machine(const char *name
)
4183 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4184 if (!strcmp(m
->name
, name
))
4190 /***********************************************************/
4191 /* main execution loop */
4193 static void gui_update(void *opaque
)
4195 DisplayState
*ds
= opaque
;
4196 ds
->dpy_refresh(ds
);
4197 qemu_mod_timer(ds
->gui_timer
,
4198 (ds
->gui_timer_interval
?
4199 ds
->gui_timer_interval
:
4200 GUI_REFRESH_INTERVAL
)
4201 + qemu_get_clock(rt_clock
));
4204 struct vm_change_state_entry
{
4205 VMChangeStateHandler
*cb
;
4207 LIST_ENTRY (vm_change_state_entry
) entries
;
4210 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
4212 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
4215 VMChangeStateEntry
*e
;
4217 e
= qemu_mallocz(sizeof (*e
));
4223 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
4227 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
4229 LIST_REMOVE (e
, entries
);
4233 static void vm_state_notify(int running
)
4235 VMChangeStateEntry
*e
;
4237 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
4238 e
->cb(e
->opaque
, running
);
4242 /* XXX: support several handlers */
4243 static VMStopHandler
*vm_stop_cb
;
4244 static void *vm_stop_opaque
;
4246 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4249 vm_stop_opaque
= opaque
;
4253 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
4264 qemu_rearm_alarm_timer(alarm_timer
);
4268 void vm_stop(int reason
)
4271 cpu_disable_ticks();
4275 vm_stop_cb(vm_stop_opaque
, reason
);
4282 /* reset/shutdown handler */
4284 typedef struct QEMUResetEntry
{
4285 QEMUResetHandler
*func
;
4287 struct QEMUResetEntry
*next
;
4290 static QEMUResetEntry
*first_reset_entry
;
4291 static int reset_requested
;
4292 static int shutdown_requested
;
4293 static int powerdown_requested
;
4295 int qemu_shutdown_requested(void)
4297 int r
= shutdown_requested
;
4298 shutdown_requested
= 0;
4302 int qemu_reset_requested(void)
4304 int r
= reset_requested
;
4305 reset_requested
= 0;
4309 int qemu_powerdown_requested(void)
4311 int r
= powerdown_requested
;
4312 powerdown_requested
= 0;
4316 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
4318 QEMUResetEntry
**pre
, *re
;
4320 pre
= &first_reset_entry
;
4321 while (*pre
!= NULL
)
4322 pre
= &(*pre
)->next
;
4323 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
4325 re
->opaque
= opaque
;
4330 void qemu_system_reset(void)
4334 /* reset all devices */
4335 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
4336 re
->func(re
->opaque
);
4340 void qemu_system_reset_request(void)
4343 shutdown_requested
= 1;
4345 reset_requested
= 1;
4348 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4351 void qemu_system_shutdown_request(void)
4353 shutdown_requested
= 1;
4355 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4358 void qemu_system_powerdown_request(void)
4360 powerdown_requested
= 1;
4362 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
4366 void host_main_loop_wait(int *timeout
)
4372 /* XXX: need to suppress polling by better using win32 events */
4374 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4375 ret
|= pe
->func(pe
->opaque
);
4379 WaitObjects
*w
= &wait_objects
;
4381 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4382 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4383 if (w
->func
[ret
- WAIT_OBJECT_0
])
4384 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4386 /* Check for additional signaled events */
4387 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4389 /* Check if event is signaled */
4390 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4391 if(ret2
== WAIT_OBJECT_0
) {
4393 w
->func
[i
](w
->opaque
[i
]);
4394 } else if (ret2
== WAIT_TIMEOUT
) {
4396 err
= GetLastError();
4397 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4400 } else if (ret
== WAIT_TIMEOUT
) {
4402 err
= GetLastError();
4403 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4410 void host_main_loop_wait(int *timeout
)
4415 void main_loop_wait(int timeout
)
4417 IOHandlerRecord
*ioh
;
4418 fd_set rfds
, wfds
, xfds
;
4422 qemu_bh_update_timeout(&timeout
);
4424 host_main_loop_wait(&timeout
);
4426 /* poll any events */
4427 /* XXX: separate device handlers from system ones */
4432 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4436 (!ioh
->fd_read_poll
||
4437 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4438 FD_SET(ioh
->fd
, &rfds
);
4442 if (ioh
->fd_write
) {
4443 FD_SET(ioh
->fd
, &wfds
);
4449 tv
.tv_sec
= timeout
/ 1000;
4450 tv
.tv_usec
= (timeout
% 1000) * 1000;
4452 #if defined(CONFIG_SLIRP)
4453 if (slirp_is_inited()) {
4454 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4457 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4459 IOHandlerRecord
**pioh
;
4461 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4462 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4463 ioh
->fd_read(ioh
->opaque
);
4465 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4466 ioh
->fd_write(ioh
->opaque
);
4470 /* remove deleted IO handlers */
4471 pioh
= &first_io_handler
;
4481 #if defined(CONFIG_SLIRP)
4482 if (slirp_is_inited()) {
4488 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4493 if (likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4494 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4495 qemu_get_clock(vm_clock
));
4498 /* real time timers */
4499 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4500 qemu_get_clock(rt_clock
));
4502 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4503 alarm_timer
->flags
&= ~(ALARM_FLAG_EXPIRED
);
4504 qemu_rearm_alarm_timer(alarm_timer
);
4507 /* Check bottom-halves last in case any of the earlier events triggered
4513 static int main_loop(void)
4516 #ifdef CONFIG_PROFILER
4521 cur_cpu
= first_cpu
;
4522 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
4529 #ifdef CONFIG_PROFILER
4530 ti
= profile_getclock();
4535 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4536 env
->icount_decr
.u16
.low
= 0;
4537 env
->icount_extra
= 0;
4538 count
= qemu_next_deadline();
4539 count
= (count
+ (1 << icount_time_shift
) - 1)
4540 >> icount_time_shift
;
4541 qemu_icount
+= count
;
4542 decr
= (count
> 0xffff) ? 0xffff : count
;
4544 env
->icount_decr
.u16
.low
= decr
;
4545 env
->icount_extra
= count
;
4547 ret
= cpu_exec(env
);
4548 #ifdef CONFIG_PROFILER
4549 qemu_time
+= profile_getclock() - ti
;
4552 /* Fold pending instructions back into the
4553 instruction counter, and clear the interrupt flag. */
4554 qemu_icount
-= (env
->icount_decr
.u16
.low
4555 + env
->icount_extra
);
4556 env
->icount_decr
.u32
= 0;
4557 env
->icount_extra
= 0;
4559 next_cpu
= env
->next_cpu
?: first_cpu
;
4560 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
4561 ret
= EXCP_INTERRUPT
;
4565 if (ret
== EXCP_HLT
) {
4566 /* Give the next CPU a chance to run. */
4570 if (ret
!= EXCP_HALTED
)
4572 /* all CPUs are halted ? */
4578 if (shutdown_requested
) {
4579 ret
= EXCP_INTERRUPT
;
4587 if (reset_requested
) {
4588 reset_requested
= 0;
4589 qemu_system_reset();
4590 ret
= EXCP_INTERRUPT
;
4592 if (powerdown_requested
) {
4593 powerdown_requested
= 0;
4594 qemu_system_powerdown();
4595 ret
= EXCP_INTERRUPT
;
4597 if (unlikely(ret
== EXCP_DEBUG
)) {
4598 vm_stop(EXCP_DEBUG
);
4600 /* If all cpus are halted then wait until the next IRQ */
4601 /* XXX: use timeout computed from timers */
4602 if (ret
== EXCP_HALTED
) {
4606 /* Advance virtual time to the next event. */
4607 if (use_icount
== 1) {
4608 /* When not using an adaptive execution frequency
4609 we tend to get badly out of sync with real time,
4610 so just delay for a reasonable amount of time. */
4613 delta
= cpu_get_icount() - cpu_get_clock();
4616 /* If virtual time is ahead of real time then just
4618 timeout
= (delta
/ 1000000) + 1;
4620 /* Wait for either IO to occur or the next
4622 add
= qemu_next_deadline();
4623 /* We advance the timer before checking for IO.
4624 Limit the amount we advance so that early IO
4625 activity won't get the guest too far ahead. */
4629 add
= (add
+ (1 << icount_time_shift
) - 1)
4630 >> icount_time_shift
;
4632 timeout
= delta
/ 1000000;
4643 if (shutdown_requested
) {
4644 ret
= EXCP_INTERRUPT
;
4649 #ifdef CONFIG_PROFILER
4650 ti
= profile_getclock();
4652 main_loop_wait(timeout
);
4653 #ifdef CONFIG_PROFILER
4654 dev_time
+= profile_getclock() - ti
;
4657 cpu_disable_ticks();
4661 static void help(int exitcode
)
4663 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
4664 "usage: %s [options] [disk_image]\n"
4666 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4668 "Standard options:\n"
4669 "-M machine select emulated machine (-M ? for list)\n"
4670 "-cpu cpu select CPU (-cpu ? for list)\n"
4671 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4672 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4673 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4674 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4675 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4676 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
4677 " [,cache=writethrough|writeback|none][,format=f]\n"
4678 " use 'file' as a drive image\n"
4679 "-mtdblock file use 'file' as on-board Flash memory image\n"
4680 "-sd file use 'file' as SecureDigital card image\n"
4681 "-pflash file use 'file' as a parallel flash image\n"
4682 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4683 "-snapshot write to temporary files instead of disk image files\n"
4685 "-no-frame open SDL window without a frame and window decorations\n"
4686 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4687 "-no-quit disable SDL window close capability\n"
4690 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4692 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4693 "-smp n set the number of CPUs to 'n' [default=1]\n"
4694 "-nographic disable graphical output and redirect serial I/Os to console\n"
4695 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4697 "-k language use keyboard layout (for example \"fr\" for French)\n"
4700 "-audio-help print list of audio drivers and their options\n"
4701 "-soundhw c1,... enable audio support\n"
4702 " and only specified sound cards (comma separated list)\n"
4703 " use -soundhw ? to get the list of supported cards\n"
4704 " use -soundhw all to enable all of them\n"
4706 "-vga [std|cirrus|vmware]\n"
4707 " select video card type\n"
4708 "-localtime set the real time clock to local time [default=utc]\n"
4709 "-full-screen start in full screen\n"
4711 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4713 "-usb enable the USB driver (will be the default soon)\n"
4714 "-usbdevice name add the host or guest USB device 'name'\n"
4715 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4716 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4718 "-name string set the name of the guest\n"
4719 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4721 "Network options:\n"
4722 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
4723 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4725 "-net user[,vlan=n][,hostname=host]\n"
4726 " connect the user mode network stack to VLAN 'n' and send\n"
4727 " hostname 'host' to DHCP clients\n"
4730 "-net tap[,vlan=n],ifname=name\n"
4731 " connect the host TAP network interface to VLAN 'n'\n"
4733 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4734 " connect the host TAP network interface to VLAN 'n' and use the\n"
4735 " network scripts 'file' (default=%s)\n"
4736 " and 'dfile' (default=%s);\n"
4737 " use '[down]script=no' to disable script execution;\n"
4738 " use 'fd=h' to connect to an already opened TAP interface\n"
4740 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4741 " connect the vlan 'n' to another VLAN using a socket connection\n"
4742 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4743 " connect the vlan 'n' to multicast maddr and port\n"
4745 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4746 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4747 " on host and listening for incoming connections on 'socketpath'.\n"
4748 " Use group 'groupname' and mode 'octalmode' to change default\n"
4749 " ownership and permissions for communication port.\n"
4751 "-net none use it alone to have zero network devices; if no -net option\n"
4752 " is provided, the default is '-net nic -net user'\n"
4755 "-tftp dir allow tftp access to files in dir [-net user]\n"
4756 "-bootp file advertise file in BOOTP replies\n"
4758 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4760 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4761 " redirect TCP or UDP connections from host to guest [-net user]\n"
4764 "Linux boot specific:\n"
4765 "-kernel bzImage use 'bzImage' as kernel image\n"
4766 "-append cmdline use 'cmdline' as kernel command line\n"
4767 "-initrd file use 'file' as initial ram disk\n"
4769 "Debug/Expert options:\n"
4770 "-monitor dev redirect the monitor to char device 'dev'\n"
4771 "-serial dev redirect the serial port to char device 'dev'\n"
4772 "-parallel dev redirect the parallel port to char device 'dev'\n"
4773 "-pidfile file Write PID to 'file'\n"
4774 "-S freeze CPU at startup (use 'c' to start execution)\n"
4775 "-s wait gdb connection to port\n"
4776 "-p port set gdb connection port [default=%s]\n"
4777 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4778 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4779 " translation (t=none or lba) (usually qemu can guess them)\n"
4780 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4782 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4783 "-no-kqemu disable KQEMU kernel module usage\n"
4786 "-no-acpi disable ACPI\n"
4788 #ifdef CONFIG_CURSES
4789 "-curses use a curses/ncurses interface instead of SDL\n"
4791 "-no-reboot exit instead of rebooting\n"
4792 "-no-shutdown stop before shutdown\n"
4793 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4794 "-vnc display start a VNC server on display\n"
4796 "-daemonize daemonize QEMU after initializing\n"
4798 "-option-rom rom load a file, rom, into the option ROM space\n"
4800 "-prom-env variable=value set OpenBIOS nvram variables\n"
4802 "-clock force the use of the given methods for timer alarm.\n"
4803 " To see what timers are available use -clock ?\n"
4804 "-startdate select initial date of the clock\n"
4805 "-icount [N|auto]\n"
4806 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4808 "During emulation, the following keys are useful:\n"
4809 "ctrl-alt-f toggle full screen\n"
4810 "ctrl-alt-n switch to virtual console 'n'\n"
4811 "ctrl-alt toggle mouse and keyboard grab\n"
4813 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4818 DEFAULT_NETWORK_SCRIPT
,
4819 DEFAULT_NETWORK_DOWN_SCRIPT
,
4821 DEFAULT_GDBSTUB_PORT
,
4826 #define HAS_ARG 0x0001
4841 QEMU_OPTION_mtdblock
,
4845 QEMU_OPTION_snapshot
,
4847 QEMU_OPTION_no_fd_bootchk
,
4850 QEMU_OPTION_nographic
,
4851 QEMU_OPTION_portrait
,
4853 QEMU_OPTION_audio_help
,
4854 QEMU_OPTION_soundhw
,
4875 QEMU_OPTION_localtime
,
4879 QEMU_OPTION_monitor
,
4881 QEMU_OPTION_parallel
,
4883 QEMU_OPTION_full_screen
,
4884 QEMU_OPTION_no_frame
,
4885 QEMU_OPTION_alt_grab
,
4886 QEMU_OPTION_no_quit
,
4887 QEMU_OPTION_pidfile
,
4888 QEMU_OPTION_no_kqemu
,
4889 QEMU_OPTION_kernel_kqemu
,
4890 QEMU_OPTION_win2k_hack
,
4892 QEMU_OPTION_usbdevice
,
4895 QEMU_OPTION_no_acpi
,
4897 QEMU_OPTION_no_reboot
,
4898 QEMU_OPTION_no_shutdown
,
4899 QEMU_OPTION_show_cursor
,
4900 QEMU_OPTION_daemonize
,
4901 QEMU_OPTION_option_rom
,
4902 QEMU_OPTION_semihosting
,
4904 QEMU_OPTION_prom_env
,
4905 QEMU_OPTION_old_param
,
4907 QEMU_OPTION_startdate
,
4908 QEMU_OPTION_tb_size
,
4911 QEMU_OPTION_incoming
,
4914 typedef struct QEMUOption
{
4920 static const QEMUOption qemu_options
[] = {
4921 { "h", 0, QEMU_OPTION_h
},
4922 { "help", 0, QEMU_OPTION_h
},
4924 { "M", HAS_ARG
, QEMU_OPTION_M
},
4925 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4926 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4927 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4928 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4929 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4930 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4931 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4932 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4933 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4934 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4935 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4936 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4937 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4938 { "snapshot", 0, QEMU_OPTION_snapshot
},
4940 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4942 { "m", HAS_ARG
, QEMU_OPTION_m
},
4943 { "nographic", 0, QEMU_OPTION_nographic
},
4944 { "portrait", 0, QEMU_OPTION_portrait
},
4945 { "k", HAS_ARG
, QEMU_OPTION_k
},
4947 { "audio-help", 0, QEMU_OPTION_audio_help
},
4948 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4951 { "net", HAS_ARG
, QEMU_OPTION_net
},
4953 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4954 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4956 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4958 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4961 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4962 { "append", HAS_ARG
, QEMU_OPTION_append
},
4963 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4965 { "S", 0, QEMU_OPTION_S
},
4966 { "s", 0, QEMU_OPTION_s
},
4967 { "p", HAS_ARG
, QEMU_OPTION_p
},
4968 { "d", HAS_ARG
, QEMU_OPTION_d
},
4969 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4970 { "L", HAS_ARG
, QEMU_OPTION_L
},
4971 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4973 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4974 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4976 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4977 { "g", 1, QEMU_OPTION_g
},
4979 { "localtime", 0, QEMU_OPTION_localtime
},
4980 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4981 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4982 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4983 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4984 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4985 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4986 { "full-screen", 0, QEMU_OPTION_full_screen
},
4988 { "no-frame", 0, QEMU_OPTION_no_frame
},
4989 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4990 { "no-quit", 0, QEMU_OPTION_no_quit
},
4992 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4993 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4994 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4995 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4996 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4997 #ifdef CONFIG_CURSES
4998 { "curses", 0, QEMU_OPTION_curses
},
5000 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
5002 /* temporary options */
5003 { "usb", 0, QEMU_OPTION_usb
},
5004 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
5005 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
5006 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
5007 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
5008 { "daemonize", 0, QEMU_OPTION_daemonize
},
5009 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
5010 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5011 { "semihosting", 0, QEMU_OPTION_semihosting
},
5013 { "name", HAS_ARG
, QEMU_OPTION_name
},
5014 #if defined(TARGET_SPARC)
5015 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
5017 #if defined(TARGET_ARM)
5018 { "old-param", 0, QEMU_OPTION_old_param
},
5020 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
5021 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
5022 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
5023 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
5024 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
5028 /* password input */
5030 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
5035 if (!bdrv_is_encrypted(bs
))
5038 term_printf("%s is encrypted.\n", name
);
5039 for(i
= 0; i
< 3; i
++) {
5040 monitor_readline("Password: ", 1, password
, sizeof(password
));
5041 if (bdrv_set_key(bs
, password
) == 0)
5043 term_printf("invalid password\n");
5048 static BlockDriverState
*get_bdrv(int index
)
5050 if (index
> nb_drives
)
5052 return drives_table
[index
].bdrv
;
5055 static void read_passwords(void)
5057 BlockDriverState
*bs
;
5060 for(i
= 0; i
< 6; i
++) {
5063 qemu_key_check(bs
, bdrv_get_device_name(bs
));
5068 struct soundhw soundhw
[] = {
5069 #ifdef HAS_AUDIO_CHOICE
5070 #if defined(TARGET_I386) || defined(TARGET_MIPS)
5076 { .init_isa
= pcspk_audio_init
}
5081 "Creative Sound Blaster 16",
5084 { .init_isa
= SB16_init
}
5087 #ifdef CONFIG_CS4231A
5093 { .init_isa
= cs4231a_init
}
5101 "Yamaha YMF262 (OPL3)",
5103 "Yamaha YM3812 (OPL2)",
5107 { .init_isa
= Adlib_init
}
5114 "Gravis Ultrasound GF1",
5117 { .init_isa
= GUS_init
}
5124 "Intel 82801AA AC97 Audio",
5127 { .init_pci
= ac97_init
}
5133 "ENSONIQ AudioPCI ES1370",
5136 { .init_pci
= es1370_init
}
5140 { NULL
, NULL
, 0, 0, { NULL
} }
5143 static void select_soundhw (const char *optarg
)
5147 if (*optarg
== '?') {
5150 printf ("Valid sound card names (comma separated):\n");
5151 for (c
= soundhw
; c
->name
; ++c
) {
5152 printf ("%-11s %s\n", c
->name
, c
->descr
);
5154 printf ("\n-soundhw all will enable all of the above\n");
5155 exit (*optarg
!= '?');
5163 if (!strcmp (optarg
, "all")) {
5164 for (c
= soundhw
; c
->name
; ++c
) {
5172 e
= strchr (p
, ',');
5173 l
= !e
? strlen (p
) : (size_t) (e
- p
);
5175 for (c
= soundhw
; c
->name
; ++c
) {
5176 if (!strncmp (c
->name
, p
, l
)) {
5185 "Unknown sound card name (too big to show)\n");
5188 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
5193 p
+= l
+ (e
!= NULL
);
5197 goto show_valid_cards
;
5202 static void select_vgahw (const char *p
)
5206 if (strstart(p
, "std", &opts
)) {
5207 cirrus_vga_enabled
= 0;
5209 } else if (strstart(p
, "cirrus", &opts
)) {
5210 cirrus_vga_enabled
= 1;
5212 } else if (strstart(p
, "vmware", &opts
)) {
5213 cirrus_vga_enabled
= 0;
5217 fprintf(stderr
, "Unknown vga type: %s\n", p
);
5221 const char *nextopt
;
5223 if (strstart(opts
, ",retrace=", &nextopt
)) {
5225 if (strstart(opts
, "dumb", &nextopt
))
5226 vga_retrace_method
= VGA_RETRACE_DUMB
;
5227 else if (strstart(opts
, "precise", &nextopt
))
5228 vga_retrace_method
= VGA_RETRACE_PRECISE
;
5229 else goto invalid_vga
;
5230 } else goto invalid_vga
;
5236 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
5238 exit(STATUS_CONTROL_C_EXIT
);
5243 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
5247 if(strlen(str
) != 36)
5250 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
5251 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
5252 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
5260 #define MAX_NET_CLIENTS 32
5264 static void termsig_handler(int signal
)
5266 qemu_system_shutdown_request();
5269 static void termsig_setup(void)
5271 struct sigaction act
;
5273 memset(&act
, 0, sizeof(act
));
5274 act
.sa_handler
= termsig_handler
;
5275 sigaction(SIGINT
, &act
, NULL
);
5276 sigaction(SIGHUP
, &act
, NULL
);
5277 sigaction(SIGTERM
, &act
, NULL
);
5282 int main(int argc
, char **argv
)
5284 #ifdef CONFIG_GDBSTUB
5286 const char *gdbstub_port
;
5288 uint32_t boot_devices_bitmap
= 0;
5290 int snapshot
, linux_boot
, net_boot
;
5291 const char *initrd_filename
;
5292 const char *kernel_filename
, *kernel_cmdline
;
5293 const char *boot_devices
= "";
5294 DisplayState
*ds
= &display_state
;
5295 int cyls
, heads
, secs
, translation
;
5296 const char *net_clients
[MAX_NET_CLIENTS
];
5300 const char *r
, *optarg
;
5301 CharDriverState
*monitor_hd
;
5302 const char *monitor_device
;
5303 const char *serial_devices
[MAX_SERIAL_PORTS
];
5304 int serial_device_index
;
5305 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
5306 int parallel_device_index
;
5307 const char *loadvm
= NULL
;
5308 QEMUMachine
*machine
;
5309 const char *cpu_model
;
5310 const char *usb_devices
[MAX_USB_CMDLINE
];
5311 int usb_devices_index
;
5314 const char *pid_file
= NULL
;
5316 const char *incoming
= NULL
;
5318 LIST_INIT (&vm_change_state_head
);
5321 struct sigaction act
;
5322 sigfillset(&act
.sa_mask
);
5324 act
.sa_handler
= SIG_IGN
;
5325 sigaction(SIGPIPE
, &act
, NULL
);
5328 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
5329 /* Note: cpu_interrupt() is currently not SMP safe, so we force
5330 QEMU to run on a single CPU */
5335 h
= GetCurrentProcess();
5336 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
5337 for(i
= 0; i
< 32; i
++) {
5338 if (mask
& (1 << i
))
5343 SetProcessAffinityMask(h
, mask
);
5349 register_machines();
5350 machine
= first_machine
;
5352 initrd_filename
= NULL
;
5354 vga_ram_size
= VGA_RAM_SIZE
;
5355 #ifdef CONFIG_GDBSTUB
5357 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
5362 kernel_filename
= NULL
;
5363 kernel_cmdline
= "";
5364 cyls
= heads
= secs
= 0;
5365 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5366 monitor_device
= "vc";
5368 serial_devices
[0] = "vc:80Cx24C";
5369 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5370 serial_devices
[i
] = NULL
;
5371 serial_device_index
= 0;
5373 parallel_devices
[0] = "vc:640x480";
5374 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5375 parallel_devices
[i
] = NULL
;
5376 parallel_device_index
= 0;
5378 usb_devices_index
= 0;
5396 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
5398 const QEMUOption
*popt
;
5401 /* Treat --foo the same as -foo. */
5404 popt
= qemu_options
;
5407 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5411 if (!strcmp(popt
->name
, r
+ 1))
5415 if (popt
->flags
& HAS_ARG
) {
5416 if (optind
>= argc
) {
5417 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5421 optarg
= argv
[optind
++];
5426 switch(popt
->index
) {
5428 machine
= find_machine(optarg
);
5431 printf("Supported machines are:\n");
5432 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5433 printf("%-10s %s%s\n",
5435 m
== first_machine
? " (default)" : "");
5437 exit(*optarg
!= '?');
5440 case QEMU_OPTION_cpu
:
5441 /* hw initialization will check this */
5442 if (*optarg
== '?') {
5443 /* XXX: implement xxx_cpu_list for targets that still miss it */
5444 #if defined(cpu_list)
5445 cpu_list(stdout
, &fprintf
);
5452 case QEMU_OPTION_initrd
:
5453 initrd_filename
= optarg
;
5455 case QEMU_OPTION_hda
:
5457 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5459 hda_index
= drive_add(optarg
, HD_ALIAS
5460 ",cyls=%d,heads=%d,secs=%d%s",
5461 0, cyls
, heads
, secs
,
5462 translation
== BIOS_ATA_TRANSLATION_LBA
?
5464 translation
== BIOS_ATA_TRANSLATION_NONE
?
5465 ",trans=none" : "");
5467 case QEMU_OPTION_hdb
:
5468 case QEMU_OPTION_hdc
:
5469 case QEMU_OPTION_hdd
:
5470 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5472 case QEMU_OPTION_drive
:
5473 drive_add(NULL
, "%s", optarg
);
5475 case QEMU_OPTION_mtdblock
:
5476 drive_add(optarg
, MTD_ALIAS
);
5478 case QEMU_OPTION_sd
:
5479 drive_add(optarg
, SD_ALIAS
);
5481 case QEMU_OPTION_pflash
:
5482 drive_add(optarg
, PFLASH_ALIAS
);
5484 case QEMU_OPTION_snapshot
:
5487 case QEMU_OPTION_hdachs
:
5491 cyls
= strtol(p
, (char **)&p
, 0);
5492 if (cyls
< 1 || cyls
> 16383)
5497 heads
= strtol(p
, (char **)&p
, 0);
5498 if (heads
< 1 || heads
> 16)
5503 secs
= strtol(p
, (char **)&p
, 0);
5504 if (secs
< 1 || secs
> 63)
5508 if (!strcmp(p
, "none"))
5509 translation
= BIOS_ATA_TRANSLATION_NONE
;
5510 else if (!strcmp(p
, "lba"))
5511 translation
= BIOS_ATA_TRANSLATION_LBA
;
5512 else if (!strcmp(p
, "auto"))
5513 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5516 } else if (*p
!= '\0') {
5518 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5521 if (hda_index
!= -1)
5522 snprintf(drives_opt
[hda_index
].opt
,
5523 sizeof(drives_opt
[hda_index
].opt
),
5524 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5525 0, cyls
, heads
, secs
,
5526 translation
== BIOS_ATA_TRANSLATION_LBA
?
5528 translation
== BIOS_ATA_TRANSLATION_NONE
?
5529 ",trans=none" : "");
5532 case QEMU_OPTION_nographic
:
5535 #ifdef CONFIG_CURSES
5536 case QEMU_OPTION_curses
:
5540 case QEMU_OPTION_portrait
:
5543 case QEMU_OPTION_kernel
:
5544 kernel_filename
= optarg
;
5546 case QEMU_OPTION_append
:
5547 kernel_cmdline
= optarg
;
5549 case QEMU_OPTION_cdrom
:
5550 drive_add(optarg
, CDROM_ALIAS
);
5552 case QEMU_OPTION_boot
:
5553 boot_devices
= optarg
;
5554 /* We just do some generic consistency checks */
5556 /* Could easily be extended to 64 devices if needed */
5559 boot_devices_bitmap
= 0;
5560 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5561 /* Allowed boot devices are:
5562 * a b : floppy disk drives
5563 * c ... f : IDE disk drives
5564 * g ... m : machine implementation dependant drives
5565 * n ... p : network devices
5566 * It's up to each machine implementation to check
5567 * if the given boot devices match the actual hardware
5568 * implementation and firmware features.
5570 if (*p
< 'a' || *p
> 'q') {
5571 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5574 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5576 "Boot device '%c' was given twice\n",*p
);
5579 boot_devices_bitmap
|= 1 << (*p
- 'a');
5583 case QEMU_OPTION_fda
:
5584 case QEMU_OPTION_fdb
:
5585 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5588 case QEMU_OPTION_no_fd_bootchk
:
5592 case QEMU_OPTION_net
:
5593 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5594 fprintf(stderr
, "qemu: too many network clients\n");
5597 net_clients
[nb_net_clients
] = optarg
;
5601 case QEMU_OPTION_tftp
:
5602 tftp_prefix
= optarg
;
5604 case QEMU_OPTION_bootp
:
5605 bootp_filename
= optarg
;
5608 case QEMU_OPTION_smb
:
5609 net_slirp_smb(optarg
);
5612 case QEMU_OPTION_redir
:
5613 net_slirp_redir(optarg
);
5617 case QEMU_OPTION_audio_help
:
5621 case QEMU_OPTION_soundhw
:
5622 select_soundhw (optarg
);
5628 case QEMU_OPTION_m
: {
5632 value
= strtoul(optarg
, &ptr
, 10);
5634 case 0: case 'M': case 'm':
5641 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5645 /* On 32-bit hosts, QEMU is limited by virtual address space */
5646 if (value
> (2047 << 20)
5648 && HOST_LONG_BITS
== 32
5651 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5654 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5655 fprintf(stderr
, "qemu: ram size too large\n");
5664 const CPULogItem
*item
;
5666 mask
= cpu_str_to_log_mask(optarg
);
5668 printf("Log items (comma separated):\n");
5669 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5670 printf("%-10s %s\n", item
->name
, item
->help
);
5677 #ifdef CONFIG_GDBSTUB
5682 gdbstub_port
= optarg
;
5688 case QEMU_OPTION_bios
:
5695 keyboard_layout
= optarg
;
5697 case QEMU_OPTION_localtime
:
5700 case QEMU_OPTION_vga
:
5701 select_vgahw (optarg
);
5708 w
= strtol(p
, (char **)&p
, 10);
5711 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5717 h
= strtol(p
, (char **)&p
, 10);
5722 depth
= strtol(p
, (char **)&p
, 10);
5723 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5724 depth
!= 24 && depth
!= 32)
5726 } else if (*p
== '\0') {
5727 depth
= graphic_depth
;
5734 graphic_depth
= depth
;
5737 case QEMU_OPTION_echr
:
5740 term_escape_char
= strtol(optarg
, &r
, 0);
5742 printf("Bad argument to echr\n");
5745 case QEMU_OPTION_monitor
:
5746 monitor_device
= optarg
;
5748 case QEMU_OPTION_serial
:
5749 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5750 fprintf(stderr
, "qemu: too many serial ports\n");
5753 serial_devices
[serial_device_index
] = optarg
;
5754 serial_device_index
++;
5756 case QEMU_OPTION_parallel
:
5757 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5758 fprintf(stderr
, "qemu: too many parallel ports\n");
5761 parallel_devices
[parallel_device_index
] = optarg
;
5762 parallel_device_index
++;
5764 case QEMU_OPTION_loadvm
:
5767 case QEMU_OPTION_full_screen
:
5771 case QEMU_OPTION_no_frame
:
5774 case QEMU_OPTION_alt_grab
:
5777 case QEMU_OPTION_no_quit
:
5781 case QEMU_OPTION_pidfile
:
5785 case QEMU_OPTION_win2k_hack
:
5786 win2k_install_hack
= 1;
5790 case QEMU_OPTION_no_kqemu
:
5793 case QEMU_OPTION_kernel_kqemu
:
5797 case QEMU_OPTION_usb
:
5800 case QEMU_OPTION_usbdevice
:
5802 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5803 fprintf(stderr
, "Too many USB devices\n");
5806 usb_devices
[usb_devices_index
] = optarg
;
5807 usb_devices_index
++;
5809 case QEMU_OPTION_smp
:
5810 smp_cpus
= atoi(optarg
);
5812 fprintf(stderr
, "Invalid number of CPUs\n");
5816 case QEMU_OPTION_vnc
:
5817 vnc_display
= optarg
;
5819 case QEMU_OPTION_no_acpi
:
5822 case QEMU_OPTION_no_reboot
:
5825 case QEMU_OPTION_no_shutdown
:
5828 case QEMU_OPTION_show_cursor
:
5831 case QEMU_OPTION_uuid
:
5832 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5833 fprintf(stderr
, "Fail to parse UUID string."
5834 " Wrong format.\n");
5838 case QEMU_OPTION_daemonize
:
5841 case QEMU_OPTION_option_rom
:
5842 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5843 fprintf(stderr
, "Too many option ROMs\n");
5846 option_rom
[nb_option_roms
] = optarg
;
5849 case QEMU_OPTION_semihosting
:
5850 semihosting_enabled
= 1;
5852 case QEMU_OPTION_name
:
5856 case QEMU_OPTION_prom_env
:
5857 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5858 fprintf(stderr
, "Too many prom variables\n");
5861 prom_envs
[nb_prom_envs
] = optarg
;
5866 case QEMU_OPTION_old_param
:
5870 case QEMU_OPTION_clock
:
5871 configure_alarms(optarg
);
5873 case QEMU_OPTION_startdate
:
5876 time_t rtc_start_date
;
5877 if (!strcmp(optarg
, "now")) {
5878 rtc_date_offset
= -1;
5880 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5888 } else if (sscanf(optarg
, "%d-%d-%d",
5891 &tm
.tm_mday
) == 3) {
5900 rtc_start_date
= mktimegm(&tm
);
5901 if (rtc_start_date
== -1) {
5903 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5904 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5907 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5911 case QEMU_OPTION_tb_size
:
5912 tb_size
= strtol(optarg
, NULL
, 0);
5916 case QEMU_OPTION_icount
:
5918 if (strcmp(optarg
, "auto") == 0) {
5919 icount_time_shift
= -1;
5921 icount_time_shift
= strtol(optarg
, NULL
, 0);
5924 case QEMU_OPTION_incoming
:
5931 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5932 if (smp_cpus
> machine
->max_cpus
) {
5933 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5934 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5940 if (serial_device_index
== 0)
5941 serial_devices
[0] = "stdio";
5942 if (parallel_device_index
== 0)
5943 parallel_devices
[0] = "null";
5944 if (strncmp(monitor_device
, "vc", 2) == 0)
5945 monitor_device
= "stdio";
5952 if (pipe(fds
) == -1)
5963 len
= read(fds
[0], &status
, 1);
5964 if (len
== -1 && (errno
== EINTR
))
5969 else if (status
== 1) {
5970 fprintf(stderr
, "Could not acquire pidfile\n");
5987 signal(SIGTSTP
, SIG_IGN
);
5988 signal(SIGTTOU
, SIG_IGN
);
5989 signal(SIGTTIN
, SIG_IGN
);
5993 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5996 write(fds
[1], &status
, 1);
5998 fprintf(stderr
, "Could not acquire pid file\n");
6006 linux_boot
= (kernel_filename
!= NULL
);
6007 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
6009 if (!linux_boot
&& net_boot
== 0 &&
6010 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
6013 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
6014 fprintf(stderr
, "-append only allowed with -kernel option\n");
6018 if (!linux_boot
&& initrd_filename
!= NULL
) {
6019 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
6023 /* boot to floppy or the default cd if no hard disk defined yet */
6024 if (!boot_devices
[0]) {
6025 boot_devices
= "cad";
6027 setvbuf(stdout
, NULL
, _IOLBF
, 0);
6031 if (use_icount
&& icount_time_shift
< 0) {
6033 /* 125MIPS seems a reasonable initial guess at the guest speed.
6034 It will be corrected fairly quickly anyway. */
6035 icount_time_shift
= 3;
6036 init_icount_adjust();
6043 /* init network clients */
6044 if (nb_net_clients
== 0) {
6045 /* if no clients, we use a default config */
6046 net_clients
[nb_net_clients
++] = "nic";
6048 net_clients
[nb_net_clients
++] = "user";
6052 for(i
= 0;i
< nb_net_clients
; i
++) {
6053 if (net_client_parse(net_clients
[i
]) < 0)
6059 /* XXX: this should be moved in the PC machine instantiation code */
6060 if (net_boot
!= 0) {
6062 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
6063 const char *model
= nd_table
[i
].model
;
6065 if (net_boot
& (1 << i
)) {
6068 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
6069 if (get_image_size(buf
) > 0) {
6070 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
6071 fprintf(stderr
, "Too many option ROMs\n");
6074 option_rom
[nb_option_roms
] = strdup(buf
);
6081 fprintf(stderr
, "No valid PXE rom found for network device\n");
6087 /* init the memory */
6088 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
6090 if (machine
->ram_require
& RAMSIZE_FIXED
) {
6092 if (ram_size
< phys_ram_size
) {
6093 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
6094 machine
->name
, (unsigned long long) phys_ram_size
);
6098 phys_ram_size
= ram_size
;
6100 ram_size
= phys_ram_size
;
6103 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
6105 phys_ram_size
+= ram_size
;
6108 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
6109 if (!phys_ram_base
) {
6110 fprintf(stderr
, "Could not allocate physical memory\n");
6114 /* init the dynamic translator */
6115 cpu_exec_init_all(tb_size
* 1024 * 1024);
6119 /* we always create the cdrom drive, even if no disk is there */
6121 if (nb_drives_opt
< MAX_DRIVES
)
6122 drive_add(NULL
, CDROM_ALIAS
);
6124 /* we always create at least one floppy */
6126 if (nb_drives_opt
< MAX_DRIVES
)
6127 drive_add(NULL
, FD_ALIAS
, 0);
6129 /* we always create one sd slot, even if no card is in it */
6131 if (nb_drives_opt
< MAX_DRIVES
)
6132 drive_add(NULL
, SD_ALIAS
);
6134 /* open the virtual block devices */
6136 for(i
= 0; i
< nb_drives_opt
; i
++)
6137 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
6140 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
6141 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
6144 memset(&display_state
, 0, sizeof(display_state
));
6147 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
6150 /* nearly nothing to do */
6151 dumb_display_init(ds
);
6152 } else if (vnc_display
!= NULL
) {
6153 vnc_display_init(ds
);
6154 if (vnc_display_open(ds
, vnc_display
) < 0)
6157 #if defined(CONFIG_CURSES)
6159 curses_display_init(ds
, full_screen
);
6163 #if defined(CONFIG_SDL)
6164 sdl_display_init(ds
, full_screen
, no_frame
);
6165 #elif defined(CONFIG_COCOA)
6166 cocoa_display_init(ds
, full_screen
);
6168 dumb_display_init(ds
);
6173 /* must be after terminal init, SDL library changes signal handlers */
6177 /* Maintain compatibility with multiple stdio monitors */
6178 if (!strcmp(monitor_device
,"stdio")) {
6179 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6180 const char *devname
= serial_devices
[i
];
6181 if (devname
&& !strcmp(devname
,"mon:stdio")) {
6182 monitor_device
= NULL
;
6184 } else if (devname
&& !strcmp(devname
,"stdio")) {
6185 monitor_device
= NULL
;
6186 serial_devices
[i
] = "mon:stdio";
6191 if (monitor_device
) {
6192 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
6194 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6197 monitor_init(monitor_hd
, !nographic
);
6200 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6201 const char *devname
= serial_devices
[i
];
6202 if (devname
&& strcmp(devname
, "none")) {
6204 snprintf(label
, sizeof(label
), "serial%d", i
);
6205 serial_hds
[i
] = qemu_chr_open(label
, devname
);
6206 if (!serial_hds
[i
]) {
6207 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6211 if (strstart(devname
, "vc", 0))
6212 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6216 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6217 const char *devname
= parallel_devices
[i
];
6218 if (devname
&& strcmp(devname
, "none")) {
6220 snprintf(label
, sizeof(label
), "parallel%d", i
);
6221 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
6222 if (!parallel_hds
[i
]) {
6223 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6227 if (strstart(devname
, "vc", 0))
6228 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6232 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
6233 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6235 /* init USB devices */
6237 for(i
= 0; i
< usb_devices_index
; i
++) {
6238 if (usb_device_add(usb_devices
[i
]) < 0) {
6239 fprintf(stderr
, "Warning: could not add USB device %s\n",
6245 if (display_state
.dpy_refresh
) {
6246 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
6247 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
6250 #ifdef CONFIG_GDBSTUB
6252 /* XXX: use standard host:port notation and modify options
6254 if (gdbserver_start(gdbstub_port
) < 0) {
6255 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
6266 autostart
= 0; /* fixme how to deal with -daemonize */
6267 qemu_start_incoming_migration(incoming
);
6271 /* XXX: simplify init */
6284 len
= write(fds
[1], &status
, 1);
6285 if (len
== -1 && (errno
== EINTR
))
6292 TFR(fd
= open("/dev/null", O_RDWR
));