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"
40 #include "audio/audio.h"
41 #include "migration.h"
53 #include <sys/times.h>
57 #include <sys/ioctl.h>
58 #include <sys/resource.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
62 #if defined(__NetBSD__)
63 #include <net/if_tap.h>
66 #include <linux/if_tun.h>
68 #include <arpa/inet.h>
71 #include <sys/select.h>
79 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
80 #include <freebsd/stdlib.h>
85 #include <linux/rtc.h>
87 /* For the benefit of older linux systems which don't supply it,
88 we use a local copy of hpet.h. */
89 /* #include <linux/hpet.h> */
92 #include <linux/ppdev.h>
93 #include <linux/parport.h>
97 #include <sys/ethernet.h>
98 #include <sys/sockio.h>
99 #include <netinet/arp.h>
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/ip.h>
103 #include <netinet/ip_icmp.h> // must come after ip.h
104 #include <netinet/udp.h>
105 #include <netinet/tcp.h>
113 #include "qemu_socket.h"
115 #if defined(CONFIG_SLIRP)
116 #include "libslirp.h"
119 #if defined(__OpenBSD__)
123 #if defined(CONFIG_VDE)
124 #include <libvdeplug.h>
129 #include <sys/timeb.h>
130 #include <mmsystem.h>
131 #define getopt_long_only getopt_long
132 #define memalign(align, size) malloc(size)
139 #endif /* CONFIG_SDL */
143 #define main qemu_main
144 #endif /* CONFIG_COCOA */
148 #include "exec-all.h"
150 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
151 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
153 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
155 #define SMBD_COMMAND "/usr/sbin/smbd"
158 //#define DEBUG_UNUSED_IOPORT
159 //#define DEBUG_IOPORT
161 //#define DEBUG_SLIRP
164 #define DEFAULT_RAM_SIZE 144
166 #define DEFAULT_RAM_SIZE 128
169 /* Max number of USB devices that can be specified on the commandline. */
170 #define MAX_USB_CMDLINE 8
172 /* Max number of bluetooth switches on the commandline. */
173 #define MAX_BT_CMDLINE 10
175 /* XXX: use a two level table to limit memory usage */
176 #define MAX_IOPORTS 65536
178 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
179 const char *bios_name
= NULL
;
180 static void *ioport_opaque
[MAX_IOPORTS
];
181 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
182 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
183 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
184 to store the VM snapshots */
185 DriveInfo drives_table
[MAX_DRIVES
+1];
187 static int vga_ram_size
;
188 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
189 DisplayState display_state
;
192 const char* keyboard_layout
= NULL
;
193 int64_t ticks_per_sec
;
196 NICInfo nd_table
[MAX_NICS
];
198 static int rtc_utc
= 1;
199 static int rtc_date_offset
= -1; /* -1 means no change */
200 int cirrus_vga_enabled
= 1;
201 int vmsvga_enabled
= 0;
203 int graphic_width
= 1024;
204 int graphic_height
= 768;
205 int graphic_depth
= 8;
207 int graphic_width
= 800;
208 int graphic_height
= 600;
209 int graphic_depth
= 15;
211 static int full_screen
= 0;
212 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
217 int win2k_install_hack
= 0;
221 const char *vnc_display
;
222 int acpi_enabled
= 1;
227 int graphic_rotate
= 0;
229 const char *option_rom
[MAX_OPTION_ROMS
];
231 int semihosting_enabled
= 0;
235 const char *qemu_name
;
238 unsigned int nb_prom_envs
= 0;
239 const char *prom_envs
[MAX_PROM_ENVS
];
241 static int nb_drives_opt
;
242 static struct drive_opt
{
245 } drives_opt
[MAX_DRIVES
];
247 static CPUState
*cur_cpu
;
248 static CPUState
*next_cpu
;
249 static int event_pending
= 1;
250 /* Conversion factor from emulated instructions to virtual clock ticks. */
251 static int icount_time_shift
;
252 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
253 #define MAX_ICOUNT_SHIFT 10
254 /* Compensate for varying guest execution speed. */
255 static int64_t qemu_icount_bias
;
256 static QEMUTimer
*icount_rt_timer
;
257 static QEMUTimer
*icount_vm_timer
;
259 uint8_t qemu_uuid
[16];
261 /***********************************************************/
262 /* x86 ISA bus support */
264 target_phys_addr_t isa_mem_base
= 0;
267 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
268 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
270 static uint32_t ioport_read(int index
, uint32_t address
)
272 static IOPortReadFunc
*default_func
[3] = {
273 default_ioport_readb
,
274 default_ioport_readw
,
277 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
279 func
= default_func
[index
];
280 return func(ioport_opaque
[address
], address
);
283 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
285 static IOPortWriteFunc
*default_func
[3] = {
286 default_ioport_writeb
,
287 default_ioport_writew
,
288 default_ioport_writel
290 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
292 func
= default_func
[index
];
293 func(ioport_opaque
[address
], address
, data
);
296 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
298 #ifdef DEBUG_UNUSED_IOPORT
299 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
304 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
306 #ifdef DEBUG_UNUSED_IOPORT
307 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
311 /* default is to make two byte accesses */
312 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
315 data
= ioport_read(0, address
);
316 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
317 data
|= ioport_read(0, address
) << 8;
321 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
323 ioport_write(0, address
, data
& 0xff);
324 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
325 ioport_write(0, address
, (data
>> 8) & 0xff);
328 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
330 #ifdef DEBUG_UNUSED_IOPORT
331 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
336 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
338 #ifdef DEBUG_UNUSED_IOPORT
339 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
343 /* size is the word size in byte */
344 int register_ioport_read(int start
, int length
, int size
,
345 IOPortReadFunc
*func
, void *opaque
)
351 } else if (size
== 2) {
353 } else if (size
== 4) {
356 hw_error("register_ioport_read: invalid size");
359 for(i
= start
; i
< start
+ length
; i
+= size
) {
360 ioport_read_table
[bsize
][i
] = func
;
361 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
362 hw_error("register_ioport_read: invalid opaque");
363 ioport_opaque
[i
] = opaque
;
368 /* size is the word size in byte */
369 int register_ioport_write(int start
, int length
, int size
,
370 IOPortWriteFunc
*func
, void *opaque
)
376 } else if (size
== 2) {
378 } else if (size
== 4) {
381 hw_error("register_ioport_write: invalid size");
384 for(i
= start
; i
< start
+ length
; i
+= size
) {
385 ioport_write_table
[bsize
][i
] = func
;
386 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
387 hw_error("register_ioport_write: invalid opaque");
388 ioport_opaque
[i
] = opaque
;
393 void isa_unassign_ioport(int start
, int length
)
397 for(i
= start
; i
< start
+ length
; i
++) {
398 ioport_read_table
[0][i
] = default_ioport_readb
;
399 ioport_read_table
[1][i
] = default_ioport_readw
;
400 ioport_read_table
[2][i
] = default_ioport_readl
;
402 ioport_write_table
[0][i
] = default_ioport_writeb
;
403 ioport_write_table
[1][i
] = default_ioport_writew
;
404 ioport_write_table
[2][i
] = default_ioport_writel
;
408 /***********************************************************/
410 void cpu_outb(CPUState
*env
, int addr
, int val
)
413 if (loglevel
& CPU_LOG_IOPORT
)
414 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
416 ioport_write(0, addr
, val
);
419 env
->last_io_time
= cpu_get_time_fast();
423 void cpu_outw(CPUState
*env
, int addr
, int val
)
426 if (loglevel
& CPU_LOG_IOPORT
)
427 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
429 ioport_write(1, addr
, val
);
432 env
->last_io_time
= cpu_get_time_fast();
436 void cpu_outl(CPUState
*env
, int addr
, int val
)
439 if (loglevel
& CPU_LOG_IOPORT
)
440 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
442 ioport_write(2, addr
, val
);
445 env
->last_io_time
= cpu_get_time_fast();
449 int cpu_inb(CPUState
*env
, int addr
)
452 val
= ioport_read(0, addr
);
454 if (loglevel
& CPU_LOG_IOPORT
)
455 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
459 env
->last_io_time
= cpu_get_time_fast();
464 int cpu_inw(CPUState
*env
, int addr
)
467 val
= ioport_read(1, addr
);
469 if (loglevel
& CPU_LOG_IOPORT
)
470 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
474 env
->last_io_time
= cpu_get_time_fast();
479 int cpu_inl(CPUState
*env
, int addr
)
482 val
= ioport_read(2, addr
);
484 if (loglevel
& CPU_LOG_IOPORT
)
485 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
489 env
->last_io_time
= cpu_get_time_fast();
494 /***********************************************************/
495 void hw_error(const char *fmt
, ...)
501 fprintf(stderr
, "qemu: hardware error: ");
502 vfprintf(stderr
, fmt
, ap
);
503 fprintf(stderr
, "\n");
504 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
505 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
507 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
509 cpu_dump_state(env
, stderr
, fprintf
, 0);
516 /***********************************************************/
519 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
520 static void *qemu_put_kbd_event_opaque
;
521 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
522 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
524 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
526 qemu_put_kbd_event_opaque
= opaque
;
527 qemu_put_kbd_event
= func
;
530 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
531 void *opaque
, int absolute
,
534 QEMUPutMouseEntry
*s
, *cursor
;
536 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
540 s
->qemu_put_mouse_event
= func
;
541 s
->qemu_put_mouse_event_opaque
= opaque
;
542 s
->qemu_put_mouse_event_absolute
= absolute
;
543 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
546 if (!qemu_put_mouse_event_head
) {
547 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
551 cursor
= qemu_put_mouse_event_head
;
552 while (cursor
->next
!= NULL
)
553 cursor
= cursor
->next
;
556 qemu_put_mouse_event_current
= s
;
561 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
563 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
565 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
568 cursor
= qemu_put_mouse_event_head
;
569 while (cursor
!= NULL
&& cursor
!= entry
) {
571 cursor
= cursor
->next
;
574 if (cursor
== NULL
) // does not exist or list empty
576 else if (prev
== NULL
) { // entry is head
577 qemu_put_mouse_event_head
= cursor
->next
;
578 if (qemu_put_mouse_event_current
== entry
)
579 qemu_put_mouse_event_current
= cursor
->next
;
580 qemu_free(entry
->qemu_put_mouse_event_name
);
585 prev
->next
= entry
->next
;
587 if (qemu_put_mouse_event_current
== entry
)
588 qemu_put_mouse_event_current
= prev
;
590 qemu_free(entry
->qemu_put_mouse_event_name
);
594 void kbd_put_keycode(int keycode
)
596 if (qemu_put_kbd_event
) {
597 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
601 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
603 QEMUPutMouseEvent
*mouse_event
;
604 void *mouse_event_opaque
;
607 if (!qemu_put_mouse_event_current
) {
612 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
614 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
617 if (graphic_rotate
) {
618 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
621 width
= graphic_width
- 1;
622 mouse_event(mouse_event_opaque
,
623 width
- dy
, dx
, dz
, buttons_state
);
625 mouse_event(mouse_event_opaque
,
626 dx
, dy
, dz
, buttons_state
);
630 int kbd_mouse_is_absolute(void)
632 if (!qemu_put_mouse_event_current
)
635 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
638 void do_info_mice(void)
640 QEMUPutMouseEntry
*cursor
;
643 if (!qemu_put_mouse_event_head
) {
644 term_printf("No mouse devices connected\n");
648 term_printf("Mouse devices available:\n");
649 cursor
= qemu_put_mouse_event_head
;
650 while (cursor
!= NULL
) {
651 term_printf("%c Mouse #%d: %s\n",
652 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
653 index
, cursor
->qemu_put_mouse_event_name
);
655 cursor
= cursor
->next
;
659 void do_mouse_set(int index
)
661 QEMUPutMouseEntry
*cursor
;
664 if (!qemu_put_mouse_event_head
) {
665 term_printf("No mouse devices connected\n");
669 cursor
= qemu_put_mouse_event_head
;
670 while (cursor
!= NULL
&& index
!= i
) {
672 cursor
= cursor
->next
;
676 qemu_put_mouse_event_current
= cursor
;
678 term_printf("Mouse at given index not found\n");
681 /* compute with 96 bit intermediate result: (a*b)/c */
682 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
687 #ifdef WORDS_BIGENDIAN
697 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
698 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
701 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
705 /***********************************************************/
706 /* real time host monotonic timer */
708 #define QEMU_TIMER_BASE 1000000000LL
712 static int64_t clock_freq
;
714 static void init_get_clock(void)
718 ret
= QueryPerformanceFrequency(&freq
);
720 fprintf(stderr
, "Could not calibrate ticks\n");
723 clock_freq
= freq
.QuadPart
;
726 static int64_t get_clock(void)
729 QueryPerformanceCounter(&ti
);
730 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
735 static int use_rt_clock
;
737 static void init_get_clock(void)
740 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
743 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
750 static int64_t get_clock(void)
752 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
755 clock_gettime(CLOCK_MONOTONIC
, &ts
);
756 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
760 /* XXX: using gettimeofday leads to problems if the date
761 changes, so it should be avoided. */
763 gettimeofday(&tv
, NULL
);
764 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
769 /* Return the virtual CPU time, based on the instruction counter. */
770 static int64_t cpu_get_icount(void)
773 CPUState
*env
= cpu_single_env
;;
774 icount
= qemu_icount
;
777 fprintf(stderr
, "Bad clock read\n");
778 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
780 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
783 /***********************************************************/
784 /* guest cycle counter */
786 static int64_t cpu_ticks_prev
;
787 static int64_t cpu_ticks_offset
;
788 static int64_t cpu_clock_offset
;
789 static int cpu_ticks_enabled
;
791 /* return the host CPU cycle counter and handle stop/restart */
792 int64_t cpu_get_ticks(void)
795 return cpu_get_icount();
797 if (!cpu_ticks_enabled
) {
798 return cpu_ticks_offset
;
801 ticks
= cpu_get_real_ticks();
802 if (cpu_ticks_prev
> ticks
) {
803 /* Note: non increasing ticks may happen if the host uses
805 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
807 cpu_ticks_prev
= ticks
;
808 return ticks
+ cpu_ticks_offset
;
812 /* return the host CPU monotonic timer and handle stop/restart */
813 static int64_t cpu_get_clock(void)
816 if (!cpu_ticks_enabled
) {
817 return cpu_clock_offset
;
820 return ti
+ cpu_clock_offset
;
824 /* enable cpu_get_ticks() */
825 void cpu_enable_ticks(void)
827 if (!cpu_ticks_enabled
) {
828 cpu_ticks_offset
-= cpu_get_real_ticks();
829 cpu_clock_offset
-= get_clock();
830 cpu_ticks_enabled
= 1;
834 /* disable cpu_get_ticks() : the clock is stopped. You must not call
835 cpu_get_ticks() after that. */
836 void cpu_disable_ticks(void)
838 if (cpu_ticks_enabled
) {
839 cpu_ticks_offset
= cpu_get_ticks();
840 cpu_clock_offset
= cpu_get_clock();
841 cpu_ticks_enabled
= 0;
845 /***********************************************************/
848 #define QEMU_TIMER_REALTIME 0
849 #define QEMU_TIMER_VIRTUAL 1
853 /* XXX: add frequency */
861 struct QEMUTimer
*next
;
864 struct qemu_alarm_timer
{
868 int (*start
)(struct qemu_alarm_timer
*t
);
869 void (*stop
)(struct qemu_alarm_timer
*t
);
870 void (*rearm
)(struct qemu_alarm_timer
*t
);
874 #define ALARM_FLAG_DYNTICKS 0x1
875 #define ALARM_FLAG_EXPIRED 0x2
877 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
879 return t
->flags
& ALARM_FLAG_DYNTICKS
;
882 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
884 if (!alarm_has_dynticks(t
))
890 /* TODO: MIN_TIMER_REARM_US should be optimized */
891 #define MIN_TIMER_REARM_US 250
893 static struct qemu_alarm_timer
*alarm_timer
;
895 static int alarm_timer_rfd
, alarm_timer_wfd
;
900 struct qemu_alarm_win32
{
904 } alarm_win32_data
= {0, NULL
, -1};
906 static int win32_start_timer(struct qemu_alarm_timer
*t
);
907 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
908 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
912 static int unix_start_timer(struct qemu_alarm_timer
*t
);
913 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
917 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
918 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
919 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
921 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
922 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
924 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
925 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
927 #endif /* __linux__ */
931 /* Correlation between real and virtual time is always going to be
932 fairly approximate, so ignore small variation.
933 When the guest is idle real and virtual time will be aligned in
935 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
937 static void icount_adjust(void)
942 static int64_t last_delta
;
943 /* If the VM is not running, then do nothing. */
947 cur_time
= cpu_get_clock();
948 cur_icount
= qemu_get_clock(vm_clock
);
949 delta
= cur_icount
- cur_time
;
950 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
952 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
953 && icount_time_shift
> 0) {
954 /* The guest is getting too far ahead. Slow time down. */
958 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
959 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
960 /* The guest is getting too far behind. Speed time up. */
964 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
967 static void icount_adjust_rt(void * opaque
)
969 qemu_mod_timer(icount_rt_timer
,
970 qemu_get_clock(rt_clock
) + 1000);
974 static void icount_adjust_vm(void * opaque
)
976 qemu_mod_timer(icount_vm_timer
,
977 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
981 static void init_icount_adjust(void)
983 /* Have both realtime and virtual time triggers for speed adjustment.
984 The realtime trigger catches emulated time passing too slowly,
985 the virtual time trigger catches emulated time passing too fast.
986 Realtime triggers occur even when idle, so use them less frequently
988 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
989 qemu_mod_timer(icount_rt_timer
,
990 qemu_get_clock(rt_clock
) + 1000);
991 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
992 qemu_mod_timer(icount_vm_timer
,
993 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
996 static struct qemu_alarm_timer alarm_timers
[] = {
999 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1000 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1001 /* HPET - if available - is preferred */
1002 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1003 /* ...otherwise try RTC */
1004 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1006 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1008 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1009 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1010 {"win32", 0, win32_start_timer
,
1011 win32_stop_timer
, NULL
, &alarm_win32_data
},
1016 static void show_available_alarms(void)
1020 printf("Available alarm timers, in order of precedence:\n");
1021 for (i
= 0; alarm_timers
[i
].name
; i
++)
1022 printf("%s\n", alarm_timers
[i
].name
);
1025 static void configure_alarms(char const *opt
)
1029 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1032 struct qemu_alarm_timer tmp
;
1034 if (!strcmp(opt
, "?")) {
1035 show_available_alarms();
1041 /* Reorder the array */
1042 name
= strtok(arg
, ",");
1044 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1045 if (!strcmp(alarm_timers
[i
].name
, name
))
1050 fprintf(stderr
, "Unknown clock %s\n", name
);
1059 tmp
= alarm_timers
[i
];
1060 alarm_timers
[i
] = alarm_timers
[cur
];
1061 alarm_timers
[cur
] = tmp
;
1065 name
= strtok(NULL
, ",");
1071 /* Disable remaining timers */
1072 for (i
= cur
; i
< count
; i
++)
1073 alarm_timers
[i
].name
= NULL
;
1075 show_available_alarms();
1080 QEMUClock
*rt_clock
;
1081 QEMUClock
*vm_clock
;
1083 static QEMUTimer
*active_timers
[2];
1085 static QEMUClock
*qemu_new_clock(int type
)
1088 clock
= qemu_mallocz(sizeof(QEMUClock
));
1095 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1099 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1102 ts
->opaque
= opaque
;
1106 void qemu_free_timer(QEMUTimer
*ts
)
1111 /* stop a timer, but do not dealloc it */
1112 void qemu_del_timer(QEMUTimer
*ts
)
1116 /* NOTE: this code must be signal safe because
1117 qemu_timer_expired() can be called from a signal. */
1118 pt
= &active_timers
[ts
->clock
->type
];
1131 /* modify the current timer so that it will be fired when current_time
1132 >= expire_time. The corresponding callback will be called. */
1133 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1139 /* add the timer in the sorted list */
1140 /* NOTE: this code must be signal safe because
1141 qemu_timer_expired() can be called from a signal. */
1142 pt
= &active_timers
[ts
->clock
->type
];
1147 if (t
->expire_time
> expire_time
)
1151 ts
->expire_time
= expire_time
;
1155 /* Rearm if necessary */
1156 if (pt
== &active_timers
[ts
->clock
->type
]) {
1157 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1158 qemu_rearm_alarm_timer(alarm_timer
);
1160 /* Interrupt execution to force deadline recalculation. */
1161 if (use_icount
&& cpu_single_env
) {
1162 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1167 int qemu_timer_pending(QEMUTimer
*ts
)
1170 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1177 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1181 return (timer_head
->expire_time
<= current_time
);
1184 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1190 if (!ts
|| ts
->expire_time
> current_time
)
1192 /* remove timer from the list before calling the callback */
1193 *ptimer_head
= ts
->next
;
1196 /* run the callback (the timer list can be modified) */
1201 int64_t qemu_get_clock(QEMUClock
*clock
)
1203 switch(clock
->type
) {
1204 case QEMU_TIMER_REALTIME
:
1205 return get_clock() / 1000000;
1207 case QEMU_TIMER_VIRTUAL
:
1209 return cpu_get_icount();
1211 return cpu_get_clock();
1216 static void init_timers(void)
1219 ticks_per_sec
= QEMU_TIMER_BASE
;
1220 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1221 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1225 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1227 uint64_t expire_time
;
1229 if (qemu_timer_pending(ts
)) {
1230 expire_time
= ts
->expire_time
;
1234 qemu_put_be64(f
, expire_time
);
1237 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1239 uint64_t expire_time
;
1241 expire_time
= qemu_get_be64(f
);
1242 if (expire_time
!= -1) {
1243 qemu_mod_timer(ts
, expire_time
);
1249 static void timer_save(QEMUFile
*f
, void *opaque
)
1251 if (cpu_ticks_enabled
) {
1252 hw_error("cannot save state if virtual timers are running");
1254 qemu_put_be64(f
, cpu_ticks_offset
);
1255 qemu_put_be64(f
, ticks_per_sec
);
1256 qemu_put_be64(f
, cpu_clock_offset
);
1259 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1261 if (version_id
!= 1 && version_id
!= 2)
1263 if (cpu_ticks_enabled
) {
1266 cpu_ticks_offset
=qemu_get_be64(f
);
1267 ticks_per_sec
=qemu_get_be64(f
);
1268 if (version_id
== 2) {
1269 cpu_clock_offset
=qemu_get_be64(f
);
1275 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1276 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1278 static void host_alarm_handler(int host_signum
)
1282 #define DISP_FREQ 1000
1284 static int64_t delta_min
= INT64_MAX
;
1285 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1287 ti
= qemu_get_clock(vm_clock
);
1288 if (last_clock
!= 0) {
1289 delta
= ti
- last_clock
;
1290 if (delta
< delta_min
)
1292 if (delta
> delta_max
)
1295 if (++count
== DISP_FREQ
) {
1296 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1297 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1298 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1299 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1300 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1302 delta_min
= INT64_MAX
;
1310 if (alarm_has_dynticks(alarm_timer
) ||
1312 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1313 qemu_get_clock(vm_clock
))) ||
1314 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1315 qemu_get_clock(rt_clock
))) {
1316 CPUState
*env
= next_cpu
;
1319 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1320 SetEvent(data
->host_alarm
);
1322 static const char byte
= 0;
1323 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1325 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1328 /* stop the currently executing cpu because a timer occured */
1329 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1331 if (env
->kqemu_enabled
) {
1332 kqemu_cpu_interrupt(env
);
1340 static int64_t qemu_next_deadline(void)
1344 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1345 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1346 qemu_get_clock(vm_clock
);
1348 /* To avoid problems with overflow limit this to 2^32. */
1358 #if defined(__linux__) || defined(_WIN32)
1359 static uint64_t qemu_next_deadline_dyntick(void)
1367 delta
= (qemu_next_deadline() + 999) / 1000;
1369 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1370 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1371 qemu_get_clock(rt_clock
))*1000;
1372 if (rtdelta
< delta
)
1376 if (delta
< MIN_TIMER_REARM_US
)
1377 delta
= MIN_TIMER_REARM_US
;
1385 /* Sets a specific flag */
1386 static int fcntl_setfl(int fd
, int flag
)
1390 flags
= fcntl(fd
, F_GETFL
);
1394 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1400 #if defined(__linux__)
1402 #define RTC_FREQ 1024
1404 static void enable_sigio_timer(int fd
)
1406 struct sigaction act
;
1409 sigfillset(&act
.sa_mask
);
1411 act
.sa_handler
= host_alarm_handler
;
1413 sigaction(SIGIO
, &act
, NULL
);
1414 fcntl_setfl(fd
, O_ASYNC
);
1415 fcntl(fd
, F_SETOWN
, getpid());
1418 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1420 struct hpet_info info
;
1423 fd
= open("/dev/hpet", O_RDONLY
);
1428 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1430 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1431 "error, but for better emulation accuracy type:\n"
1432 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1436 /* Check capabilities */
1437 r
= ioctl(fd
, HPET_INFO
, &info
);
1441 /* Enable periodic mode */
1442 r
= ioctl(fd
, HPET_EPI
, 0);
1443 if (info
.hi_flags
&& (r
< 0))
1446 /* Enable interrupt */
1447 r
= ioctl(fd
, HPET_IE_ON
, 0);
1451 enable_sigio_timer(fd
);
1452 t
->priv
= (void *)(long)fd
;
1460 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1462 int fd
= (long)t
->priv
;
1467 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1470 unsigned long current_rtc_freq
= 0;
1472 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1475 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1476 if (current_rtc_freq
!= RTC_FREQ
&&
1477 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1478 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1479 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1480 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1483 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1489 enable_sigio_timer(rtc_fd
);
1491 t
->priv
= (void *)(long)rtc_fd
;
1496 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1498 int rtc_fd
= (long)t
->priv
;
1503 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1507 struct sigaction act
;
1509 sigfillset(&act
.sa_mask
);
1511 act
.sa_handler
= host_alarm_handler
;
1513 sigaction(SIGALRM
, &act
, NULL
);
1515 ev
.sigev_value
.sival_int
= 0;
1516 ev
.sigev_notify
= SIGEV_SIGNAL
;
1517 ev
.sigev_signo
= SIGALRM
;
1519 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1520 perror("timer_create");
1522 /* disable dynticks */
1523 fprintf(stderr
, "Dynamic Ticks disabled\n");
1528 t
->priv
= (void *)host_timer
;
1533 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1535 timer_t host_timer
= (timer_t
)t
->priv
;
1537 timer_delete(host_timer
);
1540 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1542 timer_t host_timer
= (timer_t
)t
->priv
;
1543 struct itimerspec timeout
;
1544 int64_t nearest_delta_us
= INT64_MAX
;
1547 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1548 !active_timers
[QEMU_TIMER_VIRTUAL
])
1551 nearest_delta_us
= qemu_next_deadline_dyntick();
1553 /* check whether a timer is already running */
1554 if (timer_gettime(host_timer
, &timeout
)) {
1556 fprintf(stderr
, "Internal timer error: aborting\n");
1559 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1560 if (current_us
&& current_us
<= nearest_delta_us
)
1563 timeout
.it_interval
.tv_sec
= 0;
1564 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1565 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1566 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1567 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1569 fprintf(stderr
, "Internal timer error: aborting\n");
1574 #endif /* defined(__linux__) */
1576 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1578 struct sigaction act
;
1579 struct itimerval itv
;
1583 sigfillset(&act
.sa_mask
);
1585 act
.sa_handler
= host_alarm_handler
;
1587 sigaction(SIGALRM
, &act
, NULL
);
1589 itv
.it_interval
.tv_sec
= 0;
1590 /* for i386 kernel 2.6 to get 1 ms */
1591 itv
.it_interval
.tv_usec
= 999;
1592 itv
.it_value
.tv_sec
= 0;
1593 itv
.it_value
.tv_usec
= 10 * 1000;
1595 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1602 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1604 struct itimerval itv
;
1606 memset(&itv
, 0, sizeof(itv
));
1607 setitimer(ITIMER_REAL
, &itv
, NULL
);
1610 #endif /* !defined(_WIN32) */
1612 static void try_to_rearm_timer(void *opaque
)
1614 struct qemu_alarm_timer
*t
= opaque
;
1618 /* Drain the notify pipe */
1621 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1622 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1625 /* vm time timers */
1626 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
1627 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1628 qemu_get_clock(vm_clock
));
1630 /* real time timers */
1631 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1632 qemu_get_clock(rt_clock
));
1634 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1635 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1636 qemu_rearm_alarm_timer(alarm_timer
);
1642 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1645 struct qemu_alarm_win32
*data
= t
->priv
;
1648 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1649 if (!data
->host_alarm
) {
1650 perror("Failed CreateEvent");
1654 memset(&tc
, 0, sizeof(tc
));
1655 timeGetDevCaps(&tc
, sizeof(tc
));
1657 if (data
->period
< tc
.wPeriodMin
)
1658 data
->period
= tc
.wPeriodMin
;
1660 timeBeginPeriod(data
->period
);
1662 flags
= TIME_CALLBACK_FUNCTION
;
1663 if (alarm_has_dynticks(t
))
1664 flags
|= TIME_ONESHOT
;
1666 flags
|= TIME_PERIODIC
;
1668 data
->timerId
= timeSetEvent(1, // interval (ms)
1669 data
->period
, // resolution
1670 host_alarm_handler
, // function
1671 (DWORD
)t
, // parameter
1674 if (!data
->timerId
) {
1675 perror("Failed to initialize win32 alarm timer");
1677 timeEndPeriod(data
->period
);
1678 CloseHandle(data
->host_alarm
);
1682 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1687 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1689 struct qemu_alarm_win32
*data
= t
->priv
;
1691 timeKillEvent(data
->timerId
);
1692 timeEndPeriod(data
->period
);
1694 CloseHandle(data
->host_alarm
);
1697 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1699 struct qemu_alarm_win32
*data
= t
->priv
;
1700 uint64_t nearest_delta_us
;
1702 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1703 !active_timers
[QEMU_TIMER_VIRTUAL
])
1706 nearest_delta_us
= qemu_next_deadline_dyntick();
1707 nearest_delta_us
/= 1000;
1709 timeKillEvent(data
->timerId
);
1711 data
->timerId
= timeSetEvent(1,
1715 TIME_ONESHOT
| TIME_PERIODIC
);
1717 if (!data
->timerId
) {
1718 perror("Failed to re-arm win32 alarm timer");
1720 timeEndPeriod(data
->period
);
1721 CloseHandle(data
->host_alarm
);
1728 static int init_timer_alarm(void)
1730 struct qemu_alarm_timer
*t
= NULL
;
1740 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1744 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1748 alarm_timer_rfd
= fds
[0];
1749 alarm_timer_wfd
= fds
[1];
1752 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1753 t
= &alarm_timers
[i
];
1766 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1767 try_to_rearm_timer
, NULL
, t
);
1782 static void quit_timers(void)
1784 alarm_timer
->stop(alarm_timer
);
1788 /***********************************************************/
1789 /* host time/date access */
1790 void qemu_get_timedate(struct tm
*tm
, int offset
)
1797 if (rtc_date_offset
== -1) {
1801 ret
= localtime(&ti
);
1803 ti
-= rtc_date_offset
;
1807 memcpy(tm
, ret
, sizeof(struct tm
));
1810 int qemu_timedate_diff(struct tm
*tm
)
1814 if (rtc_date_offset
== -1)
1816 seconds
= mktimegm(tm
);
1818 seconds
= mktime(tm
);
1820 seconds
= mktimegm(tm
) + rtc_date_offset
;
1822 return seconds
- time(NULL
);
1826 static void socket_cleanup(void)
1831 static int socket_init(void)
1836 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1838 err
= WSAGetLastError();
1839 fprintf(stderr
, "WSAStartup: %d\n", err
);
1842 atexit(socket_cleanup
);
1847 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1852 while (*p
!= '\0' && *p
!= '=') {
1853 if (q
&& (q
- buf
) < buf_size
- 1)
1863 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1868 while (*p
!= '\0') {
1870 if (*(p
+ 1) != ',')
1874 if (q
&& (q
- buf
) < buf_size
- 1)
1884 int get_param_value(char *buf
, int buf_size
,
1885 const char *tag
, const char *str
)
1892 p
= get_opt_name(option
, sizeof(option
), p
);
1896 if (!strcmp(tag
, option
)) {
1897 (void)get_opt_value(buf
, buf_size
, p
);
1900 p
= get_opt_value(NULL
, 0, p
);
1909 int check_params(char *buf
, int buf_size
,
1910 const char * const *params
, const char *str
)
1917 p
= get_opt_name(buf
, buf_size
, p
);
1921 for(i
= 0; params
[i
] != NULL
; i
++)
1922 if (!strcmp(params
[i
], buf
))
1924 if (params
[i
] == NULL
)
1926 p
= get_opt_value(NULL
, 0, p
);
1934 /***********************************************************/
1935 /* Bluetooth support */
1938 static struct HCIInfo
*hci_table
[MAX_NICS
];
1940 static struct bt_vlan_s
{
1941 struct bt_scatternet_s net
;
1943 struct bt_vlan_s
*next
;
1946 /* find or alloc a new bluetooth "VLAN" */
1947 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1949 struct bt_vlan_s
**pvlan
, *vlan
;
1950 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1954 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1956 pvlan
= &first_bt_vlan
;
1957 while (*pvlan
!= NULL
)
1958 pvlan
= &(*pvlan
)->next
;
1963 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1967 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1972 static struct HCIInfo null_hci
= {
1973 .cmd_send
= null_hci_send
,
1974 .sco_send
= null_hci_send
,
1975 .acl_send
= null_hci_send
,
1976 .bdaddr_set
= null_hci_addr_set
,
1979 struct HCIInfo
*qemu_next_hci(void)
1981 if (cur_hci
== nb_hcis
)
1984 return hci_table
[cur_hci
++];
1987 static struct HCIInfo
*hci_init(const char *str
)
1990 struct bt_scatternet_s
*vlan
= 0;
1992 if (!strcmp(str
, "null"))
1995 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1997 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1998 else if (!strncmp(str
, "hci", 3)) {
2001 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2002 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2007 vlan
= qemu_find_bt_vlan(0);
2009 return bt_new_hci(vlan
);
2012 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2017 static int bt_hci_parse(const char *str
)
2019 struct HCIInfo
*hci
;
2022 if (nb_hcis
>= MAX_NICS
) {
2023 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2027 hci
= hci_init(str
);
2036 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2037 hci
->bdaddr_set(hci
, bdaddr
.b
);
2039 hci_table
[nb_hcis
++] = hci
;
2044 static void bt_vhci_add(int vlan_id
)
2046 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2049 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2050 "an empty scatternet %i\n", vlan_id
);
2052 bt_vhci_init(bt_new_hci(vlan
));
2055 static struct bt_device_s
*bt_device_add(const char *opt
)
2057 struct bt_scatternet_s
*vlan
;
2059 char *endp
= strstr(opt
, ",vlan=");
2060 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2063 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2066 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2068 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2073 vlan
= qemu_find_bt_vlan(vlan_id
);
2076 fprintf(stderr
, "qemu: warning: adding a slave device to "
2077 "an empty scatternet %i\n", vlan_id
);
2079 if (!strcmp(devname
, "keyboard"))
2080 return bt_keyboard_init(vlan
);
2082 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2086 static int bt_parse(const char *opt
)
2088 const char *endp
, *p
;
2091 if (strstart(opt
, "hci", &endp
)) {
2092 if (!*endp
|| *endp
== ',') {
2094 if (!strstart(endp
, ",vlan=", 0))
2097 return bt_hci_parse(opt
);
2099 } else if (strstart(opt
, "vhci", &endp
)) {
2100 if (!*endp
|| *endp
== ',') {
2102 if (strstart(endp
, ",vlan=", &p
)) {
2103 vlan
= strtol(p
, (char **) &endp
, 0);
2105 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2109 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2118 } else if (strstart(opt
, "device:", &endp
))
2119 return !bt_device_add(endp
);
2121 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2125 /***********************************************************/
2126 /* QEMU Block devices */
2128 #define HD_ALIAS "index=%d,media=disk"
2130 #define CDROM_ALIAS "index=1,media=cdrom"
2132 #define CDROM_ALIAS "index=2,media=cdrom"
2134 #define FD_ALIAS "index=%d,if=floppy"
2135 #define PFLASH_ALIAS "if=pflash"
2136 #define MTD_ALIAS "if=mtd"
2137 #define SD_ALIAS "index=0,if=sd"
2139 static int drive_add(const char *file
, const char *fmt
, ...)
2143 if (nb_drives_opt
>= MAX_DRIVES
) {
2144 fprintf(stderr
, "qemu: too many drives\n");
2148 drives_opt
[nb_drives_opt
].file
= file
;
2150 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2151 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2154 return nb_drives_opt
++;
2157 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2161 /* seek interface, bus and unit */
2163 for (index
= 0; index
< nb_drives
; index
++)
2164 if (drives_table
[index
].type
== type
&&
2165 drives_table
[index
].bus
== bus
&&
2166 drives_table
[index
].unit
== unit
)
2172 int drive_get_max_bus(BlockInterfaceType type
)
2178 for (index
= 0; index
< nb_drives
; index
++) {
2179 if(drives_table
[index
].type
== type
&&
2180 drives_table
[index
].bus
> max_bus
)
2181 max_bus
= drives_table
[index
].bus
;
2186 static void bdrv_format_print(void *opaque
, const char *name
)
2188 fprintf(stderr
, " %s", name
);
2191 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2192 QEMUMachine
*machine
)
2197 const char *mediastr
= "";
2198 BlockInterfaceType type
;
2199 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2200 int bus_id
, unit_id
;
2201 int cyls
, heads
, secs
, translation
;
2202 BlockDriverState
*bdrv
;
2203 BlockDriver
*drv
= NULL
;
2208 char *str
= arg
->opt
;
2209 static const char * const params
[] = { "bus", "unit", "if", "index",
2210 "cyls", "heads", "secs", "trans",
2211 "media", "snapshot", "file",
2212 "cache", "format", NULL
};
2214 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2215 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2221 cyls
= heads
= secs
= 0;
2224 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2228 if (machine
->use_scsi
) {
2230 max_devs
= MAX_SCSI_DEVS
;
2231 pstrcpy(devname
, sizeof(devname
), "scsi");
2234 max_devs
= MAX_IDE_DEVS
;
2235 pstrcpy(devname
, sizeof(devname
), "ide");
2239 /* extract parameters */
2241 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2242 bus_id
= strtol(buf
, NULL
, 0);
2244 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2249 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2250 unit_id
= strtol(buf
, NULL
, 0);
2252 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2257 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2258 pstrcpy(devname
, sizeof(devname
), buf
);
2259 if (!strcmp(buf
, "ide")) {
2261 max_devs
= MAX_IDE_DEVS
;
2262 } else if (!strcmp(buf
, "scsi")) {
2264 max_devs
= MAX_SCSI_DEVS
;
2265 } else if (!strcmp(buf
, "floppy")) {
2268 } else if (!strcmp(buf
, "pflash")) {
2271 } else if (!strcmp(buf
, "mtd")) {
2274 } else if (!strcmp(buf
, "sd")) {
2278 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2283 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2284 index
= strtol(buf
, NULL
, 0);
2286 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2291 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2292 cyls
= strtol(buf
, NULL
, 0);
2295 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2296 heads
= strtol(buf
, NULL
, 0);
2299 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2300 secs
= strtol(buf
, NULL
, 0);
2303 if (cyls
|| heads
|| secs
) {
2304 if (cyls
< 1 || cyls
> 16383) {
2305 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2308 if (heads
< 1 || heads
> 16) {
2309 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2312 if (secs
< 1 || secs
> 63) {
2313 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2318 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2321 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2325 if (!strcmp(buf
, "none"))
2326 translation
= BIOS_ATA_TRANSLATION_NONE
;
2327 else if (!strcmp(buf
, "lba"))
2328 translation
= BIOS_ATA_TRANSLATION_LBA
;
2329 else if (!strcmp(buf
, "auto"))
2330 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2332 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2337 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2338 if (!strcmp(buf
, "disk")) {
2340 } else if (!strcmp(buf
, "cdrom")) {
2341 if (cyls
|| secs
|| heads
) {
2343 "qemu: '%s' invalid physical CHS format\n", str
);
2346 media
= MEDIA_CDROM
;
2348 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2353 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2354 if (!strcmp(buf
, "on"))
2356 else if (!strcmp(buf
, "off"))
2359 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2364 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2365 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2367 else if (!strcmp(buf
, "writethrough"))
2369 else if (!strcmp(buf
, "writeback"))
2372 fprintf(stderr
, "qemu: invalid cache option\n");
2377 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2378 if (strcmp(buf
, "?") == 0) {
2379 fprintf(stderr
, "qemu: Supported formats:");
2380 bdrv_iterate_format(bdrv_format_print
, NULL
);
2381 fprintf(stderr
, "\n");
2384 drv
= bdrv_find_format(buf
);
2386 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2391 if (arg
->file
== NULL
)
2392 get_param_value(file
, sizeof(file
), "file", str
);
2394 pstrcpy(file
, sizeof(file
), arg
->file
);
2396 /* compute bus and unit according index */
2399 if (bus_id
!= 0 || unit_id
!= -1) {
2401 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2409 unit_id
= index
% max_devs
;
2410 bus_id
= index
/ max_devs
;
2414 /* if user doesn't specify a unit_id,
2415 * try to find the first free
2418 if (unit_id
== -1) {
2420 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2422 if (max_devs
&& unit_id
>= max_devs
) {
2423 unit_id
-= max_devs
;
2431 if (max_devs
&& unit_id
>= max_devs
) {
2432 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2433 str
, unit_id
, max_devs
- 1);
2438 * ignore multiple definitions
2441 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2446 if (type
== IF_IDE
|| type
== IF_SCSI
)
2447 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2449 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2450 devname
, bus_id
, mediastr
, unit_id
);
2452 snprintf(buf
, sizeof(buf
), "%s%s%i",
2453 devname
, mediastr
, unit_id
);
2454 bdrv
= bdrv_new(buf
);
2455 drives_table
[nb_drives
].bdrv
= bdrv
;
2456 drives_table
[nb_drives
].type
= type
;
2457 drives_table
[nb_drives
].bus
= bus_id
;
2458 drives_table
[nb_drives
].unit
= unit_id
;
2467 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2468 bdrv_set_translation_hint(bdrv
, translation
);
2472 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2477 /* FIXME: This isn't really a floppy, but it's a reasonable
2480 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2490 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2491 cache
= 2; /* always use write-back with snapshot */
2493 if (cache
== 0) /* no caching */
2494 bdrv_flags
|= BDRV_O_NOCACHE
;
2495 else if (cache
== 2) /* write-back */
2496 bdrv_flags
|= BDRV_O_CACHE_WB
;
2497 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2498 fprintf(stderr
, "qemu: could not open disk image %s\n",
2505 /***********************************************************/
2508 static USBPort
*used_usb_ports
;
2509 static USBPort
*free_usb_ports
;
2511 /* ??? Maybe change this to register a hub to keep track of the topology. */
2512 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2513 usb_attachfn attach
)
2515 port
->opaque
= opaque
;
2516 port
->index
= index
;
2517 port
->attach
= attach
;
2518 port
->next
= free_usb_ports
;
2519 free_usb_ports
= port
;
2522 int usb_device_add_dev(USBDevice
*dev
)
2526 /* Find a USB port to add the device to. */
2527 port
= free_usb_ports
;
2531 /* Create a new hub and chain it on. */
2532 free_usb_ports
= NULL
;
2533 port
->next
= used_usb_ports
;
2534 used_usb_ports
= port
;
2536 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2537 usb_attach(port
, hub
);
2538 port
= free_usb_ports
;
2541 free_usb_ports
= port
->next
;
2542 port
->next
= used_usb_ports
;
2543 used_usb_ports
= port
;
2544 usb_attach(port
, dev
);
2548 static int usb_device_add(const char *devname
)
2553 if (!free_usb_ports
)
2556 if (strstart(devname
, "host:", &p
)) {
2557 dev
= usb_host_device_open(p
);
2558 } else if (!strcmp(devname
, "mouse")) {
2559 dev
= usb_mouse_init();
2560 } else if (!strcmp(devname
, "tablet")) {
2561 dev
= usb_tablet_init();
2562 } else if (!strcmp(devname
, "keyboard")) {
2563 dev
= usb_keyboard_init();
2564 } else if (strstart(devname
, "disk:", &p
)) {
2565 dev
= usb_msd_init(p
);
2566 } else if (!strcmp(devname
, "wacom-tablet")) {
2567 dev
= usb_wacom_init();
2568 } else if (strstart(devname
, "serial:", &p
)) {
2569 dev
= usb_serial_init(p
);
2570 #ifdef CONFIG_BRLAPI
2571 } else if (!strcmp(devname
, "braille")) {
2572 dev
= usb_baum_init();
2574 } else if (strstart(devname
, "net:", &p
)) {
2577 if (net_client_init("nic", p
) < 0)
2579 nd_table
[nic
].model
= "usb";
2580 dev
= usb_net_init(&nd_table
[nic
]);
2581 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2582 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2583 bt_new_hci(qemu_find_bt_vlan(0)));
2590 return usb_device_add_dev(dev
);
2593 int usb_device_del_addr(int bus_num
, int addr
)
2599 if (!used_usb_ports
)
2605 lastp
= &used_usb_ports
;
2606 port
= used_usb_ports
;
2607 while (port
&& port
->dev
->addr
!= addr
) {
2608 lastp
= &port
->next
;
2616 *lastp
= port
->next
;
2617 usb_attach(port
, NULL
);
2618 dev
->handle_destroy(dev
);
2619 port
->next
= free_usb_ports
;
2620 free_usb_ports
= port
;
2624 static int usb_device_del(const char *devname
)
2629 if (strstart(devname
, "host:", &p
))
2630 return usb_host_device_close(p
);
2632 if (!used_usb_ports
)
2635 p
= strchr(devname
, '.');
2638 bus_num
= strtoul(devname
, NULL
, 0);
2639 addr
= strtoul(p
+ 1, NULL
, 0);
2641 return usb_device_del_addr(bus_num
, addr
);
2644 void do_usb_add(const char *devname
)
2646 usb_device_add(devname
);
2649 void do_usb_del(const char *devname
)
2651 usb_device_del(devname
);
2658 const char *speed_str
;
2661 term_printf("USB support not enabled\n");
2665 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2669 switch(dev
->speed
) {
2673 case USB_SPEED_FULL
:
2676 case USB_SPEED_HIGH
:
2683 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2684 0, dev
->addr
, speed_str
, dev
->devname
);
2688 /***********************************************************/
2689 /* PCMCIA/Cardbus */
2691 static struct pcmcia_socket_entry_s
{
2692 struct pcmcia_socket_s
*socket
;
2693 struct pcmcia_socket_entry_s
*next
;
2694 } *pcmcia_sockets
= 0;
2696 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2698 struct pcmcia_socket_entry_s
*entry
;
2700 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2701 entry
->socket
= socket
;
2702 entry
->next
= pcmcia_sockets
;
2703 pcmcia_sockets
= entry
;
2706 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2708 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2710 ptr
= &pcmcia_sockets
;
2711 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2712 if (entry
->socket
== socket
) {
2718 void pcmcia_info(void)
2720 struct pcmcia_socket_entry_s
*iter
;
2721 if (!pcmcia_sockets
)
2722 term_printf("No PCMCIA sockets\n");
2724 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2725 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2726 iter
->socket
->attached
? iter
->socket
->card_string
:
2730 /***********************************************************/
2733 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2737 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2741 static void dumb_display_init(DisplayState
*ds
)
2746 ds
->dpy_update
= dumb_update
;
2747 ds
->dpy_resize
= dumb_resize
;
2748 ds
->dpy_refresh
= NULL
;
2749 ds
->gui_timer_interval
= 0;
2753 /***********************************************************/
2756 #define MAX_IO_HANDLERS 64
2758 typedef struct IOHandlerRecord
{
2760 IOCanRWHandler
*fd_read_poll
;
2762 IOHandler
*fd_write
;
2765 /* temporary data */
2767 struct IOHandlerRecord
*next
;
2770 static IOHandlerRecord
*first_io_handler
;
2772 /* XXX: fd_read_poll should be suppressed, but an API change is
2773 necessary in the character devices to suppress fd_can_read(). */
2774 int qemu_set_fd_handler2(int fd
,
2775 IOCanRWHandler
*fd_read_poll
,
2777 IOHandler
*fd_write
,
2780 IOHandlerRecord
**pioh
, *ioh
;
2782 if (!fd_read
&& !fd_write
) {
2783 pioh
= &first_io_handler
;
2788 if (ioh
->fd
== fd
) {
2795 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2799 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2802 ioh
->next
= first_io_handler
;
2803 first_io_handler
= ioh
;
2806 ioh
->fd_read_poll
= fd_read_poll
;
2807 ioh
->fd_read
= fd_read
;
2808 ioh
->fd_write
= fd_write
;
2809 ioh
->opaque
= opaque
;
2815 int qemu_set_fd_handler(int fd
,
2817 IOHandler
*fd_write
,
2820 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2824 /***********************************************************/
2825 /* Polling handling */
2827 typedef struct PollingEntry
{
2830 struct PollingEntry
*next
;
2833 static PollingEntry
*first_polling_entry
;
2835 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2837 PollingEntry
**ppe
, *pe
;
2838 pe
= qemu_mallocz(sizeof(PollingEntry
));
2842 pe
->opaque
= opaque
;
2843 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2848 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2850 PollingEntry
**ppe
, *pe
;
2851 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2853 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2861 /***********************************************************/
2862 /* Wait objects support */
2863 typedef struct WaitObjects
{
2865 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2866 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2867 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2870 static WaitObjects wait_objects
= {0};
2872 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2874 WaitObjects
*w
= &wait_objects
;
2876 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2878 w
->events
[w
->num
] = handle
;
2879 w
->func
[w
->num
] = func
;
2880 w
->opaque
[w
->num
] = opaque
;
2885 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2888 WaitObjects
*w
= &wait_objects
;
2891 for (i
= 0; i
< w
->num
; i
++) {
2892 if (w
->events
[i
] == handle
)
2895 w
->events
[i
] = w
->events
[i
+ 1];
2896 w
->func
[i
] = w
->func
[i
+ 1];
2897 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2905 /***********************************************************/
2906 /* ram save/restore */
2908 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2912 v
= qemu_get_byte(f
);
2915 if (qemu_get_buffer(f
, buf
, len
) != len
)
2919 v
= qemu_get_byte(f
);
2920 memset(buf
, v
, len
);
2926 if (qemu_file_has_error(f
))
2932 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2937 if (qemu_get_be32(f
) != phys_ram_size
)
2939 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2940 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2947 #define BDRV_HASH_BLOCK_SIZE 1024
2948 #define IOBUF_SIZE 4096
2949 #define RAM_CBLOCK_MAGIC 0xfabe
2951 typedef struct RamDecompressState
{
2954 uint8_t buf
[IOBUF_SIZE
];
2955 } RamDecompressState
;
2957 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2960 memset(s
, 0, sizeof(*s
));
2962 ret
= inflateInit(&s
->zstream
);
2968 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2972 s
->zstream
.avail_out
= len
;
2973 s
->zstream
.next_out
= buf
;
2974 while (s
->zstream
.avail_out
> 0) {
2975 if (s
->zstream
.avail_in
== 0) {
2976 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2978 clen
= qemu_get_be16(s
->f
);
2979 if (clen
> IOBUF_SIZE
)
2981 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2982 s
->zstream
.avail_in
= clen
;
2983 s
->zstream
.next_in
= s
->buf
;
2985 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2986 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2993 static void ram_decompress_close(RamDecompressState
*s
)
2995 inflateEnd(&s
->zstream
);
2998 #define RAM_SAVE_FLAG_FULL 0x01
2999 #define RAM_SAVE_FLAG_COMPRESS 0x02
3000 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3001 #define RAM_SAVE_FLAG_PAGE 0x08
3002 #define RAM_SAVE_FLAG_EOS 0x10
3004 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3006 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3007 uint32_t *array
= (uint32_t *)page
;
3010 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3011 if (array
[i
] != val
)
3018 static int ram_save_block(QEMUFile
*f
)
3020 static ram_addr_t current_addr
= 0;
3021 ram_addr_t saved_addr
= current_addr
;
3022 ram_addr_t addr
= 0;
3025 while (addr
< phys_ram_size
) {
3026 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3029 cpu_physical_memory_reset_dirty(current_addr
,
3030 current_addr
+ TARGET_PAGE_SIZE
,
3031 MIGRATION_DIRTY_FLAG
);
3033 ch
= *(phys_ram_base
+ current_addr
);
3035 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3036 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3037 qemu_put_byte(f
, ch
);
3039 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3040 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3046 addr
+= TARGET_PAGE_SIZE
;
3047 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3053 static ram_addr_t ram_save_threshold
= 10;
3055 static ram_addr_t
ram_save_remaining(void)
3058 ram_addr_t count
= 0;
3060 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3061 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3068 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3073 /* Make sure all dirty bits are set */
3074 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3075 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3076 cpu_physical_memory_set_dirty(addr
);
3079 /* Enable dirty memory tracking */
3080 cpu_physical_memory_set_dirty_tracking(1);
3082 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3085 while (!qemu_file_rate_limit(f
)) {
3088 ret
= ram_save_block(f
);
3089 if (ret
== 0) /* no more blocks */
3093 /* try transferring iterative blocks of memory */
3096 cpu_physical_memory_set_dirty_tracking(0);
3098 /* flush all remaining blocks regardless of rate limiting */
3099 while (ram_save_block(f
) != 0);
3102 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3104 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3107 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3109 RamDecompressState s1
, *s
= &s1
;
3113 if (ram_decompress_open(s
, f
) < 0)
3115 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3116 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3117 fprintf(stderr
, "Error while reading ram block header\n");
3121 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3122 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3127 printf("Error block header\n");
3131 ram_decompress_close(s
);
3136 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3141 if (version_id
== 1)
3142 return ram_load_v1(f
, opaque
);
3144 if (version_id
== 2) {
3145 if (qemu_get_be32(f
) != phys_ram_size
)
3147 return ram_load_dead(f
, opaque
);
3150 if (version_id
!= 3)
3154 addr
= qemu_get_be64(f
);
3156 flags
= addr
& ~TARGET_PAGE_MASK
;
3157 addr
&= TARGET_PAGE_MASK
;
3159 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3160 if (addr
!= phys_ram_size
)
3164 if (flags
& RAM_SAVE_FLAG_FULL
) {
3165 if (ram_load_dead(f
, opaque
) < 0)
3169 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3170 uint8_t ch
= qemu_get_byte(f
);
3171 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3172 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3173 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3174 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3179 void qemu_service_io(void)
3181 CPUState
*env
= cpu_single_env
;
3183 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3185 if (env
->kqemu_enabled
) {
3186 kqemu_cpu_interrupt(env
);
3192 /***********************************************************/
3193 /* bottom halves (can be seen as timers which expire ASAP) */
3204 static QEMUBH
*first_bh
= NULL
;
3206 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3209 bh
= qemu_mallocz(sizeof(QEMUBH
));
3213 bh
->opaque
= opaque
;
3214 bh
->next
= first_bh
;
3219 int qemu_bh_poll(void)
3225 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3226 if (!bh
->deleted
&& bh
->scheduled
) {
3235 /* remove deleted bhs */
3249 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3257 void qemu_bh_schedule(QEMUBH
*bh
)
3259 CPUState
*env
= cpu_single_env
;
3264 /* stop the currently executing CPU to execute the BH ASAP */
3266 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3270 void qemu_bh_cancel(QEMUBH
*bh
)
3275 void qemu_bh_delete(QEMUBH
*bh
)
3281 static void qemu_bh_update_timeout(int *timeout
)
3285 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3286 if (!bh
->deleted
&& bh
->scheduled
) {
3288 /* idle bottom halves will be polled at least
3290 *timeout
= MIN(10, *timeout
);
3292 /* non-idle bottom halves will be executed
3301 /***********************************************************/
3302 /* machine registration */
3304 static QEMUMachine
*first_machine
= NULL
;
3306 int qemu_register_machine(QEMUMachine
*m
)
3309 pm
= &first_machine
;
3317 static QEMUMachine
*find_machine(const char *name
)
3321 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3322 if (!strcmp(m
->name
, name
))
3328 /***********************************************************/
3329 /* main execution loop */
3331 static void gui_update(void *opaque
)
3333 DisplayState
*ds
= opaque
;
3334 ds
->dpy_refresh(ds
);
3335 qemu_mod_timer(ds
->gui_timer
,
3336 (ds
->gui_timer_interval
?
3337 ds
->gui_timer_interval
:
3338 GUI_REFRESH_INTERVAL
)
3339 + qemu_get_clock(rt_clock
));
3342 struct vm_change_state_entry
{
3343 VMChangeStateHandler
*cb
;
3345 LIST_ENTRY (vm_change_state_entry
) entries
;
3348 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3350 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3353 VMChangeStateEntry
*e
;
3355 e
= qemu_mallocz(sizeof (*e
));
3361 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3365 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3367 LIST_REMOVE (e
, entries
);
3371 static void vm_state_notify(int running
)
3373 VMChangeStateEntry
*e
;
3375 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3376 e
->cb(e
->opaque
, running
);
3380 /* XXX: support several handlers */
3381 static VMStopHandler
*vm_stop_cb
;
3382 static void *vm_stop_opaque
;
3384 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3387 vm_stop_opaque
= opaque
;
3391 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3402 qemu_rearm_alarm_timer(alarm_timer
);
3406 void vm_stop(int reason
)
3409 cpu_disable_ticks();
3413 vm_stop_cb(vm_stop_opaque
, reason
);
3420 /* reset/shutdown handler */
3422 typedef struct QEMUResetEntry
{
3423 QEMUResetHandler
*func
;
3425 struct QEMUResetEntry
*next
;
3428 static QEMUResetEntry
*first_reset_entry
;
3429 static int reset_requested
;
3430 static int shutdown_requested
;
3431 static int powerdown_requested
;
3433 int qemu_shutdown_requested(void)
3435 int r
= shutdown_requested
;
3436 shutdown_requested
= 0;
3440 int qemu_reset_requested(void)
3442 int r
= reset_requested
;
3443 reset_requested
= 0;
3447 int qemu_powerdown_requested(void)
3449 int r
= powerdown_requested
;
3450 powerdown_requested
= 0;
3454 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3456 QEMUResetEntry
**pre
, *re
;
3458 pre
= &first_reset_entry
;
3459 while (*pre
!= NULL
)
3460 pre
= &(*pre
)->next
;
3461 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3463 re
->opaque
= opaque
;
3468 void qemu_system_reset(void)
3472 /* reset all devices */
3473 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3474 re
->func(re
->opaque
);
3478 void qemu_system_reset_request(void)
3481 shutdown_requested
= 1;
3483 reset_requested
= 1;
3486 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3489 void qemu_system_shutdown_request(void)
3491 shutdown_requested
= 1;
3493 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3496 void qemu_system_powerdown_request(void)
3498 powerdown_requested
= 1;
3500 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3504 void host_main_loop_wait(int *timeout
)
3510 /* XXX: need to suppress polling by better using win32 events */
3512 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3513 ret
|= pe
->func(pe
->opaque
);
3517 WaitObjects
*w
= &wait_objects
;
3519 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3520 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3521 if (w
->func
[ret
- WAIT_OBJECT_0
])
3522 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3524 /* Check for additional signaled events */
3525 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3527 /* Check if event is signaled */
3528 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3529 if(ret2
== WAIT_OBJECT_0
) {
3531 w
->func
[i
](w
->opaque
[i
]);
3532 } else if (ret2
== WAIT_TIMEOUT
) {
3534 err
= GetLastError();
3535 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3538 } else if (ret
== WAIT_TIMEOUT
) {
3540 err
= GetLastError();
3541 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3548 void host_main_loop_wait(int *timeout
)
3553 void main_loop_wait(int timeout
)
3555 IOHandlerRecord
*ioh
;
3556 fd_set rfds
, wfds
, xfds
;
3560 qemu_bh_update_timeout(&timeout
);
3562 host_main_loop_wait(&timeout
);
3564 /* poll any events */
3565 /* XXX: separate device handlers from system ones */
3570 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3574 (!ioh
->fd_read_poll
||
3575 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3576 FD_SET(ioh
->fd
, &rfds
);
3580 if (ioh
->fd_write
) {
3581 FD_SET(ioh
->fd
, &wfds
);
3587 tv
.tv_sec
= timeout
/ 1000;
3588 tv
.tv_usec
= (timeout
% 1000) * 1000;
3590 #if defined(CONFIG_SLIRP)
3591 if (slirp_is_inited()) {
3592 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3595 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3597 IOHandlerRecord
**pioh
;
3599 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3600 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3601 ioh
->fd_read(ioh
->opaque
);
3603 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3604 ioh
->fd_write(ioh
->opaque
);
3608 /* remove deleted IO handlers */
3609 pioh
= &first_io_handler
;
3619 #if defined(CONFIG_SLIRP)
3620 if (slirp_is_inited()) {
3626 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3630 /* Check bottom-halves last in case any of the earlier events triggered
3636 static int main_loop(void)
3639 #ifdef CONFIG_PROFILER
3644 cur_cpu
= first_cpu
;
3645 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3652 #ifdef CONFIG_PROFILER
3653 ti
= profile_getclock();
3658 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3659 env
->icount_decr
.u16
.low
= 0;
3660 env
->icount_extra
= 0;
3661 count
= qemu_next_deadline();
3662 count
= (count
+ (1 << icount_time_shift
) - 1)
3663 >> icount_time_shift
;
3664 qemu_icount
+= count
;
3665 decr
= (count
> 0xffff) ? 0xffff : count
;
3667 env
->icount_decr
.u16
.low
= decr
;
3668 env
->icount_extra
= count
;
3670 ret
= cpu_exec(env
);
3671 #ifdef CONFIG_PROFILER
3672 qemu_time
+= profile_getclock() - ti
;
3675 /* Fold pending instructions back into the
3676 instruction counter, and clear the interrupt flag. */
3677 qemu_icount
-= (env
->icount_decr
.u16
.low
3678 + env
->icount_extra
);
3679 env
->icount_decr
.u32
= 0;
3680 env
->icount_extra
= 0;
3682 next_cpu
= env
->next_cpu
?: first_cpu
;
3683 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3684 ret
= EXCP_INTERRUPT
;
3688 if (ret
== EXCP_HLT
) {
3689 /* Give the next CPU a chance to run. */
3693 if (ret
!= EXCP_HALTED
)
3695 /* all CPUs are halted ? */
3701 if (shutdown_requested
) {
3702 ret
= EXCP_INTERRUPT
;
3710 if (reset_requested
) {
3711 reset_requested
= 0;
3712 qemu_system_reset();
3713 ret
= EXCP_INTERRUPT
;
3715 if (powerdown_requested
) {
3716 powerdown_requested
= 0;
3717 qemu_system_powerdown();
3718 ret
= EXCP_INTERRUPT
;
3720 if (unlikely(ret
== EXCP_DEBUG
)) {
3721 vm_stop(EXCP_DEBUG
);
3723 /* If all cpus are halted then wait until the next IRQ */
3724 /* XXX: use timeout computed from timers */
3725 if (ret
== EXCP_HALTED
) {
3729 /* Advance virtual time to the next event. */
3730 if (use_icount
== 1) {
3731 /* When not using an adaptive execution frequency
3732 we tend to get badly out of sync with real time,
3733 so just delay for a reasonable amount of time. */
3736 delta
= cpu_get_icount() - cpu_get_clock();
3739 /* If virtual time is ahead of real time then just
3741 timeout
= (delta
/ 1000000) + 1;
3743 /* Wait for either IO to occur or the next
3745 add
= qemu_next_deadline();
3746 /* We advance the timer before checking for IO.
3747 Limit the amount we advance so that early IO
3748 activity won't get the guest too far ahead. */
3752 add
= (add
+ (1 << icount_time_shift
) - 1)
3753 >> icount_time_shift
;
3755 timeout
= delta
/ 1000000;
3766 if (shutdown_requested
) {
3767 ret
= EXCP_INTERRUPT
;
3772 #ifdef CONFIG_PROFILER
3773 ti
= profile_getclock();
3775 main_loop_wait(timeout
);
3776 #ifdef CONFIG_PROFILER
3777 dev_time
+= profile_getclock() - ti
;
3780 cpu_disable_ticks();
3784 static void help(int exitcode
)
3786 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3787 "usage: %s [options] [disk_image]\n"
3789 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3791 "Standard options:\n"
3792 "-M machine select emulated machine (-M ? for list)\n"
3793 "-cpu cpu select CPU (-cpu ? for list)\n"
3794 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3795 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3796 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3797 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3798 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3799 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3800 " [,cache=writethrough|writeback|none][,format=f]\n"
3801 " use 'file' as a drive image\n"
3802 "-mtdblock file use 'file' as on-board Flash memory image\n"
3803 "-sd file use 'file' as SecureDigital card image\n"
3804 "-pflash file use 'file' as a parallel flash image\n"
3805 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3806 "-snapshot write to temporary files instead of disk image files\n"
3808 "-no-frame open SDL window without a frame and window decorations\n"
3809 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3810 "-no-quit disable SDL window close capability\n"
3813 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3815 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3816 "-smp n set the number of CPUs to 'n' [default=1]\n"
3817 "-nographic disable graphical output and redirect serial I/Os to console\n"
3818 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3820 "-k language use keyboard layout (for example \"fr\" for French)\n"
3823 "-audio-help print list of audio drivers and their options\n"
3824 "-soundhw c1,... enable audio support\n"
3825 " and only specified sound cards (comma separated list)\n"
3826 " use -soundhw ? to get the list of supported cards\n"
3827 " use -soundhw all to enable all of them\n"
3829 "-vga [std|cirrus|vmware]\n"
3830 " select video card type\n"
3831 "-localtime set the real time clock to local time [default=utc]\n"
3832 "-full-screen start in full screen\n"
3834 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3836 "-usb enable the USB driver (will be the default soon)\n"
3837 "-usbdevice name add the host or guest USB device 'name'\n"
3838 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3839 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3841 "-name string set the name of the guest\n"
3842 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3844 "Network options:\n"
3845 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
3846 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3848 "-net user[,vlan=n][,hostname=host]\n"
3849 " connect the user mode network stack to VLAN 'n' and send\n"
3850 " hostname 'host' to DHCP clients\n"
3853 "-net tap[,vlan=n],ifname=name\n"
3854 " connect the host TAP network interface to VLAN 'n'\n"
3856 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3857 " connect the host TAP network interface to VLAN 'n' and use the\n"
3858 " network scripts 'file' (default=%s)\n"
3859 " and 'dfile' (default=%s);\n"
3860 " use '[down]script=no' to disable script execution;\n"
3861 " use 'fd=h' to connect to an already opened TAP interface\n"
3863 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3864 " connect the vlan 'n' to another VLAN using a socket connection\n"
3865 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
3866 " connect the vlan 'n' to multicast maddr and port\n"
3868 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3869 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3870 " on host and listening for incoming connections on 'socketpath'.\n"
3871 " Use group 'groupname' and mode 'octalmode' to change default\n"
3872 " ownership and permissions for communication port.\n"
3874 "-net none use it alone to have zero network devices; if no -net option\n"
3875 " is provided, the default is '-net nic -net user'\n"
3877 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3878 "-bt hci,host[:id]\n"
3879 " Use host's HCI with the given name\n"
3880 "-bt hci[,vlan=n]\n"
3881 " Emulate a standard HCI in virtual scatternet 'n'\n"
3882 "-bt vhci[,vlan=n]\n"
3883 " Add host computer to virtual scatternet 'n' using VHCI\n"
3884 "-bt device:dev[,vlan=n]\n"
3885 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3888 "-tftp dir allow tftp access to files in dir [-net user]\n"
3889 "-bootp file advertise file in BOOTP replies\n"
3891 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3893 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3894 " redirect TCP or UDP connections from host to guest [-net user]\n"
3897 "Linux boot specific:\n"
3898 "-kernel bzImage use 'bzImage' as kernel image\n"
3899 "-append cmdline use 'cmdline' as kernel command line\n"
3900 "-initrd file use 'file' as initial ram disk\n"
3902 "Debug/Expert options:\n"
3903 "-monitor dev redirect the monitor to char device 'dev'\n"
3904 "-serial dev redirect the serial port to char device 'dev'\n"
3905 "-parallel dev redirect the parallel port to char device 'dev'\n"
3906 "-pidfile file Write PID to 'file'\n"
3907 "-S freeze CPU at startup (use 'c' to start execution)\n"
3908 "-s wait gdb connection to port\n"
3909 "-p port set gdb connection port [default=%s]\n"
3910 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3911 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3912 " translation (t=none or lba) (usually qemu can guess them)\n"
3913 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3915 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3916 "-no-kqemu disable KQEMU kernel module usage\n"
3919 "-enable-kvm enable KVM full virtualization support\n"
3922 "-no-acpi disable ACPI\n"
3924 #ifdef CONFIG_CURSES
3925 "-curses use a curses/ncurses interface instead of SDL\n"
3927 "-no-reboot exit instead of rebooting\n"
3928 "-no-shutdown stop before shutdown\n"
3929 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3930 "-vnc display start a VNC server on display\n"
3932 "-daemonize daemonize QEMU after initializing\n"
3934 "-option-rom rom load a file, rom, into the option ROM space\n"
3936 "-prom-env variable=value set OpenBIOS nvram variables\n"
3938 "-clock force the use of the given methods for timer alarm.\n"
3939 " To see what timers are available use -clock ?\n"
3940 "-startdate select initial date of the clock\n"
3941 "-icount [N|auto]\n"
3942 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
3944 "During emulation, the following keys are useful:\n"
3945 "ctrl-alt-f toggle full screen\n"
3946 "ctrl-alt-n switch to virtual console 'n'\n"
3947 "ctrl-alt toggle mouse and keyboard grab\n"
3949 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3954 DEFAULT_NETWORK_SCRIPT
,
3955 DEFAULT_NETWORK_DOWN_SCRIPT
,
3957 DEFAULT_GDBSTUB_PORT
,
3962 #define HAS_ARG 0x0001
3977 QEMU_OPTION_mtdblock
,
3981 QEMU_OPTION_snapshot
,
3983 QEMU_OPTION_no_fd_bootchk
,
3986 QEMU_OPTION_nographic
,
3987 QEMU_OPTION_portrait
,
3989 QEMU_OPTION_audio_help
,
3990 QEMU_OPTION_soundhw
,
4012 QEMU_OPTION_localtime
,
4016 QEMU_OPTION_monitor
,
4018 QEMU_OPTION_parallel
,
4020 QEMU_OPTION_full_screen
,
4021 QEMU_OPTION_no_frame
,
4022 QEMU_OPTION_alt_grab
,
4023 QEMU_OPTION_no_quit
,
4024 QEMU_OPTION_pidfile
,
4025 QEMU_OPTION_no_kqemu
,
4026 QEMU_OPTION_kernel_kqemu
,
4027 QEMU_OPTION_enable_kvm
,
4028 QEMU_OPTION_win2k_hack
,
4030 QEMU_OPTION_usbdevice
,
4033 QEMU_OPTION_no_acpi
,
4035 QEMU_OPTION_no_reboot
,
4036 QEMU_OPTION_no_shutdown
,
4037 QEMU_OPTION_show_cursor
,
4038 QEMU_OPTION_daemonize
,
4039 QEMU_OPTION_option_rom
,
4040 QEMU_OPTION_semihosting
,
4042 QEMU_OPTION_prom_env
,
4043 QEMU_OPTION_old_param
,
4045 QEMU_OPTION_startdate
,
4046 QEMU_OPTION_tb_size
,
4049 QEMU_OPTION_incoming
,
4052 typedef struct QEMUOption
{
4058 static const QEMUOption qemu_options
[] = {
4059 { "h", 0, QEMU_OPTION_h
},
4060 { "help", 0, QEMU_OPTION_h
},
4062 { "M", HAS_ARG
, QEMU_OPTION_M
},
4063 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4064 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4065 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4066 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4067 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4068 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4069 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4070 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4071 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4072 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4073 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4074 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4075 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4076 { "snapshot", 0, QEMU_OPTION_snapshot
},
4078 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4080 { "m", HAS_ARG
, QEMU_OPTION_m
},
4081 { "nographic", 0, QEMU_OPTION_nographic
},
4082 { "portrait", 0, QEMU_OPTION_portrait
},
4083 { "k", HAS_ARG
, QEMU_OPTION_k
},
4085 { "audio-help", 0, QEMU_OPTION_audio_help
},
4086 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4089 { "net", HAS_ARG
, QEMU_OPTION_net
},
4091 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4092 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4094 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4096 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4098 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4100 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4101 { "append", HAS_ARG
, QEMU_OPTION_append
},
4102 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4104 { "S", 0, QEMU_OPTION_S
},
4105 { "s", 0, QEMU_OPTION_s
},
4106 { "p", HAS_ARG
, QEMU_OPTION_p
},
4107 { "d", HAS_ARG
, QEMU_OPTION_d
},
4108 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4109 { "L", HAS_ARG
, QEMU_OPTION_L
},
4110 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4112 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4113 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4116 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4118 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4119 { "g", 1, QEMU_OPTION_g
},
4121 { "localtime", 0, QEMU_OPTION_localtime
},
4122 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4123 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4124 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4125 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4126 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4127 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4128 { "full-screen", 0, QEMU_OPTION_full_screen
},
4130 { "no-frame", 0, QEMU_OPTION_no_frame
},
4131 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4132 { "no-quit", 0, QEMU_OPTION_no_quit
},
4134 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4135 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4136 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4137 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4138 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4139 #ifdef CONFIG_CURSES
4140 { "curses", 0, QEMU_OPTION_curses
},
4142 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4144 /* temporary options */
4145 { "usb", 0, QEMU_OPTION_usb
},
4146 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4147 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4148 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4149 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4150 { "daemonize", 0, QEMU_OPTION_daemonize
},
4151 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4152 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4153 { "semihosting", 0, QEMU_OPTION_semihosting
},
4155 { "name", HAS_ARG
, QEMU_OPTION_name
},
4156 #if defined(TARGET_SPARC)
4157 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4159 #if defined(TARGET_ARM)
4160 { "old-param", 0, QEMU_OPTION_old_param
},
4162 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4163 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4164 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4165 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4166 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4170 /* password input */
4172 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4177 if (!bdrv_is_encrypted(bs
))
4180 term_printf("%s is encrypted.\n", name
);
4181 for(i
= 0; i
< 3; i
++) {
4182 monitor_readline("Password: ", 1, password
, sizeof(password
));
4183 if (bdrv_set_key(bs
, password
) == 0)
4185 term_printf("invalid password\n");
4190 static BlockDriverState
*get_bdrv(int index
)
4192 if (index
> nb_drives
)
4194 return drives_table
[index
].bdrv
;
4197 static void read_passwords(void)
4199 BlockDriverState
*bs
;
4202 for(i
= 0; i
< 6; i
++) {
4205 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4210 struct soundhw soundhw
[] = {
4211 #ifdef HAS_AUDIO_CHOICE
4212 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4218 { .init_isa
= pcspk_audio_init
}
4223 "Creative Sound Blaster 16",
4226 { .init_isa
= SB16_init
}
4229 #ifdef CONFIG_CS4231A
4235 { .init_isa
= cs4231a_init
}
4243 "Yamaha YMF262 (OPL3)",
4245 "Yamaha YM3812 (OPL2)",
4249 { .init_isa
= Adlib_init
}
4256 "Gravis Ultrasound GF1",
4259 { .init_isa
= GUS_init
}
4266 "Intel 82801AA AC97 Audio",
4269 { .init_pci
= ac97_init
}
4275 "ENSONIQ AudioPCI ES1370",
4278 { .init_pci
= es1370_init
}
4282 { NULL
, NULL
, 0, 0, { NULL
} }
4285 static void select_soundhw (const char *optarg
)
4289 if (*optarg
== '?') {
4292 printf ("Valid sound card names (comma separated):\n");
4293 for (c
= soundhw
; c
->name
; ++c
) {
4294 printf ("%-11s %s\n", c
->name
, c
->descr
);
4296 printf ("\n-soundhw all will enable all of the above\n");
4297 exit (*optarg
!= '?');
4305 if (!strcmp (optarg
, "all")) {
4306 for (c
= soundhw
; c
->name
; ++c
) {
4314 e
= strchr (p
, ',');
4315 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4317 for (c
= soundhw
; c
->name
; ++c
) {
4318 if (!strncmp (c
->name
, p
, l
)) {
4327 "Unknown sound card name (too big to show)\n");
4330 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4335 p
+= l
+ (e
!= NULL
);
4339 goto show_valid_cards
;
4344 static void select_vgahw (const char *p
)
4348 if (strstart(p
, "std", &opts
)) {
4349 cirrus_vga_enabled
= 0;
4351 } else if (strstart(p
, "cirrus", &opts
)) {
4352 cirrus_vga_enabled
= 1;
4354 } else if (strstart(p
, "vmware", &opts
)) {
4355 cirrus_vga_enabled
= 0;
4359 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4363 const char *nextopt
;
4365 if (strstart(opts
, ",retrace=", &nextopt
)) {
4367 if (strstart(opts
, "dumb", &nextopt
))
4368 vga_retrace_method
= VGA_RETRACE_DUMB
;
4369 else if (strstart(opts
, "precise", &nextopt
))
4370 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4371 else goto invalid_vga
;
4372 } else goto invalid_vga
;
4378 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4380 exit(STATUS_CONTROL_C_EXIT
);
4385 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4389 if(strlen(str
) != 36)
4392 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4393 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4394 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4402 #define MAX_NET_CLIENTS 32
4406 static void termsig_handler(int signal
)
4408 qemu_system_shutdown_request();
4411 static void termsig_setup(void)
4413 struct sigaction act
;
4415 memset(&act
, 0, sizeof(act
));
4416 act
.sa_handler
= termsig_handler
;
4417 sigaction(SIGINT
, &act
, NULL
);
4418 sigaction(SIGHUP
, &act
, NULL
);
4419 sigaction(SIGTERM
, &act
, NULL
);
4424 int main(int argc
, char **argv
)
4426 #ifdef CONFIG_GDBSTUB
4428 const char *gdbstub_port
;
4430 uint32_t boot_devices_bitmap
= 0;
4432 int snapshot
, linux_boot
, net_boot
;
4433 const char *initrd_filename
;
4434 const char *kernel_filename
, *kernel_cmdline
;
4435 const char *boot_devices
= "";
4436 DisplayState
*ds
= &display_state
;
4437 int cyls
, heads
, secs
, translation
;
4438 const char *net_clients
[MAX_NET_CLIENTS
];
4440 const char *bt_opts
[MAX_BT_CMDLINE
];
4444 const char *r
, *optarg
;
4445 CharDriverState
*monitor_hd
;
4446 const char *monitor_device
;
4447 const char *serial_devices
[MAX_SERIAL_PORTS
];
4448 int serial_device_index
;
4449 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4450 int parallel_device_index
;
4451 const char *loadvm
= NULL
;
4452 QEMUMachine
*machine
;
4453 const char *cpu_model
;
4454 const char *usb_devices
[MAX_USB_CMDLINE
];
4455 int usb_devices_index
;
4458 const char *pid_file
= NULL
;
4460 const char *incoming
= NULL
;
4462 LIST_INIT (&vm_change_state_head
);
4465 struct sigaction act
;
4466 sigfillset(&act
.sa_mask
);
4468 act
.sa_handler
= SIG_IGN
;
4469 sigaction(SIGPIPE
, &act
, NULL
);
4472 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4473 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4474 QEMU to run on a single CPU */
4479 h
= GetCurrentProcess();
4480 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4481 for(i
= 0; i
< 32; i
++) {
4482 if (mask
& (1 << i
))
4487 SetProcessAffinityMask(h
, mask
);
4493 register_machines();
4494 machine
= first_machine
;
4496 initrd_filename
= NULL
;
4498 vga_ram_size
= VGA_RAM_SIZE
;
4499 #ifdef CONFIG_GDBSTUB
4501 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4506 kernel_filename
= NULL
;
4507 kernel_cmdline
= "";
4508 cyls
= heads
= secs
= 0;
4509 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4510 monitor_device
= "vc";
4512 serial_devices
[0] = "vc:80Cx24C";
4513 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4514 serial_devices
[i
] = NULL
;
4515 serial_device_index
= 0;
4517 parallel_devices
[0] = "vc:640x480";
4518 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4519 parallel_devices
[i
] = NULL
;
4520 parallel_device_index
= 0;
4522 usb_devices_index
= 0;
4541 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4543 const QEMUOption
*popt
;
4546 /* Treat --foo the same as -foo. */
4549 popt
= qemu_options
;
4552 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4556 if (!strcmp(popt
->name
, r
+ 1))
4560 if (popt
->flags
& HAS_ARG
) {
4561 if (optind
>= argc
) {
4562 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4566 optarg
= argv
[optind
++];
4571 switch(popt
->index
) {
4573 machine
= find_machine(optarg
);
4576 printf("Supported machines are:\n");
4577 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4578 printf("%-10s %s%s\n",
4580 m
== first_machine
? " (default)" : "");
4582 exit(*optarg
!= '?');
4585 case QEMU_OPTION_cpu
:
4586 /* hw initialization will check this */
4587 if (*optarg
== '?') {
4588 /* XXX: implement xxx_cpu_list for targets that still miss it */
4589 #if defined(cpu_list)
4590 cpu_list(stdout
, &fprintf
);
4597 case QEMU_OPTION_initrd
:
4598 initrd_filename
= optarg
;
4600 case QEMU_OPTION_hda
:
4602 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4604 hda_index
= drive_add(optarg
, HD_ALIAS
4605 ",cyls=%d,heads=%d,secs=%d%s",
4606 0, cyls
, heads
, secs
,
4607 translation
== BIOS_ATA_TRANSLATION_LBA
?
4609 translation
== BIOS_ATA_TRANSLATION_NONE
?
4610 ",trans=none" : "");
4612 case QEMU_OPTION_hdb
:
4613 case QEMU_OPTION_hdc
:
4614 case QEMU_OPTION_hdd
:
4615 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4617 case QEMU_OPTION_drive
:
4618 drive_add(NULL
, "%s", optarg
);
4620 case QEMU_OPTION_mtdblock
:
4621 drive_add(optarg
, MTD_ALIAS
);
4623 case QEMU_OPTION_sd
:
4624 drive_add(optarg
, SD_ALIAS
);
4626 case QEMU_OPTION_pflash
:
4627 drive_add(optarg
, PFLASH_ALIAS
);
4629 case QEMU_OPTION_snapshot
:
4632 case QEMU_OPTION_hdachs
:
4636 cyls
= strtol(p
, (char **)&p
, 0);
4637 if (cyls
< 1 || cyls
> 16383)
4642 heads
= strtol(p
, (char **)&p
, 0);
4643 if (heads
< 1 || heads
> 16)
4648 secs
= strtol(p
, (char **)&p
, 0);
4649 if (secs
< 1 || secs
> 63)
4653 if (!strcmp(p
, "none"))
4654 translation
= BIOS_ATA_TRANSLATION_NONE
;
4655 else if (!strcmp(p
, "lba"))
4656 translation
= BIOS_ATA_TRANSLATION_LBA
;
4657 else if (!strcmp(p
, "auto"))
4658 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4661 } else if (*p
!= '\0') {
4663 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4666 if (hda_index
!= -1)
4667 snprintf(drives_opt
[hda_index
].opt
,
4668 sizeof(drives_opt
[hda_index
].opt
),
4669 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4670 0, cyls
, heads
, secs
,
4671 translation
== BIOS_ATA_TRANSLATION_LBA
?
4673 translation
== BIOS_ATA_TRANSLATION_NONE
?
4674 ",trans=none" : "");
4677 case QEMU_OPTION_nographic
:
4680 #ifdef CONFIG_CURSES
4681 case QEMU_OPTION_curses
:
4685 case QEMU_OPTION_portrait
:
4688 case QEMU_OPTION_kernel
:
4689 kernel_filename
= optarg
;
4691 case QEMU_OPTION_append
:
4692 kernel_cmdline
= optarg
;
4694 case QEMU_OPTION_cdrom
:
4695 drive_add(optarg
, CDROM_ALIAS
);
4697 case QEMU_OPTION_boot
:
4698 boot_devices
= optarg
;
4699 /* We just do some generic consistency checks */
4701 /* Could easily be extended to 64 devices if needed */
4704 boot_devices_bitmap
= 0;
4705 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4706 /* Allowed boot devices are:
4707 * a b : floppy disk drives
4708 * c ... f : IDE disk drives
4709 * g ... m : machine implementation dependant drives
4710 * n ... p : network devices
4711 * It's up to each machine implementation to check
4712 * if the given boot devices match the actual hardware
4713 * implementation and firmware features.
4715 if (*p
< 'a' || *p
> 'q') {
4716 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4719 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4721 "Boot device '%c' was given twice\n",*p
);
4724 boot_devices_bitmap
|= 1 << (*p
- 'a');
4728 case QEMU_OPTION_fda
:
4729 case QEMU_OPTION_fdb
:
4730 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4733 case QEMU_OPTION_no_fd_bootchk
:
4737 case QEMU_OPTION_net
:
4738 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4739 fprintf(stderr
, "qemu: too many network clients\n");
4742 net_clients
[nb_net_clients
] = optarg
;
4746 case QEMU_OPTION_tftp
:
4747 tftp_prefix
= optarg
;
4749 case QEMU_OPTION_bootp
:
4750 bootp_filename
= optarg
;
4753 case QEMU_OPTION_smb
:
4754 net_slirp_smb(optarg
);
4757 case QEMU_OPTION_redir
:
4758 net_slirp_redir(optarg
);
4761 case QEMU_OPTION_bt
:
4762 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4763 fprintf(stderr
, "qemu: too many bluetooth options\n");
4766 bt_opts
[nb_bt_opts
++] = optarg
;
4769 case QEMU_OPTION_audio_help
:
4773 case QEMU_OPTION_soundhw
:
4774 select_soundhw (optarg
);
4780 case QEMU_OPTION_m
: {
4784 value
= strtoul(optarg
, &ptr
, 10);
4786 case 0: case 'M': case 'm':
4793 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4797 /* On 32-bit hosts, QEMU is limited by virtual address space */
4798 if (value
> (2047 << 20)
4800 && HOST_LONG_BITS
== 32
4803 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4806 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4807 fprintf(stderr
, "qemu: ram size too large\n");
4816 const CPULogItem
*item
;
4818 mask
= cpu_str_to_log_mask(optarg
);
4820 printf("Log items (comma separated):\n");
4821 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4822 printf("%-10s %s\n", item
->name
, item
->help
);
4829 #ifdef CONFIG_GDBSTUB
4834 gdbstub_port
= optarg
;
4840 case QEMU_OPTION_bios
:
4847 keyboard_layout
= optarg
;
4849 case QEMU_OPTION_localtime
:
4852 case QEMU_OPTION_vga
:
4853 select_vgahw (optarg
);
4860 w
= strtol(p
, (char **)&p
, 10);
4863 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4869 h
= strtol(p
, (char **)&p
, 10);
4874 depth
= strtol(p
, (char **)&p
, 10);
4875 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4876 depth
!= 24 && depth
!= 32)
4878 } else if (*p
== '\0') {
4879 depth
= graphic_depth
;
4886 graphic_depth
= depth
;
4889 case QEMU_OPTION_echr
:
4892 term_escape_char
= strtol(optarg
, &r
, 0);
4894 printf("Bad argument to echr\n");
4897 case QEMU_OPTION_monitor
:
4898 monitor_device
= optarg
;
4900 case QEMU_OPTION_serial
:
4901 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4902 fprintf(stderr
, "qemu: too many serial ports\n");
4905 serial_devices
[serial_device_index
] = optarg
;
4906 serial_device_index
++;
4908 case QEMU_OPTION_parallel
:
4909 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4910 fprintf(stderr
, "qemu: too many parallel ports\n");
4913 parallel_devices
[parallel_device_index
] = optarg
;
4914 parallel_device_index
++;
4916 case QEMU_OPTION_loadvm
:
4919 case QEMU_OPTION_full_screen
:
4923 case QEMU_OPTION_no_frame
:
4926 case QEMU_OPTION_alt_grab
:
4929 case QEMU_OPTION_no_quit
:
4933 case QEMU_OPTION_pidfile
:
4937 case QEMU_OPTION_win2k_hack
:
4938 win2k_install_hack
= 1;
4942 case QEMU_OPTION_no_kqemu
:
4945 case QEMU_OPTION_kernel_kqemu
:
4950 case QEMU_OPTION_enable_kvm
:
4957 case QEMU_OPTION_usb
:
4960 case QEMU_OPTION_usbdevice
:
4962 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
4963 fprintf(stderr
, "Too many USB devices\n");
4966 usb_devices
[usb_devices_index
] = optarg
;
4967 usb_devices_index
++;
4969 case QEMU_OPTION_smp
:
4970 smp_cpus
= atoi(optarg
);
4972 fprintf(stderr
, "Invalid number of CPUs\n");
4976 case QEMU_OPTION_vnc
:
4977 vnc_display
= optarg
;
4979 case QEMU_OPTION_no_acpi
:
4982 case QEMU_OPTION_no_reboot
:
4985 case QEMU_OPTION_no_shutdown
:
4988 case QEMU_OPTION_show_cursor
:
4991 case QEMU_OPTION_uuid
:
4992 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4993 fprintf(stderr
, "Fail to parse UUID string."
4994 " Wrong format.\n");
4998 case QEMU_OPTION_daemonize
:
5001 case QEMU_OPTION_option_rom
:
5002 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5003 fprintf(stderr
, "Too many option ROMs\n");
5006 option_rom
[nb_option_roms
] = optarg
;
5009 case QEMU_OPTION_semihosting
:
5010 semihosting_enabled
= 1;
5012 case QEMU_OPTION_name
:
5016 case QEMU_OPTION_prom_env
:
5017 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5018 fprintf(stderr
, "Too many prom variables\n");
5021 prom_envs
[nb_prom_envs
] = optarg
;
5026 case QEMU_OPTION_old_param
:
5030 case QEMU_OPTION_clock
:
5031 configure_alarms(optarg
);
5033 case QEMU_OPTION_startdate
:
5036 time_t rtc_start_date
;
5037 if (!strcmp(optarg
, "now")) {
5038 rtc_date_offset
= -1;
5040 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5048 } else if (sscanf(optarg
, "%d-%d-%d",
5051 &tm
.tm_mday
) == 3) {
5060 rtc_start_date
= mktimegm(&tm
);
5061 if (rtc_start_date
== -1) {
5063 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5064 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5067 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5071 case QEMU_OPTION_tb_size
:
5072 tb_size
= strtol(optarg
, NULL
, 0);
5076 case QEMU_OPTION_icount
:
5078 if (strcmp(optarg
, "auto") == 0) {
5079 icount_time_shift
= -1;
5081 icount_time_shift
= strtol(optarg
, NULL
, 0);
5084 case QEMU_OPTION_incoming
:
5091 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5092 if (kvm_allowed
&& kqemu_allowed
) {
5094 "You can not enable both KVM and kqemu at the same time\n");
5099 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5100 if (smp_cpus
> machine
->max_cpus
) {
5101 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5102 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5108 if (serial_device_index
== 0)
5109 serial_devices
[0] = "stdio";
5110 if (parallel_device_index
== 0)
5111 parallel_devices
[0] = "null";
5112 if (strncmp(monitor_device
, "vc", 2) == 0)
5113 monitor_device
= "stdio";
5120 if (pipe(fds
) == -1)
5131 len
= read(fds
[0], &status
, 1);
5132 if (len
== -1 && (errno
== EINTR
))
5137 else if (status
== 1) {
5138 fprintf(stderr
, "Could not acquire pidfile\n");
5155 signal(SIGTSTP
, SIG_IGN
);
5156 signal(SIGTTOU
, SIG_IGN
);
5157 signal(SIGTTIN
, SIG_IGN
);
5161 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5164 write(fds
[1], &status
, 1);
5166 fprintf(stderr
, "Could not acquire pid file\n");
5174 linux_boot
= (kernel_filename
!= NULL
);
5175 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5177 if (!linux_boot
&& net_boot
== 0 &&
5178 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5181 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5182 fprintf(stderr
, "-append only allowed with -kernel option\n");
5186 if (!linux_boot
&& initrd_filename
!= NULL
) {
5187 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5191 /* boot to floppy or the default cd if no hard disk defined yet */
5192 if (!boot_devices
[0]) {
5193 boot_devices
= "cad";
5195 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5198 if (init_timer_alarm() < 0) {
5199 fprintf(stderr
, "could not initialize alarm timer\n");
5202 if (use_icount
&& icount_time_shift
< 0) {
5204 /* 125MIPS seems a reasonable initial guess at the guest speed.
5205 It will be corrected fairly quickly anyway. */
5206 icount_time_shift
= 3;
5207 init_icount_adjust();
5214 /* init network clients */
5215 if (nb_net_clients
== 0) {
5216 /* if no clients, we use a default config */
5217 net_clients
[nb_net_clients
++] = "nic";
5219 net_clients
[nb_net_clients
++] = "user";
5223 for(i
= 0;i
< nb_net_clients
; i
++) {
5224 if (net_client_parse(net_clients
[i
]) < 0)
5230 /* XXX: this should be moved in the PC machine instantiation code */
5231 if (net_boot
!= 0) {
5233 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5234 const char *model
= nd_table
[i
].model
;
5236 if (net_boot
& (1 << i
)) {
5239 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5240 if (get_image_size(buf
) > 0) {
5241 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5242 fprintf(stderr
, "Too many option ROMs\n");
5245 option_rom
[nb_option_roms
] = strdup(buf
);
5252 fprintf(stderr
, "No valid PXE rom found for network device\n");
5258 /* init the bluetooth world */
5259 for (i
= 0; i
< nb_bt_opts
; i
++)
5260 if (bt_parse(bt_opts
[i
]))
5263 /* init the memory */
5264 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5266 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5268 if (ram_size
< phys_ram_size
) {
5269 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5270 machine
->name
, (unsigned long long) phys_ram_size
);
5274 phys_ram_size
= ram_size
;
5276 ram_size
= phys_ram_size
;
5279 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5281 phys_ram_size
+= ram_size
;
5284 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5285 if (!phys_ram_base
) {
5286 fprintf(stderr
, "Could not allocate physical memory\n");
5290 /* init the dynamic translator */
5291 cpu_exec_init_all(tb_size
* 1024 * 1024);
5295 /* we always create the cdrom drive, even if no disk is there */
5297 if (nb_drives_opt
< MAX_DRIVES
)
5298 drive_add(NULL
, CDROM_ALIAS
);
5300 /* we always create at least one floppy */
5302 if (nb_drives_opt
< MAX_DRIVES
)
5303 drive_add(NULL
, FD_ALIAS
, 0);
5305 /* we always create one sd slot, even if no card is in it */
5307 if (nb_drives_opt
< MAX_DRIVES
)
5308 drive_add(NULL
, SD_ALIAS
);
5310 /* open the virtual block devices */
5312 for(i
= 0; i
< nb_drives_opt
; i
++)
5313 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5316 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5317 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5320 memset(&display_state
, 0, sizeof(display_state
));
5323 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5326 /* nearly nothing to do */
5327 dumb_display_init(ds
);
5328 } else if (vnc_display
!= NULL
) {
5329 vnc_display_init(ds
);
5330 if (vnc_display_open(ds
, vnc_display
) < 0)
5333 #if defined(CONFIG_CURSES)
5335 curses_display_init(ds
, full_screen
);
5339 #if defined(CONFIG_SDL)
5340 sdl_display_init(ds
, full_screen
, no_frame
);
5341 #elif defined(CONFIG_COCOA)
5342 cocoa_display_init(ds
, full_screen
);
5344 dumb_display_init(ds
);
5349 /* must be after terminal init, SDL library changes signal handlers */
5353 /* Maintain compatibility with multiple stdio monitors */
5354 if (!strcmp(monitor_device
,"stdio")) {
5355 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5356 const char *devname
= serial_devices
[i
];
5357 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5358 monitor_device
= NULL
;
5360 } else if (devname
&& !strcmp(devname
,"stdio")) {
5361 monitor_device
= NULL
;
5362 serial_devices
[i
] = "mon:stdio";
5367 if (monitor_device
) {
5368 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5370 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5373 monitor_init(monitor_hd
, !nographic
);
5376 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5377 const char *devname
= serial_devices
[i
];
5378 if (devname
&& strcmp(devname
, "none")) {
5380 snprintf(label
, sizeof(label
), "serial%d", i
);
5381 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5382 if (!serial_hds
[i
]) {
5383 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5387 if (strstart(devname
, "vc", 0))
5388 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5392 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5393 const char *devname
= parallel_devices
[i
];
5394 if (devname
&& strcmp(devname
, "none")) {
5396 snprintf(label
, sizeof(label
), "parallel%d", i
);
5397 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5398 if (!parallel_hds
[i
]) {
5399 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5403 if (strstart(devname
, "vc", 0))
5404 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5408 if (kvm_enabled()) {
5411 ret
= kvm_init(smp_cpus
);
5413 fprintf(stderr
, "failed to initialize KVM\n");
5418 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5419 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5421 /* init USB devices */
5423 for(i
= 0; i
< usb_devices_index
; i
++) {
5424 if (usb_device_add(usb_devices
[i
]) < 0) {
5425 fprintf(stderr
, "Warning: could not add USB device %s\n",
5431 if (display_state
.dpy_refresh
) {
5432 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5433 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5436 #ifdef CONFIG_GDBSTUB
5438 /* XXX: use standard host:port notation and modify options
5440 if (gdbserver_start(gdbstub_port
) < 0) {
5441 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5452 autostart
= 0; /* fixme how to deal with -daemonize */
5453 qemu_start_incoming_migration(incoming
);
5457 /* XXX: simplify init */
5470 len
= write(fds
[1], &status
, 1);
5471 if (len
== -1 && (errno
== EINTR
))
5478 TFR(fd
= open("/dev/null", O_RDWR
));