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"
152 #include "qemu-timer.h"
153 #include "qemu-char.h"
154 #include "cache-utils.h"
157 #include "audio/audio.h"
158 #include "migration.h"
161 #include "qemu-option.h"
162 #include "qemu-config.h"
166 #include "exec-all.h"
168 #include "qemu_socket.h"
170 #include "slirp/libslirp.h"
172 #include "qemu-queue.h"
175 //#define DEBUG_SLIRP
177 #define DEFAULT_RAM_SIZE 128
179 /* Maximum number of monitor devices */
180 #define MAX_MONITOR_DEVICES 10
182 static const char *data_dir
;
183 const char *bios_name
= NULL
;
184 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
185 to store the VM snapshots */
186 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
187 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
188 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
189 static DisplayState
*display_state
;
190 DisplayType display_type
= DT_DEFAULT
;
191 const char* keyboard_layout
= NULL
;
194 NICInfo nd_table
[MAX_NICS
];
197 static int rtc_utc
= 1;
198 static int rtc_date_offset
= -1; /* -1 means no change */
199 QEMUClock
*rtc_clock
;
200 int vga_interface_type
= VGA_CIRRUS
;
202 int graphic_width
= 1024;
203 int graphic_height
= 768;
204 int graphic_depth
= 8;
206 int graphic_width
= 800;
207 int graphic_height
= 600;
208 int graphic_depth
= 15;
210 static int full_screen
= 0;
212 static int no_frame
= 0;
215 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
216 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
217 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
219 int win2k_install_hack
= 0;
228 const char *vnc_display
;
229 int acpi_enabled
= 1;
235 int graphic_rotate
= 0;
236 uint8_t irq0override
= 1;
240 const char *watchdog
;
241 const char *option_rom
[MAX_OPTION_ROMS
];
243 int semihosting_enabled
= 0;
247 const char *qemu_name
;
249 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
250 unsigned int nb_prom_envs
= 0;
251 const char *prom_envs
[MAX_PROM_ENVS
];
256 uint64_t node_mem
[MAX_NODES
];
257 uint64_t node_cpumask
[MAX_NODES
];
259 static CPUState
*cur_cpu
;
260 static CPUState
*next_cpu
;
261 static int timer_alarm_pending
= 1;
262 /* Conversion factor from emulated instructions to virtual clock ticks. */
263 static int icount_time_shift
;
264 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
265 #define MAX_ICOUNT_SHIFT 10
266 /* Compensate for varying guest execution speed. */
267 static int64_t qemu_icount_bias
;
268 static QEMUTimer
*icount_rt_timer
;
269 static QEMUTimer
*icount_vm_timer
;
270 static QEMUTimer
*nographic_timer
;
272 uint8_t qemu_uuid
[16];
274 static QEMUBootSetHandler
*boot_set_handler
;
275 static void *boot_set_opaque
;
277 /***********************************************************/
278 /* x86 ISA bus support */
280 target_phys_addr_t isa_mem_base
= 0;
283 /***********************************************************/
284 void hw_error(const char *fmt
, ...)
290 fprintf(stderr
, "qemu: hardware error: ");
291 vfprintf(stderr
, fmt
, ap
);
292 fprintf(stderr
, "\n");
293 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
294 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
296 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
298 cpu_dump_state(env
, stderr
, fprintf
, 0);
305 static void set_proc_name(const char *s
)
307 #if defined(__linux__) && defined(PR_SET_NAME)
311 name
[sizeof(name
) - 1] = 0;
312 strncpy(name
, s
, sizeof(name
));
313 /* Could rewrite argv[0] too, but that's a bit more complicated.
314 This simple way is enough for `top'. */
315 prctl(PR_SET_NAME
, name
);
322 static QEMUBalloonEvent
*qemu_balloon_event
;
323 void *qemu_balloon_event_opaque
;
325 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
327 qemu_balloon_event
= func
;
328 qemu_balloon_event_opaque
= opaque
;
331 void qemu_balloon(ram_addr_t target
)
333 if (qemu_balloon_event
)
334 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
337 ram_addr_t
qemu_balloon_status(void)
339 if (qemu_balloon_event
)
340 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
344 /***********************************************************/
347 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
348 static void *qemu_put_kbd_event_opaque
;
349 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
350 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
352 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
354 qemu_put_kbd_event_opaque
= opaque
;
355 qemu_put_kbd_event
= func
;
358 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
359 void *opaque
, int absolute
,
362 QEMUPutMouseEntry
*s
, *cursor
;
364 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
366 s
->qemu_put_mouse_event
= func
;
367 s
->qemu_put_mouse_event_opaque
= opaque
;
368 s
->qemu_put_mouse_event_absolute
= absolute
;
369 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
372 if (!qemu_put_mouse_event_head
) {
373 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
377 cursor
= qemu_put_mouse_event_head
;
378 while (cursor
->next
!= NULL
)
379 cursor
= cursor
->next
;
382 qemu_put_mouse_event_current
= s
;
387 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
389 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
391 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
394 cursor
= qemu_put_mouse_event_head
;
395 while (cursor
!= NULL
&& cursor
!= entry
) {
397 cursor
= cursor
->next
;
400 if (cursor
== NULL
) // does not exist or list empty
402 else if (prev
== NULL
) { // entry is head
403 qemu_put_mouse_event_head
= cursor
->next
;
404 if (qemu_put_mouse_event_current
== entry
)
405 qemu_put_mouse_event_current
= cursor
->next
;
406 qemu_free(entry
->qemu_put_mouse_event_name
);
411 prev
->next
= entry
->next
;
413 if (qemu_put_mouse_event_current
== entry
)
414 qemu_put_mouse_event_current
= prev
;
416 qemu_free(entry
->qemu_put_mouse_event_name
);
420 void kbd_put_keycode(int keycode
)
422 if (qemu_put_kbd_event
) {
423 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
427 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
429 QEMUPutMouseEvent
*mouse_event
;
430 void *mouse_event_opaque
;
433 if (!qemu_put_mouse_event_current
) {
438 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
440 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
443 if (graphic_rotate
) {
444 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
447 width
= graphic_width
- 1;
448 mouse_event(mouse_event_opaque
,
449 width
- dy
, dx
, dz
, buttons_state
);
451 mouse_event(mouse_event_opaque
,
452 dx
, dy
, dz
, buttons_state
);
456 int kbd_mouse_is_absolute(void)
458 if (!qemu_put_mouse_event_current
)
461 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
464 void do_info_mice(Monitor
*mon
)
466 QEMUPutMouseEntry
*cursor
;
469 if (!qemu_put_mouse_event_head
) {
470 monitor_printf(mon
, "No mouse devices connected\n");
474 monitor_printf(mon
, "Mouse devices available:\n");
475 cursor
= qemu_put_mouse_event_head
;
476 while (cursor
!= NULL
) {
477 monitor_printf(mon
, "%c Mouse #%d: %s\n",
478 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
479 index
, cursor
->qemu_put_mouse_event_name
);
481 cursor
= cursor
->next
;
485 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
487 QEMUPutMouseEntry
*cursor
;
489 int index
= qdict_get_int(qdict
, "index");
491 if (!qemu_put_mouse_event_head
) {
492 monitor_printf(mon
, "No mouse devices connected\n");
496 cursor
= qemu_put_mouse_event_head
;
497 while (cursor
!= NULL
&& index
!= i
) {
499 cursor
= cursor
->next
;
503 qemu_put_mouse_event_current
= cursor
;
505 monitor_printf(mon
, "Mouse at given index not found\n");
508 /* compute with 96 bit intermediate result: (a*b)/c */
509 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
514 #ifdef HOST_WORDS_BIGENDIAN
524 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
525 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
528 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
532 /***********************************************************/
533 /* real time host monotonic timer */
535 static int64_t get_clock_realtime(void)
539 gettimeofday(&tv
, NULL
);
540 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
545 static int64_t clock_freq
;
547 static void init_get_clock(void)
551 ret
= QueryPerformanceFrequency(&freq
);
553 fprintf(stderr
, "Could not calibrate ticks\n");
556 clock_freq
= freq
.QuadPart
;
559 static int64_t get_clock(void)
562 QueryPerformanceCounter(&ti
);
563 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
568 static int use_rt_clock
;
570 static void init_get_clock(void)
573 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
574 || defined(__DragonFly__)
577 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
584 static int64_t get_clock(void)
586 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
587 || defined(__DragonFly__)
590 clock_gettime(CLOCK_MONOTONIC
, &ts
);
591 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
595 /* XXX: using gettimeofday leads to problems if the date
596 changes, so it should be avoided. */
597 return get_clock_realtime();
602 /* Return the virtual CPU time, based on the instruction counter. */
603 static int64_t cpu_get_icount(void)
606 CPUState
*env
= cpu_single_env
;;
607 icount
= qemu_icount
;
610 fprintf(stderr
, "Bad clock read\n");
611 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
613 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
616 /***********************************************************/
617 /* guest cycle counter */
619 typedef struct TimersState
{
620 int64_t cpu_ticks_prev
;
621 int64_t cpu_ticks_offset
;
622 int64_t cpu_clock_offset
;
623 int32_t cpu_ticks_enabled
;
627 TimersState timers_state
;
629 /* return the host CPU cycle counter and handle stop/restart */
630 int64_t cpu_get_ticks(void)
633 return cpu_get_icount();
635 if (!timers_state
.cpu_ticks_enabled
) {
636 return timers_state
.cpu_ticks_offset
;
639 ticks
= cpu_get_real_ticks();
640 if (timers_state
.cpu_ticks_prev
> ticks
) {
641 /* Note: non increasing ticks may happen if the host uses
643 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
645 timers_state
.cpu_ticks_prev
= ticks
;
646 return ticks
+ timers_state
.cpu_ticks_offset
;
650 /* return the host CPU monotonic timer and handle stop/restart */
651 static int64_t cpu_get_clock(void)
654 if (!timers_state
.cpu_ticks_enabled
) {
655 return timers_state
.cpu_clock_offset
;
658 return ti
+ timers_state
.cpu_clock_offset
;
662 /* enable cpu_get_ticks() */
663 void cpu_enable_ticks(void)
665 if (!timers_state
.cpu_ticks_enabled
) {
666 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
667 timers_state
.cpu_clock_offset
-= get_clock();
668 timers_state
.cpu_ticks_enabled
= 1;
672 /* disable cpu_get_ticks() : the clock is stopped. You must not call
673 cpu_get_ticks() after that. */
674 void cpu_disable_ticks(void)
676 if (timers_state
.cpu_ticks_enabled
) {
677 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
678 timers_state
.cpu_clock_offset
= cpu_get_clock();
679 timers_state
.cpu_ticks_enabled
= 0;
683 /***********************************************************/
686 #define QEMU_CLOCK_REALTIME 0
687 #define QEMU_CLOCK_VIRTUAL 1
688 #define QEMU_CLOCK_HOST 2
692 /* XXX: add frequency */
700 struct QEMUTimer
*next
;
703 struct qemu_alarm_timer
{
707 int (*start
)(struct qemu_alarm_timer
*t
);
708 void (*stop
)(struct qemu_alarm_timer
*t
);
709 void (*rearm
)(struct qemu_alarm_timer
*t
);
713 #define ALARM_FLAG_DYNTICKS 0x1
714 #define ALARM_FLAG_EXPIRED 0x2
716 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
718 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
721 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
723 if (!alarm_has_dynticks(t
))
729 /* TODO: MIN_TIMER_REARM_US should be optimized */
730 #define MIN_TIMER_REARM_US 250
732 static struct qemu_alarm_timer
*alarm_timer
;
736 struct qemu_alarm_win32
{
739 } alarm_win32_data
= {0, -1};
741 static int win32_start_timer(struct qemu_alarm_timer
*t
);
742 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
743 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
747 static int unix_start_timer(struct qemu_alarm_timer
*t
);
748 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
752 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
753 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
754 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
756 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
757 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
759 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
760 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
762 #endif /* __linux__ */
766 /* Correlation between real and virtual time is always going to be
767 fairly approximate, so ignore small variation.
768 When the guest is idle real and virtual time will be aligned in
770 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
772 static void icount_adjust(void)
777 static int64_t last_delta
;
778 /* If the VM is not running, then do nothing. */
782 cur_time
= cpu_get_clock();
783 cur_icount
= qemu_get_clock(vm_clock
);
784 delta
= cur_icount
- cur_time
;
785 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
787 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
788 && icount_time_shift
> 0) {
789 /* The guest is getting too far ahead. Slow time down. */
793 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
794 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
795 /* The guest is getting too far behind. Speed time up. */
799 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
802 static void icount_adjust_rt(void * opaque
)
804 qemu_mod_timer(icount_rt_timer
,
805 qemu_get_clock(rt_clock
) + 1000);
809 static void icount_adjust_vm(void * opaque
)
811 qemu_mod_timer(icount_vm_timer
,
812 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
816 static void init_icount_adjust(void)
818 /* Have both realtime and virtual time triggers for speed adjustment.
819 The realtime trigger catches emulated time passing too slowly,
820 the virtual time trigger catches emulated time passing too fast.
821 Realtime triggers occur even when idle, so use them less frequently
823 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
824 qemu_mod_timer(icount_rt_timer
,
825 qemu_get_clock(rt_clock
) + 1000);
826 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
827 qemu_mod_timer(icount_vm_timer
,
828 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
831 static struct qemu_alarm_timer alarm_timers
[] = {
834 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
835 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
836 /* HPET - if available - is preferred */
837 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
838 /* ...otherwise try RTC */
839 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
841 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
843 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
844 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
845 {"win32", 0, win32_start_timer
,
846 win32_stop_timer
, NULL
, &alarm_win32_data
},
851 static void show_available_alarms(void)
855 printf("Available alarm timers, in order of precedence:\n");
856 for (i
= 0; alarm_timers
[i
].name
; i
++)
857 printf("%s\n", alarm_timers
[i
].name
);
860 static void configure_alarms(char const *opt
)
864 int count
= ARRAY_SIZE(alarm_timers
) - 1;
867 struct qemu_alarm_timer tmp
;
869 if (!strcmp(opt
, "?")) {
870 show_available_alarms();
874 arg
= qemu_strdup(opt
);
876 /* Reorder the array */
877 name
= strtok(arg
, ",");
879 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
880 if (!strcmp(alarm_timers
[i
].name
, name
))
885 fprintf(stderr
, "Unknown clock %s\n", name
);
894 tmp
= alarm_timers
[i
];
895 alarm_timers
[i
] = alarm_timers
[cur
];
896 alarm_timers
[cur
] = tmp
;
900 name
= strtok(NULL
, ",");
906 /* Disable remaining timers */
907 for (i
= cur
; i
< count
; i
++)
908 alarm_timers
[i
].name
= NULL
;
910 show_available_alarms();
915 #define QEMU_NUM_CLOCKS 3
919 QEMUClock
*host_clock
;
921 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
923 static QEMUClock
*qemu_new_clock(int type
)
926 clock
= qemu_mallocz(sizeof(QEMUClock
));
931 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
935 ts
= qemu_mallocz(sizeof(QEMUTimer
));
942 void qemu_free_timer(QEMUTimer
*ts
)
947 /* stop a timer, but do not dealloc it */
948 void qemu_del_timer(QEMUTimer
*ts
)
952 /* NOTE: this code must be signal safe because
953 qemu_timer_expired() can be called from a signal. */
954 pt
= &active_timers
[ts
->clock
->type
];
967 /* modify the current timer so that it will be fired when current_time
968 >= expire_time. The corresponding callback will be called. */
969 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
975 /* add the timer in the sorted list */
976 /* NOTE: this code must be signal safe because
977 qemu_timer_expired() can be called from a signal. */
978 pt
= &active_timers
[ts
->clock
->type
];
983 if (t
->expire_time
> expire_time
)
987 ts
->expire_time
= expire_time
;
991 /* Rearm if necessary */
992 if (pt
== &active_timers
[ts
->clock
->type
]) {
993 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
994 qemu_rearm_alarm_timer(alarm_timer
);
996 /* Interrupt execution to force deadline recalculation. */
1002 int qemu_timer_pending(QEMUTimer
*ts
)
1005 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1012 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1016 return (timer_head
->expire_time
<= current_time
);
1019 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1025 if (!ts
|| ts
->expire_time
> current_time
)
1027 /* remove timer from the list before calling the callback */
1028 *ptimer_head
= ts
->next
;
1031 /* run the callback (the timer list can be modified) */
1036 int64_t qemu_get_clock(QEMUClock
*clock
)
1038 switch(clock
->type
) {
1039 case QEMU_CLOCK_REALTIME
:
1040 return get_clock() / 1000000;
1042 case QEMU_CLOCK_VIRTUAL
:
1044 return cpu_get_icount();
1046 return cpu_get_clock();
1048 case QEMU_CLOCK_HOST
:
1049 return get_clock_realtime();
1053 static void init_clocks(void)
1056 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1057 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1058 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1060 rtc_clock
= host_clock
;
1064 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1066 uint64_t expire_time
;
1068 if (qemu_timer_pending(ts
)) {
1069 expire_time
= ts
->expire_time
;
1073 qemu_put_be64(f
, expire_time
);
1076 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1078 uint64_t expire_time
;
1080 expire_time
= qemu_get_be64(f
);
1081 if (expire_time
!= -1) {
1082 qemu_mod_timer(ts
, expire_time
);
1088 static const VMStateDescription vmstate_timers
= {
1091 .minimum_version_id
= 1,
1092 .minimum_version_id_old
= 1,
1093 .fields
= (VMStateField
[]) {
1094 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1095 VMSTATE_INT64(dummy
, TimersState
),
1096 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1097 VMSTATE_END_OF_LIST()
1101 static void qemu_event_increment(void);
1104 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1105 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1108 static void host_alarm_handler(int host_signum
)
1112 #define DISP_FREQ 1000
1114 static int64_t delta_min
= INT64_MAX
;
1115 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1117 ti
= qemu_get_clock(vm_clock
);
1118 if (last_clock
!= 0) {
1119 delta
= ti
- last_clock
;
1120 if (delta
< delta_min
)
1122 if (delta
> delta_max
)
1125 if (++count
== DISP_FREQ
) {
1126 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1127 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1128 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1129 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1130 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1132 delta_min
= INT64_MAX
;
1140 if (alarm_has_dynticks(alarm_timer
) ||
1142 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1143 qemu_get_clock(vm_clock
))) ||
1144 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1145 qemu_get_clock(rt_clock
)) ||
1146 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1147 qemu_get_clock(host_clock
))) {
1148 qemu_event_increment();
1149 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1151 #ifndef CONFIG_IOTHREAD
1153 /* stop the currently executing cpu because a timer occured */
1157 timer_alarm_pending
= 1;
1158 qemu_notify_event();
1162 static int64_t qemu_next_deadline(void)
1164 /* To avoid problems with overflow limit this to 2^32. */
1165 int64_t delta
= INT32_MAX
;
1167 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1168 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1169 qemu_get_clock(vm_clock
);
1171 if (active_timers
[QEMU_CLOCK_HOST
]) {
1172 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1173 qemu_get_clock(host_clock
);
1184 #if defined(__linux__)
1185 static uint64_t qemu_next_deadline_dyntick(void)
1193 delta
= (qemu_next_deadline() + 999) / 1000;
1195 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1196 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1197 qemu_get_clock(rt_clock
))*1000;
1198 if (rtdelta
< delta
)
1202 if (delta
< MIN_TIMER_REARM_US
)
1203 delta
= MIN_TIMER_REARM_US
;
1211 /* Sets a specific flag */
1212 static int fcntl_setfl(int fd
, int flag
)
1216 flags
= fcntl(fd
, F_GETFL
);
1220 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1226 #if defined(__linux__)
1228 #define RTC_FREQ 1024
1230 static void enable_sigio_timer(int fd
)
1232 struct sigaction act
;
1235 sigfillset(&act
.sa_mask
);
1237 act
.sa_handler
= host_alarm_handler
;
1239 sigaction(SIGIO
, &act
, NULL
);
1240 fcntl_setfl(fd
, O_ASYNC
);
1241 fcntl(fd
, F_SETOWN
, getpid());
1244 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1246 struct hpet_info info
;
1249 fd
= open("/dev/hpet", O_RDONLY
);
1254 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1256 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1257 "error, but for better emulation accuracy type:\n"
1258 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1262 /* Check capabilities */
1263 r
= ioctl(fd
, HPET_INFO
, &info
);
1267 /* Enable periodic mode */
1268 r
= ioctl(fd
, HPET_EPI
, 0);
1269 if (info
.hi_flags
&& (r
< 0))
1272 /* Enable interrupt */
1273 r
= ioctl(fd
, HPET_IE_ON
, 0);
1277 enable_sigio_timer(fd
);
1278 t
->priv
= (void *)(long)fd
;
1286 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1288 int fd
= (long)t
->priv
;
1293 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1296 unsigned long current_rtc_freq
= 0;
1298 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1301 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1302 if (current_rtc_freq
!= RTC_FREQ
&&
1303 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1304 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1305 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1306 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1309 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1315 enable_sigio_timer(rtc_fd
);
1317 t
->priv
= (void *)(long)rtc_fd
;
1322 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1324 int rtc_fd
= (long)t
->priv
;
1329 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1333 struct sigaction act
;
1335 sigfillset(&act
.sa_mask
);
1337 act
.sa_handler
= host_alarm_handler
;
1339 sigaction(SIGALRM
, &act
, NULL
);
1342 * Initialize ev struct to 0 to avoid valgrind complaining
1343 * about uninitialized data in timer_create call
1345 memset(&ev
, 0, sizeof(ev
));
1346 ev
.sigev_value
.sival_int
= 0;
1347 ev
.sigev_notify
= SIGEV_SIGNAL
;
1348 ev
.sigev_signo
= SIGALRM
;
1350 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1351 perror("timer_create");
1353 /* disable dynticks */
1354 fprintf(stderr
, "Dynamic Ticks disabled\n");
1359 t
->priv
= (void *)(long)host_timer
;
1364 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1366 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1368 timer_delete(host_timer
);
1371 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1373 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1374 struct itimerspec timeout
;
1375 int64_t nearest_delta_us
= INT64_MAX
;
1378 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1379 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1380 !active_timers
[QEMU_CLOCK_HOST
])
1383 nearest_delta_us
= qemu_next_deadline_dyntick();
1385 /* check whether a timer is already running */
1386 if (timer_gettime(host_timer
, &timeout
)) {
1388 fprintf(stderr
, "Internal timer error: aborting\n");
1391 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1392 if (current_us
&& current_us
<= nearest_delta_us
)
1395 timeout
.it_interval
.tv_sec
= 0;
1396 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1397 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1398 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1399 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1401 fprintf(stderr
, "Internal timer error: aborting\n");
1406 #endif /* defined(__linux__) */
1408 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1410 struct sigaction act
;
1411 struct itimerval itv
;
1415 sigfillset(&act
.sa_mask
);
1417 act
.sa_handler
= host_alarm_handler
;
1419 sigaction(SIGALRM
, &act
, NULL
);
1421 itv
.it_interval
.tv_sec
= 0;
1422 /* for i386 kernel 2.6 to get 1 ms */
1423 itv
.it_interval
.tv_usec
= 999;
1424 itv
.it_value
.tv_sec
= 0;
1425 itv
.it_value
.tv_usec
= 10 * 1000;
1427 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1434 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1436 struct itimerval itv
;
1438 memset(&itv
, 0, sizeof(itv
));
1439 setitimer(ITIMER_REAL
, &itv
, NULL
);
1442 #endif /* !defined(_WIN32) */
1447 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1450 struct qemu_alarm_win32
*data
= t
->priv
;
1453 memset(&tc
, 0, sizeof(tc
));
1454 timeGetDevCaps(&tc
, sizeof(tc
));
1456 if (data
->period
< tc
.wPeriodMin
)
1457 data
->period
= tc
.wPeriodMin
;
1459 timeBeginPeriod(data
->period
);
1461 flags
= TIME_CALLBACK_FUNCTION
;
1462 if (alarm_has_dynticks(t
))
1463 flags
|= TIME_ONESHOT
;
1465 flags
|= TIME_PERIODIC
;
1467 data
->timerId
= timeSetEvent(1, // interval (ms)
1468 data
->period
, // resolution
1469 host_alarm_handler
, // function
1470 (DWORD
)t
, // parameter
1473 if (!data
->timerId
) {
1474 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1476 timeEndPeriod(data
->period
);
1483 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1485 struct qemu_alarm_win32
*data
= t
->priv
;
1487 timeKillEvent(data
->timerId
);
1488 timeEndPeriod(data
->period
);
1491 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1493 struct qemu_alarm_win32
*data
= t
->priv
;
1495 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1496 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1497 !active_timers
[QEMU_CLOCK_HOST
])
1500 timeKillEvent(data
->timerId
);
1502 data
->timerId
= timeSetEvent(1,
1506 TIME_ONESHOT
| TIME_PERIODIC
);
1508 if (!data
->timerId
) {
1509 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1512 timeEndPeriod(data
->period
);
1519 static int init_timer_alarm(void)
1521 struct qemu_alarm_timer
*t
= NULL
;
1524 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1525 t
= &alarm_timers
[i
];
1545 static void quit_timers(void)
1547 alarm_timer
->stop(alarm_timer
);
1551 /***********************************************************/
1552 /* host time/date access */
1553 void qemu_get_timedate(struct tm
*tm
, int offset
)
1560 if (rtc_date_offset
== -1) {
1564 ret
= localtime(&ti
);
1566 ti
-= rtc_date_offset
;
1570 memcpy(tm
, ret
, sizeof(struct tm
));
1573 int qemu_timedate_diff(struct tm
*tm
)
1577 if (rtc_date_offset
== -1)
1579 seconds
= mktimegm(tm
);
1581 seconds
= mktime(tm
);
1583 seconds
= mktimegm(tm
) + rtc_date_offset
;
1585 return seconds
- time(NULL
);
1588 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1590 time_t rtc_start_date
;
1593 if (!strcmp(startdate
, "now") && legacy
) {
1594 rtc_date_offset
= -1;
1596 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1604 } else if (sscanf(startdate
, "%d-%d-%d",
1607 &tm
.tm_mday
) == 3) {
1616 rtc_start_date
= mktimegm(&tm
);
1617 if (rtc_start_date
== -1) {
1619 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1620 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1623 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1627 static void configure_rtc(QemuOpts
*opts
)
1631 value
= qemu_opt_get(opts
, "base");
1633 if (!strcmp(value
, "utc")) {
1635 } else if (!strcmp(value
, "localtime")) {
1638 configure_rtc_date_offset(value
, 0);
1641 value
= qemu_opt_get(opts
, "clock");
1643 if (!strcmp(value
, "host")) {
1644 rtc_clock
= host_clock
;
1645 } else if (!strcmp(value
, "vm")) {
1646 rtc_clock
= vm_clock
;
1648 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1652 #ifdef CONFIG_TARGET_I386
1653 value
= qemu_opt_get(opts
, "driftfix");
1655 if (!strcmp(buf
, "slew")) {
1657 } else if (!strcmp(buf
, "none")) {
1660 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1668 static void socket_cleanup(void)
1673 static int socket_init(void)
1678 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1680 err
= WSAGetLastError();
1681 fprintf(stderr
, "WSAStartup: %d\n", err
);
1684 atexit(socket_cleanup
);
1689 /***********************************************************/
1690 /* Bluetooth support */
1693 static struct HCIInfo
*hci_table
[MAX_NICS
];
1695 static struct bt_vlan_s
{
1696 struct bt_scatternet_s net
;
1698 struct bt_vlan_s
*next
;
1701 /* find or alloc a new bluetooth "VLAN" */
1702 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1704 struct bt_vlan_s
**pvlan
, *vlan
;
1705 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1709 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1711 pvlan
= &first_bt_vlan
;
1712 while (*pvlan
!= NULL
)
1713 pvlan
= &(*pvlan
)->next
;
1718 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1722 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1727 static struct HCIInfo null_hci
= {
1728 .cmd_send
= null_hci_send
,
1729 .sco_send
= null_hci_send
,
1730 .acl_send
= null_hci_send
,
1731 .bdaddr_set
= null_hci_addr_set
,
1734 struct HCIInfo
*qemu_next_hci(void)
1736 if (cur_hci
== nb_hcis
)
1739 return hci_table
[cur_hci
++];
1742 static struct HCIInfo
*hci_init(const char *str
)
1745 struct bt_scatternet_s
*vlan
= 0;
1747 if (!strcmp(str
, "null"))
1750 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1752 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1753 else if (!strncmp(str
, "hci", 3)) {
1756 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1757 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1762 vlan
= qemu_find_bt_vlan(0);
1764 return bt_new_hci(vlan
);
1767 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1772 static int bt_hci_parse(const char *str
)
1774 struct HCIInfo
*hci
;
1777 if (nb_hcis
>= MAX_NICS
) {
1778 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1782 hci
= hci_init(str
);
1791 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1792 hci
->bdaddr_set(hci
, bdaddr
.b
);
1794 hci_table
[nb_hcis
++] = hci
;
1799 static void bt_vhci_add(int vlan_id
)
1801 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1804 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1805 "an empty scatternet %i\n", vlan_id
);
1807 bt_vhci_init(bt_new_hci(vlan
));
1810 static struct bt_device_s
*bt_device_add(const char *opt
)
1812 struct bt_scatternet_s
*vlan
;
1814 char *endp
= strstr(opt
, ",vlan=");
1815 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1818 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1821 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1823 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1828 vlan
= qemu_find_bt_vlan(vlan_id
);
1831 fprintf(stderr
, "qemu: warning: adding a slave device to "
1832 "an empty scatternet %i\n", vlan_id
);
1834 if (!strcmp(devname
, "keyboard"))
1835 return bt_keyboard_init(vlan
);
1837 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1841 static int bt_parse(const char *opt
)
1843 const char *endp
, *p
;
1846 if (strstart(opt
, "hci", &endp
)) {
1847 if (!*endp
|| *endp
== ',') {
1849 if (!strstart(endp
, ",vlan=", 0))
1852 return bt_hci_parse(opt
);
1854 } else if (strstart(opt
, "vhci", &endp
)) {
1855 if (!*endp
|| *endp
== ',') {
1857 if (strstart(endp
, ",vlan=", &p
)) {
1858 vlan
= strtol(p
, (char **) &endp
, 0);
1860 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1864 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1873 } else if (strstart(opt
, "device:", &endp
))
1874 return !bt_device_add(endp
);
1876 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1880 /***********************************************************/
1881 /* QEMU Block devices */
1883 #define HD_ALIAS "index=%d,media=disk"
1884 #define CDROM_ALIAS "index=2,media=cdrom"
1885 #define FD_ALIAS "index=%d,if=floppy"
1886 #define PFLASH_ALIAS "if=pflash"
1887 #define MTD_ALIAS "if=mtd"
1888 #define SD_ALIAS "index=0,if=sd"
1890 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1897 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1900 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1902 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1903 __FUNCTION__
, optstr
);
1907 qemu_opt_set(opts
, "file", file
);
1911 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1915 /* seek interface, bus and unit */
1917 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1918 if (dinfo
->type
== type
&&
1919 dinfo
->bus
== bus
&&
1920 dinfo
->unit
== unit
)
1927 DriveInfo
*drive_get_by_id(const char *id
)
1931 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1932 if (strcmp(id
, dinfo
->id
))
1939 int drive_get_max_bus(BlockInterfaceType type
)
1945 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1946 if(dinfo
->type
== type
&&
1947 dinfo
->bus
> max_bus
)
1948 max_bus
= dinfo
->bus
;
1953 const char *drive_get_serial(BlockDriverState
*bdrv
)
1957 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1958 if (dinfo
->bdrv
== bdrv
)
1959 return dinfo
->serial
;
1965 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1969 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1970 if (dinfo
->bdrv
== bdrv
)
1971 return dinfo
->onerror
;
1974 return BLOCK_ERR_STOP_ENOSPC
;
1977 static void bdrv_format_print(void *opaque
, const char *name
)
1979 fprintf(stderr
, " %s", name
);
1982 void drive_uninit(DriveInfo
*dinfo
)
1984 qemu_opts_del(dinfo
->opts
);
1985 bdrv_delete(dinfo
->bdrv
);
1986 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1990 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1994 const char *file
= NULL
;
1997 const char *mediastr
= "";
1998 BlockInterfaceType type
;
1999 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2000 int bus_id
, unit_id
;
2001 int cyls
, heads
, secs
, translation
;
2002 BlockDriver
*drv
= NULL
;
2003 QEMUMachine
*machine
= opaque
;
2008 int bdrv_flags
, onerror
;
2009 const char *devaddr
;
2015 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2018 if (machine
&& machine
->use_scsi
) {
2020 max_devs
= MAX_SCSI_DEVS
;
2021 pstrcpy(devname
, sizeof(devname
), "scsi");
2024 max_devs
= MAX_IDE_DEVS
;
2025 pstrcpy(devname
, sizeof(devname
), "ide");
2029 /* extract parameters */
2030 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2031 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2032 index
= qemu_opt_get_number(opts
, "index", -1);
2034 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2035 heads
= qemu_opt_get_number(opts
, "heads", 0);
2036 secs
= qemu_opt_get_number(opts
, "secs", 0);
2038 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2040 file
= qemu_opt_get(opts
, "file");
2041 serial
= qemu_opt_get(opts
, "serial");
2043 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2044 pstrcpy(devname
, sizeof(devname
), buf
);
2045 if (!strcmp(buf
, "ide")) {
2047 max_devs
= MAX_IDE_DEVS
;
2048 } else if (!strcmp(buf
, "scsi")) {
2050 max_devs
= MAX_SCSI_DEVS
;
2051 } else if (!strcmp(buf
, "floppy")) {
2054 } else if (!strcmp(buf
, "pflash")) {
2057 } else if (!strcmp(buf
, "mtd")) {
2060 } else if (!strcmp(buf
, "sd")) {
2063 } else if (!strcmp(buf
, "virtio")) {
2066 } else if (!strcmp(buf
, "xen")) {
2069 } else if (!strcmp(buf
, "none")) {
2073 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2078 if (cyls
|| heads
|| secs
) {
2079 if (cyls
< 1 || cyls
> 16383) {
2080 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2083 if (heads
< 1 || heads
> 16) {
2084 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2087 if (secs
< 1 || secs
> 63) {
2088 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2093 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2096 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2100 if (!strcmp(buf
, "none"))
2101 translation
= BIOS_ATA_TRANSLATION_NONE
;
2102 else if (!strcmp(buf
, "lba"))
2103 translation
= BIOS_ATA_TRANSLATION_LBA
;
2104 else if (!strcmp(buf
, "auto"))
2105 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2107 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2112 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2113 if (!strcmp(buf
, "disk")) {
2115 } else if (!strcmp(buf
, "cdrom")) {
2116 if (cyls
|| secs
|| heads
) {
2118 "qemu: '%s' invalid physical CHS format\n", buf
);
2121 media
= MEDIA_CDROM
;
2123 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2128 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2129 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2131 else if (!strcmp(buf
, "writethrough"))
2133 else if (!strcmp(buf
, "writeback"))
2136 fprintf(stderr
, "qemu: invalid cache option\n");
2141 #ifdef CONFIG_LINUX_AIO
2142 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2143 if (!strcmp(buf
, "threads"))
2145 else if (!strcmp(buf
, "native"))
2148 fprintf(stderr
, "qemu: invalid aio option\n");
2154 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2155 if (strcmp(buf
, "?") == 0) {
2156 fprintf(stderr
, "qemu: Supported formats:");
2157 bdrv_iterate_format(bdrv_format_print
, NULL
);
2158 fprintf(stderr
, "\n");
2161 drv
= bdrv_find_format(buf
);
2163 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2168 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2169 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2170 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2171 fprintf(stderr
, "werror is no supported by this format\n");
2174 if (!strcmp(buf
, "ignore"))
2175 onerror
= BLOCK_ERR_IGNORE
;
2176 else if (!strcmp(buf
, "enospc"))
2177 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2178 else if (!strcmp(buf
, "stop"))
2179 onerror
= BLOCK_ERR_STOP_ANY
;
2180 else if (!strcmp(buf
, "report"))
2181 onerror
= BLOCK_ERR_REPORT
;
2183 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2188 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2189 if (type
!= IF_VIRTIO
) {
2190 fprintf(stderr
, "addr is not supported\n");
2195 /* compute bus and unit according index */
2198 if (bus_id
!= 0 || unit_id
!= -1) {
2200 "qemu: index cannot be used with bus and unit\n");
2208 unit_id
= index
% max_devs
;
2209 bus_id
= index
/ max_devs
;
2213 /* if user doesn't specify a unit_id,
2214 * try to find the first free
2217 if (unit_id
== -1) {
2219 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2221 if (max_devs
&& unit_id
>= max_devs
) {
2222 unit_id
-= max_devs
;
2230 if (max_devs
&& unit_id
>= max_devs
) {
2231 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2232 unit_id
, max_devs
- 1);
2237 * ignore multiple definitions
2240 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2247 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2248 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2249 dinfo
->id
= qemu_strdup(buf
);
2251 /* no id supplied -> create one */
2252 dinfo
->id
= qemu_mallocz(32);
2253 if (type
== IF_IDE
|| type
== IF_SCSI
)
2254 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2256 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2257 devname
, bus_id
, mediastr
, unit_id
);
2259 snprintf(dinfo
->id
, 32, "%s%s%i",
2260 devname
, mediastr
, unit_id
);
2262 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2263 dinfo
->devaddr
= devaddr
;
2265 dinfo
->bus
= bus_id
;
2266 dinfo
->unit
= unit_id
;
2267 dinfo
->onerror
= onerror
;
2270 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2271 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2281 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2282 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2286 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2291 /* FIXME: This isn't really a floppy, but it's a reasonable
2294 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2300 /* add virtio block device */
2301 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2302 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2303 qemu_opt_set(opts
, "drive", dinfo
->id
);
2305 qemu_opt_set(opts
, "addr", devaddr
);
2316 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2317 cache
= 2; /* always use write-back with snapshot */
2319 if (cache
== 0) /* no caching */
2320 bdrv_flags
|= BDRV_O_NOCACHE
;
2321 else if (cache
== 2) /* write-back */
2322 bdrv_flags
|= BDRV_O_CACHE_WB
;
2325 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2327 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2330 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2331 fprintf(stderr
, "qemu: could not open disk image %s\n",
2336 if (bdrv_key_required(dinfo
->bdrv
))
2342 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2344 QEMUMachine
*machine
= opaque
;
2345 int fatal_error
= 0;
2347 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2354 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2356 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2357 qemu_opt_set(opts
, "snapshot", "on");
2362 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2364 boot_set_handler
= func
;
2365 boot_set_opaque
= opaque
;
2368 int qemu_boot_set(const char *boot_devices
)
2370 if (!boot_set_handler
) {
2373 return boot_set_handler(boot_set_opaque
, boot_devices
);
2376 static int parse_bootdevices(char *devices
)
2378 /* We just do some generic consistency checks */
2382 for (p
= devices
; *p
!= '\0'; p
++) {
2383 /* Allowed boot devices are:
2384 * a-b: floppy disk drives
2385 * c-f: IDE disk drives
2386 * g-m: machine implementation dependant drives
2387 * n-p: network devices
2388 * It's up to each machine implementation to check if the given boot
2389 * devices match the actual hardware implementation and firmware
2392 if (*p
< 'a' || *p
> 'p') {
2393 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2396 if (bitmap
& (1 << (*p
- 'a'))) {
2397 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2400 bitmap
|= 1 << (*p
- 'a');
2405 static void restore_boot_devices(void *opaque
)
2407 char *standard_boot_devices
= opaque
;
2409 qemu_boot_set(standard_boot_devices
);
2411 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2412 qemu_free(standard_boot_devices
);
2415 static void numa_add(const char *optarg
)
2419 unsigned long long value
, endvalue
;
2422 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2423 if (!strcmp(option
, "node")) {
2424 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2425 nodenr
= nb_numa_nodes
;
2427 nodenr
= strtoull(option
, NULL
, 10);
2430 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2431 node_mem
[nodenr
] = 0;
2433 value
= strtoull(option
, &endptr
, 0);
2435 case 0: case 'M': case 'm':
2442 node_mem
[nodenr
] = value
;
2444 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2445 node_cpumask
[nodenr
] = 0;
2447 value
= strtoull(option
, &endptr
, 10);
2450 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2452 if (*endptr
== '-') {
2453 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2454 if (endvalue
>= 63) {
2457 "only 63 CPUs in NUMA mode supported.\n");
2459 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2464 node_cpumask
[nodenr
] = value
;
2471 static void smp_parse(const char *optarg
)
2473 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2477 smp
= strtoul(optarg
, &endptr
, 10);
2478 if (endptr
!= optarg
) {
2479 if (*endptr
== ',') {
2483 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2484 sockets
= strtoull(option
, NULL
, 10);
2485 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2486 cores
= strtoull(option
, NULL
, 10);
2487 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2488 threads
= strtoull(option
, NULL
, 10);
2489 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2490 max_cpus
= strtoull(option
, NULL
, 10);
2492 /* compute missing values, prefer sockets over cores over threads */
2493 if (smp
== 0 || sockets
== 0) {
2494 sockets
= sockets
> 0 ? sockets
: 1;
2495 cores
= cores
> 0 ? cores
: 1;
2496 threads
= threads
> 0 ? threads
: 1;
2498 smp
= cores
* threads
* sockets
;
2500 sockets
= smp
/ (cores
* threads
);
2504 threads
= threads
> 0 ? threads
: 1;
2505 cores
= smp
/ (sockets
* threads
);
2508 sockets
= smp
/ (cores
* threads
);
2510 threads
= smp
/ (cores
* sockets
);
2515 smp_cores
= cores
> 0 ? cores
: 1;
2516 smp_threads
= threads
> 0 ? threads
: 1;
2518 max_cpus
= smp_cpus
;
2521 /***********************************************************/
2524 static void usb_msd_password_cb(void *opaque
, int err
)
2526 USBDevice
*dev
= opaque
;
2529 usb_device_attach(dev
);
2531 dev
->info
->handle_destroy(dev
);
2540 .qdev
= "QEMU USB Mouse",
2543 .qdev
= "QEMU USB Tablet",
2546 .qdev
= "QEMU USB Keyboard",
2548 .name
= "wacom-tablet",
2549 .qdev
= "QEMU PenPartner Tablet",
2553 static int usb_device_add(const char *devname
, int is_hotplug
)
2556 USBBus
*bus
= usb_bus_find(-1 /* any */);
2557 USBDevice
*dev
= NULL
;
2563 /* simple devices which don't need extra care */
2564 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2565 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2567 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2571 /* the other ones */
2572 if (strstart(devname
, "host:", &p
)) {
2573 dev
= usb_host_device_open(p
);
2574 } else if (strstart(devname
, "disk:", &p
)) {
2575 BlockDriverState
*bs
;
2577 dev
= usb_msd_init(p
);
2580 bs
= usb_msd_get_bdrv(dev
);
2581 if (bdrv_key_required(bs
)) {
2584 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2589 } else if (strstart(devname
, "serial:", &p
)) {
2590 dev
= usb_serial_init(p
);
2591 #ifdef CONFIG_BRLAPI
2592 } else if (!strcmp(devname
, "braille")) {
2593 dev
= usb_baum_init();
2595 } else if (strstart(devname
, "net:", &p
)) {
2598 if (net_client_init(NULL
, "nic", p
) < 0)
2600 nd_table
[nic
].model
= "usb";
2601 dev
= usb_net_init(&nd_table
[nic
]);
2602 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2603 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2604 bt_new_hci(qemu_find_bt_vlan(0)));
2615 static int usb_device_del(const char *devname
)
2620 if (strstart(devname
, "host:", &p
))
2621 return usb_host_device_close(p
);
2626 p
= strchr(devname
, '.');
2629 bus_num
= strtoul(devname
, NULL
, 0);
2630 addr
= strtoul(p
+ 1, NULL
, 0);
2632 return usb_device_delete_addr(bus_num
, addr
);
2635 static int usb_parse(const char *cmdline
)
2637 return usb_device_add(cmdline
, 0);
2640 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2642 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2645 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2647 usb_device_del(qdict_get_str(qdict
, "devname"));
2650 /***********************************************************/
2651 /* PCMCIA/Cardbus */
2653 static struct pcmcia_socket_entry_s
{
2654 PCMCIASocket
*socket
;
2655 struct pcmcia_socket_entry_s
*next
;
2656 } *pcmcia_sockets
= 0;
2658 void pcmcia_socket_register(PCMCIASocket
*socket
)
2660 struct pcmcia_socket_entry_s
*entry
;
2662 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2663 entry
->socket
= socket
;
2664 entry
->next
= pcmcia_sockets
;
2665 pcmcia_sockets
= entry
;
2668 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2670 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2672 ptr
= &pcmcia_sockets
;
2673 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2674 if (entry
->socket
== socket
) {
2680 void pcmcia_info(Monitor
*mon
)
2682 struct pcmcia_socket_entry_s
*iter
;
2684 if (!pcmcia_sockets
)
2685 monitor_printf(mon
, "No PCMCIA sockets\n");
2687 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2688 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2689 iter
->socket
->attached
? iter
->socket
->card_string
:
2693 /***********************************************************/
2694 /* register display */
2696 struct DisplayAllocator default_allocator
= {
2697 defaultallocator_create_displaysurface
,
2698 defaultallocator_resize_displaysurface
,
2699 defaultallocator_free_displaysurface
2702 void register_displaystate(DisplayState
*ds
)
2712 DisplayState
*get_displaystate(void)
2714 return display_state
;
2717 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2719 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2720 return ds
->allocator
;
2725 static void dumb_display_init(void)
2727 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2728 ds
->allocator
= &default_allocator
;
2729 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2730 register_displaystate(ds
);
2733 /***********************************************************/
2736 typedef struct IOHandlerRecord
{
2738 IOCanRWHandler
*fd_read_poll
;
2740 IOHandler
*fd_write
;
2743 /* temporary data */
2745 struct IOHandlerRecord
*next
;
2748 static IOHandlerRecord
*first_io_handler
;
2750 /* XXX: fd_read_poll should be suppressed, but an API change is
2751 necessary in the character devices to suppress fd_can_read(). */
2752 int qemu_set_fd_handler2(int fd
,
2753 IOCanRWHandler
*fd_read_poll
,
2755 IOHandler
*fd_write
,
2758 IOHandlerRecord
**pioh
, *ioh
;
2760 if (!fd_read
&& !fd_write
) {
2761 pioh
= &first_io_handler
;
2766 if (ioh
->fd
== fd
) {
2773 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2777 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2778 ioh
->next
= first_io_handler
;
2779 first_io_handler
= ioh
;
2782 ioh
->fd_read_poll
= fd_read_poll
;
2783 ioh
->fd_read
= fd_read
;
2784 ioh
->fd_write
= fd_write
;
2785 ioh
->opaque
= opaque
;
2791 int qemu_set_fd_handler(int fd
,
2793 IOHandler
*fd_write
,
2796 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2800 /***********************************************************/
2801 /* Polling handling */
2803 typedef struct PollingEntry
{
2806 struct PollingEntry
*next
;
2809 static PollingEntry
*first_polling_entry
;
2811 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2813 PollingEntry
**ppe
, *pe
;
2814 pe
= qemu_mallocz(sizeof(PollingEntry
));
2816 pe
->opaque
= opaque
;
2817 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2822 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2824 PollingEntry
**ppe
, *pe
;
2825 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2827 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2835 /***********************************************************/
2836 /* Wait objects support */
2837 typedef struct WaitObjects
{
2839 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2840 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2841 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2844 static WaitObjects wait_objects
= {0};
2846 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2848 WaitObjects
*w
= &wait_objects
;
2850 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2852 w
->events
[w
->num
] = handle
;
2853 w
->func
[w
->num
] = func
;
2854 w
->opaque
[w
->num
] = opaque
;
2859 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2862 WaitObjects
*w
= &wait_objects
;
2865 for (i
= 0; i
< w
->num
; i
++) {
2866 if (w
->events
[i
] == handle
)
2869 w
->events
[i
] = w
->events
[i
+ 1];
2870 w
->func
[i
] = w
->func
[i
+ 1];
2871 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2879 /***********************************************************/
2880 /* ram save/restore */
2882 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2883 #define RAM_SAVE_FLAG_COMPRESS 0x02
2884 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2885 #define RAM_SAVE_FLAG_PAGE 0x08
2886 #define RAM_SAVE_FLAG_EOS 0x10
2888 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2890 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2891 uint32_t *array
= (uint32_t *)page
;
2894 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2895 if (array
[i
] != val
)
2902 static int ram_save_block(QEMUFile
*f
)
2904 static ram_addr_t current_addr
= 0;
2905 ram_addr_t saved_addr
= current_addr
;
2906 ram_addr_t addr
= 0;
2909 while (addr
< last_ram_offset
) {
2910 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2913 cpu_physical_memory_reset_dirty(current_addr
,
2914 current_addr
+ TARGET_PAGE_SIZE
,
2915 MIGRATION_DIRTY_FLAG
);
2917 p
= qemu_get_ram_ptr(current_addr
);
2919 if (is_dup_page(p
, *p
)) {
2920 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2921 qemu_put_byte(f
, *p
);
2923 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2924 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2930 addr
+= TARGET_PAGE_SIZE
;
2931 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2937 static uint64_t bytes_transferred
= 0;
2939 static ram_addr_t
ram_save_remaining(void)
2942 ram_addr_t count
= 0;
2944 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2945 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2952 uint64_t ram_bytes_remaining(void)
2954 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2957 uint64_t ram_bytes_transferred(void)
2959 return bytes_transferred
;
2962 uint64_t ram_bytes_total(void)
2964 return last_ram_offset
;
2967 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2970 uint64_t bytes_transferred_last
;
2972 uint64_t expected_time
= 0;
2974 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2975 qemu_file_set_error(f
);
2980 /* Make sure all dirty bits are set */
2981 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2982 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2983 cpu_physical_memory_set_dirty(addr
);
2986 /* Enable dirty memory tracking */
2987 cpu_physical_memory_set_dirty_tracking(1);
2989 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2992 bytes_transferred_last
= bytes_transferred
;
2993 bwidth
= get_clock();
2995 while (!qemu_file_rate_limit(f
)) {
2998 ret
= ram_save_block(f
);
2999 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3000 if (ret
== 0) /* no more blocks */
3004 bwidth
= get_clock() - bwidth
;
3005 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3007 /* if we haven't transferred anything this round, force expected_time to a
3008 * a very high value, but without crashing */
3012 /* try transferring iterative blocks of memory */
3016 /* flush all remaining blocks regardless of rate limiting */
3017 while (ram_save_block(f
) != 0) {
3018 bytes_transferred
+= TARGET_PAGE_SIZE
;
3020 cpu_physical_memory_set_dirty_tracking(0);
3023 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3025 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3027 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3030 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3035 if (version_id
!= 3)
3039 addr
= qemu_get_be64(f
);
3041 flags
= addr
& ~TARGET_PAGE_MASK
;
3042 addr
&= TARGET_PAGE_MASK
;
3044 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3045 if (addr
!= last_ram_offset
)
3049 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3050 uint8_t ch
= qemu_get_byte(f
);
3051 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3054 (!kvm_enabled() || kvm_has_sync_mmu())) {
3055 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3058 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3059 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3060 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3065 void qemu_service_io(void)
3067 qemu_notify_event();
3070 /***********************************************************/
3071 /* bottom halves (can be seen as timers which expire ASAP) */
3082 static QEMUBH
*first_bh
= NULL
;
3084 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3087 bh
= qemu_mallocz(sizeof(QEMUBH
));
3089 bh
->opaque
= opaque
;
3090 bh
->next
= first_bh
;
3095 int qemu_bh_poll(void)
3101 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3102 if (!bh
->deleted
&& bh
->scheduled
) {
3111 /* remove deleted bhs */
3125 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3133 void qemu_bh_schedule(QEMUBH
*bh
)
3139 /* stop the currently executing CPU to execute the BH ASAP */
3140 qemu_notify_event();
3143 void qemu_bh_cancel(QEMUBH
*bh
)
3148 void qemu_bh_delete(QEMUBH
*bh
)
3154 static void qemu_bh_update_timeout(int *timeout
)
3158 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3159 if (!bh
->deleted
&& bh
->scheduled
) {
3161 /* idle bottom halves will be polled at least
3163 *timeout
= MIN(10, *timeout
);
3165 /* non-idle bottom halves will be executed
3174 /***********************************************************/
3175 /* machine registration */
3177 static QEMUMachine
*first_machine
= NULL
;
3178 QEMUMachine
*current_machine
= NULL
;
3180 int qemu_register_machine(QEMUMachine
*m
)
3183 pm
= &first_machine
;
3191 static QEMUMachine
*find_machine(const char *name
)
3195 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3196 if (!strcmp(m
->name
, name
))
3198 if (m
->alias
&& !strcmp(m
->alias
, name
))
3204 static QEMUMachine
*find_default_machine(void)
3208 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3209 if (m
->is_default
) {
3216 /***********************************************************/
3217 /* main execution loop */
3219 static void gui_update(void *opaque
)
3221 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3222 DisplayState
*ds
= opaque
;
3223 DisplayChangeListener
*dcl
= ds
->listeners
;
3227 while (dcl
!= NULL
) {
3228 if (dcl
->gui_timer_interval
&&
3229 dcl
->gui_timer_interval
< interval
)
3230 interval
= dcl
->gui_timer_interval
;
3233 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3236 static void nographic_update(void *opaque
)
3238 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3240 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3243 struct vm_change_state_entry
{
3244 VMChangeStateHandler
*cb
;
3246 QLIST_ENTRY (vm_change_state_entry
) entries
;
3249 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3251 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3254 VMChangeStateEntry
*e
;
3256 e
= qemu_mallocz(sizeof (*e
));
3260 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3264 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3266 QLIST_REMOVE (e
, entries
);
3270 static void vm_state_notify(int running
, int reason
)
3272 VMChangeStateEntry
*e
;
3274 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3275 e
->cb(e
->opaque
, running
, reason
);
3279 static void resume_all_vcpus(void);
3280 static void pause_all_vcpus(void);
3287 vm_state_notify(1, 0);
3288 qemu_rearm_alarm_timer(alarm_timer
);
3293 /* reset/shutdown handler */
3295 typedef struct QEMUResetEntry
{
3296 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3297 QEMUResetHandler
*func
;
3301 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3302 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3303 static int reset_requested
;
3304 static int shutdown_requested
;
3305 static int powerdown_requested
;
3306 static int debug_requested
;
3307 static int vmstop_requested
;
3309 int qemu_shutdown_requested(void)
3311 int r
= shutdown_requested
;
3312 shutdown_requested
= 0;
3316 int qemu_reset_requested(void)
3318 int r
= reset_requested
;
3319 reset_requested
= 0;
3323 int qemu_powerdown_requested(void)
3325 int r
= powerdown_requested
;
3326 powerdown_requested
= 0;
3330 static int qemu_debug_requested(void)
3332 int r
= debug_requested
;
3333 debug_requested
= 0;
3337 static int qemu_vmstop_requested(void)
3339 int r
= vmstop_requested
;
3340 vmstop_requested
= 0;
3344 static void do_vm_stop(int reason
)
3347 cpu_disable_ticks();
3350 vm_state_notify(0, reason
);
3354 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3356 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3359 re
->opaque
= opaque
;
3360 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3363 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3367 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3368 if (re
->func
== func
&& re
->opaque
== opaque
) {
3369 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3376 void qemu_system_reset(void)
3378 QEMUResetEntry
*re
, *nre
;
3380 /* reset all devices */
3381 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3382 re
->func(re
->opaque
);
3386 void qemu_system_reset_request(void)
3389 shutdown_requested
= 1;
3391 reset_requested
= 1;
3393 qemu_notify_event();
3396 void qemu_system_shutdown_request(void)
3398 shutdown_requested
= 1;
3399 qemu_notify_event();
3402 void qemu_system_powerdown_request(void)
3404 powerdown_requested
= 1;
3405 qemu_notify_event();
3408 #ifdef CONFIG_IOTHREAD
3409 static void qemu_system_vmstop_request(int reason
)
3411 vmstop_requested
= reason
;
3412 qemu_notify_event();
3417 static int io_thread_fd
= -1;
3419 static void qemu_event_increment(void)
3421 static const char byte
= 0;
3423 if (io_thread_fd
== -1)
3426 write(io_thread_fd
, &byte
, sizeof(byte
));
3429 static void qemu_event_read(void *opaque
)
3431 int fd
= (unsigned long)opaque
;
3434 /* Drain the notify pipe */
3437 len
= read(fd
, buffer
, sizeof(buffer
));
3438 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3441 static int qemu_event_init(void)
3450 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3454 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3458 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3459 (void *)(unsigned long)fds
[0]);
3461 io_thread_fd
= fds
[1];
3470 HANDLE qemu_event_handle
;
3472 static void dummy_event_handler(void *opaque
)
3476 static int qemu_event_init(void)
3478 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3479 if (!qemu_event_handle
) {
3480 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3483 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3487 static void qemu_event_increment(void)
3489 if (!SetEvent(qemu_event_handle
)) {
3490 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3497 static int cpu_can_run(CPUState
*env
)
3506 #ifndef CONFIG_IOTHREAD
3507 static int qemu_init_main_loop(void)
3509 return qemu_event_init();
3512 void qemu_init_vcpu(void *_env
)
3514 CPUState
*env
= _env
;
3518 env
->nr_cores
= smp_cores
;
3519 env
->nr_threads
= smp_threads
;
3523 int qemu_cpu_self(void *env
)
3528 static void resume_all_vcpus(void)
3532 static void pause_all_vcpus(void)
3536 void qemu_cpu_kick(void *env
)
3541 void qemu_notify_event(void)
3543 CPUState
*env
= cpu_single_env
;
3550 #define qemu_mutex_lock_iothread() do { } while (0)
3551 #define qemu_mutex_unlock_iothread() do { } while (0)
3553 void vm_stop(int reason
)
3558 #else /* CONFIG_IOTHREAD */
3560 #include "qemu-thread.h"
3562 QemuMutex qemu_global_mutex
;
3563 static QemuMutex qemu_fair_mutex
;
3565 static QemuThread io_thread
;
3567 static QemuThread
*tcg_cpu_thread
;
3568 static QemuCond
*tcg_halt_cond
;
3570 static int qemu_system_ready
;
3572 static QemuCond qemu_cpu_cond
;
3574 static QemuCond qemu_system_cond
;
3575 static QemuCond qemu_pause_cond
;
3577 static void block_io_signals(void);
3578 static void unblock_io_signals(void);
3579 static int tcg_has_work(void);
3581 static int qemu_init_main_loop(void)
3585 ret
= qemu_event_init();
3589 qemu_cond_init(&qemu_pause_cond
);
3590 qemu_mutex_init(&qemu_fair_mutex
);
3591 qemu_mutex_init(&qemu_global_mutex
);
3592 qemu_mutex_lock(&qemu_global_mutex
);
3594 unblock_io_signals();
3595 qemu_thread_self(&io_thread
);
3600 static void qemu_wait_io_event(CPUState
*env
)
3602 while (!tcg_has_work())
3603 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3605 qemu_mutex_unlock(&qemu_global_mutex
);
3608 * Users of qemu_global_mutex can be starved, having no chance
3609 * to acquire it since this path will get to it first.
3610 * So use another lock to provide fairness.
3612 qemu_mutex_lock(&qemu_fair_mutex
);
3613 qemu_mutex_unlock(&qemu_fair_mutex
);
3615 qemu_mutex_lock(&qemu_global_mutex
);
3619 qemu_cond_signal(&qemu_pause_cond
);
3623 static int qemu_cpu_exec(CPUState
*env
);
3625 static void *kvm_cpu_thread_fn(void *arg
)
3627 CPUState
*env
= arg
;
3630 qemu_thread_self(env
->thread
);
3634 /* signal CPU creation */
3635 qemu_mutex_lock(&qemu_global_mutex
);
3637 qemu_cond_signal(&qemu_cpu_cond
);
3639 /* and wait for machine initialization */
3640 while (!qemu_system_ready
)
3641 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3644 if (cpu_can_run(env
))
3646 qemu_wait_io_event(env
);
3652 static void tcg_cpu_exec(void);
3654 static void *tcg_cpu_thread_fn(void *arg
)
3656 CPUState
*env
= arg
;
3659 qemu_thread_self(env
->thread
);
3661 /* signal CPU creation */
3662 qemu_mutex_lock(&qemu_global_mutex
);
3663 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3665 qemu_cond_signal(&qemu_cpu_cond
);
3667 /* and wait for machine initialization */
3668 while (!qemu_system_ready
)
3669 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3673 qemu_wait_io_event(cur_cpu
);
3679 void qemu_cpu_kick(void *_env
)
3681 CPUState
*env
= _env
;
3682 qemu_cond_broadcast(env
->halt_cond
);
3684 qemu_thread_signal(env
->thread
, SIGUSR1
);
3687 int qemu_cpu_self(void *_env
)
3689 CPUState
*env
= _env
;
3692 qemu_thread_self(&this);
3694 return qemu_thread_equal(&this, env
->thread
);
3697 static void cpu_signal(int sig
)
3700 cpu_exit(cpu_single_env
);
3703 static void block_io_signals(void)
3706 struct sigaction sigact
;
3709 sigaddset(&set
, SIGUSR2
);
3710 sigaddset(&set
, SIGIO
);
3711 sigaddset(&set
, SIGALRM
);
3712 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3715 sigaddset(&set
, SIGUSR1
);
3716 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3718 memset(&sigact
, 0, sizeof(sigact
));
3719 sigact
.sa_handler
= cpu_signal
;
3720 sigaction(SIGUSR1
, &sigact
, NULL
);
3723 static void unblock_io_signals(void)
3728 sigaddset(&set
, SIGUSR2
);
3729 sigaddset(&set
, SIGIO
);
3730 sigaddset(&set
, SIGALRM
);
3731 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3734 sigaddset(&set
, SIGUSR1
);
3735 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3738 static void qemu_signal_lock(unsigned int msecs
)
3740 qemu_mutex_lock(&qemu_fair_mutex
);
3742 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3743 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3744 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3747 qemu_mutex_unlock(&qemu_fair_mutex
);
3750 static void qemu_mutex_lock_iothread(void)
3752 if (kvm_enabled()) {
3753 qemu_mutex_lock(&qemu_fair_mutex
);
3754 qemu_mutex_lock(&qemu_global_mutex
);
3755 qemu_mutex_unlock(&qemu_fair_mutex
);
3757 qemu_signal_lock(100);
3760 static void qemu_mutex_unlock_iothread(void)
3762 qemu_mutex_unlock(&qemu_global_mutex
);
3765 static int all_vcpus_paused(void)
3767 CPUState
*penv
= first_cpu
;
3772 penv
= (CPUState
*)penv
->next_cpu
;
3778 static void pause_all_vcpus(void)
3780 CPUState
*penv
= first_cpu
;
3784 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3785 qemu_cpu_kick(penv
);
3786 penv
= (CPUState
*)penv
->next_cpu
;
3789 while (!all_vcpus_paused()) {
3790 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3793 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3794 penv
= (CPUState
*)penv
->next_cpu
;
3799 static void resume_all_vcpus(void)
3801 CPUState
*penv
= first_cpu
;
3806 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3807 qemu_cpu_kick(penv
);
3808 penv
= (CPUState
*)penv
->next_cpu
;
3812 static void tcg_init_vcpu(void *_env
)
3814 CPUState
*env
= _env
;
3815 /* share a single thread for all cpus with TCG */
3816 if (!tcg_cpu_thread
) {
3817 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3818 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3819 qemu_cond_init(env
->halt_cond
);
3820 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3821 while (env
->created
== 0)
3822 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3823 tcg_cpu_thread
= env
->thread
;
3824 tcg_halt_cond
= env
->halt_cond
;
3826 env
->thread
= tcg_cpu_thread
;
3827 env
->halt_cond
= tcg_halt_cond
;
3831 static void kvm_start_vcpu(CPUState
*env
)
3833 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3834 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3835 qemu_cond_init(env
->halt_cond
);
3836 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3837 while (env
->created
== 0)
3838 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3841 void qemu_init_vcpu(void *_env
)
3843 CPUState
*env
= _env
;
3846 kvm_start_vcpu(env
);
3849 env
->nr_cores
= smp_cores
;
3850 env
->nr_threads
= smp_threads
;
3853 void qemu_notify_event(void)
3855 qemu_event_increment();
3858 void vm_stop(int reason
)
3861 qemu_thread_self(&me
);
3863 if (!qemu_thread_equal(&me
, &io_thread
)) {
3864 qemu_system_vmstop_request(reason
);
3866 * FIXME: should not return to device code in case
3867 * vm_stop() has been requested.
3869 if (cpu_single_env
) {
3870 cpu_exit(cpu_single_env
);
3871 cpu_single_env
->stop
= 1;
3882 static void host_main_loop_wait(int *timeout
)
3888 /* XXX: need to suppress polling by better using win32 events */
3890 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3891 ret
|= pe
->func(pe
->opaque
);
3895 WaitObjects
*w
= &wait_objects
;
3897 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3898 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3899 if (w
->func
[ret
- WAIT_OBJECT_0
])
3900 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3902 /* Check for additional signaled events */
3903 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3905 /* Check if event is signaled */
3906 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3907 if(ret2
== WAIT_OBJECT_0
) {
3909 w
->func
[i
](w
->opaque
[i
]);
3910 } else if (ret2
== WAIT_TIMEOUT
) {
3912 err
= GetLastError();
3913 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3916 } else if (ret
== WAIT_TIMEOUT
) {
3918 err
= GetLastError();
3919 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3926 static void host_main_loop_wait(int *timeout
)
3931 void main_loop_wait(int timeout
)
3933 IOHandlerRecord
*ioh
;
3934 fd_set rfds
, wfds
, xfds
;
3938 qemu_bh_update_timeout(&timeout
);
3940 host_main_loop_wait(&timeout
);
3942 /* poll any events */
3943 /* XXX: separate device handlers from system ones */
3948 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3952 (!ioh
->fd_read_poll
||
3953 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3954 FD_SET(ioh
->fd
, &rfds
);
3958 if (ioh
->fd_write
) {
3959 FD_SET(ioh
->fd
, &wfds
);
3965 tv
.tv_sec
= timeout
/ 1000;
3966 tv
.tv_usec
= (timeout
% 1000) * 1000;
3968 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3970 qemu_mutex_unlock_iothread();
3971 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3972 qemu_mutex_lock_iothread();
3974 IOHandlerRecord
**pioh
;
3976 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3977 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3978 ioh
->fd_read(ioh
->opaque
);
3980 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3981 ioh
->fd_write(ioh
->opaque
);
3985 /* remove deleted IO handlers */
3986 pioh
= &first_io_handler
;
3997 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3999 /* rearm timer, if not periodic */
4000 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4001 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4002 qemu_rearm_alarm_timer(alarm_timer
);
4005 /* vm time timers */
4007 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4008 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
4009 qemu_get_clock(vm_clock
));
4012 /* real time timers */
4013 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
4014 qemu_get_clock(rt_clock
));
4016 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
4017 qemu_get_clock(host_clock
));
4019 /* Check bottom-halves last in case any of the earlier events triggered
4025 static int qemu_cpu_exec(CPUState
*env
)
4028 #ifdef CONFIG_PROFILER
4032 #ifdef CONFIG_PROFILER
4033 ti
= profile_getclock();
4038 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4039 env
->icount_decr
.u16
.low
= 0;
4040 env
->icount_extra
= 0;
4041 count
= qemu_next_deadline();
4042 count
= (count
+ (1 << icount_time_shift
) - 1)
4043 >> icount_time_shift
;
4044 qemu_icount
+= count
;
4045 decr
= (count
> 0xffff) ? 0xffff : count
;
4047 env
->icount_decr
.u16
.low
= decr
;
4048 env
->icount_extra
= count
;
4050 ret
= cpu_exec(env
);
4051 #ifdef CONFIG_PROFILER
4052 qemu_time
+= profile_getclock() - ti
;
4055 /* Fold pending instructions back into the
4056 instruction counter, and clear the interrupt flag. */
4057 qemu_icount
-= (env
->icount_decr
.u16
.low
4058 + env
->icount_extra
);
4059 env
->icount_decr
.u32
= 0;
4060 env
->icount_extra
= 0;
4065 static void tcg_cpu_exec(void)
4069 if (next_cpu
== NULL
)
4070 next_cpu
= first_cpu
;
4071 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4072 CPUState
*env
= cur_cpu
= next_cpu
;
4076 if (timer_alarm_pending
) {
4077 timer_alarm_pending
= 0;
4080 if (cpu_can_run(env
))
4081 ret
= qemu_cpu_exec(env
);
4082 if (ret
== EXCP_DEBUG
) {
4083 gdb_set_stop_cpu(env
);
4084 debug_requested
= 1;
4090 static int cpu_has_work(CPUState
*env
)
4098 if (qemu_cpu_has_work(env
))
4103 static int tcg_has_work(void)
4107 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4108 if (cpu_has_work(env
))
4113 static int qemu_calculate_timeout(void)
4115 #ifndef CONFIG_IOTHREAD
4120 else if (tcg_has_work())
4122 else if (!use_icount
)
4125 /* XXX: use timeout computed from timers */
4128 /* Advance virtual time to the next event. */
4129 if (use_icount
== 1) {
4130 /* When not using an adaptive execution frequency
4131 we tend to get badly out of sync with real time,
4132 so just delay for a reasonable amount of time. */
4135 delta
= cpu_get_icount() - cpu_get_clock();
4138 /* If virtual time is ahead of real time then just
4140 timeout
= (delta
/ 1000000) + 1;
4142 /* Wait for either IO to occur or the next
4144 add
= qemu_next_deadline();
4145 /* We advance the timer before checking for IO.
4146 Limit the amount we advance so that early IO
4147 activity won't get the guest too far ahead. */
4151 add
= (add
+ (1 << icount_time_shift
) - 1)
4152 >> icount_time_shift
;
4154 timeout
= delta
/ 1000000;
4161 #else /* CONFIG_IOTHREAD */
4166 static int vm_can_run(void)
4168 if (powerdown_requested
)
4170 if (reset_requested
)
4172 if (shutdown_requested
)
4174 if (debug_requested
)
4179 qemu_irq qemu_system_powerdown
;
4181 static void main_loop(void)
4185 #ifdef CONFIG_IOTHREAD
4186 qemu_system_ready
= 1;
4187 qemu_cond_broadcast(&qemu_system_cond
);
4192 #ifdef CONFIG_PROFILER
4195 #ifndef CONFIG_IOTHREAD
4198 #ifdef CONFIG_PROFILER
4199 ti
= profile_getclock();
4201 main_loop_wait(qemu_calculate_timeout());
4202 #ifdef CONFIG_PROFILER
4203 dev_time
+= profile_getclock() - ti
;
4205 } while (vm_can_run());
4207 if (qemu_debug_requested())
4208 vm_stop(EXCP_DEBUG
);
4209 if (qemu_shutdown_requested()) {
4216 if (qemu_reset_requested()) {
4218 qemu_system_reset();
4221 if (qemu_powerdown_requested()) {
4222 qemu_irq_raise(qemu_system_powerdown
);
4224 if ((r
= qemu_vmstop_requested()))
4230 static void version(void)
4232 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4235 static void help(int exitcode
)
4238 printf("usage: %s [options] [disk_image]\n"
4240 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4242 #define DEF(option, opt_arg, opt_enum, opt_help) \
4244 #define DEFHEADING(text) stringify(text) "\n"
4245 #include "qemu-options.h"
4250 "During emulation, the following keys are useful:\n"
4251 "ctrl-alt-f toggle full screen\n"
4252 "ctrl-alt-n switch to virtual console 'n'\n"
4253 "ctrl-alt toggle mouse and keyboard grab\n"
4255 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4260 DEFAULT_NETWORK_SCRIPT
,
4261 DEFAULT_NETWORK_DOWN_SCRIPT
,
4263 DEFAULT_GDBSTUB_PORT
,
4268 #define HAS_ARG 0x0001
4271 #define DEF(option, opt_arg, opt_enum, opt_help) \
4273 #define DEFHEADING(text)
4274 #include "qemu-options.h"
4280 typedef struct QEMUOption
{
4286 static const QEMUOption qemu_options
[] = {
4287 { "h", 0, QEMU_OPTION_h
},
4288 #define DEF(option, opt_arg, opt_enum, opt_help) \
4289 { option, opt_arg, opt_enum },
4290 #define DEFHEADING(text)
4291 #include "qemu-options.h"
4299 struct soundhw soundhw
[] = {
4300 #ifdef HAS_AUDIO_CHOICE
4301 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4307 { .init_isa
= pcspk_audio_init
}
4314 "Creative Sound Blaster 16",
4317 { .init_isa
= SB16_init
}
4321 #ifdef CONFIG_CS4231A
4327 { .init_isa
= cs4231a_init
}
4335 "Yamaha YMF262 (OPL3)",
4337 "Yamaha YM3812 (OPL2)",
4341 { .init_isa
= Adlib_init
}
4348 "Gravis Ultrasound GF1",
4351 { .init_isa
= GUS_init
}
4358 "Intel 82801AA AC97 Audio",
4361 { .init_pci
= ac97_init
}
4365 #ifdef CONFIG_ES1370
4368 "ENSONIQ AudioPCI ES1370",
4371 { .init_pci
= es1370_init
}
4375 #endif /* HAS_AUDIO_CHOICE */
4377 { NULL
, NULL
, 0, 0, { NULL
} }
4380 static void select_soundhw (const char *optarg
)
4384 if (*optarg
== '?') {
4387 printf ("Valid sound card names (comma separated):\n");
4388 for (c
= soundhw
; c
->name
; ++c
) {
4389 printf ("%-11s %s\n", c
->name
, c
->descr
);
4391 printf ("\n-soundhw all will enable all of the above\n");
4392 exit (*optarg
!= '?');
4400 if (!strcmp (optarg
, "all")) {
4401 for (c
= soundhw
; c
->name
; ++c
) {
4409 e
= strchr (p
, ',');
4410 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4412 for (c
= soundhw
; c
->name
; ++c
) {
4413 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4422 "Unknown sound card name (too big to show)\n");
4425 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4430 p
+= l
+ (e
!= NULL
);
4434 goto show_valid_cards
;
4439 static void select_vgahw (const char *p
)
4443 vga_interface_type
= VGA_NONE
;
4444 if (strstart(p
, "std", &opts
)) {
4445 vga_interface_type
= VGA_STD
;
4446 } else if (strstart(p
, "cirrus", &opts
)) {
4447 vga_interface_type
= VGA_CIRRUS
;
4448 } else if (strstart(p
, "vmware", &opts
)) {
4449 vga_interface_type
= VGA_VMWARE
;
4450 } else if (strstart(p
, "xenfb", &opts
)) {
4451 vga_interface_type
= VGA_XENFB
;
4452 } else if (!strstart(p
, "none", &opts
)) {
4454 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4458 const char *nextopt
;
4460 if (strstart(opts
, ",retrace=", &nextopt
)) {
4462 if (strstart(opts
, "dumb", &nextopt
))
4463 vga_retrace_method
= VGA_RETRACE_DUMB
;
4464 else if (strstart(opts
, "precise", &nextopt
))
4465 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4466 else goto invalid_vga
;
4467 } else goto invalid_vga
;
4473 static int balloon_parse(const char *arg
)
4477 if (strcmp(arg
, "none") == 0) {
4481 if (!strncmp(arg
, "virtio", 6)) {
4482 if (arg
[6] == ',') {
4483 /* have params -> parse them */
4484 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4488 /* create empty opts */
4489 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4491 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4500 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4502 exit(STATUS_CONTROL_C_EXIT
);
4507 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4511 if(strlen(str
) != 36)
4514 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4515 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4516 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4522 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4528 #define MAX_NET_CLIENTS 32
4532 static void termsig_handler(int signal
)
4534 qemu_system_shutdown_request();
4537 static void sigchld_handler(int signal
)
4539 waitpid(-1, NULL
, WNOHANG
);
4542 static void sighandler_setup(void)
4544 struct sigaction act
;
4546 memset(&act
, 0, sizeof(act
));
4547 act
.sa_handler
= termsig_handler
;
4548 sigaction(SIGINT
, &act
, NULL
);
4549 sigaction(SIGHUP
, &act
, NULL
);
4550 sigaction(SIGTERM
, &act
, NULL
);
4552 act
.sa_handler
= sigchld_handler
;
4553 act
.sa_flags
= SA_NOCLDSTOP
;
4554 sigaction(SIGCHLD
, &act
, NULL
);
4560 /* Look for support files in the same directory as the executable. */
4561 static char *find_datadir(const char *argv0
)
4567 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4574 while (p
!= buf
&& *p
!= '\\')
4577 if (access(buf
, R_OK
) == 0) {
4578 return qemu_strdup(buf
);
4584 /* Find a likely location for support files using the location of the binary.
4585 For installed binaries this will be "$bindir/../share/qemu". When
4586 running from the build tree this will be "$bindir/../pc-bios". */
4587 #define SHARE_SUFFIX "/share/qemu"
4588 #define BUILD_SUFFIX "/pc-bios"
4589 static char *find_datadir(const char *argv0
)
4597 #if defined(__linux__)
4600 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4606 #elif defined(__FreeBSD__)
4609 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4616 /* If we don't have any way of figuring out the actual executable
4617 location then try argv[0]. */
4619 p
= realpath(argv0
, buf
);
4627 max_len
= strlen(dir
) +
4628 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4629 res
= qemu_mallocz(max_len
);
4630 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4631 if (access(res
, R_OK
)) {
4632 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4633 if (access(res
, R_OK
)) {
4645 char *qemu_find_file(int type
, const char *name
)
4651 /* If name contains path separators then try it as a straight path. */
4652 if ((strchr(name
, '/') || strchr(name
, '\\'))
4653 && access(name
, R_OK
) == 0) {
4654 return qemu_strdup(name
);
4657 case QEMU_FILE_TYPE_BIOS
:
4660 case QEMU_FILE_TYPE_KEYMAP
:
4661 subdir
= "keymaps/";
4666 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4667 buf
= qemu_mallocz(len
);
4668 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4669 if (access(buf
, R_OK
)) {
4676 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4680 dev
= qdev_device_add(opts
);
4686 struct device_config
{
4688 DEV_USB
, /* -usbdevice */
4691 const char *cmdline
;
4692 QTAILQ_ENTRY(device_config
) next
;
4694 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4696 static void add_device_config(int type
, const char *cmdline
)
4698 struct device_config
*conf
;
4700 conf
= qemu_mallocz(sizeof(*conf
));
4702 conf
->cmdline
= cmdline
;
4703 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4706 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4708 struct device_config
*conf
;
4711 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4712 if (conf
->type
!= type
)
4714 rc
= func(conf
->cmdline
);
4721 int main(int argc
, char **argv
, char **envp
)
4723 const char *gdbstub_dev
= NULL
;
4724 uint32_t boot_devices_bitmap
= 0;
4726 int snapshot
, linux_boot
, net_boot
;
4727 const char *initrd_filename
;
4728 const char *kernel_filename
, *kernel_cmdline
;
4729 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4731 DisplayChangeListener
*dcl
;
4732 int cyls
, heads
, secs
, translation
;
4733 const char *net_clients
[MAX_NET_CLIENTS
];
4735 QemuOpts
*hda_opts
= NULL
, *opts
;
4737 const char *r
, *optarg
;
4738 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4739 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4740 int monitor_device_index
;
4741 const char *serial_devices
[MAX_SERIAL_PORTS
];
4742 int serial_device_index
;
4743 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4744 int parallel_device_index
;
4745 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4746 int virtio_console_index
;
4747 const char *loadvm
= NULL
;
4748 QEMUMachine
*machine
;
4749 const char *cpu_model
;
4754 const char *pid_file
= NULL
;
4755 const char *incoming
= NULL
;
4758 struct passwd
*pwd
= NULL
;
4759 const char *chroot_dir
= NULL
;
4760 const char *run_as
= NULL
;
4763 int show_vnc_port
= 0;
4767 qemu_errors_to_file(stderr
);
4768 qemu_cache_utils_init(envp
);
4770 QLIST_INIT (&vm_change_state_head
);
4773 struct sigaction act
;
4774 sigfillset(&act
.sa_mask
);
4776 act
.sa_handler
= SIG_IGN
;
4777 sigaction(SIGPIPE
, &act
, NULL
);
4780 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4781 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4782 QEMU to run on a single CPU */
4787 h
= GetCurrentProcess();
4788 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4789 for(i
= 0; i
< 32; i
++) {
4790 if (mask
& (1 << i
))
4795 SetProcessAffinityMask(h
, mask
);
4801 module_call_init(MODULE_INIT_MACHINE
);
4802 machine
= find_default_machine();
4804 initrd_filename
= NULL
;
4807 kernel_filename
= NULL
;
4808 kernel_cmdline
= "";
4809 cyls
= heads
= secs
= 0;
4810 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4812 serial_devices
[0] = "vc:80Cx24C";
4813 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4814 serial_devices
[i
] = NULL
;
4815 serial_device_index
= 0;
4817 parallel_devices
[0] = "vc:80Cx24C";
4818 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4819 parallel_devices
[i
] = NULL
;
4820 parallel_device_index
= 0;
4822 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4823 virtio_consoles
[i
] = NULL
;
4824 virtio_console_index
= 0;
4826 monitor_devices
[0] = "vc:80Cx24C";
4827 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4828 monitor_devices
[i
] = NULL
;
4830 monitor_device_index
= 0;
4832 for (i
= 0; i
< MAX_NODES
; i
++) {
4834 node_cpumask
[i
] = 0;
4850 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4852 const QEMUOption
*popt
;
4855 /* Treat --foo the same as -foo. */
4858 popt
= qemu_options
;
4861 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4865 if (!strcmp(popt
->name
, r
+ 1))
4869 if (popt
->flags
& HAS_ARG
) {
4870 if (optind
>= argc
) {
4871 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4875 optarg
= argv
[optind
++];
4880 switch(popt
->index
) {
4882 machine
= find_machine(optarg
);
4885 printf("Supported machines are:\n");
4886 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4888 printf("%-10s %s (alias of %s)\n",
4889 m
->alias
, m
->desc
, m
->name
);
4890 printf("%-10s %s%s\n",
4892 m
->is_default
? " (default)" : "");
4894 exit(*optarg
!= '?');
4897 case QEMU_OPTION_cpu
:
4898 /* hw initialization will check this */
4899 if (*optarg
== '?') {
4900 /* XXX: implement xxx_cpu_list for targets that still miss it */
4901 #if defined(cpu_list)
4902 cpu_list(stdout
, &fprintf
);
4909 case QEMU_OPTION_initrd
:
4910 initrd_filename
= optarg
;
4912 case QEMU_OPTION_hda
:
4914 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4916 hda_opts
= drive_add(optarg
, HD_ALIAS
4917 ",cyls=%d,heads=%d,secs=%d%s",
4918 0, cyls
, heads
, secs
,
4919 translation
== BIOS_ATA_TRANSLATION_LBA
?
4921 translation
== BIOS_ATA_TRANSLATION_NONE
?
4922 ",trans=none" : "");
4924 case QEMU_OPTION_hdb
:
4925 case QEMU_OPTION_hdc
:
4926 case QEMU_OPTION_hdd
:
4927 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4929 case QEMU_OPTION_drive
:
4930 drive_add(NULL
, "%s", optarg
);
4932 case QEMU_OPTION_set
:
4933 if (qemu_set_option(optarg
) != 0)
4936 case QEMU_OPTION_mtdblock
:
4937 drive_add(optarg
, MTD_ALIAS
);
4939 case QEMU_OPTION_sd
:
4940 drive_add(optarg
, SD_ALIAS
);
4942 case QEMU_OPTION_pflash
:
4943 drive_add(optarg
, PFLASH_ALIAS
);
4945 case QEMU_OPTION_snapshot
:
4948 case QEMU_OPTION_hdachs
:
4952 cyls
= strtol(p
, (char **)&p
, 0);
4953 if (cyls
< 1 || cyls
> 16383)
4958 heads
= strtol(p
, (char **)&p
, 0);
4959 if (heads
< 1 || heads
> 16)
4964 secs
= strtol(p
, (char **)&p
, 0);
4965 if (secs
< 1 || secs
> 63)
4969 if (!strcmp(p
, "none"))
4970 translation
= BIOS_ATA_TRANSLATION_NONE
;
4971 else if (!strcmp(p
, "lba"))
4972 translation
= BIOS_ATA_TRANSLATION_LBA
;
4973 else if (!strcmp(p
, "auto"))
4974 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4977 } else if (*p
!= '\0') {
4979 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4982 if (hda_opts
!= NULL
) {
4984 snprintf(num
, sizeof(num
), "%d", cyls
);
4985 qemu_opt_set(hda_opts
, "cyls", num
);
4986 snprintf(num
, sizeof(num
), "%d", heads
);
4987 qemu_opt_set(hda_opts
, "heads", num
);
4988 snprintf(num
, sizeof(num
), "%d", secs
);
4989 qemu_opt_set(hda_opts
, "secs", num
);
4990 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4991 qemu_opt_set(hda_opts
, "trans", "lba");
4992 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4993 qemu_opt_set(hda_opts
, "trans", "none");
4997 case QEMU_OPTION_numa
:
4998 if (nb_numa_nodes
>= MAX_NODES
) {
4999 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5004 case QEMU_OPTION_nographic
:
5005 display_type
= DT_NOGRAPHIC
;
5007 #ifdef CONFIG_CURSES
5008 case QEMU_OPTION_curses
:
5009 display_type
= DT_CURSES
;
5012 case QEMU_OPTION_portrait
:
5015 case QEMU_OPTION_kernel
:
5016 kernel_filename
= optarg
;
5018 case QEMU_OPTION_append
:
5019 kernel_cmdline
= optarg
;
5021 case QEMU_OPTION_cdrom
:
5022 drive_add(optarg
, CDROM_ALIAS
);
5024 case QEMU_OPTION_boot
:
5026 static const char * const params
[] = {
5027 "order", "once", "menu", NULL
5029 char buf
[sizeof(boot_devices
)];
5030 char *standard_boot_devices
;
5033 if (!strchr(optarg
, '=')) {
5035 pstrcpy(buf
, sizeof(buf
), optarg
);
5036 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5038 "qemu: unknown boot parameter '%s' in '%s'\n",
5044 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5045 boot_devices_bitmap
= parse_bootdevices(buf
);
5046 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5049 if (get_param_value(buf
, sizeof(buf
),
5051 boot_devices_bitmap
|= parse_bootdevices(buf
);
5052 standard_boot_devices
= qemu_strdup(boot_devices
);
5053 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5054 qemu_register_reset(restore_boot_devices
,
5055 standard_boot_devices
);
5057 if (get_param_value(buf
, sizeof(buf
),
5059 if (!strcmp(buf
, "on")) {
5061 } else if (!strcmp(buf
, "off")) {
5065 "qemu: invalid option value '%s'\n",
5073 case QEMU_OPTION_fda
:
5074 case QEMU_OPTION_fdb
:
5075 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5078 case QEMU_OPTION_no_fd_bootchk
:
5082 case QEMU_OPTION_net
:
5083 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5084 fprintf(stderr
, "qemu: too many network clients\n");
5087 net_clients
[nb_net_clients
] = optarg
;
5091 case QEMU_OPTION_tftp
:
5092 legacy_tftp_prefix
= optarg
;
5094 case QEMU_OPTION_bootp
:
5095 legacy_bootp_filename
= optarg
;
5098 case QEMU_OPTION_smb
:
5099 net_slirp_smb(optarg
);
5102 case QEMU_OPTION_redir
:
5103 net_slirp_redir(optarg
);
5106 case QEMU_OPTION_bt
:
5107 add_device_config(DEV_BT
, optarg
);
5110 case QEMU_OPTION_audio_help
:
5114 case QEMU_OPTION_soundhw
:
5115 select_soundhw (optarg
);
5121 case QEMU_OPTION_version
:
5125 case QEMU_OPTION_m
: {
5129 value
= strtoul(optarg
, &ptr
, 10);
5131 case 0: case 'M': case 'm':
5138 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5142 /* On 32-bit hosts, QEMU is limited by virtual address space */
5143 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5144 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5147 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5148 fprintf(stderr
, "qemu: ram size too large\n");
5157 const CPULogItem
*item
;
5159 mask
= cpu_str_to_log_mask(optarg
);
5161 printf("Log items (comma separated):\n");
5162 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5163 printf("%-10s %s\n", item
->name
, item
->help
);
5171 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5173 case QEMU_OPTION_gdb
:
5174 gdbstub_dev
= optarg
;
5179 case QEMU_OPTION_bios
:
5182 case QEMU_OPTION_singlestep
:
5190 keyboard_layout
= optarg
;
5193 case QEMU_OPTION_localtime
:
5196 case QEMU_OPTION_vga
:
5197 select_vgahw (optarg
);
5199 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5205 w
= strtol(p
, (char **)&p
, 10);
5208 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5214 h
= strtol(p
, (char **)&p
, 10);
5219 depth
= strtol(p
, (char **)&p
, 10);
5220 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5221 depth
!= 24 && depth
!= 32)
5223 } else if (*p
== '\0') {
5224 depth
= graphic_depth
;
5231 graphic_depth
= depth
;
5235 case QEMU_OPTION_echr
:
5238 term_escape_char
= strtol(optarg
, &r
, 0);
5240 printf("Bad argument to echr\n");
5243 case QEMU_OPTION_monitor
:
5244 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5245 fprintf(stderr
, "qemu: too many monitor devices\n");
5248 monitor_devices
[monitor_device_index
] = optarg
;
5249 monitor_device_index
++;
5251 case QEMU_OPTION_chardev
:
5252 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5254 fprintf(stderr
, "parse error: %s\n", optarg
);
5257 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5261 case QEMU_OPTION_serial
:
5262 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5263 fprintf(stderr
, "qemu: too many serial ports\n");
5266 serial_devices
[serial_device_index
] = optarg
;
5267 serial_device_index
++;
5269 case QEMU_OPTION_watchdog
:
5272 "qemu: only one watchdog option may be given\n");
5277 case QEMU_OPTION_watchdog_action
:
5278 if (select_watchdog_action(optarg
) == -1) {
5279 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5283 case QEMU_OPTION_virtiocon
:
5284 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5285 fprintf(stderr
, "qemu: too many virtio consoles\n");
5288 virtio_consoles
[virtio_console_index
] = optarg
;
5289 virtio_console_index
++;
5291 case QEMU_OPTION_parallel
:
5292 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5293 fprintf(stderr
, "qemu: too many parallel ports\n");
5296 parallel_devices
[parallel_device_index
] = optarg
;
5297 parallel_device_index
++;
5299 case QEMU_OPTION_loadvm
:
5302 case QEMU_OPTION_full_screen
:
5306 case QEMU_OPTION_no_frame
:
5309 case QEMU_OPTION_alt_grab
:
5312 case QEMU_OPTION_no_quit
:
5315 case QEMU_OPTION_sdl
:
5316 display_type
= DT_SDL
;
5319 case QEMU_OPTION_pidfile
:
5323 case QEMU_OPTION_win2k_hack
:
5324 win2k_install_hack
= 1;
5326 case QEMU_OPTION_rtc_td_hack
:
5329 case QEMU_OPTION_acpitable
:
5330 if(acpi_table_add(optarg
) < 0) {
5331 fprintf(stderr
, "Wrong acpi table provided\n");
5335 case QEMU_OPTION_smbios
:
5336 if(smbios_entry_add(optarg
) < 0) {
5337 fprintf(stderr
, "Wrong smbios provided\n");
5343 case QEMU_OPTION_enable_kvm
:
5347 case QEMU_OPTION_usb
:
5350 case QEMU_OPTION_usbdevice
:
5352 add_device_config(DEV_USB
, optarg
);
5354 case QEMU_OPTION_device
:
5355 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5357 fprintf(stderr
, "parse error: %s\n", optarg
);
5361 case QEMU_OPTION_smp
:
5364 fprintf(stderr
, "Invalid number of CPUs\n");
5367 if (max_cpus
< smp_cpus
) {
5368 fprintf(stderr
, "maxcpus must be equal to or greater than "
5372 if (max_cpus
> 255) {
5373 fprintf(stderr
, "Unsupported number of maxcpus\n");
5377 case QEMU_OPTION_vnc
:
5378 display_type
= DT_VNC
;
5379 vnc_display
= optarg
;
5382 case QEMU_OPTION_no_acpi
:
5385 case QEMU_OPTION_no_hpet
:
5388 case QEMU_OPTION_balloon
:
5389 if (balloon_parse(optarg
) < 0) {
5390 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5395 case QEMU_OPTION_no_reboot
:
5398 case QEMU_OPTION_no_shutdown
:
5401 case QEMU_OPTION_show_cursor
:
5404 case QEMU_OPTION_uuid
:
5405 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5406 fprintf(stderr
, "Fail to parse UUID string."
5407 " Wrong format.\n");
5412 case QEMU_OPTION_daemonize
:
5416 case QEMU_OPTION_option_rom
:
5417 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5418 fprintf(stderr
, "Too many option ROMs\n");
5421 option_rom
[nb_option_roms
] = optarg
;
5424 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5425 case QEMU_OPTION_semihosting
:
5426 semihosting_enabled
= 1;
5429 case QEMU_OPTION_name
:
5430 qemu_name
= qemu_strdup(optarg
);
5432 char *p
= strchr(qemu_name
, ',');
5435 if (strncmp(p
, "process=", 8)) {
5436 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5444 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5445 case QEMU_OPTION_prom_env
:
5446 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5447 fprintf(stderr
, "Too many prom variables\n");
5450 prom_envs
[nb_prom_envs
] = optarg
;
5455 case QEMU_OPTION_old_param
:
5459 case QEMU_OPTION_clock
:
5460 configure_alarms(optarg
);
5462 case QEMU_OPTION_startdate
:
5463 configure_rtc_date_offset(optarg
, 1);
5465 case QEMU_OPTION_rtc
:
5466 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5468 fprintf(stderr
, "parse error: %s\n", optarg
);
5471 configure_rtc(opts
);
5473 case QEMU_OPTION_tb_size
:
5474 tb_size
= strtol(optarg
, NULL
, 0);
5478 case QEMU_OPTION_icount
:
5480 if (strcmp(optarg
, "auto") == 0) {
5481 icount_time_shift
= -1;
5483 icount_time_shift
= strtol(optarg
, NULL
, 0);
5486 case QEMU_OPTION_incoming
:
5490 case QEMU_OPTION_chroot
:
5491 chroot_dir
= optarg
;
5493 case QEMU_OPTION_runas
:
5498 case QEMU_OPTION_xen_domid
:
5499 xen_domid
= atoi(optarg
);
5501 case QEMU_OPTION_xen_create
:
5502 xen_mode
= XEN_CREATE
;
5504 case QEMU_OPTION_xen_attach
:
5505 xen_mode
= XEN_ATTACH
;
5512 /* If no data_dir is specified then try to find it relative to the
5515 data_dir
= find_datadir(argv
[0]);
5517 /* If all else fails use the install patch specified when building. */
5519 data_dir
= CONFIG_QEMU_SHAREDIR
;
5523 * Default to max_cpus = smp_cpus, in case the user doesn't
5524 * specify a max_cpus value.
5527 max_cpus
= smp_cpus
;
5529 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5530 if (smp_cpus
> machine
->max_cpus
) {
5531 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5532 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5537 if (display_type
== DT_NOGRAPHIC
) {
5538 if (serial_device_index
== 0)
5539 serial_devices
[0] = "stdio";
5540 if (parallel_device_index
== 0)
5541 parallel_devices
[0] = "null";
5542 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5543 monitor_devices
[0] = "stdio";
5551 if (pipe(fds
) == -1)
5562 len
= read(fds
[0], &status
, 1);
5563 if (len
== -1 && (errno
== EINTR
))
5568 else if (status
== 1) {
5569 fprintf(stderr
, "Could not acquire pidfile\n");
5586 signal(SIGTSTP
, SIG_IGN
);
5587 signal(SIGTTOU
, SIG_IGN
);
5588 signal(SIGTTIN
, SIG_IGN
);
5591 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5594 write(fds
[1], &status
, 1);
5596 fprintf(stderr
, "Could not acquire pid file\n");
5601 if (kvm_enabled()) {
5604 ret
= kvm_init(smp_cpus
);
5606 fprintf(stderr
, "failed to initialize KVM\n");
5611 if (qemu_init_main_loop()) {
5612 fprintf(stderr
, "qemu_init_main_loop failed\n");
5615 linux_boot
= (kernel_filename
!= NULL
);
5617 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5618 fprintf(stderr
, "-append only allowed with -kernel option\n");
5622 if (!linux_boot
&& initrd_filename
!= NULL
) {
5623 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5628 /* Win32 doesn't support line-buffering and requires size >= 2 */
5629 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5632 if (init_timer_alarm() < 0) {
5633 fprintf(stderr
, "could not initialize alarm timer\n");
5636 if (use_icount
&& icount_time_shift
< 0) {
5638 /* 125MIPS seems a reasonable initial guess at the guest speed.
5639 It will be corrected fairly quickly anyway. */
5640 icount_time_shift
= 3;
5641 init_icount_adjust();
5648 /* init network clients */
5649 if (nb_net_clients
== 0) {
5650 /* if no clients, we use a default config */
5651 net_clients
[nb_net_clients
++] = "nic";
5653 net_clients
[nb_net_clients
++] = "user";
5657 for(i
= 0;i
< nb_net_clients
; i
++) {
5658 if (net_client_parse(net_clients
[i
]) < 0)
5662 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5663 net_set_boot_mask(net_boot
);
5667 /* init the bluetooth world */
5668 if (foreach_device_config(DEV_BT
, bt_parse
))
5671 /* init the memory */
5673 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5675 /* init the dynamic translator */
5676 cpu_exec_init_all(tb_size
* 1024 * 1024);
5680 /* we always create the cdrom drive, even if no disk is there */
5681 drive_add(NULL
, CDROM_ALIAS
);
5683 /* we always create at least one floppy */
5684 drive_add(NULL
, FD_ALIAS
, 0);
5686 /* we always create one sd slot, even if no card is in it */
5687 drive_add(NULL
, SD_ALIAS
);
5689 /* open the virtual block devices */
5691 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5692 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5695 vmstate_register(0, &vmstate_timers
,&timers_state
);
5696 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5698 /* Maintain compatibility with multiple stdio monitors */
5699 if (!strcmp(monitor_devices
[0],"stdio")) {
5700 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5701 const char *devname
= serial_devices
[i
];
5702 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5703 monitor_devices
[0] = NULL
;
5705 } else if (devname
&& !strcmp(devname
,"stdio")) {
5706 monitor_devices
[0] = NULL
;
5707 serial_devices
[i
] = "mon:stdio";
5713 if (nb_numa_nodes
> 0) {
5716 if (nb_numa_nodes
> smp_cpus
) {
5717 nb_numa_nodes
= smp_cpus
;
5720 /* If no memory size if given for any node, assume the default case
5721 * and distribute the available memory equally across all nodes
5723 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5724 if (node_mem
[i
] != 0)
5727 if (i
== nb_numa_nodes
) {
5728 uint64_t usedmem
= 0;
5730 /* On Linux, the each node's border has to be 8MB aligned,
5731 * the final node gets the rest.
5733 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5734 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5735 usedmem
+= node_mem
[i
];
5737 node_mem
[i
] = ram_size
- usedmem
;
5740 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5741 if (node_cpumask
[i
] != 0)
5744 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5745 * must cope with this anyway, because there are BIOSes out there in
5746 * real machines which also use this scheme.
5748 if (i
== nb_numa_nodes
) {
5749 for (i
= 0; i
< smp_cpus
; i
++) {
5750 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5755 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5756 const char *devname
= monitor_devices
[i
];
5757 if (devname
&& strcmp(devname
, "none")) {
5760 snprintf(label
, sizeof(label
), "monitor");
5762 snprintf(label
, sizeof(label
), "monitor%d", i
);
5764 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5765 if (!monitor_hds
[i
]) {
5766 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5773 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5774 const char *devname
= serial_devices
[i
];
5775 if (devname
&& strcmp(devname
, "none")) {
5777 snprintf(label
, sizeof(label
), "serial%d", i
);
5778 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5779 if (!serial_hds
[i
]) {
5780 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5787 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5788 const char *devname
= parallel_devices
[i
];
5789 if (devname
&& strcmp(devname
, "none")) {
5791 snprintf(label
, sizeof(label
), "parallel%d", i
);
5792 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5793 if (!parallel_hds
[i
]) {
5794 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5801 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5802 const char *devname
= virtio_consoles
[i
];
5803 if (devname
&& strcmp(devname
, "none")) {
5805 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5806 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5807 if (!virtcon_hds
[i
]) {
5808 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5815 module_call_init(MODULE_INIT_DEVICE
);
5818 i
= select_watchdog(watchdog
);
5820 exit (i
== 1 ? 1 : 0);
5823 if (machine
->compat_props
) {
5824 qdev_prop_register_compat(machine
->compat_props
);
5826 machine
->init(ram_size
, boot_devices
,
5827 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5831 /* must be after terminal init, SDL library changes signal handlers */
5835 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5836 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5837 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5843 current_machine
= machine
;
5845 /* init USB devices */
5847 foreach_device_config(DEV_USB
, usb_parse
);
5850 /* init generic devices */
5851 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5855 dumb_display_init();
5856 /* just use the first displaystate for the moment */
5859 if (display_type
== DT_DEFAULT
) {
5860 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5861 display_type
= DT_SDL
;
5863 display_type
= DT_VNC
;
5864 vnc_display
= "localhost:0,to=99";
5870 switch (display_type
) {
5873 #if defined(CONFIG_CURSES)
5875 curses_display_init(ds
, full_screen
);
5878 #if defined(CONFIG_SDL)
5880 sdl_display_init(ds
, full_screen
, no_frame
);
5882 #elif defined(CONFIG_COCOA)
5884 cocoa_display_init(ds
, full_screen
);
5888 vnc_display_init(ds
);
5889 if (vnc_display_open(ds
, vnc_display
) < 0)
5892 if (show_vnc_port
) {
5893 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5901 dcl
= ds
->listeners
;
5902 while (dcl
!= NULL
) {
5903 if (dcl
->dpy_refresh
!= NULL
) {
5904 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5905 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5910 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5911 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5912 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5915 text_consoles_set_display(display_state
);
5916 qemu_chr_initial_reset();
5918 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5919 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5920 monitor_init(monitor_hds
[i
],
5921 MONITOR_USE_READLINE
|
5922 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5926 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5927 const char *devname
= serial_devices
[i
];
5928 if (devname
&& strcmp(devname
, "none")) {
5929 if (strstart(devname
, "vc", 0))
5930 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5934 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5935 const char *devname
= parallel_devices
[i
];
5936 if (devname
&& strcmp(devname
, "none")) {
5937 if (strstart(devname
, "vc", 0))
5938 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5942 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5943 const char *devname
= virtio_consoles
[i
];
5944 if (virtcon_hds
[i
] && devname
) {
5945 if (strstart(devname
, "vc", 0))
5946 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5950 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5951 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5956 qdev_machine_creation_done();
5959 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5965 qemu_start_incoming_migration(incoming
);
5966 } else if (autostart
) {
5976 len
= write(fds
[1], &status
, 1);
5977 if (len
== -1 && (errno
== EINTR
))
5984 TFR(fd
= open("/dev/null", O_RDWR
));
5990 pwd
= getpwnam(run_as
);
5992 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5998 if (chroot(chroot_dir
) < 0) {
5999 fprintf(stderr
, "chroot failed\n");
6006 if (setgid(pwd
->pw_gid
) < 0) {
6007 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6010 if (setuid(pwd
->pw_uid
) < 0) {
6011 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6014 if (setuid(0) != -1) {
6015 fprintf(stderr
, "Dropping privileges failed\n");