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;
218 static int no_frame
= 0;
220 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
221 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
223 int win2k_install_hack
= 0;
226 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
227 int assigned_devices_index
;
229 const char *vnc_display
;
230 int acpi_enabled
= 1;
235 int graphic_rotate
= 0;
237 const char *incoming
;
238 const char *option_rom
[MAX_OPTION_ROMS
];
240 int semihosting_enabled
= 0;
241 int time_drift_fix
= 0;
242 unsigned int kvm_shadow_memory
= 0;
243 const char *mem_path
= NULL
;
245 const char *cpu_vendor_string
;
249 const char *qemu_name
;
252 unsigned int nb_prom_envs
= 0;
253 const char *prom_envs
[MAX_PROM_ENVS
];
256 struct drive_opt drives_opt
[MAX_DRIVES
];
258 static CPUState
*cur_cpu
;
259 static CPUState
*next_cpu
;
260 static int event_pending
= 1;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
270 uint8_t qemu_uuid
[16];
272 /* KVM runs the main loop in a separate thread. If we update one of the lists
273 * that are polled before or after select(), we need to make sure to break out
274 * of the select() to ensure the new item is serviced.
276 static void main_loop_break(void)
279 qemu_kvm_notify_work();
282 /***********************************************************/
283 /* x86 ISA bus support */
285 target_phys_addr_t isa_mem_base
= 0;
288 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
289 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
291 static uint32_t ioport_read(int index
, uint32_t address
)
293 static IOPortReadFunc
*default_func
[3] = {
294 default_ioport_readb
,
295 default_ioport_readw
,
298 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
300 func
= default_func
[index
];
301 return func(ioport_opaque
[address
], address
);
304 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
306 static IOPortWriteFunc
*default_func
[3] = {
307 default_ioport_writeb
,
308 default_ioport_writew
,
309 default_ioport_writel
311 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
313 func
= default_func
[index
];
314 func(ioport_opaque
[address
], address
, data
);
317 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
319 #ifdef DEBUG_UNUSED_IOPORT
320 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
325 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
327 #ifdef DEBUG_UNUSED_IOPORT
328 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
332 /* default is to make two byte accesses */
333 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
336 data
= ioport_read(0, address
);
337 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
338 data
|= ioport_read(0, address
) << 8;
342 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
344 ioport_write(0, address
, data
& 0xff);
345 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
346 ioport_write(0, address
, (data
>> 8) & 0xff);
349 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
351 #ifdef DEBUG_UNUSED_IOPORT
352 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
357 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
359 #ifdef DEBUG_UNUSED_IOPORT
360 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
364 /* size is the word size in byte */
365 int register_ioport_read(int start
, int length
, int size
,
366 IOPortReadFunc
*func
, void *opaque
)
372 } else if (size
== 2) {
374 } else if (size
== 4) {
377 hw_error("register_ioport_read: invalid size");
380 for(i
= start
; i
< start
+ length
; i
+= size
) {
381 ioport_read_table
[bsize
][i
] = func
;
382 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
383 hw_error("register_ioport_read: invalid opaque");
384 ioport_opaque
[i
] = opaque
;
389 /* size is the word size in byte */
390 int register_ioport_write(int start
, int length
, int size
,
391 IOPortWriteFunc
*func
, void *opaque
)
397 } else if (size
== 2) {
399 } else if (size
== 4) {
402 hw_error("register_ioport_write: invalid size");
405 for(i
= start
; i
< start
+ length
; i
+= size
) {
406 ioport_write_table
[bsize
][i
] = func
;
407 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
408 hw_error("register_ioport_write: invalid opaque");
409 ioport_opaque
[i
] = opaque
;
414 void isa_unassign_ioport(int start
, int length
)
418 for(i
= start
; i
< start
+ length
; i
++) {
419 ioport_read_table
[0][i
] = default_ioport_readb
;
420 ioport_read_table
[1][i
] = default_ioport_readw
;
421 ioport_read_table
[2][i
] = default_ioport_readl
;
423 ioport_write_table
[0][i
] = default_ioport_writeb
;
424 ioport_write_table
[1][i
] = default_ioport_writew
;
425 ioport_write_table
[2][i
] = default_ioport_writel
;
427 ioport_opaque
[i
] = NULL
;
431 /***********************************************************/
433 void cpu_outb(CPUState
*env
, int addr
, int val
)
436 if (loglevel
& CPU_LOG_IOPORT
)
437 fprintf(logfile
, "outb: %04x %02x\n", addr
, val
);
439 ioport_write(0, addr
, val
);
442 env
->last_io_time
= cpu_get_time_fast();
446 void cpu_outw(CPUState
*env
, int addr
, int val
)
449 if (loglevel
& CPU_LOG_IOPORT
)
450 fprintf(logfile
, "outw: %04x %04x\n", addr
, val
);
452 ioport_write(1, addr
, val
);
455 env
->last_io_time
= cpu_get_time_fast();
459 void cpu_outl(CPUState
*env
, int addr
, int val
)
462 if (loglevel
& CPU_LOG_IOPORT
)
463 fprintf(logfile
, "outl: %04x %08x\n", addr
, val
);
465 ioport_write(2, addr
, val
);
468 env
->last_io_time
= cpu_get_time_fast();
472 int cpu_inb(CPUState
*env
, int addr
)
475 val
= ioport_read(0, addr
);
477 if (loglevel
& CPU_LOG_IOPORT
)
478 fprintf(logfile
, "inb : %04x %02x\n", addr
, val
);
482 env
->last_io_time
= cpu_get_time_fast();
487 int cpu_inw(CPUState
*env
, int addr
)
490 val
= ioport_read(1, addr
);
492 if (loglevel
& CPU_LOG_IOPORT
)
493 fprintf(logfile
, "inw : %04x %04x\n", addr
, val
);
497 env
->last_io_time
= cpu_get_time_fast();
502 int cpu_inl(CPUState
*env
, int addr
)
505 val
= ioport_read(2, addr
);
507 if (loglevel
& CPU_LOG_IOPORT
)
508 fprintf(logfile
, "inl : %04x %08x\n", addr
, val
);
512 env
->last_io_time
= cpu_get_time_fast();
517 /***********************************************************/
518 void hw_error(const char *fmt
, ...)
524 fprintf(stderr
, "qemu: hardware error: ");
525 vfprintf(stderr
, fmt
, ap
);
526 fprintf(stderr
, "\n");
527 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
528 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
530 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
532 cpu_dump_state(env
, stderr
, fprintf
, 0);
542 static QEMUBalloonEvent
*qemu_balloon_event
;
543 void *qemu_balloon_event_opaque
;
545 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
547 qemu_balloon_event
= func
;
548 qemu_balloon_event_opaque
= opaque
;
551 void qemu_balloon(ram_addr_t target
)
553 if (qemu_balloon_event
)
554 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
557 ram_addr_t
qemu_balloon_status(void)
559 if (qemu_balloon_event
)
560 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
564 /***********************************************************/
567 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
568 static void *qemu_put_kbd_event_opaque
;
569 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
570 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
572 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
574 qemu_put_kbd_event_opaque
= opaque
;
575 qemu_put_kbd_event
= func
;
578 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
579 void *opaque
, int absolute
,
582 QEMUPutMouseEntry
*s
, *cursor
;
584 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
588 s
->qemu_put_mouse_event
= func
;
589 s
->qemu_put_mouse_event_opaque
= opaque
;
590 s
->qemu_put_mouse_event_absolute
= absolute
;
591 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
594 if (!qemu_put_mouse_event_head
) {
595 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
599 cursor
= qemu_put_mouse_event_head
;
600 while (cursor
->next
!= NULL
)
601 cursor
= cursor
->next
;
604 qemu_put_mouse_event_current
= s
;
609 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
611 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
613 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
616 cursor
= qemu_put_mouse_event_head
;
617 while (cursor
!= NULL
&& cursor
!= entry
) {
619 cursor
= cursor
->next
;
622 if (cursor
== NULL
) // does not exist or list empty
624 else if (prev
== NULL
) { // entry is head
625 qemu_put_mouse_event_head
= cursor
->next
;
626 if (qemu_put_mouse_event_current
== entry
)
627 qemu_put_mouse_event_current
= cursor
->next
;
628 qemu_free(entry
->qemu_put_mouse_event_name
);
633 prev
->next
= entry
->next
;
635 if (qemu_put_mouse_event_current
== entry
)
636 qemu_put_mouse_event_current
= prev
;
638 qemu_free(entry
->qemu_put_mouse_event_name
);
642 void kbd_put_keycode(int keycode
)
644 if (qemu_put_kbd_event
) {
645 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
649 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
651 QEMUPutMouseEvent
*mouse_event
;
652 void *mouse_event_opaque
;
655 if (!qemu_put_mouse_event_current
) {
660 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
662 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
665 if (graphic_rotate
) {
666 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
669 width
= graphic_width
- 1;
670 mouse_event(mouse_event_opaque
,
671 width
- dy
, dx
, dz
, buttons_state
);
673 mouse_event(mouse_event_opaque
,
674 dx
, dy
, dz
, buttons_state
);
678 int kbd_mouse_is_absolute(void)
680 if (!qemu_put_mouse_event_current
)
683 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
686 void do_info_mice(void)
688 QEMUPutMouseEntry
*cursor
;
691 if (!qemu_put_mouse_event_head
) {
692 term_printf("No mouse devices connected\n");
696 term_printf("Mouse devices available:\n");
697 cursor
= qemu_put_mouse_event_head
;
698 while (cursor
!= NULL
) {
699 term_printf("%c Mouse #%d: %s\n",
700 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
701 index
, cursor
->qemu_put_mouse_event_name
);
703 cursor
= cursor
->next
;
707 void do_mouse_set(int index
)
709 QEMUPutMouseEntry
*cursor
;
712 if (!qemu_put_mouse_event_head
) {
713 term_printf("No mouse devices connected\n");
717 cursor
= qemu_put_mouse_event_head
;
718 while (cursor
!= NULL
&& index
!= i
) {
720 cursor
= cursor
->next
;
724 qemu_put_mouse_event_current
= cursor
;
726 term_printf("Mouse at given index not found\n");
729 /* compute with 96 bit intermediate result: (a*b)/c */
730 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
735 #ifdef WORDS_BIGENDIAN
745 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
746 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
749 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
753 /***********************************************************/
754 /* real time host monotonic timer */
756 #define QEMU_TIMER_BASE 1000000000LL
760 static int64_t clock_freq
;
762 static void init_get_clock(void)
766 ret
= QueryPerformanceFrequency(&freq
);
768 fprintf(stderr
, "Could not calibrate ticks\n");
771 clock_freq
= freq
.QuadPart
;
774 static int64_t get_clock(void)
777 QueryPerformanceCounter(&ti
);
778 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
783 static int use_rt_clock
;
785 static void init_get_clock(void)
788 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
791 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
798 static int64_t get_clock(void)
800 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
803 clock_gettime(CLOCK_MONOTONIC
, &ts
);
804 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
808 /* XXX: using gettimeofday leads to problems if the date
809 changes, so it should be avoided. */
811 gettimeofday(&tv
, NULL
);
812 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
817 /* Return the virtual CPU time, based on the instruction counter. */
818 static int64_t cpu_get_icount(void)
821 CPUState
*env
= cpu_single_env
;;
822 icount
= qemu_icount
;
825 fprintf(stderr
, "Bad clock read\n");
826 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
828 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
831 /***********************************************************/
832 /* guest cycle counter */
834 static int64_t cpu_ticks_prev
;
835 static int64_t cpu_ticks_offset
;
836 static int64_t cpu_clock_offset
;
837 static int cpu_ticks_enabled
;
839 /* return the host CPU cycle counter and handle stop/restart */
840 int64_t cpu_get_ticks(void)
843 return cpu_get_icount();
845 if (!cpu_ticks_enabled
) {
846 return cpu_ticks_offset
;
849 ticks
= cpu_get_real_ticks();
850 if (cpu_ticks_prev
> ticks
) {
851 /* Note: non increasing ticks may happen if the host uses
853 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
855 cpu_ticks_prev
= ticks
;
856 return ticks
+ cpu_ticks_offset
;
860 /* return the host CPU monotonic timer and handle stop/restart */
861 static int64_t cpu_get_clock(void)
864 if (!cpu_ticks_enabled
) {
865 return cpu_clock_offset
;
868 return ti
+ cpu_clock_offset
;
872 /* enable cpu_get_ticks() */
873 void cpu_enable_ticks(void)
875 if (!cpu_ticks_enabled
) {
876 cpu_ticks_offset
-= cpu_get_real_ticks();
877 cpu_clock_offset
-= get_clock();
878 cpu_ticks_enabled
= 1;
882 /* disable cpu_get_ticks() : the clock is stopped. You must not call
883 cpu_get_ticks() after that. */
884 void cpu_disable_ticks(void)
886 if (cpu_ticks_enabled
) {
887 cpu_ticks_offset
= cpu_get_ticks();
888 cpu_clock_offset
= cpu_get_clock();
889 cpu_ticks_enabled
= 0;
893 /***********************************************************/
896 #define QEMU_TIMER_REALTIME 0
897 #define QEMU_TIMER_VIRTUAL 1
901 /* XXX: add frequency */
909 struct QEMUTimer
*next
;
912 struct qemu_alarm_timer
{
916 int (*start
)(struct qemu_alarm_timer
*t
);
917 void (*stop
)(struct qemu_alarm_timer
*t
);
918 void (*rearm
)(struct qemu_alarm_timer
*t
);
922 #define ALARM_FLAG_DYNTICKS 0x1
923 #define ALARM_FLAG_EXPIRED 0x2
925 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
927 return t
->flags
& ALARM_FLAG_DYNTICKS
;
930 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
932 if (!alarm_has_dynticks(t
))
938 /* TODO: MIN_TIMER_REARM_US should be optimized */
939 #define MIN_TIMER_REARM_US 250
941 static struct qemu_alarm_timer
*alarm_timer
;
943 static int alarm_timer_rfd
, alarm_timer_wfd
;
948 struct qemu_alarm_win32
{
952 } alarm_win32_data
= {0, NULL
, -1};
954 static int win32_start_timer(struct qemu_alarm_timer
*t
);
955 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
956 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
960 static int unix_start_timer(struct qemu_alarm_timer
*t
);
961 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
965 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
966 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
967 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
969 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
970 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
972 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
973 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
975 #endif /* __linux__ */
979 /* Correlation between real and virtual time is always going to be
980 fairly approximate, so ignore small variation.
981 When the guest is idle real and virtual time will be aligned in
983 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
985 static void icount_adjust(void)
990 static int64_t last_delta
;
991 /* If the VM is not running, then do nothing. */
995 cur_time
= cpu_get_clock();
996 cur_icount
= qemu_get_clock(vm_clock
);
997 delta
= cur_icount
- cur_time
;
998 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
1000 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
1001 && icount_time_shift
> 0) {
1002 /* The guest is getting too far ahead. Slow time down. */
1003 icount_time_shift
--;
1006 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1007 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1008 /* The guest is getting too far behind. Speed time up. */
1009 icount_time_shift
++;
1012 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1015 static void icount_adjust_rt(void * opaque
)
1017 qemu_mod_timer(icount_rt_timer
,
1018 qemu_get_clock(rt_clock
) + 1000);
1022 static void icount_adjust_vm(void * opaque
)
1024 qemu_mod_timer(icount_vm_timer
,
1025 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1029 static void init_icount_adjust(void)
1031 /* Have both realtime and virtual time triggers for speed adjustment.
1032 The realtime trigger catches emulated time passing too slowly,
1033 the virtual time trigger catches emulated time passing too fast.
1034 Realtime triggers occur even when idle, so use them less frequently
1035 than VM triggers. */
1036 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1037 qemu_mod_timer(icount_rt_timer
,
1038 qemu_get_clock(rt_clock
) + 1000);
1039 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1040 qemu_mod_timer(icount_vm_timer
,
1041 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1044 static struct qemu_alarm_timer alarm_timers
[] = {
1047 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1048 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1049 /* HPET - if available - is preferred */
1050 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1051 /* ...otherwise try RTC */
1052 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1054 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1056 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1057 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1058 {"win32", 0, win32_start_timer
,
1059 win32_stop_timer
, NULL
, &alarm_win32_data
},
1064 static void show_available_alarms(void)
1068 printf("Available alarm timers, in order of precedence:\n");
1069 for (i
= 0; alarm_timers
[i
].name
; i
++)
1070 printf("%s\n", alarm_timers
[i
].name
);
1073 static void configure_alarms(char const *opt
)
1077 int count
= (sizeof(alarm_timers
) / sizeof(*alarm_timers
)) - 1;
1080 struct qemu_alarm_timer tmp
;
1082 if (!strcmp(opt
, "?")) {
1083 show_available_alarms();
1089 /* Reorder the array */
1090 name
= strtok(arg
, ",");
1092 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1093 if (!strcmp(alarm_timers
[i
].name
, name
))
1098 fprintf(stderr
, "Unknown clock %s\n", name
);
1107 tmp
= alarm_timers
[i
];
1108 alarm_timers
[i
] = alarm_timers
[cur
];
1109 alarm_timers
[cur
] = tmp
;
1113 name
= strtok(NULL
, ",");
1119 /* Disable remaining timers */
1120 for (i
= cur
; i
< count
; i
++)
1121 alarm_timers
[i
].name
= NULL
;
1123 show_available_alarms();
1128 QEMUClock
*rt_clock
;
1129 QEMUClock
*vm_clock
;
1131 static QEMUTimer
*active_timers
[2];
1133 static QEMUClock
*qemu_new_clock(int type
)
1136 clock
= qemu_mallocz(sizeof(QEMUClock
));
1143 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1147 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1150 ts
->opaque
= opaque
;
1154 void qemu_free_timer(QEMUTimer
*ts
)
1159 /* stop a timer, but do not dealloc it */
1160 void qemu_del_timer(QEMUTimer
*ts
)
1164 /* NOTE: this code must be signal safe because
1165 qemu_timer_expired() can be called from a signal. */
1166 pt
= &active_timers
[ts
->clock
->type
];
1179 /* modify the current timer so that it will be fired when current_time
1180 >= expire_time. The corresponding callback will be called. */
1181 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1187 /* add the timer in the sorted list */
1188 /* NOTE: this code must be signal safe because
1189 qemu_timer_expired() can be called from a signal. */
1190 pt
= &active_timers
[ts
->clock
->type
];
1195 if (t
->expire_time
> expire_time
)
1199 ts
->expire_time
= expire_time
;
1203 /* Rearm if necessary */
1204 if (pt
== &active_timers
[ts
->clock
->type
]) {
1205 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1206 qemu_rearm_alarm_timer(alarm_timer
);
1208 /* Interrupt execution to force deadline recalculation. */
1209 if (use_icount
&& cpu_single_env
) {
1210 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1215 int qemu_timer_pending(QEMUTimer
*ts
)
1218 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1225 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1229 return (timer_head
->expire_time
<= current_time
);
1232 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1238 if (!ts
|| ts
->expire_time
> current_time
)
1240 /* remove timer from the list before calling the callback */
1241 *ptimer_head
= ts
->next
;
1244 /* run the callback (the timer list can be modified) */
1249 int64_t qemu_get_clock(QEMUClock
*clock
)
1251 switch(clock
->type
) {
1252 case QEMU_TIMER_REALTIME
:
1253 return get_clock() / 1000000;
1255 case QEMU_TIMER_VIRTUAL
:
1257 return cpu_get_icount();
1259 return cpu_get_clock();
1264 static void init_timers(void)
1267 ticks_per_sec
= QEMU_TIMER_BASE
;
1268 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1269 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1273 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1275 uint64_t expire_time
;
1277 if (qemu_timer_pending(ts
)) {
1278 expire_time
= ts
->expire_time
;
1282 qemu_put_be64(f
, expire_time
);
1285 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1287 uint64_t expire_time
;
1289 expire_time
= qemu_get_be64(f
);
1290 if (expire_time
!= -1) {
1291 qemu_mod_timer(ts
, expire_time
);
1297 static void timer_save(QEMUFile
*f
, void *opaque
)
1299 if (cpu_ticks_enabled
) {
1300 hw_error("cannot save state if virtual timers are running");
1302 qemu_put_be64(f
, cpu_ticks_offset
);
1303 qemu_put_be64(f
, ticks_per_sec
);
1304 qemu_put_be64(f
, cpu_clock_offset
);
1307 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1309 if (version_id
!= 1 && version_id
!= 2)
1311 if (cpu_ticks_enabled
) {
1314 cpu_ticks_offset
=qemu_get_be64(f
);
1315 ticks_per_sec
=qemu_get_be64(f
);
1316 if (version_id
== 2) {
1317 cpu_clock_offset
=qemu_get_be64(f
);
1323 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1324 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1326 static void host_alarm_handler(int host_signum
)
1330 #define DISP_FREQ 1000
1332 static int64_t delta_min
= INT64_MAX
;
1333 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1335 ti
= qemu_get_clock(vm_clock
);
1336 if (last_clock
!= 0) {
1337 delta
= ti
- last_clock
;
1338 if (delta
< delta_min
)
1340 if (delta
> delta_max
)
1343 if (++count
== DISP_FREQ
) {
1344 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1345 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1346 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1347 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1348 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1350 delta_min
= INT64_MAX
;
1359 alarm_has_dynticks(alarm_timer
) ||
1361 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1362 qemu_get_clock(vm_clock
))) ||
1363 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1364 qemu_get_clock(rt_clock
))) {
1365 CPUState
*env
= next_cpu
;
1368 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1369 SetEvent(data
->host_alarm
);
1371 static const char byte
= 0;
1372 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1374 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1377 /* stop the currently executing cpu because a timer occured */
1378 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1380 if (env
->kqemu_enabled
) {
1381 kqemu_cpu_interrupt(env
);
1389 static int64_t qemu_next_deadline(void)
1393 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1394 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1395 qemu_get_clock(vm_clock
);
1397 /* To avoid problems with overflow limit this to 2^32. */
1407 #if defined(__linux__) || defined(_WIN32)
1408 static uint64_t qemu_next_deadline_dyntick(void)
1416 delta
= (qemu_next_deadline() + 999) / 1000;
1418 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1419 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1420 qemu_get_clock(rt_clock
))*1000;
1421 if (rtdelta
< delta
)
1425 if (delta
< MIN_TIMER_REARM_US
)
1426 delta
= MIN_TIMER_REARM_US
;
1434 /* Sets a specific flag */
1435 static int fcntl_setfl(int fd
, int flag
)
1439 flags
= fcntl(fd
, F_GETFL
);
1443 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1449 #if defined(__linux__)
1451 #define RTC_FREQ 1024
1453 static void enable_sigio_timer(int fd
)
1455 struct sigaction act
;
1458 sigfillset(&act
.sa_mask
);
1460 act
.sa_handler
= host_alarm_handler
;
1462 sigaction(SIGIO
, &act
, NULL
);
1463 fcntl_setfl(fd
, O_ASYNC
);
1464 fcntl(fd
, F_SETOWN
, getpid());
1467 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1469 struct hpet_info info
;
1472 fd
= open("/dev/hpet", O_RDONLY
);
1477 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1479 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1480 "error, but for better emulation accuracy type:\n"
1481 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1485 /* Check capabilities */
1486 r
= ioctl(fd
, HPET_INFO
, &info
);
1490 /* Enable periodic mode */
1491 r
= ioctl(fd
, HPET_EPI
, 0);
1492 if (info
.hi_flags
&& (r
< 0))
1495 /* Enable interrupt */
1496 r
= ioctl(fd
, HPET_IE_ON
, 0);
1500 enable_sigio_timer(fd
);
1501 t
->priv
= (void *)(long)fd
;
1509 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1511 int fd
= (long)t
->priv
;
1516 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1519 unsigned long current_rtc_freq
= 0;
1521 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1524 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1525 if (current_rtc_freq
!= RTC_FREQ
&&
1526 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1527 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1528 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1529 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1532 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1538 enable_sigio_timer(rtc_fd
);
1540 t
->priv
= (void *)(long)rtc_fd
;
1545 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1547 int rtc_fd
= (long)t
->priv
;
1552 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1556 struct sigaction act
;
1558 sigfillset(&act
.sa_mask
);
1560 act
.sa_handler
= host_alarm_handler
;
1562 sigaction(SIGALRM
, &act
, NULL
);
1564 ev
.sigev_value
.sival_int
= 0;
1565 ev
.sigev_notify
= SIGEV_SIGNAL
;
1566 ev
.sigev_signo
= SIGALRM
;
1568 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1569 perror("timer_create");
1571 /* disable dynticks */
1572 fprintf(stderr
, "Dynamic Ticks disabled\n");
1577 t
->priv
= (void *)host_timer
;
1582 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1584 timer_t host_timer
= (timer_t
)t
->priv
;
1586 timer_delete(host_timer
);
1589 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1591 timer_t host_timer
= (timer_t
)t
->priv
;
1592 struct itimerspec timeout
;
1593 int64_t nearest_delta_us
= INT64_MAX
;
1596 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1597 !active_timers
[QEMU_TIMER_VIRTUAL
])
1600 nearest_delta_us
= qemu_next_deadline_dyntick();
1602 /* check whether a timer is already running */
1603 if (timer_gettime(host_timer
, &timeout
)) {
1605 fprintf(stderr
, "Internal timer error: aborting\n");
1608 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1609 if (current_us
&& current_us
<= nearest_delta_us
)
1612 timeout
.it_interval
.tv_sec
= 0;
1613 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1614 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1615 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1616 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1618 fprintf(stderr
, "Internal timer error: aborting\n");
1623 #endif /* defined(__linux__) */
1625 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1627 struct sigaction act
;
1628 struct itimerval itv
;
1632 sigfillset(&act
.sa_mask
);
1634 act
.sa_handler
= host_alarm_handler
;
1636 sigaction(SIGALRM
, &act
, NULL
);
1638 itv
.it_interval
.tv_sec
= 0;
1639 /* for i386 kernel 2.6 to get 1 ms */
1640 itv
.it_interval
.tv_usec
= 999;
1641 itv
.it_value
.tv_sec
= 0;
1642 itv
.it_value
.tv_usec
= 10 * 1000;
1644 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1651 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1653 struct itimerval itv
;
1655 memset(&itv
, 0, sizeof(itv
));
1656 setitimer(ITIMER_REAL
, &itv
, NULL
);
1659 #endif /* !defined(_WIN32) */
1661 static void try_to_rearm_timer(void *opaque
)
1663 struct qemu_alarm_timer
*t
= opaque
;
1667 /* Drain the notify pipe */
1670 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1671 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1674 /* vm time timers */
1675 if (vm_running
&& likely(!(cur_cpu
&& (cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
1676 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
1677 qemu_get_clock(vm_clock
));
1679 /* real time timers */
1680 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
1681 qemu_get_clock(rt_clock
));
1683 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1684 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1685 qemu_rearm_alarm_timer(alarm_timer
);
1691 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1694 struct qemu_alarm_win32
*data
= t
->priv
;
1697 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1698 if (!data
->host_alarm
) {
1699 perror("Failed CreateEvent");
1703 memset(&tc
, 0, sizeof(tc
));
1704 timeGetDevCaps(&tc
, sizeof(tc
));
1706 if (data
->period
< tc
.wPeriodMin
)
1707 data
->period
= tc
.wPeriodMin
;
1709 timeBeginPeriod(data
->period
);
1711 flags
= TIME_CALLBACK_FUNCTION
;
1712 if (alarm_has_dynticks(t
))
1713 flags
|= TIME_ONESHOT
;
1715 flags
|= TIME_PERIODIC
;
1717 data
->timerId
= timeSetEvent(1, // interval (ms)
1718 data
->period
, // resolution
1719 host_alarm_handler
, // function
1720 (DWORD
)t
, // parameter
1723 if (!data
->timerId
) {
1724 perror("Failed to initialize win32 alarm timer");
1726 timeEndPeriod(data
->period
);
1727 CloseHandle(data
->host_alarm
);
1731 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1736 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1738 struct qemu_alarm_win32
*data
= t
->priv
;
1740 timeKillEvent(data
->timerId
);
1741 timeEndPeriod(data
->period
);
1743 CloseHandle(data
->host_alarm
);
1746 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1748 struct qemu_alarm_win32
*data
= t
->priv
;
1749 uint64_t nearest_delta_us
;
1751 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1752 !active_timers
[QEMU_TIMER_VIRTUAL
])
1755 nearest_delta_us
= qemu_next_deadline_dyntick();
1756 nearest_delta_us
/= 1000;
1758 timeKillEvent(data
->timerId
);
1760 data
->timerId
= timeSetEvent(1,
1764 TIME_ONESHOT
| TIME_PERIODIC
);
1766 if (!data
->timerId
) {
1767 perror("Failed to re-arm win32 alarm timer");
1769 timeEndPeriod(data
->period
);
1770 CloseHandle(data
->host_alarm
);
1777 static int init_timer_alarm(void)
1779 struct qemu_alarm_timer
*t
= NULL
;
1789 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1793 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1797 alarm_timer_rfd
= fds
[0];
1798 alarm_timer_wfd
= fds
[1];
1801 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1802 t
= &alarm_timers
[i
];
1815 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1816 try_to_rearm_timer
, NULL
, t
);
1831 static void quit_timers(void)
1833 alarm_timer
->stop(alarm_timer
);
1837 /***********************************************************/
1838 /* host time/date access */
1839 void qemu_get_timedate(struct tm
*tm
, int offset
)
1846 if (rtc_date_offset
== -1) {
1850 ret
= localtime(&ti
);
1852 ti
-= rtc_date_offset
;
1856 memcpy(tm
, ret
, sizeof(struct tm
));
1859 int qemu_timedate_diff(struct tm
*tm
)
1863 if (rtc_date_offset
== -1)
1865 seconds
= mktimegm(tm
);
1867 seconds
= mktime(tm
);
1869 seconds
= mktimegm(tm
) + rtc_date_offset
;
1871 return seconds
- time(NULL
);
1875 static void socket_cleanup(void)
1880 static int socket_init(void)
1885 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1887 err
= WSAGetLastError();
1888 fprintf(stderr
, "WSAStartup: %d\n", err
);
1891 atexit(socket_cleanup
);
1896 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1901 while (*p
!= '\0' && *p
!= '=') {
1902 if (q
&& (q
- buf
) < buf_size
- 1)
1912 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1917 while (*p
!= '\0') {
1919 if (*(p
+ 1) != ',')
1923 if (q
&& (q
- buf
) < buf_size
- 1)
1933 int get_param_value(char *buf
, int buf_size
,
1934 const char *tag
, const char *str
)
1941 p
= get_opt_name(option
, sizeof(option
), p
);
1945 if (!strcmp(tag
, option
)) {
1946 (void)get_opt_value(buf
, buf_size
, p
);
1949 p
= get_opt_value(NULL
, 0, p
);
1958 int check_params(char *buf
, int buf_size
,
1959 const char * const *params
, const char *str
)
1966 p
= get_opt_name(buf
, buf_size
, p
);
1970 for(i
= 0; params
[i
] != NULL
; i
++)
1971 if (!strcmp(params
[i
], buf
))
1973 if (params
[i
] == NULL
)
1975 p
= get_opt_value(NULL
, 0, p
);
1983 /***********************************************************/
1984 /* Bluetooth support */
1987 static struct HCIInfo
*hci_table
[MAX_NICS
];
1989 static struct bt_vlan_s
{
1990 struct bt_scatternet_s net
;
1992 struct bt_vlan_s
*next
;
1995 /* find or alloc a new bluetooth "VLAN" */
1996 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1998 struct bt_vlan_s
**pvlan
, *vlan
;
1999 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
2003 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
2005 pvlan
= &first_bt_vlan
;
2006 while (*pvlan
!= NULL
)
2007 pvlan
= &(*pvlan
)->next
;
2012 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2016 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2021 static struct HCIInfo null_hci
= {
2022 .cmd_send
= null_hci_send
,
2023 .sco_send
= null_hci_send
,
2024 .acl_send
= null_hci_send
,
2025 .bdaddr_set
= null_hci_addr_set
,
2028 struct HCIInfo
*qemu_next_hci(void)
2030 if (cur_hci
== nb_hcis
)
2033 return hci_table
[cur_hci
++];
2036 static struct HCIInfo
*hci_init(const char *str
)
2039 struct bt_scatternet_s
*vlan
= 0;
2041 if (!strcmp(str
, "null"))
2044 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2046 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2047 else if (!strncmp(str
, "hci", 3)) {
2050 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2051 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2056 vlan
= qemu_find_bt_vlan(0);
2058 return bt_new_hci(vlan
);
2061 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2066 static int bt_hci_parse(const char *str
)
2068 struct HCIInfo
*hci
;
2071 if (nb_hcis
>= MAX_NICS
) {
2072 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2076 hci
= hci_init(str
);
2085 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2086 hci
->bdaddr_set(hci
, bdaddr
.b
);
2088 hci_table
[nb_hcis
++] = hci
;
2093 static void bt_vhci_add(int vlan_id
)
2095 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2098 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2099 "an empty scatternet %i\n", vlan_id
);
2101 bt_vhci_init(bt_new_hci(vlan
));
2104 static struct bt_device_s
*bt_device_add(const char *opt
)
2106 struct bt_scatternet_s
*vlan
;
2108 char *endp
= strstr(opt
, ",vlan=");
2109 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2112 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2115 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2117 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2122 vlan
= qemu_find_bt_vlan(vlan_id
);
2125 fprintf(stderr
, "qemu: warning: adding a slave device to "
2126 "an empty scatternet %i\n", vlan_id
);
2128 if (!strcmp(devname
, "keyboard"))
2129 return bt_keyboard_init(vlan
);
2131 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2135 static int bt_parse(const char *opt
)
2137 const char *endp
, *p
;
2140 if (strstart(opt
, "hci", &endp
)) {
2141 if (!*endp
|| *endp
== ',') {
2143 if (!strstart(endp
, ",vlan=", 0))
2146 return bt_hci_parse(opt
);
2148 } else if (strstart(opt
, "vhci", &endp
)) {
2149 if (!*endp
|| *endp
== ',') {
2151 if (strstart(endp
, ",vlan=", &p
)) {
2152 vlan
= strtol(p
, (char **) &endp
, 0);
2154 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2158 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2167 } else if (strstart(opt
, "device:", &endp
))
2168 return !bt_device_add(endp
);
2170 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2174 /***********************************************************/
2175 /* QEMU Block devices */
2177 #define HD_ALIAS "index=%d,media=disk"
2179 #define CDROM_ALIAS "index=1,media=cdrom"
2181 #define CDROM_ALIAS "index=2,media=cdrom"
2183 #define FD_ALIAS "index=%d,if=floppy"
2184 #define PFLASH_ALIAS "if=pflash"
2185 #define MTD_ALIAS "if=mtd"
2186 #define SD_ALIAS "index=0,if=sd"
2188 static int drive_opt_get_free_idx(void)
2192 for (index
= 0; index
< MAX_DRIVES
; index
++)
2193 if (!drives_opt
[index
].used
) {
2194 drives_opt
[index
].used
= 1;
2201 static int drive_get_free_idx(void)
2205 for (index
= 0; index
< MAX_DRIVES
; index
++)
2206 if (!drives_table
[index
].used
) {
2207 drives_table
[index
].used
= 1;
2214 int drive_add(const char *file
, const char *fmt
, ...)
2217 int index
= drive_opt_get_free_idx();
2219 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2220 fprintf(stderr
, "qemu: too many drives\n");
2224 drives_opt
[index
].file
= file
;
2226 vsnprintf(drives_opt
[index
].opt
,
2227 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2234 void drive_remove(int index
)
2236 drives_opt
[index
].used
= 0;
2240 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2244 /* seek interface, bus and unit */
2246 for (index
= 0; index
< MAX_DRIVES
; index
++)
2247 if (drives_table
[index
].type
== type
&&
2248 drives_table
[index
].bus
== bus
&&
2249 drives_table
[index
].unit
== unit
&&
2250 drives_table
[index
].used
)
2256 int drive_get_max_bus(BlockInterfaceType type
)
2262 for (index
= 0; index
< nb_drives
; index
++) {
2263 if(drives_table
[index
].type
== type
&&
2264 drives_table
[index
].bus
> max_bus
)
2265 max_bus
= drives_table
[index
].bus
;
2270 static void bdrv_format_print(void *opaque
, const char *name
)
2272 fprintf(stderr
, " %s", name
);
2275 void drive_uninit(BlockDriverState
*bdrv
)
2279 for (i
= 0; i
< MAX_DRIVES
; i
++)
2280 if (drives_table
[i
].bdrv
== bdrv
) {
2281 drives_table
[i
].bdrv
= NULL
;
2282 drives_table
[i
].used
= 0;
2283 drive_remove(drives_table
[i
].drive_opt_idx
);
2289 int drive_init(struct drive_opt
*arg
, int snapshot
,
2290 QEMUMachine
*machine
)
2295 const char *mediastr
= "";
2296 BlockInterfaceType type
;
2297 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2298 int bus_id
, unit_id
;
2299 int cyls
, heads
, secs
, translation
;
2300 BlockDriverState
*bdrv
;
2301 BlockDriver
*drv
= NULL
;
2306 int drives_table_idx
;
2307 char *str
= arg
->opt
;
2308 static const char * const params
[] = { "bus", "unit", "if", "index",
2309 "cyls", "heads", "secs", "trans",
2310 "media", "snapshot", "file",
2311 "cache", "format", "boot", NULL
};
2313 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2314 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2320 cyls
= heads
= secs
= 0;
2323 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2327 if (machine
->use_scsi
) {
2329 max_devs
= MAX_SCSI_DEVS
;
2330 pstrcpy(devname
, sizeof(devname
), "scsi");
2333 max_devs
= MAX_IDE_DEVS
;
2334 pstrcpy(devname
, sizeof(devname
), "ide");
2338 /* extract parameters */
2340 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2341 bus_id
= strtol(buf
, NULL
, 0);
2343 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2348 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2349 unit_id
= strtol(buf
, NULL
, 0);
2351 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2356 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2357 pstrcpy(devname
, sizeof(devname
), buf
);
2358 if (!strcmp(buf
, "ide")) {
2360 max_devs
= MAX_IDE_DEVS
;
2361 } else if (!strcmp(buf
, "scsi")) {
2363 max_devs
= MAX_SCSI_DEVS
;
2364 } else if (!strcmp(buf
, "floppy")) {
2367 } else if (!strcmp(buf
, "pflash")) {
2370 } else if (!strcmp(buf
, "mtd")) {
2373 } else if (!strcmp(buf
, "sd")) {
2376 } else if (!strcmp(buf
, "virtio")) {
2380 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2385 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2386 index
= strtol(buf
, NULL
, 0);
2388 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2393 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2394 cyls
= strtol(buf
, NULL
, 0);
2397 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2398 heads
= strtol(buf
, NULL
, 0);
2401 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2402 secs
= strtol(buf
, NULL
, 0);
2405 if (cyls
|| heads
|| secs
) {
2406 if (cyls
< 1 || cyls
> 16383) {
2407 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2410 if (heads
< 1 || heads
> 16) {
2411 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2414 if (secs
< 1 || secs
> 63) {
2415 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2420 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2423 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2427 if (!strcmp(buf
, "none"))
2428 translation
= BIOS_ATA_TRANSLATION_NONE
;
2429 else if (!strcmp(buf
, "lba"))
2430 translation
= BIOS_ATA_TRANSLATION_LBA
;
2431 else if (!strcmp(buf
, "auto"))
2432 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2434 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2439 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2440 if (!strcmp(buf
, "disk")) {
2442 } else if (!strcmp(buf
, "cdrom")) {
2443 if (cyls
|| secs
|| heads
) {
2445 "qemu: '%s' invalid physical CHS format\n", str
);
2448 media
= MEDIA_CDROM
;
2450 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2455 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2456 if (!strcmp(buf
, "on"))
2458 else if (!strcmp(buf
, "off"))
2461 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2466 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2467 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2469 else if (!strcmp(buf
, "writethrough"))
2471 else if (!strcmp(buf
, "writeback"))
2474 fprintf(stderr
, "qemu: invalid cache option\n");
2479 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2480 if (strcmp(buf
, "?") == 0) {
2481 fprintf(stderr
, "qemu: Supported formats:");
2482 bdrv_iterate_format(bdrv_format_print
, NULL
);
2483 fprintf(stderr
, "\n");
2486 drv
= bdrv_find_format(buf
);
2488 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2493 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2494 if (!strcmp(buf
, "on")) {
2495 if (extboot_drive
!= -1) {
2496 fprintf(stderr
, "qemu: two bootable drives specified\n");
2499 extboot_drive
= nb_drives
;
2500 } else if (strcmp(buf
, "off")) {
2501 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2506 if (arg
->file
== NULL
)
2507 get_param_value(file
, sizeof(file
), "file", str
);
2509 pstrcpy(file
, sizeof(file
), arg
->file
);
2511 /* compute bus and unit according index */
2514 if (bus_id
!= 0 || unit_id
!= -1) {
2516 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2524 unit_id
= index
% max_devs
;
2525 bus_id
= index
/ max_devs
;
2529 /* if user doesn't specify a unit_id,
2530 * try to find the first free
2533 if (unit_id
== -1) {
2535 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2537 if (max_devs
&& unit_id
>= max_devs
) {
2538 unit_id
-= max_devs
;
2546 if (max_devs
&& unit_id
>= max_devs
) {
2547 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2548 str
, unit_id
, max_devs
- 1);
2553 * ignore multiple definitions
2556 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2561 if (type
== IF_IDE
|| type
== IF_SCSI
)
2562 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2564 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2565 devname
, bus_id
, mediastr
, unit_id
);
2567 snprintf(buf
, sizeof(buf
), "%s%s%i",
2568 devname
, mediastr
, unit_id
);
2569 bdrv
= bdrv_new(buf
);
2570 drives_table_idx
= drive_get_free_idx();
2571 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2572 drives_table
[drives_table_idx
].type
= type
;
2573 drives_table
[drives_table_idx
].bus
= bus_id
;
2574 drives_table
[drives_table_idx
].unit
= unit_id
;
2575 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2584 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2585 bdrv_set_translation_hint(bdrv
, translation
);
2589 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2594 /* FIXME: This isn't really a floppy, but it's a reasonable
2597 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2608 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2609 cache
= 2; /* always use write-back with snapshot */
2611 if (cache
== 0) /* no caching */
2612 bdrv_flags
|= BDRV_O_NOCACHE
;
2613 else if (cache
== 2) /* write-back */
2614 bdrv_flags
|= BDRV_O_CACHE_WB
;
2615 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2616 fprintf(stderr
, "qemu: could not open disk image %s\n",
2620 return drives_table_idx
;
2623 /***********************************************************/
2626 static USBPort
*used_usb_ports
;
2627 static USBPort
*free_usb_ports
;
2629 /* ??? Maybe change this to register a hub to keep track of the topology. */
2630 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2631 usb_attachfn attach
)
2633 port
->opaque
= opaque
;
2634 port
->index
= index
;
2635 port
->attach
= attach
;
2636 port
->next
= free_usb_ports
;
2637 free_usb_ports
= port
;
2640 int usb_device_add_dev(USBDevice
*dev
)
2644 /* Find a USB port to add the device to. */
2645 port
= free_usb_ports
;
2649 /* Create a new hub and chain it on. */
2650 free_usb_ports
= NULL
;
2651 port
->next
= used_usb_ports
;
2652 used_usb_ports
= port
;
2654 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2655 usb_attach(port
, hub
);
2656 port
= free_usb_ports
;
2659 free_usb_ports
= port
->next
;
2660 port
->next
= used_usb_ports
;
2661 used_usb_ports
= port
;
2662 usb_attach(port
, dev
);
2666 static int usb_device_add(const char *devname
)
2671 if (!free_usb_ports
)
2674 if (strstart(devname
, "host:", &p
)) {
2675 dev
= usb_host_device_open(p
);
2676 } else if (!strcmp(devname
, "mouse")) {
2677 dev
= usb_mouse_init();
2678 } else if (!strcmp(devname
, "tablet")) {
2679 dev
= usb_tablet_init();
2680 } else if (!strcmp(devname
, "keyboard")) {
2681 dev
= usb_keyboard_init();
2682 } else if (strstart(devname
, "disk:", &p
)) {
2683 dev
= usb_msd_init(p
);
2684 } else if (!strcmp(devname
, "wacom-tablet")) {
2685 dev
= usb_wacom_init();
2686 } else if (strstart(devname
, "serial:", &p
)) {
2687 dev
= usb_serial_init(p
);
2688 #ifdef CONFIG_BRLAPI
2689 } else if (!strcmp(devname
, "braille")) {
2690 dev
= usb_baum_init();
2692 } else if (strstart(devname
, "net:", &p
)) {
2695 if (net_client_init("nic", p
) < 0)
2697 nd_table
[nic
].model
= "usb";
2698 dev
= usb_net_init(&nd_table
[nic
]);
2699 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2700 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2701 bt_new_hci(qemu_find_bt_vlan(0)));
2708 return usb_device_add_dev(dev
);
2711 int usb_device_del_addr(int bus_num
, int addr
)
2717 if (!used_usb_ports
)
2723 lastp
= &used_usb_ports
;
2724 port
= used_usb_ports
;
2725 while (port
&& port
->dev
->addr
!= addr
) {
2726 lastp
= &port
->next
;
2734 *lastp
= port
->next
;
2735 usb_attach(port
, NULL
);
2736 dev
->handle_destroy(dev
);
2737 port
->next
= free_usb_ports
;
2738 free_usb_ports
= port
;
2742 static int usb_device_del(const char *devname
)
2747 if (strstart(devname
, "host:", &p
))
2748 return usb_host_device_close(p
);
2750 if (!used_usb_ports
)
2753 p
= strchr(devname
, '.');
2756 bus_num
= strtoul(devname
, NULL
, 0);
2757 addr
= strtoul(p
+ 1, NULL
, 0);
2759 return usb_device_del_addr(bus_num
, addr
);
2762 void do_usb_add(const char *devname
)
2764 usb_device_add(devname
);
2767 void do_usb_del(const char *devname
)
2769 usb_device_del(devname
);
2776 const char *speed_str
;
2779 term_printf("USB support not enabled\n");
2783 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2787 switch(dev
->speed
) {
2791 case USB_SPEED_FULL
:
2794 case USB_SPEED_HIGH
:
2801 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2802 0, dev
->addr
, speed_str
, dev
->devname
);
2806 /***********************************************************/
2807 /* PCMCIA/Cardbus */
2809 static struct pcmcia_socket_entry_s
{
2810 struct pcmcia_socket_s
*socket
;
2811 struct pcmcia_socket_entry_s
*next
;
2812 } *pcmcia_sockets
= 0;
2814 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2816 struct pcmcia_socket_entry_s
*entry
;
2818 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2819 entry
->socket
= socket
;
2820 entry
->next
= pcmcia_sockets
;
2821 pcmcia_sockets
= entry
;
2824 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2826 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2828 ptr
= &pcmcia_sockets
;
2829 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2830 if (entry
->socket
== socket
) {
2836 void pcmcia_info(void)
2838 struct pcmcia_socket_entry_s
*iter
;
2839 if (!pcmcia_sockets
)
2840 term_printf("No PCMCIA sockets\n");
2842 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2843 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2844 iter
->socket
->attached
? iter
->socket
->card_string
:
2848 /***********************************************************/
2851 static void dumb_update(DisplayState
*ds
, int x
, int y
, int w
, int h
)
2855 static void dumb_resize(DisplayState
*ds
, int w
, int h
)
2859 static void dumb_display_init(DisplayState
*ds
)
2864 ds
->dpy_update
= dumb_update
;
2865 ds
->dpy_resize
= dumb_resize
;
2866 ds
->dpy_refresh
= NULL
;
2867 ds
->gui_timer_interval
= 0;
2871 /***********************************************************/
2874 #define MAX_IO_HANDLERS 64
2876 typedef struct IOHandlerRecord
{
2878 IOCanRWHandler
*fd_read_poll
;
2880 IOHandler
*fd_write
;
2883 /* temporary data */
2885 struct IOHandlerRecord
*next
;
2888 static IOHandlerRecord
*first_io_handler
;
2890 /* XXX: fd_read_poll should be suppressed, but an API change is
2891 necessary in the character devices to suppress fd_can_read(). */
2892 int qemu_set_fd_handler2(int fd
,
2893 IOCanRWHandler
*fd_read_poll
,
2895 IOHandler
*fd_write
,
2898 IOHandlerRecord
**pioh
, *ioh
;
2900 if (!fd_read
&& !fd_write
) {
2901 pioh
= &first_io_handler
;
2906 if (ioh
->fd
== fd
) {
2913 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2917 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2920 ioh
->next
= first_io_handler
;
2921 first_io_handler
= ioh
;
2924 ioh
->fd_read_poll
= fd_read_poll
;
2925 ioh
->fd_read
= fd_read
;
2926 ioh
->fd_write
= fd_write
;
2927 ioh
->opaque
= opaque
;
2934 int qemu_set_fd_handler(int fd
,
2936 IOHandler
*fd_write
,
2939 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2943 /***********************************************************/
2944 /* Polling handling */
2946 typedef struct PollingEntry
{
2949 struct PollingEntry
*next
;
2952 static PollingEntry
*first_polling_entry
;
2954 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2956 PollingEntry
**ppe
, *pe
;
2957 pe
= qemu_mallocz(sizeof(PollingEntry
));
2961 pe
->opaque
= opaque
;
2962 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2967 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2969 PollingEntry
**ppe
, *pe
;
2970 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2972 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2980 /***********************************************************/
2981 /* Wait objects support */
2982 typedef struct WaitObjects
{
2984 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2985 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2986 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2989 static WaitObjects wait_objects
= {0};
2991 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2993 WaitObjects
*w
= &wait_objects
;
2995 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2997 w
->events
[w
->num
] = handle
;
2998 w
->func
[w
->num
] = func
;
2999 w
->opaque
[w
->num
] = opaque
;
3004 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3007 WaitObjects
*w
= &wait_objects
;
3010 for (i
= 0; i
< w
->num
; i
++) {
3011 if (w
->events
[i
] == handle
)
3014 w
->events
[i
] = w
->events
[i
+ 1];
3015 w
->func
[i
] = w
->func
[i
+ 1];
3016 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3024 /***********************************************************/
3025 /* ram save/restore */
3027 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3031 v
= qemu_get_byte(f
);
3034 if (qemu_get_buffer(f
, buf
, len
) != len
)
3038 v
= qemu_get_byte(f
);
3039 memset(buf
, v
, len
);
3045 if (qemu_file_has_error(f
))
3051 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3056 if (qemu_get_be32(f
) != phys_ram_size
)
3058 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3059 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3061 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3068 #define BDRV_HASH_BLOCK_SIZE 1024
3069 #define IOBUF_SIZE 4096
3070 #define RAM_CBLOCK_MAGIC 0xfabe
3072 typedef struct RamDecompressState
{
3075 uint8_t buf
[IOBUF_SIZE
];
3076 } RamDecompressState
;
3078 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3081 memset(s
, 0, sizeof(*s
));
3083 ret
= inflateInit(&s
->zstream
);
3089 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3093 s
->zstream
.avail_out
= len
;
3094 s
->zstream
.next_out
= buf
;
3095 while (s
->zstream
.avail_out
> 0) {
3096 if (s
->zstream
.avail_in
== 0) {
3097 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3099 clen
= qemu_get_be16(s
->f
);
3100 if (clen
> IOBUF_SIZE
)
3102 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3103 s
->zstream
.avail_in
= clen
;
3104 s
->zstream
.next_in
= s
->buf
;
3106 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3107 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3114 static void ram_decompress_close(RamDecompressState
*s
)
3116 inflateEnd(&s
->zstream
);
3119 #define RAM_SAVE_FLAG_FULL 0x01
3120 #define RAM_SAVE_FLAG_COMPRESS 0x02
3121 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3122 #define RAM_SAVE_FLAG_PAGE 0x08
3123 #define RAM_SAVE_FLAG_EOS 0x10
3125 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3127 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3128 uint32_t *array
= (uint32_t *)page
;
3131 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3132 if (array
[i
] != val
)
3139 static int ram_save_block(QEMUFile
*f
)
3141 static ram_addr_t current_addr
= 0;
3142 ram_addr_t saved_addr
= current_addr
;
3143 ram_addr_t addr
= 0;
3146 while (addr
< phys_ram_size
) {
3147 if (kvm_enabled() && current_addr
== 0)
3148 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3149 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3152 cpu_physical_memory_reset_dirty(current_addr
,
3153 current_addr
+ TARGET_PAGE_SIZE
,
3154 MIGRATION_DIRTY_FLAG
);
3156 ch
= *(phys_ram_base
+ current_addr
);
3158 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3159 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3160 qemu_put_byte(f
, ch
);
3162 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3163 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3169 addr
+= TARGET_PAGE_SIZE
;
3170 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3176 static ram_addr_t ram_save_threshold
= 10;
3178 static ram_addr_t
ram_save_remaining(void)
3181 ram_addr_t count
= 0;
3183 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3184 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3191 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3196 /* Make sure all dirty bits are set */
3197 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3198 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3199 cpu_physical_memory_set_dirty(addr
);
3202 /* Enable dirty memory tracking */
3203 cpu_physical_memory_set_dirty_tracking(1);
3205 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3208 while (!qemu_file_rate_limit(f
)) {
3211 ret
= ram_save_block(f
);
3212 if (ret
== 0) /* no more blocks */
3216 /* try transferring iterative blocks of memory */
3219 cpu_physical_memory_set_dirty_tracking(0);
3221 /* flush all remaining blocks regardless of rate limiting */
3222 while (ram_save_block(f
) != 0);
3225 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3227 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3230 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3232 RamDecompressState s1
, *s
= &s1
;
3236 if (ram_decompress_open(s
, f
) < 0)
3238 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3239 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3241 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3242 fprintf(stderr
, "Error while reading ram block header\n");
3246 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3247 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3252 printf("Error block header\n");
3256 ram_decompress_close(s
);
3261 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3266 if (version_id
== 1)
3267 return ram_load_v1(f
, opaque
);
3269 if (version_id
== 2) {
3270 if (qemu_get_be32(f
) != phys_ram_size
)
3272 return ram_load_dead(f
, opaque
);
3275 if (version_id
!= 3)
3279 addr
= qemu_get_be64(f
);
3281 flags
= addr
& ~TARGET_PAGE_MASK
;
3282 addr
&= TARGET_PAGE_MASK
;
3284 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3285 if (addr
!= phys_ram_size
)
3289 if (flags
& RAM_SAVE_FLAG_FULL
) {
3290 if (ram_load_dead(f
, opaque
) < 0)
3294 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3295 uint8_t ch
= qemu_get_byte(f
);
3296 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3297 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3298 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3299 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3304 /***********************************************************/
3305 /* bottom halves (can be seen as timers which expire ASAP) */
3316 static QEMUBH
*first_bh
= NULL
;
3318 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3321 bh
= qemu_mallocz(sizeof(QEMUBH
));
3325 bh
->opaque
= opaque
;
3326 bh
->next
= first_bh
;
3331 int qemu_bh_poll(void)
3337 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3338 if (!bh
->deleted
&& bh
->scheduled
) {
3347 /* remove deleted bhs */
3361 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3369 void qemu_bh_schedule(QEMUBH
*bh
)
3371 CPUState
*env
= cpu_single_env
;
3376 /* stop the currently executing CPU to execute the BH ASAP */
3378 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3383 void qemu_bh_cancel(QEMUBH
*bh
)
3388 void qemu_bh_delete(QEMUBH
*bh
)
3394 static void qemu_bh_update_timeout(int *timeout
)
3398 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3399 if (!bh
->deleted
&& bh
->scheduled
) {
3401 /* idle bottom halves will be polled at least
3403 *timeout
= MIN(10, *timeout
);
3405 /* non-idle bottom halves will be executed
3414 /***********************************************************/
3415 /* machine registration */
3417 static QEMUMachine
*first_machine
= NULL
;
3418 QEMUMachine
*current_machine
= NULL
;
3420 int qemu_register_machine(QEMUMachine
*m
)
3423 pm
= &first_machine
;
3431 static QEMUMachine
*find_machine(const char *name
)
3435 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3436 if (!strcmp(m
->name
, name
))
3442 /***********************************************************/
3443 /* main execution loop */
3445 static void gui_update(void *opaque
)
3447 DisplayState
*ds
= opaque
;
3448 ds
->dpy_refresh(ds
);
3449 qemu_mod_timer(ds
->gui_timer
,
3450 (ds
->gui_timer_interval
?
3451 ds
->gui_timer_interval
:
3452 GUI_REFRESH_INTERVAL
)
3453 + qemu_get_clock(rt_clock
));
3456 struct vm_change_state_entry
{
3457 VMChangeStateHandler
*cb
;
3459 LIST_ENTRY (vm_change_state_entry
) entries
;
3462 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3464 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3467 VMChangeStateEntry
*e
;
3469 e
= qemu_mallocz(sizeof (*e
));
3475 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3479 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3481 LIST_REMOVE (e
, entries
);
3485 static void vm_state_notify(int running
)
3487 VMChangeStateEntry
*e
;
3489 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3490 e
->cb(e
->opaque
, running
);
3494 /* XXX: support several handlers */
3495 static VMStopHandler
*vm_stop_cb
;
3496 static void *vm_stop_opaque
;
3498 int qemu_add_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3501 vm_stop_opaque
= opaque
;
3505 void qemu_del_vm_stop_handler(VMStopHandler
*cb
, void *opaque
)
3516 qemu_rearm_alarm_timer(alarm_timer
);
3520 void vm_stop(int reason
)
3523 cpu_disable_ticks();
3527 vm_stop_cb(vm_stop_opaque
, reason
);
3534 /* reset/shutdown handler */
3536 typedef struct QEMUResetEntry
{
3537 QEMUResetHandler
*func
;
3539 struct QEMUResetEntry
*next
;
3542 static QEMUResetEntry
*first_reset_entry
;
3543 static int reset_requested
;
3544 static int shutdown_requested
;
3545 static int powerdown_requested
;
3547 int qemu_shutdown_requested(void)
3549 int r
= shutdown_requested
;
3550 shutdown_requested
= 0;
3554 int qemu_reset_requested(void)
3556 int r
= reset_requested
;
3557 reset_requested
= 0;
3561 int qemu_powerdown_requested(void)
3563 int r
= powerdown_requested
;
3564 powerdown_requested
= 0;
3568 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3570 QEMUResetEntry
**pre
, *re
;
3572 pre
= &first_reset_entry
;
3573 while (*pre
!= NULL
)
3574 pre
= &(*pre
)->next
;
3575 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3577 re
->opaque
= opaque
;
3582 void qemu_system_reset(void)
3586 /* reset all devices */
3587 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3588 re
->func(re
->opaque
);
3592 void qemu_system_reset_request(void)
3595 shutdown_requested
= 1;
3597 reset_requested
= 1;
3600 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3604 void qemu_system_shutdown_request(void)
3606 shutdown_requested
= 1;
3608 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3611 void qemu_system_powerdown_request(void)
3613 powerdown_requested
= 1;
3615 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3618 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3623 /* KVM holds a mutex while QEMU code is running, we need hooks to
3624 release the mutex whenever QEMU code sleeps. */
3628 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3636 void host_main_loop_wait(int *timeout
)
3642 /* XXX: need to suppress polling by better using win32 events */
3644 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3645 ret
|= pe
->func(pe
->opaque
);
3649 WaitObjects
*w
= &wait_objects
;
3651 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3652 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3653 if (w
->func
[ret
- WAIT_OBJECT_0
])
3654 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3656 /* Check for additional signaled events */
3657 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3659 /* Check if event is signaled */
3660 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3661 if(ret2
== WAIT_OBJECT_0
) {
3663 w
->func
[i
](w
->opaque
[i
]);
3664 } else if (ret2
== WAIT_TIMEOUT
) {
3666 err
= GetLastError();
3667 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3670 } else if (ret
== WAIT_TIMEOUT
) {
3672 err
= GetLastError();
3673 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3680 void host_main_loop_wait(int *timeout
)
3685 void main_loop_wait(int timeout
)
3687 IOHandlerRecord
*ioh
;
3688 fd_set rfds
, wfds
, xfds
;
3692 qemu_bh_update_timeout(&timeout
);
3694 host_main_loop_wait(&timeout
);
3696 /* poll any events */
3697 /* XXX: separate device handlers from system ones */
3702 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3706 (!ioh
->fd_read_poll
||
3707 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3708 FD_SET(ioh
->fd
, &rfds
);
3712 if (ioh
->fd_write
) {
3713 FD_SET(ioh
->fd
, &wfds
);
3719 tv
.tv_sec
= timeout
/ 1000;
3720 tv
.tv_usec
= (timeout
% 1000) * 1000;
3722 #if defined(CONFIG_SLIRP)
3723 if (slirp_is_inited()) {
3724 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3727 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3729 IOHandlerRecord
**pioh
;
3731 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3732 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3733 ioh
->fd_read(ioh
->opaque
);
3734 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3735 FD_CLR(ioh
->fd
, &rfds
);
3737 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3738 ioh
->fd_write(ioh
->opaque
);
3742 /* remove deleted IO handlers */
3743 pioh
= &first_io_handler
;
3753 #if defined(CONFIG_SLIRP)
3754 if (slirp_is_inited()) {
3760 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3764 /* Check bottom-halves last in case any of the earlier events triggered
3770 static int main_loop(void)
3773 #ifdef CONFIG_PROFILER
3779 if (kvm_enabled()) {
3781 cpu_disable_ticks();
3785 cur_cpu
= first_cpu
;
3786 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3793 #ifdef CONFIG_PROFILER
3794 ti
= profile_getclock();
3799 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3800 env
->icount_decr
.u16
.low
= 0;
3801 env
->icount_extra
= 0;
3802 count
= qemu_next_deadline();
3803 count
= (count
+ (1 << icount_time_shift
) - 1)
3804 >> icount_time_shift
;
3805 qemu_icount
+= count
;
3806 decr
= (count
> 0xffff) ? 0xffff : count
;
3808 env
->icount_decr
.u16
.low
= decr
;
3809 env
->icount_extra
= count
;
3811 ret
= cpu_exec(env
);
3812 #ifdef CONFIG_PROFILER
3813 qemu_time
+= profile_getclock() - ti
;
3816 /* Fold pending instructions back into the
3817 instruction counter, and clear the interrupt flag. */
3818 qemu_icount
-= (env
->icount_decr
.u16
.low
3819 + env
->icount_extra
);
3820 env
->icount_decr
.u32
= 0;
3821 env
->icount_extra
= 0;
3823 next_cpu
= env
->next_cpu
?: first_cpu
;
3824 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3825 ret
= EXCP_INTERRUPT
;
3829 if (ret
== EXCP_HLT
) {
3830 /* Give the next CPU a chance to run. */
3834 if (ret
!= EXCP_HALTED
)
3836 /* all CPUs are halted ? */
3842 if (shutdown_requested
) {
3843 ret
= EXCP_INTERRUPT
;
3851 if (reset_requested
) {
3852 reset_requested
= 0;
3853 qemu_system_reset();
3855 kvm_load_registers(env
);
3856 ret
= EXCP_INTERRUPT
;
3858 if (powerdown_requested
) {
3859 powerdown_requested
= 0;
3860 qemu_system_powerdown();
3861 ret
= EXCP_INTERRUPT
;
3863 if (unlikely(ret
== EXCP_DEBUG
)) {
3864 vm_stop(EXCP_DEBUG
);
3866 /* If all cpus are halted then wait until the next IRQ */
3867 /* XXX: use timeout computed from timers */
3868 if (ret
== EXCP_HALTED
) {
3872 /* Advance virtual time to the next event. */
3873 if (use_icount
== 1) {
3874 /* When not using an adaptive execution frequency
3875 we tend to get badly out of sync with real time,
3876 so just delay for a reasonable amount of time. */
3879 delta
= cpu_get_icount() - cpu_get_clock();
3882 /* If virtual time is ahead of real time then just
3884 timeout
= (delta
/ 1000000) + 1;
3886 /* Wait for either IO to occur or the next
3888 add
= qemu_next_deadline();
3889 /* We advance the timer before checking for IO.
3890 Limit the amount we advance so that early IO
3891 activity won't get the guest too far ahead. */
3895 add
= (add
+ (1 << icount_time_shift
) - 1)
3896 >> icount_time_shift
;
3898 timeout
= delta
/ 1000000;
3909 if (shutdown_requested
) {
3910 ret
= EXCP_INTERRUPT
;
3915 #ifdef CONFIG_PROFILER
3916 ti
= profile_getclock();
3918 main_loop_wait(timeout
);
3919 #ifdef CONFIG_PROFILER
3920 dev_time
+= profile_getclock() - ti
;
3923 cpu_disable_ticks();
3927 static void help(int exitcode
)
3929 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3930 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3931 "usage: %s [options] [disk_image]\n"
3933 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3935 "Standard options:\n"
3936 "-M machine select emulated machine (-M ? for list)\n"
3937 "-cpu cpu select CPU (-cpu ? for list)\n"
3938 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3939 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3940 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3941 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3942 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3943 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3944 " [,cache=writethrough|writeback|none][,format=f]\n"
3946 " use 'file' as a drive image\n"
3947 "-mtdblock file use 'file' as on-board Flash memory image\n"
3948 "-sd file use 'file' as SecureDigital card image\n"
3949 "-pflash file use 'file' as a parallel flash image\n"
3950 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3951 "-snapshot write to temporary files instead of disk image files\n"
3953 "-no-frame open SDL window without a frame and window decorations\n"
3954 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3955 "-no-quit disable SDL window close capability\n"
3958 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
3960 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3961 "-smp n set the number of CPUs to 'n' [default=1]\n"
3962 "-nographic disable graphical output and redirect serial I/Os to console\n"
3963 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3965 "-k language use keyboard layout (for example \"fr\" for French)\n"
3968 "-audio-help print list of audio drivers and their options\n"
3969 "-soundhw c1,... enable audio support\n"
3970 " and only specified sound cards (comma separated list)\n"
3971 " use -soundhw ? to get the list of supported cards\n"
3972 " use -soundhw all to enable all of them\n"
3974 "-vga [std|cirrus|vmware]\n"
3975 " select video card type\n"
3976 "-localtime set the real time clock to local time [default=utc]\n"
3977 "-full-screen start in full screen\n"
3979 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
3981 "-usb enable the USB driver (will be the default soon)\n"
3982 "-usbdevice name add the host or guest USB device 'name'\n"
3983 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3984 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3986 "-name string set the name of the guest\n"
3987 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
3989 "Network options:\n"
3990 "-net nic[,vlan=n][,macaddr=addr][,model=type]\n"
3991 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3993 "-net user[,vlan=n][,hostname=host]\n"
3994 " connect the user mode network stack to VLAN 'n' and send\n"
3995 " hostname 'host' to DHCP clients\n"
3998 "-net tap[,vlan=n],ifname=name\n"
3999 " connect the host TAP network interface to VLAN 'n'\n"
4001 "-net tap[,vlan=n][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4002 " connect the host TAP network interface to VLAN 'n' and use the\n"
4003 " network scripts 'file' (default=%s)\n"
4004 " and 'dfile' (default=%s);\n"
4005 " use '[down]script=no' to disable script execution;\n"
4006 " use 'fd=h' to connect to an already opened TAP interface\n"
4008 "-net socket[,vlan=n][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4009 " connect the vlan 'n' to another VLAN using a socket connection\n"
4010 "-net socket[,vlan=n][,fd=h][,mcast=maddr:port]\n"
4011 " connect the vlan 'n' to multicast maddr and port\n"
4013 "-net vde[,vlan=n][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4014 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4015 " on host and listening for incoming connections on 'socketpath'.\n"
4016 " Use group 'groupname' and mode 'octalmode' to change default\n"
4017 " ownership and permissions for communication port.\n"
4019 "-net none use it alone to have zero network devices; if no -net option\n"
4020 " is provided, the default is '-net nic -net user'\n"
4022 "-bt hci,null Dumb bluetooth HCI - doesn't respond to commands\n"
4023 "-bt hci,host[:id]\n"
4024 " Use host's HCI with the given name\n"
4025 "-bt hci[,vlan=n]\n"
4026 " Emulate a standard HCI in virtual scatternet 'n'\n"
4027 "-bt vhci[,vlan=n]\n"
4028 " Add host computer to virtual scatternet 'n' using VHCI\n"
4029 "-bt device:dev[,vlan=n]\n"
4030 " Emulate a bluetooth device 'dev' in scatternet 'n'\n"
4033 "-tftp dir allow tftp access to files in dir [-net user]\n"
4034 "-bootp file advertise file in BOOTP replies\n"
4036 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4038 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4039 " redirect TCP or UDP connections from host to guest [-net user]\n"
4042 "Linux boot specific:\n"
4043 "-kernel bzImage use 'bzImage' as kernel image\n"
4044 "-append cmdline use 'cmdline' as kernel command line\n"
4045 "-initrd file use 'file' as initial ram disk\n"
4047 "Debug/Expert options:\n"
4048 "-monitor dev redirect the monitor to char device 'dev'\n"
4049 "-serial dev redirect the serial port to char device 'dev'\n"
4050 "-parallel dev redirect the parallel port to char device 'dev'\n"
4051 "-pidfile file Write PID to 'file'\n"
4052 "-S freeze CPU at startup (use 'c' to start execution)\n"
4053 "-s wait gdb connection to port\n"
4054 "-p port set gdb connection port [default=%s]\n"
4055 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4056 "-hdachs c,h,s[,t] force hard disk 0 physical geometry and the optional BIOS\n"
4057 " translation (t=none or lba) (usually qemu can guess them)\n"
4058 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4060 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4061 "-no-kqemu disable KQEMU kernel module usage\n"
4064 "-enable-kvm enable KVM full virtualization support\n"
4067 #ifndef NO_CPU_EMULATION
4068 "-no-kvm disable KVM hardware virtualization\n"
4070 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4071 "-no-kvm-pit disable KVM kernel mode PIT\n"
4072 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4073 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4074 " expose a PCI device to the guest OS.\n"
4075 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4076 " 'string' is used in log output.\n"
4080 "-no-acpi disable ACPI\n"
4082 #ifdef CONFIG_CURSES
4083 "-curses use a curses/ncurses interface instead of SDL\n"
4085 "-no-reboot exit instead of rebooting\n"
4086 "-no-shutdown stop before shutdown\n"
4087 "-loadvm [tag|id] start right away with a saved state (loadvm in monitor)\n"
4088 "-vnc display start a VNC server on display\n"
4090 "-daemonize daemonize QEMU after initializing\n"
4092 "-tdf inject timer interrupts that got lost\n"
4093 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4094 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also enables allocation of guest memory with huge pages\n"
4095 "-option-rom rom load a file, rom, into the option ROM space\n"
4097 "-prom-env variable=value set OpenBIOS nvram variables\n"
4099 "-clock force the use of the given methods for timer alarm.\n"
4100 " To see what timers are available use -clock ?\n"
4101 "-startdate select initial date of the clock\n"
4102 "-icount [N|auto]\n"
4103 " Enable virtual instruction counter with 2^N clock ticks per instruction\n"
4105 "During emulation, the following keys are useful:\n"
4106 "ctrl-alt-f toggle full screen\n"
4107 "ctrl-alt-n switch to virtual console 'n'\n"
4108 "ctrl-alt toggle mouse and keyboard grab\n"
4110 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4115 DEFAULT_NETWORK_SCRIPT
,
4116 DEFAULT_NETWORK_DOWN_SCRIPT
,
4118 DEFAULT_GDBSTUB_PORT
,
4123 #define HAS_ARG 0x0001
4138 QEMU_OPTION_mtdblock
,
4142 QEMU_OPTION_snapshot
,
4144 QEMU_OPTION_no_fd_bootchk
,
4147 QEMU_OPTION_nographic
,
4148 QEMU_OPTION_portrait
,
4150 QEMU_OPTION_audio_help
,
4151 QEMU_OPTION_soundhw
,
4173 QEMU_OPTION_localtime
,
4177 QEMU_OPTION_monitor
,
4179 QEMU_OPTION_parallel
,
4181 QEMU_OPTION_full_screen
,
4182 QEMU_OPTION_no_frame
,
4183 QEMU_OPTION_alt_grab
,
4184 QEMU_OPTION_no_quit
,
4185 QEMU_OPTION_pidfile
,
4186 QEMU_OPTION_no_kqemu
,
4187 QEMU_OPTION_kernel_kqemu
,
4188 QEMU_OPTION_enable_kvm
,
4189 QEMU_OPTION_win2k_hack
,
4191 QEMU_OPTION_usbdevice
,
4194 QEMU_OPTION_no_acpi
,
4197 QEMU_OPTION_no_kvm_irqchip
,
4198 QEMU_OPTION_no_kvm_pit
,
4199 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4200 QEMU_OPTION_pcidevice
,
4202 QEMU_OPTION_no_reboot
,
4203 QEMU_OPTION_no_shutdown
,
4204 QEMU_OPTION_show_cursor
,
4205 QEMU_OPTION_daemonize
,
4206 QEMU_OPTION_option_rom
,
4207 QEMU_OPTION_semihosting
,
4208 QEMU_OPTION_cpu_vendor
,
4210 QEMU_OPTION_prom_env
,
4211 QEMU_OPTION_old_param
,
4213 QEMU_OPTION_startdate
,
4214 QEMU_OPTION_tb_size
,
4217 QEMU_OPTION_incoming
,
4219 QEMU_OPTION_kvm_shadow_memory
,
4220 QEMU_OPTION_mempath
,
4223 typedef struct QEMUOption
{
4229 static const QEMUOption qemu_options
[] = {
4230 { "h", 0, QEMU_OPTION_h
},
4231 { "help", 0, QEMU_OPTION_h
},
4233 { "M", HAS_ARG
, QEMU_OPTION_M
},
4234 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4235 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4236 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4237 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4238 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4239 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4240 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4241 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4242 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4243 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4244 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4245 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4246 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4247 { "snapshot", 0, QEMU_OPTION_snapshot
},
4249 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4251 { "m", HAS_ARG
, QEMU_OPTION_m
},
4252 { "nographic", 0, QEMU_OPTION_nographic
},
4253 { "portrait", 0, QEMU_OPTION_portrait
},
4254 { "k", HAS_ARG
, QEMU_OPTION_k
},
4256 { "audio-help", 0, QEMU_OPTION_audio_help
},
4257 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4260 { "net", HAS_ARG
, QEMU_OPTION_net
},
4262 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4263 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4265 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4267 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4269 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4271 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4272 { "append", HAS_ARG
, QEMU_OPTION_append
},
4273 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4275 { "S", 0, QEMU_OPTION_S
},
4276 { "s", 0, QEMU_OPTION_s
},
4277 { "p", HAS_ARG
, QEMU_OPTION_p
},
4278 { "d", HAS_ARG
, QEMU_OPTION_d
},
4279 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4280 { "L", HAS_ARG
, QEMU_OPTION_L
},
4281 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4283 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4284 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4287 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4290 #ifndef NO_CPU_EMULATION
4291 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4293 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4294 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4295 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4296 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4299 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4300 { "g", 1, QEMU_OPTION_g
},
4302 { "localtime", 0, QEMU_OPTION_localtime
},
4303 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4304 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4305 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4306 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4307 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4308 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4309 { "incoming", 1, QEMU_OPTION_incoming
},
4310 { "full-screen", 0, QEMU_OPTION_full_screen
},
4312 { "no-frame", 0, QEMU_OPTION_no_frame
},
4313 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4314 { "no-quit", 0, QEMU_OPTION_no_quit
},
4316 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4317 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4318 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4319 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4320 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4321 #ifdef CONFIG_CURSES
4322 { "curses", 0, QEMU_OPTION_curses
},
4324 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4326 /* temporary options */
4327 { "usb", 0, QEMU_OPTION_usb
},
4328 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4329 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4330 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4331 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4332 { "daemonize", 0, QEMU_OPTION_daemonize
},
4333 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4334 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4335 { "semihosting", 0, QEMU_OPTION_semihosting
},
4337 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4338 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4339 { "name", HAS_ARG
, QEMU_OPTION_name
},
4340 #if defined(TARGET_SPARC)
4341 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4343 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4344 #if defined(TARGET_ARM)
4345 { "old-param", 0, QEMU_OPTION_old_param
},
4347 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4348 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4349 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4350 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4351 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4352 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4356 /* password input */
4358 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4363 if (!bdrv_is_encrypted(bs
))
4366 term_printf("%s is encrypted.\n", name
);
4367 for(i
= 0; i
< 3; i
++) {
4368 monitor_readline("Password: ", 1, password
, sizeof(password
));
4369 if (bdrv_set_key(bs
, password
) == 0)
4371 term_printf("invalid password\n");
4376 static BlockDriverState
*get_bdrv(int index
)
4378 if (index
> nb_drives
)
4380 return drives_table
[index
].bdrv
;
4383 static void read_passwords(void)
4385 BlockDriverState
*bs
;
4388 for(i
= 0; i
< 6; i
++) {
4391 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4396 struct soundhw soundhw
[] = {
4397 #ifdef HAS_AUDIO_CHOICE
4398 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4404 { .init_isa
= pcspk_audio_init
}
4409 "Creative Sound Blaster 16",
4412 { .init_isa
= SB16_init
}
4415 #ifdef CONFIG_CS4231A
4421 { .init_isa
= cs4231a_init
}
4429 "Yamaha YMF262 (OPL3)",
4431 "Yamaha YM3812 (OPL2)",
4435 { .init_isa
= Adlib_init
}
4442 "Gravis Ultrasound GF1",
4445 { .init_isa
= GUS_init
}
4452 "Intel 82801AA AC97 Audio",
4455 { .init_pci
= ac97_init
}
4461 "ENSONIQ AudioPCI ES1370",
4464 { .init_pci
= es1370_init
}
4468 { NULL
, NULL
, 0, 0, { NULL
} }
4471 static void select_soundhw (const char *optarg
)
4475 if (*optarg
== '?') {
4478 printf ("Valid sound card names (comma separated):\n");
4479 for (c
= soundhw
; c
->name
; ++c
) {
4480 printf ("%-11s %s\n", c
->name
, c
->descr
);
4482 printf ("\n-soundhw all will enable all of the above\n");
4483 exit (*optarg
!= '?');
4491 if (!strcmp (optarg
, "all")) {
4492 for (c
= soundhw
; c
->name
; ++c
) {
4500 e
= strchr (p
, ',');
4501 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4503 for (c
= soundhw
; c
->name
; ++c
) {
4504 if (!strncmp (c
->name
, p
, l
)) {
4513 "Unknown sound card name (too big to show)\n");
4516 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4521 p
+= l
+ (e
!= NULL
);
4525 goto show_valid_cards
;
4530 static void select_vgahw (const char *p
)
4534 if (strstart(p
, "std", &opts
)) {
4535 cirrus_vga_enabled
= 0;
4537 } else if (strstart(p
, "cirrus", &opts
)) {
4538 cirrus_vga_enabled
= 1;
4540 } else if (strstart(p
, "vmware", &opts
)) {
4541 cirrus_vga_enabled
= 0;
4545 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4549 const char *nextopt
;
4551 if (strstart(opts
, ",retrace=", &nextopt
)) {
4553 if (strstart(opts
, "dumb", &nextopt
))
4554 vga_retrace_method
= VGA_RETRACE_DUMB
;
4555 else if (strstart(opts
, "precise", &nextopt
))
4556 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4557 else goto invalid_vga
;
4558 } else goto invalid_vga
;
4564 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4566 exit(STATUS_CONTROL_C_EXIT
);
4571 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4575 if(strlen(str
) != 36)
4578 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4579 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4580 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4588 #define MAX_NET_CLIENTS 32
4590 static int saved_argc
;
4591 static char **saved_argv
;
4593 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4597 *opt_daemonize
= daemonize
;
4598 *opt_incoming
= incoming
;
4602 static int gethugepagesize(void)
4606 char *needle
= "Hugepagesize:";
4608 unsigned long hugepagesize
;
4610 fd
= open("/proc/meminfo", O_RDONLY
);
4616 ret
= read(fd
, buf
, sizeof(buf
));
4622 size
= strstr(buf
, needle
);
4625 size
+= strlen(needle
);
4626 hugepagesize
= strtol(size
, NULL
, 0);
4627 return hugepagesize
;
4630 void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4636 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4639 hpagesize
= gethugepagesize() * 1024;
4643 fd
= mkstemp(filename
);
4652 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4655 * ftruncate is not supported by hugetlbfs in older
4656 * hosts, so don't bother checking for errors.
4657 * If anything goes wrong with it under other filesystems,
4660 ftruncate(fd
, memory
);
4662 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4663 if (area
== MAP_FAILED
) {
4673 void *qemu_alloc_physram(unsigned long memory
)
4676 unsigned long map_len
= memory
;
4680 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4683 area
= qemu_vmalloc(memory
);
4685 if (kvm_setup_guest_memory(area
, map_len
))
4693 static void termsig_handler(int signal
)
4695 qemu_system_shutdown_request();
4698 static void termsig_setup(void)
4700 struct sigaction act
;
4702 memset(&act
, 0, sizeof(act
));
4703 act
.sa_handler
= termsig_handler
;
4704 sigaction(SIGINT
, &act
, NULL
);
4705 sigaction(SIGHUP
, &act
, NULL
);
4706 sigaction(SIGTERM
, &act
, NULL
);
4711 int main(int argc
, char **argv
)
4713 #ifdef CONFIG_GDBSTUB
4715 const char *gdbstub_port
;
4717 uint32_t boot_devices_bitmap
= 0;
4719 int snapshot
, linux_boot
, net_boot
;
4720 const char *initrd_filename
;
4721 const char *kernel_filename
, *kernel_cmdline
;
4722 const char *boot_devices
= "";
4723 DisplayState
*ds
= &display_state
;
4724 int cyls
, heads
, secs
, translation
;
4725 const char *net_clients
[MAX_NET_CLIENTS
];
4727 const char *bt_opts
[MAX_BT_CMDLINE
];
4731 const char *r
, *optarg
;
4732 CharDriverState
*monitor_hd
;
4733 const char *monitor_device
;
4734 const char *serial_devices
[MAX_SERIAL_PORTS
];
4735 int serial_device_index
;
4736 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4737 int parallel_device_index
;
4738 const char *loadvm
= NULL
;
4739 QEMUMachine
*machine
;
4740 const char *cpu_model
;
4741 const char *usb_devices
[MAX_USB_CMDLINE
];
4742 int usb_devices_index
;
4745 const char *pid_file
= NULL
;
4747 const char *incoming
= NULL
;
4749 LIST_INIT (&vm_change_state_head
);
4752 struct sigaction act
;
4753 sigfillset(&act
.sa_mask
);
4755 act
.sa_handler
= SIG_IGN
;
4756 sigaction(SIGPIPE
, &act
, NULL
);
4759 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4760 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4761 QEMU to run on a single CPU */
4766 h
= GetCurrentProcess();
4767 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4768 for(i
= 0; i
< 32; i
++) {
4769 if (mask
& (1 << i
))
4774 SetProcessAffinityMask(h
, mask
);
4780 register_machines();
4781 machine
= first_machine
;
4783 initrd_filename
= NULL
;
4785 vga_ram_size
= VGA_RAM_SIZE
;
4786 #ifdef CONFIG_GDBSTUB
4788 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4793 kernel_filename
= NULL
;
4794 kernel_cmdline
= "";
4795 cyls
= heads
= secs
= 0;
4796 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4797 monitor_device
= "vc";
4799 serial_devices
[0] = "vc:80Cx24C";
4800 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4801 serial_devices
[i
] = NULL
;
4802 serial_device_index
= 0;
4804 parallel_devices
[0] = "vc:640x480";
4805 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4806 parallel_devices
[i
] = NULL
;
4807 parallel_device_index
= 0;
4809 usb_devices_index
= 0;
4810 assigned_devices_index
= 0;
4829 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4831 const QEMUOption
*popt
;
4834 /* Treat --foo the same as -foo. */
4837 popt
= qemu_options
;
4840 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4844 if (!strcmp(popt
->name
, r
+ 1))
4848 if (popt
->flags
& HAS_ARG
) {
4849 if (optind
>= argc
) {
4850 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4854 optarg
= argv
[optind
++];
4859 switch(popt
->index
) {
4861 machine
= find_machine(optarg
);
4864 printf("Supported machines are:\n");
4865 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4866 printf("%-10s %s%s\n",
4868 m
== first_machine
? " (default)" : "");
4870 exit(*optarg
!= '?');
4873 case QEMU_OPTION_cpu
:
4874 /* hw initialization will check this */
4875 if (*optarg
== '?') {
4876 /* XXX: implement xxx_cpu_list for targets that still miss it */
4877 #if defined(cpu_list)
4878 cpu_list(stdout
, &fprintf
);
4885 case QEMU_OPTION_initrd
:
4886 initrd_filename
= optarg
;
4888 case QEMU_OPTION_hda
:
4890 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4892 hda_index
= drive_add(optarg
, HD_ALIAS
4893 ",cyls=%d,heads=%d,secs=%d%s",
4894 0, cyls
, heads
, secs
,
4895 translation
== BIOS_ATA_TRANSLATION_LBA
?
4897 translation
== BIOS_ATA_TRANSLATION_NONE
?
4898 ",trans=none" : "");
4900 case QEMU_OPTION_hdb
:
4901 case QEMU_OPTION_hdc
:
4902 case QEMU_OPTION_hdd
:
4903 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4905 case QEMU_OPTION_drive
:
4906 drive_add(NULL
, "%s", optarg
);
4908 case QEMU_OPTION_mtdblock
:
4909 drive_add(optarg
, MTD_ALIAS
);
4911 case QEMU_OPTION_sd
:
4912 drive_add(optarg
, SD_ALIAS
);
4914 case QEMU_OPTION_pflash
:
4915 drive_add(optarg
, PFLASH_ALIAS
);
4917 case QEMU_OPTION_snapshot
:
4920 case QEMU_OPTION_hdachs
:
4924 cyls
= strtol(p
, (char **)&p
, 0);
4925 if (cyls
< 1 || cyls
> 16383)
4930 heads
= strtol(p
, (char **)&p
, 0);
4931 if (heads
< 1 || heads
> 16)
4936 secs
= strtol(p
, (char **)&p
, 0);
4937 if (secs
< 1 || secs
> 63)
4941 if (!strcmp(p
, "none"))
4942 translation
= BIOS_ATA_TRANSLATION_NONE
;
4943 else if (!strcmp(p
, "lba"))
4944 translation
= BIOS_ATA_TRANSLATION_LBA
;
4945 else if (!strcmp(p
, "auto"))
4946 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4949 } else if (*p
!= '\0') {
4951 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4954 if (hda_index
!= -1)
4955 snprintf(drives_opt
[hda_index
].opt
,
4956 sizeof(drives_opt
[hda_index
].opt
),
4957 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4958 0, cyls
, heads
, secs
,
4959 translation
== BIOS_ATA_TRANSLATION_LBA
?
4961 translation
== BIOS_ATA_TRANSLATION_NONE
?
4962 ",trans=none" : "");
4965 case QEMU_OPTION_nographic
:
4968 #ifdef CONFIG_CURSES
4969 case QEMU_OPTION_curses
:
4973 case QEMU_OPTION_portrait
:
4976 case QEMU_OPTION_kernel
:
4977 kernel_filename
= optarg
;
4979 case QEMU_OPTION_append
:
4980 kernel_cmdline
= optarg
;
4982 case QEMU_OPTION_cdrom
:
4983 drive_add(optarg
, CDROM_ALIAS
);
4985 case QEMU_OPTION_boot
:
4986 boot_devices
= optarg
;
4987 /* We just do some generic consistency checks */
4989 /* Could easily be extended to 64 devices if needed */
4992 boot_devices_bitmap
= 0;
4993 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4994 /* Allowed boot devices are:
4995 * a b : floppy disk drives
4996 * c ... f : IDE disk drives
4997 * g ... m : machine implementation dependant drives
4998 * n ... p : network devices
4999 * It's up to each machine implementation to check
5000 * if the given boot devices match the actual hardware
5001 * implementation and firmware features.
5003 if (*p
< 'a' || *p
> 'q') {
5004 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5007 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5009 "Boot device '%c' was given twice\n",*p
);
5012 boot_devices_bitmap
|= 1 << (*p
- 'a');
5016 case QEMU_OPTION_fda
:
5017 case QEMU_OPTION_fdb
:
5018 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5021 case QEMU_OPTION_no_fd_bootchk
:
5025 case QEMU_OPTION_net
:
5026 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5027 fprintf(stderr
, "qemu: too many network clients\n");
5030 net_clients
[nb_net_clients
] = optarg
;
5034 case QEMU_OPTION_tftp
:
5035 tftp_prefix
= optarg
;
5037 case QEMU_OPTION_bootp
:
5038 bootp_filename
= optarg
;
5041 case QEMU_OPTION_smb
:
5042 net_slirp_smb(optarg
);
5045 case QEMU_OPTION_redir
:
5046 net_slirp_redir(optarg
);
5049 case QEMU_OPTION_bt
:
5050 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5051 fprintf(stderr
, "qemu: too many bluetooth options\n");
5054 bt_opts
[nb_bt_opts
++] = optarg
;
5057 case QEMU_OPTION_audio_help
:
5061 case QEMU_OPTION_soundhw
:
5062 select_soundhw (optarg
);
5068 case QEMU_OPTION_m
: {
5072 value
= strtoul(optarg
, &ptr
, 10);
5074 case 0: case 'M': case 'm':
5081 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5085 /* On 32-bit hosts, QEMU is limited by virtual address space */
5086 if (value
> (2047 << 20)
5088 && HOST_LONG_BITS
== 32
5091 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5094 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5095 fprintf(stderr
, "qemu: ram size too large\n");
5104 const CPULogItem
*item
;
5106 mask
= cpu_str_to_log_mask(optarg
);
5108 printf("Log items (comma separated):\n");
5109 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5110 printf("%-10s %s\n", item
->name
, item
->help
);
5117 #ifdef CONFIG_GDBSTUB
5122 gdbstub_port
= optarg
;
5128 case QEMU_OPTION_bios
:
5135 keyboard_layout
= optarg
;
5137 case QEMU_OPTION_localtime
:
5140 case QEMU_OPTION_vga
:
5141 select_vgahw (optarg
);
5148 w
= strtol(p
, (char **)&p
, 10);
5151 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5157 h
= strtol(p
, (char **)&p
, 10);
5162 depth
= strtol(p
, (char **)&p
, 10);
5163 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5164 depth
!= 24 && depth
!= 32)
5166 } else if (*p
== '\0') {
5167 depth
= graphic_depth
;
5174 graphic_depth
= depth
;
5177 case QEMU_OPTION_echr
:
5180 term_escape_char
= strtol(optarg
, &r
, 0);
5182 printf("Bad argument to echr\n");
5185 case QEMU_OPTION_monitor
:
5186 monitor_device
= optarg
;
5188 case QEMU_OPTION_serial
:
5189 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5190 fprintf(stderr
, "qemu: too many serial ports\n");
5193 serial_devices
[serial_device_index
] = optarg
;
5194 serial_device_index
++;
5196 case QEMU_OPTION_parallel
:
5197 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5198 fprintf(stderr
, "qemu: too many parallel ports\n");
5201 parallel_devices
[parallel_device_index
] = optarg
;
5202 parallel_device_index
++;
5204 case QEMU_OPTION_loadvm
:
5207 case QEMU_OPTION_full_screen
:
5211 case QEMU_OPTION_no_frame
:
5214 case QEMU_OPTION_alt_grab
:
5217 case QEMU_OPTION_no_quit
:
5221 case QEMU_OPTION_pidfile
:
5225 case QEMU_OPTION_win2k_hack
:
5226 win2k_install_hack
= 1;
5230 case QEMU_OPTION_no_kqemu
:
5233 case QEMU_OPTION_kernel_kqemu
:
5238 case QEMU_OPTION_enable_kvm
:
5246 case QEMU_OPTION_no_kvm
:
5249 case QEMU_OPTION_no_kvm_irqchip
: {
5250 extern int kvm_irqchip
, kvm_pit
;
5255 case QEMU_OPTION_no_kvm_pit
: {
5260 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5261 case QEMU_OPTION_pcidevice
:
5262 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5263 fprintf(stderr
, "Too many assigned devices\n");
5266 assigned_devices
[assigned_devices_index
] = optarg
;
5267 assigned_devices_index
++;
5271 case QEMU_OPTION_usb
:
5274 case QEMU_OPTION_usbdevice
:
5276 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5277 fprintf(stderr
, "Too many USB devices\n");
5280 usb_devices
[usb_devices_index
] = optarg
;
5281 usb_devices_index
++;
5283 case QEMU_OPTION_smp
:
5284 smp_cpus
= atoi(optarg
);
5286 fprintf(stderr
, "Invalid number of CPUs\n");
5290 case QEMU_OPTION_vnc
:
5291 vnc_display
= optarg
;
5293 case QEMU_OPTION_no_acpi
:
5296 case QEMU_OPTION_no_reboot
:
5299 case QEMU_OPTION_no_shutdown
:
5302 case QEMU_OPTION_show_cursor
:
5305 case QEMU_OPTION_uuid
:
5306 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5307 fprintf(stderr
, "Fail to parse UUID string."
5308 " Wrong format.\n");
5312 case QEMU_OPTION_daemonize
:
5315 case QEMU_OPTION_option_rom
:
5316 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5317 fprintf(stderr
, "Too many option ROMs\n");
5320 option_rom
[nb_option_roms
] = optarg
;
5323 case QEMU_OPTION_semihosting
:
5324 semihosting_enabled
= 1;
5326 case QEMU_OPTION_tdf
:
5329 case QEMU_OPTION_kvm_shadow_memory
:
5330 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5332 case QEMU_OPTION_mempath
:
5335 case QEMU_OPTION_name
:
5339 case QEMU_OPTION_prom_env
:
5340 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5341 fprintf(stderr
, "Too many prom variables\n");
5344 prom_envs
[nb_prom_envs
] = optarg
;
5348 case QEMU_OPTION_cpu_vendor
:
5349 cpu_vendor_string
= optarg
;
5352 case QEMU_OPTION_old_param
:
5356 case QEMU_OPTION_clock
:
5357 configure_alarms(optarg
);
5359 case QEMU_OPTION_startdate
:
5362 time_t rtc_start_date
;
5363 if (!strcmp(optarg
, "now")) {
5364 rtc_date_offset
= -1;
5366 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5374 } else if (sscanf(optarg
, "%d-%d-%d",
5377 &tm
.tm_mday
) == 3) {
5386 rtc_start_date
= mktimegm(&tm
);
5387 if (rtc_start_date
== -1) {
5389 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5390 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5393 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5397 case QEMU_OPTION_tb_size
:
5398 tb_size
= strtol(optarg
, NULL
, 0);
5402 case QEMU_OPTION_icount
:
5404 if (strcmp(optarg
, "auto") == 0) {
5405 icount_time_shift
= -1;
5407 icount_time_shift
= strtol(optarg
, NULL
, 0);
5410 case QEMU_OPTION_incoming
:
5417 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5418 if (kvm_allowed
&& kqemu_allowed
) {
5420 "You can not enable both KVM and kqemu at the same time\n");
5425 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5426 if (smp_cpus
> machine
->max_cpus
) {
5427 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5428 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5434 if (serial_device_index
== 0)
5435 serial_devices
[0] = "stdio";
5436 if (parallel_device_index
== 0)
5437 parallel_devices
[0] = "null";
5438 if (strncmp(monitor_device
, "vc", 2) == 0)
5439 monitor_device
= "stdio";
5446 if (pipe(fds
) == -1)
5457 len
= read(fds
[0], &status
, 1);
5458 if (len
== -1 && (errno
== EINTR
))
5463 else if (status
== 1) {
5464 fprintf(stderr
, "Could not acquire pidfile\n");
5481 signal(SIGTSTP
, SIG_IGN
);
5482 signal(SIGTTOU
, SIG_IGN
);
5483 signal(SIGTTIN
, SIG_IGN
);
5488 if (kvm_enabled()) {
5489 if (kvm_qemu_init() < 0) {
5490 extern int kvm_allowed
;
5491 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5492 #ifdef NO_CPU_EMULATION
5493 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5501 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5504 write(fds
[1], &status
, 1);
5506 fprintf(stderr
, "Could not acquire pid file\n");
5514 linux_boot
= (kernel_filename
!= NULL
);
5515 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5517 if (!linux_boot
&& net_boot
== 0 &&
5518 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5521 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5522 fprintf(stderr
, "-append only allowed with -kernel option\n");
5526 if (!linux_boot
&& initrd_filename
!= NULL
) {
5527 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5531 /* boot to floppy or the default cd if no hard disk defined yet */
5532 if (!boot_devices
[0]) {
5533 boot_devices
= "cad";
5535 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5538 if (init_timer_alarm() < 0) {
5539 fprintf(stderr
, "could not initialize alarm timer\n");
5542 if (use_icount
&& icount_time_shift
< 0) {
5544 /* 125MIPS seems a reasonable initial guess at the guest speed.
5545 It will be corrected fairly quickly anyway. */
5546 icount_time_shift
= 3;
5547 init_icount_adjust();
5554 /* init network clients */
5555 if (nb_net_clients
== 0) {
5556 /* if no clients, we use a default config */
5557 net_clients
[nb_net_clients
++] = "nic";
5559 net_clients
[nb_net_clients
++] = "user";
5563 for(i
= 0;i
< nb_net_clients
; i
++) {
5564 if (net_client_parse(net_clients
[i
]) < 0)
5570 /* XXX: this should be moved in the PC machine instantiation code */
5571 if (net_boot
!= 0) {
5573 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5574 const char *model
= nd_table
[i
].model
;
5576 if (net_boot
& (1 << i
)) {
5579 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5580 if (get_image_size(buf
) > 0) {
5581 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5582 fprintf(stderr
, "Too many option ROMs\n");
5585 option_rom
[nb_option_roms
] = strdup(buf
);
5592 fprintf(stderr
, "No valid PXE rom found for network device\n");
5598 /* init the bluetooth world */
5599 for (i
= 0; i
< nb_bt_opts
; i
++)
5600 if (bt_parse(bt_opts
[i
]))
5603 /* init the memory */
5604 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5606 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5608 if (ram_size
< phys_ram_size
) {
5609 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5610 machine
->name
, (unsigned long long) phys_ram_size
);
5614 phys_ram_size
= ram_size
;
5616 ram_size
= phys_ram_size
;
5619 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5621 phys_ram_size
+= ram_size
;
5624 /* Initialize kvm */
5625 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5626 #define KVM_EXTRA_PAGES 3
5628 #define KVM_EXTRA_PAGES 0
5630 if (kvm_enabled()) {
5631 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5632 if (kvm_qemu_create_context() < 0) {
5633 fprintf(stderr
, "Could not create KVM context\n");
5638 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5639 if (!phys_ram_base
) {
5640 fprintf(stderr
, "Could not allocate physical memory\n");
5644 /* init the dynamic translator */
5645 cpu_exec_init_all(tb_size
* 1024 * 1024);
5649 /* we always create the cdrom drive, even if no disk is there */
5651 if (nb_drives_opt
< MAX_DRIVES
)
5652 drive_add(NULL
, CDROM_ALIAS
);
5654 /* we always create at least one floppy */
5656 if (nb_drives_opt
< MAX_DRIVES
)
5657 drive_add(NULL
, FD_ALIAS
, 0);
5659 /* we always create one sd slot, even if no card is in it */
5661 if (nb_drives_opt
< MAX_DRIVES
)
5662 drive_add(NULL
, SD_ALIAS
);
5664 /* open the virtual block devices
5665 * note that migration with device
5666 * hot add/remove is broken.
5668 for(i
= 0; i
< nb_drives_opt
; i
++)
5669 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5672 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5673 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5676 memset(&display_state
, 0, sizeof(display_state
));
5679 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5682 /* nearly nothing to do */
5683 dumb_display_init(ds
);
5684 } else if (vnc_display
!= NULL
) {
5685 vnc_display_init(ds
);
5686 if (vnc_display_open(ds
, vnc_display
) < 0)
5689 #if defined(CONFIG_CURSES)
5691 curses_display_init(ds
, full_screen
);
5695 #if defined(CONFIG_SDL)
5696 sdl_display_init(ds
, full_screen
, no_frame
);
5697 #elif defined(CONFIG_COCOA)
5698 cocoa_display_init(ds
, full_screen
);
5700 dumb_display_init(ds
);
5705 /* must be after terminal init, SDL library changes signal handlers */
5709 /* Maintain compatibility with multiple stdio monitors */
5710 if (!strcmp(monitor_device
,"stdio")) {
5711 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5712 const char *devname
= serial_devices
[i
];
5713 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5714 monitor_device
= NULL
;
5716 } else if (devname
&& !strcmp(devname
,"stdio")) {
5717 monitor_device
= NULL
;
5718 serial_devices
[i
] = "mon:stdio";
5723 if (monitor_device
) {
5724 monitor_hd
= qemu_chr_open("monitor", monitor_device
);
5726 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5729 monitor_init(monitor_hd
, !nographic
);
5732 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5733 const char *devname
= serial_devices
[i
];
5734 if (devname
&& strcmp(devname
, "none")) {
5736 snprintf(label
, sizeof(label
), "serial%d", i
);
5737 serial_hds
[i
] = qemu_chr_open(label
, devname
);
5738 if (!serial_hds
[i
]) {
5739 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5743 if (strstart(devname
, "vc", 0))
5744 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5748 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5749 const char *devname
= parallel_devices
[i
];
5750 if (devname
&& strcmp(devname
, "none")) {
5752 snprintf(label
, sizeof(label
), "parallel%d", i
);
5753 parallel_hds
[i
] = qemu_chr_open(label
, devname
);
5754 if (!parallel_hds
[i
]) {
5755 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5759 if (strstart(devname
, "vc", 0))
5760 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5768 if (kvm_enabled()) {
5771 ret
= kvm_init(smp_cpus
);
5773 fprintf(stderr
, "failed to initialize KVM\n");
5779 machine
->init(ram_size
, vga_ram_size
, boot_devices
, ds
,
5780 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5782 current_machine
= machine
;
5784 /* init USB devices */
5786 for(i
= 0; i
< usb_devices_index
; i
++) {
5787 if (usb_device_add(usb_devices
[i
]) < 0) {
5788 fprintf(stderr
, "Warning: could not add USB device %s\n",
5794 if (display_state
.dpy_refresh
) {
5795 display_state
.gui_timer
= qemu_new_timer(rt_clock
, gui_update
, &display_state
);
5796 qemu_mod_timer(display_state
.gui_timer
, qemu_get_clock(rt_clock
));
5799 #ifdef CONFIG_GDBSTUB
5801 /* XXX: use standard host:port notation and modify options
5803 if (gdbserver_start(gdbstub_port
) < 0) {
5804 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5815 autostart
= 0; /* fixme how to deal with -daemonize */
5816 qemu_start_incoming_migration(incoming
);
5820 /* XXX: simplify init */
5833 len
= write(fds
[1], &status
, 1);
5834 if (len
== -1 && (errno
== EINTR
))
5841 TFR(fd
= open("/dev/null", O_RDWR
));