4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
39 #include "cache-utils.h"
41 #include "audio/audio.h"
42 #include "migration.h"
55 #include <sys/times.h>
59 #include <sys/ioctl.h>
60 #include <sys/resource.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
64 #if defined(__NetBSD__)
65 #include <net/if_tap.h>
68 #include <linux/if_tun.h>
70 #include <arpa/inet.h>
73 #include <sys/select.h>
81 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82 #include <freebsd/stdlib.h>
87 #include <linux/rtc.h>
89 /* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91 /* #include <linux/hpet.h> */
94 #include <linux/ppdev.h>
95 #include <linux/parport.h>
99 #include <sys/ethernet.h>
100 #include <sys/sockio.h>
101 #include <netinet/arp.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_icmp.h> // must come after ip.h
106 #include <netinet/udp.h>
107 #include <netinet/tcp.h>
115 #include "qemu_socket.h"
117 #if defined(CONFIG_SLIRP)
118 #include "libslirp.h"
121 #if defined(__OpenBSD__)
125 #if defined(CONFIG_VDE)
126 #include <libvdeplug.h>
131 #include <sys/timeb.h>
132 #include <mmsystem.h>
133 #define getopt_long_only getopt_long
134 #define memalign(align, size) malloc(size)
141 #endif /* CONFIG_SDL */
145 #define main qemu_main
146 #endif /* CONFIG_COCOA */
150 #include "exec-all.h"
152 //#define DEBUG_UNUSED_IOPORT
153 //#define DEBUG_IOPORT
155 //#define DEBUG_SLIRP
159 # define LOG_IOPORT(...) do { \
160 if (loglevel & CPU_LOG_IOPORT) \
161 fprintf(logfile, ## __VA_ARGS__); \
164 # define LOG_IOPORT(...) do { } while (0)
168 #define DEFAULT_RAM_SIZE 144
170 #define DEFAULT_RAM_SIZE 128
173 /* Max number of USB devices that can be specified on the commandline. */
174 #define MAX_USB_CMDLINE 8
176 /* Max number of bluetooth switches on the commandline. */
177 #define MAX_BT_CMDLINE 10
179 /* XXX: use a two level table to limit memory usage */
180 #define MAX_IOPORTS 65536
182 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
183 const char *bios_name
= NULL
;
184 static void *ioport_opaque
[MAX_IOPORTS
];
185 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
186 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
187 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
188 to store the VM snapshots */
189 DriveInfo drives_table
[MAX_DRIVES
+1];
191 static int vga_ram_size
;
192 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
193 DisplayState display_state
;
197 const char* keyboard_layout
= NULL
;
198 int64_t ticks_per_sec
;
201 NICInfo nd_table
[MAX_NICS
];
203 static int rtc_utc
= 1;
204 static int rtc_date_offset
= -1; /* -1 means no change */
205 int cirrus_vga_enabled
= 1;
206 int std_vga_enabled
= 0;
207 int vmsvga_enabled
= 0;
209 int graphic_width
= 1024;
210 int graphic_height
= 768;
211 int graphic_depth
= 8;
213 int graphic_width
= 800;
214 int graphic_height
= 600;
215 int graphic_depth
= 15;
217 static int full_screen
= 0;
219 static int no_frame
= 0;
222 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
223 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
224 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
226 int win2k_install_hack
= 0;
231 const char *vnc_display
;
232 int acpi_enabled
= 1;
238 int graphic_rotate
= 0;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
248 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
249 unsigned int nb_prom_envs
= 0;
250 const char *prom_envs
[MAX_PROM_ENVS
];
252 static int nb_drives_opt
;
253 static struct drive_opt
{
256 } drives_opt
[MAX_DRIVES
];
258 static CPUState
*cur_cpu
;
259 static CPUState
*next_cpu
;
260 static int event_pending
= 1;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
270 uint8_t qemu_uuid
[16];
272 /***********************************************************/
273 /* x86 ISA bus support */
275 target_phys_addr_t isa_mem_base
= 0;
278 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
279 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
281 static uint32_t ioport_read(int index
, uint32_t address
)
283 static IOPortReadFunc
*default_func
[3] = {
284 default_ioport_readb
,
285 default_ioport_readw
,
288 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
290 func
= default_func
[index
];
291 return func(ioport_opaque
[address
], address
);
294 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
296 static IOPortWriteFunc
*default_func
[3] = {
297 default_ioport_writeb
,
298 default_ioport_writew
,
299 default_ioport_writel
301 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
303 func
= default_func
[index
];
304 func(ioport_opaque
[address
], address
, data
);
307 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
309 #ifdef DEBUG_UNUSED_IOPORT
310 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
315 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
317 #ifdef DEBUG_UNUSED_IOPORT
318 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
322 /* default is to make two byte accesses */
323 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
326 data
= ioport_read(0, address
);
327 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
328 data
|= ioport_read(0, address
) << 8;
332 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
334 ioport_write(0, address
, data
& 0xff);
335 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
336 ioport_write(0, address
, (data
>> 8) & 0xff);
339 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
341 #ifdef DEBUG_UNUSED_IOPORT
342 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
347 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
349 #ifdef DEBUG_UNUSED_IOPORT
350 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
354 /* size is the word size in byte */
355 int register_ioport_read(int start
, int length
, int size
,
356 IOPortReadFunc
*func
, void *opaque
)
362 } else if (size
== 2) {
364 } else if (size
== 4) {
367 hw_error("register_ioport_read: invalid size");
370 for(i
= start
; i
< start
+ length
; i
+= size
) {
371 ioport_read_table
[bsize
][i
] = func
;
372 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
373 hw_error("register_ioport_read: invalid opaque");
374 ioport_opaque
[i
] = opaque
;
379 /* size is the word size in byte */
380 int register_ioport_write(int start
, int length
, int size
,
381 IOPortWriteFunc
*func
, void *opaque
)
387 } else if (size
== 2) {
389 } else if (size
== 4) {
392 hw_error("register_ioport_write: invalid size");
395 for(i
= start
; i
< start
+ length
; i
+= size
) {
396 ioport_write_table
[bsize
][i
] = func
;
397 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
398 hw_error("register_ioport_write: invalid opaque");
399 ioport_opaque
[i
] = opaque
;
404 void isa_unassign_ioport(int start
, int length
)
408 for(i
= start
; i
< start
+ length
; i
++) {
409 ioport_read_table
[0][i
] = default_ioport_readb
;
410 ioport_read_table
[1][i
] = default_ioport_readw
;
411 ioport_read_table
[2][i
] = default_ioport_readl
;
413 ioport_write_table
[0][i
] = default_ioport_writeb
;
414 ioport_write_table
[1][i
] = default_ioport_writew
;
415 ioport_write_table
[2][i
] = default_ioport_writel
;
419 /***********************************************************/
421 void cpu_outb(CPUState
*env
, int addr
, int val
)
423 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
424 ioport_write(0, addr
, val
);
427 env
->last_io_time
= cpu_get_time_fast();
431 void cpu_outw(CPUState
*env
, int addr
, int val
)
433 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
434 ioport_write(1, addr
, val
);
437 env
->last_io_time
= cpu_get_time_fast();
441 void cpu_outl(CPUState
*env
, int addr
, int val
)
443 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
444 ioport_write(2, addr
, val
);
447 env
->last_io_time
= cpu_get_time_fast();
451 int cpu_inb(CPUState
*env
, int addr
)
454 val
= ioport_read(0, addr
);
455 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
458 env
->last_io_time
= cpu_get_time_fast();
463 int cpu_inw(CPUState
*env
, int addr
)
466 val
= ioport_read(1, addr
);
467 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
470 env
->last_io_time
= cpu_get_time_fast();
475 int cpu_inl(CPUState
*env
, int addr
)
478 val
= ioport_read(2, addr
);
479 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
482 env
->last_io_time
= cpu_get_time_fast();
487 /***********************************************************/
488 void hw_error(const char *fmt
, ...)
494 fprintf(stderr
, "qemu: hardware error: ");
495 vfprintf(stderr
, fmt
, ap
);
496 fprintf(stderr
, "\n");
497 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
498 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
500 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
502 cpu_dump_state(env
, stderr
, fprintf
, 0);
512 static QEMUBalloonEvent
*qemu_balloon_event
;
513 void *qemu_balloon_event_opaque
;
515 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
517 qemu_balloon_event
= func
;
518 qemu_balloon_event_opaque
= opaque
;
521 void qemu_balloon(ram_addr_t target
)
523 if (qemu_balloon_event
)
524 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
527 ram_addr_t
qemu_balloon_status(void)
529 if (qemu_balloon_event
)
530 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
534 /***********************************************************/
537 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
538 static void *qemu_put_kbd_event_opaque
;
539 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
540 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
542 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
544 qemu_put_kbd_event_opaque
= opaque
;
545 qemu_put_kbd_event
= func
;
548 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
549 void *opaque
, int absolute
,
552 QEMUPutMouseEntry
*s
, *cursor
;
554 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
558 s
->qemu_put_mouse_event
= func
;
559 s
->qemu_put_mouse_event_opaque
= opaque
;
560 s
->qemu_put_mouse_event_absolute
= absolute
;
561 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
564 if (!qemu_put_mouse_event_head
) {
565 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
569 cursor
= qemu_put_mouse_event_head
;
570 while (cursor
->next
!= NULL
)
571 cursor
= cursor
->next
;
574 qemu_put_mouse_event_current
= s
;
579 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
581 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
583 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
586 cursor
= qemu_put_mouse_event_head
;
587 while (cursor
!= NULL
&& cursor
!= entry
) {
589 cursor
= cursor
->next
;
592 if (cursor
== NULL
) // does not exist or list empty
594 else if (prev
== NULL
) { // entry is head
595 qemu_put_mouse_event_head
= cursor
->next
;
596 if (qemu_put_mouse_event_current
== entry
)
597 qemu_put_mouse_event_current
= cursor
->next
;
598 qemu_free(entry
->qemu_put_mouse_event_name
);
603 prev
->next
= entry
->next
;
605 if (qemu_put_mouse_event_current
== entry
)
606 qemu_put_mouse_event_current
= prev
;
608 qemu_free(entry
->qemu_put_mouse_event_name
);
612 void kbd_put_keycode(int keycode
)
614 if (qemu_put_kbd_event
) {
615 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
619 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
621 QEMUPutMouseEvent
*mouse_event
;
622 void *mouse_event_opaque
;
625 if (!qemu_put_mouse_event_current
) {
630 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
632 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
635 if (graphic_rotate
) {
636 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
639 width
= graphic_width
- 1;
640 mouse_event(mouse_event_opaque
,
641 width
- dy
, dx
, dz
, buttons_state
);
643 mouse_event(mouse_event_opaque
,
644 dx
, dy
, dz
, buttons_state
);
648 int kbd_mouse_is_absolute(void)
650 if (!qemu_put_mouse_event_current
)
653 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
656 void do_info_mice(void)
658 QEMUPutMouseEntry
*cursor
;
661 if (!qemu_put_mouse_event_head
) {
662 term_printf("No mouse devices connected\n");
666 term_printf("Mouse devices available:\n");
667 cursor
= qemu_put_mouse_event_head
;
668 while (cursor
!= NULL
) {
669 term_printf("%c Mouse #%d: %s\n",
670 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
671 index
, cursor
->qemu_put_mouse_event_name
);
673 cursor
= cursor
->next
;
677 void do_mouse_set(int index
)
679 QEMUPutMouseEntry
*cursor
;
682 if (!qemu_put_mouse_event_head
) {
683 term_printf("No mouse devices connected\n");
687 cursor
= qemu_put_mouse_event_head
;
688 while (cursor
!= NULL
&& index
!= i
) {
690 cursor
= cursor
->next
;
694 qemu_put_mouse_event_current
= cursor
;
696 term_printf("Mouse at given index not found\n");
699 /* compute with 96 bit intermediate result: (a*b)/c */
700 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
705 #ifdef WORDS_BIGENDIAN
715 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
716 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
719 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
723 /***********************************************************/
724 /* real time host monotonic timer */
726 #define QEMU_TIMER_BASE 1000000000LL
730 static int64_t clock_freq
;
732 static void init_get_clock(void)
736 ret
= QueryPerformanceFrequency(&freq
);
738 fprintf(stderr
, "Could not calibrate ticks\n");
741 clock_freq
= freq
.QuadPart
;
744 static int64_t get_clock(void)
747 QueryPerformanceCounter(&ti
);
748 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
753 static int use_rt_clock
;
755 static void init_get_clock(void)
758 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
761 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
768 static int64_t get_clock(void)
770 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
773 clock_gettime(CLOCK_MONOTONIC
, &ts
);
774 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
778 /* XXX: using gettimeofday leads to problems if the date
779 changes, so it should be avoided. */
781 gettimeofday(&tv
, NULL
);
782 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
787 /* Return the virtual CPU time, based on the instruction counter. */
788 static int64_t cpu_get_icount(void)
791 CPUState
*env
= cpu_single_env
;;
792 icount
= qemu_icount
;
795 fprintf(stderr
, "Bad clock read\n");
796 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
798 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
801 /***********************************************************/
802 /* guest cycle counter */
804 static int64_t cpu_ticks_prev
;
805 static int64_t cpu_ticks_offset
;
806 static int64_t cpu_clock_offset
;
807 static int cpu_ticks_enabled
;
809 /* return the host CPU cycle counter and handle stop/restart */
810 int64_t cpu_get_ticks(void)
813 return cpu_get_icount();
815 if (!cpu_ticks_enabled
) {
816 return cpu_ticks_offset
;
819 ticks
= cpu_get_real_ticks();
820 if (cpu_ticks_prev
> ticks
) {
821 /* Note: non increasing ticks may happen if the host uses
823 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
825 cpu_ticks_prev
= ticks
;
826 return ticks
+ cpu_ticks_offset
;
830 /* return the host CPU monotonic timer and handle stop/restart */
831 static int64_t cpu_get_clock(void)
834 if (!cpu_ticks_enabled
) {
835 return cpu_clock_offset
;
838 return ti
+ cpu_clock_offset
;
842 /* enable cpu_get_ticks() */
843 void cpu_enable_ticks(void)
845 if (!cpu_ticks_enabled
) {
846 cpu_ticks_offset
-= cpu_get_real_ticks();
847 cpu_clock_offset
-= get_clock();
848 cpu_ticks_enabled
= 1;
852 /* disable cpu_get_ticks() : the clock is stopped. You must not call
853 cpu_get_ticks() after that. */
854 void cpu_disable_ticks(void)
856 if (cpu_ticks_enabled
) {
857 cpu_ticks_offset
= cpu_get_ticks();
858 cpu_clock_offset
= cpu_get_clock();
859 cpu_ticks_enabled
= 0;
863 /***********************************************************/
866 #define QEMU_TIMER_REALTIME 0
867 #define QEMU_TIMER_VIRTUAL 1
871 /* XXX: add frequency */
879 struct QEMUTimer
*next
;
882 struct qemu_alarm_timer
{
886 int (*start
)(struct qemu_alarm_timer
*t
);
887 void (*stop
)(struct qemu_alarm_timer
*t
);
888 void (*rearm
)(struct qemu_alarm_timer
*t
);
892 #define ALARM_FLAG_DYNTICKS 0x1
893 #define ALARM_FLAG_EXPIRED 0x2
895 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
897 return t
->flags
& ALARM_FLAG_DYNTICKS
;
900 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
902 if (!alarm_has_dynticks(t
))
908 /* TODO: MIN_TIMER_REARM_US should be optimized */
909 #define MIN_TIMER_REARM_US 250
911 static struct qemu_alarm_timer
*alarm_timer
;
913 static int alarm_timer_rfd
, alarm_timer_wfd
;
918 struct qemu_alarm_win32
{
922 } alarm_win32_data
= {0, NULL
, -1};
924 static int win32_start_timer(struct qemu_alarm_timer
*t
);
925 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
926 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
930 static int unix_start_timer(struct qemu_alarm_timer
*t
);
931 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
935 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
936 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
937 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
939 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
940 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
942 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
943 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
945 #endif /* __linux__ */
949 /* Correlation between real and virtual time is always going to be
950 fairly approximate, so ignore small variation.
951 When the guest is idle real and virtual time will be aligned in
953 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
955 static void icount_adjust(void)
960 static int64_t last_delta
;
961 /* If the VM is not running, then do nothing. */
965 cur_time
= cpu_get_clock();
966 cur_icount
= qemu_get_clock(vm_clock
);
967 delta
= cur_icount
- cur_time
;
968 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
970 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
971 && icount_time_shift
> 0) {
972 /* The guest is getting too far ahead. Slow time down. */
976 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
977 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
978 /* The guest is getting too far behind. Speed time up. */
982 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
985 static void icount_adjust_rt(void * opaque
)
987 qemu_mod_timer(icount_rt_timer
,
988 qemu_get_clock(rt_clock
) + 1000);
992 static void icount_adjust_vm(void * opaque
)
994 qemu_mod_timer(icount_vm_timer
,
995 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
999 static void init_icount_adjust(void)
1001 /* Have both realtime and virtual time triggers for speed adjustment.
1002 The realtime trigger catches emulated time passing too slowly,
1003 the virtual time trigger catches emulated time passing too fast.
1004 Realtime triggers occur even when idle, so use them less frequently
1005 than VM triggers. */
1006 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1007 qemu_mod_timer(icount_rt_timer
,
1008 qemu_get_clock(rt_clock
) + 1000);
1009 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1010 qemu_mod_timer(icount_vm_timer
,
1011 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1014 static struct qemu_alarm_timer alarm_timers
[] = {
1017 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1018 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1019 /* HPET - if available - is preferred */
1020 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1021 /* ...otherwise try RTC */
1022 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1024 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1026 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1027 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1028 {"win32", 0, win32_start_timer
,
1029 win32_stop_timer
, NULL
, &alarm_win32_data
},
1034 static void show_available_alarms(void)
1038 printf("Available alarm timers, in order of precedence:\n");
1039 for (i
= 0; alarm_timers
[i
].name
; i
++)
1040 printf("%s\n", alarm_timers
[i
].name
);
1043 static void configure_alarms(char const *opt
)
1047 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1050 struct qemu_alarm_timer tmp
;
1052 if (!strcmp(opt
, "?")) {
1053 show_available_alarms();
1059 /* Reorder the array */
1060 name
= strtok(arg
, ",");
1062 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1063 if (!strcmp(alarm_timers
[i
].name
, name
))
1068 fprintf(stderr
, "Unknown clock %s\n", name
);
1077 tmp
= alarm_timers
[i
];
1078 alarm_timers
[i
] = alarm_timers
[cur
];
1079 alarm_timers
[cur
] = tmp
;
1083 name
= strtok(NULL
, ",");
1089 /* Disable remaining timers */
1090 for (i
= cur
; i
< count
; i
++)
1091 alarm_timers
[i
].name
= NULL
;
1093 show_available_alarms();
1098 QEMUClock
*rt_clock
;
1099 QEMUClock
*vm_clock
;
1101 static QEMUTimer
*active_timers
[2];
1103 static QEMUClock
*qemu_new_clock(int type
)
1106 clock
= qemu_mallocz(sizeof(QEMUClock
));
1113 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1117 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1120 ts
->opaque
= opaque
;
1124 void qemu_free_timer(QEMUTimer
*ts
)
1129 /* stop a timer, but do not dealloc it */
1130 void qemu_del_timer(QEMUTimer
*ts
)
1134 /* NOTE: this code must be signal safe because
1135 qemu_timer_expired() can be called from a signal. */
1136 pt
= &active_timers
[ts
->clock
->type
];
1149 /* modify the current timer so that it will be fired when current_time
1150 >= expire_time. The corresponding callback will be called. */
1151 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1157 /* add the timer in the sorted list */
1158 /* NOTE: this code must be signal safe because
1159 qemu_timer_expired() can be called from a signal. */
1160 pt
= &active_timers
[ts
->clock
->type
];
1165 if (t
->expire_time
> expire_time
)
1169 ts
->expire_time
= expire_time
;
1173 /* Rearm if necessary */
1174 if (pt
== &active_timers
[ts
->clock
->type
]) {
1175 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1176 qemu_rearm_alarm_timer(alarm_timer
);
1178 /* Interrupt execution to force deadline recalculation. */
1179 if (use_icount
&& cpu_single_env
) {
1180 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1185 int qemu_timer_pending(QEMUTimer
*ts
)
1188 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1195 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1199 return (timer_head
->expire_time
<= current_time
);
1202 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1208 if (!ts
|| ts
->expire_time
> current_time
)
1210 /* remove timer from the list before calling the callback */
1211 *ptimer_head
= ts
->next
;
1214 /* run the callback (the timer list can be modified) */
1219 int64_t qemu_get_clock(QEMUClock
*clock
)
1221 switch(clock
->type
) {
1222 case QEMU_TIMER_REALTIME
:
1223 return get_clock() / 1000000;
1225 case QEMU_TIMER_VIRTUAL
:
1227 return cpu_get_icount();
1229 return cpu_get_clock();
1234 static void init_timers(void)
1237 ticks_per_sec
= QEMU_TIMER_BASE
;
1238 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1239 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1243 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1245 uint64_t expire_time
;
1247 if (qemu_timer_pending(ts
)) {
1248 expire_time
= ts
->expire_time
;
1252 qemu_put_be64(f
, expire_time
);
1255 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1257 uint64_t expire_time
;
1259 expire_time
= qemu_get_be64(f
);
1260 if (expire_time
!= -1) {
1261 qemu_mod_timer(ts
, expire_time
);
1267 static void timer_save(QEMUFile
*f
, void *opaque
)
1269 if (cpu_ticks_enabled
) {
1270 hw_error("cannot save state if virtual timers are running");
1272 qemu_put_be64(f
, cpu_ticks_offset
);
1273 qemu_put_be64(f
, ticks_per_sec
);
1274 qemu_put_be64(f
, cpu_clock_offset
);
1277 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1279 if (version_id
!= 1 && version_id
!= 2)
1281 if (cpu_ticks_enabled
) {
1284 cpu_ticks_offset
=qemu_get_be64(f
);
1285 ticks_per_sec
=qemu_get_be64(f
);
1286 if (version_id
== 2) {
1287 cpu_clock_offset
=qemu_get_be64(f
);
1293 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1294 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1296 static void host_alarm_handler(int host_signum
)
1300 #define DISP_FREQ 1000
1302 static int64_t delta_min
= INT64_MAX
;
1303 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1305 ti
= qemu_get_clock(vm_clock
);
1306 if (last_clock
!= 0) {
1307 delta
= ti
- last_clock
;
1308 if (delta
< delta_min
)
1310 if (delta
> delta_max
)
1313 if (++count
== DISP_FREQ
) {
1314 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1315 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1316 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1317 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1318 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1320 delta_min
= INT64_MAX
;
1328 if (alarm_has_dynticks(alarm_timer
) ||
1330 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1331 qemu_get_clock(vm_clock
))) ||
1332 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1333 qemu_get_clock(rt_clock
))) {
1334 CPUState
*env
= next_cpu
;
1337 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1338 SetEvent(data
->host_alarm
);
1340 static const char byte
= 0;
1341 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1343 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1346 /* stop the currently executing cpu because a timer occured */
1347 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1349 if (env
->kqemu_enabled
) {
1350 kqemu_cpu_interrupt(env
);
1358 static int64_t qemu_next_deadline(void)
1362 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1363 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1364 qemu_get_clock(vm_clock
);
1366 /* To avoid problems with overflow limit this to 2^32. */
1376 #if defined(__linux__) || defined(_WIN32)
1377 static uint64_t qemu_next_deadline_dyntick(void)
1385 delta
= (qemu_next_deadline() + 999) / 1000;
1387 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1388 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1389 qemu_get_clock(rt_clock
))*1000;
1390 if (rtdelta
< delta
)
1394 if (delta
< MIN_TIMER_REARM_US
)
1395 delta
= MIN_TIMER_REARM_US
;
1403 /* Sets a specific flag */
1404 static int fcntl_setfl(int fd
, int flag
)
1408 flags
= fcntl(fd
, F_GETFL
);
1412 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1418 #if defined(__linux__)
1420 #define RTC_FREQ 1024
1422 static void enable_sigio_timer(int fd
)
1424 struct sigaction act
;
1427 sigfillset(&act
.sa_mask
);
1429 act
.sa_handler
= host_alarm_handler
;
1431 sigaction(SIGIO
, &act
, NULL
);
1432 fcntl_setfl(fd
, O_ASYNC
);
1433 fcntl(fd
, F_SETOWN
, getpid());
1436 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1438 struct hpet_info info
;
1441 fd
= open("/dev/hpet", O_RDONLY
);
1446 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1448 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1449 "error, but for better emulation accuracy type:\n"
1450 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1454 /* Check capabilities */
1455 r
= ioctl(fd
, HPET_INFO
, &info
);
1459 /* Enable periodic mode */
1460 r
= ioctl(fd
, HPET_EPI
, 0);
1461 if (info
.hi_flags
&& (r
< 0))
1464 /* Enable interrupt */
1465 r
= ioctl(fd
, HPET_IE_ON
, 0);
1469 enable_sigio_timer(fd
);
1470 t
->priv
= (void *)(long)fd
;
1478 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1480 int fd
= (long)t
->priv
;
1485 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1488 unsigned long current_rtc_freq
= 0;
1490 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1493 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1494 if (current_rtc_freq
!= RTC_FREQ
&&
1495 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1496 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1497 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1498 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1501 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1507 enable_sigio_timer(rtc_fd
);
1509 t
->priv
= (void *)(long)rtc_fd
;
1514 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1516 int rtc_fd
= (long)t
->priv
;
1521 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1525 struct sigaction act
;
1527 sigfillset(&act
.sa_mask
);
1529 act
.sa_handler
= host_alarm_handler
;
1531 sigaction(SIGALRM
, &act
, NULL
);
1533 ev
.sigev_value
.sival_int
= 0;
1534 ev
.sigev_notify
= SIGEV_SIGNAL
;
1535 ev
.sigev_signo
= SIGALRM
;
1537 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1538 perror("timer_create");
1540 /* disable dynticks */
1541 fprintf(stderr
, "Dynamic Ticks disabled\n");
1546 t
->priv
= (void *)(long)host_timer
;
1551 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1553 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1555 timer_delete(host_timer
);
1558 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1560 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1561 struct itimerspec timeout
;
1562 int64_t nearest_delta_us
= INT64_MAX
;
1565 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1566 !active_timers
[QEMU_TIMER_VIRTUAL
])
1569 nearest_delta_us
= qemu_next_deadline_dyntick();
1571 /* check whether a timer is already running */
1572 if (timer_gettime(host_timer
, &timeout
)) {
1574 fprintf(stderr
, "Internal timer error: aborting\n");
1577 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1578 if (current_us
&& current_us
<= nearest_delta_us
)
1581 timeout
.it_interval
.tv_sec
= 0;
1582 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1583 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1584 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1585 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1587 fprintf(stderr
, "Internal timer error: aborting\n");
1592 #endif /* defined(__linux__) */
1594 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1596 struct sigaction act
;
1597 struct itimerval itv
;
1601 sigfillset(&act
.sa_mask
);
1603 act
.sa_handler
= host_alarm_handler
;
1605 sigaction(SIGALRM
, &act
, NULL
);
1607 itv
.it_interval
.tv_sec
= 0;
1608 /* for i386 kernel 2.6 to get 1 ms */
1609 itv
.it_interval
.tv_usec
= 999;
1610 itv
.it_value
.tv_sec
= 0;
1611 itv
.it_value
.tv_usec
= 10 * 1000;
1613 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1620 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1622 struct itimerval itv
;
1624 memset(&itv
, 0, sizeof(itv
));
1625 setitimer(ITIMER_REAL
, &itv
, NULL
);
1628 #endif /* !defined(_WIN32) */
1630 static void try_to_rearm_timer(void *opaque
)
1632 struct qemu_alarm_timer
*t
= opaque
;
1636 /* Drain the notify pipe */
1639 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1640 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1643 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1644 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1645 qemu_rearm_alarm_timer(alarm_timer
);
1651 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1654 struct qemu_alarm_win32
*data
= t
->priv
;
1657 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1658 if (!data
->host_alarm
) {
1659 perror("Failed CreateEvent");
1663 memset(&tc
, 0, sizeof(tc
));
1664 timeGetDevCaps(&tc
, sizeof(tc
));
1666 if (data
->period
< tc
.wPeriodMin
)
1667 data
->period
= tc
.wPeriodMin
;
1669 timeBeginPeriod(data
->period
);
1671 flags
= TIME_CALLBACK_FUNCTION
;
1672 if (alarm_has_dynticks(t
))
1673 flags
|= TIME_ONESHOT
;
1675 flags
|= TIME_PERIODIC
;
1677 data
->timerId
= timeSetEvent(1, // interval (ms)
1678 data
->period
, // resolution
1679 host_alarm_handler
, // function
1680 (DWORD
)t
, // parameter
1683 if (!data
->timerId
) {
1684 perror("Failed to initialize win32 alarm timer");
1686 timeEndPeriod(data
->period
);
1687 CloseHandle(data
->host_alarm
);
1691 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1696 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1698 struct qemu_alarm_win32
*data
= t
->priv
;
1700 timeKillEvent(data
->timerId
);
1701 timeEndPeriod(data
->period
);
1703 CloseHandle(data
->host_alarm
);
1706 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1708 struct qemu_alarm_win32
*data
= t
->priv
;
1709 uint64_t nearest_delta_us
;
1711 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1712 !active_timers
[QEMU_TIMER_VIRTUAL
])
1715 nearest_delta_us
= qemu_next_deadline_dyntick();
1716 nearest_delta_us
/= 1000;
1718 timeKillEvent(data
->timerId
);
1720 data
->timerId
= timeSetEvent(1,
1724 TIME_ONESHOT
| TIME_PERIODIC
);
1726 if (!data
->timerId
) {
1727 perror("Failed to re-arm win32 alarm timer");
1729 timeEndPeriod(data
->period
);
1730 CloseHandle(data
->host_alarm
);
1737 static int init_timer_alarm(void)
1739 struct qemu_alarm_timer
*t
= NULL
;
1749 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1753 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1757 alarm_timer_rfd
= fds
[0];
1758 alarm_timer_wfd
= fds
[1];
1761 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1762 t
= &alarm_timers
[i
];
1775 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1776 try_to_rearm_timer
, NULL
, t
);
1791 static void quit_timers(void)
1793 alarm_timer
->stop(alarm_timer
);
1797 /***********************************************************/
1798 /* host time/date access */
1799 void qemu_get_timedate(struct tm
*tm
, int offset
)
1806 if (rtc_date_offset
== -1) {
1810 ret
= localtime(&ti
);
1812 ti
-= rtc_date_offset
;
1816 memcpy(tm
, ret
, sizeof(struct tm
));
1819 int qemu_timedate_diff(struct tm
*tm
)
1823 if (rtc_date_offset
== -1)
1825 seconds
= mktimegm(tm
);
1827 seconds
= mktime(tm
);
1829 seconds
= mktimegm(tm
) + rtc_date_offset
;
1831 return seconds
- time(NULL
);
1835 static void socket_cleanup(void)
1840 static int socket_init(void)
1845 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1847 err
= WSAGetLastError();
1848 fprintf(stderr
, "WSAStartup: %d\n", err
);
1851 atexit(socket_cleanup
);
1856 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1861 while (*p
!= '\0' && *p
!= '=') {
1862 if (q
&& (q
- buf
) < buf_size
- 1)
1872 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1877 while (*p
!= '\0') {
1879 if (*(p
+ 1) != ',')
1883 if (q
&& (q
- buf
) < buf_size
- 1)
1893 int get_param_value(char *buf
, int buf_size
,
1894 const char *tag
, const char *str
)
1901 p
= get_opt_name(option
, sizeof(option
), p
);
1905 if (!strcmp(tag
, option
)) {
1906 (void)get_opt_value(buf
, buf_size
, p
);
1909 p
= get_opt_value(NULL
, 0, p
);
1918 int check_params(char *buf
, int buf_size
,
1919 const char * const *params
, const char *str
)
1926 p
= get_opt_name(buf
, buf_size
, p
);
1930 for(i
= 0; params
[i
] != NULL
; i
++)
1931 if (!strcmp(params
[i
], buf
))
1933 if (params
[i
] == NULL
)
1935 p
= get_opt_value(NULL
, 0, p
);
1943 /***********************************************************/
1944 /* Bluetooth support */
1947 static struct HCIInfo
*hci_table
[MAX_NICS
];
1949 static struct bt_vlan_s
{
1950 struct bt_scatternet_s net
;
1952 struct bt_vlan_s
*next
;
1955 /* find or alloc a new bluetooth "VLAN" */
1956 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1958 struct bt_vlan_s
**pvlan
, *vlan
;
1959 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1963 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1965 pvlan
= &first_bt_vlan
;
1966 while (*pvlan
!= NULL
)
1967 pvlan
= &(*pvlan
)->next
;
1972 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1976 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1981 static struct HCIInfo null_hci
= {
1982 .cmd_send
= null_hci_send
,
1983 .sco_send
= null_hci_send
,
1984 .acl_send
= null_hci_send
,
1985 .bdaddr_set
= null_hci_addr_set
,
1988 struct HCIInfo
*qemu_next_hci(void)
1990 if (cur_hci
== nb_hcis
)
1993 return hci_table
[cur_hci
++];
1996 static struct HCIInfo
*hci_init(const char *str
)
1999 struct bt_scatternet_s
*vlan
= 0;
2001 if (!strcmp(str
, "null"))
2004 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2006 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2007 else if (!strncmp(str
, "hci", 3)) {
2010 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2011 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2016 vlan
= qemu_find_bt_vlan(0);
2018 return bt_new_hci(vlan
);
2021 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2026 static int bt_hci_parse(const char *str
)
2028 struct HCIInfo
*hci
;
2031 if (nb_hcis
>= MAX_NICS
) {
2032 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2036 hci
= hci_init(str
);
2045 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2046 hci
->bdaddr_set(hci
, bdaddr
.b
);
2048 hci_table
[nb_hcis
++] = hci
;
2053 static void bt_vhci_add(int vlan_id
)
2055 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2058 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2059 "an empty scatternet %i\n", vlan_id
);
2061 bt_vhci_init(bt_new_hci(vlan
));
2064 static struct bt_device_s
*bt_device_add(const char *opt
)
2066 struct bt_scatternet_s
*vlan
;
2068 char *endp
= strstr(opt
, ",vlan=");
2069 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2072 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2075 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2077 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2082 vlan
= qemu_find_bt_vlan(vlan_id
);
2085 fprintf(stderr
, "qemu: warning: adding a slave device to "
2086 "an empty scatternet %i\n", vlan_id
);
2088 if (!strcmp(devname
, "keyboard"))
2089 return bt_keyboard_init(vlan
);
2091 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2095 static int bt_parse(const char *opt
)
2097 const char *endp
, *p
;
2100 if (strstart(opt
, "hci", &endp
)) {
2101 if (!*endp
|| *endp
== ',') {
2103 if (!strstart(endp
, ",vlan=", 0))
2106 return bt_hci_parse(opt
);
2108 } else if (strstart(opt
, "vhci", &endp
)) {
2109 if (!*endp
|| *endp
== ',') {
2111 if (strstart(endp
, ",vlan=", &p
)) {
2112 vlan
= strtol(p
, (char **) &endp
, 0);
2114 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2118 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2127 } else if (strstart(opt
, "device:", &endp
))
2128 return !bt_device_add(endp
);
2130 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2134 /***********************************************************/
2135 /* QEMU Block devices */
2137 #define HD_ALIAS "index=%d,media=disk"
2139 #define CDROM_ALIAS "index=1,media=cdrom"
2141 #define CDROM_ALIAS "index=2,media=cdrom"
2143 #define FD_ALIAS "index=%d,if=floppy"
2144 #define PFLASH_ALIAS "if=pflash"
2145 #define MTD_ALIAS "if=mtd"
2146 #define SD_ALIAS "index=0,if=sd"
2148 static int drive_add(const char *file
, const char *fmt
, ...)
2152 if (nb_drives_opt
>= MAX_DRIVES
) {
2153 fprintf(stderr
, "qemu: too many drives\n");
2157 drives_opt
[nb_drives_opt
].file
= file
;
2159 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2160 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2163 return nb_drives_opt
++;
2166 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2170 /* seek interface, bus and unit */
2172 for (index
= 0; index
< nb_drives
; index
++)
2173 if (drives_table
[index
].type
== type
&&
2174 drives_table
[index
].bus
== bus
&&
2175 drives_table
[index
].unit
== unit
)
2181 int drive_get_max_bus(BlockInterfaceType type
)
2187 for (index
= 0; index
< nb_drives
; index
++) {
2188 if(drives_table
[index
].type
== type
&&
2189 drives_table
[index
].bus
> max_bus
)
2190 max_bus
= drives_table
[index
].bus
;
2195 const char *drive_get_serial(BlockDriverState
*bdrv
)
2199 for (index
= 0; index
< nb_drives
; index
++)
2200 if (drives_table
[index
].bdrv
== bdrv
)
2201 return drives_table
[index
].serial
;
2206 static void bdrv_format_print(void *opaque
, const char *name
)
2208 fprintf(stderr
, " %s", name
);
2211 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2212 QEMUMachine
*machine
)
2218 const char *mediastr
= "";
2219 BlockInterfaceType type
;
2220 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2221 int bus_id
, unit_id
;
2222 int cyls
, heads
, secs
, translation
;
2223 BlockDriverState
*bdrv
;
2224 BlockDriver
*drv
= NULL
;
2229 char *str
= arg
->opt
;
2230 static const char * const params
[] = { "bus", "unit", "if", "index",
2231 "cyls", "heads", "secs", "trans",
2232 "media", "snapshot", "file",
2233 "cache", "format", "serial", NULL
};
2235 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2236 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2242 cyls
= heads
= secs
= 0;
2245 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2249 if (machine
->use_scsi
) {
2251 max_devs
= MAX_SCSI_DEVS
;
2252 pstrcpy(devname
, sizeof(devname
), "scsi");
2255 max_devs
= MAX_IDE_DEVS
;
2256 pstrcpy(devname
, sizeof(devname
), "ide");
2260 /* extract parameters */
2262 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2263 bus_id
= strtol(buf
, NULL
, 0);
2265 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2270 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2271 unit_id
= strtol(buf
, NULL
, 0);
2273 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2278 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2279 pstrcpy(devname
, sizeof(devname
), buf
);
2280 if (!strcmp(buf
, "ide")) {
2282 max_devs
= MAX_IDE_DEVS
;
2283 } else if (!strcmp(buf
, "scsi")) {
2285 max_devs
= MAX_SCSI_DEVS
;
2286 } else if (!strcmp(buf
, "floppy")) {
2289 } else if (!strcmp(buf
, "pflash")) {
2292 } else if (!strcmp(buf
, "mtd")) {
2295 } else if (!strcmp(buf
, "sd")) {
2298 } else if (!strcmp(buf
, "virtio")) {
2302 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2307 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2308 index
= strtol(buf
, NULL
, 0);
2310 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2315 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2316 cyls
= strtol(buf
, NULL
, 0);
2319 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2320 heads
= strtol(buf
, NULL
, 0);
2323 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2324 secs
= strtol(buf
, NULL
, 0);
2327 if (cyls
|| heads
|| secs
) {
2328 if (cyls
< 1 || cyls
> 16383) {
2329 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2332 if (heads
< 1 || heads
> 16) {
2333 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2336 if (secs
< 1 || secs
> 63) {
2337 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2342 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2345 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2349 if (!strcmp(buf
, "none"))
2350 translation
= BIOS_ATA_TRANSLATION_NONE
;
2351 else if (!strcmp(buf
, "lba"))
2352 translation
= BIOS_ATA_TRANSLATION_LBA
;
2353 else if (!strcmp(buf
, "auto"))
2354 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2356 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2361 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2362 if (!strcmp(buf
, "disk")) {
2364 } else if (!strcmp(buf
, "cdrom")) {
2365 if (cyls
|| secs
|| heads
) {
2367 "qemu: '%s' invalid physical CHS format\n", str
);
2370 media
= MEDIA_CDROM
;
2372 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2377 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2378 if (!strcmp(buf
, "on"))
2380 else if (!strcmp(buf
, "off"))
2383 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2388 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2389 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2391 else if (!strcmp(buf
, "writethrough"))
2393 else if (!strcmp(buf
, "writeback"))
2396 fprintf(stderr
, "qemu: invalid cache option\n");
2401 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2402 if (strcmp(buf
, "?") == 0) {
2403 fprintf(stderr
, "qemu: Supported formats:");
2404 bdrv_iterate_format(bdrv_format_print
, NULL
);
2405 fprintf(stderr
, "\n");
2408 drv
= bdrv_find_format(buf
);
2410 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2415 if (arg
->file
== NULL
)
2416 get_param_value(file
, sizeof(file
), "file", str
);
2418 pstrcpy(file
, sizeof(file
), arg
->file
);
2420 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2421 memset(serial
, 0, sizeof(serial
));
2423 /* compute bus and unit according index */
2426 if (bus_id
!= 0 || unit_id
!= -1) {
2428 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2436 unit_id
= index
% max_devs
;
2437 bus_id
= index
/ max_devs
;
2441 /* if user doesn't specify a unit_id,
2442 * try to find the first free
2445 if (unit_id
== -1) {
2447 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2449 if (max_devs
&& unit_id
>= max_devs
) {
2450 unit_id
-= max_devs
;
2458 if (max_devs
&& unit_id
>= max_devs
) {
2459 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2460 str
, unit_id
, max_devs
- 1);
2465 * ignore multiple definitions
2468 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2473 if (type
== IF_IDE
|| type
== IF_SCSI
)
2474 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2476 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2477 devname
, bus_id
, mediastr
, unit_id
);
2479 snprintf(buf
, sizeof(buf
), "%s%s%i",
2480 devname
, mediastr
, unit_id
);
2481 bdrv
= bdrv_new(buf
);
2482 drives_table
[nb_drives
].bdrv
= bdrv
;
2483 drives_table
[nb_drives
].type
= type
;
2484 drives_table
[nb_drives
].bus
= bus_id
;
2485 drives_table
[nb_drives
].unit
= unit_id
;
2486 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2495 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2496 bdrv_set_translation_hint(bdrv
, translation
);
2500 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2505 /* FIXME: This isn't really a floppy, but it's a reasonable
2508 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2519 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2520 cache
= 2; /* always use write-back with snapshot */
2522 if (cache
== 0) /* no caching */
2523 bdrv_flags
|= BDRV_O_NOCACHE
;
2524 else if (cache
== 2) /* write-back */
2525 bdrv_flags
|= BDRV_O_CACHE_WB
;
2526 else if (cache
== 3) /* not specified */
2527 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2528 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2529 fprintf(stderr
, "qemu: could not open disk image %s\n",
2536 /***********************************************************/
2539 static USBPort
*used_usb_ports
;
2540 static USBPort
*free_usb_ports
;
2542 /* ??? Maybe change this to register a hub to keep track of the topology. */
2543 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2544 usb_attachfn attach
)
2546 port
->opaque
= opaque
;
2547 port
->index
= index
;
2548 port
->attach
= attach
;
2549 port
->next
= free_usb_ports
;
2550 free_usb_ports
= port
;
2553 int usb_device_add_dev(USBDevice
*dev
)
2557 /* Find a USB port to add the device to. */
2558 port
= free_usb_ports
;
2562 /* Create a new hub and chain it on. */
2563 free_usb_ports
= NULL
;
2564 port
->next
= used_usb_ports
;
2565 used_usb_ports
= port
;
2567 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2568 usb_attach(port
, hub
);
2569 port
= free_usb_ports
;
2572 free_usb_ports
= port
->next
;
2573 port
->next
= used_usb_ports
;
2574 used_usb_ports
= port
;
2575 usb_attach(port
, dev
);
2579 static int usb_device_add(const char *devname
)
2584 if (!free_usb_ports
)
2587 if (strstart(devname
, "host:", &p
)) {
2588 dev
= usb_host_device_open(p
);
2589 } else if (!strcmp(devname
, "mouse")) {
2590 dev
= usb_mouse_init();
2591 } else if (!strcmp(devname
, "tablet")) {
2592 dev
= usb_tablet_init();
2593 } else if (!strcmp(devname
, "keyboard")) {
2594 dev
= usb_keyboard_init();
2595 } else if (strstart(devname
, "disk:", &p
)) {
2596 dev
= usb_msd_init(p
);
2597 } else if (!strcmp(devname
, "wacom-tablet")) {
2598 dev
= usb_wacom_init();
2599 } else if (strstart(devname
, "serial:", &p
)) {
2600 dev
= usb_serial_init(p
);
2601 #ifdef CONFIG_BRLAPI
2602 } else if (!strcmp(devname
, "braille")) {
2603 dev
= usb_baum_init();
2605 } else if (strstart(devname
, "net:", &p
)) {
2608 if (net_client_init("nic", p
) < 0)
2610 nd_table
[nic
].model
= "usb";
2611 dev
= usb_net_init(&nd_table
[nic
]);
2612 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2613 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2614 bt_new_hci(qemu_find_bt_vlan(0)));
2621 return usb_device_add_dev(dev
);
2624 int usb_device_del_addr(int bus_num
, int addr
)
2630 if (!used_usb_ports
)
2636 lastp
= &used_usb_ports
;
2637 port
= used_usb_ports
;
2638 while (port
&& port
->dev
->addr
!= addr
) {
2639 lastp
= &port
->next
;
2647 *lastp
= port
->next
;
2648 usb_attach(port
, NULL
);
2649 dev
->handle_destroy(dev
);
2650 port
->next
= free_usb_ports
;
2651 free_usb_ports
= port
;
2655 static int usb_device_del(const char *devname
)
2660 if (strstart(devname
, "host:", &p
))
2661 return usb_host_device_close(p
);
2663 if (!used_usb_ports
)
2666 p
= strchr(devname
, '.');
2669 bus_num
= strtoul(devname
, NULL
, 0);
2670 addr
= strtoul(p
+ 1, NULL
, 0);
2672 return usb_device_del_addr(bus_num
, addr
);
2675 void do_usb_add(const char *devname
)
2677 usb_device_add(devname
);
2680 void do_usb_del(const char *devname
)
2682 usb_device_del(devname
);
2689 const char *speed_str
;
2692 term_printf("USB support not enabled\n");
2696 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2700 switch(dev
->speed
) {
2704 case USB_SPEED_FULL
:
2707 case USB_SPEED_HIGH
:
2714 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2715 0, dev
->addr
, speed_str
, dev
->devname
);
2719 /***********************************************************/
2720 /* PCMCIA/Cardbus */
2722 static struct pcmcia_socket_entry_s
{
2723 struct pcmcia_socket_s
*socket
;
2724 struct pcmcia_socket_entry_s
*next
;
2725 } *pcmcia_sockets
= 0;
2727 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2729 struct pcmcia_socket_entry_s
*entry
;
2731 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2732 entry
->socket
= socket
;
2733 entry
->next
= pcmcia_sockets
;
2734 pcmcia_sockets
= entry
;
2737 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2739 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2741 ptr
= &pcmcia_sockets
;
2742 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2743 if (entry
->socket
== socket
) {
2749 void pcmcia_info(void)
2751 struct pcmcia_socket_entry_s
*iter
;
2752 if (!pcmcia_sockets
)
2753 term_printf("No PCMCIA sockets\n");
2755 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2756 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2757 iter
->socket
->attached
? iter
->socket
->card_string
:
2761 /***********************************************************/
2764 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2768 static void dumb_resize(DisplayState
*ds
)
2772 static void dumb_display_init(DisplayState
*ds
)
2774 DisplayChangeListener
*dcl
= qemu_mallocz(sizeof(DisplayChangeListener
));
2777 dcl
->dpy_update
= dumb_update
;
2778 dcl
->dpy_resize
= dumb_resize
;
2779 dcl
->dpy_refresh
= NULL
;
2781 dcl
->gui_timer_interval
= 500;
2782 register_displaychangelistener(ds
, dcl
);
2785 /***********************************************************/
2788 #define MAX_IO_HANDLERS 64
2790 typedef struct IOHandlerRecord
{
2792 IOCanRWHandler
*fd_read_poll
;
2794 IOHandler
*fd_write
;
2797 /* temporary data */
2799 struct IOHandlerRecord
*next
;
2802 static IOHandlerRecord
*first_io_handler
;
2804 /* XXX: fd_read_poll should be suppressed, but an API change is
2805 necessary in the character devices to suppress fd_can_read(). */
2806 int qemu_set_fd_handler2(int fd
,
2807 IOCanRWHandler
*fd_read_poll
,
2809 IOHandler
*fd_write
,
2812 IOHandlerRecord
**pioh
, *ioh
;
2814 if (!fd_read
&& !fd_write
) {
2815 pioh
= &first_io_handler
;
2820 if (ioh
->fd
== fd
) {
2827 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2831 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2834 ioh
->next
= first_io_handler
;
2835 first_io_handler
= ioh
;
2838 ioh
->fd_read_poll
= fd_read_poll
;
2839 ioh
->fd_read
= fd_read
;
2840 ioh
->fd_write
= fd_write
;
2841 ioh
->opaque
= opaque
;
2847 int qemu_set_fd_handler(int fd
,
2849 IOHandler
*fd_write
,
2852 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2856 /***********************************************************/
2857 /* Polling handling */
2859 typedef struct PollingEntry
{
2862 struct PollingEntry
*next
;
2865 static PollingEntry
*first_polling_entry
;
2867 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2869 PollingEntry
**ppe
, *pe
;
2870 pe
= qemu_mallocz(sizeof(PollingEntry
));
2874 pe
->opaque
= opaque
;
2875 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2880 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2882 PollingEntry
**ppe
, *pe
;
2883 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2885 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2893 /***********************************************************/
2894 /* Wait objects support */
2895 typedef struct WaitObjects
{
2897 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2898 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2899 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2902 static WaitObjects wait_objects
= {0};
2904 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2906 WaitObjects
*w
= &wait_objects
;
2908 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2910 w
->events
[w
->num
] = handle
;
2911 w
->func
[w
->num
] = func
;
2912 w
->opaque
[w
->num
] = opaque
;
2917 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2920 WaitObjects
*w
= &wait_objects
;
2923 for (i
= 0; i
< w
->num
; i
++) {
2924 if (w
->events
[i
] == handle
)
2927 w
->events
[i
] = w
->events
[i
+ 1];
2928 w
->func
[i
] = w
->func
[i
+ 1];
2929 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2937 /***********************************************************/
2938 /* ram save/restore */
2940 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2944 v
= qemu_get_byte(f
);
2947 if (qemu_get_buffer(f
, buf
, len
) != len
)
2951 v
= qemu_get_byte(f
);
2952 memset(buf
, v
, len
);
2958 if (qemu_file_has_error(f
))
2964 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2969 if (qemu_get_be32(f
) != phys_ram_size
)
2971 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2972 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2979 #define BDRV_HASH_BLOCK_SIZE 1024
2980 #define IOBUF_SIZE 4096
2981 #define RAM_CBLOCK_MAGIC 0xfabe
2983 typedef struct RamDecompressState
{
2986 uint8_t buf
[IOBUF_SIZE
];
2987 } RamDecompressState
;
2989 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2992 memset(s
, 0, sizeof(*s
));
2994 ret
= inflateInit(&s
->zstream
);
3000 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3004 s
->zstream
.avail_out
= len
;
3005 s
->zstream
.next_out
= buf
;
3006 while (s
->zstream
.avail_out
> 0) {
3007 if (s
->zstream
.avail_in
== 0) {
3008 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3010 clen
= qemu_get_be16(s
->f
);
3011 if (clen
> IOBUF_SIZE
)
3013 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3014 s
->zstream
.avail_in
= clen
;
3015 s
->zstream
.next_in
= s
->buf
;
3017 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3018 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3025 static void ram_decompress_close(RamDecompressState
*s
)
3027 inflateEnd(&s
->zstream
);
3030 #define RAM_SAVE_FLAG_FULL 0x01
3031 #define RAM_SAVE_FLAG_COMPRESS 0x02
3032 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3033 #define RAM_SAVE_FLAG_PAGE 0x08
3034 #define RAM_SAVE_FLAG_EOS 0x10
3036 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3038 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3039 uint32_t *array
= (uint32_t *)page
;
3042 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3043 if (array
[i
] != val
)
3050 static int ram_save_block(QEMUFile
*f
)
3052 static ram_addr_t current_addr
= 0;
3053 ram_addr_t saved_addr
= current_addr
;
3054 ram_addr_t addr
= 0;
3057 while (addr
< phys_ram_size
) {
3058 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3061 cpu_physical_memory_reset_dirty(current_addr
,
3062 current_addr
+ TARGET_PAGE_SIZE
,
3063 MIGRATION_DIRTY_FLAG
);
3065 ch
= *(phys_ram_base
+ current_addr
);
3067 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3068 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3069 qemu_put_byte(f
, ch
);
3071 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3072 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3078 addr
+= TARGET_PAGE_SIZE
;
3079 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3085 static ram_addr_t ram_save_threshold
= 10;
3087 static ram_addr_t
ram_save_remaining(void)
3090 ram_addr_t count
= 0;
3092 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3093 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3100 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3105 /* Make sure all dirty bits are set */
3106 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3107 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3108 cpu_physical_memory_set_dirty(addr
);
3111 /* Enable dirty memory tracking */
3112 cpu_physical_memory_set_dirty_tracking(1);
3114 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3117 while (!qemu_file_rate_limit(f
)) {
3120 ret
= ram_save_block(f
);
3121 if (ret
== 0) /* no more blocks */
3125 /* try transferring iterative blocks of memory */
3128 cpu_physical_memory_set_dirty_tracking(0);
3130 /* flush all remaining blocks regardless of rate limiting */
3131 while (ram_save_block(f
) != 0);
3134 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3136 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3139 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3141 RamDecompressState s1
, *s
= &s1
;
3145 if (ram_decompress_open(s
, f
) < 0)
3147 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3148 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3149 fprintf(stderr
, "Error while reading ram block header\n");
3153 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3154 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3159 printf("Error block header\n");
3163 ram_decompress_close(s
);
3168 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3173 if (version_id
== 1)
3174 return ram_load_v1(f
, opaque
);
3176 if (version_id
== 2) {
3177 if (qemu_get_be32(f
) != phys_ram_size
)
3179 return ram_load_dead(f
, opaque
);
3182 if (version_id
!= 3)
3186 addr
= qemu_get_be64(f
);
3188 flags
= addr
& ~TARGET_PAGE_MASK
;
3189 addr
&= TARGET_PAGE_MASK
;
3191 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3192 if (addr
!= phys_ram_size
)
3196 if (flags
& RAM_SAVE_FLAG_FULL
) {
3197 if (ram_load_dead(f
, opaque
) < 0)
3201 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3202 uint8_t ch
= qemu_get_byte(f
);
3203 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3204 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3205 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3206 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3211 void qemu_service_io(void)
3213 CPUState
*env
= cpu_single_env
;
3215 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3217 if (env
->kqemu_enabled
) {
3218 kqemu_cpu_interrupt(env
);
3224 /***********************************************************/
3225 /* bottom halves (can be seen as timers which expire ASAP) */
3236 static QEMUBH
*first_bh
= NULL
;
3238 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3241 bh
= qemu_mallocz(sizeof(QEMUBH
));
3245 bh
->opaque
= opaque
;
3246 bh
->next
= first_bh
;
3251 int qemu_bh_poll(void)
3257 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3258 if (!bh
->deleted
&& bh
->scheduled
) {
3267 /* remove deleted bhs */
3281 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3289 void qemu_bh_schedule(QEMUBH
*bh
)
3291 CPUState
*env
= cpu_single_env
;
3296 /* stop the currently executing CPU to execute the BH ASAP */
3298 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3302 void qemu_bh_cancel(QEMUBH
*bh
)
3307 void qemu_bh_delete(QEMUBH
*bh
)
3313 static void qemu_bh_update_timeout(int *timeout
)
3317 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3318 if (!bh
->deleted
&& bh
->scheduled
) {
3320 /* idle bottom halves will be polled at least
3322 *timeout
= MIN(10, *timeout
);
3324 /* non-idle bottom halves will be executed
3333 /***********************************************************/
3334 /* machine registration */
3336 static QEMUMachine
*first_machine
= NULL
;
3338 int qemu_register_machine(QEMUMachine
*m
)
3341 pm
= &first_machine
;
3349 static QEMUMachine
*find_machine(const char *name
)
3353 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3354 if (!strcmp(m
->name
, name
))
3360 /***********************************************************/
3361 /* main execution loop */
3363 static void gui_update(void *opaque
)
3365 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3366 DisplayState
*ds
= opaque
;
3367 DisplayChangeListener
*dcl
= ds
->listeners
;
3371 while (dcl
!= NULL
) {
3372 if (dcl
->gui_timer_interval
&&
3373 dcl
->gui_timer_interval
< interval
)
3374 interval
= dcl
->gui_timer_interval
;
3377 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3380 struct vm_change_state_entry
{
3381 VMChangeStateHandler
*cb
;
3383 LIST_ENTRY (vm_change_state_entry
) entries
;
3386 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3388 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3391 VMChangeStateEntry
*e
;
3393 e
= qemu_mallocz(sizeof (*e
));
3399 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3403 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3405 LIST_REMOVE (e
, entries
);
3409 static void vm_state_notify(int running
)
3411 VMChangeStateEntry
*e
;
3413 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3414 e
->cb(e
->opaque
, running
);
3418 /* XXX: support several handlers */
3419 static VMStopHandler
*vm_stop_cb
;
3420 static void *vm_stop_opaque
;
3422 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3425 vm_stop_opaque
= opaque
;
3429 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3440 qemu_rearm_alarm_timer(alarm_timer
);
3444 void vm_stop(int reason
)
3447 cpu_disable_ticks();
3451 vm_stop_cb(vm_stop_opaque
, reason
);
3458 /* reset/shutdown handler */
3460 typedef struct QEMUResetEntry
{
3461 QEMUResetHandler
*func
;
3463 struct QEMUResetEntry
*next
;
3466 static QEMUResetEntry
*first_reset_entry
;
3467 static int reset_requested
;
3468 static int shutdown_requested
;
3469 static int powerdown_requested
;
3471 int qemu_shutdown_requested(void)
3473 int r
= shutdown_requested
;
3474 shutdown_requested
= 0;
3478 int qemu_reset_requested(void)
3480 int r
= reset_requested
;
3481 reset_requested
= 0;
3485 int qemu_powerdown_requested(void)
3487 int r
= powerdown_requested
;
3488 powerdown_requested
= 0;
3492 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3494 QEMUResetEntry
**pre
, *re
;
3496 pre
= &first_reset_entry
;
3497 while (*pre
!= NULL
)
3498 pre
= &(*pre
)->next
;
3499 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3501 re
->opaque
= opaque
;
3506 void qemu_system_reset(void)
3510 /* reset all devices */
3511 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3512 re
->func(re
->opaque
);
3516 void qemu_system_reset_request(void)
3519 shutdown_requested
= 1;
3521 reset_requested
= 1;
3524 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3527 void qemu_system_shutdown_request(void)
3529 shutdown_requested
= 1;
3531 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3534 void qemu_system_powerdown_request(void)
3536 powerdown_requested
= 1;
3538 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3542 static void host_main_loop_wait(int *timeout
)
3548 /* XXX: need to suppress polling by better using win32 events */
3550 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3551 ret
|= pe
->func(pe
->opaque
);
3555 WaitObjects
*w
= &wait_objects
;
3557 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3558 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3559 if (w
->func
[ret
- WAIT_OBJECT_0
])
3560 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3562 /* Check for additional signaled events */
3563 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3565 /* Check if event is signaled */
3566 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3567 if(ret2
== WAIT_OBJECT_0
) {
3569 w
->func
[i
](w
->opaque
[i
]);
3570 } else if (ret2
== WAIT_TIMEOUT
) {
3572 err
= GetLastError();
3573 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3576 } else if (ret
== WAIT_TIMEOUT
) {
3578 err
= GetLastError();
3579 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3586 static void host_main_loop_wait(int *timeout
)
3591 void main_loop_wait(int timeout
)
3593 IOHandlerRecord
*ioh
;
3594 fd_set rfds
, wfds
, xfds
;
3598 qemu_bh_update_timeout(&timeout
);
3600 host_main_loop_wait(&timeout
);
3602 /* poll any events */
3603 /* XXX: separate device handlers from system ones */
3608 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3612 (!ioh
->fd_read_poll
||
3613 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3614 FD_SET(ioh
->fd
, &rfds
);
3618 if (ioh
->fd_write
) {
3619 FD_SET(ioh
->fd
, &wfds
);
3625 tv
.tv_sec
= timeout
/ 1000;
3626 tv
.tv_usec
= (timeout
% 1000) * 1000;
3628 #if defined(CONFIG_SLIRP)
3629 if (slirp_is_inited()) {
3630 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3633 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3635 IOHandlerRecord
**pioh
;
3637 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3638 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3639 ioh
->fd_read(ioh
->opaque
);
3641 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3642 ioh
->fd_write(ioh
->opaque
);
3646 /* remove deleted IO handlers */
3647 pioh
= &first_io_handler
;
3657 #if defined(CONFIG_SLIRP)
3658 if (slirp_is_inited()) {
3664 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3668 /* vm time timers */
3669 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3670 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3671 qemu_get_clock(vm_clock
));
3673 /* real time timers */
3674 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3675 qemu_get_clock(rt_clock
));
3677 /* Check bottom-halves last in case any of the earlier events triggered
3683 static int main_loop(void)
3686 #ifdef CONFIG_PROFILER
3691 cur_cpu
= first_cpu
;
3692 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3699 #ifdef CONFIG_PROFILER
3700 ti
= profile_getclock();
3705 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3706 env
->icount_decr
.u16
.low
= 0;
3707 env
->icount_extra
= 0;
3708 count
= qemu_next_deadline();
3709 count
= (count
+ (1 << icount_time_shift
) - 1)
3710 >> icount_time_shift
;
3711 qemu_icount
+= count
;
3712 decr
= (count
> 0xffff) ? 0xffff : count
;
3714 env
->icount_decr
.u16
.low
= decr
;
3715 env
->icount_extra
= count
;
3717 ret
= cpu_exec(env
);
3718 #ifdef CONFIG_PROFILER
3719 qemu_time
+= profile_getclock() - ti
;
3722 /* Fold pending instructions back into the
3723 instruction counter, and clear the interrupt flag. */
3724 qemu_icount
-= (env
->icount_decr
.u16
.low
3725 + env
->icount_extra
);
3726 env
->icount_decr
.u32
= 0;
3727 env
->icount_extra
= 0;
3729 next_cpu
= env
->next_cpu
?: first_cpu
;
3730 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3731 ret
= EXCP_INTERRUPT
;
3735 if (ret
== EXCP_HLT
) {
3736 /* Give the next CPU a chance to run. */
3740 if (ret
!= EXCP_HALTED
)
3742 /* all CPUs are halted ? */
3748 if (shutdown_requested
) {
3749 ret
= EXCP_INTERRUPT
;
3757 if (reset_requested
) {
3758 reset_requested
= 0;
3759 qemu_system_reset();
3760 ret
= EXCP_INTERRUPT
;
3762 if (powerdown_requested
) {
3763 powerdown_requested
= 0;
3764 qemu_system_powerdown();
3765 ret
= EXCP_INTERRUPT
;
3767 if (unlikely(ret
== EXCP_DEBUG
)) {
3768 gdb_set_stop_cpu(cur_cpu
);
3769 vm_stop(EXCP_DEBUG
);
3771 /* If all cpus are halted then wait until the next IRQ */
3772 /* XXX: use timeout computed from timers */
3773 if (ret
== EXCP_HALTED
) {
3777 /* Advance virtual time to the next event. */
3778 if (use_icount
== 1) {
3779 /* When not using an adaptive execution frequency
3780 we tend to get badly out of sync with real time,
3781 so just delay for a reasonable amount of time. */
3784 delta
= cpu_get_icount() - cpu_get_clock();
3787 /* If virtual time is ahead of real time then just
3789 timeout
= (delta
/ 1000000) + 1;
3791 /* Wait for either IO to occur or the next
3793 add
= qemu_next_deadline();
3794 /* We advance the timer before checking for IO.
3795 Limit the amount we advance so that early IO
3796 activity won't get the guest too far ahead. */
3800 add
= (add
+ (1 << icount_time_shift
) - 1)
3801 >> icount_time_shift
;
3803 timeout
= delta
/ 1000000;
3814 if (shutdown_requested
) {
3815 ret
= EXCP_INTERRUPT
;
3820 #ifdef CONFIG_PROFILER
3821 ti
= profile_getclock();
3823 main_loop_wait(timeout
);
3824 #ifdef CONFIG_PROFILER
3825 dev_time
+= profile_getclock() - ti
;
3828 cpu_disable_ticks();
3832 static void help(int exitcode
)
3834 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3835 "usage: %s [options] [disk_image]\n"
3837 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3839 "Standard options:\n"
3840 "-M machine select emulated machine (-M ? for list)\n"
3841 "-cpu cpu select CPU (-cpu ? for list)\n"
3842 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3843 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3844 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3845 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3846 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3847 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3848 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3849 " use 'file' as a drive image\n"
3850 "-mtdblock file use 'file' as on-board Flash memory image\n"
3851 "-sd file use 'file' as SecureDigital card image\n"
3852 "-pflash file use 'file' as a parallel flash image\n"
3853 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3854 "-snapshot write to temporary files instead of disk image files\n"
3856 "-no-frame open SDL window without a frame and window decorations\n"
3857 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3858 "-no-quit disable SDL window close capability\n"
3862 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3864 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3865 "-smp n set the number of CPUs to 'n' [default=1]\n"
3866 "-nographic disable graphical output and redirect serial I/Os to console\n"
3867 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3869 "-k language use keyboard layout (for example \"fr\" for French)\n"
3872 "-audio-help print list of audio drivers and their options\n"
3873 "-soundhw c1,... enable audio support\n"
3874 " and only specified sound cards (comma separated list)\n"
3875 " use -soundhw ? to get the list of supported cards\n"
3876 " use -soundhw all to enable all of them\n"
3878 "-vga [std|cirrus|vmware|none]\n"
3879 " select video card type\n"
3880 "-localtime set the real time clock to local time [default=utc]\n"
3881 "-full-screen start in full screen\n"
3883 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3884 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
3886 "-usb enable the USB driver (will be the default soon)\n"
3887 "-usbdevice name add the host or guest USB device 'name'\n"
3888 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3889 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3891 "-name string set the name of the guest\n"
3892 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3894 "Network options:\n"
3895 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3896 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3898 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3899 " connect the user mode network stack to VLAN 'n' and send\n"
3900 " hostname 'host' to DHCP clients\n"
3903 "-net tap[,vlan=n][,name=str],ifname=name\n"
3904 " connect the host TAP network interface to VLAN 'n'\n"
3906 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3907 " connect the host TAP network interface to VLAN 'n' and use the\n"
3908 " network scripts 'file' (default=%s)\n"
3909 " and 'dfile' (default=%s);\n"
3910 " use '[down]script=no' to disable script execution;\n"
3911 " use 'fd=h' to connect to an already opened TAP interface\n"
3913 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3914 " connect the vlan 'n' to another VLAN using a socket connection\n"
3915 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3916 " connect the vlan 'n' to multicast maddr and port\n"
3918 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3919 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3920 " on host and listening for incoming connections on 'socketpath'.\n"
3921 " Use group 'groupname' and mode 'octalmode' to change default\n"
3922 " ownership and permissions for communication port.\n"
3924 "-net none use it alone to have zero network devices; if no -net option\n"
3925 " is provided, the default is '-net nic -net user'\n"
3927 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3928 "-bt hci,host[:id]\n"
3929 " Use host's HCI with the given name\n"
3930 "-bt hci[,vlan=n]\n"
3931 " Emulate a standard HCI in virtual scatternet 'n'\n"
3932 "-bt vhci[,vlan=n]\n"
3933 " Add host computer to virtual scatternet 'n' using VHCI\n"
3934 "-bt device:dev[,vlan=n]\n"
3935 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3938 "-tftp dir allow tftp access to files in dir [-net user]\n"
3939 "-bootp file advertise file in BOOTP replies\n"
3941 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3943 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3944 " redirect TCP or UDP connections from host to guest [-net user]\n"
3947 "Linux boot specific:\n"
3948 "-kernel bzImage use 'bzImage' as kernel image\n"
3949 "-append cmdline use 'cmdline' as kernel command line\n"
3950 "-initrd file use 'file' as initial ram disk\n"
3952 "Debug/Expert options:\n"
3953 "-monitor dev redirect the monitor to char device 'dev'\n"
3954 "-serial dev redirect the serial port to char device 'dev'\n"
3955 "-parallel dev redirect the parallel port to char device 'dev'\n"
3956 "-pidfile file Write PID to 'file'\n"
3957 "-S freeze CPU at startup (use 'c' to start execution)\n"
3958 "-s wait gdb connection to port\n"
3959 "-p port set gdb connection port [default=%s]\n"
3960 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3961 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3962 " translation (t=none or lba) (usually qemu can guess them)\n"
3963 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3965 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3966 "-no-kqemu disable KQEMU kernel module usage\n"
3969 "-enable-kvm enable KVM full virtualization support\n"
3972 "-no-acpi disable ACPI\n"
3973 "-no-hpet disable HPET\n"
3975 #ifdef CONFIG_CURSES
3976 "-curses use a curses/ncurses interface instead of SDL\n"
3978 "-no-reboot exit instead of rebooting\n"
3979 "-no-shutdown stop before shutdown\n"
3980 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3981 "-vnc display start a VNC server on display\n"
3983 "-daemonize daemonize QEMU after initializing\n"
3985 "-option-rom rom load a file, rom, into the option ROM space\n"
3987 "-prom-env variable=value set OpenBIOS nvram variables\n"
3989 "-clock force the use of the given methods for timer alarm.\n"
3990 " To see what timers are available use -clock ?\n"
3991 "-startdate select initial date of the clock\n"
3992 "-icount [N|auto]\n"
3993 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
3995 "During emulation, the following keys are useful:\n"
3996 "ctrl-alt-f toggle full screen\n"
3997 "ctrl-alt-n switch to virtual console 'n'\n"
3998 "ctrl-alt toggle mouse and keyboard grab\n"
4000 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4005 DEFAULT_NETWORK_SCRIPT
,
4006 DEFAULT_NETWORK_DOWN_SCRIPT
,
4008 DEFAULT_GDBSTUB_PORT
,
4013 #define HAS_ARG 0x0001
4028 QEMU_OPTION_mtdblock
,
4032 QEMU_OPTION_snapshot
,
4034 QEMU_OPTION_no_fd_bootchk
,
4037 QEMU_OPTION_nographic
,
4038 QEMU_OPTION_portrait
,
4040 QEMU_OPTION_audio_help
,
4041 QEMU_OPTION_soundhw
,
4063 QEMU_OPTION_localtime
,
4067 QEMU_OPTION_monitor
,
4069 QEMU_OPTION_virtiocon
,
4070 QEMU_OPTION_parallel
,
4072 QEMU_OPTION_full_screen
,
4073 QEMU_OPTION_no_frame
,
4074 QEMU_OPTION_alt_grab
,
4075 QEMU_OPTION_no_quit
,
4077 QEMU_OPTION_pidfile
,
4078 QEMU_OPTION_no_kqemu
,
4079 QEMU_OPTION_kernel_kqemu
,
4080 QEMU_OPTION_enable_kvm
,
4081 QEMU_OPTION_win2k_hack
,
4082 QEMU_OPTION_rtc_td_hack
,
4084 QEMU_OPTION_usbdevice
,
4087 QEMU_OPTION_no_acpi
,
4088 QEMU_OPTION_no_hpet
,
4090 QEMU_OPTION_no_reboot
,
4091 QEMU_OPTION_no_shutdown
,
4092 QEMU_OPTION_show_cursor
,
4093 QEMU_OPTION_daemonize
,
4094 QEMU_OPTION_option_rom
,
4095 QEMU_OPTION_semihosting
,
4097 QEMU_OPTION_prom_env
,
4098 QEMU_OPTION_old_param
,
4100 QEMU_OPTION_startdate
,
4101 QEMU_OPTION_tb_size
,
4104 QEMU_OPTION_incoming
,
4107 typedef struct QEMUOption
{
4113 static const QEMUOption qemu_options
[] = {
4114 { "h", 0, QEMU_OPTION_h
},
4115 { "help", 0, QEMU_OPTION_h
},
4117 { "M", HAS_ARG
, QEMU_OPTION_M
},
4118 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4119 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4120 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4121 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4122 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4123 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4124 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4125 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4126 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4127 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4128 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4129 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4130 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4131 { "snapshot", 0, QEMU_OPTION_snapshot
},
4133 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4135 { "m", HAS_ARG
, QEMU_OPTION_m
},
4136 { "nographic", 0, QEMU_OPTION_nographic
},
4137 { "portrait", 0, QEMU_OPTION_portrait
},
4138 { "k", HAS_ARG
, QEMU_OPTION_k
},
4140 { "audio-help", 0, QEMU_OPTION_audio_help
},
4141 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4144 { "net", HAS_ARG
, QEMU_OPTION_net
},
4146 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4147 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4149 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4151 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4153 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4155 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4156 { "append", HAS_ARG
, QEMU_OPTION_append
},
4157 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4159 { "S", 0, QEMU_OPTION_S
},
4160 { "s", 0, QEMU_OPTION_s
},
4161 { "p", HAS_ARG
, QEMU_OPTION_p
},
4162 { "d", HAS_ARG
, QEMU_OPTION_d
},
4163 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4164 { "L", HAS_ARG
, QEMU_OPTION_L
},
4165 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4167 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4168 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4171 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4173 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4174 { "g", 1, QEMU_OPTION_g
},
4176 { "localtime", 0, QEMU_OPTION_localtime
},
4177 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4178 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4179 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4180 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4181 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4182 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4183 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4184 { "full-screen", 0, QEMU_OPTION_full_screen
},
4186 { "no-frame", 0, QEMU_OPTION_no_frame
},
4187 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4188 { "no-quit", 0, QEMU_OPTION_no_quit
},
4189 { "sdl", 0, QEMU_OPTION_sdl
},
4191 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4192 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4193 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4194 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4195 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4196 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4197 #ifdef CONFIG_CURSES
4198 { "curses", 0, QEMU_OPTION_curses
},
4200 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4202 /* temporary options */
4203 { "usb", 0, QEMU_OPTION_usb
},
4204 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4205 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4206 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4207 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4208 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4209 { "daemonize", 0, QEMU_OPTION_daemonize
},
4210 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4211 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4212 { "semihosting", 0, QEMU_OPTION_semihosting
},
4214 { "name", HAS_ARG
, QEMU_OPTION_name
},
4215 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4216 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4218 #if defined(TARGET_ARM)
4219 { "old-param", 0, QEMU_OPTION_old_param
},
4221 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4222 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4223 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4224 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4225 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4229 /* password input */
4231 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4236 if (!bdrv_is_encrypted(bs
))
4239 term_printf("%s is encrypted.\n", name
);
4240 for(i
= 0; i
< 3; i
++) {
4241 monitor_readline("Password: ", 1, password
, sizeof(password
));
4242 if (bdrv_set_key(bs
, password
) == 0)
4244 term_printf("invalid password\n");
4249 static BlockDriverState
*get_bdrv(int index
)
4251 if (index
> nb_drives
)
4253 return drives_table
[index
].bdrv
;
4256 static void read_passwords(void)
4258 BlockDriverState
*bs
;
4261 for(i
= 0; i
< 6; i
++) {
4264 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4269 struct soundhw soundhw
[] = {
4270 #ifdef HAS_AUDIO_CHOICE
4271 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4277 { .init_isa
= pcspk_audio_init
}
4284 "Creative Sound Blaster 16",
4287 { .init_isa
= SB16_init
}
4291 #ifdef CONFIG_CS4231A
4297 { .init_isa
= cs4231a_init
}
4305 "Yamaha YMF262 (OPL3)",
4307 "Yamaha YM3812 (OPL2)",
4311 { .init_isa
= Adlib_init
}
4318 "Gravis Ultrasound GF1",
4321 { .init_isa
= GUS_init
}
4328 "Intel 82801AA AC97 Audio",
4331 { .init_pci
= ac97_init
}
4335 #ifdef CONFIG_ES1370
4338 "ENSONIQ AudioPCI ES1370",
4341 { .init_pci
= es1370_init
}
4345 #endif /* HAS_AUDIO_CHOICE */
4347 { NULL
, NULL
, 0, 0, { NULL
} }
4350 static void select_soundhw (const char *optarg
)
4354 if (*optarg
== '?') {
4357 printf ("Valid sound card names (comma separated):\n");
4358 for (c
= soundhw
; c
->name
; ++c
) {
4359 printf ("%-11s %s\n", c
->name
, c
->descr
);
4361 printf ("\n-soundhw all will enable all of the above\n");
4362 exit (*optarg
!= '?');
4370 if (!strcmp (optarg
, "all")) {
4371 for (c
= soundhw
; c
->name
; ++c
) {
4379 e
= strchr (p
, ',');
4380 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4382 for (c
= soundhw
; c
->name
; ++c
) {
4383 if (!strncmp (c
->name
, p
, l
)) {
4392 "Unknown sound card name (too big to show)\n");
4395 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4400 p
+= l
+ (e
!= NULL
);
4404 goto show_valid_cards
;
4409 static void select_vgahw (const char *p
)
4413 if (strstart(p
, "std", &opts
)) {
4414 std_vga_enabled
= 1;
4415 cirrus_vga_enabled
= 0;
4417 } else if (strstart(p
, "cirrus", &opts
)) {
4418 cirrus_vga_enabled
= 1;
4419 std_vga_enabled
= 0;
4421 } else if (strstart(p
, "vmware", &opts
)) {
4422 cirrus_vga_enabled
= 0;
4423 std_vga_enabled
= 0;
4425 } else if (strstart(p
, "none", &opts
)) {
4426 cirrus_vga_enabled
= 0;
4427 std_vga_enabled
= 0;
4431 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4435 const char *nextopt
;
4437 if (strstart(opts
, ",retrace=", &nextopt
)) {
4439 if (strstart(opts
, "dumb", &nextopt
))
4440 vga_retrace_method
= VGA_RETRACE_DUMB
;
4441 else if (strstart(opts
, "precise", &nextopt
))
4442 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4443 else goto invalid_vga
;
4444 } else goto invalid_vga
;
4450 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4452 exit(STATUS_CONTROL_C_EXIT
);
4457 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4461 if(strlen(str
) != 36)
4464 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4465 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4466 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4474 #define MAX_NET_CLIENTS 32
4478 static void termsig_handler(int signal
)
4480 qemu_system_shutdown_request();
4483 static void termsig_setup(void)
4485 struct sigaction act
;
4487 memset(&act
, 0, sizeof(act
));
4488 act
.sa_handler
= termsig_handler
;
4489 sigaction(SIGINT
, &act
, NULL
);
4490 sigaction(SIGHUP
, &act
, NULL
);
4491 sigaction(SIGTERM
, &act
, NULL
);
4496 int main(int argc
, char **argv
, char **envp
)
4498 #ifdef CONFIG_GDBSTUB
4500 const char *gdbstub_port
;
4502 uint32_t boot_devices_bitmap
= 0;
4504 int snapshot
, linux_boot
, net_boot
;
4505 const char *initrd_filename
;
4506 const char *kernel_filename
, *kernel_cmdline
;
4507 const char *boot_devices
= "";
4508 DisplayState
*ds
= &display_state
;
4509 DisplayChangeListener
*dcl
;
4510 int cyls
, heads
, secs
, translation
;
4511 const char *net_clients
[MAX_NET_CLIENTS
];
4513 const char *bt_opts
[MAX_BT_CMDLINE
];
4517 const char *r
, *optarg
;
4518 CharDriverState
*monitor_hd
;
4519 const char *monitor_device
;
4520 const char *serial_devices
[MAX_SERIAL_PORTS
];
4521 int serial_device_index
;
4522 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4523 int parallel_device_index
;
4524 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4525 int virtio_console_index
;
4526 const char *loadvm
= NULL
;
4527 QEMUMachine
*machine
;
4528 const char *cpu_model
;
4529 const char *usb_devices
[MAX_USB_CMDLINE
];
4530 int usb_devices_index
;
4533 const char *pid_file
= NULL
;
4535 const char *incoming
= NULL
;
4537 qemu_cache_utils_init(envp
);
4539 LIST_INIT (&vm_change_state_head
);
4542 struct sigaction act
;
4543 sigfillset(&act
.sa_mask
);
4545 act
.sa_handler
= SIG_IGN
;
4546 sigaction(SIGPIPE
, &act
, NULL
);
4549 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4550 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4551 QEMU to run on a single CPU */
4556 h
= GetCurrentProcess();
4557 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4558 for(i
= 0; i
< 32; i
++) {
4559 if (mask
& (1 << i
))
4564 SetProcessAffinityMask(h
, mask
);
4570 register_machines();
4571 machine
= first_machine
;
4573 initrd_filename
= NULL
;
4575 vga_ram_size
= VGA_RAM_SIZE
;
4576 #ifdef CONFIG_GDBSTUB
4578 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4583 kernel_filename
= NULL
;
4584 kernel_cmdline
= "";
4585 cyls
= heads
= secs
= 0;
4586 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4587 monitor_device
= "vc";
4589 serial_devices
[0] = "vc:80Cx24C";
4590 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4591 serial_devices
[i
] = NULL
;
4592 serial_device_index
= 0;
4594 parallel_devices
[0] = "vc:640x480";
4595 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4596 parallel_devices
[i
] = NULL
;
4597 parallel_device_index
= 0;
4599 virtio_consoles
[0] = "vc:80Cx24C";
4600 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4601 virtio_consoles
[i
] = NULL
;
4602 virtio_console_index
= 0;
4604 usb_devices_index
= 0;
4623 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4625 const QEMUOption
*popt
;
4628 /* Treat --foo the same as -foo. */
4631 popt
= qemu_options
;
4634 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4638 if (!strcmp(popt
->name
, r
+ 1))
4642 if (popt
->flags
& HAS_ARG
) {
4643 if (optind
>= argc
) {
4644 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4648 optarg
= argv
[optind
++];
4653 switch(popt
->index
) {
4655 machine
= find_machine(optarg
);
4658 printf("Supported machines are:\n");
4659 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4660 printf("%-10s %s%s\n",
4662 m
== first_machine
? " (default)" : "");
4664 exit(*optarg
!= '?');
4667 case QEMU_OPTION_cpu
:
4668 /* hw initialization will check this */
4669 if (*optarg
== '?') {
4670 /* XXX: implement xxx_cpu_list for targets that still miss it */
4671 #if defined(cpu_list)
4672 cpu_list(stdout
, &fprintf
);
4679 case QEMU_OPTION_initrd
:
4680 initrd_filename
= optarg
;
4682 case QEMU_OPTION_hda
:
4684 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4686 hda_index
= drive_add(optarg
, HD_ALIAS
4687 ",cyls=%d,heads=%d,secs=%d%s",
4688 0, cyls
, heads
, secs
,
4689 translation
== BIOS_ATA_TRANSLATION_LBA
?
4691 translation
== BIOS_ATA_TRANSLATION_NONE
?
4692 ",trans=none" : "");
4694 case QEMU_OPTION_hdb
:
4695 case QEMU_OPTION_hdc
:
4696 case QEMU_OPTION_hdd
:
4697 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4699 case QEMU_OPTION_drive
:
4700 drive_add(NULL
, "%s", optarg
);
4702 case QEMU_OPTION_mtdblock
:
4703 drive_add(optarg
, MTD_ALIAS
);
4705 case QEMU_OPTION_sd
:
4706 drive_add(optarg
, SD_ALIAS
);
4708 case QEMU_OPTION_pflash
:
4709 drive_add(optarg
, PFLASH_ALIAS
);
4711 case QEMU_OPTION_snapshot
:
4714 case QEMU_OPTION_hdachs
:
4718 cyls
= strtol(p
, (char **)&p
, 0);
4719 if (cyls
< 1 || cyls
> 16383)
4724 heads
= strtol(p
, (char **)&p
, 0);
4725 if (heads
< 1 || heads
> 16)
4730 secs
= strtol(p
, (char **)&p
, 0);
4731 if (secs
< 1 || secs
> 63)
4735 if (!strcmp(p
, "none"))
4736 translation
= BIOS_ATA_TRANSLATION_NONE
;
4737 else if (!strcmp(p
, "lba"))
4738 translation
= BIOS_ATA_TRANSLATION_LBA
;
4739 else if (!strcmp(p
, "auto"))
4740 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4743 } else if (*p
!= '\0') {
4745 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4748 if (hda_index
!= -1)
4749 snprintf(drives_opt
[hda_index
].opt
,
4750 sizeof(drives_opt
[hda_index
].opt
),
4751 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4752 0, cyls
, heads
, secs
,
4753 translation
== BIOS_ATA_TRANSLATION_LBA
?
4755 translation
== BIOS_ATA_TRANSLATION_NONE
?
4756 ",trans=none" : "");
4759 case QEMU_OPTION_nographic
:
4762 #ifdef CONFIG_CURSES
4763 case QEMU_OPTION_curses
:
4767 case QEMU_OPTION_portrait
:
4770 case QEMU_OPTION_kernel
:
4771 kernel_filename
= optarg
;
4773 case QEMU_OPTION_append
:
4774 kernel_cmdline
= optarg
;
4776 case QEMU_OPTION_cdrom
:
4777 drive_add(optarg
, CDROM_ALIAS
);
4779 case QEMU_OPTION_boot
:
4780 boot_devices
= optarg
;
4781 /* We just do some generic consistency checks */
4783 /* Could easily be extended to 64 devices if needed */
4786 boot_devices_bitmap
= 0;
4787 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4788 /* Allowed boot devices are:
4789 * a b : floppy disk drives
4790 * c ... f : IDE disk drives
4791 * g ... m : machine implementation dependant drives
4792 * n ... p : network devices
4793 * It's up to each machine implementation to check
4794 * if the given boot devices match the actual hardware
4795 * implementation and firmware features.
4797 if (*p
< 'a' || *p
> 'q') {
4798 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4801 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4803 "Boot device '%c' was given twice\n",*p
);
4806 boot_devices_bitmap
|= 1 << (*p
- 'a');
4810 case QEMU_OPTION_fda
:
4811 case QEMU_OPTION_fdb
:
4812 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4815 case QEMU_OPTION_no_fd_bootchk
:
4819 case QEMU_OPTION_net
:
4820 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4821 fprintf(stderr
, "qemu: too many network clients\n");
4824 net_clients
[nb_net_clients
] = optarg
;
4828 case QEMU_OPTION_tftp
:
4829 tftp_prefix
= optarg
;
4831 case QEMU_OPTION_bootp
:
4832 bootp_filename
= optarg
;
4835 case QEMU_OPTION_smb
:
4836 net_slirp_smb(optarg
);
4839 case QEMU_OPTION_redir
:
4840 net_slirp_redir(optarg
);
4843 case QEMU_OPTION_bt
:
4844 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4845 fprintf(stderr
, "qemu: too many bluetooth options\n");
4848 bt_opts
[nb_bt_opts
++] = optarg
;
4851 case QEMU_OPTION_audio_help
:
4855 case QEMU_OPTION_soundhw
:
4856 select_soundhw (optarg
);
4862 case QEMU_OPTION_m
: {
4866 value
= strtoul(optarg
, &ptr
, 10);
4868 case 0: case 'M': case 'm':
4875 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4879 /* On 32-bit hosts, QEMU is limited by virtual address space */
4880 if (value
> (2047 << 20)
4882 && HOST_LONG_BITS
== 32
4885 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4888 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4889 fprintf(stderr
, "qemu: ram size too large\n");
4898 const CPULogItem
*item
;
4900 mask
= cpu_str_to_log_mask(optarg
);
4902 printf("Log items (comma separated):\n");
4903 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4904 printf("%-10s %s\n", item
->name
, item
->help
);
4911 #ifdef CONFIG_GDBSTUB
4916 gdbstub_port
= optarg
;
4922 case QEMU_OPTION_bios
:
4929 keyboard_layout
= optarg
;
4931 case QEMU_OPTION_localtime
:
4934 case QEMU_OPTION_vga
:
4935 select_vgahw (optarg
);
4942 w
= strtol(p
, (char **)&p
, 10);
4945 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4951 h
= strtol(p
, (char **)&p
, 10);
4956 depth
= strtol(p
, (char **)&p
, 10);
4957 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4958 depth
!= 24 && depth
!= 32)
4960 } else if (*p
== '\0') {
4961 depth
= graphic_depth
;
4968 graphic_depth
= depth
;
4971 case QEMU_OPTION_echr
:
4974 term_escape_char
= strtol(optarg
, &r
, 0);
4976 printf("Bad argument to echr\n");
4979 case QEMU_OPTION_monitor
:
4980 monitor_device
= optarg
;
4982 case QEMU_OPTION_serial
:
4983 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4984 fprintf(stderr
, "qemu: too many serial ports\n");
4987 serial_devices
[serial_device_index
] = optarg
;
4988 serial_device_index
++;
4990 case QEMU_OPTION_virtiocon
:
4991 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
4992 fprintf(stderr
, "qemu: too many virtio consoles\n");
4995 virtio_consoles
[virtio_console_index
] = optarg
;
4996 virtio_console_index
++;
4998 case QEMU_OPTION_parallel
:
4999 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5000 fprintf(stderr
, "qemu: too many parallel ports\n");
5003 parallel_devices
[parallel_device_index
] = optarg
;
5004 parallel_device_index
++;
5006 case QEMU_OPTION_loadvm
:
5009 case QEMU_OPTION_full_screen
:
5013 case QEMU_OPTION_no_frame
:
5016 case QEMU_OPTION_alt_grab
:
5019 case QEMU_OPTION_no_quit
:
5022 case QEMU_OPTION_sdl
:
5026 case QEMU_OPTION_pidfile
:
5030 case QEMU_OPTION_win2k_hack
:
5031 win2k_install_hack
= 1;
5033 case QEMU_OPTION_rtc_td_hack
:
5038 case QEMU_OPTION_no_kqemu
:
5041 case QEMU_OPTION_kernel_kqemu
:
5046 case QEMU_OPTION_enable_kvm
:
5053 case QEMU_OPTION_usb
:
5056 case QEMU_OPTION_usbdevice
:
5058 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5059 fprintf(stderr
, "Too many USB devices\n");
5062 usb_devices
[usb_devices_index
] = optarg
;
5063 usb_devices_index
++;
5065 case QEMU_OPTION_smp
:
5066 smp_cpus
= atoi(optarg
);
5068 fprintf(stderr
, "Invalid number of CPUs\n");
5072 case QEMU_OPTION_vnc
:
5073 vnc_display
= optarg
;
5075 case QEMU_OPTION_no_acpi
:
5078 case QEMU_OPTION_no_hpet
:
5081 case QEMU_OPTION_no_reboot
:
5084 case QEMU_OPTION_no_shutdown
:
5087 case QEMU_OPTION_show_cursor
:
5090 case QEMU_OPTION_uuid
:
5091 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5092 fprintf(stderr
, "Fail to parse UUID string."
5093 " Wrong format.\n");
5097 case QEMU_OPTION_daemonize
:
5100 case QEMU_OPTION_option_rom
:
5101 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5102 fprintf(stderr
, "Too many option ROMs\n");
5105 option_rom
[nb_option_roms
] = optarg
;
5108 case QEMU_OPTION_semihosting
:
5109 semihosting_enabled
= 1;
5111 case QEMU_OPTION_name
:
5114 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5115 case QEMU_OPTION_prom_env
:
5116 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5117 fprintf(stderr
, "Too many prom variables\n");
5120 prom_envs
[nb_prom_envs
] = optarg
;
5125 case QEMU_OPTION_old_param
:
5129 case QEMU_OPTION_clock
:
5130 configure_alarms(optarg
);
5132 case QEMU_OPTION_startdate
:
5135 time_t rtc_start_date
;
5136 if (!strcmp(optarg
, "now")) {
5137 rtc_date_offset
= -1;
5139 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5147 } else if (sscanf(optarg
, "%d-%d-%d",
5150 &tm
.tm_mday
) == 3) {
5159 rtc_start_date
= mktimegm(&tm
);
5160 if (rtc_start_date
== -1) {
5162 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5163 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5166 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5170 case QEMU_OPTION_tb_size
:
5171 tb_size
= strtol(optarg
, NULL
, 0);
5175 case QEMU_OPTION_icount
:
5177 if (strcmp(optarg
, "auto") == 0) {
5178 icount_time_shift
= -1;
5180 icount_time_shift
= strtol(optarg
, NULL
, 0);
5183 case QEMU_OPTION_incoming
:
5190 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5191 if (kvm_allowed
&& kqemu_allowed
) {
5193 "You can not enable both KVM and kqemu at the same time\n");
5198 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5199 if (smp_cpus
> machine
->max_cpus
) {
5200 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5201 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5207 if (serial_device_index
== 0)
5208 serial_devices
[0] = "stdio";
5209 if (parallel_device_index
== 0)
5210 parallel_devices
[0] = "null";
5211 if (strncmp(monitor_device
, "vc", 2) == 0)
5212 monitor_device
= "stdio";
5213 if (virtio_console_index
== 0)
5214 virtio_consoles
[0] = "null";
5221 if (pipe(fds
) == -1)
5232 len
= read(fds
[0], &status
, 1);
5233 if (len
== -1 && (errno
== EINTR
))
5238 else if (status
== 1) {
5239 fprintf(stderr
, "Could not acquire pidfile\n");
5256 signal(SIGTSTP
, SIG_IGN
);
5257 signal(SIGTTOU
, SIG_IGN
);
5258 signal(SIGTTIN
, SIG_IGN
);
5262 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5265 write(fds
[1], &status
, 1);
5267 fprintf(stderr
, "Could not acquire pid file\n");
5275 linux_boot
= (kernel_filename
!= NULL
);
5276 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5278 if (!linux_boot
&& net_boot
== 0 &&
5279 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5282 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5283 fprintf(stderr
, "-append only allowed with -kernel option\n");
5287 if (!linux_boot
&& initrd_filename
!= NULL
) {
5288 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5292 /* boot to floppy or the default cd if no hard disk defined yet */
5293 if (!boot_devices
[0]) {
5294 boot_devices
= "cad";
5296 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5299 if (init_timer_alarm() < 0) {
5300 fprintf(stderr
, "could not initialize alarm timer\n");
5303 if (use_icount
&& icount_time_shift
< 0) {
5305 /* 125MIPS seems a reasonable initial guess at the guest speed.
5306 It will be corrected fairly quickly anyway. */
5307 icount_time_shift
= 3;
5308 init_icount_adjust();
5315 /* init network clients */
5316 if (nb_net_clients
== 0) {
5317 /* if no clients, we use a default config */
5318 net_clients
[nb_net_clients
++] = "nic";
5320 net_clients
[nb_net_clients
++] = "user";
5324 for(i
= 0;i
< nb_net_clients
; i
++) {
5325 if (net_client_parse(net_clients
[i
]) < 0)
5331 /* XXX: this should be moved in the PC machine instantiation code */
5332 if (net_boot
!= 0) {
5334 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5335 const char *model
= nd_table
[i
].model
;
5337 if (net_boot
& (1 << i
)) {
5340 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5341 if (get_image_size(buf
) > 0) {
5342 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5343 fprintf(stderr
, "Too many option ROMs\n");
5346 option_rom
[nb_option_roms
] = strdup(buf
);
5353 fprintf(stderr
, "No valid PXE rom found for network device\n");
5359 /* init the bluetooth world */
5360 for (i
= 0; i
< nb_bt_opts
; i
++)
5361 if (bt_parse(bt_opts
[i
]))
5364 /* init the memory */
5365 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5367 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5369 if (ram_size
< phys_ram_size
) {
5370 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5371 machine
->name
, (unsigned long long) phys_ram_size
);
5375 phys_ram_size
= ram_size
;
5377 ram_size
= phys_ram_size
;
5380 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5382 phys_ram_size
+= ram_size
;
5385 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5386 if (!phys_ram_base
) {
5387 fprintf(stderr
, "Could not allocate physical memory\n");
5391 /* init the dynamic translator */
5392 cpu_exec_init_all(tb_size
* 1024 * 1024);
5396 /* we always create the cdrom drive, even if no disk is there */
5398 if (nb_drives_opt
< MAX_DRIVES
)
5399 drive_add(NULL
, CDROM_ALIAS
);
5401 /* we always create at least one floppy */
5403 if (nb_drives_opt
< MAX_DRIVES
)
5404 drive_add(NULL
, FD_ALIAS
, 0);
5406 /* we always create one sd slot, even if no card is in it */
5408 if (nb_drives_opt
< MAX_DRIVES
)
5409 drive_add(NULL
, SD_ALIAS
);
5411 /* open the virtual block devices */
5413 for(i
= 0; i
< nb_drives_opt
; i
++)
5414 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5417 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5418 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5421 memset(&display_state
, 0, sizeof(display_state
));
5422 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
5425 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5428 /* nearly nothing to do */
5429 dumb_display_init(ds
);
5431 #if defined(CONFIG_CURSES)
5433 /* At the moment curses cannot be used with other displays */
5434 curses_display_init(ds
, full_screen
);
5438 if (vnc_display
!= NULL
) {
5439 vnc_display_init(ds
);
5440 if (vnc_display_open(ds
, vnc_display
) < 0)
5443 if (sdl
|| !vnc_display
)
5444 #if defined(CONFIG_SDL)
5445 sdl_display_init(ds
, full_screen
, no_frame
);
5446 #elif defined(CONFIG_COCOA)
5447 cocoa_display_init(ds
, full_screen
);
5449 dumb_display_init(ds
);
5455 /* must be after terminal init, SDL library changes signal handlers */
5459 /* Maintain compatibility with multiple stdio monitors */
5460 if (!strcmp(monitor_device
,"stdio")) {
5461 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5462 const char *devname
= serial_devices
[i
];
5463 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5464 monitor_device
= NULL
;
5466 } else if (devname
&& !strcmp(devname
,"stdio")) {
5467 monitor_device
= NULL
;
5468 serial_devices
[i
] = "mon:stdio";
5473 if (monitor_device
) {
5474 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5476 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5479 monitor_init(monitor_hd
, !nographic
);
5482 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5483 const char *devname
= serial_devices
[i
];
5484 if (devname
&& strcmp(devname
, "none")) {
5486 snprintf(label
, sizeof(label
), "serial%d", i
);
5487 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5488 if (!serial_hds
[i
]) {
5489 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5493 if (strstart(devname
, "vc", 0))
5494 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5498 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5499 const char *devname
= parallel_devices
[i
];
5500 if (devname
&& strcmp(devname
, "none")) {
5502 snprintf(label
, sizeof(label
), "parallel%d", i
);
5503 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5504 if (!parallel_hds
[i
]) {
5505 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5509 if (strstart(devname
, "vc", 0))
5510 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5514 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5515 const char *devname
= virtio_consoles
[i
];
5516 if (devname
&& strcmp(devname
, "none")) {
5518 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5519 virtcon_hds
[i
] = qemu_chr_open(label
, devname
);
5520 if (!virtcon_hds
[i
]) {
5521 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5525 if (strstart(devname
, "vc", 0))
5526 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5530 if (kvm_enabled()) {
5533 ret
= kvm_init(smp_cpus
);
5535 fprintf(stderr
, "failed to initialize KVM\n");
5540 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5541 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5543 /* Set KVM's vcpu state to qemu's initial CPUState. */
5544 if (kvm_enabled()) {
5547 ret
= kvm_sync_vcpus();
5549 fprintf(stderr
, "failed to initialize vcpus\n");
5554 /* init USB devices */
5556 for(i
= 0; i
< usb_devices_index
; i
++) {
5557 if (usb_device_add(usb_devices
[i
]) < 0) {
5558 fprintf(stderr
, "Warning: could not add USB device %s\n",
5564 dcl
= ds
->listeners
;
5565 while (dcl
!= NULL
) {
5566 if (dcl
->dpy_refresh
!= NULL
) {
5567 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5568 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5572 #ifdef CONFIG_GDBSTUB
5574 /* XXX: use standard host:port notation and modify options
5576 if (gdbserver_start(gdbstub_port
) < 0) {
5577 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5588 autostart
= 0; /* fixme how to deal with -daemonize */
5589 qemu_start_incoming_migration(incoming
);
5593 /* XXX: simplify init */
5606 len
= write(fds
[1], &status
, 1);
5607 if (len
== -1 && (errno
== EINTR
))
5614 TFR(fd
= open("/dev/null", O_RDWR
));