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"
40 #include "audio/audio.h"
41 #include "hw/device-assignment.h"
42 #include "migration.h"
56 #include <sys/times.h>
60 #include <sys/ioctl.h>
61 #include <sys/resource.h>
62 #include <sys/socket.h>
63 #include <netinet/in.h>
65 #if defined(__NetBSD__)
66 #include <net/if_tap.h>
69 #include <linux/if_tun.h>
71 #include <arpa/inet.h>
74 #include <sys/select.h>
82 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
83 #include <freebsd/stdlib.h>
88 #include <linux/rtc.h>
90 /* For the benefit of older linux systems which don't supply it,
91 we use a local copy of hpet.h. */
92 /* #include <linux/hpet.h> */
95 #include <linux/ppdev.h>
96 #include <linux/parport.h>
100 #include <sys/ethernet.h>
101 #include <sys/sockio.h>
102 #include <netinet/arp.h>
103 #include <netinet/in.h>
104 #include <netinet/in_systm.h>
105 #include <netinet/ip.h>
106 #include <netinet/ip_icmp.h> // must come after ip.h
107 #include <netinet/udp.h>
108 #include <netinet/tcp.h>
116 #include "qemu_socket.h"
118 #if defined(CONFIG_SLIRP)
119 #include "libslirp.h"
122 #if defined(__OpenBSD__)
126 #if defined(CONFIG_VDE)
127 #include <libvdeplug.h>
132 #include <sys/timeb.h>
133 #include <mmsystem.h>
134 #define getopt_long_only getopt_long
135 #define memalign(align, size) malloc(size)
142 #endif /* CONFIG_SDL */
146 #define main qemu_main
147 #endif /* CONFIG_COCOA */
151 #include "exec-all.h"
153 #include "qemu-kvm.h"
155 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
156 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
158 #define SMBD_COMMAND "/usr/sfw/sbin/smbd"
160 #define SMBD_COMMAND "/usr/sbin/smbd"
163 //#define DEBUG_UNUSED_IOPORT
164 //#define DEBUG_IOPORT
166 //#define DEBUG_SLIRP
169 #define DEFAULT_RAM_SIZE 144
171 #define DEFAULT_RAM_SIZE 128
174 /* Max number of USB devices that can be specified on the commandline. */
175 #define MAX_USB_CMDLINE 8
177 /* Max number of bluetooth switches on the commandline. */
178 #define MAX_BT_CMDLINE 10
180 /* XXX: use a two level table to limit memory usage */
181 #define MAX_IOPORTS 65536
183 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
184 const char *bios_name
= NULL
;
185 static void *ioport_opaque
[MAX_IOPORTS
];
186 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
187 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
188 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
189 to store the VM snapshots */
190 DriveInfo drives_table
[MAX_DRIVES
+1];
192 int extboot_drive
= -1;
193 static int vga_ram_size
;
194 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
195 DisplayState display_state
;
198 const char* keyboard_layout
= NULL
;
199 int64_t ticks_per_sec
;
202 NICInfo nd_table
[MAX_NICS
];
204 static int rtc_utc
= 1;
205 static int rtc_date_offset
= -1; /* -1 means no change */
206 int cirrus_vga_enabled
= 1;
207 int vmsvga_enabled
= 0;
209 int graphic_width
= 1024;
210 int graphic_height
= 768;
211 int graphic_depth
= 8;
213 int graphic_width
= 800;
214 int graphic_height
= 600;
215 int graphic_depth
= 15;
217 static int full_screen
= 0;
219 static int no_frame
= 0;
222 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
223 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
225 int win2k_install_hack
= 0;
228 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
229 int assigned_devices_index
;
231 const char *vnc_display
;
232 int acpi_enabled
= 1;
237 int graphic_rotate
= 0;
239 const char *incoming
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
243 int time_drift_fix
= 0;
244 unsigned int kvm_shadow_memory
= 0;
245 const char *mem_path
= NULL
;
247 const char *cpu_vendor_string
;
251 const char *qemu_name
;
254 unsigned int nb_prom_envs
= 0;
255 const char *prom_envs
[MAX_PROM_ENVS
];
258 struct drive_opt drives_opt
[MAX_DRIVES
];
260 static CPUState
*cur_cpu
;
261 static CPUState
*next_cpu
;
262 static int event_pending
= 1;
263 /* Conversion factor from emulated instructions to virtual clock ticks. */
264 static int icount_time_shift
;
265 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
266 #define MAX_ICOUNT_SHIFT 10
267 /* Compensate for varying guest execution speed. */
268 static int64_t qemu_icount_bias
;
269 static QEMUTimer
*icount_rt_timer
;
270 static QEMUTimer
*icount_vm_timer
;
272 uint8_t qemu_uuid
[16];
274 /* KVM runs the main loop in a separate thread. If we update one of the lists
275 * that are polled before or after select(), we need to make sure to break out
276 * of the select() to ensure the new item is serviced.
278 static void main_loop_break(void)
281 qemu_kvm_notify_work();
284 /***********************************************************/
285 /* x86 ISA bus support */
287 target_phys_addr_t isa_mem_base
= 0;
290 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
291 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
293 static uint32_t ioport_read(int index
, uint32_t address
)
295 static IOPortReadFunc
*default_func
[3] = {
296 default_ioport_readb
,
297 default_ioport_readw
,
300 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
302 func
= default_func
[index
];
303 return func(ioport_opaque
[address
], address
);
306 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
308 static IOPortWriteFunc
*default_func
[3] = {
309 default_ioport_writeb
,
310 default_ioport_writew
,
311 default_ioport_writel
313 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
315 func
= default_func
[index
];
316 func(ioport_opaque
[address
], address
, data
);
319 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
321 #ifdef DEBUG_UNUSED_IOPORT
322 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
327 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
329 #ifdef DEBUG_UNUSED_IOPORT
330 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
334 /* default is to make two byte accesses */
335 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
338 data
= ioport_read(0, address
);
339 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
340 data
|= ioport_read(0, address
) << 8;
344 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
346 ioport_write(0, address
, data
& 0xff);
347 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
348 ioport_write(0, address
, (data
>> 8) & 0xff);
351 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
353 #ifdef DEBUG_UNUSED_IOPORT
354 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
359 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
361 #ifdef DEBUG_UNUSED_IOPORT
362 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
366 /* size is the word size in byte */
367 int register_ioport_read(int start
, int length
, int size
,
368 IOPortReadFunc
*func
, void *opaque
)
374 } else if (size
== 2) {
376 } else if (size
== 4) {
379 hw_error("register_ioport_read: invalid size");
382 for(i
= start
; i
< start
+ length
; i
+= size
) {
383 ioport_read_table
[bsize
][i
] = func
;
384 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
385 hw_error("register_ioport_read: invalid opaque");
386 ioport_opaque
[i
] = opaque
;
391 /* size is the word size in byte */
392 int register_ioport_write(int start
, int length
, int size
,
393 IOPortWriteFunc
*func
, void *opaque
)
399 } else if (size
== 2) {
401 } else if (size
== 4) {
404 hw_error("register_ioport_write: invalid size");
407 for(i
= start
; i
< start
+ length
; i
+= size
) {
408 ioport_write_table
[bsize
][i
] = func
;
409 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
410 hw_error("register_ioport_write: invalid opaque");
411 ioport_opaque
[i
] = opaque
;
416 void isa_unassign_ioport(int start
, int length
)
420 for(i
= start
; i
< start
+ length
; i
++) {
421 ioport_read_table
[0][i
] = default_ioport_readb
;
422 ioport_read_table
[1][i
] = default_ioport_readw
;
423 ioport_read_table
[2][i
] = default_ioport_readl
;
425 ioport_write_table
[0][i
] = default_ioport_writeb
;
426 ioport_write_table
[1][i
] = default_ioport_writew
;
427 ioport_write_table
[2][i
] = default_ioport_writel
;
429 ioport_opaque
[i
] = NULL
;
433 /***********************************************************/
435 void cpu_outb(CPUState
*env
, int addr
, int val
)
438 if (loglevel
& CPU_LOG_IOPORT
)
439 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
441 ioport_write(0, addr
, val
);
444 env
->last_io_time
= cpu_get_time_fast();
448 void cpu_outw(CPUState
*env
, int addr
, int val
)
451 if (loglevel
& CPU_LOG_IOPORT
)
452 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
454 ioport_write(1, addr
, val
);
457 env
->last_io_time
= cpu_get_time_fast();
461 void cpu_outl(CPUState
*env
, int addr
, int val
)
464 if (loglevel
& CPU_LOG_IOPORT
)
465 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
467 ioport_write(2, addr
, val
);
470 env
->last_io_time
= cpu_get_time_fast();
474 int cpu_inb(CPUState
*env
, int addr
)
477 val
= ioport_read(0, addr
);
479 if (loglevel
& CPU_LOG_IOPORT
)
480 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
484 env
->last_io_time
= cpu_get_time_fast();
489 int cpu_inw(CPUState
*env
, int addr
)
492 val
= ioport_read(1, addr
);
494 if (loglevel
& CPU_LOG_IOPORT
)
495 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
499 env
->last_io_time
= cpu_get_time_fast();
504 int cpu_inl(CPUState
*env
, int addr
)
507 val
= ioport_read(2, addr
);
509 if (loglevel
& CPU_LOG_IOPORT
)
510 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
514 env
->last_io_time
= cpu_get_time_fast();
519 /***********************************************************/
520 void hw_error(const char *fmt
, ...)
526 fprintf(stderr
, "qemu: hardware error: ");
527 vfprintf(stderr
, fmt
, ap
);
528 fprintf(stderr
, "\n");
529 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
530 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
532 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
534 cpu_dump_state(env
, stderr
, fprintf
, 0);
544 static QEMUBalloonEvent
*qemu_balloon_event
;
545 void *qemu_balloon_event_opaque
;
547 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
549 qemu_balloon_event
= func
;
550 qemu_balloon_event_opaque
= opaque
;
553 void qemu_balloon(ram_addr_t target
)
555 if (qemu_balloon_event
)
556 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
559 ram_addr_t
qemu_balloon_status(void)
561 if (qemu_balloon_event
)
562 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
566 /***********************************************************/
569 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
570 static void *qemu_put_kbd_event_opaque
;
571 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
572 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
574 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
576 qemu_put_kbd_event_opaque
= opaque
;
577 qemu_put_kbd_event
= func
;
580 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
581 void *opaque
, int absolute
,
584 QEMUPutMouseEntry
*s
, *cursor
;
586 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
590 s
->qemu_put_mouse_event
= func
;
591 s
->qemu_put_mouse_event_opaque
= opaque
;
592 s
->qemu_put_mouse_event_absolute
= absolute
;
593 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
596 if (!qemu_put_mouse_event_head
) {
597 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
601 cursor
= qemu_put_mouse_event_head
;
602 while (cursor
->next
!= NULL
)
603 cursor
= cursor
->next
;
606 qemu_put_mouse_event_current
= s
;
611 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
613 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
615 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
618 cursor
= qemu_put_mouse_event_head
;
619 while (cursor
!= NULL
&& cursor
!= entry
) {
621 cursor
= cursor
->next
;
624 if (cursor
== NULL
) // does not exist or list empty
626 else if (prev
== NULL
) { // entry is head
627 qemu_put_mouse_event_head
= cursor
->next
;
628 if (qemu_put_mouse_event_current
== entry
)
629 qemu_put_mouse_event_current
= cursor
->next
;
630 qemu_free(entry
->qemu_put_mouse_event_name
);
635 prev
->next
= entry
->next
;
637 if (qemu_put_mouse_event_current
== entry
)
638 qemu_put_mouse_event_current
= prev
;
640 qemu_free(entry
->qemu_put_mouse_event_name
);
644 void kbd_put_keycode(int keycode
)
646 if (qemu_put_kbd_event
) {
647 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
651 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
653 QEMUPutMouseEvent
*mouse_event
;
654 void *mouse_event_opaque
;
657 if (!qemu_put_mouse_event_current
) {
662 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
664 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
667 if (graphic_rotate
) {
668 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
671 width
= graphic_width
- 1;
672 mouse_event(mouse_event_opaque
,
673 width
- dy
, dx
, dz
, buttons_state
);
675 mouse_event(mouse_event_opaque
,
676 dx
, dy
, dz
, buttons_state
);
680 int kbd_mouse_is_absolute(void)
682 if (!qemu_put_mouse_event_current
)
685 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
688 void do_info_mice(void)
690 QEMUPutMouseEntry
*cursor
;
693 if (!qemu_put_mouse_event_head
) {
694 term_printf("No mouse devices connected\n");
698 term_printf("Mouse devices available:\n");
699 cursor
= qemu_put_mouse_event_head
;
700 while (cursor
!= NULL
) {
701 term_printf("%c Mouse #%d: %s\n",
702 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
703 index
, cursor
->qemu_put_mouse_event_name
);
705 cursor
= cursor
->next
;
709 void do_mouse_set(int index
)
711 QEMUPutMouseEntry
*cursor
;
714 if (!qemu_put_mouse_event_head
) {
715 term_printf("No mouse devices connected\n");
719 cursor
= qemu_put_mouse_event_head
;
720 while (cursor
!= NULL
&& index
!= i
) {
722 cursor
= cursor
->next
;
726 qemu_put_mouse_event_current
= cursor
;
728 term_printf("Mouse at given index not found\n");
731 /* compute with 96 bit intermediate result: (a*b)/c */
732 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
737 #ifdef WORDS_BIGENDIAN
747 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
748 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
751 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
755 /***********************************************************/
756 /* real time host monotonic timer */
758 #define QEMU_TIMER_BASE 1000000000LL
762 static int64_t clock_freq
;
764 static void init_get_clock(void)
768 ret
= QueryPerformanceFrequency(&freq
);
770 fprintf(stderr
, "Could not calibrate ticks\n");
773 clock_freq
= freq
.QuadPart
;
776 static int64_t get_clock(void)
779 QueryPerformanceCounter(&ti
);
780 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
785 static int use_rt_clock
;
787 static void init_get_clock(void)
790 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
793 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
800 static int64_t get_clock(void)
802 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
805 clock_gettime(CLOCK_MONOTONIC
, &ts
);
806 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
810 /* XXX: using gettimeofday leads to problems if the date
811 changes, so it should be avoided. */
813 gettimeofday(&tv
, NULL
);
814 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
819 /* Return the virtual CPU time, based on the instruction counter. */
820 static int64_t cpu_get_icount(void)
823 CPUState
*env
= cpu_single_env
;;
824 icount
= qemu_icount
;
827 fprintf(stderr
, "Bad clock read\n");
828 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
830 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
833 /***********************************************************/
834 /* guest cycle counter */
836 static int64_t cpu_ticks_prev
;
837 static int64_t cpu_ticks_offset
;
838 static int64_t cpu_clock_offset
;
839 static int cpu_ticks_enabled
;
841 /* return the host CPU cycle counter and handle stop/restart */
842 int64_t cpu_get_ticks(void)
845 return cpu_get_icount();
847 if (!cpu_ticks_enabled
) {
848 return cpu_ticks_offset
;
851 ticks
= cpu_get_real_ticks();
852 if (cpu_ticks_prev
> ticks
) {
853 /* Note: non increasing ticks may happen if the host uses
855 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
857 cpu_ticks_prev
= ticks
;
858 return ticks
+ cpu_ticks_offset
;
862 /* return the host CPU monotonic timer and handle stop/restart */
863 static int64_t cpu_get_clock(void)
866 if (!cpu_ticks_enabled
) {
867 return cpu_clock_offset
;
870 return ti
+ cpu_clock_offset
;
874 /* enable cpu_get_ticks() */
875 void cpu_enable_ticks(void)
877 if (!cpu_ticks_enabled
) {
878 cpu_ticks_offset
-= cpu_get_real_ticks();
879 cpu_clock_offset
-= get_clock();
880 cpu_ticks_enabled
= 1;
884 /* disable cpu_get_ticks() : the clock is stopped. You must not call
885 cpu_get_ticks() after that. */
886 void cpu_disable_ticks(void)
888 if (cpu_ticks_enabled
) {
889 cpu_ticks_offset
= cpu_get_ticks();
890 cpu_clock_offset
= cpu_get_clock();
891 cpu_ticks_enabled
= 0;
895 /***********************************************************/
898 #define QEMU_TIMER_REALTIME 0
899 #define QEMU_TIMER_VIRTUAL 1
903 /* XXX: add frequency */
911 struct QEMUTimer
*next
;
914 struct qemu_alarm_timer
{
918 int (*start
)(struct qemu_alarm_timer
*t
);
919 void (*stop
)(struct qemu_alarm_timer
*t
);
920 void (*rearm
)(struct qemu_alarm_timer
*t
);
924 #define ALARM_FLAG_DYNTICKS 0x1
925 #define ALARM_FLAG_EXPIRED 0x2
927 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
929 return t
->flags
& ALARM_FLAG_DYNTICKS
;
932 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
934 if (!alarm_has_dynticks(t
))
940 /* TODO: MIN_TIMER_REARM_US should be optimized */
941 #define MIN_TIMER_REARM_US 250
943 static struct qemu_alarm_timer
*alarm_timer
;
945 static int alarm_timer_rfd
, alarm_timer_wfd
;
950 struct qemu_alarm_win32
{
954 } alarm_win32_data
= {0, NULL
, -1};
956 static int win32_start_timer(struct qemu_alarm_timer
*t
);
957 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
958 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
962 static int unix_start_timer(struct qemu_alarm_timer
*t
);
963 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
967 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
968 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
969 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
971 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
972 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
974 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
975 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
977 #endif /* __linux__ */
981 /* Correlation between real and virtual time is always going to be
982 fairly approximate, so ignore small variation.
983 When the guest is idle real and virtual time will be aligned in
985 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
987 static void icount_adjust(void)
992 static int64_t last_delta
;
993 /* If the VM is not running, then do nothing. */
997 cur_time
= cpu_get_clock();
998 cur_icount
= qemu_get_clock(vm_clock
);
999 delta
= cur_icount
- cur_time
;
1000 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
1002 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
1003 && icount_time_shift
> 0) {
1004 /* The guest is getting too far ahead. Slow time down. */
1005 icount_time_shift
--;
1008 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1009 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1010 /* The guest is getting too far behind. Speed time up. */
1011 icount_time_shift
++;
1014 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1017 static void icount_adjust_rt(void * opaque
)
1019 qemu_mod_timer(icount_rt_timer
,
1020 qemu_get_clock(rt_clock
) + 1000);
1024 static void icount_adjust_vm(void * opaque
)
1026 qemu_mod_timer(icount_vm_timer
,
1027 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1031 static void init_icount_adjust(void)
1033 /* Have both realtime and virtual time triggers for speed adjustment.
1034 The realtime trigger catches emulated time passing too slowly,
1035 the virtual time trigger catches emulated time passing too fast.
1036 Realtime triggers occur even when idle, so use them less frequently
1037 than VM triggers. */
1038 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1039 qemu_mod_timer(icount_rt_timer
,
1040 qemu_get_clock(rt_clock
) + 1000);
1041 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1042 qemu_mod_timer(icount_vm_timer
,
1043 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1046 static struct qemu_alarm_timer alarm_timers
[] = {
1049 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1050 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1051 /* HPET - if available - is preferred */
1052 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1053 /* ...otherwise try RTC */
1054 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1056 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1058 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1059 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1060 {"win32", 0, win32_start_timer
,
1061 win32_stop_timer
, NULL
, &alarm_win32_data
},
1066 static void show_available_alarms(void)
1070 printf("Available alarm timers, in order of precedence:\n");
1071 for (i
= 0; alarm_timers
[i
].name
; i
++)
1072 printf("%s\n", alarm_timers
[i
].name
);
1075 static void configure_alarms(char const *opt
)
1079 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1082 struct qemu_alarm_timer tmp
;
1084 if (!strcmp(opt
, "?")) {
1085 show_available_alarms();
1091 /* Reorder the array */
1092 name
= strtok(arg
, ",");
1094 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1095 if (!strcmp(alarm_timers
[i
].name
, name
))
1100 fprintf(stderr
, "Unknown clock %s\n", name
);
1109 tmp
= alarm_timers
[i
];
1110 alarm_timers
[i
] = alarm_timers
[cur
];
1111 alarm_timers
[cur
] = tmp
;
1115 name
= strtok(NULL
, ",");
1121 /* Disable remaining timers */
1122 for (i
= cur
; i
< count
; i
++)
1123 alarm_timers
[i
].name
= NULL
;
1125 show_available_alarms();
1130 QEMUClock
*rt_clock
;
1131 QEMUClock
*vm_clock
;
1133 static QEMUTimer
*active_timers
[2];
1135 static QEMUClock
*qemu_new_clock(int type
)
1138 clock
= qemu_mallocz(sizeof(QEMUClock
));
1145 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1149 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1152 ts
->opaque
= opaque
;
1156 void qemu_free_timer(QEMUTimer
*ts
)
1161 /* stop a timer, but do not dealloc it */
1162 void qemu_del_timer(QEMUTimer
*ts
)
1166 /* NOTE: this code must be signal safe because
1167 qemu_timer_expired() can be called from a signal. */
1168 pt
= &active_timers
[ts
->clock
->type
];
1181 /* modify the current timer so that it will be fired when current_time
1182 >= expire_time. The corresponding callback will be called. */
1183 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1189 /* add the timer in the sorted list */
1190 /* NOTE: this code must be signal safe because
1191 qemu_timer_expired() can be called from a signal. */
1192 pt
= &active_timers
[ts
->clock
->type
];
1197 if (t
->expire_time
> expire_time
)
1201 ts
->expire_time
= expire_time
;
1205 /* Rearm if necessary */
1206 if (pt
== &active_timers
[ts
->clock
->type
]) {
1207 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1208 qemu_rearm_alarm_timer(alarm_timer
);
1210 /* Interrupt execution to force deadline recalculation. */
1211 if (use_icount
&& cpu_single_env
) {
1212 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1217 int qemu_timer_pending(QEMUTimer
*ts
)
1220 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1227 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1231 return (timer_head
->expire_time
<= current_time
);
1234 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1240 if (!ts
|| ts
->expire_time
> current_time
)
1242 /* remove timer from the list before calling the callback */
1243 *ptimer_head
= ts
->next
;
1246 /* run the callback (the timer list can be modified) */
1251 int64_t qemu_get_clock(QEMUClock
*clock
)
1253 switch(clock
->type
) {
1254 case QEMU_TIMER_REALTIME
:
1255 return get_clock() / 1000000;
1257 case QEMU_TIMER_VIRTUAL
:
1259 return cpu_get_icount();
1261 return cpu_get_clock();
1266 static void init_timers(void)
1269 ticks_per_sec
= QEMU_TIMER_BASE
;
1270 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1271 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1275 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1277 uint64_t expire_time
;
1279 if (qemu_timer_pending(ts
)) {
1280 expire_time
= ts
->expire_time
;
1284 qemu_put_be64(f
, expire_time
);
1287 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1289 uint64_t expire_time
;
1291 expire_time
= qemu_get_be64(f
);
1292 if (expire_time
!= -1) {
1293 qemu_mod_timer(ts
, expire_time
);
1299 static void timer_save(QEMUFile
*f
, void *opaque
)
1301 if (cpu_ticks_enabled
) {
1302 hw_error("cannot save state if virtual timers are running");
1304 qemu_put_be64(f
, cpu_ticks_offset
);
1305 qemu_put_be64(f
, ticks_per_sec
);
1306 qemu_put_be64(f
, cpu_clock_offset
);
1309 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1311 if (version_id
!= 1 && version_id
!= 2)
1313 if (cpu_ticks_enabled
) {
1316 cpu_ticks_offset
=qemu_get_be64(f
);
1317 ticks_per_sec
=qemu_get_be64(f
);
1318 if (version_id
== 2) {
1319 cpu_clock_offset
=qemu_get_be64(f
);
1325 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1326 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1328 static void host_alarm_handler(int host_signum
)
1332 #define DISP_FREQ 1000
1334 static int64_t delta_min
= INT64_MAX
;
1335 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1337 ti
= qemu_get_clock(vm_clock
);
1338 if (last_clock
!= 0) {
1339 delta
= ti
- last_clock
;
1340 if (delta
< delta_min
)
1342 if (delta
> delta_max
)
1345 if (++count
== DISP_FREQ
) {
1346 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1347 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1348 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1349 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1350 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1352 delta_min
= INT64_MAX
;
1361 alarm_has_dynticks(alarm_timer
) ||
1363 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1364 qemu_get_clock(vm_clock
))) ||
1365 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1366 qemu_get_clock(rt_clock
))) {
1367 CPUState
*env
= next_cpu
;
1370 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1371 SetEvent(data
->host_alarm
);
1373 static const char byte
= 0;
1374 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1376 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1379 /* stop the currently executing cpu because a timer occured */
1380 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1382 if (env
->kqemu_enabled
) {
1383 kqemu_cpu_interrupt(env
);
1391 static int64_t qemu_next_deadline(void)
1395 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1396 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1397 qemu_get_clock(vm_clock
);
1399 /* To avoid problems with overflow limit this to 2^32. */
1409 #if defined(__linux__) || defined(_WIN32)
1410 static uint64_t qemu_next_deadline_dyntick(void)
1418 delta
= (qemu_next_deadline() + 999) / 1000;
1420 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1421 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1422 qemu_get_clock(rt_clock
))*1000;
1423 if (rtdelta
< delta
)
1427 if (delta
< MIN_TIMER_REARM_US
)
1428 delta
= MIN_TIMER_REARM_US
;
1436 /* Sets a specific flag */
1437 static int fcntl_setfl(int fd
, int flag
)
1441 flags
= fcntl(fd
, F_GETFL
);
1445 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1451 #if defined(__linux__)
1453 #define RTC_FREQ 1024
1455 static void enable_sigio_timer(int fd
)
1457 struct sigaction act
;
1460 sigfillset(&act
.sa_mask
);
1462 act
.sa_handler
= host_alarm_handler
;
1464 sigaction(SIGIO
, &act
, NULL
);
1465 fcntl_setfl(fd
, O_ASYNC
);
1466 fcntl(fd
, F_SETOWN
, getpid());
1469 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1471 struct hpet_info info
;
1474 fd
= open("/dev/hpet", O_RDONLY
);
1479 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1481 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1482 "error, but for better emulation accuracy type:\n"
1483 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1487 /* Check capabilities */
1488 r
= ioctl(fd
, HPET_INFO
, &info
);
1492 /* Enable periodic mode */
1493 r
= ioctl(fd
, HPET_EPI
, 0);
1494 if (info
.hi_flags
&& (r
< 0))
1497 /* Enable interrupt */
1498 r
= ioctl(fd
, HPET_IE_ON
, 0);
1502 enable_sigio_timer(fd
);
1503 t
->priv
= (void *)(long)fd
;
1511 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1513 int fd
= (long)t
->priv
;
1518 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1521 unsigned long current_rtc_freq
= 0;
1523 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1526 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1527 if (current_rtc_freq
!= RTC_FREQ
&&
1528 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1529 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1530 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1531 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1534 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1540 enable_sigio_timer(rtc_fd
);
1542 t
->priv
= (void *)(long)rtc_fd
;
1547 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1549 int rtc_fd
= (long)t
->priv
;
1554 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1558 struct sigaction act
;
1560 sigfillset(&act
.sa_mask
);
1562 act
.sa_handler
= host_alarm_handler
;
1564 sigaction(SIGALRM
, &act
, NULL
);
1566 ev
.sigev_value
.sival_int
= 0;
1567 ev
.sigev_notify
= SIGEV_SIGNAL
;
1568 ev
.sigev_signo
= SIGALRM
;
1570 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1571 perror("timer_create");
1573 /* disable dynticks */
1574 fprintf(stderr
, "Dynamic Ticks disabled\n");
1579 t
->priv
= (void *)(long)host_timer
;
1584 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1586 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1588 timer_delete(host_timer
);
1591 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1593 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1594 struct itimerspec timeout
;
1595 int64_t nearest_delta_us
= INT64_MAX
;
1598 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1599 !active_timers
[QEMU_TIMER_VIRTUAL
])
1602 nearest_delta_us
= qemu_next_deadline_dyntick();
1604 /* check whether a timer is already running */
1605 if (timer_gettime(host_timer
, &timeout
)) {
1607 fprintf(stderr
, "Internal timer error: aborting\n");
1610 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1611 if (current_us
&& current_us
<= nearest_delta_us
)
1614 timeout
.it_interval
.tv_sec
= 0;
1615 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1616 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1617 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1618 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1620 fprintf(stderr
, "Internal timer error: aborting\n");
1625 #endif /* defined(__linux__) */
1627 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1629 struct sigaction act
;
1630 struct itimerval itv
;
1634 sigfillset(&act
.sa_mask
);
1636 act
.sa_handler
= host_alarm_handler
;
1638 sigaction(SIGALRM
, &act
, NULL
);
1640 itv
.it_interval
.tv_sec
= 0;
1641 /* for i386 kernel 2.6 to get 1 ms */
1642 itv
.it_interval
.tv_usec
= 999;
1643 itv
.it_value
.tv_sec
= 0;
1644 itv
.it_value
.tv_usec
= 10 * 1000;
1646 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1653 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1655 struct itimerval itv
;
1657 memset(&itv
, 0, sizeof(itv
));
1658 setitimer(ITIMER_REAL
, &itv
, NULL
);
1661 #endif /* !defined(_WIN32) */
1663 static void try_to_rearm_timer(void *opaque
)
1665 struct qemu_alarm_timer
*t
= opaque
;
1669 /* Drain the notify pipe */
1672 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1673 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1676 /* vm time timers */
1677 if (vm_running
&& likely(!(cur_cpu
&& (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
1678 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1679 qemu_get_clock(vm_clock
));
1681 /* real time timers */
1682 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1683 qemu_get_clock(rt_clock
));
1685 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1686 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1687 qemu_rearm_alarm_timer(alarm_timer
);
1693 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1696 struct qemu_alarm_win32
*data
= t
->priv
;
1699 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1700 if (!data
->host_alarm
) {
1701 perror("Failed CreateEvent");
1705 memset(&tc
, 0, sizeof(tc
));
1706 timeGetDevCaps(&tc
, sizeof(tc
));
1708 if (data
->period
< tc
.wPeriodMin
)
1709 data
->period
= tc
.wPeriodMin
;
1711 timeBeginPeriod(data
->period
);
1713 flags
= TIME_CALLBACK_FUNCTION
;
1714 if (alarm_has_dynticks(t
))
1715 flags
|= TIME_ONESHOT
;
1717 flags
|= TIME_PERIODIC
;
1719 data
->timerId
= timeSetEvent(1, // interval (ms)
1720 data
->period
, // resolution
1721 host_alarm_handler
, // function
1722 (DWORD
)t
, // parameter
1725 if (!data
->timerId
) {
1726 perror("Failed to initialize win32 alarm timer");
1728 timeEndPeriod(data
->period
);
1729 CloseHandle(data
->host_alarm
);
1733 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1738 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1740 struct qemu_alarm_win32
*data
= t
->priv
;
1742 timeKillEvent(data
->timerId
);
1743 timeEndPeriod(data
->period
);
1745 CloseHandle(data
->host_alarm
);
1748 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1750 struct qemu_alarm_win32
*data
= t
->priv
;
1751 uint64_t nearest_delta_us
;
1753 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1754 !active_timers
[QEMU_TIMER_VIRTUAL
])
1757 nearest_delta_us
= qemu_next_deadline_dyntick();
1758 nearest_delta_us
/= 1000;
1760 timeKillEvent(data
->timerId
);
1762 data
->timerId
= timeSetEvent(1,
1766 TIME_ONESHOT
| TIME_PERIODIC
);
1768 if (!data
->timerId
) {
1769 perror("Failed to re-arm win32 alarm timer");
1771 timeEndPeriod(data
->period
);
1772 CloseHandle(data
->host_alarm
);
1779 static int init_timer_alarm(void)
1781 struct qemu_alarm_timer
*t
= NULL
;
1791 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1795 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1799 alarm_timer_rfd
= fds
[0];
1800 alarm_timer_wfd
= fds
[1];
1803 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1804 t
= &alarm_timers
[i
];
1817 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1818 try_to_rearm_timer
, NULL
, t
);
1833 static void quit_timers(void)
1835 alarm_timer
->stop(alarm_timer
);
1839 /***********************************************************/
1840 /* host time/date access */
1841 void qemu_get_timedate(struct tm
*tm
, int offset
)
1848 if (rtc_date_offset
== -1) {
1852 ret
= localtime(&ti
);
1854 ti
-= rtc_date_offset
;
1858 memcpy(tm
, ret
, sizeof(struct tm
));
1861 int qemu_timedate_diff(struct tm
*tm
)
1865 if (rtc_date_offset
== -1)
1867 seconds
= mktimegm(tm
);
1869 seconds
= mktime(tm
);
1871 seconds
= mktimegm(tm
) + rtc_date_offset
;
1873 return seconds
- time(NULL
);
1877 static void socket_cleanup(void)
1882 static int socket_init(void)
1887 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1889 err
= WSAGetLastError();
1890 fprintf(stderr
, "WSAStartup: %d\n", err
);
1893 atexit(socket_cleanup
);
1898 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1903 while (*p
!= '\0' && *p
!= '=') {
1904 if (q
&& (q
- buf
) < buf_size
- 1)
1914 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1919 while (*p
!= '\0') {
1921 if (*(p
+ 1) != ',')
1925 if (q
&& (q
- buf
) < buf_size
- 1)
1935 int get_param_value(char *buf
, int buf_size
,
1936 const char *tag
, const char *str
)
1943 p
= get_opt_name(option
, sizeof(option
), p
);
1947 if (!strcmp(tag
, option
)) {
1948 (void)get_opt_value(buf
, buf_size
, p
);
1951 p
= get_opt_value(NULL
, 0, p
);
1960 int check_params(char *buf
, int buf_size
,
1961 const char * const *params
, const char *str
)
1968 p
= get_opt_name(buf
, buf_size
, p
);
1972 for(i
= 0; params
[i
] != NULL
; i
++)
1973 if (!strcmp(params
[i
], buf
))
1975 if (params
[i
] == NULL
)
1977 p
= get_opt_value(NULL
, 0, p
);
1985 /***********************************************************/
1986 /* Bluetooth support */
1989 static struct HCIInfo
*hci_table
[MAX_NICS
];
1991 static struct bt_vlan_s
{
1992 struct bt_scatternet_s net
;
1994 struct bt_vlan_s
*next
;
1997 /* find or alloc a new bluetooth "VLAN" */
1998 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
2000 struct bt_vlan_s
**pvlan
, *vlan
;
2001 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2005 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
2007 pvlan
= &first_bt_vlan
;
2008 while (*pvlan
!= NULL
)
2009 pvlan
= &(*pvlan
)->next
;
2014 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2018 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2023 static struct HCIInfo null_hci
= {
2024 .cmd_send
= null_hci_send
,
2025 .sco_send
= null_hci_send
,
2026 .acl_send
= null_hci_send
,
2027 .bdaddr_set
= null_hci_addr_set
,
2030 struct HCIInfo
*qemu_next_hci(void)
2032 if (cur_hci
== nb_hcis
)
2035 return hci_table
[cur_hci
++];
2038 static struct HCIInfo
*hci_init(const char *str
)
2041 struct bt_scatternet_s
*vlan
= 0;
2043 if (!strcmp(str
, "null"))
2046 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2048 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2049 else if (!strncmp(str
, "hci", 3)) {
2052 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2053 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2058 vlan
= qemu_find_bt_vlan(0);
2060 return bt_new_hci(vlan
);
2063 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2068 static int bt_hci_parse(const char *str
)
2070 struct HCIInfo
*hci
;
2073 if (nb_hcis
>= MAX_NICS
) {
2074 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2078 hci
= hci_init(str
);
2087 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2088 hci
->bdaddr_set(hci
, bdaddr
.b
);
2090 hci_table
[nb_hcis
++] = hci
;
2095 static void bt_vhci_add(int vlan_id
)
2097 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2100 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2101 "an empty scatternet %i\n", vlan_id
);
2103 bt_vhci_init(bt_new_hci(vlan
));
2106 static struct bt_device_s
*bt_device_add(const char *opt
)
2108 struct bt_scatternet_s
*vlan
;
2110 char *endp
= strstr(opt
, ",vlan=");
2111 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2114 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2117 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2119 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2124 vlan
= qemu_find_bt_vlan(vlan_id
);
2127 fprintf(stderr
, "qemu: warning: adding a slave device to "
2128 "an empty scatternet %i\n", vlan_id
);
2130 if (!strcmp(devname
, "keyboard"))
2131 return bt_keyboard_init(vlan
);
2133 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2137 static int bt_parse(const char *opt
)
2139 const char *endp
, *p
;
2142 if (strstart(opt
, "hci", &endp
)) {
2143 if (!*endp
|| *endp
== ',') {
2145 if (!strstart(endp
, ",vlan=", 0))
2148 return bt_hci_parse(opt
);
2150 } else if (strstart(opt
, "vhci", &endp
)) {
2151 if (!*endp
|| *endp
== ',') {
2153 if (strstart(endp
, ",vlan=", &p
)) {
2154 vlan
= strtol(p
, (char **) &endp
, 0);
2156 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2160 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2169 } else if (strstart(opt
, "device:", &endp
))
2170 return !bt_device_add(endp
);
2172 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2176 /***********************************************************/
2177 /* QEMU Block devices */
2179 #define HD_ALIAS "index=%d,media=disk"
2181 #define CDROM_ALIAS "index=1,media=cdrom"
2183 #define CDROM_ALIAS "index=2,media=cdrom"
2185 #define FD_ALIAS "index=%d,if=floppy"
2186 #define PFLASH_ALIAS "if=pflash"
2187 #define MTD_ALIAS "if=mtd"
2188 #define SD_ALIAS "index=0,if=sd"
2190 static int drive_opt_get_free_idx(void)
2194 for (index
= 0; index
< MAX_DRIVES
; index
++)
2195 if (!drives_opt
[index
].used
) {
2196 drives_opt
[index
].used
= 1;
2203 static int drive_get_free_idx(void)
2207 for (index
= 0; index
< MAX_DRIVES
; index
++)
2208 if (!drives_table
[index
].used
) {
2209 drives_table
[index
].used
= 1;
2216 int drive_add(const char *file
, const char *fmt
, ...)
2219 int index
= drive_opt_get_free_idx();
2221 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2222 fprintf(stderr
, "qemu: too many drives\n");
2226 drives_opt
[index
].file
= file
;
2228 vsnprintf(drives_opt
[index
].opt
,
2229 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2236 void drive_remove(int index
)
2238 drives_opt
[index
].used
= 0;
2242 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2246 /* seek interface, bus and unit */
2248 for (index
= 0; index
< MAX_DRIVES
; index
++)
2249 if (drives_table
[index
].type
== type
&&
2250 drives_table
[index
].bus
== bus
&&
2251 drives_table
[index
].unit
== unit
&&
2252 drives_table
[index
].used
)
2258 int drive_get_max_bus(BlockInterfaceType type
)
2264 for (index
= 0; index
< nb_drives
; index
++) {
2265 if(drives_table
[index
].type
== type
&&
2266 drives_table
[index
].bus
> max_bus
)
2267 max_bus
= drives_table
[index
].bus
;
2272 static void bdrv_format_print(void *opaque
, const char *name
)
2274 fprintf(stderr
, " %s", name
);
2277 void drive_uninit(BlockDriverState
*bdrv
)
2281 for (i
= 0; i
< MAX_DRIVES
; i
++)
2282 if (drives_table
[i
].bdrv
== bdrv
) {
2283 drives_table
[i
].bdrv
= NULL
;
2284 drives_table
[i
].used
= 0;
2285 drive_remove(drives_table
[i
].drive_opt_idx
);
2291 int drive_init(struct drive_opt
*arg
, int snapshot
,
2292 QEMUMachine
*machine
)
2297 const char *mediastr
= "";
2298 BlockInterfaceType type
;
2299 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2300 int bus_id
, unit_id
;
2301 int cyls
, heads
, secs
, translation
;
2302 BlockDriverState
*bdrv
;
2303 BlockDriver
*drv
= NULL
;
2308 int drives_table_idx
;
2309 char *str
= arg
->opt
;
2310 static const char * const params
[] = { "bus", "unit", "if", "index",
2311 "cyls", "heads", "secs", "trans",
2312 "media", "snapshot", "file",
2313 "cache", "format", "boot", NULL
};
2315 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2316 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2322 cyls
= heads
= secs
= 0;
2325 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2329 if (machine
->use_scsi
) {
2331 max_devs
= MAX_SCSI_DEVS
;
2332 pstrcpy(devname
, sizeof(devname
), "scsi");
2335 max_devs
= MAX_IDE_DEVS
;
2336 pstrcpy(devname
, sizeof(devname
), "ide");
2340 /* extract parameters */
2342 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2343 bus_id
= strtol(buf
, NULL
, 0);
2345 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2350 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2351 unit_id
= strtol(buf
, NULL
, 0);
2353 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2358 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2359 pstrcpy(devname
, sizeof(devname
), buf
);
2360 if (!strcmp(buf
, "ide")) {
2362 max_devs
= MAX_IDE_DEVS
;
2363 } else if (!strcmp(buf
, "scsi")) {
2365 max_devs
= MAX_SCSI_DEVS
;
2366 } else if (!strcmp(buf
, "floppy")) {
2369 } else if (!strcmp(buf
, "pflash")) {
2372 } else if (!strcmp(buf
, "mtd")) {
2375 } else if (!strcmp(buf
, "sd")) {
2378 } else if (!strcmp(buf
, "virtio")) {
2382 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2387 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2388 index
= strtol(buf
, NULL
, 0);
2390 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2395 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2396 cyls
= strtol(buf
, NULL
, 0);
2399 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2400 heads
= strtol(buf
, NULL
, 0);
2403 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2404 secs
= strtol(buf
, NULL
, 0);
2407 if (cyls
|| heads
|| secs
) {
2408 if (cyls
< 1 || cyls
> 16383) {
2409 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2412 if (heads
< 1 || heads
> 16) {
2413 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2416 if (secs
< 1 || secs
> 63) {
2417 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2422 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2425 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2429 if (!strcmp(buf
, "none"))
2430 translation
= BIOS_ATA_TRANSLATION_NONE
;
2431 else if (!strcmp(buf
, "lba"))
2432 translation
= BIOS_ATA_TRANSLATION_LBA
;
2433 else if (!strcmp(buf
, "auto"))
2434 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2436 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2441 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2442 if (!strcmp(buf
, "disk")) {
2444 } else if (!strcmp(buf
, "cdrom")) {
2445 if (cyls
|| secs
|| heads
) {
2447 "qemu: '%s' invalid physical CHS format\n", str
);
2450 media
= MEDIA_CDROM
;
2452 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2457 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2458 if (!strcmp(buf
, "on"))
2460 else if (!strcmp(buf
, "off"))
2463 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2468 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2469 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2471 else if (!strcmp(buf
, "writethrough"))
2473 else if (!strcmp(buf
, "writeback"))
2476 fprintf(stderr
, "qemu: invalid cache option\n");
2481 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2482 if (strcmp(buf
, "?") == 0) {
2483 fprintf(stderr
, "qemu: Supported formats:");
2484 bdrv_iterate_format(bdrv_format_print
, NULL
);
2485 fprintf(stderr
, "\n");
2488 drv
= bdrv_find_format(buf
);
2490 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2495 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2496 if (!strcmp(buf
, "on")) {
2497 if (extboot_drive
!= -1) {
2498 fprintf(stderr
, "qemu: two bootable drives specified\n");
2501 extboot_drive
= nb_drives
;
2502 } else if (strcmp(buf
, "off")) {
2503 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2508 if (arg
->file
== NULL
)
2509 get_param_value(file
, sizeof(file
), "file", str
);
2511 pstrcpy(file
, sizeof(file
), arg
->file
);
2513 /* compute bus and unit according index */
2516 if (bus_id
!= 0 || unit_id
!= -1) {
2518 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2526 unit_id
= index
% max_devs
;
2527 bus_id
= index
/ max_devs
;
2531 /* if user doesn't specify a unit_id,
2532 * try to find the first free
2535 if (unit_id
== -1) {
2537 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2539 if (max_devs
&& unit_id
>= max_devs
) {
2540 unit_id
-= max_devs
;
2548 if (max_devs
&& unit_id
>= max_devs
) {
2549 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2550 str
, unit_id
, max_devs
- 1);
2555 * ignore multiple definitions
2558 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2563 if (type
== IF_IDE
|| type
== IF_SCSI
)
2564 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2566 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2567 devname
, bus_id
, mediastr
, unit_id
);
2569 snprintf(buf
, sizeof(buf
), "%s%s%i",
2570 devname
, mediastr
, unit_id
);
2571 bdrv
= bdrv_new(buf
);
2572 drives_table_idx
= drive_get_free_idx();
2573 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2574 drives_table
[drives_table_idx
].type
= type
;
2575 drives_table
[drives_table_idx
].bus
= bus_id
;
2576 drives_table
[drives_table_idx
].unit
= unit_id
;
2577 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2586 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2587 bdrv_set_translation_hint(bdrv
, translation
);
2591 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2596 /* FIXME: This isn't really a floppy, but it's a reasonable
2599 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2610 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2611 cache
= 2; /* always use write-back with snapshot */
2613 if (cache
== 0) /* no caching */
2614 bdrv_flags
|= BDRV_O_NOCACHE
;
2615 else if (cache
== 2) /* write-back */
2616 bdrv_flags
|= BDRV_O_CACHE_WB
;
2617 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2618 fprintf(stderr
, "qemu: could not open disk image %s\n",
2622 return drives_table_idx
;
2625 /***********************************************************/
2628 static USBPort
*used_usb_ports
;
2629 static USBPort
*free_usb_ports
;
2631 /* ??? Maybe change this to register a hub to keep track of the topology. */
2632 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2633 usb_attachfn attach
)
2635 port
->opaque
= opaque
;
2636 port
->index
= index
;
2637 port
->attach
= attach
;
2638 port
->next
= free_usb_ports
;
2639 free_usb_ports
= port
;
2642 int usb_device_add_dev(USBDevice
*dev
)
2646 /* Find a USB port to add the device to. */
2647 port
= free_usb_ports
;
2651 /* Create a new hub and chain it on. */
2652 free_usb_ports
= NULL
;
2653 port
->next
= used_usb_ports
;
2654 used_usb_ports
= port
;
2656 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2657 usb_attach(port
, hub
);
2658 port
= free_usb_ports
;
2661 free_usb_ports
= port
->next
;
2662 port
->next
= used_usb_ports
;
2663 used_usb_ports
= port
;
2664 usb_attach(port
, dev
);
2668 static int usb_device_add(const char *devname
)
2673 if (!free_usb_ports
)
2676 if (strstart(devname
, "host:", &p
)) {
2677 dev
= usb_host_device_open(p
);
2678 } else if (!strcmp(devname
, "mouse")) {
2679 dev
= usb_mouse_init();
2680 } else if (!strcmp(devname
, "tablet")) {
2681 dev
= usb_tablet_init();
2682 } else if (!strcmp(devname
, "keyboard")) {
2683 dev
= usb_keyboard_init();
2684 } else if (strstart(devname
, "disk:", &p
)) {
2685 dev
= usb_msd_init(p
);
2686 } else if (!strcmp(devname
, "wacom-tablet")) {
2687 dev
= usb_wacom_init();
2688 } else if (strstart(devname
, "serial:", &p
)) {
2689 dev
= usb_serial_init(p
);
2690 #ifdef CONFIG_BRLAPI
2691 } else if (!strcmp(devname
, "braille")) {
2692 dev
= usb_baum_init();
2694 } else if (strstart(devname
, "net:", &p
)) {
2697 if (net_client_init("nic", p
) < 0)
2699 nd_table
[nic
].model
= "usb";
2700 dev
= usb_net_init(&nd_table
[nic
]);
2701 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2702 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2703 bt_new_hci(qemu_find_bt_vlan(0)));
2710 return usb_device_add_dev(dev
);
2713 int usb_device_del_addr(int bus_num
, int addr
)
2719 if (!used_usb_ports
)
2725 lastp
= &used_usb_ports
;
2726 port
= used_usb_ports
;
2727 while (port
&& port
->dev
->addr
!= addr
) {
2728 lastp
= &port
->next
;
2736 *lastp
= port
->next
;
2737 usb_attach(port
, NULL
);
2738 dev
->handle_destroy(dev
);
2739 port
->next
= free_usb_ports
;
2740 free_usb_ports
= port
;
2744 static int usb_device_del(const char *devname
)
2749 if (strstart(devname
, "host:", &p
))
2750 return usb_host_device_close(p
);
2752 if (!used_usb_ports
)
2755 p
= strchr(devname
, '.');
2758 bus_num
= strtoul(devname
, NULL
, 0);
2759 addr
= strtoul(p
+ 1, NULL
, 0);
2761 return usb_device_del_addr(bus_num
, addr
);
2764 void do_usb_add(const char *devname
)
2766 usb_device_add(devname
);
2769 void do_usb_del(const char *devname
)
2771 usb_device_del(devname
);
2778 const char *speed_str
;
2781 term_printf("USB support not enabled\n");
2785 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2789 switch(dev
->speed
) {
2793 case USB_SPEED_FULL
:
2796 case USB_SPEED_HIGH
:
2803 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2804 0, dev
->addr
, speed_str
, dev
->devname
);
2808 /***********************************************************/
2809 /* PCMCIA/Cardbus */
2811 static struct pcmcia_socket_entry_s
{
2812 struct pcmcia_socket_s
*socket
;
2813 struct pcmcia_socket_entry_s
*next
;
2814 } *pcmcia_sockets
= 0;
2816 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2818 struct pcmcia_socket_entry_s
*entry
;
2820 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2821 entry
->socket
= socket
;
2822 entry
->next
= pcmcia_sockets
;
2823 pcmcia_sockets
= entry
;
2826 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2828 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2830 ptr
= &pcmcia_sockets
;
2831 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2832 if (entry
->socket
== socket
) {
2838 void pcmcia_info(void)
2840 struct pcmcia_socket_entry_s
*iter
;
2841 if (!pcmcia_sockets
)
2842 term_printf("No PCMCIA sockets\n");
2844 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2845 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2846 iter
->socket
->attached
? iter
->socket
->card_string
:
2850 /***********************************************************/
2853 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2857 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2861 static void dumb_display_init(DisplayState
*ds
)
2866 ds
->dpy_update
= dumb_update
;
2867 ds
->dpy_resize
= dumb_resize
;
2868 ds
->dpy_refresh
= NULL
;
2869 ds
->gui_timer_interval
= 0;
2873 /***********************************************************/
2876 #define MAX_IO_HANDLERS 64
2878 typedef struct IOHandlerRecord
{
2880 IOCanRWHandler
*fd_read_poll
;
2882 IOHandler
*fd_write
;
2885 /* temporary data */
2887 struct IOHandlerRecord
*next
;
2890 static IOHandlerRecord
*first_io_handler
;
2892 /* XXX: fd_read_poll should be suppressed, but an API change is
2893 necessary in the character devices to suppress fd_can_read(). */
2894 int qemu_set_fd_handler2(int fd
,
2895 IOCanRWHandler
*fd_read_poll
,
2897 IOHandler
*fd_write
,
2900 IOHandlerRecord
**pioh
, *ioh
;
2902 if (!fd_read
&& !fd_write
) {
2903 pioh
= &first_io_handler
;
2908 if (ioh
->fd
== fd
) {
2915 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2919 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2922 ioh
->next
= first_io_handler
;
2923 first_io_handler
= ioh
;
2926 ioh
->fd_read_poll
= fd_read_poll
;
2927 ioh
->fd_read
= fd_read
;
2928 ioh
->fd_write
= fd_write
;
2929 ioh
->opaque
= opaque
;
2936 int qemu_set_fd_handler(int fd
,
2938 IOHandler
*fd_write
,
2941 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2945 /***********************************************************/
2946 /* Polling handling */
2948 typedef struct PollingEntry
{
2951 struct PollingEntry
*next
;
2954 static PollingEntry
*first_polling_entry
;
2956 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2958 PollingEntry
**ppe
, *pe
;
2959 pe
= qemu_mallocz(sizeof(PollingEntry
));
2963 pe
->opaque
= opaque
;
2964 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2969 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2971 PollingEntry
**ppe
, *pe
;
2972 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2974 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2982 /***********************************************************/
2983 /* Wait objects support */
2984 typedef struct WaitObjects
{
2986 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2987 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2988 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2991 static WaitObjects wait_objects
= {0};
2993 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2995 WaitObjects
*w
= &wait_objects
;
2997 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2999 w
->events
[w
->num
] = handle
;
3000 w
->func
[w
->num
] = func
;
3001 w
->opaque
[w
->num
] = opaque
;
3006 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3009 WaitObjects
*w
= &wait_objects
;
3012 for (i
= 0; i
< w
->num
; i
++) {
3013 if (w
->events
[i
] == handle
)
3016 w
->events
[i
] = w
->events
[i
+ 1];
3017 w
->func
[i
] = w
->func
[i
+ 1];
3018 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3026 /***********************************************************/
3027 /* ram save/restore */
3029 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3033 v
= qemu_get_byte(f
);
3036 if (qemu_get_buffer(f
, buf
, len
) != len
)
3040 v
= qemu_get_byte(f
);
3041 memset(buf
, v
, len
);
3047 if (qemu_file_has_error(f
))
3053 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3058 if (qemu_get_be32(f
) != phys_ram_size
)
3060 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3061 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3063 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3070 #define BDRV_HASH_BLOCK_SIZE 1024
3071 #define IOBUF_SIZE 4096
3072 #define RAM_CBLOCK_MAGIC 0xfabe
3074 typedef struct RamDecompressState
{
3077 uint8_t buf
[IOBUF_SIZE
];
3078 } RamDecompressState
;
3080 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3083 memset(s
, 0, sizeof(*s
));
3085 ret
= inflateInit(&s
->zstream
);
3091 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3095 s
->zstream
.avail_out
= len
;
3096 s
->zstream
.next_out
= buf
;
3097 while (s
->zstream
.avail_out
> 0) {
3098 if (s
->zstream
.avail_in
== 0) {
3099 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3101 clen
= qemu_get_be16(s
->f
);
3102 if (clen
> IOBUF_SIZE
)
3104 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3105 s
->zstream
.avail_in
= clen
;
3106 s
->zstream
.next_in
= s
->buf
;
3108 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3109 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3116 static void ram_decompress_close(RamDecompressState
*s
)
3118 inflateEnd(&s
->zstream
);
3121 #define RAM_SAVE_FLAG_FULL 0x01
3122 #define RAM_SAVE_FLAG_COMPRESS 0x02
3123 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3124 #define RAM_SAVE_FLAG_PAGE 0x08
3125 #define RAM_SAVE_FLAG_EOS 0x10
3127 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3129 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3130 uint32_t *array
= (uint32_t *)page
;
3133 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3134 if (array
[i
] != val
)
3141 static int ram_save_block(QEMUFile
*f
)
3143 static ram_addr_t current_addr
= 0;
3144 ram_addr_t saved_addr
= current_addr
;
3145 ram_addr_t addr
= 0;
3148 while (addr
< phys_ram_size
) {
3149 if (kvm_enabled() && current_addr
== 0)
3150 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3151 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3154 cpu_physical_memory_reset_dirty(current_addr
,
3155 current_addr
+ TARGET_PAGE_SIZE
,
3156 MIGRATION_DIRTY_FLAG
);
3158 ch
= *(phys_ram_base
+ current_addr
);
3160 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3161 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3162 qemu_put_byte(f
, ch
);
3164 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3165 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3171 addr
+= TARGET_PAGE_SIZE
;
3172 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3178 static ram_addr_t ram_save_threshold
= 10;
3180 static ram_addr_t
ram_save_remaining(void)
3183 ram_addr_t count
= 0;
3185 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3186 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3193 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3198 /* Make sure all dirty bits are set */
3199 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3200 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3201 cpu_physical_memory_set_dirty(addr
);
3204 /* Enable dirty memory tracking */
3205 cpu_physical_memory_set_dirty_tracking(1);
3207 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3210 while (!qemu_file_rate_limit(f
)) {
3213 ret
= ram_save_block(f
);
3214 if (ret
== 0) /* no more blocks */
3218 /* try transferring iterative blocks of memory */
3221 cpu_physical_memory_set_dirty_tracking(0);
3223 /* flush all remaining blocks regardless of rate limiting */
3224 while (ram_save_block(f
) != 0);
3227 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3229 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3232 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3234 RamDecompressState s1
, *s
= &s1
;
3238 if (ram_decompress_open(s
, f
) < 0)
3240 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3241 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3243 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3244 fprintf(stderr
, "Error while reading ram block header\n");
3248 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3249 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3254 printf("Error block header\n");
3258 ram_decompress_close(s
);
3263 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3268 if (version_id
== 1)
3269 return ram_load_v1(f
, opaque
);
3271 if (version_id
== 2) {
3272 if (qemu_get_be32(f
) != phys_ram_size
)
3274 return ram_load_dead(f
, opaque
);
3277 if (version_id
!= 3)
3281 addr
= qemu_get_be64(f
);
3283 flags
= addr
& ~TARGET_PAGE_MASK
;
3284 addr
&= TARGET_PAGE_MASK
;
3286 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3287 if (addr
!= phys_ram_size
)
3291 if (flags
& RAM_SAVE_FLAG_FULL
) {
3292 if (ram_load_dead(f
, opaque
) < 0)
3296 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3297 uint8_t ch
= qemu_get_byte(f
);
3298 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3299 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3300 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3301 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3306 /***********************************************************/
3307 /* bottom halves (can be seen as timers which expire ASAP) */
3318 static QEMUBH
*first_bh
= NULL
;
3320 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3323 bh
= qemu_mallocz(sizeof(QEMUBH
));
3327 bh
->opaque
= opaque
;
3328 bh
->next
= first_bh
;
3333 int qemu_bh_poll(void)
3339 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3340 if (!bh
->deleted
&& bh
->scheduled
) {
3349 /* remove deleted bhs */
3363 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3371 void qemu_bh_schedule(QEMUBH
*bh
)
3373 CPUState
*env
= cpu_single_env
;
3378 /* stop the currently executing CPU to execute the BH ASAP */
3380 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3385 void qemu_bh_cancel(QEMUBH
*bh
)
3390 void qemu_bh_delete(QEMUBH
*bh
)
3396 static void qemu_bh_update_timeout(int *timeout
)
3400 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3401 if (!bh
->deleted
&& bh
->scheduled
) {
3403 /* idle bottom halves will be polled at least
3405 *timeout
= MIN(10, *timeout
);
3407 /* non-idle bottom halves will be executed
3416 /***********************************************************/
3417 /* machine registration */
3419 static QEMUMachine
*first_machine
= NULL
;
3420 QEMUMachine
*current_machine
= NULL
;
3422 int qemu_register_machine(QEMUMachine
*m
)
3425 pm
= &first_machine
;
3433 static QEMUMachine
*find_machine(const char *name
)
3437 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3438 if (!strcmp(m
->name
, name
))
3444 /***********************************************************/
3445 /* main execution loop */
3447 static void gui_update(void *opaque
)
3449 DisplayState
*ds
= opaque
;
3450 ds
->dpy_refresh(ds
);
3451 qemu_mod_timer(ds
->gui_timer
,
3452 (ds
->gui_timer_interval
?
3453 ds
->gui_timer_interval
:
3454 GUI_REFRESH_INTERVAL
)
3455 + qemu_get_clock(rt_clock
));
3458 struct vm_change_state_entry
{
3459 VMChangeStateHandler
*cb
;
3461 LIST_ENTRY (vm_change_state_entry
) entries
;
3464 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3466 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3469 VMChangeStateEntry
*e
;
3471 e
= qemu_mallocz(sizeof (*e
));
3477 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3481 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3483 LIST_REMOVE (e
, entries
);
3487 static void vm_state_notify(int running
)
3489 VMChangeStateEntry
*e
;
3491 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3492 e
->cb(e
->opaque
, running
);
3496 /* XXX: support several handlers */
3497 static VMStopHandler
*vm_stop_cb
;
3498 static void *vm_stop_opaque
;
3500 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3503 vm_stop_opaque
= opaque
;
3507 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3518 qemu_rearm_alarm_timer(alarm_timer
);
3522 void vm_stop(int reason
)
3525 cpu_disable_ticks();
3529 vm_stop_cb(vm_stop_opaque
, reason
);
3536 /* reset/shutdown handler */
3538 typedef struct QEMUResetEntry
{
3539 QEMUResetHandler
*func
;
3541 struct QEMUResetEntry
*next
;
3544 static QEMUResetEntry
*first_reset_entry
;
3545 static int reset_requested
;
3546 static int shutdown_requested
;
3547 static int powerdown_requested
;
3549 int qemu_shutdown_requested(void)
3551 int r
= shutdown_requested
;
3552 shutdown_requested
= 0;
3556 int qemu_reset_requested(void)
3558 int r
= reset_requested
;
3559 reset_requested
= 0;
3563 int qemu_powerdown_requested(void)
3565 int r
= powerdown_requested
;
3566 powerdown_requested
= 0;
3570 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3572 QEMUResetEntry
**pre
, *re
;
3574 pre
= &first_reset_entry
;
3575 while (*pre
!= NULL
)
3576 pre
= &(*pre
)->next
;
3577 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3579 re
->opaque
= opaque
;
3584 void qemu_system_reset(void)
3588 /* reset all devices */
3589 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3590 re
->func(re
->opaque
);
3594 void qemu_system_reset_request(void)
3597 shutdown_requested
= 1;
3599 reset_requested
= 1;
3602 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3606 void qemu_system_shutdown_request(void)
3608 shutdown_requested
= 1;
3610 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3613 void qemu_system_powerdown_request(void)
3615 powerdown_requested
= 1;
3617 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3620 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3625 /* KVM holds a mutex while QEMU code is running, we need hooks to
3626 release the mutex whenever QEMU code sleeps. */
3630 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3638 void host_main_loop_wait(int *timeout
)
3644 /* XXX: need to suppress polling by better using win32 events */
3646 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3647 ret
|= pe
->func(pe
->opaque
);
3651 WaitObjects
*w
= &wait_objects
;
3653 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3654 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3655 if (w
->func
[ret
- WAIT_OBJECT_0
])
3656 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3658 /* Check for additional signaled events */
3659 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3661 /* Check if event is signaled */
3662 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3663 if(ret2
== WAIT_OBJECT_0
) {
3665 w
->func
[i
](w
->opaque
[i
]);
3666 } else if (ret2
== WAIT_TIMEOUT
) {
3668 err
= GetLastError();
3669 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3672 } else if (ret
== WAIT_TIMEOUT
) {
3674 err
= GetLastError();
3675 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3682 void host_main_loop_wait(int *timeout
)
3687 void main_loop_wait(int timeout
)
3689 IOHandlerRecord
*ioh
;
3690 fd_set rfds
, wfds
, xfds
;
3694 qemu_bh_update_timeout(&timeout
);
3696 host_main_loop_wait(&timeout
);
3698 /* poll any events */
3699 /* XXX: separate device handlers from system ones */
3704 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3708 (!ioh
->fd_read_poll
||
3709 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3710 FD_SET(ioh
->fd
, &rfds
);
3714 if (ioh
->fd_write
) {
3715 FD_SET(ioh
->fd
, &wfds
);
3721 tv
.tv_sec
= timeout
/ 1000;
3722 tv
.tv_usec
= (timeout
% 1000) * 1000;
3724 #if defined(CONFIG_SLIRP)
3725 if (slirp_is_inited()) {
3726 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3729 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3731 IOHandlerRecord
**pioh
;
3733 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3734 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3735 ioh
->fd_read(ioh
->opaque
);
3736 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3737 FD_CLR(ioh
->fd
, &rfds
);
3739 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3740 ioh
->fd_write(ioh
->opaque
);
3744 /* remove deleted IO handlers */
3745 pioh
= &first_io_handler
;
3755 #if defined(CONFIG_SLIRP)
3756 if (slirp_is_inited()) {
3762 slirp_select_poll(&rfds
, &wfds
, &xfds
);
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 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4078 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4079 " expose a PCI device to the guest OS.\n"
4080 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4081 " 'string' is used in log output.\n"
4085 "-no-acpi disable ACPI\n"
4087 #ifdef CONFIG_CURSES
4088 "-curses use a curses/ncurses interface instead of SDL\n"
4090 "-no-reboot exit instead of rebooting\n"
4091 "-no-shutdown stop before shutdown\n"
4092 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4093 "-vnc display start a VNC server on display\n"
4095 "-daemonize daemonize QEMU after initializing\n"
4097 "-tdf inject timer interrupts that got lost\n"
4098 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4099 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also enables allocation of guest memory with huge pages\n"
4100 "-option-rom rom load a file, rom, into the option ROM space\n"
4102 "-prom-env variable=value set OpenBIOS nvram variables\n"
4104 "-clock force the use of the given methods for timer alarm.\n"
4105 " To see what timers are available use -clock ?\n"
4106 "-startdate select initial date of the clock\n"
4107 "-icount [N|auto]\n"
4108 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4110 "During emulation, the following keys are useful:\n"
4111 "ctrl-alt-f toggle full screen\n"
4112 "ctrl-alt-n switch to virtual console 'n'\n"
4113 "ctrl-alt toggle mouse and keyboard grab\n"
4115 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4120 DEFAULT_NETWORK_SCRIPT
,
4121 DEFAULT_NETWORK_DOWN_SCRIPT
,
4123 DEFAULT_GDBSTUB_PORT
,
4128 #define HAS_ARG 0x0001
4143 QEMU_OPTION_mtdblock
,
4147 QEMU_OPTION_snapshot
,
4149 QEMU_OPTION_no_fd_bootchk
,
4152 QEMU_OPTION_nographic
,
4153 QEMU_OPTION_portrait
,
4155 QEMU_OPTION_audio_help
,
4156 QEMU_OPTION_soundhw
,
4178 QEMU_OPTION_localtime
,
4182 QEMU_OPTION_monitor
,
4184 QEMU_OPTION_parallel
,
4186 QEMU_OPTION_full_screen
,
4187 QEMU_OPTION_no_frame
,
4188 QEMU_OPTION_alt_grab
,
4189 QEMU_OPTION_no_quit
,
4190 QEMU_OPTION_pidfile
,
4191 QEMU_OPTION_no_kqemu
,
4192 QEMU_OPTION_kernel_kqemu
,
4193 QEMU_OPTION_enable_kvm
,
4194 QEMU_OPTION_win2k_hack
,
4196 QEMU_OPTION_usbdevice
,
4199 QEMU_OPTION_no_acpi
,
4202 QEMU_OPTION_no_kvm_irqchip
,
4203 QEMU_OPTION_no_kvm_pit
,
4204 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4205 QEMU_OPTION_pcidevice
,
4207 QEMU_OPTION_no_reboot
,
4208 QEMU_OPTION_no_shutdown
,
4209 QEMU_OPTION_show_cursor
,
4210 QEMU_OPTION_daemonize
,
4211 QEMU_OPTION_option_rom
,
4212 QEMU_OPTION_semihosting
,
4213 QEMU_OPTION_cpu_vendor
,
4215 QEMU_OPTION_prom_env
,
4216 QEMU_OPTION_old_param
,
4218 QEMU_OPTION_startdate
,
4219 QEMU_OPTION_tb_size
,
4222 QEMU_OPTION_incoming
,
4224 QEMU_OPTION_kvm_shadow_memory
,
4225 QEMU_OPTION_mempath
,
4228 typedef struct QEMUOption
{
4234 static const QEMUOption qemu_options
[] = {
4235 { "h", 0, QEMU_OPTION_h
},
4236 { "help", 0, QEMU_OPTION_h
},
4238 { "M", HAS_ARG
, QEMU_OPTION_M
},
4239 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4240 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4241 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4242 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4243 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4244 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4245 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4246 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4247 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4248 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4249 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4250 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4251 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4252 { "snapshot", 0, QEMU_OPTION_snapshot
},
4254 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4256 { "m", HAS_ARG
, QEMU_OPTION_m
},
4257 { "nographic", 0, QEMU_OPTION_nographic
},
4258 { "portrait", 0, QEMU_OPTION_portrait
},
4259 { "k", HAS_ARG
, QEMU_OPTION_k
},
4261 { "audio-help", 0, QEMU_OPTION_audio_help
},
4262 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4265 { "net", HAS_ARG
, QEMU_OPTION_net
},
4267 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4268 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4270 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4272 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4274 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4276 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4277 { "append", HAS_ARG
, QEMU_OPTION_append
},
4278 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4280 { "S", 0, QEMU_OPTION_S
},
4281 { "s", 0, QEMU_OPTION_s
},
4282 { "p", HAS_ARG
, QEMU_OPTION_p
},
4283 { "d", HAS_ARG
, QEMU_OPTION_d
},
4284 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4285 { "L", HAS_ARG
, QEMU_OPTION_L
},
4286 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4288 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4289 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4292 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4295 #ifndef NO_CPU_EMULATION
4296 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4298 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4299 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4300 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4301 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4304 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4305 { "g", 1, QEMU_OPTION_g
},
4307 { "localtime", 0, QEMU_OPTION_localtime
},
4308 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4309 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4310 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4311 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4312 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4313 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4314 { "incoming", 1, QEMU_OPTION_incoming
},
4315 { "full-screen", 0, QEMU_OPTION_full_screen
},
4317 { "no-frame", 0, QEMU_OPTION_no_frame
},
4318 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4319 { "no-quit", 0, QEMU_OPTION_no_quit
},
4321 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4322 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4323 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4324 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4325 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4326 #ifdef CONFIG_CURSES
4327 { "curses", 0, QEMU_OPTION_curses
},
4329 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4331 /* temporary options */
4332 { "usb", 0, QEMU_OPTION_usb
},
4333 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4334 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4335 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4336 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4337 { "daemonize", 0, QEMU_OPTION_daemonize
},
4338 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4339 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4340 { "semihosting", 0, QEMU_OPTION_semihosting
},
4342 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4343 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4344 { "name", HAS_ARG
, QEMU_OPTION_name
},
4345 #if defined(TARGET_SPARC)
4346 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4348 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4349 #if defined(TARGET_ARM)
4350 { "old-param", 0, QEMU_OPTION_old_param
},
4352 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4353 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4354 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4355 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4356 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4357 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4361 /* password input */
4363 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4368 if (!bdrv_is_encrypted(bs
))
4371 term_printf("%s is encrypted.\n", name
);
4372 for(i
= 0; i
< 3; i
++) {
4373 monitor_readline("Password: ", 1, password
, sizeof(password
));
4374 if (bdrv_set_key(bs
, password
) == 0)
4376 term_printf("invalid password\n");
4381 static BlockDriverState
*get_bdrv(int index
)
4383 if (index
> nb_drives
)
4385 return drives_table
[index
].bdrv
;
4388 static void read_passwords(void)
4390 BlockDriverState
*bs
;
4393 for(i
= 0; i
< 6; i
++) {
4396 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4401 struct soundhw soundhw
[] = {
4402 #ifdef HAS_AUDIO_CHOICE
4403 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4409 { .init_isa
= pcspk_audio_init
}
4414 "Creative Sound Blaster 16",
4417 { .init_isa
= SB16_init
}
4420 #ifdef CONFIG_CS4231A
4426 { .init_isa
= cs4231a_init
}
4434 "Yamaha YMF262 (OPL3)",
4436 "Yamaha YM3812 (OPL2)",
4440 { .init_isa
= Adlib_init
}
4447 "Gravis Ultrasound GF1",
4450 { .init_isa
= GUS_init
}
4457 "Intel 82801AA AC97 Audio",
4460 { .init_pci
= ac97_init
}
4466 "ENSONIQ AudioPCI ES1370",
4469 { .init_pci
= es1370_init
}
4473 { NULL
, NULL
, 0, 0, { NULL
} }
4476 static void select_soundhw (const char *optarg
)
4480 if (*optarg
== '?') {
4483 printf ("Valid sound card names (comma separated):\n");
4484 for (c
= soundhw
; c
->name
; ++c
) {
4485 printf ("%-11s %s\n", c
->name
, c
->descr
);
4487 printf ("\n-soundhw all will enable all of the above\n");
4488 exit (*optarg
!= '?');
4496 if (!strcmp (optarg
, "all")) {
4497 for (c
= soundhw
; c
->name
; ++c
) {
4505 e
= strchr (p
, ',');
4506 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4508 for (c
= soundhw
; c
->name
; ++c
) {
4509 if (!strncmp (c
->name
, p
, l
)) {
4518 "Unknown sound card name (too big to show)\n");
4521 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4526 p
+= l
+ (e
!= NULL
);
4530 goto show_valid_cards
;
4535 static void select_vgahw (const char *p
)
4539 if (strstart(p
, "std", &opts
)) {
4540 cirrus_vga_enabled
= 0;
4542 } else if (strstart(p
, "cirrus", &opts
)) {
4543 cirrus_vga_enabled
= 1;
4545 } else if (strstart(p
, "vmware", &opts
)) {
4546 cirrus_vga_enabled
= 0;
4550 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4554 const char *nextopt
;
4556 if (strstart(opts
, ",retrace=", &nextopt
)) {
4558 if (strstart(opts
, "dumb", &nextopt
))
4559 vga_retrace_method
= VGA_RETRACE_DUMB
;
4560 else if (strstart(opts
, "precise", &nextopt
))
4561 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4562 else goto invalid_vga
;
4563 } else goto invalid_vga
;
4569 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4571 exit(STATUS_CONTROL_C_EXIT
);
4576 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4580 if(strlen(str
) != 36)
4583 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4584 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4585 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4593 #define MAX_NET_CLIENTS 32
4595 static int saved_argc
;
4596 static char **saved_argv
;
4598 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4602 *opt_daemonize
= daemonize
;
4603 *opt_incoming
= incoming
;
4607 static int gethugepagesize(void)
4611 char *needle
= "Hugepagesize:";
4613 unsigned long hugepagesize
;
4615 fd
= open("/proc/meminfo", O_RDONLY
);
4621 ret
= read(fd
, buf
, sizeof(buf
));
4627 size
= strstr(buf
, needle
);
4630 size
+= strlen(needle
);
4631 hugepagesize
= strtol(size
, NULL
, 0);
4632 return hugepagesize
;
4635 void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4641 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4644 hpagesize
= gethugepagesize() * 1024;
4648 fd
= mkstemp(filename
);
4657 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4660 * ftruncate is not supported by hugetlbfs in older
4661 * hosts, so don't bother checking for errors.
4662 * If anything goes wrong with it under other filesystems,
4665 ftruncate(fd
, memory
);
4667 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4668 if (area
== MAP_FAILED
) {
4678 void *qemu_alloc_physram(unsigned long memory
)
4681 unsigned long map_len
= memory
;
4685 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4688 area
= qemu_vmalloc(memory
);
4690 if (kvm_setup_guest_memory(area
, map_len
))
4698 static void termsig_handler(int signal
)
4700 qemu_system_shutdown_request();
4703 static void termsig_setup(void)
4705 struct sigaction act
;
4707 memset(&act
, 0, sizeof(act
));
4708 act
.sa_handler
= termsig_handler
;
4709 sigaction(SIGINT
, &act
, NULL
);
4710 sigaction(SIGHUP
, &act
, NULL
);
4711 sigaction(SIGTERM
, &act
, NULL
);
4716 int main(int argc
, char **argv
)
4718 #ifdef CONFIG_GDBSTUB
4720 const char *gdbstub_port
;
4722 uint32_t boot_devices_bitmap
= 0;
4724 int snapshot
, linux_boot
, net_boot
;
4725 const char *initrd_filename
;
4726 const char *kernel_filename
, *kernel_cmdline
;
4727 const char *boot_devices
= "";
4728 DisplayState
*ds
= &display_state
;
4729 int cyls
, heads
, secs
, translation
;
4730 const char *net_clients
[MAX_NET_CLIENTS
];
4732 const char *bt_opts
[MAX_BT_CMDLINE
];
4736 const char *r
, *optarg
;
4737 CharDriverState
*monitor_hd
;
4738 const char *monitor_device
;
4739 const char *serial_devices
[MAX_SERIAL_PORTS
];
4740 int serial_device_index
;
4741 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4742 int parallel_device_index
;
4743 const char *loadvm
= NULL
;
4744 QEMUMachine
*machine
;
4745 const char *cpu_model
;
4746 const char *usb_devices
[MAX_USB_CMDLINE
];
4747 int usb_devices_index
;
4750 const char *pid_file
= NULL
;
4752 const char *incoming
= NULL
;
4754 LIST_INIT (&vm_change_state_head
);
4757 struct sigaction act
;
4758 sigfillset(&act
.sa_mask
);
4760 act
.sa_handler
= SIG_IGN
;
4761 sigaction(SIGPIPE
, &act
, NULL
);
4764 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4765 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4766 QEMU to run on a single CPU */
4771 h
= GetCurrentProcess();
4772 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4773 for(i
= 0; i
< 32; i
++) {
4774 if (mask
& (1 << i
))
4779 SetProcessAffinityMask(h
, mask
);
4785 register_machines();
4786 machine
= first_machine
;
4788 initrd_filename
= NULL
;
4790 vga_ram_size
= VGA_RAM_SIZE
;
4791 #ifdef CONFIG_GDBSTUB
4793 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4798 kernel_filename
= NULL
;
4799 kernel_cmdline
= "";
4800 cyls
= heads
= secs
= 0;
4801 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4802 monitor_device
= "vc";
4804 serial_devices
[0] = "vc:80Cx24C";
4805 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4806 serial_devices
[i
] = NULL
;
4807 serial_device_index
= 0;
4809 parallel_devices
[0] = "vc:640x480";
4810 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4811 parallel_devices
[i
] = NULL
;
4812 parallel_device_index
= 0;
4814 usb_devices_index
= 0;
4815 assigned_devices_index
= 0;
4834 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4836 const QEMUOption
*popt
;
4839 /* Treat --foo the same as -foo. */
4842 popt
= qemu_options
;
4845 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4849 if (!strcmp(popt
->name
, r
+ 1))
4853 if (popt
->flags
& HAS_ARG
) {
4854 if (optind
>= argc
) {
4855 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4859 optarg
= argv
[optind
++];
4864 switch(popt
->index
) {
4866 machine
= find_machine(optarg
);
4869 printf("Supported machines are:\n");
4870 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4871 printf("%-10s %s%s\n",
4873 m
== first_machine
? " (default)" : "");
4875 exit(*optarg
!= '?');
4878 case QEMU_OPTION_cpu
:
4879 /* hw initialization will check this */
4880 if (*optarg
== '?') {
4881 /* XXX: implement xxx_cpu_list for targets that still miss it */
4882 #if defined(cpu_list)
4883 cpu_list(stdout
, &fprintf
);
4890 case QEMU_OPTION_initrd
:
4891 initrd_filename
= optarg
;
4893 case QEMU_OPTION_hda
:
4895 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4897 hda_index
= drive_add(optarg
, HD_ALIAS
4898 ",cyls=%d,heads=%d,secs=%d%s",
4899 0, cyls
, heads
, secs
,
4900 translation
== BIOS_ATA_TRANSLATION_LBA
?
4902 translation
== BIOS_ATA_TRANSLATION_NONE
?
4903 ",trans=none" : "");
4905 case QEMU_OPTION_hdb
:
4906 case QEMU_OPTION_hdc
:
4907 case QEMU_OPTION_hdd
:
4908 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4910 case QEMU_OPTION_drive
:
4911 drive_add(NULL
, "%s", optarg
);
4913 case QEMU_OPTION_mtdblock
:
4914 drive_add(optarg
, MTD_ALIAS
);
4916 case QEMU_OPTION_sd
:
4917 drive_add(optarg
, SD_ALIAS
);
4919 case QEMU_OPTION_pflash
:
4920 drive_add(optarg
, PFLASH_ALIAS
);
4922 case QEMU_OPTION_snapshot
:
4925 case QEMU_OPTION_hdachs
:
4929 cyls
= strtol(p
, (char **)&p
, 0);
4930 if (cyls
< 1 || cyls
> 16383)
4935 heads
= strtol(p
, (char **)&p
, 0);
4936 if (heads
< 1 || heads
> 16)
4941 secs
= strtol(p
, (char **)&p
, 0);
4942 if (secs
< 1 || secs
> 63)
4946 if (!strcmp(p
, "none"))
4947 translation
= BIOS_ATA_TRANSLATION_NONE
;
4948 else if (!strcmp(p
, "lba"))
4949 translation
= BIOS_ATA_TRANSLATION_LBA
;
4950 else if (!strcmp(p
, "auto"))
4951 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4954 } else if (*p
!= '\0') {
4956 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4959 if (hda_index
!= -1)
4960 snprintf(drives_opt
[hda_index
].opt
,
4961 sizeof(drives_opt
[hda_index
].opt
),
4962 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4963 0, cyls
, heads
, secs
,
4964 translation
== BIOS_ATA_TRANSLATION_LBA
?
4966 translation
== BIOS_ATA_TRANSLATION_NONE
?
4967 ",trans=none" : "");
4970 case QEMU_OPTION_nographic
:
4973 #ifdef CONFIG_CURSES
4974 case QEMU_OPTION_curses
:
4978 case QEMU_OPTION_portrait
:
4981 case QEMU_OPTION_kernel
:
4982 kernel_filename
= optarg
;
4984 case QEMU_OPTION_append
:
4985 kernel_cmdline
= optarg
;
4987 case QEMU_OPTION_cdrom
:
4988 drive_add(optarg
, CDROM_ALIAS
);
4990 case QEMU_OPTION_boot
:
4991 boot_devices
= optarg
;
4992 /* We just do some generic consistency checks */
4994 /* Could easily be extended to 64 devices if needed */
4997 boot_devices_bitmap
= 0;
4998 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4999 /* Allowed boot devices are:
5000 * a b : floppy disk drives
5001 * c ... f : IDE disk drives
5002 * g ... m : machine implementation dependant drives
5003 * n ... p : network devices
5004 * It's up to each machine implementation to check
5005 * if the given boot devices match the actual hardware
5006 * implementation and firmware features.
5008 if (*p
< 'a' || *p
> 'q') {
5009 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5012 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5014 "Boot device '%c' was given twice\n",*p
);
5017 boot_devices_bitmap
|= 1 << (*p
- 'a');
5021 case QEMU_OPTION_fda
:
5022 case QEMU_OPTION_fdb
:
5023 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5026 case QEMU_OPTION_no_fd_bootchk
:
5030 case QEMU_OPTION_net
:
5031 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5032 fprintf(stderr
, "qemu: too many network clients\n");
5035 net_clients
[nb_net_clients
] = optarg
;
5039 case QEMU_OPTION_tftp
:
5040 tftp_prefix
= optarg
;
5042 case QEMU_OPTION_bootp
:
5043 bootp_filename
= optarg
;
5046 case QEMU_OPTION_smb
:
5047 net_slirp_smb(optarg
);
5050 case QEMU_OPTION_redir
:
5051 net_slirp_redir(optarg
);
5054 case QEMU_OPTION_bt
:
5055 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5056 fprintf(stderr
, "qemu: too many bluetooth options\n");
5059 bt_opts
[nb_bt_opts
++] = optarg
;
5062 case QEMU_OPTION_audio_help
:
5066 case QEMU_OPTION_soundhw
:
5067 select_soundhw (optarg
);
5073 case QEMU_OPTION_m
: {
5077 value
= strtoul(optarg
, &ptr
, 10);
5079 case 0: case 'M': case 'm':
5086 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5090 /* On 32-bit hosts, QEMU is limited by virtual address space */
5091 if (value
> (2047 << 20)
5093 && HOST_LONG_BITS
== 32
5096 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5099 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5100 fprintf(stderr
, "qemu: ram size too large\n");
5109 const CPULogItem
*item
;
5111 mask
= cpu_str_to_log_mask(optarg
);
5113 printf("Log items (comma separated):\n");
5114 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5115 printf("%-10s %s\n", item
->name
, item
->help
);
5122 #ifdef CONFIG_GDBSTUB
5127 gdbstub_port
= optarg
;
5133 case QEMU_OPTION_bios
:
5140 keyboard_layout
= optarg
;
5142 case QEMU_OPTION_localtime
:
5145 case QEMU_OPTION_vga
:
5146 select_vgahw (optarg
);
5153 w
= strtol(p
, (char **)&p
, 10);
5156 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5162 h
= strtol(p
, (char **)&p
, 10);
5167 depth
= strtol(p
, (char **)&p
, 10);
5168 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5169 depth
!= 24 && depth
!= 32)
5171 } else if (*p
== '\0') {
5172 depth
= graphic_depth
;
5179 graphic_depth
= depth
;
5182 case QEMU_OPTION_echr
:
5185 term_escape_char
= strtol(optarg
, &r
, 0);
5187 printf("Bad argument to echr\n");
5190 case QEMU_OPTION_monitor
:
5191 monitor_device
= optarg
;
5193 case QEMU_OPTION_serial
:
5194 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5195 fprintf(stderr
, "qemu: too many serial ports\n");
5198 serial_devices
[serial_device_index
] = optarg
;
5199 serial_device_index
++;
5201 case QEMU_OPTION_parallel
:
5202 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5203 fprintf(stderr
, "qemu: too many parallel ports\n");
5206 parallel_devices
[parallel_device_index
] = optarg
;
5207 parallel_device_index
++;
5209 case QEMU_OPTION_loadvm
:
5212 case QEMU_OPTION_full_screen
:
5216 case QEMU_OPTION_no_frame
:
5219 case QEMU_OPTION_alt_grab
:
5222 case QEMU_OPTION_no_quit
:
5226 case QEMU_OPTION_pidfile
:
5230 case QEMU_OPTION_win2k_hack
:
5231 win2k_install_hack
= 1;
5235 case QEMU_OPTION_no_kqemu
:
5238 case QEMU_OPTION_kernel_kqemu
:
5243 case QEMU_OPTION_enable_kvm
:
5251 case QEMU_OPTION_no_kvm
:
5254 case QEMU_OPTION_no_kvm_irqchip
: {
5255 extern int kvm_irqchip
, kvm_pit
;
5260 case QEMU_OPTION_no_kvm_pit
: {
5265 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5266 case QEMU_OPTION_pcidevice
:
5267 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5268 fprintf(stderr
, "Too many assigned devices\n");
5271 assigned_devices
[assigned_devices_index
] = optarg
;
5272 assigned_devices_index
++;
5276 case QEMU_OPTION_usb
:
5279 case QEMU_OPTION_usbdevice
:
5281 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5282 fprintf(stderr
, "Too many USB devices\n");
5285 usb_devices
[usb_devices_index
] = optarg
;
5286 usb_devices_index
++;
5288 case QEMU_OPTION_smp
:
5289 smp_cpus
= atoi(optarg
);
5291 fprintf(stderr
, "Invalid number of CPUs\n");
5295 case QEMU_OPTION_vnc
:
5296 vnc_display
= optarg
;
5298 case QEMU_OPTION_no_acpi
:
5301 case QEMU_OPTION_no_reboot
:
5304 case QEMU_OPTION_no_shutdown
:
5307 case QEMU_OPTION_show_cursor
:
5310 case QEMU_OPTION_uuid
:
5311 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5312 fprintf(stderr
, "Fail to parse UUID string."
5313 " Wrong format.\n");
5317 case QEMU_OPTION_daemonize
:
5320 case QEMU_OPTION_option_rom
:
5321 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5322 fprintf(stderr
, "Too many option ROMs\n");
5325 option_rom
[nb_option_roms
] = optarg
;
5328 case QEMU_OPTION_semihosting
:
5329 semihosting_enabled
= 1;
5331 case QEMU_OPTION_tdf
:
5334 case QEMU_OPTION_kvm_shadow_memory
:
5335 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5337 case QEMU_OPTION_mempath
:
5340 case QEMU_OPTION_name
:
5344 case QEMU_OPTION_prom_env
:
5345 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5346 fprintf(stderr
, "Too many prom variables\n");
5349 prom_envs
[nb_prom_envs
] = optarg
;
5353 case QEMU_OPTION_cpu_vendor
:
5354 cpu_vendor_string
= optarg
;
5357 case QEMU_OPTION_old_param
:
5361 case QEMU_OPTION_clock
:
5362 configure_alarms(optarg
);
5364 case QEMU_OPTION_startdate
:
5367 time_t rtc_start_date
;
5368 if (!strcmp(optarg
, "now")) {
5369 rtc_date_offset
= -1;
5371 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5379 } else if (sscanf(optarg
, "%d-%d-%d",
5382 &tm
.tm_mday
) == 3) {
5391 rtc_start_date
= mktimegm(&tm
);
5392 if (rtc_start_date
== -1) {
5394 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5395 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5398 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5402 case QEMU_OPTION_tb_size
:
5403 tb_size
= strtol(optarg
, NULL
, 0);
5407 case QEMU_OPTION_icount
:
5409 if (strcmp(optarg
, "auto") == 0) {
5410 icount_time_shift
= -1;
5412 icount_time_shift
= strtol(optarg
, NULL
, 0);
5415 case QEMU_OPTION_incoming
:
5422 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5423 if (kvm_allowed
&& kqemu_allowed
) {
5425 "You can not enable both KVM and kqemu at the same time\n");
5430 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5431 if (smp_cpus
> machine
->max_cpus
) {
5432 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5433 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5439 if (serial_device_index
== 0)
5440 serial_devices
[0] = "stdio";
5441 if (parallel_device_index
== 0)
5442 parallel_devices
[0] = "null";
5443 if (strncmp(monitor_device
, "vc", 2) == 0)
5444 monitor_device
= "stdio";
5451 if (pipe(fds
) == -1)
5462 len
= read(fds
[0], &status
, 1);
5463 if (len
== -1 && (errno
== EINTR
))
5468 else if (status
== 1) {
5469 fprintf(stderr
, "Could not acquire pidfile\n");
5486 signal(SIGTSTP
, SIG_IGN
);
5487 signal(SIGTTOU
, SIG_IGN
);
5488 signal(SIGTTIN
, SIG_IGN
);
5493 if (kvm_enabled()) {
5494 if (kvm_qemu_init() < 0) {
5495 extern int kvm_allowed
;
5496 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5497 #ifdef NO_CPU_EMULATION
5498 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5506 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5509 write(fds
[1], &status
, 1);
5511 fprintf(stderr
, "Could not acquire pid file\n");
5519 linux_boot
= (kernel_filename
!= NULL
);
5520 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5522 if (!linux_boot
&& net_boot
== 0 &&
5523 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5526 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5527 fprintf(stderr
, "-append only allowed with -kernel option\n");
5531 if (!linux_boot
&& initrd_filename
!= NULL
) {
5532 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5536 /* boot to floppy or the default cd if no hard disk defined yet */
5537 if (!boot_devices
[0]) {
5538 boot_devices
= "cad";
5540 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5543 if (init_timer_alarm() < 0) {
5544 fprintf(stderr
, "could not initialize alarm timer\n");
5547 if (use_icount
&& icount_time_shift
< 0) {
5549 /* 125MIPS seems a reasonable initial guess at the guest speed.
5550 It will be corrected fairly quickly anyway. */
5551 icount_time_shift
= 3;
5552 init_icount_adjust();
5559 /* init network clients */
5560 if (nb_net_clients
== 0) {
5561 /* if no clients, we use a default config */
5562 net_clients
[nb_net_clients
++] = "nic";
5564 net_clients
[nb_net_clients
++] = "user";
5568 for(i
= 0;i
< nb_net_clients
; i
++) {
5569 if (net_client_parse(net_clients
[i
]) < 0)
5575 /* XXX: this should be moved in the PC machine instantiation code */
5576 if (net_boot
!= 0) {
5578 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5579 const char *model
= nd_table
[i
].model
;
5581 if (net_boot
& (1 << i
)) {
5584 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5585 if (get_image_size(buf
) > 0) {
5586 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5587 fprintf(stderr
, "Too many option ROMs\n");
5590 option_rom
[nb_option_roms
] = strdup(buf
);
5597 fprintf(stderr
, "No valid PXE rom found for network device\n");
5603 /* init the bluetooth world */
5604 for (i
= 0; i
< nb_bt_opts
; i
++)
5605 if (bt_parse(bt_opts
[i
]))
5608 /* init the memory */
5609 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5611 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5613 if (ram_size
< phys_ram_size
) {
5614 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5615 machine
->name
, (unsigned long long) phys_ram_size
);
5619 phys_ram_size
= ram_size
;
5621 ram_size
= phys_ram_size
;
5624 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5626 phys_ram_size
+= ram_size
;
5629 /* Initialize kvm */
5630 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5631 #define KVM_EXTRA_PAGES 3
5633 #define KVM_EXTRA_PAGES 0
5635 if (kvm_enabled()) {
5636 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5637 if (kvm_qemu_create_context() < 0) {
5638 fprintf(stderr
, "Could not create KVM context\n");
5643 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5644 if (!phys_ram_base
) {
5645 fprintf(stderr
, "Could not allocate physical memory\n");
5649 /* init the dynamic translator */
5650 cpu_exec_init_all(tb_size
* 1024 * 1024);
5654 /* we always create the cdrom drive, even if no disk is there */
5656 if (nb_drives_opt
< MAX_DRIVES
)
5657 drive_add(NULL
, CDROM_ALIAS
);
5659 /* we always create at least one floppy */
5661 if (nb_drives_opt
< MAX_DRIVES
)
5662 drive_add(NULL
, FD_ALIAS
, 0);
5664 /* we always create one sd slot, even if no card is in it */
5666 if (nb_drives_opt
< MAX_DRIVES
)
5667 drive_add(NULL
, SD_ALIAS
);
5669 /* open the virtual block devices
5670 * note that migration with device
5671 * hot add/remove is broken.
5673 for(i
= 0; i
< nb_drives_opt
; i
++)
5674 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5677 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5678 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5681 memset(&display_state
, 0, sizeof(display_state
));
5684 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5687 /* nearly nothing to do */
5688 dumb_display_init(ds
);
5689 } else if (vnc_display
!= NULL
) {
5690 vnc_display_init(ds
);
5691 if (vnc_display_open(ds
, vnc_display
) < 0)
5694 #if defined(CONFIG_CURSES)
5696 curses_display_init(ds
, full_screen
);
5700 #if defined(CONFIG_SDL)
5701 sdl_display_init(ds
, full_screen
, no_frame
);
5702 #elif defined(CONFIG_COCOA)
5703 cocoa_display_init(ds
, full_screen
);
5705 dumb_display_init(ds
);
5710 /* must be after terminal init, SDL library changes signal handlers */
5714 /* Maintain compatibility with multiple stdio monitors */
5715 if (!strcmp(monitor_device
,"stdio")) {
5716 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5717 const char *devname
= serial_devices
[i
];
5718 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5719 monitor_device
= NULL
;
5721 } else if (devname
&& !strcmp(devname
,"stdio")) {
5722 monitor_device
= NULL
;
5723 serial_devices
[i
] = "mon:stdio";
5728 if (monitor_device
) {
5729 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5731 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5734 monitor_init(monitor_hd
, !nographic
);
5737 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5738 const char *devname
= serial_devices
[i
];
5739 if (devname
&& strcmp(devname
, "none")) {
5741 snprintf(label
, sizeof(label
), "serial%d", i
);
5742 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5743 if (!serial_hds
[i
]) {
5744 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5748 if (strstart(devname
, "vc", 0))
5749 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5753 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5754 const char *devname
= parallel_devices
[i
];
5755 if (devname
&& strcmp(devname
, "none")) {
5757 snprintf(label
, sizeof(label
), "parallel%d", i
);
5758 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5759 if (!parallel_hds
[i
]) {
5760 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5764 if (strstart(devname
, "vc", 0))
5765 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5773 if (kvm_enabled()) {
5776 ret
= kvm_init(smp_cpus
);
5778 fprintf(stderr
, "failed to initialize KVM\n");
5784 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5785 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5787 current_machine
= machine
;
5789 /* init USB devices */
5791 for(i
= 0; i
< usb_devices_index
; i
++) {
5792 if (usb_device_add(usb_devices
[i
]) < 0) {
5793 fprintf(stderr
, "Warning: could not add USB device %s\n",
5799 if (display_state
.dpy_refresh
) {
5800 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5801 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5804 #ifdef CONFIG_GDBSTUB
5806 /* XXX: use standard host:port notation and modify options
5808 if (gdbserver_start(gdbstub_port
) < 0) {
5809 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5820 autostart
= 0; /* fixme how to deal with -daemonize */
5821 qemu_start_incoming_migration(incoming
);
5825 /* XXX: simplify init */
5838 len
= write(fds
[1], &status
, 1);
5839 if (len
== -1 && (errno
== EINTR
))
5846 TFR(fd
= open("/dev/null", O_RDWR
));