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"
147 #include "qemu-timer.h"
148 #include "qemu-char.h"
149 #include "cache-utils.h"
151 #include "audio/audio.h"
152 #include "migration.h"
158 #include "exec-all.h"
160 #include "qemu_socket.h"
162 #if defined(CONFIG_SLIRP)
163 #include "libslirp.h"
166 //#define DEBUG_UNUSED_IOPORT
167 //#define DEBUG_IOPORT
169 //#define DEBUG_SLIRP
173 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
175 # define LOG_IOPORT(...) do { } while (0)
178 #define DEFAULT_RAM_SIZE 128
180 /* Max number of USB devices that can be specified on the commandline. */
181 #define MAX_USB_CMDLINE 8
183 /* Max number of bluetooth switches on the commandline. */
184 #define MAX_BT_CMDLINE 10
186 /* XXX: use a two level table to limit memory usage */
187 #define MAX_IOPORTS 65536
189 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
190 const char *bios_name
= NULL
;
191 static void *ioport_opaque
[MAX_IOPORTS
];
192 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
193 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
194 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
195 to store the VM snapshots */
196 DriveInfo drives_table
[MAX_DRIVES
+1];
198 static int vga_ram_size
;
199 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
200 static DisplayState
*display_state
;
204 const char* keyboard_layout
= NULL
;
205 int64_t ticks_per_sec
;
208 NICInfo nd_table
[MAX_NICS
];
210 static int autostart
;
211 static int rtc_utc
= 1;
212 static int rtc_date_offset
= -1; /* -1 means no change */
213 int cirrus_vga_enabled
= 1;
214 int std_vga_enabled
= 0;
215 int vmsvga_enabled
= 0;
217 int graphic_width
= 1024;
218 int graphic_height
= 768;
219 int graphic_depth
= 8;
221 int graphic_width
= 800;
222 int graphic_height
= 600;
223 int graphic_depth
= 15;
225 static int full_screen
= 0;
227 static int no_frame
= 0;
230 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
231 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
232 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
234 int win2k_install_hack
= 0;
239 const char *vnc_display
;
240 int acpi_enabled
= 1;
246 int graphic_rotate
= 0;
248 const char *option_rom
[MAX_OPTION_ROMS
];
250 int semihosting_enabled
= 0;
254 const char *qemu_name
;
256 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
257 unsigned int nb_prom_envs
= 0;
258 const char *prom_envs
[MAX_PROM_ENVS
];
261 struct drive_opt drives_opt
[MAX_DRIVES
];
263 static CPUState
*cur_cpu
;
264 static CPUState
*next_cpu
;
265 static int event_pending
= 1;
266 /* Conversion factor from emulated instructions to virtual clock ticks. */
267 static int icount_time_shift
;
268 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
269 #define MAX_ICOUNT_SHIFT 10
270 /* Compensate for varying guest execution speed. */
271 static int64_t qemu_icount_bias
;
272 static QEMUTimer
*icount_rt_timer
;
273 static QEMUTimer
*icount_vm_timer
;
274 static QEMUTimer
*nographic_timer
;
276 uint8_t qemu_uuid
[16];
278 /***********************************************************/
279 /* x86 ISA bus support */
281 target_phys_addr_t isa_mem_base
= 0;
284 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
285 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
287 static uint32_t ioport_read(int index
, uint32_t address
)
289 static IOPortReadFunc
*default_func
[3] = {
290 default_ioport_readb
,
291 default_ioport_readw
,
294 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
296 func
= default_func
[index
];
297 return func(ioport_opaque
[address
], address
);
300 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
302 static IOPortWriteFunc
*default_func
[3] = {
303 default_ioport_writeb
,
304 default_ioport_writew
,
305 default_ioport_writel
307 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
309 func
= default_func
[index
];
310 func(ioport_opaque
[address
], address
, data
);
313 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
315 #ifdef DEBUG_UNUSED_IOPORT
316 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
321 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
323 #ifdef DEBUG_UNUSED_IOPORT
324 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
328 /* default is to make two byte accesses */
329 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
332 data
= ioport_read(0, address
);
333 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
334 data
|= ioport_read(0, address
) << 8;
338 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
340 ioport_write(0, address
, data
& 0xff);
341 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
342 ioport_write(0, address
, (data
>> 8) & 0xff);
345 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
347 #ifdef DEBUG_UNUSED_IOPORT
348 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
353 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
355 #ifdef DEBUG_UNUSED_IOPORT
356 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
360 /* size is the word size in byte */
361 int register_ioport_read(int start
, int length
, int size
,
362 IOPortReadFunc
*func
, void *opaque
)
368 } else if (size
== 2) {
370 } else if (size
== 4) {
373 hw_error("register_ioport_read: invalid size");
376 for(i
= start
; i
< start
+ length
; i
+= size
) {
377 ioport_read_table
[bsize
][i
] = func
;
378 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
379 hw_error("register_ioport_read: invalid opaque");
380 ioport_opaque
[i
] = opaque
;
385 /* size is the word size in byte */
386 int register_ioport_write(int start
, int length
, int size
,
387 IOPortWriteFunc
*func
, void *opaque
)
393 } else if (size
== 2) {
395 } else if (size
== 4) {
398 hw_error("register_ioport_write: invalid size");
401 for(i
= start
; i
< start
+ length
; i
+= size
) {
402 ioport_write_table
[bsize
][i
] = func
;
403 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
404 hw_error("register_ioport_write: invalid opaque");
405 ioport_opaque
[i
] = opaque
;
410 void isa_unassign_ioport(int start
, int length
)
414 for(i
= start
; i
< start
+ length
; i
++) {
415 ioport_read_table
[0][i
] = default_ioport_readb
;
416 ioport_read_table
[1][i
] = default_ioport_readw
;
417 ioport_read_table
[2][i
] = default_ioport_readl
;
419 ioport_write_table
[0][i
] = default_ioport_writeb
;
420 ioport_write_table
[1][i
] = default_ioport_writew
;
421 ioport_write_table
[2][i
] = default_ioport_writel
;
423 ioport_opaque
[i
] = NULL
;
427 /***********************************************************/
429 void cpu_outb(CPUState
*env
, int addr
, int val
)
431 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
432 ioport_write(0, addr
, val
);
435 env
->last_io_time
= cpu_get_time_fast();
439 void cpu_outw(CPUState
*env
, int addr
, int val
)
441 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
442 ioport_write(1, addr
, val
);
445 env
->last_io_time
= cpu_get_time_fast();
449 void cpu_outl(CPUState
*env
, int addr
, int val
)
451 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
452 ioport_write(2, addr
, val
);
455 env
->last_io_time
= cpu_get_time_fast();
459 int cpu_inb(CPUState
*env
, int addr
)
462 val
= ioport_read(0, addr
);
463 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
466 env
->last_io_time
= cpu_get_time_fast();
471 int cpu_inw(CPUState
*env
, int addr
)
474 val
= ioport_read(1, addr
);
475 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
478 env
->last_io_time
= cpu_get_time_fast();
483 int cpu_inl(CPUState
*env
, int addr
)
486 val
= ioport_read(2, addr
);
487 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
490 env
->last_io_time
= cpu_get_time_fast();
495 /***********************************************************/
496 void hw_error(const char *fmt
, ...)
502 fprintf(stderr
, "qemu: hardware error: ");
503 vfprintf(stderr
, fmt
, ap
);
504 fprintf(stderr
, "\n");
505 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
506 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
508 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
510 cpu_dump_state(env
, stderr
, fprintf
, 0);
520 static QEMUBalloonEvent
*qemu_balloon_event
;
521 void *qemu_balloon_event_opaque
;
523 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
525 qemu_balloon_event
= func
;
526 qemu_balloon_event_opaque
= opaque
;
529 void qemu_balloon(ram_addr_t target
)
531 if (qemu_balloon_event
)
532 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
535 ram_addr_t
qemu_balloon_status(void)
537 if (qemu_balloon_event
)
538 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
542 /***********************************************************/
545 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
546 static void *qemu_put_kbd_event_opaque
;
547 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
548 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
550 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
552 qemu_put_kbd_event_opaque
= opaque
;
553 qemu_put_kbd_event
= func
;
556 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
557 void *opaque
, int absolute
,
560 QEMUPutMouseEntry
*s
, *cursor
;
562 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
564 s
->qemu_put_mouse_event
= func
;
565 s
->qemu_put_mouse_event_opaque
= opaque
;
566 s
->qemu_put_mouse_event_absolute
= absolute
;
567 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
570 if (!qemu_put_mouse_event_head
) {
571 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
575 cursor
= qemu_put_mouse_event_head
;
576 while (cursor
->next
!= NULL
)
577 cursor
= cursor
->next
;
580 qemu_put_mouse_event_current
= s
;
585 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
587 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
589 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
592 cursor
= qemu_put_mouse_event_head
;
593 while (cursor
!= NULL
&& cursor
!= entry
) {
595 cursor
= cursor
->next
;
598 if (cursor
== NULL
) // does not exist or list empty
600 else if (prev
== NULL
) { // entry is head
601 qemu_put_mouse_event_head
= cursor
->next
;
602 if (qemu_put_mouse_event_current
== entry
)
603 qemu_put_mouse_event_current
= cursor
->next
;
604 qemu_free(entry
->qemu_put_mouse_event_name
);
609 prev
->next
= entry
->next
;
611 if (qemu_put_mouse_event_current
== entry
)
612 qemu_put_mouse_event_current
= prev
;
614 qemu_free(entry
->qemu_put_mouse_event_name
);
618 void kbd_put_keycode(int keycode
)
620 if (qemu_put_kbd_event
) {
621 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
625 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
627 QEMUPutMouseEvent
*mouse_event
;
628 void *mouse_event_opaque
;
631 if (!qemu_put_mouse_event_current
) {
636 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
638 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
641 if (graphic_rotate
) {
642 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
645 width
= graphic_width
- 1;
646 mouse_event(mouse_event_opaque
,
647 width
- dy
, dx
, dz
, buttons_state
);
649 mouse_event(mouse_event_opaque
,
650 dx
, dy
, dz
, buttons_state
);
654 int kbd_mouse_is_absolute(void)
656 if (!qemu_put_mouse_event_current
)
659 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
662 void do_info_mice(Monitor
*mon
)
664 QEMUPutMouseEntry
*cursor
;
667 if (!qemu_put_mouse_event_head
) {
668 monitor_printf(mon
, "No mouse devices connected\n");
672 monitor_printf(mon
, "Mouse devices available:\n");
673 cursor
= qemu_put_mouse_event_head
;
674 while (cursor
!= NULL
) {
675 monitor_printf(mon
, "%c Mouse #%d: %s\n",
676 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
677 index
, cursor
->qemu_put_mouse_event_name
);
679 cursor
= cursor
->next
;
683 void do_mouse_set(Monitor
*mon
, int index
)
685 QEMUPutMouseEntry
*cursor
;
688 if (!qemu_put_mouse_event_head
) {
689 monitor_printf(mon
, "No mouse devices connected\n");
693 cursor
= qemu_put_mouse_event_head
;
694 while (cursor
!= NULL
&& index
!= i
) {
696 cursor
= cursor
->next
;
700 qemu_put_mouse_event_current
= cursor
;
702 monitor_printf(mon
, "Mouse at given index not found\n");
705 /* compute with 96 bit intermediate result: (a*b)/c */
706 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
711 #ifdef WORDS_BIGENDIAN
721 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
722 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
725 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
729 /***********************************************************/
730 /* real time host monotonic timer */
732 #define QEMU_TIMER_BASE 1000000000LL
736 static int64_t clock_freq
;
738 static void init_get_clock(void)
742 ret
= QueryPerformanceFrequency(&freq
);
744 fprintf(stderr
, "Could not calibrate ticks\n");
747 clock_freq
= freq
.QuadPart
;
750 static int64_t get_clock(void)
753 QueryPerformanceCounter(&ti
);
754 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
759 static int use_rt_clock
;
761 static void init_get_clock(void)
764 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
765 || defined(__DragonFly__)
768 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
775 static int64_t get_clock(void)
777 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
778 || defined(__DragonFly__)
781 clock_gettime(CLOCK_MONOTONIC
, &ts
);
782 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
786 /* XXX: using gettimeofday leads to problems if the date
787 changes, so it should be avoided. */
789 gettimeofday(&tv
, NULL
);
790 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
795 /* Return the virtual CPU time, based on the instruction counter. */
796 static int64_t cpu_get_icount(void)
799 CPUState
*env
= cpu_single_env
;;
800 icount
= qemu_icount
;
803 fprintf(stderr
, "Bad clock read\n");
804 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
806 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
809 /***********************************************************/
810 /* guest cycle counter */
812 static int64_t cpu_ticks_prev
;
813 static int64_t cpu_ticks_offset
;
814 static int64_t cpu_clock_offset
;
815 static int cpu_ticks_enabled
;
817 /* return the host CPU cycle counter and handle stop/restart */
818 int64_t cpu_get_ticks(void)
821 return cpu_get_icount();
823 if (!cpu_ticks_enabled
) {
824 return cpu_ticks_offset
;
827 ticks
= cpu_get_real_ticks();
828 if (cpu_ticks_prev
> ticks
) {
829 /* Note: non increasing ticks may happen if the host uses
831 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
833 cpu_ticks_prev
= ticks
;
834 return ticks
+ cpu_ticks_offset
;
838 /* return the host CPU monotonic timer and handle stop/restart */
839 static int64_t cpu_get_clock(void)
842 if (!cpu_ticks_enabled
) {
843 return cpu_clock_offset
;
846 return ti
+ cpu_clock_offset
;
850 /* enable cpu_get_ticks() */
851 void cpu_enable_ticks(void)
853 if (!cpu_ticks_enabled
) {
854 cpu_ticks_offset
-= cpu_get_real_ticks();
855 cpu_clock_offset
-= get_clock();
856 cpu_ticks_enabled
= 1;
860 /* disable cpu_get_ticks() : the clock is stopped. You must not call
861 cpu_get_ticks() after that. */
862 void cpu_disable_ticks(void)
864 if (cpu_ticks_enabled
) {
865 cpu_ticks_offset
= cpu_get_ticks();
866 cpu_clock_offset
= cpu_get_clock();
867 cpu_ticks_enabled
= 0;
871 /***********************************************************/
874 #define QEMU_TIMER_REALTIME 0
875 #define QEMU_TIMER_VIRTUAL 1
879 /* XXX: add frequency */
887 struct QEMUTimer
*next
;
890 struct qemu_alarm_timer
{
894 int (*start
)(struct qemu_alarm_timer
*t
);
895 void (*stop
)(struct qemu_alarm_timer
*t
);
896 void (*rearm
)(struct qemu_alarm_timer
*t
);
900 #define ALARM_FLAG_DYNTICKS 0x1
901 #define ALARM_FLAG_EXPIRED 0x2
903 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
905 return t
->flags
& ALARM_FLAG_DYNTICKS
;
908 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
910 if (!alarm_has_dynticks(t
))
916 /* TODO: MIN_TIMER_REARM_US should be optimized */
917 #define MIN_TIMER_REARM_US 250
919 static struct qemu_alarm_timer
*alarm_timer
;
921 static int alarm_timer_rfd
, alarm_timer_wfd
;
926 struct qemu_alarm_win32
{
930 } alarm_win32_data
= {0, NULL
, -1};
932 static int win32_start_timer(struct qemu_alarm_timer
*t
);
933 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
934 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
938 static int unix_start_timer(struct qemu_alarm_timer
*t
);
939 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
943 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
944 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
945 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
947 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
948 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
950 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
951 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
953 #endif /* __linux__ */
957 /* Correlation between real and virtual time is always going to be
958 fairly approximate, so ignore small variation.
959 When the guest is idle real and virtual time will be aligned in
961 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
963 static void icount_adjust(void)
968 static int64_t last_delta
;
969 /* If the VM is not running, then do nothing. */
973 cur_time
= cpu_get_clock();
974 cur_icount
= qemu_get_clock(vm_clock
);
975 delta
= cur_icount
- cur_time
;
976 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
978 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
979 && icount_time_shift
> 0) {
980 /* The guest is getting too far ahead. Slow time down. */
984 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
985 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
986 /* The guest is getting too far behind. Speed time up. */
990 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
993 static void icount_adjust_rt(void * opaque
)
995 qemu_mod_timer(icount_rt_timer
,
996 qemu_get_clock(rt_clock
) + 1000);
1000 static void icount_adjust_vm(void * opaque
)
1002 qemu_mod_timer(icount_vm_timer
,
1003 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1007 static void init_icount_adjust(void)
1009 /* Have both realtime and virtual time triggers for speed adjustment.
1010 The realtime trigger catches emulated time passing too slowly,
1011 the virtual time trigger catches emulated time passing too fast.
1012 Realtime triggers occur even when idle, so use them less frequently
1013 than VM triggers. */
1014 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1015 qemu_mod_timer(icount_rt_timer
,
1016 qemu_get_clock(rt_clock
) + 1000);
1017 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1018 qemu_mod_timer(icount_vm_timer
,
1019 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1022 static struct qemu_alarm_timer alarm_timers
[] = {
1025 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1026 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1027 /* HPET - if available - is preferred */
1028 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1029 /* ...otherwise try RTC */
1030 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1032 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1034 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1035 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1036 {"win32", 0, win32_start_timer
,
1037 win32_stop_timer
, NULL
, &alarm_win32_data
},
1042 static void show_available_alarms(void)
1046 printf("Available alarm timers, in order of precedence:\n");
1047 for (i
= 0; alarm_timers
[i
].name
; i
++)
1048 printf("%s\n", alarm_timers
[i
].name
);
1051 static void configure_alarms(char const *opt
)
1055 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1058 struct qemu_alarm_timer tmp
;
1060 if (!strcmp(opt
, "?")) {
1061 show_available_alarms();
1067 /* Reorder the array */
1068 name
= strtok(arg
, ",");
1070 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1071 if (!strcmp(alarm_timers
[i
].name
, name
))
1076 fprintf(stderr
, "Unknown clock %s\n", name
);
1085 tmp
= alarm_timers
[i
];
1086 alarm_timers
[i
] = alarm_timers
[cur
];
1087 alarm_timers
[cur
] = tmp
;
1091 name
= strtok(NULL
, ",");
1097 /* Disable remaining timers */
1098 for (i
= cur
; i
< count
; i
++)
1099 alarm_timers
[i
].name
= NULL
;
1101 show_available_alarms();
1106 QEMUClock
*rt_clock
;
1107 QEMUClock
*vm_clock
;
1109 static QEMUTimer
*active_timers
[2];
1111 static QEMUClock
*qemu_new_clock(int type
)
1114 clock
= qemu_mallocz(sizeof(QEMUClock
));
1119 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1123 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1126 ts
->opaque
= opaque
;
1130 void qemu_free_timer(QEMUTimer
*ts
)
1135 /* stop a timer, but do not dealloc it */
1136 void qemu_del_timer(QEMUTimer
*ts
)
1140 /* NOTE: this code must be signal safe because
1141 qemu_timer_expired() can be called from a signal. */
1142 pt
= &active_timers
[ts
->clock
->type
];
1155 /* modify the current timer so that it will be fired when current_time
1156 >= expire_time. The corresponding callback will be called. */
1157 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1163 /* add the timer in the sorted list */
1164 /* NOTE: this code must be signal safe because
1165 qemu_timer_expired() can be called from a signal. */
1166 pt
= &active_timers
[ts
->clock
->type
];
1171 if (t
->expire_time
> expire_time
)
1175 ts
->expire_time
= expire_time
;
1179 /* Rearm if necessary */
1180 if (pt
== &active_timers
[ts
->clock
->type
]) {
1181 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1182 qemu_rearm_alarm_timer(alarm_timer
);
1184 /* Interrupt execution to force deadline recalculation. */
1185 if (use_icount
&& cpu_single_env
) {
1186 cpu_exit(cpu_single_env
);
1191 int qemu_timer_pending(QEMUTimer
*ts
)
1194 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1201 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1205 return (timer_head
->expire_time
<= current_time
);
1208 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1214 if (!ts
|| ts
->expire_time
> current_time
)
1216 /* remove timer from the list before calling the callback */
1217 *ptimer_head
= ts
->next
;
1220 /* run the callback (the timer list can be modified) */
1225 int64_t qemu_get_clock(QEMUClock
*clock
)
1227 switch(clock
->type
) {
1228 case QEMU_TIMER_REALTIME
:
1229 return get_clock() / 1000000;
1231 case QEMU_TIMER_VIRTUAL
:
1233 return cpu_get_icount();
1235 return cpu_get_clock();
1240 static void init_timers(void)
1243 ticks_per_sec
= QEMU_TIMER_BASE
;
1244 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1245 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1249 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1251 uint64_t expire_time
;
1253 if (qemu_timer_pending(ts
)) {
1254 expire_time
= ts
->expire_time
;
1258 qemu_put_be64(f
, expire_time
);
1261 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1263 uint64_t expire_time
;
1265 expire_time
= qemu_get_be64(f
);
1266 if (expire_time
!= -1) {
1267 qemu_mod_timer(ts
, expire_time
);
1273 static void timer_save(QEMUFile
*f
, void *opaque
)
1275 if (cpu_ticks_enabled
) {
1276 hw_error("cannot save state if virtual timers are running");
1278 qemu_put_be64(f
, cpu_ticks_offset
);
1279 qemu_put_be64(f
, ticks_per_sec
);
1280 qemu_put_be64(f
, cpu_clock_offset
);
1283 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1285 if (version_id
!= 1 && version_id
!= 2)
1287 if (cpu_ticks_enabled
) {
1290 cpu_ticks_offset
=qemu_get_be64(f
);
1291 ticks_per_sec
=qemu_get_be64(f
);
1292 if (version_id
== 2) {
1293 cpu_clock_offset
=qemu_get_be64(f
);
1299 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1300 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1302 static void host_alarm_handler(int host_signum
)
1306 #define DISP_FREQ 1000
1308 static int64_t delta_min
= INT64_MAX
;
1309 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1311 ti
= qemu_get_clock(vm_clock
);
1312 if (last_clock
!= 0) {
1313 delta
= ti
- last_clock
;
1314 if (delta
< delta_min
)
1316 if (delta
> delta_max
)
1319 if (++count
== DISP_FREQ
) {
1320 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1321 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1322 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1323 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1324 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1326 delta_min
= INT64_MAX
;
1334 if (alarm_has_dynticks(alarm_timer
) ||
1336 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1337 qemu_get_clock(vm_clock
))) ||
1338 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1339 qemu_get_clock(rt_clock
))) {
1340 CPUState
*env
= next_cpu
;
1343 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1344 SetEvent(data
->host_alarm
);
1346 static const char byte
= 0;
1347 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1349 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1352 /* stop the currently executing cpu because a timer occured */
1355 if (env
->kqemu_enabled
) {
1356 kqemu_cpu_interrupt(env
);
1364 static int64_t qemu_next_deadline(void)
1368 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1369 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1370 qemu_get_clock(vm_clock
);
1372 /* To avoid problems with overflow limit this to 2^32. */
1382 #if defined(__linux__) || defined(_WIN32)
1383 static uint64_t qemu_next_deadline_dyntick(void)
1391 delta
= (qemu_next_deadline() + 999) / 1000;
1393 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1394 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1395 qemu_get_clock(rt_clock
))*1000;
1396 if (rtdelta
< delta
)
1400 if (delta
< MIN_TIMER_REARM_US
)
1401 delta
= MIN_TIMER_REARM_US
;
1409 /* Sets a specific flag */
1410 static int fcntl_setfl(int fd
, int flag
)
1414 flags
= fcntl(fd
, F_GETFL
);
1418 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1424 #if defined(__linux__)
1426 #define RTC_FREQ 1024
1428 static void enable_sigio_timer(int fd
)
1430 struct sigaction act
;
1433 sigfillset(&act
.sa_mask
);
1435 act
.sa_handler
= host_alarm_handler
;
1437 sigaction(SIGIO
, &act
, NULL
);
1438 fcntl_setfl(fd
, O_ASYNC
);
1439 fcntl(fd
, F_SETOWN
, getpid());
1442 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1444 struct hpet_info info
;
1447 fd
= open("/dev/hpet", O_RDONLY
);
1452 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1454 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1455 "error, but for better emulation accuracy type:\n"
1456 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1460 /* Check capabilities */
1461 r
= ioctl(fd
, HPET_INFO
, &info
);
1465 /* Enable periodic mode */
1466 r
= ioctl(fd
, HPET_EPI
, 0);
1467 if (info
.hi_flags
&& (r
< 0))
1470 /* Enable interrupt */
1471 r
= ioctl(fd
, HPET_IE_ON
, 0);
1475 enable_sigio_timer(fd
);
1476 t
->priv
= (void *)(long)fd
;
1484 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1486 int fd
= (long)t
->priv
;
1491 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1494 unsigned long current_rtc_freq
= 0;
1496 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1499 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1500 if (current_rtc_freq
!= RTC_FREQ
&&
1501 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1502 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1503 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1504 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1507 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1513 enable_sigio_timer(rtc_fd
);
1515 t
->priv
= (void *)(long)rtc_fd
;
1520 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1522 int rtc_fd
= (long)t
->priv
;
1527 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1531 struct sigaction act
;
1533 sigfillset(&act
.sa_mask
);
1535 act
.sa_handler
= host_alarm_handler
;
1537 sigaction(SIGALRM
, &act
, NULL
);
1539 ev
.sigev_value
.sival_int
= 0;
1540 ev
.sigev_notify
= SIGEV_SIGNAL
;
1541 ev
.sigev_signo
= SIGALRM
;
1543 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1544 perror("timer_create");
1546 /* disable dynticks */
1547 fprintf(stderr
, "Dynamic Ticks disabled\n");
1552 t
->priv
= (void *)(long)host_timer
;
1557 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1559 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1561 timer_delete(host_timer
);
1564 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1566 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1567 struct itimerspec timeout
;
1568 int64_t nearest_delta_us
= INT64_MAX
;
1571 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1572 !active_timers
[QEMU_TIMER_VIRTUAL
])
1575 nearest_delta_us
= qemu_next_deadline_dyntick();
1577 /* check whether a timer is already running */
1578 if (timer_gettime(host_timer
, &timeout
)) {
1580 fprintf(stderr
, "Internal timer error: aborting\n");
1583 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1584 if (current_us
&& current_us
<= nearest_delta_us
)
1587 timeout
.it_interval
.tv_sec
= 0;
1588 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1589 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1590 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1591 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1593 fprintf(stderr
, "Internal timer error: aborting\n");
1598 #endif /* defined(__linux__) */
1600 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1602 struct sigaction act
;
1603 struct itimerval itv
;
1607 sigfillset(&act
.sa_mask
);
1609 act
.sa_handler
= host_alarm_handler
;
1611 sigaction(SIGALRM
, &act
, NULL
);
1613 itv
.it_interval
.tv_sec
= 0;
1614 /* for i386 kernel 2.6 to get 1 ms */
1615 itv
.it_interval
.tv_usec
= 999;
1616 itv
.it_value
.tv_sec
= 0;
1617 itv
.it_value
.tv_usec
= 10 * 1000;
1619 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1626 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1628 struct itimerval itv
;
1630 memset(&itv
, 0, sizeof(itv
));
1631 setitimer(ITIMER_REAL
, &itv
, NULL
);
1634 #endif /* !defined(_WIN32) */
1636 static void try_to_rearm_timer(void *opaque
)
1638 struct qemu_alarm_timer
*t
= opaque
;
1642 /* Drain the notify pipe */
1645 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1646 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1649 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1650 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1651 qemu_rearm_alarm_timer(alarm_timer
);
1657 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1660 struct qemu_alarm_win32
*data
= t
->priv
;
1663 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1664 if (!data
->host_alarm
) {
1665 perror("Failed CreateEvent");
1669 memset(&tc
, 0, sizeof(tc
));
1670 timeGetDevCaps(&tc
, sizeof(tc
));
1672 if (data
->period
< tc
.wPeriodMin
)
1673 data
->period
= tc
.wPeriodMin
;
1675 timeBeginPeriod(data
->period
);
1677 flags
= TIME_CALLBACK_FUNCTION
;
1678 if (alarm_has_dynticks(t
))
1679 flags
|= TIME_ONESHOT
;
1681 flags
|= TIME_PERIODIC
;
1683 data
->timerId
= timeSetEvent(1, // interval (ms)
1684 data
->period
, // resolution
1685 host_alarm_handler
, // function
1686 (DWORD
)t
, // parameter
1689 if (!data
->timerId
) {
1690 perror("Failed to initialize win32 alarm timer");
1692 timeEndPeriod(data
->period
);
1693 CloseHandle(data
->host_alarm
);
1697 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1702 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1704 struct qemu_alarm_win32
*data
= t
->priv
;
1706 timeKillEvent(data
->timerId
);
1707 timeEndPeriod(data
->period
);
1709 CloseHandle(data
->host_alarm
);
1712 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1714 struct qemu_alarm_win32
*data
= t
->priv
;
1715 uint64_t nearest_delta_us
;
1717 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1718 !active_timers
[QEMU_TIMER_VIRTUAL
])
1721 nearest_delta_us
= qemu_next_deadline_dyntick();
1722 nearest_delta_us
/= 1000;
1724 timeKillEvent(data
->timerId
);
1726 data
->timerId
= timeSetEvent(1,
1730 TIME_ONESHOT
| TIME_PERIODIC
);
1732 if (!data
->timerId
) {
1733 perror("Failed to re-arm win32 alarm timer");
1735 timeEndPeriod(data
->period
);
1736 CloseHandle(data
->host_alarm
);
1743 static int init_timer_alarm(void)
1745 struct qemu_alarm_timer
*t
= NULL
;
1755 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1759 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1763 alarm_timer_rfd
= fds
[0];
1764 alarm_timer_wfd
= fds
[1];
1767 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1768 t
= &alarm_timers
[i
];
1781 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1782 try_to_rearm_timer
, NULL
, t
);
1797 static void quit_timers(void)
1799 alarm_timer
->stop(alarm_timer
);
1803 /***********************************************************/
1804 /* host time/date access */
1805 void qemu_get_timedate(struct tm
*tm
, int offset
)
1812 if (rtc_date_offset
== -1) {
1816 ret
= localtime(&ti
);
1818 ti
-= rtc_date_offset
;
1822 memcpy(tm
, ret
, sizeof(struct tm
));
1825 int qemu_timedate_diff(struct tm
*tm
)
1829 if (rtc_date_offset
== -1)
1831 seconds
= mktimegm(tm
);
1833 seconds
= mktime(tm
);
1835 seconds
= mktimegm(tm
) + rtc_date_offset
;
1837 return seconds
- time(NULL
);
1841 static void socket_cleanup(void)
1846 static int socket_init(void)
1851 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1853 err
= WSAGetLastError();
1854 fprintf(stderr
, "WSAStartup: %d\n", err
);
1857 atexit(socket_cleanup
);
1862 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1867 while (*p
!= '\0' && *p
!= '=') {
1868 if (q
&& (q
- buf
) < buf_size
- 1)
1878 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1883 while (*p
!= '\0') {
1885 if (*(p
+ 1) != ',')
1889 if (q
&& (q
- buf
) < buf_size
- 1)
1899 int get_param_value(char *buf
, int buf_size
,
1900 const char *tag
, const char *str
)
1907 p
= get_opt_name(option
, sizeof(option
), p
);
1911 if (!strcmp(tag
, option
)) {
1912 (void)get_opt_value(buf
, buf_size
, p
);
1915 p
= get_opt_value(NULL
, 0, p
);
1924 int check_params(char *buf
, int buf_size
,
1925 const char * const *params
, const char *str
)
1932 p
= get_opt_name(buf
, buf_size
, p
);
1936 for(i
= 0; params
[i
] != NULL
; i
++)
1937 if (!strcmp(params
[i
], buf
))
1939 if (params
[i
] == NULL
)
1941 p
= get_opt_value(NULL
, 0, p
);
1949 /***********************************************************/
1950 /* Bluetooth support */
1953 static struct HCIInfo
*hci_table
[MAX_NICS
];
1955 static struct bt_vlan_s
{
1956 struct bt_scatternet_s net
;
1958 struct bt_vlan_s
*next
;
1961 /* find or alloc a new bluetooth "VLAN" */
1962 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1964 struct bt_vlan_s
**pvlan
, *vlan
;
1965 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1969 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1971 pvlan
= &first_bt_vlan
;
1972 while (*pvlan
!= NULL
)
1973 pvlan
= &(*pvlan
)->next
;
1978 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1982 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1987 static struct HCIInfo null_hci
= {
1988 .cmd_send
= null_hci_send
,
1989 .sco_send
= null_hci_send
,
1990 .acl_send
= null_hci_send
,
1991 .bdaddr_set
= null_hci_addr_set
,
1994 struct HCIInfo
*qemu_next_hci(void)
1996 if (cur_hci
== nb_hcis
)
1999 return hci_table
[cur_hci
++];
2002 static struct HCIInfo
*hci_init(const char *str
)
2005 struct bt_scatternet_s
*vlan
= 0;
2007 if (!strcmp(str
, "null"))
2010 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2012 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2013 else if (!strncmp(str
, "hci", 3)) {
2016 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2017 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2022 vlan
= qemu_find_bt_vlan(0);
2024 return bt_new_hci(vlan
);
2027 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2032 static int bt_hci_parse(const char *str
)
2034 struct HCIInfo
*hci
;
2037 if (nb_hcis
>= MAX_NICS
) {
2038 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2042 hci
= hci_init(str
);
2051 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2052 hci
->bdaddr_set(hci
, bdaddr
.b
);
2054 hci_table
[nb_hcis
++] = hci
;
2059 static void bt_vhci_add(int vlan_id
)
2061 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2064 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2065 "an empty scatternet %i\n", vlan_id
);
2067 bt_vhci_init(bt_new_hci(vlan
));
2070 static struct bt_device_s
*bt_device_add(const char *opt
)
2072 struct bt_scatternet_s
*vlan
;
2074 char *endp
= strstr(opt
, ",vlan=");
2075 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2078 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2081 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2083 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2088 vlan
= qemu_find_bt_vlan(vlan_id
);
2091 fprintf(stderr
, "qemu: warning: adding a slave device to "
2092 "an empty scatternet %i\n", vlan_id
);
2094 if (!strcmp(devname
, "keyboard"))
2095 return bt_keyboard_init(vlan
);
2097 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2101 static int bt_parse(const char *opt
)
2103 const char *endp
, *p
;
2106 if (strstart(opt
, "hci", &endp
)) {
2107 if (!*endp
|| *endp
== ',') {
2109 if (!strstart(endp
, ",vlan=", 0))
2112 return bt_hci_parse(opt
);
2114 } else if (strstart(opt
, "vhci", &endp
)) {
2115 if (!*endp
|| *endp
== ',') {
2117 if (strstart(endp
, ",vlan=", &p
)) {
2118 vlan
= strtol(p
, (char **) &endp
, 0);
2120 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2124 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2133 } else if (strstart(opt
, "device:", &endp
))
2134 return !bt_device_add(endp
);
2136 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2140 /***********************************************************/
2141 /* QEMU Block devices */
2143 #define HD_ALIAS "index=%d,media=disk"
2144 #define CDROM_ALIAS "index=2,media=cdrom"
2145 #define FD_ALIAS "index=%d,if=floppy"
2146 #define PFLASH_ALIAS "if=pflash"
2147 #define MTD_ALIAS "if=mtd"
2148 #define SD_ALIAS "index=0,if=sd"
2150 static int drive_opt_get_free_idx(void)
2154 for (index
= 0; index
< MAX_DRIVES
; index
++)
2155 if (!drives_opt
[index
].used
) {
2156 drives_opt
[index
].used
= 1;
2163 static int drive_get_free_idx(void)
2167 for (index
= 0; index
< MAX_DRIVES
; index
++)
2168 if (!drives_table
[index
].used
) {
2169 drives_table
[index
].used
= 1;
2176 int drive_add(const char *file
, const char *fmt
, ...)
2179 int index
= drive_opt_get_free_idx();
2181 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2182 fprintf(stderr
, "qemu: too many drives\n");
2186 drives_opt
[index
].file
= file
;
2188 vsnprintf(drives_opt
[index
].opt
,
2189 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2196 void drive_remove(int index
)
2198 drives_opt
[index
].used
= 0;
2202 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2206 /* seek interface, bus and unit */
2208 for (index
= 0; index
< MAX_DRIVES
; index
++)
2209 if (drives_table
[index
].type
== type
&&
2210 drives_table
[index
].bus
== bus
&&
2211 drives_table
[index
].unit
== unit
&&
2212 drives_table
[index
].used
)
2218 int drive_get_max_bus(BlockInterfaceType type
)
2224 for (index
= 0; index
< nb_drives
; index
++) {
2225 if(drives_table
[index
].type
== type
&&
2226 drives_table
[index
].bus
> max_bus
)
2227 max_bus
= drives_table
[index
].bus
;
2232 const char *drive_get_serial(BlockDriverState
*bdrv
)
2236 for (index
= 0; index
< nb_drives
; index
++)
2237 if (drives_table
[index
].bdrv
== bdrv
)
2238 return drives_table
[index
].serial
;
2243 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2247 for (index
= 0; index
< nb_drives
; index
++)
2248 if (drives_table
[index
].bdrv
== bdrv
)
2249 return drives_table
[index
].onerror
;
2251 return BLOCK_ERR_STOP_ENOSPC
;
2254 static void bdrv_format_print(void *opaque
, const char *name
)
2256 fprintf(stderr
, " %s", name
);
2259 void drive_uninit(BlockDriverState
*bdrv
)
2263 for (i
= 0; i
< MAX_DRIVES
; i
++)
2264 if (drives_table
[i
].bdrv
== bdrv
) {
2265 drives_table
[i
].bdrv
= NULL
;
2266 drives_table
[i
].used
= 0;
2267 drive_remove(drives_table
[i
].drive_opt_idx
);
2273 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2279 const char *mediastr
= "";
2280 BlockInterfaceType type
;
2281 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2282 int bus_id
, unit_id
;
2283 int cyls
, heads
, secs
, translation
;
2284 BlockDriverState
*bdrv
;
2285 BlockDriver
*drv
= NULL
;
2286 QEMUMachine
*machine
= opaque
;
2290 int bdrv_flags
, onerror
;
2291 int drives_table_idx
;
2292 char *str
= arg
->opt
;
2293 static const char * const params
[] = { "bus", "unit", "if", "index",
2294 "cyls", "heads", "secs", "trans",
2295 "media", "snapshot", "file",
2296 "cache", "format", "serial", "werror",
2299 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2300 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2306 cyls
= heads
= secs
= 0;
2309 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2313 if (machine
->use_scsi
) {
2315 max_devs
= MAX_SCSI_DEVS
;
2316 pstrcpy(devname
, sizeof(devname
), "scsi");
2319 max_devs
= MAX_IDE_DEVS
;
2320 pstrcpy(devname
, sizeof(devname
), "ide");
2324 /* extract parameters */
2326 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2327 bus_id
= strtol(buf
, NULL
, 0);
2329 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2334 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2335 unit_id
= strtol(buf
, NULL
, 0);
2337 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2342 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2343 pstrcpy(devname
, sizeof(devname
), buf
);
2344 if (!strcmp(buf
, "ide")) {
2346 max_devs
= MAX_IDE_DEVS
;
2347 } else if (!strcmp(buf
, "scsi")) {
2349 max_devs
= MAX_SCSI_DEVS
;
2350 } else if (!strcmp(buf
, "floppy")) {
2353 } else if (!strcmp(buf
, "pflash")) {
2356 } else if (!strcmp(buf
, "mtd")) {
2359 } else if (!strcmp(buf
, "sd")) {
2362 } else if (!strcmp(buf
, "virtio")) {
2366 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2371 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2372 index
= strtol(buf
, NULL
, 0);
2374 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2379 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2380 cyls
= strtol(buf
, NULL
, 0);
2383 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2384 heads
= strtol(buf
, NULL
, 0);
2387 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2388 secs
= strtol(buf
, NULL
, 0);
2391 if (cyls
|| heads
|| secs
) {
2392 if (cyls
< 1 || cyls
> 16383) {
2393 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2396 if (heads
< 1 || heads
> 16) {
2397 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2400 if (secs
< 1 || secs
> 63) {
2401 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2406 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2409 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2413 if (!strcmp(buf
, "none"))
2414 translation
= BIOS_ATA_TRANSLATION_NONE
;
2415 else if (!strcmp(buf
, "lba"))
2416 translation
= BIOS_ATA_TRANSLATION_LBA
;
2417 else if (!strcmp(buf
, "auto"))
2418 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2420 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2425 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2426 if (!strcmp(buf
, "disk")) {
2428 } else if (!strcmp(buf
, "cdrom")) {
2429 if (cyls
|| secs
|| heads
) {
2431 "qemu: '%s' invalid physical CHS format\n", str
);
2434 media
= MEDIA_CDROM
;
2436 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2441 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2442 if (!strcmp(buf
, "on"))
2444 else if (!strcmp(buf
, "off"))
2447 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2452 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2453 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2455 else if (!strcmp(buf
, "writethrough"))
2457 else if (!strcmp(buf
, "writeback"))
2460 fprintf(stderr
, "qemu: invalid cache option\n");
2465 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2466 if (strcmp(buf
, "?") == 0) {
2467 fprintf(stderr
, "qemu: Supported formats:");
2468 bdrv_iterate_format(bdrv_format_print
, NULL
);
2469 fprintf(stderr
, "\n");
2472 drv
= bdrv_find_format(buf
);
2474 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2479 if (arg
->file
== NULL
)
2480 get_param_value(file
, sizeof(file
), "file", str
);
2482 pstrcpy(file
, sizeof(file
), arg
->file
);
2484 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2485 memset(serial
, 0, sizeof(serial
));
2487 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2488 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2489 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2490 fprintf(stderr
, "werror is no supported by this format\n");
2493 if (!strcmp(buf
, "ignore"))
2494 onerror
= BLOCK_ERR_IGNORE
;
2495 else if (!strcmp(buf
, "enospc"))
2496 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2497 else if (!strcmp(buf
, "stop"))
2498 onerror
= BLOCK_ERR_STOP_ANY
;
2499 else if (!strcmp(buf
, "report"))
2500 onerror
= BLOCK_ERR_REPORT
;
2502 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2507 /* compute bus and unit according index */
2510 if (bus_id
!= 0 || unit_id
!= -1) {
2512 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2520 unit_id
= index
% max_devs
;
2521 bus_id
= index
/ max_devs
;
2525 /* if user doesn't specify a unit_id,
2526 * try to find the first free
2529 if (unit_id
== -1) {
2531 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2533 if (max_devs
&& unit_id
>= max_devs
) {
2534 unit_id
-= max_devs
;
2542 if (max_devs
&& unit_id
>= max_devs
) {
2543 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2544 str
, unit_id
, max_devs
- 1);
2549 * ignore multiple definitions
2552 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2557 if (type
== IF_IDE
|| type
== IF_SCSI
)
2558 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2560 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2561 devname
, bus_id
, mediastr
, unit_id
);
2563 snprintf(buf
, sizeof(buf
), "%s%s%i",
2564 devname
, mediastr
, unit_id
);
2565 bdrv
= bdrv_new(buf
);
2566 drives_table_idx
= drive_get_free_idx();
2567 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2568 drives_table
[drives_table_idx
].type
= type
;
2569 drives_table
[drives_table_idx
].bus
= bus_id
;
2570 drives_table
[drives_table_idx
].unit
= unit_id
;
2571 drives_table
[drives_table_idx
].onerror
= onerror
;
2572 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2573 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2582 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2583 bdrv_set_translation_hint(bdrv
, translation
);
2587 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2592 /* FIXME: This isn't really a floppy, but it's a reasonable
2595 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2606 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2607 cache
= 2; /* always use write-back with snapshot */
2609 if (cache
== 0) /* no caching */
2610 bdrv_flags
|= BDRV_O_NOCACHE
;
2611 else if (cache
== 2) /* write-back */
2612 bdrv_flags
|= BDRV_O_CACHE_WB
;
2613 else if (cache
== 3) /* not specified */
2614 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2615 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2616 fprintf(stderr
, "qemu: could not open disk image %s\n",
2620 if (bdrv_key_required(bdrv
))
2622 return drives_table_idx
;
2625 /***********************************************************/
2628 static USBPort
*used_usb_ports
;
2629 static USBPort
*free_usb_ports
;
2631 /* ??? Maybe change this to register a hub to keep track of the topology. */
2632 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2633 usb_attachfn attach
)
2635 port
->opaque
= opaque
;
2636 port
->index
= index
;
2637 port
->attach
= attach
;
2638 port
->next
= free_usb_ports
;
2639 free_usb_ports
= port
;
2642 int usb_device_add_dev(USBDevice
*dev
)
2646 /* Find a USB port to add the device to. */
2647 port
= free_usb_ports
;
2651 /* Create a new hub and chain it on. */
2652 free_usb_ports
= NULL
;
2653 port
->next
= used_usb_ports
;
2654 used_usb_ports
= port
;
2656 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2657 usb_attach(port
, hub
);
2658 port
= free_usb_ports
;
2661 free_usb_ports
= port
->next
;
2662 port
->next
= used_usb_ports
;
2663 used_usb_ports
= port
;
2664 usb_attach(port
, dev
);
2668 static void usb_msd_password_cb(void *opaque
, int err
)
2670 USBDevice
*dev
= opaque
;
2673 usb_device_add_dev(dev
);
2675 dev
->handle_destroy(dev
);
2678 static int usb_device_add(const char *devname
, int is_hotplug
)
2683 if (!free_usb_ports
)
2686 if (strstart(devname
, "host:", &p
)) {
2687 dev
= usb_host_device_open(p
);
2688 } else if (!strcmp(devname
, "mouse")) {
2689 dev
= usb_mouse_init();
2690 } else if (!strcmp(devname
, "tablet")) {
2691 dev
= usb_tablet_init();
2692 } else if (!strcmp(devname
, "keyboard")) {
2693 dev
= usb_keyboard_init();
2694 } else if (strstart(devname
, "disk:", &p
)) {
2695 BlockDriverState
*bs
;
2697 dev
= usb_msd_init(p
);
2700 bs
= usb_msd_get_bdrv(dev
);
2701 if (bdrv_key_required(bs
)) {
2704 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2709 } else if (!strcmp(devname
, "wacom-tablet")) {
2710 dev
= usb_wacom_init();
2711 } else if (strstart(devname
, "serial:", &p
)) {
2712 dev
= usb_serial_init(p
);
2713 #ifdef CONFIG_BRLAPI
2714 } else if (!strcmp(devname
, "braille")) {
2715 dev
= usb_baum_init();
2717 } else if (strstart(devname
, "net:", &p
)) {
2720 if (net_client_init("nic", p
) < 0)
2722 nd_table
[nic
].model
= "usb";
2723 dev
= usb_net_init(&nd_table
[nic
]);
2724 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2725 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2726 bt_new_hci(qemu_find_bt_vlan(0)));
2733 return usb_device_add_dev(dev
);
2736 int usb_device_del_addr(int bus_num
, int addr
)
2742 if (!used_usb_ports
)
2748 lastp
= &used_usb_ports
;
2749 port
= used_usb_ports
;
2750 while (port
&& port
->dev
->addr
!= addr
) {
2751 lastp
= &port
->next
;
2759 *lastp
= port
->next
;
2760 usb_attach(port
, NULL
);
2761 dev
->handle_destroy(dev
);
2762 port
->next
= free_usb_ports
;
2763 free_usb_ports
= port
;
2767 static int usb_device_del(const char *devname
)
2772 if (strstart(devname
, "host:", &p
))
2773 return usb_host_device_close(p
);
2775 if (!used_usb_ports
)
2778 p
= strchr(devname
, '.');
2781 bus_num
= strtoul(devname
, NULL
, 0);
2782 addr
= strtoul(p
+ 1, NULL
, 0);
2784 return usb_device_del_addr(bus_num
, addr
);
2787 void do_usb_add(Monitor
*mon
, const char *devname
)
2789 usb_device_add(devname
, 1);
2792 void do_usb_del(Monitor
*mon
, const char *devname
)
2794 usb_device_del(devname
);
2797 void usb_info(Monitor
*mon
)
2801 const char *speed_str
;
2804 monitor_printf(mon
, "USB support not enabled\n");
2808 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2812 switch(dev
->speed
) {
2816 case USB_SPEED_FULL
:
2819 case USB_SPEED_HIGH
:
2826 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2827 0, dev
->addr
, speed_str
, dev
->devname
);
2831 /***********************************************************/
2832 /* PCMCIA/Cardbus */
2834 static struct pcmcia_socket_entry_s
{
2835 struct pcmcia_socket_s
*socket
;
2836 struct pcmcia_socket_entry_s
*next
;
2837 } *pcmcia_sockets
= 0;
2839 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2841 struct pcmcia_socket_entry_s
*entry
;
2843 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2844 entry
->socket
= socket
;
2845 entry
->next
= pcmcia_sockets
;
2846 pcmcia_sockets
= entry
;
2849 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2851 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2853 ptr
= &pcmcia_sockets
;
2854 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2855 if (entry
->socket
== socket
) {
2861 void pcmcia_info(Monitor
*mon
)
2863 struct pcmcia_socket_entry_s
*iter
;
2865 if (!pcmcia_sockets
)
2866 monitor_printf(mon
, "No PCMCIA sockets\n");
2868 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2869 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2870 iter
->socket
->attached
? iter
->socket
->card_string
:
2874 /***********************************************************/
2875 /* register display */
2877 struct DisplayAllocator default_allocator
= {
2878 defaultallocator_create_displaysurface
,
2879 defaultallocator_resize_displaysurface
,
2880 defaultallocator_free_displaysurface
2883 void register_displaystate(DisplayState
*ds
)
2893 DisplayState
*get_displaystate(void)
2895 return display_state
;
2898 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2900 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2901 return ds
->allocator
;
2906 static void dumb_display_init(void)
2908 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2909 ds
->allocator
= &default_allocator
;
2910 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2911 register_displaystate(ds
);
2914 /***********************************************************/
2917 typedef struct IOHandlerRecord
{
2919 IOCanRWHandler
*fd_read_poll
;
2921 IOHandler
*fd_write
;
2924 /* temporary data */
2926 struct IOHandlerRecord
*next
;
2929 static IOHandlerRecord
*first_io_handler
;
2931 /* XXX: fd_read_poll should be suppressed, but an API change is
2932 necessary in the character devices to suppress fd_can_read(). */
2933 int qemu_set_fd_handler2(int fd
,
2934 IOCanRWHandler
*fd_read_poll
,
2936 IOHandler
*fd_write
,
2939 IOHandlerRecord
**pioh
, *ioh
;
2941 if (!fd_read
&& !fd_write
) {
2942 pioh
= &first_io_handler
;
2947 if (ioh
->fd
== fd
) {
2954 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2958 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2959 ioh
->next
= first_io_handler
;
2960 first_io_handler
= ioh
;
2963 ioh
->fd_read_poll
= fd_read_poll
;
2964 ioh
->fd_read
= fd_read
;
2965 ioh
->fd_write
= fd_write
;
2966 ioh
->opaque
= opaque
;
2972 int qemu_set_fd_handler(int fd
,
2974 IOHandler
*fd_write
,
2977 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2981 /***********************************************************/
2982 /* Polling handling */
2984 typedef struct PollingEntry
{
2987 struct PollingEntry
*next
;
2990 static PollingEntry
*first_polling_entry
;
2992 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2994 PollingEntry
**ppe
, *pe
;
2995 pe
= qemu_mallocz(sizeof(PollingEntry
));
2997 pe
->opaque
= opaque
;
2998 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3003 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3005 PollingEntry
**ppe
, *pe
;
3006 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3008 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3016 /***********************************************************/
3017 /* Wait objects support */
3018 typedef struct WaitObjects
{
3020 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3021 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3022 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3025 static WaitObjects wait_objects
= {0};
3027 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3029 WaitObjects
*w
= &wait_objects
;
3031 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3033 w
->events
[w
->num
] = handle
;
3034 w
->func
[w
->num
] = func
;
3035 w
->opaque
[w
->num
] = opaque
;
3040 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3043 WaitObjects
*w
= &wait_objects
;
3046 for (i
= 0; i
< w
->num
; i
++) {
3047 if (w
->events
[i
] == handle
)
3050 w
->events
[i
] = w
->events
[i
+ 1];
3051 w
->func
[i
] = w
->func
[i
+ 1];
3052 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3060 /***********************************************************/
3061 /* ram save/restore */
3063 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3067 v
= qemu_get_byte(f
);
3070 if (qemu_get_buffer(f
, buf
, len
) != len
)
3074 v
= qemu_get_byte(f
);
3075 memset(buf
, v
, len
);
3081 if (qemu_file_has_error(f
))
3087 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3092 if (qemu_get_be32(f
) != phys_ram_size
)
3094 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3095 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3102 #define BDRV_HASH_BLOCK_SIZE 1024
3103 #define IOBUF_SIZE 4096
3104 #define RAM_CBLOCK_MAGIC 0xfabe
3106 typedef struct RamDecompressState
{
3109 uint8_t buf
[IOBUF_SIZE
];
3110 } RamDecompressState
;
3112 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3115 memset(s
, 0, sizeof(*s
));
3117 ret
= inflateInit(&s
->zstream
);
3123 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3127 s
->zstream
.avail_out
= len
;
3128 s
->zstream
.next_out
= buf
;
3129 while (s
->zstream
.avail_out
> 0) {
3130 if (s
->zstream
.avail_in
== 0) {
3131 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3133 clen
= qemu_get_be16(s
->f
);
3134 if (clen
> IOBUF_SIZE
)
3136 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3137 s
->zstream
.avail_in
= clen
;
3138 s
->zstream
.next_in
= s
->buf
;
3140 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3141 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3148 static void ram_decompress_close(RamDecompressState
*s
)
3150 inflateEnd(&s
->zstream
);
3153 #define RAM_SAVE_FLAG_FULL 0x01
3154 #define RAM_SAVE_FLAG_COMPRESS 0x02
3155 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3156 #define RAM_SAVE_FLAG_PAGE 0x08
3157 #define RAM_SAVE_FLAG_EOS 0x10
3159 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3161 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3162 uint32_t *array
= (uint32_t *)page
;
3165 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3166 if (array
[i
] != val
)
3173 static int ram_save_block(QEMUFile
*f
)
3175 static ram_addr_t current_addr
= 0;
3176 ram_addr_t saved_addr
= current_addr
;
3177 ram_addr_t addr
= 0;
3180 while (addr
< phys_ram_size
) {
3181 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3184 cpu_physical_memory_reset_dirty(current_addr
,
3185 current_addr
+ TARGET_PAGE_SIZE
,
3186 MIGRATION_DIRTY_FLAG
);
3188 ch
= *(phys_ram_base
+ current_addr
);
3190 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3191 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3192 qemu_put_byte(f
, ch
);
3194 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3195 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3201 addr
+= TARGET_PAGE_SIZE
;
3202 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3208 static ram_addr_t ram_save_threshold
= 10;
3210 static ram_addr_t
ram_save_remaining(void)
3213 ram_addr_t count
= 0;
3215 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3216 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3223 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3228 /* Make sure all dirty bits are set */
3229 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3230 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3231 cpu_physical_memory_set_dirty(addr
);
3234 /* Enable dirty memory tracking */
3235 cpu_physical_memory_set_dirty_tracking(1);
3237 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3240 while (!qemu_file_rate_limit(f
)) {
3243 ret
= ram_save_block(f
);
3244 if (ret
== 0) /* no more blocks */
3248 /* try transferring iterative blocks of memory */
3251 cpu_physical_memory_set_dirty_tracking(0);
3253 /* flush all remaining blocks regardless of rate limiting */
3254 while (ram_save_block(f
) != 0);
3257 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3259 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3262 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3264 RamDecompressState s1
, *s
= &s1
;
3268 if (ram_decompress_open(s
, f
) < 0)
3270 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3271 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3272 fprintf(stderr
, "Error while reading ram block header\n");
3276 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3277 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3282 printf("Error block header\n");
3286 ram_decompress_close(s
);
3291 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3296 if (version_id
== 1)
3297 return ram_load_v1(f
, opaque
);
3299 if (version_id
== 2) {
3300 if (qemu_get_be32(f
) != phys_ram_size
)
3302 return ram_load_dead(f
, opaque
);
3305 if (version_id
!= 3)
3309 addr
= qemu_get_be64(f
);
3311 flags
= addr
& ~TARGET_PAGE_MASK
;
3312 addr
&= TARGET_PAGE_MASK
;
3314 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3315 if (addr
!= phys_ram_size
)
3319 if (flags
& RAM_SAVE_FLAG_FULL
) {
3320 if (ram_load_dead(f
, opaque
) < 0)
3324 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3325 uint8_t ch
= qemu_get_byte(f
);
3326 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3327 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3328 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3329 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3334 void qemu_service_io(void)
3336 CPUState
*env
= cpu_single_env
;
3340 if (env
->kqemu_enabled
) {
3341 kqemu_cpu_interrupt(env
);
3347 /***********************************************************/
3348 /* bottom halves (can be seen as timers which expire ASAP) */
3359 static QEMUBH
*first_bh
= NULL
;
3361 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3364 bh
= qemu_mallocz(sizeof(QEMUBH
));
3366 bh
->opaque
= opaque
;
3367 bh
->next
= first_bh
;
3372 int qemu_bh_poll(void)
3378 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3379 if (!bh
->deleted
&& bh
->scheduled
) {
3388 /* remove deleted bhs */
3402 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3410 void qemu_bh_schedule(QEMUBH
*bh
)
3412 CPUState
*env
= cpu_single_env
;
3417 /* stop the currently executing CPU to execute the BH ASAP */
3423 void qemu_bh_cancel(QEMUBH
*bh
)
3428 void qemu_bh_delete(QEMUBH
*bh
)
3434 static void qemu_bh_update_timeout(int *timeout
)
3438 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3439 if (!bh
->deleted
&& bh
->scheduled
) {
3441 /* idle bottom halves will be polled at least
3443 *timeout
= MIN(10, *timeout
);
3445 /* non-idle bottom halves will be executed
3454 /***********************************************************/
3455 /* machine registration */
3457 static QEMUMachine
*first_machine
= NULL
;
3458 QEMUMachine
*current_machine
= NULL
;
3460 int qemu_register_machine(QEMUMachine
*m
)
3463 pm
= &first_machine
;
3471 static QEMUMachine
*find_machine(const char *name
)
3475 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3476 if (!strcmp(m
->name
, name
))
3482 /***********************************************************/
3483 /* main execution loop */
3485 static void gui_update(void *opaque
)
3487 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3488 DisplayState
*ds
= opaque
;
3489 DisplayChangeListener
*dcl
= ds
->listeners
;
3493 while (dcl
!= NULL
) {
3494 if (dcl
->gui_timer_interval
&&
3495 dcl
->gui_timer_interval
< interval
)
3496 interval
= dcl
->gui_timer_interval
;
3499 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3502 static void nographic_update(void *opaque
)
3504 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3506 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3509 struct vm_change_state_entry
{
3510 VMChangeStateHandler
*cb
;
3512 LIST_ENTRY (vm_change_state_entry
) entries
;
3515 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3517 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3520 VMChangeStateEntry
*e
;
3522 e
= qemu_mallocz(sizeof (*e
));
3526 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3530 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3532 LIST_REMOVE (e
, entries
);
3536 static void vm_state_notify(int running
, int reason
)
3538 VMChangeStateEntry
*e
;
3540 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3541 e
->cb(e
->opaque
, running
, reason
);
3550 vm_state_notify(1, 0);
3551 qemu_rearm_alarm_timer(alarm_timer
);
3555 void vm_stop(int reason
)
3558 cpu_disable_ticks();
3560 vm_state_notify(0, reason
);
3564 /* reset/shutdown handler */
3566 typedef struct QEMUResetEntry
{
3567 QEMUResetHandler
*func
;
3569 struct QEMUResetEntry
*next
;
3572 static QEMUResetEntry
*first_reset_entry
;
3573 static int reset_requested
;
3574 static int shutdown_requested
;
3575 static int powerdown_requested
;
3577 int qemu_shutdown_requested(void)
3579 int r
= shutdown_requested
;
3580 shutdown_requested
= 0;
3584 int qemu_reset_requested(void)
3586 int r
= reset_requested
;
3587 reset_requested
= 0;
3591 int qemu_powerdown_requested(void)
3593 int r
= powerdown_requested
;
3594 powerdown_requested
= 0;
3598 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3600 QEMUResetEntry
**pre
, *re
;
3602 pre
= &first_reset_entry
;
3603 while (*pre
!= NULL
)
3604 pre
= &(*pre
)->next
;
3605 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3607 re
->opaque
= opaque
;
3612 void qemu_system_reset(void)
3616 /* reset all devices */
3617 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3618 re
->func(re
->opaque
);
3622 void qemu_system_reset_request(void)
3625 shutdown_requested
= 1;
3627 reset_requested
= 1;
3630 cpu_exit(cpu_single_env
);
3633 void qemu_system_shutdown_request(void)
3635 shutdown_requested
= 1;
3637 cpu_exit(cpu_single_env
);
3640 void qemu_system_powerdown_request(void)
3642 powerdown_requested
= 1;
3644 cpu_exit(cpu_single_env
);
3648 static void host_main_loop_wait(int *timeout
)
3654 /* XXX: need to suppress polling by better using win32 events */
3656 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3657 ret
|= pe
->func(pe
->opaque
);
3661 WaitObjects
*w
= &wait_objects
;
3663 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3664 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3665 if (w
->func
[ret
- WAIT_OBJECT_0
])
3666 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3668 /* Check for additional signaled events */
3669 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3671 /* Check if event is signaled */
3672 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3673 if(ret2
== WAIT_OBJECT_0
) {
3675 w
->func
[i
](w
->opaque
[i
]);
3676 } else if (ret2
== WAIT_TIMEOUT
) {
3678 err
= GetLastError();
3679 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3682 } else if (ret
== WAIT_TIMEOUT
) {
3684 err
= GetLastError();
3685 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3692 static void host_main_loop_wait(int *timeout
)
3697 void main_loop_wait(int timeout
)
3699 IOHandlerRecord
*ioh
;
3700 fd_set rfds
, wfds
, xfds
;
3704 qemu_bh_update_timeout(&timeout
);
3706 host_main_loop_wait(&timeout
);
3708 /* poll any events */
3709 /* XXX: separate device handlers from system ones */
3714 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3718 (!ioh
->fd_read_poll
||
3719 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3720 FD_SET(ioh
->fd
, &rfds
);
3724 if (ioh
->fd_write
) {
3725 FD_SET(ioh
->fd
, &wfds
);
3731 tv
.tv_sec
= timeout
/ 1000;
3732 tv
.tv_usec
= (timeout
% 1000) * 1000;
3734 #if defined(CONFIG_SLIRP)
3735 if (slirp_is_inited()) {
3736 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3739 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3741 IOHandlerRecord
**pioh
;
3743 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3744 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3745 ioh
->fd_read(ioh
->opaque
);
3747 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3748 ioh
->fd_write(ioh
->opaque
);
3752 /* remove deleted IO handlers */
3753 pioh
= &first_io_handler
;
3763 #if defined(CONFIG_SLIRP)
3764 if (slirp_is_inited()) {
3770 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3774 /* vm time timers */
3775 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3776 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3777 qemu_get_clock(vm_clock
));
3779 /* real time timers */
3780 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3781 qemu_get_clock(rt_clock
));
3783 /* Check bottom-halves last in case any of the earlier events triggered
3789 static int main_loop(void)
3792 #ifdef CONFIG_PROFILER
3797 cur_cpu
= first_cpu
;
3798 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3805 #ifdef CONFIG_PROFILER
3806 ti
= profile_getclock();
3811 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3812 env
->icount_decr
.u16
.low
= 0;
3813 env
->icount_extra
= 0;
3814 count
= qemu_next_deadline();
3815 count
= (count
+ (1 << icount_time_shift
) - 1)
3816 >> icount_time_shift
;
3817 qemu_icount
+= count
;
3818 decr
= (count
> 0xffff) ? 0xffff : count
;
3820 env
->icount_decr
.u16
.low
= decr
;
3821 env
->icount_extra
= count
;
3823 ret
= cpu_exec(env
);
3824 #ifdef CONFIG_PROFILER
3825 qemu_time
+= profile_getclock() - ti
;
3828 /* Fold pending instructions back into the
3829 instruction counter, and clear the interrupt flag. */
3830 qemu_icount
-= (env
->icount_decr
.u16
.low
3831 + env
->icount_extra
);
3832 env
->icount_decr
.u32
= 0;
3833 env
->icount_extra
= 0;
3835 next_cpu
= env
->next_cpu
?: first_cpu
;
3836 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3837 ret
= EXCP_INTERRUPT
;
3841 if (ret
== EXCP_HLT
) {
3842 /* Give the next CPU a chance to run. */
3846 if (ret
!= EXCP_HALTED
)
3848 /* all CPUs are halted ? */
3854 if (shutdown_requested
) {
3855 ret
= EXCP_INTERRUPT
;
3863 if (reset_requested
) {
3864 reset_requested
= 0;
3865 qemu_system_reset();
3866 ret
= EXCP_INTERRUPT
;
3868 if (powerdown_requested
) {
3869 powerdown_requested
= 0;
3870 qemu_system_powerdown();
3871 ret
= EXCP_INTERRUPT
;
3873 if (unlikely(ret
== EXCP_DEBUG
)) {
3874 gdb_set_stop_cpu(cur_cpu
);
3875 vm_stop(EXCP_DEBUG
);
3877 /* If all cpus are halted then wait until the next IRQ */
3878 /* XXX: use timeout computed from timers */
3879 if (ret
== EXCP_HALTED
) {
3883 /* Advance virtual time to the next event. */
3884 if (use_icount
== 1) {
3885 /* When not using an adaptive execution frequency
3886 we tend to get badly out of sync with real time,
3887 so just delay for a reasonable amount of time. */
3890 delta
= cpu_get_icount() - cpu_get_clock();
3893 /* If virtual time is ahead of real time then just
3895 timeout
= (delta
/ 1000000) + 1;
3897 /* Wait for either IO to occur or the next
3899 add
= qemu_next_deadline();
3900 /* We advance the timer before checking for IO.
3901 Limit the amount we advance so that early IO
3902 activity won't get the guest too far ahead. */
3906 add
= (add
+ (1 << icount_time_shift
) - 1)
3907 >> icount_time_shift
;
3909 timeout
= delta
/ 1000000;
3920 if (shutdown_requested
) {
3921 ret
= EXCP_INTERRUPT
;
3926 #ifdef CONFIG_PROFILER
3927 ti
= profile_getclock();
3929 main_loop_wait(timeout
);
3930 #ifdef CONFIG_PROFILER
3931 dev_time
+= profile_getclock() - ti
;
3934 cpu_disable_ticks();
3938 static void help(int exitcode
)
3940 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3941 and qemu-doc.texi */
3942 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3943 "usage: %s [options] [disk_image]\n"
3945 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3947 "Standard options:\n"
3948 "-h or -help display this help and exit\n"
3949 "-M machine select emulated machine (-M ? for list)\n"
3950 "-cpu cpu select CPU (-cpu ? for list)\n"
3951 "-smp n set the number of CPUs to 'n' [default=1]\n"
3952 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3953 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3954 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3955 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3956 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3957 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3958 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3959 " use 'file' as a drive image\n"
3960 "-mtdblock file use 'file' as on-board Flash memory image\n"
3961 "-sd file use 'file' as SecureDigital card image\n"
3962 "-pflash file use 'file' as a parallel flash image\n"
3963 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3964 "-snapshot write to temporary files instead of disk image files\n"
3965 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3967 "-k language use keyboard layout (for example \"fr\" for French)\n"
3970 "-audio-help print list of audio drivers and their options\n"
3971 "-soundhw c1,... enable audio support\n"
3972 " and only specified sound cards (comma separated list)\n"
3973 " use -soundhw ? to get the list of supported cards\n"
3974 " use -soundhw all to enable all of them\n"
3976 "-usb enable the USB driver (will be the default soon)\n"
3977 "-usbdevice name add the host or guest USB device 'name'\n"
3978 "-name string set the name of the guest\n"
3979 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3980 " specify machine UUID\n"
3982 "Display options:\n"
3983 "-nographic disable graphical output and redirect serial I/Os to console\n"
3984 #ifdef CONFIG_CURSES
3985 "-curses use a curses/ncurses interface instead of SDL\n"
3988 "-no-frame open SDL window without a frame and window decorations\n"
3989 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3990 "-no-quit disable SDL window close capability\n"
3993 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3994 "-vga [std|cirrus|vmware|none]\n"
3995 " select video card type\n"
3996 "-full-screen start in full screen\n"
3997 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3998 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
4000 "-vnc display start a VNC server on display\n"
4002 "Network options:\n"
4003 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
4004 " create a new Network Interface Card and connect it to VLAN 'n'\n"
4006 "-net user[,vlan=n][,name=str][,hostname=host]\n"
4007 " connect the user mode network stack to VLAN 'n' and send\n"
4008 " hostname 'host' to DHCP clients\n"
4011 "-net tap[,vlan=n][,name=str],ifname=name\n"
4012 " connect the host TAP network interface to VLAN 'n'\n"
4014 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4015 " connect the host TAP network interface to VLAN 'n' and use the\n"
4016 " network scripts 'file' (default=%s)\n"
4017 " and 'dfile' (default=%s);\n"
4018 " use '[down]script=no' to disable script execution;\n"
4019 " use 'fd=h' to connect to an already opened TAP interface\n"
4021 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4022 " connect the vlan 'n' to another VLAN using a socket connection\n"
4023 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4024 " connect the vlan 'n' to multicast maddr and port\n"
4026 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4027 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4028 " on host and listening for incoming connections on 'socketpath'.\n"
4029 " Use group 'groupname' and mode 'octalmode' to change default\n"
4030 " ownership and permissions for communication port.\n"
4032 "-net none use it alone to have zero network devices; if no -net option\n"
4033 " is provided, the default is '-net nic -net user'\n"
4035 "-tftp dir allow tftp access to files in dir [-net user]\n"
4036 "-bootp file advertise file in BOOTP replies\n"
4038 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4040 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4041 " redirect TCP or UDP connections from host to guest [-net user]\n"
4044 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4045 "-bt hci,host[:id]\n"
4046 " use host's HCI with the given name\n"
4047 "-bt hci[,vlan=n]\n"
4048 " emulate a standard HCI in virtual scatternet 'n'\n"
4049 "-bt vhci[,vlan=n]\n"
4050 " add host computer to virtual scatternet 'n' using VHCI\n"
4051 "-bt device:dev[,vlan=n]\n"
4052 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4056 "i386 target only:\n"
4057 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4058 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4059 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4060 "-no-acpi disable ACPI\n"
4061 "-no-hpet disable HPET\n"
4062 "-acpitable [sig=str][,rev=n][,oem_id=str][,oem_table_id=str][,oem_rev=n][,asl_compiler_id=str][,asl_compiler_rev=n][,data=file1[:file2]...]\n"
4063 " ACPI table description\n"
4065 "Linux boot specific:\n"
4066 "-kernel bzImage use 'bzImage' as kernel image\n"
4067 "-append cmdline use 'cmdline' as kernel command line\n"
4068 "-initrd file use 'file' as initial ram disk\n"
4070 "Debug/Expert options:\n"
4071 "-serial dev redirect the serial port to char device 'dev'\n"
4072 "-parallel dev redirect the parallel port to char device 'dev'\n"
4073 "-monitor dev redirect the monitor to char device 'dev'\n"
4074 "-pidfile file write PID to 'file'\n"
4075 "-S freeze CPU at startup (use 'c' to start execution)\n"
4076 "-s wait gdb connection to port\n"
4077 "-p port set gdb connection port [default=%s]\n"
4078 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4079 "-hdachs c,h,s[,t]\n"
4080 " force hard disk 0 physical geometry and the optional BIOS\n"
4081 " translation (t=none or lba) (usually qemu can guess them)\n"
4082 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4083 "-bios file set the filename for the BIOS\n"
4085 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4086 "-no-kqemu disable KQEMU kernel module usage\n"
4089 "-enable-kvm enable KVM full virtualization support\n"
4091 "-no-reboot exit instead of rebooting\n"
4092 "-no-shutdown stop before shutdown\n"
4093 "-loadvm [tag|id]\n"
4094 " start right away with a saved state (loadvm in monitor)\n"
4096 "-daemonize daemonize QEMU after initializing\n"
4098 "-option-rom rom load a file, rom, into the option ROM space\n"
4099 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4100 "-prom-env variable=value\n"
4101 " set OpenBIOS nvram variables\n"
4103 "-clock force the use of the given methods for timer alarm.\n"
4104 " To see what timers are available use -clock ?\n"
4105 "-localtime set the real time clock to local time [default=utc]\n"
4106 "-startdate select initial date of the clock\n"
4107 "-icount [N|auto]\n"
4108 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4109 "-echr chr set terminal escape character instead of ctrl-a\n"
4110 "-virtioconsole c\n"
4111 " set virtio console\n"
4112 "-show-cursor show cursor\n"
4113 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4114 "-semihosting semihosting mode\n"
4116 #if defined(TARGET_ARM)
4117 "-old-param old param mode\n"
4119 "-tb-size n set TB size\n"
4120 "-incoming p prepare for incoming migration, listen on port p\n"
4122 "-chroot dir Chroot to dir just before starting the VM.\n"
4123 "-runas user Change to user id user just before starting the VM.\n"
4126 "During emulation, the following keys are useful:\n"
4127 "ctrl-alt-f toggle full screen\n"
4128 "ctrl-alt-n switch to virtual console 'n'\n"
4129 "ctrl-alt toggle mouse and keyboard grab\n"
4131 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4136 DEFAULT_NETWORK_SCRIPT
,
4137 DEFAULT_NETWORK_DOWN_SCRIPT
,
4139 DEFAULT_GDBSTUB_PORT
,
4144 #define HAS_ARG 0x0001
4147 /* Please keep in synch with help, qemu_options[] and
4149 /* Standard options: */
4162 QEMU_OPTION_mtdblock
,
4166 QEMU_OPTION_snapshot
,
4169 QEMU_OPTION_audio_help
,
4170 QEMU_OPTION_soundhw
,
4172 QEMU_OPTION_usbdevice
,
4176 /* Display options: */
4177 QEMU_OPTION_nographic
,
4179 QEMU_OPTION_no_frame
,
4180 QEMU_OPTION_alt_grab
,
4181 QEMU_OPTION_no_quit
,
4183 QEMU_OPTION_portrait
,
4185 QEMU_OPTION_full_screen
,
4189 /* Network options: */
4197 /* i386 target only: */
4198 QEMU_OPTION_win2k_hack
,
4199 QEMU_OPTION_rtc_td_hack
,
4200 QEMU_OPTION_no_fd_bootchk
,
4201 QEMU_OPTION_no_acpi
,
4202 QEMU_OPTION_no_hpet
,
4203 QEMU_OPTION_acpitable
,
4205 /* Linux boot specific: */
4210 /* Debug/Expert options: */
4212 QEMU_OPTION_parallel
,
4213 QEMU_OPTION_monitor
,
4214 QEMU_OPTION_pidfile
,
4222 QEMU_OPTION_kernel_kqemu
,
4223 QEMU_OPTION_no_kqemu
,
4224 QEMU_OPTION_enable_kvm
,
4225 QEMU_OPTION_no_reboot
,
4226 QEMU_OPTION_no_shutdown
,
4228 QEMU_OPTION_daemonize
,
4229 QEMU_OPTION_option_rom
,
4230 QEMU_OPTION_prom_env
,
4232 QEMU_OPTION_localtime
,
4233 QEMU_OPTION_startdate
,
4236 QEMU_OPTION_virtiocon
,
4237 QEMU_OPTION_show_cursor
,
4238 QEMU_OPTION_semihosting
,
4239 QEMU_OPTION_old_param
,
4240 QEMU_OPTION_tb_size
,
4241 QEMU_OPTION_incoming
,
4246 typedef struct QEMUOption
{
4252 static const QEMUOption qemu_options
[] = {
4253 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4255 /* Standard options: */
4256 { "h", 0, QEMU_OPTION_h
},
4257 { "help", 0, QEMU_OPTION_h
},
4258 { "M", HAS_ARG
, QEMU_OPTION_M
},
4259 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4260 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4261 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4262 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4263 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4264 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4265 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4266 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4267 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4268 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4269 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4270 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4271 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4272 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4273 { "snapshot", 0, QEMU_OPTION_snapshot
},
4274 { "m", HAS_ARG
, QEMU_OPTION_m
},
4276 { "k", HAS_ARG
, QEMU_OPTION_k
},
4279 { "audio-help", 0, QEMU_OPTION_audio_help
},
4280 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4282 { "usb", 0, QEMU_OPTION_usb
},
4283 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4284 { "name", HAS_ARG
, QEMU_OPTION_name
},
4285 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4287 /* Display options: */
4288 { "nographic", 0, QEMU_OPTION_nographic
},
4289 #ifdef CONFIG_CURSES
4290 { "curses", 0, QEMU_OPTION_curses
},
4293 { "no-frame", 0, QEMU_OPTION_no_frame
},
4294 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4295 { "no-quit", 0, QEMU_OPTION_no_quit
},
4296 { "sdl", 0, QEMU_OPTION_sdl
},
4298 { "portrait", 0, QEMU_OPTION_portrait
},
4299 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4300 { "full-screen", 0, QEMU_OPTION_full_screen
},
4301 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4302 { "g", 1, QEMU_OPTION_g
},
4304 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4306 /* Network options: */
4307 { "net", HAS_ARG
, QEMU_OPTION_net
},
4309 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4310 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4312 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4314 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4316 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4318 /* i386 target only: */
4319 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4320 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4321 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4322 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4323 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4324 { "acpitable", HAS_ARG
, QEMU_OPTION_acpitable
},
4327 /* Linux boot specific: */
4328 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4329 { "append", HAS_ARG
, QEMU_OPTION_append
},
4330 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4332 /* Debug/Expert options: */
4333 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4334 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4335 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4336 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4337 { "S", 0, QEMU_OPTION_S
},
4338 { "s", 0, QEMU_OPTION_s
},
4339 { "p", HAS_ARG
, QEMU_OPTION_p
},
4340 { "d", HAS_ARG
, QEMU_OPTION_d
},
4341 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4342 { "L", HAS_ARG
, QEMU_OPTION_L
},
4343 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4345 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4346 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4349 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4351 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4352 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4353 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4354 { "daemonize", 0, QEMU_OPTION_daemonize
},
4355 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4356 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4357 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4359 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4360 { "localtime", 0, QEMU_OPTION_localtime
},
4361 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4362 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4363 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4364 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4365 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4366 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4367 { "semihosting", 0, QEMU_OPTION_semihosting
},
4369 #if defined(TARGET_ARM)
4370 { "old-param", 0, QEMU_OPTION_old_param
},
4372 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4373 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4374 { "chroot", HAS_ARG
, QEMU_OPTION_chroot
},
4375 { "runas", HAS_ARG
, QEMU_OPTION_runas
},
4380 struct soundhw soundhw
[] = {
4381 #ifdef HAS_AUDIO_CHOICE
4382 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4388 { .init_isa
= pcspk_audio_init
}
4395 "Creative Sound Blaster 16",
4398 { .init_isa
= SB16_init
}
4402 #ifdef CONFIG_CS4231A
4408 { .init_isa
= cs4231a_init
}
4416 "Yamaha YMF262 (OPL3)",
4418 "Yamaha YM3812 (OPL2)",
4422 { .init_isa
= Adlib_init
}
4429 "Gravis Ultrasound GF1",
4432 { .init_isa
= GUS_init
}
4439 "Intel 82801AA AC97 Audio",
4442 { .init_pci
= ac97_init
}
4446 #ifdef CONFIG_ES1370
4449 "ENSONIQ AudioPCI ES1370",
4452 { .init_pci
= es1370_init
}
4456 #endif /* HAS_AUDIO_CHOICE */
4458 { NULL
, NULL
, 0, 0, { NULL
} }
4461 static void select_soundhw (const char *optarg
)
4465 if (*optarg
== '?') {
4468 printf ("Valid sound card names (comma separated):\n");
4469 for (c
= soundhw
; c
->name
; ++c
) {
4470 printf ("%-11s %s\n", c
->name
, c
->descr
);
4472 printf ("\n-soundhw all will enable all of the above\n");
4473 exit (*optarg
!= '?');
4481 if (!strcmp (optarg
, "all")) {
4482 for (c
= soundhw
; c
->name
; ++c
) {
4490 e
= strchr (p
, ',');
4491 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4493 for (c
= soundhw
; c
->name
; ++c
) {
4494 if (!strncmp (c
->name
, p
, l
)) {
4503 "Unknown sound card name (too big to show)\n");
4506 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4511 p
+= l
+ (e
!= NULL
);
4515 goto show_valid_cards
;
4520 static void select_vgahw (const char *p
)
4524 if (strstart(p
, "std", &opts
)) {
4525 std_vga_enabled
= 1;
4526 cirrus_vga_enabled
= 0;
4528 } else if (strstart(p
, "cirrus", &opts
)) {
4529 cirrus_vga_enabled
= 1;
4530 std_vga_enabled
= 0;
4532 } else if (strstart(p
, "vmware", &opts
)) {
4533 cirrus_vga_enabled
= 0;
4534 std_vga_enabled
= 0;
4536 } else if (strstart(p
, "none", &opts
)) {
4537 cirrus_vga_enabled
= 0;
4538 std_vga_enabled
= 0;
4542 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4546 const char *nextopt
;
4548 if (strstart(opts
, ",retrace=", &nextopt
)) {
4550 if (strstart(opts
, "dumb", &nextopt
))
4551 vga_retrace_method
= VGA_RETRACE_DUMB
;
4552 else if (strstart(opts
, "precise", &nextopt
))
4553 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4554 else goto invalid_vga
;
4555 } else goto invalid_vga
;
4561 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4563 exit(STATUS_CONTROL_C_EXIT
);
4568 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4572 if(strlen(str
) != 36)
4575 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4576 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4577 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4585 #define MAX_NET_CLIENTS 32
4589 static void termsig_handler(int signal
)
4591 qemu_system_shutdown_request();
4594 static void termsig_setup(void)
4596 struct sigaction act
;
4598 memset(&act
, 0, sizeof(act
));
4599 act
.sa_handler
= termsig_handler
;
4600 sigaction(SIGINT
, &act
, NULL
);
4601 sigaction(SIGHUP
, &act
, NULL
);
4602 sigaction(SIGTERM
, &act
, NULL
);
4607 int main(int argc
, char **argv
, char **envp
)
4609 #ifdef CONFIG_GDBSTUB
4611 const char *gdbstub_port
;
4613 uint32_t boot_devices_bitmap
= 0;
4615 int snapshot
, linux_boot
, net_boot
;
4616 const char *initrd_filename
;
4617 const char *kernel_filename
, *kernel_cmdline
;
4618 const char *boot_devices
= "";
4620 DisplayChangeListener
*dcl
;
4621 int cyls
, heads
, secs
, translation
;
4622 const char *net_clients
[MAX_NET_CLIENTS
];
4624 const char *bt_opts
[MAX_BT_CMDLINE
];
4628 const char *r
, *optarg
;
4629 CharDriverState
*monitor_hd
= NULL
;
4630 const char *monitor_device
;
4631 const char *serial_devices
[MAX_SERIAL_PORTS
];
4632 int serial_device_index
;
4633 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4634 int parallel_device_index
;
4635 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4636 int virtio_console_index
;
4637 const char *loadvm
= NULL
;
4638 QEMUMachine
*machine
;
4639 const char *cpu_model
;
4640 const char *usb_devices
[MAX_USB_CMDLINE
];
4641 int usb_devices_index
;
4644 const char *pid_file
= NULL
;
4645 const char *incoming
= NULL
;
4647 struct passwd
*pwd
= NULL
;
4648 const char *chroot_dir
= NULL
;
4649 const char *run_as
= NULL
;
4651 qemu_cache_utils_init(envp
);
4653 LIST_INIT (&vm_change_state_head
);
4656 struct sigaction act
;
4657 sigfillset(&act
.sa_mask
);
4659 act
.sa_handler
= SIG_IGN
;
4660 sigaction(SIGPIPE
, &act
, NULL
);
4663 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4664 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4665 QEMU to run on a single CPU */
4670 h
= GetCurrentProcess();
4671 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4672 for(i
= 0; i
< 32; i
++) {
4673 if (mask
& (1 << i
))
4678 SetProcessAffinityMask(h
, mask
);
4684 register_machines();
4685 machine
= first_machine
;
4687 initrd_filename
= NULL
;
4689 vga_ram_size
= VGA_RAM_SIZE
;
4690 #ifdef CONFIG_GDBSTUB
4692 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4697 kernel_filename
= NULL
;
4698 kernel_cmdline
= "";
4699 cyls
= heads
= secs
= 0;
4700 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4701 monitor_device
= "vc:80Cx24C";
4703 serial_devices
[0] = "vc:80Cx24C";
4704 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4705 serial_devices
[i
] = NULL
;
4706 serial_device_index
= 0;
4708 parallel_devices
[0] = "vc:80Cx24C";
4709 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4710 parallel_devices
[i
] = NULL
;
4711 parallel_device_index
= 0;
4713 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4714 virtio_consoles
[i
] = NULL
;
4715 virtio_console_index
= 0;
4717 usb_devices_index
= 0;
4736 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4738 const QEMUOption
*popt
;
4741 /* Treat --foo the same as -foo. */
4744 popt
= qemu_options
;
4747 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4751 if (!strcmp(popt
->name
, r
+ 1))
4755 if (popt
->flags
& HAS_ARG
) {
4756 if (optind
>= argc
) {
4757 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4761 optarg
= argv
[optind
++];
4766 switch(popt
->index
) {
4768 machine
= find_machine(optarg
);
4771 printf("Supported machines are:\n");
4772 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4773 printf("%-10s %s%s\n",
4775 m
== first_machine
? " (default)" : "");
4777 exit(*optarg
!= '?');
4780 case QEMU_OPTION_cpu
:
4781 /* hw initialization will check this */
4782 if (*optarg
== '?') {
4783 /* XXX: implement xxx_cpu_list for targets that still miss it */
4784 #if defined(cpu_list)
4785 cpu_list(stdout
, &fprintf
);
4792 case QEMU_OPTION_initrd
:
4793 initrd_filename
= optarg
;
4795 case QEMU_OPTION_hda
:
4797 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4799 hda_index
= drive_add(optarg
, HD_ALIAS
4800 ",cyls=%d,heads=%d,secs=%d%s",
4801 0, cyls
, heads
, secs
,
4802 translation
== BIOS_ATA_TRANSLATION_LBA
?
4804 translation
== BIOS_ATA_TRANSLATION_NONE
?
4805 ",trans=none" : "");
4807 case QEMU_OPTION_hdb
:
4808 case QEMU_OPTION_hdc
:
4809 case QEMU_OPTION_hdd
:
4810 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4812 case QEMU_OPTION_drive
:
4813 drive_add(NULL
, "%s", optarg
);
4815 case QEMU_OPTION_mtdblock
:
4816 drive_add(optarg
, MTD_ALIAS
);
4818 case QEMU_OPTION_sd
:
4819 drive_add(optarg
, SD_ALIAS
);
4821 case QEMU_OPTION_pflash
:
4822 drive_add(optarg
, PFLASH_ALIAS
);
4824 case QEMU_OPTION_snapshot
:
4827 case QEMU_OPTION_hdachs
:
4831 cyls
= strtol(p
, (char **)&p
, 0);
4832 if (cyls
< 1 || cyls
> 16383)
4837 heads
= strtol(p
, (char **)&p
, 0);
4838 if (heads
< 1 || heads
> 16)
4843 secs
= strtol(p
, (char **)&p
, 0);
4844 if (secs
< 1 || secs
> 63)
4848 if (!strcmp(p
, "none"))
4849 translation
= BIOS_ATA_TRANSLATION_NONE
;
4850 else if (!strcmp(p
, "lba"))
4851 translation
= BIOS_ATA_TRANSLATION_LBA
;
4852 else if (!strcmp(p
, "auto"))
4853 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4856 } else if (*p
!= '\0') {
4858 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4861 if (hda_index
!= -1)
4862 snprintf(drives_opt
[hda_index
].opt
,
4863 sizeof(drives_opt
[hda_index
].opt
),
4864 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4865 0, cyls
, heads
, secs
,
4866 translation
== BIOS_ATA_TRANSLATION_LBA
?
4868 translation
== BIOS_ATA_TRANSLATION_NONE
?
4869 ",trans=none" : "");
4872 case QEMU_OPTION_nographic
:
4875 #ifdef CONFIG_CURSES
4876 case QEMU_OPTION_curses
:
4880 case QEMU_OPTION_portrait
:
4883 case QEMU_OPTION_kernel
:
4884 kernel_filename
= optarg
;
4886 case QEMU_OPTION_append
:
4887 kernel_cmdline
= optarg
;
4889 case QEMU_OPTION_cdrom
:
4890 drive_add(optarg
, CDROM_ALIAS
);
4892 case QEMU_OPTION_boot
:
4893 boot_devices
= optarg
;
4894 /* We just do some generic consistency checks */
4896 /* Could easily be extended to 64 devices if needed */
4899 boot_devices_bitmap
= 0;
4900 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4901 /* Allowed boot devices are:
4902 * a b : floppy disk drives
4903 * c ... f : IDE disk drives
4904 * g ... m : machine implementation dependant drives
4905 * n ... p : network devices
4906 * It's up to each machine implementation to check
4907 * if the given boot devices match the actual hardware
4908 * implementation and firmware features.
4910 if (*p
< 'a' || *p
> 'q') {
4911 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4914 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4916 "Boot device '%c' was given twice\n",*p
);
4919 boot_devices_bitmap
|= 1 << (*p
- 'a');
4923 case QEMU_OPTION_fda
:
4924 case QEMU_OPTION_fdb
:
4925 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4928 case QEMU_OPTION_no_fd_bootchk
:
4932 case QEMU_OPTION_net
:
4933 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4934 fprintf(stderr
, "qemu: too many network clients\n");
4937 net_clients
[nb_net_clients
] = optarg
;
4941 case QEMU_OPTION_tftp
:
4942 tftp_prefix
= optarg
;
4944 case QEMU_OPTION_bootp
:
4945 bootp_filename
= optarg
;
4948 case QEMU_OPTION_smb
:
4949 net_slirp_smb(optarg
);
4952 case QEMU_OPTION_redir
:
4953 net_slirp_redir(optarg
);
4956 case QEMU_OPTION_bt
:
4957 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4958 fprintf(stderr
, "qemu: too many bluetooth options\n");
4961 bt_opts
[nb_bt_opts
++] = optarg
;
4964 case QEMU_OPTION_audio_help
:
4968 case QEMU_OPTION_soundhw
:
4969 select_soundhw (optarg
);
4975 case QEMU_OPTION_m
: {
4979 value
= strtoul(optarg
, &ptr
, 10);
4981 case 0: case 'M': case 'm':
4988 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4992 /* On 32-bit hosts, QEMU is limited by virtual address space */
4993 if (value
> (2047 << 20)
4995 && HOST_LONG_BITS
== 32
4998 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5001 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5002 fprintf(stderr
, "qemu: ram size too large\n");
5011 const CPULogItem
*item
;
5013 mask
= cpu_str_to_log_mask(optarg
);
5015 printf("Log items (comma separated):\n");
5016 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5017 printf("%-10s %s\n", item
->name
, item
->help
);
5024 #ifdef CONFIG_GDBSTUB
5029 gdbstub_port
= optarg
;
5035 case QEMU_OPTION_bios
:
5042 keyboard_layout
= optarg
;
5044 case QEMU_OPTION_localtime
:
5047 case QEMU_OPTION_vga
:
5048 select_vgahw (optarg
);
5055 w
= strtol(p
, (char **)&p
, 10);
5058 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5064 h
= strtol(p
, (char **)&p
, 10);
5069 depth
= strtol(p
, (char **)&p
, 10);
5070 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5071 depth
!= 24 && depth
!= 32)
5073 } else if (*p
== '\0') {
5074 depth
= graphic_depth
;
5081 graphic_depth
= depth
;
5084 case QEMU_OPTION_echr
:
5087 term_escape_char
= strtol(optarg
, &r
, 0);
5089 printf("Bad argument to echr\n");
5092 case QEMU_OPTION_monitor
:
5093 monitor_device
= optarg
;
5095 case QEMU_OPTION_serial
:
5096 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5097 fprintf(stderr
, "qemu: too many serial ports\n");
5100 serial_devices
[serial_device_index
] = optarg
;
5101 serial_device_index
++;
5103 case QEMU_OPTION_virtiocon
:
5104 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5105 fprintf(stderr
, "qemu: too many virtio consoles\n");
5108 virtio_consoles
[virtio_console_index
] = optarg
;
5109 virtio_console_index
++;
5111 case QEMU_OPTION_parallel
:
5112 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5113 fprintf(stderr
, "qemu: too many parallel ports\n");
5116 parallel_devices
[parallel_device_index
] = optarg
;
5117 parallel_device_index
++;
5119 case QEMU_OPTION_loadvm
:
5122 case QEMU_OPTION_full_screen
:
5126 case QEMU_OPTION_no_frame
:
5129 case QEMU_OPTION_alt_grab
:
5132 case QEMU_OPTION_no_quit
:
5135 case QEMU_OPTION_sdl
:
5139 case QEMU_OPTION_pidfile
:
5143 case QEMU_OPTION_win2k_hack
:
5144 win2k_install_hack
= 1;
5146 case QEMU_OPTION_rtc_td_hack
:
5149 case QEMU_OPTION_acpitable
:
5150 if(acpi_table_add(optarg
) < 0) {
5151 fprintf(stderr
, "Wrong acpi table provided\n");
5157 case QEMU_OPTION_no_kqemu
:
5160 case QEMU_OPTION_kernel_kqemu
:
5165 case QEMU_OPTION_enable_kvm
:
5172 case QEMU_OPTION_usb
:
5175 case QEMU_OPTION_usbdevice
:
5177 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5178 fprintf(stderr
, "Too many USB devices\n");
5181 usb_devices
[usb_devices_index
] = optarg
;
5182 usb_devices_index
++;
5184 case QEMU_OPTION_smp
:
5185 smp_cpus
= atoi(optarg
);
5187 fprintf(stderr
, "Invalid number of CPUs\n");
5191 case QEMU_OPTION_vnc
:
5192 vnc_display
= optarg
;
5194 case QEMU_OPTION_no_acpi
:
5197 case QEMU_OPTION_no_hpet
:
5200 case QEMU_OPTION_no_reboot
:
5203 case QEMU_OPTION_no_shutdown
:
5206 case QEMU_OPTION_show_cursor
:
5209 case QEMU_OPTION_uuid
:
5210 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5211 fprintf(stderr
, "Fail to parse UUID string."
5212 " Wrong format.\n");
5216 case QEMU_OPTION_daemonize
:
5219 case QEMU_OPTION_option_rom
:
5220 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5221 fprintf(stderr
, "Too many option ROMs\n");
5224 option_rom
[nb_option_roms
] = optarg
;
5227 case QEMU_OPTION_semihosting
:
5228 semihosting_enabled
= 1;
5230 case QEMU_OPTION_name
:
5233 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5234 case QEMU_OPTION_prom_env
:
5235 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5236 fprintf(stderr
, "Too many prom variables\n");
5239 prom_envs
[nb_prom_envs
] = optarg
;
5244 case QEMU_OPTION_old_param
:
5248 case QEMU_OPTION_clock
:
5249 configure_alarms(optarg
);
5251 case QEMU_OPTION_startdate
:
5254 time_t rtc_start_date
;
5255 if (!strcmp(optarg
, "now")) {
5256 rtc_date_offset
= -1;
5258 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5266 } else if (sscanf(optarg
, "%d-%d-%d",
5269 &tm
.tm_mday
) == 3) {
5278 rtc_start_date
= mktimegm(&tm
);
5279 if (rtc_start_date
== -1) {
5281 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5282 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5285 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5289 case QEMU_OPTION_tb_size
:
5290 tb_size
= strtol(optarg
, NULL
, 0);
5294 case QEMU_OPTION_icount
:
5296 if (strcmp(optarg
, "auto") == 0) {
5297 icount_time_shift
= -1;
5299 icount_time_shift
= strtol(optarg
, NULL
, 0);
5302 case QEMU_OPTION_incoming
:
5305 case QEMU_OPTION_chroot
:
5306 chroot_dir
= optarg
;
5308 case QEMU_OPTION_runas
:
5315 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5316 if (kvm_allowed
&& kqemu_allowed
) {
5318 "You can not enable both KVM and kqemu at the same time\n");
5323 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5324 if (smp_cpus
> machine
->max_cpus
) {
5325 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5326 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5332 if (serial_device_index
== 0)
5333 serial_devices
[0] = "stdio";
5334 if (parallel_device_index
== 0)
5335 parallel_devices
[0] = "null";
5336 if (strncmp(monitor_device
, "vc", 2) == 0)
5337 monitor_device
= "stdio";
5344 if (pipe(fds
) == -1)
5355 len
= read(fds
[0], &status
, 1);
5356 if (len
== -1 && (errno
== EINTR
))
5361 else if (status
== 1) {
5362 fprintf(stderr
, "Could not acquire pidfile\n");
5379 signal(SIGTSTP
, SIG_IGN
);
5380 signal(SIGTTOU
, SIG_IGN
);
5381 signal(SIGTTIN
, SIG_IGN
);
5385 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5388 write(fds
[1], &status
, 1);
5390 fprintf(stderr
, "Could not acquire pid file\n");
5398 linux_boot
= (kernel_filename
!= NULL
);
5399 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5401 if (!linux_boot
&& net_boot
== 0 &&
5402 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5405 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5406 fprintf(stderr
, "-append only allowed with -kernel option\n");
5410 if (!linux_boot
&& initrd_filename
!= NULL
) {
5411 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5415 /* boot to floppy or the default cd if no hard disk defined yet */
5416 if (!boot_devices
[0]) {
5417 boot_devices
= "cad";
5419 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5422 if (init_timer_alarm() < 0) {
5423 fprintf(stderr
, "could not initialize alarm timer\n");
5426 if (use_icount
&& icount_time_shift
< 0) {
5428 /* 125MIPS seems a reasonable initial guess at the guest speed.
5429 It will be corrected fairly quickly anyway. */
5430 icount_time_shift
= 3;
5431 init_icount_adjust();
5438 /* init network clients */
5439 if (nb_net_clients
== 0) {
5440 /* if no clients, we use a default config */
5441 net_clients
[nb_net_clients
++] = "nic";
5443 net_clients
[nb_net_clients
++] = "user";
5447 for(i
= 0;i
< nb_net_clients
; i
++) {
5448 if (net_client_parse(net_clients
[i
]) < 0)
5454 /* XXX: this should be moved in the PC machine instantiation code */
5455 if (net_boot
!= 0) {
5457 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5458 const char *model
= nd_table
[i
].model
;
5460 if (net_boot
& (1 << i
)) {
5463 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5464 if (get_image_size(buf
) > 0) {
5465 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5466 fprintf(stderr
, "Too many option ROMs\n");
5469 option_rom
[nb_option_roms
] = strdup(buf
);
5476 fprintf(stderr
, "No valid PXE rom found for network device\n");
5482 /* init the bluetooth world */
5483 for (i
= 0; i
< nb_bt_opts
; i
++)
5484 if (bt_parse(bt_opts
[i
]))
5487 /* init the memory */
5488 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5490 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5492 if (ram_size
< phys_ram_size
) {
5493 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5494 machine
->name
, (unsigned long long) phys_ram_size
);
5498 phys_ram_size
= ram_size
;
5500 ram_size
= phys_ram_size
;
5503 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5505 phys_ram_size
+= ram_size
;
5508 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5509 if (!phys_ram_base
) {
5510 fprintf(stderr
, "Could not allocate physical memory\n");
5514 /* init the dynamic translator */
5515 cpu_exec_init_all(tb_size
* 1024 * 1024);
5519 /* we always create the cdrom drive, even if no disk is there */
5521 if (nb_drives_opt
< MAX_DRIVES
)
5522 drive_add(NULL
, CDROM_ALIAS
);
5524 /* we always create at least one floppy */
5526 if (nb_drives_opt
< MAX_DRIVES
)
5527 drive_add(NULL
, FD_ALIAS
, 0);
5529 /* we always create one sd slot, even if no card is in it */
5531 if (nb_drives_opt
< MAX_DRIVES
)
5532 drive_add(NULL
, SD_ALIAS
);
5534 /* open the virtual block devices */
5536 for(i
= 0; i
< nb_drives_opt
; i
++)
5537 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5540 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5541 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5544 /* must be after terminal init, SDL library changes signal handlers */
5548 /* Maintain compatibility with multiple stdio monitors */
5549 if (!strcmp(monitor_device
,"stdio")) {
5550 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5551 const char *devname
= serial_devices
[i
];
5552 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5553 monitor_device
= NULL
;
5555 } else if (devname
&& !strcmp(devname
,"stdio")) {
5556 monitor_device
= NULL
;
5557 serial_devices
[i
] = "mon:stdio";
5563 if (kvm_enabled()) {
5566 ret
= kvm_init(smp_cpus
);
5568 fprintf(stderr
, "failed to initialize KVM\n");
5573 if (monitor_device
) {
5574 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5576 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5581 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5582 const char *devname
= serial_devices
[i
];
5583 if (devname
&& strcmp(devname
, "none")) {
5585 snprintf(label
, sizeof(label
), "serial%d", i
);
5586 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5587 if (!serial_hds
[i
]) {
5588 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5595 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5596 const char *devname
= parallel_devices
[i
];
5597 if (devname
&& strcmp(devname
, "none")) {
5599 snprintf(label
, sizeof(label
), "parallel%d", i
);
5600 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5601 if (!parallel_hds
[i
]) {
5602 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5609 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5610 const char *devname
= virtio_consoles
[i
];
5611 if (devname
&& strcmp(devname
, "none")) {
5613 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5614 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5615 if (!virtcon_hds
[i
]) {
5616 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5623 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5624 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5626 current_machine
= machine
;
5628 /* Set KVM's vcpu state to qemu's initial CPUState. */
5629 if (kvm_enabled()) {
5632 ret
= kvm_sync_vcpus();
5634 fprintf(stderr
, "failed to initialize vcpus\n");
5639 /* init USB devices */
5641 for(i
= 0; i
< usb_devices_index
; i
++) {
5642 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5643 fprintf(stderr
, "Warning: could not add USB device %s\n",
5650 dumb_display_init();
5651 /* just use the first displaystate for the moment */
5656 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5660 #if defined(CONFIG_CURSES)
5662 /* At the moment curses cannot be used with other displays */
5663 curses_display_init(ds
, full_screen
);
5667 if (vnc_display
!= NULL
) {
5668 vnc_display_init(ds
);
5669 if (vnc_display_open(ds
, vnc_display
) < 0)
5672 #if defined(CONFIG_SDL)
5673 if (sdl
|| !vnc_display
)
5674 sdl_display_init(ds
, full_screen
, no_frame
);
5675 #elif defined(CONFIG_COCOA)
5676 if (sdl
|| !vnc_display
)
5677 cocoa_display_init(ds
, full_screen
);
5683 dcl
= ds
->listeners
;
5684 while (dcl
!= NULL
) {
5685 if (dcl
->dpy_refresh
!= NULL
) {
5686 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5687 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5692 if (nographic
|| (vnc_display
&& !sdl
)) {
5693 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5694 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5697 text_consoles_set_display(display_state
);
5698 qemu_chr_initial_reset();
5700 if (monitor_device
&& monitor_hd
)
5701 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5703 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5704 const char *devname
= serial_devices
[i
];
5705 if (devname
&& strcmp(devname
, "none")) {
5707 snprintf(label
, sizeof(label
), "serial%d", i
);
5708 if (strstart(devname
, "vc", 0))
5709 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5713 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5714 const char *devname
= parallel_devices
[i
];
5715 if (devname
&& strcmp(devname
, "none")) {
5717 snprintf(label
, sizeof(label
), "parallel%d", i
);
5718 if (strstart(devname
, "vc", 0))
5719 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5723 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5724 const char *devname
= virtio_consoles
[i
];
5725 if (virtcon_hds
[i
] && devname
) {
5727 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5728 if (strstart(devname
, "vc", 0))
5729 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5733 #ifdef CONFIG_GDBSTUB
5735 /* XXX: use standard host:port notation and modify options
5737 if (gdbserver_start(gdbstub_port
) < 0) {
5738 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5746 do_loadvm(cur_mon
, loadvm
);
5749 autostart
= 0; /* fixme how to deal with -daemonize */
5750 qemu_start_incoming_migration(incoming
);
5761 len
= write(fds
[1], &status
, 1);
5762 if (len
== -1 && (errno
== EINTR
))
5769 TFR(fd
= open("/dev/null", O_RDWR
));
5776 pwd
= getpwnam(run_as
);
5778 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5784 if (chroot(chroot_dir
) < 0) {
5785 fprintf(stderr
, "chroot failed\n");
5792 if (setgid(pwd
->pw_gid
) < 0) {
5793 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5796 if (setuid(pwd
->pw_uid
) < 0) {
5797 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5800 if (setuid(0) != -1) {
5801 fprintf(stderr
, "Dropping privileges failed\n");