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 const char *nvram
= NULL
;
253 struct drive_opt drives_opt
[MAX_DRIVES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int event_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
267 uint8_t qemu_uuid
[16];
269 /* KVM runs the main loop in a separate thread. If we update one of the lists
270 * that are polled before or after select(), we need to make sure to break out
271 * of the select() to ensure the new item is serviced.
273 static void main_loop_break(void)
276 qemu_kvm_notify_work();
279 /***********************************************************/
280 /* x86 ISA bus support */
282 target_phys_addr_t isa_mem_base
= 0;
285 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
286 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
288 static uint32_t ioport_read(int index
, uint32_t address
)
290 static IOPortReadFunc
*default_func
[3] = {
291 default_ioport_readb
,
292 default_ioport_readw
,
295 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
297 func
= default_func
[index
];
298 return func(ioport_opaque
[address
], address
);
301 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
303 static IOPortWriteFunc
*default_func
[3] = {
304 default_ioport_writeb
,
305 default_ioport_writew
,
306 default_ioport_writel
308 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
310 func
= default_func
[index
];
311 func(ioport_opaque
[address
], address
, data
);
314 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
316 #ifdef DEBUG_UNUSED_IOPORT
317 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
322 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
324 #ifdef DEBUG_UNUSED_IOPORT
325 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
329 /* default is to make two byte accesses */
330 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
333 data
= ioport_read(0, address
);
334 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
335 data
|= ioport_read(0, address
) << 8;
339 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
341 ioport_write(0, address
, data
& 0xff);
342 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
343 ioport_write(0, address
, (data
>> 8) & 0xff);
346 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
348 #ifdef DEBUG_UNUSED_IOPORT
349 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
354 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
356 #ifdef DEBUG_UNUSED_IOPORT
357 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
361 /* size is the word size in byte */
362 int register_ioport_read(int start
, int length
, int size
,
363 IOPortReadFunc
*func
, void *opaque
)
369 } else if (size
== 2) {
371 } else if (size
== 4) {
374 hw_error("register_ioport_read: invalid size");
377 for(i
= start
; i
< start
+ length
; i
+= size
) {
378 ioport_read_table
[bsize
][i
] = func
;
379 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
380 hw_error("register_ioport_read: invalid opaque");
381 ioport_opaque
[i
] = opaque
;
386 /* size is the word size in byte */
387 int register_ioport_write(int start
, int length
, int size
,
388 IOPortWriteFunc
*func
, void *opaque
)
394 } else if (size
== 2) {
396 } else if (size
== 4) {
399 hw_error("register_ioport_write: invalid size");
402 for(i
= start
; i
< start
+ length
; i
+= size
) {
403 ioport_write_table
[bsize
][i
] = func
;
404 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
405 hw_error("register_ioport_write: invalid opaque");
406 ioport_opaque
[i
] = opaque
;
411 void isa_unassign_ioport(int start
, int length
)
415 for(i
= start
; i
< start
+ length
; i
++) {
416 ioport_read_table
[0][i
] = default_ioport_readb
;
417 ioport_read_table
[1][i
] = default_ioport_readw
;
418 ioport_read_table
[2][i
] = default_ioport_readl
;
420 ioport_write_table
[0][i
] = default_ioport_writeb
;
421 ioport_write_table
[1][i
] = default_ioport_writew
;
422 ioport_write_table
[2][i
] = default_ioport_writel
;
424 ioport_opaque
[i
] = NULL
;
428 /***********************************************************/
430 void cpu_outb(CPUState
*env
, int addr
, int val
)
433 if (loglevel
& CPU_LOG_IOPORT
)
434 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
436 ioport_write(0, addr
, val
);
439 env
->last_io_time
= cpu_get_time_fast();
443 void cpu_outw(CPUState
*env
, int addr
, int val
)
446 if (loglevel
& CPU_LOG_IOPORT
)
447 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
449 ioport_write(1, addr
, val
);
452 env
->last_io_time
= cpu_get_time_fast();
456 void cpu_outl(CPUState
*env
, int addr
, int val
)
459 if (loglevel
& CPU_LOG_IOPORT
)
460 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
462 ioport_write(2, addr
, val
);
465 env
->last_io_time
= cpu_get_time_fast();
469 int cpu_inb(CPUState
*env
, int addr
)
472 val
= ioport_read(0, addr
);
474 if (loglevel
& CPU_LOG_IOPORT
)
475 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
479 env
->last_io_time
= cpu_get_time_fast();
484 int cpu_inw(CPUState
*env
, int addr
)
487 val
= ioport_read(1, addr
);
489 if (loglevel
& CPU_LOG_IOPORT
)
490 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
494 env
->last_io_time
= cpu_get_time_fast();
499 int cpu_inl(CPUState
*env
, int addr
)
502 val
= ioport_read(2, addr
);
504 if (loglevel
& CPU_LOG_IOPORT
)
505 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
509 env
->last_io_time
= cpu_get_time_fast();
514 /***********************************************************/
515 void hw_error(const char *fmt
, ...)
521 fprintf(stderr
, "qemu: hardware error: ");
522 vfprintf(stderr
, fmt
, ap
);
523 fprintf(stderr
, "\n");
524 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
525 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
527 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
529 cpu_dump_state(env
, stderr
, fprintf
, 0);
539 static QEMUBalloonEvent
*qemu_balloon_event
;
540 void *qemu_balloon_event_opaque
;
542 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
544 qemu_balloon_event
= func
;
545 qemu_balloon_event_opaque
= opaque
;
548 void qemu_balloon(ram_addr_t target
)
550 if (qemu_balloon_event
)
551 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
554 ram_addr_t
qemu_balloon_status(void)
556 if (qemu_balloon_event
)
557 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
561 /***********************************************************/
564 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
565 static void *qemu_put_kbd_event_opaque
;
566 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
567 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
569 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
571 qemu_put_kbd_event_opaque
= opaque
;
572 qemu_put_kbd_event
= func
;
575 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
576 void *opaque
, int absolute
,
579 QEMUPutMouseEntry
*s
, *cursor
;
581 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
585 s
->qemu_put_mouse_event
= func
;
586 s
->qemu_put_mouse_event_opaque
= opaque
;
587 s
->qemu_put_mouse_event_absolute
= absolute
;
588 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
591 if (!qemu_put_mouse_event_head
) {
592 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
596 cursor
= qemu_put_mouse_event_head
;
597 while (cursor
->next
!= NULL
)
598 cursor
= cursor
->next
;
601 qemu_put_mouse_event_current
= s
;
606 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
608 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
610 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
613 cursor
= qemu_put_mouse_event_head
;
614 while (cursor
!= NULL
&& cursor
!= entry
) {
616 cursor
= cursor
->next
;
619 if (cursor
== NULL
) // does not exist or list empty
621 else if (prev
== NULL
) { // entry is head
622 qemu_put_mouse_event_head
= cursor
->next
;
623 if (qemu_put_mouse_event_current
== entry
)
624 qemu_put_mouse_event_current
= cursor
->next
;
625 qemu_free(entry
->qemu_put_mouse_event_name
);
630 prev
->next
= entry
->next
;
632 if (qemu_put_mouse_event_current
== entry
)
633 qemu_put_mouse_event_current
= prev
;
635 qemu_free(entry
->qemu_put_mouse_event_name
);
639 void kbd_put_keycode(int keycode
)
641 if (qemu_put_kbd_event
) {
642 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
646 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
648 QEMUPutMouseEvent
*mouse_event
;
649 void *mouse_event_opaque
;
652 if (!qemu_put_mouse_event_current
) {
657 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
659 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
662 if (graphic_rotate
) {
663 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
666 width
= graphic_width
- 1;
667 mouse_event(mouse_event_opaque
,
668 width
- dy
, dx
, dz
, buttons_state
);
670 mouse_event(mouse_event_opaque
,
671 dx
, dy
, dz
, buttons_state
);
675 int kbd_mouse_is_absolute(void)
677 if (!qemu_put_mouse_event_current
)
680 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
683 void do_info_mice(void)
685 QEMUPutMouseEntry
*cursor
;
688 if (!qemu_put_mouse_event_head
) {
689 term_printf("No mouse devices connected\n");
693 term_printf("Mouse devices available:\n");
694 cursor
= qemu_put_mouse_event_head
;
695 while (cursor
!= NULL
) {
696 term_printf("%c Mouse #%d: %s\n",
697 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
698 index
, cursor
->qemu_put_mouse_event_name
);
700 cursor
= cursor
->next
;
704 void do_mouse_set(int index
)
706 QEMUPutMouseEntry
*cursor
;
709 if (!qemu_put_mouse_event_head
) {
710 term_printf("No mouse devices connected\n");
714 cursor
= qemu_put_mouse_event_head
;
715 while (cursor
!= NULL
&& index
!= i
) {
717 cursor
= cursor
->next
;
721 qemu_put_mouse_event_current
= cursor
;
723 term_printf("Mouse at given index not found\n");
726 /* compute with 96 bit intermediate result: (a*b)/c */
727 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
732 #ifdef WORDS_BIGENDIAN
742 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
743 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
746 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
750 /***********************************************************/
751 /* real time host monotonic timer */
753 #define QEMU_TIMER_BASE 1000000000LL
757 static int64_t clock_freq
;
759 static void init_get_clock(void)
763 ret
= QueryPerformanceFrequency(&freq
);
765 fprintf(stderr
, "Could not calibrate ticks\n");
768 clock_freq
= freq
.QuadPart
;
771 static int64_t get_clock(void)
774 QueryPerformanceCounter(&ti
);
775 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
780 static int use_rt_clock
;
782 static void init_get_clock(void)
785 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
788 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
795 static int64_t get_clock(void)
797 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
800 clock_gettime(CLOCK_MONOTONIC
, &ts
);
801 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
805 /* XXX: using gettimeofday leads to problems if the date
806 changes, so it should be avoided. */
808 gettimeofday(&tv
, NULL
);
809 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
814 /* Return the virtual CPU time, based on the instruction counter. */
815 static int64_t cpu_get_icount(void)
818 CPUState
*env
= cpu_single_env
;;
819 icount
= qemu_icount
;
822 fprintf(stderr
, "Bad clock read\n");
823 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
825 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
828 /***********************************************************/
829 /* guest cycle counter */
831 static int64_t cpu_ticks_prev
;
832 static int64_t cpu_ticks_offset
;
833 static int64_t cpu_clock_offset
;
834 static int cpu_ticks_enabled
;
836 /* return the host CPU cycle counter and handle stop/restart */
837 int64_t cpu_get_ticks(void)
840 return cpu_get_icount();
842 if (!cpu_ticks_enabled
) {
843 return cpu_ticks_offset
;
846 ticks
= cpu_get_real_ticks();
847 if (cpu_ticks_prev
> ticks
) {
848 /* Note: non increasing ticks may happen if the host uses
850 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
852 cpu_ticks_prev
= ticks
;
853 return ticks
+ cpu_ticks_offset
;
857 /* return the host CPU monotonic timer and handle stop/restart */
858 static int64_t cpu_get_clock(void)
861 if (!cpu_ticks_enabled
) {
862 return cpu_clock_offset
;
865 return ti
+ cpu_clock_offset
;
869 /* enable cpu_get_ticks() */
870 void cpu_enable_ticks(void)
872 if (!cpu_ticks_enabled
) {
873 cpu_ticks_offset
-= cpu_get_real_ticks();
874 cpu_clock_offset
-= get_clock();
875 cpu_ticks_enabled
= 1;
879 /* disable cpu_get_ticks() : the clock is stopped. You must not call
880 cpu_get_ticks() after that. */
881 void cpu_disable_ticks(void)
883 if (cpu_ticks_enabled
) {
884 cpu_ticks_offset
= cpu_get_ticks();
885 cpu_clock_offset
= cpu_get_clock();
886 cpu_ticks_enabled
= 0;
890 /***********************************************************/
893 #define QEMU_TIMER_REALTIME 0
894 #define QEMU_TIMER_VIRTUAL 1
898 /* XXX: add frequency */
906 struct QEMUTimer
*next
;
909 struct qemu_alarm_timer
{
913 int (*start
)(struct qemu_alarm_timer
*t
);
914 void (*stop
)(struct qemu_alarm_timer
*t
);
915 void (*rearm
)(struct qemu_alarm_timer
*t
);
919 #define ALARM_FLAG_DYNTICKS 0x1
920 #define ALARM_FLAG_EXPIRED 0x2
922 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
924 return t
->flags
& ALARM_FLAG_DYNTICKS
;
927 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
929 if (!alarm_has_dynticks(t
))
935 /* TODO: MIN_TIMER_REARM_US should be optimized */
936 #define MIN_TIMER_REARM_US 250
938 static struct qemu_alarm_timer
*alarm_timer
;
940 static int alarm_timer_rfd
, alarm_timer_wfd
;
945 struct qemu_alarm_win32
{
949 } alarm_win32_data
= {0, NULL
, -1};
951 static int win32_start_timer(struct qemu_alarm_timer
*t
);
952 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
953 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
957 static int unix_start_timer(struct qemu_alarm_timer
*t
);
958 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
962 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
963 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
964 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
966 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
967 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
969 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
970 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
972 #endif /* __linux__ */
976 /* Correlation between real and virtual time is always going to be
977 fairly approximate, so ignore small variation.
978 When the guest is idle real and virtual time will be aligned in
980 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
982 static void icount_adjust(void)
987 static int64_t last_delta
;
988 /* If the VM is not running, then do nothing. */
992 cur_time
= cpu_get_clock();
993 cur_icount
= qemu_get_clock(vm_clock
);
994 delta
= cur_icount
- cur_time
;
995 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
997 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
998 && icount_time_shift
> 0) {
999 /* The guest is getting too far ahead. Slow time down. */
1000 icount_time_shift
--;
1003 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1004 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1005 /* The guest is getting too far behind. Speed time up. */
1006 icount_time_shift
++;
1009 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1012 static void icount_adjust_rt(void * opaque
)
1014 qemu_mod_timer(icount_rt_timer
,
1015 qemu_get_clock(rt_clock
) + 1000);
1019 static void icount_adjust_vm(void * opaque
)
1021 qemu_mod_timer(icount_vm_timer
,
1022 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1026 static void init_icount_adjust(void)
1028 /* Have both realtime and virtual time triggers for speed adjustment.
1029 The realtime trigger catches emulated time passing too slowly,
1030 the virtual time trigger catches emulated time passing too fast.
1031 Realtime triggers occur even when idle, so use them less frequently
1032 than VM triggers. */
1033 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1034 qemu_mod_timer(icount_rt_timer
,
1035 qemu_get_clock(rt_clock
) + 1000);
1036 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1037 qemu_mod_timer(icount_vm_timer
,
1038 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1041 static struct qemu_alarm_timer alarm_timers
[] = {
1044 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1045 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1046 /* HPET - if available - is preferred */
1047 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1048 /* ...otherwise try RTC */
1049 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1051 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1053 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1054 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1055 {"win32", 0, win32_start_timer
,
1056 win32_stop_timer
, NULL
, &alarm_win32_data
},
1061 static void show_available_alarms(void)
1065 printf("Available alarm timers, in order of precedence:\n");
1066 for (i
= 0; alarm_timers
[i
].name
; i
++)
1067 printf("%s\n", alarm_timers
[i
].name
);
1070 static void configure_alarms(char const *opt
)
1074 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1077 struct qemu_alarm_timer tmp
;
1079 if (!strcmp(opt
, "?")) {
1080 show_available_alarms();
1086 /* Reorder the array */
1087 name
= strtok(arg
, ",");
1089 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1090 if (!strcmp(alarm_timers
[i
].name
, name
))
1095 fprintf(stderr
, "Unknown clock %s\n", name
);
1104 tmp
= alarm_timers
[i
];
1105 alarm_timers
[i
] = alarm_timers
[cur
];
1106 alarm_timers
[cur
] = tmp
;
1110 name
= strtok(NULL
, ",");
1116 /* Disable remaining timers */
1117 for (i
= cur
; i
< count
; i
++)
1118 alarm_timers
[i
].name
= NULL
;
1120 show_available_alarms();
1125 QEMUClock
*rt_clock
;
1126 QEMUClock
*vm_clock
;
1128 static QEMUTimer
*active_timers
[2];
1130 static QEMUClock
*qemu_new_clock(int type
)
1133 clock
= qemu_mallocz(sizeof(QEMUClock
));
1140 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1144 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1147 ts
->opaque
= opaque
;
1151 void qemu_free_timer(QEMUTimer
*ts
)
1156 /* stop a timer, but do not dealloc it */
1157 void qemu_del_timer(QEMUTimer
*ts
)
1161 /* NOTE: this code must be signal safe because
1162 qemu_timer_expired() can be called from a signal. */
1163 pt
= &active_timers
[ts
->clock
->type
];
1176 /* modify the current timer so that it will be fired when current_time
1177 >= expire_time. The corresponding callback will be called. */
1178 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1184 /* add the timer in the sorted list */
1185 /* NOTE: this code must be signal safe because
1186 qemu_timer_expired() can be called from a signal. */
1187 pt
= &active_timers
[ts
->clock
->type
];
1192 if (t
->expire_time
> expire_time
)
1196 ts
->expire_time
= expire_time
;
1200 /* Rearm if necessary */
1201 if (pt
== &active_timers
[ts
->clock
->type
]) {
1202 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1203 qemu_rearm_alarm_timer(alarm_timer
);
1205 /* Interrupt execution to force deadline recalculation. */
1206 if (use_icount
&& cpu_single_env
) {
1207 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1212 int qemu_timer_pending(QEMUTimer
*ts
)
1215 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1222 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1226 return (timer_head
->expire_time
<= current_time
);
1229 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1235 if (!ts
|| ts
->expire_time
> current_time
)
1237 /* remove timer from the list before calling the callback */
1238 *ptimer_head
= ts
->next
;
1241 /* run the callback (the timer list can be modified) */
1246 int64_t qemu_get_clock(QEMUClock
*clock
)
1248 switch(clock
->type
) {
1249 case QEMU_TIMER_REALTIME
:
1250 return get_clock() / 1000000;
1252 case QEMU_TIMER_VIRTUAL
:
1254 return cpu_get_icount();
1256 return cpu_get_clock();
1261 static void init_timers(void)
1264 ticks_per_sec
= QEMU_TIMER_BASE
;
1265 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1266 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1270 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1272 uint64_t expire_time
;
1274 if (qemu_timer_pending(ts
)) {
1275 expire_time
= ts
->expire_time
;
1279 qemu_put_be64(f
, expire_time
);
1282 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1284 uint64_t expire_time
;
1286 expire_time
= qemu_get_be64(f
);
1287 if (expire_time
!= -1) {
1288 qemu_mod_timer(ts
, expire_time
);
1294 static void timer_save(QEMUFile
*f
, void *opaque
)
1296 if (cpu_ticks_enabled
) {
1297 hw_error("cannot save state if virtual timers are running");
1299 qemu_put_be64(f
, cpu_ticks_offset
);
1300 qemu_put_be64(f
, ticks_per_sec
);
1301 qemu_put_be64(f
, cpu_clock_offset
);
1304 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1306 if (version_id
!= 1 && version_id
!= 2)
1308 if (cpu_ticks_enabled
) {
1311 cpu_ticks_offset
=qemu_get_be64(f
);
1312 ticks_per_sec
=qemu_get_be64(f
);
1313 if (version_id
== 2) {
1314 cpu_clock_offset
=qemu_get_be64(f
);
1320 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1321 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1323 static void host_alarm_handler(int host_signum
)
1327 #define DISP_FREQ 1000
1329 static int64_t delta_min
= INT64_MAX
;
1330 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1332 ti
= qemu_get_clock(vm_clock
);
1333 if (last_clock
!= 0) {
1334 delta
= ti
- last_clock
;
1335 if (delta
< delta_min
)
1337 if (delta
> delta_max
)
1340 if (++count
== DISP_FREQ
) {
1341 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1342 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1343 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1344 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1345 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1347 delta_min
= INT64_MAX
;
1356 alarm_has_dynticks(alarm_timer
) ||
1358 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1359 qemu_get_clock(vm_clock
))) ||
1360 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1361 qemu_get_clock(rt_clock
))) {
1362 CPUState
*env
= next_cpu
;
1365 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1366 SetEvent(data
->host_alarm
);
1368 static const char byte
= 0;
1369 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1371 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1374 /* stop the currently executing cpu because a timer occured */
1375 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1377 if (env
->kqemu_enabled
) {
1378 kqemu_cpu_interrupt(env
);
1386 static int64_t qemu_next_deadline(void)
1390 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1391 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1392 qemu_get_clock(vm_clock
);
1394 /* To avoid problems with overflow limit this to 2^32. */
1404 #if defined(__linux__) || defined(_WIN32)
1405 static uint64_t qemu_next_deadline_dyntick(void)
1413 delta
= (qemu_next_deadline() + 999) / 1000;
1415 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1416 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1417 qemu_get_clock(rt_clock
))*1000;
1418 if (rtdelta
< delta
)
1422 if (delta
< MIN_TIMER_REARM_US
)
1423 delta
= MIN_TIMER_REARM_US
;
1431 /* Sets a specific flag */
1432 static int fcntl_setfl(int fd
, int flag
)
1436 flags
= fcntl(fd
, F_GETFL
);
1440 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1446 #if defined(__linux__)
1448 #define RTC_FREQ 1024
1450 static void enable_sigio_timer(int fd
)
1452 struct sigaction act
;
1455 sigfillset(&act
.sa_mask
);
1457 act
.sa_handler
= host_alarm_handler
;
1459 sigaction(SIGIO
, &act
, NULL
);
1460 fcntl_setfl(fd
, O_ASYNC
);
1461 fcntl(fd
, F_SETOWN
, getpid());
1464 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1466 struct hpet_info info
;
1469 fd
= open("/dev/hpet", O_RDONLY
);
1474 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1476 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1477 "error, but for better emulation accuracy type:\n"
1478 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1482 /* Check capabilities */
1483 r
= ioctl(fd
, HPET_INFO
, &info
);
1487 /* Enable periodic mode */
1488 r
= ioctl(fd
, HPET_EPI
, 0);
1489 if (info
.hi_flags
&& (r
< 0))
1492 /* Enable interrupt */
1493 r
= ioctl(fd
, HPET_IE_ON
, 0);
1497 enable_sigio_timer(fd
);
1498 t
->priv
= (void *)(long)fd
;
1506 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1508 int fd
= (long)t
->priv
;
1513 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1516 unsigned long current_rtc_freq
= 0;
1518 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1521 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1522 if (current_rtc_freq
!= RTC_FREQ
&&
1523 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1524 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1525 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1526 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1529 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1535 enable_sigio_timer(rtc_fd
);
1537 t
->priv
= (void *)(long)rtc_fd
;
1542 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1544 int rtc_fd
= (long)t
->priv
;
1549 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1553 struct sigaction act
;
1555 sigfillset(&act
.sa_mask
);
1557 act
.sa_handler
= host_alarm_handler
;
1559 sigaction(SIGALRM
, &act
, NULL
);
1561 ev
.sigev_value
.sival_int
= 0;
1562 ev
.sigev_notify
= SIGEV_SIGNAL
;
1563 ev
.sigev_signo
= SIGALRM
;
1565 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1566 perror("timer_create");
1568 /* disable dynticks */
1569 fprintf(stderr
, "Dynamic Ticks disabled\n");
1574 t
->priv
= (void *)(long)host_timer
;
1579 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1581 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1583 timer_delete(host_timer
);
1586 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1588 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1589 struct itimerspec timeout
;
1590 int64_t nearest_delta_us
= INT64_MAX
;
1593 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1594 !active_timers
[QEMU_TIMER_VIRTUAL
])
1597 nearest_delta_us
= qemu_next_deadline_dyntick();
1599 /* check whether a timer is already running */
1600 if (timer_gettime(host_timer
, &timeout
)) {
1602 fprintf(stderr
, "Internal timer error: aborting\n");
1605 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1606 if (current_us
&& current_us
<= nearest_delta_us
)
1609 timeout
.it_interval
.tv_sec
= 0;
1610 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1611 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1612 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1613 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1615 fprintf(stderr
, "Internal timer error: aborting\n");
1620 #endif /* defined(__linux__) */
1622 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1624 struct sigaction act
;
1625 struct itimerval itv
;
1629 sigfillset(&act
.sa_mask
);
1631 act
.sa_handler
= host_alarm_handler
;
1633 sigaction(SIGALRM
, &act
, NULL
);
1635 itv
.it_interval
.tv_sec
= 0;
1636 /* for i386 kernel 2.6 to get 1 ms */
1637 itv
.it_interval
.tv_usec
= 999;
1638 itv
.it_value
.tv_sec
= 0;
1639 itv
.it_value
.tv_usec
= 10 * 1000;
1641 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1648 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1650 struct itimerval itv
;
1652 memset(&itv
, 0, sizeof(itv
));
1653 setitimer(ITIMER_REAL
, &itv
, NULL
);
1656 #endif /* !defined(_WIN32) */
1658 static void try_to_rearm_timer(void *opaque
)
1660 struct qemu_alarm_timer
*t
= opaque
;
1664 /* Drain the notify pipe */
1667 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1668 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1671 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1672 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1673 qemu_rearm_alarm_timer(alarm_timer
);
1679 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1682 struct qemu_alarm_win32
*data
= t
->priv
;
1685 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1686 if (!data
->host_alarm
) {
1687 perror("Failed CreateEvent");
1691 memset(&tc
, 0, sizeof(tc
));
1692 timeGetDevCaps(&tc
, sizeof(tc
));
1694 if (data
->period
< tc
.wPeriodMin
)
1695 data
->period
= tc
.wPeriodMin
;
1697 timeBeginPeriod(data
->period
);
1699 flags
= TIME_CALLBACK_FUNCTION
;
1700 if (alarm_has_dynticks(t
))
1701 flags
|= TIME_ONESHOT
;
1703 flags
|= TIME_PERIODIC
;
1705 data
->timerId
= timeSetEvent(1, // interval (ms)
1706 data
->period
, // resolution
1707 host_alarm_handler
, // function
1708 (DWORD
)t
, // parameter
1711 if (!data
->timerId
) {
1712 perror("Failed to initialize win32 alarm timer");
1714 timeEndPeriod(data
->period
);
1715 CloseHandle(data
->host_alarm
);
1719 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1724 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1726 struct qemu_alarm_win32
*data
= t
->priv
;
1728 timeKillEvent(data
->timerId
);
1729 timeEndPeriod(data
->period
);
1731 CloseHandle(data
->host_alarm
);
1734 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1736 struct qemu_alarm_win32
*data
= t
->priv
;
1737 uint64_t nearest_delta_us
;
1739 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1740 !active_timers
[QEMU_TIMER_VIRTUAL
])
1743 nearest_delta_us
= qemu_next_deadline_dyntick();
1744 nearest_delta_us
/= 1000;
1746 timeKillEvent(data
->timerId
);
1748 data
->timerId
= timeSetEvent(1,
1752 TIME_ONESHOT
| TIME_PERIODIC
);
1754 if (!data
->timerId
) {
1755 perror("Failed to re-arm win32 alarm timer");
1757 timeEndPeriod(data
->period
);
1758 CloseHandle(data
->host_alarm
);
1765 static int init_timer_alarm(void)
1767 struct qemu_alarm_timer
*t
= NULL
;
1777 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1781 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1785 alarm_timer_rfd
= fds
[0];
1786 alarm_timer_wfd
= fds
[1];
1789 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1790 t
= &alarm_timers
[i
];
1803 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1804 try_to_rearm_timer
, NULL
, t
);
1819 static void quit_timers(void)
1821 alarm_timer
->stop(alarm_timer
);
1825 /***********************************************************/
1826 /* host time/date access */
1827 void qemu_get_timedate(struct tm
*tm
, int offset
)
1834 if (rtc_date_offset
== -1) {
1838 ret
= localtime(&ti
);
1840 ti
-= rtc_date_offset
;
1844 memcpy(tm
, ret
, sizeof(struct tm
));
1847 int qemu_timedate_diff(struct tm
*tm
)
1851 if (rtc_date_offset
== -1)
1853 seconds
= mktimegm(tm
);
1855 seconds
= mktime(tm
);
1857 seconds
= mktimegm(tm
) + rtc_date_offset
;
1859 return seconds
- time(NULL
);
1863 static void socket_cleanup(void)
1868 static int socket_init(void)
1873 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1875 err
= WSAGetLastError();
1876 fprintf(stderr
, "WSAStartup: %d\n", err
);
1879 atexit(socket_cleanup
);
1884 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1889 while (*p
!= '\0' && *p
!= '=') {
1890 if (q
&& (q
- buf
) < buf_size
- 1)
1900 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1905 while (*p
!= '\0') {
1907 if (*(p
+ 1) != ',')
1911 if (q
&& (q
- buf
) < buf_size
- 1)
1921 int get_param_value(char *buf
, int buf_size
,
1922 const char *tag
, const char *str
)
1929 p
= get_opt_name(option
, sizeof(option
), p
);
1933 if (!strcmp(tag
, option
)) {
1934 (void)get_opt_value(buf
, buf_size
, p
);
1937 p
= get_opt_value(NULL
, 0, p
);
1946 int check_params(char *buf
, int buf_size
,
1947 const char * const *params
, const char *str
)
1954 p
= get_opt_name(buf
, buf_size
, p
);
1958 for(i
= 0; params
[i
] != NULL
; i
++)
1959 if (!strcmp(params
[i
], buf
))
1961 if (params
[i
] == NULL
)
1963 p
= get_opt_value(NULL
, 0, p
);
1971 /***********************************************************/
1972 /* Bluetooth support */
1975 static struct HCIInfo
*hci_table
[MAX_NICS
];
1977 static struct bt_vlan_s
{
1978 struct bt_scatternet_s net
;
1980 struct bt_vlan_s
*next
;
1983 /* find or alloc a new bluetooth "VLAN" */
1984 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1986 struct bt_vlan_s
**pvlan
, *vlan
;
1987 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1991 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1993 pvlan
= &first_bt_vlan
;
1994 while (*pvlan
!= NULL
)
1995 pvlan
= &(*pvlan
)->next
;
2000 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2004 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2009 static struct HCIInfo null_hci
= {
2010 .cmd_send
= null_hci_send
,
2011 .sco_send
= null_hci_send
,
2012 .acl_send
= null_hci_send
,
2013 .bdaddr_set
= null_hci_addr_set
,
2016 struct HCIInfo
*qemu_next_hci(void)
2018 if (cur_hci
== nb_hcis
)
2021 return hci_table
[cur_hci
++];
2024 static struct HCIInfo
*hci_init(const char *str
)
2027 struct bt_scatternet_s
*vlan
= 0;
2029 if (!strcmp(str
, "null"))
2032 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2034 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2035 else if (!strncmp(str
, "hci", 3)) {
2038 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2039 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2044 vlan
= qemu_find_bt_vlan(0);
2046 return bt_new_hci(vlan
);
2049 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2054 static int bt_hci_parse(const char *str
)
2056 struct HCIInfo
*hci
;
2059 if (nb_hcis
>= MAX_NICS
) {
2060 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2064 hci
= hci_init(str
);
2073 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2074 hci
->bdaddr_set(hci
, bdaddr
.b
);
2076 hci_table
[nb_hcis
++] = hci
;
2081 static void bt_vhci_add(int vlan_id
)
2083 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2086 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2087 "an empty scatternet %i\n", vlan_id
);
2089 bt_vhci_init(bt_new_hci(vlan
));
2092 static struct bt_device_s
*bt_device_add(const char *opt
)
2094 struct bt_scatternet_s
*vlan
;
2096 char *endp
= strstr(opt
, ",vlan=");
2097 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2100 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2103 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2105 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2110 vlan
= qemu_find_bt_vlan(vlan_id
);
2113 fprintf(stderr
, "qemu: warning: adding a slave device to "
2114 "an empty scatternet %i\n", vlan_id
);
2116 if (!strcmp(devname
, "keyboard"))
2117 return bt_keyboard_init(vlan
);
2119 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2123 static int bt_parse(const char *opt
)
2125 const char *endp
, *p
;
2128 if (strstart(opt
, "hci", &endp
)) {
2129 if (!*endp
|| *endp
== ',') {
2131 if (!strstart(endp
, ",vlan=", 0))
2134 return bt_hci_parse(opt
);
2136 } else if (strstart(opt
, "vhci", &endp
)) {
2137 if (!*endp
|| *endp
== ',') {
2139 if (strstart(endp
, ",vlan=", &p
)) {
2140 vlan
= strtol(p
, (char **) &endp
, 0);
2142 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2146 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2155 } else if (strstart(opt
, "device:", &endp
))
2156 return !bt_device_add(endp
);
2158 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2162 /***********************************************************/
2163 /* QEMU Block devices */
2165 #define HD_ALIAS "index=%d,media=disk"
2167 #define CDROM_ALIAS "index=1,media=cdrom"
2169 #define CDROM_ALIAS "index=2,media=cdrom"
2171 #define FD_ALIAS "index=%d,if=floppy"
2172 #define PFLASH_ALIAS "if=pflash"
2173 #define MTD_ALIAS "if=mtd"
2174 #define SD_ALIAS "index=0,if=sd"
2176 static int drive_opt_get_free_idx(void)
2180 for (index
= 0; index
< MAX_DRIVES
; index
++)
2181 if (!drives_opt
[index
].used
) {
2182 drives_opt
[index
].used
= 1;
2189 static int drive_get_free_idx(void)
2193 for (index
= 0; index
< MAX_DRIVES
; index
++)
2194 if (!drives_table
[index
].used
) {
2195 drives_table
[index
].used
= 1;
2202 int drive_add(const char *file
, const char *fmt
, ...)
2205 int index
= drive_opt_get_free_idx();
2207 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2208 fprintf(stderr
, "qemu: too many drives\n");
2212 drives_opt
[index
].file
= file
;
2214 vsnprintf(drives_opt
[index
].opt
,
2215 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2222 void drive_remove(int index
)
2224 drives_opt
[index
].used
= 0;
2228 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2232 /* seek interface, bus and unit */
2234 for (index
= 0; index
< MAX_DRIVES
; index
++)
2235 if (drives_table
[index
].type
== type
&&
2236 drives_table
[index
].bus
== bus
&&
2237 drives_table
[index
].unit
== unit
&&
2238 drives_table
[index
].used
)
2244 int drive_get_max_bus(BlockInterfaceType type
)
2250 for (index
= 0; index
< nb_drives
; index
++) {
2251 if(drives_table
[index
].type
== type
&&
2252 drives_table
[index
].bus
> max_bus
)
2253 max_bus
= drives_table
[index
].bus
;
2258 const char *drive_get_serial(BlockDriverState
*bdrv
)
2262 for (index
= 0; index
< nb_drives
; index
++)
2263 if (drives_table
[index
].bdrv
== bdrv
)
2264 return drives_table
[index
].serial
;
2269 static void bdrv_format_print(void *opaque
, const char *name
)
2271 fprintf(stderr
, " %s", name
);
2274 void drive_uninit(BlockDriverState
*bdrv
)
2278 for (i
= 0; i
< MAX_DRIVES
; i
++)
2279 if (drives_table
[i
].bdrv
== bdrv
) {
2280 drives_table
[i
].bdrv
= NULL
;
2281 drives_table
[i
].used
= 0;
2282 drive_remove(drives_table
[i
].drive_opt_idx
);
2288 int drive_init(struct drive_opt
*arg
, int snapshot
,
2289 QEMUMachine
*machine
)
2295 const char *mediastr
= "";
2296 BlockInterfaceType type
;
2297 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2298 int bus_id
, unit_id
;
2299 int cyls
, heads
, secs
, translation
;
2300 BlockDriverState
*bdrv
;
2301 BlockDriver
*drv
= NULL
;
2306 int drives_table_idx
;
2307 char *str
= arg
->opt
;
2308 static const char * const params
[] = { "bus", "unit", "if", "index",
2309 "cyls", "heads", "secs", "trans",
2310 "media", "snapshot", "file",
2311 "cache", "format", "serial",
2314 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2315 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2321 cyls
= heads
= secs
= 0;
2324 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2328 if (machine
->use_scsi
) {
2330 max_devs
= MAX_SCSI_DEVS
;
2331 pstrcpy(devname
, sizeof(devname
), "scsi");
2334 max_devs
= MAX_IDE_DEVS
;
2335 pstrcpy(devname
, sizeof(devname
), "ide");
2339 /* extract parameters */
2341 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2342 bus_id
= strtol(buf
, NULL
, 0);
2344 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2349 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2350 unit_id
= strtol(buf
, NULL
, 0);
2352 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2357 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2358 pstrcpy(devname
, sizeof(devname
), buf
);
2359 if (!strcmp(buf
, "ide")) {
2361 max_devs
= MAX_IDE_DEVS
;
2362 } else if (!strcmp(buf
, "scsi")) {
2364 max_devs
= MAX_SCSI_DEVS
;
2365 } else if (!strcmp(buf
, "floppy")) {
2368 } else if (!strcmp(buf
, "pflash")) {
2371 } else if (!strcmp(buf
, "mtd")) {
2374 } else if (!strcmp(buf
, "sd")) {
2377 } else if (!strcmp(buf
, "virtio")) {
2381 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2386 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2387 index
= strtol(buf
, NULL
, 0);
2389 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2394 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2395 cyls
= strtol(buf
, NULL
, 0);
2398 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2399 heads
= strtol(buf
, NULL
, 0);
2402 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2403 secs
= strtol(buf
, NULL
, 0);
2406 if (cyls
|| heads
|| secs
) {
2407 if (cyls
< 1 || cyls
> 16383) {
2408 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2411 if (heads
< 1 || heads
> 16) {
2412 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2415 if (secs
< 1 || secs
> 63) {
2416 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2421 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2424 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2428 if (!strcmp(buf
, "none"))
2429 translation
= BIOS_ATA_TRANSLATION_NONE
;
2430 else if (!strcmp(buf
, "lba"))
2431 translation
= BIOS_ATA_TRANSLATION_LBA
;
2432 else if (!strcmp(buf
, "auto"))
2433 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2435 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2440 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2441 if (!strcmp(buf
, "disk")) {
2443 } else if (!strcmp(buf
, "cdrom")) {
2444 if (cyls
|| secs
|| heads
) {
2446 "qemu: '%s' invalid physical CHS format\n", str
);
2449 media
= MEDIA_CDROM
;
2451 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2456 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2457 if (!strcmp(buf
, "on"))
2459 else if (!strcmp(buf
, "off"))
2462 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2467 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2468 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2470 else if (!strcmp(buf
, "writethrough"))
2472 else if (!strcmp(buf
, "writeback"))
2475 fprintf(stderr
, "qemu: invalid cache option\n");
2480 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2481 if (strcmp(buf
, "?") == 0) {
2482 fprintf(stderr
, "qemu: Supported formats:");
2483 bdrv_iterate_format(bdrv_format_print
, NULL
);
2484 fprintf(stderr
, "\n");
2487 drv
= bdrv_find_format(buf
);
2489 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2494 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2495 if (!strcmp(buf
, "on")) {
2496 if (extboot_drive
!= -1) {
2497 fprintf(stderr
, "qemu: two bootable drives specified\n");
2500 extboot_drive
= nb_drives
;
2501 } else if (strcmp(buf
, "off")) {
2502 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2507 if (arg
->file
== NULL
)
2508 get_param_value(file
, sizeof(file
), "file", str
);
2510 pstrcpy(file
, sizeof(file
), arg
->file
);
2512 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2513 memset(serial
, 0, sizeof(serial
));
2515 /* compute bus and unit according index */
2518 if (bus_id
!= 0 || unit_id
!= -1) {
2520 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2528 unit_id
= index
% max_devs
;
2529 bus_id
= index
/ max_devs
;
2533 /* if user doesn't specify a unit_id,
2534 * try to find the first free
2537 if (unit_id
== -1) {
2539 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2541 if (max_devs
&& unit_id
>= max_devs
) {
2542 unit_id
-= max_devs
;
2550 if (max_devs
&& unit_id
>= max_devs
) {
2551 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2552 str
, unit_id
, max_devs
- 1);
2557 * ignore multiple definitions
2560 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2565 if (type
== IF_IDE
|| type
== IF_SCSI
)
2566 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2568 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2569 devname
, bus_id
, mediastr
, unit_id
);
2571 snprintf(buf
, sizeof(buf
), "%s%s%i",
2572 devname
, mediastr
, unit_id
);
2573 bdrv
= bdrv_new(buf
);
2574 drives_table_idx
= drive_get_free_idx();
2575 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2576 drives_table
[drives_table_idx
].type
= type
;
2577 drives_table
[drives_table_idx
].bus
= bus_id
;
2578 drives_table
[drives_table_idx
].unit
= unit_id
;
2579 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2580 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2589 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2590 bdrv_set_translation_hint(bdrv
, translation
);
2594 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2599 /* FIXME: This isn't really a floppy, but it's a reasonable
2602 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2613 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2614 cache
= 2; /* always use write-back with snapshot */
2616 if (cache
== 0) /* no caching */
2617 bdrv_flags
|= BDRV_O_NOCACHE
;
2618 else if (cache
== 2) /* write-back */
2619 bdrv_flags
|= BDRV_O_CACHE_WB
;
2620 else if (cache
== 3) /* not specified */
2621 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2622 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2623 fprintf(stderr
, "qemu: could not open disk image %s\n",
2627 return drives_table_idx
;
2630 /***********************************************************/
2633 static USBPort
*used_usb_ports
;
2634 static USBPort
*free_usb_ports
;
2636 /* ??? Maybe change this to register a hub to keep track of the topology. */
2637 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2638 usb_attachfn attach
)
2640 port
->opaque
= opaque
;
2641 port
->index
= index
;
2642 port
->attach
= attach
;
2643 port
->next
= free_usb_ports
;
2644 free_usb_ports
= port
;
2647 int usb_device_add_dev(USBDevice
*dev
)
2651 /* Find a USB port to add the device to. */
2652 port
= free_usb_ports
;
2656 /* Create a new hub and chain it on. */
2657 free_usb_ports
= NULL
;
2658 port
->next
= used_usb_ports
;
2659 used_usb_ports
= port
;
2661 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2662 usb_attach(port
, hub
);
2663 port
= free_usb_ports
;
2666 free_usb_ports
= port
->next
;
2667 port
->next
= used_usb_ports
;
2668 used_usb_ports
= port
;
2669 usb_attach(port
, dev
);
2673 static int usb_device_add(const char *devname
)
2678 if (!free_usb_ports
)
2681 if (strstart(devname
, "host:", &p
)) {
2682 dev
= usb_host_device_open(p
);
2683 } else if (!strcmp(devname
, "mouse")) {
2684 dev
= usb_mouse_init();
2685 } else if (!strcmp(devname
, "tablet")) {
2686 dev
= usb_tablet_init();
2687 } else if (!strcmp(devname
, "keyboard")) {
2688 dev
= usb_keyboard_init();
2689 } else if (strstart(devname
, "disk:", &p
)) {
2690 dev
= usb_msd_init(p
);
2691 } else if (!strcmp(devname
, "wacom-tablet")) {
2692 dev
= usb_wacom_init();
2693 } else if (strstart(devname
, "serial:", &p
)) {
2694 dev
= usb_serial_init(p
);
2695 #ifdef CONFIG_BRLAPI
2696 } else if (!strcmp(devname
, "braille")) {
2697 dev
= usb_baum_init();
2699 } else if (strstart(devname
, "net:", &p
)) {
2702 if (net_client_init("nic", p
) < 0)
2704 nd_table
[nic
].model
= "usb";
2705 dev
= usb_net_init(&nd_table
[nic
]);
2706 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2707 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2708 bt_new_hci(qemu_find_bt_vlan(0)));
2715 return usb_device_add_dev(dev
);
2718 int usb_device_del_addr(int bus_num
, int addr
)
2724 if (!used_usb_ports
)
2730 lastp
= &used_usb_ports
;
2731 port
= used_usb_ports
;
2732 while (port
&& port
->dev
->addr
!= addr
) {
2733 lastp
= &port
->next
;
2741 *lastp
= port
->next
;
2742 usb_attach(port
, NULL
);
2743 dev
->handle_destroy(dev
);
2744 port
->next
= free_usb_ports
;
2745 free_usb_ports
= port
;
2749 static int usb_device_del(const char *devname
)
2754 if (strstart(devname
, "host:", &p
))
2755 return usb_host_device_close(p
);
2757 if (!used_usb_ports
)
2760 p
= strchr(devname
, '.');
2763 bus_num
= strtoul(devname
, NULL
, 0);
2764 addr
= strtoul(p
+ 1, NULL
, 0);
2766 return usb_device_del_addr(bus_num
, addr
);
2769 void do_usb_add(const char *devname
)
2771 usb_device_add(devname
);
2774 void do_usb_del(const char *devname
)
2776 usb_device_del(devname
);
2783 const char *speed_str
;
2786 term_printf("USB support not enabled\n");
2790 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2794 switch(dev
->speed
) {
2798 case USB_SPEED_FULL
:
2801 case USB_SPEED_HIGH
:
2808 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2809 0, dev
->addr
, speed_str
, dev
->devname
);
2813 /***********************************************************/
2814 /* PCMCIA/Cardbus */
2816 static struct pcmcia_socket_entry_s
{
2817 struct pcmcia_socket_s
*socket
;
2818 struct pcmcia_socket_entry_s
*next
;
2819 } *pcmcia_sockets
= 0;
2821 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2823 struct pcmcia_socket_entry_s
*entry
;
2825 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2826 entry
->socket
= socket
;
2827 entry
->next
= pcmcia_sockets
;
2828 pcmcia_sockets
= entry
;
2831 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2833 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2835 ptr
= &pcmcia_sockets
;
2836 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2837 if (entry
->socket
== socket
) {
2843 void pcmcia_info(void)
2845 struct pcmcia_socket_entry_s
*iter
;
2846 if (!pcmcia_sockets
)
2847 term_printf("No PCMCIA sockets\n");
2849 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2850 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2851 iter
->socket
->attached
? iter
->socket
->card_string
:
2855 /***********************************************************/
2858 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2862 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2866 static void dumb_display_init(DisplayState
*ds
)
2871 ds
->dpy_update
= dumb_update
;
2872 ds
->dpy_resize
= dumb_resize
;
2873 ds
->dpy_refresh
= NULL
;
2874 ds
->gui_timer_interval
= 0;
2878 /***********************************************************/
2881 #define MAX_IO_HANDLERS 64
2883 typedef struct IOHandlerRecord
{
2885 IOCanRWHandler
*fd_read_poll
;
2887 IOHandler
*fd_write
;
2890 /* temporary data */
2892 struct IOHandlerRecord
*next
;
2895 static IOHandlerRecord
*first_io_handler
;
2897 /* XXX: fd_read_poll should be suppressed, but an API change is
2898 necessary in the character devices to suppress fd_can_read(). */
2899 int qemu_set_fd_handler2(int fd
,
2900 IOCanRWHandler
*fd_read_poll
,
2902 IOHandler
*fd_write
,
2905 IOHandlerRecord
**pioh
, *ioh
;
2907 if (!fd_read
&& !fd_write
) {
2908 pioh
= &first_io_handler
;
2913 if (ioh
->fd
== fd
) {
2920 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2924 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2927 ioh
->next
= first_io_handler
;
2928 first_io_handler
= ioh
;
2931 ioh
->fd_read_poll
= fd_read_poll
;
2932 ioh
->fd_read
= fd_read
;
2933 ioh
->fd_write
= fd_write
;
2934 ioh
->opaque
= opaque
;
2941 int qemu_set_fd_handler(int fd
,
2943 IOHandler
*fd_write
,
2946 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2950 /***********************************************************/
2951 /* Polling handling */
2953 typedef struct PollingEntry
{
2956 struct PollingEntry
*next
;
2959 static PollingEntry
*first_polling_entry
;
2961 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2963 PollingEntry
**ppe
, *pe
;
2964 pe
= qemu_mallocz(sizeof(PollingEntry
));
2968 pe
->opaque
= opaque
;
2969 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2974 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2976 PollingEntry
**ppe
, *pe
;
2977 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2979 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2987 /***********************************************************/
2988 /* Wait objects support */
2989 typedef struct WaitObjects
{
2991 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2992 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2993 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2996 static WaitObjects wait_objects
= {0};
2998 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3000 WaitObjects
*w
= &wait_objects
;
3002 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3004 w
->events
[w
->num
] = handle
;
3005 w
->func
[w
->num
] = func
;
3006 w
->opaque
[w
->num
] = opaque
;
3011 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3014 WaitObjects
*w
= &wait_objects
;
3017 for (i
= 0; i
< w
->num
; i
++) {
3018 if (w
->events
[i
] == handle
)
3021 w
->events
[i
] = w
->events
[i
+ 1];
3022 w
->func
[i
] = w
->func
[i
+ 1];
3023 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3031 /***********************************************************/
3032 /* ram save/restore */
3034 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3038 v
= qemu_get_byte(f
);
3041 if (qemu_get_buffer(f
, buf
, len
) != len
)
3045 v
= qemu_get_byte(f
);
3046 memset(buf
, v
, len
);
3052 if (qemu_file_has_error(f
))
3058 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3063 if (qemu_get_be32(f
) != phys_ram_size
)
3065 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3066 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3068 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3075 #define BDRV_HASH_BLOCK_SIZE 1024
3076 #define IOBUF_SIZE 4096
3077 #define RAM_CBLOCK_MAGIC 0xfabe
3079 typedef struct RamDecompressState
{
3082 uint8_t buf
[IOBUF_SIZE
];
3083 } RamDecompressState
;
3085 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3088 memset(s
, 0, sizeof(*s
));
3090 ret
= inflateInit(&s
->zstream
);
3096 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3100 s
->zstream
.avail_out
= len
;
3101 s
->zstream
.next_out
= buf
;
3102 while (s
->zstream
.avail_out
> 0) {
3103 if (s
->zstream
.avail_in
== 0) {
3104 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3106 clen
= qemu_get_be16(s
->f
);
3107 if (clen
> IOBUF_SIZE
)
3109 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3110 s
->zstream
.avail_in
= clen
;
3111 s
->zstream
.next_in
= s
->buf
;
3113 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3114 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3121 static void ram_decompress_close(RamDecompressState
*s
)
3123 inflateEnd(&s
->zstream
);
3126 #define RAM_SAVE_FLAG_FULL 0x01
3127 #define RAM_SAVE_FLAG_COMPRESS 0x02
3128 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3129 #define RAM_SAVE_FLAG_PAGE 0x08
3130 #define RAM_SAVE_FLAG_EOS 0x10
3132 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3134 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3135 uint32_t *array
= (uint32_t *)page
;
3138 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3139 if (array
[i
] != val
)
3146 static int ram_save_block(QEMUFile
*f
)
3148 static ram_addr_t current_addr
= 0;
3149 ram_addr_t saved_addr
= current_addr
;
3150 ram_addr_t addr
= 0;
3153 while (addr
< phys_ram_size
) {
3154 if (kvm_enabled() && current_addr
== 0)
3155 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3156 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3159 cpu_physical_memory_reset_dirty(current_addr
,
3160 current_addr
+ TARGET_PAGE_SIZE
,
3161 MIGRATION_DIRTY_FLAG
);
3163 ch
= *(phys_ram_base
+ current_addr
);
3165 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3166 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3167 qemu_put_byte(f
, ch
);
3169 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3170 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3176 addr
+= TARGET_PAGE_SIZE
;
3177 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3183 static ram_addr_t ram_save_threshold
= 10;
3185 static ram_addr_t
ram_save_remaining(void)
3188 ram_addr_t count
= 0;
3190 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3191 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3198 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3203 /* Make sure all dirty bits are set */
3204 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3205 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3206 cpu_physical_memory_set_dirty(addr
);
3209 /* Enable dirty memory tracking */
3210 cpu_physical_memory_set_dirty_tracking(1);
3212 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3215 while (!qemu_file_rate_limit(f
)) {
3218 ret
= ram_save_block(f
);
3219 if (ret
== 0) /* no more blocks */
3223 /* try transferring iterative blocks of memory */
3226 cpu_physical_memory_set_dirty_tracking(0);
3228 /* flush all remaining blocks regardless of rate limiting */
3229 while (ram_save_block(f
) != 0);
3232 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3234 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3237 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3239 RamDecompressState s1
, *s
= &s1
;
3243 if (ram_decompress_open(s
, f
) < 0)
3245 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3246 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3248 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3249 fprintf(stderr
, "Error while reading ram block header\n");
3253 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3254 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3259 printf("Error block header\n");
3263 ram_decompress_close(s
);
3268 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3273 if (version_id
== 1)
3274 return ram_load_v1(f
, opaque
);
3276 if (version_id
== 2) {
3277 if (qemu_get_be32(f
) != phys_ram_size
)
3279 return ram_load_dead(f
, opaque
);
3282 if (version_id
!= 3)
3286 addr
= qemu_get_be64(f
);
3288 flags
= addr
& ~TARGET_PAGE_MASK
;
3289 addr
&= TARGET_PAGE_MASK
;
3291 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3292 if (addr
!= phys_ram_size
)
3296 if (flags
& RAM_SAVE_FLAG_FULL
) {
3297 if (ram_load_dead(f
, opaque
) < 0)
3301 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3302 uint8_t ch
= qemu_get_byte(f
);
3303 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3304 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3305 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3306 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3311 /***********************************************************/
3312 /* bottom halves (can be seen as timers which expire ASAP) */
3323 static QEMUBH
*first_bh
= NULL
;
3325 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3328 bh
= qemu_mallocz(sizeof(QEMUBH
));
3332 bh
->opaque
= opaque
;
3333 bh
->next
= first_bh
;
3338 int qemu_bh_poll(void)
3344 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3345 if (!bh
->deleted
&& bh
->scheduled
) {
3354 /* remove deleted bhs */
3368 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3376 void qemu_bh_schedule(QEMUBH
*bh
)
3378 CPUState
*env
= cpu_single_env
;
3383 /* stop the currently executing CPU to execute the BH ASAP */
3385 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3390 void qemu_bh_cancel(QEMUBH
*bh
)
3395 void qemu_bh_delete(QEMUBH
*bh
)
3401 static void qemu_bh_update_timeout(int *timeout
)
3405 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3406 if (!bh
->deleted
&& bh
->scheduled
) {
3408 /* idle bottom halves will be polled at least
3410 *timeout
= MIN(10, *timeout
);
3412 /* non-idle bottom halves will be executed
3421 /***********************************************************/
3422 /* machine registration */
3424 static QEMUMachine
*first_machine
= NULL
;
3425 QEMUMachine
*current_machine
= NULL
;
3427 int qemu_register_machine(QEMUMachine
*m
)
3430 pm
= &first_machine
;
3438 static QEMUMachine
*find_machine(const char *name
)
3442 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3443 if (!strcmp(m
->name
, name
))
3449 /***********************************************************/
3450 /* main execution loop */
3452 static void gui_update(void *opaque
)
3454 DisplayState
*ds
= opaque
;
3455 ds
->dpy_refresh(ds
);
3456 qemu_mod_timer(ds
->gui_timer
,
3457 (ds
->gui_timer_interval
?
3458 ds
->gui_timer_interval
:
3459 GUI_REFRESH_INTERVAL
)
3460 + qemu_get_clock(rt_clock
));
3463 struct vm_change_state_entry
{
3464 VMChangeStateHandler
*cb
;
3466 LIST_ENTRY (vm_change_state_entry
) entries
;
3469 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3471 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3474 VMChangeStateEntry
*e
;
3476 e
= qemu_mallocz(sizeof (*e
));
3482 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3486 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3488 LIST_REMOVE (e
, entries
);
3492 static void vm_state_notify(int running
)
3494 VMChangeStateEntry
*e
;
3496 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3497 e
->cb(e
->opaque
, running
);
3501 /* XXX: support several handlers */
3502 static VMStopHandler
*vm_stop_cb
;
3503 static void *vm_stop_opaque
;
3505 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3508 vm_stop_opaque
= opaque
;
3512 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3523 qemu_rearm_alarm_timer(alarm_timer
);
3527 void vm_stop(int reason
)
3530 cpu_disable_ticks();
3534 vm_stop_cb(vm_stop_opaque
, reason
);
3541 /* reset/shutdown handler */
3543 typedef struct QEMUResetEntry
{
3544 QEMUResetHandler
*func
;
3546 struct QEMUResetEntry
*next
;
3549 static QEMUResetEntry
*first_reset_entry
;
3550 static int reset_requested
;
3551 static int shutdown_requested
;
3552 static int powerdown_requested
;
3554 int qemu_shutdown_requested(void)
3556 int r
= shutdown_requested
;
3557 shutdown_requested
= 0;
3561 int qemu_reset_requested(void)
3563 int r
= reset_requested
;
3564 reset_requested
= 0;
3568 int qemu_powerdown_requested(void)
3570 int r
= powerdown_requested
;
3571 powerdown_requested
= 0;
3575 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3577 QEMUResetEntry
**pre
, *re
;
3579 pre
= &first_reset_entry
;
3580 while (*pre
!= NULL
)
3581 pre
= &(*pre
)->next
;
3582 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3584 re
->opaque
= opaque
;
3589 void qemu_system_reset(void)
3593 /* reset all devices */
3594 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3595 re
->func(re
->opaque
);
3599 void qemu_system_reset_request(void)
3602 shutdown_requested
= 1;
3604 reset_requested
= 1;
3607 if (cpu_single_env
) {
3608 qemu_kvm_cpu_stop(cpu_single_env
);
3609 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3614 void qemu_system_shutdown_request(void)
3616 shutdown_requested
= 1;
3618 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3621 void qemu_system_powerdown_request(void)
3623 powerdown_requested
= 1;
3625 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3628 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3633 /* KVM holds a mutex while QEMU code is running, we need hooks to
3634 release the mutex whenever QEMU code sleeps. */
3638 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3646 static void host_main_loop_wait(int *timeout
)
3652 /* XXX: need to suppress polling by better using win32 events */
3654 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3655 ret
|= pe
->func(pe
->opaque
);
3659 WaitObjects
*w
= &wait_objects
;
3661 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3662 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3663 if (w
->func
[ret
- WAIT_OBJECT_0
])
3664 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3666 /* Check for additional signaled events */
3667 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3669 /* Check if event is signaled */
3670 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3671 if(ret2
== WAIT_OBJECT_0
) {
3673 w
->func
[i
](w
->opaque
[i
]);
3674 } else if (ret2
== WAIT_TIMEOUT
) {
3676 err
= GetLastError();
3677 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3680 } else if (ret
== WAIT_TIMEOUT
) {
3682 err
= GetLastError();
3683 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3690 static void host_main_loop_wait(int *timeout
)
3695 void main_loop_wait(int timeout
)
3697 IOHandlerRecord
*ioh
;
3698 fd_set rfds
, wfds
, xfds
;
3702 qemu_bh_update_timeout(&timeout
);
3704 host_main_loop_wait(&timeout
);
3706 /* poll any events */
3707 /* XXX: separate device handlers from system ones */
3712 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3716 (!ioh
->fd_read_poll
||
3717 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3718 FD_SET(ioh
->fd
, &rfds
);
3722 if (ioh
->fd_write
) {
3723 FD_SET(ioh
->fd
, &wfds
);
3729 tv
.tv_sec
= timeout
/ 1000;
3730 tv
.tv_usec
= (timeout
% 1000) * 1000;
3732 #if defined(CONFIG_SLIRP)
3733 if (slirp_is_inited()) {
3734 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3737 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3739 IOHandlerRecord
**pioh
;
3741 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3742 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3743 ioh
->fd_read(ioh
->opaque
);
3744 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3745 FD_CLR(ioh
->fd
, &rfds
);
3747 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3748 ioh
->fd_write(ioh
->opaque
);
3752 /* remove deleted IO handlers */
3753 pioh
= &first_io_handler
;
3763 #if defined(CONFIG_SLIRP)
3764 if (slirp_is_inited()) {
3770 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3774 /* vm time timers */
3775 if (vm_running
&& (!cur_cpu
3776 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3777 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3778 qemu_get_clock(vm_clock
));
3780 /* real time timers */
3781 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3782 qemu_get_clock(rt_clock
));
3784 /* Check bottom-halves last in case any of the earlier events triggered
3790 static int main_loop(void)
3793 #ifdef CONFIG_PROFILER
3799 if (kvm_enabled()) {
3801 cpu_disable_ticks();
3805 cur_cpu
= first_cpu
;
3806 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3813 #ifdef CONFIG_PROFILER
3814 ti
= profile_getclock();
3819 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3820 env
->icount_decr
.u16
.low
= 0;
3821 env
->icount_extra
= 0;
3822 count
= qemu_next_deadline();
3823 count
= (count
+ (1 << icount_time_shift
) - 1)
3824 >> icount_time_shift
;
3825 qemu_icount
+= count
;
3826 decr
= (count
> 0xffff) ? 0xffff : count
;
3828 env
->icount_decr
.u16
.low
= decr
;
3829 env
->icount_extra
= count
;
3831 ret
= cpu_exec(env
);
3832 #ifdef CONFIG_PROFILER
3833 qemu_time
+= profile_getclock() - ti
;
3836 /* Fold pending instructions back into the
3837 instruction counter, and clear the interrupt flag. */
3838 qemu_icount
-= (env
->icount_decr
.u16
.low
3839 + env
->icount_extra
);
3840 env
->icount_decr
.u32
= 0;
3841 env
->icount_extra
= 0;
3843 next_cpu
= env
->next_cpu
?: first_cpu
;
3844 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3845 ret
= EXCP_INTERRUPT
;
3849 if (ret
== EXCP_HLT
) {
3850 /* Give the next CPU a chance to run. */
3854 if (ret
!= EXCP_HALTED
)
3856 /* all CPUs are halted ? */
3862 if (shutdown_requested
) {
3863 ret
= EXCP_INTERRUPT
;
3871 if (reset_requested
) {
3872 reset_requested
= 0;
3873 qemu_system_reset();
3875 kvm_load_registers(env
);
3876 ret
= EXCP_INTERRUPT
;
3878 if (powerdown_requested
) {
3879 powerdown_requested
= 0;
3880 qemu_system_powerdown();
3881 ret
= EXCP_INTERRUPT
;
3883 #ifdef CONFIG_GDBSTUB
3884 if (unlikely(ret
== EXCP_DEBUG
)) {
3885 gdb_set_stop_cpu(cur_cpu
);
3886 vm_stop(EXCP_DEBUG
);
3889 /* If all cpus are halted then wait until the next IRQ */
3890 /* XXX: use timeout computed from timers */
3891 if (ret
== EXCP_HALTED
) {
3895 /* Advance virtual time to the next event. */
3896 if (use_icount
== 1) {
3897 /* When not using an adaptive execution frequency
3898 we tend to get badly out of sync with real time,
3899 so just delay for a reasonable amount of time. */
3902 delta
= cpu_get_icount() - cpu_get_clock();
3905 /* If virtual time is ahead of real time then just
3907 timeout
= (delta
/ 1000000) + 1;
3909 /* Wait for either IO to occur or the next
3911 add
= qemu_next_deadline();
3912 /* We advance the timer before checking for IO.
3913 Limit the amount we advance so that early IO
3914 activity won't get the guest too far ahead. */
3918 add
= (add
+ (1 << icount_time_shift
) - 1)
3919 >> icount_time_shift
;
3921 timeout
= delta
/ 1000000;
3932 if (shutdown_requested
) {
3933 ret
= EXCP_INTERRUPT
;
3938 #ifdef CONFIG_PROFILER
3939 ti
= profile_getclock();
3941 main_loop_wait(timeout
);
3942 #ifdef CONFIG_PROFILER
3943 dev_time
+= profile_getclock() - ti
;
3946 cpu_disable_ticks();
3950 static void help(int exitcode
)
3952 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3953 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3954 "usage: %s [options] [disk_image]\n"
3956 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3958 "Standard options:\n"
3959 "-M machine select emulated machine (-M ? for list)\n"
3960 "-cpu cpu select CPU (-cpu ? for list)\n"
3961 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3962 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3963 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3964 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3965 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3966 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3967 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3969 " use 'file' as a drive image\n"
3970 "-mtdblock file use 'file' as on-board Flash memory image\n"
3971 "-sd file use 'file' as SecureDigital card image\n"
3972 "-pflash file use 'file' as a parallel flash image\n"
3973 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3974 "-snapshot write to temporary files instead of disk image files\n"
3976 "-no-frame open SDL window without a frame and window decorations\n"
3977 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3978 "-no-quit disable SDL window close capability\n"
3981 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3983 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3984 "-smp n set the number of CPUs to 'n' [default=1]\n"
3985 "-nographic disable graphical output and redirect serial I/Os to console\n"
3986 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3988 "-k language use keyboard layout (for example \"fr\" for French)\n"
3991 "-audio-help print list of audio drivers and their options\n"
3992 "-soundhw c1,... enable audio support\n"
3993 " and only specified sound cards (comma separated list)\n"
3994 " use -soundhw ? to get the list of supported cards\n"
3995 " use -soundhw all to enable all of them\n"
3997 "-vga [std|cirrus|vmware]\n"
3998 " select video card type\n"
3999 "-localtime set the real time clock to local time [default=utc]\n"
4000 "-full-screen start in full screen\n"
4002 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4004 "-usb enable the USB driver (will be the default soon)\n"
4005 "-usbdevice name add the host or guest USB device 'name'\n"
4006 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4007 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4010 "-nvram file use 'file' to save or load nvram image\n"
4012 "-name string set the name of the guest\n"
4013 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4015 "Network options:\n"
4016 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4017 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4019 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4020 " connect the user mode network stack to VLAN 'n' and send\n"
4021 " hostname 'host' to DHCP clients\n"
4024 "-net tap[,vlan=n][,name=str],ifname=name\n"
4025 " connect the host TAP network interface to VLAN 'n'\n"
4027 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4028 " connect the host TAP network interface to VLAN 'n' and use the\n"
4029 " network scripts 'file' (default=%s)\n"
4030 " and 'dfile' (default=%s);\n"
4031 " use '[down]script=no' to disable script execution;\n"
4032 " use 'fd=h' to connect to an already opened TAP interface\n"
4034 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4035 " connect the vlan 'n' to another VLAN using a socket connection\n"
4036 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4037 " connect the vlan 'n' to multicast maddr and port\n"
4039 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4040 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4041 " on host and listening for incoming connections on 'socketpath'.\n"
4042 " Use group 'groupname' and mode 'octalmode' to change default\n"
4043 " ownership and permissions for communication port.\n"
4045 "-net none use it alone to have zero network devices; if no -net option\n"
4046 " is provided, the default is '-net nic -net user'\n"
4048 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
4049 "-bt hci,host[:id]\n"
4050 " Use host's HCI with the given name\n"
4051 "-bt hci[,vlan=n]\n"
4052 " Emulate a standard HCI in virtual scatternet 'n'\n"
4053 "-bt vhci[,vlan=n]\n"
4054 " Add host computer to virtual scatternet 'n' using VHCI\n"
4055 "-bt device:dev[,vlan=n]\n"
4056 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
4059 "-tftp dir allow tftp access to files in dir [-net user]\n"
4060 "-bootp file advertise file in BOOTP replies\n"
4062 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4064 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4065 " redirect TCP or UDP connections from host to guest [-net user]\n"
4068 "Linux boot specific:\n"
4069 "-kernel bzImage use 'bzImage' as kernel image\n"
4070 "-append cmdline use 'cmdline' as kernel command line\n"
4071 "-initrd file use 'file' as initial ram disk\n"
4073 "Debug/Expert options:\n"
4074 "-monitor dev redirect the monitor to char device 'dev'\n"
4075 "-serial dev redirect the serial port to char device 'dev'\n"
4076 "-parallel dev redirect the parallel port to char device 'dev'\n"
4077 "-pidfile file Write PID to 'file'\n"
4078 "-S freeze CPU at startup (use 'c' to start execution)\n"
4079 "-s wait gdb connection to port\n"
4080 "-p port set gdb connection port [default=%s]\n"
4081 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4082 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4083 " translation (t=none or lba) (usually qemu can guess them)\n"
4084 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4086 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4087 "-no-kqemu disable KQEMU kernel module usage\n"
4090 "-enable-kvm enable KVM full virtualization support\n"
4093 #ifndef NO_CPU_EMULATION
4094 "-no-kvm disable KVM hardware virtualization\n"
4096 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4097 "-no-kvm-pit disable KVM kernel mode PIT\n"
4098 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4099 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4100 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4101 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4102 " expose a PCI device to the guest OS.\n"
4103 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4104 " 'string' is used in log output.\n"
4108 "-no-acpi disable ACPI\n"
4109 "-no-hpet disable HPET\n"
4111 #ifdef CONFIG_CURSES
4112 "-curses use a curses/ncurses interface instead of SDL\n"
4114 "-no-reboot exit instead of rebooting\n"
4115 "-no-shutdown stop before shutdown\n"
4116 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4117 "-vnc display start a VNC server on display\n"
4119 "-daemonize daemonize QEMU after initializing\n"
4121 "-tdf inject timer interrupts that got lost\n"
4122 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4123 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also enables allocation of guest memory with huge pages\n"
4124 "-option-rom rom load a file, rom, into the option ROM space\n"
4126 "-prom-env variable=value set OpenBIOS nvram variables\n"
4128 "-clock force the use of the given methods for timer alarm.\n"
4129 " To see what timers are available use -clock ?\n"
4130 "-startdate select initial date of the clock\n"
4131 "-icount [N|auto]\n"
4132 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4134 "During emulation, the following keys are useful:\n"
4135 "ctrl-alt-f toggle full screen\n"
4136 "ctrl-alt-n switch to virtual console 'n'\n"
4137 "ctrl-alt toggle mouse and keyboard grab\n"
4139 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4144 DEFAULT_NETWORK_SCRIPT
,
4145 DEFAULT_NETWORK_DOWN_SCRIPT
,
4147 DEFAULT_GDBSTUB_PORT
,
4152 #define HAS_ARG 0x0001
4167 QEMU_OPTION_mtdblock
,
4171 QEMU_OPTION_snapshot
,
4173 QEMU_OPTION_no_fd_bootchk
,
4176 QEMU_OPTION_nographic
,
4177 QEMU_OPTION_portrait
,
4179 QEMU_OPTION_audio_help
,
4180 QEMU_OPTION_soundhw
,
4202 QEMU_OPTION_localtime
,
4206 QEMU_OPTION_monitor
,
4208 QEMU_OPTION_parallel
,
4210 QEMU_OPTION_full_screen
,
4211 QEMU_OPTION_no_frame
,
4212 QEMU_OPTION_alt_grab
,
4213 QEMU_OPTION_no_quit
,
4214 QEMU_OPTION_pidfile
,
4215 QEMU_OPTION_no_kqemu
,
4216 QEMU_OPTION_kernel_kqemu
,
4217 QEMU_OPTION_enable_kvm
,
4218 QEMU_OPTION_enable_nesting
,
4219 QEMU_OPTION_win2k_hack
,
4221 QEMU_OPTION_usbdevice
,
4224 QEMU_OPTION_no_acpi
,
4225 QEMU_OPTION_no_hpet
,
4228 QEMU_OPTION_no_kvm_irqchip
,
4229 QEMU_OPTION_no_kvm_pit
,
4230 QEMU_OPTION_no_kvm_pit_reinjection
,
4231 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4232 QEMU_OPTION_pcidevice
,
4234 QEMU_OPTION_no_reboot
,
4235 QEMU_OPTION_no_shutdown
,
4236 QEMU_OPTION_show_cursor
,
4237 QEMU_OPTION_daemonize
,
4238 QEMU_OPTION_option_rom
,
4239 QEMU_OPTION_semihosting
,
4240 QEMU_OPTION_cpu_vendor
,
4243 QEMU_OPTION_prom_env
,
4244 QEMU_OPTION_old_param
,
4246 QEMU_OPTION_startdate
,
4247 QEMU_OPTION_tb_size
,
4250 QEMU_OPTION_incoming
,
4252 QEMU_OPTION_kvm_shadow_memory
,
4253 QEMU_OPTION_mempath
,
4256 typedef struct QEMUOption
{
4262 static const QEMUOption qemu_options
[] = {
4263 { "h", 0, QEMU_OPTION_h
},
4264 { "help", 0, QEMU_OPTION_h
},
4266 { "M", HAS_ARG
, QEMU_OPTION_M
},
4267 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4268 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4269 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4270 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4271 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4272 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4273 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4274 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4275 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4276 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4277 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4278 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4279 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4280 { "snapshot", 0, QEMU_OPTION_snapshot
},
4282 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4284 { "m", HAS_ARG
, QEMU_OPTION_m
},
4285 { "nographic", 0, QEMU_OPTION_nographic
},
4286 { "portrait", 0, QEMU_OPTION_portrait
},
4287 { "k", HAS_ARG
, QEMU_OPTION_k
},
4289 { "audio-help", 0, QEMU_OPTION_audio_help
},
4290 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4293 { "net", HAS_ARG
, QEMU_OPTION_net
},
4295 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4296 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4298 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4300 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4302 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4304 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4305 { "append", HAS_ARG
, QEMU_OPTION_append
},
4306 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4308 { "S", 0, QEMU_OPTION_S
},
4309 { "s", 0, QEMU_OPTION_s
},
4310 { "p", HAS_ARG
, QEMU_OPTION_p
},
4311 { "d", HAS_ARG
, QEMU_OPTION_d
},
4312 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4313 { "L", HAS_ARG
, QEMU_OPTION_L
},
4314 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4316 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4317 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4320 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4323 #ifndef NO_CPU_EMULATION
4324 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4326 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4327 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4328 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4329 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4330 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4331 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4334 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4335 { "g", 1, QEMU_OPTION_g
},
4337 { "localtime", 0, QEMU_OPTION_localtime
},
4338 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4339 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4340 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4341 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4342 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4343 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4344 { "incoming", 1, QEMU_OPTION_incoming
},
4345 { "full-screen", 0, QEMU_OPTION_full_screen
},
4347 { "no-frame", 0, QEMU_OPTION_no_frame
},
4348 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4349 { "no-quit", 0, QEMU_OPTION_no_quit
},
4351 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4352 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4353 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4354 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4355 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4356 #ifdef CONFIG_CURSES
4357 { "curses", 0, QEMU_OPTION_curses
},
4359 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4361 /* temporary options */
4362 { "usb", 0, QEMU_OPTION_usb
},
4363 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4364 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4365 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4366 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4367 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4368 { "daemonize", 0, QEMU_OPTION_daemonize
},
4369 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4370 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4371 { "semihosting", 0, QEMU_OPTION_semihosting
},
4373 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4374 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4375 { "name", HAS_ARG
, QEMU_OPTION_name
},
4376 { "nvram", HAS_ARG
, QEMU_OPTION_nvram
},
4377 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4378 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4380 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4381 #if defined(TARGET_ARM)
4382 { "old-param", 0, QEMU_OPTION_old_param
},
4384 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4385 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4386 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4387 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4388 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4389 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4393 /* password input */
4395 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4400 if (!bdrv_is_encrypted(bs
))
4403 term_printf("%s is encrypted.\n", name
);
4404 for(i
= 0; i
< 3; i
++) {
4405 monitor_readline("Password: ", 1, password
, sizeof(password
));
4406 if (bdrv_set_key(bs
, password
) == 0)
4408 term_printf("invalid password\n");
4413 static BlockDriverState
*get_bdrv(int index
)
4415 if (index
> nb_drives
)
4417 return drives_table
[index
].bdrv
;
4420 static void read_passwords(void)
4422 BlockDriverState
*bs
;
4425 for(i
= 0; i
< 6; i
++) {
4428 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4433 struct soundhw soundhw
[] = {
4434 #ifdef HAS_AUDIO_CHOICE
4435 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4441 { .init_isa
= pcspk_audio_init
}
4448 "Creative Sound Blaster 16",
4451 { .init_isa
= SB16_init
}
4455 #ifdef CONFIG_CS4231A
4461 { .init_isa
= cs4231a_init
}
4469 "Yamaha YMF262 (OPL3)",
4471 "Yamaha YM3812 (OPL2)",
4475 { .init_isa
= Adlib_init
}
4482 "Gravis Ultrasound GF1",
4485 { .init_isa
= GUS_init
}
4492 "Intel 82801AA AC97 Audio",
4495 { .init_pci
= ac97_init
}
4499 #ifdef CONFIG_ES1370
4502 "ENSONIQ AudioPCI ES1370",
4505 { .init_pci
= es1370_init
}
4509 #endif /* HAS_AUDIO_CHOICE */
4511 { NULL
, NULL
, 0, 0, { NULL
} }
4514 static void select_soundhw (const char *optarg
)
4518 if (*optarg
== '?') {
4521 printf ("Valid sound card names (comma separated):\n");
4522 for (c
= soundhw
; c
->name
; ++c
) {
4523 printf ("%-11s %s\n", c
->name
, c
->descr
);
4525 printf ("\n-soundhw all will enable all of the above\n");
4526 exit (*optarg
!= '?');
4534 if (!strcmp (optarg
, "all")) {
4535 for (c
= soundhw
; c
->name
; ++c
) {
4543 e
= strchr (p
, ',');
4544 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4546 for (c
= soundhw
; c
->name
; ++c
) {
4547 if (!strncmp (c
->name
, p
, l
)) {
4556 "Unknown sound card name (too big to show)\n");
4559 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4564 p
+= l
+ (e
!= NULL
);
4568 goto show_valid_cards
;
4573 static void select_vgahw (const char *p
)
4577 if (strstart(p
, "std", &opts
)) {
4578 cirrus_vga_enabled
= 0;
4580 } else if (strstart(p
, "cirrus", &opts
)) {
4581 cirrus_vga_enabled
= 1;
4583 } else if (strstart(p
, "vmware", &opts
)) {
4584 cirrus_vga_enabled
= 0;
4588 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4592 const char *nextopt
;
4594 if (strstart(opts
, ",retrace=", &nextopt
)) {
4596 if (strstart(opts
, "dumb", &nextopt
))
4597 vga_retrace_method
= VGA_RETRACE_DUMB
;
4598 else if (strstart(opts
, "precise", &nextopt
))
4599 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4600 else goto invalid_vga
;
4601 } else goto invalid_vga
;
4607 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4609 exit(STATUS_CONTROL_C_EXIT
);
4614 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4618 if(strlen(str
) != 36)
4621 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4622 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4623 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4631 #define MAX_NET_CLIENTS 32
4633 static int saved_argc
;
4634 static char **saved_argv
;
4636 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4640 *opt_daemonize
= daemonize
;
4641 *opt_incoming
= incoming
;
4645 static int gethugepagesize(void)
4649 const char *needle
= "Hugepagesize:";
4651 unsigned long hugepagesize
;
4653 fd
= open("/proc/meminfo", O_RDONLY
);
4659 ret
= read(fd
, buf
, sizeof(buf
));
4665 size
= strstr(buf
, needle
);
4668 size
+= strlen(needle
);
4669 hugepagesize
= strtol(size
, NULL
, 0);
4670 return hugepagesize
;
4673 static void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4679 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4682 hpagesize
= gethugepagesize() * 1024;
4686 fd
= mkstemp(filename
);
4695 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4698 * ftruncate is not supported by hugetlbfs in older
4699 * hosts, so don't bother checking for errors.
4700 * If anything goes wrong with it under other filesystems,
4703 ftruncate(fd
, memory
);
4705 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4706 if (area
== MAP_FAILED
) {
4716 static void *qemu_alloc_physram(unsigned long memory
)
4719 unsigned long map_len
= memory
;
4723 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4726 area
= qemu_vmalloc(memory
);
4728 if (kvm_setup_guest_memory(area
, map_len
))
4736 static void termsig_handler(int signal
)
4738 qemu_system_shutdown_request();
4741 static void termsig_setup(void)
4743 struct sigaction act
;
4745 memset(&act
, 0, sizeof(act
));
4746 act
.sa_handler
= termsig_handler
;
4747 sigaction(SIGINT
, &act
, NULL
);
4748 sigaction(SIGHUP
, &act
, NULL
);
4749 sigaction(SIGTERM
, &act
, NULL
);
4754 int main(int argc
, char **argv
, char **envp
)
4756 #ifdef CONFIG_GDBSTUB
4758 const char *gdbstub_port
;
4760 uint32_t boot_devices_bitmap
= 0;
4762 int snapshot
, linux_boot
, net_boot
;
4763 const char *initrd_filename
;
4764 const char *kernel_filename
, *kernel_cmdline
;
4765 const char *boot_devices
= "";
4766 DisplayState
*ds
= &display_state
;
4767 int cyls
, heads
, secs
, translation
;
4768 const char *net_clients
[MAX_NET_CLIENTS
];
4770 const char *bt_opts
[MAX_BT_CMDLINE
];
4774 const char *r
, *optarg
;
4775 CharDriverState
*monitor_hd
;
4776 const char *monitor_device
;
4777 const char *serial_devices
[MAX_SERIAL_PORTS
];
4778 int serial_device_index
;
4779 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4780 int parallel_device_index
;
4781 const char *loadvm
= NULL
;
4782 QEMUMachine
*machine
;
4783 const char *cpu_model
;
4784 const char *usb_devices
[MAX_USB_CMDLINE
];
4785 int usb_devices_index
;
4788 const char *pid_file
= NULL
;
4790 const char *incoming
= NULL
;
4792 qemu_cache_utils_init(envp
);
4794 LIST_INIT (&vm_change_state_head
);
4797 struct sigaction act
;
4798 sigfillset(&act
.sa_mask
);
4800 act
.sa_handler
= SIG_IGN
;
4801 sigaction(SIGPIPE
, &act
, NULL
);
4804 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4805 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4806 QEMU to run on a single CPU */
4811 h
= GetCurrentProcess();
4812 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4813 for(i
= 0; i
< 32; i
++) {
4814 if (mask
& (1 << i
))
4819 SetProcessAffinityMask(h
, mask
);
4825 register_machines();
4826 machine
= first_machine
;
4828 initrd_filename
= NULL
;
4830 vga_ram_size
= VGA_RAM_SIZE
;
4831 #ifdef CONFIG_GDBSTUB
4833 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4838 kernel_filename
= NULL
;
4839 kernel_cmdline
= "";
4840 cyls
= heads
= secs
= 0;
4841 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4842 monitor_device
= "vc";
4844 serial_devices
[0] = "vc:80Cx24C";
4845 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4846 serial_devices
[i
] = NULL
;
4847 serial_device_index
= 0;
4849 parallel_devices
[0] = "vc:640x480";
4850 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4851 parallel_devices
[i
] = NULL
;
4852 parallel_device_index
= 0;
4854 usb_devices_index
= 0;
4855 assigned_devices_index
= 0;
4874 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4876 const QEMUOption
*popt
;
4879 /* Treat --foo the same as -foo. */
4882 popt
= qemu_options
;
4885 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4889 if (!strcmp(popt
->name
, r
+ 1))
4893 if (popt
->flags
& HAS_ARG
) {
4894 if (optind
>= argc
) {
4895 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4899 optarg
= argv
[optind
++];
4904 switch(popt
->index
) {
4906 machine
= find_machine(optarg
);
4909 printf("Supported machines are:\n");
4910 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4911 printf("%-10s %s%s\n",
4913 m
== first_machine
? " (default)" : "");
4915 exit(*optarg
!= '?');
4918 case QEMU_OPTION_cpu
:
4919 /* hw initialization will check this */
4920 if (*optarg
== '?') {
4921 /* XXX: implement xxx_cpu_list for targets that still miss it */
4922 #if defined(cpu_list)
4923 cpu_list(stdout
, &fprintf
);
4930 case QEMU_OPTION_initrd
:
4931 initrd_filename
= optarg
;
4933 case QEMU_OPTION_hda
:
4935 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4937 hda_index
= drive_add(optarg
, HD_ALIAS
4938 ",cyls=%d,heads=%d,secs=%d%s",
4939 0, cyls
, heads
, secs
,
4940 translation
== BIOS_ATA_TRANSLATION_LBA
?
4942 translation
== BIOS_ATA_TRANSLATION_NONE
?
4943 ",trans=none" : "");
4945 case QEMU_OPTION_hdb
:
4946 case QEMU_OPTION_hdc
:
4947 case QEMU_OPTION_hdd
:
4948 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4950 case QEMU_OPTION_drive
:
4951 drive_add(NULL
, "%s", optarg
);
4953 case QEMU_OPTION_mtdblock
:
4954 drive_add(optarg
, MTD_ALIAS
);
4956 case QEMU_OPTION_sd
:
4957 drive_add(optarg
, SD_ALIAS
);
4959 case QEMU_OPTION_pflash
:
4960 drive_add(optarg
, PFLASH_ALIAS
);
4962 case QEMU_OPTION_snapshot
:
4965 case QEMU_OPTION_hdachs
:
4969 cyls
= strtol(p
, (char **)&p
, 0);
4970 if (cyls
< 1 || cyls
> 16383)
4975 heads
= strtol(p
, (char **)&p
, 0);
4976 if (heads
< 1 || heads
> 16)
4981 secs
= strtol(p
, (char **)&p
, 0);
4982 if (secs
< 1 || secs
> 63)
4986 if (!strcmp(p
, "none"))
4987 translation
= BIOS_ATA_TRANSLATION_NONE
;
4988 else if (!strcmp(p
, "lba"))
4989 translation
= BIOS_ATA_TRANSLATION_LBA
;
4990 else if (!strcmp(p
, "auto"))
4991 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4994 } else if (*p
!= '\0') {
4996 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4999 if (hda_index
!= -1)
5000 snprintf(drives_opt
[hda_index
].opt
,
5001 sizeof(drives_opt
[hda_index
].opt
),
5002 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5003 0, cyls
, heads
, secs
,
5004 translation
== BIOS_ATA_TRANSLATION_LBA
?
5006 translation
== BIOS_ATA_TRANSLATION_NONE
?
5007 ",trans=none" : "");
5010 case QEMU_OPTION_nographic
:
5013 #ifdef CONFIG_CURSES
5014 case QEMU_OPTION_curses
:
5018 case QEMU_OPTION_portrait
:
5021 case QEMU_OPTION_kernel
:
5022 kernel_filename
= optarg
;
5024 case QEMU_OPTION_append
:
5025 kernel_cmdline
= optarg
;
5027 case QEMU_OPTION_cdrom
:
5028 drive_add(optarg
, CDROM_ALIAS
);
5030 case QEMU_OPTION_boot
:
5031 boot_devices
= optarg
;
5032 /* We just do some generic consistency checks */
5034 /* Could easily be extended to 64 devices if needed */
5037 boot_devices_bitmap
= 0;
5038 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5039 /* Allowed boot devices are:
5040 * a b : floppy disk drives
5041 * c ... f : IDE disk drives
5042 * g ... m : machine implementation dependant drives
5043 * n ... p : network devices
5044 * It's up to each machine implementation to check
5045 * if the given boot devices match the actual hardware
5046 * implementation and firmware features.
5048 if (*p
< 'a' || *p
> 'q') {
5049 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5052 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5054 "Boot device '%c' was given twice\n",*p
);
5057 boot_devices_bitmap
|= 1 << (*p
- 'a');
5061 case QEMU_OPTION_fda
:
5062 case QEMU_OPTION_fdb
:
5063 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5066 case QEMU_OPTION_no_fd_bootchk
:
5070 case QEMU_OPTION_net
:
5071 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5072 fprintf(stderr
, "qemu: too many network clients\n");
5075 net_clients
[nb_net_clients
] = optarg
;
5079 case QEMU_OPTION_tftp
:
5080 tftp_prefix
= optarg
;
5082 case QEMU_OPTION_bootp
:
5083 bootp_filename
= optarg
;
5086 case QEMU_OPTION_smb
:
5087 net_slirp_smb(optarg
);
5090 case QEMU_OPTION_redir
:
5091 net_slirp_redir(optarg
);
5094 case QEMU_OPTION_bt
:
5095 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5096 fprintf(stderr
, "qemu: too many bluetooth options\n");
5099 bt_opts
[nb_bt_opts
++] = optarg
;
5102 case QEMU_OPTION_audio_help
:
5106 case QEMU_OPTION_soundhw
:
5107 select_soundhw (optarg
);
5113 case QEMU_OPTION_m
: {
5117 value
= strtoul(optarg
, &ptr
, 10);
5119 case 0: case 'M': case 'm':
5126 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5130 /* On 32-bit hosts, QEMU is limited by virtual address space */
5131 if (value
> (2047 << 20)
5133 && HOST_LONG_BITS
== 32
5136 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5139 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5140 fprintf(stderr
, "qemu: ram size too large\n");
5149 const CPULogItem
*item
;
5151 mask
= cpu_str_to_log_mask(optarg
);
5153 printf("Log items (comma separated):\n");
5154 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5155 printf("%-10s %s\n", item
->name
, item
->help
);
5162 #ifdef CONFIG_GDBSTUB
5167 gdbstub_port
= optarg
;
5173 case QEMU_OPTION_bios
:
5180 keyboard_layout
= optarg
;
5182 case QEMU_OPTION_localtime
:
5185 case QEMU_OPTION_vga
:
5186 select_vgahw (optarg
);
5193 w
= strtol(p
, (char **)&p
, 10);
5196 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5202 h
= strtol(p
, (char **)&p
, 10);
5207 depth
= strtol(p
, (char **)&p
, 10);
5208 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5209 depth
!= 24 && depth
!= 32)
5211 } else if (*p
== '\0') {
5212 depth
= graphic_depth
;
5219 graphic_depth
= depth
;
5222 case QEMU_OPTION_echr
:
5225 term_escape_char
= strtol(optarg
, &r
, 0);
5227 printf("Bad argument to echr\n");
5230 case QEMU_OPTION_monitor
:
5231 monitor_device
= optarg
;
5233 case QEMU_OPTION_serial
:
5234 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5235 fprintf(stderr
, "qemu: too many serial ports\n");
5238 serial_devices
[serial_device_index
] = optarg
;
5239 serial_device_index
++;
5241 case QEMU_OPTION_parallel
:
5242 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5243 fprintf(stderr
, "qemu: too many parallel ports\n");
5246 parallel_devices
[parallel_device_index
] = optarg
;
5247 parallel_device_index
++;
5249 case QEMU_OPTION_loadvm
:
5252 case QEMU_OPTION_full_screen
:
5256 case QEMU_OPTION_no_frame
:
5259 case QEMU_OPTION_alt_grab
:
5262 case QEMU_OPTION_no_quit
:
5266 case QEMU_OPTION_pidfile
:
5270 case QEMU_OPTION_win2k_hack
:
5271 win2k_install_hack
= 1;
5275 case QEMU_OPTION_no_kqemu
:
5278 case QEMU_OPTION_kernel_kqemu
:
5283 case QEMU_OPTION_enable_kvm
:
5291 case QEMU_OPTION_no_kvm
:
5294 case QEMU_OPTION_no_kvm_irqchip
: {
5299 case QEMU_OPTION_no_kvm_pit
: {
5303 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5304 kvm_pit_reinject
= 0;
5307 case QEMU_OPTION_enable_nesting
: {
5311 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5312 case QEMU_OPTION_pcidevice
:
5313 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5314 fprintf(stderr
, "Too many assigned devices\n");
5317 assigned_devices
[assigned_devices_index
] = optarg
;
5318 assigned_devices_index
++;
5322 case QEMU_OPTION_usb
:
5325 case QEMU_OPTION_usbdevice
:
5327 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5328 fprintf(stderr
, "Too many USB devices\n");
5331 usb_devices
[usb_devices_index
] = optarg
;
5332 usb_devices_index
++;
5334 case QEMU_OPTION_smp
:
5335 smp_cpus
= atoi(optarg
);
5337 fprintf(stderr
, "Invalid number of CPUs\n");
5341 case QEMU_OPTION_vnc
:
5342 vnc_display
= optarg
;
5344 case QEMU_OPTION_no_acpi
:
5347 case QEMU_OPTION_no_hpet
:
5350 case QEMU_OPTION_no_reboot
:
5353 case QEMU_OPTION_no_shutdown
:
5356 case QEMU_OPTION_show_cursor
:
5359 case QEMU_OPTION_uuid
:
5360 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5361 fprintf(stderr
, "Fail to parse UUID string."
5362 " Wrong format.\n");
5366 case QEMU_OPTION_daemonize
:
5369 case QEMU_OPTION_option_rom
:
5370 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5371 fprintf(stderr
, "Too many option ROMs\n");
5374 option_rom
[nb_option_roms
] = optarg
;
5377 case QEMU_OPTION_semihosting
:
5378 semihosting_enabled
= 1;
5380 case QEMU_OPTION_tdf
:
5383 case QEMU_OPTION_kvm_shadow_memory
:
5384 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5386 case QEMU_OPTION_mempath
:
5389 case QEMU_OPTION_name
:
5392 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5393 case QEMU_OPTION_prom_env
:
5394 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5395 fprintf(stderr
, "Too many prom variables\n");
5398 prom_envs
[nb_prom_envs
] = optarg
;
5402 case QEMU_OPTION_cpu_vendor
:
5403 cpu_vendor_string
= optarg
;
5406 case QEMU_OPTION_old_param
:
5410 case QEMU_OPTION_clock
:
5411 configure_alarms(optarg
);
5413 case QEMU_OPTION_startdate
:
5416 time_t rtc_start_date
;
5417 if (!strcmp(optarg
, "now")) {
5418 rtc_date_offset
= -1;
5420 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5428 } else if (sscanf(optarg
, "%d-%d-%d",
5431 &tm
.tm_mday
) == 3) {
5440 rtc_start_date
= mktimegm(&tm
);
5441 if (rtc_start_date
== -1) {
5443 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5444 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5447 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5451 case QEMU_OPTION_tb_size
:
5452 tb_size
= strtol(optarg
, NULL
, 0);
5456 case QEMU_OPTION_icount
:
5458 if (strcmp(optarg
, "auto") == 0) {
5459 icount_time_shift
= -1;
5461 icount_time_shift
= strtol(optarg
, NULL
, 0);
5464 case QEMU_OPTION_incoming
:
5467 case QEMU_OPTION_nvram
:
5474 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5475 if (kvm_allowed
&& kqemu_allowed
) {
5477 "You can not enable both KVM and kqemu at the same time\n");
5482 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5483 if (smp_cpus
> machine
->max_cpus
) {
5484 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5485 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5491 if (serial_device_index
== 0)
5492 serial_devices
[0] = "stdio";
5493 if (parallel_device_index
== 0)
5494 parallel_devices
[0] = "null";
5495 if (strncmp(monitor_device
, "vc", 2) == 0)
5496 monitor_device
= "stdio";
5503 if (pipe(fds
) == -1)
5514 len
= read(fds
[0], &status
, 1);
5515 if (len
== -1 && (errno
== EINTR
))
5520 else if (status
== 1) {
5521 fprintf(stderr
, "Could not acquire pidfile\n");
5538 signal(SIGTSTP
, SIG_IGN
);
5539 signal(SIGTTOU
, SIG_IGN
);
5540 signal(SIGTTIN
, SIG_IGN
);
5545 if (kvm_enabled()) {
5546 if (kvm_qemu_init() < 0) {
5547 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5548 #ifdef NO_CPU_EMULATION
5549 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5557 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5560 write(fds
[1], &status
, 1);
5562 fprintf(stderr
, "Could not acquire pid file\n");
5570 linux_boot
= (kernel_filename
!= NULL
);
5571 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5573 if (!linux_boot
&& net_boot
== 0 &&
5574 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5577 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5578 fprintf(stderr
, "-append only allowed with -kernel option\n");
5582 if (!linux_boot
&& initrd_filename
!= NULL
) {
5583 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5587 /* boot to floppy or the default cd if no hard disk defined yet */
5588 if (!boot_devices
[0]) {
5589 boot_devices
= "cad";
5591 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5594 if (init_timer_alarm() < 0) {
5595 fprintf(stderr
, "could not initialize alarm timer\n");
5598 if (use_icount
&& icount_time_shift
< 0) {
5600 /* 125MIPS seems a reasonable initial guess at the guest speed.
5601 It will be corrected fairly quickly anyway. */
5602 icount_time_shift
= 3;
5603 init_icount_adjust();
5610 /* init network clients */
5611 if (nb_net_clients
== 0) {
5612 /* if no clients, we use a default config */
5613 net_clients
[nb_net_clients
++] = "nic";
5615 net_clients
[nb_net_clients
++] = "user";
5619 for(i
= 0;i
< nb_net_clients
; i
++) {
5620 if (net_client_parse(net_clients
[i
]) < 0)
5626 /* XXX: this should be moved in the PC machine instantiation code */
5627 if (net_boot
!= 0) {
5629 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5630 const char *model
= nd_table
[i
].model
;
5632 if (net_boot
& (1 << i
)) {
5635 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5636 if (get_image_size(buf
) > 0) {
5637 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5638 fprintf(stderr
, "Too many option ROMs\n");
5641 option_rom
[nb_option_roms
] = strdup(buf
);
5648 fprintf(stderr
, "No valid PXE rom found for network device\n");
5654 /* init the bluetooth world */
5655 for (i
= 0; i
< nb_bt_opts
; i
++)
5656 if (bt_parse(bt_opts
[i
]))
5659 /* init the memory */
5660 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5662 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5664 if (ram_size
< phys_ram_size
) {
5665 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5666 machine
->name
, (unsigned long long) phys_ram_size
);
5670 phys_ram_size
= ram_size
;
5672 ram_size
= phys_ram_size
;
5675 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5677 phys_ram_size
+= ram_size
;
5680 /* Initialize kvm */
5681 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5682 #define KVM_EXTRA_PAGES 3
5684 #define KVM_EXTRA_PAGES 0
5686 if (kvm_enabled()) {
5687 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5688 if (kvm_qemu_create_context() < 0) {
5689 fprintf(stderr
, "Could not create KVM context\n");
5694 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5695 if (!phys_ram_base
) {
5696 fprintf(stderr
, "Could not allocate physical memory\n");
5700 /* init the dynamic translator */
5701 cpu_exec_init_all(tb_size
* 1024 * 1024);
5705 /* we always create the cdrom drive, even if no disk is there */
5707 if (nb_drives_opt
< MAX_DRIVES
)
5708 drive_add(NULL
, CDROM_ALIAS
);
5710 /* we always create at least one floppy */
5712 if (nb_drives_opt
< MAX_DRIVES
)
5713 drive_add(NULL
, FD_ALIAS
, 0);
5715 /* we always create one sd slot, even if no card is in it */
5717 if (nb_drives_opt
< MAX_DRIVES
)
5718 drive_add(NULL
, SD_ALIAS
);
5720 /* open the virtual block devices
5721 * note that migration with device
5722 * hot add/remove is broken.
5724 for(i
= 0; i
< nb_drives_opt
; i
++)
5725 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5728 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5729 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5732 memset(&display_state
, 0, sizeof(display_state
));
5735 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5738 /* nearly nothing to do */
5739 dumb_display_init(ds
);
5740 } else if (vnc_display
!= NULL
) {
5741 vnc_display_init(ds
);
5742 if (vnc_display_open(ds
, vnc_display
) < 0)
5745 #if defined(CONFIG_CURSES)
5747 curses_display_init(ds
, full_screen
);
5751 #if defined(CONFIG_SDL)
5752 sdl_display_init(ds
, full_screen
, no_frame
);
5753 #elif defined(CONFIG_COCOA)
5754 cocoa_display_init(ds
, full_screen
);
5756 dumb_display_init(ds
);
5761 /* must be after terminal init, SDL library changes signal handlers */
5765 /* Maintain compatibility with multiple stdio monitors */
5766 if (!strcmp(monitor_device
,"stdio")) {
5767 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5768 const char *devname
= serial_devices
[i
];
5769 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5770 monitor_device
= NULL
;
5772 } else if (devname
&& !strcmp(devname
,"stdio")) {
5773 monitor_device
= NULL
;
5774 serial_devices
[i
] = "mon:stdio";
5779 if (monitor_device
) {
5780 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5782 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5785 monitor_init(monitor_hd
, !nographic
);
5788 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5789 const char *devname
= serial_devices
[i
];
5790 if (devname
&& strcmp(devname
, "none")) {
5792 snprintf(label
, sizeof(label
), "serial%d", i
);
5793 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5794 if (!serial_hds
[i
]) {
5795 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5799 if (strstart(devname
, "vc", 0))
5800 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5804 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5805 const char *devname
= parallel_devices
[i
];
5806 if (devname
&& strcmp(devname
, "none")) {
5808 snprintf(label
, sizeof(label
), "parallel%d", i
);
5809 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5810 if (!parallel_hds
[i
]) {
5811 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5815 if (strstart(devname
, "vc", 0))
5816 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5824 if (kvm_enabled()) {
5827 ret
= kvm_init(smp_cpus
);
5829 fprintf(stderr
, "failed to initialize KVM\n");
5835 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5836 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5838 current_machine
= machine
;
5840 /* Set KVM's vcpu state to qemu's initial CPUState. */
5841 if (kvm_enabled()) {
5844 ret
= kvm_sync_vcpus();
5846 fprintf(stderr
, "failed to initialize vcpus\n");
5851 /* init USB devices */
5853 for(i
= 0; i
< usb_devices_index
; i
++) {
5854 if (usb_device_add(usb_devices
[i
]) < 0) {
5855 fprintf(stderr
, "Warning: could not add USB device %s\n",
5861 if (display_state
.dpy_refresh
) {
5862 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5863 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5866 #ifdef CONFIG_GDBSTUB
5868 /* XXX: use standard host:port notation and modify options
5870 if (gdbserver_start(gdbstub_port
) < 0) {
5871 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5882 autostart
= 0; /* fixme how to deal with -daemonize */
5883 qemu_start_incoming_migration(incoming
);
5887 /* XXX: simplify init */
5900 len
= write(fds
[1], &status
, 1);
5901 if (len
== -1 && (errno
== EINTR
))
5908 TFR(fd
= open("/dev/null", O_RDWR
));