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"
39 #include "cache-utils.h"
41 #include "audio/audio.h"
42 #include "migration.h"
55 #include <sys/times.h>
59 #include <sys/ioctl.h>
60 #include <sys/resource.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
64 #if defined(__NetBSD__)
65 #include <net/if_tap.h>
68 #include <linux/if_tun.h>
70 #include <arpa/inet.h>
73 #include <sys/select.h>
81 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82 #include <freebsd/stdlib.h>
87 #include <linux/rtc.h>
89 /* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91 /* #include <linux/hpet.h> */
94 #include <linux/ppdev.h>
95 #include <linux/parport.h>
99 #include <sys/ethernet.h>
100 #include <sys/sockio.h>
101 #include <netinet/arp.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_icmp.h> // must come after ip.h
106 #include <netinet/udp.h>
107 #include <netinet/tcp.h>
115 #include "qemu_socket.h"
117 #if defined(CONFIG_SLIRP)
118 #include "libslirp.h"
121 #if defined(__OpenBSD__)
125 #if defined(CONFIG_VDE)
126 #include <libvdeplug.h>
131 #include <sys/timeb.h>
132 #include <mmsystem.h>
133 #define getopt_long_only getopt_long
134 #define memalign(align, size) malloc(size)
141 #endif /* CONFIG_SDL */
145 #define main qemu_main
146 #endif /* CONFIG_COCOA */
150 #include "exec-all.h"
152 //#define DEBUG_UNUSED_IOPORT
153 //#define DEBUG_IOPORT
155 //#define DEBUG_SLIRP
159 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
161 # define LOG_IOPORT(...) do { } while (0)
165 #define DEFAULT_RAM_SIZE 144
167 #define DEFAULT_RAM_SIZE 128
170 /* Max number of USB devices that can be specified on the commandline. */
171 #define MAX_USB_CMDLINE 8
173 /* Max number of bluetooth switches on the commandline. */
174 #define MAX_BT_CMDLINE 10
176 /* XXX: use a two level table to limit memory usage */
177 #define MAX_IOPORTS 65536
179 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
180 const char *bios_name
= NULL
;
181 static void *ioport_opaque
[MAX_IOPORTS
];
182 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
183 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
184 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
185 to store the VM snapshots */
186 DriveInfo drives_table
[MAX_DRIVES
+1];
188 static int vga_ram_size
;
189 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
190 static DisplayState
*display_state
;
194 const char* keyboard_layout
= NULL
;
195 int64_t ticks_per_sec
;
198 NICInfo nd_table
[MAX_NICS
];
200 static int rtc_utc
= 1;
201 static int rtc_date_offset
= -1; /* -1 means no change */
202 int cirrus_vga_enabled
= 1;
203 int std_vga_enabled
= 0;
204 int vmsvga_enabled
= 0;
206 int graphic_width
= 1024;
207 int graphic_height
= 768;
208 int graphic_depth
= 8;
210 int graphic_width
= 800;
211 int graphic_height
= 600;
212 int graphic_depth
= 15;
214 static int full_screen
= 0;
216 static int no_frame
= 0;
219 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
220 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
221 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
223 int win2k_install_hack
= 0;
228 const char *vnc_display
;
229 int acpi_enabled
= 1;
235 int graphic_rotate
= 0;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
245 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
246 unsigned int nb_prom_envs
= 0;
247 const char *prom_envs
[MAX_PROM_ENVS
];
249 static int nb_drives_opt
;
250 static struct drive_opt
{
253 } drives_opt
[MAX_DRIVES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int event_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
267 uint8_t qemu_uuid
[16];
269 /***********************************************************/
270 /* x86 ISA bus support */
272 target_phys_addr_t isa_mem_base
= 0;
275 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
276 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
278 static uint32_t ioport_read(int index
, uint32_t address
)
280 static IOPortReadFunc
*default_func
[3] = {
281 default_ioport_readb
,
282 default_ioport_readw
,
285 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
287 func
= default_func
[index
];
288 return func(ioport_opaque
[address
], address
);
291 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
293 static IOPortWriteFunc
*default_func
[3] = {
294 default_ioport_writeb
,
295 default_ioport_writew
,
296 default_ioport_writel
298 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
300 func
= default_func
[index
];
301 func(ioport_opaque
[address
], address
, data
);
304 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
306 #ifdef DEBUG_UNUSED_IOPORT
307 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
312 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
314 #ifdef DEBUG_UNUSED_IOPORT
315 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
319 /* default is to make two byte accesses */
320 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
323 data
= ioport_read(0, address
);
324 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
325 data
|= ioport_read(0, address
) << 8;
329 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
331 ioport_write(0, address
, data
& 0xff);
332 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
333 ioport_write(0, address
, (data
>> 8) & 0xff);
336 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
338 #ifdef DEBUG_UNUSED_IOPORT
339 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
344 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
346 #ifdef DEBUG_UNUSED_IOPORT
347 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
351 /* size is the word size in byte */
352 int register_ioport_read(int start
, int length
, int size
,
353 IOPortReadFunc
*func
, void *opaque
)
359 } else if (size
== 2) {
361 } else if (size
== 4) {
364 hw_error("register_ioport_read: invalid size");
367 for(i
= start
; i
< start
+ length
; i
+= size
) {
368 ioport_read_table
[bsize
][i
] = func
;
369 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
370 hw_error("register_ioport_read: invalid opaque");
371 ioport_opaque
[i
] = opaque
;
376 /* size is the word size in byte */
377 int register_ioport_write(int start
, int length
, int size
,
378 IOPortWriteFunc
*func
, void *opaque
)
384 } else if (size
== 2) {
386 } else if (size
== 4) {
389 hw_error("register_ioport_write: invalid size");
392 for(i
= start
; i
< start
+ length
; i
+= size
) {
393 ioport_write_table
[bsize
][i
] = func
;
394 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
395 hw_error("register_ioport_write: invalid opaque");
396 ioport_opaque
[i
] = opaque
;
401 void isa_unassign_ioport(int start
, int length
)
405 for(i
= start
; i
< start
+ length
; i
++) {
406 ioport_read_table
[0][i
] = default_ioport_readb
;
407 ioport_read_table
[1][i
] = default_ioport_readw
;
408 ioport_read_table
[2][i
] = default_ioport_readl
;
410 ioport_write_table
[0][i
] = default_ioport_writeb
;
411 ioport_write_table
[1][i
] = default_ioport_writew
;
412 ioport_write_table
[2][i
] = default_ioport_writel
;
416 /***********************************************************/
418 void cpu_outb(CPUState
*env
, int addr
, int val
)
420 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
421 ioport_write(0, addr
, val
);
424 env
->last_io_time
= cpu_get_time_fast();
428 void cpu_outw(CPUState
*env
, int addr
, int val
)
430 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
431 ioport_write(1, addr
, val
);
434 env
->last_io_time
= cpu_get_time_fast();
438 void cpu_outl(CPUState
*env
, int addr
, int val
)
440 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
441 ioport_write(2, addr
, val
);
444 env
->last_io_time
= cpu_get_time_fast();
448 int cpu_inb(CPUState
*env
, int addr
)
451 val
= ioport_read(0, addr
);
452 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
455 env
->last_io_time
= cpu_get_time_fast();
460 int cpu_inw(CPUState
*env
, int addr
)
463 val
= ioport_read(1, addr
);
464 LOG_IOPORT("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
);
476 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
479 env
->last_io_time
= cpu_get_time_fast();
484 /***********************************************************/
485 void hw_error(const char *fmt
, ...)
491 fprintf(stderr
, "qemu: hardware error: ");
492 vfprintf(stderr
, fmt
, ap
);
493 fprintf(stderr
, "\n");
494 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
495 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
497 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
499 cpu_dump_state(env
, stderr
, fprintf
, 0);
509 static QEMUBalloonEvent
*qemu_balloon_event
;
510 void *qemu_balloon_event_opaque
;
512 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
514 qemu_balloon_event
= func
;
515 qemu_balloon_event_opaque
= opaque
;
518 void qemu_balloon(ram_addr_t target
)
520 if (qemu_balloon_event
)
521 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
524 ram_addr_t
qemu_balloon_status(void)
526 if (qemu_balloon_event
)
527 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
531 /***********************************************************/
534 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
535 static void *qemu_put_kbd_event_opaque
;
536 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
537 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
539 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
541 qemu_put_kbd_event_opaque
= opaque
;
542 qemu_put_kbd_event
= func
;
545 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
546 void *opaque
, int absolute
,
549 QEMUPutMouseEntry
*s
, *cursor
;
551 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
555 s
->qemu_put_mouse_event
= func
;
556 s
->qemu_put_mouse_event_opaque
= opaque
;
557 s
->qemu_put_mouse_event_absolute
= absolute
;
558 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
561 if (!qemu_put_mouse_event_head
) {
562 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
566 cursor
= qemu_put_mouse_event_head
;
567 while (cursor
->next
!= NULL
)
568 cursor
= cursor
->next
;
571 qemu_put_mouse_event_current
= s
;
576 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
578 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
580 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
583 cursor
= qemu_put_mouse_event_head
;
584 while (cursor
!= NULL
&& cursor
!= entry
) {
586 cursor
= cursor
->next
;
589 if (cursor
== NULL
) // does not exist or list empty
591 else if (prev
== NULL
) { // entry is head
592 qemu_put_mouse_event_head
= cursor
->next
;
593 if (qemu_put_mouse_event_current
== entry
)
594 qemu_put_mouse_event_current
= cursor
->next
;
595 qemu_free(entry
->qemu_put_mouse_event_name
);
600 prev
->next
= entry
->next
;
602 if (qemu_put_mouse_event_current
== entry
)
603 qemu_put_mouse_event_current
= prev
;
605 qemu_free(entry
->qemu_put_mouse_event_name
);
609 void kbd_put_keycode(int keycode
)
611 if (qemu_put_kbd_event
) {
612 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
616 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
618 QEMUPutMouseEvent
*mouse_event
;
619 void *mouse_event_opaque
;
622 if (!qemu_put_mouse_event_current
) {
627 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
629 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
632 if (graphic_rotate
) {
633 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
636 width
= graphic_width
- 1;
637 mouse_event(mouse_event_opaque
,
638 width
- dy
, dx
, dz
, buttons_state
);
640 mouse_event(mouse_event_opaque
,
641 dx
, dy
, dz
, buttons_state
);
645 int kbd_mouse_is_absolute(void)
647 if (!qemu_put_mouse_event_current
)
650 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
653 void do_info_mice(void)
655 QEMUPutMouseEntry
*cursor
;
658 if (!qemu_put_mouse_event_head
) {
659 term_printf("No mouse devices connected\n");
663 term_printf("Mouse devices available:\n");
664 cursor
= qemu_put_mouse_event_head
;
665 while (cursor
!= NULL
) {
666 term_printf("%c Mouse #%d: %s\n",
667 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
668 index
, cursor
->qemu_put_mouse_event_name
);
670 cursor
= cursor
->next
;
674 void do_mouse_set(int index
)
676 QEMUPutMouseEntry
*cursor
;
679 if (!qemu_put_mouse_event_head
) {
680 term_printf("No mouse devices connected\n");
684 cursor
= qemu_put_mouse_event_head
;
685 while (cursor
!= NULL
&& index
!= i
) {
687 cursor
= cursor
->next
;
691 qemu_put_mouse_event_current
= cursor
;
693 term_printf("Mouse at given index not found\n");
696 /* compute with 96 bit intermediate result: (a*b)/c */
697 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
702 #ifdef WORDS_BIGENDIAN
712 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
713 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
716 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
720 /***********************************************************/
721 /* real time host monotonic timer */
723 #define QEMU_TIMER_BASE 1000000000LL
727 static int64_t clock_freq
;
729 static void init_get_clock(void)
733 ret
= QueryPerformanceFrequency(&freq
);
735 fprintf(stderr
, "Could not calibrate ticks\n");
738 clock_freq
= freq
.QuadPart
;
741 static int64_t get_clock(void)
744 QueryPerformanceCounter(&ti
);
745 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
750 static int use_rt_clock
;
752 static void init_get_clock(void)
755 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
758 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
765 static int64_t get_clock(void)
767 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
770 clock_gettime(CLOCK_MONOTONIC
, &ts
);
771 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
775 /* XXX: using gettimeofday leads to problems if the date
776 changes, so it should be avoided. */
778 gettimeofday(&tv
, NULL
);
779 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
784 /* Return the virtual CPU time, based on the instruction counter. */
785 static int64_t cpu_get_icount(void)
788 CPUState
*env
= cpu_single_env
;;
789 icount
= qemu_icount
;
792 fprintf(stderr
, "Bad clock read\n");
793 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
795 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
798 /***********************************************************/
799 /* guest cycle counter */
801 static int64_t cpu_ticks_prev
;
802 static int64_t cpu_ticks_offset
;
803 static int64_t cpu_clock_offset
;
804 static int cpu_ticks_enabled
;
806 /* return the host CPU cycle counter and handle stop/restart */
807 int64_t cpu_get_ticks(void)
810 return cpu_get_icount();
812 if (!cpu_ticks_enabled
) {
813 return cpu_ticks_offset
;
816 ticks
= cpu_get_real_ticks();
817 if (cpu_ticks_prev
> ticks
) {
818 /* Note: non increasing ticks may happen if the host uses
820 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
822 cpu_ticks_prev
= ticks
;
823 return ticks
+ cpu_ticks_offset
;
827 /* return the host CPU monotonic timer and handle stop/restart */
828 static int64_t cpu_get_clock(void)
831 if (!cpu_ticks_enabled
) {
832 return cpu_clock_offset
;
835 return ti
+ cpu_clock_offset
;
839 /* enable cpu_get_ticks() */
840 void cpu_enable_ticks(void)
842 if (!cpu_ticks_enabled
) {
843 cpu_ticks_offset
-= cpu_get_real_ticks();
844 cpu_clock_offset
-= get_clock();
845 cpu_ticks_enabled
= 1;
849 /* disable cpu_get_ticks() : the clock is stopped. You must not call
850 cpu_get_ticks() after that. */
851 void cpu_disable_ticks(void)
853 if (cpu_ticks_enabled
) {
854 cpu_ticks_offset
= cpu_get_ticks();
855 cpu_clock_offset
= cpu_get_clock();
856 cpu_ticks_enabled
= 0;
860 /***********************************************************/
863 #define QEMU_TIMER_REALTIME 0
864 #define QEMU_TIMER_VIRTUAL 1
868 /* XXX: add frequency */
876 struct QEMUTimer
*next
;
879 struct qemu_alarm_timer
{
883 int (*start
)(struct qemu_alarm_timer
*t
);
884 void (*stop
)(struct qemu_alarm_timer
*t
);
885 void (*rearm
)(struct qemu_alarm_timer
*t
);
889 #define ALARM_FLAG_DYNTICKS 0x1
890 #define ALARM_FLAG_EXPIRED 0x2
892 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
894 return t
->flags
& ALARM_FLAG_DYNTICKS
;
897 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
899 if (!alarm_has_dynticks(t
))
905 /* TODO: MIN_TIMER_REARM_US should be optimized */
906 #define MIN_TIMER_REARM_US 250
908 static struct qemu_alarm_timer
*alarm_timer
;
910 static int alarm_timer_rfd
, alarm_timer_wfd
;
915 struct qemu_alarm_win32
{
919 } alarm_win32_data
= {0, NULL
, -1};
921 static int win32_start_timer(struct qemu_alarm_timer
*t
);
922 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
923 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
927 static int unix_start_timer(struct qemu_alarm_timer
*t
);
928 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
932 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
933 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
934 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
936 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
937 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
939 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
940 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
942 #endif /* __linux__ */
946 /* Correlation between real and virtual time is always going to be
947 fairly approximate, so ignore small variation.
948 When the guest is idle real and virtual time will be aligned in
950 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
952 static void icount_adjust(void)
957 static int64_t last_delta
;
958 /* If the VM is not running, then do nothing. */
962 cur_time
= cpu_get_clock();
963 cur_icount
= qemu_get_clock(vm_clock
);
964 delta
= cur_icount
- cur_time
;
965 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
967 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
968 && icount_time_shift
> 0) {
969 /* The guest is getting too far ahead. Slow time down. */
973 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
974 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
975 /* The guest is getting too far behind. Speed time up. */
979 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
982 static void icount_adjust_rt(void * opaque
)
984 qemu_mod_timer(icount_rt_timer
,
985 qemu_get_clock(rt_clock
) + 1000);
989 static void icount_adjust_vm(void * opaque
)
991 qemu_mod_timer(icount_vm_timer
,
992 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
996 static void init_icount_adjust(void)
998 /* Have both realtime and virtual time triggers for speed adjustment.
999 The realtime trigger catches emulated time passing too slowly,
1000 the virtual time trigger catches emulated time passing too fast.
1001 Realtime triggers occur even when idle, so use them less frequently
1002 than VM triggers. */
1003 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1004 qemu_mod_timer(icount_rt_timer
,
1005 qemu_get_clock(rt_clock
) + 1000);
1006 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1007 qemu_mod_timer(icount_vm_timer
,
1008 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1011 static struct qemu_alarm_timer alarm_timers
[] = {
1014 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1015 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1016 /* HPET - if available - is preferred */
1017 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1018 /* ...otherwise try RTC */
1019 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1021 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1023 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1024 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1025 {"win32", 0, win32_start_timer
,
1026 win32_stop_timer
, NULL
, &alarm_win32_data
},
1031 static void show_available_alarms(void)
1035 printf("Available alarm timers, in order of precedence:\n");
1036 for (i
= 0; alarm_timers
[i
].name
; i
++)
1037 printf("%s\n", alarm_timers
[i
].name
);
1040 static void configure_alarms(char const *opt
)
1044 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1047 struct qemu_alarm_timer tmp
;
1049 if (!strcmp(opt
, "?")) {
1050 show_available_alarms();
1056 /* Reorder the array */
1057 name
= strtok(arg
, ",");
1059 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1060 if (!strcmp(alarm_timers
[i
].name
, name
))
1065 fprintf(stderr
, "Unknown clock %s\n", name
);
1074 tmp
= alarm_timers
[i
];
1075 alarm_timers
[i
] = alarm_timers
[cur
];
1076 alarm_timers
[cur
] = tmp
;
1080 name
= strtok(NULL
, ",");
1086 /* Disable remaining timers */
1087 for (i
= cur
; i
< count
; i
++)
1088 alarm_timers
[i
].name
= NULL
;
1090 show_available_alarms();
1095 QEMUClock
*rt_clock
;
1096 QEMUClock
*vm_clock
;
1098 static QEMUTimer
*active_timers
[2];
1100 static QEMUClock
*qemu_new_clock(int type
)
1103 clock
= qemu_mallocz(sizeof(QEMUClock
));
1110 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1114 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1117 ts
->opaque
= opaque
;
1121 void qemu_free_timer(QEMUTimer
*ts
)
1126 /* stop a timer, but do not dealloc it */
1127 void qemu_del_timer(QEMUTimer
*ts
)
1131 /* NOTE: this code must be signal safe because
1132 qemu_timer_expired() can be called from a signal. */
1133 pt
= &active_timers
[ts
->clock
->type
];
1146 /* modify the current timer so that it will be fired when current_time
1147 >= expire_time. The corresponding callback will be called. */
1148 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1154 /* add the timer in the sorted list */
1155 /* NOTE: this code must be signal safe because
1156 qemu_timer_expired() can be called from a signal. */
1157 pt
= &active_timers
[ts
->clock
->type
];
1162 if (t
->expire_time
> expire_time
)
1166 ts
->expire_time
= expire_time
;
1170 /* Rearm if necessary */
1171 if (pt
== &active_timers
[ts
->clock
->type
]) {
1172 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1173 qemu_rearm_alarm_timer(alarm_timer
);
1175 /* Interrupt execution to force deadline recalculation. */
1176 if (use_icount
&& cpu_single_env
) {
1177 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1182 int qemu_timer_pending(QEMUTimer
*ts
)
1185 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1192 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1196 return (timer_head
->expire_time
<= current_time
);
1199 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1205 if (!ts
|| ts
->expire_time
> current_time
)
1207 /* remove timer from the list before calling the callback */
1208 *ptimer_head
= ts
->next
;
1211 /* run the callback (the timer list can be modified) */
1216 int64_t qemu_get_clock(QEMUClock
*clock
)
1218 switch(clock
->type
) {
1219 case QEMU_TIMER_REALTIME
:
1220 return get_clock() / 1000000;
1222 case QEMU_TIMER_VIRTUAL
:
1224 return cpu_get_icount();
1226 return cpu_get_clock();
1231 static void init_timers(void)
1234 ticks_per_sec
= QEMU_TIMER_BASE
;
1235 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1236 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1240 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1242 uint64_t expire_time
;
1244 if (qemu_timer_pending(ts
)) {
1245 expire_time
= ts
->expire_time
;
1249 qemu_put_be64(f
, expire_time
);
1252 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1254 uint64_t expire_time
;
1256 expire_time
= qemu_get_be64(f
);
1257 if (expire_time
!= -1) {
1258 qemu_mod_timer(ts
, expire_time
);
1264 static void timer_save(QEMUFile
*f
, void *opaque
)
1266 if (cpu_ticks_enabled
) {
1267 hw_error("cannot save state if virtual timers are running");
1269 qemu_put_be64(f
, cpu_ticks_offset
);
1270 qemu_put_be64(f
, ticks_per_sec
);
1271 qemu_put_be64(f
, cpu_clock_offset
);
1274 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1276 if (version_id
!= 1 && version_id
!= 2)
1278 if (cpu_ticks_enabled
) {
1281 cpu_ticks_offset
=qemu_get_be64(f
);
1282 ticks_per_sec
=qemu_get_be64(f
);
1283 if (version_id
== 2) {
1284 cpu_clock_offset
=qemu_get_be64(f
);
1290 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1291 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1293 static void host_alarm_handler(int host_signum
)
1297 #define DISP_FREQ 1000
1299 static int64_t delta_min
= INT64_MAX
;
1300 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1302 ti
= qemu_get_clock(vm_clock
);
1303 if (last_clock
!= 0) {
1304 delta
= ti
- last_clock
;
1305 if (delta
< delta_min
)
1307 if (delta
> delta_max
)
1310 if (++count
== DISP_FREQ
) {
1311 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1312 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1313 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1314 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1315 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1317 delta_min
= INT64_MAX
;
1325 if (alarm_has_dynticks(alarm_timer
) ||
1327 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1328 qemu_get_clock(vm_clock
))) ||
1329 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1330 qemu_get_clock(rt_clock
))) {
1331 CPUState
*env
= next_cpu
;
1334 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1335 SetEvent(data
->host_alarm
);
1337 static const char byte
= 0;
1338 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1340 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1343 /* stop the currently executing cpu because a timer occured */
1344 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1346 if (env
->kqemu_enabled
) {
1347 kqemu_cpu_interrupt(env
);
1355 static int64_t qemu_next_deadline(void)
1359 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1360 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1361 qemu_get_clock(vm_clock
);
1363 /* To avoid problems with overflow limit this to 2^32. */
1373 #if defined(__linux__) || defined(_WIN32)
1374 static uint64_t qemu_next_deadline_dyntick(void)
1382 delta
= (qemu_next_deadline() + 999) / 1000;
1384 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1385 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1386 qemu_get_clock(rt_clock
))*1000;
1387 if (rtdelta
< delta
)
1391 if (delta
< MIN_TIMER_REARM_US
)
1392 delta
= MIN_TIMER_REARM_US
;
1400 /* Sets a specific flag */
1401 static int fcntl_setfl(int fd
, int flag
)
1405 flags
= fcntl(fd
, F_GETFL
);
1409 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1415 #if defined(__linux__)
1417 #define RTC_FREQ 1024
1419 static void enable_sigio_timer(int fd
)
1421 struct sigaction act
;
1424 sigfillset(&act
.sa_mask
);
1426 act
.sa_handler
= host_alarm_handler
;
1428 sigaction(SIGIO
, &act
, NULL
);
1429 fcntl_setfl(fd
, O_ASYNC
);
1430 fcntl(fd
, F_SETOWN
, getpid());
1433 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1435 struct hpet_info info
;
1438 fd
= open("/dev/hpet", O_RDONLY
);
1443 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1445 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1446 "error, but for better emulation accuracy type:\n"
1447 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1451 /* Check capabilities */
1452 r
= ioctl(fd
, HPET_INFO
, &info
);
1456 /* Enable periodic mode */
1457 r
= ioctl(fd
, HPET_EPI
, 0);
1458 if (info
.hi_flags
&& (r
< 0))
1461 /* Enable interrupt */
1462 r
= ioctl(fd
, HPET_IE_ON
, 0);
1466 enable_sigio_timer(fd
);
1467 t
->priv
= (void *)(long)fd
;
1475 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1477 int fd
= (long)t
->priv
;
1482 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1485 unsigned long current_rtc_freq
= 0;
1487 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1490 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1491 if (current_rtc_freq
!= RTC_FREQ
&&
1492 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1493 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1494 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1495 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1498 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1504 enable_sigio_timer(rtc_fd
);
1506 t
->priv
= (void *)(long)rtc_fd
;
1511 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1513 int rtc_fd
= (long)t
->priv
;
1518 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1522 struct sigaction act
;
1524 sigfillset(&act
.sa_mask
);
1526 act
.sa_handler
= host_alarm_handler
;
1528 sigaction(SIGALRM
, &act
, NULL
);
1530 ev
.sigev_value
.sival_int
= 0;
1531 ev
.sigev_notify
= SIGEV_SIGNAL
;
1532 ev
.sigev_signo
= SIGALRM
;
1534 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1535 perror("timer_create");
1537 /* disable dynticks */
1538 fprintf(stderr
, "Dynamic Ticks disabled\n");
1543 t
->priv
= (void *)(long)host_timer
;
1548 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1550 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1552 timer_delete(host_timer
);
1555 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1557 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1558 struct itimerspec timeout
;
1559 int64_t nearest_delta_us
= INT64_MAX
;
1562 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1563 !active_timers
[QEMU_TIMER_VIRTUAL
])
1566 nearest_delta_us
= qemu_next_deadline_dyntick();
1568 /* check whether a timer is already running */
1569 if (timer_gettime(host_timer
, &timeout
)) {
1571 fprintf(stderr
, "Internal timer error: aborting\n");
1574 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1575 if (current_us
&& current_us
<= nearest_delta_us
)
1578 timeout
.it_interval
.tv_sec
= 0;
1579 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1580 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1581 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1582 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1584 fprintf(stderr
, "Internal timer error: aborting\n");
1589 #endif /* defined(__linux__) */
1591 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1593 struct sigaction act
;
1594 struct itimerval itv
;
1598 sigfillset(&act
.sa_mask
);
1600 act
.sa_handler
= host_alarm_handler
;
1602 sigaction(SIGALRM
, &act
, NULL
);
1604 itv
.it_interval
.tv_sec
= 0;
1605 /* for i386 kernel 2.6 to get 1 ms */
1606 itv
.it_interval
.tv_usec
= 999;
1607 itv
.it_value
.tv_sec
= 0;
1608 itv
.it_value
.tv_usec
= 10 * 1000;
1610 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1617 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1619 struct itimerval itv
;
1621 memset(&itv
, 0, sizeof(itv
));
1622 setitimer(ITIMER_REAL
, &itv
, NULL
);
1625 #endif /* !defined(_WIN32) */
1627 static void try_to_rearm_timer(void *opaque
)
1629 struct qemu_alarm_timer
*t
= opaque
;
1633 /* Drain the notify pipe */
1636 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1637 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1640 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1641 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1642 qemu_rearm_alarm_timer(alarm_timer
);
1648 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1651 struct qemu_alarm_win32
*data
= t
->priv
;
1654 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1655 if (!data
->host_alarm
) {
1656 perror("Failed CreateEvent");
1660 memset(&tc
, 0, sizeof(tc
));
1661 timeGetDevCaps(&tc
, sizeof(tc
));
1663 if (data
->period
< tc
.wPeriodMin
)
1664 data
->period
= tc
.wPeriodMin
;
1666 timeBeginPeriod(data
->period
);
1668 flags
= TIME_CALLBACK_FUNCTION
;
1669 if (alarm_has_dynticks(t
))
1670 flags
|= TIME_ONESHOT
;
1672 flags
|= TIME_PERIODIC
;
1674 data
->timerId
= timeSetEvent(1, // interval (ms)
1675 data
->period
, // resolution
1676 host_alarm_handler
, // function
1677 (DWORD
)t
, // parameter
1680 if (!data
->timerId
) {
1681 perror("Failed to initialize win32 alarm timer");
1683 timeEndPeriod(data
->period
);
1684 CloseHandle(data
->host_alarm
);
1688 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1693 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1695 struct qemu_alarm_win32
*data
= t
->priv
;
1697 timeKillEvent(data
->timerId
);
1698 timeEndPeriod(data
->period
);
1700 CloseHandle(data
->host_alarm
);
1703 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1705 struct qemu_alarm_win32
*data
= t
->priv
;
1706 uint64_t nearest_delta_us
;
1708 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1709 !active_timers
[QEMU_TIMER_VIRTUAL
])
1712 nearest_delta_us
= qemu_next_deadline_dyntick();
1713 nearest_delta_us
/= 1000;
1715 timeKillEvent(data
->timerId
);
1717 data
->timerId
= timeSetEvent(1,
1721 TIME_ONESHOT
| TIME_PERIODIC
);
1723 if (!data
->timerId
) {
1724 perror("Failed to re-arm win32 alarm timer");
1726 timeEndPeriod(data
->period
);
1727 CloseHandle(data
->host_alarm
);
1734 static int init_timer_alarm(void)
1736 struct qemu_alarm_timer
*t
= NULL
;
1746 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1750 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1754 alarm_timer_rfd
= fds
[0];
1755 alarm_timer_wfd
= fds
[1];
1758 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1759 t
= &alarm_timers
[i
];
1772 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1773 try_to_rearm_timer
, NULL
, t
);
1788 static void quit_timers(void)
1790 alarm_timer
->stop(alarm_timer
);
1794 /***********************************************************/
1795 /* host time/date access */
1796 void qemu_get_timedate(struct tm
*tm
, int offset
)
1803 if (rtc_date_offset
== -1) {
1807 ret
= localtime(&ti
);
1809 ti
-= rtc_date_offset
;
1813 memcpy(tm
, ret
, sizeof(struct tm
));
1816 int qemu_timedate_diff(struct tm
*tm
)
1820 if (rtc_date_offset
== -1)
1822 seconds
= mktimegm(tm
);
1824 seconds
= mktime(tm
);
1826 seconds
= mktimegm(tm
) + rtc_date_offset
;
1828 return seconds
- time(NULL
);
1832 static void socket_cleanup(void)
1837 static int socket_init(void)
1842 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1844 err
= WSAGetLastError();
1845 fprintf(stderr
, "WSAStartup: %d\n", err
);
1848 atexit(socket_cleanup
);
1853 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1858 while (*p
!= '\0' && *p
!= '=') {
1859 if (q
&& (q
- buf
) < buf_size
- 1)
1869 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1874 while (*p
!= '\0') {
1876 if (*(p
+ 1) != ',')
1880 if (q
&& (q
- buf
) < buf_size
- 1)
1890 int get_param_value(char *buf
, int buf_size
,
1891 const char *tag
, const char *str
)
1898 p
= get_opt_name(option
, sizeof(option
), p
);
1902 if (!strcmp(tag
, option
)) {
1903 (void)get_opt_value(buf
, buf_size
, p
);
1906 p
= get_opt_value(NULL
, 0, p
);
1915 int check_params(char *buf
, int buf_size
,
1916 const char * const *params
, const char *str
)
1923 p
= get_opt_name(buf
, buf_size
, p
);
1927 for(i
= 0; params
[i
] != NULL
; i
++)
1928 if (!strcmp(params
[i
], buf
))
1930 if (params
[i
] == NULL
)
1932 p
= get_opt_value(NULL
, 0, p
);
1940 /***********************************************************/
1941 /* Bluetooth support */
1944 static struct HCIInfo
*hci_table
[MAX_NICS
];
1946 static struct bt_vlan_s
{
1947 struct bt_scatternet_s net
;
1949 struct bt_vlan_s
*next
;
1952 /* find or alloc a new bluetooth "VLAN" */
1953 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1955 struct bt_vlan_s
**pvlan
, *vlan
;
1956 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1960 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1962 pvlan
= &first_bt_vlan
;
1963 while (*pvlan
!= NULL
)
1964 pvlan
= &(*pvlan
)->next
;
1969 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1973 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1978 static struct HCIInfo null_hci
= {
1979 .cmd_send
= null_hci_send
,
1980 .sco_send
= null_hci_send
,
1981 .acl_send
= null_hci_send
,
1982 .bdaddr_set
= null_hci_addr_set
,
1985 struct HCIInfo
*qemu_next_hci(void)
1987 if (cur_hci
== nb_hcis
)
1990 return hci_table
[cur_hci
++];
1993 static struct HCIInfo
*hci_init(const char *str
)
1996 struct bt_scatternet_s
*vlan
= 0;
1998 if (!strcmp(str
, "null"))
2001 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2003 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2004 else if (!strncmp(str
, "hci", 3)) {
2007 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2008 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2013 vlan
= qemu_find_bt_vlan(0);
2015 return bt_new_hci(vlan
);
2018 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2023 static int bt_hci_parse(const char *str
)
2025 struct HCIInfo
*hci
;
2028 if (nb_hcis
>= MAX_NICS
) {
2029 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2033 hci
= hci_init(str
);
2042 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2043 hci
->bdaddr_set(hci
, bdaddr
.b
);
2045 hci_table
[nb_hcis
++] = hci
;
2050 static void bt_vhci_add(int vlan_id
)
2052 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2055 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2056 "an empty scatternet %i\n", vlan_id
);
2058 bt_vhci_init(bt_new_hci(vlan
));
2061 static struct bt_device_s
*bt_device_add(const char *opt
)
2063 struct bt_scatternet_s
*vlan
;
2065 char *endp
= strstr(opt
, ",vlan=");
2066 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2069 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2072 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2074 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2079 vlan
= qemu_find_bt_vlan(vlan_id
);
2082 fprintf(stderr
, "qemu: warning: adding a slave device to "
2083 "an empty scatternet %i\n", vlan_id
);
2085 if (!strcmp(devname
, "keyboard"))
2086 return bt_keyboard_init(vlan
);
2088 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2092 static int bt_parse(const char *opt
)
2094 const char *endp
, *p
;
2097 if (strstart(opt
, "hci", &endp
)) {
2098 if (!*endp
|| *endp
== ',') {
2100 if (!strstart(endp
, ",vlan=", 0))
2103 return bt_hci_parse(opt
);
2105 } else if (strstart(opt
, "vhci", &endp
)) {
2106 if (!*endp
|| *endp
== ',') {
2108 if (strstart(endp
, ",vlan=", &p
)) {
2109 vlan
= strtol(p
, (char **) &endp
, 0);
2111 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2115 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2124 } else if (strstart(opt
, "device:", &endp
))
2125 return !bt_device_add(endp
);
2127 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2131 /***********************************************************/
2132 /* QEMU Block devices */
2134 #define HD_ALIAS "index=%d,media=disk"
2136 #define CDROM_ALIAS "index=1,media=cdrom"
2138 #define CDROM_ALIAS "index=2,media=cdrom"
2140 #define FD_ALIAS "index=%d,if=floppy"
2141 #define PFLASH_ALIAS "if=pflash"
2142 #define MTD_ALIAS "if=mtd"
2143 #define SD_ALIAS "index=0,if=sd"
2145 static int drive_add(const char *file
, const char *fmt
, ...)
2149 if (nb_drives_opt
>= MAX_DRIVES
) {
2150 fprintf(stderr
, "qemu: too many drives\n");
2154 drives_opt
[nb_drives_opt
].file
= file
;
2156 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2157 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2160 return nb_drives_opt
++;
2163 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2167 /* seek interface, bus and unit */
2169 for (index
= 0; index
< nb_drives
; index
++)
2170 if (drives_table
[index
].type
== type
&&
2171 drives_table
[index
].bus
== bus
&&
2172 drives_table
[index
].unit
== unit
)
2178 int drive_get_max_bus(BlockInterfaceType type
)
2184 for (index
= 0; index
< nb_drives
; index
++) {
2185 if(drives_table
[index
].type
== type
&&
2186 drives_table
[index
].bus
> max_bus
)
2187 max_bus
= drives_table
[index
].bus
;
2192 const char *drive_get_serial(BlockDriverState
*bdrv
)
2196 for (index
= 0; index
< nb_drives
; index
++)
2197 if (drives_table
[index
].bdrv
== bdrv
)
2198 return drives_table
[index
].serial
;
2203 static void bdrv_format_print(void *opaque
, const char *name
)
2205 fprintf(stderr
, " %s", name
);
2208 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2209 QEMUMachine
*machine
)
2215 const char *mediastr
= "";
2216 BlockInterfaceType type
;
2217 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2218 int bus_id
, unit_id
;
2219 int cyls
, heads
, secs
, translation
;
2220 BlockDriverState
*bdrv
;
2221 BlockDriver
*drv
= NULL
;
2226 char *str
= arg
->opt
;
2227 static const char * const params
[] = { "bus", "unit", "if", "index",
2228 "cyls", "heads", "secs", "trans",
2229 "media", "snapshot", "file",
2230 "cache", "format", "serial", NULL
};
2232 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2233 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2239 cyls
= heads
= secs
= 0;
2242 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2246 if (machine
->use_scsi
) {
2248 max_devs
= MAX_SCSI_DEVS
;
2249 pstrcpy(devname
, sizeof(devname
), "scsi");
2252 max_devs
= MAX_IDE_DEVS
;
2253 pstrcpy(devname
, sizeof(devname
), "ide");
2257 /* extract parameters */
2259 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2260 bus_id
= strtol(buf
, NULL
, 0);
2262 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2267 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2268 unit_id
= strtol(buf
, NULL
, 0);
2270 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2275 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2276 pstrcpy(devname
, sizeof(devname
), buf
);
2277 if (!strcmp(buf
, "ide")) {
2279 max_devs
= MAX_IDE_DEVS
;
2280 } else if (!strcmp(buf
, "scsi")) {
2282 max_devs
= MAX_SCSI_DEVS
;
2283 } else if (!strcmp(buf
, "floppy")) {
2286 } else if (!strcmp(buf
, "pflash")) {
2289 } else if (!strcmp(buf
, "mtd")) {
2292 } else if (!strcmp(buf
, "sd")) {
2295 } else if (!strcmp(buf
, "virtio")) {
2299 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2304 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2305 index
= strtol(buf
, NULL
, 0);
2307 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2312 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2313 cyls
= strtol(buf
, NULL
, 0);
2316 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2317 heads
= strtol(buf
, NULL
, 0);
2320 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2321 secs
= strtol(buf
, NULL
, 0);
2324 if (cyls
|| heads
|| secs
) {
2325 if (cyls
< 1 || cyls
> 16383) {
2326 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2329 if (heads
< 1 || heads
> 16) {
2330 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2333 if (secs
< 1 || secs
> 63) {
2334 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2339 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2342 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2346 if (!strcmp(buf
, "none"))
2347 translation
= BIOS_ATA_TRANSLATION_NONE
;
2348 else if (!strcmp(buf
, "lba"))
2349 translation
= BIOS_ATA_TRANSLATION_LBA
;
2350 else if (!strcmp(buf
, "auto"))
2351 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2353 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2358 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2359 if (!strcmp(buf
, "disk")) {
2361 } else if (!strcmp(buf
, "cdrom")) {
2362 if (cyls
|| secs
|| heads
) {
2364 "qemu: '%s' invalid physical CHS format\n", str
);
2367 media
= MEDIA_CDROM
;
2369 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2374 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2375 if (!strcmp(buf
, "on"))
2377 else if (!strcmp(buf
, "off"))
2380 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2385 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2386 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2388 else if (!strcmp(buf
, "writethrough"))
2390 else if (!strcmp(buf
, "writeback"))
2393 fprintf(stderr
, "qemu: invalid cache option\n");
2398 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2399 if (strcmp(buf
, "?") == 0) {
2400 fprintf(stderr
, "qemu: Supported formats:");
2401 bdrv_iterate_format(bdrv_format_print
, NULL
);
2402 fprintf(stderr
, "\n");
2405 drv
= bdrv_find_format(buf
);
2407 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2412 if (arg
->file
== NULL
)
2413 get_param_value(file
, sizeof(file
), "file", str
);
2415 pstrcpy(file
, sizeof(file
), arg
->file
);
2417 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2418 memset(serial
, 0, sizeof(serial
));
2420 /* compute bus and unit according index */
2423 if (bus_id
!= 0 || unit_id
!= -1) {
2425 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2433 unit_id
= index
% max_devs
;
2434 bus_id
= index
/ max_devs
;
2438 /* if user doesn't specify a unit_id,
2439 * try to find the first free
2442 if (unit_id
== -1) {
2444 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2446 if (max_devs
&& unit_id
>= max_devs
) {
2447 unit_id
-= max_devs
;
2455 if (max_devs
&& unit_id
>= max_devs
) {
2456 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2457 str
, unit_id
, max_devs
- 1);
2462 * ignore multiple definitions
2465 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2470 if (type
== IF_IDE
|| type
== IF_SCSI
)
2471 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2473 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2474 devname
, bus_id
, mediastr
, unit_id
);
2476 snprintf(buf
, sizeof(buf
), "%s%s%i",
2477 devname
, mediastr
, unit_id
);
2478 bdrv
= bdrv_new(buf
);
2479 drives_table
[nb_drives
].bdrv
= bdrv
;
2480 drives_table
[nb_drives
].type
= type
;
2481 drives_table
[nb_drives
].bus
= bus_id
;
2482 drives_table
[nb_drives
].unit
= unit_id
;
2483 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2492 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2493 bdrv_set_translation_hint(bdrv
, translation
);
2497 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2502 /* FIXME: This isn't really a floppy, but it's a reasonable
2505 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2516 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2517 cache
= 2; /* always use write-back with snapshot */
2519 if (cache
== 0) /* no caching */
2520 bdrv_flags
|= BDRV_O_NOCACHE
;
2521 else if (cache
== 2) /* write-back */
2522 bdrv_flags
|= BDRV_O_CACHE_WB
;
2523 else if (cache
== 3) /* not specified */
2524 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2525 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2526 fprintf(stderr
, "qemu: could not open disk image %s\n",
2533 /***********************************************************/
2536 static USBPort
*used_usb_ports
;
2537 static USBPort
*free_usb_ports
;
2539 /* ??? Maybe change this to register a hub to keep track of the topology. */
2540 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2541 usb_attachfn attach
)
2543 port
->opaque
= opaque
;
2544 port
->index
= index
;
2545 port
->attach
= attach
;
2546 port
->next
= free_usb_ports
;
2547 free_usb_ports
= port
;
2550 int usb_device_add_dev(USBDevice
*dev
)
2554 /* Find a USB port to add the device to. */
2555 port
= free_usb_ports
;
2559 /* Create a new hub and chain it on. */
2560 free_usb_ports
= NULL
;
2561 port
->next
= used_usb_ports
;
2562 used_usb_ports
= port
;
2564 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2565 usb_attach(port
, hub
);
2566 port
= free_usb_ports
;
2569 free_usb_ports
= port
->next
;
2570 port
->next
= used_usb_ports
;
2571 used_usb_ports
= port
;
2572 usb_attach(port
, dev
);
2576 static int usb_device_add(const char *devname
)
2581 if (!free_usb_ports
)
2584 if (strstart(devname
, "host:", &p
)) {
2585 dev
= usb_host_device_open(p
);
2586 } else if (!strcmp(devname
, "mouse")) {
2587 dev
= usb_mouse_init();
2588 } else if (!strcmp(devname
, "tablet")) {
2589 dev
= usb_tablet_init();
2590 } else if (!strcmp(devname
, "keyboard")) {
2591 dev
= usb_keyboard_init();
2592 } else if (strstart(devname
, "disk:", &p
)) {
2593 dev
= usb_msd_init(p
);
2594 } else if (!strcmp(devname
, "wacom-tablet")) {
2595 dev
= usb_wacom_init();
2596 } else if (strstart(devname
, "serial:", &p
)) {
2597 dev
= usb_serial_init(p
);
2598 #ifdef CONFIG_BRLAPI
2599 } else if (!strcmp(devname
, "braille")) {
2600 dev
= usb_baum_init();
2602 } else if (strstart(devname
, "net:", &p
)) {
2605 if (net_client_init("nic", p
) < 0)
2607 nd_table
[nic
].model
= "usb";
2608 dev
= usb_net_init(&nd_table
[nic
]);
2609 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2610 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2611 bt_new_hci(qemu_find_bt_vlan(0)));
2618 return usb_device_add_dev(dev
);
2621 int usb_device_del_addr(int bus_num
, int addr
)
2627 if (!used_usb_ports
)
2633 lastp
= &used_usb_ports
;
2634 port
= used_usb_ports
;
2635 while (port
&& port
->dev
->addr
!= addr
) {
2636 lastp
= &port
->next
;
2644 *lastp
= port
->next
;
2645 usb_attach(port
, NULL
);
2646 dev
->handle_destroy(dev
);
2647 port
->next
= free_usb_ports
;
2648 free_usb_ports
= port
;
2652 static int usb_device_del(const char *devname
)
2657 if (strstart(devname
, "host:", &p
))
2658 return usb_host_device_close(p
);
2660 if (!used_usb_ports
)
2663 p
= strchr(devname
, '.');
2666 bus_num
= strtoul(devname
, NULL
, 0);
2667 addr
= strtoul(p
+ 1, NULL
, 0);
2669 return usb_device_del_addr(bus_num
, addr
);
2672 void do_usb_add(const char *devname
)
2674 usb_device_add(devname
);
2677 void do_usb_del(const char *devname
)
2679 usb_device_del(devname
);
2686 const char *speed_str
;
2689 term_printf("USB support not enabled\n");
2693 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2697 switch(dev
->speed
) {
2701 case USB_SPEED_FULL
:
2704 case USB_SPEED_HIGH
:
2711 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2712 0, dev
->addr
, speed_str
, dev
->devname
);
2716 /***********************************************************/
2717 /* PCMCIA/Cardbus */
2719 static struct pcmcia_socket_entry_s
{
2720 struct pcmcia_socket_s
*socket
;
2721 struct pcmcia_socket_entry_s
*next
;
2722 } *pcmcia_sockets
= 0;
2724 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2726 struct pcmcia_socket_entry_s
*entry
;
2728 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2729 entry
->socket
= socket
;
2730 entry
->next
= pcmcia_sockets
;
2731 pcmcia_sockets
= entry
;
2734 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2736 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2738 ptr
= &pcmcia_sockets
;
2739 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2740 if (entry
->socket
== socket
) {
2746 void pcmcia_info(void)
2748 struct pcmcia_socket_entry_s
*iter
;
2749 if (!pcmcia_sockets
)
2750 term_printf("No PCMCIA sockets\n");
2752 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2753 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2754 iter
->socket
->attached
? iter
->socket
->card_string
:
2758 /***********************************************************/
2759 /* register display */
2761 void register_displaystate(DisplayState
*ds
)
2771 DisplayState
*get_displaystate(void)
2773 return display_state
;
2778 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2782 static void dumb_resize(DisplayState
*ds
)
2786 static void dumb_display_init(DisplayState
*ds
)
2788 DisplayChangeListener
*dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2791 dcl
->dpy_update
= dumb_update
;
2792 dcl
->dpy_resize
= dumb_resize
;
2793 dcl
->dpy_refresh
= NULL
;
2795 dcl
->gui_timer_interval
= 500;
2796 register_displaychangelistener(ds
, dcl
);
2799 /***********************************************************/
2802 #define MAX_IO_HANDLERS 64
2804 typedef struct IOHandlerRecord
{
2806 IOCanRWHandler
*fd_read_poll
;
2808 IOHandler
*fd_write
;
2811 /* temporary data */
2813 struct IOHandlerRecord
*next
;
2816 static IOHandlerRecord
*first_io_handler
;
2818 /* XXX: fd_read_poll should be suppressed, but an API change is
2819 necessary in the character devices to suppress fd_can_read(). */
2820 int qemu_set_fd_handler2(int fd
,
2821 IOCanRWHandler
*fd_read_poll
,
2823 IOHandler
*fd_write
,
2826 IOHandlerRecord
**pioh
, *ioh
;
2828 if (!fd_read
&& !fd_write
) {
2829 pioh
= &first_io_handler
;
2834 if (ioh
->fd
== fd
) {
2841 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2845 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2848 ioh
->next
= first_io_handler
;
2849 first_io_handler
= ioh
;
2852 ioh
->fd_read_poll
= fd_read_poll
;
2853 ioh
->fd_read
= fd_read
;
2854 ioh
->fd_write
= fd_write
;
2855 ioh
->opaque
= opaque
;
2861 int qemu_set_fd_handler(int fd
,
2863 IOHandler
*fd_write
,
2866 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2870 /***********************************************************/
2871 /* Polling handling */
2873 typedef struct PollingEntry
{
2876 struct PollingEntry
*next
;
2879 static PollingEntry
*first_polling_entry
;
2881 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2883 PollingEntry
**ppe
, *pe
;
2884 pe
= qemu_mallocz(sizeof(PollingEntry
));
2888 pe
->opaque
= opaque
;
2889 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2894 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2896 PollingEntry
**ppe
, *pe
;
2897 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2899 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2907 /***********************************************************/
2908 /* Wait objects support */
2909 typedef struct WaitObjects
{
2911 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2912 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2913 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2916 static WaitObjects wait_objects
= {0};
2918 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2920 WaitObjects
*w
= &wait_objects
;
2922 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2924 w
->events
[w
->num
] = handle
;
2925 w
->func
[w
->num
] = func
;
2926 w
->opaque
[w
->num
] = opaque
;
2931 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2934 WaitObjects
*w
= &wait_objects
;
2937 for (i
= 0; i
< w
->num
; i
++) {
2938 if (w
->events
[i
] == handle
)
2941 w
->events
[i
] = w
->events
[i
+ 1];
2942 w
->func
[i
] = w
->func
[i
+ 1];
2943 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2951 /***********************************************************/
2952 /* ram save/restore */
2954 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2958 v
= qemu_get_byte(f
);
2961 if (qemu_get_buffer(f
, buf
, len
) != len
)
2965 v
= qemu_get_byte(f
);
2966 memset(buf
, v
, len
);
2972 if (qemu_file_has_error(f
))
2978 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2983 if (qemu_get_be32(f
) != phys_ram_size
)
2985 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2986 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2993 #define BDRV_HASH_BLOCK_SIZE 1024
2994 #define IOBUF_SIZE 4096
2995 #define RAM_CBLOCK_MAGIC 0xfabe
2997 typedef struct RamDecompressState
{
3000 uint8_t buf
[IOBUF_SIZE
];
3001 } RamDecompressState
;
3003 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3006 memset(s
, 0, sizeof(*s
));
3008 ret
= inflateInit(&s
->zstream
);
3014 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3018 s
->zstream
.avail_out
= len
;
3019 s
->zstream
.next_out
= buf
;
3020 while (s
->zstream
.avail_out
> 0) {
3021 if (s
->zstream
.avail_in
== 0) {
3022 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3024 clen
= qemu_get_be16(s
->f
);
3025 if (clen
> IOBUF_SIZE
)
3027 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3028 s
->zstream
.avail_in
= clen
;
3029 s
->zstream
.next_in
= s
->buf
;
3031 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3032 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3039 static void ram_decompress_close(RamDecompressState
*s
)
3041 inflateEnd(&s
->zstream
);
3044 #define RAM_SAVE_FLAG_FULL 0x01
3045 #define RAM_SAVE_FLAG_COMPRESS 0x02
3046 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3047 #define RAM_SAVE_FLAG_PAGE 0x08
3048 #define RAM_SAVE_FLAG_EOS 0x10
3050 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3052 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3053 uint32_t *array
= (uint32_t *)page
;
3056 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3057 if (array
[i
] != val
)
3064 static int ram_save_block(QEMUFile
*f
)
3066 static ram_addr_t current_addr
= 0;
3067 ram_addr_t saved_addr
= current_addr
;
3068 ram_addr_t addr
= 0;
3071 while (addr
< phys_ram_size
) {
3072 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3075 cpu_physical_memory_reset_dirty(current_addr
,
3076 current_addr
+ TARGET_PAGE_SIZE
,
3077 MIGRATION_DIRTY_FLAG
);
3079 ch
= *(phys_ram_base
+ current_addr
);
3081 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3082 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3083 qemu_put_byte(f
, ch
);
3085 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3086 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3092 addr
+= TARGET_PAGE_SIZE
;
3093 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3099 static ram_addr_t ram_save_threshold
= 10;
3101 static ram_addr_t
ram_save_remaining(void)
3104 ram_addr_t count
= 0;
3106 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3107 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3114 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3119 /* Make sure all dirty bits are set */
3120 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3121 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3122 cpu_physical_memory_set_dirty(addr
);
3125 /* Enable dirty memory tracking */
3126 cpu_physical_memory_set_dirty_tracking(1);
3128 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3131 while (!qemu_file_rate_limit(f
)) {
3134 ret
= ram_save_block(f
);
3135 if (ret
== 0) /* no more blocks */
3139 /* try transferring iterative blocks of memory */
3142 cpu_physical_memory_set_dirty_tracking(0);
3144 /* flush all remaining blocks regardless of rate limiting */
3145 while (ram_save_block(f
) != 0);
3148 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3150 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3153 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3155 RamDecompressState s1
, *s
= &s1
;
3159 if (ram_decompress_open(s
, f
) < 0)
3161 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3162 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3163 fprintf(stderr
, "Error while reading ram block header\n");
3167 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3168 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3173 printf("Error block header\n");
3177 ram_decompress_close(s
);
3182 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3187 if (version_id
== 1)
3188 return ram_load_v1(f
, opaque
);
3190 if (version_id
== 2) {
3191 if (qemu_get_be32(f
) != phys_ram_size
)
3193 return ram_load_dead(f
, opaque
);
3196 if (version_id
!= 3)
3200 addr
= qemu_get_be64(f
);
3202 flags
= addr
& ~TARGET_PAGE_MASK
;
3203 addr
&= TARGET_PAGE_MASK
;
3205 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3206 if (addr
!= phys_ram_size
)
3210 if (flags
& RAM_SAVE_FLAG_FULL
) {
3211 if (ram_load_dead(f
, opaque
) < 0)
3215 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3216 uint8_t ch
= qemu_get_byte(f
);
3217 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3218 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3219 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3220 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3225 void qemu_service_io(void)
3227 CPUState
*env
= cpu_single_env
;
3229 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3231 if (env
->kqemu_enabled
) {
3232 kqemu_cpu_interrupt(env
);
3238 /***********************************************************/
3239 /* bottom halves (can be seen as timers which expire ASAP) */
3250 static QEMUBH
*first_bh
= NULL
;
3252 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3255 bh
= qemu_mallocz(sizeof(QEMUBH
));
3259 bh
->opaque
= opaque
;
3260 bh
->next
= first_bh
;
3265 int qemu_bh_poll(void)
3271 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3272 if (!bh
->deleted
&& bh
->scheduled
) {
3281 /* remove deleted bhs */
3295 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3303 void qemu_bh_schedule(QEMUBH
*bh
)
3305 CPUState
*env
= cpu_single_env
;
3310 /* stop the currently executing CPU to execute the BH ASAP */
3312 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3316 void qemu_bh_cancel(QEMUBH
*bh
)
3321 void qemu_bh_delete(QEMUBH
*bh
)
3327 static void qemu_bh_update_timeout(int *timeout
)
3331 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3332 if (!bh
->deleted
&& bh
->scheduled
) {
3334 /* idle bottom halves will be polled at least
3336 *timeout
= MIN(10, *timeout
);
3338 /* non-idle bottom halves will be executed
3347 /***********************************************************/
3348 /* machine registration */
3350 static QEMUMachine
*first_machine
= NULL
;
3352 int qemu_register_machine(QEMUMachine
*m
)
3355 pm
= &first_machine
;
3363 static QEMUMachine
*find_machine(const char *name
)
3367 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3368 if (!strcmp(m
->name
, name
))
3374 /***********************************************************/
3375 /* main execution loop */
3377 static void gui_update(void *opaque
)
3379 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3380 DisplayState
*ds
= opaque
;
3381 DisplayChangeListener
*dcl
= ds
->listeners
;
3385 while (dcl
!= NULL
) {
3386 if (dcl
->gui_timer_interval
&&
3387 dcl
->gui_timer_interval
< interval
)
3388 interval
= dcl
->gui_timer_interval
;
3391 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3394 struct vm_change_state_entry
{
3395 VMChangeStateHandler
*cb
;
3397 LIST_ENTRY (vm_change_state_entry
) entries
;
3400 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3402 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3405 VMChangeStateEntry
*e
;
3407 e
= qemu_mallocz(sizeof (*e
));
3413 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3417 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3419 LIST_REMOVE (e
, entries
);
3423 static void vm_state_notify(int running
)
3425 VMChangeStateEntry
*e
;
3427 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3428 e
->cb(e
->opaque
, running
);
3432 /* XXX: support several handlers */
3433 static VMStopHandler
*vm_stop_cb
;
3434 static void *vm_stop_opaque
;
3436 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3439 vm_stop_opaque
= opaque
;
3443 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3454 qemu_rearm_alarm_timer(alarm_timer
);
3458 void vm_stop(int reason
)
3461 cpu_disable_ticks();
3465 vm_stop_cb(vm_stop_opaque
, reason
);
3472 /* reset/shutdown handler */
3474 typedef struct QEMUResetEntry
{
3475 QEMUResetHandler
*func
;
3477 struct QEMUResetEntry
*next
;
3480 static QEMUResetEntry
*first_reset_entry
;
3481 static int reset_requested
;
3482 static int shutdown_requested
;
3483 static int powerdown_requested
;
3485 int qemu_shutdown_requested(void)
3487 int r
= shutdown_requested
;
3488 shutdown_requested
= 0;
3492 int qemu_reset_requested(void)
3494 int r
= reset_requested
;
3495 reset_requested
= 0;
3499 int qemu_powerdown_requested(void)
3501 int r
= powerdown_requested
;
3502 powerdown_requested
= 0;
3506 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3508 QEMUResetEntry
**pre
, *re
;
3510 pre
= &first_reset_entry
;
3511 while (*pre
!= NULL
)
3512 pre
= &(*pre
)->next
;
3513 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3515 re
->opaque
= opaque
;
3520 void qemu_system_reset(void)
3524 /* reset all devices */
3525 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3526 re
->func(re
->opaque
);
3530 void qemu_system_reset_request(void)
3533 shutdown_requested
= 1;
3535 reset_requested
= 1;
3538 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3541 void qemu_system_shutdown_request(void)
3543 shutdown_requested
= 1;
3545 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3548 void qemu_system_powerdown_request(void)
3550 powerdown_requested
= 1;
3552 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3556 static void host_main_loop_wait(int *timeout
)
3562 /* XXX: need to suppress polling by better using win32 events */
3564 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3565 ret
|= pe
->func(pe
->opaque
);
3569 WaitObjects
*w
= &wait_objects
;
3571 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3572 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3573 if (w
->func
[ret
- WAIT_OBJECT_0
])
3574 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3576 /* Check for additional signaled events */
3577 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3579 /* Check if event is signaled */
3580 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3581 if(ret2
== WAIT_OBJECT_0
) {
3583 w
->func
[i
](w
->opaque
[i
]);
3584 } else if (ret2
== WAIT_TIMEOUT
) {
3586 err
= GetLastError();
3587 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3590 } else if (ret
== WAIT_TIMEOUT
) {
3592 err
= GetLastError();
3593 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3600 static void host_main_loop_wait(int *timeout
)
3605 void main_loop_wait(int timeout
)
3607 IOHandlerRecord
*ioh
;
3608 fd_set rfds
, wfds
, xfds
;
3612 qemu_bh_update_timeout(&timeout
);
3614 host_main_loop_wait(&timeout
);
3616 /* poll any events */
3617 /* XXX: separate device handlers from system ones */
3622 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3626 (!ioh
->fd_read_poll
||
3627 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3628 FD_SET(ioh
->fd
, &rfds
);
3632 if (ioh
->fd_write
) {
3633 FD_SET(ioh
->fd
, &wfds
);
3639 tv
.tv_sec
= timeout
/ 1000;
3640 tv
.tv_usec
= (timeout
% 1000) * 1000;
3642 #if defined(CONFIG_SLIRP)
3643 if (slirp_is_inited()) {
3644 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3647 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3649 IOHandlerRecord
**pioh
;
3651 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3652 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3653 ioh
->fd_read(ioh
->opaque
);
3655 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3656 ioh
->fd_write(ioh
->opaque
);
3660 /* remove deleted IO handlers */
3661 pioh
= &first_io_handler
;
3671 #if defined(CONFIG_SLIRP)
3672 if (slirp_is_inited()) {
3678 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3682 /* vm time timers */
3683 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3684 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3685 qemu_get_clock(vm_clock
));
3687 /* real time timers */
3688 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3689 qemu_get_clock(rt_clock
));
3691 /* Check bottom-halves last in case any of the earlier events triggered
3697 static int main_loop(void)
3700 #ifdef CONFIG_PROFILER
3705 cur_cpu
= first_cpu
;
3706 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3713 #ifdef CONFIG_PROFILER
3714 ti
= profile_getclock();
3719 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3720 env
->icount_decr
.u16
.low
= 0;
3721 env
->icount_extra
= 0;
3722 count
= qemu_next_deadline();
3723 count
= (count
+ (1 << icount_time_shift
) - 1)
3724 >> icount_time_shift
;
3725 qemu_icount
+= count
;
3726 decr
= (count
> 0xffff) ? 0xffff : count
;
3728 env
->icount_decr
.u16
.low
= decr
;
3729 env
->icount_extra
= count
;
3731 ret
= cpu_exec(env
);
3732 #ifdef CONFIG_PROFILER
3733 qemu_time
+= profile_getclock() - ti
;
3736 /* Fold pending instructions back into the
3737 instruction counter, and clear the interrupt flag. */
3738 qemu_icount
-= (env
->icount_decr
.u16
.low
3739 + env
->icount_extra
);
3740 env
->icount_decr
.u32
= 0;
3741 env
->icount_extra
= 0;
3743 next_cpu
= env
->next_cpu
?: first_cpu
;
3744 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3745 ret
= EXCP_INTERRUPT
;
3749 if (ret
== EXCP_HLT
) {
3750 /* Give the next CPU a chance to run. */
3754 if (ret
!= EXCP_HALTED
)
3756 /* all CPUs are halted ? */
3762 if (shutdown_requested
) {
3763 ret
= EXCP_INTERRUPT
;
3771 if (reset_requested
) {
3772 reset_requested
= 0;
3773 qemu_system_reset();
3774 ret
= EXCP_INTERRUPT
;
3776 if (powerdown_requested
) {
3777 powerdown_requested
= 0;
3778 qemu_system_powerdown();
3779 ret
= EXCP_INTERRUPT
;
3781 if (unlikely(ret
== EXCP_DEBUG
)) {
3782 gdb_set_stop_cpu(cur_cpu
);
3783 vm_stop(EXCP_DEBUG
);
3785 /* If all cpus are halted then wait until the next IRQ */
3786 /* XXX: use timeout computed from timers */
3787 if (ret
== EXCP_HALTED
) {
3791 /* Advance virtual time to the next event. */
3792 if (use_icount
== 1) {
3793 /* When not using an adaptive execution frequency
3794 we tend to get badly out of sync with real time,
3795 so just delay for a reasonable amount of time. */
3798 delta
= cpu_get_icount() - cpu_get_clock();
3801 /* If virtual time is ahead of real time then just
3803 timeout
= (delta
/ 1000000) + 1;
3805 /* Wait for either IO to occur or the next
3807 add
= qemu_next_deadline();
3808 /* We advance the timer before checking for IO.
3809 Limit the amount we advance so that early IO
3810 activity won't get the guest too far ahead. */
3814 add
= (add
+ (1 << icount_time_shift
) - 1)
3815 >> icount_time_shift
;
3817 timeout
= delta
/ 1000000;
3828 if (shutdown_requested
) {
3829 ret
= EXCP_INTERRUPT
;
3834 #ifdef CONFIG_PROFILER
3835 ti
= profile_getclock();
3837 main_loop_wait(timeout
);
3838 #ifdef CONFIG_PROFILER
3839 dev_time
+= profile_getclock() - ti
;
3842 cpu_disable_ticks();
3846 static void help(int exitcode
)
3848 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3849 "usage: %s [options] [disk_image]\n"
3851 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3853 "Standard options:\n"
3854 "-M machine select emulated machine (-M ? for list)\n"
3855 "-cpu cpu select CPU (-cpu ? for list)\n"
3856 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3857 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3858 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3859 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3860 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3861 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3862 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3863 " use 'file' as a drive image\n"
3864 "-mtdblock file use 'file' as on-board Flash memory image\n"
3865 "-sd file use 'file' as SecureDigital card image\n"
3866 "-pflash file use 'file' as a parallel flash image\n"
3867 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3868 "-snapshot write to temporary files instead of disk image files\n"
3870 "-no-frame open SDL window without a frame and window decorations\n"
3871 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3872 "-no-quit disable SDL window close capability\n"
3876 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3878 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3879 "-smp n set the number of CPUs to 'n' [default=1]\n"
3880 "-nographic disable graphical output and redirect serial I/Os to console\n"
3881 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3883 "-k language use keyboard layout (for example \"fr\" for French)\n"
3886 "-audio-help print list of audio drivers and their options\n"
3887 "-soundhw c1,... enable audio support\n"
3888 " and only specified sound cards (comma separated list)\n"
3889 " use -soundhw ? to get the list of supported cards\n"
3890 " use -soundhw all to enable all of them\n"
3892 "-vga [std|cirrus|vmware|none]\n"
3893 " select video card type\n"
3894 "-localtime set the real time clock to local time [default=utc]\n"
3895 "-full-screen start in full screen\n"
3897 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3898 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
3900 "-usb enable the USB driver (will be the default soon)\n"
3901 "-usbdevice name add the host or guest USB device 'name'\n"
3902 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3903 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3905 "-name string set the name of the guest\n"
3906 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3908 "Network options:\n"
3909 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3910 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3912 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3913 " connect the user mode network stack to VLAN 'n' and send\n"
3914 " hostname 'host' to DHCP clients\n"
3917 "-net tap[,vlan=n][,name=str],ifname=name\n"
3918 " connect the host TAP network interface to VLAN 'n'\n"
3920 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3921 " connect the host TAP network interface to VLAN 'n' and use the\n"
3922 " network scripts 'file' (default=%s)\n"
3923 " and 'dfile' (default=%s);\n"
3924 " use '[down]script=no' to disable script execution;\n"
3925 " use 'fd=h' to connect to an already opened TAP interface\n"
3927 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3928 " connect the vlan 'n' to another VLAN using a socket connection\n"
3929 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3930 " connect the vlan 'n' to multicast maddr and port\n"
3932 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3933 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3934 " on host and listening for incoming connections on 'socketpath'.\n"
3935 " Use group 'groupname' and mode 'octalmode' to change default\n"
3936 " ownership and permissions for communication port.\n"
3938 "-net none use it alone to have zero network devices; if no -net option\n"
3939 " is provided, the default is '-net nic -net user'\n"
3941 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3942 "-bt hci,host[:id]\n"
3943 " Use host's HCI with the given name\n"
3944 "-bt hci[,vlan=n]\n"
3945 " Emulate a standard HCI in virtual scatternet 'n'\n"
3946 "-bt vhci[,vlan=n]\n"
3947 " Add host computer to virtual scatternet 'n' using VHCI\n"
3948 "-bt device:dev[,vlan=n]\n"
3949 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3952 "-tftp dir allow tftp access to files in dir [-net user]\n"
3953 "-bootp file advertise file in BOOTP replies\n"
3955 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3957 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3958 " redirect TCP or UDP connections from host to guest [-net user]\n"
3961 "Linux boot specific:\n"
3962 "-kernel bzImage use 'bzImage' as kernel image\n"
3963 "-append cmdline use 'cmdline' as kernel command line\n"
3964 "-initrd file use 'file' as initial ram disk\n"
3966 "Debug/Expert options:\n"
3967 "-monitor dev redirect the monitor to char device 'dev'\n"
3968 "-serial dev redirect the serial port to char device 'dev'\n"
3969 "-parallel dev redirect the parallel port to char device 'dev'\n"
3970 "-pidfile file Write PID to 'file'\n"
3971 "-S freeze CPU at startup (use 'c' to start execution)\n"
3972 "-s wait gdb connection to port\n"
3973 "-p port set gdb connection port [default=%s]\n"
3974 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3975 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3976 " translation (t=none or lba) (usually qemu can guess them)\n"
3977 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3979 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3980 "-no-kqemu disable KQEMU kernel module usage\n"
3983 "-enable-kvm enable KVM full virtualization support\n"
3986 "-no-acpi disable ACPI\n"
3987 "-no-hpet disable HPET\n"
3989 #ifdef CONFIG_CURSES
3990 "-curses use a curses/ncurses interface instead of SDL\n"
3992 "-no-reboot exit instead of rebooting\n"
3993 "-no-shutdown stop before shutdown\n"
3994 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3995 "-vnc display start a VNC server on display\n"
3997 "-daemonize daemonize QEMU after initializing\n"
3999 "-option-rom rom load a file, rom, into the option ROM space\n"
4001 "-prom-env variable=value set OpenBIOS nvram variables\n"
4003 "-clock force the use of the given methods for timer alarm.\n"
4004 " To see what timers are available use -clock ?\n"
4005 "-startdate select initial date of the clock\n"
4006 "-icount [N|auto]\n"
4007 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4009 "During emulation, the following keys are useful:\n"
4010 "ctrl-alt-f toggle full screen\n"
4011 "ctrl-alt-n switch to virtual console 'n'\n"
4012 "ctrl-alt toggle mouse and keyboard grab\n"
4014 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4019 DEFAULT_NETWORK_SCRIPT
,
4020 DEFAULT_NETWORK_DOWN_SCRIPT
,
4022 DEFAULT_GDBSTUB_PORT
,
4027 #define HAS_ARG 0x0001
4042 QEMU_OPTION_mtdblock
,
4046 QEMU_OPTION_snapshot
,
4048 QEMU_OPTION_no_fd_bootchk
,
4051 QEMU_OPTION_nographic
,
4052 QEMU_OPTION_portrait
,
4054 QEMU_OPTION_audio_help
,
4055 QEMU_OPTION_soundhw
,
4077 QEMU_OPTION_localtime
,
4081 QEMU_OPTION_monitor
,
4083 QEMU_OPTION_virtiocon
,
4084 QEMU_OPTION_parallel
,
4086 QEMU_OPTION_full_screen
,
4087 QEMU_OPTION_no_frame
,
4088 QEMU_OPTION_alt_grab
,
4089 QEMU_OPTION_no_quit
,
4091 QEMU_OPTION_pidfile
,
4092 QEMU_OPTION_no_kqemu
,
4093 QEMU_OPTION_kernel_kqemu
,
4094 QEMU_OPTION_enable_kvm
,
4095 QEMU_OPTION_win2k_hack
,
4096 QEMU_OPTION_rtc_td_hack
,
4098 QEMU_OPTION_usbdevice
,
4101 QEMU_OPTION_no_acpi
,
4102 QEMU_OPTION_no_hpet
,
4104 QEMU_OPTION_no_reboot
,
4105 QEMU_OPTION_no_shutdown
,
4106 QEMU_OPTION_show_cursor
,
4107 QEMU_OPTION_daemonize
,
4108 QEMU_OPTION_option_rom
,
4109 QEMU_OPTION_semihosting
,
4111 QEMU_OPTION_prom_env
,
4112 QEMU_OPTION_old_param
,
4114 QEMU_OPTION_startdate
,
4115 QEMU_OPTION_tb_size
,
4118 QEMU_OPTION_incoming
,
4121 typedef struct QEMUOption
{
4127 static const QEMUOption qemu_options
[] = {
4128 { "h", 0, QEMU_OPTION_h
},
4129 { "help", 0, QEMU_OPTION_h
},
4131 { "M", HAS_ARG
, QEMU_OPTION_M
},
4132 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4133 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4134 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4135 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4136 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4137 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4138 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4139 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4140 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4141 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4142 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4143 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4144 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4145 { "snapshot", 0, QEMU_OPTION_snapshot
},
4147 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4149 { "m", HAS_ARG
, QEMU_OPTION_m
},
4150 { "nographic", 0, QEMU_OPTION_nographic
},
4151 { "portrait", 0, QEMU_OPTION_portrait
},
4152 { "k", HAS_ARG
, QEMU_OPTION_k
},
4154 { "audio-help", 0, QEMU_OPTION_audio_help
},
4155 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4158 { "net", HAS_ARG
, QEMU_OPTION_net
},
4160 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4161 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4163 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4165 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4167 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4169 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4170 { "append", HAS_ARG
, QEMU_OPTION_append
},
4171 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4173 { "S", 0, QEMU_OPTION_S
},
4174 { "s", 0, QEMU_OPTION_s
},
4175 { "p", HAS_ARG
, QEMU_OPTION_p
},
4176 { "d", HAS_ARG
, QEMU_OPTION_d
},
4177 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4178 { "L", HAS_ARG
, QEMU_OPTION_L
},
4179 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4181 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4182 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4185 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4187 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4188 { "g", 1, QEMU_OPTION_g
},
4190 { "localtime", 0, QEMU_OPTION_localtime
},
4191 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4192 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4193 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4194 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4195 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4196 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4197 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4198 { "full-screen", 0, QEMU_OPTION_full_screen
},
4200 { "no-frame", 0, QEMU_OPTION_no_frame
},
4201 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4202 { "no-quit", 0, QEMU_OPTION_no_quit
},
4203 { "sdl", 0, QEMU_OPTION_sdl
},
4205 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4206 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4207 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4208 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4209 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4210 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4211 #ifdef CONFIG_CURSES
4212 { "curses", 0, QEMU_OPTION_curses
},
4214 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4216 /* temporary options */
4217 { "usb", 0, QEMU_OPTION_usb
},
4218 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4219 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4220 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4221 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4222 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4223 { "daemonize", 0, QEMU_OPTION_daemonize
},
4224 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4225 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4226 { "semihosting", 0, QEMU_OPTION_semihosting
},
4228 { "name", HAS_ARG
, QEMU_OPTION_name
},
4229 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4230 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4232 #if defined(TARGET_ARM)
4233 { "old-param", 0, QEMU_OPTION_old_param
},
4235 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4236 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4237 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4238 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4239 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4243 /* password input */
4245 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4250 if (!bdrv_is_encrypted(bs
))
4253 term_printf("%s is encrypted.\n", name
);
4254 for(i
= 0; i
< 3; i
++) {
4255 monitor_readline("Password: ", 1, password
, sizeof(password
));
4256 if (bdrv_set_key(bs
, password
) == 0)
4258 term_printf("invalid password\n");
4263 static BlockDriverState
*get_bdrv(int index
)
4265 if (index
> nb_drives
)
4267 return drives_table
[index
].bdrv
;
4270 static void read_passwords(void)
4272 BlockDriverState
*bs
;
4275 for(i
= 0; i
< 6; i
++) {
4278 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4283 struct soundhw soundhw
[] = {
4284 #ifdef HAS_AUDIO_CHOICE
4285 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4291 { .init_isa
= pcspk_audio_init
}
4298 "Creative Sound Blaster 16",
4301 { .init_isa
= SB16_init
}
4305 #ifdef CONFIG_CS4231A
4311 { .init_isa
= cs4231a_init
}
4319 "Yamaha YMF262 (OPL3)",
4321 "Yamaha YM3812 (OPL2)",
4325 { .init_isa
= Adlib_init
}
4332 "Gravis Ultrasound GF1",
4335 { .init_isa
= GUS_init
}
4342 "Intel 82801AA AC97 Audio",
4345 { .init_pci
= ac97_init
}
4349 #ifdef CONFIG_ES1370
4352 "ENSONIQ AudioPCI ES1370",
4355 { .init_pci
= es1370_init
}
4359 #endif /* HAS_AUDIO_CHOICE */
4361 { NULL
, NULL
, 0, 0, { NULL
} }
4364 static void select_soundhw (const char *optarg
)
4368 if (*optarg
== '?') {
4371 printf ("Valid sound card names (comma separated):\n");
4372 for (c
= soundhw
; c
->name
; ++c
) {
4373 printf ("%-11s %s\n", c
->name
, c
->descr
);
4375 printf ("\n-soundhw all will enable all of the above\n");
4376 exit (*optarg
!= '?');
4384 if (!strcmp (optarg
, "all")) {
4385 for (c
= soundhw
; c
->name
; ++c
) {
4393 e
= strchr (p
, ',');
4394 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4396 for (c
= soundhw
; c
->name
; ++c
) {
4397 if (!strncmp (c
->name
, p
, l
)) {
4406 "Unknown sound card name (too big to show)\n");
4409 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4414 p
+= l
+ (e
!= NULL
);
4418 goto show_valid_cards
;
4423 static void select_vgahw (const char *p
)
4427 if (strstart(p
, "std", &opts
)) {
4428 std_vga_enabled
= 1;
4429 cirrus_vga_enabled
= 0;
4431 } else if (strstart(p
, "cirrus", &opts
)) {
4432 cirrus_vga_enabled
= 1;
4433 std_vga_enabled
= 0;
4435 } else if (strstart(p
, "vmware", &opts
)) {
4436 cirrus_vga_enabled
= 0;
4437 std_vga_enabled
= 0;
4439 } else if (strstart(p
, "none", &opts
)) {
4440 cirrus_vga_enabled
= 0;
4441 std_vga_enabled
= 0;
4445 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4449 const char *nextopt
;
4451 if (strstart(opts
, ",retrace=", &nextopt
)) {
4453 if (strstart(opts
, "dumb", &nextopt
))
4454 vga_retrace_method
= VGA_RETRACE_DUMB
;
4455 else if (strstart(opts
, "precise", &nextopt
))
4456 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4457 else goto invalid_vga
;
4458 } else goto invalid_vga
;
4464 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4466 exit(STATUS_CONTROL_C_EXIT
);
4471 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4475 if(strlen(str
) != 36)
4478 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4479 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4480 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4488 #define MAX_NET_CLIENTS 32
4492 static void termsig_handler(int signal
)
4494 qemu_system_shutdown_request();
4497 static void termsig_setup(void)
4499 struct sigaction act
;
4501 memset(&act
, 0, sizeof(act
));
4502 act
.sa_handler
= termsig_handler
;
4503 sigaction(SIGINT
, &act
, NULL
);
4504 sigaction(SIGHUP
, &act
, NULL
);
4505 sigaction(SIGTERM
, &act
, NULL
);
4510 int main(int argc
, char **argv
, char **envp
)
4512 #ifdef CONFIG_GDBSTUB
4514 const char *gdbstub_port
;
4516 uint32_t boot_devices_bitmap
= 0;
4518 int snapshot
, linux_boot
, net_boot
;
4519 const char *initrd_filename
;
4520 const char *kernel_filename
, *kernel_cmdline
;
4521 const char *boot_devices
= "";
4523 DisplayChangeListener
*dcl
;
4524 int cyls
, heads
, secs
, translation
;
4525 const char *net_clients
[MAX_NET_CLIENTS
];
4527 const char *bt_opts
[MAX_BT_CMDLINE
];
4531 const char *r
, *optarg
;
4532 CharDriverState
*monitor_hd
;
4533 const char *monitor_device
;
4534 const char *serial_devices
[MAX_SERIAL_PORTS
];
4535 int serial_device_index
;
4536 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4537 int parallel_device_index
;
4538 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4539 int virtio_console_index
;
4540 const char *loadvm
= NULL
;
4541 QEMUMachine
*machine
;
4542 const char *cpu_model
;
4543 const char *usb_devices
[MAX_USB_CMDLINE
];
4544 int usb_devices_index
;
4547 const char *pid_file
= NULL
;
4549 const char *incoming
= NULL
;
4551 qemu_cache_utils_init(envp
);
4553 LIST_INIT (&vm_change_state_head
);
4556 struct sigaction act
;
4557 sigfillset(&act
.sa_mask
);
4559 act
.sa_handler
= SIG_IGN
;
4560 sigaction(SIGPIPE
, &act
, NULL
);
4563 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4564 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4565 QEMU to run on a single CPU */
4570 h
= GetCurrentProcess();
4571 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4572 for(i
= 0; i
< 32; i
++) {
4573 if (mask
& (1 << i
))
4578 SetProcessAffinityMask(h
, mask
);
4584 register_machines();
4585 machine
= first_machine
;
4587 initrd_filename
= NULL
;
4589 vga_ram_size
= VGA_RAM_SIZE
;
4590 #ifdef CONFIG_GDBSTUB
4592 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4597 kernel_filename
= NULL
;
4598 kernel_cmdline
= "";
4599 cyls
= heads
= secs
= 0;
4600 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4601 monitor_device
= "vc";
4603 serial_devices
[0] = "vc:80Cx24C";
4604 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4605 serial_devices
[i
] = NULL
;
4606 serial_device_index
= 0;
4608 parallel_devices
[0] = "vc:640x480";
4609 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4610 parallel_devices
[i
] = NULL
;
4611 parallel_device_index
= 0;
4613 virtio_consoles
[0] = "vc:80Cx24C";
4614 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4615 virtio_consoles
[i
] = NULL
;
4616 virtio_console_index
= 0;
4618 usb_devices_index
= 0;
4637 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4639 const QEMUOption
*popt
;
4642 /* Treat --foo the same as -foo. */
4645 popt
= qemu_options
;
4648 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4652 if (!strcmp(popt
->name
, r
+ 1))
4656 if (popt
->flags
& HAS_ARG
) {
4657 if (optind
>= argc
) {
4658 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4662 optarg
= argv
[optind
++];
4667 switch(popt
->index
) {
4669 machine
= find_machine(optarg
);
4672 printf("Supported machines are:\n");
4673 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4674 printf("%-10s %s%s\n",
4676 m
== first_machine
? " (default)" : "");
4678 exit(*optarg
!= '?');
4681 case QEMU_OPTION_cpu
:
4682 /* hw initialization will check this */
4683 if (*optarg
== '?') {
4684 /* XXX: implement xxx_cpu_list for targets that still miss it */
4685 #if defined(cpu_list)
4686 cpu_list(stdout
, &fprintf
);
4693 case QEMU_OPTION_initrd
:
4694 initrd_filename
= optarg
;
4696 case QEMU_OPTION_hda
:
4698 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4700 hda_index
= drive_add(optarg
, HD_ALIAS
4701 ",cyls=%d,heads=%d,secs=%d%s",
4702 0, cyls
, heads
, secs
,
4703 translation
== BIOS_ATA_TRANSLATION_LBA
?
4705 translation
== BIOS_ATA_TRANSLATION_NONE
?
4706 ",trans=none" : "");
4708 case QEMU_OPTION_hdb
:
4709 case QEMU_OPTION_hdc
:
4710 case QEMU_OPTION_hdd
:
4711 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4713 case QEMU_OPTION_drive
:
4714 drive_add(NULL
, "%s", optarg
);
4716 case QEMU_OPTION_mtdblock
:
4717 drive_add(optarg
, MTD_ALIAS
);
4719 case QEMU_OPTION_sd
:
4720 drive_add(optarg
, SD_ALIAS
);
4722 case QEMU_OPTION_pflash
:
4723 drive_add(optarg
, PFLASH_ALIAS
);
4725 case QEMU_OPTION_snapshot
:
4728 case QEMU_OPTION_hdachs
:
4732 cyls
= strtol(p
, (char **)&p
, 0);
4733 if (cyls
< 1 || cyls
> 16383)
4738 heads
= strtol(p
, (char **)&p
, 0);
4739 if (heads
< 1 || heads
> 16)
4744 secs
= strtol(p
, (char **)&p
, 0);
4745 if (secs
< 1 || secs
> 63)
4749 if (!strcmp(p
, "none"))
4750 translation
= BIOS_ATA_TRANSLATION_NONE
;
4751 else if (!strcmp(p
, "lba"))
4752 translation
= BIOS_ATA_TRANSLATION_LBA
;
4753 else if (!strcmp(p
, "auto"))
4754 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4757 } else if (*p
!= '\0') {
4759 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4762 if (hda_index
!= -1)
4763 snprintf(drives_opt
[hda_index
].opt
,
4764 sizeof(drives_opt
[hda_index
].opt
),
4765 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4766 0, cyls
, heads
, secs
,
4767 translation
== BIOS_ATA_TRANSLATION_LBA
?
4769 translation
== BIOS_ATA_TRANSLATION_NONE
?
4770 ",trans=none" : "");
4773 case QEMU_OPTION_nographic
:
4776 #ifdef CONFIG_CURSES
4777 case QEMU_OPTION_curses
:
4781 case QEMU_OPTION_portrait
:
4784 case QEMU_OPTION_kernel
:
4785 kernel_filename
= optarg
;
4787 case QEMU_OPTION_append
:
4788 kernel_cmdline
= optarg
;
4790 case QEMU_OPTION_cdrom
:
4791 drive_add(optarg
, CDROM_ALIAS
);
4793 case QEMU_OPTION_boot
:
4794 boot_devices
= optarg
;
4795 /* We just do some generic consistency checks */
4797 /* Could easily be extended to 64 devices if needed */
4800 boot_devices_bitmap
= 0;
4801 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4802 /* Allowed boot devices are:
4803 * a b : floppy disk drives
4804 * c ... f : IDE disk drives
4805 * g ... m : machine implementation dependant drives
4806 * n ... p : network devices
4807 * It's up to each machine implementation to check
4808 * if the given boot devices match the actual hardware
4809 * implementation and firmware features.
4811 if (*p
< 'a' || *p
> 'q') {
4812 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4815 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4817 "Boot device '%c' was given twice\n",*p
);
4820 boot_devices_bitmap
|= 1 << (*p
- 'a');
4824 case QEMU_OPTION_fda
:
4825 case QEMU_OPTION_fdb
:
4826 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4829 case QEMU_OPTION_no_fd_bootchk
:
4833 case QEMU_OPTION_net
:
4834 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4835 fprintf(stderr
, "qemu: too many network clients\n");
4838 net_clients
[nb_net_clients
] = optarg
;
4842 case QEMU_OPTION_tftp
:
4843 tftp_prefix
= optarg
;
4845 case QEMU_OPTION_bootp
:
4846 bootp_filename
= optarg
;
4849 case QEMU_OPTION_smb
:
4850 net_slirp_smb(optarg
);
4853 case QEMU_OPTION_redir
:
4854 net_slirp_redir(optarg
);
4857 case QEMU_OPTION_bt
:
4858 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4859 fprintf(stderr
, "qemu: too many bluetooth options\n");
4862 bt_opts
[nb_bt_opts
++] = optarg
;
4865 case QEMU_OPTION_audio_help
:
4869 case QEMU_OPTION_soundhw
:
4870 select_soundhw (optarg
);
4876 case QEMU_OPTION_m
: {
4880 value
= strtoul(optarg
, &ptr
, 10);
4882 case 0: case 'M': case 'm':
4889 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4893 /* On 32-bit hosts, QEMU is limited by virtual address space */
4894 if (value
> (2047 << 20)
4896 && HOST_LONG_BITS
== 32
4899 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4902 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4903 fprintf(stderr
, "qemu: ram size too large\n");
4912 const CPULogItem
*item
;
4914 mask
= cpu_str_to_log_mask(optarg
);
4916 printf("Log items (comma separated):\n");
4917 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4918 printf("%-10s %s\n", item
->name
, item
->help
);
4925 #ifdef CONFIG_GDBSTUB
4930 gdbstub_port
= optarg
;
4936 case QEMU_OPTION_bios
:
4943 keyboard_layout
= optarg
;
4945 case QEMU_OPTION_localtime
:
4948 case QEMU_OPTION_vga
:
4949 select_vgahw (optarg
);
4956 w
= strtol(p
, (char **)&p
, 10);
4959 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4965 h
= strtol(p
, (char **)&p
, 10);
4970 depth
= strtol(p
, (char **)&p
, 10);
4971 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4972 depth
!= 24 && depth
!= 32)
4974 } else if (*p
== '\0') {
4975 depth
= graphic_depth
;
4982 graphic_depth
= depth
;
4985 case QEMU_OPTION_echr
:
4988 term_escape_char
= strtol(optarg
, &r
, 0);
4990 printf("Bad argument to echr\n");
4993 case QEMU_OPTION_monitor
:
4994 monitor_device
= optarg
;
4996 case QEMU_OPTION_serial
:
4997 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4998 fprintf(stderr
, "qemu: too many serial ports\n");
5001 serial_devices
[serial_device_index
] = optarg
;
5002 serial_device_index
++;
5004 case QEMU_OPTION_virtiocon
:
5005 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5006 fprintf(stderr
, "qemu: too many virtio consoles\n");
5009 virtio_consoles
[virtio_console_index
] = optarg
;
5010 virtio_console_index
++;
5012 case QEMU_OPTION_parallel
:
5013 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5014 fprintf(stderr
, "qemu: too many parallel ports\n");
5017 parallel_devices
[parallel_device_index
] = optarg
;
5018 parallel_device_index
++;
5020 case QEMU_OPTION_loadvm
:
5023 case QEMU_OPTION_full_screen
:
5027 case QEMU_OPTION_no_frame
:
5030 case QEMU_OPTION_alt_grab
:
5033 case QEMU_OPTION_no_quit
:
5036 case QEMU_OPTION_sdl
:
5040 case QEMU_OPTION_pidfile
:
5044 case QEMU_OPTION_win2k_hack
:
5045 win2k_install_hack
= 1;
5047 case QEMU_OPTION_rtc_td_hack
:
5052 case QEMU_OPTION_no_kqemu
:
5055 case QEMU_OPTION_kernel_kqemu
:
5060 case QEMU_OPTION_enable_kvm
:
5067 case QEMU_OPTION_usb
:
5070 case QEMU_OPTION_usbdevice
:
5072 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5073 fprintf(stderr
, "Too many USB devices\n");
5076 usb_devices
[usb_devices_index
] = optarg
;
5077 usb_devices_index
++;
5079 case QEMU_OPTION_smp
:
5080 smp_cpus
= atoi(optarg
);
5082 fprintf(stderr
, "Invalid number of CPUs\n");
5086 case QEMU_OPTION_vnc
:
5087 vnc_display
= optarg
;
5089 case QEMU_OPTION_no_acpi
:
5092 case QEMU_OPTION_no_hpet
:
5095 case QEMU_OPTION_no_reboot
:
5098 case QEMU_OPTION_no_shutdown
:
5101 case QEMU_OPTION_show_cursor
:
5104 case QEMU_OPTION_uuid
:
5105 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5106 fprintf(stderr
, "Fail to parse UUID string."
5107 " Wrong format.\n");
5111 case QEMU_OPTION_daemonize
:
5114 case QEMU_OPTION_option_rom
:
5115 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5116 fprintf(stderr
, "Too many option ROMs\n");
5119 option_rom
[nb_option_roms
] = optarg
;
5122 case QEMU_OPTION_semihosting
:
5123 semihosting_enabled
= 1;
5125 case QEMU_OPTION_name
:
5128 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5129 case QEMU_OPTION_prom_env
:
5130 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5131 fprintf(stderr
, "Too many prom variables\n");
5134 prom_envs
[nb_prom_envs
] = optarg
;
5139 case QEMU_OPTION_old_param
:
5143 case QEMU_OPTION_clock
:
5144 configure_alarms(optarg
);
5146 case QEMU_OPTION_startdate
:
5149 time_t rtc_start_date
;
5150 if (!strcmp(optarg
, "now")) {
5151 rtc_date_offset
= -1;
5153 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5161 } else if (sscanf(optarg
, "%d-%d-%d",
5164 &tm
.tm_mday
) == 3) {
5173 rtc_start_date
= mktimegm(&tm
);
5174 if (rtc_start_date
== -1) {
5176 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5177 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5180 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5184 case QEMU_OPTION_tb_size
:
5185 tb_size
= strtol(optarg
, NULL
, 0);
5189 case QEMU_OPTION_icount
:
5191 if (strcmp(optarg
, "auto") == 0) {
5192 icount_time_shift
= -1;
5194 icount_time_shift
= strtol(optarg
, NULL
, 0);
5197 case QEMU_OPTION_incoming
:
5204 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5205 if (kvm_allowed
&& kqemu_allowed
) {
5207 "You can not enable both KVM and kqemu at the same time\n");
5212 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5213 if (smp_cpus
> machine
->max_cpus
) {
5214 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5215 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5221 if (serial_device_index
== 0)
5222 serial_devices
[0] = "stdio";
5223 if (parallel_device_index
== 0)
5224 parallel_devices
[0] = "null";
5225 if (strncmp(monitor_device
, "vc", 2) == 0)
5226 monitor_device
= "stdio";
5227 if (virtio_console_index
== 0)
5228 virtio_consoles
[0] = "null";
5235 if (pipe(fds
) == -1)
5246 len
= read(fds
[0], &status
, 1);
5247 if (len
== -1 && (errno
== EINTR
))
5252 else if (status
== 1) {
5253 fprintf(stderr
, "Could not acquire pidfile\n");
5270 signal(SIGTSTP
, SIG_IGN
);
5271 signal(SIGTTOU
, SIG_IGN
);
5272 signal(SIGTTIN
, SIG_IGN
);
5276 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5279 write(fds
[1], &status
, 1);
5281 fprintf(stderr
, "Could not acquire pid file\n");
5289 linux_boot
= (kernel_filename
!= NULL
);
5290 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5292 if (!linux_boot
&& net_boot
== 0 &&
5293 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5296 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5297 fprintf(stderr
, "-append only allowed with -kernel option\n");
5301 if (!linux_boot
&& initrd_filename
!= NULL
) {
5302 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5306 /* boot to floppy or the default cd if no hard disk defined yet */
5307 if (!boot_devices
[0]) {
5308 boot_devices
= "cad";
5310 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5313 if (init_timer_alarm() < 0) {
5314 fprintf(stderr
, "could not initialize alarm timer\n");
5317 if (use_icount
&& icount_time_shift
< 0) {
5319 /* 125MIPS seems a reasonable initial guess at the guest speed.
5320 It will be corrected fairly quickly anyway. */
5321 icount_time_shift
= 3;
5322 init_icount_adjust();
5329 /* init network clients */
5330 if (nb_net_clients
== 0) {
5331 /* if no clients, we use a default config */
5332 net_clients
[nb_net_clients
++] = "nic";
5334 net_clients
[nb_net_clients
++] = "user";
5338 for(i
= 0;i
< nb_net_clients
; i
++) {
5339 if (net_client_parse(net_clients
[i
]) < 0)
5345 /* XXX: this should be moved in the PC machine instantiation code */
5346 if (net_boot
!= 0) {
5348 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5349 const char *model
= nd_table
[i
].model
;
5351 if (net_boot
& (1 << i
)) {
5354 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5355 if (get_image_size(buf
) > 0) {
5356 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5357 fprintf(stderr
, "Too many option ROMs\n");
5360 option_rom
[nb_option_roms
] = strdup(buf
);
5367 fprintf(stderr
, "No valid PXE rom found for network device\n");
5373 /* init the bluetooth world */
5374 for (i
= 0; i
< nb_bt_opts
; i
++)
5375 if (bt_parse(bt_opts
[i
]))
5378 /* init the memory */
5379 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5381 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5383 if (ram_size
< phys_ram_size
) {
5384 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5385 machine
->name
, (unsigned long long) phys_ram_size
);
5389 phys_ram_size
= ram_size
;
5391 ram_size
= phys_ram_size
;
5394 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5396 phys_ram_size
+= ram_size
;
5399 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5400 if (!phys_ram_base
) {
5401 fprintf(stderr
, "Could not allocate physical memory\n");
5405 /* init the dynamic translator */
5406 cpu_exec_init_all(tb_size
* 1024 * 1024);
5410 /* we always create the cdrom drive, even if no disk is there */
5412 if (nb_drives_opt
< MAX_DRIVES
)
5413 drive_add(NULL
, CDROM_ALIAS
);
5415 /* we always create at least one floppy */
5417 if (nb_drives_opt
< MAX_DRIVES
)
5418 drive_add(NULL
, FD_ALIAS
, 0);
5420 /* we always create one sd slot, even if no card is in it */
5422 if (nb_drives_opt
< MAX_DRIVES
)
5423 drive_add(NULL
, SD_ALIAS
);
5425 /* open the virtual block devices */
5427 for(i
= 0; i
< nb_drives_opt
; i
++)
5428 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5431 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5432 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5435 /* must be after terminal init, SDL library changes signal handlers */
5439 /* Maintain compatibility with multiple stdio monitors */
5440 if (!strcmp(monitor_device
,"stdio")) {
5441 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5442 const char *devname
= serial_devices
[i
];
5443 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5444 monitor_device
= NULL
;
5446 } else if (devname
&& !strcmp(devname
,"stdio")) {
5447 monitor_device
= NULL
;
5448 serial_devices
[i
] = "mon:stdio";
5454 if (kvm_enabled()) {
5457 ret
= kvm_init(smp_cpus
);
5459 fprintf(stderr
, "failed to initialize KVM\n");
5464 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5465 const char *devname
= serial_devices
[i
];
5466 if (devname
&& strcmp(devname
, "none")) {
5468 snprintf(label
, sizeof(label
), "serial%d", i
);
5469 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5470 if (!serial_hds
[i
]) {
5471 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5478 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5479 const char *devname
= parallel_devices
[i
];
5480 if (devname
&& strcmp(devname
, "none")) {
5482 snprintf(label
, sizeof(label
), "parallel%d", i
);
5483 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5484 if (!parallel_hds
[i
]) {
5485 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5492 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5493 const char *devname
= virtio_consoles
[i
];
5494 if (devname
&& strcmp(devname
, "none")) {
5496 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5497 virtcon_hds
[i
] = qemu_chr_open(label
, devname
);
5498 if (!virtcon_hds
[i
]) {
5499 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5506 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5507 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5509 /* Set KVM's vcpu state to qemu's initial CPUState. */
5510 if (kvm_enabled()) {
5513 ret
= kvm_sync_vcpus();
5515 fprintf(stderr
, "failed to initialize vcpus\n");
5520 /* init USB devices */
5522 for(i
= 0; i
< usb_devices_index
; i
++) {
5523 if (usb_device_add(usb_devices
[i
]) < 0) {
5524 fprintf(stderr
, "Warning: could not add USB device %s\n",
5530 /* just use the first displaystate for the moment */
5535 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5538 /* nearly nothing to do */
5539 dumb_display_init(ds
);
5541 #if defined(CONFIG_CURSES)
5543 /* At the moment curses cannot be used with other displays */
5544 curses_display_init(ds
, full_screen
);
5548 if (vnc_display
!= NULL
) {
5549 vnc_display_init(ds
);
5550 if (vnc_display_open(ds
, vnc_display
) < 0)
5553 if (sdl
|| !vnc_display
)
5554 #if defined(CONFIG_SDL)
5555 sdl_display_init(ds
, full_screen
, no_frame
);
5556 #elif defined(CONFIG_COCOA)
5557 cocoa_display_init(ds
, full_screen
);
5559 dumb_display_init(ds
);
5565 dcl
= ds
->listeners
;
5566 while (dcl
!= NULL
) {
5567 if (dcl
->dpy_refresh
!= NULL
) {
5568 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5569 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5574 text_consoles_set_display(display_state
);
5576 if (monitor_device
) {
5577 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5579 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5582 monitor_init(monitor_hd
, !nographic
);
5585 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5586 const char *devname
= serial_devices
[i
];
5587 if (devname
&& strcmp(devname
, "none")) {
5589 snprintf(label
, sizeof(label
), "serial%d", i
);
5590 if (strstart(devname
, "vc", 0))
5591 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5595 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5596 const char *devname
= parallel_devices
[i
];
5597 if (devname
&& strcmp(devname
, "none")) {
5599 snprintf(label
, sizeof(label
), "parallel%d", i
);
5600 if (strstart(devname
, "vc", 0))
5601 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5605 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5606 const char *devname
= virtio_consoles
[i
];
5607 if (virtcon_hds
[i
] && devname
) {
5609 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5610 if (strstart(devname
, "vc", 0))
5611 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5615 #ifdef CONFIG_GDBSTUB
5617 /* XXX: use standard host:port notation and modify options
5619 if (gdbserver_start(gdbstub_port
) < 0) {
5620 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5631 autostart
= 0; /* fixme how to deal with -daemonize */
5632 qemu_start_incoming_migration(incoming
);
5636 /* XXX: simplify init */
5649 len
= write(fds
[1], &status
, 1);
5650 if (len
== -1 && (errno
== EINTR
))
5657 TFR(fd
= open("/dev/null", O_RDWR
));