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"
2143 #define CDROM_ALIAS "index=1,media=cdrom"
2145 #define CDROM_ALIAS "index=2,media=cdrom"
2147 #define FD_ALIAS "index=%d,if=floppy"
2148 #define PFLASH_ALIAS "if=pflash"
2149 #define MTD_ALIAS "if=mtd"
2150 #define SD_ALIAS "index=0,if=sd"
2152 static int drive_opt_get_free_idx(void)
2156 for (index
= 0; index
< MAX_DRIVES
; index
++)
2157 if (!drives_opt
[index
].used
) {
2158 drives_opt
[index
].used
= 1;
2165 static int drive_get_free_idx(void)
2169 for (index
= 0; index
< MAX_DRIVES
; index
++)
2170 if (!drives_table
[index
].used
) {
2171 drives_table
[index
].used
= 1;
2178 int drive_add(const char *file
, const char *fmt
, ...)
2181 int index
= drive_opt_get_free_idx();
2183 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2184 fprintf(stderr
, "qemu: too many drives\n");
2188 drives_opt
[index
].file
= file
;
2190 vsnprintf(drives_opt
[index
].opt
,
2191 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2198 void drive_remove(int index
)
2200 drives_opt
[index
].used
= 0;
2204 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2208 /* seek interface, bus and unit */
2210 for (index
= 0; index
< MAX_DRIVES
; index
++)
2211 if (drives_table
[index
].type
== type
&&
2212 drives_table
[index
].bus
== bus
&&
2213 drives_table
[index
].unit
== unit
&&
2214 drives_table
[index
].used
)
2220 int drive_get_max_bus(BlockInterfaceType type
)
2226 for (index
= 0; index
< nb_drives
; index
++) {
2227 if(drives_table
[index
].type
== type
&&
2228 drives_table
[index
].bus
> max_bus
)
2229 max_bus
= drives_table
[index
].bus
;
2234 const char *drive_get_serial(BlockDriverState
*bdrv
)
2238 for (index
= 0; index
< nb_drives
; index
++)
2239 if (drives_table
[index
].bdrv
== bdrv
)
2240 return drives_table
[index
].serial
;
2245 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2249 for (index
= 0; index
< nb_drives
; index
++)
2250 if (drives_table
[index
].bdrv
== bdrv
)
2251 return drives_table
[index
].onerror
;
2253 return BLOCK_ERR_STOP_ENOSPC
;
2256 static void bdrv_format_print(void *opaque
, const char *name
)
2258 fprintf(stderr
, " %s", name
);
2261 void drive_uninit(BlockDriverState
*bdrv
)
2265 for (i
= 0; i
< MAX_DRIVES
; i
++)
2266 if (drives_table
[i
].bdrv
== bdrv
) {
2267 drives_table
[i
].bdrv
= NULL
;
2268 drives_table
[i
].used
= 0;
2269 drive_remove(drives_table
[i
].drive_opt_idx
);
2275 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2281 const char *mediastr
= "";
2282 BlockInterfaceType type
;
2283 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2284 int bus_id
, unit_id
;
2285 int cyls
, heads
, secs
, translation
;
2286 BlockDriverState
*bdrv
;
2287 BlockDriver
*drv
= NULL
;
2288 QEMUMachine
*machine
= opaque
;
2292 int bdrv_flags
, onerror
;
2293 int drives_table_idx
;
2294 char *str
= arg
->opt
;
2295 static const char * const params
[] = { "bus", "unit", "if", "index",
2296 "cyls", "heads", "secs", "trans",
2297 "media", "snapshot", "file",
2298 "cache", "format", "serial", "werror",
2301 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2302 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2308 cyls
= heads
= secs
= 0;
2311 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2315 if (machine
->use_scsi
) {
2317 max_devs
= MAX_SCSI_DEVS
;
2318 pstrcpy(devname
, sizeof(devname
), "scsi");
2321 max_devs
= MAX_IDE_DEVS
;
2322 pstrcpy(devname
, sizeof(devname
), "ide");
2326 /* extract parameters */
2328 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2329 bus_id
= strtol(buf
, NULL
, 0);
2331 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2336 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2337 unit_id
= strtol(buf
, NULL
, 0);
2339 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2344 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2345 pstrcpy(devname
, sizeof(devname
), buf
);
2346 if (!strcmp(buf
, "ide")) {
2348 max_devs
= MAX_IDE_DEVS
;
2349 } else if (!strcmp(buf
, "scsi")) {
2351 max_devs
= MAX_SCSI_DEVS
;
2352 } else if (!strcmp(buf
, "floppy")) {
2355 } else if (!strcmp(buf
, "pflash")) {
2358 } else if (!strcmp(buf
, "mtd")) {
2361 } else if (!strcmp(buf
, "sd")) {
2364 } else if (!strcmp(buf
, "virtio")) {
2368 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2373 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2374 index
= strtol(buf
, NULL
, 0);
2376 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2381 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2382 cyls
= strtol(buf
, NULL
, 0);
2385 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2386 heads
= strtol(buf
, NULL
, 0);
2389 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2390 secs
= strtol(buf
, NULL
, 0);
2393 if (cyls
|| heads
|| secs
) {
2394 if (cyls
< 1 || cyls
> 16383) {
2395 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2398 if (heads
< 1 || heads
> 16) {
2399 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2402 if (secs
< 1 || secs
> 63) {
2403 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2408 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2411 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2415 if (!strcmp(buf
, "none"))
2416 translation
= BIOS_ATA_TRANSLATION_NONE
;
2417 else if (!strcmp(buf
, "lba"))
2418 translation
= BIOS_ATA_TRANSLATION_LBA
;
2419 else if (!strcmp(buf
, "auto"))
2420 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2422 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2427 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2428 if (!strcmp(buf
, "disk")) {
2430 } else if (!strcmp(buf
, "cdrom")) {
2431 if (cyls
|| secs
|| heads
) {
2433 "qemu: '%s' invalid physical CHS format\n", str
);
2436 media
= MEDIA_CDROM
;
2438 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2443 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2444 if (!strcmp(buf
, "on"))
2446 else if (!strcmp(buf
, "off"))
2449 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2454 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2455 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2457 else if (!strcmp(buf
, "writethrough"))
2459 else if (!strcmp(buf
, "writeback"))
2462 fprintf(stderr
, "qemu: invalid cache option\n");
2467 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2468 if (strcmp(buf
, "?") == 0) {
2469 fprintf(stderr
, "qemu: Supported formats:");
2470 bdrv_iterate_format(bdrv_format_print
, NULL
);
2471 fprintf(stderr
, "\n");
2474 drv
= bdrv_find_format(buf
);
2476 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2481 if (arg
->file
== NULL
)
2482 get_param_value(file
, sizeof(file
), "file", str
);
2484 pstrcpy(file
, sizeof(file
), arg
->file
);
2486 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2487 memset(serial
, 0, sizeof(serial
));
2489 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2490 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2491 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2492 fprintf(stderr
, "werror is no supported by this format\n");
2495 if (!strcmp(buf
, "ignore"))
2496 onerror
= BLOCK_ERR_IGNORE
;
2497 else if (!strcmp(buf
, "enospc"))
2498 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2499 else if (!strcmp(buf
, "stop"))
2500 onerror
= BLOCK_ERR_STOP_ANY
;
2501 else if (!strcmp(buf
, "report"))
2502 onerror
= BLOCK_ERR_REPORT
;
2504 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2509 /* compute bus and unit according index */
2512 if (bus_id
!= 0 || unit_id
!= -1) {
2514 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2522 unit_id
= index
% max_devs
;
2523 bus_id
= index
/ max_devs
;
2527 /* if user doesn't specify a unit_id,
2528 * try to find the first free
2531 if (unit_id
== -1) {
2533 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2535 if (max_devs
&& unit_id
>= max_devs
) {
2536 unit_id
-= max_devs
;
2544 if (max_devs
&& unit_id
>= max_devs
) {
2545 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2546 str
, unit_id
, max_devs
- 1);
2551 * ignore multiple definitions
2554 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2559 if (type
== IF_IDE
|| type
== IF_SCSI
)
2560 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2562 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2563 devname
, bus_id
, mediastr
, unit_id
);
2565 snprintf(buf
, sizeof(buf
), "%s%s%i",
2566 devname
, mediastr
, unit_id
);
2567 bdrv
= bdrv_new(buf
);
2568 drives_table_idx
= drive_get_free_idx();
2569 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2570 drives_table
[drives_table_idx
].type
= type
;
2571 drives_table
[drives_table_idx
].bus
= bus_id
;
2572 drives_table
[drives_table_idx
].unit
= unit_id
;
2573 drives_table
[drives_table_idx
].onerror
= onerror
;
2574 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2575 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2584 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2585 bdrv_set_translation_hint(bdrv
, translation
);
2589 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2594 /* FIXME: This isn't really a floppy, but it's a reasonable
2597 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2608 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2609 cache
= 2; /* always use write-back with snapshot */
2611 if (cache
== 0) /* no caching */
2612 bdrv_flags
|= BDRV_O_NOCACHE
;
2613 else if (cache
== 2) /* write-back */
2614 bdrv_flags
|= BDRV_O_CACHE_WB
;
2615 else if (cache
== 3) /* not specified */
2616 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2617 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2618 fprintf(stderr
, "qemu: could not open disk image %s\n",
2622 if (bdrv_key_required(bdrv
))
2624 return drives_table_idx
;
2627 /***********************************************************/
2630 static USBPort
*used_usb_ports
;
2631 static USBPort
*free_usb_ports
;
2633 /* ??? Maybe change this to register a hub to keep track of the topology. */
2634 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2635 usb_attachfn attach
)
2637 port
->opaque
= opaque
;
2638 port
->index
= index
;
2639 port
->attach
= attach
;
2640 port
->next
= free_usb_ports
;
2641 free_usb_ports
= port
;
2644 int usb_device_add_dev(USBDevice
*dev
)
2648 /* Find a USB port to add the device to. */
2649 port
= free_usb_ports
;
2653 /* Create a new hub and chain it on. */
2654 free_usb_ports
= NULL
;
2655 port
->next
= used_usb_ports
;
2656 used_usb_ports
= port
;
2658 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2659 usb_attach(port
, hub
);
2660 port
= free_usb_ports
;
2663 free_usb_ports
= port
->next
;
2664 port
->next
= used_usb_ports
;
2665 used_usb_ports
= port
;
2666 usb_attach(port
, dev
);
2670 static void usb_msd_password_cb(void *opaque
, int err
)
2672 USBDevice
*dev
= opaque
;
2675 usb_device_add_dev(dev
);
2677 dev
->handle_destroy(dev
);
2680 static int usb_device_add(const char *devname
, int is_hotplug
)
2685 if (!free_usb_ports
)
2688 if (strstart(devname
, "host:", &p
)) {
2689 dev
= usb_host_device_open(p
);
2690 } else if (!strcmp(devname
, "mouse")) {
2691 dev
= usb_mouse_init();
2692 } else if (!strcmp(devname
, "tablet")) {
2693 dev
= usb_tablet_init();
2694 } else if (!strcmp(devname
, "keyboard")) {
2695 dev
= usb_keyboard_init();
2696 } else if (strstart(devname
, "disk:", &p
)) {
2697 BlockDriverState
*bs
;
2699 dev
= usb_msd_init(p
);
2702 bs
= usb_msd_get_bdrv(dev
);
2703 if (bdrv_key_required(bs
)) {
2706 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2711 } else if (!strcmp(devname
, "wacom-tablet")) {
2712 dev
= usb_wacom_init();
2713 } else if (strstart(devname
, "serial:", &p
)) {
2714 dev
= usb_serial_init(p
);
2715 #ifdef CONFIG_BRLAPI
2716 } else if (!strcmp(devname
, "braille")) {
2717 dev
= usb_baum_init();
2719 } else if (strstart(devname
, "net:", &p
)) {
2722 if (net_client_init("nic", p
) < 0)
2724 nd_table
[nic
].model
= "usb";
2725 dev
= usb_net_init(&nd_table
[nic
]);
2726 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2727 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2728 bt_new_hci(qemu_find_bt_vlan(0)));
2735 return usb_device_add_dev(dev
);
2738 int usb_device_del_addr(int bus_num
, int addr
)
2744 if (!used_usb_ports
)
2750 lastp
= &used_usb_ports
;
2751 port
= used_usb_ports
;
2752 while (port
&& port
->dev
->addr
!= addr
) {
2753 lastp
= &port
->next
;
2761 *lastp
= port
->next
;
2762 usb_attach(port
, NULL
);
2763 dev
->handle_destroy(dev
);
2764 port
->next
= free_usb_ports
;
2765 free_usb_ports
= port
;
2769 static int usb_device_del(const char *devname
)
2774 if (strstart(devname
, "host:", &p
))
2775 return usb_host_device_close(p
);
2777 if (!used_usb_ports
)
2780 p
= strchr(devname
, '.');
2783 bus_num
= strtoul(devname
, NULL
, 0);
2784 addr
= strtoul(p
+ 1, NULL
, 0);
2786 return usb_device_del_addr(bus_num
, addr
);
2789 void do_usb_add(Monitor
*mon
, const char *devname
)
2791 usb_device_add(devname
, 1);
2794 void do_usb_del(Monitor
*mon
, const char *devname
)
2796 usb_device_del(devname
);
2799 void usb_info(Monitor
*mon
)
2803 const char *speed_str
;
2806 monitor_printf(mon
, "USB support not enabled\n");
2810 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2814 switch(dev
->speed
) {
2818 case USB_SPEED_FULL
:
2821 case USB_SPEED_HIGH
:
2828 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2829 0, dev
->addr
, speed_str
, dev
->devname
);
2833 /***********************************************************/
2834 /* PCMCIA/Cardbus */
2836 static struct pcmcia_socket_entry_s
{
2837 struct pcmcia_socket_s
*socket
;
2838 struct pcmcia_socket_entry_s
*next
;
2839 } *pcmcia_sockets
= 0;
2841 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2843 struct pcmcia_socket_entry_s
*entry
;
2845 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2846 entry
->socket
= socket
;
2847 entry
->next
= pcmcia_sockets
;
2848 pcmcia_sockets
= entry
;
2851 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2853 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2855 ptr
= &pcmcia_sockets
;
2856 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2857 if (entry
->socket
== socket
) {
2863 void pcmcia_info(Monitor
*mon
)
2865 struct pcmcia_socket_entry_s
*iter
;
2867 if (!pcmcia_sockets
)
2868 monitor_printf(mon
, "No PCMCIA sockets\n");
2870 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2871 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2872 iter
->socket
->attached
? iter
->socket
->card_string
:
2876 /***********************************************************/
2877 /* register display */
2879 void register_displaystate(DisplayState
*ds
)
2889 DisplayState
*get_displaystate(void)
2891 return display_state
;
2896 static void dumb_display_init(void)
2898 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2899 ds
->surface
= qemu_create_displaysurface(640, 480, 32, 640 * 4);
2900 register_displaystate(ds
);
2903 /***********************************************************/
2906 #define MAX_IO_HANDLERS 64
2908 typedef struct IOHandlerRecord
{
2910 IOCanRWHandler
*fd_read_poll
;
2912 IOHandler
*fd_write
;
2915 /* temporary data */
2917 struct IOHandlerRecord
*next
;
2920 static IOHandlerRecord
*first_io_handler
;
2922 /* XXX: fd_read_poll should be suppressed, but an API change is
2923 necessary in the character devices to suppress fd_can_read(). */
2924 int qemu_set_fd_handler2(int fd
,
2925 IOCanRWHandler
*fd_read_poll
,
2927 IOHandler
*fd_write
,
2930 IOHandlerRecord
**pioh
, *ioh
;
2932 if (!fd_read
&& !fd_write
) {
2933 pioh
= &first_io_handler
;
2938 if (ioh
->fd
== fd
) {
2945 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2949 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2950 ioh
->next
= first_io_handler
;
2951 first_io_handler
= ioh
;
2954 ioh
->fd_read_poll
= fd_read_poll
;
2955 ioh
->fd_read
= fd_read
;
2956 ioh
->fd_write
= fd_write
;
2957 ioh
->opaque
= opaque
;
2963 int qemu_set_fd_handler(int fd
,
2965 IOHandler
*fd_write
,
2968 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2972 /***********************************************************/
2973 /* Polling handling */
2975 typedef struct PollingEntry
{
2978 struct PollingEntry
*next
;
2981 static PollingEntry
*first_polling_entry
;
2983 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2985 PollingEntry
**ppe
, *pe
;
2986 pe
= qemu_mallocz(sizeof(PollingEntry
));
2988 pe
->opaque
= opaque
;
2989 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2994 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2996 PollingEntry
**ppe
, *pe
;
2997 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2999 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3007 /***********************************************************/
3008 /* Wait objects support */
3009 typedef struct WaitObjects
{
3011 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3012 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3013 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3016 static WaitObjects wait_objects
= {0};
3018 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3020 WaitObjects
*w
= &wait_objects
;
3022 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3024 w
->events
[w
->num
] = handle
;
3025 w
->func
[w
->num
] = func
;
3026 w
->opaque
[w
->num
] = opaque
;
3031 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3034 WaitObjects
*w
= &wait_objects
;
3037 for (i
= 0; i
< w
->num
; i
++) {
3038 if (w
->events
[i
] == handle
)
3041 w
->events
[i
] = w
->events
[i
+ 1];
3042 w
->func
[i
] = w
->func
[i
+ 1];
3043 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3051 /***********************************************************/
3052 /* ram save/restore */
3054 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3058 v
= qemu_get_byte(f
);
3061 if (qemu_get_buffer(f
, buf
, len
) != len
)
3065 v
= qemu_get_byte(f
);
3066 memset(buf
, v
, len
);
3072 if (qemu_file_has_error(f
))
3078 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3083 if (qemu_get_be32(f
) != phys_ram_size
)
3085 for(i
= 0; i
< phys_ram_size
; i
+= TARGET_PAGE_SIZE
) {
3086 ret
= ram_get_page(f
, phys_ram_base
+ i
, TARGET_PAGE_SIZE
);
3093 #define BDRV_HASH_BLOCK_SIZE 1024
3094 #define IOBUF_SIZE 4096
3095 #define RAM_CBLOCK_MAGIC 0xfabe
3097 typedef struct RamDecompressState
{
3100 uint8_t buf
[IOBUF_SIZE
];
3101 } RamDecompressState
;
3103 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3106 memset(s
, 0, sizeof(*s
));
3108 ret
= inflateInit(&s
->zstream
);
3114 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3118 s
->zstream
.avail_out
= len
;
3119 s
->zstream
.next_out
= buf
;
3120 while (s
->zstream
.avail_out
> 0) {
3121 if (s
->zstream
.avail_in
== 0) {
3122 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3124 clen
= qemu_get_be16(s
->f
);
3125 if (clen
> IOBUF_SIZE
)
3127 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3128 s
->zstream
.avail_in
= clen
;
3129 s
->zstream
.next_in
= s
->buf
;
3131 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3132 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3139 static void ram_decompress_close(RamDecompressState
*s
)
3141 inflateEnd(&s
->zstream
);
3144 #define RAM_SAVE_FLAG_FULL 0x01
3145 #define RAM_SAVE_FLAG_COMPRESS 0x02
3146 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3147 #define RAM_SAVE_FLAG_PAGE 0x08
3148 #define RAM_SAVE_FLAG_EOS 0x10
3150 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3152 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3153 uint32_t *array
= (uint32_t *)page
;
3156 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3157 if (array
[i
] != val
)
3164 static int ram_save_block(QEMUFile
*f
)
3166 static ram_addr_t current_addr
= 0;
3167 ram_addr_t saved_addr
= current_addr
;
3168 ram_addr_t addr
= 0;
3171 while (addr
< phys_ram_size
) {
3172 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3175 cpu_physical_memory_reset_dirty(current_addr
,
3176 current_addr
+ TARGET_PAGE_SIZE
,
3177 MIGRATION_DIRTY_FLAG
);
3179 ch
= *(phys_ram_base
+ current_addr
);
3181 if (is_dup_page(phys_ram_base
+ current_addr
, ch
)) {
3182 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3183 qemu_put_byte(f
, ch
);
3185 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3186 qemu_put_buffer(f
, phys_ram_base
+ current_addr
, TARGET_PAGE_SIZE
);
3192 addr
+= TARGET_PAGE_SIZE
;
3193 current_addr
= (saved_addr
+ addr
) % phys_ram_size
;
3199 static ram_addr_t ram_save_threshold
= 10;
3201 static ram_addr_t
ram_save_remaining(void)
3204 ram_addr_t count
= 0;
3206 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3207 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3214 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3219 /* Make sure all dirty bits are set */
3220 for (addr
= 0; addr
< phys_ram_size
; addr
+= TARGET_PAGE_SIZE
) {
3221 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3222 cpu_physical_memory_set_dirty(addr
);
3225 /* Enable dirty memory tracking */
3226 cpu_physical_memory_set_dirty_tracking(1);
3228 qemu_put_be64(f
, phys_ram_size
| RAM_SAVE_FLAG_MEM_SIZE
);
3231 while (!qemu_file_rate_limit(f
)) {
3234 ret
= ram_save_block(f
);
3235 if (ret
== 0) /* no more blocks */
3239 /* try transferring iterative blocks of memory */
3242 cpu_physical_memory_set_dirty_tracking(0);
3244 /* flush all remaining blocks regardless of rate limiting */
3245 while (ram_save_block(f
) != 0);
3248 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3250 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3253 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3255 RamDecompressState s1
, *s
= &s1
;
3259 if (ram_decompress_open(s
, f
) < 0)
3261 for(i
= 0; i
< phys_ram_size
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3262 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3263 fprintf(stderr
, "Error while reading ram block header\n");
3267 if (ram_decompress_buf(s
, phys_ram_base
+ i
, BDRV_HASH_BLOCK_SIZE
) < 0) {
3268 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3273 printf("Error block header\n");
3277 ram_decompress_close(s
);
3282 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3287 if (version_id
== 1)
3288 return ram_load_v1(f
, opaque
);
3290 if (version_id
== 2) {
3291 if (qemu_get_be32(f
) != phys_ram_size
)
3293 return ram_load_dead(f
, opaque
);
3296 if (version_id
!= 3)
3300 addr
= qemu_get_be64(f
);
3302 flags
= addr
& ~TARGET_PAGE_MASK
;
3303 addr
&= TARGET_PAGE_MASK
;
3305 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3306 if (addr
!= phys_ram_size
)
3310 if (flags
& RAM_SAVE_FLAG_FULL
) {
3311 if (ram_load_dead(f
, opaque
) < 0)
3315 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3316 uint8_t ch
= qemu_get_byte(f
);
3317 memset(phys_ram_base
+ addr
, ch
, TARGET_PAGE_SIZE
);
3318 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3319 qemu_get_buffer(f
, phys_ram_base
+ addr
, TARGET_PAGE_SIZE
);
3320 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3325 void qemu_service_io(void)
3327 CPUState
*env
= cpu_single_env
;
3331 if (env
->kqemu_enabled
) {
3332 kqemu_cpu_interrupt(env
);
3338 /***********************************************************/
3339 /* bottom halves (can be seen as timers which expire ASAP) */
3350 static QEMUBH
*first_bh
= NULL
;
3352 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3355 bh
= qemu_mallocz(sizeof(QEMUBH
));
3357 bh
->opaque
= opaque
;
3358 bh
->next
= first_bh
;
3363 int qemu_bh_poll(void)
3369 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3370 if (!bh
->deleted
&& bh
->scheduled
) {
3379 /* remove deleted bhs */
3393 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3401 void qemu_bh_schedule(QEMUBH
*bh
)
3403 CPUState
*env
= cpu_single_env
;
3408 /* stop the currently executing CPU to execute the BH ASAP */
3414 void qemu_bh_cancel(QEMUBH
*bh
)
3419 void qemu_bh_delete(QEMUBH
*bh
)
3425 static void qemu_bh_update_timeout(int *timeout
)
3429 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3430 if (!bh
->deleted
&& bh
->scheduled
) {
3432 /* idle bottom halves will be polled at least
3434 *timeout
= MIN(10, *timeout
);
3436 /* non-idle bottom halves will be executed
3445 /***********************************************************/
3446 /* machine registration */
3448 static QEMUMachine
*first_machine
= NULL
;
3449 QEMUMachine
*current_machine
= NULL
;
3451 int qemu_register_machine(QEMUMachine
*m
)
3454 pm
= &first_machine
;
3462 static QEMUMachine
*find_machine(const char *name
)
3466 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3467 if (!strcmp(m
->name
, name
))
3473 /***********************************************************/
3474 /* main execution loop */
3476 static void gui_update(void *opaque
)
3478 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3479 DisplayState
*ds
= opaque
;
3480 DisplayChangeListener
*dcl
= ds
->listeners
;
3484 while (dcl
!= NULL
) {
3485 if (dcl
->gui_timer_interval
&&
3486 dcl
->gui_timer_interval
< interval
)
3487 interval
= dcl
->gui_timer_interval
;
3490 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3493 static void nographic_update(void *opaque
)
3495 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3497 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3500 struct vm_change_state_entry
{
3501 VMChangeStateHandler
*cb
;
3503 LIST_ENTRY (vm_change_state_entry
) entries
;
3506 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3508 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3511 VMChangeStateEntry
*e
;
3513 e
= qemu_mallocz(sizeof (*e
));
3517 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3521 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3523 LIST_REMOVE (e
, entries
);
3527 static void vm_state_notify(int running
, int reason
)
3529 VMChangeStateEntry
*e
;
3531 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3532 e
->cb(e
->opaque
, running
, reason
);
3541 vm_state_notify(1, 0);
3542 qemu_rearm_alarm_timer(alarm_timer
);
3546 void vm_stop(int reason
)
3549 cpu_disable_ticks();
3551 vm_state_notify(0, reason
);
3555 /* reset/shutdown handler */
3557 typedef struct QEMUResetEntry
{
3558 QEMUResetHandler
*func
;
3560 struct QEMUResetEntry
*next
;
3563 static QEMUResetEntry
*first_reset_entry
;
3564 static int reset_requested
;
3565 static int shutdown_requested
;
3566 static int powerdown_requested
;
3568 int qemu_shutdown_requested(void)
3570 int r
= shutdown_requested
;
3571 shutdown_requested
= 0;
3575 int qemu_reset_requested(void)
3577 int r
= reset_requested
;
3578 reset_requested
= 0;
3582 int qemu_powerdown_requested(void)
3584 int r
= powerdown_requested
;
3585 powerdown_requested
= 0;
3589 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3591 QEMUResetEntry
**pre
, *re
;
3593 pre
= &first_reset_entry
;
3594 while (*pre
!= NULL
)
3595 pre
= &(*pre
)->next
;
3596 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3598 re
->opaque
= opaque
;
3603 void qemu_system_reset(void)
3607 /* reset all devices */
3608 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3609 re
->func(re
->opaque
);
3613 void qemu_system_reset_request(void)
3616 shutdown_requested
= 1;
3618 reset_requested
= 1;
3621 cpu_exit(cpu_single_env
);
3624 void qemu_system_shutdown_request(void)
3626 shutdown_requested
= 1;
3628 cpu_exit(cpu_single_env
);
3631 void qemu_system_powerdown_request(void)
3633 powerdown_requested
= 1;
3635 cpu_exit(cpu_single_env
);
3639 static void host_main_loop_wait(int *timeout
)
3645 /* XXX: need to suppress polling by better using win32 events */
3647 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3648 ret
|= pe
->func(pe
->opaque
);
3652 WaitObjects
*w
= &wait_objects
;
3654 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3655 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3656 if (w
->func
[ret
- WAIT_OBJECT_0
])
3657 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3659 /* Check for additional signaled events */
3660 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3662 /* Check if event is signaled */
3663 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3664 if(ret2
== WAIT_OBJECT_0
) {
3666 w
->func
[i
](w
->opaque
[i
]);
3667 } else if (ret2
== WAIT_TIMEOUT
) {
3669 err
= GetLastError();
3670 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3673 } else if (ret
== WAIT_TIMEOUT
) {
3675 err
= GetLastError();
3676 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3683 static void host_main_loop_wait(int *timeout
)
3688 void main_loop_wait(int timeout
)
3690 IOHandlerRecord
*ioh
;
3691 fd_set rfds
, wfds
, xfds
;
3695 qemu_bh_update_timeout(&timeout
);
3697 host_main_loop_wait(&timeout
);
3699 /* poll any events */
3700 /* XXX: separate device handlers from system ones */
3705 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3709 (!ioh
->fd_read_poll
||
3710 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3711 FD_SET(ioh
->fd
, &rfds
);
3715 if (ioh
->fd_write
) {
3716 FD_SET(ioh
->fd
, &wfds
);
3722 tv
.tv_sec
= timeout
/ 1000;
3723 tv
.tv_usec
= (timeout
% 1000) * 1000;
3725 #if defined(CONFIG_SLIRP)
3726 if (slirp_is_inited()) {
3727 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3730 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3732 IOHandlerRecord
**pioh
;
3734 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3735 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3736 ioh
->fd_read(ioh
->opaque
);
3738 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3739 ioh
->fd_write(ioh
->opaque
);
3743 /* remove deleted IO handlers */
3744 pioh
= &first_io_handler
;
3754 #if defined(CONFIG_SLIRP)
3755 if (slirp_is_inited()) {
3761 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3765 /* vm time timers */
3766 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3767 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3768 qemu_get_clock(vm_clock
));
3770 /* real time timers */
3771 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3772 qemu_get_clock(rt_clock
));
3774 /* Check bottom-halves last in case any of the earlier events triggered
3780 static int main_loop(void)
3783 #ifdef CONFIG_PROFILER
3788 cur_cpu
= first_cpu
;
3789 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3796 #ifdef CONFIG_PROFILER
3797 ti
= profile_getclock();
3802 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3803 env
->icount_decr
.u16
.low
= 0;
3804 env
->icount_extra
= 0;
3805 count
= qemu_next_deadline();
3806 count
= (count
+ (1 << icount_time_shift
) - 1)
3807 >> icount_time_shift
;
3808 qemu_icount
+= count
;
3809 decr
= (count
> 0xffff) ? 0xffff : count
;
3811 env
->icount_decr
.u16
.low
= decr
;
3812 env
->icount_extra
= count
;
3814 ret
= cpu_exec(env
);
3815 #ifdef CONFIG_PROFILER
3816 qemu_time
+= profile_getclock() - ti
;
3819 /* Fold pending instructions back into the
3820 instruction counter, and clear the interrupt flag. */
3821 qemu_icount
-= (env
->icount_decr
.u16
.low
3822 + env
->icount_extra
);
3823 env
->icount_decr
.u32
= 0;
3824 env
->icount_extra
= 0;
3826 next_cpu
= env
->next_cpu
?: first_cpu
;
3827 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3828 ret
= EXCP_INTERRUPT
;
3832 if (ret
== EXCP_HLT
) {
3833 /* Give the next CPU a chance to run. */
3837 if (ret
!= EXCP_HALTED
)
3839 /* all CPUs are halted ? */
3845 if (shutdown_requested
) {
3846 ret
= EXCP_INTERRUPT
;
3854 if (reset_requested
) {
3855 reset_requested
= 0;
3856 qemu_system_reset();
3857 ret
= EXCP_INTERRUPT
;
3859 if (powerdown_requested
) {
3860 powerdown_requested
= 0;
3861 qemu_system_powerdown();
3862 ret
= EXCP_INTERRUPT
;
3864 if (unlikely(ret
== EXCP_DEBUG
)) {
3865 gdb_set_stop_cpu(cur_cpu
);
3866 vm_stop(EXCP_DEBUG
);
3868 /* If all cpus are halted then wait until the next IRQ */
3869 /* XXX: use timeout computed from timers */
3870 if (ret
== EXCP_HALTED
) {
3874 /* Advance virtual time to the next event. */
3875 if (use_icount
== 1) {
3876 /* When not using an adaptive execution frequency
3877 we tend to get badly out of sync with real time,
3878 so just delay for a reasonable amount of time. */
3881 delta
= cpu_get_icount() - cpu_get_clock();
3884 /* If virtual time is ahead of real time then just
3886 timeout
= (delta
/ 1000000) + 1;
3888 /* Wait for either IO to occur or the next
3890 add
= qemu_next_deadline();
3891 /* We advance the timer before checking for IO.
3892 Limit the amount we advance so that early IO
3893 activity won't get the guest too far ahead. */
3897 add
= (add
+ (1 << icount_time_shift
) - 1)
3898 >> icount_time_shift
;
3900 timeout
= delta
/ 1000000;
3911 if (shutdown_requested
) {
3912 ret
= EXCP_INTERRUPT
;
3917 #ifdef CONFIG_PROFILER
3918 ti
= profile_getclock();
3920 main_loop_wait(timeout
);
3921 #ifdef CONFIG_PROFILER
3922 dev_time
+= profile_getclock() - ti
;
3925 cpu_disable_ticks();
3929 static void help(int exitcode
)
3931 /* Please keep in synch with QEMU_OPTION_ enums, qemu_options[]
3932 and qemu-doc.texi */
3933 printf("QEMU PC emulator version " QEMU_VERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n"
3934 "usage: %s [options] [disk_image]\n"
3936 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
3938 "Standard options:\n"
3939 "-h or -help display this help and exit\n"
3940 "-M machine select emulated machine (-M ? for list)\n"
3941 "-cpu cpu select CPU (-cpu ? for list)\n"
3942 "-smp n set the number of CPUs to 'n' [default=1]\n"
3943 "-fda/-fdb file use 'file' as floppy disk 0/1 image\n"
3944 "-hda/-hdb file use 'file' as IDE hard disk 0/1 image\n"
3945 "-hdc/-hdd file use 'file' as IDE hard disk 2/3 image\n"
3946 "-cdrom file use 'file' as IDE cdrom image (cdrom is ide1 master)\n"
3947 "-drive [file=file][,if=type][,bus=n][,unit=m][,media=d][,index=i]\n"
3948 " [,cyls=c,heads=h,secs=s[,trans=t]][,snapshot=on|off]\n"
3949 " [,cache=writethrough|writeback|none][,format=f][,serial=s]\n"
3950 " use 'file' as a drive image\n"
3951 "-mtdblock file use 'file' as on-board Flash memory image\n"
3952 "-sd file use 'file' as SecureDigital card image\n"
3953 "-pflash file use 'file' as a parallel flash image\n"
3954 "-boot [a|c|d|n] boot on floppy (a), hard disk (c), CD-ROM (d), or network (n)\n"
3955 "-snapshot write to temporary files instead of disk image files\n"
3956 "-m megs set virtual RAM size to megs MB [default=%d]\n"
3958 "-k language use keyboard layout (for example \"fr\" for French)\n"
3961 "-audio-help print list of audio drivers and their options\n"
3962 "-soundhw c1,... enable audio support\n"
3963 " and only specified sound cards (comma separated list)\n"
3964 " use -soundhw ? to get the list of supported cards\n"
3965 " use -soundhw all to enable all of them\n"
3967 "-usb enable the USB driver (will be the default soon)\n"
3968 "-usbdevice name add the host or guest USB device 'name'\n"
3969 "-name string set the name of the guest\n"
3970 "-uuid %%08x-%%04x-%%04x-%%04x-%%012x\n"
3971 " specify machine UUID\n"
3973 "Display options:\n"
3974 "-nographic disable graphical output and redirect serial I/Os to console\n"
3975 #ifdef CONFIG_CURSES
3976 "-curses use a curses/ncurses interface instead of SDL\n"
3979 "-no-frame open SDL window without a frame and window decorations\n"
3980 "-alt-grab use Ctrl-Alt-Shift to grab mouse (instead of Ctrl-Alt)\n"
3981 "-no-quit disable SDL window close capability\n"
3984 "-portrait rotate graphical output 90 deg left (only PXA LCD)\n"
3985 "-vga [std|cirrus|vmware|none]\n"
3986 " select video card type\n"
3987 "-full-screen start in full screen\n"
3988 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
3989 "-g WxH[xDEPTH] Set the initial graphical resolution and depth\n"
3991 "-vnc display start a VNC server on display\n"
3993 "Network options:\n"
3994 "-net nic[,vlan=n][,macaddr=addr][,model=type][,name=str]\n"
3995 " create a new Network Interface Card and connect it to VLAN 'n'\n"
3997 "-net user[,vlan=n][,name=str][,hostname=host]\n"
3998 " connect the user mode network stack to VLAN 'n' and send\n"
3999 " hostname 'host' to DHCP clients\n"
4002 "-net tap[,vlan=n][,name=str],ifname=name\n"
4003 " connect the host TAP network interface to VLAN 'n'\n"
4005 "-net tap[,vlan=n][,name=str][,fd=h][,ifname=name][,script=file][,downscript=dfile]\n"
4006 " connect the host TAP network interface to VLAN 'n' and use the\n"
4007 " network scripts 'file' (default=%s)\n"
4008 " and 'dfile' (default=%s);\n"
4009 " use '[down]script=no' to disable script execution;\n"
4010 " use 'fd=h' to connect to an already opened TAP interface\n"
4012 "-net socket[,vlan=n][,name=str][,fd=h][,listen=[host]:port][,connect=host:port]\n"
4013 " connect the vlan 'n' to another VLAN using a socket connection\n"
4014 "-net socket[,vlan=n][,name=str][,fd=h][,mcast=maddr:port]\n"
4015 " connect the vlan 'n' to multicast maddr and port\n"
4017 "-net vde[,vlan=n][,name=str][,sock=socketpath][,port=n][,group=groupname][,mode=octalmode]\n"
4018 " connect the vlan 'n' to port 'n' of a vde switch running\n"
4019 " on host and listening for incoming connections on 'socketpath'.\n"
4020 " Use group 'groupname' and mode 'octalmode' to change default\n"
4021 " ownership and permissions for communication port.\n"
4023 "-net none use it alone to have zero network devices; if no -net option\n"
4024 " is provided, the default is '-net nic -net user'\n"
4026 "-tftp dir allow tftp access to files in dir [-net user]\n"
4027 "-bootp file advertise file in BOOTP replies\n"
4029 "-smb dir allow SMB access to files in 'dir' [-net user]\n"
4031 "-redir [tcp|udp]:host-port:[guest-host]:guest-port\n"
4032 " redirect TCP or UDP connections from host to guest [-net user]\n"
4035 "-bt hci,null dumb bluetooth HCI - doesn't respond to commands\n"
4036 "-bt hci,host[:id]\n"
4037 " use host's HCI with the given name\n"
4038 "-bt hci[,vlan=n]\n"
4039 " emulate a standard HCI in virtual scatternet 'n'\n"
4040 "-bt vhci[,vlan=n]\n"
4041 " add host computer to virtual scatternet 'n' using VHCI\n"
4042 "-bt device:dev[,vlan=n]\n"
4043 " emulate a bluetooth device 'dev' in scatternet 'n'\n"
4047 "i386 target only:\n"
4048 "-win2k-hack use it when installing Windows 2000 to avoid a disk full bug\n"
4049 "-rtc-td-hack use it to fix time drift in Windows ACPI HAL\n"
4050 "-no-fd-bootchk disable boot signature checking for floppy disks\n"
4051 "-no-acpi disable ACPI\n"
4052 "-no-hpet disable HPET\n"
4053 "-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"
4054 " ACPI table description\n"
4056 "Linux boot specific:\n"
4057 "-kernel bzImage use 'bzImage' as kernel image\n"
4058 "-append cmdline use 'cmdline' as kernel command line\n"
4059 "-initrd file use 'file' as initial ram disk\n"
4061 "Debug/Expert options:\n"
4062 "-serial dev redirect the serial port to char device 'dev'\n"
4063 "-parallel dev redirect the parallel port to char device 'dev'\n"
4064 "-monitor dev redirect the monitor to char device 'dev'\n"
4065 "-pidfile file write PID to 'file'\n"
4066 "-S freeze CPU at startup (use 'c' to start execution)\n"
4067 "-s wait gdb connection to port\n"
4068 "-p port set gdb connection port [default=%s]\n"
4069 "-d item1,... output log to %s (use -d ? for a list of log items)\n"
4070 "-hdachs c,h,s[,t]\n"
4071 " force hard disk 0 physical geometry and the optional BIOS\n"
4072 " translation (t=none or lba) (usually qemu can guess them)\n"
4073 "-L path set the directory for the BIOS, VGA BIOS and keymaps\n"
4074 "-bios file set the filename for the BIOS\n"
4076 "-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n"
4077 "-no-kqemu disable KQEMU kernel module usage\n"
4080 "-enable-kvm enable KVM full virtualization support\n"
4082 "-no-reboot exit instead of rebooting\n"
4083 "-no-shutdown stop before shutdown\n"
4084 "-loadvm [tag|id]\n"
4085 " start right away with a saved state (loadvm in monitor)\n"
4087 "-daemonize daemonize QEMU after initializing\n"
4089 "-option-rom rom load a file, rom, into the option ROM space\n"
4090 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4091 "-prom-env variable=value\n"
4092 " set OpenBIOS nvram variables\n"
4094 "-clock force the use of the given methods for timer alarm.\n"
4095 " To see what timers are available use -clock ?\n"
4096 "-localtime set the real time clock to local time [default=utc]\n"
4097 "-startdate select initial date of the clock\n"
4098 "-icount [N|auto]\n"
4099 " enable virtual instruction counter with 2^N clock ticks per instruction\n"
4100 "-echr chr set terminal escape character instead of ctrl-a\n"
4101 "-virtioconsole c\n"
4102 " set virtio console\n"
4103 "-show-cursor show cursor\n"
4104 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4105 "-semihosting semihosting mode\n"
4107 #if defined(TARGET_ARM)
4108 "-old-param old param mode\n"
4110 "-tb-size n set TB size\n"
4111 "-incoming p prepare for incoming migration, listen on port p\n"
4113 "-chroot dir Chroot to dir just before starting the VM.\n"
4114 "-runas user Change to user id user just before starting the VM.\n"
4117 "During emulation, the following keys are useful:\n"
4118 "ctrl-alt-f toggle full screen\n"
4119 "ctrl-alt-n switch to virtual console 'n'\n"
4120 "ctrl-alt toggle mouse and keyboard grab\n"
4122 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4127 DEFAULT_NETWORK_SCRIPT
,
4128 DEFAULT_NETWORK_DOWN_SCRIPT
,
4130 DEFAULT_GDBSTUB_PORT
,
4135 #define HAS_ARG 0x0001
4138 /* Please keep in synch with help, qemu_options[] and
4140 /* Standard options: */
4153 QEMU_OPTION_mtdblock
,
4157 QEMU_OPTION_snapshot
,
4160 QEMU_OPTION_audio_help
,
4161 QEMU_OPTION_soundhw
,
4163 QEMU_OPTION_usbdevice
,
4167 /* Display options: */
4168 QEMU_OPTION_nographic
,
4170 QEMU_OPTION_no_frame
,
4171 QEMU_OPTION_alt_grab
,
4172 QEMU_OPTION_no_quit
,
4174 QEMU_OPTION_portrait
,
4176 QEMU_OPTION_full_screen
,
4180 /* Network options: */
4188 /* i386 target only: */
4189 QEMU_OPTION_win2k_hack
,
4190 QEMU_OPTION_rtc_td_hack
,
4191 QEMU_OPTION_no_fd_bootchk
,
4192 QEMU_OPTION_no_acpi
,
4193 QEMU_OPTION_no_hpet
,
4194 QEMU_OPTION_acpitable
,
4196 /* Linux boot specific: */
4201 /* Debug/Expert options: */
4203 QEMU_OPTION_parallel
,
4204 QEMU_OPTION_monitor
,
4205 QEMU_OPTION_pidfile
,
4213 QEMU_OPTION_kernel_kqemu
,
4214 QEMU_OPTION_no_kqemu
,
4215 QEMU_OPTION_enable_kvm
,
4216 QEMU_OPTION_no_reboot
,
4217 QEMU_OPTION_no_shutdown
,
4219 QEMU_OPTION_daemonize
,
4220 QEMU_OPTION_option_rom
,
4221 QEMU_OPTION_prom_env
,
4223 QEMU_OPTION_localtime
,
4224 QEMU_OPTION_startdate
,
4227 QEMU_OPTION_virtiocon
,
4228 QEMU_OPTION_show_cursor
,
4229 QEMU_OPTION_semihosting
,
4230 QEMU_OPTION_old_param
,
4231 QEMU_OPTION_tb_size
,
4232 QEMU_OPTION_incoming
,
4237 typedef struct QEMUOption
{
4243 static const QEMUOption qemu_options
[] = {
4244 /* Please keep in synch with help, QEMU_OPTION_ enums, and
4246 /* Standard options: */
4247 { "h", 0, QEMU_OPTION_h
},
4248 { "help", 0, QEMU_OPTION_h
},
4249 { "M", HAS_ARG
, QEMU_OPTION_M
},
4250 { "cpu", HAS_ARG
, QEMU_OPTION_cpu
},
4251 { "smp", HAS_ARG
, QEMU_OPTION_smp
},
4252 { "fda", HAS_ARG
, QEMU_OPTION_fda
},
4253 { "fdb", HAS_ARG
, QEMU_OPTION_fdb
},
4254 { "hda", HAS_ARG
, QEMU_OPTION_hda
},
4255 { "hdb", HAS_ARG
, QEMU_OPTION_hdb
},
4256 { "hdc", HAS_ARG
, QEMU_OPTION_hdc
},
4257 { "hdd", HAS_ARG
, QEMU_OPTION_hdd
},
4258 { "cdrom", HAS_ARG
, QEMU_OPTION_cdrom
},
4259 { "drive", HAS_ARG
, QEMU_OPTION_drive
},
4260 { "mtdblock", HAS_ARG
, QEMU_OPTION_mtdblock
},
4261 { "sd", HAS_ARG
, QEMU_OPTION_sd
},
4262 { "pflash", HAS_ARG
, QEMU_OPTION_pflash
},
4263 { "boot", HAS_ARG
, QEMU_OPTION_boot
},
4264 { "snapshot", 0, QEMU_OPTION_snapshot
},
4265 { "m", HAS_ARG
, QEMU_OPTION_m
},
4267 { "k", HAS_ARG
, QEMU_OPTION_k
},
4270 { "audio-help", 0, QEMU_OPTION_audio_help
},
4271 { "soundhw", HAS_ARG
, QEMU_OPTION_soundhw
},
4273 { "usb", 0, QEMU_OPTION_usb
},
4274 { "usbdevice", HAS_ARG
, QEMU_OPTION_usbdevice
},
4275 { "name", HAS_ARG
, QEMU_OPTION_name
},
4276 { "uuid", HAS_ARG
, QEMU_OPTION_uuid
},
4278 /* Display options: */
4279 { "nographic", 0, QEMU_OPTION_nographic
},
4280 #ifdef CONFIG_CURSES
4281 { "curses", 0, QEMU_OPTION_curses
},
4284 { "no-frame", 0, QEMU_OPTION_no_frame
},
4285 { "alt-grab", 0, QEMU_OPTION_alt_grab
},
4286 { "no-quit", 0, QEMU_OPTION_no_quit
},
4287 { "sdl", 0, QEMU_OPTION_sdl
},
4289 { "portrait", 0, QEMU_OPTION_portrait
},
4290 { "vga", HAS_ARG
, QEMU_OPTION_vga
},
4291 { "full-screen", 0, QEMU_OPTION_full_screen
},
4292 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4293 { "g", 1, QEMU_OPTION_g
},
4295 { "vnc", HAS_ARG
, QEMU_OPTION_vnc
},
4297 /* Network options: */
4298 { "net", HAS_ARG
, QEMU_OPTION_net
},
4300 { "tftp", HAS_ARG
, QEMU_OPTION_tftp
},
4301 { "bootp", HAS_ARG
, QEMU_OPTION_bootp
},
4303 { "smb", HAS_ARG
, QEMU_OPTION_smb
},
4305 { "redir", HAS_ARG
, QEMU_OPTION_redir
},
4307 { "bt", HAS_ARG
, QEMU_OPTION_bt
},
4309 /* i386 target only: */
4310 { "win2k-hack", 0, QEMU_OPTION_win2k_hack
},
4311 { "rtc-td-hack", 0, QEMU_OPTION_rtc_td_hack
},
4312 { "no-fd-bootchk", 0, QEMU_OPTION_no_fd_bootchk
},
4313 { "no-acpi", 0, QEMU_OPTION_no_acpi
},
4314 { "no-hpet", 0, QEMU_OPTION_no_hpet
},
4315 { "acpitable", HAS_ARG
, QEMU_OPTION_acpitable
},
4318 /* Linux boot specific: */
4319 { "kernel", HAS_ARG
, QEMU_OPTION_kernel
},
4320 { "append", HAS_ARG
, QEMU_OPTION_append
},
4321 { "initrd", HAS_ARG
, QEMU_OPTION_initrd
},
4323 /* Debug/Expert options: */
4324 { "serial", HAS_ARG
, QEMU_OPTION_serial
},
4325 { "parallel", HAS_ARG
, QEMU_OPTION_parallel
},
4326 { "monitor", HAS_ARG
, QEMU_OPTION_monitor
},
4327 { "pidfile", HAS_ARG
, QEMU_OPTION_pidfile
},
4328 { "S", 0, QEMU_OPTION_S
},
4329 { "s", 0, QEMU_OPTION_s
},
4330 { "p", HAS_ARG
, QEMU_OPTION_p
},
4331 { "d", HAS_ARG
, QEMU_OPTION_d
},
4332 { "hdachs", HAS_ARG
, QEMU_OPTION_hdachs
},
4333 { "L", HAS_ARG
, QEMU_OPTION_L
},
4334 { "bios", HAS_ARG
, QEMU_OPTION_bios
},
4336 { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu
},
4337 { "no-kqemu", 0, QEMU_OPTION_no_kqemu
},
4340 { "enable-kvm", 0, QEMU_OPTION_enable_kvm
},
4342 { "no-reboot", 0, QEMU_OPTION_no_reboot
},
4343 { "no-shutdown", 0, QEMU_OPTION_no_shutdown
},
4344 { "loadvm", HAS_ARG
, QEMU_OPTION_loadvm
},
4345 { "daemonize", 0, QEMU_OPTION_daemonize
},
4346 { "option-rom", HAS_ARG
, QEMU_OPTION_option_rom
},
4347 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4348 { "prom-env", HAS_ARG
, QEMU_OPTION_prom_env
},
4350 { "clock", HAS_ARG
, QEMU_OPTION_clock
},
4351 { "localtime", 0, QEMU_OPTION_localtime
},
4352 { "startdate", HAS_ARG
, QEMU_OPTION_startdate
},
4353 { "icount", HAS_ARG
, QEMU_OPTION_icount
},
4354 { "echr", HAS_ARG
, QEMU_OPTION_echr
},
4355 { "virtioconsole", HAS_ARG
, QEMU_OPTION_virtiocon
},
4356 { "show-cursor", 0, QEMU_OPTION_show_cursor
},
4357 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4358 { "semihosting", 0, QEMU_OPTION_semihosting
},
4360 #if defined(TARGET_ARM)
4361 { "old-param", 0, QEMU_OPTION_old_param
},
4363 { "tb-size", HAS_ARG
, QEMU_OPTION_tb_size
},
4364 { "incoming", HAS_ARG
, QEMU_OPTION_incoming
},
4365 { "chroot", HAS_ARG
, QEMU_OPTION_chroot
},
4366 { "runas", HAS_ARG
, QEMU_OPTION_runas
},
4371 struct soundhw soundhw
[] = {
4372 #ifdef HAS_AUDIO_CHOICE
4373 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4379 { .init_isa
= pcspk_audio_init
}
4386 "Creative Sound Blaster 16",
4389 { .init_isa
= SB16_init
}
4393 #ifdef CONFIG_CS4231A
4399 { .init_isa
= cs4231a_init
}
4407 "Yamaha YMF262 (OPL3)",
4409 "Yamaha YM3812 (OPL2)",
4413 { .init_isa
= Adlib_init
}
4420 "Gravis Ultrasound GF1",
4423 { .init_isa
= GUS_init
}
4430 "Intel 82801AA AC97 Audio",
4433 { .init_pci
= ac97_init
}
4437 #ifdef CONFIG_ES1370
4440 "ENSONIQ AudioPCI ES1370",
4443 { .init_pci
= es1370_init
}
4447 #endif /* HAS_AUDIO_CHOICE */
4449 { NULL
, NULL
, 0, 0, { NULL
} }
4452 static void select_soundhw (const char *optarg
)
4456 if (*optarg
== '?') {
4459 printf ("Valid sound card names (comma separated):\n");
4460 for (c
= soundhw
; c
->name
; ++c
) {
4461 printf ("%-11s %s\n", c
->name
, c
->descr
);
4463 printf ("\n-soundhw all will enable all of the above\n");
4464 exit (*optarg
!= '?');
4472 if (!strcmp (optarg
, "all")) {
4473 for (c
= soundhw
; c
->name
; ++c
) {
4481 e
= strchr (p
, ',');
4482 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4484 for (c
= soundhw
; c
->name
; ++c
) {
4485 if (!strncmp (c
->name
, p
, l
)) {
4494 "Unknown sound card name (too big to show)\n");
4497 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4502 p
+= l
+ (e
!= NULL
);
4506 goto show_valid_cards
;
4511 static void select_vgahw (const char *p
)
4515 if (strstart(p
, "std", &opts
)) {
4516 std_vga_enabled
= 1;
4517 cirrus_vga_enabled
= 0;
4519 } else if (strstart(p
, "cirrus", &opts
)) {
4520 cirrus_vga_enabled
= 1;
4521 std_vga_enabled
= 0;
4523 } else if (strstart(p
, "vmware", &opts
)) {
4524 cirrus_vga_enabled
= 0;
4525 std_vga_enabled
= 0;
4527 } else if (strstart(p
, "none", &opts
)) {
4528 cirrus_vga_enabled
= 0;
4529 std_vga_enabled
= 0;
4533 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4537 const char *nextopt
;
4539 if (strstart(opts
, ",retrace=", &nextopt
)) {
4541 if (strstart(opts
, "dumb", &nextopt
))
4542 vga_retrace_method
= VGA_RETRACE_DUMB
;
4543 else if (strstart(opts
, "precise", &nextopt
))
4544 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4545 else goto invalid_vga
;
4546 } else goto invalid_vga
;
4552 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4554 exit(STATUS_CONTROL_C_EXIT
);
4559 static int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4563 if(strlen(str
) != 36)
4566 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4567 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4568 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4576 #define MAX_NET_CLIENTS 32
4580 static void termsig_handler(int signal
)
4582 qemu_system_shutdown_request();
4585 static void termsig_setup(void)
4587 struct sigaction act
;
4589 memset(&act
, 0, sizeof(act
));
4590 act
.sa_handler
= termsig_handler
;
4591 sigaction(SIGINT
, &act
, NULL
);
4592 sigaction(SIGHUP
, &act
, NULL
);
4593 sigaction(SIGTERM
, &act
, NULL
);
4598 int main(int argc
, char **argv
, char **envp
)
4600 #ifdef CONFIG_GDBSTUB
4602 const char *gdbstub_port
;
4604 uint32_t boot_devices_bitmap
= 0;
4606 int snapshot
, linux_boot
, net_boot
;
4607 const char *initrd_filename
;
4608 const char *kernel_filename
, *kernel_cmdline
;
4609 const char *boot_devices
= "";
4611 DisplayChangeListener
*dcl
;
4612 int cyls
, heads
, secs
, translation
;
4613 const char *net_clients
[MAX_NET_CLIENTS
];
4615 const char *bt_opts
[MAX_BT_CMDLINE
];
4619 const char *r
, *optarg
;
4620 CharDriverState
*monitor_hd
= NULL
;
4621 const char *monitor_device
;
4622 const char *serial_devices
[MAX_SERIAL_PORTS
];
4623 int serial_device_index
;
4624 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4625 int parallel_device_index
;
4626 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4627 int virtio_console_index
;
4628 const char *loadvm
= NULL
;
4629 QEMUMachine
*machine
;
4630 const char *cpu_model
;
4631 const char *usb_devices
[MAX_USB_CMDLINE
];
4632 int usb_devices_index
;
4635 const char *pid_file
= NULL
;
4636 const char *incoming
= NULL
;
4638 struct passwd
*pwd
= NULL
;
4639 const char *chroot_dir
= NULL
;
4640 const char *run_as
= NULL
;
4642 qemu_cache_utils_init(envp
);
4644 LIST_INIT (&vm_change_state_head
);
4647 struct sigaction act
;
4648 sigfillset(&act
.sa_mask
);
4650 act
.sa_handler
= SIG_IGN
;
4651 sigaction(SIGPIPE
, &act
, NULL
);
4654 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4655 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4656 QEMU to run on a single CPU */
4661 h
= GetCurrentProcess();
4662 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4663 for(i
= 0; i
< 32; i
++) {
4664 if (mask
& (1 << i
))
4669 SetProcessAffinityMask(h
, mask
);
4675 register_machines();
4676 machine
= first_machine
;
4678 initrd_filename
= NULL
;
4680 vga_ram_size
= VGA_RAM_SIZE
;
4681 #ifdef CONFIG_GDBSTUB
4683 gdbstub_port
= DEFAULT_GDBSTUB_PORT
;
4688 kernel_filename
= NULL
;
4689 kernel_cmdline
= "";
4690 cyls
= heads
= secs
= 0;
4691 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4692 monitor_device
= "vc:80Cx24C";
4694 serial_devices
[0] = "vc:80Cx24C";
4695 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4696 serial_devices
[i
] = NULL
;
4697 serial_device_index
= 0;
4699 parallel_devices
[0] = "vc:80Cx24C";
4700 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4701 parallel_devices
[i
] = NULL
;
4702 parallel_device_index
= 0;
4704 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4705 virtio_consoles
[i
] = NULL
;
4706 virtio_console_index
= 0;
4708 usb_devices_index
= 0;
4727 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4729 const QEMUOption
*popt
;
4732 /* Treat --foo the same as -foo. */
4735 popt
= qemu_options
;
4738 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4742 if (!strcmp(popt
->name
, r
+ 1))
4746 if (popt
->flags
& HAS_ARG
) {
4747 if (optind
>= argc
) {
4748 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4752 optarg
= argv
[optind
++];
4757 switch(popt
->index
) {
4759 machine
= find_machine(optarg
);
4762 printf("Supported machines are:\n");
4763 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4764 printf("%-10s %s%s\n",
4766 m
== first_machine
? " (default)" : "");
4768 exit(*optarg
!= '?');
4771 case QEMU_OPTION_cpu
:
4772 /* hw initialization will check this */
4773 if (*optarg
== '?') {
4774 /* XXX: implement xxx_cpu_list for targets that still miss it */
4775 #if defined(cpu_list)
4776 cpu_list(stdout
, &fprintf
);
4783 case QEMU_OPTION_initrd
:
4784 initrd_filename
= optarg
;
4786 case QEMU_OPTION_hda
:
4788 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4790 hda_index
= drive_add(optarg
, HD_ALIAS
4791 ",cyls=%d,heads=%d,secs=%d%s",
4792 0, cyls
, heads
, secs
,
4793 translation
== BIOS_ATA_TRANSLATION_LBA
?
4795 translation
== BIOS_ATA_TRANSLATION_NONE
?
4796 ",trans=none" : "");
4798 case QEMU_OPTION_hdb
:
4799 case QEMU_OPTION_hdc
:
4800 case QEMU_OPTION_hdd
:
4801 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4803 case QEMU_OPTION_drive
:
4804 drive_add(NULL
, "%s", optarg
);
4806 case QEMU_OPTION_mtdblock
:
4807 drive_add(optarg
, MTD_ALIAS
);
4809 case QEMU_OPTION_sd
:
4810 drive_add(optarg
, SD_ALIAS
);
4812 case QEMU_OPTION_pflash
:
4813 drive_add(optarg
, PFLASH_ALIAS
);
4815 case QEMU_OPTION_snapshot
:
4818 case QEMU_OPTION_hdachs
:
4822 cyls
= strtol(p
, (char **)&p
, 0);
4823 if (cyls
< 1 || cyls
> 16383)
4828 heads
= strtol(p
, (char **)&p
, 0);
4829 if (heads
< 1 || heads
> 16)
4834 secs
= strtol(p
, (char **)&p
, 0);
4835 if (secs
< 1 || secs
> 63)
4839 if (!strcmp(p
, "none"))
4840 translation
= BIOS_ATA_TRANSLATION_NONE
;
4841 else if (!strcmp(p
, "lba"))
4842 translation
= BIOS_ATA_TRANSLATION_LBA
;
4843 else if (!strcmp(p
, "auto"))
4844 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4847 } else if (*p
!= '\0') {
4849 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4852 if (hda_index
!= -1)
4853 snprintf(drives_opt
[hda_index
].opt
,
4854 sizeof(drives_opt
[hda_index
].opt
),
4855 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4856 0, cyls
, heads
, secs
,
4857 translation
== BIOS_ATA_TRANSLATION_LBA
?
4859 translation
== BIOS_ATA_TRANSLATION_NONE
?
4860 ",trans=none" : "");
4863 case QEMU_OPTION_nographic
:
4866 #ifdef CONFIG_CURSES
4867 case QEMU_OPTION_curses
:
4871 case QEMU_OPTION_portrait
:
4874 case QEMU_OPTION_kernel
:
4875 kernel_filename
= optarg
;
4877 case QEMU_OPTION_append
:
4878 kernel_cmdline
= optarg
;
4880 case QEMU_OPTION_cdrom
:
4881 drive_add(optarg
, CDROM_ALIAS
);
4883 case QEMU_OPTION_boot
:
4884 boot_devices
= optarg
;
4885 /* We just do some generic consistency checks */
4887 /* Could easily be extended to 64 devices if needed */
4890 boot_devices_bitmap
= 0;
4891 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4892 /* Allowed boot devices are:
4893 * a b : floppy disk drives
4894 * c ... f : IDE disk drives
4895 * g ... m : machine implementation dependant drives
4896 * n ... p : network devices
4897 * It's up to each machine implementation to check
4898 * if the given boot devices match the actual hardware
4899 * implementation and firmware features.
4901 if (*p
< 'a' || *p
> 'q') {
4902 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4905 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4907 "Boot device '%c' was given twice\n",*p
);
4910 boot_devices_bitmap
|= 1 << (*p
- 'a');
4914 case QEMU_OPTION_fda
:
4915 case QEMU_OPTION_fdb
:
4916 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4919 case QEMU_OPTION_no_fd_bootchk
:
4923 case QEMU_OPTION_net
:
4924 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4925 fprintf(stderr
, "qemu: too many network clients\n");
4928 net_clients
[nb_net_clients
] = optarg
;
4932 case QEMU_OPTION_tftp
:
4933 tftp_prefix
= optarg
;
4935 case QEMU_OPTION_bootp
:
4936 bootp_filename
= optarg
;
4939 case QEMU_OPTION_smb
:
4940 net_slirp_smb(optarg
);
4943 case QEMU_OPTION_redir
:
4944 net_slirp_redir(optarg
);
4947 case QEMU_OPTION_bt
:
4948 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4949 fprintf(stderr
, "qemu: too many bluetooth options\n");
4952 bt_opts
[nb_bt_opts
++] = optarg
;
4955 case QEMU_OPTION_audio_help
:
4959 case QEMU_OPTION_soundhw
:
4960 select_soundhw (optarg
);
4966 case QEMU_OPTION_m
: {
4970 value
= strtoul(optarg
, &ptr
, 10);
4972 case 0: case 'M': case 'm':
4979 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4983 /* On 32-bit hosts, QEMU is limited by virtual address space */
4984 if (value
> (2047 << 20)
4986 && HOST_LONG_BITS
== 32
4989 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4992 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4993 fprintf(stderr
, "qemu: ram size too large\n");
5002 const CPULogItem
*item
;
5004 mask
= cpu_str_to_log_mask(optarg
);
5006 printf("Log items (comma separated):\n");
5007 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5008 printf("%-10s %s\n", item
->name
, item
->help
);
5015 #ifdef CONFIG_GDBSTUB
5020 gdbstub_port
= optarg
;
5026 case QEMU_OPTION_bios
:
5033 keyboard_layout
= optarg
;
5035 case QEMU_OPTION_localtime
:
5038 case QEMU_OPTION_vga
:
5039 select_vgahw (optarg
);
5046 w
= strtol(p
, (char **)&p
, 10);
5049 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5055 h
= strtol(p
, (char **)&p
, 10);
5060 depth
= strtol(p
, (char **)&p
, 10);
5061 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5062 depth
!= 24 && depth
!= 32)
5064 } else if (*p
== '\0') {
5065 depth
= graphic_depth
;
5072 graphic_depth
= depth
;
5075 case QEMU_OPTION_echr
:
5078 term_escape_char
= strtol(optarg
, &r
, 0);
5080 printf("Bad argument to echr\n");
5083 case QEMU_OPTION_monitor
:
5084 monitor_device
= optarg
;
5086 case QEMU_OPTION_serial
:
5087 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5088 fprintf(stderr
, "qemu: too many serial ports\n");
5091 serial_devices
[serial_device_index
] = optarg
;
5092 serial_device_index
++;
5094 case QEMU_OPTION_virtiocon
:
5095 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5096 fprintf(stderr
, "qemu: too many virtio consoles\n");
5099 virtio_consoles
[virtio_console_index
] = optarg
;
5100 virtio_console_index
++;
5102 case QEMU_OPTION_parallel
:
5103 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5104 fprintf(stderr
, "qemu: too many parallel ports\n");
5107 parallel_devices
[parallel_device_index
] = optarg
;
5108 parallel_device_index
++;
5110 case QEMU_OPTION_loadvm
:
5113 case QEMU_OPTION_full_screen
:
5117 case QEMU_OPTION_no_frame
:
5120 case QEMU_OPTION_alt_grab
:
5123 case QEMU_OPTION_no_quit
:
5126 case QEMU_OPTION_sdl
:
5130 case QEMU_OPTION_pidfile
:
5134 case QEMU_OPTION_win2k_hack
:
5135 win2k_install_hack
= 1;
5137 case QEMU_OPTION_rtc_td_hack
:
5140 case QEMU_OPTION_acpitable
:
5141 if(acpi_table_add(optarg
) < 0) {
5142 fprintf(stderr
, "Wrong acpi table provided\n");
5148 case QEMU_OPTION_no_kqemu
:
5151 case QEMU_OPTION_kernel_kqemu
:
5156 case QEMU_OPTION_enable_kvm
:
5163 case QEMU_OPTION_usb
:
5166 case QEMU_OPTION_usbdevice
:
5168 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5169 fprintf(stderr
, "Too many USB devices\n");
5172 usb_devices
[usb_devices_index
] = optarg
;
5173 usb_devices_index
++;
5175 case QEMU_OPTION_smp
:
5176 smp_cpus
= atoi(optarg
);
5178 fprintf(stderr
, "Invalid number of CPUs\n");
5182 case QEMU_OPTION_vnc
:
5183 vnc_display
= optarg
;
5185 case QEMU_OPTION_no_acpi
:
5188 case QEMU_OPTION_no_hpet
:
5191 case QEMU_OPTION_no_reboot
:
5194 case QEMU_OPTION_no_shutdown
:
5197 case QEMU_OPTION_show_cursor
:
5200 case QEMU_OPTION_uuid
:
5201 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5202 fprintf(stderr
, "Fail to parse UUID string."
5203 " Wrong format.\n");
5207 case QEMU_OPTION_daemonize
:
5210 case QEMU_OPTION_option_rom
:
5211 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5212 fprintf(stderr
, "Too many option ROMs\n");
5215 option_rom
[nb_option_roms
] = optarg
;
5218 case QEMU_OPTION_semihosting
:
5219 semihosting_enabled
= 1;
5221 case QEMU_OPTION_name
:
5224 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5225 case QEMU_OPTION_prom_env
:
5226 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5227 fprintf(stderr
, "Too many prom variables\n");
5230 prom_envs
[nb_prom_envs
] = optarg
;
5235 case QEMU_OPTION_old_param
:
5239 case QEMU_OPTION_clock
:
5240 configure_alarms(optarg
);
5242 case QEMU_OPTION_startdate
:
5245 time_t rtc_start_date
;
5246 if (!strcmp(optarg
, "now")) {
5247 rtc_date_offset
= -1;
5249 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5257 } else if (sscanf(optarg
, "%d-%d-%d",
5260 &tm
.tm_mday
) == 3) {
5269 rtc_start_date
= mktimegm(&tm
);
5270 if (rtc_start_date
== -1) {
5272 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5273 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5276 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5280 case QEMU_OPTION_tb_size
:
5281 tb_size
= strtol(optarg
, NULL
, 0);
5285 case QEMU_OPTION_icount
:
5287 if (strcmp(optarg
, "auto") == 0) {
5288 icount_time_shift
= -1;
5290 icount_time_shift
= strtol(optarg
, NULL
, 0);
5293 case QEMU_OPTION_incoming
:
5296 case QEMU_OPTION_chroot
:
5297 chroot_dir
= optarg
;
5299 case QEMU_OPTION_runas
:
5306 #if defined(CONFIG_KVM) && defined(USE_KQEMU)
5307 if (kvm_allowed
&& kqemu_allowed
) {
5309 "You can not enable both KVM and kqemu at the same time\n");
5314 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5315 if (smp_cpus
> machine
->max_cpus
) {
5316 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5317 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5323 if (serial_device_index
== 0)
5324 serial_devices
[0] = "stdio";
5325 if (parallel_device_index
== 0)
5326 parallel_devices
[0] = "null";
5327 if (strncmp(monitor_device
, "vc", 2) == 0)
5328 monitor_device
= "stdio";
5335 if (pipe(fds
) == -1)
5346 len
= read(fds
[0], &status
, 1);
5347 if (len
== -1 && (errno
== EINTR
))
5352 else if (status
== 1) {
5353 fprintf(stderr
, "Could not acquire pidfile\n");
5370 signal(SIGTSTP
, SIG_IGN
);
5371 signal(SIGTTOU
, SIG_IGN
);
5372 signal(SIGTTIN
, SIG_IGN
);
5376 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5379 write(fds
[1], &status
, 1);
5381 fprintf(stderr
, "Could not acquire pid file\n");
5389 linux_boot
= (kernel_filename
!= NULL
);
5390 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5392 if (!linux_boot
&& net_boot
== 0 &&
5393 !machine
->nodisk_ok
&& nb_drives_opt
== 0)
5396 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5397 fprintf(stderr
, "-append only allowed with -kernel option\n");
5401 if (!linux_boot
&& initrd_filename
!= NULL
) {
5402 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5406 /* boot to floppy or the default cd if no hard disk defined yet */
5407 if (!boot_devices
[0]) {
5408 boot_devices
= "cad";
5410 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5413 if (init_timer_alarm() < 0) {
5414 fprintf(stderr
, "could not initialize alarm timer\n");
5417 if (use_icount
&& icount_time_shift
< 0) {
5419 /* 125MIPS seems a reasonable initial guess at the guest speed.
5420 It will be corrected fairly quickly anyway. */
5421 icount_time_shift
= 3;
5422 init_icount_adjust();
5429 /* init network clients */
5430 if (nb_net_clients
== 0) {
5431 /* if no clients, we use a default config */
5432 net_clients
[nb_net_clients
++] = "nic";
5434 net_clients
[nb_net_clients
++] = "user";
5438 for(i
= 0;i
< nb_net_clients
; i
++) {
5439 if (net_client_parse(net_clients
[i
]) < 0)
5445 /* XXX: this should be moved in the PC machine instantiation code */
5446 if (net_boot
!= 0) {
5448 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5449 const char *model
= nd_table
[i
].model
;
5451 if (net_boot
& (1 << i
)) {
5454 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5455 if (get_image_size(buf
) > 0) {
5456 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5457 fprintf(stderr
, "Too many option ROMs\n");
5460 option_rom
[nb_option_roms
] = strdup(buf
);
5467 fprintf(stderr
, "No valid PXE rom found for network device\n");
5473 /* init the bluetooth world */
5474 for (i
= 0; i
< nb_bt_opts
; i
++)
5475 if (bt_parse(bt_opts
[i
]))
5478 /* init the memory */
5479 phys_ram_size
= machine
->ram_require
& ~RAMSIZE_FIXED
;
5481 if (machine
->ram_require
& RAMSIZE_FIXED
) {
5483 if (ram_size
< phys_ram_size
) {
5484 fprintf(stderr
, "Machine `%s' requires %llu bytes of memory\n",
5485 machine
->name
, (unsigned long long) phys_ram_size
);
5489 phys_ram_size
= ram_size
;
5491 ram_size
= phys_ram_size
;
5494 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5496 phys_ram_size
+= ram_size
;
5499 phys_ram_base
= qemu_vmalloc(phys_ram_size
);
5500 if (!phys_ram_base
) {
5501 fprintf(stderr
, "Could not allocate physical memory\n");
5505 /* init the dynamic translator */
5506 cpu_exec_init_all(tb_size
* 1024 * 1024);
5510 /* we always create the cdrom drive, even if no disk is there */
5512 if (nb_drives_opt
< MAX_DRIVES
)
5513 drive_add(NULL
, CDROM_ALIAS
);
5515 /* we always create at least one floppy */
5517 if (nb_drives_opt
< MAX_DRIVES
)
5518 drive_add(NULL
, FD_ALIAS
, 0);
5520 /* we always create one sd slot, even if no card is in it */
5522 if (nb_drives_opt
< MAX_DRIVES
)
5523 drive_add(NULL
, SD_ALIAS
);
5525 /* open the virtual block devices */
5527 for(i
= 0; i
< nb_drives_opt
; i
++)
5528 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5531 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5532 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5535 /* must be after terminal init, SDL library changes signal handlers */
5539 /* Maintain compatibility with multiple stdio monitors */
5540 if (!strcmp(monitor_device
,"stdio")) {
5541 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5542 const char *devname
= serial_devices
[i
];
5543 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5544 monitor_device
= NULL
;
5546 } else if (devname
&& !strcmp(devname
,"stdio")) {
5547 monitor_device
= NULL
;
5548 serial_devices
[i
] = "mon:stdio";
5554 if (kvm_enabled()) {
5557 ret
= kvm_init(smp_cpus
);
5559 fprintf(stderr
, "failed to initialize KVM\n");
5564 if (monitor_device
) {
5565 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5567 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5572 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5573 const char *devname
= serial_devices
[i
];
5574 if (devname
&& strcmp(devname
, "none")) {
5576 snprintf(label
, sizeof(label
), "serial%d", i
);
5577 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5578 if (!serial_hds
[i
]) {
5579 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5586 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5587 const char *devname
= parallel_devices
[i
];
5588 if (devname
&& strcmp(devname
, "none")) {
5590 snprintf(label
, sizeof(label
), "parallel%d", i
);
5591 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5592 if (!parallel_hds
[i
]) {
5593 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5600 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5601 const char *devname
= virtio_consoles
[i
];
5602 if (devname
&& strcmp(devname
, "none")) {
5604 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5605 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5606 if (!virtcon_hds
[i
]) {
5607 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5614 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5615 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5617 current_machine
= machine
;
5619 /* Set KVM's vcpu state to qemu's initial CPUState. */
5620 if (kvm_enabled()) {
5623 ret
= kvm_sync_vcpus();
5625 fprintf(stderr
, "failed to initialize vcpus\n");
5630 /* init USB devices */
5632 for(i
= 0; i
< usb_devices_index
; i
++) {
5633 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5634 fprintf(stderr
, "Warning: could not add USB device %s\n",
5641 dumb_display_init();
5642 /* just use the first displaystate for the moment */
5647 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5651 #if defined(CONFIG_CURSES)
5653 /* At the moment curses cannot be used with other displays */
5654 curses_display_init(ds
, full_screen
);
5658 if (vnc_display
!= NULL
) {
5659 vnc_display_init(ds
);
5660 if (vnc_display_open(ds
, vnc_display
) < 0)
5663 #if defined(CONFIG_SDL)
5664 if (sdl
|| !vnc_display
)
5665 sdl_display_init(ds
, full_screen
, no_frame
);
5666 #elif defined(CONFIG_COCOA)
5667 if (sdl
|| !vnc_display
)
5668 cocoa_display_init(ds
, full_screen
);
5674 dcl
= ds
->listeners
;
5675 while (dcl
!= NULL
) {
5676 if (dcl
->dpy_refresh
!= NULL
) {
5677 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5678 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5683 if (nographic
|| (vnc_display
&& !sdl
)) {
5684 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5685 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5688 text_consoles_set_display(display_state
);
5689 qemu_chr_initial_reset();
5691 if (monitor_device
&& monitor_hd
)
5692 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5694 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5695 const char *devname
= serial_devices
[i
];
5696 if (devname
&& strcmp(devname
, "none")) {
5698 snprintf(label
, sizeof(label
), "serial%d", i
);
5699 if (strstart(devname
, "vc", 0))
5700 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5704 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5705 const char *devname
= parallel_devices
[i
];
5706 if (devname
&& strcmp(devname
, "none")) {
5708 snprintf(label
, sizeof(label
), "parallel%d", i
);
5709 if (strstart(devname
, "vc", 0))
5710 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5714 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5715 const char *devname
= virtio_consoles
[i
];
5716 if (virtcon_hds
[i
] && devname
) {
5718 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5719 if (strstart(devname
, "vc", 0))
5720 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5724 #ifdef CONFIG_GDBSTUB
5726 /* XXX: use standard host:port notation and modify options
5728 if (gdbserver_start(gdbstub_port
) < 0) {
5729 fprintf(stderr
, "qemu: could not open gdbstub device on port '%s'\n",
5737 do_loadvm(cur_mon
, loadvm
);
5740 autostart
= 0; /* fixme how to deal with -daemonize */
5741 qemu_start_incoming_migration(incoming
);
5752 len
= write(fds
[1], &status
, 1);
5753 if (len
== -1 && (errno
== EINTR
))
5760 TFR(fd
= open("/dev/null", O_RDWR
));
5767 pwd
= getpwnam(run_as
);
5769 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5775 if (chroot(chroot_dir
) < 0) {
5776 fprintf(stderr
, "chroot failed\n");
5783 if (setgid(pwd
->pw_gid
) < 0) {
5784 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5787 if (setuid(pwd
->pw_uid
) < 0) {
5788 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5791 if (setuid(0) != -1) {
5792 fprintf(stderr
, "Dropping privileges failed\n");