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"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #if defined(__NetBSD__)
48 #include <net/if_tap.h>
51 #include <linux/if_tun.h>
53 #include <arpa/inet.h>
56 #include <sys/select.h>
59 #if defined(__FreeBSD__) || defined(__DragonFly__)
64 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65 #include <freebsd/stdlib.h>
70 #include <linux/rtc.h>
72 /* For the benefit of older linux systems which don't supply it,
73 we use a local copy of hpet.h. */
74 /* #include <linux/hpet.h> */
77 #include <linux/ppdev.h>
78 #include <linux/parport.h>
82 #include <sys/ethernet.h>
83 #include <sys/sockio.h>
84 #include <netinet/arp.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/ip_icmp.h> // must come after ip.h
89 #include <netinet/udp.h>
90 #include <netinet/tcp.h>
98 #if defined(__OpenBSD__)
102 #if defined(CONFIG_VDE)
103 #include <libvdeplug.h>
109 #include <sys/timeb.h>
110 #include <mmsystem.h>
111 #define getopt_long_only getopt_long
112 #define memalign(align, size) malloc(size)
116 #if defined(__APPLE__) || defined(main)
118 int qemu_main(int argc
, char **argv
, char **envp
);
119 int main(int argc
, char **argv
)
121 return qemu_main(argc
, argv
, NULL
);
124 #define main qemu_main
126 #endif /* CONFIG_SDL */
130 #define main qemu_main
131 #endif /* CONFIG_COCOA */
134 #include "hw/boards.h"
136 #include "hw/pcmcia.h"
138 #include "hw/audiodev.h"
142 #include "hw/watchdog.h"
143 #include "hw/smbios.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
156 #include "audio/audio.h"
157 #include "migration.h"
160 #include "qemu-option.h"
164 #include "exec-all.h"
166 #include "qemu_socket.h"
168 #if defined(CONFIG_SLIRP)
169 #include "libslirp.h"
172 //#define DEBUG_UNUSED_IOPORT
173 //#define DEBUG_IOPORT
175 //#define DEBUG_SLIRP
179 # define LOG_IOPORT(...) qemu_log_mask(CPU_LOG_IOPORT, ## __VA_ARGS__)
181 # define LOG_IOPORT(...) do { } while (0)
184 #define DEFAULT_RAM_SIZE 128
186 /* Max number of USB devices that can be specified on the commandline. */
187 #define MAX_USB_CMDLINE 8
189 /* Max number of bluetooth switches on the commandline. */
190 #define MAX_BT_CMDLINE 10
192 /* XXX: use a two level table to limit memory usage */
193 #define MAX_IOPORTS 65536
195 static const char *data_dir
;
196 const char *bios_name
= NULL
;
197 static void *ioport_opaque
[MAX_IOPORTS
];
198 static IOPortReadFunc
*ioport_read_table
[3][MAX_IOPORTS
];
199 static IOPortWriteFunc
*ioport_write_table
[3][MAX_IOPORTS
];
200 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
201 to store the VM snapshots */
202 DriveInfo drives_table
[MAX_DRIVES
+1];
204 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
205 static DisplayState
*display_state
;
206 DisplayType display_type
= DT_DEFAULT
;
207 const char* keyboard_layout
= NULL
;
208 int64_t ticks_per_sec
;
211 NICInfo nd_table
[MAX_NICS
];
213 static int autostart
;
214 static int rtc_utc
= 1;
215 static int rtc_date_offset
= -1; /* -1 means no change */
216 int cirrus_vga_enabled
= 1;
217 int std_vga_enabled
= 0;
218 int vmsvga_enabled
= 0;
219 int xenfb_enabled
= 0;
221 int graphic_width
= 1024;
222 int graphic_height
= 768;
223 int graphic_depth
= 8;
225 int graphic_width
= 800;
226 int graphic_height
= 600;
227 int graphic_depth
= 15;
229 static int full_screen
= 0;
231 static int no_frame
= 0;
234 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
235 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
236 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
238 int win2k_install_hack
= 0;
244 const char *vnc_display
;
245 int acpi_enabled
= 1;
247 int no_virtio_balloon
= 0;
252 int graphic_rotate
= 0;
256 WatchdogTimerModel
*watchdog
= NULL
;
257 int watchdog_action
= WDT_RESET
;
258 const char *option_rom
[MAX_OPTION_ROMS
];
260 int semihosting_enabled
= 0;
264 const char *qemu_name
;
266 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
267 unsigned int nb_prom_envs
= 0;
268 const char *prom_envs
[MAX_PROM_ENVS
];
271 struct drive_opt drives_opt
[MAX_DRIVES
];
274 uint64_t node_mem
[MAX_NODES
];
275 uint64_t node_cpumask
[MAX_NODES
];
277 static CPUState
*cur_cpu
;
278 static CPUState
*next_cpu
;
279 static int timer_alarm_pending
= 1;
280 /* Conversion factor from emulated instructions to virtual clock ticks. */
281 static int icount_time_shift
;
282 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
283 #define MAX_ICOUNT_SHIFT 10
284 /* Compensate for varying guest execution speed. */
285 static int64_t qemu_icount_bias
;
286 static QEMUTimer
*icount_rt_timer
;
287 static QEMUTimer
*icount_vm_timer
;
288 static QEMUTimer
*nographic_timer
;
290 uint8_t qemu_uuid
[16];
292 /***********************************************************/
293 /* x86 ISA bus support */
295 target_phys_addr_t isa_mem_base
= 0;
298 static IOPortReadFunc default_ioport_readb
, default_ioport_readw
, default_ioport_readl
;
299 static IOPortWriteFunc default_ioport_writeb
, default_ioport_writew
, default_ioport_writel
;
301 static uint32_t ioport_read(int index
, uint32_t address
)
303 static IOPortReadFunc
*default_func
[3] = {
304 default_ioport_readb
,
305 default_ioport_readw
,
308 IOPortReadFunc
*func
= ioport_read_table
[index
][address
];
310 func
= default_func
[index
];
311 return func(ioport_opaque
[address
], address
);
314 static void ioport_write(int index
, uint32_t address
, uint32_t data
)
316 static IOPortWriteFunc
*default_func
[3] = {
317 default_ioport_writeb
,
318 default_ioport_writew
,
319 default_ioport_writel
321 IOPortWriteFunc
*func
= ioport_write_table
[index
][address
];
323 func
= default_func
[index
];
324 func(ioport_opaque
[address
], address
, data
);
327 static uint32_t default_ioport_readb(void *opaque
, uint32_t address
)
329 #ifdef DEBUG_UNUSED_IOPORT
330 fprintf(stderr
, "unused inb: port=0x%04x\n", address
);
335 static void default_ioport_writeb(void *opaque
, uint32_t address
, uint32_t data
)
337 #ifdef DEBUG_UNUSED_IOPORT
338 fprintf(stderr
, "unused outb: port=0x%04x data=0x%02x\n", address
, data
);
342 /* default is to make two byte accesses */
343 static uint32_t default_ioport_readw(void *opaque
, uint32_t address
)
346 data
= ioport_read(0, address
);
347 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
348 data
|= ioport_read(0, address
) << 8;
352 static void default_ioport_writew(void *opaque
, uint32_t address
, uint32_t data
)
354 ioport_write(0, address
, data
& 0xff);
355 address
= (address
+ 1) & (MAX_IOPORTS
- 1);
356 ioport_write(0, address
, (data
>> 8) & 0xff);
359 static uint32_t default_ioport_readl(void *opaque
, uint32_t address
)
361 #ifdef DEBUG_UNUSED_IOPORT
362 fprintf(stderr
, "unused inl: port=0x%04x\n", address
);
367 static void default_ioport_writel(void *opaque
, uint32_t address
, uint32_t data
)
369 #ifdef DEBUG_UNUSED_IOPORT
370 fprintf(stderr
, "unused outl: port=0x%04x data=0x%02x\n", address
, data
);
374 /* size is the word size in byte */
375 int register_ioport_read(int start
, int length
, int size
,
376 IOPortReadFunc
*func
, void *opaque
)
382 } else if (size
== 2) {
384 } else if (size
== 4) {
387 hw_error("register_ioport_read: invalid size");
390 for(i
= start
; i
< start
+ length
; i
+= size
) {
391 ioport_read_table
[bsize
][i
] = func
;
392 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
393 hw_error("register_ioport_read: invalid opaque");
394 ioport_opaque
[i
] = opaque
;
399 /* size is the word size in byte */
400 int register_ioport_write(int start
, int length
, int size
,
401 IOPortWriteFunc
*func
, void *opaque
)
407 } else if (size
== 2) {
409 } else if (size
== 4) {
412 hw_error("register_ioport_write: invalid size");
415 for(i
= start
; i
< start
+ length
; i
+= size
) {
416 ioport_write_table
[bsize
][i
] = func
;
417 if (ioport_opaque
[i
] != NULL
&& ioport_opaque
[i
] != opaque
)
418 hw_error("register_ioport_write: invalid opaque");
419 ioport_opaque
[i
] = opaque
;
424 void isa_unassign_ioport(int start
, int length
)
428 for(i
= start
; i
< start
+ length
; i
++) {
429 ioport_read_table
[0][i
] = default_ioport_readb
;
430 ioport_read_table
[1][i
] = default_ioport_readw
;
431 ioport_read_table
[2][i
] = default_ioport_readl
;
433 ioport_write_table
[0][i
] = default_ioport_writeb
;
434 ioport_write_table
[1][i
] = default_ioport_writew
;
435 ioport_write_table
[2][i
] = default_ioport_writel
;
437 ioport_opaque
[i
] = NULL
;
441 /***********************************************************/
443 void cpu_outb(CPUState
*env
, int addr
, int val
)
445 LOG_IOPORT("outb: %04x %02x\n", addr
, val
);
446 ioport_write(0, addr
, val
);
449 env
->last_io_time
= cpu_get_time_fast();
453 void cpu_outw(CPUState
*env
, int addr
, int val
)
455 LOG_IOPORT("outw: %04x %04x\n", addr
, val
);
456 ioport_write(1, addr
, val
);
459 env
->last_io_time
= cpu_get_time_fast();
463 void cpu_outl(CPUState
*env
, int addr
, int val
)
465 LOG_IOPORT("outl: %04x %08x\n", addr
, val
);
466 ioport_write(2, addr
, val
);
469 env
->last_io_time
= cpu_get_time_fast();
473 int cpu_inb(CPUState
*env
, int addr
)
476 val
= ioport_read(0, addr
);
477 LOG_IOPORT("inb : %04x %02x\n", addr
, val
);
480 env
->last_io_time
= cpu_get_time_fast();
485 int cpu_inw(CPUState
*env
, int addr
)
488 val
= ioport_read(1, addr
);
489 LOG_IOPORT("inw : %04x %04x\n", addr
, val
);
492 env
->last_io_time
= cpu_get_time_fast();
497 int cpu_inl(CPUState
*env
, int addr
)
500 val
= ioport_read(2, addr
);
501 LOG_IOPORT("inl : %04x %08x\n", addr
, val
);
504 env
->last_io_time
= cpu_get_time_fast();
509 /***********************************************************/
510 void hw_error(const char *fmt
, ...)
516 fprintf(stderr
, "qemu: hardware error: ");
517 vfprintf(stderr
, fmt
, ap
);
518 fprintf(stderr
, "\n");
519 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
520 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
522 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
524 cpu_dump_state(env
, stderr
, fprintf
, 0);
534 static QEMUBalloonEvent
*qemu_balloon_event
;
535 void *qemu_balloon_event_opaque
;
537 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
539 qemu_balloon_event
= func
;
540 qemu_balloon_event_opaque
= opaque
;
543 void qemu_balloon(ram_addr_t target
)
545 if (qemu_balloon_event
)
546 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
549 ram_addr_t
qemu_balloon_status(void)
551 if (qemu_balloon_event
)
552 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
556 /***********************************************************/
559 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
560 static void *qemu_put_kbd_event_opaque
;
561 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
562 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
564 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
566 qemu_put_kbd_event_opaque
= opaque
;
567 qemu_put_kbd_event
= func
;
570 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
571 void *opaque
, int absolute
,
574 QEMUPutMouseEntry
*s
, *cursor
;
576 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
578 s
->qemu_put_mouse_event
= func
;
579 s
->qemu_put_mouse_event_opaque
= opaque
;
580 s
->qemu_put_mouse_event_absolute
= absolute
;
581 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
584 if (!qemu_put_mouse_event_head
) {
585 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
589 cursor
= qemu_put_mouse_event_head
;
590 while (cursor
->next
!= NULL
)
591 cursor
= cursor
->next
;
594 qemu_put_mouse_event_current
= s
;
599 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
601 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
603 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
606 cursor
= qemu_put_mouse_event_head
;
607 while (cursor
!= NULL
&& cursor
!= entry
) {
609 cursor
= cursor
->next
;
612 if (cursor
== NULL
) // does not exist or list empty
614 else if (prev
== NULL
) { // entry is head
615 qemu_put_mouse_event_head
= cursor
->next
;
616 if (qemu_put_mouse_event_current
== entry
)
617 qemu_put_mouse_event_current
= cursor
->next
;
618 qemu_free(entry
->qemu_put_mouse_event_name
);
623 prev
->next
= entry
->next
;
625 if (qemu_put_mouse_event_current
== entry
)
626 qemu_put_mouse_event_current
= prev
;
628 qemu_free(entry
->qemu_put_mouse_event_name
);
632 void kbd_put_keycode(int keycode
)
634 if (qemu_put_kbd_event
) {
635 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
639 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
641 QEMUPutMouseEvent
*mouse_event
;
642 void *mouse_event_opaque
;
645 if (!qemu_put_mouse_event_current
) {
650 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
652 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
655 if (graphic_rotate
) {
656 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
659 width
= graphic_width
- 1;
660 mouse_event(mouse_event_opaque
,
661 width
- dy
, dx
, dz
, buttons_state
);
663 mouse_event(mouse_event_opaque
,
664 dx
, dy
, dz
, buttons_state
);
668 int kbd_mouse_is_absolute(void)
670 if (!qemu_put_mouse_event_current
)
673 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
676 void do_info_mice(Monitor
*mon
)
678 QEMUPutMouseEntry
*cursor
;
681 if (!qemu_put_mouse_event_head
) {
682 monitor_printf(mon
, "No mouse devices connected\n");
686 monitor_printf(mon
, "Mouse devices available:\n");
687 cursor
= qemu_put_mouse_event_head
;
688 while (cursor
!= NULL
) {
689 monitor_printf(mon
, "%c Mouse #%d: %s\n",
690 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
691 index
, cursor
->qemu_put_mouse_event_name
);
693 cursor
= cursor
->next
;
697 void do_mouse_set(Monitor
*mon
, int index
)
699 QEMUPutMouseEntry
*cursor
;
702 if (!qemu_put_mouse_event_head
) {
703 monitor_printf(mon
, "No mouse devices connected\n");
707 cursor
= qemu_put_mouse_event_head
;
708 while (cursor
!= NULL
&& index
!= i
) {
710 cursor
= cursor
->next
;
714 qemu_put_mouse_event_current
= cursor
;
716 monitor_printf(mon
, "Mouse at given index not found\n");
719 /* compute with 96 bit intermediate result: (a*b)/c */
720 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
725 #ifdef WORDS_BIGENDIAN
735 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
736 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
739 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
743 /***********************************************************/
744 /* real time host monotonic timer */
746 #define QEMU_TIMER_BASE 1000000000LL
750 static int64_t clock_freq
;
752 static void init_get_clock(void)
756 ret
= QueryPerformanceFrequency(&freq
);
758 fprintf(stderr
, "Could not calibrate ticks\n");
761 clock_freq
= freq
.QuadPart
;
764 static int64_t get_clock(void)
767 QueryPerformanceCounter(&ti
);
768 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
773 static int use_rt_clock
;
775 static void init_get_clock(void)
778 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
779 || defined(__DragonFly__)
782 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
789 static int64_t get_clock(void)
791 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
792 || defined(__DragonFly__)
795 clock_gettime(CLOCK_MONOTONIC
, &ts
);
796 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
800 /* XXX: using gettimeofday leads to problems if the date
801 changes, so it should be avoided. */
803 gettimeofday(&tv
, NULL
);
804 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
809 /* Return the virtual CPU time, based on the instruction counter. */
810 static int64_t cpu_get_icount(void)
813 CPUState
*env
= cpu_single_env
;;
814 icount
= qemu_icount
;
817 fprintf(stderr
, "Bad clock read\n");
818 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
820 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
823 /***********************************************************/
824 /* guest cycle counter */
826 static int64_t cpu_ticks_prev
;
827 static int64_t cpu_ticks_offset
;
828 static int64_t cpu_clock_offset
;
829 static int cpu_ticks_enabled
;
831 /* return the host CPU cycle counter and handle stop/restart */
832 int64_t cpu_get_ticks(void)
835 return cpu_get_icount();
837 if (!cpu_ticks_enabled
) {
838 return cpu_ticks_offset
;
841 ticks
= cpu_get_real_ticks();
842 if (cpu_ticks_prev
> ticks
) {
843 /* Note: non increasing ticks may happen if the host uses
845 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
847 cpu_ticks_prev
= ticks
;
848 return ticks
+ cpu_ticks_offset
;
852 /* return the host CPU monotonic timer and handle stop/restart */
853 static int64_t cpu_get_clock(void)
856 if (!cpu_ticks_enabled
) {
857 return cpu_clock_offset
;
860 return ti
+ cpu_clock_offset
;
864 /* enable cpu_get_ticks() */
865 void cpu_enable_ticks(void)
867 if (!cpu_ticks_enabled
) {
868 cpu_ticks_offset
-= cpu_get_real_ticks();
869 cpu_clock_offset
-= get_clock();
870 cpu_ticks_enabled
= 1;
874 /* disable cpu_get_ticks() : the clock is stopped. You must not call
875 cpu_get_ticks() after that. */
876 void cpu_disable_ticks(void)
878 if (cpu_ticks_enabled
) {
879 cpu_ticks_offset
= cpu_get_ticks();
880 cpu_clock_offset
= cpu_get_clock();
881 cpu_ticks_enabled
= 0;
885 /***********************************************************/
888 #define QEMU_TIMER_REALTIME 0
889 #define QEMU_TIMER_VIRTUAL 1
893 /* XXX: add frequency */
901 struct QEMUTimer
*next
;
904 struct qemu_alarm_timer
{
908 int (*start
)(struct qemu_alarm_timer
*t
);
909 void (*stop
)(struct qemu_alarm_timer
*t
);
910 void (*rearm
)(struct qemu_alarm_timer
*t
);
914 #define ALARM_FLAG_DYNTICKS 0x1
915 #define ALARM_FLAG_EXPIRED 0x2
917 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
919 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
922 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
924 if (!alarm_has_dynticks(t
))
930 /* TODO: MIN_TIMER_REARM_US should be optimized */
931 #define MIN_TIMER_REARM_US 250
933 static struct qemu_alarm_timer
*alarm_timer
;
937 struct qemu_alarm_win32
{
940 } alarm_win32_data
= {0, -1};
942 static int win32_start_timer(struct qemu_alarm_timer
*t
);
943 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
944 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
948 static int unix_start_timer(struct qemu_alarm_timer
*t
);
949 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
953 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
954 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
955 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
957 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
958 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
960 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
961 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
963 #endif /* __linux__ */
967 /* Correlation between real and virtual time is always going to be
968 fairly approximate, so ignore small variation.
969 When the guest is idle real and virtual time will be aligned in
971 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
973 static void icount_adjust(void)
978 static int64_t last_delta
;
979 /* If the VM is not running, then do nothing. */
983 cur_time
= cpu_get_clock();
984 cur_icount
= qemu_get_clock(vm_clock
);
985 delta
= cur_icount
- cur_time
;
986 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
988 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
989 && icount_time_shift
> 0) {
990 /* The guest is getting too far ahead. Slow time down. */
994 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
995 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
996 /* The guest is getting too far behind. Speed time up. */
1000 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
1003 static void icount_adjust_rt(void * opaque
)
1005 qemu_mod_timer(icount_rt_timer
,
1006 qemu_get_clock(rt_clock
) + 1000);
1010 static void icount_adjust_vm(void * opaque
)
1012 qemu_mod_timer(icount_vm_timer
,
1013 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1017 static void init_icount_adjust(void)
1019 /* Have both realtime and virtual time triggers for speed adjustment.
1020 The realtime trigger catches emulated time passing too slowly,
1021 the virtual time trigger catches emulated time passing too fast.
1022 Realtime triggers occur even when idle, so use them less frequently
1023 than VM triggers. */
1024 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
1025 qemu_mod_timer(icount_rt_timer
,
1026 qemu_get_clock(rt_clock
) + 1000);
1027 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
1028 qemu_mod_timer(icount_vm_timer
,
1029 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
1032 static struct qemu_alarm_timer alarm_timers
[] = {
1035 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
1036 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
1037 /* HPET - if available - is preferred */
1038 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
1039 /* ...otherwise try RTC */
1040 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
1042 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
1044 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
1045 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
1046 {"win32", 0, win32_start_timer
,
1047 win32_stop_timer
, NULL
, &alarm_win32_data
},
1052 static void show_available_alarms(void)
1056 printf("Available alarm timers, in order of precedence:\n");
1057 for (i
= 0; alarm_timers
[i
].name
; i
++)
1058 printf("%s\n", alarm_timers
[i
].name
);
1061 static void configure_alarms(char const *opt
)
1065 int count
= ARRAY_SIZE(alarm_timers
) - 1;
1068 struct qemu_alarm_timer tmp
;
1070 if (!strcmp(opt
, "?")) {
1071 show_available_alarms();
1077 /* Reorder the array */
1078 name
= strtok(arg
, ",");
1080 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
1081 if (!strcmp(alarm_timers
[i
].name
, name
))
1086 fprintf(stderr
, "Unknown clock %s\n", name
);
1095 tmp
= alarm_timers
[i
];
1096 alarm_timers
[i
] = alarm_timers
[cur
];
1097 alarm_timers
[cur
] = tmp
;
1101 name
= strtok(NULL
, ",");
1107 /* Disable remaining timers */
1108 for (i
= cur
; i
< count
; i
++)
1109 alarm_timers
[i
].name
= NULL
;
1111 show_available_alarms();
1116 QEMUClock
*rt_clock
;
1117 QEMUClock
*vm_clock
;
1119 static QEMUTimer
*active_timers
[2];
1121 static QEMUClock
*qemu_new_clock(int type
)
1124 clock
= qemu_mallocz(sizeof(QEMUClock
));
1129 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
1133 ts
= qemu_mallocz(sizeof(QEMUTimer
));
1136 ts
->opaque
= opaque
;
1140 void qemu_free_timer(QEMUTimer
*ts
)
1145 /* stop a timer, but do not dealloc it */
1146 void qemu_del_timer(QEMUTimer
*ts
)
1150 /* NOTE: this code must be signal safe because
1151 qemu_timer_expired() can be called from a signal. */
1152 pt
= &active_timers
[ts
->clock
->type
];
1165 /* modify the current timer so that it will be fired when current_time
1166 >= expire_time. The corresponding callback will be called. */
1167 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
1173 /* add the timer in the sorted list */
1174 /* NOTE: this code must be signal safe because
1175 qemu_timer_expired() can be called from a signal. */
1176 pt
= &active_timers
[ts
->clock
->type
];
1181 if (t
->expire_time
> expire_time
)
1185 ts
->expire_time
= expire_time
;
1189 /* Rearm if necessary */
1190 if (pt
== &active_timers
[ts
->clock
->type
]) {
1191 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
1192 qemu_rearm_alarm_timer(alarm_timer
);
1194 /* Interrupt execution to force deadline recalculation. */
1196 qemu_notify_event();
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
);
1307 static void qemu_event_increment(void);
1310 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1311 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1314 static void host_alarm_handler(int host_signum
)
1318 #define DISP_FREQ 1000
1320 static int64_t delta_min
= INT64_MAX
;
1321 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1323 ti
= qemu_get_clock(vm_clock
);
1324 if (last_clock
!= 0) {
1325 delta
= ti
- last_clock
;
1326 if (delta
< delta_min
)
1328 if (delta
> delta_max
)
1331 if (++count
== DISP_FREQ
) {
1332 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1333 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1334 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1335 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1336 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1338 delta_min
= INT64_MAX
;
1346 if (alarm_has_dynticks(alarm_timer
) ||
1348 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1349 qemu_get_clock(vm_clock
))) ||
1350 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1351 qemu_get_clock(rt_clock
))) {
1352 qemu_event_increment();
1353 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1355 #ifndef CONFIG_IOTHREAD
1357 /* stop the currently executing cpu because a timer occured */
1360 if (next_cpu
->kqemu_enabled
) {
1361 kqemu_cpu_interrupt(next_cpu
);
1366 timer_alarm_pending
= 1;
1367 qemu_notify_event();
1371 static int64_t qemu_next_deadline(void)
1375 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1376 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1377 qemu_get_clock(vm_clock
);
1379 /* To avoid problems with overflow limit this to 2^32. */
1389 #if defined(__linux__) || defined(_WIN32)
1390 static uint64_t qemu_next_deadline_dyntick(void)
1398 delta
= (qemu_next_deadline() + 999) / 1000;
1400 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1401 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1402 qemu_get_clock(rt_clock
))*1000;
1403 if (rtdelta
< delta
)
1407 if (delta
< MIN_TIMER_REARM_US
)
1408 delta
= MIN_TIMER_REARM_US
;
1416 /* Sets a specific flag */
1417 static int fcntl_setfl(int fd
, int flag
)
1421 flags
= fcntl(fd
, F_GETFL
);
1425 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1431 #if defined(__linux__)
1433 #define RTC_FREQ 1024
1435 static void enable_sigio_timer(int fd
)
1437 struct sigaction act
;
1440 sigfillset(&act
.sa_mask
);
1442 act
.sa_handler
= host_alarm_handler
;
1444 sigaction(SIGIO
, &act
, NULL
);
1445 fcntl_setfl(fd
, O_ASYNC
);
1446 fcntl(fd
, F_SETOWN
, getpid());
1449 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1451 struct hpet_info info
;
1454 fd
= open("/dev/hpet", O_RDONLY
);
1459 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1461 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1462 "error, but for better emulation accuracy type:\n"
1463 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1467 /* Check capabilities */
1468 r
= ioctl(fd
, HPET_INFO
, &info
);
1472 /* Enable periodic mode */
1473 r
= ioctl(fd
, HPET_EPI
, 0);
1474 if (info
.hi_flags
&& (r
< 0))
1477 /* Enable interrupt */
1478 r
= ioctl(fd
, HPET_IE_ON
, 0);
1482 enable_sigio_timer(fd
);
1483 t
->priv
= (void *)(long)fd
;
1491 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1493 int fd
= (long)t
->priv
;
1498 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1501 unsigned long current_rtc_freq
= 0;
1503 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1506 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1507 if (current_rtc_freq
!= RTC_FREQ
&&
1508 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1509 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1510 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1511 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1514 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1520 enable_sigio_timer(rtc_fd
);
1522 t
->priv
= (void *)(long)rtc_fd
;
1527 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1529 int rtc_fd
= (long)t
->priv
;
1534 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1538 struct sigaction act
;
1540 sigfillset(&act
.sa_mask
);
1542 act
.sa_handler
= host_alarm_handler
;
1544 sigaction(SIGALRM
, &act
, NULL
);
1547 * Initialize ev struct to 0 to avoid valgrind complaining
1548 * about uninitialized data in timer_create call
1550 memset(&ev
, 0, sizeof(ev
));
1551 ev
.sigev_value
.sival_int
= 0;
1552 ev
.sigev_notify
= SIGEV_SIGNAL
;
1553 ev
.sigev_signo
= SIGALRM
;
1555 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1556 perror("timer_create");
1558 /* disable dynticks */
1559 fprintf(stderr
, "Dynamic Ticks disabled\n");
1564 t
->priv
= (void *)(long)host_timer
;
1569 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1571 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1573 timer_delete(host_timer
);
1576 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1578 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1579 struct itimerspec timeout
;
1580 int64_t nearest_delta_us
= INT64_MAX
;
1583 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1584 !active_timers
[QEMU_TIMER_VIRTUAL
])
1587 nearest_delta_us
= qemu_next_deadline_dyntick();
1589 /* check whether a timer is already running */
1590 if (timer_gettime(host_timer
, &timeout
)) {
1592 fprintf(stderr
, "Internal timer error: aborting\n");
1595 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1596 if (current_us
&& current_us
<= nearest_delta_us
)
1599 timeout
.it_interval
.tv_sec
= 0;
1600 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1601 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1602 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1603 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1605 fprintf(stderr
, "Internal timer error: aborting\n");
1610 #endif /* defined(__linux__) */
1612 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1614 struct sigaction act
;
1615 struct itimerval itv
;
1619 sigfillset(&act
.sa_mask
);
1621 act
.sa_handler
= host_alarm_handler
;
1623 sigaction(SIGALRM
, &act
, NULL
);
1625 itv
.it_interval
.tv_sec
= 0;
1626 /* for i386 kernel 2.6 to get 1 ms */
1627 itv
.it_interval
.tv_usec
= 999;
1628 itv
.it_value
.tv_sec
= 0;
1629 itv
.it_value
.tv_usec
= 10 * 1000;
1631 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1638 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1640 struct itimerval itv
;
1642 memset(&itv
, 0, sizeof(itv
));
1643 setitimer(ITIMER_REAL
, &itv
, NULL
);
1646 #endif /* !defined(_WIN32) */
1651 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1654 struct qemu_alarm_win32
*data
= t
->priv
;
1657 memset(&tc
, 0, sizeof(tc
));
1658 timeGetDevCaps(&tc
, sizeof(tc
));
1660 if (data
->period
< tc
.wPeriodMin
)
1661 data
->period
= tc
.wPeriodMin
;
1663 timeBeginPeriod(data
->period
);
1665 flags
= TIME_CALLBACK_FUNCTION
;
1666 if (alarm_has_dynticks(t
))
1667 flags
|= TIME_ONESHOT
;
1669 flags
|= TIME_PERIODIC
;
1671 data
->timerId
= timeSetEvent(1, // interval (ms)
1672 data
->period
, // resolution
1673 host_alarm_handler
, // function
1674 (DWORD
)t
, // parameter
1677 if (!data
->timerId
) {
1678 perror("Failed to initialize win32 alarm timer");
1679 timeEndPeriod(data
->period
);
1686 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1688 struct qemu_alarm_win32
*data
= t
->priv
;
1690 timeKillEvent(data
->timerId
);
1691 timeEndPeriod(data
->period
);
1694 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1696 struct qemu_alarm_win32
*data
= t
->priv
;
1697 uint64_t nearest_delta_us
;
1699 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1700 !active_timers
[QEMU_TIMER_VIRTUAL
])
1703 nearest_delta_us
= qemu_next_deadline_dyntick();
1704 nearest_delta_us
/= 1000;
1706 timeKillEvent(data
->timerId
);
1708 data
->timerId
= timeSetEvent(1,
1712 TIME_ONESHOT
| TIME_PERIODIC
);
1714 if (!data
->timerId
) {
1715 perror("Failed to re-arm win32 alarm timer");
1717 timeEndPeriod(data
->period
);
1724 static int init_timer_alarm(void)
1726 struct qemu_alarm_timer
*t
= NULL
;
1729 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1730 t
= &alarm_timers
[i
];
1750 static void quit_timers(void)
1752 alarm_timer
->stop(alarm_timer
);
1756 /***********************************************************/
1757 /* host time/date access */
1758 void qemu_get_timedate(struct tm
*tm
, int offset
)
1765 if (rtc_date_offset
== -1) {
1769 ret
= localtime(&ti
);
1771 ti
-= rtc_date_offset
;
1775 memcpy(tm
, ret
, sizeof(struct tm
));
1778 int qemu_timedate_diff(struct tm
*tm
)
1782 if (rtc_date_offset
== -1)
1784 seconds
= mktimegm(tm
);
1786 seconds
= mktime(tm
);
1788 seconds
= mktimegm(tm
) + rtc_date_offset
;
1790 return seconds
- time(NULL
);
1794 static void socket_cleanup(void)
1799 static int socket_init(void)
1804 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1806 err
= WSAGetLastError();
1807 fprintf(stderr
, "WSAStartup: %d\n", err
);
1810 atexit(socket_cleanup
);
1815 int get_param_value(char *buf
, int buf_size
,
1816 const char *tag
, const char *str
)
1823 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1827 if (!strcmp(tag
, option
)) {
1828 (void)get_opt_value(buf
, buf_size
, p
);
1831 p
= get_opt_value(NULL
, 0, p
);
1840 int check_params(char *buf
, int buf_size
,
1841 const char * const *params
, const char *str
)
1847 while (*p
!= '\0') {
1848 p
= get_opt_name(buf
, buf_size
, p
, '=');
1853 for (i
= 0; params
[i
] != NULL
; i
++) {
1854 if (!strcmp(params
[i
], buf
)) {
1858 if (params
[i
] == NULL
) {
1861 p
= get_opt_value(NULL
, 0, p
);
1870 /***********************************************************/
1871 /* Bluetooth support */
1874 static struct HCIInfo
*hci_table
[MAX_NICS
];
1876 static struct bt_vlan_s
{
1877 struct bt_scatternet_s net
;
1879 struct bt_vlan_s
*next
;
1882 /* find or alloc a new bluetooth "VLAN" */
1883 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1885 struct bt_vlan_s
**pvlan
, *vlan
;
1886 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1890 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1892 pvlan
= &first_bt_vlan
;
1893 while (*pvlan
!= NULL
)
1894 pvlan
= &(*pvlan
)->next
;
1899 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1903 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1908 static struct HCIInfo null_hci
= {
1909 .cmd_send
= null_hci_send
,
1910 .sco_send
= null_hci_send
,
1911 .acl_send
= null_hci_send
,
1912 .bdaddr_set
= null_hci_addr_set
,
1915 struct HCIInfo
*qemu_next_hci(void)
1917 if (cur_hci
== nb_hcis
)
1920 return hci_table
[cur_hci
++];
1923 static struct HCIInfo
*hci_init(const char *str
)
1926 struct bt_scatternet_s
*vlan
= 0;
1928 if (!strcmp(str
, "null"))
1931 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1933 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1934 else if (!strncmp(str
, "hci", 3)) {
1937 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1938 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1943 vlan
= qemu_find_bt_vlan(0);
1945 return bt_new_hci(vlan
);
1948 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1953 static int bt_hci_parse(const char *str
)
1955 struct HCIInfo
*hci
;
1958 if (nb_hcis
>= MAX_NICS
) {
1959 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1963 hci
= hci_init(str
);
1972 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1973 hci
->bdaddr_set(hci
, bdaddr
.b
);
1975 hci_table
[nb_hcis
++] = hci
;
1980 static void bt_vhci_add(int vlan_id
)
1982 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1985 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1986 "an empty scatternet %i\n", vlan_id
);
1988 bt_vhci_init(bt_new_hci(vlan
));
1991 static struct bt_device_s
*bt_device_add(const char *opt
)
1993 struct bt_scatternet_s
*vlan
;
1995 char *endp
= strstr(opt
, ",vlan=");
1996 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1999 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2002 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2004 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2009 vlan
= qemu_find_bt_vlan(vlan_id
);
2012 fprintf(stderr
, "qemu: warning: adding a slave device to "
2013 "an empty scatternet %i\n", vlan_id
);
2015 if (!strcmp(devname
, "keyboard"))
2016 return bt_keyboard_init(vlan
);
2018 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2022 static int bt_parse(const char *opt
)
2024 const char *endp
, *p
;
2027 if (strstart(opt
, "hci", &endp
)) {
2028 if (!*endp
|| *endp
== ',') {
2030 if (!strstart(endp
, ",vlan=", 0))
2033 return bt_hci_parse(opt
);
2035 } else if (strstart(opt
, "vhci", &endp
)) {
2036 if (!*endp
|| *endp
== ',') {
2038 if (strstart(endp
, ",vlan=", &p
)) {
2039 vlan
= strtol(p
, (char **) &endp
, 0);
2041 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2045 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2054 } else if (strstart(opt
, "device:", &endp
))
2055 return !bt_device_add(endp
);
2057 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2061 /***********************************************************/
2062 /* QEMU Block devices */
2064 #define HD_ALIAS "index=%d,media=disk"
2065 #define CDROM_ALIAS "index=2,media=cdrom"
2066 #define FD_ALIAS "index=%d,if=floppy"
2067 #define PFLASH_ALIAS "if=pflash"
2068 #define MTD_ALIAS "if=mtd"
2069 #define SD_ALIAS "index=0,if=sd"
2071 static int drive_opt_get_free_idx(void)
2075 for (index
= 0; index
< MAX_DRIVES
; index
++)
2076 if (!drives_opt
[index
].used
) {
2077 drives_opt
[index
].used
= 1;
2084 static int drive_get_free_idx(void)
2088 for (index
= 0; index
< MAX_DRIVES
; index
++)
2089 if (!drives_table
[index
].used
) {
2090 drives_table
[index
].used
= 1;
2097 int drive_add(const char *file
, const char *fmt
, ...)
2100 int index
= drive_opt_get_free_idx();
2102 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2103 fprintf(stderr
, "qemu: too many drives\n");
2107 drives_opt
[index
].file
= file
;
2109 vsnprintf(drives_opt
[index
].opt
,
2110 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2117 void drive_remove(int index
)
2119 drives_opt
[index
].used
= 0;
2123 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2127 /* seek interface, bus and unit */
2129 for (index
= 0; index
< MAX_DRIVES
; index
++)
2130 if (drives_table
[index
].type
== type
&&
2131 drives_table
[index
].bus
== bus
&&
2132 drives_table
[index
].unit
== unit
&&
2133 drives_table
[index
].used
)
2139 int drive_get_max_bus(BlockInterfaceType type
)
2145 for (index
= 0; index
< nb_drives
; index
++) {
2146 if(drives_table
[index
].type
== type
&&
2147 drives_table
[index
].bus
> max_bus
)
2148 max_bus
= drives_table
[index
].bus
;
2153 const char *drive_get_serial(BlockDriverState
*bdrv
)
2157 for (index
= 0; index
< nb_drives
; index
++)
2158 if (drives_table
[index
].bdrv
== bdrv
)
2159 return drives_table
[index
].serial
;
2164 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2168 for (index
= 0; index
< nb_drives
; index
++)
2169 if (drives_table
[index
].bdrv
== bdrv
)
2170 return drives_table
[index
].onerror
;
2172 return BLOCK_ERR_STOP_ENOSPC
;
2175 static void bdrv_format_print(void *opaque
, const char *name
)
2177 fprintf(stderr
, " %s", name
);
2180 void drive_uninit(BlockDriverState
*bdrv
)
2184 for (i
= 0; i
< MAX_DRIVES
; i
++)
2185 if (drives_table
[i
].bdrv
== bdrv
) {
2186 drives_table
[i
].bdrv
= NULL
;
2187 drives_table
[i
].used
= 0;
2188 drive_remove(drives_table
[i
].drive_opt_idx
);
2194 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2200 const char *mediastr
= "";
2201 BlockInterfaceType type
;
2202 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2203 int bus_id
, unit_id
;
2204 int cyls
, heads
, secs
, translation
;
2205 BlockDriverState
*bdrv
;
2206 BlockDriver
*drv
= NULL
;
2207 QEMUMachine
*machine
= opaque
;
2211 int bdrv_flags
, onerror
;
2212 const char *devaddr
;
2213 int drives_table_idx
;
2214 char *str
= arg
->opt
;
2215 static const char * const params
[] = { "bus", "unit", "if", "index",
2216 "cyls", "heads", "secs", "trans",
2217 "media", "snapshot", "file",
2218 "cache", "format", "serial",
2222 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2223 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2229 cyls
= heads
= secs
= 0;
2232 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2236 if (machine
->use_scsi
) {
2238 max_devs
= MAX_SCSI_DEVS
;
2239 pstrcpy(devname
, sizeof(devname
), "scsi");
2242 max_devs
= MAX_IDE_DEVS
;
2243 pstrcpy(devname
, sizeof(devname
), "ide");
2247 /* extract parameters */
2249 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2250 bus_id
= strtol(buf
, NULL
, 0);
2252 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2257 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2258 unit_id
= strtol(buf
, NULL
, 0);
2260 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2265 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2266 pstrcpy(devname
, sizeof(devname
), buf
);
2267 if (!strcmp(buf
, "ide")) {
2269 max_devs
= MAX_IDE_DEVS
;
2270 } else if (!strcmp(buf
, "scsi")) {
2272 max_devs
= MAX_SCSI_DEVS
;
2273 } else if (!strcmp(buf
, "floppy")) {
2276 } else if (!strcmp(buf
, "pflash")) {
2279 } else if (!strcmp(buf
, "mtd")) {
2282 } else if (!strcmp(buf
, "sd")) {
2285 } else if (!strcmp(buf
, "virtio")) {
2288 } else if (!strcmp(buf
, "xen")) {
2292 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2297 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2298 index
= strtol(buf
, NULL
, 0);
2300 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2305 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2306 cyls
= strtol(buf
, NULL
, 0);
2309 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2310 heads
= strtol(buf
, NULL
, 0);
2313 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2314 secs
= strtol(buf
, NULL
, 0);
2317 if (cyls
|| heads
|| secs
) {
2318 if (cyls
< 1 || cyls
> 16383) {
2319 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2322 if (heads
< 1 || heads
> 16) {
2323 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2326 if (secs
< 1 || secs
> 63) {
2327 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2332 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2335 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2339 if (!strcmp(buf
, "none"))
2340 translation
= BIOS_ATA_TRANSLATION_NONE
;
2341 else if (!strcmp(buf
, "lba"))
2342 translation
= BIOS_ATA_TRANSLATION_LBA
;
2343 else if (!strcmp(buf
, "auto"))
2344 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2346 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2351 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2352 if (!strcmp(buf
, "disk")) {
2354 } else if (!strcmp(buf
, "cdrom")) {
2355 if (cyls
|| secs
|| heads
) {
2357 "qemu: '%s' invalid physical CHS format\n", str
);
2360 media
= MEDIA_CDROM
;
2362 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2367 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2368 if (!strcmp(buf
, "on"))
2370 else if (!strcmp(buf
, "off"))
2373 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2378 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2379 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2381 else if (!strcmp(buf
, "writethrough"))
2383 else if (!strcmp(buf
, "writeback"))
2386 fprintf(stderr
, "qemu: invalid cache option\n");
2391 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2392 if (strcmp(buf
, "?") == 0) {
2393 fprintf(stderr
, "qemu: Supported formats:");
2394 bdrv_iterate_format(bdrv_format_print
, NULL
);
2395 fprintf(stderr
, "\n");
2398 drv
= bdrv_find_format(buf
);
2400 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2405 if (arg
->file
== NULL
)
2406 get_param_value(file
, sizeof(file
), "file", str
);
2408 pstrcpy(file
, sizeof(file
), arg
->file
);
2410 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2411 memset(serial
, 0, sizeof(serial
));
2413 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2414 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2415 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2416 fprintf(stderr
, "werror is no supported by this format\n");
2419 if (!strcmp(buf
, "ignore"))
2420 onerror
= BLOCK_ERR_IGNORE
;
2421 else if (!strcmp(buf
, "enospc"))
2422 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2423 else if (!strcmp(buf
, "stop"))
2424 onerror
= BLOCK_ERR_STOP_ANY
;
2425 else if (!strcmp(buf
, "report"))
2426 onerror
= BLOCK_ERR_REPORT
;
2428 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2434 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2435 if (type
!= IF_VIRTIO
) {
2436 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2439 devaddr
= strdup(buf
);
2442 /* compute bus and unit according index */
2445 if (bus_id
!= 0 || unit_id
!= -1) {
2447 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2455 unit_id
= index
% max_devs
;
2456 bus_id
= index
/ max_devs
;
2460 /* if user doesn't specify a unit_id,
2461 * try to find the first free
2464 if (unit_id
== -1) {
2466 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2468 if (max_devs
&& unit_id
>= max_devs
) {
2469 unit_id
-= max_devs
;
2477 if (max_devs
&& unit_id
>= max_devs
) {
2478 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2479 str
, unit_id
, max_devs
- 1);
2484 * ignore multiple definitions
2487 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2492 if (type
== IF_IDE
|| type
== IF_SCSI
)
2493 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2495 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2496 devname
, bus_id
, mediastr
, unit_id
);
2498 snprintf(buf
, sizeof(buf
), "%s%s%i",
2499 devname
, mediastr
, unit_id
);
2500 bdrv
= bdrv_new(buf
);
2501 drives_table_idx
= drive_get_free_idx();
2502 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2503 drives_table
[drives_table_idx
].devaddr
= devaddr
;
2504 drives_table
[drives_table_idx
].type
= type
;
2505 drives_table
[drives_table_idx
].bus
= bus_id
;
2506 drives_table
[drives_table_idx
].unit
= unit_id
;
2507 drives_table
[drives_table_idx
].onerror
= onerror
;
2508 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2509 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2519 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2520 bdrv_set_translation_hint(bdrv
, translation
);
2524 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2529 /* FIXME: This isn't really a floppy, but it's a reasonable
2532 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2545 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2546 cache
= 2; /* always use write-back with snapshot */
2548 if (cache
== 0) /* no caching */
2549 bdrv_flags
|= BDRV_O_NOCACHE
;
2550 else if (cache
== 2) /* write-back */
2551 bdrv_flags
|= BDRV_O_CACHE_WB
;
2552 else if (cache
== 3) /* not specified */
2553 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2554 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2555 fprintf(stderr
, "qemu: could not open disk image %s\n",
2559 if (bdrv_key_required(bdrv
))
2561 return drives_table_idx
;
2564 static void numa_add(const char *optarg
)
2568 unsigned long long value
, endvalue
;
2571 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2572 if (!strcmp(option
, "node")) {
2573 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2574 nodenr
= nb_numa_nodes
;
2576 nodenr
= strtoull(option
, NULL
, 10);
2579 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2580 node_mem
[nodenr
] = 0;
2582 value
= strtoull(option
, &endptr
, 0);
2584 case 0: case 'M': case 'm':
2591 node_mem
[nodenr
] = value
;
2593 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2594 node_cpumask
[nodenr
] = 0;
2596 value
= strtoull(option
, &endptr
, 10);
2599 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2601 if (*endptr
== '-') {
2602 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2603 if (endvalue
>= 63) {
2606 "only 63 CPUs in NUMA mode supported.\n");
2608 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2613 node_cpumask
[nodenr
] = value
;
2620 /***********************************************************/
2623 static USBPort
*used_usb_ports
;
2624 static USBPort
*free_usb_ports
;
2626 /* ??? Maybe change this to register a hub to keep track of the topology. */
2627 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2628 usb_attachfn attach
)
2630 port
->opaque
= opaque
;
2631 port
->index
= index
;
2632 port
->attach
= attach
;
2633 port
->next
= free_usb_ports
;
2634 free_usb_ports
= port
;
2637 int usb_device_add_dev(USBDevice
*dev
)
2641 /* Find a USB port to add the device to. */
2642 port
= free_usb_ports
;
2646 /* Create a new hub and chain it on. */
2647 free_usb_ports
= NULL
;
2648 port
->next
= used_usb_ports
;
2649 used_usb_ports
= port
;
2651 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2652 usb_attach(port
, hub
);
2653 port
= free_usb_ports
;
2656 free_usb_ports
= port
->next
;
2657 port
->next
= used_usb_ports
;
2658 used_usb_ports
= port
;
2659 usb_attach(port
, dev
);
2663 static void usb_msd_password_cb(void *opaque
, int err
)
2665 USBDevice
*dev
= opaque
;
2668 usb_device_add_dev(dev
);
2670 dev
->handle_destroy(dev
);
2673 static int usb_device_add(const char *devname
, int is_hotplug
)
2678 if (!free_usb_ports
)
2681 if (strstart(devname
, "host:", &p
)) {
2682 dev
= usb_host_device_open(p
);
2683 } else if (!strcmp(devname
, "mouse")) {
2684 dev
= usb_mouse_init();
2685 } else if (!strcmp(devname
, "tablet")) {
2686 dev
= usb_tablet_init();
2687 } else if (!strcmp(devname
, "keyboard")) {
2688 dev
= usb_keyboard_init();
2689 } else if (strstart(devname
, "disk:", &p
)) {
2690 BlockDriverState
*bs
;
2692 dev
= usb_msd_init(p
);
2695 bs
= usb_msd_get_bdrv(dev
);
2696 if (bdrv_key_required(bs
)) {
2699 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2704 } else if (!strcmp(devname
, "wacom-tablet")) {
2705 dev
= usb_wacom_init();
2706 } else if (strstart(devname
, "serial:", &p
)) {
2707 dev
= usb_serial_init(p
);
2708 #ifdef CONFIG_BRLAPI
2709 } else if (!strcmp(devname
, "braille")) {
2710 dev
= usb_baum_init();
2712 } else if (strstart(devname
, "net:", &p
)) {
2715 if (net_client_init(NULL
, "nic", p
) < 0)
2717 nd_table
[nic
].model
= "usb";
2718 dev
= usb_net_init(&nd_table
[nic
]);
2719 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2720 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2721 bt_new_hci(qemu_find_bt_vlan(0)));
2728 return usb_device_add_dev(dev
);
2731 int usb_device_del_addr(int bus_num
, int addr
)
2737 if (!used_usb_ports
)
2743 lastp
= &used_usb_ports
;
2744 port
= used_usb_ports
;
2745 while (port
&& port
->dev
->addr
!= addr
) {
2746 lastp
= &port
->next
;
2754 *lastp
= port
->next
;
2755 usb_attach(port
, NULL
);
2756 dev
->handle_destroy(dev
);
2757 port
->next
= free_usb_ports
;
2758 free_usb_ports
= port
;
2762 static int usb_device_del(const char *devname
)
2767 if (strstart(devname
, "host:", &p
))
2768 return usb_host_device_close(p
);
2770 if (!used_usb_ports
)
2773 p
= strchr(devname
, '.');
2776 bus_num
= strtoul(devname
, NULL
, 0);
2777 addr
= strtoul(p
+ 1, NULL
, 0);
2779 return usb_device_del_addr(bus_num
, addr
);
2782 void do_usb_add(Monitor
*mon
, const char *devname
)
2784 usb_device_add(devname
, 1);
2787 void do_usb_del(Monitor
*mon
, const char *devname
)
2789 usb_device_del(devname
);
2792 void usb_info(Monitor
*mon
)
2796 const char *speed_str
;
2799 monitor_printf(mon
, "USB support not enabled\n");
2803 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2807 switch(dev
->speed
) {
2811 case USB_SPEED_FULL
:
2814 case USB_SPEED_HIGH
:
2821 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2822 0, dev
->addr
, speed_str
, dev
->devname
);
2826 /***********************************************************/
2827 /* PCMCIA/Cardbus */
2829 static struct pcmcia_socket_entry_s
{
2830 PCMCIASocket
*socket
;
2831 struct pcmcia_socket_entry_s
*next
;
2832 } *pcmcia_sockets
= 0;
2834 void pcmcia_socket_register(PCMCIASocket
*socket
)
2836 struct pcmcia_socket_entry_s
*entry
;
2838 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2839 entry
->socket
= socket
;
2840 entry
->next
= pcmcia_sockets
;
2841 pcmcia_sockets
= entry
;
2844 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2846 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2848 ptr
= &pcmcia_sockets
;
2849 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2850 if (entry
->socket
== socket
) {
2856 void pcmcia_info(Monitor
*mon
)
2858 struct pcmcia_socket_entry_s
*iter
;
2860 if (!pcmcia_sockets
)
2861 monitor_printf(mon
, "No PCMCIA sockets\n");
2863 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2864 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2865 iter
->socket
->attached
? iter
->socket
->card_string
:
2869 /***********************************************************/
2870 /* register display */
2872 struct DisplayAllocator default_allocator
= {
2873 defaultallocator_create_displaysurface
,
2874 defaultallocator_resize_displaysurface
,
2875 defaultallocator_free_displaysurface
2878 void register_displaystate(DisplayState
*ds
)
2888 DisplayState
*get_displaystate(void)
2890 return display_state
;
2893 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2895 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2896 return ds
->allocator
;
2901 static void dumb_display_init(void)
2903 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2904 ds
->allocator
= &default_allocator
;
2905 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2906 register_displaystate(ds
);
2909 /***********************************************************/
2912 typedef struct IOHandlerRecord
{
2914 IOCanRWHandler
*fd_read_poll
;
2916 IOHandler
*fd_write
;
2919 /* temporary data */
2921 struct IOHandlerRecord
*next
;
2924 static IOHandlerRecord
*first_io_handler
;
2926 /* XXX: fd_read_poll should be suppressed, but an API change is
2927 necessary in the character devices to suppress fd_can_read(). */
2928 int qemu_set_fd_handler2(int fd
,
2929 IOCanRWHandler
*fd_read_poll
,
2931 IOHandler
*fd_write
,
2934 IOHandlerRecord
**pioh
, *ioh
;
2936 if (!fd_read
&& !fd_write
) {
2937 pioh
= &first_io_handler
;
2942 if (ioh
->fd
== fd
) {
2949 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2953 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2954 ioh
->next
= first_io_handler
;
2955 first_io_handler
= ioh
;
2958 ioh
->fd_read_poll
= fd_read_poll
;
2959 ioh
->fd_read
= fd_read
;
2960 ioh
->fd_write
= fd_write
;
2961 ioh
->opaque
= opaque
;
2967 int qemu_set_fd_handler(int fd
,
2969 IOHandler
*fd_write
,
2972 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2976 /***********************************************************/
2977 /* Polling handling */
2979 typedef struct PollingEntry
{
2982 struct PollingEntry
*next
;
2985 static PollingEntry
*first_polling_entry
;
2987 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2989 PollingEntry
**ppe
, *pe
;
2990 pe
= qemu_mallocz(sizeof(PollingEntry
));
2992 pe
->opaque
= opaque
;
2993 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2998 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3000 PollingEntry
**ppe
, *pe
;
3001 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3003 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3011 /***********************************************************/
3012 /* Wait objects support */
3013 typedef struct WaitObjects
{
3015 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3016 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3017 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3020 static WaitObjects wait_objects
= {0};
3022 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3024 WaitObjects
*w
= &wait_objects
;
3026 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3028 w
->events
[w
->num
] = handle
;
3029 w
->func
[w
->num
] = func
;
3030 w
->opaque
[w
->num
] = opaque
;
3035 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3038 WaitObjects
*w
= &wait_objects
;
3041 for (i
= 0; i
< w
->num
; i
++) {
3042 if (w
->events
[i
] == handle
)
3045 w
->events
[i
] = w
->events
[i
+ 1];
3046 w
->func
[i
] = w
->func
[i
+ 1];
3047 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3055 /***********************************************************/
3056 /* ram save/restore */
3058 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3062 v
= qemu_get_byte(f
);
3065 if (qemu_get_buffer(f
, buf
, len
) != len
)
3069 v
= qemu_get_byte(f
);
3070 memset(buf
, v
, len
);
3076 if (qemu_file_has_error(f
))
3082 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3087 if (qemu_get_be32(f
) != last_ram_offset
)
3089 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
3090 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
3097 #define BDRV_HASH_BLOCK_SIZE 1024
3098 #define IOBUF_SIZE 4096
3099 #define RAM_CBLOCK_MAGIC 0xfabe
3101 typedef struct RamDecompressState
{
3104 uint8_t buf
[IOBUF_SIZE
];
3105 } RamDecompressState
;
3107 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3110 memset(s
, 0, sizeof(*s
));
3112 ret
= inflateInit(&s
->zstream
);
3118 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3122 s
->zstream
.avail_out
= len
;
3123 s
->zstream
.next_out
= buf
;
3124 while (s
->zstream
.avail_out
> 0) {
3125 if (s
->zstream
.avail_in
== 0) {
3126 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3128 clen
= qemu_get_be16(s
->f
);
3129 if (clen
> IOBUF_SIZE
)
3131 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3132 s
->zstream
.avail_in
= clen
;
3133 s
->zstream
.next_in
= s
->buf
;
3135 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3136 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3143 static void ram_decompress_close(RamDecompressState
*s
)
3145 inflateEnd(&s
->zstream
);
3148 #define RAM_SAVE_FLAG_FULL 0x01
3149 #define RAM_SAVE_FLAG_COMPRESS 0x02
3150 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3151 #define RAM_SAVE_FLAG_PAGE 0x08
3152 #define RAM_SAVE_FLAG_EOS 0x10
3154 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3156 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3157 uint32_t *array
= (uint32_t *)page
;
3160 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3161 if (array
[i
] != val
)
3168 static int ram_save_block(QEMUFile
*f
)
3170 static ram_addr_t current_addr
= 0;
3171 ram_addr_t saved_addr
= current_addr
;
3172 ram_addr_t addr
= 0;
3175 while (addr
< last_ram_offset
) {
3176 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3179 cpu_physical_memory_reset_dirty(current_addr
,
3180 current_addr
+ TARGET_PAGE_SIZE
,
3181 MIGRATION_DIRTY_FLAG
);
3183 p
= qemu_get_ram_ptr(current_addr
);
3185 if (is_dup_page(p
, *p
)) {
3186 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3187 qemu_put_byte(f
, *p
);
3189 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3190 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3196 addr
+= TARGET_PAGE_SIZE
;
3197 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3203 static uint64_t bytes_transferred
= 0;
3205 static ram_addr_t
ram_save_remaining(void)
3208 ram_addr_t count
= 0;
3210 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3211 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3218 uint64_t ram_bytes_remaining(void)
3220 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3223 uint64_t ram_bytes_transferred(void)
3225 return bytes_transferred
;
3228 uint64_t ram_bytes_total(void)
3230 return last_ram_offset
;
3233 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3236 uint64_t bytes_transferred_last
;
3238 uint64_t expected_time
= 0;
3240 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3241 qemu_file_set_error(f
);
3246 /* Make sure all dirty bits are set */
3247 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3248 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3249 cpu_physical_memory_set_dirty(addr
);
3252 /* Enable dirty memory tracking */
3253 cpu_physical_memory_set_dirty_tracking(1);
3255 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3258 bytes_transferred_last
= bytes_transferred
;
3259 bwidth
= get_clock();
3261 while (!qemu_file_rate_limit(f
)) {
3264 ret
= ram_save_block(f
);
3265 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3266 if (ret
== 0) /* no more blocks */
3270 bwidth
= get_clock() - bwidth
;
3271 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3273 /* if we haven't transferred anything this round, force expected_time to a
3274 * a very high value, but without crashing */
3278 /* try transferring iterative blocks of memory */
3282 /* flush all remaining blocks regardless of rate limiting */
3283 while (ram_save_block(f
) != 0) {
3284 bytes_transferred
+= TARGET_PAGE_SIZE
;
3286 cpu_physical_memory_set_dirty_tracking(0);
3289 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3291 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3293 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3296 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3298 RamDecompressState s1
, *s
= &s1
;
3302 if (ram_decompress_open(s
, f
) < 0)
3304 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3305 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3306 fprintf(stderr
, "Error while reading ram block header\n");
3310 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3311 BDRV_HASH_BLOCK_SIZE
) < 0) {
3312 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3317 printf("Error block header\n");
3321 ram_decompress_close(s
);
3326 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3331 if (version_id
== 1)
3332 return ram_load_v1(f
, opaque
);
3334 if (version_id
== 2) {
3335 if (qemu_get_be32(f
) != last_ram_offset
)
3337 return ram_load_dead(f
, opaque
);
3340 if (version_id
!= 3)
3344 addr
= qemu_get_be64(f
);
3346 flags
= addr
& ~TARGET_PAGE_MASK
;
3347 addr
&= TARGET_PAGE_MASK
;
3349 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3350 if (addr
!= last_ram_offset
)
3354 if (flags
& RAM_SAVE_FLAG_FULL
) {
3355 if (ram_load_dead(f
, opaque
) < 0)
3359 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3360 uint8_t ch
= qemu_get_byte(f
);
3361 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3364 (!kvm_enabled() || kvm_has_sync_mmu())) {
3365 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3368 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3369 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3370 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3375 void qemu_service_io(void)
3377 qemu_notify_event();
3380 /***********************************************************/
3381 /* bottom halves (can be seen as timers which expire ASAP) */
3392 static QEMUBH
*first_bh
= NULL
;
3394 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3397 bh
= qemu_mallocz(sizeof(QEMUBH
));
3399 bh
->opaque
= opaque
;
3400 bh
->next
= first_bh
;
3405 int qemu_bh_poll(void)
3411 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3412 if (!bh
->deleted
&& bh
->scheduled
) {
3421 /* remove deleted bhs */
3435 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3443 void qemu_bh_schedule(QEMUBH
*bh
)
3449 /* stop the currently executing CPU to execute the BH ASAP */
3450 qemu_notify_event();
3453 void qemu_bh_cancel(QEMUBH
*bh
)
3458 void qemu_bh_delete(QEMUBH
*bh
)
3464 static void qemu_bh_update_timeout(int *timeout
)
3468 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3469 if (!bh
->deleted
&& bh
->scheduled
) {
3471 /* idle bottom halves will be polled at least
3473 *timeout
= MIN(10, *timeout
);
3475 /* non-idle bottom halves will be executed
3484 /***********************************************************/
3485 /* machine registration */
3487 static QEMUMachine
*first_machine
= NULL
;
3488 QEMUMachine
*current_machine
= NULL
;
3490 int qemu_register_machine(QEMUMachine
*m
)
3493 pm
= &first_machine
;
3501 static QEMUMachine
*find_machine(const char *name
)
3505 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3506 if (!strcmp(m
->name
, name
))
3512 static QEMUMachine
*find_default_machine(void)
3516 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3517 if (m
->is_default
) {
3524 /***********************************************************/
3525 /* main execution loop */
3527 static void gui_update(void *opaque
)
3529 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3530 DisplayState
*ds
= opaque
;
3531 DisplayChangeListener
*dcl
= ds
->listeners
;
3535 while (dcl
!= NULL
) {
3536 if (dcl
->gui_timer_interval
&&
3537 dcl
->gui_timer_interval
< interval
)
3538 interval
= dcl
->gui_timer_interval
;
3541 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3544 static void nographic_update(void *opaque
)
3546 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3548 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3551 struct vm_change_state_entry
{
3552 VMChangeStateHandler
*cb
;
3554 LIST_ENTRY (vm_change_state_entry
) entries
;
3557 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3559 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3562 VMChangeStateEntry
*e
;
3564 e
= qemu_mallocz(sizeof (*e
));
3568 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3572 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3574 LIST_REMOVE (e
, entries
);
3578 static void vm_state_notify(int running
, int reason
)
3580 VMChangeStateEntry
*e
;
3582 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3583 e
->cb(e
->opaque
, running
, reason
);
3587 static void resume_all_vcpus(void);
3588 static void pause_all_vcpus(void);
3595 vm_state_notify(1, 0);
3596 qemu_rearm_alarm_timer(alarm_timer
);
3601 /* reset/shutdown handler */
3603 typedef struct QEMUResetEntry
{
3604 QEMUResetHandler
*func
;
3607 struct QEMUResetEntry
*next
;
3610 static QEMUResetEntry
*first_reset_entry
;
3611 static int reset_requested
;
3612 static int shutdown_requested
;
3613 static int powerdown_requested
;
3614 static int debug_requested
;
3615 static int vmstop_requested
;
3617 int qemu_shutdown_requested(void)
3619 int r
= shutdown_requested
;
3620 shutdown_requested
= 0;
3624 int qemu_reset_requested(void)
3626 int r
= reset_requested
;
3627 reset_requested
= 0;
3631 int qemu_powerdown_requested(void)
3633 int r
= powerdown_requested
;
3634 powerdown_requested
= 0;
3638 static int qemu_debug_requested(void)
3640 int r
= debug_requested
;
3641 debug_requested
= 0;
3645 static int qemu_vmstop_requested(void)
3647 int r
= vmstop_requested
;
3648 vmstop_requested
= 0;
3652 static void do_vm_stop(int reason
)
3655 cpu_disable_ticks();
3658 vm_state_notify(0, reason
);
3662 void qemu_register_reset(QEMUResetHandler
*func
, int order
, void *opaque
)
3664 QEMUResetEntry
**pre
, *re
;
3666 pre
= &first_reset_entry
;
3667 while (*pre
!= NULL
&& (*pre
)->order
>= order
) {
3668 pre
= &(*pre
)->next
;
3670 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3672 re
->opaque
= opaque
;
3678 void qemu_system_reset(void)
3682 /* reset all devices */
3683 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3684 re
->func(re
->opaque
);
3688 void qemu_system_reset_request(void)
3691 shutdown_requested
= 1;
3693 reset_requested
= 1;
3695 qemu_notify_event();
3698 void qemu_system_shutdown_request(void)
3700 shutdown_requested
= 1;
3701 qemu_notify_event();
3704 void qemu_system_powerdown_request(void)
3706 powerdown_requested
= 1;
3707 qemu_notify_event();
3710 #ifdef CONFIG_IOTHREAD
3711 static void qemu_system_vmstop_request(int reason
)
3713 vmstop_requested
= reason
;
3714 qemu_notify_event();
3719 static int io_thread_fd
= -1;
3721 static void qemu_event_increment(void)
3723 static const char byte
= 0;
3725 if (io_thread_fd
== -1)
3728 write(io_thread_fd
, &byte
, sizeof(byte
));
3731 static void qemu_event_read(void *opaque
)
3733 int fd
= (unsigned long)opaque
;
3736 /* Drain the notify pipe */
3739 len
= read(fd
, buffer
, sizeof(buffer
));
3740 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3743 static int qemu_event_init(void)
3752 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3756 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3760 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3761 (void *)(unsigned long)fds
[0]);
3763 io_thread_fd
= fds
[1];
3772 HANDLE qemu_event_handle
;
3774 static void dummy_event_handler(void *opaque
)
3778 static int qemu_event_init(void)
3780 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3781 if (!qemu_event_handle
) {
3782 perror("Failed CreateEvent");
3785 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3789 static void qemu_event_increment(void)
3791 SetEvent(qemu_event_handle
);
3795 static int cpu_can_run(CPUState
*env
)
3804 #ifndef CONFIG_IOTHREAD
3805 static int qemu_init_main_loop(void)
3807 return qemu_event_init();
3810 void qemu_init_vcpu(void *_env
)
3812 CPUState
*env
= _env
;
3819 int qemu_cpu_self(void *env
)
3824 static void resume_all_vcpus(void)
3828 static void pause_all_vcpus(void)
3832 void qemu_cpu_kick(void *env
)
3837 void qemu_notify_event(void)
3839 CPUState
*env
= cpu_single_env
;
3844 if (env
->kqemu_enabled
)
3845 kqemu_cpu_interrupt(env
);
3850 #define qemu_mutex_lock_iothread() do { } while (0)
3851 #define qemu_mutex_unlock_iothread() do { } while (0)
3853 void vm_stop(int reason
)
3858 #else /* CONFIG_IOTHREAD */
3860 #include "qemu-thread.h"
3862 QemuMutex qemu_global_mutex
;
3863 static QemuMutex qemu_fair_mutex
;
3865 static QemuThread io_thread
;
3867 static QemuThread
*tcg_cpu_thread
;
3868 static QemuCond
*tcg_halt_cond
;
3870 static int qemu_system_ready
;
3872 static QemuCond qemu_cpu_cond
;
3874 static QemuCond qemu_system_cond
;
3875 static QemuCond qemu_pause_cond
;
3877 static void block_io_signals(void);
3878 static void unblock_io_signals(void);
3879 static int tcg_has_work(void);
3881 static int qemu_init_main_loop(void)
3885 ret
= qemu_event_init();
3889 qemu_cond_init(&qemu_pause_cond
);
3890 qemu_mutex_init(&qemu_fair_mutex
);
3891 qemu_mutex_init(&qemu_global_mutex
);
3892 qemu_mutex_lock(&qemu_global_mutex
);
3894 unblock_io_signals();
3895 qemu_thread_self(&io_thread
);
3900 static void qemu_wait_io_event(CPUState
*env
)
3902 while (!tcg_has_work())
3903 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3905 qemu_mutex_unlock(&qemu_global_mutex
);
3908 * Users of qemu_global_mutex can be starved, having no chance
3909 * to acquire it since this path will get to it first.
3910 * So use another lock to provide fairness.
3912 qemu_mutex_lock(&qemu_fair_mutex
);
3913 qemu_mutex_unlock(&qemu_fair_mutex
);
3915 qemu_mutex_lock(&qemu_global_mutex
);
3919 qemu_cond_signal(&qemu_pause_cond
);
3923 static int qemu_cpu_exec(CPUState
*env
);
3925 static void *kvm_cpu_thread_fn(void *arg
)
3927 CPUState
*env
= arg
;
3930 qemu_thread_self(env
->thread
);
3932 /* signal CPU creation */
3933 qemu_mutex_lock(&qemu_global_mutex
);
3935 qemu_cond_signal(&qemu_cpu_cond
);
3937 /* and wait for machine initialization */
3938 while (!qemu_system_ready
)
3939 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3942 if (cpu_can_run(env
))
3944 qemu_wait_io_event(env
);
3950 static void tcg_cpu_exec(void);
3952 static void *tcg_cpu_thread_fn(void *arg
)
3954 CPUState
*env
= arg
;
3957 qemu_thread_self(env
->thread
);
3959 /* signal CPU creation */
3960 qemu_mutex_lock(&qemu_global_mutex
);
3961 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3963 qemu_cond_signal(&qemu_cpu_cond
);
3965 /* and wait for machine initialization */
3966 while (!qemu_system_ready
)
3967 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3971 qemu_wait_io_event(cur_cpu
);
3977 void qemu_cpu_kick(void *_env
)
3979 CPUState
*env
= _env
;
3980 qemu_cond_broadcast(env
->halt_cond
);
3982 qemu_thread_signal(env
->thread
, SIGUSR1
);
3985 int qemu_cpu_self(void *env
)
3987 return (cpu_single_env
!= NULL
);
3990 static void cpu_signal(int sig
)
3993 cpu_exit(cpu_single_env
);
3996 static void block_io_signals(void)
3999 struct sigaction sigact
;
4002 sigaddset(&set
, SIGUSR2
);
4003 sigaddset(&set
, SIGIO
);
4004 sigaddset(&set
, SIGALRM
);
4005 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
4008 sigaddset(&set
, SIGUSR1
);
4009 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
4011 memset(&sigact
, 0, sizeof(sigact
));
4012 sigact
.sa_handler
= cpu_signal
;
4013 sigaction(SIGUSR1
, &sigact
, NULL
);
4016 static void unblock_io_signals(void)
4021 sigaddset(&set
, SIGUSR2
);
4022 sigaddset(&set
, SIGIO
);
4023 sigaddset(&set
, SIGALRM
);
4024 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
4027 sigaddset(&set
, SIGUSR1
);
4028 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
4031 static void qemu_signal_lock(unsigned int msecs
)
4033 qemu_mutex_lock(&qemu_fair_mutex
);
4035 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
4036 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
4037 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
4040 qemu_mutex_unlock(&qemu_fair_mutex
);
4043 static void qemu_mutex_lock_iothread(void)
4045 if (kvm_enabled()) {
4046 qemu_mutex_lock(&qemu_fair_mutex
);
4047 qemu_mutex_lock(&qemu_global_mutex
);
4048 qemu_mutex_unlock(&qemu_fair_mutex
);
4050 qemu_signal_lock(100);
4053 static void qemu_mutex_unlock_iothread(void)
4055 qemu_mutex_unlock(&qemu_global_mutex
);
4058 static int all_vcpus_paused(void)
4060 CPUState
*penv
= first_cpu
;
4065 penv
= (CPUState
*)penv
->next_cpu
;
4071 static void pause_all_vcpus(void)
4073 CPUState
*penv
= first_cpu
;
4077 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4078 qemu_cpu_kick(penv
);
4079 penv
= (CPUState
*)penv
->next_cpu
;
4082 while (!all_vcpus_paused()) {
4083 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
4086 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4087 penv
= (CPUState
*)penv
->next_cpu
;
4092 static void resume_all_vcpus(void)
4094 CPUState
*penv
= first_cpu
;
4099 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4100 qemu_cpu_kick(penv
);
4101 penv
= (CPUState
*)penv
->next_cpu
;
4105 static void tcg_init_vcpu(void *_env
)
4107 CPUState
*env
= _env
;
4108 /* share a single thread for all cpus with TCG */
4109 if (!tcg_cpu_thread
) {
4110 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
4111 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
4112 qemu_cond_init(env
->halt_cond
);
4113 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
4114 while (env
->created
== 0)
4115 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
4116 tcg_cpu_thread
= env
->thread
;
4117 tcg_halt_cond
= env
->halt_cond
;
4119 env
->thread
= tcg_cpu_thread
;
4120 env
->halt_cond
= tcg_halt_cond
;
4124 static void kvm_start_vcpu(CPUState
*env
)
4127 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
4128 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
4129 qemu_cond_init(env
->halt_cond
);
4130 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
4131 while (env
->created
== 0)
4132 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
4135 void qemu_init_vcpu(void *_env
)
4137 CPUState
*env
= _env
;
4140 kvm_start_vcpu(env
);
4145 void qemu_notify_event(void)
4147 qemu_event_increment();
4150 void vm_stop(int reason
)
4153 qemu_thread_self(&me
);
4155 if (!qemu_thread_equal(&me
, &io_thread
)) {
4156 qemu_system_vmstop_request(reason
);
4158 * FIXME: should not return to device code in case
4159 * vm_stop() has been requested.
4161 if (cpu_single_env
) {
4162 cpu_exit(cpu_single_env
);
4163 cpu_single_env
->stop
= 1;
4174 static void host_main_loop_wait(int *timeout
)
4180 /* XXX: need to suppress polling by better using win32 events */
4182 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4183 ret
|= pe
->func(pe
->opaque
);
4187 WaitObjects
*w
= &wait_objects
;
4189 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4190 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4191 if (w
->func
[ret
- WAIT_OBJECT_0
])
4192 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4194 /* Check for additional signaled events */
4195 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4197 /* Check if event is signaled */
4198 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4199 if(ret2
== WAIT_OBJECT_0
) {
4201 w
->func
[i
](w
->opaque
[i
]);
4202 } else if (ret2
== WAIT_TIMEOUT
) {
4204 err
= GetLastError();
4205 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4208 } else if (ret
== WAIT_TIMEOUT
) {
4210 err
= GetLastError();
4211 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4218 static void host_main_loop_wait(int *timeout
)
4223 void main_loop_wait(int timeout
)
4225 IOHandlerRecord
*ioh
;
4226 fd_set rfds
, wfds
, xfds
;
4230 qemu_bh_update_timeout(&timeout
);
4232 host_main_loop_wait(&timeout
);
4234 /* poll any events */
4235 /* XXX: separate device handlers from system ones */
4240 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4244 (!ioh
->fd_read_poll
||
4245 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4246 FD_SET(ioh
->fd
, &rfds
);
4250 if (ioh
->fd_write
) {
4251 FD_SET(ioh
->fd
, &wfds
);
4257 tv
.tv_sec
= timeout
/ 1000;
4258 tv
.tv_usec
= (timeout
% 1000) * 1000;
4260 #if defined(CONFIG_SLIRP)
4261 if (slirp_is_inited()) {
4262 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4265 qemu_mutex_unlock_iothread();
4266 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4267 qemu_mutex_lock_iothread();
4269 IOHandlerRecord
**pioh
;
4271 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4272 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4273 ioh
->fd_read(ioh
->opaque
);
4275 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4276 ioh
->fd_write(ioh
->opaque
);
4280 /* remove deleted IO handlers */
4281 pioh
= &first_io_handler
;
4291 #if defined(CONFIG_SLIRP)
4292 if (slirp_is_inited()) {
4298 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4302 /* rearm timer, if not periodic */
4303 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4304 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4305 qemu_rearm_alarm_timer(alarm_timer
);
4308 /* vm time timers */
4310 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4311 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4312 qemu_get_clock(vm_clock
));
4315 /* real time timers */
4316 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4317 qemu_get_clock(rt_clock
));
4319 /* Check bottom-halves last in case any of the earlier events triggered
4325 static int qemu_cpu_exec(CPUState
*env
)
4328 #ifdef CONFIG_PROFILER
4332 #ifdef CONFIG_PROFILER
4333 ti
= profile_getclock();
4338 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4339 env
->icount_decr
.u16
.low
= 0;
4340 env
->icount_extra
= 0;
4341 count
= qemu_next_deadline();
4342 count
= (count
+ (1 << icount_time_shift
) - 1)
4343 >> icount_time_shift
;
4344 qemu_icount
+= count
;
4345 decr
= (count
> 0xffff) ? 0xffff : count
;
4347 env
->icount_decr
.u16
.low
= decr
;
4348 env
->icount_extra
= count
;
4350 ret
= cpu_exec(env
);
4351 #ifdef CONFIG_PROFILER
4352 qemu_time
+= profile_getclock() - ti
;
4355 /* Fold pending instructions back into the
4356 instruction counter, and clear the interrupt flag. */
4357 qemu_icount
-= (env
->icount_decr
.u16
.low
4358 + env
->icount_extra
);
4359 env
->icount_decr
.u32
= 0;
4360 env
->icount_extra
= 0;
4365 static void tcg_cpu_exec(void)
4369 if (next_cpu
== NULL
)
4370 next_cpu
= first_cpu
;
4371 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4372 CPUState
*env
= cur_cpu
= next_cpu
;
4376 if (timer_alarm_pending
) {
4377 timer_alarm_pending
= 0;
4380 if (cpu_can_run(env
))
4381 ret
= qemu_cpu_exec(env
);
4382 if (ret
== EXCP_DEBUG
) {
4383 gdb_set_stop_cpu(env
);
4384 debug_requested
= 1;
4390 static int cpu_has_work(CPUState
*env
)
4398 if (qemu_cpu_has_work(env
))
4403 static int tcg_has_work(void)
4407 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4408 if (cpu_has_work(env
))
4413 static int qemu_calculate_timeout(void)
4415 #ifndef CONFIG_IOTHREAD
4420 else if (tcg_has_work())
4422 else if (!use_icount
)
4425 /* XXX: use timeout computed from timers */
4428 /* Advance virtual time to the next event. */
4429 if (use_icount
== 1) {
4430 /* When not using an adaptive execution frequency
4431 we tend to get badly out of sync with real time,
4432 so just delay for a reasonable amount of time. */
4435 delta
= cpu_get_icount() - cpu_get_clock();
4438 /* If virtual time is ahead of real time then just
4440 timeout
= (delta
/ 1000000) + 1;
4442 /* Wait for either IO to occur or the next
4444 add
= qemu_next_deadline();
4445 /* We advance the timer before checking for IO.
4446 Limit the amount we advance so that early IO
4447 activity won't get the guest too far ahead. */
4451 add
= (add
+ (1 << icount_time_shift
) - 1)
4452 >> icount_time_shift
;
4454 timeout
= delta
/ 1000000;
4461 #else /* CONFIG_IOTHREAD */
4466 static int vm_can_run(void)
4468 if (powerdown_requested
)
4470 if (reset_requested
)
4472 if (shutdown_requested
)
4474 if (debug_requested
)
4479 static void main_loop(void)
4483 #ifdef CONFIG_IOTHREAD
4484 qemu_system_ready
= 1;
4485 qemu_cond_broadcast(&qemu_system_cond
);
4490 #ifdef CONFIG_PROFILER
4493 #ifndef CONFIG_IOTHREAD
4496 #ifdef CONFIG_PROFILER
4497 ti
= profile_getclock();
4499 main_loop_wait(qemu_calculate_timeout());
4500 #ifdef CONFIG_PROFILER
4501 dev_time
+= profile_getclock() - ti
;
4503 } while (vm_can_run());
4505 if (qemu_debug_requested())
4506 vm_stop(EXCP_DEBUG
);
4507 if (qemu_shutdown_requested()) {
4514 if (qemu_reset_requested()) {
4516 qemu_system_reset();
4519 if (qemu_powerdown_requested())
4520 qemu_system_powerdown();
4521 if ((r
= qemu_vmstop_requested()))
4527 static void version(void)
4529 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4532 static void help(int exitcode
)
4535 printf("usage: %s [options] [disk_image]\n"
4537 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4539 #define DEF(option, opt_arg, opt_enum, opt_help) \
4541 #define DEFHEADING(text) stringify(text) "\n"
4542 #include "qemu-options.h"
4547 "During emulation, the following keys are useful:\n"
4548 "ctrl-alt-f toggle full screen\n"
4549 "ctrl-alt-n switch to virtual console 'n'\n"
4550 "ctrl-alt toggle mouse and keyboard grab\n"
4552 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4557 DEFAULT_NETWORK_SCRIPT
,
4558 DEFAULT_NETWORK_DOWN_SCRIPT
,
4560 DEFAULT_GDBSTUB_PORT
,
4565 #define HAS_ARG 0x0001
4568 #define DEF(option, opt_arg, opt_enum, opt_help) \
4570 #define DEFHEADING(text)
4571 #include "qemu-options.h"
4577 typedef struct QEMUOption
{
4583 static const QEMUOption qemu_options
[] = {
4584 { "h", 0, QEMU_OPTION_h
},
4585 #define DEF(option, opt_arg, opt_enum, opt_help) \
4586 { option, opt_arg, opt_enum },
4587 #define DEFHEADING(text)
4588 #include "qemu-options.h"
4596 struct soundhw soundhw
[] = {
4597 #ifdef HAS_AUDIO_CHOICE
4598 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4604 { .init_isa
= pcspk_audio_init
}
4611 "Creative Sound Blaster 16",
4614 { .init_isa
= SB16_init
}
4618 #ifdef CONFIG_CS4231A
4624 { .init_isa
= cs4231a_init
}
4632 "Yamaha YMF262 (OPL3)",
4634 "Yamaha YM3812 (OPL2)",
4638 { .init_isa
= Adlib_init
}
4645 "Gravis Ultrasound GF1",
4648 { .init_isa
= GUS_init
}
4655 "Intel 82801AA AC97 Audio",
4658 { .init_pci
= ac97_init
}
4662 #ifdef CONFIG_ES1370
4665 "ENSONIQ AudioPCI ES1370",
4668 { .init_pci
= es1370_init
}
4672 #endif /* HAS_AUDIO_CHOICE */
4674 { NULL
, NULL
, 0, 0, { NULL
} }
4677 static void select_soundhw (const char *optarg
)
4681 if (*optarg
== '?') {
4684 printf ("Valid sound card names (comma separated):\n");
4685 for (c
= soundhw
; c
->name
; ++c
) {
4686 printf ("%-11s %s\n", c
->name
, c
->descr
);
4688 printf ("\n-soundhw all will enable all of the above\n");
4689 exit (*optarg
!= '?');
4697 if (!strcmp (optarg
, "all")) {
4698 for (c
= soundhw
; c
->name
; ++c
) {
4706 e
= strchr (p
, ',');
4707 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4709 for (c
= soundhw
; c
->name
; ++c
) {
4710 if (!strncmp (c
->name
, p
, l
)) {
4719 "Unknown sound card name (too big to show)\n");
4722 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4727 p
+= l
+ (e
!= NULL
);
4731 goto show_valid_cards
;
4736 static void select_vgahw (const char *p
)
4740 cirrus_vga_enabled
= 0;
4741 std_vga_enabled
= 0;
4744 if (strstart(p
, "std", &opts
)) {
4745 std_vga_enabled
= 1;
4746 } else if (strstart(p
, "cirrus", &opts
)) {
4747 cirrus_vga_enabled
= 1;
4748 } else if (strstart(p
, "vmware", &opts
)) {
4750 } else if (strstart(p
, "xenfb", &opts
)) {
4752 } else if (!strstart(p
, "none", &opts
)) {
4754 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4758 const char *nextopt
;
4760 if (strstart(opts
, ",retrace=", &nextopt
)) {
4762 if (strstart(opts
, "dumb", &nextopt
))
4763 vga_retrace_method
= VGA_RETRACE_DUMB
;
4764 else if (strstart(opts
, "precise", &nextopt
))
4765 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4766 else goto invalid_vga
;
4767 } else goto invalid_vga
;
4773 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4775 exit(STATUS_CONTROL_C_EXIT
);
4780 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4784 if(strlen(str
) != 36)
4787 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4788 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4789 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4795 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4801 #define MAX_NET_CLIENTS 32
4805 static void termsig_handler(int signal
)
4807 qemu_system_shutdown_request();
4810 static void sigchld_handler(int signal
)
4812 waitpid(-1, NULL
, WNOHANG
);
4815 static void sighandler_setup(void)
4817 struct sigaction act
;
4819 memset(&act
, 0, sizeof(act
));
4820 act
.sa_handler
= termsig_handler
;
4821 sigaction(SIGINT
, &act
, NULL
);
4822 sigaction(SIGHUP
, &act
, NULL
);
4823 sigaction(SIGTERM
, &act
, NULL
);
4825 act
.sa_handler
= sigchld_handler
;
4826 act
.sa_flags
= SA_NOCLDSTOP
;
4827 sigaction(SIGCHLD
, &act
, NULL
);
4833 /* Look for support files in the same directory as the executable. */
4834 static char *find_datadir(const char *argv0
)
4840 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4847 while (p
!= buf
&& *p
!= '\\')
4850 if (access(buf
, R_OK
) == 0) {
4851 return qemu_strdup(buf
);
4857 /* Find a likely location for support files using the location of the binary.
4858 For installed binaries this will be "$bindir/../share/qemu". When
4859 running from the build tree this will be "$bindir/../pc-bios". */
4860 #define SHARE_SUFFIX "/share/qemu"
4861 #define BUILD_SUFFIX "/pc-bios"
4862 static char *find_datadir(const char *argv0
)
4872 #if defined(__linux__)
4875 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4881 #elif defined(__FreeBSD__)
4884 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4891 /* If we don't have any way of figuring out the actual executable
4892 location then try argv[0]. */
4897 p
= realpath(argv0
, p
);
4905 max_len
= strlen(dir
) +
4906 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4907 res
= qemu_mallocz(max_len
);
4908 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4909 if (access(res
, R_OK
)) {
4910 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4911 if (access(res
, R_OK
)) {
4925 char *qemu_find_file(int type
, const char *name
)
4931 /* If name contains path separators then try it as a straight path. */
4932 if ((strchr(name
, '/') || strchr(name
, '\\'))
4933 && access(name
, R_OK
) == 0) {
4934 return strdup(name
);
4937 case QEMU_FILE_TYPE_BIOS
:
4940 case QEMU_FILE_TYPE_KEYMAP
:
4941 subdir
= "keymaps/";
4946 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4947 buf
= qemu_mallocz(len
);
4948 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4949 if (access(buf
, R_OK
)) {
4956 int main(int argc
, char **argv
, char **envp
)
4958 const char *gdbstub_dev
= NULL
;
4959 uint32_t boot_devices_bitmap
= 0;
4961 int snapshot
, linux_boot
, net_boot
;
4962 const char *initrd_filename
;
4963 const char *kernel_filename
, *kernel_cmdline
;
4964 const char *boot_devices
= "";
4966 DisplayChangeListener
*dcl
;
4967 int cyls
, heads
, secs
, translation
;
4968 const char *net_clients
[MAX_NET_CLIENTS
];
4970 const char *bt_opts
[MAX_BT_CMDLINE
];
4974 const char *r
, *optarg
;
4975 CharDriverState
*monitor_hd
= NULL
;
4976 const char *monitor_device
;
4977 const char *serial_devices
[MAX_SERIAL_PORTS
];
4978 int serial_device_index
;
4979 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4980 int parallel_device_index
;
4981 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4982 int virtio_console_index
;
4983 const char *loadvm
= NULL
;
4984 QEMUMachine
*machine
;
4985 const char *cpu_model
;
4986 const char *usb_devices
[MAX_USB_CMDLINE
];
4987 int usb_devices_index
;
4992 const char *pid_file
= NULL
;
4993 const char *incoming
= NULL
;
4996 struct passwd
*pwd
= NULL
;
4997 const char *chroot_dir
= NULL
;
4998 const char *run_as
= NULL
;
5001 int show_vnc_port
= 0;
5003 qemu_cache_utils_init(envp
);
5005 LIST_INIT (&vm_change_state_head
);
5008 struct sigaction act
;
5009 sigfillset(&act
.sa_mask
);
5011 act
.sa_handler
= SIG_IGN
;
5012 sigaction(SIGPIPE
, &act
, NULL
);
5015 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
5016 /* Note: cpu_interrupt() is currently not SMP safe, so we force
5017 QEMU to run on a single CPU */
5022 h
= GetCurrentProcess();
5023 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
5024 for(i
= 0; i
< 32; i
++) {
5025 if (mask
& (1 << i
))
5030 SetProcessAffinityMask(h
, mask
);
5036 module_call_init(MODULE_INIT_MACHINE
);
5037 machine
= find_default_machine();
5039 initrd_filename
= NULL
;
5042 kernel_filename
= NULL
;
5043 kernel_cmdline
= "";
5044 cyls
= heads
= secs
= 0;
5045 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5046 monitor_device
= "vc:80Cx24C";
5048 serial_devices
[0] = "vc:80Cx24C";
5049 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5050 serial_devices
[i
] = NULL
;
5051 serial_device_index
= 0;
5053 parallel_devices
[0] = "vc:80Cx24C";
5054 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5055 parallel_devices
[i
] = NULL
;
5056 parallel_device_index
= 0;
5058 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
5059 virtio_consoles
[i
] = NULL
;
5060 virtio_console_index
= 0;
5062 for (i
= 0; i
< MAX_NODES
; i
++) {
5064 node_cpumask
[i
] = 0;
5067 usb_devices_index
= 0;
5081 register_watchdogs();
5089 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
5091 const QEMUOption
*popt
;
5094 /* Treat --foo the same as -foo. */
5097 popt
= qemu_options
;
5100 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5104 if (!strcmp(popt
->name
, r
+ 1))
5108 if (popt
->flags
& HAS_ARG
) {
5109 if (optind
>= argc
) {
5110 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5114 optarg
= argv
[optind
++];
5119 switch(popt
->index
) {
5121 machine
= find_machine(optarg
);
5124 printf("Supported machines are:\n");
5125 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5126 printf("%-10s %s%s\n",
5128 m
->is_default
? " (default)" : "");
5130 exit(*optarg
!= '?');
5133 case QEMU_OPTION_cpu
:
5134 /* hw initialization will check this */
5135 if (*optarg
== '?') {
5136 /* XXX: implement xxx_cpu_list for targets that still miss it */
5137 #if defined(cpu_list)
5138 cpu_list(stdout
, &fprintf
);
5145 case QEMU_OPTION_initrd
:
5146 initrd_filename
= optarg
;
5148 case QEMU_OPTION_hda
:
5150 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5152 hda_index
= drive_add(optarg
, HD_ALIAS
5153 ",cyls=%d,heads=%d,secs=%d%s",
5154 0, cyls
, heads
, secs
,
5155 translation
== BIOS_ATA_TRANSLATION_LBA
?
5157 translation
== BIOS_ATA_TRANSLATION_NONE
?
5158 ",trans=none" : "");
5160 case QEMU_OPTION_hdb
:
5161 case QEMU_OPTION_hdc
:
5162 case QEMU_OPTION_hdd
:
5163 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5165 case QEMU_OPTION_drive
:
5166 drive_add(NULL
, "%s", optarg
);
5168 case QEMU_OPTION_mtdblock
:
5169 drive_add(optarg
, MTD_ALIAS
);
5171 case QEMU_OPTION_sd
:
5172 drive_add(optarg
, SD_ALIAS
);
5174 case QEMU_OPTION_pflash
:
5175 drive_add(optarg
, PFLASH_ALIAS
);
5177 case QEMU_OPTION_snapshot
:
5180 case QEMU_OPTION_hdachs
:
5184 cyls
= strtol(p
, (char **)&p
, 0);
5185 if (cyls
< 1 || cyls
> 16383)
5190 heads
= strtol(p
, (char **)&p
, 0);
5191 if (heads
< 1 || heads
> 16)
5196 secs
= strtol(p
, (char **)&p
, 0);
5197 if (secs
< 1 || secs
> 63)
5201 if (!strcmp(p
, "none"))
5202 translation
= BIOS_ATA_TRANSLATION_NONE
;
5203 else if (!strcmp(p
, "lba"))
5204 translation
= BIOS_ATA_TRANSLATION_LBA
;
5205 else if (!strcmp(p
, "auto"))
5206 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5209 } else if (*p
!= '\0') {
5211 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5214 if (hda_index
!= -1)
5215 snprintf(drives_opt
[hda_index
].opt
,
5216 sizeof(drives_opt
[hda_index
].opt
),
5217 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5218 0, cyls
, heads
, secs
,
5219 translation
== BIOS_ATA_TRANSLATION_LBA
?
5221 translation
== BIOS_ATA_TRANSLATION_NONE
?
5222 ",trans=none" : "");
5225 case QEMU_OPTION_numa
:
5226 if (nb_numa_nodes
>= MAX_NODES
) {
5227 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5232 case QEMU_OPTION_nographic
:
5233 display_type
= DT_NOGRAPHIC
;
5235 #ifdef CONFIG_CURSES
5236 case QEMU_OPTION_curses
:
5237 display_type
= DT_CURSES
;
5240 case QEMU_OPTION_portrait
:
5243 case QEMU_OPTION_kernel
:
5244 kernel_filename
= optarg
;
5246 case QEMU_OPTION_append
:
5247 kernel_cmdline
= optarg
;
5249 case QEMU_OPTION_cdrom
:
5250 drive_add(optarg
, CDROM_ALIAS
);
5252 case QEMU_OPTION_boot
:
5253 boot_devices
= optarg
;
5254 /* We just do some generic consistency checks */
5256 /* Could easily be extended to 64 devices if needed */
5259 boot_devices_bitmap
= 0;
5260 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5261 /* Allowed boot devices are:
5262 * a b : floppy disk drives
5263 * c ... f : IDE disk drives
5264 * g ... m : machine implementation dependant drives
5265 * n ... p : network devices
5266 * It's up to each machine implementation to check
5267 * if the given boot devices match the actual hardware
5268 * implementation and firmware features.
5270 if (*p
< 'a' || *p
> 'q') {
5271 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5274 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5276 "Boot device '%c' was given twice\n",*p
);
5279 boot_devices_bitmap
|= 1 << (*p
- 'a');
5283 case QEMU_OPTION_fda
:
5284 case QEMU_OPTION_fdb
:
5285 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5288 case QEMU_OPTION_no_fd_bootchk
:
5292 case QEMU_OPTION_net
:
5293 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5294 fprintf(stderr
, "qemu: too many network clients\n");
5297 net_clients
[nb_net_clients
] = optarg
;
5301 case QEMU_OPTION_tftp
:
5302 tftp_prefix
= optarg
;
5304 case QEMU_OPTION_bootp
:
5305 bootp_filename
= optarg
;
5308 case QEMU_OPTION_smb
:
5309 net_slirp_smb(optarg
);
5312 case QEMU_OPTION_redir
:
5313 net_slirp_redir(NULL
, optarg
, NULL
);
5316 case QEMU_OPTION_bt
:
5317 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5318 fprintf(stderr
, "qemu: too many bluetooth options\n");
5321 bt_opts
[nb_bt_opts
++] = optarg
;
5324 case QEMU_OPTION_audio_help
:
5328 case QEMU_OPTION_soundhw
:
5329 select_soundhw (optarg
);
5335 case QEMU_OPTION_version
:
5339 case QEMU_OPTION_m
: {
5343 value
= strtoul(optarg
, &ptr
, 10);
5345 case 0: case 'M': case 'm':
5352 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5356 /* On 32-bit hosts, QEMU is limited by virtual address space */
5357 if (value
> (2047 << 20)
5358 #ifndef CONFIG_KQEMU
5359 && HOST_LONG_BITS
== 32
5362 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5365 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5366 fprintf(stderr
, "qemu: ram size too large\n");
5375 const CPULogItem
*item
;
5377 mask
= cpu_str_to_log_mask(optarg
);
5379 printf("Log items (comma separated):\n");
5380 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5381 printf("%-10s %s\n", item
->name
, item
->help
);
5389 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5391 case QEMU_OPTION_gdb
:
5392 gdbstub_dev
= optarg
;
5397 case QEMU_OPTION_bios
:
5400 case QEMU_OPTION_singlestep
:
5408 keyboard_layout
= optarg
;
5411 case QEMU_OPTION_localtime
:
5414 case QEMU_OPTION_vga
:
5415 select_vgahw (optarg
);
5417 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5423 w
= strtol(p
, (char **)&p
, 10);
5426 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5432 h
= strtol(p
, (char **)&p
, 10);
5437 depth
= strtol(p
, (char **)&p
, 10);
5438 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5439 depth
!= 24 && depth
!= 32)
5441 } else if (*p
== '\0') {
5442 depth
= graphic_depth
;
5449 graphic_depth
= depth
;
5453 case QEMU_OPTION_echr
:
5456 term_escape_char
= strtol(optarg
, &r
, 0);
5458 printf("Bad argument to echr\n");
5461 case QEMU_OPTION_monitor
:
5462 monitor_device
= optarg
;
5464 case QEMU_OPTION_serial
:
5465 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5466 fprintf(stderr
, "qemu: too many serial ports\n");
5469 serial_devices
[serial_device_index
] = optarg
;
5470 serial_device_index
++;
5472 case QEMU_OPTION_watchdog
:
5473 i
= select_watchdog(optarg
);
5475 exit (i
== 1 ? 1 : 0);
5477 case QEMU_OPTION_watchdog_action
:
5478 if (select_watchdog_action(optarg
) == -1) {
5479 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5483 case QEMU_OPTION_virtiocon
:
5484 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5485 fprintf(stderr
, "qemu: too many virtio consoles\n");
5488 virtio_consoles
[virtio_console_index
] = optarg
;
5489 virtio_console_index
++;
5491 case QEMU_OPTION_parallel
:
5492 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5493 fprintf(stderr
, "qemu: too many parallel ports\n");
5496 parallel_devices
[parallel_device_index
] = optarg
;
5497 parallel_device_index
++;
5499 case QEMU_OPTION_loadvm
:
5502 case QEMU_OPTION_full_screen
:
5506 case QEMU_OPTION_no_frame
:
5509 case QEMU_OPTION_alt_grab
:
5512 case QEMU_OPTION_no_quit
:
5515 case QEMU_OPTION_sdl
:
5516 display_type
= DT_SDL
;
5519 case QEMU_OPTION_pidfile
:
5523 case QEMU_OPTION_win2k_hack
:
5524 win2k_install_hack
= 1;
5526 case QEMU_OPTION_rtc_td_hack
:
5529 case QEMU_OPTION_acpitable
:
5530 if(acpi_table_add(optarg
) < 0) {
5531 fprintf(stderr
, "Wrong acpi table provided\n");
5535 case QEMU_OPTION_smbios
:
5536 if(smbios_entry_add(optarg
) < 0) {
5537 fprintf(stderr
, "Wrong smbios provided\n");
5543 case QEMU_OPTION_no_kqemu
:
5546 case QEMU_OPTION_kernel_kqemu
:
5551 case QEMU_OPTION_enable_kvm
:
5558 case QEMU_OPTION_usb
:
5561 case QEMU_OPTION_usbdevice
:
5563 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5564 fprintf(stderr
, "Too many USB devices\n");
5567 usb_devices
[usb_devices_index
] = optarg
;
5568 usb_devices_index
++;
5570 case QEMU_OPTION_smp
:
5571 smp_cpus
= atoi(optarg
);
5573 fprintf(stderr
, "Invalid number of CPUs\n");
5577 case QEMU_OPTION_vnc
:
5578 display_type
= DT_VNC
;
5579 vnc_display
= optarg
;
5582 case QEMU_OPTION_no_acpi
:
5585 case QEMU_OPTION_no_hpet
:
5588 case QEMU_OPTION_no_virtio_balloon
:
5589 no_virtio_balloon
= 1;
5592 case QEMU_OPTION_no_reboot
:
5595 case QEMU_OPTION_no_shutdown
:
5598 case QEMU_OPTION_show_cursor
:
5601 case QEMU_OPTION_uuid
:
5602 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5603 fprintf(stderr
, "Fail to parse UUID string."
5604 " Wrong format.\n");
5609 case QEMU_OPTION_daemonize
:
5613 case QEMU_OPTION_option_rom
:
5614 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5615 fprintf(stderr
, "Too many option ROMs\n");
5618 option_rom
[nb_option_roms
] = optarg
;
5621 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5622 case QEMU_OPTION_semihosting
:
5623 semihosting_enabled
= 1;
5626 case QEMU_OPTION_name
:
5629 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5630 case QEMU_OPTION_prom_env
:
5631 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5632 fprintf(stderr
, "Too many prom variables\n");
5635 prom_envs
[nb_prom_envs
] = optarg
;
5640 case QEMU_OPTION_old_param
:
5644 case QEMU_OPTION_clock
:
5645 configure_alarms(optarg
);
5647 case QEMU_OPTION_startdate
:
5650 time_t rtc_start_date
;
5651 if (!strcmp(optarg
, "now")) {
5652 rtc_date_offset
= -1;
5654 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5662 } else if (sscanf(optarg
, "%d-%d-%d",
5665 &tm
.tm_mday
) == 3) {
5674 rtc_start_date
= mktimegm(&tm
);
5675 if (rtc_start_date
== -1) {
5677 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5678 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5681 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5685 case QEMU_OPTION_tb_size
:
5686 tb_size
= strtol(optarg
, NULL
, 0);
5690 case QEMU_OPTION_icount
:
5692 if (strcmp(optarg
, "auto") == 0) {
5693 icount_time_shift
= -1;
5695 icount_time_shift
= strtol(optarg
, NULL
, 0);
5698 case QEMU_OPTION_incoming
:
5702 case QEMU_OPTION_chroot
:
5703 chroot_dir
= optarg
;
5705 case QEMU_OPTION_runas
:
5710 case QEMU_OPTION_xen_domid
:
5711 xen_domid
= atoi(optarg
);
5713 case QEMU_OPTION_xen_create
:
5714 xen_mode
= XEN_CREATE
;
5716 case QEMU_OPTION_xen_attach
:
5717 xen_mode
= XEN_ATTACH
;
5724 /* If no data_dir is specified then try to find it relative to the
5727 data_dir
= find_datadir(argv
[0]);
5729 /* If all else fails use the install patch specified when building. */
5731 data_dir
= CONFIG_QEMU_SHAREDIR
;
5734 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5735 if (kvm_allowed
&& kqemu_allowed
) {
5737 "You can not enable both KVM and kqemu at the same time\n");
5742 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5743 if (smp_cpus
> machine
->max_cpus
) {
5744 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5745 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5750 if (display_type
== DT_NOGRAPHIC
) {
5751 if (serial_device_index
== 0)
5752 serial_devices
[0] = "stdio";
5753 if (parallel_device_index
== 0)
5754 parallel_devices
[0] = "null";
5755 if (strncmp(monitor_device
, "vc", 2) == 0)
5756 monitor_device
= "stdio";
5763 if (pipe(fds
) == -1)
5774 len
= read(fds
[0], &status
, 1);
5775 if (len
== -1 && (errno
== EINTR
))
5780 else if (status
== 1) {
5781 fprintf(stderr
, "Could not acquire pidfile\n");
5798 signal(SIGTSTP
, SIG_IGN
);
5799 signal(SIGTTOU
, SIG_IGN
);
5800 signal(SIGTTIN
, SIG_IGN
);
5803 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5806 write(fds
[1], &status
, 1);
5808 fprintf(stderr
, "Could not acquire pid file\n");
5817 if (qemu_init_main_loop()) {
5818 fprintf(stderr
, "qemu_init_main_loop failed\n");
5821 linux_boot
= (kernel_filename
!= NULL
);
5823 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5824 fprintf(stderr
, "-append only allowed with -kernel option\n");
5828 if (!linux_boot
&& initrd_filename
!= NULL
) {
5829 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5833 /* boot to floppy or the default cd if no hard disk defined yet */
5834 if (!boot_devices
[0]) {
5835 boot_devices
= "cad";
5837 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5840 if (init_timer_alarm() < 0) {
5841 fprintf(stderr
, "could not initialize alarm timer\n");
5844 if (use_icount
&& icount_time_shift
< 0) {
5846 /* 125MIPS seems a reasonable initial guess at the guest speed.
5847 It will be corrected fairly quickly anyway. */
5848 icount_time_shift
= 3;
5849 init_icount_adjust();
5856 /* init network clients */
5857 if (nb_net_clients
== 0) {
5858 /* if no clients, we use a default config */
5859 net_clients
[nb_net_clients
++] = "nic";
5861 net_clients
[nb_net_clients
++] = "user";
5865 for(i
= 0;i
< nb_net_clients
; i
++) {
5866 if (net_client_parse(net_clients
[i
]) < 0)
5870 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5871 net_set_boot_mask(net_boot
);
5875 /* init the bluetooth world */
5876 for (i
= 0; i
< nb_bt_opts
; i
++)
5877 if (bt_parse(bt_opts
[i
]))
5880 /* init the memory */
5882 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5885 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5886 guest ram allocation. It needs to go away. */
5887 if (kqemu_allowed
) {
5888 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5889 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5890 if (!kqemu_phys_ram_base
) {
5891 fprintf(stderr
, "Could not allocate physical memory\n");
5897 /* init the dynamic translator */
5898 cpu_exec_init_all(tb_size
* 1024 * 1024);
5902 /* we always create the cdrom drive, even if no disk is there */
5904 if (nb_drives_opt
< MAX_DRIVES
)
5905 drive_add(NULL
, CDROM_ALIAS
);
5907 /* we always create at least one floppy */
5909 if (nb_drives_opt
< MAX_DRIVES
)
5910 drive_add(NULL
, FD_ALIAS
, 0);
5912 /* we always create one sd slot, even if no card is in it */
5914 if (nb_drives_opt
< MAX_DRIVES
)
5915 drive_add(NULL
, SD_ALIAS
);
5917 /* open the virtual block devices */
5919 for(i
= 0; i
< nb_drives_opt
; i
++)
5920 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5923 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5924 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5927 /* must be after terminal init, SDL library changes signal handlers */
5931 /* Maintain compatibility with multiple stdio monitors */
5932 if (!strcmp(monitor_device
,"stdio")) {
5933 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5934 const char *devname
= serial_devices
[i
];
5935 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5936 monitor_device
= NULL
;
5938 } else if (devname
&& !strcmp(devname
,"stdio")) {
5939 monitor_device
= NULL
;
5940 serial_devices
[i
] = "mon:stdio";
5946 if (nb_numa_nodes
> 0) {
5949 if (nb_numa_nodes
> smp_cpus
) {
5950 nb_numa_nodes
= smp_cpus
;
5953 /* If no memory size if given for any node, assume the default case
5954 * and distribute the available memory equally across all nodes
5956 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5957 if (node_mem
[i
] != 0)
5960 if (i
== nb_numa_nodes
) {
5961 uint64_t usedmem
= 0;
5963 /* On Linux, the each node's border has to be 8MB aligned,
5964 * the final node gets the rest.
5966 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5967 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5968 usedmem
+= node_mem
[i
];
5970 node_mem
[i
] = ram_size
- usedmem
;
5973 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5974 if (node_cpumask
[i
] != 0)
5977 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5978 * must cope with this anyway, because there are BIOSes out there in
5979 * real machines which also use this scheme.
5981 if (i
== nb_numa_nodes
) {
5982 for (i
= 0; i
< smp_cpus
; i
++) {
5983 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5988 if (kvm_enabled()) {
5991 ret
= kvm_init(smp_cpus
);
5993 fprintf(stderr
, "failed to initialize KVM\n");
5998 if (monitor_device
) {
5999 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
6001 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6006 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6007 const char *devname
= serial_devices
[i
];
6008 if (devname
&& strcmp(devname
, "none")) {
6010 snprintf(label
, sizeof(label
), "serial%d", i
);
6011 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6012 if (!serial_hds
[i
]) {
6013 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6020 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6021 const char *devname
= parallel_devices
[i
];
6022 if (devname
&& strcmp(devname
, "none")) {
6024 snprintf(label
, sizeof(label
), "parallel%d", i
);
6025 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6026 if (!parallel_hds
[i
]) {
6027 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6034 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6035 const char *devname
= virtio_consoles
[i
];
6036 if (devname
&& strcmp(devname
, "none")) {
6038 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6039 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6040 if (!virtcon_hds
[i
]) {
6041 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
6048 module_call_init(MODULE_INIT_DEVICE
);
6050 machine
->init(ram_size
, boot_devices
,
6051 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6054 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
6055 for (i
= 0; i
< nb_numa_nodes
; i
++) {
6056 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
6062 current_machine
= machine
;
6064 /* Set KVM's vcpu state to qemu's initial CPUState. */
6065 if (kvm_enabled()) {
6068 ret
= kvm_sync_vcpus();
6070 fprintf(stderr
, "failed to initialize vcpus\n");
6075 /* init USB devices */
6077 for(i
= 0; i
< usb_devices_index
; i
++) {
6078 if (usb_device_add(usb_devices
[i
], 0) < 0) {
6079 fprintf(stderr
, "Warning: could not add USB device %s\n",
6086 dumb_display_init();
6087 /* just use the first displaystate for the moment */
6090 if (display_type
== DT_DEFAULT
) {
6091 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6092 display_type
= DT_SDL
;
6094 display_type
= DT_VNC
;
6095 vnc_display
= "localhost:0,to=99";
6101 switch (display_type
) {
6104 #if defined(CONFIG_CURSES)
6106 curses_display_init(ds
, full_screen
);
6109 #if defined(CONFIG_SDL)
6111 sdl_display_init(ds
, full_screen
, no_frame
);
6113 #elif defined(CONFIG_COCOA)
6115 cocoa_display_init(ds
, full_screen
);
6119 vnc_display_init(ds
);
6120 if (vnc_display_open(ds
, vnc_display
) < 0)
6123 if (show_vnc_port
) {
6124 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6132 dcl
= ds
->listeners
;
6133 while (dcl
!= NULL
) {
6134 if (dcl
->dpy_refresh
!= NULL
) {
6135 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6136 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6141 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6142 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6143 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6146 text_consoles_set_display(display_state
);
6147 qemu_chr_initial_reset();
6149 if (monitor_device
&& monitor_hd
)
6150 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6152 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6153 const char *devname
= serial_devices
[i
];
6154 if (devname
&& strcmp(devname
, "none")) {
6155 if (strstart(devname
, "vc", 0))
6156 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6160 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6161 const char *devname
= parallel_devices
[i
];
6162 if (devname
&& strcmp(devname
, "none")) {
6163 if (strstart(devname
, "vc", 0))
6164 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6168 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6169 const char *devname
= virtio_consoles
[i
];
6170 if (virtcon_hds
[i
] && devname
) {
6171 if (strstart(devname
, "vc", 0))
6172 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6176 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6177 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6183 do_loadvm(cur_mon
, loadvm
);
6186 autostart
= 0; /* fixme how to deal with -daemonize */
6187 qemu_start_incoming_migration(incoming
);
6199 len
= write(fds
[1], &status
, 1);
6200 if (len
== -1 && (errno
== EINTR
))
6207 TFR(fd
= open("/dev/null", O_RDWR
));
6213 pwd
= getpwnam(run_as
);
6215 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6221 if (chroot(chroot_dir
) < 0) {
6222 fprintf(stderr
, "chroot failed\n");
6229 if (setgid(pwd
->pw_gid
) < 0) {
6230 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6233 if (setuid(pwd
->pw_uid
) < 0) {
6234 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6237 if (setuid(0) != -1) {
6238 fprintf(stderr
, "Dropping privileges failed\n");