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 "hw/device-assignment.h"
43 #include "migration.h"
57 #include <sys/times.h>
61 #include <sys/ioctl.h>
62 #include <sys/resource.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
66 #if defined(__NetBSD__)
67 #include <net/if_tap.h>
70 #include <linux/if_tun.h>
72 #include <arpa/inet.h>
75 #include <sys/select.h>
83 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
84 #include <freebsd/stdlib.h>
89 #include <linux/rtc.h>
91 /* For the benefit of older linux systems which don't supply it,
92 we use a local copy of hpet.h. */
93 /* #include <linux/hpet.h> */
96 #include <linux/ppdev.h>
97 #include <linux/parport.h>
100 #include <sys/stat.h>
101 #include <sys/ethernet.h>
102 #include <sys/sockio.h>
103 #include <netinet/arp.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip.h>
107 #include <netinet/ip_icmp.h> // must come after ip.h
108 #include <netinet/udp.h>
109 #include <netinet/tcp.h>
117 #include "qemu_socket.h"
119 #if defined(CONFIG_SLIRP)
120 #include "libslirp.h"
123 #if defined(__OpenBSD__)
127 #if defined(CONFIG_VDE)
128 #include <libvdeplug.h>
133 #include <sys/timeb.h>
134 #include <mmsystem.h>
135 #define getopt_long_only getopt_long
136 #define memalign(align, size) malloc(size)
143 #endif /* CONFIG_SDL */
147 #define main qemu_main
148 #endif /* CONFIG_COCOA */
152 #include "exec-all.h"
154 #include "qemu-kvm.h"
156 //#define DEBUG_UNUSED_IOPORT
157 //#define DEBUG_IOPORT
159 //#define DEBUG_SLIRP
162 #define DEFAULT_RAM_SIZE 144
164 #define DEFAULT_RAM_SIZE 128
167 /* Max number of USB devices that can be specified on the commandline. */
168 #define MAX_USB_CMDLINE 8
170 /* Max number of bluetooth switches on the commandline. */
171 #define MAX_BT_CMDLINE 10
173 /* XXX: use a two level table to limit memory usage */
174 #define MAX_IOPORTS 65536
176 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
177 const char *bios_name
= NULL
;
178 static void *ioport_opaque
[MAX_IOPORTS
];
179 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
180 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
181 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
182 to store the VM snapshots */
183 DriveInfo drives_table
[MAX_DRIVES
+1];
185 int extboot_drive
= -1;
186 static int vga_ram_size
;
187 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
188 DisplayState display_state
;
191 const char* keyboard_layout
= NULL
;
192 int64_t ticks_per_sec
;
195 NICInfo nd_table
[MAX_NICS
];
197 static int rtc_utc
= 1;
198 static int rtc_date_offset
= -1; /* -1 means no change */
199 int cirrus_vga_enabled
= 1;
200 int vmsvga_enabled
= 0;
202 int graphic_width
= 1024;
203 int graphic_height
= 768;
204 int graphic_depth
= 8;
206 int graphic_width
= 800;
207 int graphic_height
= 600;
208 int graphic_depth
= 15;
210 static int full_screen
= 0;
212 static int no_frame
= 0;
215 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
216 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
218 int win2k_install_hack
= 0;
221 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
222 int assigned_devices_index
;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
233 const char *incoming
;
234 const char *option_rom
[MAX_OPTION_ROMS
];
236 int semihosting_enabled
= 0;
237 int time_drift_fix
= 0;
238 unsigned int kvm_shadow_memory
= 0;
239 const char *mem_path
= NULL
;
241 const char *cpu_vendor_string
;
245 const char *qemu_name
;
247 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
248 unsigned int nb_prom_envs
= 0;
249 const char *prom_envs
[MAX_PROM_ENVS
];
252 struct drive_opt drives_opt
[MAX_DRIVES
];
254 static CPUState
*cur_cpu
;
255 static CPUState
*next_cpu
;
256 static int event_pending
= 1;
257 /* Conversion factor from emulated instructions to virtual clock ticks. */
258 static int icount_time_shift
;
259 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
260 #define MAX_ICOUNT_SHIFT 10
261 /* Compensate for varying guest execution speed. */
262 static int64_t qemu_icount_bias
;
263 static QEMUTimer
*icount_rt_timer
;
264 static QEMUTimer
*icount_vm_timer
;
266 uint8_t qemu_uuid
[16];
268 /* KVM runs the main loop in a separate thread. If we update one of the lists
269 * that are polled before or after select(), we need to make sure to break out
270 * of the select() to ensure the new item is serviced.
272 static void main_loop_break(void)
275 qemu_kvm_notify_work();
278 /***********************************************************/
279 /* x86 ISA bus support */
281 target_phys_addr_t isa_mem_base
= 0;
284 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
285 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
287 static uint32_t ioport_read(int index
, uint32_t address
)
289 static IOPortReadFunc
*default_func
[3] = {
290 default_ioport_readb
,
291 default_ioport_readw
,
294 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
296 func
= default_func
[index
];
297 return func(ioport_opaque
[address
], address
);
300 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
302 static IOPortWriteFunc
*default_func
[3] = {
303 default_ioport_writeb
,
304 default_ioport_writew
,
305 default_ioport_writel
307 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
309 func
= default_func
[index
];
310 func(ioport_opaque
[address
], address
, data
);
313 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
315 #ifdef DEBUG_UNUSED_IOPORT
316 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
321 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
323 #ifdef DEBUG_UNUSED_IOPORT
324 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
328 /* default is to make two byte accesses */
329 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
332 data
= ioport_read(0, address
);
333 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
334 data
|= ioport_read(0, address
) << 8;
338 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
340 ioport_write(0, address
, data
& 0xff);
341 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
342 ioport_write(0, address
, (data
>> 8) & 0xff);
345 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
347 #ifdef DEBUG_UNUSED_IOPORT
348 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
353 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
355 #ifdef DEBUG_UNUSED_IOPORT
356 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
360 /* size is the word size in byte */
361 int register_ioport_read(int start
, int length
, int size
,
362 IOPortReadFunc
*func
, void *opaque
)
368 } else if (size
== 2) {
370 } else if (size
== 4) {
373 hw_error("register_ioport_read: invalid size");
376 for(i
= start
; i
< start
+ length
; i
+= size
) {
377 ioport_read_table
[bsize
][i
] = func
;
378 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
379 hw_error("register_ioport_read: invalid opaque");
380 ioport_opaque
[i
] = opaque
;
385 /* size is the word size in byte */
386 int register_ioport_write(int start
, int length
, int size
,
387 IOPortWriteFunc
*func
, void *opaque
)
393 } else if (size
== 2) {
395 } else if (size
== 4) {
398 hw_error("register_ioport_write: invalid size");
401 for(i
= start
; i
< start
+ length
; i
+= size
) {
402 ioport_write_table
[bsize
][i
] = func
;
403 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
404 hw_error("register_ioport_write: invalid opaque");
405 ioport_opaque
[i
] = opaque
;
410 void isa_unassign_ioport(int start
, int length
)
414 for(i
= start
; i
< start
+ length
; i
++) {
415 ioport_read_table
[0][i
] = default_ioport_readb
;
416 ioport_read_table
[1][i
] = default_ioport_readw
;
417 ioport_read_table
[2][i
] = default_ioport_readl
;
419 ioport_write_table
[0][i
] = default_ioport_writeb
;
420 ioport_write_table
[1][i
] = default_ioport_writew
;
421 ioport_write_table
[2][i
] = default_ioport_writel
;
423 ioport_opaque
[i
] = NULL
;
427 /***********************************************************/
429 void cpu_outb(CPUState
*env
, int addr
, int val
)
432 if (loglevel
& CPU_LOG_IOPORT
)
433 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
435 ioport_write(0, addr
, val
);
438 env
->last_io_time
= cpu_get_time_fast();
442 void cpu_outw(CPUState
*env
, int addr
, int val
)
445 if (loglevel
& CPU_LOG_IOPORT
)
446 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
448 ioport_write(1, addr
, val
);
451 env
->last_io_time
= cpu_get_time_fast();
455 void cpu_outl(CPUState
*env
, int addr
, int val
)
458 if (loglevel
& CPU_LOG_IOPORT
)
459 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
461 ioport_write(2, addr
, val
);
464 env
->last_io_time
= cpu_get_time_fast();
468 int cpu_inb(CPUState
*env
, int addr
)
471 val
= ioport_read(0, addr
);
473 if (loglevel
& CPU_LOG_IOPORT
)
474 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
478 env
->last_io_time
= cpu_get_time_fast();
483 int cpu_inw(CPUState
*env
, int addr
)
486 val
= ioport_read(1, addr
);
488 if (loglevel
& CPU_LOG_IOPORT
)
489 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
493 env
->last_io_time
= cpu_get_time_fast();
498 int cpu_inl(CPUState
*env
, int addr
)
501 val
= ioport_read(2, addr
);
503 if (loglevel
& CPU_LOG_IOPORT
)
504 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
508 env
->last_io_time
= cpu_get_time_fast();
513 /***********************************************************/
514 void hw_error(const char *fmt
, ...)
520 fprintf(stderr
, "qemu: hardware error: ");
521 vfprintf(stderr
, fmt
, ap
);
522 fprintf(stderr
, "\n");
523 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
524 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
526 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
528 cpu_dump_state(env
, stderr
, fprintf
, 0);
538 static QEMUBalloonEvent
*qemu_balloon_event
;
539 void *qemu_balloon_event_opaque
;
541 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
543 qemu_balloon_event
= func
;
544 qemu_balloon_event_opaque
= opaque
;
547 void qemu_balloon(ram_addr_t target
)
549 if (qemu_balloon_event
)
550 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
553 ram_addr_t
qemu_balloon_status(void)
555 if (qemu_balloon_event
)
556 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
560 /***********************************************************/
563 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
564 static void *qemu_put_kbd_event_opaque
;
565 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
566 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
568 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
570 qemu_put_kbd_event_opaque
= opaque
;
571 qemu_put_kbd_event
= func
;
574 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
575 void *opaque
, int absolute
,
578 QEMUPutMouseEntry
*s
, *cursor
;
580 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
584 s
->qemu_put_mouse_event
= func
;
585 s
->qemu_put_mouse_event_opaque
= opaque
;
586 s
->qemu_put_mouse_event_absolute
= absolute
;
587 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
590 if (!qemu_put_mouse_event_head
) {
591 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
595 cursor
= qemu_put_mouse_event_head
;
596 while (cursor
->next
!= NULL
)
597 cursor
= cursor
->next
;
600 qemu_put_mouse_event_current
= s
;
605 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
607 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
609 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
612 cursor
= qemu_put_mouse_event_head
;
613 while (cursor
!= NULL
&& cursor
!= entry
) {
615 cursor
= cursor
->next
;
618 if (cursor
== NULL
) // does not exist or list empty
620 else if (prev
== NULL
) { // entry is head
621 qemu_put_mouse_event_head
= cursor
->next
;
622 if (qemu_put_mouse_event_current
== entry
)
623 qemu_put_mouse_event_current
= cursor
->next
;
624 qemu_free(entry
->qemu_put_mouse_event_name
);
629 prev
->next
= entry
->next
;
631 if (qemu_put_mouse_event_current
== entry
)
632 qemu_put_mouse_event_current
= prev
;
634 qemu_free(entry
->qemu_put_mouse_event_name
);
638 void kbd_put_keycode(int keycode
)
640 if (qemu_put_kbd_event
) {
641 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
645 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
647 QEMUPutMouseEvent
*mouse_event
;
648 void *mouse_event_opaque
;
651 if (!qemu_put_mouse_event_current
) {
656 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
658 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
661 if (graphic_rotate
) {
662 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
665 width
= graphic_width
- 1;
666 mouse_event(mouse_event_opaque
,
667 width
- dy
, dx
, dz
, buttons_state
);
669 mouse_event(mouse_event_opaque
,
670 dx
, dy
, dz
, buttons_state
);
674 int kbd_mouse_is_absolute(void)
676 if (!qemu_put_mouse_event_current
)
679 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
682 void do_info_mice(void)
684 QEMUPutMouseEntry
*cursor
;
687 if (!qemu_put_mouse_event_head
) {
688 term_printf("No mouse devices connected\n");
692 term_printf("Mouse devices available:\n");
693 cursor
= qemu_put_mouse_event_head
;
694 while (cursor
!= NULL
) {
695 term_printf("%c Mouse #%d: %s\n",
696 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
697 index
, cursor
->qemu_put_mouse_event_name
);
699 cursor
= cursor
->next
;
703 void do_mouse_set(int index
)
705 QEMUPutMouseEntry
*cursor
;
708 if (!qemu_put_mouse_event_head
) {
709 term_printf("No mouse devices connected\n");
713 cursor
= qemu_put_mouse_event_head
;
714 while (cursor
!= NULL
&& index
!= i
) {
716 cursor
= cursor
->next
;
720 qemu_put_mouse_event_current
= cursor
;
722 term_printf("Mouse at given index not found\n");
725 /* compute with 96 bit intermediate result: (a*b)/c */
726 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
731 #ifdef WORDS_BIGENDIAN
741 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
742 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
745 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
749 /***********************************************************/
750 /* real time host monotonic timer */
752 #define QEMU_TIMER_BASE 1000000000LL
756 static int64_t clock_freq
;
758 static void init_get_clock(void)
762 ret
= QueryPerformanceFrequency(&freq
);
764 fprintf(stderr
, "Could not calibrate ticks\n");
767 clock_freq
= freq
.QuadPart
;
770 static int64_t get_clock(void)
773 QueryPerformanceCounter(&ti
);
774 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
779 static int use_rt_clock
;
781 static void init_get_clock(void)
784 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
787 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
794 static int64_t get_clock(void)
796 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
799 clock_gettime(CLOCK_MONOTONIC
, &ts
);
800 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
804 /* XXX: using gettimeofday leads to problems if the date
805 changes, so it should be avoided. */
807 gettimeofday(&tv
, NULL
);
808 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
813 /* Return the virtual CPU time, based on the instruction counter. */
814 static int64_t cpu_get_icount(void)
817 CPUState
*env
= cpu_single_env
;;
818 icount
= qemu_icount
;
821 fprintf(stderr
, "Bad clock read\n");
822 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
824 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
827 /***********************************************************/
828 /* guest cycle counter */
830 static int64_t cpu_ticks_prev
;
831 static int64_t cpu_ticks_offset
;
832 static int64_t cpu_clock_offset
;
833 static int cpu_ticks_enabled
;
835 /* return the host CPU cycle counter and handle stop/restart */
836 int64_t cpu_get_ticks(void)
839 return cpu_get_icount();
841 if (!cpu_ticks_enabled
) {
842 return cpu_ticks_offset
;
845 ticks
= cpu_get_real_ticks();
846 if (cpu_ticks_prev
> ticks
) {
847 /* Note: non increasing ticks may happen if the host uses
849 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
851 cpu_ticks_prev
= ticks
;
852 return ticks
+ cpu_ticks_offset
;
856 /* return the host CPU monotonic timer and handle stop/restart */
857 static int64_t cpu_get_clock(void)
860 if (!cpu_ticks_enabled
) {
861 return cpu_clock_offset
;
864 return ti
+ cpu_clock_offset
;
868 /* enable cpu_get_ticks() */
869 void cpu_enable_ticks(void)
871 if (!cpu_ticks_enabled
) {
872 cpu_ticks_offset
-= cpu_get_real_ticks();
873 cpu_clock_offset
-= get_clock();
874 cpu_ticks_enabled
= 1;
878 /* disable cpu_get_ticks() : the clock is stopped. You must not call
879 cpu_get_ticks() after that. */
880 void cpu_disable_ticks(void)
882 if (cpu_ticks_enabled
) {
883 cpu_ticks_offset
= cpu_get_ticks();
884 cpu_clock_offset
= cpu_get_clock();
885 cpu_ticks_enabled
= 0;
889 /***********************************************************/
892 #define QEMU_TIMER_REALTIME 0
893 #define QEMU_TIMER_VIRTUAL 1
897 /* XXX: add frequency */
905 struct QEMUTimer
*next
;
908 struct qemu_alarm_timer
{
912 int (*start
)(struct qemu_alarm_timer
*t
);
913 void (*stop
)(struct qemu_alarm_timer
*t
);
914 void (*rearm
)(struct qemu_alarm_timer
*t
);
918 #define ALARM_FLAG_DYNTICKS 0x1
919 #define ALARM_FLAG_EXPIRED 0x2
921 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
923 return t
->flags
& ALARM_FLAG_DYNTICKS
;
926 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
928 if (!alarm_has_dynticks(t
))
934 /* TODO: MIN_TIMER_REARM_US should be optimized */
935 #define MIN_TIMER_REARM_US 250
937 static struct qemu_alarm_timer
*alarm_timer
;
939 static int alarm_timer_rfd
, alarm_timer_wfd
;
944 struct qemu_alarm_win32
{
948 } alarm_win32_data
= {0, NULL
, -1};
950 static int win32_start_timer(struct qemu_alarm_timer
*t
);
951 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
952 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
956 static int unix_start_timer(struct qemu_alarm_timer
*t
);
957 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
961 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
962 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
963 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
965 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
966 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
968 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
969 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
971 #endif /* __linux__ */
975 /* Correlation between real and virtual time is always going to be
976 fairly approximate, so ignore small variation.
977 When the guest is idle real and virtual time will be aligned in
979 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
981 static void icount_adjust(void)
986 static int64_t last_delta
;
987 /* If the VM is not running, then do nothing. */
991 cur_time
= cpu_get_clock();
992 cur_icount
= qemu_get_clock(vm_clock
);
993 delta
= cur_icount
- cur_time
;
994 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
996 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
997 && icount_time_shift
> 0) {
998 /* The guest is getting too far ahead. Slow time down. */
1002 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1003 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1004 /* The guest is getting too far behind. Speed time up. */
1005 icount_time_shift
++;
1008 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1011 static void icount_adjust_rt(void * opaque
)
1013 qemu_mod_timer(icount_rt_timer
,
1014 qemu_get_clock(rt_clock
) + 1000);
1018 static void icount_adjust_vm(void * opaque
)
1020 qemu_mod_timer(icount_vm_timer
,
1021 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1025 static void init_icount_adjust(void)
1027 /* Have both realtime and virtual time triggers for speed adjustment.
1028 The realtime trigger catches emulated time passing too slowly,
1029 the virtual time trigger catches emulated time passing too fast.
1030 Realtime triggers occur even when idle, so use them less frequently
1031 than VM triggers. */
1032 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1033 qemu_mod_timer(icount_rt_timer
,
1034 qemu_get_clock(rt_clock
) + 1000);
1035 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1036 qemu_mod_timer(icount_vm_timer
,
1037 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1040 static struct qemu_alarm_timer alarm_timers
[] = {
1043 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1044 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1045 /* HPET - if available - is preferred */
1046 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1047 /* ...otherwise try RTC */
1048 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1050 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1052 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1053 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1054 {"win32", 0, win32_start_timer
,
1055 win32_stop_timer
, NULL
, &alarm_win32_data
},
1060 static void show_available_alarms(void)
1064 printf("Available alarm timers, in order of precedence:\n");
1065 for (i
= 0; alarm_timers
[i
].name
; i
++)
1066 printf("%s\n", alarm_timers
[i
].name
);
1069 static void configure_alarms(char const *opt
)
1073 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1076 struct qemu_alarm_timer tmp
;
1078 if (!strcmp(opt
, "?")) {
1079 show_available_alarms();
1085 /* Reorder the array */
1086 name
= strtok(arg
, ",");
1088 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1089 if (!strcmp(alarm_timers
[i
].name
, name
))
1094 fprintf(stderr
, "Unknown clock %s\n", name
);
1103 tmp
= alarm_timers
[i
];
1104 alarm_timers
[i
] = alarm_timers
[cur
];
1105 alarm_timers
[cur
] = tmp
;
1109 name
= strtok(NULL
, ",");
1115 /* Disable remaining timers */
1116 for (i
= cur
; i
< count
; i
++)
1117 alarm_timers
[i
].name
= NULL
;
1119 show_available_alarms();
1124 QEMUClock
*rt_clock
;
1125 QEMUClock
*vm_clock
;
1127 static QEMUTimer
*active_timers
[2];
1129 static QEMUClock
*qemu_new_clock(int type
)
1132 clock
= qemu_mallocz(sizeof(QEMUClock
));
1139 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1143 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1146 ts
->opaque
= opaque
;
1150 void qemu_free_timer(QEMUTimer
*ts
)
1155 /* stop a timer, but do not dealloc it */
1156 void qemu_del_timer(QEMUTimer
*ts
)
1160 /* NOTE: this code must be signal safe because
1161 qemu_timer_expired() can be called from a signal. */
1162 pt
= &active_timers
[ts
->clock
->type
];
1175 /* modify the current timer so that it will be fired when current_time
1176 >= expire_time. The corresponding callback will be called. */
1177 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1183 /* add the timer in the sorted list */
1184 /* NOTE: this code must be signal safe because
1185 qemu_timer_expired() can be called from a signal. */
1186 pt
= &active_timers
[ts
->clock
->type
];
1191 if (t
->expire_time
> expire_time
)
1195 ts
->expire_time
= expire_time
;
1199 /* Rearm if necessary */
1200 if (pt
== &active_timers
[ts
->clock
->type
]) {
1201 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1202 qemu_rearm_alarm_timer(alarm_timer
);
1204 /* Interrupt execution to force deadline recalculation. */
1205 if (use_icount
&& cpu_single_env
) {
1206 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1211 int qemu_timer_pending(QEMUTimer
*ts
)
1214 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1221 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1225 return (timer_head
->expire_time
<= current_time
);
1228 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1234 if (!ts
|| ts
->expire_time
> current_time
)
1236 /* remove timer from the list before calling the callback */
1237 *ptimer_head
= ts
->next
;
1240 /* run the callback (the timer list can be modified) */
1245 int64_t qemu_get_clock(QEMUClock
*clock
)
1247 switch(clock
->type
) {
1248 case QEMU_TIMER_REALTIME
:
1249 return get_clock() / 1000000;
1251 case QEMU_TIMER_VIRTUAL
:
1253 return cpu_get_icount();
1255 return cpu_get_clock();
1260 static void init_timers(void)
1263 ticks_per_sec
= QEMU_TIMER_BASE
;
1264 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1265 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1269 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1271 uint64_t expire_time
;
1273 if (qemu_timer_pending(ts
)) {
1274 expire_time
= ts
->expire_time
;
1278 qemu_put_be64(f
, expire_time
);
1281 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1283 uint64_t expire_time
;
1285 expire_time
= qemu_get_be64(f
);
1286 if (expire_time
!= -1) {
1287 qemu_mod_timer(ts
, expire_time
);
1293 static void timer_save(QEMUFile
*f
, void *opaque
)
1295 if (cpu_ticks_enabled
) {
1296 hw_error("cannot save state if virtual timers are running");
1298 qemu_put_be64(f
, cpu_ticks_offset
);
1299 qemu_put_be64(f
, ticks_per_sec
);
1300 qemu_put_be64(f
, cpu_clock_offset
);
1303 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1305 if (version_id
!= 1 && version_id
!= 2)
1307 if (cpu_ticks_enabled
) {
1310 cpu_ticks_offset
=qemu_get_be64(f
);
1311 ticks_per_sec
=qemu_get_be64(f
);
1312 if (version_id
== 2) {
1313 cpu_clock_offset
=qemu_get_be64(f
);
1319 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1320 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1322 static void host_alarm_handler(int host_signum
)
1326 #define DISP_FREQ 1000
1328 static int64_t delta_min
= INT64_MAX
;
1329 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1331 ti
= qemu_get_clock(vm_clock
);
1332 if (last_clock
!= 0) {
1333 delta
= ti
- last_clock
;
1334 if (delta
< delta_min
)
1336 if (delta
> delta_max
)
1339 if (++count
== DISP_FREQ
) {
1340 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1341 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1342 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1343 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1344 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1346 delta_min
= INT64_MAX
;
1355 alarm_has_dynticks(alarm_timer
) ||
1357 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1358 qemu_get_clock(vm_clock
))) ||
1359 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1360 qemu_get_clock(rt_clock
))) {
1361 CPUState
*env
= next_cpu
;
1364 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1365 SetEvent(data
->host_alarm
);
1367 static const char byte
= 0;
1368 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1370 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1373 /* stop the currently executing cpu because a timer occured */
1374 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1376 if (env
->kqemu_enabled
) {
1377 kqemu_cpu_interrupt(env
);
1385 static int64_t qemu_next_deadline(void)
1389 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1390 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1391 qemu_get_clock(vm_clock
);
1393 /* To avoid problems with overflow limit this to 2^32. */
1403 #if defined(__linux__) || defined(_WIN32)
1404 static uint64_t qemu_next_deadline_dyntick(void)
1412 delta
= (qemu_next_deadline() + 999) / 1000;
1414 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1415 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1416 qemu_get_clock(rt_clock
))*1000;
1417 if (rtdelta
< delta
)
1421 if (delta
< MIN_TIMER_REARM_US
)
1422 delta
= MIN_TIMER_REARM_US
;
1430 /* Sets a specific flag */
1431 static int fcntl_setfl(int fd
, int flag
)
1435 flags
= fcntl(fd
, F_GETFL
);
1439 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1445 #if defined(__linux__)
1447 #define RTC_FREQ 1024
1449 static void enable_sigio_timer(int fd
)
1451 struct sigaction act
;
1454 sigfillset(&act
.sa_mask
);
1456 act
.sa_handler
= host_alarm_handler
;
1458 sigaction(SIGIO
, &act
, NULL
);
1459 fcntl_setfl(fd
, O_ASYNC
);
1460 fcntl(fd
, F_SETOWN
, getpid());
1463 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1465 struct hpet_info info
;
1468 fd
= open("/dev/hpet", O_RDONLY
);
1473 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1475 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1476 "error, but for better emulation accuracy type:\n"
1477 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1481 /* Check capabilities */
1482 r
= ioctl(fd
, HPET_INFO
, &info
);
1486 /* Enable periodic mode */
1487 r
= ioctl(fd
, HPET_EPI
, 0);
1488 if (info
.hi_flags
&& (r
< 0))
1491 /* Enable interrupt */
1492 r
= ioctl(fd
, HPET_IE_ON
, 0);
1496 enable_sigio_timer(fd
);
1497 t
->priv
= (void *)(long)fd
;
1505 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1507 int fd
= (long)t
->priv
;
1512 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1515 unsigned long current_rtc_freq
= 0;
1517 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1520 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1521 if (current_rtc_freq
!= RTC_FREQ
&&
1522 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1523 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1524 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1525 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1528 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1534 enable_sigio_timer(rtc_fd
);
1536 t
->priv
= (void *)(long)rtc_fd
;
1541 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1543 int rtc_fd
= (long)t
->priv
;
1548 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1552 struct sigaction act
;
1554 sigfillset(&act
.sa_mask
);
1556 act
.sa_handler
= host_alarm_handler
;
1558 sigaction(SIGALRM
, &act
, NULL
);
1560 ev
.sigev_value
.sival_int
= 0;
1561 ev
.sigev_notify
= SIGEV_SIGNAL
;
1562 ev
.sigev_signo
= SIGALRM
;
1564 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1565 perror("timer_create");
1567 /* disable dynticks */
1568 fprintf(stderr
, "Dynamic Ticks disabled\n");
1573 t
->priv
= (void *)(long)host_timer
;
1578 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1580 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1582 timer_delete(host_timer
);
1585 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1587 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1588 struct itimerspec timeout
;
1589 int64_t nearest_delta_us
= INT64_MAX
;
1592 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1593 !active_timers
[QEMU_TIMER_VIRTUAL
])
1596 nearest_delta_us
= qemu_next_deadline_dyntick();
1598 /* check whether a timer is already running */
1599 if (timer_gettime(host_timer
, &timeout
)) {
1601 fprintf(stderr
, "Internal timer error: aborting\n");
1604 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1605 if (current_us
&& current_us
<= nearest_delta_us
)
1608 timeout
.it_interval
.tv_sec
= 0;
1609 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1610 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1611 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1612 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1614 fprintf(stderr
, "Internal timer error: aborting\n");
1619 #endif /* defined(__linux__) */
1621 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1623 struct sigaction act
;
1624 struct itimerval itv
;
1628 sigfillset(&act
.sa_mask
);
1630 act
.sa_handler
= host_alarm_handler
;
1632 sigaction(SIGALRM
, &act
, NULL
);
1634 itv
.it_interval
.tv_sec
= 0;
1635 /* for i386 kernel 2.6 to get 1 ms */
1636 itv
.it_interval
.tv_usec
= 999;
1637 itv
.it_value
.tv_sec
= 0;
1638 itv
.it_value
.tv_usec
= 10 * 1000;
1640 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1647 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1649 struct itimerval itv
;
1651 memset(&itv
, 0, sizeof(itv
));
1652 setitimer(ITIMER_REAL
, &itv
, NULL
);
1655 #endif /* !defined(_WIN32) */
1657 static void try_to_rearm_timer(void *opaque
)
1659 struct qemu_alarm_timer
*t
= opaque
;
1663 /* Drain the notify pipe */
1666 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1667 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1670 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1671 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1672 qemu_rearm_alarm_timer(alarm_timer
);
1678 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1681 struct qemu_alarm_win32
*data
= t
->priv
;
1684 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1685 if (!data
->host_alarm
) {
1686 perror("Failed CreateEvent");
1690 memset(&tc
, 0, sizeof(tc
));
1691 timeGetDevCaps(&tc
, sizeof(tc
));
1693 if (data
->period
< tc
.wPeriodMin
)
1694 data
->period
= tc
.wPeriodMin
;
1696 timeBeginPeriod(data
->period
);
1698 flags
= TIME_CALLBACK_FUNCTION
;
1699 if (alarm_has_dynticks(t
))
1700 flags
|= TIME_ONESHOT
;
1702 flags
|= TIME_PERIODIC
;
1704 data
->timerId
= timeSetEvent(1, // interval (ms)
1705 data
->period
, // resolution
1706 host_alarm_handler
, // function
1707 (DWORD
)t
, // parameter
1710 if (!data
->timerId
) {
1711 perror("Failed to initialize win32 alarm timer");
1713 timeEndPeriod(data
->period
);
1714 CloseHandle(data
->host_alarm
);
1718 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1723 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1725 struct qemu_alarm_win32
*data
= t
->priv
;
1727 timeKillEvent(data
->timerId
);
1728 timeEndPeriod(data
->period
);
1730 CloseHandle(data
->host_alarm
);
1733 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1735 struct qemu_alarm_win32
*data
= t
->priv
;
1736 uint64_t nearest_delta_us
;
1738 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1739 !active_timers
[QEMU_TIMER_VIRTUAL
])
1742 nearest_delta_us
= qemu_next_deadline_dyntick();
1743 nearest_delta_us
/= 1000;
1745 timeKillEvent(data
->timerId
);
1747 data
->timerId
= timeSetEvent(1,
1751 TIME_ONESHOT
| TIME_PERIODIC
);
1753 if (!data
->timerId
) {
1754 perror("Failed to re-arm win32 alarm timer");
1756 timeEndPeriod(data
->period
);
1757 CloseHandle(data
->host_alarm
);
1764 static int init_timer_alarm(void)
1766 struct qemu_alarm_timer
*t
= NULL
;
1776 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1780 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1784 alarm_timer_rfd
= fds
[0];
1785 alarm_timer_wfd
= fds
[1];
1788 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1789 t
= &alarm_timers
[i
];
1802 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1803 try_to_rearm_timer
, NULL
, t
);
1818 static void quit_timers(void)
1820 alarm_timer
->stop(alarm_timer
);
1824 /***********************************************************/
1825 /* host time/date access */
1826 void qemu_get_timedate(struct tm
*tm
, int offset
)
1833 if (rtc_date_offset
== -1) {
1837 ret
= localtime(&ti
);
1839 ti
-= rtc_date_offset
;
1843 memcpy(tm
, ret
, sizeof(struct tm
));
1846 int qemu_timedate_diff(struct tm
*tm
)
1850 if (rtc_date_offset
== -1)
1852 seconds
= mktimegm(tm
);
1854 seconds
= mktime(tm
);
1856 seconds
= mktimegm(tm
) + rtc_date_offset
;
1858 return seconds
- time(NULL
);
1862 static void socket_cleanup(void)
1867 static int socket_init(void)
1872 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1874 err
= WSAGetLastError();
1875 fprintf(stderr
, "WSAStartup: %d\n", err
);
1878 atexit(socket_cleanup
);
1883 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1888 while (*p
!= '\0' && *p
!= '=') {
1889 if (q
&& (q
- buf
) < buf_size
- 1)
1899 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1904 while (*p
!= '\0') {
1906 if (*(p
+ 1) != ',')
1910 if (q
&& (q
- buf
) < buf_size
- 1)
1920 int get_param_value(char *buf
, int buf_size
,
1921 const char *tag
, const char *str
)
1928 p
= get_opt_name(option
, sizeof(option
), p
);
1932 if (!strcmp(tag
, option
)) {
1933 (void)get_opt_value(buf
, buf_size
, p
);
1936 p
= get_opt_value(NULL
, 0, p
);
1945 int check_params(char *buf
, int buf_size
,
1946 const char * const *params
, const char *str
)
1953 p
= get_opt_name(buf
, buf_size
, p
);
1957 for(i
= 0; params
[i
] != NULL
; i
++)
1958 if (!strcmp(params
[i
], buf
))
1960 if (params
[i
] == NULL
)
1962 p
= get_opt_value(NULL
, 0, p
);
1970 /***********************************************************/
1971 /* Bluetooth support */
1974 static struct HCIInfo
*hci_table
[MAX_NICS
];
1976 static struct bt_vlan_s
{
1977 struct bt_scatternet_s net
;
1979 struct bt_vlan_s
*next
;
1982 /* find or alloc a new bluetooth "VLAN" */
1983 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1985 struct bt_vlan_s
**pvlan
, *vlan
;
1986 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1990 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1992 pvlan
= &first_bt_vlan
;
1993 while (*pvlan
!= NULL
)
1994 pvlan
= &(*pvlan
)->next
;
1999 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2003 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2008 static struct HCIInfo null_hci
= {
2009 .cmd_send
= null_hci_send
,
2010 .sco_send
= null_hci_send
,
2011 .acl_send
= null_hci_send
,
2012 .bdaddr_set
= null_hci_addr_set
,
2015 struct HCIInfo
*qemu_next_hci(void)
2017 if (cur_hci
== nb_hcis
)
2020 return hci_table
[cur_hci
++];
2023 static struct HCIInfo
*hci_init(const char *str
)
2026 struct bt_scatternet_s
*vlan
= 0;
2028 if (!strcmp(str
, "null"))
2031 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2033 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2034 else if (!strncmp(str
, "hci", 3)) {
2037 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2038 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2043 vlan
= qemu_find_bt_vlan(0);
2045 return bt_new_hci(vlan
);
2048 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2053 static int bt_hci_parse(const char *str
)
2055 struct HCIInfo
*hci
;
2058 if (nb_hcis
>= MAX_NICS
) {
2059 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2063 hci
= hci_init(str
);
2072 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2073 hci
->bdaddr_set(hci
, bdaddr
.b
);
2075 hci_table
[nb_hcis
++] = hci
;
2080 static void bt_vhci_add(int vlan_id
)
2082 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2085 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2086 "an empty scatternet %i\n", vlan_id
);
2088 bt_vhci_init(bt_new_hci(vlan
));
2091 static struct bt_device_s
*bt_device_add(const char *opt
)
2093 struct bt_scatternet_s
*vlan
;
2095 char *endp
= strstr(opt
, ",vlan=");
2096 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2099 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2102 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2104 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2109 vlan
= qemu_find_bt_vlan(vlan_id
);
2112 fprintf(stderr
, "qemu: warning: adding a slave device to "
2113 "an empty scatternet %i\n", vlan_id
);
2115 if (!strcmp(devname
, "keyboard"))
2116 return bt_keyboard_init(vlan
);
2118 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2122 static int bt_parse(const char *opt
)
2124 const char *endp
, *p
;
2127 if (strstart(opt
, "hci", &endp
)) {
2128 if (!*endp
|| *endp
== ',') {
2130 if (!strstart(endp
, ",vlan=", 0))
2133 return bt_hci_parse(opt
);
2135 } else if (strstart(opt
, "vhci", &endp
)) {
2136 if (!*endp
|| *endp
== ',') {
2138 if (strstart(endp
, ",vlan=", &p
)) {
2139 vlan
= strtol(p
, (char **) &endp
, 0);
2141 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2145 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2154 } else if (strstart(opt
, "device:", &endp
))
2155 return !bt_device_add(endp
);
2157 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2161 /***********************************************************/
2162 /* QEMU Block devices */
2164 #define HD_ALIAS "index=%d,media=disk"
2166 #define CDROM_ALIAS "index=1,media=cdrom"
2168 #define CDROM_ALIAS "index=2,media=cdrom"
2170 #define FD_ALIAS "index=%d,if=floppy"
2171 #define PFLASH_ALIAS "if=pflash"
2172 #define MTD_ALIAS "if=mtd"
2173 #define SD_ALIAS "index=0,if=sd"
2175 static int drive_opt_get_free_idx(void)
2179 for (index
= 0; index
< MAX_DRIVES
; index
++)
2180 if (!drives_opt
[index
].used
) {
2181 drives_opt
[index
].used
= 1;
2188 static int drive_get_free_idx(void)
2192 for (index
= 0; index
< MAX_DRIVES
; index
++)
2193 if (!drives_table
[index
].used
) {
2194 drives_table
[index
].used
= 1;
2201 int drive_add(const char *file
, const char *fmt
, ...)
2204 int index
= drive_opt_get_free_idx();
2206 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2207 fprintf(stderr
, "qemu: too many drives\n");
2211 drives_opt
[index
].file
= file
;
2213 vsnprintf(drives_opt
[index
].opt
,
2214 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2221 void drive_remove(int index
)
2223 drives_opt
[index
].used
= 0;
2227 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2231 /* seek interface, bus and unit */
2233 for (index
= 0; index
< MAX_DRIVES
; index
++)
2234 if (drives_table
[index
].type
== type
&&
2235 drives_table
[index
].bus
== bus
&&
2236 drives_table
[index
].unit
== unit
&&
2237 drives_table
[index
].used
)
2243 int drive_get_max_bus(BlockInterfaceType type
)
2249 for (index
= 0; index
< nb_drives
; index
++) {
2250 if(drives_table
[index
].type
== type
&&
2251 drives_table
[index
].bus
> max_bus
)
2252 max_bus
= drives_table
[index
].bus
;
2257 const char *drive_get_serial(BlockDriverState
*bdrv
)
2261 for (index
= 0; index
< nb_drives
; index
++)
2262 if (drives_table
[index
].bdrv
== bdrv
)
2263 return drives_table
[index
].serial
;
2268 static void bdrv_format_print(void *opaque
, const char *name
)
2270 fprintf(stderr
, " %s", name
);
2273 void drive_uninit(BlockDriverState
*bdrv
)
2277 for (i
= 0; i
< MAX_DRIVES
; i
++)
2278 if (drives_table
[i
].bdrv
== bdrv
) {
2279 drives_table
[i
].bdrv
= NULL
;
2280 drives_table
[i
].used
= 0;
2281 drive_remove(drives_table
[i
].drive_opt_idx
);
2287 int drive_init(struct drive_opt
*arg
, int snapshot
,
2288 QEMUMachine
*machine
)
2294 const char *mediastr
= "";
2295 BlockInterfaceType type
;
2296 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2297 int bus_id
, unit_id
;
2298 int cyls
, heads
, secs
, translation
;
2299 BlockDriverState
*bdrv
;
2300 BlockDriver
*drv
= NULL
;
2305 int drives_table_idx
;
2306 char *str
= arg
->opt
;
2307 static const char * const params
[] = { "bus", "unit", "if", "index",
2308 "cyls", "heads", "secs", "trans",
2309 "media", "snapshot", "file",
2310 "cache", "format", "serial",
2313 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2314 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2320 cyls
= heads
= secs
= 0;
2323 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2327 if (machine
->use_scsi
) {
2329 max_devs
= MAX_SCSI_DEVS
;
2330 pstrcpy(devname
, sizeof(devname
), "scsi");
2333 max_devs
= MAX_IDE_DEVS
;
2334 pstrcpy(devname
, sizeof(devname
), "ide");
2338 /* extract parameters */
2340 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2341 bus_id
= strtol(buf
, NULL
, 0);
2343 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2348 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2349 unit_id
= strtol(buf
, NULL
, 0);
2351 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2356 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2357 pstrcpy(devname
, sizeof(devname
), buf
);
2358 if (!strcmp(buf
, "ide")) {
2360 max_devs
= MAX_IDE_DEVS
;
2361 } else if (!strcmp(buf
, "scsi")) {
2363 max_devs
= MAX_SCSI_DEVS
;
2364 } else if (!strcmp(buf
, "floppy")) {
2367 } else if (!strcmp(buf
, "pflash")) {
2370 } else if (!strcmp(buf
, "mtd")) {
2373 } else if (!strcmp(buf
, "sd")) {
2376 } else if (!strcmp(buf
, "virtio")) {
2380 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2385 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2386 index
= strtol(buf
, NULL
, 0);
2388 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2393 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2394 cyls
= strtol(buf
, NULL
, 0);
2397 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2398 heads
= strtol(buf
, NULL
, 0);
2401 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2402 secs
= strtol(buf
, NULL
, 0);
2405 if (cyls
|| heads
|| secs
) {
2406 if (cyls
< 1 || cyls
> 16383) {
2407 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2410 if (heads
< 1 || heads
> 16) {
2411 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2414 if (secs
< 1 || secs
> 63) {
2415 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2420 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2423 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2427 if (!strcmp(buf
, "none"))
2428 translation
= BIOS_ATA_TRANSLATION_NONE
;
2429 else if (!strcmp(buf
, "lba"))
2430 translation
= BIOS_ATA_TRANSLATION_LBA
;
2431 else if (!strcmp(buf
, "auto"))
2432 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2434 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2439 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2440 if (!strcmp(buf
, "disk")) {
2442 } else if (!strcmp(buf
, "cdrom")) {
2443 if (cyls
|| secs
|| heads
) {
2445 "qemu: '%s' invalid physical CHS format\n", str
);
2448 media
= MEDIA_CDROM
;
2450 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2455 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2456 if (!strcmp(buf
, "on"))
2458 else if (!strcmp(buf
, "off"))
2461 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2466 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2467 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2469 else if (!strcmp(buf
, "writethrough"))
2471 else if (!strcmp(buf
, "writeback"))
2474 fprintf(stderr
, "qemu: invalid cache option\n");
2479 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2480 if (strcmp(buf
, "?") == 0) {
2481 fprintf(stderr
, "qemu: Supported formats:");
2482 bdrv_iterate_format(bdrv_format_print
, NULL
);
2483 fprintf(stderr
, "\n");
2486 drv
= bdrv_find_format(buf
);
2488 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2493 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2494 if (!strcmp(buf
, "on")) {
2495 if (extboot_drive
!= -1) {
2496 fprintf(stderr
, "qemu: two bootable drives specified\n");
2499 extboot_drive
= nb_drives
;
2500 } else if (strcmp(buf
, "off")) {
2501 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2506 if (arg
->file
== NULL
)
2507 get_param_value(file
, sizeof(file
), "file", str
);
2509 pstrcpy(file
, sizeof(file
), arg
->file
);
2511 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2512 memset(serial
, 0, sizeof(serial
));
2514 /* compute bus and unit according index */
2517 if (bus_id
!= 0 || unit_id
!= -1) {
2519 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2527 unit_id
= index
% max_devs
;
2528 bus_id
= index
/ max_devs
;
2532 /* if user doesn't specify a unit_id,
2533 * try to find the first free
2536 if (unit_id
== -1) {
2538 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2540 if (max_devs
&& unit_id
>= max_devs
) {
2541 unit_id
-= max_devs
;
2549 if (max_devs
&& unit_id
>= max_devs
) {
2550 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2551 str
, unit_id
, max_devs
- 1);
2556 * ignore multiple definitions
2559 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2564 if (type
== IF_IDE
|| type
== IF_SCSI
)
2565 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2567 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2568 devname
, bus_id
, mediastr
, unit_id
);
2570 snprintf(buf
, sizeof(buf
), "%s%s%i",
2571 devname
, mediastr
, unit_id
);
2572 bdrv
= bdrv_new(buf
);
2573 drives_table_idx
= drive_get_free_idx();
2574 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2575 drives_table
[drives_table_idx
].type
= type
;
2576 drives_table
[drives_table_idx
].bus
= bus_id
;
2577 drives_table
[drives_table_idx
].unit
= unit_id
;
2578 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2579 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2588 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2589 bdrv_set_translation_hint(bdrv
, translation
);
2593 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2598 /* FIXME: This isn't really a floppy, but it's a reasonable
2601 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2612 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2613 cache
= 2; /* always use write-back with snapshot */
2615 if (cache
== 0) /* no caching */
2616 bdrv_flags
|= BDRV_O_NOCACHE
;
2617 else if (cache
== 2) /* write-back */
2618 bdrv_flags
|= BDRV_O_CACHE_WB
;
2619 else if (cache
== 3) /* not specified */
2620 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2621 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2622 fprintf(stderr
, "qemu: could not open disk image %s\n",
2626 return drives_table_idx
;
2629 /***********************************************************/
2632 static USBPort
*used_usb_ports
;
2633 static USBPort
*free_usb_ports
;
2635 /* ??? Maybe change this to register a hub to keep track of the topology. */
2636 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2637 usb_attachfn attach
)
2639 port
->opaque
= opaque
;
2640 port
->index
= index
;
2641 port
->attach
= attach
;
2642 port
->next
= free_usb_ports
;
2643 free_usb_ports
= port
;
2646 int usb_device_add_dev(USBDevice
*dev
)
2650 /* Find a USB port to add the device to. */
2651 port
= free_usb_ports
;
2655 /* Create a new hub and chain it on. */
2656 free_usb_ports
= NULL
;
2657 port
->next
= used_usb_ports
;
2658 used_usb_ports
= port
;
2660 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2661 usb_attach(port
, hub
);
2662 port
= free_usb_ports
;
2665 free_usb_ports
= port
->next
;
2666 port
->next
= used_usb_ports
;
2667 used_usb_ports
= port
;
2668 usb_attach(port
, dev
);
2672 static int usb_device_add(const char *devname
)
2677 if (!free_usb_ports
)
2680 if (strstart(devname
, "host:", &p
)) {
2681 dev
= usb_host_device_open(p
);
2682 } else if (!strcmp(devname
, "mouse")) {
2683 dev
= usb_mouse_init();
2684 } else if (!strcmp(devname
, "tablet")) {
2685 dev
= usb_tablet_init();
2686 } else if (!strcmp(devname
, "keyboard")) {
2687 dev
= usb_keyboard_init();
2688 } else if (strstart(devname
, "disk:", &p
)) {
2689 dev
= usb_msd_init(p
);
2690 } else if (!strcmp(devname
, "wacom-tablet")) {
2691 dev
= usb_wacom_init();
2692 } else if (strstart(devname
, "serial:", &p
)) {
2693 dev
= usb_serial_init(p
);
2694 #ifdef CONFIG_BRLAPI
2695 } else if (!strcmp(devname
, "braille")) {
2696 dev
= usb_baum_init();
2698 } else if (strstart(devname
, "net:", &p
)) {
2701 if (net_client_init("nic", p
) < 0)
2703 nd_table
[nic
].model
= "usb";
2704 dev
= usb_net_init(&nd_table
[nic
]);
2705 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2706 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2707 bt_new_hci(qemu_find_bt_vlan(0)));
2714 return usb_device_add_dev(dev
);
2717 int usb_device_del_addr(int bus_num
, int addr
)
2723 if (!used_usb_ports
)
2729 lastp
= &used_usb_ports
;
2730 port
= used_usb_ports
;
2731 while (port
&& port
->dev
->addr
!= addr
) {
2732 lastp
= &port
->next
;
2740 *lastp
= port
->next
;
2741 usb_attach(port
, NULL
);
2742 dev
->handle_destroy(dev
);
2743 port
->next
= free_usb_ports
;
2744 free_usb_ports
= port
;
2748 static int usb_device_del(const char *devname
)
2753 if (strstart(devname
, "host:", &p
))
2754 return usb_host_device_close(p
);
2756 if (!used_usb_ports
)
2759 p
= strchr(devname
, '.');
2762 bus_num
= strtoul(devname
, NULL
, 0);
2763 addr
= strtoul(p
+ 1, NULL
, 0);
2765 return usb_device_del_addr(bus_num
, addr
);
2768 void do_usb_add(const char *devname
)
2770 usb_device_add(devname
);
2773 void do_usb_del(const char *devname
)
2775 usb_device_del(devname
);
2782 const char *speed_str
;
2785 term_printf("USB support not enabled\n");
2789 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2793 switch(dev
->speed
) {
2797 case USB_SPEED_FULL
:
2800 case USB_SPEED_HIGH
:
2807 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2808 0, dev
->addr
, speed_str
, dev
->devname
);
2812 /***********************************************************/
2813 /* PCMCIA/Cardbus */
2815 static struct pcmcia_socket_entry_s
{
2816 struct pcmcia_socket_s
*socket
;
2817 struct pcmcia_socket_entry_s
*next
;
2818 } *pcmcia_sockets
= 0;
2820 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2822 struct pcmcia_socket_entry_s
*entry
;
2824 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2825 entry
->socket
= socket
;
2826 entry
->next
= pcmcia_sockets
;
2827 pcmcia_sockets
= entry
;
2830 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2832 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2834 ptr
= &pcmcia_sockets
;
2835 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2836 if (entry
->socket
== socket
) {
2842 void pcmcia_info(void)
2844 struct pcmcia_socket_entry_s
*iter
;
2845 if (!pcmcia_sockets
)
2846 term_printf("No PCMCIA sockets\n");
2848 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2849 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2850 iter
->socket
->attached
? iter
->socket
->card_string
:
2854 /***********************************************************/
2857 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2861 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2865 static void dumb_display_init(DisplayState
*ds
)
2870 ds
->dpy_update
= dumb_update
;
2871 ds
->dpy_resize
= dumb_resize
;
2872 ds
->dpy_refresh
= NULL
;
2873 ds
->gui_timer_interval
= 0;
2877 /***********************************************************/
2880 #define MAX_IO_HANDLERS 64
2882 typedef struct IOHandlerRecord
{
2884 IOCanRWHandler
*fd_read_poll
;
2886 IOHandler
*fd_write
;
2889 /* temporary data */
2891 struct IOHandlerRecord
*next
;
2894 static IOHandlerRecord
*first_io_handler
;
2896 /* XXX: fd_read_poll should be suppressed, but an API change is
2897 necessary in the character devices to suppress fd_can_read(). */
2898 int qemu_set_fd_handler2(int fd
,
2899 IOCanRWHandler
*fd_read_poll
,
2901 IOHandler
*fd_write
,
2904 IOHandlerRecord
**pioh
, *ioh
;
2906 if (!fd_read
&& !fd_write
) {
2907 pioh
= &first_io_handler
;
2912 if (ioh
->fd
== fd
) {
2919 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2923 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2926 ioh
->next
= first_io_handler
;
2927 first_io_handler
= ioh
;
2930 ioh
->fd_read_poll
= fd_read_poll
;
2931 ioh
->fd_read
= fd_read
;
2932 ioh
->fd_write
= fd_write
;
2933 ioh
->opaque
= opaque
;
2940 int qemu_set_fd_handler(int fd
,
2942 IOHandler
*fd_write
,
2945 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2949 /***********************************************************/
2950 /* Polling handling */
2952 typedef struct PollingEntry
{
2955 struct PollingEntry
*next
;
2958 static PollingEntry
*first_polling_entry
;
2960 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2962 PollingEntry
**ppe
, *pe
;
2963 pe
= qemu_mallocz(sizeof(PollingEntry
));
2967 pe
->opaque
= opaque
;
2968 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2973 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2975 PollingEntry
**ppe
, *pe
;
2976 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2978 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2986 /***********************************************************/
2987 /* Wait objects support */
2988 typedef struct WaitObjects
{
2990 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2991 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2992 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2995 static WaitObjects wait_objects
= {0};
2997 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2999 WaitObjects
*w
= &wait_objects
;
3001 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3003 w
->events
[w
->num
] = handle
;
3004 w
->func
[w
->num
] = func
;
3005 w
->opaque
[w
->num
] = opaque
;
3010 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3013 WaitObjects
*w
= &wait_objects
;
3016 for (i
= 0; i
< w
->num
; i
++) {
3017 if (w
->events
[i
] == handle
)
3020 w
->events
[i
] = w
->events
[i
+ 1];
3021 w
->func
[i
] = w
->func
[i
+ 1];
3022 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3030 /***********************************************************/
3031 /* ram save/restore */
3033 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3037 v
= qemu_get_byte(f
);
3040 if (qemu_get_buffer(f
, buf
, len
) != len
)
3044 v
= qemu_get_byte(f
);
3045 memset(buf
, v
, len
);
3051 if (qemu_file_has_error(f
))
3057 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3062 if (qemu_get_be32(f
) != phys_ram_size
)
3064 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3065 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3067 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3074 #define BDRV_HASH_BLOCK_SIZE 1024
3075 #define IOBUF_SIZE 4096
3076 #define RAM_CBLOCK_MAGIC 0xfabe
3078 typedef struct RamDecompressState
{
3081 uint8_t buf
[IOBUF_SIZE
];
3082 } RamDecompressState
;
3084 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3087 memset(s
, 0, sizeof(*s
));
3089 ret
= inflateInit(&s
->zstream
);
3095 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3099 s
->zstream
.avail_out
= len
;
3100 s
->zstream
.next_out
= buf
;
3101 while (s
->zstream
.avail_out
> 0) {
3102 if (s
->zstream
.avail_in
== 0) {
3103 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3105 clen
= qemu_get_be16(s
->f
);
3106 if (clen
> IOBUF_SIZE
)
3108 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3109 s
->zstream
.avail_in
= clen
;
3110 s
->zstream
.next_in
= s
->buf
;
3112 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3113 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3120 static void ram_decompress_close(RamDecompressState
*s
)
3122 inflateEnd(&s
->zstream
);
3125 #define RAM_SAVE_FLAG_FULL 0x01
3126 #define RAM_SAVE_FLAG_COMPRESS 0x02
3127 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3128 #define RAM_SAVE_FLAG_PAGE 0x08
3129 #define RAM_SAVE_FLAG_EOS 0x10
3131 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3133 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3134 uint32_t *array
= (uint32_t *)page
;
3137 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3138 if (array
[i
] != val
)
3145 static int ram_save_block(QEMUFile
*f
)
3147 static ram_addr_t current_addr
= 0;
3148 ram_addr_t saved_addr
= current_addr
;
3149 ram_addr_t addr
= 0;
3152 while (addr
< phys_ram_size
) {
3153 if (kvm_enabled() && current_addr
== 0)
3154 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3155 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3158 cpu_physical_memory_reset_dirty(current_addr
,
3159 current_addr
+ TARGET_PAGE_SIZE
,
3160 MIGRATION_DIRTY_FLAG
);
3162 ch
= *(phys_ram_base
+ current_addr
);
3164 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3165 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3166 qemu_put_byte(f
, ch
);
3168 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3169 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3175 addr
+= TARGET_PAGE_SIZE
;
3176 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3182 static ram_addr_t ram_save_threshold
= 10;
3184 static ram_addr_t
ram_save_remaining(void)
3187 ram_addr_t count
= 0;
3189 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3190 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3197 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3202 /* Make sure all dirty bits are set */
3203 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3204 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3205 cpu_physical_memory_set_dirty(addr
);
3208 /* Enable dirty memory tracking */
3209 cpu_physical_memory_set_dirty_tracking(1);
3211 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3214 while (!qemu_file_rate_limit(f
)) {
3217 ret
= ram_save_block(f
);
3218 if (ret
== 0) /* no more blocks */
3222 /* try transferring iterative blocks of memory */
3225 cpu_physical_memory_set_dirty_tracking(0);
3227 /* flush all remaining blocks regardless of rate limiting */
3228 while (ram_save_block(f
) != 0);
3231 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3233 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3236 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3238 RamDecompressState s1
, *s
= &s1
;
3242 if (ram_decompress_open(s
, f
) < 0)
3244 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3245 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3247 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3248 fprintf(stderr
, "Error while reading ram block header\n");
3252 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3253 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3258 printf("Error block header\n");
3262 ram_decompress_close(s
);
3267 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3272 if (version_id
== 1)
3273 return ram_load_v1(f
, opaque
);
3275 if (version_id
== 2) {
3276 if (qemu_get_be32(f
) != phys_ram_size
)
3278 return ram_load_dead(f
, opaque
);
3281 if (version_id
!= 3)
3285 addr
= qemu_get_be64(f
);
3287 flags
= addr
& ~TARGET_PAGE_MASK
;
3288 addr
&= TARGET_PAGE_MASK
;
3290 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3291 if (addr
!= phys_ram_size
)
3295 if (flags
& RAM_SAVE_FLAG_FULL
) {
3296 if (ram_load_dead(f
, opaque
) < 0)
3300 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3301 uint8_t ch
= qemu_get_byte(f
);
3302 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3303 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3304 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3305 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3310 /***********************************************************/
3311 /* bottom halves (can be seen as timers which expire ASAP) */
3322 static QEMUBH
*first_bh
= NULL
;
3324 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3327 bh
= qemu_mallocz(sizeof(QEMUBH
));
3331 bh
->opaque
= opaque
;
3332 bh
->next
= first_bh
;
3337 int qemu_bh_poll(void)
3343 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3344 if (!bh
->deleted
&& bh
->scheduled
) {
3353 /* remove deleted bhs */
3367 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3375 void qemu_bh_schedule(QEMUBH
*bh
)
3377 CPUState
*env
= cpu_single_env
;
3382 /* stop the currently executing CPU to execute the BH ASAP */
3384 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3389 void qemu_bh_cancel(QEMUBH
*bh
)
3394 void qemu_bh_delete(QEMUBH
*bh
)
3400 static void qemu_bh_update_timeout(int *timeout
)
3404 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3405 if (!bh
->deleted
&& bh
->scheduled
) {
3407 /* idle bottom halves will be polled at least
3409 *timeout
= MIN(10, *timeout
);
3411 /* non-idle bottom halves will be executed
3420 /***********************************************************/
3421 /* machine registration */
3423 static QEMUMachine
*first_machine
= NULL
;
3424 QEMUMachine
*current_machine
= NULL
;
3426 int qemu_register_machine(QEMUMachine
*m
)
3429 pm
= &first_machine
;
3437 static QEMUMachine
*find_machine(const char *name
)
3441 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3442 if (!strcmp(m
->name
, name
))
3448 /***********************************************************/
3449 /* main execution loop */
3451 static void gui_update(void *opaque
)
3453 DisplayState
*ds
= opaque
;
3454 ds
->dpy_refresh(ds
);
3455 qemu_mod_timer(ds
->gui_timer
,
3456 (ds
->gui_timer_interval
?
3457 ds
->gui_timer_interval
:
3458 GUI_REFRESH_INTERVAL
)
3459 + qemu_get_clock(rt_clock
));
3462 struct vm_change_state_entry
{
3463 VMChangeStateHandler
*cb
;
3465 LIST_ENTRY (vm_change_state_entry
) entries
;
3468 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3470 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3473 VMChangeStateEntry
*e
;
3475 e
= qemu_mallocz(sizeof (*e
));
3481 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3485 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3487 LIST_REMOVE (e
, entries
);
3491 static void vm_state_notify(int running
)
3493 VMChangeStateEntry
*e
;
3495 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3496 e
->cb(e
->opaque
, running
);
3500 /* XXX: support several handlers */
3501 static VMStopHandler
*vm_stop_cb
;
3502 static void *vm_stop_opaque
;
3504 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3507 vm_stop_opaque
= opaque
;
3511 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3522 qemu_rearm_alarm_timer(alarm_timer
);
3526 void vm_stop(int reason
)
3529 cpu_disable_ticks();
3533 vm_stop_cb(vm_stop_opaque
, reason
);
3540 /* reset/shutdown handler */
3542 typedef struct QEMUResetEntry
{
3543 QEMUResetHandler
*func
;
3545 struct QEMUResetEntry
*next
;
3548 static QEMUResetEntry
*first_reset_entry
;
3549 static int reset_requested
;
3550 static int shutdown_requested
;
3551 static int powerdown_requested
;
3553 int qemu_shutdown_requested(void)
3555 int r
= shutdown_requested
;
3556 shutdown_requested
= 0;
3560 int qemu_reset_requested(void)
3562 int r
= reset_requested
;
3563 reset_requested
= 0;
3567 int qemu_powerdown_requested(void)
3569 int r
= powerdown_requested
;
3570 powerdown_requested
= 0;
3574 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3576 QEMUResetEntry
**pre
, *re
;
3578 pre
= &first_reset_entry
;
3579 while (*pre
!= NULL
)
3580 pre
= &(*pre
)->next
;
3581 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3583 re
->opaque
= opaque
;
3588 void qemu_system_reset(void)
3592 /* reset all devices */
3593 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3594 re
->func(re
->opaque
);
3598 void qemu_system_reset_request(void)
3601 shutdown_requested
= 1;
3603 reset_requested
= 1;
3606 if (cpu_single_env
) {
3607 qemu_kvm_cpu_stop(cpu_single_env
);
3608 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3613 void qemu_system_shutdown_request(void)
3615 shutdown_requested
= 1;
3617 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3620 void qemu_system_powerdown_request(void)
3622 powerdown_requested
= 1;
3624 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3627 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3632 /* KVM holds a mutex while QEMU code is running, we need hooks to
3633 release the mutex whenever QEMU code sleeps. */
3637 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3645 static void host_main_loop_wait(int *timeout
)
3651 /* XXX: need to suppress polling by better using win32 events */
3653 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3654 ret
|= pe
->func(pe
->opaque
);
3658 WaitObjects
*w
= &wait_objects
;
3660 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3661 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3662 if (w
->func
[ret
- WAIT_OBJECT_0
])
3663 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3665 /* Check for additional signaled events */
3666 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3668 /* Check if event is signaled */
3669 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3670 if(ret2
== WAIT_OBJECT_0
) {
3672 w
->func
[i
](w
->opaque
[i
]);
3673 } else if (ret2
== WAIT_TIMEOUT
) {
3675 err
= GetLastError();
3676 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3679 } else if (ret
== WAIT_TIMEOUT
) {
3681 err
= GetLastError();
3682 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3689 static void host_main_loop_wait(int *timeout
)
3694 void main_loop_wait(int timeout
)
3696 IOHandlerRecord
*ioh
;
3697 fd_set rfds
, wfds
, xfds
;
3701 qemu_bh_update_timeout(&timeout
);
3703 host_main_loop_wait(&timeout
);
3705 /* poll any events */
3706 /* XXX: separate device handlers from system ones */
3711 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3715 (!ioh
->fd_read_poll
||
3716 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3717 FD_SET(ioh
->fd
, &rfds
);
3721 if (ioh
->fd_write
) {
3722 FD_SET(ioh
->fd
, &wfds
);
3728 tv
.tv_sec
= timeout
/ 1000;
3729 tv
.tv_usec
= (timeout
% 1000) * 1000;
3731 #if defined(CONFIG_SLIRP)
3732 if (slirp_is_inited()) {
3733 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3736 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3738 IOHandlerRecord
**pioh
;
3740 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3741 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3742 ioh
->fd_read(ioh
->opaque
);
3743 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3744 FD_CLR(ioh
->fd
, &rfds
);
3746 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3747 ioh
->fd_write(ioh
->opaque
);
3751 /* remove deleted IO handlers */
3752 pioh
= &first_io_handler
;
3762 #if defined(CONFIG_SLIRP)
3763 if (slirp_is_inited()) {
3769 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3773 /* vm time timers */
3774 if (vm_running
&& (!cur_cpu
3775 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3776 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3777 qemu_get_clock(vm_clock
));
3779 /* real time timers */
3780 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3781 qemu_get_clock(rt_clock
));
3783 /* Check bottom-halves last in case any of the earlier events triggered
3789 static int main_loop(void)
3792 #ifdef CONFIG_PROFILER
3798 if (kvm_enabled()) {
3800 cpu_disable_ticks();
3804 cur_cpu
= first_cpu
;
3805 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3812 #ifdef CONFIG_PROFILER
3813 ti
= profile_getclock();
3818 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3819 env
->icount_decr
.u16
.low
= 0;
3820 env
->icount_extra
= 0;
3821 count
= qemu_next_deadline();
3822 count
= (count
+ (1 << icount_time_shift
) - 1)
3823 >> icount_time_shift
;
3824 qemu_icount
+= count
;
3825 decr
= (count
> 0xffff) ? 0xffff : count
;
3827 env
->icount_decr
.u16
.low
= decr
;
3828 env
->icount_extra
= count
;
3830 ret
= cpu_exec(env
);
3831 #ifdef CONFIG_PROFILER
3832 qemu_time
+= profile_getclock() - ti
;
3835 /* Fold pending instructions back into the
3836 instruction counter, and clear the interrupt flag. */
3837 qemu_icount
-= (env
->icount_decr
.u16
.low
3838 + env
->icount_extra
);
3839 env
->icount_decr
.u32
= 0;
3840 env
->icount_extra
= 0;
3842 next_cpu
= env
->next_cpu
?: first_cpu
;
3843 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3844 ret
= EXCP_INTERRUPT
;
3848 if (ret
== EXCP_HLT
) {
3849 /* Give the next CPU a chance to run. */
3853 if (ret
!= EXCP_HALTED
)
3855 /* all CPUs are halted ? */
3861 if (shutdown_requested
) {
3862 ret
= EXCP_INTERRUPT
;
3870 if (reset_requested
) {
3871 reset_requested
= 0;
3872 qemu_system_reset();
3874 kvm_load_registers(env
);
3875 ret
= EXCP_INTERRUPT
;
3877 if (powerdown_requested
) {
3878 powerdown_requested
= 0;
3879 qemu_system_powerdown();
3880 ret
= EXCP_INTERRUPT
;
3882 #ifdef CONFIG_GDBSTUB
3883 if (unlikely(ret
== EXCP_DEBUG
)) {
3884 gdb_set_stop_cpu(cur_cpu
);
3885 vm_stop(EXCP_DEBUG
);
3888 /* If all cpus are halted then wait until the next IRQ */
3889 /* XXX: use timeout computed from timers */
3890 if (ret
== EXCP_HALTED
) {
3894 /* Advance virtual time to the next event. */
3895 if (use_icount
== 1) {
3896 /* When not using an adaptive execution frequency
3897 we tend to get badly out of sync with real time,
3898 so just delay for a reasonable amount of time. */
3901 delta
= cpu_get_icount() - cpu_get_clock();
3904 /* If virtual time is ahead of real time then just
3906 timeout
= (delta
/ 1000000) + 1;
3908 /* Wait for either IO to occur or the next
3910 add
= qemu_next_deadline();
3911 /* We advance the timer before checking for IO.
3912 Limit the amount we advance so that early IO
3913 activity won't get the guest too far ahead. */
3917 add
= (add
+ (1 << icount_time_shift
) - 1)
3918 >> icount_time_shift
;
3920 timeout
= delta
/ 1000000;
3931 if (shutdown_requested
) {
3932 ret
= EXCP_INTERRUPT
;
3937 #ifdef CONFIG_PROFILER
3938 ti
= profile_getclock();
3940 main_loop_wait(timeout
);
3941 #ifdef CONFIG_PROFILER
3942 dev_time
+= profile_getclock() - ti
;
3945 cpu_disable_ticks();
3949 static void help(int exitcode
)
3951 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3952 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3953 "usage: %s [options] [disk_image]\n"
3955 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3957 "Standard options:\n"
3958 "-M machine select emulated machine (-M ? for list)\n"
3959 "-cpu cpu select CPU (-cpu ? for list)\n"
3960 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3961 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3962 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3963 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3964 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3965 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3966 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3968 " use 'file' as a drive image\n"
3969 "-mtdblock file use 'file' as on-board Flash memory image\n"
3970 "-sd file use 'file' as SecureDigital card image\n"
3971 "-pflash file use 'file' as a parallel flash image\n"
3972 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3973 "-snapshot write to temporary files instead of disk image files\n"
3975 "-no-frame open SDL window without a frame and window decorations\n"
3976 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3977 "-no-quit disable SDL window close capability\n"
3980 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3982 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3983 "-smp n set the number of CPUs to 'n' [default=1]\n"
3984 "-nographic disable graphical output and redirect serial I/Os to console\n"
3985 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3987 "-k language use keyboard layout (for example \"fr\" for French)\n"
3990 "-audio-help print list of audio drivers and their options\n"
3991 "-soundhw c1,... enable audio support\n"
3992 " and only specified sound cards (comma separated list)\n"
3993 " use -soundhw ? to get the list of supported cards\n"
3994 " use -soundhw all to enable all of them\n"
3996 "-vga [std|cirrus|vmware]\n"
3997 " select video card type\n"
3998 "-localtime set the real time clock to local time [default=utc]\n"
3999 "-full-screen start in full screen\n"
4001 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4003 "-usb enable the USB driver (will be the default soon)\n"
4004 "-usbdevice name add the host or guest USB device 'name'\n"
4005 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4006 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4008 "-name string set the name of the guest\n"
4009 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4011 "Network options:\n"
4012 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4013 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4015 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4016 " connect the user mode network stack to VLAN 'n' and send\n"
4017 " hostname 'host' to DHCP clients\n"
4020 "-net tap[,vlan=n][,name=str],ifname=name\n"
4021 " connect the host TAP network interface to VLAN 'n'\n"
4023 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4024 " connect the host TAP network interface to VLAN 'n' and use the\n"
4025 " network scripts 'file' (default=%s)\n"
4026 " and 'dfile' (default=%s);\n"
4027 " use '[down]script=no' to disable script execution;\n"
4028 " use 'fd=h' to connect to an already opened TAP interface\n"
4030 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4031 " connect the vlan 'n' to another VLAN using a socket connection\n"
4032 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4033 " connect the vlan 'n' to multicast maddr and port\n"
4035 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4036 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4037 " on host and listening for incoming connections on 'socketpath'.\n"
4038 " Use group 'groupname' and mode 'octalmode' to change default\n"
4039 " ownership and permissions for communication port.\n"
4041 "-net none use it alone to have zero network devices; if no -net option\n"
4042 " is provided, the default is '-net nic -net user'\n"
4044 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
4045 "-bt hci,host[:id]\n"
4046 " Use host's HCI with the given name\n"
4047 "-bt hci[,vlan=n]\n"
4048 " Emulate a standard HCI in virtual scatternet 'n'\n"
4049 "-bt vhci[,vlan=n]\n"
4050 " Add host computer to virtual scatternet 'n' using VHCI\n"
4051 "-bt device:dev[,vlan=n]\n"
4052 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
4055 "-tftp dir allow tftp access to files in dir [-net user]\n"
4056 "-bootp file advertise file in BOOTP replies\n"
4058 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4060 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4061 " redirect TCP or UDP connections from host to guest [-net user]\n"
4064 "Linux boot specific:\n"
4065 "-kernel bzImage use 'bzImage' as kernel image\n"
4066 "-append cmdline use 'cmdline' as kernel command line\n"
4067 "-initrd file use 'file' as initial ram disk\n"
4069 "Debug/Expert options:\n"
4070 "-monitor dev redirect the monitor to char device 'dev'\n"
4071 "-serial dev redirect the serial port to char device 'dev'\n"
4072 "-parallel dev redirect the parallel port to char device 'dev'\n"
4073 "-pidfile file Write PID to 'file'\n"
4074 "-S freeze CPU at startup (use 'c' to start execution)\n"
4075 "-s wait gdb connection to port\n"
4076 "-p port set gdb connection port [default=%s]\n"
4077 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4078 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4079 " translation (t=none or lba) (usually qemu can guess them)\n"
4080 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4082 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4083 "-no-kqemu disable KQEMU kernel module usage\n"
4086 "-enable-kvm enable KVM full virtualization support\n"
4089 #ifndef NO_CPU_EMULATION
4090 "-no-kvm disable KVM hardware virtualization\n"
4092 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4093 "-no-kvm-pit disable KVM kernel mode PIT\n"
4094 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4095 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4096 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4097 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4098 " expose a PCI device to the guest OS.\n"
4099 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4100 " 'string' is used in log output.\n"
4104 "-no-acpi disable ACPI\n"
4105 "-no-hpet disable HPET\n"
4107 #ifdef CONFIG_CURSES
4108 "-curses use a curses/ncurses interface instead of SDL\n"
4110 "-no-reboot exit instead of rebooting\n"
4111 "-no-shutdown stop before shutdown\n"
4112 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4113 "-vnc display start a VNC server on display\n"
4115 "-daemonize daemonize QEMU after initializing\n"
4117 "-tdf inject timer interrupts that got lost\n"
4118 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4119 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also enables allocation of guest memory with huge pages\n"
4120 "-option-rom rom load a file, rom, into the option ROM space\n"
4122 "-prom-env variable=value set OpenBIOS nvram variables\n"
4124 "-clock force the use of the given methods for timer alarm.\n"
4125 " To see what timers are available use -clock ?\n"
4126 "-startdate select initial date of the clock\n"
4127 "-icount [N|auto]\n"
4128 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4130 "During emulation, the following keys are useful:\n"
4131 "ctrl-alt-f toggle full screen\n"
4132 "ctrl-alt-n switch to virtual console 'n'\n"
4133 "ctrl-alt toggle mouse and keyboard grab\n"
4135 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4140 DEFAULT_NETWORK_SCRIPT
,
4141 DEFAULT_NETWORK_DOWN_SCRIPT
,
4143 DEFAULT_GDBSTUB_PORT
,
4148 #define HAS_ARG 0x0001
4163 QEMU_OPTION_mtdblock
,
4167 QEMU_OPTION_snapshot
,
4169 QEMU_OPTION_no_fd_bootchk
,
4172 QEMU_OPTION_nographic
,
4173 QEMU_OPTION_portrait
,
4175 QEMU_OPTION_audio_help
,
4176 QEMU_OPTION_soundhw
,
4198 QEMU_OPTION_localtime
,
4202 QEMU_OPTION_monitor
,
4204 QEMU_OPTION_parallel
,
4206 QEMU_OPTION_full_screen
,
4207 QEMU_OPTION_no_frame
,
4208 QEMU_OPTION_alt_grab
,
4209 QEMU_OPTION_no_quit
,
4210 QEMU_OPTION_pidfile
,
4211 QEMU_OPTION_no_kqemu
,
4212 QEMU_OPTION_kernel_kqemu
,
4213 QEMU_OPTION_enable_kvm
,
4214 QEMU_OPTION_enable_nesting
,
4215 QEMU_OPTION_win2k_hack
,
4217 QEMU_OPTION_usbdevice
,
4220 QEMU_OPTION_no_acpi
,
4221 QEMU_OPTION_no_hpet
,
4224 QEMU_OPTION_no_kvm_irqchip
,
4225 QEMU_OPTION_no_kvm_pit
,
4226 QEMU_OPTION_no_kvm_pit_reinjection
,
4227 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4228 QEMU_OPTION_pcidevice
,
4230 QEMU_OPTION_no_reboot
,
4231 QEMU_OPTION_no_shutdown
,
4232 QEMU_OPTION_show_cursor
,
4233 QEMU_OPTION_daemonize
,
4234 QEMU_OPTION_option_rom
,
4235 QEMU_OPTION_semihosting
,
4236 QEMU_OPTION_cpu_vendor
,
4238 QEMU_OPTION_prom_env
,
4239 QEMU_OPTION_old_param
,
4241 QEMU_OPTION_startdate
,
4242 QEMU_OPTION_tb_size
,
4245 QEMU_OPTION_incoming
,
4247 QEMU_OPTION_kvm_shadow_memory
,
4248 QEMU_OPTION_mempath
,
4251 typedef struct QEMUOption
{
4257 static const QEMUOption qemu_options
[] = {
4258 { "h", 0, QEMU_OPTION_h
},
4259 { "help", 0, QEMU_OPTION_h
},
4261 { "M", HAS_ARG
, QEMU_OPTION_M
},
4262 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4263 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4264 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4265 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4266 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4267 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4268 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4269 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4270 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4271 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4272 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4273 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4274 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4275 { "snapshot", 0, QEMU_OPTION_snapshot
},
4277 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4279 { "m", HAS_ARG
, QEMU_OPTION_m
},
4280 { "nographic", 0, QEMU_OPTION_nographic
},
4281 { "portrait", 0, QEMU_OPTION_portrait
},
4282 { "k", HAS_ARG
, QEMU_OPTION_k
},
4284 { "audio-help", 0, QEMU_OPTION_audio_help
},
4285 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4288 { "net", HAS_ARG
, QEMU_OPTION_net
},
4290 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4291 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4293 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4295 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4297 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4299 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4300 { "append", HAS_ARG
, QEMU_OPTION_append
},
4301 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4303 { "S", 0, QEMU_OPTION_S
},
4304 { "s", 0, QEMU_OPTION_s
},
4305 { "p", HAS_ARG
, QEMU_OPTION_p
},
4306 { "d", HAS_ARG
, QEMU_OPTION_d
},
4307 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4308 { "L", HAS_ARG
, QEMU_OPTION_L
},
4309 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4311 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4312 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4315 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4318 #ifndef NO_CPU_EMULATION
4319 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4321 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4322 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4323 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4324 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4325 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4326 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4329 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4330 { "g", 1, QEMU_OPTION_g
},
4332 { "localtime", 0, QEMU_OPTION_localtime
},
4333 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4334 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4335 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4336 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4337 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4338 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4339 { "incoming", 1, QEMU_OPTION_incoming
},
4340 { "full-screen", 0, QEMU_OPTION_full_screen
},
4342 { "no-frame", 0, QEMU_OPTION_no_frame
},
4343 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4344 { "no-quit", 0, QEMU_OPTION_no_quit
},
4346 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4347 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4348 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4349 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4350 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4351 #ifdef CONFIG_CURSES
4352 { "curses", 0, QEMU_OPTION_curses
},
4354 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4356 /* temporary options */
4357 { "usb", 0, QEMU_OPTION_usb
},
4358 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4359 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4360 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4361 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4362 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4363 { "daemonize", 0, QEMU_OPTION_daemonize
},
4364 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4365 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4366 { "semihosting", 0, QEMU_OPTION_semihosting
},
4368 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4369 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4370 { "name", HAS_ARG
, QEMU_OPTION_name
},
4371 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4372 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4374 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4375 #if defined(TARGET_ARM)
4376 { "old-param", 0, QEMU_OPTION_old_param
},
4378 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4379 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4380 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4381 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4382 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4383 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4387 /* password input */
4389 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4394 if (!bdrv_is_encrypted(bs
))
4397 term_printf("%s is encrypted.\n", name
);
4398 for(i
= 0; i
< 3; i
++) {
4399 monitor_readline("Password: ", 1, password
, sizeof(password
));
4400 if (bdrv_set_key(bs
, password
) == 0)
4402 term_printf("invalid password\n");
4407 static BlockDriverState
*get_bdrv(int index
)
4409 if (index
> nb_drives
)
4411 return drives_table
[index
].bdrv
;
4414 static void read_passwords(void)
4416 BlockDriverState
*bs
;
4419 for(i
= 0; i
< 6; i
++) {
4422 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4427 struct soundhw soundhw
[] = {
4428 #ifdef HAS_AUDIO_CHOICE
4429 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4435 { .init_isa
= pcspk_audio_init
}
4440 "Creative Sound Blaster 16",
4443 { .init_isa
= SB16_init
}
4446 #ifdef CONFIG_CS4231A
4452 { .init_isa
= cs4231a_init
}
4460 "Yamaha YMF262 (OPL3)",
4462 "Yamaha YM3812 (OPL2)",
4466 { .init_isa
= Adlib_init
}
4473 "Gravis Ultrasound GF1",
4476 { .init_isa
= GUS_init
}
4483 "Intel 82801AA AC97 Audio",
4486 { .init_pci
= ac97_init
}
4492 "ENSONIQ AudioPCI ES1370",
4495 { .init_pci
= es1370_init
}
4499 { NULL
, NULL
, 0, 0, { NULL
} }
4502 static void select_soundhw (const char *optarg
)
4506 if (*optarg
== '?') {
4509 printf ("Valid sound card names (comma separated):\n");
4510 for (c
= soundhw
; c
->name
; ++c
) {
4511 printf ("%-11s %s\n", c
->name
, c
->descr
);
4513 printf ("\n-soundhw all will enable all of the above\n");
4514 exit (*optarg
!= '?');
4522 if (!strcmp (optarg
, "all")) {
4523 for (c
= soundhw
; c
->name
; ++c
) {
4531 e
= strchr (p
, ',');
4532 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4534 for (c
= soundhw
; c
->name
; ++c
) {
4535 if (!strncmp (c
->name
, p
, l
)) {
4544 "Unknown sound card name (too big to show)\n");
4547 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4552 p
+= l
+ (e
!= NULL
);
4556 goto show_valid_cards
;
4561 static void select_vgahw (const char *p
)
4565 if (strstart(p
, "std", &opts
)) {
4566 cirrus_vga_enabled
= 0;
4568 } else if (strstart(p
, "cirrus", &opts
)) {
4569 cirrus_vga_enabled
= 1;
4571 } else if (strstart(p
, "vmware", &opts
)) {
4572 cirrus_vga_enabled
= 0;
4576 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4580 const char *nextopt
;
4582 if (strstart(opts
, ",retrace=", &nextopt
)) {
4584 if (strstart(opts
, "dumb", &nextopt
))
4585 vga_retrace_method
= VGA_RETRACE_DUMB
;
4586 else if (strstart(opts
, "precise", &nextopt
))
4587 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4588 else goto invalid_vga
;
4589 } else goto invalid_vga
;
4595 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4597 exit(STATUS_CONTROL_C_EXIT
);
4602 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4606 if(strlen(str
) != 36)
4609 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4610 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4611 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4619 #define MAX_NET_CLIENTS 32
4621 static int saved_argc
;
4622 static char **saved_argv
;
4624 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4628 *opt_daemonize
= daemonize
;
4629 *opt_incoming
= incoming
;
4633 static int gethugepagesize(void)
4637 const char *needle
= "Hugepagesize:";
4639 unsigned long hugepagesize
;
4641 fd
= open("/proc/meminfo", O_RDONLY
);
4647 ret
= read(fd
, buf
, sizeof(buf
));
4653 size
= strstr(buf
, needle
);
4656 size
+= strlen(needle
);
4657 hugepagesize
= strtol(size
, NULL
, 0);
4658 return hugepagesize
;
4661 void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4667 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4670 hpagesize
= gethugepagesize() * 1024;
4674 fd
= mkstemp(filename
);
4683 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4686 * ftruncate is not supported by hugetlbfs in older
4687 * hosts, so don't bother checking for errors.
4688 * If anything goes wrong with it under other filesystems,
4691 ftruncate(fd
, memory
);
4693 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4694 if (area
== MAP_FAILED
) {
4704 void *qemu_alloc_physram(unsigned long memory
)
4707 unsigned long map_len
= memory
;
4711 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4714 area
= qemu_vmalloc(memory
);
4716 if (kvm_setup_guest_memory(area
, map_len
))
4724 static void termsig_handler(int signal
)
4726 qemu_system_shutdown_request();
4729 static void termsig_setup(void)
4731 struct sigaction act
;
4733 memset(&act
, 0, sizeof(act
));
4734 act
.sa_handler
= termsig_handler
;
4735 sigaction(SIGINT
, &act
, NULL
);
4736 sigaction(SIGHUP
, &act
, NULL
);
4737 sigaction(SIGTERM
, &act
, NULL
);
4742 int main(int argc
, char **argv
, char **envp
)
4744 #ifdef CONFIG_GDBSTUB
4746 const char *gdbstub_port
;
4748 uint32_t boot_devices_bitmap
= 0;
4750 int snapshot
, linux_boot
, net_boot
;
4751 const char *initrd_filename
;
4752 const char *kernel_filename
, *kernel_cmdline
;
4753 const char *boot_devices
= "";
4754 DisplayState
*ds
= &display_state
;
4755 int cyls
, heads
, secs
, translation
;
4756 const char *net_clients
[MAX_NET_CLIENTS
];
4758 const char *bt_opts
[MAX_BT_CMDLINE
];
4762 const char *r
, *optarg
;
4763 CharDriverState
*monitor_hd
;
4764 const char *monitor_device
;
4765 const char *serial_devices
[MAX_SERIAL_PORTS
];
4766 int serial_device_index
;
4767 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4768 int parallel_device_index
;
4769 const char *loadvm
= NULL
;
4770 QEMUMachine
*machine
;
4771 const char *cpu_model
;
4772 const char *usb_devices
[MAX_USB_CMDLINE
];
4773 int usb_devices_index
;
4776 const char *pid_file
= NULL
;
4778 const char *incoming
= NULL
;
4780 qemu_cache_utils_init(envp
);
4782 LIST_INIT (&vm_change_state_head
);
4785 struct sigaction act
;
4786 sigfillset(&act
.sa_mask
);
4788 act
.sa_handler
= SIG_IGN
;
4789 sigaction(SIGPIPE
, &act
, NULL
);
4792 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4793 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4794 QEMU to run on a single CPU */
4799 h
= GetCurrentProcess();
4800 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4801 for(i
= 0; i
< 32; i
++) {
4802 if (mask
& (1 << i
))
4807 SetProcessAffinityMask(h
, mask
);
4813 register_machines();
4814 machine
= first_machine
;
4816 initrd_filename
= NULL
;
4818 vga_ram_size
= VGA_RAM_SIZE
;
4819 #ifdef CONFIG_GDBSTUB
4821 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4826 kernel_filename
= NULL
;
4827 kernel_cmdline
= "";
4828 cyls
= heads
= secs
= 0;
4829 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4830 monitor_device
= "vc";
4832 serial_devices
[0] = "vc:80Cx24C";
4833 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4834 serial_devices
[i
] = NULL
;
4835 serial_device_index
= 0;
4837 parallel_devices
[0] = "vc:640x480";
4838 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4839 parallel_devices
[i
] = NULL
;
4840 parallel_device_index
= 0;
4842 usb_devices_index
= 0;
4843 assigned_devices_index
= 0;
4862 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4864 const QEMUOption
*popt
;
4867 /* Treat --foo the same as -foo. */
4870 popt
= qemu_options
;
4873 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4877 if (!strcmp(popt
->name
, r
+ 1))
4881 if (popt
->flags
& HAS_ARG
) {
4882 if (optind
>= argc
) {
4883 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4887 optarg
= argv
[optind
++];
4892 switch(popt
->index
) {
4894 machine
= find_machine(optarg
);
4897 printf("Supported machines are:\n");
4898 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4899 printf("%-10s %s%s\n",
4901 m
== first_machine
? " (default)" : "");
4903 exit(*optarg
!= '?');
4906 case QEMU_OPTION_cpu
:
4907 /* hw initialization will check this */
4908 if (*optarg
== '?') {
4909 /* XXX: implement xxx_cpu_list for targets that still miss it */
4910 #if defined(cpu_list)
4911 cpu_list(stdout
, &fprintf
);
4918 case QEMU_OPTION_initrd
:
4919 initrd_filename
= optarg
;
4921 case QEMU_OPTION_hda
:
4923 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4925 hda_index
= drive_add(optarg
, HD_ALIAS
4926 ",cyls=%d,heads=%d,secs=%d%s",
4927 0, cyls
, heads
, secs
,
4928 translation
== BIOS_ATA_TRANSLATION_LBA
?
4930 translation
== BIOS_ATA_TRANSLATION_NONE
?
4931 ",trans=none" : "");
4933 case QEMU_OPTION_hdb
:
4934 case QEMU_OPTION_hdc
:
4935 case QEMU_OPTION_hdd
:
4936 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4938 case QEMU_OPTION_drive
:
4939 drive_add(NULL
, "%s", optarg
);
4941 case QEMU_OPTION_mtdblock
:
4942 drive_add(optarg
, MTD_ALIAS
);
4944 case QEMU_OPTION_sd
:
4945 drive_add(optarg
, SD_ALIAS
);
4947 case QEMU_OPTION_pflash
:
4948 drive_add(optarg
, PFLASH_ALIAS
);
4950 case QEMU_OPTION_snapshot
:
4953 case QEMU_OPTION_hdachs
:
4957 cyls
= strtol(p
, (char **)&p
, 0);
4958 if (cyls
< 1 || cyls
> 16383)
4963 heads
= strtol(p
, (char **)&p
, 0);
4964 if (heads
< 1 || heads
> 16)
4969 secs
= strtol(p
, (char **)&p
, 0);
4970 if (secs
< 1 || secs
> 63)
4974 if (!strcmp(p
, "none"))
4975 translation
= BIOS_ATA_TRANSLATION_NONE
;
4976 else if (!strcmp(p
, "lba"))
4977 translation
= BIOS_ATA_TRANSLATION_LBA
;
4978 else if (!strcmp(p
, "auto"))
4979 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4982 } else if (*p
!= '\0') {
4984 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4987 if (hda_index
!= -1)
4988 snprintf(drives_opt
[hda_index
].opt
,
4989 sizeof(drives_opt
[hda_index
].opt
),
4990 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4991 0, cyls
, heads
, secs
,
4992 translation
== BIOS_ATA_TRANSLATION_LBA
?
4994 translation
== BIOS_ATA_TRANSLATION_NONE
?
4995 ",trans=none" : "");
4998 case QEMU_OPTION_nographic
:
5001 #ifdef CONFIG_CURSES
5002 case QEMU_OPTION_curses
:
5006 case QEMU_OPTION_portrait
:
5009 case QEMU_OPTION_kernel
:
5010 kernel_filename
= optarg
;
5012 case QEMU_OPTION_append
:
5013 kernel_cmdline
= optarg
;
5015 case QEMU_OPTION_cdrom
:
5016 drive_add(optarg
, CDROM_ALIAS
);
5018 case QEMU_OPTION_boot
:
5019 boot_devices
= optarg
;
5020 /* We just do some generic consistency checks */
5022 /* Could easily be extended to 64 devices if needed */
5025 boot_devices_bitmap
= 0;
5026 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5027 /* Allowed boot devices are:
5028 * a b : floppy disk drives
5029 * c ... f : IDE disk drives
5030 * g ... m : machine implementation dependant drives
5031 * n ... p : network devices
5032 * It's up to each machine implementation to check
5033 * if the given boot devices match the actual hardware
5034 * implementation and firmware features.
5036 if (*p
< 'a' || *p
> 'q') {
5037 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5040 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5042 "Boot device '%c' was given twice\n",*p
);
5045 boot_devices_bitmap
|= 1 << (*p
- 'a');
5049 case QEMU_OPTION_fda
:
5050 case QEMU_OPTION_fdb
:
5051 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5054 case QEMU_OPTION_no_fd_bootchk
:
5058 case QEMU_OPTION_net
:
5059 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5060 fprintf(stderr
, "qemu: too many network clients\n");
5063 net_clients
[nb_net_clients
] = optarg
;
5067 case QEMU_OPTION_tftp
:
5068 tftp_prefix
= optarg
;
5070 case QEMU_OPTION_bootp
:
5071 bootp_filename
= optarg
;
5074 case QEMU_OPTION_smb
:
5075 net_slirp_smb(optarg
);
5078 case QEMU_OPTION_redir
:
5079 net_slirp_redir(optarg
);
5082 case QEMU_OPTION_bt
:
5083 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5084 fprintf(stderr
, "qemu: too many bluetooth options\n");
5087 bt_opts
[nb_bt_opts
++] = optarg
;
5090 case QEMU_OPTION_audio_help
:
5094 case QEMU_OPTION_soundhw
:
5095 select_soundhw (optarg
);
5101 case QEMU_OPTION_m
: {
5105 value
= strtoul(optarg
, &ptr
, 10);
5107 case 0: case 'M': case 'm':
5114 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5118 /* On 32-bit hosts, QEMU is limited by virtual address space */
5119 if (value
> (2047 << 20)
5121 && HOST_LONG_BITS
== 32
5124 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5127 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5128 fprintf(stderr
, "qemu: ram size too large\n");
5137 const CPULogItem
*item
;
5139 mask
= cpu_str_to_log_mask(optarg
);
5141 printf("Log items (comma separated):\n");
5142 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5143 printf("%-10s %s\n", item
->name
, item
->help
);
5150 #ifdef CONFIG_GDBSTUB
5155 gdbstub_port
= optarg
;
5161 case QEMU_OPTION_bios
:
5168 keyboard_layout
= optarg
;
5170 case QEMU_OPTION_localtime
:
5173 case QEMU_OPTION_vga
:
5174 select_vgahw (optarg
);
5181 w
= strtol(p
, (char **)&p
, 10);
5184 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5190 h
= strtol(p
, (char **)&p
, 10);
5195 depth
= strtol(p
, (char **)&p
, 10);
5196 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5197 depth
!= 24 && depth
!= 32)
5199 } else if (*p
== '\0') {
5200 depth
= graphic_depth
;
5207 graphic_depth
= depth
;
5210 case QEMU_OPTION_echr
:
5213 term_escape_char
= strtol(optarg
, &r
, 0);
5215 printf("Bad argument to echr\n");
5218 case QEMU_OPTION_monitor
:
5219 monitor_device
= optarg
;
5221 case QEMU_OPTION_serial
:
5222 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5223 fprintf(stderr
, "qemu: too many serial ports\n");
5226 serial_devices
[serial_device_index
] = optarg
;
5227 serial_device_index
++;
5229 case QEMU_OPTION_parallel
:
5230 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5231 fprintf(stderr
, "qemu: too many parallel ports\n");
5234 parallel_devices
[parallel_device_index
] = optarg
;
5235 parallel_device_index
++;
5237 case QEMU_OPTION_loadvm
:
5240 case QEMU_OPTION_full_screen
:
5244 case QEMU_OPTION_no_frame
:
5247 case QEMU_OPTION_alt_grab
:
5250 case QEMU_OPTION_no_quit
:
5254 case QEMU_OPTION_pidfile
:
5258 case QEMU_OPTION_win2k_hack
:
5259 win2k_install_hack
= 1;
5263 case QEMU_OPTION_no_kqemu
:
5266 case QEMU_OPTION_kernel_kqemu
:
5271 case QEMU_OPTION_enable_kvm
:
5279 case QEMU_OPTION_no_kvm
:
5282 case QEMU_OPTION_no_kvm_irqchip
: {
5283 extern int kvm_irqchip
, kvm_pit
;
5288 case QEMU_OPTION_no_kvm_pit
: {
5293 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5294 extern int kvm_pit_reinject
;
5295 kvm_pit_reinject
= 0;
5298 case QEMU_OPTION_enable_nesting
: {
5302 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5303 case QEMU_OPTION_pcidevice
:
5304 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5305 fprintf(stderr
, "Too many assigned devices\n");
5308 assigned_devices
[assigned_devices_index
] = optarg
;
5309 assigned_devices_index
++;
5313 case QEMU_OPTION_usb
:
5316 case QEMU_OPTION_usbdevice
:
5318 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5319 fprintf(stderr
, "Too many USB devices\n");
5322 usb_devices
[usb_devices_index
] = optarg
;
5323 usb_devices_index
++;
5325 case QEMU_OPTION_smp
:
5326 smp_cpus
= atoi(optarg
);
5328 fprintf(stderr
, "Invalid number of CPUs\n");
5332 case QEMU_OPTION_vnc
:
5333 vnc_display
= optarg
;
5335 case QEMU_OPTION_no_acpi
:
5338 case QEMU_OPTION_no_hpet
:
5341 case QEMU_OPTION_no_reboot
:
5344 case QEMU_OPTION_no_shutdown
:
5347 case QEMU_OPTION_show_cursor
:
5350 case QEMU_OPTION_uuid
:
5351 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5352 fprintf(stderr
, "Fail to parse UUID string."
5353 " Wrong format.\n");
5357 case QEMU_OPTION_daemonize
:
5360 case QEMU_OPTION_option_rom
:
5361 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5362 fprintf(stderr
, "Too many option ROMs\n");
5365 option_rom
[nb_option_roms
] = optarg
;
5368 case QEMU_OPTION_semihosting
:
5369 semihosting_enabled
= 1;
5371 case QEMU_OPTION_tdf
:
5374 case QEMU_OPTION_kvm_shadow_memory
:
5375 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5377 case QEMU_OPTION_mempath
:
5380 case QEMU_OPTION_name
:
5383 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5384 case QEMU_OPTION_prom_env
:
5385 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5386 fprintf(stderr
, "Too many prom variables\n");
5389 prom_envs
[nb_prom_envs
] = optarg
;
5393 case QEMU_OPTION_cpu_vendor
:
5394 cpu_vendor_string
= optarg
;
5397 case QEMU_OPTION_old_param
:
5401 case QEMU_OPTION_clock
:
5402 configure_alarms(optarg
);
5404 case QEMU_OPTION_startdate
:
5407 time_t rtc_start_date
;
5408 if (!strcmp(optarg
, "now")) {
5409 rtc_date_offset
= -1;
5411 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5419 } else if (sscanf(optarg
, "%d-%d-%d",
5422 &tm
.tm_mday
) == 3) {
5431 rtc_start_date
= mktimegm(&tm
);
5432 if (rtc_start_date
== -1) {
5434 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5435 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5438 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5442 case QEMU_OPTION_tb_size
:
5443 tb_size
= strtol(optarg
, NULL
, 0);
5447 case QEMU_OPTION_icount
:
5449 if (strcmp(optarg
, "auto") == 0) {
5450 icount_time_shift
= -1;
5452 icount_time_shift
= strtol(optarg
, NULL
, 0);
5455 case QEMU_OPTION_incoming
:
5462 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5463 if (kvm_allowed
&& kqemu_allowed
) {
5465 "You can not enable both KVM and kqemu at the same time\n");
5470 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5471 if (smp_cpus
> machine
->max_cpus
) {
5472 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5473 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5479 if (serial_device_index
== 0)
5480 serial_devices
[0] = "stdio";
5481 if (parallel_device_index
== 0)
5482 parallel_devices
[0] = "null";
5483 if (strncmp(monitor_device
, "vc", 2) == 0)
5484 monitor_device
= "stdio";
5491 if (pipe(fds
) == -1)
5502 len
= read(fds
[0], &status
, 1);
5503 if (len
== -1 && (errno
== EINTR
))
5508 else if (status
== 1) {
5509 fprintf(stderr
, "Could not acquire pidfile\n");
5526 signal(SIGTSTP
, SIG_IGN
);
5527 signal(SIGTTOU
, SIG_IGN
);
5528 signal(SIGTTIN
, SIG_IGN
);
5533 if (kvm_enabled()) {
5534 if (kvm_qemu_init() < 0) {
5535 extern int kvm_allowed
;
5536 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5537 #ifdef NO_CPU_EMULATION
5538 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5546 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5549 write(fds
[1], &status
, 1);
5551 fprintf(stderr
, "Could not acquire pid file\n");
5559 linux_boot
= (kernel_filename
!= NULL
);
5560 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5562 if (!linux_boot
&& net_boot
== 0 &&
5563 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5566 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5567 fprintf(stderr
, "-append only allowed with -kernel option\n");
5571 if (!linux_boot
&& initrd_filename
!= NULL
) {
5572 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5576 /* boot to floppy or the default cd if no hard disk defined yet */
5577 if (!boot_devices
[0]) {
5578 boot_devices
= "cad";
5580 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5583 if (init_timer_alarm() < 0) {
5584 fprintf(stderr
, "could not initialize alarm timer\n");
5587 if (use_icount
&& icount_time_shift
< 0) {
5589 /* 125MIPS seems a reasonable initial guess at the guest speed.
5590 It will be corrected fairly quickly anyway. */
5591 icount_time_shift
= 3;
5592 init_icount_adjust();
5599 /* init network clients */
5600 if (nb_net_clients
== 0) {
5601 /* if no clients, we use a default config */
5602 net_clients
[nb_net_clients
++] = "nic";
5604 net_clients
[nb_net_clients
++] = "user";
5608 for(i
= 0;i
< nb_net_clients
; i
++) {
5609 if (net_client_parse(net_clients
[i
]) < 0)
5615 /* XXX: this should be moved in the PC machine instantiation code */
5616 if (net_boot
!= 0) {
5618 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5619 const char *model
= nd_table
[i
].model
;
5621 if (net_boot
& (1 << i
)) {
5624 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5625 if (get_image_size(buf
) > 0) {
5626 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5627 fprintf(stderr
, "Too many option ROMs\n");
5630 option_rom
[nb_option_roms
] = strdup(buf
);
5637 fprintf(stderr
, "No valid PXE rom found for network device\n");
5643 /* init the bluetooth world */
5644 for (i
= 0; i
< nb_bt_opts
; i
++)
5645 if (bt_parse(bt_opts
[i
]))
5648 /* init the memory */
5649 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5651 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5653 if (ram_size
< phys_ram_size
) {
5654 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5655 machine
->name
, (unsigned long long) phys_ram_size
);
5659 phys_ram_size
= ram_size
;
5661 ram_size
= phys_ram_size
;
5664 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5666 phys_ram_size
+= ram_size
;
5669 /* Initialize kvm */
5670 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5671 #define KVM_EXTRA_PAGES 3
5673 #define KVM_EXTRA_PAGES 0
5675 if (kvm_enabled()) {
5676 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5677 if (kvm_qemu_create_context() < 0) {
5678 fprintf(stderr
, "Could not create KVM context\n");
5683 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5684 if (!phys_ram_base
) {
5685 fprintf(stderr
, "Could not allocate physical memory\n");
5689 /* init the dynamic translator */
5690 cpu_exec_init_all(tb_size
* 1024 * 1024);
5694 /* we always create the cdrom drive, even if no disk is there */
5696 if (nb_drives_opt
< MAX_DRIVES
)
5697 drive_add(NULL
, CDROM_ALIAS
);
5699 /* we always create at least one floppy */
5701 if (nb_drives_opt
< MAX_DRIVES
)
5702 drive_add(NULL
, FD_ALIAS
, 0);
5704 /* we always create one sd slot, even if no card is in it */
5706 if (nb_drives_opt
< MAX_DRIVES
)
5707 drive_add(NULL
, SD_ALIAS
);
5709 /* open the virtual block devices
5710 * note that migration with device
5711 * hot add/remove is broken.
5713 for(i
= 0; i
< nb_drives_opt
; i
++)
5714 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5717 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5718 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5721 memset(&display_state
, 0, sizeof(display_state
));
5724 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5727 /* nearly nothing to do */
5728 dumb_display_init(ds
);
5729 } else if (vnc_display
!= NULL
) {
5730 vnc_display_init(ds
);
5731 if (vnc_display_open(ds
, vnc_display
) < 0)
5734 #if defined(CONFIG_CURSES)
5736 curses_display_init(ds
, full_screen
);
5740 #if defined(CONFIG_SDL)
5741 sdl_display_init(ds
, full_screen
, no_frame
);
5742 #elif defined(CONFIG_COCOA)
5743 cocoa_display_init(ds
, full_screen
);
5745 dumb_display_init(ds
);
5750 /* must be after terminal init, SDL library changes signal handlers */
5754 /* Maintain compatibility with multiple stdio monitors */
5755 if (!strcmp(monitor_device
,"stdio")) {
5756 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5757 const char *devname
= serial_devices
[i
];
5758 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5759 monitor_device
= NULL
;
5761 } else if (devname
&& !strcmp(devname
,"stdio")) {
5762 monitor_device
= NULL
;
5763 serial_devices
[i
] = "mon:stdio";
5768 if (monitor_device
) {
5769 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5771 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5774 monitor_init(monitor_hd
, !nographic
);
5777 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5778 const char *devname
= serial_devices
[i
];
5779 if (devname
&& strcmp(devname
, "none")) {
5781 snprintf(label
, sizeof(label
), "serial%d", i
);
5782 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5783 if (!serial_hds
[i
]) {
5784 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5788 if (strstart(devname
, "vc", 0))
5789 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5793 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5794 const char *devname
= parallel_devices
[i
];
5795 if (devname
&& strcmp(devname
, "none")) {
5797 snprintf(label
, sizeof(label
), "parallel%d", i
);
5798 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5799 if (!parallel_hds
[i
]) {
5800 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5804 if (strstart(devname
, "vc", 0))
5805 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5813 if (kvm_enabled()) {
5816 ret
= kvm_init(smp_cpus
);
5818 fprintf(stderr
, "failed to initialize KVM\n");
5824 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5825 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5827 current_machine
= machine
;
5829 /* Set KVM's vcpu state to qemu's initial CPUState. */
5830 if (kvm_enabled()) {
5833 ret
= kvm_sync_vcpus();
5835 fprintf(stderr
, "failed to initialize vcpus\n");
5840 /* init USB devices */
5842 for(i
= 0; i
< usb_devices_index
; i
++) {
5843 if (usb_device_add(usb_devices
[i
]) < 0) {
5844 fprintf(stderr
, "Warning: could not add USB device %s\n",
5850 if (display_state
.dpy_refresh
) {
5851 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5852 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5855 #ifdef CONFIG_GDBSTUB
5857 /* XXX: use standard host:port notation and modify options
5859 if (gdbserver_start(gdbstub_port
) < 0) {
5860 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5871 autostart
= 0; /* fixme how to deal with -daemonize */
5872 qemu_start_incoming_migration(incoming
);
5876 /* XXX: simplify init */
5889 len
= write(fds
[1], &status
, 1);
5890 if (len
== -1 && (errno
== EINTR
))
5897 TFR(fd
= open("/dev/null", O_RDWR
));