4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
39 #include "cache-utils.h"
41 #include "audio/audio.h"
42 #include "migration.h"
55 #include <sys/times.h>
59 #include <sys/ioctl.h>
60 #include <sys/resource.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
64 #if defined(__NetBSD__)
65 #include <net/if_tap.h>
68 #include <linux/if_tun.h>
70 #include <arpa/inet.h>
73 #include <sys/select.h>
81 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82 #include <freebsd/stdlib.h>
87 #include <linux/rtc.h>
89 /* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91 /* #include <linux/hpet.h> */
94 #include <linux/ppdev.h>
95 #include <linux/parport.h>
99 #include <sys/ethernet.h>
100 #include <sys/sockio.h>
101 #include <netinet/arp.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_icmp.h> // must come after ip.h
106 #include <netinet/udp.h>
107 #include <netinet/tcp.h>
115 #include "qemu_socket.h"
117 #if defined(CONFIG_SLIRP)
118 #include "libslirp.h"
121 #if defined(__OpenBSD__)
125 #if defined(CONFIG_VDE)
126 #include <libvdeplug.h>
131 #include <sys/timeb.h>
132 #include <mmsystem.h>
133 #define getopt_long_only getopt_long
134 #define memalign(align, size) malloc(size)
141 #endif /* CONFIG_SDL */
145 #define main qemu_main
146 #endif /* CONFIG_COCOA */
150 #include "exec-all.h"
152 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
153 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
155 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
157 #define SMBD_COMMAND "/usr/sbin/smbd"
160 //#define DEBUG_UNUSED_IOPORT
161 //#define DEBUG_IOPORT
163 //#define DEBUG_SLIRP
166 #define DEFAULT_RAM_SIZE 144
168 #define DEFAULT_RAM_SIZE 128
171 /* Max number of USB devices that can be specified on the commandline. */
172 #define MAX_USB_CMDLINE 8
174 /* Max number of bluetooth switches on the commandline. */
175 #define MAX_BT_CMDLINE 10
177 /* XXX: use a two level table to limit memory usage */
178 #define MAX_IOPORTS 65536
180 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
181 const char *bios_name
= NULL
;
182 static void *ioport_opaque
[MAX_IOPORTS
];
183 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
184 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
185 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
186 to store the VM snapshots */
187 DriveInfo drives_table
[MAX_DRIVES
+1];
189 static int vga_ram_size
;
190 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
191 DisplayState display_state
;
194 const char* keyboard_layout
= NULL
;
195 int64_t ticks_per_sec
;
198 NICInfo nd_table
[MAX_NICS
];
200 static int rtc_utc
= 1;
201 static int rtc_date_offset
= -1; /* -1 means no change */
202 int cirrus_vga_enabled
= 1;
203 int vmsvga_enabled
= 0;
205 int graphic_width
= 1024;
206 int graphic_height
= 768;
207 int graphic_depth
= 8;
209 int graphic_width
= 800;
210 int graphic_height
= 600;
211 int graphic_depth
= 15;
213 static int full_screen
= 0;
215 static int no_frame
= 0;
218 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
219 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
221 int win2k_install_hack
= 0;
225 const char *vnc_display
;
226 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
233 const char *option_rom
[MAX_OPTION_ROMS
];
235 int semihosting_enabled
= 0;
239 const char *qemu_name
;
242 unsigned int nb_prom_envs
= 0;
243 const char *prom_envs
[MAX_PROM_ENVS
];
245 static int nb_drives_opt
;
246 static struct drive_opt
{
249 } drives_opt
[MAX_DRIVES
];
251 static CPUState
*cur_cpu
;
252 static CPUState
*next_cpu
;
253 static int event_pending
= 1;
254 /* Conversion factor from emulated instructions to virtual clock ticks. */
255 static int icount_time_shift
;
256 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
257 #define MAX_ICOUNT_SHIFT 10
258 /* Compensate for varying guest execution speed. */
259 static int64_t qemu_icount_bias
;
260 static QEMUTimer
*icount_rt_timer
;
261 static QEMUTimer
*icount_vm_timer
;
263 uint8_t qemu_uuid
[16];
265 /***********************************************************/
266 /* x86 ISA bus support */
268 target_phys_addr_t isa_mem_base
= 0;
271 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
272 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
274 static uint32_t ioport_read(int index
, uint32_t address
)
276 static IOPortReadFunc
*default_func
[3] = {
277 default_ioport_readb
,
278 default_ioport_readw
,
281 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
283 func
= default_func
[index
];
284 return func(ioport_opaque
[address
], address
);
287 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
289 static IOPortWriteFunc
*default_func
[3] = {
290 default_ioport_writeb
,
291 default_ioport_writew
,
292 default_ioport_writel
294 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
296 func
= default_func
[index
];
297 func(ioport_opaque
[address
], address
, data
);
300 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
302 #ifdef DEBUG_UNUSED_IOPORT
303 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
308 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
310 #ifdef DEBUG_UNUSED_IOPORT
311 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
315 /* default is to make two byte accesses */
316 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
319 data
= ioport_read(0, address
);
320 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
321 data
|= ioport_read(0, address
) << 8;
325 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
327 ioport_write(0, address
, data
& 0xff);
328 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
329 ioport_write(0, address
, (data
>> 8) & 0xff);
332 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
334 #ifdef DEBUG_UNUSED_IOPORT
335 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
340 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
342 #ifdef DEBUG_UNUSED_IOPORT
343 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
347 /* size is the word size in byte */
348 int register_ioport_read(int start
, int length
, int size
,
349 IOPortReadFunc
*func
, void *opaque
)
355 } else if (size
== 2) {
357 } else if (size
== 4) {
360 hw_error("register_ioport_read: invalid size");
363 for(i
= start
; i
< start
+ length
; i
+= size
) {
364 ioport_read_table
[bsize
][i
] = func
;
365 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
366 hw_error("register_ioport_read: invalid opaque");
367 ioport_opaque
[i
] = opaque
;
372 /* size is the word size in byte */
373 int register_ioport_write(int start
, int length
, int size
,
374 IOPortWriteFunc
*func
, void *opaque
)
380 } else if (size
== 2) {
382 } else if (size
== 4) {
385 hw_error("register_ioport_write: invalid size");
388 for(i
= start
; i
< start
+ length
; i
+= size
) {
389 ioport_write_table
[bsize
][i
] = func
;
390 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
391 hw_error("register_ioport_write: invalid opaque");
392 ioport_opaque
[i
] = opaque
;
397 void isa_unassign_ioport(int start
, int length
)
401 for(i
= start
; i
< start
+ length
; i
++) {
402 ioport_read_table
[0][i
] = default_ioport_readb
;
403 ioport_read_table
[1][i
] = default_ioport_readw
;
404 ioport_read_table
[2][i
] = default_ioport_readl
;
406 ioport_write_table
[0][i
] = default_ioport_writeb
;
407 ioport_write_table
[1][i
] = default_ioport_writew
;
408 ioport_write_table
[2][i
] = default_ioport_writel
;
412 /***********************************************************/
414 void cpu_outb(CPUState
*env
, int addr
, int val
)
417 if (loglevel
& CPU_LOG_IOPORT
)
418 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
420 ioport_write(0, addr
, val
);
423 env
->last_io_time
= cpu_get_time_fast();
427 void cpu_outw(CPUState
*env
, int addr
, int val
)
430 if (loglevel
& CPU_LOG_IOPORT
)
431 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
433 ioport_write(1, addr
, val
);
436 env
->last_io_time
= cpu_get_time_fast();
440 void cpu_outl(CPUState
*env
, int addr
, int val
)
443 if (loglevel
& CPU_LOG_IOPORT
)
444 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
446 ioport_write(2, addr
, val
);
449 env
->last_io_time
= cpu_get_time_fast();
453 int cpu_inb(CPUState
*env
, int addr
)
456 val
= ioport_read(0, addr
);
458 if (loglevel
& CPU_LOG_IOPORT
)
459 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
463 env
->last_io_time
= cpu_get_time_fast();
468 int cpu_inw(CPUState
*env
, int addr
)
471 val
= ioport_read(1, addr
);
473 if (loglevel
& CPU_LOG_IOPORT
)
474 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
478 env
->last_io_time
= cpu_get_time_fast();
483 int cpu_inl(CPUState
*env
, int addr
)
486 val
= ioport_read(2, addr
);
488 if (loglevel
& CPU_LOG_IOPORT
)
489 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
493 env
->last_io_time
= cpu_get_time_fast();
498 /***********************************************************/
499 void hw_error(const char *fmt
, ...)
505 fprintf(stderr
, "qemu: hardware error: ");
506 vfprintf(stderr
, fmt
, ap
);
507 fprintf(stderr
, "\n");
508 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
509 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
511 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
513 cpu_dump_state(env
, stderr
, fprintf
, 0);
523 static QEMUBalloonEvent
*qemu_balloon_event
;
524 void *qemu_balloon_event_opaque
;
526 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
528 qemu_balloon_event
= func
;
529 qemu_balloon_event_opaque
= opaque
;
532 void qemu_balloon(ram_addr_t target
)
534 if (qemu_balloon_event
)
535 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
538 ram_addr_t
qemu_balloon_status(void)
540 if (qemu_balloon_event
)
541 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
545 /***********************************************************/
548 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
549 static void *qemu_put_kbd_event_opaque
;
550 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
551 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
553 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
555 qemu_put_kbd_event_opaque
= opaque
;
556 qemu_put_kbd_event
= func
;
559 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
560 void *opaque
, int absolute
,
563 QEMUPutMouseEntry
*s
, *cursor
;
565 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
569 s
->qemu_put_mouse_event
= func
;
570 s
->qemu_put_mouse_event_opaque
= opaque
;
571 s
->qemu_put_mouse_event_absolute
= absolute
;
572 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
575 if (!qemu_put_mouse_event_head
) {
576 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
580 cursor
= qemu_put_mouse_event_head
;
581 while (cursor
->next
!= NULL
)
582 cursor
= cursor
->next
;
585 qemu_put_mouse_event_current
= s
;
590 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
592 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
594 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
597 cursor
= qemu_put_mouse_event_head
;
598 while (cursor
!= NULL
&& cursor
!= entry
) {
600 cursor
= cursor
->next
;
603 if (cursor
== NULL
) // does not exist or list empty
605 else if (prev
== NULL
) { // entry is head
606 qemu_put_mouse_event_head
= cursor
->next
;
607 if (qemu_put_mouse_event_current
== entry
)
608 qemu_put_mouse_event_current
= cursor
->next
;
609 qemu_free(entry
->qemu_put_mouse_event_name
);
614 prev
->next
= entry
->next
;
616 if (qemu_put_mouse_event_current
== entry
)
617 qemu_put_mouse_event_current
= prev
;
619 qemu_free(entry
->qemu_put_mouse_event_name
);
623 void kbd_put_keycode(int keycode
)
625 if (qemu_put_kbd_event
) {
626 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
630 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
632 QEMUPutMouseEvent
*mouse_event
;
633 void *mouse_event_opaque
;
636 if (!qemu_put_mouse_event_current
) {
641 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
643 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
646 if (graphic_rotate
) {
647 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
650 width
= graphic_width
- 1;
651 mouse_event(mouse_event_opaque
,
652 width
- dy
, dx
, dz
, buttons_state
);
654 mouse_event(mouse_event_opaque
,
655 dx
, dy
, dz
, buttons_state
);
659 int kbd_mouse_is_absolute(void)
661 if (!qemu_put_mouse_event_current
)
664 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
667 void do_info_mice(void)
669 QEMUPutMouseEntry
*cursor
;
672 if (!qemu_put_mouse_event_head
) {
673 term_printf("No mouse devices connected\n");
677 term_printf("Mouse devices available:\n");
678 cursor
= qemu_put_mouse_event_head
;
679 while (cursor
!= NULL
) {
680 term_printf("%c Mouse #%d: %s\n",
681 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
682 index
, cursor
->qemu_put_mouse_event_name
);
684 cursor
= cursor
->next
;
688 void do_mouse_set(int index
)
690 QEMUPutMouseEntry
*cursor
;
693 if (!qemu_put_mouse_event_head
) {
694 term_printf("No mouse devices connected\n");
698 cursor
= qemu_put_mouse_event_head
;
699 while (cursor
!= NULL
&& index
!= i
) {
701 cursor
= cursor
->next
;
705 qemu_put_mouse_event_current
= cursor
;
707 term_printf("Mouse at given index not found\n");
710 /* compute with 96 bit intermediate result: (a*b)/c */
711 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
716 #ifdef WORDS_BIGENDIAN
726 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
727 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
730 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
734 /***********************************************************/
735 /* real time host monotonic timer */
737 #define QEMU_TIMER_BASE 1000000000LL
741 static int64_t clock_freq
;
743 static void init_get_clock(void)
747 ret
= QueryPerformanceFrequency(&freq
);
749 fprintf(stderr
, "Could not calibrate ticks\n");
752 clock_freq
= freq
.QuadPart
;
755 static int64_t get_clock(void)
758 QueryPerformanceCounter(&ti
);
759 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
764 static int use_rt_clock
;
766 static void init_get_clock(void)
769 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
772 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
779 static int64_t get_clock(void)
781 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
784 clock_gettime(CLOCK_MONOTONIC
, &ts
);
785 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
789 /* XXX: using gettimeofday leads to problems if the date
790 changes, so it should be avoided. */
792 gettimeofday(&tv
, NULL
);
793 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
798 /* Return the virtual CPU time, based on the instruction counter. */
799 static int64_t cpu_get_icount(void)
802 CPUState
*env
= cpu_single_env
;;
803 icount
= qemu_icount
;
806 fprintf(stderr
, "Bad clock read\n");
807 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
809 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
812 /***********************************************************/
813 /* guest cycle counter */
815 static int64_t cpu_ticks_prev
;
816 static int64_t cpu_ticks_offset
;
817 static int64_t cpu_clock_offset
;
818 static int cpu_ticks_enabled
;
820 /* return the host CPU cycle counter and handle stop/restart */
821 int64_t cpu_get_ticks(void)
824 return cpu_get_icount();
826 if (!cpu_ticks_enabled
) {
827 return cpu_ticks_offset
;
830 ticks
= cpu_get_real_ticks();
831 if (cpu_ticks_prev
> ticks
) {
832 /* Note: non increasing ticks may happen if the host uses
834 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
836 cpu_ticks_prev
= ticks
;
837 return ticks
+ cpu_ticks_offset
;
841 /* return the host CPU monotonic timer and handle stop/restart */
842 static int64_t cpu_get_clock(void)
845 if (!cpu_ticks_enabled
) {
846 return cpu_clock_offset
;
849 return ti
+ cpu_clock_offset
;
853 /* enable cpu_get_ticks() */
854 void cpu_enable_ticks(void)
856 if (!cpu_ticks_enabled
) {
857 cpu_ticks_offset
-= cpu_get_real_ticks();
858 cpu_clock_offset
-= get_clock();
859 cpu_ticks_enabled
= 1;
863 /* disable cpu_get_ticks() : the clock is stopped. You must not call
864 cpu_get_ticks() after that. */
865 void cpu_disable_ticks(void)
867 if (cpu_ticks_enabled
) {
868 cpu_ticks_offset
= cpu_get_ticks();
869 cpu_clock_offset
= cpu_get_clock();
870 cpu_ticks_enabled
= 0;
874 /***********************************************************/
877 #define QEMU_TIMER_REALTIME 0
878 #define QEMU_TIMER_VIRTUAL 1
882 /* XXX: add frequency */
890 struct QEMUTimer
*next
;
893 struct qemu_alarm_timer
{
897 int (*start
)(struct qemu_alarm_timer
*t
);
898 void (*stop
)(struct qemu_alarm_timer
*t
);
899 void (*rearm
)(struct qemu_alarm_timer
*t
);
903 #define ALARM_FLAG_DYNTICKS 0x1
904 #define ALARM_FLAG_EXPIRED 0x2
906 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
908 return t
->flags
& ALARM_FLAG_DYNTICKS
;
911 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
913 if (!alarm_has_dynticks(t
))
919 /* TODO: MIN_TIMER_REARM_US should be optimized */
920 #define MIN_TIMER_REARM_US 250
922 static struct qemu_alarm_timer
*alarm_timer
;
924 static int alarm_timer_rfd
, alarm_timer_wfd
;
929 struct qemu_alarm_win32
{
933 } alarm_win32_data
= {0, NULL
, -1};
935 static int win32_start_timer(struct qemu_alarm_timer
*t
);
936 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
937 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
941 static int unix_start_timer(struct qemu_alarm_timer
*t
);
942 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
946 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
947 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
948 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
950 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
951 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
953 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
954 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
956 #endif /* __linux__ */
960 /* Correlation between real and virtual time is always going to be
961 fairly approximate, so ignore small variation.
962 When the guest is idle real and virtual time will be aligned in
964 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
966 static void icount_adjust(void)
971 static int64_t last_delta
;
972 /* If the VM is not running, then do nothing. */
976 cur_time
= cpu_get_clock();
977 cur_icount
= qemu_get_clock(vm_clock
);
978 delta
= cur_icount
- cur_time
;
979 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
981 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
982 && icount_time_shift
> 0) {
983 /* The guest is getting too far ahead. Slow time down. */
987 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
988 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
989 /* The guest is getting too far behind. Speed time up. */
993 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
996 static void icount_adjust_rt(void * opaque
)
998 qemu_mod_timer(icount_rt_timer
,
999 qemu_get_clock(rt_clock
) + 1000);
1003 static void icount_adjust_vm(void * opaque
)
1005 qemu_mod_timer(icount_vm_timer
,
1006 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1010 static void init_icount_adjust(void)
1012 /* Have both realtime and virtual time triggers for speed adjustment.
1013 The realtime trigger catches emulated time passing too slowly,
1014 the virtual time trigger catches emulated time passing too fast.
1015 Realtime triggers occur even when idle, so use them less frequently
1016 than VM triggers. */
1017 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1018 qemu_mod_timer(icount_rt_timer
,
1019 qemu_get_clock(rt_clock
) + 1000);
1020 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1021 qemu_mod_timer(icount_vm_timer
,
1022 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1025 static struct qemu_alarm_timer alarm_timers
[] = {
1028 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1029 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1030 /* HPET - if available - is preferred */
1031 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1032 /* ...otherwise try RTC */
1033 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1035 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1037 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1038 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1039 {"win32", 0, win32_start_timer
,
1040 win32_stop_timer
, NULL
, &alarm_win32_data
},
1045 static void show_available_alarms(void)
1049 printf("Available alarm timers, in order of precedence:\n");
1050 for (i
= 0; alarm_timers
[i
].name
; i
++)
1051 printf("%s\n", alarm_timers
[i
].name
);
1054 static void configure_alarms(char const *opt
)
1058 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1061 struct qemu_alarm_timer tmp
;
1063 if (!strcmp(opt
, "?")) {
1064 show_available_alarms();
1070 /* Reorder the array */
1071 name
= strtok(arg
, ",");
1073 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1074 if (!strcmp(alarm_timers
[i
].name
, name
))
1079 fprintf(stderr
, "Unknown clock %s\n", name
);
1088 tmp
= alarm_timers
[i
];
1089 alarm_timers
[i
] = alarm_timers
[cur
];
1090 alarm_timers
[cur
] = tmp
;
1094 name
= strtok(NULL
, ",");
1100 /* Disable remaining timers */
1101 for (i
= cur
; i
< count
; i
++)
1102 alarm_timers
[i
].name
= NULL
;
1104 show_available_alarms();
1109 QEMUClock
*rt_clock
;
1110 QEMUClock
*vm_clock
;
1112 static QEMUTimer
*active_timers
[2];
1114 static QEMUClock
*qemu_new_clock(int type
)
1117 clock
= qemu_mallocz(sizeof(QEMUClock
));
1124 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1128 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1131 ts
->opaque
= opaque
;
1135 void qemu_free_timer(QEMUTimer
*ts
)
1140 /* stop a timer, but do not dealloc it */
1141 void qemu_del_timer(QEMUTimer
*ts
)
1145 /* NOTE: this code must be signal safe because
1146 qemu_timer_expired() can be called from a signal. */
1147 pt
= &active_timers
[ts
->clock
->type
];
1160 /* modify the current timer so that it will be fired when current_time
1161 >= expire_time. The corresponding callback will be called. */
1162 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1168 /* add the timer in the sorted list */
1169 /* NOTE: this code must be signal safe because
1170 qemu_timer_expired() can be called from a signal. */
1171 pt
= &active_timers
[ts
->clock
->type
];
1176 if (t
->expire_time
> expire_time
)
1180 ts
->expire_time
= expire_time
;
1184 /* Rearm if necessary */
1185 if (pt
== &active_timers
[ts
->clock
->type
]) {
1186 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1187 qemu_rearm_alarm_timer(alarm_timer
);
1189 /* Interrupt execution to force deadline recalculation. */
1190 if (use_icount
&& cpu_single_env
) {
1191 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1196 int qemu_timer_pending(QEMUTimer
*ts
)
1199 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1206 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1210 return (timer_head
->expire_time
<= current_time
);
1213 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1219 if (!ts
|| ts
->expire_time
> current_time
)
1221 /* remove timer from the list before calling the callback */
1222 *ptimer_head
= ts
->next
;
1225 /* run the callback (the timer list can be modified) */
1230 int64_t qemu_get_clock(QEMUClock
*clock
)
1232 switch(clock
->type
) {
1233 case QEMU_TIMER_REALTIME
:
1234 return get_clock() / 1000000;
1236 case QEMU_TIMER_VIRTUAL
:
1238 return cpu_get_icount();
1240 return cpu_get_clock();
1245 static void init_timers(void)
1248 ticks_per_sec
= QEMU_TIMER_BASE
;
1249 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1250 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1254 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1256 uint64_t expire_time
;
1258 if (qemu_timer_pending(ts
)) {
1259 expire_time
= ts
->expire_time
;
1263 qemu_put_be64(f
, expire_time
);
1266 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1268 uint64_t expire_time
;
1270 expire_time
= qemu_get_be64(f
);
1271 if (expire_time
!= -1) {
1272 qemu_mod_timer(ts
, expire_time
);
1278 static void timer_save(QEMUFile
*f
, void *opaque
)
1280 if (cpu_ticks_enabled
) {
1281 hw_error("cannot save state if virtual timers are running");
1283 qemu_put_be64(f
, cpu_ticks_offset
);
1284 qemu_put_be64(f
, ticks_per_sec
);
1285 qemu_put_be64(f
, cpu_clock_offset
);
1288 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1290 if (version_id
!= 1 && version_id
!= 2)
1292 if (cpu_ticks_enabled
) {
1295 cpu_ticks_offset
=qemu_get_be64(f
);
1296 ticks_per_sec
=qemu_get_be64(f
);
1297 if (version_id
== 2) {
1298 cpu_clock_offset
=qemu_get_be64(f
);
1304 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1305 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1307 static void host_alarm_handler(int host_signum
)
1311 #define DISP_FREQ 1000
1313 static int64_t delta_min
= INT64_MAX
;
1314 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1316 ti
= qemu_get_clock(vm_clock
);
1317 if (last_clock
!= 0) {
1318 delta
= ti
- last_clock
;
1319 if (delta
< delta_min
)
1321 if (delta
> delta_max
)
1324 if (++count
== DISP_FREQ
) {
1325 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1326 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1327 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1328 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1329 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1331 delta_min
= INT64_MAX
;
1339 if (alarm_has_dynticks(alarm_timer
) ||
1341 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1342 qemu_get_clock(vm_clock
))) ||
1343 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1344 qemu_get_clock(rt_clock
))) {
1345 CPUState
*env
= next_cpu
;
1348 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1349 SetEvent(data
->host_alarm
);
1351 static const char byte
= 0;
1352 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1354 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1357 /* stop the currently executing cpu because a timer occured */
1358 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1360 if (env
->kqemu_enabled
) {
1361 kqemu_cpu_interrupt(env
);
1369 static int64_t qemu_next_deadline(void)
1373 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1374 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1375 qemu_get_clock(vm_clock
);
1377 /* To avoid problems with overflow limit this to 2^32. */
1387 #if defined(__linux__) || defined(_WIN32)
1388 static uint64_t qemu_next_deadline_dyntick(void)
1396 delta
= (qemu_next_deadline() + 999) / 1000;
1398 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1399 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1400 qemu_get_clock(rt_clock
))*1000;
1401 if (rtdelta
< delta
)
1405 if (delta
< MIN_TIMER_REARM_US
)
1406 delta
= MIN_TIMER_REARM_US
;
1414 /* Sets a specific flag */
1415 static int fcntl_setfl(int fd
, int flag
)
1419 flags
= fcntl(fd
, F_GETFL
);
1423 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1429 #if defined(__linux__)
1431 #define RTC_FREQ 1024
1433 static void enable_sigio_timer(int fd
)
1435 struct sigaction act
;
1438 sigfillset(&act
.sa_mask
);
1440 act
.sa_handler
= host_alarm_handler
;
1442 sigaction(SIGIO
, &act
, NULL
);
1443 fcntl_setfl(fd
, O_ASYNC
);
1444 fcntl(fd
, F_SETOWN
, getpid());
1447 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1449 struct hpet_info info
;
1452 fd
= open("/dev/hpet", O_RDONLY
);
1457 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1459 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1460 "error, but for better emulation accuracy type:\n"
1461 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1465 /* Check capabilities */
1466 r
= ioctl(fd
, HPET_INFO
, &info
);
1470 /* Enable periodic mode */
1471 r
= ioctl(fd
, HPET_EPI
, 0);
1472 if (info
.hi_flags
&& (r
< 0))
1475 /* Enable interrupt */
1476 r
= ioctl(fd
, HPET_IE_ON
, 0);
1480 enable_sigio_timer(fd
);
1481 t
->priv
= (void *)(long)fd
;
1489 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1491 int fd
= (long)t
->priv
;
1496 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1499 unsigned long current_rtc_freq
= 0;
1501 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1504 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1505 if (current_rtc_freq
!= RTC_FREQ
&&
1506 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1507 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1508 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1509 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1512 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1518 enable_sigio_timer(rtc_fd
);
1520 t
->priv
= (void *)(long)rtc_fd
;
1525 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1527 int rtc_fd
= (long)t
->priv
;
1532 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1536 struct sigaction act
;
1538 sigfillset(&act
.sa_mask
);
1540 act
.sa_handler
= host_alarm_handler
;
1542 sigaction(SIGALRM
, &act
, NULL
);
1544 ev
.sigev_value
.sival_int
= 0;
1545 ev
.sigev_notify
= SIGEV_SIGNAL
;
1546 ev
.sigev_signo
= SIGALRM
;
1548 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1549 perror("timer_create");
1551 /* disable dynticks */
1552 fprintf(stderr
, "Dynamic Ticks disabled\n");
1557 t
->priv
= (void *)(long)host_timer
;
1562 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1564 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1566 timer_delete(host_timer
);
1569 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1571 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1572 struct itimerspec timeout
;
1573 int64_t nearest_delta_us
= INT64_MAX
;
1576 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1577 !active_timers
[QEMU_TIMER_VIRTUAL
])
1580 nearest_delta_us
= qemu_next_deadline_dyntick();
1582 /* check whether a timer is already running */
1583 if (timer_gettime(host_timer
, &timeout
)) {
1585 fprintf(stderr
, "Internal timer error: aborting\n");
1588 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1589 if (current_us
&& current_us
<= nearest_delta_us
)
1592 timeout
.it_interval
.tv_sec
= 0;
1593 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1594 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1595 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1596 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1598 fprintf(stderr
, "Internal timer error: aborting\n");
1603 #endif /* defined(__linux__) */
1605 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1607 struct sigaction act
;
1608 struct itimerval itv
;
1612 sigfillset(&act
.sa_mask
);
1614 act
.sa_handler
= host_alarm_handler
;
1616 sigaction(SIGALRM
, &act
, NULL
);
1618 itv
.it_interval
.tv_sec
= 0;
1619 /* for i386 kernel 2.6 to get 1 ms */
1620 itv
.it_interval
.tv_usec
= 999;
1621 itv
.it_value
.tv_sec
= 0;
1622 itv
.it_value
.tv_usec
= 10 * 1000;
1624 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1631 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1633 struct itimerval itv
;
1635 memset(&itv
, 0, sizeof(itv
));
1636 setitimer(ITIMER_REAL
, &itv
, NULL
);
1639 #endif /* !defined(_WIN32) */
1641 static void try_to_rearm_timer(void *opaque
)
1643 struct qemu_alarm_timer
*t
= opaque
;
1647 /* Drain the notify pipe */
1650 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1651 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1654 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1655 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1656 qemu_rearm_alarm_timer(alarm_timer
);
1662 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1665 struct qemu_alarm_win32
*data
= t
->priv
;
1668 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1669 if (!data
->host_alarm
) {
1670 perror("Failed CreateEvent");
1674 memset(&tc
, 0, sizeof(tc
));
1675 timeGetDevCaps(&tc
, sizeof(tc
));
1677 if (data
->period
< tc
.wPeriodMin
)
1678 data
->period
= tc
.wPeriodMin
;
1680 timeBeginPeriod(data
->period
);
1682 flags
= TIME_CALLBACK_FUNCTION
;
1683 if (alarm_has_dynticks(t
))
1684 flags
|= TIME_ONESHOT
;
1686 flags
|= TIME_PERIODIC
;
1688 data
->timerId
= timeSetEvent(1, // interval (ms)
1689 data
->period
, // resolution
1690 host_alarm_handler
, // function
1691 (DWORD
)t
, // parameter
1694 if (!data
->timerId
) {
1695 perror("Failed to initialize win32 alarm timer");
1697 timeEndPeriod(data
->period
);
1698 CloseHandle(data
->host_alarm
);
1702 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1707 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1709 struct qemu_alarm_win32
*data
= t
->priv
;
1711 timeKillEvent(data
->timerId
);
1712 timeEndPeriod(data
->period
);
1714 CloseHandle(data
->host_alarm
);
1717 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1719 struct qemu_alarm_win32
*data
= t
->priv
;
1720 uint64_t nearest_delta_us
;
1722 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1723 !active_timers
[QEMU_TIMER_VIRTUAL
])
1726 nearest_delta_us
= qemu_next_deadline_dyntick();
1727 nearest_delta_us
/= 1000;
1729 timeKillEvent(data
->timerId
);
1731 data
->timerId
= timeSetEvent(1,
1735 TIME_ONESHOT
| TIME_PERIODIC
);
1737 if (!data
->timerId
) {
1738 perror("Failed to re-arm win32 alarm timer");
1740 timeEndPeriod(data
->period
);
1741 CloseHandle(data
->host_alarm
);
1748 static int init_timer_alarm(void)
1750 struct qemu_alarm_timer
*t
= NULL
;
1760 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1764 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1768 alarm_timer_rfd
= fds
[0];
1769 alarm_timer_wfd
= fds
[1];
1772 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1773 t
= &alarm_timers
[i
];
1786 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1787 try_to_rearm_timer
, NULL
, t
);
1802 static void quit_timers(void)
1804 alarm_timer
->stop(alarm_timer
);
1808 /***********************************************************/
1809 /* host time/date access */
1810 void qemu_get_timedate(struct tm
*tm
, int offset
)
1817 if (rtc_date_offset
== -1) {
1821 ret
= localtime(&ti
);
1823 ti
-= rtc_date_offset
;
1827 memcpy(tm
, ret
, sizeof(struct tm
));
1830 int qemu_timedate_diff(struct tm
*tm
)
1834 if (rtc_date_offset
== -1)
1836 seconds
= mktimegm(tm
);
1838 seconds
= mktime(tm
);
1840 seconds
= mktimegm(tm
) + rtc_date_offset
;
1842 return seconds
- time(NULL
);
1846 static void socket_cleanup(void)
1851 static int socket_init(void)
1856 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1858 err
= WSAGetLastError();
1859 fprintf(stderr
, "WSAStartup: %d\n", err
);
1862 atexit(socket_cleanup
);
1867 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1872 while (*p
!= '\0' && *p
!= '=') {
1873 if (q
&& (q
- buf
) < buf_size
- 1)
1883 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1888 while (*p
!= '\0') {
1890 if (*(p
+ 1) != ',')
1894 if (q
&& (q
- buf
) < buf_size
- 1)
1904 int get_param_value(char *buf
, int buf_size
,
1905 const char *tag
, const char *str
)
1912 p
= get_opt_name(option
, sizeof(option
), p
);
1916 if (!strcmp(tag
, option
)) {
1917 (void)get_opt_value(buf
, buf_size
, p
);
1920 p
= get_opt_value(NULL
, 0, p
);
1929 int check_params(char *buf
, int buf_size
,
1930 const char * const *params
, const char *str
)
1937 p
= get_opt_name(buf
, buf_size
, p
);
1941 for(i
= 0; params
[i
] != NULL
; i
++)
1942 if (!strcmp(params
[i
], buf
))
1944 if (params
[i
] == NULL
)
1946 p
= get_opt_value(NULL
, 0, p
);
1954 /***********************************************************/
1955 /* Bluetooth support */
1958 static struct HCIInfo
*hci_table
[MAX_NICS
];
1960 static struct bt_vlan_s
{
1961 struct bt_scatternet_s net
;
1963 struct bt_vlan_s
*next
;
1966 /* find or alloc a new bluetooth "VLAN" */
1967 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1969 struct bt_vlan_s
**pvlan
, *vlan
;
1970 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1974 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1976 pvlan
= &first_bt_vlan
;
1977 while (*pvlan
!= NULL
)
1978 pvlan
= &(*pvlan
)->next
;
1983 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1987 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1992 static struct HCIInfo null_hci
= {
1993 .cmd_send
= null_hci_send
,
1994 .sco_send
= null_hci_send
,
1995 .acl_send
= null_hci_send
,
1996 .bdaddr_set
= null_hci_addr_set
,
1999 struct HCIInfo
*qemu_next_hci(void)
2001 if (cur_hci
== nb_hcis
)
2004 return hci_table
[cur_hci
++];
2007 static struct HCIInfo
*hci_init(const char *str
)
2010 struct bt_scatternet_s
*vlan
= 0;
2012 if (!strcmp(str
, "null"))
2015 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2017 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2018 else if (!strncmp(str
, "hci", 3)) {
2021 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2022 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2027 vlan
= qemu_find_bt_vlan(0);
2029 return bt_new_hci(vlan
);
2032 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2037 static int bt_hci_parse(const char *str
)
2039 struct HCIInfo
*hci
;
2042 if (nb_hcis
>= MAX_NICS
) {
2043 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2047 hci
= hci_init(str
);
2056 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2057 hci
->bdaddr_set(hci
, bdaddr
.b
);
2059 hci_table
[nb_hcis
++] = hci
;
2064 static void bt_vhci_add(int vlan_id
)
2066 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2069 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2070 "an empty scatternet %i\n", vlan_id
);
2072 bt_vhci_init(bt_new_hci(vlan
));
2075 static struct bt_device_s
*bt_device_add(const char *opt
)
2077 struct bt_scatternet_s
*vlan
;
2079 char *endp
= strstr(opt
, ",vlan=");
2080 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2083 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2086 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2088 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2093 vlan
= qemu_find_bt_vlan(vlan_id
);
2096 fprintf(stderr
, "qemu: warning: adding a slave device to "
2097 "an empty scatternet %i\n", vlan_id
);
2099 if (!strcmp(devname
, "keyboard"))
2100 return bt_keyboard_init(vlan
);
2102 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2106 static int bt_parse(const char *opt
)
2108 const char *endp
, *p
;
2111 if (strstart(opt
, "hci", &endp
)) {
2112 if (!*endp
|| *endp
== ',') {
2114 if (!strstart(endp
, ",vlan=", 0))
2117 return bt_hci_parse(opt
);
2119 } else if (strstart(opt
, "vhci", &endp
)) {
2120 if (!*endp
|| *endp
== ',') {
2122 if (strstart(endp
, ",vlan=", &p
)) {
2123 vlan
= strtol(p
, (char **) &endp
, 0);
2125 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2129 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2138 } else if (strstart(opt
, "device:", &endp
))
2139 return !bt_device_add(endp
);
2141 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2145 /***********************************************************/
2146 /* QEMU Block devices */
2148 #define HD_ALIAS "index=%d,media=disk"
2150 #define CDROM_ALIAS "index=1,media=cdrom"
2152 #define CDROM_ALIAS "index=2,media=cdrom"
2154 #define FD_ALIAS "index=%d,if=floppy"
2155 #define PFLASH_ALIAS "if=pflash"
2156 #define MTD_ALIAS "if=mtd"
2157 #define SD_ALIAS "index=0,if=sd"
2159 static int drive_add(const char *file
, const char *fmt
, ...)
2163 if (nb_drives_opt
>= MAX_DRIVES
) {
2164 fprintf(stderr
, "qemu: too many drives\n");
2168 drives_opt
[nb_drives_opt
].file
= file
;
2170 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2171 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2174 return nb_drives_opt
++;
2177 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2181 /* seek interface, bus and unit */
2183 for (index
= 0; index
< nb_drives
; index
++)
2184 if (drives_table
[index
].type
== type
&&
2185 drives_table
[index
].bus
== bus
&&
2186 drives_table
[index
].unit
== unit
)
2192 int drive_get_max_bus(BlockInterfaceType type
)
2198 for (index
= 0; index
< nb_drives
; index
++) {
2199 if(drives_table
[index
].type
== type
&&
2200 drives_table
[index
].bus
> max_bus
)
2201 max_bus
= drives_table
[index
].bus
;
2206 static void bdrv_format_print(void *opaque
, const char *name
)
2208 fprintf(stderr
, " %s", name
);
2211 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2212 QEMUMachine
*machine
)
2217 const char *mediastr
= "";
2218 BlockInterfaceType type
;
2219 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2220 int bus_id
, unit_id
;
2221 int cyls
, heads
, secs
, translation
;
2222 BlockDriverState
*bdrv
;
2223 BlockDriver
*drv
= NULL
;
2228 char *str
= arg
->opt
;
2229 static const char * const params
[] = { "bus", "unit", "if", "index",
2230 "cyls", "heads", "secs", "trans",
2231 "media", "snapshot", "file",
2232 "cache", "format", NULL
};
2234 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2235 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2241 cyls
= heads
= secs
= 0;
2244 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2248 if (machine
->use_scsi
) {
2250 max_devs
= MAX_SCSI_DEVS
;
2251 pstrcpy(devname
, sizeof(devname
), "scsi");
2254 max_devs
= MAX_IDE_DEVS
;
2255 pstrcpy(devname
, sizeof(devname
), "ide");
2259 /* extract parameters */
2261 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2262 bus_id
= strtol(buf
, NULL
, 0);
2264 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2269 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2270 unit_id
= strtol(buf
, NULL
, 0);
2272 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2277 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2278 pstrcpy(devname
, sizeof(devname
), buf
);
2279 if (!strcmp(buf
, "ide")) {
2281 max_devs
= MAX_IDE_DEVS
;
2282 } else if (!strcmp(buf
, "scsi")) {
2284 max_devs
= MAX_SCSI_DEVS
;
2285 } else if (!strcmp(buf
, "floppy")) {
2288 } else if (!strcmp(buf
, "pflash")) {
2291 } else if (!strcmp(buf
, "mtd")) {
2294 } else if (!strcmp(buf
, "sd")) {
2297 } else if (!strcmp(buf
, "virtio")) {
2301 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2306 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2307 index
= strtol(buf
, NULL
, 0);
2309 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2314 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2315 cyls
= strtol(buf
, NULL
, 0);
2318 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2319 heads
= strtol(buf
, NULL
, 0);
2322 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2323 secs
= strtol(buf
, NULL
, 0);
2326 if (cyls
|| heads
|| secs
) {
2327 if (cyls
< 1 || cyls
> 16383) {
2328 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2331 if (heads
< 1 || heads
> 16) {
2332 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2335 if (secs
< 1 || secs
> 63) {
2336 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2341 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2344 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2348 if (!strcmp(buf
, "none"))
2349 translation
= BIOS_ATA_TRANSLATION_NONE
;
2350 else if (!strcmp(buf
, "lba"))
2351 translation
= BIOS_ATA_TRANSLATION_LBA
;
2352 else if (!strcmp(buf
, "auto"))
2353 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2355 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2360 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2361 if (!strcmp(buf
, "disk")) {
2363 } else if (!strcmp(buf
, "cdrom")) {
2364 if (cyls
|| secs
|| heads
) {
2366 "qemu: '%s' invalid physical CHS format\n", str
);
2369 media
= MEDIA_CDROM
;
2371 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2376 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2377 if (!strcmp(buf
, "on"))
2379 else if (!strcmp(buf
, "off"))
2382 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2387 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2388 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2390 else if (!strcmp(buf
, "writethrough"))
2392 else if (!strcmp(buf
, "writeback"))
2395 fprintf(stderr
, "qemu: invalid cache option\n");
2400 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2401 if (strcmp(buf
, "?") == 0) {
2402 fprintf(stderr
, "qemu: Supported formats:");
2403 bdrv_iterate_format(bdrv_format_print
, NULL
);
2404 fprintf(stderr
, "\n");
2407 drv
= bdrv_find_format(buf
);
2409 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2414 if (arg
->file
== NULL
)
2415 get_param_value(file
, sizeof(file
), "file", str
);
2417 pstrcpy(file
, sizeof(file
), arg
->file
);
2419 /* compute bus and unit according index */
2422 if (bus_id
!= 0 || unit_id
!= -1) {
2424 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2432 unit_id
= index
% max_devs
;
2433 bus_id
= index
/ max_devs
;
2437 /* if user doesn't specify a unit_id,
2438 * try to find the first free
2441 if (unit_id
== -1) {
2443 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2445 if (max_devs
&& unit_id
>= max_devs
) {
2446 unit_id
-= max_devs
;
2454 if (max_devs
&& unit_id
>= max_devs
) {
2455 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2456 str
, unit_id
, max_devs
- 1);
2461 * ignore multiple definitions
2464 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2469 if (type
== IF_IDE
|| type
== IF_SCSI
)
2470 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2472 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2473 devname
, bus_id
, mediastr
, unit_id
);
2475 snprintf(buf
, sizeof(buf
), "%s%s%i",
2476 devname
, mediastr
, unit_id
);
2477 bdrv
= bdrv_new(buf
);
2478 drives_table
[nb_drives
].bdrv
= bdrv
;
2479 drives_table
[nb_drives
].type
= type
;
2480 drives_table
[nb_drives
].bus
= bus_id
;
2481 drives_table
[nb_drives
].unit
= unit_id
;
2490 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2491 bdrv_set_translation_hint(bdrv
, translation
);
2495 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2500 /* FIXME: This isn't really a floppy, but it's a reasonable
2503 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2514 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2515 cache
= 2; /* always use write-back with snapshot */
2517 if (cache
== 0) /* no caching */
2518 bdrv_flags
|= BDRV_O_NOCACHE
;
2519 else if (cache
== 2) /* write-back */
2520 bdrv_flags
|= BDRV_O_CACHE_WB
;
2521 else if (cache
== 3) /* not specified */
2522 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2523 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2524 fprintf(stderr
, "qemu: could not open disk image %s\n",
2531 /***********************************************************/
2534 static USBPort
*used_usb_ports
;
2535 static USBPort
*free_usb_ports
;
2537 /* ??? Maybe change this to register a hub to keep track of the topology. */
2538 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2539 usb_attachfn attach
)
2541 port
->opaque
= opaque
;
2542 port
->index
= index
;
2543 port
->attach
= attach
;
2544 port
->next
= free_usb_ports
;
2545 free_usb_ports
= port
;
2548 int usb_device_add_dev(USBDevice
*dev
)
2552 /* Find a USB port to add the device to. */
2553 port
= free_usb_ports
;
2557 /* Create a new hub and chain it on. */
2558 free_usb_ports
= NULL
;
2559 port
->next
= used_usb_ports
;
2560 used_usb_ports
= port
;
2562 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2563 usb_attach(port
, hub
);
2564 port
= free_usb_ports
;
2567 free_usb_ports
= port
->next
;
2568 port
->next
= used_usb_ports
;
2569 used_usb_ports
= port
;
2570 usb_attach(port
, dev
);
2574 static int usb_device_add(const char *devname
)
2579 if (!free_usb_ports
)
2582 if (strstart(devname
, "host:", &p
)) {
2583 dev
= usb_host_device_open(p
);
2584 } else if (!strcmp(devname
, "mouse")) {
2585 dev
= usb_mouse_init();
2586 } else if (!strcmp(devname
, "tablet")) {
2587 dev
= usb_tablet_init();
2588 } else if (!strcmp(devname
, "keyboard")) {
2589 dev
= usb_keyboard_init();
2590 } else if (strstart(devname
, "disk:", &p
)) {
2591 dev
= usb_msd_init(p
);
2592 } else if (!strcmp(devname
, "wacom-tablet")) {
2593 dev
= usb_wacom_init();
2594 } else if (strstart(devname
, "serial:", &p
)) {
2595 dev
= usb_serial_init(p
);
2596 #ifdef CONFIG_BRLAPI
2597 } else if (!strcmp(devname
, "braille")) {
2598 dev
= usb_baum_init();
2600 } else if (strstart(devname
, "net:", &p
)) {
2603 if (net_client_init("nic", p
) < 0)
2605 nd_table
[nic
].model
= "usb";
2606 dev
= usb_net_init(&nd_table
[nic
]);
2607 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2608 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2609 bt_new_hci(qemu_find_bt_vlan(0)));
2616 return usb_device_add_dev(dev
);
2619 int usb_device_del_addr(int bus_num
, int addr
)
2625 if (!used_usb_ports
)
2631 lastp
= &used_usb_ports
;
2632 port
= used_usb_ports
;
2633 while (port
&& port
->dev
->addr
!= addr
) {
2634 lastp
= &port
->next
;
2642 *lastp
= port
->next
;
2643 usb_attach(port
, NULL
);
2644 dev
->handle_destroy(dev
);
2645 port
->next
= free_usb_ports
;
2646 free_usb_ports
= port
;
2650 static int usb_device_del(const char *devname
)
2655 if (strstart(devname
, "host:", &p
))
2656 return usb_host_device_close(p
);
2658 if (!used_usb_ports
)
2661 p
= strchr(devname
, '.');
2664 bus_num
= strtoul(devname
, NULL
, 0);
2665 addr
= strtoul(p
+ 1, NULL
, 0);
2667 return usb_device_del_addr(bus_num
, addr
);
2670 void do_usb_add(const char *devname
)
2672 usb_device_add(devname
);
2675 void do_usb_del(const char *devname
)
2677 usb_device_del(devname
);
2684 const char *speed_str
;
2687 term_printf("USB support not enabled\n");
2691 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2695 switch(dev
->speed
) {
2699 case USB_SPEED_FULL
:
2702 case USB_SPEED_HIGH
:
2709 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2710 0, dev
->addr
, speed_str
, dev
->devname
);
2714 /***********************************************************/
2715 /* PCMCIA/Cardbus */
2717 static struct pcmcia_socket_entry_s
{
2718 struct pcmcia_socket_s
*socket
;
2719 struct pcmcia_socket_entry_s
*next
;
2720 } *pcmcia_sockets
= 0;
2722 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2724 struct pcmcia_socket_entry_s
*entry
;
2726 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2727 entry
->socket
= socket
;
2728 entry
->next
= pcmcia_sockets
;
2729 pcmcia_sockets
= entry
;
2732 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2734 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2736 ptr
= &pcmcia_sockets
;
2737 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2738 if (entry
->socket
== socket
) {
2744 void pcmcia_info(void)
2746 struct pcmcia_socket_entry_s
*iter
;
2747 if (!pcmcia_sockets
)
2748 term_printf("No PCMCIA sockets\n");
2750 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2751 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2752 iter
->socket
->attached
? iter
->socket
->card_string
:
2756 /***********************************************************/
2759 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2763 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2767 static void dumb_display_init(DisplayState
*ds
)
2772 ds
->dpy_update
= dumb_update
;
2773 ds
->dpy_resize
= dumb_resize
;
2774 ds
->dpy_refresh
= NULL
;
2775 ds
->gui_timer_interval
= 0;
2779 /***********************************************************/
2782 #define MAX_IO_HANDLERS 64
2784 typedef struct IOHandlerRecord
{
2786 IOCanRWHandler
*fd_read_poll
;
2788 IOHandler
*fd_write
;
2791 /* temporary data */
2793 struct IOHandlerRecord
*next
;
2796 static IOHandlerRecord
*first_io_handler
;
2798 /* XXX: fd_read_poll should be suppressed, but an API change is
2799 necessary in the character devices to suppress fd_can_read(). */
2800 int qemu_set_fd_handler2(int fd
,
2801 IOCanRWHandler
*fd_read_poll
,
2803 IOHandler
*fd_write
,
2806 IOHandlerRecord
**pioh
, *ioh
;
2808 if (!fd_read
&& !fd_write
) {
2809 pioh
= &first_io_handler
;
2814 if (ioh
->fd
== fd
) {
2821 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2825 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2828 ioh
->next
= first_io_handler
;
2829 first_io_handler
= ioh
;
2832 ioh
->fd_read_poll
= fd_read_poll
;
2833 ioh
->fd_read
= fd_read
;
2834 ioh
->fd_write
= fd_write
;
2835 ioh
->opaque
= opaque
;
2841 int qemu_set_fd_handler(int fd
,
2843 IOHandler
*fd_write
,
2846 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2850 /***********************************************************/
2851 /* Polling handling */
2853 typedef struct PollingEntry
{
2856 struct PollingEntry
*next
;
2859 static PollingEntry
*first_polling_entry
;
2861 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2863 PollingEntry
**ppe
, *pe
;
2864 pe
= qemu_mallocz(sizeof(PollingEntry
));
2868 pe
->opaque
= opaque
;
2869 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2874 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2876 PollingEntry
**ppe
, *pe
;
2877 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2879 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2887 /***********************************************************/
2888 /* Wait objects support */
2889 typedef struct WaitObjects
{
2891 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2892 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2893 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2896 static WaitObjects wait_objects
= {0};
2898 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2900 WaitObjects
*w
= &wait_objects
;
2902 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2904 w
->events
[w
->num
] = handle
;
2905 w
->func
[w
->num
] = func
;
2906 w
->opaque
[w
->num
] = opaque
;
2911 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2914 WaitObjects
*w
= &wait_objects
;
2917 for (i
= 0; i
< w
->num
; i
++) {
2918 if (w
->events
[i
] == handle
)
2921 w
->events
[i
] = w
->events
[i
+ 1];
2922 w
->func
[i
] = w
->func
[i
+ 1];
2923 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2931 /***********************************************************/
2932 /* ram save/restore */
2934 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2938 v
= qemu_get_byte(f
);
2941 if (qemu_get_buffer(f
, buf
, len
) != len
)
2945 v
= qemu_get_byte(f
);
2946 memset(buf
, v
, len
);
2952 if (qemu_file_has_error(f
))
2958 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2963 if (qemu_get_be32(f
) != phys_ram_size
)
2965 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2966 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2973 #define BDRV_HASH_BLOCK_SIZE 1024
2974 #define IOBUF_SIZE 4096
2975 #define RAM_CBLOCK_MAGIC 0xfabe
2977 typedef struct RamDecompressState
{
2980 uint8_t buf
[IOBUF_SIZE
];
2981 } RamDecompressState
;
2983 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2986 memset(s
, 0, sizeof(*s
));
2988 ret
= inflateInit(&s
->zstream
);
2994 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2998 s
->zstream
.avail_out
= len
;
2999 s
->zstream
.next_out
= buf
;
3000 while (s
->zstream
.avail_out
> 0) {
3001 if (s
->zstream
.avail_in
== 0) {
3002 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3004 clen
= qemu_get_be16(s
->f
);
3005 if (clen
> IOBUF_SIZE
)
3007 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3008 s
->zstream
.avail_in
= clen
;
3009 s
->zstream
.next_in
= s
->buf
;
3011 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3012 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3019 static void ram_decompress_close(RamDecompressState
*s
)
3021 inflateEnd(&s
->zstream
);
3024 #define RAM_SAVE_FLAG_FULL 0x01
3025 #define RAM_SAVE_FLAG_COMPRESS 0x02
3026 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3027 #define RAM_SAVE_FLAG_PAGE 0x08
3028 #define RAM_SAVE_FLAG_EOS 0x10
3030 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3032 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3033 uint32_t *array
= (uint32_t *)page
;
3036 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3037 if (array
[i
] != val
)
3044 static int ram_save_block(QEMUFile
*f
)
3046 static ram_addr_t current_addr
= 0;
3047 ram_addr_t saved_addr
= current_addr
;
3048 ram_addr_t addr
= 0;
3051 while (addr
< phys_ram_size
) {
3052 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3055 cpu_physical_memory_reset_dirty(current_addr
,
3056 current_addr
+ TARGET_PAGE_SIZE
,
3057 MIGRATION_DIRTY_FLAG
);
3059 ch
= *(phys_ram_base
+ current_addr
);
3061 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3062 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3063 qemu_put_byte(f
, ch
);
3065 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3066 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3072 addr
+= TARGET_PAGE_SIZE
;
3073 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3079 static ram_addr_t ram_save_threshold
= 10;
3081 static ram_addr_t
ram_save_remaining(void)
3084 ram_addr_t count
= 0;
3086 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3087 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3094 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3099 /* Make sure all dirty bits are set */
3100 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3101 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3102 cpu_physical_memory_set_dirty(addr
);
3105 /* Enable dirty memory tracking */
3106 cpu_physical_memory_set_dirty_tracking(1);
3108 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3111 while (!qemu_file_rate_limit(f
)) {
3114 ret
= ram_save_block(f
);
3115 if (ret
== 0) /* no more blocks */
3119 /* try transferring iterative blocks of memory */
3122 cpu_physical_memory_set_dirty_tracking(0);
3124 /* flush all remaining blocks regardless of rate limiting */
3125 while (ram_save_block(f
) != 0);
3128 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3130 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3133 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3135 RamDecompressState s1
, *s
= &s1
;
3139 if (ram_decompress_open(s
, f
) < 0)
3141 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3142 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3143 fprintf(stderr
, "Error while reading ram block header\n");
3147 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3148 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3153 printf("Error block header\n");
3157 ram_decompress_close(s
);
3162 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3167 if (version_id
== 1)
3168 return ram_load_v1(f
, opaque
);
3170 if (version_id
== 2) {
3171 if (qemu_get_be32(f
) != phys_ram_size
)
3173 return ram_load_dead(f
, opaque
);
3176 if (version_id
!= 3)
3180 addr
= qemu_get_be64(f
);
3182 flags
= addr
& ~TARGET_PAGE_MASK
;
3183 addr
&= TARGET_PAGE_MASK
;
3185 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3186 if (addr
!= phys_ram_size
)
3190 if (flags
& RAM_SAVE_FLAG_FULL
) {
3191 if (ram_load_dead(f
, opaque
) < 0)
3195 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3196 uint8_t ch
= qemu_get_byte(f
);
3197 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3198 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3199 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3200 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3205 void qemu_service_io(void)
3207 CPUState
*env
= cpu_single_env
;
3209 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3211 if (env
->kqemu_enabled
) {
3212 kqemu_cpu_interrupt(env
);
3218 /***********************************************************/
3219 /* bottom halves (can be seen as timers which expire ASAP) */
3230 static QEMUBH
*first_bh
= NULL
;
3232 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3235 bh
= qemu_mallocz(sizeof(QEMUBH
));
3239 bh
->opaque
= opaque
;
3240 bh
->next
= first_bh
;
3245 int qemu_bh_poll(void)
3251 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3252 if (!bh
->deleted
&& bh
->scheduled
) {
3261 /* remove deleted bhs */
3275 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3283 void qemu_bh_schedule(QEMUBH
*bh
)
3285 CPUState
*env
= cpu_single_env
;
3290 /* stop the currently executing CPU to execute the BH ASAP */
3292 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3296 void qemu_bh_cancel(QEMUBH
*bh
)
3301 void qemu_bh_delete(QEMUBH
*bh
)
3307 static void qemu_bh_update_timeout(int *timeout
)
3311 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3312 if (!bh
->deleted
&& bh
->scheduled
) {
3314 /* idle bottom halves will be polled at least
3316 *timeout
= MIN(10, *timeout
);
3318 /* non-idle bottom halves will be executed
3327 /***********************************************************/
3328 /* machine registration */
3330 static QEMUMachine
*first_machine
= NULL
;
3332 int qemu_register_machine(QEMUMachine
*m
)
3335 pm
= &first_machine
;
3343 static QEMUMachine
*find_machine(const char *name
)
3347 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3348 if (!strcmp(m
->name
, name
))
3354 /***********************************************************/
3355 /* main execution loop */
3357 static void gui_update(void *opaque
)
3359 DisplayState
*ds
= opaque
;
3360 ds
->dpy_refresh(ds
);
3361 qemu_mod_timer(ds
->gui_timer
,
3362 (ds
->gui_timer_interval
?
3363 ds
->gui_timer_interval
:
3364 GUI_REFRESH_INTERVAL
)
3365 + qemu_get_clock(rt_clock
));
3368 struct vm_change_state_entry
{
3369 VMChangeStateHandler
*cb
;
3371 LIST_ENTRY (vm_change_state_entry
) entries
;
3374 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3376 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3379 VMChangeStateEntry
*e
;
3381 e
= qemu_mallocz(sizeof (*e
));
3387 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3391 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3393 LIST_REMOVE (e
, entries
);
3397 static void vm_state_notify(int running
)
3399 VMChangeStateEntry
*e
;
3401 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3402 e
->cb(e
->opaque
, running
);
3406 /* XXX: support several handlers */
3407 static VMStopHandler
*vm_stop_cb
;
3408 static void *vm_stop_opaque
;
3410 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3413 vm_stop_opaque
= opaque
;
3417 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3428 qemu_rearm_alarm_timer(alarm_timer
);
3432 void vm_stop(int reason
)
3435 cpu_disable_ticks();
3439 vm_stop_cb(vm_stop_opaque
, reason
);
3446 /* reset/shutdown handler */
3448 typedef struct QEMUResetEntry
{
3449 QEMUResetHandler
*func
;
3451 struct QEMUResetEntry
*next
;
3454 static QEMUResetEntry
*first_reset_entry
;
3455 static int reset_requested
;
3456 static int shutdown_requested
;
3457 static int powerdown_requested
;
3459 int qemu_shutdown_requested(void)
3461 int r
= shutdown_requested
;
3462 shutdown_requested
= 0;
3466 int qemu_reset_requested(void)
3468 int r
= reset_requested
;
3469 reset_requested
= 0;
3473 int qemu_powerdown_requested(void)
3475 int r
= powerdown_requested
;
3476 powerdown_requested
= 0;
3480 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3482 QEMUResetEntry
**pre
, *re
;
3484 pre
= &first_reset_entry
;
3485 while (*pre
!= NULL
)
3486 pre
= &(*pre
)->next
;
3487 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3489 re
->opaque
= opaque
;
3494 void qemu_system_reset(void)
3498 /* reset all devices */
3499 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3500 re
->func(re
->opaque
);
3504 void qemu_system_reset_request(void)
3507 shutdown_requested
= 1;
3509 reset_requested
= 1;
3512 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3515 void qemu_system_shutdown_request(void)
3517 shutdown_requested
= 1;
3519 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3522 void qemu_system_powerdown_request(void)
3524 powerdown_requested
= 1;
3526 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3530 static void host_main_loop_wait(int *timeout
)
3536 /* XXX: need to suppress polling by better using win32 events */
3538 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3539 ret
|= pe
->func(pe
->opaque
);
3543 WaitObjects
*w
= &wait_objects
;
3545 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3546 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3547 if (w
->func
[ret
- WAIT_OBJECT_0
])
3548 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3550 /* Check for additional signaled events */
3551 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3553 /* Check if event is signaled */
3554 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3555 if(ret2
== WAIT_OBJECT_0
) {
3557 w
->func
[i
](w
->opaque
[i
]);
3558 } else if (ret2
== WAIT_TIMEOUT
) {
3560 err
= GetLastError();
3561 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3564 } else if (ret
== WAIT_TIMEOUT
) {
3566 err
= GetLastError();
3567 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3574 static void host_main_loop_wait(int *timeout
)
3579 void main_loop_wait(int timeout
)
3581 IOHandlerRecord
*ioh
;
3582 fd_set rfds
, wfds
, xfds
;
3586 qemu_bh_update_timeout(&timeout
);
3588 host_main_loop_wait(&timeout
);
3590 /* poll any events */
3591 /* XXX: separate device handlers from system ones */
3596 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3600 (!ioh
->fd_read_poll
||
3601 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3602 FD_SET(ioh
->fd
, &rfds
);
3606 if (ioh
->fd_write
) {
3607 FD_SET(ioh
->fd
, &wfds
);
3613 tv
.tv_sec
= timeout
/ 1000;
3614 tv
.tv_usec
= (timeout
% 1000) * 1000;
3616 #if defined(CONFIG_SLIRP)
3617 if (slirp_is_inited()) {
3618 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3621 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3623 IOHandlerRecord
**pioh
;
3625 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3626 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3627 ioh
->fd_read(ioh
->opaque
);
3629 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3630 ioh
->fd_write(ioh
->opaque
);
3634 /* remove deleted IO handlers */
3635 pioh
= &first_io_handler
;
3645 #if defined(CONFIG_SLIRP)
3646 if (slirp_is_inited()) {
3652 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3656 /* vm time timers */
3657 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3658 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3659 qemu_get_clock(vm_clock
));
3661 /* real time timers */
3662 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3663 qemu_get_clock(rt_clock
));
3665 /* Check bottom-halves last in case any of the earlier events triggered
3671 static int main_loop(void)
3674 #ifdef CONFIG_PROFILER
3679 cur_cpu
= first_cpu
;
3680 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3687 #ifdef CONFIG_PROFILER
3688 ti
= profile_getclock();
3693 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3694 env
->icount_decr
.u16
.low
= 0;
3695 env
->icount_extra
= 0;
3696 count
= qemu_next_deadline();
3697 count
= (count
+ (1 << icount_time_shift
) - 1)
3698 >> icount_time_shift
;
3699 qemu_icount
+= count
;
3700 decr
= (count
> 0xffff) ? 0xffff : count
;
3702 env
->icount_decr
.u16
.low
= decr
;
3703 env
->icount_extra
= count
;
3705 ret
= cpu_exec(env
);
3706 #ifdef CONFIG_PROFILER
3707 qemu_time
+= profile_getclock() - ti
;
3710 /* Fold pending instructions back into the
3711 instruction counter, and clear the interrupt flag. */
3712 qemu_icount
-= (env
->icount_decr
.u16
.low
3713 + env
->icount_extra
);
3714 env
->icount_decr
.u32
= 0;
3715 env
->icount_extra
= 0;
3717 next_cpu
= env
->next_cpu
?: first_cpu
;
3718 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3719 ret
= EXCP_INTERRUPT
;
3723 if (ret
== EXCP_HLT
) {
3724 /* Give the next CPU a chance to run. */
3728 if (ret
!= EXCP_HALTED
)
3730 /* all CPUs are halted ? */
3736 if (shutdown_requested
) {
3737 ret
= EXCP_INTERRUPT
;
3745 if (reset_requested
) {
3746 reset_requested
= 0;
3747 qemu_system_reset();
3748 ret
= EXCP_INTERRUPT
;
3750 if (powerdown_requested
) {
3751 powerdown_requested
= 0;
3752 qemu_system_powerdown();
3753 ret
= EXCP_INTERRUPT
;
3755 if (unlikely(ret
== EXCP_DEBUG
)) {
3756 gdb_set_stop_cpu(cur_cpu
);
3757 vm_stop(EXCP_DEBUG
);
3759 /* If all cpus are halted then wait until the next IRQ */
3760 /* XXX: use timeout computed from timers */
3761 if (ret
== EXCP_HALTED
) {
3765 /* Advance virtual time to the next event. */
3766 if (use_icount
== 1) {
3767 /* When not using an adaptive execution frequency
3768 we tend to get badly out of sync with real time,
3769 so just delay for a reasonable amount of time. */
3772 delta
= cpu_get_icount() - cpu_get_clock();
3775 /* If virtual time is ahead of real time then just
3777 timeout
= (delta
/ 1000000) + 1;
3779 /* Wait for either IO to occur or the next
3781 add
= qemu_next_deadline();
3782 /* We advance the timer before checking for IO.
3783 Limit the amount we advance so that early IO
3784 activity won't get the guest too far ahead. */
3788 add
= (add
+ (1 << icount_time_shift
) - 1)
3789 >> icount_time_shift
;
3791 timeout
= delta
/ 1000000;
3802 if (shutdown_requested
) {
3803 ret
= EXCP_INTERRUPT
;
3808 #ifdef CONFIG_PROFILER
3809 ti
= profile_getclock();
3811 main_loop_wait(timeout
);
3812 #ifdef CONFIG_PROFILER
3813 dev_time
+= profile_getclock() - ti
;
3816 cpu_disable_ticks();
3820 static void help(int exitcode
)
3822 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3823 "usage: %s [options] [disk_image]\n"
3825 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3827 "Standard options:\n"
3828 "-M machine select emulated machine (-M ? for list)\n"
3829 "-cpu cpu select CPU (-cpu ? for list)\n"
3830 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3831 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3832 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3833 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3834 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3835 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3836 " [,cache=writethrough|writeback|none][,format=f]\n"
3837 " use 'file' as a drive image\n"
3838 "-mtdblock file use 'file' as on-board Flash memory image\n"
3839 "-sd file use 'file' as SecureDigital card image\n"
3840 "-pflash file use 'file' as a parallel flash image\n"
3841 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3842 "-snapshot write to temporary files instead of disk image files\n"
3844 "-no-frame open SDL window without a frame and window decorations\n"
3845 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3846 "-no-quit disable SDL window close capability\n"
3849 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3851 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3852 "-smp n set the number of CPUs to 'n' [default=1]\n"
3853 "-nographic disable graphical output and redirect serial I/Os to console\n"
3854 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3856 "-k language use keyboard layout (for example \"fr\" for French)\n"
3859 "-audio-help print list of audio drivers and their options\n"
3860 "-soundhw c1,... enable audio support\n"
3861 " and only specified sound cards (comma separated list)\n"
3862 " use -soundhw ? to get the list of supported cards\n"
3863 " use -soundhw all to enable all of them\n"
3865 "-vga [std|cirrus|vmware]\n"
3866 " select video card type\n"
3867 "-localtime set the real time clock to local time [default=utc]\n"
3868 "-full-screen start in full screen\n"
3870 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3872 "-usb enable the USB driver (will be the default soon)\n"
3873 "-usbdevice name add the host or guest USB device 'name'\n"
3874 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3875 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3877 "-name string set the name of the guest\n"
3878 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3880 "Network options:\n"
3881 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
3882 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3884 "-net user[,vlan=n][,hostname=host]\n"
3885 " connect the user mode network stack to VLAN 'n' and send\n"
3886 " hostname 'host' to DHCP clients\n"
3889 "-net tap[,vlan=n],ifname=name\n"
3890 " connect the host TAP network interface to VLAN 'n'\n"
3892 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3893 " connect the host TAP network interface to VLAN 'n' and use the\n"
3894 " network scripts 'file' (default=%s)\n"
3895 " and 'dfile' (default=%s);\n"
3896 " use '[down]script=no' to disable script execution;\n"
3897 " use 'fd=h' to connect to an already opened TAP interface\n"
3899 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3900 " connect the vlan 'n' to another VLAN using a socket connection\n"
3901 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
3902 " connect the vlan 'n' to multicast maddr and port\n"
3904 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3905 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3906 " on host and listening for incoming connections on 'socketpath'.\n"
3907 " Use group 'groupname' and mode 'octalmode' to change default\n"
3908 " ownership and permissions for communication port.\n"
3910 "-net none use it alone to have zero network devices; if no -net option\n"
3911 " is provided, the default is '-net nic -net user'\n"
3913 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3914 "-bt hci,host[:id]\n"
3915 " Use host's HCI with the given name\n"
3916 "-bt hci[,vlan=n]\n"
3917 " Emulate a standard HCI in virtual scatternet 'n'\n"
3918 "-bt vhci[,vlan=n]\n"
3919 " Add host computer to virtual scatternet 'n' using VHCI\n"
3920 "-bt device:dev[,vlan=n]\n"
3921 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3924 "-tftp dir allow tftp access to files in dir [-net user]\n"
3925 "-bootp file advertise file in BOOTP replies\n"
3927 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3929 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3930 " redirect TCP or UDP connections from host to guest [-net user]\n"
3933 "Linux boot specific:\n"
3934 "-kernel bzImage use 'bzImage' as kernel image\n"
3935 "-append cmdline use 'cmdline' as kernel command line\n"
3936 "-initrd file use 'file' as initial ram disk\n"
3938 "Debug/Expert options:\n"
3939 "-monitor dev redirect the monitor to char device 'dev'\n"
3940 "-serial dev redirect the serial port to char device 'dev'\n"
3941 "-parallel dev redirect the parallel port to char device 'dev'\n"
3942 "-pidfile file Write PID to 'file'\n"
3943 "-S freeze CPU at startup (use 'c' to start execution)\n"
3944 "-s wait gdb connection to port\n"
3945 "-p port set gdb connection port [default=%s]\n"
3946 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3947 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3948 " translation (t=none or lba) (usually qemu can guess them)\n"
3949 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3951 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3952 "-no-kqemu disable KQEMU kernel module usage\n"
3955 "-enable-kvm enable KVM full virtualization support\n"
3958 "-no-acpi disable ACPI\n"
3960 #ifdef CONFIG_CURSES
3961 "-curses use a curses/ncurses interface instead of SDL\n"
3963 "-no-reboot exit instead of rebooting\n"
3964 "-no-shutdown stop before shutdown\n"
3965 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3966 "-vnc display start a VNC server on display\n"
3968 "-daemonize daemonize QEMU after initializing\n"
3970 "-option-rom rom load a file, rom, into the option ROM space\n"
3972 "-prom-env variable=value set OpenBIOS nvram variables\n"
3974 "-clock force the use of the given methods for timer alarm.\n"
3975 " To see what timers are available use -clock ?\n"
3976 "-startdate select initial date of the clock\n"
3977 "-icount [N|auto]\n"
3978 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
3980 "During emulation, the following keys are useful:\n"
3981 "ctrl-alt-f toggle full screen\n"
3982 "ctrl-alt-n switch to virtual console 'n'\n"
3983 "ctrl-alt toggle mouse and keyboard grab\n"
3985 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3990 DEFAULT_NETWORK_SCRIPT
,
3991 DEFAULT_NETWORK_DOWN_SCRIPT
,
3993 DEFAULT_GDBSTUB_PORT
,
3998 #define HAS_ARG 0x0001
4013 QEMU_OPTION_mtdblock
,
4017 QEMU_OPTION_snapshot
,
4019 QEMU_OPTION_no_fd_bootchk
,
4022 QEMU_OPTION_nographic
,
4023 QEMU_OPTION_portrait
,
4025 QEMU_OPTION_audio_help
,
4026 QEMU_OPTION_soundhw
,
4048 QEMU_OPTION_localtime
,
4052 QEMU_OPTION_monitor
,
4054 QEMU_OPTION_parallel
,
4056 QEMU_OPTION_full_screen
,
4057 QEMU_OPTION_no_frame
,
4058 QEMU_OPTION_alt_grab
,
4059 QEMU_OPTION_no_quit
,
4060 QEMU_OPTION_pidfile
,
4061 QEMU_OPTION_no_kqemu
,
4062 QEMU_OPTION_kernel_kqemu
,
4063 QEMU_OPTION_enable_kvm
,
4064 QEMU_OPTION_win2k_hack
,
4066 QEMU_OPTION_usbdevice
,
4069 QEMU_OPTION_no_acpi
,
4071 QEMU_OPTION_no_reboot
,
4072 QEMU_OPTION_no_shutdown
,
4073 QEMU_OPTION_show_cursor
,
4074 QEMU_OPTION_daemonize
,
4075 QEMU_OPTION_option_rom
,
4076 QEMU_OPTION_semihosting
,
4078 QEMU_OPTION_prom_env
,
4079 QEMU_OPTION_old_param
,
4081 QEMU_OPTION_startdate
,
4082 QEMU_OPTION_tb_size
,
4085 QEMU_OPTION_incoming
,
4088 typedef struct QEMUOption
{
4094 static const QEMUOption qemu_options
[] = {
4095 { "h", 0, QEMU_OPTION_h
},
4096 { "help", 0, QEMU_OPTION_h
},
4098 { "M", HAS_ARG
, QEMU_OPTION_M
},
4099 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4100 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4101 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4102 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4103 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4104 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4105 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4106 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4107 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4108 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4109 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4110 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4111 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4112 { "snapshot", 0, QEMU_OPTION_snapshot
},
4114 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4116 { "m", HAS_ARG
, QEMU_OPTION_m
},
4117 { "nographic", 0, QEMU_OPTION_nographic
},
4118 { "portrait", 0, QEMU_OPTION_portrait
},
4119 { "k", HAS_ARG
, QEMU_OPTION_k
},
4121 { "audio-help", 0, QEMU_OPTION_audio_help
},
4122 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4125 { "net", HAS_ARG
, QEMU_OPTION_net
},
4127 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4128 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4130 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4132 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4134 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4136 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4137 { "append", HAS_ARG
, QEMU_OPTION_append
},
4138 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4140 { "S", 0, QEMU_OPTION_S
},
4141 { "s", 0, QEMU_OPTION_s
},
4142 { "p", HAS_ARG
, QEMU_OPTION_p
},
4143 { "d", HAS_ARG
, QEMU_OPTION_d
},
4144 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4145 { "L", HAS_ARG
, QEMU_OPTION_L
},
4146 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4148 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4149 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4152 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4154 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4155 { "g", 1, QEMU_OPTION_g
},
4157 { "localtime", 0, QEMU_OPTION_localtime
},
4158 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4159 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4160 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4161 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4162 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4163 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4164 { "full-screen", 0, QEMU_OPTION_full_screen
},
4166 { "no-frame", 0, QEMU_OPTION_no_frame
},
4167 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4168 { "no-quit", 0, QEMU_OPTION_no_quit
},
4170 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4171 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4172 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4173 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4174 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4175 #ifdef CONFIG_CURSES
4176 { "curses", 0, QEMU_OPTION_curses
},
4178 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4180 /* temporary options */
4181 { "usb", 0, QEMU_OPTION_usb
},
4182 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4183 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4184 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4185 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4186 { "daemonize", 0, QEMU_OPTION_daemonize
},
4187 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4188 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4189 { "semihosting", 0, QEMU_OPTION_semihosting
},
4191 { "name", HAS_ARG
, QEMU_OPTION_name
},
4192 #if defined(TARGET_SPARC)
4193 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4195 #if defined(TARGET_ARM)
4196 { "old-param", 0, QEMU_OPTION_old_param
},
4198 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4199 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4200 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4201 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4202 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4206 /* password input */
4208 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4213 if (!bdrv_is_encrypted(bs
))
4216 term_printf("%s is encrypted.\n", name
);
4217 for(i
= 0; i
< 3; i
++) {
4218 monitor_readline("Password: ", 1, password
, sizeof(password
));
4219 if (bdrv_set_key(bs
, password
) == 0)
4221 term_printf("invalid password\n");
4226 static BlockDriverState
*get_bdrv(int index
)
4228 if (index
> nb_drives
)
4230 return drives_table
[index
].bdrv
;
4233 static void read_passwords(void)
4235 BlockDriverState
*bs
;
4238 for(i
= 0; i
< 6; i
++) {
4241 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4246 struct soundhw soundhw
[] = {
4247 #ifdef HAS_AUDIO_CHOICE
4248 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4254 { .init_isa
= pcspk_audio_init
}
4259 "Creative Sound Blaster 16",
4262 { .init_isa
= SB16_init
}
4265 #ifdef CONFIG_CS4231A
4271 { .init_isa
= cs4231a_init
}
4279 "Yamaha YMF262 (OPL3)",
4281 "Yamaha YM3812 (OPL2)",
4285 { .init_isa
= Adlib_init
}
4292 "Gravis Ultrasound GF1",
4295 { .init_isa
= GUS_init
}
4302 "Intel 82801AA AC97 Audio",
4305 { .init_pci
= ac97_init
}
4311 "ENSONIQ AudioPCI ES1370",
4314 { .init_pci
= es1370_init
}
4318 { NULL
, NULL
, 0, 0, { NULL
} }
4321 static void select_soundhw (const char *optarg
)
4325 if (*optarg
== '?') {
4328 printf ("Valid sound card names (comma separated):\n");
4329 for (c
= soundhw
; c
->name
; ++c
) {
4330 printf ("%-11s %s\n", c
->name
, c
->descr
);
4332 printf ("\n-soundhw all will enable all of the above\n");
4333 exit (*optarg
!= '?');
4341 if (!strcmp (optarg
, "all")) {
4342 for (c
= soundhw
; c
->name
; ++c
) {
4350 e
= strchr (p
, ',');
4351 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4353 for (c
= soundhw
; c
->name
; ++c
) {
4354 if (!strncmp (c
->name
, p
, l
)) {
4363 "Unknown sound card name (too big to show)\n");
4366 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4371 p
+= l
+ (e
!= NULL
);
4375 goto show_valid_cards
;
4380 static void select_vgahw (const char *p
)
4384 if (strstart(p
, "std", &opts
)) {
4385 cirrus_vga_enabled
= 0;
4387 } else if (strstart(p
, "cirrus", &opts
)) {
4388 cirrus_vga_enabled
= 1;
4390 } else if (strstart(p
, "vmware", &opts
)) {
4391 cirrus_vga_enabled
= 0;
4395 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4399 const char *nextopt
;
4401 if (strstart(opts
, ",retrace=", &nextopt
)) {
4403 if (strstart(opts
, "dumb", &nextopt
))
4404 vga_retrace_method
= VGA_RETRACE_DUMB
;
4405 else if (strstart(opts
, "precise", &nextopt
))
4406 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4407 else goto invalid_vga
;
4408 } else goto invalid_vga
;
4414 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4416 exit(STATUS_CONTROL_C_EXIT
);
4421 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4425 if(strlen(str
) != 36)
4428 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4429 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4430 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4438 #define MAX_NET_CLIENTS 32
4442 static void termsig_handler(int signal
)
4444 qemu_system_shutdown_request();
4447 static void termsig_setup(void)
4449 struct sigaction act
;
4451 memset(&act
, 0, sizeof(act
));
4452 act
.sa_handler
= termsig_handler
;
4453 sigaction(SIGINT
, &act
, NULL
);
4454 sigaction(SIGHUP
, &act
, NULL
);
4455 sigaction(SIGTERM
, &act
, NULL
);
4460 int main(int argc
, char **argv
, char **envp
)
4462 #ifdef CONFIG_GDBSTUB
4464 const char *gdbstub_port
;
4466 uint32_t boot_devices_bitmap
= 0;
4468 int snapshot
, linux_boot
, net_boot
;
4469 const char *initrd_filename
;
4470 const char *kernel_filename
, *kernel_cmdline
;
4471 const char *boot_devices
= "";
4472 DisplayState
*ds
= &display_state
;
4473 int cyls
, heads
, secs
, translation
;
4474 const char *net_clients
[MAX_NET_CLIENTS
];
4476 const char *bt_opts
[MAX_BT_CMDLINE
];
4480 const char *r
, *optarg
;
4481 CharDriverState
*monitor_hd
;
4482 const char *monitor_device
;
4483 const char *serial_devices
[MAX_SERIAL_PORTS
];
4484 int serial_device_index
;
4485 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4486 int parallel_device_index
;
4487 const char *loadvm
= NULL
;
4488 QEMUMachine
*machine
;
4489 const char *cpu_model
;
4490 const char *usb_devices
[MAX_USB_CMDLINE
];
4491 int usb_devices_index
;
4494 const char *pid_file
= NULL
;
4496 const char *incoming
= NULL
;
4498 qemu_cache_utils_init(envp
);
4500 LIST_INIT (&vm_change_state_head
);
4503 struct sigaction act
;
4504 sigfillset(&act
.sa_mask
);
4506 act
.sa_handler
= SIG_IGN
;
4507 sigaction(SIGPIPE
, &act
, NULL
);
4510 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4511 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4512 QEMU to run on a single CPU */
4517 h
= GetCurrentProcess();
4518 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4519 for(i
= 0; i
< 32; i
++) {
4520 if (mask
& (1 << i
))
4525 SetProcessAffinityMask(h
, mask
);
4531 register_machines();
4532 machine
= first_machine
;
4534 initrd_filename
= NULL
;
4536 vga_ram_size
= VGA_RAM_SIZE
;
4537 #ifdef CONFIG_GDBSTUB
4539 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4544 kernel_filename
= NULL
;
4545 kernel_cmdline
= "";
4546 cyls
= heads
= secs
= 0;
4547 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4548 monitor_device
= "vc";
4550 serial_devices
[0] = "vc:80Cx24C";
4551 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4552 serial_devices
[i
] = NULL
;
4553 serial_device_index
= 0;
4555 parallel_devices
[0] = "vc:640x480";
4556 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4557 parallel_devices
[i
] = NULL
;
4558 parallel_device_index
= 0;
4560 usb_devices_index
= 0;
4579 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4581 const QEMUOption
*popt
;
4584 /* Treat --foo the same as -foo. */
4587 popt
= qemu_options
;
4590 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4594 if (!strcmp(popt
->name
, r
+ 1))
4598 if (popt
->flags
& HAS_ARG
) {
4599 if (optind
>= argc
) {
4600 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4604 optarg
= argv
[optind
++];
4609 switch(popt
->index
) {
4611 machine
= find_machine(optarg
);
4614 printf("Supported machines are:\n");
4615 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4616 printf("%-10s %s%s\n",
4618 m
== first_machine
? " (default)" : "");
4620 exit(*optarg
!= '?');
4623 case QEMU_OPTION_cpu
:
4624 /* hw initialization will check this */
4625 if (*optarg
== '?') {
4626 /* XXX: implement xxx_cpu_list for targets that still miss it */
4627 #if defined(cpu_list)
4628 cpu_list(stdout
, &fprintf
);
4635 case QEMU_OPTION_initrd
:
4636 initrd_filename
= optarg
;
4638 case QEMU_OPTION_hda
:
4640 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4642 hda_index
= drive_add(optarg
, HD_ALIAS
4643 ",cyls=%d,heads=%d,secs=%d%s",
4644 0, cyls
, heads
, secs
,
4645 translation
== BIOS_ATA_TRANSLATION_LBA
?
4647 translation
== BIOS_ATA_TRANSLATION_NONE
?
4648 ",trans=none" : "");
4650 case QEMU_OPTION_hdb
:
4651 case QEMU_OPTION_hdc
:
4652 case QEMU_OPTION_hdd
:
4653 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4655 case QEMU_OPTION_drive
:
4656 drive_add(NULL
, "%s", optarg
);
4658 case QEMU_OPTION_mtdblock
:
4659 drive_add(optarg
, MTD_ALIAS
);
4661 case QEMU_OPTION_sd
:
4662 drive_add(optarg
, SD_ALIAS
);
4664 case QEMU_OPTION_pflash
:
4665 drive_add(optarg
, PFLASH_ALIAS
);
4667 case QEMU_OPTION_snapshot
:
4670 case QEMU_OPTION_hdachs
:
4674 cyls
= strtol(p
, (char **)&p
, 0);
4675 if (cyls
< 1 || cyls
> 16383)
4680 heads
= strtol(p
, (char **)&p
, 0);
4681 if (heads
< 1 || heads
> 16)
4686 secs
= strtol(p
, (char **)&p
, 0);
4687 if (secs
< 1 || secs
> 63)
4691 if (!strcmp(p
, "none"))
4692 translation
= BIOS_ATA_TRANSLATION_NONE
;
4693 else if (!strcmp(p
, "lba"))
4694 translation
= BIOS_ATA_TRANSLATION_LBA
;
4695 else if (!strcmp(p
, "auto"))
4696 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4699 } else if (*p
!= '\0') {
4701 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4704 if (hda_index
!= -1)
4705 snprintf(drives_opt
[hda_index
].opt
,
4706 sizeof(drives_opt
[hda_index
].opt
),
4707 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4708 0, cyls
, heads
, secs
,
4709 translation
== BIOS_ATA_TRANSLATION_LBA
?
4711 translation
== BIOS_ATA_TRANSLATION_NONE
?
4712 ",trans=none" : "");
4715 case QEMU_OPTION_nographic
:
4718 #ifdef CONFIG_CURSES
4719 case QEMU_OPTION_curses
:
4723 case QEMU_OPTION_portrait
:
4726 case QEMU_OPTION_kernel
:
4727 kernel_filename
= optarg
;
4729 case QEMU_OPTION_append
:
4730 kernel_cmdline
= optarg
;
4732 case QEMU_OPTION_cdrom
:
4733 drive_add(optarg
, CDROM_ALIAS
);
4735 case QEMU_OPTION_boot
:
4736 boot_devices
= optarg
;
4737 /* We just do some generic consistency checks */
4739 /* Could easily be extended to 64 devices if needed */
4742 boot_devices_bitmap
= 0;
4743 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4744 /* Allowed boot devices are:
4745 * a b : floppy disk drives
4746 * c ... f : IDE disk drives
4747 * g ... m : machine implementation dependant drives
4748 * n ... p : network devices
4749 * It's up to each machine implementation to check
4750 * if the given boot devices match the actual hardware
4751 * implementation and firmware features.
4753 if (*p
< 'a' || *p
> 'q') {
4754 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4757 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4759 "Boot device '%c' was given twice\n",*p
);
4762 boot_devices_bitmap
|= 1 << (*p
- 'a');
4766 case QEMU_OPTION_fda
:
4767 case QEMU_OPTION_fdb
:
4768 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4771 case QEMU_OPTION_no_fd_bootchk
:
4775 case QEMU_OPTION_net
:
4776 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4777 fprintf(stderr
, "qemu: too many network clients\n");
4780 net_clients
[nb_net_clients
] = optarg
;
4784 case QEMU_OPTION_tftp
:
4785 tftp_prefix
= optarg
;
4787 case QEMU_OPTION_bootp
:
4788 bootp_filename
= optarg
;
4791 case QEMU_OPTION_smb
:
4792 net_slirp_smb(optarg
);
4795 case QEMU_OPTION_redir
:
4796 net_slirp_redir(optarg
);
4799 case QEMU_OPTION_bt
:
4800 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4801 fprintf(stderr
, "qemu: too many bluetooth options\n");
4804 bt_opts
[nb_bt_opts
++] = optarg
;
4807 case QEMU_OPTION_audio_help
:
4811 case QEMU_OPTION_soundhw
:
4812 select_soundhw (optarg
);
4818 case QEMU_OPTION_m
: {
4822 value
= strtoul(optarg
, &ptr
, 10);
4824 case 0: case 'M': case 'm':
4831 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4835 /* On 32-bit hosts, QEMU is limited by virtual address space */
4836 if (value
> (2047 << 20)
4838 && HOST_LONG_BITS
== 32
4841 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4844 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4845 fprintf(stderr
, "qemu: ram size too large\n");
4854 const CPULogItem
*item
;
4856 mask
= cpu_str_to_log_mask(optarg
);
4858 printf("Log items (comma separated):\n");
4859 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4860 printf("%-10s %s\n", item
->name
, item
->help
);
4867 #ifdef CONFIG_GDBSTUB
4872 gdbstub_port
= optarg
;
4878 case QEMU_OPTION_bios
:
4885 keyboard_layout
= optarg
;
4887 case QEMU_OPTION_localtime
:
4890 case QEMU_OPTION_vga
:
4891 select_vgahw (optarg
);
4898 w
= strtol(p
, (char **)&p
, 10);
4901 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4907 h
= strtol(p
, (char **)&p
, 10);
4912 depth
= strtol(p
, (char **)&p
, 10);
4913 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4914 depth
!= 24 && depth
!= 32)
4916 } else if (*p
== '\0') {
4917 depth
= graphic_depth
;
4924 graphic_depth
= depth
;
4927 case QEMU_OPTION_echr
:
4930 term_escape_char
= strtol(optarg
, &r
, 0);
4932 printf("Bad argument to echr\n");
4935 case QEMU_OPTION_monitor
:
4936 monitor_device
= optarg
;
4938 case QEMU_OPTION_serial
:
4939 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4940 fprintf(stderr
, "qemu: too many serial ports\n");
4943 serial_devices
[serial_device_index
] = optarg
;
4944 serial_device_index
++;
4946 case QEMU_OPTION_parallel
:
4947 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4948 fprintf(stderr
, "qemu: too many parallel ports\n");
4951 parallel_devices
[parallel_device_index
] = optarg
;
4952 parallel_device_index
++;
4954 case QEMU_OPTION_loadvm
:
4957 case QEMU_OPTION_full_screen
:
4961 case QEMU_OPTION_no_frame
:
4964 case QEMU_OPTION_alt_grab
:
4967 case QEMU_OPTION_no_quit
:
4971 case QEMU_OPTION_pidfile
:
4975 case QEMU_OPTION_win2k_hack
:
4976 win2k_install_hack
= 1;
4980 case QEMU_OPTION_no_kqemu
:
4983 case QEMU_OPTION_kernel_kqemu
:
4988 case QEMU_OPTION_enable_kvm
:
4995 case QEMU_OPTION_usb
:
4998 case QEMU_OPTION_usbdevice
:
5000 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5001 fprintf(stderr
, "Too many USB devices\n");
5004 usb_devices
[usb_devices_index
] = optarg
;
5005 usb_devices_index
++;
5007 case QEMU_OPTION_smp
:
5008 smp_cpus
= atoi(optarg
);
5010 fprintf(stderr
, "Invalid number of CPUs\n");
5014 case QEMU_OPTION_vnc
:
5015 vnc_display
= optarg
;
5017 case QEMU_OPTION_no_acpi
:
5020 case QEMU_OPTION_no_reboot
:
5023 case QEMU_OPTION_no_shutdown
:
5026 case QEMU_OPTION_show_cursor
:
5029 case QEMU_OPTION_uuid
:
5030 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5031 fprintf(stderr
, "Fail to parse UUID string."
5032 " Wrong format.\n");
5036 case QEMU_OPTION_daemonize
:
5039 case QEMU_OPTION_option_rom
:
5040 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5041 fprintf(stderr
, "Too many option ROMs\n");
5044 option_rom
[nb_option_roms
] = optarg
;
5047 case QEMU_OPTION_semihosting
:
5048 semihosting_enabled
= 1;
5050 case QEMU_OPTION_name
:
5054 case QEMU_OPTION_prom_env
:
5055 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5056 fprintf(stderr
, "Too many prom variables\n");
5059 prom_envs
[nb_prom_envs
] = optarg
;
5064 case QEMU_OPTION_old_param
:
5068 case QEMU_OPTION_clock
:
5069 configure_alarms(optarg
);
5071 case QEMU_OPTION_startdate
:
5074 time_t rtc_start_date
;
5075 if (!strcmp(optarg
, "now")) {
5076 rtc_date_offset
= -1;
5078 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5086 } else if (sscanf(optarg
, "%d-%d-%d",
5089 &tm
.tm_mday
) == 3) {
5098 rtc_start_date
= mktimegm(&tm
);
5099 if (rtc_start_date
== -1) {
5101 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5102 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5105 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5109 case QEMU_OPTION_tb_size
:
5110 tb_size
= strtol(optarg
, NULL
, 0);
5114 case QEMU_OPTION_icount
:
5116 if (strcmp(optarg
, "auto") == 0) {
5117 icount_time_shift
= -1;
5119 icount_time_shift
= strtol(optarg
, NULL
, 0);
5122 case QEMU_OPTION_incoming
:
5129 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5130 if (kvm_allowed
&& kqemu_allowed
) {
5132 "You can not enable both KVM and kqemu at the same time\n");
5137 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5138 if (smp_cpus
> machine
->max_cpus
) {
5139 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5140 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5146 if (serial_device_index
== 0)
5147 serial_devices
[0] = "stdio";
5148 if (parallel_device_index
== 0)
5149 parallel_devices
[0] = "null";
5150 if (strncmp(monitor_device
, "vc", 2) == 0)
5151 monitor_device
= "stdio";
5158 if (pipe(fds
) == -1)
5169 len
= read(fds
[0], &status
, 1);
5170 if (len
== -1 && (errno
== EINTR
))
5175 else if (status
== 1) {
5176 fprintf(stderr
, "Could not acquire pidfile\n");
5193 signal(SIGTSTP
, SIG_IGN
);
5194 signal(SIGTTOU
, SIG_IGN
);
5195 signal(SIGTTIN
, SIG_IGN
);
5199 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5202 write(fds
[1], &status
, 1);
5204 fprintf(stderr
, "Could not acquire pid file\n");
5212 linux_boot
= (kernel_filename
!= NULL
);
5213 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5215 if (!linux_boot
&& net_boot
== 0 &&
5216 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5219 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5220 fprintf(stderr
, "-append only allowed with -kernel option\n");
5224 if (!linux_boot
&& initrd_filename
!= NULL
) {
5225 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5229 /* boot to floppy or the default cd if no hard disk defined yet */
5230 if (!boot_devices
[0]) {
5231 boot_devices
= "cad";
5233 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5236 if (init_timer_alarm() < 0) {
5237 fprintf(stderr
, "could not initialize alarm timer\n");
5240 if (use_icount
&& icount_time_shift
< 0) {
5242 /* 125MIPS seems a reasonable initial guess at the guest speed.
5243 It will be corrected fairly quickly anyway. */
5244 icount_time_shift
= 3;
5245 init_icount_adjust();
5252 /* init network clients */
5253 if (nb_net_clients
== 0) {
5254 /* if no clients, we use a default config */
5255 net_clients
[nb_net_clients
++] = "nic";
5257 net_clients
[nb_net_clients
++] = "user";
5261 for(i
= 0;i
< nb_net_clients
; i
++) {
5262 if (net_client_parse(net_clients
[i
]) < 0)
5268 /* XXX: this should be moved in the PC machine instantiation code */
5269 if (net_boot
!= 0) {
5271 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5272 const char *model
= nd_table
[i
].model
;
5274 if (net_boot
& (1 << i
)) {
5277 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5278 if (get_image_size(buf
) > 0) {
5279 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5280 fprintf(stderr
, "Too many option ROMs\n");
5283 option_rom
[nb_option_roms
] = strdup(buf
);
5290 fprintf(stderr
, "No valid PXE rom found for network device\n");
5296 /* init the bluetooth world */
5297 for (i
= 0; i
< nb_bt_opts
; i
++)
5298 if (bt_parse(bt_opts
[i
]))
5301 /* init the memory */
5302 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5304 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5306 if (ram_size
< phys_ram_size
) {
5307 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5308 machine
->name
, (unsigned long long) phys_ram_size
);
5312 phys_ram_size
= ram_size
;
5314 ram_size
= phys_ram_size
;
5317 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5319 phys_ram_size
+= ram_size
;
5322 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5323 if (!phys_ram_base
) {
5324 fprintf(stderr
, "Could not allocate physical memory\n");
5328 /* init the dynamic translator */
5329 cpu_exec_init_all(tb_size
* 1024 * 1024);
5333 /* we always create the cdrom drive, even if no disk is there */
5335 if (nb_drives_opt
< MAX_DRIVES
)
5336 drive_add(NULL
, CDROM_ALIAS
);
5338 /* we always create at least one floppy */
5340 if (nb_drives_opt
< MAX_DRIVES
)
5341 drive_add(NULL
, FD_ALIAS
, 0);
5343 /* we always create one sd slot, even if no card is in it */
5345 if (nb_drives_opt
< MAX_DRIVES
)
5346 drive_add(NULL
, SD_ALIAS
);
5348 /* open the virtual block devices */
5350 for(i
= 0; i
< nb_drives_opt
; i
++)
5351 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5354 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5355 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5358 memset(&display_state
, 0, sizeof(display_state
));
5361 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5364 /* nearly nothing to do */
5365 dumb_display_init(ds
);
5366 } else if (vnc_display
!= NULL
) {
5367 vnc_display_init(ds
);
5368 if (vnc_display_open(ds
, vnc_display
) < 0)
5371 #if defined(CONFIG_CURSES)
5373 curses_display_init(ds
, full_screen
);
5377 #if defined(CONFIG_SDL)
5378 sdl_display_init(ds
, full_screen
, no_frame
);
5379 #elif defined(CONFIG_COCOA)
5380 cocoa_display_init(ds
, full_screen
);
5382 dumb_display_init(ds
);
5387 /* must be after terminal init, SDL library changes signal handlers */
5391 /* Maintain compatibility with multiple stdio monitors */
5392 if (!strcmp(monitor_device
,"stdio")) {
5393 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5394 const char *devname
= serial_devices
[i
];
5395 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5396 monitor_device
= NULL
;
5398 } else if (devname
&& !strcmp(devname
,"stdio")) {
5399 monitor_device
= NULL
;
5400 serial_devices
[i
] = "mon:stdio";
5405 if (monitor_device
) {
5406 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5408 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5411 monitor_init(monitor_hd
, !nographic
);
5414 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5415 const char *devname
= serial_devices
[i
];
5416 if (devname
&& strcmp(devname
, "none")) {
5418 snprintf(label
, sizeof(label
), "serial%d", i
);
5419 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5420 if (!serial_hds
[i
]) {
5421 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5425 if (strstart(devname
, "vc", 0))
5426 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5430 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5431 const char *devname
= parallel_devices
[i
];
5432 if (devname
&& strcmp(devname
, "none")) {
5434 snprintf(label
, sizeof(label
), "parallel%d", i
);
5435 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5436 if (!parallel_hds
[i
]) {
5437 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5441 if (strstart(devname
, "vc", 0))
5442 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5446 if (kvm_enabled()) {
5449 ret
= kvm_init(smp_cpus
);
5451 fprintf(stderr
, "failed to initialize KVM\n");
5456 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5457 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5459 /* init USB devices */
5461 for(i
= 0; i
< usb_devices_index
; i
++) {
5462 if (usb_device_add(usb_devices
[i
]) < 0) {
5463 fprintf(stderr
, "Warning: could not add USB device %s\n",
5469 if (display_state
.dpy_refresh
) {
5470 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5471 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5474 #ifdef CONFIG_GDBSTUB
5476 /* XXX: use standard host:port notation and modify options
5478 if (gdbserver_start(gdbstub_port
) < 0) {
5479 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5490 autostart
= 0; /* fixme how to deal with -daemonize */
5491 qemu_start_incoming_migration(incoming
);
5495 /* XXX: simplify init */
5508 len
= write(fds
[1], &status
, 1);
5509 if (len
== -1 && (errno
== EINTR
))
5516 TFR(fd
= open("/dev/null", O_RDWR
));