4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
37 #include <sys/times.h>
41 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
46 #if defined(__NetBSD__)
47 #include <net/if_tap.h>
50 #include <linux/if_tun.h>
52 #include <arpa/inet.h>
55 #include <sys/select.h>
58 #if defined(__FreeBSD__) || defined(__DragonFly__)
63 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
64 #include <freebsd/stdlib.h>
69 #include <linux/rtc.h>
71 /* For the benefit of older linux systems which don't supply it,
72 we use a local copy of hpet.h. */
73 /* #include <linux/hpet.h> */
76 #include <linux/ppdev.h>
77 #include <linux/parport.h>
81 #include <sys/ethernet.h>
82 #include <sys/sockio.h>
83 #include <netinet/arp.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/ip_icmp.h> // must come after ip.h
88 #include <netinet/udp.h>
89 #include <netinet/tcp.h>
97 #if defined(__OpenBSD__)
101 #if defined(CONFIG_VDE)
102 #include <libvdeplug.h>
108 #include <sys/timeb.h>
109 #include <mmsystem.h>
110 #define getopt_long_only getopt_long
111 #define memalign(align, size) malloc(size)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/smbios.h"
149 #include "qemu-timer.h"
150 #include "qemu-char.h"
151 #include "cache-utils.h"
154 #include "audio/audio.h"
155 #include "migration.h"
161 #include "exec-all.h"
163 #include "qemu_socket.h"
165 #if defined(CONFIG_SLIRP)
166 #include "libslirp.h"
169 //#define DEBUG_UNUSED_IOPORT
170 //#define DEBUG_IOPORT
172 //#define DEBUG_SLIRP
176 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
178 # define LOG_IOPORT(...) do { } while (0)
181 #define DEFAULT_RAM_SIZE 128
183 /* Max number of USB devices that can be specified on the commandline. */
184 #define MAX_USB_CMDLINE 8
186 /* Max number of bluetooth switches on the commandline. */
187 #define MAX_BT_CMDLINE 10
189 /* XXX: use a two level table to limit memory usage */
190 #define MAX_IOPORTS 65536
192 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
193 const char *bios_name
= NULL
;
194 static void *ioport_opaque
[MAX_IOPORTS
];
195 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
196 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
197 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
198 to store the VM snapshots */
199 DriveInfo drives_table
[MAX_DRIVES
+1];
201 static int vga_ram_size
;
202 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
203 static DisplayState
*display_state
;
207 const char* keyboard_layout
= NULL
;
208 int64_t ticks_per_sec
;
211 NICInfo nd_table
[MAX_NICS
];
213 static int autostart
;
214 static int rtc_utc
= 1;
215 static int rtc_date_offset
= -1; /* -1 means no change */
216 int cirrus_vga_enabled
= 1;
217 int std_vga_enabled
= 0;
218 int vmsvga_enabled
= 0;
220 int graphic_width
= 1024;
221 int graphic_height
= 768;
222 int graphic_depth
= 8;
224 int graphic_width
= 800;
225 int graphic_height
= 600;
226 int graphic_depth
= 15;
228 static int full_screen
= 0;
230 static int no_frame
= 0;
233 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
234 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
235 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
237 int win2k_install_hack
= 0;
243 const char *vnc_display
;
244 int acpi_enabled
= 1;
250 int graphic_rotate
= 0;
254 const char *option_rom
[MAX_OPTION_ROMS
];
256 int semihosting_enabled
= 0;
260 const char *qemu_name
;
262 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
263 unsigned int nb_prom_envs
= 0;
264 const char *prom_envs
[MAX_PROM_ENVS
];
267 struct drive_opt drives_opt
[MAX_DRIVES
];
270 uint64_t node_mem
[MAX_NODES
];
271 uint64_t node_cpumask
[MAX_NODES
];
273 static CPUState
*cur_cpu
;
274 static CPUState
*next_cpu
;
275 static int event_pending
= 1;
276 /* Conversion factor from emulated instructions to virtual clock ticks. */
277 static int icount_time_shift
;
278 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
279 #define MAX_ICOUNT_SHIFT 10
280 /* Compensate for varying guest execution speed. */
281 static int64_t qemu_icount_bias
;
282 static QEMUTimer
*icount_rt_timer
;
283 static QEMUTimer
*icount_vm_timer
;
284 static QEMUTimer
*nographic_timer
;
286 uint8_t qemu_uuid
[16];
288 /***********************************************************/
289 /* x86 ISA bus support */
291 target_phys_addr_t isa_mem_base
= 0;
294 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
295 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
297 static uint32_t ioport_read(int index
, uint32_t address
)
299 static IOPortReadFunc
*default_func
[3] = {
300 default_ioport_readb
,
301 default_ioport_readw
,
304 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
306 func
= default_func
[index
];
307 return func(ioport_opaque
[address
], address
);
310 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
312 static IOPortWriteFunc
*default_func
[3] = {
313 default_ioport_writeb
,
314 default_ioport_writew
,
315 default_ioport_writel
317 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
319 func
= default_func
[index
];
320 func(ioport_opaque
[address
], address
, data
);
323 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
325 #ifdef DEBUG_UNUSED_IOPORT
326 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
331 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
333 #ifdef DEBUG_UNUSED_IOPORT
334 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
338 /* default is to make two byte accesses */
339 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
342 data
= ioport_read(0, address
);
343 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
344 data
|= ioport_read(0, address
) << 8;
348 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
350 ioport_write(0, address
, data
& 0xff);
351 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
352 ioport_write(0, address
, (data
>> 8) & 0xff);
355 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
357 #ifdef DEBUG_UNUSED_IOPORT
358 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
363 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
365 #ifdef DEBUG_UNUSED_IOPORT
366 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
370 /* size is the word size in byte */
371 int register_ioport_read(int start
, int length
, int size
,
372 IOPortReadFunc
*func
, void *opaque
)
378 } else if (size
== 2) {
380 } else if (size
== 4) {
383 hw_error("register_ioport_read: invalid size");
386 for(i
= start
; i
< start
+ length
; i
+= size
) {
387 ioport_read_table
[bsize
][i
] = func
;
388 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
389 hw_error("register_ioport_read: invalid opaque");
390 ioport_opaque
[i
] = opaque
;
395 /* size is the word size in byte */
396 int register_ioport_write(int start
, int length
, int size
,
397 IOPortWriteFunc
*func
, void *opaque
)
403 } else if (size
== 2) {
405 } else if (size
== 4) {
408 hw_error("register_ioport_write: invalid size");
411 for(i
= start
; i
< start
+ length
; i
+= size
) {
412 ioport_write_table
[bsize
][i
] = func
;
413 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
414 hw_error("register_ioport_write: invalid opaque");
415 ioport_opaque
[i
] = opaque
;
420 void isa_unassign_ioport(int start
, int length
)
424 for(i
= start
; i
< start
+ length
; i
++) {
425 ioport_read_table
[0][i
] = default_ioport_readb
;
426 ioport_read_table
[1][i
] = default_ioport_readw
;
427 ioport_read_table
[2][i
] = default_ioport_readl
;
429 ioport_write_table
[0][i
] = default_ioport_writeb
;
430 ioport_write_table
[1][i
] = default_ioport_writew
;
431 ioport_write_table
[2][i
] = default_ioport_writel
;
433 ioport_opaque
[i
] = NULL
;
437 /***********************************************************/
439 void cpu_outb(CPUState
*env
, int addr
, int val
)
441 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
442 ioport_write(0, addr
, val
);
445 env
->last_io_time
= cpu_get_time_fast();
449 void cpu_outw(CPUState
*env
, int addr
, int val
)
451 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
452 ioport_write(1, addr
, val
);
455 env
->last_io_time
= cpu_get_time_fast();
459 void cpu_outl(CPUState
*env
, int addr
, int val
)
461 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
462 ioport_write(2, addr
, val
);
465 env
->last_io_time
= cpu_get_time_fast();
469 int cpu_inb(CPUState
*env
, int addr
)
472 val
= ioport_read(0, addr
);
473 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
476 env
->last_io_time
= cpu_get_time_fast();
481 int cpu_inw(CPUState
*env
, int addr
)
484 val
= ioport_read(1, addr
);
485 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
488 env
->last_io_time
= cpu_get_time_fast();
493 int cpu_inl(CPUState
*env
, int addr
)
496 val
= ioport_read(2, addr
);
497 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
500 env
->last_io_time
= cpu_get_time_fast();
505 /***********************************************************/
506 void hw_error(const char *fmt
, ...)
512 fprintf(stderr
, "qemu: hardware error: ");
513 vfprintf(stderr
, fmt
, ap
);
514 fprintf(stderr
, "\n");
515 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
516 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
518 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
520 cpu_dump_state(env
, stderr
, fprintf
, 0);
530 static QEMUBalloonEvent
*qemu_balloon_event
;
531 void *qemu_balloon_event_opaque
;
533 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
535 qemu_balloon_event
= func
;
536 qemu_balloon_event_opaque
= opaque
;
539 void qemu_balloon(ram_addr_t target
)
541 if (qemu_balloon_event
)
542 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
545 ram_addr_t
qemu_balloon_status(void)
547 if (qemu_balloon_event
)
548 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
552 /***********************************************************/
555 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
556 static void *qemu_put_kbd_event_opaque
;
557 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
558 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
560 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
562 qemu_put_kbd_event_opaque
= opaque
;
563 qemu_put_kbd_event
= func
;
566 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
567 void *opaque
, int absolute
,
570 QEMUPutMouseEntry
*s
, *cursor
;
572 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
574 s
->qemu_put_mouse_event
= func
;
575 s
->qemu_put_mouse_event_opaque
= opaque
;
576 s
->qemu_put_mouse_event_absolute
= absolute
;
577 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
580 if (!qemu_put_mouse_event_head
) {
581 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
585 cursor
= qemu_put_mouse_event_head
;
586 while (cursor
->next
!= NULL
)
587 cursor
= cursor
->next
;
590 qemu_put_mouse_event_current
= s
;
595 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
597 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
599 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
602 cursor
= qemu_put_mouse_event_head
;
603 while (cursor
!= NULL
&& cursor
!= entry
) {
605 cursor
= cursor
->next
;
608 if (cursor
== NULL
) // does not exist or list empty
610 else if (prev
== NULL
) { // entry is head
611 qemu_put_mouse_event_head
= cursor
->next
;
612 if (qemu_put_mouse_event_current
== entry
)
613 qemu_put_mouse_event_current
= cursor
->next
;
614 qemu_free(entry
->qemu_put_mouse_event_name
);
619 prev
->next
= entry
->next
;
621 if (qemu_put_mouse_event_current
== entry
)
622 qemu_put_mouse_event_current
= prev
;
624 qemu_free(entry
->qemu_put_mouse_event_name
);
628 void kbd_put_keycode(int keycode
)
630 if (qemu_put_kbd_event
) {
631 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
635 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
637 QEMUPutMouseEvent
*mouse_event
;
638 void *mouse_event_opaque
;
641 if (!qemu_put_mouse_event_current
) {
646 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
648 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
651 if (graphic_rotate
) {
652 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
655 width
= graphic_width
- 1;
656 mouse_event(mouse_event_opaque
,
657 width
- dy
, dx
, dz
, buttons_state
);
659 mouse_event(mouse_event_opaque
,
660 dx
, dy
, dz
, buttons_state
);
664 int kbd_mouse_is_absolute(void)
666 if (!qemu_put_mouse_event_current
)
669 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
672 void do_info_mice(Monitor
*mon
)
674 QEMUPutMouseEntry
*cursor
;
677 if (!qemu_put_mouse_event_head
) {
678 monitor_printf(mon
, "No mouse devices connected\n");
682 monitor_printf(mon
, "Mouse devices available:\n");
683 cursor
= qemu_put_mouse_event_head
;
684 while (cursor
!= NULL
) {
685 monitor_printf(mon
, "%c Mouse #%d: %s\n",
686 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
687 index
, cursor
->qemu_put_mouse_event_name
);
689 cursor
= cursor
->next
;
693 void do_mouse_set(Monitor
*mon
, int index
)
695 QEMUPutMouseEntry
*cursor
;
698 if (!qemu_put_mouse_event_head
) {
699 monitor_printf(mon
, "No mouse devices connected\n");
703 cursor
= qemu_put_mouse_event_head
;
704 while (cursor
!= NULL
&& index
!= i
) {
706 cursor
= cursor
->next
;
710 qemu_put_mouse_event_current
= cursor
;
712 monitor_printf(mon
, "Mouse at given index not found\n");
715 /* compute with 96 bit intermediate result: (a*b)/c */
716 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
721 #ifdef WORDS_BIGENDIAN
731 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
732 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
735 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
739 /***********************************************************/
740 /* real time host monotonic timer */
742 #define QEMU_TIMER_BASE 1000000000LL
746 static int64_t clock_freq
;
748 static void init_get_clock(void)
752 ret
= QueryPerformanceFrequency(&freq
);
754 fprintf(stderr
, "Could not calibrate ticks\n");
757 clock_freq
= freq
.QuadPart
;
760 static int64_t get_clock(void)
763 QueryPerformanceCounter(&ti
);
764 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
769 static int use_rt_clock
;
771 static void init_get_clock(void)
774 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
775 || defined(__DragonFly__)
778 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
785 static int64_t get_clock(void)
787 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
788 || defined(__DragonFly__)
791 clock_gettime(CLOCK_MONOTONIC
, &ts
);
792 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
796 /* XXX: using gettimeofday leads to problems if the date
797 changes, so it should be avoided. */
799 gettimeofday(&tv
, NULL
);
800 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
805 /* Return the virtual CPU time, based on the instruction counter. */
806 static int64_t cpu_get_icount(void)
809 CPUState
*env
= cpu_single_env
;;
810 icount
= qemu_icount
;
813 fprintf(stderr
, "Bad clock read\n");
814 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
816 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
819 /***********************************************************/
820 /* guest cycle counter */
822 static int64_t cpu_ticks_prev
;
823 static int64_t cpu_ticks_offset
;
824 static int64_t cpu_clock_offset
;
825 static int cpu_ticks_enabled
;
827 /* return the host CPU cycle counter and handle stop/restart */
828 int64_t cpu_get_ticks(void)
831 return cpu_get_icount();
833 if (!cpu_ticks_enabled
) {
834 return cpu_ticks_offset
;
837 ticks
= cpu_get_real_ticks();
838 if (cpu_ticks_prev
> ticks
) {
839 /* Note: non increasing ticks may happen if the host uses
841 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
843 cpu_ticks_prev
= ticks
;
844 return ticks
+ cpu_ticks_offset
;
848 /* return the host CPU monotonic timer and handle stop/restart */
849 static int64_t cpu_get_clock(void)
852 if (!cpu_ticks_enabled
) {
853 return cpu_clock_offset
;
856 return ti
+ cpu_clock_offset
;
860 /* enable cpu_get_ticks() */
861 void cpu_enable_ticks(void)
863 if (!cpu_ticks_enabled
) {
864 cpu_ticks_offset
-= cpu_get_real_ticks();
865 cpu_clock_offset
-= get_clock();
866 cpu_ticks_enabled
= 1;
870 /* disable cpu_get_ticks() : the clock is stopped. You must not call
871 cpu_get_ticks() after that. */
872 void cpu_disable_ticks(void)
874 if (cpu_ticks_enabled
) {
875 cpu_ticks_offset
= cpu_get_ticks();
876 cpu_clock_offset
= cpu_get_clock();
877 cpu_ticks_enabled
= 0;
881 /***********************************************************/
884 #define QEMU_TIMER_REALTIME 0
885 #define QEMU_TIMER_VIRTUAL 1
889 /* XXX: add frequency */
897 struct QEMUTimer
*next
;
900 struct qemu_alarm_timer
{
904 int (*start
)(struct qemu_alarm_timer
*t
);
905 void (*stop
)(struct qemu_alarm_timer
*t
);
906 void (*rearm
)(struct qemu_alarm_timer
*t
);
910 #define ALARM_FLAG_DYNTICKS 0x1
911 #define ALARM_FLAG_EXPIRED 0x2
913 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
915 return t
->flags
& ALARM_FLAG_DYNTICKS
;
918 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
920 if (!alarm_has_dynticks(t
))
926 /* TODO: MIN_TIMER_REARM_US should be optimized */
927 #define MIN_TIMER_REARM_US 250
929 static struct qemu_alarm_timer
*alarm_timer
;
931 static int alarm_timer_rfd
, alarm_timer_wfd
;
936 struct qemu_alarm_win32
{
940 } alarm_win32_data
= {0, NULL
, -1};
942 static int win32_start_timer(struct qemu_alarm_timer
*t
);
943 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
944 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
948 static int unix_start_timer(struct qemu_alarm_timer
*t
);
949 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
953 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
954 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
955 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
957 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
958 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
960 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
961 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
963 #endif /* __linux__ */
967 /* Correlation between real and virtual time is always going to be
968 fairly approximate, so ignore small variation.
969 When the guest is idle real and virtual time will be aligned in
971 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
973 static void icount_adjust(void)
978 static int64_t last_delta
;
979 /* If the VM is not running, then do nothing. */
983 cur_time
= cpu_get_clock();
984 cur_icount
= qemu_get_clock(vm_clock
);
985 delta
= cur_icount
- cur_time
;
986 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
988 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
989 && icount_time_shift
> 0) {
990 /* The guest is getting too far ahead. Slow time down. */
994 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
995 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
996 /* The guest is getting too far behind. Speed time up. */
1000 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1003 static void icount_adjust_rt(void * opaque
)
1005 qemu_mod_timer(icount_rt_timer
,
1006 qemu_get_clock(rt_clock
) + 1000);
1010 static void icount_adjust_vm(void * opaque
)
1012 qemu_mod_timer(icount_vm_timer
,
1013 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1017 static void init_icount_adjust(void)
1019 /* Have both realtime and virtual time triggers for speed adjustment.
1020 The realtime trigger catches emulated time passing too slowly,
1021 the virtual time trigger catches emulated time passing too fast.
1022 Realtime triggers occur even when idle, so use them less frequently
1023 than VM triggers. */
1024 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1025 qemu_mod_timer(icount_rt_timer
,
1026 qemu_get_clock(rt_clock
) + 1000);
1027 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1028 qemu_mod_timer(icount_vm_timer
,
1029 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1032 static struct qemu_alarm_timer alarm_timers
[] = {
1035 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1036 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1037 /* HPET - if available - is preferred */
1038 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1039 /* ...otherwise try RTC */
1040 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1042 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1044 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1045 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1046 {"win32", 0, win32_start_timer
,
1047 win32_stop_timer
, NULL
, &alarm_win32_data
},
1052 static void show_available_alarms(void)
1056 printf("Available alarm timers, in order of precedence:\n");
1057 for (i
= 0; alarm_timers
[i
].name
; i
++)
1058 printf("%s\n", alarm_timers
[i
].name
);
1061 static void configure_alarms(char const *opt
)
1065 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1068 struct qemu_alarm_timer tmp
;
1070 if (!strcmp(opt
, "?")) {
1071 show_available_alarms();
1077 /* Reorder the array */
1078 name
= strtok(arg
, ",");
1080 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1081 if (!strcmp(alarm_timers
[i
].name
, name
))
1086 fprintf(stderr
, "Unknown clock %s\n", name
);
1095 tmp
= alarm_timers
[i
];
1096 alarm_timers
[i
] = alarm_timers
[cur
];
1097 alarm_timers
[cur
] = tmp
;
1101 name
= strtok(NULL
, ",");
1107 /* Disable remaining timers */
1108 for (i
= cur
; i
< count
; i
++)
1109 alarm_timers
[i
].name
= NULL
;
1111 show_available_alarms();
1116 QEMUClock
*rt_clock
;
1117 QEMUClock
*vm_clock
;
1119 static QEMUTimer
*active_timers
[2];
1121 static QEMUClock
*qemu_new_clock(int type
)
1124 clock
= qemu_mallocz(sizeof(QEMUClock
));
1129 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1133 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1136 ts
->opaque
= opaque
;
1140 void qemu_free_timer(QEMUTimer
*ts
)
1145 /* stop a timer, but do not dealloc it */
1146 void qemu_del_timer(QEMUTimer
*ts
)
1150 /* NOTE: this code must be signal safe because
1151 qemu_timer_expired() can be called from a signal. */
1152 pt
= &active_timers
[ts
->clock
->type
];
1165 /* modify the current timer so that it will be fired when current_time
1166 >= expire_time. The corresponding callback will be called. */
1167 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1173 /* add the timer in the sorted list */
1174 /* NOTE: this code must be signal safe because
1175 qemu_timer_expired() can be called from a signal. */
1176 pt
= &active_timers
[ts
->clock
->type
];
1181 if (t
->expire_time
> expire_time
)
1185 ts
->expire_time
= expire_time
;
1189 /* Rearm if necessary */
1190 if (pt
== &active_timers
[ts
->clock
->type
]) {
1191 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1192 qemu_rearm_alarm_timer(alarm_timer
);
1194 /* Interrupt execution to force deadline recalculation. */
1195 if (use_icount
&& cpu_single_env
) {
1196 cpu_exit(cpu_single_env
);
1201 int qemu_timer_pending(QEMUTimer
*ts
)
1204 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1211 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1215 return (timer_head
->expire_time
<= current_time
);
1218 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1224 if (!ts
|| ts
->expire_time
> current_time
)
1226 /* remove timer from the list before calling the callback */
1227 *ptimer_head
= ts
->next
;
1230 /* run the callback (the timer list can be modified) */
1235 int64_t qemu_get_clock(QEMUClock
*clock
)
1237 switch(clock
->type
) {
1238 case QEMU_TIMER_REALTIME
:
1239 return get_clock() / 1000000;
1241 case QEMU_TIMER_VIRTUAL
:
1243 return cpu_get_icount();
1245 return cpu_get_clock();
1250 static void init_timers(void)
1253 ticks_per_sec
= QEMU_TIMER_BASE
;
1254 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1255 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1259 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1261 uint64_t expire_time
;
1263 if (qemu_timer_pending(ts
)) {
1264 expire_time
= ts
->expire_time
;
1268 qemu_put_be64(f
, expire_time
);
1271 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1273 uint64_t expire_time
;
1275 expire_time
= qemu_get_be64(f
);
1276 if (expire_time
!= -1) {
1277 qemu_mod_timer(ts
, expire_time
);
1283 static void timer_save(QEMUFile
*f
, void *opaque
)
1285 if (cpu_ticks_enabled
) {
1286 hw_error("cannot save state if virtual timers are running");
1288 qemu_put_be64(f
, cpu_ticks_offset
);
1289 qemu_put_be64(f
, ticks_per_sec
);
1290 qemu_put_be64(f
, cpu_clock_offset
);
1293 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1295 if (version_id
!= 1 && version_id
!= 2)
1297 if (cpu_ticks_enabled
) {
1300 cpu_ticks_offset
=qemu_get_be64(f
);
1301 ticks_per_sec
=qemu_get_be64(f
);
1302 if (version_id
== 2) {
1303 cpu_clock_offset
=qemu_get_be64(f
);
1309 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1310 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1313 static void host_alarm_handler(int host_signum
)
1317 #define DISP_FREQ 1000
1319 static int64_t delta_min
= INT64_MAX
;
1320 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1322 ti
= qemu_get_clock(vm_clock
);
1323 if (last_clock
!= 0) {
1324 delta
= ti
- last_clock
;
1325 if (delta
< delta_min
)
1327 if (delta
> delta_max
)
1330 if (++count
== DISP_FREQ
) {
1331 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1332 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1333 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1334 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1335 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1337 delta_min
= INT64_MAX
;
1345 if (alarm_has_dynticks(alarm_timer
) ||
1347 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1348 qemu_get_clock(vm_clock
))) ||
1349 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1350 qemu_get_clock(rt_clock
))) {
1351 CPUState
*env
= next_cpu
;
1354 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1355 SetEvent(data
->host_alarm
);
1357 static const char byte
= 0;
1358 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1360 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1363 /* stop the currently executing cpu because a timer occured */
1366 if (env
->kqemu_enabled
) {
1367 kqemu_cpu_interrupt(env
);
1375 static int64_t qemu_next_deadline(void)
1379 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1380 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1381 qemu_get_clock(vm_clock
);
1383 /* To avoid problems with overflow limit this to 2^32. */
1393 #if defined(__linux__) || defined(_WIN32)
1394 static uint64_t qemu_next_deadline_dyntick(void)
1402 delta
= (qemu_next_deadline() + 999) / 1000;
1404 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1405 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1406 qemu_get_clock(rt_clock
))*1000;
1407 if (rtdelta
< delta
)
1411 if (delta
< MIN_TIMER_REARM_US
)
1412 delta
= MIN_TIMER_REARM_US
;
1420 /* Sets a specific flag */
1421 static int fcntl_setfl(int fd
, int flag
)
1425 flags
= fcntl(fd
, F_GETFL
);
1429 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1435 #if defined(__linux__)
1437 #define RTC_FREQ 1024
1439 static void enable_sigio_timer(int fd
)
1441 struct sigaction act
;
1444 sigfillset(&act
.sa_mask
);
1446 act
.sa_handler
= host_alarm_handler
;
1448 sigaction(SIGIO
, &act
, NULL
);
1449 fcntl_setfl(fd
, O_ASYNC
);
1450 fcntl(fd
, F_SETOWN
, getpid());
1453 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1455 struct hpet_info info
;
1458 fd
= open("/dev/hpet", O_RDONLY
);
1463 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1465 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1466 "error, but for better emulation accuracy type:\n"
1467 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1471 /* Check capabilities */
1472 r
= ioctl(fd
, HPET_INFO
, &info
);
1476 /* Enable periodic mode */
1477 r
= ioctl(fd
, HPET_EPI
, 0);
1478 if (info
.hi_flags
&& (r
< 0))
1481 /* Enable interrupt */
1482 r
= ioctl(fd
, HPET_IE_ON
, 0);
1486 enable_sigio_timer(fd
);
1487 t
->priv
= (void *)(long)fd
;
1495 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1497 int fd
= (long)t
->priv
;
1502 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1505 unsigned long current_rtc_freq
= 0;
1507 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1510 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1511 if (current_rtc_freq
!= RTC_FREQ
&&
1512 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1513 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1514 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1515 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1518 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1524 enable_sigio_timer(rtc_fd
);
1526 t
->priv
= (void *)(long)rtc_fd
;
1531 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1533 int rtc_fd
= (long)t
->priv
;
1538 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1542 struct sigaction act
;
1544 sigfillset(&act
.sa_mask
);
1546 act
.sa_handler
= host_alarm_handler
;
1548 sigaction(SIGALRM
, &act
, NULL
);
1550 ev
.sigev_value
.sival_int
= 0;
1551 ev
.sigev_notify
= SIGEV_SIGNAL
;
1552 ev
.sigev_signo
= SIGALRM
;
1554 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1555 perror("timer_create");
1557 /* disable dynticks */
1558 fprintf(stderr
, "Dynamic Ticks disabled\n");
1563 t
->priv
= (void *)(long)host_timer
;
1568 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1570 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1572 timer_delete(host_timer
);
1575 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1577 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1578 struct itimerspec timeout
;
1579 int64_t nearest_delta_us
= INT64_MAX
;
1582 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1583 !active_timers
[QEMU_TIMER_VIRTUAL
])
1586 nearest_delta_us
= qemu_next_deadline_dyntick();
1588 /* check whether a timer is already running */
1589 if (timer_gettime(host_timer
, &timeout
)) {
1591 fprintf(stderr
, "Internal timer error: aborting\n");
1594 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1595 if (current_us
&& current_us
<= nearest_delta_us
)
1598 timeout
.it_interval
.tv_sec
= 0;
1599 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1600 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1601 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1602 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1604 fprintf(stderr
, "Internal timer error: aborting\n");
1609 #endif /* defined(__linux__) */
1611 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1613 struct sigaction act
;
1614 struct itimerval itv
;
1618 sigfillset(&act
.sa_mask
);
1620 act
.sa_handler
= host_alarm_handler
;
1622 sigaction(SIGALRM
, &act
, NULL
);
1624 itv
.it_interval
.tv_sec
= 0;
1625 /* for i386 kernel 2.6 to get 1 ms */
1626 itv
.it_interval
.tv_usec
= 999;
1627 itv
.it_value
.tv_sec
= 0;
1628 itv
.it_value
.tv_usec
= 10 * 1000;
1630 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1637 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1639 struct itimerval itv
;
1641 memset(&itv
, 0, sizeof(itv
));
1642 setitimer(ITIMER_REAL
, &itv
, NULL
);
1645 #endif /* !defined(_WIN32) */
1647 static void try_to_rearm_timer(void *opaque
)
1649 struct qemu_alarm_timer
*t
= opaque
;
1653 /* Drain the notify pipe */
1656 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1657 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1660 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1661 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1662 qemu_rearm_alarm_timer(alarm_timer
);
1668 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1671 struct qemu_alarm_win32
*data
= t
->priv
;
1674 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1675 if (!data
->host_alarm
) {
1676 perror("Failed CreateEvent");
1680 memset(&tc
, 0, sizeof(tc
));
1681 timeGetDevCaps(&tc
, sizeof(tc
));
1683 if (data
->period
< tc
.wPeriodMin
)
1684 data
->period
= tc
.wPeriodMin
;
1686 timeBeginPeriod(data
->period
);
1688 flags
= TIME_CALLBACK_FUNCTION
;
1689 if (alarm_has_dynticks(t
))
1690 flags
|= TIME_ONESHOT
;
1692 flags
|= TIME_PERIODIC
;
1694 data
->timerId
= timeSetEvent(1, // interval (ms)
1695 data
->period
, // resolution
1696 host_alarm_handler
, // function
1697 (DWORD
)t
, // parameter
1700 if (!data
->timerId
) {
1701 perror("Failed to initialize win32 alarm timer");
1703 timeEndPeriod(data
->period
);
1704 CloseHandle(data
->host_alarm
);
1708 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1713 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1715 struct qemu_alarm_win32
*data
= t
->priv
;
1717 timeKillEvent(data
->timerId
);
1718 timeEndPeriod(data
->period
);
1720 CloseHandle(data
->host_alarm
);
1723 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1725 struct qemu_alarm_win32
*data
= t
->priv
;
1726 uint64_t nearest_delta_us
;
1728 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1729 !active_timers
[QEMU_TIMER_VIRTUAL
])
1732 nearest_delta_us
= qemu_next_deadline_dyntick();
1733 nearest_delta_us
/= 1000;
1735 timeKillEvent(data
->timerId
);
1737 data
->timerId
= timeSetEvent(1,
1741 TIME_ONESHOT
| TIME_PERIODIC
);
1743 if (!data
->timerId
) {
1744 perror("Failed to re-arm win32 alarm timer");
1746 timeEndPeriod(data
->period
);
1747 CloseHandle(data
->host_alarm
);
1754 static int init_timer_alarm(void)
1756 struct qemu_alarm_timer
*t
= NULL
;
1766 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1770 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1774 alarm_timer_rfd
= fds
[0];
1775 alarm_timer_wfd
= fds
[1];
1778 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1779 t
= &alarm_timers
[i
];
1792 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1793 try_to_rearm_timer
, NULL
, t
);
1808 static void quit_timers(void)
1810 alarm_timer
->stop(alarm_timer
);
1814 /***********************************************************/
1815 /* host time/date access */
1816 void qemu_get_timedate(struct tm
*tm
, int offset
)
1823 if (rtc_date_offset
== -1) {
1827 ret
= localtime(&ti
);
1829 ti
-= rtc_date_offset
;
1833 memcpy(tm
, ret
, sizeof(struct tm
));
1836 int qemu_timedate_diff(struct tm
*tm
)
1840 if (rtc_date_offset
== -1)
1842 seconds
= mktimegm(tm
);
1844 seconds
= mktime(tm
);
1846 seconds
= mktimegm(tm
) + rtc_date_offset
;
1848 return seconds
- time(NULL
);
1852 static void socket_cleanup(void)
1857 static int socket_init(void)
1862 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1864 err
= WSAGetLastError();
1865 fprintf(stderr
, "WSAStartup: %d\n", err
);
1868 atexit(socket_cleanup
);
1873 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
1878 while (*p
!= '\0' && *p
!= delim
) {
1879 if (q
&& (q
- buf
) < buf_size
- 1)
1889 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1894 while (*p
!= '\0') {
1896 if (*(p
+ 1) != ',')
1900 if (q
&& (q
- buf
) < buf_size
- 1)
1910 int get_param_value(char *buf
, int buf_size
,
1911 const char *tag
, const char *str
)
1918 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1922 if (!strcmp(tag
, option
)) {
1923 (void)get_opt_value(buf
, buf_size
, p
);
1926 p
= get_opt_value(NULL
, 0, p
);
1935 int check_params(char *buf
, int buf_size
,
1936 const char * const *params
, const char *str
)
1942 while (*p
!= '\0') {
1943 p
= get_opt_name(buf
, buf_size
, p
, '=');
1947 for(i
= 0; params
[i
] != NULL
; i
++)
1948 if (!strcmp(params
[i
], buf
))
1950 if (params
[i
] == NULL
)
1952 p
= get_opt_value(NULL
, 0, p
);
1960 /***********************************************************/
1961 /* Bluetooth support */
1964 static struct HCIInfo
*hci_table
[MAX_NICS
];
1966 static struct bt_vlan_s
{
1967 struct bt_scatternet_s net
;
1969 struct bt_vlan_s
*next
;
1972 /* find or alloc a new bluetooth "VLAN" */
1973 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1975 struct bt_vlan_s
**pvlan
, *vlan
;
1976 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1980 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1982 pvlan
= &first_bt_vlan
;
1983 while (*pvlan
!= NULL
)
1984 pvlan
= &(*pvlan
)->next
;
1989 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1993 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1998 static struct HCIInfo null_hci
= {
1999 .cmd_send
= null_hci_send
,
2000 .sco_send
= null_hci_send
,
2001 .acl_send
= null_hci_send
,
2002 .bdaddr_set
= null_hci_addr_set
,
2005 struct HCIInfo
*qemu_next_hci(void)
2007 if (cur_hci
== nb_hcis
)
2010 return hci_table
[cur_hci
++];
2013 static struct HCIInfo
*hci_init(const char *str
)
2016 struct bt_scatternet_s
*vlan
= 0;
2018 if (!strcmp(str
, "null"))
2021 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2023 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2024 else if (!strncmp(str
, "hci", 3)) {
2027 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2028 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2033 vlan
= qemu_find_bt_vlan(0);
2035 return bt_new_hci(vlan
);
2038 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2043 static int bt_hci_parse(const char *str
)
2045 struct HCIInfo
*hci
;
2048 if (nb_hcis
>= MAX_NICS
) {
2049 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2053 hci
= hci_init(str
);
2062 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2063 hci
->bdaddr_set(hci
, bdaddr
.b
);
2065 hci_table
[nb_hcis
++] = hci
;
2070 static void bt_vhci_add(int vlan_id
)
2072 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2075 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2076 "an empty scatternet %i\n", vlan_id
);
2078 bt_vhci_init(bt_new_hci(vlan
));
2081 static struct bt_device_s
*bt_device_add(const char *opt
)
2083 struct bt_scatternet_s
*vlan
;
2085 char *endp
= strstr(opt
, ",vlan=");
2086 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2089 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2092 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2094 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2099 vlan
= qemu_find_bt_vlan(vlan_id
);
2102 fprintf(stderr
, "qemu: warning: adding a slave device to "
2103 "an empty scatternet %i\n", vlan_id
);
2105 if (!strcmp(devname
, "keyboard"))
2106 return bt_keyboard_init(vlan
);
2108 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2112 static int bt_parse(const char *opt
)
2114 const char *endp
, *p
;
2117 if (strstart(opt
, "hci", &endp
)) {
2118 if (!*endp
|| *endp
== ',') {
2120 if (!strstart(endp
, ",vlan=", 0))
2123 return bt_hci_parse(opt
);
2125 } else if (strstart(opt
, "vhci", &endp
)) {
2126 if (!*endp
|| *endp
== ',') {
2128 if (strstart(endp
, ",vlan=", &p
)) {
2129 vlan
= strtol(p
, (char **) &endp
, 0);
2131 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2135 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2144 } else if (strstart(opt
, "device:", &endp
))
2145 return !bt_device_add(endp
);
2147 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2151 /***********************************************************/
2152 /* QEMU Block devices */
2154 #define HD_ALIAS "index=%d,media=disk"
2155 #define CDROM_ALIAS "index=2,media=cdrom"
2156 #define FD_ALIAS "index=%d,if=floppy"
2157 #define PFLASH_ALIAS "if=pflash"
2158 #define MTD_ALIAS "if=mtd"
2159 #define SD_ALIAS "index=0,if=sd"
2161 static int drive_opt_get_free_idx(void)
2165 for (index
= 0; index
< MAX_DRIVES
; index
++)
2166 if (!drives_opt
[index
].used
) {
2167 drives_opt
[index
].used
= 1;
2174 static int drive_get_free_idx(void)
2178 for (index
= 0; index
< MAX_DRIVES
; index
++)
2179 if (!drives_table
[index
].used
) {
2180 drives_table
[index
].used
= 1;
2187 int drive_add(const char *file
, const char *fmt
, ...)
2190 int index
= drive_opt_get_free_idx();
2192 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2193 fprintf(stderr
, "qemu: too many drives\n");
2197 drives_opt
[index
].file
= file
;
2199 vsnprintf(drives_opt
[index
].opt
,
2200 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2207 void drive_remove(int index
)
2209 drives_opt
[index
].used
= 0;
2213 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2217 /* seek interface, bus and unit */
2219 for (index
= 0; index
< MAX_DRIVES
; index
++)
2220 if (drives_table
[index
].type
== type
&&
2221 drives_table
[index
].bus
== bus
&&
2222 drives_table
[index
].unit
== unit
&&
2223 drives_table
[index
].used
)
2229 int drive_get_max_bus(BlockInterfaceType type
)
2235 for (index
= 0; index
< nb_drives
; index
++) {
2236 if(drives_table
[index
].type
== type
&&
2237 drives_table
[index
].bus
> max_bus
)
2238 max_bus
= drives_table
[index
].bus
;
2243 const char *drive_get_serial(BlockDriverState
*bdrv
)
2247 for (index
= 0; index
< nb_drives
; index
++)
2248 if (drives_table
[index
].bdrv
== bdrv
)
2249 return drives_table
[index
].serial
;
2254 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2258 for (index
= 0; index
< nb_drives
; index
++)
2259 if (drives_table
[index
].bdrv
== bdrv
)
2260 return drives_table
[index
].onerror
;
2262 return BLOCK_ERR_STOP_ENOSPC
;
2265 static void bdrv_format_print(void *opaque
, const char *name
)
2267 fprintf(stderr
, " %s", name
);
2270 void drive_uninit(BlockDriverState
*bdrv
)
2274 for (i
= 0; i
< MAX_DRIVES
; i
++)
2275 if (drives_table
[i
].bdrv
== bdrv
) {
2276 drives_table
[i
].bdrv
= NULL
;
2277 drives_table
[i
].used
= 0;
2278 drive_remove(drives_table
[i
].drive_opt_idx
);
2284 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2290 const char *mediastr
= "";
2291 BlockInterfaceType type
;
2292 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2293 int bus_id
, unit_id
;
2294 int cyls
, heads
, secs
, translation
;
2295 BlockDriverState
*bdrv
;
2296 BlockDriver
*drv
= NULL
;
2297 QEMUMachine
*machine
= opaque
;
2301 int bdrv_flags
, onerror
;
2302 int drives_table_idx
;
2303 char *str
= arg
->opt
;
2304 static const char * const params
[] = { "bus", "unit", "if", "index",
2305 "cyls", "heads", "secs", "trans",
2306 "media", "snapshot", "file",
2307 "cache", "format", "serial", "werror",
2310 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2311 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2317 cyls
= heads
= secs
= 0;
2320 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2324 if (machine
->use_scsi
) {
2326 max_devs
= MAX_SCSI_DEVS
;
2327 pstrcpy(devname
, sizeof(devname
), "scsi");
2330 max_devs
= MAX_IDE_DEVS
;
2331 pstrcpy(devname
, sizeof(devname
), "ide");
2335 /* extract parameters */
2337 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2338 bus_id
= strtol(buf
, NULL
, 0);
2340 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2345 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2346 unit_id
= strtol(buf
, NULL
, 0);
2348 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2353 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2354 pstrcpy(devname
, sizeof(devname
), buf
);
2355 if (!strcmp(buf
, "ide")) {
2357 max_devs
= MAX_IDE_DEVS
;
2358 } else if (!strcmp(buf
, "scsi")) {
2360 max_devs
= MAX_SCSI_DEVS
;
2361 } else if (!strcmp(buf
, "floppy")) {
2364 } else if (!strcmp(buf
, "pflash")) {
2367 } else if (!strcmp(buf
, "mtd")) {
2370 } else if (!strcmp(buf
, "sd")) {
2373 } else if (!strcmp(buf
, "virtio")) {
2377 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2382 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2383 index
= strtol(buf
, NULL
, 0);
2385 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2390 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2391 cyls
= strtol(buf
, NULL
, 0);
2394 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2395 heads
= strtol(buf
, NULL
, 0);
2398 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2399 secs
= strtol(buf
, NULL
, 0);
2402 if (cyls
|| heads
|| secs
) {
2403 if (cyls
< 1 || cyls
> 16383) {
2404 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2407 if (heads
< 1 || heads
> 16) {
2408 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2411 if (secs
< 1 || secs
> 63) {
2412 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2417 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2420 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2424 if (!strcmp(buf
, "none"))
2425 translation
= BIOS_ATA_TRANSLATION_NONE
;
2426 else if (!strcmp(buf
, "lba"))
2427 translation
= BIOS_ATA_TRANSLATION_LBA
;
2428 else if (!strcmp(buf
, "auto"))
2429 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2431 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2436 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2437 if (!strcmp(buf
, "disk")) {
2439 } else if (!strcmp(buf
, "cdrom")) {
2440 if (cyls
|| secs
|| heads
) {
2442 "qemu: '%s' invalid physical CHS format\n", str
);
2445 media
= MEDIA_CDROM
;
2447 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2452 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2453 if (!strcmp(buf
, "on"))
2455 else if (!strcmp(buf
, "off"))
2458 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2463 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2464 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2466 else if (!strcmp(buf
, "writethrough"))
2468 else if (!strcmp(buf
, "writeback"))
2471 fprintf(stderr
, "qemu: invalid cache option\n");
2476 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2477 if (strcmp(buf
, "?") == 0) {
2478 fprintf(stderr
, "qemu: Supported formats:");
2479 bdrv_iterate_format(bdrv_format_print
, NULL
);
2480 fprintf(stderr
, "\n");
2483 drv
= bdrv_find_format(buf
);
2485 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2490 if (arg
->file
== NULL
)
2491 get_param_value(file
, sizeof(file
), "file", str
);
2493 pstrcpy(file
, sizeof(file
), arg
->file
);
2495 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2496 memset(serial
, 0, sizeof(serial
));
2498 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2499 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2500 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2501 fprintf(stderr
, "werror is no supported by this format\n");
2504 if (!strcmp(buf
, "ignore"))
2505 onerror
= BLOCK_ERR_IGNORE
;
2506 else if (!strcmp(buf
, "enospc"))
2507 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2508 else if (!strcmp(buf
, "stop"))
2509 onerror
= BLOCK_ERR_STOP_ANY
;
2510 else if (!strcmp(buf
, "report"))
2511 onerror
= BLOCK_ERR_REPORT
;
2513 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2518 /* compute bus and unit according index */
2521 if (bus_id
!= 0 || unit_id
!= -1) {
2523 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2531 unit_id
= index
% max_devs
;
2532 bus_id
= index
/ max_devs
;
2536 /* if user doesn't specify a unit_id,
2537 * try to find the first free
2540 if (unit_id
== -1) {
2542 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2544 if (max_devs
&& unit_id
>= max_devs
) {
2545 unit_id
-= max_devs
;
2553 if (max_devs
&& unit_id
>= max_devs
) {
2554 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2555 str
, unit_id
, max_devs
- 1);
2560 * ignore multiple definitions
2563 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2568 if (type
== IF_IDE
|| type
== IF_SCSI
)
2569 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2571 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2572 devname
, bus_id
, mediastr
, unit_id
);
2574 snprintf(buf
, sizeof(buf
), "%s%s%i",
2575 devname
, mediastr
, unit_id
);
2576 bdrv
= bdrv_new(buf
);
2577 drives_table_idx
= drive_get_free_idx();
2578 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2579 drives_table
[drives_table_idx
].type
= type
;
2580 drives_table
[drives_table_idx
].bus
= bus_id
;
2581 drives_table
[drives_table_idx
].unit
= unit_id
;
2582 drives_table
[drives_table_idx
].onerror
= onerror
;
2583 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2584 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2593 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2594 bdrv_set_translation_hint(bdrv
, translation
);
2598 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2603 /* FIXME: This isn't really a floppy, but it's a reasonable
2606 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2617 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2618 cache
= 2; /* always use write-back with snapshot */
2620 if (cache
== 0) /* no caching */
2621 bdrv_flags
|= BDRV_O_NOCACHE
;
2622 else if (cache
== 2) /* write-back */
2623 bdrv_flags
|= BDRV_O_CACHE_WB
;
2624 else if (cache
== 3) /* not specified */
2625 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2626 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2627 fprintf(stderr
, "qemu: could not open disk image %s\n",
2631 if (bdrv_key_required(bdrv
))
2633 return drives_table_idx
;
2636 static void numa_add(const char *optarg
)
2640 unsigned long long value
, endvalue
;
2643 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2644 if (!strcmp(option
, "node")) {
2645 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2646 nodenr
= nb_numa_nodes
;
2648 nodenr
= strtoull(option
, NULL
, 10);
2651 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2652 node_mem
[nodenr
] = 0;
2654 value
= strtoull(option
, &endptr
, 0);
2656 case 0: case 'M': case 'm':
2663 node_mem
[nodenr
] = value
;
2665 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2666 node_cpumask
[nodenr
] = 0;
2668 value
= strtoull(option
, &endptr
, 10);
2671 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2673 if (*endptr
== '-') {
2674 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2675 if (endvalue
>= 63) {
2678 "only 63 CPUs in NUMA mode supported.\n");
2680 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2685 node_cpumask
[nodenr
] = value
;
2692 /***********************************************************/
2695 static USBPort
*used_usb_ports
;
2696 static USBPort
*free_usb_ports
;
2698 /* ??? Maybe change this to register a hub to keep track of the topology. */
2699 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2700 usb_attachfn attach
)
2702 port
->opaque
= opaque
;
2703 port
->index
= index
;
2704 port
->attach
= attach
;
2705 port
->next
= free_usb_ports
;
2706 free_usb_ports
= port
;
2709 int usb_device_add_dev(USBDevice
*dev
)
2713 /* Find a USB port to add the device to. */
2714 port
= free_usb_ports
;
2718 /* Create a new hub and chain it on. */
2719 free_usb_ports
= NULL
;
2720 port
->next
= used_usb_ports
;
2721 used_usb_ports
= port
;
2723 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2724 usb_attach(port
, hub
);
2725 port
= free_usb_ports
;
2728 free_usb_ports
= port
->next
;
2729 port
->next
= used_usb_ports
;
2730 used_usb_ports
= port
;
2731 usb_attach(port
, dev
);
2735 static void usb_msd_password_cb(void *opaque
, int err
)
2737 USBDevice
*dev
= opaque
;
2740 usb_device_add_dev(dev
);
2742 dev
->handle_destroy(dev
);
2745 static int usb_device_add(const char *devname
, int is_hotplug
)
2750 if (!free_usb_ports
)
2753 if (strstart(devname
, "host:", &p
)) {
2754 dev
= usb_host_device_open(p
);
2755 } else if (!strcmp(devname
, "mouse")) {
2756 dev
= usb_mouse_init();
2757 } else if (!strcmp(devname
, "tablet")) {
2758 dev
= usb_tablet_init();
2759 } else if (!strcmp(devname
, "keyboard")) {
2760 dev
= usb_keyboard_init();
2761 } else if (strstart(devname
, "disk:", &p
)) {
2762 BlockDriverState
*bs
;
2764 dev
= usb_msd_init(p
);
2767 bs
= usb_msd_get_bdrv(dev
);
2768 if (bdrv_key_required(bs
)) {
2771 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2776 } else if (!strcmp(devname
, "wacom-tablet")) {
2777 dev
= usb_wacom_init();
2778 } else if (strstart(devname
, "serial:", &p
)) {
2779 dev
= usb_serial_init(p
);
2780 #ifdef CONFIG_BRLAPI
2781 } else if (!strcmp(devname
, "braille")) {
2782 dev
= usb_baum_init();
2784 } else if (strstart(devname
, "net:", &p
)) {
2787 if (net_client_init("nic", p
) < 0)
2789 nd_table
[nic
].model
= "usb";
2790 dev
= usb_net_init(&nd_table
[nic
]);
2791 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2792 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2793 bt_new_hci(qemu_find_bt_vlan(0)));
2800 return usb_device_add_dev(dev
);
2803 int usb_device_del_addr(int bus_num
, int addr
)
2809 if (!used_usb_ports
)
2815 lastp
= &used_usb_ports
;
2816 port
= used_usb_ports
;
2817 while (port
&& port
->dev
->addr
!= addr
) {
2818 lastp
= &port
->next
;
2826 *lastp
= port
->next
;
2827 usb_attach(port
, NULL
);
2828 dev
->handle_destroy(dev
);
2829 port
->next
= free_usb_ports
;
2830 free_usb_ports
= port
;
2834 static int usb_device_del(const char *devname
)
2839 if (strstart(devname
, "host:", &p
))
2840 return usb_host_device_close(p
);
2842 if (!used_usb_ports
)
2845 p
= strchr(devname
, '.');
2848 bus_num
= strtoul(devname
, NULL
, 0);
2849 addr
= strtoul(p
+ 1, NULL
, 0);
2851 return usb_device_del_addr(bus_num
, addr
);
2854 void do_usb_add(Monitor
*mon
, const char *devname
)
2856 usb_device_add(devname
, 1);
2859 void do_usb_del(Monitor
*mon
, const char *devname
)
2861 usb_device_del(devname
);
2864 void usb_info(Monitor
*mon
)
2868 const char *speed_str
;
2871 monitor_printf(mon
, "USB support not enabled\n");
2875 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2879 switch(dev
->speed
) {
2883 case USB_SPEED_FULL
:
2886 case USB_SPEED_HIGH
:
2893 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2894 0, dev
->addr
, speed_str
, dev
->devname
);
2898 /***********************************************************/
2899 /* PCMCIA/Cardbus */
2901 static struct pcmcia_socket_entry_s
{
2902 struct pcmcia_socket_s
*socket
;
2903 struct pcmcia_socket_entry_s
*next
;
2904 } *pcmcia_sockets
= 0;
2906 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2908 struct pcmcia_socket_entry_s
*entry
;
2910 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2911 entry
->socket
= socket
;
2912 entry
->next
= pcmcia_sockets
;
2913 pcmcia_sockets
= entry
;
2916 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2918 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2920 ptr
= &pcmcia_sockets
;
2921 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2922 if (entry
->socket
== socket
) {
2928 void pcmcia_info(Monitor
*mon
)
2930 struct pcmcia_socket_entry_s
*iter
;
2932 if (!pcmcia_sockets
)
2933 monitor_printf(mon
, "No PCMCIA sockets\n");
2935 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2936 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2937 iter
->socket
->attached
? iter
->socket
->card_string
:
2941 /***********************************************************/
2942 /* register display */
2944 struct DisplayAllocator default_allocator
= {
2945 defaultallocator_create_displaysurface
,
2946 defaultallocator_resize_displaysurface
,
2947 defaultallocator_free_displaysurface
2950 void register_displaystate(DisplayState
*ds
)
2960 DisplayState
*get_displaystate(void)
2962 return display_state
;
2965 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2967 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2968 return ds
->allocator
;
2973 static void dumb_display_init(void)
2975 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2976 ds
->allocator
= &default_allocator
;
2977 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2978 register_displaystate(ds
);
2981 /***********************************************************/
2984 typedef struct IOHandlerRecord
{
2986 IOCanRWHandler
*fd_read_poll
;
2988 IOHandler
*fd_write
;
2991 /* temporary data */
2993 struct IOHandlerRecord
*next
;
2996 static IOHandlerRecord
*first_io_handler
;
2998 /* XXX: fd_read_poll should be suppressed, but an API change is
2999 necessary in the character devices to suppress fd_can_read(). */
3000 int qemu_set_fd_handler2(int fd
,
3001 IOCanRWHandler
*fd_read_poll
,
3003 IOHandler
*fd_write
,
3006 IOHandlerRecord
**pioh
, *ioh
;
3008 if (!fd_read
&& !fd_write
) {
3009 pioh
= &first_io_handler
;
3014 if (ioh
->fd
== fd
) {
3021 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3025 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3026 ioh
->next
= first_io_handler
;
3027 first_io_handler
= ioh
;
3030 ioh
->fd_read_poll
= fd_read_poll
;
3031 ioh
->fd_read
= fd_read
;
3032 ioh
->fd_write
= fd_write
;
3033 ioh
->opaque
= opaque
;
3039 int qemu_set_fd_handler(int fd
,
3041 IOHandler
*fd_write
,
3044 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3048 /***********************************************************/
3049 /* Polling handling */
3051 typedef struct PollingEntry
{
3054 struct PollingEntry
*next
;
3057 static PollingEntry
*first_polling_entry
;
3059 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3061 PollingEntry
**ppe
, *pe
;
3062 pe
= qemu_mallocz(sizeof(PollingEntry
));
3064 pe
->opaque
= opaque
;
3065 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3070 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3072 PollingEntry
**ppe
, *pe
;
3073 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3075 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3083 /***********************************************************/
3084 /* Wait objects support */
3085 typedef struct WaitObjects
{
3087 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3088 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3089 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3092 static WaitObjects wait_objects
= {0};
3094 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3096 WaitObjects
*w
= &wait_objects
;
3098 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3100 w
->events
[w
->num
] = handle
;
3101 w
->func
[w
->num
] = func
;
3102 w
->opaque
[w
->num
] = opaque
;
3107 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3110 WaitObjects
*w
= &wait_objects
;
3113 for (i
= 0; i
< w
->num
; i
++) {
3114 if (w
->events
[i
] == handle
)
3117 w
->events
[i
] = w
->events
[i
+ 1];
3118 w
->func
[i
] = w
->func
[i
+ 1];
3119 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3127 /***********************************************************/
3128 /* ram save/restore */
3130 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3134 v
= qemu_get_byte(f
);
3137 if (qemu_get_buffer(f
, buf
, len
) != len
)
3141 v
= qemu_get_byte(f
);
3142 memset(buf
, v
, len
);
3148 if (qemu_file_has_error(f
))
3154 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3159 if (qemu_get_be32(f
) != last_ram_offset
)
3161 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
3162 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
3169 #define BDRV_HASH_BLOCK_SIZE 1024
3170 #define IOBUF_SIZE 4096
3171 #define RAM_CBLOCK_MAGIC 0xfabe
3173 typedef struct RamDecompressState
{
3176 uint8_t buf
[IOBUF_SIZE
];
3177 } RamDecompressState
;
3179 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3182 memset(s
, 0, sizeof(*s
));
3184 ret
= inflateInit(&s
->zstream
);
3190 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3194 s
->zstream
.avail_out
= len
;
3195 s
->zstream
.next_out
= buf
;
3196 while (s
->zstream
.avail_out
> 0) {
3197 if (s
->zstream
.avail_in
== 0) {
3198 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3200 clen
= qemu_get_be16(s
->f
);
3201 if (clen
> IOBUF_SIZE
)
3203 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3204 s
->zstream
.avail_in
= clen
;
3205 s
->zstream
.next_in
= s
->buf
;
3207 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3208 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3215 static void ram_decompress_close(RamDecompressState
*s
)
3217 inflateEnd(&s
->zstream
);
3220 #define RAM_SAVE_FLAG_FULL 0x01
3221 #define RAM_SAVE_FLAG_COMPRESS 0x02
3222 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3223 #define RAM_SAVE_FLAG_PAGE 0x08
3224 #define RAM_SAVE_FLAG_EOS 0x10
3226 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3228 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3229 uint32_t *array
= (uint32_t *)page
;
3232 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3233 if (array
[i
] != val
)
3240 static int ram_save_block(QEMUFile
*f
)
3242 static ram_addr_t current_addr
= 0;
3243 ram_addr_t saved_addr
= current_addr
;
3244 ram_addr_t addr
= 0;
3247 while (addr
< last_ram_offset
) {
3248 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3251 cpu_physical_memory_reset_dirty(current_addr
,
3252 current_addr
+ TARGET_PAGE_SIZE
,
3253 MIGRATION_DIRTY_FLAG
);
3255 p
= qemu_get_ram_ptr(current_addr
);
3257 if (is_dup_page(p
, *p
)) {
3258 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3259 qemu_put_byte(f
, *p
);
3261 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3262 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3268 addr
+= TARGET_PAGE_SIZE
;
3269 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3275 static ram_addr_t ram_save_threshold
= 10;
3277 static ram_addr_t
ram_save_remaining(void)
3280 ram_addr_t count
= 0;
3282 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3283 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3290 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3295 /* Make sure all dirty bits are set */
3296 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3297 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3298 cpu_physical_memory_set_dirty(addr
);
3301 /* Enable dirty memory tracking */
3302 cpu_physical_memory_set_dirty_tracking(1);
3304 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3307 while (!qemu_file_rate_limit(f
)) {
3310 ret
= ram_save_block(f
);
3311 if (ret
== 0) /* no more blocks */
3315 /* try transferring iterative blocks of memory */
3319 /* flush all remaining blocks regardless of rate limiting */
3320 while (ram_save_block(f
) != 0);
3321 cpu_physical_memory_set_dirty_tracking(0);
3324 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3326 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3329 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3331 RamDecompressState s1
, *s
= &s1
;
3335 if (ram_decompress_open(s
, f
) < 0)
3337 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3338 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3339 fprintf(stderr
, "Error while reading ram block header\n");
3343 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3344 BDRV_HASH_BLOCK_SIZE
) < 0) {
3345 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3350 printf("Error block header\n");
3354 ram_decompress_close(s
);
3359 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3364 if (version_id
== 1)
3365 return ram_load_v1(f
, opaque
);
3367 if (version_id
== 2) {
3368 if (qemu_get_be32(f
) != last_ram_offset
)
3370 return ram_load_dead(f
, opaque
);
3373 if (version_id
!= 3)
3377 addr
= qemu_get_be64(f
);
3379 flags
= addr
& ~TARGET_PAGE_MASK
;
3380 addr
&= TARGET_PAGE_MASK
;
3382 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3383 if (addr
!= last_ram_offset
)
3387 if (flags
& RAM_SAVE_FLAG_FULL
) {
3388 if (ram_load_dead(f
, opaque
) < 0)
3392 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3393 uint8_t ch
= qemu_get_byte(f
);
3394 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3395 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3396 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3397 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3402 void qemu_service_io(void)
3404 CPUState
*env
= cpu_single_env
;
3408 if (env
->kqemu_enabled
) {
3409 kqemu_cpu_interrupt(env
);
3415 /***********************************************************/
3416 /* bottom halves (can be seen as timers which expire ASAP) */
3427 static QEMUBH
*first_bh
= NULL
;
3429 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3432 bh
= qemu_mallocz(sizeof(QEMUBH
));
3434 bh
->opaque
= opaque
;
3435 bh
->next
= first_bh
;
3440 int qemu_bh_poll(void)
3446 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3447 if (!bh
->deleted
&& bh
->scheduled
) {
3456 /* remove deleted bhs */
3470 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3478 void qemu_bh_schedule(QEMUBH
*bh
)
3480 CPUState
*env
= cpu_single_env
;
3485 /* stop the currently executing CPU to execute the BH ASAP */
3491 void qemu_bh_cancel(QEMUBH
*bh
)
3496 void qemu_bh_delete(QEMUBH
*bh
)
3502 static void qemu_bh_update_timeout(int *timeout
)
3506 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3507 if (!bh
->deleted
&& bh
->scheduled
) {
3509 /* idle bottom halves will be polled at least
3511 *timeout
= MIN(10, *timeout
);
3513 /* non-idle bottom halves will be executed
3522 /***********************************************************/
3523 /* machine registration */
3525 static QEMUMachine
*first_machine
= NULL
;
3526 QEMUMachine
*current_machine
= NULL
;
3528 int qemu_register_machine(QEMUMachine
*m
)
3531 pm
= &first_machine
;
3539 static QEMUMachine
*find_machine(const char *name
)
3543 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3544 if (!strcmp(m
->name
, name
))
3550 /***********************************************************/
3551 /* main execution loop */
3553 static void gui_update(void *opaque
)
3555 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3556 DisplayState
*ds
= opaque
;
3557 DisplayChangeListener
*dcl
= ds
->listeners
;
3561 while (dcl
!= NULL
) {
3562 if (dcl
->gui_timer_interval
&&
3563 dcl
->gui_timer_interval
< interval
)
3564 interval
= dcl
->gui_timer_interval
;
3567 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3570 static void nographic_update(void *opaque
)
3572 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3574 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3577 struct vm_change_state_entry
{
3578 VMChangeStateHandler
*cb
;
3580 LIST_ENTRY (vm_change_state_entry
) entries
;
3583 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3585 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3588 VMChangeStateEntry
*e
;
3590 e
= qemu_mallocz(sizeof (*e
));
3594 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3598 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3600 LIST_REMOVE (e
, entries
);
3604 static void vm_state_notify(int running
, int reason
)
3606 VMChangeStateEntry
*e
;
3608 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3609 e
->cb(e
->opaque
, running
, reason
);
3618 vm_state_notify(1, 0);
3619 qemu_rearm_alarm_timer(alarm_timer
);
3623 void vm_stop(int reason
)
3626 cpu_disable_ticks();
3628 vm_state_notify(0, reason
);
3632 /* reset/shutdown handler */
3634 typedef struct QEMUResetEntry
{
3635 QEMUResetHandler
*func
;
3637 struct QEMUResetEntry
*next
;
3640 static QEMUResetEntry
*first_reset_entry
;
3641 static int reset_requested
;
3642 static int shutdown_requested
;
3643 static int powerdown_requested
;
3645 int qemu_shutdown_requested(void)
3647 int r
= shutdown_requested
;
3648 shutdown_requested
= 0;
3652 int qemu_reset_requested(void)
3654 int r
= reset_requested
;
3655 reset_requested
= 0;
3659 int qemu_powerdown_requested(void)
3661 int r
= powerdown_requested
;
3662 powerdown_requested
= 0;
3666 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3668 QEMUResetEntry
**pre
, *re
;
3670 pre
= &first_reset_entry
;
3671 while (*pre
!= NULL
)
3672 pre
= &(*pre
)->next
;
3673 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3675 re
->opaque
= opaque
;
3680 void qemu_system_reset(void)
3684 /* reset all devices */
3685 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3686 re
->func(re
->opaque
);
3692 void qemu_system_reset_request(void)
3695 shutdown_requested
= 1;
3697 reset_requested
= 1;
3700 cpu_exit(cpu_single_env
);
3703 void qemu_system_shutdown_request(void)
3705 shutdown_requested
= 1;
3707 cpu_exit(cpu_single_env
);
3710 void qemu_system_powerdown_request(void)
3712 powerdown_requested
= 1;
3714 cpu_exit(cpu_single_env
);
3718 static void host_main_loop_wait(int *timeout
)
3724 /* XXX: need to suppress polling by better using win32 events */
3726 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3727 ret
|= pe
->func(pe
->opaque
);
3731 WaitObjects
*w
= &wait_objects
;
3733 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3734 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3735 if (w
->func
[ret
- WAIT_OBJECT_0
])
3736 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3738 /* Check for additional signaled events */
3739 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3741 /* Check if event is signaled */
3742 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3743 if(ret2
== WAIT_OBJECT_0
) {
3745 w
->func
[i
](w
->opaque
[i
]);
3746 } else if (ret2
== WAIT_TIMEOUT
) {
3748 err
= GetLastError();
3749 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3752 } else if (ret
== WAIT_TIMEOUT
) {
3754 err
= GetLastError();
3755 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3762 static void host_main_loop_wait(int *timeout
)
3767 void main_loop_wait(int timeout
)
3769 IOHandlerRecord
*ioh
;
3770 fd_set rfds
, wfds
, xfds
;
3774 qemu_bh_update_timeout(&timeout
);
3776 host_main_loop_wait(&timeout
);
3778 /* poll any events */
3779 /* XXX: separate device handlers from system ones */
3784 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3788 (!ioh
->fd_read_poll
||
3789 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3790 FD_SET(ioh
->fd
, &rfds
);
3794 if (ioh
->fd_write
) {
3795 FD_SET(ioh
->fd
, &wfds
);
3801 tv
.tv_sec
= timeout
/ 1000;
3802 tv
.tv_usec
= (timeout
% 1000) * 1000;
3804 #if defined(CONFIG_SLIRP)
3805 if (slirp_is_inited()) {
3806 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3809 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3811 IOHandlerRecord
**pioh
;
3813 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3814 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3815 ioh
->fd_read(ioh
->opaque
);
3817 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3818 ioh
->fd_write(ioh
->opaque
);
3822 /* remove deleted IO handlers */
3823 pioh
= &first_io_handler
;
3833 #if defined(CONFIG_SLIRP)
3834 if (slirp_is_inited()) {
3840 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3844 /* vm time timers */
3845 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3846 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3847 qemu_get_clock(vm_clock
));
3849 /* real time timers */
3850 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3851 qemu_get_clock(rt_clock
));
3853 /* Check bottom-halves last in case any of the earlier events triggered
3859 static int main_loop(void)
3862 #ifdef CONFIG_PROFILER
3867 cur_cpu
= first_cpu
;
3868 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3875 #ifdef CONFIG_PROFILER
3876 ti
= profile_getclock();
3881 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3882 env
->icount_decr
.u16
.low
= 0;
3883 env
->icount_extra
= 0;
3884 count
= qemu_next_deadline();
3885 count
= (count
+ (1 << icount_time_shift
) - 1)
3886 >> icount_time_shift
;
3887 qemu_icount
+= count
;
3888 decr
= (count
> 0xffff) ? 0xffff : count
;
3890 env
->icount_decr
.u16
.low
= decr
;
3891 env
->icount_extra
= count
;
3893 ret
= cpu_exec(env
);
3894 #ifdef CONFIG_PROFILER
3895 qemu_time
+= profile_getclock() - ti
;
3898 /* Fold pending instructions back into the
3899 instruction counter, and clear the interrupt flag. */
3900 qemu_icount
-= (env
->icount_decr
.u16
.low
3901 + env
->icount_extra
);
3902 env
->icount_decr
.u32
= 0;
3903 env
->icount_extra
= 0;
3905 next_cpu
= env
->next_cpu
?: first_cpu
;
3906 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3907 ret
= EXCP_INTERRUPT
;
3911 if (ret
== EXCP_HLT
) {
3912 /* Give the next CPU a chance to run. */
3916 if (ret
!= EXCP_HALTED
)
3918 /* all CPUs are halted ? */
3924 if (shutdown_requested
) {
3925 ret
= EXCP_INTERRUPT
;
3933 if (reset_requested
) {
3934 reset_requested
= 0;
3935 qemu_system_reset();
3936 ret
= EXCP_INTERRUPT
;
3938 if (powerdown_requested
) {
3939 powerdown_requested
= 0;
3940 qemu_system_powerdown();
3941 ret
= EXCP_INTERRUPT
;
3943 if (unlikely(ret
== EXCP_DEBUG
)) {
3944 gdb_set_stop_cpu(cur_cpu
);
3945 vm_stop(EXCP_DEBUG
);
3947 /* If all cpus are halted then wait until the next IRQ */
3948 /* XXX: use timeout computed from timers */
3949 if (ret
== EXCP_HALTED
) {
3953 /* Advance virtual time to the next event. */
3954 if (use_icount
== 1) {
3955 /* When not using an adaptive execution frequency
3956 we tend to get badly out of sync with real time,
3957 so just delay for a reasonable amount of time. */
3960 delta
= cpu_get_icount() - cpu_get_clock();
3963 /* If virtual time is ahead of real time then just
3965 timeout
= (delta
/ 1000000) + 1;
3967 /* Wait for either IO to occur or the next
3969 add
= qemu_next_deadline();
3970 /* We advance the timer before checking for IO.
3971 Limit the amount we advance so that early IO
3972 activity won't get the guest too far ahead. */
3976 add
= (add
+ (1 << icount_time_shift
) - 1)
3977 >> icount_time_shift
;
3979 timeout
= delta
/ 1000000;
3990 if (shutdown_requested
) {
3991 ret
= EXCP_INTERRUPT
;
3996 #ifdef CONFIG_PROFILER
3997 ti
= profile_getclock();
3999 main_loop_wait(timeout
);
4000 #ifdef CONFIG_PROFILER
4001 dev_time
+= profile_getclock() - ti
;
4004 cpu_disable_ticks();
4008 static void version(void)
4010 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4013 static void help(int exitcode
)
4016 printf("usage: %s [options] [disk_image]\n"
4018 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4020 #define DEF(option, opt_arg, opt_enum, opt_help) \
4022 #define DEFHEADING(text) stringify(text) "\n"
4023 #include "qemu-options.h"
4028 "During emulation, the following keys are useful:\n"
4029 "ctrl-alt-f toggle full screen\n"
4030 "ctrl-alt-n switch to virtual console 'n'\n"
4031 "ctrl-alt toggle mouse and keyboard grab\n"
4033 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4038 DEFAULT_NETWORK_SCRIPT
,
4039 DEFAULT_NETWORK_DOWN_SCRIPT
,
4041 DEFAULT_GDBSTUB_PORT
,
4046 #define HAS_ARG 0x0001
4049 #define DEF(option, opt_arg, opt_enum, opt_help) \
4051 #define DEFHEADING(text)
4052 #include "qemu-options.h"
4058 typedef struct QEMUOption
{
4064 static const QEMUOption qemu_options
[] = {
4065 { "h", 0, QEMU_OPTION_h
},
4066 #define DEF(option, opt_arg, opt_enum, opt_help) \
4067 { option, opt_arg, opt_enum },
4068 #define DEFHEADING(text)
4069 #include "qemu-options.h"
4077 struct soundhw soundhw
[] = {
4078 #ifdef HAS_AUDIO_CHOICE
4079 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4085 { .init_isa
= pcspk_audio_init
}
4092 "Creative Sound Blaster 16",
4095 { .init_isa
= SB16_init
}
4099 #ifdef CONFIG_CS4231A
4105 { .init_isa
= cs4231a_init
}
4113 "Yamaha YMF262 (OPL3)",
4115 "Yamaha YM3812 (OPL2)",
4119 { .init_isa
= Adlib_init
}
4126 "Gravis Ultrasound GF1",
4129 { .init_isa
= GUS_init
}
4136 "Intel 82801AA AC97 Audio",
4139 { .init_pci
= ac97_init
}
4143 #ifdef CONFIG_ES1370
4146 "ENSONIQ AudioPCI ES1370",
4149 { .init_pci
= es1370_init
}
4153 #endif /* HAS_AUDIO_CHOICE */
4155 { NULL
, NULL
, 0, 0, { NULL
} }
4158 static void select_soundhw (const char *optarg
)
4162 if (*optarg
== '?') {
4165 printf ("Valid sound card names (comma separated):\n");
4166 for (c
= soundhw
; c
->name
; ++c
) {
4167 printf ("%-11s %s\n", c
->name
, c
->descr
);
4169 printf ("\n-soundhw all will enable all of the above\n");
4170 exit (*optarg
!= '?');
4178 if (!strcmp (optarg
, "all")) {
4179 for (c
= soundhw
; c
->name
; ++c
) {
4187 e
= strchr (p
, ',');
4188 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4190 for (c
= soundhw
; c
->name
; ++c
) {
4191 if (!strncmp (c
->name
, p
, l
)) {
4200 "Unknown sound card name (too big to show)\n");
4203 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4208 p
+= l
+ (e
!= NULL
);
4212 goto show_valid_cards
;
4217 static void select_vgahw (const char *p
)
4221 if (strstart(p
, "std", &opts
)) {
4222 std_vga_enabled
= 1;
4223 cirrus_vga_enabled
= 0;
4225 } else if (strstart(p
, "cirrus", &opts
)) {
4226 cirrus_vga_enabled
= 1;
4227 std_vga_enabled
= 0;
4229 } else if (strstart(p
, "vmware", &opts
)) {
4230 cirrus_vga_enabled
= 0;
4231 std_vga_enabled
= 0;
4233 } else if (strstart(p
, "none", &opts
)) {
4234 cirrus_vga_enabled
= 0;
4235 std_vga_enabled
= 0;
4239 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4243 const char *nextopt
;
4245 if (strstart(opts
, ",retrace=", &nextopt
)) {
4247 if (strstart(opts
, "dumb", &nextopt
))
4248 vga_retrace_method
= VGA_RETRACE_DUMB
;
4249 else if (strstart(opts
, "precise", &nextopt
))
4250 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4251 else goto invalid_vga
;
4252 } else goto invalid_vga
;
4258 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4260 exit(STATUS_CONTROL_C_EXIT
);
4265 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4269 if(strlen(str
) != 36)
4272 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4273 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4274 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4280 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4286 #define MAX_NET_CLIENTS 32
4290 static void termsig_handler(int signal
)
4292 qemu_system_shutdown_request();
4295 static void termsig_setup(void)
4297 struct sigaction act
;
4299 memset(&act
, 0, sizeof(act
));
4300 act
.sa_handler
= termsig_handler
;
4301 sigaction(SIGINT
, &act
, NULL
);
4302 sigaction(SIGHUP
, &act
, NULL
);
4303 sigaction(SIGTERM
, &act
, NULL
);
4308 int main(int argc
, char **argv
, char **envp
)
4310 #ifdef CONFIG_GDBSTUB
4311 const char *gdbstub_dev
= NULL
;
4313 uint32_t boot_devices_bitmap
= 0;
4315 int snapshot
, linux_boot
, net_boot
;
4316 const char *initrd_filename
;
4317 const char *kernel_filename
, *kernel_cmdline
;
4318 const char *boot_devices
= "";
4320 DisplayChangeListener
*dcl
;
4321 int cyls
, heads
, secs
, translation
;
4322 const char *net_clients
[MAX_NET_CLIENTS
];
4324 const char *bt_opts
[MAX_BT_CMDLINE
];
4328 const char *r
, *optarg
;
4329 CharDriverState
*monitor_hd
= NULL
;
4330 const char *monitor_device
;
4331 const char *serial_devices
[MAX_SERIAL_PORTS
];
4332 int serial_device_index
;
4333 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4334 int parallel_device_index
;
4335 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4336 int virtio_console_index
;
4337 const char *loadvm
= NULL
;
4338 QEMUMachine
*machine
;
4339 const char *cpu_model
;
4340 const char *usb_devices
[MAX_USB_CMDLINE
];
4341 int usb_devices_index
;
4346 const char *pid_file
= NULL
;
4347 const char *incoming
= NULL
;
4350 struct passwd
*pwd
= NULL
;
4351 const char *chroot_dir
= NULL
;
4352 const char *run_as
= NULL
;
4356 qemu_cache_utils_init(envp
);
4358 LIST_INIT (&vm_change_state_head
);
4361 struct sigaction act
;
4362 sigfillset(&act
.sa_mask
);
4364 act
.sa_handler
= SIG_IGN
;
4365 sigaction(SIGPIPE
, &act
, NULL
);
4368 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4369 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4370 QEMU to run on a single CPU */
4375 h
= GetCurrentProcess();
4376 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4377 for(i
= 0; i
< 32; i
++) {
4378 if (mask
& (1 << i
))
4383 SetProcessAffinityMask(h
, mask
);
4389 register_machines();
4390 machine
= first_machine
;
4392 initrd_filename
= NULL
;
4394 vga_ram_size
= VGA_RAM_SIZE
;
4398 kernel_filename
= NULL
;
4399 kernel_cmdline
= "";
4400 cyls
= heads
= secs
= 0;
4401 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4402 monitor_device
= "vc:80Cx24C";
4404 serial_devices
[0] = "vc:80Cx24C";
4405 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4406 serial_devices
[i
] = NULL
;
4407 serial_device_index
= 0;
4409 parallel_devices
[0] = "vc:80Cx24C";
4410 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4411 parallel_devices
[i
] = NULL
;
4412 parallel_device_index
= 0;
4414 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4415 virtio_consoles
[i
] = NULL
;
4416 virtio_console_index
= 0;
4418 for (i
= 0; i
< MAX_NODES
; i
++) {
4420 node_cpumask
[i
] = 0;
4423 usb_devices_index
= 0;
4443 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4445 const QEMUOption
*popt
;
4448 /* Treat --foo the same as -foo. */
4451 popt
= qemu_options
;
4454 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4458 if (!strcmp(popt
->name
, r
+ 1))
4462 if (popt
->flags
& HAS_ARG
) {
4463 if (optind
>= argc
) {
4464 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4468 optarg
= argv
[optind
++];
4473 switch(popt
->index
) {
4475 machine
= find_machine(optarg
);
4478 printf("Supported machines are:\n");
4479 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4480 printf("%-10s %s%s\n",
4482 m
== first_machine
? " (default)" : "");
4484 exit(*optarg
!= '?');
4487 case QEMU_OPTION_cpu
:
4488 /* hw initialization will check this */
4489 if (*optarg
== '?') {
4490 /* XXX: implement xxx_cpu_list for targets that still miss it */
4491 #if defined(cpu_list)
4492 cpu_list(stdout
, &fprintf
);
4499 case QEMU_OPTION_initrd
:
4500 initrd_filename
= optarg
;
4502 case QEMU_OPTION_hda
:
4504 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4506 hda_index
= drive_add(optarg
, HD_ALIAS
4507 ",cyls=%d,heads=%d,secs=%d%s",
4508 0, cyls
, heads
, secs
,
4509 translation
== BIOS_ATA_TRANSLATION_LBA
?
4511 translation
== BIOS_ATA_TRANSLATION_NONE
?
4512 ",trans=none" : "");
4514 case QEMU_OPTION_hdb
:
4515 case QEMU_OPTION_hdc
:
4516 case QEMU_OPTION_hdd
:
4517 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4519 case QEMU_OPTION_drive
:
4520 drive_add(NULL
, "%s", optarg
);
4522 case QEMU_OPTION_mtdblock
:
4523 drive_add(optarg
, MTD_ALIAS
);
4525 case QEMU_OPTION_sd
:
4526 drive_add(optarg
, SD_ALIAS
);
4528 case QEMU_OPTION_pflash
:
4529 drive_add(optarg
, PFLASH_ALIAS
);
4531 case QEMU_OPTION_snapshot
:
4534 case QEMU_OPTION_hdachs
:
4538 cyls
= strtol(p
, (char **)&p
, 0);
4539 if (cyls
< 1 || cyls
> 16383)
4544 heads
= strtol(p
, (char **)&p
, 0);
4545 if (heads
< 1 || heads
> 16)
4550 secs
= strtol(p
, (char **)&p
, 0);
4551 if (secs
< 1 || secs
> 63)
4555 if (!strcmp(p
, "none"))
4556 translation
= BIOS_ATA_TRANSLATION_NONE
;
4557 else if (!strcmp(p
, "lba"))
4558 translation
= BIOS_ATA_TRANSLATION_LBA
;
4559 else if (!strcmp(p
, "auto"))
4560 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4563 } else if (*p
!= '\0') {
4565 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4568 if (hda_index
!= -1)
4569 snprintf(drives_opt
[hda_index
].opt
,
4570 sizeof(drives_opt
[hda_index
].opt
),
4571 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4572 0, cyls
, heads
, secs
,
4573 translation
== BIOS_ATA_TRANSLATION_LBA
?
4575 translation
== BIOS_ATA_TRANSLATION_NONE
?
4576 ",trans=none" : "");
4579 case QEMU_OPTION_numa
:
4580 if (nb_numa_nodes
>= MAX_NODES
) {
4581 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4586 case QEMU_OPTION_nographic
:
4589 #ifdef CONFIG_CURSES
4590 case QEMU_OPTION_curses
:
4594 case QEMU_OPTION_portrait
:
4597 case QEMU_OPTION_kernel
:
4598 kernel_filename
= optarg
;
4600 case QEMU_OPTION_append
:
4601 kernel_cmdline
= optarg
;
4603 case QEMU_OPTION_cdrom
:
4604 drive_add(optarg
, CDROM_ALIAS
);
4606 case QEMU_OPTION_boot
:
4607 boot_devices
= optarg
;
4608 /* We just do some generic consistency checks */
4610 /* Could easily be extended to 64 devices if needed */
4613 boot_devices_bitmap
= 0;
4614 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4615 /* Allowed boot devices are:
4616 * a b : floppy disk drives
4617 * c ... f : IDE disk drives
4618 * g ... m : machine implementation dependant drives
4619 * n ... p : network devices
4620 * It's up to each machine implementation to check
4621 * if the given boot devices match the actual hardware
4622 * implementation and firmware features.
4624 if (*p
< 'a' || *p
> 'q') {
4625 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4628 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4630 "Boot device '%c' was given twice\n",*p
);
4633 boot_devices_bitmap
|= 1 << (*p
- 'a');
4637 case QEMU_OPTION_fda
:
4638 case QEMU_OPTION_fdb
:
4639 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4642 case QEMU_OPTION_no_fd_bootchk
:
4646 case QEMU_OPTION_net
:
4647 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4648 fprintf(stderr
, "qemu: too many network clients\n");
4651 net_clients
[nb_net_clients
] = optarg
;
4655 case QEMU_OPTION_tftp
:
4656 tftp_prefix
= optarg
;
4658 case QEMU_OPTION_bootp
:
4659 bootp_filename
= optarg
;
4662 case QEMU_OPTION_smb
:
4663 net_slirp_smb(optarg
);
4666 case QEMU_OPTION_redir
:
4667 net_slirp_redir(NULL
, optarg
);
4670 case QEMU_OPTION_bt
:
4671 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4672 fprintf(stderr
, "qemu: too many bluetooth options\n");
4675 bt_opts
[nb_bt_opts
++] = optarg
;
4678 case QEMU_OPTION_audio_help
:
4682 case QEMU_OPTION_soundhw
:
4683 select_soundhw (optarg
);
4689 case QEMU_OPTION_version
:
4693 case QEMU_OPTION_m
: {
4697 value
= strtoul(optarg
, &ptr
, 10);
4699 case 0: case 'M': case 'm':
4706 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4710 /* On 32-bit hosts, QEMU is limited by virtual address space */
4711 if (value
> (2047 << 20)
4712 #ifndef CONFIG_KQEMU
4713 && HOST_LONG_BITS
== 32
4716 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4719 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4720 fprintf(stderr
, "qemu: ram size too large\n");
4729 const CPULogItem
*item
;
4731 mask
= cpu_str_to_log_mask(optarg
);
4733 printf("Log items (comma separated):\n");
4734 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4735 printf("%-10s %s\n", item
->name
, item
->help
);
4742 #ifdef CONFIG_GDBSTUB
4744 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4746 case QEMU_OPTION_gdb
:
4747 gdbstub_dev
= optarg
;
4753 case QEMU_OPTION_bios
:
4756 case QEMU_OPTION_singlestep
:
4764 keyboard_layout
= optarg
;
4767 case QEMU_OPTION_localtime
:
4770 case QEMU_OPTION_vga
:
4771 select_vgahw (optarg
);
4773 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4779 w
= strtol(p
, (char **)&p
, 10);
4782 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4788 h
= strtol(p
, (char **)&p
, 10);
4793 depth
= strtol(p
, (char **)&p
, 10);
4794 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4795 depth
!= 24 && depth
!= 32)
4797 } else if (*p
== '\0') {
4798 depth
= graphic_depth
;
4805 graphic_depth
= depth
;
4809 case QEMU_OPTION_echr
:
4812 term_escape_char
= strtol(optarg
, &r
, 0);
4814 printf("Bad argument to echr\n");
4817 case QEMU_OPTION_monitor
:
4818 monitor_device
= optarg
;
4820 case QEMU_OPTION_serial
:
4821 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4822 fprintf(stderr
, "qemu: too many serial ports\n");
4825 serial_devices
[serial_device_index
] = optarg
;
4826 serial_device_index
++;
4828 case QEMU_OPTION_virtiocon
:
4829 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
4830 fprintf(stderr
, "qemu: too many virtio consoles\n");
4833 virtio_consoles
[virtio_console_index
] = optarg
;
4834 virtio_console_index
++;
4836 case QEMU_OPTION_parallel
:
4837 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4838 fprintf(stderr
, "qemu: too many parallel ports\n");
4841 parallel_devices
[parallel_device_index
] = optarg
;
4842 parallel_device_index
++;
4844 case QEMU_OPTION_loadvm
:
4847 case QEMU_OPTION_full_screen
:
4851 case QEMU_OPTION_no_frame
:
4854 case QEMU_OPTION_alt_grab
:
4857 case QEMU_OPTION_no_quit
:
4860 case QEMU_OPTION_sdl
:
4864 case QEMU_OPTION_pidfile
:
4868 case QEMU_OPTION_win2k_hack
:
4869 win2k_install_hack
= 1;
4871 case QEMU_OPTION_rtc_td_hack
:
4874 case QEMU_OPTION_acpitable
:
4875 if(acpi_table_add(optarg
) < 0) {
4876 fprintf(stderr
, "Wrong acpi table provided\n");
4880 case QEMU_OPTION_smbios
:
4881 if(smbios_entry_add(optarg
) < 0) {
4882 fprintf(stderr
, "Wrong smbios provided\n");
4888 case QEMU_OPTION_no_kqemu
:
4891 case QEMU_OPTION_kernel_kqemu
:
4896 case QEMU_OPTION_enable_kvm
:
4903 case QEMU_OPTION_usb
:
4906 case QEMU_OPTION_usbdevice
:
4908 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
4909 fprintf(stderr
, "Too many USB devices\n");
4912 usb_devices
[usb_devices_index
] = optarg
;
4913 usb_devices_index
++;
4915 case QEMU_OPTION_smp
:
4916 smp_cpus
= atoi(optarg
);
4918 fprintf(stderr
, "Invalid number of CPUs\n");
4922 case QEMU_OPTION_vnc
:
4923 vnc_display
= optarg
;
4926 case QEMU_OPTION_no_acpi
:
4929 case QEMU_OPTION_no_hpet
:
4933 case QEMU_OPTION_no_reboot
:
4936 case QEMU_OPTION_no_shutdown
:
4939 case QEMU_OPTION_show_cursor
:
4942 case QEMU_OPTION_uuid
:
4943 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4944 fprintf(stderr
, "Fail to parse UUID string."
4945 " Wrong format.\n");
4950 case QEMU_OPTION_daemonize
:
4954 case QEMU_OPTION_option_rom
:
4955 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4956 fprintf(stderr
, "Too many option ROMs\n");
4959 option_rom
[nb_option_roms
] = optarg
;
4962 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4963 case QEMU_OPTION_semihosting
:
4964 semihosting_enabled
= 1;
4967 case QEMU_OPTION_name
:
4970 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4971 case QEMU_OPTION_prom_env
:
4972 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4973 fprintf(stderr
, "Too many prom variables\n");
4976 prom_envs
[nb_prom_envs
] = optarg
;
4981 case QEMU_OPTION_old_param
:
4985 case QEMU_OPTION_clock
:
4986 configure_alarms(optarg
);
4988 case QEMU_OPTION_startdate
:
4991 time_t rtc_start_date
;
4992 if (!strcmp(optarg
, "now")) {
4993 rtc_date_offset
= -1;
4995 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5003 } else if (sscanf(optarg
, "%d-%d-%d",
5006 &tm
.tm_mday
) == 3) {
5015 rtc_start_date
= mktimegm(&tm
);
5016 if (rtc_start_date
== -1) {
5018 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5019 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5022 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5026 case QEMU_OPTION_tb_size
:
5027 tb_size
= strtol(optarg
, NULL
, 0);
5031 case QEMU_OPTION_icount
:
5033 if (strcmp(optarg
, "auto") == 0) {
5034 icount_time_shift
= -1;
5036 icount_time_shift
= strtol(optarg
, NULL
, 0);
5039 case QEMU_OPTION_incoming
:
5043 case QEMU_OPTION_chroot
:
5044 chroot_dir
= optarg
;
5046 case QEMU_OPTION_runas
:
5051 case QEMU_OPTION_xen_domid
:
5052 xen_domid
= atoi(optarg
);
5054 case QEMU_OPTION_xen_create
:
5055 xen_mode
= XEN_CREATE
;
5057 case QEMU_OPTION_xen_attach
:
5058 xen_mode
= XEN_ATTACH
;
5065 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5066 if (kvm_allowed
&& kqemu_allowed
) {
5068 "You can not enable both KVM and kqemu at the same time\n");
5073 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5074 if (smp_cpus
> machine
->max_cpus
) {
5075 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5076 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5082 if (serial_device_index
== 0)
5083 serial_devices
[0] = "stdio";
5084 if (parallel_device_index
== 0)
5085 parallel_devices
[0] = "null";
5086 if (strncmp(monitor_device
, "vc", 2) == 0)
5087 monitor_device
= "stdio";
5094 if (pipe(fds
) == -1)
5105 len
= read(fds
[0], &status
, 1);
5106 if (len
== -1 && (errno
== EINTR
))
5111 else if (status
== 1) {
5112 fprintf(stderr
, "Could not acquire pidfile\n");
5129 signal(SIGTSTP
, SIG_IGN
);
5130 signal(SIGTTOU
, SIG_IGN
);
5131 signal(SIGTTIN
, SIG_IGN
);
5134 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5137 write(fds
[1], &status
, 1);
5139 fprintf(stderr
, "Could not acquire pid file\n");
5148 linux_boot
= (kernel_filename
!= NULL
);
5149 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5151 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5152 fprintf(stderr
, "-append only allowed with -kernel option\n");
5156 if (!linux_boot
&& initrd_filename
!= NULL
) {
5157 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5161 /* boot to floppy or the default cd if no hard disk defined yet */
5162 if (!boot_devices
[0]) {
5163 boot_devices
= "cad";
5165 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5168 if (init_timer_alarm() < 0) {
5169 fprintf(stderr
, "could not initialize alarm timer\n");
5172 if (use_icount
&& icount_time_shift
< 0) {
5174 /* 125MIPS seems a reasonable initial guess at the guest speed.
5175 It will be corrected fairly quickly anyway. */
5176 icount_time_shift
= 3;
5177 init_icount_adjust();
5184 /* init network clients */
5185 if (nb_net_clients
== 0) {
5186 /* if no clients, we use a default config */
5187 net_clients
[nb_net_clients
++] = "nic";
5189 net_clients
[nb_net_clients
++] = "user";
5193 for(i
= 0;i
< nb_net_clients
; i
++) {
5194 if (net_client_parse(net_clients
[i
]) < 0)
5200 /* XXX: this should be moved in the PC machine instantiation code */
5201 if (net_boot
!= 0) {
5203 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5204 const char *model
= nd_table
[i
].model
;
5206 if (net_boot
& (1 << i
)) {
5209 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5210 if (get_image_size(buf
) > 0) {
5211 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5212 fprintf(stderr
, "Too many option ROMs\n");
5215 option_rom
[nb_option_roms
] = strdup(buf
);
5222 fprintf(stderr
, "No valid PXE rom found for network device\n");
5228 /* init the bluetooth world */
5229 for (i
= 0; i
< nb_bt_opts
; i
++)
5230 if (bt_parse(bt_opts
[i
]))
5233 /* init the memory */
5235 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5238 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5239 guest ram allocation. It needs to go away. */
5240 if (kqemu_allowed
) {
5241 kqemu_phys_ram_size
= ram_size
+ VGA_RAM_SIZE
+ 4 * 1024 * 1024;
5242 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5243 if (!kqemu_phys_ram_base
) {
5244 fprintf(stderr
, "Could not allocate physical memory\n");
5250 /* init the dynamic translator */
5251 cpu_exec_init_all(tb_size
* 1024 * 1024);
5256 /* we always create the cdrom drive, even if no disk is there */
5258 if (nb_drives_opt
< MAX_DRIVES
)
5259 drive_add(NULL
, CDROM_ALIAS
);
5261 /* we always create at least one floppy */
5263 if (nb_drives_opt
< MAX_DRIVES
)
5264 drive_add(NULL
, FD_ALIAS
, 0);
5266 /* we always create one sd slot, even if no card is in it */
5268 if (nb_drives_opt
< MAX_DRIVES
)
5269 drive_add(NULL
, SD_ALIAS
);
5271 /* open the virtual block devices */
5273 for(i
= 0; i
< nb_drives_opt
; i
++)
5274 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5277 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5278 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5281 /* must be after terminal init, SDL library changes signal handlers */
5285 /* Maintain compatibility with multiple stdio monitors */
5286 if (!strcmp(monitor_device
,"stdio")) {
5287 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5288 const char *devname
= serial_devices
[i
];
5289 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5290 monitor_device
= NULL
;
5292 } else if (devname
&& !strcmp(devname
,"stdio")) {
5293 monitor_device
= NULL
;
5294 serial_devices
[i
] = "mon:stdio";
5300 if (nb_numa_nodes
> 0) {
5303 if (nb_numa_nodes
> smp_cpus
) {
5304 nb_numa_nodes
= smp_cpus
;
5307 /* If no memory size if given for any node, assume the default case
5308 * and distribute the available memory equally across all nodes
5310 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5311 if (node_mem
[i
] != 0)
5314 if (i
== nb_numa_nodes
) {
5315 uint64_t usedmem
= 0;
5317 /* On Linux, the each node's border has to be 8MB aligned,
5318 * the final node gets the rest.
5320 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5321 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5322 usedmem
+= node_mem
[i
];
5324 node_mem
[i
] = ram_size
- usedmem
;
5327 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5328 if (node_cpumask
[i
] != 0)
5331 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5332 * must cope with this anyway, because there are BIOSes out there in
5333 * real machines which also use this scheme.
5335 if (i
== nb_numa_nodes
) {
5336 for (i
= 0; i
< smp_cpus
; i
++) {
5337 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5342 if (kvm_enabled()) {
5345 ret
= kvm_init(smp_cpus
);
5347 fprintf(stderr
, "failed to initialize KVM\n");
5352 if (monitor_device
) {
5353 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5355 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5360 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5361 const char *devname
= serial_devices
[i
];
5362 if (devname
&& strcmp(devname
, "none")) {
5364 snprintf(label
, sizeof(label
), "serial%d", i
);
5365 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5366 if (!serial_hds
[i
]) {
5367 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5374 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5375 const char *devname
= parallel_devices
[i
];
5376 if (devname
&& strcmp(devname
, "none")) {
5378 snprintf(label
, sizeof(label
), "parallel%d", i
);
5379 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5380 if (!parallel_hds
[i
]) {
5381 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5388 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5389 const char *devname
= virtio_consoles
[i
];
5390 if (devname
&& strcmp(devname
, "none")) {
5392 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5393 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5394 if (!virtcon_hds
[i
]) {
5395 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5402 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5403 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5406 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5407 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5408 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5414 current_machine
= machine
;
5416 /* Set KVM's vcpu state to qemu's initial CPUState. */
5417 if (kvm_enabled()) {
5420 ret
= kvm_sync_vcpus();
5422 fprintf(stderr
, "failed to initialize vcpus\n");
5427 /* init USB devices */
5429 for(i
= 0; i
< usb_devices_index
; i
++) {
5430 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5431 fprintf(stderr
, "Warning: could not add USB device %s\n",
5438 dumb_display_init();
5439 /* just use the first displaystate for the moment */
5444 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5448 #if defined(CONFIG_CURSES)
5450 /* At the moment curses cannot be used with other displays */
5451 curses_display_init(ds
, full_screen
);
5455 if (vnc_display
!= NULL
) {
5456 vnc_display_init(ds
);
5457 if (vnc_display_open(ds
, vnc_display
) < 0)
5460 #if defined(CONFIG_SDL)
5461 if (sdl
|| !vnc_display
)
5462 sdl_display_init(ds
, full_screen
, no_frame
);
5463 #elif defined(CONFIG_COCOA)
5464 if (sdl
|| !vnc_display
)
5465 cocoa_display_init(ds
, full_screen
);
5471 dcl
= ds
->listeners
;
5472 while (dcl
!= NULL
) {
5473 if (dcl
->dpy_refresh
!= NULL
) {
5474 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5475 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5480 if (nographic
|| (vnc_display
&& !sdl
)) {
5481 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5482 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5485 text_consoles_set_display(display_state
);
5486 qemu_chr_initial_reset();
5488 if (monitor_device
&& monitor_hd
)
5489 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5491 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5492 const char *devname
= serial_devices
[i
];
5493 if (devname
&& strcmp(devname
, "none")) {
5495 snprintf(label
, sizeof(label
), "serial%d", i
);
5496 if (strstart(devname
, "vc", 0))
5497 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5501 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5502 const char *devname
= parallel_devices
[i
];
5503 if (devname
&& strcmp(devname
, "none")) {
5505 snprintf(label
, sizeof(label
), "parallel%d", i
);
5506 if (strstart(devname
, "vc", 0))
5507 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5511 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5512 const char *devname
= virtio_consoles
[i
];
5513 if (virtcon_hds
[i
] && devname
) {
5515 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5516 if (strstart(devname
, "vc", 0))
5517 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5521 #ifdef CONFIG_GDBSTUB
5522 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5523 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5530 do_loadvm(cur_mon
, loadvm
);
5533 autostart
= 0; /* fixme how to deal with -daemonize */
5534 qemu_start_incoming_migration(incoming
);
5546 len
= write(fds
[1], &status
, 1);
5547 if (len
== -1 && (errno
== EINTR
))
5554 TFR(fd
= open("/dev/null", O_RDWR
));
5560 pwd
= getpwnam(run_as
);
5562 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5568 if (chroot(chroot_dir
) < 0) {
5569 fprintf(stderr
, "chroot failed\n");
5576 if (setgid(pwd
->pw_gid
) < 0) {
5577 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5580 if (setuid(pwd
->pw_uid
) < 0) {
5581 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5584 if (setuid(0) != -1) {
5585 fprintf(stderr
, "Dropping privileges failed\n");