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"
148 #include "qemu-timer.h"
149 #include "qemu-char.h"
150 #include "cache-utils.h"
153 #include "audio/audio.h"
154 #include "migration.h"
160 #include "exec-all.h"
162 #include "qemu_socket.h"
164 #if defined(CONFIG_SLIRP)
165 #include "libslirp.h"
168 //#define DEBUG_UNUSED_IOPORT
169 //#define DEBUG_IOPORT
171 //#define DEBUG_SLIRP
175 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
177 # define LOG_IOPORT(...) do { } while (0)
180 #define DEFAULT_RAM_SIZE 128
182 /* Max number of USB devices that can be specified on the commandline. */
183 #define MAX_USB_CMDLINE 8
185 /* Max number of bluetooth switches on the commandline. */
186 #define MAX_BT_CMDLINE 10
188 /* XXX: use a two level table to limit memory usage */
189 #define MAX_IOPORTS 65536
191 const char *bios_dir
= CONFIG_QEMU_SHAREDIR
;
192 const char *bios_name
= NULL
;
193 static void *ioport_opaque
[MAX_IOPORTS
];
194 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
195 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
196 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
197 to store the VM snapshots */
198 DriveInfo drives_table
[MAX_DRIVES
+1];
200 static int vga_ram_size
;
201 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
202 static DisplayState
*display_state
;
206 const char* keyboard_layout
= NULL
;
207 int64_t ticks_per_sec
;
210 NICInfo nd_table
[MAX_NICS
];
212 static int autostart
;
213 static int rtc_utc
= 1;
214 static int rtc_date_offset
= -1; /* -1 means no change */
215 int cirrus_vga_enabled
= 1;
216 int std_vga_enabled
= 0;
217 int vmsvga_enabled
= 0;
219 int graphic_width
= 1024;
220 int graphic_height
= 768;
221 int graphic_depth
= 8;
223 int graphic_width
= 800;
224 int graphic_height
= 600;
225 int graphic_depth
= 15;
227 static int full_screen
= 0;
229 static int no_frame
= 0;
232 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
233 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
234 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
236 int win2k_install_hack
= 0;
242 const char *vnc_display
;
243 int acpi_enabled
= 1;
249 int graphic_rotate
= 0;
253 const char *option_rom
[MAX_OPTION_ROMS
];
255 int semihosting_enabled
= 0;
259 const char *qemu_name
;
261 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
262 unsigned int nb_prom_envs
= 0;
263 const char *prom_envs
[MAX_PROM_ENVS
];
266 struct drive_opt drives_opt
[MAX_DRIVES
];
269 uint64_t node_mem
[MAX_NODES
];
270 uint64_t node_cpumask
[MAX_NODES
];
272 static CPUState
*cur_cpu
;
273 static CPUState
*next_cpu
;
274 static int event_pending
= 1;
275 /* Conversion factor from emulated instructions to virtual clock ticks. */
276 static int icount_time_shift
;
277 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
278 #define MAX_ICOUNT_SHIFT 10
279 /* Compensate for varying guest execution speed. */
280 static int64_t qemu_icount_bias
;
281 static QEMUTimer
*icount_rt_timer
;
282 static QEMUTimer
*icount_vm_timer
;
283 static QEMUTimer
*nographic_timer
;
285 uint8_t qemu_uuid
[16];
287 /***********************************************************/
288 /* x86 ISA bus support */
290 target_phys_addr_t isa_mem_base
= 0;
293 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
294 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
296 static uint32_t ioport_read(int index
, uint32_t address
)
298 static IOPortReadFunc
*default_func
[3] = {
299 default_ioport_readb
,
300 default_ioport_readw
,
303 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
305 func
= default_func
[index
];
306 return func(ioport_opaque
[address
], address
);
309 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
311 static IOPortWriteFunc
*default_func
[3] = {
312 default_ioport_writeb
,
313 default_ioport_writew
,
314 default_ioport_writel
316 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
318 func
= default_func
[index
];
319 func(ioport_opaque
[address
], address
, data
);
322 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
324 #ifdef DEBUG_UNUSED_IOPORT
325 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
330 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
332 #ifdef DEBUG_UNUSED_IOPORT
333 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
337 /* default is to make two byte accesses */
338 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
341 data
= ioport_read(0, address
);
342 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
343 data
|= ioport_read(0, address
) << 8;
347 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
349 ioport_write(0, address
, data
& 0xff);
350 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
351 ioport_write(0, address
, (data
>> 8) & 0xff);
354 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
356 #ifdef DEBUG_UNUSED_IOPORT
357 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
362 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
364 #ifdef DEBUG_UNUSED_IOPORT
365 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
369 /* size is the word size in byte */
370 int register_ioport_read(int start
, int length
, int size
,
371 IOPortReadFunc
*func
, void *opaque
)
377 } else if (size
== 2) {
379 } else if (size
== 4) {
382 hw_error("register_ioport_read: invalid size");
385 for(i
= start
; i
< start
+ length
; i
+= size
) {
386 ioport_read_table
[bsize
][i
] = func
;
387 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
388 hw_error("register_ioport_read: invalid opaque");
389 ioport_opaque
[i
] = opaque
;
394 /* size is the word size in byte */
395 int register_ioport_write(int start
, int length
, int size
,
396 IOPortWriteFunc
*func
, void *opaque
)
402 } else if (size
== 2) {
404 } else if (size
== 4) {
407 hw_error("register_ioport_write: invalid size");
410 for(i
= start
; i
< start
+ length
; i
+= size
) {
411 ioport_write_table
[bsize
][i
] = func
;
412 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
413 hw_error("register_ioport_write: invalid opaque");
414 ioport_opaque
[i
] = opaque
;
419 void isa_unassign_ioport(int start
, int length
)
423 for(i
= start
; i
< start
+ length
; i
++) {
424 ioport_read_table
[0][i
] = default_ioport_readb
;
425 ioport_read_table
[1][i
] = default_ioport_readw
;
426 ioport_read_table
[2][i
] = default_ioport_readl
;
428 ioport_write_table
[0][i
] = default_ioport_writeb
;
429 ioport_write_table
[1][i
] = default_ioport_writew
;
430 ioport_write_table
[2][i
] = default_ioport_writel
;
432 ioport_opaque
[i
] = NULL
;
436 /***********************************************************/
438 void cpu_outb(CPUState
*env
, int addr
, int val
)
440 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
441 ioport_write(0, addr
, val
);
444 env
->last_io_time
= cpu_get_time_fast();
448 void cpu_outw(CPUState
*env
, int addr
, int val
)
450 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
451 ioport_write(1, addr
, val
);
454 env
->last_io_time
= cpu_get_time_fast();
458 void cpu_outl(CPUState
*env
, int addr
, int val
)
460 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
461 ioport_write(2, addr
, val
);
464 env
->last_io_time
= cpu_get_time_fast();
468 int cpu_inb(CPUState
*env
, int addr
)
471 val
= ioport_read(0, addr
);
472 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
475 env
->last_io_time
= cpu_get_time_fast();
480 int cpu_inw(CPUState
*env
, int addr
)
483 val
= ioport_read(1, addr
);
484 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
487 env
->last_io_time
= cpu_get_time_fast();
492 int cpu_inl(CPUState
*env
, int addr
)
495 val
= ioport_read(2, addr
);
496 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
499 env
->last_io_time
= cpu_get_time_fast();
504 /***********************************************************/
505 void hw_error(const char *fmt
, ...)
511 fprintf(stderr
, "qemu: hardware error: ");
512 vfprintf(stderr
, fmt
, ap
);
513 fprintf(stderr
, "\n");
514 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
515 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
517 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
519 cpu_dump_state(env
, stderr
, fprintf
, 0);
529 static QEMUBalloonEvent
*qemu_balloon_event
;
530 void *qemu_balloon_event_opaque
;
532 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
534 qemu_balloon_event
= func
;
535 qemu_balloon_event_opaque
= opaque
;
538 void qemu_balloon(ram_addr_t target
)
540 if (qemu_balloon_event
)
541 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
544 ram_addr_t
qemu_balloon_status(void)
546 if (qemu_balloon_event
)
547 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
551 /***********************************************************/
554 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
555 static void *qemu_put_kbd_event_opaque
;
556 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
557 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
559 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
561 qemu_put_kbd_event_opaque
= opaque
;
562 qemu_put_kbd_event
= func
;
565 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
566 void *opaque
, int absolute
,
569 QEMUPutMouseEntry
*s
, *cursor
;
571 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
573 s
->qemu_put_mouse_event
= func
;
574 s
->qemu_put_mouse_event_opaque
= opaque
;
575 s
->qemu_put_mouse_event_absolute
= absolute
;
576 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
579 if (!qemu_put_mouse_event_head
) {
580 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
584 cursor
= qemu_put_mouse_event_head
;
585 while (cursor
->next
!= NULL
)
586 cursor
= cursor
->next
;
589 qemu_put_mouse_event_current
= s
;
594 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
596 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
598 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
601 cursor
= qemu_put_mouse_event_head
;
602 while (cursor
!= NULL
&& cursor
!= entry
) {
604 cursor
= cursor
->next
;
607 if (cursor
== NULL
) // does not exist or list empty
609 else if (prev
== NULL
) { // entry is head
610 qemu_put_mouse_event_head
= cursor
->next
;
611 if (qemu_put_mouse_event_current
== entry
)
612 qemu_put_mouse_event_current
= cursor
->next
;
613 qemu_free(entry
->qemu_put_mouse_event_name
);
618 prev
->next
= entry
->next
;
620 if (qemu_put_mouse_event_current
== entry
)
621 qemu_put_mouse_event_current
= prev
;
623 qemu_free(entry
->qemu_put_mouse_event_name
);
627 void kbd_put_keycode(int keycode
)
629 if (qemu_put_kbd_event
) {
630 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
634 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
636 QEMUPutMouseEvent
*mouse_event
;
637 void *mouse_event_opaque
;
640 if (!qemu_put_mouse_event_current
) {
645 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
647 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
650 if (graphic_rotate
) {
651 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
654 width
= graphic_width
- 1;
655 mouse_event(mouse_event_opaque
,
656 width
- dy
, dx
, dz
, buttons_state
);
658 mouse_event(mouse_event_opaque
,
659 dx
, dy
, dz
, buttons_state
);
663 int kbd_mouse_is_absolute(void)
665 if (!qemu_put_mouse_event_current
)
668 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
671 void do_info_mice(Monitor
*mon
)
673 QEMUPutMouseEntry
*cursor
;
676 if (!qemu_put_mouse_event_head
) {
677 monitor_printf(mon
, "No mouse devices connected\n");
681 monitor_printf(mon
, "Mouse devices available:\n");
682 cursor
= qemu_put_mouse_event_head
;
683 while (cursor
!= NULL
) {
684 monitor_printf(mon
, "%c Mouse #%d: %s\n",
685 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
686 index
, cursor
->qemu_put_mouse_event_name
);
688 cursor
= cursor
->next
;
692 void do_mouse_set(Monitor
*mon
, int index
)
694 QEMUPutMouseEntry
*cursor
;
697 if (!qemu_put_mouse_event_head
) {
698 monitor_printf(mon
, "No mouse devices connected\n");
702 cursor
= qemu_put_mouse_event_head
;
703 while (cursor
!= NULL
&& index
!= i
) {
705 cursor
= cursor
->next
;
709 qemu_put_mouse_event_current
= cursor
;
711 monitor_printf(mon
, "Mouse at given index not found\n");
714 /* compute with 96 bit intermediate result: (a*b)/c */
715 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
720 #ifdef WORDS_BIGENDIAN
730 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
731 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
734 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
738 /***********************************************************/
739 /* real time host monotonic timer */
741 #define QEMU_TIMER_BASE 1000000000LL
745 static int64_t clock_freq
;
747 static void init_get_clock(void)
751 ret
= QueryPerformanceFrequency(&freq
);
753 fprintf(stderr
, "Could not calibrate ticks\n");
756 clock_freq
= freq
.QuadPart
;
759 static int64_t get_clock(void)
762 QueryPerformanceCounter(&ti
);
763 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
768 static int use_rt_clock
;
770 static void init_get_clock(void)
773 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
774 || defined(__DragonFly__)
777 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
784 static int64_t get_clock(void)
786 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
787 || defined(__DragonFly__)
790 clock_gettime(CLOCK_MONOTONIC
, &ts
);
791 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
795 /* XXX: using gettimeofday leads to problems if the date
796 changes, so it should be avoided. */
798 gettimeofday(&tv
, NULL
);
799 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
804 /* Return the virtual CPU time, based on the instruction counter. */
805 static int64_t cpu_get_icount(void)
808 CPUState
*env
= cpu_single_env
;;
809 icount
= qemu_icount
;
812 fprintf(stderr
, "Bad clock read\n");
813 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
815 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
818 /***********************************************************/
819 /* guest cycle counter */
821 static int64_t cpu_ticks_prev
;
822 static int64_t cpu_ticks_offset
;
823 static int64_t cpu_clock_offset
;
824 static int cpu_ticks_enabled
;
826 /* return the host CPU cycle counter and handle stop/restart */
827 int64_t cpu_get_ticks(void)
830 return cpu_get_icount();
832 if (!cpu_ticks_enabled
) {
833 return cpu_ticks_offset
;
836 ticks
= cpu_get_real_ticks();
837 if (cpu_ticks_prev
> ticks
) {
838 /* Note: non increasing ticks may happen if the host uses
840 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
842 cpu_ticks_prev
= ticks
;
843 return ticks
+ cpu_ticks_offset
;
847 /* return the host CPU monotonic timer and handle stop/restart */
848 static int64_t cpu_get_clock(void)
851 if (!cpu_ticks_enabled
) {
852 return cpu_clock_offset
;
855 return ti
+ cpu_clock_offset
;
859 /* enable cpu_get_ticks() */
860 void cpu_enable_ticks(void)
862 if (!cpu_ticks_enabled
) {
863 cpu_ticks_offset
-= cpu_get_real_ticks();
864 cpu_clock_offset
-= get_clock();
865 cpu_ticks_enabled
= 1;
869 /* disable cpu_get_ticks() : the clock is stopped. You must not call
870 cpu_get_ticks() after that. */
871 void cpu_disable_ticks(void)
873 if (cpu_ticks_enabled
) {
874 cpu_ticks_offset
= cpu_get_ticks();
875 cpu_clock_offset
= cpu_get_clock();
876 cpu_ticks_enabled
= 0;
880 /***********************************************************/
883 #define QEMU_TIMER_REALTIME 0
884 #define QEMU_TIMER_VIRTUAL 1
888 /* XXX: add frequency */
896 struct QEMUTimer
*next
;
899 struct qemu_alarm_timer
{
903 int (*start
)(struct qemu_alarm_timer
*t
);
904 void (*stop
)(struct qemu_alarm_timer
*t
);
905 void (*rearm
)(struct qemu_alarm_timer
*t
);
909 #define ALARM_FLAG_DYNTICKS 0x1
910 #define ALARM_FLAG_EXPIRED 0x2
912 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
914 return t
->flags
& ALARM_FLAG_DYNTICKS
;
917 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
919 if (!alarm_has_dynticks(t
))
925 /* TODO: MIN_TIMER_REARM_US should be optimized */
926 #define MIN_TIMER_REARM_US 250
928 static struct qemu_alarm_timer
*alarm_timer
;
930 static int alarm_timer_rfd
, alarm_timer_wfd
;
935 struct qemu_alarm_win32
{
939 } alarm_win32_data
= {0, NULL
, -1};
941 static int win32_start_timer(struct qemu_alarm_timer
*t
);
942 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
943 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
947 static int unix_start_timer(struct qemu_alarm_timer
*t
);
948 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
952 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
953 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
954 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
956 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
957 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
959 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
960 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
962 #endif /* __linux__ */
966 /* Correlation between real and virtual time is always going to be
967 fairly approximate, so ignore small variation.
968 When the guest is idle real and virtual time will be aligned in
970 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
972 static void icount_adjust(void)
977 static int64_t last_delta
;
978 /* If the VM is not running, then do nothing. */
982 cur_time
= cpu_get_clock();
983 cur_icount
= qemu_get_clock(vm_clock
);
984 delta
= cur_icount
- cur_time
;
985 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
987 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
988 && icount_time_shift
> 0) {
989 /* The guest is getting too far ahead. Slow time down. */
993 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
994 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
995 /* The guest is getting too far behind. Speed time up. */
999 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1002 static void icount_adjust_rt(void * opaque
)
1004 qemu_mod_timer(icount_rt_timer
,
1005 qemu_get_clock(rt_clock
) + 1000);
1009 static void icount_adjust_vm(void * opaque
)
1011 qemu_mod_timer(icount_vm_timer
,
1012 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1016 static void init_icount_adjust(void)
1018 /* Have both realtime and virtual time triggers for speed adjustment.
1019 The realtime trigger catches emulated time passing too slowly,
1020 the virtual time trigger catches emulated time passing too fast.
1021 Realtime triggers occur even when idle, so use them less frequently
1022 than VM triggers. */
1023 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1024 qemu_mod_timer(icount_rt_timer
,
1025 qemu_get_clock(rt_clock
) + 1000);
1026 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1027 qemu_mod_timer(icount_vm_timer
,
1028 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1031 static struct qemu_alarm_timer alarm_timers
[] = {
1034 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1035 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1036 /* HPET - if available - is preferred */
1037 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1038 /* ...otherwise try RTC */
1039 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1041 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1043 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1044 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1045 {"win32", 0, win32_start_timer
,
1046 win32_stop_timer
, NULL
, &alarm_win32_data
},
1051 static void show_available_alarms(void)
1055 printf("Available alarm timers, in order of precedence:\n");
1056 for (i
= 0; alarm_timers
[i
].name
; i
++)
1057 printf("%s\n", alarm_timers
[i
].name
);
1060 static void configure_alarms(char const *opt
)
1064 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1067 struct qemu_alarm_timer tmp
;
1069 if (!strcmp(opt
, "?")) {
1070 show_available_alarms();
1076 /* Reorder the array */
1077 name
= strtok(arg
, ",");
1079 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1080 if (!strcmp(alarm_timers
[i
].name
, name
))
1085 fprintf(stderr
, "Unknown clock %s\n", name
);
1094 tmp
= alarm_timers
[i
];
1095 alarm_timers
[i
] = alarm_timers
[cur
];
1096 alarm_timers
[cur
] = tmp
;
1100 name
= strtok(NULL
, ",");
1106 /* Disable remaining timers */
1107 for (i
= cur
; i
< count
; i
++)
1108 alarm_timers
[i
].name
= NULL
;
1110 show_available_alarms();
1115 QEMUClock
*rt_clock
;
1116 QEMUClock
*vm_clock
;
1118 static QEMUTimer
*active_timers
[2];
1120 static QEMUClock
*qemu_new_clock(int type
)
1123 clock
= qemu_mallocz(sizeof(QEMUClock
));
1128 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1132 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1135 ts
->opaque
= opaque
;
1139 void qemu_free_timer(QEMUTimer
*ts
)
1144 /* stop a timer, but do not dealloc it */
1145 void qemu_del_timer(QEMUTimer
*ts
)
1149 /* NOTE: this code must be signal safe because
1150 qemu_timer_expired() can be called from a signal. */
1151 pt
= &active_timers
[ts
->clock
->type
];
1164 /* modify the current timer so that it will be fired when current_time
1165 >= expire_time. The corresponding callback will be called. */
1166 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1172 /* add the timer in the sorted list */
1173 /* NOTE: this code must be signal safe because
1174 qemu_timer_expired() can be called from a signal. */
1175 pt
= &active_timers
[ts
->clock
->type
];
1180 if (t
->expire_time
> expire_time
)
1184 ts
->expire_time
= expire_time
;
1188 /* Rearm if necessary */
1189 if (pt
== &active_timers
[ts
->clock
->type
]) {
1190 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1191 qemu_rearm_alarm_timer(alarm_timer
);
1193 /* Interrupt execution to force deadline recalculation. */
1194 if (use_icount
&& cpu_single_env
) {
1195 cpu_exit(cpu_single_env
);
1200 int qemu_timer_pending(QEMUTimer
*ts
)
1203 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1210 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1214 return (timer_head
->expire_time
<= current_time
);
1217 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1223 if (!ts
|| ts
->expire_time
> current_time
)
1225 /* remove timer from the list before calling the callback */
1226 *ptimer_head
= ts
->next
;
1229 /* run the callback (the timer list can be modified) */
1234 int64_t qemu_get_clock(QEMUClock
*clock
)
1236 switch(clock
->type
) {
1237 case QEMU_TIMER_REALTIME
:
1238 return get_clock() / 1000000;
1240 case QEMU_TIMER_VIRTUAL
:
1242 return cpu_get_icount();
1244 return cpu_get_clock();
1249 static void init_timers(void)
1252 ticks_per_sec
= QEMU_TIMER_BASE
;
1253 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1254 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1258 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1260 uint64_t expire_time
;
1262 if (qemu_timer_pending(ts
)) {
1263 expire_time
= ts
->expire_time
;
1267 qemu_put_be64(f
, expire_time
);
1270 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1272 uint64_t expire_time
;
1274 expire_time
= qemu_get_be64(f
);
1275 if (expire_time
!= -1) {
1276 qemu_mod_timer(ts
, expire_time
);
1282 static void timer_save(QEMUFile
*f
, void *opaque
)
1284 if (cpu_ticks_enabled
) {
1285 hw_error("cannot save state if virtual timers are running");
1287 qemu_put_be64(f
, cpu_ticks_offset
);
1288 qemu_put_be64(f
, ticks_per_sec
);
1289 qemu_put_be64(f
, cpu_clock_offset
);
1292 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1294 if (version_id
!= 1 && version_id
!= 2)
1296 if (cpu_ticks_enabled
) {
1299 cpu_ticks_offset
=qemu_get_be64(f
);
1300 ticks_per_sec
=qemu_get_be64(f
);
1301 if (version_id
== 2) {
1302 cpu_clock_offset
=qemu_get_be64(f
);
1308 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1309 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1312 static void host_alarm_handler(int host_signum
)
1316 #define DISP_FREQ 1000
1318 static int64_t delta_min
= INT64_MAX
;
1319 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1321 ti
= qemu_get_clock(vm_clock
);
1322 if (last_clock
!= 0) {
1323 delta
= ti
- last_clock
;
1324 if (delta
< delta_min
)
1326 if (delta
> delta_max
)
1329 if (++count
== DISP_FREQ
) {
1330 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1331 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1332 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1333 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1334 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1336 delta_min
= INT64_MAX
;
1344 if (alarm_has_dynticks(alarm_timer
) ||
1346 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1347 qemu_get_clock(vm_clock
))) ||
1348 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1349 qemu_get_clock(rt_clock
))) {
1350 CPUState
*env
= next_cpu
;
1353 struct qemu_alarm_win32
*data
= ((struct qemu_alarm_timer
*)dwUser
)->priv
;
1354 SetEvent(data
->host_alarm
);
1356 static const char byte
= 0;
1357 write(alarm_timer_wfd
, &byte
, sizeof(byte
));
1359 alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1362 /* stop the currently executing cpu because a timer occured */
1365 if (env
->kqemu_enabled
) {
1366 kqemu_cpu_interrupt(env
);
1374 static int64_t qemu_next_deadline(void)
1378 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1379 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1380 qemu_get_clock(vm_clock
);
1382 /* To avoid problems with overflow limit this to 2^32. */
1392 #if defined(__linux__) || defined(_WIN32)
1393 static uint64_t qemu_next_deadline_dyntick(void)
1401 delta
= (qemu_next_deadline() + 999) / 1000;
1403 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1404 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1405 qemu_get_clock(rt_clock
))*1000;
1406 if (rtdelta
< delta
)
1410 if (delta
< MIN_TIMER_REARM_US
)
1411 delta
= MIN_TIMER_REARM_US
;
1419 /* Sets a specific flag */
1420 static int fcntl_setfl(int fd
, int flag
)
1424 flags
= fcntl(fd
, F_GETFL
);
1428 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1434 #if defined(__linux__)
1436 #define RTC_FREQ 1024
1438 static void enable_sigio_timer(int fd
)
1440 struct sigaction act
;
1443 sigfillset(&act
.sa_mask
);
1445 act
.sa_handler
= host_alarm_handler
;
1447 sigaction(SIGIO
, &act
, NULL
);
1448 fcntl_setfl(fd
, O_ASYNC
);
1449 fcntl(fd
, F_SETOWN
, getpid());
1452 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1454 struct hpet_info info
;
1457 fd
= open("/dev/hpet", O_RDONLY
);
1462 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1464 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1465 "error, but for better emulation accuracy type:\n"
1466 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1470 /* Check capabilities */
1471 r
= ioctl(fd
, HPET_INFO
, &info
);
1475 /* Enable periodic mode */
1476 r
= ioctl(fd
, HPET_EPI
, 0);
1477 if (info
.hi_flags
&& (r
< 0))
1480 /* Enable interrupt */
1481 r
= ioctl(fd
, HPET_IE_ON
, 0);
1485 enable_sigio_timer(fd
);
1486 t
->priv
= (void *)(long)fd
;
1494 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1496 int fd
= (long)t
->priv
;
1501 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1504 unsigned long current_rtc_freq
= 0;
1506 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1509 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1510 if (current_rtc_freq
!= RTC_FREQ
&&
1511 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1512 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1513 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1514 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1517 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1523 enable_sigio_timer(rtc_fd
);
1525 t
->priv
= (void *)(long)rtc_fd
;
1530 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1532 int rtc_fd
= (long)t
->priv
;
1537 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1541 struct sigaction act
;
1543 sigfillset(&act
.sa_mask
);
1545 act
.sa_handler
= host_alarm_handler
;
1547 sigaction(SIGALRM
, &act
, NULL
);
1549 ev
.sigev_value
.sival_int
= 0;
1550 ev
.sigev_notify
= SIGEV_SIGNAL
;
1551 ev
.sigev_signo
= SIGALRM
;
1553 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1554 perror("timer_create");
1556 /* disable dynticks */
1557 fprintf(stderr
, "Dynamic Ticks disabled\n");
1562 t
->priv
= (void *)(long)host_timer
;
1567 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1569 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1571 timer_delete(host_timer
);
1574 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1576 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1577 struct itimerspec timeout
;
1578 int64_t nearest_delta_us
= INT64_MAX
;
1581 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1582 !active_timers
[QEMU_TIMER_VIRTUAL
])
1585 nearest_delta_us
= qemu_next_deadline_dyntick();
1587 /* check whether a timer is already running */
1588 if (timer_gettime(host_timer
, &timeout
)) {
1590 fprintf(stderr
, "Internal timer error: aborting\n");
1593 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1594 if (current_us
&& current_us
<= nearest_delta_us
)
1597 timeout
.it_interval
.tv_sec
= 0;
1598 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1599 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1600 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1601 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1603 fprintf(stderr
, "Internal timer error: aborting\n");
1608 #endif /* defined(__linux__) */
1610 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1612 struct sigaction act
;
1613 struct itimerval itv
;
1617 sigfillset(&act
.sa_mask
);
1619 act
.sa_handler
= host_alarm_handler
;
1621 sigaction(SIGALRM
, &act
, NULL
);
1623 itv
.it_interval
.tv_sec
= 0;
1624 /* for i386 kernel 2.6 to get 1 ms */
1625 itv
.it_interval
.tv_usec
= 999;
1626 itv
.it_value
.tv_sec
= 0;
1627 itv
.it_value
.tv_usec
= 10 * 1000;
1629 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1636 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1638 struct itimerval itv
;
1640 memset(&itv
, 0, sizeof(itv
));
1641 setitimer(ITIMER_REAL
, &itv
, NULL
);
1644 #endif /* !defined(_WIN32) */
1646 static void try_to_rearm_timer(void *opaque
)
1648 struct qemu_alarm_timer
*t
= opaque
;
1652 /* Drain the notify pipe */
1655 len
= read(alarm_timer_rfd
, buffer
, sizeof(buffer
));
1656 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
1659 if (t
->flags
& ALARM_FLAG_EXPIRED
) {
1660 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
1661 qemu_rearm_alarm_timer(alarm_timer
);
1667 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1670 struct qemu_alarm_win32
*data
= t
->priv
;
1673 data
->host_alarm
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1674 if (!data
->host_alarm
) {
1675 perror("Failed CreateEvent");
1679 memset(&tc
, 0, sizeof(tc
));
1680 timeGetDevCaps(&tc
, sizeof(tc
));
1682 if (data
->period
< tc
.wPeriodMin
)
1683 data
->period
= tc
.wPeriodMin
;
1685 timeBeginPeriod(data
->period
);
1687 flags
= TIME_CALLBACK_FUNCTION
;
1688 if (alarm_has_dynticks(t
))
1689 flags
|= TIME_ONESHOT
;
1691 flags
|= TIME_PERIODIC
;
1693 data
->timerId
= timeSetEvent(1, // interval (ms)
1694 data
->period
, // resolution
1695 host_alarm_handler
, // function
1696 (DWORD
)t
, // parameter
1699 if (!data
->timerId
) {
1700 perror("Failed to initialize win32 alarm timer");
1702 timeEndPeriod(data
->period
);
1703 CloseHandle(data
->host_alarm
);
1707 qemu_add_wait_object(data
->host_alarm
, try_to_rearm_timer
, t
);
1712 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1714 struct qemu_alarm_win32
*data
= t
->priv
;
1716 timeKillEvent(data
->timerId
);
1717 timeEndPeriod(data
->period
);
1719 CloseHandle(data
->host_alarm
);
1722 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1724 struct qemu_alarm_win32
*data
= t
->priv
;
1725 uint64_t nearest_delta_us
;
1727 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1728 !active_timers
[QEMU_TIMER_VIRTUAL
])
1731 nearest_delta_us
= qemu_next_deadline_dyntick();
1732 nearest_delta_us
/= 1000;
1734 timeKillEvent(data
->timerId
);
1736 data
->timerId
= timeSetEvent(1,
1740 TIME_ONESHOT
| TIME_PERIODIC
);
1742 if (!data
->timerId
) {
1743 perror("Failed to re-arm win32 alarm timer");
1745 timeEndPeriod(data
->period
);
1746 CloseHandle(data
->host_alarm
);
1753 static int init_timer_alarm(void)
1755 struct qemu_alarm_timer
*t
= NULL
;
1765 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
1769 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
1773 alarm_timer_rfd
= fds
[0];
1774 alarm_timer_wfd
= fds
[1];
1777 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1778 t
= &alarm_timers
[i
];
1791 qemu_set_fd_handler2(alarm_timer_rfd
, NULL
,
1792 try_to_rearm_timer
, NULL
, t
);
1807 static void quit_timers(void)
1809 alarm_timer
->stop(alarm_timer
);
1813 /***********************************************************/
1814 /* host time/date access */
1815 void qemu_get_timedate(struct tm
*tm
, int offset
)
1822 if (rtc_date_offset
== -1) {
1826 ret
= localtime(&ti
);
1828 ti
-= rtc_date_offset
;
1832 memcpy(tm
, ret
, sizeof(struct tm
));
1835 int qemu_timedate_diff(struct tm
*tm
)
1839 if (rtc_date_offset
== -1)
1841 seconds
= mktimegm(tm
);
1843 seconds
= mktime(tm
);
1845 seconds
= mktimegm(tm
) + rtc_date_offset
;
1847 return seconds
- time(NULL
);
1851 static void socket_cleanup(void)
1856 static int socket_init(void)
1861 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1863 err
= WSAGetLastError();
1864 fprintf(stderr
, "WSAStartup: %d\n", err
);
1867 atexit(socket_cleanup
);
1872 const char *get_opt_name(char *buf
, int buf_size
, const char *p
, char delim
)
1877 while (*p
!= '\0' && *p
!= delim
) {
1878 if (q
&& (q
- buf
) < buf_size
- 1)
1888 const char *get_opt_value(char *buf
, int buf_size
, const char *p
)
1893 while (*p
!= '\0') {
1895 if (*(p
+ 1) != ',')
1899 if (q
&& (q
- buf
) < buf_size
- 1)
1909 int get_param_value(char *buf
, int buf_size
,
1910 const char *tag
, const char *str
)
1917 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1921 if (!strcmp(tag
, option
)) {
1922 (void)get_opt_value(buf
, buf_size
, p
);
1925 p
= get_opt_value(NULL
, 0, p
);
1934 int check_params(char *buf
, int buf_size
,
1935 const char * const *params
, const char *str
)
1941 while (*p
!= '\0') {
1942 p
= get_opt_name(buf
, buf_size
, p
, '=');
1946 for(i
= 0; params
[i
] != NULL
; i
++)
1947 if (!strcmp(params
[i
], buf
))
1949 if (params
[i
] == NULL
)
1951 p
= get_opt_value(NULL
, 0, p
);
1959 /***********************************************************/
1960 /* Bluetooth support */
1963 static struct HCIInfo
*hci_table
[MAX_NICS
];
1965 static struct bt_vlan_s
{
1966 struct bt_scatternet_s net
;
1968 struct bt_vlan_s
*next
;
1971 /* find or alloc a new bluetooth "VLAN" */
1972 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1974 struct bt_vlan_s
**pvlan
, *vlan
;
1975 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1979 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1981 pvlan
= &first_bt_vlan
;
1982 while (*pvlan
!= NULL
)
1983 pvlan
= &(*pvlan
)->next
;
1988 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1992 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1997 static struct HCIInfo null_hci
= {
1998 .cmd_send
= null_hci_send
,
1999 .sco_send
= null_hci_send
,
2000 .acl_send
= null_hci_send
,
2001 .bdaddr_set
= null_hci_addr_set
,
2004 struct HCIInfo
*qemu_next_hci(void)
2006 if (cur_hci
== nb_hcis
)
2009 return hci_table
[cur_hci
++];
2012 static struct HCIInfo
*hci_init(const char *str
)
2015 struct bt_scatternet_s
*vlan
= 0;
2017 if (!strcmp(str
, "null"))
2020 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
2022 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
2023 else if (!strncmp(str
, "hci", 3)) {
2026 if (!strncmp(str
+ 3, ",vlan=", 6)) {
2027 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
2032 vlan
= qemu_find_bt_vlan(0);
2034 return bt_new_hci(vlan
);
2037 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
2042 static int bt_hci_parse(const char *str
)
2044 struct HCIInfo
*hci
;
2047 if (nb_hcis
>= MAX_NICS
) {
2048 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
2052 hci
= hci_init(str
);
2061 bdaddr
.b
[5] = 0x56 + nb_hcis
;
2062 hci
->bdaddr_set(hci
, bdaddr
.b
);
2064 hci_table
[nb_hcis
++] = hci
;
2069 static void bt_vhci_add(int vlan_id
)
2071 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
2074 fprintf(stderr
, "qemu: warning: adding a VHCI to "
2075 "an empty scatternet %i\n", vlan_id
);
2077 bt_vhci_init(bt_new_hci(vlan
));
2080 static struct bt_device_s
*bt_device_add(const char *opt
)
2082 struct bt_scatternet_s
*vlan
;
2084 char *endp
= strstr(opt
, ",vlan=");
2085 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2088 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2091 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2093 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2098 vlan
= qemu_find_bt_vlan(vlan_id
);
2101 fprintf(stderr
, "qemu: warning: adding a slave device to "
2102 "an empty scatternet %i\n", vlan_id
);
2104 if (!strcmp(devname
, "keyboard"))
2105 return bt_keyboard_init(vlan
);
2107 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2111 static int bt_parse(const char *opt
)
2113 const char *endp
, *p
;
2116 if (strstart(opt
, "hci", &endp
)) {
2117 if (!*endp
|| *endp
== ',') {
2119 if (!strstart(endp
, ",vlan=", 0))
2122 return bt_hci_parse(opt
);
2124 } else if (strstart(opt
, "vhci", &endp
)) {
2125 if (!*endp
|| *endp
== ',') {
2127 if (strstart(endp
, ",vlan=", &p
)) {
2128 vlan
= strtol(p
, (char **) &endp
, 0);
2130 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2134 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2143 } else if (strstart(opt
, "device:", &endp
))
2144 return !bt_device_add(endp
);
2146 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2150 /***********************************************************/
2151 /* QEMU Block devices */
2153 #define HD_ALIAS "index=%d,media=disk"
2154 #define CDROM_ALIAS "index=2,media=cdrom"
2155 #define FD_ALIAS "index=%d,if=floppy"
2156 #define PFLASH_ALIAS "if=pflash"
2157 #define MTD_ALIAS "if=mtd"
2158 #define SD_ALIAS "index=0,if=sd"
2160 static int drive_opt_get_free_idx(void)
2164 for (index
= 0; index
< MAX_DRIVES
; index
++)
2165 if (!drives_opt
[index
].used
) {
2166 drives_opt
[index
].used
= 1;
2173 static int drive_get_free_idx(void)
2177 for (index
= 0; index
< MAX_DRIVES
; index
++)
2178 if (!drives_table
[index
].used
) {
2179 drives_table
[index
].used
= 1;
2186 int drive_add(const char *file
, const char *fmt
, ...)
2189 int index
= drive_opt_get_free_idx();
2191 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2192 fprintf(stderr
, "qemu: too many drives\n");
2196 drives_opt
[index
].file
= file
;
2198 vsnprintf(drives_opt
[index
].opt
,
2199 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2206 void drive_remove(int index
)
2208 drives_opt
[index
].used
= 0;
2212 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2216 /* seek interface, bus and unit */
2218 for (index
= 0; index
< MAX_DRIVES
; index
++)
2219 if (drives_table
[index
].type
== type
&&
2220 drives_table
[index
].bus
== bus
&&
2221 drives_table
[index
].unit
== unit
&&
2222 drives_table
[index
].used
)
2228 int drive_get_max_bus(BlockInterfaceType type
)
2234 for (index
= 0; index
< nb_drives
; index
++) {
2235 if(drives_table
[index
].type
== type
&&
2236 drives_table
[index
].bus
> max_bus
)
2237 max_bus
= drives_table
[index
].bus
;
2242 const char *drive_get_serial(BlockDriverState
*bdrv
)
2246 for (index
= 0; index
< nb_drives
; index
++)
2247 if (drives_table
[index
].bdrv
== bdrv
)
2248 return drives_table
[index
].serial
;
2253 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2257 for (index
= 0; index
< nb_drives
; index
++)
2258 if (drives_table
[index
].bdrv
== bdrv
)
2259 return drives_table
[index
].onerror
;
2261 return BLOCK_ERR_STOP_ENOSPC
;
2264 static void bdrv_format_print(void *opaque
, const char *name
)
2266 fprintf(stderr
, " %s", name
);
2269 void drive_uninit(BlockDriverState
*bdrv
)
2273 for (i
= 0; i
< MAX_DRIVES
; i
++)
2274 if (drives_table
[i
].bdrv
== bdrv
) {
2275 drives_table
[i
].bdrv
= NULL
;
2276 drives_table
[i
].used
= 0;
2277 drive_remove(drives_table
[i
].drive_opt_idx
);
2283 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2289 const char *mediastr
= "";
2290 BlockInterfaceType type
;
2291 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2292 int bus_id
, unit_id
;
2293 int cyls
, heads
, secs
, translation
;
2294 BlockDriverState
*bdrv
;
2295 BlockDriver
*drv
= NULL
;
2296 QEMUMachine
*machine
= opaque
;
2300 int bdrv_flags
, onerror
;
2301 int drives_table_idx
;
2302 char *str
= arg
->opt
;
2303 static const char * const params
[] = { "bus", "unit", "if", "index",
2304 "cyls", "heads", "secs", "trans",
2305 "media", "snapshot", "file",
2306 "cache", "format", "serial", "werror",
2309 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2310 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2316 cyls
= heads
= secs
= 0;
2319 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2323 if (machine
->use_scsi
) {
2325 max_devs
= MAX_SCSI_DEVS
;
2326 pstrcpy(devname
, sizeof(devname
), "scsi");
2329 max_devs
= MAX_IDE_DEVS
;
2330 pstrcpy(devname
, sizeof(devname
), "ide");
2334 /* extract parameters */
2336 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2337 bus_id
= strtol(buf
, NULL
, 0);
2339 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2344 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2345 unit_id
= strtol(buf
, NULL
, 0);
2347 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2352 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2353 pstrcpy(devname
, sizeof(devname
), buf
);
2354 if (!strcmp(buf
, "ide")) {
2356 max_devs
= MAX_IDE_DEVS
;
2357 } else if (!strcmp(buf
, "scsi")) {
2359 max_devs
= MAX_SCSI_DEVS
;
2360 } else if (!strcmp(buf
, "floppy")) {
2363 } else if (!strcmp(buf
, "pflash")) {
2366 } else if (!strcmp(buf
, "mtd")) {
2369 } else if (!strcmp(buf
, "sd")) {
2372 } else if (!strcmp(buf
, "virtio")) {
2376 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2381 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2382 index
= strtol(buf
, NULL
, 0);
2384 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2389 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2390 cyls
= strtol(buf
, NULL
, 0);
2393 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2394 heads
= strtol(buf
, NULL
, 0);
2397 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2398 secs
= strtol(buf
, NULL
, 0);
2401 if (cyls
|| heads
|| secs
) {
2402 if (cyls
< 1 || cyls
> 16383) {
2403 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2406 if (heads
< 1 || heads
> 16) {
2407 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2410 if (secs
< 1 || secs
> 63) {
2411 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2416 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2419 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2423 if (!strcmp(buf
, "none"))
2424 translation
= BIOS_ATA_TRANSLATION_NONE
;
2425 else if (!strcmp(buf
, "lba"))
2426 translation
= BIOS_ATA_TRANSLATION_LBA
;
2427 else if (!strcmp(buf
, "auto"))
2428 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2430 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2435 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2436 if (!strcmp(buf
, "disk")) {
2438 } else if (!strcmp(buf
, "cdrom")) {
2439 if (cyls
|| secs
|| heads
) {
2441 "qemu: '%s' invalid physical CHS format\n", str
);
2444 media
= MEDIA_CDROM
;
2446 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2451 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2452 if (!strcmp(buf
, "on"))
2454 else if (!strcmp(buf
, "off"))
2457 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2462 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2463 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2465 else if (!strcmp(buf
, "writethrough"))
2467 else if (!strcmp(buf
, "writeback"))
2470 fprintf(stderr
, "qemu: invalid cache option\n");
2475 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2476 if (strcmp(buf
, "?") == 0) {
2477 fprintf(stderr
, "qemu: Supported formats:");
2478 bdrv_iterate_format(bdrv_format_print
, NULL
);
2479 fprintf(stderr
, "\n");
2482 drv
= bdrv_find_format(buf
);
2484 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2489 if (arg
->file
== NULL
)
2490 get_param_value(file
, sizeof(file
), "file", str
);
2492 pstrcpy(file
, sizeof(file
), arg
->file
);
2494 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2495 memset(serial
, 0, sizeof(serial
));
2497 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2498 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2499 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2500 fprintf(stderr
, "werror is no supported by this format\n");
2503 if (!strcmp(buf
, "ignore"))
2504 onerror
= BLOCK_ERR_IGNORE
;
2505 else if (!strcmp(buf
, "enospc"))
2506 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2507 else if (!strcmp(buf
, "stop"))
2508 onerror
= BLOCK_ERR_STOP_ANY
;
2509 else if (!strcmp(buf
, "report"))
2510 onerror
= BLOCK_ERR_REPORT
;
2512 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2517 /* compute bus and unit according index */
2520 if (bus_id
!= 0 || unit_id
!= -1) {
2522 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2530 unit_id
= index
% max_devs
;
2531 bus_id
= index
/ max_devs
;
2535 /* if user doesn't specify a unit_id,
2536 * try to find the first free
2539 if (unit_id
== -1) {
2541 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2543 if (max_devs
&& unit_id
>= max_devs
) {
2544 unit_id
-= max_devs
;
2552 if (max_devs
&& unit_id
>= max_devs
) {
2553 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2554 str
, unit_id
, max_devs
- 1);
2559 * ignore multiple definitions
2562 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2567 if (type
== IF_IDE
|| type
== IF_SCSI
)
2568 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2570 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2571 devname
, bus_id
, mediastr
, unit_id
);
2573 snprintf(buf
, sizeof(buf
), "%s%s%i",
2574 devname
, mediastr
, unit_id
);
2575 bdrv
= bdrv_new(buf
);
2576 drives_table_idx
= drive_get_free_idx();
2577 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2578 drives_table
[drives_table_idx
].type
= type
;
2579 drives_table
[drives_table_idx
].bus
= bus_id
;
2580 drives_table
[drives_table_idx
].unit
= unit_id
;
2581 drives_table
[drives_table_idx
].onerror
= onerror
;
2582 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2583 strncpy(drives_table
[nb_drives
].serial
, serial
, sizeof(serial
));
2592 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2593 bdrv_set_translation_hint(bdrv
, translation
);
2597 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2602 /* FIXME: This isn't really a floppy, but it's a reasonable
2605 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2616 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2617 cache
= 2; /* always use write-back with snapshot */
2619 if (cache
== 0) /* no caching */
2620 bdrv_flags
|= BDRV_O_NOCACHE
;
2621 else if (cache
== 2) /* write-back */
2622 bdrv_flags
|= BDRV_O_CACHE_WB
;
2623 else if (cache
== 3) /* not specified */
2624 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2625 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2626 fprintf(stderr
, "qemu: could not open disk image %s\n",
2630 if (bdrv_key_required(bdrv
))
2632 return drives_table_idx
;
2635 static void numa_add(const char *optarg
)
2639 unsigned long long value
, endvalue
;
2642 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2643 if (!strcmp(option
, "node")) {
2644 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2645 nodenr
= nb_numa_nodes
;
2647 nodenr
= strtoull(option
, NULL
, 10);
2650 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2651 node_mem
[nodenr
] = 0;
2653 value
= strtoull(option
, &endptr
, 0);
2655 case 0: case 'M': case 'm':
2662 node_mem
[nodenr
] = value
;
2664 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2665 node_cpumask
[nodenr
] = 0;
2667 value
= strtoull(option
, &endptr
, 10);
2670 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2672 if (*endptr
== '-') {
2673 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2674 if (endvalue
>= 63) {
2677 "only 63 CPUs in NUMA mode supported.\n");
2679 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2684 node_cpumask
[nodenr
] = value
;
2691 /***********************************************************/
2694 static USBPort
*used_usb_ports
;
2695 static USBPort
*free_usb_ports
;
2697 /* ??? Maybe change this to register a hub to keep track of the topology. */
2698 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2699 usb_attachfn attach
)
2701 port
->opaque
= opaque
;
2702 port
->index
= index
;
2703 port
->attach
= attach
;
2704 port
->next
= free_usb_ports
;
2705 free_usb_ports
= port
;
2708 int usb_device_add_dev(USBDevice
*dev
)
2712 /* Find a USB port to add the device to. */
2713 port
= free_usb_ports
;
2717 /* Create a new hub and chain it on. */
2718 free_usb_ports
= NULL
;
2719 port
->next
= used_usb_ports
;
2720 used_usb_ports
= port
;
2722 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2723 usb_attach(port
, hub
);
2724 port
= free_usb_ports
;
2727 free_usb_ports
= port
->next
;
2728 port
->next
= used_usb_ports
;
2729 used_usb_ports
= port
;
2730 usb_attach(port
, dev
);
2734 static void usb_msd_password_cb(void *opaque
, int err
)
2736 USBDevice
*dev
= opaque
;
2739 usb_device_add_dev(dev
);
2741 dev
->handle_destroy(dev
);
2744 static int usb_device_add(const char *devname
, int is_hotplug
)
2749 if (!free_usb_ports
)
2752 if (strstart(devname
, "host:", &p
)) {
2753 dev
= usb_host_device_open(p
);
2754 } else if (!strcmp(devname
, "mouse")) {
2755 dev
= usb_mouse_init();
2756 } else if (!strcmp(devname
, "tablet")) {
2757 dev
= usb_tablet_init();
2758 } else if (!strcmp(devname
, "keyboard")) {
2759 dev
= usb_keyboard_init();
2760 } else if (strstart(devname
, "disk:", &p
)) {
2761 BlockDriverState
*bs
;
2763 dev
= usb_msd_init(p
);
2766 bs
= usb_msd_get_bdrv(dev
);
2767 if (bdrv_key_required(bs
)) {
2770 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2775 } else if (!strcmp(devname
, "wacom-tablet")) {
2776 dev
= usb_wacom_init();
2777 } else if (strstart(devname
, "serial:", &p
)) {
2778 dev
= usb_serial_init(p
);
2779 #ifdef CONFIG_BRLAPI
2780 } else if (!strcmp(devname
, "braille")) {
2781 dev
= usb_baum_init();
2783 } else if (strstart(devname
, "net:", &p
)) {
2786 if (net_client_init("nic", p
) < 0)
2788 nd_table
[nic
].model
= "usb";
2789 dev
= usb_net_init(&nd_table
[nic
]);
2790 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2791 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2792 bt_new_hci(qemu_find_bt_vlan(0)));
2799 return usb_device_add_dev(dev
);
2802 int usb_device_del_addr(int bus_num
, int addr
)
2808 if (!used_usb_ports
)
2814 lastp
= &used_usb_ports
;
2815 port
= used_usb_ports
;
2816 while (port
&& port
->dev
->addr
!= addr
) {
2817 lastp
= &port
->next
;
2825 *lastp
= port
->next
;
2826 usb_attach(port
, NULL
);
2827 dev
->handle_destroy(dev
);
2828 port
->next
= free_usb_ports
;
2829 free_usb_ports
= port
;
2833 static int usb_device_del(const char *devname
)
2838 if (strstart(devname
, "host:", &p
))
2839 return usb_host_device_close(p
);
2841 if (!used_usb_ports
)
2844 p
= strchr(devname
, '.');
2847 bus_num
= strtoul(devname
, NULL
, 0);
2848 addr
= strtoul(p
+ 1, NULL
, 0);
2850 return usb_device_del_addr(bus_num
, addr
);
2853 void do_usb_add(Monitor
*mon
, const char *devname
)
2855 usb_device_add(devname
, 1);
2858 void do_usb_del(Monitor
*mon
, const char *devname
)
2860 usb_device_del(devname
);
2863 void usb_info(Monitor
*mon
)
2867 const char *speed_str
;
2870 monitor_printf(mon
, "USB support not enabled\n");
2874 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2878 switch(dev
->speed
) {
2882 case USB_SPEED_FULL
:
2885 case USB_SPEED_HIGH
:
2892 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2893 0, dev
->addr
, speed_str
, dev
->devname
);
2897 /***********************************************************/
2898 /* PCMCIA/Cardbus */
2900 static struct pcmcia_socket_entry_s
{
2901 struct pcmcia_socket_s
*socket
;
2902 struct pcmcia_socket_entry_s
*next
;
2903 } *pcmcia_sockets
= 0;
2905 void pcmcia_socket_register(struct pcmcia_socket_s
*socket
)
2907 struct pcmcia_socket_entry_s
*entry
;
2909 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2910 entry
->socket
= socket
;
2911 entry
->next
= pcmcia_sockets
;
2912 pcmcia_sockets
= entry
;
2915 void pcmcia_socket_unregister(struct pcmcia_socket_s
*socket
)
2917 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2919 ptr
= &pcmcia_sockets
;
2920 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2921 if (entry
->socket
== socket
) {
2927 void pcmcia_info(Monitor
*mon
)
2929 struct pcmcia_socket_entry_s
*iter
;
2931 if (!pcmcia_sockets
)
2932 monitor_printf(mon
, "No PCMCIA sockets\n");
2934 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2935 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2936 iter
->socket
->attached
? iter
->socket
->card_string
:
2940 /***********************************************************/
2941 /* register display */
2943 struct DisplayAllocator default_allocator
= {
2944 defaultallocator_create_displaysurface
,
2945 defaultallocator_resize_displaysurface
,
2946 defaultallocator_free_displaysurface
2949 void register_displaystate(DisplayState
*ds
)
2959 DisplayState
*get_displaystate(void)
2961 return display_state
;
2964 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2966 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2967 return ds
->allocator
;
2972 static void dumb_display_init(void)
2974 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2975 ds
->allocator
= &default_allocator
;
2976 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2977 register_displaystate(ds
);
2980 /***********************************************************/
2983 typedef struct IOHandlerRecord
{
2985 IOCanRWHandler
*fd_read_poll
;
2987 IOHandler
*fd_write
;
2990 /* temporary data */
2992 struct IOHandlerRecord
*next
;
2995 static IOHandlerRecord
*first_io_handler
;
2997 /* XXX: fd_read_poll should be suppressed, but an API change is
2998 necessary in the character devices to suppress fd_can_read(). */
2999 int qemu_set_fd_handler2(int fd
,
3000 IOCanRWHandler
*fd_read_poll
,
3002 IOHandler
*fd_write
,
3005 IOHandlerRecord
**pioh
, *ioh
;
3007 if (!fd_read
&& !fd_write
) {
3008 pioh
= &first_io_handler
;
3013 if (ioh
->fd
== fd
) {
3020 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3024 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
3025 ioh
->next
= first_io_handler
;
3026 first_io_handler
= ioh
;
3029 ioh
->fd_read_poll
= fd_read_poll
;
3030 ioh
->fd_read
= fd_read
;
3031 ioh
->fd_write
= fd_write
;
3032 ioh
->opaque
= opaque
;
3038 int qemu_set_fd_handler(int fd
,
3040 IOHandler
*fd_write
,
3043 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
3047 /***********************************************************/
3048 /* Polling handling */
3050 typedef struct PollingEntry
{
3053 struct PollingEntry
*next
;
3056 static PollingEntry
*first_polling_entry
;
3058 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
3060 PollingEntry
**ppe
, *pe
;
3061 pe
= qemu_mallocz(sizeof(PollingEntry
));
3063 pe
->opaque
= opaque
;
3064 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3069 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3071 PollingEntry
**ppe
, *pe
;
3072 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3074 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3082 /***********************************************************/
3083 /* Wait objects support */
3084 typedef struct WaitObjects
{
3086 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3087 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3088 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3091 static WaitObjects wait_objects
= {0};
3093 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3095 WaitObjects
*w
= &wait_objects
;
3097 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3099 w
->events
[w
->num
] = handle
;
3100 w
->func
[w
->num
] = func
;
3101 w
->opaque
[w
->num
] = opaque
;
3106 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3109 WaitObjects
*w
= &wait_objects
;
3112 for (i
= 0; i
< w
->num
; i
++) {
3113 if (w
->events
[i
] == handle
)
3116 w
->events
[i
] = w
->events
[i
+ 1];
3117 w
->func
[i
] = w
->func
[i
+ 1];
3118 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3126 /***********************************************************/
3127 /* ram save/restore */
3129 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3133 v
= qemu_get_byte(f
);
3136 if (qemu_get_buffer(f
, buf
, len
) != len
)
3140 v
= qemu_get_byte(f
);
3141 memset(buf
, v
, len
);
3147 if (qemu_file_has_error(f
))
3153 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3158 if (qemu_get_be32(f
) != last_ram_offset
)
3160 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
3161 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
3168 #define BDRV_HASH_BLOCK_SIZE 1024
3169 #define IOBUF_SIZE 4096
3170 #define RAM_CBLOCK_MAGIC 0xfabe
3172 typedef struct RamDecompressState
{
3175 uint8_t buf
[IOBUF_SIZE
];
3176 } RamDecompressState
;
3178 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3181 memset(s
, 0, sizeof(*s
));
3183 ret
= inflateInit(&s
->zstream
);
3189 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3193 s
->zstream
.avail_out
= len
;
3194 s
->zstream
.next_out
= buf
;
3195 while (s
->zstream
.avail_out
> 0) {
3196 if (s
->zstream
.avail_in
== 0) {
3197 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3199 clen
= qemu_get_be16(s
->f
);
3200 if (clen
> IOBUF_SIZE
)
3202 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3203 s
->zstream
.avail_in
= clen
;
3204 s
->zstream
.next_in
= s
->buf
;
3206 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3207 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3214 static void ram_decompress_close(RamDecompressState
*s
)
3216 inflateEnd(&s
->zstream
);
3219 #define RAM_SAVE_FLAG_FULL 0x01
3220 #define RAM_SAVE_FLAG_COMPRESS 0x02
3221 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3222 #define RAM_SAVE_FLAG_PAGE 0x08
3223 #define RAM_SAVE_FLAG_EOS 0x10
3225 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3227 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3228 uint32_t *array
= (uint32_t *)page
;
3231 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3232 if (array
[i
] != val
)
3239 static int ram_save_block(QEMUFile
*f
)
3241 static ram_addr_t current_addr
= 0;
3242 ram_addr_t saved_addr
= current_addr
;
3243 ram_addr_t addr
= 0;
3246 while (addr
< last_ram_offset
) {
3247 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3250 cpu_physical_memory_reset_dirty(current_addr
,
3251 current_addr
+ TARGET_PAGE_SIZE
,
3252 MIGRATION_DIRTY_FLAG
);
3254 p
= qemu_get_ram_ptr(current_addr
);
3256 if (is_dup_page(p
, *p
)) {
3257 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3258 qemu_put_byte(f
, *p
);
3260 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3261 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3267 addr
+= TARGET_PAGE_SIZE
;
3268 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3274 static ram_addr_t ram_save_threshold
= 10;
3276 static ram_addr_t
ram_save_remaining(void)
3279 ram_addr_t count
= 0;
3281 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3282 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3289 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3294 /* Make sure all dirty bits are set */
3295 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3296 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3297 cpu_physical_memory_set_dirty(addr
);
3300 /* Enable dirty memory tracking */
3301 cpu_physical_memory_set_dirty_tracking(1);
3303 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3306 while (!qemu_file_rate_limit(f
)) {
3309 ret
= ram_save_block(f
);
3310 if (ret
== 0) /* no more blocks */
3314 /* try transferring iterative blocks of memory */
3318 /* flush all remaining blocks regardless of rate limiting */
3319 while (ram_save_block(f
) != 0);
3320 cpu_physical_memory_set_dirty_tracking(0);
3323 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3325 return (stage
== 2) && (ram_save_remaining() < ram_save_threshold
);
3328 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3330 RamDecompressState s1
, *s
= &s1
;
3334 if (ram_decompress_open(s
, f
) < 0)
3336 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3337 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3338 fprintf(stderr
, "Error while reading ram block header\n");
3342 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3343 BDRV_HASH_BLOCK_SIZE
) < 0) {
3344 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3349 printf("Error block header\n");
3353 ram_decompress_close(s
);
3358 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3363 if (version_id
== 1)
3364 return ram_load_v1(f
, opaque
);
3366 if (version_id
== 2) {
3367 if (qemu_get_be32(f
) != last_ram_offset
)
3369 return ram_load_dead(f
, opaque
);
3372 if (version_id
!= 3)
3376 addr
= qemu_get_be64(f
);
3378 flags
= addr
& ~TARGET_PAGE_MASK
;
3379 addr
&= TARGET_PAGE_MASK
;
3381 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3382 if (addr
!= last_ram_offset
)
3386 if (flags
& RAM_SAVE_FLAG_FULL
) {
3387 if (ram_load_dead(f
, opaque
) < 0)
3391 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3392 uint8_t ch
= qemu_get_byte(f
);
3393 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3394 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3395 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3396 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3401 void qemu_service_io(void)
3403 CPUState
*env
= cpu_single_env
;
3407 if (env
->kqemu_enabled
) {
3408 kqemu_cpu_interrupt(env
);
3414 /***********************************************************/
3415 /* bottom halves (can be seen as timers which expire ASAP) */
3426 static QEMUBH
*first_bh
= NULL
;
3428 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3431 bh
= qemu_mallocz(sizeof(QEMUBH
));
3433 bh
->opaque
= opaque
;
3434 bh
->next
= first_bh
;
3439 int qemu_bh_poll(void)
3445 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3446 if (!bh
->deleted
&& bh
->scheduled
) {
3455 /* remove deleted bhs */
3469 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3477 void qemu_bh_schedule(QEMUBH
*bh
)
3479 CPUState
*env
= cpu_single_env
;
3484 /* stop the currently executing CPU to execute the BH ASAP */
3490 void qemu_bh_cancel(QEMUBH
*bh
)
3495 void qemu_bh_delete(QEMUBH
*bh
)
3501 static void qemu_bh_update_timeout(int *timeout
)
3505 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3506 if (!bh
->deleted
&& bh
->scheduled
) {
3508 /* idle bottom halves will be polled at least
3510 *timeout
= MIN(10, *timeout
);
3512 /* non-idle bottom halves will be executed
3521 /***********************************************************/
3522 /* machine registration */
3524 static QEMUMachine
*first_machine
= NULL
;
3525 QEMUMachine
*current_machine
= NULL
;
3527 int qemu_register_machine(QEMUMachine
*m
)
3530 pm
= &first_machine
;
3538 static QEMUMachine
*find_machine(const char *name
)
3542 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3543 if (!strcmp(m
->name
, name
))
3549 /***********************************************************/
3550 /* main execution loop */
3552 static void gui_update(void *opaque
)
3554 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3555 DisplayState
*ds
= opaque
;
3556 DisplayChangeListener
*dcl
= ds
->listeners
;
3560 while (dcl
!= NULL
) {
3561 if (dcl
->gui_timer_interval
&&
3562 dcl
->gui_timer_interval
< interval
)
3563 interval
= dcl
->gui_timer_interval
;
3566 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3569 static void nographic_update(void *opaque
)
3571 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3573 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3576 struct vm_change_state_entry
{
3577 VMChangeStateHandler
*cb
;
3579 LIST_ENTRY (vm_change_state_entry
) entries
;
3582 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3584 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3587 VMChangeStateEntry
*e
;
3589 e
= qemu_mallocz(sizeof (*e
));
3593 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3597 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3599 LIST_REMOVE (e
, entries
);
3603 static void vm_state_notify(int running
, int reason
)
3605 VMChangeStateEntry
*e
;
3607 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3608 e
->cb(e
->opaque
, running
, reason
);
3617 vm_state_notify(1, 0);
3618 qemu_rearm_alarm_timer(alarm_timer
);
3622 void vm_stop(int reason
)
3625 cpu_disable_ticks();
3627 vm_state_notify(0, reason
);
3631 /* reset/shutdown handler */
3633 typedef struct QEMUResetEntry
{
3634 QEMUResetHandler
*func
;
3636 struct QEMUResetEntry
*next
;
3639 static QEMUResetEntry
*first_reset_entry
;
3640 static int reset_requested
;
3641 static int shutdown_requested
;
3642 static int powerdown_requested
;
3644 int qemu_shutdown_requested(void)
3646 int r
= shutdown_requested
;
3647 shutdown_requested
= 0;
3651 int qemu_reset_requested(void)
3653 int r
= reset_requested
;
3654 reset_requested
= 0;
3658 int qemu_powerdown_requested(void)
3660 int r
= powerdown_requested
;
3661 powerdown_requested
= 0;
3665 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3667 QEMUResetEntry
**pre
, *re
;
3669 pre
= &first_reset_entry
;
3670 while (*pre
!= NULL
)
3671 pre
= &(*pre
)->next
;
3672 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3674 re
->opaque
= opaque
;
3679 void qemu_system_reset(void)
3683 /* reset all devices */
3684 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3685 re
->func(re
->opaque
);
3691 void qemu_system_reset_request(void)
3694 shutdown_requested
= 1;
3696 reset_requested
= 1;
3699 cpu_exit(cpu_single_env
);
3702 void qemu_system_shutdown_request(void)
3704 shutdown_requested
= 1;
3706 cpu_exit(cpu_single_env
);
3709 void qemu_system_powerdown_request(void)
3711 powerdown_requested
= 1;
3713 cpu_exit(cpu_single_env
);
3717 static void host_main_loop_wait(int *timeout
)
3723 /* XXX: need to suppress polling by better using win32 events */
3725 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3726 ret
|= pe
->func(pe
->opaque
);
3730 WaitObjects
*w
= &wait_objects
;
3732 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3733 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3734 if (w
->func
[ret
- WAIT_OBJECT_0
])
3735 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3737 /* Check for additional signaled events */
3738 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3740 /* Check if event is signaled */
3741 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3742 if(ret2
== WAIT_OBJECT_0
) {
3744 w
->func
[i
](w
->opaque
[i
]);
3745 } else if (ret2
== WAIT_TIMEOUT
) {
3747 err
= GetLastError();
3748 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3751 } else if (ret
== WAIT_TIMEOUT
) {
3753 err
= GetLastError();
3754 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3761 static void host_main_loop_wait(int *timeout
)
3766 void main_loop_wait(int timeout
)
3768 IOHandlerRecord
*ioh
;
3769 fd_set rfds
, wfds
, xfds
;
3773 qemu_bh_update_timeout(&timeout
);
3775 host_main_loop_wait(&timeout
);
3777 /* poll any events */
3778 /* XXX: separate device handlers from system ones */
3783 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3787 (!ioh
->fd_read_poll
||
3788 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3789 FD_SET(ioh
->fd
, &rfds
);
3793 if (ioh
->fd_write
) {
3794 FD_SET(ioh
->fd
, &wfds
);
3800 tv
.tv_sec
= timeout
/ 1000;
3801 tv
.tv_usec
= (timeout
% 1000) * 1000;
3803 #if defined(CONFIG_SLIRP)
3804 if (slirp_is_inited()) {
3805 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3808 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3810 IOHandlerRecord
**pioh
;
3812 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3813 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3814 ioh
->fd_read(ioh
->opaque
);
3816 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3817 ioh
->fd_write(ioh
->opaque
);
3821 /* remove deleted IO handlers */
3822 pioh
= &first_io_handler
;
3832 #if defined(CONFIG_SLIRP)
3833 if (slirp_is_inited()) {
3839 slirp_select_poll(&rfds
, &wfds
, &xfds
);
3843 /* vm time timers */
3844 if (vm_running
&& likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3845 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3846 qemu_get_clock(vm_clock
));
3848 /* real time timers */
3849 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3850 qemu_get_clock(rt_clock
));
3852 /* Check bottom-halves last in case any of the earlier events triggered
3858 static int main_loop(void)
3861 #ifdef CONFIG_PROFILER
3866 cur_cpu
= first_cpu
;
3867 next_cpu
= cur_cpu
->next_cpu
?: first_cpu
;
3874 #ifdef CONFIG_PROFILER
3875 ti
= profile_getclock();
3880 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3881 env
->icount_decr
.u16
.low
= 0;
3882 env
->icount_extra
= 0;
3883 count
= qemu_next_deadline();
3884 count
= (count
+ (1 << icount_time_shift
) - 1)
3885 >> icount_time_shift
;
3886 qemu_icount
+= count
;
3887 decr
= (count
> 0xffff) ? 0xffff : count
;
3889 env
->icount_decr
.u16
.low
= decr
;
3890 env
->icount_extra
= count
;
3892 ret
= cpu_exec(env
);
3893 #ifdef CONFIG_PROFILER
3894 qemu_time
+= profile_getclock() - ti
;
3897 /* Fold pending instructions back into the
3898 instruction counter, and clear the interrupt flag. */
3899 qemu_icount
-= (env
->icount_decr
.u16
.low
3900 + env
->icount_extra
);
3901 env
->icount_decr
.u32
= 0;
3902 env
->icount_extra
= 0;
3904 next_cpu
= env
->next_cpu
?: first_cpu
;
3905 if (event_pending
&& likely(ret
!= EXCP_DEBUG
)) {
3906 ret
= EXCP_INTERRUPT
;
3910 if (ret
== EXCP_HLT
) {
3911 /* Give the next CPU a chance to run. */
3915 if (ret
!= EXCP_HALTED
)
3917 /* all CPUs are halted ? */
3923 if (shutdown_requested
) {
3924 ret
= EXCP_INTERRUPT
;
3932 if (reset_requested
) {
3933 reset_requested
= 0;
3934 qemu_system_reset();
3935 ret
= EXCP_INTERRUPT
;
3937 if (powerdown_requested
) {
3938 powerdown_requested
= 0;
3939 qemu_system_powerdown();
3940 ret
= EXCP_INTERRUPT
;
3942 if (unlikely(ret
== EXCP_DEBUG
)) {
3943 gdb_set_stop_cpu(cur_cpu
);
3944 vm_stop(EXCP_DEBUG
);
3946 /* If all cpus are halted then wait until the next IRQ */
3947 /* XXX: use timeout computed from timers */
3948 if (ret
== EXCP_HALTED
) {
3952 /* Advance virtual time to the next event. */
3953 if (use_icount
== 1) {
3954 /* When not using an adaptive execution frequency
3955 we tend to get badly out of sync with real time,
3956 so just delay for a reasonable amount of time. */
3959 delta
= cpu_get_icount() - cpu_get_clock();
3962 /* If virtual time is ahead of real time then just
3964 timeout
= (delta
/ 1000000) + 1;
3966 /* Wait for either IO to occur or the next
3968 add
= qemu_next_deadline();
3969 /* We advance the timer before checking for IO.
3970 Limit the amount we advance so that early IO
3971 activity won't get the guest too far ahead. */
3975 add
= (add
+ (1 << icount_time_shift
) - 1)
3976 >> icount_time_shift
;
3978 timeout
= delta
/ 1000000;
3989 if (shutdown_requested
) {
3990 ret
= EXCP_INTERRUPT
;
3995 #ifdef CONFIG_PROFILER
3996 ti
= profile_getclock();
3998 main_loop_wait(timeout
);
3999 #ifdef CONFIG_PROFILER
4000 dev_time
+= profile_getclock() - ti
;
4003 cpu_disable_ticks();
4007 static void version(void)
4009 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4012 static void help(int exitcode
)
4015 printf("usage: %s [options] [disk_image]\n"
4017 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4019 #define DEF(option, opt_arg, opt_enum, opt_help) \
4021 #define DEFHEADING(text) stringify(text) "\n"
4022 #include "qemu-options.h"
4027 "During emulation, the following keys are useful:\n"
4028 "ctrl-alt-f toggle full screen\n"
4029 "ctrl-alt-n switch to virtual console 'n'\n"
4030 "ctrl-alt toggle mouse and keyboard grab\n"
4032 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4037 DEFAULT_NETWORK_SCRIPT
,
4038 DEFAULT_NETWORK_DOWN_SCRIPT
,
4040 DEFAULT_GDBSTUB_PORT
,
4045 #define HAS_ARG 0x0001
4048 #define DEF(option, opt_arg, opt_enum, opt_help) \
4050 #define DEFHEADING(text)
4051 #include "qemu-options.h"
4057 typedef struct QEMUOption
{
4063 static const QEMUOption qemu_options
[] = {
4064 { "h", 0, QEMU_OPTION_h
},
4065 #define DEF(option, opt_arg, opt_enum, opt_help) \
4066 { option, opt_arg, opt_enum },
4067 #define DEFHEADING(text)
4068 #include "qemu-options.h"
4076 struct soundhw soundhw
[] = {
4077 #ifdef HAS_AUDIO_CHOICE
4078 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4084 { .init_isa
= pcspk_audio_init
}
4091 "Creative Sound Blaster 16",
4094 { .init_isa
= SB16_init
}
4098 #ifdef CONFIG_CS4231A
4104 { .init_isa
= cs4231a_init
}
4112 "Yamaha YMF262 (OPL3)",
4114 "Yamaha YM3812 (OPL2)",
4118 { .init_isa
= Adlib_init
}
4125 "Gravis Ultrasound GF1",
4128 { .init_isa
= GUS_init
}
4135 "Intel 82801AA AC97 Audio",
4138 { .init_pci
= ac97_init
}
4142 #ifdef CONFIG_ES1370
4145 "ENSONIQ AudioPCI ES1370",
4148 { .init_pci
= es1370_init
}
4152 #endif /* HAS_AUDIO_CHOICE */
4154 { NULL
, NULL
, 0, 0, { NULL
} }
4157 static void select_soundhw (const char *optarg
)
4161 if (*optarg
== '?') {
4164 printf ("Valid sound card names (comma separated):\n");
4165 for (c
= soundhw
; c
->name
; ++c
) {
4166 printf ("%-11s %s\n", c
->name
, c
->descr
);
4168 printf ("\n-soundhw all will enable all of the above\n");
4169 exit (*optarg
!= '?');
4177 if (!strcmp (optarg
, "all")) {
4178 for (c
= soundhw
; c
->name
; ++c
) {
4186 e
= strchr (p
, ',');
4187 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4189 for (c
= soundhw
; c
->name
; ++c
) {
4190 if (!strncmp (c
->name
, p
, l
)) {
4199 "Unknown sound card name (too big to show)\n");
4202 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4207 p
+= l
+ (e
!= NULL
);
4211 goto show_valid_cards
;
4216 static void select_vgahw (const char *p
)
4220 if (strstart(p
, "std", &opts
)) {
4221 std_vga_enabled
= 1;
4222 cirrus_vga_enabled
= 0;
4224 } else if (strstart(p
, "cirrus", &opts
)) {
4225 cirrus_vga_enabled
= 1;
4226 std_vga_enabled
= 0;
4228 } else if (strstart(p
, "vmware", &opts
)) {
4229 cirrus_vga_enabled
= 0;
4230 std_vga_enabled
= 0;
4232 } else if (strstart(p
, "none", &opts
)) {
4233 cirrus_vga_enabled
= 0;
4234 std_vga_enabled
= 0;
4238 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4242 const char *nextopt
;
4244 if (strstart(opts
, ",retrace=", &nextopt
)) {
4246 if (strstart(opts
, "dumb", &nextopt
))
4247 vga_retrace_method
= VGA_RETRACE_DUMB
;
4248 else if (strstart(opts
, "precise", &nextopt
))
4249 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4250 else goto invalid_vga
;
4251 } else goto invalid_vga
;
4257 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4259 exit(STATUS_CONTROL_C_EXIT
);
4264 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4268 if(strlen(str
) != 36)
4271 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4272 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4273 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4279 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4285 #define MAX_NET_CLIENTS 32
4289 static void termsig_handler(int signal
)
4291 qemu_system_shutdown_request();
4294 static void termsig_setup(void)
4296 struct sigaction act
;
4298 memset(&act
, 0, sizeof(act
));
4299 act
.sa_handler
= termsig_handler
;
4300 sigaction(SIGINT
, &act
, NULL
);
4301 sigaction(SIGHUP
, &act
, NULL
);
4302 sigaction(SIGTERM
, &act
, NULL
);
4307 int main(int argc
, char **argv
, char **envp
)
4309 #ifdef CONFIG_GDBSTUB
4310 const char *gdbstub_dev
= NULL
;
4312 uint32_t boot_devices_bitmap
= 0;
4314 int snapshot
, linux_boot
, net_boot
;
4315 const char *initrd_filename
;
4316 const char *kernel_filename
, *kernel_cmdline
;
4317 const char *boot_devices
= "";
4319 DisplayChangeListener
*dcl
;
4320 int cyls
, heads
, secs
, translation
;
4321 const char *net_clients
[MAX_NET_CLIENTS
];
4323 const char *bt_opts
[MAX_BT_CMDLINE
];
4327 const char *r
, *optarg
;
4328 CharDriverState
*monitor_hd
= NULL
;
4329 const char *monitor_device
;
4330 const char *serial_devices
[MAX_SERIAL_PORTS
];
4331 int serial_device_index
;
4332 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4333 int parallel_device_index
;
4334 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4335 int virtio_console_index
;
4336 const char *loadvm
= NULL
;
4337 QEMUMachine
*machine
;
4338 const char *cpu_model
;
4339 const char *usb_devices
[MAX_USB_CMDLINE
];
4340 int usb_devices_index
;
4345 const char *pid_file
= NULL
;
4346 const char *incoming
= NULL
;
4349 struct passwd
*pwd
= NULL
;
4350 const char *chroot_dir
= NULL
;
4351 const char *run_as
= NULL
;
4355 qemu_cache_utils_init(envp
);
4357 LIST_INIT (&vm_change_state_head
);
4360 struct sigaction act
;
4361 sigfillset(&act
.sa_mask
);
4363 act
.sa_handler
= SIG_IGN
;
4364 sigaction(SIGPIPE
, &act
, NULL
);
4367 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4368 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4369 QEMU to run on a single CPU */
4374 h
= GetCurrentProcess();
4375 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4376 for(i
= 0; i
< 32; i
++) {
4377 if (mask
& (1 << i
))
4382 SetProcessAffinityMask(h
, mask
);
4388 register_machines();
4389 machine
= first_machine
;
4391 initrd_filename
= NULL
;
4393 vga_ram_size
= VGA_RAM_SIZE
;
4397 kernel_filename
= NULL
;
4398 kernel_cmdline
= "";
4399 cyls
= heads
= secs
= 0;
4400 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4401 monitor_device
= "vc:80Cx24C";
4403 serial_devices
[0] = "vc:80Cx24C";
4404 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4405 serial_devices
[i
] = NULL
;
4406 serial_device_index
= 0;
4408 parallel_devices
[0] = "vc:80Cx24C";
4409 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4410 parallel_devices
[i
] = NULL
;
4411 parallel_device_index
= 0;
4413 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4414 virtio_consoles
[i
] = NULL
;
4415 virtio_console_index
= 0;
4417 for (i
= 0; i
< MAX_NODES
; i
++) {
4419 node_cpumask
[i
] = 0;
4422 usb_devices_index
= 0;
4442 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4444 const QEMUOption
*popt
;
4447 /* Treat --foo the same as -foo. */
4450 popt
= qemu_options
;
4453 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4457 if (!strcmp(popt
->name
, r
+ 1))
4461 if (popt
->flags
& HAS_ARG
) {
4462 if (optind
>= argc
) {
4463 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4467 optarg
= argv
[optind
++];
4472 switch(popt
->index
) {
4474 machine
= find_machine(optarg
);
4477 printf("Supported machines are:\n");
4478 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4479 printf("%-10s %s%s\n",
4481 m
== first_machine
? " (default)" : "");
4483 exit(*optarg
!= '?');
4486 case QEMU_OPTION_cpu
:
4487 /* hw initialization will check this */
4488 if (*optarg
== '?') {
4489 /* XXX: implement xxx_cpu_list for targets that still miss it */
4490 #if defined(cpu_list)
4491 cpu_list(stdout
, &fprintf
);
4498 case QEMU_OPTION_initrd
:
4499 initrd_filename
= optarg
;
4501 case QEMU_OPTION_hda
:
4503 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
4505 hda_index
= drive_add(optarg
, HD_ALIAS
4506 ",cyls=%d,heads=%d,secs=%d%s",
4507 0, cyls
, heads
, secs
,
4508 translation
== BIOS_ATA_TRANSLATION_LBA
?
4510 translation
== BIOS_ATA_TRANSLATION_NONE
?
4511 ",trans=none" : "");
4513 case QEMU_OPTION_hdb
:
4514 case QEMU_OPTION_hdc
:
4515 case QEMU_OPTION_hdd
:
4516 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4518 case QEMU_OPTION_drive
:
4519 drive_add(NULL
, "%s", optarg
);
4521 case QEMU_OPTION_mtdblock
:
4522 drive_add(optarg
, MTD_ALIAS
);
4524 case QEMU_OPTION_sd
:
4525 drive_add(optarg
, SD_ALIAS
);
4527 case QEMU_OPTION_pflash
:
4528 drive_add(optarg
, PFLASH_ALIAS
);
4530 case QEMU_OPTION_snapshot
:
4533 case QEMU_OPTION_hdachs
:
4537 cyls
= strtol(p
, (char **)&p
, 0);
4538 if (cyls
< 1 || cyls
> 16383)
4543 heads
= strtol(p
, (char **)&p
, 0);
4544 if (heads
< 1 || heads
> 16)
4549 secs
= strtol(p
, (char **)&p
, 0);
4550 if (secs
< 1 || secs
> 63)
4554 if (!strcmp(p
, "none"))
4555 translation
= BIOS_ATA_TRANSLATION_NONE
;
4556 else if (!strcmp(p
, "lba"))
4557 translation
= BIOS_ATA_TRANSLATION_LBA
;
4558 else if (!strcmp(p
, "auto"))
4559 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4562 } else if (*p
!= '\0') {
4564 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4567 if (hda_index
!= -1)
4568 snprintf(drives_opt
[hda_index
].opt
,
4569 sizeof(drives_opt
[hda_index
].opt
),
4570 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
4571 0, cyls
, heads
, secs
,
4572 translation
== BIOS_ATA_TRANSLATION_LBA
?
4574 translation
== BIOS_ATA_TRANSLATION_NONE
?
4575 ",trans=none" : "");
4578 case QEMU_OPTION_numa
:
4579 if (nb_numa_nodes
>= MAX_NODES
) {
4580 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4585 case QEMU_OPTION_nographic
:
4588 #ifdef CONFIG_CURSES
4589 case QEMU_OPTION_curses
:
4593 case QEMU_OPTION_portrait
:
4596 case QEMU_OPTION_kernel
:
4597 kernel_filename
= optarg
;
4599 case QEMU_OPTION_append
:
4600 kernel_cmdline
= optarg
;
4602 case QEMU_OPTION_cdrom
:
4603 drive_add(optarg
, CDROM_ALIAS
);
4605 case QEMU_OPTION_boot
:
4606 boot_devices
= optarg
;
4607 /* We just do some generic consistency checks */
4609 /* Could easily be extended to 64 devices if needed */
4612 boot_devices_bitmap
= 0;
4613 for (p
= boot_devices
; *p
!= '\0'; p
++) {
4614 /* Allowed boot devices are:
4615 * a b : floppy disk drives
4616 * c ... f : IDE disk drives
4617 * g ... m : machine implementation dependant drives
4618 * n ... p : network devices
4619 * It's up to each machine implementation to check
4620 * if the given boot devices match the actual hardware
4621 * implementation and firmware features.
4623 if (*p
< 'a' || *p
> 'q') {
4624 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
4627 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
4629 "Boot device '%c' was given twice\n",*p
);
4632 boot_devices_bitmap
|= 1 << (*p
- 'a');
4636 case QEMU_OPTION_fda
:
4637 case QEMU_OPTION_fdb
:
4638 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4641 case QEMU_OPTION_no_fd_bootchk
:
4645 case QEMU_OPTION_net
:
4646 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4647 fprintf(stderr
, "qemu: too many network clients\n");
4650 net_clients
[nb_net_clients
] = optarg
;
4654 case QEMU_OPTION_tftp
:
4655 tftp_prefix
= optarg
;
4657 case QEMU_OPTION_bootp
:
4658 bootp_filename
= optarg
;
4661 case QEMU_OPTION_smb
:
4662 net_slirp_smb(optarg
);
4665 case QEMU_OPTION_redir
:
4666 net_slirp_redir(NULL
, optarg
);
4669 case QEMU_OPTION_bt
:
4670 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
4671 fprintf(stderr
, "qemu: too many bluetooth options\n");
4674 bt_opts
[nb_bt_opts
++] = optarg
;
4677 case QEMU_OPTION_audio_help
:
4681 case QEMU_OPTION_soundhw
:
4682 select_soundhw (optarg
);
4688 case QEMU_OPTION_version
:
4692 case QEMU_OPTION_m
: {
4696 value
= strtoul(optarg
, &ptr
, 10);
4698 case 0: case 'M': case 'm':
4705 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4709 /* On 32-bit hosts, QEMU is limited by virtual address space */
4710 if (value
> (2047 << 20)
4711 #ifndef CONFIG_KQEMU
4712 && HOST_LONG_BITS
== 32
4715 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
4718 if (value
!= (uint64_t)(ram_addr_t
)value
) {
4719 fprintf(stderr
, "qemu: ram size too large\n");
4728 const CPULogItem
*item
;
4730 mask
= cpu_str_to_log_mask(optarg
);
4732 printf("Log items (comma separated):\n");
4733 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
4734 printf("%-10s %s\n", item
->name
, item
->help
);
4741 #ifdef CONFIG_GDBSTUB
4743 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
4745 case QEMU_OPTION_gdb
:
4746 gdbstub_dev
= optarg
;
4752 case QEMU_OPTION_bios
:
4755 case QEMU_OPTION_singlestep
:
4763 keyboard_layout
= optarg
;
4766 case QEMU_OPTION_localtime
:
4769 case QEMU_OPTION_vga
:
4770 select_vgahw (optarg
);
4772 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
4778 w
= strtol(p
, (char **)&p
, 10);
4781 fprintf(stderr
, "qemu: invalid resolution or depth\n");
4787 h
= strtol(p
, (char **)&p
, 10);
4792 depth
= strtol(p
, (char **)&p
, 10);
4793 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
4794 depth
!= 24 && depth
!= 32)
4796 } else if (*p
== '\0') {
4797 depth
= graphic_depth
;
4804 graphic_depth
= depth
;
4808 case QEMU_OPTION_echr
:
4811 term_escape_char
= strtol(optarg
, &r
, 0);
4813 printf("Bad argument to echr\n");
4816 case QEMU_OPTION_monitor
:
4817 monitor_device
= optarg
;
4819 case QEMU_OPTION_serial
:
4820 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
4821 fprintf(stderr
, "qemu: too many serial ports\n");
4824 serial_devices
[serial_device_index
] = optarg
;
4825 serial_device_index
++;
4827 case QEMU_OPTION_virtiocon
:
4828 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
4829 fprintf(stderr
, "qemu: too many virtio consoles\n");
4832 virtio_consoles
[virtio_console_index
] = optarg
;
4833 virtio_console_index
++;
4835 case QEMU_OPTION_parallel
:
4836 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
4837 fprintf(stderr
, "qemu: too many parallel ports\n");
4840 parallel_devices
[parallel_device_index
] = optarg
;
4841 parallel_device_index
++;
4843 case QEMU_OPTION_loadvm
:
4846 case QEMU_OPTION_full_screen
:
4850 case QEMU_OPTION_no_frame
:
4853 case QEMU_OPTION_alt_grab
:
4856 case QEMU_OPTION_no_quit
:
4859 case QEMU_OPTION_sdl
:
4863 case QEMU_OPTION_pidfile
:
4867 case QEMU_OPTION_win2k_hack
:
4868 win2k_install_hack
= 1;
4870 case QEMU_OPTION_rtc_td_hack
:
4873 case QEMU_OPTION_acpitable
:
4874 if(acpi_table_add(optarg
) < 0) {
4875 fprintf(stderr
, "Wrong acpi table provided\n");
4879 case QEMU_OPTION_smbios
:
4880 if(smbios_entry_add(optarg
) < 0) {
4881 fprintf(stderr
, "Wrong smbios provided\n");
4887 case QEMU_OPTION_no_kqemu
:
4890 case QEMU_OPTION_kernel_kqemu
:
4895 case QEMU_OPTION_enable_kvm
:
4902 case QEMU_OPTION_usb
:
4905 case QEMU_OPTION_usbdevice
:
4907 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
4908 fprintf(stderr
, "Too many USB devices\n");
4911 usb_devices
[usb_devices_index
] = optarg
;
4912 usb_devices_index
++;
4914 case QEMU_OPTION_smp
:
4915 smp_cpus
= atoi(optarg
);
4917 fprintf(stderr
, "Invalid number of CPUs\n");
4921 case QEMU_OPTION_vnc
:
4922 vnc_display
= optarg
;
4925 case QEMU_OPTION_no_acpi
:
4928 case QEMU_OPTION_no_hpet
:
4932 case QEMU_OPTION_no_reboot
:
4935 case QEMU_OPTION_no_shutdown
:
4938 case QEMU_OPTION_show_cursor
:
4941 case QEMU_OPTION_uuid
:
4942 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
4943 fprintf(stderr
, "Fail to parse UUID string."
4944 " Wrong format.\n");
4949 case QEMU_OPTION_daemonize
:
4953 case QEMU_OPTION_option_rom
:
4954 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
4955 fprintf(stderr
, "Too many option ROMs\n");
4958 option_rom
[nb_option_roms
] = optarg
;
4961 #if defined(TARGET_ARM) || defined(TARGET_M68K)
4962 case QEMU_OPTION_semihosting
:
4963 semihosting_enabled
= 1;
4966 case QEMU_OPTION_name
:
4969 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
4970 case QEMU_OPTION_prom_env
:
4971 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
4972 fprintf(stderr
, "Too many prom variables\n");
4975 prom_envs
[nb_prom_envs
] = optarg
;
4980 case QEMU_OPTION_old_param
:
4984 case QEMU_OPTION_clock
:
4985 configure_alarms(optarg
);
4987 case QEMU_OPTION_startdate
:
4990 time_t rtc_start_date
;
4991 if (!strcmp(optarg
, "now")) {
4992 rtc_date_offset
= -1;
4994 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5002 } else if (sscanf(optarg
, "%d-%d-%d",
5005 &tm
.tm_mday
) == 3) {
5014 rtc_start_date
= mktimegm(&tm
);
5015 if (rtc_start_date
== -1) {
5017 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5018 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5021 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5025 case QEMU_OPTION_tb_size
:
5026 tb_size
= strtol(optarg
, NULL
, 0);
5030 case QEMU_OPTION_icount
:
5032 if (strcmp(optarg
, "auto") == 0) {
5033 icount_time_shift
= -1;
5035 icount_time_shift
= strtol(optarg
, NULL
, 0);
5038 case QEMU_OPTION_incoming
:
5042 case QEMU_OPTION_chroot
:
5043 chroot_dir
= optarg
;
5045 case QEMU_OPTION_runas
:
5053 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5054 if (kvm_allowed
&& kqemu_allowed
) {
5056 "You can not enable both KVM and kqemu at the same time\n");
5061 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5062 if (smp_cpus
> machine
->max_cpus
) {
5063 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5064 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5070 if (serial_device_index
== 0)
5071 serial_devices
[0] = "stdio";
5072 if (parallel_device_index
== 0)
5073 parallel_devices
[0] = "null";
5074 if (strncmp(monitor_device
, "vc", 2) == 0)
5075 monitor_device
= "stdio";
5082 if (pipe(fds
) == -1)
5093 len
= read(fds
[0], &status
, 1);
5094 if (len
== -1 && (errno
== EINTR
))
5099 else if (status
== 1) {
5100 fprintf(stderr
, "Could not acquire pidfile\n");
5117 signal(SIGTSTP
, SIG_IGN
);
5118 signal(SIGTTOU
, SIG_IGN
);
5119 signal(SIGTTIN
, SIG_IGN
);
5122 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5125 write(fds
[1], &status
, 1);
5127 fprintf(stderr
, "Could not acquire pid file\n");
5136 linux_boot
= (kernel_filename
!= NULL
);
5137 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5139 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5140 fprintf(stderr
, "-append only allowed with -kernel option\n");
5144 if (!linux_boot
&& initrd_filename
!= NULL
) {
5145 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5149 /* boot to floppy or the default cd if no hard disk defined yet */
5150 if (!boot_devices
[0]) {
5151 boot_devices
= "cad";
5153 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5156 if (init_timer_alarm() < 0) {
5157 fprintf(stderr
, "could not initialize alarm timer\n");
5160 if (use_icount
&& icount_time_shift
< 0) {
5162 /* 125MIPS seems a reasonable initial guess at the guest speed.
5163 It will be corrected fairly quickly anyway. */
5164 icount_time_shift
= 3;
5165 init_icount_adjust();
5172 /* init network clients */
5173 if (nb_net_clients
== 0) {
5174 /* if no clients, we use a default config */
5175 net_clients
[nb_net_clients
++] = "nic";
5177 net_clients
[nb_net_clients
++] = "user";
5181 for(i
= 0;i
< nb_net_clients
; i
++) {
5182 if (net_client_parse(net_clients
[i
]) < 0)
5188 /* XXX: this should be moved in the PC machine instantiation code */
5189 if (net_boot
!= 0) {
5191 for (i
= 0; i
< nb_nics
&& i
< 4; i
++) {
5192 const char *model
= nd_table
[i
].model
;
5194 if (net_boot
& (1 << i
)) {
5197 snprintf(buf
, sizeof(buf
), "%s/pxe-%s.bin", bios_dir
, model
);
5198 if (get_image_size(buf
) > 0) {
5199 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5200 fprintf(stderr
, "Too many option ROMs\n");
5203 option_rom
[nb_option_roms
] = strdup(buf
);
5210 fprintf(stderr
, "No valid PXE rom found for network device\n");
5216 /* init the bluetooth world */
5217 for (i
= 0; i
< nb_bt_opts
; i
++)
5218 if (bt_parse(bt_opts
[i
]))
5221 /* init the memory */
5223 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5226 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5227 guest ram allocation. It needs to go away. */
5228 if (kqemu_allowed
) {
5229 kqemu_phys_ram_size
= ram_size
+ VGA_RAM_SIZE
+ 4 * 1024 * 1024;
5230 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5231 if (!kqemu_phys_ram_base
) {
5232 fprintf(stderr
, "Could not allocate physical memory\n");
5238 /* init the dynamic translator */
5239 cpu_exec_init_all(tb_size
* 1024 * 1024);
5244 /* we always create the cdrom drive, even if no disk is there */
5246 if (nb_drives_opt
< MAX_DRIVES
)
5247 drive_add(NULL
, CDROM_ALIAS
);
5249 /* we always create at least one floppy */
5251 if (nb_drives_opt
< MAX_DRIVES
)
5252 drive_add(NULL
, FD_ALIAS
, 0);
5254 /* we always create one sd slot, even if no card is in it */
5256 if (nb_drives_opt
< MAX_DRIVES
)
5257 drive_add(NULL
, SD_ALIAS
);
5259 /* open the virtual block devices */
5261 for(i
= 0; i
< nb_drives_opt
; i
++)
5262 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5265 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5266 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5269 /* must be after terminal init, SDL library changes signal handlers */
5273 /* Maintain compatibility with multiple stdio monitors */
5274 if (!strcmp(monitor_device
,"stdio")) {
5275 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5276 const char *devname
= serial_devices
[i
];
5277 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5278 monitor_device
= NULL
;
5280 } else if (devname
&& !strcmp(devname
,"stdio")) {
5281 monitor_device
= NULL
;
5282 serial_devices
[i
] = "mon:stdio";
5288 if (nb_numa_nodes
> 0) {
5291 if (nb_numa_nodes
> smp_cpus
) {
5292 nb_numa_nodes
= smp_cpus
;
5295 /* If no memory size if given for any node, assume the default case
5296 * and distribute the available memory equally across all nodes
5298 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5299 if (node_mem
[i
] != 0)
5302 if (i
== nb_numa_nodes
) {
5303 uint64_t usedmem
= 0;
5305 /* On Linux, the each node's border has to be 8MB aligned,
5306 * the final node gets the rest.
5308 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5309 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5310 usedmem
+= node_mem
[i
];
5312 node_mem
[i
] = ram_size
- usedmem
;
5315 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5316 if (node_cpumask
[i
] != 0)
5319 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5320 * must cope with this anyway, because there are BIOSes out there in
5321 * real machines which also use this scheme.
5323 if (i
== nb_numa_nodes
) {
5324 for (i
= 0; i
< smp_cpus
; i
++) {
5325 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5330 if (kvm_enabled()) {
5333 ret
= kvm_init(smp_cpus
);
5335 fprintf(stderr
, "failed to initialize KVM\n");
5340 if (monitor_device
) {
5341 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5343 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5348 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5349 const char *devname
= serial_devices
[i
];
5350 if (devname
&& strcmp(devname
, "none")) {
5352 snprintf(label
, sizeof(label
), "serial%d", i
);
5353 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5354 if (!serial_hds
[i
]) {
5355 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5362 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5363 const char *devname
= parallel_devices
[i
];
5364 if (devname
&& strcmp(devname
, "none")) {
5366 snprintf(label
, sizeof(label
), "parallel%d", i
);
5367 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5368 if (!parallel_hds
[i
]) {
5369 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5376 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5377 const char *devname
= virtio_consoles
[i
];
5378 if (devname
&& strcmp(devname
, "none")) {
5380 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5381 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5382 if (!virtcon_hds
[i
]) {
5383 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5390 machine
->init(ram_size
, vga_ram_size
, boot_devices
,
5391 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5394 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5395 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5396 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5402 current_machine
= machine
;
5404 /* Set KVM's vcpu state to qemu's initial CPUState. */
5405 if (kvm_enabled()) {
5408 ret
= kvm_sync_vcpus();
5410 fprintf(stderr
, "failed to initialize vcpus\n");
5415 /* init USB devices */
5417 for(i
= 0; i
< usb_devices_index
; i
++) {
5418 if (usb_device_add(usb_devices
[i
], 0) < 0) {
5419 fprintf(stderr
, "Warning: could not add USB device %s\n",
5426 dumb_display_init();
5427 /* just use the first displaystate for the moment */
5432 fprintf(stderr
, "fatal: -nographic can't be used with -curses\n");
5436 #if defined(CONFIG_CURSES)
5438 /* At the moment curses cannot be used with other displays */
5439 curses_display_init(ds
, full_screen
);
5443 if (vnc_display
!= NULL
) {
5444 vnc_display_init(ds
);
5445 if (vnc_display_open(ds
, vnc_display
) < 0)
5448 #if defined(CONFIG_SDL)
5449 if (sdl
|| !vnc_display
)
5450 sdl_display_init(ds
, full_screen
, no_frame
);
5451 #elif defined(CONFIG_COCOA)
5452 if (sdl
|| !vnc_display
)
5453 cocoa_display_init(ds
, full_screen
);
5459 dcl
= ds
->listeners
;
5460 while (dcl
!= NULL
) {
5461 if (dcl
->dpy_refresh
!= NULL
) {
5462 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5463 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5468 if (nographic
|| (vnc_display
&& !sdl
)) {
5469 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5470 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5473 text_consoles_set_display(display_state
);
5474 qemu_chr_initial_reset();
5476 if (monitor_device
&& monitor_hd
)
5477 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
5479 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5480 const char *devname
= serial_devices
[i
];
5481 if (devname
&& strcmp(devname
, "none")) {
5483 snprintf(label
, sizeof(label
), "serial%d", i
);
5484 if (strstart(devname
, "vc", 0))
5485 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5489 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5490 const char *devname
= parallel_devices
[i
];
5491 if (devname
&& strcmp(devname
, "none")) {
5493 snprintf(label
, sizeof(label
), "parallel%d", i
);
5494 if (strstart(devname
, "vc", 0))
5495 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5499 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5500 const char *devname
= virtio_consoles
[i
];
5501 if (virtcon_hds
[i
] && devname
) {
5503 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5504 if (strstart(devname
, "vc", 0))
5505 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5509 #ifdef CONFIG_GDBSTUB
5510 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5511 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5518 do_loadvm(cur_mon
, loadvm
);
5521 autostart
= 0; /* fixme how to deal with -daemonize */
5522 qemu_start_incoming_migration(incoming
);
5534 len
= write(fds
[1], &status
, 1);
5535 if (len
== -1 && (errno
== EINTR
))
5542 TFR(fd
= open("/dev/null", O_RDWR
));
5548 pwd
= getpwnam(run_as
);
5550 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5556 if (chroot(chroot_dir
) < 0) {
5557 fprintf(stderr
, "chroot failed\n");
5564 if (setgid(pwd
->pw_gid
) < 0) {
5565 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5568 if (setuid(pwd
->pw_uid
) < 0) {
5569 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5572 if (setuid(0) != -1) {
5573 fprintf(stderr
, "Dropping privileges failed\n");