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"
58 #include <sys/times.h>
62 #include <sys/ioctl.h>
63 #include <sys/resource.h>
64 #include <sys/socket.h>
66 #include <netinet/in.h>
68 #if defined(__NetBSD__)
69 #include <net/if_tap.h>
72 #include <linux/if_tun.h>
74 #include <arpa/inet.h>
77 #include <sys/select.h>
85 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
86 #include <freebsd/stdlib.h>
91 #include <linux/rtc.h>
93 /* For the benefit of older linux systems which don't supply it,
94 we use a local copy of hpet.h. */
95 /* #include <linux/hpet.h> */
98 #include <linux/ppdev.h>
99 #include <linux/parport.h>
102 #include <sys/stat.h>
103 #include <sys/ethernet.h>
104 #include <sys/sockio.h>
105 #include <netinet/arp.h>
106 #include <netinet/in.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/ip.h>
109 #include <netinet/ip_icmp.h> // must come after ip.h
110 #include <netinet/udp.h>
111 #include <netinet/tcp.h>
119 #include "qemu_socket.h"
121 #if defined(CONFIG_SLIRP)
122 #include "libslirp.h"
125 #if defined(__OpenBSD__)
129 #if defined(CONFIG_VDE)
130 #include <libvdeplug.h>
135 #include <sys/timeb.h>
136 #include <mmsystem.h>
137 #define getopt_long_only getopt_long
138 #define memalign(align, size) malloc(size)
144 int qemu_main(int argc
, char **argv
, char **envp
);
145 int main(int argc
, char **argv
)
147 qemu_main(argc
, argv
, NULL
);
150 #define main qemu_main
152 #endif /* CONFIG_SDL */
156 #define main qemu_main
157 #endif /* CONFIG_COCOA */
161 #include "exec-all.h"
163 #include "qemu-kvm.h"
165 //#define DEBUG_UNUSED_IOPORT
166 //#define DEBUG_IOPORT
168 //#define DEBUG_SLIRP
172 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
174 # define LOG_IOPORT(...) do { } while (0)
177 #define DEFAULT_RAM_SIZE 128
179 /* Max number of USB devices that can be specified on the commandline. */
180 #define MAX_USB_CMDLINE 8
182 /* Max number of bluetooth switches on the commandline. */
183 #define MAX_BT_CMDLINE 10
185 /* XXX: use a two level table to limit memory usage */
186 #define MAX_IOPORTS 65536
188 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
189 const char *bios_name
= NULL
;
190 static void *ioport_opaque
[MAX_IOPORTS
];
191 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
192 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
193 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
194 to store the VM snapshots */
195 DriveInfo drives_table
[MAX_DRIVES
+1];
197 int extboot_drive
= -1;
198 static int vga_ram_size
;
199 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
200 static DisplayState
*display_state
;
204 const char* keyboard_layout
= NULL
;
205 int64_t ticks_per_sec
;
208 NICInfo nd_table
[MAX_NICS
];
210 static int autostart
;
211 static int rtc_utc
= 1;
212 static int rtc_date_offset
= -1; /* -1 means no change */
213 int cirrus_vga_enabled
= 1;
214 int std_vga_enabled
= 0;
215 int vmsvga_enabled
= 0;
217 int graphic_width
= 1024;
218 int graphic_height
= 768;
219 int graphic_depth
= 8;
221 int graphic_width
= 800;
222 int graphic_height
= 600;
223 int graphic_depth
= 15;
225 static int full_screen
= 0;
227 static int no_frame
= 0;
230 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
231 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
232 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
234 int win2k_install_hack
= 0;
238 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
239 int assigned_devices_index
;
241 const char *vnc_display
;
242 int acpi_enabled
= 1;
248 int graphic_rotate
= 0;
250 const char *incoming
;
251 const char *option_rom
[MAX_OPTION_ROMS
];
253 int semihosting_enabled
= 0;
254 int time_drift_fix
= 0;
255 unsigned int kvm_shadow_memory
= 0;
256 const char *mem_path
= NULL
;
258 int mem_prealloc
= 1; /* force preallocation of physical target memory */
261 const char *cpu_vendor_string
;
265 const char *qemu_name
;
267 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
268 unsigned int nb_prom_envs
= 0;
269 const char *prom_envs
[MAX_PROM_ENVS
];
272 const char *nvram
= NULL
;
273 struct drive_opt drives_opt
[MAX_DRIVES
];
275 static CPUState
*cur_cpu
;
276 static CPUState
*next_cpu
;
277 static int event_pending
= 1;
278 /* Conversion factor from emulated instructions to virtual clock ticks. */
279 static int icount_time_shift
;
280 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
281 #define MAX_ICOUNT_SHIFT 10
282 /* Compensate for varying guest execution speed. */
283 static int64_t qemu_icount_bias
;
284 static QEMUTimer
*icount_rt_timer
;
285 static QEMUTimer
*icount_vm_timer
;
286 static QEMUTimer
*nographic_timer
;
288 uint8_t qemu_uuid
[16];
290 /* KVM runs the main loop in a separate thread. If we update one of the lists
291 * that are polled before or after select(), we need to make sure to break out
292 * of the select() to ensure the new item is serviced.
294 static void main_loop_break(void)
297 qemu_kvm_notify_work();
300 /***********************************************************/
301 /* x86 ISA bus support */
303 target_phys_addr_t isa_mem_base
= 0;
306 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
307 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
309 static uint32_t ioport_read(int index
, uint32_t address
)
311 static IOPortReadFunc
*default_func
[3] = {
312 default_ioport_readb
,
313 default_ioport_readw
,
316 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
318 func
= default_func
[index
];
319 return func(ioport_opaque
[address
], address
);
322 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
324 static IOPortWriteFunc
*default_func
[3] = {
325 default_ioport_writeb
,
326 default_ioport_writew
,
327 default_ioport_writel
329 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
331 func
= default_func
[index
];
332 func(ioport_opaque
[address
], address
, data
);
335 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
337 #ifdef DEBUG_UNUSED_IOPORT
338 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
343 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
345 #ifdef DEBUG_UNUSED_IOPORT
346 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
350 /* default is to make two byte accesses */
351 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
354 data
= ioport_read(0, address
);
355 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
356 data
|= ioport_read(0, address
) << 8;
360 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
362 ioport_write(0, address
, data
& 0xff);
363 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
364 ioport_write(0, address
, (data
>> 8) & 0xff);
367 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
369 #ifdef DEBUG_UNUSED_IOPORT
370 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
375 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
377 #ifdef DEBUG_UNUSED_IOPORT
378 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
382 /* size is the word size in byte */
383 int register_ioport_read(int start
, int length
, int size
,
384 IOPortReadFunc
*func
, void *opaque
)
390 } else if (size
== 2) {
392 } else if (size
== 4) {
395 hw_error("register_ioport_read: invalid size");
398 for(i
= start
; i
< start
+ length
; i
+= size
) {
399 ioport_read_table
[bsize
][i
] = func
;
400 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
401 hw_error("register_ioport_read: invalid opaque");
402 ioport_opaque
[i
] = opaque
;
407 /* size is the word size in byte */
408 int register_ioport_write(int start
, int length
, int size
,
409 IOPortWriteFunc
*func
, void *opaque
)
415 } else if (size
== 2) {
417 } else if (size
== 4) {
420 hw_error("register_ioport_write: invalid size");
423 for(i
= start
; i
< start
+ length
; i
+= size
) {
424 ioport_write_table
[bsize
][i
] = func
;
425 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
426 hw_error("register_ioport_write: invalid opaque");
427 ioport_opaque
[i
] = opaque
;
432 void isa_unassign_ioport(int start
, int length
)
436 for(i
= start
; i
< start
+ length
; i
++) {
437 ioport_read_table
[0][i
] = default_ioport_readb
;
438 ioport_read_table
[1][i
] = default_ioport_readw
;
439 ioport_read_table
[2][i
] = default_ioport_readl
;
441 ioport_write_table
[0][i
] = default_ioport_writeb
;
442 ioport_write_table
[1][i
] = default_ioport_writew
;
443 ioport_write_table
[2][i
] = default_ioport_writel
;
445 ioport_opaque
[i
] = NULL
;
449 /***********************************************************/
451 void cpu_outb(CPUState
*env
, int addr
, int val
)
453 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
454 ioport_write(0, addr
, val
);
457 env
->last_io_time
= cpu_get_time_fast();
461 void cpu_outw(CPUState
*env
, int addr
, int val
)
463 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
464 ioport_write(1, addr
, val
);
467 env
->last_io_time
= cpu_get_time_fast();
471 void cpu_outl(CPUState
*env
, int addr
, int val
)
473 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
474 ioport_write(2, addr
, val
);
477 env
->last_io_time
= cpu_get_time_fast();
481 int cpu_inb(CPUState
*env
, int addr
)
484 val
= ioport_read(0, addr
);
485 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
488 env
->last_io_time
= cpu_get_time_fast();
493 int cpu_inw(CPUState
*env
, int addr
)
496 val
= ioport_read(1, addr
);
497 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
500 env
->last_io_time
= cpu_get_time_fast();
505 int cpu_inl(CPUState
*env
, int addr
)
508 val
= ioport_read(2, addr
);
509 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
512 env
->last_io_time
= cpu_get_time_fast();
517 /***********************************************************/
518 void hw_error(const char *fmt
, ...)
524 fprintf(stderr
, "qemu: hardware error: ");
525 vfprintf(stderr
, fmt
, ap
);
526 fprintf(stderr
, "\n");
527 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
528 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
530 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
532 cpu_dump_state(env
, stderr
, fprintf
, 0);
542 static QEMUBalloonEvent
*qemu_balloon_event
;
543 void *qemu_balloon_event_opaque
;
545 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
547 qemu_balloon_event
= func
;
548 qemu_balloon_event_opaque
= opaque
;
551 void qemu_balloon(ram_addr_t target
)
553 if (qemu_balloon_event
)
554 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
557 ram_addr_t
qemu_balloon_status(void)
559 if (qemu_balloon_event
)
560 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
564 /***********************************************************/
567 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
568 static void *qemu_put_kbd_event_opaque
;
569 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
570 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
572 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
574 qemu_put_kbd_event_opaque
= opaque
;
575 qemu_put_kbd_event
= func
;
578 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
579 void *opaque
, int absolute
,
582 QEMUPutMouseEntry
*s
, *cursor
;
584 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
586 s
->qemu_put_mouse_event
= func
;
587 s
->qemu_put_mouse_event_opaque
= opaque
;
588 s
->qemu_put_mouse_event_absolute
= absolute
;
589 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
592 if (!qemu_put_mouse_event_head
) {
593 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
597 cursor
= qemu_put_mouse_event_head
;
598 while (cursor
->next
!= NULL
)
599 cursor
= cursor
->next
;
602 qemu_put_mouse_event_current
= s
;
607 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
609 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
611 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
614 cursor
= qemu_put_mouse_event_head
;
615 while (cursor
!= NULL
&& cursor
!= entry
) {
617 cursor
= cursor
->next
;
620 if (cursor
== NULL
) // does not exist or list empty
622 else if (prev
== NULL
) { // entry is head
623 qemu_put_mouse_event_head
= cursor
->next
;
624 if (qemu_put_mouse_event_current
== entry
)
625 qemu_put_mouse_event_current
= cursor
->next
;
626 qemu_free(entry
->qemu_put_mouse_event_name
);
631 prev
->next
= entry
->next
;
633 if (qemu_put_mouse_event_current
== entry
)
634 qemu_put_mouse_event_current
= prev
;
636 qemu_free(entry
->qemu_put_mouse_event_name
);
640 void kbd_put_keycode(int keycode
)
642 if (qemu_put_kbd_event
) {
643 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
647 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
649 QEMUPutMouseEvent
*mouse_event
;
650 void *mouse_event_opaque
;
653 if (!qemu_put_mouse_event_current
) {
658 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
660 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
663 if (graphic_rotate
) {
664 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
667 width
= graphic_width
- 1;
668 mouse_event(mouse_event_opaque
,
669 width
- dy
, dx
, dz
, buttons_state
);
671 mouse_event(mouse_event_opaque
,
672 dx
, dy
, dz
, buttons_state
);
676 int kbd_mouse_is_absolute(void)
678 if (!qemu_put_mouse_event_current
)
681 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
684 void do_info_mice(void)
686 QEMUPutMouseEntry
*cursor
;
689 if (!qemu_put_mouse_event_head
) {
690 term_printf("No mouse devices connected\n");
694 term_printf("Mouse devices available:\n");
695 cursor
= qemu_put_mouse_event_head
;
696 while (cursor
!= NULL
) {
697 term_printf("%c Mouse #%d: %s\n",
698 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
699 index
, cursor
->qemu_put_mouse_event_name
);
701 cursor
= cursor
->next
;
705 void do_mouse_set(int index
)
707 QEMUPutMouseEntry
*cursor
;
710 if (!qemu_put_mouse_event_head
) {
711 term_printf("No mouse devices connected\n");
715 cursor
= qemu_put_mouse_event_head
;
716 while (cursor
!= NULL
&& index
!= i
) {
718 cursor
= cursor
->next
;
722 qemu_put_mouse_event_current
= cursor
;
724 term_printf("Mouse at given index not found\n");
727 /* compute with 96 bit intermediate result: (a*b)/c */
728 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
733 #ifdef WORDS_BIGENDIAN
743 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
744 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
747 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
751 /***********************************************************/
752 /* real time host monotonic timer */
754 #define QEMU_TIMER_BASE 1000000000LL
758 static int64_t clock_freq
;
760 static void init_get_clock(void)
764 ret
= QueryPerformanceFrequency(&freq
);
766 fprintf(stderr
, "Could not calibrate ticks\n");
769 clock_freq
= freq
.QuadPart
;
772 static int64_t get_clock(void)
775 QueryPerformanceCounter(&ti
);
776 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
781 static int use_rt_clock
;
783 static void init_get_clock(void)
786 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
789 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
796 static int64_t get_clock(void)
798 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
801 clock_gettime(CLOCK_MONOTONIC
, &ts
);
802 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
806 /* XXX: using gettimeofday leads to problems if the date
807 changes, so it should be avoided. */
809 gettimeofday(&tv
, NULL
);
810 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
815 /* Return the virtual CPU time, based on the instruction counter. */
816 static int64_t cpu_get_icount(void)
819 CPUState
*env
= cpu_single_env
;;
820 icount
= qemu_icount
;
823 fprintf(stderr
, "Bad clock read\n");
824 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
826 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
829 /***********************************************************/
830 /* guest cycle counter */
832 static int64_t cpu_ticks_prev
;
833 static int64_t cpu_ticks_offset
;
834 static int64_t cpu_clock_offset
;
835 static int cpu_ticks_enabled
;
837 /* return the host CPU cycle counter and handle stop/restart */
838 int64_t cpu_get_ticks(void)
841 return cpu_get_icount();
843 if (!cpu_ticks_enabled
) {
844 return cpu_ticks_offset
;
847 ticks
= cpu_get_real_ticks();
848 if (cpu_ticks_prev
> ticks
) {
849 /* Note: non increasing ticks may happen if the host uses
851 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
853 cpu_ticks_prev
= ticks
;
854 return ticks
+ cpu_ticks_offset
;
858 /* return the host CPU monotonic timer and handle stop/restart */
859 static int64_t cpu_get_clock(void)
862 if (!cpu_ticks_enabled
) {
863 return cpu_clock_offset
;
866 return ti
+ cpu_clock_offset
;
870 /* enable cpu_get_ticks() */
871 void cpu_enable_ticks(void)
873 if (!cpu_ticks_enabled
) {
874 cpu_ticks_offset
-= cpu_get_real_ticks();
875 cpu_clock_offset
-= get_clock();
876 cpu_ticks_enabled
= 1;
880 /* disable cpu_get_ticks() : the clock is stopped. You must not call
881 cpu_get_ticks() after that. */
882 void cpu_disable_ticks(void)
884 if (cpu_ticks_enabled
) {
885 cpu_ticks_offset
= cpu_get_ticks();
886 cpu_clock_offset
= cpu_get_clock();
887 cpu_ticks_enabled
= 0;
891 /***********************************************************/
894 #define QEMU_TIMER_REALTIME 0
895 #define QEMU_TIMER_VIRTUAL 1
899 /* XXX: add frequency */
907 struct QEMUTimer
*next
;
910 struct qemu_alarm_timer
{
914 int (*start
)(struct qemu_alarm_timer
*t
);
915 void (*stop
)(struct qemu_alarm_timer
*t
);
916 void (*rearm
)(struct qemu_alarm_timer
*t
);
920 #define ALARM_FLAG_DYNTICKS 0x1
921 #define ALARM_FLAG_EXPIRED 0x2
923 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
925 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
928 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
930 if (!alarm_has_dynticks(t
))
936 /* TODO: MIN_TIMER_REARM_US should be optimized */
937 #define MIN_TIMER_REARM_US 250
939 static struct qemu_alarm_timer
*alarm_timer
;
941 static int alarm_timer_rfd
, alarm_timer_wfd
;
946 struct qemu_alarm_win32
{
950 } alarm_win32_data
= {0, NULL
, -1};
952 static int win32_start_timer(struct qemu_alarm_timer
*t
);
953 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
954 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
958 static int unix_start_timer(struct qemu_alarm_timer
*t
);
959 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
963 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
964 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
965 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
967 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
968 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
970 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
971 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
973 #endif /* __linux__ */
977 /* Correlation between real and virtual time is always going to be
978 fairly approximate, so ignore small variation.
979 When the guest is idle real and virtual time will be aligned in
981 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
983 static void icount_adjust(void)
988 static int64_t last_delta
;
989 /* If the VM is not running, then do nothing. */
993 cur_time
= cpu_get_clock();
994 cur_icount
= qemu_get_clock(vm_clock
);
995 delta
= cur_icount
- cur_time
;
996 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
998 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
999 && icount_time_shift
> 0) {
1000 /* The guest is getting too far ahead. Slow time down. */
1001 icount_time_shift
--;
1004 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1005 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1006 /* The guest is getting too far behind. Speed time up. */
1007 icount_time_shift
++;
1010 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1013 static void icount_adjust_rt(void * opaque
)
1015 qemu_mod_timer(icount_rt_timer
,
1016 qemu_get_clock(rt_clock
) + 1000);
1020 static void icount_adjust_vm(void * opaque
)
1022 qemu_mod_timer(icount_vm_timer
,
1023 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1027 static void init_icount_adjust(void)
1029 /* Have both realtime and virtual time triggers for speed adjustment.
1030 The realtime trigger catches emulated time passing too slowly,
1031 the virtual time trigger catches emulated time passing too fast.
1032 Realtime triggers occur even when idle, so use them less frequently
1033 than VM triggers. */
1034 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1035 qemu_mod_timer(icount_rt_timer
,
1036 qemu_get_clock(rt_clock
) + 1000);
1037 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1038 qemu_mod_timer(icount_vm_timer
,
1039 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1042 static struct qemu_alarm_timer alarm_timers
[] = {
1045 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1046 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1047 /* HPET - if available - is preferred */
1048 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1049 /* ...otherwise try RTC */
1050 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1052 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1054 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1055 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1056 {"win32", 0, win32_start_timer
,
1057 win32_stop_timer
, NULL
, &alarm_win32_data
},
1062 static void show_available_alarms(void)
1066 printf("Available alarm timers, in order of precedence:\n");
1067 for (i
= 0; alarm_timers
[i
].name
; i
++)
1068 printf("%s\n", alarm_timers
[i
].name
);
1071 static void configure_alarms(char const *opt
)
1075 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1078 struct qemu_alarm_timer tmp
;
1080 if (!strcmp(opt
, "?")) {
1081 show_available_alarms();
1087 /* Reorder the array */
1088 name
= strtok(arg
, ",");
1090 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1091 if (!strcmp(alarm_timers
[i
].name
, name
))
1096 fprintf(stderr
, "Unknown clock %s\n", name
);
1105 tmp
= alarm_timers
[i
];
1106 alarm_timers
[i
] = alarm_timers
[cur
];
1107 alarm_timers
[cur
] = tmp
;
1111 name
= strtok(NULL
, ",");
1117 /* Disable remaining timers */
1118 for (i
= cur
; i
< count
; i
++)
1119 alarm_timers
[i
].name
= NULL
;
1121 show_available_alarms();
1126 QEMUClock
*rt_clock
;
1127 QEMUClock
*vm_clock
;
1129 static QEMUTimer
*active_timers
[2];
1131 static QEMUClock
*qemu_new_clock(int type
)
1134 clock
= qemu_mallocz(sizeof(QEMUClock
));
1139 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1143 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1146 ts
->opaque
= opaque
;
1150 void qemu_free_timer(QEMUTimer
*ts
)
1155 /* stop a timer, but do not dealloc it */
1156 void qemu_del_timer(QEMUTimer
*ts
)
1160 /* NOTE: this code must be signal safe because
1161 qemu_timer_expired() can be called from a signal. */
1162 pt
= &active_timers
[ts
->clock
->type
];
1175 /* modify the current timer so that it will be fired when current_time
1176 >= expire_time. The corresponding callback will be called. */
1177 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1183 /* add the timer in the sorted list */
1184 /* NOTE: this code must be signal safe because
1185 qemu_timer_expired() can be called from a signal. */
1186 pt
= &active_timers
[ts
->clock
->type
];
1191 if (t
->expire_time
> expire_time
)
1195 ts
->expire_time
= expire_time
;
1199 /* Rearm if necessary */
1200 if (pt
== &active_timers
[ts
->clock
->type
]) {
1201 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1202 qemu_rearm_alarm_timer(alarm_timer
);
1204 /* Interrupt execution to force deadline recalculation. */
1205 if (use_icount
&& cpu_single_env
) {
1206 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1211 int qemu_timer_pending(QEMUTimer
*ts
)
1214 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1221 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1225 return (timer_head
->expire_time
<= current_time
);
1228 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1234 if (!ts
|| ts
->expire_time
> current_time
)
1236 /* remove timer from the list before calling the callback */
1237 *ptimer_head
= ts
->next
;
1240 /* run the callback (the timer list can be modified) */
1245 int64_t qemu_get_clock(QEMUClock
*clock
)
1247 switch(clock
->type
) {
1248 case QEMU_TIMER_REALTIME
:
1249 return get_clock() / 1000000;
1251 case QEMU_TIMER_VIRTUAL
:
1253 return cpu_get_icount();
1255 return cpu_get_clock();
1260 static void init_timers(void)
1263 ticks_per_sec
= QEMU_TIMER_BASE
;
1264 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1265 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1269 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1271 uint64_t expire_time
;
1273 if (qemu_timer_pending(ts
)) {
1274 expire_time
= ts
->expire_time
;
1278 qemu_put_be64(f
, expire_time
);
1281 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1283 uint64_t expire_time
;
1285 expire_time
= qemu_get_be64(f
);
1286 if (expire_time
!= -1) {
1287 qemu_mod_timer(ts
, expire_time
);
1293 static void timer_save(QEMUFile
*f
, void *opaque
)
1295 if (cpu_ticks_enabled
) {
1296 hw_error("cannot save state if virtual timers are running");
1298 qemu_put_be64(f
, cpu_ticks_offset
);
1299 qemu_put_be64(f
, ticks_per_sec
);
1300 qemu_put_be64(f
, cpu_clock_offset
);
1303 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1305 if (version_id
!= 1 && version_id
!= 2)
1307 if (cpu_ticks_enabled
) {
1310 cpu_ticks_offset
=qemu_get_be64(f
);
1311 ticks_per_sec
=qemu_get_be64(f
);
1312 if (version_id
== 2) {
1313 cpu_clock_offset
=qemu_get_be64(f
);
1319 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1320 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1322 static void host_alarm_handler(int host_signum
)
1326 #define DISP_FREQ 1000
1328 static int64_t delta_min
= INT64_MAX
;
1329 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1331 ti
= qemu_get_clock(vm_clock
);
1332 if (last_clock
!= 0) {
1333 delta
= ti
- last_clock
;
1334 if (delta
< delta_min
)
1336 if (delta
> delta_max
)
1339 if (++count
== DISP_FREQ
) {
1340 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1341 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1342 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1343 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1344 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1346 delta_min
= INT64_MAX
;
1355 alarm_has_dynticks(alarm_timer
) ||
1357 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1358 qemu_get_clock(vm_clock
))) ||
1359 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1360 qemu_get_clock(rt_clock
))) {
1361 CPUState
*env
= next_cpu
;
1364 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1365 SetEvent(data
->host_alarm
);
1367 static const char byte
= 0;
1368 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1370 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1373 /* stop the currently executing cpu because a timer occured */
1374 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1376 if (env
->kqemu_enabled
) {
1377 kqemu_cpu_interrupt(env
);
1385 static int64_t qemu_next_deadline(void)
1389 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1390 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1391 qemu_get_clock(vm_clock
);
1393 /* To avoid problems with overflow limit this to 2^32. */
1403 #if defined(__linux__) || defined(_WIN32)
1404 static uint64_t qemu_next_deadline_dyntick(void)
1412 delta
= (qemu_next_deadline() + 999) / 1000;
1414 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1415 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1416 qemu_get_clock(rt_clock
))*1000;
1417 if (rtdelta
< delta
)
1421 if (delta
< MIN_TIMER_REARM_US
)
1422 delta
= MIN_TIMER_REARM_US
;
1430 /* Sets a specific flag */
1431 static int fcntl_setfl(int fd
, int flag
)
1435 flags
= fcntl(fd
, F_GETFL
);
1439 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1445 #if defined(__linux__)
1447 #define RTC_FREQ 1024
1449 static void enable_sigio_timer(int fd
)
1451 struct sigaction act
;
1454 sigfillset(&act
.sa_mask
);
1456 act
.sa_handler
= host_alarm_handler
;
1458 sigaction(SIGIO
, &act
, NULL
);
1459 fcntl_setfl(fd
, O_ASYNC
);
1460 fcntl(fd
, F_SETOWN
, getpid());
1463 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1465 struct hpet_info info
;
1468 fd
= open("/dev/hpet", O_RDONLY
);
1473 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1475 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1476 "error, but for better emulation accuracy type:\n"
1477 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1481 /* Check capabilities */
1482 r
= ioctl(fd
, HPET_INFO
, &info
);
1486 /* Enable periodic mode */
1487 r
= ioctl(fd
, HPET_EPI
, 0);
1488 if (info
.hi_flags
&& (r
< 0))
1491 /* Enable interrupt */
1492 r
= ioctl(fd
, HPET_IE_ON
, 0);
1496 enable_sigio_timer(fd
);
1497 t
->priv
= (void *)(long)fd
;
1505 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1507 int fd
= (long)t
->priv
;
1512 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1515 unsigned long current_rtc_freq
= 0;
1517 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1520 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1521 if (current_rtc_freq
!= RTC_FREQ
&&
1522 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1523 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1524 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1525 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1528 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1534 enable_sigio_timer(rtc_fd
);
1536 t
->priv
= (void *)(long)rtc_fd
;
1541 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1543 int rtc_fd
= (long)t
->priv
;
1548 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1552 struct sigaction act
;
1554 sigfillset(&act
.sa_mask
);
1556 act
.sa_handler
= host_alarm_handler
;
1558 sigaction(SIGALRM
, &act
, NULL
);
1561 * Initialize ev struct to 0 to avoid valgrind complaining
1562 * about uninitialized data in timer_create call
1564 memset(&ev
, 0, sizeof(ev
));
1565 ev
.sigev_value
.sival_int
= 0;
1566 ev
.sigev_notify
= SIGEV_SIGNAL
;
1567 ev
.sigev_signo
= SIGALRM
;
1569 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1570 perror("timer_create");
1572 /* disable dynticks */
1573 fprintf(stderr
, "Dynamic Ticks disabled\n");
1578 t
->priv
= (void *)(long)host_timer
;
1583 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1585 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1587 timer_delete(host_timer
);
1590 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1592 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1593 struct itimerspec timeout
;
1594 int64_t nearest_delta_us
= INT64_MAX
;
1597 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1598 !active_timers
[QEMU_TIMER_VIRTUAL
])
1601 nearest_delta_us
= qemu_next_deadline_dyntick();
1603 /* check whether a timer is already running */
1604 if (timer_gettime(host_timer
, &timeout
)) {
1606 fprintf(stderr
, "Internal timer error: aborting\n");
1609 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1610 if (current_us
&& current_us
<= nearest_delta_us
)
1613 timeout
.it_interval
.tv_sec
= 0;
1614 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1615 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1616 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1617 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1619 fprintf(stderr
, "Internal timer error: aborting\n");
1624 #endif /* defined(__linux__) */
1626 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1628 struct sigaction act
;
1629 struct itimerval itv
;
1633 sigfillset(&act
.sa_mask
);
1635 act
.sa_handler
= host_alarm_handler
;
1637 sigaction(SIGALRM
, &act
, NULL
);
1639 itv
.it_interval
.tv_sec
= 0;
1640 /* for i386 kernel 2.6 to get 1 ms */
1641 itv
.it_interval
.tv_usec
= 999;
1642 itv
.it_value
.tv_sec
= 0;
1643 itv
.it_value
.tv_usec
= 10 * 1000;
1645 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1652 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1654 struct itimerval itv
;
1656 memset(&itv
, 0, sizeof(itv
));
1657 setitimer(ITIMER_REAL
, &itv
, NULL
);
1660 #endif /* !defined(_WIN32) */
1662 static void try_to_rearm_timer(void *opaque
)
1664 struct qemu_alarm_timer
*t
= opaque
;
1668 /* Drain the notify pipe */
1671 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1672 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1675 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1676 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1677 qemu_rearm_alarm_timer(alarm_timer
);
1683 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1686 struct qemu_alarm_win32
*data
= t
->priv
;
1689 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1690 if (!data
->host_alarm
) {
1691 perror("Failed CreateEvent");
1695 memset(&tc
, 0, sizeof(tc
));
1696 timeGetDevCaps(&tc
, sizeof(tc
));
1698 if (data
->period
< tc
.wPeriodMin
)
1699 data
->period
= tc
.wPeriodMin
;
1701 timeBeginPeriod(data
->period
);
1703 flags
= TIME_CALLBACK_FUNCTION
;
1704 if (alarm_has_dynticks(t
))
1705 flags
|= TIME_ONESHOT
;
1707 flags
|= TIME_PERIODIC
;
1709 data
->timerId
= timeSetEvent(1, // interval (ms)
1710 data
->period
, // resolution
1711 host_alarm_handler
, // function
1712 (DWORD
)t
, // parameter
1715 if (!data
->timerId
) {
1716 perror("Failed to initialize win32 alarm timer");
1718 timeEndPeriod(data
->period
);
1719 CloseHandle(data
->host_alarm
);
1723 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1728 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1730 struct qemu_alarm_win32
*data
= t
->priv
;
1732 timeKillEvent(data
->timerId
);
1733 timeEndPeriod(data
->period
);
1735 CloseHandle(data
->host_alarm
);
1738 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1740 struct qemu_alarm_win32
*data
= t
->priv
;
1741 uint64_t nearest_delta_us
;
1743 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1744 !active_timers
[QEMU_TIMER_VIRTUAL
])
1747 nearest_delta_us
= qemu_next_deadline_dyntick();
1748 nearest_delta_us
/= 1000;
1750 timeKillEvent(data
->timerId
);
1752 data
->timerId
= timeSetEvent(1,
1756 TIME_ONESHOT
| TIME_PERIODIC
);
1758 if (!data
->timerId
) {
1759 perror("Failed to re-arm win32 alarm timer");
1761 timeEndPeriod(data
->period
);
1762 CloseHandle(data
->host_alarm
);
1769 static int init_timer_alarm(void)
1771 struct qemu_alarm_timer
*t
= NULL
;
1781 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1785 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1789 alarm_timer_rfd
= fds
[0];
1790 alarm_timer_wfd
= fds
[1];
1793 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1794 t
= &alarm_timers
[i
];
1807 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1808 try_to_rearm_timer
, NULL
, t
);
1823 static void quit_timers(void)
1825 alarm_timer
->stop(alarm_timer
);
1829 /***********************************************************/
1830 /* host time/date access */
1831 void qemu_get_timedate(struct tm
*tm
, int offset
)
1838 if (rtc_date_offset
== -1) {
1842 ret
= localtime(&ti
);
1844 ti
-= rtc_date_offset
;
1848 memcpy(tm
, ret
, sizeof(struct tm
));
1851 int qemu_timedate_diff(struct tm
*tm
)
1855 if (rtc_date_offset
== -1)
1857 seconds
= mktimegm(tm
);
1859 seconds
= mktime(tm
);
1861 seconds
= mktimegm(tm
) + rtc_date_offset
;
1863 return seconds
- time(NULL
);
1867 static void socket_cleanup(void)
1872 static int socket_init(void)
1877 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1879 err
= WSAGetLastError();
1880 fprintf(stderr
, "WSAStartup: %d\n", err
);
1883 atexit(socket_cleanup
);
1888 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1893 while (*p
!= '\0' && *p
!= '=') {
1894 if (q
&& (q
- buf
) < buf_size
- 1)
1904 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1909 while (*p
!= '\0') {
1911 if (*(p
+ 1) != ',')
1915 if (q
&& (q
- buf
) < buf_size
- 1)
1925 int get_param_value(char *buf
, int buf_size
,
1926 const char *tag
, const char *str
)
1933 p
= get_opt_name(option
, sizeof(option
), p
);
1937 if (!strcmp(tag
, option
)) {
1938 (void)get_opt_value(buf
, buf_size
, p
);
1941 p
= get_opt_value(NULL
, 0, p
);
1950 int check_params(char *buf
, int buf_size
,
1951 const char * const *params
, const char *str
)
1958 p
= get_opt_name(buf
, buf_size
, p
);
1962 for(i
= 0; params
[i
] != NULL
; i
++)
1963 if (!strcmp(params
[i
], buf
))
1965 if (params
[i
] == NULL
)
1967 p
= get_opt_value(NULL
, 0, p
);
1975 /***********************************************************/
1976 /* Bluetooth support */
1979 static struct HCIInfo
*hci_table
[MAX_NICS
];
1981 static struct bt_vlan_s
{
1982 struct bt_scatternet_s net
;
1984 struct bt_vlan_s
*next
;
1987 /* find or alloc a new bluetooth "VLAN" */
1988 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1990 struct bt_vlan_s
**pvlan
, *vlan
;
1991 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1995 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1997 pvlan
= &first_bt_vlan
;
1998 while (*pvlan
!= NULL
)
1999 pvlan
= &(*pvlan
)->next
;
2004 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2008 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2013 static struct HCIInfo null_hci
= {
2014 .cmd_send
= null_hci_send
,
2015 .sco_send
= null_hci_send
,
2016 .acl_send
= null_hci_send
,
2017 .bdaddr_set
= null_hci_addr_set
,
2020 struct HCIInfo
*qemu_next_hci(void)
2022 if (cur_hci
== nb_hcis
)
2025 return hci_table
[cur_hci
++];
2028 static struct HCIInfo
*hci_init(const char *str
)
2031 struct bt_scatternet_s
*vlan
= 0;
2033 if (!strcmp(str
, "null"))
2036 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2038 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2039 else if (!strncmp(str
, "hci", 3)) {
2042 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2043 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2048 vlan
= qemu_find_bt_vlan(0);
2050 return bt_new_hci(vlan
);
2053 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2058 static int bt_hci_parse(const char *str
)
2060 struct HCIInfo
*hci
;
2063 if (nb_hcis
>= MAX_NICS
) {
2064 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2068 hci
= hci_init(str
);
2077 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2078 hci
->bdaddr_set(hci
, bdaddr
.b
);
2080 hci_table
[nb_hcis
++] = hci
;
2085 static void bt_vhci_add(int vlan_id
)
2087 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2090 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2091 "an empty scatternet %i\n", vlan_id
);
2093 bt_vhci_init(bt_new_hci(vlan
));
2096 static struct bt_device_s
*bt_device_add(const char *opt
)
2098 struct bt_scatternet_s
*vlan
;
2100 char *endp
= strstr(opt
, ",vlan=");
2101 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2104 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2107 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2109 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2114 vlan
= qemu_find_bt_vlan(vlan_id
);
2117 fprintf(stderr
, "qemu: warning: adding a slave device to "
2118 "an empty scatternet %i\n", vlan_id
);
2120 if (!strcmp(devname
, "keyboard"))
2121 return bt_keyboard_init(vlan
);
2123 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2127 static int bt_parse(const char *opt
)
2129 const char *endp
, *p
;
2132 if (strstart(opt
, "hci", &endp
)) {
2133 if (!*endp
|| *endp
== ',') {
2135 if (!strstart(endp
, ",vlan=", 0))
2138 return bt_hci_parse(opt
);
2140 } else if (strstart(opt
, "vhci", &endp
)) {
2141 if (!*endp
|| *endp
== ',') {
2143 if (strstart(endp
, ",vlan=", &p
)) {
2144 vlan
= strtol(p
, (char **) &endp
, 0);
2146 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2150 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2159 } else if (strstart(opt
, "device:", &endp
))
2160 return !bt_device_add(endp
);
2162 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2166 /***********************************************************/
2167 /* QEMU Block devices */
2169 #define HD_ALIAS "index=%d,media=disk"
2171 #define CDROM_ALIAS "index=1,media=cdrom"
2173 #define CDROM_ALIAS "index=2,media=cdrom"
2175 #define FD_ALIAS "index=%d,if=floppy"
2176 #define PFLASH_ALIAS "if=pflash"
2177 #define MTD_ALIAS "if=mtd"
2178 #define SD_ALIAS "index=0,if=sd"
2180 static int drive_opt_get_free_idx(void)
2184 for (index
= 0; index
< MAX_DRIVES
; index
++)
2185 if (!drives_opt
[index
].used
) {
2186 drives_opt
[index
].used
= 1;
2193 static int drive_get_free_idx(void)
2197 for (index
= 0; index
< MAX_DRIVES
; index
++)
2198 if (!drives_table
[index
].used
) {
2199 drives_table
[index
].used
= 1;
2206 int drive_add(const char *file
, const char *fmt
, ...)
2209 int index
= drive_opt_get_free_idx();
2211 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2212 fprintf(stderr
, "qemu: too many drives\n");
2216 drives_opt
[index
].file
= file
;
2218 vsnprintf(drives_opt
[index
].opt
,
2219 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2226 void drive_remove(int index
)
2228 drives_opt
[index
].used
= 0;
2232 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2236 /* seek interface, bus and unit */
2238 for (index
= 0; index
< MAX_DRIVES
; index
++)
2239 if (drives_table
[index
].type
== type
&&
2240 drives_table
[index
].bus
== bus
&&
2241 drives_table
[index
].unit
== unit
&&
2242 drives_table
[index
].used
)
2248 int drive_get_max_bus(BlockInterfaceType type
)
2254 for (index
= 0; index
< nb_drives
; index
++) {
2255 if(drives_table
[index
].type
== type
&&
2256 drives_table
[index
].bus
> max_bus
)
2257 max_bus
= drives_table
[index
].bus
;
2262 const char *drive_get_serial(BlockDriverState
*bdrv
)
2266 for (index
= 0; index
< nb_drives
; index
++)
2267 if (drives_table
[index
].bdrv
== bdrv
)
2268 return drives_table
[index
].serial
;
2273 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2277 for (index
= 0; index
< nb_drives
; index
++)
2278 if (drives_table
[index
].bdrv
== bdrv
)
2279 return drives_table
[index
].onerror
;
2281 return BLOCK_ERR_STOP_ENOSPC
;
2284 static void bdrv_format_print(void *opaque
, const char *name
)
2286 fprintf(stderr
, " %s", name
);
2289 void drive_uninit(BlockDriverState
*bdrv
)
2293 for (i
= 0; i
< MAX_DRIVES
; i
++)
2294 if (drives_table
[i
].bdrv
== bdrv
) {
2295 drives_table
[i
].bdrv
= NULL
;
2296 drives_table
[i
].used
= 0;
2297 drive_remove(drives_table
[i
].drive_opt_idx
);
2303 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2309 const char *mediastr
= "";
2310 BlockInterfaceType type
;
2311 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2312 int bus_id
, unit_id
;
2313 int cyls
, heads
, secs
, translation
;
2314 BlockDriverState
*bdrv
;
2315 BlockDriver
*drv
= NULL
;
2316 QEMUMachine
*machine
= opaque
;
2320 int bdrv_flags
, onerror
;
2321 int drives_table_idx
;
2322 char *str
= arg
->opt
;
2323 static const char * const params
[] = { "bus", "unit", "if", "index",
2324 "cyls", "heads", "secs", "trans",
2325 "media", "snapshot", "file",
2326 "cache", "format", "serial", "werror",
2329 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2330 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2336 cyls
= heads
= secs
= 0;
2339 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2343 if (machine
->use_scsi
) {
2345 max_devs
= MAX_SCSI_DEVS
;
2346 pstrcpy(devname
, sizeof(devname
), "scsi");
2349 max_devs
= MAX_IDE_DEVS
;
2350 pstrcpy(devname
, sizeof(devname
), "ide");
2354 /* extract parameters */
2356 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2357 bus_id
= strtol(buf
, NULL
, 0);
2359 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2364 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2365 unit_id
= strtol(buf
, NULL
, 0);
2367 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2372 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2373 pstrcpy(devname
, sizeof(devname
), buf
);
2374 if (!strcmp(buf
, "ide")) {
2376 max_devs
= MAX_IDE_DEVS
;
2377 } else if (!strcmp(buf
, "scsi")) {
2379 max_devs
= MAX_SCSI_DEVS
;
2380 } else if (!strcmp(buf
, "floppy")) {
2383 } else if (!strcmp(buf
, "pflash")) {
2386 } else if (!strcmp(buf
, "mtd")) {
2389 } else if (!strcmp(buf
, "sd")) {
2392 } else if (!strcmp(buf
, "virtio")) {
2396 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2401 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2402 index
= strtol(buf
, NULL
, 0);
2404 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2409 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2410 cyls
= strtol(buf
, NULL
, 0);
2413 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2414 heads
= strtol(buf
, NULL
, 0);
2417 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2418 secs
= strtol(buf
, NULL
, 0);
2421 if (cyls
|| heads
|| secs
) {
2422 if (cyls
< 1 || cyls
> 16383) {
2423 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2426 if (heads
< 1 || heads
> 16) {
2427 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2430 if (secs
< 1 || secs
> 63) {
2431 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2436 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2439 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2443 if (!strcmp(buf
, "none"))
2444 translation
= BIOS_ATA_TRANSLATION_NONE
;
2445 else if (!strcmp(buf
, "lba"))
2446 translation
= BIOS_ATA_TRANSLATION_LBA
;
2447 else if (!strcmp(buf
, "auto"))
2448 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2450 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2455 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2456 if (!strcmp(buf
, "disk")) {
2458 } else if (!strcmp(buf
, "cdrom")) {
2459 if (cyls
|| secs
|| heads
) {
2461 "qemu: '%s' invalid physical CHS format\n", str
);
2464 media
= MEDIA_CDROM
;
2466 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2471 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2472 if (!strcmp(buf
, "on"))
2474 else if (!strcmp(buf
, "off"))
2477 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2482 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2483 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2485 else if (!strcmp(buf
, "writethrough"))
2487 else if (!strcmp(buf
, "writeback"))
2490 fprintf(stderr
, "qemu: invalid cache option\n");
2495 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2496 if (strcmp(buf
, "?") == 0) {
2497 fprintf(stderr
, "qemu: Supported formats:");
2498 bdrv_iterate_format(bdrv_format_print
, NULL
);
2499 fprintf(stderr
, "\n");
2502 drv
= bdrv_find_format(buf
);
2504 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2509 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2510 if (!strcmp(buf
, "on")) {
2511 if (extboot_drive
!= -1) {
2512 fprintf(stderr
, "qemu: two bootable drives specified\n");
2515 extboot_drive
= nb_drives
;
2516 } else if (strcmp(buf
, "off")) {
2517 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2522 if (arg
->file
== NULL
)
2523 get_param_value(file
, sizeof(file
), "file", str
);
2525 pstrcpy(file
, sizeof(file
), arg
->file
);
2527 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2528 memset(serial
, 0, sizeof(serial
));
2530 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2531 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2532 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2533 fprintf(stderr
, "werror is no supported by this format\n");
2536 if (!strcmp(buf
, "ignore"))
2537 onerror
= BLOCK_ERR_IGNORE
;
2538 else if (!strcmp(buf
, "enospc"))
2539 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2540 else if (!strcmp(buf
, "stop"))
2541 onerror
= BLOCK_ERR_STOP_ANY
;
2542 else if (!strcmp(buf
, "report"))
2543 onerror
= BLOCK_ERR_REPORT
;
2545 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2550 /* compute bus and unit according index */
2553 if (bus_id
!= 0 || unit_id
!= -1) {
2555 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2563 unit_id
= index
% max_devs
;
2564 bus_id
= index
/ max_devs
;
2568 /* if user doesn't specify a unit_id,
2569 * try to find the first free
2572 if (unit_id
== -1) {
2574 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2576 if (max_devs
&& unit_id
>= max_devs
) {
2577 unit_id
-= max_devs
;
2585 if (max_devs
&& unit_id
>= max_devs
) {
2586 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2587 str
, unit_id
, max_devs
- 1);
2592 * ignore multiple definitions
2595 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2600 if (type
== IF_IDE
|| type
== IF_SCSI
)
2601 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2603 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2604 devname
, bus_id
, mediastr
, unit_id
);
2606 snprintf(buf
, sizeof(buf
), "%s%s%i",
2607 devname
, mediastr
, unit_id
);
2608 bdrv
= bdrv_new(buf
);
2609 drives_table_idx
= drive_get_free_idx();
2610 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2611 drives_table
[drives_table_idx
].type
= type
;
2612 drives_table
[drives_table_idx
].bus
= bus_id
;
2613 drives_table
[drives_table_idx
].unit
= unit_id
;
2614 drives_table
[drives_table_idx
].onerror
= onerror
;
2615 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2616 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2625 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2626 bdrv_set_translation_hint(bdrv
, translation
);
2630 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2635 /* FIXME: This isn't really a floppy, but it's a reasonable
2638 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2649 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2650 cache
= 2; /* always use write-back with snapshot */
2652 if (cache
== 0) /* no caching */
2653 bdrv_flags
|= BDRV_O_NOCACHE
;
2654 else if (cache
== 2) /* write-back */
2655 bdrv_flags
|= BDRV_O_CACHE_WB
;
2656 else if (cache
== 3) /* not specified */
2657 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2658 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2659 fprintf(stderr
, "qemu: could not open disk image %s\n",
2663 if (bdrv_key_required(bdrv
))
2665 return drives_table_idx
;
2668 /***********************************************************/
2671 static USBPort
*used_usb_ports
;
2672 static USBPort
*free_usb_ports
;
2674 /* ??? Maybe change this to register a hub to keep track of the topology. */
2675 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2676 usb_attachfn attach
)
2678 port
->opaque
= opaque
;
2679 port
->index
= index
;
2680 port
->attach
= attach
;
2681 port
->next
= free_usb_ports
;
2682 free_usb_ports
= port
;
2685 int usb_device_add_dev(USBDevice
*dev
)
2689 /* Find a USB port to add the device to. */
2690 port
= free_usb_ports
;
2694 /* Create a new hub and chain it on. */
2695 free_usb_ports
= NULL
;
2696 port
->next
= used_usb_ports
;
2697 used_usb_ports
= port
;
2699 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2700 usb_attach(port
, hub
);
2701 port
= free_usb_ports
;
2704 free_usb_ports
= port
->next
;
2705 port
->next
= used_usb_ports
;
2706 used_usb_ports
= port
;
2707 usb_attach(port
, dev
);
2711 static int usb_device_add(const char *devname
, int is_hotplug
)
2716 if (!free_usb_ports
)
2719 if (strstart(devname
, "host:", &p
)) {
2720 dev
= usb_host_device_open(p
);
2721 } else if (!strcmp(devname
, "mouse")) {
2722 dev
= usb_mouse_init();
2723 } else if (!strcmp(devname
, "tablet")) {
2724 dev
= usb_tablet_init();
2725 } else if (!strcmp(devname
, "keyboard")) {
2726 dev
= usb_keyboard_init();
2727 } else if (strstart(devname
, "disk:", &p
)) {
2728 BlockDriverState
*bs
;
2730 dev
= usb_msd_init(p
, &bs
);
2733 if (bdrv_key_required(bs
)) {
2735 if (is_hotplug
&& monitor_read_bdrv_key(bs
) < 0) {
2736 dev
->handle_destroy(dev
);
2740 } else if (!strcmp(devname
, "wacom-tablet")) {
2741 dev
= usb_wacom_init();
2742 } else if (strstart(devname
, "serial:", &p
)) {
2743 dev
= usb_serial_init(p
);
2744 #ifdef CONFIG_BRLAPI
2745 } else if (!strcmp(devname
, "braille")) {
2746 dev
= usb_baum_init();
2748 } else if (strstart(devname
, "net:", &p
)) {
2751 if (net_client_init("nic", p
) < 0)
2753 nd_table
[nic
].model
= "usb";
2754 dev
= usb_net_init(&nd_table
[nic
]);
2755 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2756 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2757 bt_new_hci(qemu_find_bt_vlan(0)));
2764 return usb_device_add_dev(dev
);
2767 int usb_device_del_addr(int bus_num
, int addr
)
2773 if (!used_usb_ports
)
2779 lastp
= &used_usb_ports
;
2780 port
= used_usb_ports
;
2781 while (port
&& port
->dev
->addr
!= addr
) {
2782 lastp
= &port
->next
;
2790 *lastp
= port
->next
;
2791 usb_attach(port
, NULL
);
2792 dev
->handle_destroy(dev
);
2793 port
->next
= free_usb_ports
;
2794 free_usb_ports
= port
;
2798 static int usb_device_del(const char *devname
)
2803 if (strstart(devname
, "host:", &p
))
2804 return usb_host_device_close(p
);
2806 if (!used_usb_ports
)
2809 p
= strchr(devname
, '.');
2812 bus_num
= strtoul(devname
, NULL
, 0);
2813 addr
= strtoul(p
+ 1, NULL
, 0);
2815 return usb_device_del_addr(bus_num
, addr
);
2818 void do_usb_add(const char *devname
)
2820 usb_device_add(devname
, 1);
2823 void do_usb_del(const char *devname
)
2825 usb_device_del(devname
);
2832 const char *speed_str
;
2835 term_printf("USB support not enabled\n");
2839 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2843 switch(dev
->speed
) {
2847 case USB_SPEED_FULL
:
2850 case USB_SPEED_HIGH
:
2857 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2858 0, dev
->addr
, speed_str
, dev
->devname
);
2862 /***********************************************************/
2863 /* PCMCIA/Cardbus */
2865 static struct pcmcia_socket_entry_s
{
2866 struct pcmcia_socket_s
*socket
;
2867 struct pcmcia_socket_entry_s
*next
;
2868 } *pcmcia_sockets
= 0;
2870 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2872 struct pcmcia_socket_entry_s
*entry
;
2874 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2875 entry
->socket
= socket
;
2876 entry
->next
= pcmcia_sockets
;
2877 pcmcia_sockets
= entry
;
2880 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2882 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2884 ptr
= &pcmcia_sockets
;
2885 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2886 if (entry
->socket
== socket
) {
2892 void pcmcia_info(void)
2894 struct pcmcia_socket_entry_s
*iter
;
2895 if (!pcmcia_sockets
)
2896 term_printf("No PCMCIA sockets\n");
2898 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2899 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2900 iter
->socket
->attached
? iter
->socket
->card_string
:
2904 /***********************************************************/
2905 /* register display */
2907 void register_displaystate(DisplayState
*ds
)
2917 DisplayState
*get_displaystate(void)
2919 return display_state
;
2924 static void dumb_display_init(void)
2926 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2927 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2928 register_displaystate(ds
);
2931 /***********************************************************/
2934 #define MAX_IO_HANDLERS 64
2936 typedef struct IOHandlerRecord
{
2938 IOCanRWHandler
*fd_read_poll
;
2940 IOHandler
*fd_write
;
2943 /* temporary data */
2945 struct IOHandlerRecord
*next
;
2948 static IOHandlerRecord
*first_io_handler
;
2950 /* XXX: fd_read_poll should be suppressed, but an API change is
2951 necessary in the character devices to suppress fd_can_read(). */
2952 int qemu_set_fd_handler2(int fd
,
2953 IOCanRWHandler
*fd_read_poll
,
2955 IOHandler
*fd_write
,
2958 IOHandlerRecord
**pioh
, *ioh
;
2960 if (!fd_read
&& !fd_write
) {
2961 pioh
= &first_io_handler
;
2966 if (ioh
->fd
== fd
) {
2973 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2977 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2978 ioh
->next
= first_io_handler
;
2979 first_io_handler
= ioh
;
2982 ioh
->fd_read_poll
= fd_read_poll
;
2983 ioh
->fd_read
= fd_read
;
2984 ioh
->fd_write
= fd_write
;
2985 ioh
->opaque
= opaque
;
2992 int qemu_set_fd_handler(int fd
,
2994 IOHandler
*fd_write
,
2997 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3001 /***********************************************************/
3002 /* Polling handling */
3004 typedef struct PollingEntry
{
3007 struct PollingEntry
*next
;
3010 static PollingEntry
*first_polling_entry
;
3012 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3014 PollingEntry
**ppe
, *pe
;
3015 pe
= qemu_mallocz(sizeof(PollingEntry
));
3017 pe
->opaque
= opaque
;
3018 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3023 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3025 PollingEntry
**ppe
, *pe
;
3026 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3028 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3036 /***********************************************************/
3037 /* Wait objects support */
3038 typedef struct WaitObjects
{
3040 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3041 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3042 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3045 static WaitObjects wait_objects
= {0};
3047 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3049 WaitObjects
*w
= &wait_objects
;
3051 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3053 w
->events
[w
->num
] = handle
;
3054 w
->func
[w
->num
] = func
;
3055 w
->opaque
[w
->num
] = opaque
;
3060 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3063 WaitObjects
*w
= &wait_objects
;
3066 for (i
= 0; i
< w
->num
; i
++) {
3067 if (w
->events
[i
] == handle
)
3070 w
->events
[i
] = w
->events
[i
+ 1];
3071 w
->func
[i
] = w
->func
[i
+ 1];
3072 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3080 /***********************************************************/
3081 /* ram save/restore */
3083 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3087 v
= qemu_get_byte(f
);
3090 if (qemu_get_buffer(f
, buf
, len
) != len
)
3094 v
= qemu_get_byte(f
);
3095 memset(buf
, v
, len
);
3101 if (qemu_file_has_error(f
))
3107 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3112 if (qemu_get_be32(f
) != phys_ram_size
)
3114 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3115 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3117 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3124 #define BDRV_HASH_BLOCK_SIZE 1024
3125 #define IOBUF_SIZE 4096
3126 #define RAM_CBLOCK_MAGIC 0xfabe
3128 typedef struct RamDecompressState
{
3131 uint8_t buf
[IOBUF_SIZE
];
3132 } RamDecompressState
;
3134 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3137 memset(s
, 0, sizeof(*s
));
3139 ret
= inflateInit(&s
->zstream
);
3145 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3149 s
->zstream
.avail_out
= len
;
3150 s
->zstream
.next_out
= buf
;
3151 while (s
->zstream
.avail_out
> 0) {
3152 if (s
->zstream
.avail_in
== 0) {
3153 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3155 clen
= qemu_get_be16(s
->f
);
3156 if (clen
> IOBUF_SIZE
)
3158 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3159 s
->zstream
.avail_in
= clen
;
3160 s
->zstream
.next_in
= s
->buf
;
3162 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3163 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3170 static void ram_decompress_close(RamDecompressState
*s
)
3172 inflateEnd(&s
->zstream
);
3175 #define RAM_SAVE_FLAG_FULL 0x01
3176 #define RAM_SAVE_FLAG_COMPRESS 0x02
3177 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3178 #define RAM_SAVE_FLAG_PAGE 0x08
3179 #define RAM_SAVE_FLAG_EOS 0x10
3181 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3183 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3184 uint32_t *array
= (uint32_t *)page
;
3187 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3188 if (array
[i
] != val
)
3195 static int ram_save_block(QEMUFile
*f
)
3197 static ram_addr_t current_addr
= 0;
3198 ram_addr_t saved_addr
= current_addr
;
3199 ram_addr_t addr
= 0;
3202 while (addr
< phys_ram_size
) {
3203 if (kvm_enabled() && current_addr
== 0) {
3205 r
= kvm_update_dirty_pages_log();
3207 fprintf(stderr
, "%s: update dirty pages log failed %d\n", __FUNCTION__
, r
);
3208 qemu_file_set_error(f
);
3212 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3215 cpu_physical_memory_reset_dirty(current_addr
,
3216 current_addr
+ TARGET_PAGE_SIZE
,
3217 MIGRATION_DIRTY_FLAG
);
3219 ch
= *(phys_ram_base
+ current_addr
);
3221 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3222 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3223 qemu_put_byte(f
, ch
);
3225 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3226 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3232 addr
+= TARGET_PAGE_SIZE
;
3233 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3239 static ram_addr_t ram_save_threshold
= 10;
3241 static ram_addr_t
ram_save_remaining(void)
3244 ram_addr_t count
= 0;
3246 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3247 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3254 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3259 /* Make sure all dirty bits are set */
3260 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3261 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3262 cpu_physical_memory_set_dirty(addr
);
3265 /* Enable dirty memory tracking */
3266 cpu_physical_memory_set_dirty_tracking(1);
3268 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3271 while (!qemu_file_rate_limit(f
)) {
3274 ret
= ram_save_block(f
);
3275 if (ret
== 0) /* no more blocks */
3279 /* try transferring iterative blocks of memory */
3283 /* flush all remaining blocks regardless of rate limiting */
3284 while (ram_save_block(f
) != 0);
3285 cpu_physical_memory_set_dirty_tracking(0);
3288 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3290 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3293 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3295 RamDecompressState s1
, *s
= &s1
;
3299 if (ram_decompress_open(s
, f
) < 0)
3301 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3302 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3304 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3305 fprintf(stderr
, "Error while reading ram block header\n");
3309 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3310 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3315 printf("Error block header\n");
3319 ram_decompress_close(s
);
3324 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3329 if (version_id
== 1)
3330 return ram_load_v1(f
, opaque
);
3332 if (version_id
== 2) {
3333 if (qemu_get_be32(f
) != phys_ram_size
)
3335 return ram_load_dead(f
, opaque
);
3338 if (version_id
!= 3)
3342 addr
= qemu_get_be64(f
);
3344 flags
= addr
& ~TARGET_PAGE_MASK
;
3345 addr
&= TARGET_PAGE_MASK
;
3347 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3348 if (addr
!= phys_ram_size
)
3352 if (flags
& RAM_SAVE_FLAG_FULL
) {
3353 if (ram_load_dead(f
, opaque
) < 0)
3357 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3358 uint8_t ch
= qemu_get_byte(f
);
3359 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3360 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3361 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3362 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3367 /***********************************************************/
3368 /* bottom halves (can be seen as timers which expire ASAP) */
3379 static QEMUBH
*first_bh
= NULL
;
3381 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3384 bh
= qemu_mallocz(sizeof(QEMUBH
));
3386 bh
->opaque
= opaque
;
3387 bh
->next
= first_bh
;
3392 int qemu_bh_poll(void)
3398 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3399 if (!bh
->deleted
&& bh
->scheduled
) {
3408 /* remove deleted bhs */
3422 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3430 void qemu_bh_schedule(QEMUBH
*bh
)
3432 CPUState
*env
= cpu_single_env
;
3437 /* stop the currently executing CPU to execute the BH ASAP */
3439 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3444 void qemu_bh_cancel(QEMUBH
*bh
)
3449 void qemu_bh_delete(QEMUBH
*bh
)
3455 static void qemu_bh_update_timeout(int *timeout
)
3459 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3460 if (!bh
->deleted
&& bh
->scheduled
) {
3462 /* idle bottom halves will be polled at least
3464 *timeout
= MIN(10, *timeout
);
3466 /* non-idle bottom halves will be executed
3475 /***********************************************************/
3476 /* machine registration */
3478 static QEMUMachine
*first_machine
= NULL
;
3479 QEMUMachine
*current_machine
= NULL
;
3481 int qemu_register_machine(QEMUMachine
*m
)
3484 pm
= &first_machine
;
3492 static QEMUMachine
*find_machine(const char *name
)
3496 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3497 if (!strcmp(m
->name
, name
))
3503 /***********************************************************/
3504 /* main execution loop */
3506 static void gui_update(void *opaque
)
3508 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3509 DisplayState
*ds
= opaque
;
3510 DisplayChangeListener
*dcl
= ds
->listeners
;
3514 while (dcl
!= NULL
) {
3515 if (dcl
->gui_timer_interval
&&
3516 dcl
->gui_timer_interval
< interval
)
3517 interval
= dcl
->gui_timer_interval
;
3520 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3523 static void nographic_update(void *opaque
)
3525 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3527 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3530 struct vm_change_state_entry
{
3531 VMChangeStateHandler
*cb
;
3533 LIST_ENTRY (vm_change_state_entry
) entries
;
3536 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3538 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3541 VMChangeStateEntry
*e
;
3543 e
= qemu_mallocz(sizeof (*e
));
3547 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3551 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3553 LIST_REMOVE (e
, entries
);
3557 static void vm_state_notify(int running
, int reason
)
3559 VMChangeStateEntry
*e
;
3561 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3562 e
->cb(e
->opaque
, running
, reason
);
3571 vm_state_notify(1, 0);
3572 qemu_rearm_alarm_timer(alarm_timer
);
3576 void vm_stop(int reason
)
3579 cpu_disable_ticks();
3581 vm_state_notify(0, reason
);
3585 /* reset/shutdown handler */
3587 typedef struct QEMUResetEntry
{
3588 QEMUResetHandler
*func
;
3590 struct QEMUResetEntry
*next
;
3593 static QEMUResetEntry
*first_reset_entry
;
3594 static int reset_requested
;
3595 static int shutdown_requested
;
3596 static int powerdown_requested
;
3598 int qemu_no_shutdown(void)
3600 int r
= no_shutdown
;
3605 int qemu_shutdown_requested(void)
3607 int r
= shutdown_requested
;
3608 shutdown_requested
= 0;
3612 int qemu_reset_requested(void)
3614 int r
= reset_requested
;
3615 reset_requested
= 0;
3619 int qemu_powerdown_requested(void)
3621 int r
= powerdown_requested
;
3622 powerdown_requested
= 0;
3626 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3628 QEMUResetEntry
**pre
, *re
;
3630 pre
= &first_reset_entry
;
3631 while (*pre
!= NULL
)
3632 pre
= &(*pre
)->next
;
3633 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3635 re
->opaque
= opaque
;
3640 void qemu_system_reset(void)
3644 /* reset all devices */
3645 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3646 re
->func(re
->opaque
);
3650 void qemu_system_reset_request(void)
3653 shutdown_requested
= 1;
3655 reset_requested
= 1;
3658 if (cpu_single_env
) {
3659 qemu_kvm_cpu_stop(cpu_single_env
);
3660 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3665 void qemu_system_shutdown_request(void)
3667 shutdown_requested
= 1;
3669 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3672 void qemu_system_powerdown_request(void)
3674 powerdown_requested
= 1;
3676 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3679 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3684 /* KVM holds a mutex while QEMU code is running, we need hooks to
3685 release the mutex whenever QEMU code sleeps. */
3689 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3697 static void host_main_loop_wait(int *timeout
)
3703 /* XXX: need to suppress polling by better using win32 events */
3705 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3706 ret
|= pe
->func(pe
->opaque
);
3710 WaitObjects
*w
= &wait_objects
;
3712 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3713 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3714 if (w
->func
[ret
- WAIT_OBJECT_0
])
3715 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3717 /* Check for additional signaled events */
3718 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3720 /* Check if event is signaled */
3721 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3722 if(ret2
== WAIT_OBJECT_0
) {
3724 w
->func
[i
](w
->opaque
[i
]);
3725 } else if (ret2
== WAIT_TIMEOUT
) {
3727 err
= GetLastError();
3728 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3731 } else if (ret
== WAIT_TIMEOUT
) {
3733 err
= GetLastError();
3734 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3741 static void host_main_loop_wait(int *timeout
)
3746 void main_loop_wait(int timeout
)
3748 IOHandlerRecord
*ioh
;
3749 fd_set rfds
, wfds
, xfds
;
3753 qemu_bh_update_timeout(&timeout
);
3755 host_main_loop_wait(&timeout
);
3757 /* poll any events */
3758 /* XXX: separate device handlers from system ones */
3763 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3767 (!ioh
->fd_read_poll
||
3768 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3769 FD_SET(ioh
->fd
, &rfds
);
3773 if (ioh
->fd_write
) {
3774 FD_SET(ioh
->fd
, &wfds
);
3780 tv
.tv_sec
= timeout
/ 1000;
3781 tv
.tv_usec
= (timeout
% 1000) * 1000;
3783 #if defined(CONFIG_SLIRP)
3784 if (slirp_is_inited()) {
3785 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3788 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3790 IOHandlerRecord
**pioh
;
3792 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3793 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3794 ioh
->fd_read(ioh
->opaque
);
3795 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3796 FD_CLR(ioh
->fd
, &rfds
);
3798 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3799 ioh
->fd_write(ioh
->opaque
);
3803 /* remove deleted IO handlers */
3804 pioh
= &first_io_handler
;
3814 #if defined(CONFIG_SLIRP)
3815 if (slirp_is_inited()) {
3821 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3825 /* vm time timers */
3826 if (vm_running
&& (!cur_cpu
3827 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3828 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3829 qemu_get_clock(vm_clock
));
3831 /* real time timers */
3832 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3833 qemu_get_clock(rt_clock
));
3835 /* Check bottom-halves last in case any of the earlier events triggered
3841 static int main_loop(void)
3844 #ifdef CONFIG_PROFILER
3850 if (kvm_enabled()) {
3852 cpu_disable_ticks();
3856 cur_cpu
= first_cpu
;
3857 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3864 #ifdef CONFIG_PROFILER
3865 ti
= profile_getclock();
3870 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3871 env
->icount_decr
.u16
.low
= 0;
3872 env
->icount_extra
= 0;
3873 count
= qemu_next_deadline();
3874 count
= (count
+ (1 << icount_time_shift
) - 1)
3875 >> icount_time_shift
;
3876 qemu_icount
+= count
;
3877 decr
= (count
> 0xffff) ? 0xffff : count
;
3879 env
->icount_decr
.u16
.low
= decr
;
3880 env
->icount_extra
= count
;
3882 ret
= cpu_exec(env
);
3883 #ifdef CONFIG_PROFILER
3884 qemu_time
+= profile_getclock() - ti
;
3887 /* Fold pending instructions back into the
3888 instruction counter, and clear the interrupt flag. */
3889 qemu_icount
-= (env
->icount_decr
.u16
.low
3890 + env
->icount_extra
);
3891 env
->icount_decr
.u32
= 0;
3892 env
->icount_extra
= 0;
3894 next_cpu
= env
->next_cpu
?: first_cpu
;
3895 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3896 ret
= EXCP_INTERRUPT
;
3900 if (ret
== EXCP_HLT
) {
3901 /* Give the next CPU a chance to run. */
3905 if (ret
!= EXCP_HALTED
)
3907 /* all CPUs are halted ? */
3913 if (shutdown_requested
) {
3914 ret
= EXCP_INTERRUPT
;
3922 if (reset_requested
) {
3923 reset_requested
= 0;
3924 qemu_system_reset();
3926 kvm_load_registers(env
);
3927 ret
= EXCP_INTERRUPT
;
3929 if (powerdown_requested
) {
3930 powerdown_requested
= 0;
3931 qemu_system_powerdown();
3932 ret
= EXCP_INTERRUPT
;
3934 #ifdef CONFIG_GDBSTUB
3935 if (unlikely(ret
== EXCP_DEBUG
)) {
3936 gdb_set_stop_cpu(cur_cpu
);
3937 vm_stop(EXCP_DEBUG
);
3940 /* If all cpus are halted then wait until the next IRQ */
3941 /* XXX: use timeout computed from timers */
3942 if (ret
== EXCP_HALTED
) {
3946 /* Advance virtual time to the next event. */
3947 if (use_icount
== 1) {
3948 /* When not using an adaptive execution frequency
3949 we tend to get badly out of sync with real time,
3950 so just delay for a reasonable amount of time. */
3953 delta
= cpu_get_icount() - cpu_get_clock();
3956 /* If virtual time is ahead of real time then just
3958 timeout
= (delta
/ 1000000) + 1;
3960 /* Wait for either IO to occur or the next
3962 add
= qemu_next_deadline();
3963 /* We advance the timer before checking for IO.
3964 Limit the amount we advance so that early IO
3965 activity won't get the guest too far ahead. */
3969 add
= (add
+ (1 << icount_time_shift
) - 1)
3970 >> icount_time_shift
;
3972 timeout
= delta
/ 1000000;
3983 if (shutdown_requested
) {
3984 ret
= EXCP_INTERRUPT
;
3989 #ifdef CONFIG_PROFILER
3990 ti
= profile_getclock();
3992 main_loop_wait(timeout
);
3993 #ifdef CONFIG_PROFILER
3994 dev_time
+= profile_getclock() - ti
;
3997 cpu_disable_ticks();
4001 static void help(int exitcode
)
4003 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
4004 and qemu-doc.texi */
4005 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
4006 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
4007 "usage: %s [options] [disk_image]\n"
4009 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4011 "Standard options:\n"
4012 "-h or -help display this help and exit\n"
4013 "-M machine select emulated machine (-M ? for list)\n"
4014 "-cpu cpu select CPU (-cpu ? for list)\n"
4015 "-smp n set the number of CPUs to 'n' [default=1]\n"
4016 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4017 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4018 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4019 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4020 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4021 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
4022 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
4024 " use 'file' as a drive image\n"
4025 "-mtdblock file use 'file' as on-board Flash memory image\n"
4026 "-sd file use 'file' as SecureDigital card image\n"
4027 "-pflash file use 'file' as a parallel flash image\n"
4028 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4029 "-snapshot write to temporary files instead of disk image files\n"
4030 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4032 "-k language use keyboard layout (for example \"fr\" for French)\n"
4035 "-audio-help print list of audio drivers and their options\n"
4036 "-soundhw c1,... enable audio support\n"
4037 " and only specified sound cards (comma separated list)\n"
4038 " use -soundhw ? to get the list of supported cards\n"
4039 " use -soundhw all to enable all of them\n"
4041 "-usb enable the USB driver (will be the default soon)\n"
4042 "-usbdevice name add the host or guest USB device 'name'\n"
4043 "-name string set the name of the guest\n"
4044 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
4045 " specify machine UUID\n"
4047 "Display options:\n"
4048 "-nographic disable graphical output and redirect serial I/Os to console\n"
4049 #ifdef CONFIG_CURSES
4050 "-curses use a curses/ncurses interface instead of SDL\n"
4053 "-no-frame open SDL window without a frame and window decorations\n"
4054 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4055 "-no-quit disable SDL window close capability\n"
4058 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4059 "-vga [std|cirrus|vmware|none]\n"
4060 " select video card type\n"
4061 "-full-screen start in full screen\n"
4062 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4063 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4065 "-vnc display start a VNC server on display\n"
4067 "-nvram file use 'file' to save or load nvram image\n"
4069 "-name string set the name of the guest\n"
4070 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4072 "Network options:\n"
4073 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4074 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4076 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4077 " connect the user mode network stack to VLAN 'n' and send\n"
4078 " hostname 'host' to DHCP clients\n"
4081 "-net tap[,vlan=n][,name=str],ifname=name\n"
4082 " connect the host TAP network interface to VLAN 'n'\n"
4084 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4085 " connect the host TAP network interface to VLAN 'n' and use the\n"
4086 " network scripts 'file' (default=%s)\n"
4087 " and 'dfile' (default=%s);\n"
4088 " use '[down]script=no' to disable script execution;\n"
4089 " use 'fd=h' to connect to an already opened TAP interface\n"
4091 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4092 " connect the vlan 'n' to another VLAN using a socket connection\n"
4093 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4094 " connect the vlan 'n' to multicast maddr and port\n"
4096 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4097 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4098 " on host and listening for incoming connections on 'socketpath'.\n"
4099 " Use group 'groupname' and mode 'octalmode' to change default\n"
4100 " ownership and permissions for communication port.\n"
4102 "-net none use it alone to have zero network devices; if no -net option\n"
4103 " is provided, the default is '-net nic -net user'\n"
4105 "-tftp dir allow tftp access to files in dir [-net user]\n"
4106 "-bootp file advertise file in BOOTP replies\n"
4108 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4110 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4111 " redirect TCP or UDP connections from host to guest [-net user]\n"
4114 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4115 "-bt hci,host[:id]\n"
4116 " use host's HCI with the given name\n"
4117 "-bt hci[,vlan=n]\n"
4118 " emulate a standard HCI in virtual scatternet 'n'\n"
4119 "-bt vhci[,vlan=n]\n"
4120 " add host computer to virtual scatternet 'n' using VHCI\n"
4121 "-bt device:dev[,vlan=n]\n"
4122 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4126 "i386 target only:\n"
4127 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4128 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4129 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4130 "-no-acpi disable ACPI\n"
4131 "-no-hpet disable HPET\n"
4132 "-acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,data=file1[:file2]...]\n"
4133 " ACPI table description\n"
4135 "Linux boot specific:\n"
4136 "-kernel bzImage use 'bzImage' as kernel image\n"
4137 "-append cmdline use 'cmdline' as kernel command line\n"
4138 "-initrd file use 'file' as initial ram disk\n"
4140 "Debug/Expert options:\n"
4141 "-serial dev redirect the serial port to char device 'dev'\n"
4142 "-parallel dev redirect the parallel port to char device 'dev'\n"
4143 "-monitor dev redirect the monitor to char device 'dev'\n"
4144 "-pidfile file write PID to 'file'\n"
4145 "-S freeze CPU at startup (use 'c' to start execution)\n"
4146 "-s wait gdb connection to port\n"
4147 "-p port set gdb connection port [default=%s]\n"
4148 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4149 "-hdachs c,h,s[,t]\n"
4150 " force hard disk 0 physical geometry and the optional BIOS\n"
4151 " translation (t=none or lba) (usually qemu can guess them)\n"
4152 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4153 "-bios file set the filename for the BIOS\n"
4155 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4156 "-no-kqemu disable KQEMU kernel module usage\n"
4159 "-enable-kvm enable KVM full virtualization support\n"
4162 #ifndef NO_CPU_EMULATION
4163 "-no-kvm disable KVM hardware virtualization\n"
4165 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4166 "-no-kvm-pit disable KVM kernel mode PIT\n"
4167 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4168 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4169 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4170 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4171 " expose a PCI device to the guest OS.\n"
4172 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4173 " 'string' is used in log output.\n"
4176 "-no-reboot exit instead of rebooting\n"
4177 "-no-shutdown stop before shutdown\n"
4178 "-loadvm [tag|id]\n"
4179 " start right away with a saved state (loadvm in monitor)\n"
4181 "-daemonize daemonize QEMU after initializing\n"
4183 "-tdf inject timer interrupts that got lost\n"
4184 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4185 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also\n"
4186 " enables allocation of guest memory with huge pages\n"
4188 "-mem-prealloc toggles preallocation of -mem-path backed physical memory\n"
4189 " at startup. Default is enabled.\n"
4191 "-option-rom rom load a file, rom, into the option ROM space\n"
4192 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4193 "-prom-env variable=value\n"
4194 " set OpenBIOS nvram variables\n"
4196 "-clock force the use of the given methods for timer alarm.\n"
4197 " To see what timers are available use -clock ?\n"
4198 "-localtime set the real time clock to local time [default=utc]\n"
4199 "-startdate select initial date of the clock\n"
4200 "-icount [N|auto]\n"
4201 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4202 "-echr chr set terminal escape character instead of ctrl-a\n"
4203 "-virtioconsole c\n"
4204 " set virtio console\n"
4205 "-show-cursor show cursor\n"
4206 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4207 "-semihosting semihosting mode\n"
4209 #if defined(TARGET_ARM)
4210 "-old-param old param mode\n"
4212 "-tb-size n set TB size\n"
4213 "-incoming p prepare for incoming migration, listen on port p\n"
4215 "-chroot dir Chroot to dir just before starting the VM.\n"
4216 "-runas user Change to user id user just before starting the VM.\n"
4219 "During emulation, the following keys are useful:\n"
4220 "ctrl-alt-f toggle full screen\n"
4221 "ctrl-alt-n switch to virtual console 'n'\n"
4222 "ctrl-alt toggle mouse and keyboard grab\n"
4224 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4229 DEFAULT_NETWORK_SCRIPT
,
4230 DEFAULT_NETWORK_DOWN_SCRIPT
,
4232 DEFAULT_GDBSTUB_PORT
,
4237 #define HAS_ARG 0x0001
4240 /* Please keep in synch with help, qemu_options[] and
4242 /* Standard options: */
4255 QEMU_OPTION_mtdblock
,
4259 QEMU_OPTION_snapshot
,
4262 QEMU_OPTION_audio_help
,
4263 QEMU_OPTION_soundhw
,
4265 QEMU_OPTION_usbdevice
,
4269 /* Display options: */
4270 QEMU_OPTION_nographic
,
4272 QEMU_OPTION_no_frame
,
4273 QEMU_OPTION_alt_grab
,
4274 QEMU_OPTION_no_quit
,
4276 QEMU_OPTION_portrait
,
4278 QEMU_OPTION_full_screen
,
4282 /* Network options: */
4290 /* i386 target only: */
4291 QEMU_OPTION_win2k_hack
,
4292 QEMU_OPTION_rtc_td_hack
,
4293 QEMU_OPTION_no_fd_bootchk
,
4294 QEMU_OPTION_no_acpi
,
4295 QEMU_OPTION_no_hpet
,
4296 QEMU_OPTION_acpitable
,
4298 /* Linux boot specific: */
4303 /* Debug/Expert options: */
4305 QEMU_OPTION_parallel
,
4306 QEMU_OPTION_monitor
,
4307 QEMU_OPTION_pidfile
,
4315 QEMU_OPTION_kernel_kqemu
,
4316 QEMU_OPTION_no_kqemu
,
4317 QEMU_OPTION_enable_kvm
,
4318 QEMU_OPTION_enable_nesting
,
4320 QEMU_OPTION_no_kvm_irqchip
,
4321 QEMU_OPTION_no_kvm_pit
,
4322 QEMU_OPTION_no_kvm_pit_reinjection
,
4323 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4324 QEMU_OPTION_pcidevice
,
4326 QEMU_OPTION_no_reboot
,
4327 QEMU_OPTION_no_shutdown
,
4329 QEMU_OPTION_daemonize
,
4330 QEMU_OPTION_option_rom
,
4331 QEMU_OPTION_cpu_vendor
,
4333 QEMU_OPTION_prom_env
,
4335 QEMU_OPTION_localtime
,
4336 QEMU_OPTION_startdate
,
4339 QEMU_OPTION_virtiocon
,
4340 QEMU_OPTION_show_cursor
,
4341 QEMU_OPTION_semihosting
,
4342 QEMU_OPTION_old_param
,
4343 QEMU_OPTION_tb_size
,
4344 QEMU_OPTION_incoming
,
4348 QEMU_OPTION_kvm_shadow_memory
,
4349 QEMU_OPTION_mempath
,
4351 QEMU_OPTION_mem_prealloc
,
4355 typedef struct QEMUOption
{
4361 static const QEMUOption qemu_options
[] = {
4362 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4364 /* Standard options: */
4365 { "h", 0, QEMU_OPTION_h
},
4366 { "help", 0, QEMU_OPTION_h
},
4367 { "M", HAS_ARG
, QEMU_OPTION_M
},
4368 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4369 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4370 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4371 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4372 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4373 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4374 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4375 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4376 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4377 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4378 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4379 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4380 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4381 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4382 { "snapshot", 0, QEMU_OPTION_snapshot
},
4383 { "m", HAS_ARG
, QEMU_OPTION_m
},
4384 { "k", HAS_ARG
, QEMU_OPTION_k
},
4386 { "audio-help", 0, QEMU_OPTION_audio_help
},
4387 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4389 { "usb", 0, QEMU_OPTION_usb
},
4390 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4391 { "name", HAS_ARG
, QEMU_OPTION_name
},
4392 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4394 /* Display options: */
4395 { "nographic", 0, QEMU_OPTION_nographic
},
4396 #ifdef CONFIG_CURSES
4397 { "curses", 0, QEMU_OPTION_curses
},
4400 { "no-frame", 0, QEMU_OPTION_no_frame
},
4401 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4402 { "no-quit", 0, QEMU_OPTION_no_quit
},
4403 { "sdl", 0, QEMU_OPTION_sdl
},
4405 { "portrait", 0, QEMU_OPTION_portrait
},
4406 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4407 { "full-screen", 0, QEMU_OPTION_full_screen
},
4408 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4409 { "g", 1, QEMU_OPTION_g
},
4411 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4413 /* Network options: */
4414 { "net", HAS_ARG
, QEMU_OPTION_net
},
4416 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4417 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4419 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4421 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4423 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4425 /* i386 target only: */
4426 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4427 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4428 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4429 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4430 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4431 { "acpitable", HAS_ARG
, QEMU_OPTION_acpitable
},
4434 /* Linux boot specific: */
4435 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4436 { "append", HAS_ARG
, QEMU_OPTION_append
},
4437 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4439 /* Debug/Expert options: */
4440 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4441 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4442 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4443 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4444 { "S", 0, QEMU_OPTION_S
},
4445 { "s", 0, QEMU_OPTION_s
},
4446 { "p", HAS_ARG
, QEMU_OPTION_p
},
4447 { "d", HAS_ARG
, QEMU_OPTION_d
},
4448 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4449 { "L", HAS_ARG
, QEMU_OPTION_L
},
4450 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4452 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4453 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4456 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4459 #ifndef NO_CPU_EMULATION
4460 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4462 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4463 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4464 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4465 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4466 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4467 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4470 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4471 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4472 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4473 { "daemonize", 0, QEMU_OPTION_daemonize
},
4474 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4475 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4476 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4478 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4479 { "localtime", 0, QEMU_OPTION_localtime
},
4480 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4481 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4482 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4483 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4484 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4485 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4486 { "semihosting", 0, QEMU_OPTION_semihosting
},
4488 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4489 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4490 { "nvram", HAS_ARG
, QEMU_OPTION_nvram
},
4491 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4492 #if defined(TARGET_ARM)
4493 { "old-param", 0, QEMU_OPTION_old_param
},
4495 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4496 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4497 { "chroot", HAS_ARG
, QEMU_OPTION_chroot
},
4498 { "runas", HAS_ARG
, QEMU_OPTION_runas
},
4499 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4501 { "mem-prealloc", 0, QEMU_OPTION_mem_prealloc
},
4507 struct soundhw soundhw
[] = {
4508 #ifdef HAS_AUDIO_CHOICE
4509 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4515 { .init_isa
= pcspk_audio_init
}
4522 "Creative Sound Blaster 16",
4525 { .init_isa
= SB16_init
}
4529 #ifdef CONFIG_CS4231A
4535 { .init_isa
= cs4231a_init
}
4543 "Yamaha YMF262 (OPL3)",
4545 "Yamaha YM3812 (OPL2)",
4549 { .init_isa
= Adlib_init
}
4556 "Gravis Ultrasound GF1",
4559 { .init_isa
= GUS_init
}
4566 "Intel 82801AA AC97 Audio",
4569 { .init_pci
= ac97_init
}
4573 #ifdef CONFIG_ES1370
4576 "ENSONIQ AudioPCI ES1370",
4579 { .init_pci
= es1370_init
}
4583 #endif /* HAS_AUDIO_CHOICE */
4585 { NULL
, NULL
, 0, 0, { NULL
} }
4588 static void select_soundhw (const char *optarg
)
4592 if (*optarg
== '?') {
4595 printf ("Valid sound card names (comma separated):\n");
4596 for (c
= soundhw
; c
->name
; ++c
) {
4597 printf ("%-11s %s\n", c
->name
, c
->descr
);
4599 printf ("\n-soundhw all will enable all of the above\n");
4600 exit (*optarg
!= '?');
4608 if (!strcmp (optarg
, "all")) {
4609 for (c
= soundhw
; c
->name
; ++c
) {
4617 e
= strchr (p
, ',');
4618 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4620 for (c
= soundhw
; c
->name
; ++c
) {
4621 if (!strncmp (c
->name
, p
, l
)) {
4630 "Unknown sound card name (too big to show)\n");
4633 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4638 p
+= l
+ (e
!= NULL
);
4642 goto show_valid_cards
;
4647 static void select_vgahw (const char *p
)
4651 if (strstart(p
, "std", &opts
)) {
4652 std_vga_enabled
= 1;
4653 cirrus_vga_enabled
= 0;
4655 } else if (strstart(p
, "cirrus", &opts
)) {
4656 cirrus_vga_enabled
= 1;
4657 std_vga_enabled
= 0;
4659 } else if (strstart(p
, "vmware", &opts
)) {
4660 cirrus_vga_enabled
= 0;
4661 std_vga_enabled
= 0;
4663 } else if (strstart(p
, "none", &opts
)) {
4664 cirrus_vga_enabled
= 0;
4665 std_vga_enabled
= 0;
4669 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4673 const char *nextopt
;
4675 if (strstart(opts
, ",retrace=", &nextopt
)) {
4677 if (strstart(opts
, "dumb", &nextopt
))
4678 vga_retrace_method
= VGA_RETRACE_DUMB
;
4679 else if (strstart(opts
, "precise", &nextopt
))
4680 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4681 else goto invalid_vga
;
4682 } else goto invalid_vga
;
4688 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4690 exit(STATUS_CONTROL_C_EXIT
);
4695 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4699 if(strlen(str
) != 36)
4702 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4703 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4704 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4712 #define MAX_NET_CLIENTS 32
4714 static int saved_argc
;
4715 static char **saved_argv
;
4717 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4721 *opt_daemonize
= daemonize
;
4722 *opt_incoming
= incoming
;
4727 #define HUGETLBFS_MAGIC 0x958458f6
4729 static long gethugepagesize(const char *path
)
4735 ret
= statfs(path
, &fs
);
4736 } while (ret
!= 0 && errno
== EINTR
);
4743 if (fs
.f_type
!= HUGETLBFS_MAGIC
)
4744 fprintf(stderr
, "Warning: path not on HugeTLBFS: %s\n", path
);
4749 static void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4758 if (!kvm_has_sync_mmu()) {
4759 fprintf(stderr
, "host lacks mmu notifiers, disabling --mem-path\n");
4763 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4766 hpagesize
= gethugepagesize(path
);
4770 fd
= mkstemp(filename
);
4779 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4782 * ftruncate is not supported by hugetlbfs in older
4783 * hosts, so don't bother checking for errors.
4784 * If anything goes wrong with it under other filesystems,
4787 ftruncate(fd
, memory
);
4790 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
4791 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
4792 * to sidestep this quirk.
4794 flags
= mem_prealloc
? MAP_POPULATE
|MAP_SHARED
: MAP_PRIVATE
;
4795 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, flags
, fd
, 0);
4797 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4799 if (area
== MAP_FAILED
) {
4800 perror("alloc_mem_area: can't mmap hugetlbfs pages");
4809 static void *qemu_alloc_physram(unsigned long memory
)
4813 unsigned long map_len
= memory
;
4816 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4819 area
= qemu_vmalloc(memory
);
4821 if (kvm_setup_guest_memory(area
, map_len
))
4829 static void termsig_handler(int signal
)
4831 qemu_system_shutdown_request();
4834 static void termsig_setup(void)
4836 struct sigaction act
;
4838 memset(&act
, 0, sizeof(act
));
4839 act
.sa_handler
= termsig_handler
;
4840 sigaction(SIGINT
, &act
, NULL
);
4841 sigaction(SIGHUP
, &act
, NULL
);
4842 sigaction(SIGTERM
, &act
, NULL
);
4847 int main(int argc
, char **argv
, char **envp
)
4849 #ifdef CONFIG_GDBSTUB
4851 const char *gdbstub_port
;
4853 uint32_t boot_devices_bitmap
= 0;
4855 int snapshot
, linux_boot
, net_boot
;
4856 const char *initrd_filename
;
4857 const char *kernel_filename
, *kernel_cmdline
;
4858 const char *boot_devices
= "";
4860 DisplayChangeListener
*dcl
;
4861 int cyls
, heads
, secs
, translation
;
4862 const char *net_clients
[MAX_NET_CLIENTS
];
4864 const char *bt_opts
[MAX_BT_CMDLINE
];
4868 const char *r
, *optarg
;
4869 CharDriverState
*monitor_hd
= NULL
;
4870 const char *monitor_device
;
4871 const char *serial_devices
[MAX_SERIAL_PORTS
];
4872 int serial_device_index
;
4873 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4874 int parallel_device_index
;
4875 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4876 int virtio_console_index
;
4877 const char *loadvm
= NULL
;
4878 QEMUMachine
*machine
;
4879 const char *cpu_model
;
4880 const char *usb_devices
[MAX_USB_CMDLINE
];
4881 int usb_devices_index
;
4884 const char *pid_file
= NULL
;
4885 const char *incoming
= NULL
;
4887 struct passwd
*pwd
= NULL
;
4888 const char *chroot_dir
= NULL
;
4889 const char *run_as
= NULL
;
4891 qemu_cache_utils_init(envp
);
4893 LIST_INIT (&vm_change_state_head
);
4896 struct sigaction act
;
4897 sigfillset(&act
.sa_mask
);
4899 act
.sa_handler
= SIG_IGN
;
4900 sigaction(SIGPIPE
, &act
, NULL
);
4903 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4904 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4905 QEMU to run on a single CPU */
4910 h
= GetCurrentProcess();
4911 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4912 for(i
= 0; i
< 32; i
++) {
4913 if (mask
& (1 << i
))
4918 SetProcessAffinityMask(h
, mask
);
4924 register_machines();
4925 machine
= first_machine
;
4927 initrd_filename
= NULL
;
4929 vga_ram_size
= VGA_RAM_SIZE
;
4930 #ifdef CONFIG_GDBSTUB
4932 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4937 kernel_filename
= NULL
;
4938 kernel_cmdline
= "";
4939 cyls
= heads
= secs
= 0;
4940 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4941 monitor_device
= "vc:80Cx24C";
4943 serial_devices
[0] = "vc:80Cx24C";
4944 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4945 serial_devices
[i
] = NULL
;
4946 serial_device_index
= 0;
4948 parallel_devices
[0] = "vc:80Cx24C";
4949 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4950 parallel_devices
[i
] = NULL
;
4951 parallel_device_index
= 0;
4953 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4954 virtio_consoles
[i
] = NULL
;
4955 virtio_console_index
= 0;
4957 usb_devices_index
= 0;
4958 assigned_devices_index
= 0;
4977 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4979 const QEMUOption
*popt
;
4982 /* Treat --foo the same as -foo. */
4985 popt
= qemu_options
;
4988 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4992 if (!strcmp(popt
->name
, r
+ 1))
4996 if (popt
->flags
& HAS_ARG
) {
4997 if (optind
>= argc
) {
4998 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5002 optarg
= argv
[optind
++];
5007 switch(popt
->index
) {
5009 machine
= find_machine(optarg
);
5012 printf("Supported machines are:\n");
5013 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5014 printf("%-10s %s%s\n",
5016 m
== first_machine
? " (default)" : "");
5018 exit(*optarg
!= '?');
5021 case QEMU_OPTION_cpu
:
5022 /* hw initialization will check this */
5023 if (*optarg
== '?') {
5024 /* XXX: implement xxx_cpu_list for targets that still miss it */
5025 #if defined(cpu_list)
5026 cpu_list(stdout
, &fprintf
);
5033 case QEMU_OPTION_initrd
:
5034 initrd_filename
= optarg
;
5036 case QEMU_OPTION_hda
:
5038 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5040 hda_index
= drive_add(optarg
, HD_ALIAS
5041 ",cyls=%d,heads=%d,secs=%d%s",
5042 0, cyls
, heads
, secs
,
5043 translation
== BIOS_ATA_TRANSLATION_LBA
?
5045 translation
== BIOS_ATA_TRANSLATION_NONE
?
5046 ",trans=none" : "");
5048 case QEMU_OPTION_hdb
:
5049 case QEMU_OPTION_hdc
:
5050 case QEMU_OPTION_hdd
:
5051 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5053 case QEMU_OPTION_drive
:
5054 drive_add(NULL
, "%s", optarg
);
5056 case QEMU_OPTION_mtdblock
:
5057 drive_add(optarg
, MTD_ALIAS
);
5059 case QEMU_OPTION_sd
:
5060 drive_add(optarg
, SD_ALIAS
);
5062 case QEMU_OPTION_pflash
:
5063 drive_add(optarg
, PFLASH_ALIAS
);
5065 case QEMU_OPTION_snapshot
:
5068 case QEMU_OPTION_hdachs
:
5072 cyls
= strtol(p
, (char **)&p
, 0);
5073 if (cyls
< 1 || cyls
> 16383)
5078 heads
= strtol(p
, (char **)&p
, 0);
5079 if (heads
< 1 || heads
> 16)
5084 secs
= strtol(p
, (char **)&p
, 0);
5085 if (secs
< 1 || secs
> 63)
5089 if (!strcmp(p
, "none"))
5090 translation
= BIOS_ATA_TRANSLATION_NONE
;
5091 else if (!strcmp(p
, "lba"))
5092 translation
= BIOS_ATA_TRANSLATION_LBA
;
5093 else if (!strcmp(p
, "auto"))
5094 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5097 } else if (*p
!= '\0') {
5099 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5102 if (hda_index
!= -1)
5103 snprintf(drives_opt
[hda_index
].opt
,
5104 sizeof(drives_opt
[hda_index
].opt
),
5105 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5106 0, cyls
, heads
, secs
,
5107 translation
== BIOS_ATA_TRANSLATION_LBA
?
5109 translation
== BIOS_ATA_TRANSLATION_NONE
?
5110 ",trans=none" : "");
5113 case QEMU_OPTION_nographic
:
5116 #ifdef CONFIG_CURSES
5117 case QEMU_OPTION_curses
:
5121 case QEMU_OPTION_portrait
:
5124 case QEMU_OPTION_kernel
:
5125 kernel_filename
= optarg
;
5127 case QEMU_OPTION_append
:
5128 kernel_cmdline
= optarg
;
5130 case QEMU_OPTION_cdrom
:
5131 drive_add(optarg
, CDROM_ALIAS
);
5133 case QEMU_OPTION_boot
:
5134 boot_devices
= optarg
;
5135 /* We just do some generic consistency checks */
5137 /* Could easily be extended to 64 devices if needed */
5140 boot_devices_bitmap
= 0;
5141 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5142 /* Allowed boot devices are:
5143 * a b : floppy disk drives
5144 * c ... f : IDE disk drives
5145 * g ... m : machine implementation dependant drives
5146 * n ... p : network devices
5147 * It's up to each machine implementation to check
5148 * if the given boot devices match the actual hardware
5149 * implementation and firmware features.
5151 if (*p
< 'a' || *p
> 'q') {
5152 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5155 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5157 "Boot device '%c' was given twice\n",*p
);
5160 boot_devices_bitmap
|= 1 << (*p
- 'a');
5164 case QEMU_OPTION_fda
:
5165 case QEMU_OPTION_fdb
:
5166 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5169 case QEMU_OPTION_no_fd_bootchk
:
5173 case QEMU_OPTION_net
:
5174 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5175 fprintf(stderr
, "qemu: too many network clients\n");
5178 net_clients
[nb_net_clients
] = optarg
;
5182 case QEMU_OPTION_tftp
:
5183 tftp_prefix
= optarg
;
5185 case QEMU_OPTION_bootp
:
5186 bootp_filename
= optarg
;
5189 case QEMU_OPTION_smb
:
5190 net_slirp_smb(optarg
);
5193 case QEMU_OPTION_redir
:
5194 net_slirp_redir(optarg
);
5197 case QEMU_OPTION_bt
:
5198 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5199 fprintf(stderr
, "qemu: too many bluetooth options\n");
5202 bt_opts
[nb_bt_opts
++] = optarg
;
5205 case QEMU_OPTION_audio_help
:
5209 case QEMU_OPTION_soundhw
:
5210 select_soundhw (optarg
);
5216 case QEMU_OPTION_m
: {
5220 value
= strtoul(optarg
, &ptr
, 10);
5222 case 0: case 'M': case 'm':
5229 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5233 /* On 32-bit hosts, QEMU is limited by virtual address space */
5234 if (value
> (2047 << 20)
5236 && HOST_LONG_BITS
== 32
5239 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5242 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5243 fprintf(stderr
, "qemu: ram size too large\n");
5252 const CPULogItem
*item
;
5254 mask
= cpu_str_to_log_mask(optarg
);
5256 printf("Log items (comma separated):\n");
5257 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5258 printf("%-10s %s\n", item
->name
, item
->help
);
5265 #ifdef CONFIG_GDBSTUB
5270 gdbstub_port
= optarg
;
5276 case QEMU_OPTION_bios
:
5283 keyboard_layout
= optarg
;
5285 case QEMU_OPTION_localtime
:
5288 case QEMU_OPTION_vga
:
5289 select_vgahw (optarg
);
5296 w
= strtol(p
, (char **)&p
, 10);
5299 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5305 h
= strtol(p
, (char **)&p
, 10);
5310 depth
= strtol(p
, (char **)&p
, 10);
5311 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5312 depth
!= 24 && depth
!= 32)
5314 } else if (*p
== '\0') {
5315 depth
= graphic_depth
;
5322 graphic_depth
= depth
;
5325 case QEMU_OPTION_echr
:
5328 term_escape_char
= strtol(optarg
, &r
, 0);
5330 printf("Bad argument to echr\n");
5333 case QEMU_OPTION_monitor
:
5334 monitor_device
= optarg
;
5336 case QEMU_OPTION_serial
:
5337 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5338 fprintf(stderr
, "qemu: too many serial ports\n");
5341 serial_devices
[serial_device_index
] = optarg
;
5342 serial_device_index
++;
5344 case QEMU_OPTION_virtiocon
:
5345 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5346 fprintf(stderr
, "qemu: too many virtio consoles\n");
5349 virtio_consoles
[virtio_console_index
] = optarg
;
5350 virtio_console_index
++;
5352 case QEMU_OPTION_parallel
:
5353 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5354 fprintf(stderr
, "qemu: too many parallel ports\n");
5357 parallel_devices
[parallel_device_index
] = optarg
;
5358 parallel_device_index
++;
5360 case QEMU_OPTION_loadvm
:
5363 case QEMU_OPTION_full_screen
:
5367 case QEMU_OPTION_no_frame
:
5370 case QEMU_OPTION_alt_grab
:
5373 case QEMU_OPTION_no_quit
:
5376 case QEMU_OPTION_sdl
:
5380 case QEMU_OPTION_pidfile
:
5384 case QEMU_OPTION_win2k_hack
:
5385 win2k_install_hack
= 1;
5387 case QEMU_OPTION_rtc_td_hack
:
5390 case QEMU_OPTION_acpitable
:
5391 if(acpi_table_add(optarg
) < 0) {
5392 fprintf(stderr
, "Wrong acpi table provided\n");
5398 case QEMU_OPTION_no_kqemu
:
5401 case QEMU_OPTION_kernel_kqemu
:
5406 case QEMU_OPTION_enable_kvm
:
5414 case QEMU_OPTION_no_kvm
:
5417 case QEMU_OPTION_no_kvm_irqchip
: {
5422 case QEMU_OPTION_no_kvm_pit
: {
5426 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5427 kvm_pit_reinject
= 0;
5430 case QEMU_OPTION_enable_nesting
: {
5434 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5435 case QEMU_OPTION_pcidevice
:
5436 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5437 fprintf(stderr
, "Too many assigned devices\n");
5440 assigned_devices
[assigned_devices_index
] = optarg
;
5441 assigned_devices_index
++;
5445 case QEMU_OPTION_usb
:
5448 case QEMU_OPTION_usbdevice
:
5450 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5451 fprintf(stderr
, "Too many USB devices\n");
5454 usb_devices
[usb_devices_index
] = optarg
;
5455 usb_devices_index
++;
5457 case QEMU_OPTION_smp
:
5458 smp_cpus
= atoi(optarg
);
5460 fprintf(stderr
, "Invalid number of CPUs\n");
5464 case QEMU_OPTION_vnc
:
5465 vnc_display
= optarg
;
5467 case QEMU_OPTION_no_acpi
:
5470 case QEMU_OPTION_no_hpet
:
5473 case QEMU_OPTION_no_reboot
:
5476 case QEMU_OPTION_no_shutdown
:
5479 case QEMU_OPTION_show_cursor
:
5482 case QEMU_OPTION_uuid
:
5483 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5484 fprintf(stderr
, "Fail to parse UUID string."
5485 " Wrong format.\n");
5489 case QEMU_OPTION_daemonize
:
5492 case QEMU_OPTION_option_rom
:
5493 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5494 fprintf(stderr
, "Too many option ROMs\n");
5497 option_rom
[nb_option_roms
] = optarg
;
5500 case QEMU_OPTION_semihosting
:
5501 semihosting_enabled
= 1;
5503 case QEMU_OPTION_tdf
:
5506 case QEMU_OPTION_kvm_shadow_memory
:
5507 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5509 case QEMU_OPTION_mempath
:
5513 case QEMU_OPTION_mem_prealloc
:
5514 mem_prealloc
= !mem_prealloc
;
5517 case QEMU_OPTION_name
:
5520 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5521 case QEMU_OPTION_prom_env
:
5522 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5523 fprintf(stderr
, "Too many prom variables\n");
5526 prom_envs
[nb_prom_envs
] = optarg
;
5530 case QEMU_OPTION_cpu_vendor
:
5531 cpu_vendor_string
= optarg
;
5534 case QEMU_OPTION_old_param
:
5538 case QEMU_OPTION_clock
:
5539 configure_alarms(optarg
);
5541 case QEMU_OPTION_startdate
:
5544 time_t rtc_start_date
;
5545 if (!strcmp(optarg
, "now")) {
5546 rtc_date_offset
= -1;
5548 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5556 } else if (sscanf(optarg
, "%d-%d-%d",
5559 &tm
.tm_mday
) == 3) {
5568 rtc_start_date
= mktimegm(&tm
);
5569 if (rtc_start_date
== -1) {
5571 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5572 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5575 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5579 case QEMU_OPTION_tb_size
:
5580 tb_size
= strtol(optarg
, NULL
, 0);
5584 case QEMU_OPTION_icount
:
5586 if (strcmp(optarg
, "auto") == 0) {
5587 icount_time_shift
= -1;
5589 icount_time_shift
= strtol(optarg
, NULL
, 0);
5592 case QEMU_OPTION_incoming
:
5595 case QEMU_OPTION_chroot
:
5596 chroot_dir
= optarg
;
5598 case QEMU_OPTION_runas
:
5600 case QEMU_OPTION_nvram
:
5607 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5608 if (kvm_allowed
&& kqemu_allowed
) {
5610 "You can not enable both KVM and kqemu at the same time\n");
5615 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5616 if (smp_cpus
> machine
->max_cpus
) {
5617 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5618 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5624 if (serial_device_index
== 0)
5625 serial_devices
[0] = "stdio";
5626 if (parallel_device_index
== 0)
5627 parallel_devices
[0] = "null";
5628 if (strncmp(monitor_device
, "vc", 2) == 0)
5629 monitor_device
= "stdio";
5636 if (pipe(fds
) == -1)
5647 len
= read(fds
[0], &status
, 1);
5648 if (len
== -1 && (errno
== EINTR
))
5653 else if (status
== 1) {
5654 fprintf(stderr
, "Could not acquire pidfile\n");
5671 signal(SIGTSTP
, SIG_IGN
);
5672 signal(SIGTTOU
, SIG_IGN
);
5673 signal(SIGTTIN
, SIG_IGN
);
5678 if (kvm_enabled()) {
5679 if (kvm_qemu_init() < 0) {
5680 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5681 #ifdef NO_CPU_EMULATION
5682 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5690 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5693 write(fds
[1], &status
, 1);
5695 fprintf(stderr
, "Could not acquire pid file\n");
5703 linux_boot
= (kernel_filename
!= NULL
);
5704 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5706 if (!linux_boot
&& net_boot
== 0 &&
5707 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5710 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5711 fprintf(stderr
, "-append only allowed with -kernel option\n");
5715 if (!linux_boot
&& initrd_filename
!= NULL
) {
5716 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5720 /* boot to floppy or the default cd if no hard disk defined yet */
5721 if (!boot_devices
[0]) {
5722 boot_devices
= "cad";
5724 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5727 if (init_timer_alarm() < 0) {
5728 fprintf(stderr
, "could not initialize alarm timer\n");
5731 if (use_icount
&& icount_time_shift
< 0) {
5733 /* 125MIPS seems a reasonable initial guess at the guest speed.
5734 It will be corrected fairly quickly anyway. */
5735 icount_time_shift
= 3;
5736 init_icount_adjust();
5743 /* init network clients */
5744 if (nb_net_clients
== 0) {
5745 /* if no clients, we use a default config */
5746 net_clients
[nb_net_clients
++] = "nic";
5748 net_clients
[nb_net_clients
++] = "user";
5752 for(i
= 0;i
< nb_net_clients
; i
++) {
5753 if (net_client_parse(net_clients
[i
]) < 0)
5759 /* XXX: this should be moved in the PC machine instantiation code */
5760 if (net_boot
!= 0) {
5762 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5763 const char *model
= nd_table
[i
].model
;
5765 if (net_boot
& (1 << i
)) {
5768 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5769 if (get_image_size(buf
) > 0) {
5770 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5771 fprintf(stderr
, "Too many option ROMs\n");
5774 option_rom
[nb_option_roms
] = strdup(buf
);
5781 fprintf(stderr
, "No valid PXE rom found for network device\n");
5787 /* init the bluetooth world */
5788 for (i
= 0; i
< nb_bt_opts
; i
++)
5789 if (bt_parse(bt_opts
[i
]))
5792 /* init the memory */
5793 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5795 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5797 if (ram_size
< phys_ram_size
) {
5798 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5799 machine
->name
, (unsigned long long) phys_ram_size
);
5803 phys_ram_size
= ram_size
;
5805 ram_size
= phys_ram_size
;
5808 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5810 phys_ram_size
+= ram_size
;
5813 /* Initialize kvm */
5814 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5815 #define KVM_EXTRA_PAGES 3
5817 #define KVM_EXTRA_PAGES 0
5819 if (kvm_enabled()) {
5820 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5821 if (kvm_qemu_create_context() < 0) {
5822 fprintf(stderr
, "Could not create KVM context\n");
5827 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5828 if (!phys_ram_base
) {
5829 fprintf(stderr
, "Could not allocate physical memory\n");
5833 /* init the dynamic translator */
5834 cpu_exec_init_all(tb_size
* 1024 * 1024);
5839 /* we always create the cdrom drive, even if no disk is there */
5841 if (nb_drives_opt
< MAX_DRIVES
)
5842 drive_add(NULL
, CDROM_ALIAS
);
5844 /* we always create at least one floppy */
5846 if (nb_drives_opt
< MAX_DRIVES
)
5847 drive_add(NULL
, FD_ALIAS
, 0);
5849 /* we always create one sd slot, even if no card is in it */
5851 if (nb_drives_opt
< MAX_DRIVES
)
5852 drive_add(NULL
, SD_ALIAS
);
5854 /* open the virtual block devices
5855 * note that migration with device
5856 * hot add/remove is broken.
5858 for(i
= 0; i
< nb_drives_opt
; i
++)
5859 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5862 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5863 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5866 /* must be after terminal init, SDL library changes signal handlers */
5870 /* Maintain compatibility with multiple stdio monitors */
5871 if (!strcmp(monitor_device
,"stdio")) {
5872 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5873 const char *devname
= serial_devices
[i
];
5874 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5875 monitor_device
= NULL
;
5877 } else if (devname
&& !strcmp(devname
,"stdio")) {
5878 monitor_device
= NULL
;
5879 serial_devices
[i
] = "mon:stdio";
5886 if (kvm_enabled()) {
5889 ret
= kvm_init(smp_cpus
);
5891 fprintf(stderr
, "failed to initialize KVM\n");
5897 if (monitor_device
) {
5898 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5900 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5905 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5906 const char *devname
= serial_devices
[i
];
5907 if (devname
&& strcmp(devname
, "none")) {
5909 snprintf(label
, sizeof(label
), "serial%d", i
);
5910 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5911 if (!serial_hds
[i
]) {
5912 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5919 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5920 const char *devname
= parallel_devices
[i
];
5921 if (devname
&& strcmp(devname
, "none")) {
5923 snprintf(label
, sizeof(label
), "parallel%d", i
);
5924 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5925 if (!parallel_hds
[i
]) {
5926 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5933 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5934 const char *devname
= virtio_consoles
[i
];
5935 if (devname
&& strcmp(devname
, "none")) {
5937 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5938 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5939 if (!virtcon_hds
[i
]) {
5940 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5950 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5951 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5953 current_machine
= machine
;
5955 /* Set KVM's vcpu state to qemu's initial CPUState. */
5956 if (kvm_enabled()) {
5959 ret
= kvm_sync_vcpus();
5961 fprintf(stderr
, "failed to initialize vcpus\n");
5966 /* init USB devices */
5968 for(i
= 0; i
< usb_devices_index
; i
++) {
5969 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5970 fprintf(stderr
, "Warning: could not add USB device %s\n",
5977 dumb_display_init();
5978 /* just use the first displaystate for the moment */
5983 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5987 #if defined(CONFIG_CURSES)
5989 /* At the moment curses cannot be used with other displays */
5990 curses_display_init(ds
, full_screen
);
5994 if (vnc_display
!= NULL
) {
5995 vnc_display_init(ds
);
5996 if (vnc_display_open(ds
, vnc_display
) < 0)
5999 #if defined(CONFIG_SDL)
6000 if (sdl
|| !vnc_display
)
6001 sdl_display_init(ds
, full_screen
, no_frame
);
6002 #elif defined(CONFIG_COCOA)
6003 if (sdl
|| !vnc_display
)
6004 cocoa_display_init(ds
, full_screen
);
6010 dcl
= ds
->listeners
;
6011 while (dcl
!= NULL
) {
6012 if (dcl
->dpy_refresh
!= NULL
) {
6013 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6014 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6019 if (nographic
|| (vnc_display
&& !sdl
)) {
6020 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6021 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6024 text_consoles_set_display(display_state
);
6025 qemu_chr_initial_reset();
6027 if (monitor_device
&& monitor_hd
)
6028 monitor_init(monitor_hd
, !nographic
);
6030 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6031 const char *devname
= serial_devices
[i
];
6032 if (devname
&& strcmp(devname
, "none")) {
6034 snprintf(label
, sizeof(label
), "serial%d", i
);
6035 if (strstart(devname
, "vc", 0))
6036 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6040 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6041 const char *devname
= parallel_devices
[i
];
6042 if (devname
&& strcmp(devname
, "none")) {
6044 snprintf(label
, sizeof(label
), "parallel%d", i
);
6045 if (strstart(devname
, "vc", 0))
6046 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6050 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6051 const char *devname
= virtio_consoles
[i
];
6052 if (virtcon_hds
[i
] && devname
) {
6054 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6055 if (strstart(devname
, "vc", 0))
6056 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6060 #ifdef CONFIG_GDBSTUB
6062 /* XXX: use standard host:port notation and modify options
6064 if (gdbserver_start(gdbstub_port
) < 0) {
6065 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
6076 qemu_start_incoming_migration(incoming
);
6086 len
= write(fds
[1], &status
, 1);
6087 if (len
== -1 && (errno
== EINTR
))
6094 TFR(fd
= open("/dev/null", O_RDWR
));
6101 pwd
= getpwnam(run_as
);
6103 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6109 if (chroot(chroot_dir
) < 0) {
6110 fprintf(stderr
, "chroot failed\n");
6117 if (setgid(pwd
->pw_gid
) < 0) {
6118 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6121 if (setuid(pwd
->pw_uid
) < 0) {
6122 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6125 if (setuid(0) != -1) {
6126 fprintf(stderr
, "Dropping privileges failed\n");