4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
39 #include "cache-utils.h"
41 #include "audio/audio.h"
42 #include "migration.h"
55 #include <sys/times.h>
59 #include <sys/ioctl.h>
60 #include <sys/resource.h>
61 #include <sys/socket.h>
62 #include <netinet/in.h>
64 #if defined(__NetBSD__)
65 #include <net/if_tap.h>
68 #include <linux/if_tun.h>
70 #include <arpa/inet.h>
73 #include <sys/select.h>
81 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
82 #include <freebsd/stdlib.h>
87 #include <linux/rtc.h>
89 /* For the benefit of older linux systems which don't supply it,
90 we use a local copy of hpet.h. */
91 /* #include <linux/hpet.h> */
94 #include <linux/ppdev.h>
95 #include <linux/parport.h>
99 #include <sys/ethernet.h>
100 #include <sys/sockio.h>
101 #include <netinet/arp.h>
102 #include <netinet/in.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_icmp.h> // must come after ip.h
106 #include <netinet/udp.h>
107 #include <netinet/tcp.h>
115 #include "qemu_socket.h"
117 #if defined(CONFIG_SLIRP)
118 #include "libslirp.h"
121 #if defined(__OpenBSD__)
125 #if defined(CONFIG_VDE)
126 #include <libvdeplug.h>
131 #include <sys/timeb.h>
132 #include <mmsystem.h>
133 #define getopt_long_only getopt_long
134 #define memalign(align, size) malloc(size)
141 #endif /* CONFIG_SDL */
145 #define main qemu_main
146 #endif /* CONFIG_COCOA */
150 #include "exec-all.h"
152 //#define DEBUG_UNUSED_IOPORT
153 //#define DEBUG_IOPORT
155 //#define DEBUG_SLIRP
158 #define DEFAULT_RAM_SIZE 144
160 #define DEFAULT_RAM_SIZE 128
163 /* Max number of USB devices that can be specified on the commandline. */
164 #define MAX_USB_CMDLINE 8
166 /* Max number of bluetooth switches on the commandline. */
167 #define MAX_BT_CMDLINE 10
169 /* XXX: use a two level table to limit memory usage */
170 #define MAX_IOPORTS 65536
172 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
173 const char *bios_name
= NULL
;
174 static void *ioport_opaque
[MAX_IOPORTS
];
175 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
176 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
177 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
178 to store the VM snapshots */
179 DriveInfo drives_table
[MAX_DRIVES
+1];
181 static int vga_ram_size
;
182 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
183 DisplayState display_state
;
186 const char* keyboard_layout
= NULL
;
187 int64_t ticks_per_sec
;
190 NICInfo nd_table
[MAX_NICS
];
192 static int rtc_utc
= 1;
193 static int rtc_date_offset
= -1; /* -1 means no change */
194 int cirrus_vga_enabled
= 1;
195 int vmsvga_enabled
= 0;
197 int graphic_width
= 1024;
198 int graphic_height
= 768;
199 int graphic_depth
= 8;
201 int graphic_width
= 800;
202 int graphic_height
= 600;
203 int graphic_depth
= 15;
205 static int full_screen
= 0;
207 static int no_frame
= 0;
210 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
211 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 int win2k_install_hack
= 0;
217 const char *vnc_display
;
218 int acpi_enabled
= 1;
224 int graphic_rotate
= 0;
226 const char *option_rom
[MAX_OPTION_ROMS
];
228 int semihosting_enabled
= 0;
232 const char *qemu_name
;
234 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
235 unsigned int nb_prom_envs
= 0;
236 const char *prom_envs
[MAX_PROM_ENVS
];
238 static int nb_drives_opt
;
239 static struct drive_opt
{
242 } drives_opt
[MAX_DRIVES
];
244 static CPUState
*cur_cpu
;
245 static CPUState
*next_cpu
;
246 static int event_pending
= 1;
247 /* Conversion factor from emulated instructions to virtual clock ticks. */
248 static int icount_time_shift
;
249 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
250 #define MAX_ICOUNT_SHIFT 10
251 /* Compensate for varying guest execution speed. */
252 static int64_t qemu_icount_bias
;
253 static QEMUTimer
*icount_rt_timer
;
254 static QEMUTimer
*icount_vm_timer
;
256 uint8_t qemu_uuid
[16];
258 /***********************************************************/
259 /* x86 ISA bus support */
261 target_phys_addr_t isa_mem_base
= 0;
264 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
265 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
267 static uint32_t ioport_read(int index
, uint32_t address
)
269 static IOPortReadFunc
*default_func
[3] = {
270 default_ioport_readb
,
271 default_ioport_readw
,
274 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
276 func
= default_func
[index
];
277 return func(ioport_opaque
[address
], address
);
280 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
282 static IOPortWriteFunc
*default_func
[3] = {
283 default_ioport_writeb
,
284 default_ioport_writew
,
285 default_ioport_writel
287 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
289 func
= default_func
[index
];
290 func(ioport_opaque
[address
], address
, data
);
293 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
295 #ifdef DEBUG_UNUSED_IOPORT
296 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
301 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
303 #ifdef DEBUG_UNUSED_IOPORT
304 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
308 /* default is to make two byte accesses */
309 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
312 data
= ioport_read(0, address
);
313 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
314 data
|= ioport_read(0, address
) << 8;
318 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
320 ioport_write(0, address
, data
& 0xff);
321 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
322 ioport_write(0, address
, (data
>> 8) & 0xff);
325 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
327 #ifdef DEBUG_UNUSED_IOPORT
328 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
333 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
335 #ifdef DEBUG_UNUSED_IOPORT
336 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
340 /* size is the word size in byte */
341 int register_ioport_read(int start
, int length
, int size
,
342 IOPortReadFunc
*func
, void *opaque
)
348 } else if (size
== 2) {
350 } else if (size
== 4) {
353 hw_error("register_ioport_read: invalid size");
356 for(i
= start
; i
< start
+ length
; i
+= size
) {
357 ioport_read_table
[bsize
][i
] = func
;
358 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
359 hw_error("register_ioport_read: invalid opaque");
360 ioport_opaque
[i
] = opaque
;
365 /* size is the word size in byte */
366 int register_ioport_write(int start
, int length
, int size
,
367 IOPortWriteFunc
*func
, void *opaque
)
373 } else if (size
== 2) {
375 } else if (size
== 4) {
378 hw_error("register_ioport_write: invalid size");
381 for(i
= start
; i
< start
+ length
; i
+= size
) {
382 ioport_write_table
[bsize
][i
] = func
;
383 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
384 hw_error("register_ioport_write: invalid opaque");
385 ioport_opaque
[i
] = opaque
;
390 void isa_unassign_ioport(int start
, int length
)
394 for(i
= start
; i
< start
+ length
; i
++) {
395 ioport_read_table
[0][i
] = default_ioport_readb
;
396 ioport_read_table
[1][i
] = default_ioport_readw
;
397 ioport_read_table
[2][i
] = default_ioport_readl
;
399 ioport_write_table
[0][i
] = default_ioport_writeb
;
400 ioport_write_table
[1][i
] = default_ioport_writew
;
401 ioport_write_table
[2][i
] = default_ioport_writel
;
405 /***********************************************************/
407 void cpu_outb(CPUState
*env
, int addr
, int val
)
410 if (loglevel
& CPU_LOG_IOPORT
)
411 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
413 ioport_write(0, addr
, val
);
416 env
->last_io_time
= cpu_get_time_fast();
420 void cpu_outw(CPUState
*env
, int addr
, int val
)
423 if (loglevel
& CPU_LOG_IOPORT
)
424 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
426 ioport_write(1, addr
, val
);
429 env
->last_io_time
= cpu_get_time_fast();
433 void cpu_outl(CPUState
*env
, int addr
, int val
)
436 if (loglevel
& CPU_LOG_IOPORT
)
437 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
439 ioport_write(2, addr
, val
);
442 env
->last_io_time
= cpu_get_time_fast();
446 int cpu_inb(CPUState
*env
, int addr
)
449 val
= ioport_read(0, addr
);
451 if (loglevel
& CPU_LOG_IOPORT
)
452 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
456 env
->last_io_time
= cpu_get_time_fast();
461 int cpu_inw(CPUState
*env
, int addr
)
464 val
= ioport_read(1, addr
);
466 if (loglevel
& CPU_LOG_IOPORT
)
467 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
471 env
->last_io_time
= cpu_get_time_fast();
476 int cpu_inl(CPUState
*env
, int addr
)
479 val
= ioport_read(2, addr
);
481 if (loglevel
& CPU_LOG_IOPORT
)
482 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
486 env
->last_io_time
= cpu_get_time_fast();
491 /***********************************************************/
492 void hw_error(const char *fmt
, ...)
498 fprintf(stderr
, "qemu: hardware error: ");
499 vfprintf(stderr
, fmt
, ap
);
500 fprintf(stderr
, "\n");
501 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
502 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
504 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
506 cpu_dump_state(env
, stderr
, fprintf
, 0);
516 static QEMUBalloonEvent
*qemu_balloon_event
;
517 void *qemu_balloon_event_opaque
;
519 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
521 qemu_balloon_event
= func
;
522 qemu_balloon_event_opaque
= opaque
;
525 void qemu_balloon(ram_addr_t target
)
527 if (qemu_balloon_event
)
528 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
531 ram_addr_t
qemu_balloon_status(void)
533 if (qemu_balloon_event
)
534 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
538 /***********************************************************/
541 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
542 static void *qemu_put_kbd_event_opaque
;
543 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
544 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
546 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
548 qemu_put_kbd_event_opaque
= opaque
;
549 qemu_put_kbd_event
= func
;
552 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
553 void *opaque
, int absolute
,
556 QEMUPutMouseEntry
*s
, *cursor
;
558 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
562 s
->qemu_put_mouse_event
= func
;
563 s
->qemu_put_mouse_event_opaque
= opaque
;
564 s
->qemu_put_mouse_event_absolute
= absolute
;
565 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
568 if (!qemu_put_mouse_event_head
) {
569 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
573 cursor
= qemu_put_mouse_event_head
;
574 while (cursor
->next
!= NULL
)
575 cursor
= cursor
->next
;
578 qemu_put_mouse_event_current
= s
;
583 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
585 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
587 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
590 cursor
= qemu_put_mouse_event_head
;
591 while (cursor
!= NULL
&& cursor
!= entry
) {
593 cursor
= cursor
->next
;
596 if (cursor
== NULL
) // does not exist or list empty
598 else if (prev
== NULL
) { // entry is head
599 qemu_put_mouse_event_head
= cursor
->next
;
600 if (qemu_put_mouse_event_current
== entry
)
601 qemu_put_mouse_event_current
= cursor
->next
;
602 qemu_free(entry
->qemu_put_mouse_event_name
);
607 prev
->next
= entry
->next
;
609 if (qemu_put_mouse_event_current
== entry
)
610 qemu_put_mouse_event_current
= prev
;
612 qemu_free(entry
->qemu_put_mouse_event_name
);
616 void kbd_put_keycode(int keycode
)
618 if (qemu_put_kbd_event
) {
619 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
623 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
625 QEMUPutMouseEvent
*mouse_event
;
626 void *mouse_event_opaque
;
629 if (!qemu_put_mouse_event_current
) {
634 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
636 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
639 if (graphic_rotate
) {
640 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
643 width
= graphic_width
- 1;
644 mouse_event(mouse_event_opaque
,
645 width
- dy
, dx
, dz
, buttons_state
);
647 mouse_event(mouse_event_opaque
,
648 dx
, dy
, dz
, buttons_state
);
652 int kbd_mouse_is_absolute(void)
654 if (!qemu_put_mouse_event_current
)
657 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
660 void do_info_mice(void)
662 QEMUPutMouseEntry
*cursor
;
665 if (!qemu_put_mouse_event_head
) {
666 term_printf("No mouse devices connected\n");
670 term_printf("Mouse devices available:\n");
671 cursor
= qemu_put_mouse_event_head
;
672 while (cursor
!= NULL
) {
673 term_printf("%c Mouse #%d: %s\n",
674 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
675 index
, cursor
->qemu_put_mouse_event_name
);
677 cursor
= cursor
->next
;
681 void do_mouse_set(int index
)
683 QEMUPutMouseEntry
*cursor
;
686 if (!qemu_put_mouse_event_head
) {
687 term_printf("No mouse devices connected\n");
691 cursor
= qemu_put_mouse_event_head
;
692 while (cursor
!= NULL
&& index
!= i
) {
694 cursor
= cursor
->next
;
698 qemu_put_mouse_event_current
= cursor
;
700 term_printf("Mouse at given index not found\n");
703 /* compute with 96 bit intermediate result: (a*b)/c */
704 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
709 #ifdef WORDS_BIGENDIAN
719 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
720 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
723 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
727 /***********************************************************/
728 /* real time host monotonic timer */
730 #define QEMU_TIMER_BASE 1000000000LL
734 static int64_t clock_freq
;
736 static void init_get_clock(void)
740 ret
= QueryPerformanceFrequency(&freq
);
742 fprintf(stderr
, "Could not calibrate ticks\n");
745 clock_freq
= freq
.QuadPart
;
748 static int64_t get_clock(void)
751 QueryPerformanceCounter(&ti
);
752 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
757 static int use_rt_clock
;
759 static void init_get_clock(void)
762 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
765 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
772 static int64_t get_clock(void)
774 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
777 clock_gettime(CLOCK_MONOTONIC
, &ts
);
778 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
782 /* XXX: using gettimeofday leads to problems if the date
783 changes, so it should be avoided. */
785 gettimeofday(&tv
, NULL
);
786 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
791 /* Return the virtual CPU time, based on the instruction counter. */
792 static int64_t cpu_get_icount(void)
795 CPUState
*env
= cpu_single_env
;;
796 icount
= qemu_icount
;
799 fprintf(stderr
, "Bad clock read\n");
800 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
802 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
805 /***********************************************************/
806 /* guest cycle counter */
808 static int64_t cpu_ticks_prev
;
809 static int64_t cpu_ticks_offset
;
810 static int64_t cpu_clock_offset
;
811 static int cpu_ticks_enabled
;
813 /* return the host CPU cycle counter and handle stop/restart */
814 int64_t cpu_get_ticks(void)
817 return cpu_get_icount();
819 if (!cpu_ticks_enabled
) {
820 return cpu_ticks_offset
;
823 ticks
= cpu_get_real_ticks();
824 if (cpu_ticks_prev
> ticks
) {
825 /* Note: non increasing ticks may happen if the host uses
827 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
829 cpu_ticks_prev
= ticks
;
830 return ticks
+ cpu_ticks_offset
;
834 /* return the host CPU monotonic timer and handle stop/restart */
835 static int64_t cpu_get_clock(void)
838 if (!cpu_ticks_enabled
) {
839 return cpu_clock_offset
;
842 return ti
+ cpu_clock_offset
;
846 /* enable cpu_get_ticks() */
847 void cpu_enable_ticks(void)
849 if (!cpu_ticks_enabled
) {
850 cpu_ticks_offset
-= cpu_get_real_ticks();
851 cpu_clock_offset
-= get_clock();
852 cpu_ticks_enabled
= 1;
856 /* disable cpu_get_ticks() : the clock is stopped. You must not call
857 cpu_get_ticks() after that. */
858 void cpu_disable_ticks(void)
860 if (cpu_ticks_enabled
) {
861 cpu_ticks_offset
= cpu_get_ticks();
862 cpu_clock_offset
= cpu_get_clock();
863 cpu_ticks_enabled
= 0;
867 /***********************************************************/
870 #define QEMU_TIMER_REALTIME 0
871 #define QEMU_TIMER_VIRTUAL 1
875 /* XXX: add frequency */
883 struct QEMUTimer
*next
;
886 struct qemu_alarm_timer
{
890 int (*start
)(struct qemu_alarm_timer
*t
);
891 void (*stop
)(struct qemu_alarm_timer
*t
);
892 void (*rearm
)(struct qemu_alarm_timer
*t
);
896 #define ALARM_FLAG_DYNTICKS 0x1
897 #define ALARM_FLAG_EXPIRED 0x2
899 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
901 return t
->flags
& ALARM_FLAG_DYNTICKS
;
904 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
906 if (!alarm_has_dynticks(t
))
912 /* TODO: MIN_TIMER_REARM_US should be optimized */
913 #define MIN_TIMER_REARM_US 250
915 static struct qemu_alarm_timer
*alarm_timer
;
917 static int alarm_timer_rfd
, alarm_timer_wfd
;
922 struct qemu_alarm_win32
{
926 } alarm_win32_data
= {0, NULL
, -1};
928 static int win32_start_timer(struct qemu_alarm_timer
*t
);
929 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
930 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
934 static int unix_start_timer(struct qemu_alarm_timer
*t
);
935 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
939 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
940 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
941 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
943 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
944 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
946 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
947 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
949 #endif /* __linux__ */
953 /* Correlation between real and virtual time is always going to be
954 fairly approximate, so ignore small variation.
955 When the guest is idle real and virtual time will be aligned in
957 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
959 static void icount_adjust(void)
964 static int64_t last_delta
;
965 /* If the VM is not running, then do nothing. */
969 cur_time
= cpu_get_clock();
970 cur_icount
= qemu_get_clock(vm_clock
);
971 delta
= cur_icount
- cur_time
;
972 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
974 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
975 && icount_time_shift
> 0) {
976 /* The guest is getting too far ahead. Slow time down. */
980 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
981 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
982 /* The guest is getting too far behind. Speed time up. */
986 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
989 static void icount_adjust_rt(void * opaque
)
991 qemu_mod_timer(icount_rt_timer
,
992 qemu_get_clock(rt_clock
) + 1000);
996 static void icount_adjust_vm(void * opaque
)
998 qemu_mod_timer(icount_vm_timer
,
999 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1003 static void init_icount_adjust(void)
1005 /* Have both realtime and virtual time triggers for speed adjustment.
1006 The realtime trigger catches emulated time passing too slowly,
1007 the virtual time trigger catches emulated time passing too fast.
1008 Realtime triggers occur even when idle, so use them less frequently
1009 than VM triggers. */
1010 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1011 qemu_mod_timer(icount_rt_timer
,
1012 qemu_get_clock(rt_clock
) + 1000);
1013 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1014 qemu_mod_timer(icount_vm_timer
,
1015 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1018 static struct qemu_alarm_timer alarm_timers
[] = {
1021 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1022 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1023 /* HPET - if available - is preferred */
1024 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1025 /* ...otherwise try RTC */
1026 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1028 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1030 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1031 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1032 {"win32", 0, win32_start_timer
,
1033 win32_stop_timer
, NULL
, &alarm_win32_data
},
1038 static void show_available_alarms(void)
1042 printf("Available alarm timers, in order of precedence:\n");
1043 for (i
= 0; alarm_timers
[i
].name
; i
++)
1044 printf("%s\n", alarm_timers
[i
].name
);
1047 static void configure_alarms(char const *opt
)
1051 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1054 struct qemu_alarm_timer tmp
;
1056 if (!strcmp(opt
, "?")) {
1057 show_available_alarms();
1063 /* Reorder the array */
1064 name
= strtok(arg
, ",");
1066 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1067 if (!strcmp(alarm_timers
[i
].name
, name
))
1072 fprintf(stderr
, "Unknown clock %s\n", name
);
1081 tmp
= alarm_timers
[i
];
1082 alarm_timers
[i
] = alarm_timers
[cur
];
1083 alarm_timers
[cur
] = tmp
;
1087 name
= strtok(NULL
, ",");
1093 /* Disable remaining timers */
1094 for (i
= cur
; i
< count
; i
++)
1095 alarm_timers
[i
].name
= NULL
;
1097 show_available_alarms();
1102 QEMUClock
*rt_clock
;
1103 QEMUClock
*vm_clock
;
1105 static QEMUTimer
*active_timers
[2];
1107 static QEMUClock
*qemu_new_clock(int type
)
1110 clock
= qemu_mallocz(sizeof(QEMUClock
));
1117 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1121 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1124 ts
->opaque
= opaque
;
1128 void qemu_free_timer(QEMUTimer
*ts
)
1133 /* stop a timer, but do not dealloc it */
1134 void qemu_del_timer(QEMUTimer
*ts
)
1138 /* NOTE: this code must be signal safe because
1139 qemu_timer_expired() can be called from a signal. */
1140 pt
= &active_timers
[ts
->clock
->type
];
1153 /* modify the current timer so that it will be fired when current_time
1154 >= expire_time. The corresponding callback will be called. */
1155 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1161 /* add the timer in the sorted list */
1162 /* NOTE: this code must be signal safe because
1163 qemu_timer_expired() can be called from a signal. */
1164 pt
= &active_timers
[ts
->clock
->type
];
1169 if (t
->expire_time
> expire_time
)
1173 ts
->expire_time
= expire_time
;
1177 /* Rearm if necessary */
1178 if (pt
== &active_timers
[ts
->clock
->type
]) {
1179 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1180 qemu_rearm_alarm_timer(alarm_timer
);
1182 /* Interrupt execution to force deadline recalculation. */
1183 if (use_icount
&& cpu_single_env
) {
1184 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1189 int qemu_timer_pending(QEMUTimer
*ts
)
1192 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1199 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1203 return (timer_head
->expire_time
<= current_time
);
1206 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1212 if (!ts
|| ts
->expire_time
> current_time
)
1214 /* remove timer from the list before calling the callback */
1215 *ptimer_head
= ts
->next
;
1218 /* run the callback (the timer list can be modified) */
1223 int64_t qemu_get_clock(QEMUClock
*clock
)
1225 switch(clock
->type
) {
1226 case QEMU_TIMER_REALTIME
:
1227 return get_clock() / 1000000;
1229 case QEMU_TIMER_VIRTUAL
:
1231 return cpu_get_icount();
1233 return cpu_get_clock();
1238 static void init_timers(void)
1241 ticks_per_sec
= QEMU_TIMER_BASE
;
1242 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1243 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1247 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1249 uint64_t expire_time
;
1251 if (qemu_timer_pending(ts
)) {
1252 expire_time
= ts
->expire_time
;
1256 qemu_put_be64(f
, expire_time
);
1259 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1261 uint64_t expire_time
;
1263 expire_time
= qemu_get_be64(f
);
1264 if (expire_time
!= -1) {
1265 qemu_mod_timer(ts
, expire_time
);
1271 static void timer_save(QEMUFile
*f
, void *opaque
)
1273 if (cpu_ticks_enabled
) {
1274 hw_error("cannot save state if virtual timers are running");
1276 qemu_put_be64(f
, cpu_ticks_offset
);
1277 qemu_put_be64(f
, ticks_per_sec
);
1278 qemu_put_be64(f
, cpu_clock_offset
);
1281 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1283 if (version_id
!= 1 && version_id
!= 2)
1285 if (cpu_ticks_enabled
) {
1288 cpu_ticks_offset
=qemu_get_be64(f
);
1289 ticks_per_sec
=qemu_get_be64(f
);
1290 if (version_id
== 2) {
1291 cpu_clock_offset
=qemu_get_be64(f
);
1297 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1298 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1300 static void host_alarm_handler(int host_signum
)
1304 #define DISP_FREQ 1000
1306 static int64_t delta_min
= INT64_MAX
;
1307 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1309 ti
= qemu_get_clock(vm_clock
);
1310 if (last_clock
!= 0) {
1311 delta
= ti
- last_clock
;
1312 if (delta
< delta_min
)
1314 if (delta
> delta_max
)
1317 if (++count
== DISP_FREQ
) {
1318 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1319 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1320 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1321 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1322 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1324 delta_min
= INT64_MAX
;
1332 if (alarm_has_dynticks(alarm_timer
) ||
1334 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1335 qemu_get_clock(vm_clock
))) ||
1336 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1337 qemu_get_clock(rt_clock
))) {
1338 CPUState
*env
= next_cpu
;
1341 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1342 SetEvent(data
->host_alarm
);
1344 static const char byte
= 0;
1345 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1347 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1350 /* stop the currently executing cpu because a timer occured */
1351 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1353 if (env
->kqemu_enabled
) {
1354 kqemu_cpu_interrupt(env
);
1362 static int64_t qemu_next_deadline(void)
1366 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1367 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1368 qemu_get_clock(vm_clock
);
1370 /* To avoid problems with overflow limit this to 2^32. */
1380 #if defined(__linux__) || defined(_WIN32)
1381 static uint64_t qemu_next_deadline_dyntick(void)
1389 delta
= (qemu_next_deadline() + 999) / 1000;
1391 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1392 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1393 qemu_get_clock(rt_clock
))*1000;
1394 if (rtdelta
< delta
)
1398 if (delta
< MIN_TIMER_REARM_US
)
1399 delta
= MIN_TIMER_REARM_US
;
1407 /* Sets a specific flag */
1408 static int fcntl_setfl(int fd
, int flag
)
1412 flags
= fcntl(fd
, F_GETFL
);
1416 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1422 #if defined(__linux__)
1424 #define RTC_FREQ 1024
1426 static void enable_sigio_timer(int fd
)
1428 struct sigaction act
;
1431 sigfillset(&act
.sa_mask
);
1433 act
.sa_handler
= host_alarm_handler
;
1435 sigaction(SIGIO
, &act
, NULL
);
1436 fcntl_setfl(fd
, O_ASYNC
);
1437 fcntl(fd
, F_SETOWN
, getpid());
1440 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1442 struct hpet_info info
;
1445 fd
= open("/dev/hpet", O_RDONLY
);
1450 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1452 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1453 "error, but for better emulation accuracy type:\n"
1454 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1458 /* Check capabilities */
1459 r
= ioctl(fd
, HPET_INFO
, &info
);
1463 /* Enable periodic mode */
1464 r
= ioctl(fd
, HPET_EPI
, 0);
1465 if (info
.hi_flags
&& (r
< 0))
1468 /* Enable interrupt */
1469 r
= ioctl(fd
, HPET_IE_ON
, 0);
1473 enable_sigio_timer(fd
);
1474 t
->priv
= (void *)(long)fd
;
1482 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1484 int fd
= (long)t
->priv
;
1489 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1492 unsigned long current_rtc_freq
= 0;
1494 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1497 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1498 if (current_rtc_freq
!= RTC_FREQ
&&
1499 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1500 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1501 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1502 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1505 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1511 enable_sigio_timer(rtc_fd
);
1513 t
->priv
= (void *)(long)rtc_fd
;
1518 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1520 int rtc_fd
= (long)t
->priv
;
1525 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1529 struct sigaction act
;
1531 sigfillset(&act
.sa_mask
);
1533 act
.sa_handler
= host_alarm_handler
;
1535 sigaction(SIGALRM
, &act
, NULL
);
1537 ev
.sigev_value
.sival_int
= 0;
1538 ev
.sigev_notify
= SIGEV_SIGNAL
;
1539 ev
.sigev_signo
= SIGALRM
;
1541 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1542 perror("timer_create");
1544 /* disable dynticks */
1545 fprintf(stderr
, "Dynamic Ticks disabled\n");
1550 t
->priv
= (void *)(long)host_timer
;
1555 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1557 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1559 timer_delete(host_timer
);
1562 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1564 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1565 struct itimerspec timeout
;
1566 int64_t nearest_delta_us
= INT64_MAX
;
1569 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1570 !active_timers
[QEMU_TIMER_VIRTUAL
])
1573 nearest_delta_us
= qemu_next_deadline_dyntick();
1575 /* check whether a timer is already running */
1576 if (timer_gettime(host_timer
, &timeout
)) {
1578 fprintf(stderr
, "Internal timer error: aborting\n");
1581 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1582 if (current_us
&& current_us
<= nearest_delta_us
)
1585 timeout
.it_interval
.tv_sec
= 0;
1586 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1587 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1588 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1589 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1591 fprintf(stderr
, "Internal timer error: aborting\n");
1596 #endif /* defined(__linux__) */
1598 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1600 struct sigaction act
;
1601 struct itimerval itv
;
1605 sigfillset(&act
.sa_mask
);
1607 act
.sa_handler
= host_alarm_handler
;
1609 sigaction(SIGALRM
, &act
, NULL
);
1611 itv
.it_interval
.tv_sec
= 0;
1612 /* for i386 kernel 2.6 to get 1 ms */
1613 itv
.it_interval
.tv_usec
= 999;
1614 itv
.it_value
.tv_sec
= 0;
1615 itv
.it_value
.tv_usec
= 10 * 1000;
1617 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1624 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1626 struct itimerval itv
;
1628 memset(&itv
, 0, sizeof(itv
));
1629 setitimer(ITIMER_REAL
, &itv
, NULL
);
1632 #endif /* !defined(_WIN32) */
1634 static void try_to_rearm_timer(void *opaque
)
1636 struct qemu_alarm_timer
*t
= opaque
;
1640 /* Drain the notify pipe */
1643 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1644 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1647 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1648 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1649 qemu_rearm_alarm_timer(alarm_timer
);
1655 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1658 struct qemu_alarm_win32
*data
= t
->priv
;
1661 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1662 if (!data
->host_alarm
) {
1663 perror("Failed CreateEvent");
1667 memset(&tc
, 0, sizeof(tc
));
1668 timeGetDevCaps(&tc
, sizeof(tc
));
1670 if (data
->period
< tc
.wPeriodMin
)
1671 data
->period
= tc
.wPeriodMin
;
1673 timeBeginPeriod(data
->period
);
1675 flags
= TIME_CALLBACK_FUNCTION
;
1676 if (alarm_has_dynticks(t
))
1677 flags
|= TIME_ONESHOT
;
1679 flags
|= TIME_PERIODIC
;
1681 data
->timerId
= timeSetEvent(1, // interval (ms)
1682 data
->period
, // resolution
1683 host_alarm_handler
, // function
1684 (DWORD
)t
, // parameter
1687 if (!data
->timerId
) {
1688 perror("Failed to initialize win32 alarm timer");
1690 timeEndPeriod(data
->period
);
1691 CloseHandle(data
->host_alarm
);
1695 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1700 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1702 struct qemu_alarm_win32
*data
= t
->priv
;
1704 timeKillEvent(data
->timerId
);
1705 timeEndPeriod(data
->period
);
1707 CloseHandle(data
->host_alarm
);
1710 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1712 struct qemu_alarm_win32
*data
= t
->priv
;
1713 uint64_t nearest_delta_us
;
1715 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1716 !active_timers
[QEMU_TIMER_VIRTUAL
])
1719 nearest_delta_us
= qemu_next_deadline_dyntick();
1720 nearest_delta_us
/= 1000;
1722 timeKillEvent(data
->timerId
);
1724 data
->timerId
= timeSetEvent(1,
1728 TIME_ONESHOT
| TIME_PERIODIC
);
1730 if (!data
->timerId
) {
1731 perror("Failed to re-arm win32 alarm timer");
1733 timeEndPeriod(data
->period
);
1734 CloseHandle(data
->host_alarm
);
1741 static int init_timer_alarm(void)
1743 struct qemu_alarm_timer
*t
= NULL
;
1753 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1757 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1761 alarm_timer_rfd
= fds
[0];
1762 alarm_timer_wfd
= fds
[1];
1765 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1766 t
= &alarm_timers
[i
];
1779 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1780 try_to_rearm_timer
, NULL
, t
);
1795 static void quit_timers(void)
1797 alarm_timer
->stop(alarm_timer
);
1801 /***********************************************************/
1802 /* host time/date access */
1803 void qemu_get_timedate(struct tm
*tm
, int offset
)
1810 if (rtc_date_offset
== -1) {
1814 ret
= localtime(&ti
);
1816 ti
-= rtc_date_offset
;
1820 memcpy(tm
, ret
, sizeof(struct tm
));
1823 int qemu_timedate_diff(struct tm
*tm
)
1827 if (rtc_date_offset
== -1)
1829 seconds
= mktimegm(tm
);
1831 seconds
= mktime(tm
);
1833 seconds
= mktimegm(tm
) + rtc_date_offset
;
1835 return seconds
- time(NULL
);
1839 static void socket_cleanup(void)
1844 static int socket_init(void)
1849 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1851 err
= WSAGetLastError();
1852 fprintf(stderr
, "WSAStartup: %d\n", err
);
1855 atexit(socket_cleanup
);
1860 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1865 while (*p
!= '\0' && *p
!= '=') {
1866 if (q
&& (q
- buf
) < buf_size
- 1)
1876 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1881 while (*p
!= '\0') {
1883 if (*(p
+ 1) != ',')
1887 if (q
&& (q
- buf
) < buf_size
- 1)
1897 int get_param_value(char *buf
, int buf_size
,
1898 const char *tag
, const char *str
)
1905 p
= get_opt_name(option
, sizeof(option
), p
);
1909 if (!strcmp(tag
, option
)) {
1910 (void)get_opt_value(buf
, buf_size
, p
);
1913 p
= get_opt_value(NULL
, 0, p
);
1922 int check_params(char *buf
, int buf_size
,
1923 const char * const *params
, const char *str
)
1930 p
= get_opt_name(buf
, buf_size
, p
);
1934 for(i
= 0; params
[i
] != NULL
; i
++)
1935 if (!strcmp(params
[i
], buf
))
1937 if (params
[i
] == NULL
)
1939 p
= get_opt_value(NULL
, 0, p
);
1947 /***********************************************************/
1948 /* Bluetooth support */
1951 static struct HCIInfo
*hci_table
[MAX_NICS
];
1953 static struct bt_vlan_s
{
1954 struct bt_scatternet_s net
;
1956 struct bt_vlan_s
*next
;
1959 /* find or alloc a new bluetooth "VLAN" */
1960 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1962 struct bt_vlan_s
**pvlan
, *vlan
;
1963 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1967 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1969 pvlan
= &first_bt_vlan
;
1970 while (*pvlan
!= NULL
)
1971 pvlan
= &(*pvlan
)->next
;
1976 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1980 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1985 static struct HCIInfo null_hci
= {
1986 .cmd_send
= null_hci_send
,
1987 .sco_send
= null_hci_send
,
1988 .acl_send
= null_hci_send
,
1989 .bdaddr_set
= null_hci_addr_set
,
1992 struct HCIInfo
*qemu_next_hci(void)
1994 if (cur_hci
== nb_hcis
)
1997 return hci_table
[cur_hci
++];
2000 static struct HCIInfo
*hci_init(const char *str
)
2003 struct bt_scatternet_s
*vlan
= 0;
2005 if (!strcmp(str
, "null"))
2008 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2010 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2011 else if (!strncmp(str
, "hci", 3)) {
2014 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2015 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2020 vlan
= qemu_find_bt_vlan(0);
2022 return bt_new_hci(vlan
);
2025 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2030 static int bt_hci_parse(const char *str
)
2032 struct HCIInfo
*hci
;
2035 if (nb_hcis
>= MAX_NICS
) {
2036 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2040 hci
= hci_init(str
);
2049 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2050 hci
->bdaddr_set(hci
, bdaddr
.b
);
2052 hci_table
[nb_hcis
++] = hci
;
2057 static void bt_vhci_add(int vlan_id
)
2059 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2062 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2063 "an empty scatternet %i\n", vlan_id
);
2065 bt_vhci_init(bt_new_hci(vlan
));
2068 static struct bt_device_s
*bt_device_add(const char *opt
)
2070 struct bt_scatternet_s
*vlan
;
2072 char *endp
= strstr(opt
, ",vlan=");
2073 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2076 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2079 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2081 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2086 vlan
= qemu_find_bt_vlan(vlan_id
);
2089 fprintf(stderr
, "qemu: warning: adding a slave device to "
2090 "an empty scatternet %i\n", vlan_id
);
2092 if (!strcmp(devname
, "keyboard"))
2093 return bt_keyboard_init(vlan
);
2095 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2099 static int bt_parse(const char *opt
)
2101 const char *endp
, *p
;
2104 if (strstart(opt
, "hci", &endp
)) {
2105 if (!*endp
|| *endp
== ',') {
2107 if (!strstart(endp
, ",vlan=", 0))
2110 return bt_hci_parse(opt
);
2112 } else if (strstart(opt
, "vhci", &endp
)) {
2113 if (!*endp
|| *endp
== ',') {
2115 if (strstart(endp
, ",vlan=", &p
)) {
2116 vlan
= strtol(p
, (char **) &endp
, 0);
2118 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2122 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2131 } else if (strstart(opt
, "device:", &endp
))
2132 return !bt_device_add(endp
);
2134 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2138 /***********************************************************/
2139 /* QEMU Block devices */
2141 #define HD_ALIAS "index=%d,media=disk"
2143 #define CDROM_ALIAS "index=1,media=cdrom"
2145 #define CDROM_ALIAS "index=2,media=cdrom"
2147 #define FD_ALIAS "index=%d,if=floppy"
2148 #define PFLASH_ALIAS "if=pflash"
2149 #define MTD_ALIAS "if=mtd"
2150 #define SD_ALIAS "index=0,if=sd"
2152 static int drive_add(const char *file
, const char *fmt
, ...)
2156 if (nb_drives_opt
>= MAX_DRIVES
) {
2157 fprintf(stderr
, "qemu: too many drives\n");
2161 drives_opt
[nb_drives_opt
].file
= file
;
2163 vsnprintf(drives_opt
[nb_drives_opt
].opt
,
2164 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2167 return nb_drives_opt
++;
2170 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2174 /* seek interface, bus and unit */
2176 for (index
= 0; index
< nb_drives
; index
++)
2177 if (drives_table
[index
].type
== type
&&
2178 drives_table
[index
].bus
== bus
&&
2179 drives_table
[index
].unit
== unit
)
2185 int drive_get_max_bus(BlockInterfaceType type
)
2191 for (index
= 0; index
< nb_drives
; index
++) {
2192 if(drives_table
[index
].type
== type
&&
2193 drives_table
[index
].bus
> max_bus
)
2194 max_bus
= drives_table
[index
].bus
;
2199 const char *drive_get_serial(BlockDriverState
*bdrv
)
2203 for (index
= 0; index
< nb_drives
; index
++)
2204 if (drives_table
[index
].bdrv
== bdrv
)
2205 return drives_table
[index
].serial
;
2210 static void bdrv_format_print(void *opaque
, const char *name
)
2212 fprintf(stderr
, " %s", name
);
2215 static int drive_init(struct drive_opt
*arg
, int snapshot
,
2216 QEMUMachine
*machine
)
2222 const char *mediastr
= "";
2223 BlockInterfaceType type
;
2224 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2225 int bus_id
, unit_id
;
2226 int cyls
, heads
, secs
, translation
;
2227 BlockDriverState
*bdrv
;
2228 BlockDriver
*drv
= NULL
;
2233 char *str
= arg
->opt
;
2234 static const char * const params
[] = { "bus", "unit", "if", "index",
2235 "cyls", "heads", "secs", "trans",
2236 "media", "snapshot", "file",
2237 "cache", "format", "serial", NULL
};
2239 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2240 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2246 cyls
= heads
= secs
= 0;
2249 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2253 if (machine
->use_scsi
) {
2255 max_devs
= MAX_SCSI_DEVS
;
2256 pstrcpy(devname
, sizeof(devname
), "scsi");
2259 max_devs
= MAX_IDE_DEVS
;
2260 pstrcpy(devname
, sizeof(devname
), "ide");
2264 /* extract parameters */
2266 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2267 bus_id
= strtol(buf
, NULL
, 0);
2269 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2274 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2275 unit_id
= strtol(buf
, NULL
, 0);
2277 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2282 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2283 pstrcpy(devname
, sizeof(devname
), buf
);
2284 if (!strcmp(buf
, "ide")) {
2286 max_devs
= MAX_IDE_DEVS
;
2287 } else if (!strcmp(buf
, "scsi")) {
2289 max_devs
= MAX_SCSI_DEVS
;
2290 } else if (!strcmp(buf
, "floppy")) {
2293 } else if (!strcmp(buf
, "pflash")) {
2296 } else if (!strcmp(buf
, "mtd")) {
2299 } else if (!strcmp(buf
, "sd")) {
2302 } else if (!strcmp(buf
, "virtio")) {
2306 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2311 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2312 index
= strtol(buf
, NULL
, 0);
2314 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2319 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2320 cyls
= strtol(buf
, NULL
, 0);
2323 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2324 heads
= strtol(buf
, NULL
, 0);
2327 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2328 secs
= strtol(buf
, NULL
, 0);
2331 if (cyls
|| heads
|| secs
) {
2332 if (cyls
< 1 || cyls
> 16383) {
2333 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2336 if (heads
< 1 || heads
> 16) {
2337 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2340 if (secs
< 1 || secs
> 63) {
2341 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2346 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2349 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2353 if (!strcmp(buf
, "none"))
2354 translation
= BIOS_ATA_TRANSLATION_NONE
;
2355 else if (!strcmp(buf
, "lba"))
2356 translation
= BIOS_ATA_TRANSLATION_LBA
;
2357 else if (!strcmp(buf
, "auto"))
2358 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2360 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2365 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2366 if (!strcmp(buf
, "disk")) {
2368 } else if (!strcmp(buf
, "cdrom")) {
2369 if (cyls
|| secs
|| heads
) {
2371 "qemu: '%s' invalid physical CHS format\n", str
);
2374 media
= MEDIA_CDROM
;
2376 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2381 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2382 if (!strcmp(buf
, "on"))
2384 else if (!strcmp(buf
, "off"))
2387 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2392 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2393 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2395 else if (!strcmp(buf
, "writethrough"))
2397 else if (!strcmp(buf
, "writeback"))
2400 fprintf(stderr
, "qemu: invalid cache option\n");
2405 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2406 if (strcmp(buf
, "?") == 0) {
2407 fprintf(stderr
, "qemu: Supported formats:");
2408 bdrv_iterate_format(bdrv_format_print
, NULL
);
2409 fprintf(stderr
, "\n");
2412 drv
= bdrv_find_format(buf
);
2414 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2419 if (arg
->file
== NULL
)
2420 get_param_value(file
, sizeof(file
), "file", str
);
2422 pstrcpy(file
, sizeof(file
), arg
->file
);
2424 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2425 memset(serial
, 0, sizeof(serial
));
2427 /* compute bus and unit according index */
2430 if (bus_id
!= 0 || unit_id
!= -1) {
2432 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2440 unit_id
= index
% max_devs
;
2441 bus_id
= index
/ max_devs
;
2445 /* if user doesn't specify a unit_id,
2446 * try to find the first free
2449 if (unit_id
== -1) {
2451 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2453 if (max_devs
&& unit_id
>= max_devs
) {
2454 unit_id
-= max_devs
;
2462 if (max_devs
&& unit_id
>= max_devs
) {
2463 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2464 str
, unit_id
, max_devs
- 1);
2469 * ignore multiple definitions
2472 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2477 if (type
== IF_IDE
|| type
== IF_SCSI
)
2478 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2480 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2481 devname
, bus_id
, mediastr
, unit_id
);
2483 snprintf(buf
, sizeof(buf
), "%s%s%i",
2484 devname
, mediastr
, unit_id
);
2485 bdrv
= bdrv_new(buf
);
2486 drives_table
[nb_drives
].bdrv
= bdrv
;
2487 drives_table
[nb_drives
].type
= type
;
2488 drives_table
[nb_drives
].bus
= bus_id
;
2489 drives_table
[nb_drives
].unit
= unit_id
;
2490 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2499 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2500 bdrv_set_translation_hint(bdrv
, translation
);
2504 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2509 /* FIXME: This isn't really a floppy, but it's a reasonable
2512 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2523 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2524 cache
= 2; /* always use write-back with snapshot */
2526 if (cache
== 0) /* no caching */
2527 bdrv_flags
|= BDRV_O_NOCACHE
;
2528 else if (cache
== 2) /* write-back */
2529 bdrv_flags
|= BDRV_O_CACHE_WB
;
2530 else if (cache
== 3) /* not specified */
2531 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2532 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2533 fprintf(stderr
, "qemu: could not open disk image %s\n",
2540 /***********************************************************/
2543 static USBPort
*used_usb_ports
;
2544 static USBPort
*free_usb_ports
;
2546 /* ??? Maybe change this to register a hub to keep track of the topology. */
2547 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2548 usb_attachfn attach
)
2550 port
->opaque
= opaque
;
2551 port
->index
= index
;
2552 port
->attach
= attach
;
2553 port
->next
= free_usb_ports
;
2554 free_usb_ports
= port
;
2557 int usb_device_add_dev(USBDevice
*dev
)
2561 /* Find a USB port to add the device to. */
2562 port
= free_usb_ports
;
2566 /* Create a new hub and chain it on. */
2567 free_usb_ports
= NULL
;
2568 port
->next
= used_usb_ports
;
2569 used_usb_ports
= port
;
2571 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2572 usb_attach(port
, hub
);
2573 port
= free_usb_ports
;
2576 free_usb_ports
= port
->next
;
2577 port
->next
= used_usb_ports
;
2578 used_usb_ports
= port
;
2579 usb_attach(port
, dev
);
2583 static int usb_device_add(const char *devname
)
2588 if (!free_usb_ports
)
2591 if (strstart(devname
, "host:", &p
)) {
2592 dev
= usb_host_device_open(p
);
2593 } else if (!strcmp(devname
, "mouse")) {
2594 dev
= usb_mouse_init();
2595 } else if (!strcmp(devname
, "tablet")) {
2596 dev
= usb_tablet_init();
2597 } else if (!strcmp(devname
, "keyboard")) {
2598 dev
= usb_keyboard_init();
2599 } else if (strstart(devname
, "disk:", &p
)) {
2600 dev
= usb_msd_init(p
);
2601 } else if (!strcmp(devname
, "wacom-tablet")) {
2602 dev
= usb_wacom_init();
2603 } else if (strstart(devname
, "serial:", &p
)) {
2604 dev
= usb_serial_init(p
);
2605 #ifdef CONFIG_BRLAPI
2606 } else if (!strcmp(devname
, "braille")) {
2607 dev
= usb_baum_init();
2609 } else if (strstart(devname
, "net:", &p
)) {
2612 if (net_client_init("nic", p
) < 0)
2614 nd_table
[nic
].model
= "usb";
2615 dev
= usb_net_init(&nd_table
[nic
]);
2616 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2617 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2618 bt_new_hci(qemu_find_bt_vlan(0)));
2625 return usb_device_add_dev(dev
);
2628 int usb_device_del_addr(int bus_num
, int addr
)
2634 if (!used_usb_ports
)
2640 lastp
= &used_usb_ports
;
2641 port
= used_usb_ports
;
2642 while (port
&& port
->dev
->addr
!= addr
) {
2643 lastp
= &port
->next
;
2651 *lastp
= port
->next
;
2652 usb_attach(port
, NULL
);
2653 dev
->handle_destroy(dev
);
2654 port
->next
= free_usb_ports
;
2655 free_usb_ports
= port
;
2659 static int usb_device_del(const char *devname
)
2664 if (strstart(devname
, "host:", &p
))
2665 return usb_host_device_close(p
);
2667 if (!used_usb_ports
)
2670 p
= strchr(devname
, '.');
2673 bus_num
= strtoul(devname
, NULL
, 0);
2674 addr
= strtoul(p
+ 1, NULL
, 0);
2676 return usb_device_del_addr(bus_num
, addr
);
2679 void do_usb_add(const char *devname
)
2681 usb_device_add(devname
);
2684 void do_usb_del(const char *devname
)
2686 usb_device_del(devname
);
2693 const char *speed_str
;
2696 term_printf("USB support not enabled\n");
2700 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2704 switch(dev
->speed
) {
2708 case USB_SPEED_FULL
:
2711 case USB_SPEED_HIGH
:
2718 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2719 0, dev
->addr
, speed_str
, dev
->devname
);
2723 /***********************************************************/
2724 /* PCMCIA/Cardbus */
2726 static struct pcmcia_socket_entry_s
{
2727 struct pcmcia_socket_s
*socket
;
2728 struct pcmcia_socket_entry_s
*next
;
2729 } *pcmcia_sockets
= 0;
2731 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2733 struct pcmcia_socket_entry_s
*entry
;
2735 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2736 entry
->socket
= socket
;
2737 entry
->next
= pcmcia_sockets
;
2738 pcmcia_sockets
= entry
;
2741 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2743 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2745 ptr
= &pcmcia_sockets
;
2746 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2747 if (entry
->socket
== socket
) {
2753 void pcmcia_info(void)
2755 struct pcmcia_socket_entry_s
*iter
;
2756 if (!pcmcia_sockets
)
2757 term_printf("No PCMCIA sockets\n");
2759 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2760 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2761 iter
->socket
->attached
? iter
->socket
->card_string
:
2765 /***********************************************************/
2768 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2772 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2776 static void dumb_display_init(DisplayState
*ds
)
2781 ds
->dpy_update
= dumb_update
;
2782 ds
->dpy_resize
= dumb_resize
;
2783 ds
->dpy_refresh
= NULL
;
2784 ds
->gui_timer_interval
= 0;
2788 /***********************************************************/
2791 #define MAX_IO_HANDLERS 64
2793 typedef struct IOHandlerRecord
{
2795 IOCanRWHandler
*fd_read_poll
;
2797 IOHandler
*fd_write
;
2800 /* temporary data */
2802 struct IOHandlerRecord
*next
;
2805 static IOHandlerRecord
*first_io_handler
;
2807 /* XXX: fd_read_poll should be suppressed, but an API change is
2808 necessary in the character devices to suppress fd_can_read(). */
2809 int qemu_set_fd_handler2(int fd
,
2810 IOCanRWHandler
*fd_read_poll
,
2812 IOHandler
*fd_write
,
2815 IOHandlerRecord
**pioh
, *ioh
;
2817 if (!fd_read
&& !fd_write
) {
2818 pioh
= &first_io_handler
;
2823 if (ioh
->fd
== fd
) {
2830 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2834 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2837 ioh
->next
= first_io_handler
;
2838 first_io_handler
= ioh
;
2841 ioh
->fd_read_poll
= fd_read_poll
;
2842 ioh
->fd_read
= fd_read
;
2843 ioh
->fd_write
= fd_write
;
2844 ioh
->opaque
= opaque
;
2850 int qemu_set_fd_handler(int fd
,
2852 IOHandler
*fd_write
,
2855 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2859 /***********************************************************/
2860 /* Polling handling */
2862 typedef struct PollingEntry
{
2865 struct PollingEntry
*next
;
2868 static PollingEntry
*first_polling_entry
;
2870 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2872 PollingEntry
**ppe
, *pe
;
2873 pe
= qemu_mallocz(sizeof(PollingEntry
));
2877 pe
->opaque
= opaque
;
2878 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2883 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2885 PollingEntry
**ppe
, *pe
;
2886 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2888 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2896 /***********************************************************/
2897 /* Wait objects support */
2898 typedef struct WaitObjects
{
2900 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2901 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2902 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2905 static WaitObjects wait_objects
= {0};
2907 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2909 WaitObjects
*w
= &wait_objects
;
2911 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2913 w
->events
[w
->num
] = handle
;
2914 w
->func
[w
->num
] = func
;
2915 w
->opaque
[w
->num
] = opaque
;
2920 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2923 WaitObjects
*w
= &wait_objects
;
2926 for (i
= 0; i
< w
->num
; i
++) {
2927 if (w
->events
[i
] == handle
)
2930 w
->events
[i
] = w
->events
[i
+ 1];
2931 w
->func
[i
] = w
->func
[i
+ 1];
2932 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2940 /***********************************************************/
2941 /* ram save/restore */
2943 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2947 v
= qemu_get_byte(f
);
2950 if (qemu_get_buffer(f
, buf
, len
) != len
)
2954 v
= qemu_get_byte(f
);
2955 memset(buf
, v
, len
);
2961 if (qemu_file_has_error(f
))
2967 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2972 if (qemu_get_be32(f
) != phys_ram_size
)
2974 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
2975 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
2982 #define BDRV_HASH_BLOCK_SIZE 1024
2983 #define IOBUF_SIZE 4096
2984 #define RAM_CBLOCK_MAGIC 0xfabe
2986 typedef struct RamDecompressState
{
2989 uint8_t buf
[IOBUF_SIZE
];
2990 } RamDecompressState
;
2992 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2995 memset(s
, 0, sizeof(*s
));
2997 ret
= inflateInit(&s
->zstream
);
3003 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3007 s
->zstream
.avail_out
= len
;
3008 s
->zstream
.next_out
= buf
;
3009 while (s
->zstream
.avail_out
> 0) {
3010 if (s
->zstream
.avail_in
== 0) {
3011 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3013 clen
= qemu_get_be16(s
->f
);
3014 if (clen
> IOBUF_SIZE
)
3016 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3017 s
->zstream
.avail_in
= clen
;
3018 s
->zstream
.next_in
= s
->buf
;
3020 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3021 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3028 static void ram_decompress_close(RamDecompressState
*s
)
3030 inflateEnd(&s
->zstream
);
3033 #define RAM_SAVE_FLAG_FULL 0x01
3034 #define RAM_SAVE_FLAG_COMPRESS 0x02
3035 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3036 #define RAM_SAVE_FLAG_PAGE 0x08
3037 #define RAM_SAVE_FLAG_EOS 0x10
3039 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3041 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3042 uint32_t *array
= (uint32_t *)page
;
3045 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3046 if (array
[i
] != val
)
3053 static int ram_save_block(QEMUFile
*f
)
3055 static ram_addr_t current_addr
= 0;
3056 ram_addr_t saved_addr
= current_addr
;
3057 ram_addr_t addr
= 0;
3060 while (addr
< phys_ram_size
) {
3061 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3064 cpu_physical_memory_reset_dirty(current_addr
,
3065 current_addr
+ TARGET_PAGE_SIZE
,
3066 MIGRATION_DIRTY_FLAG
);
3068 ch
= *(phys_ram_base
+ current_addr
);
3070 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3071 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3072 qemu_put_byte(f
, ch
);
3074 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3075 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3081 addr
+= TARGET_PAGE_SIZE
;
3082 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3088 static ram_addr_t ram_save_threshold
= 10;
3090 static ram_addr_t
ram_save_remaining(void)
3093 ram_addr_t count
= 0;
3095 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3096 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3103 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3108 /* Make sure all dirty bits are set */
3109 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3110 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3111 cpu_physical_memory_set_dirty(addr
);
3114 /* Enable dirty memory tracking */
3115 cpu_physical_memory_set_dirty_tracking(1);
3117 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3120 while (!qemu_file_rate_limit(f
)) {
3123 ret
= ram_save_block(f
);
3124 if (ret
== 0) /* no more blocks */
3128 /* try transferring iterative blocks of memory */
3131 cpu_physical_memory_set_dirty_tracking(0);
3133 /* flush all remaining blocks regardless of rate limiting */
3134 while (ram_save_block(f
) != 0);
3137 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3139 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3142 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3144 RamDecompressState s1
, *s
= &s1
;
3148 if (ram_decompress_open(s
, f
) < 0)
3150 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3151 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3152 fprintf(stderr
, "Error while reading ram block header\n");
3156 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3157 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3162 printf("Error block header\n");
3166 ram_decompress_close(s
);
3171 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3176 if (version_id
== 1)
3177 return ram_load_v1(f
, opaque
);
3179 if (version_id
== 2) {
3180 if (qemu_get_be32(f
) != phys_ram_size
)
3182 return ram_load_dead(f
, opaque
);
3185 if (version_id
!= 3)
3189 addr
= qemu_get_be64(f
);
3191 flags
= addr
& ~TARGET_PAGE_MASK
;
3192 addr
&= TARGET_PAGE_MASK
;
3194 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3195 if (addr
!= phys_ram_size
)
3199 if (flags
& RAM_SAVE_FLAG_FULL
) {
3200 if (ram_load_dead(f
, opaque
) < 0)
3204 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3205 uint8_t ch
= qemu_get_byte(f
);
3206 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3207 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3208 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3209 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3214 void qemu_service_io(void)
3216 CPUState
*env
= cpu_single_env
;
3218 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3220 if (env
->kqemu_enabled
) {
3221 kqemu_cpu_interrupt(env
);
3227 /***********************************************************/
3228 /* bottom halves (can be seen as timers which expire ASAP) */
3239 static QEMUBH
*first_bh
= NULL
;
3241 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3244 bh
= qemu_mallocz(sizeof(QEMUBH
));
3248 bh
->opaque
= opaque
;
3249 bh
->next
= first_bh
;
3254 int qemu_bh_poll(void)
3260 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3261 if (!bh
->deleted
&& bh
->scheduled
) {
3270 /* remove deleted bhs */
3284 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3292 void qemu_bh_schedule(QEMUBH
*bh
)
3294 CPUState
*env
= cpu_single_env
;
3299 /* stop the currently executing CPU to execute the BH ASAP */
3301 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3305 void qemu_bh_cancel(QEMUBH
*bh
)
3310 void qemu_bh_delete(QEMUBH
*bh
)
3316 static void qemu_bh_update_timeout(int *timeout
)
3320 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3321 if (!bh
->deleted
&& bh
->scheduled
) {
3323 /* idle bottom halves will be polled at least
3325 *timeout
= MIN(10, *timeout
);
3327 /* non-idle bottom halves will be executed
3336 /***********************************************************/
3337 /* machine registration */
3339 static QEMUMachine
*first_machine
= NULL
;
3341 int qemu_register_machine(QEMUMachine
*m
)
3344 pm
= &first_machine
;
3352 static QEMUMachine
*find_machine(const char *name
)
3356 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3357 if (!strcmp(m
->name
, name
))
3363 /***********************************************************/
3364 /* main execution loop */
3366 static void gui_update(void *opaque
)
3368 DisplayState
*ds
= opaque
;
3369 ds
->dpy_refresh(ds
);
3370 qemu_mod_timer(ds
->gui_timer
,
3371 (ds
->gui_timer_interval
?
3372 ds
->gui_timer_interval
:
3373 GUI_REFRESH_INTERVAL
)
3374 + qemu_get_clock(rt_clock
));
3377 struct vm_change_state_entry
{
3378 VMChangeStateHandler
*cb
;
3380 LIST_ENTRY (vm_change_state_entry
) entries
;
3383 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3385 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3388 VMChangeStateEntry
*e
;
3390 e
= qemu_mallocz(sizeof (*e
));
3396 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3400 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3402 LIST_REMOVE (e
, entries
);
3406 static void vm_state_notify(int running
)
3408 VMChangeStateEntry
*e
;
3410 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3411 e
->cb(e
->opaque
, running
);
3415 /* XXX: support several handlers */
3416 static VMStopHandler
*vm_stop_cb
;
3417 static void *vm_stop_opaque
;
3419 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3422 vm_stop_opaque
= opaque
;
3426 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3437 qemu_rearm_alarm_timer(alarm_timer
);
3441 void vm_stop(int reason
)
3444 cpu_disable_ticks();
3448 vm_stop_cb(vm_stop_opaque
, reason
);
3455 /* reset/shutdown handler */
3457 typedef struct QEMUResetEntry
{
3458 QEMUResetHandler
*func
;
3460 struct QEMUResetEntry
*next
;
3463 static QEMUResetEntry
*first_reset_entry
;
3464 static int reset_requested
;
3465 static int shutdown_requested
;
3466 static int powerdown_requested
;
3468 int qemu_shutdown_requested(void)
3470 int r
= shutdown_requested
;
3471 shutdown_requested
= 0;
3475 int qemu_reset_requested(void)
3477 int r
= reset_requested
;
3478 reset_requested
= 0;
3482 int qemu_powerdown_requested(void)
3484 int r
= powerdown_requested
;
3485 powerdown_requested
= 0;
3489 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3491 QEMUResetEntry
**pre
, *re
;
3493 pre
= &first_reset_entry
;
3494 while (*pre
!= NULL
)
3495 pre
= &(*pre
)->next
;
3496 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3498 re
->opaque
= opaque
;
3503 void qemu_system_reset(void)
3507 /* reset all devices */
3508 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3509 re
->func(re
->opaque
);
3513 void qemu_system_reset_request(void)
3516 shutdown_requested
= 1;
3518 reset_requested
= 1;
3521 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3524 void qemu_system_shutdown_request(void)
3526 shutdown_requested
= 1;
3528 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3531 void qemu_system_powerdown_request(void)
3533 powerdown_requested
= 1;
3535 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3539 static void host_main_loop_wait(int *timeout
)
3545 /* XXX: need to suppress polling by better using win32 events */
3547 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3548 ret
|= pe
->func(pe
->opaque
);
3552 WaitObjects
*w
= &wait_objects
;
3554 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3555 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3556 if (w
->func
[ret
- WAIT_OBJECT_0
])
3557 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3559 /* Check for additional signaled events */
3560 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3562 /* Check if event is signaled */
3563 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3564 if(ret2
== WAIT_OBJECT_0
) {
3566 w
->func
[i
](w
->opaque
[i
]);
3567 } else if (ret2
== WAIT_TIMEOUT
) {
3569 err
= GetLastError();
3570 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3573 } else if (ret
== WAIT_TIMEOUT
) {
3575 err
= GetLastError();
3576 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3583 static void host_main_loop_wait(int *timeout
)
3588 void main_loop_wait(int timeout
)
3590 IOHandlerRecord
*ioh
;
3591 fd_set rfds
, wfds
, xfds
;
3595 qemu_bh_update_timeout(&timeout
);
3597 host_main_loop_wait(&timeout
);
3599 /* poll any events */
3600 /* XXX: separate device handlers from system ones */
3605 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3609 (!ioh
->fd_read_poll
||
3610 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3611 FD_SET(ioh
->fd
, &rfds
);
3615 if (ioh
->fd_write
) {
3616 FD_SET(ioh
->fd
, &wfds
);
3622 tv
.tv_sec
= timeout
/ 1000;
3623 tv
.tv_usec
= (timeout
% 1000) * 1000;
3625 #if defined(CONFIG_SLIRP)
3626 if (slirp_is_inited()) {
3627 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3630 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3632 IOHandlerRecord
**pioh
;
3634 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3635 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3636 ioh
->fd_read(ioh
->opaque
);
3638 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3639 ioh
->fd_write(ioh
->opaque
);
3643 /* remove deleted IO handlers */
3644 pioh
= &first_io_handler
;
3654 #if defined(CONFIG_SLIRP)
3655 if (slirp_is_inited()) {
3661 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3665 /* vm time timers */
3666 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3667 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3668 qemu_get_clock(vm_clock
));
3670 /* real time timers */
3671 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3672 qemu_get_clock(rt_clock
));
3674 /* Check bottom-halves last in case any of the earlier events triggered
3680 static int main_loop(void)
3683 #ifdef CONFIG_PROFILER
3688 cur_cpu
= first_cpu
;
3689 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3696 #ifdef CONFIG_PROFILER
3697 ti
= profile_getclock();
3702 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3703 env
->icount_decr
.u16
.low
= 0;
3704 env
->icount_extra
= 0;
3705 count
= qemu_next_deadline();
3706 count
= (count
+ (1 << icount_time_shift
) - 1)
3707 >> icount_time_shift
;
3708 qemu_icount
+= count
;
3709 decr
= (count
> 0xffff) ? 0xffff : count
;
3711 env
->icount_decr
.u16
.low
= decr
;
3712 env
->icount_extra
= count
;
3714 ret
= cpu_exec(env
);
3715 #ifdef CONFIG_PROFILER
3716 qemu_time
+= profile_getclock() - ti
;
3719 /* Fold pending instructions back into the
3720 instruction counter, and clear the interrupt flag. */
3721 qemu_icount
-= (env
->icount_decr
.u16
.low
3722 + env
->icount_extra
);
3723 env
->icount_decr
.u32
= 0;
3724 env
->icount_extra
= 0;
3726 next_cpu
= env
->next_cpu
?: first_cpu
;
3727 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3728 ret
= EXCP_INTERRUPT
;
3732 if (ret
== EXCP_HLT
) {
3733 /* Give the next CPU a chance to run. */
3737 if (ret
!= EXCP_HALTED
)
3739 /* all CPUs are halted ? */
3745 if (shutdown_requested
) {
3746 ret
= EXCP_INTERRUPT
;
3754 if (reset_requested
) {
3755 reset_requested
= 0;
3756 qemu_system_reset();
3757 ret
= EXCP_INTERRUPT
;
3759 if (powerdown_requested
) {
3760 powerdown_requested
= 0;
3761 qemu_system_powerdown();
3762 ret
= EXCP_INTERRUPT
;
3764 if (unlikely(ret
== EXCP_DEBUG
)) {
3765 gdb_set_stop_cpu(cur_cpu
);
3766 vm_stop(EXCP_DEBUG
);
3768 /* If all cpus are halted then wait until the next IRQ */
3769 /* XXX: use timeout computed from timers */
3770 if (ret
== EXCP_HALTED
) {
3774 /* Advance virtual time to the next event. */
3775 if (use_icount
== 1) {
3776 /* When not using an adaptive execution frequency
3777 we tend to get badly out of sync with real time,
3778 so just delay for a reasonable amount of time. */
3781 delta
= cpu_get_icount() - cpu_get_clock();
3784 /* If virtual time is ahead of real time then just
3786 timeout
= (delta
/ 1000000) + 1;
3788 /* Wait for either IO to occur or the next
3790 add
= qemu_next_deadline();
3791 /* We advance the timer before checking for IO.
3792 Limit the amount we advance so that early IO
3793 activity won't get the guest too far ahead. */
3797 add
= (add
+ (1 << icount_time_shift
) - 1)
3798 >> icount_time_shift
;
3800 timeout
= delta
/ 1000000;
3811 if (shutdown_requested
) {
3812 ret
= EXCP_INTERRUPT
;
3817 #ifdef CONFIG_PROFILER
3818 ti
= profile_getclock();
3820 main_loop_wait(timeout
);
3821 #ifdef CONFIG_PROFILER
3822 dev_time
+= profile_getclock() - ti
;
3825 cpu_disable_ticks();
3829 static void help(int exitcode
)
3831 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3832 "usage: %s [options] [disk_image]\n"
3834 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3836 "Standard options:\n"
3837 "-M machine select emulated machine (-M ? for list)\n"
3838 "-cpu cpu select CPU (-cpu ? for list)\n"
3839 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3840 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3841 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3842 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3843 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3844 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3845 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3846 " use 'file' as a drive image\n"
3847 "-mtdblock file use 'file' as on-board Flash memory image\n"
3848 "-sd file use 'file' as SecureDigital card image\n"
3849 "-pflash file use 'file' as a parallel flash image\n"
3850 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3851 "-snapshot write to temporary files instead of disk image files\n"
3853 "-no-frame open SDL window without a frame and window decorations\n"
3854 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3855 "-no-quit disable SDL window close capability\n"
3858 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3860 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3861 "-smp n set the number of CPUs to 'n' [default=1]\n"
3862 "-nographic disable graphical output and redirect serial I/Os to console\n"
3863 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3865 "-k language use keyboard layout (for example \"fr\" for French)\n"
3868 "-audio-help print list of audio drivers and their options\n"
3869 "-soundhw c1,... enable audio support\n"
3870 " and only specified sound cards (comma separated list)\n"
3871 " use -soundhw ? to get the list of supported cards\n"
3872 " use -soundhw all to enable all of them\n"
3874 "-vga [std|cirrus|vmware]\n"
3875 " select video card type\n"
3876 "-localtime set the real time clock to local time [default=utc]\n"
3877 "-full-screen start in full screen\n"
3879 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3881 "-usb enable the USB driver (will be the default soon)\n"
3882 "-usbdevice name add the host or guest USB device 'name'\n"
3883 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3884 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3886 "-name string set the name of the guest\n"
3887 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3889 "Network options:\n"
3890 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3891 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3893 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3894 " connect the user mode network stack to VLAN 'n' and send\n"
3895 " hostname 'host' to DHCP clients\n"
3898 "-net tap[,vlan=n][,name=str],ifname=name\n"
3899 " connect the host TAP network interface to VLAN 'n'\n"
3901 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
3902 " connect the host TAP network interface to VLAN 'n' and use the\n"
3903 " network scripts 'file' (default=%s)\n"
3904 " and 'dfile' (default=%s);\n"
3905 " use '[down]script=no' to disable script execution;\n"
3906 " use 'fd=h' to connect to an already opened TAP interface\n"
3908 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
3909 " connect the vlan 'n' to another VLAN using a socket connection\n"
3910 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
3911 " connect the vlan 'n' to multicast maddr and port\n"
3913 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
3914 " connect the vlan 'n' to port 'n' of a vde switch running\n"
3915 " on host and listening for incoming connections on 'socketpath'.\n"
3916 " Use group 'groupname' and mode 'octalmode' to change default\n"
3917 " ownership and permissions for communication port.\n"
3919 "-net none use it alone to have zero network devices; if no -net option\n"
3920 " is provided, the default is '-net nic -net user'\n"
3922 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
3923 "-bt hci,host[:id]\n"
3924 " Use host's HCI with the given name\n"
3925 "-bt hci[,vlan=n]\n"
3926 " Emulate a standard HCI in virtual scatternet 'n'\n"
3927 "-bt vhci[,vlan=n]\n"
3928 " Add host computer to virtual scatternet 'n' using VHCI\n"
3929 "-bt device:dev[,vlan=n]\n"
3930 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
3933 "-tftp dir allow tftp access to files in dir [-net user]\n"
3934 "-bootp file advertise file in BOOTP replies\n"
3936 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
3938 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
3939 " redirect TCP or UDP connections from host to guest [-net user]\n"
3942 "Linux boot specific:\n"
3943 "-kernel bzImage use 'bzImage' as kernel image\n"
3944 "-append cmdline use 'cmdline' as kernel command line\n"
3945 "-initrd file use 'file' as initial ram disk\n"
3947 "Debug/Expert options:\n"
3948 "-monitor dev redirect the monitor to char device 'dev'\n"
3949 "-serial dev redirect the serial port to char device 'dev'\n"
3950 "-parallel dev redirect the parallel port to char device 'dev'\n"
3951 "-pidfile file Write PID to 'file'\n"
3952 "-S freeze CPU at startup (use 'c' to start execution)\n"
3953 "-s wait gdb connection to port\n"
3954 "-p port set gdb connection port [default=%s]\n"
3955 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
3956 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
3957 " translation (t=none or lba) (usually qemu can guess them)\n"
3958 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
3960 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
3961 "-no-kqemu disable KQEMU kernel module usage\n"
3964 "-enable-kvm enable KVM full virtualization support\n"
3967 "-no-acpi disable ACPI\n"
3968 "-no-hpet disable HPET\n"
3970 #ifdef CONFIG_CURSES
3971 "-curses use a curses/ncurses interface instead of SDL\n"
3973 "-no-reboot exit instead of rebooting\n"
3974 "-no-shutdown stop before shutdown\n"
3975 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
3976 "-vnc display start a VNC server on display\n"
3978 "-daemonize daemonize QEMU after initializing\n"
3980 "-option-rom rom load a file, rom, into the option ROM space\n"
3982 "-prom-env variable=value set OpenBIOS nvram variables\n"
3984 "-clock force the use of the given methods for timer alarm.\n"
3985 " To see what timers are available use -clock ?\n"
3986 "-startdate select initial date of the clock\n"
3987 "-icount [N|auto]\n"
3988 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
3990 "During emulation, the following keys are useful:\n"
3991 "ctrl-alt-f toggle full screen\n"
3992 "ctrl-alt-n switch to virtual console 'n'\n"
3993 "ctrl-alt toggle mouse and keyboard grab\n"
3995 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4000 DEFAULT_NETWORK_SCRIPT
,
4001 DEFAULT_NETWORK_DOWN_SCRIPT
,
4003 DEFAULT_GDBSTUB_PORT
,
4008 #define HAS_ARG 0x0001
4023 QEMU_OPTION_mtdblock
,
4027 QEMU_OPTION_snapshot
,
4029 QEMU_OPTION_no_fd_bootchk
,
4032 QEMU_OPTION_nographic
,
4033 QEMU_OPTION_portrait
,
4035 QEMU_OPTION_audio_help
,
4036 QEMU_OPTION_soundhw
,
4058 QEMU_OPTION_localtime
,
4062 QEMU_OPTION_monitor
,
4064 QEMU_OPTION_parallel
,
4066 QEMU_OPTION_full_screen
,
4067 QEMU_OPTION_no_frame
,
4068 QEMU_OPTION_alt_grab
,
4069 QEMU_OPTION_no_quit
,
4070 QEMU_OPTION_pidfile
,
4071 QEMU_OPTION_no_kqemu
,
4072 QEMU_OPTION_kernel_kqemu
,
4073 QEMU_OPTION_enable_kvm
,
4074 QEMU_OPTION_win2k_hack
,
4076 QEMU_OPTION_usbdevice
,
4079 QEMU_OPTION_no_acpi
,
4080 QEMU_OPTION_no_hpet
,
4082 QEMU_OPTION_no_reboot
,
4083 QEMU_OPTION_no_shutdown
,
4084 QEMU_OPTION_show_cursor
,
4085 QEMU_OPTION_daemonize
,
4086 QEMU_OPTION_option_rom
,
4087 QEMU_OPTION_semihosting
,
4089 QEMU_OPTION_prom_env
,
4090 QEMU_OPTION_old_param
,
4092 QEMU_OPTION_startdate
,
4093 QEMU_OPTION_tb_size
,
4096 QEMU_OPTION_incoming
,
4099 typedef struct QEMUOption
{
4105 static const QEMUOption qemu_options
[] = {
4106 { "h", 0, QEMU_OPTION_h
},
4107 { "help", 0, QEMU_OPTION_h
},
4109 { "M", HAS_ARG
, QEMU_OPTION_M
},
4110 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4111 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4112 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4113 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4114 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4115 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4116 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4117 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4118 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4119 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4120 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4121 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4122 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4123 { "snapshot", 0, QEMU_OPTION_snapshot
},
4125 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4127 { "m", HAS_ARG
, QEMU_OPTION_m
},
4128 { "nographic", 0, QEMU_OPTION_nographic
},
4129 { "portrait", 0, QEMU_OPTION_portrait
},
4130 { "k", HAS_ARG
, QEMU_OPTION_k
},
4132 { "audio-help", 0, QEMU_OPTION_audio_help
},
4133 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4136 { "net", HAS_ARG
, QEMU_OPTION_net
},
4138 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4139 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4141 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4143 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4145 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4147 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4148 { "append", HAS_ARG
, QEMU_OPTION_append
},
4149 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4151 { "S", 0, QEMU_OPTION_S
},
4152 { "s", 0, QEMU_OPTION_s
},
4153 { "p", HAS_ARG
, QEMU_OPTION_p
},
4154 { "d", HAS_ARG
, QEMU_OPTION_d
},
4155 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4156 { "L", HAS_ARG
, QEMU_OPTION_L
},
4157 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4159 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4160 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4163 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4165 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4166 { "g", 1, QEMU_OPTION_g
},
4168 { "localtime", 0, QEMU_OPTION_localtime
},
4169 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4170 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4171 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4172 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4173 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4174 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4175 { "full-screen", 0, QEMU_OPTION_full_screen
},
4177 { "no-frame", 0, QEMU_OPTION_no_frame
},
4178 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4179 { "no-quit", 0, QEMU_OPTION_no_quit
},
4181 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4182 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4183 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4184 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4185 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4186 #ifdef CONFIG_CURSES
4187 { "curses", 0, QEMU_OPTION_curses
},
4189 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4191 /* temporary options */
4192 { "usb", 0, QEMU_OPTION_usb
},
4193 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4194 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4195 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4196 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4197 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4198 { "daemonize", 0, QEMU_OPTION_daemonize
},
4199 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4200 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4201 { "semihosting", 0, QEMU_OPTION_semihosting
},
4203 { "name", HAS_ARG
, QEMU_OPTION_name
},
4204 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4205 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4207 #if defined(TARGET_ARM)
4208 { "old-param", 0, QEMU_OPTION_old_param
},
4210 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4211 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4212 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4213 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4214 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4218 /* password input */
4220 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4225 if (!bdrv_is_encrypted(bs
))
4228 term_printf("%s is encrypted.\n", name
);
4229 for(i
= 0; i
< 3; i
++) {
4230 monitor_readline("Password: ", 1, password
, sizeof(password
));
4231 if (bdrv_set_key(bs
, password
) == 0)
4233 term_printf("invalid password\n");
4238 static BlockDriverState
*get_bdrv(int index
)
4240 if (index
> nb_drives
)
4242 return drives_table
[index
].bdrv
;
4245 static void read_passwords(void)
4247 BlockDriverState
*bs
;
4250 for(i
= 0; i
< 6; i
++) {
4253 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4258 struct soundhw soundhw
[] = {
4259 #ifdef HAS_AUDIO_CHOICE
4260 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4266 { .init_isa
= pcspk_audio_init
}
4273 "Creative Sound Blaster 16",
4276 { .init_isa
= SB16_init
}
4280 #ifdef CONFIG_CS4231A
4286 { .init_isa
= cs4231a_init
}
4294 "Yamaha YMF262 (OPL3)",
4296 "Yamaha YM3812 (OPL2)",
4300 { .init_isa
= Adlib_init
}
4307 "Gravis Ultrasound GF1",
4310 { .init_isa
= GUS_init
}
4317 "Intel 82801AA AC97 Audio",
4320 { .init_pci
= ac97_init
}
4324 #ifdef CONFIG_ES1370
4327 "ENSONIQ AudioPCI ES1370",
4330 { .init_pci
= es1370_init
}
4334 #endif /* HAS_AUDIO_CHOICE */
4336 { NULL
, NULL
, 0, 0, { NULL
} }
4339 static void select_soundhw (const char *optarg
)
4343 if (*optarg
== '?') {
4346 printf ("Valid sound card names (comma separated):\n");
4347 for (c
= soundhw
; c
->name
; ++c
) {
4348 printf ("%-11s %s\n", c
->name
, c
->descr
);
4350 printf ("\n-soundhw all will enable all of the above\n");
4351 exit (*optarg
!= '?');
4359 if (!strcmp (optarg
, "all")) {
4360 for (c
= soundhw
; c
->name
; ++c
) {
4368 e
= strchr (p
, ',');
4369 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4371 for (c
= soundhw
; c
->name
; ++c
) {
4372 if (!strncmp (c
->name
, p
, l
)) {
4381 "Unknown sound card name (too big to show)\n");
4384 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4389 p
+= l
+ (e
!= NULL
);
4393 goto show_valid_cards
;
4398 static void select_vgahw (const char *p
)
4402 if (strstart(p
, "std", &opts
)) {
4403 cirrus_vga_enabled
= 0;
4405 } else if (strstart(p
, "cirrus", &opts
)) {
4406 cirrus_vga_enabled
= 1;
4408 } else if (strstart(p
, "vmware", &opts
)) {
4409 cirrus_vga_enabled
= 0;
4413 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4417 const char *nextopt
;
4419 if (strstart(opts
, ",retrace=", &nextopt
)) {
4421 if (strstart(opts
, "dumb", &nextopt
))
4422 vga_retrace_method
= VGA_RETRACE_DUMB
;
4423 else if (strstart(opts
, "precise", &nextopt
))
4424 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4425 else goto invalid_vga
;
4426 } else goto invalid_vga
;
4432 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4434 exit(STATUS_CONTROL_C_EXIT
);
4439 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4443 if(strlen(str
) != 36)
4446 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4447 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4448 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4456 #define MAX_NET_CLIENTS 32
4460 static void termsig_handler(int signal
)
4462 qemu_system_shutdown_request();
4465 static void termsig_setup(void)
4467 struct sigaction act
;
4469 memset(&act
, 0, sizeof(act
));
4470 act
.sa_handler
= termsig_handler
;
4471 sigaction(SIGINT
, &act
, NULL
);
4472 sigaction(SIGHUP
, &act
, NULL
);
4473 sigaction(SIGTERM
, &act
, NULL
);
4478 int main(int argc
, char **argv
, char **envp
)
4480 #ifdef CONFIG_GDBSTUB
4482 const char *gdbstub_port
;
4484 uint32_t boot_devices_bitmap
= 0;
4486 int snapshot
, linux_boot
, net_boot
;
4487 const char *initrd_filename
;
4488 const char *kernel_filename
, *kernel_cmdline
;
4489 const char *boot_devices
= "";
4490 DisplayState
*ds
= &display_state
;
4491 int cyls
, heads
, secs
, translation
;
4492 const char *net_clients
[MAX_NET_CLIENTS
];
4494 const char *bt_opts
[MAX_BT_CMDLINE
];
4498 const char *r
, *optarg
;
4499 CharDriverState
*monitor_hd
;
4500 const char *monitor_device
;
4501 const char *serial_devices
[MAX_SERIAL_PORTS
];
4502 int serial_device_index
;
4503 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4504 int parallel_device_index
;
4505 const char *loadvm
= NULL
;
4506 QEMUMachine
*machine
;
4507 const char *cpu_model
;
4508 const char *usb_devices
[MAX_USB_CMDLINE
];
4509 int usb_devices_index
;
4512 const char *pid_file
= NULL
;
4514 const char *incoming
= NULL
;
4516 qemu_cache_utils_init(envp
);
4518 LIST_INIT (&vm_change_state_head
);
4521 struct sigaction act
;
4522 sigfillset(&act
.sa_mask
);
4524 act
.sa_handler
= SIG_IGN
;
4525 sigaction(SIGPIPE
, &act
, NULL
);
4528 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4529 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4530 QEMU to run on a single CPU */
4535 h
= GetCurrentProcess();
4536 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4537 for(i
= 0; i
< 32; i
++) {
4538 if (mask
& (1 << i
))
4543 SetProcessAffinityMask(h
, mask
);
4549 register_machines();
4550 machine
= first_machine
;
4552 initrd_filename
= NULL
;
4554 vga_ram_size
= VGA_RAM_SIZE
;
4555 #ifdef CONFIG_GDBSTUB
4557 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4562 kernel_filename
= NULL
;
4563 kernel_cmdline
= "";
4564 cyls
= heads
= secs
= 0;
4565 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4566 monitor_device
= "vc";
4568 serial_devices
[0] = "vc:80Cx24C";
4569 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4570 serial_devices
[i
] = NULL
;
4571 serial_device_index
= 0;
4573 parallel_devices
[0] = "vc:640x480";
4574 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4575 parallel_devices
[i
] = NULL
;
4576 parallel_device_index
= 0;
4578 usb_devices_index
= 0;
4597 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4599 const QEMUOption
*popt
;
4602 /* Treat --foo the same as -foo. */
4605 popt
= qemu_options
;
4608 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4612 if (!strcmp(popt
->name
, r
+ 1))
4616 if (popt
->flags
& HAS_ARG
) {
4617 if (optind
>= argc
) {
4618 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4622 optarg
= argv
[optind
++];
4627 switch(popt
->index
) {
4629 machine
= find_machine(optarg
);
4632 printf("Supported machines are:\n");
4633 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4634 printf("%-10s %s%s\n",
4636 m
== first_machine
? " (default)" : "");
4638 exit(*optarg
!= '?');
4641 case QEMU_OPTION_cpu
:
4642 /* hw initialization will check this */
4643 if (*optarg
== '?') {
4644 /* XXX: implement xxx_cpu_list for targets that still miss it */
4645 #if defined(cpu_list)
4646 cpu_list(stdout
, &fprintf
);
4653 case QEMU_OPTION_initrd
:
4654 initrd_filename
= optarg
;
4656 case QEMU_OPTION_hda
:
4658 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4660 hda_index
= drive_add(optarg
, HD_ALIAS
4661 ",cyls=%d,heads=%d,secs=%d%s",
4662 0, cyls
, heads
, secs
,
4663 translation
== BIOS_ATA_TRANSLATION_LBA
?
4665 translation
== BIOS_ATA_TRANSLATION_NONE
?
4666 ",trans=none" : "");
4668 case QEMU_OPTION_hdb
:
4669 case QEMU_OPTION_hdc
:
4670 case QEMU_OPTION_hdd
:
4671 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4673 case QEMU_OPTION_drive
:
4674 drive_add(NULL
, "%s", optarg
);
4676 case QEMU_OPTION_mtdblock
:
4677 drive_add(optarg
, MTD_ALIAS
);
4679 case QEMU_OPTION_sd
:
4680 drive_add(optarg
, SD_ALIAS
);
4682 case QEMU_OPTION_pflash
:
4683 drive_add(optarg
, PFLASH_ALIAS
);
4685 case QEMU_OPTION_snapshot
:
4688 case QEMU_OPTION_hdachs
:
4692 cyls
= strtol(p
, (char **)&p
, 0);
4693 if (cyls
< 1 || cyls
> 16383)
4698 heads
= strtol(p
, (char **)&p
, 0);
4699 if (heads
< 1 || heads
> 16)
4704 secs
= strtol(p
, (char **)&p
, 0);
4705 if (secs
< 1 || secs
> 63)
4709 if (!strcmp(p
, "none"))
4710 translation
= BIOS_ATA_TRANSLATION_NONE
;
4711 else if (!strcmp(p
, "lba"))
4712 translation
= BIOS_ATA_TRANSLATION_LBA
;
4713 else if (!strcmp(p
, "auto"))
4714 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4717 } else if (*p
!= '\0') {
4719 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4722 if (hda_index
!= -1)
4723 snprintf(drives_opt
[hda_index
].opt
,
4724 sizeof(drives_opt
[hda_index
].opt
),
4725 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4726 0, cyls
, heads
, secs
,
4727 translation
== BIOS_ATA_TRANSLATION_LBA
?
4729 translation
== BIOS_ATA_TRANSLATION_NONE
?
4730 ",trans=none" : "");
4733 case QEMU_OPTION_nographic
:
4736 #ifdef CONFIG_CURSES
4737 case QEMU_OPTION_curses
:
4741 case QEMU_OPTION_portrait
:
4744 case QEMU_OPTION_kernel
:
4745 kernel_filename
= optarg
;
4747 case QEMU_OPTION_append
:
4748 kernel_cmdline
= optarg
;
4750 case QEMU_OPTION_cdrom
:
4751 drive_add(optarg
, CDROM_ALIAS
);
4753 case QEMU_OPTION_boot
:
4754 boot_devices
= optarg
;
4755 /* We just do some generic consistency checks */
4757 /* Could easily be extended to 64 devices if needed */
4760 boot_devices_bitmap
= 0;
4761 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4762 /* Allowed boot devices are:
4763 * a b : floppy disk drives
4764 * c ... f : IDE disk drives
4765 * g ... m : machine implementation dependant drives
4766 * n ... p : network devices
4767 * It's up to each machine implementation to check
4768 * if the given boot devices match the actual hardware
4769 * implementation and firmware features.
4771 if (*p
< 'a' || *p
> 'q') {
4772 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4775 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4777 "Boot device '%c' was given twice\n",*p
);
4780 boot_devices_bitmap
|= 1 << (*p
- 'a');
4784 case QEMU_OPTION_fda
:
4785 case QEMU_OPTION_fdb
:
4786 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4789 case QEMU_OPTION_no_fd_bootchk
:
4793 case QEMU_OPTION_net
:
4794 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4795 fprintf(stderr
, "qemu: too many network clients\n");
4798 net_clients
[nb_net_clients
] = optarg
;
4802 case QEMU_OPTION_tftp
:
4803 tftp_prefix
= optarg
;
4805 case QEMU_OPTION_bootp
:
4806 bootp_filename
= optarg
;
4809 case QEMU_OPTION_smb
:
4810 net_slirp_smb(optarg
);
4813 case QEMU_OPTION_redir
:
4814 net_slirp_redir(optarg
);
4817 case QEMU_OPTION_bt
:
4818 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4819 fprintf(stderr
, "qemu: too many bluetooth options\n");
4822 bt_opts
[nb_bt_opts
++] = optarg
;
4825 case QEMU_OPTION_audio_help
:
4829 case QEMU_OPTION_soundhw
:
4830 select_soundhw (optarg
);
4836 case QEMU_OPTION_m
: {
4840 value
= strtoul(optarg
, &ptr
, 10);
4842 case 0: case 'M': case 'm':
4849 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4853 /* On 32-bit hosts, QEMU is limited by virtual address space */
4854 if (value
> (2047 << 20)
4856 && HOST_LONG_BITS
== 32
4859 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4862 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4863 fprintf(stderr
, "qemu: ram size too large\n");
4872 const CPULogItem
*item
;
4874 mask
= cpu_str_to_log_mask(optarg
);
4876 printf("Log items (comma separated):\n");
4877 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4878 printf("%-10s %s\n", item
->name
, item
->help
);
4885 #ifdef CONFIG_GDBSTUB
4890 gdbstub_port
= optarg
;
4896 case QEMU_OPTION_bios
:
4903 keyboard_layout
= optarg
;
4905 case QEMU_OPTION_localtime
:
4908 case QEMU_OPTION_vga
:
4909 select_vgahw (optarg
);
4916 w
= strtol(p
, (char **)&p
, 10);
4919 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4925 h
= strtol(p
, (char **)&p
, 10);
4930 depth
= strtol(p
, (char **)&p
, 10);
4931 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4932 depth
!= 24 && depth
!= 32)
4934 } else if (*p
== '\0') {
4935 depth
= graphic_depth
;
4942 graphic_depth
= depth
;
4945 case QEMU_OPTION_echr
:
4948 term_escape_char
= strtol(optarg
, &r
, 0);
4950 printf("Bad argument to echr\n");
4953 case QEMU_OPTION_monitor
:
4954 monitor_device
= optarg
;
4956 case QEMU_OPTION_serial
:
4957 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4958 fprintf(stderr
, "qemu: too many serial ports\n");
4961 serial_devices
[serial_device_index
] = optarg
;
4962 serial_device_index
++;
4964 case QEMU_OPTION_parallel
:
4965 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4966 fprintf(stderr
, "qemu: too many parallel ports\n");
4969 parallel_devices
[parallel_device_index
] = optarg
;
4970 parallel_device_index
++;
4972 case QEMU_OPTION_loadvm
:
4975 case QEMU_OPTION_full_screen
:
4979 case QEMU_OPTION_no_frame
:
4982 case QEMU_OPTION_alt_grab
:
4985 case QEMU_OPTION_no_quit
:
4989 case QEMU_OPTION_pidfile
:
4993 case QEMU_OPTION_win2k_hack
:
4994 win2k_install_hack
= 1;
4998 case QEMU_OPTION_no_kqemu
:
5001 case QEMU_OPTION_kernel_kqemu
:
5006 case QEMU_OPTION_enable_kvm
:
5013 case QEMU_OPTION_usb
:
5016 case QEMU_OPTION_usbdevice
:
5018 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5019 fprintf(stderr
, "Too many USB devices\n");
5022 usb_devices
[usb_devices_index
] = optarg
;
5023 usb_devices_index
++;
5025 case QEMU_OPTION_smp
:
5026 smp_cpus
= atoi(optarg
);
5028 fprintf(stderr
, "Invalid number of CPUs\n");
5032 case QEMU_OPTION_vnc
:
5033 vnc_display
= optarg
;
5035 case QEMU_OPTION_no_acpi
:
5038 case QEMU_OPTION_no_hpet
:
5041 case QEMU_OPTION_no_reboot
:
5044 case QEMU_OPTION_no_shutdown
:
5047 case QEMU_OPTION_show_cursor
:
5050 case QEMU_OPTION_uuid
:
5051 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5052 fprintf(stderr
, "Fail to parse UUID string."
5053 " Wrong format.\n");
5057 case QEMU_OPTION_daemonize
:
5060 case QEMU_OPTION_option_rom
:
5061 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5062 fprintf(stderr
, "Too many option ROMs\n");
5065 option_rom
[nb_option_roms
] = optarg
;
5068 case QEMU_OPTION_semihosting
:
5069 semihosting_enabled
= 1;
5071 case QEMU_OPTION_name
:
5074 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5075 case QEMU_OPTION_prom_env
:
5076 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5077 fprintf(stderr
, "Too many prom variables\n");
5080 prom_envs
[nb_prom_envs
] = optarg
;
5085 case QEMU_OPTION_old_param
:
5089 case QEMU_OPTION_clock
:
5090 configure_alarms(optarg
);
5092 case QEMU_OPTION_startdate
:
5095 time_t rtc_start_date
;
5096 if (!strcmp(optarg
, "now")) {
5097 rtc_date_offset
= -1;
5099 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5107 } else if (sscanf(optarg
, "%d-%d-%d",
5110 &tm
.tm_mday
) == 3) {
5119 rtc_start_date
= mktimegm(&tm
);
5120 if (rtc_start_date
== -1) {
5122 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5123 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5126 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5130 case QEMU_OPTION_tb_size
:
5131 tb_size
= strtol(optarg
, NULL
, 0);
5135 case QEMU_OPTION_icount
:
5137 if (strcmp(optarg
, "auto") == 0) {
5138 icount_time_shift
= -1;
5140 icount_time_shift
= strtol(optarg
, NULL
, 0);
5143 case QEMU_OPTION_incoming
:
5150 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5151 if (kvm_allowed
&& kqemu_allowed
) {
5153 "You can not enable both KVM and kqemu at the same time\n");
5158 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5159 if (smp_cpus
> machine
->max_cpus
) {
5160 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5161 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5167 if (serial_device_index
== 0)
5168 serial_devices
[0] = "stdio";
5169 if (parallel_device_index
== 0)
5170 parallel_devices
[0] = "null";
5171 if (strncmp(monitor_device
, "vc", 2) == 0)
5172 monitor_device
= "stdio";
5179 if (pipe(fds
) == -1)
5190 len
= read(fds
[0], &status
, 1);
5191 if (len
== -1 && (errno
== EINTR
))
5196 else if (status
== 1) {
5197 fprintf(stderr
, "Could not acquire pidfile\n");
5214 signal(SIGTSTP
, SIG_IGN
);
5215 signal(SIGTTOU
, SIG_IGN
);
5216 signal(SIGTTIN
, SIG_IGN
);
5220 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5223 write(fds
[1], &status
, 1);
5225 fprintf(stderr
, "Could not acquire pid file\n");
5233 linux_boot
= (kernel_filename
!= NULL
);
5234 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5236 if (!linux_boot
&& net_boot
== 0 &&
5237 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5240 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5241 fprintf(stderr
, "-append only allowed with -kernel option\n");
5245 if (!linux_boot
&& initrd_filename
!= NULL
) {
5246 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5250 /* boot to floppy or the default cd if no hard disk defined yet */
5251 if (!boot_devices
[0]) {
5252 boot_devices
= "cad";
5254 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5257 if (init_timer_alarm() < 0) {
5258 fprintf(stderr
, "could not initialize alarm timer\n");
5261 if (use_icount
&& icount_time_shift
< 0) {
5263 /* 125MIPS seems a reasonable initial guess at the guest speed.
5264 It will be corrected fairly quickly anyway. */
5265 icount_time_shift
= 3;
5266 init_icount_adjust();
5273 /* init network clients */
5274 if (nb_net_clients
== 0) {
5275 /* if no clients, we use a default config */
5276 net_clients
[nb_net_clients
++] = "nic";
5278 net_clients
[nb_net_clients
++] = "user";
5282 for(i
= 0;i
< nb_net_clients
; i
++) {
5283 if (net_client_parse(net_clients
[i
]) < 0)
5289 /* XXX: this should be moved in the PC machine instantiation code */
5290 if (net_boot
!= 0) {
5292 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5293 const char *model
= nd_table
[i
].model
;
5295 if (net_boot
& (1 << i
)) {
5298 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5299 if (get_image_size(buf
) > 0) {
5300 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5301 fprintf(stderr
, "Too many option ROMs\n");
5304 option_rom
[nb_option_roms
] = strdup(buf
);
5311 fprintf(stderr
, "No valid PXE rom found for network device\n");
5317 /* init the bluetooth world */
5318 for (i
= 0; i
< nb_bt_opts
; i
++)
5319 if (bt_parse(bt_opts
[i
]))
5322 /* init the memory */
5323 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5325 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5327 if (ram_size
< phys_ram_size
) {
5328 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5329 machine
->name
, (unsigned long long) phys_ram_size
);
5333 phys_ram_size
= ram_size
;
5335 ram_size
= phys_ram_size
;
5338 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5340 phys_ram_size
+= ram_size
;
5343 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5344 if (!phys_ram_base
) {
5345 fprintf(stderr
, "Could not allocate physical memory\n");
5349 /* init the dynamic translator */
5350 cpu_exec_init_all(tb_size
* 1024 * 1024);
5354 /* we always create the cdrom drive, even if no disk is there */
5356 if (nb_drives_opt
< MAX_DRIVES
)
5357 drive_add(NULL
, CDROM_ALIAS
);
5359 /* we always create at least one floppy */
5361 if (nb_drives_opt
< MAX_DRIVES
)
5362 drive_add(NULL
, FD_ALIAS
, 0);
5364 /* we always create one sd slot, even if no card is in it */
5366 if (nb_drives_opt
< MAX_DRIVES
)
5367 drive_add(NULL
, SD_ALIAS
);
5369 /* open the virtual block devices */
5371 for(i
= 0; i
< nb_drives_opt
; i
++)
5372 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5375 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5376 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5379 memset(&display_state
, 0, sizeof(display_state
));
5382 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5385 /* nearly nothing to do */
5386 dumb_display_init(ds
);
5387 } else if (vnc_display
!= NULL
) {
5388 vnc_display_init(ds
);
5389 if (vnc_display_open(ds
, vnc_display
) < 0)
5392 #if defined(CONFIG_CURSES)
5394 curses_display_init(ds
, full_screen
);
5398 #if defined(CONFIG_SDL)
5399 sdl_display_init(ds
, full_screen
, no_frame
);
5400 #elif defined(CONFIG_COCOA)
5401 cocoa_display_init(ds
, full_screen
);
5403 dumb_display_init(ds
);
5408 /* must be after terminal init, SDL library changes signal handlers */
5412 /* Maintain compatibility with multiple stdio monitors */
5413 if (!strcmp(monitor_device
,"stdio")) {
5414 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5415 const char *devname
= serial_devices
[i
];
5416 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5417 monitor_device
= NULL
;
5419 } else if (devname
&& !strcmp(devname
,"stdio")) {
5420 monitor_device
= NULL
;
5421 serial_devices
[i
] = "mon:stdio";
5426 if (monitor_device
) {
5427 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5429 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5432 monitor_init(monitor_hd
, !nographic
);
5435 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5436 const char *devname
= serial_devices
[i
];
5437 if (devname
&& strcmp(devname
, "none")) {
5439 snprintf(label
, sizeof(label
), "serial%d", i
);
5440 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5441 if (!serial_hds
[i
]) {
5442 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5446 if (strstart(devname
, "vc", 0))
5447 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5451 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5452 const char *devname
= parallel_devices
[i
];
5453 if (devname
&& strcmp(devname
, "none")) {
5455 snprintf(label
, sizeof(label
), "parallel%d", i
);
5456 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5457 if (!parallel_hds
[i
]) {
5458 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5462 if (strstart(devname
, "vc", 0))
5463 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5467 if (kvm_enabled()) {
5470 ret
= kvm_init(smp_cpus
);
5472 fprintf(stderr
, "failed to initialize KVM\n");
5477 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5478 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5480 /* Set KVM's vcpu state to qemu's initial CPUState. */
5481 if (kvm_enabled()) {
5484 ret
= kvm_sync_vcpus();
5486 fprintf(stderr
, "failed to initialize vcpus\n");
5491 /* init USB devices */
5493 for(i
= 0; i
< usb_devices_index
; i
++) {
5494 if (usb_device_add(usb_devices
[i
]) < 0) {
5495 fprintf(stderr
, "Warning: could not add USB device %s\n",
5501 if (display_state
.dpy_refresh
) {
5502 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5503 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5506 #ifdef CONFIG_GDBSTUB
5508 /* XXX: use standard host:port notation and modify options
5510 if (gdbserver_start(gdbstub_port
) < 0) {
5511 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5522 autostart
= 0; /* fixme how to deal with -daemonize */
5523 qemu_start_incoming_migration(incoming
);
5527 /* XXX: simplify init */
5540 len
= write(fds
[1], &status
, 1);
5541 if (len
== -1 && (errno
== EINTR
))
5548 TFR(fd
= open("/dev/null", O_RDWR
));