4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "hw/boards.h"
27 #include "hw/pcmcia.h"
29 #include "hw/audiodev.h"
37 #include "qemu-timer.h"
38 #include "qemu-char.h"
39 #include "cache-utils.h"
41 #include "audio/audio.h"
42 #include "hw/device-assignment.h"
43 #include "migration.h"
57 #include <sys/times.h>
61 #include <sys/ioctl.h>
62 #include <sys/resource.h>
63 #include <sys/socket.h>
64 #include <netinet/in.h>
66 #if defined(__NetBSD__)
67 #include <net/if_tap.h>
70 #include <linux/if_tun.h>
72 #include <arpa/inet.h>
75 #include <sys/select.h>
83 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
84 #include <freebsd/stdlib.h>
89 #include <linux/rtc.h>
91 /* For the benefit of older linux systems which don't supply it,
92 we use a local copy of hpet.h. */
93 /* #include <linux/hpet.h> */
96 #include <linux/ppdev.h>
97 #include <linux/parport.h>
100 #include <sys/stat.h>
101 #include <sys/ethernet.h>
102 #include <sys/sockio.h>
103 #include <netinet/arp.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/ip.h>
107 #include <netinet/ip_icmp.h> // must come after ip.h
108 #include <netinet/udp.h>
109 #include <netinet/tcp.h>
117 #include "qemu_socket.h"
119 #if defined(CONFIG_SLIRP)
120 #include "libslirp.h"
123 #if defined(__OpenBSD__)
127 #if defined(CONFIG_VDE)
128 #include <libvdeplug.h>
133 #include <sys/timeb.h>
134 #include <mmsystem.h>
135 #define getopt_long_only getopt_long
136 #define memalign(align, size) malloc(size)
143 #endif /* CONFIG_SDL */
147 #define main qemu_main
148 #endif /* CONFIG_COCOA */
152 #include "exec-all.h"
154 #include "qemu-kvm.h"
156 //#define DEBUG_UNUSED_IOPORT
157 //#define DEBUG_IOPORT
159 //#define DEBUG_SLIRP
163 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
165 # define LOG_IOPORT(...) do { } while (0)
168 #define DEFAULT_RAM_SIZE 128
170 /* Max number of USB devices that can be specified on the commandline. */
171 #define MAX_USB_CMDLINE 8
173 /* Max number of bluetooth switches on the commandline. */
174 #define MAX_BT_CMDLINE 10
176 /* XXX: use a two level table to limit memory usage */
177 #define MAX_IOPORTS 65536
179 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
180 const char *bios_name
= NULL
;
181 static void *ioport_opaque
[MAX_IOPORTS
];
182 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
183 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
184 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
185 to store the VM snapshots */
186 DriveInfo drives_table
[MAX_DRIVES
+1];
188 int extboot_drive
= -1;
189 static int vga_ram_size
;
190 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
191 static DisplayState
*display_state
;
195 const char* keyboard_layout
= NULL
;
196 int64_t ticks_per_sec
;
199 NICInfo nd_table
[MAX_NICS
];
201 static int rtc_utc
= 1;
202 static int rtc_date_offset
= -1; /* -1 means no change */
203 int cirrus_vga_enabled
= 1;
204 int std_vga_enabled
= 0;
205 int vmsvga_enabled
= 0;
207 int graphic_width
= 1024;
208 int graphic_height
= 768;
209 int graphic_depth
= 8;
211 int graphic_width
= 800;
212 int graphic_height
= 600;
213 int graphic_depth
= 15;
215 static int full_screen
= 0;
217 static int no_frame
= 0;
220 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
221 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
222 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
224 int win2k_install_hack
= 0;
228 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
229 int assigned_devices_index
;
231 const char *vnc_display
;
232 int acpi_enabled
= 1;
238 int graphic_rotate
= 0;
240 const char *incoming
;
241 const char *option_rom
[MAX_OPTION_ROMS
];
243 int semihosting_enabled
= 0;
244 int time_drift_fix
= 0;
245 unsigned int kvm_shadow_memory
= 0;
246 const char *mem_path
= NULL
;
248 const char *cpu_vendor_string
;
252 const char *qemu_name
;
254 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
255 unsigned int nb_prom_envs
= 0;
256 const char *prom_envs
[MAX_PROM_ENVS
];
259 const char *nvram
= NULL
;
260 struct drive_opt drives_opt
[MAX_DRIVES
];
262 static CPUState
*cur_cpu
;
263 static CPUState
*next_cpu
;
264 static int event_pending
= 1;
265 /* Conversion factor from emulated instructions to virtual clock ticks. */
266 static int icount_time_shift
;
267 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
268 #define MAX_ICOUNT_SHIFT 10
269 /* Compensate for varying guest execution speed. */
270 static int64_t qemu_icount_bias
;
271 static QEMUTimer
*icount_rt_timer
;
272 static QEMUTimer
*icount_vm_timer
;
273 static QEMUTimer
*nographic_timer
;
275 uint8_t qemu_uuid
[16];
277 /* KVM runs the main loop in a separate thread. If we update one of the lists
278 * that are polled before or after select(), we need to make sure to break out
279 * of the select() to ensure the new item is serviced.
281 static void main_loop_break(void)
284 qemu_kvm_notify_work();
287 /***********************************************************/
288 /* x86 ISA bus support */
290 target_phys_addr_t isa_mem_base
= 0;
293 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
294 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
296 static uint32_t ioport_read(int index
, uint32_t address
)
298 static IOPortReadFunc
*default_func
[3] = {
299 default_ioport_readb
,
300 default_ioport_readw
,
303 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
305 func
= default_func
[index
];
306 return func(ioport_opaque
[address
], address
);
309 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
311 static IOPortWriteFunc
*default_func
[3] = {
312 default_ioport_writeb
,
313 default_ioport_writew
,
314 default_ioport_writel
316 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
318 func
= default_func
[index
];
319 func(ioport_opaque
[address
], address
, data
);
322 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
324 #ifdef DEBUG_UNUSED_IOPORT
325 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
330 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
332 #ifdef DEBUG_UNUSED_IOPORT
333 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
337 /* default is to make two byte accesses */
338 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
341 data
= ioport_read(0, address
);
342 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
343 data
|= ioport_read(0, address
) << 8;
347 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
349 ioport_write(0, address
, data
& 0xff);
350 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
351 ioport_write(0, address
, (data
>> 8) & 0xff);
354 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
356 #ifdef DEBUG_UNUSED_IOPORT
357 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
362 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
364 #ifdef DEBUG_UNUSED_IOPORT
365 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
369 /* size is the word size in byte */
370 int register_ioport_read(int start
, int length
, int size
,
371 IOPortReadFunc
*func
, void *opaque
)
377 } else if (size
== 2) {
379 } else if (size
== 4) {
382 hw_error("register_ioport_read: invalid size");
385 for(i
= start
; i
< start
+ length
; i
+= size
) {
386 ioport_read_table
[bsize
][i
] = func
;
387 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
388 hw_error("register_ioport_read: invalid opaque");
389 ioport_opaque
[i
] = opaque
;
394 /* size is the word size in byte */
395 int register_ioport_write(int start
, int length
, int size
,
396 IOPortWriteFunc
*func
, void *opaque
)
402 } else if (size
== 2) {
404 } else if (size
== 4) {
407 hw_error("register_ioport_write: invalid size");
410 for(i
= start
; i
< start
+ length
; i
+= size
) {
411 ioport_write_table
[bsize
][i
] = func
;
412 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
413 hw_error("register_ioport_write: invalid opaque");
414 ioport_opaque
[i
] = opaque
;
419 void isa_unassign_ioport(int start
, int length
)
423 for(i
= start
; i
< start
+ length
; i
++) {
424 ioport_read_table
[0][i
] = default_ioport_readb
;
425 ioport_read_table
[1][i
] = default_ioport_readw
;
426 ioport_read_table
[2][i
] = default_ioport_readl
;
428 ioport_write_table
[0][i
] = default_ioport_writeb
;
429 ioport_write_table
[1][i
] = default_ioport_writew
;
430 ioport_write_table
[2][i
] = default_ioport_writel
;
432 ioport_opaque
[i
] = NULL
;
436 /***********************************************************/
438 void cpu_outb(CPUState
*env
, int addr
, int val
)
440 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
441 ioport_write(0, addr
, val
);
444 env
->last_io_time
= cpu_get_time_fast();
448 void cpu_outw(CPUState
*env
, int addr
, int val
)
450 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
451 ioport_write(1, addr
, val
);
454 env
->last_io_time
= cpu_get_time_fast();
458 void cpu_outl(CPUState
*env
, int addr
, int val
)
460 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
461 ioport_write(2, addr
, val
);
464 env
->last_io_time
= cpu_get_time_fast();
468 int cpu_inb(CPUState
*env
, int addr
)
471 val
= ioport_read(0, addr
);
472 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
475 env
->last_io_time
= cpu_get_time_fast();
480 int cpu_inw(CPUState
*env
, int addr
)
483 val
= ioport_read(1, addr
);
484 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
487 env
->last_io_time
= cpu_get_time_fast();
492 int cpu_inl(CPUState
*env
, int addr
)
495 val
= ioport_read(2, addr
);
496 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
499 env
->last_io_time
= cpu_get_time_fast();
504 /***********************************************************/
505 void hw_error(const char *fmt
, ...)
511 fprintf(stderr
, "qemu: hardware error: ");
512 vfprintf(stderr
, fmt
, ap
);
513 fprintf(stderr
, "\n");
514 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
515 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
517 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
519 cpu_dump_state(env
, stderr
, fprintf
, 0);
529 static QEMUBalloonEvent
*qemu_balloon_event
;
530 void *qemu_balloon_event_opaque
;
532 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
534 qemu_balloon_event
= func
;
535 qemu_balloon_event_opaque
= opaque
;
538 void qemu_balloon(ram_addr_t target
)
540 if (qemu_balloon_event
)
541 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
544 ram_addr_t
qemu_balloon_status(void)
546 if (qemu_balloon_event
)
547 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
551 /***********************************************************/
554 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
555 static void *qemu_put_kbd_event_opaque
;
556 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
557 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
559 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
561 qemu_put_kbd_event_opaque
= opaque
;
562 qemu_put_kbd_event
= func
;
565 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
566 void *opaque
, int absolute
,
569 QEMUPutMouseEntry
*s
, *cursor
;
571 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
575 s
->qemu_put_mouse_event
= func
;
576 s
->qemu_put_mouse_event_opaque
= opaque
;
577 s
->qemu_put_mouse_event_absolute
= absolute
;
578 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
581 if (!qemu_put_mouse_event_head
) {
582 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
586 cursor
= qemu_put_mouse_event_head
;
587 while (cursor
->next
!= NULL
)
588 cursor
= cursor
->next
;
591 qemu_put_mouse_event_current
= s
;
596 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
598 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
600 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
603 cursor
= qemu_put_mouse_event_head
;
604 while (cursor
!= NULL
&& cursor
!= entry
) {
606 cursor
= cursor
->next
;
609 if (cursor
== NULL
) // does not exist or list empty
611 else if (prev
== NULL
) { // entry is head
612 qemu_put_mouse_event_head
= cursor
->next
;
613 if (qemu_put_mouse_event_current
== entry
)
614 qemu_put_mouse_event_current
= cursor
->next
;
615 qemu_free(entry
->qemu_put_mouse_event_name
);
620 prev
->next
= entry
->next
;
622 if (qemu_put_mouse_event_current
== entry
)
623 qemu_put_mouse_event_current
= prev
;
625 qemu_free(entry
->qemu_put_mouse_event_name
);
629 void kbd_put_keycode(int keycode
)
631 if (qemu_put_kbd_event
) {
632 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
636 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
638 QEMUPutMouseEvent
*mouse_event
;
639 void *mouse_event_opaque
;
642 if (!qemu_put_mouse_event_current
) {
647 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
649 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
652 if (graphic_rotate
) {
653 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
656 width
= graphic_width
- 1;
657 mouse_event(mouse_event_opaque
,
658 width
- dy
, dx
, dz
, buttons_state
);
660 mouse_event(mouse_event_opaque
,
661 dx
, dy
, dz
, buttons_state
);
665 int kbd_mouse_is_absolute(void)
667 if (!qemu_put_mouse_event_current
)
670 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
673 void do_info_mice(void)
675 QEMUPutMouseEntry
*cursor
;
678 if (!qemu_put_mouse_event_head
) {
679 term_printf("No mouse devices connected\n");
683 term_printf("Mouse devices available:\n");
684 cursor
= qemu_put_mouse_event_head
;
685 while (cursor
!= NULL
) {
686 term_printf("%c Mouse #%d: %s\n",
687 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
688 index
, cursor
->qemu_put_mouse_event_name
);
690 cursor
= cursor
->next
;
694 void do_mouse_set(int index
)
696 QEMUPutMouseEntry
*cursor
;
699 if (!qemu_put_mouse_event_head
) {
700 term_printf("No mouse devices connected\n");
704 cursor
= qemu_put_mouse_event_head
;
705 while (cursor
!= NULL
&& index
!= i
) {
707 cursor
= cursor
->next
;
711 qemu_put_mouse_event_current
= cursor
;
713 term_printf("Mouse at given index not found\n");
716 /* compute with 96 bit intermediate result: (a*b)/c */
717 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
722 #ifdef WORDS_BIGENDIAN
732 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
733 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
736 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
740 /***********************************************************/
741 /* real time host monotonic timer */
743 #define QEMU_TIMER_BASE 1000000000LL
747 static int64_t clock_freq
;
749 static void init_get_clock(void)
753 ret
= QueryPerformanceFrequency(&freq
);
755 fprintf(stderr
, "Could not calibrate ticks\n");
758 clock_freq
= freq
.QuadPart
;
761 static int64_t get_clock(void)
764 QueryPerformanceCounter(&ti
);
765 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
770 static int use_rt_clock
;
772 static void init_get_clock(void)
775 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
778 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
785 static int64_t get_clock(void)
787 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
790 clock_gettime(CLOCK_MONOTONIC
, &ts
);
791 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
795 /* XXX: using gettimeofday leads to problems if the date
796 changes, so it should be avoided. */
798 gettimeofday(&tv
, NULL
);
799 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
804 /* Return the virtual CPU time, based on the instruction counter. */
805 static int64_t cpu_get_icount(void)
808 CPUState
*env
= cpu_single_env
;;
809 icount
= qemu_icount
;
812 fprintf(stderr
, "Bad clock read\n");
813 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
815 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
818 /***********************************************************/
819 /* guest cycle counter */
821 static int64_t cpu_ticks_prev
;
822 static int64_t cpu_ticks_offset
;
823 static int64_t cpu_clock_offset
;
824 static int cpu_ticks_enabled
;
826 /* return the host CPU cycle counter and handle stop/restart */
827 int64_t cpu_get_ticks(void)
830 return cpu_get_icount();
832 if (!cpu_ticks_enabled
) {
833 return cpu_ticks_offset
;
836 ticks
= cpu_get_real_ticks();
837 if (cpu_ticks_prev
> ticks
) {
838 /* Note: non increasing ticks may happen if the host uses
840 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
842 cpu_ticks_prev
= ticks
;
843 return ticks
+ cpu_ticks_offset
;
847 /* return the host CPU monotonic timer and handle stop/restart */
848 static int64_t cpu_get_clock(void)
851 if (!cpu_ticks_enabled
) {
852 return cpu_clock_offset
;
855 return ti
+ cpu_clock_offset
;
859 /* enable cpu_get_ticks() */
860 void cpu_enable_ticks(void)
862 if (!cpu_ticks_enabled
) {
863 cpu_ticks_offset
-= cpu_get_real_ticks();
864 cpu_clock_offset
-= get_clock();
865 cpu_ticks_enabled
= 1;
869 /* disable cpu_get_ticks() : the clock is stopped. You must not call
870 cpu_get_ticks() after that. */
871 void cpu_disable_ticks(void)
873 if (cpu_ticks_enabled
) {
874 cpu_ticks_offset
= cpu_get_ticks();
875 cpu_clock_offset
= cpu_get_clock();
876 cpu_ticks_enabled
= 0;
880 /***********************************************************/
883 #define QEMU_TIMER_REALTIME 0
884 #define QEMU_TIMER_VIRTUAL 1
888 /* XXX: add frequency */
896 struct QEMUTimer
*next
;
899 struct qemu_alarm_timer
{
903 int (*start
)(struct qemu_alarm_timer
*t
);
904 void (*stop
)(struct qemu_alarm_timer
*t
);
905 void (*rearm
)(struct qemu_alarm_timer
*t
);
909 #define ALARM_FLAG_DYNTICKS 0x1
910 #define ALARM_FLAG_EXPIRED 0x2
912 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
914 return t
->flags
& ALARM_FLAG_DYNTICKS
;
917 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
919 if (!alarm_has_dynticks(t
))
925 /* TODO: MIN_TIMER_REARM_US should be optimized */
926 #define MIN_TIMER_REARM_US 250
928 static struct qemu_alarm_timer
*alarm_timer
;
930 static int alarm_timer_rfd
, alarm_timer_wfd
;
935 struct qemu_alarm_win32
{
939 } alarm_win32_data
= {0, NULL
, -1};
941 static int win32_start_timer(struct qemu_alarm_timer
*t
);
942 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
943 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
947 static int unix_start_timer(struct qemu_alarm_timer
*t
);
948 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
952 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
953 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
954 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
956 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
957 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
959 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
960 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
962 #endif /* __linux__ */
966 /* Correlation between real and virtual time is always going to be
967 fairly approximate, so ignore small variation.
968 When the guest is idle real and virtual time will be aligned in
970 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
972 static void icount_adjust(void)
977 static int64_t last_delta
;
978 /* If the VM is not running, then do nothing. */
982 cur_time
= cpu_get_clock();
983 cur_icount
= qemu_get_clock(vm_clock
);
984 delta
= cur_icount
- cur_time
;
985 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
987 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
988 && icount_time_shift
> 0) {
989 /* The guest is getting too far ahead. Slow time down. */
993 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
994 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
995 /* The guest is getting too far behind. Speed time up. */
999 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1002 static void icount_adjust_rt(void * opaque
)
1004 qemu_mod_timer(icount_rt_timer
,
1005 qemu_get_clock(rt_clock
) + 1000);
1009 static void icount_adjust_vm(void * opaque
)
1011 qemu_mod_timer(icount_vm_timer
,
1012 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1016 static void init_icount_adjust(void)
1018 /* Have both realtime and virtual time triggers for speed adjustment.
1019 The realtime trigger catches emulated time passing too slowly,
1020 the virtual time trigger catches emulated time passing too fast.
1021 Realtime triggers occur even when idle, so use them less frequently
1022 than VM triggers. */
1023 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1024 qemu_mod_timer(icount_rt_timer
,
1025 qemu_get_clock(rt_clock
) + 1000);
1026 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1027 qemu_mod_timer(icount_vm_timer
,
1028 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1031 static struct qemu_alarm_timer alarm_timers
[] = {
1034 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1035 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1036 /* HPET - if available - is preferred */
1037 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1038 /* ...otherwise try RTC */
1039 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1041 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1043 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1044 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1045 {"win32", 0, win32_start_timer
,
1046 win32_stop_timer
, NULL
, &alarm_win32_data
},
1051 static void show_available_alarms(void)
1055 printf("Available alarm timers, in order of precedence:\n");
1056 for (i
= 0; alarm_timers
[i
].name
; i
++)
1057 printf("%s\n", alarm_timers
[i
].name
);
1060 static void configure_alarms(char const *opt
)
1064 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1067 struct qemu_alarm_timer tmp
;
1069 if (!strcmp(opt
, "?")) {
1070 show_available_alarms();
1076 /* Reorder the array */
1077 name
= strtok(arg
, ",");
1079 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1080 if (!strcmp(alarm_timers
[i
].name
, name
))
1085 fprintf(stderr
, "Unknown clock %s\n", name
);
1094 tmp
= alarm_timers
[i
];
1095 alarm_timers
[i
] = alarm_timers
[cur
];
1096 alarm_timers
[cur
] = tmp
;
1100 name
= strtok(NULL
, ",");
1106 /* Disable remaining timers */
1107 for (i
= cur
; i
< count
; i
++)
1108 alarm_timers
[i
].name
= NULL
;
1110 show_available_alarms();
1115 QEMUClock
*rt_clock
;
1116 QEMUClock
*vm_clock
;
1118 static QEMUTimer
*active_timers
[2];
1120 static QEMUClock
*qemu_new_clock(int type
)
1123 clock
= qemu_mallocz(sizeof(QEMUClock
));
1130 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1134 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1137 ts
->opaque
= opaque
;
1141 void qemu_free_timer(QEMUTimer
*ts
)
1146 /* stop a timer, but do not dealloc it */
1147 void qemu_del_timer(QEMUTimer
*ts
)
1151 /* NOTE: this code must be signal safe because
1152 qemu_timer_expired() can be called from a signal. */
1153 pt
= &active_timers
[ts
->clock
->type
];
1166 /* modify the current timer so that it will be fired when current_time
1167 >= expire_time. The corresponding callback will be called. */
1168 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1174 /* add the timer in the sorted list */
1175 /* NOTE: this code must be signal safe because
1176 qemu_timer_expired() can be called from a signal. */
1177 pt
= &active_timers
[ts
->clock
->type
];
1182 if (t
->expire_time
> expire_time
)
1186 ts
->expire_time
= expire_time
;
1190 /* Rearm if necessary */
1191 if (pt
== &active_timers
[ts
->clock
->type
]) {
1192 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1193 qemu_rearm_alarm_timer(alarm_timer
);
1195 /* Interrupt execution to force deadline recalculation. */
1196 if (use_icount
&& cpu_single_env
) {
1197 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1202 int qemu_timer_pending(QEMUTimer
*ts
)
1205 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1212 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1216 return (timer_head
->expire_time
<= current_time
);
1219 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1225 if (!ts
|| ts
->expire_time
> current_time
)
1227 /* remove timer from the list before calling the callback */
1228 *ptimer_head
= ts
->next
;
1231 /* run the callback (the timer list can be modified) */
1236 int64_t qemu_get_clock(QEMUClock
*clock
)
1238 switch(clock
->type
) {
1239 case QEMU_TIMER_REALTIME
:
1240 return get_clock() / 1000000;
1242 case QEMU_TIMER_VIRTUAL
:
1244 return cpu_get_icount();
1246 return cpu_get_clock();
1251 static void init_timers(void)
1254 ticks_per_sec
= QEMU_TIMER_BASE
;
1255 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1256 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1260 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1262 uint64_t expire_time
;
1264 if (qemu_timer_pending(ts
)) {
1265 expire_time
= ts
->expire_time
;
1269 qemu_put_be64(f
, expire_time
);
1272 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1274 uint64_t expire_time
;
1276 expire_time
= qemu_get_be64(f
);
1277 if (expire_time
!= -1) {
1278 qemu_mod_timer(ts
, expire_time
);
1284 static void timer_save(QEMUFile
*f
, void *opaque
)
1286 if (cpu_ticks_enabled
) {
1287 hw_error("cannot save state if virtual timers are running");
1289 qemu_put_be64(f
, cpu_ticks_offset
);
1290 qemu_put_be64(f
, ticks_per_sec
);
1291 qemu_put_be64(f
, cpu_clock_offset
);
1294 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1296 if (version_id
!= 1 && version_id
!= 2)
1298 if (cpu_ticks_enabled
) {
1301 cpu_ticks_offset
=qemu_get_be64(f
);
1302 ticks_per_sec
=qemu_get_be64(f
);
1303 if (version_id
== 2) {
1304 cpu_clock_offset
=qemu_get_be64(f
);
1310 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1311 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1313 static void host_alarm_handler(int host_signum
)
1317 #define DISP_FREQ 1000
1319 static int64_t delta_min
= INT64_MAX
;
1320 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1322 ti
= qemu_get_clock(vm_clock
);
1323 if (last_clock
!= 0) {
1324 delta
= ti
- last_clock
;
1325 if (delta
< delta_min
)
1327 if (delta
> delta_max
)
1330 if (++count
== DISP_FREQ
) {
1331 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1332 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1333 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1334 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1335 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1337 delta_min
= INT64_MAX
;
1346 alarm_has_dynticks(alarm_timer
) ||
1348 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1349 qemu_get_clock(vm_clock
))) ||
1350 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1351 qemu_get_clock(rt_clock
))) {
1352 CPUState
*env
= next_cpu
;
1355 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1356 SetEvent(data
->host_alarm
);
1358 static const char byte
= 0;
1359 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1361 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1364 /* stop the currently executing cpu because a timer occured */
1365 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1367 if (env
->kqemu_enabled
) {
1368 kqemu_cpu_interrupt(env
);
1376 static int64_t qemu_next_deadline(void)
1380 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1381 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1382 qemu_get_clock(vm_clock
);
1384 /* To avoid problems with overflow limit this to 2^32. */
1394 #if defined(__linux__) || defined(_WIN32)
1395 static uint64_t qemu_next_deadline_dyntick(void)
1403 delta
= (qemu_next_deadline() + 999) / 1000;
1405 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1406 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1407 qemu_get_clock(rt_clock
))*1000;
1408 if (rtdelta
< delta
)
1412 if (delta
< MIN_TIMER_REARM_US
)
1413 delta
= MIN_TIMER_REARM_US
;
1421 /* Sets a specific flag */
1422 static int fcntl_setfl(int fd
, int flag
)
1426 flags
= fcntl(fd
, F_GETFL
);
1430 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1436 #if defined(__linux__)
1438 #define RTC_FREQ 1024
1440 static void enable_sigio_timer(int fd
)
1442 struct sigaction act
;
1445 sigfillset(&act
.sa_mask
);
1447 act
.sa_handler
= host_alarm_handler
;
1449 sigaction(SIGIO
, &act
, NULL
);
1450 fcntl_setfl(fd
, O_ASYNC
);
1451 fcntl(fd
, F_SETOWN
, getpid());
1454 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1456 struct hpet_info info
;
1459 fd
= open("/dev/hpet", O_RDONLY
);
1464 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1466 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1467 "error, but for better emulation accuracy type:\n"
1468 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1472 /* Check capabilities */
1473 r
= ioctl(fd
, HPET_INFO
, &info
);
1477 /* Enable periodic mode */
1478 r
= ioctl(fd
, HPET_EPI
, 0);
1479 if (info
.hi_flags
&& (r
< 0))
1482 /* Enable interrupt */
1483 r
= ioctl(fd
, HPET_IE_ON
, 0);
1487 enable_sigio_timer(fd
);
1488 t
->priv
= (void *)(long)fd
;
1496 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1498 int fd
= (long)t
->priv
;
1503 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1506 unsigned long current_rtc_freq
= 0;
1508 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1511 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1512 if (current_rtc_freq
!= RTC_FREQ
&&
1513 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1514 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1515 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1516 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1519 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1525 enable_sigio_timer(rtc_fd
);
1527 t
->priv
= (void *)(long)rtc_fd
;
1532 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1534 int rtc_fd
= (long)t
->priv
;
1539 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1543 struct sigaction act
;
1545 sigfillset(&act
.sa_mask
);
1547 act
.sa_handler
= host_alarm_handler
;
1549 sigaction(SIGALRM
, &act
, NULL
);
1551 ev
.sigev_value
.sival_int
= 0;
1552 ev
.sigev_notify
= SIGEV_SIGNAL
;
1553 ev
.sigev_signo
= SIGALRM
;
1555 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1556 perror("timer_create");
1558 /* disable dynticks */
1559 fprintf(stderr
, "Dynamic Ticks disabled\n");
1564 t
->priv
= (void *)(long)host_timer
;
1569 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1571 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1573 timer_delete(host_timer
);
1576 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1578 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1579 struct itimerspec timeout
;
1580 int64_t nearest_delta_us
= INT64_MAX
;
1583 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1584 !active_timers
[QEMU_TIMER_VIRTUAL
])
1587 nearest_delta_us
= qemu_next_deadline_dyntick();
1589 /* check whether a timer is already running */
1590 if (timer_gettime(host_timer
, &timeout
)) {
1592 fprintf(stderr
, "Internal timer error: aborting\n");
1595 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1596 if (current_us
&& current_us
<= nearest_delta_us
)
1599 timeout
.it_interval
.tv_sec
= 0;
1600 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1601 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1602 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1603 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1605 fprintf(stderr
, "Internal timer error: aborting\n");
1610 #endif /* defined(__linux__) */
1612 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1614 struct sigaction act
;
1615 struct itimerval itv
;
1619 sigfillset(&act
.sa_mask
);
1621 act
.sa_handler
= host_alarm_handler
;
1623 sigaction(SIGALRM
, &act
, NULL
);
1625 itv
.it_interval
.tv_sec
= 0;
1626 /* for i386 kernel 2.6 to get 1 ms */
1627 itv
.it_interval
.tv_usec
= 999;
1628 itv
.it_value
.tv_sec
= 0;
1629 itv
.it_value
.tv_usec
= 10 * 1000;
1631 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1638 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1640 struct itimerval itv
;
1642 memset(&itv
, 0, sizeof(itv
));
1643 setitimer(ITIMER_REAL
, &itv
, NULL
);
1646 #endif /* !defined(_WIN32) */
1648 static void try_to_rearm_timer(void *opaque
)
1650 struct qemu_alarm_timer
*t
= opaque
;
1654 /* Drain the notify pipe */
1657 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1658 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1661 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1662 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1663 qemu_rearm_alarm_timer(alarm_timer
);
1669 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1672 struct qemu_alarm_win32
*data
= t
->priv
;
1675 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1676 if (!data
->host_alarm
) {
1677 perror("Failed CreateEvent");
1681 memset(&tc
, 0, sizeof(tc
));
1682 timeGetDevCaps(&tc
, sizeof(tc
));
1684 if (data
->period
< tc
.wPeriodMin
)
1685 data
->period
= tc
.wPeriodMin
;
1687 timeBeginPeriod(data
->period
);
1689 flags
= TIME_CALLBACK_FUNCTION
;
1690 if (alarm_has_dynticks(t
))
1691 flags
|= TIME_ONESHOT
;
1693 flags
|= TIME_PERIODIC
;
1695 data
->timerId
= timeSetEvent(1, // interval (ms)
1696 data
->period
, // resolution
1697 host_alarm_handler
, // function
1698 (DWORD
)t
, // parameter
1701 if (!data
->timerId
) {
1702 perror("Failed to initialize win32 alarm timer");
1704 timeEndPeriod(data
->period
);
1705 CloseHandle(data
->host_alarm
);
1709 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1714 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1716 struct qemu_alarm_win32
*data
= t
->priv
;
1718 timeKillEvent(data
->timerId
);
1719 timeEndPeriod(data
->period
);
1721 CloseHandle(data
->host_alarm
);
1724 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1726 struct qemu_alarm_win32
*data
= t
->priv
;
1727 uint64_t nearest_delta_us
;
1729 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1730 !active_timers
[QEMU_TIMER_VIRTUAL
])
1733 nearest_delta_us
= qemu_next_deadline_dyntick();
1734 nearest_delta_us
/= 1000;
1736 timeKillEvent(data
->timerId
);
1738 data
->timerId
= timeSetEvent(1,
1742 TIME_ONESHOT
| TIME_PERIODIC
);
1744 if (!data
->timerId
) {
1745 perror("Failed to re-arm win32 alarm timer");
1747 timeEndPeriod(data
->period
);
1748 CloseHandle(data
->host_alarm
);
1755 static int init_timer_alarm(void)
1757 struct qemu_alarm_timer
*t
= NULL
;
1767 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1771 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1775 alarm_timer_rfd
= fds
[0];
1776 alarm_timer_wfd
= fds
[1];
1779 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1780 t
= &alarm_timers
[i
];
1793 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1794 try_to_rearm_timer
, NULL
, t
);
1809 static void quit_timers(void)
1811 alarm_timer
->stop(alarm_timer
);
1815 /***********************************************************/
1816 /* host time/date access */
1817 void qemu_get_timedate(struct tm
*tm
, int offset
)
1824 if (rtc_date_offset
== -1) {
1828 ret
= localtime(&ti
);
1830 ti
-= rtc_date_offset
;
1834 memcpy(tm
, ret
, sizeof(struct tm
));
1837 int qemu_timedate_diff(struct tm
*tm
)
1841 if (rtc_date_offset
== -1)
1843 seconds
= mktimegm(tm
);
1845 seconds
= mktime(tm
);
1847 seconds
= mktimegm(tm
) + rtc_date_offset
;
1849 return seconds
- time(NULL
);
1853 static void socket_cleanup(void)
1858 static int socket_init(void)
1863 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1865 err
= WSAGetLastError();
1866 fprintf(stderr
, "WSAStartup: %d\n", err
);
1869 atexit(socket_cleanup
);
1874 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1879 while (*p
!= '\0' && *p
!= '=') {
1880 if (q
&& (q
- buf
) < buf_size
- 1)
1890 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1895 while (*p
!= '\0') {
1897 if (*(p
+ 1) != ',')
1901 if (q
&& (q
- buf
) < buf_size
- 1)
1911 int get_param_value(char *buf
, int buf_size
,
1912 const char *tag
, const char *str
)
1919 p
= get_opt_name(option
, sizeof(option
), p
);
1923 if (!strcmp(tag
, option
)) {
1924 (void)get_opt_value(buf
, buf_size
, p
);
1927 p
= get_opt_value(NULL
, 0, p
);
1936 int check_params(char *buf
, int buf_size
,
1937 const char * const *params
, const char *str
)
1944 p
= get_opt_name(buf
, buf_size
, p
);
1948 for(i
= 0; params
[i
] != NULL
; i
++)
1949 if (!strcmp(params
[i
], buf
))
1951 if (params
[i
] == NULL
)
1953 p
= get_opt_value(NULL
, 0, p
);
1961 /***********************************************************/
1962 /* Bluetooth support */
1965 static struct HCIInfo
*hci_table
[MAX_NICS
];
1967 static struct bt_vlan_s
{
1968 struct bt_scatternet_s net
;
1970 struct bt_vlan_s
*next
;
1973 /* find or alloc a new bluetooth "VLAN" */
1974 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1976 struct bt_vlan_s
**pvlan
, *vlan
;
1977 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1981 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1983 pvlan
= &first_bt_vlan
;
1984 while (*pvlan
!= NULL
)
1985 pvlan
= &(*pvlan
)->next
;
1990 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1994 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1999 static struct HCIInfo null_hci
= {
2000 .cmd_send
= null_hci_send
,
2001 .sco_send
= null_hci_send
,
2002 .acl_send
= null_hci_send
,
2003 .bdaddr_set
= null_hci_addr_set
,
2006 struct HCIInfo
*qemu_next_hci(void)
2008 if (cur_hci
== nb_hcis
)
2011 return hci_table
[cur_hci
++];
2014 static struct HCIInfo
*hci_init(const char *str
)
2017 struct bt_scatternet_s
*vlan
= 0;
2019 if (!strcmp(str
, "null"))
2022 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2024 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2025 else if (!strncmp(str
, "hci", 3)) {
2028 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2029 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2034 vlan
= qemu_find_bt_vlan(0);
2036 return bt_new_hci(vlan
);
2039 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2044 static int bt_hci_parse(const char *str
)
2046 struct HCIInfo
*hci
;
2049 if (nb_hcis
>= MAX_NICS
) {
2050 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2054 hci
= hci_init(str
);
2063 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2064 hci
->bdaddr_set(hci
, bdaddr
.b
);
2066 hci_table
[nb_hcis
++] = hci
;
2071 static void bt_vhci_add(int vlan_id
)
2073 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2076 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2077 "an empty scatternet %i\n", vlan_id
);
2079 bt_vhci_init(bt_new_hci(vlan
));
2082 static struct bt_device_s
*bt_device_add(const char *opt
)
2084 struct bt_scatternet_s
*vlan
;
2086 char *endp
= strstr(opt
, ",vlan=");
2087 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2090 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2093 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2095 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2100 vlan
= qemu_find_bt_vlan(vlan_id
);
2103 fprintf(stderr
, "qemu: warning: adding a slave device to "
2104 "an empty scatternet %i\n", vlan_id
);
2106 if (!strcmp(devname
, "keyboard"))
2107 return bt_keyboard_init(vlan
);
2109 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2113 static int bt_parse(const char *opt
)
2115 const char *endp
, *p
;
2118 if (strstart(opt
, "hci", &endp
)) {
2119 if (!*endp
|| *endp
== ',') {
2121 if (!strstart(endp
, ",vlan=", 0))
2124 return bt_hci_parse(opt
);
2126 } else if (strstart(opt
, "vhci", &endp
)) {
2127 if (!*endp
|| *endp
== ',') {
2129 if (strstart(endp
, ",vlan=", &p
)) {
2130 vlan
= strtol(p
, (char **) &endp
, 0);
2132 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2136 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2145 } else if (strstart(opt
, "device:", &endp
))
2146 return !bt_device_add(endp
);
2148 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2152 /***********************************************************/
2153 /* QEMU Block devices */
2155 #define HD_ALIAS "index=%d,media=disk"
2157 #define CDROM_ALIAS "index=1,media=cdrom"
2159 #define CDROM_ALIAS "index=2,media=cdrom"
2161 #define FD_ALIAS "index=%d,if=floppy"
2162 #define PFLASH_ALIAS "if=pflash"
2163 #define MTD_ALIAS "if=mtd"
2164 #define SD_ALIAS "index=0,if=sd"
2166 static int drive_opt_get_free_idx(void)
2170 for (index
= 0; index
< MAX_DRIVES
; index
++)
2171 if (!drives_opt
[index
].used
) {
2172 drives_opt
[index
].used
= 1;
2179 static int drive_get_free_idx(void)
2183 for (index
= 0; index
< MAX_DRIVES
; index
++)
2184 if (!drives_table
[index
].used
) {
2185 drives_table
[index
].used
= 1;
2192 int drive_add(const char *file
, const char *fmt
, ...)
2195 int index
= drive_opt_get_free_idx();
2197 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2198 fprintf(stderr
, "qemu: too many drives\n");
2202 drives_opt
[index
].file
= file
;
2204 vsnprintf(drives_opt
[index
].opt
,
2205 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2212 void drive_remove(int index
)
2214 drives_opt
[index
].used
= 0;
2218 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2222 /* seek interface, bus and unit */
2224 for (index
= 0; index
< MAX_DRIVES
; index
++)
2225 if (drives_table
[index
].type
== type
&&
2226 drives_table
[index
].bus
== bus
&&
2227 drives_table
[index
].unit
== unit
&&
2228 drives_table
[index
].used
)
2234 int drive_get_max_bus(BlockInterfaceType type
)
2240 for (index
= 0; index
< nb_drives
; index
++) {
2241 if(drives_table
[index
].type
== type
&&
2242 drives_table
[index
].bus
> max_bus
)
2243 max_bus
= drives_table
[index
].bus
;
2248 const char *drive_get_serial(BlockDriverState
*bdrv
)
2252 for (index
= 0; index
< nb_drives
; index
++)
2253 if (drives_table
[index
].bdrv
== bdrv
)
2254 return drives_table
[index
].serial
;
2259 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2263 for (index
= 0; index
< nb_drives
; index
++)
2264 if (drives_table
[index
].bdrv
== bdrv
)
2265 return drives_table
[index
].onerror
;
2267 return BLOCK_ERR_REPORT
;
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
)
2296 const char *mediastr
= "";
2297 BlockInterfaceType type
;
2298 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2299 int bus_id
, unit_id
;
2300 int cyls
, heads
, secs
, translation
;
2301 BlockDriverState
*bdrv
;
2302 BlockDriver
*drv
= NULL
;
2306 int bdrv_flags
, onerror
;
2307 int drives_table_idx
;
2308 char *str
= arg
->opt
;
2309 static const char * const params
[] = { "bus", "unit", "if", "index",
2310 "cyls", "heads", "secs", "trans",
2311 "media", "snapshot", "file",
2312 "cache", "format", "serial", "werror",
2315 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2316 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2322 cyls
= heads
= secs
= 0;
2325 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2329 if (machine
->use_scsi
) {
2331 max_devs
= MAX_SCSI_DEVS
;
2332 pstrcpy(devname
, sizeof(devname
), "scsi");
2335 max_devs
= MAX_IDE_DEVS
;
2336 pstrcpy(devname
, sizeof(devname
), "ide");
2340 /* extract parameters */
2342 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2343 bus_id
= strtol(buf
, NULL
, 0);
2345 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2350 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2351 unit_id
= strtol(buf
, NULL
, 0);
2353 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2358 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2359 pstrcpy(devname
, sizeof(devname
), buf
);
2360 if (!strcmp(buf
, "ide")) {
2362 max_devs
= MAX_IDE_DEVS
;
2363 } else if (!strcmp(buf
, "scsi")) {
2365 max_devs
= MAX_SCSI_DEVS
;
2366 } else if (!strcmp(buf
, "floppy")) {
2369 } else if (!strcmp(buf
, "pflash")) {
2372 } else if (!strcmp(buf
, "mtd")) {
2375 } else if (!strcmp(buf
, "sd")) {
2378 } else if (!strcmp(buf
, "virtio")) {
2382 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2387 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2388 index
= strtol(buf
, NULL
, 0);
2390 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2395 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2396 cyls
= strtol(buf
, NULL
, 0);
2399 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2400 heads
= strtol(buf
, NULL
, 0);
2403 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2404 secs
= strtol(buf
, NULL
, 0);
2407 if (cyls
|| heads
|| secs
) {
2408 if (cyls
< 1 || cyls
> 16383) {
2409 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2412 if (heads
< 1 || heads
> 16) {
2413 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2416 if (secs
< 1 || secs
> 63) {
2417 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2422 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2425 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2429 if (!strcmp(buf
, "none"))
2430 translation
= BIOS_ATA_TRANSLATION_NONE
;
2431 else if (!strcmp(buf
, "lba"))
2432 translation
= BIOS_ATA_TRANSLATION_LBA
;
2433 else if (!strcmp(buf
, "auto"))
2434 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2436 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2441 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2442 if (!strcmp(buf
, "disk")) {
2444 } else if (!strcmp(buf
, "cdrom")) {
2445 if (cyls
|| secs
|| heads
) {
2447 "qemu: '%s' invalid physical CHS format\n", str
);
2450 media
= MEDIA_CDROM
;
2452 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2457 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2458 if (!strcmp(buf
, "on"))
2460 else if (!strcmp(buf
, "off"))
2463 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2468 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2469 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2471 else if (!strcmp(buf
, "writethrough"))
2473 else if (!strcmp(buf
, "writeback"))
2476 fprintf(stderr
, "qemu: invalid cache option\n");
2481 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2482 if (strcmp(buf
, "?") == 0) {
2483 fprintf(stderr
, "qemu: Supported formats:");
2484 bdrv_iterate_format(bdrv_format_print
, NULL
);
2485 fprintf(stderr
, "\n");
2488 drv
= bdrv_find_format(buf
);
2490 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2495 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2496 if (!strcmp(buf
, "on")) {
2497 if (extboot_drive
!= -1) {
2498 fprintf(stderr
, "qemu: two bootable drives specified\n");
2501 extboot_drive
= nb_drives
;
2502 } else if (strcmp(buf
, "off")) {
2503 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2508 if (arg
->file
== NULL
)
2509 get_param_value(file
, sizeof(file
), "file", str
);
2511 pstrcpy(file
, sizeof(file
), arg
->file
);
2513 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2514 memset(serial
, 0, sizeof(serial
));
2516 onerror
= BLOCK_ERR_REPORT
;
2517 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2518 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2519 fprintf(stderr
, "werror is no supported by this format\n");
2522 if (!strcmp(buf
, "ignore"))
2523 onerror
= BLOCK_ERR_IGNORE
;
2524 else if (!strcmp(buf
, "enospc"))
2525 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2526 else if (!strcmp(buf
, "stop"))
2527 onerror
= BLOCK_ERR_STOP_ANY
;
2528 else if (!strcmp(buf
, "report"))
2529 onerror
= BLOCK_ERR_REPORT
;
2531 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2536 /* compute bus and unit according index */
2539 if (bus_id
!= 0 || unit_id
!= -1) {
2541 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2549 unit_id
= index
% max_devs
;
2550 bus_id
= index
/ max_devs
;
2554 /* if user doesn't specify a unit_id,
2555 * try to find the first free
2558 if (unit_id
== -1) {
2560 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2562 if (max_devs
&& unit_id
>= max_devs
) {
2563 unit_id
-= max_devs
;
2571 if (max_devs
&& unit_id
>= max_devs
) {
2572 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2573 str
, unit_id
, max_devs
- 1);
2578 * ignore multiple definitions
2581 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2586 if (type
== IF_IDE
|| type
== IF_SCSI
)
2587 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2589 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2590 devname
, bus_id
, mediastr
, unit_id
);
2592 snprintf(buf
, sizeof(buf
), "%s%s%i",
2593 devname
, mediastr
, unit_id
);
2594 bdrv
= bdrv_new(buf
);
2595 drives_table_idx
= drive_get_free_idx();
2596 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2597 drives_table
[drives_table_idx
].type
= type
;
2598 drives_table
[drives_table_idx
].bus
= bus_id
;
2599 drives_table
[drives_table_idx
].unit
= unit_id
;
2600 drives_table
[drives_table_idx
].onerror
= onerror
;
2601 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2602 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2611 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2612 bdrv_set_translation_hint(bdrv
, translation
);
2616 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2621 /* FIXME: This isn't really a floppy, but it's a reasonable
2624 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2635 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2636 cache
= 2; /* always use write-back with snapshot */
2638 if (cache
== 0) /* no caching */
2639 bdrv_flags
|= BDRV_O_NOCACHE
;
2640 else if (cache
== 2) /* write-back */
2641 bdrv_flags
|= BDRV_O_CACHE_WB
;
2642 else if (cache
== 3) /* not specified */
2643 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2644 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2645 fprintf(stderr
, "qemu: could not open disk image %s\n",
2649 return drives_table_idx
;
2652 /***********************************************************/
2655 static USBPort
*used_usb_ports
;
2656 static USBPort
*free_usb_ports
;
2658 /* ??? Maybe change this to register a hub to keep track of the topology. */
2659 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2660 usb_attachfn attach
)
2662 port
->opaque
= opaque
;
2663 port
->index
= index
;
2664 port
->attach
= attach
;
2665 port
->next
= free_usb_ports
;
2666 free_usb_ports
= port
;
2669 int usb_device_add_dev(USBDevice
*dev
)
2673 /* Find a USB port to add the device to. */
2674 port
= free_usb_ports
;
2678 /* Create a new hub and chain it on. */
2679 free_usb_ports
= NULL
;
2680 port
->next
= used_usb_ports
;
2681 used_usb_ports
= port
;
2683 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2684 usb_attach(port
, hub
);
2685 port
= free_usb_ports
;
2688 free_usb_ports
= port
->next
;
2689 port
->next
= used_usb_ports
;
2690 used_usb_ports
= port
;
2691 usb_attach(port
, dev
);
2695 static int usb_device_add(const char *devname
)
2700 if (!free_usb_ports
)
2703 if (strstart(devname
, "host:", &p
)) {
2704 dev
= usb_host_device_open(p
);
2705 } else if (!strcmp(devname
, "mouse")) {
2706 dev
= usb_mouse_init();
2707 } else if (!strcmp(devname
, "tablet")) {
2708 dev
= usb_tablet_init();
2709 } else if (!strcmp(devname
, "keyboard")) {
2710 dev
= usb_keyboard_init();
2711 } else if (strstart(devname
, "disk:", &p
)) {
2712 dev
= usb_msd_init(p
);
2713 } else if (!strcmp(devname
, "wacom-tablet")) {
2714 dev
= usb_wacom_init();
2715 } else if (strstart(devname
, "serial:", &p
)) {
2716 dev
= usb_serial_init(p
);
2717 #ifdef CONFIG_BRLAPI
2718 } else if (!strcmp(devname
, "braille")) {
2719 dev
= usb_baum_init();
2721 } else if (strstart(devname
, "net:", &p
)) {
2724 if (net_client_init("nic", p
) < 0)
2726 nd_table
[nic
].model
= "usb";
2727 dev
= usb_net_init(&nd_table
[nic
]);
2728 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2729 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2730 bt_new_hci(qemu_find_bt_vlan(0)));
2737 return usb_device_add_dev(dev
);
2740 int usb_device_del_addr(int bus_num
, int addr
)
2746 if (!used_usb_ports
)
2752 lastp
= &used_usb_ports
;
2753 port
= used_usb_ports
;
2754 while (port
&& port
->dev
->addr
!= addr
) {
2755 lastp
= &port
->next
;
2763 *lastp
= port
->next
;
2764 usb_attach(port
, NULL
);
2765 dev
->handle_destroy(dev
);
2766 port
->next
= free_usb_ports
;
2767 free_usb_ports
= port
;
2771 static int usb_device_del(const char *devname
)
2776 if (strstart(devname
, "host:", &p
))
2777 return usb_host_device_close(p
);
2779 if (!used_usb_ports
)
2782 p
= strchr(devname
, '.');
2785 bus_num
= strtoul(devname
, NULL
, 0);
2786 addr
= strtoul(p
+ 1, NULL
, 0);
2788 return usb_device_del_addr(bus_num
, addr
);
2791 void do_usb_add(const char *devname
)
2793 usb_device_add(devname
);
2796 void do_usb_del(const char *devname
)
2798 usb_device_del(devname
);
2805 const char *speed_str
;
2808 term_printf("USB support not enabled\n");
2812 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2816 switch(dev
->speed
) {
2820 case USB_SPEED_FULL
:
2823 case USB_SPEED_HIGH
:
2830 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2831 0, dev
->addr
, speed_str
, dev
->devname
);
2835 /***********************************************************/
2836 /* PCMCIA/Cardbus */
2838 static struct pcmcia_socket_entry_s
{
2839 struct pcmcia_socket_s
*socket
;
2840 struct pcmcia_socket_entry_s
*next
;
2841 } *pcmcia_sockets
= 0;
2843 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2845 struct pcmcia_socket_entry_s
*entry
;
2847 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2848 entry
->socket
= socket
;
2849 entry
->next
= pcmcia_sockets
;
2850 pcmcia_sockets
= entry
;
2853 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2855 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2857 ptr
= &pcmcia_sockets
;
2858 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2859 if (entry
->socket
== socket
) {
2865 void pcmcia_info(void)
2867 struct pcmcia_socket_entry_s
*iter
;
2868 if (!pcmcia_sockets
)
2869 term_printf("No PCMCIA sockets\n");
2871 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2872 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2873 iter
->socket
->attached
? iter
->socket
->card_string
:
2877 /***********************************************************/
2878 /* register display */
2880 void register_displaystate(DisplayState
*ds
)
2890 DisplayState
*get_displaystate(void)
2892 return display_state
;
2897 static void dumb_display_init(void)
2899 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2901 fprintf(stderr
, "dumb_display_init: DisplayState allocation failed\n");
2904 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2905 register_displaystate(ds
);
2908 /***********************************************************/
2911 #define MAX_IO_HANDLERS 64
2913 typedef struct IOHandlerRecord
{
2915 IOCanRWHandler
*fd_read_poll
;
2917 IOHandler
*fd_write
;
2920 /* temporary data */
2922 struct IOHandlerRecord
*next
;
2925 static IOHandlerRecord
*first_io_handler
;
2927 /* XXX: fd_read_poll should be suppressed, but an API change is
2928 necessary in the character devices to suppress fd_can_read(). */
2929 int qemu_set_fd_handler2(int fd
,
2930 IOCanRWHandler
*fd_read_poll
,
2932 IOHandler
*fd_write
,
2935 IOHandlerRecord
**pioh
, *ioh
;
2937 if (!fd_read
&& !fd_write
) {
2938 pioh
= &first_io_handler
;
2943 if (ioh
->fd
== fd
) {
2950 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2954 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2957 ioh
->next
= first_io_handler
;
2958 first_io_handler
= ioh
;
2961 ioh
->fd_read_poll
= fd_read_poll
;
2962 ioh
->fd_read
= fd_read
;
2963 ioh
->fd_write
= fd_write
;
2964 ioh
->opaque
= opaque
;
2971 int qemu_set_fd_handler(int fd
,
2973 IOHandler
*fd_write
,
2976 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2980 /***********************************************************/
2981 /* Polling handling */
2983 typedef struct PollingEntry
{
2986 struct PollingEntry
*next
;
2989 static PollingEntry
*first_polling_entry
;
2991 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2993 PollingEntry
**ppe
, *pe
;
2994 pe
= qemu_mallocz(sizeof(PollingEntry
));
2998 pe
->opaque
= opaque
;
2999 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3004 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3006 PollingEntry
**ppe
, *pe
;
3007 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3009 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3017 /***********************************************************/
3018 /* Wait objects support */
3019 typedef struct WaitObjects
{
3021 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3022 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3023 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3026 static WaitObjects wait_objects
= {0};
3028 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3030 WaitObjects
*w
= &wait_objects
;
3032 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3034 w
->events
[w
->num
] = handle
;
3035 w
->func
[w
->num
] = func
;
3036 w
->opaque
[w
->num
] = opaque
;
3041 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3044 WaitObjects
*w
= &wait_objects
;
3047 for (i
= 0; i
< w
->num
; i
++) {
3048 if (w
->events
[i
] == handle
)
3051 w
->events
[i
] = w
->events
[i
+ 1];
3052 w
->func
[i
] = w
->func
[i
+ 1];
3053 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3061 /***********************************************************/
3062 /* ram save/restore */
3064 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3068 v
= qemu_get_byte(f
);
3071 if (qemu_get_buffer(f
, buf
, len
) != len
)
3075 v
= qemu_get_byte(f
);
3076 memset(buf
, v
, len
);
3082 if (qemu_file_has_error(f
))
3088 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3093 if (qemu_get_be32(f
) != phys_ram_size
)
3095 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3096 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3098 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3105 #define BDRV_HASH_BLOCK_SIZE 1024
3106 #define IOBUF_SIZE 4096
3107 #define RAM_CBLOCK_MAGIC 0xfabe
3109 typedef struct RamDecompressState
{
3112 uint8_t buf
[IOBUF_SIZE
];
3113 } RamDecompressState
;
3115 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3118 memset(s
, 0, sizeof(*s
));
3120 ret
= inflateInit(&s
->zstream
);
3126 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3130 s
->zstream
.avail_out
= len
;
3131 s
->zstream
.next_out
= buf
;
3132 while (s
->zstream
.avail_out
> 0) {
3133 if (s
->zstream
.avail_in
== 0) {
3134 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3136 clen
= qemu_get_be16(s
->f
);
3137 if (clen
> IOBUF_SIZE
)
3139 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3140 s
->zstream
.avail_in
= clen
;
3141 s
->zstream
.next_in
= s
->buf
;
3143 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3144 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3151 static void ram_decompress_close(RamDecompressState
*s
)
3153 inflateEnd(&s
->zstream
);
3156 #define RAM_SAVE_FLAG_FULL 0x01
3157 #define RAM_SAVE_FLAG_COMPRESS 0x02
3158 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3159 #define RAM_SAVE_FLAG_PAGE 0x08
3160 #define RAM_SAVE_FLAG_EOS 0x10
3162 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3164 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3165 uint32_t *array
= (uint32_t *)page
;
3168 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3169 if (array
[i
] != val
)
3176 static int ram_save_block(QEMUFile
*f
)
3178 static ram_addr_t current_addr
= 0;
3179 ram_addr_t saved_addr
= current_addr
;
3180 ram_addr_t addr
= 0;
3183 while (addr
< phys_ram_size
) {
3184 if (kvm_enabled() && current_addr
== 0)
3185 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3186 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3189 cpu_physical_memory_reset_dirty(current_addr
,
3190 current_addr
+ TARGET_PAGE_SIZE
,
3191 MIGRATION_DIRTY_FLAG
);
3193 ch
= *(phys_ram_base
+ current_addr
);
3195 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3196 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3197 qemu_put_byte(f
, ch
);
3199 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3200 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3206 addr
+= TARGET_PAGE_SIZE
;
3207 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3213 static ram_addr_t ram_save_threshold
= 10;
3215 static ram_addr_t
ram_save_remaining(void)
3218 ram_addr_t count
= 0;
3220 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3221 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3228 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3233 /* Make sure all dirty bits are set */
3234 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3235 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3236 cpu_physical_memory_set_dirty(addr
);
3239 /* Enable dirty memory tracking */
3240 cpu_physical_memory_set_dirty_tracking(1);
3242 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3245 while (!qemu_file_rate_limit(f
)) {
3248 ret
= ram_save_block(f
);
3249 if (ret
== 0) /* no more blocks */
3253 /* try transferring iterative blocks of memory */
3256 cpu_physical_memory_set_dirty_tracking(0);
3258 /* flush all remaining blocks regardless of rate limiting */
3259 while (ram_save_block(f
) != 0);
3262 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3264 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3267 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3269 RamDecompressState s1
, *s
= &s1
;
3273 if (ram_decompress_open(s
, f
) < 0)
3275 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3276 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3278 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3279 fprintf(stderr
, "Error while reading ram block header\n");
3283 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3284 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3289 printf("Error block header\n");
3293 ram_decompress_close(s
);
3298 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3303 if (version_id
== 1)
3304 return ram_load_v1(f
, opaque
);
3306 if (version_id
== 2) {
3307 if (qemu_get_be32(f
) != phys_ram_size
)
3309 return ram_load_dead(f
, opaque
);
3312 if (version_id
!= 3)
3316 addr
= qemu_get_be64(f
);
3318 flags
= addr
& ~TARGET_PAGE_MASK
;
3319 addr
&= TARGET_PAGE_MASK
;
3321 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3322 if (addr
!= phys_ram_size
)
3326 if (flags
& RAM_SAVE_FLAG_FULL
) {
3327 if (ram_load_dead(f
, opaque
) < 0)
3331 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3332 uint8_t ch
= qemu_get_byte(f
);
3333 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3334 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3335 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3336 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3341 /***********************************************************/
3342 /* bottom halves (can be seen as timers which expire ASAP) */
3353 static QEMUBH
*first_bh
= NULL
;
3355 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3358 bh
= qemu_mallocz(sizeof(QEMUBH
));
3362 bh
->opaque
= opaque
;
3363 bh
->next
= first_bh
;
3368 int qemu_bh_poll(void)
3374 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3375 if (!bh
->deleted
&& bh
->scheduled
) {
3384 /* remove deleted bhs */
3398 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3406 void qemu_bh_schedule(QEMUBH
*bh
)
3408 CPUState
*env
= cpu_single_env
;
3413 /* stop the currently executing CPU to execute the BH ASAP */
3415 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3420 void qemu_bh_cancel(QEMUBH
*bh
)
3425 void qemu_bh_delete(QEMUBH
*bh
)
3431 static void qemu_bh_update_timeout(int *timeout
)
3435 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3436 if (!bh
->deleted
&& bh
->scheduled
) {
3438 /* idle bottom halves will be polled at least
3440 *timeout
= MIN(10, *timeout
);
3442 /* non-idle bottom halves will be executed
3451 /***********************************************************/
3452 /* machine registration */
3454 static QEMUMachine
*first_machine
= NULL
;
3455 QEMUMachine
*current_machine
= NULL
;
3457 int qemu_register_machine(QEMUMachine
*m
)
3460 pm
= &first_machine
;
3468 static QEMUMachine
*find_machine(const char *name
)
3472 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3473 if (!strcmp(m
->name
, name
))
3479 /***********************************************************/
3480 /* main execution loop */
3482 static void gui_update(void *opaque
)
3484 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3485 DisplayState
*ds
= opaque
;
3486 DisplayChangeListener
*dcl
= ds
->listeners
;
3490 while (dcl
!= NULL
) {
3491 if (dcl
->gui_timer_interval
&&
3492 dcl
->gui_timer_interval
< interval
)
3493 interval
= dcl
->gui_timer_interval
;
3496 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3499 static void nographic_update(void *opaque
)
3501 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3503 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3506 struct vm_change_state_entry
{
3507 VMChangeStateHandler
*cb
;
3509 LIST_ENTRY (vm_change_state_entry
) entries
;
3512 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3514 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3517 VMChangeStateEntry
*e
;
3519 e
= qemu_mallocz(sizeof (*e
));
3525 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3529 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3531 LIST_REMOVE (e
, entries
);
3535 static void vm_state_notify(int running
, int reason
)
3537 VMChangeStateEntry
*e
;
3539 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3540 e
->cb(e
->opaque
, running
, reason
);
3549 vm_state_notify(1, 0);
3550 qemu_rearm_alarm_timer(alarm_timer
);
3554 void vm_stop(int reason
)
3557 cpu_disable_ticks();
3559 vm_state_notify(0, reason
);
3563 /* reset/shutdown handler */
3565 typedef struct QEMUResetEntry
{
3566 QEMUResetHandler
*func
;
3568 struct QEMUResetEntry
*next
;
3571 static QEMUResetEntry
*first_reset_entry
;
3572 static int reset_requested
;
3573 static int shutdown_requested
;
3574 static int powerdown_requested
;
3576 int qemu_shutdown_requested(void)
3578 int r
= shutdown_requested
;
3579 shutdown_requested
= 0;
3583 int qemu_reset_requested(void)
3585 int r
= reset_requested
;
3586 reset_requested
= 0;
3590 int qemu_powerdown_requested(void)
3592 int r
= powerdown_requested
;
3593 powerdown_requested
= 0;
3597 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3599 QEMUResetEntry
**pre
, *re
;
3601 pre
= &first_reset_entry
;
3602 while (*pre
!= NULL
)
3603 pre
= &(*pre
)->next
;
3604 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3606 re
->opaque
= opaque
;
3611 void qemu_system_reset(void)
3615 /* reset all devices */
3616 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3617 re
->func(re
->opaque
);
3621 void qemu_system_reset_request(void)
3624 shutdown_requested
= 1;
3626 reset_requested
= 1;
3629 if (cpu_single_env
) {
3630 qemu_kvm_cpu_stop(cpu_single_env
);
3631 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3636 void qemu_system_shutdown_request(void)
3638 shutdown_requested
= 1;
3640 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3643 void qemu_system_powerdown_request(void)
3645 powerdown_requested
= 1;
3647 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3650 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3655 /* KVM holds a mutex while QEMU code is running, we need hooks to
3656 release the mutex whenever QEMU code sleeps. */
3660 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3668 static void host_main_loop_wait(int *timeout
)
3674 /* XXX: need to suppress polling by better using win32 events */
3676 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3677 ret
|= pe
->func(pe
->opaque
);
3681 WaitObjects
*w
= &wait_objects
;
3683 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3684 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3685 if (w
->func
[ret
- WAIT_OBJECT_0
])
3686 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3688 /* Check for additional signaled events */
3689 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3691 /* Check if event is signaled */
3692 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3693 if(ret2
== WAIT_OBJECT_0
) {
3695 w
->func
[i
](w
->opaque
[i
]);
3696 } else if (ret2
== WAIT_TIMEOUT
) {
3698 err
= GetLastError();
3699 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3702 } else if (ret
== WAIT_TIMEOUT
) {
3704 err
= GetLastError();
3705 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3712 static void host_main_loop_wait(int *timeout
)
3717 void main_loop_wait(int timeout
)
3719 IOHandlerRecord
*ioh
;
3720 fd_set rfds
, wfds
, xfds
;
3724 qemu_bh_update_timeout(&timeout
);
3726 host_main_loop_wait(&timeout
);
3728 /* poll any events */
3729 /* XXX: separate device handlers from system ones */
3734 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3738 (!ioh
->fd_read_poll
||
3739 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3740 FD_SET(ioh
->fd
, &rfds
);
3744 if (ioh
->fd_write
) {
3745 FD_SET(ioh
->fd
, &wfds
);
3751 tv
.tv_sec
= timeout
/ 1000;
3752 tv
.tv_usec
= (timeout
% 1000) * 1000;
3754 #if defined(CONFIG_SLIRP)
3755 if (slirp_is_inited()) {
3756 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3759 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3761 IOHandlerRecord
**pioh
;
3763 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3764 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3765 ioh
->fd_read(ioh
->opaque
);
3766 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3767 FD_CLR(ioh
->fd
, &rfds
);
3769 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3770 ioh
->fd_write(ioh
->opaque
);
3774 /* remove deleted IO handlers */
3775 pioh
= &first_io_handler
;
3785 #if defined(CONFIG_SLIRP)
3786 if (slirp_is_inited()) {
3792 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3796 /* vm time timers */
3797 if (vm_running
&& (!cur_cpu
3798 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3799 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3800 qemu_get_clock(vm_clock
));
3802 /* real time timers */
3803 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3804 qemu_get_clock(rt_clock
));
3806 /* Check bottom-halves last in case any of the earlier events triggered
3812 static int main_loop(void)
3815 #ifdef CONFIG_PROFILER
3821 if (kvm_enabled()) {
3823 cpu_disable_ticks();
3827 cur_cpu
= first_cpu
;
3828 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3835 #ifdef CONFIG_PROFILER
3836 ti
= profile_getclock();
3841 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3842 env
->icount_decr
.u16
.low
= 0;
3843 env
->icount_extra
= 0;
3844 count
= qemu_next_deadline();
3845 count
= (count
+ (1 << icount_time_shift
) - 1)
3846 >> icount_time_shift
;
3847 qemu_icount
+= count
;
3848 decr
= (count
> 0xffff) ? 0xffff : count
;
3850 env
->icount_decr
.u16
.low
= decr
;
3851 env
->icount_extra
= count
;
3853 ret
= cpu_exec(env
);
3854 #ifdef CONFIG_PROFILER
3855 qemu_time
+= profile_getclock() - ti
;
3858 /* Fold pending instructions back into the
3859 instruction counter, and clear the interrupt flag. */
3860 qemu_icount
-= (env
->icount_decr
.u16
.low
3861 + env
->icount_extra
);
3862 env
->icount_decr
.u32
= 0;
3863 env
->icount_extra
= 0;
3865 next_cpu
= env
->next_cpu
?: first_cpu
;
3866 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3867 ret
= EXCP_INTERRUPT
;
3871 if (ret
== EXCP_HLT
) {
3872 /* Give the next CPU a chance to run. */
3876 if (ret
!= EXCP_HALTED
)
3878 /* all CPUs are halted ? */
3884 if (shutdown_requested
) {
3885 ret
= EXCP_INTERRUPT
;
3893 if (reset_requested
) {
3894 reset_requested
= 0;
3895 qemu_system_reset();
3897 kvm_load_registers(env
);
3898 ret
= EXCP_INTERRUPT
;
3900 if (powerdown_requested
) {
3901 powerdown_requested
= 0;
3902 qemu_system_powerdown();
3903 ret
= EXCP_INTERRUPT
;
3905 #ifdef CONFIG_GDBSTUB
3906 if (unlikely(ret
== EXCP_DEBUG
)) {
3907 gdb_set_stop_cpu(cur_cpu
);
3908 vm_stop(EXCP_DEBUG
);
3911 /* If all cpus are halted then wait until the next IRQ */
3912 /* XXX: use timeout computed from timers */
3913 if (ret
== EXCP_HALTED
) {
3917 /* Advance virtual time to the next event. */
3918 if (use_icount
== 1) {
3919 /* When not using an adaptive execution frequency
3920 we tend to get badly out of sync with real time,
3921 so just delay for a reasonable amount of time. */
3924 delta
= cpu_get_icount() - cpu_get_clock();
3927 /* If virtual time is ahead of real time then just
3929 timeout
= (delta
/ 1000000) + 1;
3931 /* Wait for either IO to occur or the next
3933 add
= qemu_next_deadline();
3934 /* We advance the timer before checking for IO.
3935 Limit the amount we advance so that early IO
3936 activity won't get the guest too far ahead. */
3940 add
= (add
+ (1 << icount_time_shift
) - 1)
3941 >> icount_time_shift
;
3943 timeout
= delta
/ 1000000;
3954 if (shutdown_requested
) {
3955 ret
= EXCP_INTERRUPT
;
3960 #ifdef CONFIG_PROFILER
3961 ti
= profile_getclock();
3963 main_loop_wait(timeout
);
3964 #ifdef CONFIG_PROFILER
3965 dev_time
+= profile_getclock() - ti
;
3968 cpu_disable_ticks();
3972 static void help(int exitcode
)
3974 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3975 and qemu-doc.texi */
3976 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3977 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3978 "usage: %s [options] [disk_image]\n"
3980 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3982 "Standard options:\n"
3983 "-h or -help display this help and exit\n"
3984 "-M machine select emulated machine (-M ? for list)\n"
3985 "-cpu cpu select CPU (-cpu ? for list)\n"
3986 "-smp n set the number of CPUs to 'n' [default=1]\n"
3987 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3988 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3989 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3990 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3991 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3992 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3993 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3995 " use 'file' as a drive image\n"
3996 "-mtdblock file use 'file' as on-board Flash memory image\n"
3997 "-sd file use 'file' as SecureDigital card image\n"
3998 "-pflash file use 'file' as a parallel flash image\n"
3999 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4000 "-snapshot write to temporary files instead of disk image files\n"
4001 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4003 "-k language use keyboard layout (for example \"fr\" for French)\n"
4006 "-audio-help print list of audio drivers and their options\n"
4007 "-soundhw c1,... enable audio support\n"
4008 " and only specified sound cards (comma separated list)\n"
4009 " use -soundhw ? to get the list of supported cards\n"
4010 " use -soundhw all to enable all of them\n"
4012 "-usb enable the USB driver (will be the default soon)\n"
4013 "-usbdevice name add the host or guest USB device 'name'\n"
4014 "-name string set the name of the guest\n"
4015 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
4016 " specify machine UUID\n"
4018 "Display options:\n"
4019 "-nographic disable graphical output and redirect serial I/Os to console\n"
4020 #ifdef CONFIG_CURSES
4021 "-curses use a curses/ncurses interface instead of SDL\n"
4024 "-no-frame open SDL window without a frame and window decorations\n"
4025 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4026 "-no-quit disable SDL window close capability\n"
4029 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4030 "-vga [std|cirrus|vmware|none]\n"
4031 " select video card type\n"
4032 "-full-screen start in full screen\n"
4033 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4034 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4036 "-vnc display start a VNC server on display\n"
4038 "-nvram file use 'file' to save or load nvram image\n"
4040 "-name string set the name of the guest\n"
4041 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4043 "Network options:\n"
4044 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4045 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4047 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4048 " connect the user mode network stack to VLAN 'n' and send\n"
4049 " hostname 'host' to DHCP clients\n"
4052 "-net tap[,vlan=n][,name=str],ifname=name\n"
4053 " connect the host TAP network interface to VLAN 'n'\n"
4055 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4056 " connect the host TAP network interface to VLAN 'n' and use the\n"
4057 " network scripts 'file' (default=%s)\n"
4058 " and 'dfile' (default=%s);\n"
4059 " use '[down]script=no' to disable script execution;\n"
4060 " use 'fd=h' to connect to an already opened TAP interface\n"
4062 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4063 " connect the vlan 'n' to another VLAN using a socket connection\n"
4064 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4065 " connect the vlan 'n' to multicast maddr and port\n"
4067 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4068 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4069 " on host and listening for incoming connections on 'socketpath'.\n"
4070 " Use group 'groupname' and mode 'octalmode' to change default\n"
4071 " ownership and permissions for communication port.\n"
4073 "-net none use it alone to have zero network devices; if no -net option\n"
4074 " is provided, the default is '-net nic -net user'\n"
4076 "-tftp dir allow tftp access to files in dir [-net user]\n"
4077 "-bootp file advertise file in BOOTP replies\n"
4079 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4081 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4082 " redirect TCP or UDP connections from host to guest [-net user]\n"
4085 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4086 "-bt hci,host[:id]\n"
4087 " use host's HCI with the given name\n"
4088 "-bt hci[,vlan=n]\n"
4089 " emulate a standard HCI in virtual scatternet 'n'\n"
4090 "-bt vhci[,vlan=n]\n"
4091 " add host computer to virtual scatternet 'n' using VHCI\n"
4092 "-bt device:dev[,vlan=n]\n"
4093 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4097 "i386 target only:\n"
4098 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4099 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4100 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4101 "-no-acpi disable ACPI\n"
4102 "-no-hpet disable HPET\n"
4104 "Linux boot specific:\n"
4105 "-kernel bzImage use 'bzImage' as kernel image\n"
4106 "-append cmdline use 'cmdline' as kernel command line\n"
4107 "-initrd file use 'file' as initial ram disk\n"
4109 "Debug/Expert options:\n"
4110 "-serial dev redirect the serial port to char device 'dev'\n"
4111 "-parallel dev redirect the parallel port to char device 'dev'\n"
4112 "-monitor dev redirect the monitor to char device 'dev'\n"
4113 "-pidfile file write PID to 'file'\n"
4114 "-S freeze CPU at startup (use 'c' to start execution)\n"
4115 "-s wait gdb connection to port\n"
4116 "-p port set gdb connection port [default=%s]\n"
4117 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4118 "-hdachs c,h,s[,t]\n"
4119 " force hard disk 0 physical geometry and the optional BIOS\n"
4120 " translation (t=none or lba) (usually qemu can guess them)\n"
4121 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4122 "-bios file set the filename for the BIOS\n"
4124 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4125 "-no-kqemu disable KQEMU kernel module usage\n"
4128 "-enable-kvm enable KVM full virtualization support\n"
4131 #ifndef NO_CPU_EMULATION
4132 "-no-kvm disable KVM hardware virtualization\n"
4134 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4135 "-no-kvm-pit disable KVM kernel mode PIT\n"
4136 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4137 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4138 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4139 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4140 " expose a PCI device to the guest OS.\n"
4141 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4142 " 'string' is used in log output.\n"
4145 "-no-reboot exit instead of rebooting\n"
4146 "-no-shutdown stop before shutdown\n"
4147 "-loadvm [tag|id]\n"
4148 " start right away with a saved state (loadvm in monitor)\n"
4150 "-daemonize daemonize QEMU after initializing\n"
4152 "-tdf inject timer interrupts that got lost\n"
4153 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4154 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also enables allocation of guest memory with huge pages\n"
4155 "-option-rom rom load a file, rom, into the option ROM space\n"
4156 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4157 "-prom-env variable=value\n"
4158 " set OpenBIOS nvram variables\n"
4160 "-clock force the use of the given methods for timer alarm.\n"
4161 " To see what timers are available use -clock ?\n"
4162 "-localtime set the real time clock to local time [default=utc]\n"
4163 "-startdate select initial date of the clock\n"
4164 "-icount [N|auto]\n"
4165 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4166 "-echr chr set terminal escape character instead of ctrl-a\n"
4167 "-virtioconsole c\n"
4168 " set virtio console\n"
4169 "-show-cursor show cursor\n"
4170 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4171 "-semihosting semihosting mode\n"
4173 #if defined(TARGET_ARM)
4174 "-old-param old param mode\n"
4176 "-tb-size n set TB size\n"
4177 "-incoming p prepare for incoming migration, listen on port p\n"
4179 "During emulation, the following keys are useful:\n"
4180 "ctrl-alt-f toggle full screen\n"
4181 "ctrl-alt-n switch to virtual console 'n'\n"
4182 "ctrl-alt toggle mouse and keyboard grab\n"
4184 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4189 DEFAULT_NETWORK_SCRIPT
,
4190 DEFAULT_NETWORK_DOWN_SCRIPT
,
4192 DEFAULT_GDBSTUB_PORT
,
4197 #define HAS_ARG 0x0001
4200 /* Please keep in synch with help, qemu_options[] and
4202 /* Standard options: */
4215 QEMU_OPTION_mtdblock
,
4219 QEMU_OPTION_snapshot
,
4222 QEMU_OPTION_audio_help
,
4223 QEMU_OPTION_soundhw
,
4225 QEMU_OPTION_usbdevice
,
4229 /* Display options: */
4230 QEMU_OPTION_nographic
,
4232 QEMU_OPTION_no_frame
,
4233 QEMU_OPTION_alt_grab
,
4234 QEMU_OPTION_no_quit
,
4236 QEMU_OPTION_portrait
,
4238 QEMU_OPTION_full_screen
,
4242 /* Network options: */
4250 /* i386 target only: */
4251 QEMU_OPTION_win2k_hack
,
4252 QEMU_OPTION_rtc_td_hack
,
4253 QEMU_OPTION_no_fd_bootchk
,
4254 QEMU_OPTION_no_acpi
,
4255 QEMU_OPTION_no_hpet
,
4257 /* Linux boot specific: */
4262 /* Debug/Expert options: */
4264 QEMU_OPTION_parallel
,
4265 QEMU_OPTION_monitor
,
4266 QEMU_OPTION_pidfile
,
4274 QEMU_OPTION_kernel_kqemu
,
4275 QEMU_OPTION_no_kqemu
,
4276 QEMU_OPTION_enable_kvm
,
4277 QEMU_OPTION_enable_nesting
,
4279 QEMU_OPTION_no_kvm_irqchip
,
4280 QEMU_OPTION_no_kvm_pit
,
4281 QEMU_OPTION_no_kvm_pit_reinjection
,
4282 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4283 QEMU_OPTION_pcidevice
,
4285 QEMU_OPTION_no_reboot
,
4286 QEMU_OPTION_no_shutdown
,
4288 QEMU_OPTION_daemonize
,
4289 QEMU_OPTION_option_rom
,
4290 QEMU_OPTION_cpu_vendor
,
4292 QEMU_OPTION_prom_env
,
4294 QEMU_OPTION_localtime
,
4295 QEMU_OPTION_startdate
,
4298 QEMU_OPTION_virtiocon
,
4299 QEMU_OPTION_show_cursor
,
4300 QEMU_OPTION_semihosting
,
4301 QEMU_OPTION_old_param
,
4302 QEMU_OPTION_tb_size
,
4303 QEMU_OPTION_incoming
,
4305 QEMU_OPTION_kvm_shadow_memory
,
4306 QEMU_OPTION_mempath
,
4309 typedef struct QEMUOption
{
4315 static const QEMUOption qemu_options
[] = {
4316 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4318 /* Standard options: */
4319 { "h", 0, QEMU_OPTION_h
},
4320 { "help", 0, QEMU_OPTION_h
},
4321 { "M", HAS_ARG
, QEMU_OPTION_M
},
4322 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4323 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4324 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4325 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4326 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4327 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4328 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4329 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4330 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4331 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4332 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4333 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4334 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4335 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4336 { "snapshot", 0, QEMU_OPTION_snapshot
},
4337 { "m", HAS_ARG
, QEMU_OPTION_m
},
4339 { "k", HAS_ARG
, QEMU_OPTION_k
},
4342 { "audio-help", 0, QEMU_OPTION_audio_help
},
4343 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4345 { "usb", 0, QEMU_OPTION_usb
},
4346 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4347 { "name", HAS_ARG
, QEMU_OPTION_name
},
4348 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4350 /* Display options: */
4351 { "nographic", 0, QEMU_OPTION_nographic
},
4352 #ifdef CONFIG_CURSES
4353 { "curses", 0, QEMU_OPTION_curses
},
4356 { "no-frame", 0, QEMU_OPTION_no_frame
},
4357 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4358 { "no-quit", 0, QEMU_OPTION_no_quit
},
4359 { "sdl", 0, QEMU_OPTION_sdl
},
4361 { "portrait", 0, QEMU_OPTION_portrait
},
4362 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4363 { "full-screen", 0, QEMU_OPTION_full_screen
},
4364 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4365 { "g", 1, QEMU_OPTION_g
},
4367 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4369 /* Network options: */
4370 { "net", HAS_ARG
, QEMU_OPTION_net
},
4372 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4373 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4375 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4377 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4379 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4381 /* i386 target only: */
4382 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4383 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4384 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4385 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4386 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4389 /* Linux boot specific: */
4390 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4391 { "append", HAS_ARG
, QEMU_OPTION_append
},
4392 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4394 /* Debug/Expert options: */
4395 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4396 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4397 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4398 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4399 { "S", 0, QEMU_OPTION_S
},
4400 { "s", 0, QEMU_OPTION_s
},
4401 { "p", HAS_ARG
, QEMU_OPTION_p
},
4402 { "d", HAS_ARG
, QEMU_OPTION_d
},
4403 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4404 { "L", HAS_ARG
, QEMU_OPTION_L
},
4405 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4407 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4408 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4411 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4414 #ifndef NO_CPU_EMULATION
4415 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4417 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4418 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4419 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4420 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4421 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4422 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4425 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4426 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4427 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4428 { "daemonize", 0, QEMU_OPTION_daemonize
},
4429 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4430 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4431 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4433 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4434 { "localtime", 0, QEMU_OPTION_localtime
},
4435 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4436 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4437 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4438 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4439 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4440 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4441 { "semihosting", 0, QEMU_OPTION_semihosting
},
4443 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4444 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4445 { "nvram", HAS_ARG
, QEMU_OPTION_nvram
},
4446 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4447 #if defined(TARGET_ARM)
4448 { "old-param", 0, QEMU_OPTION_old_param
},
4450 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4451 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4452 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4456 /* password input */
4458 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4463 if (!bdrv_is_encrypted(bs
))
4466 term_printf("%s is encrypted.\n", name
);
4467 for(i
= 0; i
< 3; i
++) {
4468 monitor_readline("Password: ", 1, password
, sizeof(password
));
4469 if (bdrv_set_key(bs
, password
) == 0)
4471 term_printf("invalid password\n");
4476 static BlockDriverState
*get_bdrv(int index
)
4478 if (index
> nb_drives
)
4480 return drives_table
[index
].bdrv
;
4483 static void read_passwords(void)
4485 BlockDriverState
*bs
;
4488 for(i
= 0; i
< 6; i
++) {
4491 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4496 struct soundhw soundhw
[] = {
4497 #ifdef HAS_AUDIO_CHOICE
4498 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4504 { .init_isa
= pcspk_audio_init
}
4511 "Creative Sound Blaster 16",
4514 { .init_isa
= SB16_init
}
4518 #ifdef CONFIG_CS4231A
4524 { .init_isa
= cs4231a_init
}
4532 "Yamaha YMF262 (OPL3)",
4534 "Yamaha YM3812 (OPL2)",
4538 { .init_isa
= Adlib_init
}
4545 "Gravis Ultrasound GF1",
4548 { .init_isa
= GUS_init
}
4555 "Intel 82801AA AC97 Audio",
4558 { .init_pci
= ac97_init
}
4562 #ifdef CONFIG_ES1370
4565 "ENSONIQ AudioPCI ES1370",
4568 { .init_pci
= es1370_init
}
4572 #endif /* HAS_AUDIO_CHOICE */
4574 { NULL
, NULL
, 0, 0, { NULL
} }
4577 static void select_soundhw (const char *optarg
)
4581 if (*optarg
== '?') {
4584 printf ("Valid sound card names (comma separated):\n");
4585 for (c
= soundhw
; c
->name
; ++c
) {
4586 printf ("%-11s %s\n", c
->name
, c
->descr
);
4588 printf ("\n-soundhw all will enable all of the above\n");
4589 exit (*optarg
!= '?');
4597 if (!strcmp (optarg
, "all")) {
4598 for (c
= soundhw
; c
->name
; ++c
) {
4606 e
= strchr (p
, ',');
4607 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4609 for (c
= soundhw
; c
->name
; ++c
) {
4610 if (!strncmp (c
->name
, p
, l
)) {
4619 "Unknown sound card name (too big to show)\n");
4622 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4627 p
+= l
+ (e
!= NULL
);
4631 goto show_valid_cards
;
4636 static void select_vgahw (const char *p
)
4640 if (strstart(p
, "std", &opts
)) {
4641 std_vga_enabled
= 1;
4642 cirrus_vga_enabled
= 0;
4644 } else if (strstart(p
, "cirrus", &opts
)) {
4645 cirrus_vga_enabled
= 1;
4646 std_vga_enabled
= 0;
4648 } else if (strstart(p
, "vmware", &opts
)) {
4649 cirrus_vga_enabled
= 0;
4650 std_vga_enabled
= 0;
4652 } else if (strstart(p
, "none", &opts
)) {
4653 cirrus_vga_enabled
= 0;
4654 std_vga_enabled
= 0;
4658 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4662 const char *nextopt
;
4664 if (strstart(opts
, ",retrace=", &nextopt
)) {
4666 if (strstart(opts
, "dumb", &nextopt
))
4667 vga_retrace_method
= VGA_RETRACE_DUMB
;
4668 else if (strstart(opts
, "precise", &nextopt
))
4669 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4670 else goto invalid_vga
;
4671 } else goto invalid_vga
;
4677 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4679 exit(STATUS_CONTROL_C_EXIT
);
4684 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4688 if(strlen(str
) != 36)
4691 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4692 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4693 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4701 #define MAX_NET_CLIENTS 32
4703 static int saved_argc
;
4704 static char **saved_argv
;
4706 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4710 *opt_daemonize
= daemonize
;
4711 *opt_incoming
= incoming
;
4715 static int gethugepagesize(void)
4719 const char *needle
= "Hugepagesize:";
4721 unsigned long hugepagesize
;
4723 fd
= open("/proc/meminfo", O_RDONLY
);
4729 ret
= read(fd
, buf
, sizeof(buf
));
4735 size
= strstr(buf
, needle
);
4738 size
+= strlen(needle
);
4739 hugepagesize
= strtol(size
, NULL
, 0);
4740 return hugepagesize
;
4743 static void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4749 if (!kvm_has_sync_mmu()) {
4750 fprintf(stderr
, "host lacks mmu notifiers, disabling --mem-path\n");
4754 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4757 hpagesize
= gethugepagesize() * 1024;
4761 fd
= mkstemp(filename
);
4770 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4773 * ftruncate is not supported by hugetlbfs in older
4774 * hosts, so don't bother checking for errors.
4775 * If anything goes wrong with it under other filesystems,
4778 ftruncate(fd
, memory
);
4780 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4781 if (area
== MAP_FAILED
) {
4791 static void *qemu_alloc_physram(unsigned long memory
)
4794 unsigned long map_len
= memory
;
4798 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4801 area
= qemu_vmalloc(memory
);
4803 if (kvm_setup_guest_memory(area
, map_len
))
4811 static void termsig_handler(int signal
)
4813 qemu_system_shutdown_request();
4816 static void termsig_setup(void)
4818 struct sigaction act
;
4820 memset(&act
, 0, sizeof(act
));
4821 act
.sa_handler
= termsig_handler
;
4822 sigaction(SIGINT
, &act
, NULL
);
4823 sigaction(SIGHUP
, &act
, NULL
);
4824 sigaction(SIGTERM
, &act
, NULL
);
4829 int main(int argc
, char **argv
, char **envp
)
4831 #ifdef CONFIG_GDBSTUB
4833 const char *gdbstub_port
;
4835 uint32_t boot_devices_bitmap
= 0;
4837 int snapshot
, linux_boot
, net_boot
;
4838 const char *initrd_filename
;
4839 const char *kernel_filename
, *kernel_cmdline
;
4840 const char *boot_devices
= "";
4842 DisplayChangeListener
*dcl
;
4843 int cyls
, heads
, secs
, translation
;
4844 const char *net_clients
[MAX_NET_CLIENTS
];
4846 const char *bt_opts
[MAX_BT_CMDLINE
];
4850 const char *r
, *optarg
;
4851 CharDriverState
*monitor_hd
= NULL
;
4852 const char *monitor_device
;
4853 const char *serial_devices
[MAX_SERIAL_PORTS
];
4854 int serial_device_index
;
4855 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4856 int parallel_device_index
;
4857 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4858 int virtio_console_index
;
4859 const char *loadvm
= NULL
;
4860 QEMUMachine
*machine
;
4861 const char *cpu_model
;
4862 const char *usb_devices
[MAX_USB_CMDLINE
];
4863 int usb_devices_index
;
4866 const char *pid_file
= NULL
;
4868 const char *incoming
= NULL
;
4870 qemu_cache_utils_init(envp
);
4872 LIST_INIT (&vm_change_state_head
);
4875 struct sigaction act
;
4876 sigfillset(&act
.sa_mask
);
4878 act
.sa_handler
= SIG_IGN
;
4879 sigaction(SIGPIPE
, &act
, NULL
);
4882 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4883 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4884 QEMU to run on a single CPU */
4889 h
= GetCurrentProcess();
4890 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4891 for(i
= 0; i
< 32; i
++) {
4892 if (mask
& (1 << i
))
4897 SetProcessAffinityMask(h
, mask
);
4903 register_machines();
4904 machine
= first_machine
;
4906 initrd_filename
= NULL
;
4908 vga_ram_size
= VGA_RAM_SIZE
;
4909 #ifdef CONFIG_GDBSTUB
4911 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4916 kernel_filename
= NULL
;
4917 kernel_cmdline
= "";
4918 cyls
= heads
= secs
= 0;
4919 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4920 monitor_device
= "vc";
4922 serial_devices
[0] = "vc:80Cx24C";
4923 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4924 serial_devices
[i
] = NULL
;
4925 serial_device_index
= 0;
4927 parallel_devices
[0] = "vc:640x480";
4928 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4929 parallel_devices
[i
] = NULL
;
4930 parallel_device_index
= 0;
4932 virtio_consoles
[0] = "vc:80Cx24C";
4933 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4934 virtio_consoles
[i
] = NULL
;
4935 virtio_console_index
= 0;
4937 usb_devices_index
= 0;
4938 assigned_devices_index
= 0;
4957 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4959 const QEMUOption
*popt
;
4962 /* Treat --foo the same as -foo. */
4965 popt
= qemu_options
;
4968 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4972 if (!strcmp(popt
->name
, r
+ 1))
4976 if (popt
->flags
& HAS_ARG
) {
4977 if (optind
>= argc
) {
4978 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4982 optarg
= argv
[optind
++];
4987 switch(popt
->index
) {
4989 machine
= find_machine(optarg
);
4992 printf("Supported machines are:\n");
4993 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4994 printf("%-10s %s%s\n",
4996 m
== first_machine
? " (default)" : "");
4998 exit(*optarg
!= '?');
5001 case QEMU_OPTION_cpu
:
5002 /* hw initialization will check this */
5003 if (*optarg
== '?') {
5004 /* XXX: implement xxx_cpu_list for targets that still miss it */
5005 #if defined(cpu_list)
5006 cpu_list(stdout
, &fprintf
);
5013 case QEMU_OPTION_initrd
:
5014 initrd_filename
= optarg
;
5016 case QEMU_OPTION_hda
:
5018 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5020 hda_index
= drive_add(optarg
, HD_ALIAS
5021 ",cyls=%d,heads=%d,secs=%d%s",
5022 0, cyls
, heads
, secs
,
5023 translation
== BIOS_ATA_TRANSLATION_LBA
?
5025 translation
== BIOS_ATA_TRANSLATION_NONE
?
5026 ",trans=none" : "");
5028 case QEMU_OPTION_hdb
:
5029 case QEMU_OPTION_hdc
:
5030 case QEMU_OPTION_hdd
:
5031 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5033 case QEMU_OPTION_drive
:
5034 drive_add(NULL
, "%s", optarg
);
5036 case QEMU_OPTION_mtdblock
:
5037 drive_add(optarg
, MTD_ALIAS
);
5039 case QEMU_OPTION_sd
:
5040 drive_add(optarg
, SD_ALIAS
);
5042 case QEMU_OPTION_pflash
:
5043 drive_add(optarg
, PFLASH_ALIAS
);
5045 case QEMU_OPTION_snapshot
:
5048 case QEMU_OPTION_hdachs
:
5052 cyls
= strtol(p
, (char **)&p
, 0);
5053 if (cyls
< 1 || cyls
> 16383)
5058 heads
= strtol(p
, (char **)&p
, 0);
5059 if (heads
< 1 || heads
> 16)
5064 secs
= strtol(p
, (char **)&p
, 0);
5065 if (secs
< 1 || secs
> 63)
5069 if (!strcmp(p
, "none"))
5070 translation
= BIOS_ATA_TRANSLATION_NONE
;
5071 else if (!strcmp(p
, "lba"))
5072 translation
= BIOS_ATA_TRANSLATION_LBA
;
5073 else if (!strcmp(p
, "auto"))
5074 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5077 } else if (*p
!= '\0') {
5079 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5082 if (hda_index
!= -1)
5083 snprintf(drives_opt
[hda_index
].opt
,
5084 sizeof(drives_opt
[hda_index
].opt
),
5085 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5086 0, cyls
, heads
, secs
,
5087 translation
== BIOS_ATA_TRANSLATION_LBA
?
5089 translation
== BIOS_ATA_TRANSLATION_NONE
?
5090 ",trans=none" : "");
5093 case QEMU_OPTION_nographic
:
5096 #ifdef CONFIG_CURSES
5097 case QEMU_OPTION_curses
:
5101 case QEMU_OPTION_portrait
:
5104 case QEMU_OPTION_kernel
:
5105 kernel_filename
= optarg
;
5107 case QEMU_OPTION_append
:
5108 kernel_cmdline
= optarg
;
5110 case QEMU_OPTION_cdrom
:
5111 drive_add(optarg
, CDROM_ALIAS
);
5113 case QEMU_OPTION_boot
:
5114 boot_devices
= optarg
;
5115 /* We just do some generic consistency checks */
5117 /* Could easily be extended to 64 devices if needed */
5120 boot_devices_bitmap
= 0;
5121 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5122 /* Allowed boot devices are:
5123 * a b : floppy disk drives
5124 * c ... f : IDE disk drives
5125 * g ... m : machine implementation dependant drives
5126 * n ... p : network devices
5127 * It's up to each machine implementation to check
5128 * if the given boot devices match the actual hardware
5129 * implementation and firmware features.
5131 if (*p
< 'a' || *p
> 'q') {
5132 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5135 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5137 "Boot device '%c' was given twice\n",*p
);
5140 boot_devices_bitmap
|= 1 << (*p
- 'a');
5144 case QEMU_OPTION_fda
:
5145 case QEMU_OPTION_fdb
:
5146 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5149 case QEMU_OPTION_no_fd_bootchk
:
5153 case QEMU_OPTION_net
:
5154 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5155 fprintf(stderr
, "qemu: too many network clients\n");
5158 net_clients
[nb_net_clients
] = optarg
;
5162 case QEMU_OPTION_tftp
:
5163 tftp_prefix
= optarg
;
5165 case QEMU_OPTION_bootp
:
5166 bootp_filename
= optarg
;
5169 case QEMU_OPTION_smb
:
5170 net_slirp_smb(optarg
);
5173 case QEMU_OPTION_redir
:
5174 net_slirp_redir(optarg
);
5177 case QEMU_OPTION_bt
:
5178 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5179 fprintf(stderr
, "qemu: too many bluetooth options\n");
5182 bt_opts
[nb_bt_opts
++] = optarg
;
5185 case QEMU_OPTION_audio_help
:
5189 case QEMU_OPTION_soundhw
:
5190 select_soundhw (optarg
);
5196 case QEMU_OPTION_m
: {
5200 value
= strtoul(optarg
, &ptr
, 10);
5202 case 0: case 'M': case 'm':
5209 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5213 /* On 32-bit hosts, QEMU is limited by virtual address space */
5214 if (value
> (2047 << 20)
5216 && HOST_LONG_BITS
== 32
5219 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5222 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5223 fprintf(stderr
, "qemu: ram size too large\n");
5232 const CPULogItem
*item
;
5234 mask
= cpu_str_to_log_mask(optarg
);
5236 printf("Log items (comma separated):\n");
5237 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5238 printf("%-10s %s\n", item
->name
, item
->help
);
5245 #ifdef CONFIG_GDBSTUB
5250 gdbstub_port
= optarg
;
5256 case QEMU_OPTION_bios
:
5263 keyboard_layout
= optarg
;
5265 case QEMU_OPTION_localtime
:
5268 case QEMU_OPTION_vga
:
5269 select_vgahw (optarg
);
5276 w
= strtol(p
, (char **)&p
, 10);
5279 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5285 h
= strtol(p
, (char **)&p
, 10);
5290 depth
= strtol(p
, (char **)&p
, 10);
5291 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5292 depth
!= 24 && depth
!= 32)
5294 } else if (*p
== '\0') {
5295 depth
= graphic_depth
;
5302 graphic_depth
= depth
;
5305 case QEMU_OPTION_echr
:
5308 term_escape_char
= strtol(optarg
, &r
, 0);
5310 printf("Bad argument to echr\n");
5313 case QEMU_OPTION_monitor
:
5314 monitor_device
= optarg
;
5316 case QEMU_OPTION_serial
:
5317 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5318 fprintf(stderr
, "qemu: too many serial ports\n");
5321 serial_devices
[serial_device_index
] = optarg
;
5322 serial_device_index
++;
5324 case QEMU_OPTION_virtiocon
:
5325 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5326 fprintf(stderr
, "qemu: too many virtio consoles\n");
5329 virtio_consoles
[virtio_console_index
] = optarg
;
5330 virtio_console_index
++;
5332 case QEMU_OPTION_parallel
:
5333 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5334 fprintf(stderr
, "qemu: too many parallel ports\n");
5337 parallel_devices
[parallel_device_index
] = optarg
;
5338 parallel_device_index
++;
5340 case QEMU_OPTION_loadvm
:
5343 case QEMU_OPTION_full_screen
:
5347 case QEMU_OPTION_no_frame
:
5350 case QEMU_OPTION_alt_grab
:
5353 case QEMU_OPTION_no_quit
:
5356 case QEMU_OPTION_sdl
:
5360 case QEMU_OPTION_pidfile
:
5364 case QEMU_OPTION_win2k_hack
:
5365 win2k_install_hack
= 1;
5367 case QEMU_OPTION_rtc_td_hack
:
5372 case QEMU_OPTION_no_kqemu
:
5375 case QEMU_OPTION_kernel_kqemu
:
5380 case QEMU_OPTION_enable_kvm
:
5388 case QEMU_OPTION_no_kvm
:
5391 case QEMU_OPTION_no_kvm_irqchip
: {
5396 case QEMU_OPTION_no_kvm_pit
: {
5400 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5401 kvm_pit_reinject
= 0;
5404 case QEMU_OPTION_enable_nesting
: {
5408 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5409 case QEMU_OPTION_pcidevice
:
5410 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5411 fprintf(stderr
, "Too many assigned devices\n");
5414 assigned_devices
[assigned_devices_index
] = optarg
;
5415 assigned_devices_index
++;
5419 case QEMU_OPTION_usb
:
5422 case QEMU_OPTION_usbdevice
:
5424 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5425 fprintf(stderr
, "Too many USB devices\n");
5428 usb_devices
[usb_devices_index
] = optarg
;
5429 usb_devices_index
++;
5431 case QEMU_OPTION_smp
:
5432 smp_cpus
= atoi(optarg
);
5434 fprintf(stderr
, "Invalid number of CPUs\n");
5438 case QEMU_OPTION_vnc
:
5439 vnc_display
= optarg
;
5441 case QEMU_OPTION_no_acpi
:
5444 case QEMU_OPTION_no_hpet
:
5447 case QEMU_OPTION_no_reboot
:
5450 case QEMU_OPTION_no_shutdown
:
5453 case QEMU_OPTION_show_cursor
:
5456 case QEMU_OPTION_uuid
:
5457 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5458 fprintf(stderr
, "Fail to parse UUID string."
5459 " Wrong format.\n");
5463 case QEMU_OPTION_daemonize
:
5466 case QEMU_OPTION_option_rom
:
5467 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5468 fprintf(stderr
, "Too many option ROMs\n");
5471 option_rom
[nb_option_roms
] = optarg
;
5474 case QEMU_OPTION_semihosting
:
5475 semihosting_enabled
= 1;
5477 case QEMU_OPTION_tdf
:
5480 case QEMU_OPTION_kvm_shadow_memory
:
5481 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5483 case QEMU_OPTION_mempath
:
5486 case QEMU_OPTION_name
:
5489 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5490 case QEMU_OPTION_prom_env
:
5491 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5492 fprintf(stderr
, "Too many prom variables\n");
5495 prom_envs
[nb_prom_envs
] = optarg
;
5499 case QEMU_OPTION_cpu_vendor
:
5500 cpu_vendor_string
= optarg
;
5503 case QEMU_OPTION_old_param
:
5507 case QEMU_OPTION_clock
:
5508 configure_alarms(optarg
);
5510 case QEMU_OPTION_startdate
:
5513 time_t rtc_start_date
;
5514 if (!strcmp(optarg
, "now")) {
5515 rtc_date_offset
= -1;
5517 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5525 } else if (sscanf(optarg
, "%d-%d-%d",
5528 &tm
.tm_mday
) == 3) {
5537 rtc_start_date
= mktimegm(&tm
);
5538 if (rtc_start_date
== -1) {
5540 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5541 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5544 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5548 case QEMU_OPTION_tb_size
:
5549 tb_size
= strtol(optarg
, NULL
, 0);
5553 case QEMU_OPTION_icount
:
5555 if (strcmp(optarg
, "auto") == 0) {
5556 icount_time_shift
= -1;
5558 icount_time_shift
= strtol(optarg
, NULL
, 0);
5561 case QEMU_OPTION_incoming
:
5564 case QEMU_OPTION_nvram
:
5571 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5572 if (kvm_allowed
&& kqemu_allowed
) {
5574 "You can not enable both KVM and kqemu at the same time\n");
5579 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5580 if (smp_cpus
> machine
->max_cpus
) {
5581 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5582 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5588 if (serial_device_index
== 0)
5589 serial_devices
[0] = "stdio";
5590 if (parallel_device_index
== 0)
5591 parallel_devices
[0] = "null";
5592 if (strncmp(monitor_device
, "vc", 2) == 0)
5593 monitor_device
= "stdio";
5594 if (virtio_console_index
== 0)
5595 virtio_consoles
[0] = "null";
5602 if (pipe(fds
) == -1)
5613 len
= read(fds
[0], &status
, 1);
5614 if (len
== -1 && (errno
== EINTR
))
5619 else if (status
== 1) {
5620 fprintf(stderr
, "Could not acquire pidfile\n");
5637 signal(SIGTSTP
, SIG_IGN
);
5638 signal(SIGTTOU
, SIG_IGN
);
5639 signal(SIGTTIN
, SIG_IGN
);
5644 if (kvm_enabled()) {
5645 if (kvm_qemu_init() < 0) {
5646 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5647 #ifdef NO_CPU_EMULATION
5648 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5656 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5659 write(fds
[1], &status
, 1);
5661 fprintf(stderr
, "Could not acquire pid file\n");
5669 linux_boot
= (kernel_filename
!= NULL
);
5670 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5672 if (!linux_boot
&& net_boot
== 0 &&
5673 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5676 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5677 fprintf(stderr
, "-append only allowed with -kernel option\n");
5681 if (!linux_boot
&& initrd_filename
!= NULL
) {
5682 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5686 /* boot to floppy or the default cd if no hard disk defined yet */
5687 if (!boot_devices
[0]) {
5688 boot_devices
= "cad";
5690 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5693 if (init_timer_alarm() < 0) {
5694 fprintf(stderr
, "could not initialize alarm timer\n");
5697 if (use_icount
&& icount_time_shift
< 0) {
5699 /* 125MIPS seems a reasonable initial guess at the guest speed.
5700 It will be corrected fairly quickly anyway. */
5701 icount_time_shift
= 3;
5702 init_icount_adjust();
5709 /* init network clients */
5710 if (nb_net_clients
== 0) {
5711 /* if no clients, we use a default config */
5712 net_clients
[nb_net_clients
++] = "nic";
5714 net_clients
[nb_net_clients
++] = "user";
5718 for(i
= 0;i
< nb_net_clients
; i
++) {
5719 if (net_client_parse(net_clients
[i
]) < 0)
5725 /* XXX: this should be moved in the PC machine instantiation code */
5726 if (net_boot
!= 0) {
5728 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5729 const char *model
= nd_table
[i
].model
;
5731 if (net_boot
& (1 << i
)) {
5734 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5735 if (get_image_size(buf
) > 0) {
5736 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5737 fprintf(stderr
, "Too many option ROMs\n");
5740 option_rom
[nb_option_roms
] = strdup(buf
);
5747 fprintf(stderr
, "No valid PXE rom found for network device\n");
5753 /* init the bluetooth world */
5754 for (i
= 0; i
< nb_bt_opts
; i
++)
5755 if (bt_parse(bt_opts
[i
]))
5758 /* init the memory */
5759 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5761 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5763 if (ram_size
< phys_ram_size
) {
5764 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5765 machine
->name
, (unsigned long long) phys_ram_size
);
5769 phys_ram_size
= ram_size
;
5771 ram_size
= phys_ram_size
;
5774 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5776 phys_ram_size
+= ram_size
;
5779 /* Initialize kvm */
5780 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5781 #define KVM_EXTRA_PAGES 3
5783 #define KVM_EXTRA_PAGES 0
5785 if (kvm_enabled()) {
5786 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5787 if (kvm_qemu_create_context() < 0) {
5788 fprintf(stderr
, "Could not create KVM context\n");
5793 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5794 if (!phys_ram_base
) {
5795 fprintf(stderr
, "Could not allocate physical memory\n");
5799 /* init the dynamic translator */
5800 cpu_exec_init_all(tb_size
* 1024 * 1024);
5804 /* we always create the cdrom drive, even if no disk is there */
5806 if (nb_drives_opt
< MAX_DRIVES
)
5807 drive_add(NULL
, CDROM_ALIAS
);
5809 /* we always create at least one floppy */
5811 if (nb_drives_opt
< MAX_DRIVES
)
5812 drive_add(NULL
, FD_ALIAS
, 0);
5814 /* we always create one sd slot, even if no card is in it */
5816 if (nb_drives_opt
< MAX_DRIVES
)
5817 drive_add(NULL
, SD_ALIAS
);
5819 /* open the virtual block devices
5820 * note that migration with device
5821 * hot add/remove is broken.
5823 for(i
= 0; i
< nb_drives_opt
; i
++)
5824 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5827 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5828 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5831 /* must be after terminal init, SDL library changes signal handlers */
5835 /* Maintain compatibility with multiple stdio monitors */
5836 if (!strcmp(monitor_device
,"stdio")) {
5837 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5838 const char *devname
= serial_devices
[i
];
5839 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5840 monitor_device
= NULL
;
5842 } else if (devname
&& !strcmp(devname
,"stdio")) {
5843 monitor_device
= NULL
;
5844 serial_devices
[i
] = "mon:stdio";
5851 if (kvm_enabled()) {
5854 ret
= kvm_init(smp_cpus
);
5856 fprintf(stderr
, "failed to initialize KVM\n");
5862 if (monitor_device
) {
5863 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5865 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5870 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5871 const char *devname
= serial_devices
[i
];
5872 if (devname
&& strcmp(devname
, "none")) {
5874 snprintf(label
, sizeof(label
), "serial%d", i
);
5875 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5876 if (!serial_hds
[i
]) {
5877 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5884 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5885 const char *devname
= parallel_devices
[i
];
5886 if (devname
&& strcmp(devname
, "none")) {
5888 snprintf(label
, sizeof(label
), "parallel%d", i
);
5889 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5890 if (!parallel_hds
[i
]) {
5891 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5898 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5899 const char *devname
= virtio_consoles
[i
];
5900 if (devname
&& strcmp(devname
, "none")) {
5902 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5903 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5904 if (!virtcon_hds
[i
]) {
5905 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5915 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5916 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5918 current_machine
= machine
;
5920 /* Set KVM's vcpu state to qemu's initial CPUState. */
5921 if (kvm_enabled()) {
5924 ret
= kvm_sync_vcpus();
5926 fprintf(stderr
, "failed to initialize vcpus\n");
5931 /* init USB devices */
5933 for(i
= 0; i
< usb_devices_index
; i
++) {
5934 if (usb_device_add(usb_devices
[i
]) < 0) {
5935 fprintf(stderr
, "Warning: could not add USB device %s\n",
5942 dumb_display_init();
5943 /* just use the first displaystate for the moment */
5948 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5952 #if defined(CONFIG_CURSES)
5954 /* At the moment curses cannot be used with other displays */
5955 curses_display_init(ds
, full_screen
);
5959 if (vnc_display
!= NULL
) {
5960 vnc_display_init(ds
);
5961 if (vnc_display_open(ds
, vnc_display
) < 0)
5964 #if defined(CONFIG_SDL)
5965 if (sdl
|| !vnc_display
)
5966 sdl_display_init(ds
, full_screen
, no_frame
);
5967 #elif defined(CONFIG_COCOA)
5968 if (sdl
|| !vnc_display
)
5969 cocoa_display_init(ds
, full_screen
);
5975 dcl
= ds
->listeners
;
5976 while (dcl
!= NULL
) {
5977 if (dcl
->dpy_refresh
!= NULL
) {
5978 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5979 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5984 if (nographic
|| (vnc_display
&& !sdl
)) {
5985 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5986 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5989 text_consoles_set_display(display_state
);
5991 if (monitor_device
&& monitor_hd
)
5992 monitor_init(monitor_hd
, !nographic
);
5994 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5995 const char *devname
= serial_devices
[i
];
5996 if (devname
&& strcmp(devname
, "none")) {
5998 snprintf(label
, sizeof(label
), "serial%d", i
);
5999 if (strstart(devname
, "vc", 0))
6000 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6004 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6005 const char *devname
= parallel_devices
[i
];
6006 if (devname
&& strcmp(devname
, "none")) {
6008 snprintf(label
, sizeof(label
), "parallel%d", i
);
6009 if (strstart(devname
, "vc", 0))
6010 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6014 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6015 const char *devname
= virtio_consoles
[i
];
6016 if (virtcon_hds
[i
] && devname
) {
6018 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6019 if (strstart(devname
, "vc", 0))
6020 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6024 #ifdef CONFIG_GDBSTUB
6026 /* XXX: use standard host:port notation and modify options
6028 if (gdbserver_start(gdbstub_port
) < 0) {
6029 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
6040 autostart
= 0; /* fixme how to deal with -daemonize */
6041 qemu_start_incoming_migration(incoming
);
6045 /* XXX: simplify init */
6058 len
= write(fds
[1], &status
, 1);
6059 if (len
== -1 && (errno
== EINTR
))
6066 TFR(fd
= open("/dev/null", O_RDWR
));