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
];
245 static int nb_drives_opt
;
246 static struct drive_opt
{
249 } drives_opt
[MAX_DRIVES
];
251 static CPUState
*cur_cpu
;
252 static CPUState
*next_cpu
;
253 static int event_pending
= 1;
254 /* Conversion factor from emulated instructions to virtual clock ticks. */
255 static int icount_time_shift
;
256 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
257 #define MAX_ICOUNT_SHIFT 10
258 /* Compensate for varying guest execution speed. */
259 static int64_t qemu_icount_bias
;
260 static QEMUTimer
*icount_rt_timer
;
261 static QEMUTimer
*icount_vm_timer
;
262 static QEMUTimer
*nographic_timer
;
264 uint8_t qemu_uuid
[16];
266 /***********************************************************/
267 /* x86 ISA bus support */
269 target_phys_addr_t isa_mem_base
= 0;
272 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
273 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
275 static uint32_t ioport_read(int index
, uint32_t address
)
277 static IOPortReadFunc
*default_func
[3] = {
278 default_ioport_readb
,
279 default_ioport_readw
,
282 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
284 func
= default_func
[index
];
285 return func(ioport_opaque
[address
], address
);
288 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
290 static IOPortWriteFunc
*default_func
[3] = {
291 default_ioport_writeb
,
292 default_ioport_writew
,
293 default_ioport_writel
295 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
297 func
= default_func
[index
];
298 func(ioport_opaque
[address
], address
, data
);
301 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
303 #ifdef DEBUG_UNUSED_IOPORT
304 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
309 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
311 #ifdef DEBUG_UNUSED_IOPORT
312 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
316 /* default is to make two byte accesses */
317 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
320 data
= ioport_read(0, address
);
321 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
322 data
|= ioport_read(0, address
) << 8;
326 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
328 ioport_write(0, address
, data
& 0xff);
329 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
330 ioport_write(0, address
, (data
>> 8) & 0xff);
333 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
335 #ifdef DEBUG_UNUSED_IOPORT
336 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
341 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
343 #ifdef DEBUG_UNUSED_IOPORT
344 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
348 /* size is the word size in byte */
349 int register_ioport_read(int start
, int length
, int size
,
350 IOPortReadFunc
*func
, void *opaque
)
356 } else if (size
== 2) {
358 } else if (size
== 4) {
361 hw_error("register_ioport_read: invalid size");
364 for(i
= start
; i
< start
+ length
; i
+= size
) {
365 ioport_read_table
[bsize
][i
] = func
;
366 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
367 hw_error("register_ioport_read: invalid opaque");
368 ioport_opaque
[i
] = opaque
;
373 /* size is the word size in byte */
374 int register_ioport_write(int start
, int length
, int size
,
375 IOPortWriteFunc
*func
, void *opaque
)
381 } else if (size
== 2) {
383 } else if (size
== 4) {
386 hw_error("register_ioport_write: invalid size");
389 for(i
= start
; i
< start
+ length
; i
+= size
) {
390 ioport_write_table
[bsize
][i
] = func
;
391 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
392 hw_error("register_ioport_write: invalid opaque");
393 ioport_opaque
[i
] = opaque
;
398 void isa_unassign_ioport(int start
, int length
)
402 for(i
= start
; i
< start
+ length
; i
++) {
403 ioport_read_table
[0][i
] = default_ioport_readb
;
404 ioport_read_table
[1][i
] = default_ioport_readw
;
405 ioport_read_table
[2][i
] = default_ioport_readl
;
407 ioport_write_table
[0][i
] = default_ioport_writeb
;
408 ioport_write_table
[1][i
] = default_ioport_writew
;
409 ioport_write_table
[2][i
] = default_ioport_writel
;
413 /***********************************************************/
415 void cpu_outb(CPUState
*env
, int addr
, int val
)
417 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
418 ioport_write(0, addr
, val
);
421 env
->last_io_time
= cpu_get_time_fast();
425 void cpu_outw(CPUState
*env
, int addr
, int val
)
427 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
428 ioport_write(1, addr
, val
);
431 env
->last_io_time
= cpu_get_time_fast();
435 void cpu_outl(CPUState
*env
, int addr
, int val
)
437 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
438 ioport_write(2, addr
, val
);
441 env
->last_io_time
= cpu_get_time_fast();
445 int cpu_inb(CPUState
*env
, int addr
)
448 val
= ioport_read(0, addr
);
449 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
452 env
->last_io_time
= cpu_get_time_fast();
457 int cpu_inw(CPUState
*env
, int addr
)
460 val
= ioport_read(1, addr
);
461 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
464 env
->last_io_time
= cpu_get_time_fast();
469 int cpu_inl(CPUState
*env
, int addr
)
472 val
= ioport_read(2, addr
);
473 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
476 env
->last_io_time
= cpu_get_time_fast();
481 /***********************************************************/
482 void hw_error(const char *fmt
, ...)
488 fprintf(stderr
, "qemu: hardware error: ");
489 vfprintf(stderr
, fmt
, ap
);
490 fprintf(stderr
, "\n");
491 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
492 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
494 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
496 cpu_dump_state(env
, stderr
, fprintf
, 0);
506 static QEMUBalloonEvent
*qemu_balloon_event
;
507 void *qemu_balloon_event_opaque
;
509 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
511 qemu_balloon_event
= func
;
512 qemu_balloon_event_opaque
= opaque
;
515 void qemu_balloon(ram_addr_t target
)
517 if (qemu_balloon_event
)
518 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
521 ram_addr_t
qemu_balloon_status(void)
523 if (qemu_balloon_event
)
524 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
528 /***********************************************************/
531 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
532 static void *qemu_put_kbd_event_opaque
;
533 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
534 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
536 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
538 qemu_put_kbd_event_opaque
= opaque
;
539 qemu_put_kbd_event
= func
;
542 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
543 void *opaque
, int absolute
,
546 QEMUPutMouseEntry
*s
, *cursor
;
548 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
550 s
->qemu_put_mouse_event
= func
;
551 s
->qemu_put_mouse_event_opaque
= opaque
;
552 s
->qemu_put_mouse_event_absolute
= absolute
;
553 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
556 if (!qemu_put_mouse_event_head
) {
557 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
561 cursor
= qemu_put_mouse_event_head
;
562 while (cursor
->next
!= NULL
)
563 cursor
= cursor
->next
;
566 qemu_put_mouse_event_current
= s
;
571 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
573 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
575 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
578 cursor
= qemu_put_mouse_event_head
;
579 while (cursor
!= NULL
&& cursor
!= entry
) {
581 cursor
= cursor
->next
;
584 if (cursor
== NULL
) // does not exist or list empty
586 else if (prev
== NULL
) { // entry is head
587 qemu_put_mouse_event_head
= cursor
->next
;
588 if (qemu_put_mouse_event_current
== entry
)
589 qemu_put_mouse_event_current
= cursor
->next
;
590 qemu_free(entry
->qemu_put_mouse_event_name
);
595 prev
->next
= entry
->next
;
597 if (qemu_put_mouse_event_current
== entry
)
598 qemu_put_mouse_event_current
= prev
;
600 qemu_free(entry
->qemu_put_mouse_event_name
);
604 void kbd_put_keycode(int keycode
)
606 if (qemu_put_kbd_event
) {
607 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
611 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
613 QEMUPutMouseEvent
*mouse_event
;
614 void *mouse_event_opaque
;
617 if (!qemu_put_mouse_event_current
) {
622 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
624 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
627 if (graphic_rotate
) {
628 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
631 width
= graphic_width
- 1;
632 mouse_event(mouse_event_opaque
,
633 width
- dy
, dx
, dz
, buttons_state
);
635 mouse_event(mouse_event_opaque
,
636 dx
, dy
, dz
, buttons_state
);
640 int kbd_mouse_is_absolute(void)
642 if (!qemu_put_mouse_event_current
)
645 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
648 void do_info_mice(void)
650 QEMUPutMouseEntry
*cursor
;
653 if (!qemu_put_mouse_event_head
) {
654 term_printf("No mouse devices connected\n");
658 term_printf("Mouse devices available:\n");
659 cursor
= qemu_put_mouse_event_head
;
660 while (cursor
!= NULL
) {
661 term_printf("%c Mouse #%d: %s\n",
662 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
663 index
, cursor
->qemu_put_mouse_event_name
);
665 cursor
= cursor
->next
;
669 void do_mouse_set(int index
)
671 QEMUPutMouseEntry
*cursor
;
674 if (!qemu_put_mouse_event_head
) {
675 term_printf("No mouse devices connected\n");
679 cursor
= qemu_put_mouse_event_head
;
680 while (cursor
!= NULL
&& index
!= i
) {
682 cursor
= cursor
->next
;
686 qemu_put_mouse_event_current
= cursor
;
688 term_printf("Mouse at given index not found\n");
691 /* compute with 96 bit intermediate result: (a*b)/c */
692 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
697 #ifdef WORDS_BIGENDIAN
707 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
708 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
711 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
715 /***********************************************************/
716 /* real time host monotonic timer */
718 #define QEMU_TIMER_BASE 1000000000LL
722 static int64_t clock_freq
;
724 static void init_get_clock(void)
728 ret
= QueryPerformanceFrequency(&freq
);
730 fprintf(stderr
, "Could not calibrate ticks\n");
733 clock_freq
= freq
.QuadPart
;
736 static int64_t get_clock(void)
739 QueryPerformanceCounter(&ti
);
740 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
745 static int use_rt_clock
;
747 static void init_get_clock(void)
750 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
753 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
760 static int64_t get_clock(void)
762 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
765 clock_gettime(CLOCK_MONOTONIC
, &ts
);
766 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
770 /* XXX: using gettimeofday leads to problems if the date
771 changes, so it should be avoided. */
773 gettimeofday(&tv
, NULL
);
774 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
779 /* Return the virtual CPU time, based on the instruction counter. */
780 static int64_t cpu_get_icount(void)
783 CPUState
*env
= cpu_single_env
;;
784 icount
= qemu_icount
;
787 fprintf(stderr
, "Bad clock read\n");
788 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
790 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
793 /***********************************************************/
794 /* guest cycle counter */
796 static int64_t cpu_ticks_prev
;
797 static int64_t cpu_ticks_offset
;
798 static int64_t cpu_clock_offset
;
799 static int cpu_ticks_enabled
;
801 /* return the host CPU cycle counter and handle stop/restart */
802 int64_t cpu_get_ticks(void)
805 return cpu_get_icount();
807 if (!cpu_ticks_enabled
) {
808 return cpu_ticks_offset
;
811 ticks
= cpu_get_real_ticks();
812 if (cpu_ticks_prev
> ticks
) {
813 /* Note: non increasing ticks may happen if the host uses
815 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
817 cpu_ticks_prev
= ticks
;
818 return ticks
+ cpu_ticks_offset
;
822 /* return the host CPU monotonic timer and handle stop/restart */
823 static int64_t cpu_get_clock(void)
826 if (!cpu_ticks_enabled
) {
827 return cpu_clock_offset
;
830 return ti
+ cpu_clock_offset
;
834 /* enable cpu_get_ticks() */
835 void cpu_enable_ticks(void)
837 if (!cpu_ticks_enabled
) {
838 cpu_ticks_offset
-= cpu_get_real_ticks();
839 cpu_clock_offset
-= get_clock();
840 cpu_ticks_enabled
= 1;
844 /* disable cpu_get_ticks() : the clock is stopped. You must not call
845 cpu_get_ticks() after that. */
846 void cpu_disable_ticks(void)
848 if (cpu_ticks_enabled
) {
849 cpu_ticks_offset
= cpu_get_ticks();
850 cpu_clock_offset
= cpu_get_clock();
851 cpu_ticks_enabled
= 0;
855 /***********************************************************/
858 #define QEMU_TIMER_REALTIME 0
859 #define QEMU_TIMER_VIRTUAL 1
863 /* XXX: add frequency */
871 struct QEMUTimer
*next
;
874 struct qemu_alarm_timer
{
878 int (*start
)(struct qemu_alarm_timer
*t
);
879 void (*stop
)(struct qemu_alarm_timer
*t
);
880 void (*rearm
)(struct qemu_alarm_timer
*t
);
884 #define ALARM_FLAG_DYNTICKS 0x1
885 #define ALARM_FLAG_EXPIRED 0x2
887 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
889 return t
->flags
& ALARM_FLAG_DYNTICKS
;
892 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
894 if (!alarm_has_dynticks(t
))
900 /* TODO: MIN_TIMER_REARM_US should be optimized */
901 #define MIN_TIMER_REARM_US 250
903 static struct qemu_alarm_timer
*alarm_timer
;
905 static int alarm_timer_rfd
, alarm_timer_wfd
;
910 struct qemu_alarm_win32
{
914 } alarm_win32_data
= {0, NULL
, -1};
916 static int win32_start_timer(struct qemu_alarm_timer
*t
);
917 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
918 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
922 static int unix_start_timer(struct qemu_alarm_timer
*t
);
923 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
927 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
928 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
929 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
931 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
932 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
934 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
935 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
937 #endif /* __linux__ */
941 /* Correlation between real and virtual time is always going to be
942 fairly approximate, so ignore small variation.
943 When the guest is idle real and virtual time will be aligned in
945 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
947 static void icount_adjust(void)
952 static int64_t last_delta
;
953 /* If the VM is not running, then do nothing. */
957 cur_time
= cpu_get_clock();
958 cur_icount
= qemu_get_clock(vm_clock
);
959 delta
= cur_icount
- cur_time
;
960 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
962 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
963 && icount_time_shift
> 0) {
964 /* The guest is getting too far ahead. Slow time down. */
968 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
969 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
970 /* The guest is getting too far behind. Speed time up. */
974 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
977 static void icount_adjust_rt(void * opaque
)
979 qemu_mod_timer(icount_rt_timer
,
980 qemu_get_clock(rt_clock
) + 1000);
984 static void icount_adjust_vm(void * opaque
)
986 qemu_mod_timer(icount_vm_timer
,
987 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
991 static void init_icount_adjust(void)
993 /* Have both realtime and virtual time triggers for speed adjustment.
994 The realtime trigger catches emulated time passing too slowly,
995 the virtual time trigger catches emulated time passing too fast.
996 Realtime triggers occur even when idle, so use them less frequently
998 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
999 qemu_mod_timer(icount_rt_timer
,
1000 qemu_get_clock(rt_clock
) + 1000);
1001 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1002 qemu_mod_timer(icount_vm_timer
,
1003 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1006 static struct qemu_alarm_timer alarm_timers
[] = {
1009 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1010 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1011 /* HPET - if available - is preferred */
1012 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1013 /* ...otherwise try RTC */
1014 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1016 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1018 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1019 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1020 {"win32", 0, win32_start_timer
,
1021 win32_stop_timer
, NULL
, &alarm_win32_data
},
1026 static void show_available_alarms(void)
1030 printf("Available alarm timers, in order of precedence:\n");
1031 for (i
= 0; alarm_timers
[i
].name
; i
++)
1032 printf("%s\n", alarm_timers
[i
].name
);
1035 static void configure_alarms(char const *opt
)
1039 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1042 struct qemu_alarm_timer tmp
;
1044 if (!strcmp(opt
, "?")) {
1045 show_available_alarms();
1051 /* Reorder the array */
1052 name
= strtok(arg
, ",");
1054 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1055 if (!strcmp(alarm_timers
[i
].name
, name
))
1060 fprintf(stderr
, "Unknown clock %s\n", name
);
1069 tmp
= alarm_timers
[i
];
1070 alarm_timers
[i
] = alarm_timers
[cur
];
1071 alarm_timers
[cur
] = tmp
;
1075 name
= strtok(NULL
, ",");
1081 /* Disable remaining timers */
1082 for (i
= cur
; i
< count
; i
++)
1083 alarm_timers
[i
].name
= NULL
;
1085 show_available_alarms();
1090 QEMUClock
*rt_clock
;
1091 QEMUClock
*vm_clock
;
1093 static QEMUTimer
*active_timers
[2];
1095 static QEMUClock
*qemu_new_clock(int type
)
1098 clock
= qemu_mallocz(sizeof(QEMUClock
));
1103 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1107 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1110 ts
->opaque
= opaque
;
1114 void qemu_free_timer(QEMUTimer
*ts
)
1119 /* stop a timer, but do not dealloc it */
1120 void qemu_del_timer(QEMUTimer
*ts
)
1124 /* NOTE: this code must be signal safe because
1125 qemu_timer_expired() can be called from a signal. */
1126 pt
= &active_timers
[ts
->clock
->type
];
1139 /* modify the current timer so that it will be fired when current_time
1140 >= expire_time. The corresponding callback will be called. */
1141 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1147 /* add the timer in the sorted list */
1148 /* NOTE: this code must be signal safe because
1149 qemu_timer_expired() can be called from a signal. */
1150 pt
= &active_timers
[ts
->clock
->type
];
1155 if (t
->expire_time
> expire_time
)
1159 ts
->expire_time
= expire_time
;
1163 /* Rearm if necessary */
1164 if (pt
== &active_timers
[ts
->clock
->type
]) {
1165 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1166 qemu_rearm_alarm_timer(alarm_timer
);
1168 /* Interrupt execution to force deadline recalculation. */
1169 if (use_icount
&& cpu_single_env
) {
1170 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1175 int qemu_timer_pending(QEMUTimer
*ts
)
1178 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1185 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1189 return (timer_head
->expire_time
<= current_time
);
1192 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1198 if (!ts
|| ts
->expire_time
> current_time
)
1200 /* remove timer from the list before calling the callback */
1201 *ptimer_head
= ts
->next
;
1204 /* run the callback (the timer list can be modified) */
1209 int64_t qemu_get_clock(QEMUClock
*clock
)
1211 switch(clock
->type
) {
1212 case QEMU_TIMER_REALTIME
:
1213 return get_clock() / 1000000;
1215 case QEMU_TIMER_VIRTUAL
:
1217 return cpu_get_icount();
1219 return cpu_get_clock();
1224 static void init_timers(void)
1227 ticks_per_sec
= QEMU_TIMER_BASE
;
1228 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1229 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1233 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1235 uint64_t expire_time
;
1237 if (qemu_timer_pending(ts
)) {
1238 expire_time
= ts
->expire_time
;
1242 qemu_put_be64(f
, expire_time
);
1245 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1247 uint64_t expire_time
;
1249 expire_time
= qemu_get_be64(f
);
1250 if (expire_time
!= -1) {
1251 qemu_mod_timer(ts
, expire_time
);
1257 static void timer_save(QEMUFile
*f
, void *opaque
)
1259 if (cpu_ticks_enabled
) {
1260 hw_error("cannot save state if virtual timers are running");
1262 qemu_put_be64(f
, cpu_ticks_offset
);
1263 qemu_put_be64(f
, ticks_per_sec
);
1264 qemu_put_be64(f
, cpu_clock_offset
);
1267 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1269 if (version_id
!= 1 && version_id
!= 2)
1271 if (cpu_ticks_enabled
) {
1274 cpu_ticks_offset
=qemu_get_be64(f
);
1275 ticks_per_sec
=qemu_get_be64(f
);
1276 if (version_id
== 2) {
1277 cpu_clock_offset
=qemu_get_be64(f
);
1283 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1284 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1286 static void host_alarm_handler(int host_signum
)
1290 #define DISP_FREQ 1000
1292 static int64_t delta_min
= INT64_MAX
;
1293 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1295 ti
= qemu_get_clock(vm_clock
);
1296 if (last_clock
!= 0) {
1297 delta
= ti
- last_clock
;
1298 if (delta
< delta_min
)
1300 if (delta
> delta_max
)
1303 if (++count
== DISP_FREQ
) {
1304 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1305 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1306 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1307 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1308 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1310 delta_min
= INT64_MAX
;
1318 if (alarm_has_dynticks(alarm_timer
) ||
1320 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1321 qemu_get_clock(vm_clock
))) ||
1322 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1323 qemu_get_clock(rt_clock
))) {
1324 CPUState
*env
= next_cpu
;
1327 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1328 SetEvent(data
->host_alarm
);
1330 static const char byte
= 0;
1331 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1333 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1336 /* stop the currently executing cpu because a timer occured */
1337 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1339 if (env
->kqemu_enabled
) {
1340 kqemu_cpu_interrupt(env
);
1348 static int64_t qemu_next_deadline(void)
1352 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1353 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1354 qemu_get_clock(vm_clock
);
1356 /* To avoid problems with overflow limit this to 2^32. */
1366 #if defined(__linux__) || defined(_WIN32)
1367 static uint64_t qemu_next_deadline_dyntick(void)
1375 delta
= (qemu_next_deadline() + 999) / 1000;
1377 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1378 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1379 qemu_get_clock(rt_clock
))*1000;
1380 if (rtdelta
< delta
)
1384 if (delta
< MIN_TIMER_REARM_US
)
1385 delta
= MIN_TIMER_REARM_US
;
1393 /* Sets a specific flag */
1394 static int fcntl_setfl(int fd
, int flag
)
1398 flags
= fcntl(fd
, F_GETFL
);
1402 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1408 #if defined(__linux__)
1410 #define RTC_FREQ 1024
1412 static void enable_sigio_timer(int fd
)
1414 struct sigaction act
;
1417 sigfillset(&act
.sa_mask
);
1419 act
.sa_handler
= host_alarm_handler
;
1421 sigaction(SIGIO
, &act
, NULL
);
1422 fcntl_setfl(fd
, O_ASYNC
);
1423 fcntl(fd
, F_SETOWN
, getpid());
1426 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1428 struct hpet_info info
;
1431 fd
= open("/dev/hpet", O_RDONLY
);
1436 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1438 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1439 "error, but for better emulation accuracy type:\n"
1440 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1444 /* Check capabilities */
1445 r
= ioctl(fd
, HPET_INFO
, &info
);
1449 /* Enable periodic mode */
1450 r
= ioctl(fd
, HPET_EPI
, 0);
1451 if (info
.hi_flags
&& (r
< 0))
1454 /* Enable interrupt */
1455 r
= ioctl(fd
, HPET_IE_ON
, 0);
1459 enable_sigio_timer(fd
);
1460 t
->priv
= (void *)(long)fd
;
1468 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1470 int fd
= (long)t
->priv
;
1475 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1478 unsigned long current_rtc_freq
= 0;
1480 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1483 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1484 if (current_rtc_freq
!= RTC_FREQ
&&
1485 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1486 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1487 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1488 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1491 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1497 enable_sigio_timer(rtc_fd
);
1499 t
->priv
= (void *)(long)rtc_fd
;
1504 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1506 int rtc_fd
= (long)t
->priv
;
1511 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1515 struct sigaction act
;
1517 sigfillset(&act
.sa_mask
);
1519 act
.sa_handler
= host_alarm_handler
;
1521 sigaction(SIGALRM
, &act
, NULL
);
1523 ev
.sigev_value
.sival_int
= 0;
1524 ev
.sigev_notify
= SIGEV_SIGNAL
;
1525 ev
.sigev_signo
= SIGALRM
;
1527 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1528 perror("timer_create");
1530 /* disable dynticks */
1531 fprintf(stderr
, "Dynamic Ticks disabled\n");
1536 t
->priv
= (void *)(long)host_timer
;
1541 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1543 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1545 timer_delete(host_timer
);
1548 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1550 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1551 struct itimerspec timeout
;
1552 int64_t nearest_delta_us
= INT64_MAX
;
1555 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1556 !active_timers
[QEMU_TIMER_VIRTUAL
])
1559 nearest_delta_us
= qemu_next_deadline_dyntick();
1561 /* check whether a timer is already running */
1562 if (timer_gettime(host_timer
, &timeout
)) {
1564 fprintf(stderr
, "Internal timer error: aborting\n");
1567 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1568 if (current_us
&& current_us
<= nearest_delta_us
)
1571 timeout
.it_interval
.tv_sec
= 0;
1572 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1573 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1574 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1575 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1577 fprintf(stderr
, "Internal timer error: aborting\n");
1582 #endif /* defined(__linux__) */
1584 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1586 struct sigaction act
;
1587 struct itimerval itv
;
1591 sigfillset(&act
.sa_mask
);
1593 act
.sa_handler
= host_alarm_handler
;
1595 sigaction(SIGALRM
, &act
, NULL
);
1597 itv
.it_interval
.tv_sec
= 0;
1598 /* for i386 kernel 2.6 to get 1 ms */
1599 itv
.it_interval
.tv_usec
= 999;
1600 itv
.it_value
.tv_sec
= 0;
1601 itv
.it_value
.tv_usec
= 10 * 1000;
1603 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1610 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1612 struct itimerval itv
;
1614 memset(&itv
, 0, sizeof(itv
));
1615 setitimer(ITIMER_REAL
, &itv
, NULL
);
1618 #endif /* !defined(_WIN32) */
1620 static void try_to_rearm_timer(void *opaque
)
1622 struct qemu_alarm_timer
*t
= opaque
;
1626 /* Drain the notify pipe */
1629 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1630 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1633 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1634 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1635 qemu_rearm_alarm_timer(alarm_timer
);
1641 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1644 struct qemu_alarm_win32
*data
= t
->priv
;
1647 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1648 if (!data
->host_alarm
) {
1649 perror("Failed CreateEvent");
1653 memset(&tc
, 0, sizeof(tc
));
1654 timeGetDevCaps(&tc
, sizeof(tc
));
1656 if (data
->period
< tc
.wPeriodMin
)
1657 data
->period
= tc
.wPeriodMin
;
1659 timeBeginPeriod(data
->period
);
1661 flags
= TIME_CALLBACK_FUNCTION
;
1662 if (alarm_has_dynticks(t
))
1663 flags
|= TIME_ONESHOT
;
1665 flags
|= TIME_PERIODIC
;
1667 data
->timerId
= timeSetEvent(1, // interval (ms)
1668 data
->period
, // resolution
1669 host_alarm_handler
, // function
1670 (DWORD
)t
, // parameter
1673 if (!data
->timerId
) {
1674 perror("Failed to initialize win32 alarm timer");
1676 timeEndPeriod(data
->period
);
1677 CloseHandle(data
->host_alarm
);
1681 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1686 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1688 struct qemu_alarm_win32
*data
= t
->priv
;
1690 timeKillEvent(data
->timerId
);
1691 timeEndPeriod(data
->period
);
1693 CloseHandle(data
->host_alarm
);
1696 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1698 struct qemu_alarm_win32
*data
= t
->priv
;
1699 uint64_t nearest_delta_us
;
1701 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1702 !active_timers
[QEMU_TIMER_VIRTUAL
])
1705 nearest_delta_us
= qemu_next_deadline_dyntick();
1706 nearest_delta_us
/= 1000;
1708 timeKillEvent(data
->timerId
);
1710 data
->timerId
= timeSetEvent(1,
1714 TIME_ONESHOT
| TIME_PERIODIC
);
1716 if (!data
->timerId
) {
1717 perror("Failed to re-arm win32 alarm timer");
1719 timeEndPeriod(data
->period
);
1720 CloseHandle(data
->host_alarm
);
1727 static int init_timer_alarm(void)
1729 struct qemu_alarm_timer
*t
= NULL
;
1739 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1743 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1747 alarm_timer_rfd
= fds
[0];
1748 alarm_timer_wfd
= fds
[1];
1751 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1752 t
= &alarm_timers
[i
];
1765 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1766 try_to_rearm_timer
, NULL
, t
);
1781 static void quit_timers(void)
1783 alarm_timer
->stop(alarm_timer
);
1787 /***********************************************************/
1788 /* host time/date access */
1789 void qemu_get_timedate(struct tm
*tm
, int offset
)
1796 if (rtc_date_offset
== -1) {
1800 ret
= localtime(&ti
);
1802 ti
-= rtc_date_offset
;
1806 memcpy(tm
, ret
, sizeof(struct tm
));
1809 int qemu_timedate_diff(struct tm
*tm
)
1813 if (rtc_date_offset
== -1)
1815 seconds
= mktimegm(tm
);
1817 seconds
= mktime(tm
);
1819 seconds
= mktimegm(tm
) + rtc_date_offset
;
1821 return seconds
- time(NULL
);
1825 static void socket_cleanup(void)
1830 static int socket_init(void)
1835 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1837 err
= WSAGetLastError();
1838 fprintf(stderr
, "WSAStartup: %d\n", err
);
1841 atexit(socket_cleanup
);
1846 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1851 while (*p
!= '\0' && *p
!= '=') {
1852 if (q
&& (q
- buf
) < buf_size
- 1)
1862 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1867 while (*p
!= '\0') {
1869 if (*(p
+ 1) != ',')
1873 if (q
&& (q
- buf
) < buf_size
- 1)
1883 int get_param_value(char *buf
, int buf_size
,
1884 const char *tag
, const char *str
)
1891 p
= get_opt_name(option
, sizeof(option
), p
);
1895 if (!strcmp(tag
, option
)) {
1896 (void)get_opt_value(buf
, buf_size
, p
);
1899 p
= get_opt_value(NULL
, 0, p
);
1908 int check_params(char *buf
, int buf_size
,
1909 const char * const *params
, const char *str
)
1916 p
= get_opt_name(buf
, buf_size
, p
);
1920 for(i
= 0; params
[i
] != NULL
; i
++)
1921 if (!strcmp(params
[i
], buf
))
1923 if (params
[i
] == NULL
)
1925 p
= get_opt_value(NULL
, 0, p
);
1933 /***********************************************************/
1934 /* Bluetooth support */
1937 static struct HCIInfo
*hci_table
[MAX_NICS
];
1939 static struct bt_vlan_s
{
1940 struct bt_scatternet_s net
;
1942 struct bt_vlan_s
*next
;
1945 /* find or alloc a new bluetooth "VLAN" */
1946 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1948 struct bt_vlan_s
**pvlan
, *vlan
;
1949 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1953 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1955 pvlan
= &first_bt_vlan
;
1956 while (*pvlan
!= NULL
)
1957 pvlan
= &(*pvlan
)->next
;
1962 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1966 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1971 static struct HCIInfo null_hci
= {
1972 .cmd_send
= null_hci_send
,
1973 .sco_send
= null_hci_send
,
1974 .acl_send
= null_hci_send
,
1975 .bdaddr_set
= null_hci_addr_set
,
1978 struct HCIInfo
*qemu_next_hci(void)
1980 if (cur_hci
== nb_hcis
)
1983 return hci_table
[cur_hci
++];
1986 static struct HCIInfo
*hci_init(const char *str
)
1989 struct bt_scatternet_s
*vlan
= 0;
1991 if (!strcmp(str
, "null"))
1994 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1996 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1997 else if (!strncmp(str
, "hci", 3)) {
2000 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2001 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2006 vlan
= qemu_find_bt_vlan(0);
2008 return bt_new_hci(vlan
);
2011 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2016 static int bt_hci_parse(const char *str
)
2018 struct HCIInfo
*hci
;
2021 if (nb_hcis
>= MAX_NICS
) {
2022 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2026 hci
= hci_init(str
);
2035 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2036 hci
->bdaddr_set(hci
, bdaddr
.b
);
2038 hci_table
[nb_hcis
++] = hci
;
2043 static void bt_vhci_add(int vlan_id
)
2045 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2048 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2049 "an empty scatternet %i\n", vlan_id
);
2051 bt_vhci_init(bt_new_hci(vlan
));
2054 static struct bt_device_s
*bt_device_add(const char *opt
)
2056 struct bt_scatternet_s
*vlan
;
2058 char *endp
= strstr(opt
, ",vlan=");
2059 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2062 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2065 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2067 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2072 vlan
= qemu_find_bt_vlan(vlan_id
);
2075 fprintf(stderr
, "qemu: warning: adding a slave device to "
2076 "an empty scatternet %i\n", vlan_id
);
2078 if (!strcmp(devname
, "keyboard"))
2079 return bt_keyboard_init(vlan
);
2081 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2085 static int bt_parse(const char *opt
)
2087 const char *endp
, *p
;
2090 if (strstart(opt
, "hci", &endp
)) {
2091 if (!*endp
|| *endp
== ',') {
2093 if (!strstart(endp
, ",vlan=", 0))
2096 return bt_hci_parse(opt
);
2098 } else if (strstart(opt
, "vhci", &endp
)) {
2099 if (!*endp
|| *endp
== ',') {
2101 if (strstart(endp
, ",vlan=", &p
)) {
2102 vlan
= strtol(p
, (char **) &endp
, 0);
2104 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2108 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2117 } else if (strstart(opt
, "device:", &endp
))
2118 return !bt_device_add(endp
);
2120 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2124 /***********************************************************/
2125 /* QEMU Block devices */
2127 #define HD_ALIAS "index=%d,media=disk"
2129 #define CDROM_ALIAS "index=1,media=cdrom"
2131 #define CDROM_ALIAS "index=2,media=cdrom"
2133 #define FD_ALIAS "index=%d,if=floppy"
2134 #define PFLASH_ALIAS "if=pflash"
2135 #define MTD_ALIAS "if=mtd"
2136 #define SD_ALIAS "index=0,if=sd"
2138 static int drive_add(const char *file
, const char *fmt
, ...)
2142 if (nb_drives_opt
>= MAX_DRIVES
) {
2143 fprintf(stderr
, "qemu: too many drives\n");
2147 drives_opt
[nb_drives_opt
].file
= file
;
2149 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2150 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2153 return nb_drives_opt
++;
2156 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2160 /* seek interface, bus and unit */
2162 for (index
= 0; index
< nb_drives
; index
++)
2163 if (drives_table
[index
].type
== type
&&
2164 drives_table
[index
].bus
== bus
&&
2165 drives_table
[index
].unit
== unit
)
2171 int drive_get_max_bus(BlockInterfaceType type
)
2177 for (index
= 0; index
< nb_drives
; index
++) {
2178 if(drives_table
[index
].type
== type
&&
2179 drives_table
[index
].bus
> max_bus
)
2180 max_bus
= drives_table
[index
].bus
;
2185 const char *drive_get_serial(BlockDriverState
*bdrv
)
2189 for (index
= 0; index
< nb_drives
; index
++)
2190 if (drives_table
[index
].bdrv
== bdrv
)
2191 return drives_table
[index
].serial
;
2196 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2200 for (index
= 0; index
< nb_drives
; index
++)
2201 if (drives_table
[index
].bdrv
== bdrv
)
2202 return drives_table
[index
].onerror
;
2204 return BLOCK_ERR_REPORT
;
2207 static void bdrv_format_print(void *opaque
, const char *name
)
2209 fprintf(stderr
, " %s", name
);
2212 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2213 QEMUMachine
*machine
)
2219 const char *mediastr
= "";
2220 BlockInterfaceType type
;
2221 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2222 int bus_id
, unit_id
;
2223 int cyls
, heads
, secs
, translation
;
2224 BlockDriverState
*bdrv
;
2225 BlockDriver
*drv
= NULL
;
2229 int bdrv_flags
, onerror
;
2230 char *str
= arg
->opt
;
2231 static const char * const params
[] = { "bus", "unit", "if", "index",
2232 "cyls", "heads", "secs", "trans",
2233 "media", "snapshot", "file",
2234 "cache", "format", "serial", "werror",
2237 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2238 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2244 cyls
= heads
= secs
= 0;
2247 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2251 if (machine
->use_scsi
) {
2253 max_devs
= MAX_SCSI_DEVS
;
2254 pstrcpy(devname
, sizeof(devname
), "scsi");
2257 max_devs
= MAX_IDE_DEVS
;
2258 pstrcpy(devname
, sizeof(devname
), "ide");
2262 /* extract parameters */
2264 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2265 bus_id
= strtol(buf
, NULL
, 0);
2267 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2272 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2273 unit_id
= strtol(buf
, NULL
, 0);
2275 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2280 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2281 pstrcpy(devname
, sizeof(devname
), buf
);
2282 if (!strcmp(buf
, "ide")) {
2284 max_devs
= MAX_IDE_DEVS
;
2285 } else if (!strcmp(buf
, "scsi")) {
2287 max_devs
= MAX_SCSI_DEVS
;
2288 } else if (!strcmp(buf
, "floppy")) {
2291 } else if (!strcmp(buf
, "pflash")) {
2294 } else if (!strcmp(buf
, "mtd")) {
2297 } else if (!strcmp(buf
, "sd")) {
2300 } else if (!strcmp(buf
, "virtio")) {
2304 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2309 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2310 index
= strtol(buf
, NULL
, 0);
2312 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2317 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2318 cyls
= strtol(buf
, NULL
, 0);
2321 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2322 heads
= strtol(buf
, NULL
, 0);
2325 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2326 secs
= strtol(buf
, NULL
, 0);
2329 if (cyls
|| heads
|| secs
) {
2330 if (cyls
< 1 || cyls
> 16383) {
2331 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2334 if (heads
< 1 || heads
> 16) {
2335 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2338 if (secs
< 1 || secs
> 63) {
2339 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2344 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2347 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2351 if (!strcmp(buf
, "none"))
2352 translation
= BIOS_ATA_TRANSLATION_NONE
;
2353 else if (!strcmp(buf
, "lba"))
2354 translation
= BIOS_ATA_TRANSLATION_LBA
;
2355 else if (!strcmp(buf
, "auto"))
2356 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2358 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2363 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2364 if (!strcmp(buf
, "disk")) {
2366 } else if (!strcmp(buf
, "cdrom")) {
2367 if (cyls
|| secs
|| heads
) {
2369 "qemu: '%s' invalid physical CHS format\n", str
);
2372 media
= MEDIA_CDROM
;
2374 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2379 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2380 if (!strcmp(buf
, "on"))
2382 else if (!strcmp(buf
, "off"))
2385 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2390 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2391 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2393 else if (!strcmp(buf
, "writethrough"))
2395 else if (!strcmp(buf
, "writeback"))
2398 fprintf(stderr
, "qemu: invalid cache option\n");
2403 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2404 if (strcmp(buf
, "?") == 0) {
2405 fprintf(stderr
, "qemu: Supported formats:");
2406 bdrv_iterate_format(bdrv_format_print
, NULL
);
2407 fprintf(stderr
, "\n");
2410 drv
= bdrv_find_format(buf
);
2412 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2417 if (arg
->file
== NULL
)
2418 get_param_value(file
, sizeof(file
), "file", str
);
2420 pstrcpy(file
, sizeof(file
), arg
->file
);
2422 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2423 memset(serial
, 0, sizeof(serial
));
2425 onerror
= BLOCK_ERR_REPORT
;
2426 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2427 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2428 fprintf(stderr
, "werror is no supported by this format\n");
2431 if (!strcmp(buf
, "ignore"))
2432 onerror
= BLOCK_ERR_IGNORE
;
2433 else if (!strcmp(buf
, "enospc"))
2434 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2435 else if (!strcmp(buf
, "stop"))
2436 onerror
= BLOCK_ERR_STOP_ANY
;
2437 else if (!strcmp(buf
, "report"))
2438 onerror
= BLOCK_ERR_REPORT
;
2440 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2445 /* compute bus and unit according index */
2448 if (bus_id
!= 0 || unit_id
!= -1) {
2450 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2458 unit_id
= index
% max_devs
;
2459 bus_id
= index
/ max_devs
;
2463 /* if user doesn't specify a unit_id,
2464 * try to find the first free
2467 if (unit_id
== -1) {
2469 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2471 if (max_devs
&& unit_id
>= max_devs
) {
2472 unit_id
-= max_devs
;
2480 if (max_devs
&& unit_id
>= max_devs
) {
2481 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2482 str
, unit_id
, max_devs
- 1);
2487 * ignore multiple definitions
2490 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2495 if (type
== IF_IDE
|| type
== IF_SCSI
)
2496 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2498 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2499 devname
, bus_id
, mediastr
, unit_id
);
2501 snprintf(buf
, sizeof(buf
), "%s%s%i",
2502 devname
, mediastr
, unit_id
);
2503 bdrv
= bdrv_new(buf
);
2504 drives_table
[nb_drives
].bdrv
= bdrv
;
2505 drives_table
[nb_drives
].type
= type
;
2506 drives_table
[nb_drives
].bus
= bus_id
;
2507 drives_table
[nb_drives
].unit
= unit_id
;
2508 drives_table
[nb_drives
].onerror
= onerror
;
2509 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2518 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2519 bdrv_set_translation_hint(bdrv
, translation
);
2523 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2528 /* FIXME: This isn't really a floppy, but it's a reasonable
2531 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2542 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2543 cache
= 2; /* always use write-back with snapshot */
2545 if (cache
== 0) /* no caching */
2546 bdrv_flags
|= BDRV_O_NOCACHE
;
2547 else if (cache
== 2) /* write-back */
2548 bdrv_flags
|= BDRV_O_CACHE_WB
;
2549 else if (cache
== 3) /* not specified */
2550 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2551 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2552 fprintf(stderr
, "qemu: could not open disk image %s\n",
2559 /***********************************************************/
2562 static USBPort
*used_usb_ports
;
2563 static USBPort
*free_usb_ports
;
2565 /* ??? Maybe change this to register a hub to keep track of the topology. */
2566 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2567 usb_attachfn attach
)
2569 port
->opaque
= opaque
;
2570 port
->index
= index
;
2571 port
->attach
= attach
;
2572 port
->next
= free_usb_ports
;
2573 free_usb_ports
= port
;
2576 int usb_device_add_dev(USBDevice
*dev
)
2580 /* Find a USB port to add the device to. */
2581 port
= free_usb_ports
;
2585 /* Create a new hub and chain it on. */
2586 free_usb_ports
= NULL
;
2587 port
->next
= used_usb_ports
;
2588 used_usb_ports
= port
;
2590 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2591 usb_attach(port
, hub
);
2592 port
= free_usb_ports
;
2595 free_usb_ports
= port
->next
;
2596 port
->next
= used_usb_ports
;
2597 used_usb_ports
= port
;
2598 usb_attach(port
, dev
);
2602 static int usb_device_add(const char *devname
)
2607 if (!free_usb_ports
)
2610 if (strstart(devname
, "host:", &p
)) {
2611 dev
= usb_host_device_open(p
);
2612 } else if (!strcmp(devname
, "mouse")) {
2613 dev
= usb_mouse_init();
2614 } else if (!strcmp(devname
, "tablet")) {
2615 dev
= usb_tablet_init();
2616 } else if (!strcmp(devname
, "keyboard")) {
2617 dev
= usb_keyboard_init();
2618 } else if (strstart(devname
, "disk:", &p
)) {
2619 dev
= usb_msd_init(p
);
2620 } else if (!strcmp(devname
, "wacom-tablet")) {
2621 dev
= usb_wacom_init();
2622 } else if (strstart(devname
, "serial:", &p
)) {
2623 dev
= usb_serial_init(p
);
2624 #ifdef CONFIG_BRLAPI
2625 } else if (!strcmp(devname
, "braille")) {
2626 dev
= usb_baum_init();
2628 } else if (strstart(devname
, "net:", &p
)) {
2631 if (net_client_init("nic", p
) < 0)
2633 nd_table
[nic
].model
= "usb";
2634 dev
= usb_net_init(&nd_table
[nic
]);
2635 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2636 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2637 bt_new_hci(qemu_find_bt_vlan(0)));
2644 return usb_device_add_dev(dev
);
2647 int usb_device_del_addr(int bus_num
, int addr
)
2653 if (!used_usb_ports
)
2659 lastp
= &used_usb_ports
;
2660 port
= used_usb_ports
;
2661 while (port
&& port
->dev
->addr
!= addr
) {
2662 lastp
= &port
->next
;
2670 *lastp
= port
->next
;
2671 usb_attach(port
, NULL
);
2672 dev
->handle_destroy(dev
);
2673 port
->next
= free_usb_ports
;
2674 free_usb_ports
= port
;
2678 static int usb_device_del(const char *devname
)
2683 if (strstart(devname
, "host:", &p
))
2684 return usb_host_device_close(p
);
2686 if (!used_usb_ports
)
2689 p
= strchr(devname
, '.');
2692 bus_num
= strtoul(devname
, NULL
, 0);
2693 addr
= strtoul(p
+ 1, NULL
, 0);
2695 return usb_device_del_addr(bus_num
, addr
);
2698 void do_usb_add(const char *devname
)
2700 usb_device_add(devname
);
2703 void do_usb_del(const char *devname
)
2705 usb_device_del(devname
);
2712 const char *speed_str
;
2715 term_printf("USB support not enabled\n");
2719 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2723 switch(dev
->speed
) {
2727 case USB_SPEED_FULL
:
2730 case USB_SPEED_HIGH
:
2737 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2738 0, dev
->addr
, speed_str
, dev
->devname
);
2742 /***********************************************************/
2743 /* PCMCIA/Cardbus */
2745 static struct pcmcia_socket_entry_s
{
2746 struct pcmcia_socket_s
*socket
;
2747 struct pcmcia_socket_entry_s
*next
;
2748 } *pcmcia_sockets
= 0;
2750 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2752 struct pcmcia_socket_entry_s
*entry
;
2754 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2755 entry
->socket
= socket
;
2756 entry
->next
= pcmcia_sockets
;
2757 pcmcia_sockets
= entry
;
2760 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2762 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2764 ptr
= &pcmcia_sockets
;
2765 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2766 if (entry
->socket
== socket
) {
2772 void pcmcia_info(void)
2774 struct pcmcia_socket_entry_s
*iter
;
2775 if (!pcmcia_sockets
)
2776 term_printf("No PCMCIA sockets\n");
2778 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2779 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2780 iter
->socket
->attached
? iter
->socket
->card_string
:
2784 /***********************************************************/
2785 /* register display */
2787 void register_displaystate(DisplayState
*ds
)
2797 DisplayState
*get_displaystate(void)
2799 return display_state
;
2804 static void dumb_display_init(void)
2806 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2807 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2808 register_displaystate(ds
);
2811 /***********************************************************/
2814 #define MAX_IO_HANDLERS 64
2816 typedef struct IOHandlerRecord
{
2818 IOCanRWHandler
*fd_read_poll
;
2820 IOHandler
*fd_write
;
2823 /* temporary data */
2825 struct IOHandlerRecord
*next
;
2828 static IOHandlerRecord
*first_io_handler
;
2830 /* XXX: fd_read_poll should be suppressed, but an API change is
2831 necessary in the character devices to suppress fd_can_read(). */
2832 int qemu_set_fd_handler2(int fd
,
2833 IOCanRWHandler
*fd_read_poll
,
2835 IOHandler
*fd_write
,
2838 IOHandlerRecord
**pioh
, *ioh
;
2840 if (!fd_read
&& !fd_write
) {
2841 pioh
= &first_io_handler
;
2846 if (ioh
->fd
== fd
) {
2853 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2857 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2858 ioh
->next
= first_io_handler
;
2859 first_io_handler
= ioh
;
2862 ioh
->fd_read_poll
= fd_read_poll
;
2863 ioh
->fd_read
= fd_read
;
2864 ioh
->fd_write
= fd_write
;
2865 ioh
->opaque
= opaque
;
2871 int qemu_set_fd_handler(int fd
,
2873 IOHandler
*fd_write
,
2876 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2880 /***********************************************************/
2881 /* Polling handling */
2883 typedef struct PollingEntry
{
2886 struct PollingEntry
*next
;
2889 static PollingEntry
*first_polling_entry
;
2891 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2893 PollingEntry
**ppe
, *pe
;
2894 pe
= qemu_mallocz(sizeof(PollingEntry
));
2896 pe
->opaque
= opaque
;
2897 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2902 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2904 PollingEntry
**ppe
, *pe
;
2905 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2907 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2915 /***********************************************************/
2916 /* Wait objects support */
2917 typedef struct WaitObjects
{
2919 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2920 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2921 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2924 static WaitObjects wait_objects
= {0};
2926 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2928 WaitObjects
*w
= &wait_objects
;
2930 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2932 w
->events
[w
->num
] = handle
;
2933 w
->func
[w
->num
] = func
;
2934 w
->opaque
[w
->num
] = opaque
;
2939 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2942 WaitObjects
*w
= &wait_objects
;
2945 for (i
= 0; i
< w
->num
; i
++) {
2946 if (w
->events
[i
] == handle
)
2949 w
->events
[i
] = w
->events
[i
+ 1];
2950 w
->func
[i
] = w
->func
[i
+ 1];
2951 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2959 /***********************************************************/
2960 /* ram save/restore */
2962 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2966 v
= qemu_get_byte(f
);
2969 if (qemu_get_buffer(f
, buf
, len
) != len
)
2973 v
= qemu_get_byte(f
);
2974 memset(buf
, v
, len
);
2980 if (qemu_file_has_error(f
))
2986 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2991 if (qemu_get_be32(f
) != phys_ram_size
)
2993 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2994 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3001 #define BDRV_HASH_BLOCK_SIZE 1024
3002 #define IOBUF_SIZE 4096
3003 #define RAM_CBLOCK_MAGIC 0xfabe
3005 typedef struct RamDecompressState
{
3008 uint8_t buf
[IOBUF_SIZE
];
3009 } RamDecompressState
;
3011 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3014 memset(s
, 0, sizeof(*s
));
3016 ret
= inflateInit(&s
->zstream
);
3022 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3026 s
->zstream
.avail_out
= len
;
3027 s
->zstream
.next_out
= buf
;
3028 while (s
->zstream
.avail_out
> 0) {
3029 if (s
->zstream
.avail_in
== 0) {
3030 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3032 clen
= qemu_get_be16(s
->f
);
3033 if (clen
> IOBUF_SIZE
)
3035 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3036 s
->zstream
.avail_in
= clen
;
3037 s
->zstream
.next_in
= s
->buf
;
3039 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3040 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3047 static void ram_decompress_close(RamDecompressState
*s
)
3049 inflateEnd(&s
->zstream
);
3052 #define RAM_SAVE_FLAG_FULL 0x01
3053 #define RAM_SAVE_FLAG_COMPRESS 0x02
3054 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3055 #define RAM_SAVE_FLAG_PAGE 0x08
3056 #define RAM_SAVE_FLAG_EOS 0x10
3058 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3060 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3061 uint32_t *array
= (uint32_t *)page
;
3064 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3065 if (array
[i
] != val
)
3072 static int ram_save_block(QEMUFile
*f
)
3074 static ram_addr_t current_addr
= 0;
3075 ram_addr_t saved_addr
= current_addr
;
3076 ram_addr_t addr
= 0;
3079 while (addr
< phys_ram_size
) {
3080 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3083 cpu_physical_memory_reset_dirty(current_addr
,
3084 current_addr
+ TARGET_PAGE_SIZE
,
3085 MIGRATION_DIRTY_FLAG
);
3087 ch
= *(phys_ram_base
+ current_addr
);
3089 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3090 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3091 qemu_put_byte(f
, ch
);
3093 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3094 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3100 addr
+= TARGET_PAGE_SIZE
;
3101 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3107 static ram_addr_t ram_save_threshold
= 10;
3109 static ram_addr_t
ram_save_remaining(void)
3112 ram_addr_t count
= 0;
3114 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3115 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3122 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3127 /* Make sure all dirty bits are set */
3128 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3129 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3130 cpu_physical_memory_set_dirty(addr
);
3133 /* Enable dirty memory tracking */
3134 cpu_physical_memory_set_dirty_tracking(1);
3136 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3139 while (!qemu_file_rate_limit(f
)) {
3142 ret
= ram_save_block(f
);
3143 if (ret
== 0) /* no more blocks */
3147 /* try transferring iterative blocks of memory */
3150 cpu_physical_memory_set_dirty_tracking(0);
3152 /* flush all remaining blocks regardless of rate limiting */
3153 while (ram_save_block(f
) != 0);
3156 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3158 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3161 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3163 RamDecompressState s1
, *s
= &s1
;
3167 if (ram_decompress_open(s
, f
) < 0)
3169 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3170 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3171 fprintf(stderr
, "Error while reading ram block header\n");
3175 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3176 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3181 printf("Error block header\n");
3185 ram_decompress_close(s
);
3190 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3195 if (version_id
== 1)
3196 return ram_load_v1(f
, opaque
);
3198 if (version_id
== 2) {
3199 if (qemu_get_be32(f
) != phys_ram_size
)
3201 return ram_load_dead(f
, opaque
);
3204 if (version_id
!= 3)
3208 addr
= qemu_get_be64(f
);
3210 flags
= addr
& ~TARGET_PAGE_MASK
;
3211 addr
&= TARGET_PAGE_MASK
;
3213 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3214 if (addr
!= phys_ram_size
)
3218 if (flags
& RAM_SAVE_FLAG_FULL
) {
3219 if (ram_load_dead(f
, opaque
) < 0)
3223 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3224 uint8_t ch
= qemu_get_byte(f
);
3225 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3226 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3227 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3228 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3233 void qemu_service_io(void)
3235 CPUState
*env
= cpu_single_env
;
3237 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3239 if (env
->kqemu_enabled
) {
3240 kqemu_cpu_interrupt(env
);
3246 /***********************************************************/
3247 /* bottom halves (can be seen as timers which expire ASAP) */
3258 static QEMUBH
*first_bh
= NULL
;
3260 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3263 bh
= qemu_mallocz(sizeof(QEMUBH
));
3265 bh
->opaque
= opaque
;
3266 bh
->next
= first_bh
;
3271 int qemu_bh_poll(void)
3277 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3278 if (!bh
->deleted
&& bh
->scheduled
) {
3287 /* remove deleted bhs */
3301 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3309 void qemu_bh_schedule(QEMUBH
*bh
)
3311 CPUState
*env
= cpu_single_env
;
3316 /* stop the currently executing CPU to execute the BH ASAP */
3318 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3322 void qemu_bh_cancel(QEMUBH
*bh
)
3327 void qemu_bh_delete(QEMUBH
*bh
)
3333 static void qemu_bh_update_timeout(int *timeout
)
3337 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3338 if (!bh
->deleted
&& bh
->scheduled
) {
3340 /* idle bottom halves will be polled at least
3342 *timeout
= MIN(10, *timeout
);
3344 /* non-idle bottom halves will be executed
3353 /***********************************************************/
3354 /* machine registration */
3356 static QEMUMachine
*first_machine
= NULL
;
3358 int qemu_register_machine(QEMUMachine
*m
)
3361 pm
= &first_machine
;
3369 static QEMUMachine
*find_machine(const char *name
)
3373 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3374 if (!strcmp(m
->name
, name
))
3380 /***********************************************************/
3381 /* main execution loop */
3383 static void gui_update(void *opaque
)
3385 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3386 DisplayState
*ds
= opaque
;
3387 DisplayChangeListener
*dcl
= ds
->listeners
;
3391 while (dcl
!= NULL
) {
3392 if (dcl
->gui_timer_interval
&&
3393 dcl
->gui_timer_interval
< interval
)
3394 interval
= dcl
->gui_timer_interval
;
3397 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3400 static void nographic_update(void *opaque
)
3402 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3404 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3407 struct vm_change_state_entry
{
3408 VMChangeStateHandler
*cb
;
3410 LIST_ENTRY (vm_change_state_entry
) entries
;
3413 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3415 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3418 VMChangeStateEntry
*e
;
3420 e
= qemu_mallocz(sizeof (*e
));
3424 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3428 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3430 LIST_REMOVE (e
, entries
);
3434 static void vm_state_notify(int running
, int reason
)
3436 VMChangeStateEntry
*e
;
3438 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3439 e
->cb(e
->opaque
, running
, reason
);
3448 vm_state_notify(1, 0);
3449 qemu_rearm_alarm_timer(alarm_timer
);
3453 void vm_stop(int reason
)
3456 cpu_disable_ticks();
3458 vm_state_notify(0, reason
);
3462 /* reset/shutdown handler */
3464 typedef struct QEMUResetEntry
{
3465 QEMUResetHandler
*func
;
3467 struct QEMUResetEntry
*next
;
3470 static QEMUResetEntry
*first_reset_entry
;
3471 static int reset_requested
;
3472 static int shutdown_requested
;
3473 static int powerdown_requested
;
3475 int qemu_shutdown_requested(void)
3477 int r
= shutdown_requested
;
3478 shutdown_requested
= 0;
3482 int qemu_reset_requested(void)
3484 int r
= reset_requested
;
3485 reset_requested
= 0;
3489 int qemu_powerdown_requested(void)
3491 int r
= powerdown_requested
;
3492 powerdown_requested
= 0;
3496 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3498 QEMUResetEntry
**pre
, *re
;
3500 pre
= &first_reset_entry
;
3501 while (*pre
!= NULL
)
3502 pre
= &(*pre
)->next
;
3503 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3505 re
->opaque
= opaque
;
3510 void qemu_system_reset(void)
3514 /* reset all devices */
3515 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3516 re
->func(re
->opaque
);
3520 void qemu_system_reset_request(void)
3523 shutdown_requested
= 1;
3525 reset_requested
= 1;
3528 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3531 void qemu_system_shutdown_request(void)
3533 shutdown_requested
= 1;
3535 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3538 void qemu_system_powerdown_request(void)
3540 powerdown_requested
= 1;
3542 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3546 static void host_main_loop_wait(int *timeout
)
3552 /* XXX: need to suppress polling by better using win32 events */
3554 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3555 ret
|= pe
->func(pe
->opaque
);
3559 WaitObjects
*w
= &wait_objects
;
3561 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3562 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3563 if (w
->func
[ret
- WAIT_OBJECT_0
])
3564 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3566 /* Check for additional signaled events */
3567 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3569 /* Check if event is signaled */
3570 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3571 if(ret2
== WAIT_OBJECT_0
) {
3573 w
->func
[i
](w
->opaque
[i
]);
3574 } else if (ret2
== WAIT_TIMEOUT
) {
3576 err
= GetLastError();
3577 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3580 } else if (ret
== WAIT_TIMEOUT
) {
3582 err
= GetLastError();
3583 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3590 static void host_main_loop_wait(int *timeout
)
3595 void main_loop_wait(int timeout
)
3597 IOHandlerRecord
*ioh
;
3598 fd_set rfds
, wfds
, xfds
;
3602 qemu_bh_update_timeout(&timeout
);
3604 host_main_loop_wait(&timeout
);
3606 /* poll any events */
3607 /* XXX: separate device handlers from system ones */
3612 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3616 (!ioh
->fd_read_poll
||
3617 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3618 FD_SET(ioh
->fd
, &rfds
);
3622 if (ioh
->fd_write
) {
3623 FD_SET(ioh
->fd
, &wfds
);
3629 tv
.tv_sec
= timeout
/ 1000;
3630 tv
.tv_usec
= (timeout
% 1000) * 1000;
3632 #if defined(CONFIG_SLIRP)
3633 if (slirp_is_inited()) {
3634 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3637 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3639 IOHandlerRecord
**pioh
;
3641 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3642 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3643 ioh
->fd_read(ioh
->opaque
);
3645 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3646 ioh
->fd_write(ioh
->opaque
);
3650 /* remove deleted IO handlers */
3651 pioh
= &first_io_handler
;
3661 #if defined(CONFIG_SLIRP)
3662 if (slirp_is_inited()) {
3668 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3672 /* vm time timers */
3673 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3674 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3675 qemu_get_clock(vm_clock
));
3677 /* real time timers */
3678 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3679 qemu_get_clock(rt_clock
));
3681 /* Check bottom-halves last in case any of the earlier events triggered
3687 static int main_loop(void)
3690 #ifdef CONFIG_PROFILER
3695 cur_cpu
= first_cpu
;
3696 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3703 #ifdef CONFIG_PROFILER
3704 ti
= profile_getclock();
3709 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3710 env
->icount_decr
.u16
.low
= 0;
3711 env
->icount_extra
= 0;
3712 count
= qemu_next_deadline();
3713 count
= (count
+ (1 << icount_time_shift
) - 1)
3714 >> icount_time_shift
;
3715 qemu_icount
+= count
;
3716 decr
= (count
> 0xffff) ? 0xffff : count
;
3718 env
->icount_decr
.u16
.low
= decr
;
3719 env
->icount_extra
= count
;
3721 ret
= cpu_exec(env
);
3722 #ifdef CONFIG_PROFILER
3723 qemu_time
+= profile_getclock() - ti
;
3726 /* Fold pending instructions back into the
3727 instruction counter, and clear the interrupt flag. */
3728 qemu_icount
-= (env
->icount_decr
.u16
.low
3729 + env
->icount_extra
);
3730 env
->icount_decr
.u32
= 0;
3731 env
->icount_extra
= 0;
3733 next_cpu
= env
->next_cpu
?: first_cpu
;
3734 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3735 ret
= EXCP_INTERRUPT
;
3739 if (ret
== EXCP_HLT
) {
3740 /* Give the next CPU a chance to run. */
3744 if (ret
!= EXCP_HALTED
)
3746 /* all CPUs are halted ? */
3752 if (shutdown_requested
) {
3753 ret
= EXCP_INTERRUPT
;
3761 if (reset_requested
) {
3762 reset_requested
= 0;
3763 qemu_system_reset();
3764 ret
= EXCP_INTERRUPT
;
3766 if (powerdown_requested
) {
3767 powerdown_requested
= 0;
3768 qemu_system_powerdown();
3769 ret
= EXCP_INTERRUPT
;
3771 if (unlikely(ret
== EXCP_DEBUG
)) {
3772 gdb_set_stop_cpu(cur_cpu
);
3773 vm_stop(EXCP_DEBUG
);
3775 /* If all cpus are halted then wait until the next IRQ */
3776 /* XXX: use timeout computed from timers */
3777 if (ret
== EXCP_HALTED
) {
3781 /* Advance virtual time to the next event. */
3782 if (use_icount
== 1) {
3783 /* When not using an adaptive execution frequency
3784 we tend to get badly out of sync with real time,
3785 so just delay for a reasonable amount of time. */
3788 delta
= cpu_get_icount() - cpu_get_clock();
3791 /* If virtual time is ahead of real time then just
3793 timeout
= (delta
/ 1000000) + 1;
3795 /* Wait for either IO to occur or the next
3797 add
= qemu_next_deadline();
3798 /* We advance the timer before checking for IO.
3799 Limit the amount we advance so that early IO
3800 activity won't get the guest too far ahead. */
3804 add
= (add
+ (1 << icount_time_shift
) - 1)
3805 >> icount_time_shift
;
3807 timeout
= delta
/ 1000000;
3818 if (shutdown_requested
) {
3819 ret
= EXCP_INTERRUPT
;
3824 #ifdef CONFIG_PROFILER
3825 ti
= profile_getclock();
3827 main_loop_wait(timeout
);
3828 #ifdef CONFIG_PROFILER
3829 dev_time
+= profile_getclock() - ti
;
3832 cpu_disable_ticks();
3836 static void help(int exitcode
)
3838 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3839 and qemu-doc.texi */
3840 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3841 "usage: %s [options] [disk_image]\n"
3843 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3845 "Standard options:\n"
3846 "-h or -help display this help and exit\n"
3847 "-M machine select emulated machine (-M ? for list)\n"
3848 "-cpu cpu select CPU (-cpu ? for list)\n"
3849 "-smp n set the number of CPUs to 'n' [default=1]\n"
3850 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3851 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3852 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3853 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3854 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3855 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3856 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3857 " use 'file' as a drive image\n"
3858 "-mtdblock file use 'file' as on-board Flash memory image\n"
3859 "-sd file use 'file' as SecureDigital card image\n"
3860 "-pflash file use 'file' as a parallel flash image\n"
3861 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3862 "-snapshot write to temporary files instead of disk image files\n"
3863 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3865 "-k language use keyboard layout (for example \"fr\" for French)\n"
3868 "-audio-help print list of audio drivers and their options\n"
3869 "-soundhw c1,... enable audio support\n"
3870 " and only specified sound cards (comma separated list)\n"
3871 " use -soundhw ? to get the list of supported cards\n"
3872 " use -soundhw all to enable all of them\n"
3874 "-usb enable the USB driver (will be the default soon)\n"
3875 "-usbdevice name add the host or guest USB device 'name'\n"
3876 "-name string set the name of the guest\n"
3877 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3878 " specify machine UUID\n"
3880 "Display options:\n"
3881 "-nographic disable graphical output and redirect serial I/Os to console\n"
3882 #ifdef CONFIG_CURSES
3883 "-curses use a curses/ncurses interface instead of SDL\n"
3886 "-no-frame open SDL window without a frame and window decorations\n"
3887 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3888 "-no-quit disable SDL window close capability\n"
3891 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3892 "-vga [std|cirrus|vmware|none]\n"
3893 " select video card type\n"
3894 "-full-screen start in full screen\n"
3895 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3896 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3898 "-vnc display start a VNC server on display\n"
3900 "Network options:\n"
3901 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3902 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3904 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3905 " connect the user mode network stack to VLAN 'n' and send\n"
3906 " hostname 'host' to DHCP clients\n"
3909 "-net tap[,vlan=n][,name=str],ifname=name\n"
3910 " connect the host TAP network interface to VLAN 'n'\n"
3912 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3913 " connect the host TAP network interface to VLAN 'n' and use the\n"
3914 " network scripts 'file' (default=%s)\n"
3915 " and 'dfile' (default=%s);\n"
3916 " use '[down]script=no' to disable script execution;\n"
3917 " use 'fd=h' to connect to an already opened TAP interface\n"
3919 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3920 " connect the vlan 'n' to another VLAN using a socket connection\n"
3921 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3922 " connect the vlan 'n' to multicast maddr and port\n"
3924 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3925 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3926 " on host and listening for incoming connections on 'socketpath'.\n"
3927 " Use group 'groupname' and mode 'octalmode' to change default\n"
3928 " ownership and permissions for communication port.\n"
3930 "-net none use it alone to have zero network devices; if no -net option\n"
3931 " is provided, the default is '-net nic -net user'\n"
3933 "-tftp dir allow tftp access to files in dir [-net user]\n"
3934 "-bootp file advertise file in BOOTP replies\n"
3936 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3938 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3939 " redirect TCP or UDP connections from host to guest [-net user]\n"
3942 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
3943 "-bt hci,host[:id]\n"
3944 " use host's HCI with the given name\n"
3945 "-bt hci[,vlan=n]\n"
3946 " emulate a standard HCI in virtual scatternet 'n'\n"
3947 "-bt vhci[,vlan=n]\n"
3948 " add host computer to virtual scatternet 'n' using VHCI\n"
3949 "-bt device:dev[,vlan=n]\n"
3950 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
3954 "i386 target only:\n"
3955 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3956 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
3957 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3958 "-no-acpi disable ACPI\n"
3959 "-no-hpet disable HPET\n"
3961 "Linux boot specific:\n"
3962 "-kernel bzImage use 'bzImage' as kernel image\n"
3963 "-append cmdline use 'cmdline' as kernel command line\n"
3964 "-initrd file use 'file' as initial ram disk\n"
3966 "Debug/Expert options:\n"
3967 "-serial dev redirect the serial port to char device 'dev'\n"
3968 "-parallel dev redirect the parallel port to char device 'dev'\n"
3969 "-monitor dev redirect the monitor to char device 'dev'\n"
3970 "-pidfile file write PID to 'file'\n"
3971 "-S freeze CPU at startup (use 'c' to start execution)\n"
3972 "-s wait gdb connection to port\n"
3973 "-p port set gdb connection port [default=%s]\n"
3974 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3975 "-hdachs c,h,s[,t]\n"
3976 " force hard disk 0 physical geometry and the optional BIOS\n"
3977 " translation (t=none or lba) (usually qemu can guess them)\n"
3978 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3979 "-bios file set the filename for the BIOS\n"
3981 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3982 "-no-kqemu disable KQEMU kernel module usage\n"
3985 "-enable-kvm enable KVM full virtualization support\n"
3987 "-no-reboot exit instead of rebooting\n"
3988 "-no-shutdown stop before shutdown\n"
3989 "-loadvm [tag|id]\n"
3990 " start right away with a saved state (loadvm in monitor)\n"
3992 "-daemonize daemonize QEMU after initializing\n"
3994 "-option-rom rom load a file, rom, into the option ROM space\n"
3995 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
3996 "-prom-env variable=value\n"
3997 " set OpenBIOS nvram variables\n"
3999 "-clock force the use of the given methods for timer alarm.\n"
4000 " To see what timers are available use -clock ?\n"
4001 "-localtime set the real time clock to local time [default=utc]\n"
4002 "-startdate select initial date of the clock\n"
4003 "-icount [N|auto]\n"
4004 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4005 "-echr chr set terminal escape character instead of ctrl-a\n"
4006 "-virtioconsole c\n"
4007 " set virtio console\n"
4008 "-show-cursor show cursor\n"
4009 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4010 "-semihosting semihosting mode\n"
4012 #if defined(TARGET_ARM)
4013 "-old-param old param mode\n"
4015 "-tb-size n set TB size\n"
4016 "-incoming p prepare for incoming migration, listen on port p\n"
4018 "During emulation, the following keys are useful:\n"
4019 "ctrl-alt-f toggle full screen\n"
4020 "ctrl-alt-n switch to virtual console 'n'\n"
4021 "ctrl-alt toggle mouse and keyboard grab\n"
4023 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4028 DEFAULT_NETWORK_SCRIPT
,
4029 DEFAULT_NETWORK_DOWN_SCRIPT
,
4031 DEFAULT_GDBSTUB_PORT
,
4036 #define HAS_ARG 0x0001
4039 /* Please keep in synch with help, qemu_options[] and
4041 /* Standard options: */
4054 QEMU_OPTION_mtdblock
,
4058 QEMU_OPTION_snapshot
,
4061 QEMU_OPTION_audio_help
,
4062 QEMU_OPTION_soundhw
,
4064 QEMU_OPTION_usbdevice
,
4068 /* Display options: */
4069 QEMU_OPTION_nographic
,
4071 QEMU_OPTION_no_frame
,
4072 QEMU_OPTION_alt_grab
,
4073 QEMU_OPTION_no_quit
,
4075 QEMU_OPTION_portrait
,
4077 QEMU_OPTION_full_screen
,
4081 /* Network options: */
4089 /* i386 target only: */
4090 QEMU_OPTION_win2k_hack
,
4091 QEMU_OPTION_rtc_td_hack
,
4092 QEMU_OPTION_no_fd_bootchk
,
4093 QEMU_OPTION_no_acpi
,
4094 QEMU_OPTION_no_hpet
,
4096 /* Linux boot specific: */
4101 /* Debug/Expert options: */
4103 QEMU_OPTION_parallel
,
4104 QEMU_OPTION_monitor
,
4105 QEMU_OPTION_pidfile
,
4113 QEMU_OPTION_kernel_kqemu
,
4114 QEMU_OPTION_no_kqemu
,
4115 QEMU_OPTION_enable_kvm
,
4116 QEMU_OPTION_no_reboot
,
4117 QEMU_OPTION_no_shutdown
,
4119 QEMU_OPTION_daemonize
,
4120 QEMU_OPTION_option_rom
,
4121 QEMU_OPTION_prom_env
,
4123 QEMU_OPTION_localtime
,
4124 QEMU_OPTION_startdate
,
4127 QEMU_OPTION_virtiocon
,
4128 QEMU_OPTION_show_cursor
,
4129 QEMU_OPTION_semihosting
,
4130 QEMU_OPTION_old_param
,
4131 QEMU_OPTION_tb_size
,
4132 QEMU_OPTION_incoming
,
4135 typedef struct QEMUOption
{
4141 static const QEMUOption qemu_options
[] = {
4142 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4144 /* Standard options: */
4145 { "h", 0, QEMU_OPTION_h
},
4146 { "help", 0, QEMU_OPTION_h
},
4147 { "M", HAS_ARG
, QEMU_OPTION_M
},
4148 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4149 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4150 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4151 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4152 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4153 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4154 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4155 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4156 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4157 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4158 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4159 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4160 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4161 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4162 { "snapshot", 0, QEMU_OPTION_snapshot
},
4163 { "m", HAS_ARG
, QEMU_OPTION_m
},
4165 { "k", HAS_ARG
, QEMU_OPTION_k
},
4168 { "audio-help", 0, QEMU_OPTION_audio_help
},
4169 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4171 { "usb", 0, QEMU_OPTION_usb
},
4172 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4173 { "name", HAS_ARG
, QEMU_OPTION_name
},
4174 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4176 /* Display options: */
4177 { "nographic", 0, QEMU_OPTION_nographic
},
4178 #ifdef CONFIG_CURSES
4179 { "curses", 0, QEMU_OPTION_curses
},
4182 { "no-frame", 0, QEMU_OPTION_no_frame
},
4183 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4184 { "no-quit", 0, QEMU_OPTION_no_quit
},
4185 { "sdl", 0, QEMU_OPTION_sdl
},
4187 { "portrait", 0, QEMU_OPTION_portrait
},
4188 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4189 { "full-screen", 0, QEMU_OPTION_full_screen
},
4190 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4191 { "g", 1, QEMU_OPTION_g
},
4193 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4195 /* Network options: */
4196 { "net", HAS_ARG
, QEMU_OPTION_net
},
4198 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4199 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4201 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4203 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4205 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4207 /* i386 target only: */
4208 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4209 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4210 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4211 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4212 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4215 /* Linux boot specific: */
4216 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4217 { "append", HAS_ARG
, QEMU_OPTION_append
},
4218 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4220 /* Debug/Expert options: */
4221 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4222 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4223 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4224 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4225 { "S", 0, QEMU_OPTION_S
},
4226 { "s", 0, QEMU_OPTION_s
},
4227 { "p", HAS_ARG
, QEMU_OPTION_p
},
4228 { "d", HAS_ARG
, QEMU_OPTION_d
},
4229 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4230 { "L", HAS_ARG
, QEMU_OPTION_L
},
4231 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4233 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4234 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4237 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4239 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4240 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4241 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4242 { "daemonize", 0, QEMU_OPTION_daemonize
},
4243 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4244 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4245 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4247 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4248 { "localtime", 0, QEMU_OPTION_localtime
},
4249 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4250 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4251 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4252 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4253 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4254 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4255 { "semihosting", 0, QEMU_OPTION_semihosting
},
4257 #if defined(TARGET_ARM)
4258 { "old-param", 0, QEMU_OPTION_old_param
},
4260 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4261 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4265 /* password input */
4267 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4272 if (!bdrv_is_encrypted(bs
))
4275 term_printf("%s is encrypted.\n", name
);
4276 for(i
= 0; i
< 3; i
++) {
4277 monitor_readline("Password: ", 1, password
, sizeof(password
));
4278 if (bdrv_set_key(bs
, password
) == 0)
4280 term_printf("invalid password\n");
4285 static BlockDriverState
*get_bdrv(int index
)
4287 if (index
> nb_drives
)
4289 return drives_table
[index
].bdrv
;
4292 static void read_passwords(void)
4294 BlockDriverState
*bs
;
4297 for(i
= 0; i
< 6; i
++) {
4300 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4305 struct soundhw soundhw
[] = {
4306 #ifdef HAS_AUDIO_CHOICE
4307 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4313 { .init_isa
= pcspk_audio_init
}
4320 "Creative Sound Blaster 16",
4323 { .init_isa
= SB16_init
}
4327 #ifdef CONFIG_CS4231A
4333 { .init_isa
= cs4231a_init
}
4341 "Yamaha YMF262 (OPL3)",
4343 "Yamaha YM3812 (OPL2)",
4347 { .init_isa
= Adlib_init
}
4354 "Gravis Ultrasound GF1",
4357 { .init_isa
= GUS_init
}
4364 "Intel 82801AA AC97 Audio",
4367 { .init_pci
= ac97_init
}
4371 #ifdef CONFIG_ES1370
4374 "ENSONIQ AudioPCI ES1370",
4377 { .init_pci
= es1370_init
}
4381 #endif /* HAS_AUDIO_CHOICE */
4383 { NULL
, NULL
, 0, 0, { NULL
} }
4386 static void select_soundhw (const char *optarg
)
4390 if (*optarg
== '?') {
4393 printf ("Valid sound card names (comma separated):\n");
4394 for (c
= soundhw
; c
->name
; ++c
) {
4395 printf ("%-11s %s\n", c
->name
, c
->descr
);
4397 printf ("\n-soundhw all will enable all of the above\n");
4398 exit (*optarg
!= '?');
4406 if (!strcmp (optarg
, "all")) {
4407 for (c
= soundhw
; c
->name
; ++c
) {
4415 e
= strchr (p
, ',');
4416 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4418 for (c
= soundhw
; c
->name
; ++c
) {
4419 if (!strncmp (c
->name
, p
, l
)) {
4428 "Unknown sound card name (too big to show)\n");
4431 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4436 p
+= l
+ (e
!= NULL
);
4440 goto show_valid_cards
;
4445 static void select_vgahw (const char *p
)
4449 if (strstart(p
, "std", &opts
)) {
4450 std_vga_enabled
= 1;
4451 cirrus_vga_enabled
= 0;
4453 } else if (strstart(p
, "cirrus", &opts
)) {
4454 cirrus_vga_enabled
= 1;
4455 std_vga_enabled
= 0;
4457 } else if (strstart(p
, "vmware", &opts
)) {
4458 cirrus_vga_enabled
= 0;
4459 std_vga_enabled
= 0;
4461 } else if (strstart(p
, "none", &opts
)) {
4462 cirrus_vga_enabled
= 0;
4463 std_vga_enabled
= 0;
4467 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4471 const char *nextopt
;
4473 if (strstart(opts
, ",retrace=", &nextopt
)) {
4475 if (strstart(opts
, "dumb", &nextopt
))
4476 vga_retrace_method
= VGA_RETRACE_DUMB
;
4477 else if (strstart(opts
, "precise", &nextopt
))
4478 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4479 else goto invalid_vga
;
4480 } else goto invalid_vga
;
4486 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4488 exit(STATUS_CONTROL_C_EXIT
);
4493 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4497 if(strlen(str
) != 36)
4500 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4501 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4502 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4510 #define MAX_NET_CLIENTS 32
4514 static void termsig_handler(int signal
)
4516 qemu_system_shutdown_request();
4519 static void termsig_setup(void)
4521 struct sigaction act
;
4523 memset(&act
, 0, sizeof(act
));
4524 act
.sa_handler
= termsig_handler
;
4525 sigaction(SIGINT
, &act
, NULL
);
4526 sigaction(SIGHUP
, &act
, NULL
);
4527 sigaction(SIGTERM
, &act
, NULL
);
4532 int main(int argc
, char **argv
, char **envp
)
4534 #ifdef CONFIG_GDBSTUB
4536 const char *gdbstub_port
;
4538 uint32_t boot_devices_bitmap
= 0;
4540 int snapshot
, linux_boot
, net_boot
;
4541 const char *initrd_filename
;
4542 const char *kernel_filename
, *kernel_cmdline
;
4543 const char *boot_devices
= "";
4545 DisplayChangeListener
*dcl
;
4546 int cyls
, heads
, secs
, translation
;
4547 const char *net_clients
[MAX_NET_CLIENTS
];
4549 const char *bt_opts
[MAX_BT_CMDLINE
];
4553 const char *r
, *optarg
;
4554 CharDriverState
*monitor_hd
= NULL
;
4555 const char *monitor_device
;
4556 const char *serial_devices
[MAX_SERIAL_PORTS
];
4557 int serial_device_index
;
4558 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4559 int parallel_device_index
;
4560 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4561 int virtio_console_index
;
4562 const char *loadvm
= NULL
;
4563 QEMUMachine
*machine
;
4564 const char *cpu_model
;
4565 const char *usb_devices
[MAX_USB_CMDLINE
];
4566 int usb_devices_index
;
4569 const char *pid_file
= NULL
;
4571 const char *incoming
= NULL
;
4573 qemu_cache_utils_init(envp
);
4575 LIST_INIT (&vm_change_state_head
);
4578 struct sigaction act
;
4579 sigfillset(&act
.sa_mask
);
4581 act
.sa_handler
= SIG_IGN
;
4582 sigaction(SIGPIPE
, &act
, NULL
);
4585 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4586 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4587 QEMU to run on a single CPU */
4592 h
= GetCurrentProcess();
4593 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4594 for(i
= 0; i
< 32; i
++) {
4595 if (mask
& (1 << i
))
4600 SetProcessAffinityMask(h
, mask
);
4606 register_machines();
4607 machine
= first_machine
;
4609 initrd_filename
= NULL
;
4611 vga_ram_size
= VGA_RAM_SIZE
;
4612 #ifdef CONFIG_GDBSTUB
4614 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4619 kernel_filename
= NULL
;
4620 kernel_cmdline
= "";
4621 cyls
= heads
= secs
= 0;
4622 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4623 monitor_device
= "vc";
4625 serial_devices
[0] = "vc:80Cx24C";
4626 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4627 serial_devices
[i
] = NULL
;
4628 serial_device_index
= 0;
4630 parallel_devices
[0] = "vc:640x480";
4631 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4632 parallel_devices
[i
] = NULL
;
4633 parallel_device_index
= 0;
4635 virtio_consoles
[0] = "vc:80Cx24C";
4636 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4637 virtio_consoles
[i
] = NULL
;
4638 virtio_console_index
= 0;
4640 usb_devices_index
= 0;
4659 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4661 const QEMUOption
*popt
;
4664 /* Treat --foo the same as -foo. */
4667 popt
= qemu_options
;
4670 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4674 if (!strcmp(popt
->name
, r
+ 1))
4678 if (popt
->flags
& HAS_ARG
) {
4679 if (optind
>= argc
) {
4680 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4684 optarg
= argv
[optind
++];
4689 switch(popt
->index
) {
4691 machine
= find_machine(optarg
);
4694 printf("Supported machines are:\n");
4695 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4696 printf("%-10s %s%s\n",
4698 m
== first_machine
? " (default)" : "");
4700 exit(*optarg
!= '?');
4703 case QEMU_OPTION_cpu
:
4704 /* hw initialization will check this */
4705 if (*optarg
== '?') {
4706 /* XXX: implement xxx_cpu_list for targets that still miss it */
4707 #if defined(cpu_list)
4708 cpu_list(stdout
, &fprintf
);
4715 case QEMU_OPTION_initrd
:
4716 initrd_filename
= optarg
;
4718 case QEMU_OPTION_hda
:
4720 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4722 hda_index
= drive_add(optarg
, HD_ALIAS
4723 ",cyls=%d,heads=%d,secs=%d%s",
4724 0, cyls
, heads
, secs
,
4725 translation
== BIOS_ATA_TRANSLATION_LBA
?
4727 translation
== BIOS_ATA_TRANSLATION_NONE
?
4728 ",trans=none" : "");
4730 case QEMU_OPTION_hdb
:
4731 case QEMU_OPTION_hdc
:
4732 case QEMU_OPTION_hdd
:
4733 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4735 case QEMU_OPTION_drive
:
4736 drive_add(NULL
, "%s", optarg
);
4738 case QEMU_OPTION_mtdblock
:
4739 drive_add(optarg
, MTD_ALIAS
);
4741 case QEMU_OPTION_sd
:
4742 drive_add(optarg
, SD_ALIAS
);
4744 case QEMU_OPTION_pflash
:
4745 drive_add(optarg
, PFLASH_ALIAS
);
4747 case QEMU_OPTION_snapshot
:
4750 case QEMU_OPTION_hdachs
:
4754 cyls
= strtol(p
, (char **)&p
, 0);
4755 if (cyls
< 1 || cyls
> 16383)
4760 heads
= strtol(p
, (char **)&p
, 0);
4761 if (heads
< 1 || heads
> 16)
4766 secs
= strtol(p
, (char **)&p
, 0);
4767 if (secs
< 1 || secs
> 63)
4771 if (!strcmp(p
, "none"))
4772 translation
= BIOS_ATA_TRANSLATION_NONE
;
4773 else if (!strcmp(p
, "lba"))
4774 translation
= BIOS_ATA_TRANSLATION_LBA
;
4775 else if (!strcmp(p
, "auto"))
4776 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4779 } else if (*p
!= '\0') {
4781 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4784 if (hda_index
!= -1)
4785 snprintf(drives_opt
[hda_index
].opt
,
4786 sizeof(drives_opt
[hda_index
].opt
),
4787 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4788 0, cyls
, heads
, secs
,
4789 translation
== BIOS_ATA_TRANSLATION_LBA
?
4791 translation
== BIOS_ATA_TRANSLATION_NONE
?
4792 ",trans=none" : "");
4795 case QEMU_OPTION_nographic
:
4798 #ifdef CONFIG_CURSES
4799 case QEMU_OPTION_curses
:
4803 case QEMU_OPTION_portrait
:
4806 case QEMU_OPTION_kernel
:
4807 kernel_filename
= optarg
;
4809 case QEMU_OPTION_append
:
4810 kernel_cmdline
= optarg
;
4812 case QEMU_OPTION_cdrom
:
4813 drive_add(optarg
, CDROM_ALIAS
);
4815 case QEMU_OPTION_boot
:
4816 boot_devices
= optarg
;
4817 /* We just do some generic consistency checks */
4819 /* Could easily be extended to 64 devices if needed */
4822 boot_devices_bitmap
= 0;
4823 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4824 /* Allowed boot devices are:
4825 * a b : floppy disk drives
4826 * c ... f : IDE disk drives
4827 * g ... m : machine implementation dependant drives
4828 * n ... p : network devices
4829 * It's up to each machine implementation to check
4830 * if the given boot devices match the actual hardware
4831 * implementation and firmware features.
4833 if (*p
< 'a' || *p
> 'q') {
4834 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4837 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4839 "Boot device '%c' was given twice\n",*p
);
4842 boot_devices_bitmap
|= 1 << (*p
- 'a');
4846 case QEMU_OPTION_fda
:
4847 case QEMU_OPTION_fdb
:
4848 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4851 case QEMU_OPTION_no_fd_bootchk
:
4855 case QEMU_OPTION_net
:
4856 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4857 fprintf(stderr
, "qemu: too many network clients\n");
4860 net_clients
[nb_net_clients
] = optarg
;
4864 case QEMU_OPTION_tftp
:
4865 tftp_prefix
= optarg
;
4867 case QEMU_OPTION_bootp
:
4868 bootp_filename
= optarg
;
4871 case QEMU_OPTION_smb
:
4872 net_slirp_smb(optarg
);
4875 case QEMU_OPTION_redir
:
4876 net_slirp_redir(optarg
);
4879 case QEMU_OPTION_bt
:
4880 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4881 fprintf(stderr
, "qemu: too many bluetooth options\n");
4884 bt_opts
[nb_bt_opts
++] = optarg
;
4887 case QEMU_OPTION_audio_help
:
4891 case QEMU_OPTION_soundhw
:
4892 select_soundhw (optarg
);
4898 case QEMU_OPTION_m
: {
4902 value
= strtoul(optarg
, &ptr
, 10);
4904 case 0: case 'M': case 'm':
4911 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4915 /* On 32-bit hosts, QEMU is limited by virtual address space */
4916 if (value
> (2047 << 20)
4918 && HOST_LONG_BITS
== 32
4921 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4924 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4925 fprintf(stderr
, "qemu: ram size too large\n");
4934 const CPULogItem
*item
;
4936 mask
= cpu_str_to_log_mask(optarg
);
4938 printf("Log items (comma separated):\n");
4939 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4940 printf("%-10s %s\n", item
->name
, item
->help
);
4947 #ifdef CONFIG_GDBSTUB
4952 gdbstub_port
= optarg
;
4958 case QEMU_OPTION_bios
:
4965 keyboard_layout
= optarg
;
4967 case QEMU_OPTION_localtime
:
4970 case QEMU_OPTION_vga
:
4971 select_vgahw (optarg
);
4978 w
= strtol(p
, (char **)&p
, 10);
4981 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4987 h
= strtol(p
, (char **)&p
, 10);
4992 depth
= strtol(p
, (char **)&p
, 10);
4993 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4994 depth
!= 24 && depth
!= 32)
4996 } else if (*p
== '\0') {
4997 depth
= graphic_depth
;
5004 graphic_depth
= depth
;
5007 case QEMU_OPTION_echr
:
5010 term_escape_char
= strtol(optarg
, &r
, 0);
5012 printf("Bad argument to echr\n");
5015 case QEMU_OPTION_monitor
:
5016 monitor_device
= optarg
;
5018 case QEMU_OPTION_serial
:
5019 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5020 fprintf(stderr
, "qemu: too many serial ports\n");
5023 serial_devices
[serial_device_index
] = optarg
;
5024 serial_device_index
++;
5026 case QEMU_OPTION_virtiocon
:
5027 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5028 fprintf(stderr
, "qemu: too many virtio consoles\n");
5031 virtio_consoles
[virtio_console_index
] = optarg
;
5032 virtio_console_index
++;
5034 case QEMU_OPTION_parallel
:
5035 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5036 fprintf(stderr
, "qemu: too many parallel ports\n");
5039 parallel_devices
[parallel_device_index
] = optarg
;
5040 parallel_device_index
++;
5042 case QEMU_OPTION_loadvm
:
5045 case QEMU_OPTION_full_screen
:
5049 case QEMU_OPTION_no_frame
:
5052 case QEMU_OPTION_alt_grab
:
5055 case QEMU_OPTION_no_quit
:
5058 case QEMU_OPTION_sdl
:
5062 case QEMU_OPTION_pidfile
:
5066 case QEMU_OPTION_win2k_hack
:
5067 win2k_install_hack
= 1;
5069 case QEMU_OPTION_rtc_td_hack
:
5074 case QEMU_OPTION_no_kqemu
:
5077 case QEMU_OPTION_kernel_kqemu
:
5082 case QEMU_OPTION_enable_kvm
:
5089 case QEMU_OPTION_usb
:
5092 case QEMU_OPTION_usbdevice
:
5094 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5095 fprintf(stderr
, "Too many USB devices\n");
5098 usb_devices
[usb_devices_index
] = optarg
;
5099 usb_devices_index
++;
5101 case QEMU_OPTION_smp
:
5102 smp_cpus
= atoi(optarg
);
5104 fprintf(stderr
, "Invalid number of CPUs\n");
5108 case QEMU_OPTION_vnc
:
5109 vnc_display
= optarg
;
5111 case QEMU_OPTION_no_acpi
:
5114 case QEMU_OPTION_no_hpet
:
5117 case QEMU_OPTION_no_reboot
:
5120 case QEMU_OPTION_no_shutdown
:
5123 case QEMU_OPTION_show_cursor
:
5126 case QEMU_OPTION_uuid
:
5127 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5128 fprintf(stderr
, "Fail to parse UUID string."
5129 " Wrong format.\n");
5133 case QEMU_OPTION_daemonize
:
5136 case QEMU_OPTION_option_rom
:
5137 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5138 fprintf(stderr
, "Too many option ROMs\n");
5141 option_rom
[nb_option_roms
] = optarg
;
5144 case QEMU_OPTION_semihosting
:
5145 semihosting_enabled
= 1;
5147 case QEMU_OPTION_name
:
5150 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5151 case QEMU_OPTION_prom_env
:
5152 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5153 fprintf(stderr
, "Too many prom variables\n");
5156 prom_envs
[nb_prom_envs
] = optarg
;
5161 case QEMU_OPTION_old_param
:
5165 case QEMU_OPTION_clock
:
5166 configure_alarms(optarg
);
5168 case QEMU_OPTION_startdate
:
5171 time_t rtc_start_date
;
5172 if (!strcmp(optarg
, "now")) {
5173 rtc_date_offset
= -1;
5175 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5183 } else if (sscanf(optarg
, "%d-%d-%d",
5186 &tm
.tm_mday
) == 3) {
5195 rtc_start_date
= mktimegm(&tm
);
5196 if (rtc_start_date
== -1) {
5198 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5199 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5202 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5206 case QEMU_OPTION_tb_size
:
5207 tb_size
= strtol(optarg
, NULL
, 0);
5211 case QEMU_OPTION_icount
:
5213 if (strcmp(optarg
, "auto") == 0) {
5214 icount_time_shift
= -1;
5216 icount_time_shift
= strtol(optarg
, NULL
, 0);
5219 case QEMU_OPTION_incoming
:
5226 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5227 if (kvm_allowed
&& kqemu_allowed
) {
5229 "You can not enable both KVM and kqemu at the same time\n");
5234 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5235 if (smp_cpus
> machine
->max_cpus
) {
5236 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5237 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5243 if (serial_device_index
== 0)
5244 serial_devices
[0] = "stdio";
5245 if (parallel_device_index
== 0)
5246 parallel_devices
[0] = "null";
5247 if (strncmp(monitor_device
, "vc", 2) == 0)
5248 monitor_device
= "stdio";
5249 if (virtio_console_index
== 0)
5250 virtio_consoles
[0] = "null";
5257 if (pipe(fds
) == -1)
5268 len
= read(fds
[0], &status
, 1);
5269 if (len
== -1 && (errno
== EINTR
))
5274 else if (status
== 1) {
5275 fprintf(stderr
, "Could not acquire pidfile\n");
5292 signal(SIGTSTP
, SIG_IGN
);
5293 signal(SIGTTOU
, SIG_IGN
);
5294 signal(SIGTTIN
, SIG_IGN
);
5298 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5301 write(fds
[1], &status
, 1);
5303 fprintf(stderr
, "Could not acquire pid file\n");
5311 linux_boot
= (kernel_filename
!= NULL
);
5312 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5314 if (!linux_boot
&& net_boot
== 0 &&
5315 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5318 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5319 fprintf(stderr
, "-append only allowed with -kernel option\n");
5323 if (!linux_boot
&& initrd_filename
!= NULL
) {
5324 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5328 /* boot to floppy or the default cd if no hard disk defined yet */
5329 if (!boot_devices
[0]) {
5330 boot_devices
= "cad";
5332 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5335 if (init_timer_alarm() < 0) {
5336 fprintf(stderr
, "could not initialize alarm timer\n");
5339 if (use_icount
&& icount_time_shift
< 0) {
5341 /* 125MIPS seems a reasonable initial guess at the guest speed.
5342 It will be corrected fairly quickly anyway. */
5343 icount_time_shift
= 3;
5344 init_icount_adjust();
5351 /* init network clients */
5352 if (nb_net_clients
== 0) {
5353 /* if no clients, we use a default config */
5354 net_clients
[nb_net_clients
++] = "nic";
5356 net_clients
[nb_net_clients
++] = "user";
5360 for(i
= 0;i
< nb_net_clients
; i
++) {
5361 if (net_client_parse(net_clients
[i
]) < 0)
5367 /* XXX: this should be moved in the PC machine instantiation code */
5368 if (net_boot
!= 0) {
5370 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5371 const char *model
= nd_table
[i
].model
;
5373 if (net_boot
& (1 << i
)) {
5376 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5377 if (get_image_size(buf
) > 0) {
5378 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5379 fprintf(stderr
, "Too many option ROMs\n");
5382 option_rom
[nb_option_roms
] = strdup(buf
);
5389 fprintf(stderr
, "No valid PXE rom found for network device\n");
5395 /* init the bluetooth world */
5396 for (i
= 0; i
< nb_bt_opts
; i
++)
5397 if (bt_parse(bt_opts
[i
]))
5400 /* init the memory */
5401 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5403 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5405 if (ram_size
< phys_ram_size
) {
5406 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5407 machine
->name
, (unsigned long long) phys_ram_size
);
5411 phys_ram_size
= ram_size
;
5413 ram_size
= phys_ram_size
;
5416 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5418 phys_ram_size
+= ram_size
;
5421 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5422 if (!phys_ram_base
) {
5423 fprintf(stderr
, "Could not allocate physical memory\n");
5427 /* init the dynamic translator */
5428 cpu_exec_init_all(tb_size
* 1024 * 1024);
5432 /* we always create the cdrom drive, even if no disk is there */
5434 if (nb_drives_opt
< MAX_DRIVES
)
5435 drive_add(NULL
, CDROM_ALIAS
);
5437 /* we always create at least one floppy */
5439 if (nb_drives_opt
< MAX_DRIVES
)
5440 drive_add(NULL
, FD_ALIAS
, 0);
5442 /* we always create one sd slot, even if no card is in it */
5444 if (nb_drives_opt
< MAX_DRIVES
)
5445 drive_add(NULL
, SD_ALIAS
);
5447 /* open the virtual block devices */
5449 for(i
= 0; i
< nb_drives_opt
; i
++)
5450 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5453 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5454 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5457 /* must be after terminal init, SDL library changes signal handlers */
5461 /* Maintain compatibility with multiple stdio monitors */
5462 if (!strcmp(monitor_device
,"stdio")) {
5463 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5464 const char *devname
= serial_devices
[i
];
5465 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5466 monitor_device
= NULL
;
5468 } else if (devname
&& !strcmp(devname
,"stdio")) {
5469 monitor_device
= NULL
;
5470 serial_devices
[i
] = "mon:stdio";
5476 if (kvm_enabled()) {
5479 ret
= kvm_init(smp_cpus
);
5481 fprintf(stderr
, "failed to initialize KVM\n");
5486 if (monitor_device
) {
5487 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5489 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5494 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5495 const char *devname
= serial_devices
[i
];
5496 if (devname
&& strcmp(devname
, "none")) {
5498 snprintf(label
, sizeof(label
), "serial%d", i
);
5499 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5500 if (!serial_hds
[i
]) {
5501 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5508 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5509 const char *devname
= parallel_devices
[i
];
5510 if (devname
&& strcmp(devname
, "none")) {
5512 snprintf(label
, sizeof(label
), "parallel%d", i
);
5513 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5514 if (!parallel_hds
[i
]) {
5515 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5522 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5523 const char *devname
= virtio_consoles
[i
];
5524 if (devname
&& strcmp(devname
, "none")) {
5526 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5527 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5528 if (!virtcon_hds
[i
]) {
5529 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5536 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5537 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5539 /* Set KVM's vcpu state to qemu's initial CPUState. */
5540 if (kvm_enabled()) {
5543 ret
= kvm_sync_vcpus();
5545 fprintf(stderr
, "failed to initialize vcpus\n");
5550 /* init USB devices */
5552 for(i
= 0; i
< usb_devices_index
; i
++) {
5553 if (usb_device_add(usb_devices
[i
]) < 0) {
5554 fprintf(stderr
, "Warning: could not add USB device %s\n",
5561 dumb_display_init();
5562 /* just use the first displaystate for the moment */
5567 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5571 #if defined(CONFIG_CURSES)
5573 /* At the moment curses cannot be used with other displays */
5574 curses_display_init(ds
, full_screen
);
5578 if (vnc_display
!= NULL
) {
5579 vnc_display_init(ds
);
5580 if (vnc_display_open(ds
, vnc_display
) < 0)
5583 #if defined(CONFIG_SDL)
5584 if (sdl
|| !vnc_display
)
5585 sdl_display_init(ds
, full_screen
, no_frame
);
5586 #elif defined(CONFIG_COCOA)
5587 if (sdl
|| !vnc_display
)
5588 cocoa_display_init(ds
, full_screen
);
5594 dcl
= ds
->listeners
;
5595 while (dcl
!= NULL
) {
5596 if (dcl
->dpy_refresh
!= NULL
) {
5597 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5598 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5603 if (nographic
|| (vnc_display
&& !sdl
)) {
5604 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5605 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5608 text_consoles_set_display(display_state
);
5610 if (monitor_device
&& monitor_hd
)
5611 monitor_init(monitor_hd
, !nographic
);
5613 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5614 const char *devname
= serial_devices
[i
];
5615 if (devname
&& strcmp(devname
, "none")) {
5617 snprintf(label
, sizeof(label
), "serial%d", i
);
5618 if (strstart(devname
, "vc", 0))
5619 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5623 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5624 const char *devname
= parallel_devices
[i
];
5625 if (devname
&& strcmp(devname
, "none")) {
5627 snprintf(label
, sizeof(label
), "parallel%d", i
);
5628 if (strstart(devname
, "vc", 0))
5629 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5633 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5634 const char *devname
= virtio_consoles
[i
];
5635 if (virtcon_hds
[i
] && devname
) {
5637 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5638 if (strstart(devname
, "vc", 0))
5639 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5643 #ifdef CONFIG_GDBSTUB
5645 /* XXX: use standard host:port notation and modify options
5647 if (gdbserver_start(gdbstub_port
) < 0) {
5648 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5659 autostart
= 0; /* fixme how to deal with -daemonize */
5660 qemu_start_incoming_migration(incoming
);
5664 /* XXX: simplify init */
5677 len
= write(fds
[1], &status
, 1);
5678 if (len
== -1 && (errno
== EINTR
))
5685 TFR(fd
= open("/dev/null", O_RDWR
));