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 int mem_prealloc
= 1; /* force preallocation of physical target memory */
251 const char *cpu_vendor_string
;
255 const char *qemu_name
;
257 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
258 unsigned int nb_prom_envs
= 0;
259 const char *prom_envs
[MAX_PROM_ENVS
];
262 const char *nvram
= NULL
;
263 struct drive_opt drives_opt
[MAX_DRIVES
];
265 static CPUState
*cur_cpu
;
266 static CPUState
*next_cpu
;
267 static int event_pending
= 1;
268 /* Conversion factor from emulated instructions to virtual clock ticks. */
269 static int icount_time_shift
;
270 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
271 #define MAX_ICOUNT_SHIFT 10
272 /* Compensate for varying guest execution speed. */
273 static int64_t qemu_icount_bias
;
274 static QEMUTimer
*icount_rt_timer
;
275 static QEMUTimer
*icount_vm_timer
;
276 static QEMUTimer
*nographic_timer
;
278 uint8_t qemu_uuid
[16];
280 /* KVM runs the main loop in a separate thread. If we update one of the lists
281 * that are polled before or after select(), we need to make sure to break out
282 * of the select() to ensure the new item is serviced.
284 static void main_loop_break(void)
287 qemu_kvm_notify_work();
290 /***********************************************************/
291 /* x86 ISA bus support */
293 target_phys_addr_t isa_mem_base
= 0;
296 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
297 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
299 static uint32_t ioport_read(int index
, uint32_t address
)
301 static IOPortReadFunc
*default_func
[3] = {
302 default_ioport_readb
,
303 default_ioport_readw
,
306 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
308 func
= default_func
[index
];
309 return func(ioport_opaque
[address
], address
);
312 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
314 static IOPortWriteFunc
*default_func
[3] = {
315 default_ioport_writeb
,
316 default_ioport_writew
,
317 default_ioport_writel
319 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
321 func
= default_func
[index
];
322 func(ioport_opaque
[address
], address
, data
);
325 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
327 #ifdef DEBUG_UNUSED_IOPORT
328 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
333 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
335 #ifdef DEBUG_UNUSED_IOPORT
336 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
340 /* default is to make two byte accesses */
341 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
344 data
= ioport_read(0, address
);
345 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
346 data
|= ioport_read(0, address
) << 8;
350 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
352 ioport_write(0, address
, data
& 0xff);
353 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
354 ioport_write(0, address
, (data
>> 8) & 0xff);
357 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
359 #ifdef DEBUG_UNUSED_IOPORT
360 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
365 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
367 #ifdef DEBUG_UNUSED_IOPORT
368 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
372 /* size is the word size in byte */
373 int register_ioport_read(int start
, int length
, int size
,
374 IOPortReadFunc
*func
, void *opaque
)
380 } else if (size
== 2) {
382 } else if (size
== 4) {
385 hw_error("register_ioport_read: invalid size");
388 for(i
= start
; i
< start
+ length
; i
+= size
) {
389 ioport_read_table
[bsize
][i
] = func
;
390 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
391 hw_error("register_ioport_read: invalid opaque");
392 ioport_opaque
[i
] = opaque
;
397 /* size is the word size in byte */
398 int register_ioport_write(int start
, int length
, int size
,
399 IOPortWriteFunc
*func
, void *opaque
)
405 } else if (size
== 2) {
407 } else if (size
== 4) {
410 hw_error("register_ioport_write: invalid size");
413 for(i
= start
; i
< start
+ length
; i
+= size
) {
414 ioport_write_table
[bsize
][i
] = func
;
415 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
416 hw_error("register_ioport_write: invalid opaque");
417 ioport_opaque
[i
] = opaque
;
422 void isa_unassign_ioport(int start
, int length
)
426 for(i
= start
; i
< start
+ length
; i
++) {
427 ioport_read_table
[0][i
] = default_ioport_readb
;
428 ioport_read_table
[1][i
] = default_ioport_readw
;
429 ioport_read_table
[2][i
] = default_ioport_readl
;
431 ioport_write_table
[0][i
] = default_ioport_writeb
;
432 ioport_write_table
[1][i
] = default_ioport_writew
;
433 ioport_write_table
[2][i
] = default_ioport_writel
;
435 ioport_opaque
[i
] = NULL
;
439 /***********************************************************/
441 void cpu_outb(CPUState
*env
, int addr
, int val
)
443 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
444 ioport_write(0, addr
, val
);
447 env
->last_io_time
= cpu_get_time_fast();
451 void cpu_outw(CPUState
*env
, int addr
, int val
)
453 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
454 ioport_write(1, addr
, val
);
457 env
->last_io_time
= cpu_get_time_fast();
461 void cpu_outl(CPUState
*env
, int addr
, int val
)
463 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
464 ioport_write(2, addr
, val
);
467 env
->last_io_time
= cpu_get_time_fast();
471 int cpu_inb(CPUState
*env
, int addr
)
474 val
= ioport_read(0, addr
);
475 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
478 env
->last_io_time
= cpu_get_time_fast();
483 int cpu_inw(CPUState
*env
, int addr
)
486 val
= ioport_read(1, addr
);
487 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
490 env
->last_io_time
= cpu_get_time_fast();
495 int cpu_inl(CPUState
*env
, int addr
)
498 val
= ioport_read(2, addr
);
499 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
502 env
->last_io_time
= cpu_get_time_fast();
507 /***********************************************************/
508 void hw_error(const char *fmt
, ...)
514 fprintf(stderr
, "qemu: hardware error: ");
515 vfprintf(stderr
, fmt
, ap
);
516 fprintf(stderr
, "\n");
517 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
518 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
520 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
522 cpu_dump_state(env
, stderr
, fprintf
, 0);
532 static QEMUBalloonEvent
*qemu_balloon_event
;
533 void *qemu_balloon_event_opaque
;
535 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
537 qemu_balloon_event
= func
;
538 qemu_balloon_event_opaque
= opaque
;
541 void qemu_balloon(ram_addr_t target
)
543 if (qemu_balloon_event
)
544 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
547 ram_addr_t
qemu_balloon_status(void)
549 if (qemu_balloon_event
)
550 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
554 /***********************************************************/
557 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
558 static void *qemu_put_kbd_event_opaque
;
559 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
560 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
562 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
564 qemu_put_kbd_event_opaque
= opaque
;
565 qemu_put_kbd_event
= func
;
568 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
569 void *opaque
, int absolute
,
572 QEMUPutMouseEntry
*s
, *cursor
;
574 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
576 s
->qemu_put_mouse_event
= func
;
577 s
->qemu_put_mouse_event_opaque
= opaque
;
578 s
->qemu_put_mouse_event_absolute
= absolute
;
579 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
582 if (!qemu_put_mouse_event_head
) {
583 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
587 cursor
= qemu_put_mouse_event_head
;
588 while (cursor
->next
!= NULL
)
589 cursor
= cursor
->next
;
592 qemu_put_mouse_event_current
= s
;
597 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
599 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
601 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
604 cursor
= qemu_put_mouse_event_head
;
605 while (cursor
!= NULL
&& cursor
!= entry
) {
607 cursor
= cursor
->next
;
610 if (cursor
== NULL
) // does not exist or list empty
612 else if (prev
== NULL
) { // entry is head
613 qemu_put_mouse_event_head
= cursor
->next
;
614 if (qemu_put_mouse_event_current
== entry
)
615 qemu_put_mouse_event_current
= cursor
->next
;
616 qemu_free(entry
->qemu_put_mouse_event_name
);
621 prev
->next
= entry
->next
;
623 if (qemu_put_mouse_event_current
== entry
)
624 qemu_put_mouse_event_current
= prev
;
626 qemu_free(entry
->qemu_put_mouse_event_name
);
630 void kbd_put_keycode(int keycode
)
632 if (qemu_put_kbd_event
) {
633 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
637 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
639 QEMUPutMouseEvent
*mouse_event
;
640 void *mouse_event_opaque
;
643 if (!qemu_put_mouse_event_current
) {
648 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
650 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
653 if (graphic_rotate
) {
654 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
657 width
= graphic_width
- 1;
658 mouse_event(mouse_event_opaque
,
659 width
- dy
, dx
, dz
, buttons_state
);
661 mouse_event(mouse_event_opaque
,
662 dx
, dy
, dz
, buttons_state
);
666 int kbd_mouse_is_absolute(void)
668 if (!qemu_put_mouse_event_current
)
671 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
674 void do_info_mice(void)
676 QEMUPutMouseEntry
*cursor
;
679 if (!qemu_put_mouse_event_head
) {
680 term_printf("No mouse devices connected\n");
684 term_printf("Mouse devices available:\n");
685 cursor
= qemu_put_mouse_event_head
;
686 while (cursor
!= NULL
) {
687 term_printf("%c Mouse #%d: %s\n",
688 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
689 index
, cursor
->qemu_put_mouse_event_name
);
691 cursor
= cursor
->next
;
695 void do_mouse_set(int index
)
697 QEMUPutMouseEntry
*cursor
;
700 if (!qemu_put_mouse_event_head
) {
701 term_printf("No mouse devices connected\n");
705 cursor
= qemu_put_mouse_event_head
;
706 while (cursor
!= NULL
&& index
!= i
) {
708 cursor
= cursor
->next
;
712 qemu_put_mouse_event_current
= cursor
;
714 term_printf("Mouse at given index not found\n");
717 /* compute with 96 bit intermediate result: (a*b)/c */
718 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
723 #ifdef WORDS_BIGENDIAN
733 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
734 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
737 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
741 /***********************************************************/
742 /* real time host monotonic timer */
744 #define QEMU_TIMER_BASE 1000000000LL
748 static int64_t clock_freq
;
750 static void init_get_clock(void)
754 ret
= QueryPerformanceFrequency(&freq
);
756 fprintf(stderr
, "Could not calibrate ticks\n");
759 clock_freq
= freq
.QuadPart
;
762 static int64_t get_clock(void)
765 QueryPerformanceCounter(&ti
);
766 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
771 static int use_rt_clock
;
773 static void init_get_clock(void)
776 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
779 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
786 static int64_t get_clock(void)
788 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
791 clock_gettime(CLOCK_MONOTONIC
, &ts
);
792 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
796 /* XXX: using gettimeofday leads to problems if the date
797 changes, so it should be avoided. */
799 gettimeofday(&tv
, NULL
);
800 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
805 /* Return the virtual CPU time, based on the instruction counter. */
806 static int64_t cpu_get_icount(void)
809 CPUState
*env
= cpu_single_env
;;
810 icount
= qemu_icount
;
813 fprintf(stderr
, "Bad clock read\n");
814 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
816 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
819 /***********************************************************/
820 /* guest cycle counter */
822 static int64_t cpu_ticks_prev
;
823 static int64_t cpu_ticks_offset
;
824 static int64_t cpu_clock_offset
;
825 static int cpu_ticks_enabled
;
827 /* return the host CPU cycle counter and handle stop/restart */
828 int64_t cpu_get_ticks(void)
831 return cpu_get_icount();
833 if (!cpu_ticks_enabled
) {
834 return cpu_ticks_offset
;
837 ticks
= cpu_get_real_ticks();
838 if (cpu_ticks_prev
> ticks
) {
839 /* Note: non increasing ticks may happen if the host uses
841 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
843 cpu_ticks_prev
= ticks
;
844 return ticks
+ cpu_ticks_offset
;
848 /* return the host CPU monotonic timer and handle stop/restart */
849 static int64_t cpu_get_clock(void)
852 if (!cpu_ticks_enabled
) {
853 return cpu_clock_offset
;
856 return ti
+ cpu_clock_offset
;
860 /* enable cpu_get_ticks() */
861 void cpu_enable_ticks(void)
863 if (!cpu_ticks_enabled
) {
864 cpu_ticks_offset
-= cpu_get_real_ticks();
865 cpu_clock_offset
-= get_clock();
866 cpu_ticks_enabled
= 1;
870 /* disable cpu_get_ticks() : the clock is stopped. You must not call
871 cpu_get_ticks() after that. */
872 void cpu_disable_ticks(void)
874 if (cpu_ticks_enabled
) {
875 cpu_ticks_offset
= cpu_get_ticks();
876 cpu_clock_offset
= cpu_get_clock();
877 cpu_ticks_enabled
= 0;
881 /***********************************************************/
884 #define QEMU_TIMER_REALTIME 0
885 #define QEMU_TIMER_VIRTUAL 1
889 /* XXX: add frequency */
897 struct QEMUTimer
*next
;
900 struct qemu_alarm_timer
{
904 int (*start
)(struct qemu_alarm_timer
*t
);
905 void (*stop
)(struct qemu_alarm_timer
*t
);
906 void (*rearm
)(struct qemu_alarm_timer
*t
);
910 #define ALARM_FLAG_DYNTICKS 0x1
911 #define ALARM_FLAG_EXPIRED 0x2
913 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
915 return t
->flags
& ALARM_FLAG_DYNTICKS
;
918 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
920 if (!alarm_has_dynticks(t
))
926 /* TODO: MIN_TIMER_REARM_US should be optimized */
927 #define MIN_TIMER_REARM_US 250
929 static struct qemu_alarm_timer
*alarm_timer
;
931 static int alarm_timer_rfd
, alarm_timer_wfd
;
936 struct qemu_alarm_win32
{
940 } alarm_win32_data
= {0, NULL
, -1};
942 static int win32_start_timer(struct qemu_alarm_timer
*t
);
943 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
944 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
948 static int unix_start_timer(struct qemu_alarm_timer
*t
);
949 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
953 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
954 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
955 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
957 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
958 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
960 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
961 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
963 #endif /* __linux__ */
967 /* Correlation between real and virtual time is always going to be
968 fairly approximate, so ignore small variation.
969 When the guest is idle real and virtual time will be aligned in
971 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
973 static void icount_adjust(void)
978 static int64_t last_delta
;
979 /* If the VM is not running, then do nothing. */
983 cur_time
= cpu_get_clock();
984 cur_icount
= qemu_get_clock(vm_clock
);
985 delta
= cur_icount
- cur_time
;
986 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
988 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
989 && icount_time_shift
> 0) {
990 /* The guest is getting too far ahead. Slow time down. */
994 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
995 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
996 /* The guest is getting too far behind. Speed time up. */
1000 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1003 static void icount_adjust_rt(void * opaque
)
1005 qemu_mod_timer(icount_rt_timer
,
1006 qemu_get_clock(rt_clock
) + 1000);
1010 static void icount_adjust_vm(void * opaque
)
1012 qemu_mod_timer(icount_vm_timer
,
1013 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1017 static void init_icount_adjust(void)
1019 /* Have both realtime and virtual time triggers for speed adjustment.
1020 The realtime trigger catches emulated time passing too slowly,
1021 the virtual time trigger catches emulated time passing too fast.
1022 Realtime triggers occur even when idle, so use them less frequently
1023 than VM triggers. */
1024 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1025 qemu_mod_timer(icount_rt_timer
,
1026 qemu_get_clock(rt_clock
) + 1000);
1027 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1028 qemu_mod_timer(icount_vm_timer
,
1029 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1032 static struct qemu_alarm_timer alarm_timers
[] = {
1035 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1036 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1037 /* HPET - if available - is preferred */
1038 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1039 /* ...otherwise try RTC */
1040 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1042 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1044 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1045 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1046 {"win32", 0, win32_start_timer
,
1047 win32_stop_timer
, NULL
, &alarm_win32_data
},
1052 static void show_available_alarms(void)
1056 printf("Available alarm timers, in order of precedence:\n");
1057 for (i
= 0; alarm_timers
[i
].name
; i
++)
1058 printf("%s\n", alarm_timers
[i
].name
);
1061 static void configure_alarms(char const *opt
)
1065 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1068 struct qemu_alarm_timer tmp
;
1070 if (!strcmp(opt
, "?")) {
1071 show_available_alarms();
1077 /* Reorder the array */
1078 name
= strtok(arg
, ",");
1080 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1081 if (!strcmp(alarm_timers
[i
].name
, name
))
1086 fprintf(stderr
, "Unknown clock %s\n", name
);
1095 tmp
= alarm_timers
[i
];
1096 alarm_timers
[i
] = alarm_timers
[cur
];
1097 alarm_timers
[cur
] = tmp
;
1101 name
= strtok(NULL
, ",");
1107 /* Disable remaining timers */
1108 for (i
= cur
; i
< count
; i
++)
1109 alarm_timers
[i
].name
= NULL
;
1111 show_available_alarms();
1116 QEMUClock
*rt_clock
;
1117 QEMUClock
*vm_clock
;
1119 static QEMUTimer
*active_timers
[2];
1121 static QEMUClock
*qemu_new_clock(int type
)
1124 clock
= qemu_mallocz(sizeof(QEMUClock
));
1129 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1133 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1136 ts
->opaque
= opaque
;
1140 void qemu_free_timer(QEMUTimer
*ts
)
1145 /* stop a timer, but do not dealloc it */
1146 void qemu_del_timer(QEMUTimer
*ts
)
1150 /* NOTE: this code must be signal safe because
1151 qemu_timer_expired() can be called from a signal. */
1152 pt
= &active_timers
[ts
->clock
->type
];
1165 /* modify the current timer so that it will be fired when current_time
1166 >= expire_time. The corresponding callback will be called. */
1167 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1173 /* add the timer in the sorted list */
1174 /* NOTE: this code must be signal safe because
1175 qemu_timer_expired() can be called from a signal. */
1176 pt
= &active_timers
[ts
->clock
->type
];
1181 if (t
->expire_time
> expire_time
)
1185 ts
->expire_time
= expire_time
;
1189 /* Rearm if necessary */
1190 if (pt
== &active_timers
[ts
->clock
->type
]) {
1191 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1192 qemu_rearm_alarm_timer(alarm_timer
);
1194 /* Interrupt execution to force deadline recalculation. */
1195 if (use_icount
&& cpu_single_env
) {
1196 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1201 int qemu_timer_pending(QEMUTimer
*ts
)
1204 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1211 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1215 return (timer_head
->expire_time
<= current_time
);
1218 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1224 if (!ts
|| ts
->expire_time
> current_time
)
1226 /* remove timer from the list before calling the callback */
1227 *ptimer_head
= ts
->next
;
1230 /* run the callback (the timer list can be modified) */
1235 int64_t qemu_get_clock(QEMUClock
*clock
)
1237 switch(clock
->type
) {
1238 case QEMU_TIMER_REALTIME
:
1239 return get_clock() / 1000000;
1241 case QEMU_TIMER_VIRTUAL
:
1243 return cpu_get_icount();
1245 return cpu_get_clock();
1250 static void init_timers(void)
1253 ticks_per_sec
= QEMU_TIMER_BASE
;
1254 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1255 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1259 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1261 uint64_t expire_time
;
1263 if (qemu_timer_pending(ts
)) {
1264 expire_time
= ts
->expire_time
;
1268 qemu_put_be64(f
, expire_time
);
1271 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1273 uint64_t expire_time
;
1275 expire_time
= qemu_get_be64(f
);
1276 if (expire_time
!= -1) {
1277 qemu_mod_timer(ts
, expire_time
);
1283 static void timer_save(QEMUFile
*f
, void *opaque
)
1285 if (cpu_ticks_enabled
) {
1286 hw_error("cannot save state if virtual timers are running");
1288 qemu_put_be64(f
, cpu_ticks_offset
);
1289 qemu_put_be64(f
, ticks_per_sec
);
1290 qemu_put_be64(f
, cpu_clock_offset
);
1293 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1295 if (version_id
!= 1 && version_id
!= 2)
1297 if (cpu_ticks_enabled
) {
1300 cpu_ticks_offset
=qemu_get_be64(f
);
1301 ticks_per_sec
=qemu_get_be64(f
);
1302 if (version_id
== 2) {
1303 cpu_clock_offset
=qemu_get_be64(f
);
1309 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1310 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1312 static void host_alarm_handler(int host_signum
)
1316 #define DISP_FREQ 1000
1318 static int64_t delta_min
= INT64_MAX
;
1319 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1321 ti
= qemu_get_clock(vm_clock
);
1322 if (last_clock
!= 0) {
1323 delta
= ti
- last_clock
;
1324 if (delta
< delta_min
)
1326 if (delta
> delta_max
)
1329 if (++count
== DISP_FREQ
) {
1330 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1331 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1332 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1333 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1334 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1336 delta_min
= INT64_MAX
;
1345 alarm_has_dynticks(alarm_timer
) ||
1347 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1348 qemu_get_clock(vm_clock
))) ||
1349 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1350 qemu_get_clock(rt_clock
))) {
1351 CPUState
*env
= next_cpu
;
1354 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1355 SetEvent(data
->host_alarm
);
1357 static const char byte
= 0;
1358 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1360 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1363 /* stop the currently executing cpu because a timer occured */
1364 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1366 if (env
->kqemu_enabled
) {
1367 kqemu_cpu_interrupt(env
);
1375 static int64_t qemu_next_deadline(void)
1379 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1380 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1381 qemu_get_clock(vm_clock
);
1383 /* To avoid problems with overflow limit this to 2^32. */
1393 #if defined(__linux__) || defined(_WIN32)
1394 static uint64_t qemu_next_deadline_dyntick(void)
1402 delta
= (qemu_next_deadline() + 999) / 1000;
1404 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1405 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1406 qemu_get_clock(rt_clock
))*1000;
1407 if (rtdelta
< delta
)
1411 if (delta
< MIN_TIMER_REARM_US
)
1412 delta
= MIN_TIMER_REARM_US
;
1420 /* Sets a specific flag */
1421 static int fcntl_setfl(int fd
, int flag
)
1425 flags
= fcntl(fd
, F_GETFL
);
1429 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1435 #if defined(__linux__)
1437 #define RTC_FREQ 1024
1439 static void enable_sigio_timer(int fd
)
1441 struct sigaction act
;
1444 sigfillset(&act
.sa_mask
);
1446 act
.sa_handler
= host_alarm_handler
;
1448 sigaction(SIGIO
, &act
, NULL
);
1449 fcntl_setfl(fd
, O_ASYNC
);
1450 fcntl(fd
, F_SETOWN
, getpid());
1453 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1455 struct hpet_info info
;
1458 fd
= open("/dev/hpet", O_RDONLY
);
1463 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1465 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1466 "error, but for better emulation accuracy type:\n"
1467 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1471 /* Check capabilities */
1472 r
= ioctl(fd
, HPET_INFO
, &info
);
1476 /* Enable periodic mode */
1477 r
= ioctl(fd
, HPET_EPI
, 0);
1478 if (info
.hi_flags
&& (r
< 0))
1481 /* Enable interrupt */
1482 r
= ioctl(fd
, HPET_IE_ON
, 0);
1486 enable_sigio_timer(fd
);
1487 t
->priv
= (void *)(long)fd
;
1495 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1497 int fd
= (long)t
->priv
;
1502 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1505 unsigned long current_rtc_freq
= 0;
1507 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1510 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1511 if (current_rtc_freq
!= RTC_FREQ
&&
1512 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1513 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1514 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1515 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1518 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1524 enable_sigio_timer(rtc_fd
);
1526 t
->priv
= (void *)(long)rtc_fd
;
1531 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1533 int rtc_fd
= (long)t
->priv
;
1538 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1542 struct sigaction act
;
1544 sigfillset(&act
.sa_mask
);
1546 act
.sa_handler
= host_alarm_handler
;
1548 sigaction(SIGALRM
, &act
, NULL
);
1550 ev
.sigev_value
.sival_int
= 0;
1551 ev
.sigev_notify
= SIGEV_SIGNAL
;
1552 ev
.sigev_signo
= SIGALRM
;
1554 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1555 perror("timer_create");
1557 /* disable dynticks */
1558 fprintf(stderr
, "Dynamic Ticks disabled\n");
1563 t
->priv
= (void *)(long)host_timer
;
1568 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1570 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1572 timer_delete(host_timer
);
1575 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1577 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1578 struct itimerspec timeout
;
1579 int64_t nearest_delta_us
= INT64_MAX
;
1582 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1583 !active_timers
[QEMU_TIMER_VIRTUAL
])
1586 nearest_delta_us
= qemu_next_deadline_dyntick();
1588 /* check whether a timer is already running */
1589 if (timer_gettime(host_timer
, &timeout
)) {
1591 fprintf(stderr
, "Internal timer error: aborting\n");
1594 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1595 if (current_us
&& current_us
<= nearest_delta_us
)
1598 timeout
.it_interval
.tv_sec
= 0;
1599 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1600 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1601 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1602 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1604 fprintf(stderr
, "Internal timer error: aborting\n");
1609 #endif /* defined(__linux__) */
1611 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1613 struct sigaction act
;
1614 struct itimerval itv
;
1618 sigfillset(&act
.sa_mask
);
1620 act
.sa_handler
= host_alarm_handler
;
1622 sigaction(SIGALRM
, &act
, NULL
);
1624 itv
.it_interval
.tv_sec
= 0;
1625 /* for i386 kernel 2.6 to get 1 ms */
1626 itv
.it_interval
.tv_usec
= 999;
1627 itv
.it_value
.tv_sec
= 0;
1628 itv
.it_value
.tv_usec
= 10 * 1000;
1630 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1637 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1639 struct itimerval itv
;
1641 memset(&itv
, 0, sizeof(itv
));
1642 setitimer(ITIMER_REAL
, &itv
, NULL
);
1645 #endif /* !defined(_WIN32) */
1647 static void try_to_rearm_timer(void *opaque
)
1649 struct qemu_alarm_timer
*t
= opaque
;
1653 /* Drain the notify pipe */
1656 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1657 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1660 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1661 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1662 qemu_rearm_alarm_timer(alarm_timer
);
1668 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1671 struct qemu_alarm_win32
*data
= t
->priv
;
1674 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1675 if (!data
->host_alarm
) {
1676 perror("Failed CreateEvent");
1680 memset(&tc
, 0, sizeof(tc
));
1681 timeGetDevCaps(&tc
, sizeof(tc
));
1683 if (data
->period
< tc
.wPeriodMin
)
1684 data
->period
= tc
.wPeriodMin
;
1686 timeBeginPeriod(data
->period
);
1688 flags
= TIME_CALLBACK_FUNCTION
;
1689 if (alarm_has_dynticks(t
))
1690 flags
|= TIME_ONESHOT
;
1692 flags
|= TIME_PERIODIC
;
1694 data
->timerId
= timeSetEvent(1, // interval (ms)
1695 data
->period
, // resolution
1696 host_alarm_handler
, // function
1697 (DWORD
)t
, // parameter
1700 if (!data
->timerId
) {
1701 perror("Failed to initialize win32 alarm timer");
1703 timeEndPeriod(data
->period
);
1704 CloseHandle(data
->host_alarm
);
1708 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1713 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1715 struct qemu_alarm_win32
*data
= t
->priv
;
1717 timeKillEvent(data
->timerId
);
1718 timeEndPeriod(data
->period
);
1720 CloseHandle(data
->host_alarm
);
1723 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1725 struct qemu_alarm_win32
*data
= t
->priv
;
1726 uint64_t nearest_delta_us
;
1728 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1729 !active_timers
[QEMU_TIMER_VIRTUAL
])
1732 nearest_delta_us
= qemu_next_deadline_dyntick();
1733 nearest_delta_us
/= 1000;
1735 timeKillEvent(data
->timerId
);
1737 data
->timerId
= timeSetEvent(1,
1741 TIME_ONESHOT
| TIME_PERIODIC
);
1743 if (!data
->timerId
) {
1744 perror("Failed to re-arm win32 alarm timer");
1746 timeEndPeriod(data
->period
);
1747 CloseHandle(data
->host_alarm
);
1754 static int init_timer_alarm(void)
1756 struct qemu_alarm_timer
*t
= NULL
;
1766 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1770 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1774 alarm_timer_rfd
= fds
[0];
1775 alarm_timer_wfd
= fds
[1];
1778 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1779 t
= &alarm_timers
[i
];
1792 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1793 try_to_rearm_timer
, NULL
, t
);
1808 static void quit_timers(void)
1810 alarm_timer
->stop(alarm_timer
);
1814 /***********************************************************/
1815 /* host time/date access */
1816 void qemu_get_timedate(struct tm
*tm
, int offset
)
1823 if (rtc_date_offset
== -1) {
1827 ret
= localtime(&ti
);
1829 ti
-= rtc_date_offset
;
1833 memcpy(tm
, ret
, sizeof(struct tm
));
1836 int qemu_timedate_diff(struct tm
*tm
)
1840 if (rtc_date_offset
== -1)
1842 seconds
= mktimegm(tm
);
1844 seconds
= mktime(tm
);
1846 seconds
= mktimegm(tm
) + rtc_date_offset
;
1848 return seconds
- time(NULL
);
1852 static void socket_cleanup(void)
1857 static int socket_init(void)
1862 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1864 err
= WSAGetLastError();
1865 fprintf(stderr
, "WSAStartup: %d\n", err
);
1868 atexit(socket_cleanup
);
1873 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1878 while (*p
!= '\0' && *p
!= '=') {
1879 if (q
&& (q
- buf
) < buf_size
- 1)
1889 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1894 while (*p
!= '\0') {
1896 if (*(p
+ 1) != ',')
1900 if (q
&& (q
- buf
) < buf_size
- 1)
1910 int get_param_value(char *buf
, int buf_size
,
1911 const char *tag
, const char *str
)
1918 p
= get_opt_name(option
, sizeof(option
), p
);
1922 if (!strcmp(tag
, option
)) {
1923 (void)get_opt_value(buf
, buf_size
, p
);
1926 p
= get_opt_value(NULL
, 0, p
);
1935 int check_params(char *buf
, int buf_size
,
1936 const char * const *params
, const char *str
)
1943 p
= get_opt_name(buf
, buf_size
, p
);
1947 for(i
= 0; params
[i
] != NULL
; i
++)
1948 if (!strcmp(params
[i
], buf
))
1950 if (params
[i
] == NULL
)
1952 p
= get_opt_value(NULL
, 0, p
);
1960 /***********************************************************/
1961 /* Bluetooth support */
1964 static struct HCIInfo
*hci_table
[MAX_NICS
];
1966 static struct bt_vlan_s
{
1967 struct bt_scatternet_s net
;
1969 struct bt_vlan_s
*next
;
1972 /* find or alloc a new bluetooth "VLAN" */
1973 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1975 struct bt_vlan_s
**pvlan
, *vlan
;
1976 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1980 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1982 pvlan
= &first_bt_vlan
;
1983 while (*pvlan
!= NULL
)
1984 pvlan
= &(*pvlan
)->next
;
1989 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1993 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1998 static struct HCIInfo null_hci
= {
1999 .cmd_send
= null_hci_send
,
2000 .sco_send
= null_hci_send
,
2001 .acl_send
= null_hci_send
,
2002 .bdaddr_set
= null_hci_addr_set
,
2005 struct HCIInfo
*qemu_next_hci(void)
2007 if (cur_hci
== nb_hcis
)
2010 return hci_table
[cur_hci
++];
2013 static struct HCIInfo
*hci_init(const char *str
)
2016 struct bt_scatternet_s
*vlan
= 0;
2018 if (!strcmp(str
, "null"))
2021 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2023 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2024 else if (!strncmp(str
, "hci", 3)) {
2027 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2028 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2033 vlan
= qemu_find_bt_vlan(0);
2035 return bt_new_hci(vlan
);
2038 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2043 static int bt_hci_parse(const char *str
)
2045 struct HCIInfo
*hci
;
2048 if (nb_hcis
>= MAX_NICS
) {
2049 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2053 hci
= hci_init(str
);
2062 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2063 hci
->bdaddr_set(hci
, bdaddr
.b
);
2065 hci_table
[nb_hcis
++] = hci
;
2070 static void bt_vhci_add(int vlan_id
)
2072 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2075 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2076 "an empty scatternet %i\n", vlan_id
);
2078 bt_vhci_init(bt_new_hci(vlan
));
2081 static struct bt_device_s
*bt_device_add(const char *opt
)
2083 struct bt_scatternet_s
*vlan
;
2085 char *endp
= strstr(opt
, ",vlan=");
2086 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2089 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2092 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2094 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2099 vlan
= qemu_find_bt_vlan(vlan_id
);
2102 fprintf(stderr
, "qemu: warning: adding a slave device to "
2103 "an empty scatternet %i\n", vlan_id
);
2105 if (!strcmp(devname
, "keyboard"))
2106 return bt_keyboard_init(vlan
);
2108 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2112 static int bt_parse(const char *opt
)
2114 const char *endp
, *p
;
2117 if (strstart(opt
, "hci", &endp
)) {
2118 if (!*endp
|| *endp
== ',') {
2120 if (!strstart(endp
, ",vlan=", 0))
2123 return bt_hci_parse(opt
);
2125 } else if (strstart(opt
, "vhci", &endp
)) {
2126 if (!*endp
|| *endp
== ',') {
2128 if (strstart(endp
, ",vlan=", &p
)) {
2129 vlan
= strtol(p
, (char **) &endp
, 0);
2131 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2135 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2144 } else if (strstart(opt
, "device:", &endp
))
2145 return !bt_device_add(endp
);
2147 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2151 /***********************************************************/
2152 /* QEMU Block devices */
2154 #define HD_ALIAS "index=%d,media=disk"
2156 #define CDROM_ALIAS "index=1,media=cdrom"
2158 #define CDROM_ALIAS "index=2,media=cdrom"
2160 #define FD_ALIAS "index=%d,if=floppy"
2161 #define PFLASH_ALIAS "if=pflash"
2162 #define MTD_ALIAS "if=mtd"
2163 #define SD_ALIAS "index=0,if=sd"
2165 static int drive_opt_get_free_idx(void)
2169 for (index
= 0; index
< MAX_DRIVES
; index
++)
2170 if (!drives_opt
[index
].used
) {
2171 drives_opt
[index
].used
= 1;
2178 static int drive_get_free_idx(void)
2182 for (index
= 0; index
< MAX_DRIVES
; index
++)
2183 if (!drives_table
[index
].used
) {
2184 drives_table
[index
].used
= 1;
2191 int drive_add(const char *file
, const char *fmt
, ...)
2194 int index
= drive_opt_get_free_idx();
2196 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2197 fprintf(stderr
, "qemu: too many drives\n");
2201 drives_opt
[index
].file
= file
;
2203 vsnprintf(drives_opt
[index
].opt
,
2204 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2211 void drive_remove(int index
)
2213 drives_opt
[index
].used
= 0;
2217 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2221 /* seek interface, bus and unit */
2223 for (index
= 0; index
< MAX_DRIVES
; index
++)
2224 if (drives_table
[index
].type
== type
&&
2225 drives_table
[index
].bus
== bus
&&
2226 drives_table
[index
].unit
== unit
&&
2227 drives_table
[index
].used
)
2233 int drive_get_max_bus(BlockInterfaceType type
)
2239 for (index
= 0; index
< nb_drives
; index
++) {
2240 if(drives_table
[index
].type
== type
&&
2241 drives_table
[index
].bus
> max_bus
)
2242 max_bus
= drives_table
[index
].bus
;
2247 const char *drive_get_serial(BlockDriverState
*bdrv
)
2251 for (index
= 0; index
< nb_drives
; index
++)
2252 if (drives_table
[index
].bdrv
== bdrv
)
2253 return drives_table
[index
].serial
;
2258 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2262 for (index
= 0; index
< nb_drives
; index
++)
2263 if (drives_table
[index
].bdrv
== bdrv
)
2264 return drives_table
[index
].onerror
;
2266 return BLOCK_ERR_REPORT
;
2269 static void bdrv_format_print(void *opaque
, const char *name
)
2271 fprintf(stderr
, " %s", name
);
2274 void drive_uninit(BlockDriverState
*bdrv
)
2278 for (i
= 0; i
< MAX_DRIVES
; i
++)
2279 if (drives_table
[i
].bdrv
== bdrv
) {
2280 drives_table
[i
].bdrv
= NULL
;
2281 drives_table
[i
].used
= 0;
2282 drive_remove(drives_table
[i
].drive_opt_idx
);
2288 int drive_init(struct drive_opt
*arg
, int snapshot
,
2289 QEMUMachine
*machine
)
2295 const char *mediastr
= "";
2296 BlockInterfaceType type
;
2297 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2298 int bus_id
, unit_id
;
2299 int cyls
, heads
, secs
, translation
;
2300 BlockDriverState
*bdrv
;
2301 BlockDriver
*drv
= NULL
;
2305 int bdrv_flags
, onerror
;
2306 int drives_table_idx
;
2307 char *str
= arg
->opt
;
2308 static const char * const params
[] = { "bus", "unit", "if", "index",
2309 "cyls", "heads", "secs", "trans",
2310 "media", "snapshot", "file",
2311 "cache", "format", "serial", "werror",
2314 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2315 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2321 cyls
= heads
= secs
= 0;
2324 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2328 if (machine
->use_scsi
) {
2330 max_devs
= MAX_SCSI_DEVS
;
2331 pstrcpy(devname
, sizeof(devname
), "scsi");
2334 max_devs
= MAX_IDE_DEVS
;
2335 pstrcpy(devname
, sizeof(devname
), "ide");
2339 /* extract parameters */
2341 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2342 bus_id
= strtol(buf
, NULL
, 0);
2344 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2349 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2350 unit_id
= strtol(buf
, NULL
, 0);
2352 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2357 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2358 pstrcpy(devname
, sizeof(devname
), buf
);
2359 if (!strcmp(buf
, "ide")) {
2361 max_devs
= MAX_IDE_DEVS
;
2362 } else if (!strcmp(buf
, "scsi")) {
2364 max_devs
= MAX_SCSI_DEVS
;
2365 } else if (!strcmp(buf
, "floppy")) {
2368 } else if (!strcmp(buf
, "pflash")) {
2371 } else if (!strcmp(buf
, "mtd")) {
2374 } else if (!strcmp(buf
, "sd")) {
2377 } else if (!strcmp(buf
, "virtio")) {
2381 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2386 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2387 index
= strtol(buf
, NULL
, 0);
2389 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2394 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2395 cyls
= strtol(buf
, NULL
, 0);
2398 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2399 heads
= strtol(buf
, NULL
, 0);
2402 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2403 secs
= strtol(buf
, NULL
, 0);
2406 if (cyls
|| heads
|| secs
) {
2407 if (cyls
< 1 || cyls
> 16383) {
2408 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2411 if (heads
< 1 || heads
> 16) {
2412 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2415 if (secs
< 1 || secs
> 63) {
2416 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2421 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2424 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2428 if (!strcmp(buf
, "none"))
2429 translation
= BIOS_ATA_TRANSLATION_NONE
;
2430 else if (!strcmp(buf
, "lba"))
2431 translation
= BIOS_ATA_TRANSLATION_LBA
;
2432 else if (!strcmp(buf
, "auto"))
2433 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2435 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2440 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2441 if (!strcmp(buf
, "disk")) {
2443 } else if (!strcmp(buf
, "cdrom")) {
2444 if (cyls
|| secs
|| heads
) {
2446 "qemu: '%s' invalid physical CHS format\n", str
);
2449 media
= MEDIA_CDROM
;
2451 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2456 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2457 if (!strcmp(buf
, "on"))
2459 else if (!strcmp(buf
, "off"))
2462 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2467 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2468 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2470 else if (!strcmp(buf
, "writethrough"))
2472 else if (!strcmp(buf
, "writeback"))
2475 fprintf(stderr
, "qemu: invalid cache option\n");
2480 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2481 if (strcmp(buf
, "?") == 0) {
2482 fprintf(stderr
, "qemu: Supported formats:");
2483 bdrv_iterate_format(bdrv_format_print
, NULL
);
2484 fprintf(stderr
, "\n");
2487 drv
= bdrv_find_format(buf
);
2489 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2494 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2495 if (!strcmp(buf
, "on")) {
2496 if (extboot_drive
!= -1) {
2497 fprintf(stderr
, "qemu: two bootable drives specified\n");
2500 extboot_drive
= nb_drives
;
2501 } else if (strcmp(buf
, "off")) {
2502 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2507 if (arg
->file
== NULL
)
2508 get_param_value(file
, sizeof(file
), "file", str
);
2510 pstrcpy(file
, sizeof(file
), arg
->file
);
2512 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2513 memset(serial
, 0, sizeof(serial
));
2515 onerror
= BLOCK_ERR_REPORT
;
2516 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2517 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2518 fprintf(stderr
, "werror is no supported by this format\n");
2521 if (!strcmp(buf
, "ignore"))
2522 onerror
= BLOCK_ERR_IGNORE
;
2523 else if (!strcmp(buf
, "enospc"))
2524 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2525 else if (!strcmp(buf
, "stop"))
2526 onerror
= BLOCK_ERR_STOP_ANY
;
2527 else if (!strcmp(buf
, "report"))
2528 onerror
= BLOCK_ERR_REPORT
;
2530 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2535 /* compute bus and unit according index */
2538 if (bus_id
!= 0 || unit_id
!= -1) {
2540 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2548 unit_id
= index
% max_devs
;
2549 bus_id
= index
/ max_devs
;
2553 /* if user doesn't specify a unit_id,
2554 * try to find the first free
2557 if (unit_id
== -1) {
2559 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2561 if (max_devs
&& unit_id
>= max_devs
) {
2562 unit_id
-= max_devs
;
2570 if (max_devs
&& unit_id
>= max_devs
) {
2571 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2572 str
, unit_id
, max_devs
- 1);
2577 * ignore multiple definitions
2580 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2585 if (type
== IF_IDE
|| type
== IF_SCSI
)
2586 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2588 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2589 devname
, bus_id
, mediastr
, unit_id
);
2591 snprintf(buf
, sizeof(buf
), "%s%s%i",
2592 devname
, mediastr
, unit_id
);
2593 bdrv
= bdrv_new(buf
);
2594 drives_table_idx
= drive_get_free_idx();
2595 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2596 drives_table
[drives_table_idx
].type
= type
;
2597 drives_table
[drives_table_idx
].bus
= bus_id
;
2598 drives_table
[drives_table_idx
].unit
= unit_id
;
2599 drives_table
[drives_table_idx
].onerror
= onerror
;
2600 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2601 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2610 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2611 bdrv_set_translation_hint(bdrv
, translation
);
2615 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2620 /* FIXME: This isn't really a floppy, but it's a reasonable
2623 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2634 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2635 cache
= 2; /* always use write-back with snapshot */
2637 if (cache
== 0) /* no caching */
2638 bdrv_flags
|= BDRV_O_NOCACHE
;
2639 else if (cache
== 2) /* write-back */
2640 bdrv_flags
|= BDRV_O_CACHE_WB
;
2641 else if (cache
== 3) /* not specified */
2642 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2643 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0 || qemu_key_check(bdrv
, file
)) {
2644 fprintf(stderr
, "qemu: could not open disk image %s\n",
2648 return drives_table_idx
;
2651 /***********************************************************/
2654 static USBPort
*used_usb_ports
;
2655 static USBPort
*free_usb_ports
;
2657 /* ??? Maybe change this to register a hub to keep track of the topology. */
2658 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2659 usb_attachfn attach
)
2661 port
->opaque
= opaque
;
2662 port
->index
= index
;
2663 port
->attach
= attach
;
2664 port
->next
= free_usb_ports
;
2665 free_usb_ports
= port
;
2668 int usb_device_add_dev(USBDevice
*dev
)
2672 /* Find a USB port to add the device to. */
2673 port
= free_usb_ports
;
2677 /* Create a new hub and chain it on. */
2678 free_usb_ports
= NULL
;
2679 port
->next
= used_usb_ports
;
2680 used_usb_ports
= port
;
2682 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2683 usb_attach(port
, hub
);
2684 port
= free_usb_ports
;
2687 free_usb_ports
= port
->next
;
2688 port
->next
= used_usb_ports
;
2689 used_usb_ports
= port
;
2690 usb_attach(port
, dev
);
2694 static int usb_device_add(const char *devname
)
2699 if (!free_usb_ports
)
2702 if (strstart(devname
, "host:", &p
)) {
2703 dev
= usb_host_device_open(p
);
2704 } else if (!strcmp(devname
, "mouse")) {
2705 dev
= usb_mouse_init();
2706 } else if (!strcmp(devname
, "tablet")) {
2707 dev
= usb_tablet_init();
2708 } else if (!strcmp(devname
, "keyboard")) {
2709 dev
= usb_keyboard_init();
2710 } else if (strstart(devname
, "disk:", &p
)) {
2711 dev
= usb_msd_init(p
);
2712 } else if (!strcmp(devname
, "wacom-tablet")) {
2713 dev
= usb_wacom_init();
2714 } else if (strstart(devname
, "serial:", &p
)) {
2715 dev
= usb_serial_init(p
);
2716 #ifdef CONFIG_BRLAPI
2717 } else if (!strcmp(devname
, "braille")) {
2718 dev
= usb_baum_init();
2720 } else if (strstart(devname
, "net:", &p
)) {
2723 if (net_client_init("nic", p
) < 0)
2725 nd_table
[nic
].model
= "usb";
2726 dev
= usb_net_init(&nd_table
[nic
]);
2727 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2728 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2729 bt_new_hci(qemu_find_bt_vlan(0)));
2736 return usb_device_add_dev(dev
);
2739 int usb_device_del_addr(int bus_num
, int addr
)
2745 if (!used_usb_ports
)
2751 lastp
= &used_usb_ports
;
2752 port
= used_usb_ports
;
2753 while (port
&& port
->dev
->addr
!= addr
) {
2754 lastp
= &port
->next
;
2762 *lastp
= port
->next
;
2763 usb_attach(port
, NULL
);
2764 dev
->handle_destroy(dev
);
2765 port
->next
= free_usb_ports
;
2766 free_usb_ports
= port
;
2770 static int usb_device_del(const char *devname
)
2775 if (strstart(devname
, "host:", &p
))
2776 return usb_host_device_close(p
);
2778 if (!used_usb_ports
)
2781 p
= strchr(devname
, '.');
2784 bus_num
= strtoul(devname
, NULL
, 0);
2785 addr
= strtoul(p
+ 1, NULL
, 0);
2787 return usb_device_del_addr(bus_num
, addr
);
2790 void do_usb_add(const char *devname
)
2792 usb_device_add(devname
);
2795 void do_usb_del(const char *devname
)
2797 usb_device_del(devname
);
2804 const char *speed_str
;
2807 term_printf("USB support not enabled\n");
2811 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2815 switch(dev
->speed
) {
2819 case USB_SPEED_FULL
:
2822 case USB_SPEED_HIGH
:
2829 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2830 0, dev
->addr
, speed_str
, dev
->devname
);
2834 /***********************************************************/
2835 /* PCMCIA/Cardbus */
2837 static struct pcmcia_socket_entry_s
{
2838 struct pcmcia_socket_s
*socket
;
2839 struct pcmcia_socket_entry_s
*next
;
2840 } *pcmcia_sockets
= 0;
2842 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2844 struct pcmcia_socket_entry_s
*entry
;
2846 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2847 entry
->socket
= socket
;
2848 entry
->next
= pcmcia_sockets
;
2849 pcmcia_sockets
= entry
;
2852 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2854 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2856 ptr
= &pcmcia_sockets
;
2857 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2858 if (entry
->socket
== socket
) {
2864 void pcmcia_info(void)
2866 struct pcmcia_socket_entry_s
*iter
;
2867 if (!pcmcia_sockets
)
2868 term_printf("No PCMCIA sockets\n");
2870 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2871 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2872 iter
->socket
->attached
? iter
->socket
->card_string
:
2876 /***********************************************************/
2877 /* register display */
2879 void register_displaystate(DisplayState
*ds
)
2889 DisplayState
*get_displaystate(void)
2891 return display_state
;
2896 static void dumb_display_init(void)
2898 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2899 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2900 register_displaystate(ds
);
2903 /***********************************************************/
2906 #define MAX_IO_HANDLERS 64
2908 typedef struct IOHandlerRecord
{
2910 IOCanRWHandler
*fd_read_poll
;
2912 IOHandler
*fd_write
;
2915 /* temporary data */
2917 struct IOHandlerRecord
*next
;
2920 static IOHandlerRecord
*first_io_handler
;
2922 /* XXX: fd_read_poll should be suppressed, but an API change is
2923 necessary in the character devices to suppress fd_can_read(). */
2924 int qemu_set_fd_handler2(int fd
,
2925 IOCanRWHandler
*fd_read_poll
,
2927 IOHandler
*fd_write
,
2930 IOHandlerRecord
**pioh
, *ioh
;
2932 if (!fd_read
&& !fd_write
) {
2933 pioh
= &first_io_handler
;
2938 if (ioh
->fd
== fd
) {
2945 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2949 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2950 ioh
->next
= first_io_handler
;
2951 first_io_handler
= ioh
;
2954 ioh
->fd_read_poll
= fd_read_poll
;
2955 ioh
->fd_read
= fd_read
;
2956 ioh
->fd_write
= fd_write
;
2957 ioh
->opaque
= opaque
;
2964 int qemu_set_fd_handler(int fd
,
2966 IOHandler
*fd_write
,
2969 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2973 /***********************************************************/
2974 /* Polling handling */
2976 typedef struct PollingEntry
{
2979 struct PollingEntry
*next
;
2982 static PollingEntry
*first_polling_entry
;
2984 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2986 PollingEntry
**ppe
, *pe
;
2987 pe
= qemu_mallocz(sizeof(PollingEntry
));
2989 pe
->opaque
= opaque
;
2990 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2995 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2997 PollingEntry
**ppe
, *pe
;
2998 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3000 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3008 /***********************************************************/
3009 /* Wait objects support */
3010 typedef struct WaitObjects
{
3012 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3013 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3014 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3017 static WaitObjects wait_objects
= {0};
3019 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3021 WaitObjects
*w
= &wait_objects
;
3023 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3025 w
->events
[w
->num
] = handle
;
3026 w
->func
[w
->num
] = func
;
3027 w
->opaque
[w
->num
] = opaque
;
3032 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3035 WaitObjects
*w
= &wait_objects
;
3038 for (i
= 0; i
< w
->num
; i
++) {
3039 if (w
->events
[i
] == handle
)
3042 w
->events
[i
] = w
->events
[i
+ 1];
3043 w
->func
[i
] = w
->func
[i
+ 1];
3044 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3052 /***********************************************************/
3053 /* ram save/restore */
3055 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3059 v
= qemu_get_byte(f
);
3062 if (qemu_get_buffer(f
, buf
, len
) != len
)
3066 v
= qemu_get_byte(f
);
3067 memset(buf
, v
, len
);
3073 if (qemu_file_has_error(f
))
3079 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3084 if (qemu_get_be32(f
) != phys_ram_size
)
3086 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3087 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3089 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3096 #define BDRV_HASH_BLOCK_SIZE 1024
3097 #define IOBUF_SIZE 4096
3098 #define RAM_CBLOCK_MAGIC 0xfabe
3100 typedef struct RamDecompressState
{
3103 uint8_t buf
[IOBUF_SIZE
];
3104 } RamDecompressState
;
3106 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3109 memset(s
, 0, sizeof(*s
));
3111 ret
= inflateInit(&s
->zstream
);
3117 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3121 s
->zstream
.avail_out
= len
;
3122 s
->zstream
.next_out
= buf
;
3123 while (s
->zstream
.avail_out
> 0) {
3124 if (s
->zstream
.avail_in
== 0) {
3125 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3127 clen
= qemu_get_be16(s
->f
);
3128 if (clen
> IOBUF_SIZE
)
3130 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3131 s
->zstream
.avail_in
= clen
;
3132 s
->zstream
.next_in
= s
->buf
;
3134 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3135 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3142 static void ram_decompress_close(RamDecompressState
*s
)
3144 inflateEnd(&s
->zstream
);
3147 #define RAM_SAVE_FLAG_FULL 0x01
3148 #define RAM_SAVE_FLAG_COMPRESS 0x02
3149 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3150 #define RAM_SAVE_FLAG_PAGE 0x08
3151 #define RAM_SAVE_FLAG_EOS 0x10
3153 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3155 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3156 uint32_t *array
= (uint32_t *)page
;
3159 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3160 if (array
[i
] != val
)
3167 static int ram_save_block(QEMUFile
*f
)
3169 static ram_addr_t current_addr
= 0;
3170 ram_addr_t saved_addr
= current_addr
;
3171 ram_addr_t addr
= 0;
3174 while (addr
< phys_ram_size
) {
3175 if (kvm_enabled() && current_addr
== 0)
3176 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3177 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3180 cpu_physical_memory_reset_dirty(current_addr
,
3181 current_addr
+ TARGET_PAGE_SIZE
,
3182 MIGRATION_DIRTY_FLAG
);
3184 ch
= *(phys_ram_base
+ current_addr
);
3186 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3187 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3188 qemu_put_byte(f
, ch
);
3190 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3191 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3197 addr
+= TARGET_PAGE_SIZE
;
3198 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3204 static ram_addr_t ram_save_threshold
= 10;
3206 static ram_addr_t
ram_save_remaining(void)
3209 ram_addr_t count
= 0;
3211 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3212 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3219 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3224 /* Make sure all dirty bits are set */
3225 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3226 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3227 cpu_physical_memory_set_dirty(addr
);
3230 /* Enable dirty memory tracking */
3231 cpu_physical_memory_set_dirty_tracking(1);
3233 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3236 while (!qemu_file_rate_limit(f
)) {
3239 ret
= ram_save_block(f
);
3240 if (ret
== 0) /* no more blocks */
3244 /* try transferring iterative blocks of memory */
3247 cpu_physical_memory_set_dirty_tracking(0);
3249 /* flush all remaining blocks regardless of rate limiting */
3250 while (ram_save_block(f
) != 0);
3253 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3255 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3258 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3260 RamDecompressState s1
, *s
= &s1
;
3264 if (ram_decompress_open(s
, f
) < 0)
3266 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3267 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3269 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3270 fprintf(stderr
, "Error while reading ram block header\n");
3274 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3275 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3280 printf("Error block header\n");
3284 ram_decompress_close(s
);
3289 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3294 if (version_id
== 1)
3295 return ram_load_v1(f
, opaque
);
3297 if (version_id
== 2) {
3298 if (qemu_get_be32(f
) != phys_ram_size
)
3300 return ram_load_dead(f
, opaque
);
3303 if (version_id
!= 3)
3307 addr
= qemu_get_be64(f
);
3309 flags
= addr
& ~TARGET_PAGE_MASK
;
3310 addr
&= TARGET_PAGE_MASK
;
3312 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3313 if (addr
!= phys_ram_size
)
3317 if (flags
& RAM_SAVE_FLAG_FULL
) {
3318 if (ram_load_dead(f
, opaque
) < 0)
3322 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3323 uint8_t ch
= qemu_get_byte(f
);
3324 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3325 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3326 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3327 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3332 /***********************************************************/
3333 /* bottom halves (can be seen as timers which expire ASAP) */
3344 static QEMUBH
*first_bh
= NULL
;
3346 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3349 bh
= qemu_mallocz(sizeof(QEMUBH
));
3351 bh
->opaque
= opaque
;
3352 bh
->next
= first_bh
;
3357 int qemu_bh_poll(void)
3363 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3364 if (!bh
->deleted
&& bh
->scheduled
) {
3373 /* remove deleted bhs */
3387 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3395 void qemu_bh_schedule(QEMUBH
*bh
)
3397 CPUState
*env
= cpu_single_env
;
3402 /* stop the currently executing CPU to execute the BH ASAP */
3404 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3409 void qemu_bh_cancel(QEMUBH
*bh
)
3414 void qemu_bh_delete(QEMUBH
*bh
)
3420 static void qemu_bh_update_timeout(int *timeout
)
3424 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3425 if (!bh
->deleted
&& bh
->scheduled
) {
3427 /* idle bottom halves will be polled at least
3429 *timeout
= MIN(10, *timeout
);
3431 /* non-idle bottom halves will be executed
3440 /***********************************************************/
3441 /* machine registration */
3443 static QEMUMachine
*first_machine
= NULL
;
3444 QEMUMachine
*current_machine
= NULL
;
3446 int qemu_register_machine(QEMUMachine
*m
)
3449 pm
= &first_machine
;
3457 static QEMUMachine
*find_machine(const char *name
)
3461 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3462 if (!strcmp(m
->name
, name
))
3468 /***********************************************************/
3469 /* main execution loop */
3471 static void gui_update(void *opaque
)
3473 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3474 DisplayState
*ds
= opaque
;
3475 DisplayChangeListener
*dcl
= ds
->listeners
;
3479 while (dcl
!= NULL
) {
3480 if (dcl
->gui_timer_interval
&&
3481 dcl
->gui_timer_interval
< interval
)
3482 interval
= dcl
->gui_timer_interval
;
3485 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3488 static void nographic_update(void *opaque
)
3490 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3492 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3495 struct vm_change_state_entry
{
3496 VMChangeStateHandler
*cb
;
3498 LIST_ENTRY (vm_change_state_entry
) entries
;
3501 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3503 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3506 VMChangeStateEntry
*e
;
3508 e
= qemu_mallocz(sizeof (*e
));
3512 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3516 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3518 LIST_REMOVE (e
, entries
);
3522 static void vm_state_notify(int running
, int reason
)
3524 VMChangeStateEntry
*e
;
3526 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3527 e
->cb(e
->opaque
, running
, reason
);
3536 vm_state_notify(1, 0);
3537 qemu_rearm_alarm_timer(alarm_timer
);
3541 void vm_stop(int reason
)
3544 cpu_disable_ticks();
3546 vm_state_notify(0, reason
);
3550 /* reset/shutdown handler */
3552 typedef struct QEMUResetEntry
{
3553 QEMUResetHandler
*func
;
3555 struct QEMUResetEntry
*next
;
3558 static QEMUResetEntry
*first_reset_entry
;
3559 static int reset_requested
;
3560 static int shutdown_requested
;
3561 static int powerdown_requested
;
3563 int qemu_shutdown_requested(void)
3565 int r
= shutdown_requested
;
3566 shutdown_requested
= 0;
3570 int qemu_reset_requested(void)
3572 int r
= reset_requested
;
3573 reset_requested
= 0;
3577 int qemu_powerdown_requested(void)
3579 int r
= powerdown_requested
;
3580 powerdown_requested
= 0;
3584 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3586 QEMUResetEntry
**pre
, *re
;
3588 pre
= &first_reset_entry
;
3589 while (*pre
!= NULL
)
3590 pre
= &(*pre
)->next
;
3591 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3593 re
->opaque
= opaque
;
3598 void qemu_system_reset(void)
3602 /* reset all devices */
3603 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3604 re
->func(re
->opaque
);
3608 void qemu_system_reset_request(void)
3611 shutdown_requested
= 1;
3613 reset_requested
= 1;
3616 if (cpu_single_env
) {
3617 qemu_kvm_cpu_stop(cpu_single_env
);
3618 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3623 void qemu_system_shutdown_request(void)
3625 shutdown_requested
= 1;
3627 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3630 void qemu_system_powerdown_request(void)
3632 powerdown_requested
= 1;
3634 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3637 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3642 /* KVM holds a mutex while QEMU code is running, we need hooks to
3643 release the mutex whenever QEMU code sleeps. */
3647 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3655 static void host_main_loop_wait(int *timeout
)
3661 /* XXX: need to suppress polling by better using win32 events */
3663 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3664 ret
|= pe
->func(pe
->opaque
);
3668 WaitObjects
*w
= &wait_objects
;
3670 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3671 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3672 if (w
->func
[ret
- WAIT_OBJECT_0
])
3673 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3675 /* Check for additional signaled events */
3676 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3678 /* Check if event is signaled */
3679 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3680 if(ret2
== WAIT_OBJECT_0
) {
3682 w
->func
[i
](w
->opaque
[i
]);
3683 } else if (ret2
== WAIT_TIMEOUT
) {
3685 err
= GetLastError();
3686 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3689 } else if (ret
== WAIT_TIMEOUT
) {
3691 err
= GetLastError();
3692 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3699 static void host_main_loop_wait(int *timeout
)
3704 void main_loop_wait(int timeout
)
3706 IOHandlerRecord
*ioh
;
3707 fd_set rfds
, wfds
, xfds
;
3711 qemu_bh_update_timeout(&timeout
);
3713 host_main_loop_wait(&timeout
);
3715 /* poll any events */
3716 /* XXX: separate device handlers from system ones */
3721 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3725 (!ioh
->fd_read_poll
||
3726 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3727 FD_SET(ioh
->fd
, &rfds
);
3731 if (ioh
->fd_write
) {
3732 FD_SET(ioh
->fd
, &wfds
);
3738 tv
.tv_sec
= timeout
/ 1000;
3739 tv
.tv_usec
= (timeout
% 1000) * 1000;
3741 #if defined(CONFIG_SLIRP)
3742 if (slirp_is_inited()) {
3743 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3746 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3748 IOHandlerRecord
**pioh
;
3750 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3751 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3752 ioh
->fd_read(ioh
->opaque
);
3753 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3754 FD_CLR(ioh
->fd
, &rfds
);
3756 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3757 ioh
->fd_write(ioh
->opaque
);
3761 /* remove deleted IO handlers */
3762 pioh
= &first_io_handler
;
3772 #if defined(CONFIG_SLIRP)
3773 if (slirp_is_inited()) {
3779 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3783 /* vm time timers */
3784 if (vm_running
&& (!cur_cpu
3785 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3786 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3787 qemu_get_clock(vm_clock
));
3789 /* real time timers */
3790 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3791 qemu_get_clock(rt_clock
));
3793 /* Check bottom-halves last in case any of the earlier events triggered
3799 static int main_loop(void)
3802 #ifdef CONFIG_PROFILER
3808 if (kvm_enabled()) {
3810 cpu_disable_ticks();
3814 cur_cpu
= first_cpu
;
3815 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3822 #ifdef CONFIG_PROFILER
3823 ti
= profile_getclock();
3828 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3829 env
->icount_decr
.u16
.low
= 0;
3830 env
->icount_extra
= 0;
3831 count
= qemu_next_deadline();
3832 count
= (count
+ (1 << icount_time_shift
) - 1)
3833 >> icount_time_shift
;
3834 qemu_icount
+= count
;
3835 decr
= (count
> 0xffff) ? 0xffff : count
;
3837 env
->icount_decr
.u16
.low
= decr
;
3838 env
->icount_extra
= count
;
3840 ret
= cpu_exec(env
);
3841 #ifdef CONFIG_PROFILER
3842 qemu_time
+= profile_getclock() - ti
;
3845 /* Fold pending instructions back into the
3846 instruction counter, and clear the interrupt flag. */
3847 qemu_icount
-= (env
->icount_decr
.u16
.low
3848 + env
->icount_extra
);
3849 env
->icount_decr
.u32
= 0;
3850 env
->icount_extra
= 0;
3852 next_cpu
= env
->next_cpu
?: first_cpu
;
3853 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3854 ret
= EXCP_INTERRUPT
;
3858 if (ret
== EXCP_HLT
) {
3859 /* Give the next CPU a chance to run. */
3863 if (ret
!= EXCP_HALTED
)
3865 /* all CPUs are halted ? */
3871 if (shutdown_requested
) {
3872 ret
= EXCP_INTERRUPT
;
3880 if (reset_requested
) {
3881 reset_requested
= 0;
3882 qemu_system_reset();
3884 kvm_load_registers(env
);
3885 ret
= EXCP_INTERRUPT
;
3887 if (powerdown_requested
) {
3888 powerdown_requested
= 0;
3889 qemu_system_powerdown();
3890 ret
= EXCP_INTERRUPT
;
3892 #ifdef CONFIG_GDBSTUB
3893 if (unlikely(ret
== EXCP_DEBUG
)) {
3894 gdb_set_stop_cpu(cur_cpu
);
3895 vm_stop(EXCP_DEBUG
);
3898 /* If all cpus are halted then wait until the next IRQ */
3899 /* XXX: use timeout computed from timers */
3900 if (ret
== EXCP_HALTED
) {
3904 /* Advance virtual time to the next event. */
3905 if (use_icount
== 1) {
3906 /* When not using an adaptive execution frequency
3907 we tend to get badly out of sync with real time,
3908 so just delay for a reasonable amount of time. */
3911 delta
= cpu_get_icount() - cpu_get_clock();
3914 /* If virtual time is ahead of real time then just
3916 timeout
= (delta
/ 1000000) + 1;
3918 /* Wait for either IO to occur or the next
3920 add
= qemu_next_deadline();
3921 /* We advance the timer before checking for IO.
3922 Limit the amount we advance so that early IO
3923 activity won't get the guest too far ahead. */
3927 add
= (add
+ (1 << icount_time_shift
) - 1)
3928 >> icount_time_shift
;
3930 timeout
= delta
/ 1000000;
3941 if (shutdown_requested
) {
3942 ret
= EXCP_INTERRUPT
;
3947 #ifdef CONFIG_PROFILER
3948 ti
= profile_getclock();
3950 main_loop_wait(timeout
);
3951 #ifdef CONFIG_PROFILER
3952 dev_time
+= profile_getclock() - ti
;
3955 cpu_disable_ticks();
3959 static void help(int exitcode
)
3961 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3962 and qemu-doc.texi */
3963 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3964 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3965 "usage: %s [options] [disk_image]\n"
3967 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3969 "Standard options:\n"
3970 "-h or -help display this help and exit\n"
3971 "-M machine select emulated machine (-M ? for list)\n"
3972 "-cpu cpu select CPU (-cpu ? for list)\n"
3973 "-smp n set the number of CPUs to 'n' [default=1]\n"
3974 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3975 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3976 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3977 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3978 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3979 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3980 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3982 " use 'file' as a drive image\n"
3983 "-mtdblock file use 'file' as on-board Flash memory image\n"
3984 "-sd file use 'file' as SecureDigital card image\n"
3985 "-pflash file use 'file' as a parallel flash image\n"
3986 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3987 "-snapshot write to temporary files instead of disk image files\n"
3988 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3990 "-k language use keyboard layout (for example \"fr\" for French)\n"
3993 "-audio-help print list of audio drivers and their options\n"
3994 "-soundhw c1,... enable audio support\n"
3995 " and only specified sound cards (comma separated list)\n"
3996 " use -soundhw ? to get the list of supported cards\n"
3997 " use -soundhw all to enable all of them\n"
3999 "-usb enable the USB driver (will be the default soon)\n"
4000 "-usbdevice name add the host or guest USB device 'name'\n"
4001 "-name string set the name of the guest\n"
4002 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
4003 " specify machine UUID\n"
4005 "Display options:\n"
4006 "-nographic disable graphical output and redirect serial I/Os to console\n"
4007 #ifdef CONFIG_CURSES
4008 "-curses use a curses/ncurses interface instead of SDL\n"
4011 "-no-frame open SDL window without a frame and window decorations\n"
4012 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4013 "-no-quit disable SDL window close capability\n"
4016 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4017 "-vga [std|cirrus|vmware|none]\n"
4018 " select video card type\n"
4019 "-full-screen start in full screen\n"
4020 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4021 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4023 "-vnc display start a VNC server on display\n"
4025 "-nvram file use 'file' to save or load nvram image\n"
4027 "-name string set the name of the guest\n"
4028 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4030 "Network options:\n"
4031 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4032 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4034 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4035 " connect the user mode network stack to VLAN 'n' and send\n"
4036 " hostname 'host' to DHCP clients\n"
4039 "-net tap[,vlan=n][,name=str],ifname=name\n"
4040 " connect the host TAP network interface to VLAN 'n'\n"
4042 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4043 " connect the host TAP network interface to VLAN 'n' and use the\n"
4044 " network scripts 'file' (default=%s)\n"
4045 " and 'dfile' (default=%s);\n"
4046 " use '[down]script=no' to disable script execution;\n"
4047 " use 'fd=h' to connect to an already opened TAP interface\n"
4049 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4050 " connect the vlan 'n' to another VLAN using a socket connection\n"
4051 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4052 " connect the vlan 'n' to multicast maddr and port\n"
4054 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4055 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4056 " on host and listening for incoming connections on 'socketpath'.\n"
4057 " Use group 'groupname' and mode 'octalmode' to change default\n"
4058 " ownership and permissions for communication port.\n"
4060 "-net none use it alone to have zero network devices; if no -net option\n"
4061 " is provided, the default is '-net nic -net user'\n"
4063 "-tftp dir allow tftp access to files in dir [-net user]\n"
4064 "-bootp file advertise file in BOOTP replies\n"
4066 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4068 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4069 " redirect TCP or UDP connections from host to guest [-net user]\n"
4072 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4073 "-bt hci,host[:id]\n"
4074 " use host's HCI with the given name\n"
4075 "-bt hci[,vlan=n]\n"
4076 " emulate a standard HCI in virtual scatternet 'n'\n"
4077 "-bt vhci[,vlan=n]\n"
4078 " add host computer to virtual scatternet 'n' using VHCI\n"
4079 "-bt device:dev[,vlan=n]\n"
4080 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4084 "i386 target only:\n"
4085 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4086 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4087 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4088 "-no-acpi disable ACPI\n"
4089 "-no-hpet disable HPET\n"
4091 "Linux boot specific:\n"
4092 "-kernel bzImage use 'bzImage' as kernel image\n"
4093 "-append cmdline use 'cmdline' as kernel command line\n"
4094 "-initrd file use 'file' as initial ram disk\n"
4096 "Debug/Expert options:\n"
4097 "-serial dev redirect the serial port to char device 'dev'\n"
4098 "-parallel dev redirect the parallel port to char device 'dev'\n"
4099 "-monitor dev redirect the monitor to char device 'dev'\n"
4100 "-pidfile file write PID to 'file'\n"
4101 "-S freeze CPU at startup (use 'c' to start execution)\n"
4102 "-s wait gdb connection to port\n"
4103 "-p port set gdb connection port [default=%s]\n"
4104 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4105 "-hdachs c,h,s[,t]\n"
4106 " force hard disk 0 physical geometry and the optional BIOS\n"
4107 " translation (t=none or lba) (usually qemu can guess them)\n"
4108 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4109 "-bios file set the filename for the BIOS\n"
4111 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4112 "-no-kqemu disable KQEMU kernel module usage\n"
4115 "-enable-kvm enable KVM full virtualization support\n"
4118 #ifndef NO_CPU_EMULATION
4119 "-no-kvm disable KVM hardware virtualization\n"
4121 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4122 "-no-kvm-pit disable KVM kernel mode PIT\n"
4123 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4124 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4125 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4126 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4127 " expose a PCI device to the guest OS.\n"
4128 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4129 " 'string' is used in log output.\n"
4132 "-no-reboot exit instead of rebooting\n"
4133 "-no-shutdown stop before shutdown\n"
4134 "-loadvm [tag|id]\n"
4135 " start right away with a saved state (loadvm in monitor)\n"
4137 "-daemonize daemonize QEMU after initializing\n"
4139 "-tdf inject timer interrupts that got lost\n"
4140 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4141 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also\n"
4142 " enables allocation of guest memory with huge pages\n"
4144 "-mem-prealloc toggles preallocation of -mem-path backed physical memory\n"
4145 " at startup. Default is enabled.\n"
4147 "-option-rom rom load a file, rom, into the option ROM space\n"
4148 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4149 "-prom-env variable=value\n"
4150 " set OpenBIOS nvram variables\n"
4152 "-clock force the use of the given methods for timer alarm.\n"
4153 " To see what timers are available use -clock ?\n"
4154 "-localtime set the real time clock to local time [default=utc]\n"
4155 "-startdate select initial date of the clock\n"
4156 "-icount [N|auto]\n"
4157 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4158 "-echr chr set terminal escape character instead of ctrl-a\n"
4159 "-virtioconsole c\n"
4160 " set virtio console\n"
4161 "-show-cursor show cursor\n"
4162 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4163 "-semihosting semihosting mode\n"
4165 #if defined(TARGET_ARM)
4166 "-old-param old param mode\n"
4168 "-tb-size n set TB size\n"
4169 "-incoming p prepare for incoming migration, listen on port p\n"
4171 "During emulation, the following keys are useful:\n"
4172 "ctrl-alt-f toggle full screen\n"
4173 "ctrl-alt-n switch to virtual console 'n'\n"
4174 "ctrl-alt toggle mouse and keyboard grab\n"
4176 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4181 DEFAULT_NETWORK_SCRIPT
,
4182 DEFAULT_NETWORK_DOWN_SCRIPT
,
4184 DEFAULT_GDBSTUB_PORT
,
4189 #define HAS_ARG 0x0001
4192 /* Please keep in synch with help, qemu_options[] and
4194 /* Standard options: */
4207 QEMU_OPTION_mtdblock
,
4211 QEMU_OPTION_snapshot
,
4214 QEMU_OPTION_audio_help
,
4215 QEMU_OPTION_soundhw
,
4217 QEMU_OPTION_usbdevice
,
4221 /* Display options: */
4222 QEMU_OPTION_nographic
,
4224 QEMU_OPTION_no_frame
,
4225 QEMU_OPTION_alt_grab
,
4226 QEMU_OPTION_no_quit
,
4228 QEMU_OPTION_portrait
,
4230 QEMU_OPTION_full_screen
,
4234 /* Network options: */
4242 /* i386 target only: */
4243 QEMU_OPTION_win2k_hack
,
4244 QEMU_OPTION_rtc_td_hack
,
4245 QEMU_OPTION_no_fd_bootchk
,
4246 QEMU_OPTION_no_acpi
,
4247 QEMU_OPTION_no_hpet
,
4249 /* Linux boot specific: */
4254 /* Debug/Expert options: */
4256 QEMU_OPTION_parallel
,
4257 QEMU_OPTION_monitor
,
4258 QEMU_OPTION_pidfile
,
4266 QEMU_OPTION_kernel_kqemu
,
4267 QEMU_OPTION_no_kqemu
,
4268 QEMU_OPTION_enable_kvm
,
4269 QEMU_OPTION_enable_nesting
,
4271 QEMU_OPTION_no_kvm_irqchip
,
4272 QEMU_OPTION_no_kvm_pit
,
4273 QEMU_OPTION_no_kvm_pit_reinjection
,
4274 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4275 QEMU_OPTION_pcidevice
,
4277 QEMU_OPTION_no_reboot
,
4278 QEMU_OPTION_no_shutdown
,
4280 QEMU_OPTION_daemonize
,
4281 QEMU_OPTION_option_rom
,
4282 QEMU_OPTION_cpu_vendor
,
4284 QEMU_OPTION_prom_env
,
4286 QEMU_OPTION_localtime
,
4287 QEMU_OPTION_startdate
,
4290 QEMU_OPTION_virtiocon
,
4291 QEMU_OPTION_show_cursor
,
4292 QEMU_OPTION_semihosting
,
4293 QEMU_OPTION_old_param
,
4294 QEMU_OPTION_tb_size
,
4295 QEMU_OPTION_incoming
,
4297 QEMU_OPTION_kvm_shadow_memory
,
4298 QEMU_OPTION_mempath
,
4300 QEMU_OPTION_mem_prealloc
,
4304 typedef struct QEMUOption
{
4310 static const QEMUOption qemu_options
[] = {
4311 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4313 /* Standard options: */
4314 { "h", 0, QEMU_OPTION_h
},
4315 { "help", 0, QEMU_OPTION_h
},
4316 { "M", HAS_ARG
, QEMU_OPTION_M
},
4317 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4318 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4319 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4320 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4321 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4322 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4323 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4324 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4325 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4326 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4327 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4328 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4329 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4330 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4331 { "snapshot", 0, QEMU_OPTION_snapshot
},
4332 { "m", HAS_ARG
, QEMU_OPTION_m
},
4334 { "k", HAS_ARG
, QEMU_OPTION_k
},
4337 { "audio-help", 0, QEMU_OPTION_audio_help
},
4338 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4340 { "usb", 0, QEMU_OPTION_usb
},
4341 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4342 { "name", HAS_ARG
, QEMU_OPTION_name
},
4343 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4345 /* Display options: */
4346 { "nographic", 0, QEMU_OPTION_nographic
},
4347 #ifdef CONFIG_CURSES
4348 { "curses", 0, QEMU_OPTION_curses
},
4351 { "no-frame", 0, QEMU_OPTION_no_frame
},
4352 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4353 { "no-quit", 0, QEMU_OPTION_no_quit
},
4354 { "sdl", 0, QEMU_OPTION_sdl
},
4356 { "portrait", 0, QEMU_OPTION_portrait
},
4357 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4358 { "full-screen", 0, QEMU_OPTION_full_screen
},
4359 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4360 { "g", 1, QEMU_OPTION_g
},
4362 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4364 /* Network options: */
4365 { "net", HAS_ARG
, QEMU_OPTION_net
},
4367 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4368 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4370 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4372 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4374 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4376 /* i386 target only: */
4377 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4378 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4379 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4380 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4381 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4384 /* Linux boot specific: */
4385 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4386 { "append", HAS_ARG
, QEMU_OPTION_append
},
4387 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4389 /* Debug/Expert options: */
4390 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4391 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4392 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4393 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4394 { "S", 0, QEMU_OPTION_S
},
4395 { "s", 0, QEMU_OPTION_s
},
4396 { "p", HAS_ARG
, QEMU_OPTION_p
},
4397 { "d", HAS_ARG
, QEMU_OPTION_d
},
4398 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4399 { "L", HAS_ARG
, QEMU_OPTION_L
},
4400 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4402 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4403 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4406 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4409 #ifndef NO_CPU_EMULATION
4410 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4412 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4413 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4414 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4415 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4416 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4417 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4420 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4421 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4422 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4423 { "daemonize", 0, QEMU_OPTION_daemonize
},
4424 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4425 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4426 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4428 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4429 { "localtime", 0, QEMU_OPTION_localtime
},
4430 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4431 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4432 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4433 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4434 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4435 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4436 { "semihosting", 0, QEMU_OPTION_semihosting
},
4438 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4439 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4440 { "nvram", HAS_ARG
, QEMU_OPTION_nvram
},
4441 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4442 #if defined(TARGET_ARM)
4443 { "old-param", 0, QEMU_OPTION_old_param
},
4445 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4446 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4447 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4449 { "mem-prealloc", 0, QEMU_OPTION_mem_prealloc
},
4454 /* password input */
4456 int qemu_key_check(BlockDriverState
*bs
, const char *name
)
4461 if (!bdrv_is_encrypted(bs
))
4464 term_printf("%s is encrypted.\n", name
);
4465 for(i
= 0; i
< 3; i
++) {
4466 monitor_readline("Password: ", 1, password
, sizeof(password
));
4467 if (bdrv_set_key(bs
, password
) == 0)
4469 term_printf("invalid password\n");
4474 static BlockDriverState
*get_bdrv(int index
)
4476 if (index
> nb_drives
)
4478 return drives_table
[index
].bdrv
;
4481 static void read_passwords(void)
4483 BlockDriverState
*bs
;
4486 for(i
= 0; i
< 6; i
++) {
4489 qemu_key_check(bs
, bdrv_get_device_name(bs
));
4494 struct soundhw soundhw
[] = {
4495 #ifdef HAS_AUDIO_CHOICE
4496 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4502 { .init_isa
= pcspk_audio_init
}
4509 "Creative Sound Blaster 16",
4512 { .init_isa
= SB16_init
}
4516 #ifdef CONFIG_CS4231A
4522 { .init_isa
= cs4231a_init
}
4530 "Yamaha YMF262 (OPL3)",
4532 "Yamaha YM3812 (OPL2)",
4536 { .init_isa
= Adlib_init
}
4543 "Gravis Ultrasound GF1",
4546 { .init_isa
= GUS_init
}
4553 "Intel 82801AA AC97 Audio",
4556 { .init_pci
= ac97_init
}
4560 #ifdef CONFIG_ES1370
4563 "ENSONIQ AudioPCI ES1370",
4566 { .init_pci
= es1370_init
}
4570 #endif /* HAS_AUDIO_CHOICE */
4572 { NULL
, NULL
, 0, 0, { NULL
} }
4575 static void select_soundhw (const char *optarg
)
4579 if (*optarg
== '?') {
4582 printf ("Valid sound card names (comma separated):\n");
4583 for (c
= soundhw
; c
->name
; ++c
) {
4584 printf ("%-11s %s\n", c
->name
, c
->descr
);
4586 printf ("\n-soundhw all will enable all of the above\n");
4587 exit (*optarg
!= '?');
4595 if (!strcmp (optarg
, "all")) {
4596 for (c
= soundhw
; c
->name
; ++c
) {
4604 e
= strchr (p
, ',');
4605 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4607 for (c
= soundhw
; c
->name
; ++c
) {
4608 if (!strncmp (c
->name
, p
, l
)) {
4617 "Unknown sound card name (too big to show)\n");
4620 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4625 p
+= l
+ (e
!= NULL
);
4629 goto show_valid_cards
;
4634 static void select_vgahw (const char *p
)
4638 if (strstart(p
, "std", &opts
)) {
4639 std_vga_enabled
= 1;
4640 cirrus_vga_enabled
= 0;
4642 } else if (strstart(p
, "cirrus", &opts
)) {
4643 cirrus_vga_enabled
= 1;
4644 std_vga_enabled
= 0;
4646 } else if (strstart(p
, "vmware", &opts
)) {
4647 cirrus_vga_enabled
= 0;
4648 std_vga_enabled
= 0;
4650 } else if (strstart(p
, "none", &opts
)) {
4651 cirrus_vga_enabled
= 0;
4652 std_vga_enabled
= 0;
4656 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4660 const char *nextopt
;
4662 if (strstart(opts
, ",retrace=", &nextopt
)) {
4664 if (strstart(opts
, "dumb", &nextopt
))
4665 vga_retrace_method
= VGA_RETRACE_DUMB
;
4666 else if (strstart(opts
, "precise", &nextopt
))
4667 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4668 else goto invalid_vga
;
4669 } else goto invalid_vga
;
4675 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4677 exit(STATUS_CONTROL_C_EXIT
);
4682 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4686 if(strlen(str
) != 36)
4689 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4690 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4691 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4699 #define MAX_NET_CLIENTS 32
4701 static int saved_argc
;
4702 static char **saved_argv
;
4704 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4708 *opt_daemonize
= daemonize
;
4709 *opt_incoming
= incoming
;
4713 static int gethugepagesize(void)
4717 const char *needle
= "Hugepagesize:";
4719 unsigned long hugepagesize
;
4721 fd
= open("/proc/meminfo", O_RDONLY
);
4727 ret
= read(fd
, buf
, sizeof(buf
));
4733 size
= strstr(buf
, needle
);
4736 size
+= strlen(needle
);
4737 hugepagesize
= strtol(size
, NULL
, 0);
4738 return hugepagesize
;
4741 static void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4750 if (!kvm_has_sync_mmu()) {
4751 fprintf(stderr
, "host lacks mmu notifiers, disabling --mem-path\n");
4755 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4758 hpagesize
= gethugepagesize() * 1024;
4762 fd
= mkstemp(filename
);
4771 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4774 * ftruncate is not supported by hugetlbfs in older
4775 * hosts, so don't bother checking for errors.
4776 * If anything goes wrong with it under other filesystems,
4779 ftruncate(fd
, memory
);
4782 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
4783 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
4784 * to sidestep this quirk.
4786 flags
= mem_prealloc
? MAP_POPULATE
|MAP_SHARED
: MAP_PRIVATE
;
4787 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, flags
, fd
, 0);
4789 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4791 if (area
== MAP_FAILED
) {
4792 perror("alloc_mem_area: can't mmap hugetlbfs pages");
4801 static void *qemu_alloc_physram(unsigned long memory
)
4805 unsigned long map_len
= memory
;
4808 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4811 area
= qemu_vmalloc(memory
);
4813 if (kvm_setup_guest_memory(area
, map_len
))
4821 static void termsig_handler(int signal
)
4823 qemu_system_shutdown_request();
4826 static void termsig_setup(void)
4828 struct sigaction act
;
4830 memset(&act
, 0, sizeof(act
));
4831 act
.sa_handler
= termsig_handler
;
4832 sigaction(SIGINT
, &act
, NULL
);
4833 sigaction(SIGHUP
, &act
, NULL
);
4834 sigaction(SIGTERM
, &act
, NULL
);
4839 int main(int argc
, char **argv
, char **envp
)
4841 #ifdef CONFIG_GDBSTUB
4843 const char *gdbstub_port
;
4845 uint32_t boot_devices_bitmap
= 0;
4847 int snapshot
, linux_boot
, net_boot
;
4848 const char *initrd_filename
;
4849 const char *kernel_filename
, *kernel_cmdline
;
4850 const char *boot_devices
= "";
4852 DisplayChangeListener
*dcl
;
4853 int cyls
, heads
, secs
, translation
;
4854 const char *net_clients
[MAX_NET_CLIENTS
];
4856 const char *bt_opts
[MAX_BT_CMDLINE
];
4860 const char *r
, *optarg
;
4861 CharDriverState
*monitor_hd
= NULL
;
4862 const char *monitor_device
;
4863 const char *serial_devices
[MAX_SERIAL_PORTS
];
4864 int serial_device_index
;
4865 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4866 int parallel_device_index
;
4867 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4868 int virtio_console_index
;
4869 const char *loadvm
= NULL
;
4870 QEMUMachine
*machine
;
4871 const char *cpu_model
;
4872 const char *usb_devices
[MAX_USB_CMDLINE
];
4873 int usb_devices_index
;
4876 const char *pid_file
= NULL
;
4878 const char *incoming
= NULL
;
4880 qemu_cache_utils_init(envp
);
4882 LIST_INIT (&vm_change_state_head
);
4885 struct sigaction act
;
4886 sigfillset(&act
.sa_mask
);
4888 act
.sa_handler
= SIG_IGN
;
4889 sigaction(SIGPIPE
, &act
, NULL
);
4892 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4893 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4894 QEMU to run on a single CPU */
4899 h
= GetCurrentProcess();
4900 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4901 for(i
= 0; i
< 32; i
++) {
4902 if (mask
& (1 << i
))
4907 SetProcessAffinityMask(h
, mask
);
4913 register_machines();
4914 machine
= first_machine
;
4916 initrd_filename
= NULL
;
4918 vga_ram_size
= VGA_RAM_SIZE
;
4919 #ifdef CONFIG_GDBSTUB
4921 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4926 kernel_filename
= NULL
;
4927 kernel_cmdline
= "";
4928 cyls
= heads
= secs
= 0;
4929 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4930 monitor_device
= "vc";
4932 serial_devices
[0] = "vc:80Cx24C";
4933 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4934 serial_devices
[i
] = NULL
;
4935 serial_device_index
= 0;
4937 parallel_devices
[0] = "vc:640x480";
4938 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4939 parallel_devices
[i
] = NULL
;
4940 parallel_device_index
= 0;
4942 virtio_consoles
[0] = "vc:80Cx24C";
4943 for(i
= 1; i
< MAX_VIRTIO_CONSOLES
; i
++)
4944 virtio_consoles
[i
] = NULL
;
4945 virtio_console_index
= 0;
4947 usb_devices_index
= 0;
4948 assigned_devices_index
= 0;
4967 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4969 const QEMUOption
*popt
;
4972 /* Treat --foo the same as -foo. */
4975 popt
= qemu_options
;
4978 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4982 if (!strcmp(popt
->name
, r
+ 1))
4986 if (popt
->flags
& HAS_ARG
) {
4987 if (optind
>= argc
) {
4988 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4992 optarg
= argv
[optind
++];
4997 switch(popt
->index
) {
4999 machine
= find_machine(optarg
);
5002 printf("Supported machines are:\n");
5003 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5004 printf("%-10s %s%s\n",
5006 m
== first_machine
? " (default)" : "");
5008 exit(*optarg
!= '?');
5011 case QEMU_OPTION_cpu
:
5012 /* hw initialization will check this */
5013 if (*optarg
== '?') {
5014 /* XXX: implement xxx_cpu_list for targets that still miss it */
5015 #if defined(cpu_list)
5016 cpu_list(stdout
, &fprintf
);
5023 case QEMU_OPTION_initrd
:
5024 initrd_filename
= optarg
;
5026 case QEMU_OPTION_hda
:
5028 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5030 hda_index
= drive_add(optarg
, HD_ALIAS
5031 ",cyls=%d,heads=%d,secs=%d%s",
5032 0, cyls
, heads
, secs
,
5033 translation
== BIOS_ATA_TRANSLATION_LBA
?
5035 translation
== BIOS_ATA_TRANSLATION_NONE
?
5036 ",trans=none" : "");
5038 case QEMU_OPTION_hdb
:
5039 case QEMU_OPTION_hdc
:
5040 case QEMU_OPTION_hdd
:
5041 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5043 case QEMU_OPTION_drive
:
5044 drive_add(NULL
, "%s", optarg
);
5046 case QEMU_OPTION_mtdblock
:
5047 drive_add(optarg
, MTD_ALIAS
);
5049 case QEMU_OPTION_sd
:
5050 drive_add(optarg
, SD_ALIAS
);
5052 case QEMU_OPTION_pflash
:
5053 drive_add(optarg
, PFLASH_ALIAS
);
5055 case QEMU_OPTION_snapshot
:
5058 case QEMU_OPTION_hdachs
:
5062 cyls
= strtol(p
, (char **)&p
, 0);
5063 if (cyls
< 1 || cyls
> 16383)
5068 heads
= strtol(p
, (char **)&p
, 0);
5069 if (heads
< 1 || heads
> 16)
5074 secs
= strtol(p
, (char **)&p
, 0);
5075 if (secs
< 1 || secs
> 63)
5079 if (!strcmp(p
, "none"))
5080 translation
= BIOS_ATA_TRANSLATION_NONE
;
5081 else if (!strcmp(p
, "lba"))
5082 translation
= BIOS_ATA_TRANSLATION_LBA
;
5083 else if (!strcmp(p
, "auto"))
5084 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5087 } else if (*p
!= '\0') {
5089 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5092 if (hda_index
!= -1)
5093 snprintf(drives_opt
[hda_index
].opt
,
5094 sizeof(drives_opt
[hda_index
].opt
),
5095 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5096 0, cyls
, heads
, secs
,
5097 translation
== BIOS_ATA_TRANSLATION_LBA
?
5099 translation
== BIOS_ATA_TRANSLATION_NONE
?
5100 ",trans=none" : "");
5103 case QEMU_OPTION_nographic
:
5106 #ifdef CONFIG_CURSES
5107 case QEMU_OPTION_curses
:
5111 case QEMU_OPTION_portrait
:
5114 case QEMU_OPTION_kernel
:
5115 kernel_filename
= optarg
;
5117 case QEMU_OPTION_append
:
5118 kernel_cmdline
= optarg
;
5120 case QEMU_OPTION_cdrom
:
5121 drive_add(optarg
, CDROM_ALIAS
);
5123 case QEMU_OPTION_boot
:
5124 boot_devices
= optarg
;
5125 /* We just do some generic consistency checks */
5127 /* Could easily be extended to 64 devices if needed */
5130 boot_devices_bitmap
= 0;
5131 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5132 /* Allowed boot devices are:
5133 * a b : floppy disk drives
5134 * c ... f : IDE disk drives
5135 * g ... m : machine implementation dependant drives
5136 * n ... p : network devices
5137 * It's up to each machine implementation to check
5138 * if the given boot devices match the actual hardware
5139 * implementation and firmware features.
5141 if (*p
< 'a' || *p
> 'q') {
5142 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5145 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5147 "Boot device '%c' was given twice\n",*p
);
5150 boot_devices_bitmap
|= 1 << (*p
- 'a');
5154 case QEMU_OPTION_fda
:
5155 case QEMU_OPTION_fdb
:
5156 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5159 case QEMU_OPTION_no_fd_bootchk
:
5163 case QEMU_OPTION_net
:
5164 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5165 fprintf(stderr
, "qemu: too many network clients\n");
5168 net_clients
[nb_net_clients
] = optarg
;
5172 case QEMU_OPTION_tftp
:
5173 tftp_prefix
= optarg
;
5175 case QEMU_OPTION_bootp
:
5176 bootp_filename
= optarg
;
5179 case QEMU_OPTION_smb
:
5180 net_slirp_smb(optarg
);
5183 case QEMU_OPTION_redir
:
5184 net_slirp_redir(optarg
);
5187 case QEMU_OPTION_bt
:
5188 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5189 fprintf(stderr
, "qemu: too many bluetooth options\n");
5192 bt_opts
[nb_bt_opts
++] = optarg
;
5195 case QEMU_OPTION_audio_help
:
5199 case QEMU_OPTION_soundhw
:
5200 select_soundhw (optarg
);
5206 case QEMU_OPTION_m
: {
5210 value
= strtoul(optarg
, &ptr
, 10);
5212 case 0: case 'M': case 'm':
5219 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5223 /* On 32-bit hosts, QEMU is limited by virtual address space */
5224 if (value
> (2047 << 20)
5226 && HOST_LONG_BITS
== 32
5229 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5232 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5233 fprintf(stderr
, "qemu: ram size too large\n");
5242 const CPULogItem
*item
;
5244 mask
= cpu_str_to_log_mask(optarg
);
5246 printf("Log items (comma separated):\n");
5247 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5248 printf("%-10s %s\n", item
->name
, item
->help
);
5255 #ifdef CONFIG_GDBSTUB
5260 gdbstub_port
= optarg
;
5266 case QEMU_OPTION_bios
:
5273 keyboard_layout
= optarg
;
5275 case QEMU_OPTION_localtime
:
5278 case QEMU_OPTION_vga
:
5279 select_vgahw (optarg
);
5286 w
= strtol(p
, (char **)&p
, 10);
5289 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5295 h
= strtol(p
, (char **)&p
, 10);
5300 depth
= strtol(p
, (char **)&p
, 10);
5301 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5302 depth
!= 24 && depth
!= 32)
5304 } else if (*p
== '\0') {
5305 depth
= graphic_depth
;
5312 graphic_depth
= depth
;
5315 case QEMU_OPTION_echr
:
5318 term_escape_char
= strtol(optarg
, &r
, 0);
5320 printf("Bad argument to echr\n");
5323 case QEMU_OPTION_monitor
:
5324 monitor_device
= optarg
;
5326 case QEMU_OPTION_serial
:
5327 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5328 fprintf(stderr
, "qemu: too many serial ports\n");
5331 serial_devices
[serial_device_index
] = optarg
;
5332 serial_device_index
++;
5334 case QEMU_OPTION_virtiocon
:
5335 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5336 fprintf(stderr
, "qemu: too many virtio consoles\n");
5339 virtio_consoles
[virtio_console_index
] = optarg
;
5340 virtio_console_index
++;
5342 case QEMU_OPTION_parallel
:
5343 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5344 fprintf(stderr
, "qemu: too many parallel ports\n");
5347 parallel_devices
[parallel_device_index
] = optarg
;
5348 parallel_device_index
++;
5350 case QEMU_OPTION_loadvm
:
5353 case QEMU_OPTION_full_screen
:
5357 case QEMU_OPTION_no_frame
:
5360 case QEMU_OPTION_alt_grab
:
5363 case QEMU_OPTION_no_quit
:
5366 case QEMU_OPTION_sdl
:
5370 case QEMU_OPTION_pidfile
:
5374 case QEMU_OPTION_win2k_hack
:
5375 win2k_install_hack
= 1;
5377 case QEMU_OPTION_rtc_td_hack
:
5382 case QEMU_OPTION_no_kqemu
:
5385 case QEMU_OPTION_kernel_kqemu
:
5390 case QEMU_OPTION_enable_kvm
:
5398 case QEMU_OPTION_no_kvm
:
5401 case QEMU_OPTION_no_kvm_irqchip
: {
5406 case QEMU_OPTION_no_kvm_pit
: {
5410 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5411 kvm_pit_reinject
= 0;
5414 case QEMU_OPTION_enable_nesting
: {
5418 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5419 case QEMU_OPTION_pcidevice
:
5420 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5421 fprintf(stderr
, "Too many assigned devices\n");
5424 assigned_devices
[assigned_devices_index
] = optarg
;
5425 assigned_devices_index
++;
5429 case QEMU_OPTION_usb
:
5432 case QEMU_OPTION_usbdevice
:
5434 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5435 fprintf(stderr
, "Too many USB devices\n");
5438 usb_devices
[usb_devices_index
] = optarg
;
5439 usb_devices_index
++;
5441 case QEMU_OPTION_smp
:
5442 smp_cpus
= atoi(optarg
);
5444 fprintf(stderr
, "Invalid number of CPUs\n");
5448 case QEMU_OPTION_vnc
:
5449 vnc_display
= optarg
;
5451 case QEMU_OPTION_no_acpi
:
5454 case QEMU_OPTION_no_hpet
:
5457 case QEMU_OPTION_no_reboot
:
5460 case QEMU_OPTION_no_shutdown
:
5463 case QEMU_OPTION_show_cursor
:
5466 case QEMU_OPTION_uuid
:
5467 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5468 fprintf(stderr
, "Fail to parse UUID string."
5469 " Wrong format.\n");
5473 case QEMU_OPTION_daemonize
:
5476 case QEMU_OPTION_option_rom
:
5477 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5478 fprintf(stderr
, "Too many option ROMs\n");
5481 option_rom
[nb_option_roms
] = optarg
;
5484 case QEMU_OPTION_semihosting
:
5485 semihosting_enabled
= 1;
5487 case QEMU_OPTION_tdf
:
5490 case QEMU_OPTION_kvm_shadow_memory
:
5491 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5493 case QEMU_OPTION_mempath
:
5497 case QEMU_OPTION_mem_prealloc
:
5498 mem_prealloc
= !mem_prealloc
;
5501 case QEMU_OPTION_name
:
5504 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5505 case QEMU_OPTION_prom_env
:
5506 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5507 fprintf(stderr
, "Too many prom variables\n");
5510 prom_envs
[nb_prom_envs
] = optarg
;
5514 case QEMU_OPTION_cpu_vendor
:
5515 cpu_vendor_string
= optarg
;
5518 case QEMU_OPTION_old_param
:
5522 case QEMU_OPTION_clock
:
5523 configure_alarms(optarg
);
5525 case QEMU_OPTION_startdate
:
5528 time_t rtc_start_date
;
5529 if (!strcmp(optarg
, "now")) {
5530 rtc_date_offset
= -1;
5532 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5540 } else if (sscanf(optarg
, "%d-%d-%d",
5543 &tm
.tm_mday
) == 3) {
5552 rtc_start_date
= mktimegm(&tm
);
5553 if (rtc_start_date
== -1) {
5555 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5556 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5559 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5563 case QEMU_OPTION_tb_size
:
5564 tb_size
= strtol(optarg
, NULL
, 0);
5568 case QEMU_OPTION_icount
:
5570 if (strcmp(optarg
, "auto") == 0) {
5571 icount_time_shift
= -1;
5573 icount_time_shift
= strtol(optarg
, NULL
, 0);
5576 case QEMU_OPTION_incoming
:
5579 case QEMU_OPTION_nvram
:
5586 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5587 if (kvm_allowed
&& kqemu_allowed
) {
5589 "You can not enable both KVM and kqemu at the same time\n");
5594 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5595 if (smp_cpus
> machine
->max_cpus
) {
5596 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5597 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5603 if (serial_device_index
== 0)
5604 serial_devices
[0] = "stdio";
5605 if (parallel_device_index
== 0)
5606 parallel_devices
[0] = "null";
5607 if (strncmp(monitor_device
, "vc", 2) == 0)
5608 monitor_device
= "stdio";
5609 if (virtio_console_index
== 0)
5610 virtio_consoles
[0] = "null";
5617 if (pipe(fds
) == -1)
5628 len
= read(fds
[0], &status
, 1);
5629 if (len
== -1 && (errno
== EINTR
))
5634 else if (status
== 1) {
5635 fprintf(stderr
, "Could not acquire pidfile\n");
5652 signal(SIGTSTP
, SIG_IGN
);
5653 signal(SIGTTOU
, SIG_IGN
);
5654 signal(SIGTTIN
, SIG_IGN
);
5659 if (kvm_enabled()) {
5660 if (kvm_qemu_init() < 0) {
5661 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5662 #ifdef NO_CPU_EMULATION
5663 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5671 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5674 write(fds
[1], &status
, 1);
5676 fprintf(stderr
, "Could not acquire pid file\n");
5684 linux_boot
= (kernel_filename
!= NULL
);
5685 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5687 if (!linux_boot
&& net_boot
== 0 &&
5688 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5691 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5692 fprintf(stderr
, "-append only allowed with -kernel option\n");
5696 if (!linux_boot
&& initrd_filename
!= NULL
) {
5697 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5701 /* boot to floppy or the default cd if no hard disk defined yet */
5702 if (!boot_devices
[0]) {
5703 boot_devices
= "cad";
5705 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5708 if (init_timer_alarm() < 0) {
5709 fprintf(stderr
, "could not initialize alarm timer\n");
5712 if (use_icount
&& icount_time_shift
< 0) {
5714 /* 125MIPS seems a reasonable initial guess at the guest speed.
5715 It will be corrected fairly quickly anyway. */
5716 icount_time_shift
= 3;
5717 init_icount_adjust();
5724 /* init network clients */
5725 if (nb_net_clients
== 0) {
5726 /* if no clients, we use a default config */
5727 net_clients
[nb_net_clients
++] = "nic";
5729 net_clients
[nb_net_clients
++] = "user";
5733 for(i
= 0;i
< nb_net_clients
; i
++) {
5734 if (net_client_parse(net_clients
[i
]) < 0)
5740 /* XXX: this should be moved in the PC machine instantiation code */
5741 if (net_boot
!= 0) {
5743 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5744 const char *model
= nd_table
[i
].model
;
5746 if (net_boot
& (1 << i
)) {
5749 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5750 if (get_image_size(buf
) > 0) {
5751 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5752 fprintf(stderr
, "Too many option ROMs\n");
5755 option_rom
[nb_option_roms
] = strdup(buf
);
5762 fprintf(stderr
, "No valid PXE rom found for network device\n");
5768 /* init the bluetooth world */
5769 for (i
= 0; i
< nb_bt_opts
; i
++)
5770 if (bt_parse(bt_opts
[i
]))
5773 /* init the memory */
5774 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5776 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5778 if (ram_size
< phys_ram_size
) {
5779 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5780 machine
->name
, (unsigned long long) phys_ram_size
);
5784 phys_ram_size
= ram_size
;
5786 ram_size
= phys_ram_size
;
5789 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5791 phys_ram_size
+= ram_size
;
5794 /* Initialize kvm */
5795 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5796 #define KVM_EXTRA_PAGES 3
5798 #define KVM_EXTRA_PAGES 0
5800 if (kvm_enabled()) {
5801 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5802 if (kvm_qemu_create_context() < 0) {
5803 fprintf(stderr
, "Could not create KVM context\n");
5808 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5809 if (!phys_ram_base
) {
5810 fprintf(stderr
, "Could not allocate physical memory\n");
5814 /* init the dynamic translator */
5815 cpu_exec_init_all(tb_size
* 1024 * 1024);
5819 /* we always create the cdrom drive, even if no disk is there */
5821 if (nb_drives_opt
< MAX_DRIVES
)
5822 drive_add(NULL
, CDROM_ALIAS
);
5824 /* we always create at least one floppy */
5826 if (nb_drives_opt
< MAX_DRIVES
)
5827 drive_add(NULL
, FD_ALIAS
, 0);
5829 /* we always create one sd slot, even if no card is in it */
5831 if (nb_drives_opt
< MAX_DRIVES
)
5832 drive_add(NULL
, SD_ALIAS
);
5834 /* open the virtual block devices
5835 * note that migration with device
5836 * hot add/remove is broken.
5838 for(i
= 0; i
< nb_drives_opt
; i
++)
5839 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5842 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5843 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5846 /* must be after terminal init, SDL library changes signal handlers */
5850 /* Maintain compatibility with multiple stdio monitors */
5851 if (!strcmp(monitor_device
,"stdio")) {
5852 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5853 const char *devname
= serial_devices
[i
];
5854 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5855 monitor_device
= NULL
;
5857 } else if (devname
&& !strcmp(devname
,"stdio")) {
5858 monitor_device
= NULL
;
5859 serial_devices
[i
] = "mon:stdio";
5866 if (kvm_enabled()) {
5869 ret
= kvm_init(smp_cpus
);
5871 fprintf(stderr
, "failed to initialize KVM\n");
5877 if (monitor_device
) {
5878 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5880 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5885 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5886 const char *devname
= serial_devices
[i
];
5887 if (devname
&& strcmp(devname
, "none")) {
5889 snprintf(label
, sizeof(label
), "serial%d", i
);
5890 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5891 if (!serial_hds
[i
]) {
5892 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5899 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5900 const char *devname
= parallel_devices
[i
];
5901 if (devname
&& strcmp(devname
, "none")) {
5903 snprintf(label
, sizeof(label
), "parallel%d", i
);
5904 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5905 if (!parallel_hds
[i
]) {
5906 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5913 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5914 const char *devname
= virtio_consoles
[i
];
5915 if (devname
&& strcmp(devname
, "none")) {
5917 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5918 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5919 if (!virtcon_hds
[i
]) {
5920 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5930 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5931 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5933 current_machine
= machine
;
5935 /* Set KVM's vcpu state to qemu's initial CPUState. */
5936 if (kvm_enabled()) {
5939 ret
= kvm_sync_vcpus();
5941 fprintf(stderr
, "failed to initialize vcpus\n");
5946 /* init USB devices */
5948 for(i
= 0; i
< usb_devices_index
; i
++) {
5949 if (usb_device_add(usb_devices
[i
]) < 0) {
5950 fprintf(stderr
, "Warning: could not add USB device %s\n",
5957 dumb_display_init();
5958 /* just use the first displaystate for the moment */
5963 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5967 #if defined(CONFIG_CURSES)
5969 /* At the moment curses cannot be used with other displays */
5970 curses_display_init(ds
, full_screen
);
5974 if (vnc_display
!= NULL
) {
5975 vnc_display_init(ds
);
5976 if (vnc_display_open(ds
, vnc_display
) < 0)
5979 #if defined(CONFIG_SDL)
5980 if (sdl
|| !vnc_display
)
5981 sdl_display_init(ds
, full_screen
, no_frame
);
5982 #elif defined(CONFIG_COCOA)
5983 if (sdl
|| !vnc_display
)
5984 cocoa_display_init(ds
, full_screen
);
5990 dcl
= ds
->listeners
;
5991 while (dcl
!= NULL
) {
5992 if (dcl
->dpy_refresh
!= NULL
) {
5993 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5994 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5999 if (nographic
|| (vnc_display
&& !sdl
)) {
6000 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6001 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6004 text_consoles_set_display(display_state
);
6006 if (monitor_device
&& monitor_hd
)
6007 monitor_init(monitor_hd
, !nographic
);
6009 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6010 const char *devname
= serial_devices
[i
];
6011 if (devname
&& strcmp(devname
, "none")) {
6013 snprintf(label
, sizeof(label
), "serial%d", i
);
6014 if (strstart(devname
, "vc", 0))
6015 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6019 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6020 const char *devname
= parallel_devices
[i
];
6021 if (devname
&& strcmp(devname
, "none")) {
6023 snprintf(label
, sizeof(label
), "parallel%d", i
);
6024 if (strstart(devname
, "vc", 0))
6025 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6029 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6030 const char *devname
= virtio_consoles
[i
];
6031 if (virtcon_hds
[i
] && devname
) {
6033 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6034 if (strstart(devname
, "vc", 0))
6035 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6039 #ifdef CONFIG_GDBSTUB
6041 /* XXX: use standard host:port notation and modify options
6043 if (gdbserver_start(gdbstub_port
) < 0) {
6044 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
6055 autostart
= 0; /* fixme how to deal with -daemonize */
6056 qemu_start_incoming_migration(incoming
);
6060 /* XXX: simplify init */
6073 len
= write(fds
[1], &status
, 1);
6074 if (len
== -1 && (errno
== EINTR
))
6081 TFR(fd
= open("/dev/null", O_RDWR
));