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 _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>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
116 int qemu_main(int argc
, char **argv
, char **envp
);
117 int main(int argc
, char **argv
)
119 qemu_main(argc
, argv
, NULL
);
122 #define main qemu_main
124 #endif /* CONFIG_SDL */
128 #define main qemu_main
129 #endif /* CONFIG_COCOA */
132 #include "hw/boards.h"
134 #include "hw/pcmcia.h"
136 #include "hw/audiodev.h"
145 #include "qemu-timer.h"
146 #include "qemu-char.h"
147 #include "cache-utils.h"
149 #include "audio/audio.h"
150 #include "migration.h"
156 #include "exec-all.h"
158 #include "qemu_socket.h"
160 #if defined(CONFIG_SLIRP)
161 #include "libslirp.h"
164 //#define DEBUG_UNUSED_IOPORT
165 //#define DEBUG_IOPORT
167 //#define DEBUG_SLIRP
171 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
173 # define LOG_IOPORT(...) do { } while (0)
176 #define DEFAULT_RAM_SIZE 128
178 /* Max number of USB devices that can be specified on the commandline. */
179 #define MAX_USB_CMDLINE 8
181 /* Max number of bluetooth switches on the commandline. */
182 #define MAX_BT_CMDLINE 10
184 /* XXX: use a two level table to limit memory usage */
185 #define MAX_IOPORTS 65536
187 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
188 const char *bios_name
= NULL
;
189 static void *ioport_opaque
[MAX_IOPORTS
];
190 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
191 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
192 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
193 to store the VM snapshots */
194 DriveInfo drives_table
[MAX_DRIVES
+1];
196 static int vga_ram_size
;
197 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
198 static DisplayState
*display_state
;
202 const char* keyboard_layout
= NULL
;
203 int64_t ticks_per_sec
;
206 NICInfo nd_table
[MAX_NICS
];
208 static int autostart
;
209 static int rtc_utc
= 1;
210 static int rtc_date_offset
= -1; /* -1 means no change */
211 int cirrus_vga_enabled
= 1;
212 int std_vga_enabled
= 0;
213 int vmsvga_enabled
= 0;
215 int graphic_width
= 1024;
216 int graphic_height
= 768;
217 int graphic_depth
= 8;
219 int graphic_width
= 800;
220 int graphic_height
= 600;
221 int graphic_depth
= 15;
223 static int full_screen
= 0;
225 static int no_frame
= 0;
228 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
229 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
230 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
232 int win2k_install_hack
= 0;
237 const char *vnc_display
;
238 int acpi_enabled
= 1;
244 int graphic_rotate
= 0;
246 const char *option_rom
[MAX_OPTION_ROMS
];
248 int semihosting_enabled
= 0;
252 const char *qemu_name
;
254 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
255 unsigned int nb_prom_envs
= 0;
256 const char *prom_envs
[MAX_PROM_ENVS
];
259 struct drive_opt drives_opt
[MAX_DRIVES
];
261 static CPUState
*cur_cpu
;
262 static CPUState
*next_cpu
;
263 static int event_pending
= 1;
264 /* Conversion factor from emulated instructions to virtual clock ticks. */
265 static int icount_time_shift
;
266 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
267 #define MAX_ICOUNT_SHIFT 10
268 /* Compensate for varying guest execution speed. */
269 static int64_t qemu_icount_bias
;
270 static QEMUTimer
*icount_rt_timer
;
271 static QEMUTimer
*icount_vm_timer
;
272 static QEMUTimer
*nographic_timer
;
274 uint8_t qemu_uuid
[16];
276 /***********************************************************/
277 /* x86 ISA bus support */
279 target_phys_addr_t isa_mem_base
= 0;
282 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
283 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
285 static uint32_t ioport_read(int index
, uint32_t address
)
287 static IOPortReadFunc
*default_func
[3] = {
288 default_ioport_readb
,
289 default_ioport_readw
,
292 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
294 func
= default_func
[index
];
295 return func(ioport_opaque
[address
], address
);
298 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
300 static IOPortWriteFunc
*default_func
[3] = {
301 default_ioport_writeb
,
302 default_ioport_writew
,
303 default_ioport_writel
305 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
307 func
= default_func
[index
];
308 func(ioport_opaque
[address
], address
, data
);
311 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
313 #ifdef DEBUG_UNUSED_IOPORT
314 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
319 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
321 #ifdef DEBUG_UNUSED_IOPORT
322 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
326 /* default is to make two byte accesses */
327 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
330 data
= ioport_read(0, address
);
331 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
332 data
|= ioport_read(0, address
) << 8;
336 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
338 ioport_write(0, address
, data
& 0xff);
339 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
340 ioport_write(0, address
, (data
>> 8) & 0xff);
343 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
345 #ifdef DEBUG_UNUSED_IOPORT
346 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
351 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
353 #ifdef DEBUG_UNUSED_IOPORT
354 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
358 /* size is the word size in byte */
359 int register_ioport_read(int start
, int length
, int size
,
360 IOPortReadFunc
*func
, void *opaque
)
366 } else if (size
== 2) {
368 } else if (size
== 4) {
371 hw_error("register_ioport_read: invalid size");
374 for(i
= start
; i
< start
+ length
; i
+= size
) {
375 ioport_read_table
[bsize
][i
] = func
;
376 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
377 hw_error("register_ioport_read: invalid opaque");
378 ioport_opaque
[i
] = opaque
;
383 /* size is the word size in byte */
384 int register_ioport_write(int start
, int length
, int size
,
385 IOPortWriteFunc
*func
, void *opaque
)
391 } else if (size
== 2) {
393 } else if (size
== 4) {
396 hw_error("register_ioport_write: invalid size");
399 for(i
= start
; i
< start
+ length
; i
+= size
) {
400 ioport_write_table
[bsize
][i
] = func
;
401 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
402 hw_error("register_ioport_write: invalid opaque");
403 ioport_opaque
[i
] = opaque
;
408 void isa_unassign_ioport(int start
, int length
)
412 for(i
= start
; i
< start
+ length
; i
++) {
413 ioport_read_table
[0][i
] = default_ioport_readb
;
414 ioport_read_table
[1][i
] = default_ioport_readw
;
415 ioport_read_table
[2][i
] = default_ioport_readl
;
417 ioport_write_table
[0][i
] = default_ioport_writeb
;
418 ioport_write_table
[1][i
] = default_ioport_writew
;
419 ioport_write_table
[2][i
] = default_ioport_writel
;
421 ioport_opaque
[i
] = NULL
;
425 /***********************************************************/
427 void cpu_outb(CPUState
*env
, int addr
, int val
)
429 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
430 ioport_write(0, addr
, val
);
433 env
->last_io_time
= cpu_get_time_fast();
437 void cpu_outw(CPUState
*env
, int addr
, int val
)
439 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
440 ioport_write(1, addr
, val
);
443 env
->last_io_time
= cpu_get_time_fast();
447 void cpu_outl(CPUState
*env
, int addr
, int val
)
449 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
450 ioport_write(2, addr
, val
);
453 env
->last_io_time
= cpu_get_time_fast();
457 int cpu_inb(CPUState
*env
, int addr
)
460 val
= ioport_read(0, addr
);
461 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
464 env
->last_io_time
= cpu_get_time_fast();
469 int cpu_inw(CPUState
*env
, int addr
)
472 val
= ioport_read(1, addr
);
473 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
476 env
->last_io_time
= cpu_get_time_fast();
481 int cpu_inl(CPUState
*env
, int addr
)
484 val
= ioport_read(2, addr
);
485 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
488 env
->last_io_time
= cpu_get_time_fast();
493 /***********************************************************/
494 void hw_error(const char *fmt
, ...)
500 fprintf(stderr
, "qemu: hardware error: ");
501 vfprintf(stderr
, fmt
, ap
);
502 fprintf(stderr
, "\n");
503 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
504 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
506 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
508 cpu_dump_state(env
, stderr
, fprintf
, 0);
518 static QEMUBalloonEvent
*qemu_balloon_event
;
519 void *qemu_balloon_event_opaque
;
521 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
523 qemu_balloon_event
= func
;
524 qemu_balloon_event_opaque
= opaque
;
527 void qemu_balloon(ram_addr_t target
)
529 if (qemu_balloon_event
)
530 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
533 ram_addr_t
qemu_balloon_status(void)
535 if (qemu_balloon_event
)
536 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
540 /***********************************************************/
543 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
544 static void *qemu_put_kbd_event_opaque
;
545 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
546 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
548 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
550 qemu_put_kbd_event_opaque
= opaque
;
551 qemu_put_kbd_event
= func
;
554 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
555 void *opaque
, int absolute
,
558 QEMUPutMouseEntry
*s
, *cursor
;
560 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
562 s
->qemu_put_mouse_event
= func
;
563 s
->qemu_put_mouse_event_opaque
= opaque
;
564 s
->qemu_put_mouse_event_absolute
= absolute
;
565 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
568 if (!qemu_put_mouse_event_head
) {
569 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
573 cursor
= qemu_put_mouse_event_head
;
574 while (cursor
->next
!= NULL
)
575 cursor
= cursor
->next
;
578 qemu_put_mouse_event_current
= s
;
583 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
585 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
587 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
590 cursor
= qemu_put_mouse_event_head
;
591 while (cursor
!= NULL
&& cursor
!= entry
) {
593 cursor
= cursor
->next
;
596 if (cursor
== NULL
) // does not exist or list empty
598 else if (prev
== NULL
) { // entry is head
599 qemu_put_mouse_event_head
= cursor
->next
;
600 if (qemu_put_mouse_event_current
== entry
)
601 qemu_put_mouse_event_current
= cursor
->next
;
602 qemu_free(entry
->qemu_put_mouse_event_name
);
607 prev
->next
= entry
->next
;
609 if (qemu_put_mouse_event_current
== entry
)
610 qemu_put_mouse_event_current
= prev
;
612 qemu_free(entry
->qemu_put_mouse_event_name
);
616 void kbd_put_keycode(int keycode
)
618 if (qemu_put_kbd_event
) {
619 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
623 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
625 QEMUPutMouseEvent
*mouse_event
;
626 void *mouse_event_opaque
;
629 if (!qemu_put_mouse_event_current
) {
634 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
636 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
639 if (graphic_rotate
) {
640 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
643 width
= graphic_width
- 1;
644 mouse_event(mouse_event_opaque
,
645 width
- dy
, dx
, dz
, buttons_state
);
647 mouse_event(mouse_event_opaque
,
648 dx
, dy
, dz
, buttons_state
);
652 int kbd_mouse_is_absolute(void)
654 if (!qemu_put_mouse_event_current
)
657 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
660 void do_info_mice(Monitor
*mon
)
662 QEMUPutMouseEntry
*cursor
;
665 if (!qemu_put_mouse_event_head
) {
666 monitor_printf(mon
, "No mouse devices connected\n");
670 monitor_printf(mon
, "Mouse devices available:\n");
671 cursor
= qemu_put_mouse_event_head
;
672 while (cursor
!= NULL
) {
673 monitor_printf(mon
, "%c Mouse #%d: %s\n",
674 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
675 index
, cursor
->qemu_put_mouse_event_name
);
677 cursor
= cursor
->next
;
681 void do_mouse_set(Monitor
*mon
, int index
)
683 QEMUPutMouseEntry
*cursor
;
686 if (!qemu_put_mouse_event_head
) {
687 monitor_printf(mon
, "No mouse devices connected\n");
691 cursor
= qemu_put_mouse_event_head
;
692 while (cursor
!= NULL
&& index
!= i
) {
694 cursor
= cursor
->next
;
698 qemu_put_mouse_event_current
= cursor
;
700 monitor_printf(mon
, "Mouse at given index not found\n");
703 /* compute with 96 bit intermediate result: (a*b)/c */
704 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
709 #ifdef WORDS_BIGENDIAN
719 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
720 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
723 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
727 /***********************************************************/
728 /* real time host monotonic timer */
730 #define QEMU_TIMER_BASE 1000000000LL
734 static int64_t clock_freq
;
736 static void init_get_clock(void)
740 ret
= QueryPerformanceFrequency(&freq
);
742 fprintf(stderr
, "Could not calibrate ticks\n");
745 clock_freq
= freq
.QuadPart
;
748 static int64_t get_clock(void)
751 QueryPerformanceCounter(&ti
);
752 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
757 static int use_rt_clock
;
759 static void init_get_clock(void)
762 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
763 || defined(__DragonFly__)
766 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
773 static int64_t get_clock(void)
775 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
776 || defined(__DragonFly__)
779 clock_gettime(CLOCK_MONOTONIC
, &ts
);
780 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
784 /* XXX: using gettimeofday leads to problems if the date
785 changes, so it should be avoided. */
787 gettimeofday(&tv
, NULL
);
788 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
793 /* Return the virtual CPU time, based on the instruction counter. */
794 static int64_t cpu_get_icount(void)
797 CPUState
*env
= cpu_single_env
;;
798 icount
= qemu_icount
;
801 fprintf(stderr
, "Bad clock read\n");
802 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
804 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
807 /***********************************************************/
808 /* guest cycle counter */
810 static int64_t cpu_ticks_prev
;
811 static int64_t cpu_ticks_offset
;
812 static int64_t cpu_clock_offset
;
813 static int cpu_ticks_enabled
;
815 /* return the host CPU cycle counter and handle stop/restart */
816 int64_t cpu_get_ticks(void)
819 return cpu_get_icount();
821 if (!cpu_ticks_enabled
) {
822 return cpu_ticks_offset
;
825 ticks
= cpu_get_real_ticks();
826 if (cpu_ticks_prev
> ticks
) {
827 /* Note: non increasing ticks may happen if the host uses
829 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
831 cpu_ticks_prev
= ticks
;
832 return ticks
+ cpu_ticks_offset
;
836 /* return the host CPU monotonic timer and handle stop/restart */
837 static int64_t cpu_get_clock(void)
840 if (!cpu_ticks_enabled
) {
841 return cpu_clock_offset
;
844 return ti
+ cpu_clock_offset
;
848 /* enable cpu_get_ticks() */
849 void cpu_enable_ticks(void)
851 if (!cpu_ticks_enabled
) {
852 cpu_ticks_offset
-= cpu_get_real_ticks();
853 cpu_clock_offset
-= get_clock();
854 cpu_ticks_enabled
= 1;
858 /* disable cpu_get_ticks() : the clock is stopped. You must not call
859 cpu_get_ticks() after that. */
860 void cpu_disable_ticks(void)
862 if (cpu_ticks_enabled
) {
863 cpu_ticks_offset
= cpu_get_ticks();
864 cpu_clock_offset
= cpu_get_clock();
865 cpu_ticks_enabled
= 0;
869 /***********************************************************/
872 #define QEMU_TIMER_REALTIME 0
873 #define QEMU_TIMER_VIRTUAL 1
877 /* XXX: add frequency */
885 struct QEMUTimer
*next
;
888 struct qemu_alarm_timer
{
892 int (*start
)(struct qemu_alarm_timer
*t
);
893 void (*stop
)(struct qemu_alarm_timer
*t
);
894 void (*rearm
)(struct qemu_alarm_timer
*t
);
898 #define ALARM_FLAG_DYNTICKS 0x1
899 #define ALARM_FLAG_EXPIRED 0x2
901 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
903 return t
->flags
& ALARM_FLAG_DYNTICKS
;
906 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
908 if (!alarm_has_dynticks(t
))
914 /* TODO: MIN_TIMER_REARM_US should be optimized */
915 #define MIN_TIMER_REARM_US 250
917 static struct qemu_alarm_timer
*alarm_timer
;
919 static int alarm_timer_rfd
, alarm_timer_wfd
;
924 struct qemu_alarm_win32
{
928 } alarm_win32_data
= {0, NULL
, -1};
930 static int win32_start_timer(struct qemu_alarm_timer
*t
);
931 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
932 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
936 static int unix_start_timer(struct qemu_alarm_timer
*t
);
937 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
941 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
942 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
943 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
945 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
946 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
948 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
949 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
951 #endif /* __linux__ */
955 /* Correlation between real and virtual time is always going to be
956 fairly approximate, so ignore small variation.
957 When the guest is idle real and virtual time will be aligned in
959 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
961 static void icount_adjust(void)
966 static int64_t last_delta
;
967 /* If the VM is not running, then do nothing. */
971 cur_time
= cpu_get_clock();
972 cur_icount
= qemu_get_clock(vm_clock
);
973 delta
= cur_icount
- cur_time
;
974 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
976 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
977 && icount_time_shift
> 0) {
978 /* The guest is getting too far ahead. Slow time down. */
982 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
983 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
984 /* The guest is getting too far behind. Speed time up. */
988 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
991 static void icount_adjust_rt(void * opaque
)
993 qemu_mod_timer(icount_rt_timer
,
994 qemu_get_clock(rt_clock
) + 1000);
998 static void icount_adjust_vm(void * opaque
)
1000 qemu_mod_timer(icount_vm_timer
,
1001 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1005 static void init_icount_adjust(void)
1007 /* Have both realtime and virtual time triggers for speed adjustment.
1008 The realtime trigger catches emulated time passing too slowly,
1009 the virtual time trigger catches emulated time passing too fast.
1010 Realtime triggers occur even when idle, so use them less frequently
1011 than VM triggers. */
1012 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1013 qemu_mod_timer(icount_rt_timer
,
1014 qemu_get_clock(rt_clock
) + 1000);
1015 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1016 qemu_mod_timer(icount_vm_timer
,
1017 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1020 static struct qemu_alarm_timer alarm_timers
[] = {
1023 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1024 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1025 /* HPET - if available - is preferred */
1026 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1027 /* ...otherwise try RTC */
1028 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1030 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1032 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1033 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1034 {"win32", 0, win32_start_timer
,
1035 win32_stop_timer
, NULL
, &alarm_win32_data
},
1040 static void show_available_alarms(void)
1044 printf("Available alarm timers, in order of precedence:\n");
1045 for (i
= 0; alarm_timers
[i
].name
; i
++)
1046 printf("%s\n", alarm_timers
[i
].name
);
1049 static void configure_alarms(char const *opt
)
1053 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1056 struct qemu_alarm_timer tmp
;
1058 if (!strcmp(opt
, "?")) {
1059 show_available_alarms();
1065 /* Reorder the array */
1066 name
= strtok(arg
, ",");
1068 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1069 if (!strcmp(alarm_timers
[i
].name
, name
))
1074 fprintf(stderr
, "Unknown clock %s\n", name
);
1083 tmp
= alarm_timers
[i
];
1084 alarm_timers
[i
] = alarm_timers
[cur
];
1085 alarm_timers
[cur
] = tmp
;
1089 name
= strtok(NULL
, ",");
1095 /* Disable remaining timers */
1096 for (i
= cur
; i
< count
; i
++)
1097 alarm_timers
[i
].name
= NULL
;
1099 show_available_alarms();
1104 QEMUClock
*rt_clock
;
1105 QEMUClock
*vm_clock
;
1107 static QEMUTimer
*active_timers
[2];
1109 static QEMUClock
*qemu_new_clock(int type
)
1112 clock
= qemu_mallocz(sizeof(QEMUClock
));
1117 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1121 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1124 ts
->opaque
= opaque
;
1128 void qemu_free_timer(QEMUTimer
*ts
)
1133 /* stop a timer, but do not dealloc it */
1134 void qemu_del_timer(QEMUTimer
*ts
)
1138 /* NOTE: this code must be signal safe because
1139 qemu_timer_expired() can be called from a signal. */
1140 pt
= &active_timers
[ts
->clock
->type
];
1153 /* modify the current timer so that it will be fired when current_time
1154 >= expire_time. The corresponding callback will be called. */
1155 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1161 /* add the timer in the sorted list */
1162 /* NOTE: this code must be signal safe because
1163 qemu_timer_expired() can be called from a signal. */
1164 pt
= &active_timers
[ts
->clock
->type
];
1169 if (t
->expire_time
> expire_time
)
1173 ts
->expire_time
= expire_time
;
1177 /* Rearm if necessary */
1178 if (pt
== &active_timers
[ts
->clock
->type
]) {
1179 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1180 qemu_rearm_alarm_timer(alarm_timer
);
1182 /* Interrupt execution to force deadline recalculation. */
1183 if (use_icount
&& cpu_single_env
) {
1184 cpu_exit(cpu_single_env
);
1189 int qemu_timer_pending(QEMUTimer
*ts
)
1192 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1199 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1203 return (timer_head
->expire_time
<= current_time
);
1206 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1212 if (!ts
|| ts
->expire_time
> current_time
)
1214 /* remove timer from the list before calling the callback */
1215 *ptimer_head
= ts
->next
;
1218 /* run the callback (the timer list can be modified) */
1223 int64_t qemu_get_clock(QEMUClock
*clock
)
1225 switch(clock
->type
) {
1226 case QEMU_TIMER_REALTIME
:
1227 return get_clock() / 1000000;
1229 case QEMU_TIMER_VIRTUAL
:
1231 return cpu_get_icount();
1233 return cpu_get_clock();
1238 static void init_timers(void)
1241 ticks_per_sec
= QEMU_TIMER_BASE
;
1242 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1243 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1247 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1249 uint64_t expire_time
;
1251 if (qemu_timer_pending(ts
)) {
1252 expire_time
= ts
->expire_time
;
1256 qemu_put_be64(f
, expire_time
);
1259 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1261 uint64_t expire_time
;
1263 expire_time
= qemu_get_be64(f
);
1264 if (expire_time
!= -1) {
1265 qemu_mod_timer(ts
, expire_time
);
1271 static void timer_save(QEMUFile
*f
, void *opaque
)
1273 if (cpu_ticks_enabled
) {
1274 hw_error("cannot save state if virtual timers are running");
1276 qemu_put_be64(f
, cpu_ticks_offset
);
1277 qemu_put_be64(f
, ticks_per_sec
);
1278 qemu_put_be64(f
, cpu_clock_offset
);
1281 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1283 if (version_id
!= 1 && version_id
!= 2)
1285 if (cpu_ticks_enabled
) {
1288 cpu_ticks_offset
=qemu_get_be64(f
);
1289 ticks_per_sec
=qemu_get_be64(f
);
1290 if (version_id
== 2) {
1291 cpu_clock_offset
=qemu_get_be64(f
);
1297 void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1298 DWORD_PTR dwUser
, DWORD_PTR dw1
, DWORD_PTR dw2
)
1300 static void host_alarm_handler(int host_signum
)
1304 #define DISP_FREQ 1000
1306 static int64_t delta_min
= INT64_MAX
;
1307 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1309 ti
= qemu_get_clock(vm_clock
);
1310 if (last_clock
!= 0) {
1311 delta
= ti
- last_clock
;
1312 if (delta
< delta_min
)
1314 if (delta
> delta_max
)
1317 if (++count
== DISP_FREQ
) {
1318 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1319 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1320 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1321 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1322 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1324 delta_min
= INT64_MAX
;
1332 if (alarm_has_dynticks(alarm_timer
) ||
1334 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1335 qemu_get_clock(vm_clock
))) ||
1336 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1337 qemu_get_clock(rt_clock
))) {
1338 CPUState
*env
= next_cpu
;
1341 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1342 SetEvent(data
->host_alarm
);
1344 static const char byte
= 0;
1345 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1347 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1350 /* stop the currently executing cpu because a timer occured */
1353 if (env
->kqemu_enabled
) {
1354 kqemu_cpu_interrupt(env
);
1362 static int64_t qemu_next_deadline(void)
1366 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1367 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1368 qemu_get_clock(vm_clock
);
1370 /* To avoid problems with overflow limit this to 2^32. */
1380 #if defined(__linux__) || defined(_WIN32)
1381 static uint64_t qemu_next_deadline_dyntick(void)
1389 delta
= (qemu_next_deadline() + 999) / 1000;
1391 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1392 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1393 qemu_get_clock(rt_clock
))*1000;
1394 if (rtdelta
< delta
)
1398 if (delta
< MIN_TIMER_REARM_US
)
1399 delta
= MIN_TIMER_REARM_US
;
1407 /* Sets a specific flag */
1408 static int fcntl_setfl(int fd
, int flag
)
1412 flags
= fcntl(fd
, F_GETFL
);
1416 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1422 #if defined(__linux__)
1424 #define RTC_FREQ 1024
1426 static void enable_sigio_timer(int fd
)
1428 struct sigaction act
;
1431 sigfillset(&act
.sa_mask
);
1433 act
.sa_handler
= host_alarm_handler
;
1435 sigaction(SIGIO
, &act
, NULL
);
1436 fcntl_setfl(fd
, O_ASYNC
);
1437 fcntl(fd
, F_SETOWN
, getpid());
1440 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1442 struct hpet_info info
;
1445 fd
= open("/dev/hpet", O_RDONLY
);
1450 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1452 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1453 "error, but for better emulation accuracy type:\n"
1454 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1458 /* Check capabilities */
1459 r
= ioctl(fd
, HPET_INFO
, &info
);
1463 /* Enable periodic mode */
1464 r
= ioctl(fd
, HPET_EPI
, 0);
1465 if (info
.hi_flags
&& (r
< 0))
1468 /* Enable interrupt */
1469 r
= ioctl(fd
, HPET_IE_ON
, 0);
1473 enable_sigio_timer(fd
);
1474 t
->priv
= (void *)(long)fd
;
1482 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1484 int fd
= (long)t
->priv
;
1489 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1492 unsigned long current_rtc_freq
= 0;
1494 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1497 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1498 if (current_rtc_freq
!= RTC_FREQ
&&
1499 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1500 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1501 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1502 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1505 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1511 enable_sigio_timer(rtc_fd
);
1513 t
->priv
= (void *)(long)rtc_fd
;
1518 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1520 int rtc_fd
= (long)t
->priv
;
1525 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1529 struct sigaction act
;
1531 sigfillset(&act
.sa_mask
);
1533 act
.sa_handler
= host_alarm_handler
;
1535 sigaction(SIGALRM
, &act
, NULL
);
1537 ev
.sigev_value
.sival_int
= 0;
1538 ev
.sigev_notify
= SIGEV_SIGNAL
;
1539 ev
.sigev_signo
= SIGALRM
;
1541 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1542 perror("timer_create");
1544 /* disable dynticks */
1545 fprintf(stderr
, "Dynamic Ticks disabled\n");
1550 t
->priv
= (void *)(long)host_timer
;
1555 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1557 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1559 timer_delete(host_timer
);
1562 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1564 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1565 struct itimerspec timeout
;
1566 int64_t nearest_delta_us
= INT64_MAX
;
1569 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1570 !active_timers
[QEMU_TIMER_VIRTUAL
])
1573 nearest_delta_us
= qemu_next_deadline_dyntick();
1575 /* check whether a timer is already running */
1576 if (timer_gettime(host_timer
, &timeout
)) {
1578 fprintf(stderr
, "Internal timer error: aborting\n");
1581 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1582 if (current_us
&& current_us
<= nearest_delta_us
)
1585 timeout
.it_interval
.tv_sec
= 0;
1586 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1587 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1588 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1589 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1591 fprintf(stderr
, "Internal timer error: aborting\n");
1596 #endif /* defined(__linux__) */
1598 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1600 struct sigaction act
;
1601 struct itimerval itv
;
1605 sigfillset(&act
.sa_mask
);
1607 act
.sa_handler
= host_alarm_handler
;
1609 sigaction(SIGALRM
, &act
, NULL
);
1611 itv
.it_interval
.tv_sec
= 0;
1612 /* for i386 kernel 2.6 to get 1 ms */
1613 itv
.it_interval
.tv_usec
= 999;
1614 itv
.it_value
.tv_sec
= 0;
1615 itv
.it_value
.tv_usec
= 10 * 1000;
1617 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1624 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1626 struct itimerval itv
;
1628 memset(&itv
, 0, sizeof(itv
));
1629 setitimer(ITIMER_REAL
, &itv
, NULL
);
1632 #endif /* !defined(_WIN32) */
1634 static void try_to_rearm_timer(void *opaque
)
1636 struct qemu_alarm_timer
*t
= opaque
;
1640 /* Drain the notify pipe */
1643 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1644 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1647 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1648 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1649 qemu_rearm_alarm_timer(alarm_timer
);
1655 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1658 struct qemu_alarm_win32
*data
= t
->priv
;
1661 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1662 if (!data
->host_alarm
) {
1663 perror("Failed CreateEvent");
1667 memset(&tc
, 0, sizeof(tc
));
1668 timeGetDevCaps(&tc
, sizeof(tc
));
1670 if (data
->period
< tc
.wPeriodMin
)
1671 data
->period
= tc
.wPeriodMin
;
1673 timeBeginPeriod(data
->period
);
1675 flags
= TIME_CALLBACK_FUNCTION
;
1676 if (alarm_has_dynticks(t
))
1677 flags
|= TIME_ONESHOT
;
1679 flags
|= TIME_PERIODIC
;
1681 data
->timerId
= timeSetEvent(1, // interval (ms)
1682 data
->period
, // resolution
1683 host_alarm_handler
, // function
1684 (DWORD
)t
, // parameter
1687 if (!data
->timerId
) {
1688 perror("Failed to initialize win32 alarm timer");
1690 timeEndPeriod(data
->period
);
1691 CloseHandle(data
->host_alarm
);
1695 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1700 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1702 struct qemu_alarm_win32
*data
= t
->priv
;
1704 timeKillEvent(data
->timerId
);
1705 timeEndPeriod(data
->period
);
1707 CloseHandle(data
->host_alarm
);
1710 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1712 struct qemu_alarm_win32
*data
= t
->priv
;
1713 uint64_t nearest_delta_us
;
1715 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1716 !active_timers
[QEMU_TIMER_VIRTUAL
])
1719 nearest_delta_us
= qemu_next_deadline_dyntick();
1720 nearest_delta_us
/= 1000;
1722 timeKillEvent(data
->timerId
);
1724 data
->timerId
= timeSetEvent(1,
1728 TIME_ONESHOT
| TIME_PERIODIC
);
1730 if (!data
->timerId
) {
1731 perror("Failed to re-arm win32 alarm timer");
1733 timeEndPeriod(data
->period
);
1734 CloseHandle(data
->host_alarm
);
1741 static int init_timer_alarm(void)
1743 struct qemu_alarm_timer
*t
= NULL
;
1753 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1757 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1761 alarm_timer_rfd
= fds
[0];
1762 alarm_timer_wfd
= fds
[1];
1765 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1766 t
= &alarm_timers
[i
];
1779 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1780 try_to_rearm_timer
, NULL
, t
);
1795 static void quit_timers(void)
1797 alarm_timer
->stop(alarm_timer
);
1801 /***********************************************************/
1802 /* host time/date access */
1803 void qemu_get_timedate(struct tm
*tm
, int offset
)
1810 if (rtc_date_offset
== -1) {
1814 ret
= localtime(&ti
);
1816 ti
-= rtc_date_offset
;
1820 memcpy(tm
, ret
, sizeof(struct tm
));
1823 int qemu_timedate_diff(struct tm
*tm
)
1827 if (rtc_date_offset
== -1)
1829 seconds
= mktimegm(tm
);
1831 seconds
= mktime(tm
);
1833 seconds
= mktimegm(tm
) + rtc_date_offset
;
1835 return seconds
- time(NULL
);
1839 static void socket_cleanup(void)
1844 static int socket_init(void)
1849 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1851 err
= WSAGetLastError();
1852 fprintf(stderr
, "WSAStartup: %d\n", err
);
1855 atexit(socket_cleanup
);
1860 const char *get_opt_name(char *buf
, int buf_size
, const char *p
)
1865 while (*p
!= '\0' && *p
!= '=') {
1866 if (q
&& (q
- buf
) < buf_size
- 1)
1876 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1881 while (*p
!= '\0') {
1883 if (*(p
+ 1) != ',')
1887 if (q
&& (q
- buf
) < buf_size
- 1)
1897 int get_param_value(char *buf
, int buf_size
,
1898 const char *tag
, const char *str
)
1905 p
= get_opt_name(option
, sizeof(option
), p
);
1909 if (!strcmp(tag
, option
)) {
1910 (void)get_opt_value(buf
, buf_size
, p
);
1913 p
= get_opt_value(NULL
, 0, p
);
1922 int check_params(char *buf
, int buf_size
,
1923 const char * const *params
, const char *str
)
1930 p
= get_opt_name(buf
, buf_size
, p
);
1934 for(i
= 0; params
[i
] != NULL
; i
++)
1935 if (!strcmp(params
[i
], buf
))
1937 if (params
[i
] == NULL
)
1939 p
= get_opt_value(NULL
, 0, p
);
1947 /***********************************************************/
1948 /* Bluetooth support */
1951 static struct HCIInfo
*hci_table
[MAX_NICS
];
1953 static struct bt_vlan_s
{
1954 struct bt_scatternet_s net
;
1956 struct bt_vlan_s
*next
;
1959 /* find or alloc a new bluetooth "VLAN" */
1960 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1962 struct bt_vlan_s
**pvlan
, *vlan
;
1963 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1967 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1969 pvlan
= &first_bt_vlan
;
1970 while (*pvlan
!= NULL
)
1971 pvlan
= &(*pvlan
)->next
;
1976 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1980 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1985 static struct HCIInfo null_hci
= {
1986 .cmd_send
= null_hci_send
,
1987 .sco_send
= null_hci_send
,
1988 .acl_send
= null_hci_send
,
1989 .bdaddr_set
= null_hci_addr_set
,
1992 struct HCIInfo
*qemu_next_hci(void)
1994 if (cur_hci
== nb_hcis
)
1997 return hci_table
[cur_hci
++];
2000 static struct HCIInfo
*hci_init(const char *str
)
2003 struct bt_scatternet_s
*vlan
= 0;
2005 if (!strcmp(str
, "null"))
2008 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2010 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2011 else if (!strncmp(str
, "hci", 3)) {
2014 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2015 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2020 vlan
= qemu_find_bt_vlan(0);
2022 return bt_new_hci(vlan
);
2025 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2030 static int bt_hci_parse(const char *str
)
2032 struct HCIInfo
*hci
;
2035 if (nb_hcis
>= MAX_NICS
) {
2036 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2040 hci
= hci_init(str
);
2049 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2050 hci
->bdaddr_set(hci
, bdaddr
.b
);
2052 hci_table
[nb_hcis
++] = hci
;
2057 static void bt_vhci_add(int vlan_id
)
2059 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2062 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2063 "an empty scatternet %i\n", vlan_id
);
2065 bt_vhci_init(bt_new_hci(vlan
));
2068 static struct bt_device_s
*bt_device_add(const char *opt
)
2070 struct bt_scatternet_s
*vlan
;
2072 char *endp
= strstr(opt
, ",vlan=");
2073 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2076 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2079 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2081 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2086 vlan
= qemu_find_bt_vlan(vlan_id
);
2089 fprintf(stderr
, "qemu: warning: adding a slave device to "
2090 "an empty scatternet %i\n", vlan_id
);
2092 if (!strcmp(devname
, "keyboard"))
2093 return bt_keyboard_init(vlan
);
2095 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2099 static int bt_parse(const char *opt
)
2101 const char *endp
, *p
;
2104 if (strstart(opt
, "hci", &endp
)) {
2105 if (!*endp
|| *endp
== ',') {
2107 if (!strstart(endp
, ",vlan=", 0))
2110 return bt_hci_parse(opt
);
2112 } else if (strstart(opt
, "vhci", &endp
)) {
2113 if (!*endp
|| *endp
== ',') {
2115 if (strstart(endp
, ",vlan=", &p
)) {
2116 vlan
= strtol(p
, (char **) &endp
, 0);
2118 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2122 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2131 } else if (strstart(opt
, "device:", &endp
))
2132 return !bt_device_add(endp
);
2134 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2138 /***********************************************************/
2139 /* QEMU Block devices */
2141 #define HD_ALIAS "index=%d,media=disk"
2142 #define CDROM_ALIAS "index=2,media=cdrom"
2143 #define FD_ALIAS "index=%d,if=floppy"
2144 #define PFLASH_ALIAS "if=pflash"
2145 #define MTD_ALIAS "if=mtd"
2146 #define SD_ALIAS "index=0,if=sd"
2148 static int drive_opt_get_free_idx(void)
2152 for (index
= 0; index
< MAX_DRIVES
; index
++)
2153 if (!drives_opt
[index
].used
) {
2154 drives_opt
[index
].used
= 1;
2161 static int drive_get_free_idx(void)
2165 for (index
= 0; index
< MAX_DRIVES
; index
++)
2166 if (!drives_table
[index
].used
) {
2167 drives_table
[index
].used
= 1;
2174 int drive_add(const char *file
, const char *fmt
, ...)
2177 int index
= drive_opt_get_free_idx();
2179 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2180 fprintf(stderr
, "qemu: too many drives\n");
2184 drives_opt
[index
].file
= file
;
2186 vsnprintf(drives_opt
[index
].opt
,
2187 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2194 void drive_remove(int index
)
2196 drives_opt
[index
].used
= 0;
2200 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2204 /* seek interface, bus and unit */
2206 for (index
= 0; index
< MAX_DRIVES
; index
++)
2207 if (drives_table
[index
].type
== type
&&
2208 drives_table
[index
].bus
== bus
&&
2209 drives_table
[index
].unit
== unit
&&
2210 drives_table
[index
].used
)
2216 int drive_get_max_bus(BlockInterfaceType type
)
2222 for (index
= 0; index
< nb_drives
; index
++) {
2223 if(drives_table
[index
].type
== type
&&
2224 drives_table
[index
].bus
> max_bus
)
2225 max_bus
= drives_table
[index
].bus
;
2230 const char *drive_get_serial(BlockDriverState
*bdrv
)
2234 for (index
= 0; index
< nb_drives
; index
++)
2235 if (drives_table
[index
].bdrv
== bdrv
)
2236 return drives_table
[index
].serial
;
2241 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2245 for (index
= 0; index
< nb_drives
; index
++)
2246 if (drives_table
[index
].bdrv
== bdrv
)
2247 return drives_table
[index
].onerror
;
2249 return BLOCK_ERR_STOP_ENOSPC
;
2252 static void bdrv_format_print(void *opaque
, const char *name
)
2254 fprintf(stderr
, " %s", name
);
2257 void drive_uninit(BlockDriverState
*bdrv
)
2261 for (i
= 0; i
< MAX_DRIVES
; i
++)
2262 if (drives_table
[i
].bdrv
== bdrv
) {
2263 drives_table
[i
].bdrv
= NULL
;
2264 drives_table
[i
].used
= 0;
2265 drive_remove(drives_table
[i
].drive_opt_idx
);
2271 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2277 const char *mediastr
= "";
2278 BlockInterfaceType type
;
2279 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2280 int bus_id
, unit_id
;
2281 int cyls
, heads
, secs
, translation
;
2282 BlockDriverState
*bdrv
;
2283 BlockDriver
*drv
= NULL
;
2284 QEMUMachine
*machine
= opaque
;
2288 int bdrv_flags
, onerror
;
2289 int drives_table_idx
;
2290 char *str
= arg
->opt
;
2291 static const char * const params
[] = { "bus", "unit", "if", "index",
2292 "cyls", "heads", "secs", "trans",
2293 "media", "snapshot", "file",
2294 "cache", "format", "serial", "werror",
2297 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2298 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2304 cyls
= heads
= secs
= 0;
2307 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2311 if (machine
->use_scsi
) {
2313 max_devs
= MAX_SCSI_DEVS
;
2314 pstrcpy(devname
, sizeof(devname
), "scsi");
2317 max_devs
= MAX_IDE_DEVS
;
2318 pstrcpy(devname
, sizeof(devname
), "ide");
2322 /* extract parameters */
2324 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2325 bus_id
= strtol(buf
, NULL
, 0);
2327 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2332 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2333 unit_id
= strtol(buf
, NULL
, 0);
2335 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2340 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2341 pstrcpy(devname
, sizeof(devname
), buf
);
2342 if (!strcmp(buf
, "ide")) {
2344 max_devs
= MAX_IDE_DEVS
;
2345 } else if (!strcmp(buf
, "scsi")) {
2347 max_devs
= MAX_SCSI_DEVS
;
2348 } else if (!strcmp(buf
, "floppy")) {
2351 } else if (!strcmp(buf
, "pflash")) {
2354 } else if (!strcmp(buf
, "mtd")) {
2357 } else if (!strcmp(buf
, "sd")) {
2360 } else if (!strcmp(buf
, "virtio")) {
2364 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2369 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2370 index
= strtol(buf
, NULL
, 0);
2372 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2377 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2378 cyls
= strtol(buf
, NULL
, 0);
2381 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2382 heads
= strtol(buf
, NULL
, 0);
2385 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2386 secs
= strtol(buf
, NULL
, 0);
2389 if (cyls
|| heads
|| secs
) {
2390 if (cyls
< 1 || cyls
> 16383) {
2391 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2394 if (heads
< 1 || heads
> 16) {
2395 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2398 if (secs
< 1 || secs
> 63) {
2399 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2404 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2407 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2411 if (!strcmp(buf
, "none"))
2412 translation
= BIOS_ATA_TRANSLATION_NONE
;
2413 else if (!strcmp(buf
, "lba"))
2414 translation
= BIOS_ATA_TRANSLATION_LBA
;
2415 else if (!strcmp(buf
, "auto"))
2416 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2418 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2423 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2424 if (!strcmp(buf
, "disk")) {
2426 } else if (!strcmp(buf
, "cdrom")) {
2427 if (cyls
|| secs
|| heads
) {
2429 "qemu: '%s' invalid physical CHS format\n", str
);
2432 media
= MEDIA_CDROM
;
2434 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2439 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2440 if (!strcmp(buf
, "on"))
2442 else if (!strcmp(buf
, "off"))
2445 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2450 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2451 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2453 else if (!strcmp(buf
, "writethrough"))
2455 else if (!strcmp(buf
, "writeback"))
2458 fprintf(stderr
, "qemu: invalid cache option\n");
2463 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2464 if (strcmp(buf
, "?") == 0) {
2465 fprintf(stderr
, "qemu: Supported formats:");
2466 bdrv_iterate_format(bdrv_format_print
, NULL
);
2467 fprintf(stderr
, "\n");
2470 drv
= bdrv_find_format(buf
);
2472 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2477 if (arg
->file
== NULL
)
2478 get_param_value(file
, sizeof(file
), "file", str
);
2480 pstrcpy(file
, sizeof(file
), arg
->file
);
2482 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2483 memset(serial
, 0, sizeof(serial
));
2485 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2486 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2487 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2488 fprintf(stderr
, "werror is no supported by this format\n");
2491 if (!strcmp(buf
, "ignore"))
2492 onerror
= BLOCK_ERR_IGNORE
;
2493 else if (!strcmp(buf
, "enospc"))
2494 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2495 else if (!strcmp(buf
, "stop"))
2496 onerror
= BLOCK_ERR_STOP_ANY
;
2497 else if (!strcmp(buf
, "report"))
2498 onerror
= BLOCK_ERR_REPORT
;
2500 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2505 /* compute bus and unit according index */
2508 if (bus_id
!= 0 || unit_id
!= -1) {
2510 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2518 unit_id
= index
% max_devs
;
2519 bus_id
= index
/ max_devs
;
2523 /* if user doesn't specify a unit_id,
2524 * try to find the first free
2527 if (unit_id
== -1) {
2529 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2531 if (max_devs
&& unit_id
>= max_devs
) {
2532 unit_id
-= max_devs
;
2540 if (max_devs
&& unit_id
>= max_devs
) {
2541 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2542 str
, unit_id
, max_devs
- 1);
2547 * ignore multiple definitions
2550 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2555 if (type
== IF_IDE
|| type
== IF_SCSI
)
2556 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2558 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2559 devname
, bus_id
, mediastr
, unit_id
);
2561 snprintf(buf
, sizeof(buf
), "%s%s%i",
2562 devname
, mediastr
, unit_id
);
2563 bdrv
= bdrv_new(buf
);
2564 drives_table_idx
= drive_get_free_idx();
2565 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2566 drives_table
[drives_table_idx
].type
= type
;
2567 drives_table
[drives_table_idx
].bus
= bus_id
;
2568 drives_table
[drives_table_idx
].unit
= unit_id
;
2569 drives_table
[drives_table_idx
].onerror
= onerror
;
2570 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2571 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2580 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2581 bdrv_set_translation_hint(bdrv
, translation
);
2585 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2590 /* FIXME: This isn't really a floppy, but it's a reasonable
2593 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2604 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2605 cache
= 2; /* always use write-back with snapshot */
2607 if (cache
== 0) /* no caching */
2608 bdrv_flags
|= BDRV_O_NOCACHE
;
2609 else if (cache
== 2) /* write-back */
2610 bdrv_flags
|= BDRV_O_CACHE_WB
;
2611 else if (cache
== 3) /* not specified */
2612 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2613 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2614 fprintf(stderr
, "qemu: could not open disk image %s\n",
2618 if (bdrv_key_required(bdrv
))
2620 return drives_table_idx
;
2623 /***********************************************************/
2626 static USBPort
*used_usb_ports
;
2627 static USBPort
*free_usb_ports
;
2629 /* ??? Maybe change this to register a hub to keep track of the topology. */
2630 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2631 usb_attachfn attach
)
2633 port
->opaque
= opaque
;
2634 port
->index
= index
;
2635 port
->attach
= attach
;
2636 port
->next
= free_usb_ports
;
2637 free_usb_ports
= port
;
2640 int usb_device_add_dev(USBDevice
*dev
)
2644 /* Find a USB port to add the device to. */
2645 port
= free_usb_ports
;
2649 /* Create a new hub and chain it on. */
2650 free_usb_ports
= NULL
;
2651 port
->next
= used_usb_ports
;
2652 used_usb_ports
= port
;
2654 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2655 usb_attach(port
, hub
);
2656 port
= free_usb_ports
;
2659 free_usb_ports
= port
->next
;
2660 port
->next
= used_usb_ports
;
2661 used_usb_ports
= port
;
2662 usb_attach(port
, dev
);
2666 static void usb_msd_password_cb(void *opaque
, int err
)
2668 USBDevice
*dev
= opaque
;
2671 usb_device_add_dev(dev
);
2673 dev
->handle_destroy(dev
);
2676 static int usb_device_add(const char *devname
, int is_hotplug
)
2681 if (!free_usb_ports
)
2684 if (strstart(devname
, "host:", &p
)) {
2685 dev
= usb_host_device_open(p
);
2686 } else if (!strcmp(devname
, "mouse")) {
2687 dev
= usb_mouse_init();
2688 } else if (!strcmp(devname
, "tablet")) {
2689 dev
= usb_tablet_init();
2690 } else if (!strcmp(devname
, "keyboard")) {
2691 dev
= usb_keyboard_init();
2692 } else if (strstart(devname
, "disk:", &p
)) {
2693 BlockDriverState
*bs
;
2695 dev
= usb_msd_init(p
);
2698 bs
= usb_msd_get_bdrv(dev
);
2699 if (bdrv_key_required(bs
)) {
2702 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2707 } else if (!strcmp(devname
, "wacom-tablet")) {
2708 dev
= usb_wacom_init();
2709 } else if (strstart(devname
, "serial:", &p
)) {
2710 dev
= usb_serial_init(p
);
2711 #ifdef CONFIG_BRLAPI
2712 } else if (!strcmp(devname
, "braille")) {
2713 dev
= usb_baum_init();
2715 } else if (strstart(devname
, "net:", &p
)) {
2718 if (net_client_init("nic", p
) < 0)
2720 nd_table
[nic
].model
= "usb";
2721 dev
= usb_net_init(&nd_table
[nic
]);
2722 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2723 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2724 bt_new_hci(qemu_find_bt_vlan(0)));
2731 return usb_device_add_dev(dev
);
2734 int usb_device_del_addr(int bus_num
, int addr
)
2740 if (!used_usb_ports
)
2746 lastp
= &used_usb_ports
;
2747 port
= used_usb_ports
;
2748 while (port
&& port
->dev
->addr
!= addr
) {
2749 lastp
= &port
->next
;
2757 *lastp
= port
->next
;
2758 usb_attach(port
, NULL
);
2759 dev
->handle_destroy(dev
);
2760 port
->next
= free_usb_ports
;
2761 free_usb_ports
= port
;
2765 static int usb_device_del(const char *devname
)
2770 if (strstart(devname
, "host:", &p
))
2771 return usb_host_device_close(p
);
2773 if (!used_usb_ports
)
2776 p
= strchr(devname
, '.');
2779 bus_num
= strtoul(devname
, NULL
, 0);
2780 addr
= strtoul(p
+ 1, NULL
, 0);
2782 return usb_device_del_addr(bus_num
, addr
);
2785 void do_usb_add(Monitor
*mon
, const char *devname
)
2787 usb_device_add(devname
, 1);
2790 void do_usb_del(Monitor
*mon
, const char *devname
)
2792 usb_device_del(devname
);
2795 void usb_info(Monitor
*mon
)
2799 const char *speed_str
;
2802 monitor_printf(mon
, "USB support not enabled\n");
2806 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2810 switch(dev
->speed
) {
2814 case USB_SPEED_FULL
:
2817 case USB_SPEED_HIGH
:
2824 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2825 0, dev
->addr
, speed_str
, dev
->devname
);
2829 /***********************************************************/
2830 /* PCMCIA/Cardbus */
2832 static struct pcmcia_socket_entry_s
{
2833 struct pcmcia_socket_s
*socket
;
2834 struct pcmcia_socket_entry_s
*next
;
2835 } *pcmcia_sockets
= 0;
2837 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2839 struct pcmcia_socket_entry_s
*entry
;
2841 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2842 entry
->socket
= socket
;
2843 entry
->next
= pcmcia_sockets
;
2844 pcmcia_sockets
= entry
;
2847 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2849 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2851 ptr
= &pcmcia_sockets
;
2852 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2853 if (entry
->socket
== socket
) {
2859 void pcmcia_info(Monitor
*mon
)
2861 struct pcmcia_socket_entry_s
*iter
;
2863 if (!pcmcia_sockets
)
2864 monitor_printf(mon
, "No PCMCIA sockets\n");
2866 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2867 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2868 iter
->socket
->attached
? iter
->socket
->card_string
:
2872 /***********************************************************/
2873 /* register display */
2875 void register_displaystate(DisplayState
*ds
)
2885 DisplayState
*get_displaystate(void)
2887 return display_state
;
2892 static void dumb_display_init(void)
2894 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2895 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2896 register_displaystate(ds
);
2899 /***********************************************************/
2902 #define MAX_IO_HANDLERS 64
2904 typedef struct IOHandlerRecord
{
2906 IOCanRWHandler
*fd_read_poll
;
2908 IOHandler
*fd_write
;
2911 /* temporary data */
2913 struct IOHandlerRecord
*next
;
2916 static IOHandlerRecord
*first_io_handler
;
2918 /* XXX: fd_read_poll should be suppressed, but an API change is
2919 necessary in the character devices to suppress fd_can_read(). */
2920 int qemu_set_fd_handler2(int fd
,
2921 IOCanRWHandler
*fd_read_poll
,
2923 IOHandler
*fd_write
,
2926 IOHandlerRecord
**pioh
, *ioh
;
2928 if (!fd_read
&& !fd_write
) {
2929 pioh
= &first_io_handler
;
2934 if (ioh
->fd
== fd
) {
2941 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2945 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2946 ioh
->next
= first_io_handler
;
2947 first_io_handler
= ioh
;
2950 ioh
->fd_read_poll
= fd_read_poll
;
2951 ioh
->fd_read
= fd_read
;
2952 ioh
->fd_write
= fd_write
;
2953 ioh
->opaque
= opaque
;
2959 int qemu_set_fd_handler(int fd
,
2961 IOHandler
*fd_write
,
2964 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2968 /***********************************************************/
2969 /* Polling handling */
2971 typedef struct PollingEntry
{
2974 struct PollingEntry
*next
;
2977 static PollingEntry
*first_polling_entry
;
2979 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2981 PollingEntry
**ppe
, *pe
;
2982 pe
= qemu_mallocz(sizeof(PollingEntry
));
2984 pe
->opaque
= opaque
;
2985 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2990 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2992 PollingEntry
**ppe
, *pe
;
2993 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2995 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3003 /***********************************************************/
3004 /* Wait objects support */
3005 typedef struct WaitObjects
{
3007 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3008 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3009 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3012 static WaitObjects wait_objects
= {0};
3014 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3016 WaitObjects
*w
= &wait_objects
;
3018 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3020 w
->events
[w
->num
] = handle
;
3021 w
->func
[w
->num
] = func
;
3022 w
->opaque
[w
->num
] = opaque
;
3027 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3030 WaitObjects
*w
= &wait_objects
;
3033 for (i
= 0; i
< w
->num
; i
++) {
3034 if (w
->events
[i
] == handle
)
3037 w
->events
[i
] = w
->events
[i
+ 1];
3038 w
->func
[i
] = w
->func
[i
+ 1];
3039 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3047 /***********************************************************/
3048 /* ram save/restore */
3050 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3054 v
= qemu_get_byte(f
);
3057 if (qemu_get_buffer(f
, buf
, len
) != len
)
3061 v
= qemu_get_byte(f
);
3062 memset(buf
, v
, len
);
3068 if (qemu_file_has_error(f
))
3074 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3079 if (qemu_get_be32(f
) != phys_ram_size
)
3081 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3082 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3089 #define BDRV_HASH_BLOCK_SIZE 1024
3090 #define IOBUF_SIZE 4096
3091 #define RAM_CBLOCK_MAGIC 0xfabe
3093 typedef struct RamDecompressState
{
3096 uint8_t buf
[IOBUF_SIZE
];
3097 } RamDecompressState
;
3099 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3102 memset(s
, 0, sizeof(*s
));
3104 ret
= inflateInit(&s
->zstream
);
3110 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3114 s
->zstream
.avail_out
= len
;
3115 s
->zstream
.next_out
= buf
;
3116 while (s
->zstream
.avail_out
> 0) {
3117 if (s
->zstream
.avail_in
== 0) {
3118 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3120 clen
= qemu_get_be16(s
->f
);
3121 if (clen
> IOBUF_SIZE
)
3123 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3124 s
->zstream
.avail_in
= clen
;
3125 s
->zstream
.next_in
= s
->buf
;
3127 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3128 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3135 static void ram_decompress_close(RamDecompressState
*s
)
3137 inflateEnd(&s
->zstream
);
3140 #define RAM_SAVE_FLAG_FULL 0x01
3141 #define RAM_SAVE_FLAG_COMPRESS 0x02
3142 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3143 #define RAM_SAVE_FLAG_PAGE 0x08
3144 #define RAM_SAVE_FLAG_EOS 0x10
3146 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3148 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3149 uint32_t *array
= (uint32_t *)page
;
3152 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3153 if (array
[i
] != val
)
3160 static int ram_save_block(QEMUFile
*f
)
3162 static ram_addr_t current_addr
= 0;
3163 ram_addr_t saved_addr
= current_addr
;
3164 ram_addr_t addr
= 0;
3167 while (addr
< phys_ram_size
) {
3168 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3171 cpu_physical_memory_reset_dirty(current_addr
,
3172 current_addr
+ TARGET_PAGE_SIZE
,
3173 MIGRATION_DIRTY_FLAG
);
3175 ch
= *(phys_ram_base
+ current_addr
);
3177 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3178 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3179 qemu_put_byte(f
, ch
);
3181 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3182 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3188 addr
+= TARGET_PAGE_SIZE
;
3189 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3195 static ram_addr_t ram_save_threshold
= 10;
3197 static ram_addr_t
ram_save_remaining(void)
3200 ram_addr_t count
= 0;
3202 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3203 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3210 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3215 /* Make sure all dirty bits are set */
3216 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3217 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3218 cpu_physical_memory_set_dirty(addr
);
3221 /* Enable dirty memory tracking */
3222 cpu_physical_memory_set_dirty_tracking(1);
3224 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3227 while (!qemu_file_rate_limit(f
)) {
3230 ret
= ram_save_block(f
);
3231 if (ret
== 0) /* no more blocks */
3235 /* try transferring iterative blocks of memory */
3238 cpu_physical_memory_set_dirty_tracking(0);
3240 /* flush all remaining blocks regardless of rate limiting */
3241 while (ram_save_block(f
) != 0);
3244 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3246 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3249 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3251 RamDecompressState s1
, *s
= &s1
;
3255 if (ram_decompress_open(s
, f
) < 0)
3257 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3258 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3259 fprintf(stderr
, "Error while reading ram block header\n");
3263 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3264 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3269 printf("Error block header\n");
3273 ram_decompress_close(s
);
3278 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3283 if (version_id
== 1)
3284 return ram_load_v1(f
, opaque
);
3286 if (version_id
== 2) {
3287 if (qemu_get_be32(f
) != phys_ram_size
)
3289 return ram_load_dead(f
, opaque
);
3292 if (version_id
!= 3)
3296 addr
= qemu_get_be64(f
);
3298 flags
= addr
& ~TARGET_PAGE_MASK
;
3299 addr
&= TARGET_PAGE_MASK
;
3301 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3302 if (addr
!= phys_ram_size
)
3306 if (flags
& RAM_SAVE_FLAG_FULL
) {
3307 if (ram_load_dead(f
, opaque
) < 0)
3311 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3312 uint8_t ch
= qemu_get_byte(f
);
3313 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3314 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3315 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3316 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3321 void qemu_service_io(void)
3323 CPUState
*env
= cpu_single_env
;
3327 if (env
->kqemu_enabled
) {
3328 kqemu_cpu_interrupt(env
);
3334 /***********************************************************/
3335 /* bottom halves (can be seen as timers which expire ASAP) */
3346 static QEMUBH
*first_bh
= NULL
;
3348 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3351 bh
= qemu_mallocz(sizeof(QEMUBH
));
3353 bh
->opaque
= opaque
;
3354 bh
->next
= first_bh
;
3359 int qemu_bh_poll(void)
3365 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3366 if (!bh
->deleted
&& bh
->scheduled
) {
3375 /* remove deleted bhs */
3389 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3397 void qemu_bh_schedule(QEMUBH
*bh
)
3399 CPUState
*env
= cpu_single_env
;
3404 /* stop the currently executing CPU to execute the BH ASAP */
3410 void qemu_bh_cancel(QEMUBH
*bh
)
3415 void qemu_bh_delete(QEMUBH
*bh
)
3421 static void qemu_bh_update_timeout(int *timeout
)
3425 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3426 if (!bh
->deleted
&& bh
->scheduled
) {
3428 /* idle bottom halves will be polled at least
3430 *timeout
= MIN(10, *timeout
);
3432 /* non-idle bottom halves will be executed
3441 /***********************************************************/
3442 /* machine registration */
3444 static QEMUMachine
*first_machine
= NULL
;
3445 QEMUMachine
*current_machine
= NULL
;
3447 int qemu_register_machine(QEMUMachine
*m
)
3450 pm
= &first_machine
;
3458 static QEMUMachine
*find_machine(const char *name
)
3462 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3463 if (!strcmp(m
->name
, name
))
3469 /***********************************************************/
3470 /* main execution loop */
3472 static void gui_update(void *opaque
)
3474 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3475 DisplayState
*ds
= opaque
;
3476 DisplayChangeListener
*dcl
= ds
->listeners
;
3480 while (dcl
!= NULL
) {
3481 if (dcl
->gui_timer_interval
&&
3482 dcl
->gui_timer_interval
< interval
)
3483 interval
= dcl
->gui_timer_interval
;
3486 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3489 static void nographic_update(void *opaque
)
3491 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3493 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3496 struct vm_change_state_entry
{
3497 VMChangeStateHandler
*cb
;
3499 LIST_ENTRY (vm_change_state_entry
) entries
;
3502 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3504 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3507 VMChangeStateEntry
*e
;
3509 e
= qemu_mallocz(sizeof (*e
));
3513 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3517 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3519 LIST_REMOVE (e
, entries
);
3523 static void vm_state_notify(int running
, int reason
)
3525 VMChangeStateEntry
*e
;
3527 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3528 e
->cb(e
->opaque
, running
, reason
);
3537 vm_state_notify(1, 0);
3538 qemu_rearm_alarm_timer(alarm_timer
);
3542 void vm_stop(int reason
)
3545 cpu_disable_ticks();
3547 vm_state_notify(0, reason
);
3551 /* reset/shutdown handler */
3553 typedef struct QEMUResetEntry
{
3554 QEMUResetHandler
*func
;
3556 struct QEMUResetEntry
*next
;
3559 static QEMUResetEntry
*first_reset_entry
;
3560 static int reset_requested
;
3561 static int shutdown_requested
;
3562 static int powerdown_requested
;
3564 int qemu_shutdown_requested(void)
3566 int r
= shutdown_requested
;
3567 shutdown_requested
= 0;
3571 int qemu_reset_requested(void)
3573 int r
= reset_requested
;
3574 reset_requested
= 0;
3578 int qemu_powerdown_requested(void)
3580 int r
= powerdown_requested
;
3581 powerdown_requested
= 0;
3585 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3587 QEMUResetEntry
**pre
, *re
;
3589 pre
= &first_reset_entry
;
3590 while (*pre
!= NULL
)
3591 pre
= &(*pre
)->next
;
3592 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3594 re
->opaque
= opaque
;
3599 void qemu_system_reset(void)
3603 /* reset all devices */
3604 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3605 re
->func(re
->opaque
);
3609 void qemu_system_reset_request(void)
3612 shutdown_requested
= 1;
3614 reset_requested
= 1;
3617 cpu_exit(cpu_single_env
);
3620 void qemu_system_shutdown_request(void)
3622 shutdown_requested
= 1;
3624 cpu_exit(cpu_single_env
);
3627 void qemu_system_powerdown_request(void)
3629 powerdown_requested
= 1;
3631 cpu_exit(cpu_single_env
);
3635 static void host_main_loop_wait(int *timeout
)
3641 /* XXX: need to suppress polling by better using win32 events */
3643 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3644 ret
|= pe
->func(pe
->opaque
);
3648 WaitObjects
*w
= &wait_objects
;
3650 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3651 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3652 if (w
->func
[ret
- WAIT_OBJECT_0
])
3653 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3655 /* Check for additional signaled events */
3656 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3658 /* Check if event is signaled */
3659 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3660 if(ret2
== WAIT_OBJECT_0
) {
3662 w
->func
[i
](w
->opaque
[i
]);
3663 } else if (ret2
== WAIT_TIMEOUT
) {
3665 err
= GetLastError();
3666 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3669 } else if (ret
== WAIT_TIMEOUT
) {
3671 err
= GetLastError();
3672 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3679 static void host_main_loop_wait(int *timeout
)
3684 void main_loop_wait(int timeout
)
3686 IOHandlerRecord
*ioh
;
3687 fd_set rfds
, wfds
, xfds
;
3691 qemu_bh_update_timeout(&timeout
);
3693 host_main_loop_wait(&timeout
);
3695 /* poll any events */
3696 /* XXX: separate device handlers from system ones */
3701 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3705 (!ioh
->fd_read_poll
||
3706 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3707 FD_SET(ioh
->fd
, &rfds
);
3711 if (ioh
->fd_write
) {
3712 FD_SET(ioh
->fd
, &wfds
);
3718 tv
.tv_sec
= timeout
/ 1000;
3719 tv
.tv_usec
= (timeout
% 1000) * 1000;
3721 #if defined(CONFIG_SLIRP)
3722 if (slirp_is_inited()) {
3723 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3726 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3728 IOHandlerRecord
**pioh
;
3730 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3731 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3732 ioh
->fd_read(ioh
->opaque
);
3734 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3735 ioh
->fd_write(ioh
->opaque
);
3739 /* remove deleted IO handlers */
3740 pioh
= &first_io_handler
;
3750 #if defined(CONFIG_SLIRP)
3751 if (slirp_is_inited()) {
3757 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3761 /* vm time timers */
3762 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3763 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3764 qemu_get_clock(vm_clock
));
3766 /* real time timers */
3767 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3768 qemu_get_clock(rt_clock
));
3770 /* Check bottom-halves last in case any of the earlier events triggered
3776 static int main_loop(void)
3779 #ifdef CONFIG_PROFILER
3784 cur_cpu
= first_cpu
;
3785 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3792 #ifdef CONFIG_PROFILER
3793 ti
= profile_getclock();
3798 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3799 env
->icount_decr
.u16
.low
= 0;
3800 env
->icount_extra
= 0;
3801 count
= qemu_next_deadline();
3802 count
= (count
+ (1 << icount_time_shift
) - 1)
3803 >> icount_time_shift
;
3804 qemu_icount
+= count
;
3805 decr
= (count
> 0xffff) ? 0xffff : count
;
3807 env
->icount_decr
.u16
.low
= decr
;
3808 env
->icount_extra
= count
;
3810 ret
= cpu_exec(env
);
3811 #ifdef CONFIG_PROFILER
3812 qemu_time
+= profile_getclock() - ti
;
3815 /* Fold pending instructions back into the
3816 instruction counter, and clear the interrupt flag. */
3817 qemu_icount
-= (env
->icount_decr
.u16
.low
3818 + env
->icount_extra
);
3819 env
->icount_decr
.u32
= 0;
3820 env
->icount_extra
= 0;
3822 next_cpu
= env
->next_cpu
?: first_cpu
;
3823 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3824 ret
= EXCP_INTERRUPT
;
3828 if (ret
== EXCP_HLT
) {
3829 /* Give the next CPU a chance to run. */
3833 if (ret
!= EXCP_HALTED
)
3835 /* all CPUs are halted ? */
3841 if (shutdown_requested
) {
3842 ret
= EXCP_INTERRUPT
;
3850 if (reset_requested
) {
3851 reset_requested
= 0;
3852 qemu_system_reset();
3853 ret
= EXCP_INTERRUPT
;
3855 if (powerdown_requested
) {
3856 powerdown_requested
= 0;
3857 qemu_system_powerdown();
3858 ret
= EXCP_INTERRUPT
;
3860 if (unlikely(ret
== EXCP_DEBUG
)) {
3861 gdb_set_stop_cpu(cur_cpu
);
3862 vm_stop(EXCP_DEBUG
);
3864 /* If all cpus are halted then wait until the next IRQ */
3865 /* XXX: use timeout computed from timers */
3866 if (ret
== EXCP_HALTED
) {
3870 /* Advance virtual time to the next event. */
3871 if (use_icount
== 1) {
3872 /* When not using an adaptive execution frequency
3873 we tend to get badly out of sync with real time,
3874 so just delay for a reasonable amount of time. */
3877 delta
= cpu_get_icount() - cpu_get_clock();
3880 /* If virtual time is ahead of real time then just
3882 timeout
= (delta
/ 1000000) + 1;
3884 /* Wait for either IO to occur or the next
3886 add
= qemu_next_deadline();
3887 /* We advance the timer before checking for IO.
3888 Limit the amount we advance so that early IO
3889 activity won't get the guest too far ahead. */
3893 add
= (add
+ (1 << icount_time_shift
) - 1)
3894 >> icount_time_shift
;
3896 timeout
= delta
/ 1000000;
3907 if (shutdown_requested
) {
3908 ret
= EXCP_INTERRUPT
;
3913 #ifdef CONFIG_PROFILER
3914 ti
= profile_getclock();
3916 main_loop_wait(timeout
);
3917 #ifdef CONFIG_PROFILER
3918 dev_time
+= profile_getclock() - ti
;
3921 cpu_disable_ticks();
3925 static void help(int exitcode
)
3927 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3928 and qemu-doc.texi */
3929 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3930 "usage: %s [options] [disk_image]\n"
3932 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3934 "Standard options:\n"
3935 "-h or -help display this help and exit\n"
3936 "-M machine select emulated machine (-M ? for list)\n"
3937 "-cpu cpu select CPU (-cpu ? for list)\n"
3938 "-smp n set the number of CPUs to 'n' [default=1]\n"
3939 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3940 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3941 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3942 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3943 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3944 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3945 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3946 " use 'file' as a drive image\n"
3947 "-mtdblock file use 'file' as on-board Flash memory image\n"
3948 "-sd file use 'file' as SecureDigital card image\n"
3949 "-pflash file use 'file' as a parallel flash image\n"
3950 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3951 "-snapshot write to temporary files instead of disk image files\n"
3952 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3954 "-k language use keyboard layout (for example \"fr\" for French)\n"
3957 "-audio-help print list of audio drivers and their options\n"
3958 "-soundhw c1,... enable audio support\n"
3959 " and only specified sound cards (comma separated list)\n"
3960 " use -soundhw ? to get the list of supported cards\n"
3961 " use -soundhw all to enable all of them\n"
3963 "-usb enable the USB driver (will be the default soon)\n"
3964 "-usbdevice name add the host or guest USB device 'name'\n"
3965 "-name string set the name of the guest\n"
3966 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3967 " specify machine UUID\n"
3969 "Display options:\n"
3970 "-nographic disable graphical output and redirect serial I/Os to console\n"
3971 #ifdef CONFIG_CURSES
3972 "-curses use a curses/ncurses interface instead of SDL\n"
3975 "-no-frame open SDL window without a frame and window decorations\n"
3976 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3977 "-no-quit disable SDL window close capability\n"
3980 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3981 "-vga [std|cirrus|vmware|none]\n"
3982 " select video card type\n"
3983 "-full-screen start in full screen\n"
3984 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3985 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3987 "-vnc display start a VNC server on display\n"
3989 "Network options:\n"
3990 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3991 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3993 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3994 " connect the user mode network stack to VLAN 'n' and send\n"
3995 " hostname 'host' to DHCP clients\n"
3998 "-net tap[,vlan=n][,name=str],ifname=name\n"
3999 " connect the host TAP network interface to VLAN 'n'\n"
4001 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4002 " connect the host TAP network interface to VLAN 'n' and use the\n"
4003 " network scripts 'file' (default=%s)\n"
4004 " and 'dfile' (default=%s);\n"
4005 " use '[down]script=no' to disable script execution;\n"
4006 " use 'fd=h' to connect to an already opened TAP interface\n"
4008 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4009 " connect the vlan 'n' to another VLAN using a socket connection\n"
4010 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4011 " connect the vlan 'n' to multicast maddr and port\n"
4013 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4014 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4015 " on host and listening for incoming connections on 'socketpath'.\n"
4016 " Use group 'groupname' and mode 'octalmode' to change default\n"
4017 " ownership and permissions for communication port.\n"
4019 "-net none use it alone to have zero network devices; if no -net option\n"
4020 " is provided, the default is '-net nic -net user'\n"
4022 "-tftp dir allow tftp access to files in dir [-net user]\n"
4023 "-bootp file advertise file in BOOTP replies\n"
4025 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4027 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4028 " redirect TCP or UDP connections from host to guest [-net user]\n"
4031 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4032 "-bt hci,host[:id]\n"
4033 " use host's HCI with the given name\n"
4034 "-bt hci[,vlan=n]\n"
4035 " emulate a standard HCI in virtual scatternet 'n'\n"
4036 "-bt vhci[,vlan=n]\n"
4037 " add host computer to virtual scatternet 'n' using VHCI\n"
4038 "-bt device:dev[,vlan=n]\n"
4039 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4043 "i386 target only:\n"
4044 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4045 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4046 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4047 "-no-acpi disable ACPI\n"
4048 "-no-hpet disable HPET\n"
4049 "-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"
4050 " ACPI table description\n"
4052 "Linux boot specific:\n"
4053 "-kernel bzImage use 'bzImage' as kernel image\n"
4054 "-append cmdline use 'cmdline' as kernel command line\n"
4055 "-initrd file use 'file' as initial ram disk\n"
4057 "Debug/Expert options:\n"
4058 "-serial dev redirect the serial port to char device 'dev'\n"
4059 "-parallel dev redirect the parallel port to char device 'dev'\n"
4060 "-monitor dev redirect the monitor to char device 'dev'\n"
4061 "-pidfile file write PID to 'file'\n"
4062 "-S freeze CPU at startup (use 'c' to start execution)\n"
4063 "-s wait gdb connection to port\n"
4064 "-p port set gdb connection port [default=%s]\n"
4065 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4066 "-hdachs c,h,s[,t]\n"
4067 " force hard disk 0 physical geometry and the optional BIOS\n"
4068 " translation (t=none or lba) (usually qemu can guess them)\n"
4069 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4070 "-bios file set the filename for the BIOS\n"
4072 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4073 "-no-kqemu disable KQEMU kernel module usage\n"
4076 "-enable-kvm enable KVM full virtualization support\n"
4078 "-no-reboot exit instead of rebooting\n"
4079 "-no-shutdown stop before shutdown\n"
4080 "-loadvm [tag|id]\n"
4081 " start right away with a saved state (loadvm in monitor)\n"
4083 "-daemonize daemonize QEMU after initializing\n"
4085 "-option-rom rom load a file, rom, into the option ROM space\n"
4086 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4087 "-prom-env variable=value\n"
4088 " set OpenBIOS nvram variables\n"
4090 "-clock force the use of the given methods for timer alarm.\n"
4091 " To see what timers are available use -clock ?\n"
4092 "-localtime set the real time clock to local time [default=utc]\n"
4093 "-startdate select initial date of the clock\n"
4094 "-icount [N|auto]\n"
4095 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4096 "-echr chr set terminal escape character instead of ctrl-a\n"
4097 "-virtioconsole c\n"
4098 " set virtio console\n"
4099 "-show-cursor show cursor\n"
4100 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4101 "-semihosting semihosting mode\n"
4103 #if defined(TARGET_ARM)
4104 "-old-param old param mode\n"
4106 "-tb-size n set TB size\n"
4107 "-incoming p prepare for incoming migration, listen on port p\n"
4109 "-chroot dir Chroot to dir just before starting the VM.\n"
4110 "-runas user Change to user id user just before starting the VM.\n"
4113 "During emulation, the following keys are useful:\n"
4114 "ctrl-alt-f toggle full screen\n"
4115 "ctrl-alt-n switch to virtual console 'n'\n"
4116 "ctrl-alt toggle mouse and keyboard grab\n"
4118 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4123 DEFAULT_NETWORK_SCRIPT
,
4124 DEFAULT_NETWORK_DOWN_SCRIPT
,
4126 DEFAULT_GDBSTUB_PORT
,
4131 #define HAS_ARG 0x0001
4134 /* Please keep in synch with help, qemu_options[] and
4136 /* Standard options: */
4149 QEMU_OPTION_mtdblock
,
4153 QEMU_OPTION_snapshot
,
4156 QEMU_OPTION_audio_help
,
4157 QEMU_OPTION_soundhw
,
4159 QEMU_OPTION_usbdevice
,
4163 /* Display options: */
4164 QEMU_OPTION_nographic
,
4166 QEMU_OPTION_no_frame
,
4167 QEMU_OPTION_alt_grab
,
4168 QEMU_OPTION_no_quit
,
4170 QEMU_OPTION_portrait
,
4172 QEMU_OPTION_full_screen
,
4176 /* Network options: */
4184 /* i386 target only: */
4185 QEMU_OPTION_win2k_hack
,
4186 QEMU_OPTION_rtc_td_hack
,
4187 QEMU_OPTION_no_fd_bootchk
,
4188 QEMU_OPTION_no_acpi
,
4189 QEMU_OPTION_no_hpet
,
4190 QEMU_OPTION_acpitable
,
4192 /* Linux boot specific: */
4197 /* Debug/Expert options: */
4199 QEMU_OPTION_parallel
,
4200 QEMU_OPTION_monitor
,
4201 QEMU_OPTION_pidfile
,
4209 QEMU_OPTION_kernel_kqemu
,
4210 QEMU_OPTION_no_kqemu
,
4211 QEMU_OPTION_enable_kvm
,
4212 QEMU_OPTION_no_reboot
,
4213 QEMU_OPTION_no_shutdown
,
4215 QEMU_OPTION_daemonize
,
4216 QEMU_OPTION_option_rom
,
4217 QEMU_OPTION_prom_env
,
4219 QEMU_OPTION_localtime
,
4220 QEMU_OPTION_startdate
,
4223 QEMU_OPTION_virtiocon
,
4224 QEMU_OPTION_show_cursor
,
4225 QEMU_OPTION_semihosting
,
4226 QEMU_OPTION_old_param
,
4227 QEMU_OPTION_tb_size
,
4228 QEMU_OPTION_incoming
,
4233 typedef struct QEMUOption
{
4239 static const QEMUOption qemu_options
[] = {
4240 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4242 /* Standard options: */
4243 { "h", 0, QEMU_OPTION_h
},
4244 { "help", 0, QEMU_OPTION_h
},
4245 { "M", HAS_ARG
, QEMU_OPTION_M
},
4246 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4247 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4248 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4249 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4250 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4251 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4252 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4253 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4254 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4255 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4256 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4257 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4258 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4259 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4260 { "snapshot", 0, QEMU_OPTION_snapshot
},
4261 { "m", HAS_ARG
, QEMU_OPTION_m
},
4263 { "k", HAS_ARG
, QEMU_OPTION_k
},
4266 { "audio-help", 0, QEMU_OPTION_audio_help
},
4267 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4269 { "usb", 0, QEMU_OPTION_usb
},
4270 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4271 { "name", HAS_ARG
, QEMU_OPTION_name
},
4272 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4274 /* Display options: */
4275 { "nographic", 0, QEMU_OPTION_nographic
},
4276 #ifdef CONFIG_CURSES
4277 { "curses", 0, QEMU_OPTION_curses
},
4280 { "no-frame", 0, QEMU_OPTION_no_frame
},
4281 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4282 { "no-quit", 0, QEMU_OPTION_no_quit
},
4283 { "sdl", 0, QEMU_OPTION_sdl
},
4285 { "portrait", 0, QEMU_OPTION_portrait
},
4286 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4287 { "full-screen", 0, QEMU_OPTION_full_screen
},
4288 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4289 { "g", 1, QEMU_OPTION_g
},
4291 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4293 /* Network options: */
4294 { "net", HAS_ARG
, QEMU_OPTION_net
},
4296 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4297 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4299 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4301 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4303 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4305 /* i386 target only: */
4306 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4307 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4308 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4309 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4310 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4311 { "acpitable", HAS_ARG
, QEMU_OPTION_acpitable
},
4314 /* Linux boot specific: */
4315 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4316 { "append", HAS_ARG
, QEMU_OPTION_append
},
4317 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4319 /* Debug/Expert options: */
4320 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4321 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4322 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4323 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4324 { "S", 0, QEMU_OPTION_S
},
4325 { "s", 0, QEMU_OPTION_s
},
4326 { "p", HAS_ARG
, QEMU_OPTION_p
},
4327 { "d", HAS_ARG
, QEMU_OPTION_d
},
4328 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4329 { "L", HAS_ARG
, QEMU_OPTION_L
},
4330 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4332 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4333 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4336 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4338 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4339 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4340 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4341 { "daemonize", 0, QEMU_OPTION_daemonize
},
4342 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4343 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4344 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4346 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4347 { "localtime", 0, QEMU_OPTION_localtime
},
4348 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4349 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4350 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4351 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4352 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4353 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4354 { "semihosting", 0, QEMU_OPTION_semihosting
},
4356 #if defined(TARGET_ARM)
4357 { "old-param", 0, QEMU_OPTION_old_param
},
4359 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4360 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4361 { "chroot", HAS_ARG
, QEMU_OPTION_chroot
},
4362 { "runas", HAS_ARG
, QEMU_OPTION_runas
},
4367 struct soundhw soundhw
[] = {
4368 #ifdef HAS_AUDIO_CHOICE
4369 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4375 { .init_isa
= pcspk_audio_init
}
4382 "Creative Sound Blaster 16",
4385 { .init_isa
= SB16_init
}
4389 #ifdef CONFIG_CS4231A
4395 { .init_isa
= cs4231a_init
}
4403 "Yamaha YMF262 (OPL3)",
4405 "Yamaha YM3812 (OPL2)",
4409 { .init_isa
= Adlib_init
}
4416 "Gravis Ultrasound GF1",
4419 { .init_isa
= GUS_init
}
4426 "Intel 82801AA AC97 Audio",
4429 { .init_pci
= ac97_init
}
4433 #ifdef CONFIG_ES1370
4436 "ENSONIQ AudioPCI ES1370",
4439 { .init_pci
= es1370_init
}
4443 #endif /* HAS_AUDIO_CHOICE */
4445 { NULL
, NULL
, 0, 0, { NULL
} }
4448 static void select_soundhw (const char *optarg
)
4452 if (*optarg
== '?') {
4455 printf ("Valid sound card names (comma separated):\n");
4456 for (c
= soundhw
; c
->name
; ++c
) {
4457 printf ("%-11s %s\n", c
->name
, c
->descr
);
4459 printf ("\n-soundhw all will enable all of the above\n");
4460 exit (*optarg
!= '?');
4468 if (!strcmp (optarg
, "all")) {
4469 for (c
= soundhw
; c
->name
; ++c
) {
4477 e
= strchr (p
, ',');
4478 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4480 for (c
= soundhw
; c
->name
; ++c
) {
4481 if (!strncmp (c
->name
, p
, l
)) {
4490 "Unknown sound card name (too big to show)\n");
4493 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4498 p
+= l
+ (e
!= NULL
);
4502 goto show_valid_cards
;
4507 static void select_vgahw (const char *p
)
4511 if (strstart(p
, "std", &opts
)) {
4512 std_vga_enabled
= 1;
4513 cirrus_vga_enabled
= 0;
4515 } else if (strstart(p
, "cirrus", &opts
)) {
4516 cirrus_vga_enabled
= 1;
4517 std_vga_enabled
= 0;
4519 } else if (strstart(p
, "vmware", &opts
)) {
4520 cirrus_vga_enabled
= 0;
4521 std_vga_enabled
= 0;
4523 } else if (strstart(p
, "none", &opts
)) {
4524 cirrus_vga_enabled
= 0;
4525 std_vga_enabled
= 0;
4529 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4533 const char *nextopt
;
4535 if (strstart(opts
, ",retrace=", &nextopt
)) {
4537 if (strstart(opts
, "dumb", &nextopt
))
4538 vga_retrace_method
= VGA_RETRACE_DUMB
;
4539 else if (strstart(opts
, "precise", &nextopt
))
4540 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4541 else goto invalid_vga
;
4542 } else goto invalid_vga
;
4548 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4550 exit(STATUS_CONTROL_C_EXIT
);
4555 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4559 if(strlen(str
) != 36)
4562 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4563 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4564 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4572 #define MAX_NET_CLIENTS 32
4576 static void termsig_handler(int signal
)
4578 qemu_system_shutdown_request();
4581 static void termsig_setup(void)
4583 struct sigaction act
;
4585 memset(&act
, 0, sizeof(act
));
4586 act
.sa_handler
= termsig_handler
;
4587 sigaction(SIGINT
, &act
, NULL
);
4588 sigaction(SIGHUP
, &act
, NULL
);
4589 sigaction(SIGTERM
, &act
, NULL
);
4594 int main(int argc
, char **argv
, char **envp
)
4596 #ifdef CONFIG_GDBSTUB
4598 const char *gdbstub_port
;
4600 uint32_t boot_devices_bitmap
= 0;
4602 int snapshot
, linux_boot
, net_boot
;
4603 const char *initrd_filename
;
4604 const char *kernel_filename
, *kernel_cmdline
;
4605 const char *boot_devices
= "";
4607 DisplayChangeListener
*dcl
;
4608 int cyls
, heads
, secs
, translation
;
4609 const char *net_clients
[MAX_NET_CLIENTS
];
4611 const char *bt_opts
[MAX_BT_CMDLINE
];
4615 const char *r
, *optarg
;
4616 CharDriverState
*monitor_hd
= NULL
;
4617 const char *monitor_device
;
4618 const char *serial_devices
[MAX_SERIAL_PORTS
];
4619 int serial_device_index
;
4620 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4621 int parallel_device_index
;
4622 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4623 int virtio_console_index
;
4624 const char *loadvm
= NULL
;
4625 QEMUMachine
*machine
;
4626 const char *cpu_model
;
4627 const char *usb_devices
[MAX_USB_CMDLINE
];
4628 int usb_devices_index
;
4631 const char *pid_file
= NULL
;
4632 const char *incoming
= NULL
;
4634 struct passwd
*pwd
= NULL
;
4635 const char *chroot_dir
= NULL
;
4636 const char *run_as
= NULL
;
4638 qemu_cache_utils_init(envp
);
4640 LIST_INIT (&vm_change_state_head
);
4643 struct sigaction act
;
4644 sigfillset(&act
.sa_mask
);
4646 act
.sa_handler
= SIG_IGN
;
4647 sigaction(SIGPIPE
, &act
, NULL
);
4650 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4651 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4652 QEMU to run on a single CPU */
4657 h
= GetCurrentProcess();
4658 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4659 for(i
= 0; i
< 32; i
++) {
4660 if (mask
& (1 << i
))
4665 SetProcessAffinityMask(h
, mask
);
4671 register_machines();
4672 machine
= first_machine
;
4674 initrd_filename
= NULL
;
4676 vga_ram_size
= VGA_RAM_SIZE
;
4677 #ifdef CONFIG_GDBSTUB
4679 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4684 kernel_filename
= NULL
;
4685 kernel_cmdline
= "";
4686 cyls
= heads
= secs
= 0;
4687 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4688 monitor_device
= "vc:80Cx24C";
4690 serial_devices
[0] = "vc:80Cx24C";
4691 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4692 serial_devices
[i
] = NULL
;
4693 serial_device_index
= 0;
4695 parallel_devices
[0] = "vc:80Cx24C";
4696 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4697 parallel_devices
[i
] = NULL
;
4698 parallel_device_index
= 0;
4700 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4701 virtio_consoles
[i
] = NULL
;
4702 virtio_console_index
= 0;
4704 usb_devices_index
= 0;
4723 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4725 const QEMUOption
*popt
;
4728 /* Treat --foo the same as -foo. */
4731 popt
= qemu_options
;
4734 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4738 if (!strcmp(popt
->name
, r
+ 1))
4742 if (popt
->flags
& HAS_ARG
) {
4743 if (optind
>= argc
) {
4744 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4748 optarg
= argv
[optind
++];
4753 switch(popt
->index
) {
4755 machine
= find_machine(optarg
);
4758 printf("Supported machines are:\n");
4759 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4760 printf("%-10s %s%s\n",
4762 m
== first_machine
? " (default)" : "");
4764 exit(*optarg
!= '?');
4767 case QEMU_OPTION_cpu
:
4768 /* hw initialization will check this */
4769 if (*optarg
== '?') {
4770 /* XXX: implement xxx_cpu_list for targets that still miss it */
4771 #if defined(cpu_list)
4772 cpu_list(stdout
, &fprintf
);
4779 case QEMU_OPTION_initrd
:
4780 initrd_filename
= optarg
;
4782 case QEMU_OPTION_hda
:
4784 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4786 hda_index
= drive_add(optarg
, HD_ALIAS
4787 ",cyls=%d,heads=%d,secs=%d%s",
4788 0, cyls
, heads
, secs
,
4789 translation
== BIOS_ATA_TRANSLATION_LBA
?
4791 translation
== BIOS_ATA_TRANSLATION_NONE
?
4792 ",trans=none" : "");
4794 case QEMU_OPTION_hdb
:
4795 case QEMU_OPTION_hdc
:
4796 case QEMU_OPTION_hdd
:
4797 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4799 case QEMU_OPTION_drive
:
4800 drive_add(NULL
, "%s", optarg
);
4802 case QEMU_OPTION_mtdblock
:
4803 drive_add(optarg
, MTD_ALIAS
);
4805 case QEMU_OPTION_sd
:
4806 drive_add(optarg
, SD_ALIAS
);
4808 case QEMU_OPTION_pflash
:
4809 drive_add(optarg
, PFLASH_ALIAS
);
4811 case QEMU_OPTION_snapshot
:
4814 case QEMU_OPTION_hdachs
:
4818 cyls
= strtol(p
, (char **)&p
, 0);
4819 if (cyls
< 1 || cyls
> 16383)
4824 heads
= strtol(p
, (char **)&p
, 0);
4825 if (heads
< 1 || heads
> 16)
4830 secs
= strtol(p
, (char **)&p
, 0);
4831 if (secs
< 1 || secs
> 63)
4835 if (!strcmp(p
, "none"))
4836 translation
= BIOS_ATA_TRANSLATION_NONE
;
4837 else if (!strcmp(p
, "lba"))
4838 translation
= BIOS_ATA_TRANSLATION_LBA
;
4839 else if (!strcmp(p
, "auto"))
4840 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4843 } else if (*p
!= '\0') {
4845 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4848 if (hda_index
!= -1)
4849 snprintf(drives_opt
[hda_index
].opt
,
4850 sizeof(drives_opt
[hda_index
].opt
),
4851 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4852 0, cyls
, heads
, secs
,
4853 translation
== BIOS_ATA_TRANSLATION_LBA
?
4855 translation
== BIOS_ATA_TRANSLATION_NONE
?
4856 ",trans=none" : "");
4859 case QEMU_OPTION_nographic
:
4862 #ifdef CONFIG_CURSES
4863 case QEMU_OPTION_curses
:
4867 case QEMU_OPTION_portrait
:
4870 case QEMU_OPTION_kernel
:
4871 kernel_filename
= optarg
;
4873 case QEMU_OPTION_append
:
4874 kernel_cmdline
= optarg
;
4876 case QEMU_OPTION_cdrom
:
4877 drive_add(optarg
, CDROM_ALIAS
);
4879 case QEMU_OPTION_boot
:
4880 boot_devices
= optarg
;
4881 /* We just do some generic consistency checks */
4883 /* Could easily be extended to 64 devices if needed */
4886 boot_devices_bitmap
= 0;
4887 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4888 /* Allowed boot devices are:
4889 * a b : floppy disk drives
4890 * c ... f : IDE disk drives
4891 * g ... m : machine implementation dependant drives
4892 * n ... p : network devices
4893 * It's up to each machine implementation to check
4894 * if the given boot devices match the actual hardware
4895 * implementation and firmware features.
4897 if (*p
< 'a' || *p
> 'q') {
4898 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4901 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4903 "Boot device '%c' was given twice\n",*p
);
4906 boot_devices_bitmap
|= 1 << (*p
- 'a');
4910 case QEMU_OPTION_fda
:
4911 case QEMU_OPTION_fdb
:
4912 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4915 case QEMU_OPTION_no_fd_bootchk
:
4919 case QEMU_OPTION_net
:
4920 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4921 fprintf(stderr
, "qemu: too many network clients\n");
4924 net_clients
[nb_net_clients
] = optarg
;
4928 case QEMU_OPTION_tftp
:
4929 tftp_prefix
= optarg
;
4931 case QEMU_OPTION_bootp
:
4932 bootp_filename
= optarg
;
4935 case QEMU_OPTION_smb
:
4936 net_slirp_smb(optarg
);
4939 case QEMU_OPTION_redir
:
4940 net_slirp_redir(optarg
);
4943 case QEMU_OPTION_bt
:
4944 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4945 fprintf(stderr
, "qemu: too many bluetooth options\n");
4948 bt_opts
[nb_bt_opts
++] = optarg
;
4951 case QEMU_OPTION_audio_help
:
4955 case QEMU_OPTION_soundhw
:
4956 select_soundhw (optarg
);
4962 case QEMU_OPTION_m
: {
4966 value
= strtoul(optarg
, &ptr
, 10);
4968 case 0: case 'M': case 'm':
4975 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4979 /* On 32-bit hosts, QEMU is limited by virtual address space */
4980 if (value
> (2047 << 20)
4982 && HOST_LONG_BITS
== 32
4985 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4988 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4989 fprintf(stderr
, "qemu: ram size too large\n");
4998 const CPULogItem
*item
;
5000 mask
= cpu_str_to_log_mask(optarg
);
5002 printf("Log items (comma separated):\n");
5003 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5004 printf("%-10s %s\n", item
->name
, item
->help
);
5011 #ifdef CONFIG_GDBSTUB
5016 gdbstub_port
= optarg
;
5022 case QEMU_OPTION_bios
:
5029 keyboard_layout
= optarg
;
5031 case QEMU_OPTION_localtime
:
5034 case QEMU_OPTION_vga
:
5035 select_vgahw (optarg
);
5042 w
= strtol(p
, (char **)&p
, 10);
5045 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5051 h
= strtol(p
, (char **)&p
, 10);
5056 depth
= strtol(p
, (char **)&p
, 10);
5057 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5058 depth
!= 24 && depth
!= 32)
5060 } else if (*p
== '\0') {
5061 depth
= graphic_depth
;
5068 graphic_depth
= depth
;
5071 case QEMU_OPTION_echr
:
5074 term_escape_char
= strtol(optarg
, &r
, 0);
5076 printf("Bad argument to echr\n");
5079 case QEMU_OPTION_monitor
:
5080 monitor_device
= optarg
;
5082 case QEMU_OPTION_serial
:
5083 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5084 fprintf(stderr
, "qemu: too many serial ports\n");
5087 serial_devices
[serial_device_index
] = optarg
;
5088 serial_device_index
++;
5090 case QEMU_OPTION_virtiocon
:
5091 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5092 fprintf(stderr
, "qemu: too many virtio consoles\n");
5095 virtio_consoles
[virtio_console_index
] = optarg
;
5096 virtio_console_index
++;
5098 case QEMU_OPTION_parallel
:
5099 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5100 fprintf(stderr
, "qemu: too many parallel ports\n");
5103 parallel_devices
[parallel_device_index
] = optarg
;
5104 parallel_device_index
++;
5106 case QEMU_OPTION_loadvm
:
5109 case QEMU_OPTION_full_screen
:
5113 case QEMU_OPTION_no_frame
:
5116 case QEMU_OPTION_alt_grab
:
5119 case QEMU_OPTION_no_quit
:
5122 case QEMU_OPTION_sdl
:
5126 case QEMU_OPTION_pidfile
:
5130 case QEMU_OPTION_win2k_hack
:
5131 win2k_install_hack
= 1;
5133 case QEMU_OPTION_rtc_td_hack
:
5136 case QEMU_OPTION_acpitable
:
5137 if(acpi_table_add(optarg
) < 0) {
5138 fprintf(stderr
, "Wrong acpi table provided\n");
5144 case QEMU_OPTION_no_kqemu
:
5147 case QEMU_OPTION_kernel_kqemu
:
5152 case QEMU_OPTION_enable_kvm
:
5159 case QEMU_OPTION_usb
:
5162 case QEMU_OPTION_usbdevice
:
5164 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5165 fprintf(stderr
, "Too many USB devices\n");
5168 usb_devices
[usb_devices_index
] = optarg
;
5169 usb_devices_index
++;
5171 case QEMU_OPTION_smp
:
5172 smp_cpus
= atoi(optarg
);
5174 fprintf(stderr
, "Invalid number of CPUs\n");
5178 case QEMU_OPTION_vnc
:
5179 vnc_display
= optarg
;
5181 case QEMU_OPTION_no_acpi
:
5184 case QEMU_OPTION_no_hpet
:
5187 case QEMU_OPTION_no_reboot
:
5190 case QEMU_OPTION_no_shutdown
:
5193 case QEMU_OPTION_show_cursor
:
5196 case QEMU_OPTION_uuid
:
5197 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5198 fprintf(stderr
, "Fail to parse UUID string."
5199 " Wrong format.\n");
5203 case QEMU_OPTION_daemonize
:
5206 case QEMU_OPTION_option_rom
:
5207 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5208 fprintf(stderr
, "Too many option ROMs\n");
5211 option_rom
[nb_option_roms
] = optarg
;
5214 case QEMU_OPTION_semihosting
:
5215 semihosting_enabled
= 1;
5217 case QEMU_OPTION_name
:
5220 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5221 case QEMU_OPTION_prom_env
:
5222 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5223 fprintf(stderr
, "Too many prom variables\n");
5226 prom_envs
[nb_prom_envs
] = optarg
;
5231 case QEMU_OPTION_old_param
:
5235 case QEMU_OPTION_clock
:
5236 configure_alarms(optarg
);
5238 case QEMU_OPTION_startdate
:
5241 time_t rtc_start_date
;
5242 if (!strcmp(optarg
, "now")) {
5243 rtc_date_offset
= -1;
5245 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5253 } else if (sscanf(optarg
, "%d-%d-%d",
5256 &tm
.tm_mday
) == 3) {
5265 rtc_start_date
= mktimegm(&tm
);
5266 if (rtc_start_date
== -1) {
5268 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5269 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5272 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5276 case QEMU_OPTION_tb_size
:
5277 tb_size
= strtol(optarg
, NULL
, 0);
5281 case QEMU_OPTION_icount
:
5283 if (strcmp(optarg
, "auto") == 0) {
5284 icount_time_shift
= -1;
5286 icount_time_shift
= strtol(optarg
, NULL
, 0);
5289 case QEMU_OPTION_incoming
:
5292 case QEMU_OPTION_chroot
:
5293 chroot_dir
= optarg
;
5295 case QEMU_OPTION_runas
:
5302 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5303 if (kvm_allowed
&& kqemu_allowed
) {
5305 "You can not enable both KVM and kqemu at the same time\n");
5310 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5311 if (smp_cpus
> machine
->max_cpus
) {
5312 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5313 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5319 if (serial_device_index
== 0)
5320 serial_devices
[0] = "stdio";
5321 if (parallel_device_index
== 0)
5322 parallel_devices
[0] = "null";
5323 if (strncmp(monitor_device
, "vc", 2) == 0)
5324 monitor_device
= "stdio";
5331 if (pipe(fds
) == -1)
5342 len
= read(fds
[0], &status
, 1);
5343 if (len
== -1 && (errno
== EINTR
))
5348 else if (status
== 1) {
5349 fprintf(stderr
, "Could not acquire pidfile\n");
5366 signal(SIGTSTP
, SIG_IGN
);
5367 signal(SIGTTOU
, SIG_IGN
);
5368 signal(SIGTTIN
, SIG_IGN
);
5372 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5375 write(fds
[1], &status
, 1);
5377 fprintf(stderr
, "Could not acquire pid file\n");
5385 linux_boot
= (kernel_filename
!= NULL
);
5386 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5388 if (!linux_boot
&& net_boot
== 0 &&
5389 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5392 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5393 fprintf(stderr
, "-append only allowed with -kernel option\n");
5397 if (!linux_boot
&& initrd_filename
!= NULL
) {
5398 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5402 /* boot to floppy or the default cd if no hard disk defined yet */
5403 if (!boot_devices
[0]) {
5404 boot_devices
= "cad";
5406 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5409 if (init_timer_alarm() < 0) {
5410 fprintf(stderr
, "could not initialize alarm timer\n");
5413 if (use_icount
&& icount_time_shift
< 0) {
5415 /* 125MIPS seems a reasonable initial guess at the guest speed.
5416 It will be corrected fairly quickly anyway. */
5417 icount_time_shift
= 3;
5418 init_icount_adjust();
5425 /* init network clients */
5426 if (nb_net_clients
== 0) {
5427 /* if no clients, we use a default config */
5428 net_clients
[nb_net_clients
++] = "nic";
5430 net_clients
[nb_net_clients
++] = "user";
5434 for(i
= 0;i
< nb_net_clients
; i
++) {
5435 if (net_client_parse(net_clients
[i
]) < 0)
5441 /* XXX: this should be moved in the PC machine instantiation code */
5442 if (net_boot
!= 0) {
5444 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5445 const char *model
= nd_table
[i
].model
;
5447 if (net_boot
& (1 << i
)) {
5450 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5451 if (get_image_size(buf
) > 0) {
5452 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5453 fprintf(stderr
, "Too many option ROMs\n");
5456 option_rom
[nb_option_roms
] = strdup(buf
);
5463 fprintf(stderr
, "No valid PXE rom found for network device\n");
5469 /* init the bluetooth world */
5470 for (i
= 0; i
< nb_bt_opts
; i
++)
5471 if (bt_parse(bt_opts
[i
]))
5474 /* init the memory */
5475 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5477 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5479 if (ram_size
< phys_ram_size
) {
5480 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5481 machine
->name
, (unsigned long long) phys_ram_size
);
5485 phys_ram_size
= ram_size
;
5487 ram_size
= phys_ram_size
;
5490 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5492 phys_ram_size
+= ram_size
;
5495 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5496 if (!phys_ram_base
) {
5497 fprintf(stderr
, "Could not allocate physical memory\n");
5501 /* init the dynamic translator */
5502 cpu_exec_init_all(tb_size
* 1024 * 1024);
5506 /* we always create the cdrom drive, even if no disk is there */
5508 if (nb_drives_opt
< MAX_DRIVES
)
5509 drive_add(NULL
, CDROM_ALIAS
);
5511 /* we always create at least one floppy */
5513 if (nb_drives_opt
< MAX_DRIVES
)
5514 drive_add(NULL
, FD_ALIAS
, 0);
5516 /* we always create one sd slot, even if no card is in it */
5518 if (nb_drives_opt
< MAX_DRIVES
)
5519 drive_add(NULL
, SD_ALIAS
);
5521 /* open the virtual block devices */
5523 for(i
= 0; i
< nb_drives_opt
; i
++)
5524 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5527 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5528 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5531 /* must be after terminal init, SDL library changes signal handlers */
5535 /* Maintain compatibility with multiple stdio monitors */
5536 if (!strcmp(monitor_device
,"stdio")) {
5537 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5538 const char *devname
= serial_devices
[i
];
5539 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5540 monitor_device
= NULL
;
5542 } else if (devname
&& !strcmp(devname
,"stdio")) {
5543 monitor_device
= NULL
;
5544 serial_devices
[i
] = "mon:stdio";
5550 if (kvm_enabled()) {
5553 ret
= kvm_init(smp_cpus
);
5555 fprintf(stderr
, "failed to initialize KVM\n");
5560 if (monitor_device
) {
5561 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5563 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5568 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5569 const char *devname
= serial_devices
[i
];
5570 if (devname
&& strcmp(devname
, "none")) {
5572 snprintf(label
, sizeof(label
), "serial%d", i
);
5573 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5574 if (!serial_hds
[i
]) {
5575 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5582 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5583 const char *devname
= parallel_devices
[i
];
5584 if (devname
&& strcmp(devname
, "none")) {
5586 snprintf(label
, sizeof(label
), "parallel%d", i
);
5587 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5588 if (!parallel_hds
[i
]) {
5589 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5596 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5597 const char *devname
= virtio_consoles
[i
];
5598 if (devname
&& strcmp(devname
, "none")) {
5600 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5601 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5602 if (!virtcon_hds
[i
]) {
5603 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5610 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5611 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5613 current_machine
= machine
;
5615 /* Set KVM's vcpu state to qemu's initial CPUState. */
5616 if (kvm_enabled()) {
5619 ret
= kvm_sync_vcpus();
5621 fprintf(stderr
, "failed to initialize vcpus\n");
5626 /* init USB devices */
5628 for(i
= 0; i
< usb_devices_index
; i
++) {
5629 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5630 fprintf(stderr
, "Warning: could not add USB device %s\n",
5637 dumb_display_init();
5638 /* just use the first displaystate for the moment */
5643 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5647 #if defined(CONFIG_CURSES)
5649 /* At the moment curses cannot be used with other displays */
5650 curses_display_init(ds
, full_screen
);
5654 if (vnc_display
!= NULL
) {
5655 vnc_display_init(ds
);
5656 if (vnc_display_open(ds
, vnc_display
) < 0)
5659 #if defined(CONFIG_SDL)
5660 if (sdl
|| !vnc_display
)
5661 sdl_display_init(ds
, full_screen
, no_frame
);
5662 #elif defined(CONFIG_COCOA)
5663 if (sdl
|| !vnc_display
)
5664 cocoa_display_init(ds
, full_screen
);
5670 dcl
= ds
->listeners
;
5671 while (dcl
!= NULL
) {
5672 if (dcl
->dpy_refresh
!= NULL
) {
5673 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5674 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5679 if (nographic
|| (vnc_display
&& !sdl
)) {
5680 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5681 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5684 text_consoles_set_display(display_state
);
5685 qemu_chr_initial_reset();
5687 if (monitor_device
&& monitor_hd
)
5688 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5690 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5691 const char *devname
= serial_devices
[i
];
5692 if (devname
&& strcmp(devname
, "none")) {
5694 snprintf(label
, sizeof(label
), "serial%d", i
);
5695 if (strstart(devname
, "vc", 0))
5696 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5700 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5701 const char *devname
= parallel_devices
[i
];
5702 if (devname
&& strcmp(devname
, "none")) {
5704 snprintf(label
, sizeof(label
), "parallel%d", i
);
5705 if (strstart(devname
, "vc", 0))
5706 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5710 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5711 const char *devname
= virtio_consoles
[i
];
5712 if (virtcon_hds
[i
] && devname
) {
5714 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5715 if (strstart(devname
, "vc", 0))
5716 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5720 #ifdef CONFIG_GDBSTUB
5722 /* XXX: use standard host:port notation and modify options
5724 if (gdbserver_start(gdbstub_port
) < 0) {
5725 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5733 do_loadvm(cur_mon
, loadvm
);
5736 autostart
= 0; /* fixme how to deal with -daemonize */
5737 qemu_start_incoming_migration(incoming
);
5748 len
= write(fds
[1], &status
, 1);
5749 if (len
== -1 && (errno
== EINTR
))
5756 TFR(fd
= open("/dev/null", O_RDWR
));
5763 pwd
= getpwnam(run_as
);
5765 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5771 if (chroot(chroot_dir
) < 0) {
5772 fprintf(stderr
, "chroot failed\n");
5779 if (setgid(pwd
->pw_gid
) < 0) {
5780 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5783 if (setuid(pwd
->pw_uid
) < 0) {
5784 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5787 if (setuid(0) != -1) {
5788 fprintf(stderr
, "Dropping privileges failed\n");