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;
219 int xenfb_enabled
= 0;
221 int graphic_width
= 1024;
222 int graphic_height
= 768;
223 int graphic_depth
= 8;
225 int graphic_width
= 800;
226 int graphic_height
= 600;
227 int graphic_depth
= 15;
229 static int full_screen
= 0;
231 static int no_frame
= 0;
234 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
235 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
236 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
238 int win2k_install_hack
= 0;
244 const char *vnc_display
;
245 int acpi_enabled
= 1;
251 int graphic_rotate
= 0;
255 const char *option_rom
[MAX_OPTION_ROMS
];
257 int semihosting_enabled
= 0;
261 const char *qemu_name
;
263 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
264 unsigned int nb_prom_envs
= 0;
265 const char *prom_envs
[MAX_PROM_ENVS
];
268 struct drive_opt drives_opt
[MAX_DRIVES
];
271 uint64_t node_mem
[MAX_NODES
];
272 uint64_t node_cpumask
[MAX_NODES
];
274 static CPUState
*cur_cpu
;
275 static CPUState
*next_cpu
;
276 static int event_pending
= 1;
277 /* Conversion factor from emulated instructions to virtual clock ticks. */
278 static int icount_time_shift
;
279 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
280 #define MAX_ICOUNT_SHIFT 10
281 /* Compensate for varying guest execution speed. */
282 static int64_t qemu_icount_bias
;
283 static QEMUTimer
*icount_rt_timer
;
284 static QEMUTimer
*icount_vm_timer
;
285 static QEMUTimer
*nographic_timer
;
287 uint8_t qemu_uuid
[16];
289 /***********************************************************/
290 /* x86 ISA bus support */
292 target_phys_addr_t isa_mem_base
= 0;
295 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
296 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
298 static uint32_t ioport_read(int index
, uint32_t address
)
300 static IOPortReadFunc
*default_func
[3] = {
301 default_ioport_readb
,
302 default_ioport_readw
,
305 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
307 func
= default_func
[index
];
308 return func(ioport_opaque
[address
], address
);
311 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
313 static IOPortWriteFunc
*default_func
[3] = {
314 default_ioport_writeb
,
315 default_ioport_writew
,
316 default_ioport_writel
318 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
320 func
= default_func
[index
];
321 func(ioport_opaque
[address
], address
, data
);
324 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
326 #ifdef DEBUG_UNUSED_IOPORT
327 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
332 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
334 #ifdef DEBUG_UNUSED_IOPORT
335 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
339 /* default is to make two byte accesses */
340 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
343 data
= ioport_read(0, address
);
344 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
345 data
|= ioport_read(0, address
) << 8;
349 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
351 ioport_write(0, address
, data
& 0xff);
352 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
353 ioport_write(0, address
, (data
>> 8) & 0xff);
356 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
358 #ifdef DEBUG_UNUSED_IOPORT
359 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
364 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
366 #ifdef DEBUG_UNUSED_IOPORT
367 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
371 /* size is the word size in byte */
372 int register_ioport_read(int start
, int length
, int size
,
373 IOPortReadFunc
*func
, void *opaque
)
379 } else if (size
== 2) {
381 } else if (size
== 4) {
384 hw_error("register_ioport_read: invalid size");
387 for(i
= start
; i
< start
+ length
; i
+= size
) {
388 ioport_read_table
[bsize
][i
] = func
;
389 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
390 hw_error("register_ioport_read: invalid opaque");
391 ioport_opaque
[i
] = opaque
;
396 /* size is the word size in byte */
397 int register_ioport_write(int start
, int length
, int size
,
398 IOPortWriteFunc
*func
, void *opaque
)
404 } else if (size
== 2) {
406 } else if (size
== 4) {
409 hw_error("register_ioport_write: invalid size");
412 for(i
= start
; i
< start
+ length
; i
+= size
) {
413 ioport_write_table
[bsize
][i
] = func
;
414 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
415 hw_error("register_ioport_write: invalid opaque");
416 ioport_opaque
[i
] = opaque
;
421 void isa_unassign_ioport(int start
, int length
)
425 for(i
= start
; i
< start
+ length
; i
++) {
426 ioport_read_table
[0][i
] = default_ioport_readb
;
427 ioport_read_table
[1][i
] = default_ioport_readw
;
428 ioport_read_table
[2][i
] = default_ioport_readl
;
430 ioport_write_table
[0][i
] = default_ioport_writeb
;
431 ioport_write_table
[1][i
] = default_ioport_writew
;
432 ioport_write_table
[2][i
] = default_ioport_writel
;
434 ioport_opaque
[i
] = NULL
;
438 /***********************************************************/
440 void cpu_outb(CPUState
*env
, int addr
, int val
)
442 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
443 ioport_write(0, addr
, val
);
446 env
->last_io_time
= cpu_get_time_fast();
450 void cpu_outw(CPUState
*env
, int addr
, int val
)
452 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
453 ioport_write(1, addr
, val
);
456 env
->last_io_time
= cpu_get_time_fast();
460 void cpu_outl(CPUState
*env
, int addr
, int val
)
462 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
463 ioport_write(2, addr
, val
);
466 env
->last_io_time
= cpu_get_time_fast();
470 int cpu_inb(CPUState
*env
, int addr
)
473 val
= ioport_read(0, addr
);
474 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
477 env
->last_io_time
= cpu_get_time_fast();
482 int cpu_inw(CPUState
*env
, int addr
)
485 val
= ioport_read(1, addr
);
486 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
489 env
->last_io_time
= cpu_get_time_fast();
494 int cpu_inl(CPUState
*env
, int addr
)
497 val
= ioport_read(2, addr
);
498 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
501 env
->last_io_time
= cpu_get_time_fast();
506 /***********************************************************/
507 void hw_error(const char *fmt
, ...)
513 fprintf(stderr
, "qemu: hardware error: ");
514 vfprintf(stderr
, fmt
, ap
);
515 fprintf(stderr
, "\n");
516 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
517 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
519 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
521 cpu_dump_state(env
, stderr
, fprintf
, 0);
531 static QEMUBalloonEvent
*qemu_balloon_event
;
532 void *qemu_balloon_event_opaque
;
534 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
536 qemu_balloon_event
= func
;
537 qemu_balloon_event_opaque
= opaque
;
540 void qemu_balloon(ram_addr_t target
)
542 if (qemu_balloon_event
)
543 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
546 ram_addr_t
qemu_balloon_status(void)
548 if (qemu_balloon_event
)
549 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
553 /***********************************************************/
556 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
557 static void *qemu_put_kbd_event_opaque
;
558 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
559 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
561 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
563 qemu_put_kbd_event_opaque
= opaque
;
564 qemu_put_kbd_event
= func
;
567 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
568 void *opaque
, int absolute
,
571 QEMUPutMouseEntry
*s
, *cursor
;
573 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
575 s
->qemu_put_mouse_event
= func
;
576 s
->qemu_put_mouse_event_opaque
= opaque
;
577 s
->qemu_put_mouse_event_absolute
= absolute
;
578 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
581 if (!qemu_put_mouse_event_head
) {
582 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
586 cursor
= qemu_put_mouse_event_head
;
587 while (cursor
->next
!= NULL
)
588 cursor
= cursor
->next
;
591 qemu_put_mouse_event_current
= s
;
596 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
598 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
600 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
603 cursor
= qemu_put_mouse_event_head
;
604 while (cursor
!= NULL
&& cursor
!= entry
) {
606 cursor
= cursor
->next
;
609 if (cursor
== NULL
) // does not exist or list empty
611 else if (prev
== NULL
) { // entry is head
612 qemu_put_mouse_event_head
= cursor
->next
;
613 if (qemu_put_mouse_event_current
== entry
)
614 qemu_put_mouse_event_current
= cursor
->next
;
615 qemu_free(entry
->qemu_put_mouse_event_name
);
620 prev
->next
= entry
->next
;
622 if (qemu_put_mouse_event_current
== entry
)
623 qemu_put_mouse_event_current
= prev
;
625 qemu_free(entry
->qemu_put_mouse_event_name
);
629 void kbd_put_keycode(int keycode
)
631 if (qemu_put_kbd_event
) {
632 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
636 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
638 QEMUPutMouseEvent
*mouse_event
;
639 void *mouse_event_opaque
;
642 if (!qemu_put_mouse_event_current
) {
647 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
649 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
652 if (graphic_rotate
) {
653 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
656 width
= graphic_width
- 1;
657 mouse_event(mouse_event_opaque
,
658 width
- dy
, dx
, dz
, buttons_state
);
660 mouse_event(mouse_event_opaque
,
661 dx
, dy
, dz
, buttons_state
);
665 int kbd_mouse_is_absolute(void)
667 if (!qemu_put_mouse_event_current
)
670 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
673 void do_info_mice(Monitor
*mon
)
675 QEMUPutMouseEntry
*cursor
;
678 if (!qemu_put_mouse_event_head
) {
679 monitor_printf(mon
, "No mouse devices connected\n");
683 monitor_printf(mon
, "Mouse devices available:\n");
684 cursor
= qemu_put_mouse_event_head
;
685 while (cursor
!= NULL
) {
686 monitor_printf(mon
, "%c Mouse #%d: %s\n",
687 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
688 index
, cursor
->qemu_put_mouse_event_name
);
690 cursor
= cursor
->next
;
694 void do_mouse_set(Monitor
*mon
, int index
)
696 QEMUPutMouseEntry
*cursor
;
699 if (!qemu_put_mouse_event_head
) {
700 monitor_printf(mon
, "No mouse devices connected\n");
704 cursor
= qemu_put_mouse_event_head
;
705 while (cursor
!= NULL
&& index
!= i
) {
707 cursor
= cursor
->next
;
711 qemu_put_mouse_event_current
= cursor
;
713 monitor_printf(mon
, "Mouse at given index not found\n");
716 /* compute with 96 bit intermediate result: (a*b)/c */
717 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
722 #ifdef WORDS_BIGENDIAN
732 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
733 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
736 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
740 /***********************************************************/
741 /* real time host monotonic timer */
743 #define QEMU_TIMER_BASE 1000000000LL
747 static int64_t clock_freq
;
749 static void init_get_clock(void)
753 ret
= QueryPerformanceFrequency(&freq
);
755 fprintf(stderr
, "Could not calibrate ticks\n");
758 clock_freq
= freq
.QuadPart
;
761 static int64_t get_clock(void)
764 QueryPerformanceCounter(&ti
);
765 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
770 static int use_rt_clock
;
772 static void init_get_clock(void)
775 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
776 || defined(__DragonFly__)
779 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
786 static int64_t get_clock(void)
788 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
789 || defined(__DragonFly__)
792 clock_gettime(CLOCK_MONOTONIC
, &ts
);
793 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
797 /* XXX: using gettimeofday leads to problems if the date
798 changes, so it should be avoided. */
800 gettimeofday(&tv
, NULL
);
801 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
806 /* Return the virtual CPU time, based on the instruction counter. */
807 static int64_t cpu_get_icount(void)
810 CPUState
*env
= cpu_single_env
;;
811 icount
= qemu_icount
;
814 fprintf(stderr
, "Bad clock read\n");
815 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
817 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
820 /***********************************************************/
821 /* guest cycle counter */
823 static int64_t cpu_ticks_prev
;
824 static int64_t cpu_ticks_offset
;
825 static int64_t cpu_clock_offset
;
826 static int cpu_ticks_enabled
;
828 /* return the host CPU cycle counter and handle stop/restart */
829 int64_t cpu_get_ticks(void)
832 return cpu_get_icount();
834 if (!cpu_ticks_enabled
) {
835 return cpu_ticks_offset
;
838 ticks
= cpu_get_real_ticks();
839 if (cpu_ticks_prev
> ticks
) {
840 /* Note: non increasing ticks may happen if the host uses
842 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
844 cpu_ticks_prev
= ticks
;
845 return ticks
+ cpu_ticks_offset
;
849 /* return the host CPU monotonic timer and handle stop/restart */
850 static int64_t cpu_get_clock(void)
853 if (!cpu_ticks_enabled
) {
854 return cpu_clock_offset
;
857 return ti
+ cpu_clock_offset
;
861 /* enable cpu_get_ticks() */
862 void cpu_enable_ticks(void)
864 if (!cpu_ticks_enabled
) {
865 cpu_ticks_offset
-= cpu_get_real_ticks();
866 cpu_clock_offset
-= get_clock();
867 cpu_ticks_enabled
= 1;
871 /* disable cpu_get_ticks() : the clock is stopped. You must not call
872 cpu_get_ticks() after that. */
873 void cpu_disable_ticks(void)
875 if (cpu_ticks_enabled
) {
876 cpu_ticks_offset
= cpu_get_ticks();
877 cpu_clock_offset
= cpu_get_clock();
878 cpu_ticks_enabled
= 0;
882 /***********************************************************/
885 #define QEMU_TIMER_REALTIME 0
886 #define QEMU_TIMER_VIRTUAL 1
890 /* XXX: add frequency */
898 struct QEMUTimer
*next
;
901 struct qemu_alarm_timer
{
905 int (*start
)(struct qemu_alarm_timer
*t
);
906 void (*stop
)(struct qemu_alarm_timer
*t
);
907 void (*rearm
)(struct qemu_alarm_timer
*t
);
911 #define ALARM_FLAG_DYNTICKS 0x1
912 #define ALARM_FLAG_EXPIRED 0x2
914 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
916 return t
->flags
& ALARM_FLAG_DYNTICKS
;
919 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
921 if (!alarm_has_dynticks(t
))
927 /* TODO: MIN_TIMER_REARM_US should be optimized */
928 #define MIN_TIMER_REARM_US 250
930 static struct qemu_alarm_timer
*alarm_timer
;
932 static int alarm_timer_rfd
, alarm_timer_wfd
;
937 struct qemu_alarm_win32
{
941 } alarm_win32_data
= {0, NULL
, -1};
943 static int win32_start_timer(struct qemu_alarm_timer
*t
);
944 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
945 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
949 static int unix_start_timer(struct qemu_alarm_timer
*t
);
950 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
954 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
955 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
956 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
958 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
959 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
961 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
962 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
964 #endif /* __linux__ */
968 /* Correlation between real and virtual time is always going to be
969 fairly approximate, so ignore small variation.
970 When the guest is idle real and virtual time will be aligned in
972 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
974 static void icount_adjust(void)
979 static int64_t last_delta
;
980 /* If the VM is not running, then do nothing. */
984 cur_time
= cpu_get_clock();
985 cur_icount
= qemu_get_clock(vm_clock
);
986 delta
= cur_icount
- cur_time
;
987 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
989 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
990 && icount_time_shift
> 0) {
991 /* The guest is getting too far ahead. Slow time down. */
995 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
996 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
997 /* The guest is getting too far behind. Speed time up. */
1001 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1004 static void icount_adjust_rt(void * opaque
)
1006 qemu_mod_timer(icount_rt_timer
,
1007 qemu_get_clock(rt_clock
) + 1000);
1011 static void icount_adjust_vm(void * opaque
)
1013 qemu_mod_timer(icount_vm_timer
,
1014 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1018 static void init_icount_adjust(void)
1020 /* Have both realtime and virtual time triggers for speed adjustment.
1021 The realtime trigger catches emulated time passing too slowly,
1022 the virtual time trigger catches emulated time passing too fast.
1023 Realtime triggers occur even when idle, so use them less frequently
1024 than VM triggers. */
1025 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1026 qemu_mod_timer(icount_rt_timer
,
1027 qemu_get_clock(rt_clock
) + 1000);
1028 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1029 qemu_mod_timer(icount_vm_timer
,
1030 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1033 static struct qemu_alarm_timer alarm_timers
[] = {
1036 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1037 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1038 /* HPET - if available - is preferred */
1039 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1040 /* ...otherwise try RTC */
1041 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1043 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1045 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1046 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1047 {"win32", 0, win32_start_timer
,
1048 win32_stop_timer
, NULL
, &alarm_win32_data
},
1053 static void show_available_alarms(void)
1057 printf("Available alarm timers, in order of precedence:\n");
1058 for (i
= 0; alarm_timers
[i
].name
; i
++)
1059 printf("%s\n", alarm_timers
[i
].name
);
1062 static void configure_alarms(char const *opt
)
1066 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1069 struct qemu_alarm_timer tmp
;
1071 if (!strcmp(opt
, "?")) {
1072 show_available_alarms();
1078 /* Reorder the array */
1079 name
= strtok(arg
, ",");
1081 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1082 if (!strcmp(alarm_timers
[i
].name
, name
))
1087 fprintf(stderr
, "Unknown clock %s\n", name
);
1096 tmp
= alarm_timers
[i
];
1097 alarm_timers
[i
] = alarm_timers
[cur
];
1098 alarm_timers
[cur
] = tmp
;
1102 name
= strtok(NULL
, ",");
1108 /* Disable remaining timers */
1109 for (i
= cur
; i
< count
; i
++)
1110 alarm_timers
[i
].name
= NULL
;
1112 show_available_alarms();
1117 QEMUClock
*rt_clock
;
1118 QEMUClock
*vm_clock
;
1120 static QEMUTimer
*active_timers
[2];
1122 static QEMUClock
*qemu_new_clock(int type
)
1125 clock
= qemu_mallocz(sizeof(QEMUClock
));
1130 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1134 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1137 ts
->opaque
= opaque
;
1141 void qemu_free_timer(QEMUTimer
*ts
)
1146 /* stop a timer, but do not dealloc it */
1147 void qemu_del_timer(QEMUTimer
*ts
)
1151 /* NOTE: this code must be signal safe because
1152 qemu_timer_expired() can be called from a signal. */
1153 pt
= &active_timers
[ts
->clock
->type
];
1166 /* modify the current timer so that it will be fired when current_time
1167 >= expire_time. The corresponding callback will be called. */
1168 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1174 /* add the timer in the sorted list */
1175 /* NOTE: this code must be signal safe because
1176 qemu_timer_expired() can be called from a signal. */
1177 pt
= &active_timers
[ts
->clock
->type
];
1182 if (t
->expire_time
> expire_time
)
1186 ts
->expire_time
= expire_time
;
1190 /* Rearm if necessary */
1191 if (pt
== &active_timers
[ts
->clock
->type
]) {
1192 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1193 qemu_rearm_alarm_timer(alarm_timer
);
1195 /* Interrupt execution to force deadline recalculation. */
1196 if (use_icount
&& cpu_single_env
) {
1197 cpu_exit(cpu_single_env
);
1202 int qemu_timer_pending(QEMUTimer
*ts
)
1205 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1212 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1216 return (timer_head
->expire_time
<= current_time
);
1219 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1225 if (!ts
|| ts
->expire_time
> current_time
)
1227 /* remove timer from the list before calling the callback */
1228 *ptimer_head
= ts
->next
;
1231 /* run the callback (the timer list can be modified) */
1236 int64_t qemu_get_clock(QEMUClock
*clock
)
1238 switch(clock
->type
) {
1239 case QEMU_TIMER_REALTIME
:
1240 return get_clock() / 1000000;
1242 case QEMU_TIMER_VIRTUAL
:
1244 return cpu_get_icount();
1246 return cpu_get_clock();
1251 static void init_timers(void)
1254 ticks_per_sec
= QEMU_TIMER_BASE
;
1255 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1256 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1260 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1262 uint64_t expire_time
;
1264 if (qemu_timer_pending(ts
)) {
1265 expire_time
= ts
->expire_time
;
1269 qemu_put_be64(f
, expire_time
);
1272 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1274 uint64_t expire_time
;
1276 expire_time
= qemu_get_be64(f
);
1277 if (expire_time
!= -1) {
1278 qemu_mod_timer(ts
, expire_time
);
1284 static void timer_save(QEMUFile
*f
, void *opaque
)
1286 if (cpu_ticks_enabled
) {
1287 hw_error("cannot save state if virtual timers are running");
1289 qemu_put_be64(f
, cpu_ticks_offset
);
1290 qemu_put_be64(f
, ticks_per_sec
);
1291 qemu_put_be64(f
, cpu_clock_offset
);
1294 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1296 if (version_id
!= 1 && version_id
!= 2)
1298 if (cpu_ticks_enabled
) {
1301 cpu_ticks_offset
=qemu_get_be64(f
);
1302 ticks_per_sec
=qemu_get_be64(f
);
1303 if (version_id
== 2) {
1304 cpu_clock_offset
=qemu_get_be64(f
);
1310 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1311 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1314 static void host_alarm_handler(int host_signum
)
1318 #define DISP_FREQ 1000
1320 static int64_t delta_min
= INT64_MAX
;
1321 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1323 ti
= qemu_get_clock(vm_clock
);
1324 if (last_clock
!= 0) {
1325 delta
= ti
- last_clock
;
1326 if (delta
< delta_min
)
1328 if (delta
> delta_max
)
1331 if (++count
== DISP_FREQ
) {
1332 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1333 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1334 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1335 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1336 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1338 delta_min
= INT64_MAX
;
1346 if (alarm_has_dynticks(alarm_timer
) ||
1348 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1349 qemu_get_clock(vm_clock
))) ||
1350 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1351 qemu_get_clock(rt_clock
))) {
1352 CPUState
*env
= next_cpu
;
1355 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1356 SetEvent(data
->host_alarm
);
1358 static const char byte
= 0;
1359 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1361 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1364 /* stop the currently executing cpu because a timer occured */
1367 if (env
->kqemu_enabled
) {
1368 kqemu_cpu_interrupt(env
);
1376 static int64_t qemu_next_deadline(void)
1380 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1381 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1382 qemu_get_clock(vm_clock
);
1384 /* To avoid problems with overflow limit this to 2^32. */
1394 #if defined(__linux__) || defined(_WIN32)
1395 static uint64_t qemu_next_deadline_dyntick(void)
1403 delta
= (qemu_next_deadline() + 999) / 1000;
1405 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1406 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1407 qemu_get_clock(rt_clock
))*1000;
1408 if (rtdelta
< delta
)
1412 if (delta
< MIN_TIMER_REARM_US
)
1413 delta
= MIN_TIMER_REARM_US
;
1421 /* Sets a specific flag */
1422 static int fcntl_setfl(int fd
, int flag
)
1426 flags
= fcntl(fd
, F_GETFL
);
1430 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1436 #if defined(__linux__)
1438 #define RTC_FREQ 1024
1440 static void enable_sigio_timer(int fd
)
1442 struct sigaction act
;
1445 sigfillset(&act
.sa_mask
);
1447 act
.sa_handler
= host_alarm_handler
;
1449 sigaction(SIGIO
, &act
, NULL
);
1450 fcntl_setfl(fd
, O_ASYNC
);
1451 fcntl(fd
, F_SETOWN
, getpid());
1454 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1456 struct hpet_info info
;
1459 fd
= open("/dev/hpet", O_RDONLY
);
1464 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1466 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1467 "error, but for better emulation accuracy type:\n"
1468 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1472 /* Check capabilities */
1473 r
= ioctl(fd
, HPET_INFO
, &info
);
1477 /* Enable periodic mode */
1478 r
= ioctl(fd
, HPET_EPI
, 0);
1479 if (info
.hi_flags
&& (r
< 0))
1482 /* Enable interrupt */
1483 r
= ioctl(fd
, HPET_IE_ON
, 0);
1487 enable_sigio_timer(fd
);
1488 t
->priv
= (void *)(long)fd
;
1496 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1498 int fd
= (long)t
->priv
;
1503 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1506 unsigned long current_rtc_freq
= 0;
1508 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1511 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1512 if (current_rtc_freq
!= RTC_FREQ
&&
1513 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1514 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1515 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1516 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1519 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1525 enable_sigio_timer(rtc_fd
);
1527 t
->priv
= (void *)(long)rtc_fd
;
1532 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1534 int rtc_fd
= (long)t
->priv
;
1539 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1543 struct sigaction act
;
1545 sigfillset(&act
.sa_mask
);
1547 act
.sa_handler
= host_alarm_handler
;
1549 sigaction(SIGALRM
, &act
, NULL
);
1551 ev
.sigev_value
.sival_int
= 0;
1552 ev
.sigev_notify
= SIGEV_SIGNAL
;
1553 ev
.sigev_signo
= SIGALRM
;
1555 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1556 perror("timer_create");
1558 /* disable dynticks */
1559 fprintf(stderr
, "Dynamic Ticks disabled\n");
1564 t
->priv
= (void *)(long)host_timer
;
1569 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1571 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1573 timer_delete(host_timer
);
1576 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1578 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1579 struct itimerspec timeout
;
1580 int64_t nearest_delta_us
= INT64_MAX
;
1583 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1584 !active_timers
[QEMU_TIMER_VIRTUAL
])
1587 nearest_delta_us
= qemu_next_deadline_dyntick();
1589 /* check whether a timer is already running */
1590 if (timer_gettime(host_timer
, &timeout
)) {
1592 fprintf(stderr
, "Internal timer error: aborting\n");
1595 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1596 if (current_us
&& current_us
<= nearest_delta_us
)
1599 timeout
.it_interval
.tv_sec
= 0;
1600 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1601 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1602 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1603 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1605 fprintf(stderr
, "Internal timer error: aborting\n");
1610 #endif /* defined(__linux__) */
1612 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1614 struct sigaction act
;
1615 struct itimerval itv
;
1619 sigfillset(&act
.sa_mask
);
1621 act
.sa_handler
= host_alarm_handler
;
1623 sigaction(SIGALRM
, &act
, NULL
);
1625 itv
.it_interval
.tv_sec
= 0;
1626 /* for i386 kernel 2.6 to get 1 ms */
1627 itv
.it_interval
.tv_usec
= 999;
1628 itv
.it_value
.tv_sec
= 0;
1629 itv
.it_value
.tv_usec
= 10 * 1000;
1631 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1638 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1640 struct itimerval itv
;
1642 memset(&itv
, 0, sizeof(itv
));
1643 setitimer(ITIMER_REAL
, &itv
, NULL
);
1646 #endif /* !defined(_WIN32) */
1648 static void try_to_rearm_timer(void *opaque
)
1650 struct qemu_alarm_timer
*t
= opaque
;
1654 /* Drain the notify pipe */
1657 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1658 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1661 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1662 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1663 qemu_rearm_alarm_timer(alarm_timer
);
1669 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1672 struct qemu_alarm_win32
*data
= t
->priv
;
1675 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1676 if (!data
->host_alarm
) {
1677 perror("Failed CreateEvent");
1681 memset(&tc
, 0, sizeof(tc
));
1682 timeGetDevCaps(&tc
, sizeof(tc
));
1684 if (data
->period
< tc
.wPeriodMin
)
1685 data
->period
= tc
.wPeriodMin
;
1687 timeBeginPeriod(data
->period
);
1689 flags
= TIME_CALLBACK_FUNCTION
;
1690 if (alarm_has_dynticks(t
))
1691 flags
|= TIME_ONESHOT
;
1693 flags
|= TIME_PERIODIC
;
1695 data
->timerId
= timeSetEvent(1, // interval (ms)
1696 data
->period
, // resolution
1697 host_alarm_handler
, // function
1698 (DWORD
)t
, // parameter
1701 if (!data
->timerId
) {
1702 perror("Failed to initialize win32 alarm timer");
1704 timeEndPeriod(data
->period
);
1705 CloseHandle(data
->host_alarm
);
1709 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1714 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1716 struct qemu_alarm_win32
*data
= t
->priv
;
1718 timeKillEvent(data
->timerId
);
1719 timeEndPeriod(data
->period
);
1721 CloseHandle(data
->host_alarm
);
1724 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1726 struct qemu_alarm_win32
*data
= t
->priv
;
1727 uint64_t nearest_delta_us
;
1729 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1730 !active_timers
[QEMU_TIMER_VIRTUAL
])
1733 nearest_delta_us
= qemu_next_deadline_dyntick();
1734 nearest_delta_us
/= 1000;
1736 timeKillEvent(data
->timerId
);
1738 data
->timerId
= timeSetEvent(1,
1742 TIME_ONESHOT
| TIME_PERIODIC
);
1744 if (!data
->timerId
) {
1745 perror("Failed to re-arm win32 alarm timer");
1747 timeEndPeriod(data
->period
);
1748 CloseHandle(data
->host_alarm
);
1755 static int init_timer_alarm(void)
1757 struct qemu_alarm_timer
*t
= NULL
;
1767 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1771 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1775 alarm_timer_rfd
= fds
[0];
1776 alarm_timer_wfd
= fds
[1];
1779 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1780 t
= &alarm_timers
[i
];
1793 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1794 try_to_rearm_timer
, NULL
, t
);
1809 static void quit_timers(void)
1811 alarm_timer
->stop(alarm_timer
);
1815 /***********************************************************/
1816 /* host time/date access */
1817 void qemu_get_timedate(struct tm
*tm
, int offset
)
1824 if (rtc_date_offset
== -1) {
1828 ret
= localtime(&ti
);
1830 ti
-= rtc_date_offset
;
1834 memcpy(tm
, ret
, sizeof(struct tm
));
1837 int qemu_timedate_diff(struct tm
*tm
)
1841 if (rtc_date_offset
== -1)
1843 seconds
= mktimegm(tm
);
1845 seconds
= mktime(tm
);
1847 seconds
= mktimegm(tm
) + rtc_date_offset
;
1849 return seconds
- time(NULL
);
1853 static void socket_cleanup(void)
1858 static int socket_init(void)
1863 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1865 err
= WSAGetLastError();
1866 fprintf(stderr
, "WSAStartup: %d\n", err
);
1869 atexit(socket_cleanup
);
1874 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
1879 while (*p
!= '\0' && *p
!= delim
) {
1880 if (q
&& (q
- buf
) < buf_size
- 1)
1890 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1895 while (*p
!= '\0') {
1897 if (*(p
+ 1) != ',')
1901 if (q
&& (q
- buf
) < buf_size
- 1)
1911 int get_param_value(char *buf
, int buf_size
,
1912 const char *tag
, const char *str
)
1919 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1923 if (!strcmp(tag
, option
)) {
1924 (void)get_opt_value(buf
, buf_size
, p
);
1927 p
= get_opt_value(NULL
, 0, p
);
1936 int check_params(char *buf
, int buf_size
,
1937 const char * const *params
, const char *str
)
1943 while (*p
!= '\0') {
1944 p
= get_opt_name(buf
, buf_size
, p
, '=');
1948 for(i
= 0; params
[i
] != NULL
; i
++)
1949 if (!strcmp(params
[i
], buf
))
1951 if (params
[i
] == NULL
)
1953 p
= get_opt_value(NULL
, 0, p
);
1961 /***********************************************************/
1962 /* Bluetooth support */
1965 static struct HCIInfo
*hci_table
[MAX_NICS
];
1967 static struct bt_vlan_s
{
1968 struct bt_scatternet_s net
;
1970 struct bt_vlan_s
*next
;
1973 /* find or alloc a new bluetooth "VLAN" */
1974 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1976 struct bt_vlan_s
**pvlan
, *vlan
;
1977 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1981 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1983 pvlan
= &first_bt_vlan
;
1984 while (*pvlan
!= NULL
)
1985 pvlan
= &(*pvlan
)->next
;
1990 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1994 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1999 static struct HCIInfo null_hci
= {
2000 .cmd_send
= null_hci_send
,
2001 .sco_send
= null_hci_send
,
2002 .acl_send
= null_hci_send
,
2003 .bdaddr_set
= null_hci_addr_set
,
2006 struct HCIInfo
*qemu_next_hci(void)
2008 if (cur_hci
== nb_hcis
)
2011 return hci_table
[cur_hci
++];
2014 static struct HCIInfo
*hci_init(const char *str
)
2017 struct bt_scatternet_s
*vlan
= 0;
2019 if (!strcmp(str
, "null"))
2022 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2024 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2025 else if (!strncmp(str
, "hci", 3)) {
2028 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2029 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2034 vlan
= qemu_find_bt_vlan(0);
2036 return bt_new_hci(vlan
);
2039 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2044 static int bt_hci_parse(const char *str
)
2046 struct HCIInfo
*hci
;
2049 if (nb_hcis
>= MAX_NICS
) {
2050 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2054 hci
= hci_init(str
);
2063 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2064 hci
->bdaddr_set(hci
, bdaddr
.b
);
2066 hci_table
[nb_hcis
++] = hci
;
2071 static void bt_vhci_add(int vlan_id
)
2073 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2076 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2077 "an empty scatternet %i\n", vlan_id
);
2079 bt_vhci_init(bt_new_hci(vlan
));
2082 static struct bt_device_s
*bt_device_add(const char *opt
)
2084 struct bt_scatternet_s
*vlan
;
2086 char *endp
= strstr(opt
, ",vlan=");
2087 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2090 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2093 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2095 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2100 vlan
= qemu_find_bt_vlan(vlan_id
);
2103 fprintf(stderr
, "qemu: warning: adding a slave device to "
2104 "an empty scatternet %i\n", vlan_id
);
2106 if (!strcmp(devname
, "keyboard"))
2107 return bt_keyboard_init(vlan
);
2109 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2113 static int bt_parse(const char *opt
)
2115 const char *endp
, *p
;
2118 if (strstart(opt
, "hci", &endp
)) {
2119 if (!*endp
|| *endp
== ',') {
2121 if (!strstart(endp
, ",vlan=", 0))
2124 return bt_hci_parse(opt
);
2126 } else if (strstart(opt
, "vhci", &endp
)) {
2127 if (!*endp
|| *endp
== ',') {
2129 if (strstart(endp
, ",vlan=", &p
)) {
2130 vlan
= strtol(p
, (char **) &endp
, 0);
2132 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2136 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2145 } else if (strstart(opt
, "device:", &endp
))
2146 return !bt_device_add(endp
);
2148 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2152 /***********************************************************/
2153 /* QEMU Block devices */
2155 #define HD_ALIAS "index=%d,media=disk"
2156 #define CDROM_ALIAS "index=2,media=cdrom"
2157 #define FD_ALIAS "index=%d,if=floppy"
2158 #define PFLASH_ALIAS "if=pflash"
2159 #define MTD_ALIAS "if=mtd"
2160 #define SD_ALIAS "index=0,if=sd"
2162 static int drive_opt_get_free_idx(void)
2166 for (index
= 0; index
< MAX_DRIVES
; index
++)
2167 if (!drives_opt
[index
].used
) {
2168 drives_opt
[index
].used
= 1;
2175 static int drive_get_free_idx(void)
2179 for (index
= 0; index
< MAX_DRIVES
; index
++)
2180 if (!drives_table
[index
].used
) {
2181 drives_table
[index
].used
= 1;
2188 int drive_add(const char *file
, const char *fmt
, ...)
2191 int index
= drive_opt_get_free_idx();
2193 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2194 fprintf(stderr
, "qemu: too many drives\n");
2198 drives_opt
[index
].file
= file
;
2200 vsnprintf(drives_opt
[index
].opt
,
2201 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2208 void drive_remove(int index
)
2210 drives_opt
[index
].used
= 0;
2214 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2218 /* seek interface, bus and unit */
2220 for (index
= 0; index
< MAX_DRIVES
; index
++)
2221 if (drives_table
[index
].type
== type
&&
2222 drives_table
[index
].bus
== bus
&&
2223 drives_table
[index
].unit
== unit
&&
2224 drives_table
[index
].used
)
2230 int drive_get_max_bus(BlockInterfaceType type
)
2236 for (index
= 0; index
< nb_drives
; index
++) {
2237 if(drives_table
[index
].type
== type
&&
2238 drives_table
[index
].bus
> max_bus
)
2239 max_bus
= drives_table
[index
].bus
;
2244 const char *drive_get_serial(BlockDriverState
*bdrv
)
2248 for (index
= 0; index
< nb_drives
; index
++)
2249 if (drives_table
[index
].bdrv
== bdrv
)
2250 return drives_table
[index
].serial
;
2255 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2259 for (index
= 0; index
< nb_drives
; index
++)
2260 if (drives_table
[index
].bdrv
== bdrv
)
2261 return drives_table
[index
].onerror
;
2263 return BLOCK_ERR_STOP_ENOSPC
;
2266 static void bdrv_format_print(void *opaque
, const char *name
)
2268 fprintf(stderr
, " %s", name
);
2271 void drive_uninit(BlockDriverState
*bdrv
)
2275 for (i
= 0; i
< MAX_DRIVES
; i
++)
2276 if (drives_table
[i
].bdrv
== bdrv
) {
2277 drives_table
[i
].bdrv
= NULL
;
2278 drives_table
[i
].used
= 0;
2279 drive_remove(drives_table
[i
].drive_opt_idx
);
2285 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2291 const char *mediastr
= "";
2292 BlockInterfaceType type
;
2293 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2294 int bus_id
, unit_id
;
2295 int cyls
, heads
, secs
, translation
;
2296 BlockDriverState
*bdrv
;
2297 BlockDriver
*drv
= NULL
;
2298 QEMUMachine
*machine
= opaque
;
2302 int bdrv_flags
, onerror
;
2303 int drives_table_idx
;
2304 char *str
= arg
->opt
;
2305 static const char * const params
[] = { "bus", "unit", "if", "index",
2306 "cyls", "heads", "secs", "trans",
2307 "media", "snapshot", "file",
2308 "cache", "format", "serial", "werror",
2311 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2312 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2318 cyls
= heads
= secs
= 0;
2321 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2325 if (machine
->use_scsi
) {
2327 max_devs
= MAX_SCSI_DEVS
;
2328 pstrcpy(devname
, sizeof(devname
), "scsi");
2331 max_devs
= MAX_IDE_DEVS
;
2332 pstrcpy(devname
, sizeof(devname
), "ide");
2336 /* extract parameters */
2338 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2339 bus_id
= strtol(buf
, NULL
, 0);
2341 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2346 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2347 unit_id
= strtol(buf
, NULL
, 0);
2349 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2354 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2355 pstrcpy(devname
, sizeof(devname
), buf
);
2356 if (!strcmp(buf
, "ide")) {
2358 max_devs
= MAX_IDE_DEVS
;
2359 } else if (!strcmp(buf
, "scsi")) {
2361 max_devs
= MAX_SCSI_DEVS
;
2362 } else if (!strcmp(buf
, "floppy")) {
2365 } else if (!strcmp(buf
, "pflash")) {
2368 } else if (!strcmp(buf
, "mtd")) {
2371 } else if (!strcmp(buf
, "sd")) {
2374 } else if (!strcmp(buf
, "virtio")) {
2377 } else if (!strcmp(buf
, "xen")) {
2381 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2386 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2387 index
= strtol(buf
, NULL
, 0);
2389 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2394 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2395 cyls
= strtol(buf
, NULL
, 0);
2398 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2399 heads
= strtol(buf
, NULL
, 0);
2402 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2403 secs
= strtol(buf
, NULL
, 0);
2406 if (cyls
|| heads
|| secs
) {
2407 if (cyls
< 1 || cyls
> 16383) {
2408 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2411 if (heads
< 1 || heads
> 16) {
2412 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2415 if (secs
< 1 || secs
> 63) {
2416 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2421 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2424 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2428 if (!strcmp(buf
, "none"))
2429 translation
= BIOS_ATA_TRANSLATION_NONE
;
2430 else if (!strcmp(buf
, "lba"))
2431 translation
= BIOS_ATA_TRANSLATION_LBA
;
2432 else if (!strcmp(buf
, "auto"))
2433 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2435 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2440 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2441 if (!strcmp(buf
, "disk")) {
2443 } else if (!strcmp(buf
, "cdrom")) {
2444 if (cyls
|| secs
|| heads
) {
2446 "qemu: '%s' invalid physical CHS format\n", str
);
2449 media
= MEDIA_CDROM
;
2451 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2456 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2457 if (!strcmp(buf
, "on"))
2459 else if (!strcmp(buf
, "off"))
2462 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2467 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2468 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2470 else if (!strcmp(buf
, "writethrough"))
2472 else if (!strcmp(buf
, "writeback"))
2475 fprintf(stderr
, "qemu: invalid cache option\n");
2480 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2481 if (strcmp(buf
, "?") == 0) {
2482 fprintf(stderr
, "qemu: Supported formats:");
2483 bdrv_iterate_format(bdrv_format_print
, NULL
);
2484 fprintf(stderr
, "\n");
2487 drv
= bdrv_find_format(buf
);
2489 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2494 if (arg
->file
== NULL
)
2495 get_param_value(file
, sizeof(file
), "file", str
);
2497 pstrcpy(file
, sizeof(file
), arg
->file
);
2499 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2500 memset(serial
, 0, sizeof(serial
));
2502 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2503 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2504 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2505 fprintf(stderr
, "werror is no supported by this format\n");
2508 if (!strcmp(buf
, "ignore"))
2509 onerror
= BLOCK_ERR_IGNORE
;
2510 else if (!strcmp(buf
, "enospc"))
2511 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2512 else if (!strcmp(buf
, "stop"))
2513 onerror
= BLOCK_ERR_STOP_ANY
;
2514 else if (!strcmp(buf
, "report"))
2515 onerror
= BLOCK_ERR_REPORT
;
2517 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2522 /* compute bus and unit according index */
2525 if (bus_id
!= 0 || unit_id
!= -1) {
2527 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2535 unit_id
= index
% max_devs
;
2536 bus_id
= index
/ max_devs
;
2540 /* if user doesn't specify a unit_id,
2541 * try to find the first free
2544 if (unit_id
== -1) {
2546 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2548 if (max_devs
&& unit_id
>= max_devs
) {
2549 unit_id
-= max_devs
;
2557 if (max_devs
&& unit_id
>= max_devs
) {
2558 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2559 str
, unit_id
, max_devs
- 1);
2564 * ignore multiple definitions
2567 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2572 if (type
== IF_IDE
|| type
== IF_SCSI
)
2573 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2575 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2576 devname
, bus_id
, mediastr
, unit_id
);
2578 snprintf(buf
, sizeof(buf
), "%s%s%i",
2579 devname
, mediastr
, unit_id
);
2580 bdrv
= bdrv_new(buf
);
2581 drives_table_idx
= drive_get_free_idx();
2582 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2583 drives_table
[drives_table_idx
].type
= type
;
2584 drives_table
[drives_table_idx
].bus
= bus_id
;
2585 drives_table
[drives_table_idx
].unit
= unit_id
;
2586 drives_table
[drives_table_idx
].onerror
= onerror
;
2587 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2588 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2598 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2599 bdrv_set_translation_hint(bdrv
, translation
);
2603 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2608 /* FIXME: This isn't really a floppy, but it's a reasonable
2611 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2622 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2623 cache
= 2; /* always use write-back with snapshot */
2625 if (cache
== 0) /* no caching */
2626 bdrv_flags
|= BDRV_O_NOCACHE
;
2627 else if (cache
== 2) /* write-back */
2628 bdrv_flags
|= BDRV_O_CACHE_WB
;
2629 else if (cache
== 3) /* not specified */
2630 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2631 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2632 fprintf(stderr
, "qemu: could not open disk image %s\n",
2636 if (bdrv_key_required(bdrv
))
2638 return drives_table_idx
;
2641 static void numa_add(const char *optarg
)
2645 unsigned long long value
, endvalue
;
2648 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2649 if (!strcmp(option
, "node")) {
2650 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2651 nodenr
= nb_numa_nodes
;
2653 nodenr
= strtoull(option
, NULL
, 10);
2656 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2657 node_mem
[nodenr
] = 0;
2659 value
= strtoull(option
, &endptr
, 0);
2661 case 0: case 'M': case 'm':
2668 node_mem
[nodenr
] = value
;
2670 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2671 node_cpumask
[nodenr
] = 0;
2673 value
= strtoull(option
, &endptr
, 10);
2676 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2678 if (*endptr
== '-') {
2679 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2680 if (endvalue
>= 63) {
2683 "only 63 CPUs in NUMA mode supported.\n");
2685 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2690 node_cpumask
[nodenr
] = value
;
2697 /***********************************************************/
2700 static USBPort
*used_usb_ports
;
2701 static USBPort
*free_usb_ports
;
2703 /* ??? Maybe change this to register a hub to keep track of the topology. */
2704 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2705 usb_attachfn attach
)
2707 port
->opaque
= opaque
;
2708 port
->index
= index
;
2709 port
->attach
= attach
;
2710 port
->next
= free_usb_ports
;
2711 free_usb_ports
= port
;
2714 int usb_device_add_dev(USBDevice
*dev
)
2718 /* Find a USB port to add the device to. */
2719 port
= free_usb_ports
;
2723 /* Create a new hub and chain it on. */
2724 free_usb_ports
= NULL
;
2725 port
->next
= used_usb_ports
;
2726 used_usb_ports
= port
;
2728 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2729 usb_attach(port
, hub
);
2730 port
= free_usb_ports
;
2733 free_usb_ports
= port
->next
;
2734 port
->next
= used_usb_ports
;
2735 used_usb_ports
= port
;
2736 usb_attach(port
, dev
);
2740 static void usb_msd_password_cb(void *opaque
, int err
)
2742 USBDevice
*dev
= opaque
;
2745 usb_device_add_dev(dev
);
2747 dev
->handle_destroy(dev
);
2750 static int usb_device_add(const char *devname
, int is_hotplug
)
2755 if (!free_usb_ports
)
2758 if (strstart(devname
, "host:", &p
)) {
2759 dev
= usb_host_device_open(p
);
2760 } else if (!strcmp(devname
, "mouse")) {
2761 dev
= usb_mouse_init();
2762 } else if (!strcmp(devname
, "tablet")) {
2763 dev
= usb_tablet_init();
2764 } else if (!strcmp(devname
, "keyboard")) {
2765 dev
= usb_keyboard_init();
2766 } else if (strstart(devname
, "disk:", &p
)) {
2767 BlockDriverState
*bs
;
2769 dev
= usb_msd_init(p
);
2772 bs
= usb_msd_get_bdrv(dev
);
2773 if (bdrv_key_required(bs
)) {
2776 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2781 } else if (!strcmp(devname
, "wacom-tablet")) {
2782 dev
= usb_wacom_init();
2783 } else if (strstart(devname
, "serial:", &p
)) {
2784 dev
= usb_serial_init(p
);
2785 #ifdef CONFIG_BRLAPI
2786 } else if (!strcmp(devname
, "braille")) {
2787 dev
= usb_baum_init();
2789 } else if (strstart(devname
, "net:", &p
)) {
2792 if (net_client_init("nic", p
) < 0)
2794 nd_table
[nic
].model
= "usb";
2795 dev
= usb_net_init(&nd_table
[nic
]);
2796 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2797 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2798 bt_new_hci(qemu_find_bt_vlan(0)));
2805 return usb_device_add_dev(dev
);
2808 int usb_device_del_addr(int bus_num
, int addr
)
2814 if (!used_usb_ports
)
2820 lastp
= &used_usb_ports
;
2821 port
= used_usb_ports
;
2822 while (port
&& port
->dev
->addr
!= addr
) {
2823 lastp
= &port
->next
;
2831 *lastp
= port
->next
;
2832 usb_attach(port
, NULL
);
2833 dev
->handle_destroy(dev
);
2834 port
->next
= free_usb_ports
;
2835 free_usb_ports
= port
;
2839 static int usb_device_del(const char *devname
)
2844 if (strstart(devname
, "host:", &p
))
2845 return usb_host_device_close(p
);
2847 if (!used_usb_ports
)
2850 p
= strchr(devname
, '.');
2853 bus_num
= strtoul(devname
, NULL
, 0);
2854 addr
= strtoul(p
+ 1, NULL
, 0);
2856 return usb_device_del_addr(bus_num
, addr
);
2859 void do_usb_add(Monitor
*mon
, const char *devname
)
2861 usb_device_add(devname
, 1);
2864 void do_usb_del(Monitor
*mon
, const char *devname
)
2866 usb_device_del(devname
);
2869 void usb_info(Monitor
*mon
)
2873 const char *speed_str
;
2876 monitor_printf(mon
, "USB support not enabled\n");
2880 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2884 switch(dev
->speed
) {
2888 case USB_SPEED_FULL
:
2891 case USB_SPEED_HIGH
:
2898 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2899 0, dev
->addr
, speed_str
, dev
->devname
);
2903 /***********************************************************/
2904 /* PCMCIA/Cardbus */
2906 static struct pcmcia_socket_entry_s
{
2907 struct pcmcia_socket_s
*socket
;
2908 struct pcmcia_socket_entry_s
*next
;
2909 } *pcmcia_sockets
= 0;
2911 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2913 struct pcmcia_socket_entry_s
*entry
;
2915 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2916 entry
->socket
= socket
;
2917 entry
->next
= pcmcia_sockets
;
2918 pcmcia_sockets
= entry
;
2921 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2923 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2925 ptr
= &pcmcia_sockets
;
2926 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2927 if (entry
->socket
== socket
) {
2933 void pcmcia_info(Monitor
*mon
)
2935 struct pcmcia_socket_entry_s
*iter
;
2937 if (!pcmcia_sockets
)
2938 monitor_printf(mon
, "No PCMCIA sockets\n");
2940 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2941 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2942 iter
->socket
->attached
? iter
->socket
->card_string
:
2946 /***********************************************************/
2947 /* register display */
2949 struct DisplayAllocator default_allocator
= {
2950 defaultallocator_create_displaysurface
,
2951 defaultallocator_resize_displaysurface
,
2952 defaultallocator_free_displaysurface
2955 void register_displaystate(DisplayState
*ds
)
2965 DisplayState
*get_displaystate(void)
2967 return display_state
;
2970 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2972 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2973 return ds
->allocator
;
2978 static void dumb_display_init(void)
2980 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2981 ds
->allocator
= &default_allocator
;
2982 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2983 register_displaystate(ds
);
2986 /***********************************************************/
2989 typedef struct IOHandlerRecord
{
2991 IOCanRWHandler
*fd_read_poll
;
2993 IOHandler
*fd_write
;
2996 /* temporary data */
2998 struct IOHandlerRecord
*next
;
3001 static IOHandlerRecord
*first_io_handler
;
3003 /* XXX: fd_read_poll should be suppressed, but an API change is
3004 necessary in the character devices to suppress fd_can_read(). */
3005 int qemu_set_fd_handler2(int fd
,
3006 IOCanRWHandler
*fd_read_poll
,
3008 IOHandler
*fd_write
,
3011 IOHandlerRecord
**pioh
, *ioh
;
3013 if (!fd_read
&& !fd_write
) {
3014 pioh
= &first_io_handler
;
3019 if (ioh
->fd
== fd
) {
3026 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3030 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3031 ioh
->next
= first_io_handler
;
3032 first_io_handler
= ioh
;
3035 ioh
->fd_read_poll
= fd_read_poll
;
3036 ioh
->fd_read
= fd_read
;
3037 ioh
->fd_write
= fd_write
;
3038 ioh
->opaque
= opaque
;
3044 int qemu_set_fd_handler(int fd
,
3046 IOHandler
*fd_write
,
3049 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3053 /***********************************************************/
3054 /* Polling handling */
3056 typedef struct PollingEntry
{
3059 struct PollingEntry
*next
;
3062 static PollingEntry
*first_polling_entry
;
3064 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3066 PollingEntry
**ppe
, *pe
;
3067 pe
= qemu_mallocz(sizeof(PollingEntry
));
3069 pe
->opaque
= opaque
;
3070 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3075 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3077 PollingEntry
**ppe
, *pe
;
3078 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3080 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3088 /***********************************************************/
3089 /* Wait objects support */
3090 typedef struct WaitObjects
{
3092 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3093 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3094 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3097 static WaitObjects wait_objects
= {0};
3099 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3101 WaitObjects
*w
= &wait_objects
;
3103 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3105 w
->events
[w
->num
] = handle
;
3106 w
->func
[w
->num
] = func
;
3107 w
->opaque
[w
->num
] = opaque
;
3112 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3115 WaitObjects
*w
= &wait_objects
;
3118 for (i
= 0; i
< w
->num
; i
++) {
3119 if (w
->events
[i
] == handle
)
3122 w
->events
[i
] = w
->events
[i
+ 1];
3123 w
->func
[i
] = w
->func
[i
+ 1];
3124 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3132 /***********************************************************/
3133 /* ram save/restore */
3135 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3139 v
= qemu_get_byte(f
);
3142 if (qemu_get_buffer(f
, buf
, len
) != len
)
3146 v
= qemu_get_byte(f
);
3147 memset(buf
, v
, len
);
3153 if (qemu_file_has_error(f
))
3159 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3164 if (qemu_get_be32(f
) != last_ram_offset
)
3166 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
3167 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
3174 #define BDRV_HASH_BLOCK_SIZE 1024
3175 #define IOBUF_SIZE 4096
3176 #define RAM_CBLOCK_MAGIC 0xfabe
3178 typedef struct RamDecompressState
{
3181 uint8_t buf
[IOBUF_SIZE
];
3182 } RamDecompressState
;
3184 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3187 memset(s
, 0, sizeof(*s
));
3189 ret
= inflateInit(&s
->zstream
);
3195 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3199 s
->zstream
.avail_out
= len
;
3200 s
->zstream
.next_out
= buf
;
3201 while (s
->zstream
.avail_out
> 0) {
3202 if (s
->zstream
.avail_in
== 0) {
3203 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3205 clen
= qemu_get_be16(s
->f
);
3206 if (clen
> IOBUF_SIZE
)
3208 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3209 s
->zstream
.avail_in
= clen
;
3210 s
->zstream
.next_in
= s
->buf
;
3212 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3213 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3220 static void ram_decompress_close(RamDecompressState
*s
)
3222 inflateEnd(&s
->zstream
);
3225 #define RAM_SAVE_FLAG_FULL 0x01
3226 #define RAM_SAVE_FLAG_COMPRESS 0x02
3227 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3228 #define RAM_SAVE_FLAG_PAGE 0x08
3229 #define RAM_SAVE_FLAG_EOS 0x10
3231 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3233 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3234 uint32_t *array
= (uint32_t *)page
;
3237 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3238 if (array
[i
] != val
)
3245 static int ram_save_block(QEMUFile
*f
)
3247 static ram_addr_t current_addr
= 0;
3248 ram_addr_t saved_addr
= current_addr
;
3249 ram_addr_t addr
= 0;
3252 while (addr
< last_ram_offset
) {
3253 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3256 cpu_physical_memory_reset_dirty(current_addr
,
3257 current_addr
+ TARGET_PAGE_SIZE
,
3258 MIGRATION_DIRTY_FLAG
);
3260 p
= qemu_get_ram_ptr(current_addr
);
3262 if (is_dup_page(p
, *p
)) {
3263 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3264 qemu_put_byte(f
, *p
);
3266 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3267 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3273 addr
+= TARGET_PAGE_SIZE
;
3274 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3280 static ram_addr_t ram_save_threshold
= 10;
3282 static ram_addr_t
ram_save_remaining(void)
3285 ram_addr_t count
= 0;
3287 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3288 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3295 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3300 /* Make sure all dirty bits are set */
3301 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3302 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3303 cpu_physical_memory_set_dirty(addr
);
3306 /* Enable dirty memory tracking */
3307 cpu_physical_memory_set_dirty_tracking(1);
3309 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3312 while (!qemu_file_rate_limit(f
)) {
3315 ret
= ram_save_block(f
);
3316 if (ret
== 0) /* no more blocks */
3320 /* try transferring iterative blocks of memory */
3324 /* flush all remaining blocks regardless of rate limiting */
3325 while (ram_save_block(f
) != 0);
3326 cpu_physical_memory_set_dirty_tracking(0);
3329 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3331 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3334 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3336 RamDecompressState s1
, *s
= &s1
;
3340 if (ram_decompress_open(s
, f
) < 0)
3342 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3343 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3344 fprintf(stderr
, "Error while reading ram block header\n");
3348 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3349 BDRV_HASH_BLOCK_SIZE
) < 0) {
3350 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3355 printf("Error block header\n");
3359 ram_decompress_close(s
);
3364 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3369 if (version_id
== 1)
3370 return ram_load_v1(f
, opaque
);
3372 if (version_id
== 2) {
3373 if (qemu_get_be32(f
) != last_ram_offset
)
3375 return ram_load_dead(f
, opaque
);
3378 if (version_id
!= 3)
3382 addr
= qemu_get_be64(f
);
3384 flags
= addr
& ~TARGET_PAGE_MASK
;
3385 addr
&= TARGET_PAGE_MASK
;
3387 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3388 if (addr
!= last_ram_offset
)
3392 if (flags
& RAM_SAVE_FLAG_FULL
) {
3393 if (ram_load_dead(f
, opaque
) < 0)
3397 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3398 uint8_t ch
= qemu_get_byte(f
);
3399 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3400 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3401 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3402 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3407 void qemu_service_io(void)
3409 CPUState
*env
= cpu_single_env
;
3413 if (env
->kqemu_enabled
) {
3414 kqemu_cpu_interrupt(env
);
3420 /***********************************************************/
3421 /* bottom halves (can be seen as timers which expire ASAP) */
3432 static QEMUBH
*first_bh
= NULL
;
3434 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3437 bh
= qemu_mallocz(sizeof(QEMUBH
));
3439 bh
->opaque
= opaque
;
3440 bh
->next
= first_bh
;
3445 int qemu_bh_poll(void)
3451 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3452 if (!bh
->deleted
&& bh
->scheduled
) {
3461 /* remove deleted bhs */
3475 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3483 void qemu_bh_schedule(QEMUBH
*bh
)
3485 CPUState
*env
= cpu_single_env
;
3490 /* stop the currently executing CPU to execute the BH ASAP */
3496 void qemu_bh_cancel(QEMUBH
*bh
)
3501 void qemu_bh_delete(QEMUBH
*bh
)
3507 static void qemu_bh_update_timeout(int *timeout
)
3511 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3512 if (!bh
->deleted
&& bh
->scheduled
) {
3514 /* idle bottom halves will be polled at least
3516 *timeout
= MIN(10, *timeout
);
3518 /* non-idle bottom halves will be executed
3527 /***********************************************************/
3528 /* machine registration */
3530 static QEMUMachine
*first_machine
= NULL
;
3531 QEMUMachine
*current_machine
= NULL
;
3533 int qemu_register_machine(QEMUMachine
*m
)
3536 pm
= &first_machine
;
3544 static QEMUMachine
*find_machine(const char *name
)
3548 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3549 if (!strcmp(m
->name
, name
))
3555 /***********************************************************/
3556 /* main execution loop */
3558 static void gui_update(void *opaque
)
3560 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3561 DisplayState
*ds
= opaque
;
3562 DisplayChangeListener
*dcl
= ds
->listeners
;
3566 while (dcl
!= NULL
) {
3567 if (dcl
->gui_timer_interval
&&
3568 dcl
->gui_timer_interval
< interval
)
3569 interval
= dcl
->gui_timer_interval
;
3572 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3575 static void nographic_update(void *opaque
)
3577 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3579 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3582 struct vm_change_state_entry
{
3583 VMChangeStateHandler
*cb
;
3585 LIST_ENTRY (vm_change_state_entry
) entries
;
3588 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3590 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3593 VMChangeStateEntry
*e
;
3595 e
= qemu_mallocz(sizeof (*e
));
3599 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3603 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3605 LIST_REMOVE (e
, entries
);
3609 static void vm_state_notify(int running
, int reason
)
3611 VMChangeStateEntry
*e
;
3613 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3614 e
->cb(e
->opaque
, running
, reason
);
3623 vm_state_notify(1, 0);
3624 qemu_rearm_alarm_timer(alarm_timer
);
3628 void vm_stop(int reason
)
3631 cpu_disable_ticks();
3633 vm_state_notify(0, reason
);
3637 /* reset/shutdown handler */
3639 typedef struct QEMUResetEntry
{
3640 QEMUResetHandler
*func
;
3642 struct QEMUResetEntry
*next
;
3645 static QEMUResetEntry
*first_reset_entry
;
3646 static int reset_requested
;
3647 static int shutdown_requested
;
3648 static int powerdown_requested
;
3650 int qemu_shutdown_requested(void)
3652 int r
= shutdown_requested
;
3653 shutdown_requested
= 0;
3657 int qemu_reset_requested(void)
3659 int r
= reset_requested
;
3660 reset_requested
= 0;
3664 int qemu_powerdown_requested(void)
3666 int r
= powerdown_requested
;
3667 powerdown_requested
= 0;
3671 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3673 QEMUResetEntry
**pre
, *re
;
3675 pre
= &first_reset_entry
;
3676 while (*pre
!= NULL
)
3677 pre
= &(*pre
)->next
;
3678 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3680 re
->opaque
= opaque
;
3685 void qemu_system_reset(void)
3689 /* reset all devices */
3690 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3691 re
->func(re
->opaque
);
3697 void qemu_system_reset_request(void)
3700 shutdown_requested
= 1;
3702 reset_requested
= 1;
3705 cpu_exit(cpu_single_env
);
3708 void qemu_system_shutdown_request(void)
3710 shutdown_requested
= 1;
3712 cpu_exit(cpu_single_env
);
3715 void qemu_system_powerdown_request(void)
3717 powerdown_requested
= 1;
3719 cpu_exit(cpu_single_env
);
3723 static void host_main_loop_wait(int *timeout
)
3729 /* XXX: need to suppress polling by better using win32 events */
3731 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3732 ret
|= pe
->func(pe
->opaque
);
3736 WaitObjects
*w
= &wait_objects
;
3738 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3739 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3740 if (w
->func
[ret
- WAIT_OBJECT_0
])
3741 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3743 /* Check for additional signaled events */
3744 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3746 /* Check if event is signaled */
3747 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3748 if(ret2
== WAIT_OBJECT_0
) {
3750 w
->func
[i
](w
->opaque
[i
]);
3751 } else if (ret2
== WAIT_TIMEOUT
) {
3753 err
= GetLastError();
3754 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3757 } else if (ret
== WAIT_TIMEOUT
) {
3759 err
= GetLastError();
3760 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3767 static void host_main_loop_wait(int *timeout
)
3772 void main_loop_wait(int timeout
)
3774 IOHandlerRecord
*ioh
;
3775 fd_set rfds
, wfds
, xfds
;
3779 qemu_bh_update_timeout(&timeout
);
3781 host_main_loop_wait(&timeout
);
3783 /* poll any events */
3784 /* XXX: separate device handlers from system ones */
3789 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3793 (!ioh
->fd_read_poll
||
3794 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3795 FD_SET(ioh
->fd
, &rfds
);
3799 if (ioh
->fd_write
) {
3800 FD_SET(ioh
->fd
, &wfds
);
3806 tv
.tv_sec
= timeout
/ 1000;
3807 tv
.tv_usec
= (timeout
% 1000) * 1000;
3809 #if defined(CONFIG_SLIRP)
3810 if (slirp_is_inited()) {
3811 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3814 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3816 IOHandlerRecord
**pioh
;
3818 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3819 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3820 ioh
->fd_read(ioh
->opaque
);
3822 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3823 ioh
->fd_write(ioh
->opaque
);
3827 /* remove deleted IO handlers */
3828 pioh
= &first_io_handler
;
3838 #if defined(CONFIG_SLIRP)
3839 if (slirp_is_inited()) {
3845 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3849 /* vm time timers */
3850 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3851 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3852 qemu_get_clock(vm_clock
));
3854 /* real time timers */
3855 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3856 qemu_get_clock(rt_clock
));
3858 /* Check bottom-halves last in case any of the earlier events triggered
3864 static int main_loop(void)
3867 #ifdef CONFIG_PROFILER
3872 cur_cpu
= first_cpu
;
3873 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3880 #ifdef CONFIG_PROFILER
3881 ti
= profile_getclock();
3886 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3887 env
->icount_decr
.u16
.low
= 0;
3888 env
->icount_extra
= 0;
3889 count
= qemu_next_deadline();
3890 count
= (count
+ (1 << icount_time_shift
) - 1)
3891 >> icount_time_shift
;
3892 qemu_icount
+= count
;
3893 decr
= (count
> 0xffff) ? 0xffff : count
;
3895 env
->icount_decr
.u16
.low
= decr
;
3896 env
->icount_extra
= count
;
3898 ret
= cpu_exec(env
);
3899 #ifdef CONFIG_PROFILER
3900 qemu_time
+= profile_getclock() - ti
;
3903 /* Fold pending instructions back into the
3904 instruction counter, and clear the interrupt flag. */
3905 qemu_icount
-= (env
->icount_decr
.u16
.low
3906 + env
->icount_extra
);
3907 env
->icount_decr
.u32
= 0;
3908 env
->icount_extra
= 0;
3910 next_cpu
= env
->next_cpu
?: first_cpu
;
3911 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3912 ret
= EXCP_INTERRUPT
;
3916 if (ret
== EXCP_HLT
) {
3917 /* Give the next CPU a chance to run. */
3921 if (ret
!= EXCP_HALTED
)
3923 /* all CPUs are halted ? */
3929 if (shutdown_requested
) {
3930 ret
= EXCP_INTERRUPT
;
3938 if (reset_requested
) {
3939 reset_requested
= 0;
3940 qemu_system_reset();
3941 ret
= EXCP_INTERRUPT
;
3943 if (powerdown_requested
) {
3944 powerdown_requested
= 0;
3945 qemu_system_powerdown();
3946 ret
= EXCP_INTERRUPT
;
3948 if (unlikely(ret
== EXCP_DEBUG
)) {
3949 gdb_set_stop_cpu(cur_cpu
);
3950 vm_stop(EXCP_DEBUG
);
3952 /* If all cpus are halted then wait until the next IRQ */
3953 /* XXX: use timeout computed from timers */
3954 if (ret
== EXCP_HALTED
) {
3958 /* Advance virtual time to the next event. */
3959 if (use_icount
== 1) {
3960 /* When not using an adaptive execution frequency
3961 we tend to get badly out of sync with real time,
3962 so just delay for a reasonable amount of time. */
3965 delta
= cpu_get_icount() - cpu_get_clock();
3968 /* If virtual time is ahead of real time then just
3970 timeout
= (delta
/ 1000000) + 1;
3972 /* Wait for either IO to occur or the next
3974 add
= qemu_next_deadline();
3975 /* We advance the timer before checking for IO.
3976 Limit the amount we advance so that early IO
3977 activity won't get the guest too far ahead. */
3981 add
= (add
+ (1 << icount_time_shift
) - 1)
3982 >> icount_time_shift
;
3984 timeout
= delta
/ 1000000;
3995 if (shutdown_requested
) {
3996 ret
= EXCP_INTERRUPT
;
4001 #ifdef CONFIG_PROFILER
4002 ti
= profile_getclock();
4004 main_loop_wait(timeout
);
4005 #ifdef CONFIG_PROFILER
4006 dev_time
+= profile_getclock() - ti
;
4009 cpu_disable_ticks();
4013 static void version(void)
4015 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4018 static void help(int exitcode
)
4021 printf("usage: %s [options] [disk_image]\n"
4023 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4025 #define DEF(option, opt_arg, opt_enum, opt_help) \
4027 #define DEFHEADING(text) stringify(text) "\n"
4028 #include "qemu-options.h"
4033 "During emulation, the following keys are useful:\n"
4034 "ctrl-alt-f toggle full screen\n"
4035 "ctrl-alt-n switch to virtual console 'n'\n"
4036 "ctrl-alt toggle mouse and keyboard grab\n"
4038 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4043 DEFAULT_NETWORK_SCRIPT
,
4044 DEFAULT_NETWORK_DOWN_SCRIPT
,
4046 DEFAULT_GDBSTUB_PORT
,
4051 #define HAS_ARG 0x0001
4054 #define DEF(option, opt_arg, opt_enum, opt_help) \
4056 #define DEFHEADING(text)
4057 #include "qemu-options.h"
4063 typedef struct QEMUOption
{
4069 static const QEMUOption qemu_options
[] = {
4070 { "h", 0, QEMU_OPTION_h
},
4071 #define DEF(option, opt_arg, opt_enum, opt_help) \
4072 { option, opt_arg, opt_enum },
4073 #define DEFHEADING(text)
4074 #include "qemu-options.h"
4082 struct soundhw soundhw
[] = {
4083 #ifdef HAS_AUDIO_CHOICE
4084 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4090 { .init_isa
= pcspk_audio_init
}
4097 "Creative Sound Blaster 16",
4100 { .init_isa
= SB16_init
}
4104 #ifdef CONFIG_CS4231A
4110 { .init_isa
= cs4231a_init
}
4118 "Yamaha YMF262 (OPL3)",
4120 "Yamaha YM3812 (OPL2)",
4124 { .init_isa
= Adlib_init
}
4131 "Gravis Ultrasound GF1",
4134 { .init_isa
= GUS_init
}
4141 "Intel 82801AA AC97 Audio",
4144 { .init_pci
= ac97_init
}
4148 #ifdef CONFIG_ES1370
4151 "ENSONIQ AudioPCI ES1370",
4154 { .init_pci
= es1370_init
}
4158 #endif /* HAS_AUDIO_CHOICE */
4160 { NULL
, NULL
, 0, 0, { NULL
} }
4163 static void select_soundhw (const char *optarg
)
4167 if (*optarg
== '?') {
4170 printf ("Valid sound card names (comma separated):\n");
4171 for (c
= soundhw
; c
->name
; ++c
) {
4172 printf ("%-11s %s\n", c
->name
, c
->descr
);
4174 printf ("\n-soundhw all will enable all of the above\n");
4175 exit (*optarg
!= '?');
4183 if (!strcmp (optarg
, "all")) {
4184 for (c
= soundhw
; c
->name
; ++c
) {
4192 e
= strchr (p
, ',');
4193 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4195 for (c
= soundhw
; c
->name
; ++c
) {
4196 if (!strncmp (c
->name
, p
, l
)) {
4205 "Unknown sound card name (too big to show)\n");
4208 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4213 p
+= l
+ (e
!= NULL
);
4217 goto show_valid_cards
;
4222 static void select_vgahw (const char *p
)
4226 cirrus_vga_enabled
= 0;
4227 std_vga_enabled
= 0;
4230 if (strstart(p
, "std", &opts
)) {
4231 std_vga_enabled
= 1;
4232 } else if (strstart(p
, "cirrus", &opts
)) {
4233 cirrus_vga_enabled
= 1;
4234 } else if (strstart(p
, "vmware", &opts
)) {
4236 } else if (strstart(p
, "xenfb", &opts
)) {
4238 } else if (!strstart(p
, "none", &opts
)) {
4240 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4244 const char *nextopt
;
4246 if (strstart(opts
, ",retrace=", &nextopt
)) {
4248 if (strstart(opts
, "dumb", &nextopt
))
4249 vga_retrace_method
= VGA_RETRACE_DUMB
;
4250 else if (strstart(opts
, "precise", &nextopt
))
4251 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4252 else goto invalid_vga
;
4253 } else goto invalid_vga
;
4259 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4261 exit(STATUS_CONTROL_C_EXIT
);
4266 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4270 if(strlen(str
) != 36)
4273 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4274 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4275 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4281 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4287 #define MAX_NET_CLIENTS 32
4291 static void termsig_handler(int signal
)
4293 qemu_system_shutdown_request();
4296 static void termsig_setup(void)
4298 struct sigaction act
;
4300 memset(&act
, 0, sizeof(act
));
4301 act
.sa_handler
= termsig_handler
;
4302 sigaction(SIGINT
, &act
, NULL
);
4303 sigaction(SIGHUP
, &act
, NULL
);
4304 sigaction(SIGTERM
, &act
, NULL
);
4309 int main(int argc
, char **argv
, char **envp
)
4311 #ifdef CONFIG_GDBSTUB
4312 const char *gdbstub_dev
= NULL
;
4314 uint32_t boot_devices_bitmap
= 0;
4316 int snapshot
, linux_boot
, net_boot
;
4317 const char *initrd_filename
;
4318 const char *kernel_filename
, *kernel_cmdline
;
4319 const char *boot_devices
= "";
4321 DisplayChangeListener
*dcl
;
4322 int cyls
, heads
, secs
, translation
;
4323 const char *net_clients
[MAX_NET_CLIENTS
];
4325 const char *bt_opts
[MAX_BT_CMDLINE
];
4329 const char *r
, *optarg
;
4330 CharDriverState
*monitor_hd
= NULL
;
4331 const char *monitor_device
;
4332 const char *serial_devices
[MAX_SERIAL_PORTS
];
4333 int serial_device_index
;
4334 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4335 int parallel_device_index
;
4336 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4337 int virtio_console_index
;
4338 const char *loadvm
= NULL
;
4339 QEMUMachine
*machine
;
4340 const char *cpu_model
;
4341 const char *usb_devices
[MAX_USB_CMDLINE
];
4342 int usb_devices_index
;
4347 const char *pid_file
= NULL
;
4348 const char *incoming
= NULL
;
4351 struct passwd
*pwd
= NULL
;
4352 const char *chroot_dir
= NULL
;
4353 const char *run_as
= NULL
;
4357 qemu_cache_utils_init(envp
);
4359 LIST_INIT (&vm_change_state_head
);
4362 struct sigaction act
;
4363 sigfillset(&act
.sa_mask
);
4365 act
.sa_handler
= SIG_IGN
;
4366 sigaction(SIGPIPE
, &act
, NULL
);
4369 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4370 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4371 QEMU to run on a single CPU */
4376 h
= GetCurrentProcess();
4377 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4378 for(i
= 0; i
< 32; i
++) {
4379 if (mask
& (1 << i
))
4384 SetProcessAffinityMask(h
, mask
);
4390 register_machines();
4391 machine
= first_machine
;
4393 initrd_filename
= NULL
;
4395 vga_ram_size
= VGA_RAM_SIZE
;
4399 kernel_filename
= NULL
;
4400 kernel_cmdline
= "";
4401 cyls
= heads
= secs
= 0;
4402 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4403 monitor_device
= "vc:80Cx24C";
4405 serial_devices
[0] = "vc:80Cx24C";
4406 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4407 serial_devices
[i
] = NULL
;
4408 serial_device_index
= 0;
4410 parallel_devices
[0] = "vc:80Cx24C";
4411 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4412 parallel_devices
[i
] = NULL
;
4413 parallel_device_index
= 0;
4415 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4416 virtio_consoles
[i
] = NULL
;
4417 virtio_console_index
= 0;
4419 for (i
= 0; i
< MAX_NODES
; i
++) {
4421 node_cpumask
[i
] = 0;
4424 usb_devices_index
= 0;
4444 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4446 const QEMUOption
*popt
;
4449 /* Treat --foo the same as -foo. */
4452 popt
= qemu_options
;
4455 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4459 if (!strcmp(popt
->name
, r
+ 1))
4463 if (popt
->flags
& HAS_ARG
) {
4464 if (optind
>= argc
) {
4465 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4469 optarg
= argv
[optind
++];
4474 switch(popt
->index
) {
4476 machine
= find_machine(optarg
);
4479 printf("Supported machines are:\n");
4480 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4481 printf("%-10s %s%s\n",
4483 m
== first_machine
? " (default)" : "");
4485 exit(*optarg
!= '?');
4488 case QEMU_OPTION_cpu
:
4489 /* hw initialization will check this */
4490 if (*optarg
== '?') {
4491 /* XXX: implement xxx_cpu_list for targets that still miss it */
4492 #if defined(cpu_list)
4493 cpu_list(stdout
, &fprintf
);
4500 case QEMU_OPTION_initrd
:
4501 initrd_filename
= optarg
;
4503 case QEMU_OPTION_hda
:
4505 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4507 hda_index
= drive_add(optarg
, HD_ALIAS
4508 ",cyls=%d,heads=%d,secs=%d%s",
4509 0, cyls
, heads
, secs
,
4510 translation
== BIOS_ATA_TRANSLATION_LBA
?
4512 translation
== BIOS_ATA_TRANSLATION_NONE
?
4513 ",trans=none" : "");
4515 case QEMU_OPTION_hdb
:
4516 case QEMU_OPTION_hdc
:
4517 case QEMU_OPTION_hdd
:
4518 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4520 case QEMU_OPTION_drive
:
4521 drive_add(NULL
, "%s", optarg
);
4523 case QEMU_OPTION_mtdblock
:
4524 drive_add(optarg
, MTD_ALIAS
);
4526 case QEMU_OPTION_sd
:
4527 drive_add(optarg
, SD_ALIAS
);
4529 case QEMU_OPTION_pflash
:
4530 drive_add(optarg
, PFLASH_ALIAS
);
4532 case QEMU_OPTION_snapshot
:
4535 case QEMU_OPTION_hdachs
:
4539 cyls
= strtol(p
, (char **)&p
, 0);
4540 if (cyls
< 1 || cyls
> 16383)
4545 heads
= strtol(p
, (char **)&p
, 0);
4546 if (heads
< 1 || heads
> 16)
4551 secs
= strtol(p
, (char **)&p
, 0);
4552 if (secs
< 1 || secs
> 63)
4556 if (!strcmp(p
, "none"))
4557 translation
= BIOS_ATA_TRANSLATION_NONE
;
4558 else if (!strcmp(p
, "lba"))
4559 translation
= BIOS_ATA_TRANSLATION_LBA
;
4560 else if (!strcmp(p
, "auto"))
4561 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4564 } else if (*p
!= '\0') {
4566 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4569 if (hda_index
!= -1)
4570 snprintf(drives_opt
[hda_index
].opt
,
4571 sizeof(drives_opt
[hda_index
].opt
),
4572 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4573 0, cyls
, heads
, secs
,
4574 translation
== BIOS_ATA_TRANSLATION_LBA
?
4576 translation
== BIOS_ATA_TRANSLATION_NONE
?
4577 ",trans=none" : "");
4580 case QEMU_OPTION_numa
:
4581 if (nb_numa_nodes
>= MAX_NODES
) {
4582 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4587 case QEMU_OPTION_nographic
:
4590 #ifdef CONFIG_CURSES
4591 case QEMU_OPTION_curses
:
4595 case QEMU_OPTION_portrait
:
4598 case QEMU_OPTION_kernel
:
4599 kernel_filename
= optarg
;
4601 case QEMU_OPTION_append
:
4602 kernel_cmdline
= optarg
;
4604 case QEMU_OPTION_cdrom
:
4605 drive_add(optarg
, CDROM_ALIAS
);
4607 case QEMU_OPTION_boot
:
4608 boot_devices
= optarg
;
4609 /* We just do some generic consistency checks */
4611 /* Could easily be extended to 64 devices if needed */
4614 boot_devices_bitmap
= 0;
4615 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4616 /* Allowed boot devices are:
4617 * a b : floppy disk drives
4618 * c ... f : IDE disk drives
4619 * g ... m : machine implementation dependant drives
4620 * n ... p : network devices
4621 * It's up to each machine implementation to check
4622 * if the given boot devices match the actual hardware
4623 * implementation and firmware features.
4625 if (*p
< 'a' || *p
> 'q') {
4626 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4629 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4631 "Boot device '%c' was given twice\n",*p
);
4634 boot_devices_bitmap
|= 1 << (*p
- 'a');
4638 case QEMU_OPTION_fda
:
4639 case QEMU_OPTION_fdb
:
4640 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4643 case QEMU_OPTION_no_fd_bootchk
:
4647 case QEMU_OPTION_net
:
4648 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4649 fprintf(stderr
, "qemu: too many network clients\n");
4652 net_clients
[nb_net_clients
] = optarg
;
4656 case QEMU_OPTION_tftp
:
4657 tftp_prefix
= optarg
;
4659 case QEMU_OPTION_bootp
:
4660 bootp_filename
= optarg
;
4663 case QEMU_OPTION_smb
:
4664 net_slirp_smb(optarg
);
4667 case QEMU_OPTION_redir
:
4668 net_slirp_redir(NULL
, optarg
);
4671 case QEMU_OPTION_bt
:
4672 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4673 fprintf(stderr
, "qemu: too many bluetooth options\n");
4676 bt_opts
[nb_bt_opts
++] = optarg
;
4679 case QEMU_OPTION_audio_help
:
4683 case QEMU_OPTION_soundhw
:
4684 select_soundhw (optarg
);
4690 case QEMU_OPTION_version
:
4694 case QEMU_OPTION_m
: {
4698 value
= strtoul(optarg
, &ptr
, 10);
4700 case 0: case 'M': case 'm':
4707 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4711 /* On 32-bit hosts, QEMU is limited by virtual address space */
4712 if (value
> (2047 << 20)
4713 #ifndef CONFIG_KQEMU
4714 && HOST_LONG_BITS
== 32
4717 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4720 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4721 fprintf(stderr
, "qemu: ram size too large\n");
4730 const CPULogItem
*item
;
4732 mask
= cpu_str_to_log_mask(optarg
);
4734 printf("Log items (comma separated):\n");
4735 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4736 printf("%-10s %s\n", item
->name
, item
->help
);
4743 #ifdef CONFIG_GDBSTUB
4745 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4747 case QEMU_OPTION_gdb
:
4748 gdbstub_dev
= optarg
;
4754 case QEMU_OPTION_bios
:
4757 case QEMU_OPTION_singlestep
:
4765 keyboard_layout
= optarg
;
4768 case QEMU_OPTION_localtime
:
4771 case QEMU_OPTION_vga
:
4772 select_vgahw (optarg
);
4774 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4780 w
= strtol(p
, (char **)&p
, 10);
4783 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4789 h
= strtol(p
, (char **)&p
, 10);
4794 depth
= strtol(p
, (char **)&p
, 10);
4795 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4796 depth
!= 24 && depth
!= 32)
4798 } else if (*p
== '\0') {
4799 depth
= graphic_depth
;
4806 graphic_depth
= depth
;
4810 case QEMU_OPTION_echr
:
4813 term_escape_char
= strtol(optarg
, &r
, 0);
4815 printf("Bad argument to echr\n");
4818 case QEMU_OPTION_monitor
:
4819 monitor_device
= optarg
;
4821 case QEMU_OPTION_serial
:
4822 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4823 fprintf(stderr
, "qemu: too many serial ports\n");
4826 serial_devices
[serial_device_index
] = optarg
;
4827 serial_device_index
++;
4829 case QEMU_OPTION_virtiocon
:
4830 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
4831 fprintf(stderr
, "qemu: too many virtio consoles\n");
4834 virtio_consoles
[virtio_console_index
] = optarg
;
4835 virtio_console_index
++;
4837 case QEMU_OPTION_parallel
:
4838 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4839 fprintf(stderr
, "qemu: too many parallel ports\n");
4842 parallel_devices
[parallel_device_index
] = optarg
;
4843 parallel_device_index
++;
4845 case QEMU_OPTION_loadvm
:
4848 case QEMU_OPTION_full_screen
:
4852 case QEMU_OPTION_no_frame
:
4855 case QEMU_OPTION_alt_grab
:
4858 case QEMU_OPTION_no_quit
:
4861 case QEMU_OPTION_sdl
:
4865 case QEMU_OPTION_pidfile
:
4869 case QEMU_OPTION_win2k_hack
:
4870 win2k_install_hack
= 1;
4872 case QEMU_OPTION_rtc_td_hack
:
4875 case QEMU_OPTION_acpitable
:
4876 if(acpi_table_add(optarg
) < 0) {
4877 fprintf(stderr
, "Wrong acpi table provided\n");
4881 case QEMU_OPTION_smbios
:
4882 if(smbios_entry_add(optarg
) < 0) {
4883 fprintf(stderr
, "Wrong smbios provided\n");
4889 case QEMU_OPTION_no_kqemu
:
4892 case QEMU_OPTION_kernel_kqemu
:
4897 case QEMU_OPTION_enable_kvm
:
4904 case QEMU_OPTION_usb
:
4907 case QEMU_OPTION_usbdevice
:
4909 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
4910 fprintf(stderr
, "Too many USB devices\n");
4913 usb_devices
[usb_devices_index
] = optarg
;
4914 usb_devices_index
++;
4916 case QEMU_OPTION_smp
:
4917 smp_cpus
= atoi(optarg
);
4919 fprintf(stderr
, "Invalid number of CPUs\n");
4923 case QEMU_OPTION_vnc
:
4924 vnc_display
= optarg
;
4927 case QEMU_OPTION_no_acpi
:
4930 case QEMU_OPTION_no_hpet
:
4934 case QEMU_OPTION_no_reboot
:
4937 case QEMU_OPTION_no_shutdown
:
4940 case QEMU_OPTION_show_cursor
:
4943 case QEMU_OPTION_uuid
:
4944 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4945 fprintf(stderr
, "Fail to parse UUID string."
4946 " Wrong format.\n");
4951 case QEMU_OPTION_daemonize
:
4955 case QEMU_OPTION_option_rom
:
4956 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4957 fprintf(stderr
, "Too many option ROMs\n");
4960 option_rom
[nb_option_roms
] = optarg
;
4963 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4964 case QEMU_OPTION_semihosting
:
4965 semihosting_enabled
= 1;
4968 case QEMU_OPTION_name
:
4971 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4972 case QEMU_OPTION_prom_env
:
4973 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4974 fprintf(stderr
, "Too many prom variables\n");
4977 prom_envs
[nb_prom_envs
] = optarg
;
4982 case QEMU_OPTION_old_param
:
4986 case QEMU_OPTION_clock
:
4987 configure_alarms(optarg
);
4989 case QEMU_OPTION_startdate
:
4992 time_t rtc_start_date
;
4993 if (!strcmp(optarg
, "now")) {
4994 rtc_date_offset
= -1;
4996 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5004 } else if (sscanf(optarg
, "%d-%d-%d",
5007 &tm
.tm_mday
) == 3) {
5016 rtc_start_date
= mktimegm(&tm
);
5017 if (rtc_start_date
== -1) {
5019 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5020 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5023 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5027 case QEMU_OPTION_tb_size
:
5028 tb_size
= strtol(optarg
, NULL
, 0);
5032 case QEMU_OPTION_icount
:
5034 if (strcmp(optarg
, "auto") == 0) {
5035 icount_time_shift
= -1;
5037 icount_time_shift
= strtol(optarg
, NULL
, 0);
5040 case QEMU_OPTION_incoming
:
5044 case QEMU_OPTION_chroot
:
5045 chroot_dir
= optarg
;
5047 case QEMU_OPTION_runas
:
5052 case QEMU_OPTION_xen_domid
:
5053 xen_domid
= atoi(optarg
);
5055 case QEMU_OPTION_xen_create
:
5056 xen_mode
= XEN_CREATE
;
5058 case QEMU_OPTION_xen_attach
:
5059 xen_mode
= XEN_ATTACH
;
5066 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5067 if (kvm_allowed
&& kqemu_allowed
) {
5069 "You can not enable both KVM and kqemu at the same time\n");
5074 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5075 if (smp_cpus
> machine
->max_cpus
) {
5076 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5077 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5083 if (serial_device_index
== 0)
5084 serial_devices
[0] = "stdio";
5085 if (parallel_device_index
== 0)
5086 parallel_devices
[0] = "null";
5087 if (strncmp(monitor_device
, "vc", 2) == 0)
5088 monitor_device
= "stdio";
5095 if (pipe(fds
) == -1)
5106 len
= read(fds
[0], &status
, 1);
5107 if (len
== -1 && (errno
== EINTR
))
5112 else if (status
== 1) {
5113 fprintf(stderr
, "Could not acquire pidfile\n");
5130 signal(SIGTSTP
, SIG_IGN
);
5131 signal(SIGTTOU
, SIG_IGN
);
5132 signal(SIGTTIN
, SIG_IGN
);
5135 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5138 write(fds
[1], &status
, 1);
5140 fprintf(stderr
, "Could not acquire pid file\n");
5149 linux_boot
= (kernel_filename
!= NULL
);
5150 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5152 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5153 fprintf(stderr
, "-append only allowed with -kernel option\n");
5157 if (!linux_boot
&& initrd_filename
!= NULL
) {
5158 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5162 /* boot to floppy or the default cd if no hard disk defined yet */
5163 if (!boot_devices
[0]) {
5164 boot_devices
= "cad";
5166 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5169 if (init_timer_alarm() < 0) {
5170 fprintf(stderr
, "could not initialize alarm timer\n");
5173 if (use_icount
&& icount_time_shift
< 0) {
5175 /* 125MIPS seems a reasonable initial guess at the guest speed.
5176 It will be corrected fairly quickly anyway. */
5177 icount_time_shift
= 3;
5178 init_icount_adjust();
5185 /* init network clients */
5186 if (nb_net_clients
== 0) {
5187 /* if no clients, we use a default config */
5188 net_clients
[nb_net_clients
++] = "nic";
5190 net_clients
[nb_net_clients
++] = "user";
5194 for(i
= 0;i
< nb_net_clients
; i
++) {
5195 if (net_client_parse(net_clients
[i
]) < 0)
5201 /* XXX: this should be moved in the PC machine instantiation code */
5202 if (net_boot
!= 0) {
5204 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5205 const char *model
= nd_table
[i
].model
;
5207 if (net_boot
& (1 << i
)) {
5210 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5211 if (get_image_size(buf
) > 0) {
5212 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5213 fprintf(stderr
, "Too many option ROMs\n");
5216 option_rom
[nb_option_roms
] = strdup(buf
);
5223 fprintf(stderr
, "No valid PXE rom found for network device\n");
5229 /* init the bluetooth world */
5230 for (i
= 0; i
< nb_bt_opts
; i
++)
5231 if (bt_parse(bt_opts
[i
]))
5234 /* init the memory */
5236 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5239 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5240 guest ram allocation. It needs to go away. */
5241 if (kqemu_allowed
) {
5242 kqemu_phys_ram_size
= ram_size
+ VGA_RAM_SIZE
+ 4 * 1024 * 1024;
5243 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5244 if (!kqemu_phys_ram_base
) {
5245 fprintf(stderr
, "Could not allocate physical memory\n");
5251 /* init the dynamic translator */
5252 cpu_exec_init_all(tb_size
* 1024 * 1024);
5257 /* we always create the cdrom drive, even if no disk is there */
5259 if (nb_drives_opt
< MAX_DRIVES
)
5260 drive_add(NULL
, CDROM_ALIAS
);
5262 /* we always create at least one floppy */
5264 if (nb_drives_opt
< MAX_DRIVES
)
5265 drive_add(NULL
, FD_ALIAS
, 0);
5267 /* we always create one sd slot, even if no card is in it */
5269 if (nb_drives_opt
< MAX_DRIVES
)
5270 drive_add(NULL
, SD_ALIAS
);
5272 /* open the virtual block devices */
5274 for(i
= 0; i
< nb_drives_opt
; i
++)
5275 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5278 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5279 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5282 /* must be after terminal init, SDL library changes signal handlers */
5286 /* Maintain compatibility with multiple stdio monitors */
5287 if (!strcmp(monitor_device
,"stdio")) {
5288 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5289 const char *devname
= serial_devices
[i
];
5290 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5291 monitor_device
= NULL
;
5293 } else if (devname
&& !strcmp(devname
,"stdio")) {
5294 monitor_device
= NULL
;
5295 serial_devices
[i
] = "mon:stdio";
5301 if (nb_numa_nodes
> 0) {
5304 if (nb_numa_nodes
> smp_cpus
) {
5305 nb_numa_nodes
= smp_cpus
;
5308 /* If no memory size if given for any node, assume the default case
5309 * and distribute the available memory equally across all nodes
5311 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5312 if (node_mem
[i
] != 0)
5315 if (i
== nb_numa_nodes
) {
5316 uint64_t usedmem
= 0;
5318 /* On Linux, the each node's border has to be 8MB aligned,
5319 * the final node gets the rest.
5321 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5322 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5323 usedmem
+= node_mem
[i
];
5325 node_mem
[i
] = ram_size
- usedmem
;
5328 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5329 if (node_cpumask
[i
] != 0)
5332 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5333 * must cope with this anyway, because there are BIOSes out there in
5334 * real machines which also use this scheme.
5336 if (i
== nb_numa_nodes
) {
5337 for (i
= 0; i
< smp_cpus
; i
++) {
5338 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5343 if (kvm_enabled()) {
5346 ret
= kvm_init(smp_cpus
);
5348 fprintf(stderr
, "failed to initialize KVM\n");
5353 if (monitor_device
) {
5354 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5356 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5361 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5362 const char *devname
= serial_devices
[i
];
5363 if (devname
&& strcmp(devname
, "none")) {
5365 snprintf(label
, sizeof(label
), "serial%d", i
);
5366 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5367 if (!serial_hds
[i
]) {
5368 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5375 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5376 const char *devname
= parallel_devices
[i
];
5377 if (devname
&& strcmp(devname
, "none")) {
5379 snprintf(label
, sizeof(label
), "parallel%d", i
);
5380 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5381 if (!parallel_hds
[i
]) {
5382 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5389 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5390 const char *devname
= virtio_consoles
[i
];
5391 if (devname
&& strcmp(devname
, "none")) {
5393 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5394 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5395 if (!virtcon_hds
[i
]) {
5396 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5403 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5404 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5407 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5408 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5409 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5415 current_machine
= machine
;
5417 /* Set KVM's vcpu state to qemu's initial CPUState. */
5418 if (kvm_enabled()) {
5421 ret
= kvm_sync_vcpus();
5423 fprintf(stderr
, "failed to initialize vcpus\n");
5428 /* init USB devices */
5430 for(i
= 0; i
< usb_devices_index
; i
++) {
5431 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5432 fprintf(stderr
, "Warning: could not add USB device %s\n",
5439 dumb_display_init();
5440 /* just use the first displaystate for the moment */
5445 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5449 #if defined(CONFIG_CURSES)
5451 /* At the moment curses cannot be used with other displays */
5452 curses_display_init(ds
, full_screen
);
5456 if (vnc_display
!= NULL
) {
5457 vnc_display_init(ds
);
5458 if (vnc_display_open(ds
, vnc_display
) < 0)
5461 #if defined(CONFIG_SDL)
5462 if (sdl
|| !vnc_display
)
5463 sdl_display_init(ds
, full_screen
, no_frame
);
5464 #elif defined(CONFIG_COCOA)
5465 if (sdl
|| !vnc_display
)
5466 cocoa_display_init(ds
, full_screen
);
5472 dcl
= ds
->listeners
;
5473 while (dcl
!= NULL
) {
5474 if (dcl
->dpy_refresh
!= NULL
) {
5475 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5476 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5481 if (nographic
|| (vnc_display
&& !sdl
)) {
5482 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5483 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5486 text_consoles_set_display(display_state
);
5487 qemu_chr_initial_reset();
5489 if (monitor_device
&& monitor_hd
)
5490 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5492 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5493 const char *devname
= serial_devices
[i
];
5494 if (devname
&& strcmp(devname
, "none")) {
5496 snprintf(label
, sizeof(label
), "serial%d", i
);
5497 if (strstart(devname
, "vc", 0))
5498 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5502 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5503 const char *devname
= parallel_devices
[i
];
5504 if (devname
&& strcmp(devname
, "none")) {
5506 snprintf(label
, sizeof(label
), "parallel%d", i
);
5507 if (strstart(devname
, "vc", 0))
5508 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5512 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5513 const char *devname
= virtio_consoles
[i
];
5514 if (virtcon_hds
[i
] && devname
) {
5516 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5517 if (strstart(devname
, "vc", 0))
5518 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5522 #ifdef CONFIG_GDBSTUB
5523 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5524 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5531 do_loadvm(cur_mon
, loadvm
);
5534 autostart
= 0; /* fixme how to deal with -daemonize */
5535 qemu_start_incoming_migration(incoming
);
5547 len
= write(fds
[1], &status
, 1);
5548 if (len
== -1 && (errno
== EINTR
))
5555 TFR(fd
= open("/dev/null", O_RDWR
));
5561 pwd
= getpwnam(run_as
);
5563 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5569 if (chroot(chroot_dir
) < 0) {
5570 fprintf(stderr
, "chroot failed\n");
5577 if (setgid(pwd
->pw_gid
) < 0) {
5578 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5581 if (setuid(pwd
->pw_uid
) < 0) {
5582 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5585 if (setuid(0) != -1) {
5586 fprintf(stderr
, "Dropping privileges failed\n");