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
;
248 unsigned int nb_prom_envs
= 0;
249 const char *prom_envs
[MAX_PROM_ENVS
];
252 struct drive_opt drives_opt
[MAX_DRIVES
];
254 static CPUState
*cur_cpu
;
255 static CPUState
*next_cpu
;
256 static int event_pending
= 1;
257 /* Conversion factor from emulated instructions to virtual clock ticks. */
258 static int icount_time_shift
;
259 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
260 #define MAX_ICOUNT_SHIFT 10
261 /* Compensate for varying guest execution speed. */
262 static int64_t qemu_icount_bias
;
263 static QEMUTimer
*icount_rt_timer
;
264 static QEMUTimer
*icount_vm_timer
;
266 uint8_t qemu_uuid
[16];
268 /* KVM runs the main loop in a separate thread. If we update one of the lists
269 * that are polled before or after select(), we need to make sure to break out
270 * of the select() to ensure the new item is serviced.
272 static void main_loop_break(void)
275 qemu_kvm_notify_work();
278 /***********************************************************/
279 /* x86 ISA bus support */
281 target_phys_addr_t isa_mem_base
= 0;
284 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
285 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
287 static uint32_t ioport_read(int index
, uint32_t address
)
289 static IOPortReadFunc
*default_func
[3] = {
290 default_ioport_readb
,
291 default_ioport_readw
,
294 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
296 func
= default_func
[index
];
297 return func(ioport_opaque
[address
], address
);
300 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
302 static IOPortWriteFunc
*default_func
[3] = {
303 default_ioport_writeb
,
304 default_ioport_writew
,
305 default_ioport_writel
307 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
309 func
= default_func
[index
];
310 func(ioport_opaque
[address
], address
, data
);
313 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
315 #ifdef DEBUG_UNUSED_IOPORT
316 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
321 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
323 #ifdef DEBUG_UNUSED_IOPORT
324 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
328 /* default is to make two byte accesses */
329 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
332 data
= ioport_read(0, address
);
333 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
334 data
|= ioport_read(0, address
) << 8;
338 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
340 ioport_write(0, address
, data
& 0xff);
341 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
342 ioport_write(0, address
, (data
>> 8) & 0xff);
345 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
347 #ifdef DEBUG_UNUSED_IOPORT
348 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
353 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
355 #ifdef DEBUG_UNUSED_IOPORT
356 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
360 /* size is the word size in byte */
361 int register_ioport_read(int start
, int length
, int size
,
362 IOPortReadFunc
*func
, void *opaque
)
368 } else if (size
== 2) {
370 } else if (size
== 4) {
373 hw_error("register_ioport_read: invalid size");
376 for(i
= start
; i
< start
+ length
; i
+= size
) {
377 ioport_read_table
[bsize
][i
] = func
;
378 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
379 hw_error("register_ioport_read: invalid opaque");
380 ioport_opaque
[i
] = opaque
;
385 /* size is the word size in byte */
386 int register_ioport_write(int start
, int length
, int size
,
387 IOPortWriteFunc
*func
, void *opaque
)
393 } else if (size
== 2) {
395 } else if (size
== 4) {
398 hw_error("register_ioport_write: invalid size");
401 for(i
= start
; i
< start
+ length
; i
+= size
) {
402 ioport_write_table
[bsize
][i
] = func
;
403 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
404 hw_error("register_ioport_write: invalid opaque");
405 ioport_opaque
[i
] = opaque
;
410 void isa_unassign_ioport(int start
, int length
)
414 for(i
= start
; i
< start
+ length
; i
++) {
415 ioport_read_table
[0][i
] = default_ioport_readb
;
416 ioport_read_table
[1][i
] = default_ioport_readw
;
417 ioport_read_table
[2][i
] = default_ioport_readl
;
419 ioport_write_table
[0][i
] = default_ioport_writeb
;
420 ioport_write_table
[1][i
] = default_ioport_writew
;
421 ioport_write_table
[2][i
] = default_ioport_writel
;
423 ioport_opaque
[i
] = NULL
;
427 /***********************************************************/
429 void cpu_outb(CPUState
*env
, int addr
, int val
)
432 if (loglevel
& CPU_LOG_IOPORT
)
433 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
435 ioport_write(0, addr
, val
);
438 env
->last_io_time
= cpu_get_time_fast();
442 void cpu_outw(CPUState
*env
, int addr
, int val
)
445 if (loglevel
& CPU_LOG_IOPORT
)
446 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
448 ioport_write(1, addr
, val
);
451 env
->last_io_time
= cpu_get_time_fast();
455 void cpu_outl(CPUState
*env
, int addr
, int val
)
458 if (loglevel
& CPU_LOG_IOPORT
)
459 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
461 ioport_write(2, addr
, val
);
464 env
->last_io_time
= cpu_get_time_fast();
468 int cpu_inb(CPUState
*env
, int addr
)
471 val
= ioport_read(0, addr
);
473 if (loglevel
& CPU_LOG_IOPORT
)
474 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
478 env
->last_io_time
= cpu_get_time_fast();
483 int cpu_inw(CPUState
*env
, int addr
)
486 val
= ioport_read(1, addr
);
488 if (loglevel
& CPU_LOG_IOPORT
)
489 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
493 env
->last_io_time
= cpu_get_time_fast();
498 int cpu_inl(CPUState
*env
, int addr
)
501 val
= ioport_read(2, addr
);
503 if (loglevel
& CPU_LOG_IOPORT
)
504 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
508 env
->last_io_time
= cpu_get_time_fast();
513 /***********************************************************/
514 void hw_error(const char *fmt
, ...)
520 fprintf(stderr
, "qemu: hardware error: ");
521 vfprintf(stderr
, fmt
, ap
);
522 fprintf(stderr
, "\n");
523 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
524 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
526 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
528 cpu_dump_state(env
, stderr
, fprintf
, 0);
538 static QEMUBalloonEvent
*qemu_balloon_event
;
539 void *qemu_balloon_event_opaque
;
541 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
543 qemu_balloon_event
= func
;
544 qemu_balloon_event_opaque
= opaque
;
547 void qemu_balloon(ram_addr_t target
)
549 if (qemu_balloon_event
)
550 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
553 ram_addr_t
qemu_balloon_status(void)
555 if (qemu_balloon_event
)
556 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
560 /***********************************************************/
563 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
564 static void *qemu_put_kbd_event_opaque
;
565 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
566 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
568 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
570 qemu_put_kbd_event_opaque
= opaque
;
571 qemu_put_kbd_event
= func
;
574 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
575 void *opaque
, int absolute
,
578 QEMUPutMouseEntry
*s
, *cursor
;
580 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
584 s
->qemu_put_mouse_event
= func
;
585 s
->qemu_put_mouse_event_opaque
= opaque
;
586 s
->qemu_put_mouse_event_absolute
= absolute
;
587 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
590 if (!qemu_put_mouse_event_head
) {
591 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
595 cursor
= qemu_put_mouse_event_head
;
596 while (cursor
->next
!= NULL
)
597 cursor
= cursor
->next
;
600 qemu_put_mouse_event_current
= s
;
605 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
607 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
609 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
612 cursor
= qemu_put_mouse_event_head
;
613 while (cursor
!= NULL
&& cursor
!= entry
) {
615 cursor
= cursor
->next
;
618 if (cursor
== NULL
) // does not exist or list empty
620 else if (prev
== NULL
) { // entry is head
621 qemu_put_mouse_event_head
= cursor
->next
;
622 if (qemu_put_mouse_event_current
== entry
)
623 qemu_put_mouse_event_current
= cursor
->next
;
624 qemu_free(entry
->qemu_put_mouse_event_name
);
629 prev
->next
= entry
->next
;
631 if (qemu_put_mouse_event_current
== entry
)
632 qemu_put_mouse_event_current
= prev
;
634 qemu_free(entry
->qemu_put_mouse_event_name
);
638 void kbd_put_keycode(int keycode
)
640 if (qemu_put_kbd_event
) {
641 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
645 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
647 QEMUPutMouseEvent
*mouse_event
;
648 void *mouse_event_opaque
;
651 if (!qemu_put_mouse_event_current
) {
656 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
658 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
661 if (graphic_rotate
) {
662 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
665 width
= graphic_width
- 1;
666 mouse_event(mouse_event_opaque
,
667 width
- dy
, dx
, dz
, buttons_state
);
669 mouse_event(mouse_event_opaque
,
670 dx
, dy
, dz
, buttons_state
);
674 int kbd_mouse_is_absolute(void)
676 if (!qemu_put_mouse_event_current
)
679 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
682 void do_info_mice(void)
684 QEMUPutMouseEntry
*cursor
;
687 if (!qemu_put_mouse_event_head
) {
688 term_printf("No mouse devices connected\n");
692 term_printf("Mouse devices available:\n");
693 cursor
= qemu_put_mouse_event_head
;
694 while (cursor
!= NULL
) {
695 term_printf("%c Mouse #%d: %s\n",
696 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
697 index
, cursor
->qemu_put_mouse_event_name
);
699 cursor
= cursor
->next
;
703 void do_mouse_set(int index
)
705 QEMUPutMouseEntry
*cursor
;
708 if (!qemu_put_mouse_event_head
) {
709 term_printf("No mouse devices connected\n");
713 cursor
= qemu_put_mouse_event_head
;
714 while (cursor
!= NULL
&& index
!= i
) {
716 cursor
= cursor
->next
;
720 qemu_put_mouse_event_current
= cursor
;
722 term_printf("Mouse at given index not found\n");
725 /* compute with 96 bit intermediate result: (a*b)/c */
726 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
731 #ifdef WORDS_BIGENDIAN
741 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
742 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
745 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
749 /***********************************************************/
750 /* real time host monotonic timer */
752 #define QEMU_TIMER_BASE 1000000000LL
756 static int64_t clock_freq
;
758 static void init_get_clock(void)
762 ret
= QueryPerformanceFrequency(&freq
);
764 fprintf(stderr
, "Could not calibrate ticks\n");
767 clock_freq
= freq
.QuadPart
;
770 static int64_t get_clock(void)
773 QueryPerformanceCounter(&ti
);
774 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
779 static int use_rt_clock
;
781 static void init_get_clock(void)
784 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
787 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
794 static int64_t get_clock(void)
796 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
799 clock_gettime(CLOCK_MONOTONIC
, &ts
);
800 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
804 /* XXX: using gettimeofday leads to problems if the date
805 changes, so it should be avoided. */
807 gettimeofday(&tv
, NULL
);
808 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
813 /* Return the virtual CPU time, based on the instruction counter. */
814 static int64_t cpu_get_icount(void)
817 CPUState
*env
= cpu_single_env
;;
818 icount
= qemu_icount
;
821 fprintf(stderr
, "Bad clock read\n");
822 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
824 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
827 /***********************************************************/
828 /* guest cycle counter */
830 static int64_t cpu_ticks_prev
;
831 static int64_t cpu_ticks_offset
;
832 static int64_t cpu_clock_offset
;
833 static int cpu_ticks_enabled
;
835 /* return the host CPU cycle counter and handle stop/restart */
836 int64_t cpu_get_ticks(void)
839 return cpu_get_icount();
841 if (!cpu_ticks_enabled
) {
842 return cpu_ticks_offset
;
845 ticks
= cpu_get_real_ticks();
846 if (cpu_ticks_prev
> ticks
) {
847 /* Note: non increasing ticks may happen if the host uses
849 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
851 cpu_ticks_prev
= ticks
;
852 return ticks
+ cpu_ticks_offset
;
856 /* return the host CPU monotonic timer and handle stop/restart */
857 static int64_t cpu_get_clock(void)
860 if (!cpu_ticks_enabled
) {
861 return cpu_clock_offset
;
864 return ti
+ cpu_clock_offset
;
868 /* enable cpu_get_ticks() */
869 void cpu_enable_ticks(void)
871 if (!cpu_ticks_enabled
) {
872 cpu_ticks_offset
-= cpu_get_real_ticks();
873 cpu_clock_offset
-= get_clock();
874 cpu_ticks_enabled
= 1;
878 /* disable cpu_get_ticks() : the clock is stopped. You must not call
879 cpu_get_ticks() after that. */
880 void cpu_disable_ticks(void)
882 if (cpu_ticks_enabled
) {
883 cpu_ticks_offset
= cpu_get_ticks();
884 cpu_clock_offset
= cpu_get_clock();
885 cpu_ticks_enabled
= 0;
889 /***********************************************************/
892 #define QEMU_TIMER_REALTIME 0
893 #define QEMU_TIMER_VIRTUAL 1
897 /* XXX: add frequency */
905 struct QEMUTimer
*next
;
908 struct qemu_alarm_timer
{
912 int (*start
)(struct qemu_alarm_timer
*t
);
913 void (*stop
)(struct qemu_alarm_timer
*t
);
914 void (*rearm
)(struct qemu_alarm_timer
*t
);
918 #define ALARM_FLAG_DYNTICKS 0x1
919 #define ALARM_FLAG_EXPIRED 0x2
921 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
923 return t
->flags
& ALARM_FLAG_DYNTICKS
;
926 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
928 if (!alarm_has_dynticks(t
))
934 /* TODO: MIN_TIMER_REARM_US should be optimized */
935 #define MIN_TIMER_REARM_US 250
937 static struct qemu_alarm_timer
*alarm_timer
;
939 static int alarm_timer_rfd
, alarm_timer_wfd
;
944 struct qemu_alarm_win32
{
948 } alarm_win32_data
= {0, NULL
, -1};
950 static int win32_start_timer(struct qemu_alarm_timer
*t
);
951 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
952 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
956 static int unix_start_timer(struct qemu_alarm_timer
*t
);
957 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
961 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
962 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
963 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
965 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
966 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
968 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
969 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
971 #endif /* __linux__ */
975 /* Correlation between real and virtual time is always going to be
976 fairly approximate, so ignore small variation.
977 When the guest is idle real and virtual time will be aligned in
979 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
981 static void icount_adjust(void)
986 static int64_t last_delta
;
987 /* If the VM is not running, then do nothing. */
991 cur_time
= cpu_get_clock();
992 cur_icount
= qemu_get_clock(vm_clock
);
993 delta
= cur_icount
- cur_time
;
994 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
996 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
997 && icount_time_shift
> 0) {
998 /* The guest is getting too far ahead. Slow time down. */
1002 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1003 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1004 /* The guest is getting too far behind. Speed time up. */
1005 icount_time_shift
++;
1008 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1011 static void icount_adjust_rt(void * opaque
)
1013 qemu_mod_timer(icount_rt_timer
,
1014 qemu_get_clock(rt_clock
) + 1000);
1018 static void icount_adjust_vm(void * opaque
)
1020 qemu_mod_timer(icount_vm_timer
,
1021 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1025 static void init_icount_adjust(void)
1027 /* Have both realtime and virtual time triggers for speed adjustment.
1028 The realtime trigger catches emulated time passing too slowly,
1029 the virtual time trigger catches emulated time passing too fast.
1030 Realtime triggers occur even when idle, so use them less frequently
1031 than VM triggers. */
1032 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1033 qemu_mod_timer(icount_rt_timer
,
1034 qemu_get_clock(rt_clock
) + 1000);
1035 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1036 qemu_mod_timer(icount_vm_timer
,
1037 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1040 static struct qemu_alarm_timer alarm_timers
[] = {
1043 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1044 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1045 /* HPET - if available - is preferred */
1046 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1047 /* ...otherwise try RTC */
1048 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1050 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1052 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1053 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1054 {"win32", 0, win32_start_timer
,
1055 win32_stop_timer
, NULL
, &alarm_win32_data
},
1060 static void show_available_alarms(void)
1064 printf("Available alarm timers, in order of precedence:\n");
1065 for (i
= 0; alarm_timers
[i
].name
; i
++)
1066 printf("%s\n", alarm_timers
[i
].name
);
1069 static void configure_alarms(char const *opt
)
1073 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1076 struct qemu_alarm_timer tmp
;
1078 if (!strcmp(opt
, "?")) {
1079 show_available_alarms();
1085 /* Reorder the array */
1086 name
= strtok(arg
, ",");
1088 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1089 if (!strcmp(alarm_timers
[i
].name
, name
))
1094 fprintf(stderr
, "Unknown clock %s\n", name
);
1103 tmp
= alarm_timers
[i
];
1104 alarm_timers
[i
] = alarm_timers
[cur
];
1105 alarm_timers
[cur
] = tmp
;
1109 name
= strtok(NULL
, ",");
1115 /* Disable remaining timers */
1116 for (i
= cur
; i
< count
; i
++)
1117 alarm_timers
[i
].name
= NULL
;
1119 show_available_alarms();
1124 QEMUClock
*rt_clock
;
1125 QEMUClock
*vm_clock
;
1127 static QEMUTimer
*active_timers
[2];
1129 static QEMUClock
*qemu_new_clock(int type
)
1132 clock
= qemu_mallocz(sizeof(QEMUClock
));
1139 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1143 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1146 ts
->opaque
= opaque
;
1150 void qemu_free_timer(QEMUTimer
*ts
)
1155 /* stop a timer, but do not dealloc it */
1156 void qemu_del_timer(QEMUTimer
*ts
)
1160 /* NOTE: this code must be signal safe because
1161 qemu_timer_expired() can be called from a signal. */
1162 pt
= &active_timers
[ts
->clock
->type
];
1175 /* modify the current timer so that it will be fired when current_time
1176 >= expire_time. The corresponding callback will be called. */
1177 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1183 /* add the timer in the sorted list */
1184 /* NOTE: this code must be signal safe because
1185 qemu_timer_expired() can be called from a signal. */
1186 pt
= &active_timers
[ts
->clock
->type
];
1191 if (t
->expire_time
> expire_time
)
1195 ts
->expire_time
= expire_time
;
1199 /* Rearm if necessary */
1200 if (pt
== &active_timers
[ts
->clock
->type
]) {
1201 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1202 qemu_rearm_alarm_timer(alarm_timer
);
1204 /* Interrupt execution to force deadline recalculation. */
1205 if (use_icount
&& cpu_single_env
) {
1206 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1211 int qemu_timer_pending(QEMUTimer
*ts
)
1214 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1221 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1225 return (timer_head
->expire_time
<= current_time
);
1228 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1234 if (!ts
|| ts
->expire_time
> current_time
)
1236 /* remove timer from the list before calling the callback */
1237 *ptimer_head
= ts
->next
;
1240 /* run the callback (the timer list can be modified) */
1245 int64_t qemu_get_clock(QEMUClock
*clock
)
1247 switch(clock
->type
) {
1248 case QEMU_TIMER_REALTIME
:
1249 return get_clock() / 1000000;
1251 case QEMU_TIMER_VIRTUAL
:
1253 return cpu_get_icount();
1255 return cpu_get_clock();
1260 static void init_timers(void)
1263 ticks_per_sec
= QEMU_TIMER_BASE
;
1264 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1265 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1269 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1271 uint64_t expire_time
;
1273 if (qemu_timer_pending(ts
)) {
1274 expire_time
= ts
->expire_time
;
1278 qemu_put_be64(f
, expire_time
);
1281 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1283 uint64_t expire_time
;
1285 expire_time
= qemu_get_be64(f
);
1286 if (expire_time
!= -1) {
1287 qemu_mod_timer(ts
, expire_time
);
1293 static void timer_save(QEMUFile
*f
, void *opaque
)
1295 if (cpu_ticks_enabled
) {
1296 hw_error("cannot save state if virtual timers are running");
1298 qemu_put_be64(f
, cpu_ticks_offset
);
1299 qemu_put_be64(f
, ticks_per_sec
);
1300 qemu_put_be64(f
, cpu_clock_offset
);
1303 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1305 if (version_id
!= 1 && version_id
!= 2)
1307 if (cpu_ticks_enabled
) {
1310 cpu_ticks_offset
=qemu_get_be64(f
);
1311 ticks_per_sec
=qemu_get_be64(f
);
1312 if (version_id
== 2) {
1313 cpu_clock_offset
=qemu_get_be64(f
);
1319 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1320 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1322 static void host_alarm_handler(int host_signum
)
1326 #define DISP_FREQ 1000
1328 static int64_t delta_min
= INT64_MAX
;
1329 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1331 ti
= qemu_get_clock(vm_clock
);
1332 if (last_clock
!= 0) {
1333 delta
= ti
- last_clock
;
1334 if (delta
< delta_min
)
1336 if (delta
> delta_max
)
1339 if (++count
== DISP_FREQ
) {
1340 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1341 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1342 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1343 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1344 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1346 delta_min
= INT64_MAX
;
1355 alarm_has_dynticks(alarm_timer
) ||
1357 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1358 qemu_get_clock(vm_clock
))) ||
1359 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1360 qemu_get_clock(rt_clock
))) {
1361 CPUState
*env
= next_cpu
;
1364 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1365 SetEvent(data
->host_alarm
);
1367 static const char byte
= 0;
1368 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1370 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1373 /* stop the currently executing cpu because a timer occured */
1374 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1376 if (env
->kqemu_enabled
) {
1377 kqemu_cpu_interrupt(env
);
1385 static int64_t qemu_next_deadline(void)
1389 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1390 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1391 qemu_get_clock(vm_clock
);
1393 /* To avoid problems with overflow limit this to 2^32. */
1403 #if defined(__linux__) || defined(_WIN32)
1404 static uint64_t qemu_next_deadline_dyntick(void)
1412 delta
= (qemu_next_deadline() + 999) / 1000;
1414 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1415 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1416 qemu_get_clock(rt_clock
))*1000;
1417 if (rtdelta
< delta
)
1421 if (delta
< MIN_TIMER_REARM_US
)
1422 delta
= MIN_TIMER_REARM_US
;
1430 /* Sets a specific flag */
1431 static int fcntl_setfl(int fd
, int flag
)
1435 flags
= fcntl(fd
, F_GETFL
);
1439 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1445 #if defined(__linux__)
1447 #define RTC_FREQ 1024
1449 static void enable_sigio_timer(int fd
)
1451 struct sigaction act
;
1454 sigfillset(&act
.sa_mask
);
1456 act
.sa_handler
= host_alarm_handler
;
1458 sigaction(SIGIO
, &act
, NULL
);
1459 fcntl_setfl(fd
, O_ASYNC
);
1460 fcntl(fd
, F_SETOWN
, getpid());
1463 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1465 struct hpet_info info
;
1468 fd
= open("/dev/hpet", O_RDONLY
);
1473 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1475 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1476 "error, but for better emulation accuracy type:\n"
1477 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1481 /* Check capabilities */
1482 r
= ioctl(fd
, HPET_INFO
, &info
);
1486 /* Enable periodic mode */
1487 r
= ioctl(fd
, HPET_EPI
, 0);
1488 if (info
.hi_flags
&& (r
< 0))
1491 /* Enable interrupt */
1492 r
= ioctl(fd
, HPET_IE_ON
, 0);
1496 enable_sigio_timer(fd
);
1497 t
->priv
= (void *)(long)fd
;
1505 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1507 int fd
= (long)t
->priv
;
1512 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1515 unsigned long current_rtc_freq
= 0;
1517 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1520 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1521 if (current_rtc_freq
!= RTC_FREQ
&&
1522 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1523 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1524 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1525 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1528 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1534 enable_sigio_timer(rtc_fd
);
1536 t
->priv
= (void *)(long)rtc_fd
;
1541 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1543 int rtc_fd
= (long)t
->priv
;
1548 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1552 struct sigaction act
;
1554 sigfillset(&act
.sa_mask
);
1556 act
.sa_handler
= host_alarm_handler
;
1558 sigaction(SIGALRM
, &act
, NULL
);
1560 ev
.sigev_value
.sival_int
= 0;
1561 ev
.sigev_notify
= SIGEV_SIGNAL
;
1562 ev
.sigev_signo
= SIGALRM
;
1564 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1565 perror("timer_create");
1567 /* disable dynticks */
1568 fprintf(stderr
, "Dynamic Ticks disabled\n");
1573 t
->priv
= (void *)(long)host_timer
;
1578 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1580 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1582 timer_delete(host_timer
);
1585 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1587 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1588 struct itimerspec timeout
;
1589 int64_t nearest_delta_us
= INT64_MAX
;
1592 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1593 !active_timers
[QEMU_TIMER_VIRTUAL
])
1596 nearest_delta_us
= qemu_next_deadline_dyntick();
1598 /* check whether a timer is already running */
1599 if (timer_gettime(host_timer
, &timeout
)) {
1601 fprintf(stderr
, "Internal timer error: aborting\n");
1604 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1605 if (current_us
&& current_us
<= nearest_delta_us
)
1608 timeout
.it_interval
.tv_sec
= 0;
1609 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1610 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1611 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1612 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1614 fprintf(stderr
, "Internal timer error: aborting\n");
1619 #endif /* defined(__linux__) */
1621 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1623 struct sigaction act
;
1624 struct itimerval itv
;
1628 sigfillset(&act
.sa_mask
);
1630 act
.sa_handler
= host_alarm_handler
;
1632 sigaction(SIGALRM
, &act
, NULL
);
1634 itv
.it_interval
.tv_sec
= 0;
1635 /* for i386 kernel 2.6 to get 1 ms */
1636 itv
.it_interval
.tv_usec
= 999;
1637 itv
.it_value
.tv_sec
= 0;
1638 itv
.it_value
.tv_usec
= 10 * 1000;
1640 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1647 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1649 struct itimerval itv
;
1651 memset(&itv
, 0, sizeof(itv
));
1652 setitimer(ITIMER_REAL
, &itv
, NULL
);
1655 #endif /* !defined(_WIN32) */
1657 static void try_to_rearm_timer(void *opaque
)
1659 struct qemu_alarm_timer
*t
= opaque
;
1663 /* Drain the notify pipe */
1666 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1667 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1670 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1671 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1672 qemu_rearm_alarm_timer(alarm_timer
);
1678 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1681 struct qemu_alarm_win32
*data
= t
->priv
;
1684 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1685 if (!data
->host_alarm
) {
1686 perror("Failed CreateEvent");
1690 memset(&tc
, 0, sizeof(tc
));
1691 timeGetDevCaps(&tc
, sizeof(tc
));
1693 if (data
->period
< tc
.wPeriodMin
)
1694 data
->period
= tc
.wPeriodMin
;
1696 timeBeginPeriod(data
->period
);
1698 flags
= TIME_CALLBACK_FUNCTION
;
1699 if (alarm_has_dynticks(t
))
1700 flags
|= TIME_ONESHOT
;
1702 flags
|= TIME_PERIODIC
;
1704 data
->timerId
= timeSetEvent(1, // interval (ms)
1705 data
->period
, // resolution
1706 host_alarm_handler
, // function
1707 (DWORD
)t
, // parameter
1710 if (!data
->timerId
) {
1711 perror("Failed to initialize win32 alarm timer");
1713 timeEndPeriod(data
->period
);
1714 CloseHandle(data
->host_alarm
);
1718 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1723 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1725 struct qemu_alarm_win32
*data
= t
->priv
;
1727 timeKillEvent(data
->timerId
);
1728 timeEndPeriod(data
->period
);
1730 CloseHandle(data
->host_alarm
);
1733 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1735 struct qemu_alarm_win32
*data
= t
->priv
;
1736 uint64_t nearest_delta_us
;
1738 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1739 !active_timers
[QEMU_TIMER_VIRTUAL
])
1742 nearest_delta_us
= qemu_next_deadline_dyntick();
1743 nearest_delta_us
/= 1000;
1745 timeKillEvent(data
->timerId
);
1747 data
->timerId
= timeSetEvent(1,
1751 TIME_ONESHOT
| TIME_PERIODIC
);
1753 if (!data
->timerId
) {
1754 perror("Failed to re-arm win32 alarm timer");
1756 timeEndPeriod(data
->period
);
1757 CloseHandle(data
->host_alarm
);
1764 static int init_timer_alarm(void)
1766 struct qemu_alarm_timer
*t
= NULL
;
1776 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1780 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1784 alarm_timer_rfd
= fds
[0];
1785 alarm_timer_wfd
= fds
[1];
1788 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1789 t
= &alarm_timers
[i
];
1802 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1803 try_to_rearm_timer
, NULL
, t
);
1818 static void quit_timers(void)
1820 alarm_timer
->stop(alarm_timer
);
1824 /***********************************************************/
1825 /* host time/date access */
1826 void qemu_get_timedate(struct tm
*tm
, int offset
)
1833 if (rtc_date_offset
== -1) {
1837 ret
= localtime(&ti
);
1839 ti
-= rtc_date_offset
;
1843 memcpy(tm
, ret
, sizeof(struct tm
));
1846 int qemu_timedate_diff(struct tm
*tm
)
1850 if (rtc_date_offset
== -1)
1852 seconds
= mktimegm(tm
);
1854 seconds
= mktime(tm
);
1856 seconds
= mktimegm(tm
) + rtc_date_offset
;
1858 return seconds
- time(NULL
);
1862 static void socket_cleanup(void)
1867 static int socket_init(void)
1872 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1874 err
= WSAGetLastError();
1875 fprintf(stderr
, "WSAStartup: %d\n", err
);
1878 atexit(socket_cleanup
);
1883 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1888 while (*p
!= '\0' && *p
!= '=') {
1889 if (q
&& (q
- buf
) < buf_size
- 1)
1899 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1904 while (*p
!= '\0') {
1906 if (*(p
+ 1) != ',')
1910 if (q
&& (q
- buf
) < buf_size
- 1)
1920 int get_param_value(char *buf
, int buf_size
,
1921 const char *tag
, const char *str
)
1928 p
= get_opt_name(option
, sizeof(option
), p
);
1932 if (!strcmp(tag
, option
)) {
1933 (void)get_opt_value(buf
, buf_size
, p
);
1936 p
= get_opt_value(NULL
, 0, p
);
1945 int check_params(char *buf
, int buf_size
,
1946 const char * const *params
, const char *str
)
1953 p
= get_opt_name(buf
, buf_size
, p
);
1957 for(i
= 0; params
[i
] != NULL
; i
++)
1958 if (!strcmp(params
[i
], buf
))
1960 if (params
[i
] == NULL
)
1962 p
= get_opt_value(NULL
, 0, p
);
1970 /***********************************************************/
1971 /* Bluetooth support */
1974 static struct HCIInfo
*hci_table
[MAX_NICS
];
1976 static struct bt_vlan_s
{
1977 struct bt_scatternet_s net
;
1979 struct bt_vlan_s
*next
;
1982 /* find or alloc a new bluetooth "VLAN" */
1983 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1985 struct bt_vlan_s
**pvlan
, *vlan
;
1986 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1990 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1992 pvlan
= &first_bt_vlan
;
1993 while (*pvlan
!= NULL
)
1994 pvlan
= &(*pvlan
)->next
;
1999 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2003 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2008 static struct HCIInfo null_hci
= {
2009 .cmd_send
= null_hci_send
,
2010 .sco_send
= null_hci_send
,
2011 .acl_send
= null_hci_send
,
2012 .bdaddr_set
= null_hci_addr_set
,
2015 struct HCIInfo
*qemu_next_hci(void)
2017 if (cur_hci
== nb_hcis
)
2020 return hci_table
[cur_hci
++];
2023 static struct HCIInfo
*hci_init(const char *str
)
2026 struct bt_scatternet_s
*vlan
= 0;
2028 if (!strcmp(str
, "null"))
2031 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2033 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2034 else if (!strncmp(str
, "hci", 3)) {
2037 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2038 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2043 vlan
= qemu_find_bt_vlan(0);
2045 return bt_new_hci(vlan
);
2048 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2053 static int bt_hci_parse(const char *str
)
2055 struct HCIInfo
*hci
;
2058 if (nb_hcis
>= MAX_NICS
) {
2059 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2063 hci
= hci_init(str
);
2072 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2073 hci
->bdaddr_set(hci
, bdaddr
.b
);
2075 hci_table
[nb_hcis
++] = hci
;
2080 static void bt_vhci_add(int vlan_id
)
2082 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2085 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2086 "an empty scatternet %i\n", vlan_id
);
2088 bt_vhci_init(bt_new_hci(vlan
));
2091 static struct bt_device_s
*bt_device_add(const char *opt
)
2093 struct bt_scatternet_s
*vlan
;
2095 char *endp
= strstr(opt
, ",vlan=");
2096 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2099 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2102 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2104 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2109 vlan
= qemu_find_bt_vlan(vlan_id
);
2112 fprintf(stderr
, "qemu: warning: adding a slave device to "
2113 "an empty scatternet %i\n", vlan_id
);
2115 if (!strcmp(devname
, "keyboard"))
2116 return bt_keyboard_init(vlan
);
2118 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2122 static int bt_parse(const char *opt
)
2124 const char *endp
, *p
;
2127 if (strstart(opt
, "hci", &endp
)) {
2128 if (!*endp
|| *endp
== ',') {
2130 if (!strstart(endp
, ",vlan=", 0))
2133 return bt_hci_parse(opt
);
2135 } else if (strstart(opt
, "vhci", &endp
)) {
2136 if (!*endp
|| *endp
== ',') {
2138 if (strstart(endp
, ",vlan=", &p
)) {
2139 vlan
= strtol(p
, (char **) &endp
, 0);
2141 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2145 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2154 } else if (strstart(opt
, "device:", &endp
))
2155 return !bt_device_add(endp
);
2157 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2161 /***********************************************************/
2162 /* QEMU Block devices */
2164 #define HD_ALIAS "index=%d,media=disk"
2166 #define CDROM_ALIAS "index=1,media=cdrom"
2168 #define CDROM_ALIAS "index=2,media=cdrom"
2170 #define FD_ALIAS "index=%d,if=floppy"
2171 #define PFLASH_ALIAS "if=pflash"
2172 #define MTD_ALIAS "if=mtd"
2173 #define SD_ALIAS "index=0,if=sd"
2175 static int drive_opt_get_free_idx(void)
2179 for (index
= 0; index
< MAX_DRIVES
; index
++)
2180 if (!drives_opt
[index
].used
) {
2181 drives_opt
[index
].used
= 1;
2188 static int drive_get_free_idx(void)
2192 for (index
= 0; index
< MAX_DRIVES
; index
++)
2193 if (!drives_table
[index
].used
) {
2194 drives_table
[index
].used
= 1;
2201 int drive_add(const char *file
, const char *fmt
, ...)
2204 int index
= drive_opt_get_free_idx();
2206 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2207 fprintf(stderr
, "qemu: too many drives\n");
2211 drives_opt
[index
].file
= file
;
2213 vsnprintf(drives_opt
[index
].opt
,
2214 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2221 void drive_remove(int index
)
2223 drives_opt
[index
].used
= 0;
2227 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2231 /* seek interface, bus and unit */
2233 for (index
= 0; index
< MAX_DRIVES
; index
++)
2234 if (drives_table
[index
].type
== type
&&
2235 drives_table
[index
].bus
== bus
&&
2236 drives_table
[index
].unit
== unit
&&
2237 drives_table
[index
].used
)
2243 int drive_get_max_bus(BlockInterfaceType type
)
2249 for (index
= 0; index
< nb_drives
; index
++) {
2250 if(drives_table
[index
].type
== type
&&
2251 drives_table
[index
].bus
> max_bus
)
2252 max_bus
= drives_table
[index
].bus
;
2257 static void bdrv_format_print(void *opaque
, const char *name
)
2259 fprintf(stderr
, " %s", name
);
2262 void drive_uninit(BlockDriverState
*bdrv
)
2266 for (i
= 0; i
< MAX_DRIVES
; i
++)
2267 if (drives_table
[i
].bdrv
== bdrv
) {
2268 drives_table
[i
].bdrv
= NULL
;
2269 drives_table
[i
].used
= 0;
2270 drive_remove(drives_table
[i
].drive_opt_idx
);
2276 int drive_init(struct drive_opt
*arg
, int snapshot
,
2277 QEMUMachine
*machine
)
2282 const char *mediastr
= "";
2283 BlockInterfaceType type
;
2284 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2285 int bus_id
, unit_id
;
2286 int cyls
, heads
, secs
, translation
;
2287 BlockDriverState
*bdrv
;
2288 BlockDriver
*drv
= NULL
;
2293 int drives_table_idx
;
2294 char *str
= arg
->opt
;
2295 static const char * const params
[] = { "bus", "unit", "if", "index",
2296 "cyls", "heads", "secs", "trans",
2297 "media", "snapshot", "file",
2298 "cache", "format", "boot", NULL
};
2300 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2301 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2307 cyls
= heads
= secs
= 0;
2310 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2314 if (machine
->use_scsi
) {
2316 max_devs
= MAX_SCSI_DEVS
;
2317 pstrcpy(devname
, sizeof(devname
), "scsi");
2320 max_devs
= MAX_IDE_DEVS
;
2321 pstrcpy(devname
, sizeof(devname
), "ide");
2325 /* extract parameters */
2327 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2328 bus_id
= strtol(buf
, NULL
, 0);
2330 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2335 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2336 unit_id
= strtol(buf
, NULL
, 0);
2338 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2343 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2344 pstrcpy(devname
, sizeof(devname
), buf
);
2345 if (!strcmp(buf
, "ide")) {
2347 max_devs
= MAX_IDE_DEVS
;
2348 } else if (!strcmp(buf
, "scsi")) {
2350 max_devs
= MAX_SCSI_DEVS
;
2351 } else if (!strcmp(buf
, "floppy")) {
2354 } else if (!strcmp(buf
, "pflash")) {
2357 } else if (!strcmp(buf
, "mtd")) {
2360 } else if (!strcmp(buf
, "sd")) {
2363 } else if (!strcmp(buf
, "virtio")) {
2367 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2372 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2373 index
= strtol(buf
, NULL
, 0);
2375 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2380 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2381 cyls
= strtol(buf
, NULL
, 0);
2384 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2385 heads
= strtol(buf
, NULL
, 0);
2388 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2389 secs
= strtol(buf
, NULL
, 0);
2392 if (cyls
|| heads
|| secs
) {
2393 if (cyls
< 1 || cyls
> 16383) {
2394 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2397 if (heads
< 1 || heads
> 16) {
2398 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2401 if (secs
< 1 || secs
> 63) {
2402 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2407 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2410 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2414 if (!strcmp(buf
, "none"))
2415 translation
= BIOS_ATA_TRANSLATION_NONE
;
2416 else if (!strcmp(buf
, "lba"))
2417 translation
= BIOS_ATA_TRANSLATION_LBA
;
2418 else if (!strcmp(buf
, "auto"))
2419 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2421 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2426 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2427 if (!strcmp(buf
, "disk")) {
2429 } else if (!strcmp(buf
, "cdrom")) {
2430 if (cyls
|| secs
|| heads
) {
2432 "qemu: '%s' invalid physical CHS format\n", str
);
2435 media
= MEDIA_CDROM
;
2437 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2442 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2443 if (!strcmp(buf
, "on"))
2445 else if (!strcmp(buf
, "off"))
2448 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2453 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2454 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2456 else if (!strcmp(buf
, "writethrough"))
2458 else if (!strcmp(buf
, "writeback"))
2461 fprintf(stderr
, "qemu: invalid cache option\n");
2466 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2467 if (strcmp(buf
, "?") == 0) {
2468 fprintf(stderr
, "qemu: Supported formats:");
2469 bdrv_iterate_format(bdrv_format_print
, NULL
);
2470 fprintf(stderr
, "\n");
2473 drv
= bdrv_find_format(buf
);
2475 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2480 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2481 if (!strcmp(buf
, "on")) {
2482 if (extboot_drive
!= -1) {
2483 fprintf(stderr
, "qemu: two bootable drives specified\n");
2486 extboot_drive
= nb_drives
;
2487 } else if (strcmp(buf
, "off")) {
2488 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2493 if (arg
->file
== NULL
)
2494 get_param_value(file
, sizeof(file
), "file", str
);
2496 pstrcpy(file
, sizeof(file
), arg
->file
);
2498 /* compute bus and unit according index */
2501 if (bus_id
!= 0 || unit_id
!= -1) {
2503 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2511 unit_id
= index
% max_devs
;
2512 bus_id
= index
/ max_devs
;
2516 /* if user doesn't specify a unit_id,
2517 * try to find the first free
2520 if (unit_id
== -1) {
2522 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2524 if (max_devs
&& unit_id
>= max_devs
) {
2525 unit_id
-= max_devs
;
2533 if (max_devs
&& unit_id
>= max_devs
) {
2534 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2535 str
, unit_id
, max_devs
- 1);
2540 * ignore multiple definitions
2543 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2548 if (type
== IF_IDE
|| type
== IF_SCSI
)
2549 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2551 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2552 devname
, bus_id
, mediastr
, unit_id
);
2554 snprintf(buf
, sizeof(buf
), "%s%s%i",
2555 devname
, mediastr
, unit_id
);
2556 bdrv
= bdrv_new(buf
);
2557 drives_table_idx
= drive_get_free_idx();
2558 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2559 drives_table
[drives_table_idx
].type
= type
;
2560 drives_table
[drives_table_idx
].bus
= bus_id
;
2561 drives_table
[drives_table_idx
].unit
= unit_id
;
2562 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2571 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2572 bdrv_set_translation_hint(bdrv
, translation
);
2576 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2581 /* FIXME: This isn't really a floppy, but it's a reasonable
2584 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2595 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2596 cache
= 2; /* always use write-back with snapshot */
2598 if (cache
== 0) /* no caching */
2599 bdrv_flags
|= BDRV_O_NOCACHE
;
2600 else if (cache
== 2) /* write-back */
2601 bdrv_flags
|= BDRV_O_CACHE_WB
;
2602 else if (cache
== 3) /* not specified */
2603 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2604 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2605 fprintf(stderr
, "qemu: could not open disk image %s\n",
2609 return drives_table_idx
;
2612 /***********************************************************/
2615 static USBPort
*used_usb_ports
;
2616 static USBPort
*free_usb_ports
;
2618 /* ??? Maybe change this to register a hub to keep track of the topology. */
2619 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2620 usb_attachfn attach
)
2622 port
->opaque
= opaque
;
2623 port
->index
= index
;
2624 port
->attach
= attach
;
2625 port
->next
= free_usb_ports
;
2626 free_usb_ports
= port
;
2629 int usb_device_add_dev(USBDevice
*dev
)
2633 /* Find a USB port to add the device to. */
2634 port
= free_usb_ports
;
2638 /* Create a new hub and chain it on. */
2639 free_usb_ports
= NULL
;
2640 port
->next
= used_usb_ports
;
2641 used_usb_ports
= port
;
2643 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2644 usb_attach(port
, hub
);
2645 port
= free_usb_ports
;
2648 free_usb_ports
= port
->next
;
2649 port
->next
= used_usb_ports
;
2650 used_usb_ports
= port
;
2651 usb_attach(port
, dev
);
2655 static int usb_device_add(const char *devname
)
2660 if (!free_usb_ports
)
2663 if (strstart(devname
, "host:", &p
)) {
2664 dev
= usb_host_device_open(p
);
2665 } else if (!strcmp(devname
, "mouse")) {
2666 dev
= usb_mouse_init();
2667 } else if (!strcmp(devname
, "tablet")) {
2668 dev
= usb_tablet_init();
2669 } else if (!strcmp(devname
, "keyboard")) {
2670 dev
= usb_keyboard_init();
2671 } else if (strstart(devname
, "disk:", &p
)) {
2672 dev
= usb_msd_init(p
);
2673 } else if (!strcmp(devname
, "wacom-tablet")) {
2674 dev
= usb_wacom_init();
2675 } else if (strstart(devname
, "serial:", &p
)) {
2676 dev
= usb_serial_init(p
);
2677 #ifdef CONFIG_BRLAPI
2678 } else if (!strcmp(devname
, "braille")) {
2679 dev
= usb_baum_init();
2681 } else if (strstart(devname
, "net:", &p
)) {
2684 if (net_client_init("nic", p
) < 0)
2686 nd_table
[nic
].model
= "usb";
2687 dev
= usb_net_init(&nd_table
[nic
]);
2688 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2689 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2690 bt_new_hci(qemu_find_bt_vlan(0)));
2697 return usb_device_add_dev(dev
);
2700 int usb_device_del_addr(int bus_num
, int addr
)
2706 if (!used_usb_ports
)
2712 lastp
= &used_usb_ports
;
2713 port
= used_usb_ports
;
2714 while (port
&& port
->dev
->addr
!= addr
) {
2715 lastp
= &port
->next
;
2723 *lastp
= port
->next
;
2724 usb_attach(port
, NULL
);
2725 dev
->handle_destroy(dev
);
2726 port
->next
= free_usb_ports
;
2727 free_usb_ports
= port
;
2731 static int usb_device_del(const char *devname
)
2736 if (strstart(devname
, "host:", &p
))
2737 return usb_host_device_close(p
);
2739 if (!used_usb_ports
)
2742 p
= strchr(devname
, '.');
2745 bus_num
= strtoul(devname
, NULL
, 0);
2746 addr
= strtoul(p
+ 1, NULL
, 0);
2748 return usb_device_del_addr(bus_num
, addr
);
2751 void do_usb_add(const char *devname
)
2753 usb_device_add(devname
);
2756 void do_usb_del(const char *devname
)
2758 usb_device_del(devname
);
2765 const char *speed_str
;
2768 term_printf("USB support not enabled\n");
2772 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2776 switch(dev
->speed
) {
2780 case USB_SPEED_FULL
:
2783 case USB_SPEED_HIGH
:
2790 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2791 0, dev
->addr
, speed_str
, dev
->devname
);
2795 /***********************************************************/
2796 /* PCMCIA/Cardbus */
2798 static struct pcmcia_socket_entry_s
{
2799 struct pcmcia_socket_s
*socket
;
2800 struct pcmcia_socket_entry_s
*next
;
2801 } *pcmcia_sockets
= 0;
2803 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2805 struct pcmcia_socket_entry_s
*entry
;
2807 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2808 entry
->socket
= socket
;
2809 entry
->next
= pcmcia_sockets
;
2810 pcmcia_sockets
= entry
;
2813 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2815 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2817 ptr
= &pcmcia_sockets
;
2818 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2819 if (entry
->socket
== socket
) {
2825 void pcmcia_info(void)
2827 struct pcmcia_socket_entry_s
*iter
;
2828 if (!pcmcia_sockets
)
2829 term_printf("No PCMCIA sockets\n");
2831 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2832 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2833 iter
->socket
->attached
? iter
->socket
->card_string
:
2837 /***********************************************************/
2840 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2844 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2848 static void dumb_display_init(DisplayState
*ds
)
2853 ds
->dpy_update
= dumb_update
;
2854 ds
->dpy_resize
= dumb_resize
;
2855 ds
->dpy_refresh
= NULL
;
2856 ds
->gui_timer_interval
= 0;
2860 /***********************************************************/
2863 #define MAX_IO_HANDLERS 64
2865 typedef struct IOHandlerRecord
{
2867 IOCanRWHandler
*fd_read_poll
;
2869 IOHandler
*fd_write
;
2872 /* temporary data */
2874 struct IOHandlerRecord
*next
;
2877 static IOHandlerRecord
*first_io_handler
;
2879 /* XXX: fd_read_poll should be suppressed, but an API change is
2880 necessary in the character devices to suppress fd_can_read(). */
2881 int qemu_set_fd_handler2(int fd
,
2882 IOCanRWHandler
*fd_read_poll
,
2884 IOHandler
*fd_write
,
2887 IOHandlerRecord
**pioh
, *ioh
;
2889 if (!fd_read
&& !fd_write
) {
2890 pioh
= &first_io_handler
;
2895 if (ioh
->fd
== fd
) {
2902 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2906 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2909 ioh
->next
= first_io_handler
;
2910 first_io_handler
= ioh
;
2913 ioh
->fd_read_poll
= fd_read_poll
;
2914 ioh
->fd_read
= fd_read
;
2915 ioh
->fd_write
= fd_write
;
2916 ioh
->opaque
= opaque
;
2923 int qemu_set_fd_handler(int fd
,
2925 IOHandler
*fd_write
,
2928 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2932 /***********************************************************/
2933 /* Polling handling */
2935 typedef struct PollingEntry
{
2938 struct PollingEntry
*next
;
2941 static PollingEntry
*first_polling_entry
;
2943 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2945 PollingEntry
**ppe
, *pe
;
2946 pe
= qemu_mallocz(sizeof(PollingEntry
));
2950 pe
->opaque
= opaque
;
2951 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2956 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2958 PollingEntry
**ppe
, *pe
;
2959 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2961 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2969 /***********************************************************/
2970 /* Wait objects support */
2971 typedef struct WaitObjects
{
2973 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2974 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2975 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2978 static WaitObjects wait_objects
= {0};
2980 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2982 WaitObjects
*w
= &wait_objects
;
2984 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2986 w
->events
[w
->num
] = handle
;
2987 w
->func
[w
->num
] = func
;
2988 w
->opaque
[w
->num
] = opaque
;
2993 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2996 WaitObjects
*w
= &wait_objects
;
2999 for (i
= 0; i
< w
->num
; i
++) {
3000 if (w
->events
[i
] == handle
)
3003 w
->events
[i
] = w
->events
[i
+ 1];
3004 w
->func
[i
] = w
->func
[i
+ 1];
3005 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3013 /***********************************************************/
3014 /* ram save/restore */
3016 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3020 v
= qemu_get_byte(f
);
3023 if (qemu_get_buffer(f
, buf
, len
) != len
)
3027 v
= qemu_get_byte(f
);
3028 memset(buf
, v
, len
);
3034 if (qemu_file_has_error(f
))
3040 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3045 if (qemu_get_be32(f
) != phys_ram_size
)
3047 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3048 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3050 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3057 #define BDRV_HASH_BLOCK_SIZE 1024
3058 #define IOBUF_SIZE 4096
3059 #define RAM_CBLOCK_MAGIC 0xfabe
3061 typedef struct RamDecompressState
{
3064 uint8_t buf
[IOBUF_SIZE
];
3065 } RamDecompressState
;
3067 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3070 memset(s
, 0, sizeof(*s
));
3072 ret
= inflateInit(&s
->zstream
);
3078 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3082 s
->zstream
.avail_out
= len
;
3083 s
->zstream
.next_out
= buf
;
3084 while (s
->zstream
.avail_out
> 0) {
3085 if (s
->zstream
.avail_in
== 0) {
3086 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3088 clen
= qemu_get_be16(s
->f
);
3089 if (clen
> IOBUF_SIZE
)
3091 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3092 s
->zstream
.avail_in
= clen
;
3093 s
->zstream
.next_in
= s
->buf
;
3095 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3096 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3103 static void ram_decompress_close(RamDecompressState
*s
)
3105 inflateEnd(&s
->zstream
);
3108 #define RAM_SAVE_FLAG_FULL 0x01
3109 #define RAM_SAVE_FLAG_COMPRESS 0x02
3110 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3111 #define RAM_SAVE_FLAG_PAGE 0x08
3112 #define RAM_SAVE_FLAG_EOS 0x10
3114 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3116 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3117 uint32_t *array
= (uint32_t *)page
;
3120 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3121 if (array
[i
] != val
)
3128 static int ram_save_block(QEMUFile
*f
)
3130 static ram_addr_t current_addr
= 0;
3131 ram_addr_t saved_addr
= current_addr
;
3132 ram_addr_t addr
= 0;
3135 while (addr
< phys_ram_size
) {
3136 if (kvm_enabled() && current_addr
== 0)
3137 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3138 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3141 cpu_physical_memory_reset_dirty(current_addr
,
3142 current_addr
+ TARGET_PAGE_SIZE
,
3143 MIGRATION_DIRTY_FLAG
);
3145 ch
= *(phys_ram_base
+ current_addr
);
3147 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3148 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3149 qemu_put_byte(f
, ch
);
3151 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3152 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3158 addr
+= TARGET_PAGE_SIZE
;
3159 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3165 static ram_addr_t ram_save_threshold
= 10;
3167 static ram_addr_t
ram_save_remaining(void)
3170 ram_addr_t count
= 0;
3172 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3173 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3180 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3185 /* Make sure all dirty bits are set */
3186 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3187 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3188 cpu_physical_memory_set_dirty(addr
);
3191 /* Enable dirty memory tracking */
3192 cpu_physical_memory_set_dirty_tracking(1);
3194 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3197 while (!qemu_file_rate_limit(f
)) {
3200 ret
= ram_save_block(f
);
3201 if (ret
== 0) /* no more blocks */
3205 /* try transferring iterative blocks of memory */
3208 cpu_physical_memory_set_dirty_tracking(0);
3210 /* flush all remaining blocks regardless of rate limiting */
3211 while (ram_save_block(f
) != 0);
3214 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3216 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3219 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3221 RamDecompressState s1
, *s
= &s1
;
3225 if (ram_decompress_open(s
, f
) < 0)
3227 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3228 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3230 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3231 fprintf(stderr
, "Error while reading ram block header\n");
3235 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3236 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3241 printf("Error block header\n");
3245 ram_decompress_close(s
);
3250 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3255 if (version_id
== 1)
3256 return ram_load_v1(f
, opaque
);
3258 if (version_id
== 2) {
3259 if (qemu_get_be32(f
) != phys_ram_size
)
3261 return ram_load_dead(f
, opaque
);
3264 if (version_id
!= 3)
3268 addr
= qemu_get_be64(f
);
3270 flags
= addr
& ~TARGET_PAGE_MASK
;
3271 addr
&= TARGET_PAGE_MASK
;
3273 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3274 if (addr
!= phys_ram_size
)
3278 if (flags
& RAM_SAVE_FLAG_FULL
) {
3279 if (ram_load_dead(f
, opaque
) < 0)
3283 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3284 uint8_t ch
= qemu_get_byte(f
);
3285 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3286 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3287 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3288 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3293 /***********************************************************/
3294 /* bottom halves (can be seen as timers which expire ASAP) */
3305 static QEMUBH
*first_bh
= NULL
;
3307 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3310 bh
= qemu_mallocz(sizeof(QEMUBH
));
3314 bh
->opaque
= opaque
;
3315 bh
->next
= first_bh
;
3320 int qemu_bh_poll(void)
3326 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3327 if (!bh
->deleted
&& bh
->scheduled
) {
3336 /* remove deleted bhs */
3350 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3358 void qemu_bh_schedule(QEMUBH
*bh
)
3360 CPUState
*env
= cpu_single_env
;
3365 /* stop the currently executing CPU to execute the BH ASAP */
3367 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3372 void qemu_bh_cancel(QEMUBH
*bh
)
3377 void qemu_bh_delete(QEMUBH
*bh
)
3383 static void qemu_bh_update_timeout(int *timeout
)
3387 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3388 if (!bh
->deleted
&& bh
->scheduled
) {
3390 /* idle bottom halves will be polled at least
3392 *timeout
= MIN(10, *timeout
);
3394 /* non-idle bottom halves will be executed
3403 /***********************************************************/
3404 /* machine registration */
3406 static QEMUMachine
*first_machine
= NULL
;
3407 QEMUMachine
*current_machine
= NULL
;
3409 int qemu_register_machine(QEMUMachine
*m
)
3412 pm
= &first_machine
;
3420 static QEMUMachine
*find_machine(const char *name
)
3424 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3425 if (!strcmp(m
->name
, name
))
3431 /***********************************************************/
3432 /* main execution loop */
3434 static void gui_update(void *opaque
)
3436 DisplayState
*ds
= opaque
;
3437 ds
->dpy_refresh(ds
);
3438 qemu_mod_timer(ds
->gui_timer
,
3439 (ds
->gui_timer_interval
?
3440 ds
->gui_timer_interval
:
3441 GUI_REFRESH_INTERVAL
)
3442 + qemu_get_clock(rt_clock
));
3445 struct vm_change_state_entry
{
3446 VMChangeStateHandler
*cb
;
3448 LIST_ENTRY (vm_change_state_entry
) entries
;
3451 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3453 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3456 VMChangeStateEntry
*e
;
3458 e
= qemu_mallocz(sizeof (*e
));
3464 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3468 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3470 LIST_REMOVE (e
, entries
);
3474 static void vm_state_notify(int running
)
3476 VMChangeStateEntry
*e
;
3478 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3479 e
->cb(e
->opaque
, running
);
3483 /* XXX: support several handlers */
3484 static VMStopHandler
*vm_stop_cb
;
3485 static void *vm_stop_opaque
;
3487 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3490 vm_stop_opaque
= opaque
;
3494 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3505 qemu_rearm_alarm_timer(alarm_timer
);
3509 void vm_stop(int reason
)
3512 cpu_disable_ticks();
3516 vm_stop_cb(vm_stop_opaque
, reason
);
3523 /* reset/shutdown handler */
3525 typedef struct QEMUResetEntry
{
3526 QEMUResetHandler
*func
;
3528 struct QEMUResetEntry
*next
;
3531 static QEMUResetEntry
*first_reset_entry
;
3532 static int reset_requested
;
3533 static int shutdown_requested
;
3534 static int powerdown_requested
;
3536 int qemu_shutdown_requested(void)
3538 int r
= shutdown_requested
;
3539 shutdown_requested
= 0;
3543 int qemu_reset_requested(void)
3545 int r
= reset_requested
;
3546 reset_requested
= 0;
3550 int qemu_powerdown_requested(void)
3552 int r
= powerdown_requested
;
3553 powerdown_requested
= 0;
3557 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3559 QEMUResetEntry
**pre
, *re
;
3561 pre
= &first_reset_entry
;
3562 while (*pre
!= NULL
)
3563 pre
= &(*pre
)->next
;
3564 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3566 re
->opaque
= opaque
;
3571 void qemu_system_reset(void)
3575 /* reset all devices */
3576 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3577 re
->func(re
->opaque
);
3581 void qemu_system_reset_request(void)
3584 shutdown_requested
= 1;
3586 reset_requested
= 1;
3589 if (cpu_single_env
) {
3590 qemu_kvm_cpu_stop(cpu_single_env
);
3591 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3596 void qemu_system_shutdown_request(void)
3598 shutdown_requested
= 1;
3600 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3603 void qemu_system_powerdown_request(void)
3605 powerdown_requested
= 1;
3607 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3610 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3615 /* KVM holds a mutex while QEMU code is running, we need hooks to
3616 release the mutex whenever QEMU code sleeps. */
3620 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3628 static void host_main_loop_wait(int *timeout
)
3634 /* XXX: need to suppress polling by better using win32 events */
3636 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3637 ret
|= pe
->func(pe
->opaque
);
3641 WaitObjects
*w
= &wait_objects
;
3643 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3644 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3645 if (w
->func
[ret
- WAIT_OBJECT_0
])
3646 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3648 /* Check for additional signaled events */
3649 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3651 /* Check if event is signaled */
3652 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3653 if(ret2
== WAIT_OBJECT_0
) {
3655 w
->func
[i
](w
->opaque
[i
]);
3656 } else if (ret2
== WAIT_TIMEOUT
) {
3658 err
= GetLastError();
3659 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3662 } else if (ret
== WAIT_TIMEOUT
) {
3664 err
= GetLastError();
3665 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3672 static void host_main_loop_wait(int *timeout
)
3677 void main_loop_wait(int timeout
)
3679 IOHandlerRecord
*ioh
;
3680 fd_set rfds
, wfds
, xfds
;
3684 qemu_bh_update_timeout(&timeout
);
3686 host_main_loop_wait(&timeout
);
3688 /* poll any events */
3689 /* XXX: separate device handlers from system ones */
3694 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3698 (!ioh
->fd_read_poll
||
3699 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3700 FD_SET(ioh
->fd
, &rfds
);
3704 if (ioh
->fd_write
) {
3705 FD_SET(ioh
->fd
, &wfds
);
3711 tv
.tv_sec
= timeout
/ 1000;
3712 tv
.tv_usec
= (timeout
% 1000) * 1000;
3714 #if defined(CONFIG_SLIRP)
3715 if (slirp_is_inited()) {
3716 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3719 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3721 IOHandlerRecord
**pioh
;
3723 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3724 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3725 ioh
->fd_read(ioh
->opaque
);
3726 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3727 FD_CLR(ioh
->fd
, &rfds
);
3729 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3730 ioh
->fd_write(ioh
->opaque
);
3734 /* remove deleted IO handlers */
3735 pioh
= &first_io_handler
;
3745 #if defined(CONFIG_SLIRP)
3746 if (slirp_is_inited()) {
3752 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3756 /* vm time timers */
3757 if (vm_running
&& (!cur_cpu
3758 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3759 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3760 qemu_get_clock(vm_clock
));
3762 /* real time timers */
3763 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3764 qemu_get_clock(rt_clock
));
3766 /* Check bottom-halves last in case any of the earlier events triggered
3772 static int main_loop(void)
3775 #ifdef CONFIG_PROFILER
3781 if (kvm_enabled()) {
3783 cpu_disable_ticks();
3787 cur_cpu
= first_cpu
;
3788 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3795 #ifdef CONFIG_PROFILER
3796 ti
= profile_getclock();
3801 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3802 env
->icount_decr
.u16
.low
= 0;
3803 env
->icount_extra
= 0;
3804 count
= qemu_next_deadline();
3805 count
= (count
+ (1 << icount_time_shift
) - 1)
3806 >> icount_time_shift
;
3807 qemu_icount
+= count
;
3808 decr
= (count
> 0xffff) ? 0xffff : count
;
3810 env
->icount_decr
.u16
.low
= decr
;
3811 env
->icount_extra
= count
;
3813 ret
= cpu_exec(env
);
3814 #ifdef CONFIG_PROFILER
3815 qemu_time
+= profile_getclock() - ti
;
3818 /* Fold pending instructions back into the
3819 instruction counter, and clear the interrupt flag. */
3820 qemu_icount
-= (env
->icount_decr
.u16
.low
3821 + env
->icount_extra
);
3822 env
->icount_decr
.u32
= 0;
3823 env
->icount_extra
= 0;
3825 next_cpu
= env
->next_cpu
?: first_cpu
;
3826 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3827 ret
= EXCP_INTERRUPT
;
3831 if (ret
== EXCP_HLT
) {
3832 /* Give the next CPU a chance to run. */
3836 if (ret
!= EXCP_HALTED
)
3838 /* all CPUs are halted ? */
3844 if (shutdown_requested
) {
3845 ret
= EXCP_INTERRUPT
;
3853 if (reset_requested
) {
3854 reset_requested
= 0;
3855 qemu_system_reset();
3857 kvm_load_registers(env
);
3858 ret
= EXCP_INTERRUPT
;
3860 if (powerdown_requested
) {
3861 powerdown_requested
= 0;
3862 qemu_system_powerdown();
3863 ret
= EXCP_INTERRUPT
;
3865 #ifdef CONFIG_GDBSTUB
3866 if (unlikely(ret
== EXCP_DEBUG
)) {
3867 gdb_set_stop_cpu(cur_cpu
);
3868 vm_stop(EXCP_DEBUG
);
3871 /* If all cpus are halted then wait until the next IRQ */
3872 /* XXX: use timeout computed from timers */
3873 if (ret
== EXCP_HALTED
) {
3877 /* Advance virtual time to the next event. */
3878 if (use_icount
== 1) {
3879 /* When not using an adaptive execution frequency
3880 we tend to get badly out of sync with real time,
3881 so just delay for a reasonable amount of time. */
3884 delta
= cpu_get_icount() - cpu_get_clock();
3887 /* If virtual time is ahead of real time then just
3889 timeout
= (delta
/ 1000000) + 1;
3891 /* Wait for either IO to occur or the next
3893 add
= qemu_next_deadline();
3894 /* We advance the timer before checking for IO.
3895 Limit the amount we advance so that early IO
3896 activity won't get the guest too far ahead. */
3900 add
= (add
+ (1 << icount_time_shift
) - 1)
3901 >> icount_time_shift
;
3903 timeout
= delta
/ 1000000;
3914 if (shutdown_requested
) {
3915 ret
= EXCP_INTERRUPT
;
3920 #ifdef CONFIG_PROFILER
3921 ti
= profile_getclock();
3923 main_loop_wait(timeout
);
3924 #ifdef CONFIG_PROFILER
3925 dev_time
+= profile_getclock() - ti
;
3928 cpu_disable_ticks();
3932 static void help(int exitcode
)
3934 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3935 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3936 "usage: %s [options] [disk_image]\n"
3938 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3940 "Standard options:\n"
3941 "-M machine select emulated machine (-M ? for list)\n"
3942 "-cpu cpu select CPU (-cpu ? for list)\n"
3943 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3944 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3945 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3946 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3947 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3948 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3949 " [,cache=writethrough|writeback|none][,format=f]\n"
3951 " use 'file' as a drive image\n"
3952 "-mtdblock file use 'file' as on-board Flash memory image\n"
3953 "-sd file use 'file' as SecureDigital card image\n"
3954 "-pflash file use 'file' as a parallel flash image\n"
3955 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3956 "-snapshot write to temporary files instead of disk image files\n"
3958 "-no-frame open SDL window without a frame and window decorations\n"
3959 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3960 "-no-quit disable SDL window close capability\n"
3963 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3965 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3966 "-smp n set the number of CPUs to 'n' [default=1]\n"
3967 "-nographic disable graphical output and redirect serial I/Os to console\n"
3968 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3970 "-k language use keyboard layout (for example \"fr\" for French)\n"
3973 "-audio-help print list of audio drivers and their options\n"
3974 "-soundhw c1,... enable audio support\n"
3975 " and only specified sound cards (comma separated list)\n"
3976 " use -soundhw ? to get the list of supported cards\n"
3977 " use -soundhw all to enable all of them\n"
3979 "-vga [std|cirrus|vmware]\n"
3980 " select video card type\n"
3981 "-localtime set the real time clock to local time [default=utc]\n"
3982 "-full-screen start in full screen\n"
3984 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3986 "-usb enable the USB driver (will be the default soon)\n"
3987 "-usbdevice name add the host or guest USB device 'name'\n"
3988 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3989 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3991 "-name string set the name of the guest\n"
3992 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3994 "Network options:\n"
3995 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
3996 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3998 "-net user[,vlan=n][,hostname=host]\n"
3999 " connect the user mode network stack to VLAN 'n' and send\n"
4000 " hostname 'host' to DHCP clients\n"
4003 "-net tap[,vlan=n],ifname=name\n"
4004 " connect the host TAP network interface to VLAN 'n'\n"
4006 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4007 " connect the host TAP network interface to VLAN 'n' and use the\n"
4008 " network scripts 'file' (default=%s)\n"
4009 " and 'dfile' (default=%s);\n"
4010 " use '[down]script=no' to disable script execution;\n"
4011 " use 'fd=h' to connect to an already opened TAP interface\n"
4013 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4014 " connect the vlan 'n' to another VLAN using a socket connection\n"
4015 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4016 " connect the vlan 'n' to multicast maddr and port\n"
4018 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4019 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4020 " on host and listening for incoming connections on 'socketpath'.\n"
4021 " Use group 'groupname' and mode 'octalmode' to change default\n"
4022 " ownership and permissions for communication port.\n"
4024 "-net none use it alone to have zero network devices; if no -net option\n"
4025 " is provided, the default is '-net nic -net user'\n"
4027 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
4028 "-bt hci,host[:id]\n"
4029 " Use host's HCI with the given name\n"
4030 "-bt hci[,vlan=n]\n"
4031 " Emulate a standard HCI in virtual scatternet 'n'\n"
4032 "-bt vhci[,vlan=n]\n"
4033 " Add host computer to virtual scatternet 'n' using VHCI\n"
4034 "-bt device:dev[,vlan=n]\n"
4035 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
4038 "-tftp dir allow tftp access to files in dir [-net user]\n"
4039 "-bootp file advertise file in BOOTP replies\n"
4041 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4043 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4044 " redirect TCP or UDP connections from host to guest [-net user]\n"
4047 "Linux boot specific:\n"
4048 "-kernel bzImage use 'bzImage' as kernel image\n"
4049 "-append cmdline use 'cmdline' as kernel command line\n"
4050 "-initrd file use 'file' as initial ram disk\n"
4052 "Debug/Expert options:\n"
4053 "-monitor dev redirect the monitor to char device 'dev'\n"
4054 "-serial dev redirect the serial port to char device 'dev'\n"
4055 "-parallel dev redirect the parallel port to char device 'dev'\n"
4056 "-pidfile file Write PID to 'file'\n"
4057 "-S freeze CPU at startup (use 'c' to start execution)\n"
4058 "-s wait gdb connection to port\n"
4059 "-p port set gdb connection port [default=%s]\n"
4060 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4061 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4062 " translation (t=none or lba) (usually qemu can guess them)\n"
4063 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4065 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4066 "-no-kqemu disable KQEMU kernel module usage\n"
4069 "-enable-kvm enable KVM full virtualization support\n"
4072 #ifndef NO_CPU_EMULATION
4073 "-no-kvm disable KVM hardware virtualization\n"
4075 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4076 "-no-kvm-pit disable KVM kernel mode PIT\n"
4077 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4078 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4079 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4080 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4081 " expose a PCI device to the guest OS.\n"
4082 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4083 " 'string' is used in log output.\n"
4087 "-no-acpi disable ACPI\n"
4088 "-no-hpet disable HPET\n"
4090 #ifdef CONFIG_CURSES
4091 "-curses use a curses/ncurses interface instead of SDL\n"
4093 "-no-reboot exit instead of rebooting\n"
4094 "-no-shutdown stop before shutdown\n"
4095 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4096 "-vnc display start a VNC server on display\n"
4098 "-daemonize daemonize QEMU after initializing\n"
4100 "-tdf inject timer interrupts that got lost\n"
4101 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4102 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also enables allocation of guest memory with huge pages\n"
4103 "-option-rom rom load a file, rom, into the option ROM space\n"
4105 "-prom-env variable=value set OpenBIOS nvram variables\n"
4107 "-clock force the use of the given methods for timer alarm.\n"
4108 " To see what timers are available use -clock ?\n"
4109 "-startdate select initial date of the clock\n"
4110 "-icount [N|auto]\n"
4111 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4113 "During emulation, the following keys are useful:\n"
4114 "ctrl-alt-f toggle full screen\n"
4115 "ctrl-alt-n switch to virtual console 'n'\n"
4116 "ctrl-alt toggle mouse and keyboard grab\n"
4118 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4123 DEFAULT_NETWORK_SCRIPT
,
4124 DEFAULT_NETWORK_DOWN_SCRIPT
,
4126 DEFAULT_GDBSTUB_PORT
,
4131 #define HAS_ARG 0x0001
4146 QEMU_OPTION_mtdblock
,
4150 QEMU_OPTION_snapshot
,
4152 QEMU_OPTION_no_fd_bootchk
,
4155 QEMU_OPTION_nographic
,
4156 QEMU_OPTION_portrait
,
4158 QEMU_OPTION_audio_help
,
4159 QEMU_OPTION_soundhw
,
4181 QEMU_OPTION_localtime
,
4185 QEMU_OPTION_monitor
,
4187 QEMU_OPTION_parallel
,
4189 QEMU_OPTION_full_screen
,
4190 QEMU_OPTION_no_frame
,
4191 QEMU_OPTION_alt_grab
,
4192 QEMU_OPTION_no_quit
,
4193 QEMU_OPTION_pidfile
,
4194 QEMU_OPTION_no_kqemu
,
4195 QEMU_OPTION_kernel_kqemu
,
4196 QEMU_OPTION_enable_kvm
,
4197 QEMU_OPTION_enable_nesting
,
4198 QEMU_OPTION_win2k_hack
,
4200 QEMU_OPTION_usbdevice
,
4203 QEMU_OPTION_no_acpi
,
4204 QEMU_OPTION_no_hpet
,
4207 QEMU_OPTION_no_kvm_irqchip
,
4208 QEMU_OPTION_no_kvm_pit
,
4209 QEMU_OPTION_no_kvm_pit_reinjection
,
4210 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4211 QEMU_OPTION_pcidevice
,
4213 QEMU_OPTION_no_reboot
,
4214 QEMU_OPTION_no_shutdown
,
4215 QEMU_OPTION_show_cursor
,
4216 QEMU_OPTION_daemonize
,
4217 QEMU_OPTION_option_rom
,
4218 QEMU_OPTION_semihosting
,
4219 QEMU_OPTION_cpu_vendor
,
4221 QEMU_OPTION_prom_env
,
4222 QEMU_OPTION_old_param
,
4224 QEMU_OPTION_startdate
,
4225 QEMU_OPTION_tb_size
,
4228 QEMU_OPTION_incoming
,
4230 QEMU_OPTION_kvm_shadow_memory
,
4231 QEMU_OPTION_mempath
,
4234 typedef struct QEMUOption
{
4240 static const QEMUOption qemu_options
[] = {
4241 { "h", 0, QEMU_OPTION_h
},
4242 { "help", 0, QEMU_OPTION_h
},
4244 { "M", HAS_ARG
, QEMU_OPTION_M
},
4245 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4246 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4247 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4248 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4249 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4250 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4251 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4252 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4253 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4254 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4255 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4256 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4257 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4258 { "snapshot", 0, QEMU_OPTION_snapshot
},
4260 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4262 { "m", HAS_ARG
, QEMU_OPTION_m
},
4263 { "nographic", 0, QEMU_OPTION_nographic
},
4264 { "portrait", 0, QEMU_OPTION_portrait
},
4265 { "k", HAS_ARG
, QEMU_OPTION_k
},
4267 { "audio-help", 0, QEMU_OPTION_audio_help
},
4268 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4271 { "net", HAS_ARG
, QEMU_OPTION_net
},
4273 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4274 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4276 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4278 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4280 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4282 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4283 { "append", HAS_ARG
, QEMU_OPTION_append
},
4284 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4286 { "S", 0, QEMU_OPTION_S
},
4287 { "s", 0, QEMU_OPTION_s
},
4288 { "p", HAS_ARG
, QEMU_OPTION_p
},
4289 { "d", HAS_ARG
, QEMU_OPTION_d
},
4290 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4291 { "L", HAS_ARG
, QEMU_OPTION_L
},
4292 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4294 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4295 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4298 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4301 #ifndef NO_CPU_EMULATION
4302 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4304 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4305 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4306 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4307 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4308 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4309 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4312 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4313 { "g", 1, QEMU_OPTION_g
},
4315 { "localtime", 0, QEMU_OPTION_localtime
},
4316 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4317 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4318 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4319 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4320 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4321 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4322 { "incoming", 1, QEMU_OPTION_incoming
},
4323 { "full-screen", 0, QEMU_OPTION_full_screen
},
4325 { "no-frame", 0, QEMU_OPTION_no_frame
},
4326 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4327 { "no-quit", 0, QEMU_OPTION_no_quit
},
4329 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4330 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4331 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4332 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4333 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4334 #ifdef CONFIG_CURSES
4335 { "curses", 0, QEMU_OPTION_curses
},
4337 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4339 /* temporary options */
4340 { "usb", 0, QEMU_OPTION_usb
},
4341 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4342 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4343 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4344 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4345 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4346 { "daemonize", 0, QEMU_OPTION_daemonize
},
4347 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4348 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4349 { "semihosting", 0, QEMU_OPTION_semihosting
},
4351 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4352 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4353 { "name", HAS_ARG
, QEMU_OPTION_name
},
4354 #if defined(TARGET_SPARC)
4355 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4357 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4358 #if defined(TARGET_ARM)
4359 { "old-param", 0, QEMU_OPTION_old_param
},
4361 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4362 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4363 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4364 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4365 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4366 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4370 /* password input */
4372 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4377 if (!bdrv_is_encrypted(bs
))
4380 term_printf("%s is encrypted.\n", name
);
4381 for(i
= 0; i
< 3; i
++) {
4382 monitor_readline("Password: ", 1, password
, sizeof(password
));
4383 if (bdrv_set_key(bs
, password
) == 0)
4385 term_printf("invalid password\n");
4390 static BlockDriverState
*get_bdrv(int index
)
4392 if (index
> nb_drives
)
4394 return drives_table
[index
].bdrv
;
4397 static void read_passwords(void)
4399 BlockDriverState
*bs
;
4402 for(i
= 0; i
< 6; i
++) {
4405 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4410 struct soundhw soundhw
[] = {
4411 #ifdef HAS_AUDIO_CHOICE
4412 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4418 { .init_isa
= pcspk_audio_init
}
4423 "Creative Sound Blaster 16",
4426 { .init_isa
= SB16_init
}
4429 #ifdef CONFIG_CS4231A
4435 { .init_isa
= cs4231a_init
}
4443 "Yamaha YMF262 (OPL3)",
4445 "Yamaha YM3812 (OPL2)",
4449 { .init_isa
= Adlib_init
}
4456 "Gravis Ultrasound GF1",
4459 { .init_isa
= GUS_init
}
4466 "Intel 82801AA AC97 Audio",
4469 { .init_pci
= ac97_init
}
4475 "ENSONIQ AudioPCI ES1370",
4478 { .init_pci
= es1370_init
}
4482 { NULL
, NULL
, 0, 0, { NULL
} }
4485 static void select_soundhw (const char *optarg
)
4489 if (*optarg
== '?') {
4492 printf ("Valid sound card names (comma separated):\n");
4493 for (c
= soundhw
; c
->name
; ++c
) {
4494 printf ("%-11s %s\n", c
->name
, c
->descr
);
4496 printf ("\n-soundhw all will enable all of the above\n");
4497 exit (*optarg
!= '?');
4505 if (!strcmp (optarg
, "all")) {
4506 for (c
= soundhw
; c
->name
; ++c
) {
4514 e
= strchr (p
, ',');
4515 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4517 for (c
= soundhw
; c
->name
; ++c
) {
4518 if (!strncmp (c
->name
, p
, l
)) {
4527 "Unknown sound card name (too big to show)\n");
4530 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4535 p
+= l
+ (e
!= NULL
);
4539 goto show_valid_cards
;
4544 static void select_vgahw (const char *p
)
4548 if (strstart(p
, "std", &opts
)) {
4549 cirrus_vga_enabled
= 0;
4551 } else if (strstart(p
, "cirrus", &opts
)) {
4552 cirrus_vga_enabled
= 1;
4554 } else if (strstart(p
, "vmware", &opts
)) {
4555 cirrus_vga_enabled
= 0;
4559 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4563 const char *nextopt
;
4565 if (strstart(opts
, ",retrace=", &nextopt
)) {
4567 if (strstart(opts
, "dumb", &nextopt
))
4568 vga_retrace_method
= VGA_RETRACE_DUMB
;
4569 else if (strstart(opts
, "precise", &nextopt
))
4570 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4571 else goto invalid_vga
;
4572 } else goto invalid_vga
;
4578 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4580 exit(STATUS_CONTROL_C_EXIT
);
4585 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4589 if(strlen(str
) != 36)
4592 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4593 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4594 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4602 #define MAX_NET_CLIENTS 32
4604 static int saved_argc
;
4605 static char **saved_argv
;
4607 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4611 *opt_daemonize
= daemonize
;
4612 *opt_incoming
= incoming
;
4616 static int gethugepagesize(void)
4620 const char *needle
= "Hugepagesize:";
4622 unsigned long hugepagesize
;
4624 fd
= open("/proc/meminfo", O_RDONLY
);
4630 ret
= read(fd
, buf
, sizeof(buf
));
4636 size
= strstr(buf
, needle
);
4639 size
+= strlen(needle
);
4640 hugepagesize
= strtol(size
, NULL
, 0);
4641 return hugepagesize
;
4644 void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4650 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4653 hpagesize
= gethugepagesize() * 1024;
4657 fd
= mkstemp(filename
);
4666 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4669 * ftruncate is not supported by hugetlbfs in older
4670 * hosts, so don't bother checking for errors.
4671 * If anything goes wrong with it under other filesystems,
4674 ftruncate(fd
, memory
);
4676 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4677 if (area
== MAP_FAILED
) {
4687 void *qemu_alloc_physram(unsigned long memory
)
4690 unsigned long map_len
= memory
;
4694 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4697 area
= qemu_vmalloc(memory
);
4699 if (kvm_setup_guest_memory(area
, map_len
))
4707 static void termsig_handler(int signal
)
4709 qemu_system_shutdown_request();
4712 static void termsig_setup(void)
4714 struct sigaction act
;
4716 memset(&act
, 0, sizeof(act
));
4717 act
.sa_handler
= termsig_handler
;
4718 sigaction(SIGINT
, &act
, NULL
);
4719 sigaction(SIGHUP
, &act
, NULL
);
4720 sigaction(SIGTERM
, &act
, NULL
);
4725 int main(int argc
, char **argv
, char **envp
)
4727 #ifdef CONFIG_GDBSTUB
4729 const char *gdbstub_port
;
4731 uint32_t boot_devices_bitmap
= 0;
4733 int snapshot
, linux_boot
, net_boot
;
4734 const char *initrd_filename
;
4735 const char *kernel_filename
, *kernel_cmdline
;
4736 const char *boot_devices
= "";
4737 DisplayState
*ds
= &display_state
;
4738 int cyls
, heads
, secs
, translation
;
4739 const char *net_clients
[MAX_NET_CLIENTS
];
4741 const char *bt_opts
[MAX_BT_CMDLINE
];
4745 const char *r
, *optarg
;
4746 CharDriverState
*monitor_hd
;
4747 const char *monitor_device
;
4748 const char *serial_devices
[MAX_SERIAL_PORTS
];
4749 int serial_device_index
;
4750 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4751 int parallel_device_index
;
4752 const char *loadvm
= NULL
;
4753 QEMUMachine
*machine
;
4754 const char *cpu_model
;
4755 const char *usb_devices
[MAX_USB_CMDLINE
];
4756 int usb_devices_index
;
4759 const char *pid_file
= NULL
;
4761 const char *incoming
= NULL
;
4763 qemu_cache_utils_init(envp
);
4765 LIST_INIT (&vm_change_state_head
);
4768 struct sigaction act
;
4769 sigfillset(&act
.sa_mask
);
4771 act
.sa_handler
= SIG_IGN
;
4772 sigaction(SIGPIPE
, &act
, NULL
);
4775 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4776 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4777 QEMU to run on a single CPU */
4782 h
= GetCurrentProcess();
4783 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4784 for(i
= 0; i
< 32; i
++) {
4785 if (mask
& (1 << i
))
4790 SetProcessAffinityMask(h
, mask
);
4796 register_machines();
4797 machine
= first_machine
;
4799 initrd_filename
= NULL
;
4801 vga_ram_size
= VGA_RAM_SIZE
;
4802 #ifdef CONFIG_GDBSTUB
4804 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4809 kernel_filename
= NULL
;
4810 kernel_cmdline
= "";
4811 cyls
= heads
= secs
= 0;
4812 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4813 monitor_device
= "vc";
4815 serial_devices
[0] = "vc:80Cx24C";
4816 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4817 serial_devices
[i
] = NULL
;
4818 serial_device_index
= 0;
4820 parallel_devices
[0] = "vc:640x480";
4821 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4822 parallel_devices
[i
] = NULL
;
4823 parallel_device_index
= 0;
4825 usb_devices_index
= 0;
4826 assigned_devices_index
= 0;
4845 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4847 const QEMUOption
*popt
;
4850 /* Treat --foo the same as -foo. */
4853 popt
= qemu_options
;
4856 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4860 if (!strcmp(popt
->name
, r
+ 1))
4864 if (popt
->flags
& HAS_ARG
) {
4865 if (optind
>= argc
) {
4866 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4870 optarg
= argv
[optind
++];
4875 switch(popt
->index
) {
4877 machine
= find_machine(optarg
);
4880 printf("Supported machines are:\n");
4881 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4882 printf("%-10s %s%s\n",
4884 m
== first_machine
? " (default)" : "");
4886 exit(*optarg
!= '?');
4889 case QEMU_OPTION_cpu
:
4890 /* hw initialization will check this */
4891 if (*optarg
== '?') {
4892 /* XXX: implement xxx_cpu_list for targets that still miss it */
4893 #if defined(cpu_list)
4894 cpu_list(stdout
, &fprintf
);
4901 case QEMU_OPTION_initrd
:
4902 initrd_filename
= optarg
;
4904 case QEMU_OPTION_hda
:
4906 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4908 hda_index
= drive_add(optarg
, HD_ALIAS
4909 ",cyls=%d,heads=%d,secs=%d%s",
4910 0, cyls
, heads
, secs
,
4911 translation
== BIOS_ATA_TRANSLATION_LBA
?
4913 translation
== BIOS_ATA_TRANSLATION_NONE
?
4914 ",trans=none" : "");
4916 case QEMU_OPTION_hdb
:
4917 case QEMU_OPTION_hdc
:
4918 case QEMU_OPTION_hdd
:
4919 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4921 case QEMU_OPTION_drive
:
4922 drive_add(NULL
, "%s", optarg
);
4924 case QEMU_OPTION_mtdblock
:
4925 drive_add(optarg
, MTD_ALIAS
);
4927 case QEMU_OPTION_sd
:
4928 drive_add(optarg
, SD_ALIAS
);
4930 case QEMU_OPTION_pflash
:
4931 drive_add(optarg
, PFLASH_ALIAS
);
4933 case QEMU_OPTION_snapshot
:
4936 case QEMU_OPTION_hdachs
:
4940 cyls
= strtol(p
, (char **)&p
, 0);
4941 if (cyls
< 1 || cyls
> 16383)
4946 heads
= strtol(p
, (char **)&p
, 0);
4947 if (heads
< 1 || heads
> 16)
4952 secs
= strtol(p
, (char **)&p
, 0);
4953 if (secs
< 1 || secs
> 63)
4957 if (!strcmp(p
, "none"))
4958 translation
= BIOS_ATA_TRANSLATION_NONE
;
4959 else if (!strcmp(p
, "lba"))
4960 translation
= BIOS_ATA_TRANSLATION_LBA
;
4961 else if (!strcmp(p
, "auto"))
4962 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4965 } else if (*p
!= '\0') {
4967 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4970 if (hda_index
!= -1)
4971 snprintf(drives_opt
[hda_index
].opt
,
4972 sizeof(drives_opt
[hda_index
].opt
),
4973 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4974 0, cyls
, heads
, secs
,
4975 translation
== BIOS_ATA_TRANSLATION_LBA
?
4977 translation
== BIOS_ATA_TRANSLATION_NONE
?
4978 ",trans=none" : "");
4981 case QEMU_OPTION_nographic
:
4984 #ifdef CONFIG_CURSES
4985 case QEMU_OPTION_curses
:
4989 case QEMU_OPTION_portrait
:
4992 case QEMU_OPTION_kernel
:
4993 kernel_filename
= optarg
;
4995 case QEMU_OPTION_append
:
4996 kernel_cmdline
= optarg
;
4998 case QEMU_OPTION_cdrom
:
4999 drive_add(optarg
, CDROM_ALIAS
);
5001 case QEMU_OPTION_boot
:
5002 boot_devices
= optarg
;
5003 /* We just do some generic consistency checks */
5005 /* Could easily be extended to 64 devices if needed */
5008 boot_devices_bitmap
= 0;
5009 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5010 /* Allowed boot devices are:
5011 * a b : floppy disk drives
5012 * c ... f : IDE disk drives
5013 * g ... m : machine implementation dependant drives
5014 * n ... p : network devices
5015 * It's up to each machine implementation to check
5016 * if the given boot devices match the actual hardware
5017 * implementation and firmware features.
5019 if (*p
< 'a' || *p
> 'q') {
5020 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5023 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5025 "Boot device '%c' was given twice\n",*p
);
5028 boot_devices_bitmap
|= 1 << (*p
- 'a');
5032 case QEMU_OPTION_fda
:
5033 case QEMU_OPTION_fdb
:
5034 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5037 case QEMU_OPTION_no_fd_bootchk
:
5041 case QEMU_OPTION_net
:
5042 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5043 fprintf(stderr
, "qemu: too many network clients\n");
5046 net_clients
[nb_net_clients
] = optarg
;
5050 case QEMU_OPTION_tftp
:
5051 tftp_prefix
= optarg
;
5053 case QEMU_OPTION_bootp
:
5054 bootp_filename
= optarg
;
5057 case QEMU_OPTION_smb
:
5058 net_slirp_smb(optarg
);
5061 case QEMU_OPTION_redir
:
5062 net_slirp_redir(optarg
);
5065 case QEMU_OPTION_bt
:
5066 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5067 fprintf(stderr
, "qemu: too many bluetooth options\n");
5070 bt_opts
[nb_bt_opts
++] = optarg
;
5073 case QEMU_OPTION_audio_help
:
5077 case QEMU_OPTION_soundhw
:
5078 select_soundhw (optarg
);
5084 case QEMU_OPTION_m
: {
5088 value
= strtoul(optarg
, &ptr
, 10);
5090 case 0: case 'M': case 'm':
5097 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5101 /* On 32-bit hosts, QEMU is limited by virtual address space */
5102 if (value
> (2047 << 20)
5104 && HOST_LONG_BITS
== 32
5107 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5110 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5111 fprintf(stderr
, "qemu: ram size too large\n");
5120 const CPULogItem
*item
;
5122 mask
= cpu_str_to_log_mask(optarg
);
5124 printf("Log items (comma separated):\n");
5125 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5126 printf("%-10s %s\n", item
->name
, item
->help
);
5133 #ifdef CONFIG_GDBSTUB
5138 gdbstub_port
= optarg
;
5144 case QEMU_OPTION_bios
:
5151 keyboard_layout
= optarg
;
5153 case QEMU_OPTION_localtime
:
5156 case QEMU_OPTION_vga
:
5157 select_vgahw (optarg
);
5164 w
= strtol(p
, (char **)&p
, 10);
5167 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5173 h
= strtol(p
, (char **)&p
, 10);
5178 depth
= strtol(p
, (char **)&p
, 10);
5179 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5180 depth
!= 24 && depth
!= 32)
5182 } else if (*p
== '\0') {
5183 depth
= graphic_depth
;
5190 graphic_depth
= depth
;
5193 case QEMU_OPTION_echr
:
5196 term_escape_char
= strtol(optarg
, &r
, 0);
5198 printf("Bad argument to echr\n");
5201 case QEMU_OPTION_monitor
:
5202 monitor_device
= optarg
;
5204 case QEMU_OPTION_serial
:
5205 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5206 fprintf(stderr
, "qemu: too many serial ports\n");
5209 serial_devices
[serial_device_index
] = optarg
;
5210 serial_device_index
++;
5212 case QEMU_OPTION_parallel
:
5213 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5214 fprintf(stderr
, "qemu: too many parallel ports\n");
5217 parallel_devices
[parallel_device_index
] = optarg
;
5218 parallel_device_index
++;
5220 case QEMU_OPTION_loadvm
:
5223 case QEMU_OPTION_full_screen
:
5227 case QEMU_OPTION_no_frame
:
5230 case QEMU_OPTION_alt_grab
:
5233 case QEMU_OPTION_no_quit
:
5237 case QEMU_OPTION_pidfile
:
5241 case QEMU_OPTION_win2k_hack
:
5242 win2k_install_hack
= 1;
5246 case QEMU_OPTION_no_kqemu
:
5249 case QEMU_OPTION_kernel_kqemu
:
5254 case QEMU_OPTION_enable_kvm
:
5262 case QEMU_OPTION_no_kvm
:
5265 case QEMU_OPTION_no_kvm_irqchip
: {
5266 extern int kvm_irqchip
, kvm_pit
;
5271 case QEMU_OPTION_no_kvm_pit
: {
5276 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5277 extern int kvm_pit_reinject
;
5278 kvm_pit_reinject
= 0;
5281 case QEMU_OPTION_enable_nesting
: {
5285 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5286 case QEMU_OPTION_pcidevice
:
5287 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5288 fprintf(stderr
, "Too many assigned devices\n");
5291 assigned_devices
[assigned_devices_index
] = optarg
;
5292 assigned_devices_index
++;
5296 case QEMU_OPTION_usb
:
5299 case QEMU_OPTION_usbdevice
:
5301 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5302 fprintf(stderr
, "Too many USB devices\n");
5305 usb_devices
[usb_devices_index
] = optarg
;
5306 usb_devices_index
++;
5308 case QEMU_OPTION_smp
:
5309 smp_cpus
= atoi(optarg
);
5311 fprintf(stderr
, "Invalid number of CPUs\n");
5315 case QEMU_OPTION_vnc
:
5316 vnc_display
= optarg
;
5318 case QEMU_OPTION_no_acpi
:
5321 case QEMU_OPTION_no_hpet
:
5324 case QEMU_OPTION_no_reboot
:
5327 case QEMU_OPTION_no_shutdown
:
5330 case QEMU_OPTION_show_cursor
:
5333 case QEMU_OPTION_uuid
:
5334 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5335 fprintf(stderr
, "Fail to parse UUID string."
5336 " Wrong format.\n");
5340 case QEMU_OPTION_daemonize
:
5343 case QEMU_OPTION_option_rom
:
5344 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5345 fprintf(stderr
, "Too many option ROMs\n");
5348 option_rom
[nb_option_roms
] = optarg
;
5351 case QEMU_OPTION_semihosting
:
5352 semihosting_enabled
= 1;
5354 case QEMU_OPTION_tdf
:
5357 case QEMU_OPTION_kvm_shadow_memory
:
5358 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5360 case QEMU_OPTION_mempath
:
5363 case QEMU_OPTION_name
:
5367 case QEMU_OPTION_prom_env
:
5368 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5369 fprintf(stderr
, "Too many prom variables\n");
5372 prom_envs
[nb_prom_envs
] = optarg
;
5376 case QEMU_OPTION_cpu_vendor
:
5377 cpu_vendor_string
= optarg
;
5380 case QEMU_OPTION_old_param
:
5384 case QEMU_OPTION_clock
:
5385 configure_alarms(optarg
);
5387 case QEMU_OPTION_startdate
:
5390 time_t rtc_start_date
;
5391 if (!strcmp(optarg
, "now")) {
5392 rtc_date_offset
= -1;
5394 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5402 } else if (sscanf(optarg
, "%d-%d-%d",
5405 &tm
.tm_mday
) == 3) {
5414 rtc_start_date
= mktimegm(&tm
);
5415 if (rtc_start_date
== -1) {
5417 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5418 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5421 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5425 case QEMU_OPTION_tb_size
:
5426 tb_size
= strtol(optarg
, NULL
, 0);
5430 case QEMU_OPTION_icount
:
5432 if (strcmp(optarg
, "auto") == 0) {
5433 icount_time_shift
= -1;
5435 icount_time_shift
= strtol(optarg
, NULL
, 0);
5438 case QEMU_OPTION_incoming
:
5445 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5446 if (kvm_allowed
&& kqemu_allowed
) {
5448 "You can not enable both KVM and kqemu at the same time\n");
5453 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5454 if (smp_cpus
> machine
->max_cpus
) {
5455 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5456 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5462 if (serial_device_index
== 0)
5463 serial_devices
[0] = "stdio";
5464 if (parallel_device_index
== 0)
5465 parallel_devices
[0] = "null";
5466 if (strncmp(monitor_device
, "vc", 2) == 0)
5467 monitor_device
= "stdio";
5474 if (pipe(fds
) == -1)
5485 len
= read(fds
[0], &status
, 1);
5486 if (len
== -1 && (errno
== EINTR
))
5491 else if (status
== 1) {
5492 fprintf(stderr
, "Could not acquire pidfile\n");
5509 signal(SIGTSTP
, SIG_IGN
);
5510 signal(SIGTTOU
, SIG_IGN
);
5511 signal(SIGTTIN
, SIG_IGN
);
5516 if (kvm_enabled()) {
5517 if (kvm_qemu_init() < 0) {
5518 extern int kvm_allowed
;
5519 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5520 #ifdef NO_CPU_EMULATION
5521 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5529 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5532 write(fds
[1], &status
, 1);
5534 fprintf(stderr
, "Could not acquire pid file\n");
5542 linux_boot
= (kernel_filename
!= NULL
);
5543 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5545 if (!linux_boot
&& net_boot
== 0 &&
5546 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5549 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5550 fprintf(stderr
, "-append only allowed with -kernel option\n");
5554 if (!linux_boot
&& initrd_filename
!= NULL
) {
5555 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5559 /* boot to floppy or the default cd if no hard disk defined yet */
5560 if (!boot_devices
[0]) {
5561 boot_devices
= "cad";
5563 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5566 if (init_timer_alarm() < 0) {
5567 fprintf(stderr
, "could not initialize alarm timer\n");
5570 if (use_icount
&& icount_time_shift
< 0) {
5572 /* 125MIPS seems a reasonable initial guess at the guest speed.
5573 It will be corrected fairly quickly anyway. */
5574 icount_time_shift
= 3;
5575 init_icount_adjust();
5582 /* init network clients */
5583 if (nb_net_clients
== 0) {
5584 /* if no clients, we use a default config */
5585 net_clients
[nb_net_clients
++] = "nic";
5587 net_clients
[nb_net_clients
++] = "user";
5591 for(i
= 0;i
< nb_net_clients
; i
++) {
5592 if (net_client_parse(net_clients
[i
]) < 0)
5598 /* XXX: this should be moved in the PC machine instantiation code */
5599 if (net_boot
!= 0) {
5601 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5602 const char *model
= nd_table
[i
].model
;
5604 if (net_boot
& (1 << i
)) {
5607 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5608 if (get_image_size(buf
) > 0) {
5609 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5610 fprintf(stderr
, "Too many option ROMs\n");
5613 option_rom
[nb_option_roms
] = strdup(buf
);
5620 fprintf(stderr
, "No valid PXE rom found for network device\n");
5626 /* init the bluetooth world */
5627 for (i
= 0; i
< nb_bt_opts
; i
++)
5628 if (bt_parse(bt_opts
[i
]))
5631 /* init the memory */
5632 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5634 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5636 if (ram_size
< phys_ram_size
) {
5637 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5638 machine
->name
, (unsigned long long) phys_ram_size
);
5642 phys_ram_size
= ram_size
;
5644 ram_size
= phys_ram_size
;
5647 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5649 phys_ram_size
+= ram_size
;
5652 /* Initialize kvm */
5653 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5654 #define KVM_EXTRA_PAGES 3
5656 #define KVM_EXTRA_PAGES 0
5658 if (kvm_enabled()) {
5659 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5660 if (kvm_qemu_create_context() < 0) {
5661 fprintf(stderr
, "Could not create KVM context\n");
5666 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5667 if (!phys_ram_base
) {
5668 fprintf(stderr
, "Could not allocate physical memory\n");
5672 /* init the dynamic translator */
5673 cpu_exec_init_all(tb_size
* 1024 * 1024);
5677 /* we always create the cdrom drive, even if no disk is there */
5679 if (nb_drives_opt
< MAX_DRIVES
)
5680 drive_add(NULL
, CDROM_ALIAS
);
5682 /* we always create at least one floppy */
5684 if (nb_drives_opt
< MAX_DRIVES
)
5685 drive_add(NULL
, FD_ALIAS
, 0);
5687 /* we always create one sd slot, even if no card is in it */
5689 if (nb_drives_opt
< MAX_DRIVES
)
5690 drive_add(NULL
, SD_ALIAS
);
5692 /* open the virtual block devices
5693 * note that migration with device
5694 * hot add/remove is broken.
5696 for(i
= 0; i
< nb_drives_opt
; i
++)
5697 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5700 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5701 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5704 memset(&display_state
, 0, sizeof(display_state
));
5707 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5710 /* nearly nothing to do */
5711 dumb_display_init(ds
);
5712 } else if (vnc_display
!= NULL
) {
5713 vnc_display_init(ds
);
5714 if (vnc_display_open(ds
, vnc_display
) < 0)
5717 #if defined(CONFIG_CURSES)
5719 curses_display_init(ds
, full_screen
);
5723 #if defined(CONFIG_SDL)
5724 sdl_display_init(ds
, full_screen
, no_frame
);
5725 #elif defined(CONFIG_COCOA)
5726 cocoa_display_init(ds
, full_screen
);
5728 dumb_display_init(ds
);
5733 /* must be after terminal init, SDL library changes signal handlers */
5737 /* Maintain compatibility with multiple stdio monitors */
5738 if (!strcmp(monitor_device
,"stdio")) {
5739 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5740 const char *devname
= serial_devices
[i
];
5741 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5742 monitor_device
= NULL
;
5744 } else if (devname
&& !strcmp(devname
,"stdio")) {
5745 monitor_device
= NULL
;
5746 serial_devices
[i
] = "mon:stdio";
5751 if (monitor_device
) {
5752 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5754 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5757 monitor_init(monitor_hd
, !nographic
);
5760 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5761 const char *devname
= serial_devices
[i
];
5762 if (devname
&& strcmp(devname
, "none")) {
5764 snprintf(label
, sizeof(label
), "serial%d", i
);
5765 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5766 if (!serial_hds
[i
]) {
5767 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5771 if (strstart(devname
, "vc", 0))
5772 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5776 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5777 const char *devname
= parallel_devices
[i
];
5778 if (devname
&& strcmp(devname
, "none")) {
5780 snprintf(label
, sizeof(label
), "parallel%d", i
);
5781 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5782 if (!parallel_hds
[i
]) {
5783 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5787 if (strstart(devname
, "vc", 0))
5788 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5796 if (kvm_enabled()) {
5799 ret
= kvm_init(smp_cpus
);
5801 fprintf(stderr
, "failed to initialize KVM\n");
5807 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5808 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5810 current_machine
= machine
;
5812 /* Set KVM's vcpu state to qemu's initial CPUState. */
5813 if (kvm_enabled()) {
5816 ret
= kvm_sync_vcpus();
5818 fprintf(stderr
, "failed to initialize vcpus\n");
5823 /* init USB devices */
5825 for(i
= 0; i
< usb_devices_index
; i
++) {
5826 if (usb_device_add(usb_devices
[i
]) < 0) {
5827 fprintf(stderr
, "Warning: could not add USB device %s\n",
5833 if (display_state
.dpy_refresh
) {
5834 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5835 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5838 #ifdef CONFIG_GDBSTUB
5840 /* XXX: use standard host:port notation and modify options
5842 if (gdbserver_start(gdbstub_port
) < 0) {
5843 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5854 autostart
= 0; /* fixme how to deal with -daemonize */
5855 qemu_start_incoming_migration(incoming
);
5859 /* XXX: simplify init */
5872 len
= write(fds
[1], &status
, 1);
5873 if (len
== -1 && (errno
== EINTR
))
5880 TFR(fd
= open("/dev/null", O_RDWR
));