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_next_param_value(char *buf
, int buf_size
,
1816 const char *tag
, const char **pstr
)
1823 p
= get_opt_name(option
, sizeof(option
), p
, '=');
1827 if (!strcmp(tag
, option
)) {
1828 *pstr
= get_opt_value(buf
, buf_size
, p
);
1829 if (**pstr
== ',') {
1834 p
= get_opt_value(NULL
, 0, p
);
1843 int get_param_value(char *buf
, int buf_size
,
1844 const char *tag
, const char *str
)
1846 return get_next_param_value(buf
, buf_size
, tag
, &str
);
1849 int check_params(char *buf
, int buf_size
,
1850 const char * const *params
, const char *str
)
1856 while (*p
!= '\0') {
1857 p
= get_opt_name(buf
, buf_size
, p
, '=');
1862 for (i
= 0; params
[i
] != NULL
; i
++) {
1863 if (!strcmp(params
[i
], buf
)) {
1867 if (params
[i
] == NULL
) {
1870 p
= get_opt_value(NULL
, 0, p
);
1879 /***********************************************************/
1880 /* Bluetooth support */
1883 static struct HCIInfo
*hci_table
[MAX_NICS
];
1885 static struct bt_vlan_s
{
1886 struct bt_scatternet_s net
;
1888 struct bt_vlan_s
*next
;
1891 /* find or alloc a new bluetooth "VLAN" */
1892 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1894 struct bt_vlan_s
**pvlan
, *vlan
;
1895 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1899 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1901 pvlan
= &first_bt_vlan
;
1902 while (*pvlan
!= NULL
)
1903 pvlan
= &(*pvlan
)->next
;
1908 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1912 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1917 static struct HCIInfo null_hci
= {
1918 .cmd_send
= null_hci_send
,
1919 .sco_send
= null_hci_send
,
1920 .acl_send
= null_hci_send
,
1921 .bdaddr_set
= null_hci_addr_set
,
1924 struct HCIInfo
*qemu_next_hci(void)
1926 if (cur_hci
== nb_hcis
)
1929 return hci_table
[cur_hci
++];
1932 static struct HCIInfo
*hci_init(const char *str
)
1935 struct bt_scatternet_s
*vlan
= 0;
1937 if (!strcmp(str
, "null"))
1940 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1942 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1943 else if (!strncmp(str
, "hci", 3)) {
1946 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1947 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1952 vlan
= qemu_find_bt_vlan(0);
1954 return bt_new_hci(vlan
);
1957 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1962 static int bt_hci_parse(const char *str
)
1964 struct HCIInfo
*hci
;
1967 if (nb_hcis
>= MAX_NICS
) {
1968 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1972 hci
= hci_init(str
);
1981 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1982 hci
->bdaddr_set(hci
, bdaddr
.b
);
1984 hci_table
[nb_hcis
++] = hci
;
1989 static void bt_vhci_add(int vlan_id
)
1991 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1994 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1995 "an empty scatternet %i\n", vlan_id
);
1997 bt_vhci_init(bt_new_hci(vlan
));
2000 static struct bt_device_s
*bt_device_add(const char *opt
)
2002 struct bt_scatternet_s
*vlan
;
2004 char *endp
= strstr(opt
, ",vlan=");
2005 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
2008 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
2011 vlan_id
= strtol(endp
+ 6, &endp
, 0);
2013 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
2018 vlan
= qemu_find_bt_vlan(vlan_id
);
2021 fprintf(stderr
, "qemu: warning: adding a slave device to "
2022 "an empty scatternet %i\n", vlan_id
);
2024 if (!strcmp(devname
, "keyboard"))
2025 return bt_keyboard_init(vlan
);
2027 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
2031 static int bt_parse(const char *opt
)
2033 const char *endp
, *p
;
2036 if (strstart(opt
, "hci", &endp
)) {
2037 if (!*endp
|| *endp
== ',') {
2039 if (!strstart(endp
, ",vlan=", 0))
2042 return bt_hci_parse(opt
);
2044 } else if (strstart(opt
, "vhci", &endp
)) {
2045 if (!*endp
|| *endp
== ',') {
2047 if (strstart(endp
, ",vlan=", &p
)) {
2048 vlan
= strtol(p
, (char **) &endp
, 0);
2050 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
2054 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
2063 } else if (strstart(opt
, "device:", &endp
))
2064 return !bt_device_add(endp
);
2066 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
2070 /***********************************************************/
2071 /* QEMU Block devices */
2073 #define HD_ALIAS "index=%d,media=disk"
2074 #define CDROM_ALIAS "index=2,media=cdrom"
2075 #define FD_ALIAS "index=%d,if=floppy"
2076 #define PFLASH_ALIAS "if=pflash"
2077 #define MTD_ALIAS "if=mtd"
2078 #define SD_ALIAS "index=0,if=sd"
2080 static int drive_opt_get_free_idx(void)
2084 for (index
= 0; index
< MAX_DRIVES
; index
++)
2085 if (!drives_opt
[index
].used
) {
2086 drives_opt
[index
].used
= 1;
2093 static int drive_get_free_idx(void)
2097 for (index
= 0; index
< MAX_DRIVES
; index
++)
2098 if (!drives_table
[index
].used
) {
2099 drives_table
[index
].used
= 1;
2106 int drive_add(const char *file
, const char *fmt
, ...)
2109 int index
= drive_opt_get_free_idx();
2111 if (nb_drives_opt
>= MAX_DRIVES
|| index
== -1) {
2112 fprintf(stderr
, "qemu: too many drives\n");
2116 drives_opt
[index
].file
= file
;
2118 vsnprintf(drives_opt
[index
].opt
,
2119 sizeof(drives_opt
[0].opt
), fmt
, ap
);
2126 void drive_remove(int index
)
2128 drives_opt
[index
].used
= 0;
2132 int drive_get_index(BlockInterfaceType type
, int bus
, int unit
)
2136 /* seek interface, bus and unit */
2138 for (index
= 0; index
< MAX_DRIVES
; index
++)
2139 if (drives_table
[index
].type
== type
&&
2140 drives_table
[index
].bus
== bus
&&
2141 drives_table
[index
].unit
== unit
&&
2142 drives_table
[index
].used
)
2148 int drive_get_max_bus(BlockInterfaceType type
)
2154 for (index
= 0; index
< nb_drives
; index
++) {
2155 if(drives_table
[index
].type
== type
&&
2156 drives_table
[index
].bus
> max_bus
)
2157 max_bus
= drives_table
[index
].bus
;
2162 const char *drive_get_serial(BlockDriverState
*bdrv
)
2166 for (index
= 0; index
< nb_drives
; index
++)
2167 if (drives_table
[index
].bdrv
== bdrv
)
2168 return drives_table
[index
].serial
;
2173 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
2177 for (index
= 0; index
< nb_drives
; index
++)
2178 if (drives_table
[index
].bdrv
== bdrv
)
2179 return drives_table
[index
].onerror
;
2181 return BLOCK_ERR_STOP_ENOSPC
;
2184 static void bdrv_format_print(void *opaque
, const char *name
)
2186 fprintf(stderr
, " %s", name
);
2189 void drive_uninit(BlockDriverState
*bdrv
)
2193 for (i
= 0; i
< MAX_DRIVES
; i
++)
2194 if (drives_table
[i
].bdrv
== bdrv
) {
2195 drives_table
[i
].bdrv
= NULL
;
2196 drives_table
[i
].used
= 0;
2197 drive_remove(drives_table
[i
].drive_opt_idx
);
2203 int drive_init(struct drive_opt
*arg
, int snapshot
, void *opaque
)
2209 const char *mediastr
= "";
2210 BlockInterfaceType type
;
2211 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2212 int bus_id
, unit_id
;
2213 int cyls
, heads
, secs
, translation
;
2214 BlockDriverState
*bdrv
;
2215 BlockDriver
*drv
= NULL
;
2216 QEMUMachine
*machine
= opaque
;
2220 int bdrv_flags
, onerror
;
2221 const char *devaddr
;
2222 int drives_table_idx
;
2223 char *str
= arg
->opt
;
2224 static const char * const params
[] = { "bus", "unit", "if", "index",
2225 "cyls", "heads", "secs", "trans",
2226 "media", "snapshot", "file",
2227 "cache", "format", "serial",
2231 if (check_params(buf
, sizeof(buf
), params
, str
) < 0) {
2232 fprintf(stderr
, "qemu: unknown parameter '%s' in '%s'\n",
2238 cyls
= heads
= secs
= 0;
2241 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2245 if (machine
->use_scsi
) {
2247 max_devs
= MAX_SCSI_DEVS
;
2248 pstrcpy(devname
, sizeof(devname
), "scsi");
2251 max_devs
= MAX_IDE_DEVS
;
2252 pstrcpy(devname
, sizeof(devname
), "ide");
2256 /* extract parameters */
2258 if (get_param_value(buf
, sizeof(buf
), "bus", str
)) {
2259 bus_id
= strtol(buf
, NULL
, 0);
2261 fprintf(stderr
, "qemu: '%s' invalid bus id\n", str
);
2266 if (get_param_value(buf
, sizeof(buf
), "unit", str
)) {
2267 unit_id
= strtol(buf
, NULL
, 0);
2269 fprintf(stderr
, "qemu: '%s' invalid unit id\n", str
);
2274 if (get_param_value(buf
, sizeof(buf
), "if", str
)) {
2275 pstrcpy(devname
, sizeof(devname
), buf
);
2276 if (!strcmp(buf
, "ide")) {
2278 max_devs
= MAX_IDE_DEVS
;
2279 } else if (!strcmp(buf
, "scsi")) {
2281 max_devs
= MAX_SCSI_DEVS
;
2282 } else if (!strcmp(buf
, "floppy")) {
2285 } else if (!strcmp(buf
, "pflash")) {
2288 } else if (!strcmp(buf
, "mtd")) {
2291 } else if (!strcmp(buf
, "sd")) {
2294 } else if (!strcmp(buf
, "virtio")) {
2297 } else if (!strcmp(buf
, "xen")) {
2301 fprintf(stderr
, "qemu: '%s' unsupported bus type '%s'\n", str
, buf
);
2306 if (get_param_value(buf
, sizeof(buf
), "index", str
)) {
2307 index
= strtol(buf
, NULL
, 0);
2309 fprintf(stderr
, "qemu: '%s' invalid index\n", str
);
2314 if (get_param_value(buf
, sizeof(buf
), "cyls", str
)) {
2315 cyls
= strtol(buf
, NULL
, 0);
2318 if (get_param_value(buf
, sizeof(buf
), "heads", str
)) {
2319 heads
= strtol(buf
, NULL
, 0);
2322 if (get_param_value(buf
, sizeof(buf
), "secs", str
)) {
2323 secs
= strtol(buf
, NULL
, 0);
2326 if (cyls
|| heads
|| secs
) {
2327 if (cyls
< 1 || cyls
> 16383) {
2328 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", str
);
2331 if (heads
< 1 || heads
> 16) {
2332 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", str
);
2335 if (secs
< 1 || secs
> 63) {
2336 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", str
);
2341 if (get_param_value(buf
, sizeof(buf
), "trans", str
)) {
2344 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2348 if (!strcmp(buf
, "none"))
2349 translation
= BIOS_ATA_TRANSLATION_NONE
;
2350 else if (!strcmp(buf
, "lba"))
2351 translation
= BIOS_ATA_TRANSLATION_LBA
;
2352 else if (!strcmp(buf
, "auto"))
2353 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2355 fprintf(stderr
, "qemu: '%s' invalid translation type\n", str
);
2360 if (get_param_value(buf
, sizeof(buf
), "media", str
)) {
2361 if (!strcmp(buf
, "disk")) {
2363 } else if (!strcmp(buf
, "cdrom")) {
2364 if (cyls
|| secs
|| heads
) {
2366 "qemu: '%s' invalid physical CHS format\n", str
);
2369 media
= MEDIA_CDROM
;
2371 fprintf(stderr
, "qemu: '%s' invalid media\n", str
);
2376 if (get_param_value(buf
, sizeof(buf
), "snapshot", str
)) {
2377 if (!strcmp(buf
, "on"))
2379 else if (!strcmp(buf
, "off"))
2382 fprintf(stderr
, "qemu: '%s' invalid snapshot option\n", str
);
2387 if (get_param_value(buf
, sizeof(buf
), "cache", str
)) {
2388 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2390 else if (!strcmp(buf
, "writethrough"))
2392 else if (!strcmp(buf
, "writeback"))
2395 fprintf(stderr
, "qemu: invalid cache option\n");
2400 if (get_param_value(buf
, sizeof(buf
), "format", str
)) {
2401 if (strcmp(buf
, "?") == 0) {
2402 fprintf(stderr
, "qemu: Supported formats:");
2403 bdrv_iterate_format(bdrv_format_print
, NULL
);
2404 fprintf(stderr
, "\n");
2407 drv
= bdrv_find_format(buf
);
2409 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2414 if (arg
->file
== NULL
)
2415 get_param_value(file
, sizeof(file
), "file", str
);
2417 pstrcpy(file
, sizeof(file
), arg
->file
);
2419 if (!get_param_value(serial
, sizeof(serial
), "serial", str
))
2420 memset(serial
, 0, sizeof(serial
));
2422 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2423 if (get_param_value(buf
, sizeof(serial
), "werror", str
)) {
2424 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2425 fprintf(stderr
, "werror is no supported by this format\n");
2428 if (!strcmp(buf
, "ignore"))
2429 onerror
= BLOCK_ERR_IGNORE
;
2430 else if (!strcmp(buf
, "enospc"))
2431 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2432 else if (!strcmp(buf
, "stop"))
2433 onerror
= BLOCK_ERR_STOP_ANY
;
2434 else if (!strcmp(buf
, "report"))
2435 onerror
= BLOCK_ERR_REPORT
;
2437 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2443 if (get_param_value(buf
, sizeof(buf
), "addr", str
)) {
2444 if (type
!= IF_VIRTIO
) {
2445 fprintf(stderr
, "addr is not supported by in '%s'\n", str
);
2448 devaddr
= strdup(buf
);
2451 /* compute bus and unit according index */
2454 if (bus_id
!= 0 || unit_id
!= -1) {
2456 "qemu: '%s' index cannot be used with bus and unit\n", str
);
2464 unit_id
= index
% max_devs
;
2465 bus_id
= index
/ max_devs
;
2469 /* if user doesn't specify a unit_id,
2470 * try to find the first free
2473 if (unit_id
== -1) {
2475 while (drive_get_index(type
, bus_id
, unit_id
) != -1) {
2477 if (max_devs
&& unit_id
>= max_devs
) {
2478 unit_id
-= max_devs
;
2486 if (max_devs
&& unit_id
>= max_devs
) {
2487 fprintf(stderr
, "qemu: '%s' unit %d too big (max is %d)\n",
2488 str
, unit_id
, max_devs
- 1);
2493 * ignore multiple definitions
2496 if (drive_get_index(type
, bus_id
, unit_id
) != -1)
2501 if (type
== IF_IDE
|| type
== IF_SCSI
)
2502 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2504 snprintf(buf
, sizeof(buf
), "%s%i%s%i",
2505 devname
, bus_id
, mediastr
, unit_id
);
2507 snprintf(buf
, sizeof(buf
), "%s%s%i",
2508 devname
, mediastr
, unit_id
);
2509 bdrv
= bdrv_new(buf
);
2510 drives_table_idx
= drive_get_free_idx();
2511 drives_table
[drives_table_idx
].bdrv
= bdrv
;
2512 drives_table
[drives_table_idx
].devaddr
= devaddr
;
2513 drives_table
[drives_table_idx
].type
= type
;
2514 drives_table
[drives_table_idx
].bus
= bus_id
;
2515 drives_table
[drives_table_idx
].unit
= unit_id
;
2516 drives_table
[drives_table_idx
].onerror
= onerror
;
2517 drives_table
[drives_table_idx
].drive_opt_idx
= arg
- drives_opt
;
2518 strncpy(drives_table
[drives_table_idx
].serial
, serial
, sizeof(serial
));
2528 bdrv_set_geometry_hint(bdrv
, cyls
, heads
, secs
);
2529 bdrv_set_translation_hint(bdrv
, translation
);
2533 bdrv_set_type_hint(bdrv
, BDRV_TYPE_CDROM
);
2538 /* FIXME: This isn't really a floppy, but it's a reasonable
2541 bdrv_set_type_hint(bdrv
, BDRV_TYPE_FLOPPY
);
2554 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2555 cache
= 2; /* always use write-back with snapshot */
2557 if (cache
== 0) /* no caching */
2558 bdrv_flags
|= BDRV_O_NOCACHE
;
2559 else if (cache
== 2) /* write-back */
2560 bdrv_flags
|= BDRV_O_CACHE_WB
;
2561 else if (cache
== 3) /* not specified */
2562 bdrv_flags
|= BDRV_O_CACHE_DEF
;
2563 if (bdrv_open2(bdrv
, file
, bdrv_flags
, drv
) < 0) {
2564 fprintf(stderr
, "qemu: could not open disk image %s\n",
2568 if (bdrv_key_required(bdrv
))
2570 return drives_table_idx
;
2573 static void numa_add(const char *optarg
)
2577 unsigned long long value
, endvalue
;
2580 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2581 if (!strcmp(option
, "node")) {
2582 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2583 nodenr
= nb_numa_nodes
;
2585 nodenr
= strtoull(option
, NULL
, 10);
2588 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2589 node_mem
[nodenr
] = 0;
2591 value
= strtoull(option
, &endptr
, 0);
2593 case 0: case 'M': case 'm':
2600 node_mem
[nodenr
] = value
;
2602 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2603 node_cpumask
[nodenr
] = 0;
2605 value
= strtoull(option
, &endptr
, 10);
2608 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2610 if (*endptr
== '-') {
2611 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2612 if (endvalue
>= 63) {
2615 "only 63 CPUs in NUMA mode supported.\n");
2617 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2622 node_cpumask
[nodenr
] = value
;
2629 /***********************************************************/
2632 static USBPort
*used_usb_ports
;
2633 static USBPort
*free_usb_ports
;
2635 /* ??? Maybe change this to register a hub to keep track of the topology. */
2636 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2637 usb_attachfn attach
)
2639 port
->opaque
= opaque
;
2640 port
->index
= index
;
2641 port
->attach
= attach
;
2642 port
->next
= free_usb_ports
;
2643 free_usb_ports
= port
;
2646 int usb_device_add_dev(USBDevice
*dev
)
2650 /* Find a USB port to add the device to. */
2651 port
= free_usb_ports
;
2655 /* Create a new hub and chain it on. */
2656 free_usb_ports
= NULL
;
2657 port
->next
= used_usb_ports
;
2658 used_usb_ports
= port
;
2660 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2661 usb_attach(port
, hub
);
2662 port
= free_usb_ports
;
2665 free_usb_ports
= port
->next
;
2666 port
->next
= used_usb_ports
;
2667 used_usb_ports
= port
;
2668 usb_attach(port
, dev
);
2672 static void usb_msd_password_cb(void *opaque
, int err
)
2674 USBDevice
*dev
= opaque
;
2677 usb_device_add_dev(dev
);
2679 dev
->handle_destroy(dev
);
2682 static int usb_device_add(const char *devname
, int is_hotplug
)
2687 if (!free_usb_ports
)
2690 if (strstart(devname
, "host:", &p
)) {
2691 dev
= usb_host_device_open(p
);
2692 } else if (!strcmp(devname
, "mouse")) {
2693 dev
= usb_mouse_init();
2694 } else if (!strcmp(devname
, "tablet")) {
2695 dev
= usb_tablet_init();
2696 } else if (!strcmp(devname
, "keyboard")) {
2697 dev
= usb_keyboard_init();
2698 } else if (strstart(devname
, "disk:", &p
)) {
2699 BlockDriverState
*bs
;
2701 dev
= usb_msd_init(p
);
2704 bs
= usb_msd_get_bdrv(dev
);
2705 if (bdrv_key_required(bs
)) {
2708 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2713 } else if (!strcmp(devname
, "wacom-tablet")) {
2714 dev
= usb_wacom_init();
2715 } else if (strstart(devname
, "serial:", &p
)) {
2716 dev
= usb_serial_init(p
);
2717 #ifdef CONFIG_BRLAPI
2718 } else if (!strcmp(devname
, "braille")) {
2719 dev
= usb_baum_init();
2721 } else if (strstart(devname
, "net:", &p
)) {
2724 if (net_client_init(NULL
, "nic", p
) < 0)
2726 nd_table
[nic
].model
= "usb";
2727 dev
= usb_net_init(&nd_table
[nic
]);
2728 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2729 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2730 bt_new_hci(qemu_find_bt_vlan(0)));
2737 return usb_device_add_dev(dev
);
2740 int usb_device_del_addr(int bus_num
, int addr
)
2746 if (!used_usb_ports
)
2752 lastp
= &used_usb_ports
;
2753 port
= used_usb_ports
;
2754 while (port
&& port
->dev
->addr
!= addr
) {
2755 lastp
= &port
->next
;
2763 *lastp
= port
->next
;
2764 usb_attach(port
, NULL
);
2765 dev
->handle_destroy(dev
);
2766 port
->next
= free_usb_ports
;
2767 free_usb_ports
= port
;
2771 static int usb_device_del(const char *devname
)
2776 if (strstart(devname
, "host:", &p
))
2777 return usb_host_device_close(p
);
2779 if (!used_usb_ports
)
2782 p
= strchr(devname
, '.');
2785 bus_num
= strtoul(devname
, NULL
, 0);
2786 addr
= strtoul(p
+ 1, NULL
, 0);
2788 return usb_device_del_addr(bus_num
, addr
);
2791 void do_usb_add(Monitor
*mon
, const char *devname
)
2793 usb_device_add(devname
, 1);
2796 void do_usb_del(Monitor
*mon
, const char *devname
)
2798 usb_device_del(devname
);
2801 void usb_info(Monitor
*mon
)
2805 const char *speed_str
;
2808 monitor_printf(mon
, "USB support not enabled\n");
2812 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2816 switch(dev
->speed
) {
2820 case USB_SPEED_FULL
:
2823 case USB_SPEED_HIGH
:
2830 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2831 0, dev
->addr
, speed_str
, dev
->devname
);
2835 /***********************************************************/
2836 /* PCMCIA/Cardbus */
2838 static struct pcmcia_socket_entry_s
{
2839 PCMCIASocket
*socket
;
2840 struct pcmcia_socket_entry_s
*next
;
2841 } *pcmcia_sockets
= 0;
2843 void pcmcia_socket_register(PCMCIASocket
*socket
)
2845 struct pcmcia_socket_entry_s
*entry
;
2847 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2848 entry
->socket
= socket
;
2849 entry
->next
= pcmcia_sockets
;
2850 pcmcia_sockets
= entry
;
2853 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2855 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2857 ptr
= &pcmcia_sockets
;
2858 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2859 if (entry
->socket
== socket
) {
2865 void pcmcia_info(Monitor
*mon
)
2867 struct pcmcia_socket_entry_s
*iter
;
2869 if (!pcmcia_sockets
)
2870 monitor_printf(mon
, "No PCMCIA sockets\n");
2872 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2873 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2874 iter
->socket
->attached
? iter
->socket
->card_string
:
2878 /***********************************************************/
2879 /* register display */
2881 struct DisplayAllocator default_allocator
= {
2882 defaultallocator_create_displaysurface
,
2883 defaultallocator_resize_displaysurface
,
2884 defaultallocator_free_displaysurface
2887 void register_displaystate(DisplayState
*ds
)
2897 DisplayState
*get_displaystate(void)
2899 return display_state
;
2902 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2904 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2905 return ds
->allocator
;
2910 static void dumb_display_init(void)
2912 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2913 ds
->allocator
= &default_allocator
;
2914 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2915 register_displaystate(ds
);
2918 /***********************************************************/
2921 typedef struct IOHandlerRecord
{
2923 IOCanRWHandler
*fd_read_poll
;
2925 IOHandler
*fd_write
;
2928 /* temporary data */
2930 struct IOHandlerRecord
*next
;
2933 static IOHandlerRecord
*first_io_handler
;
2935 /* XXX: fd_read_poll should be suppressed, but an API change is
2936 necessary in the character devices to suppress fd_can_read(). */
2937 int qemu_set_fd_handler2(int fd
,
2938 IOCanRWHandler
*fd_read_poll
,
2940 IOHandler
*fd_write
,
2943 IOHandlerRecord
**pioh
, *ioh
;
2945 if (!fd_read
&& !fd_write
) {
2946 pioh
= &first_io_handler
;
2951 if (ioh
->fd
== fd
) {
2958 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2962 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2963 ioh
->next
= first_io_handler
;
2964 first_io_handler
= ioh
;
2967 ioh
->fd_read_poll
= fd_read_poll
;
2968 ioh
->fd_read
= fd_read
;
2969 ioh
->fd_write
= fd_write
;
2970 ioh
->opaque
= opaque
;
2976 int qemu_set_fd_handler(int fd
,
2978 IOHandler
*fd_write
,
2981 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2985 /***********************************************************/
2986 /* Polling handling */
2988 typedef struct PollingEntry
{
2991 struct PollingEntry
*next
;
2994 static PollingEntry
*first_polling_entry
;
2996 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2998 PollingEntry
**ppe
, *pe
;
2999 pe
= qemu_mallocz(sizeof(PollingEntry
));
3001 pe
->opaque
= opaque
;
3002 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
3007 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
3009 PollingEntry
**ppe
, *pe
;
3010 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
3012 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
3020 /***********************************************************/
3021 /* Wait objects support */
3022 typedef struct WaitObjects
{
3024 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
3025 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
3026 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
3029 static WaitObjects wait_objects
= {0};
3031 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3033 WaitObjects
*w
= &wait_objects
;
3035 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
3037 w
->events
[w
->num
] = handle
;
3038 w
->func
[w
->num
] = func
;
3039 w
->opaque
[w
->num
] = opaque
;
3044 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
3047 WaitObjects
*w
= &wait_objects
;
3050 for (i
= 0; i
< w
->num
; i
++) {
3051 if (w
->events
[i
] == handle
)
3054 w
->events
[i
] = w
->events
[i
+ 1];
3055 w
->func
[i
] = w
->func
[i
+ 1];
3056 w
->opaque
[i
] = w
->opaque
[i
+ 1];
3064 /***********************************************************/
3065 /* ram save/restore */
3067 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
3071 v
= qemu_get_byte(f
);
3074 if (qemu_get_buffer(f
, buf
, len
) != len
)
3078 v
= qemu_get_byte(f
);
3079 memset(buf
, v
, len
);
3085 if (qemu_file_has_error(f
))
3091 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
3096 if (qemu_get_be32(f
) != last_ram_offset
)
3098 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
3099 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
3106 #define BDRV_HASH_BLOCK_SIZE 1024
3107 #define IOBUF_SIZE 4096
3108 #define RAM_CBLOCK_MAGIC 0xfabe
3110 typedef struct RamDecompressState
{
3113 uint8_t buf
[IOBUF_SIZE
];
3114 } RamDecompressState
;
3116 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
3119 memset(s
, 0, sizeof(*s
));
3121 ret
= inflateInit(&s
->zstream
);
3127 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
3131 s
->zstream
.avail_out
= len
;
3132 s
->zstream
.next_out
= buf
;
3133 while (s
->zstream
.avail_out
> 0) {
3134 if (s
->zstream
.avail_in
== 0) {
3135 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
3137 clen
= qemu_get_be16(s
->f
);
3138 if (clen
> IOBUF_SIZE
)
3140 qemu_get_buffer(s
->f
, s
->buf
, clen
);
3141 s
->zstream
.avail_in
= clen
;
3142 s
->zstream
.next_in
= s
->buf
;
3144 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
3145 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
3152 static void ram_decompress_close(RamDecompressState
*s
)
3154 inflateEnd(&s
->zstream
);
3157 #define RAM_SAVE_FLAG_FULL 0x01
3158 #define RAM_SAVE_FLAG_COMPRESS 0x02
3159 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
3160 #define RAM_SAVE_FLAG_PAGE 0x08
3161 #define RAM_SAVE_FLAG_EOS 0x10
3163 static int is_dup_page(uint8_t *page
, uint8_t ch
)
3165 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
3166 uint32_t *array
= (uint32_t *)page
;
3169 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
3170 if (array
[i
] != val
)
3177 static int ram_save_block(QEMUFile
*f
)
3179 static ram_addr_t current_addr
= 0;
3180 ram_addr_t saved_addr
= current_addr
;
3181 ram_addr_t addr
= 0;
3184 while (addr
< last_ram_offset
) {
3185 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
3188 cpu_physical_memory_reset_dirty(current_addr
,
3189 current_addr
+ TARGET_PAGE_SIZE
,
3190 MIGRATION_DIRTY_FLAG
);
3192 p
= qemu_get_ram_ptr(current_addr
);
3194 if (is_dup_page(p
, *p
)) {
3195 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
3196 qemu_put_byte(f
, *p
);
3198 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3199 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3205 addr
+= TARGET_PAGE_SIZE
;
3206 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3212 static uint64_t bytes_transferred
= 0;
3214 static ram_addr_t
ram_save_remaining(void)
3217 ram_addr_t count
= 0;
3219 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3220 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3227 uint64_t ram_bytes_remaining(void)
3229 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3232 uint64_t ram_bytes_transferred(void)
3234 return bytes_transferred
;
3237 uint64_t ram_bytes_total(void)
3239 return last_ram_offset
;
3242 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3245 uint64_t bytes_transferred_last
;
3247 uint64_t expected_time
= 0;
3249 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3250 qemu_file_set_error(f
);
3255 /* Make sure all dirty bits are set */
3256 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3257 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3258 cpu_physical_memory_set_dirty(addr
);
3261 /* Enable dirty memory tracking */
3262 cpu_physical_memory_set_dirty_tracking(1);
3264 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3267 bytes_transferred_last
= bytes_transferred
;
3268 bwidth
= get_clock();
3270 while (!qemu_file_rate_limit(f
)) {
3273 ret
= ram_save_block(f
);
3274 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3275 if (ret
== 0) /* no more blocks */
3279 bwidth
= get_clock() - bwidth
;
3280 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3282 /* if we haven't transferred anything this round, force expected_time to a
3283 * a very high value, but without crashing */
3287 /* try transferring iterative blocks of memory */
3291 /* flush all remaining blocks regardless of rate limiting */
3292 while (ram_save_block(f
) != 0) {
3293 bytes_transferred
+= TARGET_PAGE_SIZE
;
3295 cpu_physical_memory_set_dirty_tracking(0);
3298 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3300 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3302 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3305 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3307 RamDecompressState s1
, *s
= &s1
;
3311 if (ram_decompress_open(s
, f
) < 0)
3313 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3314 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3315 fprintf(stderr
, "Error while reading ram block header\n");
3319 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3320 BDRV_HASH_BLOCK_SIZE
) < 0) {
3321 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3326 printf("Error block header\n");
3330 ram_decompress_close(s
);
3335 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3340 if (version_id
== 1)
3341 return ram_load_v1(f
, opaque
);
3343 if (version_id
== 2) {
3344 if (qemu_get_be32(f
) != last_ram_offset
)
3346 return ram_load_dead(f
, opaque
);
3349 if (version_id
!= 3)
3353 addr
= qemu_get_be64(f
);
3355 flags
= addr
& ~TARGET_PAGE_MASK
;
3356 addr
&= TARGET_PAGE_MASK
;
3358 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3359 if (addr
!= last_ram_offset
)
3363 if (flags
& RAM_SAVE_FLAG_FULL
) {
3364 if (ram_load_dead(f
, opaque
) < 0)
3368 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3369 uint8_t ch
= qemu_get_byte(f
);
3370 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3373 (!kvm_enabled() || kvm_has_sync_mmu())) {
3374 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3377 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3378 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3379 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3384 void qemu_service_io(void)
3386 qemu_notify_event();
3389 /***********************************************************/
3390 /* bottom halves (can be seen as timers which expire ASAP) */
3401 static QEMUBH
*first_bh
= NULL
;
3403 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3406 bh
= qemu_mallocz(sizeof(QEMUBH
));
3408 bh
->opaque
= opaque
;
3409 bh
->next
= first_bh
;
3414 int qemu_bh_poll(void)
3420 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3421 if (!bh
->deleted
&& bh
->scheduled
) {
3430 /* remove deleted bhs */
3444 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3452 void qemu_bh_schedule(QEMUBH
*bh
)
3458 /* stop the currently executing CPU to execute the BH ASAP */
3459 qemu_notify_event();
3462 void qemu_bh_cancel(QEMUBH
*bh
)
3467 void qemu_bh_delete(QEMUBH
*bh
)
3473 static void qemu_bh_update_timeout(int *timeout
)
3477 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3478 if (!bh
->deleted
&& bh
->scheduled
) {
3480 /* idle bottom halves will be polled at least
3482 *timeout
= MIN(10, *timeout
);
3484 /* non-idle bottom halves will be executed
3493 /***********************************************************/
3494 /* machine registration */
3496 static QEMUMachine
*first_machine
= NULL
;
3497 QEMUMachine
*current_machine
= NULL
;
3499 int qemu_register_machine(QEMUMachine
*m
)
3502 pm
= &first_machine
;
3510 static QEMUMachine
*find_machine(const char *name
)
3514 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3515 if (!strcmp(m
->name
, name
))
3521 static QEMUMachine
*find_default_machine(void)
3525 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3526 if (m
->is_default
) {
3533 /***********************************************************/
3534 /* main execution loop */
3536 static void gui_update(void *opaque
)
3538 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3539 DisplayState
*ds
= opaque
;
3540 DisplayChangeListener
*dcl
= ds
->listeners
;
3544 while (dcl
!= NULL
) {
3545 if (dcl
->gui_timer_interval
&&
3546 dcl
->gui_timer_interval
< interval
)
3547 interval
= dcl
->gui_timer_interval
;
3550 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3553 static void nographic_update(void *opaque
)
3555 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3557 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3560 struct vm_change_state_entry
{
3561 VMChangeStateHandler
*cb
;
3563 LIST_ENTRY (vm_change_state_entry
) entries
;
3566 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3568 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3571 VMChangeStateEntry
*e
;
3573 e
= qemu_mallocz(sizeof (*e
));
3577 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3581 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3583 LIST_REMOVE (e
, entries
);
3587 static void vm_state_notify(int running
, int reason
)
3589 VMChangeStateEntry
*e
;
3591 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3592 e
->cb(e
->opaque
, running
, reason
);
3596 static void resume_all_vcpus(void);
3597 static void pause_all_vcpus(void);
3604 vm_state_notify(1, 0);
3605 qemu_rearm_alarm_timer(alarm_timer
);
3610 /* reset/shutdown handler */
3612 typedef struct QEMUResetEntry
{
3613 QEMUResetHandler
*func
;
3616 struct QEMUResetEntry
*next
;
3619 static QEMUResetEntry
*first_reset_entry
;
3620 static int reset_requested
;
3621 static int shutdown_requested
;
3622 static int powerdown_requested
;
3623 static int debug_requested
;
3624 static int vmstop_requested
;
3626 int qemu_shutdown_requested(void)
3628 int r
= shutdown_requested
;
3629 shutdown_requested
= 0;
3633 int qemu_reset_requested(void)
3635 int r
= reset_requested
;
3636 reset_requested
= 0;
3640 int qemu_powerdown_requested(void)
3642 int r
= powerdown_requested
;
3643 powerdown_requested
= 0;
3647 static int qemu_debug_requested(void)
3649 int r
= debug_requested
;
3650 debug_requested
= 0;
3654 static int qemu_vmstop_requested(void)
3656 int r
= vmstop_requested
;
3657 vmstop_requested
= 0;
3661 static void do_vm_stop(int reason
)
3664 cpu_disable_ticks();
3667 vm_state_notify(0, reason
);
3671 void qemu_register_reset(QEMUResetHandler
*func
, int order
, void *opaque
)
3673 QEMUResetEntry
**pre
, *re
;
3675 pre
= &first_reset_entry
;
3676 while (*pre
!= NULL
&& (*pre
)->order
>= order
) {
3677 pre
= &(*pre
)->next
;
3679 re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3681 re
->opaque
= opaque
;
3687 void qemu_system_reset(void)
3691 /* reset all devices */
3692 for(re
= first_reset_entry
; re
!= NULL
; re
= re
->next
) {
3693 re
->func(re
->opaque
);
3697 void qemu_system_reset_request(void)
3700 shutdown_requested
= 1;
3702 reset_requested
= 1;
3704 qemu_notify_event();
3707 void qemu_system_shutdown_request(void)
3709 shutdown_requested
= 1;
3710 qemu_notify_event();
3713 void qemu_system_powerdown_request(void)
3715 powerdown_requested
= 1;
3716 qemu_notify_event();
3719 #ifdef CONFIG_IOTHREAD
3720 static void qemu_system_vmstop_request(int reason
)
3722 vmstop_requested
= reason
;
3723 qemu_notify_event();
3728 static int io_thread_fd
= -1;
3730 static void qemu_event_increment(void)
3732 static const char byte
= 0;
3734 if (io_thread_fd
== -1)
3737 write(io_thread_fd
, &byte
, sizeof(byte
));
3740 static void qemu_event_read(void *opaque
)
3742 int fd
= (unsigned long)opaque
;
3745 /* Drain the notify pipe */
3748 len
= read(fd
, buffer
, sizeof(buffer
));
3749 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3752 static int qemu_event_init(void)
3761 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3765 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3769 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3770 (void *)(unsigned long)fds
[0]);
3772 io_thread_fd
= fds
[1];
3781 HANDLE qemu_event_handle
;
3783 static void dummy_event_handler(void *opaque
)
3787 static int qemu_event_init(void)
3789 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3790 if (!qemu_event_handle
) {
3791 perror("Failed CreateEvent");
3794 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3798 static void qemu_event_increment(void)
3800 SetEvent(qemu_event_handle
);
3804 static int cpu_can_run(CPUState
*env
)
3813 #ifndef CONFIG_IOTHREAD
3814 static int qemu_init_main_loop(void)
3816 return qemu_event_init();
3819 void qemu_init_vcpu(void *_env
)
3821 CPUState
*env
= _env
;
3828 int qemu_cpu_self(void *env
)
3833 static void resume_all_vcpus(void)
3837 static void pause_all_vcpus(void)
3841 void qemu_cpu_kick(void *env
)
3846 void qemu_notify_event(void)
3848 CPUState
*env
= cpu_single_env
;
3853 if (env
->kqemu_enabled
)
3854 kqemu_cpu_interrupt(env
);
3859 #define qemu_mutex_lock_iothread() do { } while (0)
3860 #define qemu_mutex_unlock_iothread() do { } while (0)
3862 void vm_stop(int reason
)
3867 #else /* CONFIG_IOTHREAD */
3869 #include "qemu-thread.h"
3871 QemuMutex qemu_global_mutex
;
3872 static QemuMutex qemu_fair_mutex
;
3874 static QemuThread io_thread
;
3876 static QemuThread
*tcg_cpu_thread
;
3877 static QemuCond
*tcg_halt_cond
;
3879 static int qemu_system_ready
;
3881 static QemuCond qemu_cpu_cond
;
3883 static QemuCond qemu_system_cond
;
3884 static QemuCond qemu_pause_cond
;
3886 static void block_io_signals(void);
3887 static void unblock_io_signals(void);
3888 static int tcg_has_work(void);
3890 static int qemu_init_main_loop(void)
3894 ret
= qemu_event_init();
3898 qemu_cond_init(&qemu_pause_cond
);
3899 qemu_mutex_init(&qemu_fair_mutex
);
3900 qemu_mutex_init(&qemu_global_mutex
);
3901 qemu_mutex_lock(&qemu_global_mutex
);
3903 unblock_io_signals();
3904 qemu_thread_self(&io_thread
);
3909 static void qemu_wait_io_event(CPUState
*env
)
3911 while (!tcg_has_work())
3912 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3914 qemu_mutex_unlock(&qemu_global_mutex
);
3917 * Users of qemu_global_mutex can be starved, having no chance
3918 * to acquire it since this path will get to it first.
3919 * So use another lock to provide fairness.
3921 qemu_mutex_lock(&qemu_fair_mutex
);
3922 qemu_mutex_unlock(&qemu_fair_mutex
);
3924 qemu_mutex_lock(&qemu_global_mutex
);
3928 qemu_cond_signal(&qemu_pause_cond
);
3932 static int qemu_cpu_exec(CPUState
*env
);
3934 static void *kvm_cpu_thread_fn(void *arg
)
3936 CPUState
*env
= arg
;
3939 qemu_thread_self(env
->thread
);
3941 /* signal CPU creation */
3942 qemu_mutex_lock(&qemu_global_mutex
);
3944 qemu_cond_signal(&qemu_cpu_cond
);
3946 /* and wait for machine initialization */
3947 while (!qemu_system_ready
)
3948 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3951 if (cpu_can_run(env
))
3953 qemu_wait_io_event(env
);
3959 static void tcg_cpu_exec(void);
3961 static void *tcg_cpu_thread_fn(void *arg
)
3963 CPUState
*env
= arg
;
3966 qemu_thread_self(env
->thread
);
3968 /* signal CPU creation */
3969 qemu_mutex_lock(&qemu_global_mutex
);
3970 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3972 qemu_cond_signal(&qemu_cpu_cond
);
3974 /* and wait for machine initialization */
3975 while (!qemu_system_ready
)
3976 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3980 qemu_wait_io_event(cur_cpu
);
3986 void qemu_cpu_kick(void *_env
)
3988 CPUState
*env
= _env
;
3989 qemu_cond_broadcast(env
->halt_cond
);
3991 qemu_thread_signal(env
->thread
, SIGUSR1
);
3994 int qemu_cpu_self(void *env
)
3996 return (cpu_single_env
!= NULL
);
3999 static void cpu_signal(int sig
)
4002 cpu_exit(cpu_single_env
);
4005 static void block_io_signals(void)
4008 struct sigaction sigact
;
4011 sigaddset(&set
, SIGUSR2
);
4012 sigaddset(&set
, SIGIO
);
4013 sigaddset(&set
, SIGALRM
);
4014 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
4017 sigaddset(&set
, SIGUSR1
);
4018 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
4020 memset(&sigact
, 0, sizeof(sigact
));
4021 sigact
.sa_handler
= cpu_signal
;
4022 sigaction(SIGUSR1
, &sigact
, NULL
);
4025 static void unblock_io_signals(void)
4030 sigaddset(&set
, SIGUSR2
);
4031 sigaddset(&set
, SIGIO
);
4032 sigaddset(&set
, SIGALRM
);
4033 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
4036 sigaddset(&set
, SIGUSR1
);
4037 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
4040 static void qemu_signal_lock(unsigned int msecs
)
4042 qemu_mutex_lock(&qemu_fair_mutex
);
4044 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
4045 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
4046 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
4049 qemu_mutex_unlock(&qemu_fair_mutex
);
4052 static void qemu_mutex_lock_iothread(void)
4054 if (kvm_enabled()) {
4055 qemu_mutex_lock(&qemu_fair_mutex
);
4056 qemu_mutex_lock(&qemu_global_mutex
);
4057 qemu_mutex_unlock(&qemu_fair_mutex
);
4059 qemu_signal_lock(100);
4062 static void qemu_mutex_unlock_iothread(void)
4064 qemu_mutex_unlock(&qemu_global_mutex
);
4067 static int all_vcpus_paused(void)
4069 CPUState
*penv
= first_cpu
;
4074 penv
= (CPUState
*)penv
->next_cpu
;
4080 static void pause_all_vcpus(void)
4082 CPUState
*penv
= first_cpu
;
4086 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4087 qemu_cpu_kick(penv
);
4088 penv
= (CPUState
*)penv
->next_cpu
;
4091 while (!all_vcpus_paused()) {
4092 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
4095 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4096 penv
= (CPUState
*)penv
->next_cpu
;
4101 static void resume_all_vcpus(void)
4103 CPUState
*penv
= first_cpu
;
4108 qemu_thread_signal(penv
->thread
, SIGUSR1
);
4109 qemu_cpu_kick(penv
);
4110 penv
= (CPUState
*)penv
->next_cpu
;
4114 static void tcg_init_vcpu(void *_env
)
4116 CPUState
*env
= _env
;
4117 /* share a single thread for all cpus with TCG */
4118 if (!tcg_cpu_thread
) {
4119 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
4120 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
4121 qemu_cond_init(env
->halt_cond
);
4122 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
4123 while (env
->created
== 0)
4124 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
4125 tcg_cpu_thread
= env
->thread
;
4126 tcg_halt_cond
= env
->halt_cond
;
4128 env
->thread
= tcg_cpu_thread
;
4129 env
->halt_cond
= tcg_halt_cond
;
4133 static void kvm_start_vcpu(CPUState
*env
)
4136 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
4137 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
4138 qemu_cond_init(env
->halt_cond
);
4139 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
4140 while (env
->created
== 0)
4141 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
4144 void qemu_init_vcpu(void *_env
)
4146 CPUState
*env
= _env
;
4149 kvm_start_vcpu(env
);
4154 void qemu_notify_event(void)
4156 qemu_event_increment();
4159 void vm_stop(int reason
)
4162 qemu_thread_self(&me
);
4164 if (!qemu_thread_equal(&me
, &io_thread
)) {
4165 qemu_system_vmstop_request(reason
);
4167 * FIXME: should not return to device code in case
4168 * vm_stop() has been requested.
4170 if (cpu_single_env
) {
4171 cpu_exit(cpu_single_env
);
4172 cpu_single_env
->stop
= 1;
4183 static void host_main_loop_wait(int *timeout
)
4189 /* XXX: need to suppress polling by better using win32 events */
4191 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4192 ret
|= pe
->func(pe
->opaque
);
4196 WaitObjects
*w
= &wait_objects
;
4198 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4199 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4200 if (w
->func
[ret
- WAIT_OBJECT_0
])
4201 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4203 /* Check for additional signaled events */
4204 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4206 /* Check if event is signaled */
4207 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4208 if(ret2
== WAIT_OBJECT_0
) {
4210 w
->func
[i
](w
->opaque
[i
]);
4211 } else if (ret2
== WAIT_TIMEOUT
) {
4213 err
= GetLastError();
4214 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4217 } else if (ret
== WAIT_TIMEOUT
) {
4219 err
= GetLastError();
4220 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4227 static void host_main_loop_wait(int *timeout
)
4232 void main_loop_wait(int timeout
)
4234 IOHandlerRecord
*ioh
;
4235 fd_set rfds
, wfds
, xfds
;
4239 qemu_bh_update_timeout(&timeout
);
4241 host_main_loop_wait(&timeout
);
4243 /* poll any events */
4244 /* XXX: separate device handlers from system ones */
4249 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4253 (!ioh
->fd_read_poll
||
4254 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4255 FD_SET(ioh
->fd
, &rfds
);
4259 if (ioh
->fd_write
) {
4260 FD_SET(ioh
->fd
, &wfds
);
4266 tv
.tv_sec
= timeout
/ 1000;
4267 tv
.tv_usec
= (timeout
% 1000) * 1000;
4269 #if defined(CONFIG_SLIRP)
4270 if (slirp_is_inited()) {
4271 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4274 qemu_mutex_unlock_iothread();
4275 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4276 qemu_mutex_lock_iothread();
4278 IOHandlerRecord
**pioh
;
4280 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4281 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4282 ioh
->fd_read(ioh
->opaque
);
4284 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4285 ioh
->fd_write(ioh
->opaque
);
4289 /* remove deleted IO handlers */
4290 pioh
= &first_io_handler
;
4300 #if defined(CONFIG_SLIRP)
4301 if (slirp_is_inited()) {
4307 slirp_select_poll(&rfds
, &wfds
, &xfds
);
4311 /* rearm timer, if not periodic */
4312 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4313 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4314 qemu_rearm_alarm_timer(alarm_timer
);
4317 /* vm time timers */
4319 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4320 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4321 qemu_get_clock(vm_clock
));
4324 /* real time timers */
4325 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4326 qemu_get_clock(rt_clock
));
4328 /* Check bottom-halves last in case any of the earlier events triggered
4334 static int qemu_cpu_exec(CPUState
*env
)
4337 #ifdef CONFIG_PROFILER
4341 #ifdef CONFIG_PROFILER
4342 ti
= profile_getclock();
4347 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4348 env
->icount_decr
.u16
.low
= 0;
4349 env
->icount_extra
= 0;
4350 count
= qemu_next_deadline();
4351 count
= (count
+ (1 << icount_time_shift
) - 1)
4352 >> icount_time_shift
;
4353 qemu_icount
+= count
;
4354 decr
= (count
> 0xffff) ? 0xffff : count
;
4356 env
->icount_decr
.u16
.low
= decr
;
4357 env
->icount_extra
= count
;
4359 ret
= cpu_exec(env
);
4360 #ifdef CONFIG_PROFILER
4361 qemu_time
+= profile_getclock() - ti
;
4364 /* Fold pending instructions back into the
4365 instruction counter, and clear the interrupt flag. */
4366 qemu_icount
-= (env
->icount_decr
.u16
.low
4367 + env
->icount_extra
);
4368 env
->icount_decr
.u32
= 0;
4369 env
->icount_extra
= 0;
4374 static void tcg_cpu_exec(void)
4378 if (next_cpu
== NULL
)
4379 next_cpu
= first_cpu
;
4380 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4381 CPUState
*env
= cur_cpu
= next_cpu
;
4385 if (timer_alarm_pending
) {
4386 timer_alarm_pending
= 0;
4389 if (cpu_can_run(env
))
4390 ret
= qemu_cpu_exec(env
);
4391 if (ret
== EXCP_DEBUG
) {
4392 gdb_set_stop_cpu(env
);
4393 debug_requested
= 1;
4399 static int cpu_has_work(CPUState
*env
)
4407 if (qemu_cpu_has_work(env
))
4412 static int tcg_has_work(void)
4416 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4417 if (cpu_has_work(env
))
4422 static int qemu_calculate_timeout(void)
4424 #ifndef CONFIG_IOTHREAD
4429 else if (tcg_has_work())
4431 else if (!use_icount
)
4434 /* XXX: use timeout computed from timers */
4437 /* Advance virtual time to the next event. */
4438 if (use_icount
== 1) {
4439 /* When not using an adaptive execution frequency
4440 we tend to get badly out of sync with real time,
4441 so just delay for a reasonable amount of time. */
4444 delta
= cpu_get_icount() - cpu_get_clock();
4447 /* If virtual time is ahead of real time then just
4449 timeout
= (delta
/ 1000000) + 1;
4451 /* Wait for either IO to occur or the next
4453 add
= qemu_next_deadline();
4454 /* We advance the timer before checking for IO.
4455 Limit the amount we advance so that early IO
4456 activity won't get the guest too far ahead. */
4460 add
= (add
+ (1 << icount_time_shift
) - 1)
4461 >> icount_time_shift
;
4463 timeout
= delta
/ 1000000;
4470 #else /* CONFIG_IOTHREAD */
4475 static int vm_can_run(void)
4477 if (powerdown_requested
)
4479 if (reset_requested
)
4481 if (shutdown_requested
)
4483 if (debug_requested
)
4488 static void main_loop(void)
4492 #ifdef CONFIG_IOTHREAD
4493 qemu_system_ready
= 1;
4494 qemu_cond_broadcast(&qemu_system_cond
);
4499 #ifdef CONFIG_PROFILER
4502 #ifndef CONFIG_IOTHREAD
4505 #ifdef CONFIG_PROFILER
4506 ti
= profile_getclock();
4508 main_loop_wait(qemu_calculate_timeout());
4509 #ifdef CONFIG_PROFILER
4510 dev_time
+= profile_getclock() - ti
;
4512 } while (vm_can_run());
4514 if (qemu_debug_requested())
4515 vm_stop(EXCP_DEBUG
);
4516 if (qemu_shutdown_requested()) {
4523 if (qemu_reset_requested()) {
4525 qemu_system_reset();
4528 if (qemu_powerdown_requested())
4529 qemu_system_powerdown();
4530 if ((r
= qemu_vmstop_requested()))
4536 static void version(void)
4538 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4541 static void help(int exitcode
)
4544 printf("usage: %s [options] [disk_image]\n"
4546 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4548 #define DEF(option, opt_arg, opt_enum, opt_help) \
4550 #define DEFHEADING(text) stringify(text) "\n"
4551 #include "qemu-options.h"
4556 "During emulation, the following keys are useful:\n"
4557 "ctrl-alt-f toggle full screen\n"
4558 "ctrl-alt-n switch to virtual console 'n'\n"
4559 "ctrl-alt toggle mouse and keyboard grab\n"
4561 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4566 DEFAULT_NETWORK_SCRIPT
,
4567 DEFAULT_NETWORK_DOWN_SCRIPT
,
4569 DEFAULT_GDBSTUB_PORT
,
4574 #define HAS_ARG 0x0001
4577 #define DEF(option, opt_arg, opt_enum, opt_help) \
4579 #define DEFHEADING(text)
4580 #include "qemu-options.h"
4586 typedef struct QEMUOption
{
4592 static const QEMUOption qemu_options
[] = {
4593 { "h", 0, QEMU_OPTION_h
},
4594 #define DEF(option, opt_arg, opt_enum, opt_help) \
4595 { option, opt_arg, opt_enum },
4596 #define DEFHEADING(text)
4597 #include "qemu-options.h"
4605 struct soundhw soundhw
[] = {
4606 #ifdef HAS_AUDIO_CHOICE
4607 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4613 { .init_isa
= pcspk_audio_init
}
4620 "Creative Sound Blaster 16",
4623 { .init_isa
= SB16_init
}
4627 #ifdef CONFIG_CS4231A
4633 { .init_isa
= cs4231a_init
}
4641 "Yamaha YMF262 (OPL3)",
4643 "Yamaha YM3812 (OPL2)",
4647 { .init_isa
= Adlib_init
}
4654 "Gravis Ultrasound GF1",
4657 { .init_isa
= GUS_init
}
4664 "Intel 82801AA AC97 Audio",
4667 { .init_pci
= ac97_init
}
4671 #ifdef CONFIG_ES1370
4674 "ENSONIQ AudioPCI ES1370",
4677 { .init_pci
= es1370_init
}
4681 #endif /* HAS_AUDIO_CHOICE */
4683 { NULL
, NULL
, 0, 0, { NULL
} }
4686 static void select_soundhw (const char *optarg
)
4690 if (*optarg
== '?') {
4693 printf ("Valid sound card names (comma separated):\n");
4694 for (c
= soundhw
; c
->name
; ++c
) {
4695 printf ("%-11s %s\n", c
->name
, c
->descr
);
4697 printf ("\n-soundhw all will enable all of the above\n");
4698 exit (*optarg
!= '?');
4706 if (!strcmp (optarg
, "all")) {
4707 for (c
= soundhw
; c
->name
; ++c
) {
4715 e
= strchr (p
, ',');
4716 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4718 for (c
= soundhw
; c
->name
; ++c
) {
4719 if (!strncmp (c
->name
, p
, l
)) {
4728 "Unknown sound card name (too big to show)\n");
4731 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4736 p
+= l
+ (e
!= NULL
);
4740 goto show_valid_cards
;
4745 static void select_vgahw (const char *p
)
4749 cirrus_vga_enabled
= 0;
4750 std_vga_enabled
= 0;
4753 if (strstart(p
, "std", &opts
)) {
4754 std_vga_enabled
= 1;
4755 } else if (strstart(p
, "cirrus", &opts
)) {
4756 cirrus_vga_enabled
= 1;
4757 } else if (strstart(p
, "vmware", &opts
)) {
4759 } else if (strstart(p
, "xenfb", &opts
)) {
4761 } else if (!strstart(p
, "none", &opts
)) {
4763 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4767 const char *nextopt
;
4769 if (strstart(opts
, ",retrace=", &nextopt
)) {
4771 if (strstart(opts
, "dumb", &nextopt
))
4772 vga_retrace_method
= VGA_RETRACE_DUMB
;
4773 else if (strstart(opts
, "precise", &nextopt
))
4774 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4775 else goto invalid_vga
;
4776 } else goto invalid_vga
;
4782 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4784 exit(STATUS_CONTROL_C_EXIT
);
4789 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4793 if(strlen(str
) != 36)
4796 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4797 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4798 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4804 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4810 #define MAX_NET_CLIENTS 32
4814 static void termsig_handler(int signal
)
4816 qemu_system_shutdown_request();
4819 static void sigchld_handler(int signal
)
4821 waitpid(-1, NULL
, WNOHANG
);
4824 static void sighandler_setup(void)
4826 struct sigaction act
;
4828 memset(&act
, 0, sizeof(act
));
4829 act
.sa_handler
= termsig_handler
;
4830 sigaction(SIGINT
, &act
, NULL
);
4831 sigaction(SIGHUP
, &act
, NULL
);
4832 sigaction(SIGTERM
, &act
, NULL
);
4834 act
.sa_handler
= sigchld_handler
;
4835 act
.sa_flags
= SA_NOCLDSTOP
;
4836 sigaction(SIGCHLD
, &act
, NULL
);
4842 /* Look for support files in the same directory as the executable. */
4843 static char *find_datadir(const char *argv0
)
4849 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4856 while (p
!= buf
&& *p
!= '\\')
4859 if (access(buf
, R_OK
) == 0) {
4860 return qemu_strdup(buf
);
4866 /* Find a likely location for support files using the location of the binary.
4867 For installed binaries this will be "$bindir/../share/qemu". When
4868 running from the build tree this will be "$bindir/../pc-bios". */
4869 #define SHARE_SUFFIX "/share/qemu"
4870 #define BUILD_SUFFIX "/pc-bios"
4871 static char *find_datadir(const char *argv0
)
4881 #if defined(__linux__)
4884 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4890 #elif defined(__FreeBSD__)
4893 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4900 /* If we don't have any way of figuring out the actual executable
4901 location then try argv[0]. */
4906 p
= realpath(argv0
, p
);
4914 max_len
= strlen(dir
) +
4915 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4916 res
= qemu_mallocz(max_len
);
4917 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4918 if (access(res
, R_OK
)) {
4919 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4920 if (access(res
, R_OK
)) {
4934 char *qemu_find_file(int type
, const char *name
)
4940 /* If name contains path separators then try it as a straight path. */
4941 if ((strchr(name
, '/') || strchr(name
, '\\'))
4942 && access(name
, R_OK
) == 0) {
4943 return strdup(name
);
4946 case QEMU_FILE_TYPE_BIOS
:
4949 case QEMU_FILE_TYPE_KEYMAP
:
4950 subdir
= "keymaps/";
4955 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4956 buf
= qemu_mallocz(len
);
4957 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4958 if (access(buf
, R_OK
)) {
4965 int main(int argc
, char **argv
, char **envp
)
4967 const char *gdbstub_dev
= NULL
;
4968 uint32_t boot_devices_bitmap
= 0;
4970 int snapshot
, linux_boot
, net_boot
;
4971 const char *initrd_filename
;
4972 const char *kernel_filename
, *kernel_cmdline
;
4973 const char *boot_devices
= "";
4975 DisplayChangeListener
*dcl
;
4976 int cyls
, heads
, secs
, translation
;
4977 const char *net_clients
[MAX_NET_CLIENTS
];
4979 const char *bt_opts
[MAX_BT_CMDLINE
];
4983 const char *r
, *optarg
;
4984 CharDriverState
*monitor_hd
= NULL
;
4985 const char *monitor_device
;
4986 const char *serial_devices
[MAX_SERIAL_PORTS
];
4987 int serial_device_index
;
4988 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4989 int parallel_device_index
;
4990 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4991 int virtio_console_index
;
4992 const char *loadvm
= NULL
;
4993 QEMUMachine
*machine
;
4994 const char *cpu_model
;
4995 const char *usb_devices
[MAX_USB_CMDLINE
];
4996 int usb_devices_index
;
5001 const char *pid_file
= NULL
;
5002 const char *incoming
= NULL
;
5005 struct passwd
*pwd
= NULL
;
5006 const char *chroot_dir
= NULL
;
5007 const char *run_as
= NULL
;
5010 int show_vnc_port
= 0;
5012 qemu_cache_utils_init(envp
);
5014 LIST_INIT (&vm_change_state_head
);
5017 struct sigaction act
;
5018 sigfillset(&act
.sa_mask
);
5020 act
.sa_handler
= SIG_IGN
;
5021 sigaction(SIGPIPE
, &act
, NULL
);
5024 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
5025 /* Note: cpu_interrupt() is currently not SMP safe, so we force
5026 QEMU to run on a single CPU */
5031 h
= GetCurrentProcess();
5032 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
5033 for(i
= 0; i
< 32; i
++) {
5034 if (mask
& (1 << i
))
5039 SetProcessAffinityMask(h
, mask
);
5045 module_call_init(MODULE_INIT_MACHINE
);
5046 machine
= find_default_machine();
5048 initrd_filename
= NULL
;
5051 kernel_filename
= NULL
;
5052 kernel_cmdline
= "";
5053 cyls
= heads
= secs
= 0;
5054 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5055 monitor_device
= "vc:80Cx24C";
5057 serial_devices
[0] = "vc:80Cx24C";
5058 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
5059 serial_devices
[i
] = NULL
;
5060 serial_device_index
= 0;
5062 parallel_devices
[0] = "vc:80Cx24C";
5063 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
5064 parallel_devices
[i
] = NULL
;
5065 parallel_device_index
= 0;
5067 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
5068 virtio_consoles
[i
] = NULL
;
5069 virtio_console_index
= 0;
5071 for (i
= 0; i
< MAX_NODES
; i
++) {
5073 node_cpumask
[i
] = 0;
5076 usb_devices_index
= 0;
5090 register_watchdogs();
5098 hda_index
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
5100 const QEMUOption
*popt
;
5103 /* Treat --foo the same as -foo. */
5106 popt
= qemu_options
;
5109 fprintf(stderr
, "%s: invalid option -- '%s'\n",
5113 if (!strcmp(popt
->name
, r
+ 1))
5117 if (popt
->flags
& HAS_ARG
) {
5118 if (optind
>= argc
) {
5119 fprintf(stderr
, "%s: option '%s' requires an argument\n",
5123 optarg
= argv
[optind
++];
5128 switch(popt
->index
) {
5130 machine
= find_machine(optarg
);
5133 printf("Supported machines are:\n");
5134 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
5135 printf("%-10s %s%s\n",
5137 m
->is_default
? " (default)" : "");
5139 exit(*optarg
!= '?');
5142 case QEMU_OPTION_cpu
:
5143 /* hw initialization will check this */
5144 if (*optarg
== '?') {
5145 /* XXX: implement xxx_cpu_list for targets that still miss it */
5146 #if defined(cpu_list)
5147 cpu_list(stdout
, &fprintf
);
5154 case QEMU_OPTION_initrd
:
5155 initrd_filename
= optarg
;
5157 case QEMU_OPTION_hda
:
5159 hda_index
= drive_add(optarg
, HD_ALIAS
, 0);
5161 hda_index
= drive_add(optarg
, HD_ALIAS
5162 ",cyls=%d,heads=%d,secs=%d%s",
5163 0, cyls
, heads
, secs
,
5164 translation
== BIOS_ATA_TRANSLATION_LBA
?
5166 translation
== BIOS_ATA_TRANSLATION_NONE
?
5167 ",trans=none" : "");
5169 case QEMU_OPTION_hdb
:
5170 case QEMU_OPTION_hdc
:
5171 case QEMU_OPTION_hdd
:
5172 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5174 case QEMU_OPTION_drive
:
5175 drive_add(NULL
, "%s", optarg
);
5177 case QEMU_OPTION_mtdblock
:
5178 drive_add(optarg
, MTD_ALIAS
);
5180 case QEMU_OPTION_sd
:
5181 drive_add(optarg
, SD_ALIAS
);
5183 case QEMU_OPTION_pflash
:
5184 drive_add(optarg
, PFLASH_ALIAS
);
5186 case QEMU_OPTION_snapshot
:
5189 case QEMU_OPTION_hdachs
:
5193 cyls
= strtol(p
, (char **)&p
, 0);
5194 if (cyls
< 1 || cyls
> 16383)
5199 heads
= strtol(p
, (char **)&p
, 0);
5200 if (heads
< 1 || heads
> 16)
5205 secs
= strtol(p
, (char **)&p
, 0);
5206 if (secs
< 1 || secs
> 63)
5210 if (!strcmp(p
, "none"))
5211 translation
= BIOS_ATA_TRANSLATION_NONE
;
5212 else if (!strcmp(p
, "lba"))
5213 translation
= BIOS_ATA_TRANSLATION_LBA
;
5214 else if (!strcmp(p
, "auto"))
5215 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5218 } else if (*p
!= '\0') {
5220 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5223 if (hda_index
!= -1)
5224 snprintf(drives_opt
[hda_index
].opt
,
5225 sizeof(drives_opt
[hda_index
].opt
),
5226 HD_ALIAS
",cyls=%d,heads=%d,secs=%d%s",
5227 0, cyls
, heads
, secs
,
5228 translation
== BIOS_ATA_TRANSLATION_LBA
?
5230 translation
== BIOS_ATA_TRANSLATION_NONE
?
5231 ",trans=none" : "");
5234 case QEMU_OPTION_numa
:
5235 if (nb_numa_nodes
>= MAX_NODES
) {
5236 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5241 case QEMU_OPTION_nographic
:
5242 display_type
= DT_NOGRAPHIC
;
5244 #ifdef CONFIG_CURSES
5245 case QEMU_OPTION_curses
:
5246 display_type
= DT_CURSES
;
5249 case QEMU_OPTION_portrait
:
5252 case QEMU_OPTION_kernel
:
5253 kernel_filename
= optarg
;
5255 case QEMU_OPTION_append
:
5256 kernel_cmdline
= optarg
;
5258 case QEMU_OPTION_cdrom
:
5259 drive_add(optarg
, CDROM_ALIAS
);
5261 case QEMU_OPTION_boot
:
5262 boot_devices
= optarg
;
5263 /* We just do some generic consistency checks */
5265 /* Could easily be extended to 64 devices if needed */
5268 boot_devices_bitmap
= 0;
5269 for (p
= boot_devices
; *p
!= '\0'; p
++) {
5270 /* Allowed boot devices are:
5271 * a b : floppy disk drives
5272 * c ... f : IDE disk drives
5273 * g ... m : machine implementation dependant drives
5274 * n ... p : network devices
5275 * It's up to each machine implementation to check
5276 * if the given boot devices match the actual hardware
5277 * implementation and firmware features.
5279 if (*p
< 'a' || *p
> 'q') {
5280 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
5283 if (boot_devices_bitmap
& (1 << (*p
- 'a'))) {
5285 "Boot device '%c' was given twice\n",*p
);
5288 boot_devices_bitmap
|= 1 << (*p
- 'a');
5292 case QEMU_OPTION_fda
:
5293 case QEMU_OPTION_fdb
:
5294 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5297 case QEMU_OPTION_no_fd_bootchk
:
5301 case QEMU_OPTION_net
:
5302 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5303 fprintf(stderr
, "qemu: too many network clients\n");
5306 net_clients
[nb_net_clients
] = optarg
;
5310 case QEMU_OPTION_tftp
:
5311 legacy_tftp_prefix
= optarg
;
5313 case QEMU_OPTION_bootp
:
5314 legacy_bootp_filename
= optarg
;
5317 case QEMU_OPTION_smb
:
5318 net_slirp_smb(optarg
);
5321 case QEMU_OPTION_redir
:
5322 net_slirp_redir(optarg
);
5325 case QEMU_OPTION_bt
:
5326 if (nb_bt_opts
>= MAX_BT_CMDLINE
) {
5327 fprintf(stderr
, "qemu: too many bluetooth options\n");
5330 bt_opts
[nb_bt_opts
++] = optarg
;
5333 case QEMU_OPTION_audio_help
:
5337 case QEMU_OPTION_soundhw
:
5338 select_soundhw (optarg
);
5344 case QEMU_OPTION_version
:
5348 case QEMU_OPTION_m
: {
5352 value
= strtoul(optarg
, &ptr
, 10);
5354 case 0: case 'M': case 'm':
5361 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5365 /* On 32-bit hosts, QEMU is limited by virtual address space */
5366 if (value
> (2047 << 20)
5367 #ifndef CONFIG_KQEMU
5368 && HOST_LONG_BITS
== 32
5371 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5374 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5375 fprintf(stderr
, "qemu: ram size too large\n");
5384 const CPULogItem
*item
;
5386 mask
= cpu_str_to_log_mask(optarg
);
5388 printf("Log items (comma separated):\n");
5389 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5390 printf("%-10s %s\n", item
->name
, item
->help
);
5398 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5400 case QEMU_OPTION_gdb
:
5401 gdbstub_dev
= optarg
;
5406 case QEMU_OPTION_bios
:
5409 case QEMU_OPTION_singlestep
:
5417 keyboard_layout
= optarg
;
5420 case QEMU_OPTION_localtime
:
5423 case QEMU_OPTION_vga
:
5424 select_vgahw (optarg
);
5426 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5432 w
= strtol(p
, (char **)&p
, 10);
5435 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5441 h
= strtol(p
, (char **)&p
, 10);
5446 depth
= strtol(p
, (char **)&p
, 10);
5447 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5448 depth
!= 24 && depth
!= 32)
5450 } else if (*p
== '\0') {
5451 depth
= graphic_depth
;
5458 graphic_depth
= depth
;
5462 case QEMU_OPTION_echr
:
5465 term_escape_char
= strtol(optarg
, &r
, 0);
5467 printf("Bad argument to echr\n");
5470 case QEMU_OPTION_monitor
:
5471 monitor_device
= optarg
;
5473 case QEMU_OPTION_serial
:
5474 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5475 fprintf(stderr
, "qemu: too many serial ports\n");
5478 serial_devices
[serial_device_index
] = optarg
;
5479 serial_device_index
++;
5481 case QEMU_OPTION_watchdog
:
5482 i
= select_watchdog(optarg
);
5484 exit (i
== 1 ? 1 : 0);
5486 case QEMU_OPTION_watchdog_action
:
5487 if (select_watchdog_action(optarg
) == -1) {
5488 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5492 case QEMU_OPTION_virtiocon
:
5493 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5494 fprintf(stderr
, "qemu: too many virtio consoles\n");
5497 virtio_consoles
[virtio_console_index
] = optarg
;
5498 virtio_console_index
++;
5500 case QEMU_OPTION_parallel
:
5501 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5502 fprintf(stderr
, "qemu: too many parallel ports\n");
5505 parallel_devices
[parallel_device_index
] = optarg
;
5506 parallel_device_index
++;
5508 case QEMU_OPTION_loadvm
:
5511 case QEMU_OPTION_full_screen
:
5515 case QEMU_OPTION_no_frame
:
5518 case QEMU_OPTION_alt_grab
:
5521 case QEMU_OPTION_no_quit
:
5524 case QEMU_OPTION_sdl
:
5525 display_type
= DT_SDL
;
5528 case QEMU_OPTION_pidfile
:
5532 case QEMU_OPTION_win2k_hack
:
5533 win2k_install_hack
= 1;
5535 case QEMU_OPTION_rtc_td_hack
:
5538 case QEMU_OPTION_acpitable
:
5539 if(acpi_table_add(optarg
) < 0) {
5540 fprintf(stderr
, "Wrong acpi table provided\n");
5544 case QEMU_OPTION_smbios
:
5545 if(smbios_entry_add(optarg
) < 0) {
5546 fprintf(stderr
, "Wrong smbios provided\n");
5552 case QEMU_OPTION_no_kqemu
:
5555 case QEMU_OPTION_kernel_kqemu
:
5560 case QEMU_OPTION_enable_kvm
:
5567 case QEMU_OPTION_usb
:
5570 case QEMU_OPTION_usbdevice
:
5572 if (usb_devices_index
>= MAX_USB_CMDLINE
) {
5573 fprintf(stderr
, "Too many USB devices\n");
5576 usb_devices
[usb_devices_index
] = optarg
;
5577 usb_devices_index
++;
5579 case QEMU_OPTION_smp
:
5580 smp_cpus
= atoi(optarg
);
5582 fprintf(stderr
, "Invalid number of CPUs\n");
5586 case QEMU_OPTION_vnc
:
5587 display_type
= DT_VNC
;
5588 vnc_display
= optarg
;
5591 case QEMU_OPTION_no_acpi
:
5594 case QEMU_OPTION_no_hpet
:
5597 case QEMU_OPTION_no_virtio_balloon
:
5598 no_virtio_balloon
= 1;
5601 case QEMU_OPTION_no_reboot
:
5604 case QEMU_OPTION_no_shutdown
:
5607 case QEMU_OPTION_show_cursor
:
5610 case QEMU_OPTION_uuid
:
5611 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5612 fprintf(stderr
, "Fail to parse UUID string."
5613 " Wrong format.\n");
5618 case QEMU_OPTION_daemonize
:
5622 case QEMU_OPTION_option_rom
:
5623 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5624 fprintf(stderr
, "Too many option ROMs\n");
5627 option_rom
[nb_option_roms
] = optarg
;
5630 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5631 case QEMU_OPTION_semihosting
:
5632 semihosting_enabled
= 1;
5635 case QEMU_OPTION_name
:
5638 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5639 case QEMU_OPTION_prom_env
:
5640 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5641 fprintf(stderr
, "Too many prom variables\n");
5644 prom_envs
[nb_prom_envs
] = optarg
;
5649 case QEMU_OPTION_old_param
:
5653 case QEMU_OPTION_clock
:
5654 configure_alarms(optarg
);
5656 case QEMU_OPTION_startdate
:
5659 time_t rtc_start_date
;
5660 if (!strcmp(optarg
, "now")) {
5661 rtc_date_offset
= -1;
5663 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5671 } else if (sscanf(optarg
, "%d-%d-%d",
5674 &tm
.tm_mday
) == 3) {
5683 rtc_start_date
= mktimegm(&tm
);
5684 if (rtc_start_date
== -1) {
5686 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5687 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5690 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5694 case QEMU_OPTION_tb_size
:
5695 tb_size
= strtol(optarg
, NULL
, 0);
5699 case QEMU_OPTION_icount
:
5701 if (strcmp(optarg
, "auto") == 0) {
5702 icount_time_shift
= -1;
5704 icount_time_shift
= strtol(optarg
, NULL
, 0);
5707 case QEMU_OPTION_incoming
:
5711 case QEMU_OPTION_chroot
:
5712 chroot_dir
= optarg
;
5714 case QEMU_OPTION_runas
:
5719 case QEMU_OPTION_xen_domid
:
5720 xen_domid
= atoi(optarg
);
5722 case QEMU_OPTION_xen_create
:
5723 xen_mode
= XEN_CREATE
;
5725 case QEMU_OPTION_xen_attach
:
5726 xen_mode
= XEN_ATTACH
;
5733 /* If no data_dir is specified then try to find it relative to the
5736 data_dir
= find_datadir(argv
[0]);
5738 /* If all else fails use the install patch specified when building. */
5740 data_dir
= CONFIG_QEMU_SHAREDIR
;
5743 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5744 if (kvm_allowed
&& kqemu_allowed
) {
5746 "You can not enable both KVM and kqemu at the same time\n");
5751 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5752 if (smp_cpus
> machine
->max_cpus
) {
5753 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5754 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5759 if (display_type
== DT_NOGRAPHIC
) {
5760 if (serial_device_index
== 0)
5761 serial_devices
[0] = "stdio";
5762 if (parallel_device_index
== 0)
5763 parallel_devices
[0] = "null";
5764 if (strncmp(monitor_device
, "vc", 2) == 0)
5765 monitor_device
= "stdio";
5772 if (pipe(fds
) == -1)
5783 len
= read(fds
[0], &status
, 1);
5784 if (len
== -1 && (errno
== EINTR
))
5789 else if (status
== 1) {
5790 fprintf(stderr
, "Could not acquire pidfile\n");
5807 signal(SIGTSTP
, SIG_IGN
);
5808 signal(SIGTTOU
, SIG_IGN
);
5809 signal(SIGTTIN
, SIG_IGN
);
5812 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5815 write(fds
[1], &status
, 1);
5817 fprintf(stderr
, "Could not acquire pid file\n");
5826 if (qemu_init_main_loop()) {
5827 fprintf(stderr
, "qemu_init_main_loop failed\n");
5830 linux_boot
= (kernel_filename
!= NULL
);
5832 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5833 fprintf(stderr
, "-append only allowed with -kernel option\n");
5837 if (!linux_boot
&& initrd_filename
!= NULL
) {
5838 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5842 /* boot to floppy or the default cd if no hard disk defined yet */
5843 if (!boot_devices
[0]) {
5844 boot_devices
= "cad";
5846 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5849 if (init_timer_alarm() < 0) {
5850 fprintf(stderr
, "could not initialize alarm timer\n");
5853 if (use_icount
&& icount_time_shift
< 0) {
5855 /* 125MIPS seems a reasonable initial guess at the guest speed.
5856 It will be corrected fairly quickly anyway. */
5857 icount_time_shift
= 3;
5858 init_icount_adjust();
5865 /* init network clients */
5866 if (nb_net_clients
== 0) {
5867 /* if no clients, we use a default config */
5868 net_clients
[nb_net_clients
++] = "nic";
5870 net_clients
[nb_net_clients
++] = "user";
5874 for(i
= 0;i
< nb_net_clients
; i
++) {
5875 if (net_client_parse(net_clients
[i
]) < 0)
5879 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5880 net_set_boot_mask(net_boot
);
5884 /* init the bluetooth world */
5885 for (i
= 0; i
< nb_bt_opts
; i
++)
5886 if (bt_parse(bt_opts
[i
]))
5889 /* init the memory */
5891 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5894 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5895 guest ram allocation. It needs to go away. */
5896 if (kqemu_allowed
) {
5897 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5898 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5899 if (!kqemu_phys_ram_base
) {
5900 fprintf(stderr
, "Could not allocate physical memory\n");
5906 /* init the dynamic translator */
5907 cpu_exec_init_all(tb_size
* 1024 * 1024);
5911 /* we always create the cdrom drive, even if no disk is there */
5913 if (nb_drives_opt
< MAX_DRIVES
)
5914 drive_add(NULL
, CDROM_ALIAS
);
5916 /* we always create at least one floppy */
5918 if (nb_drives_opt
< MAX_DRIVES
)
5919 drive_add(NULL
, FD_ALIAS
, 0);
5921 /* we always create one sd slot, even if no card is in it */
5923 if (nb_drives_opt
< MAX_DRIVES
)
5924 drive_add(NULL
, SD_ALIAS
);
5926 /* open the virtual block devices */
5928 for(i
= 0; i
< nb_drives_opt
; i
++)
5929 if (drive_init(&drives_opt
[i
], snapshot
, machine
) == -1)
5932 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5933 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5936 /* must be after terminal init, SDL library changes signal handlers */
5940 /* Maintain compatibility with multiple stdio monitors */
5941 if (!strcmp(monitor_device
,"stdio")) {
5942 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5943 const char *devname
= serial_devices
[i
];
5944 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5945 monitor_device
= NULL
;
5947 } else if (devname
&& !strcmp(devname
,"stdio")) {
5948 monitor_device
= NULL
;
5949 serial_devices
[i
] = "mon:stdio";
5955 if (nb_numa_nodes
> 0) {
5958 if (nb_numa_nodes
> smp_cpus
) {
5959 nb_numa_nodes
= smp_cpus
;
5962 /* If no memory size if given for any node, assume the default case
5963 * and distribute the available memory equally across all nodes
5965 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5966 if (node_mem
[i
] != 0)
5969 if (i
== nb_numa_nodes
) {
5970 uint64_t usedmem
= 0;
5972 /* On Linux, the each node's border has to be 8MB aligned,
5973 * the final node gets the rest.
5975 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5976 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5977 usedmem
+= node_mem
[i
];
5979 node_mem
[i
] = ram_size
- usedmem
;
5982 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5983 if (node_cpumask
[i
] != 0)
5986 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5987 * must cope with this anyway, because there are BIOSes out there in
5988 * real machines which also use this scheme.
5990 if (i
== nb_numa_nodes
) {
5991 for (i
= 0; i
< smp_cpus
; i
++) {
5992 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5997 if (kvm_enabled()) {
6000 ret
= kvm_init(smp_cpus
);
6002 fprintf(stderr
, "failed to initialize KVM\n");
6007 if (monitor_device
) {
6008 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
6010 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
6015 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6016 const char *devname
= serial_devices
[i
];
6017 if (devname
&& strcmp(devname
, "none")) {
6019 snprintf(label
, sizeof(label
), "serial%d", i
);
6020 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6021 if (!serial_hds
[i
]) {
6022 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
6029 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6030 const char *devname
= parallel_devices
[i
];
6031 if (devname
&& strcmp(devname
, "none")) {
6033 snprintf(label
, sizeof(label
), "parallel%d", i
);
6034 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6035 if (!parallel_hds
[i
]) {
6036 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
6043 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6044 const char *devname
= virtio_consoles
[i
];
6045 if (devname
&& strcmp(devname
, "none")) {
6047 snprintf(label
, sizeof(label
), "virtcon%d", i
);
6048 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
6049 if (!virtcon_hds
[i
]) {
6050 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
6057 module_call_init(MODULE_INIT_DEVICE
);
6059 machine
->init(ram_size
, boot_devices
,
6060 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
6063 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
6064 for (i
= 0; i
< nb_numa_nodes
; i
++) {
6065 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
6071 current_machine
= machine
;
6073 /* Set KVM's vcpu state to qemu's initial CPUState. */
6074 if (kvm_enabled()) {
6077 ret
= kvm_sync_vcpus();
6079 fprintf(stderr
, "failed to initialize vcpus\n");
6084 /* init USB devices */
6086 for(i
= 0; i
< usb_devices_index
; i
++) {
6087 if (usb_device_add(usb_devices
[i
], 0) < 0) {
6088 fprintf(stderr
, "Warning: could not add USB device %s\n",
6095 dumb_display_init();
6096 /* just use the first displaystate for the moment */
6099 if (display_type
== DT_DEFAULT
) {
6100 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
6101 display_type
= DT_SDL
;
6103 display_type
= DT_VNC
;
6104 vnc_display
= "localhost:0,to=99";
6110 switch (display_type
) {
6113 #if defined(CONFIG_CURSES)
6115 curses_display_init(ds
, full_screen
);
6118 #if defined(CONFIG_SDL)
6120 sdl_display_init(ds
, full_screen
, no_frame
);
6122 #elif defined(CONFIG_COCOA)
6124 cocoa_display_init(ds
, full_screen
);
6128 vnc_display_init(ds
);
6129 if (vnc_display_open(ds
, vnc_display
) < 0)
6132 if (show_vnc_port
) {
6133 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6141 dcl
= ds
->listeners
;
6142 while (dcl
!= NULL
) {
6143 if (dcl
->dpy_refresh
!= NULL
) {
6144 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6145 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6150 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6151 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6152 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6155 text_consoles_set_display(display_state
);
6156 qemu_chr_initial_reset();
6158 if (monitor_device
&& monitor_hd
)
6159 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6161 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6162 const char *devname
= serial_devices
[i
];
6163 if (devname
&& strcmp(devname
, "none")) {
6164 if (strstart(devname
, "vc", 0))
6165 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6169 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6170 const char *devname
= parallel_devices
[i
];
6171 if (devname
&& strcmp(devname
, "none")) {
6172 if (strstart(devname
, "vc", 0))
6173 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6177 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6178 const char *devname
= virtio_consoles
[i
];
6179 if (virtcon_hds
[i
] && devname
) {
6180 if (strstart(devname
, "vc", 0))
6181 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6185 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6186 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6192 do_loadvm(cur_mon
, loadvm
);
6195 autostart
= 0; /* fixme how to deal with -daemonize */
6196 qemu_start_incoming_migration(incoming
);
6208 len
= write(fds
[1], &status
, 1);
6209 if (len
== -1 && (errno
== EINTR
))
6216 TFR(fd
= open("/dev/null", O_RDWR
));
6222 pwd
= getpwnam(run_as
);
6224 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6230 if (chroot(chroot_dir
) < 0) {
6231 fprintf(stderr
, "chroot failed\n");
6238 if (setgid(pwd
->pw_gid
) < 0) {
6239 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6242 if (setuid(pwd
->pw_uid
) < 0) {
6243 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6246 if (setuid(0) != -1) {
6247 fprintf(stderr
, "Dropping privileges failed\n");