4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
40 #include "audio/audio.h"
41 #include "migration.h"
54 #include <sys/times.h>
58 #include <sys/ioctl.h>
59 #include <sys/resource.h>
60 #include <sys/socket.h>
61 #include <netinet/in.h>
63 #if defined(__NetBSD__)
64 #include <net/if_tap.h>
67 #include <linux/if_tun.h>
69 #include <arpa/inet.h>
72 #include <sys/select.h>
80 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
81 #include <freebsd/stdlib.h>
86 #include <linux/rtc.h>
88 /* For the benefit of older linux systems which don't supply it,
89 we use a local copy of hpet.h. */
90 /* #include <linux/hpet.h> */
93 #include <linux/ppdev.h>
94 #include <linux/parport.h>
98 #include <sys/ethernet.h>
99 #include <sys/sockio.h>
100 #include <netinet/arp.h>
101 #include <netinet/in.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/ip.h>
104 #include <netinet/ip_icmp.h> // must come after ip.h
105 #include <netinet/udp.h>
106 #include <netinet/tcp.h>
114 #include "qemu_socket.h"
116 #if defined(CONFIG_SLIRP)
117 #include "libslirp.h"
120 #if defined(__OpenBSD__)
124 #if defined(CONFIG_VDE)
125 #include <libvdeplug.h>
130 #include <sys/timeb.h>
131 #include <mmsystem.h>
132 #define getopt_long_only getopt_long
133 #define memalign(align, size) malloc(size)
140 #endif /* CONFIG_SDL */
144 #define main qemu_main
145 #endif /* CONFIG_COCOA */
149 #include "exec-all.h"
151 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
152 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
154 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
156 #define SMBD_COMMAND "/usr/sbin/smbd"
159 //#define DEBUG_UNUSED_IOPORT
160 //#define DEBUG_IOPORT
162 //#define DEBUG_SLIRP
165 #define DEFAULT_RAM_SIZE 144
167 #define DEFAULT_RAM_SIZE 128
170 /* Max number of USB devices that can be specified on the commandline. */
171 #define MAX_USB_CMDLINE 8
173 /* Max number of bluetooth switches on the commandline. */
174 #define MAX_BT_CMDLINE 10
176 /* XXX: use a two level table to limit memory usage */
177 #define MAX_IOPORTS 65536
179 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
180 const char *bios_name
= NULL
;
181 static void *ioport_opaque
[MAX_IOPORTS
];
182 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
183 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
184 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
185 to store the VM snapshots */
186 DriveInfo drives_table
[MAX_DRIVES
+1];
188 static int vga_ram_size
;
189 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
190 DisplayState display_state
;
193 const char* keyboard_layout
= NULL
;
194 int64_t ticks_per_sec
;
197 NICInfo nd_table
[MAX_NICS
];
199 static int rtc_utc
= 1;
200 static int rtc_date_offset
= -1; /* -1 means no change */
201 int cirrus_vga_enabled
= 1;
202 int vmsvga_enabled
= 0;
204 int graphic_width
= 1024;
205 int graphic_height
= 768;
206 int graphic_depth
= 8;
208 int graphic_width
= 800;
209 int graphic_height
= 600;
210 int graphic_depth
= 15;
212 static int full_screen
= 0;
214 static int no_frame
= 0;
217 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
218 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
220 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
230 int graphic_rotate
= 0;
232 const char *option_rom
[MAX_OPTION_ROMS
];
234 int semihosting_enabled
= 0;
238 const char *qemu_name
;
241 unsigned int nb_prom_envs
= 0;
242 const char *prom_envs
[MAX_PROM_ENVS
];
244 static int nb_drives_opt
;
245 static struct drive_opt
{
248 } drives_opt
[MAX_DRIVES
];
250 static CPUState
*cur_cpu
;
251 static CPUState
*next_cpu
;
252 static int event_pending
= 1;
253 /* Conversion factor from emulated instructions to virtual clock ticks. */
254 static int icount_time_shift
;
255 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
256 #define MAX_ICOUNT_SHIFT 10
257 /* Compensate for varying guest execution speed. */
258 static int64_t qemu_icount_bias
;
259 static QEMUTimer
*icount_rt_timer
;
260 static QEMUTimer
*icount_vm_timer
;
262 uint8_t qemu_uuid
[16];
264 /***********************************************************/
265 /* x86 ISA bus support */
267 target_phys_addr_t isa_mem_base
= 0;
270 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
271 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
273 static uint32_t ioport_read(int index
, uint32_t address
)
275 static IOPortReadFunc
*default_func
[3] = {
276 default_ioport_readb
,
277 default_ioport_readw
,
280 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
282 func
= default_func
[index
];
283 return func(ioport_opaque
[address
], address
);
286 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
288 static IOPortWriteFunc
*default_func
[3] = {
289 default_ioport_writeb
,
290 default_ioport_writew
,
291 default_ioport_writel
293 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
295 func
= default_func
[index
];
296 func(ioport_opaque
[address
], address
, data
);
299 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
301 #ifdef DEBUG_UNUSED_IOPORT
302 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
307 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
309 #ifdef DEBUG_UNUSED_IOPORT
310 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
314 /* default is to make two byte accesses */
315 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
318 data
= ioport_read(0, address
);
319 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
320 data
|= ioport_read(0, address
) << 8;
324 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
326 ioport_write(0, address
, data
& 0xff);
327 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
328 ioport_write(0, address
, (data
>> 8) & 0xff);
331 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
333 #ifdef DEBUG_UNUSED_IOPORT
334 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
339 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
341 #ifdef DEBUG_UNUSED_IOPORT
342 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
346 /* size is the word size in byte */
347 int register_ioport_read(int start
, int length
, int size
,
348 IOPortReadFunc
*func
, void *opaque
)
354 } else if (size
== 2) {
356 } else if (size
== 4) {
359 hw_error("register_ioport_read: invalid size");
362 for(i
= start
; i
< start
+ length
; i
+= size
) {
363 ioport_read_table
[bsize
][i
] = func
;
364 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
365 hw_error("register_ioport_read: invalid opaque");
366 ioport_opaque
[i
] = opaque
;
371 /* size is the word size in byte */
372 int register_ioport_write(int start
, int length
, int size
,
373 IOPortWriteFunc
*func
, void *opaque
)
379 } else if (size
== 2) {
381 } else if (size
== 4) {
384 hw_error("register_ioport_write: invalid size");
387 for(i
= start
; i
< start
+ length
; i
+= size
) {
388 ioport_write_table
[bsize
][i
] = func
;
389 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
390 hw_error("register_ioport_write: invalid opaque");
391 ioport_opaque
[i
] = opaque
;
396 void isa_unassign_ioport(int start
, int length
)
400 for(i
= start
; i
< start
+ length
; i
++) {
401 ioport_read_table
[0][i
] = default_ioport_readb
;
402 ioport_read_table
[1][i
] = default_ioport_readw
;
403 ioport_read_table
[2][i
] = default_ioport_readl
;
405 ioport_write_table
[0][i
] = default_ioport_writeb
;
406 ioport_write_table
[1][i
] = default_ioport_writew
;
407 ioport_write_table
[2][i
] = default_ioport_writel
;
411 /***********************************************************/
413 void cpu_outb(CPUState
*env
, int addr
, int val
)
416 if (loglevel
& CPU_LOG_IOPORT
)
417 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
419 ioport_write(0, addr
, val
);
422 env
->last_io_time
= cpu_get_time_fast();
426 void cpu_outw(CPUState
*env
, int addr
, int val
)
429 if (loglevel
& CPU_LOG_IOPORT
)
430 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
432 ioport_write(1, addr
, val
);
435 env
->last_io_time
= cpu_get_time_fast();
439 void cpu_outl(CPUState
*env
, int addr
, int val
)
442 if (loglevel
& CPU_LOG_IOPORT
)
443 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
445 ioport_write(2, addr
, val
);
448 env
->last_io_time
= cpu_get_time_fast();
452 int cpu_inb(CPUState
*env
, int addr
)
455 val
= ioport_read(0, addr
);
457 if (loglevel
& CPU_LOG_IOPORT
)
458 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
462 env
->last_io_time
= cpu_get_time_fast();
467 int cpu_inw(CPUState
*env
, int addr
)
470 val
= ioport_read(1, addr
);
472 if (loglevel
& CPU_LOG_IOPORT
)
473 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
477 env
->last_io_time
= cpu_get_time_fast();
482 int cpu_inl(CPUState
*env
, int addr
)
485 val
= ioport_read(2, addr
);
487 if (loglevel
& CPU_LOG_IOPORT
)
488 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
492 env
->last_io_time
= cpu_get_time_fast();
497 /***********************************************************/
498 void hw_error(const char *fmt
, ...)
504 fprintf(stderr
, "qemu: hardware error: ");
505 vfprintf(stderr
, fmt
, ap
);
506 fprintf(stderr
, "\n");
507 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
508 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
510 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
512 cpu_dump_state(env
, stderr
, fprintf
, 0);
522 static QEMUBalloonEvent
*qemu_balloon_event
;
523 void *qemu_balloon_event_opaque
;
525 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
527 qemu_balloon_event
= func
;
528 qemu_balloon_event_opaque
= opaque
;
531 void qemu_balloon(ram_addr_t target
)
533 if (qemu_balloon_event
)
534 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
537 ram_addr_t
qemu_balloon_status(void)
539 if (qemu_balloon_event
)
540 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
544 /***********************************************************/
547 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
548 static void *qemu_put_kbd_event_opaque
;
549 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
550 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
552 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
554 qemu_put_kbd_event_opaque
= opaque
;
555 qemu_put_kbd_event
= func
;
558 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
559 void *opaque
, int absolute
,
562 QEMUPutMouseEntry
*s
, *cursor
;
564 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
568 s
->qemu_put_mouse_event
= func
;
569 s
->qemu_put_mouse_event_opaque
= opaque
;
570 s
->qemu_put_mouse_event_absolute
= absolute
;
571 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
574 if (!qemu_put_mouse_event_head
) {
575 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
579 cursor
= qemu_put_mouse_event_head
;
580 while (cursor
->next
!= NULL
)
581 cursor
= cursor
->next
;
584 qemu_put_mouse_event_current
= s
;
589 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
591 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
593 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
596 cursor
= qemu_put_mouse_event_head
;
597 while (cursor
!= NULL
&& cursor
!= entry
) {
599 cursor
= cursor
->next
;
602 if (cursor
== NULL
) // does not exist or list empty
604 else if (prev
== NULL
) { // entry is head
605 qemu_put_mouse_event_head
= cursor
->next
;
606 if (qemu_put_mouse_event_current
== entry
)
607 qemu_put_mouse_event_current
= cursor
->next
;
608 qemu_free(entry
->qemu_put_mouse_event_name
);
613 prev
->next
= entry
->next
;
615 if (qemu_put_mouse_event_current
== entry
)
616 qemu_put_mouse_event_current
= prev
;
618 qemu_free(entry
->qemu_put_mouse_event_name
);
622 void kbd_put_keycode(int keycode
)
624 if (qemu_put_kbd_event
) {
625 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
629 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
631 QEMUPutMouseEvent
*mouse_event
;
632 void *mouse_event_opaque
;
635 if (!qemu_put_mouse_event_current
) {
640 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
642 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
645 if (graphic_rotate
) {
646 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
649 width
= graphic_width
- 1;
650 mouse_event(mouse_event_opaque
,
651 width
- dy
, dx
, dz
, buttons_state
);
653 mouse_event(mouse_event_opaque
,
654 dx
, dy
, dz
, buttons_state
);
658 int kbd_mouse_is_absolute(void)
660 if (!qemu_put_mouse_event_current
)
663 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
666 void do_info_mice(void)
668 QEMUPutMouseEntry
*cursor
;
671 if (!qemu_put_mouse_event_head
) {
672 term_printf("No mouse devices connected\n");
676 term_printf("Mouse devices available:\n");
677 cursor
= qemu_put_mouse_event_head
;
678 while (cursor
!= NULL
) {
679 term_printf("%c Mouse #%d: %s\n",
680 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
681 index
, cursor
->qemu_put_mouse_event_name
);
683 cursor
= cursor
->next
;
687 void do_mouse_set(int index
)
689 QEMUPutMouseEntry
*cursor
;
692 if (!qemu_put_mouse_event_head
) {
693 term_printf("No mouse devices connected\n");
697 cursor
= qemu_put_mouse_event_head
;
698 while (cursor
!= NULL
&& index
!= i
) {
700 cursor
= cursor
->next
;
704 qemu_put_mouse_event_current
= cursor
;
706 term_printf("Mouse at given index not found\n");
709 /* compute with 96 bit intermediate result: (a*b)/c */
710 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
715 #ifdef WORDS_BIGENDIAN
725 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
726 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
729 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
733 /***********************************************************/
734 /* real time host monotonic timer */
736 #define QEMU_TIMER_BASE 1000000000LL
740 static int64_t clock_freq
;
742 static void init_get_clock(void)
746 ret
= QueryPerformanceFrequency(&freq
);
748 fprintf(stderr
, "Could not calibrate ticks\n");
751 clock_freq
= freq
.QuadPart
;
754 static int64_t get_clock(void)
757 QueryPerformanceCounter(&ti
);
758 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
763 static int use_rt_clock
;
765 static void init_get_clock(void)
768 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
771 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
778 static int64_t get_clock(void)
780 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
783 clock_gettime(CLOCK_MONOTONIC
, &ts
);
784 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
788 /* XXX: using gettimeofday leads to problems if the date
789 changes, so it should be avoided. */
791 gettimeofday(&tv
, NULL
);
792 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
797 /* Return the virtual CPU time, based on the instruction counter. */
798 static int64_t cpu_get_icount(void)
801 CPUState
*env
= cpu_single_env
;;
802 icount
= qemu_icount
;
805 fprintf(stderr
, "Bad clock read\n");
806 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
808 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
811 /***********************************************************/
812 /* guest cycle counter */
814 static int64_t cpu_ticks_prev
;
815 static int64_t cpu_ticks_offset
;
816 static int64_t cpu_clock_offset
;
817 static int cpu_ticks_enabled
;
819 /* return the host CPU cycle counter and handle stop/restart */
820 int64_t cpu_get_ticks(void)
823 return cpu_get_icount();
825 if (!cpu_ticks_enabled
) {
826 return cpu_ticks_offset
;
829 ticks
= cpu_get_real_ticks();
830 if (cpu_ticks_prev
> ticks
) {
831 /* Note: non increasing ticks may happen if the host uses
833 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
835 cpu_ticks_prev
= ticks
;
836 return ticks
+ cpu_ticks_offset
;
840 /* return the host CPU monotonic timer and handle stop/restart */
841 static int64_t cpu_get_clock(void)
844 if (!cpu_ticks_enabled
) {
845 return cpu_clock_offset
;
848 return ti
+ cpu_clock_offset
;
852 /* enable cpu_get_ticks() */
853 void cpu_enable_ticks(void)
855 if (!cpu_ticks_enabled
) {
856 cpu_ticks_offset
-= cpu_get_real_ticks();
857 cpu_clock_offset
-= get_clock();
858 cpu_ticks_enabled
= 1;
862 /* disable cpu_get_ticks() : the clock is stopped. You must not call
863 cpu_get_ticks() after that. */
864 void cpu_disable_ticks(void)
866 if (cpu_ticks_enabled
) {
867 cpu_ticks_offset
= cpu_get_ticks();
868 cpu_clock_offset
= cpu_get_clock();
869 cpu_ticks_enabled
= 0;
873 /***********************************************************/
876 #define QEMU_TIMER_REALTIME 0
877 #define QEMU_TIMER_VIRTUAL 1
881 /* XXX: add frequency */
889 struct QEMUTimer
*next
;
892 struct qemu_alarm_timer
{
896 int (*start
)(struct qemu_alarm_timer
*t
);
897 void (*stop
)(struct qemu_alarm_timer
*t
);
898 void (*rearm
)(struct qemu_alarm_timer
*t
);
902 #define ALARM_FLAG_DYNTICKS 0x1
903 #define ALARM_FLAG_EXPIRED 0x2
905 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
907 return t
->flags
& ALARM_FLAG_DYNTICKS
;
910 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
912 if (!alarm_has_dynticks(t
))
918 /* TODO: MIN_TIMER_REARM_US should be optimized */
919 #define MIN_TIMER_REARM_US 250
921 static struct qemu_alarm_timer
*alarm_timer
;
923 static int alarm_timer_rfd
, alarm_timer_wfd
;
928 struct qemu_alarm_win32
{
932 } alarm_win32_data
= {0, NULL
, -1};
934 static int win32_start_timer(struct qemu_alarm_timer
*t
);
935 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
936 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
940 static int unix_start_timer(struct qemu_alarm_timer
*t
);
941 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
945 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
946 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
947 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
949 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
950 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
952 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
953 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
955 #endif /* __linux__ */
959 /* Correlation between real and virtual time is always going to be
960 fairly approximate, so ignore small variation.
961 When the guest is idle real and virtual time will be aligned in
963 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
965 static void icount_adjust(void)
970 static int64_t last_delta
;
971 /* If the VM is not running, then do nothing. */
975 cur_time
= cpu_get_clock();
976 cur_icount
= qemu_get_clock(vm_clock
);
977 delta
= cur_icount
- cur_time
;
978 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
980 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
981 && icount_time_shift
> 0) {
982 /* The guest is getting too far ahead. Slow time down. */
986 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
987 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
988 /* The guest is getting too far behind. Speed time up. */
992 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
995 static void icount_adjust_rt(void * opaque
)
997 qemu_mod_timer(icount_rt_timer
,
998 qemu_get_clock(rt_clock
) + 1000);
1002 static void icount_adjust_vm(void * opaque
)
1004 qemu_mod_timer(icount_vm_timer
,
1005 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1009 static void init_icount_adjust(void)
1011 /* Have both realtime and virtual time triggers for speed adjustment.
1012 The realtime trigger catches emulated time passing too slowly,
1013 the virtual time trigger catches emulated time passing too fast.
1014 Realtime triggers occur even when idle, so use them less frequently
1015 than VM triggers. */
1016 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1017 qemu_mod_timer(icount_rt_timer
,
1018 qemu_get_clock(rt_clock
) + 1000);
1019 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1020 qemu_mod_timer(icount_vm_timer
,
1021 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1024 static struct qemu_alarm_timer alarm_timers
[] = {
1027 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1028 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1029 /* HPET - if available - is preferred */
1030 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1031 /* ...otherwise try RTC */
1032 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1034 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1036 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1037 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1038 {"win32", 0, win32_start_timer
,
1039 win32_stop_timer
, NULL
, &alarm_win32_data
},
1044 static void show_available_alarms(void)
1048 printf("Available alarm timers, in order of precedence:\n");
1049 for (i
= 0; alarm_timers
[i
].name
; i
++)
1050 printf("%s\n", alarm_timers
[i
].name
);
1053 static void configure_alarms(char const *opt
)
1057 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1060 struct qemu_alarm_timer tmp
;
1062 if (!strcmp(opt
, "?")) {
1063 show_available_alarms();
1069 /* Reorder the array */
1070 name
= strtok(arg
, ",");
1072 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1073 if (!strcmp(alarm_timers
[i
].name
, name
))
1078 fprintf(stderr
, "Unknown clock %s\n", name
);
1087 tmp
= alarm_timers
[i
];
1088 alarm_timers
[i
] = alarm_timers
[cur
];
1089 alarm_timers
[cur
] = tmp
;
1093 name
= strtok(NULL
, ",");
1099 /* Disable remaining timers */
1100 for (i
= cur
; i
< count
; i
++)
1101 alarm_timers
[i
].name
= NULL
;
1103 show_available_alarms();
1108 QEMUClock
*rt_clock
;
1109 QEMUClock
*vm_clock
;
1111 static QEMUTimer
*active_timers
[2];
1113 static QEMUClock
*qemu_new_clock(int type
)
1116 clock
= qemu_mallocz(sizeof(QEMUClock
));
1123 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1127 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1130 ts
->opaque
= opaque
;
1134 void qemu_free_timer(QEMUTimer
*ts
)
1139 /* stop a timer, but do not dealloc it */
1140 void qemu_del_timer(QEMUTimer
*ts
)
1144 /* NOTE: this code must be signal safe because
1145 qemu_timer_expired() can be called from a signal. */
1146 pt
= &active_timers
[ts
->clock
->type
];
1159 /* modify the current timer so that it will be fired when current_time
1160 >= expire_time. The corresponding callback will be called. */
1161 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1167 /* add the timer in the sorted list */
1168 /* NOTE: this code must be signal safe because
1169 qemu_timer_expired() can be called from a signal. */
1170 pt
= &active_timers
[ts
->clock
->type
];
1175 if (t
->expire_time
> expire_time
)
1179 ts
->expire_time
= expire_time
;
1183 /* Rearm if necessary */
1184 if (pt
== &active_timers
[ts
->clock
->type
]) {
1185 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1186 qemu_rearm_alarm_timer(alarm_timer
);
1188 /* Interrupt execution to force deadline recalculation. */
1189 if (use_icount
&& cpu_single_env
) {
1190 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1195 int qemu_timer_pending(QEMUTimer
*ts
)
1198 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1205 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1209 return (timer_head
->expire_time
<= current_time
);
1212 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1218 if (!ts
|| ts
->expire_time
> current_time
)
1220 /* remove timer from the list before calling the callback */
1221 *ptimer_head
= ts
->next
;
1224 /* run the callback (the timer list can be modified) */
1229 int64_t qemu_get_clock(QEMUClock
*clock
)
1231 switch(clock
->type
) {
1232 case QEMU_TIMER_REALTIME
:
1233 return get_clock() / 1000000;
1235 case QEMU_TIMER_VIRTUAL
:
1237 return cpu_get_icount();
1239 return cpu_get_clock();
1244 static void init_timers(void)
1247 ticks_per_sec
= QEMU_TIMER_BASE
;
1248 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1249 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1253 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1255 uint64_t expire_time
;
1257 if (qemu_timer_pending(ts
)) {
1258 expire_time
= ts
->expire_time
;
1262 qemu_put_be64(f
, expire_time
);
1265 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1267 uint64_t expire_time
;
1269 expire_time
= qemu_get_be64(f
);
1270 if (expire_time
!= -1) {
1271 qemu_mod_timer(ts
, expire_time
);
1277 static void timer_save(QEMUFile
*f
, void *opaque
)
1279 if (cpu_ticks_enabled
) {
1280 hw_error("cannot save state if virtual timers are running");
1282 qemu_put_be64(f
, cpu_ticks_offset
);
1283 qemu_put_be64(f
, ticks_per_sec
);
1284 qemu_put_be64(f
, cpu_clock_offset
);
1287 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1289 if (version_id
!= 1 && version_id
!= 2)
1291 if (cpu_ticks_enabled
) {
1294 cpu_ticks_offset
=qemu_get_be64(f
);
1295 ticks_per_sec
=qemu_get_be64(f
);
1296 if (version_id
== 2) {
1297 cpu_clock_offset
=qemu_get_be64(f
);
1303 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1304 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1306 static void host_alarm_handler(int host_signum
)
1310 #define DISP_FREQ 1000
1312 static int64_t delta_min
= INT64_MAX
;
1313 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1315 ti
= qemu_get_clock(vm_clock
);
1316 if (last_clock
!= 0) {
1317 delta
= ti
- last_clock
;
1318 if (delta
< delta_min
)
1320 if (delta
> delta_max
)
1323 if (++count
== DISP_FREQ
) {
1324 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1325 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1326 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1327 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1328 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1330 delta_min
= INT64_MAX
;
1338 if (alarm_has_dynticks(alarm_timer
) ||
1340 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1341 qemu_get_clock(vm_clock
))) ||
1342 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1343 qemu_get_clock(rt_clock
))) {
1344 CPUState
*env
= next_cpu
;
1347 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1348 SetEvent(data
->host_alarm
);
1350 static const char byte
= 0;
1351 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1353 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1356 /* stop the currently executing cpu because a timer occured */
1357 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1359 if (env
->kqemu_enabled
) {
1360 kqemu_cpu_interrupt(env
);
1368 static int64_t qemu_next_deadline(void)
1372 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1373 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1374 qemu_get_clock(vm_clock
);
1376 /* To avoid problems with overflow limit this to 2^32. */
1386 #if defined(__linux__) || defined(_WIN32)
1387 static uint64_t qemu_next_deadline_dyntick(void)
1395 delta
= (qemu_next_deadline() + 999) / 1000;
1397 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1398 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1399 qemu_get_clock(rt_clock
))*1000;
1400 if (rtdelta
< delta
)
1404 if (delta
< MIN_TIMER_REARM_US
)
1405 delta
= MIN_TIMER_REARM_US
;
1413 /* Sets a specific flag */
1414 static int fcntl_setfl(int fd
, int flag
)
1418 flags
= fcntl(fd
, F_GETFL
);
1422 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1428 #if defined(__linux__)
1430 #define RTC_FREQ 1024
1432 static void enable_sigio_timer(int fd
)
1434 struct sigaction act
;
1437 sigfillset(&act
.sa_mask
);
1439 act
.sa_handler
= host_alarm_handler
;
1441 sigaction(SIGIO
, &act
, NULL
);
1442 fcntl_setfl(fd
, O_ASYNC
);
1443 fcntl(fd
, F_SETOWN
, getpid());
1446 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1448 struct hpet_info info
;
1451 fd
= open("/dev/hpet", O_RDONLY
);
1456 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1458 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1459 "error, but for better emulation accuracy type:\n"
1460 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1464 /* Check capabilities */
1465 r
= ioctl(fd
, HPET_INFO
, &info
);
1469 /* Enable periodic mode */
1470 r
= ioctl(fd
, HPET_EPI
, 0);
1471 if (info
.hi_flags
&& (r
< 0))
1474 /* Enable interrupt */
1475 r
= ioctl(fd
, HPET_IE_ON
, 0);
1479 enable_sigio_timer(fd
);
1480 t
->priv
= (void *)(long)fd
;
1488 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1490 int fd
= (long)t
->priv
;
1495 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1498 unsigned long current_rtc_freq
= 0;
1500 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1503 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1504 if (current_rtc_freq
!= RTC_FREQ
&&
1505 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1506 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1507 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1508 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1511 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1517 enable_sigio_timer(rtc_fd
);
1519 t
->priv
= (void *)(long)rtc_fd
;
1524 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1526 int rtc_fd
= (long)t
->priv
;
1531 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1535 struct sigaction act
;
1537 sigfillset(&act
.sa_mask
);
1539 act
.sa_handler
= host_alarm_handler
;
1541 sigaction(SIGALRM
, &act
, NULL
);
1543 ev
.sigev_value
.sival_int
= 0;
1544 ev
.sigev_notify
= SIGEV_SIGNAL
;
1545 ev
.sigev_signo
= SIGALRM
;
1547 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1548 perror("timer_create");
1550 /* disable dynticks */
1551 fprintf(stderr
, "Dynamic Ticks disabled\n");
1556 t
->priv
= (void *)(long)host_timer
;
1561 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1563 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1565 timer_delete(host_timer
);
1568 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1570 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1571 struct itimerspec timeout
;
1572 int64_t nearest_delta_us
= INT64_MAX
;
1575 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1576 !active_timers
[QEMU_TIMER_VIRTUAL
])
1579 nearest_delta_us
= qemu_next_deadline_dyntick();
1581 /* check whether a timer is already running */
1582 if (timer_gettime(host_timer
, &timeout
)) {
1584 fprintf(stderr
, "Internal timer error: aborting\n");
1587 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1588 if (current_us
&& current_us
<= nearest_delta_us
)
1591 timeout
.it_interval
.tv_sec
= 0;
1592 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1593 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1594 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1595 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1597 fprintf(stderr
, "Internal timer error: aborting\n");
1602 #endif /* defined(__linux__) */
1604 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1606 struct sigaction act
;
1607 struct itimerval itv
;
1611 sigfillset(&act
.sa_mask
);
1613 act
.sa_handler
= host_alarm_handler
;
1615 sigaction(SIGALRM
, &act
, NULL
);
1617 itv
.it_interval
.tv_sec
= 0;
1618 /* for i386 kernel 2.6 to get 1 ms */
1619 itv
.it_interval
.tv_usec
= 999;
1620 itv
.it_value
.tv_sec
= 0;
1621 itv
.it_value
.tv_usec
= 10 * 1000;
1623 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1630 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1632 struct itimerval itv
;
1634 memset(&itv
, 0, sizeof(itv
));
1635 setitimer(ITIMER_REAL
, &itv
, NULL
);
1638 #endif /* !defined(_WIN32) */
1640 static void try_to_rearm_timer(void *opaque
)
1642 struct qemu_alarm_timer
*t
= opaque
;
1646 /* Drain the notify pipe */
1649 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1650 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1653 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1654 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1655 qemu_rearm_alarm_timer(alarm_timer
);
1661 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1664 struct qemu_alarm_win32
*data
= t
->priv
;
1667 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1668 if (!data
->host_alarm
) {
1669 perror("Failed CreateEvent");
1673 memset(&tc
, 0, sizeof(tc
));
1674 timeGetDevCaps(&tc
, sizeof(tc
));
1676 if (data
->period
< tc
.wPeriodMin
)
1677 data
->period
= tc
.wPeriodMin
;
1679 timeBeginPeriod(data
->period
);
1681 flags
= TIME_CALLBACK_FUNCTION
;
1682 if (alarm_has_dynticks(t
))
1683 flags
|= TIME_ONESHOT
;
1685 flags
|= TIME_PERIODIC
;
1687 data
->timerId
= timeSetEvent(1, // interval (ms)
1688 data
->period
, // resolution
1689 host_alarm_handler
, // function
1690 (DWORD
)t
, // parameter
1693 if (!data
->timerId
) {
1694 perror("Failed to initialize win32 alarm timer");
1696 timeEndPeriod(data
->period
);
1697 CloseHandle(data
->host_alarm
);
1701 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1706 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1708 struct qemu_alarm_win32
*data
= t
->priv
;
1710 timeKillEvent(data
->timerId
);
1711 timeEndPeriod(data
->period
);
1713 CloseHandle(data
->host_alarm
);
1716 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1718 struct qemu_alarm_win32
*data
= t
->priv
;
1719 uint64_t nearest_delta_us
;
1721 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1722 !active_timers
[QEMU_TIMER_VIRTUAL
])
1725 nearest_delta_us
= qemu_next_deadline_dyntick();
1726 nearest_delta_us
/= 1000;
1728 timeKillEvent(data
->timerId
);
1730 data
->timerId
= timeSetEvent(1,
1734 TIME_ONESHOT
| TIME_PERIODIC
);
1736 if (!data
->timerId
) {
1737 perror("Failed to re-arm win32 alarm timer");
1739 timeEndPeriod(data
->period
);
1740 CloseHandle(data
->host_alarm
);
1747 static int init_timer_alarm(void)
1749 struct qemu_alarm_timer
*t
= NULL
;
1759 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1763 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1767 alarm_timer_rfd
= fds
[0];
1768 alarm_timer_wfd
= fds
[1];
1771 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1772 t
= &alarm_timers
[i
];
1785 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1786 try_to_rearm_timer
, NULL
, t
);
1801 static void quit_timers(void)
1803 alarm_timer
->stop(alarm_timer
);
1807 /***********************************************************/
1808 /* host time/date access */
1809 void qemu_get_timedate(struct tm
*tm
, int offset
)
1816 if (rtc_date_offset
== -1) {
1820 ret
= localtime(&ti
);
1822 ti
-= rtc_date_offset
;
1826 memcpy(tm
, ret
, sizeof(struct tm
));
1829 int qemu_timedate_diff(struct tm
*tm
)
1833 if (rtc_date_offset
== -1)
1835 seconds
= mktimegm(tm
);
1837 seconds
= mktime(tm
);
1839 seconds
= mktimegm(tm
) + rtc_date_offset
;
1841 return seconds
- time(NULL
);
1845 static void socket_cleanup(void)
1850 static int socket_init(void)
1855 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1857 err
= WSAGetLastError();
1858 fprintf(stderr
, "WSAStartup: %d\n", err
);
1861 atexit(socket_cleanup
);
1866 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1871 while (*p
!= '\0' && *p
!= '=') {
1872 if (q
&& (q
- buf
) < buf_size
- 1)
1882 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1887 while (*p
!= '\0') {
1889 if (*(p
+ 1) != ',')
1893 if (q
&& (q
- buf
) < buf_size
- 1)
1903 int get_param_value(char *buf
, int buf_size
,
1904 const char *tag
, const char *str
)
1911 p
= get_opt_name(option
, sizeof(option
), p
);
1915 if (!strcmp(tag
, option
)) {
1916 (void)get_opt_value(buf
, buf_size
, p
);
1919 p
= get_opt_value(NULL
, 0, p
);
1928 int check_params(char *buf
, int buf_size
,
1929 const char * const *params
, const char *str
)
1936 p
= get_opt_name(buf
, buf_size
, p
);
1940 for(i
= 0; params
[i
] != NULL
; i
++)
1941 if (!strcmp(params
[i
], buf
))
1943 if (params
[i
] == NULL
)
1945 p
= get_opt_value(NULL
, 0, p
);
1953 /***********************************************************/
1954 /* Bluetooth support */
1957 static struct HCIInfo
*hci_table
[MAX_NICS
];
1959 static struct bt_vlan_s
{
1960 struct bt_scatternet_s net
;
1962 struct bt_vlan_s
*next
;
1965 /* find or alloc a new bluetooth "VLAN" */
1966 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1968 struct bt_vlan_s
**pvlan
, *vlan
;
1969 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1973 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1975 pvlan
= &first_bt_vlan
;
1976 while (*pvlan
!= NULL
)
1977 pvlan
= &(*pvlan
)->next
;
1982 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1986 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1991 static struct HCIInfo null_hci
= {
1992 .cmd_send
= null_hci_send
,
1993 .sco_send
= null_hci_send
,
1994 .acl_send
= null_hci_send
,
1995 .bdaddr_set
= null_hci_addr_set
,
1998 struct HCIInfo
*qemu_next_hci(void)
2000 if (cur_hci
== nb_hcis
)
2003 return hci_table
[cur_hci
++];
2006 static struct HCIInfo
*hci_init(const char *str
)
2009 struct bt_scatternet_s
*vlan
= 0;
2011 if (!strcmp(str
, "null"))
2014 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2016 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2017 else if (!strncmp(str
, "hci", 3)) {
2020 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2021 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2026 vlan
= qemu_find_bt_vlan(0);
2028 return bt_new_hci(vlan
);
2031 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2036 static int bt_hci_parse(const char *str
)
2038 struct HCIInfo
*hci
;
2041 if (nb_hcis
>= MAX_NICS
) {
2042 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2046 hci
= hci_init(str
);
2055 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2056 hci
->bdaddr_set(hci
, bdaddr
.b
);
2058 hci_table
[nb_hcis
++] = hci
;
2063 static void bt_vhci_add(int vlan_id
)
2065 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2068 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2069 "an empty scatternet %i\n", vlan_id
);
2071 bt_vhci_init(bt_new_hci(vlan
));
2074 static struct bt_device_s
*bt_device_add(const char *opt
)
2076 struct bt_scatternet_s
*vlan
;
2078 char *endp
= strstr(opt
, ",vlan=");
2079 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2082 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2085 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2087 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2092 vlan
= qemu_find_bt_vlan(vlan_id
);
2095 fprintf(stderr
, "qemu: warning: adding a slave device to "
2096 "an empty scatternet %i\n", vlan_id
);
2098 if (!strcmp(devname
, "keyboard"))
2099 return bt_keyboard_init(vlan
);
2101 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2105 static int bt_parse(const char *opt
)
2107 const char *endp
, *p
;
2110 if (strstart(opt
, "hci", &endp
)) {
2111 if (!*endp
|| *endp
== ',') {
2113 if (!strstart(endp
, ",vlan=", 0))
2116 return bt_hci_parse(opt
);
2118 } else if (strstart(opt
, "vhci", &endp
)) {
2119 if (!*endp
|| *endp
== ',') {
2121 if (strstart(endp
, ",vlan=", &p
)) {
2122 vlan
= strtol(p
, (char **) &endp
, 0);
2124 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2128 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2137 } else if (strstart(opt
, "device:", &endp
))
2138 return !bt_device_add(endp
);
2140 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2144 /***********************************************************/
2145 /* QEMU Block devices */
2147 #define HD_ALIAS "index=%d,media=disk"
2149 #define CDROM_ALIAS "index=1,media=cdrom"
2151 #define CDROM_ALIAS "index=2,media=cdrom"
2153 #define FD_ALIAS "index=%d,if=floppy"
2154 #define PFLASH_ALIAS "if=pflash"
2155 #define MTD_ALIAS "if=mtd"
2156 #define SD_ALIAS "index=0,if=sd"
2158 static int drive_add(const char *file
, const char *fmt
, ...)
2162 if (nb_drives_opt
>= MAX_DRIVES
) {
2163 fprintf(stderr
, "qemu: too many drives\n");
2167 drives_opt
[nb_drives_opt
].file
= file
;
2169 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2170 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2173 return nb_drives_opt
++;
2176 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2180 /* seek interface, bus and unit */
2182 for (index
= 0; index
< nb_drives
; index
++)
2183 if (drives_table
[index
].type
== type
&&
2184 drives_table
[index
].bus
== bus
&&
2185 drives_table
[index
].unit
== unit
)
2191 int drive_get_max_bus(BlockInterfaceType type
)
2197 for (index
= 0; index
< nb_drives
; index
++) {
2198 if(drives_table
[index
].type
== type
&&
2199 drives_table
[index
].bus
> max_bus
)
2200 max_bus
= drives_table
[index
].bus
;
2205 static void bdrv_format_print(void *opaque
, const char *name
)
2207 fprintf(stderr
, " %s", name
);
2210 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2211 QEMUMachine
*machine
)
2216 const char *mediastr
= "";
2217 BlockInterfaceType type
;
2218 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2219 int bus_id
, unit_id
;
2220 int cyls
, heads
, secs
, translation
;
2221 BlockDriverState
*bdrv
;
2222 BlockDriver
*drv
= NULL
;
2227 char *str
= arg
->opt
;
2228 static const char * const params
[] = { "bus", "unit", "if", "index",
2229 "cyls", "heads", "secs", "trans",
2230 "media", "snapshot", "file",
2231 "cache", "format", NULL
};
2233 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2234 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2240 cyls
= heads
= secs
= 0;
2243 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2247 if (machine
->use_scsi
) {
2249 max_devs
= MAX_SCSI_DEVS
;
2250 pstrcpy(devname
, sizeof(devname
), "scsi");
2253 max_devs
= MAX_IDE_DEVS
;
2254 pstrcpy(devname
, sizeof(devname
), "ide");
2258 /* extract parameters */
2260 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2261 bus_id
= strtol(buf
, NULL
, 0);
2263 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2268 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2269 unit_id
= strtol(buf
, NULL
, 0);
2271 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2276 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2277 pstrcpy(devname
, sizeof(devname
), buf
);
2278 if (!strcmp(buf
, "ide")) {
2280 max_devs
= MAX_IDE_DEVS
;
2281 } else if (!strcmp(buf
, "scsi")) {
2283 max_devs
= MAX_SCSI_DEVS
;
2284 } else if (!strcmp(buf
, "floppy")) {
2287 } else if (!strcmp(buf
, "pflash")) {
2290 } else if (!strcmp(buf
, "mtd")) {
2293 } else if (!strcmp(buf
, "sd")) {
2296 } else if (!strcmp(buf
, "virtio")) {
2300 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2305 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2306 index
= strtol(buf
, NULL
, 0);
2308 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2313 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2314 cyls
= strtol(buf
, NULL
, 0);
2317 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2318 heads
= strtol(buf
, NULL
, 0);
2321 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2322 secs
= strtol(buf
, NULL
, 0);
2325 if (cyls
|| heads
|| secs
) {
2326 if (cyls
< 1 || cyls
> 16383) {
2327 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2330 if (heads
< 1 || heads
> 16) {
2331 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2334 if (secs
< 1 || secs
> 63) {
2335 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2340 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2343 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2347 if (!strcmp(buf
, "none"))
2348 translation
= BIOS_ATA_TRANSLATION_NONE
;
2349 else if (!strcmp(buf
, "lba"))
2350 translation
= BIOS_ATA_TRANSLATION_LBA
;
2351 else if (!strcmp(buf
, "auto"))
2352 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2354 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2359 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2360 if (!strcmp(buf
, "disk")) {
2362 } else if (!strcmp(buf
, "cdrom")) {
2363 if (cyls
|| secs
|| heads
) {
2365 "qemu: '%s' invalid physical CHS format\n", str
);
2368 media
= MEDIA_CDROM
;
2370 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2375 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2376 if (!strcmp(buf
, "on"))
2378 else if (!strcmp(buf
, "off"))
2381 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2386 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2387 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2389 else if (!strcmp(buf
, "writethrough"))
2391 else if (!strcmp(buf
, "writeback"))
2394 fprintf(stderr
, "qemu: invalid cache option\n");
2399 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2400 if (strcmp(buf
, "?") == 0) {
2401 fprintf(stderr
, "qemu: Supported formats:");
2402 bdrv_iterate_format(bdrv_format_print
, NULL
);
2403 fprintf(stderr
, "\n");
2406 drv
= bdrv_find_format(buf
);
2408 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2413 if (arg
->file
== NULL
)
2414 get_param_value(file
, sizeof(file
), "file", str
);
2416 pstrcpy(file
, sizeof(file
), arg
->file
);
2418 /* compute bus and unit according index */
2421 if (bus_id
!= 0 || unit_id
!= -1) {
2423 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2431 unit_id
= index
% max_devs
;
2432 bus_id
= index
/ max_devs
;
2436 /* if user doesn't specify a unit_id,
2437 * try to find the first free
2440 if (unit_id
== -1) {
2442 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2444 if (max_devs
&& unit_id
>= max_devs
) {
2445 unit_id
-= max_devs
;
2453 if (max_devs
&& unit_id
>= max_devs
) {
2454 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2455 str
, unit_id
, max_devs
- 1);
2460 * ignore multiple definitions
2463 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2468 if (type
== IF_IDE
|| type
== IF_SCSI
)
2469 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2471 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2472 devname
, bus_id
, mediastr
, unit_id
);
2474 snprintf(buf
, sizeof(buf
), "%s%s%i",
2475 devname
, mediastr
, unit_id
);
2476 bdrv
= bdrv_new(buf
);
2477 drives_table
[nb_drives
].bdrv
= bdrv
;
2478 drives_table
[nb_drives
].type
= type
;
2479 drives_table
[nb_drives
].bus
= bus_id
;
2480 drives_table
[nb_drives
].unit
= unit_id
;
2489 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2490 bdrv_set_translation_hint(bdrv
, translation
);
2494 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2499 /* FIXME: This isn't really a floppy, but it's a reasonable
2502 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2513 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2514 cache
= 2; /* always use write-back with snapshot */
2516 if (cache
== 0) /* no caching */
2517 bdrv_flags
|= BDRV_O_NOCACHE
;
2518 else if (cache
== 2) /* write-back */
2519 bdrv_flags
|= BDRV_O_CACHE_WB
;
2520 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2521 fprintf(stderr
, "qemu: could not open disk image %s\n",
2528 /***********************************************************/
2531 static USBPort
*used_usb_ports
;
2532 static USBPort
*free_usb_ports
;
2534 /* ??? Maybe change this to register a hub to keep track of the topology. */
2535 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2536 usb_attachfn attach
)
2538 port
->opaque
= opaque
;
2539 port
->index
= index
;
2540 port
->attach
= attach
;
2541 port
->next
= free_usb_ports
;
2542 free_usb_ports
= port
;
2545 int usb_device_add_dev(USBDevice
*dev
)
2549 /* Find a USB port to add the device to. */
2550 port
= free_usb_ports
;
2554 /* Create a new hub and chain it on. */
2555 free_usb_ports
= NULL
;
2556 port
->next
= used_usb_ports
;
2557 used_usb_ports
= port
;
2559 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2560 usb_attach(port
, hub
);
2561 port
= free_usb_ports
;
2564 free_usb_ports
= port
->next
;
2565 port
->next
= used_usb_ports
;
2566 used_usb_ports
= port
;
2567 usb_attach(port
, dev
);
2571 static int usb_device_add(const char *devname
)
2576 if (!free_usb_ports
)
2579 if (strstart(devname
, "host:", &p
)) {
2580 dev
= usb_host_device_open(p
);
2581 } else if (!strcmp(devname
, "mouse")) {
2582 dev
= usb_mouse_init();
2583 } else if (!strcmp(devname
, "tablet")) {
2584 dev
= usb_tablet_init();
2585 } else if (!strcmp(devname
, "keyboard")) {
2586 dev
= usb_keyboard_init();
2587 } else if (strstart(devname
, "disk:", &p
)) {
2588 dev
= usb_msd_init(p
);
2589 } else if (!strcmp(devname
, "wacom-tablet")) {
2590 dev
= usb_wacom_init();
2591 } else if (strstart(devname
, "serial:", &p
)) {
2592 dev
= usb_serial_init(p
);
2593 #ifdef CONFIG_BRLAPI
2594 } else if (!strcmp(devname
, "braille")) {
2595 dev
= usb_baum_init();
2597 } else if (strstart(devname
, "net:", &p
)) {
2600 if (net_client_init("nic", p
) < 0)
2602 nd_table
[nic
].model
= "usb";
2603 dev
= usb_net_init(&nd_table
[nic
]);
2604 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2605 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2606 bt_new_hci(qemu_find_bt_vlan(0)));
2613 return usb_device_add_dev(dev
);
2616 int usb_device_del_addr(int bus_num
, int addr
)
2622 if (!used_usb_ports
)
2628 lastp
= &used_usb_ports
;
2629 port
= used_usb_ports
;
2630 while (port
&& port
->dev
->addr
!= addr
) {
2631 lastp
= &port
->next
;
2639 *lastp
= port
->next
;
2640 usb_attach(port
, NULL
);
2641 dev
->handle_destroy(dev
);
2642 port
->next
= free_usb_ports
;
2643 free_usb_ports
= port
;
2647 static int usb_device_del(const char *devname
)
2652 if (strstart(devname
, "host:", &p
))
2653 return usb_host_device_close(p
);
2655 if (!used_usb_ports
)
2658 p
= strchr(devname
, '.');
2661 bus_num
= strtoul(devname
, NULL
, 0);
2662 addr
= strtoul(p
+ 1, NULL
, 0);
2664 return usb_device_del_addr(bus_num
, addr
);
2667 void do_usb_add(const char *devname
)
2669 usb_device_add(devname
);
2672 void do_usb_del(const char *devname
)
2674 usb_device_del(devname
);
2681 const char *speed_str
;
2684 term_printf("USB support not enabled\n");
2688 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2692 switch(dev
->speed
) {
2696 case USB_SPEED_FULL
:
2699 case USB_SPEED_HIGH
:
2706 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2707 0, dev
->addr
, speed_str
, dev
->devname
);
2711 /***********************************************************/
2712 /* PCMCIA/Cardbus */
2714 static struct pcmcia_socket_entry_s
{
2715 struct pcmcia_socket_s
*socket
;
2716 struct pcmcia_socket_entry_s
*next
;
2717 } *pcmcia_sockets
= 0;
2719 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2721 struct pcmcia_socket_entry_s
*entry
;
2723 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2724 entry
->socket
= socket
;
2725 entry
->next
= pcmcia_sockets
;
2726 pcmcia_sockets
= entry
;
2729 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2731 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2733 ptr
= &pcmcia_sockets
;
2734 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2735 if (entry
->socket
== socket
) {
2741 void pcmcia_info(void)
2743 struct pcmcia_socket_entry_s
*iter
;
2744 if (!pcmcia_sockets
)
2745 term_printf("No PCMCIA sockets\n");
2747 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2748 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2749 iter
->socket
->attached
? iter
->socket
->card_string
:
2753 /***********************************************************/
2756 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2760 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2764 static void dumb_display_init(DisplayState
*ds
)
2769 ds
->dpy_update
= dumb_update
;
2770 ds
->dpy_resize
= dumb_resize
;
2771 ds
->dpy_refresh
= NULL
;
2772 ds
->gui_timer_interval
= 0;
2776 /***********************************************************/
2779 #define MAX_IO_HANDLERS 64
2781 typedef struct IOHandlerRecord
{
2783 IOCanRWHandler
*fd_read_poll
;
2785 IOHandler
*fd_write
;
2788 /* temporary data */
2790 struct IOHandlerRecord
*next
;
2793 static IOHandlerRecord
*first_io_handler
;
2795 /* XXX: fd_read_poll should be suppressed, but an API change is
2796 necessary in the character devices to suppress fd_can_read(). */
2797 int qemu_set_fd_handler2(int fd
,
2798 IOCanRWHandler
*fd_read_poll
,
2800 IOHandler
*fd_write
,
2803 IOHandlerRecord
**pioh
, *ioh
;
2805 if (!fd_read
&& !fd_write
) {
2806 pioh
= &first_io_handler
;
2811 if (ioh
->fd
== fd
) {
2818 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2822 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2825 ioh
->next
= first_io_handler
;
2826 first_io_handler
= ioh
;
2829 ioh
->fd_read_poll
= fd_read_poll
;
2830 ioh
->fd_read
= fd_read
;
2831 ioh
->fd_write
= fd_write
;
2832 ioh
->opaque
= opaque
;
2838 int qemu_set_fd_handler(int fd
,
2840 IOHandler
*fd_write
,
2843 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2847 /***********************************************************/
2848 /* Polling handling */
2850 typedef struct PollingEntry
{
2853 struct PollingEntry
*next
;
2856 static PollingEntry
*first_polling_entry
;
2858 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2860 PollingEntry
**ppe
, *pe
;
2861 pe
= qemu_mallocz(sizeof(PollingEntry
));
2865 pe
->opaque
= opaque
;
2866 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2871 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2873 PollingEntry
**ppe
, *pe
;
2874 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2876 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2884 /***********************************************************/
2885 /* Wait objects support */
2886 typedef struct WaitObjects
{
2888 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2889 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2890 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2893 static WaitObjects wait_objects
= {0};
2895 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2897 WaitObjects
*w
= &wait_objects
;
2899 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2901 w
->events
[w
->num
] = handle
;
2902 w
->func
[w
->num
] = func
;
2903 w
->opaque
[w
->num
] = opaque
;
2908 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2911 WaitObjects
*w
= &wait_objects
;
2914 for (i
= 0; i
< w
->num
; i
++) {
2915 if (w
->events
[i
] == handle
)
2918 w
->events
[i
] = w
->events
[i
+ 1];
2919 w
->func
[i
] = w
->func
[i
+ 1];
2920 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2928 /***********************************************************/
2929 /* ram save/restore */
2931 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2935 v
= qemu_get_byte(f
);
2938 if (qemu_get_buffer(f
, buf
, len
) != len
)
2942 v
= qemu_get_byte(f
);
2943 memset(buf
, v
, len
);
2949 if (qemu_file_has_error(f
))
2955 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2960 if (qemu_get_be32(f
) != phys_ram_size
)
2962 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2963 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2970 #define BDRV_HASH_BLOCK_SIZE 1024
2971 #define IOBUF_SIZE 4096
2972 #define RAM_CBLOCK_MAGIC 0xfabe
2974 typedef struct RamDecompressState
{
2977 uint8_t buf
[IOBUF_SIZE
];
2978 } RamDecompressState
;
2980 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2983 memset(s
, 0, sizeof(*s
));
2985 ret
= inflateInit(&s
->zstream
);
2991 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2995 s
->zstream
.avail_out
= len
;
2996 s
->zstream
.next_out
= buf
;
2997 while (s
->zstream
.avail_out
> 0) {
2998 if (s
->zstream
.avail_in
== 0) {
2999 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3001 clen
= qemu_get_be16(s
->f
);
3002 if (clen
> IOBUF_SIZE
)
3004 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3005 s
->zstream
.avail_in
= clen
;
3006 s
->zstream
.next_in
= s
->buf
;
3008 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3009 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3016 static void ram_decompress_close(RamDecompressState
*s
)
3018 inflateEnd(&s
->zstream
);
3021 #define RAM_SAVE_FLAG_FULL 0x01
3022 #define RAM_SAVE_FLAG_COMPRESS 0x02
3023 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3024 #define RAM_SAVE_FLAG_PAGE 0x08
3025 #define RAM_SAVE_FLAG_EOS 0x10
3027 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3029 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3030 uint32_t *array
= (uint32_t *)page
;
3033 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3034 if (array
[i
] != val
)
3041 static int ram_save_block(QEMUFile
*f
)
3043 static ram_addr_t current_addr
= 0;
3044 ram_addr_t saved_addr
= current_addr
;
3045 ram_addr_t addr
= 0;
3048 while (addr
< phys_ram_size
) {
3049 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3052 cpu_physical_memory_reset_dirty(current_addr
,
3053 current_addr
+ TARGET_PAGE_SIZE
,
3054 MIGRATION_DIRTY_FLAG
);
3056 ch
= *(phys_ram_base
+ current_addr
);
3058 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3059 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3060 qemu_put_byte(f
, ch
);
3062 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3063 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3069 addr
+= TARGET_PAGE_SIZE
;
3070 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3076 static ram_addr_t ram_save_threshold
= 10;
3078 static ram_addr_t
ram_save_remaining(void)
3081 ram_addr_t count
= 0;
3083 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3084 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3091 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3096 /* Make sure all dirty bits are set */
3097 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3098 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3099 cpu_physical_memory_set_dirty(addr
);
3102 /* Enable dirty memory tracking */
3103 cpu_physical_memory_set_dirty_tracking(1);
3105 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3108 while (!qemu_file_rate_limit(f
)) {
3111 ret
= ram_save_block(f
);
3112 if (ret
== 0) /* no more blocks */
3116 /* try transferring iterative blocks of memory */
3119 cpu_physical_memory_set_dirty_tracking(0);
3121 /* flush all remaining blocks regardless of rate limiting */
3122 while (ram_save_block(f
) != 0);
3125 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3127 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3130 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3132 RamDecompressState s1
, *s
= &s1
;
3136 if (ram_decompress_open(s
, f
) < 0)
3138 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3139 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3140 fprintf(stderr
, "Error while reading ram block header\n");
3144 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3145 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3150 printf("Error block header\n");
3154 ram_decompress_close(s
);
3159 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3164 if (version_id
== 1)
3165 return ram_load_v1(f
, opaque
);
3167 if (version_id
== 2) {
3168 if (qemu_get_be32(f
) != phys_ram_size
)
3170 return ram_load_dead(f
, opaque
);
3173 if (version_id
!= 3)
3177 addr
= qemu_get_be64(f
);
3179 flags
= addr
& ~TARGET_PAGE_MASK
;
3180 addr
&= TARGET_PAGE_MASK
;
3182 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3183 if (addr
!= phys_ram_size
)
3187 if (flags
& RAM_SAVE_FLAG_FULL
) {
3188 if (ram_load_dead(f
, opaque
) < 0)
3192 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3193 uint8_t ch
= qemu_get_byte(f
);
3194 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3195 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3196 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3197 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3202 void qemu_service_io(void)
3204 CPUState
*env
= cpu_single_env
;
3206 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3208 if (env
->kqemu_enabled
) {
3209 kqemu_cpu_interrupt(env
);
3215 /***********************************************************/
3216 /* bottom halves (can be seen as timers which expire ASAP) */
3227 static QEMUBH
*first_bh
= NULL
;
3229 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3232 bh
= qemu_mallocz(sizeof(QEMUBH
));
3236 bh
->opaque
= opaque
;
3237 bh
->next
= first_bh
;
3242 int qemu_bh_poll(void)
3248 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3249 if (!bh
->deleted
&& bh
->scheduled
) {
3258 /* remove deleted bhs */
3272 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3280 void qemu_bh_schedule(QEMUBH
*bh
)
3282 CPUState
*env
= cpu_single_env
;
3287 /* stop the currently executing CPU to execute the BH ASAP */
3289 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3293 void qemu_bh_cancel(QEMUBH
*bh
)
3298 void qemu_bh_delete(QEMUBH
*bh
)
3304 static void qemu_bh_update_timeout(int *timeout
)
3308 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3309 if (!bh
->deleted
&& bh
->scheduled
) {
3311 /* idle bottom halves will be polled at least
3313 *timeout
= MIN(10, *timeout
);
3315 /* non-idle bottom halves will be executed
3324 /***********************************************************/
3325 /* machine registration */
3327 static QEMUMachine
*first_machine
= NULL
;
3329 int qemu_register_machine(QEMUMachine
*m
)
3332 pm
= &first_machine
;
3340 static QEMUMachine
*find_machine(const char *name
)
3344 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3345 if (!strcmp(m
->name
, name
))
3351 /***********************************************************/
3352 /* main execution loop */
3354 static void gui_update(void *opaque
)
3356 DisplayState
*ds
= opaque
;
3357 ds
->dpy_refresh(ds
);
3358 qemu_mod_timer(ds
->gui_timer
,
3359 (ds
->gui_timer_interval
?
3360 ds
->gui_timer_interval
:
3361 GUI_REFRESH_INTERVAL
)
3362 + qemu_get_clock(rt_clock
));
3365 struct vm_change_state_entry
{
3366 VMChangeStateHandler
*cb
;
3368 LIST_ENTRY (vm_change_state_entry
) entries
;
3371 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3373 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3376 VMChangeStateEntry
*e
;
3378 e
= qemu_mallocz(sizeof (*e
));
3384 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3388 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3390 LIST_REMOVE (e
, entries
);
3394 static void vm_state_notify(int running
)
3396 VMChangeStateEntry
*e
;
3398 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3399 e
->cb(e
->opaque
, running
);
3403 /* XXX: support several handlers */
3404 static VMStopHandler
*vm_stop_cb
;
3405 static void *vm_stop_opaque
;
3407 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3410 vm_stop_opaque
= opaque
;
3414 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3425 qemu_rearm_alarm_timer(alarm_timer
);
3429 void vm_stop(int reason
)
3432 cpu_disable_ticks();
3436 vm_stop_cb(vm_stop_opaque
, reason
);
3443 /* reset/shutdown handler */
3445 typedef struct QEMUResetEntry
{
3446 QEMUResetHandler
*func
;
3448 struct QEMUResetEntry
*next
;
3451 static QEMUResetEntry
*first_reset_entry
;
3452 static int reset_requested
;
3453 static int shutdown_requested
;
3454 static int powerdown_requested
;
3456 int qemu_shutdown_requested(void)
3458 int r
= shutdown_requested
;
3459 shutdown_requested
= 0;
3463 int qemu_reset_requested(void)
3465 int r
= reset_requested
;
3466 reset_requested
= 0;
3470 int qemu_powerdown_requested(void)
3472 int r
= powerdown_requested
;
3473 powerdown_requested
= 0;
3477 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3479 QEMUResetEntry
**pre
, *re
;
3481 pre
= &first_reset_entry
;
3482 while (*pre
!= NULL
)
3483 pre
= &(*pre
)->next
;
3484 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3486 re
->opaque
= opaque
;
3491 void qemu_system_reset(void)
3495 /* reset all devices */
3496 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3497 re
->func(re
->opaque
);
3501 void qemu_system_reset_request(void)
3504 shutdown_requested
= 1;
3506 reset_requested
= 1;
3509 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3512 void qemu_system_shutdown_request(void)
3514 shutdown_requested
= 1;
3516 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3519 void qemu_system_powerdown_request(void)
3521 powerdown_requested
= 1;
3523 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3527 void host_main_loop_wait(int *timeout
)
3533 /* XXX: need to suppress polling by better using win32 events */
3535 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3536 ret
|= pe
->func(pe
->opaque
);
3540 WaitObjects
*w
= &wait_objects
;
3542 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3543 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3544 if (w
->func
[ret
- WAIT_OBJECT_0
])
3545 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3547 /* Check for additional signaled events */
3548 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3550 /* Check if event is signaled */
3551 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3552 if(ret2
== WAIT_OBJECT_0
) {
3554 w
->func
[i
](w
->opaque
[i
]);
3555 } else if (ret2
== WAIT_TIMEOUT
) {
3557 err
= GetLastError();
3558 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3561 } else if (ret
== WAIT_TIMEOUT
) {
3563 err
= GetLastError();
3564 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3571 void host_main_loop_wait(int *timeout
)
3576 void main_loop_wait(int timeout
)
3578 IOHandlerRecord
*ioh
;
3579 fd_set rfds
, wfds
, xfds
;
3583 qemu_bh_update_timeout(&timeout
);
3585 host_main_loop_wait(&timeout
);
3587 /* poll any events */
3588 /* XXX: separate device handlers from system ones */
3593 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3597 (!ioh
->fd_read_poll
||
3598 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3599 FD_SET(ioh
->fd
, &rfds
);
3603 if (ioh
->fd_write
) {
3604 FD_SET(ioh
->fd
, &wfds
);
3610 tv
.tv_sec
= timeout
/ 1000;
3611 tv
.tv_usec
= (timeout
% 1000) * 1000;
3613 #if defined(CONFIG_SLIRP)
3614 if (slirp_is_inited()) {
3615 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3618 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3620 IOHandlerRecord
**pioh
;
3622 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3623 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3624 ioh
->fd_read(ioh
->opaque
);
3626 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3627 ioh
->fd_write(ioh
->opaque
);
3631 /* remove deleted IO handlers */
3632 pioh
= &first_io_handler
;
3642 #if defined(CONFIG_SLIRP)
3643 if (slirp_is_inited()) {
3649 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3653 /* vm time timers */
3654 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3655 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3656 qemu_get_clock(vm_clock
));
3658 /* real time timers */
3659 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3660 qemu_get_clock(rt_clock
));
3662 /* Check bottom-halves last in case any of the earlier events triggered
3668 static int main_loop(void)
3671 #ifdef CONFIG_PROFILER
3676 cur_cpu
= first_cpu
;
3677 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3684 #ifdef CONFIG_PROFILER
3685 ti
= profile_getclock();
3690 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3691 env
->icount_decr
.u16
.low
= 0;
3692 env
->icount_extra
= 0;
3693 count
= qemu_next_deadline();
3694 count
= (count
+ (1 << icount_time_shift
) - 1)
3695 >> icount_time_shift
;
3696 qemu_icount
+= count
;
3697 decr
= (count
> 0xffff) ? 0xffff : count
;
3699 env
->icount_decr
.u16
.low
= decr
;
3700 env
->icount_extra
= count
;
3702 ret
= cpu_exec(env
);
3703 #ifdef CONFIG_PROFILER
3704 qemu_time
+= profile_getclock() - ti
;
3707 /* Fold pending instructions back into the
3708 instruction counter, and clear the interrupt flag. */
3709 qemu_icount
-= (env
->icount_decr
.u16
.low
3710 + env
->icount_extra
);
3711 env
->icount_decr
.u32
= 0;
3712 env
->icount_extra
= 0;
3714 next_cpu
= env
->next_cpu
?: first_cpu
;
3715 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3716 ret
= EXCP_INTERRUPT
;
3720 if (ret
== EXCP_HLT
) {
3721 /* Give the next CPU a chance to run. */
3725 if (ret
!= EXCP_HALTED
)
3727 /* all CPUs are halted ? */
3733 if (shutdown_requested
) {
3734 ret
= EXCP_INTERRUPT
;
3742 if (reset_requested
) {
3743 reset_requested
= 0;
3744 qemu_system_reset();
3745 ret
= EXCP_INTERRUPT
;
3747 if (powerdown_requested
) {
3748 powerdown_requested
= 0;
3749 qemu_system_powerdown();
3750 ret
= EXCP_INTERRUPT
;
3752 if (unlikely(ret
== EXCP_DEBUG
)) {
3753 gdb_set_stop_cpu(cur_cpu
);
3754 vm_stop(EXCP_DEBUG
);
3756 /* If all cpus are halted then wait until the next IRQ */
3757 /* XXX: use timeout computed from timers */
3758 if (ret
== EXCP_HALTED
) {
3762 /* Advance virtual time to the next event. */
3763 if (use_icount
== 1) {
3764 /* When not using an adaptive execution frequency
3765 we tend to get badly out of sync with real time,
3766 so just delay for a reasonable amount of time. */
3769 delta
= cpu_get_icount() - cpu_get_clock();
3772 /* If virtual time is ahead of real time then just
3774 timeout
= (delta
/ 1000000) + 1;
3776 /* Wait for either IO to occur or the next
3778 add
= qemu_next_deadline();
3779 /* We advance the timer before checking for IO.
3780 Limit the amount we advance so that early IO
3781 activity won't get the guest too far ahead. */
3785 add
= (add
+ (1 << icount_time_shift
) - 1)
3786 >> icount_time_shift
;
3788 timeout
= delta
/ 1000000;
3799 if (shutdown_requested
) {
3800 ret
= EXCP_INTERRUPT
;
3805 #ifdef CONFIG_PROFILER
3806 ti
= profile_getclock();
3808 main_loop_wait(timeout
);
3809 #ifdef CONFIG_PROFILER
3810 dev_time
+= profile_getclock() - ti
;
3813 cpu_disable_ticks();
3817 static void help(int exitcode
)
3819 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3820 "usage: %s [options] [disk_image]\n"
3822 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3824 "Standard options:\n"
3825 "-M machine select emulated machine (-M ? for list)\n"
3826 "-cpu cpu select CPU (-cpu ? for list)\n"
3827 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3828 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3829 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3830 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3831 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3832 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3833 " [,cache=writethrough|writeback|none][,format=f]\n"
3834 " use 'file' as a drive image\n"
3835 "-mtdblock file use 'file' as on-board Flash memory image\n"
3836 "-sd file use 'file' as SecureDigital card image\n"
3837 "-pflash file use 'file' as a parallel flash image\n"
3838 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3839 "-snapshot write to temporary files instead of disk image files\n"
3841 "-no-frame open SDL window without a frame and window decorations\n"
3842 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3843 "-no-quit disable SDL window close capability\n"
3846 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3848 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3849 "-smp n set the number of CPUs to 'n' [default=1]\n"
3850 "-nographic disable graphical output and redirect serial I/Os to console\n"
3851 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3853 "-k language use keyboard layout (for example \"fr\" for French)\n"
3856 "-audio-help print list of audio drivers and their options\n"
3857 "-soundhw c1,... enable audio support\n"
3858 " and only specified sound cards (comma separated list)\n"
3859 " use -soundhw ? to get the list of supported cards\n"
3860 " use -soundhw all to enable all of them\n"
3862 "-vga [std|cirrus|vmware]\n"
3863 " select video card type\n"
3864 "-localtime set the real time clock to local time [default=utc]\n"
3865 "-full-screen start in full screen\n"
3867 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3869 "-usb enable the USB driver (will be the default soon)\n"
3870 "-usbdevice name add the host or guest USB device 'name'\n"
3871 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3872 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3874 "-name string set the name of the guest\n"
3875 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3877 "Network options:\n"
3878 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
3879 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3881 "-net user[,vlan=n][,hostname=host]\n"
3882 " connect the user mode network stack to VLAN 'n' and send\n"
3883 " hostname 'host' to DHCP clients\n"
3886 "-net tap[,vlan=n],ifname=name\n"
3887 " connect the host TAP network interface to VLAN 'n'\n"
3889 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3890 " connect the host TAP network interface to VLAN 'n' and use the\n"
3891 " network scripts 'file' (default=%s)\n"
3892 " and 'dfile' (default=%s);\n"
3893 " use '[down]script=no' to disable script execution;\n"
3894 " use 'fd=h' to connect to an already opened TAP interface\n"
3896 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3897 " connect the vlan 'n' to another VLAN using a socket connection\n"
3898 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
3899 " connect the vlan 'n' to multicast maddr and port\n"
3901 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3902 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3903 " on host and listening for incoming connections on 'socketpath'.\n"
3904 " Use group 'groupname' and mode 'octalmode' to change default\n"
3905 " ownership and permissions for communication port.\n"
3907 "-net none use it alone to have zero network devices; if no -net option\n"
3908 " is provided, the default is '-net nic -net user'\n"
3910 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3911 "-bt hci,host[:id]\n"
3912 " Use host's HCI with the given name\n"
3913 "-bt hci[,vlan=n]\n"
3914 " Emulate a standard HCI in virtual scatternet 'n'\n"
3915 "-bt vhci[,vlan=n]\n"
3916 " Add host computer to virtual scatternet 'n' using VHCI\n"
3917 "-bt device:dev[,vlan=n]\n"
3918 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3921 "-tftp dir allow tftp access to files in dir [-net user]\n"
3922 "-bootp file advertise file in BOOTP replies\n"
3924 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3926 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3927 " redirect TCP or UDP connections from host to guest [-net user]\n"
3930 "Linux boot specific:\n"
3931 "-kernel bzImage use 'bzImage' as kernel image\n"
3932 "-append cmdline use 'cmdline' as kernel command line\n"
3933 "-initrd file use 'file' as initial ram disk\n"
3935 "Debug/Expert options:\n"
3936 "-monitor dev redirect the monitor to char device 'dev'\n"
3937 "-serial dev redirect the serial port to char device 'dev'\n"
3938 "-parallel dev redirect the parallel port to char device 'dev'\n"
3939 "-pidfile file Write PID to 'file'\n"
3940 "-S freeze CPU at startup (use 'c' to start execution)\n"
3941 "-s wait gdb connection to port\n"
3942 "-p port set gdb connection port [default=%s]\n"
3943 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3944 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3945 " translation (t=none or lba) (usually qemu can guess them)\n"
3946 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3948 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3949 "-no-kqemu disable KQEMU kernel module usage\n"
3952 "-enable-kvm enable KVM full virtualization support\n"
3955 "-no-acpi disable ACPI\n"
3957 #ifdef CONFIG_CURSES
3958 "-curses use a curses/ncurses interface instead of SDL\n"
3960 "-no-reboot exit instead of rebooting\n"
3961 "-no-shutdown stop before shutdown\n"
3962 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3963 "-vnc display start a VNC server on display\n"
3965 "-daemonize daemonize QEMU after initializing\n"
3967 "-option-rom rom load a file, rom, into the option ROM space\n"
3969 "-prom-env variable=value set OpenBIOS nvram variables\n"
3971 "-clock force the use of the given methods for timer alarm.\n"
3972 " To see what timers are available use -clock ?\n"
3973 "-startdate select initial date of the clock\n"
3974 "-icount [N|auto]\n"
3975 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
3977 "During emulation, the following keys are useful:\n"
3978 "ctrl-alt-f toggle full screen\n"
3979 "ctrl-alt-n switch to virtual console 'n'\n"
3980 "ctrl-alt toggle mouse and keyboard grab\n"
3982 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3987 DEFAULT_NETWORK_SCRIPT
,
3988 DEFAULT_NETWORK_DOWN_SCRIPT
,
3990 DEFAULT_GDBSTUB_PORT
,
3995 #define HAS_ARG 0x0001
4010 QEMU_OPTION_mtdblock
,
4014 QEMU_OPTION_snapshot
,
4016 QEMU_OPTION_no_fd_bootchk
,
4019 QEMU_OPTION_nographic
,
4020 QEMU_OPTION_portrait
,
4022 QEMU_OPTION_audio_help
,
4023 QEMU_OPTION_soundhw
,
4045 QEMU_OPTION_localtime
,
4049 QEMU_OPTION_monitor
,
4051 QEMU_OPTION_parallel
,
4053 QEMU_OPTION_full_screen
,
4054 QEMU_OPTION_no_frame
,
4055 QEMU_OPTION_alt_grab
,
4056 QEMU_OPTION_no_quit
,
4057 QEMU_OPTION_pidfile
,
4058 QEMU_OPTION_no_kqemu
,
4059 QEMU_OPTION_kernel_kqemu
,
4060 QEMU_OPTION_enable_kvm
,
4061 QEMU_OPTION_win2k_hack
,
4063 QEMU_OPTION_usbdevice
,
4066 QEMU_OPTION_no_acpi
,
4068 QEMU_OPTION_no_reboot
,
4069 QEMU_OPTION_no_shutdown
,
4070 QEMU_OPTION_show_cursor
,
4071 QEMU_OPTION_daemonize
,
4072 QEMU_OPTION_option_rom
,
4073 QEMU_OPTION_semihosting
,
4075 QEMU_OPTION_prom_env
,
4076 QEMU_OPTION_old_param
,
4078 QEMU_OPTION_startdate
,
4079 QEMU_OPTION_tb_size
,
4082 QEMU_OPTION_incoming
,
4085 typedef struct QEMUOption
{
4091 static const QEMUOption qemu_options
[] = {
4092 { "h", 0, QEMU_OPTION_h
},
4093 { "help", 0, QEMU_OPTION_h
},
4095 { "M", HAS_ARG
, QEMU_OPTION_M
},
4096 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4097 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4098 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4099 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4100 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4101 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4102 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4103 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4104 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4105 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4106 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4107 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4108 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4109 { "snapshot", 0, QEMU_OPTION_snapshot
},
4111 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4113 { "m", HAS_ARG
, QEMU_OPTION_m
},
4114 { "nographic", 0, QEMU_OPTION_nographic
},
4115 { "portrait", 0, QEMU_OPTION_portrait
},
4116 { "k", HAS_ARG
, QEMU_OPTION_k
},
4118 { "audio-help", 0, QEMU_OPTION_audio_help
},
4119 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4122 { "net", HAS_ARG
, QEMU_OPTION_net
},
4124 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4125 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4127 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4129 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4131 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4133 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4134 { "append", HAS_ARG
, QEMU_OPTION_append
},
4135 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4137 { "S", 0, QEMU_OPTION_S
},
4138 { "s", 0, QEMU_OPTION_s
},
4139 { "p", HAS_ARG
, QEMU_OPTION_p
},
4140 { "d", HAS_ARG
, QEMU_OPTION_d
},
4141 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4142 { "L", HAS_ARG
, QEMU_OPTION_L
},
4143 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4145 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4146 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4149 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4151 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4152 { "g", 1, QEMU_OPTION_g
},
4154 { "localtime", 0, QEMU_OPTION_localtime
},
4155 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4156 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4157 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4158 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4159 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4160 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4161 { "full-screen", 0, QEMU_OPTION_full_screen
},
4163 { "no-frame", 0, QEMU_OPTION_no_frame
},
4164 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4165 { "no-quit", 0, QEMU_OPTION_no_quit
},
4167 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4168 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4169 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4170 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4171 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4172 #ifdef CONFIG_CURSES
4173 { "curses", 0, QEMU_OPTION_curses
},
4175 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4177 /* temporary options */
4178 { "usb", 0, QEMU_OPTION_usb
},
4179 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4180 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4181 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4182 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4183 { "daemonize", 0, QEMU_OPTION_daemonize
},
4184 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4185 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4186 { "semihosting", 0, QEMU_OPTION_semihosting
},
4188 { "name", HAS_ARG
, QEMU_OPTION_name
},
4189 #if defined(TARGET_SPARC)
4190 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4192 #if defined(TARGET_ARM)
4193 { "old-param", 0, QEMU_OPTION_old_param
},
4195 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4196 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4197 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4198 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4199 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4203 /* password input */
4205 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4210 if (!bdrv_is_encrypted(bs
))
4213 term_printf("%s is encrypted.\n", name
);
4214 for(i
= 0; i
< 3; i
++) {
4215 monitor_readline("Password: ", 1, password
, sizeof(password
));
4216 if (bdrv_set_key(bs
, password
) == 0)
4218 term_printf("invalid password\n");
4223 static BlockDriverState
*get_bdrv(int index
)
4225 if (index
> nb_drives
)
4227 return drives_table
[index
].bdrv
;
4230 static void read_passwords(void)
4232 BlockDriverState
*bs
;
4235 for(i
= 0; i
< 6; i
++) {
4238 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4243 struct soundhw soundhw
[] = {
4244 #ifdef HAS_AUDIO_CHOICE
4245 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4251 { .init_isa
= pcspk_audio_init
}
4256 "Creative Sound Blaster 16",
4259 { .init_isa
= SB16_init
}
4262 #ifdef CONFIG_CS4231A
4268 { .init_isa
= cs4231a_init
}
4276 "Yamaha YMF262 (OPL3)",
4278 "Yamaha YM3812 (OPL2)",
4282 { .init_isa
= Adlib_init
}
4289 "Gravis Ultrasound GF1",
4292 { .init_isa
= GUS_init
}
4299 "Intel 82801AA AC97 Audio",
4302 { .init_pci
= ac97_init
}
4308 "ENSONIQ AudioPCI ES1370",
4311 { .init_pci
= es1370_init
}
4315 { NULL
, NULL
, 0, 0, { NULL
} }
4318 static void select_soundhw (const char *optarg
)
4322 if (*optarg
== '?') {
4325 printf ("Valid sound card names (comma separated):\n");
4326 for (c
= soundhw
; c
->name
; ++c
) {
4327 printf ("%-11s %s\n", c
->name
, c
->descr
);
4329 printf ("\n-soundhw all will enable all of the above\n");
4330 exit (*optarg
!= '?');
4338 if (!strcmp (optarg
, "all")) {
4339 for (c
= soundhw
; c
->name
; ++c
) {
4347 e
= strchr (p
, ',');
4348 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4350 for (c
= soundhw
; c
->name
; ++c
) {
4351 if (!strncmp (c
->name
, p
, l
)) {
4360 "Unknown sound card name (too big to show)\n");
4363 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4368 p
+= l
+ (e
!= NULL
);
4372 goto show_valid_cards
;
4377 static void select_vgahw (const char *p
)
4381 if (strstart(p
, "std", &opts
)) {
4382 cirrus_vga_enabled
= 0;
4384 } else if (strstart(p
, "cirrus", &opts
)) {
4385 cirrus_vga_enabled
= 1;
4387 } else if (strstart(p
, "vmware", &opts
)) {
4388 cirrus_vga_enabled
= 0;
4392 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4396 const char *nextopt
;
4398 if (strstart(opts
, ",retrace=", &nextopt
)) {
4400 if (strstart(opts
, "dumb", &nextopt
))
4401 vga_retrace_method
= VGA_RETRACE_DUMB
;
4402 else if (strstart(opts
, "precise", &nextopt
))
4403 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4404 else goto invalid_vga
;
4405 } else goto invalid_vga
;
4411 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4413 exit(STATUS_CONTROL_C_EXIT
);
4418 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4422 if(strlen(str
) != 36)
4425 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4426 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4427 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4435 #define MAX_NET_CLIENTS 32
4439 static void termsig_handler(int signal
)
4441 qemu_system_shutdown_request();
4444 static void termsig_setup(void)
4446 struct sigaction act
;
4448 memset(&act
, 0, sizeof(act
));
4449 act
.sa_handler
= termsig_handler
;
4450 sigaction(SIGINT
, &act
, NULL
);
4451 sigaction(SIGHUP
, &act
, NULL
);
4452 sigaction(SIGTERM
, &act
, NULL
);
4457 int main(int argc
, char **argv
)
4459 #ifdef CONFIG_GDBSTUB
4461 const char *gdbstub_port
;
4463 uint32_t boot_devices_bitmap
= 0;
4465 int snapshot
, linux_boot
, net_boot
;
4466 const char *initrd_filename
;
4467 const char *kernel_filename
, *kernel_cmdline
;
4468 const char *boot_devices
= "";
4469 DisplayState
*ds
= &display_state
;
4470 int cyls
, heads
, secs
, translation
;
4471 const char *net_clients
[MAX_NET_CLIENTS
];
4473 const char *bt_opts
[MAX_BT_CMDLINE
];
4477 const char *r
, *optarg
;
4478 CharDriverState
*monitor_hd
;
4479 const char *monitor_device
;
4480 const char *serial_devices
[MAX_SERIAL_PORTS
];
4481 int serial_device_index
;
4482 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4483 int parallel_device_index
;
4484 const char *loadvm
= NULL
;
4485 QEMUMachine
*machine
;
4486 const char *cpu_model
;
4487 const char *usb_devices
[MAX_USB_CMDLINE
];
4488 int usb_devices_index
;
4491 const char *pid_file
= NULL
;
4493 const char *incoming
= NULL
;
4495 LIST_INIT (&vm_change_state_head
);
4498 struct sigaction act
;
4499 sigfillset(&act
.sa_mask
);
4501 act
.sa_handler
= SIG_IGN
;
4502 sigaction(SIGPIPE
, &act
, NULL
);
4505 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4506 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4507 QEMU to run on a single CPU */
4512 h
= GetCurrentProcess();
4513 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4514 for(i
= 0; i
< 32; i
++) {
4515 if (mask
& (1 << i
))
4520 SetProcessAffinityMask(h
, mask
);
4526 register_machines();
4527 machine
= first_machine
;
4529 initrd_filename
= NULL
;
4531 vga_ram_size
= VGA_RAM_SIZE
;
4532 #ifdef CONFIG_GDBSTUB
4534 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4539 kernel_filename
= NULL
;
4540 kernel_cmdline
= "";
4541 cyls
= heads
= secs
= 0;
4542 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4543 monitor_device
= "vc";
4545 serial_devices
[0] = "vc:80Cx24C";
4546 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4547 serial_devices
[i
] = NULL
;
4548 serial_device_index
= 0;
4550 parallel_devices
[0] = "vc:640x480";
4551 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4552 parallel_devices
[i
] = NULL
;
4553 parallel_device_index
= 0;
4555 usb_devices_index
= 0;
4574 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4576 const QEMUOption
*popt
;
4579 /* Treat --foo the same as -foo. */
4582 popt
= qemu_options
;
4585 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4589 if (!strcmp(popt
->name
, r
+ 1))
4593 if (popt
->flags
& HAS_ARG
) {
4594 if (optind
>= argc
) {
4595 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4599 optarg
= argv
[optind
++];
4604 switch(popt
->index
) {
4606 machine
= find_machine(optarg
);
4609 printf("Supported machines are:\n");
4610 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4611 printf("%-10s %s%s\n",
4613 m
== first_machine
? " (default)" : "");
4615 exit(*optarg
!= '?');
4618 case QEMU_OPTION_cpu
:
4619 /* hw initialization will check this */
4620 if (*optarg
== '?') {
4621 /* XXX: implement xxx_cpu_list for targets that still miss it */
4622 #if defined(cpu_list)
4623 cpu_list(stdout
, &fprintf
);
4630 case QEMU_OPTION_initrd
:
4631 initrd_filename
= optarg
;
4633 case QEMU_OPTION_hda
:
4635 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4637 hda_index
= drive_add(optarg
, HD_ALIAS
4638 ",cyls=%d,heads=%d,secs=%d%s",
4639 0, cyls
, heads
, secs
,
4640 translation
== BIOS_ATA_TRANSLATION_LBA
?
4642 translation
== BIOS_ATA_TRANSLATION_NONE
?
4643 ",trans=none" : "");
4645 case QEMU_OPTION_hdb
:
4646 case QEMU_OPTION_hdc
:
4647 case QEMU_OPTION_hdd
:
4648 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4650 case QEMU_OPTION_drive
:
4651 drive_add(NULL
, "%s", optarg
);
4653 case QEMU_OPTION_mtdblock
:
4654 drive_add(optarg
, MTD_ALIAS
);
4656 case QEMU_OPTION_sd
:
4657 drive_add(optarg
, SD_ALIAS
);
4659 case QEMU_OPTION_pflash
:
4660 drive_add(optarg
, PFLASH_ALIAS
);
4662 case QEMU_OPTION_snapshot
:
4665 case QEMU_OPTION_hdachs
:
4669 cyls
= strtol(p
, (char **)&p
, 0);
4670 if (cyls
< 1 || cyls
> 16383)
4675 heads
= strtol(p
, (char **)&p
, 0);
4676 if (heads
< 1 || heads
> 16)
4681 secs
= strtol(p
, (char **)&p
, 0);
4682 if (secs
< 1 || secs
> 63)
4686 if (!strcmp(p
, "none"))
4687 translation
= BIOS_ATA_TRANSLATION_NONE
;
4688 else if (!strcmp(p
, "lba"))
4689 translation
= BIOS_ATA_TRANSLATION_LBA
;
4690 else if (!strcmp(p
, "auto"))
4691 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4694 } else if (*p
!= '\0') {
4696 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4699 if (hda_index
!= -1)
4700 snprintf(drives_opt
[hda_index
].opt
,
4701 sizeof(drives_opt
[hda_index
].opt
),
4702 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4703 0, cyls
, heads
, secs
,
4704 translation
== BIOS_ATA_TRANSLATION_LBA
?
4706 translation
== BIOS_ATA_TRANSLATION_NONE
?
4707 ",trans=none" : "");
4710 case QEMU_OPTION_nographic
:
4713 #ifdef CONFIG_CURSES
4714 case QEMU_OPTION_curses
:
4718 case QEMU_OPTION_portrait
:
4721 case QEMU_OPTION_kernel
:
4722 kernel_filename
= optarg
;
4724 case QEMU_OPTION_append
:
4725 kernel_cmdline
= optarg
;
4727 case QEMU_OPTION_cdrom
:
4728 drive_add(optarg
, CDROM_ALIAS
);
4730 case QEMU_OPTION_boot
:
4731 boot_devices
= optarg
;
4732 /* We just do some generic consistency checks */
4734 /* Could easily be extended to 64 devices if needed */
4737 boot_devices_bitmap
= 0;
4738 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4739 /* Allowed boot devices are:
4740 * a b : floppy disk drives
4741 * c ... f : IDE disk drives
4742 * g ... m : machine implementation dependant drives
4743 * n ... p : network devices
4744 * It's up to each machine implementation to check
4745 * if the given boot devices match the actual hardware
4746 * implementation and firmware features.
4748 if (*p
< 'a' || *p
> 'q') {
4749 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4752 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4754 "Boot device '%c' was given twice\n",*p
);
4757 boot_devices_bitmap
|= 1 << (*p
- 'a');
4761 case QEMU_OPTION_fda
:
4762 case QEMU_OPTION_fdb
:
4763 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4766 case QEMU_OPTION_no_fd_bootchk
:
4770 case QEMU_OPTION_net
:
4771 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4772 fprintf(stderr
, "qemu: too many network clients\n");
4775 net_clients
[nb_net_clients
] = optarg
;
4779 case QEMU_OPTION_tftp
:
4780 tftp_prefix
= optarg
;
4782 case QEMU_OPTION_bootp
:
4783 bootp_filename
= optarg
;
4786 case QEMU_OPTION_smb
:
4787 net_slirp_smb(optarg
);
4790 case QEMU_OPTION_redir
:
4791 net_slirp_redir(optarg
);
4794 case QEMU_OPTION_bt
:
4795 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4796 fprintf(stderr
, "qemu: too many bluetooth options\n");
4799 bt_opts
[nb_bt_opts
++] = optarg
;
4802 case QEMU_OPTION_audio_help
:
4806 case QEMU_OPTION_soundhw
:
4807 select_soundhw (optarg
);
4813 case QEMU_OPTION_m
: {
4817 value
= strtoul(optarg
, &ptr
, 10);
4819 case 0: case 'M': case 'm':
4826 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4830 /* On 32-bit hosts, QEMU is limited by virtual address space */
4831 if (value
> (2047 << 20)
4833 && HOST_LONG_BITS
== 32
4836 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4839 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4840 fprintf(stderr
, "qemu: ram size too large\n");
4849 const CPULogItem
*item
;
4851 mask
= cpu_str_to_log_mask(optarg
);
4853 printf("Log items (comma separated):\n");
4854 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4855 printf("%-10s %s\n", item
->name
, item
->help
);
4862 #ifdef CONFIG_GDBSTUB
4867 gdbstub_port
= optarg
;
4873 case QEMU_OPTION_bios
:
4880 keyboard_layout
= optarg
;
4882 case QEMU_OPTION_localtime
:
4885 case QEMU_OPTION_vga
:
4886 select_vgahw (optarg
);
4893 w
= strtol(p
, (char **)&p
, 10);
4896 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4902 h
= strtol(p
, (char **)&p
, 10);
4907 depth
= strtol(p
, (char **)&p
, 10);
4908 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4909 depth
!= 24 && depth
!= 32)
4911 } else if (*p
== '\0') {
4912 depth
= graphic_depth
;
4919 graphic_depth
= depth
;
4922 case QEMU_OPTION_echr
:
4925 term_escape_char
= strtol(optarg
, &r
, 0);
4927 printf("Bad argument to echr\n");
4930 case QEMU_OPTION_monitor
:
4931 monitor_device
= optarg
;
4933 case QEMU_OPTION_serial
:
4934 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4935 fprintf(stderr
, "qemu: too many serial ports\n");
4938 serial_devices
[serial_device_index
] = optarg
;
4939 serial_device_index
++;
4941 case QEMU_OPTION_parallel
:
4942 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4943 fprintf(stderr
, "qemu: too many parallel ports\n");
4946 parallel_devices
[parallel_device_index
] = optarg
;
4947 parallel_device_index
++;
4949 case QEMU_OPTION_loadvm
:
4952 case QEMU_OPTION_full_screen
:
4956 case QEMU_OPTION_no_frame
:
4959 case QEMU_OPTION_alt_grab
:
4962 case QEMU_OPTION_no_quit
:
4966 case QEMU_OPTION_pidfile
:
4970 case QEMU_OPTION_win2k_hack
:
4971 win2k_install_hack
= 1;
4975 case QEMU_OPTION_no_kqemu
:
4978 case QEMU_OPTION_kernel_kqemu
:
4983 case QEMU_OPTION_enable_kvm
:
4990 case QEMU_OPTION_usb
:
4993 case QEMU_OPTION_usbdevice
:
4995 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
4996 fprintf(stderr
, "Too many USB devices\n");
4999 usb_devices
[usb_devices_index
] = optarg
;
5000 usb_devices_index
++;
5002 case QEMU_OPTION_smp
:
5003 smp_cpus
= atoi(optarg
);
5005 fprintf(stderr
, "Invalid number of CPUs\n");
5009 case QEMU_OPTION_vnc
:
5010 vnc_display
= optarg
;
5012 case QEMU_OPTION_no_acpi
:
5015 case QEMU_OPTION_no_reboot
:
5018 case QEMU_OPTION_no_shutdown
:
5021 case QEMU_OPTION_show_cursor
:
5024 case QEMU_OPTION_uuid
:
5025 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5026 fprintf(stderr
, "Fail to parse UUID string."
5027 " Wrong format.\n");
5031 case QEMU_OPTION_daemonize
:
5034 case QEMU_OPTION_option_rom
:
5035 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5036 fprintf(stderr
, "Too many option ROMs\n");
5039 option_rom
[nb_option_roms
] = optarg
;
5042 case QEMU_OPTION_semihosting
:
5043 semihosting_enabled
= 1;
5045 case QEMU_OPTION_name
:
5049 case QEMU_OPTION_prom_env
:
5050 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5051 fprintf(stderr
, "Too many prom variables\n");
5054 prom_envs
[nb_prom_envs
] = optarg
;
5059 case QEMU_OPTION_old_param
:
5063 case QEMU_OPTION_clock
:
5064 configure_alarms(optarg
);
5066 case QEMU_OPTION_startdate
:
5069 time_t rtc_start_date
;
5070 if (!strcmp(optarg
, "now")) {
5071 rtc_date_offset
= -1;
5073 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5081 } else if (sscanf(optarg
, "%d-%d-%d",
5084 &tm
.tm_mday
) == 3) {
5093 rtc_start_date
= mktimegm(&tm
);
5094 if (rtc_start_date
== -1) {
5096 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5097 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5100 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5104 case QEMU_OPTION_tb_size
:
5105 tb_size
= strtol(optarg
, NULL
, 0);
5109 case QEMU_OPTION_icount
:
5111 if (strcmp(optarg
, "auto") == 0) {
5112 icount_time_shift
= -1;
5114 icount_time_shift
= strtol(optarg
, NULL
, 0);
5117 case QEMU_OPTION_incoming
:
5124 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5125 if (kvm_allowed
&& kqemu_allowed
) {
5127 "You can not enable both KVM and kqemu at the same time\n");
5132 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5133 if (smp_cpus
> machine
->max_cpus
) {
5134 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5135 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5141 if (serial_device_index
== 0)
5142 serial_devices
[0] = "stdio";
5143 if (parallel_device_index
== 0)
5144 parallel_devices
[0] = "null";
5145 if (strncmp(monitor_device
, "vc", 2) == 0)
5146 monitor_device
= "stdio";
5153 if (pipe(fds
) == -1)
5164 len
= read(fds
[0], &status
, 1);
5165 if (len
== -1 && (errno
== EINTR
))
5170 else if (status
== 1) {
5171 fprintf(stderr
, "Could not acquire pidfile\n");
5188 signal(SIGTSTP
, SIG_IGN
);
5189 signal(SIGTTOU
, SIG_IGN
);
5190 signal(SIGTTIN
, SIG_IGN
);
5194 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5197 write(fds
[1], &status
, 1);
5199 fprintf(stderr
, "Could not acquire pid file\n");
5207 linux_boot
= (kernel_filename
!= NULL
);
5208 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5210 if (!linux_boot
&& net_boot
== 0 &&
5211 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5214 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5215 fprintf(stderr
, "-append only allowed with -kernel option\n");
5219 if (!linux_boot
&& initrd_filename
!= NULL
) {
5220 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5224 /* boot to floppy or the default cd if no hard disk defined yet */
5225 if (!boot_devices
[0]) {
5226 boot_devices
= "cad";
5228 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5231 if (init_timer_alarm() < 0) {
5232 fprintf(stderr
, "could not initialize alarm timer\n");
5235 if (use_icount
&& icount_time_shift
< 0) {
5237 /* 125MIPS seems a reasonable initial guess at the guest speed.
5238 It will be corrected fairly quickly anyway. */
5239 icount_time_shift
= 3;
5240 init_icount_adjust();
5247 /* init network clients */
5248 if (nb_net_clients
== 0) {
5249 /* if no clients, we use a default config */
5250 net_clients
[nb_net_clients
++] = "nic";
5252 net_clients
[nb_net_clients
++] = "user";
5256 for(i
= 0;i
< nb_net_clients
; i
++) {
5257 if (net_client_parse(net_clients
[i
]) < 0)
5263 /* XXX: this should be moved in the PC machine instantiation code */
5264 if (net_boot
!= 0) {
5266 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5267 const char *model
= nd_table
[i
].model
;
5269 if (net_boot
& (1 << i
)) {
5272 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5273 if (get_image_size(buf
) > 0) {
5274 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5275 fprintf(stderr
, "Too many option ROMs\n");
5278 option_rom
[nb_option_roms
] = strdup(buf
);
5285 fprintf(stderr
, "No valid PXE rom found for network device\n");
5291 /* init the bluetooth world */
5292 for (i
= 0; i
< nb_bt_opts
; i
++)
5293 if (bt_parse(bt_opts
[i
]))
5296 /* init the memory */
5297 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5299 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5301 if (ram_size
< phys_ram_size
) {
5302 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5303 machine
->name
, (unsigned long long) phys_ram_size
);
5307 phys_ram_size
= ram_size
;
5309 ram_size
= phys_ram_size
;
5312 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5314 phys_ram_size
+= ram_size
;
5317 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5318 if (!phys_ram_base
) {
5319 fprintf(stderr
, "Could not allocate physical memory\n");
5323 /* init the dynamic translator */
5324 cpu_exec_init_all(tb_size
* 1024 * 1024);
5328 /* we always create the cdrom drive, even if no disk is there */
5330 if (nb_drives_opt
< MAX_DRIVES
)
5331 drive_add(NULL
, CDROM_ALIAS
);
5333 /* we always create at least one floppy */
5335 if (nb_drives_opt
< MAX_DRIVES
)
5336 drive_add(NULL
, FD_ALIAS
, 0);
5338 /* we always create one sd slot, even if no card is in it */
5340 if (nb_drives_opt
< MAX_DRIVES
)
5341 drive_add(NULL
, SD_ALIAS
);
5343 /* open the virtual block devices */
5345 for(i
= 0; i
< nb_drives_opt
; i
++)
5346 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5349 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5350 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5353 memset(&display_state
, 0, sizeof(display_state
));
5356 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5359 /* nearly nothing to do */
5360 dumb_display_init(ds
);
5361 } else if (vnc_display
!= NULL
) {
5362 vnc_display_init(ds
);
5363 if (vnc_display_open(ds
, vnc_display
) < 0)
5366 #if defined(CONFIG_CURSES)
5368 curses_display_init(ds
, full_screen
);
5372 #if defined(CONFIG_SDL)
5373 sdl_display_init(ds
, full_screen
, no_frame
);
5374 #elif defined(CONFIG_COCOA)
5375 cocoa_display_init(ds
, full_screen
);
5377 dumb_display_init(ds
);
5382 /* must be after terminal init, SDL library changes signal handlers */
5386 /* Maintain compatibility with multiple stdio monitors */
5387 if (!strcmp(monitor_device
,"stdio")) {
5388 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5389 const char *devname
= serial_devices
[i
];
5390 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5391 monitor_device
= NULL
;
5393 } else if (devname
&& !strcmp(devname
,"stdio")) {
5394 monitor_device
= NULL
;
5395 serial_devices
[i
] = "mon:stdio";
5400 if (monitor_device
) {
5401 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5403 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5406 monitor_init(monitor_hd
, !nographic
);
5409 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5410 const char *devname
= serial_devices
[i
];
5411 if (devname
&& strcmp(devname
, "none")) {
5413 snprintf(label
, sizeof(label
), "serial%d", i
);
5414 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5415 if (!serial_hds
[i
]) {
5416 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5420 if (strstart(devname
, "vc", 0))
5421 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5425 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5426 const char *devname
= parallel_devices
[i
];
5427 if (devname
&& strcmp(devname
, "none")) {
5429 snprintf(label
, sizeof(label
), "parallel%d", i
);
5430 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5431 if (!parallel_hds
[i
]) {
5432 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5436 if (strstart(devname
, "vc", 0))
5437 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5441 if (kvm_enabled()) {
5444 ret
= kvm_init(smp_cpus
);
5446 fprintf(stderr
, "failed to initialize KVM\n");
5451 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5452 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5454 /* init USB devices */
5456 for(i
= 0; i
< usb_devices_index
; i
++) {
5457 if (usb_device_add(usb_devices
[i
]) < 0) {
5458 fprintf(stderr
, "Warning: could not add USB device %s\n",
5464 if (display_state
.dpy_refresh
) {
5465 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5466 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5469 #ifdef CONFIG_GDBSTUB
5471 /* XXX: use standard host:port notation and modify options
5473 if (gdbserver_start(gdbstub_port
) < 0) {
5474 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5485 autostart
= 0; /* fixme how to deal with -daemonize */
5486 qemu_start_incoming_migration(incoming
);
5490 /* XXX: simplify init */
5503 len
= write(fds
[1], &status
, 1);
5504 if (len
== -1 && (errno
== EINTR
))
5511 TFR(fd
= open("/dev/null", O_RDWR
));