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>
65 #include <netinet/in.h>
67 #if defined(__NetBSD__)
68 #include <net/if_tap.h>
71 #include <linux/if_tun.h>
73 #include <arpa/inet.h>
76 #include <sys/select.h>
84 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
85 #include <freebsd/stdlib.h>
90 #include <linux/rtc.h>
92 /* For the benefit of older linux systems which don't supply it,
93 we use a local copy of hpet.h. */
94 /* #include <linux/hpet.h> */
97 #include <linux/ppdev.h>
98 #include <linux/parport.h>
101 #include <sys/stat.h>
102 #include <sys/ethernet.h>
103 #include <sys/sockio.h>
104 #include <netinet/arp.h>
105 #include <netinet/in.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/ip.h>
108 #include <netinet/ip_icmp.h> // must come after ip.h
109 #include <netinet/udp.h>
110 #include <netinet/tcp.h>
118 #include "qemu_socket.h"
120 #if defined(CONFIG_SLIRP)
121 #include "libslirp.h"
124 #if defined(__OpenBSD__)
128 #if defined(CONFIG_VDE)
129 #include <libvdeplug.h>
134 #include <sys/timeb.h>
135 #include <mmsystem.h>
136 #define getopt_long_only getopt_long
137 #define memalign(align, size) malloc(size)
143 int qemu_main(int argc
, char **argv
, char **envp
);
144 int main(int argc
, char **argv
)
146 qemu_main(argc
, argv
, NULL
);
149 #define main qemu_main
151 #endif /* CONFIG_SDL */
155 #define main qemu_main
156 #endif /* CONFIG_COCOA */
160 #include "exec-all.h"
162 #include "qemu-kvm.h"
164 //#define DEBUG_UNUSED_IOPORT
165 //#define DEBUG_IOPORT
167 //#define DEBUG_SLIRP
171 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
173 # define LOG_IOPORT(...) do { } while (0)
176 #define DEFAULT_RAM_SIZE 128
178 /* Max number of USB devices that can be specified on the commandline. */
179 #define MAX_USB_CMDLINE 8
181 /* Max number of bluetooth switches on the commandline. */
182 #define MAX_BT_CMDLINE 10
184 /* XXX: use a two level table to limit memory usage */
185 #define MAX_IOPORTS 65536
187 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
188 const char *bios_name
= NULL
;
189 static void *ioport_opaque
[MAX_IOPORTS
];
190 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
191 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
192 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
193 to store the VM snapshots */
194 DriveInfo drives_table
[MAX_DRIVES
+1];
196 int extboot_drive
= -1;
197 static int vga_ram_size
;
198 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
199 static DisplayState
*display_state
;
203 const char* keyboard_layout
= NULL
;
204 int64_t ticks_per_sec
;
207 NICInfo nd_table
[MAX_NICS
];
209 static int autostart
;
210 static int rtc_utc
= 1;
211 static int rtc_date_offset
= -1; /* -1 means no change */
212 int cirrus_vga_enabled
= 1;
213 int std_vga_enabled
= 0;
214 int vmsvga_enabled
= 0;
216 int graphic_width
= 1024;
217 int graphic_height
= 768;
218 int graphic_depth
= 8;
220 int graphic_width
= 800;
221 int graphic_height
= 600;
222 int graphic_depth
= 15;
224 static int full_screen
= 0;
226 static int no_frame
= 0;
229 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
230 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
231 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
233 int win2k_install_hack
= 0;
237 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
238 int assigned_devices_index
;
240 const char *vnc_display
;
241 int acpi_enabled
= 1;
247 int graphic_rotate
= 0;
249 const char *incoming
;
250 const char *option_rom
[MAX_OPTION_ROMS
];
252 int semihosting_enabled
= 0;
253 int time_drift_fix
= 0;
254 unsigned int kvm_shadow_memory
= 0;
255 const char *mem_path
= NULL
;
257 int mem_prealloc
= 1; /* force preallocation of physical target memory */
260 const char *cpu_vendor_string
;
264 const char *qemu_name
;
266 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
267 unsigned int nb_prom_envs
= 0;
268 const char *prom_envs
[MAX_PROM_ENVS
];
271 const char *nvram
= NULL
;
272 struct drive_opt drives_opt
[MAX_DRIVES
];
274 static CPUState
*cur_cpu
;
275 static CPUState
*next_cpu
;
276 static int event_pending
= 1;
277 /* Conversion factor from emulated instructions to virtual clock ticks. */
278 static int icount_time_shift
;
279 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
280 #define MAX_ICOUNT_SHIFT 10
281 /* Compensate for varying guest execution speed. */
282 static int64_t qemu_icount_bias
;
283 static QEMUTimer
*icount_rt_timer
;
284 static QEMUTimer
*icount_vm_timer
;
285 static QEMUTimer
*nographic_timer
;
287 uint8_t qemu_uuid
[16];
289 /* KVM runs the main loop in a separate thread. If we update one of the lists
290 * that are polled before or after select(), we need to make sure to break out
291 * of the select() to ensure the new item is serviced.
293 static void main_loop_break(void)
296 qemu_kvm_notify_work();
299 /***********************************************************/
300 /* x86 ISA bus support */
302 target_phys_addr_t isa_mem_base
= 0;
305 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
306 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
308 static uint32_t ioport_read(int index
, uint32_t address
)
310 static IOPortReadFunc
*default_func
[3] = {
311 default_ioport_readb
,
312 default_ioport_readw
,
315 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
317 func
= default_func
[index
];
318 return func(ioport_opaque
[address
], address
);
321 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
323 static IOPortWriteFunc
*default_func
[3] = {
324 default_ioport_writeb
,
325 default_ioport_writew
,
326 default_ioport_writel
328 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
330 func
= default_func
[index
];
331 func(ioport_opaque
[address
], address
, data
);
334 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
336 #ifdef DEBUG_UNUSED_IOPORT
337 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
342 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
344 #ifdef DEBUG_UNUSED_IOPORT
345 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
349 /* default is to make two byte accesses */
350 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
353 data
= ioport_read(0, address
);
354 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
355 data
|= ioport_read(0, address
) << 8;
359 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
361 ioport_write(0, address
, data
& 0xff);
362 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
363 ioport_write(0, address
, (data
>> 8) & 0xff);
366 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
368 #ifdef DEBUG_UNUSED_IOPORT
369 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
374 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
376 #ifdef DEBUG_UNUSED_IOPORT
377 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
381 /* size is the word size in byte */
382 int register_ioport_read(int start
, int length
, int size
,
383 IOPortReadFunc
*func
, void *opaque
)
389 } else if (size
== 2) {
391 } else if (size
== 4) {
394 hw_error("register_ioport_read: invalid size");
397 for(i
= start
; i
< start
+ length
; i
+= size
) {
398 ioport_read_table
[bsize
][i
] = func
;
399 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
400 hw_error("register_ioport_read: invalid opaque");
401 ioport_opaque
[i
] = opaque
;
406 /* size is the word size in byte */
407 int register_ioport_write(int start
, int length
, int size
,
408 IOPortWriteFunc
*func
, void *opaque
)
414 } else if (size
== 2) {
416 } else if (size
== 4) {
419 hw_error("register_ioport_write: invalid size");
422 for(i
= start
; i
< start
+ length
; i
+= size
) {
423 ioport_write_table
[bsize
][i
] = func
;
424 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
425 hw_error("register_ioport_write: invalid opaque");
426 ioport_opaque
[i
] = opaque
;
431 void isa_unassign_ioport(int start
, int length
)
435 for(i
= start
; i
< start
+ length
; i
++) {
436 ioport_read_table
[0][i
] = default_ioport_readb
;
437 ioport_read_table
[1][i
] = default_ioport_readw
;
438 ioport_read_table
[2][i
] = default_ioport_readl
;
440 ioport_write_table
[0][i
] = default_ioport_writeb
;
441 ioport_write_table
[1][i
] = default_ioport_writew
;
442 ioport_write_table
[2][i
] = default_ioport_writel
;
444 ioport_opaque
[i
] = NULL
;
448 /***********************************************************/
450 void cpu_outb(CPUState
*env
, int addr
, int val
)
452 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
453 ioport_write(0, addr
, val
);
456 env
->last_io_time
= cpu_get_time_fast();
460 void cpu_outw(CPUState
*env
, int addr
, int val
)
462 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
463 ioport_write(1, addr
, val
);
466 env
->last_io_time
= cpu_get_time_fast();
470 void cpu_outl(CPUState
*env
, int addr
, int val
)
472 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
473 ioport_write(2, addr
, val
);
476 env
->last_io_time
= cpu_get_time_fast();
480 int cpu_inb(CPUState
*env
, int addr
)
483 val
= ioport_read(0, addr
);
484 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
487 env
->last_io_time
= cpu_get_time_fast();
492 int cpu_inw(CPUState
*env
, int addr
)
495 val
= ioport_read(1, addr
);
496 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
499 env
->last_io_time
= cpu_get_time_fast();
504 int cpu_inl(CPUState
*env
, int addr
)
507 val
= ioport_read(2, addr
);
508 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
511 env
->last_io_time
= cpu_get_time_fast();
516 /***********************************************************/
517 void hw_error(const char *fmt
, ...)
523 fprintf(stderr
, "qemu: hardware error: ");
524 vfprintf(stderr
, fmt
, ap
);
525 fprintf(stderr
, "\n");
526 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
527 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
529 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
531 cpu_dump_state(env
, stderr
, fprintf
, 0);
541 static QEMUBalloonEvent
*qemu_balloon_event
;
542 void *qemu_balloon_event_opaque
;
544 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
546 qemu_balloon_event
= func
;
547 qemu_balloon_event_opaque
= opaque
;
550 void qemu_balloon(ram_addr_t target
)
552 if (qemu_balloon_event
)
553 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
556 ram_addr_t
qemu_balloon_status(void)
558 if (qemu_balloon_event
)
559 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
563 /***********************************************************/
566 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
567 static void *qemu_put_kbd_event_opaque
;
568 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
569 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
571 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
573 qemu_put_kbd_event_opaque
= opaque
;
574 qemu_put_kbd_event
= func
;
577 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
578 void *opaque
, int absolute
,
581 QEMUPutMouseEntry
*s
, *cursor
;
583 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
585 s
->qemu_put_mouse_event
= func
;
586 s
->qemu_put_mouse_event_opaque
= opaque
;
587 s
->qemu_put_mouse_event_absolute
= absolute
;
588 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
591 if (!qemu_put_mouse_event_head
) {
592 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
596 cursor
= qemu_put_mouse_event_head
;
597 while (cursor
->next
!= NULL
)
598 cursor
= cursor
->next
;
601 qemu_put_mouse_event_current
= s
;
606 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
608 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
610 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
613 cursor
= qemu_put_mouse_event_head
;
614 while (cursor
!= NULL
&& cursor
!= entry
) {
616 cursor
= cursor
->next
;
619 if (cursor
== NULL
) // does not exist or list empty
621 else if (prev
== NULL
) { // entry is head
622 qemu_put_mouse_event_head
= cursor
->next
;
623 if (qemu_put_mouse_event_current
== entry
)
624 qemu_put_mouse_event_current
= cursor
->next
;
625 qemu_free(entry
->qemu_put_mouse_event_name
);
630 prev
->next
= entry
->next
;
632 if (qemu_put_mouse_event_current
== entry
)
633 qemu_put_mouse_event_current
= prev
;
635 qemu_free(entry
->qemu_put_mouse_event_name
);
639 void kbd_put_keycode(int keycode
)
641 if (qemu_put_kbd_event
) {
642 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
646 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
648 QEMUPutMouseEvent
*mouse_event
;
649 void *mouse_event_opaque
;
652 if (!qemu_put_mouse_event_current
) {
657 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
659 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
662 if (graphic_rotate
) {
663 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
666 width
= graphic_width
- 1;
667 mouse_event(mouse_event_opaque
,
668 width
- dy
, dx
, dz
, buttons_state
);
670 mouse_event(mouse_event_opaque
,
671 dx
, dy
, dz
, buttons_state
);
675 int kbd_mouse_is_absolute(void)
677 if (!qemu_put_mouse_event_current
)
680 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
683 void do_info_mice(void)
685 QEMUPutMouseEntry
*cursor
;
688 if (!qemu_put_mouse_event_head
) {
689 term_printf("No mouse devices connected\n");
693 term_printf("Mouse devices available:\n");
694 cursor
= qemu_put_mouse_event_head
;
695 while (cursor
!= NULL
) {
696 term_printf("%c Mouse #%d: %s\n",
697 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
698 index
, cursor
->qemu_put_mouse_event_name
);
700 cursor
= cursor
->next
;
704 void do_mouse_set(int index
)
706 QEMUPutMouseEntry
*cursor
;
709 if (!qemu_put_mouse_event_head
) {
710 term_printf("No mouse devices connected\n");
714 cursor
= qemu_put_mouse_event_head
;
715 while (cursor
!= NULL
&& index
!= i
) {
717 cursor
= cursor
->next
;
721 qemu_put_mouse_event_current
= cursor
;
723 term_printf("Mouse at given index not found\n");
726 /* compute with 96 bit intermediate result: (a*b)/c */
727 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
732 #ifdef WORDS_BIGENDIAN
742 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
743 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
746 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
750 /***********************************************************/
751 /* real time host monotonic timer */
753 #define QEMU_TIMER_BASE 1000000000LL
757 static int64_t clock_freq
;
759 static void init_get_clock(void)
763 ret
= QueryPerformanceFrequency(&freq
);
765 fprintf(stderr
, "Could not calibrate ticks\n");
768 clock_freq
= freq
.QuadPart
;
771 static int64_t get_clock(void)
774 QueryPerformanceCounter(&ti
);
775 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
780 static int use_rt_clock
;
782 static void init_get_clock(void)
785 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
788 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
795 static int64_t get_clock(void)
797 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000)
800 clock_gettime(CLOCK_MONOTONIC
, &ts
);
801 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
805 /* XXX: using gettimeofday leads to problems if the date
806 changes, so it should be avoided. */
808 gettimeofday(&tv
, NULL
);
809 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
814 /* Return the virtual CPU time, based on the instruction counter. */
815 static int64_t cpu_get_icount(void)
818 CPUState
*env
= cpu_single_env
;;
819 icount
= qemu_icount
;
822 fprintf(stderr
, "Bad clock read\n");
823 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
825 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
828 /***********************************************************/
829 /* guest cycle counter */
831 static int64_t cpu_ticks_prev
;
832 static int64_t cpu_ticks_offset
;
833 static int64_t cpu_clock_offset
;
834 static int cpu_ticks_enabled
;
836 /* return the host CPU cycle counter and handle stop/restart */
837 int64_t cpu_get_ticks(void)
840 return cpu_get_icount();
842 if (!cpu_ticks_enabled
) {
843 return cpu_ticks_offset
;
846 ticks
= cpu_get_real_ticks();
847 if (cpu_ticks_prev
> ticks
) {
848 /* Note: non increasing ticks may happen if the host uses
850 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
852 cpu_ticks_prev
= ticks
;
853 return ticks
+ cpu_ticks_offset
;
857 /* return the host CPU monotonic timer and handle stop/restart */
858 static int64_t cpu_get_clock(void)
861 if (!cpu_ticks_enabled
) {
862 return cpu_clock_offset
;
865 return ti
+ cpu_clock_offset
;
869 /* enable cpu_get_ticks() */
870 void cpu_enable_ticks(void)
872 if (!cpu_ticks_enabled
) {
873 cpu_ticks_offset
-= cpu_get_real_ticks();
874 cpu_clock_offset
-= get_clock();
875 cpu_ticks_enabled
= 1;
879 /* disable cpu_get_ticks() : the clock is stopped. You must not call
880 cpu_get_ticks() after that. */
881 void cpu_disable_ticks(void)
883 if (cpu_ticks_enabled
) {
884 cpu_ticks_offset
= cpu_get_ticks();
885 cpu_clock_offset
= cpu_get_clock();
886 cpu_ticks_enabled
= 0;
890 /***********************************************************/
893 #define QEMU_TIMER_REALTIME 0
894 #define QEMU_TIMER_VIRTUAL 1
898 /* XXX: add frequency */
906 struct QEMUTimer
*next
;
909 struct qemu_alarm_timer
{
913 int (*start
)(struct qemu_alarm_timer
*t
);
914 void (*stop
)(struct qemu_alarm_timer
*t
);
915 void (*rearm
)(struct qemu_alarm_timer
*t
);
919 #define ALARM_FLAG_DYNTICKS 0x1
920 #define ALARM_FLAG_EXPIRED 0x2
922 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
924 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
927 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
929 if (!alarm_has_dynticks(t
))
935 /* TODO: MIN_TIMER_REARM_US should be optimized */
936 #define MIN_TIMER_REARM_US 250
938 static struct qemu_alarm_timer
*alarm_timer
;
940 static int alarm_timer_rfd
, alarm_timer_wfd
;
945 struct qemu_alarm_win32
{
949 } alarm_win32_data
= {0, NULL
, -1};
951 static int win32_start_timer(struct qemu_alarm_timer
*t
);
952 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
953 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
957 static int unix_start_timer(struct qemu_alarm_timer
*t
);
958 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
962 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
963 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
964 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
966 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
967 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
969 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
970 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
972 #endif /* __linux__ */
976 /* Correlation between real and virtual time is always going to be
977 fairly approximate, so ignore small variation.
978 When the guest is idle real and virtual time will be aligned in
980 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
982 static void icount_adjust(void)
987 static int64_t last_delta
;
988 /* If the VM is not running, then do nothing. */
992 cur_time
= cpu_get_clock();
993 cur_icount
= qemu_get_clock(vm_clock
);
994 delta
= cur_icount
- cur_time
;
995 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
997 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
998 && icount_time_shift
> 0) {
999 /* The guest is getting too far ahead. Slow time down. */
1000 icount_time_shift
--;
1003 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
1004 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
1005 /* The guest is getting too far behind. Speed time up. */
1006 icount_time_shift
++;
1009 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1012 static void icount_adjust_rt(void * opaque
)
1014 qemu_mod_timer(icount_rt_timer
,
1015 qemu_get_clock(rt_clock
) + 1000);
1019 static void icount_adjust_vm(void * opaque
)
1021 qemu_mod_timer(icount_vm_timer
,
1022 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1026 static void init_icount_adjust(void)
1028 /* Have both realtime and virtual time triggers for speed adjustment.
1029 The realtime trigger catches emulated time passing too slowly,
1030 the virtual time trigger catches emulated time passing too fast.
1031 Realtime triggers occur even when idle, so use them less frequently
1032 than VM triggers. */
1033 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1034 qemu_mod_timer(icount_rt_timer
,
1035 qemu_get_clock(rt_clock
) + 1000);
1036 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1037 qemu_mod_timer(icount_vm_timer
,
1038 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1041 static struct qemu_alarm_timer alarm_timers
[] = {
1044 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1045 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1046 /* HPET - if available - is preferred */
1047 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1048 /* ...otherwise try RTC */
1049 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1051 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1053 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1054 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1055 {"win32", 0, win32_start_timer
,
1056 win32_stop_timer
, NULL
, &alarm_win32_data
},
1061 static void show_available_alarms(void)
1065 printf("Available alarm timers, in order of precedence:\n");
1066 for (i
= 0; alarm_timers
[i
].name
; i
++)
1067 printf("%s\n", alarm_timers
[i
].name
);
1070 static void configure_alarms(char const *opt
)
1074 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1077 struct qemu_alarm_timer tmp
;
1079 if (!strcmp(opt
, "?")) {
1080 show_available_alarms();
1086 /* Reorder the array */
1087 name
= strtok(arg
, ",");
1089 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1090 if (!strcmp(alarm_timers
[i
].name
, name
))
1095 fprintf(stderr
, "Unknown clock %s\n", name
);
1104 tmp
= alarm_timers
[i
];
1105 alarm_timers
[i
] = alarm_timers
[cur
];
1106 alarm_timers
[cur
] = tmp
;
1110 name
= strtok(NULL
, ",");
1116 /* Disable remaining timers */
1117 for (i
= cur
; i
< count
; i
++)
1118 alarm_timers
[i
].name
= NULL
;
1120 show_available_alarms();
1125 QEMUClock
*rt_clock
;
1126 QEMUClock
*vm_clock
;
1128 static QEMUTimer
*active_timers
[2];
1130 static QEMUClock
*qemu_new_clock(int type
)
1133 clock
= qemu_mallocz(sizeof(QEMUClock
));
1138 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1142 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1145 ts
->opaque
= opaque
;
1149 void qemu_free_timer(QEMUTimer
*ts
)
1154 /* stop a timer, but do not dealloc it */
1155 void qemu_del_timer(QEMUTimer
*ts
)
1159 /* NOTE: this code must be signal safe because
1160 qemu_timer_expired() can be called from a signal. */
1161 pt
= &active_timers
[ts
->clock
->type
];
1174 /* modify the current timer so that it will be fired when current_time
1175 >= expire_time. The corresponding callback will be called. */
1176 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1182 /* add the timer in the sorted list */
1183 /* NOTE: this code must be signal safe because
1184 qemu_timer_expired() can be called from a signal. */
1185 pt
= &active_timers
[ts
->clock
->type
];
1190 if (t
->expire_time
> expire_time
)
1194 ts
->expire_time
= expire_time
;
1198 /* Rearm if necessary */
1199 if (pt
== &active_timers
[ts
->clock
->type
]) {
1200 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1201 qemu_rearm_alarm_timer(alarm_timer
);
1203 /* Interrupt execution to force deadline recalculation. */
1204 if (use_icount
&& cpu_single_env
) {
1205 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
1210 int qemu_timer_pending(QEMUTimer
*ts
)
1213 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1220 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1224 return (timer_head
->expire_time
<= current_time
);
1227 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1233 if (!ts
|| ts
->expire_time
> current_time
)
1235 /* remove timer from the list before calling the callback */
1236 *ptimer_head
= ts
->next
;
1239 /* run the callback (the timer list can be modified) */
1244 int64_t qemu_get_clock(QEMUClock
*clock
)
1246 switch(clock
->type
) {
1247 case QEMU_TIMER_REALTIME
:
1248 return get_clock() / 1000000;
1250 case QEMU_TIMER_VIRTUAL
:
1252 return cpu_get_icount();
1254 return cpu_get_clock();
1259 static void init_timers(void)
1262 ticks_per_sec
= QEMU_TIMER_BASE
;
1263 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1264 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1268 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1270 uint64_t expire_time
;
1272 if (qemu_timer_pending(ts
)) {
1273 expire_time
= ts
->expire_time
;
1277 qemu_put_be64(f
, expire_time
);
1280 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1282 uint64_t expire_time
;
1284 expire_time
= qemu_get_be64(f
);
1285 if (expire_time
!= -1) {
1286 qemu_mod_timer(ts
, expire_time
);
1292 static void timer_save(QEMUFile
*f
, void *opaque
)
1294 if (cpu_ticks_enabled
) {
1295 hw_error("cannot save state if virtual timers are running");
1297 qemu_put_be64(f
, cpu_ticks_offset
);
1298 qemu_put_be64(f
, ticks_per_sec
);
1299 qemu_put_be64(f
, cpu_clock_offset
);
1302 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1304 if (version_id
!= 1 && version_id
!= 2)
1306 if (cpu_ticks_enabled
) {
1309 cpu_ticks_offset
=qemu_get_be64(f
);
1310 ticks_per_sec
=qemu_get_be64(f
);
1311 if (version_id
== 2) {
1312 cpu_clock_offset
=qemu_get_be64(f
);
1318 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1319 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1321 static void host_alarm_handler(int host_signum
)
1325 #define DISP_FREQ 1000
1327 static int64_t delta_min
= INT64_MAX
;
1328 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1330 ti
= qemu_get_clock(vm_clock
);
1331 if (last_clock
!= 0) {
1332 delta
= ti
- last_clock
;
1333 if (delta
< delta_min
)
1335 if (delta
> delta_max
)
1338 if (++count
== DISP_FREQ
) {
1339 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1340 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1341 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1342 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1343 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1345 delta_min
= INT64_MAX
;
1354 alarm_has_dynticks(alarm_timer
) ||
1356 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1357 qemu_get_clock(vm_clock
))) ||
1358 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1359 qemu_get_clock(rt_clock
))) {
1360 CPUState
*env
= next_cpu
;
1363 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1364 SetEvent(data
->host_alarm
);
1366 static const char byte
= 0;
1367 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1369 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1372 /* stop the currently executing cpu because a timer occured */
1373 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
1375 if (env
->kqemu_enabled
) {
1376 kqemu_cpu_interrupt(env
);
1384 static int64_t qemu_next_deadline(void)
1388 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1389 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1390 qemu_get_clock(vm_clock
);
1392 /* To avoid problems with overflow limit this to 2^32. */
1402 #if defined(__linux__) || defined(_WIN32)
1403 static uint64_t qemu_next_deadline_dyntick(void)
1411 delta
= (qemu_next_deadline() + 999) / 1000;
1413 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1414 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1415 qemu_get_clock(rt_clock
))*1000;
1416 if (rtdelta
< delta
)
1420 if (delta
< MIN_TIMER_REARM_US
)
1421 delta
= MIN_TIMER_REARM_US
;
1429 /* Sets a specific flag */
1430 static int fcntl_setfl(int fd
, int flag
)
1434 flags
= fcntl(fd
, F_GETFL
);
1438 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1444 #if defined(__linux__)
1446 #define RTC_FREQ 1024
1448 static void enable_sigio_timer(int fd
)
1450 struct sigaction act
;
1453 sigfillset(&act
.sa_mask
);
1455 act
.sa_handler
= host_alarm_handler
;
1457 sigaction(SIGIO
, &act
, NULL
);
1458 fcntl_setfl(fd
, O_ASYNC
);
1459 fcntl(fd
, F_SETOWN
, getpid());
1462 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1464 struct hpet_info info
;
1467 fd
= open("/dev/hpet", O_RDONLY
);
1472 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1474 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1475 "error, but for better emulation accuracy type:\n"
1476 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1480 /* Check capabilities */
1481 r
= ioctl(fd
, HPET_INFO
, &info
);
1485 /* Enable periodic mode */
1486 r
= ioctl(fd
, HPET_EPI
, 0);
1487 if (info
.hi_flags
&& (r
< 0))
1490 /* Enable interrupt */
1491 r
= ioctl(fd
, HPET_IE_ON
, 0);
1495 enable_sigio_timer(fd
);
1496 t
->priv
= (void *)(long)fd
;
1504 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1506 int fd
= (long)t
->priv
;
1511 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1514 unsigned long current_rtc_freq
= 0;
1516 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1519 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1520 if (current_rtc_freq
!= RTC_FREQ
&&
1521 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1522 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1523 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1524 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1527 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1533 enable_sigio_timer(rtc_fd
);
1535 t
->priv
= (void *)(long)rtc_fd
;
1540 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1542 int rtc_fd
= (long)t
->priv
;
1547 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1551 struct sigaction act
;
1553 sigfillset(&act
.sa_mask
);
1555 act
.sa_handler
= host_alarm_handler
;
1557 sigaction(SIGALRM
, &act
, NULL
);
1560 * Initialize ev struct to 0 to avoid valgrind complaining
1561 * about uninitialized data in timer_create call
1563 memset(&ev
, 0, sizeof(ev
));
1564 ev
.sigev_value
.sival_int
= 0;
1565 ev
.sigev_notify
= SIGEV_SIGNAL
;
1566 ev
.sigev_signo
= SIGALRM
;
1568 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1569 perror("timer_create");
1571 /* disable dynticks */
1572 fprintf(stderr
, "Dynamic Ticks disabled\n");
1577 t
->priv
= (void *)(long)host_timer
;
1582 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1584 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1586 timer_delete(host_timer
);
1589 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1591 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1592 struct itimerspec timeout
;
1593 int64_t nearest_delta_us
= INT64_MAX
;
1596 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1597 !active_timers
[QEMU_TIMER_VIRTUAL
])
1600 nearest_delta_us
= qemu_next_deadline_dyntick();
1602 /* check whether a timer is already running */
1603 if (timer_gettime(host_timer
, &timeout
)) {
1605 fprintf(stderr
, "Internal timer error: aborting\n");
1608 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1609 if (current_us
&& current_us
<= nearest_delta_us
)
1612 timeout
.it_interval
.tv_sec
= 0;
1613 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1614 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1615 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1616 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1618 fprintf(stderr
, "Internal timer error: aborting\n");
1623 #endif /* defined(__linux__) */
1625 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1627 struct sigaction act
;
1628 struct itimerval itv
;
1632 sigfillset(&act
.sa_mask
);
1634 act
.sa_handler
= host_alarm_handler
;
1636 sigaction(SIGALRM
, &act
, NULL
);
1638 itv
.it_interval
.tv_sec
= 0;
1639 /* for i386 kernel 2.6 to get 1 ms */
1640 itv
.it_interval
.tv_usec
= 999;
1641 itv
.it_value
.tv_sec
= 0;
1642 itv
.it_value
.tv_usec
= 10 * 1000;
1644 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1651 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1653 struct itimerval itv
;
1655 memset(&itv
, 0, sizeof(itv
));
1656 setitimer(ITIMER_REAL
, &itv
, NULL
);
1659 #endif /* !defined(_WIN32) */
1661 static void try_to_rearm_timer(void *opaque
)
1663 struct qemu_alarm_timer
*t
= opaque
;
1667 /* Drain the notify pipe */
1670 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1671 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1674 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1675 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1676 qemu_rearm_alarm_timer(alarm_timer
);
1682 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1685 struct qemu_alarm_win32
*data
= t
->priv
;
1688 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1689 if (!data
->host_alarm
) {
1690 perror("Failed CreateEvent");
1694 memset(&tc
, 0, sizeof(tc
));
1695 timeGetDevCaps(&tc
, sizeof(tc
));
1697 if (data
->period
< tc
.wPeriodMin
)
1698 data
->period
= tc
.wPeriodMin
;
1700 timeBeginPeriod(data
->period
);
1702 flags
= TIME_CALLBACK_FUNCTION
;
1703 if (alarm_has_dynticks(t
))
1704 flags
|= TIME_ONESHOT
;
1706 flags
|= TIME_PERIODIC
;
1708 data
->timerId
= timeSetEvent(1, // interval (ms)
1709 data
->period
, // resolution
1710 host_alarm_handler
, // function
1711 (DWORD
)t
, // parameter
1714 if (!data
->timerId
) {
1715 perror("Failed to initialize win32 alarm timer");
1717 timeEndPeriod(data
->period
);
1718 CloseHandle(data
->host_alarm
);
1722 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1727 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1729 struct qemu_alarm_win32
*data
= t
->priv
;
1731 timeKillEvent(data
->timerId
);
1732 timeEndPeriod(data
->period
);
1734 CloseHandle(data
->host_alarm
);
1737 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1739 struct qemu_alarm_win32
*data
= t
->priv
;
1740 uint64_t nearest_delta_us
;
1742 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1743 !active_timers
[QEMU_TIMER_VIRTUAL
])
1746 nearest_delta_us
= qemu_next_deadline_dyntick();
1747 nearest_delta_us
/= 1000;
1749 timeKillEvent(data
->timerId
);
1751 data
->timerId
= timeSetEvent(1,
1755 TIME_ONESHOT
| TIME_PERIODIC
);
1757 if (!data
->timerId
) {
1758 perror("Failed to re-arm win32 alarm timer");
1760 timeEndPeriod(data
->period
);
1761 CloseHandle(data
->host_alarm
);
1768 static int init_timer_alarm(void)
1770 struct qemu_alarm_timer
*t
= NULL
;
1780 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1784 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1788 alarm_timer_rfd
= fds
[0];
1789 alarm_timer_wfd
= fds
[1];
1792 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1793 t
= &alarm_timers
[i
];
1806 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1807 try_to_rearm_timer
, NULL
, t
);
1822 static void quit_timers(void)
1824 alarm_timer
->stop(alarm_timer
);
1828 /***********************************************************/
1829 /* host time/date access */
1830 void qemu_get_timedate(struct tm
*tm
, int offset
)
1837 if (rtc_date_offset
== -1) {
1841 ret
= localtime(&ti
);
1843 ti
-= rtc_date_offset
;
1847 memcpy(tm
, ret
, sizeof(struct tm
));
1850 int qemu_timedate_diff(struct tm
*tm
)
1854 if (rtc_date_offset
== -1)
1856 seconds
= mktimegm(tm
);
1858 seconds
= mktime(tm
);
1860 seconds
= mktimegm(tm
) + rtc_date_offset
;
1862 return seconds
- time(NULL
);
1866 static void socket_cleanup(void)
1871 static int socket_init(void)
1876 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1878 err
= WSAGetLastError();
1879 fprintf(stderr
, "WSAStartup: %d\n", err
);
1882 atexit(socket_cleanup
);
1887 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1892 while (*p
!= '\0' && *p
!= '=') {
1893 if (q
&& (q
- buf
) < buf_size
- 1)
1903 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1908 while (*p
!= '\0') {
1910 if (*(p
+ 1) != ',')
1914 if (q
&& (q
- buf
) < buf_size
- 1)
1924 int get_param_value(char *buf
, int buf_size
,
1925 const char *tag
, const char *str
)
1932 p
= get_opt_name(option
, sizeof(option
), p
);
1936 if (!strcmp(tag
, option
)) {
1937 (void)get_opt_value(buf
, buf_size
, p
);
1940 p
= get_opt_value(NULL
, 0, p
);
1949 int check_params(char *buf
, int buf_size
,
1950 const char * const *params
, const char *str
)
1957 p
= get_opt_name(buf
, buf_size
, p
);
1961 for(i
= 0; params
[i
] != NULL
; i
++)
1962 if (!strcmp(params
[i
], buf
))
1964 if (params
[i
] == NULL
)
1966 p
= get_opt_value(NULL
, 0, p
);
1974 /***********************************************************/
1975 /* Bluetooth support */
1978 static struct HCIInfo
*hci_table
[MAX_NICS
];
1980 static struct bt_vlan_s
{
1981 struct bt_scatternet_s net
;
1983 struct bt_vlan_s
*next
;
1986 /* find or alloc a new bluetooth "VLAN" */
1987 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1989 struct bt_vlan_s
**pvlan
, *vlan
;
1990 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1994 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1996 pvlan
= &first_bt_vlan
;
1997 while (*pvlan
!= NULL
)
1998 pvlan
= &(*pvlan
)->next
;
2003 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
2007 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
2012 static struct HCIInfo null_hci
= {
2013 .cmd_send
= null_hci_send
,
2014 .sco_send
= null_hci_send
,
2015 .acl_send
= null_hci_send
,
2016 .bdaddr_set
= null_hci_addr_set
,
2019 struct HCIInfo
*qemu_next_hci(void)
2021 if (cur_hci
== nb_hcis
)
2024 return hci_table
[cur_hci
++];
2027 static struct HCIInfo
*hci_init(const char *str
)
2030 struct bt_scatternet_s
*vlan
= 0;
2032 if (!strcmp(str
, "null"))
2035 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2037 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2038 else if (!strncmp(str
, "hci", 3)) {
2041 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2042 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2047 vlan
= qemu_find_bt_vlan(0);
2049 return bt_new_hci(vlan
);
2052 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2057 static int bt_hci_parse(const char *str
)
2059 struct HCIInfo
*hci
;
2062 if (nb_hcis
>= MAX_NICS
) {
2063 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2067 hci
= hci_init(str
);
2076 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2077 hci
->bdaddr_set(hci
, bdaddr
.b
);
2079 hci_table
[nb_hcis
++] = hci
;
2084 static void bt_vhci_add(int vlan_id
)
2086 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2089 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2090 "an empty scatternet %i\n", vlan_id
);
2092 bt_vhci_init(bt_new_hci(vlan
));
2095 static struct bt_device_s
*bt_device_add(const char *opt
)
2097 struct bt_scatternet_s
*vlan
;
2099 char *endp
= strstr(opt
, ",vlan=");
2100 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2103 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2106 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2108 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2113 vlan
= qemu_find_bt_vlan(vlan_id
);
2116 fprintf(stderr
, "qemu: warning: adding a slave device to "
2117 "an empty scatternet %i\n", vlan_id
);
2119 if (!strcmp(devname
, "keyboard"))
2120 return bt_keyboard_init(vlan
);
2122 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2126 static int bt_parse(const char *opt
)
2128 const char *endp
, *p
;
2131 if (strstart(opt
, "hci", &endp
)) {
2132 if (!*endp
|| *endp
== ',') {
2134 if (!strstart(endp
, ",vlan=", 0))
2137 return bt_hci_parse(opt
);
2139 } else if (strstart(opt
, "vhci", &endp
)) {
2140 if (!*endp
|| *endp
== ',') {
2142 if (strstart(endp
, ",vlan=", &p
)) {
2143 vlan
= strtol(p
, (char **) &endp
, 0);
2145 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2149 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2158 } else if (strstart(opt
, "device:", &endp
))
2159 return !bt_device_add(endp
);
2161 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2165 /***********************************************************/
2166 /* QEMU Block devices */
2168 #define HD_ALIAS "index=%d,media=disk"
2170 #define CDROM_ALIAS "index=1,media=cdrom"
2172 #define CDROM_ALIAS "index=2,media=cdrom"
2174 #define FD_ALIAS "index=%d,if=floppy"
2175 #define PFLASH_ALIAS "if=pflash"
2176 #define MTD_ALIAS "if=mtd"
2177 #define SD_ALIAS "index=0,if=sd"
2179 static int drive_opt_get_free_idx(void)
2183 for (index
= 0; index
< MAX_DRIVES
; index
++)
2184 if (!drives_opt
[index
].used
) {
2185 drives_opt
[index
].used
= 1;
2192 static int drive_get_free_idx(void)
2196 for (index
= 0; index
< MAX_DRIVES
; index
++)
2197 if (!drives_table
[index
].used
) {
2198 drives_table
[index
].used
= 1;
2205 int drive_add(const char *file
, const char *fmt
, ...)
2208 int index
= drive_opt_get_free_idx();
2210 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2211 fprintf(stderr
, "qemu: too many drives\n");
2215 drives_opt
[index
].file
= file
;
2217 vsnprintf(drives_opt
[index
].opt
,
2218 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2225 void drive_remove(int index
)
2227 drives_opt
[index
].used
= 0;
2231 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2235 /* seek interface, bus and unit */
2237 for (index
= 0; index
< MAX_DRIVES
; index
++)
2238 if (drives_table
[index
].type
== type
&&
2239 drives_table
[index
].bus
== bus
&&
2240 drives_table
[index
].unit
== unit
&&
2241 drives_table
[index
].used
)
2247 int drive_get_max_bus(BlockInterfaceType type
)
2253 for (index
= 0; index
< nb_drives
; index
++) {
2254 if(drives_table
[index
].type
== type
&&
2255 drives_table
[index
].bus
> max_bus
)
2256 max_bus
= drives_table
[index
].bus
;
2261 const char *drive_get_serial(BlockDriverState
*bdrv
)
2265 for (index
= 0; index
< nb_drives
; index
++)
2266 if (drives_table
[index
].bdrv
== bdrv
)
2267 return drives_table
[index
].serial
;
2272 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2276 for (index
= 0; index
< nb_drives
; index
++)
2277 if (drives_table
[index
].bdrv
== bdrv
)
2278 return drives_table
[index
].onerror
;
2280 return BLOCK_ERR_STOP_ENOSPC
;
2283 static void bdrv_format_print(void *opaque
, const char *name
)
2285 fprintf(stderr
, " %s", name
);
2288 void drive_uninit(BlockDriverState
*bdrv
)
2292 for (i
= 0; i
< MAX_DRIVES
; i
++)
2293 if (drives_table
[i
].bdrv
== bdrv
) {
2294 drives_table
[i
].bdrv
= NULL
;
2295 drives_table
[i
].used
= 0;
2296 drive_remove(drives_table
[i
].drive_opt_idx
);
2302 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2308 const char *mediastr
= "";
2309 BlockInterfaceType type
;
2310 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2311 int bus_id
, unit_id
;
2312 int cyls
, heads
, secs
, translation
;
2313 BlockDriverState
*bdrv
;
2314 BlockDriver
*drv
= NULL
;
2315 QEMUMachine
*machine
= opaque
;
2319 int bdrv_flags
, onerror
;
2320 int drives_table_idx
;
2321 char *str
= arg
->opt
;
2322 static const char * const params
[] = { "bus", "unit", "if", "index",
2323 "cyls", "heads", "secs", "trans",
2324 "media", "snapshot", "file",
2325 "cache", "format", "serial", "werror",
2328 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2329 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2335 cyls
= heads
= secs
= 0;
2338 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2342 if (machine
->use_scsi
) {
2344 max_devs
= MAX_SCSI_DEVS
;
2345 pstrcpy(devname
, sizeof(devname
), "scsi");
2348 max_devs
= MAX_IDE_DEVS
;
2349 pstrcpy(devname
, sizeof(devname
), "ide");
2353 /* extract parameters */
2355 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2356 bus_id
= strtol(buf
, NULL
, 0);
2358 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2363 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2364 unit_id
= strtol(buf
, NULL
, 0);
2366 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2371 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2372 pstrcpy(devname
, sizeof(devname
), buf
);
2373 if (!strcmp(buf
, "ide")) {
2375 max_devs
= MAX_IDE_DEVS
;
2376 } else if (!strcmp(buf
, "scsi")) {
2378 max_devs
= MAX_SCSI_DEVS
;
2379 } else if (!strcmp(buf
, "floppy")) {
2382 } else if (!strcmp(buf
, "pflash")) {
2385 } else if (!strcmp(buf
, "mtd")) {
2388 } else if (!strcmp(buf
, "sd")) {
2391 } else if (!strcmp(buf
, "virtio")) {
2395 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2400 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2401 index
= strtol(buf
, NULL
, 0);
2403 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2408 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2409 cyls
= strtol(buf
, NULL
, 0);
2412 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2413 heads
= strtol(buf
, NULL
, 0);
2416 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2417 secs
= strtol(buf
, NULL
, 0);
2420 if (cyls
|| heads
|| secs
) {
2421 if (cyls
< 1 || cyls
> 16383) {
2422 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2425 if (heads
< 1 || heads
> 16) {
2426 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2429 if (secs
< 1 || secs
> 63) {
2430 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2435 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2438 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2442 if (!strcmp(buf
, "none"))
2443 translation
= BIOS_ATA_TRANSLATION_NONE
;
2444 else if (!strcmp(buf
, "lba"))
2445 translation
= BIOS_ATA_TRANSLATION_LBA
;
2446 else if (!strcmp(buf
, "auto"))
2447 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2449 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2454 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2455 if (!strcmp(buf
, "disk")) {
2457 } else if (!strcmp(buf
, "cdrom")) {
2458 if (cyls
|| secs
|| heads
) {
2460 "qemu: '%s' invalid physical CHS format\n", str
);
2463 media
= MEDIA_CDROM
;
2465 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2470 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2471 if (!strcmp(buf
, "on"))
2473 else if (!strcmp(buf
, "off"))
2476 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2481 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2482 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2484 else if (!strcmp(buf
, "writethrough"))
2486 else if (!strcmp(buf
, "writeback"))
2489 fprintf(stderr
, "qemu: invalid cache option\n");
2494 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2495 if (strcmp(buf
, "?") == 0) {
2496 fprintf(stderr
, "qemu: Supported formats:");
2497 bdrv_iterate_format(bdrv_format_print
, NULL
);
2498 fprintf(stderr
, "\n");
2501 drv
= bdrv_find_format(buf
);
2503 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2508 if (get_param_value(buf
, sizeof(buf
), "boot", str
)) {
2509 if (!strcmp(buf
, "on")) {
2510 if (extboot_drive
!= -1) {
2511 fprintf(stderr
, "qemu: two bootable drives specified\n");
2514 extboot_drive
= nb_drives
;
2515 } else if (strcmp(buf
, "off")) {
2516 fprintf(stderr
, "qemu: '%s' invalid boot option\n", str
);
2521 if (arg
->file
== NULL
)
2522 get_param_value(file
, sizeof(file
), "file", str
);
2524 pstrcpy(file
, sizeof(file
), arg
->file
);
2526 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2527 memset(serial
, 0, sizeof(serial
));
2529 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2530 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2531 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2532 fprintf(stderr
, "werror is no supported by this format\n");
2535 if (!strcmp(buf
, "ignore"))
2536 onerror
= BLOCK_ERR_IGNORE
;
2537 else if (!strcmp(buf
, "enospc"))
2538 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2539 else if (!strcmp(buf
, "stop"))
2540 onerror
= BLOCK_ERR_STOP_ANY
;
2541 else if (!strcmp(buf
, "report"))
2542 onerror
= BLOCK_ERR_REPORT
;
2544 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2549 /* compute bus and unit according index */
2552 if (bus_id
!= 0 || unit_id
!= -1) {
2554 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2562 unit_id
= index
% max_devs
;
2563 bus_id
= index
/ max_devs
;
2567 /* if user doesn't specify a unit_id,
2568 * try to find the first free
2571 if (unit_id
== -1) {
2573 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2575 if (max_devs
&& unit_id
>= max_devs
) {
2576 unit_id
-= max_devs
;
2584 if (max_devs
&& unit_id
>= max_devs
) {
2585 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2586 str
, unit_id
, max_devs
- 1);
2591 * ignore multiple definitions
2594 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2599 if (type
== IF_IDE
|| type
== IF_SCSI
)
2600 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2602 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2603 devname
, bus_id
, mediastr
, unit_id
);
2605 snprintf(buf
, sizeof(buf
), "%s%s%i",
2606 devname
, mediastr
, unit_id
);
2607 bdrv
= bdrv_new(buf
);
2608 drives_table_idx
= drive_get_free_idx();
2609 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2610 drives_table
[drives_table_idx
].type
= type
;
2611 drives_table
[drives_table_idx
].bus
= bus_id
;
2612 drives_table
[drives_table_idx
].unit
= unit_id
;
2613 drives_table
[drives_table_idx
].onerror
= onerror
;
2614 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2615 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2624 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2625 bdrv_set_translation_hint(bdrv
, translation
);
2629 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2634 /* FIXME: This isn't really a floppy, but it's a reasonable
2637 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2648 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2649 cache
= 2; /* always use write-back with snapshot */
2651 if (cache
== 0) /* no caching */
2652 bdrv_flags
|= BDRV_O_NOCACHE
;
2653 else if (cache
== 2) /* write-back */
2654 bdrv_flags
|= BDRV_O_CACHE_WB
;
2655 else if (cache
== 3) /* not specified */
2656 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2657 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2658 fprintf(stderr
, "qemu: could not open disk image %s\n",
2662 if (bdrv_key_required(bdrv
))
2664 return drives_table_idx
;
2667 /***********************************************************/
2670 static USBPort
*used_usb_ports
;
2671 static USBPort
*free_usb_ports
;
2673 /* ??? Maybe change this to register a hub to keep track of the topology. */
2674 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2675 usb_attachfn attach
)
2677 port
->opaque
= opaque
;
2678 port
->index
= index
;
2679 port
->attach
= attach
;
2680 port
->next
= free_usb_ports
;
2681 free_usb_ports
= port
;
2684 int usb_device_add_dev(USBDevice
*dev
)
2688 /* Find a USB port to add the device to. */
2689 port
= free_usb_ports
;
2693 /* Create a new hub and chain it on. */
2694 free_usb_ports
= NULL
;
2695 port
->next
= used_usb_ports
;
2696 used_usb_ports
= port
;
2698 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2699 usb_attach(port
, hub
);
2700 port
= free_usb_ports
;
2703 free_usb_ports
= port
->next
;
2704 port
->next
= used_usb_ports
;
2705 used_usb_ports
= port
;
2706 usb_attach(port
, dev
);
2710 static int usb_device_add(const char *devname
, int is_hotplug
)
2715 if (!free_usb_ports
)
2718 if (strstart(devname
, "host:", &p
)) {
2719 dev
= usb_host_device_open(p
);
2720 } else if (!strcmp(devname
, "mouse")) {
2721 dev
= usb_mouse_init();
2722 } else if (!strcmp(devname
, "tablet")) {
2723 dev
= usb_tablet_init();
2724 } else if (!strcmp(devname
, "keyboard")) {
2725 dev
= usb_keyboard_init();
2726 } else if (strstart(devname
, "disk:", &p
)) {
2727 BlockDriverState
*bs
;
2729 dev
= usb_msd_init(p
, &bs
);
2732 if (bdrv_key_required(bs
)) {
2734 if (is_hotplug
&& monitor_read_bdrv_key(bs
) < 0) {
2735 dev
->handle_destroy(dev
);
2739 } else if (!strcmp(devname
, "wacom-tablet")) {
2740 dev
= usb_wacom_init();
2741 } else if (strstart(devname
, "serial:", &p
)) {
2742 dev
= usb_serial_init(p
);
2743 #ifdef CONFIG_BRLAPI
2744 } else if (!strcmp(devname
, "braille")) {
2745 dev
= usb_baum_init();
2747 } else if (strstart(devname
, "net:", &p
)) {
2750 if (net_client_init("nic", p
) < 0)
2752 nd_table
[nic
].model
= "usb";
2753 dev
= usb_net_init(&nd_table
[nic
]);
2754 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2755 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2756 bt_new_hci(qemu_find_bt_vlan(0)));
2763 return usb_device_add_dev(dev
);
2766 int usb_device_del_addr(int bus_num
, int addr
)
2772 if (!used_usb_ports
)
2778 lastp
= &used_usb_ports
;
2779 port
= used_usb_ports
;
2780 while (port
&& port
->dev
->addr
!= addr
) {
2781 lastp
= &port
->next
;
2789 *lastp
= port
->next
;
2790 usb_attach(port
, NULL
);
2791 dev
->handle_destroy(dev
);
2792 port
->next
= free_usb_ports
;
2793 free_usb_ports
= port
;
2797 static int usb_device_del(const char *devname
)
2802 if (strstart(devname
, "host:", &p
))
2803 return usb_host_device_close(p
);
2805 if (!used_usb_ports
)
2808 p
= strchr(devname
, '.');
2811 bus_num
= strtoul(devname
, NULL
, 0);
2812 addr
= strtoul(p
+ 1, NULL
, 0);
2814 return usb_device_del_addr(bus_num
, addr
);
2817 void do_usb_add(const char *devname
)
2819 usb_device_add(devname
, 1);
2822 void do_usb_del(const char *devname
)
2824 usb_device_del(devname
);
2831 const char *speed_str
;
2834 term_printf("USB support not enabled\n");
2838 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2842 switch(dev
->speed
) {
2846 case USB_SPEED_FULL
:
2849 case USB_SPEED_HIGH
:
2856 term_printf(" Device %d.%d, Speed %s Mb/s, Product %s\n",
2857 0, dev
->addr
, speed_str
, dev
->devname
);
2861 /***********************************************************/
2862 /* PCMCIA/Cardbus */
2864 static struct pcmcia_socket_entry_s
{
2865 struct pcmcia_socket_s
*socket
;
2866 struct pcmcia_socket_entry_s
*next
;
2867 } *pcmcia_sockets
= 0;
2869 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2871 struct pcmcia_socket_entry_s
*entry
;
2873 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2874 entry
->socket
= socket
;
2875 entry
->next
= pcmcia_sockets
;
2876 pcmcia_sockets
= entry
;
2879 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2881 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2883 ptr
= &pcmcia_sockets
;
2884 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2885 if (entry
->socket
== socket
) {
2891 void pcmcia_info(void)
2893 struct pcmcia_socket_entry_s
*iter
;
2894 if (!pcmcia_sockets
)
2895 term_printf("No PCMCIA sockets\n");
2897 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2898 term_printf("%s: %s\n", iter
->socket
->slot_string
,
2899 iter
->socket
->attached
? iter
->socket
->card_string
:
2903 /***********************************************************/
2904 /* register display */
2906 void register_displaystate(DisplayState
*ds
)
2916 DisplayState
*get_displaystate(void)
2918 return display_state
;
2923 static void dumb_display_init(void)
2925 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2926 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2927 register_displaystate(ds
);
2930 /***********************************************************/
2933 #define MAX_IO_HANDLERS 64
2935 typedef struct IOHandlerRecord
{
2937 IOCanRWHandler
*fd_read_poll
;
2939 IOHandler
*fd_write
;
2942 /* temporary data */
2944 struct IOHandlerRecord
*next
;
2947 static IOHandlerRecord
*first_io_handler
;
2949 /* XXX: fd_read_poll should be suppressed, but an API change is
2950 necessary in the character devices to suppress fd_can_read(). */
2951 int qemu_set_fd_handler2(int fd
,
2952 IOCanRWHandler
*fd_read_poll
,
2954 IOHandler
*fd_write
,
2957 IOHandlerRecord
**pioh
, *ioh
;
2959 if (!fd_read
&& !fd_write
) {
2960 pioh
= &first_io_handler
;
2965 if (ioh
->fd
== fd
) {
2972 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2976 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2977 ioh
->next
= first_io_handler
;
2978 first_io_handler
= ioh
;
2981 ioh
->fd_read_poll
= fd_read_poll
;
2982 ioh
->fd_read
= fd_read
;
2983 ioh
->fd_write
= fd_write
;
2984 ioh
->opaque
= opaque
;
2991 int qemu_set_fd_handler(int fd
,
2993 IOHandler
*fd_write
,
2996 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3000 /***********************************************************/
3001 /* Polling handling */
3003 typedef struct PollingEntry
{
3006 struct PollingEntry
*next
;
3009 static PollingEntry
*first_polling_entry
;
3011 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3013 PollingEntry
**ppe
, *pe
;
3014 pe
= qemu_mallocz(sizeof(PollingEntry
));
3016 pe
->opaque
= opaque
;
3017 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3022 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3024 PollingEntry
**ppe
, *pe
;
3025 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3027 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3035 /***********************************************************/
3036 /* Wait objects support */
3037 typedef struct WaitObjects
{
3039 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3040 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3041 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3044 static WaitObjects wait_objects
= {0};
3046 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3048 WaitObjects
*w
= &wait_objects
;
3050 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3052 w
->events
[w
->num
] = handle
;
3053 w
->func
[w
->num
] = func
;
3054 w
->opaque
[w
->num
] = opaque
;
3059 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3062 WaitObjects
*w
= &wait_objects
;
3065 for (i
= 0; i
< w
->num
; i
++) {
3066 if (w
->events
[i
] == handle
)
3069 w
->events
[i
] = w
->events
[i
+ 1];
3070 w
->func
[i
] = w
->func
[i
+ 1];
3071 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3079 /***********************************************************/
3080 /* ram save/restore */
3082 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3086 v
= qemu_get_byte(f
);
3089 if (qemu_get_buffer(f
, buf
, len
) != len
)
3093 v
= qemu_get_byte(f
);
3094 memset(buf
, v
, len
);
3100 if (qemu_file_has_error(f
))
3106 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3111 if (qemu_get_be32(f
) != phys_ram_size
)
3113 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3114 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3116 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3123 #define BDRV_HASH_BLOCK_SIZE 1024
3124 #define IOBUF_SIZE 4096
3125 #define RAM_CBLOCK_MAGIC 0xfabe
3127 typedef struct RamDecompressState
{
3130 uint8_t buf
[IOBUF_SIZE
];
3131 } RamDecompressState
;
3133 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3136 memset(s
, 0, sizeof(*s
));
3138 ret
= inflateInit(&s
->zstream
);
3144 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3148 s
->zstream
.avail_out
= len
;
3149 s
->zstream
.next_out
= buf
;
3150 while (s
->zstream
.avail_out
> 0) {
3151 if (s
->zstream
.avail_in
== 0) {
3152 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3154 clen
= qemu_get_be16(s
->f
);
3155 if (clen
> IOBUF_SIZE
)
3157 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3158 s
->zstream
.avail_in
= clen
;
3159 s
->zstream
.next_in
= s
->buf
;
3161 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3162 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3169 static void ram_decompress_close(RamDecompressState
*s
)
3171 inflateEnd(&s
->zstream
);
3174 #define RAM_SAVE_FLAG_FULL 0x01
3175 #define RAM_SAVE_FLAG_COMPRESS 0x02
3176 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3177 #define RAM_SAVE_FLAG_PAGE 0x08
3178 #define RAM_SAVE_FLAG_EOS 0x10
3180 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3182 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3183 uint32_t *array
= (uint32_t *)page
;
3186 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3187 if (array
[i
] != val
)
3194 static int ram_save_block(QEMUFile
*f
)
3196 static ram_addr_t current_addr
= 0;
3197 ram_addr_t saved_addr
= current_addr
;
3198 ram_addr_t addr
= 0;
3201 while (addr
< phys_ram_size
) {
3202 if (kvm_enabled() && current_addr
== 0)
3203 kvm_update_dirty_pages_log(); /* FIXME: propagate errors */
3204 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3207 cpu_physical_memory_reset_dirty(current_addr
,
3208 current_addr
+ TARGET_PAGE_SIZE
,
3209 MIGRATION_DIRTY_FLAG
);
3211 ch
= *(phys_ram_base
+ current_addr
);
3213 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3214 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3215 qemu_put_byte(f
, ch
);
3217 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3218 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3224 addr
+= TARGET_PAGE_SIZE
;
3225 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3231 static ram_addr_t ram_save_threshold
= 10;
3233 static ram_addr_t
ram_save_remaining(void)
3236 ram_addr_t count
= 0;
3238 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3239 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3246 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3251 /* Make sure all dirty bits are set */
3252 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3253 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3254 cpu_physical_memory_set_dirty(addr
);
3257 /* Enable dirty memory tracking */
3258 cpu_physical_memory_set_dirty_tracking(1);
3260 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3263 while (!qemu_file_rate_limit(f
)) {
3266 ret
= ram_save_block(f
);
3267 if (ret
== 0) /* no more blocks */
3271 /* try transferring iterative blocks of memory */
3275 /* flush all remaining blocks regardless of rate limiting */
3276 while (ram_save_block(f
) != 0);
3277 cpu_physical_memory_set_dirty_tracking(0);
3280 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3282 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3285 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3287 RamDecompressState s1
, *s
= &s1
;
3291 if (ram_decompress_open(s
, f
) < 0)
3293 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3294 if (kvm_enabled() && (i
>=0xa0000) && (i
<0xc0000)) /* do not access video-addresses */
3296 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3297 fprintf(stderr
, "Error while reading ram block header\n");
3301 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3302 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3307 printf("Error block header\n");
3311 ram_decompress_close(s
);
3316 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3321 if (version_id
== 1)
3322 return ram_load_v1(f
, opaque
);
3324 if (version_id
== 2) {
3325 if (qemu_get_be32(f
) != phys_ram_size
)
3327 return ram_load_dead(f
, opaque
);
3330 if (version_id
!= 3)
3334 addr
= qemu_get_be64(f
);
3336 flags
= addr
& ~TARGET_PAGE_MASK
;
3337 addr
&= TARGET_PAGE_MASK
;
3339 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3340 if (addr
!= phys_ram_size
)
3344 if (flags
& RAM_SAVE_FLAG_FULL
) {
3345 if (ram_load_dead(f
, opaque
) < 0)
3349 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3350 uint8_t ch
= qemu_get_byte(f
);
3351 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3352 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3353 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3354 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3359 /***********************************************************/
3360 /* bottom halves (can be seen as timers which expire ASAP) */
3371 static QEMUBH
*first_bh
= NULL
;
3373 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3376 bh
= qemu_mallocz(sizeof(QEMUBH
));
3378 bh
->opaque
= opaque
;
3379 bh
->next
= first_bh
;
3384 int qemu_bh_poll(void)
3390 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3391 if (!bh
->deleted
&& bh
->scheduled
) {
3400 /* remove deleted bhs */
3414 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3422 void qemu_bh_schedule(QEMUBH
*bh
)
3424 CPUState
*env
= cpu_single_env
;
3429 /* stop the currently executing CPU to execute the BH ASAP */
3431 cpu_interrupt(env
, CPU_INTERRUPT_EXIT
);
3436 void qemu_bh_cancel(QEMUBH
*bh
)
3441 void qemu_bh_delete(QEMUBH
*bh
)
3447 static void qemu_bh_update_timeout(int *timeout
)
3451 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3452 if (!bh
->deleted
&& bh
->scheduled
) {
3454 /* idle bottom halves will be polled at least
3456 *timeout
= MIN(10, *timeout
);
3458 /* non-idle bottom halves will be executed
3467 /***********************************************************/
3468 /* machine registration */
3470 static QEMUMachine
*first_machine
= NULL
;
3471 QEMUMachine
*current_machine
= NULL
;
3473 int qemu_register_machine(QEMUMachine
*m
)
3476 pm
= &first_machine
;
3484 static QEMUMachine
*find_machine(const char *name
)
3488 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3489 if (!strcmp(m
->name
, name
))
3495 /***********************************************************/
3496 /* main execution loop */
3498 static void gui_update(void *opaque
)
3500 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3501 DisplayState
*ds
= opaque
;
3502 DisplayChangeListener
*dcl
= ds
->listeners
;
3506 while (dcl
!= NULL
) {
3507 if (dcl
->gui_timer_interval
&&
3508 dcl
->gui_timer_interval
< interval
)
3509 interval
= dcl
->gui_timer_interval
;
3512 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3515 static void nographic_update(void *opaque
)
3517 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3519 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3522 struct vm_change_state_entry
{
3523 VMChangeStateHandler
*cb
;
3525 LIST_ENTRY (vm_change_state_entry
) entries
;
3528 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3530 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3533 VMChangeStateEntry
*e
;
3535 e
= qemu_mallocz(sizeof (*e
));
3539 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3543 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3545 LIST_REMOVE (e
, entries
);
3549 static void vm_state_notify(int running
, int reason
)
3551 VMChangeStateEntry
*e
;
3553 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3554 e
->cb(e
->opaque
, running
, reason
);
3563 vm_state_notify(1, 0);
3564 qemu_rearm_alarm_timer(alarm_timer
);
3568 void vm_stop(int reason
)
3571 cpu_disable_ticks();
3573 vm_state_notify(0, reason
);
3577 /* reset/shutdown handler */
3579 typedef struct QEMUResetEntry
{
3580 QEMUResetHandler
*func
;
3582 struct QEMUResetEntry
*next
;
3585 static QEMUResetEntry
*first_reset_entry
;
3586 static int reset_requested
;
3587 static int shutdown_requested
;
3588 static int powerdown_requested
;
3590 int qemu_no_shutdown(void)
3592 int r
= no_shutdown
;
3597 int qemu_shutdown_requested(void)
3599 int r
= shutdown_requested
;
3600 shutdown_requested
= 0;
3604 int qemu_reset_requested(void)
3606 int r
= reset_requested
;
3607 reset_requested
= 0;
3611 int qemu_powerdown_requested(void)
3613 int r
= powerdown_requested
;
3614 powerdown_requested
= 0;
3618 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3620 QEMUResetEntry
**pre
, *re
;
3622 pre
= &first_reset_entry
;
3623 while (*pre
!= NULL
)
3624 pre
= &(*pre
)->next
;
3625 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3627 re
->opaque
= opaque
;
3632 void qemu_system_reset(void)
3636 /* reset all devices */
3637 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3638 re
->func(re
->opaque
);
3642 void qemu_system_reset_request(void)
3645 shutdown_requested
= 1;
3647 reset_requested
= 1;
3650 if (cpu_single_env
) {
3651 qemu_kvm_cpu_stop(cpu_single_env
);
3652 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3657 void qemu_system_shutdown_request(void)
3659 shutdown_requested
= 1;
3661 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3664 void qemu_system_powerdown_request(void)
3666 powerdown_requested
= 1;
3668 cpu_interrupt(cpu_single_env
, CPU_INTERRUPT_EXIT
);
3671 static int qemu_select(int max_fd
, fd_set
*rfds
, fd_set
*wfds
, fd_set
*xfds
,
3676 /* KVM holds a mutex while QEMU code is running, we need hooks to
3677 release the mutex whenever QEMU code sleeps. */
3681 ret
= select(max_fd
, rfds
, wfds
, xfds
, tv
);
3689 static void host_main_loop_wait(int *timeout
)
3695 /* XXX: need to suppress polling by better using win32 events */
3697 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3698 ret
|= pe
->func(pe
->opaque
);
3702 WaitObjects
*w
= &wait_objects
;
3704 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3705 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3706 if (w
->func
[ret
- WAIT_OBJECT_0
])
3707 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3709 /* Check for additional signaled events */
3710 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3712 /* Check if event is signaled */
3713 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3714 if(ret2
== WAIT_OBJECT_0
) {
3716 w
->func
[i
](w
->opaque
[i
]);
3717 } else if (ret2
== WAIT_TIMEOUT
) {
3719 err
= GetLastError();
3720 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3723 } else if (ret
== WAIT_TIMEOUT
) {
3725 err
= GetLastError();
3726 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3733 static void host_main_loop_wait(int *timeout
)
3738 void main_loop_wait(int timeout
)
3740 IOHandlerRecord
*ioh
;
3741 fd_set rfds
, wfds
, xfds
;
3745 qemu_bh_update_timeout(&timeout
);
3747 host_main_loop_wait(&timeout
);
3749 /* poll any events */
3750 /* XXX: separate device handlers from system ones */
3755 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3759 (!ioh
->fd_read_poll
||
3760 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3761 FD_SET(ioh
->fd
, &rfds
);
3765 if (ioh
->fd_write
) {
3766 FD_SET(ioh
->fd
, &wfds
);
3772 tv
.tv_sec
= timeout
/ 1000;
3773 tv
.tv_usec
= (timeout
% 1000) * 1000;
3775 #if defined(CONFIG_SLIRP)
3776 if (slirp_is_inited()) {
3777 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3780 ret
= qemu_select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3782 IOHandlerRecord
**pioh
;
3784 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3785 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3786 ioh
->fd_read(ioh
->opaque
);
3787 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3788 FD_CLR(ioh
->fd
, &rfds
);
3790 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3791 ioh
->fd_write(ioh
->opaque
);
3795 /* remove deleted IO handlers */
3796 pioh
= &first_io_handler
;
3806 #if defined(CONFIG_SLIRP)
3807 if (slirp_is_inited()) {
3813 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3817 /* vm time timers */
3818 if (vm_running
&& (!cur_cpu
3819 || likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
))))
3820 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3821 qemu_get_clock(vm_clock
));
3823 /* real time timers */
3824 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3825 qemu_get_clock(rt_clock
));
3827 /* Check bottom-halves last in case any of the earlier events triggered
3833 static int main_loop(void)
3836 #ifdef CONFIG_PROFILER
3842 if (kvm_enabled()) {
3844 cpu_disable_ticks();
3848 cur_cpu
= first_cpu
;
3849 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3856 #ifdef CONFIG_PROFILER
3857 ti
= profile_getclock();
3862 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3863 env
->icount_decr
.u16
.low
= 0;
3864 env
->icount_extra
= 0;
3865 count
= qemu_next_deadline();
3866 count
= (count
+ (1 << icount_time_shift
) - 1)
3867 >> icount_time_shift
;
3868 qemu_icount
+= count
;
3869 decr
= (count
> 0xffff) ? 0xffff : count
;
3871 env
->icount_decr
.u16
.low
= decr
;
3872 env
->icount_extra
= count
;
3874 ret
= cpu_exec(env
);
3875 #ifdef CONFIG_PROFILER
3876 qemu_time
+= profile_getclock() - ti
;
3879 /* Fold pending instructions back into the
3880 instruction counter, and clear the interrupt flag. */
3881 qemu_icount
-= (env
->icount_decr
.u16
.low
3882 + env
->icount_extra
);
3883 env
->icount_decr
.u32
= 0;
3884 env
->icount_extra
= 0;
3886 next_cpu
= env
->next_cpu
?: first_cpu
;
3887 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3888 ret
= EXCP_INTERRUPT
;
3892 if (ret
== EXCP_HLT
) {
3893 /* Give the next CPU a chance to run. */
3897 if (ret
!= EXCP_HALTED
)
3899 /* all CPUs are halted ? */
3905 if (shutdown_requested
) {
3906 ret
= EXCP_INTERRUPT
;
3914 if (reset_requested
) {
3915 reset_requested
= 0;
3916 qemu_system_reset();
3918 kvm_load_registers(env
);
3919 ret
= EXCP_INTERRUPT
;
3921 if (powerdown_requested
) {
3922 powerdown_requested
= 0;
3923 qemu_system_powerdown();
3924 ret
= EXCP_INTERRUPT
;
3926 #ifdef CONFIG_GDBSTUB
3927 if (unlikely(ret
== EXCP_DEBUG
)) {
3928 gdb_set_stop_cpu(cur_cpu
);
3929 vm_stop(EXCP_DEBUG
);
3932 /* If all cpus are halted then wait until the next IRQ */
3933 /* XXX: use timeout computed from timers */
3934 if (ret
== EXCP_HALTED
) {
3938 /* Advance virtual time to the next event. */
3939 if (use_icount
== 1) {
3940 /* When not using an adaptive execution frequency
3941 we tend to get badly out of sync with real time,
3942 so just delay for a reasonable amount of time. */
3945 delta
= cpu_get_icount() - cpu_get_clock();
3948 /* If virtual time is ahead of real time then just
3950 timeout
= (delta
/ 1000000) + 1;
3952 /* Wait for either IO to occur or the next
3954 add
= qemu_next_deadline();
3955 /* We advance the timer before checking for IO.
3956 Limit the amount we advance so that early IO
3957 activity won't get the guest too far ahead. */
3961 add
= (add
+ (1 << icount_time_shift
) - 1)
3962 >> icount_time_shift
;
3964 timeout
= delta
/ 1000000;
3975 if (shutdown_requested
) {
3976 ret
= EXCP_INTERRUPT
;
3981 #ifdef CONFIG_PROFILER
3982 ti
= profile_getclock();
3984 main_loop_wait(timeout
);
3985 #ifdef CONFIG_PROFILER
3986 dev_time
+= profile_getclock() - ti
;
3989 cpu_disable_ticks();
3993 static void help(int exitcode
)
3995 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3996 and qemu-doc.texi */
3997 printf("QEMU PC emulator version " QEMU_VERSION
" (" KVM_VERSION
")"
3998 ", Copyright (c) 2003-2008 Fabrice Bellard\n"
3999 "usage: %s [options] [disk_image]\n"
4001 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4003 "Standard options:\n"
4004 "-h or -help display this help and exit\n"
4005 "-M machine select emulated machine (-M ? for list)\n"
4006 "-cpu cpu select CPU (-cpu ? for list)\n"
4007 "-smp n set the number of CPUs to 'n' [default=1]\n"
4008 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
4009 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
4010 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
4011 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
4012 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
4013 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
4014 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
4016 " use 'file' as a drive image\n"
4017 "-mtdblock file use 'file' as on-board Flash memory image\n"
4018 "-sd file use 'file' as SecureDigital card image\n"
4019 "-pflash file use 'file' as a parallel flash image\n"
4020 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
4021 "-snapshot write to temporary files instead of disk image files\n"
4022 "-m megs set virtual RAM size to megs MB [default=%d]\n"
4024 "-k language use keyboard layout (for example \"fr\" for French)\n"
4027 "-audio-help print list of audio drivers and their options\n"
4028 "-soundhw c1,... enable audio support\n"
4029 " and only specified sound cards (comma separated list)\n"
4030 " use -soundhw ? to get the list of supported cards\n"
4031 " use -soundhw all to enable all of them\n"
4033 "-usb enable the USB driver (will be the default soon)\n"
4034 "-usbdevice name add the host or guest USB device 'name'\n"
4035 "-name string set the name of the guest\n"
4036 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
4037 " specify machine UUID\n"
4039 "Display options:\n"
4040 "-nographic disable graphical output and redirect serial I/Os to console\n"
4041 #ifdef CONFIG_CURSES
4042 "-curses use a curses/ncurses interface instead of SDL\n"
4045 "-no-frame open SDL window without a frame and window decorations\n"
4046 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
4047 "-no-quit disable SDL window close capability\n"
4050 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
4051 "-vga [std|cirrus|vmware|none]\n"
4052 " select video card type\n"
4053 "-full-screen start in full screen\n"
4054 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4055 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4057 "-vnc display start a VNC server on display\n"
4059 "-nvram file use 'file' to save or load nvram image\n"
4061 "-name string set the name of the guest\n"
4062 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x specify machine UUID\n"
4064 "Network options:\n"
4065 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4066 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4068 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4069 " connect the user mode network stack to VLAN 'n' and send\n"
4070 " hostname 'host' to DHCP clients\n"
4073 "-net tap[,vlan=n][,name=str],ifname=name\n"
4074 " connect the host TAP network interface to VLAN 'n'\n"
4076 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4077 " connect the host TAP network interface to VLAN 'n' and use the\n"
4078 " network scripts 'file' (default=%s)\n"
4079 " and 'dfile' (default=%s);\n"
4080 " use '[down]script=no' to disable script execution;\n"
4081 " use 'fd=h' to connect to an already opened TAP interface\n"
4083 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4084 " connect the vlan 'n' to another VLAN using a socket connection\n"
4085 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4086 " connect the vlan 'n' to multicast maddr and port\n"
4088 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4089 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4090 " on host and listening for incoming connections on 'socketpath'.\n"
4091 " Use group 'groupname' and mode 'octalmode' to change default\n"
4092 " ownership and permissions for communication port.\n"
4094 "-net none use it alone to have zero network devices; if no -net option\n"
4095 " is provided, the default is '-net nic -net user'\n"
4097 "-tftp dir allow tftp access to files in dir [-net user]\n"
4098 "-bootp file advertise file in BOOTP replies\n"
4100 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4102 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4103 " redirect TCP or UDP connections from host to guest [-net user]\n"
4106 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4107 "-bt hci,host[:id]\n"
4108 " use host's HCI with the given name\n"
4109 "-bt hci[,vlan=n]\n"
4110 " emulate a standard HCI in virtual scatternet 'n'\n"
4111 "-bt vhci[,vlan=n]\n"
4112 " add host computer to virtual scatternet 'n' using VHCI\n"
4113 "-bt device:dev[,vlan=n]\n"
4114 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4118 "i386 target only:\n"
4119 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4120 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4121 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4122 "-no-acpi disable ACPI\n"
4123 "-no-hpet disable HPET\n"
4124 "-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"
4125 " ACPI table description\n"
4127 "Linux boot specific:\n"
4128 "-kernel bzImage use 'bzImage' as kernel image\n"
4129 "-append cmdline use 'cmdline' as kernel command line\n"
4130 "-initrd file use 'file' as initial ram disk\n"
4132 "Debug/Expert options:\n"
4133 "-serial dev redirect the serial port to char device 'dev'\n"
4134 "-parallel dev redirect the parallel port to char device 'dev'\n"
4135 "-monitor dev redirect the monitor to char device 'dev'\n"
4136 "-pidfile file write PID to 'file'\n"
4137 "-S freeze CPU at startup (use 'c' to start execution)\n"
4138 "-s wait gdb connection to port\n"
4139 "-p port set gdb connection port [default=%s]\n"
4140 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4141 "-hdachs c,h,s[,t]\n"
4142 " force hard disk 0 physical geometry and the optional BIOS\n"
4143 " translation (t=none or lba) (usually qemu can guess them)\n"
4144 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4145 "-bios file set the filename for the BIOS\n"
4147 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4148 "-no-kqemu disable KQEMU kernel module usage\n"
4151 "-enable-kvm enable KVM full virtualization support\n"
4154 #ifndef NO_CPU_EMULATION
4155 "-no-kvm disable KVM hardware virtualization\n"
4157 "-no-kvm-irqchip disable KVM kernel mode PIC/IOAPIC/LAPIC\n"
4158 "-no-kvm-pit disable KVM kernel mode PIT\n"
4159 "-no-kvm-pit-reinjection disable KVM kernel mode PIT interrupt reinjection\n"
4160 "-enable-nesting enable support for running a VM inside the VM (AMD only)\n"
4161 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4162 "-pcidevice host=bus:dev.func[,dma=none][,name=string]\n"
4163 " expose a PCI device to the guest OS.\n"
4164 " dma=none: don't perform any dma translations (default is to use an iommu)\n"
4165 " 'string' is used in log output.\n"
4168 "-no-reboot exit instead of rebooting\n"
4169 "-no-shutdown stop before shutdown\n"
4170 "-loadvm [tag|id]\n"
4171 " start right away with a saved state (loadvm in monitor)\n"
4173 "-daemonize daemonize QEMU after initializing\n"
4175 "-tdf inject timer interrupts that got lost\n"
4176 "-kvm-shadow-memory megs set the amount of shadow pages to be allocated\n"
4177 "-mem-path set the path to hugetlbfs/tmpfs mounted directory, also\n"
4178 " enables allocation of guest memory with huge pages\n"
4180 "-mem-prealloc toggles preallocation of -mem-path backed physical memory\n"
4181 " at startup. Default is enabled.\n"
4183 "-option-rom rom load a file, rom, into the option ROM space\n"
4184 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4185 "-prom-env variable=value\n"
4186 " set OpenBIOS nvram variables\n"
4188 "-clock force the use of the given methods for timer alarm.\n"
4189 " To see what timers are available use -clock ?\n"
4190 "-localtime set the real time clock to local time [default=utc]\n"
4191 "-startdate select initial date of the clock\n"
4192 "-icount [N|auto]\n"
4193 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4194 "-echr chr set terminal escape character instead of ctrl-a\n"
4195 "-virtioconsole c\n"
4196 " set virtio console\n"
4197 "-show-cursor show cursor\n"
4198 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4199 "-semihosting semihosting mode\n"
4201 #if defined(TARGET_ARM)
4202 "-old-param old param mode\n"
4204 "-tb-size n set TB size\n"
4205 "-incoming p prepare for incoming migration, listen on port p\n"
4207 "-chroot dir Chroot to dir just before starting the VM.\n"
4208 "-runas user Change to user id user just before starting the VM.\n"
4211 "During emulation, the following keys are useful:\n"
4212 "ctrl-alt-f toggle full screen\n"
4213 "ctrl-alt-n switch to virtual console 'n'\n"
4214 "ctrl-alt toggle mouse and keyboard grab\n"
4216 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4221 DEFAULT_NETWORK_SCRIPT
,
4222 DEFAULT_NETWORK_DOWN_SCRIPT
,
4224 DEFAULT_GDBSTUB_PORT
,
4229 #define HAS_ARG 0x0001
4232 /* Please keep in synch with help, qemu_options[] and
4234 /* Standard options: */
4247 QEMU_OPTION_mtdblock
,
4251 QEMU_OPTION_snapshot
,
4254 QEMU_OPTION_audio_help
,
4255 QEMU_OPTION_soundhw
,
4257 QEMU_OPTION_usbdevice
,
4261 /* Display options: */
4262 QEMU_OPTION_nographic
,
4264 QEMU_OPTION_no_frame
,
4265 QEMU_OPTION_alt_grab
,
4266 QEMU_OPTION_no_quit
,
4268 QEMU_OPTION_portrait
,
4270 QEMU_OPTION_full_screen
,
4274 /* Network options: */
4282 /* i386 target only: */
4283 QEMU_OPTION_win2k_hack
,
4284 QEMU_OPTION_rtc_td_hack
,
4285 QEMU_OPTION_no_fd_bootchk
,
4286 QEMU_OPTION_no_acpi
,
4287 QEMU_OPTION_no_hpet
,
4288 QEMU_OPTION_acpitable
,
4290 /* Linux boot specific: */
4295 /* Debug/Expert options: */
4297 QEMU_OPTION_parallel
,
4298 QEMU_OPTION_monitor
,
4299 QEMU_OPTION_pidfile
,
4307 QEMU_OPTION_kernel_kqemu
,
4308 QEMU_OPTION_no_kqemu
,
4309 QEMU_OPTION_enable_kvm
,
4310 QEMU_OPTION_enable_nesting
,
4312 QEMU_OPTION_no_kvm_irqchip
,
4313 QEMU_OPTION_no_kvm_pit
,
4314 QEMU_OPTION_no_kvm_pit_reinjection
,
4315 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4316 QEMU_OPTION_pcidevice
,
4318 QEMU_OPTION_no_reboot
,
4319 QEMU_OPTION_no_shutdown
,
4321 QEMU_OPTION_daemonize
,
4322 QEMU_OPTION_option_rom
,
4323 QEMU_OPTION_cpu_vendor
,
4325 QEMU_OPTION_prom_env
,
4327 QEMU_OPTION_localtime
,
4328 QEMU_OPTION_startdate
,
4331 QEMU_OPTION_virtiocon
,
4332 QEMU_OPTION_show_cursor
,
4333 QEMU_OPTION_semihosting
,
4334 QEMU_OPTION_old_param
,
4335 QEMU_OPTION_tb_size
,
4336 QEMU_OPTION_incoming
,
4340 QEMU_OPTION_kvm_shadow_memory
,
4341 QEMU_OPTION_mempath
,
4343 QEMU_OPTION_mem_prealloc
,
4347 typedef struct QEMUOption
{
4353 static const QEMUOption qemu_options
[] = {
4354 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4356 /* Standard options: */
4357 { "h", 0, QEMU_OPTION_h
},
4358 { "help", 0, QEMU_OPTION_h
},
4359 { "M", HAS_ARG
, QEMU_OPTION_M
},
4360 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4361 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4362 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4363 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4364 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4365 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4366 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4367 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4368 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4369 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4370 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4371 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4372 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4373 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4374 { "snapshot", 0, QEMU_OPTION_snapshot
},
4375 { "m", HAS_ARG
, QEMU_OPTION_m
},
4376 { "k", HAS_ARG
, QEMU_OPTION_k
},
4378 { "audio-help", 0, QEMU_OPTION_audio_help
},
4379 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4381 { "usb", 0, QEMU_OPTION_usb
},
4382 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4383 { "name", HAS_ARG
, QEMU_OPTION_name
},
4384 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4386 /* Display options: */
4387 { "nographic", 0, QEMU_OPTION_nographic
},
4388 #ifdef CONFIG_CURSES
4389 { "curses", 0, QEMU_OPTION_curses
},
4392 { "no-frame", 0, QEMU_OPTION_no_frame
},
4393 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4394 { "no-quit", 0, QEMU_OPTION_no_quit
},
4395 { "sdl", 0, QEMU_OPTION_sdl
},
4397 { "portrait", 0, QEMU_OPTION_portrait
},
4398 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4399 { "full-screen", 0, QEMU_OPTION_full_screen
},
4400 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4401 { "g", 1, QEMU_OPTION_g
},
4403 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4405 /* Network options: */
4406 { "net", HAS_ARG
, QEMU_OPTION_net
},
4408 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4409 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4411 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4413 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4415 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4417 /* i386 target only: */
4418 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4419 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4420 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4421 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4422 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4423 { "acpitable", HAS_ARG
, QEMU_OPTION_acpitable
},
4426 /* Linux boot specific: */
4427 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4428 { "append", HAS_ARG
, QEMU_OPTION_append
},
4429 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4431 /* Debug/Expert options: */
4432 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4433 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4434 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4435 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4436 { "S", 0, QEMU_OPTION_S
},
4437 { "s", 0, QEMU_OPTION_s
},
4438 { "p", HAS_ARG
, QEMU_OPTION_p
},
4439 { "d", HAS_ARG
, QEMU_OPTION_d
},
4440 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4441 { "L", HAS_ARG
, QEMU_OPTION_L
},
4442 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4444 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4445 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4448 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4451 #ifndef NO_CPU_EMULATION
4452 { "no-kvm", 0, QEMU_OPTION_no_kvm
},
4454 { "no-kvm-irqchip", 0, QEMU_OPTION_no_kvm_irqchip
},
4455 { "no-kvm-pit", 0, QEMU_OPTION_no_kvm_pit
},
4456 { "no-kvm-pit-reinjection", 0, QEMU_OPTION_no_kvm_pit_reinjection
},
4457 { "enable-nesting", 0, QEMU_OPTION_enable_nesting
},
4458 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
4459 { "pcidevice", HAS_ARG
, QEMU_OPTION_pcidevice
},
4462 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4463 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4464 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4465 { "daemonize", 0, QEMU_OPTION_daemonize
},
4466 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4467 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4468 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4470 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4471 { "localtime", 0, QEMU_OPTION_localtime
},
4472 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4473 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4474 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4475 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4476 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4477 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4478 { "semihosting", 0, QEMU_OPTION_semihosting
},
4480 { "tdf", 0, QEMU_OPTION_tdf
}, /* enable time drift fix */
4481 { "kvm-shadow-memory", HAS_ARG
, QEMU_OPTION_kvm_shadow_memory
},
4482 { "nvram", HAS_ARG
, QEMU_OPTION_nvram
},
4483 { "cpu-vendor", HAS_ARG
, QEMU_OPTION_cpu_vendor
},
4484 #if defined(TARGET_ARM)
4485 { "old-param", 0, QEMU_OPTION_old_param
},
4487 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4488 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4489 { "chroot", HAS_ARG
, QEMU_OPTION_chroot
},
4490 { "runas", HAS_ARG
, QEMU_OPTION_runas
},
4491 { "mem-path", HAS_ARG
, QEMU_OPTION_mempath
},
4493 { "mem-prealloc", 0, QEMU_OPTION_mem_prealloc
},
4499 struct soundhw soundhw
[] = {
4500 #ifdef HAS_AUDIO_CHOICE
4501 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4507 { .init_isa
= pcspk_audio_init
}
4514 "Creative Sound Blaster 16",
4517 { .init_isa
= SB16_init
}
4521 #ifdef CONFIG_CS4231A
4527 { .init_isa
= cs4231a_init
}
4535 "Yamaha YMF262 (OPL3)",
4537 "Yamaha YM3812 (OPL2)",
4541 { .init_isa
= Adlib_init
}
4548 "Gravis Ultrasound GF1",
4551 { .init_isa
= GUS_init
}
4558 "Intel 82801AA AC97 Audio",
4561 { .init_pci
= ac97_init
}
4565 #ifdef CONFIG_ES1370
4568 "ENSONIQ AudioPCI ES1370",
4571 { .init_pci
= es1370_init
}
4575 #endif /* HAS_AUDIO_CHOICE */
4577 { NULL
, NULL
, 0, 0, { NULL
} }
4580 static void select_soundhw (const char *optarg
)
4584 if (*optarg
== '?') {
4587 printf ("Valid sound card names (comma separated):\n");
4588 for (c
= soundhw
; c
->name
; ++c
) {
4589 printf ("%-11s %s\n", c
->name
, c
->descr
);
4591 printf ("\n-soundhw all will enable all of the above\n");
4592 exit (*optarg
!= '?');
4600 if (!strcmp (optarg
, "all")) {
4601 for (c
= soundhw
; c
->name
; ++c
) {
4609 e
= strchr (p
, ',');
4610 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4612 for (c
= soundhw
; c
->name
; ++c
) {
4613 if (!strncmp (c
->name
, p
, l
)) {
4622 "Unknown sound card name (too big to show)\n");
4625 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4630 p
+= l
+ (e
!= NULL
);
4634 goto show_valid_cards
;
4639 static void select_vgahw (const char *p
)
4643 if (strstart(p
, "std", &opts
)) {
4644 std_vga_enabled
= 1;
4645 cirrus_vga_enabled
= 0;
4647 } else if (strstart(p
, "cirrus", &opts
)) {
4648 cirrus_vga_enabled
= 1;
4649 std_vga_enabled
= 0;
4651 } else if (strstart(p
, "vmware", &opts
)) {
4652 cirrus_vga_enabled
= 0;
4653 std_vga_enabled
= 0;
4655 } else if (strstart(p
, "none", &opts
)) {
4656 cirrus_vga_enabled
= 0;
4657 std_vga_enabled
= 0;
4661 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4665 const char *nextopt
;
4667 if (strstart(opts
, ",retrace=", &nextopt
)) {
4669 if (strstart(opts
, "dumb", &nextopt
))
4670 vga_retrace_method
= VGA_RETRACE_DUMB
;
4671 else if (strstart(opts
, "precise", &nextopt
))
4672 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4673 else goto invalid_vga
;
4674 } else goto invalid_vga
;
4680 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4682 exit(STATUS_CONTROL_C_EXIT
);
4687 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4691 if(strlen(str
) != 36)
4694 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4695 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4696 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4704 #define MAX_NET_CLIENTS 32
4706 static int saved_argc
;
4707 static char **saved_argv
;
4709 void qemu_get_launch_info(int *argc
, char ***argv
, int *opt_daemonize
, const char **opt_incoming
)
4713 *opt_daemonize
= daemonize
;
4714 *opt_incoming
= incoming
;
4718 static int gethugepagesize(void)
4722 const char *needle
= "Hugepagesize:";
4724 unsigned long hugepagesize
;
4726 fd
= open("/proc/meminfo", O_RDONLY
);
4732 ret
= read(fd
, buf
, sizeof(buf
));
4738 size
= strstr(buf
, needle
);
4741 size
+= strlen(needle
);
4742 hugepagesize
= strtol(size
, NULL
, 0);
4743 return hugepagesize
;
4746 static void *alloc_mem_area(size_t memory
, unsigned long *len
, const char *path
)
4755 if (!kvm_has_sync_mmu()) {
4756 fprintf(stderr
, "host lacks mmu notifiers, disabling --mem-path\n");
4760 if (asprintf(&filename
, "%s/kvm.XXXXXX", path
) == -1)
4763 hpagesize
= gethugepagesize() * 1024;
4767 fd
= mkstemp(filename
);
4776 memory
= (memory
+hpagesize
-1) & ~(hpagesize
-1);
4779 * ftruncate is not supported by hugetlbfs in older
4780 * hosts, so don't bother checking for errors.
4781 * If anything goes wrong with it under other filesystems,
4784 ftruncate(fd
, memory
);
4787 /* NB: MAP_POPULATE won't exhaustively alloc all phys pages in the case
4788 * MAP_PRIVATE is requested. For mem_prealloc we mmap as MAP_SHARED
4789 * to sidestep this quirk.
4791 flags
= mem_prealloc
? MAP_POPULATE
|MAP_SHARED
: MAP_PRIVATE
;
4792 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, flags
, fd
, 0);
4794 area
= mmap(0, memory
, PROT_READ
|PROT_WRITE
, MAP_PRIVATE
, fd
, 0);
4796 if (area
== MAP_FAILED
) {
4797 perror("alloc_mem_area: can't mmap hugetlbfs pages");
4806 static void *qemu_alloc_physram(unsigned long memory
)
4810 unsigned long map_len
= memory
;
4813 area
= alloc_mem_area(memory
, &map_len
, mem_path
);
4816 area
= qemu_vmalloc(memory
);
4818 if (kvm_setup_guest_memory(area
, map_len
))
4826 static void termsig_handler(int signal
)
4828 qemu_system_shutdown_request();
4831 static void termsig_setup(void)
4833 struct sigaction act
;
4835 memset(&act
, 0, sizeof(act
));
4836 act
.sa_handler
= termsig_handler
;
4837 sigaction(SIGINT
, &act
, NULL
);
4838 sigaction(SIGHUP
, &act
, NULL
);
4839 sigaction(SIGTERM
, &act
, NULL
);
4844 int main(int argc
, char **argv
, char **envp
)
4846 #ifdef CONFIG_GDBSTUB
4848 const char *gdbstub_port
;
4850 uint32_t boot_devices_bitmap
= 0;
4852 int snapshot
, linux_boot
, net_boot
;
4853 const char *initrd_filename
;
4854 const char *kernel_filename
, *kernel_cmdline
;
4855 const char *boot_devices
= "";
4857 DisplayChangeListener
*dcl
;
4858 int cyls
, heads
, secs
, translation
;
4859 const char *net_clients
[MAX_NET_CLIENTS
];
4861 const char *bt_opts
[MAX_BT_CMDLINE
];
4865 const char *r
, *optarg
;
4866 CharDriverState
*monitor_hd
= NULL
;
4867 const char *monitor_device
;
4868 const char *serial_devices
[MAX_SERIAL_PORTS
];
4869 int serial_device_index
;
4870 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4871 int parallel_device_index
;
4872 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4873 int virtio_console_index
;
4874 const char *loadvm
= NULL
;
4875 QEMUMachine
*machine
;
4876 const char *cpu_model
;
4877 const char *usb_devices
[MAX_USB_CMDLINE
];
4878 int usb_devices_index
;
4881 const char *pid_file
= NULL
;
4882 const char *incoming
= NULL
;
4884 struct passwd
*pwd
= NULL
;
4885 const char *chroot_dir
= NULL
;
4886 const char *run_as
= NULL
;
4888 qemu_cache_utils_init(envp
);
4890 LIST_INIT (&vm_change_state_head
);
4893 struct sigaction act
;
4894 sigfillset(&act
.sa_mask
);
4896 act
.sa_handler
= SIG_IGN
;
4897 sigaction(SIGPIPE
, &act
, NULL
);
4900 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4901 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4902 QEMU to run on a single CPU */
4907 h
= GetCurrentProcess();
4908 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4909 for(i
= 0; i
< 32; i
++) {
4910 if (mask
& (1 << i
))
4915 SetProcessAffinityMask(h
, mask
);
4921 register_machines();
4922 machine
= first_machine
;
4924 initrd_filename
= NULL
;
4926 vga_ram_size
= VGA_RAM_SIZE
;
4927 #ifdef CONFIG_GDBSTUB
4929 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4934 kernel_filename
= NULL
;
4935 kernel_cmdline
= "";
4936 cyls
= heads
= secs
= 0;
4937 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4938 monitor_device
= "vc:80Cx24C";
4940 serial_devices
[0] = "vc:80Cx24C";
4941 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4942 serial_devices
[i
] = NULL
;
4943 serial_device_index
= 0;
4945 parallel_devices
[0] = "vc:80Cx24C";
4946 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4947 parallel_devices
[i
] = NULL
;
4948 parallel_device_index
= 0;
4950 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4951 virtio_consoles
[i
] = NULL
;
4952 virtio_console_index
= 0;
4954 usb_devices_index
= 0;
4955 assigned_devices_index
= 0;
4974 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4976 const QEMUOption
*popt
;
4979 /* Treat --foo the same as -foo. */
4982 popt
= qemu_options
;
4985 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4989 if (!strcmp(popt
->name
, r
+ 1))
4993 if (popt
->flags
& HAS_ARG
) {
4994 if (optind
>= argc
) {
4995 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4999 optarg
= argv
[optind
++];
5004 switch(popt
->index
) {
5006 machine
= find_machine(optarg
);
5009 printf("Supported machines are:\n");
5010 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5011 printf("%-10s %s%s\n",
5013 m
== first_machine
? " (default)" : "");
5015 exit(*optarg
!= '?');
5018 case QEMU_OPTION_cpu
:
5019 /* hw initialization will check this */
5020 if (*optarg
== '?') {
5021 /* XXX: implement xxx_cpu_list for targets that still miss it */
5022 #if defined(cpu_list)
5023 cpu_list(stdout
, &fprintf
);
5030 case QEMU_OPTION_initrd
:
5031 initrd_filename
= optarg
;
5033 case QEMU_OPTION_hda
:
5035 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5037 hda_index
= drive_add(optarg
, HD_ALIAS
5038 ",cyls=%d,heads=%d,secs=%d%s",
5039 0, cyls
, heads
, secs
,
5040 translation
== BIOS_ATA_TRANSLATION_LBA
?
5042 translation
== BIOS_ATA_TRANSLATION_NONE
?
5043 ",trans=none" : "");
5045 case QEMU_OPTION_hdb
:
5046 case QEMU_OPTION_hdc
:
5047 case QEMU_OPTION_hdd
:
5048 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5050 case QEMU_OPTION_drive
:
5051 drive_add(NULL
, "%s", optarg
);
5053 case QEMU_OPTION_mtdblock
:
5054 drive_add(optarg
, MTD_ALIAS
);
5056 case QEMU_OPTION_sd
:
5057 drive_add(optarg
, SD_ALIAS
);
5059 case QEMU_OPTION_pflash
:
5060 drive_add(optarg
, PFLASH_ALIAS
);
5062 case QEMU_OPTION_snapshot
:
5065 case QEMU_OPTION_hdachs
:
5069 cyls
= strtol(p
, (char **)&p
, 0);
5070 if (cyls
< 1 || cyls
> 16383)
5075 heads
= strtol(p
, (char **)&p
, 0);
5076 if (heads
< 1 || heads
> 16)
5081 secs
= strtol(p
, (char **)&p
, 0);
5082 if (secs
< 1 || secs
> 63)
5086 if (!strcmp(p
, "none"))
5087 translation
= BIOS_ATA_TRANSLATION_NONE
;
5088 else if (!strcmp(p
, "lba"))
5089 translation
= BIOS_ATA_TRANSLATION_LBA
;
5090 else if (!strcmp(p
, "auto"))
5091 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5094 } else if (*p
!= '\0') {
5096 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5099 if (hda_index
!= -1)
5100 snprintf(drives_opt
[hda_index
].opt
,
5101 sizeof(drives_opt
[hda_index
].opt
),
5102 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5103 0, cyls
, heads
, secs
,
5104 translation
== BIOS_ATA_TRANSLATION_LBA
?
5106 translation
== BIOS_ATA_TRANSLATION_NONE
?
5107 ",trans=none" : "");
5110 case QEMU_OPTION_nographic
:
5113 #ifdef CONFIG_CURSES
5114 case QEMU_OPTION_curses
:
5118 case QEMU_OPTION_portrait
:
5121 case QEMU_OPTION_kernel
:
5122 kernel_filename
= optarg
;
5124 case QEMU_OPTION_append
:
5125 kernel_cmdline
= optarg
;
5127 case QEMU_OPTION_cdrom
:
5128 drive_add(optarg
, CDROM_ALIAS
);
5130 case QEMU_OPTION_boot
:
5131 boot_devices
= optarg
;
5132 /* We just do some generic consistency checks */
5134 /* Could easily be extended to 64 devices if needed */
5137 boot_devices_bitmap
= 0;
5138 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5139 /* Allowed boot devices are:
5140 * a b : floppy disk drives
5141 * c ... f : IDE disk drives
5142 * g ... m : machine implementation dependant drives
5143 * n ... p : network devices
5144 * It's up to each machine implementation to check
5145 * if the given boot devices match the actual hardware
5146 * implementation and firmware features.
5148 if (*p
< 'a' || *p
> 'q') {
5149 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5152 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5154 "Boot device '%c' was given twice\n",*p
);
5157 boot_devices_bitmap
|= 1 << (*p
- 'a');
5161 case QEMU_OPTION_fda
:
5162 case QEMU_OPTION_fdb
:
5163 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5166 case QEMU_OPTION_no_fd_bootchk
:
5170 case QEMU_OPTION_net
:
5171 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5172 fprintf(stderr
, "qemu: too many network clients\n");
5175 net_clients
[nb_net_clients
] = optarg
;
5179 case QEMU_OPTION_tftp
:
5180 tftp_prefix
= optarg
;
5182 case QEMU_OPTION_bootp
:
5183 bootp_filename
= optarg
;
5186 case QEMU_OPTION_smb
:
5187 net_slirp_smb(optarg
);
5190 case QEMU_OPTION_redir
:
5191 net_slirp_redir(optarg
);
5194 case QEMU_OPTION_bt
:
5195 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5196 fprintf(stderr
, "qemu: too many bluetooth options\n");
5199 bt_opts
[nb_bt_opts
++] = optarg
;
5202 case QEMU_OPTION_audio_help
:
5206 case QEMU_OPTION_soundhw
:
5207 select_soundhw (optarg
);
5213 case QEMU_OPTION_m
: {
5217 value
= strtoul(optarg
, &ptr
, 10);
5219 case 0: case 'M': case 'm':
5226 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5230 /* On 32-bit hosts, QEMU is limited by virtual address space */
5231 if (value
> (2047 << 20)
5233 && HOST_LONG_BITS
== 32
5236 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5239 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5240 fprintf(stderr
, "qemu: ram size too large\n");
5249 const CPULogItem
*item
;
5251 mask
= cpu_str_to_log_mask(optarg
);
5253 printf("Log items (comma separated):\n");
5254 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5255 printf("%-10s %s\n", item
->name
, item
->help
);
5262 #ifdef CONFIG_GDBSTUB
5267 gdbstub_port
= optarg
;
5273 case QEMU_OPTION_bios
:
5280 keyboard_layout
= optarg
;
5282 case QEMU_OPTION_localtime
:
5285 case QEMU_OPTION_vga
:
5286 select_vgahw (optarg
);
5293 w
= strtol(p
, (char **)&p
, 10);
5296 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5302 h
= strtol(p
, (char **)&p
, 10);
5307 depth
= strtol(p
, (char **)&p
, 10);
5308 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5309 depth
!= 24 && depth
!= 32)
5311 } else if (*p
== '\0') {
5312 depth
= graphic_depth
;
5319 graphic_depth
= depth
;
5322 case QEMU_OPTION_echr
:
5325 term_escape_char
= strtol(optarg
, &r
, 0);
5327 printf("Bad argument to echr\n");
5330 case QEMU_OPTION_monitor
:
5331 monitor_device
= optarg
;
5333 case QEMU_OPTION_serial
:
5334 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5335 fprintf(stderr
, "qemu: too many serial ports\n");
5338 serial_devices
[serial_device_index
] = optarg
;
5339 serial_device_index
++;
5341 case QEMU_OPTION_virtiocon
:
5342 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5343 fprintf(stderr
, "qemu: too many virtio consoles\n");
5346 virtio_consoles
[virtio_console_index
] = optarg
;
5347 virtio_console_index
++;
5349 case QEMU_OPTION_parallel
:
5350 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5351 fprintf(stderr
, "qemu: too many parallel ports\n");
5354 parallel_devices
[parallel_device_index
] = optarg
;
5355 parallel_device_index
++;
5357 case QEMU_OPTION_loadvm
:
5360 case QEMU_OPTION_full_screen
:
5364 case QEMU_OPTION_no_frame
:
5367 case QEMU_OPTION_alt_grab
:
5370 case QEMU_OPTION_no_quit
:
5373 case QEMU_OPTION_sdl
:
5377 case QEMU_OPTION_pidfile
:
5381 case QEMU_OPTION_win2k_hack
:
5382 win2k_install_hack
= 1;
5384 case QEMU_OPTION_rtc_td_hack
:
5387 case QEMU_OPTION_acpitable
:
5388 if(acpi_table_add(optarg
) < 0) {
5389 fprintf(stderr
, "Wrong acpi table provided\n");
5395 case QEMU_OPTION_no_kqemu
:
5398 case QEMU_OPTION_kernel_kqemu
:
5403 case QEMU_OPTION_enable_kvm
:
5411 case QEMU_OPTION_no_kvm
:
5414 case QEMU_OPTION_no_kvm_irqchip
: {
5419 case QEMU_OPTION_no_kvm_pit
: {
5423 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5424 kvm_pit_reinject
= 0;
5427 case QEMU_OPTION_enable_nesting
: {
5431 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5432 case QEMU_OPTION_pcidevice
:
5433 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5434 fprintf(stderr
, "Too many assigned devices\n");
5437 assigned_devices
[assigned_devices_index
] = optarg
;
5438 assigned_devices_index
++;
5442 case QEMU_OPTION_usb
:
5445 case QEMU_OPTION_usbdevice
:
5447 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5448 fprintf(stderr
, "Too many USB devices\n");
5451 usb_devices
[usb_devices_index
] = optarg
;
5452 usb_devices_index
++;
5454 case QEMU_OPTION_smp
:
5455 smp_cpus
= atoi(optarg
);
5457 fprintf(stderr
, "Invalid number of CPUs\n");
5461 case QEMU_OPTION_vnc
:
5462 vnc_display
= optarg
;
5464 case QEMU_OPTION_no_acpi
:
5467 case QEMU_OPTION_no_hpet
:
5470 case QEMU_OPTION_no_reboot
:
5473 case QEMU_OPTION_no_shutdown
:
5476 case QEMU_OPTION_show_cursor
:
5479 case QEMU_OPTION_uuid
:
5480 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5481 fprintf(stderr
, "Fail to parse UUID string."
5482 " Wrong format.\n");
5486 case QEMU_OPTION_daemonize
:
5489 case QEMU_OPTION_option_rom
:
5490 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5491 fprintf(stderr
, "Too many option ROMs\n");
5494 option_rom
[nb_option_roms
] = optarg
;
5497 case QEMU_OPTION_semihosting
:
5498 semihosting_enabled
= 1;
5500 case QEMU_OPTION_tdf
:
5503 case QEMU_OPTION_kvm_shadow_memory
:
5504 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5506 case QEMU_OPTION_mempath
:
5510 case QEMU_OPTION_mem_prealloc
:
5511 mem_prealloc
= !mem_prealloc
;
5514 case QEMU_OPTION_name
:
5517 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5518 case QEMU_OPTION_prom_env
:
5519 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5520 fprintf(stderr
, "Too many prom variables\n");
5523 prom_envs
[nb_prom_envs
] = optarg
;
5527 case QEMU_OPTION_cpu_vendor
:
5528 cpu_vendor_string
= optarg
;
5531 case QEMU_OPTION_old_param
:
5535 case QEMU_OPTION_clock
:
5536 configure_alarms(optarg
);
5538 case QEMU_OPTION_startdate
:
5541 time_t rtc_start_date
;
5542 if (!strcmp(optarg
, "now")) {
5543 rtc_date_offset
= -1;
5545 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5553 } else if (sscanf(optarg
, "%d-%d-%d",
5556 &tm
.tm_mday
) == 3) {
5565 rtc_start_date
= mktimegm(&tm
);
5566 if (rtc_start_date
== -1) {
5568 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5569 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5572 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5576 case QEMU_OPTION_tb_size
:
5577 tb_size
= strtol(optarg
, NULL
, 0);
5581 case QEMU_OPTION_icount
:
5583 if (strcmp(optarg
, "auto") == 0) {
5584 icount_time_shift
= -1;
5586 icount_time_shift
= strtol(optarg
, NULL
, 0);
5589 case QEMU_OPTION_incoming
:
5592 case QEMU_OPTION_chroot
:
5593 chroot_dir
= optarg
;
5595 case QEMU_OPTION_runas
:
5597 case QEMU_OPTION_nvram
:
5604 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5605 if (kvm_allowed
&& kqemu_allowed
) {
5607 "You can not enable both KVM and kqemu at the same time\n");
5612 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5613 if (smp_cpus
> machine
->max_cpus
) {
5614 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5615 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5621 if (serial_device_index
== 0)
5622 serial_devices
[0] = "stdio";
5623 if (parallel_device_index
== 0)
5624 parallel_devices
[0] = "null";
5625 if (strncmp(monitor_device
, "vc", 2) == 0)
5626 monitor_device
= "stdio";
5633 if (pipe(fds
) == -1)
5644 len
= read(fds
[0], &status
, 1);
5645 if (len
== -1 && (errno
== EINTR
))
5650 else if (status
== 1) {
5651 fprintf(stderr
, "Could not acquire pidfile\n");
5668 signal(SIGTSTP
, SIG_IGN
);
5669 signal(SIGTTOU
, SIG_IGN
);
5670 signal(SIGTTIN
, SIG_IGN
);
5675 if (kvm_enabled()) {
5676 if (kvm_qemu_init() < 0) {
5677 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5678 #ifdef NO_CPU_EMULATION
5679 fprintf(stderr
, "Compiled with --disable-cpu-emulation, exiting.\n");
5687 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5690 write(fds
[1], &status
, 1);
5692 fprintf(stderr
, "Could not acquire pid file\n");
5700 linux_boot
= (kernel_filename
!= NULL
);
5701 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5703 if (!linux_boot
&& net_boot
== 0 &&
5704 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5707 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5708 fprintf(stderr
, "-append only allowed with -kernel option\n");
5712 if (!linux_boot
&& initrd_filename
!= NULL
) {
5713 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5717 /* boot to floppy or the default cd if no hard disk defined yet */
5718 if (!boot_devices
[0]) {
5719 boot_devices
= "cad";
5721 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5724 if (init_timer_alarm() < 0) {
5725 fprintf(stderr
, "could not initialize alarm timer\n");
5728 if (use_icount
&& icount_time_shift
< 0) {
5730 /* 125MIPS seems a reasonable initial guess at the guest speed.
5731 It will be corrected fairly quickly anyway. */
5732 icount_time_shift
= 3;
5733 init_icount_adjust();
5740 /* init network clients */
5741 if (nb_net_clients
== 0) {
5742 /* if no clients, we use a default config */
5743 net_clients
[nb_net_clients
++] = "nic";
5745 net_clients
[nb_net_clients
++] = "user";
5749 for(i
= 0;i
< nb_net_clients
; i
++) {
5750 if (net_client_parse(net_clients
[i
]) < 0)
5756 /* XXX: this should be moved in the PC machine instantiation code */
5757 if (net_boot
!= 0) {
5759 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5760 const char *model
= nd_table
[i
].model
;
5762 if (net_boot
& (1 << i
)) {
5765 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5766 if (get_image_size(buf
) > 0) {
5767 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5768 fprintf(stderr
, "Too many option ROMs\n");
5771 option_rom
[nb_option_roms
] = strdup(buf
);
5778 fprintf(stderr
, "No valid PXE rom found for network device\n");
5784 /* init the bluetooth world */
5785 for (i
= 0; i
< nb_bt_opts
; i
++)
5786 if (bt_parse(bt_opts
[i
]))
5789 /* init the memory */
5790 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5792 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5794 if (ram_size
< phys_ram_size
) {
5795 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5796 machine
->name
, (unsigned long long) phys_ram_size
);
5800 phys_ram_size
= ram_size
;
5802 ram_size
= phys_ram_size
;
5805 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5807 phys_ram_size
+= ram_size
;
5810 /* Initialize kvm */
5811 #if defined(TARGET_I386) || defined(TARGET_X86_64)
5812 #define KVM_EXTRA_PAGES 3
5814 #define KVM_EXTRA_PAGES 0
5816 if (kvm_enabled()) {
5817 phys_ram_size
+= KVM_EXTRA_PAGES
* TARGET_PAGE_SIZE
;
5818 if (kvm_qemu_create_context() < 0) {
5819 fprintf(stderr
, "Could not create KVM context\n");
5824 phys_ram_base
= qemu_alloc_physram(phys_ram_size
);
5825 if (!phys_ram_base
) {
5826 fprintf(stderr
, "Could not allocate physical memory\n");
5830 /* init the dynamic translator */
5831 cpu_exec_init_all(tb_size
* 1024 * 1024);
5836 /* we always create the cdrom drive, even if no disk is there */
5838 if (nb_drives_opt
< MAX_DRIVES
)
5839 drive_add(NULL
, CDROM_ALIAS
);
5841 /* we always create at least one floppy */
5843 if (nb_drives_opt
< MAX_DRIVES
)
5844 drive_add(NULL
, FD_ALIAS
, 0);
5846 /* we always create one sd slot, even if no card is in it */
5848 if (nb_drives_opt
< MAX_DRIVES
)
5849 drive_add(NULL
, SD_ALIAS
);
5851 /* open the virtual block devices
5852 * note that migration with device
5853 * hot add/remove is broken.
5855 for(i
= 0; i
< nb_drives_opt
; i
++)
5856 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5859 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5860 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5863 /* must be after terminal init, SDL library changes signal handlers */
5867 /* Maintain compatibility with multiple stdio monitors */
5868 if (!strcmp(monitor_device
,"stdio")) {
5869 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5870 const char *devname
= serial_devices
[i
];
5871 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5872 monitor_device
= NULL
;
5874 } else if (devname
&& !strcmp(devname
,"stdio")) {
5875 monitor_device
= NULL
;
5876 serial_devices
[i
] = "mon:stdio";
5883 if (kvm_enabled()) {
5886 ret
= kvm_init(smp_cpus
);
5888 fprintf(stderr
, "failed to initialize KVM\n");
5894 if (monitor_device
) {
5895 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5897 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5902 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5903 const char *devname
= serial_devices
[i
];
5904 if (devname
&& strcmp(devname
, "none")) {
5906 snprintf(label
, sizeof(label
), "serial%d", i
);
5907 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5908 if (!serial_hds
[i
]) {
5909 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5916 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5917 const char *devname
= parallel_devices
[i
];
5918 if (devname
&& strcmp(devname
, "none")) {
5920 snprintf(label
, sizeof(label
), "parallel%d", i
);
5921 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5922 if (!parallel_hds
[i
]) {
5923 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5930 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5931 const char *devname
= virtio_consoles
[i
];
5932 if (devname
&& strcmp(devname
, "none")) {
5934 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5935 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5936 if (!virtcon_hds
[i
]) {
5937 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5947 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5948 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5950 current_machine
= machine
;
5952 /* Set KVM's vcpu state to qemu's initial CPUState. */
5953 if (kvm_enabled()) {
5956 ret
= kvm_sync_vcpus();
5958 fprintf(stderr
, "failed to initialize vcpus\n");
5963 /* init USB devices */
5965 for(i
= 0; i
< usb_devices_index
; i
++) {
5966 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5967 fprintf(stderr
, "Warning: could not add USB device %s\n",
5974 dumb_display_init();
5975 /* just use the first displaystate for the moment */
5980 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5984 #if defined(CONFIG_CURSES)
5986 /* At the moment curses cannot be used with other displays */
5987 curses_display_init(ds
, full_screen
);
5991 if (vnc_display
!= NULL
) {
5992 vnc_display_init(ds
);
5993 if (vnc_display_open(ds
, vnc_display
) < 0)
5996 #if defined(CONFIG_SDL)
5997 if (sdl
|| !vnc_display
)
5998 sdl_display_init(ds
, full_screen
, no_frame
);
5999 #elif defined(CONFIG_COCOA)
6000 if (sdl
|| !vnc_display
)
6001 cocoa_display_init(ds
, full_screen
);
6007 dcl
= ds
->listeners
;
6008 while (dcl
!= NULL
) {
6009 if (dcl
->dpy_refresh
!= NULL
) {
6010 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6011 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6016 if (nographic
|| (vnc_display
&& !sdl
)) {
6017 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6018 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6021 text_consoles_set_display(display_state
);
6022 qemu_chr_initial_reset();
6024 if (monitor_device
&& monitor_hd
)
6025 monitor_init(monitor_hd
, !nographic
);
6027 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6028 const char *devname
= serial_devices
[i
];
6029 if (devname
&& strcmp(devname
, "none")) {
6031 snprintf(label
, sizeof(label
), "serial%d", i
);
6032 if (strstart(devname
, "vc", 0))
6033 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6037 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6038 const char *devname
= parallel_devices
[i
];
6039 if (devname
&& strcmp(devname
, "none")) {
6041 snprintf(label
, sizeof(label
), "parallel%d", i
);
6042 if (strstart(devname
, "vc", 0))
6043 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6047 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6048 const char *devname
= virtio_consoles
[i
];
6049 if (virtcon_hds
[i
] && devname
) {
6051 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6052 if (strstart(devname
, "vc", 0))
6053 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6057 #ifdef CONFIG_GDBSTUB
6059 /* XXX: use standard host:port notation and modify options
6061 if (gdbserver_start(gdbstub_port
) < 0) {
6062 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
6073 autostart
= 0; /* fixme how to deal with -daemonize */
6074 qemu_start_incoming_migration(incoming
);
6085 len
= write(fds
[1], &status
, 1);
6086 if (len
== -1 && (errno
== EINTR
))
6093 TFR(fd
= open("/dev/null", O_RDWR
));
6100 pwd
= getpwnam(run_as
);
6102 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6108 if (chroot(chroot_dir
) < 0) {
6109 fprintf(stderr
, "chroot failed\n");
6116 if (setgid(pwd
->pw_gid
) < 0) {
6117 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6120 if (setuid(pwd
->pw_uid
) < 0) {
6121 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6124 if (setuid(0) != -1) {
6125 fprintf(stderr
, "Dropping privileges failed\n");