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
;
408 ioport_opaque
[i
] = NULL
;
412 /***********************************************************/
414 void cpu_outb(CPUState
*env
, int addr
, int val
)
416 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
417 ioport_write(0, addr
, val
);
420 env
->last_io_time
= cpu_get_time_fast();
424 void cpu_outw(CPUState
*env
, int addr
, int val
)
426 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
427 ioport_write(1, addr
, val
);
430 env
->last_io_time
= cpu_get_time_fast();
434 void cpu_outl(CPUState
*env
, int addr
, int val
)
436 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
437 ioport_write(2, addr
, val
);
440 env
->last_io_time
= cpu_get_time_fast();
444 int cpu_inb(CPUState
*env
, int addr
)
447 val
= ioport_read(0, addr
);
448 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
451 env
->last_io_time
= cpu_get_time_fast();
456 int cpu_inw(CPUState
*env
, int addr
)
459 val
= ioport_read(1, addr
);
460 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
463 env
->last_io_time
= cpu_get_time_fast();
468 int cpu_inl(CPUState
*env
, int addr
)
471 val
= ioport_read(2, addr
);
472 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
475 env
->last_io_time
= cpu_get_time_fast();
480 /***********************************************************/
481 void hw_error(const char *fmt
, ...)
487 fprintf(stderr
, "qemu: hardware error: ");
488 vfprintf(stderr
, fmt
, ap
);
489 fprintf(stderr
, "\n");
490 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
491 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
493 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
495 cpu_dump_state(env
, stderr
, fprintf
, 0);
505 static QEMUBalloonEvent
*qemu_balloon_event
;
506 void *qemu_balloon_event_opaque
;
508 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
510 qemu_balloon_event
= func
;
511 qemu_balloon_event_opaque
= opaque
;
514 void qemu_balloon(ram_addr_t target
)
516 if (qemu_balloon_event
)
517 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
520 ram_addr_t
qemu_balloon_status(void)
522 if (qemu_balloon_event
)
523 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
527 /***********************************************************/
530 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
531 static void *qemu_put_kbd_event_opaque
;
532 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
533 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
535 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
537 qemu_put_kbd_event_opaque
= opaque
;
538 qemu_put_kbd_event
= func
;
541 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
542 void *opaque
, int absolute
,
545 QEMUPutMouseEntry
*s
, *cursor
;
547 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
549 s
->qemu_put_mouse_event
= func
;
550 s
->qemu_put_mouse_event_opaque
= opaque
;
551 s
->qemu_put_mouse_event_absolute
= absolute
;
552 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
555 if (!qemu_put_mouse_event_head
) {
556 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
560 cursor
= qemu_put_mouse_event_head
;
561 while (cursor
->next
!= NULL
)
562 cursor
= cursor
->next
;
565 qemu_put_mouse_event_current
= s
;
570 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
572 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
574 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
577 cursor
= qemu_put_mouse_event_head
;
578 while (cursor
!= NULL
&& cursor
!= entry
) {
580 cursor
= cursor
->next
;
583 if (cursor
== NULL
) // does not exist or list empty
585 else if (prev
== NULL
) { // entry is head
586 qemu_put_mouse_event_head
= cursor
->next
;
587 if (qemu_put_mouse_event_current
== entry
)
588 qemu_put_mouse_event_current
= cursor
->next
;
589 qemu_free(entry
->qemu_put_mouse_event_name
);
594 prev
->next
= entry
->next
;
596 if (qemu_put_mouse_event_current
== entry
)
597 qemu_put_mouse_event_current
= prev
;
599 qemu_free(entry
->qemu_put_mouse_event_name
);
603 void kbd_put_keycode(int keycode
)
605 if (qemu_put_kbd_event
) {
606 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
610 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
612 QEMUPutMouseEvent
*mouse_event
;
613 void *mouse_event_opaque
;
616 if (!qemu_put_mouse_event_current
) {
621 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
623 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
626 if (graphic_rotate
) {
627 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
630 width
= graphic_width
- 1;
631 mouse_event(mouse_event_opaque
,
632 width
- dy
, dx
, dz
, buttons_state
);
634 mouse_event(mouse_event_opaque
,
635 dx
, dy
, dz
, buttons_state
);
639 int kbd_mouse_is_absolute(void)
641 if (!qemu_put_mouse_event_current
)
644 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
647 void do_info_mice(void)
649 QEMUPutMouseEntry
*cursor
;
652 if (!qemu_put_mouse_event_head
) {
653 term_printf("No mouse devices connected\n");
657 term_printf("Mouse devices available:\n");
658 cursor
= qemu_put_mouse_event_head
;
659 while (cursor
!= NULL
) {
660 term_printf("%c Mouse #%d: %s\n",
661 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
662 index
, cursor
->qemu_put_mouse_event_name
);
664 cursor
= cursor
->next
;
668 void do_mouse_set(int index
)
670 QEMUPutMouseEntry
*cursor
;
673 if (!qemu_put_mouse_event_head
) {
674 term_printf("No mouse devices connected\n");
678 cursor
= qemu_put_mouse_event_head
;
679 while (cursor
!= NULL
&& index
!= i
) {
681 cursor
= cursor
->next
;
685 qemu_put_mouse_event_current
= cursor
;
687 term_printf("Mouse at given index not found\n");
690 /* compute with 96 bit intermediate result: (a*b)/c */
691 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
696 #ifdef WORDS_BIGENDIAN
706 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
707 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
710 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
714 /***********************************************************/
715 /* real time host monotonic timer */
717 #define QEMU_TIMER_BASE 1000000000LL
721 static int64_t clock_freq
;
723 static void init_get_clock(void)
727 ret
= QueryPerformanceFrequency(&freq
);
729 fprintf(stderr
, "Could not calibrate ticks\n");
732 clock_freq
= freq
.QuadPart
;
735 static int64_t get_clock(void)
738 QueryPerformanceCounter(&ti
);
739 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
744 static int use_rt_clock
;
746 static void init_get_clock(void)
749 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
752 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
759 static int64_t get_clock(void)
761 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
764 clock_gettime(CLOCK_MONOTONIC
, &ts
);
765 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
769 /* XXX: using gettimeofday leads to problems if the date
770 changes, so it should be avoided. */
772 gettimeofday(&tv
, NULL
);
773 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
778 /* Return the virtual CPU time, based on the instruction counter. */
779 static int64_t cpu_get_icount(void)
782 CPUState
*env
= cpu_single_env
;;
783 icount
= qemu_icount
;
786 fprintf(stderr
, "Bad clock read\n");
787 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
789 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
792 /***********************************************************/
793 /* guest cycle counter */
795 static int64_t cpu_ticks_prev
;
796 static int64_t cpu_ticks_offset
;
797 static int64_t cpu_clock_offset
;
798 static int cpu_ticks_enabled
;
800 /* return the host CPU cycle counter and handle stop/restart */
801 int64_t cpu_get_ticks(void)
804 return cpu_get_icount();
806 if (!cpu_ticks_enabled
) {
807 return cpu_ticks_offset
;
810 ticks
= cpu_get_real_ticks();
811 if (cpu_ticks_prev
> ticks
) {
812 /* Note: non increasing ticks may happen if the host uses
814 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
816 cpu_ticks_prev
= ticks
;
817 return ticks
+ cpu_ticks_offset
;
821 /* return the host CPU monotonic timer and handle stop/restart */
822 static int64_t cpu_get_clock(void)
825 if (!cpu_ticks_enabled
) {
826 return cpu_clock_offset
;
829 return ti
+ cpu_clock_offset
;
833 /* enable cpu_get_ticks() */
834 void cpu_enable_ticks(void)
836 if (!cpu_ticks_enabled
) {
837 cpu_ticks_offset
-= cpu_get_real_ticks();
838 cpu_clock_offset
-= get_clock();
839 cpu_ticks_enabled
= 1;
843 /* disable cpu_get_ticks() : the clock is stopped. You must not call
844 cpu_get_ticks() after that. */
845 void cpu_disable_ticks(void)
847 if (cpu_ticks_enabled
) {
848 cpu_ticks_offset
= cpu_get_ticks();
849 cpu_clock_offset
= cpu_get_clock();
850 cpu_ticks_enabled
= 0;
854 /***********************************************************/
857 #define QEMU_TIMER_REALTIME 0
858 #define QEMU_TIMER_VIRTUAL 1
862 /* XXX: add frequency */
870 struct QEMUTimer
*next
;
873 struct qemu_alarm_timer
{
877 int (*start
)(struct qemu_alarm_timer
*t
);
878 void (*stop
)(struct qemu_alarm_timer
*t
);
879 void (*rearm
)(struct qemu_alarm_timer
*t
);
883 #define ALARM_FLAG_DYNTICKS 0x1
884 #define ALARM_FLAG_EXPIRED 0x2
886 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
888 return t
->flags
& ALARM_FLAG_DYNTICKS
;
891 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
893 if (!alarm_has_dynticks(t
))
899 /* TODO: MIN_TIMER_REARM_US should be optimized */
900 #define MIN_TIMER_REARM_US 250
902 static struct qemu_alarm_timer
*alarm_timer
;
904 static int alarm_timer_rfd
, alarm_timer_wfd
;
909 struct qemu_alarm_win32
{
913 } alarm_win32_data
= {0, NULL
, -1};
915 static int win32_start_timer(struct qemu_alarm_timer
*t
);
916 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
917 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
921 static int unix_start_timer(struct qemu_alarm_timer
*t
);
922 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
926 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
927 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
928 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
930 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
931 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
933 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
934 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
936 #endif /* __linux__ */
940 /* Correlation between real and virtual time is always going to be
941 fairly approximate, so ignore small variation.
942 When the guest is idle real and virtual time will be aligned in
944 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
946 static void icount_adjust(void)
951 static int64_t last_delta
;
952 /* If the VM is not running, then do nothing. */
956 cur_time
= cpu_get_clock();
957 cur_icount
= qemu_get_clock(vm_clock
);
958 delta
= cur_icount
- cur_time
;
959 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
961 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
962 && icount_time_shift
> 0) {
963 /* The guest is getting too far ahead. Slow time down. */
967 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
968 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
969 /* The guest is getting too far behind. Speed time up. */
973 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
976 static void icount_adjust_rt(void * opaque
)
978 qemu_mod_timer(icount_rt_timer
,
979 qemu_get_clock(rt_clock
) + 1000);
983 static void icount_adjust_vm(void * opaque
)
985 qemu_mod_timer(icount_vm_timer
,
986 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
990 static void init_icount_adjust(void)
992 /* Have both realtime and virtual time triggers for speed adjustment.
993 The realtime trigger catches emulated time passing too slowly,
994 the virtual time trigger catches emulated time passing too fast.
995 Realtime triggers occur even when idle, so use them less frequently
997 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
998 qemu_mod_timer(icount_rt_timer
,
999 qemu_get_clock(rt_clock
) + 1000);
1000 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1001 qemu_mod_timer(icount_vm_timer
,
1002 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1005 static struct qemu_alarm_timer alarm_timers
[] = {
1008 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1009 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1010 /* HPET - if available - is preferred */
1011 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1012 /* ...otherwise try RTC */
1013 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1015 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1017 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1018 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1019 {"win32", 0, win32_start_timer
,
1020 win32_stop_timer
, NULL
, &alarm_win32_data
},
1025 static void show_available_alarms(void)
1029 printf("Available alarm timers, in order of precedence:\n");
1030 for (i
= 0; alarm_timers
[i
].name
; i
++)
1031 printf("%s\n", alarm_timers
[i
].name
);
1034 static void configure_alarms(char const *opt
)
1038 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1041 struct qemu_alarm_timer tmp
;
1043 if (!strcmp(opt
, "?")) {
1044 show_available_alarms();
1050 /* Reorder the array */
1051 name
= strtok(arg
, ",");
1053 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1054 if (!strcmp(alarm_timers
[i
].name
, name
))
1059 fprintf(stderr
, "Unknown clock %s\n", name
);
1068 tmp
= alarm_timers
[i
];
1069 alarm_timers
[i
] = alarm_timers
[cur
];
1070 alarm_timers
[cur
] = tmp
;
1074 name
= strtok(NULL
, ",");
1080 /* Disable remaining timers */
1081 for (i
= cur
; i
< count
; i
++)
1082 alarm_timers
[i
].name
= NULL
;
1084 show_available_alarms();
1089 QEMUClock
*rt_clock
;
1090 QEMUClock
*vm_clock
;
1092 static QEMUTimer
*active_timers
[2];
1094 static QEMUClock
*qemu_new_clock(int type
)
1097 clock
= qemu_mallocz(sizeof(QEMUClock
));
1102 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1106 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1109 ts
->opaque
= opaque
;
1113 void qemu_free_timer(QEMUTimer
*ts
)
1118 /* stop a timer, but do not dealloc it */
1119 void qemu_del_timer(QEMUTimer
*ts
)
1123 /* NOTE: this code must be signal safe because
1124 qemu_timer_expired() can be called from a signal. */
1125 pt
= &active_timers
[ts
->clock
->type
];
1138 /* modify the current timer so that it will be fired when current_time
1139 >= expire_time. The corresponding callback will be called. */
1140 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1146 /* add the timer in the sorted list */
1147 /* NOTE: this code must be signal safe because
1148 qemu_timer_expired() can be called from a signal. */
1149 pt
= &active_timers
[ts
->clock
->type
];
1154 if (t
->expire_time
> expire_time
)
1158 ts
->expire_time
= expire_time
;
1162 /* Rearm if necessary */
1163 if (pt
== &active_timers
[ts
->clock
->type
]) {
1164 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1165 qemu_rearm_alarm_timer(alarm_timer
);
1167 /* Interrupt execution to force deadline recalculation. */
1168 if (use_icount
&& cpu_single_env
) {
1169 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1174 int qemu_timer_pending(QEMUTimer
*ts
)
1177 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1184 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1188 return (timer_head
->expire_time
<= current_time
);
1191 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1197 if (!ts
|| ts
->expire_time
> current_time
)
1199 /* remove timer from the list before calling the callback */
1200 *ptimer_head
= ts
->next
;
1203 /* run the callback (the timer list can be modified) */
1208 int64_t qemu_get_clock(QEMUClock
*clock
)
1210 switch(clock
->type
) {
1211 case QEMU_TIMER_REALTIME
:
1212 return get_clock() / 1000000;
1214 case QEMU_TIMER_VIRTUAL
:
1216 return cpu_get_icount();
1218 return cpu_get_clock();
1223 static void init_timers(void)
1226 ticks_per_sec
= QEMU_TIMER_BASE
;
1227 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1228 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1232 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1234 uint64_t expire_time
;
1236 if (qemu_timer_pending(ts
)) {
1237 expire_time
= ts
->expire_time
;
1241 qemu_put_be64(f
, expire_time
);
1244 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1246 uint64_t expire_time
;
1248 expire_time
= qemu_get_be64(f
);
1249 if (expire_time
!= -1) {
1250 qemu_mod_timer(ts
, expire_time
);
1256 static void timer_save(QEMUFile
*f
, void *opaque
)
1258 if (cpu_ticks_enabled
) {
1259 hw_error("cannot save state if virtual timers are running");
1261 qemu_put_be64(f
, cpu_ticks_offset
);
1262 qemu_put_be64(f
, ticks_per_sec
);
1263 qemu_put_be64(f
, cpu_clock_offset
);
1266 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1268 if (version_id
!= 1 && version_id
!= 2)
1270 if (cpu_ticks_enabled
) {
1273 cpu_ticks_offset
=qemu_get_be64(f
);
1274 ticks_per_sec
=qemu_get_be64(f
);
1275 if (version_id
== 2) {
1276 cpu_clock_offset
=qemu_get_be64(f
);
1282 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1283 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1285 static void host_alarm_handler(int host_signum
)
1289 #define DISP_FREQ 1000
1291 static int64_t delta_min
= INT64_MAX
;
1292 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1294 ti
= qemu_get_clock(vm_clock
);
1295 if (last_clock
!= 0) {
1296 delta
= ti
- last_clock
;
1297 if (delta
< delta_min
)
1299 if (delta
> delta_max
)
1302 if (++count
== DISP_FREQ
) {
1303 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1304 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1305 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1306 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1307 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1309 delta_min
= INT64_MAX
;
1317 if (alarm_has_dynticks(alarm_timer
) ||
1319 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1320 qemu_get_clock(vm_clock
))) ||
1321 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1322 qemu_get_clock(rt_clock
))) {
1323 CPUState
*env
= next_cpu
;
1326 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1327 SetEvent(data
->host_alarm
);
1329 static const char byte
= 0;
1330 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1332 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1335 /* stop the currently executing cpu because a timer occured */
1336 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1338 if (env
->kqemu_enabled
) {
1339 kqemu_cpu_interrupt(env
);
1347 static int64_t qemu_next_deadline(void)
1351 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1352 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1353 qemu_get_clock(vm_clock
);
1355 /* To avoid problems with overflow limit this to 2^32. */
1365 #if defined(__linux__) || defined(_WIN32)
1366 static uint64_t qemu_next_deadline_dyntick(void)
1374 delta
= (qemu_next_deadline() + 999) / 1000;
1376 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1377 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1378 qemu_get_clock(rt_clock
))*1000;
1379 if (rtdelta
< delta
)
1383 if (delta
< MIN_TIMER_REARM_US
)
1384 delta
= MIN_TIMER_REARM_US
;
1392 /* Sets a specific flag */
1393 static int fcntl_setfl(int fd
, int flag
)
1397 flags
= fcntl(fd
, F_GETFL
);
1401 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1407 #if defined(__linux__)
1409 #define RTC_FREQ 1024
1411 static void enable_sigio_timer(int fd
)
1413 struct sigaction act
;
1416 sigfillset(&act
.sa_mask
);
1418 act
.sa_handler
= host_alarm_handler
;
1420 sigaction(SIGIO
, &act
, NULL
);
1421 fcntl_setfl(fd
, O_ASYNC
);
1422 fcntl(fd
, F_SETOWN
, getpid());
1425 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1427 struct hpet_info info
;
1430 fd
= open("/dev/hpet", O_RDONLY
);
1435 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1437 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1438 "error, but for better emulation accuracy type:\n"
1439 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1443 /* Check capabilities */
1444 r
= ioctl(fd
, HPET_INFO
, &info
);
1448 /* Enable periodic mode */
1449 r
= ioctl(fd
, HPET_EPI
, 0);
1450 if (info
.hi_flags
&& (r
< 0))
1453 /* Enable interrupt */
1454 r
= ioctl(fd
, HPET_IE_ON
, 0);
1458 enable_sigio_timer(fd
);
1459 t
->priv
= (void *)(long)fd
;
1467 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1469 int fd
= (long)t
->priv
;
1474 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1477 unsigned long current_rtc_freq
= 0;
1479 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1482 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1483 if (current_rtc_freq
!= RTC_FREQ
&&
1484 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1485 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1486 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1487 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1490 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1496 enable_sigio_timer(rtc_fd
);
1498 t
->priv
= (void *)(long)rtc_fd
;
1503 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1505 int rtc_fd
= (long)t
->priv
;
1510 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1514 struct sigaction act
;
1516 sigfillset(&act
.sa_mask
);
1518 act
.sa_handler
= host_alarm_handler
;
1520 sigaction(SIGALRM
, &act
, NULL
);
1522 ev
.sigev_value
.sival_int
= 0;
1523 ev
.sigev_notify
= SIGEV_SIGNAL
;
1524 ev
.sigev_signo
= SIGALRM
;
1526 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1527 perror("timer_create");
1529 /* disable dynticks */
1530 fprintf(stderr
, "Dynamic Ticks disabled\n");
1535 t
->priv
= (void *)(long)host_timer
;
1540 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1542 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1544 timer_delete(host_timer
);
1547 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1549 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1550 struct itimerspec timeout
;
1551 int64_t nearest_delta_us
= INT64_MAX
;
1554 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1555 !active_timers
[QEMU_TIMER_VIRTUAL
])
1558 nearest_delta_us
= qemu_next_deadline_dyntick();
1560 /* check whether a timer is already running */
1561 if (timer_gettime(host_timer
, &timeout
)) {
1563 fprintf(stderr
, "Internal timer error: aborting\n");
1566 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1567 if (current_us
&& current_us
<= nearest_delta_us
)
1570 timeout
.it_interval
.tv_sec
= 0;
1571 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1572 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1573 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1574 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1576 fprintf(stderr
, "Internal timer error: aborting\n");
1581 #endif /* defined(__linux__) */
1583 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1585 struct sigaction act
;
1586 struct itimerval itv
;
1590 sigfillset(&act
.sa_mask
);
1592 act
.sa_handler
= host_alarm_handler
;
1594 sigaction(SIGALRM
, &act
, NULL
);
1596 itv
.it_interval
.tv_sec
= 0;
1597 /* for i386 kernel 2.6 to get 1 ms */
1598 itv
.it_interval
.tv_usec
= 999;
1599 itv
.it_value
.tv_sec
= 0;
1600 itv
.it_value
.tv_usec
= 10 * 1000;
1602 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1609 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1611 struct itimerval itv
;
1613 memset(&itv
, 0, sizeof(itv
));
1614 setitimer(ITIMER_REAL
, &itv
, NULL
);
1617 #endif /* !defined(_WIN32) */
1619 static void try_to_rearm_timer(void *opaque
)
1621 struct qemu_alarm_timer
*t
= opaque
;
1625 /* Drain the notify pipe */
1628 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1629 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1632 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1633 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1634 qemu_rearm_alarm_timer(alarm_timer
);
1640 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1643 struct qemu_alarm_win32
*data
= t
->priv
;
1646 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1647 if (!data
->host_alarm
) {
1648 perror("Failed CreateEvent");
1652 memset(&tc
, 0, sizeof(tc
));
1653 timeGetDevCaps(&tc
, sizeof(tc
));
1655 if (data
->period
< tc
.wPeriodMin
)
1656 data
->period
= tc
.wPeriodMin
;
1658 timeBeginPeriod(data
->period
);
1660 flags
= TIME_CALLBACK_FUNCTION
;
1661 if (alarm_has_dynticks(t
))
1662 flags
|= TIME_ONESHOT
;
1664 flags
|= TIME_PERIODIC
;
1666 data
->timerId
= timeSetEvent(1, // interval (ms)
1667 data
->period
, // resolution
1668 host_alarm_handler
, // function
1669 (DWORD
)t
, // parameter
1672 if (!data
->timerId
) {
1673 perror("Failed to initialize win32 alarm timer");
1675 timeEndPeriod(data
->period
);
1676 CloseHandle(data
->host_alarm
);
1680 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1685 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1687 struct qemu_alarm_win32
*data
= t
->priv
;
1689 timeKillEvent(data
->timerId
);
1690 timeEndPeriod(data
->period
);
1692 CloseHandle(data
->host_alarm
);
1695 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1697 struct qemu_alarm_win32
*data
= t
->priv
;
1698 uint64_t nearest_delta_us
;
1700 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1701 !active_timers
[QEMU_TIMER_VIRTUAL
])
1704 nearest_delta_us
= qemu_next_deadline_dyntick();
1705 nearest_delta_us
/= 1000;
1707 timeKillEvent(data
->timerId
);
1709 data
->timerId
= timeSetEvent(1,
1713 TIME_ONESHOT
| TIME_PERIODIC
);
1715 if (!data
->timerId
) {
1716 perror("Failed to re-arm win32 alarm timer");
1718 timeEndPeriod(data
->period
);
1719 CloseHandle(data
->host_alarm
);
1726 static int init_timer_alarm(void)
1728 struct qemu_alarm_timer
*t
= NULL
;
1738 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1742 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1746 alarm_timer_rfd
= fds
[0];
1747 alarm_timer_wfd
= fds
[1];
1750 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1751 t
= &alarm_timers
[i
];
1764 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1765 try_to_rearm_timer
, NULL
, t
);
1780 static void quit_timers(void)
1782 alarm_timer
->stop(alarm_timer
);
1786 /***********************************************************/
1787 /* host time/date access */
1788 void qemu_get_timedate(struct tm
*tm
, int offset
)
1795 if (rtc_date_offset
== -1) {
1799 ret
= localtime(&ti
);
1801 ti
-= rtc_date_offset
;
1805 memcpy(tm
, ret
, sizeof(struct tm
));
1808 int qemu_timedate_diff(struct tm
*tm
)
1812 if (rtc_date_offset
== -1)
1814 seconds
= mktimegm(tm
);
1816 seconds
= mktime(tm
);
1818 seconds
= mktimegm(tm
) + rtc_date_offset
;
1820 return seconds
- time(NULL
);
1824 static void socket_cleanup(void)
1829 static int socket_init(void)
1834 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1836 err
= WSAGetLastError();
1837 fprintf(stderr
, "WSAStartup: %d\n", err
);
1840 atexit(socket_cleanup
);
1845 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1850 while (*p
!= '\0' && *p
!= '=') {
1851 if (q
&& (q
- buf
) < buf_size
- 1)
1861 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1866 while (*p
!= '\0') {
1868 if (*(p
+ 1) != ',')
1872 if (q
&& (q
- buf
) < buf_size
- 1)
1882 int get_param_value(char *buf
, int buf_size
,
1883 const char *tag
, const char *str
)
1890 p
= get_opt_name(option
, sizeof(option
), p
);
1894 if (!strcmp(tag
, option
)) {
1895 (void)get_opt_value(buf
, buf_size
, p
);
1898 p
= get_opt_value(NULL
, 0, p
);
1907 int check_params(char *buf
, int buf_size
,
1908 const char * const *params
, const char *str
)
1915 p
= get_opt_name(buf
, buf_size
, p
);
1919 for(i
= 0; params
[i
] != NULL
; i
++)
1920 if (!strcmp(params
[i
], buf
))
1922 if (params
[i
] == NULL
)
1924 p
= get_opt_value(NULL
, 0, p
);
1932 /***********************************************************/
1933 /* Bluetooth support */
1936 static struct HCIInfo
*hci_table
[MAX_NICS
];
1938 static struct bt_vlan_s
{
1939 struct bt_scatternet_s net
;
1941 struct bt_vlan_s
*next
;
1944 /* find or alloc a new bluetooth "VLAN" */
1945 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1947 struct bt_vlan_s
**pvlan
, *vlan
;
1948 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1952 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1954 pvlan
= &first_bt_vlan
;
1955 while (*pvlan
!= NULL
)
1956 pvlan
= &(*pvlan
)->next
;
1961 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1965 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1970 static struct HCIInfo null_hci
= {
1971 .cmd_send
= null_hci_send
,
1972 .sco_send
= null_hci_send
,
1973 .acl_send
= null_hci_send
,
1974 .bdaddr_set
= null_hci_addr_set
,
1977 struct HCIInfo
*qemu_next_hci(void)
1979 if (cur_hci
== nb_hcis
)
1982 return hci_table
[cur_hci
++];
1985 static struct HCIInfo
*hci_init(const char *str
)
1988 struct bt_scatternet_s
*vlan
= 0;
1990 if (!strcmp(str
, "null"))
1993 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1995 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1996 else if (!strncmp(str
, "hci", 3)) {
1999 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2000 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2005 vlan
= qemu_find_bt_vlan(0);
2007 return bt_new_hci(vlan
);
2010 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2015 static int bt_hci_parse(const char *str
)
2017 struct HCIInfo
*hci
;
2020 if (nb_hcis
>= MAX_NICS
) {
2021 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2025 hci
= hci_init(str
);
2034 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2035 hci
->bdaddr_set(hci
, bdaddr
.b
);
2037 hci_table
[nb_hcis
++] = hci
;
2042 static void bt_vhci_add(int vlan_id
)
2044 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2047 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2048 "an empty scatternet %i\n", vlan_id
);
2050 bt_vhci_init(bt_new_hci(vlan
));
2053 static struct bt_device_s
*bt_device_add(const char *opt
)
2055 struct bt_scatternet_s
*vlan
;
2057 char *endp
= strstr(opt
, ",vlan=");
2058 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2061 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2064 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2066 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2071 vlan
= qemu_find_bt_vlan(vlan_id
);
2074 fprintf(stderr
, "qemu: warning: adding a slave device to "
2075 "an empty scatternet %i\n", vlan_id
);
2077 if (!strcmp(devname
, "keyboard"))
2078 return bt_keyboard_init(vlan
);
2080 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2084 static int bt_parse(const char *opt
)
2086 const char *endp
, *p
;
2089 if (strstart(opt
, "hci", &endp
)) {
2090 if (!*endp
|| *endp
== ',') {
2092 if (!strstart(endp
, ",vlan=", 0))
2095 return bt_hci_parse(opt
);
2097 } else if (strstart(opt
, "vhci", &endp
)) {
2098 if (!*endp
|| *endp
== ',') {
2100 if (strstart(endp
, ",vlan=", &p
)) {
2101 vlan
= strtol(p
, (char **) &endp
, 0);
2103 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2107 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2116 } else if (strstart(opt
, "device:", &endp
))
2117 return !bt_device_add(endp
);
2119 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2123 /***********************************************************/
2124 /* QEMU Block devices */
2126 #define HD_ALIAS "index=%d,media=disk"
2128 #define CDROM_ALIAS "index=1,media=cdrom"
2130 #define CDROM_ALIAS "index=2,media=cdrom"
2132 #define FD_ALIAS "index=%d,if=floppy"
2133 #define PFLASH_ALIAS "if=pflash"
2134 #define MTD_ALIAS "if=mtd"
2135 #define SD_ALIAS "index=0,if=sd"
2137 static int drive_opt_get_free_idx(void)
2141 for (index
= 0; index
< MAX_DRIVES
; index
++)
2142 if (!drives_opt
[index
].used
) {
2143 drives_opt
[index
].used
= 1;
2150 static int drive_get_free_idx(void)
2154 for (index
= 0; index
< MAX_DRIVES
; index
++)
2155 if (!drives_table
[index
].used
) {
2156 drives_table
[index
].used
= 1;
2163 int drive_add(const char *file
, const char *fmt
, ...)
2166 int index
= drive_opt_get_free_idx();
2168 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2169 fprintf(stderr
, "qemu: too many drives\n");
2173 drives_opt
[index
].file
= file
;
2175 vsnprintf(drives_opt
[index
].opt
,
2176 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2183 void drive_remove(int index
)
2185 drives_opt
[index
].used
= 0;
2189 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2193 /* seek interface, bus and unit */
2195 for (index
= 0; index
< MAX_DRIVES
; index
++)
2196 if (drives_table
[index
].type
== type
&&
2197 drives_table
[index
].bus
== bus
&&
2198 drives_table
[index
].unit
== unit
&&
2199 drives_table
[index
].used
)
2205 int drive_get_max_bus(BlockInterfaceType type
)
2211 for (index
= 0; index
< nb_drives
; index
++) {
2212 if(drives_table
[index
].type
== type
&&
2213 drives_table
[index
].bus
> max_bus
)
2214 max_bus
= drives_table
[index
].bus
;
2219 const char *drive_get_serial(BlockDriverState
*bdrv
)
2223 for (index
= 0; index
< nb_drives
; index
++)
2224 if (drives_table
[index
].bdrv
== bdrv
)
2225 return drives_table
[index
].serial
;
2230 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2234 for (index
= 0; index
< nb_drives
; index
++)
2235 if (drives_table
[index
].bdrv
== bdrv
)
2236 return drives_table
[index
].onerror
;
2238 return BLOCK_ERR_REPORT
;
2241 static void bdrv_format_print(void *opaque
, const char *name
)
2243 fprintf(stderr
, " %s", name
);
2246 void drive_uninit(BlockDriverState
*bdrv
)
2250 for (i
= 0; i
< MAX_DRIVES
; i
++)
2251 if (drives_table
[i
].bdrv
== bdrv
) {
2252 drives_table
[i
].bdrv
= NULL
;
2253 drives_table
[i
].used
= 0;
2254 drive_remove(drives_table
[i
].drive_opt_idx
);
2260 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2266 const char *mediastr
= "";
2267 BlockInterfaceType type
;
2268 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2269 int bus_id
, unit_id
;
2270 int cyls
, heads
, secs
, translation
;
2271 BlockDriverState
*bdrv
;
2272 BlockDriver
*drv
= NULL
;
2273 QEMUMachine
*machine
= opaque
;
2277 int bdrv_flags
, onerror
;
2278 int drives_table_idx
;
2279 char *str
= arg
->opt
;
2280 static const char * const params
[] = { "bus", "unit", "if", "index",
2281 "cyls", "heads", "secs", "trans",
2282 "media", "snapshot", "file",
2283 "cache", "format", "serial", "werror",
2286 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2287 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2293 cyls
= heads
= secs
= 0;
2296 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2300 if (machine
->use_scsi
) {
2302 max_devs
= MAX_SCSI_DEVS
;
2303 pstrcpy(devname
, sizeof(devname
), "scsi");
2306 max_devs
= MAX_IDE_DEVS
;
2307 pstrcpy(devname
, sizeof(devname
), "ide");
2311 /* extract parameters */
2313 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2314 bus_id
= strtol(buf
, NULL
, 0);
2316 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2321 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2322 unit_id
= strtol(buf
, NULL
, 0);
2324 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2329 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2330 pstrcpy(devname
, sizeof(devname
), buf
);
2331 if (!strcmp(buf
, "ide")) {
2333 max_devs
= MAX_IDE_DEVS
;
2334 } else if (!strcmp(buf
, "scsi")) {
2336 max_devs
= MAX_SCSI_DEVS
;
2337 } else if (!strcmp(buf
, "floppy")) {
2340 } else if (!strcmp(buf
, "pflash")) {
2343 } else if (!strcmp(buf
, "mtd")) {
2346 } else if (!strcmp(buf
, "sd")) {
2349 } else if (!strcmp(buf
, "virtio")) {
2353 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2358 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2359 index
= strtol(buf
, NULL
, 0);
2361 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2366 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2367 cyls
= strtol(buf
, NULL
, 0);
2370 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2371 heads
= strtol(buf
, NULL
, 0);
2374 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2375 secs
= strtol(buf
, NULL
, 0);
2378 if (cyls
|| heads
|| secs
) {
2379 if (cyls
< 1 || cyls
> 16383) {
2380 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2383 if (heads
< 1 || heads
> 16) {
2384 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2387 if (secs
< 1 || secs
> 63) {
2388 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2393 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2396 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2400 if (!strcmp(buf
, "none"))
2401 translation
= BIOS_ATA_TRANSLATION_NONE
;
2402 else if (!strcmp(buf
, "lba"))
2403 translation
= BIOS_ATA_TRANSLATION_LBA
;
2404 else if (!strcmp(buf
, "auto"))
2405 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2407 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2412 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2413 if (!strcmp(buf
, "disk")) {
2415 } else if (!strcmp(buf
, "cdrom")) {
2416 if (cyls
|| secs
|| heads
) {
2418 "qemu: '%s' invalid physical CHS format\n", str
);
2421 media
= MEDIA_CDROM
;
2423 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2428 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2429 if (!strcmp(buf
, "on"))
2431 else if (!strcmp(buf
, "off"))
2434 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2439 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2440 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2442 else if (!strcmp(buf
, "writethrough"))
2444 else if (!strcmp(buf
, "writeback"))
2447 fprintf(stderr
, "qemu: invalid cache option\n");
2452 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2453 if (strcmp(buf
, "?") == 0) {
2454 fprintf(stderr
, "qemu: Supported formats:");
2455 bdrv_iterate_format(bdrv_format_print
, NULL
);
2456 fprintf(stderr
, "\n");
2459 drv
= bdrv_find_format(buf
);
2461 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2466 if (arg
->file
== NULL
)
2467 get_param_value(file
, sizeof(file
), "file", str
);
2469 pstrcpy(file
, sizeof(file
), arg
->file
);
2471 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2472 memset(serial
, 0, sizeof(serial
));
2474 onerror
= BLOCK_ERR_REPORT
;
2475 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2476 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2477 fprintf(stderr
, "werror is no supported by this format\n");
2480 if (!strcmp(buf
, "ignore"))
2481 onerror
= BLOCK_ERR_IGNORE
;
2482 else if (!strcmp(buf
, "enospc"))
2483 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2484 else if (!strcmp(buf
, "stop"))
2485 onerror
= BLOCK_ERR_STOP_ANY
;
2486 else if (!strcmp(buf
, "report"))
2487 onerror
= BLOCK_ERR_REPORT
;
2489 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2494 /* compute bus and unit according index */
2497 if (bus_id
!= 0 || unit_id
!= -1) {
2499 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2507 unit_id
= index
% max_devs
;
2508 bus_id
= index
/ max_devs
;
2512 /* if user doesn't specify a unit_id,
2513 * try to find the first free
2516 if (unit_id
== -1) {
2518 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2520 if (max_devs
&& unit_id
>= max_devs
) {
2521 unit_id
-= max_devs
;
2529 if (max_devs
&& unit_id
>= max_devs
) {
2530 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2531 str
, unit_id
, max_devs
- 1);
2536 * ignore multiple definitions
2539 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2544 if (type
== IF_IDE
|| type
== IF_SCSI
)
2545 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2547 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2548 devname
, bus_id
, mediastr
, unit_id
);
2550 snprintf(buf
, sizeof(buf
), "%s%s%i",
2551 devname
, mediastr
, unit_id
);
2552 bdrv
= bdrv_new(buf
);
2553 drives_table_idx
= drive_get_free_idx();
2554 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2555 drives_table
[drives_table_idx
].type
= type
;
2556 drives_table
[drives_table_idx
].bus
= bus_id
;
2557 drives_table
[drives_table_idx
].unit
= unit_id
;
2558 drives_table
[drives_table_idx
].onerror
= onerror
;
2559 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2560 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2569 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2570 bdrv_set_translation_hint(bdrv
, translation
);
2574 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2579 /* FIXME: This isn't really a floppy, but it's a reasonable
2582 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2593 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2594 cache
= 2; /* always use write-back with snapshot */
2596 if (cache
== 0) /* no caching */
2597 bdrv_flags
|= BDRV_O_NOCACHE
;
2598 else if (cache
== 2) /* write-back */
2599 bdrv_flags
|= BDRV_O_CACHE_WB
;
2600 else if (cache
== 3) /* not specified */
2601 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2602 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2603 fprintf(stderr
, "qemu: could not open disk image %s\n",
2607 return drives_table_idx
;
2610 /***********************************************************/
2613 static USBPort
*used_usb_ports
;
2614 static USBPort
*free_usb_ports
;
2616 /* ??? Maybe change this to register a hub to keep track of the topology. */
2617 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2618 usb_attachfn attach
)
2620 port
->opaque
= opaque
;
2621 port
->index
= index
;
2622 port
->attach
= attach
;
2623 port
->next
= free_usb_ports
;
2624 free_usb_ports
= port
;
2627 int usb_device_add_dev(USBDevice
*dev
)
2631 /* Find a USB port to add the device to. */
2632 port
= free_usb_ports
;
2636 /* Create a new hub and chain it on. */
2637 free_usb_ports
= NULL
;
2638 port
->next
= used_usb_ports
;
2639 used_usb_ports
= port
;
2641 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2642 usb_attach(port
, hub
);
2643 port
= free_usb_ports
;
2646 free_usb_ports
= port
->next
;
2647 port
->next
= used_usb_ports
;
2648 used_usb_ports
= port
;
2649 usb_attach(port
, dev
);
2653 static int usb_device_add(const char *devname
)
2658 if (!free_usb_ports
)
2661 if (strstart(devname
, "host:", &p
)) {
2662 dev
= usb_host_device_open(p
);
2663 } else if (!strcmp(devname
, "mouse")) {
2664 dev
= usb_mouse_init();
2665 } else if (!strcmp(devname
, "tablet")) {
2666 dev
= usb_tablet_init();
2667 } else if (!strcmp(devname
, "keyboard")) {
2668 dev
= usb_keyboard_init();
2669 } else if (strstart(devname
, "disk:", &p
)) {
2670 dev
= usb_msd_init(p
);
2671 } else if (!strcmp(devname
, "wacom-tablet")) {
2672 dev
= usb_wacom_init();
2673 } else if (strstart(devname
, "serial:", &p
)) {
2674 dev
= usb_serial_init(p
);
2675 #ifdef CONFIG_BRLAPI
2676 } else if (!strcmp(devname
, "braille")) {
2677 dev
= usb_baum_init();
2679 } else if (strstart(devname
, "net:", &p
)) {
2682 if (net_client_init("nic", p
) < 0)
2684 nd_table
[nic
].model
= "usb";
2685 dev
= usb_net_init(&nd_table
[nic
]);
2686 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2687 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2688 bt_new_hci(qemu_find_bt_vlan(0)));
2695 return usb_device_add_dev(dev
);
2698 int usb_device_del_addr(int bus_num
, int addr
)
2704 if (!used_usb_ports
)
2710 lastp
= &used_usb_ports
;
2711 port
= used_usb_ports
;
2712 while (port
&& port
->dev
->addr
!= addr
) {
2713 lastp
= &port
->next
;
2721 *lastp
= port
->next
;
2722 usb_attach(port
, NULL
);
2723 dev
->handle_destroy(dev
);
2724 port
->next
= free_usb_ports
;
2725 free_usb_ports
= port
;
2729 static int usb_device_del(const char *devname
)
2734 if (strstart(devname
, "host:", &p
))
2735 return usb_host_device_close(p
);
2737 if (!used_usb_ports
)
2740 p
= strchr(devname
, '.');
2743 bus_num
= strtoul(devname
, NULL
, 0);
2744 addr
= strtoul(p
+ 1, NULL
, 0);
2746 return usb_device_del_addr(bus_num
, addr
);
2749 void do_usb_add(const char *devname
)
2751 usb_device_add(devname
);
2754 void do_usb_del(const char *devname
)
2756 usb_device_del(devname
);
2763 const char *speed_str
;
2766 term_printf("USB support not enabled\n");
2770 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2774 switch(dev
->speed
) {
2778 case USB_SPEED_FULL
:
2781 case USB_SPEED_HIGH
:
2788 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2789 0, dev
->addr
, speed_str
, dev
->devname
);
2793 /***********************************************************/
2794 /* PCMCIA/Cardbus */
2796 static struct pcmcia_socket_entry_s
{
2797 struct pcmcia_socket_s
*socket
;
2798 struct pcmcia_socket_entry_s
*next
;
2799 } *pcmcia_sockets
= 0;
2801 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2803 struct pcmcia_socket_entry_s
*entry
;
2805 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2806 entry
->socket
= socket
;
2807 entry
->next
= pcmcia_sockets
;
2808 pcmcia_sockets
= entry
;
2811 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2813 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2815 ptr
= &pcmcia_sockets
;
2816 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2817 if (entry
->socket
== socket
) {
2823 void pcmcia_info(void)
2825 struct pcmcia_socket_entry_s
*iter
;
2826 if (!pcmcia_sockets
)
2827 term_printf("No PCMCIA sockets\n");
2829 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2830 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2831 iter
->socket
->attached
? iter
->socket
->card_string
:
2835 /***********************************************************/
2836 /* register display */
2838 void register_displaystate(DisplayState
*ds
)
2848 DisplayState
*get_displaystate(void)
2850 return display_state
;
2855 static void dumb_display_init(void)
2857 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2858 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2859 register_displaystate(ds
);
2862 /***********************************************************/
2865 #define MAX_IO_HANDLERS 64
2867 typedef struct IOHandlerRecord
{
2869 IOCanRWHandler
*fd_read_poll
;
2871 IOHandler
*fd_write
;
2874 /* temporary data */
2876 struct IOHandlerRecord
*next
;
2879 static IOHandlerRecord
*first_io_handler
;
2881 /* XXX: fd_read_poll should be suppressed, but an API change is
2882 necessary in the character devices to suppress fd_can_read(). */
2883 int qemu_set_fd_handler2(int fd
,
2884 IOCanRWHandler
*fd_read_poll
,
2886 IOHandler
*fd_write
,
2889 IOHandlerRecord
**pioh
, *ioh
;
2891 if (!fd_read
&& !fd_write
) {
2892 pioh
= &first_io_handler
;
2897 if (ioh
->fd
== fd
) {
2904 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2908 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2909 ioh
->next
= first_io_handler
;
2910 first_io_handler
= ioh
;
2913 ioh
->fd_read_poll
= fd_read_poll
;
2914 ioh
->fd_read
= fd_read
;
2915 ioh
->fd_write
= fd_write
;
2916 ioh
->opaque
= opaque
;
2922 int qemu_set_fd_handler(int fd
,
2924 IOHandler
*fd_write
,
2927 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2931 /***********************************************************/
2932 /* Polling handling */
2934 typedef struct PollingEntry
{
2937 struct PollingEntry
*next
;
2940 static PollingEntry
*first_polling_entry
;
2942 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2944 PollingEntry
**ppe
, *pe
;
2945 pe
= qemu_mallocz(sizeof(PollingEntry
));
2947 pe
->opaque
= opaque
;
2948 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2953 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2955 PollingEntry
**ppe
, *pe
;
2956 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2958 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2966 /***********************************************************/
2967 /* Wait objects support */
2968 typedef struct WaitObjects
{
2970 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2971 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2972 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2975 static WaitObjects wait_objects
= {0};
2977 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2979 WaitObjects
*w
= &wait_objects
;
2981 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2983 w
->events
[w
->num
] = handle
;
2984 w
->func
[w
->num
] = func
;
2985 w
->opaque
[w
->num
] = opaque
;
2990 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2993 WaitObjects
*w
= &wait_objects
;
2996 for (i
= 0; i
< w
->num
; i
++) {
2997 if (w
->events
[i
] == handle
)
3000 w
->events
[i
] = w
->events
[i
+ 1];
3001 w
->func
[i
] = w
->func
[i
+ 1];
3002 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3010 /***********************************************************/
3011 /* ram save/restore */
3013 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3017 v
= qemu_get_byte(f
);
3020 if (qemu_get_buffer(f
, buf
, len
) != len
)
3024 v
= qemu_get_byte(f
);
3025 memset(buf
, v
, len
);
3031 if (qemu_file_has_error(f
))
3037 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3042 if (qemu_get_be32(f
) != phys_ram_size
)
3044 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3045 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3052 #define BDRV_HASH_BLOCK_SIZE 1024
3053 #define IOBUF_SIZE 4096
3054 #define RAM_CBLOCK_MAGIC 0xfabe
3056 typedef struct RamDecompressState
{
3059 uint8_t buf
[IOBUF_SIZE
];
3060 } RamDecompressState
;
3062 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3065 memset(s
, 0, sizeof(*s
));
3067 ret
= inflateInit(&s
->zstream
);
3073 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3077 s
->zstream
.avail_out
= len
;
3078 s
->zstream
.next_out
= buf
;
3079 while (s
->zstream
.avail_out
> 0) {
3080 if (s
->zstream
.avail_in
== 0) {
3081 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3083 clen
= qemu_get_be16(s
->f
);
3084 if (clen
> IOBUF_SIZE
)
3086 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3087 s
->zstream
.avail_in
= clen
;
3088 s
->zstream
.next_in
= s
->buf
;
3090 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3091 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3098 static void ram_decompress_close(RamDecompressState
*s
)
3100 inflateEnd(&s
->zstream
);
3103 #define RAM_SAVE_FLAG_FULL 0x01
3104 #define RAM_SAVE_FLAG_COMPRESS 0x02
3105 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3106 #define RAM_SAVE_FLAG_PAGE 0x08
3107 #define RAM_SAVE_FLAG_EOS 0x10
3109 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3111 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3112 uint32_t *array
= (uint32_t *)page
;
3115 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3116 if (array
[i
] != val
)
3123 static int ram_save_block(QEMUFile
*f
)
3125 static ram_addr_t current_addr
= 0;
3126 ram_addr_t saved_addr
= current_addr
;
3127 ram_addr_t addr
= 0;
3130 while (addr
< phys_ram_size
) {
3131 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3134 cpu_physical_memory_reset_dirty(current_addr
,
3135 current_addr
+ TARGET_PAGE_SIZE
,
3136 MIGRATION_DIRTY_FLAG
);
3138 ch
= *(phys_ram_base
+ current_addr
);
3140 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3141 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3142 qemu_put_byte(f
, ch
);
3144 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3145 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3151 addr
+= TARGET_PAGE_SIZE
;
3152 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3158 static ram_addr_t ram_save_threshold
= 10;
3160 static ram_addr_t
ram_save_remaining(void)
3163 ram_addr_t count
= 0;
3165 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3166 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3173 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3178 /* Make sure all dirty bits are set */
3179 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3180 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3181 cpu_physical_memory_set_dirty(addr
);
3184 /* Enable dirty memory tracking */
3185 cpu_physical_memory_set_dirty_tracking(1);
3187 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3190 while (!qemu_file_rate_limit(f
)) {
3193 ret
= ram_save_block(f
);
3194 if (ret
== 0) /* no more blocks */
3198 /* try transferring iterative blocks of memory */
3201 cpu_physical_memory_set_dirty_tracking(0);
3203 /* flush all remaining blocks regardless of rate limiting */
3204 while (ram_save_block(f
) != 0);
3207 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3209 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3212 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3214 RamDecompressState s1
, *s
= &s1
;
3218 if (ram_decompress_open(s
, f
) < 0)
3220 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3221 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3222 fprintf(stderr
, "Error while reading ram block header\n");
3226 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3227 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3232 printf("Error block header\n");
3236 ram_decompress_close(s
);
3241 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3246 if (version_id
== 1)
3247 return ram_load_v1(f
, opaque
);
3249 if (version_id
== 2) {
3250 if (qemu_get_be32(f
) != phys_ram_size
)
3252 return ram_load_dead(f
, opaque
);
3255 if (version_id
!= 3)
3259 addr
= qemu_get_be64(f
);
3261 flags
= addr
& ~TARGET_PAGE_MASK
;
3262 addr
&= TARGET_PAGE_MASK
;
3264 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3265 if (addr
!= phys_ram_size
)
3269 if (flags
& RAM_SAVE_FLAG_FULL
) {
3270 if (ram_load_dead(f
, opaque
) < 0)
3274 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3275 uint8_t ch
= qemu_get_byte(f
);
3276 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3277 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3278 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3279 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3284 void qemu_service_io(void)
3286 CPUState
*env
= cpu_single_env
;
3288 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3290 if (env
->kqemu_enabled
) {
3291 kqemu_cpu_interrupt(env
);
3297 /***********************************************************/
3298 /* bottom halves (can be seen as timers which expire ASAP) */
3309 static QEMUBH
*first_bh
= NULL
;
3311 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3314 bh
= qemu_mallocz(sizeof(QEMUBH
));
3316 bh
->opaque
= opaque
;
3317 bh
->next
= first_bh
;
3322 int qemu_bh_poll(void)
3328 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3329 if (!bh
->deleted
&& bh
->scheduled
) {
3338 /* remove deleted bhs */
3352 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3360 void qemu_bh_schedule(QEMUBH
*bh
)
3362 CPUState
*env
= cpu_single_env
;
3367 /* stop the currently executing CPU to execute the BH ASAP */
3369 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3373 void qemu_bh_cancel(QEMUBH
*bh
)
3378 void qemu_bh_delete(QEMUBH
*bh
)
3384 static void qemu_bh_update_timeout(int *timeout
)
3388 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3389 if (!bh
->deleted
&& bh
->scheduled
) {
3391 /* idle bottom halves will be polled at least
3393 *timeout
= MIN(10, *timeout
);
3395 /* non-idle bottom halves will be executed
3404 /***********************************************************/
3405 /* machine registration */
3407 static QEMUMachine
*first_machine
= NULL
;
3408 QEMUMachine
*current_machine
= NULL
;
3410 int qemu_register_machine(QEMUMachine
*m
)
3413 pm
= &first_machine
;
3421 static QEMUMachine
*find_machine(const char *name
)
3425 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3426 if (!strcmp(m
->name
, name
))
3432 /***********************************************************/
3433 /* main execution loop */
3435 static void gui_update(void *opaque
)
3437 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3438 DisplayState
*ds
= opaque
;
3439 DisplayChangeListener
*dcl
= ds
->listeners
;
3443 while (dcl
!= NULL
) {
3444 if (dcl
->gui_timer_interval
&&
3445 dcl
->gui_timer_interval
< interval
)
3446 interval
= dcl
->gui_timer_interval
;
3449 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3452 static void nographic_update(void *opaque
)
3454 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3456 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3459 struct vm_change_state_entry
{
3460 VMChangeStateHandler
*cb
;
3462 LIST_ENTRY (vm_change_state_entry
) entries
;
3465 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3467 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3470 VMChangeStateEntry
*e
;
3472 e
= qemu_mallocz(sizeof (*e
));
3476 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3480 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3482 LIST_REMOVE (e
, entries
);
3486 static void vm_state_notify(int running
, int reason
)
3488 VMChangeStateEntry
*e
;
3490 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3491 e
->cb(e
->opaque
, running
, reason
);
3500 vm_state_notify(1, 0);
3501 qemu_rearm_alarm_timer(alarm_timer
);
3505 void vm_stop(int reason
)
3508 cpu_disable_ticks();
3510 vm_state_notify(0, reason
);
3514 /* reset/shutdown handler */
3516 typedef struct QEMUResetEntry
{
3517 QEMUResetHandler
*func
;
3519 struct QEMUResetEntry
*next
;
3522 static QEMUResetEntry
*first_reset_entry
;
3523 static int reset_requested
;
3524 static int shutdown_requested
;
3525 static int powerdown_requested
;
3527 int qemu_shutdown_requested(void)
3529 int r
= shutdown_requested
;
3530 shutdown_requested
= 0;
3534 int qemu_reset_requested(void)
3536 int r
= reset_requested
;
3537 reset_requested
= 0;
3541 int qemu_powerdown_requested(void)
3543 int r
= powerdown_requested
;
3544 powerdown_requested
= 0;
3548 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3550 QEMUResetEntry
**pre
, *re
;
3552 pre
= &first_reset_entry
;
3553 while (*pre
!= NULL
)
3554 pre
= &(*pre
)->next
;
3555 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3557 re
->opaque
= opaque
;
3562 void qemu_system_reset(void)
3566 /* reset all devices */
3567 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3568 re
->func(re
->opaque
);
3572 void qemu_system_reset_request(void)
3575 shutdown_requested
= 1;
3577 reset_requested
= 1;
3580 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3583 void qemu_system_shutdown_request(void)
3585 shutdown_requested
= 1;
3587 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3590 void qemu_system_powerdown_request(void)
3592 powerdown_requested
= 1;
3594 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3598 static void host_main_loop_wait(int *timeout
)
3604 /* XXX: need to suppress polling by better using win32 events */
3606 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3607 ret
|= pe
->func(pe
->opaque
);
3611 WaitObjects
*w
= &wait_objects
;
3613 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3614 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3615 if (w
->func
[ret
- WAIT_OBJECT_0
])
3616 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3618 /* Check for additional signaled events */
3619 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3621 /* Check if event is signaled */
3622 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3623 if(ret2
== WAIT_OBJECT_0
) {
3625 w
->func
[i
](w
->opaque
[i
]);
3626 } else if (ret2
== WAIT_TIMEOUT
) {
3628 err
= GetLastError();
3629 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3632 } else if (ret
== WAIT_TIMEOUT
) {
3634 err
= GetLastError();
3635 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3642 static void host_main_loop_wait(int *timeout
)
3647 void main_loop_wait(int timeout
)
3649 IOHandlerRecord
*ioh
;
3650 fd_set rfds
, wfds
, xfds
;
3654 qemu_bh_update_timeout(&timeout
);
3656 host_main_loop_wait(&timeout
);
3658 /* poll any events */
3659 /* XXX: separate device handlers from system ones */
3664 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3668 (!ioh
->fd_read_poll
||
3669 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3670 FD_SET(ioh
->fd
, &rfds
);
3674 if (ioh
->fd_write
) {
3675 FD_SET(ioh
->fd
, &wfds
);
3681 tv
.tv_sec
= timeout
/ 1000;
3682 tv
.tv_usec
= (timeout
% 1000) * 1000;
3684 #if defined(CONFIG_SLIRP)
3685 if (slirp_is_inited()) {
3686 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3689 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3691 IOHandlerRecord
**pioh
;
3693 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3694 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3695 ioh
->fd_read(ioh
->opaque
);
3697 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3698 ioh
->fd_write(ioh
->opaque
);
3702 /* remove deleted IO handlers */
3703 pioh
= &first_io_handler
;
3713 #if defined(CONFIG_SLIRP)
3714 if (slirp_is_inited()) {
3720 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3724 /* vm time timers */
3725 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3726 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3727 qemu_get_clock(vm_clock
));
3729 /* real time timers */
3730 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3731 qemu_get_clock(rt_clock
));
3733 /* Check bottom-halves last in case any of the earlier events triggered
3739 static int main_loop(void)
3742 #ifdef CONFIG_PROFILER
3747 cur_cpu
= first_cpu
;
3748 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3755 #ifdef CONFIG_PROFILER
3756 ti
= profile_getclock();
3761 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3762 env
->icount_decr
.u16
.low
= 0;
3763 env
->icount_extra
= 0;
3764 count
= qemu_next_deadline();
3765 count
= (count
+ (1 << icount_time_shift
) - 1)
3766 >> icount_time_shift
;
3767 qemu_icount
+= count
;
3768 decr
= (count
> 0xffff) ? 0xffff : count
;
3770 env
->icount_decr
.u16
.low
= decr
;
3771 env
->icount_extra
= count
;
3773 ret
= cpu_exec(env
);
3774 #ifdef CONFIG_PROFILER
3775 qemu_time
+= profile_getclock() - ti
;
3778 /* Fold pending instructions back into the
3779 instruction counter, and clear the interrupt flag. */
3780 qemu_icount
-= (env
->icount_decr
.u16
.low
3781 + env
->icount_extra
);
3782 env
->icount_decr
.u32
= 0;
3783 env
->icount_extra
= 0;
3785 next_cpu
= env
->next_cpu
?: first_cpu
;
3786 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3787 ret
= EXCP_INTERRUPT
;
3791 if (ret
== EXCP_HLT
) {
3792 /* Give the next CPU a chance to run. */
3796 if (ret
!= EXCP_HALTED
)
3798 /* all CPUs are halted ? */
3804 if (shutdown_requested
) {
3805 ret
= EXCP_INTERRUPT
;
3813 if (reset_requested
) {
3814 reset_requested
= 0;
3815 qemu_system_reset();
3816 ret
= EXCP_INTERRUPT
;
3818 if (powerdown_requested
) {
3819 powerdown_requested
= 0;
3820 qemu_system_powerdown();
3821 ret
= EXCP_INTERRUPT
;
3823 if (unlikely(ret
== EXCP_DEBUG
)) {
3824 gdb_set_stop_cpu(cur_cpu
);
3825 vm_stop(EXCP_DEBUG
);
3827 /* If all cpus are halted then wait until the next IRQ */
3828 /* XXX: use timeout computed from timers */
3829 if (ret
== EXCP_HALTED
) {
3833 /* Advance virtual time to the next event. */
3834 if (use_icount
== 1) {
3835 /* When not using an adaptive execution frequency
3836 we tend to get badly out of sync with real time,
3837 so just delay for a reasonable amount of time. */
3840 delta
= cpu_get_icount() - cpu_get_clock();
3843 /* If virtual time is ahead of real time then just
3845 timeout
= (delta
/ 1000000) + 1;
3847 /* Wait for either IO to occur or the next
3849 add
= qemu_next_deadline();
3850 /* We advance the timer before checking for IO.
3851 Limit the amount we advance so that early IO
3852 activity won't get the guest too far ahead. */
3856 add
= (add
+ (1 << icount_time_shift
) - 1)
3857 >> icount_time_shift
;
3859 timeout
= delta
/ 1000000;
3870 if (shutdown_requested
) {
3871 ret
= EXCP_INTERRUPT
;
3876 #ifdef CONFIG_PROFILER
3877 ti
= profile_getclock();
3879 main_loop_wait(timeout
);
3880 #ifdef CONFIG_PROFILER
3881 dev_time
+= profile_getclock() - ti
;
3884 cpu_disable_ticks();
3888 static void help(int exitcode
)
3890 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3891 and qemu-doc.texi */
3892 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3893 "usage: %s [options] [disk_image]\n"
3895 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3897 "Standard options:\n"
3898 "-h or -help display this help and exit\n"
3899 "-M machine select emulated machine (-M ? for list)\n"
3900 "-cpu cpu select CPU (-cpu ? for list)\n"
3901 "-smp n set the number of CPUs to 'n' [default=1]\n"
3902 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3903 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3904 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3905 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3906 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3907 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3908 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3909 " use 'file' as a drive image\n"
3910 "-mtdblock file use 'file' as on-board Flash memory image\n"
3911 "-sd file use 'file' as SecureDigital card image\n"
3912 "-pflash file use 'file' as a parallel flash image\n"
3913 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3914 "-snapshot write to temporary files instead of disk image files\n"
3915 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3917 "-k language use keyboard layout (for example \"fr\" for French)\n"
3920 "-audio-help print list of audio drivers and their options\n"
3921 "-soundhw c1,... enable audio support\n"
3922 " and only specified sound cards (comma separated list)\n"
3923 " use -soundhw ? to get the list of supported cards\n"
3924 " use -soundhw all to enable all of them\n"
3926 "-usb enable the USB driver (will be the default soon)\n"
3927 "-usbdevice name add the host or guest USB device 'name'\n"
3928 "-name string set the name of the guest\n"
3929 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3930 " specify machine UUID\n"
3932 "Display options:\n"
3933 "-nographic disable graphical output and redirect serial I/Os to console\n"
3934 #ifdef CONFIG_CURSES
3935 "-curses use a curses/ncurses interface instead of SDL\n"
3938 "-no-frame open SDL window without a frame and window decorations\n"
3939 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3940 "-no-quit disable SDL window close capability\n"
3943 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3944 "-vga [std|cirrus|vmware|none]\n"
3945 " select video card type\n"
3946 "-full-screen start in full screen\n"
3947 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3948 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3950 "-vnc display start a VNC server on display\n"
3952 "Network options:\n"
3953 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3954 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3956 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3957 " connect the user mode network stack to VLAN 'n' and send\n"
3958 " hostname 'host' to DHCP clients\n"
3961 "-net tap[,vlan=n][,name=str],ifname=name\n"
3962 " connect the host TAP network interface to VLAN 'n'\n"
3964 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3965 " connect the host TAP network interface to VLAN 'n' and use the\n"
3966 " network scripts 'file' (default=%s)\n"
3967 " and 'dfile' (default=%s);\n"
3968 " use '[down]script=no' to disable script execution;\n"
3969 " use 'fd=h' to connect to an already opened TAP interface\n"
3971 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3972 " connect the vlan 'n' to another VLAN using a socket connection\n"
3973 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3974 " connect the vlan 'n' to multicast maddr and port\n"
3976 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3977 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3978 " on host and listening for incoming connections on 'socketpath'.\n"
3979 " Use group 'groupname' and mode 'octalmode' to change default\n"
3980 " ownership and permissions for communication port.\n"
3982 "-net none use it alone to have zero network devices; if no -net option\n"
3983 " is provided, the default is '-net nic -net user'\n"
3985 "-tftp dir allow tftp access to files in dir [-net user]\n"
3986 "-bootp file advertise file in BOOTP replies\n"
3988 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3990 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3991 " redirect TCP or UDP connections from host to guest [-net user]\n"
3994 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
3995 "-bt hci,host[:id]\n"
3996 " use host's HCI with the given name\n"
3997 "-bt hci[,vlan=n]\n"
3998 " emulate a standard HCI in virtual scatternet 'n'\n"
3999 "-bt vhci[,vlan=n]\n"
4000 " add host computer to virtual scatternet 'n' using VHCI\n"
4001 "-bt device:dev[,vlan=n]\n"
4002 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4006 "i386 target only:\n"
4007 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4008 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4009 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4010 "-no-acpi disable ACPI\n"
4011 "-no-hpet disable HPET\n"
4013 "Linux boot specific:\n"
4014 "-kernel bzImage use 'bzImage' as kernel image\n"
4015 "-append cmdline use 'cmdline' as kernel command line\n"
4016 "-initrd file use 'file' as initial ram disk\n"
4018 "Debug/Expert options:\n"
4019 "-serial dev redirect the serial port to char device 'dev'\n"
4020 "-parallel dev redirect the parallel port to char device 'dev'\n"
4021 "-monitor dev redirect the monitor to char device 'dev'\n"
4022 "-pidfile file write PID to 'file'\n"
4023 "-S freeze CPU at startup (use 'c' to start execution)\n"
4024 "-s wait gdb connection to port\n"
4025 "-p port set gdb connection port [default=%s]\n"
4026 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4027 "-hdachs c,h,s[,t]\n"
4028 " force hard disk 0 physical geometry and the optional BIOS\n"
4029 " translation (t=none or lba) (usually qemu can guess them)\n"
4030 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4031 "-bios file set the filename for the BIOS\n"
4033 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4034 "-no-kqemu disable KQEMU kernel module usage\n"
4037 "-enable-kvm enable KVM full virtualization support\n"
4039 "-no-reboot exit instead of rebooting\n"
4040 "-no-shutdown stop before shutdown\n"
4041 "-loadvm [tag|id]\n"
4042 " start right away with a saved state (loadvm in monitor)\n"
4044 "-daemonize daemonize QEMU after initializing\n"
4046 "-option-rom rom load a file, rom, into the option ROM space\n"
4047 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4048 "-prom-env variable=value\n"
4049 " set OpenBIOS nvram variables\n"
4051 "-clock force the use of the given methods for timer alarm.\n"
4052 " To see what timers are available use -clock ?\n"
4053 "-localtime set the real time clock to local time [default=utc]\n"
4054 "-startdate select initial date of the clock\n"
4055 "-icount [N|auto]\n"
4056 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4057 "-echr chr set terminal escape character instead of ctrl-a\n"
4058 "-virtioconsole c\n"
4059 " set virtio console\n"
4060 "-show-cursor show cursor\n"
4061 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4062 "-semihosting semihosting mode\n"
4064 #if defined(TARGET_ARM)
4065 "-old-param old param mode\n"
4067 "-tb-size n set TB size\n"
4068 "-incoming p prepare for incoming migration, listen on port p\n"
4070 "During emulation, the following keys are useful:\n"
4071 "ctrl-alt-f toggle full screen\n"
4072 "ctrl-alt-n switch to virtual console 'n'\n"
4073 "ctrl-alt toggle mouse and keyboard grab\n"
4075 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4080 DEFAULT_NETWORK_SCRIPT
,
4081 DEFAULT_NETWORK_DOWN_SCRIPT
,
4083 DEFAULT_GDBSTUB_PORT
,
4088 #define HAS_ARG 0x0001
4091 /* Please keep in synch with help, qemu_options[] and
4093 /* Standard options: */
4106 QEMU_OPTION_mtdblock
,
4110 QEMU_OPTION_snapshot
,
4113 QEMU_OPTION_audio_help
,
4114 QEMU_OPTION_soundhw
,
4116 QEMU_OPTION_usbdevice
,
4120 /* Display options: */
4121 QEMU_OPTION_nographic
,
4123 QEMU_OPTION_no_frame
,
4124 QEMU_OPTION_alt_grab
,
4125 QEMU_OPTION_no_quit
,
4127 QEMU_OPTION_portrait
,
4129 QEMU_OPTION_full_screen
,
4133 /* Network options: */
4141 /* i386 target only: */
4142 QEMU_OPTION_win2k_hack
,
4143 QEMU_OPTION_rtc_td_hack
,
4144 QEMU_OPTION_no_fd_bootchk
,
4145 QEMU_OPTION_no_acpi
,
4146 QEMU_OPTION_no_hpet
,
4148 /* Linux boot specific: */
4153 /* Debug/Expert options: */
4155 QEMU_OPTION_parallel
,
4156 QEMU_OPTION_monitor
,
4157 QEMU_OPTION_pidfile
,
4165 QEMU_OPTION_kernel_kqemu
,
4166 QEMU_OPTION_no_kqemu
,
4167 QEMU_OPTION_enable_kvm
,
4168 QEMU_OPTION_no_reboot
,
4169 QEMU_OPTION_no_shutdown
,
4171 QEMU_OPTION_daemonize
,
4172 QEMU_OPTION_option_rom
,
4173 QEMU_OPTION_prom_env
,
4175 QEMU_OPTION_localtime
,
4176 QEMU_OPTION_startdate
,
4179 QEMU_OPTION_virtiocon
,
4180 QEMU_OPTION_show_cursor
,
4181 QEMU_OPTION_semihosting
,
4182 QEMU_OPTION_old_param
,
4183 QEMU_OPTION_tb_size
,
4184 QEMU_OPTION_incoming
,
4187 typedef struct QEMUOption
{
4193 static const QEMUOption qemu_options
[] = {
4194 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4196 /* Standard options: */
4197 { "h", 0, QEMU_OPTION_h
},
4198 { "help", 0, QEMU_OPTION_h
},
4199 { "M", HAS_ARG
, QEMU_OPTION_M
},
4200 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4201 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4202 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4203 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4204 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4205 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4206 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4207 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4208 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4209 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4210 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4211 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4212 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4213 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4214 { "snapshot", 0, QEMU_OPTION_snapshot
},
4215 { "m", HAS_ARG
, QEMU_OPTION_m
},
4217 { "k", HAS_ARG
, QEMU_OPTION_k
},
4220 { "audio-help", 0, QEMU_OPTION_audio_help
},
4221 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4223 { "usb", 0, QEMU_OPTION_usb
},
4224 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4225 { "name", HAS_ARG
, QEMU_OPTION_name
},
4226 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4228 /* Display options: */
4229 { "nographic", 0, QEMU_OPTION_nographic
},
4230 #ifdef CONFIG_CURSES
4231 { "curses", 0, QEMU_OPTION_curses
},
4234 { "no-frame", 0, QEMU_OPTION_no_frame
},
4235 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4236 { "no-quit", 0, QEMU_OPTION_no_quit
},
4237 { "sdl", 0, QEMU_OPTION_sdl
},
4239 { "portrait", 0, QEMU_OPTION_portrait
},
4240 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4241 { "full-screen", 0, QEMU_OPTION_full_screen
},
4242 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4243 { "g", 1, QEMU_OPTION_g
},
4245 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4247 /* Network options: */
4248 { "net", HAS_ARG
, QEMU_OPTION_net
},
4250 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4251 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4253 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4255 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4257 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4259 /* i386 target only: */
4260 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4261 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4262 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4263 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4264 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4267 /* Linux boot specific: */
4268 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4269 { "append", HAS_ARG
, QEMU_OPTION_append
},
4270 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4272 /* Debug/Expert options: */
4273 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4274 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4275 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4276 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4277 { "S", 0, QEMU_OPTION_S
},
4278 { "s", 0, QEMU_OPTION_s
},
4279 { "p", HAS_ARG
, QEMU_OPTION_p
},
4280 { "d", HAS_ARG
, QEMU_OPTION_d
},
4281 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4282 { "L", HAS_ARG
, QEMU_OPTION_L
},
4283 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4285 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4286 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4289 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4291 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4292 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4293 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4294 { "daemonize", 0, QEMU_OPTION_daemonize
},
4295 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4296 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4297 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4299 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4300 { "localtime", 0, QEMU_OPTION_localtime
},
4301 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4302 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4303 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4304 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4305 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4306 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4307 { "semihosting", 0, QEMU_OPTION_semihosting
},
4309 #if defined(TARGET_ARM)
4310 { "old-param", 0, QEMU_OPTION_old_param
},
4312 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4313 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4317 /* password input */
4319 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4324 if (!bdrv_is_encrypted(bs
))
4327 term_printf("%s is encrypted.\n", name
);
4328 for(i
= 0; i
< 3; i
++) {
4329 monitor_readline("Password: ", 1, password
, sizeof(password
));
4330 if (bdrv_set_key(bs
, password
) == 0)
4332 term_printf("invalid password\n");
4337 static BlockDriverState
*get_bdrv(int index
)
4339 if (index
> nb_drives
)
4341 return drives_table
[index
].bdrv
;
4344 static void read_passwords(void)
4346 BlockDriverState
*bs
;
4349 for(i
= 0; i
< 6; i
++) {
4352 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4357 struct soundhw soundhw
[] = {
4358 #ifdef HAS_AUDIO_CHOICE
4359 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4365 { .init_isa
= pcspk_audio_init
}
4372 "Creative Sound Blaster 16",
4375 { .init_isa
= SB16_init
}
4379 #ifdef CONFIG_CS4231A
4385 { .init_isa
= cs4231a_init
}
4393 "Yamaha YMF262 (OPL3)",
4395 "Yamaha YM3812 (OPL2)",
4399 { .init_isa
= Adlib_init
}
4406 "Gravis Ultrasound GF1",
4409 { .init_isa
= GUS_init
}
4416 "Intel 82801AA AC97 Audio",
4419 { .init_pci
= ac97_init
}
4423 #ifdef CONFIG_ES1370
4426 "ENSONIQ AudioPCI ES1370",
4429 { .init_pci
= es1370_init
}
4433 #endif /* HAS_AUDIO_CHOICE */
4435 { NULL
, NULL
, 0, 0, { NULL
} }
4438 static void select_soundhw (const char *optarg
)
4442 if (*optarg
== '?') {
4445 printf ("Valid sound card names (comma separated):\n");
4446 for (c
= soundhw
; c
->name
; ++c
) {
4447 printf ("%-11s %s\n", c
->name
, c
->descr
);
4449 printf ("\n-soundhw all will enable all of the above\n");
4450 exit (*optarg
!= '?');
4458 if (!strcmp (optarg
, "all")) {
4459 for (c
= soundhw
; c
->name
; ++c
) {
4467 e
= strchr (p
, ',');
4468 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4470 for (c
= soundhw
; c
->name
; ++c
) {
4471 if (!strncmp (c
->name
, p
, l
)) {
4480 "Unknown sound card name (too big to show)\n");
4483 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4488 p
+= l
+ (e
!= NULL
);
4492 goto show_valid_cards
;
4497 static void select_vgahw (const char *p
)
4501 if (strstart(p
, "std", &opts
)) {
4502 std_vga_enabled
= 1;
4503 cirrus_vga_enabled
= 0;
4505 } else if (strstart(p
, "cirrus", &opts
)) {
4506 cirrus_vga_enabled
= 1;
4507 std_vga_enabled
= 0;
4509 } else if (strstart(p
, "vmware", &opts
)) {
4510 cirrus_vga_enabled
= 0;
4511 std_vga_enabled
= 0;
4513 } else if (strstart(p
, "none", &opts
)) {
4514 cirrus_vga_enabled
= 0;
4515 std_vga_enabled
= 0;
4519 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4523 const char *nextopt
;
4525 if (strstart(opts
, ",retrace=", &nextopt
)) {
4527 if (strstart(opts
, "dumb", &nextopt
))
4528 vga_retrace_method
= VGA_RETRACE_DUMB
;
4529 else if (strstart(opts
, "precise", &nextopt
))
4530 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4531 else goto invalid_vga
;
4532 } else goto invalid_vga
;
4538 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4540 exit(STATUS_CONTROL_C_EXIT
);
4545 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4549 if(strlen(str
) != 36)
4552 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4553 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4554 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4562 #define MAX_NET_CLIENTS 32
4566 static void termsig_handler(int signal
)
4568 qemu_system_shutdown_request();
4571 static void termsig_setup(void)
4573 struct sigaction act
;
4575 memset(&act
, 0, sizeof(act
));
4576 act
.sa_handler
= termsig_handler
;
4577 sigaction(SIGINT
, &act
, NULL
);
4578 sigaction(SIGHUP
, &act
, NULL
);
4579 sigaction(SIGTERM
, &act
, NULL
);
4584 int main(int argc
, char **argv
, char **envp
)
4586 #ifdef CONFIG_GDBSTUB
4588 const char *gdbstub_port
;
4590 uint32_t boot_devices_bitmap
= 0;
4592 int snapshot
, linux_boot
, net_boot
;
4593 const char *initrd_filename
;
4594 const char *kernel_filename
, *kernel_cmdline
;
4595 const char *boot_devices
= "";
4597 DisplayChangeListener
*dcl
;
4598 int cyls
, heads
, secs
, translation
;
4599 const char *net_clients
[MAX_NET_CLIENTS
];
4601 const char *bt_opts
[MAX_BT_CMDLINE
];
4605 const char *r
, *optarg
;
4606 CharDriverState
*monitor_hd
= NULL
;
4607 const char *monitor_device
;
4608 const char *serial_devices
[MAX_SERIAL_PORTS
];
4609 int serial_device_index
;
4610 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4611 int parallel_device_index
;
4612 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4613 int virtio_console_index
;
4614 const char *loadvm
= NULL
;
4615 QEMUMachine
*machine
;
4616 const char *cpu_model
;
4617 const char *usb_devices
[MAX_USB_CMDLINE
];
4618 int usb_devices_index
;
4621 const char *pid_file
= NULL
;
4623 const char *incoming
= NULL
;
4625 qemu_cache_utils_init(envp
);
4627 LIST_INIT (&vm_change_state_head
);
4630 struct sigaction act
;
4631 sigfillset(&act
.sa_mask
);
4633 act
.sa_handler
= SIG_IGN
;
4634 sigaction(SIGPIPE
, &act
, NULL
);
4637 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4638 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4639 QEMU to run on a single CPU */
4644 h
= GetCurrentProcess();
4645 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4646 for(i
= 0; i
< 32; i
++) {
4647 if (mask
& (1 << i
))
4652 SetProcessAffinityMask(h
, mask
);
4658 register_machines();
4659 machine
= first_machine
;
4661 initrd_filename
= NULL
;
4663 vga_ram_size
= VGA_RAM_SIZE
;
4664 #ifdef CONFIG_GDBSTUB
4666 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4671 kernel_filename
= NULL
;
4672 kernel_cmdline
= "";
4673 cyls
= heads
= secs
= 0;
4674 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4675 monitor_device
= "vc";
4677 serial_devices
[0] = "vc:80Cx24C";
4678 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4679 serial_devices
[i
] = NULL
;
4680 serial_device_index
= 0;
4682 parallel_devices
[0] = "vc:640x480";
4683 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4684 parallel_devices
[i
] = NULL
;
4685 parallel_device_index
= 0;
4687 virtio_consoles
[0] = "vc:80Cx24C";
4688 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4689 virtio_consoles
[i
] = NULL
;
4690 virtio_console_index
= 0;
4692 usb_devices_index
= 0;
4711 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4713 const QEMUOption
*popt
;
4716 /* Treat --foo the same as -foo. */
4719 popt
= qemu_options
;
4722 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4726 if (!strcmp(popt
->name
, r
+ 1))
4730 if (popt
->flags
& HAS_ARG
) {
4731 if (optind
>= argc
) {
4732 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4736 optarg
= argv
[optind
++];
4741 switch(popt
->index
) {
4743 machine
= find_machine(optarg
);
4746 printf("Supported machines are:\n");
4747 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4748 printf("%-10s %s%s\n",
4750 m
== first_machine
? " (default)" : "");
4752 exit(*optarg
!= '?');
4755 case QEMU_OPTION_cpu
:
4756 /* hw initialization will check this */
4757 if (*optarg
== '?') {
4758 /* XXX: implement xxx_cpu_list for targets that still miss it */
4759 #if defined(cpu_list)
4760 cpu_list(stdout
, &fprintf
);
4767 case QEMU_OPTION_initrd
:
4768 initrd_filename
= optarg
;
4770 case QEMU_OPTION_hda
:
4772 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4774 hda_index
= drive_add(optarg
, HD_ALIAS
4775 ",cyls=%d,heads=%d,secs=%d%s",
4776 0, cyls
, heads
, secs
,
4777 translation
== BIOS_ATA_TRANSLATION_LBA
?
4779 translation
== BIOS_ATA_TRANSLATION_NONE
?
4780 ",trans=none" : "");
4782 case QEMU_OPTION_hdb
:
4783 case QEMU_OPTION_hdc
:
4784 case QEMU_OPTION_hdd
:
4785 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4787 case QEMU_OPTION_drive
:
4788 drive_add(NULL
, "%s", optarg
);
4790 case QEMU_OPTION_mtdblock
:
4791 drive_add(optarg
, MTD_ALIAS
);
4793 case QEMU_OPTION_sd
:
4794 drive_add(optarg
, SD_ALIAS
);
4796 case QEMU_OPTION_pflash
:
4797 drive_add(optarg
, PFLASH_ALIAS
);
4799 case QEMU_OPTION_snapshot
:
4802 case QEMU_OPTION_hdachs
:
4806 cyls
= strtol(p
, (char **)&p
, 0);
4807 if (cyls
< 1 || cyls
> 16383)
4812 heads
= strtol(p
, (char **)&p
, 0);
4813 if (heads
< 1 || heads
> 16)
4818 secs
= strtol(p
, (char **)&p
, 0);
4819 if (secs
< 1 || secs
> 63)
4823 if (!strcmp(p
, "none"))
4824 translation
= BIOS_ATA_TRANSLATION_NONE
;
4825 else if (!strcmp(p
, "lba"))
4826 translation
= BIOS_ATA_TRANSLATION_LBA
;
4827 else if (!strcmp(p
, "auto"))
4828 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4831 } else if (*p
!= '\0') {
4833 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4836 if (hda_index
!= -1)
4837 snprintf(drives_opt
[hda_index
].opt
,
4838 sizeof(drives_opt
[hda_index
].opt
),
4839 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4840 0, cyls
, heads
, secs
,
4841 translation
== BIOS_ATA_TRANSLATION_LBA
?
4843 translation
== BIOS_ATA_TRANSLATION_NONE
?
4844 ",trans=none" : "");
4847 case QEMU_OPTION_nographic
:
4850 #ifdef CONFIG_CURSES
4851 case QEMU_OPTION_curses
:
4855 case QEMU_OPTION_portrait
:
4858 case QEMU_OPTION_kernel
:
4859 kernel_filename
= optarg
;
4861 case QEMU_OPTION_append
:
4862 kernel_cmdline
= optarg
;
4864 case QEMU_OPTION_cdrom
:
4865 drive_add(optarg
, CDROM_ALIAS
);
4867 case QEMU_OPTION_boot
:
4868 boot_devices
= optarg
;
4869 /* We just do some generic consistency checks */
4871 /* Could easily be extended to 64 devices if needed */
4874 boot_devices_bitmap
= 0;
4875 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4876 /* Allowed boot devices are:
4877 * a b : floppy disk drives
4878 * c ... f : IDE disk drives
4879 * g ... m : machine implementation dependant drives
4880 * n ... p : network devices
4881 * It's up to each machine implementation to check
4882 * if the given boot devices match the actual hardware
4883 * implementation and firmware features.
4885 if (*p
< 'a' || *p
> 'q') {
4886 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4889 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4891 "Boot device '%c' was given twice\n",*p
);
4894 boot_devices_bitmap
|= 1 << (*p
- 'a');
4898 case QEMU_OPTION_fda
:
4899 case QEMU_OPTION_fdb
:
4900 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4903 case QEMU_OPTION_no_fd_bootchk
:
4907 case QEMU_OPTION_net
:
4908 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4909 fprintf(stderr
, "qemu: too many network clients\n");
4912 net_clients
[nb_net_clients
] = optarg
;
4916 case QEMU_OPTION_tftp
:
4917 tftp_prefix
= optarg
;
4919 case QEMU_OPTION_bootp
:
4920 bootp_filename
= optarg
;
4923 case QEMU_OPTION_smb
:
4924 net_slirp_smb(optarg
);
4927 case QEMU_OPTION_redir
:
4928 net_slirp_redir(optarg
);
4931 case QEMU_OPTION_bt
:
4932 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4933 fprintf(stderr
, "qemu: too many bluetooth options\n");
4936 bt_opts
[nb_bt_opts
++] = optarg
;
4939 case QEMU_OPTION_audio_help
:
4943 case QEMU_OPTION_soundhw
:
4944 select_soundhw (optarg
);
4950 case QEMU_OPTION_m
: {
4954 value
= strtoul(optarg
, &ptr
, 10);
4956 case 0: case 'M': case 'm':
4963 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4967 /* On 32-bit hosts, QEMU is limited by virtual address space */
4968 if (value
> (2047 << 20)
4970 && HOST_LONG_BITS
== 32
4973 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4976 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4977 fprintf(stderr
, "qemu: ram size too large\n");
4986 const CPULogItem
*item
;
4988 mask
= cpu_str_to_log_mask(optarg
);
4990 printf("Log items (comma separated):\n");
4991 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4992 printf("%-10s %s\n", item
->name
, item
->help
);
4999 #ifdef CONFIG_GDBSTUB
5004 gdbstub_port
= optarg
;
5010 case QEMU_OPTION_bios
:
5017 keyboard_layout
= optarg
;
5019 case QEMU_OPTION_localtime
:
5022 case QEMU_OPTION_vga
:
5023 select_vgahw (optarg
);
5030 w
= strtol(p
, (char **)&p
, 10);
5033 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5039 h
= strtol(p
, (char **)&p
, 10);
5044 depth
= strtol(p
, (char **)&p
, 10);
5045 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5046 depth
!= 24 && depth
!= 32)
5048 } else if (*p
== '\0') {
5049 depth
= graphic_depth
;
5056 graphic_depth
= depth
;
5059 case QEMU_OPTION_echr
:
5062 term_escape_char
= strtol(optarg
, &r
, 0);
5064 printf("Bad argument to echr\n");
5067 case QEMU_OPTION_monitor
:
5068 monitor_device
= optarg
;
5070 case QEMU_OPTION_serial
:
5071 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5072 fprintf(stderr
, "qemu: too many serial ports\n");
5075 serial_devices
[serial_device_index
] = optarg
;
5076 serial_device_index
++;
5078 case QEMU_OPTION_virtiocon
:
5079 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5080 fprintf(stderr
, "qemu: too many virtio consoles\n");
5083 virtio_consoles
[virtio_console_index
] = optarg
;
5084 virtio_console_index
++;
5086 case QEMU_OPTION_parallel
:
5087 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5088 fprintf(stderr
, "qemu: too many parallel ports\n");
5091 parallel_devices
[parallel_device_index
] = optarg
;
5092 parallel_device_index
++;
5094 case QEMU_OPTION_loadvm
:
5097 case QEMU_OPTION_full_screen
:
5101 case QEMU_OPTION_no_frame
:
5104 case QEMU_OPTION_alt_grab
:
5107 case QEMU_OPTION_no_quit
:
5110 case QEMU_OPTION_sdl
:
5114 case QEMU_OPTION_pidfile
:
5118 case QEMU_OPTION_win2k_hack
:
5119 win2k_install_hack
= 1;
5121 case QEMU_OPTION_rtc_td_hack
:
5126 case QEMU_OPTION_no_kqemu
:
5129 case QEMU_OPTION_kernel_kqemu
:
5134 case QEMU_OPTION_enable_kvm
:
5141 case QEMU_OPTION_usb
:
5144 case QEMU_OPTION_usbdevice
:
5146 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5147 fprintf(stderr
, "Too many USB devices\n");
5150 usb_devices
[usb_devices_index
] = optarg
;
5151 usb_devices_index
++;
5153 case QEMU_OPTION_smp
:
5154 smp_cpus
= atoi(optarg
);
5156 fprintf(stderr
, "Invalid number of CPUs\n");
5160 case QEMU_OPTION_vnc
:
5161 vnc_display
= optarg
;
5163 case QEMU_OPTION_no_acpi
:
5166 case QEMU_OPTION_no_hpet
:
5169 case QEMU_OPTION_no_reboot
:
5172 case QEMU_OPTION_no_shutdown
:
5175 case QEMU_OPTION_show_cursor
:
5178 case QEMU_OPTION_uuid
:
5179 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5180 fprintf(stderr
, "Fail to parse UUID string."
5181 " Wrong format.\n");
5185 case QEMU_OPTION_daemonize
:
5188 case QEMU_OPTION_option_rom
:
5189 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5190 fprintf(stderr
, "Too many option ROMs\n");
5193 option_rom
[nb_option_roms
] = optarg
;
5196 case QEMU_OPTION_semihosting
:
5197 semihosting_enabled
= 1;
5199 case QEMU_OPTION_name
:
5202 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5203 case QEMU_OPTION_prom_env
:
5204 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5205 fprintf(stderr
, "Too many prom variables\n");
5208 prom_envs
[nb_prom_envs
] = optarg
;
5213 case QEMU_OPTION_old_param
:
5217 case QEMU_OPTION_clock
:
5218 configure_alarms(optarg
);
5220 case QEMU_OPTION_startdate
:
5223 time_t rtc_start_date
;
5224 if (!strcmp(optarg
, "now")) {
5225 rtc_date_offset
= -1;
5227 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5235 } else if (sscanf(optarg
, "%d-%d-%d",
5238 &tm
.tm_mday
) == 3) {
5247 rtc_start_date
= mktimegm(&tm
);
5248 if (rtc_start_date
== -1) {
5250 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5251 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5254 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5258 case QEMU_OPTION_tb_size
:
5259 tb_size
= strtol(optarg
, NULL
, 0);
5263 case QEMU_OPTION_icount
:
5265 if (strcmp(optarg
, "auto") == 0) {
5266 icount_time_shift
= -1;
5268 icount_time_shift
= strtol(optarg
, NULL
, 0);
5271 case QEMU_OPTION_incoming
:
5278 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5279 if (kvm_allowed
&& kqemu_allowed
) {
5281 "You can not enable both KVM and kqemu at the same time\n");
5286 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5287 if (smp_cpus
> machine
->max_cpus
) {
5288 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5289 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5295 if (serial_device_index
== 0)
5296 serial_devices
[0] = "stdio";
5297 if (parallel_device_index
== 0)
5298 parallel_devices
[0] = "null";
5299 if (strncmp(monitor_device
, "vc", 2) == 0)
5300 monitor_device
= "stdio";
5301 if (virtio_console_index
== 0)
5302 virtio_consoles
[0] = "null";
5309 if (pipe(fds
) == -1)
5320 len
= read(fds
[0], &status
, 1);
5321 if (len
== -1 && (errno
== EINTR
))
5326 else if (status
== 1) {
5327 fprintf(stderr
, "Could not acquire pidfile\n");
5344 signal(SIGTSTP
, SIG_IGN
);
5345 signal(SIGTTOU
, SIG_IGN
);
5346 signal(SIGTTIN
, SIG_IGN
);
5350 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5353 write(fds
[1], &status
, 1);
5355 fprintf(stderr
, "Could not acquire pid file\n");
5363 linux_boot
= (kernel_filename
!= NULL
);
5364 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5366 if (!linux_boot
&& net_boot
== 0 &&
5367 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5370 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5371 fprintf(stderr
, "-append only allowed with -kernel option\n");
5375 if (!linux_boot
&& initrd_filename
!= NULL
) {
5376 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5380 /* boot to floppy or the default cd if no hard disk defined yet */
5381 if (!boot_devices
[0]) {
5382 boot_devices
= "cad";
5384 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5387 if (init_timer_alarm() < 0) {
5388 fprintf(stderr
, "could not initialize alarm timer\n");
5391 if (use_icount
&& icount_time_shift
< 0) {
5393 /* 125MIPS seems a reasonable initial guess at the guest speed.
5394 It will be corrected fairly quickly anyway. */
5395 icount_time_shift
= 3;
5396 init_icount_adjust();
5403 /* init network clients */
5404 if (nb_net_clients
== 0) {
5405 /* if no clients, we use a default config */
5406 net_clients
[nb_net_clients
++] = "nic";
5408 net_clients
[nb_net_clients
++] = "user";
5412 for(i
= 0;i
< nb_net_clients
; i
++) {
5413 if (net_client_parse(net_clients
[i
]) < 0)
5419 /* XXX: this should be moved in the PC machine instantiation code */
5420 if (net_boot
!= 0) {
5422 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5423 const char *model
= nd_table
[i
].model
;
5425 if (net_boot
& (1 << i
)) {
5428 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5429 if (get_image_size(buf
) > 0) {
5430 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5431 fprintf(stderr
, "Too many option ROMs\n");
5434 option_rom
[nb_option_roms
] = strdup(buf
);
5441 fprintf(stderr
, "No valid PXE rom found for network device\n");
5447 /* init the bluetooth world */
5448 for (i
= 0; i
< nb_bt_opts
; i
++)
5449 if (bt_parse(bt_opts
[i
]))
5452 /* init the memory */
5453 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5455 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5457 if (ram_size
< phys_ram_size
) {
5458 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5459 machine
->name
, (unsigned long long) phys_ram_size
);
5463 phys_ram_size
= ram_size
;
5465 ram_size
= phys_ram_size
;
5468 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5470 phys_ram_size
+= ram_size
;
5473 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5474 if (!phys_ram_base
) {
5475 fprintf(stderr
, "Could not allocate physical memory\n");
5479 /* init the dynamic translator */
5480 cpu_exec_init_all(tb_size
* 1024 * 1024);
5484 /* we always create the cdrom drive, even if no disk is there */
5486 if (nb_drives_opt
< MAX_DRIVES
)
5487 drive_add(NULL
, CDROM_ALIAS
);
5489 /* we always create at least one floppy */
5491 if (nb_drives_opt
< MAX_DRIVES
)
5492 drive_add(NULL
, FD_ALIAS
, 0);
5494 /* we always create one sd slot, even if no card is in it */
5496 if (nb_drives_opt
< MAX_DRIVES
)
5497 drive_add(NULL
, SD_ALIAS
);
5499 /* open the virtual block devices */
5501 for(i
= 0; i
< nb_drives_opt
; i
++)
5502 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5505 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5506 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5509 /* must be after terminal init, SDL library changes signal handlers */
5513 /* Maintain compatibility with multiple stdio monitors */
5514 if (!strcmp(monitor_device
,"stdio")) {
5515 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5516 const char *devname
= serial_devices
[i
];
5517 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5518 monitor_device
= NULL
;
5520 } else if (devname
&& !strcmp(devname
,"stdio")) {
5521 monitor_device
= NULL
;
5522 serial_devices
[i
] = "mon:stdio";
5528 if (kvm_enabled()) {
5531 ret
= kvm_init(smp_cpus
);
5533 fprintf(stderr
, "failed to initialize KVM\n");
5538 if (monitor_device
) {
5539 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5541 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5546 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5547 const char *devname
= serial_devices
[i
];
5548 if (devname
&& strcmp(devname
, "none")) {
5550 snprintf(label
, sizeof(label
), "serial%d", i
);
5551 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5552 if (!serial_hds
[i
]) {
5553 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5560 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5561 const char *devname
= parallel_devices
[i
];
5562 if (devname
&& strcmp(devname
, "none")) {
5564 snprintf(label
, sizeof(label
), "parallel%d", i
);
5565 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5566 if (!parallel_hds
[i
]) {
5567 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5574 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5575 const char *devname
= virtio_consoles
[i
];
5576 if (devname
&& strcmp(devname
, "none")) {
5578 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5579 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5580 if (!virtcon_hds
[i
]) {
5581 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5588 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5589 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5591 current_machine
= machine
;
5593 /* Set KVM's vcpu state to qemu's initial CPUState. */
5594 if (kvm_enabled()) {
5597 ret
= kvm_sync_vcpus();
5599 fprintf(stderr
, "failed to initialize vcpus\n");
5604 /* init USB devices */
5606 for(i
= 0; i
< usb_devices_index
; i
++) {
5607 if (usb_device_add(usb_devices
[i
]) < 0) {
5608 fprintf(stderr
, "Warning: could not add USB device %s\n",
5615 dumb_display_init();
5616 /* just use the first displaystate for the moment */
5621 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5625 #if defined(CONFIG_CURSES)
5627 /* At the moment curses cannot be used with other displays */
5628 curses_display_init(ds
, full_screen
);
5632 if (vnc_display
!= NULL
) {
5633 vnc_display_init(ds
);
5634 if (vnc_display_open(ds
, vnc_display
) < 0)
5637 #if defined(CONFIG_SDL)
5638 if (sdl
|| !vnc_display
)
5639 sdl_display_init(ds
, full_screen
, no_frame
);
5640 #elif defined(CONFIG_COCOA)
5641 if (sdl
|| !vnc_display
)
5642 cocoa_display_init(ds
, full_screen
);
5648 dcl
= ds
->listeners
;
5649 while (dcl
!= NULL
) {
5650 if (dcl
->dpy_refresh
!= NULL
) {
5651 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5652 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5657 if (nographic
|| (vnc_display
&& !sdl
)) {
5658 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5659 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5662 text_consoles_set_display(display_state
);
5664 if (monitor_device
&& monitor_hd
)
5665 monitor_init(monitor_hd
, !nographic
);
5667 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5668 const char *devname
= serial_devices
[i
];
5669 if (devname
&& strcmp(devname
, "none")) {
5671 snprintf(label
, sizeof(label
), "serial%d", i
);
5672 if (strstart(devname
, "vc", 0))
5673 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5677 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5678 const char *devname
= parallel_devices
[i
];
5679 if (devname
&& strcmp(devname
, "none")) {
5681 snprintf(label
, sizeof(label
), "parallel%d", i
);
5682 if (strstart(devname
, "vc", 0))
5683 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5687 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5688 const char *devname
= virtio_consoles
[i
];
5689 if (virtcon_hds
[i
] && devname
) {
5691 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5692 if (strstart(devname
, "vc", 0))
5693 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5697 #ifdef CONFIG_GDBSTUB
5699 /* XXX: use standard host:port notation and modify options
5701 if (gdbserver_start(gdbstub_port
) < 0) {
5702 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5713 autostart
= 0; /* fixme how to deal with -daemonize */
5714 qemu_start_incoming_migration(incoming
);
5718 /* XXX: simplify init */
5731 len
= write(fds
[1], &status
, 1);
5732 if (len
== -1 && (errno
== EINTR
))
5739 TFR(fd
= open("/dev/null", O_RDWR
));