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 CONFIG_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>
71 #include <sys/prctl.h>
73 /* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75 /* #include <linux/hpet.h> */
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
95 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
96 discussion about Solaris header problems */
97 extern int madvise(caddr_t
, size_t, int);
102 #if defined(__OpenBSD__)
106 #if defined(CONFIG_VDE)
107 #include <libvdeplug.h>
112 #include <mmsystem.h>
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"
146 #include "hw/loader.h"
153 #include "qemu-timer.h"
154 #include "qemu-char.h"
155 #include "cache-utils.h"
158 #include "audio/audio.h"
159 #include "migration.h"
162 #include "qemu-option.h"
163 #include "qemu-config.h"
167 #include "exec-all.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
173 #include "qemu-queue.h"
176 //#define DEBUG_SLIRP
178 #define DEFAULT_RAM_SIZE 128
180 /* Maximum number of monitor devices */
181 #define MAX_MONITOR_DEVICES 10
183 static const char *data_dir
;
184 const char *bios_name
= NULL
;
185 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
186 to store the VM snapshots */
187 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
188 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
189 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
190 static DisplayState
*display_state
;
191 DisplayType display_type
= DT_DEFAULT
;
192 const char* keyboard_layout
= NULL
;
195 NICInfo nd_table
[MAX_NICS
];
198 static int rtc_utc
= 1;
199 static int rtc_date_offset
= -1; /* -1 means no change */
200 QEMUClock
*rtc_clock
;
201 int vga_interface_type
= VGA_CIRRUS
;
203 int graphic_width
= 1024;
204 int graphic_height
= 768;
205 int graphic_depth
= 8;
207 int graphic_width
= 800;
208 int graphic_height
= 600;
209 int graphic_depth
= 15;
211 static int full_screen
= 0;
213 static int no_frame
= 0;
216 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
217 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
218 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
220 int win2k_install_hack
= 0;
229 const char *vnc_display
;
230 int acpi_enabled
= 1;
236 int graphic_rotate
= 0;
237 uint8_t irq0override
= 1;
241 const char *watchdog
;
242 const char *option_rom
[MAX_OPTION_ROMS
];
244 int semihosting_enabled
= 0;
248 const char *qemu_name
;
250 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
251 unsigned int nb_prom_envs
= 0;
252 const char *prom_envs
[MAX_PROM_ENVS
];
257 uint64_t node_mem
[MAX_NODES
];
258 uint64_t node_cpumask
[MAX_NODES
];
260 static CPUState
*cur_cpu
;
261 static CPUState
*next_cpu
;
262 static int timer_alarm_pending
= 1;
263 /* Conversion factor from emulated instructions to virtual clock ticks. */
264 static int icount_time_shift
;
265 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
266 #define MAX_ICOUNT_SHIFT 10
267 /* Compensate for varying guest execution speed. */
268 static int64_t qemu_icount_bias
;
269 static QEMUTimer
*icount_rt_timer
;
270 static QEMUTimer
*icount_vm_timer
;
271 static QEMUTimer
*nographic_timer
;
273 uint8_t qemu_uuid
[16];
275 static QEMUBootSetHandler
*boot_set_handler
;
276 static void *boot_set_opaque
;
278 /***********************************************************/
279 /* x86 ISA bus support */
281 target_phys_addr_t isa_mem_base
= 0;
284 /***********************************************************/
285 void hw_error(const char *fmt
, ...)
291 fprintf(stderr
, "qemu: hardware error: ");
292 vfprintf(stderr
, fmt
, ap
);
293 fprintf(stderr
, "\n");
294 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
295 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
297 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
299 cpu_dump_state(env
, stderr
, fprintf
, 0);
306 static void set_proc_name(const char *s
)
308 #if defined(__linux__) && defined(PR_SET_NAME)
312 name
[sizeof(name
) - 1] = 0;
313 strncpy(name
, s
, sizeof(name
));
314 /* Could rewrite argv[0] too, but that's a bit more complicated.
315 This simple way is enough for `top'. */
316 prctl(PR_SET_NAME
, name
);
323 static QEMUBalloonEvent
*qemu_balloon_event
;
324 void *qemu_balloon_event_opaque
;
326 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
328 qemu_balloon_event
= func
;
329 qemu_balloon_event_opaque
= opaque
;
332 void qemu_balloon(ram_addr_t target
)
334 if (qemu_balloon_event
)
335 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
338 ram_addr_t
qemu_balloon_status(void)
340 if (qemu_balloon_event
)
341 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
345 /***********************************************************/
348 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
349 static void *qemu_put_kbd_event_opaque
;
350 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
351 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
353 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
355 qemu_put_kbd_event_opaque
= opaque
;
356 qemu_put_kbd_event
= func
;
359 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
360 void *opaque
, int absolute
,
363 QEMUPutMouseEntry
*s
, *cursor
;
365 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
367 s
->qemu_put_mouse_event
= func
;
368 s
->qemu_put_mouse_event_opaque
= opaque
;
369 s
->qemu_put_mouse_event_absolute
= absolute
;
370 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
373 if (!qemu_put_mouse_event_head
) {
374 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
378 cursor
= qemu_put_mouse_event_head
;
379 while (cursor
->next
!= NULL
)
380 cursor
= cursor
->next
;
383 qemu_put_mouse_event_current
= s
;
388 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
390 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
392 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
395 cursor
= qemu_put_mouse_event_head
;
396 while (cursor
!= NULL
&& cursor
!= entry
) {
398 cursor
= cursor
->next
;
401 if (cursor
== NULL
) // does not exist or list empty
403 else if (prev
== NULL
) { // entry is head
404 qemu_put_mouse_event_head
= cursor
->next
;
405 if (qemu_put_mouse_event_current
== entry
)
406 qemu_put_mouse_event_current
= cursor
->next
;
407 qemu_free(entry
->qemu_put_mouse_event_name
);
412 prev
->next
= entry
->next
;
414 if (qemu_put_mouse_event_current
== entry
)
415 qemu_put_mouse_event_current
= prev
;
417 qemu_free(entry
->qemu_put_mouse_event_name
);
421 void kbd_put_keycode(int keycode
)
423 if (qemu_put_kbd_event
) {
424 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
428 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
430 QEMUPutMouseEvent
*mouse_event
;
431 void *mouse_event_opaque
;
434 if (!qemu_put_mouse_event_current
) {
439 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
441 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
444 if (graphic_rotate
) {
445 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
448 width
= graphic_width
- 1;
449 mouse_event(mouse_event_opaque
,
450 width
- dy
, dx
, dz
, buttons_state
);
452 mouse_event(mouse_event_opaque
,
453 dx
, dy
, dz
, buttons_state
);
457 int kbd_mouse_is_absolute(void)
459 if (!qemu_put_mouse_event_current
)
462 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
465 void do_info_mice(Monitor
*mon
)
467 QEMUPutMouseEntry
*cursor
;
470 if (!qemu_put_mouse_event_head
) {
471 monitor_printf(mon
, "No mouse devices connected\n");
475 monitor_printf(mon
, "Mouse devices available:\n");
476 cursor
= qemu_put_mouse_event_head
;
477 while (cursor
!= NULL
) {
478 monitor_printf(mon
, "%c Mouse #%d: %s\n",
479 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
480 index
, cursor
->qemu_put_mouse_event_name
);
482 cursor
= cursor
->next
;
486 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
488 QEMUPutMouseEntry
*cursor
;
490 int index
= qdict_get_int(qdict
, "index");
492 if (!qemu_put_mouse_event_head
) {
493 monitor_printf(mon
, "No mouse devices connected\n");
497 cursor
= qemu_put_mouse_event_head
;
498 while (cursor
!= NULL
&& index
!= i
) {
500 cursor
= cursor
->next
;
504 qemu_put_mouse_event_current
= cursor
;
506 monitor_printf(mon
, "Mouse at given index not found\n");
509 /* compute with 96 bit intermediate result: (a*b)/c */
510 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
515 #ifdef HOST_WORDS_BIGENDIAN
525 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
526 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
529 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
533 /***********************************************************/
534 /* real time host monotonic timer */
536 static int64_t get_clock_realtime(void)
540 gettimeofday(&tv
, NULL
);
541 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
546 static int64_t clock_freq
;
548 static void init_get_clock(void)
552 ret
= QueryPerformanceFrequency(&freq
);
554 fprintf(stderr
, "Could not calibrate ticks\n");
557 clock_freq
= freq
.QuadPart
;
560 static int64_t get_clock(void)
563 QueryPerformanceCounter(&ti
);
564 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
569 static int use_rt_clock
;
571 static void init_get_clock(void)
574 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
575 || defined(__DragonFly__)
578 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
585 static int64_t get_clock(void)
587 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
588 || defined(__DragonFly__)
591 clock_gettime(CLOCK_MONOTONIC
, &ts
);
592 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
596 /* XXX: using gettimeofday leads to problems if the date
597 changes, so it should be avoided. */
598 return get_clock_realtime();
603 /* Return the virtual CPU time, based on the instruction counter. */
604 static int64_t cpu_get_icount(void)
607 CPUState
*env
= cpu_single_env
;;
608 icount
= qemu_icount
;
611 fprintf(stderr
, "Bad clock read\n");
612 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
614 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
617 /***********************************************************/
618 /* guest cycle counter */
620 typedef struct TimersState
{
621 int64_t cpu_ticks_prev
;
622 int64_t cpu_ticks_offset
;
623 int64_t cpu_clock_offset
;
624 int32_t cpu_ticks_enabled
;
628 TimersState timers_state
;
630 /* return the host CPU cycle counter and handle stop/restart */
631 int64_t cpu_get_ticks(void)
634 return cpu_get_icount();
636 if (!timers_state
.cpu_ticks_enabled
) {
637 return timers_state
.cpu_ticks_offset
;
640 ticks
= cpu_get_real_ticks();
641 if (timers_state
.cpu_ticks_prev
> ticks
) {
642 /* Note: non increasing ticks may happen if the host uses
644 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
646 timers_state
.cpu_ticks_prev
= ticks
;
647 return ticks
+ timers_state
.cpu_ticks_offset
;
651 /* return the host CPU monotonic timer and handle stop/restart */
652 static int64_t cpu_get_clock(void)
655 if (!timers_state
.cpu_ticks_enabled
) {
656 return timers_state
.cpu_clock_offset
;
659 return ti
+ timers_state
.cpu_clock_offset
;
663 /* enable cpu_get_ticks() */
664 void cpu_enable_ticks(void)
666 if (!timers_state
.cpu_ticks_enabled
) {
667 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
668 timers_state
.cpu_clock_offset
-= get_clock();
669 timers_state
.cpu_ticks_enabled
= 1;
673 /* disable cpu_get_ticks() : the clock is stopped. You must not call
674 cpu_get_ticks() after that. */
675 void cpu_disable_ticks(void)
677 if (timers_state
.cpu_ticks_enabled
) {
678 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
679 timers_state
.cpu_clock_offset
= cpu_get_clock();
680 timers_state
.cpu_ticks_enabled
= 0;
684 /***********************************************************/
687 #define QEMU_CLOCK_REALTIME 0
688 #define QEMU_CLOCK_VIRTUAL 1
689 #define QEMU_CLOCK_HOST 2
693 /* XXX: add frequency */
701 struct QEMUTimer
*next
;
704 struct qemu_alarm_timer
{
708 int (*start
)(struct qemu_alarm_timer
*t
);
709 void (*stop
)(struct qemu_alarm_timer
*t
);
710 void (*rearm
)(struct qemu_alarm_timer
*t
);
714 #define ALARM_FLAG_DYNTICKS 0x1
715 #define ALARM_FLAG_EXPIRED 0x2
717 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
719 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
722 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
724 if (!alarm_has_dynticks(t
))
730 /* TODO: MIN_TIMER_REARM_US should be optimized */
731 #define MIN_TIMER_REARM_US 250
733 static struct qemu_alarm_timer
*alarm_timer
;
737 struct qemu_alarm_win32
{
740 } alarm_win32_data
= {0, -1};
742 static int win32_start_timer(struct qemu_alarm_timer
*t
);
743 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
744 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
748 static int unix_start_timer(struct qemu_alarm_timer
*t
);
749 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
753 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
754 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
755 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
757 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
758 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
760 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
761 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
763 #endif /* __linux__ */
767 /* Correlation between real and virtual time is always going to be
768 fairly approximate, so ignore small variation.
769 When the guest is idle real and virtual time will be aligned in
771 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
773 static void icount_adjust(void)
778 static int64_t last_delta
;
779 /* If the VM is not running, then do nothing. */
783 cur_time
= cpu_get_clock();
784 cur_icount
= qemu_get_clock(vm_clock
);
785 delta
= cur_icount
- cur_time
;
786 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
788 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
789 && icount_time_shift
> 0) {
790 /* The guest is getting too far ahead. Slow time down. */
794 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
795 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
796 /* The guest is getting too far behind. Speed time up. */
800 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
803 static void icount_adjust_rt(void * opaque
)
805 qemu_mod_timer(icount_rt_timer
,
806 qemu_get_clock(rt_clock
) + 1000);
810 static void icount_adjust_vm(void * opaque
)
812 qemu_mod_timer(icount_vm_timer
,
813 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
817 static void init_icount_adjust(void)
819 /* Have both realtime and virtual time triggers for speed adjustment.
820 The realtime trigger catches emulated time passing too slowly,
821 the virtual time trigger catches emulated time passing too fast.
822 Realtime triggers occur even when idle, so use them less frequently
824 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
825 qemu_mod_timer(icount_rt_timer
,
826 qemu_get_clock(rt_clock
) + 1000);
827 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
828 qemu_mod_timer(icount_vm_timer
,
829 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
832 static struct qemu_alarm_timer alarm_timers
[] = {
835 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
836 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
837 /* HPET - if available - is preferred */
838 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
839 /* ...otherwise try RTC */
840 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
842 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
844 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
845 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
846 {"win32", 0, win32_start_timer
,
847 win32_stop_timer
, NULL
, &alarm_win32_data
},
852 static void show_available_alarms(void)
856 printf("Available alarm timers, in order of precedence:\n");
857 for (i
= 0; alarm_timers
[i
].name
; i
++)
858 printf("%s\n", alarm_timers
[i
].name
);
861 static void configure_alarms(char const *opt
)
865 int count
= ARRAY_SIZE(alarm_timers
) - 1;
868 struct qemu_alarm_timer tmp
;
870 if (!strcmp(opt
, "?")) {
871 show_available_alarms();
875 arg
= qemu_strdup(opt
);
877 /* Reorder the array */
878 name
= strtok(arg
, ",");
880 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
881 if (!strcmp(alarm_timers
[i
].name
, name
))
886 fprintf(stderr
, "Unknown clock %s\n", name
);
895 tmp
= alarm_timers
[i
];
896 alarm_timers
[i
] = alarm_timers
[cur
];
897 alarm_timers
[cur
] = tmp
;
901 name
= strtok(NULL
, ",");
907 /* Disable remaining timers */
908 for (i
= cur
; i
< count
; i
++)
909 alarm_timers
[i
].name
= NULL
;
911 show_available_alarms();
916 #define QEMU_NUM_CLOCKS 3
920 QEMUClock
*host_clock
;
922 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
924 static QEMUClock
*qemu_new_clock(int type
)
927 clock
= qemu_mallocz(sizeof(QEMUClock
));
932 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
936 ts
= qemu_mallocz(sizeof(QEMUTimer
));
943 void qemu_free_timer(QEMUTimer
*ts
)
948 /* stop a timer, but do not dealloc it */
949 void qemu_del_timer(QEMUTimer
*ts
)
953 /* NOTE: this code must be signal safe because
954 qemu_timer_expired() can be called from a signal. */
955 pt
= &active_timers
[ts
->clock
->type
];
968 /* modify the current timer so that it will be fired when current_time
969 >= expire_time. The corresponding callback will be called. */
970 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
976 /* add the timer in the sorted list */
977 /* NOTE: this code must be signal safe because
978 qemu_timer_expired() can be called from a signal. */
979 pt
= &active_timers
[ts
->clock
->type
];
984 if (t
->expire_time
> expire_time
)
988 ts
->expire_time
= expire_time
;
992 /* Rearm if necessary */
993 if (pt
== &active_timers
[ts
->clock
->type
]) {
994 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
995 qemu_rearm_alarm_timer(alarm_timer
);
997 /* Interrupt execution to force deadline recalculation. */
1003 int qemu_timer_pending(QEMUTimer
*ts
)
1006 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1013 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1017 return (timer_head
->expire_time
<= current_time
);
1020 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1026 if (!ts
|| ts
->expire_time
> current_time
)
1028 /* remove timer from the list before calling the callback */
1029 *ptimer_head
= ts
->next
;
1032 /* run the callback (the timer list can be modified) */
1037 int64_t qemu_get_clock(QEMUClock
*clock
)
1039 switch(clock
->type
) {
1040 case QEMU_CLOCK_REALTIME
:
1041 return get_clock() / 1000000;
1043 case QEMU_CLOCK_VIRTUAL
:
1045 return cpu_get_icount();
1047 return cpu_get_clock();
1049 case QEMU_CLOCK_HOST
:
1050 return get_clock_realtime();
1054 static void init_clocks(void)
1057 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1058 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1059 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1061 rtc_clock
= host_clock
;
1065 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1067 uint64_t expire_time
;
1069 if (qemu_timer_pending(ts
)) {
1070 expire_time
= ts
->expire_time
;
1074 qemu_put_be64(f
, expire_time
);
1077 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1079 uint64_t expire_time
;
1081 expire_time
= qemu_get_be64(f
);
1082 if (expire_time
!= -1) {
1083 qemu_mod_timer(ts
, expire_time
);
1089 static const VMStateDescription vmstate_timers
= {
1092 .minimum_version_id
= 1,
1093 .minimum_version_id_old
= 1,
1094 .fields
= (VMStateField
[]) {
1095 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1096 VMSTATE_INT64(dummy
, TimersState
),
1097 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1098 VMSTATE_END_OF_LIST()
1102 static void qemu_event_increment(void);
1105 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1106 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1109 static void host_alarm_handler(int host_signum
)
1113 #define DISP_FREQ 1000
1115 static int64_t delta_min
= INT64_MAX
;
1116 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1118 ti
= qemu_get_clock(vm_clock
);
1119 if (last_clock
!= 0) {
1120 delta
= ti
- last_clock
;
1121 if (delta
< delta_min
)
1123 if (delta
> delta_max
)
1126 if (++count
== DISP_FREQ
) {
1127 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1128 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1129 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1130 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1131 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1133 delta_min
= INT64_MAX
;
1141 if (alarm_has_dynticks(alarm_timer
) ||
1143 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1144 qemu_get_clock(vm_clock
))) ||
1145 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1146 qemu_get_clock(rt_clock
)) ||
1147 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1148 qemu_get_clock(host_clock
))) {
1149 qemu_event_increment();
1150 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1152 #ifndef CONFIG_IOTHREAD
1154 /* stop the currently executing cpu because a timer occured */
1158 timer_alarm_pending
= 1;
1159 qemu_notify_event();
1163 static int64_t qemu_next_deadline(void)
1165 /* To avoid problems with overflow limit this to 2^32. */
1166 int64_t delta
= INT32_MAX
;
1168 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1169 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1170 qemu_get_clock(vm_clock
);
1172 if (active_timers
[QEMU_CLOCK_HOST
]) {
1173 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1174 qemu_get_clock(host_clock
);
1185 #if defined(__linux__)
1186 static uint64_t qemu_next_deadline_dyntick(void)
1194 delta
= (qemu_next_deadline() + 999) / 1000;
1196 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1197 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1198 qemu_get_clock(rt_clock
))*1000;
1199 if (rtdelta
< delta
)
1203 if (delta
< MIN_TIMER_REARM_US
)
1204 delta
= MIN_TIMER_REARM_US
;
1212 /* Sets a specific flag */
1213 static int fcntl_setfl(int fd
, int flag
)
1217 flags
= fcntl(fd
, F_GETFL
);
1221 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1227 #if defined(__linux__)
1229 #define RTC_FREQ 1024
1231 static void enable_sigio_timer(int fd
)
1233 struct sigaction act
;
1236 sigfillset(&act
.sa_mask
);
1238 act
.sa_handler
= host_alarm_handler
;
1240 sigaction(SIGIO
, &act
, NULL
);
1241 fcntl_setfl(fd
, O_ASYNC
);
1242 fcntl(fd
, F_SETOWN
, getpid());
1245 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1247 struct hpet_info info
;
1250 fd
= open("/dev/hpet", O_RDONLY
);
1255 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1257 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1258 "error, but for better emulation accuracy type:\n"
1259 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1263 /* Check capabilities */
1264 r
= ioctl(fd
, HPET_INFO
, &info
);
1268 /* Enable periodic mode */
1269 r
= ioctl(fd
, HPET_EPI
, 0);
1270 if (info
.hi_flags
&& (r
< 0))
1273 /* Enable interrupt */
1274 r
= ioctl(fd
, HPET_IE_ON
, 0);
1278 enable_sigio_timer(fd
);
1279 t
->priv
= (void *)(long)fd
;
1287 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1289 int fd
= (long)t
->priv
;
1294 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1297 unsigned long current_rtc_freq
= 0;
1299 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1302 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1303 if (current_rtc_freq
!= RTC_FREQ
&&
1304 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1305 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1306 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1307 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1310 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1316 enable_sigio_timer(rtc_fd
);
1318 t
->priv
= (void *)(long)rtc_fd
;
1323 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1325 int rtc_fd
= (long)t
->priv
;
1330 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1334 struct sigaction act
;
1336 sigfillset(&act
.sa_mask
);
1338 act
.sa_handler
= host_alarm_handler
;
1340 sigaction(SIGALRM
, &act
, NULL
);
1343 * Initialize ev struct to 0 to avoid valgrind complaining
1344 * about uninitialized data in timer_create call
1346 memset(&ev
, 0, sizeof(ev
));
1347 ev
.sigev_value
.sival_int
= 0;
1348 ev
.sigev_notify
= SIGEV_SIGNAL
;
1349 ev
.sigev_signo
= SIGALRM
;
1351 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1352 perror("timer_create");
1354 /* disable dynticks */
1355 fprintf(stderr
, "Dynamic Ticks disabled\n");
1360 t
->priv
= (void *)(long)host_timer
;
1365 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1367 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1369 timer_delete(host_timer
);
1372 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1374 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1375 struct itimerspec timeout
;
1376 int64_t nearest_delta_us
= INT64_MAX
;
1379 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1380 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1381 !active_timers
[QEMU_CLOCK_HOST
])
1384 nearest_delta_us
= qemu_next_deadline_dyntick();
1386 /* check whether a timer is already running */
1387 if (timer_gettime(host_timer
, &timeout
)) {
1389 fprintf(stderr
, "Internal timer error: aborting\n");
1392 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1393 if (current_us
&& current_us
<= nearest_delta_us
)
1396 timeout
.it_interval
.tv_sec
= 0;
1397 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1398 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1399 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1400 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1402 fprintf(stderr
, "Internal timer error: aborting\n");
1407 #endif /* defined(__linux__) */
1409 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1411 struct sigaction act
;
1412 struct itimerval itv
;
1416 sigfillset(&act
.sa_mask
);
1418 act
.sa_handler
= host_alarm_handler
;
1420 sigaction(SIGALRM
, &act
, NULL
);
1422 itv
.it_interval
.tv_sec
= 0;
1423 /* for i386 kernel 2.6 to get 1 ms */
1424 itv
.it_interval
.tv_usec
= 999;
1425 itv
.it_value
.tv_sec
= 0;
1426 itv
.it_value
.tv_usec
= 10 * 1000;
1428 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1435 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1437 struct itimerval itv
;
1439 memset(&itv
, 0, sizeof(itv
));
1440 setitimer(ITIMER_REAL
, &itv
, NULL
);
1443 #endif /* !defined(_WIN32) */
1448 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1451 struct qemu_alarm_win32
*data
= t
->priv
;
1454 memset(&tc
, 0, sizeof(tc
));
1455 timeGetDevCaps(&tc
, sizeof(tc
));
1457 if (data
->period
< tc
.wPeriodMin
)
1458 data
->period
= tc
.wPeriodMin
;
1460 timeBeginPeriod(data
->period
);
1462 flags
= TIME_CALLBACK_FUNCTION
;
1463 if (alarm_has_dynticks(t
))
1464 flags
|= TIME_ONESHOT
;
1466 flags
|= TIME_PERIODIC
;
1468 data
->timerId
= timeSetEvent(1, // interval (ms)
1469 data
->period
, // resolution
1470 host_alarm_handler
, // function
1471 (DWORD
)t
, // parameter
1474 if (!data
->timerId
) {
1475 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1477 timeEndPeriod(data
->period
);
1484 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1486 struct qemu_alarm_win32
*data
= t
->priv
;
1488 timeKillEvent(data
->timerId
);
1489 timeEndPeriod(data
->period
);
1492 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1494 struct qemu_alarm_win32
*data
= t
->priv
;
1496 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1497 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1498 !active_timers
[QEMU_CLOCK_HOST
])
1501 timeKillEvent(data
->timerId
);
1503 data
->timerId
= timeSetEvent(1,
1507 TIME_ONESHOT
| TIME_PERIODIC
);
1509 if (!data
->timerId
) {
1510 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1513 timeEndPeriod(data
->period
);
1520 static int init_timer_alarm(void)
1522 struct qemu_alarm_timer
*t
= NULL
;
1525 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1526 t
= &alarm_timers
[i
];
1546 static void quit_timers(void)
1548 alarm_timer
->stop(alarm_timer
);
1552 /***********************************************************/
1553 /* host time/date access */
1554 void qemu_get_timedate(struct tm
*tm
, int offset
)
1561 if (rtc_date_offset
== -1) {
1565 ret
= localtime(&ti
);
1567 ti
-= rtc_date_offset
;
1571 memcpy(tm
, ret
, sizeof(struct tm
));
1574 int qemu_timedate_diff(struct tm
*tm
)
1578 if (rtc_date_offset
== -1)
1580 seconds
= mktimegm(tm
);
1582 seconds
= mktime(tm
);
1584 seconds
= mktimegm(tm
) + rtc_date_offset
;
1586 return seconds
- time(NULL
);
1589 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1591 time_t rtc_start_date
;
1594 if (!strcmp(startdate
, "now") && legacy
) {
1595 rtc_date_offset
= -1;
1597 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1605 } else if (sscanf(startdate
, "%d-%d-%d",
1608 &tm
.tm_mday
) == 3) {
1617 rtc_start_date
= mktimegm(&tm
);
1618 if (rtc_start_date
== -1) {
1620 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1621 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1624 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1628 static void configure_rtc(QemuOpts
*opts
)
1632 value
= qemu_opt_get(opts
, "base");
1634 if (!strcmp(value
, "utc")) {
1636 } else if (!strcmp(value
, "localtime")) {
1639 configure_rtc_date_offset(value
, 0);
1642 value
= qemu_opt_get(opts
, "clock");
1644 if (!strcmp(value
, "host")) {
1645 rtc_clock
= host_clock
;
1646 } else if (!strcmp(value
, "vm")) {
1647 rtc_clock
= vm_clock
;
1649 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1653 #ifdef CONFIG_TARGET_I386
1654 value
= qemu_opt_get(opts
, "driftfix");
1656 if (!strcmp(buf
, "slew")) {
1658 } else if (!strcmp(buf
, "none")) {
1661 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1669 static void socket_cleanup(void)
1674 static int socket_init(void)
1679 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1681 err
= WSAGetLastError();
1682 fprintf(stderr
, "WSAStartup: %d\n", err
);
1685 atexit(socket_cleanup
);
1690 /***********************************************************/
1691 /* Bluetooth support */
1694 static struct HCIInfo
*hci_table
[MAX_NICS
];
1696 static struct bt_vlan_s
{
1697 struct bt_scatternet_s net
;
1699 struct bt_vlan_s
*next
;
1702 /* find or alloc a new bluetooth "VLAN" */
1703 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1705 struct bt_vlan_s
**pvlan
, *vlan
;
1706 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1710 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1712 pvlan
= &first_bt_vlan
;
1713 while (*pvlan
!= NULL
)
1714 pvlan
= &(*pvlan
)->next
;
1719 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1723 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1728 static struct HCIInfo null_hci
= {
1729 .cmd_send
= null_hci_send
,
1730 .sco_send
= null_hci_send
,
1731 .acl_send
= null_hci_send
,
1732 .bdaddr_set
= null_hci_addr_set
,
1735 struct HCIInfo
*qemu_next_hci(void)
1737 if (cur_hci
== nb_hcis
)
1740 return hci_table
[cur_hci
++];
1743 static struct HCIInfo
*hci_init(const char *str
)
1746 struct bt_scatternet_s
*vlan
= 0;
1748 if (!strcmp(str
, "null"))
1751 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1753 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1754 else if (!strncmp(str
, "hci", 3)) {
1757 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1758 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1763 vlan
= qemu_find_bt_vlan(0);
1765 return bt_new_hci(vlan
);
1768 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1773 static int bt_hci_parse(const char *str
)
1775 struct HCIInfo
*hci
;
1778 if (nb_hcis
>= MAX_NICS
) {
1779 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1783 hci
= hci_init(str
);
1792 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1793 hci
->bdaddr_set(hci
, bdaddr
.b
);
1795 hci_table
[nb_hcis
++] = hci
;
1800 static void bt_vhci_add(int vlan_id
)
1802 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1805 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1806 "an empty scatternet %i\n", vlan_id
);
1808 bt_vhci_init(bt_new_hci(vlan
));
1811 static struct bt_device_s
*bt_device_add(const char *opt
)
1813 struct bt_scatternet_s
*vlan
;
1815 char *endp
= strstr(opt
, ",vlan=");
1816 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1819 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1822 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1824 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1829 vlan
= qemu_find_bt_vlan(vlan_id
);
1832 fprintf(stderr
, "qemu: warning: adding a slave device to "
1833 "an empty scatternet %i\n", vlan_id
);
1835 if (!strcmp(devname
, "keyboard"))
1836 return bt_keyboard_init(vlan
);
1838 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1842 static int bt_parse(const char *opt
)
1844 const char *endp
, *p
;
1847 if (strstart(opt
, "hci", &endp
)) {
1848 if (!*endp
|| *endp
== ',') {
1850 if (!strstart(endp
, ",vlan=", 0))
1853 return bt_hci_parse(opt
);
1855 } else if (strstart(opt
, "vhci", &endp
)) {
1856 if (!*endp
|| *endp
== ',') {
1858 if (strstart(endp
, ",vlan=", &p
)) {
1859 vlan
= strtol(p
, (char **) &endp
, 0);
1861 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1865 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1874 } else if (strstart(opt
, "device:", &endp
))
1875 return !bt_device_add(endp
);
1877 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1881 /***********************************************************/
1882 /* QEMU Block devices */
1884 #define HD_ALIAS "index=%d,media=disk"
1885 #define CDROM_ALIAS "index=2,media=cdrom"
1886 #define FD_ALIAS "index=%d,if=floppy"
1887 #define PFLASH_ALIAS "if=pflash"
1888 #define MTD_ALIAS "if=mtd"
1889 #define SD_ALIAS "index=0,if=sd"
1891 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1898 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1901 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1903 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1904 __FUNCTION__
, optstr
);
1908 qemu_opt_set(opts
, "file", file
);
1912 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1916 /* seek interface, bus and unit */
1918 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1919 if (dinfo
->type
== type
&&
1920 dinfo
->bus
== bus
&&
1921 dinfo
->unit
== unit
)
1928 DriveInfo
*drive_get_by_id(const char *id
)
1932 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1933 if (strcmp(id
, dinfo
->id
))
1940 int drive_get_max_bus(BlockInterfaceType type
)
1946 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1947 if(dinfo
->type
== type
&&
1948 dinfo
->bus
> max_bus
)
1949 max_bus
= dinfo
->bus
;
1954 const char *drive_get_serial(BlockDriverState
*bdrv
)
1958 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1959 if (dinfo
->bdrv
== bdrv
)
1960 return dinfo
->serial
;
1966 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1970 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1971 if (dinfo
->bdrv
== bdrv
)
1972 return dinfo
->onerror
;
1975 return BLOCK_ERR_STOP_ENOSPC
;
1978 static void bdrv_format_print(void *opaque
, const char *name
)
1980 fprintf(stderr
, " %s", name
);
1983 void drive_uninit(DriveInfo
*dinfo
)
1985 qemu_opts_del(dinfo
->opts
);
1986 bdrv_delete(dinfo
->bdrv
);
1987 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1991 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1995 const char *file
= NULL
;
1998 const char *mediastr
= "";
1999 BlockInterfaceType type
;
2000 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2001 int bus_id
, unit_id
;
2002 int cyls
, heads
, secs
, translation
;
2003 BlockDriver
*drv
= NULL
;
2004 QEMUMachine
*machine
= opaque
;
2009 int bdrv_flags
, onerror
;
2010 const char *devaddr
;
2016 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2019 if (machine
&& machine
->use_scsi
) {
2021 max_devs
= MAX_SCSI_DEVS
;
2022 pstrcpy(devname
, sizeof(devname
), "scsi");
2025 max_devs
= MAX_IDE_DEVS
;
2026 pstrcpy(devname
, sizeof(devname
), "ide");
2030 /* extract parameters */
2031 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2032 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2033 index
= qemu_opt_get_number(opts
, "index", -1);
2035 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2036 heads
= qemu_opt_get_number(opts
, "heads", 0);
2037 secs
= qemu_opt_get_number(opts
, "secs", 0);
2039 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2041 file
= qemu_opt_get(opts
, "file");
2042 serial
= qemu_opt_get(opts
, "serial");
2044 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2045 pstrcpy(devname
, sizeof(devname
), buf
);
2046 if (!strcmp(buf
, "ide")) {
2048 max_devs
= MAX_IDE_DEVS
;
2049 } else if (!strcmp(buf
, "scsi")) {
2051 max_devs
= MAX_SCSI_DEVS
;
2052 } else if (!strcmp(buf
, "floppy")) {
2055 } else if (!strcmp(buf
, "pflash")) {
2058 } else if (!strcmp(buf
, "mtd")) {
2061 } else if (!strcmp(buf
, "sd")) {
2064 } else if (!strcmp(buf
, "virtio")) {
2067 } else if (!strcmp(buf
, "xen")) {
2070 } else if (!strcmp(buf
, "none")) {
2074 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2079 if (cyls
|| heads
|| secs
) {
2080 if (cyls
< 1 || cyls
> 16383) {
2081 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2084 if (heads
< 1 || heads
> 16) {
2085 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2088 if (secs
< 1 || secs
> 63) {
2089 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2094 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2097 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2101 if (!strcmp(buf
, "none"))
2102 translation
= BIOS_ATA_TRANSLATION_NONE
;
2103 else if (!strcmp(buf
, "lba"))
2104 translation
= BIOS_ATA_TRANSLATION_LBA
;
2105 else if (!strcmp(buf
, "auto"))
2106 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2108 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2113 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2114 if (!strcmp(buf
, "disk")) {
2116 } else if (!strcmp(buf
, "cdrom")) {
2117 if (cyls
|| secs
|| heads
) {
2119 "qemu: '%s' invalid physical CHS format\n", buf
);
2122 media
= MEDIA_CDROM
;
2124 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2129 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2130 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2132 else if (!strcmp(buf
, "writethrough"))
2134 else if (!strcmp(buf
, "writeback"))
2137 fprintf(stderr
, "qemu: invalid cache option\n");
2142 #ifdef CONFIG_LINUX_AIO
2143 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2144 if (!strcmp(buf
, "threads"))
2146 else if (!strcmp(buf
, "native"))
2149 fprintf(stderr
, "qemu: invalid aio option\n");
2155 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2156 if (strcmp(buf
, "?") == 0) {
2157 fprintf(stderr
, "qemu: Supported formats:");
2158 bdrv_iterate_format(bdrv_format_print
, NULL
);
2159 fprintf(stderr
, "\n");
2162 drv
= bdrv_find_format(buf
);
2164 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2169 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2170 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2171 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2172 fprintf(stderr
, "werror is no supported by this format\n");
2175 if (!strcmp(buf
, "ignore"))
2176 onerror
= BLOCK_ERR_IGNORE
;
2177 else if (!strcmp(buf
, "enospc"))
2178 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2179 else if (!strcmp(buf
, "stop"))
2180 onerror
= BLOCK_ERR_STOP_ANY
;
2181 else if (!strcmp(buf
, "report"))
2182 onerror
= BLOCK_ERR_REPORT
;
2184 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2189 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2190 if (type
!= IF_VIRTIO
) {
2191 fprintf(stderr
, "addr is not supported\n");
2196 /* compute bus and unit according index */
2199 if (bus_id
!= 0 || unit_id
!= -1) {
2201 "qemu: index cannot be used with bus and unit\n");
2209 unit_id
= index
% max_devs
;
2210 bus_id
= index
/ max_devs
;
2214 /* if user doesn't specify a unit_id,
2215 * try to find the first free
2218 if (unit_id
== -1) {
2220 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2222 if (max_devs
&& unit_id
>= max_devs
) {
2223 unit_id
-= max_devs
;
2231 if (max_devs
&& unit_id
>= max_devs
) {
2232 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2233 unit_id
, max_devs
- 1);
2238 * ignore multiple definitions
2241 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2248 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2249 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2250 dinfo
->id
= qemu_strdup(buf
);
2252 /* no id supplied -> create one */
2253 dinfo
->id
= qemu_mallocz(32);
2254 if (type
== IF_IDE
|| type
== IF_SCSI
)
2255 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2257 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2258 devname
, bus_id
, mediastr
, unit_id
);
2260 snprintf(dinfo
->id
, 32, "%s%s%i",
2261 devname
, mediastr
, unit_id
);
2263 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2264 dinfo
->devaddr
= devaddr
;
2266 dinfo
->bus
= bus_id
;
2267 dinfo
->unit
= unit_id
;
2268 dinfo
->onerror
= onerror
;
2271 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2272 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2282 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2283 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2287 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2292 /* FIXME: This isn't really a floppy, but it's a reasonable
2295 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2301 /* add virtio block device */
2302 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2303 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2304 qemu_opt_set(opts
, "drive", dinfo
->id
);
2306 qemu_opt_set(opts
, "addr", devaddr
);
2317 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2318 cache
= 2; /* always use write-back with snapshot */
2320 if (cache
== 0) /* no caching */
2321 bdrv_flags
|= BDRV_O_NOCACHE
;
2322 else if (cache
== 2) /* write-back */
2323 bdrv_flags
|= BDRV_O_CACHE_WB
;
2326 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2328 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2331 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2332 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2333 file
, strerror(errno
));
2337 if (bdrv_key_required(dinfo
->bdrv
))
2343 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2345 QEMUMachine
*machine
= opaque
;
2346 int fatal_error
= 0;
2348 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2355 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2357 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2358 qemu_opt_set(opts
, "snapshot", "on");
2363 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2365 boot_set_handler
= func
;
2366 boot_set_opaque
= opaque
;
2369 int qemu_boot_set(const char *boot_devices
)
2371 if (!boot_set_handler
) {
2374 return boot_set_handler(boot_set_opaque
, boot_devices
);
2377 static int parse_bootdevices(char *devices
)
2379 /* We just do some generic consistency checks */
2383 for (p
= devices
; *p
!= '\0'; p
++) {
2384 /* Allowed boot devices are:
2385 * a-b: floppy disk drives
2386 * c-f: IDE disk drives
2387 * g-m: machine implementation dependant drives
2388 * n-p: network devices
2389 * It's up to each machine implementation to check if the given boot
2390 * devices match the actual hardware implementation and firmware
2393 if (*p
< 'a' || *p
> 'p') {
2394 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2397 if (bitmap
& (1 << (*p
- 'a'))) {
2398 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2401 bitmap
|= 1 << (*p
- 'a');
2406 static void restore_boot_devices(void *opaque
)
2408 char *standard_boot_devices
= opaque
;
2410 qemu_boot_set(standard_boot_devices
);
2412 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2413 qemu_free(standard_boot_devices
);
2416 static void numa_add(const char *optarg
)
2420 unsigned long long value
, endvalue
;
2423 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2424 if (!strcmp(option
, "node")) {
2425 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2426 nodenr
= nb_numa_nodes
;
2428 nodenr
= strtoull(option
, NULL
, 10);
2431 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2432 node_mem
[nodenr
] = 0;
2434 value
= strtoull(option
, &endptr
, 0);
2436 case 0: case 'M': case 'm':
2443 node_mem
[nodenr
] = value
;
2445 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2446 node_cpumask
[nodenr
] = 0;
2448 value
= strtoull(option
, &endptr
, 10);
2451 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2453 if (*endptr
== '-') {
2454 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2455 if (endvalue
>= 63) {
2458 "only 63 CPUs in NUMA mode supported.\n");
2460 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2465 node_cpumask
[nodenr
] = value
;
2472 static void smp_parse(const char *optarg
)
2474 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2478 smp
= strtoul(optarg
, &endptr
, 10);
2479 if (endptr
!= optarg
) {
2480 if (*endptr
== ',') {
2484 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2485 sockets
= strtoull(option
, NULL
, 10);
2486 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2487 cores
= strtoull(option
, NULL
, 10);
2488 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2489 threads
= strtoull(option
, NULL
, 10);
2490 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2491 max_cpus
= strtoull(option
, NULL
, 10);
2493 /* compute missing values, prefer sockets over cores over threads */
2494 if (smp
== 0 || sockets
== 0) {
2495 sockets
= sockets
> 0 ? sockets
: 1;
2496 cores
= cores
> 0 ? cores
: 1;
2497 threads
= threads
> 0 ? threads
: 1;
2499 smp
= cores
* threads
* sockets
;
2501 sockets
= smp
/ (cores
* threads
);
2505 threads
= threads
> 0 ? threads
: 1;
2506 cores
= smp
/ (sockets
* threads
);
2509 sockets
= smp
/ (cores
* threads
);
2511 threads
= smp
/ (cores
* sockets
);
2516 smp_cores
= cores
> 0 ? cores
: 1;
2517 smp_threads
= threads
> 0 ? threads
: 1;
2519 max_cpus
= smp_cpus
;
2522 /***********************************************************/
2525 static void usb_msd_password_cb(void *opaque
, int err
)
2527 USBDevice
*dev
= opaque
;
2530 usb_device_attach(dev
);
2532 dev
->info
->handle_destroy(dev
);
2541 .qdev
= "QEMU USB Mouse",
2544 .qdev
= "QEMU USB Tablet",
2547 .qdev
= "QEMU USB Keyboard",
2549 .name
= "wacom-tablet",
2550 .qdev
= "QEMU PenPartner Tablet",
2554 static int usb_device_add(const char *devname
, int is_hotplug
)
2557 USBBus
*bus
= usb_bus_find(-1 /* any */);
2558 USBDevice
*dev
= NULL
;
2564 /* simple devices which don't need extra care */
2565 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2566 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2568 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2572 /* the other ones */
2573 if (strstart(devname
, "host:", &p
)) {
2574 dev
= usb_host_device_open(p
);
2575 } else if (strstart(devname
, "disk:", &p
)) {
2576 BlockDriverState
*bs
;
2578 dev
= usb_msd_init(p
);
2581 bs
= usb_msd_get_bdrv(dev
);
2582 if (bdrv_key_required(bs
)) {
2585 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2590 } else if (strstart(devname
, "serial:", &p
)) {
2591 dev
= usb_serial_init(p
);
2592 #ifdef CONFIG_BRLAPI
2593 } else if (!strcmp(devname
, "braille")) {
2594 dev
= usb_baum_init();
2596 } else if (strstart(devname
, "net:", &p
)) {
2599 if (net_client_init(NULL
, "nic", p
) < 0)
2601 nd_table
[nic
].model
= "usb";
2602 dev
= usb_net_init(&nd_table
[nic
]);
2603 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2604 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2605 bt_new_hci(qemu_find_bt_vlan(0)));
2616 static int usb_device_del(const char *devname
)
2621 if (strstart(devname
, "host:", &p
))
2622 return usb_host_device_close(p
);
2627 p
= strchr(devname
, '.');
2630 bus_num
= strtoul(devname
, NULL
, 0);
2631 addr
= strtoul(p
+ 1, NULL
, 0);
2633 return usb_device_delete_addr(bus_num
, addr
);
2636 static int usb_parse(const char *cmdline
)
2638 return usb_device_add(cmdline
, 0);
2641 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2643 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2646 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2648 usb_device_del(qdict_get_str(qdict
, "devname"));
2651 /***********************************************************/
2652 /* PCMCIA/Cardbus */
2654 static struct pcmcia_socket_entry_s
{
2655 PCMCIASocket
*socket
;
2656 struct pcmcia_socket_entry_s
*next
;
2657 } *pcmcia_sockets
= 0;
2659 void pcmcia_socket_register(PCMCIASocket
*socket
)
2661 struct pcmcia_socket_entry_s
*entry
;
2663 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2664 entry
->socket
= socket
;
2665 entry
->next
= pcmcia_sockets
;
2666 pcmcia_sockets
= entry
;
2669 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2671 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2673 ptr
= &pcmcia_sockets
;
2674 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2675 if (entry
->socket
== socket
) {
2681 void pcmcia_info(Monitor
*mon
)
2683 struct pcmcia_socket_entry_s
*iter
;
2685 if (!pcmcia_sockets
)
2686 monitor_printf(mon
, "No PCMCIA sockets\n");
2688 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2689 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2690 iter
->socket
->attached
? iter
->socket
->card_string
:
2694 /***********************************************************/
2695 /* register display */
2697 struct DisplayAllocator default_allocator
= {
2698 defaultallocator_create_displaysurface
,
2699 defaultallocator_resize_displaysurface
,
2700 defaultallocator_free_displaysurface
2703 void register_displaystate(DisplayState
*ds
)
2713 DisplayState
*get_displaystate(void)
2715 return display_state
;
2718 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2720 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2721 return ds
->allocator
;
2726 static void dumb_display_init(void)
2728 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2729 ds
->allocator
= &default_allocator
;
2730 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2731 register_displaystate(ds
);
2734 /***********************************************************/
2737 typedef struct IOHandlerRecord
{
2739 IOCanRWHandler
*fd_read_poll
;
2741 IOHandler
*fd_write
;
2744 /* temporary data */
2746 struct IOHandlerRecord
*next
;
2749 static IOHandlerRecord
*first_io_handler
;
2751 /* XXX: fd_read_poll should be suppressed, but an API change is
2752 necessary in the character devices to suppress fd_can_read(). */
2753 int qemu_set_fd_handler2(int fd
,
2754 IOCanRWHandler
*fd_read_poll
,
2756 IOHandler
*fd_write
,
2759 IOHandlerRecord
**pioh
, *ioh
;
2761 if (!fd_read
&& !fd_write
) {
2762 pioh
= &first_io_handler
;
2767 if (ioh
->fd
== fd
) {
2774 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2778 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2779 ioh
->next
= first_io_handler
;
2780 first_io_handler
= ioh
;
2783 ioh
->fd_read_poll
= fd_read_poll
;
2784 ioh
->fd_read
= fd_read
;
2785 ioh
->fd_write
= fd_write
;
2786 ioh
->opaque
= opaque
;
2792 int qemu_set_fd_handler(int fd
,
2794 IOHandler
*fd_write
,
2797 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2801 /***********************************************************/
2802 /* Polling handling */
2804 typedef struct PollingEntry
{
2807 struct PollingEntry
*next
;
2810 static PollingEntry
*first_polling_entry
;
2812 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2814 PollingEntry
**ppe
, *pe
;
2815 pe
= qemu_mallocz(sizeof(PollingEntry
));
2817 pe
->opaque
= opaque
;
2818 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2823 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2825 PollingEntry
**ppe
, *pe
;
2826 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2828 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2836 /***********************************************************/
2837 /* Wait objects support */
2838 typedef struct WaitObjects
{
2840 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2841 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2842 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2845 static WaitObjects wait_objects
= {0};
2847 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2849 WaitObjects
*w
= &wait_objects
;
2851 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2853 w
->events
[w
->num
] = handle
;
2854 w
->func
[w
->num
] = func
;
2855 w
->opaque
[w
->num
] = opaque
;
2860 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2863 WaitObjects
*w
= &wait_objects
;
2866 for (i
= 0; i
< w
->num
; i
++) {
2867 if (w
->events
[i
] == handle
)
2870 w
->events
[i
] = w
->events
[i
+ 1];
2871 w
->func
[i
] = w
->func
[i
+ 1];
2872 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2880 /***********************************************************/
2881 /* ram save/restore */
2883 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2884 #define RAM_SAVE_FLAG_COMPRESS 0x02
2885 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2886 #define RAM_SAVE_FLAG_PAGE 0x08
2887 #define RAM_SAVE_FLAG_EOS 0x10
2889 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2891 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2892 uint32_t *array
= (uint32_t *)page
;
2895 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2896 if (array
[i
] != val
)
2903 static int ram_save_block(QEMUFile
*f
)
2905 static ram_addr_t current_addr
= 0;
2906 ram_addr_t saved_addr
= current_addr
;
2907 ram_addr_t addr
= 0;
2910 while (addr
< last_ram_offset
) {
2911 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2914 cpu_physical_memory_reset_dirty(current_addr
,
2915 current_addr
+ TARGET_PAGE_SIZE
,
2916 MIGRATION_DIRTY_FLAG
);
2918 p
= qemu_get_ram_ptr(current_addr
);
2920 if (is_dup_page(p
, *p
)) {
2921 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2922 qemu_put_byte(f
, *p
);
2924 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2925 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2931 addr
+= TARGET_PAGE_SIZE
;
2932 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2938 static uint64_t bytes_transferred
= 0;
2940 static ram_addr_t
ram_save_remaining(void)
2943 ram_addr_t count
= 0;
2945 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2946 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2953 uint64_t ram_bytes_remaining(void)
2955 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2958 uint64_t ram_bytes_transferred(void)
2960 return bytes_transferred
;
2963 uint64_t ram_bytes_total(void)
2965 return last_ram_offset
;
2968 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2971 uint64_t bytes_transferred_last
;
2973 uint64_t expected_time
= 0;
2975 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2976 qemu_file_set_error(f
);
2981 /* Make sure all dirty bits are set */
2982 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2983 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2984 cpu_physical_memory_set_dirty(addr
);
2987 /* Enable dirty memory tracking */
2988 cpu_physical_memory_set_dirty_tracking(1);
2990 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2993 bytes_transferred_last
= bytes_transferred
;
2994 bwidth
= get_clock();
2996 while (!qemu_file_rate_limit(f
)) {
2999 ret
= ram_save_block(f
);
3000 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3001 if (ret
== 0) /* no more blocks */
3005 bwidth
= get_clock() - bwidth
;
3006 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3008 /* if we haven't transferred anything this round, force expected_time to a
3009 * a very high value, but without crashing */
3013 /* try transferring iterative blocks of memory */
3017 /* flush all remaining blocks regardless of rate limiting */
3018 while (ram_save_block(f
) != 0) {
3019 bytes_transferred
+= TARGET_PAGE_SIZE
;
3021 cpu_physical_memory_set_dirty_tracking(0);
3024 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3026 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3028 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3031 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3036 if (version_id
!= 3)
3040 addr
= qemu_get_be64(f
);
3042 flags
= addr
& ~TARGET_PAGE_MASK
;
3043 addr
&= TARGET_PAGE_MASK
;
3045 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3046 if (addr
!= last_ram_offset
)
3050 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3051 uint8_t ch
= qemu_get_byte(f
);
3052 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3055 (!kvm_enabled() || kvm_has_sync_mmu())) {
3056 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3059 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3060 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3061 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3066 void qemu_service_io(void)
3068 qemu_notify_event();
3071 /***********************************************************/
3072 /* bottom halves (can be seen as timers which expire ASAP) */
3083 static QEMUBH
*first_bh
= NULL
;
3085 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3088 bh
= qemu_mallocz(sizeof(QEMUBH
));
3090 bh
->opaque
= opaque
;
3091 bh
->next
= first_bh
;
3096 int qemu_bh_poll(void)
3102 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3103 if (!bh
->deleted
&& bh
->scheduled
) {
3112 /* remove deleted bhs */
3126 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3134 void qemu_bh_schedule(QEMUBH
*bh
)
3140 /* stop the currently executing CPU to execute the BH ASAP */
3141 qemu_notify_event();
3144 void qemu_bh_cancel(QEMUBH
*bh
)
3149 void qemu_bh_delete(QEMUBH
*bh
)
3155 static void qemu_bh_update_timeout(int *timeout
)
3159 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3160 if (!bh
->deleted
&& bh
->scheduled
) {
3162 /* idle bottom halves will be polled at least
3164 *timeout
= MIN(10, *timeout
);
3166 /* non-idle bottom halves will be executed
3175 /***********************************************************/
3176 /* machine registration */
3178 static QEMUMachine
*first_machine
= NULL
;
3179 QEMUMachine
*current_machine
= NULL
;
3181 int qemu_register_machine(QEMUMachine
*m
)
3184 pm
= &first_machine
;
3192 static QEMUMachine
*find_machine(const char *name
)
3196 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3197 if (!strcmp(m
->name
, name
))
3199 if (m
->alias
&& !strcmp(m
->alias
, name
))
3205 static QEMUMachine
*find_default_machine(void)
3209 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3210 if (m
->is_default
) {
3217 /***********************************************************/
3218 /* main execution loop */
3220 static void gui_update(void *opaque
)
3222 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3223 DisplayState
*ds
= opaque
;
3224 DisplayChangeListener
*dcl
= ds
->listeners
;
3228 while (dcl
!= NULL
) {
3229 if (dcl
->gui_timer_interval
&&
3230 dcl
->gui_timer_interval
< interval
)
3231 interval
= dcl
->gui_timer_interval
;
3234 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3237 static void nographic_update(void *opaque
)
3239 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3241 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3244 struct vm_change_state_entry
{
3245 VMChangeStateHandler
*cb
;
3247 QLIST_ENTRY (vm_change_state_entry
) entries
;
3250 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3252 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3255 VMChangeStateEntry
*e
;
3257 e
= qemu_mallocz(sizeof (*e
));
3261 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3265 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3267 QLIST_REMOVE (e
, entries
);
3271 static void vm_state_notify(int running
, int reason
)
3273 VMChangeStateEntry
*e
;
3275 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3276 e
->cb(e
->opaque
, running
, reason
);
3280 static void resume_all_vcpus(void);
3281 static void pause_all_vcpus(void);
3288 vm_state_notify(1, 0);
3289 qemu_rearm_alarm_timer(alarm_timer
);
3294 /* reset/shutdown handler */
3296 typedef struct QEMUResetEntry
{
3297 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3298 QEMUResetHandler
*func
;
3302 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3303 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3304 static int reset_requested
;
3305 static int shutdown_requested
;
3306 static int powerdown_requested
;
3307 static int debug_requested
;
3308 static int vmstop_requested
;
3310 int qemu_shutdown_requested(void)
3312 int r
= shutdown_requested
;
3313 shutdown_requested
= 0;
3317 int qemu_reset_requested(void)
3319 int r
= reset_requested
;
3320 reset_requested
= 0;
3324 int qemu_powerdown_requested(void)
3326 int r
= powerdown_requested
;
3327 powerdown_requested
= 0;
3331 static int qemu_debug_requested(void)
3333 int r
= debug_requested
;
3334 debug_requested
= 0;
3338 static int qemu_vmstop_requested(void)
3340 int r
= vmstop_requested
;
3341 vmstop_requested
= 0;
3345 static void do_vm_stop(int reason
)
3348 cpu_disable_ticks();
3351 vm_state_notify(0, reason
);
3355 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3357 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3360 re
->opaque
= opaque
;
3361 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3364 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3368 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3369 if (re
->func
== func
&& re
->opaque
== opaque
) {
3370 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3377 void qemu_system_reset(void)
3379 QEMUResetEntry
*re
, *nre
;
3381 /* reset all devices */
3382 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3383 re
->func(re
->opaque
);
3387 void qemu_system_reset_request(void)
3390 shutdown_requested
= 1;
3392 reset_requested
= 1;
3394 qemu_notify_event();
3397 void qemu_system_shutdown_request(void)
3399 shutdown_requested
= 1;
3400 qemu_notify_event();
3403 void qemu_system_powerdown_request(void)
3405 powerdown_requested
= 1;
3406 qemu_notify_event();
3409 #ifdef CONFIG_IOTHREAD
3410 static void qemu_system_vmstop_request(int reason
)
3412 vmstop_requested
= reason
;
3413 qemu_notify_event();
3418 static int io_thread_fd
= -1;
3420 static void qemu_event_increment(void)
3422 static const char byte
= 0;
3424 if (io_thread_fd
== -1)
3427 write(io_thread_fd
, &byte
, sizeof(byte
));
3430 static void qemu_event_read(void *opaque
)
3432 int fd
= (unsigned long)opaque
;
3435 /* Drain the notify pipe */
3438 len
= read(fd
, buffer
, sizeof(buffer
));
3439 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3442 static int qemu_event_init(void)
3451 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3455 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3459 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3460 (void *)(unsigned long)fds
[0]);
3462 io_thread_fd
= fds
[1];
3471 HANDLE qemu_event_handle
;
3473 static void dummy_event_handler(void *opaque
)
3477 static int qemu_event_init(void)
3479 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3480 if (!qemu_event_handle
) {
3481 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3484 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3488 static void qemu_event_increment(void)
3490 if (!SetEvent(qemu_event_handle
)) {
3491 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3498 static int cpu_can_run(CPUState
*env
)
3507 #ifndef CONFIG_IOTHREAD
3508 static int qemu_init_main_loop(void)
3510 return qemu_event_init();
3513 void qemu_init_vcpu(void *_env
)
3515 CPUState
*env
= _env
;
3519 env
->nr_cores
= smp_cores
;
3520 env
->nr_threads
= smp_threads
;
3524 int qemu_cpu_self(void *env
)
3529 static void resume_all_vcpus(void)
3533 static void pause_all_vcpus(void)
3537 void qemu_cpu_kick(void *env
)
3542 void qemu_notify_event(void)
3544 CPUState
*env
= cpu_single_env
;
3551 #define qemu_mutex_lock_iothread() do { } while (0)
3552 #define qemu_mutex_unlock_iothread() do { } while (0)
3554 void vm_stop(int reason
)
3559 #else /* CONFIG_IOTHREAD */
3561 #include "qemu-thread.h"
3563 QemuMutex qemu_global_mutex
;
3564 static QemuMutex qemu_fair_mutex
;
3566 static QemuThread io_thread
;
3568 static QemuThread
*tcg_cpu_thread
;
3569 static QemuCond
*tcg_halt_cond
;
3571 static int qemu_system_ready
;
3573 static QemuCond qemu_cpu_cond
;
3575 static QemuCond qemu_system_cond
;
3576 static QemuCond qemu_pause_cond
;
3578 static void block_io_signals(void);
3579 static void unblock_io_signals(void);
3580 static int tcg_has_work(void);
3582 static int qemu_init_main_loop(void)
3586 ret
= qemu_event_init();
3590 qemu_cond_init(&qemu_pause_cond
);
3591 qemu_mutex_init(&qemu_fair_mutex
);
3592 qemu_mutex_init(&qemu_global_mutex
);
3593 qemu_mutex_lock(&qemu_global_mutex
);
3595 unblock_io_signals();
3596 qemu_thread_self(&io_thread
);
3601 static void qemu_wait_io_event(CPUState
*env
)
3603 while (!tcg_has_work())
3604 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3606 qemu_mutex_unlock(&qemu_global_mutex
);
3609 * Users of qemu_global_mutex can be starved, having no chance
3610 * to acquire it since this path will get to it first.
3611 * So use another lock to provide fairness.
3613 qemu_mutex_lock(&qemu_fair_mutex
);
3614 qemu_mutex_unlock(&qemu_fair_mutex
);
3616 qemu_mutex_lock(&qemu_global_mutex
);
3620 qemu_cond_signal(&qemu_pause_cond
);
3624 static int qemu_cpu_exec(CPUState
*env
);
3626 static void *kvm_cpu_thread_fn(void *arg
)
3628 CPUState
*env
= arg
;
3631 qemu_thread_self(env
->thread
);
3635 /* signal CPU creation */
3636 qemu_mutex_lock(&qemu_global_mutex
);
3638 qemu_cond_signal(&qemu_cpu_cond
);
3640 /* and wait for machine initialization */
3641 while (!qemu_system_ready
)
3642 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3645 if (cpu_can_run(env
))
3647 qemu_wait_io_event(env
);
3653 static void tcg_cpu_exec(void);
3655 static void *tcg_cpu_thread_fn(void *arg
)
3657 CPUState
*env
= arg
;
3660 qemu_thread_self(env
->thread
);
3662 /* signal CPU creation */
3663 qemu_mutex_lock(&qemu_global_mutex
);
3664 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3666 qemu_cond_signal(&qemu_cpu_cond
);
3668 /* and wait for machine initialization */
3669 while (!qemu_system_ready
)
3670 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3674 qemu_wait_io_event(cur_cpu
);
3680 void qemu_cpu_kick(void *_env
)
3682 CPUState
*env
= _env
;
3683 qemu_cond_broadcast(env
->halt_cond
);
3685 qemu_thread_signal(env
->thread
, SIGUSR1
);
3688 int qemu_cpu_self(void *_env
)
3690 CPUState
*env
= _env
;
3693 qemu_thread_self(&this);
3695 return qemu_thread_equal(&this, env
->thread
);
3698 static void cpu_signal(int sig
)
3701 cpu_exit(cpu_single_env
);
3704 static void block_io_signals(void)
3707 struct sigaction sigact
;
3710 sigaddset(&set
, SIGUSR2
);
3711 sigaddset(&set
, SIGIO
);
3712 sigaddset(&set
, SIGALRM
);
3713 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3716 sigaddset(&set
, SIGUSR1
);
3717 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3719 memset(&sigact
, 0, sizeof(sigact
));
3720 sigact
.sa_handler
= cpu_signal
;
3721 sigaction(SIGUSR1
, &sigact
, NULL
);
3724 static void unblock_io_signals(void)
3729 sigaddset(&set
, SIGUSR2
);
3730 sigaddset(&set
, SIGIO
);
3731 sigaddset(&set
, SIGALRM
);
3732 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3735 sigaddset(&set
, SIGUSR1
);
3736 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3739 static void qemu_signal_lock(unsigned int msecs
)
3741 qemu_mutex_lock(&qemu_fair_mutex
);
3743 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3744 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3745 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3748 qemu_mutex_unlock(&qemu_fair_mutex
);
3751 static void qemu_mutex_lock_iothread(void)
3753 if (kvm_enabled()) {
3754 qemu_mutex_lock(&qemu_fair_mutex
);
3755 qemu_mutex_lock(&qemu_global_mutex
);
3756 qemu_mutex_unlock(&qemu_fair_mutex
);
3758 qemu_signal_lock(100);
3761 static void qemu_mutex_unlock_iothread(void)
3763 qemu_mutex_unlock(&qemu_global_mutex
);
3766 static int all_vcpus_paused(void)
3768 CPUState
*penv
= first_cpu
;
3773 penv
= (CPUState
*)penv
->next_cpu
;
3779 static void pause_all_vcpus(void)
3781 CPUState
*penv
= first_cpu
;
3785 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3786 qemu_cpu_kick(penv
);
3787 penv
= (CPUState
*)penv
->next_cpu
;
3790 while (!all_vcpus_paused()) {
3791 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3794 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3795 penv
= (CPUState
*)penv
->next_cpu
;
3800 static void resume_all_vcpus(void)
3802 CPUState
*penv
= first_cpu
;
3807 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3808 qemu_cpu_kick(penv
);
3809 penv
= (CPUState
*)penv
->next_cpu
;
3813 static void tcg_init_vcpu(void *_env
)
3815 CPUState
*env
= _env
;
3816 /* share a single thread for all cpus with TCG */
3817 if (!tcg_cpu_thread
) {
3818 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3819 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3820 qemu_cond_init(env
->halt_cond
);
3821 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3822 while (env
->created
== 0)
3823 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3824 tcg_cpu_thread
= env
->thread
;
3825 tcg_halt_cond
= env
->halt_cond
;
3827 env
->thread
= tcg_cpu_thread
;
3828 env
->halt_cond
= tcg_halt_cond
;
3832 static void kvm_start_vcpu(CPUState
*env
)
3834 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3835 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3836 qemu_cond_init(env
->halt_cond
);
3837 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3838 while (env
->created
== 0)
3839 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3842 void qemu_init_vcpu(void *_env
)
3844 CPUState
*env
= _env
;
3847 kvm_start_vcpu(env
);
3850 env
->nr_cores
= smp_cores
;
3851 env
->nr_threads
= smp_threads
;
3854 void qemu_notify_event(void)
3856 qemu_event_increment();
3859 void vm_stop(int reason
)
3862 qemu_thread_self(&me
);
3864 if (!qemu_thread_equal(&me
, &io_thread
)) {
3865 qemu_system_vmstop_request(reason
);
3867 * FIXME: should not return to device code in case
3868 * vm_stop() has been requested.
3870 if (cpu_single_env
) {
3871 cpu_exit(cpu_single_env
);
3872 cpu_single_env
->stop
= 1;
3883 static void host_main_loop_wait(int *timeout
)
3889 /* XXX: need to suppress polling by better using win32 events */
3891 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3892 ret
|= pe
->func(pe
->opaque
);
3896 WaitObjects
*w
= &wait_objects
;
3898 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3899 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3900 if (w
->func
[ret
- WAIT_OBJECT_0
])
3901 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3903 /* Check for additional signaled events */
3904 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3906 /* Check if event is signaled */
3907 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3908 if(ret2
== WAIT_OBJECT_0
) {
3910 w
->func
[i
](w
->opaque
[i
]);
3911 } else if (ret2
== WAIT_TIMEOUT
) {
3913 err
= GetLastError();
3914 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3917 } else if (ret
== WAIT_TIMEOUT
) {
3919 err
= GetLastError();
3920 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3927 static void host_main_loop_wait(int *timeout
)
3932 void main_loop_wait(int timeout
)
3934 IOHandlerRecord
*ioh
;
3935 fd_set rfds
, wfds
, xfds
;
3939 qemu_bh_update_timeout(&timeout
);
3941 host_main_loop_wait(&timeout
);
3943 /* poll any events */
3944 /* XXX: separate device handlers from system ones */
3949 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3953 (!ioh
->fd_read_poll
||
3954 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3955 FD_SET(ioh
->fd
, &rfds
);
3959 if (ioh
->fd_write
) {
3960 FD_SET(ioh
->fd
, &wfds
);
3966 tv
.tv_sec
= timeout
/ 1000;
3967 tv
.tv_usec
= (timeout
% 1000) * 1000;
3969 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3971 qemu_mutex_unlock_iothread();
3972 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3973 qemu_mutex_lock_iothread();
3975 IOHandlerRecord
**pioh
;
3977 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3978 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3979 ioh
->fd_read(ioh
->opaque
);
3981 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3982 ioh
->fd_write(ioh
->opaque
);
3986 /* remove deleted IO handlers */
3987 pioh
= &first_io_handler
;
3998 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4000 /* rearm timer, if not periodic */
4001 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4002 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4003 qemu_rearm_alarm_timer(alarm_timer
);
4006 /* vm time timers */
4008 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4009 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
4010 qemu_get_clock(vm_clock
));
4013 /* real time timers */
4014 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
4015 qemu_get_clock(rt_clock
));
4017 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
4018 qemu_get_clock(host_clock
));
4020 /* Check bottom-halves last in case any of the earlier events triggered
4026 static int qemu_cpu_exec(CPUState
*env
)
4029 #ifdef CONFIG_PROFILER
4033 #ifdef CONFIG_PROFILER
4034 ti
= profile_getclock();
4039 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4040 env
->icount_decr
.u16
.low
= 0;
4041 env
->icount_extra
= 0;
4042 count
= qemu_next_deadline();
4043 count
= (count
+ (1 << icount_time_shift
) - 1)
4044 >> icount_time_shift
;
4045 qemu_icount
+= count
;
4046 decr
= (count
> 0xffff) ? 0xffff : count
;
4048 env
->icount_decr
.u16
.low
= decr
;
4049 env
->icount_extra
= count
;
4051 ret
= cpu_exec(env
);
4052 #ifdef CONFIG_PROFILER
4053 qemu_time
+= profile_getclock() - ti
;
4056 /* Fold pending instructions back into the
4057 instruction counter, and clear the interrupt flag. */
4058 qemu_icount
-= (env
->icount_decr
.u16
.low
4059 + env
->icount_extra
);
4060 env
->icount_decr
.u32
= 0;
4061 env
->icount_extra
= 0;
4066 static void tcg_cpu_exec(void)
4070 if (next_cpu
== NULL
)
4071 next_cpu
= first_cpu
;
4072 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4073 CPUState
*env
= cur_cpu
= next_cpu
;
4077 if (timer_alarm_pending
) {
4078 timer_alarm_pending
= 0;
4081 if (cpu_can_run(env
))
4082 ret
= qemu_cpu_exec(env
);
4083 if (ret
== EXCP_DEBUG
) {
4084 gdb_set_stop_cpu(env
);
4085 debug_requested
= 1;
4091 static int cpu_has_work(CPUState
*env
)
4099 if (qemu_cpu_has_work(env
))
4104 static int tcg_has_work(void)
4108 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4109 if (cpu_has_work(env
))
4114 static int qemu_calculate_timeout(void)
4116 #ifndef CONFIG_IOTHREAD
4121 else if (tcg_has_work())
4123 else if (!use_icount
)
4126 /* XXX: use timeout computed from timers */
4129 /* Advance virtual time to the next event. */
4130 if (use_icount
== 1) {
4131 /* When not using an adaptive execution frequency
4132 we tend to get badly out of sync with real time,
4133 so just delay for a reasonable amount of time. */
4136 delta
= cpu_get_icount() - cpu_get_clock();
4139 /* If virtual time is ahead of real time then just
4141 timeout
= (delta
/ 1000000) + 1;
4143 /* Wait for either IO to occur or the next
4145 add
= qemu_next_deadline();
4146 /* We advance the timer before checking for IO.
4147 Limit the amount we advance so that early IO
4148 activity won't get the guest too far ahead. */
4152 add
= (add
+ (1 << icount_time_shift
) - 1)
4153 >> icount_time_shift
;
4155 timeout
= delta
/ 1000000;
4162 #else /* CONFIG_IOTHREAD */
4167 static int vm_can_run(void)
4169 if (powerdown_requested
)
4171 if (reset_requested
)
4173 if (shutdown_requested
)
4175 if (debug_requested
)
4180 qemu_irq qemu_system_powerdown
;
4182 static void main_loop(void)
4186 #ifdef CONFIG_IOTHREAD
4187 qemu_system_ready
= 1;
4188 qemu_cond_broadcast(&qemu_system_cond
);
4193 #ifdef CONFIG_PROFILER
4196 #ifndef CONFIG_IOTHREAD
4199 #ifdef CONFIG_PROFILER
4200 ti
= profile_getclock();
4202 main_loop_wait(qemu_calculate_timeout());
4203 #ifdef CONFIG_PROFILER
4204 dev_time
+= profile_getclock() - ti
;
4206 } while (vm_can_run());
4208 if (qemu_debug_requested())
4209 vm_stop(EXCP_DEBUG
);
4210 if (qemu_shutdown_requested()) {
4217 if (qemu_reset_requested()) {
4219 qemu_system_reset();
4222 if (qemu_powerdown_requested()) {
4223 qemu_irq_raise(qemu_system_powerdown
);
4225 if ((r
= qemu_vmstop_requested()))
4231 static void version(void)
4233 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4236 static void help(int exitcode
)
4239 printf("usage: %s [options] [disk_image]\n"
4241 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4243 #define DEF(option, opt_arg, opt_enum, opt_help) \
4245 #define DEFHEADING(text) stringify(text) "\n"
4246 #include "qemu-options.h"
4251 "During emulation, the following keys are useful:\n"
4252 "ctrl-alt-f toggle full screen\n"
4253 "ctrl-alt-n switch to virtual console 'n'\n"
4254 "ctrl-alt toggle mouse and keyboard grab\n"
4256 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4261 DEFAULT_NETWORK_SCRIPT
,
4262 DEFAULT_NETWORK_DOWN_SCRIPT
,
4264 DEFAULT_GDBSTUB_PORT
,
4269 #define HAS_ARG 0x0001
4272 #define DEF(option, opt_arg, opt_enum, opt_help) \
4274 #define DEFHEADING(text)
4275 #include "qemu-options.h"
4281 typedef struct QEMUOption
{
4287 static const QEMUOption qemu_options
[] = {
4288 { "h", 0, QEMU_OPTION_h
},
4289 #define DEF(option, opt_arg, opt_enum, opt_help) \
4290 { option, opt_arg, opt_enum },
4291 #define DEFHEADING(text)
4292 #include "qemu-options.h"
4300 struct soundhw soundhw
[] = {
4301 #ifdef HAS_AUDIO_CHOICE
4302 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4308 { .init_isa
= pcspk_audio_init
}
4315 "Creative Sound Blaster 16",
4318 { .init_isa
= SB16_init
}
4322 #ifdef CONFIG_CS4231A
4328 { .init_isa
= cs4231a_init
}
4336 "Yamaha YMF262 (OPL3)",
4338 "Yamaha YM3812 (OPL2)",
4342 { .init_isa
= Adlib_init
}
4349 "Gravis Ultrasound GF1",
4352 { .init_isa
= GUS_init
}
4359 "Intel 82801AA AC97 Audio",
4362 { .init_pci
= ac97_init
}
4366 #ifdef CONFIG_ES1370
4369 "ENSONIQ AudioPCI ES1370",
4372 { .init_pci
= es1370_init
}
4376 #endif /* HAS_AUDIO_CHOICE */
4378 { NULL
, NULL
, 0, 0, { NULL
} }
4381 static void select_soundhw (const char *optarg
)
4385 if (*optarg
== '?') {
4388 printf ("Valid sound card names (comma separated):\n");
4389 for (c
= soundhw
; c
->name
; ++c
) {
4390 printf ("%-11s %s\n", c
->name
, c
->descr
);
4392 printf ("\n-soundhw all will enable all of the above\n");
4393 exit (*optarg
!= '?');
4401 if (!strcmp (optarg
, "all")) {
4402 for (c
= soundhw
; c
->name
; ++c
) {
4410 e
= strchr (p
, ',');
4411 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4413 for (c
= soundhw
; c
->name
; ++c
) {
4414 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4423 "Unknown sound card name (too big to show)\n");
4426 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4431 p
+= l
+ (e
!= NULL
);
4435 goto show_valid_cards
;
4440 static void select_vgahw (const char *p
)
4444 vga_interface_type
= VGA_NONE
;
4445 if (strstart(p
, "std", &opts
)) {
4446 vga_interface_type
= VGA_STD
;
4447 } else if (strstart(p
, "cirrus", &opts
)) {
4448 vga_interface_type
= VGA_CIRRUS
;
4449 } else if (strstart(p
, "vmware", &opts
)) {
4450 vga_interface_type
= VGA_VMWARE
;
4451 } else if (strstart(p
, "xenfb", &opts
)) {
4452 vga_interface_type
= VGA_XENFB
;
4453 } else if (!strstart(p
, "none", &opts
)) {
4455 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4459 const char *nextopt
;
4461 if (strstart(opts
, ",retrace=", &nextopt
)) {
4463 if (strstart(opts
, "dumb", &nextopt
))
4464 vga_retrace_method
= VGA_RETRACE_DUMB
;
4465 else if (strstart(opts
, "precise", &nextopt
))
4466 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4467 else goto invalid_vga
;
4468 } else goto invalid_vga
;
4474 static int balloon_parse(const char *arg
)
4478 if (strcmp(arg
, "none") == 0) {
4482 if (!strncmp(arg
, "virtio", 6)) {
4483 if (arg
[6] == ',') {
4484 /* have params -> parse them */
4485 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4489 /* create empty opts */
4490 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4492 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4501 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4503 exit(STATUS_CONTROL_C_EXIT
);
4508 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4512 if(strlen(str
) != 36)
4515 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4516 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4517 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4523 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4529 #define MAX_NET_CLIENTS 32
4533 static void termsig_handler(int signal
)
4535 qemu_system_shutdown_request();
4538 static void sigchld_handler(int signal
)
4540 waitpid(-1, NULL
, WNOHANG
);
4543 static void sighandler_setup(void)
4545 struct sigaction act
;
4547 memset(&act
, 0, sizeof(act
));
4548 act
.sa_handler
= termsig_handler
;
4549 sigaction(SIGINT
, &act
, NULL
);
4550 sigaction(SIGHUP
, &act
, NULL
);
4551 sigaction(SIGTERM
, &act
, NULL
);
4553 act
.sa_handler
= sigchld_handler
;
4554 act
.sa_flags
= SA_NOCLDSTOP
;
4555 sigaction(SIGCHLD
, &act
, NULL
);
4561 /* Look for support files in the same directory as the executable. */
4562 static char *find_datadir(const char *argv0
)
4568 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4575 while (p
!= buf
&& *p
!= '\\')
4578 if (access(buf
, R_OK
) == 0) {
4579 return qemu_strdup(buf
);
4585 /* Find a likely location for support files using the location of the binary.
4586 For installed binaries this will be "$bindir/../share/qemu". When
4587 running from the build tree this will be "$bindir/../pc-bios". */
4588 #define SHARE_SUFFIX "/share/qemu"
4589 #define BUILD_SUFFIX "/pc-bios"
4590 static char *find_datadir(const char *argv0
)
4598 #if defined(__linux__)
4601 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4607 #elif defined(__FreeBSD__)
4610 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4617 /* If we don't have any way of figuring out the actual executable
4618 location then try argv[0]. */
4620 p
= realpath(argv0
, buf
);
4628 max_len
= strlen(dir
) +
4629 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4630 res
= qemu_mallocz(max_len
);
4631 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4632 if (access(res
, R_OK
)) {
4633 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4634 if (access(res
, R_OK
)) {
4646 char *qemu_find_file(int type
, const char *name
)
4652 /* If name contains path separators then try it as a straight path. */
4653 if ((strchr(name
, '/') || strchr(name
, '\\'))
4654 && access(name
, R_OK
) == 0) {
4655 return qemu_strdup(name
);
4658 case QEMU_FILE_TYPE_BIOS
:
4661 case QEMU_FILE_TYPE_KEYMAP
:
4662 subdir
= "keymaps/";
4667 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4668 buf
= qemu_mallocz(len
);
4669 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4670 if (access(buf
, R_OK
)) {
4677 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4681 dev
= qdev_device_add(opts
);
4687 struct device_config
{
4689 DEV_USB
, /* -usbdevice */
4692 const char *cmdline
;
4693 QTAILQ_ENTRY(device_config
) next
;
4695 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4697 static void add_device_config(int type
, const char *cmdline
)
4699 struct device_config
*conf
;
4701 conf
= qemu_mallocz(sizeof(*conf
));
4703 conf
->cmdline
= cmdline
;
4704 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4707 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4709 struct device_config
*conf
;
4712 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4713 if (conf
->type
!= type
)
4715 rc
= func(conf
->cmdline
);
4722 int main(int argc
, char **argv
, char **envp
)
4724 const char *gdbstub_dev
= NULL
;
4725 uint32_t boot_devices_bitmap
= 0;
4727 int snapshot
, linux_boot
, net_boot
;
4728 const char *initrd_filename
;
4729 const char *kernel_filename
, *kernel_cmdline
;
4730 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4732 DisplayChangeListener
*dcl
;
4733 int cyls
, heads
, secs
, translation
;
4734 const char *net_clients
[MAX_NET_CLIENTS
];
4736 QemuOpts
*hda_opts
= NULL
, *opts
;
4738 const char *r
, *optarg
;
4739 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4740 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4741 int monitor_device_index
;
4742 const char *serial_devices
[MAX_SERIAL_PORTS
];
4743 int serial_device_index
;
4744 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4745 int parallel_device_index
;
4746 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4747 int virtio_console_index
;
4748 const char *loadvm
= NULL
;
4749 QEMUMachine
*machine
;
4750 const char *cpu_model
;
4755 const char *pid_file
= NULL
;
4756 const char *incoming
= NULL
;
4759 struct passwd
*pwd
= NULL
;
4760 const char *chroot_dir
= NULL
;
4761 const char *run_as
= NULL
;
4764 int show_vnc_port
= 0;
4768 qemu_errors_to_file(stderr
);
4769 qemu_cache_utils_init(envp
);
4771 QLIST_INIT (&vm_change_state_head
);
4774 struct sigaction act
;
4775 sigfillset(&act
.sa_mask
);
4777 act
.sa_handler
= SIG_IGN
;
4778 sigaction(SIGPIPE
, &act
, NULL
);
4781 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4782 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4783 QEMU to run on a single CPU */
4788 h
= GetCurrentProcess();
4789 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4790 for(i
= 0; i
< 32; i
++) {
4791 if (mask
& (1 << i
))
4796 SetProcessAffinityMask(h
, mask
);
4802 module_call_init(MODULE_INIT_MACHINE
);
4803 machine
= find_default_machine();
4805 initrd_filename
= NULL
;
4808 kernel_filename
= NULL
;
4809 kernel_cmdline
= "";
4810 cyls
= heads
= secs
= 0;
4811 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4813 serial_devices
[0] = "vc:80Cx24C";
4814 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4815 serial_devices
[i
] = NULL
;
4816 serial_device_index
= 0;
4818 parallel_devices
[0] = "vc:80Cx24C";
4819 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4820 parallel_devices
[i
] = NULL
;
4821 parallel_device_index
= 0;
4823 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4824 virtio_consoles
[i
] = NULL
;
4825 virtio_console_index
= 0;
4827 monitor_devices
[0] = "vc:80Cx24C";
4828 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4829 monitor_devices
[i
] = NULL
;
4831 monitor_device_index
= 0;
4833 for (i
= 0; i
< MAX_NODES
; i
++) {
4835 node_cpumask
[i
] = 0;
4851 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4853 const QEMUOption
*popt
;
4856 /* Treat --foo the same as -foo. */
4859 popt
= qemu_options
;
4862 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4866 if (!strcmp(popt
->name
, r
+ 1))
4870 if (popt
->flags
& HAS_ARG
) {
4871 if (optind
>= argc
) {
4872 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4876 optarg
= argv
[optind
++];
4881 switch(popt
->index
) {
4883 machine
= find_machine(optarg
);
4886 printf("Supported machines are:\n");
4887 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4889 printf("%-10s %s (alias of %s)\n",
4890 m
->alias
, m
->desc
, m
->name
);
4891 printf("%-10s %s%s\n",
4893 m
->is_default
? " (default)" : "");
4895 exit(*optarg
!= '?');
4898 case QEMU_OPTION_cpu
:
4899 /* hw initialization will check this */
4900 if (*optarg
== '?') {
4901 /* XXX: implement xxx_cpu_list for targets that still miss it */
4902 #if defined(cpu_list)
4903 cpu_list(stdout
, &fprintf
);
4910 case QEMU_OPTION_initrd
:
4911 initrd_filename
= optarg
;
4913 case QEMU_OPTION_hda
:
4915 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4917 hda_opts
= drive_add(optarg
, HD_ALIAS
4918 ",cyls=%d,heads=%d,secs=%d%s",
4919 0, cyls
, heads
, secs
,
4920 translation
== BIOS_ATA_TRANSLATION_LBA
?
4922 translation
== BIOS_ATA_TRANSLATION_NONE
?
4923 ",trans=none" : "");
4925 case QEMU_OPTION_hdb
:
4926 case QEMU_OPTION_hdc
:
4927 case QEMU_OPTION_hdd
:
4928 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4930 case QEMU_OPTION_drive
:
4931 drive_add(NULL
, "%s", optarg
);
4933 case QEMU_OPTION_set
:
4934 if (qemu_set_option(optarg
) != 0)
4937 case QEMU_OPTION_mtdblock
:
4938 drive_add(optarg
, MTD_ALIAS
);
4940 case QEMU_OPTION_sd
:
4941 drive_add(optarg
, SD_ALIAS
);
4943 case QEMU_OPTION_pflash
:
4944 drive_add(optarg
, PFLASH_ALIAS
);
4946 case QEMU_OPTION_snapshot
:
4949 case QEMU_OPTION_hdachs
:
4953 cyls
= strtol(p
, (char **)&p
, 0);
4954 if (cyls
< 1 || cyls
> 16383)
4959 heads
= strtol(p
, (char **)&p
, 0);
4960 if (heads
< 1 || heads
> 16)
4965 secs
= strtol(p
, (char **)&p
, 0);
4966 if (secs
< 1 || secs
> 63)
4970 if (!strcmp(p
, "none"))
4971 translation
= BIOS_ATA_TRANSLATION_NONE
;
4972 else if (!strcmp(p
, "lba"))
4973 translation
= BIOS_ATA_TRANSLATION_LBA
;
4974 else if (!strcmp(p
, "auto"))
4975 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4978 } else if (*p
!= '\0') {
4980 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4983 if (hda_opts
!= NULL
) {
4985 snprintf(num
, sizeof(num
), "%d", cyls
);
4986 qemu_opt_set(hda_opts
, "cyls", num
);
4987 snprintf(num
, sizeof(num
), "%d", heads
);
4988 qemu_opt_set(hda_opts
, "heads", num
);
4989 snprintf(num
, sizeof(num
), "%d", secs
);
4990 qemu_opt_set(hda_opts
, "secs", num
);
4991 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4992 qemu_opt_set(hda_opts
, "trans", "lba");
4993 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4994 qemu_opt_set(hda_opts
, "trans", "none");
4998 case QEMU_OPTION_numa
:
4999 if (nb_numa_nodes
>= MAX_NODES
) {
5000 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5005 case QEMU_OPTION_nographic
:
5006 display_type
= DT_NOGRAPHIC
;
5008 #ifdef CONFIG_CURSES
5009 case QEMU_OPTION_curses
:
5010 display_type
= DT_CURSES
;
5013 case QEMU_OPTION_portrait
:
5016 case QEMU_OPTION_kernel
:
5017 kernel_filename
= optarg
;
5019 case QEMU_OPTION_append
:
5020 kernel_cmdline
= optarg
;
5022 case QEMU_OPTION_cdrom
:
5023 drive_add(optarg
, CDROM_ALIAS
);
5025 case QEMU_OPTION_boot
:
5027 static const char * const params
[] = {
5028 "order", "once", "menu", NULL
5030 char buf
[sizeof(boot_devices
)];
5031 char *standard_boot_devices
;
5034 if (!strchr(optarg
, '=')) {
5036 pstrcpy(buf
, sizeof(buf
), optarg
);
5037 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5039 "qemu: unknown boot parameter '%s' in '%s'\n",
5045 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5046 boot_devices_bitmap
= parse_bootdevices(buf
);
5047 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5050 if (get_param_value(buf
, sizeof(buf
),
5052 boot_devices_bitmap
|= parse_bootdevices(buf
);
5053 standard_boot_devices
= qemu_strdup(boot_devices
);
5054 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5055 qemu_register_reset(restore_boot_devices
,
5056 standard_boot_devices
);
5058 if (get_param_value(buf
, sizeof(buf
),
5060 if (!strcmp(buf
, "on")) {
5062 } else if (!strcmp(buf
, "off")) {
5066 "qemu: invalid option value '%s'\n",
5074 case QEMU_OPTION_fda
:
5075 case QEMU_OPTION_fdb
:
5076 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5079 case QEMU_OPTION_no_fd_bootchk
:
5083 case QEMU_OPTION_net
:
5084 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5085 fprintf(stderr
, "qemu: too many network clients\n");
5088 net_clients
[nb_net_clients
] = optarg
;
5092 case QEMU_OPTION_tftp
:
5093 legacy_tftp_prefix
= optarg
;
5095 case QEMU_OPTION_bootp
:
5096 legacy_bootp_filename
= optarg
;
5099 case QEMU_OPTION_smb
:
5100 net_slirp_smb(optarg
);
5103 case QEMU_OPTION_redir
:
5104 net_slirp_redir(optarg
);
5107 case QEMU_OPTION_bt
:
5108 add_device_config(DEV_BT
, optarg
);
5111 case QEMU_OPTION_audio_help
:
5115 case QEMU_OPTION_soundhw
:
5116 select_soundhw (optarg
);
5122 case QEMU_OPTION_version
:
5126 case QEMU_OPTION_m
: {
5130 value
= strtoul(optarg
, &ptr
, 10);
5132 case 0: case 'M': case 'm':
5139 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5143 /* On 32-bit hosts, QEMU is limited by virtual address space */
5144 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5145 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5148 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5149 fprintf(stderr
, "qemu: ram size too large\n");
5158 const CPULogItem
*item
;
5160 mask
= cpu_str_to_log_mask(optarg
);
5162 printf("Log items (comma separated):\n");
5163 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5164 printf("%-10s %s\n", item
->name
, item
->help
);
5172 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5174 case QEMU_OPTION_gdb
:
5175 gdbstub_dev
= optarg
;
5180 case QEMU_OPTION_bios
:
5183 case QEMU_OPTION_singlestep
:
5191 keyboard_layout
= optarg
;
5194 case QEMU_OPTION_localtime
:
5197 case QEMU_OPTION_vga
:
5198 select_vgahw (optarg
);
5200 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5206 w
= strtol(p
, (char **)&p
, 10);
5209 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5215 h
= strtol(p
, (char **)&p
, 10);
5220 depth
= strtol(p
, (char **)&p
, 10);
5221 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5222 depth
!= 24 && depth
!= 32)
5224 } else if (*p
== '\0') {
5225 depth
= graphic_depth
;
5232 graphic_depth
= depth
;
5236 case QEMU_OPTION_echr
:
5239 term_escape_char
= strtol(optarg
, &r
, 0);
5241 printf("Bad argument to echr\n");
5244 case QEMU_OPTION_monitor
:
5245 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5246 fprintf(stderr
, "qemu: too many monitor devices\n");
5249 monitor_devices
[monitor_device_index
] = optarg
;
5250 monitor_device_index
++;
5252 case QEMU_OPTION_chardev
:
5253 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5255 fprintf(stderr
, "parse error: %s\n", optarg
);
5258 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5262 case QEMU_OPTION_serial
:
5263 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5264 fprintf(stderr
, "qemu: too many serial ports\n");
5267 serial_devices
[serial_device_index
] = optarg
;
5268 serial_device_index
++;
5270 case QEMU_OPTION_watchdog
:
5273 "qemu: only one watchdog option may be given\n");
5278 case QEMU_OPTION_watchdog_action
:
5279 if (select_watchdog_action(optarg
) == -1) {
5280 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5284 case QEMU_OPTION_virtiocon
:
5285 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5286 fprintf(stderr
, "qemu: too many virtio consoles\n");
5289 virtio_consoles
[virtio_console_index
] = optarg
;
5290 virtio_console_index
++;
5292 case QEMU_OPTION_parallel
:
5293 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5294 fprintf(stderr
, "qemu: too many parallel ports\n");
5297 parallel_devices
[parallel_device_index
] = optarg
;
5298 parallel_device_index
++;
5300 case QEMU_OPTION_loadvm
:
5303 case QEMU_OPTION_full_screen
:
5307 case QEMU_OPTION_no_frame
:
5310 case QEMU_OPTION_alt_grab
:
5313 case QEMU_OPTION_no_quit
:
5316 case QEMU_OPTION_sdl
:
5317 display_type
= DT_SDL
;
5320 case QEMU_OPTION_pidfile
:
5324 case QEMU_OPTION_win2k_hack
:
5325 win2k_install_hack
= 1;
5327 case QEMU_OPTION_rtc_td_hack
:
5330 case QEMU_OPTION_acpitable
:
5331 if(acpi_table_add(optarg
) < 0) {
5332 fprintf(stderr
, "Wrong acpi table provided\n");
5336 case QEMU_OPTION_smbios
:
5337 if(smbios_entry_add(optarg
) < 0) {
5338 fprintf(stderr
, "Wrong smbios provided\n");
5344 case QEMU_OPTION_enable_kvm
:
5348 case QEMU_OPTION_usb
:
5351 case QEMU_OPTION_usbdevice
:
5353 add_device_config(DEV_USB
, optarg
);
5355 case QEMU_OPTION_device
:
5356 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5358 fprintf(stderr
, "parse error: %s\n", optarg
);
5362 case QEMU_OPTION_smp
:
5365 fprintf(stderr
, "Invalid number of CPUs\n");
5368 if (max_cpus
< smp_cpus
) {
5369 fprintf(stderr
, "maxcpus must be equal to or greater than "
5373 if (max_cpus
> 255) {
5374 fprintf(stderr
, "Unsupported number of maxcpus\n");
5378 case QEMU_OPTION_vnc
:
5379 display_type
= DT_VNC
;
5380 vnc_display
= optarg
;
5383 case QEMU_OPTION_no_acpi
:
5386 case QEMU_OPTION_no_hpet
:
5389 case QEMU_OPTION_balloon
:
5390 if (balloon_parse(optarg
) < 0) {
5391 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5396 case QEMU_OPTION_no_reboot
:
5399 case QEMU_OPTION_no_shutdown
:
5402 case QEMU_OPTION_show_cursor
:
5405 case QEMU_OPTION_uuid
:
5406 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5407 fprintf(stderr
, "Fail to parse UUID string."
5408 " Wrong format.\n");
5413 case QEMU_OPTION_daemonize
:
5417 case QEMU_OPTION_option_rom
:
5418 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5419 fprintf(stderr
, "Too many option ROMs\n");
5422 option_rom
[nb_option_roms
] = optarg
;
5425 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5426 case QEMU_OPTION_semihosting
:
5427 semihosting_enabled
= 1;
5430 case QEMU_OPTION_name
:
5431 qemu_name
= qemu_strdup(optarg
);
5433 char *p
= strchr(qemu_name
, ',');
5436 if (strncmp(p
, "process=", 8)) {
5437 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5445 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5446 case QEMU_OPTION_prom_env
:
5447 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5448 fprintf(stderr
, "Too many prom variables\n");
5451 prom_envs
[nb_prom_envs
] = optarg
;
5456 case QEMU_OPTION_old_param
:
5460 case QEMU_OPTION_clock
:
5461 configure_alarms(optarg
);
5463 case QEMU_OPTION_startdate
:
5464 configure_rtc_date_offset(optarg
, 1);
5466 case QEMU_OPTION_rtc
:
5467 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5469 fprintf(stderr
, "parse error: %s\n", optarg
);
5472 configure_rtc(opts
);
5474 case QEMU_OPTION_tb_size
:
5475 tb_size
= strtol(optarg
, NULL
, 0);
5479 case QEMU_OPTION_icount
:
5481 if (strcmp(optarg
, "auto") == 0) {
5482 icount_time_shift
= -1;
5484 icount_time_shift
= strtol(optarg
, NULL
, 0);
5487 case QEMU_OPTION_incoming
:
5491 case QEMU_OPTION_chroot
:
5492 chroot_dir
= optarg
;
5494 case QEMU_OPTION_runas
:
5499 case QEMU_OPTION_xen_domid
:
5500 xen_domid
= atoi(optarg
);
5502 case QEMU_OPTION_xen_create
:
5503 xen_mode
= XEN_CREATE
;
5505 case QEMU_OPTION_xen_attach
:
5506 xen_mode
= XEN_ATTACH
;
5513 /* If no data_dir is specified then try to find it relative to the
5516 data_dir
= find_datadir(argv
[0]);
5518 /* If all else fails use the install patch specified when building. */
5520 data_dir
= CONFIG_QEMU_SHAREDIR
;
5524 * Default to max_cpus = smp_cpus, in case the user doesn't
5525 * specify a max_cpus value.
5528 max_cpus
= smp_cpus
;
5530 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5531 if (smp_cpus
> machine
->max_cpus
) {
5532 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5533 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5538 if (display_type
== DT_NOGRAPHIC
) {
5539 if (serial_device_index
== 0)
5540 serial_devices
[0] = "stdio";
5541 if (parallel_device_index
== 0)
5542 parallel_devices
[0] = "null";
5543 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5544 monitor_devices
[0] = "stdio";
5552 if (pipe(fds
) == -1)
5563 len
= read(fds
[0], &status
, 1);
5564 if (len
== -1 && (errno
== EINTR
))
5569 else if (status
== 1) {
5570 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5587 signal(SIGTSTP
, SIG_IGN
);
5588 signal(SIGTTOU
, SIG_IGN
);
5589 signal(SIGTTIN
, SIG_IGN
);
5592 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5595 write(fds
[1], &status
, 1);
5597 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5602 if (kvm_enabled()) {
5605 ret
= kvm_init(smp_cpus
);
5607 fprintf(stderr
, "failed to initialize KVM\n");
5612 if (qemu_init_main_loop()) {
5613 fprintf(stderr
, "qemu_init_main_loop failed\n");
5616 linux_boot
= (kernel_filename
!= NULL
);
5618 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5619 fprintf(stderr
, "-append only allowed with -kernel option\n");
5623 if (!linux_boot
&& initrd_filename
!= NULL
) {
5624 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5629 /* Win32 doesn't support line-buffering and requires size >= 2 */
5630 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5633 if (init_timer_alarm() < 0) {
5634 fprintf(stderr
, "could not initialize alarm timer\n");
5637 if (use_icount
&& icount_time_shift
< 0) {
5639 /* 125MIPS seems a reasonable initial guess at the guest speed.
5640 It will be corrected fairly quickly anyway. */
5641 icount_time_shift
= 3;
5642 init_icount_adjust();
5649 /* init network clients */
5650 if (nb_net_clients
== 0) {
5651 /* if no clients, we use a default config */
5652 net_clients
[nb_net_clients
++] = "nic";
5654 net_clients
[nb_net_clients
++] = "user";
5658 for(i
= 0;i
< nb_net_clients
; i
++) {
5659 if (net_client_parse(net_clients
[i
]) < 0)
5663 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5664 net_set_boot_mask(net_boot
);
5668 /* init the bluetooth world */
5669 if (foreach_device_config(DEV_BT
, bt_parse
))
5672 /* init the memory */
5674 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5676 /* init the dynamic translator */
5677 cpu_exec_init_all(tb_size
* 1024 * 1024);
5681 /* we always create the cdrom drive, even if no disk is there */
5682 drive_add(NULL
, CDROM_ALIAS
);
5684 /* we always create at least one floppy */
5685 drive_add(NULL
, FD_ALIAS
, 0);
5687 /* we always create one sd slot, even if no card is in it */
5688 drive_add(NULL
, SD_ALIAS
);
5690 /* open the virtual block devices */
5692 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5693 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5696 vmstate_register(0, &vmstate_timers
,&timers_state
);
5697 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5699 /* Maintain compatibility with multiple stdio monitors */
5700 if (!strcmp(monitor_devices
[0],"stdio")) {
5701 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5702 const char *devname
= serial_devices
[i
];
5703 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5704 monitor_devices
[0] = NULL
;
5706 } else if (devname
&& !strcmp(devname
,"stdio")) {
5707 monitor_devices
[0] = NULL
;
5708 serial_devices
[i
] = "mon:stdio";
5714 if (nb_numa_nodes
> 0) {
5717 if (nb_numa_nodes
> smp_cpus
) {
5718 nb_numa_nodes
= smp_cpus
;
5721 /* If no memory size if given for any node, assume the default case
5722 * and distribute the available memory equally across all nodes
5724 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5725 if (node_mem
[i
] != 0)
5728 if (i
== nb_numa_nodes
) {
5729 uint64_t usedmem
= 0;
5731 /* On Linux, the each node's border has to be 8MB aligned,
5732 * the final node gets the rest.
5734 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5735 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5736 usedmem
+= node_mem
[i
];
5738 node_mem
[i
] = ram_size
- usedmem
;
5741 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5742 if (node_cpumask
[i
] != 0)
5745 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5746 * must cope with this anyway, because there are BIOSes out there in
5747 * real machines which also use this scheme.
5749 if (i
== nb_numa_nodes
) {
5750 for (i
= 0; i
< smp_cpus
; i
++) {
5751 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5756 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5757 const char *devname
= monitor_devices
[i
];
5758 if (devname
&& strcmp(devname
, "none")) {
5761 snprintf(label
, sizeof(label
), "monitor");
5763 snprintf(label
, sizeof(label
), "monitor%d", i
);
5765 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5766 if (!monitor_hds
[i
]) {
5767 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5774 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5775 const char *devname
= serial_devices
[i
];
5776 if (devname
&& strcmp(devname
, "none")) {
5778 snprintf(label
, sizeof(label
), "serial%d", i
);
5779 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5780 if (!serial_hds
[i
]) {
5781 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
5782 devname
, strerror(errno
));
5788 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5789 const char *devname
= parallel_devices
[i
];
5790 if (devname
&& strcmp(devname
, "none")) {
5792 snprintf(label
, sizeof(label
), "parallel%d", i
);
5793 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5794 if (!parallel_hds
[i
]) {
5795 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5796 devname
, strerror(errno
));
5802 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5803 const char *devname
= virtio_consoles
[i
];
5804 if (devname
&& strcmp(devname
, "none")) {
5806 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5807 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5808 if (!virtcon_hds
[i
]) {
5809 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5810 devname
, strerror(errno
));
5816 module_call_init(MODULE_INIT_DEVICE
);
5819 i
= select_watchdog(watchdog
);
5821 exit (i
== 1 ? 1 : 0);
5824 if (machine
->compat_props
) {
5825 qdev_prop_register_compat(machine
->compat_props
);
5827 machine
->init(ram_size
, boot_devices
,
5828 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5832 /* must be after terminal init, SDL library changes signal handlers */
5836 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5837 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5838 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5844 current_machine
= machine
;
5846 /* init USB devices */
5848 foreach_device_config(DEV_USB
, usb_parse
);
5851 /* init generic devices */
5852 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5856 dumb_display_init();
5857 /* just use the first displaystate for the moment */
5860 if (display_type
== DT_DEFAULT
) {
5861 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5862 display_type
= DT_SDL
;
5864 display_type
= DT_VNC
;
5865 vnc_display
= "localhost:0,to=99";
5871 switch (display_type
) {
5874 #if defined(CONFIG_CURSES)
5876 curses_display_init(ds
, full_screen
);
5879 #if defined(CONFIG_SDL)
5881 sdl_display_init(ds
, full_screen
, no_frame
);
5883 #elif defined(CONFIG_COCOA)
5885 cocoa_display_init(ds
, full_screen
);
5889 vnc_display_init(ds
);
5890 if (vnc_display_open(ds
, vnc_display
) < 0)
5893 if (show_vnc_port
) {
5894 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5902 dcl
= ds
->listeners
;
5903 while (dcl
!= NULL
) {
5904 if (dcl
->dpy_refresh
!= NULL
) {
5905 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5906 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5911 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5912 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5913 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5916 text_consoles_set_display(display_state
);
5917 qemu_chr_initial_reset();
5919 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5920 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5921 monitor_init(monitor_hds
[i
],
5922 MONITOR_USE_READLINE
|
5923 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5927 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5928 const char *devname
= serial_devices
[i
];
5929 if (devname
&& strcmp(devname
, "none")) {
5930 if (strstart(devname
, "vc", 0))
5931 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5935 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5936 const char *devname
= parallel_devices
[i
];
5937 if (devname
&& strcmp(devname
, "none")) {
5938 if (strstart(devname
, "vc", 0))
5939 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5943 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5944 const char *devname
= virtio_consoles
[i
];
5945 if (virtcon_hds
[i
] && devname
) {
5946 if (strstart(devname
, "vc", 0))
5947 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5951 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5952 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5957 qdev_machine_creation_done();
5962 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5968 qemu_start_incoming_migration(incoming
);
5969 } else if (autostart
) {
5979 len
= write(fds
[1], &status
, 1);
5980 if (len
== -1 && (errno
== EINTR
))
5987 TFR(fd
= open("/dev/null", O_RDWR
));
5993 pwd
= getpwnam(run_as
);
5995 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6001 if (chroot(chroot_dir
) < 0) {
6002 fprintf(stderr
, "chroot failed\n");
6009 if (setgid(pwd
->pw_gid
) < 0) {
6010 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6013 if (setuid(pwd
->pw_uid
) < 0) {
6014 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6017 if (setuid(0) != -1) {
6018 fprintf(stderr
, "Dropping privileges failed\n");