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
158 #define DEFAULT_RAM_SIZE 144
160 #define DEFAULT_RAM_SIZE 128
163 /* Max number of USB devices that can be specified on the commandline. */
164 #define MAX_USB_CMDLINE 8
166 /* Max number of bluetooth switches on the commandline. */
167 #define MAX_BT_CMDLINE 10
169 /* XXX: use a two level table to limit memory usage */
170 #define MAX_IOPORTS 65536
172 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
173 const char *bios_name
= NULL
;
174 static void *ioport_opaque
[MAX_IOPORTS
];
175 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
176 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
177 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
178 to store the VM snapshots */
179 DriveInfo drives_table
[MAX_DRIVES
+1];
181 static int vga_ram_size
;
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 DisplayState display_state
;
186 const char* keyboard_layout
= NULL
;
187 int64_t ticks_per_sec
;
190 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 int cirrus_vga_enabled
= 1;
195 int vmsvga_enabled
= 0;
197 int graphic_width
= 1024;
198 int graphic_height
= 768;
199 int graphic_depth
= 8;
201 int graphic_width
= 800;
202 int graphic_height
= 600;
203 int graphic_depth
= 15;
205 static int full_screen
= 0;
207 static int no_frame
= 0;
210 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
211 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
212 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
214 int win2k_install_hack
= 0;
219 const char *vnc_display
;
220 int acpi_enabled
= 1;
226 int graphic_rotate
= 0;
228 const char *option_rom
[MAX_OPTION_ROMS
];
230 int semihosting_enabled
= 0;
234 const char *qemu_name
;
236 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
237 unsigned int nb_prom_envs
= 0;
238 const char *prom_envs
[MAX_PROM_ENVS
];
240 static int nb_drives_opt
;
241 static struct drive_opt
{
244 } drives_opt
[MAX_DRIVES
];
246 static CPUState
*cur_cpu
;
247 static CPUState
*next_cpu
;
248 static int event_pending
= 1;
249 /* Conversion factor from emulated instructions to virtual clock ticks. */
250 static int icount_time_shift
;
251 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
252 #define MAX_ICOUNT_SHIFT 10
253 /* Compensate for varying guest execution speed. */
254 static int64_t qemu_icount_bias
;
255 static QEMUTimer
*icount_rt_timer
;
256 static QEMUTimer
*icount_vm_timer
;
258 uint8_t qemu_uuid
[16];
260 /***********************************************************/
261 /* x86 ISA bus support */
263 target_phys_addr_t isa_mem_base
= 0;
266 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
267 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
269 static uint32_t ioport_read(int index
, uint32_t address
)
271 static IOPortReadFunc
*default_func
[3] = {
272 default_ioport_readb
,
273 default_ioport_readw
,
276 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
278 func
= default_func
[index
];
279 return func(ioport_opaque
[address
], address
);
282 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
284 static IOPortWriteFunc
*default_func
[3] = {
285 default_ioport_writeb
,
286 default_ioport_writew
,
287 default_ioport_writel
289 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
291 func
= default_func
[index
];
292 func(ioport_opaque
[address
], address
, data
);
295 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
297 #ifdef DEBUG_UNUSED_IOPORT
298 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
303 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
305 #ifdef DEBUG_UNUSED_IOPORT
306 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
310 /* default is to make two byte accesses */
311 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
314 data
= ioport_read(0, address
);
315 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
316 data
|= ioport_read(0, address
) << 8;
320 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
322 ioport_write(0, address
, data
& 0xff);
323 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
324 ioport_write(0, address
, (data
>> 8) & 0xff);
327 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
329 #ifdef DEBUG_UNUSED_IOPORT
330 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
335 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
337 #ifdef DEBUG_UNUSED_IOPORT
338 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
342 /* size is the word size in byte */
343 int register_ioport_read(int start
, int length
, int size
,
344 IOPortReadFunc
*func
, void *opaque
)
350 } else if (size
== 2) {
352 } else if (size
== 4) {
355 hw_error("register_ioport_read: invalid size");
358 for(i
= start
; i
< start
+ length
; i
+= size
) {
359 ioport_read_table
[bsize
][i
] = func
;
360 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
361 hw_error("register_ioport_read: invalid opaque");
362 ioport_opaque
[i
] = opaque
;
367 /* size is the word size in byte */
368 int register_ioport_write(int start
, int length
, int size
,
369 IOPortWriteFunc
*func
, void *opaque
)
375 } else if (size
== 2) {
377 } else if (size
== 4) {
380 hw_error("register_ioport_write: invalid size");
383 for(i
= start
; i
< start
+ length
; i
+= size
) {
384 ioport_write_table
[bsize
][i
] = func
;
385 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
386 hw_error("register_ioport_write: invalid opaque");
387 ioport_opaque
[i
] = opaque
;
392 void isa_unassign_ioport(int start
, int length
)
396 for(i
= start
; i
< start
+ length
; i
++) {
397 ioport_read_table
[0][i
] = default_ioport_readb
;
398 ioport_read_table
[1][i
] = default_ioport_readw
;
399 ioport_read_table
[2][i
] = default_ioport_readl
;
401 ioport_write_table
[0][i
] = default_ioport_writeb
;
402 ioport_write_table
[1][i
] = default_ioport_writew
;
403 ioport_write_table
[2][i
] = default_ioport_writel
;
407 /***********************************************************/
409 void cpu_outb(CPUState
*env
, int addr
, int val
)
412 if (loglevel
& CPU_LOG_IOPORT
)
413 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
415 ioport_write(0, addr
, val
);
418 env
->last_io_time
= cpu_get_time_fast();
422 void cpu_outw(CPUState
*env
, int addr
, int val
)
425 if (loglevel
& CPU_LOG_IOPORT
)
426 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
428 ioport_write(1, addr
, val
);
431 env
->last_io_time
= cpu_get_time_fast();
435 void cpu_outl(CPUState
*env
, int addr
, int val
)
438 if (loglevel
& CPU_LOG_IOPORT
)
439 fprintf(logfile
, "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
);
453 if (loglevel
& CPU_LOG_IOPORT
)
454 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
458 env
->last_io_time
= cpu_get_time_fast();
463 int cpu_inw(CPUState
*env
, int addr
)
466 val
= ioport_read(1, addr
);
468 if (loglevel
& CPU_LOG_IOPORT
)
469 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
473 env
->last_io_time
= cpu_get_time_fast();
478 int cpu_inl(CPUState
*env
, int addr
)
481 val
= ioport_read(2, addr
);
483 if (loglevel
& CPU_LOG_IOPORT
)
484 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
488 env
->last_io_time
= cpu_get_time_fast();
493 /***********************************************************/
494 void hw_error(const char *fmt
, ...)
500 fprintf(stderr
, "qemu: hardware error: ");
501 vfprintf(stderr
, fmt
, ap
);
502 fprintf(stderr
, "\n");
503 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
504 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
506 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
508 cpu_dump_state(env
, stderr
, fprintf
, 0);
518 static QEMUBalloonEvent
*qemu_balloon_event
;
519 void *qemu_balloon_event_opaque
;
521 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
523 qemu_balloon_event
= func
;
524 qemu_balloon_event_opaque
= opaque
;
527 void qemu_balloon(ram_addr_t target
)
529 if (qemu_balloon_event
)
530 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
533 ram_addr_t
qemu_balloon_status(void)
535 if (qemu_balloon_event
)
536 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
540 /***********************************************************/
543 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
544 static void *qemu_put_kbd_event_opaque
;
545 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
546 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
548 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
550 qemu_put_kbd_event_opaque
= opaque
;
551 qemu_put_kbd_event
= func
;
554 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
555 void *opaque
, int absolute
,
558 QEMUPutMouseEntry
*s
, *cursor
;
560 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
564 s
->qemu_put_mouse_event
= func
;
565 s
->qemu_put_mouse_event_opaque
= opaque
;
566 s
->qemu_put_mouse_event_absolute
= absolute
;
567 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
570 if (!qemu_put_mouse_event_head
) {
571 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
575 cursor
= qemu_put_mouse_event_head
;
576 while (cursor
->next
!= NULL
)
577 cursor
= cursor
->next
;
580 qemu_put_mouse_event_current
= s
;
585 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
587 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
589 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
592 cursor
= qemu_put_mouse_event_head
;
593 while (cursor
!= NULL
&& cursor
!= entry
) {
595 cursor
= cursor
->next
;
598 if (cursor
== NULL
) // does not exist or list empty
600 else if (prev
== NULL
) { // entry is head
601 qemu_put_mouse_event_head
= cursor
->next
;
602 if (qemu_put_mouse_event_current
== entry
)
603 qemu_put_mouse_event_current
= cursor
->next
;
604 qemu_free(entry
->qemu_put_mouse_event_name
);
609 prev
->next
= entry
->next
;
611 if (qemu_put_mouse_event_current
== entry
)
612 qemu_put_mouse_event_current
= prev
;
614 qemu_free(entry
->qemu_put_mouse_event_name
);
618 void kbd_put_keycode(int keycode
)
620 if (qemu_put_kbd_event
) {
621 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
625 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
627 QEMUPutMouseEvent
*mouse_event
;
628 void *mouse_event_opaque
;
631 if (!qemu_put_mouse_event_current
) {
636 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
638 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
641 if (graphic_rotate
) {
642 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
645 width
= graphic_width
- 1;
646 mouse_event(mouse_event_opaque
,
647 width
- dy
, dx
, dz
, buttons_state
);
649 mouse_event(mouse_event_opaque
,
650 dx
, dy
, dz
, buttons_state
);
654 int kbd_mouse_is_absolute(void)
656 if (!qemu_put_mouse_event_current
)
659 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
662 void do_info_mice(void)
664 QEMUPutMouseEntry
*cursor
;
667 if (!qemu_put_mouse_event_head
) {
668 term_printf("No mouse devices connected\n");
672 term_printf("Mouse devices available:\n");
673 cursor
= qemu_put_mouse_event_head
;
674 while (cursor
!= NULL
) {
675 term_printf("%c Mouse #%d: %s\n",
676 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
677 index
, cursor
->qemu_put_mouse_event_name
);
679 cursor
= cursor
->next
;
683 void do_mouse_set(int index
)
685 QEMUPutMouseEntry
*cursor
;
688 if (!qemu_put_mouse_event_head
) {
689 term_printf("No mouse devices connected\n");
693 cursor
= qemu_put_mouse_event_head
;
694 while (cursor
!= NULL
&& index
!= i
) {
696 cursor
= cursor
->next
;
700 qemu_put_mouse_event_current
= cursor
;
702 term_printf("Mouse at given index not found\n");
705 /* compute with 96 bit intermediate result: (a*b)/c */
706 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
711 #ifdef WORDS_BIGENDIAN
721 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
722 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
725 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
729 /***********************************************************/
730 /* real time host monotonic timer */
732 #define QEMU_TIMER_BASE 1000000000LL
736 static int64_t clock_freq
;
738 static void init_get_clock(void)
742 ret
= QueryPerformanceFrequency(&freq
);
744 fprintf(stderr
, "Could not calibrate ticks\n");
747 clock_freq
= freq
.QuadPart
;
750 static int64_t get_clock(void)
753 QueryPerformanceCounter(&ti
);
754 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
759 static int use_rt_clock
;
761 static void init_get_clock(void)
764 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
767 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
774 static int64_t get_clock(void)
776 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
779 clock_gettime(CLOCK_MONOTONIC
, &ts
);
780 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
784 /* XXX: using gettimeofday leads to problems if the date
785 changes, so it should be avoided. */
787 gettimeofday(&tv
, NULL
);
788 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
793 /* Return the virtual CPU time, based on the instruction counter. */
794 static int64_t cpu_get_icount(void)
797 CPUState
*env
= cpu_single_env
;;
798 icount
= qemu_icount
;
801 fprintf(stderr
, "Bad clock read\n");
802 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
804 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
807 /***********************************************************/
808 /* guest cycle counter */
810 static int64_t cpu_ticks_prev
;
811 static int64_t cpu_ticks_offset
;
812 static int64_t cpu_clock_offset
;
813 static int cpu_ticks_enabled
;
815 /* return the host CPU cycle counter and handle stop/restart */
816 int64_t cpu_get_ticks(void)
819 return cpu_get_icount();
821 if (!cpu_ticks_enabled
) {
822 return cpu_ticks_offset
;
825 ticks
= cpu_get_real_ticks();
826 if (cpu_ticks_prev
> ticks
) {
827 /* Note: non increasing ticks may happen if the host uses
829 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
831 cpu_ticks_prev
= ticks
;
832 return ticks
+ cpu_ticks_offset
;
836 /* return the host CPU monotonic timer and handle stop/restart */
837 static int64_t cpu_get_clock(void)
840 if (!cpu_ticks_enabled
) {
841 return cpu_clock_offset
;
844 return ti
+ cpu_clock_offset
;
848 /* enable cpu_get_ticks() */
849 void cpu_enable_ticks(void)
851 if (!cpu_ticks_enabled
) {
852 cpu_ticks_offset
-= cpu_get_real_ticks();
853 cpu_clock_offset
-= get_clock();
854 cpu_ticks_enabled
= 1;
858 /* disable cpu_get_ticks() : the clock is stopped. You must not call
859 cpu_get_ticks() after that. */
860 void cpu_disable_ticks(void)
862 if (cpu_ticks_enabled
) {
863 cpu_ticks_offset
= cpu_get_ticks();
864 cpu_clock_offset
= cpu_get_clock();
865 cpu_ticks_enabled
= 0;
869 /***********************************************************/
872 #define QEMU_TIMER_REALTIME 0
873 #define QEMU_TIMER_VIRTUAL 1
877 /* XXX: add frequency */
885 struct QEMUTimer
*next
;
888 struct qemu_alarm_timer
{
892 int (*start
)(struct qemu_alarm_timer
*t
);
893 void (*stop
)(struct qemu_alarm_timer
*t
);
894 void (*rearm
)(struct qemu_alarm_timer
*t
);
898 #define ALARM_FLAG_DYNTICKS 0x1
899 #define ALARM_FLAG_EXPIRED 0x2
901 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
903 return t
->flags
& ALARM_FLAG_DYNTICKS
;
906 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
908 if (!alarm_has_dynticks(t
))
914 /* TODO: MIN_TIMER_REARM_US should be optimized */
915 #define MIN_TIMER_REARM_US 250
917 static struct qemu_alarm_timer
*alarm_timer
;
919 static int alarm_timer_rfd
, alarm_timer_wfd
;
924 struct qemu_alarm_win32
{
928 } alarm_win32_data
= {0, NULL
, -1};
930 static int win32_start_timer(struct qemu_alarm_timer
*t
);
931 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
932 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
936 static int unix_start_timer(struct qemu_alarm_timer
*t
);
937 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
941 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
942 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
943 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
945 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
946 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
948 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
949 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
951 #endif /* __linux__ */
955 /* Correlation between real and virtual time is always going to be
956 fairly approximate, so ignore small variation.
957 When the guest is idle real and virtual time will be aligned in
959 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
961 static void icount_adjust(void)
966 static int64_t last_delta
;
967 /* If the VM is not running, then do nothing. */
971 cur_time
= cpu_get_clock();
972 cur_icount
= qemu_get_clock(vm_clock
);
973 delta
= cur_icount
- cur_time
;
974 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
976 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
977 && icount_time_shift
> 0) {
978 /* The guest is getting too far ahead. Slow time down. */
982 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
983 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
984 /* The guest is getting too far behind. Speed time up. */
988 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
991 static void icount_adjust_rt(void * opaque
)
993 qemu_mod_timer(icount_rt_timer
,
994 qemu_get_clock(rt_clock
) + 1000);
998 static void icount_adjust_vm(void * opaque
)
1000 qemu_mod_timer(icount_vm_timer
,
1001 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1005 static void init_icount_adjust(void)
1007 /* Have both realtime and virtual time triggers for speed adjustment.
1008 The realtime trigger catches emulated time passing too slowly,
1009 the virtual time trigger catches emulated time passing too fast.
1010 Realtime triggers occur even when idle, so use them less frequently
1011 than VM triggers. */
1012 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1013 qemu_mod_timer(icount_rt_timer
,
1014 qemu_get_clock(rt_clock
) + 1000);
1015 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1016 qemu_mod_timer(icount_vm_timer
,
1017 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1020 static struct qemu_alarm_timer alarm_timers
[] = {
1023 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1024 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1025 /* HPET - if available - is preferred */
1026 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1027 /* ...otherwise try RTC */
1028 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1030 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1032 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1033 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1034 {"win32", 0, win32_start_timer
,
1035 win32_stop_timer
, NULL
, &alarm_win32_data
},
1040 static void show_available_alarms(void)
1044 printf("Available alarm timers, in order of precedence:\n");
1045 for (i
= 0; alarm_timers
[i
].name
; i
++)
1046 printf("%s\n", alarm_timers
[i
].name
);
1049 static void configure_alarms(char const *opt
)
1053 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1056 struct qemu_alarm_timer tmp
;
1058 if (!strcmp(opt
, "?")) {
1059 show_available_alarms();
1065 /* Reorder the array */
1066 name
= strtok(arg
, ",");
1068 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1069 if (!strcmp(alarm_timers
[i
].name
, name
))
1074 fprintf(stderr
, "Unknown clock %s\n", name
);
1083 tmp
= alarm_timers
[i
];
1084 alarm_timers
[i
] = alarm_timers
[cur
];
1085 alarm_timers
[cur
] = tmp
;
1089 name
= strtok(NULL
, ",");
1095 /* Disable remaining timers */
1096 for (i
= cur
; i
< count
; i
++)
1097 alarm_timers
[i
].name
= NULL
;
1099 show_available_alarms();
1104 QEMUClock
*rt_clock
;
1105 QEMUClock
*vm_clock
;
1107 static QEMUTimer
*active_timers
[2];
1109 static QEMUClock
*qemu_new_clock(int type
)
1112 clock
= qemu_mallocz(sizeof(QEMUClock
));
1119 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1123 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1126 ts
->opaque
= opaque
;
1130 void qemu_free_timer(QEMUTimer
*ts
)
1135 /* stop a timer, but do not dealloc it */
1136 void qemu_del_timer(QEMUTimer
*ts
)
1140 /* NOTE: this code must be signal safe because
1141 qemu_timer_expired() can be called from a signal. */
1142 pt
= &active_timers
[ts
->clock
->type
];
1155 /* modify the current timer so that it will be fired when current_time
1156 >= expire_time. The corresponding callback will be called. */
1157 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1163 /* add the timer in the sorted list */
1164 /* NOTE: this code must be signal safe because
1165 qemu_timer_expired() can be called from a signal. */
1166 pt
= &active_timers
[ts
->clock
->type
];
1171 if (t
->expire_time
> expire_time
)
1175 ts
->expire_time
= expire_time
;
1179 /* Rearm if necessary */
1180 if (pt
== &active_timers
[ts
->clock
->type
]) {
1181 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1182 qemu_rearm_alarm_timer(alarm_timer
);
1184 /* Interrupt execution to force deadline recalculation. */
1185 if (use_icount
&& cpu_single_env
) {
1186 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1191 int qemu_timer_pending(QEMUTimer
*ts
)
1194 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1201 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1205 return (timer_head
->expire_time
<= current_time
);
1208 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1214 if (!ts
|| ts
->expire_time
> current_time
)
1216 /* remove timer from the list before calling the callback */
1217 *ptimer_head
= ts
->next
;
1220 /* run the callback (the timer list can be modified) */
1225 int64_t qemu_get_clock(QEMUClock
*clock
)
1227 switch(clock
->type
) {
1228 case QEMU_TIMER_REALTIME
:
1229 return get_clock() / 1000000;
1231 case QEMU_TIMER_VIRTUAL
:
1233 return cpu_get_icount();
1235 return cpu_get_clock();
1240 static void init_timers(void)
1243 ticks_per_sec
= QEMU_TIMER_BASE
;
1244 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1245 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1249 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1251 uint64_t expire_time
;
1253 if (qemu_timer_pending(ts
)) {
1254 expire_time
= ts
->expire_time
;
1258 qemu_put_be64(f
, expire_time
);
1261 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1263 uint64_t expire_time
;
1265 expire_time
= qemu_get_be64(f
);
1266 if (expire_time
!= -1) {
1267 qemu_mod_timer(ts
, expire_time
);
1273 static void timer_save(QEMUFile
*f
, void *opaque
)
1275 if (cpu_ticks_enabled
) {
1276 hw_error("cannot save state if virtual timers are running");
1278 qemu_put_be64(f
, cpu_ticks_offset
);
1279 qemu_put_be64(f
, ticks_per_sec
);
1280 qemu_put_be64(f
, cpu_clock_offset
);
1283 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1285 if (version_id
!= 1 && version_id
!= 2)
1287 if (cpu_ticks_enabled
) {
1290 cpu_ticks_offset
=qemu_get_be64(f
);
1291 ticks_per_sec
=qemu_get_be64(f
);
1292 if (version_id
== 2) {
1293 cpu_clock_offset
=qemu_get_be64(f
);
1299 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1300 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1302 static void host_alarm_handler(int host_signum
)
1306 #define DISP_FREQ 1000
1308 static int64_t delta_min
= INT64_MAX
;
1309 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1311 ti
= qemu_get_clock(vm_clock
);
1312 if (last_clock
!= 0) {
1313 delta
= ti
- last_clock
;
1314 if (delta
< delta_min
)
1316 if (delta
> delta_max
)
1319 if (++count
== DISP_FREQ
) {
1320 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1321 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1322 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1323 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1324 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1326 delta_min
= INT64_MAX
;
1334 if (alarm_has_dynticks(alarm_timer
) ||
1336 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1337 qemu_get_clock(vm_clock
))) ||
1338 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1339 qemu_get_clock(rt_clock
))) {
1340 CPUState
*env
= next_cpu
;
1343 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1344 SetEvent(data
->host_alarm
);
1346 static const char byte
= 0;
1347 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1349 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1352 /* stop the currently executing cpu because a timer occured */
1353 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1355 if (env
->kqemu_enabled
) {
1356 kqemu_cpu_interrupt(env
);
1364 static int64_t qemu_next_deadline(void)
1368 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1369 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1370 qemu_get_clock(vm_clock
);
1372 /* To avoid problems with overflow limit this to 2^32. */
1382 #if defined(__linux__) || defined(_WIN32)
1383 static uint64_t qemu_next_deadline_dyntick(void)
1391 delta
= (qemu_next_deadline() + 999) / 1000;
1393 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1394 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1395 qemu_get_clock(rt_clock
))*1000;
1396 if (rtdelta
< delta
)
1400 if (delta
< MIN_TIMER_REARM_US
)
1401 delta
= MIN_TIMER_REARM_US
;
1409 /* Sets a specific flag */
1410 static int fcntl_setfl(int fd
, int flag
)
1414 flags
= fcntl(fd
, F_GETFL
);
1418 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1424 #if defined(__linux__)
1426 #define RTC_FREQ 1024
1428 static void enable_sigio_timer(int fd
)
1430 struct sigaction act
;
1433 sigfillset(&act
.sa_mask
);
1435 act
.sa_handler
= host_alarm_handler
;
1437 sigaction(SIGIO
, &act
, NULL
);
1438 fcntl_setfl(fd
, O_ASYNC
);
1439 fcntl(fd
, F_SETOWN
, getpid());
1442 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1444 struct hpet_info info
;
1447 fd
= open("/dev/hpet", O_RDONLY
);
1452 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1454 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1455 "error, but for better emulation accuracy type:\n"
1456 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1460 /* Check capabilities */
1461 r
= ioctl(fd
, HPET_INFO
, &info
);
1465 /* Enable periodic mode */
1466 r
= ioctl(fd
, HPET_EPI
, 0);
1467 if (info
.hi_flags
&& (r
< 0))
1470 /* Enable interrupt */
1471 r
= ioctl(fd
, HPET_IE_ON
, 0);
1475 enable_sigio_timer(fd
);
1476 t
->priv
= (void *)(long)fd
;
1484 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1486 int fd
= (long)t
->priv
;
1491 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1494 unsigned long current_rtc_freq
= 0;
1496 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1499 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1500 if (current_rtc_freq
!= RTC_FREQ
&&
1501 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1502 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1503 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1504 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1507 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1513 enable_sigio_timer(rtc_fd
);
1515 t
->priv
= (void *)(long)rtc_fd
;
1520 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1522 int rtc_fd
= (long)t
->priv
;
1527 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1531 struct sigaction act
;
1533 sigfillset(&act
.sa_mask
);
1535 act
.sa_handler
= host_alarm_handler
;
1537 sigaction(SIGALRM
, &act
, NULL
);
1539 ev
.sigev_value
.sival_int
= 0;
1540 ev
.sigev_notify
= SIGEV_SIGNAL
;
1541 ev
.sigev_signo
= SIGALRM
;
1543 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1544 perror("timer_create");
1546 /* disable dynticks */
1547 fprintf(stderr
, "Dynamic Ticks disabled\n");
1552 t
->priv
= (void *)(long)host_timer
;
1557 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1559 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1561 timer_delete(host_timer
);
1564 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1566 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1567 struct itimerspec timeout
;
1568 int64_t nearest_delta_us
= INT64_MAX
;
1571 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1572 !active_timers
[QEMU_TIMER_VIRTUAL
])
1575 nearest_delta_us
= qemu_next_deadline_dyntick();
1577 /* check whether a timer is already running */
1578 if (timer_gettime(host_timer
, &timeout
)) {
1580 fprintf(stderr
, "Internal timer error: aborting\n");
1583 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1584 if (current_us
&& current_us
<= nearest_delta_us
)
1587 timeout
.it_interval
.tv_sec
= 0;
1588 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1589 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1590 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1591 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1593 fprintf(stderr
, "Internal timer error: aborting\n");
1598 #endif /* defined(__linux__) */
1600 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1602 struct sigaction act
;
1603 struct itimerval itv
;
1607 sigfillset(&act
.sa_mask
);
1609 act
.sa_handler
= host_alarm_handler
;
1611 sigaction(SIGALRM
, &act
, NULL
);
1613 itv
.it_interval
.tv_sec
= 0;
1614 /* for i386 kernel 2.6 to get 1 ms */
1615 itv
.it_interval
.tv_usec
= 999;
1616 itv
.it_value
.tv_sec
= 0;
1617 itv
.it_value
.tv_usec
= 10 * 1000;
1619 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1626 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1628 struct itimerval itv
;
1630 memset(&itv
, 0, sizeof(itv
));
1631 setitimer(ITIMER_REAL
, &itv
, NULL
);
1634 #endif /* !defined(_WIN32) */
1636 static void try_to_rearm_timer(void *opaque
)
1638 struct qemu_alarm_timer
*t
= opaque
;
1642 /* Drain the notify pipe */
1645 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1646 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1649 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1650 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1651 qemu_rearm_alarm_timer(alarm_timer
);
1657 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1660 struct qemu_alarm_win32
*data
= t
->priv
;
1663 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1664 if (!data
->host_alarm
) {
1665 perror("Failed CreateEvent");
1669 memset(&tc
, 0, sizeof(tc
));
1670 timeGetDevCaps(&tc
, sizeof(tc
));
1672 if (data
->period
< tc
.wPeriodMin
)
1673 data
->period
= tc
.wPeriodMin
;
1675 timeBeginPeriod(data
->period
);
1677 flags
= TIME_CALLBACK_FUNCTION
;
1678 if (alarm_has_dynticks(t
))
1679 flags
|= TIME_ONESHOT
;
1681 flags
|= TIME_PERIODIC
;
1683 data
->timerId
= timeSetEvent(1, // interval (ms)
1684 data
->period
, // resolution
1685 host_alarm_handler
, // function
1686 (DWORD
)t
, // parameter
1689 if (!data
->timerId
) {
1690 perror("Failed to initialize win32 alarm timer");
1692 timeEndPeriod(data
->period
);
1693 CloseHandle(data
->host_alarm
);
1697 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1702 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1704 struct qemu_alarm_win32
*data
= t
->priv
;
1706 timeKillEvent(data
->timerId
);
1707 timeEndPeriod(data
->period
);
1709 CloseHandle(data
->host_alarm
);
1712 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1714 struct qemu_alarm_win32
*data
= t
->priv
;
1715 uint64_t nearest_delta_us
;
1717 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1718 !active_timers
[QEMU_TIMER_VIRTUAL
])
1721 nearest_delta_us
= qemu_next_deadline_dyntick();
1722 nearest_delta_us
/= 1000;
1724 timeKillEvent(data
->timerId
);
1726 data
->timerId
= timeSetEvent(1,
1730 TIME_ONESHOT
| TIME_PERIODIC
);
1732 if (!data
->timerId
) {
1733 perror("Failed to re-arm win32 alarm timer");
1735 timeEndPeriod(data
->period
);
1736 CloseHandle(data
->host_alarm
);
1743 static int init_timer_alarm(void)
1745 struct qemu_alarm_timer
*t
= NULL
;
1755 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1759 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1763 alarm_timer_rfd
= fds
[0];
1764 alarm_timer_wfd
= fds
[1];
1767 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1768 t
= &alarm_timers
[i
];
1781 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1782 try_to_rearm_timer
, NULL
, t
);
1797 static void quit_timers(void)
1799 alarm_timer
->stop(alarm_timer
);
1803 /***********************************************************/
1804 /* host time/date access */
1805 void qemu_get_timedate(struct tm
*tm
, int offset
)
1812 if (rtc_date_offset
== -1) {
1816 ret
= localtime(&ti
);
1818 ti
-= rtc_date_offset
;
1822 memcpy(tm
, ret
, sizeof(struct tm
));
1825 int qemu_timedate_diff(struct tm
*tm
)
1829 if (rtc_date_offset
== -1)
1831 seconds
= mktimegm(tm
);
1833 seconds
= mktime(tm
);
1835 seconds
= mktimegm(tm
) + rtc_date_offset
;
1837 return seconds
- time(NULL
);
1841 static void socket_cleanup(void)
1846 static int socket_init(void)
1851 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1853 err
= WSAGetLastError();
1854 fprintf(stderr
, "WSAStartup: %d\n", err
);
1857 atexit(socket_cleanup
);
1862 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1867 while (*p
!= '\0' && *p
!= '=') {
1868 if (q
&& (q
- buf
) < buf_size
- 1)
1878 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1883 while (*p
!= '\0') {
1885 if (*(p
+ 1) != ',')
1889 if (q
&& (q
- buf
) < buf_size
- 1)
1899 int get_param_value(char *buf
, int buf_size
,
1900 const char *tag
, const char *str
)
1907 p
= get_opt_name(option
, sizeof(option
), p
);
1911 if (!strcmp(tag
, option
)) {
1912 (void)get_opt_value(buf
, buf_size
, p
);
1915 p
= get_opt_value(NULL
, 0, p
);
1924 int check_params(char *buf
, int buf_size
,
1925 const char * const *params
, const char *str
)
1932 p
= get_opt_name(buf
, buf_size
, p
);
1936 for(i
= 0; params
[i
] != NULL
; i
++)
1937 if (!strcmp(params
[i
], buf
))
1939 if (params
[i
] == NULL
)
1941 p
= get_opt_value(NULL
, 0, p
);
1949 /***********************************************************/
1950 /* Bluetooth support */
1953 static struct HCIInfo
*hci_table
[MAX_NICS
];
1955 static struct bt_vlan_s
{
1956 struct bt_scatternet_s net
;
1958 struct bt_vlan_s
*next
;
1961 /* find or alloc a new bluetooth "VLAN" */
1962 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1964 struct bt_vlan_s
**pvlan
, *vlan
;
1965 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1969 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1971 pvlan
= &first_bt_vlan
;
1972 while (*pvlan
!= NULL
)
1973 pvlan
= &(*pvlan
)->next
;
1978 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1982 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1987 static struct HCIInfo null_hci
= {
1988 .cmd_send
= null_hci_send
,
1989 .sco_send
= null_hci_send
,
1990 .acl_send
= null_hci_send
,
1991 .bdaddr_set
= null_hci_addr_set
,
1994 struct HCIInfo
*qemu_next_hci(void)
1996 if (cur_hci
== nb_hcis
)
1999 return hci_table
[cur_hci
++];
2002 static struct HCIInfo
*hci_init(const char *str
)
2005 struct bt_scatternet_s
*vlan
= 0;
2007 if (!strcmp(str
, "null"))
2010 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2012 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2013 else if (!strncmp(str
, "hci", 3)) {
2016 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2017 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2022 vlan
= qemu_find_bt_vlan(0);
2024 return bt_new_hci(vlan
);
2027 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2032 static int bt_hci_parse(const char *str
)
2034 struct HCIInfo
*hci
;
2037 if (nb_hcis
>= MAX_NICS
) {
2038 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2042 hci
= hci_init(str
);
2051 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2052 hci
->bdaddr_set(hci
, bdaddr
.b
);
2054 hci_table
[nb_hcis
++] = hci
;
2059 static void bt_vhci_add(int vlan_id
)
2061 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2064 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2065 "an empty scatternet %i\n", vlan_id
);
2067 bt_vhci_init(bt_new_hci(vlan
));
2070 static struct bt_device_s
*bt_device_add(const char *opt
)
2072 struct bt_scatternet_s
*vlan
;
2074 char *endp
= strstr(opt
, ",vlan=");
2075 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2078 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2081 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2083 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2088 vlan
= qemu_find_bt_vlan(vlan_id
);
2091 fprintf(stderr
, "qemu: warning: adding a slave device to "
2092 "an empty scatternet %i\n", vlan_id
);
2094 if (!strcmp(devname
, "keyboard"))
2095 return bt_keyboard_init(vlan
);
2097 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2101 static int bt_parse(const char *opt
)
2103 const char *endp
, *p
;
2106 if (strstart(opt
, "hci", &endp
)) {
2107 if (!*endp
|| *endp
== ',') {
2109 if (!strstart(endp
, ",vlan=", 0))
2112 return bt_hci_parse(opt
);
2114 } else if (strstart(opt
, "vhci", &endp
)) {
2115 if (!*endp
|| *endp
== ',') {
2117 if (strstart(endp
, ",vlan=", &p
)) {
2118 vlan
= strtol(p
, (char **) &endp
, 0);
2120 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2124 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2133 } else if (strstart(opt
, "device:", &endp
))
2134 return !bt_device_add(endp
);
2136 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2140 /***********************************************************/
2141 /* QEMU Block devices */
2143 #define HD_ALIAS "index=%d,media=disk"
2145 #define CDROM_ALIAS "index=1,media=cdrom"
2147 #define CDROM_ALIAS "index=2,media=cdrom"
2149 #define FD_ALIAS "index=%d,if=floppy"
2150 #define PFLASH_ALIAS "if=pflash"
2151 #define MTD_ALIAS "if=mtd"
2152 #define SD_ALIAS "index=0,if=sd"
2154 static int drive_add(const char *file
, const char *fmt
, ...)
2158 if (nb_drives_opt
>= MAX_DRIVES
) {
2159 fprintf(stderr
, "qemu: too many drives\n");
2163 drives_opt
[nb_drives_opt
].file
= file
;
2165 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2166 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2169 return nb_drives_opt
++;
2172 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2176 /* seek interface, bus and unit */
2178 for (index
= 0; index
< nb_drives
; index
++)
2179 if (drives_table
[index
].type
== type
&&
2180 drives_table
[index
].bus
== bus
&&
2181 drives_table
[index
].unit
== unit
)
2187 int drive_get_max_bus(BlockInterfaceType type
)
2193 for (index
= 0; index
< nb_drives
; index
++) {
2194 if(drives_table
[index
].type
== type
&&
2195 drives_table
[index
].bus
> max_bus
)
2196 max_bus
= drives_table
[index
].bus
;
2201 const char *drive_get_serial(BlockDriverState
*bdrv
)
2205 for (index
= 0; index
< nb_drives
; index
++)
2206 if (drives_table
[index
].bdrv
== bdrv
)
2207 return drives_table
[index
].serial
;
2212 static void bdrv_format_print(void *opaque
, const char *name
)
2214 fprintf(stderr
, " %s", name
);
2217 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2218 QEMUMachine
*machine
)
2224 const char *mediastr
= "";
2225 BlockInterfaceType type
;
2226 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2227 int bus_id
, unit_id
;
2228 int cyls
, heads
, secs
, translation
;
2229 BlockDriverState
*bdrv
;
2230 BlockDriver
*drv
= NULL
;
2235 char *str
= arg
->opt
;
2236 static const char * const params
[] = { "bus", "unit", "if", "index",
2237 "cyls", "heads", "secs", "trans",
2238 "media", "snapshot", "file",
2239 "cache", "format", "serial", NULL
};
2241 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2242 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2248 cyls
= heads
= secs
= 0;
2251 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2255 if (machine
->use_scsi
) {
2257 max_devs
= MAX_SCSI_DEVS
;
2258 pstrcpy(devname
, sizeof(devname
), "scsi");
2261 max_devs
= MAX_IDE_DEVS
;
2262 pstrcpy(devname
, sizeof(devname
), "ide");
2266 /* extract parameters */
2268 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2269 bus_id
= strtol(buf
, NULL
, 0);
2271 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2276 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2277 unit_id
= strtol(buf
, NULL
, 0);
2279 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2284 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2285 pstrcpy(devname
, sizeof(devname
), buf
);
2286 if (!strcmp(buf
, "ide")) {
2288 max_devs
= MAX_IDE_DEVS
;
2289 } else if (!strcmp(buf
, "scsi")) {
2291 max_devs
= MAX_SCSI_DEVS
;
2292 } else if (!strcmp(buf
, "floppy")) {
2295 } else if (!strcmp(buf
, "pflash")) {
2298 } else if (!strcmp(buf
, "mtd")) {
2301 } else if (!strcmp(buf
, "sd")) {
2304 } else if (!strcmp(buf
, "virtio")) {
2308 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2313 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2314 index
= strtol(buf
, NULL
, 0);
2316 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2321 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2322 cyls
= strtol(buf
, NULL
, 0);
2325 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2326 heads
= strtol(buf
, NULL
, 0);
2329 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2330 secs
= strtol(buf
, NULL
, 0);
2333 if (cyls
|| heads
|| secs
) {
2334 if (cyls
< 1 || cyls
> 16383) {
2335 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2338 if (heads
< 1 || heads
> 16) {
2339 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2342 if (secs
< 1 || secs
> 63) {
2343 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2348 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2351 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2355 if (!strcmp(buf
, "none"))
2356 translation
= BIOS_ATA_TRANSLATION_NONE
;
2357 else if (!strcmp(buf
, "lba"))
2358 translation
= BIOS_ATA_TRANSLATION_LBA
;
2359 else if (!strcmp(buf
, "auto"))
2360 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2362 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2367 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2368 if (!strcmp(buf
, "disk")) {
2370 } else if (!strcmp(buf
, "cdrom")) {
2371 if (cyls
|| secs
|| heads
) {
2373 "qemu: '%s' invalid physical CHS format\n", str
);
2376 media
= MEDIA_CDROM
;
2378 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2383 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2384 if (!strcmp(buf
, "on"))
2386 else if (!strcmp(buf
, "off"))
2389 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2394 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2395 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2397 else if (!strcmp(buf
, "writethrough"))
2399 else if (!strcmp(buf
, "writeback"))
2402 fprintf(stderr
, "qemu: invalid cache option\n");
2407 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2408 if (strcmp(buf
, "?") == 0) {
2409 fprintf(stderr
, "qemu: Supported formats:");
2410 bdrv_iterate_format(bdrv_format_print
, NULL
);
2411 fprintf(stderr
, "\n");
2414 drv
= bdrv_find_format(buf
);
2416 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2421 if (arg
->file
== NULL
)
2422 get_param_value(file
, sizeof(file
), "file", str
);
2424 pstrcpy(file
, sizeof(file
), arg
->file
);
2426 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2427 memset(serial
, 0, sizeof(serial
));
2429 /* compute bus and unit according index */
2432 if (bus_id
!= 0 || unit_id
!= -1) {
2434 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2442 unit_id
= index
% max_devs
;
2443 bus_id
= index
/ max_devs
;
2447 /* if user doesn't specify a unit_id,
2448 * try to find the first free
2451 if (unit_id
== -1) {
2453 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2455 if (max_devs
&& unit_id
>= max_devs
) {
2456 unit_id
-= max_devs
;
2464 if (max_devs
&& unit_id
>= max_devs
) {
2465 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2466 str
, unit_id
, max_devs
- 1);
2471 * ignore multiple definitions
2474 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2479 if (type
== IF_IDE
|| type
== IF_SCSI
)
2480 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2482 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2483 devname
, bus_id
, mediastr
, unit_id
);
2485 snprintf(buf
, sizeof(buf
), "%s%s%i",
2486 devname
, mediastr
, unit_id
);
2487 bdrv
= bdrv_new(buf
);
2488 drives_table
[nb_drives
].bdrv
= bdrv
;
2489 drives_table
[nb_drives
].type
= type
;
2490 drives_table
[nb_drives
].bus
= bus_id
;
2491 drives_table
[nb_drives
].unit
= unit_id
;
2492 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2501 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2502 bdrv_set_translation_hint(bdrv
, translation
);
2506 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2511 /* FIXME: This isn't really a floppy, but it's a reasonable
2514 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2525 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2526 cache
= 2; /* always use write-back with snapshot */
2528 if (cache
== 0) /* no caching */
2529 bdrv_flags
|= BDRV_O_NOCACHE
;
2530 else if (cache
== 2) /* write-back */
2531 bdrv_flags
|= BDRV_O_CACHE_WB
;
2532 else if (cache
== 3) /* not specified */
2533 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2534 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2535 fprintf(stderr
, "qemu: could not open disk image %s\n",
2542 /***********************************************************/
2545 static USBPort
*used_usb_ports
;
2546 static USBPort
*free_usb_ports
;
2548 /* ??? Maybe change this to register a hub to keep track of the topology. */
2549 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2550 usb_attachfn attach
)
2552 port
->opaque
= opaque
;
2553 port
->index
= index
;
2554 port
->attach
= attach
;
2555 port
->next
= free_usb_ports
;
2556 free_usb_ports
= port
;
2559 int usb_device_add_dev(USBDevice
*dev
)
2563 /* Find a USB port to add the device to. */
2564 port
= free_usb_ports
;
2568 /* Create a new hub and chain it on. */
2569 free_usb_ports
= NULL
;
2570 port
->next
= used_usb_ports
;
2571 used_usb_ports
= port
;
2573 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2574 usb_attach(port
, hub
);
2575 port
= free_usb_ports
;
2578 free_usb_ports
= port
->next
;
2579 port
->next
= used_usb_ports
;
2580 used_usb_ports
= port
;
2581 usb_attach(port
, dev
);
2585 static int usb_device_add(const char *devname
)
2590 if (!free_usb_ports
)
2593 if (strstart(devname
, "host:", &p
)) {
2594 dev
= usb_host_device_open(p
);
2595 } else if (!strcmp(devname
, "mouse")) {
2596 dev
= usb_mouse_init();
2597 } else if (!strcmp(devname
, "tablet")) {
2598 dev
= usb_tablet_init();
2599 } else if (!strcmp(devname
, "keyboard")) {
2600 dev
= usb_keyboard_init();
2601 } else if (strstart(devname
, "disk:", &p
)) {
2602 dev
= usb_msd_init(p
);
2603 } else if (!strcmp(devname
, "wacom-tablet")) {
2604 dev
= usb_wacom_init();
2605 } else if (strstart(devname
, "serial:", &p
)) {
2606 dev
= usb_serial_init(p
);
2607 #ifdef CONFIG_BRLAPI
2608 } else if (!strcmp(devname
, "braille")) {
2609 dev
= usb_baum_init();
2611 } else if (strstart(devname
, "net:", &p
)) {
2614 if (net_client_init("nic", p
) < 0)
2616 nd_table
[nic
].model
= "usb";
2617 dev
= usb_net_init(&nd_table
[nic
]);
2618 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2619 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2620 bt_new_hci(qemu_find_bt_vlan(0)));
2627 return usb_device_add_dev(dev
);
2630 int usb_device_del_addr(int bus_num
, int addr
)
2636 if (!used_usb_ports
)
2642 lastp
= &used_usb_ports
;
2643 port
= used_usb_ports
;
2644 while (port
&& port
->dev
->addr
!= addr
) {
2645 lastp
= &port
->next
;
2653 *lastp
= port
->next
;
2654 usb_attach(port
, NULL
);
2655 dev
->handle_destroy(dev
);
2656 port
->next
= free_usb_ports
;
2657 free_usb_ports
= port
;
2661 static int usb_device_del(const char *devname
)
2666 if (strstart(devname
, "host:", &p
))
2667 return usb_host_device_close(p
);
2669 if (!used_usb_ports
)
2672 p
= strchr(devname
, '.');
2675 bus_num
= strtoul(devname
, NULL
, 0);
2676 addr
= strtoul(p
+ 1, NULL
, 0);
2678 return usb_device_del_addr(bus_num
, addr
);
2681 void do_usb_add(const char *devname
)
2683 usb_device_add(devname
);
2686 void do_usb_del(const char *devname
)
2688 usb_device_del(devname
);
2695 const char *speed_str
;
2698 term_printf("USB support not enabled\n");
2702 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2706 switch(dev
->speed
) {
2710 case USB_SPEED_FULL
:
2713 case USB_SPEED_HIGH
:
2720 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2721 0, dev
->addr
, speed_str
, dev
->devname
);
2725 /***********************************************************/
2726 /* PCMCIA/Cardbus */
2728 static struct pcmcia_socket_entry_s
{
2729 struct pcmcia_socket_s
*socket
;
2730 struct pcmcia_socket_entry_s
*next
;
2731 } *pcmcia_sockets
= 0;
2733 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2735 struct pcmcia_socket_entry_s
*entry
;
2737 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2738 entry
->socket
= socket
;
2739 entry
->next
= pcmcia_sockets
;
2740 pcmcia_sockets
= entry
;
2743 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2745 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2747 ptr
= &pcmcia_sockets
;
2748 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2749 if (entry
->socket
== socket
) {
2755 void pcmcia_info(void)
2757 struct pcmcia_socket_entry_s
*iter
;
2758 if (!pcmcia_sockets
)
2759 term_printf("No PCMCIA sockets\n");
2761 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2762 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2763 iter
->socket
->attached
? iter
->socket
->card_string
:
2767 /***********************************************************/
2770 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2774 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2778 static void dumb_display_init(DisplayState
*ds
)
2783 ds
->dpy_update
= dumb_update
;
2784 ds
->dpy_resize
= dumb_resize
;
2785 ds
->dpy_refresh
= NULL
;
2786 ds
->gui_timer_interval
= 0;
2790 /***********************************************************/
2793 #define MAX_IO_HANDLERS 64
2795 typedef struct IOHandlerRecord
{
2797 IOCanRWHandler
*fd_read_poll
;
2799 IOHandler
*fd_write
;
2802 /* temporary data */
2804 struct IOHandlerRecord
*next
;
2807 static IOHandlerRecord
*first_io_handler
;
2809 /* XXX: fd_read_poll should be suppressed, but an API change is
2810 necessary in the character devices to suppress fd_can_read(). */
2811 int qemu_set_fd_handler2(int fd
,
2812 IOCanRWHandler
*fd_read_poll
,
2814 IOHandler
*fd_write
,
2817 IOHandlerRecord
**pioh
, *ioh
;
2819 if (!fd_read
&& !fd_write
) {
2820 pioh
= &first_io_handler
;
2825 if (ioh
->fd
== fd
) {
2832 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2836 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2839 ioh
->next
= first_io_handler
;
2840 first_io_handler
= ioh
;
2843 ioh
->fd_read_poll
= fd_read_poll
;
2844 ioh
->fd_read
= fd_read
;
2845 ioh
->fd_write
= fd_write
;
2846 ioh
->opaque
= opaque
;
2852 int qemu_set_fd_handler(int fd
,
2854 IOHandler
*fd_write
,
2857 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2861 /***********************************************************/
2862 /* Polling handling */
2864 typedef struct PollingEntry
{
2867 struct PollingEntry
*next
;
2870 static PollingEntry
*first_polling_entry
;
2872 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2874 PollingEntry
**ppe
, *pe
;
2875 pe
= qemu_mallocz(sizeof(PollingEntry
));
2879 pe
->opaque
= opaque
;
2880 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2885 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2887 PollingEntry
**ppe
, *pe
;
2888 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2890 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2898 /***********************************************************/
2899 /* Wait objects support */
2900 typedef struct WaitObjects
{
2902 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2903 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2904 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2907 static WaitObjects wait_objects
= {0};
2909 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2911 WaitObjects
*w
= &wait_objects
;
2913 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2915 w
->events
[w
->num
] = handle
;
2916 w
->func
[w
->num
] = func
;
2917 w
->opaque
[w
->num
] = opaque
;
2922 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2925 WaitObjects
*w
= &wait_objects
;
2928 for (i
= 0; i
< w
->num
; i
++) {
2929 if (w
->events
[i
] == handle
)
2932 w
->events
[i
] = w
->events
[i
+ 1];
2933 w
->func
[i
] = w
->func
[i
+ 1];
2934 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2942 /***********************************************************/
2943 /* ram save/restore */
2945 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2949 v
= qemu_get_byte(f
);
2952 if (qemu_get_buffer(f
, buf
, len
) != len
)
2956 v
= qemu_get_byte(f
);
2957 memset(buf
, v
, len
);
2963 if (qemu_file_has_error(f
))
2969 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2974 if (qemu_get_be32(f
) != phys_ram_size
)
2976 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2977 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2984 #define BDRV_HASH_BLOCK_SIZE 1024
2985 #define IOBUF_SIZE 4096
2986 #define RAM_CBLOCK_MAGIC 0xfabe
2988 typedef struct RamDecompressState
{
2991 uint8_t buf
[IOBUF_SIZE
];
2992 } RamDecompressState
;
2994 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2997 memset(s
, 0, sizeof(*s
));
2999 ret
= inflateInit(&s
->zstream
);
3005 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3009 s
->zstream
.avail_out
= len
;
3010 s
->zstream
.next_out
= buf
;
3011 while (s
->zstream
.avail_out
> 0) {
3012 if (s
->zstream
.avail_in
== 0) {
3013 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3015 clen
= qemu_get_be16(s
->f
);
3016 if (clen
> IOBUF_SIZE
)
3018 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3019 s
->zstream
.avail_in
= clen
;
3020 s
->zstream
.next_in
= s
->buf
;
3022 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3023 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3030 static void ram_decompress_close(RamDecompressState
*s
)
3032 inflateEnd(&s
->zstream
);
3035 #define RAM_SAVE_FLAG_FULL 0x01
3036 #define RAM_SAVE_FLAG_COMPRESS 0x02
3037 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3038 #define RAM_SAVE_FLAG_PAGE 0x08
3039 #define RAM_SAVE_FLAG_EOS 0x10
3041 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3043 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3044 uint32_t *array
= (uint32_t *)page
;
3047 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3048 if (array
[i
] != val
)
3055 static int ram_save_block(QEMUFile
*f
)
3057 static ram_addr_t current_addr
= 0;
3058 ram_addr_t saved_addr
= current_addr
;
3059 ram_addr_t addr
= 0;
3062 while (addr
< phys_ram_size
) {
3063 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3066 cpu_physical_memory_reset_dirty(current_addr
,
3067 current_addr
+ TARGET_PAGE_SIZE
,
3068 MIGRATION_DIRTY_FLAG
);
3070 ch
= *(phys_ram_base
+ current_addr
);
3072 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3073 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3074 qemu_put_byte(f
, ch
);
3076 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3077 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3083 addr
+= TARGET_PAGE_SIZE
;
3084 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3090 static ram_addr_t ram_save_threshold
= 10;
3092 static ram_addr_t
ram_save_remaining(void)
3095 ram_addr_t count
= 0;
3097 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3098 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3105 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3110 /* Make sure all dirty bits are set */
3111 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3112 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3113 cpu_physical_memory_set_dirty(addr
);
3116 /* Enable dirty memory tracking */
3117 cpu_physical_memory_set_dirty_tracking(1);
3119 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3122 while (!qemu_file_rate_limit(f
)) {
3125 ret
= ram_save_block(f
);
3126 if (ret
== 0) /* no more blocks */
3130 /* try transferring iterative blocks of memory */
3133 cpu_physical_memory_set_dirty_tracking(0);
3135 /* flush all remaining blocks regardless of rate limiting */
3136 while (ram_save_block(f
) != 0);
3139 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3141 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3144 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3146 RamDecompressState s1
, *s
= &s1
;
3150 if (ram_decompress_open(s
, f
) < 0)
3152 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3153 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3154 fprintf(stderr
, "Error while reading ram block header\n");
3158 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3159 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3164 printf("Error block header\n");
3168 ram_decompress_close(s
);
3173 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3178 if (version_id
== 1)
3179 return ram_load_v1(f
, opaque
);
3181 if (version_id
== 2) {
3182 if (qemu_get_be32(f
) != phys_ram_size
)
3184 return ram_load_dead(f
, opaque
);
3187 if (version_id
!= 3)
3191 addr
= qemu_get_be64(f
);
3193 flags
= addr
& ~TARGET_PAGE_MASK
;
3194 addr
&= TARGET_PAGE_MASK
;
3196 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3197 if (addr
!= phys_ram_size
)
3201 if (flags
& RAM_SAVE_FLAG_FULL
) {
3202 if (ram_load_dead(f
, opaque
) < 0)
3206 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3207 uint8_t ch
= qemu_get_byte(f
);
3208 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3209 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3210 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3211 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3216 void qemu_service_io(void)
3218 CPUState
*env
= cpu_single_env
;
3220 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3222 if (env
->kqemu_enabled
) {
3223 kqemu_cpu_interrupt(env
);
3229 /***********************************************************/
3230 /* bottom halves (can be seen as timers which expire ASAP) */
3241 static QEMUBH
*first_bh
= NULL
;
3243 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3246 bh
= qemu_mallocz(sizeof(QEMUBH
));
3250 bh
->opaque
= opaque
;
3251 bh
->next
= first_bh
;
3256 int qemu_bh_poll(void)
3262 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3263 if (!bh
->deleted
&& bh
->scheduled
) {
3272 /* remove deleted bhs */
3286 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3294 void qemu_bh_schedule(QEMUBH
*bh
)
3296 CPUState
*env
= cpu_single_env
;
3301 /* stop the currently executing CPU to execute the BH ASAP */
3303 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3307 void qemu_bh_cancel(QEMUBH
*bh
)
3312 void qemu_bh_delete(QEMUBH
*bh
)
3318 static void qemu_bh_update_timeout(int *timeout
)
3322 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3323 if (!bh
->deleted
&& bh
->scheduled
) {
3325 /* idle bottom halves will be polled at least
3327 *timeout
= MIN(10, *timeout
);
3329 /* non-idle bottom halves will be executed
3338 /***********************************************************/
3339 /* machine registration */
3341 static QEMUMachine
*first_machine
= NULL
;
3343 int qemu_register_machine(QEMUMachine
*m
)
3346 pm
= &first_machine
;
3354 static QEMUMachine
*find_machine(const char *name
)
3358 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3359 if (!strcmp(m
->name
, name
))
3365 /***********************************************************/
3366 /* main execution loop */
3368 static void gui_update(void *opaque
)
3370 DisplayState
*ds
= opaque
;
3371 ds
->dpy_refresh(ds
);
3372 qemu_mod_timer(ds
->gui_timer
,
3373 (ds
->gui_timer_interval
?
3374 ds
->gui_timer_interval
:
3375 GUI_REFRESH_INTERVAL
)
3376 + qemu_get_clock(rt_clock
));
3379 struct vm_change_state_entry
{
3380 VMChangeStateHandler
*cb
;
3382 LIST_ENTRY (vm_change_state_entry
) entries
;
3385 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3387 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3390 VMChangeStateEntry
*e
;
3392 e
= qemu_mallocz(sizeof (*e
));
3398 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3402 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3404 LIST_REMOVE (e
, entries
);
3408 static void vm_state_notify(int running
)
3410 VMChangeStateEntry
*e
;
3412 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3413 e
->cb(e
->opaque
, running
);
3417 /* XXX: support several handlers */
3418 static VMStopHandler
*vm_stop_cb
;
3419 static void *vm_stop_opaque
;
3421 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3424 vm_stop_opaque
= opaque
;
3428 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3439 qemu_rearm_alarm_timer(alarm_timer
);
3443 void vm_stop(int reason
)
3446 cpu_disable_ticks();
3450 vm_stop_cb(vm_stop_opaque
, reason
);
3457 /* reset/shutdown handler */
3459 typedef struct QEMUResetEntry
{
3460 QEMUResetHandler
*func
;
3462 struct QEMUResetEntry
*next
;
3465 static QEMUResetEntry
*first_reset_entry
;
3466 static int reset_requested
;
3467 static int shutdown_requested
;
3468 static int powerdown_requested
;
3470 int qemu_shutdown_requested(void)
3472 int r
= shutdown_requested
;
3473 shutdown_requested
= 0;
3477 int qemu_reset_requested(void)
3479 int r
= reset_requested
;
3480 reset_requested
= 0;
3484 int qemu_powerdown_requested(void)
3486 int r
= powerdown_requested
;
3487 powerdown_requested
= 0;
3491 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3493 QEMUResetEntry
**pre
, *re
;
3495 pre
= &first_reset_entry
;
3496 while (*pre
!= NULL
)
3497 pre
= &(*pre
)->next
;
3498 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3500 re
->opaque
= opaque
;
3505 void qemu_system_reset(void)
3509 /* reset all devices */
3510 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3511 re
->func(re
->opaque
);
3515 void qemu_system_reset_request(void)
3518 shutdown_requested
= 1;
3520 reset_requested
= 1;
3523 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3526 void qemu_system_shutdown_request(void)
3528 shutdown_requested
= 1;
3530 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3533 void qemu_system_powerdown_request(void)
3535 powerdown_requested
= 1;
3537 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3541 static void host_main_loop_wait(int *timeout
)
3547 /* XXX: need to suppress polling by better using win32 events */
3549 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3550 ret
|= pe
->func(pe
->opaque
);
3554 WaitObjects
*w
= &wait_objects
;
3556 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3557 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3558 if (w
->func
[ret
- WAIT_OBJECT_0
])
3559 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3561 /* Check for additional signaled events */
3562 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3564 /* Check if event is signaled */
3565 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3566 if(ret2
== WAIT_OBJECT_0
) {
3568 w
->func
[i
](w
->opaque
[i
]);
3569 } else if (ret2
== WAIT_TIMEOUT
) {
3571 err
= GetLastError();
3572 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3575 } else if (ret
== WAIT_TIMEOUT
) {
3577 err
= GetLastError();
3578 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3585 static void host_main_loop_wait(int *timeout
)
3590 void main_loop_wait(int timeout
)
3592 IOHandlerRecord
*ioh
;
3593 fd_set rfds
, wfds
, xfds
;
3597 qemu_bh_update_timeout(&timeout
);
3599 host_main_loop_wait(&timeout
);
3601 /* poll any events */
3602 /* XXX: separate device handlers from system ones */
3607 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3611 (!ioh
->fd_read_poll
||
3612 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3613 FD_SET(ioh
->fd
, &rfds
);
3617 if (ioh
->fd_write
) {
3618 FD_SET(ioh
->fd
, &wfds
);
3624 tv
.tv_sec
= timeout
/ 1000;
3625 tv
.tv_usec
= (timeout
% 1000) * 1000;
3627 #if defined(CONFIG_SLIRP)
3628 if (slirp_is_inited()) {
3629 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3632 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3634 IOHandlerRecord
**pioh
;
3636 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3637 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3638 ioh
->fd_read(ioh
->opaque
);
3640 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3641 ioh
->fd_write(ioh
->opaque
);
3645 /* remove deleted IO handlers */
3646 pioh
= &first_io_handler
;
3656 #if defined(CONFIG_SLIRP)
3657 if (slirp_is_inited()) {
3663 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3667 /* vm time timers */
3668 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3669 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3670 qemu_get_clock(vm_clock
));
3672 /* real time timers */
3673 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3674 qemu_get_clock(rt_clock
));
3676 /* Check bottom-halves last in case any of the earlier events triggered
3682 static int main_loop(void)
3685 #ifdef CONFIG_PROFILER
3690 cur_cpu
= first_cpu
;
3691 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3698 #ifdef CONFIG_PROFILER
3699 ti
= profile_getclock();
3704 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3705 env
->icount_decr
.u16
.low
= 0;
3706 env
->icount_extra
= 0;
3707 count
= qemu_next_deadline();
3708 count
= (count
+ (1 << icount_time_shift
) - 1)
3709 >> icount_time_shift
;
3710 qemu_icount
+= count
;
3711 decr
= (count
> 0xffff) ? 0xffff : count
;
3713 env
->icount_decr
.u16
.low
= decr
;
3714 env
->icount_extra
= count
;
3716 ret
= cpu_exec(env
);
3717 #ifdef CONFIG_PROFILER
3718 qemu_time
+= profile_getclock() - ti
;
3721 /* Fold pending instructions back into the
3722 instruction counter, and clear the interrupt flag. */
3723 qemu_icount
-= (env
->icount_decr
.u16
.low
3724 + env
->icount_extra
);
3725 env
->icount_decr
.u32
= 0;
3726 env
->icount_extra
= 0;
3728 next_cpu
= env
->next_cpu
?: first_cpu
;
3729 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3730 ret
= EXCP_INTERRUPT
;
3734 if (ret
== EXCP_HLT
) {
3735 /* Give the next CPU a chance to run. */
3739 if (ret
!= EXCP_HALTED
)
3741 /* all CPUs are halted ? */
3747 if (shutdown_requested
) {
3748 ret
= EXCP_INTERRUPT
;
3756 if (reset_requested
) {
3757 reset_requested
= 0;
3758 qemu_system_reset();
3759 ret
= EXCP_INTERRUPT
;
3761 if (powerdown_requested
) {
3762 powerdown_requested
= 0;
3763 qemu_system_powerdown();
3764 ret
= EXCP_INTERRUPT
;
3766 if (unlikely(ret
== EXCP_DEBUG
)) {
3767 gdb_set_stop_cpu(cur_cpu
);
3768 vm_stop(EXCP_DEBUG
);
3770 /* If all cpus are halted then wait until the next IRQ */
3771 /* XXX: use timeout computed from timers */
3772 if (ret
== EXCP_HALTED
) {
3776 /* Advance virtual time to the next event. */
3777 if (use_icount
== 1) {
3778 /* When not using an adaptive execution frequency
3779 we tend to get badly out of sync with real time,
3780 so just delay for a reasonable amount of time. */
3783 delta
= cpu_get_icount() - cpu_get_clock();
3786 /* If virtual time is ahead of real time then just
3788 timeout
= (delta
/ 1000000) + 1;
3790 /* Wait for either IO to occur or the next
3792 add
= qemu_next_deadline();
3793 /* We advance the timer before checking for IO.
3794 Limit the amount we advance so that early IO
3795 activity won't get the guest too far ahead. */
3799 add
= (add
+ (1 << icount_time_shift
) - 1)
3800 >> icount_time_shift
;
3802 timeout
= delta
/ 1000000;
3813 if (shutdown_requested
) {
3814 ret
= EXCP_INTERRUPT
;
3819 #ifdef CONFIG_PROFILER
3820 ti
= profile_getclock();
3822 main_loop_wait(timeout
);
3823 #ifdef CONFIG_PROFILER
3824 dev_time
+= profile_getclock() - ti
;
3827 cpu_disable_ticks();
3831 static void help(int exitcode
)
3833 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3834 "usage: %s [options] [disk_image]\n"
3836 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3838 "Standard options:\n"
3839 "-M machine select emulated machine (-M ? for list)\n"
3840 "-cpu cpu select CPU (-cpu ? for list)\n"
3841 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3842 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3843 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3844 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3845 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3846 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3847 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3848 " use 'file' as a drive image\n"
3849 "-mtdblock file use 'file' as on-board Flash memory image\n"
3850 "-sd file use 'file' as SecureDigital card image\n"
3851 "-pflash file use 'file' as a parallel flash image\n"
3852 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3853 "-snapshot write to temporary files instead of disk image files\n"
3855 "-no-frame open SDL window without a frame and window decorations\n"
3856 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3857 "-no-quit disable SDL window close capability\n"
3860 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3862 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3863 "-smp n set the number of CPUs to 'n' [default=1]\n"
3864 "-nographic disable graphical output and redirect serial I/Os to console\n"
3865 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3867 "-k language use keyboard layout (for example \"fr\" for French)\n"
3870 "-audio-help print list of audio drivers and their options\n"
3871 "-soundhw c1,... enable audio support\n"
3872 " and only specified sound cards (comma separated list)\n"
3873 " use -soundhw ? to get the list of supported cards\n"
3874 " use -soundhw all to enable all of them\n"
3876 "-vga [std|cirrus|vmware]\n"
3877 " select video card type\n"
3878 "-localtime set the real time clock to local time [default=utc]\n"
3879 "-full-screen start in full screen\n"
3881 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3882 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
3884 "-usb enable the USB driver (will be the default soon)\n"
3885 "-usbdevice name add the host or guest USB device 'name'\n"
3886 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3887 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3889 "-name string set the name of the guest\n"
3890 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3892 "Network options:\n"
3893 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3894 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3896 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3897 " connect the user mode network stack to VLAN 'n' and send\n"
3898 " hostname 'host' to DHCP clients\n"
3901 "-net tap[,vlan=n][,name=str],ifname=name\n"
3902 " connect the host TAP network interface to VLAN 'n'\n"
3904 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3905 " connect the host TAP network interface to VLAN 'n' and use the\n"
3906 " network scripts 'file' (default=%s)\n"
3907 " and 'dfile' (default=%s);\n"
3908 " use '[down]script=no' to disable script execution;\n"
3909 " use 'fd=h' to connect to an already opened TAP interface\n"
3911 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3912 " connect the vlan 'n' to another VLAN using a socket connection\n"
3913 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3914 " connect the vlan 'n' to multicast maddr and port\n"
3916 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3917 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3918 " on host and listening for incoming connections on 'socketpath'.\n"
3919 " Use group 'groupname' and mode 'octalmode' to change default\n"
3920 " ownership and permissions for communication port.\n"
3922 "-net none use it alone to have zero network devices; if no -net option\n"
3923 " is provided, the default is '-net nic -net user'\n"
3925 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3926 "-bt hci,host[:id]\n"
3927 " Use host's HCI with the given name\n"
3928 "-bt hci[,vlan=n]\n"
3929 " Emulate a standard HCI in virtual scatternet 'n'\n"
3930 "-bt vhci[,vlan=n]\n"
3931 " Add host computer to virtual scatternet 'n' using VHCI\n"
3932 "-bt device:dev[,vlan=n]\n"
3933 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3936 "-tftp dir allow tftp access to files in dir [-net user]\n"
3937 "-bootp file advertise file in BOOTP replies\n"
3939 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3941 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3942 " redirect TCP or UDP connections from host to guest [-net user]\n"
3945 "Linux boot specific:\n"
3946 "-kernel bzImage use 'bzImage' as kernel image\n"
3947 "-append cmdline use 'cmdline' as kernel command line\n"
3948 "-initrd file use 'file' as initial ram disk\n"
3950 "Debug/Expert options:\n"
3951 "-monitor dev redirect the monitor to char device 'dev'\n"
3952 "-serial dev redirect the serial port to char device 'dev'\n"
3953 "-parallel dev redirect the parallel port to char device 'dev'\n"
3954 "-pidfile file Write PID to 'file'\n"
3955 "-S freeze CPU at startup (use 'c' to start execution)\n"
3956 "-s wait gdb connection to port\n"
3957 "-p port set gdb connection port [default=%s]\n"
3958 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3959 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3960 " translation (t=none or lba) (usually qemu can guess them)\n"
3961 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3963 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3964 "-no-kqemu disable KQEMU kernel module usage\n"
3967 "-enable-kvm enable KVM full virtualization support\n"
3970 "-no-acpi disable ACPI\n"
3971 "-no-hpet disable HPET\n"
3973 #ifdef CONFIG_CURSES
3974 "-curses use a curses/ncurses interface instead of SDL\n"
3976 "-no-reboot exit instead of rebooting\n"
3977 "-no-shutdown stop before shutdown\n"
3978 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3979 "-vnc display start a VNC server on display\n"
3981 "-daemonize daemonize QEMU after initializing\n"
3983 "-option-rom rom load a file, rom, into the option ROM space\n"
3985 "-prom-env variable=value set OpenBIOS nvram variables\n"
3987 "-clock force the use of the given methods for timer alarm.\n"
3988 " To see what timers are available use -clock ?\n"
3989 "-startdate select initial date of the clock\n"
3990 "-icount [N|auto]\n"
3991 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
3993 "During emulation, the following keys are useful:\n"
3994 "ctrl-alt-f toggle full screen\n"
3995 "ctrl-alt-n switch to virtual console 'n'\n"
3996 "ctrl-alt toggle mouse and keyboard grab\n"
3998 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4003 DEFAULT_NETWORK_SCRIPT
,
4004 DEFAULT_NETWORK_DOWN_SCRIPT
,
4006 DEFAULT_GDBSTUB_PORT
,
4011 #define HAS_ARG 0x0001
4026 QEMU_OPTION_mtdblock
,
4030 QEMU_OPTION_snapshot
,
4032 QEMU_OPTION_no_fd_bootchk
,
4035 QEMU_OPTION_nographic
,
4036 QEMU_OPTION_portrait
,
4038 QEMU_OPTION_audio_help
,
4039 QEMU_OPTION_soundhw
,
4061 QEMU_OPTION_localtime
,
4065 QEMU_OPTION_monitor
,
4067 QEMU_OPTION_virtiocon
,
4068 QEMU_OPTION_parallel
,
4070 QEMU_OPTION_full_screen
,
4071 QEMU_OPTION_no_frame
,
4072 QEMU_OPTION_alt_grab
,
4073 QEMU_OPTION_no_quit
,
4074 QEMU_OPTION_pidfile
,
4075 QEMU_OPTION_no_kqemu
,
4076 QEMU_OPTION_kernel_kqemu
,
4077 QEMU_OPTION_enable_kvm
,
4078 QEMU_OPTION_win2k_hack
,
4079 QEMU_OPTION_rtc_td_hack
,
4081 QEMU_OPTION_usbdevice
,
4084 QEMU_OPTION_no_acpi
,
4085 QEMU_OPTION_no_hpet
,
4087 QEMU_OPTION_no_reboot
,
4088 QEMU_OPTION_no_shutdown
,
4089 QEMU_OPTION_show_cursor
,
4090 QEMU_OPTION_daemonize
,
4091 QEMU_OPTION_option_rom
,
4092 QEMU_OPTION_semihosting
,
4094 QEMU_OPTION_prom_env
,
4095 QEMU_OPTION_old_param
,
4097 QEMU_OPTION_startdate
,
4098 QEMU_OPTION_tb_size
,
4101 QEMU_OPTION_incoming
,
4104 typedef struct QEMUOption
{
4110 static const QEMUOption qemu_options
[] = {
4111 { "h", 0, QEMU_OPTION_h
},
4112 { "help", 0, QEMU_OPTION_h
},
4114 { "M", HAS_ARG
, QEMU_OPTION_M
},
4115 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4116 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4117 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4118 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4119 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4120 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4121 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4122 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4123 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4124 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4125 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4126 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4127 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4128 { "snapshot", 0, QEMU_OPTION_snapshot
},
4130 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4132 { "m", HAS_ARG
, QEMU_OPTION_m
},
4133 { "nographic", 0, QEMU_OPTION_nographic
},
4134 { "portrait", 0, QEMU_OPTION_portrait
},
4135 { "k", HAS_ARG
, QEMU_OPTION_k
},
4137 { "audio-help", 0, QEMU_OPTION_audio_help
},
4138 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4141 { "net", HAS_ARG
, QEMU_OPTION_net
},
4143 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4144 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4146 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4148 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4150 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4152 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4153 { "append", HAS_ARG
, QEMU_OPTION_append
},
4154 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4156 { "S", 0, QEMU_OPTION_S
},
4157 { "s", 0, QEMU_OPTION_s
},
4158 { "p", HAS_ARG
, QEMU_OPTION_p
},
4159 { "d", HAS_ARG
, QEMU_OPTION_d
},
4160 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4161 { "L", HAS_ARG
, QEMU_OPTION_L
},
4162 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4164 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4165 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4168 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4170 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4171 { "g", 1, QEMU_OPTION_g
},
4173 { "localtime", 0, QEMU_OPTION_localtime
},
4174 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4175 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4176 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4177 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4178 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4179 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4180 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4181 { "full-screen", 0, QEMU_OPTION_full_screen
},
4183 { "no-frame", 0, QEMU_OPTION_no_frame
},
4184 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4185 { "no-quit", 0, QEMU_OPTION_no_quit
},
4187 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4188 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4189 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4190 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4191 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4192 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4193 #ifdef CONFIG_CURSES
4194 { "curses", 0, QEMU_OPTION_curses
},
4196 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4198 /* temporary options */
4199 { "usb", 0, QEMU_OPTION_usb
},
4200 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4201 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4202 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4203 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4204 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4205 { "daemonize", 0, QEMU_OPTION_daemonize
},
4206 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4207 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4208 { "semihosting", 0, QEMU_OPTION_semihosting
},
4210 { "name", HAS_ARG
, QEMU_OPTION_name
},
4211 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4212 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4214 #if defined(TARGET_ARM)
4215 { "old-param", 0, QEMU_OPTION_old_param
},
4217 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4218 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4219 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4220 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4221 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4225 /* password input */
4227 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4232 if (!bdrv_is_encrypted(bs
))
4235 term_printf("%s is encrypted.\n", name
);
4236 for(i
= 0; i
< 3; i
++) {
4237 monitor_readline("Password: ", 1, password
, sizeof(password
));
4238 if (bdrv_set_key(bs
, password
) == 0)
4240 term_printf("invalid password\n");
4245 static BlockDriverState
*get_bdrv(int index
)
4247 if (index
> nb_drives
)
4249 return drives_table
[index
].bdrv
;
4252 static void read_passwords(void)
4254 BlockDriverState
*bs
;
4257 for(i
= 0; i
< 6; i
++) {
4260 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4265 struct soundhw soundhw
[] = {
4266 #ifdef HAS_AUDIO_CHOICE
4267 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4273 { .init_isa
= pcspk_audio_init
}
4280 "Creative Sound Blaster 16",
4283 { .init_isa
= SB16_init
}
4287 #ifdef CONFIG_CS4231A
4293 { .init_isa
= cs4231a_init
}
4301 "Yamaha YMF262 (OPL3)",
4303 "Yamaha YM3812 (OPL2)",
4307 { .init_isa
= Adlib_init
}
4314 "Gravis Ultrasound GF1",
4317 { .init_isa
= GUS_init
}
4324 "Intel 82801AA AC97 Audio",
4327 { .init_pci
= ac97_init
}
4331 #ifdef CONFIG_ES1370
4334 "ENSONIQ AudioPCI ES1370",
4337 { .init_pci
= es1370_init
}
4341 #endif /* HAS_AUDIO_CHOICE */
4343 { NULL
, NULL
, 0, 0, { NULL
} }
4346 static void select_soundhw (const char *optarg
)
4350 if (*optarg
== '?') {
4353 printf ("Valid sound card names (comma separated):\n");
4354 for (c
= soundhw
; c
->name
; ++c
) {
4355 printf ("%-11s %s\n", c
->name
, c
->descr
);
4357 printf ("\n-soundhw all will enable all of the above\n");
4358 exit (*optarg
!= '?');
4366 if (!strcmp (optarg
, "all")) {
4367 for (c
= soundhw
; c
->name
; ++c
) {
4375 e
= strchr (p
, ',');
4376 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4378 for (c
= soundhw
; c
->name
; ++c
) {
4379 if (!strncmp (c
->name
, p
, l
)) {
4388 "Unknown sound card name (too big to show)\n");
4391 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4396 p
+= l
+ (e
!= NULL
);
4400 goto show_valid_cards
;
4405 static void select_vgahw (const char *p
)
4409 if (strstart(p
, "std", &opts
)) {
4410 cirrus_vga_enabled
= 0;
4412 } else if (strstart(p
, "cirrus", &opts
)) {
4413 cirrus_vga_enabled
= 1;
4415 } else if (strstart(p
, "vmware", &opts
)) {
4416 cirrus_vga_enabled
= 0;
4420 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4424 const char *nextopt
;
4426 if (strstart(opts
, ",retrace=", &nextopt
)) {
4428 if (strstart(opts
, "dumb", &nextopt
))
4429 vga_retrace_method
= VGA_RETRACE_DUMB
;
4430 else if (strstart(opts
, "precise", &nextopt
))
4431 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4432 else goto invalid_vga
;
4433 } else goto invalid_vga
;
4439 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4441 exit(STATUS_CONTROL_C_EXIT
);
4446 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4450 if(strlen(str
) != 36)
4453 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4454 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4455 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4463 #define MAX_NET_CLIENTS 32
4467 static void termsig_handler(int signal
)
4469 qemu_system_shutdown_request();
4472 static void termsig_setup(void)
4474 struct sigaction act
;
4476 memset(&act
, 0, sizeof(act
));
4477 act
.sa_handler
= termsig_handler
;
4478 sigaction(SIGINT
, &act
, NULL
);
4479 sigaction(SIGHUP
, &act
, NULL
);
4480 sigaction(SIGTERM
, &act
, NULL
);
4485 int main(int argc
, char **argv
, char **envp
)
4487 #ifdef CONFIG_GDBSTUB
4489 const char *gdbstub_port
;
4491 uint32_t boot_devices_bitmap
= 0;
4493 int snapshot
, linux_boot
, net_boot
;
4494 const char *initrd_filename
;
4495 const char *kernel_filename
, *kernel_cmdline
;
4496 const char *boot_devices
= "";
4497 DisplayState
*ds
= &display_state
;
4498 int cyls
, heads
, secs
, translation
;
4499 const char *net_clients
[MAX_NET_CLIENTS
];
4501 const char *bt_opts
[MAX_BT_CMDLINE
];
4505 const char *r
, *optarg
;
4506 CharDriverState
*monitor_hd
;
4507 const char *monitor_device
;
4508 const char *serial_devices
[MAX_SERIAL_PORTS
];
4509 int serial_device_index
;
4510 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4511 int parallel_device_index
;
4512 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4513 int virtio_console_index
;
4514 const char *loadvm
= NULL
;
4515 QEMUMachine
*machine
;
4516 const char *cpu_model
;
4517 const char *usb_devices
[MAX_USB_CMDLINE
];
4518 int usb_devices_index
;
4521 const char *pid_file
= NULL
;
4523 const char *incoming
= NULL
;
4525 qemu_cache_utils_init(envp
);
4527 LIST_INIT (&vm_change_state_head
);
4530 struct sigaction act
;
4531 sigfillset(&act
.sa_mask
);
4533 act
.sa_handler
= SIG_IGN
;
4534 sigaction(SIGPIPE
, &act
, NULL
);
4537 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4538 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4539 QEMU to run on a single CPU */
4544 h
= GetCurrentProcess();
4545 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4546 for(i
= 0; i
< 32; i
++) {
4547 if (mask
& (1 << i
))
4552 SetProcessAffinityMask(h
, mask
);
4558 register_machines();
4559 machine
= first_machine
;
4561 initrd_filename
= NULL
;
4563 vga_ram_size
= VGA_RAM_SIZE
;
4564 #ifdef CONFIG_GDBSTUB
4566 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4571 kernel_filename
= NULL
;
4572 kernel_cmdline
= "";
4573 cyls
= heads
= secs
= 0;
4574 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4575 monitor_device
= "vc";
4577 serial_devices
[0] = "vc:80Cx24C";
4578 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4579 serial_devices
[i
] = NULL
;
4580 serial_device_index
= 0;
4582 parallel_devices
[0] = "vc:640x480";
4583 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4584 parallel_devices
[i
] = NULL
;
4585 parallel_device_index
= 0;
4587 virtio_consoles
[0] = "vc:80Cx24C";
4588 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4589 virtio_consoles
[i
] = NULL
;
4590 virtio_console_index
= 0;
4592 usb_devices_index
= 0;
4611 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4613 const QEMUOption
*popt
;
4616 /* Treat --foo the same as -foo. */
4619 popt
= qemu_options
;
4622 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4626 if (!strcmp(popt
->name
, r
+ 1))
4630 if (popt
->flags
& HAS_ARG
) {
4631 if (optind
>= argc
) {
4632 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4636 optarg
= argv
[optind
++];
4641 switch(popt
->index
) {
4643 machine
= find_machine(optarg
);
4646 printf("Supported machines are:\n");
4647 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4648 printf("%-10s %s%s\n",
4650 m
== first_machine
? " (default)" : "");
4652 exit(*optarg
!= '?');
4655 case QEMU_OPTION_cpu
:
4656 /* hw initialization will check this */
4657 if (*optarg
== '?') {
4658 /* XXX: implement xxx_cpu_list for targets that still miss it */
4659 #if defined(cpu_list)
4660 cpu_list(stdout
, &fprintf
);
4667 case QEMU_OPTION_initrd
:
4668 initrd_filename
= optarg
;
4670 case QEMU_OPTION_hda
:
4672 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4674 hda_index
= drive_add(optarg
, HD_ALIAS
4675 ",cyls=%d,heads=%d,secs=%d%s",
4676 0, cyls
, heads
, secs
,
4677 translation
== BIOS_ATA_TRANSLATION_LBA
?
4679 translation
== BIOS_ATA_TRANSLATION_NONE
?
4680 ",trans=none" : "");
4682 case QEMU_OPTION_hdb
:
4683 case QEMU_OPTION_hdc
:
4684 case QEMU_OPTION_hdd
:
4685 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4687 case QEMU_OPTION_drive
:
4688 drive_add(NULL
, "%s", optarg
);
4690 case QEMU_OPTION_mtdblock
:
4691 drive_add(optarg
, MTD_ALIAS
);
4693 case QEMU_OPTION_sd
:
4694 drive_add(optarg
, SD_ALIAS
);
4696 case QEMU_OPTION_pflash
:
4697 drive_add(optarg
, PFLASH_ALIAS
);
4699 case QEMU_OPTION_snapshot
:
4702 case QEMU_OPTION_hdachs
:
4706 cyls
= strtol(p
, (char **)&p
, 0);
4707 if (cyls
< 1 || cyls
> 16383)
4712 heads
= strtol(p
, (char **)&p
, 0);
4713 if (heads
< 1 || heads
> 16)
4718 secs
= strtol(p
, (char **)&p
, 0);
4719 if (secs
< 1 || secs
> 63)
4723 if (!strcmp(p
, "none"))
4724 translation
= BIOS_ATA_TRANSLATION_NONE
;
4725 else if (!strcmp(p
, "lba"))
4726 translation
= BIOS_ATA_TRANSLATION_LBA
;
4727 else if (!strcmp(p
, "auto"))
4728 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4731 } else if (*p
!= '\0') {
4733 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4736 if (hda_index
!= -1)
4737 snprintf(drives_opt
[hda_index
].opt
,
4738 sizeof(drives_opt
[hda_index
].opt
),
4739 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4740 0, cyls
, heads
, secs
,
4741 translation
== BIOS_ATA_TRANSLATION_LBA
?
4743 translation
== BIOS_ATA_TRANSLATION_NONE
?
4744 ",trans=none" : "");
4747 case QEMU_OPTION_nographic
:
4750 #ifdef CONFIG_CURSES
4751 case QEMU_OPTION_curses
:
4755 case QEMU_OPTION_portrait
:
4758 case QEMU_OPTION_kernel
:
4759 kernel_filename
= optarg
;
4761 case QEMU_OPTION_append
:
4762 kernel_cmdline
= optarg
;
4764 case QEMU_OPTION_cdrom
:
4765 drive_add(optarg
, CDROM_ALIAS
);
4767 case QEMU_OPTION_boot
:
4768 boot_devices
= optarg
;
4769 /* We just do some generic consistency checks */
4771 /* Could easily be extended to 64 devices if needed */
4774 boot_devices_bitmap
= 0;
4775 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4776 /* Allowed boot devices are:
4777 * a b : floppy disk drives
4778 * c ... f : IDE disk drives
4779 * g ... m : machine implementation dependant drives
4780 * n ... p : network devices
4781 * It's up to each machine implementation to check
4782 * if the given boot devices match the actual hardware
4783 * implementation and firmware features.
4785 if (*p
< 'a' || *p
> 'q') {
4786 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4789 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4791 "Boot device '%c' was given twice\n",*p
);
4794 boot_devices_bitmap
|= 1 << (*p
- 'a');
4798 case QEMU_OPTION_fda
:
4799 case QEMU_OPTION_fdb
:
4800 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4803 case QEMU_OPTION_no_fd_bootchk
:
4807 case QEMU_OPTION_net
:
4808 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4809 fprintf(stderr
, "qemu: too many network clients\n");
4812 net_clients
[nb_net_clients
] = optarg
;
4816 case QEMU_OPTION_tftp
:
4817 tftp_prefix
= optarg
;
4819 case QEMU_OPTION_bootp
:
4820 bootp_filename
= optarg
;
4823 case QEMU_OPTION_smb
:
4824 net_slirp_smb(optarg
);
4827 case QEMU_OPTION_redir
:
4828 net_slirp_redir(optarg
);
4831 case QEMU_OPTION_bt
:
4832 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4833 fprintf(stderr
, "qemu: too many bluetooth options\n");
4836 bt_opts
[nb_bt_opts
++] = optarg
;
4839 case QEMU_OPTION_audio_help
:
4843 case QEMU_OPTION_soundhw
:
4844 select_soundhw (optarg
);
4850 case QEMU_OPTION_m
: {
4854 value
= strtoul(optarg
, &ptr
, 10);
4856 case 0: case 'M': case 'm':
4863 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4867 /* On 32-bit hosts, QEMU is limited by virtual address space */
4868 if (value
> (2047 << 20)
4870 && HOST_LONG_BITS
== 32
4873 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4876 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4877 fprintf(stderr
, "qemu: ram size too large\n");
4886 const CPULogItem
*item
;
4888 mask
= cpu_str_to_log_mask(optarg
);
4890 printf("Log items (comma separated):\n");
4891 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4892 printf("%-10s %s\n", item
->name
, item
->help
);
4899 #ifdef CONFIG_GDBSTUB
4904 gdbstub_port
= optarg
;
4910 case QEMU_OPTION_bios
:
4917 keyboard_layout
= optarg
;
4919 case QEMU_OPTION_localtime
:
4922 case QEMU_OPTION_vga
:
4923 select_vgahw (optarg
);
4930 w
= strtol(p
, (char **)&p
, 10);
4933 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4939 h
= strtol(p
, (char **)&p
, 10);
4944 depth
= strtol(p
, (char **)&p
, 10);
4945 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4946 depth
!= 24 && depth
!= 32)
4948 } else if (*p
== '\0') {
4949 depth
= graphic_depth
;
4956 graphic_depth
= depth
;
4959 case QEMU_OPTION_echr
:
4962 term_escape_char
= strtol(optarg
, &r
, 0);
4964 printf("Bad argument to echr\n");
4967 case QEMU_OPTION_monitor
:
4968 monitor_device
= optarg
;
4970 case QEMU_OPTION_serial
:
4971 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4972 fprintf(stderr
, "qemu: too many serial ports\n");
4975 serial_devices
[serial_device_index
] = optarg
;
4976 serial_device_index
++;
4978 case QEMU_OPTION_virtiocon
:
4979 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
4980 fprintf(stderr
, "qemu: too many virtio consoles\n");
4983 virtio_consoles
[virtio_console_index
] = optarg
;
4984 virtio_console_index
++;
4986 case QEMU_OPTION_parallel
:
4987 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4988 fprintf(stderr
, "qemu: too many parallel ports\n");
4991 parallel_devices
[parallel_device_index
] = optarg
;
4992 parallel_device_index
++;
4994 case QEMU_OPTION_loadvm
:
4997 case QEMU_OPTION_full_screen
:
5001 case QEMU_OPTION_no_frame
:
5004 case QEMU_OPTION_alt_grab
:
5007 case QEMU_OPTION_no_quit
:
5011 case QEMU_OPTION_pidfile
:
5015 case QEMU_OPTION_win2k_hack
:
5016 win2k_install_hack
= 1;
5018 case QEMU_OPTION_rtc_td_hack
:
5023 case QEMU_OPTION_no_kqemu
:
5026 case QEMU_OPTION_kernel_kqemu
:
5031 case QEMU_OPTION_enable_kvm
:
5038 case QEMU_OPTION_usb
:
5041 case QEMU_OPTION_usbdevice
:
5043 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5044 fprintf(stderr
, "Too many USB devices\n");
5047 usb_devices
[usb_devices_index
] = optarg
;
5048 usb_devices_index
++;
5050 case QEMU_OPTION_smp
:
5051 smp_cpus
= atoi(optarg
);
5053 fprintf(stderr
, "Invalid number of CPUs\n");
5057 case QEMU_OPTION_vnc
:
5058 vnc_display
= optarg
;
5060 case QEMU_OPTION_no_acpi
:
5063 case QEMU_OPTION_no_hpet
:
5066 case QEMU_OPTION_no_reboot
:
5069 case QEMU_OPTION_no_shutdown
:
5072 case QEMU_OPTION_show_cursor
:
5075 case QEMU_OPTION_uuid
:
5076 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5077 fprintf(stderr
, "Fail to parse UUID string."
5078 " Wrong format.\n");
5082 case QEMU_OPTION_daemonize
:
5085 case QEMU_OPTION_option_rom
:
5086 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5087 fprintf(stderr
, "Too many option ROMs\n");
5090 option_rom
[nb_option_roms
] = optarg
;
5093 case QEMU_OPTION_semihosting
:
5094 semihosting_enabled
= 1;
5096 case QEMU_OPTION_name
:
5099 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5100 case QEMU_OPTION_prom_env
:
5101 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5102 fprintf(stderr
, "Too many prom variables\n");
5105 prom_envs
[nb_prom_envs
] = optarg
;
5110 case QEMU_OPTION_old_param
:
5114 case QEMU_OPTION_clock
:
5115 configure_alarms(optarg
);
5117 case QEMU_OPTION_startdate
:
5120 time_t rtc_start_date
;
5121 if (!strcmp(optarg
, "now")) {
5122 rtc_date_offset
= -1;
5124 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5132 } else if (sscanf(optarg
, "%d-%d-%d",
5135 &tm
.tm_mday
) == 3) {
5144 rtc_start_date
= mktimegm(&tm
);
5145 if (rtc_start_date
== -1) {
5147 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5148 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5151 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5155 case QEMU_OPTION_tb_size
:
5156 tb_size
= strtol(optarg
, NULL
, 0);
5160 case QEMU_OPTION_icount
:
5162 if (strcmp(optarg
, "auto") == 0) {
5163 icount_time_shift
= -1;
5165 icount_time_shift
= strtol(optarg
, NULL
, 0);
5168 case QEMU_OPTION_incoming
:
5175 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5176 if (kvm_allowed
&& kqemu_allowed
) {
5178 "You can not enable both KVM and kqemu at the same time\n");
5183 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5184 if (smp_cpus
> machine
->max_cpus
) {
5185 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5186 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5192 if (serial_device_index
== 0)
5193 serial_devices
[0] = "stdio";
5194 if (parallel_device_index
== 0)
5195 parallel_devices
[0] = "null";
5196 if (strncmp(monitor_device
, "vc", 2) == 0)
5197 monitor_device
= "stdio";
5198 if (virtio_console_index
== 0)
5199 virtio_consoles
[0] = "null";
5206 if (pipe(fds
) == -1)
5217 len
= read(fds
[0], &status
, 1);
5218 if (len
== -1 && (errno
== EINTR
))
5223 else if (status
== 1) {
5224 fprintf(stderr
, "Could not acquire pidfile\n");
5241 signal(SIGTSTP
, SIG_IGN
);
5242 signal(SIGTTOU
, SIG_IGN
);
5243 signal(SIGTTIN
, SIG_IGN
);
5247 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5250 write(fds
[1], &status
, 1);
5252 fprintf(stderr
, "Could not acquire pid file\n");
5260 linux_boot
= (kernel_filename
!= NULL
);
5261 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5263 if (!linux_boot
&& net_boot
== 0 &&
5264 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5267 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5268 fprintf(stderr
, "-append only allowed with -kernel option\n");
5272 if (!linux_boot
&& initrd_filename
!= NULL
) {
5273 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5277 /* boot to floppy or the default cd if no hard disk defined yet */
5278 if (!boot_devices
[0]) {
5279 boot_devices
= "cad";
5281 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5284 if (init_timer_alarm() < 0) {
5285 fprintf(stderr
, "could not initialize alarm timer\n");
5288 if (use_icount
&& icount_time_shift
< 0) {
5290 /* 125MIPS seems a reasonable initial guess at the guest speed.
5291 It will be corrected fairly quickly anyway. */
5292 icount_time_shift
= 3;
5293 init_icount_adjust();
5300 /* init network clients */
5301 if (nb_net_clients
== 0) {
5302 /* if no clients, we use a default config */
5303 net_clients
[nb_net_clients
++] = "nic";
5305 net_clients
[nb_net_clients
++] = "user";
5309 for(i
= 0;i
< nb_net_clients
; i
++) {
5310 if (net_client_parse(net_clients
[i
]) < 0)
5316 /* XXX: this should be moved in the PC machine instantiation code */
5317 if (net_boot
!= 0) {
5319 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5320 const char *model
= nd_table
[i
].model
;
5322 if (net_boot
& (1 << i
)) {
5325 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5326 if (get_image_size(buf
) > 0) {
5327 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5328 fprintf(stderr
, "Too many option ROMs\n");
5331 option_rom
[nb_option_roms
] = strdup(buf
);
5338 fprintf(stderr
, "No valid PXE rom found for network device\n");
5344 /* init the bluetooth world */
5345 for (i
= 0; i
< nb_bt_opts
; i
++)
5346 if (bt_parse(bt_opts
[i
]))
5349 /* init the memory */
5350 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5352 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5354 if (ram_size
< phys_ram_size
) {
5355 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5356 machine
->name
, (unsigned long long) phys_ram_size
);
5360 phys_ram_size
= ram_size
;
5362 ram_size
= phys_ram_size
;
5365 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5367 phys_ram_size
+= ram_size
;
5370 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5371 if (!phys_ram_base
) {
5372 fprintf(stderr
, "Could not allocate physical memory\n");
5376 /* init the dynamic translator */
5377 cpu_exec_init_all(tb_size
* 1024 * 1024);
5381 /* we always create the cdrom drive, even if no disk is there */
5383 if (nb_drives_opt
< MAX_DRIVES
)
5384 drive_add(NULL
, CDROM_ALIAS
);
5386 /* we always create at least one floppy */
5388 if (nb_drives_opt
< MAX_DRIVES
)
5389 drive_add(NULL
, FD_ALIAS
, 0);
5391 /* we always create one sd slot, even if no card is in it */
5393 if (nb_drives_opt
< MAX_DRIVES
)
5394 drive_add(NULL
, SD_ALIAS
);
5396 /* open the virtual block devices */
5398 for(i
= 0; i
< nb_drives_opt
; i
++)
5399 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5402 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5403 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5406 memset(&display_state
, 0, sizeof(display_state
));
5409 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5412 /* nearly nothing to do */
5413 dumb_display_init(ds
);
5414 } else if (vnc_display
!= NULL
) {
5415 vnc_display_init(ds
);
5416 if (vnc_display_open(ds
, vnc_display
) < 0)
5419 #if defined(CONFIG_CURSES)
5421 curses_display_init(ds
, full_screen
);
5425 #if defined(CONFIG_SDL)
5426 sdl_display_init(ds
, full_screen
, no_frame
);
5427 #elif defined(CONFIG_COCOA)
5428 cocoa_display_init(ds
, full_screen
);
5430 dumb_display_init(ds
);
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";
5453 if (monitor_device
) {
5454 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5456 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5459 monitor_init(monitor_hd
, !nographic
);
5462 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5463 const char *devname
= serial_devices
[i
];
5464 if (devname
&& strcmp(devname
, "none")) {
5466 snprintf(label
, sizeof(label
), "serial%d", i
);
5467 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5468 if (!serial_hds
[i
]) {
5469 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5473 if (strstart(devname
, "vc", 0))
5474 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
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",
5489 if (strstart(devname
, "vc", 0))
5490 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5494 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5495 const char *devname
= virtio_consoles
[i
];
5496 if (devname
&& strcmp(devname
, "none")) {
5498 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5499 virtcon_hds
[i
] = qemu_chr_open(label
, devname
);
5500 if (!virtcon_hds
[i
]) {
5501 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5505 if (strstart(devname
, "vc", 0))
5506 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5510 if (kvm_enabled()) {
5513 ret
= kvm_init(smp_cpus
);
5515 fprintf(stderr
, "failed to initialize KVM\n");
5520 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5521 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5523 /* Set KVM's vcpu state to qemu's initial CPUState. */
5524 if (kvm_enabled()) {
5527 ret
= kvm_sync_vcpus();
5529 fprintf(stderr
, "failed to initialize vcpus\n");
5534 /* init USB devices */
5536 for(i
= 0; i
< usb_devices_index
; i
++) {
5537 if (usb_device_add(usb_devices
[i
]) < 0) {
5538 fprintf(stderr
, "Warning: could not add USB device %s\n",
5544 if (display_state
.dpy_refresh
) {
5545 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5546 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5549 #ifdef CONFIG_GDBSTUB
5551 /* XXX: use standard host:port notation and modify options
5553 if (gdbserver_start(gdbstub_port
) < 0) {
5554 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5565 autostart
= 0; /* fixme how to deal with -daemonize */
5566 qemu_start_incoming_migration(incoming
);
5570 /* XXX: simplify init */
5583 len
= write(fds
[1], &status
, 1);
5584 if (len
== -1 && (errno
== EINTR
))
5591 TFR(fd
= open("/dev/null", O_RDWR
));