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
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #if defined(__NetBSD__)
47 #include <net/if_tap.h>
50 #include <linux/if_tun.h>
52 #include <arpa/inet.h>
55 #include <sys/select.h>
58 #if defined(__FreeBSD__) || defined(__DragonFly__)
63 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64 #include <freebsd/stdlib.h>
69 #include <linux/rtc.h>
71 /* For the benefit of older linux systems which don't supply it,
72 we use a local copy of hpet.h. */
73 /* #include <linux/hpet.h> */
76 #include <linux/ppdev.h>
77 #include <linux/parport.h>
81 #include <sys/ethernet.h>
82 #include <sys/sockio.h>
83 #include <netinet/arp.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_icmp.h> // must come after ip.h
88 #include <netinet/udp.h>
89 #include <netinet/tcp.h>
97 #if defined(__OpenBSD__)
101 #if defined(CONFIG_VDE)
102 #include <libvdeplug.h>
108 #include <sys/timeb.h>
109 #include <mmsystem.h>
110 #define getopt_long_only getopt_long
111 #define memalign(align, size) malloc(size)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/smbios.h"
148 #include "qemu-timer.h"
149 #include "qemu-char.h"
150 #include "cache-utils.h"
153 #include "audio/audio.h"
154 #include "migration.h"
160 #include "exec-all.h"
162 #include "qemu_socket.h"
164 #if defined(CONFIG_SLIRP)
165 #include "libslirp.h"
168 //#define DEBUG_UNUSED_IOPORT
169 //#define DEBUG_IOPORT
171 //#define DEBUG_SLIRP
175 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
177 # define LOG_IOPORT(...) do { } while (0)
180 #define DEFAULT_RAM_SIZE 128
182 /* Max number of USB devices that can be specified on the commandline. */
183 #define MAX_USB_CMDLINE 8
185 /* Max number of bluetooth switches on the commandline. */
186 #define MAX_BT_CMDLINE 10
188 /* XXX: use a two level table to limit memory usage */
189 #define MAX_IOPORTS 65536
191 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
192 const char *bios_name
= NULL
;
193 static void *ioport_opaque
[MAX_IOPORTS
];
194 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
195 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
196 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
197 to store the VM snapshots */
198 DriveInfo drives_table
[MAX_DRIVES
+1];
200 static int vga_ram_size
;
201 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
202 static DisplayState
*display_state
;
206 const char* keyboard_layout
= NULL
;
207 int64_t ticks_per_sec
;
210 NICInfo nd_table
[MAX_NICS
];
212 static int autostart
;
213 static int rtc_utc
= 1;
214 static int rtc_date_offset
= -1; /* -1 means no change */
215 int cirrus_vga_enabled
= 1;
216 int std_vga_enabled
= 0;
217 int vmsvga_enabled
= 0;
219 int graphic_width
= 1024;
220 int graphic_height
= 768;
221 int graphic_depth
= 8;
223 int graphic_width
= 800;
224 int graphic_height
= 600;
225 int graphic_depth
= 15;
227 static int full_screen
= 0;
229 static int no_frame
= 0;
232 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
233 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
234 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
236 int win2k_install_hack
= 0;
242 const char *vnc_display
;
243 int acpi_enabled
= 1;
249 int graphic_rotate
= 0;
253 const char *option_rom
[MAX_OPTION_ROMS
];
255 int semihosting_enabled
= 0;
259 const char *qemu_name
;
261 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
262 unsigned int nb_prom_envs
= 0;
263 const char *prom_envs
[MAX_PROM_ENVS
];
266 struct drive_opt drives_opt
[MAX_DRIVES
];
268 static CPUState
*cur_cpu
;
269 static CPUState
*next_cpu
;
270 static int event_pending
= 1;
271 /* Conversion factor from emulated instructions to virtual clock ticks. */
272 static int icount_time_shift
;
273 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
274 #define MAX_ICOUNT_SHIFT 10
275 /* Compensate for varying guest execution speed. */
276 static int64_t qemu_icount_bias
;
277 static QEMUTimer
*icount_rt_timer
;
278 static QEMUTimer
*icount_vm_timer
;
279 static QEMUTimer
*nographic_timer
;
281 uint8_t qemu_uuid
[16];
283 /***********************************************************/
284 /* x86 ISA bus support */
286 target_phys_addr_t isa_mem_base
= 0;
289 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
290 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
292 static uint32_t ioport_read(int index
, uint32_t address
)
294 static IOPortReadFunc
*default_func
[3] = {
295 default_ioport_readb
,
296 default_ioport_readw
,
299 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
301 func
= default_func
[index
];
302 return func(ioport_opaque
[address
], address
);
305 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
307 static IOPortWriteFunc
*default_func
[3] = {
308 default_ioport_writeb
,
309 default_ioport_writew
,
310 default_ioport_writel
312 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
314 func
= default_func
[index
];
315 func(ioport_opaque
[address
], address
, data
);
318 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
320 #ifdef DEBUG_UNUSED_IOPORT
321 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
326 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
328 #ifdef DEBUG_UNUSED_IOPORT
329 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
333 /* default is to make two byte accesses */
334 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
337 data
= ioport_read(0, address
);
338 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
339 data
|= ioport_read(0, address
) << 8;
343 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
345 ioport_write(0, address
, data
& 0xff);
346 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
347 ioport_write(0, address
, (data
>> 8) & 0xff);
350 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
352 #ifdef DEBUG_UNUSED_IOPORT
353 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
358 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
360 #ifdef DEBUG_UNUSED_IOPORT
361 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
365 /* size is the word size in byte */
366 int register_ioport_read(int start
, int length
, int size
,
367 IOPortReadFunc
*func
, void *opaque
)
373 } else if (size
== 2) {
375 } else if (size
== 4) {
378 hw_error("register_ioport_read: invalid size");
381 for(i
= start
; i
< start
+ length
; i
+= size
) {
382 ioport_read_table
[bsize
][i
] = func
;
383 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
384 hw_error("register_ioport_read: invalid opaque");
385 ioport_opaque
[i
] = opaque
;
390 /* size is the word size in byte */
391 int register_ioport_write(int start
, int length
, int size
,
392 IOPortWriteFunc
*func
, void *opaque
)
398 } else if (size
== 2) {
400 } else if (size
== 4) {
403 hw_error("register_ioport_write: invalid size");
406 for(i
= start
; i
< start
+ length
; i
+= size
) {
407 ioport_write_table
[bsize
][i
] = func
;
408 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
409 hw_error("register_ioport_write: invalid opaque");
410 ioport_opaque
[i
] = opaque
;
415 void isa_unassign_ioport(int start
, int length
)
419 for(i
= start
; i
< start
+ length
; i
++) {
420 ioport_read_table
[0][i
] = default_ioport_readb
;
421 ioport_read_table
[1][i
] = default_ioport_readw
;
422 ioport_read_table
[2][i
] = default_ioport_readl
;
424 ioport_write_table
[0][i
] = default_ioport_writeb
;
425 ioport_write_table
[1][i
] = default_ioport_writew
;
426 ioport_write_table
[2][i
] = default_ioport_writel
;
428 ioport_opaque
[i
] = NULL
;
432 /***********************************************************/
434 void cpu_outb(CPUState
*env
, int addr
, int val
)
436 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
437 ioport_write(0, addr
, val
);
440 env
->last_io_time
= cpu_get_time_fast();
444 void cpu_outw(CPUState
*env
, int addr
, int val
)
446 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
447 ioport_write(1, addr
, val
);
450 env
->last_io_time
= cpu_get_time_fast();
454 void cpu_outl(CPUState
*env
, int addr
, int val
)
456 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
457 ioport_write(2, addr
, val
);
460 env
->last_io_time
= cpu_get_time_fast();
464 int cpu_inb(CPUState
*env
, int addr
)
467 val
= ioport_read(0, addr
);
468 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
471 env
->last_io_time
= cpu_get_time_fast();
476 int cpu_inw(CPUState
*env
, int addr
)
479 val
= ioport_read(1, addr
);
480 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
483 env
->last_io_time
= cpu_get_time_fast();
488 int cpu_inl(CPUState
*env
, int addr
)
491 val
= ioport_read(2, addr
);
492 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
495 env
->last_io_time
= cpu_get_time_fast();
500 /***********************************************************/
501 void hw_error(const char *fmt
, ...)
507 fprintf(stderr
, "qemu: hardware error: ");
508 vfprintf(stderr
, fmt
, ap
);
509 fprintf(stderr
, "\n");
510 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
511 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
513 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
515 cpu_dump_state(env
, stderr
, fprintf
, 0);
525 static QEMUBalloonEvent
*qemu_balloon_event
;
526 void *qemu_balloon_event_opaque
;
528 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
530 qemu_balloon_event
= func
;
531 qemu_balloon_event_opaque
= opaque
;
534 void qemu_balloon(ram_addr_t target
)
536 if (qemu_balloon_event
)
537 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
540 ram_addr_t
qemu_balloon_status(void)
542 if (qemu_balloon_event
)
543 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
547 /***********************************************************/
550 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
551 static void *qemu_put_kbd_event_opaque
;
552 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
553 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
555 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
557 qemu_put_kbd_event_opaque
= opaque
;
558 qemu_put_kbd_event
= func
;
561 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
562 void *opaque
, int absolute
,
565 QEMUPutMouseEntry
*s
, *cursor
;
567 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
569 s
->qemu_put_mouse_event
= func
;
570 s
->qemu_put_mouse_event_opaque
= opaque
;
571 s
->qemu_put_mouse_event_absolute
= absolute
;
572 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
575 if (!qemu_put_mouse_event_head
) {
576 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
580 cursor
= qemu_put_mouse_event_head
;
581 while (cursor
->next
!= NULL
)
582 cursor
= cursor
->next
;
585 qemu_put_mouse_event_current
= s
;
590 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
592 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
594 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
597 cursor
= qemu_put_mouse_event_head
;
598 while (cursor
!= NULL
&& cursor
!= entry
) {
600 cursor
= cursor
->next
;
603 if (cursor
== NULL
) // does not exist or list empty
605 else if (prev
== NULL
) { // entry is head
606 qemu_put_mouse_event_head
= cursor
->next
;
607 if (qemu_put_mouse_event_current
== entry
)
608 qemu_put_mouse_event_current
= cursor
->next
;
609 qemu_free(entry
->qemu_put_mouse_event_name
);
614 prev
->next
= entry
->next
;
616 if (qemu_put_mouse_event_current
== entry
)
617 qemu_put_mouse_event_current
= prev
;
619 qemu_free(entry
->qemu_put_mouse_event_name
);
623 void kbd_put_keycode(int keycode
)
625 if (qemu_put_kbd_event
) {
626 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
630 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
632 QEMUPutMouseEvent
*mouse_event
;
633 void *mouse_event_opaque
;
636 if (!qemu_put_mouse_event_current
) {
641 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
643 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
646 if (graphic_rotate
) {
647 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
650 width
= graphic_width
- 1;
651 mouse_event(mouse_event_opaque
,
652 width
- dy
, dx
, dz
, buttons_state
);
654 mouse_event(mouse_event_opaque
,
655 dx
, dy
, dz
, buttons_state
);
659 int kbd_mouse_is_absolute(void)
661 if (!qemu_put_mouse_event_current
)
664 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
667 void do_info_mice(Monitor
*mon
)
669 QEMUPutMouseEntry
*cursor
;
672 if (!qemu_put_mouse_event_head
) {
673 monitor_printf(mon
, "No mouse devices connected\n");
677 monitor_printf(mon
, "Mouse devices available:\n");
678 cursor
= qemu_put_mouse_event_head
;
679 while (cursor
!= NULL
) {
680 monitor_printf(mon
, "%c Mouse #%d: %s\n",
681 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
682 index
, cursor
->qemu_put_mouse_event_name
);
684 cursor
= cursor
->next
;
688 void do_mouse_set(Monitor
*mon
, int index
)
690 QEMUPutMouseEntry
*cursor
;
693 if (!qemu_put_mouse_event_head
) {
694 monitor_printf(mon
, "No mouse devices connected\n");
698 cursor
= qemu_put_mouse_event_head
;
699 while (cursor
!= NULL
&& index
!= i
) {
701 cursor
= cursor
->next
;
705 qemu_put_mouse_event_current
= cursor
;
707 monitor_printf(mon
, "Mouse at given index not found\n");
710 /* compute with 96 bit intermediate result: (a*b)/c */
711 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
716 #ifdef WORDS_BIGENDIAN
726 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
727 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
730 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
734 /***********************************************************/
735 /* real time host monotonic timer */
737 #define QEMU_TIMER_BASE 1000000000LL
741 static int64_t clock_freq
;
743 static void init_get_clock(void)
747 ret
= QueryPerformanceFrequency(&freq
);
749 fprintf(stderr
, "Could not calibrate ticks\n");
752 clock_freq
= freq
.QuadPart
;
755 static int64_t get_clock(void)
758 QueryPerformanceCounter(&ti
);
759 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
764 static int use_rt_clock
;
766 static void init_get_clock(void)
769 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
770 || defined(__DragonFly__)
773 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
780 static int64_t get_clock(void)
782 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
783 || defined(__DragonFly__)
786 clock_gettime(CLOCK_MONOTONIC
, &ts
);
787 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
791 /* XXX: using gettimeofday leads to problems if the date
792 changes, so it should be avoided. */
794 gettimeofday(&tv
, NULL
);
795 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
800 /* Return the virtual CPU time, based on the instruction counter. */
801 static int64_t cpu_get_icount(void)
804 CPUState
*env
= cpu_single_env
;;
805 icount
= qemu_icount
;
808 fprintf(stderr
, "Bad clock read\n");
809 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
811 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
814 /***********************************************************/
815 /* guest cycle counter */
817 static int64_t cpu_ticks_prev
;
818 static int64_t cpu_ticks_offset
;
819 static int64_t cpu_clock_offset
;
820 static int cpu_ticks_enabled
;
822 /* return the host CPU cycle counter and handle stop/restart */
823 int64_t cpu_get_ticks(void)
826 return cpu_get_icount();
828 if (!cpu_ticks_enabled
) {
829 return cpu_ticks_offset
;
832 ticks
= cpu_get_real_ticks();
833 if (cpu_ticks_prev
> ticks
) {
834 /* Note: non increasing ticks may happen if the host uses
836 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
838 cpu_ticks_prev
= ticks
;
839 return ticks
+ cpu_ticks_offset
;
843 /* return the host CPU monotonic timer and handle stop/restart */
844 static int64_t cpu_get_clock(void)
847 if (!cpu_ticks_enabled
) {
848 return cpu_clock_offset
;
851 return ti
+ cpu_clock_offset
;
855 /* enable cpu_get_ticks() */
856 void cpu_enable_ticks(void)
858 if (!cpu_ticks_enabled
) {
859 cpu_ticks_offset
-= cpu_get_real_ticks();
860 cpu_clock_offset
-= get_clock();
861 cpu_ticks_enabled
= 1;
865 /* disable cpu_get_ticks() : the clock is stopped. You must not call
866 cpu_get_ticks() after that. */
867 void cpu_disable_ticks(void)
869 if (cpu_ticks_enabled
) {
870 cpu_ticks_offset
= cpu_get_ticks();
871 cpu_clock_offset
= cpu_get_clock();
872 cpu_ticks_enabled
= 0;
876 /***********************************************************/
879 #define QEMU_TIMER_REALTIME 0
880 #define QEMU_TIMER_VIRTUAL 1
884 /* XXX: add frequency */
892 struct QEMUTimer
*next
;
895 struct qemu_alarm_timer
{
899 int (*start
)(struct qemu_alarm_timer
*t
);
900 void (*stop
)(struct qemu_alarm_timer
*t
);
901 void (*rearm
)(struct qemu_alarm_timer
*t
);
905 #define ALARM_FLAG_DYNTICKS 0x1
906 #define ALARM_FLAG_EXPIRED 0x2
908 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
910 return t
->flags
& ALARM_FLAG_DYNTICKS
;
913 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
915 if (!alarm_has_dynticks(t
))
921 /* TODO: MIN_TIMER_REARM_US should be optimized */
922 #define MIN_TIMER_REARM_US 250
924 static struct qemu_alarm_timer
*alarm_timer
;
926 static int alarm_timer_rfd
, alarm_timer_wfd
;
931 struct qemu_alarm_win32
{
935 } alarm_win32_data
= {0, NULL
, -1};
937 static int win32_start_timer(struct qemu_alarm_timer
*t
);
938 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
939 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
943 static int unix_start_timer(struct qemu_alarm_timer
*t
);
944 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
948 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
949 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
950 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
952 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
953 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
955 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
956 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
958 #endif /* __linux__ */
962 /* Correlation between real and virtual time is always going to be
963 fairly approximate, so ignore small variation.
964 When the guest is idle real and virtual time will be aligned in
966 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
968 static void icount_adjust(void)
973 static int64_t last_delta
;
974 /* If the VM is not running, then do nothing. */
978 cur_time
= cpu_get_clock();
979 cur_icount
= qemu_get_clock(vm_clock
);
980 delta
= cur_icount
- cur_time
;
981 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
983 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
984 && icount_time_shift
> 0) {
985 /* The guest is getting too far ahead. Slow time down. */
989 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
990 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
991 /* The guest is getting too far behind. Speed time up. */
995 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
998 static void icount_adjust_rt(void * opaque
)
1000 qemu_mod_timer(icount_rt_timer
,
1001 qemu_get_clock(rt_clock
) + 1000);
1005 static void icount_adjust_vm(void * opaque
)
1007 qemu_mod_timer(icount_vm_timer
,
1008 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1012 static void init_icount_adjust(void)
1014 /* Have both realtime and virtual time triggers for speed adjustment.
1015 The realtime trigger catches emulated time passing too slowly,
1016 the virtual time trigger catches emulated time passing too fast.
1017 Realtime triggers occur even when idle, so use them less frequently
1018 than VM triggers. */
1019 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1020 qemu_mod_timer(icount_rt_timer
,
1021 qemu_get_clock(rt_clock
) + 1000);
1022 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1023 qemu_mod_timer(icount_vm_timer
,
1024 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1027 static struct qemu_alarm_timer alarm_timers
[] = {
1030 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1031 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1032 /* HPET - if available - is preferred */
1033 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1034 /* ...otherwise try RTC */
1035 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1037 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1039 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1040 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1041 {"win32", 0, win32_start_timer
,
1042 win32_stop_timer
, NULL
, &alarm_win32_data
},
1047 static void show_available_alarms(void)
1051 printf("Available alarm timers, in order of precedence:\n");
1052 for (i
= 0; alarm_timers
[i
].name
; i
++)
1053 printf("%s\n", alarm_timers
[i
].name
);
1056 static void configure_alarms(char const *opt
)
1060 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1063 struct qemu_alarm_timer tmp
;
1065 if (!strcmp(opt
, "?")) {
1066 show_available_alarms();
1072 /* Reorder the array */
1073 name
= strtok(arg
, ",");
1075 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1076 if (!strcmp(alarm_timers
[i
].name
, name
))
1081 fprintf(stderr
, "Unknown clock %s\n", name
);
1090 tmp
= alarm_timers
[i
];
1091 alarm_timers
[i
] = alarm_timers
[cur
];
1092 alarm_timers
[cur
] = tmp
;
1096 name
= strtok(NULL
, ",");
1102 /* Disable remaining timers */
1103 for (i
= cur
; i
< count
; i
++)
1104 alarm_timers
[i
].name
= NULL
;
1106 show_available_alarms();
1111 QEMUClock
*rt_clock
;
1112 QEMUClock
*vm_clock
;
1114 static QEMUTimer
*active_timers
[2];
1116 static QEMUClock
*qemu_new_clock(int type
)
1119 clock
= qemu_mallocz(sizeof(QEMUClock
));
1124 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1128 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1131 ts
->opaque
= opaque
;
1135 void qemu_free_timer(QEMUTimer
*ts
)
1140 /* stop a timer, but do not dealloc it */
1141 void qemu_del_timer(QEMUTimer
*ts
)
1145 /* NOTE: this code must be signal safe because
1146 qemu_timer_expired() can be called from a signal. */
1147 pt
= &active_timers
[ts
->clock
->type
];
1160 /* modify the current timer so that it will be fired when current_time
1161 >= expire_time. The corresponding callback will be called. */
1162 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1168 /* add the timer in the sorted list */
1169 /* NOTE: this code must be signal safe because
1170 qemu_timer_expired() can be called from a signal. */
1171 pt
= &active_timers
[ts
->clock
->type
];
1176 if (t
->expire_time
> expire_time
)
1180 ts
->expire_time
= expire_time
;
1184 /* Rearm if necessary */
1185 if (pt
== &active_timers
[ts
->clock
->type
]) {
1186 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1187 qemu_rearm_alarm_timer(alarm_timer
);
1189 /* Interrupt execution to force deadline recalculation. */
1190 if (use_icount
&& cpu_single_env
) {
1191 cpu_exit(cpu_single_env
);
1196 int qemu_timer_pending(QEMUTimer
*ts
)
1199 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1206 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1210 return (timer_head
->expire_time
<= current_time
);
1213 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1219 if (!ts
|| ts
->expire_time
> current_time
)
1221 /* remove timer from the list before calling the callback */
1222 *ptimer_head
= ts
->next
;
1225 /* run the callback (the timer list can be modified) */
1230 int64_t qemu_get_clock(QEMUClock
*clock
)
1232 switch(clock
->type
) {
1233 case QEMU_TIMER_REALTIME
:
1234 return get_clock() / 1000000;
1236 case QEMU_TIMER_VIRTUAL
:
1238 return cpu_get_icount();
1240 return cpu_get_clock();
1245 static void init_timers(void)
1248 ticks_per_sec
= QEMU_TIMER_BASE
;
1249 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1250 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1254 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1256 uint64_t expire_time
;
1258 if (qemu_timer_pending(ts
)) {
1259 expire_time
= ts
->expire_time
;
1263 qemu_put_be64(f
, expire_time
);
1266 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1268 uint64_t expire_time
;
1270 expire_time
= qemu_get_be64(f
);
1271 if (expire_time
!= -1) {
1272 qemu_mod_timer(ts
, expire_time
);
1278 static void timer_save(QEMUFile
*f
, void *opaque
)
1280 if (cpu_ticks_enabled
) {
1281 hw_error("cannot save state if virtual timers are running");
1283 qemu_put_be64(f
, cpu_ticks_offset
);
1284 qemu_put_be64(f
, ticks_per_sec
);
1285 qemu_put_be64(f
, cpu_clock_offset
);
1288 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1290 if (version_id
!= 1 && version_id
!= 2)
1292 if (cpu_ticks_enabled
) {
1295 cpu_ticks_offset
=qemu_get_be64(f
);
1296 ticks_per_sec
=qemu_get_be64(f
);
1297 if (version_id
== 2) {
1298 cpu_clock_offset
=qemu_get_be64(f
);
1304 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1305 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1308 static void host_alarm_handler(int host_signum
)
1312 #define DISP_FREQ 1000
1314 static int64_t delta_min
= INT64_MAX
;
1315 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1317 ti
= qemu_get_clock(vm_clock
);
1318 if (last_clock
!= 0) {
1319 delta
= ti
- last_clock
;
1320 if (delta
< delta_min
)
1322 if (delta
> delta_max
)
1325 if (++count
== DISP_FREQ
) {
1326 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1327 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1328 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1329 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1330 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1332 delta_min
= INT64_MAX
;
1340 if (alarm_has_dynticks(alarm_timer
) ||
1342 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1343 qemu_get_clock(vm_clock
))) ||
1344 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1345 qemu_get_clock(rt_clock
))) {
1346 CPUState
*env
= next_cpu
;
1349 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1350 SetEvent(data
->host_alarm
);
1352 static const char byte
= 0;
1353 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1355 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1358 /* stop the currently executing cpu because a timer occured */
1361 if (env
->kqemu_enabled
) {
1362 kqemu_cpu_interrupt(env
);
1370 static int64_t qemu_next_deadline(void)
1374 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1375 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1376 qemu_get_clock(vm_clock
);
1378 /* To avoid problems with overflow limit this to 2^32. */
1388 #if defined(__linux__) || defined(_WIN32)
1389 static uint64_t qemu_next_deadline_dyntick(void)
1397 delta
= (qemu_next_deadline() + 999) / 1000;
1399 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1400 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1401 qemu_get_clock(rt_clock
))*1000;
1402 if (rtdelta
< delta
)
1406 if (delta
< MIN_TIMER_REARM_US
)
1407 delta
= MIN_TIMER_REARM_US
;
1415 /* Sets a specific flag */
1416 static int fcntl_setfl(int fd
, int flag
)
1420 flags
= fcntl(fd
, F_GETFL
);
1424 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1430 #if defined(__linux__)
1432 #define RTC_FREQ 1024
1434 static void enable_sigio_timer(int fd
)
1436 struct sigaction act
;
1439 sigfillset(&act
.sa_mask
);
1441 act
.sa_handler
= host_alarm_handler
;
1443 sigaction(SIGIO
, &act
, NULL
);
1444 fcntl_setfl(fd
, O_ASYNC
);
1445 fcntl(fd
, F_SETOWN
, getpid());
1448 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1450 struct hpet_info info
;
1453 fd
= open("/dev/hpet", O_RDONLY
);
1458 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1460 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1461 "error, but for better emulation accuracy type:\n"
1462 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1466 /* Check capabilities */
1467 r
= ioctl(fd
, HPET_INFO
, &info
);
1471 /* Enable periodic mode */
1472 r
= ioctl(fd
, HPET_EPI
, 0);
1473 if (info
.hi_flags
&& (r
< 0))
1476 /* Enable interrupt */
1477 r
= ioctl(fd
, HPET_IE_ON
, 0);
1481 enable_sigio_timer(fd
);
1482 t
->priv
= (void *)(long)fd
;
1490 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1492 int fd
= (long)t
->priv
;
1497 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1500 unsigned long current_rtc_freq
= 0;
1502 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1505 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1506 if (current_rtc_freq
!= RTC_FREQ
&&
1507 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1508 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1509 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1510 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1513 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1519 enable_sigio_timer(rtc_fd
);
1521 t
->priv
= (void *)(long)rtc_fd
;
1526 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1528 int rtc_fd
= (long)t
->priv
;
1533 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1537 struct sigaction act
;
1539 sigfillset(&act
.sa_mask
);
1541 act
.sa_handler
= host_alarm_handler
;
1543 sigaction(SIGALRM
, &act
, NULL
);
1545 ev
.sigev_value
.sival_int
= 0;
1546 ev
.sigev_notify
= SIGEV_SIGNAL
;
1547 ev
.sigev_signo
= SIGALRM
;
1549 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1550 perror("timer_create");
1552 /* disable dynticks */
1553 fprintf(stderr
, "Dynamic Ticks disabled\n");
1558 t
->priv
= (void *)(long)host_timer
;
1563 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1565 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1567 timer_delete(host_timer
);
1570 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1572 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1573 struct itimerspec timeout
;
1574 int64_t nearest_delta_us
= INT64_MAX
;
1577 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1578 !active_timers
[QEMU_TIMER_VIRTUAL
])
1581 nearest_delta_us
= qemu_next_deadline_dyntick();
1583 /* check whether a timer is already running */
1584 if (timer_gettime(host_timer
, &timeout
)) {
1586 fprintf(stderr
, "Internal timer error: aborting\n");
1589 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1590 if (current_us
&& current_us
<= nearest_delta_us
)
1593 timeout
.it_interval
.tv_sec
= 0;
1594 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1595 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1596 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1597 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1599 fprintf(stderr
, "Internal timer error: aborting\n");
1604 #endif /* defined(__linux__) */
1606 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1608 struct sigaction act
;
1609 struct itimerval itv
;
1613 sigfillset(&act
.sa_mask
);
1615 act
.sa_handler
= host_alarm_handler
;
1617 sigaction(SIGALRM
, &act
, NULL
);
1619 itv
.it_interval
.tv_sec
= 0;
1620 /* for i386 kernel 2.6 to get 1 ms */
1621 itv
.it_interval
.tv_usec
= 999;
1622 itv
.it_value
.tv_sec
= 0;
1623 itv
.it_value
.tv_usec
= 10 * 1000;
1625 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1632 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1634 struct itimerval itv
;
1636 memset(&itv
, 0, sizeof(itv
));
1637 setitimer(ITIMER_REAL
, &itv
, NULL
);
1640 #endif /* !defined(_WIN32) */
1642 static void try_to_rearm_timer(void *opaque
)
1644 struct qemu_alarm_timer
*t
= opaque
;
1648 /* Drain the notify pipe */
1651 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1652 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1655 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1656 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1657 qemu_rearm_alarm_timer(alarm_timer
);
1663 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1666 struct qemu_alarm_win32
*data
= t
->priv
;
1669 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1670 if (!data
->host_alarm
) {
1671 perror("Failed CreateEvent");
1675 memset(&tc
, 0, sizeof(tc
));
1676 timeGetDevCaps(&tc
, sizeof(tc
));
1678 if (data
->period
< tc
.wPeriodMin
)
1679 data
->period
= tc
.wPeriodMin
;
1681 timeBeginPeriod(data
->period
);
1683 flags
= TIME_CALLBACK_FUNCTION
;
1684 if (alarm_has_dynticks(t
))
1685 flags
|= TIME_ONESHOT
;
1687 flags
|= TIME_PERIODIC
;
1689 data
->timerId
= timeSetEvent(1, // interval (ms)
1690 data
->period
, // resolution
1691 host_alarm_handler
, // function
1692 (DWORD
)t
, // parameter
1695 if (!data
->timerId
) {
1696 perror("Failed to initialize win32 alarm timer");
1698 timeEndPeriod(data
->period
);
1699 CloseHandle(data
->host_alarm
);
1703 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1708 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1710 struct qemu_alarm_win32
*data
= t
->priv
;
1712 timeKillEvent(data
->timerId
);
1713 timeEndPeriod(data
->period
);
1715 CloseHandle(data
->host_alarm
);
1718 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1720 struct qemu_alarm_win32
*data
= t
->priv
;
1721 uint64_t nearest_delta_us
;
1723 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1724 !active_timers
[QEMU_TIMER_VIRTUAL
])
1727 nearest_delta_us
= qemu_next_deadline_dyntick();
1728 nearest_delta_us
/= 1000;
1730 timeKillEvent(data
->timerId
);
1732 data
->timerId
= timeSetEvent(1,
1736 TIME_ONESHOT
| TIME_PERIODIC
);
1738 if (!data
->timerId
) {
1739 perror("Failed to re-arm win32 alarm timer");
1741 timeEndPeriod(data
->period
);
1742 CloseHandle(data
->host_alarm
);
1749 static int init_timer_alarm(void)
1751 struct qemu_alarm_timer
*t
= NULL
;
1761 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1765 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1769 alarm_timer_rfd
= fds
[0];
1770 alarm_timer_wfd
= fds
[1];
1773 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1774 t
= &alarm_timers
[i
];
1787 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1788 try_to_rearm_timer
, NULL
, t
);
1803 static void quit_timers(void)
1805 alarm_timer
->stop(alarm_timer
);
1809 /***********************************************************/
1810 /* host time/date access */
1811 void qemu_get_timedate(struct tm
*tm
, int offset
)
1818 if (rtc_date_offset
== -1) {
1822 ret
= localtime(&ti
);
1824 ti
-= rtc_date_offset
;
1828 memcpy(tm
, ret
, sizeof(struct tm
));
1831 int qemu_timedate_diff(struct tm
*tm
)
1835 if (rtc_date_offset
== -1)
1837 seconds
= mktimegm(tm
);
1839 seconds
= mktime(tm
);
1841 seconds
= mktimegm(tm
) + rtc_date_offset
;
1843 return seconds
- time(NULL
);
1847 static void socket_cleanup(void)
1852 static int socket_init(void)
1857 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1859 err
= WSAGetLastError();
1860 fprintf(stderr
, "WSAStartup: %d\n", err
);
1863 atexit(socket_cleanup
);
1868 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1873 while (*p
!= '\0' && *p
!= '=') {
1874 if (q
&& (q
- buf
) < buf_size
- 1)
1884 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1889 while (*p
!= '\0') {
1891 if (*(p
+ 1) != ',')
1895 if (q
&& (q
- buf
) < buf_size
- 1)
1905 int get_param_value(char *buf
, int buf_size
,
1906 const char *tag
, const char *str
)
1913 p
= get_opt_name(option
, sizeof(option
), p
);
1917 if (!strcmp(tag
, option
)) {
1918 (void)get_opt_value(buf
, buf_size
, p
);
1921 p
= get_opt_value(NULL
, 0, p
);
1930 int check_params(char *buf
, int buf_size
,
1931 const char * const *params
, const char *str
)
1937 while (*p
!= '\0') {
1938 p
= get_opt_name(buf
, buf_size
, p
);
1942 for(i
= 0; params
[i
] != NULL
; i
++)
1943 if (!strcmp(params
[i
], buf
))
1945 if (params
[i
] == NULL
)
1947 p
= get_opt_value(NULL
, 0, p
);
1955 /***********************************************************/
1956 /* Bluetooth support */
1959 static struct HCIInfo
*hci_table
[MAX_NICS
];
1961 static struct bt_vlan_s
{
1962 struct bt_scatternet_s net
;
1964 struct bt_vlan_s
*next
;
1967 /* find or alloc a new bluetooth "VLAN" */
1968 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1970 struct bt_vlan_s
**pvlan
, *vlan
;
1971 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1975 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1977 pvlan
= &first_bt_vlan
;
1978 while (*pvlan
!= NULL
)
1979 pvlan
= &(*pvlan
)->next
;
1984 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1988 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1993 static struct HCIInfo null_hci
= {
1994 .cmd_send
= null_hci_send
,
1995 .sco_send
= null_hci_send
,
1996 .acl_send
= null_hci_send
,
1997 .bdaddr_set
= null_hci_addr_set
,
2000 struct HCIInfo
*qemu_next_hci(void)
2002 if (cur_hci
== nb_hcis
)
2005 return hci_table
[cur_hci
++];
2008 static struct HCIInfo
*hci_init(const char *str
)
2011 struct bt_scatternet_s
*vlan
= 0;
2013 if (!strcmp(str
, "null"))
2016 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2018 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2019 else if (!strncmp(str
, "hci", 3)) {
2022 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2023 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2028 vlan
= qemu_find_bt_vlan(0);
2030 return bt_new_hci(vlan
);
2033 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2038 static int bt_hci_parse(const char *str
)
2040 struct HCIInfo
*hci
;
2043 if (nb_hcis
>= MAX_NICS
) {
2044 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2048 hci
= hci_init(str
);
2057 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2058 hci
->bdaddr_set(hci
, bdaddr
.b
);
2060 hci_table
[nb_hcis
++] = hci
;
2065 static void bt_vhci_add(int vlan_id
)
2067 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2070 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2071 "an empty scatternet %i\n", vlan_id
);
2073 bt_vhci_init(bt_new_hci(vlan
));
2076 static struct bt_device_s
*bt_device_add(const char *opt
)
2078 struct bt_scatternet_s
*vlan
;
2080 char *endp
= strstr(opt
, ",vlan=");
2081 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2084 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2087 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2089 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2094 vlan
= qemu_find_bt_vlan(vlan_id
);
2097 fprintf(stderr
, "qemu: warning: adding a slave device to "
2098 "an empty scatternet %i\n", vlan_id
);
2100 if (!strcmp(devname
, "keyboard"))
2101 return bt_keyboard_init(vlan
);
2103 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2107 static int bt_parse(const char *opt
)
2109 const char *endp
, *p
;
2112 if (strstart(opt
, "hci", &endp
)) {
2113 if (!*endp
|| *endp
== ',') {
2115 if (!strstart(endp
, ",vlan=", 0))
2118 return bt_hci_parse(opt
);
2120 } else if (strstart(opt
, "vhci", &endp
)) {
2121 if (!*endp
|| *endp
== ',') {
2123 if (strstart(endp
, ",vlan=", &p
)) {
2124 vlan
= strtol(p
, (char **) &endp
, 0);
2126 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2130 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2139 } else if (strstart(opt
, "device:", &endp
))
2140 return !bt_device_add(endp
);
2142 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2146 /***********************************************************/
2147 /* QEMU Block devices */
2149 #define HD_ALIAS "index=%d,media=disk"
2150 #define CDROM_ALIAS "index=2,media=cdrom"
2151 #define FD_ALIAS "index=%d,if=floppy"
2152 #define PFLASH_ALIAS "if=pflash"
2153 #define MTD_ALIAS "if=mtd"
2154 #define SD_ALIAS "index=0,if=sd"
2156 static int drive_opt_get_free_idx(void)
2160 for (index
= 0; index
< MAX_DRIVES
; index
++)
2161 if (!drives_opt
[index
].used
) {
2162 drives_opt
[index
].used
= 1;
2169 static int drive_get_free_idx(void)
2173 for (index
= 0; index
< MAX_DRIVES
; index
++)
2174 if (!drives_table
[index
].used
) {
2175 drives_table
[index
].used
= 1;
2182 int drive_add(const char *file
, const char *fmt
, ...)
2185 int index
= drive_opt_get_free_idx();
2187 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2188 fprintf(stderr
, "qemu: too many drives\n");
2192 drives_opt
[index
].file
= file
;
2194 vsnprintf(drives_opt
[index
].opt
,
2195 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2202 void drive_remove(int index
)
2204 drives_opt
[index
].used
= 0;
2208 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2212 /* seek interface, bus and unit */
2214 for (index
= 0; index
< MAX_DRIVES
; index
++)
2215 if (drives_table
[index
].type
== type
&&
2216 drives_table
[index
].bus
== bus
&&
2217 drives_table
[index
].unit
== unit
&&
2218 drives_table
[index
].used
)
2224 int drive_get_max_bus(BlockInterfaceType type
)
2230 for (index
= 0; index
< nb_drives
; index
++) {
2231 if(drives_table
[index
].type
== type
&&
2232 drives_table
[index
].bus
> max_bus
)
2233 max_bus
= drives_table
[index
].bus
;
2238 const char *drive_get_serial(BlockDriverState
*bdrv
)
2242 for (index
= 0; index
< nb_drives
; index
++)
2243 if (drives_table
[index
].bdrv
== bdrv
)
2244 return drives_table
[index
].serial
;
2249 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2253 for (index
= 0; index
< nb_drives
; index
++)
2254 if (drives_table
[index
].bdrv
== bdrv
)
2255 return drives_table
[index
].onerror
;
2257 return BLOCK_ERR_STOP_ENOSPC
;
2260 static void bdrv_format_print(void *opaque
, const char *name
)
2262 fprintf(stderr
, " %s", name
);
2265 void drive_uninit(BlockDriverState
*bdrv
)
2269 for (i
= 0; i
< MAX_DRIVES
; i
++)
2270 if (drives_table
[i
].bdrv
== bdrv
) {
2271 drives_table
[i
].bdrv
= NULL
;
2272 drives_table
[i
].used
= 0;
2273 drive_remove(drives_table
[i
].drive_opt_idx
);
2279 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2285 const char *mediastr
= "";
2286 BlockInterfaceType type
;
2287 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2288 int bus_id
, unit_id
;
2289 int cyls
, heads
, secs
, translation
;
2290 BlockDriverState
*bdrv
;
2291 BlockDriver
*drv
= NULL
;
2292 QEMUMachine
*machine
= opaque
;
2296 int bdrv_flags
, onerror
;
2297 int drives_table_idx
;
2298 char *str
= arg
->opt
;
2299 static const char * const params
[] = { "bus", "unit", "if", "index",
2300 "cyls", "heads", "secs", "trans",
2301 "media", "snapshot", "file",
2302 "cache", "format", "serial", "werror",
2305 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2306 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2312 cyls
= heads
= secs
= 0;
2315 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2319 if (machine
->use_scsi
) {
2321 max_devs
= MAX_SCSI_DEVS
;
2322 pstrcpy(devname
, sizeof(devname
), "scsi");
2325 max_devs
= MAX_IDE_DEVS
;
2326 pstrcpy(devname
, sizeof(devname
), "ide");
2330 /* extract parameters */
2332 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2333 bus_id
= strtol(buf
, NULL
, 0);
2335 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2340 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2341 unit_id
= strtol(buf
, NULL
, 0);
2343 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2348 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2349 pstrcpy(devname
, sizeof(devname
), buf
);
2350 if (!strcmp(buf
, "ide")) {
2352 max_devs
= MAX_IDE_DEVS
;
2353 } else if (!strcmp(buf
, "scsi")) {
2355 max_devs
= MAX_SCSI_DEVS
;
2356 } else if (!strcmp(buf
, "floppy")) {
2359 } else if (!strcmp(buf
, "pflash")) {
2362 } else if (!strcmp(buf
, "mtd")) {
2365 } else if (!strcmp(buf
, "sd")) {
2368 } else if (!strcmp(buf
, "virtio")) {
2372 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2377 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2378 index
= strtol(buf
, NULL
, 0);
2380 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2385 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2386 cyls
= strtol(buf
, NULL
, 0);
2389 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2390 heads
= strtol(buf
, NULL
, 0);
2393 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2394 secs
= strtol(buf
, NULL
, 0);
2397 if (cyls
|| heads
|| secs
) {
2398 if (cyls
< 1 || cyls
> 16383) {
2399 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2402 if (heads
< 1 || heads
> 16) {
2403 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2406 if (secs
< 1 || secs
> 63) {
2407 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2412 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2415 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2419 if (!strcmp(buf
, "none"))
2420 translation
= BIOS_ATA_TRANSLATION_NONE
;
2421 else if (!strcmp(buf
, "lba"))
2422 translation
= BIOS_ATA_TRANSLATION_LBA
;
2423 else if (!strcmp(buf
, "auto"))
2424 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2426 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2431 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2432 if (!strcmp(buf
, "disk")) {
2434 } else if (!strcmp(buf
, "cdrom")) {
2435 if (cyls
|| secs
|| heads
) {
2437 "qemu: '%s' invalid physical CHS format\n", str
);
2440 media
= MEDIA_CDROM
;
2442 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2447 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2448 if (!strcmp(buf
, "on"))
2450 else if (!strcmp(buf
, "off"))
2453 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2458 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2459 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2461 else if (!strcmp(buf
, "writethrough"))
2463 else if (!strcmp(buf
, "writeback"))
2466 fprintf(stderr
, "qemu: invalid cache option\n");
2471 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2472 if (strcmp(buf
, "?") == 0) {
2473 fprintf(stderr
, "qemu: Supported formats:");
2474 bdrv_iterate_format(bdrv_format_print
, NULL
);
2475 fprintf(stderr
, "\n");
2478 drv
= bdrv_find_format(buf
);
2480 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2485 if (arg
->file
== NULL
)
2486 get_param_value(file
, sizeof(file
), "file", str
);
2488 pstrcpy(file
, sizeof(file
), arg
->file
);
2490 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2491 memset(serial
, 0, sizeof(serial
));
2493 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2494 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2495 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2496 fprintf(stderr
, "werror is no supported by this format\n");
2499 if (!strcmp(buf
, "ignore"))
2500 onerror
= BLOCK_ERR_IGNORE
;
2501 else if (!strcmp(buf
, "enospc"))
2502 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2503 else if (!strcmp(buf
, "stop"))
2504 onerror
= BLOCK_ERR_STOP_ANY
;
2505 else if (!strcmp(buf
, "report"))
2506 onerror
= BLOCK_ERR_REPORT
;
2508 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2513 /* compute bus and unit according index */
2516 if (bus_id
!= 0 || unit_id
!= -1) {
2518 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2526 unit_id
= index
% max_devs
;
2527 bus_id
= index
/ max_devs
;
2531 /* if user doesn't specify a unit_id,
2532 * try to find the first free
2535 if (unit_id
== -1) {
2537 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2539 if (max_devs
&& unit_id
>= max_devs
) {
2540 unit_id
-= max_devs
;
2548 if (max_devs
&& unit_id
>= max_devs
) {
2549 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2550 str
, unit_id
, max_devs
- 1);
2555 * ignore multiple definitions
2558 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2563 if (type
== IF_IDE
|| type
== IF_SCSI
)
2564 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2566 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2567 devname
, bus_id
, mediastr
, unit_id
);
2569 snprintf(buf
, sizeof(buf
), "%s%s%i",
2570 devname
, mediastr
, unit_id
);
2571 bdrv
= bdrv_new(buf
);
2572 drives_table_idx
= drive_get_free_idx();
2573 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2574 drives_table
[drives_table_idx
].type
= type
;
2575 drives_table
[drives_table_idx
].bus
= bus_id
;
2576 drives_table
[drives_table_idx
].unit
= unit_id
;
2577 drives_table
[drives_table_idx
].onerror
= onerror
;
2578 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2579 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2588 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2589 bdrv_set_translation_hint(bdrv
, translation
);
2593 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2598 /* FIXME: This isn't really a floppy, but it's a reasonable
2601 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2612 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2613 cache
= 2; /* always use write-back with snapshot */
2615 if (cache
== 0) /* no caching */
2616 bdrv_flags
|= BDRV_O_NOCACHE
;
2617 else if (cache
== 2) /* write-back */
2618 bdrv_flags
|= BDRV_O_CACHE_WB
;
2619 else if (cache
== 3) /* not specified */
2620 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2621 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2622 fprintf(stderr
, "qemu: could not open disk image %s\n",
2626 if (bdrv_key_required(bdrv
))
2628 return drives_table_idx
;
2631 /***********************************************************/
2634 static USBPort
*used_usb_ports
;
2635 static USBPort
*free_usb_ports
;
2637 /* ??? Maybe change this to register a hub to keep track of the topology. */
2638 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2639 usb_attachfn attach
)
2641 port
->opaque
= opaque
;
2642 port
->index
= index
;
2643 port
->attach
= attach
;
2644 port
->next
= free_usb_ports
;
2645 free_usb_ports
= port
;
2648 int usb_device_add_dev(USBDevice
*dev
)
2652 /* Find a USB port to add the device to. */
2653 port
= free_usb_ports
;
2657 /* Create a new hub and chain it on. */
2658 free_usb_ports
= NULL
;
2659 port
->next
= used_usb_ports
;
2660 used_usb_ports
= port
;
2662 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2663 usb_attach(port
, hub
);
2664 port
= free_usb_ports
;
2667 free_usb_ports
= port
->next
;
2668 port
->next
= used_usb_ports
;
2669 used_usb_ports
= port
;
2670 usb_attach(port
, dev
);
2674 static void usb_msd_password_cb(void *opaque
, int err
)
2676 USBDevice
*dev
= opaque
;
2679 usb_device_add_dev(dev
);
2681 dev
->handle_destroy(dev
);
2684 static int usb_device_add(const char *devname
, int is_hotplug
)
2689 if (!free_usb_ports
)
2692 if (strstart(devname
, "host:", &p
)) {
2693 dev
= usb_host_device_open(p
);
2694 } else if (!strcmp(devname
, "mouse")) {
2695 dev
= usb_mouse_init();
2696 } else if (!strcmp(devname
, "tablet")) {
2697 dev
= usb_tablet_init();
2698 } else if (!strcmp(devname
, "keyboard")) {
2699 dev
= usb_keyboard_init();
2700 } else if (strstart(devname
, "disk:", &p
)) {
2701 BlockDriverState
*bs
;
2703 dev
= usb_msd_init(p
);
2706 bs
= usb_msd_get_bdrv(dev
);
2707 if (bdrv_key_required(bs
)) {
2710 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2715 } else if (!strcmp(devname
, "wacom-tablet")) {
2716 dev
= usb_wacom_init();
2717 } else if (strstart(devname
, "serial:", &p
)) {
2718 dev
= usb_serial_init(p
);
2719 #ifdef CONFIG_BRLAPI
2720 } else if (!strcmp(devname
, "braille")) {
2721 dev
= usb_baum_init();
2723 } else if (strstart(devname
, "net:", &p
)) {
2726 if (net_client_init("nic", p
) < 0)
2728 nd_table
[nic
].model
= "usb";
2729 dev
= usb_net_init(&nd_table
[nic
]);
2730 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2731 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2732 bt_new_hci(qemu_find_bt_vlan(0)));
2739 return usb_device_add_dev(dev
);
2742 int usb_device_del_addr(int bus_num
, int addr
)
2748 if (!used_usb_ports
)
2754 lastp
= &used_usb_ports
;
2755 port
= used_usb_ports
;
2756 while (port
&& port
->dev
->addr
!= addr
) {
2757 lastp
= &port
->next
;
2765 *lastp
= port
->next
;
2766 usb_attach(port
, NULL
);
2767 dev
->handle_destroy(dev
);
2768 port
->next
= free_usb_ports
;
2769 free_usb_ports
= port
;
2773 static int usb_device_del(const char *devname
)
2778 if (strstart(devname
, "host:", &p
))
2779 return usb_host_device_close(p
);
2781 if (!used_usb_ports
)
2784 p
= strchr(devname
, '.');
2787 bus_num
= strtoul(devname
, NULL
, 0);
2788 addr
= strtoul(p
+ 1, NULL
, 0);
2790 return usb_device_del_addr(bus_num
, addr
);
2793 void do_usb_add(Monitor
*mon
, const char *devname
)
2795 usb_device_add(devname
, 1);
2798 void do_usb_del(Monitor
*mon
, const char *devname
)
2800 usb_device_del(devname
);
2803 void usb_info(Monitor
*mon
)
2807 const char *speed_str
;
2810 monitor_printf(mon
, "USB support not enabled\n");
2814 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2818 switch(dev
->speed
) {
2822 case USB_SPEED_FULL
:
2825 case USB_SPEED_HIGH
:
2832 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2833 0, dev
->addr
, speed_str
, dev
->devname
);
2837 /***********************************************************/
2838 /* PCMCIA/Cardbus */
2840 static struct pcmcia_socket_entry_s
{
2841 struct pcmcia_socket_s
*socket
;
2842 struct pcmcia_socket_entry_s
*next
;
2843 } *pcmcia_sockets
= 0;
2845 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2847 struct pcmcia_socket_entry_s
*entry
;
2849 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2850 entry
->socket
= socket
;
2851 entry
->next
= pcmcia_sockets
;
2852 pcmcia_sockets
= entry
;
2855 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2857 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2859 ptr
= &pcmcia_sockets
;
2860 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2861 if (entry
->socket
== socket
) {
2867 void pcmcia_info(Monitor
*mon
)
2869 struct pcmcia_socket_entry_s
*iter
;
2871 if (!pcmcia_sockets
)
2872 monitor_printf(mon
, "No PCMCIA sockets\n");
2874 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2875 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2876 iter
->socket
->attached
? iter
->socket
->card_string
:
2880 /***********************************************************/
2881 /* register display */
2883 struct DisplayAllocator default_allocator
= {
2884 defaultallocator_create_displaysurface
,
2885 defaultallocator_resize_displaysurface
,
2886 defaultallocator_free_displaysurface
2889 void register_displaystate(DisplayState
*ds
)
2899 DisplayState
*get_displaystate(void)
2901 return display_state
;
2904 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2906 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2907 return ds
->allocator
;
2912 static void dumb_display_init(void)
2914 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2915 ds
->allocator
= &default_allocator
;
2916 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2917 register_displaystate(ds
);
2920 /***********************************************************/
2923 typedef struct IOHandlerRecord
{
2925 IOCanRWHandler
*fd_read_poll
;
2927 IOHandler
*fd_write
;
2930 /* temporary data */
2932 struct IOHandlerRecord
*next
;
2935 static IOHandlerRecord
*first_io_handler
;
2937 /* XXX: fd_read_poll should be suppressed, but an API change is
2938 necessary in the character devices to suppress fd_can_read(). */
2939 int qemu_set_fd_handler2(int fd
,
2940 IOCanRWHandler
*fd_read_poll
,
2942 IOHandler
*fd_write
,
2945 IOHandlerRecord
**pioh
, *ioh
;
2947 if (!fd_read
&& !fd_write
) {
2948 pioh
= &first_io_handler
;
2953 if (ioh
->fd
== fd
) {
2960 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2964 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2965 ioh
->next
= first_io_handler
;
2966 first_io_handler
= ioh
;
2969 ioh
->fd_read_poll
= fd_read_poll
;
2970 ioh
->fd_read
= fd_read
;
2971 ioh
->fd_write
= fd_write
;
2972 ioh
->opaque
= opaque
;
2978 int qemu_set_fd_handler(int fd
,
2980 IOHandler
*fd_write
,
2983 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2987 /***********************************************************/
2988 /* Polling handling */
2990 typedef struct PollingEntry
{
2993 struct PollingEntry
*next
;
2996 static PollingEntry
*first_polling_entry
;
2998 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3000 PollingEntry
**ppe
, *pe
;
3001 pe
= qemu_mallocz(sizeof(PollingEntry
));
3003 pe
->opaque
= opaque
;
3004 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3009 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3011 PollingEntry
**ppe
, *pe
;
3012 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3014 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3022 /***********************************************************/
3023 /* Wait objects support */
3024 typedef struct WaitObjects
{
3026 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3027 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3028 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3031 static WaitObjects wait_objects
= {0};
3033 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3035 WaitObjects
*w
= &wait_objects
;
3037 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3039 w
->events
[w
->num
] = handle
;
3040 w
->func
[w
->num
] = func
;
3041 w
->opaque
[w
->num
] = opaque
;
3046 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3049 WaitObjects
*w
= &wait_objects
;
3052 for (i
= 0; i
< w
->num
; i
++) {
3053 if (w
->events
[i
] == handle
)
3056 w
->events
[i
] = w
->events
[i
+ 1];
3057 w
->func
[i
] = w
->func
[i
+ 1];
3058 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3066 /***********************************************************/
3067 /* ram save/restore */
3069 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3073 v
= qemu_get_byte(f
);
3076 if (qemu_get_buffer(f
, buf
, len
) != len
)
3080 v
= qemu_get_byte(f
);
3081 memset(buf
, v
, len
);
3087 if (qemu_file_has_error(f
))
3093 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3098 if (qemu_get_be32(f
) != last_ram_offset
)
3100 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
3101 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
3108 #define BDRV_HASH_BLOCK_SIZE 1024
3109 #define IOBUF_SIZE 4096
3110 #define RAM_CBLOCK_MAGIC 0xfabe
3112 typedef struct RamDecompressState
{
3115 uint8_t buf
[IOBUF_SIZE
];
3116 } RamDecompressState
;
3118 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3121 memset(s
, 0, sizeof(*s
));
3123 ret
= inflateInit(&s
->zstream
);
3129 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3133 s
->zstream
.avail_out
= len
;
3134 s
->zstream
.next_out
= buf
;
3135 while (s
->zstream
.avail_out
> 0) {
3136 if (s
->zstream
.avail_in
== 0) {
3137 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3139 clen
= qemu_get_be16(s
->f
);
3140 if (clen
> IOBUF_SIZE
)
3142 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3143 s
->zstream
.avail_in
= clen
;
3144 s
->zstream
.next_in
= s
->buf
;
3146 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3147 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3154 static void ram_decompress_close(RamDecompressState
*s
)
3156 inflateEnd(&s
->zstream
);
3159 #define RAM_SAVE_FLAG_FULL 0x01
3160 #define RAM_SAVE_FLAG_COMPRESS 0x02
3161 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3162 #define RAM_SAVE_FLAG_PAGE 0x08
3163 #define RAM_SAVE_FLAG_EOS 0x10
3165 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3167 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3168 uint32_t *array
= (uint32_t *)page
;
3171 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3172 if (array
[i
] != val
)
3179 static int ram_save_block(QEMUFile
*f
)
3181 static ram_addr_t current_addr
= 0;
3182 ram_addr_t saved_addr
= current_addr
;
3183 ram_addr_t addr
= 0;
3186 while (addr
< last_ram_offset
) {
3187 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3190 cpu_physical_memory_reset_dirty(current_addr
,
3191 current_addr
+ TARGET_PAGE_SIZE
,
3192 MIGRATION_DIRTY_FLAG
);
3194 p
= qemu_get_ram_ptr(current_addr
);
3196 if (is_dup_page(p
, *p
)) {
3197 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3198 qemu_put_byte(f
, *p
);
3200 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3201 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3207 addr
+= TARGET_PAGE_SIZE
;
3208 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3214 static ram_addr_t ram_save_threshold
= 10;
3216 static ram_addr_t
ram_save_remaining(void)
3219 ram_addr_t count
= 0;
3221 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3222 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3229 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3234 /* Make sure all dirty bits are set */
3235 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3236 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3237 cpu_physical_memory_set_dirty(addr
);
3240 /* Enable dirty memory tracking */
3241 cpu_physical_memory_set_dirty_tracking(1);
3243 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3246 while (!qemu_file_rate_limit(f
)) {
3249 ret
= ram_save_block(f
);
3250 if (ret
== 0) /* no more blocks */
3254 /* try transferring iterative blocks of memory */
3258 /* flush all remaining blocks regardless of rate limiting */
3259 while (ram_save_block(f
) != 0);
3260 cpu_physical_memory_set_dirty_tracking(0);
3263 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3265 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3268 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3270 RamDecompressState s1
, *s
= &s1
;
3274 if (ram_decompress_open(s
, f
) < 0)
3276 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3277 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3278 fprintf(stderr
, "Error while reading ram block header\n");
3282 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3283 BDRV_HASH_BLOCK_SIZE
) < 0) {
3284 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3289 printf("Error block header\n");
3293 ram_decompress_close(s
);
3298 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3303 if (version_id
== 1)
3304 return ram_load_v1(f
, opaque
);
3306 if (version_id
== 2) {
3307 if (qemu_get_be32(f
) != last_ram_offset
)
3309 return ram_load_dead(f
, opaque
);
3312 if (version_id
!= 3)
3316 addr
= qemu_get_be64(f
);
3318 flags
= addr
& ~TARGET_PAGE_MASK
;
3319 addr
&= TARGET_PAGE_MASK
;
3321 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3322 if (addr
!= last_ram_offset
)
3326 if (flags
& RAM_SAVE_FLAG_FULL
) {
3327 if (ram_load_dead(f
, opaque
) < 0)
3331 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3332 uint8_t ch
= qemu_get_byte(f
);
3333 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3334 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3335 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3336 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3341 void qemu_service_io(void)
3343 CPUState
*env
= cpu_single_env
;
3347 if (env
->kqemu_enabled
) {
3348 kqemu_cpu_interrupt(env
);
3354 /***********************************************************/
3355 /* bottom halves (can be seen as timers which expire ASAP) */
3366 static QEMUBH
*first_bh
= NULL
;
3368 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3371 bh
= qemu_mallocz(sizeof(QEMUBH
));
3373 bh
->opaque
= opaque
;
3374 bh
->next
= first_bh
;
3379 int qemu_bh_poll(void)
3385 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3386 if (!bh
->deleted
&& bh
->scheduled
) {
3395 /* remove deleted bhs */
3409 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3417 void qemu_bh_schedule(QEMUBH
*bh
)
3419 CPUState
*env
= cpu_single_env
;
3424 /* stop the currently executing CPU to execute the BH ASAP */
3430 void qemu_bh_cancel(QEMUBH
*bh
)
3435 void qemu_bh_delete(QEMUBH
*bh
)
3441 static void qemu_bh_update_timeout(int *timeout
)
3445 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3446 if (!bh
->deleted
&& bh
->scheduled
) {
3448 /* idle bottom halves will be polled at least
3450 *timeout
= MIN(10, *timeout
);
3452 /* non-idle bottom halves will be executed
3461 /***********************************************************/
3462 /* machine registration */
3464 static QEMUMachine
*first_machine
= NULL
;
3465 QEMUMachine
*current_machine
= NULL
;
3467 int qemu_register_machine(QEMUMachine
*m
)
3470 pm
= &first_machine
;
3478 static QEMUMachine
*find_machine(const char *name
)
3482 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3483 if (!strcmp(m
->name
, name
))
3489 /***********************************************************/
3490 /* main execution loop */
3492 static void gui_update(void *opaque
)
3494 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3495 DisplayState
*ds
= opaque
;
3496 DisplayChangeListener
*dcl
= ds
->listeners
;
3500 while (dcl
!= NULL
) {
3501 if (dcl
->gui_timer_interval
&&
3502 dcl
->gui_timer_interval
< interval
)
3503 interval
= dcl
->gui_timer_interval
;
3506 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3509 static void nographic_update(void *opaque
)
3511 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3513 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3516 struct vm_change_state_entry
{
3517 VMChangeStateHandler
*cb
;
3519 LIST_ENTRY (vm_change_state_entry
) entries
;
3522 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3524 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3527 VMChangeStateEntry
*e
;
3529 e
= qemu_mallocz(sizeof (*e
));
3533 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3537 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3539 LIST_REMOVE (e
, entries
);
3543 static void vm_state_notify(int running
, int reason
)
3545 VMChangeStateEntry
*e
;
3547 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3548 e
->cb(e
->opaque
, running
, reason
);
3557 vm_state_notify(1, 0);
3558 qemu_rearm_alarm_timer(alarm_timer
);
3562 void vm_stop(int reason
)
3565 cpu_disable_ticks();
3567 vm_state_notify(0, reason
);
3571 /* reset/shutdown handler */
3573 typedef struct QEMUResetEntry
{
3574 QEMUResetHandler
*func
;
3576 struct QEMUResetEntry
*next
;
3579 static QEMUResetEntry
*first_reset_entry
;
3580 static int reset_requested
;
3581 static int shutdown_requested
;
3582 static int powerdown_requested
;
3584 int qemu_shutdown_requested(void)
3586 int r
= shutdown_requested
;
3587 shutdown_requested
= 0;
3591 int qemu_reset_requested(void)
3593 int r
= reset_requested
;
3594 reset_requested
= 0;
3598 int qemu_powerdown_requested(void)
3600 int r
= powerdown_requested
;
3601 powerdown_requested
= 0;
3605 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3607 QEMUResetEntry
**pre
, *re
;
3609 pre
= &first_reset_entry
;
3610 while (*pre
!= NULL
)
3611 pre
= &(*pre
)->next
;
3612 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3614 re
->opaque
= opaque
;
3619 void qemu_system_reset(void)
3623 /* reset all devices */
3624 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3625 re
->func(re
->opaque
);
3631 void qemu_system_reset_request(void)
3634 shutdown_requested
= 1;
3636 reset_requested
= 1;
3639 cpu_exit(cpu_single_env
);
3642 void qemu_system_shutdown_request(void)
3644 shutdown_requested
= 1;
3646 cpu_exit(cpu_single_env
);
3649 void qemu_system_powerdown_request(void)
3651 powerdown_requested
= 1;
3653 cpu_exit(cpu_single_env
);
3657 static void host_main_loop_wait(int *timeout
)
3663 /* XXX: need to suppress polling by better using win32 events */
3665 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3666 ret
|= pe
->func(pe
->opaque
);
3670 WaitObjects
*w
= &wait_objects
;
3672 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3673 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3674 if (w
->func
[ret
- WAIT_OBJECT_0
])
3675 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3677 /* Check for additional signaled events */
3678 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3680 /* Check if event is signaled */
3681 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3682 if(ret2
== WAIT_OBJECT_0
) {
3684 w
->func
[i
](w
->opaque
[i
]);
3685 } else if (ret2
== WAIT_TIMEOUT
) {
3687 err
= GetLastError();
3688 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3691 } else if (ret
== WAIT_TIMEOUT
) {
3693 err
= GetLastError();
3694 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3701 static void host_main_loop_wait(int *timeout
)
3706 void main_loop_wait(int timeout
)
3708 IOHandlerRecord
*ioh
;
3709 fd_set rfds
, wfds
, xfds
;
3713 qemu_bh_update_timeout(&timeout
);
3715 host_main_loop_wait(&timeout
);
3717 /* poll any events */
3718 /* XXX: separate device handlers from system ones */
3723 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3727 (!ioh
->fd_read_poll
||
3728 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3729 FD_SET(ioh
->fd
, &rfds
);
3733 if (ioh
->fd_write
) {
3734 FD_SET(ioh
->fd
, &wfds
);
3740 tv
.tv_sec
= timeout
/ 1000;
3741 tv
.tv_usec
= (timeout
% 1000) * 1000;
3743 #if defined(CONFIG_SLIRP)
3744 if (slirp_is_inited()) {
3745 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3748 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3750 IOHandlerRecord
**pioh
;
3752 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3753 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3754 ioh
->fd_read(ioh
->opaque
);
3756 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3757 ioh
->fd_write(ioh
->opaque
);
3761 /* remove deleted IO handlers */
3762 pioh
= &first_io_handler
;
3772 #if defined(CONFIG_SLIRP)
3773 if (slirp_is_inited()) {
3779 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3783 /* vm time timers */
3784 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3785 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3786 qemu_get_clock(vm_clock
));
3788 /* real time timers */
3789 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3790 qemu_get_clock(rt_clock
));
3792 /* Check bottom-halves last in case any of the earlier events triggered
3798 static int main_loop(void)
3801 #ifdef CONFIG_PROFILER
3806 cur_cpu
= first_cpu
;
3807 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3814 #ifdef CONFIG_PROFILER
3815 ti
= profile_getclock();
3820 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3821 env
->icount_decr
.u16
.low
= 0;
3822 env
->icount_extra
= 0;
3823 count
= qemu_next_deadline();
3824 count
= (count
+ (1 << icount_time_shift
) - 1)
3825 >> icount_time_shift
;
3826 qemu_icount
+= count
;
3827 decr
= (count
> 0xffff) ? 0xffff : count
;
3829 env
->icount_decr
.u16
.low
= decr
;
3830 env
->icount_extra
= count
;
3832 ret
= cpu_exec(env
);
3833 #ifdef CONFIG_PROFILER
3834 qemu_time
+= profile_getclock() - ti
;
3837 /* Fold pending instructions back into the
3838 instruction counter, and clear the interrupt flag. */
3839 qemu_icount
-= (env
->icount_decr
.u16
.low
3840 + env
->icount_extra
);
3841 env
->icount_decr
.u32
= 0;
3842 env
->icount_extra
= 0;
3844 next_cpu
= env
->next_cpu
?: first_cpu
;
3845 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3846 ret
= EXCP_INTERRUPT
;
3850 if (ret
== EXCP_HLT
) {
3851 /* Give the next CPU a chance to run. */
3855 if (ret
!= EXCP_HALTED
)
3857 /* all CPUs are halted ? */
3863 if (shutdown_requested
) {
3864 ret
= EXCP_INTERRUPT
;
3872 if (reset_requested
) {
3873 reset_requested
= 0;
3874 qemu_system_reset();
3875 ret
= EXCP_INTERRUPT
;
3877 if (powerdown_requested
) {
3878 powerdown_requested
= 0;
3879 qemu_system_powerdown();
3880 ret
= EXCP_INTERRUPT
;
3882 if (unlikely(ret
== EXCP_DEBUG
)) {
3883 gdb_set_stop_cpu(cur_cpu
);
3884 vm_stop(EXCP_DEBUG
);
3886 /* If all cpus are halted then wait until the next IRQ */
3887 /* XXX: use timeout computed from timers */
3888 if (ret
== EXCP_HALTED
) {
3892 /* Advance virtual time to the next event. */
3893 if (use_icount
== 1) {
3894 /* When not using an adaptive execution frequency
3895 we tend to get badly out of sync with real time,
3896 so just delay for a reasonable amount of time. */
3899 delta
= cpu_get_icount() - cpu_get_clock();
3902 /* If virtual time is ahead of real time then just
3904 timeout
= (delta
/ 1000000) + 1;
3906 /* Wait for either IO to occur or the next
3908 add
= qemu_next_deadline();
3909 /* We advance the timer before checking for IO.
3910 Limit the amount we advance so that early IO
3911 activity won't get the guest too far ahead. */
3915 add
= (add
+ (1 << icount_time_shift
) - 1)
3916 >> icount_time_shift
;
3918 timeout
= delta
/ 1000000;
3929 if (shutdown_requested
) {
3930 ret
= EXCP_INTERRUPT
;
3935 #ifdef CONFIG_PROFILER
3936 ti
= profile_getclock();
3938 main_loop_wait(timeout
);
3939 #ifdef CONFIG_PROFILER
3940 dev_time
+= profile_getclock() - ti
;
3943 cpu_disable_ticks();
3947 static void version(void)
3949 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
3952 static void help(int exitcode
)
3955 printf("usage: %s [options] [disk_image]\n"
3957 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3959 #define DEF(option, opt_arg, opt_enum, opt_help) \
3961 #define DEFHEADING(text) stringify(text) "\n"
3962 #include "qemu-options.h"
3967 "During emulation, the following keys are useful:\n"
3968 "ctrl-alt-f toggle full screen\n"
3969 "ctrl-alt-n switch to virtual console 'n'\n"
3970 "ctrl-alt toggle mouse and keyboard grab\n"
3972 "When using -nographic, press 'ctrl-a h' to get some help.\n"
3977 DEFAULT_NETWORK_SCRIPT
,
3978 DEFAULT_NETWORK_DOWN_SCRIPT
,
3980 DEFAULT_GDBSTUB_PORT
,
3985 #define HAS_ARG 0x0001
3988 #define DEF(option, opt_arg, opt_enum, opt_help) \
3990 #define DEFHEADING(text)
3991 #include "qemu-options.h"
3997 typedef struct QEMUOption
{
4003 static const QEMUOption qemu_options
[] = {
4004 { "h", 0, QEMU_OPTION_h
},
4005 #define DEF(option, opt_arg, opt_enum, opt_help) \
4006 { option, opt_arg, opt_enum },
4007 #define DEFHEADING(text)
4008 #include "qemu-options.h"
4016 struct soundhw soundhw
[] = {
4017 #ifdef HAS_AUDIO_CHOICE
4018 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4024 { .init_isa
= pcspk_audio_init
}
4031 "Creative Sound Blaster 16",
4034 { .init_isa
= SB16_init
}
4038 #ifdef CONFIG_CS4231A
4044 { .init_isa
= cs4231a_init
}
4052 "Yamaha YMF262 (OPL3)",
4054 "Yamaha YM3812 (OPL2)",
4058 { .init_isa
= Adlib_init
}
4065 "Gravis Ultrasound GF1",
4068 { .init_isa
= GUS_init
}
4075 "Intel 82801AA AC97 Audio",
4078 { .init_pci
= ac97_init
}
4082 #ifdef CONFIG_ES1370
4085 "ENSONIQ AudioPCI ES1370",
4088 { .init_pci
= es1370_init
}
4092 #endif /* HAS_AUDIO_CHOICE */
4094 { NULL
, NULL
, 0, 0, { NULL
} }
4097 static void select_soundhw (const char *optarg
)
4101 if (*optarg
== '?') {
4104 printf ("Valid sound card names (comma separated):\n");
4105 for (c
= soundhw
; c
->name
; ++c
) {
4106 printf ("%-11s %s\n", c
->name
, c
->descr
);
4108 printf ("\n-soundhw all will enable all of the above\n");
4109 exit (*optarg
!= '?');
4117 if (!strcmp (optarg
, "all")) {
4118 for (c
= soundhw
; c
->name
; ++c
) {
4126 e
= strchr (p
, ',');
4127 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4129 for (c
= soundhw
; c
->name
; ++c
) {
4130 if (!strncmp (c
->name
, p
, l
)) {
4139 "Unknown sound card name (too big to show)\n");
4142 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4147 p
+= l
+ (e
!= NULL
);
4151 goto show_valid_cards
;
4156 static void select_vgahw (const char *p
)
4160 if (strstart(p
, "std", &opts
)) {
4161 std_vga_enabled
= 1;
4162 cirrus_vga_enabled
= 0;
4164 } else if (strstart(p
, "cirrus", &opts
)) {
4165 cirrus_vga_enabled
= 1;
4166 std_vga_enabled
= 0;
4168 } else if (strstart(p
, "vmware", &opts
)) {
4169 cirrus_vga_enabled
= 0;
4170 std_vga_enabled
= 0;
4172 } else if (strstart(p
, "none", &opts
)) {
4173 cirrus_vga_enabled
= 0;
4174 std_vga_enabled
= 0;
4178 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4182 const char *nextopt
;
4184 if (strstart(opts
, ",retrace=", &nextopt
)) {
4186 if (strstart(opts
, "dumb", &nextopt
))
4187 vga_retrace_method
= VGA_RETRACE_DUMB
;
4188 else if (strstart(opts
, "precise", &nextopt
))
4189 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4190 else goto invalid_vga
;
4191 } else goto invalid_vga
;
4197 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4199 exit(STATUS_CONTROL_C_EXIT
);
4204 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4208 if(strlen(str
) != 36)
4211 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4212 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4213 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4219 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4225 #define MAX_NET_CLIENTS 32
4229 static void termsig_handler(int signal
)
4231 qemu_system_shutdown_request();
4234 static void termsig_setup(void)
4236 struct sigaction act
;
4238 memset(&act
, 0, sizeof(act
));
4239 act
.sa_handler
= termsig_handler
;
4240 sigaction(SIGINT
, &act
, NULL
);
4241 sigaction(SIGHUP
, &act
, NULL
);
4242 sigaction(SIGTERM
, &act
, NULL
);
4247 int main(int argc
, char **argv
, char **envp
)
4249 #ifdef CONFIG_GDBSTUB
4250 const char *gdbstub_dev
= NULL
;
4252 uint32_t boot_devices_bitmap
= 0;
4254 int snapshot
, linux_boot
, net_boot
;
4255 const char *initrd_filename
;
4256 const char *kernel_filename
, *kernel_cmdline
;
4257 const char *boot_devices
= "";
4259 DisplayChangeListener
*dcl
;
4260 int cyls
, heads
, secs
, translation
;
4261 const char *net_clients
[MAX_NET_CLIENTS
];
4263 const char *bt_opts
[MAX_BT_CMDLINE
];
4267 const char *r
, *optarg
;
4268 CharDriverState
*monitor_hd
= NULL
;
4269 const char *monitor_device
;
4270 const char *serial_devices
[MAX_SERIAL_PORTS
];
4271 int serial_device_index
;
4272 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4273 int parallel_device_index
;
4274 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4275 int virtio_console_index
;
4276 const char *loadvm
= NULL
;
4277 QEMUMachine
*machine
;
4278 const char *cpu_model
;
4279 const char *usb_devices
[MAX_USB_CMDLINE
];
4280 int usb_devices_index
;
4285 const char *pid_file
= NULL
;
4286 const char *incoming
= NULL
;
4289 struct passwd
*pwd
= NULL
;
4290 const char *chroot_dir
= NULL
;
4291 const char *run_as
= NULL
;
4294 qemu_cache_utils_init(envp
);
4296 LIST_INIT (&vm_change_state_head
);
4299 struct sigaction act
;
4300 sigfillset(&act
.sa_mask
);
4302 act
.sa_handler
= SIG_IGN
;
4303 sigaction(SIGPIPE
, &act
, NULL
);
4306 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4307 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4308 QEMU to run on a single CPU */
4313 h
= GetCurrentProcess();
4314 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4315 for(i
= 0; i
< 32; i
++) {
4316 if (mask
& (1 << i
))
4321 SetProcessAffinityMask(h
, mask
);
4327 register_machines();
4328 machine
= first_machine
;
4330 initrd_filename
= NULL
;
4332 vga_ram_size
= VGA_RAM_SIZE
;
4336 kernel_filename
= NULL
;
4337 kernel_cmdline
= "";
4338 cyls
= heads
= secs
= 0;
4339 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4340 monitor_device
= "vc:80Cx24C";
4342 serial_devices
[0] = "vc:80Cx24C";
4343 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4344 serial_devices
[i
] = NULL
;
4345 serial_device_index
= 0;
4347 parallel_devices
[0] = "vc:80Cx24C";
4348 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4349 parallel_devices
[i
] = NULL
;
4350 parallel_device_index
= 0;
4352 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4353 virtio_consoles
[i
] = NULL
;
4354 virtio_console_index
= 0;
4356 usb_devices_index
= 0;
4375 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4377 const QEMUOption
*popt
;
4380 /* Treat --foo the same as -foo. */
4383 popt
= qemu_options
;
4386 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4390 if (!strcmp(popt
->name
, r
+ 1))
4394 if (popt
->flags
& HAS_ARG
) {
4395 if (optind
>= argc
) {
4396 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4400 optarg
= argv
[optind
++];
4405 switch(popt
->index
) {
4407 machine
= find_machine(optarg
);
4410 printf("Supported machines are:\n");
4411 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4412 printf("%-10s %s%s\n",
4414 m
== first_machine
? " (default)" : "");
4416 exit(*optarg
!= '?');
4419 case QEMU_OPTION_cpu
:
4420 /* hw initialization will check this */
4421 if (*optarg
== '?') {
4422 /* XXX: implement xxx_cpu_list for targets that still miss it */
4423 #if defined(cpu_list)
4424 cpu_list(stdout
, &fprintf
);
4431 case QEMU_OPTION_initrd
:
4432 initrd_filename
= optarg
;
4434 case QEMU_OPTION_hda
:
4436 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4438 hda_index
= drive_add(optarg
, HD_ALIAS
4439 ",cyls=%d,heads=%d,secs=%d%s",
4440 0, cyls
, heads
, secs
,
4441 translation
== BIOS_ATA_TRANSLATION_LBA
?
4443 translation
== BIOS_ATA_TRANSLATION_NONE
?
4444 ",trans=none" : "");
4446 case QEMU_OPTION_hdb
:
4447 case QEMU_OPTION_hdc
:
4448 case QEMU_OPTION_hdd
:
4449 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4451 case QEMU_OPTION_drive
:
4452 drive_add(NULL
, "%s", optarg
);
4454 case QEMU_OPTION_mtdblock
:
4455 drive_add(optarg
, MTD_ALIAS
);
4457 case QEMU_OPTION_sd
:
4458 drive_add(optarg
, SD_ALIAS
);
4460 case QEMU_OPTION_pflash
:
4461 drive_add(optarg
, PFLASH_ALIAS
);
4463 case QEMU_OPTION_snapshot
:
4466 case QEMU_OPTION_hdachs
:
4470 cyls
= strtol(p
, (char **)&p
, 0);
4471 if (cyls
< 1 || cyls
> 16383)
4476 heads
= strtol(p
, (char **)&p
, 0);
4477 if (heads
< 1 || heads
> 16)
4482 secs
= strtol(p
, (char **)&p
, 0);
4483 if (secs
< 1 || secs
> 63)
4487 if (!strcmp(p
, "none"))
4488 translation
= BIOS_ATA_TRANSLATION_NONE
;
4489 else if (!strcmp(p
, "lba"))
4490 translation
= BIOS_ATA_TRANSLATION_LBA
;
4491 else if (!strcmp(p
, "auto"))
4492 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4495 } else if (*p
!= '\0') {
4497 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4500 if (hda_index
!= -1)
4501 snprintf(drives_opt
[hda_index
].opt
,
4502 sizeof(drives_opt
[hda_index
].opt
),
4503 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4504 0, cyls
, heads
, secs
,
4505 translation
== BIOS_ATA_TRANSLATION_LBA
?
4507 translation
== BIOS_ATA_TRANSLATION_NONE
?
4508 ",trans=none" : "");
4511 case QEMU_OPTION_nographic
:
4514 #ifdef CONFIG_CURSES
4515 case QEMU_OPTION_curses
:
4519 case QEMU_OPTION_portrait
:
4522 case QEMU_OPTION_kernel
:
4523 kernel_filename
= optarg
;
4525 case QEMU_OPTION_append
:
4526 kernel_cmdline
= optarg
;
4528 case QEMU_OPTION_cdrom
:
4529 drive_add(optarg
, CDROM_ALIAS
);
4531 case QEMU_OPTION_boot
:
4532 boot_devices
= optarg
;
4533 /* We just do some generic consistency checks */
4535 /* Could easily be extended to 64 devices if needed */
4538 boot_devices_bitmap
= 0;
4539 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4540 /* Allowed boot devices are:
4541 * a b : floppy disk drives
4542 * c ... f : IDE disk drives
4543 * g ... m : machine implementation dependant drives
4544 * n ... p : network devices
4545 * It's up to each machine implementation to check
4546 * if the given boot devices match the actual hardware
4547 * implementation and firmware features.
4549 if (*p
< 'a' || *p
> 'q') {
4550 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4553 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4555 "Boot device '%c' was given twice\n",*p
);
4558 boot_devices_bitmap
|= 1 << (*p
- 'a');
4562 case QEMU_OPTION_fda
:
4563 case QEMU_OPTION_fdb
:
4564 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4567 case QEMU_OPTION_no_fd_bootchk
:
4571 case QEMU_OPTION_net
:
4572 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4573 fprintf(stderr
, "qemu: too many network clients\n");
4576 net_clients
[nb_net_clients
] = optarg
;
4580 case QEMU_OPTION_tftp
:
4581 tftp_prefix
= optarg
;
4583 case QEMU_OPTION_bootp
:
4584 bootp_filename
= optarg
;
4587 case QEMU_OPTION_smb
:
4588 net_slirp_smb(optarg
);
4591 case QEMU_OPTION_redir
:
4592 net_slirp_redir(NULL
, optarg
);
4595 case QEMU_OPTION_bt
:
4596 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4597 fprintf(stderr
, "qemu: too many bluetooth options\n");
4600 bt_opts
[nb_bt_opts
++] = optarg
;
4603 case QEMU_OPTION_audio_help
:
4607 case QEMU_OPTION_soundhw
:
4608 select_soundhw (optarg
);
4614 case QEMU_OPTION_version
:
4618 case QEMU_OPTION_m
: {
4622 value
= strtoul(optarg
, &ptr
, 10);
4624 case 0: case 'M': case 'm':
4631 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4635 /* On 32-bit hosts, QEMU is limited by virtual address space */
4636 if (value
> (2047 << 20)
4637 #ifndef CONFIG_KQEMU
4638 && HOST_LONG_BITS
== 32
4641 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4644 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4645 fprintf(stderr
, "qemu: ram size too large\n");
4654 const CPULogItem
*item
;
4656 mask
= cpu_str_to_log_mask(optarg
);
4658 printf("Log items (comma separated):\n");
4659 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4660 printf("%-10s %s\n", item
->name
, item
->help
);
4667 #ifdef CONFIG_GDBSTUB
4669 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4671 case QEMU_OPTION_gdb
:
4672 gdbstub_dev
= optarg
;
4678 case QEMU_OPTION_bios
:
4681 case QEMU_OPTION_singlestep
:
4689 keyboard_layout
= optarg
;
4692 case QEMU_OPTION_localtime
:
4695 case QEMU_OPTION_vga
:
4696 select_vgahw (optarg
);
4698 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4704 w
= strtol(p
, (char **)&p
, 10);
4707 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4713 h
= strtol(p
, (char **)&p
, 10);
4718 depth
= strtol(p
, (char **)&p
, 10);
4719 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4720 depth
!= 24 && depth
!= 32)
4722 } else if (*p
== '\0') {
4723 depth
= graphic_depth
;
4730 graphic_depth
= depth
;
4734 case QEMU_OPTION_echr
:
4737 term_escape_char
= strtol(optarg
, &r
, 0);
4739 printf("Bad argument to echr\n");
4742 case QEMU_OPTION_monitor
:
4743 monitor_device
= optarg
;
4745 case QEMU_OPTION_serial
:
4746 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4747 fprintf(stderr
, "qemu: too many serial ports\n");
4750 serial_devices
[serial_device_index
] = optarg
;
4751 serial_device_index
++;
4753 case QEMU_OPTION_virtiocon
:
4754 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
4755 fprintf(stderr
, "qemu: too many virtio consoles\n");
4758 virtio_consoles
[virtio_console_index
] = optarg
;
4759 virtio_console_index
++;
4761 case QEMU_OPTION_parallel
:
4762 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4763 fprintf(stderr
, "qemu: too many parallel ports\n");
4766 parallel_devices
[parallel_device_index
] = optarg
;
4767 parallel_device_index
++;
4769 case QEMU_OPTION_loadvm
:
4772 case QEMU_OPTION_full_screen
:
4776 case QEMU_OPTION_no_frame
:
4779 case QEMU_OPTION_alt_grab
:
4782 case QEMU_OPTION_no_quit
:
4785 case QEMU_OPTION_sdl
:
4789 case QEMU_OPTION_pidfile
:
4793 case QEMU_OPTION_win2k_hack
:
4794 win2k_install_hack
= 1;
4796 case QEMU_OPTION_rtc_td_hack
:
4799 case QEMU_OPTION_acpitable
:
4800 if(acpi_table_add(optarg
) < 0) {
4801 fprintf(stderr
, "Wrong acpi table provided\n");
4805 case QEMU_OPTION_smbios
:
4806 if(smbios_entry_add(optarg
) < 0) {
4807 fprintf(stderr
, "Wrong smbios provided\n");
4813 case QEMU_OPTION_no_kqemu
:
4816 case QEMU_OPTION_kernel_kqemu
:
4821 case QEMU_OPTION_enable_kvm
:
4828 case QEMU_OPTION_usb
:
4831 case QEMU_OPTION_usbdevice
:
4833 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
4834 fprintf(stderr
, "Too many USB devices\n");
4837 usb_devices
[usb_devices_index
] = optarg
;
4838 usb_devices_index
++;
4840 case QEMU_OPTION_smp
:
4841 smp_cpus
= atoi(optarg
);
4843 fprintf(stderr
, "Invalid number of CPUs\n");
4847 case QEMU_OPTION_vnc
:
4848 vnc_display
= optarg
;
4851 case QEMU_OPTION_no_acpi
:
4854 case QEMU_OPTION_no_hpet
:
4858 case QEMU_OPTION_no_reboot
:
4861 case QEMU_OPTION_no_shutdown
:
4864 case QEMU_OPTION_show_cursor
:
4867 case QEMU_OPTION_uuid
:
4868 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4869 fprintf(stderr
, "Fail to parse UUID string."
4870 " Wrong format.\n");
4875 case QEMU_OPTION_daemonize
:
4879 case QEMU_OPTION_option_rom
:
4880 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4881 fprintf(stderr
, "Too many option ROMs\n");
4884 option_rom
[nb_option_roms
] = optarg
;
4887 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4888 case QEMU_OPTION_semihosting
:
4889 semihosting_enabled
= 1;
4892 case QEMU_OPTION_name
:
4895 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4896 case QEMU_OPTION_prom_env
:
4897 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4898 fprintf(stderr
, "Too many prom variables\n");
4901 prom_envs
[nb_prom_envs
] = optarg
;
4906 case QEMU_OPTION_old_param
:
4910 case QEMU_OPTION_clock
:
4911 configure_alarms(optarg
);
4913 case QEMU_OPTION_startdate
:
4916 time_t rtc_start_date
;
4917 if (!strcmp(optarg
, "now")) {
4918 rtc_date_offset
= -1;
4920 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
4928 } else if (sscanf(optarg
, "%d-%d-%d",
4931 &tm
.tm_mday
) == 3) {
4940 rtc_start_date
= mktimegm(&tm
);
4941 if (rtc_start_date
== -1) {
4943 fprintf(stderr
, "Invalid date format. Valid format are:\n"
4944 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
4947 rtc_date_offset
= time(NULL
) - rtc_start_date
;
4951 case QEMU_OPTION_tb_size
:
4952 tb_size
= strtol(optarg
, NULL
, 0);
4956 case QEMU_OPTION_icount
:
4958 if (strcmp(optarg
, "auto") == 0) {
4959 icount_time_shift
= -1;
4961 icount_time_shift
= strtol(optarg
, NULL
, 0);
4964 case QEMU_OPTION_incoming
:
4968 case QEMU_OPTION_chroot
:
4969 chroot_dir
= optarg
;
4971 case QEMU_OPTION_runas
:
4979 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
4980 if (kvm_allowed
&& kqemu_allowed
) {
4982 "You can not enable both KVM and kqemu at the same time\n");
4987 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
4988 if (smp_cpus
> machine
->max_cpus
) {
4989 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
4990 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
4996 if (serial_device_index
== 0)
4997 serial_devices
[0] = "stdio";
4998 if (parallel_device_index
== 0)
4999 parallel_devices
[0] = "null";
5000 if (strncmp(monitor_device
, "vc", 2) == 0)
5001 monitor_device
= "stdio";
5008 if (pipe(fds
) == -1)
5019 len
= read(fds
[0], &status
, 1);
5020 if (len
== -1 && (errno
== EINTR
))
5025 else if (status
== 1) {
5026 fprintf(stderr
, "Could not acquire pidfile\n");
5043 signal(SIGTSTP
, SIG_IGN
);
5044 signal(SIGTTOU
, SIG_IGN
);
5045 signal(SIGTTIN
, SIG_IGN
);
5048 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5051 write(fds
[1], &status
, 1);
5053 fprintf(stderr
, "Could not acquire pid file\n");
5062 linux_boot
= (kernel_filename
!= NULL
);
5063 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5065 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5066 fprintf(stderr
, "-append only allowed with -kernel option\n");
5070 if (!linux_boot
&& initrd_filename
!= NULL
) {
5071 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5075 /* boot to floppy or the default cd if no hard disk defined yet */
5076 if (!boot_devices
[0]) {
5077 boot_devices
= "cad";
5079 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5082 if (init_timer_alarm() < 0) {
5083 fprintf(stderr
, "could not initialize alarm timer\n");
5086 if (use_icount
&& icount_time_shift
< 0) {
5088 /* 125MIPS seems a reasonable initial guess at the guest speed.
5089 It will be corrected fairly quickly anyway. */
5090 icount_time_shift
= 3;
5091 init_icount_adjust();
5098 /* init network clients */
5099 if (nb_net_clients
== 0) {
5100 /* if no clients, we use a default config */
5101 net_clients
[nb_net_clients
++] = "nic";
5103 net_clients
[nb_net_clients
++] = "user";
5107 for(i
= 0;i
< nb_net_clients
; i
++) {
5108 if (net_client_parse(net_clients
[i
]) < 0)
5114 /* XXX: this should be moved in the PC machine instantiation code */
5115 if (net_boot
!= 0) {
5117 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5118 const char *model
= nd_table
[i
].model
;
5120 if (net_boot
& (1 << i
)) {
5123 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5124 if (get_image_size(buf
) > 0) {
5125 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5126 fprintf(stderr
, "Too many option ROMs\n");
5129 option_rom
[nb_option_roms
] = strdup(buf
);
5136 fprintf(stderr
, "No valid PXE rom found for network device\n");
5142 /* init the bluetooth world */
5143 for (i
= 0; i
< nb_bt_opts
; i
++)
5144 if (bt_parse(bt_opts
[i
]))
5147 /* init the memory */
5149 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5152 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5153 guest ram allocation. It needs to go away. */
5154 if (kqemu_allowed
) {
5155 kqemu_phys_ram_size
= ram_size
+ VGA_RAM_SIZE
+ 4 * 1024 * 1024;
5156 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5157 if (!kqemu_phys_ram_base
) {
5158 fprintf(stderr
, "Could not allocate physical memory\n");
5164 /* init the dynamic translator */
5165 cpu_exec_init_all(tb_size
* 1024 * 1024);
5170 /* we always create the cdrom drive, even if no disk is there */
5172 if (nb_drives_opt
< MAX_DRIVES
)
5173 drive_add(NULL
, CDROM_ALIAS
);
5175 /* we always create at least one floppy */
5177 if (nb_drives_opt
< MAX_DRIVES
)
5178 drive_add(NULL
, FD_ALIAS
, 0);
5180 /* we always create one sd slot, even if no card is in it */
5182 if (nb_drives_opt
< MAX_DRIVES
)
5183 drive_add(NULL
, SD_ALIAS
);
5185 /* open the virtual block devices */
5187 for(i
= 0; i
< nb_drives_opt
; i
++)
5188 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5191 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5192 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5195 /* must be after terminal init, SDL library changes signal handlers */
5199 /* Maintain compatibility with multiple stdio monitors */
5200 if (!strcmp(monitor_device
,"stdio")) {
5201 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5202 const char *devname
= serial_devices
[i
];
5203 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5204 monitor_device
= NULL
;
5206 } else if (devname
&& !strcmp(devname
,"stdio")) {
5207 monitor_device
= NULL
;
5208 serial_devices
[i
] = "mon:stdio";
5214 if (kvm_enabled()) {
5217 ret
= kvm_init(smp_cpus
);
5219 fprintf(stderr
, "failed to initialize KVM\n");
5224 if (monitor_device
) {
5225 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5227 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5232 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5233 const char *devname
= serial_devices
[i
];
5234 if (devname
&& strcmp(devname
, "none")) {
5236 snprintf(label
, sizeof(label
), "serial%d", i
);
5237 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5238 if (!serial_hds
[i
]) {
5239 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5246 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5247 const char *devname
= parallel_devices
[i
];
5248 if (devname
&& strcmp(devname
, "none")) {
5250 snprintf(label
, sizeof(label
), "parallel%d", i
);
5251 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5252 if (!parallel_hds
[i
]) {
5253 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5260 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5261 const char *devname
= virtio_consoles
[i
];
5262 if (devname
&& strcmp(devname
, "none")) {
5264 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5265 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5266 if (!virtcon_hds
[i
]) {
5267 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5274 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5275 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5277 current_machine
= machine
;
5279 /* Set KVM's vcpu state to qemu's initial CPUState. */
5280 if (kvm_enabled()) {
5283 ret
= kvm_sync_vcpus();
5285 fprintf(stderr
, "failed to initialize vcpus\n");
5290 /* init USB devices */
5292 for(i
= 0; i
< usb_devices_index
; i
++) {
5293 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5294 fprintf(stderr
, "Warning: could not add USB device %s\n",
5301 dumb_display_init();
5302 /* just use the first displaystate for the moment */
5307 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5311 #if defined(CONFIG_CURSES)
5313 /* At the moment curses cannot be used with other displays */
5314 curses_display_init(ds
, full_screen
);
5318 if (vnc_display
!= NULL
) {
5319 vnc_display_init(ds
);
5320 if (vnc_display_open(ds
, vnc_display
) < 0)
5323 #if defined(CONFIG_SDL)
5324 if (sdl
|| !vnc_display
)
5325 sdl_display_init(ds
, full_screen
, no_frame
);
5326 #elif defined(CONFIG_COCOA)
5327 if (sdl
|| !vnc_display
)
5328 cocoa_display_init(ds
, full_screen
);
5334 dcl
= ds
->listeners
;
5335 while (dcl
!= NULL
) {
5336 if (dcl
->dpy_refresh
!= NULL
) {
5337 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5338 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5343 if (nographic
|| (vnc_display
&& !sdl
)) {
5344 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5345 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5348 text_consoles_set_display(display_state
);
5349 qemu_chr_initial_reset();
5351 if (monitor_device
&& monitor_hd
)
5352 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5354 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5355 const char *devname
= serial_devices
[i
];
5356 if (devname
&& strcmp(devname
, "none")) {
5358 snprintf(label
, sizeof(label
), "serial%d", i
);
5359 if (strstart(devname
, "vc", 0))
5360 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5364 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5365 const char *devname
= parallel_devices
[i
];
5366 if (devname
&& strcmp(devname
, "none")) {
5368 snprintf(label
, sizeof(label
), "parallel%d", i
);
5369 if (strstart(devname
, "vc", 0))
5370 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5374 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5375 const char *devname
= virtio_consoles
[i
];
5376 if (virtcon_hds
[i
] && devname
) {
5378 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5379 if (strstart(devname
, "vc", 0))
5380 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5384 #ifdef CONFIG_GDBSTUB
5385 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5386 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5393 do_loadvm(cur_mon
, loadvm
);
5396 autostart
= 0; /* fixme how to deal with -daemonize */
5397 qemu_start_incoming_migration(incoming
);
5409 len
= write(fds
[1], &status
, 1);
5410 if (len
== -1 && (errno
== EINTR
))
5417 TFR(fd
= open("/dev/null", O_RDWR
));
5423 pwd
= getpwnam(run_as
);
5425 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5431 if (chroot(chroot_dir
) < 0) {
5432 fprintf(stderr
, "chroot failed\n");
5439 if (setgid(pwd
->pw_gid
) < 0) {
5440 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5443 if (setuid(pwd
->pw_uid
) < 0) {
5444 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5447 if (setuid(0) != -1) {
5448 fprintf(stderr
, "Dropping privileges failed\n");