4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
39 #include "cache-utils.h"
41 #include "audio/audio.h"
42 #include "migration.h"
55 #include <sys/times.h>
59 #include <sys/ioctl.h>
60 #include <sys/resource.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
64 #if defined(__NetBSD__)
65 #include <net/if_tap.h>
68 #include <linux/if_tun.h>
70 #include <arpa/inet.h>
73 #include <sys/select.h>
81 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82 #include <freebsd/stdlib.h>
87 #include <linux/rtc.h>
89 /* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91 /* #include <linux/hpet.h> */
94 #include <linux/ppdev.h>
95 #include <linux/parport.h>
99 #include <sys/ethernet.h>
100 #include <sys/sockio.h>
101 #include <netinet/arp.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_icmp.h> // must come after ip.h
106 #include <netinet/udp.h>
107 #include <netinet/tcp.h>
115 #include "qemu_socket.h"
117 #if defined(CONFIG_SLIRP)
118 #include "libslirp.h"
121 #if defined(__OpenBSD__)
125 #if defined(CONFIG_VDE)
126 #include <libvdeplug.h>
131 #include <sys/timeb.h>
132 #include <mmsystem.h>
133 #define getopt_long_only getopt_long
134 #define memalign(align, size) malloc(size)
141 #endif /* CONFIG_SDL */
145 #define main qemu_main
146 #endif /* CONFIG_COCOA */
150 #include "exec-all.h"
152 //#define DEBUG_UNUSED_IOPORT
153 //#define DEBUG_IOPORT
155 //#define DEBUG_SLIRP
159 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
161 # define LOG_IOPORT(...) do { } while (0)
164 #define DEFAULT_RAM_SIZE 128
166 /* Max number of USB devices that can be specified on the commandline. */
167 #define MAX_USB_CMDLINE 8
169 /* Max number of bluetooth switches on the commandline. */
170 #define MAX_BT_CMDLINE 10
172 /* XXX: use a two level table to limit memory usage */
173 #define MAX_IOPORTS 65536
175 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
176 const char *bios_name
= NULL
;
177 static void *ioport_opaque
[MAX_IOPORTS
];
178 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
179 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
180 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
181 to store the VM snapshots */
182 DriveInfo drives_table
[MAX_DRIVES
+1];
184 static int vga_ram_size
;
185 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
186 static DisplayState
*display_state
;
190 const char* keyboard_layout
= NULL
;
191 int64_t ticks_per_sec
;
194 NICInfo nd_table
[MAX_NICS
];
196 static int rtc_utc
= 1;
197 static int rtc_date_offset
= -1; /* -1 means no change */
198 int cirrus_vga_enabled
= 1;
199 int std_vga_enabled
= 0;
200 int vmsvga_enabled
= 0;
202 int graphic_width
= 1024;
203 int graphic_height
= 768;
204 int graphic_depth
= 8;
206 int graphic_width
= 800;
207 int graphic_height
= 600;
208 int graphic_depth
= 15;
210 static int full_screen
= 0;
212 static int no_frame
= 0;
215 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
216 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
217 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
219 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
233 const char *option_rom
[MAX_OPTION_ROMS
];
235 int semihosting_enabled
= 0;
239 const char *qemu_name
;
241 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
242 unsigned int nb_prom_envs
= 0;
243 const char *prom_envs
[MAX_PROM_ENVS
];
246 struct drive_opt drives_opt
[MAX_DRIVES
];
248 static CPUState
*cur_cpu
;
249 static CPUState
*next_cpu
;
250 static int event_pending
= 1;
251 /* Conversion factor from emulated instructions to virtual clock ticks. */
252 static int icount_time_shift
;
253 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
254 #define MAX_ICOUNT_SHIFT 10
255 /* Compensate for varying guest execution speed. */
256 static int64_t qemu_icount_bias
;
257 static QEMUTimer
*icount_rt_timer
;
258 static QEMUTimer
*icount_vm_timer
;
259 static QEMUTimer
*nographic_timer
;
261 uint8_t qemu_uuid
[16];
263 /***********************************************************/
264 /* x86 ISA bus support */
266 target_phys_addr_t isa_mem_base
= 0;
269 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
270 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
272 static uint32_t ioport_read(int index
, uint32_t address
)
274 static IOPortReadFunc
*default_func
[3] = {
275 default_ioport_readb
,
276 default_ioport_readw
,
279 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
281 func
= default_func
[index
];
282 return func(ioport_opaque
[address
], address
);
285 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
287 static IOPortWriteFunc
*default_func
[3] = {
288 default_ioport_writeb
,
289 default_ioport_writew
,
290 default_ioport_writel
292 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
294 func
= default_func
[index
];
295 func(ioport_opaque
[address
], address
, data
);
298 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
300 #ifdef DEBUG_UNUSED_IOPORT
301 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
306 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
308 #ifdef DEBUG_UNUSED_IOPORT
309 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
313 /* default is to make two byte accesses */
314 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
317 data
= ioport_read(0, address
);
318 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
319 data
|= ioport_read(0, address
) << 8;
323 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
325 ioport_write(0, address
, data
& 0xff);
326 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
327 ioport_write(0, address
, (data
>> 8) & 0xff);
330 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
332 #ifdef DEBUG_UNUSED_IOPORT
333 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
338 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
340 #ifdef DEBUG_UNUSED_IOPORT
341 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
345 /* size is the word size in byte */
346 int register_ioport_read(int start
, int length
, int size
,
347 IOPortReadFunc
*func
, void *opaque
)
353 } else if (size
== 2) {
355 } else if (size
== 4) {
358 hw_error("register_ioport_read: invalid size");
361 for(i
= start
; i
< start
+ length
; i
+= size
) {
362 ioport_read_table
[bsize
][i
] = func
;
363 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
364 hw_error("register_ioport_read: invalid opaque");
365 ioport_opaque
[i
] = opaque
;
370 /* size is the word size in byte */
371 int register_ioport_write(int start
, int length
, int size
,
372 IOPortWriteFunc
*func
, void *opaque
)
378 } else if (size
== 2) {
380 } else if (size
== 4) {
383 hw_error("register_ioport_write: invalid size");
386 for(i
= start
; i
< start
+ length
; i
+= size
) {
387 ioport_write_table
[bsize
][i
] = func
;
388 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
389 hw_error("register_ioport_write: invalid opaque");
390 ioport_opaque
[i
] = opaque
;
395 void isa_unassign_ioport(int start
, int length
)
399 for(i
= start
; i
< start
+ length
; i
++) {
400 ioport_read_table
[0][i
] = default_ioport_readb
;
401 ioport_read_table
[1][i
] = default_ioport_readw
;
402 ioport_read_table
[2][i
] = default_ioport_readl
;
404 ioport_write_table
[0][i
] = default_ioport_writeb
;
405 ioport_write_table
[1][i
] = default_ioport_writew
;
406 ioport_write_table
[2][i
] = default_ioport_writel
;
410 /***********************************************************/
412 void cpu_outb(CPUState
*env
, int addr
, int val
)
414 LOG_IOPORT("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
)
424 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
425 ioport_write(1, addr
, val
);
428 env
->last_io_time
= cpu_get_time_fast();
432 void cpu_outl(CPUState
*env
, int addr
, int val
)
434 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
435 ioport_write(2, addr
, val
);
438 env
->last_io_time
= cpu_get_time_fast();
442 int cpu_inb(CPUState
*env
, int addr
)
445 val
= ioport_read(0, addr
);
446 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
449 env
->last_io_time
= cpu_get_time_fast();
454 int cpu_inw(CPUState
*env
, int addr
)
457 val
= ioport_read(1, addr
);
458 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
461 env
->last_io_time
= cpu_get_time_fast();
466 int cpu_inl(CPUState
*env
, int addr
)
469 val
= ioport_read(2, addr
);
470 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
473 env
->last_io_time
= cpu_get_time_fast();
478 /***********************************************************/
479 void hw_error(const char *fmt
, ...)
485 fprintf(stderr
, "qemu: hardware error: ");
486 vfprintf(stderr
, fmt
, ap
);
487 fprintf(stderr
, "\n");
488 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
489 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
491 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
493 cpu_dump_state(env
, stderr
, fprintf
, 0);
503 static QEMUBalloonEvent
*qemu_balloon_event
;
504 void *qemu_balloon_event_opaque
;
506 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
508 qemu_balloon_event
= func
;
509 qemu_balloon_event_opaque
= opaque
;
512 void qemu_balloon(ram_addr_t target
)
514 if (qemu_balloon_event
)
515 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
518 ram_addr_t
qemu_balloon_status(void)
520 if (qemu_balloon_event
)
521 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
525 /***********************************************************/
528 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
529 static void *qemu_put_kbd_event_opaque
;
530 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
531 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
533 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
535 qemu_put_kbd_event_opaque
= opaque
;
536 qemu_put_kbd_event
= func
;
539 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
540 void *opaque
, int absolute
,
543 QEMUPutMouseEntry
*s
, *cursor
;
545 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
547 s
->qemu_put_mouse_event
= func
;
548 s
->qemu_put_mouse_event_opaque
= opaque
;
549 s
->qemu_put_mouse_event_absolute
= absolute
;
550 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
553 if (!qemu_put_mouse_event_head
) {
554 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
558 cursor
= qemu_put_mouse_event_head
;
559 while (cursor
->next
!= NULL
)
560 cursor
= cursor
->next
;
563 qemu_put_mouse_event_current
= s
;
568 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
570 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
572 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
575 cursor
= qemu_put_mouse_event_head
;
576 while (cursor
!= NULL
&& cursor
!= entry
) {
578 cursor
= cursor
->next
;
581 if (cursor
== NULL
) // does not exist or list empty
583 else if (prev
== NULL
) { // entry is head
584 qemu_put_mouse_event_head
= cursor
->next
;
585 if (qemu_put_mouse_event_current
== entry
)
586 qemu_put_mouse_event_current
= cursor
->next
;
587 qemu_free(entry
->qemu_put_mouse_event_name
);
592 prev
->next
= entry
->next
;
594 if (qemu_put_mouse_event_current
== entry
)
595 qemu_put_mouse_event_current
= prev
;
597 qemu_free(entry
->qemu_put_mouse_event_name
);
601 void kbd_put_keycode(int keycode
)
603 if (qemu_put_kbd_event
) {
604 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
608 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
610 QEMUPutMouseEvent
*mouse_event
;
611 void *mouse_event_opaque
;
614 if (!qemu_put_mouse_event_current
) {
619 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
621 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
624 if (graphic_rotate
) {
625 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
628 width
= graphic_width
- 1;
629 mouse_event(mouse_event_opaque
,
630 width
- dy
, dx
, dz
, buttons_state
);
632 mouse_event(mouse_event_opaque
,
633 dx
, dy
, dz
, buttons_state
);
637 int kbd_mouse_is_absolute(void)
639 if (!qemu_put_mouse_event_current
)
642 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
645 void do_info_mice(void)
647 QEMUPutMouseEntry
*cursor
;
650 if (!qemu_put_mouse_event_head
) {
651 term_printf("No mouse devices connected\n");
655 term_printf("Mouse devices available:\n");
656 cursor
= qemu_put_mouse_event_head
;
657 while (cursor
!= NULL
) {
658 term_printf("%c Mouse #%d: %s\n",
659 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
660 index
, cursor
->qemu_put_mouse_event_name
);
662 cursor
= cursor
->next
;
666 void do_mouse_set(int index
)
668 QEMUPutMouseEntry
*cursor
;
671 if (!qemu_put_mouse_event_head
) {
672 term_printf("No mouse devices connected\n");
676 cursor
= qemu_put_mouse_event_head
;
677 while (cursor
!= NULL
&& index
!= i
) {
679 cursor
= cursor
->next
;
683 qemu_put_mouse_event_current
= cursor
;
685 term_printf("Mouse at given index not found\n");
688 /* compute with 96 bit intermediate result: (a*b)/c */
689 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
694 #ifdef WORDS_BIGENDIAN
704 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
705 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
708 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
712 /***********************************************************/
713 /* real time host monotonic timer */
715 #define QEMU_TIMER_BASE 1000000000LL
719 static int64_t clock_freq
;
721 static void init_get_clock(void)
725 ret
= QueryPerformanceFrequency(&freq
);
727 fprintf(stderr
, "Could not calibrate ticks\n");
730 clock_freq
= freq
.QuadPart
;
733 static int64_t get_clock(void)
736 QueryPerformanceCounter(&ti
);
737 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
742 static int use_rt_clock
;
744 static void init_get_clock(void)
747 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
750 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
757 static int64_t get_clock(void)
759 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
762 clock_gettime(CLOCK_MONOTONIC
, &ts
);
763 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
767 /* XXX: using gettimeofday leads to problems if the date
768 changes, so it should be avoided. */
770 gettimeofday(&tv
, NULL
);
771 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
776 /* Return the virtual CPU time, based on the instruction counter. */
777 static int64_t cpu_get_icount(void)
780 CPUState
*env
= cpu_single_env
;;
781 icount
= qemu_icount
;
784 fprintf(stderr
, "Bad clock read\n");
785 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
787 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
790 /***********************************************************/
791 /* guest cycle counter */
793 static int64_t cpu_ticks_prev
;
794 static int64_t cpu_ticks_offset
;
795 static int64_t cpu_clock_offset
;
796 static int cpu_ticks_enabled
;
798 /* return the host CPU cycle counter and handle stop/restart */
799 int64_t cpu_get_ticks(void)
802 return cpu_get_icount();
804 if (!cpu_ticks_enabled
) {
805 return cpu_ticks_offset
;
808 ticks
= cpu_get_real_ticks();
809 if (cpu_ticks_prev
> ticks
) {
810 /* Note: non increasing ticks may happen if the host uses
812 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
814 cpu_ticks_prev
= ticks
;
815 return ticks
+ cpu_ticks_offset
;
819 /* return the host CPU monotonic timer and handle stop/restart */
820 static int64_t cpu_get_clock(void)
823 if (!cpu_ticks_enabled
) {
824 return cpu_clock_offset
;
827 return ti
+ cpu_clock_offset
;
831 /* enable cpu_get_ticks() */
832 void cpu_enable_ticks(void)
834 if (!cpu_ticks_enabled
) {
835 cpu_ticks_offset
-= cpu_get_real_ticks();
836 cpu_clock_offset
-= get_clock();
837 cpu_ticks_enabled
= 1;
841 /* disable cpu_get_ticks() : the clock is stopped. You must not call
842 cpu_get_ticks() after that. */
843 void cpu_disable_ticks(void)
845 if (cpu_ticks_enabled
) {
846 cpu_ticks_offset
= cpu_get_ticks();
847 cpu_clock_offset
= cpu_get_clock();
848 cpu_ticks_enabled
= 0;
852 /***********************************************************/
855 #define QEMU_TIMER_REALTIME 0
856 #define QEMU_TIMER_VIRTUAL 1
860 /* XXX: add frequency */
868 struct QEMUTimer
*next
;
871 struct qemu_alarm_timer
{
875 int (*start
)(struct qemu_alarm_timer
*t
);
876 void (*stop
)(struct qemu_alarm_timer
*t
);
877 void (*rearm
)(struct qemu_alarm_timer
*t
);
881 #define ALARM_FLAG_DYNTICKS 0x1
882 #define ALARM_FLAG_EXPIRED 0x2
884 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
886 return t
->flags
& ALARM_FLAG_DYNTICKS
;
889 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
891 if (!alarm_has_dynticks(t
))
897 /* TODO: MIN_TIMER_REARM_US should be optimized */
898 #define MIN_TIMER_REARM_US 250
900 static struct qemu_alarm_timer
*alarm_timer
;
902 static int alarm_timer_rfd
, alarm_timer_wfd
;
907 struct qemu_alarm_win32
{
911 } alarm_win32_data
= {0, NULL
, -1};
913 static int win32_start_timer(struct qemu_alarm_timer
*t
);
914 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
915 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
919 static int unix_start_timer(struct qemu_alarm_timer
*t
);
920 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
924 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
925 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
926 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
928 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
929 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
931 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
932 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
934 #endif /* __linux__ */
938 /* Correlation between real and virtual time is always going to be
939 fairly approximate, so ignore small variation.
940 When the guest is idle real and virtual time will be aligned in
942 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
944 static void icount_adjust(void)
949 static int64_t last_delta
;
950 /* If the VM is not running, then do nothing. */
954 cur_time
= cpu_get_clock();
955 cur_icount
= qemu_get_clock(vm_clock
);
956 delta
= cur_icount
- cur_time
;
957 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
959 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
960 && icount_time_shift
> 0) {
961 /* The guest is getting too far ahead. Slow time down. */
965 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
966 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
967 /* The guest is getting too far behind. Speed time up. */
971 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
974 static void icount_adjust_rt(void * opaque
)
976 qemu_mod_timer(icount_rt_timer
,
977 qemu_get_clock(rt_clock
) + 1000);
981 static void icount_adjust_vm(void * opaque
)
983 qemu_mod_timer(icount_vm_timer
,
984 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
988 static void init_icount_adjust(void)
990 /* Have both realtime and virtual time triggers for speed adjustment.
991 The realtime trigger catches emulated time passing too slowly,
992 the virtual time trigger catches emulated time passing too fast.
993 Realtime triggers occur even when idle, so use them less frequently
995 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
996 qemu_mod_timer(icount_rt_timer
,
997 qemu_get_clock(rt_clock
) + 1000);
998 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
999 qemu_mod_timer(icount_vm_timer
,
1000 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1003 static struct qemu_alarm_timer alarm_timers
[] = {
1006 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1007 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1008 /* HPET - if available - is preferred */
1009 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1010 /* ...otherwise try RTC */
1011 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1013 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1015 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1016 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1017 {"win32", 0, win32_start_timer
,
1018 win32_stop_timer
, NULL
, &alarm_win32_data
},
1023 static void show_available_alarms(void)
1027 printf("Available alarm timers, in order of precedence:\n");
1028 for (i
= 0; alarm_timers
[i
].name
; i
++)
1029 printf("%s\n", alarm_timers
[i
].name
);
1032 static void configure_alarms(char const *opt
)
1036 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1039 struct qemu_alarm_timer tmp
;
1041 if (!strcmp(opt
, "?")) {
1042 show_available_alarms();
1048 /* Reorder the array */
1049 name
= strtok(arg
, ",");
1051 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1052 if (!strcmp(alarm_timers
[i
].name
, name
))
1057 fprintf(stderr
, "Unknown clock %s\n", name
);
1066 tmp
= alarm_timers
[i
];
1067 alarm_timers
[i
] = alarm_timers
[cur
];
1068 alarm_timers
[cur
] = tmp
;
1072 name
= strtok(NULL
, ",");
1078 /* Disable remaining timers */
1079 for (i
= cur
; i
< count
; i
++)
1080 alarm_timers
[i
].name
= NULL
;
1082 show_available_alarms();
1087 QEMUClock
*rt_clock
;
1088 QEMUClock
*vm_clock
;
1090 static QEMUTimer
*active_timers
[2];
1092 static QEMUClock
*qemu_new_clock(int type
)
1095 clock
= qemu_mallocz(sizeof(QEMUClock
));
1100 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1104 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1107 ts
->opaque
= opaque
;
1111 void qemu_free_timer(QEMUTimer
*ts
)
1116 /* stop a timer, but do not dealloc it */
1117 void qemu_del_timer(QEMUTimer
*ts
)
1121 /* NOTE: this code must be signal safe because
1122 qemu_timer_expired() can be called from a signal. */
1123 pt
= &active_timers
[ts
->clock
->type
];
1136 /* modify the current timer so that it will be fired when current_time
1137 >= expire_time. The corresponding callback will be called. */
1138 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1144 /* add the timer in the sorted list */
1145 /* NOTE: this code must be signal safe because
1146 qemu_timer_expired() can be called from a signal. */
1147 pt
= &active_timers
[ts
->clock
->type
];
1152 if (t
->expire_time
> expire_time
)
1156 ts
->expire_time
= expire_time
;
1160 /* Rearm if necessary */
1161 if (pt
== &active_timers
[ts
->clock
->type
]) {
1162 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1163 qemu_rearm_alarm_timer(alarm_timer
);
1165 /* Interrupt execution to force deadline recalculation. */
1166 if (use_icount
&& cpu_single_env
) {
1167 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1172 int qemu_timer_pending(QEMUTimer
*ts
)
1175 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1182 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1186 return (timer_head
->expire_time
<= current_time
);
1189 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1195 if (!ts
|| ts
->expire_time
> current_time
)
1197 /* remove timer from the list before calling the callback */
1198 *ptimer_head
= ts
->next
;
1201 /* run the callback (the timer list can be modified) */
1206 int64_t qemu_get_clock(QEMUClock
*clock
)
1208 switch(clock
->type
) {
1209 case QEMU_TIMER_REALTIME
:
1210 return get_clock() / 1000000;
1212 case QEMU_TIMER_VIRTUAL
:
1214 return cpu_get_icount();
1216 return cpu_get_clock();
1221 static void init_timers(void)
1224 ticks_per_sec
= QEMU_TIMER_BASE
;
1225 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1226 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1230 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1232 uint64_t expire_time
;
1234 if (qemu_timer_pending(ts
)) {
1235 expire_time
= ts
->expire_time
;
1239 qemu_put_be64(f
, expire_time
);
1242 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1244 uint64_t expire_time
;
1246 expire_time
= qemu_get_be64(f
);
1247 if (expire_time
!= -1) {
1248 qemu_mod_timer(ts
, expire_time
);
1254 static void timer_save(QEMUFile
*f
, void *opaque
)
1256 if (cpu_ticks_enabled
) {
1257 hw_error("cannot save state if virtual timers are running");
1259 qemu_put_be64(f
, cpu_ticks_offset
);
1260 qemu_put_be64(f
, ticks_per_sec
);
1261 qemu_put_be64(f
, cpu_clock_offset
);
1264 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1266 if (version_id
!= 1 && version_id
!= 2)
1268 if (cpu_ticks_enabled
) {
1271 cpu_ticks_offset
=qemu_get_be64(f
);
1272 ticks_per_sec
=qemu_get_be64(f
);
1273 if (version_id
== 2) {
1274 cpu_clock_offset
=qemu_get_be64(f
);
1280 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1281 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1283 static void host_alarm_handler(int host_signum
)
1287 #define DISP_FREQ 1000
1289 static int64_t delta_min
= INT64_MAX
;
1290 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1292 ti
= qemu_get_clock(vm_clock
);
1293 if (last_clock
!= 0) {
1294 delta
= ti
- last_clock
;
1295 if (delta
< delta_min
)
1297 if (delta
> delta_max
)
1300 if (++count
== DISP_FREQ
) {
1301 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1302 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1303 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1304 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1305 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1307 delta_min
= INT64_MAX
;
1315 if (alarm_has_dynticks(alarm_timer
) ||
1317 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1318 qemu_get_clock(vm_clock
))) ||
1319 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1320 qemu_get_clock(rt_clock
))) {
1321 CPUState
*env
= next_cpu
;
1324 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1325 SetEvent(data
->host_alarm
);
1327 static const char byte
= 0;
1328 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1330 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1333 /* stop the currently executing cpu because a timer occured */
1334 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1336 if (env
->kqemu_enabled
) {
1337 kqemu_cpu_interrupt(env
);
1345 static int64_t qemu_next_deadline(void)
1349 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1350 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1351 qemu_get_clock(vm_clock
);
1353 /* To avoid problems with overflow limit this to 2^32. */
1363 #if defined(__linux__) || defined(_WIN32)
1364 static uint64_t qemu_next_deadline_dyntick(void)
1372 delta
= (qemu_next_deadline() + 999) / 1000;
1374 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1375 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1376 qemu_get_clock(rt_clock
))*1000;
1377 if (rtdelta
< delta
)
1381 if (delta
< MIN_TIMER_REARM_US
)
1382 delta
= MIN_TIMER_REARM_US
;
1390 /* Sets a specific flag */
1391 static int fcntl_setfl(int fd
, int flag
)
1395 flags
= fcntl(fd
, F_GETFL
);
1399 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1405 #if defined(__linux__)
1407 #define RTC_FREQ 1024
1409 static void enable_sigio_timer(int fd
)
1411 struct sigaction act
;
1414 sigfillset(&act
.sa_mask
);
1416 act
.sa_handler
= host_alarm_handler
;
1418 sigaction(SIGIO
, &act
, NULL
);
1419 fcntl_setfl(fd
, O_ASYNC
);
1420 fcntl(fd
, F_SETOWN
, getpid());
1423 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1425 struct hpet_info info
;
1428 fd
= open("/dev/hpet", O_RDONLY
);
1433 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1435 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1436 "error, but for better emulation accuracy type:\n"
1437 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1441 /* Check capabilities */
1442 r
= ioctl(fd
, HPET_INFO
, &info
);
1446 /* Enable periodic mode */
1447 r
= ioctl(fd
, HPET_EPI
, 0);
1448 if (info
.hi_flags
&& (r
< 0))
1451 /* Enable interrupt */
1452 r
= ioctl(fd
, HPET_IE_ON
, 0);
1456 enable_sigio_timer(fd
);
1457 t
->priv
= (void *)(long)fd
;
1465 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1467 int fd
= (long)t
->priv
;
1472 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1475 unsigned long current_rtc_freq
= 0;
1477 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1480 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1481 if (current_rtc_freq
!= RTC_FREQ
&&
1482 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1483 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1484 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1485 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1488 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1494 enable_sigio_timer(rtc_fd
);
1496 t
->priv
= (void *)(long)rtc_fd
;
1501 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1503 int rtc_fd
= (long)t
->priv
;
1508 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1512 struct sigaction act
;
1514 sigfillset(&act
.sa_mask
);
1516 act
.sa_handler
= host_alarm_handler
;
1518 sigaction(SIGALRM
, &act
, NULL
);
1520 ev
.sigev_value
.sival_int
= 0;
1521 ev
.sigev_notify
= SIGEV_SIGNAL
;
1522 ev
.sigev_signo
= SIGALRM
;
1524 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1525 perror("timer_create");
1527 /* disable dynticks */
1528 fprintf(stderr
, "Dynamic Ticks disabled\n");
1533 t
->priv
= (void *)(long)host_timer
;
1538 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1540 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1542 timer_delete(host_timer
);
1545 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1547 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1548 struct itimerspec timeout
;
1549 int64_t nearest_delta_us
= INT64_MAX
;
1552 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1553 !active_timers
[QEMU_TIMER_VIRTUAL
])
1556 nearest_delta_us
= qemu_next_deadline_dyntick();
1558 /* check whether a timer is already running */
1559 if (timer_gettime(host_timer
, &timeout
)) {
1561 fprintf(stderr
, "Internal timer error: aborting\n");
1564 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1565 if (current_us
&& current_us
<= nearest_delta_us
)
1568 timeout
.it_interval
.tv_sec
= 0;
1569 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1570 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1571 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1572 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1574 fprintf(stderr
, "Internal timer error: aborting\n");
1579 #endif /* defined(__linux__) */
1581 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1583 struct sigaction act
;
1584 struct itimerval itv
;
1588 sigfillset(&act
.sa_mask
);
1590 act
.sa_handler
= host_alarm_handler
;
1592 sigaction(SIGALRM
, &act
, NULL
);
1594 itv
.it_interval
.tv_sec
= 0;
1595 /* for i386 kernel 2.6 to get 1 ms */
1596 itv
.it_interval
.tv_usec
= 999;
1597 itv
.it_value
.tv_sec
= 0;
1598 itv
.it_value
.tv_usec
= 10 * 1000;
1600 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1607 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1609 struct itimerval itv
;
1611 memset(&itv
, 0, sizeof(itv
));
1612 setitimer(ITIMER_REAL
, &itv
, NULL
);
1615 #endif /* !defined(_WIN32) */
1617 static void try_to_rearm_timer(void *opaque
)
1619 struct qemu_alarm_timer
*t
= opaque
;
1623 /* Drain the notify pipe */
1626 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1627 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1630 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1631 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1632 qemu_rearm_alarm_timer(alarm_timer
);
1638 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1641 struct qemu_alarm_win32
*data
= t
->priv
;
1644 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1645 if (!data
->host_alarm
) {
1646 perror("Failed CreateEvent");
1650 memset(&tc
, 0, sizeof(tc
));
1651 timeGetDevCaps(&tc
, sizeof(tc
));
1653 if (data
->period
< tc
.wPeriodMin
)
1654 data
->period
= tc
.wPeriodMin
;
1656 timeBeginPeriod(data
->period
);
1658 flags
= TIME_CALLBACK_FUNCTION
;
1659 if (alarm_has_dynticks(t
))
1660 flags
|= TIME_ONESHOT
;
1662 flags
|= TIME_PERIODIC
;
1664 data
->timerId
= timeSetEvent(1, // interval (ms)
1665 data
->period
, // resolution
1666 host_alarm_handler
, // function
1667 (DWORD
)t
, // parameter
1670 if (!data
->timerId
) {
1671 perror("Failed to initialize win32 alarm timer");
1673 timeEndPeriod(data
->period
);
1674 CloseHandle(data
->host_alarm
);
1678 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1683 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1685 struct qemu_alarm_win32
*data
= t
->priv
;
1687 timeKillEvent(data
->timerId
);
1688 timeEndPeriod(data
->period
);
1690 CloseHandle(data
->host_alarm
);
1693 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1695 struct qemu_alarm_win32
*data
= t
->priv
;
1696 uint64_t nearest_delta_us
;
1698 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1699 !active_timers
[QEMU_TIMER_VIRTUAL
])
1702 nearest_delta_us
= qemu_next_deadline_dyntick();
1703 nearest_delta_us
/= 1000;
1705 timeKillEvent(data
->timerId
);
1707 data
->timerId
= timeSetEvent(1,
1711 TIME_ONESHOT
| TIME_PERIODIC
);
1713 if (!data
->timerId
) {
1714 perror("Failed to re-arm win32 alarm timer");
1716 timeEndPeriod(data
->period
);
1717 CloseHandle(data
->host_alarm
);
1724 static int init_timer_alarm(void)
1726 struct qemu_alarm_timer
*t
= NULL
;
1736 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1740 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1744 alarm_timer_rfd
= fds
[0];
1745 alarm_timer_wfd
= fds
[1];
1748 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1749 t
= &alarm_timers
[i
];
1762 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1763 try_to_rearm_timer
, NULL
, t
);
1778 static void quit_timers(void)
1780 alarm_timer
->stop(alarm_timer
);
1784 /***********************************************************/
1785 /* host time/date access */
1786 void qemu_get_timedate(struct tm
*tm
, int offset
)
1793 if (rtc_date_offset
== -1) {
1797 ret
= localtime(&ti
);
1799 ti
-= rtc_date_offset
;
1803 memcpy(tm
, ret
, sizeof(struct tm
));
1806 int qemu_timedate_diff(struct tm
*tm
)
1810 if (rtc_date_offset
== -1)
1812 seconds
= mktimegm(tm
);
1814 seconds
= mktime(tm
);
1816 seconds
= mktimegm(tm
) + rtc_date_offset
;
1818 return seconds
- time(NULL
);
1822 static void socket_cleanup(void)
1827 static int socket_init(void)
1832 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1834 err
= WSAGetLastError();
1835 fprintf(stderr
, "WSAStartup: %d\n", err
);
1838 atexit(socket_cleanup
);
1843 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1848 while (*p
!= '\0' && *p
!= '=') {
1849 if (q
&& (q
- buf
) < buf_size
- 1)
1859 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1864 while (*p
!= '\0') {
1866 if (*(p
+ 1) != ',')
1870 if (q
&& (q
- buf
) < buf_size
- 1)
1880 int get_param_value(char *buf
, int buf_size
,
1881 const char *tag
, const char *str
)
1888 p
= get_opt_name(option
, sizeof(option
), p
);
1892 if (!strcmp(tag
, option
)) {
1893 (void)get_opt_value(buf
, buf_size
, p
);
1896 p
= get_opt_value(NULL
, 0, p
);
1905 int check_params(char *buf
, int buf_size
,
1906 const char * const *params
, const char *str
)
1913 p
= get_opt_name(buf
, buf_size
, p
);
1917 for(i
= 0; params
[i
] != NULL
; i
++)
1918 if (!strcmp(params
[i
], buf
))
1920 if (params
[i
] == NULL
)
1922 p
= get_opt_value(NULL
, 0, p
);
1930 /***********************************************************/
1931 /* Bluetooth support */
1934 static struct HCIInfo
*hci_table
[MAX_NICS
];
1936 static struct bt_vlan_s
{
1937 struct bt_scatternet_s net
;
1939 struct bt_vlan_s
*next
;
1942 /* find or alloc a new bluetooth "VLAN" */
1943 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1945 struct bt_vlan_s
**pvlan
, *vlan
;
1946 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1950 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1952 pvlan
= &first_bt_vlan
;
1953 while (*pvlan
!= NULL
)
1954 pvlan
= &(*pvlan
)->next
;
1959 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1963 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1968 static struct HCIInfo null_hci
= {
1969 .cmd_send
= null_hci_send
,
1970 .sco_send
= null_hci_send
,
1971 .acl_send
= null_hci_send
,
1972 .bdaddr_set
= null_hci_addr_set
,
1975 struct HCIInfo
*qemu_next_hci(void)
1977 if (cur_hci
== nb_hcis
)
1980 return hci_table
[cur_hci
++];
1983 static struct HCIInfo
*hci_init(const char *str
)
1986 struct bt_scatternet_s
*vlan
= 0;
1988 if (!strcmp(str
, "null"))
1991 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1993 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1994 else if (!strncmp(str
, "hci", 3)) {
1997 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1998 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2003 vlan
= qemu_find_bt_vlan(0);
2005 return bt_new_hci(vlan
);
2008 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2013 static int bt_hci_parse(const char *str
)
2015 struct HCIInfo
*hci
;
2018 if (nb_hcis
>= MAX_NICS
) {
2019 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2023 hci
= hci_init(str
);
2032 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2033 hci
->bdaddr_set(hci
, bdaddr
.b
);
2035 hci_table
[nb_hcis
++] = hci
;
2040 static void bt_vhci_add(int vlan_id
)
2042 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2045 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2046 "an empty scatternet %i\n", vlan_id
);
2048 bt_vhci_init(bt_new_hci(vlan
));
2051 static struct bt_device_s
*bt_device_add(const char *opt
)
2053 struct bt_scatternet_s
*vlan
;
2055 char *endp
= strstr(opt
, ",vlan=");
2056 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2059 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2062 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2064 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2069 vlan
= qemu_find_bt_vlan(vlan_id
);
2072 fprintf(stderr
, "qemu: warning: adding a slave device to "
2073 "an empty scatternet %i\n", vlan_id
);
2075 if (!strcmp(devname
, "keyboard"))
2076 return bt_keyboard_init(vlan
);
2078 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2082 static int bt_parse(const char *opt
)
2084 const char *endp
, *p
;
2087 if (strstart(opt
, "hci", &endp
)) {
2088 if (!*endp
|| *endp
== ',') {
2090 if (!strstart(endp
, ",vlan=", 0))
2093 return bt_hci_parse(opt
);
2095 } else if (strstart(opt
, "vhci", &endp
)) {
2096 if (!*endp
|| *endp
== ',') {
2098 if (strstart(endp
, ",vlan=", &p
)) {
2099 vlan
= strtol(p
, (char **) &endp
, 0);
2101 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2105 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2114 } else if (strstart(opt
, "device:", &endp
))
2115 return !bt_device_add(endp
);
2117 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2121 /***********************************************************/
2122 /* QEMU Block devices */
2124 #define HD_ALIAS "index=%d,media=disk"
2126 #define CDROM_ALIAS "index=1,media=cdrom"
2128 #define CDROM_ALIAS "index=2,media=cdrom"
2130 #define FD_ALIAS "index=%d,if=floppy"
2131 #define PFLASH_ALIAS "if=pflash"
2132 #define MTD_ALIAS "if=mtd"
2133 #define SD_ALIAS "index=0,if=sd"
2135 static int drive_opt_get_free_idx(void)
2139 for (index
= 0; index
< MAX_DRIVES
; index
++)
2140 if (!drives_opt
[index
].used
) {
2141 drives_opt
[index
].used
= 1;
2148 static int drive_get_free_idx(void)
2152 for (index
= 0; index
< MAX_DRIVES
; index
++)
2153 if (!drives_table
[index
].used
) {
2154 drives_table
[index
].used
= 1;
2161 int drive_add(const char *file
, const char *fmt
, ...)
2164 int index
= drive_opt_get_free_idx();
2166 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2167 fprintf(stderr
, "qemu: too many drives\n");
2171 drives_opt
[index
].file
= file
;
2173 vsnprintf(drives_opt
[index
].opt
,
2174 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2181 void drive_remove(int index
)
2183 drives_opt
[index
].used
= 0;
2187 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2191 /* seek interface, bus and unit */
2193 for (index
= 0; index
< MAX_DRIVES
; index
++)
2194 if (drives_table
[index
].type
== type
&&
2195 drives_table
[index
].bus
== bus
&&
2196 drives_table
[index
].unit
== unit
&&
2197 drives_table
[index
].used
)
2203 int drive_get_max_bus(BlockInterfaceType type
)
2209 for (index
= 0; index
< nb_drives
; index
++) {
2210 if(drives_table
[index
].type
== type
&&
2211 drives_table
[index
].bus
> max_bus
)
2212 max_bus
= drives_table
[index
].bus
;
2217 const char *drive_get_serial(BlockDriverState
*bdrv
)
2221 for (index
= 0; index
< nb_drives
; index
++)
2222 if (drives_table
[index
].bdrv
== bdrv
)
2223 return drives_table
[index
].serial
;
2228 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2232 for (index
= 0; index
< nb_drives
; index
++)
2233 if (drives_table
[index
].bdrv
== bdrv
)
2234 return drives_table
[index
].onerror
;
2236 return BLOCK_ERR_REPORT
;
2239 static void bdrv_format_print(void *opaque
, const char *name
)
2241 fprintf(stderr
, " %s", name
);
2244 void drive_uninit(BlockDriverState
*bdrv
)
2248 for (i
= 0; i
< MAX_DRIVES
; i
++)
2249 if (drives_table
[i
].bdrv
== bdrv
) {
2250 drives_table
[i
].bdrv
= NULL
;
2251 drives_table
[i
].used
= 0;
2252 drive_remove(drives_table
[i
].drive_opt_idx
);
2258 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2264 const char *mediastr
= "";
2265 BlockInterfaceType type
;
2266 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2267 int bus_id
, unit_id
;
2268 int cyls
, heads
, secs
, translation
;
2269 BlockDriverState
*bdrv
;
2270 BlockDriver
*drv
= NULL
;
2271 QEMUMachine
*machine
= opaque
;
2275 int bdrv_flags
, onerror
;
2276 int drives_table_idx
;
2277 char *str
= arg
->opt
;
2278 static const char * const params
[] = { "bus", "unit", "if", "index",
2279 "cyls", "heads", "secs", "trans",
2280 "media", "snapshot", "file",
2281 "cache", "format", "serial", "werror",
2284 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2285 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2291 cyls
= heads
= secs
= 0;
2294 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2298 if (machine
->use_scsi
) {
2300 max_devs
= MAX_SCSI_DEVS
;
2301 pstrcpy(devname
, sizeof(devname
), "scsi");
2304 max_devs
= MAX_IDE_DEVS
;
2305 pstrcpy(devname
, sizeof(devname
), "ide");
2309 /* extract parameters */
2311 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2312 bus_id
= strtol(buf
, NULL
, 0);
2314 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2319 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2320 unit_id
= strtol(buf
, NULL
, 0);
2322 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2327 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2328 pstrcpy(devname
, sizeof(devname
), buf
);
2329 if (!strcmp(buf
, "ide")) {
2331 max_devs
= MAX_IDE_DEVS
;
2332 } else if (!strcmp(buf
, "scsi")) {
2334 max_devs
= MAX_SCSI_DEVS
;
2335 } else if (!strcmp(buf
, "floppy")) {
2338 } else if (!strcmp(buf
, "pflash")) {
2341 } else if (!strcmp(buf
, "mtd")) {
2344 } else if (!strcmp(buf
, "sd")) {
2347 } else if (!strcmp(buf
, "virtio")) {
2351 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2356 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2357 index
= strtol(buf
, NULL
, 0);
2359 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2364 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2365 cyls
= strtol(buf
, NULL
, 0);
2368 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2369 heads
= strtol(buf
, NULL
, 0);
2372 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2373 secs
= strtol(buf
, NULL
, 0);
2376 if (cyls
|| heads
|| secs
) {
2377 if (cyls
< 1 || cyls
> 16383) {
2378 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2381 if (heads
< 1 || heads
> 16) {
2382 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2385 if (secs
< 1 || secs
> 63) {
2386 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2391 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2394 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2398 if (!strcmp(buf
, "none"))
2399 translation
= BIOS_ATA_TRANSLATION_NONE
;
2400 else if (!strcmp(buf
, "lba"))
2401 translation
= BIOS_ATA_TRANSLATION_LBA
;
2402 else if (!strcmp(buf
, "auto"))
2403 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2405 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2410 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2411 if (!strcmp(buf
, "disk")) {
2413 } else if (!strcmp(buf
, "cdrom")) {
2414 if (cyls
|| secs
|| heads
) {
2416 "qemu: '%s' invalid physical CHS format\n", str
);
2419 media
= MEDIA_CDROM
;
2421 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2426 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2427 if (!strcmp(buf
, "on"))
2429 else if (!strcmp(buf
, "off"))
2432 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2437 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2438 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2440 else if (!strcmp(buf
, "writethrough"))
2442 else if (!strcmp(buf
, "writeback"))
2445 fprintf(stderr
, "qemu: invalid cache option\n");
2450 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2451 if (strcmp(buf
, "?") == 0) {
2452 fprintf(stderr
, "qemu: Supported formats:");
2453 bdrv_iterate_format(bdrv_format_print
, NULL
);
2454 fprintf(stderr
, "\n");
2457 drv
= bdrv_find_format(buf
);
2459 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2464 if (arg
->file
== NULL
)
2465 get_param_value(file
, sizeof(file
), "file", str
);
2467 pstrcpy(file
, sizeof(file
), arg
->file
);
2469 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2470 memset(serial
, 0, sizeof(serial
));
2472 onerror
= BLOCK_ERR_REPORT
;
2473 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2474 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2475 fprintf(stderr
, "werror is no supported by this format\n");
2478 if (!strcmp(buf
, "ignore"))
2479 onerror
= BLOCK_ERR_IGNORE
;
2480 else if (!strcmp(buf
, "enospc"))
2481 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2482 else if (!strcmp(buf
, "stop"))
2483 onerror
= BLOCK_ERR_STOP_ANY
;
2484 else if (!strcmp(buf
, "report"))
2485 onerror
= BLOCK_ERR_REPORT
;
2487 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2492 /* compute bus and unit according index */
2495 if (bus_id
!= 0 || unit_id
!= -1) {
2497 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2505 unit_id
= index
% max_devs
;
2506 bus_id
= index
/ max_devs
;
2510 /* if user doesn't specify a unit_id,
2511 * try to find the first free
2514 if (unit_id
== -1) {
2516 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2518 if (max_devs
&& unit_id
>= max_devs
) {
2519 unit_id
-= max_devs
;
2527 if (max_devs
&& unit_id
>= max_devs
) {
2528 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2529 str
, unit_id
, max_devs
- 1);
2534 * ignore multiple definitions
2537 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2542 if (type
== IF_IDE
|| type
== IF_SCSI
)
2543 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2545 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2546 devname
, bus_id
, mediastr
, unit_id
);
2548 snprintf(buf
, sizeof(buf
), "%s%s%i",
2549 devname
, mediastr
, unit_id
);
2550 bdrv
= bdrv_new(buf
);
2551 drives_table_idx
= drive_get_free_idx();
2552 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2553 drives_table
[drives_table_idx
].type
= type
;
2554 drives_table
[drives_table_idx
].bus
= bus_id
;
2555 drives_table
[drives_table_idx
].unit
= unit_id
;
2556 drives_table
[drives_table_idx
].onerror
= onerror
;
2557 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2558 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2567 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2568 bdrv_set_translation_hint(bdrv
, translation
);
2572 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2577 /* FIXME: This isn't really a floppy, but it's a reasonable
2580 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2591 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2592 cache
= 2; /* always use write-back with snapshot */
2594 if (cache
== 0) /* no caching */
2595 bdrv_flags
|= BDRV_O_NOCACHE
;
2596 else if (cache
== 2) /* write-back */
2597 bdrv_flags
|= BDRV_O_CACHE_WB
;
2598 else if (cache
== 3) /* not specified */
2599 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2600 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2601 fprintf(stderr
, "qemu: could not open disk image %s\n",
2605 return drives_table_idx
;
2608 /***********************************************************/
2611 static USBPort
*used_usb_ports
;
2612 static USBPort
*free_usb_ports
;
2614 /* ??? Maybe change this to register a hub to keep track of the topology. */
2615 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2616 usb_attachfn attach
)
2618 port
->opaque
= opaque
;
2619 port
->index
= index
;
2620 port
->attach
= attach
;
2621 port
->next
= free_usb_ports
;
2622 free_usb_ports
= port
;
2625 int usb_device_add_dev(USBDevice
*dev
)
2629 /* Find a USB port to add the device to. */
2630 port
= free_usb_ports
;
2634 /* Create a new hub and chain it on. */
2635 free_usb_ports
= NULL
;
2636 port
->next
= used_usb_ports
;
2637 used_usb_ports
= port
;
2639 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2640 usb_attach(port
, hub
);
2641 port
= free_usb_ports
;
2644 free_usb_ports
= port
->next
;
2645 port
->next
= used_usb_ports
;
2646 used_usb_ports
= port
;
2647 usb_attach(port
, dev
);
2651 static int usb_device_add(const char *devname
)
2656 if (!free_usb_ports
)
2659 if (strstart(devname
, "host:", &p
)) {
2660 dev
= usb_host_device_open(p
);
2661 } else if (!strcmp(devname
, "mouse")) {
2662 dev
= usb_mouse_init();
2663 } else if (!strcmp(devname
, "tablet")) {
2664 dev
= usb_tablet_init();
2665 } else if (!strcmp(devname
, "keyboard")) {
2666 dev
= usb_keyboard_init();
2667 } else if (strstart(devname
, "disk:", &p
)) {
2668 dev
= usb_msd_init(p
);
2669 } else if (!strcmp(devname
, "wacom-tablet")) {
2670 dev
= usb_wacom_init();
2671 } else if (strstart(devname
, "serial:", &p
)) {
2672 dev
= usb_serial_init(p
);
2673 #ifdef CONFIG_BRLAPI
2674 } else if (!strcmp(devname
, "braille")) {
2675 dev
= usb_baum_init();
2677 } else if (strstart(devname
, "net:", &p
)) {
2680 if (net_client_init("nic", p
) < 0)
2682 nd_table
[nic
].model
= "usb";
2683 dev
= usb_net_init(&nd_table
[nic
]);
2684 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2685 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2686 bt_new_hci(qemu_find_bt_vlan(0)));
2693 return usb_device_add_dev(dev
);
2696 int usb_device_del_addr(int bus_num
, int addr
)
2702 if (!used_usb_ports
)
2708 lastp
= &used_usb_ports
;
2709 port
= used_usb_ports
;
2710 while (port
&& port
->dev
->addr
!= addr
) {
2711 lastp
= &port
->next
;
2719 *lastp
= port
->next
;
2720 usb_attach(port
, NULL
);
2721 dev
->handle_destroy(dev
);
2722 port
->next
= free_usb_ports
;
2723 free_usb_ports
= port
;
2727 static int usb_device_del(const char *devname
)
2732 if (strstart(devname
, "host:", &p
))
2733 return usb_host_device_close(p
);
2735 if (!used_usb_ports
)
2738 p
= strchr(devname
, '.');
2741 bus_num
= strtoul(devname
, NULL
, 0);
2742 addr
= strtoul(p
+ 1, NULL
, 0);
2744 return usb_device_del_addr(bus_num
, addr
);
2747 void do_usb_add(const char *devname
)
2749 usb_device_add(devname
);
2752 void do_usb_del(const char *devname
)
2754 usb_device_del(devname
);
2761 const char *speed_str
;
2764 term_printf("USB support not enabled\n");
2768 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2772 switch(dev
->speed
) {
2776 case USB_SPEED_FULL
:
2779 case USB_SPEED_HIGH
:
2786 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2787 0, dev
->addr
, speed_str
, dev
->devname
);
2791 /***********************************************************/
2792 /* PCMCIA/Cardbus */
2794 static struct pcmcia_socket_entry_s
{
2795 struct pcmcia_socket_s
*socket
;
2796 struct pcmcia_socket_entry_s
*next
;
2797 } *pcmcia_sockets
= 0;
2799 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2801 struct pcmcia_socket_entry_s
*entry
;
2803 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2804 entry
->socket
= socket
;
2805 entry
->next
= pcmcia_sockets
;
2806 pcmcia_sockets
= entry
;
2809 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2811 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2813 ptr
= &pcmcia_sockets
;
2814 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2815 if (entry
->socket
== socket
) {
2821 void pcmcia_info(void)
2823 struct pcmcia_socket_entry_s
*iter
;
2824 if (!pcmcia_sockets
)
2825 term_printf("No PCMCIA sockets\n");
2827 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2828 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2829 iter
->socket
->attached
? iter
->socket
->card_string
:
2833 /***********************************************************/
2834 /* register display */
2836 void register_displaystate(DisplayState
*ds
)
2846 DisplayState
*get_displaystate(void)
2848 return display_state
;
2853 static void dumb_display_init(void)
2855 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2856 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2857 register_displaystate(ds
);
2860 /***********************************************************/
2863 #define MAX_IO_HANDLERS 64
2865 typedef struct IOHandlerRecord
{
2867 IOCanRWHandler
*fd_read_poll
;
2869 IOHandler
*fd_write
;
2872 /* temporary data */
2874 struct IOHandlerRecord
*next
;
2877 static IOHandlerRecord
*first_io_handler
;
2879 /* XXX: fd_read_poll should be suppressed, but an API change is
2880 necessary in the character devices to suppress fd_can_read(). */
2881 int qemu_set_fd_handler2(int fd
,
2882 IOCanRWHandler
*fd_read_poll
,
2884 IOHandler
*fd_write
,
2887 IOHandlerRecord
**pioh
, *ioh
;
2889 if (!fd_read
&& !fd_write
) {
2890 pioh
= &first_io_handler
;
2895 if (ioh
->fd
== fd
) {
2902 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2906 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2907 ioh
->next
= first_io_handler
;
2908 first_io_handler
= ioh
;
2911 ioh
->fd_read_poll
= fd_read_poll
;
2912 ioh
->fd_read
= fd_read
;
2913 ioh
->fd_write
= fd_write
;
2914 ioh
->opaque
= opaque
;
2920 int qemu_set_fd_handler(int fd
,
2922 IOHandler
*fd_write
,
2925 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2929 /***********************************************************/
2930 /* Polling handling */
2932 typedef struct PollingEntry
{
2935 struct PollingEntry
*next
;
2938 static PollingEntry
*first_polling_entry
;
2940 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2942 PollingEntry
**ppe
, *pe
;
2943 pe
= qemu_mallocz(sizeof(PollingEntry
));
2945 pe
->opaque
= opaque
;
2946 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2951 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2953 PollingEntry
**ppe
, *pe
;
2954 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2956 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2964 /***********************************************************/
2965 /* Wait objects support */
2966 typedef struct WaitObjects
{
2968 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2969 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2970 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2973 static WaitObjects wait_objects
= {0};
2975 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2977 WaitObjects
*w
= &wait_objects
;
2979 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2981 w
->events
[w
->num
] = handle
;
2982 w
->func
[w
->num
] = func
;
2983 w
->opaque
[w
->num
] = opaque
;
2988 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2991 WaitObjects
*w
= &wait_objects
;
2994 for (i
= 0; i
< w
->num
; i
++) {
2995 if (w
->events
[i
] == handle
)
2998 w
->events
[i
] = w
->events
[i
+ 1];
2999 w
->func
[i
] = w
->func
[i
+ 1];
3000 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3008 /***********************************************************/
3009 /* ram save/restore */
3011 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3015 v
= qemu_get_byte(f
);
3018 if (qemu_get_buffer(f
, buf
, len
) != len
)
3022 v
= qemu_get_byte(f
);
3023 memset(buf
, v
, len
);
3029 if (qemu_file_has_error(f
))
3035 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3040 if (qemu_get_be32(f
) != phys_ram_size
)
3042 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3043 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3050 #define BDRV_HASH_BLOCK_SIZE 1024
3051 #define IOBUF_SIZE 4096
3052 #define RAM_CBLOCK_MAGIC 0xfabe
3054 typedef struct RamDecompressState
{
3057 uint8_t buf
[IOBUF_SIZE
];
3058 } RamDecompressState
;
3060 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3063 memset(s
, 0, sizeof(*s
));
3065 ret
= inflateInit(&s
->zstream
);
3071 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3075 s
->zstream
.avail_out
= len
;
3076 s
->zstream
.next_out
= buf
;
3077 while (s
->zstream
.avail_out
> 0) {
3078 if (s
->zstream
.avail_in
== 0) {
3079 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3081 clen
= qemu_get_be16(s
->f
);
3082 if (clen
> IOBUF_SIZE
)
3084 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3085 s
->zstream
.avail_in
= clen
;
3086 s
->zstream
.next_in
= s
->buf
;
3088 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3089 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3096 static void ram_decompress_close(RamDecompressState
*s
)
3098 inflateEnd(&s
->zstream
);
3101 #define RAM_SAVE_FLAG_FULL 0x01
3102 #define RAM_SAVE_FLAG_COMPRESS 0x02
3103 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3104 #define RAM_SAVE_FLAG_PAGE 0x08
3105 #define RAM_SAVE_FLAG_EOS 0x10
3107 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3109 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3110 uint32_t *array
= (uint32_t *)page
;
3113 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3114 if (array
[i
] != val
)
3121 static int ram_save_block(QEMUFile
*f
)
3123 static ram_addr_t current_addr
= 0;
3124 ram_addr_t saved_addr
= current_addr
;
3125 ram_addr_t addr
= 0;
3128 while (addr
< phys_ram_size
) {
3129 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3132 cpu_physical_memory_reset_dirty(current_addr
,
3133 current_addr
+ TARGET_PAGE_SIZE
,
3134 MIGRATION_DIRTY_FLAG
);
3136 ch
= *(phys_ram_base
+ current_addr
);
3138 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3139 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3140 qemu_put_byte(f
, ch
);
3142 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3143 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3149 addr
+= TARGET_PAGE_SIZE
;
3150 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3156 static ram_addr_t ram_save_threshold
= 10;
3158 static ram_addr_t
ram_save_remaining(void)
3161 ram_addr_t count
= 0;
3163 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3164 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3171 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3176 /* Make sure all dirty bits are set */
3177 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3178 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3179 cpu_physical_memory_set_dirty(addr
);
3182 /* Enable dirty memory tracking */
3183 cpu_physical_memory_set_dirty_tracking(1);
3185 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3188 while (!qemu_file_rate_limit(f
)) {
3191 ret
= ram_save_block(f
);
3192 if (ret
== 0) /* no more blocks */
3196 /* try transferring iterative blocks of memory */
3199 cpu_physical_memory_set_dirty_tracking(0);
3201 /* flush all remaining blocks regardless of rate limiting */
3202 while (ram_save_block(f
) != 0);
3205 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3207 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3210 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3212 RamDecompressState s1
, *s
= &s1
;
3216 if (ram_decompress_open(s
, f
) < 0)
3218 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3219 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3220 fprintf(stderr
, "Error while reading ram block header\n");
3224 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3225 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3230 printf("Error block header\n");
3234 ram_decompress_close(s
);
3239 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3244 if (version_id
== 1)
3245 return ram_load_v1(f
, opaque
);
3247 if (version_id
== 2) {
3248 if (qemu_get_be32(f
) != phys_ram_size
)
3250 return ram_load_dead(f
, opaque
);
3253 if (version_id
!= 3)
3257 addr
= qemu_get_be64(f
);
3259 flags
= addr
& ~TARGET_PAGE_MASK
;
3260 addr
&= TARGET_PAGE_MASK
;
3262 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3263 if (addr
!= phys_ram_size
)
3267 if (flags
& RAM_SAVE_FLAG_FULL
) {
3268 if (ram_load_dead(f
, opaque
) < 0)
3272 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3273 uint8_t ch
= qemu_get_byte(f
);
3274 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3275 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3276 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3277 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3282 void qemu_service_io(void)
3284 CPUState
*env
= cpu_single_env
;
3286 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3288 if (env
->kqemu_enabled
) {
3289 kqemu_cpu_interrupt(env
);
3295 /***********************************************************/
3296 /* bottom halves (can be seen as timers which expire ASAP) */
3307 static QEMUBH
*first_bh
= NULL
;
3309 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3312 bh
= qemu_mallocz(sizeof(QEMUBH
));
3314 bh
->opaque
= opaque
;
3315 bh
->next
= first_bh
;
3320 int qemu_bh_poll(void)
3326 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3327 if (!bh
->deleted
&& bh
->scheduled
) {
3336 /* remove deleted bhs */
3350 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3358 void qemu_bh_schedule(QEMUBH
*bh
)
3360 CPUState
*env
= cpu_single_env
;
3365 /* stop the currently executing CPU to execute the BH ASAP */
3367 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3371 void qemu_bh_cancel(QEMUBH
*bh
)
3376 void qemu_bh_delete(QEMUBH
*bh
)
3382 static void qemu_bh_update_timeout(int *timeout
)
3386 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3387 if (!bh
->deleted
&& bh
->scheduled
) {
3389 /* idle bottom halves will be polled at least
3391 *timeout
= MIN(10, *timeout
);
3393 /* non-idle bottom halves will be executed
3402 /***********************************************************/
3403 /* machine registration */
3405 static QEMUMachine
*first_machine
= NULL
;
3407 int qemu_register_machine(QEMUMachine
*m
)
3410 pm
= &first_machine
;
3418 static QEMUMachine
*find_machine(const char *name
)
3422 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3423 if (!strcmp(m
->name
, name
))
3429 /***********************************************************/
3430 /* main execution loop */
3432 static void gui_update(void *opaque
)
3434 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3435 DisplayState
*ds
= opaque
;
3436 DisplayChangeListener
*dcl
= ds
->listeners
;
3440 while (dcl
!= NULL
) {
3441 if (dcl
->gui_timer_interval
&&
3442 dcl
->gui_timer_interval
< interval
)
3443 interval
= dcl
->gui_timer_interval
;
3446 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3449 static void nographic_update(void *opaque
)
3451 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3453 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3456 struct vm_change_state_entry
{
3457 VMChangeStateHandler
*cb
;
3459 LIST_ENTRY (vm_change_state_entry
) entries
;
3462 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3464 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3467 VMChangeStateEntry
*e
;
3469 e
= qemu_mallocz(sizeof (*e
));
3473 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3477 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3479 LIST_REMOVE (e
, entries
);
3483 static void vm_state_notify(int running
, int reason
)
3485 VMChangeStateEntry
*e
;
3487 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3488 e
->cb(e
->opaque
, running
, reason
);
3497 vm_state_notify(1, 0);
3498 qemu_rearm_alarm_timer(alarm_timer
);
3502 void vm_stop(int reason
)
3505 cpu_disable_ticks();
3507 vm_state_notify(0, reason
);
3511 /* reset/shutdown handler */
3513 typedef struct QEMUResetEntry
{
3514 QEMUResetHandler
*func
;
3516 struct QEMUResetEntry
*next
;
3519 static QEMUResetEntry
*first_reset_entry
;
3520 static int reset_requested
;
3521 static int shutdown_requested
;
3522 static int powerdown_requested
;
3524 int qemu_shutdown_requested(void)
3526 int r
= shutdown_requested
;
3527 shutdown_requested
= 0;
3531 int qemu_reset_requested(void)
3533 int r
= reset_requested
;
3534 reset_requested
= 0;
3538 int qemu_powerdown_requested(void)
3540 int r
= powerdown_requested
;
3541 powerdown_requested
= 0;
3545 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3547 QEMUResetEntry
**pre
, *re
;
3549 pre
= &first_reset_entry
;
3550 while (*pre
!= NULL
)
3551 pre
= &(*pre
)->next
;
3552 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3554 re
->opaque
= opaque
;
3559 void qemu_system_reset(void)
3563 /* reset all devices */
3564 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3565 re
->func(re
->opaque
);
3569 void qemu_system_reset_request(void)
3572 shutdown_requested
= 1;
3574 reset_requested
= 1;
3577 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3580 void qemu_system_shutdown_request(void)
3582 shutdown_requested
= 1;
3584 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3587 void qemu_system_powerdown_request(void)
3589 powerdown_requested
= 1;
3591 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3595 static void host_main_loop_wait(int *timeout
)
3601 /* XXX: need to suppress polling by better using win32 events */
3603 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3604 ret
|= pe
->func(pe
->opaque
);
3608 WaitObjects
*w
= &wait_objects
;
3610 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3611 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3612 if (w
->func
[ret
- WAIT_OBJECT_0
])
3613 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3615 /* Check for additional signaled events */
3616 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3618 /* Check if event is signaled */
3619 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3620 if(ret2
== WAIT_OBJECT_0
) {
3622 w
->func
[i
](w
->opaque
[i
]);
3623 } else if (ret2
== WAIT_TIMEOUT
) {
3625 err
= GetLastError();
3626 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3629 } else if (ret
== WAIT_TIMEOUT
) {
3631 err
= GetLastError();
3632 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3639 static void host_main_loop_wait(int *timeout
)
3644 void main_loop_wait(int timeout
)
3646 IOHandlerRecord
*ioh
;
3647 fd_set rfds
, wfds
, xfds
;
3651 qemu_bh_update_timeout(&timeout
);
3653 host_main_loop_wait(&timeout
);
3655 /* poll any events */
3656 /* XXX: separate device handlers from system ones */
3661 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3665 (!ioh
->fd_read_poll
||
3666 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3667 FD_SET(ioh
->fd
, &rfds
);
3671 if (ioh
->fd_write
) {
3672 FD_SET(ioh
->fd
, &wfds
);
3678 tv
.tv_sec
= timeout
/ 1000;
3679 tv
.tv_usec
= (timeout
% 1000) * 1000;
3681 #if defined(CONFIG_SLIRP)
3682 if (slirp_is_inited()) {
3683 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3686 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3688 IOHandlerRecord
**pioh
;
3690 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3691 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3692 ioh
->fd_read(ioh
->opaque
);
3694 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3695 ioh
->fd_write(ioh
->opaque
);
3699 /* remove deleted IO handlers */
3700 pioh
= &first_io_handler
;
3710 #if defined(CONFIG_SLIRP)
3711 if (slirp_is_inited()) {
3717 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3721 /* vm time timers */
3722 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3723 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3724 qemu_get_clock(vm_clock
));
3726 /* real time timers */
3727 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3728 qemu_get_clock(rt_clock
));
3730 /* Check bottom-halves last in case any of the earlier events triggered
3736 static int main_loop(void)
3739 #ifdef CONFIG_PROFILER
3744 cur_cpu
= first_cpu
;
3745 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3752 #ifdef CONFIG_PROFILER
3753 ti
= profile_getclock();
3758 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3759 env
->icount_decr
.u16
.low
= 0;
3760 env
->icount_extra
= 0;
3761 count
= qemu_next_deadline();
3762 count
= (count
+ (1 << icount_time_shift
) - 1)
3763 >> icount_time_shift
;
3764 qemu_icount
+= count
;
3765 decr
= (count
> 0xffff) ? 0xffff : count
;
3767 env
->icount_decr
.u16
.low
= decr
;
3768 env
->icount_extra
= count
;
3770 ret
= cpu_exec(env
);
3771 #ifdef CONFIG_PROFILER
3772 qemu_time
+= profile_getclock() - ti
;
3775 /* Fold pending instructions back into the
3776 instruction counter, and clear the interrupt flag. */
3777 qemu_icount
-= (env
->icount_decr
.u16
.low
3778 + env
->icount_extra
);
3779 env
->icount_decr
.u32
= 0;
3780 env
->icount_extra
= 0;
3782 next_cpu
= env
->next_cpu
?: first_cpu
;
3783 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3784 ret
= EXCP_INTERRUPT
;
3788 if (ret
== EXCP_HLT
) {
3789 /* Give the next CPU a chance to run. */
3793 if (ret
!= EXCP_HALTED
)
3795 /* all CPUs are halted ? */
3801 if (shutdown_requested
) {
3802 ret
= EXCP_INTERRUPT
;
3810 if (reset_requested
) {
3811 reset_requested
= 0;
3812 qemu_system_reset();
3813 ret
= EXCP_INTERRUPT
;
3815 if (powerdown_requested
) {
3816 powerdown_requested
= 0;
3817 qemu_system_powerdown();
3818 ret
= EXCP_INTERRUPT
;
3820 if (unlikely(ret
== EXCP_DEBUG
)) {
3821 gdb_set_stop_cpu(cur_cpu
);
3822 vm_stop(EXCP_DEBUG
);
3824 /* If all cpus are halted then wait until the next IRQ */
3825 /* XXX: use timeout computed from timers */
3826 if (ret
== EXCP_HALTED
) {
3830 /* Advance virtual time to the next event. */
3831 if (use_icount
== 1) {
3832 /* When not using an adaptive execution frequency
3833 we tend to get badly out of sync with real time,
3834 so just delay for a reasonable amount of time. */
3837 delta
= cpu_get_icount() - cpu_get_clock();
3840 /* If virtual time is ahead of real time then just
3842 timeout
= (delta
/ 1000000) + 1;
3844 /* Wait for either IO to occur or the next
3846 add
= qemu_next_deadline();
3847 /* We advance the timer before checking for IO.
3848 Limit the amount we advance so that early IO
3849 activity won't get the guest too far ahead. */
3853 add
= (add
+ (1 << icount_time_shift
) - 1)
3854 >> icount_time_shift
;
3856 timeout
= delta
/ 1000000;
3867 if (shutdown_requested
) {
3868 ret
= EXCP_INTERRUPT
;
3873 #ifdef CONFIG_PROFILER
3874 ti
= profile_getclock();
3876 main_loop_wait(timeout
);
3877 #ifdef CONFIG_PROFILER
3878 dev_time
+= profile_getclock() - ti
;
3881 cpu_disable_ticks();
3885 static void help(int exitcode
)
3887 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3888 and qemu-doc.texi */
3889 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3890 "usage: %s [options] [disk_image]\n"
3892 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3894 "Standard options:\n"
3895 "-h or -help display this help and exit\n"
3896 "-M machine select emulated machine (-M ? for list)\n"
3897 "-cpu cpu select CPU (-cpu ? for list)\n"
3898 "-smp n set the number of CPUs to 'n' [default=1]\n"
3899 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3900 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3901 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3902 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3903 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3904 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3905 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3906 " use 'file' as a drive image\n"
3907 "-mtdblock file use 'file' as on-board Flash memory image\n"
3908 "-sd file use 'file' as SecureDigital card image\n"
3909 "-pflash file use 'file' as a parallel flash image\n"
3910 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3911 "-snapshot write to temporary files instead of disk image files\n"
3912 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3914 "-k language use keyboard layout (for example \"fr\" for French)\n"
3917 "-audio-help print list of audio drivers and their options\n"
3918 "-soundhw c1,... enable audio support\n"
3919 " and only specified sound cards (comma separated list)\n"
3920 " use -soundhw ? to get the list of supported cards\n"
3921 " use -soundhw all to enable all of them\n"
3923 "-usb enable the USB driver (will be the default soon)\n"
3924 "-usbdevice name add the host or guest USB device 'name'\n"
3925 "-name string set the name of the guest\n"
3926 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3927 " specify machine UUID\n"
3929 "Display options:\n"
3930 "-nographic disable graphical output and redirect serial I/Os to console\n"
3931 #ifdef CONFIG_CURSES
3932 "-curses use a curses/ncurses interface instead of SDL\n"
3935 "-no-frame open SDL window without a frame and window decorations\n"
3936 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3937 "-no-quit disable SDL window close capability\n"
3940 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3941 "-vga [std|cirrus|vmware|none]\n"
3942 " select video card type\n"
3943 "-full-screen start in full screen\n"
3944 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3945 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3947 "-vnc display start a VNC server on display\n"
3949 "Network options:\n"
3950 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3951 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3953 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3954 " connect the user mode network stack to VLAN 'n' and send\n"
3955 " hostname 'host' to DHCP clients\n"
3958 "-net tap[,vlan=n][,name=str],ifname=name\n"
3959 " connect the host TAP network interface to VLAN 'n'\n"
3961 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3962 " connect the host TAP network interface to VLAN 'n' and use the\n"
3963 " network scripts 'file' (default=%s)\n"
3964 " and 'dfile' (default=%s);\n"
3965 " use '[down]script=no' to disable script execution;\n"
3966 " use 'fd=h' to connect to an already opened TAP interface\n"
3968 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3969 " connect the vlan 'n' to another VLAN using a socket connection\n"
3970 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3971 " connect the vlan 'n' to multicast maddr and port\n"
3973 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3974 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3975 " on host and listening for incoming connections on 'socketpath'.\n"
3976 " Use group 'groupname' and mode 'octalmode' to change default\n"
3977 " ownership and permissions for communication port.\n"
3979 "-net none use it alone to have zero network devices; if no -net option\n"
3980 " is provided, the default is '-net nic -net user'\n"
3982 "-tftp dir allow tftp access to files in dir [-net user]\n"
3983 "-bootp file advertise file in BOOTP replies\n"
3985 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3987 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3988 " redirect TCP or UDP connections from host to guest [-net user]\n"
3991 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
3992 "-bt hci,host[:id]\n"
3993 " use host's HCI with the given name\n"
3994 "-bt hci[,vlan=n]\n"
3995 " emulate a standard HCI in virtual scatternet 'n'\n"
3996 "-bt vhci[,vlan=n]\n"
3997 " add host computer to virtual scatternet 'n' using VHCI\n"
3998 "-bt device:dev[,vlan=n]\n"
3999 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4003 "i386 target only:\n"
4004 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4005 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4006 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4007 "-no-acpi disable ACPI\n"
4008 "-no-hpet disable HPET\n"
4010 "Linux boot specific:\n"
4011 "-kernel bzImage use 'bzImage' as kernel image\n"
4012 "-append cmdline use 'cmdline' as kernel command line\n"
4013 "-initrd file use 'file' as initial ram disk\n"
4015 "Debug/Expert options:\n"
4016 "-serial dev redirect the serial port to char device 'dev'\n"
4017 "-parallel dev redirect the parallel port to char device 'dev'\n"
4018 "-monitor dev redirect the monitor to char device 'dev'\n"
4019 "-pidfile file write PID to 'file'\n"
4020 "-S freeze CPU at startup (use 'c' to start execution)\n"
4021 "-s wait gdb connection to port\n"
4022 "-p port set gdb connection port [default=%s]\n"
4023 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4024 "-hdachs c,h,s[,t]\n"
4025 " force hard disk 0 physical geometry and the optional BIOS\n"
4026 " translation (t=none or lba) (usually qemu can guess them)\n"
4027 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4028 "-bios file set the filename for the BIOS\n"
4030 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4031 "-no-kqemu disable KQEMU kernel module usage\n"
4034 "-enable-kvm enable KVM full virtualization support\n"
4036 "-no-reboot exit instead of rebooting\n"
4037 "-no-shutdown stop before shutdown\n"
4038 "-loadvm [tag|id]\n"
4039 " start right away with a saved state (loadvm in monitor)\n"
4041 "-daemonize daemonize QEMU after initializing\n"
4043 "-option-rom rom load a file, rom, into the option ROM space\n"
4044 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4045 "-prom-env variable=value\n"
4046 " set OpenBIOS nvram variables\n"
4048 "-clock force the use of the given methods for timer alarm.\n"
4049 " To see what timers are available use -clock ?\n"
4050 "-localtime set the real time clock to local time [default=utc]\n"
4051 "-startdate select initial date of the clock\n"
4052 "-icount [N|auto]\n"
4053 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4054 "-echr chr set terminal escape character instead of ctrl-a\n"
4055 "-virtioconsole c\n"
4056 " set virtio console\n"
4057 "-show-cursor show cursor\n"
4058 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4059 "-semihosting semihosting mode\n"
4061 #if defined(TARGET_ARM)
4062 "-old-param old param mode\n"
4064 "-tb-size n set TB size\n"
4065 "-incoming p prepare for incoming migration, listen on port p\n"
4067 "During emulation, the following keys are useful:\n"
4068 "ctrl-alt-f toggle full screen\n"
4069 "ctrl-alt-n switch to virtual console 'n'\n"
4070 "ctrl-alt toggle mouse and keyboard grab\n"
4072 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4077 DEFAULT_NETWORK_SCRIPT
,
4078 DEFAULT_NETWORK_DOWN_SCRIPT
,
4080 DEFAULT_GDBSTUB_PORT
,
4085 #define HAS_ARG 0x0001
4088 /* Please keep in synch with help, qemu_options[] and
4090 /* Standard options: */
4103 QEMU_OPTION_mtdblock
,
4107 QEMU_OPTION_snapshot
,
4110 QEMU_OPTION_audio_help
,
4111 QEMU_OPTION_soundhw
,
4113 QEMU_OPTION_usbdevice
,
4117 /* Display options: */
4118 QEMU_OPTION_nographic
,
4120 QEMU_OPTION_no_frame
,
4121 QEMU_OPTION_alt_grab
,
4122 QEMU_OPTION_no_quit
,
4124 QEMU_OPTION_portrait
,
4126 QEMU_OPTION_full_screen
,
4130 /* Network options: */
4138 /* i386 target only: */
4139 QEMU_OPTION_win2k_hack
,
4140 QEMU_OPTION_rtc_td_hack
,
4141 QEMU_OPTION_no_fd_bootchk
,
4142 QEMU_OPTION_no_acpi
,
4143 QEMU_OPTION_no_hpet
,
4145 /* Linux boot specific: */
4150 /* Debug/Expert options: */
4152 QEMU_OPTION_parallel
,
4153 QEMU_OPTION_monitor
,
4154 QEMU_OPTION_pidfile
,
4162 QEMU_OPTION_kernel_kqemu
,
4163 QEMU_OPTION_no_kqemu
,
4164 QEMU_OPTION_enable_kvm
,
4165 QEMU_OPTION_no_reboot
,
4166 QEMU_OPTION_no_shutdown
,
4168 QEMU_OPTION_daemonize
,
4169 QEMU_OPTION_option_rom
,
4170 QEMU_OPTION_prom_env
,
4172 QEMU_OPTION_localtime
,
4173 QEMU_OPTION_startdate
,
4176 QEMU_OPTION_virtiocon
,
4177 QEMU_OPTION_show_cursor
,
4178 QEMU_OPTION_semihosting
,
4179 QEMU_OPTION_old_param
,
4180 QEMU_OPTION_tb_size
,
4181 QEMU_OPTION_incoming
,
4184 typedef struct QEMUOption
{
4190 static const QEMUOption qemu_options
[] = {
4191 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4193 /* Standard options: */
4194 { "h", 0, QEMU_OPTION_h
},
4195 { "help", 0, QEMU_OPTION_h
},
4196 { "M", HAS_ARG
, QEMU_OPTION_M
},
4197 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4198 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4199 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4200 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4201 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4202 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4203 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4204 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4205 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4206 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4207 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4208 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4209 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4210 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4211 { "snapshot", 0, QEMU_OPTION_snapshot
},
4212 { "m", HAS_ARG
, QEMU_OPTION_m
},
4214 { "k", HAS_ARG
, QEMU_OPTION_k
},
4217 { "audio-help", 0, QEMU_OPTION_audio_help
},
4218 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4220 { "usb", 0, QEMU_OPTION_usb
},
4221 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4222 { "name", HAS_ARG
, QEMU_OPTION_name
},
4223 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4225 /* Display options: */
4226 { "nographic", 0, QEMU_OPTION_nographic
},
4227 #ifdef CONFIG_CURSES
4228 { "curses", 0, QEMU_OPTION_curses
},
4231 { "no-frame", 0, QEMU_OPTION_no_frame
},
4232 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4233 { "no-quit", 0, QEMU_OPTION_no_quit
},
4234 { "sdl", 0, QEMU_OPTION_sdl
},
4236 { "portrait", 0, QEMU_OPTION_portrait
},
4237 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4238 { "full-screen", 0, QEMU_OPTION_full_screen
},
4239 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4240 { "g", 1, QEMU_OPTION_g
},
4242 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4244 /* Network options: */
4245 { "net", HAS_ARG
, QEMU_OPTION_net
},
4247 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4248 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4250 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4252 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4254 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4256 /* i386 target only: */
4257 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4258 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4259 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4260 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4261 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4264 /* Linux boot specific: */
4265 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4266 { "append", HAS_ARG
, QEMU_OPTION_append
},
4267 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4269 /* Debug/Expert options: */
4270 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4271 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4272 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4273 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4274 { "S", 0, QEMU_OPTION_S
},
4275 { "s", 0, QEMU_OPTION_s
},
4276 { "p", HAS_ARG
, QEMU_OPTION_p
},
4277 { "d", HAS_ARG
, QEMU_OPTION_d
},
4278 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4279 { "L", HAS_ARG
, QEMU_OPTION_L
},
4280 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4282 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4283 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4286 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4288 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4289 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4290 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4291 { "daemonize", 0, QEMU_OPTION_daemonize
},
4292 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4293 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4294 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4296 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4297 { "localtime", 0, QEMU_OPTION_localtime
},
4298 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4299 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4300 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4301 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4302 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4303 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4304 { "semihosting", 0, QEMU_OPTION_semihosting
},
4306 #if defined(TARGET_ARM)
4307 { "old-param", 0, QEMU_OPTION_old_param
},
4309 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4310 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4314 /* password input */
4316 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4321 if (!bdrv_is_encrypted(bs
))
4324 term_printf("%s is encrypted.\n", name
);
4325 for(i
= 0; i
< 3; i
++) {
4326 monitor_readline("Password: ", 1, password
, sizeof(password
));
4327 if (bdrv_set_key(bs
, password
) == 0)
4329 term_printf("invalid password\n");
4334 static BlockDriverState
*get_bdrv(int index
)
4336 if (index
> nb_drives
)
4338 return drives_table
[index
].bdrv
;
4341 static void read_passwords(void)
4343 BlockDriverState
*bs
;
4346 for(i
= 0; i
< 6; i
++) {
4349 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4354 struct soundhw soundhw
[] = {
4355 #ifdef HAS_AUDIO_CHOICE
4356 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4362 { .init_isa
= pcspk_audio_init
}
4369 "Creative Sound Blaster 16",
4372 { .init_isa
= SB16_init
}
4376 #ifdef CONFIG_CS4231A
4382 { .init_isa
= cs4231a_init
}
4390 "Yamaha YMF262 (OPL3)",
4392 "Yamaha YM3812 (OPL2)",
4396 { .init_isa
= Adlib_init
}
4403 "Gravis Ultrasound GF1",
4406 { .init_isa
= GUS_init
}
4413 "Intel 82801AA AC97 Audio",
4416 { .init_pci
= ac97_init
}
4420 #ifdef CONFIG_ES1370
4423 "ENSONIQ AudioPCI ES1370",
4426 { .init_pci
= es1370_init
}
4430 #endif /* HAS_AUDIO_CHOICE */
4432 { NULL
, NULL
, 0, 0, { NULL
} }
4435 static void select_soundhw (const char *optarg
)
4439 if (*optarg
== '?') {
4442 printf ("Valid sound card names (comma separated):\n");
4443 for (c
= soundhw
; c
->name
; ++c
) {
4444 printf ("%-11s %s\n", c
->name
, c
->descr
);
4446 printf ("\n-soundhw all will enable all of the above\n");
4447 exit (*optarg
!= '?');
4455 if (!strcmp (optarg
, "all")) {
4456 for (c
= soundhw
; c
->name
; ++c
) {
4464 e
= strchr (p
, ',');
4465 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4467 for (c
= soundhw
; c
->name
; ++c
) {
4468 if (!strncmp (c
->name
, p
, l
)) {
4477 "Unknown sound card name (too big to show)\n");
4480 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4485 p
+= l
+ (e
!= NULL
);
4489 goto show_valid_cards
;
4494 static void select_vgahw (const char *p
)
4498 if (strstart(p
, "std", &opts
)) {
4499 std_vga_enabled
= 1;
4500 cirrus_vga_enabled
= 0;
4502 } else if (strstart(p
, "cirrus", &opts
)) {
4503 cirrus_vga_enabled
= 1;
4504 std_vga_enabled
= 0;
4506 } else if (strstart(p
, "vmware", &opts
)) {
4507 cirrus_vga_enabled
= 0;
4508 std_vga_enabled
= 0;
4510 } else if (strstart(p
, "none", &opts
)) {
4511 cirrus_vga_enabled
= 0;
4512 std_vga_enabled
= 0;
4516 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4520 const char *nextopt
;
4522 if (strstart(opts
, ",retrace=", &nextopt
)) {
4524 if (strstart(opts
, "dumb", &nextopt
))
4525 vga_retrace_method
= VGA_RETRACE_DUMB
;
4526 else if (strstart(opts
, "precise", &nextopt
))
4527 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4528 else goto invalid_vga
;
4529 } else goto invalid_vga
;
4535 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4537 exit(STATUS_CONTROL_C_EXIT
);
4542 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4546 if(strlen(str
) != 36)
4549 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4550 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4551 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4559 #define MAX_NET_CLIENTS 32
4563 static void termsig_handler(int signal
)
4565 qemu_system_shutdown_request();
4568 static void termsig_setup(void)
4570 struct sigaction act
;
4572 memset(&act
, 0, sizeof(act
));
4573 act
.sa_handler
= termsig_handler
;
4574 sigaction(SIGINT
, &act
, NULL
);
4575 sigaction(SIGHUP
, &act
, NULL
);
4576 sigaction(SIGTERM
, &act
, NULL
);
4581 int main(int argc
, char **argv
, char **envp
)
4583 #ifdef CONFIG_GDBSTUB
4585 const char *gdbstub_port
;
4587 uint32_t boot_devices_bitmap
= 0;
4589 int snapshot
, linux_boot
, net_boot
;
4590 const char *initrd_filename
;
4591 const char *kernel_filename
, *kernel_cmdline
;
4592 const char *boot_devices
= "";
4594 DisplayChangeListener
*dcl
;
4595 int cyls
, heads
, secs
, translation
;
4596 const char *net_clients
[MAX_NET_CLIENTS
];
4598 const char *bt_opts
[MAX_BT_CMDLINE
];
4602 const char *r
, *optarg
;
4603 CharDriverState
*monitor_hd
= NULL
;
4604 const char *monitor_device
;
4605 const char *serial_devices
[MAX_SERIAL_PORTS
];
4606 int serial_device_index
;
4607 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4608 int parallel_device_index
;
4609 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4610 int virtio_console_index
;
4611 const char *loadvm
= NULL
;
4612 QEMUMachine
*machine
;
4613 const char *cpu_model
;
4614 const char *usb_devices
[MAX_USB_CMDLINE
];
4615 int usb_devices_index
;
4618 const char *pid_file
= NULL
;
4620 const char *incoming
= NULL
;
4622 qemu_cache_utils_init(envp
);
4624 LIST_INIT (&vm_change_state_head
);
4627 struct sigaction act
;
4628 sigfillset(&act
.sa_mask
);
4630 act
.sa_handler
= SIG_IGN
;
4631 sigaction(SIGPIPE
, &act
, NULL
);
4634 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4635 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4636 QEMU to run on a single CPU */
4641 h
= GetCurrentProcess();
4642 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4643 for(i
= 0; i
< 32; i
++) {
4644 if (mask
& (1 << i
))
4649 SetProcessAffinityMask(h
, mask
);
4655 register_machines();
4656 machine
= first_machine
;
4658 initrd_filename
= NULL
;
4660 vga_ram_size
= VGA_RAM_SIZE
;
4661 #ifdef CONFIG_GDBSTUB
4663 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4668 kernel_filename
= NULL
;
4669 kernel_cmdline
= "";
4670 cyls
= heads
= secs
= 0;
4671 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4672 monitor_device
= "vc";
4674 serial_devices
[0] = "vc:80Cx24C";
4675 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4676 serial_devices
[i
] = NULL
;
4677 serial_device_index
= 0;
4679 parallel_devices
[0] = "vc:640x480";
4680 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4681 parallel_devices
[i
] = NULL
;
4682 parallel_device_index
= 0;
4684 virtio_consoles
[0] = "vc:80Cx24C";
4685 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4686 virtio_consoles
[i
] = NULL
;
4687 virtio_console_index
= 0;
4689 usb_devices_index
= 0;
4708 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4710 const QEMUOption
*popt
;
4713 /* Treat --foo the same as -foo. */
4716 popt
= qemu_options
;
4719 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4723 if (!strcmp(popt
->name
, r
+ 1))
4727 if (popt
->flags
& HAS_ARG
) {
4728 if (optind
>= argc
) {
4729 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4733 optarg
= argv
[optind
++];
4738 switch(popt
->index
) {
4740 machine
= find_machine(optarg
);
4743 printf("Supported machines are:\n");
4744 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4745 printf("%-10s %s%s\n",
4747 m
== first_machine
? " (default)" : "");
4749 exit(*optarg
!= '?');
4752 case QEMU_OPTION_cpu
:
4753 /* hw initialization will check this */
4754 if (*optarg
== '?') {
4755 /* XXX: implement xxx_cpu_list for targets that still miss it */
4756 #if defined(cpu_list)
4757 cpu_list(stdout
, &fprintf
);
4764 case QEMU_OPTION_initrd
:
4765 initrd_filename
= optarg
;
4767 case QEMU_OPTION_hda
:
4769 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4771 hda_index
= drive_add(optarg
, HD_ALIAS
4772 ",cyls=%d,heads=%d,secs=%d%s",
4773 0, cyls
, heads
, secs
,
4774 translation
== BIOS_ATA_TRANSLATION_LBA
?
4776 translation
== BIOS_ATA_TRANSLATION_NONE
?
4777 ",trans=none" : "");
4779 case QEMU_OPTION_hdb
:
4780 case QEMU_OPTION_hdc
:
4781 case QEMU_OPTION_hdd
:
4782 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4784 case QEMU_OPTION_drive
:
4785 drive_add(NULL
, "%s", optarg
);
4787 case QEMU_OPTION_mtdblock
:
4788 drive_add(optarg
, MTD_ALIAS
);
4790 case QEMU_OPTION_sd
:
4791 drive_add(optarg
, SD_ALIAS
);
4793 case QEMU_OPTION_pflash
:
4794 drive_add(optarg
, PFLASH_ALIAS
);
4796 case QEMU_OPTION_snapshot
:
4799 case QEMU_OPTION_hdachs
:
4803 cyls
= strtol(p
, (char **)&p
, 0);
4804 if (cyls
< 1 || cyls
> 16383)
4809 heads
= strtol(p
, (char **)&p
, 0);
4810 if (heads
< 1 || heads
> 16)
4815 secs
= strtol(p
, (char **)&p
, 0);
4816 if (secs
< 1 || secs
> 63)
4820 if (!strcmp(p
, "none"))
4821 translation
= BIOS_ATA_TRANSLATION_NONE
;
4822 else if (!strcmp(p
, "lba"))
4823 translation
= BIOS_ATA_TRANSLATION_LBA
;
4824 else if (!strcmp(p
, "auto"))
4825 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4828 } else if (*p
!= '\0') {
4830 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4833 if (hda_index
!= -1)
4834 snprintf(drives_opt
[hda_index
].opt
,
4835 sizeof(drives_opt
[hda_index
].opt
),
4836 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4837 0, cyls
, heads
, secs
,
4838 translation
== BIOS_ATA_TRANSLATION_LBA
?
4840 translation
== BIOS_ATA_TRANSLATION_NONE
?
4841 ",trans=none" : "");
4844 case QEMU_OPTION_nographic
:
4847 #ifdef CONFIG_CURSES
4848 case QEMU_OPTION_curses
:
4852 case QEMU_OPTION_portrait
:
4855 case QEMU_OPTION_kernel
:
4856 kernel_filename
= optarg
;
4858 case QEMU_OPTION_append
:
4859 kernel_cmdline
= optarg
;
4861 case QEMU_OPTION_cdrom
:
4862 drive_add(optarg
, CDROM_ALIAS
);
4864 case QEMU_OPTION_boot
:
4865 boot_devices
= optarg
;
4866 /* We just do some generic consistency checks */
4868 /* Could easily be extended to 64 devices if needed */
4871 boot_devices_bitmap
= 0;
4872 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4873 /* Allowed boot devices are:
4874 * a b : floppy disk drives
4875 * c ... f : IDE disk drives
4876 * g ... m : machine implementation dependant drives
4877 * n ... p : network devices
4878 * It's up to each machine implementation to check
4879 * if the given boot devices match the actual hardware
4880 * implementation and firmware features.
4882 if (*p
< 'a' || *p
> 'q') {
4883 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4886 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4888 "Boot device '%c' was given twice\n",*p
);
4891 boot_devices_bitmap
|= 1 << (*p
- 'a');
4895 case QEMU_OPTION_fda
:
4896 case QEMU_OPTION_fdb
:
4897 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4900 case QEMU_OPTION_no_fd_bootchk
:
4904 case QEMU_OPTION_net
:
4905 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4906 fprintf(stderr
, "qemu: too many network clients\n");
4909 net_clients
[nb_net_clients
] = optarg
;
4913 case QEMU_OPTION_tftp
:
4914 tftp_prefix
= optarg
;
4916 case QEMU_OPTION_bootp
:
4917 bootp_filename
= optarg
;
4920 case QEMU_OPTION_smb
:
4921 net_slirp_smb(optarg
);
4924 case QEMU_OPTION_redir
:
4925 net_slirp_redir(optarg
);
4928 case QEMU_OPTION_bt
:
4929 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4930 fprintf(stderr
, "qemu: too many bluetooth options\n");
4933 bt_opts
[nb_bt_opts
++] = optarg
;
4936 case QEMU_OPTION_audio_help
:
4940 case QEMU_OPTION_soundhw
:
4941 select_soundhw (optarg
);
4947 case QEMU_OPTION_m
: {
4951 value
= strtoul(optarg
, &ptr
, 10);
4953 case 0: case 'M': case 'm':
4960 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4964 /* On 32-bit hosts, QEMU is limited by virtual address space */
4965 if (value
> (2047 << 20)
4967 && HOST_LONG_BITS
== 32
4970 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4973 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4974 fprintf(stderr
, "qemu: ram size too large\n");
4983 const CPULogItem
*item
;
4985 mask
= cpu_str_to_log_mask(optarg
);
4987 printf("Log items (comma separated):\n");
4988 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4989 printf("%-10s %s\n", item
->name
, item
->help
);
4996 #ifdef CONFIG_GDBSTUB
5001 gdbstub_port
= optarg
;
5007 case QEMU_OPTION_bios
:
5014 keyboard_layout
= optarg
;
5016 case QEMU_OPTION_localtime
:
5019 case QEMU_OPTION_vga
:
5020 select_vgahw (optarg
);
5027 w
= strtol(p
, (char **)&p
, 10);
5030 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5036 h
= strtol(p
, (char **)&p
, 10);
5041 depth
= strtol(p
, (char **)&p
, 10);
5042 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5043 depth
!= 24 && depth
!= 32)
5045 } else if (*p
== '\0') {
5046 depth
= graphic_depth
;
5053 graphic_depth
= depth
;
5056 case QEMU_OPTION_echr
:
5059 term_escape_char
= strtol(optarg
, &r
, 0);
5061 printf("Bad argument to echr\n");
5064 case QEMU_OPTION_monitor
:
5065 monitor_device
= optarg
;
5067 case QEMU_OPTION_serial
:
5068 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5069 fprintf(stderr
, "qemu: too many serial ports\n");
5072 serial_devices
[serial_device_index
] = optarg
;
5073 serial_device_index
++;
5075 case QEMU_OPTION_virtiocon
:
5076 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5077 fprintf(stderr
, "qemu: too many virtio consoles\n");
5080 virtio_consoles
[virtio_console_index
] = optarg
;
5081 virtio_console_index
++;
5083 case QEMU_OPTION_parallel
:
5084 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5085 fprintf(stderr
, "qemu: too many parallel ports\n");
5088 parallel_devices
[parallel_device_index
] = optarg
;
5089 parallel_device_index
++;
5091 case QEMU_OPTION_loadvm
:
5094 case QEMU_OPTION_full_screen
:
5098 case QEMU_OPTION_no_frame
:
5101 case QEMU_OPTION_alt_grab
:
5104 case QEMU_OPTION_no_quit
:
5107 case QEMU_OPTION_sdl
:
5111 case QEMU_OPTION_pidfile
:
5115 case QEMU_OPTION_win2k_hack
:
5116 win2k_install_hack
= 1;
5118 case QEMU_OPTION_rtc_td_hack
:
5123 case QEMU_OPTION_no_kqemu
:
5126 case QEMU_OPTION_kernel_kqemu
:
5131 case QEMU_OPTION_enable_kvm
:
5138 case QEMU_OPTION_usb
:
5141 case QEMU_OPTION_usbdevice
:
5143 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5144 fprintf(stderr
, "Too many USB devices\n");
5147 usb_devices
[usb_devices_index
] = optarg
;
5148 usb_devices_index
++;
5150 case QEMU_OPTION_smp
:
5151 smp_cpus
= atoi(optarg
);
5153 fprintf(stderr
, "Invalid number of CPUs\n");
5157 case QEMU_OPTION_vnc
:
5158 vnc_display
= optarg
;
5160 case QEMU_OPTION_no_acpi
:
5163 case QEMU_OPTION_no_hpet
:
5166 case QEMU_OPTION_no_reboot
:
5169 case QEMU_OPTION_no_shutdown
:
5172 case QEMU_OPTION_show_cursor
:
5175 case QEMU_OPTION_uuid
:
5176 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5177 fprintf(stderr
, "Fail to parse UUID string."
5178 " Wrong format.\n");
5182 case QEMU_OPTION_daemonize
:
5185 case QEMU_OPTION_option_rom
:
5186 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5187 fprintf(stderr
, "Too many option ROMs\n");
5190 option_rom
[nb_option_roms
] = optarg
;
5193 case QEMU_OPTION_semihosting
:
5194 semihosting_enabled
= 1;
5196 case QEMU_OPTION_name
:
5199 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5200 case QEMU_OPTION_prom_env
:
5201 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5202 fprintf(stderr
, "Too many prom variables\n");
5205 prom_envs
[nb_prom_envs
] = optarg
;
5210 case QEMU_OPTION_old_param
:
5214 case QEMU_OPTION_clock
:
5215 configure_alarms(optarg
);
5217 case QEMU_OPTION_startdate
:
5220 time_t rtc_start_date
;
5221 if (!strcmp(optarg
, "now")) {
5222 rtc_date_offset
= -1;
5224 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5232 } else if (sscanf(optarg
, "%d-%d-%d",
5235 &tm
.tm_mday
) == 3) {
5244 rtc_start_date
= mktimegm(&tm
);
5245 if (rtc_start_date
== -1) {
5247 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5248 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5251 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5255 case QEMU_OPTION_tb_size
:
5256 tb_size
= strtol(optarg
, NULL
, 0);
5260 case QEMU_OPTION_icount
:
5262 if (strcmp(optarg
, "auto") == 0) {
5263 icount_time_shift
= -1;
5265 icount_time_shift
= strtol(optarg
, NULL
, 0);
5268 case QEMU_OPTION_incoming
:
5275 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5276 if (kvm_allowed
&& kqemu_allowed
) {
5278 "You can not enable both KVM and kqemu at the same time\n");
5283 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5284 if (smp_cpus
> machine
->max_cpus
) {
5285 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5286 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5292 if (serial_device_index
== 0)
5293 serial_devices
[0] = "stdio";
5294 if (parallel_device_index
== 0)
5295 parallel_devices
[0] = "null";
5296 if (strncmp(monitor_device
, "vc", 2) == 0)
5297 monitor_device
= "stdio";
5298 if (virtio_console_index
== 0)
5299 virtio_consoles
[0] = "null";
5306 if (pipe(fds
) == -1)
5317 len
= read(fds
[0], &status
, 1);
5318 if (len
== -1 && (errno
== EINTR
))
5323 else if (status
== 1) {
5324 fprintf(stderr
, "Could not acquire pidfile\n");
5341 signal(SIGTSTP
, SIG_IGN
);
5342 signal(SIGTTOU
, SIG_IGN
);
5343 signal(SIGTTIN
, SIG_IGN
);
5347 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5350 write(fds
[1], &status
, 1);
5352 fprintf(stderr
, "Could not acquire pid file\n");
5360 linux_boot
= (kernel_filename
!= NULL
);
5361 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5363 if (!linux_boot
&& net_boot
== 0 &&
5364 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5367 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5368 fprintf(stderr
, "-append only allowed with -kernel option\n");
5372 if (!linux_boot
&& initrd_filename
!= NULL
) {
5373 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5377 /* boot to floppy or the default cd if no hard disk defined yet */
5378 if (!boot_devices
[0]) {
5379 boot_devices
= "cad";
5381 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5384 if (init_timer_alarm() < 0) {
5385 fprintf(stderr
, "could not initialize alarm timer\n");
5388 if (use_icount
&& icount_time_shift
< 0) {
5390 /* 125MIPS seems a reasonable initial guess at the guest speed.
5391 It will be corrected fairly quickly anyway. */
5392 icount_time_shift
= 3;
5393 init_icount_adjust();
5400 /* init network clients */
5401 if (nb_net_clients
== 0) {
5402 /* if no clients, we use a default config */
5403 net_clients
[nb_net_clients
++] = "nic";
5405 net_clients
[nb_net_clients
++] = "user";
5409 for(i
= 0;i
< nb_net_clients
; i
++) {
5410 if (net_client_parse(net_clients
[i
]) < 0)
5416 /* XXX: this should be moved in the PC machine instantiation code */
5417 if (net_boot
!= 0) {
5419 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5420 const char *model
= nd_table
[i
].model
;
5422 if (net_boot
& (1 << i
)) {
5425 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5426 if (get_image_size(buf
) > 0) {
5427 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5428 fprintf(stderr
, "Too many option ROMs\n");
5431 option_rom
[nb_option_roms
] = strdup(buf
);
5438 fprintf(stderr
, "No valid PXE rom found for network device\n");
5444 /* init the bluetooth world */
5445 for (i
= 0; i
< nb_bt_opts
; i
++)
5446 if (bt_parse(bt_opts
[i
]))
5449 /* init the memory */
5450 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5452 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5454 if (ram_size
< phys_ram_size
) {
5455 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5456 machine
->name
, (unsigned long long) phys_ram_size
);
5460 phys_ram_size
= ram_size
;
5462 ram_size
= phys_ram_size
;
5465 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5467 phys_ram_size
+= ram_size
;
5470 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5471 if (!phys_ram_base
) {
5472 fprintf(stderr
, "Could not allocate physical memory\n");
5476 /* init the dynamic translator */
5477 cpu_exec_init_all(tb_size
* 1024 * 1024);
5481 /* we always create the cdrom drive, even if no disk is there */
5483 if (nb_drives_opt
< MAX_DRIVES
)
5484 drive_add(NULL
, CDROM_ALIAS
);
5486 /* we always create at least one floppy */
5488 if (nb_drives_opt
< MAX_DRIVES
)
5489 drive_add(NULL
, FD_ALIAS
, 0);
5491 /* we always create one sd slot, even if no card is in it */
5493 if (nb_drives_opt
< MAX_DRIVES
)
5494 drive_add(NULL
, SD_ALIAS
);
5496 /* open the virtual block devices */
5498 for(i
= 0; i
< nb_drives_opt
; i
++)
5499 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5502 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5503 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5506 /* must be after terminal init, SDL library changes signal handlers */
5510 /* Maintain compatibility with multiple stdio monitors */
5511 if (!strcmp(monitor_device
,"stdio")) {
5512 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5513 const char *devname
= serial_devices
[i
];
5514 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5515 monitor_device
= NULL
;
5517 } else if (devname
&& !strcmp(devname
,"stdio")) {
5518 monitor_device
= NULL
;
5519 serial_devices
[i
] = "mon:stdio";
5525 if (kvm_enabled()) {
5528 ret
= kvm_init(smp_cpus
);
5530 fprintf(stderr
, "failed to initialize KVM\n");
5535 if (monitor_device
) {
5536 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5538 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5543 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5544 const char *devname
= serial_devices
[i
];
5545 if (devname
&& strcmp(devname
, "none")) {
5547 snprintf(label
, sizeof(label
), "serial%d", i
);
5548 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5549 if (!serial_hds
[i
]) {
5550 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5557 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5558 const char *devname
= parallel_devices
[i
];
5559 if (devname
&& strcmp(devname
, "none")) {
5561 snprintf(label
, sizeof(label
), "parallel%d", i
);
5562 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5563 if (!parallel_hds
[i
]) {
5564 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5571 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5572 const char *devname
= virtio_consoles
[i
];
5573 if (devname
&& strcmp(devname
, "none")) {
5575 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5576 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5577 if (!virtcon_hds
[i
]) {
5578 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5585 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5586 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5588 /* Set KVM's vcpu state to qemu's initial CPUState. */
5589 if (kvm_enabled()) {
5592 ret
= kvm_sync_vcpus();
5594 fprintf(stderr
, "failed to initialize vcpus\n");
5599 /* init USB devices */
5601 for(i
= 0; i
< usb_devices_index
; i
++) {
5602 if (usb_device_add(usb_devices
[i
]) < 0) {
5603 fprintf(stderr
, "Warning: could not add USB device %s\n",
5610 dumb_display_init();
5611 /* just use the first displaystate for the moment */
5616 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5620 #if defined(CONFIG_CURSES)
5622 /* At the moment curses cannot be used with other displays */
5623 curses_display_init(ds
, full_screen
);
5627 if (vnc_display
!= NULL
) {
5628 vnc_display_init(ds
);
5629 if (vnc_display_open(ds
, vnc_display
) < 0)
5632 #if defined(CONFIG_SDL)
5633 if (sdl
|| !vnc_display
)
5634 sdl_display_init(ds
, full_screen
, no_frame
);
5635 #elif defined(CONFIG_COCOA)
5636 if (sdl
|| !vnc_display
)
5637 cocoa_display_init(ds
, full_screen
);
5643 dcl
= ds
->listeners
;
5644 while (dcl
!= NULL
) {
5645 if (dcl
->dpy_refresh
!= NULL
) {
5646 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5647 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5652 if (nographic
|| (vnc_display
&& !sdl
)) {
5653 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5654 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5657 text_consoles_set_display(display_state
);
5659 if (monitor_device
&& monitor_hd
)
5660 monitor_init(monitor_hd
, !nographic
);
5662 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5663 const char *devname
= serial_devices
[i
];
5664 if (devname
&& strcmp(devname
, "none")) {
5666 snprintf(label
, sizeof(label
), "serial%d", i
);
5667 if (strstart(devname
, "vc", 0))
5668 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5672 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5673 const char *devname
= parallel_devices
[i
];
5674 if (devname
&& strcmp(devname
, "none")) {
5676 snprintf(label
, sizeof(label
), "parallel%d", i
);
5677 if (strstart(devname
, "vc", 0))
5678 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5682 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5683 const char *devname
= virtio_consoles
[i
];
5684 if (virtcon_hds
[i
] && devname
) {
5686 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5687 if (strstart(devname
, "vc", 0))
5688 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5692 #ifdef CONFIG_GDBSTUB
5694 /* XXX: use standard host:port notation and modify options
5696 if (gdbserver_start(gdbstub_port
) < 0) {
5697 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5708 autostart
= 0; /* fixme how to deal with -daemonize */
5709 qemu_start_incoming_migration(incoming
);
5713 /* XXX: simplify init */
5726 len
= write(fds
[1], &status
, 1);
5727 if (len
== -1 && (errno
== EINTR
))
5734 TFR(fd
= open("/dev/null", O_RDWR
));