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>
99 #if defined(__OpenBSD__)
103 #if defined(CONFIG_VDE)
104 #include <libvdeplug.h>
109 #include <mmsystem.h>
113 #if defined(__APPLE__) || defined(main)
115 int qemu_main(int argc
, char **argv
, char **envp
);
116 int main(int argc
, char **argv
)
118 return qemu_main(argc
, argv
, NULL
);
121 #define main qemu_main
123 #endif /* CONFIG_SDL */
127 #define main qemu_main
128 #endif /* CONFIG_COCOA */
131 #include "hw/boards.h"
133 #include "hw/pcmcia.h"
135 #include "hw/audiodev.h"
139 #include "hw/watchdog.h"
140 #include "hw/smbios.h"
149 #include "qemu-timer.h"
150 #include "qemu-char.h"
151 #include "cache-utils.h"
154 #include "audio/audio.h"
155 #include "migration.h"
158 #include "qemu-option.h"
159 #include "qemu-config.h"
163 #include "exec-all.h"
165 #include "qemu_socket.h"
167 #include "slirp/libslirp.h"
169 #include "qemu-queue.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 /* Maximum number of monitor devices */
177 #define MAX_MONITOR_DEVICES 10
179 static const char *data_dir
;
180 const char *bios_name
= NULL
;
181 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
182 to store the VM snapshots */
183 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
184 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
185 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
186 static DisplayState
*display_state
;
187 DisplayType display_type
= DT_DEFAULT
;
188 const char* keyboard_layout
= NULL
;
191 NICInfo nd_table
[MAX_NICS
];
194 static int rtc_utc
= 1;
195 static int rtc_date_offset
= -1; /* -1 means no change */
196 int vga_interface_type
= VGA_CIRRUS
;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
224 const char *vnc_display
;
225 int acpi_enabled
= 1;
231 int graphic_rotate
= 0;
232 uint8_t irq0override
= 1;
236 const char *watchdog
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
245 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
246 unsigned int nb_prom_envs
= 0;
247 const char *prom_envs
[MAX_PROM_ENVS
];
252 uint64_t node_mem
[MAX_NODES
];
253 uint64_t node_cpumask
[MAX_NODES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int timer_alarm_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
266 static QEMUTimer
*nographic_timer
;
268 uint8_t qemu_uuid
[16];
270 static QEMUBootSetHandler
*boot_set_handler
;
271 static void *boot_set_opaque
;
273 /***********************************************************/
274 /* x86 ISA bus support */
276 target_phys_addr_t isa_mem_base
= 0;
279 /***********************************************************/
280 void hw_error(const char *fmt
, ...)
286 fprintf(stderr
, "qemu: hardware error: ");
287 vfprintf(stderr
, fmt
, ap
);
288 fprintf(stderr
, "\n");
289 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
290 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
292 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
294 cpu_dump_state(env
, stderr
, fprintf
, 0);
301 static void set_proc_name(const char *s
)
303 #if defined(__linux__) && defined(PR_SET_NAME)
307 name
[sizeof(name
) - 1] = 0;
308 strncpy(name
, s
, sizeof(name
));
309 /* Could rewrite argv[0] too, but that's a bit more complicated.
310 This simple way is enough for `top'. */
311 prctl(PR_SET_NAME
, name
);
318 static QEMUBalloonEvent
*qemu_balloon_event
;
319 void *qemu_balloon_event_opaque
;
321 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
323 qemu_balloon_event
= func
;
324 qemu_balloon_event_opaque
= opaque
;
327 void qemu_balloon(ram_addr_t target
)
329 if (qemu_balloon_event
)
330 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
333 ram_addr_t
qemu_balloon_status(void)
335 if (qemu_balloon_event
)
336 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
340 /***********************************************************/
343 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
344 static void *qemu_put_kbd_event_opaque
;
345 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
348 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
350 qemu_put_kbd_event_opaque
= opaque
;
351 qemu_put_kbd_event
= func
;
354 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
355 void *opaque
, int absolute
,
358 QEMUPutMouseEntry
*s
, *cursor
;
360 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
362 s
->qemu_put_mouse_event
= func
;
363 s
->qemu_put_mouse_event_opaque
= opaque
;
364 s
->qemu_put_mouse_event_absolute
= absolute
;
365 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
368 if (!qemu_put_mouse_event_head
) {
369 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
373 cursor
= qemu_put_mouse_event_head
;
374 while (cursor
->next
!= NULL
)
375 cursor
= cursor
->next
;
378 qemu_put_mouse_event_current
= s
;
383 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
385 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
387 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
390 cursor
= qemu_put_mouse_event_head
;
391 while (cursor
!= NULL
&& cursor
!= entry
) {
393 cursor
= cursor
->next
;
396 if (cursor
== NULL
) // does not exist or list empty
398 else if (prev
== NULL
) { // entry is head
399 qemu_put_mouse_event_head
= cursor
->next
;
400 if (qemu_put_mouse_event_current
== entry
)
401 qemu_put_mouse_event_current
= cursor
->next
;
402 qemu_free(entry
->qemu_put_mouse_event_name
);
407 prev
->next
= entry
->next
;
409 if (qemu_put_mouse_event_current
== entry
)
410 qemu_put_mouse_event_current
= prev
;
412 qemu_free(entry
->qemu_put_mouse_event_name
);
416 void kbd_put_keycode(int keycode
)
418 if (qemu_put_kbd_event
) {
419 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
423 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
425 QEMUPutMouseEvent
*mouse_event
;
426 void *mouse_event_opaque
;
429 if (!qemu_put_mouse_event_current
) {
434 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
436 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
439 if (graphic_rotate
) {
440 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
443 width
= graphic_width
- 1;
444 mouse_event(mouse_event_opaque
,
445 width
- dy
, dx
, dz
, buttons_state
);
447 mouse_event(mouse_event_opaque
,
448 dx
, dy
, dz
, buttons_state
);
452 int kbd_mouse_is_absolute(void)
454 if (!qemu_put_mouse_event_current
)
457 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
460 void do_info_mice(Monitor
*mon
)
462 QEMUPutMouseEntry
*cursor
;
465 if (!qemu_put_mouse_event_head
) {
466 monitor_printf(mon
, "No mouse devices connected\n");
470 monitor_printf(mon
, "Mouse devices available:\n");
471 cursor
= qemu_put_mouse_event_head
;
472 while (cursor
!= NULL
) {
473 monitor_printf(mon
, "%c Mouse #%d: %s\n",
474 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
475 index
, cursor
->qemu_put_mouse_event_name
);
477 cursor
= cursor
->next
;
481 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
483 QEMUPutMouseEntry
*cursor
;
485 int index
= qdict_get_int(qdict
, "index");
487 if (!qemu_put_mouse_event_head
) {
488 monitor_printf(mon
, "No mouse devices connected\n");
492 cursor
= qemu_put_mouse_event_head
;
493 while (cursor
!= NULL
&& index
!= i
) {
495 cursor
= cursor
->next
;
499 qemu_put_mouse_event_current
= cursor
;
501 monitor_printf(mon
, "Mouse at given index not found\n");
504 /* compute with 96 bit intermediate result: (a*b)/c */
505 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
510 #ifdef HOST_WORDS_BIGENDIAN
520 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
521 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
524 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
528 /***********************************************************/
529 /* real time host monotonic timer */
533 static int64_t clock_freq
;
535 static void init_get_clock(void)
539 ret
= QueryPerformanceFrequency(&freq
);
541 fprintf(stderr
, "Could not calibrate ticks\n");
544 clock_freq
= freq
.QuadPart
;
547 static int64_t get_clock(void)
550 QueryPerformanceCounter(&ti
);
551 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
556 static int use_rt_clock
;
558 static void init_get_clock(void)
561 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
562 || defined(__DragonFly__)
565 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
572 static int64_t get_clock(void)
574 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
575 || defined(__DragonFly__)
578 clock_gettime(CLOCK_MONOTONIC
, &ts
);
579 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
583 /* XXX: using gettimeofday leads to problems if the date
584 changes, so it should be avoided. */
586 gettimeofday(&tv
, NULL
);
587 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
592 /* Return the virtual CPU time, based on the instruction counter. */
593 static int64_t cpu_get_icount(void)
596 CPUState
*env
= cpu_single_env
;;
597 icount
= qemu_icount
;
600 fprintf(stderr
, "Bad clock read\n");
601 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
603 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
606 /***********************************************************/
607 /* guest cycle counter */
609 typedef struct TimersState
{
610 int64_t cpu_ticks_prev
;
611 int64_t cpu_ticks_offset
;
612 int64_t cpu_clock_offset
;
613 int32_t cpu_ticks_enabled
;
617 TimersState timers_state
;
619 /* return the host CPU cycle counter and handle stop/restart */
620 int64_t cpu_get_ticks(void)
623 return cpu_get_icount();
625 if (!timers_state
.cpu_ticks_enabled
) {
626 return timers_state
.cpu_ticks_offset
;
629 ticks
= cpu_get_real_ticks();
630 if (timers_state
.cpu_ticks_prev
> ticks
) {
631 /* Note: non increasing ticks may happen if the host uses
633 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
635 timers_state
.cpu_ticks_prev
= ticks
;
636 return ticks
+ timers_state
.cpu_ticks_offset
;
640 /* return the host CPU monotonic timer and handle stop/restart */
641 static int64_t cpu_get_clock(void)
644 if (!timers_state
.cpu_ticks_enabled
) {
645 return timers_state
.cpu_clock_offset
;
648 return ti
+ timers_state
.cpu_clock_offset
;
652 /* enable cpu_get_ticks() */
653 void cpu_enable_ticks(void)
655 if (!timers_state
.cpu_ticks_enabled
) {
656 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
657 timers_state
.cpu_clock_offset
-= get_clock();
658 timers_state
.cpu_ticks_enabled
= 1;
662 /* disable cpu_get_ticks() : the clock is stopped. You must not call
663 cpu_get_ticks() after that. */
664 void cpu_disable_ticks(void)
666 if (timers_state
.cpu_ticks_enabled
) {
667 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
668 timers_state
.cpu_clock_offset
= cpu_get_clock();
669 timers_state
.cpu_ticks_enabled
= 0;
673 /***********************************************************/
676 #define QEMU_TIMER_REALTIME 0
677 #define QEMU_TIMER_VIRTUAL 1
681 /* XXX: add frequency */
689 struct QEMUTimer
*next
;
692 struct qemu_alarm_timer
{
696 int (*start
)(struct qemu_alarm_timer
*t
);
697 void (*stop
)(struct qemu_alarm_timer
*t
);
698 void (*rearm
)(struct qemu_alarm_timer
*t
);
702 #define ALARM_FLAG_DYNTICKS 0x1
703 #define ALARM_FLAG_EXPIRED 0x2
705 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
707 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
710 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
712 if (!alarm_has_dynticks(t
))
718 /* TODO: MIN_TIMER_REARM_US should be optimized */
719 #define MIN_TIMER_REARM_US 250
721 static struct qemu_alarm_timer
*alarm_timer
;
725 struct qemu_alarm_win32
{
728 } alarm_win32_data
= {0, -1};
730 static int win32_start_timer(struct qemu_alarm_timer
*t
);
731 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
732 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
736 static int unix_start_timer(struct qemu_alarm_timer
*t
);
737 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
741 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
742 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
743 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
745 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
746 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
748 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
749 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
751 #endif /* __linux__ */
755 /* Correlation between real and virtual time is always going to be
756 fairly approximate, so ignore small variation.
757 When the guest is idle real and virtual time will be aligned in
759 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
761 static void icount_adjust(void)
766 static int64_t last_delta
;
767 /* If the VM is not running, then do nothing. */
771 cur_time
= cpu_get_clock();
772 cur_icount
= qemu_get_clock(vm_clock
);
773 delta
= cur_icount
- cur_time
;
774 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
776 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
777 && icount_time_shift
> 0) {
778 /* The guest is getting too far ahead. Slow time down. */
782 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
783 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
784 /* The guest is getting too far behind. Speed time up. */
788 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
791 static void icount_adjust_rt(void * opaque
)
793 qemu_mod_timer(icount_rt_timer
,
794 qemu_get_clock(rt_clock
) + 1000);
798 static void icount_adjust_vm(void * opaque
)
800 qemu_mod_timer(icount_vm_timer
,
801 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
805 static void init_icount_adjust(void)
807 /* Have both realtime and virtual time triggers for speed adjustment.
808 The realtime trigger catches emulated time passing too slowly,
809 the virtual time trigger catches emulated time passing too fast.
810 Realtime triggers occur even when idle, so use them less frequently
812 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
813 qemu_mod_timer(icount_rt_timer
,
814 qemu_get_clock(rt_clock
) + 1000);
815 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
816 qemu_mod_timer(icount_vm_timer
,
817 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
820 static struct qemu_alarm_timer alarm_timers
[] = {
823 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
824 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
825 /* HPET - if available - is preferred */
826 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
827 /* ...otherwise try RTC */
828 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
830 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
832 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
833 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
834 {"win32", 0, win32_start_timer
,
835 win32_stop_timer
, NULL
, &alarm_win32_data
},
840 static void show_available_alarms(void)
844 printf("Available alarm timers, in order of precedence:\n");
845 for (i
= 0; alarm_timers
[i
].name
; i
++)
846 printf("%s\n", alarm_timers
[i
].name
);
849 static void configure_alarms(char const *opt
)
853 int count
= ARRAY_SIZE(alarm_timers
) - 1;
856 struct qemu_alarm_timer tmp
;
858 if (!strcmp(opt
, "?")) {
859 show_available_alarms();
863 arg
= qemu_strdup(opt
);
865 /* Reorder the array */
866 name
= strtok(arg
, ",");
868 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
869 if (!strcmp(alarm_timers
[i
].name
, name
))
874 fprintf(stderr
, "Unknown clock %s\n", name
);
883 tmp
= alarm_timers
[i
];
884 alarm_timers
[i
] = alarm_timers
[cur
];
885 alarm_timers
[cur
] = tmp
;
889 name
= strtok(NULL
, ",");
895 /* Disable remaining timers */
896 for (i
= cur
; i
< count
; i
++)
897 alarm_timers
[i
].name
= NULL
;
899 show_available_alarms();
907 static QEMUTimer
*active_timers
[2];
909 static QEMUClock
*qemu_new_clock(int type
)
912 clock
= qemu_mallocz(sizeof(QEMUClock
));
917 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
921 ts
= qemu_mallocz(sizeof(QEMUTimer
));
928 void qemu_free_timer(QEMUTimer
*ts
)
933 /* stop a timer, but do not dealloc it */
934 void qemu_del_timer(QEMUTimer
*ts
)
938 /* NOTE: this code must be signal safe because
939 qemu_timer_expired() can be called from a signal. */
940 pt
= &active_timers
[ts
->clock
->type
];
953 /* modify the current timer so that it will be fired when current_time
954 >= expire_time. The corresponding callback will be called. */
955 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
961 /* add the timer in the sorted list */
962 /* NOTE: this code must be signal safe because
963 qemu_timer_expired() can be called from a signal. */
964 pt
= &active_timers
[ts
->clock
->type
];
969 if (t
->expire_time
> expire_time
)
973 ts
->expire_time
= expire_time
;
977 /* Rearm if necessary */
978 if (pt
== &active_timers
[ts
->clock
->type
]) {
979 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
980 qemu_rearm_alarm_timer(alarm_timer
);
982 /* Interrupt execution to force deadline recalculation. */
988 int qemu_timer_pending(QEMUTimer
*ts
)
991 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
998 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1002 return (timer_head
->expire_time
<= current_time
);
1005 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1011 if (!ts
|| ts
->expire_time
> current_time
)
1013 /* remove timer from the list before calling the callback */
1014 *ptimer_head
= ts
->next
;
1017 /* run the callback (the timer list can be modified) */
1022 int64_t qemu_get_clock(QEMUClock
*clock
)
1024 switch(clock
->type
) {
1025 case QEMU_TIMER_REALTIME
:
1026 return get_clock() / 1000000;
1028 case QEMU_TIMER_VIRTUAL
:
1030 return cpu_get_icount();
1032 return cpu_get_clock();
1037 static void init_timers(void)
1040 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1041 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1045 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1047 uint64_t expire_time
;
1049 if (qemu_timer_pending(ts
)) {
1050 expire_time
= ts
->expire_time
;
1054 qemu_put_be64(f
, expire_time
);
1057 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1059 uint64_t expire_time
;
1061 expire_time
= qemu_get_be64(f
);
1062 if (expire_time
!= -1) {
1063 qemu_mod_timer(ts
, expire_time
);
1069 static const VMStateDescription vmstate_timers
= {
1072 .minimum_version_id
= 1,
1073 .minimum_version_id_old
= 1,
1074 .fields
= (VMStateField
[]) {
1075 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1076 VMSTATE_INT64(dummy
, TimersState
),
1077 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1078 VMSTATE_END_OF_LIST()
1082 static void qemu_event_increment(void);
1085 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1086 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1089 static void host_alarm_handler(int host_signum
)
1093 #define DISP_FREQ 1000
1095 static int64_t delta_min
= INT64_MAX
;
1096 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1098 ti
= qemu_get_clock(vm_clock
);
1099 if (last_clock
!= 0) {
1100 delta
= ti
- last_clock
;
1101 if (delta
< delta_min
)
1103 if (delta
> delta_max
)
1106 if (++count
== DISP_FREQ
) {
1107 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1108 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1109 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1110 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1111 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1113 delta_min
= INT64_MAX
;
1121 if (alarm_has_dynticks(alarm_timer
) ||
1123 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1124 qemu_get_clock(vm_clock
))) ||
1125 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1126 qemu_get_clock(rt_clock
))) {
1127 qemu_event_increment();
1128 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1130 #ifndef CONFIG_IOTHREAD
1132 /* stop the currently executing cpu because a timer occured */
1136 timer_alarm_pending
= 1;
1137 qemu_notify_event();
1141 static int64_t qemu_next_deadline(void)
1145 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1146 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1147 qemu_get_clock(vm_clock
);
1149 /* To avoid problems with overflow limit this to 2^32. */
1159 #if defined(__linux__) || defined(_WIN32)
1160 static uint64_t qemu_next_deadline_dyntick(void)
1168 delta
= (qemu_next_deadline() + 999) / 1000;
1170 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1171 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1172 qemu_get_clock(rt_clock
))*1000;
1173 if (rtdelta
< delta
)
1177 if (delta
< MIN_TIMER_REARM_US
)
1178 delta
= MIN_TIMER_REARM_US
;
1186 /* Sets a specific flag */
1187 static int fcntl_setfl(int fd
, int flag
)
1191 flags
= fcntl(fd
, F_GETFL
);
1195 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1201 #if defined(__linux__)
1203 #define RTC_FREQ 1024
1205 static void enable_sigio_timer(int fd
)
1207 struct sigaction act
;
1210 sigfillset(&act
.sa_mask
);
1212 act
.sa_handler
= host_alarm_handler
;
1214 sigaction(SIGIO
, &act
, NULL
);
1215 fcntl_setfl(fd
, O_ASYNC
);
1216 fcntl(fd
, F_SETOWN
, getpid());
1219 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1221 struct hpet_info info
;
1224 fd
= open("/dev/hpet", O_RDONLY
);
1229 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1231 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1232 "error, but for better emulation accuracy type:\n"
1233 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1237 /* Check capabilities */
1238 r
= ioctl(fd
, HPET_INFO
, &info
);
1242 /* Enable periodic mode */
1243 r
= ioctl(fd
, HPET_EPI
, 0);
1244 if (info
.hi_flags
&& (r
< 0))
1247 /* Enable interrupt */
1248 r
= ioctl(fd
, HPET_IE_ON
, 0);
1252 enable_sigio_timer(fd
);
1253 t
->priv
= (void *)(long)fd
;
1261 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1263 int fd
= (long)t
->priv
;
1268 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1271 unsigned long current_rtc_freq
= 0;
1273 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1276 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1277 if (current_rtc_freq
!= RTC_FREQ
&&
1278 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1279 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1280 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1281 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1284 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1290 enable_sigio_timer(rtc_fd
);
1292 t
->priv
= (void *)(long)rtc_fd
;
1297 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1299 int rtc_fd
= (long)t
->priv
;
1304 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1308 struct sigaction act
;
1310 sigfillset(&act
.sa_mask
);
1312 act
.sa_handler
= host_alarm_handler
;
1314 sigaction(SIGALRM
, &act
, NULL
);
1317 * Initialize ev struct to 0 to avoid valgrind complaining
1318 * about uninitialized data in timer_create call
1320 memset(&ev
, 0, sizeof(ev
));
1321 ev
.sigev_value
.sival_int
= 0;
1322 ev
.sigev_notify
= SIGEV_SIGNAL
;
1323 ev
.sigev_signo
= SIGALRM
;
1325 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1326 perror("timer_create");
1328 /* disable dynticks */
1329 fprintf(stderr
, "Dynamic Ticks disabled\n");
1334 t
->priv
= (void *)(long)host_timer
;
1339 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1341 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1343 timer_delete(host_timer
);
1346 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1348 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1349 struct itimerspec timeout
;
1350 int64_t nearest_delta_us
= INT64_MAX
;
1353 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1354 !active_timers
[QEMU_TIMER_VIRTUAL
])
1357 nearest_delta_us
= qemu_next_deadline_dyntick();
1359 /* check whether a timer is already running */
1360 if (timer_gettime(host_timer
, &timeout
)) {
1362 fprintf(stderr
, "Internal timer error: aborting\n");
1365 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1366 if (current_us
&& current_us
<= nearest_delta_us
)
1369 timeout
.it_interval
.tv_sec
= 0;
1370 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1371 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1372 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1373 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1375 fprintf(stderr
, "Internal timer error: aborting\n");
1380 #endif /* defined(__linux__) */
1382 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1384 struct sigaction act
;
1385 struct itimerval itv
;
1389 sigfillset(&act
.sa_mask
);
1391 act
.sa_handler
= host_alarm_handler
;
1393 sigaction(SIGALRM
, &act
, NULL
);
1395 itv
.it_interval
.tv_sec
= 0;
1396 /* for i386 kernel 2.6 to get 1 ms */
1397 itv
.it_interval
.tv_usec
= 999;
1398 itv
.it_value
.tv_sec
= 0;
1399 itv
.it_value
.tv_usec
= 10 * 1000;
1401 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1408 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1410 struct itimerval itv
;
1412 memset(&itv
, 0, sizeof(itv
));
1413 setitimer(ITIMER_REAL
, &itv
, NULL
);
1416 #endif /* !defined(_WIN32) */
1421 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1424 struct qemu_alarm_win32
*data
= t
->priv
;
1427 memset(&tc
, 0, sizeof(tc
));
1428 timeGetDevCaps(&tc
, sizeof(tc
));
1430 if (data
->period
< tc
.wPeriodMin
)
1431 data
->period
= tc
.wPeriodMin
;
1433 timeBeginPeriod(data
->period
);
1435 flags
= TIME_CALLBACK_FUNCTION
;
1436 if (alarm_has_dynticks(t
))
1437 flags
|= TIME_ONESHOT
;
1439 flags
|= TIME_PERIODIC
;
1441 data
->timerId
= timeSetEvent(1, // interval (ms)
1442 data
->period
, // resolution
1443 host_alarm_handler
, // function
1444 (DWORD
)t
, // parameter
1447 if (!data
->timerId
) {
1448 perror("Failed to initialize win32 alarm timer");
1449 timeEndPeriod(data
->period
);
1456 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1458 struct qemu_alarm_win32
*data
= t
->priv
;
1460 timeKillEvent(data
->timerId
);
1461 timeEndPeriod(data
->period
);
1464 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1466 struct qemu_alarm_win32
*data
= t
->priv
;
1467 uint64_t nearest_delta_us
;
1469 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1470 !active_timers
[QEMU_TIMER_VIRTUAL
])
1473 nearest_delta_us
= qemu_next_deadline_dyntick();
1474 nearest_delta_us
/= 1000;
1476 timeKillEvent(data
->timerId
);
1478 data
->timerId
= timeSetEvent(1,
1482 TIME_ONESHOT
| TIME_PERIODIC
);
1484 if (!data
->timerId
) {
1485 perror("Failed to re-arm win32 alarm timer");
1487 timeEndPeriod(data
->period
);
1494 static int init_timer_alarm(void)
1496 struct qemu_alarm_timer
*t
= NULL
;
1499 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1500 t
= &alarm_timers
[i
];
1520 static void quit_timers(void)
1522 alarm_timer
->stop(alarm_timer
);
1526 /***********************************************************/
1527 /* host time/date access */
1528 void qemu_get_timedate(struct tm
*tm
, int offset
)
1535 if (rtc_date_offset
== -1) {
1539 ret
= localtime(&ti
);
1541 ti
-= rtc_date_offset
;
1545 memcpy(tm
, ret
, sizeof(struct tm
));
1548 int qemu_timedate_diff(struct tm
*tm
)
1552 if (rtc_date_offset
== -1)
1554 seconds
= mktimegm(tm
);
1556 seconds
= mktime(tm
);
1558 seconds
= mktimegm(tm
) + rtc_date_offset
;
1560 return seconds
- time(NULL
);
1564 static void socket_cleanup(void)
1569 static int socket_init(void)
1574 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1576 err
= WSAGetLastError();
1577 fprintf(stderr
, "WSAStartup: %d\n", err
);
1580 atexit(socket_cleanup
);
1585 /***********************************************************/
1586 /* Bluetooth support */
1589 static struct HCIInfo
*hci_table
[MAX_NICS
];
1591 static struct bt_vlan_s
{
1592 struct bt_scatternet_s net
;
1594 struct bt_vlan_s
*next
;
1597 /* find or alloc a new bluetooth "VLAN" */
1598 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1600 struct bt_vlan_s
**pvlan
, *vlan
;
1601 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1605 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1607 pvlan
= &first_bt_vlan
;
1608 while (*pvlan
!= NULL
)
1609 pvlan
= &(*pvlan
)->next
;
1614 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1618 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1623 static struct HCIInfo null_hci
= {
1624 .cmd_send
= null_hci_send
,
1625 .sco_send
= null_hci_send
,
1626 .acl_send
= null_hci_send
,
1627 .bdaddr_set
= null_hci_addr_set
,
1630 struct HCIInfo
*qemu_next_hci(void)
1632 if (cur_hci
== nb_hcis
)
1635 return hci_table
[cur_hci
++];
1638 static struct HCIInfo
*hci_init(const char *str
)
1641 struct bt_scatternet_s
*vlan
= 0;
1643 if (!strcmp(str
, "null"))
1646 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1648 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1649 else if (!strncmp(str
, "hci", 3)) {
1652 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1653 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1658 vlan
= qemu_find_bt_vlan(0);
1660 return bt_new_hci(vlan
);
1663 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1668 static int bt_hci_parse(const char *str
)
1670 struct HCIInfo
*hci
;
1673 if (nb_hcis
>= MAX_NICS
) {
1674 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1678 hci
= hci_init(str
);
1687 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1688 hci
->bdaddr_set(hci
, bdaddr
.b
);
1690 hci_table
[nb_hcis
++] = hci
;
1695 static void bt_vhci_add(int vlan_id
)
1697 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1700 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1701 "an empty scatternet %i\n", vlan_id
);
1703 bt_vhci_init(bt_new_hci(vlan
));
1706 static struct bt_device_s
*bt_device_add(const char *opt
)
1708 struct bt_scatternet_s
*vlan
;
1710 char *endp
= strstr(opt
, ",vlan=");
1711 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1714 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1717 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1719 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1724 vlan
= qemu_find_bt_vlan(vlan_id
);
1727 fprintf(stderr
, "qemu: warning: adding a slave device to "
1728 "an empty scatternet %i\n", vlan_id
);
1730 if (!strcmp(devname
, "keyboard"))
1731 return bt_keyboard_init(vlan
);
1733 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1737 static int bt_parse(const char *opt
)
1739 const char *endp
, *p
;
1742 if (strstart(opt
, "hci", &endp
)) {
1743 if (!*endp
|| *endp
== ',') {
1745 if (!strstart(endp
, ",vlan=", 0))
1748 return bt_hci_parse(opt
);
1750 } else if (strstart(opt
, "vhci", &endp
)) {
1751 if (!*endp
|| *endp
== ',') {
1753 if (strstart(endp
, ",vlan=", &p
)) {
1754 vlan
= strtol(p
, (char **) &endp
, 0);
1756 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1760 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1769 } else if (strstart(opt
, "device:", &endp
))
1770 return !bt_device_add(endp
);
1772 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1776 /***********************************************************/
1777 /* QEMU Block devices */
1779 #define HD_ALIAS "index=%d,media=disk"
1780 #define CDROM_ALIAS "index=2,media=cdrom"
1781 #define FD_ALIAS "index=%d,if=floppy"
1782 #define PFLASH_ALIAS "if=pflash"
1783 #define MTD_ALIAS "if=mtd"
1784 #define SD_ALIAS "index=0,if=sd"
1786 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1793 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1796 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1798 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1799 __FUNCTION__
, optstr
);
1803 qemu_opt_set(opts
, "file", file
);
1807 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1811 /* seek interface, bus and unit */
1813 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1814 if (dinfo
->type
== type
&&
1815 dinfo
->bus
== bus
&&
1816 dinfo
->unit
== unit
)
1823 DriveInfo
*drive_get_by_id(const char *id
)
1827 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1828 if (strcmp(id
, dinfo
->id
))
1835 int drive_get_max_bus(BlockInterfaceType type
)
1841 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1842 if(dinfo
->type
== type
&&
1843 dinfo
->bus
> max_bus
)
1844 max_bus
= dinfo
->bus
;
1849 const char *drive_get_serial(BlockDriverState
*bdrv
)
1853 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1854 if (dinfo
->bdrv
== bdrv
)
1855 return dinfo
->serial
;
1861 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1865 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1866 if (dinfo
->bdrv
== bdrv
)
1867 return dinfo
->onerror
;
1870 return BLOCK_ERR_STOP_ENOSPC
;
1873 static void bdrv_format_print(void *opaque
, const char *name
)
1875 fprintf(stderr
, " %s", name
);
1878 void drive_uninit(BlockDriverState
*bdrv
)
1882 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1883 if (dinfo
->bdrv
!= bdrv
)
1885 qemu_opts_del(dinfo
->opts
);
1886 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1892 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1896 const char *file
= NULL
;
1899 const char *mediastr
= "";
1900 BlockInterfaceType type
;
1901 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1902 int bus_id
, unit_id
;
1903 int cyls
, heads
, secs
, translation
;
1904 BlockDriver
*drv
= NULL
;
1905 QEMUMachine
*machine
= opaque
;
1910 int bdrv_flags
, onerror
;
1911 const char *devaddr
;
1917 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1920 if (machine
&& machine
->use_scsi
) {
1922 max_devs
= MAX_SCSI_DEVS
;
1923 pstrcpy(devname
, sizeof(devname
), "scsi");
1926 max_devs
= MAX_IDE_DEVS
;
1927 pstrcpy(devname
, sizeof(devname
), "ide");
1931 /* extract parameters */
1932 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1933 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1934 index
= qemu_opt_get_number(opts
, "index", -1);
1936 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1937 heads
= qemu_opt_get_number(opts
, "heads", 0);
1938 secs
= qemu_opt_get_number(opts
, "secs", 0);
1940 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1942 file
= qemu_opt_get(opts
, "file");
1943 serial
= qemu_opt_get(opts
, "serial");
1945 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1946 pstrcpy(devname
, sizeof(devname
), buf
);
1947 if (!strcmp(buf
, "ide")) {
1949 max_devs
= MAX_IDE_DEVS
;
1950 } else if (!strcmp(buf
, "scsi")) {
1952 max_devs
= MAX_SCSI_DEVS
;
1953 } else if (!strcmp(buf
, "floppy")) {
1956 } else if (!strcmp(buf
, "pflash")) {
1959 } else if (!strcmp(buf
, "mtd")) {
1962 } else if (!strcmp(buf
, "sd")) {
1965 } else if (!strcmp(buf
, "virtio")) {
1968 } else if (!strcmp(buf
, "xen")) {
1971 } else if (!strcmp(buf
, "none")) {
1975 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1980 if (cyls
|| heads
|| secs
) {
1981 if (cyls
< 1 || cyls
> 16383) {
1982 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1985 if (heads
< 1 || heads
> 16) {
1986 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1989 if (secs
< 1 || secs
> 63) {
1990 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
1995 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
1998 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2002 if (!strcmp(buf
, "none"))
2003 translation
= BIOS_ATA_TRANSLATION_NONE
;
2004 else if (!strcmp(buf
, "lba"))
2005 translation
= BIOS_ATA_TRANSLATION_LBA
;
2006 else if (!strcmp(buf
, "auto"))
2007 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2009 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2014 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2015 if (!strcmp(buf
, "disk")) {
2017 } else if (!strcmp(buf
, "cdrom")) {
2018 if (cyls
|| secs
|| heads
) {
2020 "qemu: '%s' invalid physical CHS format\n", buf
);
2023 media
= MEDIA_CDROM
;
2025 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2030 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2031 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2033 else if (!strcmp(buf
, "writethrough"))
2035 else if (!strcmp(buf
, "writeback"))
2038 fprintf(stderr
, "qemu: invalid cache option\n");
2043 #ifdef CONFIG_LINUX_AIO
2044 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2045 if (!strcmp(buf
, "threads"))
2047 else if (!strcmp(buf
, "native"))
2050 fprintf(stderr
, "qemu: invalid aio option\n");
2056 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2057 if (strcmp(buf
, "?") == 0) {
2058 fprintf(stderr
, "qemu: Supported formats:");
2059 bdrv_iterate_format(bdrv_format_print
, NULL
);
2060 fprintf(stderr
, "\n");
2063 drv
= bdrv_find_format(buf
);
2065 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2070 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2071 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2072 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2073 fprintf(stderr
, "werror is no supported by this format\n");
2076 if (!strcmp(buf
, "ignore"))
2077 onerror
= BLOCK_ERR_IGNORE
;
2078 else if (!strcmp(buf
, "enospc"))
2079 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2080 else if (!strcmp(buf
, "stop"))
2081 onerror
= BLOCK_ERR_STOP_ANY
;
2082 else if (!strcmp(buf
, "report"))
2083 onerror
= BLOCK_ERR_REPORT
;
2085 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2090 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2091 if (type
!= IF_VIRTIO
) {
2092 fprintf(stderr
, "addr is not supported\n");
2097 /* compute bus and unit according index */
2100 if (bus_id
!= 0 || unit_id
!= -1) {
2102 "qemu: index cannot be used with bus and unit\n");
2110 unit_id
= index
% max_devs
;
2111 bus_id
= index
/ max_devs
;
2115 /* if user doesn't specify a unit_id,
2116 * try to find the first free
2119 if (unit_id
== -1) {
2121 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2123 if (max_devs
&& unit_id
>= max_devs
) {
2124 unit_id
-= max_devs
;
2132 if (max_devs
&& unit_id
>= max_devs
) {
2133 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2134 unit_id
, max_devs
- 1);
2139 * ignore multiple definitions
2142 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2149 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2150 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2151 dinfo
->id
= qemu_strdup(buf
);
2153 /* no id supplied -> create one */
2154 dinfo
->id
= qemu_mallocz(32);
2155 if (type
== IF_IDE
|| type
== IF_SCSI
)
2156 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2158 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2159 devname
, bus_id
, mediastr
, unit_id
);
2161 snprintf(dinfo
->id
, 32, "%s%s%i",
2162 devname
, mediastr
, unit_id
);
2164 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2165 dinfo
->devaddr
= devaddr
;
2167 dinfo
->bus
= bus_id
;
2168 dinfo
->unit
= unit_id
;
2169 dinfo
->onerror
= onerror
;
2172 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2173 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2182 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2183 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2187 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2192 /* FIXME: This isn't really a floppy, but it's a reasonable
2195 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2202 /* add virtio block device */
2203 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2204 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2205 qemu_opt_set(opts
, "drive", dinfo
->id
);
2207 qemu_opt_set(opts
, "addr", devaddr
);
2218 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2219 cache
= 2; /* always use write-back with snapshot */
2221 if (cache
== 0) /* no caching */
2222 bdrv_flags
|= BDRV_O_NOCACHE
;
2223 else if (cache
== 2) /* write-back */
2224 bdrv_flags
|= BDRV_O_CACHE_WB
;
2227 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2229 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2232 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2233 fprintf(stderr
, "qemu: could not open disk image %s\n",
2238 if (bdrv_key_required(dinfo
->bdrv
))
2244 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2246 QEMUMachine
*machine
= opaque
;
2247 int fatal_error
= 0;
2249 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2256 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2258 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2259 qemu_opt_set(opts
, "snapshot", "on");
2264 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2266 boot_set_handler
= func
;
2267 boot_set_opaque
= opaque
;
2270 int qemu_boot_set(const char *boot_devices
)
2272 if (!boot_set_handler
) {
2275 return boot_set_handler(boot_set_opaque
, boot_devices
);
2278 static int parse_bootdevices(char *devices
)
2280 /* We just do some generic consistency checks */
2284 for (p
= devices
; *p
!= '\0'; p
++) {
2285 /* Allowed boot devices are:
2286 * a-b: floppy disk drives
2287 * c-f: IDE disk drives
2288 * g-m: machine implementation dependant drives
2289 * n-p: network devices
2290 * It's up to each machine implementation to check if the given boot
2291 * devices match the actual hardware implementation and firmware
2294 if (*p
< 'a' || *p
> 'p') {
2295 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2298 if (bitmap
& (1 << (*p
- 'a'))) {
2299 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2302 bitmap
|= 1 << (*p
- 'a');
2307 static void restore_boot_devices(void *opaque
)
2309 char *standard_boot_devices
= opaque
;
2311 qemu_boot_set(standard_boot_devices
);
2313 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2314 qemu_free(standard_boot_devices
);
2317 static void numa_add(const char *optarg
)
2321 unsigned long long value
, endvalue
;
2324 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2325 if (!strcmp(option
, "node")) {
2326 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2327 nodenr
= nb_numa_nodes
;
2329 nodenr
= strtoull(option
, NULL
, 10);
2332 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2333 node_mem
[nodenr
] = 0;
2335 value
= strtoull(option
, &endptr
, 0);
2337 case 0: case 'M': case 'm':
2344 node_mem
[nodenr
] = value
;
2346 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2347 node_cpumask
[nodenr
] = 0;
2349 value
= strtoull(option
, &endptr
, 10);
2352 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2354 if (*endptr
== '-') {
2355 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2356 if (endvalue
>= 63) {
2359 "only 63 CPUs in NUMA mode supported.\n");
2361 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2366 node_cpumask
[nodenr
] = value
;
2373 static void smp_parse(const char *optarg
)
2375 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2379 smp
= strtoul(optarg
, &endptr
, 10);
2380 if (endptr
!= optarg
) {
2381 if (*endptr
== ',') {
2385 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2386 sockets
= strtoull(option
, NULL
, 10);
2387 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2388 cores
= strtoull(option
, NULL
, 10);
2389 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2390 threads
= strtoull(option
, NULL
, 10);
2391 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2392 max_cpus
= strtoull(option
, NULL
, 10);
2394 /* compute missing values, prefer sockets over cores over threads */
2395 if (smp
== 0 || sockets
== 0) {
2396 sockets
= sockets
> 0 ? sockets
: 1;
2397 cores
= cores
> 0 ? cores
: 1;
2398 threads
= threads
> 0 ? threads
: 1;
2400 smp
= cores
* threads
* sockets
;
2402 sockets
= smp
/ (cores
* threads
);
2406 threads
= threads
> 0 ? threads
: 1;
2407 cores
= smp
/ (sockets
* threads
);
2410 sockets
= smp
/ (cores
* threads
);
2412 threads
= smp
/ (cores
* sockets
);
2417 smp_cores
= cores
> 0 ? cores
: 1;
2418 smp_threads
= threads
> 0 ? threads
: 1;
2420 max_cpus
= smp_cpus
;
2423 /***********************************************************/
2426 static void usb_msd_password_cb(void *opaque
, int err
)
2428 USBDevice
*dev
= opaque
;
2431 usb_device_attach(dev
);
2433 dev
->info
->handle_destroy(dev
);
2442 .qdev
= "QEMU USB Mouse",
2445 .qdev
= "QEMU USB Tablet",
2448 .qdev
= "QEMU USB Keyboard",
2450 .name
= "wacom-tablet",
2451 .qdev
= "QEMU PenPartner Tablet",
2455 static int usb_device_add(const char *devname
, int is_hotplug
)
2458 USBBus
*bus
= usb_bus_find(-1 /* any */);
2459 USBDevice
*dev
= NULL
;
2465 /* simple devices which don't need extra care */
2466 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2467 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2469 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2473 /* the other ones */
2474 if (strstart(devname
, "host:", &p
)) {
2475 dev
= usb_host_device_open(p
);
2476 } else if (strstart(devname
, "disk:", &p
)) {
2477 BlockDriverState
*bs
;
2479 dev
= usb_msd_init(p
);
2482 bs
= usb_msd_get_bdrv(dev
);
2483 if (bdrv_key_required(bs
)) {
2486 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2491 } else if (strstart(devname
, "serial:", &p
)) {
2492 dev
= usb_serial_init(p
);
2493 #ifdef CONFIG_BRLAPI
2494 } else if (!strcmp(devname
, "braille")) {
2495 dev
= usb_baum_init();
2497 } else if (strstart(devname
, "net:", &p
)) {
2500 if (net_client_init(NULL
, "nic", p
) < 0)
2502 nd_table
[nic
].model
= "usb";
2503 dev
= usb_net_init(&nd_table
[nic
]);
2504 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2505 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2506 bt_new_hci(qemu_find_bt_vlan(0)));
2517 static int usb_device_del(const char *devname
)
2522 if (strstart(devname
, "host:", &p
))
2523 return usb_host_device_close(p
);
2528 p
= strchr(devname
, '.');
2531 bus_num
= strtoul(devname
, NULL
, 0);
2532 addr
= strtoul(p
+ 1, NULL
, 0);
2534 return usb_device_delete_addr(bus_num
, addr
);
2537 static int usb_parse(const char *cmdline
)
2539 return usb_device_add(cmdline
, 0);
2542 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2544 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2547 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2549 usb_device_del(qdict_get_str(qdict
, "devname"));
2552 /***********************************************************/
2553 /* PCMCIA/Cardbus */
2555 static struct pcmcia_socket_entry_s
{
2556 PCMCIASocket
*socket
;
2557 struct pcmcia_socket_entry_s
*next
;
2558 } *pcmcia_sockets
= 0;
2560 void pcmcia_socket_register(PCMCIASocket
*socket
)
2562 struct pcmcia_socket_entry_s
*entry
;
2564 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2565 entry
->socket
= socket
;
2566 entry
->next
= pcmcia_sockets
;
2567 pcmcia_sockets
= entry
;
2570 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2572 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2574 ptr
= &pcmcia_sockets
;
2575 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2576 if (entry
->socket
== socket
) {
2582 void pcmcia_info(Monitor
*mon
)
2584 struct pcmcia_socket_entry_s
*iter
;
2586 if (!pcmcia_sockets
)
2587 monitor_printf(mon
, "No PCMCIA sockets\n");
2589 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2590 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2591 iter
->socket
->attached
? iter
->socket
->card_string
:
2595 /***********************************************************/
2596 /* register display */
2598 struct DisplayAllocator default_allocator
= {
2599 defaultallocator_create_displaysurface
,
2600 defaultallocator_resize_displaysurface
,
2601 defaultallocator_free_displaysurface
2604 void register_displaystate(DisplayState
*ds
)
2614 DisplayState
*get_displaystate(void)
2616 return display_state
;
2619 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2621 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2622 return ds
->allocator
;
2627 static void dumb_display_init(void)
2629 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2630 ds
->allocator
= &default_allocator
;
2631 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2632 register_displaystate(ds
);
2635 /***********************************************************/
2638 typedef struct IOHandlerRecord
{
2640 IOCanRWHandler
*fd_read_poll
;
2642 IOHandler
*fd_write
;
2645 /* temporary data */
2647 struct IOHandlerRecord
*next
;
2650 static IOHandlerRecord
*first_io_handler
;
2652 /* XXX: fd_read_poll should be suppressed, but an API change is
2653 necessary in the character devices to suppress fd_can_read(). */
2654 int qemu_set_fd_handler2(int fd
,
2655 IOCanRWHandler
*fd_read_poll
,
2657 IOHandler
*fd_write
,
2660 IOHandlerRecord
**pioh
, *ioh
;
2662 if (!fd_read
&& !fd_write
) {
2663 pioh
= &first_io_handler
;
2668 if (ioh
->fd
== fd
) {
2675 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2679 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2680 ioh
->next
= first_io_handler
;
2681 first_io_handler
= ioh
;
2684 ioh
->fd_read_poll
= fd_read_poll
;
2685 ioh
->fd_read
= fd_read
;
2686 ioh
->fd_write
= fd_write
;
2687 ioh
->opaque
= opaque
;
2693 int qemu_set_fd_handler(int fd
,
2695 IOHandler
*fd_write
,
2698 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2702 /***********************************************************/
2703 /* Polling handling */
2705 typedef struct PollingEntry
{
2708 struct PollingEntry
*next
;
2711 static PollingEntry
*first_polling_entry
;
2713 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2715 PollingEntry
**ppe
, *pe
;
2716 pe
= qemu_mallocz(sizeof(PollingEntry
));
2718 pe
->opaque
= opaque
;
2719 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2724 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2726 PollingEntry
**ppe
, *pe
;
2727 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2729 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2737 /***********************************************************/
2738 /* Wait objects support */
2739 typedef struct WaitObjects
{
2741 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2742 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2743 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2746 static WaitObjects wait_objects
= {0};
2748 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2750 WaitObjects
*w
= &wait_objects
;
2752 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2754 w
->events
[w
->num
] = handle
;
2755 w
->func
[w
->num
] = func
;
2756 w
->opaque
[w
->num
] = opaque
;
2761 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2764 WaitObjects
*w
= &wait_objects
;
2767 for (i
= 0; i
< w
->num
; i
++) {
2768 if (w
->events
[i
] == handle
)
2771 w
->events
[i
] = w
->events
[i
+ 1];
2772 w
->func
[i
] = w
->func
[i
+ 1];
2773 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2781 /***********************************************************/
2782 /* ram save/restore */
2784 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2785 #define RAM_SAVE_FLAG_COMPRESS 0x02
2786 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2787 #define RAM_SAVE_FLAG_PAGE 0x08
2788 #define RAM_SAVE_FLAG_EOS 0x10
2790 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2792 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2793 uint32_t *array
= (uint32_t *)page
;
2796 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2797 if (array
[i
] != val
)
2804 static int ram_save_block(QEMUFile
*f
)
2806 static ram_addr_t current_addr
= 0;
2807 ram_addr_t saved_addr
= current_addr
;
2808 ram_addr_t addr
= 0;
2811 while (addr
< last_ram_offset
) {
2812 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2815 cpu_physical_memory_reset_dirty(current_addr
,
2816 current_addr
+ TARGET_PAGE_SIZE
,
2817 MIGRATION_DIRTY_FLAG
);
2819 p
= qemu_get_ram_ptr(current_addr
);
2821 if (is_dup_page(p
, *p
)) {
2822 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2823 qemu_put_byte(f
, *p
);
2825 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2826 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2832 addr
+= TARGET_PAGE_SIZE
;
2833 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2839 static uint64_t bytes_transferred
= 0;
2841 static ram_addr_t
ram_save_remaining(void)
2844 ram_addr_t count
= 0;
2846 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2847 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2854 uint64_t ram_bytes_remaining(void)
2856 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2859 uint64_t ram_bytes_transferred(void)
2861 return bytes_transferred
;
2864 uint64_t ram_bytes_total(void)
2866 return last_ram_offset
;
2869 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2872 uint64_t bytes_transferred_last
;
2874 uint64_t expected_time
= 0;
2876 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2877 qemu_file_set_error(f
);
2882 /* Make sure all dirty bits are set */
2883 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2884 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2885 cpu_physical_memory_set_dirty(addr
);
2888 /* Enable dirty memory tracking */
2889 cpu_physical_memory_set_dirty_tracking(1);
2891 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2894 bytes_transferred_last
= bytes_transferred
;
2895 bwidth
= get_clock();
2897 while (!qemu_file_rate_limit(f
)) {
2900 ret
= ram_save_block(f
);
2901 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2902 if (ret
== 0) /* no more blocks */
2906 bwidth
= get_clock() - bwidth
;
2907 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2909 /* if we haven't transferred anything this round, force expected_time to a
2910 * a very high value, but without crashing */
2914 /* try transferring iterative blocks of memory */
2918 /* flush all remaining blocks regardless of rate limiting */
2919 while (ram_save_block(f
) != 0) {
2920 bytes_transferred
+= TARGET_PAGE_SIZE
;
2922 cpu_physical_memory_set_dirty_tracking(0);
2925 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2927 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2929 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2932 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2937 if (version_id
!= 3)
2941 addr
= qemu_get_be64(f
);
2943 flags
= addr
& ~TARGET_PAGE_MASK
;
2944 addr
&= TARGET_PAGE_MASK
;
2946 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2947 if (addr
!= last_ram_offset
)
2951 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2952 uint8_t ch
= qemu_get_byte(f
);
2953 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2956 (!kvm_enabled() || kvm_has_sync_mmu())) {
2957 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2960 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
2961 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2962 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2967 void qemu_service_io(void)
2969 qemu_notify_event();
2972 /***********************************************************/
2973 /* bottom halves (can be seen as timers which expire ASAP) */
2984 static QEMUBH
*first_bh
= NULL
;
2986 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
2989 bh
= qemu_mallocz(sizeof(QEMUBH
));
2991 bh
->opaque
= opaque
;
2992 bh
->next
= first_bh
;
2997 int qemu_bh_poll(void)
3003 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3004 if (!bh
->deleted
&& bh
->scheduled
) {
3013 /* remove deleted bhs */
3027 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3035 void qemu_bh_schedule(QEMUBH
*bh
)
3041 /* stop the currently executing CPU to execute the BH ASAP */
3042 qemu_notify_event();
3045 void qemu_bh_cancel(QEMUBH
*bh
)
3050 void qemu_bh_delete(QEMUBH
*bh
)
3056 static void qemu_bh_update_timeout(int *timeout
)
3060 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3061 if (!bh
->deleted
&& bh
->scheduled
) {
3063 /* idle bottom halves will be polled at least
3065 *timeout
= MIN(10, *timeout
);
3067 /* non-idle bottom halves will be executed
3076 /***********************************************************/
3077 /* machine registration */
3079 static QEMUMachine
*first_machine
= NULL
;
3080 QEMUMachine
*current_machine
= NULL
;
3082 int qemu_register_machine(QEMUMachine
*m
)
3085 pm
= &first_machine
;
3093 static QEMUMachine
*find_machine(const char *name
)
3097 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3098 if (!strcmp(m
->name
, name
))
3100 if (m
->alias
&& !strcmp(m
->alias
, name
))
3106 static QEMUMachine
*find_default_machine(void)
3110 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3111 if (m
->is_default
) {
3118 /***********************************************************/
3119 /* main execution loop */
3121 static void gui_update(void *opaque
)
3123 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3124 DisplayState
*ds
= opaque
;
3125 DisplayChangeListener
*dcl
= ds
->listeners
;
3129 while (dcl
!= NULL
) {
3130 if (dcl
->gui_timer_interval
&&
3131 dcl
->gui_timer_interval
< interval
)
3132 interval
= dcl
->gui_timer_interval
;
3135 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3138 static void nographic_update(void *opaque
)
3140 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3142 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3145 struct vm_change_state_entry
{
3146 VMChangeStateHandler
*cb
;
3148 QLIST_ENTRY (vm_change_state_entry
) entries
;
3151 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3153 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3156 VMChangeStateEntry
*e
;
3158 e
= qemu_mallocz(sizeof (*e
));
3162 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3166 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3168 QLIST_REMOVE (e
, entries
);
3172 static void vm_state_notify(int running
, int reason
)
3174 VMChangeStateEntry
*e
;
3176 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3177 e
->cb(e
->opaque
, running
, reason
);
3181 static void resume_all_vcpus(void);
3182 static void pause_all_vcpus(void);
3189 vm_state_notify(1, 0);
3190 qemu_rearm_alarm_timer(alarm_timer
);
3195 /* reset/shutdown handler */
3197 typedef struct QEMUResetEntry
{
3198 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3199 QEMUResetHandler
*func
;
3203 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3204 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3205 static int reset_requested
;
3206 static int shutdown_requested
;
3207 static int powerdown_requested
;
3208 static int debug_requested
;
3209 static int vmstop_requested
;
3211 int qemu_shutdown_requested(void)
3213 int r
= shutdown_requested
;
3214 shutdown_requested
= 0;
3218 int qemu_reset_requested(void)
3220 int r
= reset_requested
;
3221 reset_requested
= 0;
3225 int qemu_powerdown_requested(void)
3227 int r
= powerdown_requested
;
3228 powerdown_requested
= 0;
3232 static int qemu_debug_requested(void)
3234 int r
= debug_requested
;
3235 debug_requested
= 0;
3239 static int qemu_vmstop_requested(void)
3241 int r
= vmstop_requested
;
3242 vmstop_requested
= 0;
3246 static void do_vm_stop(int reason
)
3249 cpu_disable_ticks();
3252 vm_state_notify(0, reason
);
3256 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3258 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3261 re
->opaque
= opaque
;
3262 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3265 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3269 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3270 if (re
->func
== func
&& re
->opaque
== opaque
) {
3271 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3278 void qemu_system_reset(void)
3280 QEMUResetEntry
*re
, *nre
;
3282 /* reset all devices */
3283 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3284 re
->func(re
->opaque
);
3288 void qemu_system_reset_request(void)
3291 shutdown_requested
= 1;
3293 reset_requested
= 1;
3295 qemu_notify_event();
3298 void qemu_system_shutdown_request(void)
3300 shutdown_requested
= 1;
3301 qemu_notify_event();
3304 void qemu_system_powerdown_request(void)
3306 powerdown_requested
= 1;
3307 qemu_notify_event();
3310 #ifdef CONFIG_IOTHREAD
3311 static void qemu_system_vmstop_request(int reason
)
3313 vmstop_requested
= reason
;
3314 qemu_notify_event();
3319 static int io_thread_fd
= -1;
3321 static void qemu_event_increment(void)
3323 static const char byte
= 0;
3325 if (io_thread_fd
== -1)
3328 write(io_thread_fd
, &byte
, sizeof(byte
));
3331 static void qemu_event_read(void *opaque
)
3333 int fd
= (unsigned long)opaque
;
3336 /* Drain the notify pipe */
3339 len
= read(fd
, buffer
, sizeof(buffer
));
3340 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3343 static int qemu_event_init(void)
3352 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3356 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3360 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3361 (void *)(unsigned long)fds
[0]);
3363 io_thread_fd
= fds
[1];
3372 HANDLE qemu_event_handle
;
3374 static void dummy_event_handler(void *opaque
)
3378 static int qemu_event_init(void)
3380 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3381 if (!qemu_event_handle
) {
3382 perror("Failed CreateEvent");
3385 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3389 static void qemu_event_increment(void)
3391 SetEvent(qemu_event_handle
);
3395 static int cpu_can_run(CPUState
*env
)
3404 #ifndef CONFIG_IOTHREAD
3405 static int qemu_init_main_loop(void)
3407 return qemu_event_init();
3410 void qemu_init_vcpu(void *_env
)
3412 CPUState
*env
= _env
;
3416 env
->nr_cores
= smp_cores
;
3417 env
->nr_threads
= smp_threads
;
3421 int qemu_cpu_self(void *env
)
3426 static void resume_all_vcpus(void)
3430 static void pause_all_vcpus(void)
3434 void qemu_cpu_kick(void *env
)
3439 void qemu_notify_event(void)
3441 CPUState
*env
= cpu_single_env
;
3448 #define qemu_mutex_lock_iothread() do { } while (0)
3449 #define qemu_mutex_unlock_iothread() do { } while (0)
3451 void vm_stop(int reason
)
3456 #else /* CONFIG_IOTHREAD */
3458 #include "qemu-thread.h"
3460 QemuMutex qemu_global_mutex
;
3461 static QemuMutex qemu_fair_mutex
;
3463 static QemuThread io_thread
;
3465 static QemuThread
*tcg_cpu_thread
;
3466 static QemuCond
*tcg_halt_cond
;
3468 static int qemu_system_ready
;
3470 static QemuCond qemu_cpu_cond
;
3472 static QemuCond qemu_system_cond
;
3473 static QemuCond qemu_pause_cond
;
3475 static void block_io_signals(void);
3476 static void unblock_io_signals(void);
3477 static int tcg_has_work(void);
3479 static int qemu_init_main_loop(void)
3483 ret
= qemu_event_init();
3487 qemu_cond_init(&qemu_pause_cond
);
3488 qemu_mutex_init(&qemu_fair_mutex
);
3489 qemu_mutex_init(&qemu_global_mutex
);
3490 qemu_mutex_lock(&qemu_global_mutex
);
3492 unblock_io_signals();
3493 qemu_thread_self(&io_thread
);
3498 static void qemu_wait_io_event(CPUState
*env
)
3500 while (!tcg_has_work())
3501 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3503 qemu_mutex_unlock(&qemu_global_mutex
);
3506 * Users of qemu_global_mutex can be starved, having no chance
3507 * to acquire it since this path will get to it first.
3508 * So use another lock to provide fairness.
3510 qemu_mutex_lock(&qemu_fair_mutex
);
3511 qemu_mutex_unlock(&qemu_fair_mutex
);
3513 qemu_mutex_lock(&qemu_global_mutex
);
3517 qemu_cond_signal(&qemu_pause_cond
);
3521 static int qemu_cpu_exec(CPUState
*env
);
3523 static void *kvm_cpu_thread_fn(void *arg
)
3525 CPUState
*env
= arg
;
3528 qemu_thread_self(env
->thread
);
3532 /* signal CPU creation */
3533 qemu_mutex_lock(&qemu_global_mutex
);
3535 qemu_cond_signal(&qemu_cpu_cond
);
3537 /* and wait for machine initialization */
3538 while (!qemu_system_ready
)
3539 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3542 if (cpu_can_run(env
))
3544 qemu_wait_io_event(env
);
3550 static void tcg_cpu_exec(void);
3552 static void *tcg_cpu_thread_fn(void *arg
)
3554 CPUState
*env
= arg
;
3557 qemu_thread_self(env
->thread
);
3559 /* signal CPU creation */
3560 qemu_mutex_lock(&qemu_global_mutex
);
3561 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3563 qemu_cond_signal(&qemu_cpu_cond
);
3565 /* and wait for machine initialization */
3566 while (!qemu_system_ready
)
3567 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3571 qemu_wait_io_event(cur_cpu
);
3577 void qemu_cpu_kick(void *_env
)
3579 CPUState
*env
= _env
;
3580 qemu_cond_broadcast(env
->halt_cond
);
3582 qemu_thread_signal(env
->thread
, SIGUSR1
);
3585 int qemu_cpu_self(void *env
)
3587 return (cpu_single_env
!= NULL
);
3590 static void cpu_signal(int sig
)
3593 cpu_exit(cpu_single_env
);
3596 static void block_io_signals(void)
3599 struct sigaction sigact
;
3602 sigaddset(&set
, SIGUSR2
);
3603 sigaddset(&set
, SIGIO
);
3604 sigaddset(&set
, SIGALRM
);
3605 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3608 sigaddset(&set
, SIGUSR1
);
3609 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3611 memset(&sigact
, 0, sizeof(sigact
));
3612 sigact
.sa_handler
= cpu_signal
;
3613 sigaction(SIGUSR1
, &sigact
, NULL
);
3616 static void unblock_io_signals(void)
3621 sigaddset(&set
, SIGUSR2
);
3622 sigaddset(&set
, SIGIO
);
3623 sigaddset(&set
, SIGALRM
);
3624 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3627 sigaddset(&set
, SIGUSR1
);
3628 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3631 static void qemu_signal_lock(unsigned int msecs
)
3633 qemu_mutex_lock(&qemu_fair_mutex
);
3635 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3636 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3637 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3640 qemu_mutex_unlock(&qemu_fair_mutex
);
3643 static void qemu_mutex_lock_iothread(void)
3645 if (kvm_enabled()) {
3646 qemu_mutex_lock(&qemu_fair_mutex
);
3647 qemu_mutex_lock(&qemu_global_mutex
);
3648 qemu_mutex_unlock(&qemu_fair_mutex
);
3650 qemu_signal_lock(100);
3653 static void qemu_mutex_unlock_iothread(void)
3655 qemu_mutex_unlock(&qemu_global_mutex
);
3658 static int all_vcpus_paused(void)
3660 CPUState
*penv
= first_cpu
;
3665 penv
= (CPUState
*)penv
->next_cpu
;
3671 static void pause_all_vcpus(void)
3673 CPUState
*penv
= first_cpu
;
3677 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3678 qemu_cpu_kick(penv
);
3679 penv
= (CPUState
*)penv
->next_cpu
;
3682 while (!all_vcpus_paused()) {
3683 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3686 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3687 penv
= (CPUState
*)penv
->next_cpu
;
3692 static void resume_all_vcpus(void)
3694 CPUState
*penv
= first_cpu
;
3699 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3700 qemu_cpu_kick(penv
);
3701 penv
= (CPUState
*)penv
->next_cpu
;
3705 static void tcg_init_vcpu(void *_env
)
3707 CPUState
*env
= _env
;
3708 /* share a single thread for all cpus with TCG */
3709 if (!tcg_cpu_thread
) {
3710 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3711 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3712 qemu_cond_init(env
->halt_cond
);
3713 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3714 while (env
->created
== 0)
3715 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3716 tcg_cpu_thread
= env
->thread
;
3717 tcg_halt_cond
= env
->halt_cond
;
3719 env
->thread
= tcg_cpu_thread
;
3720 env
->halt_cond
= tcg_halt_cond
;
3724 static void kvm_start_vcpu(CPUState
*env
)
3726 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3727 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3728 qemu_cond_init(env
->halt_cond
);
3729 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3730 while (env
->created
== 0)
3731 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3734 void qemu_init_vcpu(void *_env
)
3736 CPUState
*env
= _env
;
3739 kvm_start_vcpu(env
);
3742 env
->nr_cores
= smp_cores
;
3743 env
->nr_threads
= smp_threads
;
3746 void qemu_notify_event(void)
3748 qemu_event_increment();
3751 void vm_stop(int reason
)
3754 qemu_thread_self(&me
);
3756 if (!qemu_thread_equal(&me
, &io_thread
)) {
3757 qemu_system_vmstop_request(reason
);
3759 * FIXME: should not return to device code in case
3760 * vm_stop() has been requested.
3762 if (cpu_single_env
) {
3763 cpu_exit(cpu_single_env
);
3764 cpu_single_env
->stop
= 1;
3775 static void host_main_loop_wait(int *timeout
)
3781 /* XXX: need to suppress polling by better using win32 events */
3783 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3784 ret
|= pe
->func(pe
->opaque
);
3788 WaitObjects
*w
= &wait_objects
;
3790 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3791 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3792 if (w
->func
[ret
- WAIT_OBJECT_0
])
3793 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3795 /* Check for additional signaled events */
3796 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3798 /* Check if event is signaled */
3799 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3800 if(ret2
== WAIT_OBJECT_0
) {
3802 w
->func
[i
](w
->opaque
[i
]);
3803 } else if (ret2
== WAIT_TIMEOUT
) {
3805 err
= GetLastError();
3806 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3809 } else if (ret
== WAIT_TIMEOUT
) {
3811 err
= GetLastError();
3812 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3819 static void host_main_loop_wait(int *timeout
)
3824 void main_loop_wait(int timeout
)
3826 IOHandlerRecord
*ioh
;
3827 fd_set rfds
, wfds
, xfds
;
3831 qemu_bh_update_timeout(&timeout
);
3833 host_main_loop_wait(&timeout
);
3835 /* poll any events */
3836 /* XXX: separate device handlers from system ones */
3841 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3845 (!ioh
->fd_read_poll
||
3846 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3847 FD_SET(ioh
->fd
, &rfds
);
3851 if (ioh
->fd_write
) {
3852 FD_SET(ioh
->fd
, &wfds
);
3858 tv
.tv_sec
= timeout
/ 1000;
3859 tv
.tv_usec
= (timeout
% 1000) * 1000;
3861 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3863 qemu_mutex_unlock_iothread();
3864 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3865 qemu_mutex_lock_iothread();
3867 IOHandlerRecord
**pioh
;
3869 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3870 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3871 ioh
->fd_read(ioh
->opaque
);
3873 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3874 ioh
->fd_write(ioh
->opaque
);
3878 /* remove deleted IO handlers */
3879 pioh
= &first_io_handler
;
3890 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3892 /* rearm timer, if not periodic */
3893 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3894 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3895 qemu_rearm_alarm_timer(alarm_timer
);
3898 /* vm time timers */
3900 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3901 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3902 qemu_get_clock(vm_clock
));
3905 /* real time timers */
3906 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3907 qemu_get_clock(rt_clock
));
3909 /* Check bottom-halves last in case any of the earlier events triggered
3915 static int qemu_cpu_exec(CPUState
*env
)
3918 #ifdef CONFIG_PROFILER
3922 #ifdef CONFIG_PROFILER
3923 ti
= profile_getclock();
3928 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3929 env
->icount_decr
.u16
.low
= 0;
3930 env
->icount_extra
= 0;
3931 count
= qemu_next_deadline();
3932 count
= (count
+ (1 << icount_time_shift
) - 1)
3933 >> icount_time_shift
;
3934 qemu_icount
+= count
;
3935 decr
= (count
> 0xffff) ? 0xffff : count
;
3937 env
->icount_decr
.u16
.low
= decr
;
3938 env
->icount_extra
= count
;
3940 ret
= cpu_exec(env
);
3941 #ifdef CONFIG_PROFILER
3942 qemu_time
+= profile_getclock() - ti
;
3945 /* Fold pending instructions back into the
3946 instruction counter, and clear the interrupt flag. */
3947 qemu_icount
-= (env
->icount_decr
.u16
.low
3948 + env
->icount_extra
);
3949 env
->icount_decr
.u32
= 0;
3950 env
->icount_extra
= 0;
3955 static void tcg_cpu_exec(void)
3959 if (next_cpu
== NULL
)
3960 next_cpu
= first_cpu
;
3961 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3962 CPUState
*env
= cur_cpu
= next_cpu
;
3966 if (timer_alarm_pending
) {
3967 timer_alarm_pending
= 0;
3970 if (cpu_can_run(env
))
3971 ret
= qemu_cpu_exec(env
);
3972 if (ret
== EXCP_DEBUG
) {
3973 gdb_set_stop_cpu(env
);
3974 debug_requested
= 1;
3980 static int cpu_has_work(CPUState
*env
)
3988 if (qemu_cpu_has_work(env
))
3993 static int tcg_has_work(void)
3997 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3998 if (cpu_has_work(env
))
4003 static int qemu_calculate_timeout(void)
4005 #ifndef CONFIG_IOTHREAD
4010 else if (tcg_has_work())
4012 else if (!use_icount
)
4015 /* XXX: use timeout computed from timers */
4018 /* Advance virtual time to the next event. */
4019 if (use_icount
== 1) {
4020 /* When not using an adaptive execution frequency
4021 we tend to get badly out of sync with real time,
4022 so just delay for a reasonable amount of time. */
4025 delta
= cpu_get_icount() - cpu_get_clock();
4028 /* If virtual time is ahead of real time then just
4030 timeout
= (delta
/ 1000000) + 1;
4032 /* Wait for either IO to occur or the next
4034 add
= qemu_next_deadline();
4035 /* We advance the timer before checking for IO.
4036 Limit the amount we advance so that early IO
4037 activity won't get the guest too far ahead. */
4041 add
= (add
+ (1 << icount_time_shift
) - 1)
4042 >> icount_time_shift
;
4044 timeout
= delta
/ 1000000;
4051 #else /* CONFIG_IOTHREAD */
4056 static int vm_can_run(void)
4058 if (powerdown_requested
)
4060 if (reset_requested
)
4062 if (shutdown_requested
)
4064 if (debug_requested
)
4069 qemu_irq qemu_system_powerdown
;
4071 static void main_loop(void)
4075 #ifdef CONFIG_IOTHREAD
4076 qemu_system_ready
= 1;
4077 qemu_cond_broadcast(&qemu_system_cond
);
4082 #ifdef CONFIG_PROFILER
4085 #ifndef CONFIG_IOTHREAD
4088 #ifdef CONFIG_PROFILER
4089 ti
= profile_getclock();
4091 main_loop_wait(qemu_calculate_timeout());
4092 #ifdef CONFIG_PROFILER
4093 dev_time
+= profile_getclock() - ti
;
4095 } while (vm_can_run());
4097 if (qemu_debug_requested())
4098 vm_stop(EXCP_DEBUG
);
4099 if (qemu_shutdown_requested()) {
4106 if (qemu_reset_requested()) {
4108 qemu_system_reset();
4111 if (qemu_powerdown_requested()) {
4112 qemu_irq_raise(qemu_system_powerdown
);
4114 if ((r
= qemu_vmstop_requested()))
4120 static void version(void)
4122 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4125 static void help(int exitcode
)
4128 printf("usage: %s [options] [disk_image]\n"
4130 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4132 #define DEF(option, opt_arg, opt_enum, opt_help) \
4134 #define DEFHEADING(text) stringify(text) "\n"
4135 #include "qemu-options.h"
4140 "During emulation, the following keys are useful:\n"
4141 "ctrl-alt-f toggle full screen\n"
4142 "ctrl-alt-n switch to virtual console 'n'\n"
4143 "ctrl-alt toggle mouse and keyboard grab\n"
4145 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4150 DEFAULT_NETWORK_SCRIPT
,
4151 DEFAULT_NETWORK_DOWN_SCRIPT
,
4153 DEFAULT_GDBSTUB_PORT
,
4158 #define HAS_ARG 0x0001
4161 #define DEF(option, opt_arg, opt_enum, opt_help) \
4163 #define DEFHEADING(text)
4164 #include "qemu-options.h"
4170 typedef struct QEMUOption
{
4176 static const QEMUOption qemu_options
[] = {
4177 { "h", 0, QEMU_OPTION_h
},
4178 #define DEF(option, opt_arg, opt_enum, opt_help) \
4179 { option, opt_arg, opt_enum },
4180 #define DEFHEADING(text)
4181 #include "qemu-options.h"
4189 struct soundhw soundhw
[] = {
4190 #ifdef HAS_AUDIO_CHOICE
4191 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4197 { .init_isa
= pcspk_audio_init
}
4204 "Creative Sound Blaster 16",
4207 { .init_isa
= SB16_init
}
4211 #ifdef CONFIG_CS4231A
4217 { .init_isa
= cs4231a_init
}
4225 "Yamaha YMF262 (OPL3)",
4227 "Yamaha YM3812 (OPL2)",
4231 { .init_isa
= Adlib_init
}
4238 "Gravis Ultrasound GF1",
4241 { .init_isa
= GUS_init
}
4248 "Intel 82801AA AC97 Audio",
4251 { .init_pci
= ac97_init
}
4255 #ifdef CONFIG_ES1370
4258 "ENSONIQ AudioPCI ES1370",
4261 { .init_pci
= es1370_init
}
4265 #endif /* HAS_AUDIO_CHOICE */
4267 { NULL
, NULL
, 0, 0, { NULL
} }
4270 static void select_soundhw (const char *optarg
)
4274 if (*optarg
== '?') {
4277 printf ("Valid sound card names (comma separated):\n");
4278 for (c
= soundhw
; c
->name
; ++c
) {
4279 printf ("%-11s %s\n", c
->name
, c
->descr
);
4281 printf ("\n-soundhw all will enable all of the above\n");
4282 exit (*optarg
!= '?');
4290 if (!strcmp (optarg
, "all")) {
4291 for (c
= soundhw
; c
->name
; ++c
) {
4299 e
= strchr (p
, ',');
4300 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4302 for (c
= soundhw
; c
->name
; ++c
) {
4303 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4312 "Unknown sound card name (too big to show)\n");
4315 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4320 p
+= l
+ (e
!= NULL
);
4324 goto show_valid_cards
;
4329 static void select_vgahw (const char *p
)
4333 vga_interface_type
= VGA_NONE
;
4334 if (strstart(p
, "std", &opts
)) {
4335 vga_interface_type
= VGA_STD
;
4336 } else if (strstart(p
, "cirrus", &opts
)) {
4337 vga_interface_type
= VGA_CIRRUS
;
4338 } else if (strstart(p
, "vmware", &opts
)) {
4339 vga_interface_type
= VGA_VMWARE
;
4340 } else if (strstart(p
, "xenfb", &opts
)) {
4341 vga_interface_type
= VGA_XENFB
;
4342 } else if (!strstart(p
, "none", &opts
)) {
4344 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4348 const char *nextopt
;
4350 if (strstart(opts
, ",retrace=", &nextopt
)) {
4352 if (strstart(opts
, "dumb", &nextopt
))
4353 vga_retrace_method
= VGA_RETRACE_DUMB
;
4354 else if (strstart(opts
, "precise", &nextopt
))
4355 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4356 else goto invalid_vga
;
4357 } else goto invalid_vga
;
4363 static int balloon_parse(const char *arg
)
4367 if (strcmp(arg
, "none") == 0) {
4371 if (!strncmp(arg
, "virtio", 6)) {
4372 if (arg
[6] == ',') {
4373 /* have params -> parse them */
4374 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4378 /* create empty opts */
4379 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4381 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4390 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4392 exit(STATUS_CONTROL_C_EXIT
);
4397 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4401 if(strlen(str
) != 36)
4404 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4405 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4406 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4412 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4418 #define MAX_NET_CLIENTS 32
4422 static void termsig_handler(int signal
)
4424 qemu_system_shutdown_request();
4427 static void sigchld_handler(int signal
)
4429 waitpid(-1, NULL
, WNOHANG
);
4432 static void sighandler_setup(void)
4434 struct sigaction act
;
4436 memset(&act
, 0, sizeof(act
));
4437 act
.sa_handler
= termsig_handler
;
4438 sigaction(SIGINT
, &act
, NULL
);
4439 sigaction(SIGHUP
, &act
, NULL
);
4440 sigaction(SIGTERM
, &act
, NULL
);
4442 act
.sa_handler
= sigchld_handler
;
4443 act
.sa_flags
= SA_NOCLDSTOP
;
4444 sigaction(SIGCHLD
, &act
, NULL
);
4450 /* Look for support files in the same directory as the executable. */
4451 static char *find_datadir(const char *argv0
)
4457 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4464 while (p
!= buf
&& *p
!= '\\')
4467 if (access(buf
, R_OK
) == 0) {
4468 return qemu_strdup(buf
);
4474 /* Find a likely location for support files using the location of the binary.
4475 For installed binaries this will be "$bindir/../share/qemu". When
4476 running from the build tree this will be "$bindir/../pc-bios". */
4477 #define SHARE_SUFFIX "/share/qemu"
4478 #define BUILD_SUFFIX "/pc-bios"
4479 static char *find_datadir(const char *argv0
)
4487 #if defined(__linux__)
4490 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4496 #elif defined(__FreeBSD__)
4499 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4506 /* If we don't have any way of figuring out the actual executable
4507 location then try argv[0]. */
4509 p
= realpath(argv0
, buf
);
4517 max_len
= strlen(dir
) +
4518 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4519 res
= qemu_mallocz(max_len
);
4520 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4521 if (access(res
, R_OK
)) {
4522 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4523 if (access(res
, R_OK
)) {
4535 char *qemu_find_file(int type
, const char *name
)
4541 /* If name contains path separators then try it as a straight path. */
4542 if ((strchr(name
, '/') || strchr(name
, '\\'))
4543 && access(name
, R_OK
) == 0) {
4544 return qemu_strdup(name
);
4547 case QEMU_FILE_TYPE_BIOS
:
4550 case QEMU_FILE_TYPE_KEYMAP
:
4551 subdir
= "keymaps/";
4556 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4557 buf
= qemu_mallocz(len
);
4558 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4559 if (access(buf
, R_OK
)) {
4566 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4570 dev
= qdev_device_add(opts
);
4576 struct device_config
{
4578 DEV_USB
, /* -usbdevice */
4581 const char *cmdline
;
4582 QTAILQ_ENTRY(device_config
) next
;
4584 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4586 static void add_device_config(int type
, const char *cmdline
)
4588 struct device_config
*conf
;
4590 conf
= qemu_mallocz(sizeof(*conf
));
4592 conf
->cmdline
= cmdline
;
4593 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4596 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4598 struct device_config
*conf
;
4601 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4602 if (conf
->type
!= type
)
4604 rc
= func(conf
->cmdline
);
4611 int main(int argc
, char **argv
, char **envp
)
4613 const char *gdbstub_dev
= NULL
;
4614 uint32_t boot_devices_bitmap
= 0;
4616 int snapshot
, linux_boot
, net_boot
;
4617 const char *initrd_filename
;
4618 const char *kernel_filename
, *kernel_cmdline
;
4619 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4621 DisplayChangeListener
*dcl
;
4622 int cyls
, heads
, secs
, translation
;
4623 const char *net_clients
[MAX_NET_CLIENTS
];
4625 QemuOpts
*hda_opts
= NULL
, *opts
;
4627 const char *r
, *optarg
;
4628 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4629 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4630 int monitor_device_index
;
4631 const char *serial_devices
[MAX_SERIAL_PORTS
];
4632 int serial_device_index
;
4633 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4634 int parallel_device_index
;
4635 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4636 int virtio_console_index
;
4637 const char *loadvm
= NULL
;
4638 QEMUMachine
*machine
;
4639 const char *cpu_model
;
4644 const char *pid_file
= NULL
;
4645 const char *incoming
= NULL
;
4648 struct passwd
*pwd
= NULL
;
4649 const char *chroot_dir
= NULL
;
4650 const char *run_as
= NULL
;
4653 int show_vnc_port
= 0;
4655 qemu_errors_to_file(stderr
);
4656 qemu_cache_utils_init(envp
);
4658 QLIST_INIT (&vm_change_state_head
);
4661 struct sigaction act
;
4662 sigfillset(&act
.sa_mask
);
4664 act
.sa_handler
= SIG_IGN
;
4665 sigaction(SIGPIPE
, &act
, NULL
);
4668 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4669 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4670 QEMU to run on a single CPU */
4675 h
= GetCurrentProcess();
4676 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4677 for(i
= 0; i
< 32; i
++) {
4678 if (mask
& (1 << i
))
4683 SetProcessAffinityMask(h
, mask
);
4689 module_call_init(MODULE_INIT_MACHINE
);
4690 machine
= find_default_machine();
4692 initrd_filename
= NULL
;
4695 kernel_filename
= NULL
;
4696 kernel_cmdline
= "";
4697 cyls
= heads
= secs
= 0;
4698 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4700 serial_devices
[0] = "vc:80Cx24C";
4701 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4702 serial_devices
[i
] = NULL
;
4703 serial_device_index
= 0;
4705 parallel_devices
[0] = "vc:80Cx24C";
4706 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4707 parallel_devices
[i
] = NULL
;
4708 parallel_device_index
= 0;
4710 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4711 virtio_consoles
[i
] = NULL
;
4712 virtio_console_index
= 0;
4714 monitor_devices
[0] = "vc:80Cx24C";
4715 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4716 monitor_devices
[i
] = NULL
;
4718 monitor_device_index
= 0;
4720 for (i
= 0; i
< MAX_NODES
; i
++) {
4722 node_cpumask
[i
] = 0;
4738 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4740 const QEMUOption
*popt
;
4743 /* Treat --foo the same as -foo. */
4746 popt
= qemu_options
;
4749 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4753 if (!strcmp(popt
->name
, r
+ 1))
4757 if (popt
->flags
& HAS_ARG
) {
4758 if (optind
>= argc
) {
4759 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4763 optarg
= argv
[optind
++];
4768 switch(popt
->index
) {
4770 machine
= find_machine(optarg
);
4773 printf("Supported machines are:\n");
4774 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4776 printf("%-10s %s (alias of %s)\n",
4777 m
->alias
, m
->desc
, m
->name
);
4778 printf("%-10s %s%s\n",
4780 m
->is_default
? " (default)" : "");
4782 exit(*optarg
!= '?');
4785 case QEMU_OPTION_cpu
:
4786 /* hw initialization will check this */
4787 if (*optarg
== '?') {
4788 /* XXX: implement xxx_cpu_list for targets that still miss it */
4789 #if defined(cpu_list)
4790 cpu_list(stdout
, &fprintf
);
4797 case QEMU_OPTION_initrd
:
4798 initrd_filename
= optarg
;
4800 case QEMU_OPTION_hda
:
4802 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4804 hda_opts
= drive_add(optarg
, HD_ALIAS
4805 ",cyls=%d,heads=%d,secs=%d%s",
4806 0, cyls
, heads
, secs
,
4807 translation
== BIOS_ATA_TRANSLATION_LBA
?
4809 translation
== BIOS_ATA_TRANSLATION_NONE
?
4810 ",trans=none" : "");
4812 case QEMU_OPTION_hdb
:
4813 case QEMU_OPTION_hdc
:
4814 case QEMU_OPTION_hdd
:
4815 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4817 case QEMU_OPTION_drive
:
4818 drive_add(NULL
, "%s", optarg
);
4820 case QEMU_OPTION_set
:
4821 if (qemu_set_option(optarg
) != 0)
4824 case QEMU_OPTION_mtdblock
:
4825 drive_add(optarg
, MTD_ALIAS
);
4827 case QEMU_OPTION_sd
:
4828 drive_add(optarg
, SD_ALIAS
);
4830 case QEMU_OPTION_pflash
:
4831 drive_add(optarg
, PFLASH_ALIAS
);
4833 case QEMU_OPTION_snapshot
:
4836 case QEMU_OPTION_hdachs
:
4840 cyls
= strtol(p
, (char **)&p
, 0);
4841 if (cyls
< 1 || cyls
> 16383)
4846 heads
= strtol(p
, (char **)&p
, 0);
4847 if (heads
< 1 || heads
> 16)
4852 secs
= strtol(p
, (char **)&p
, 0);
4853 if (secs
< 1 || secs
> 63)
4857 if (!strcmp(p
, "none"))
4858 translation
= BIOS_ATA_TRANSLATION_NONE
;
4859 else if (!strcmp(p
, "lba"))
4860 translation
= BIOS_ATA_TRANSLATION_LBA
;
4861 else if (!strcmp(p
, "auto"))
4862 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4865 } else if (*p
!= '\0') {
4867 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4870 if (hda_opts
!= NULL
) {
4872 snprintf(num
, sizeof(num
), "%d", cyls
);
4873 qemu_opt_set(hda_opts
, "cyls", num
);
4874 snprintf(num
, sizeof(num
), "%d", heads
);
4875 qemu_opt_set(hda_opts
, "heads", num
);
4876 snprintf(num
, sizeof(num
), "%d", secs
);
4877 qemu_opt_set(hda_opts
, "secs", num
);
4878 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4879 qemu_opt_set(hda_opts
, "trans", "lba");
4880 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4881 qemu_opt_set(hda_opts
, "trans", "none");
4885 case QEMU_OPTION_numa
:
4886 if (nb_numa_nodes
>= MAX_NODES
) {
4887 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4892 case QEMU_OPTION_nographic
:
4893 display_type
= DT_NOGRAPHIC
;
4895 #ifdef CONFIG_CURSES
4896 case QEMU_OPTION_curses
:
4897 display_type
= DT_CURSES
;
4900 case QEMU_OPTION_portrait
:
4903 case QEMU_OPTION_kernel
:
4904 kernel_filename
= optarg
;
4906 case QEMU_OPTION_append
:
4907 kernel_cmdline
= optarg
;
4909 case QEMU_OPTION_cdrom
:
4910 drive_add(optarg
, CDROM_ALIAS
);
4912 case QEMU_OPTION_boot
:
4914 static const char * const params
[] = {
4915 "order", "once", "menu", NULL
4917 char buf
[sizeof(boot_devices
)];
4918 char *standard_boot_devices
;
4921 if (!strchr(optarg
, '=')) {
4923 pstrcpy(buf
, sizeof(buf
), optarg
);
4924 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4926 "qemu: unknown boot parameter '%s' in '%s'\n",
4932 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4933 boot_devices_bitmap
= parse_bootdevices(buf
);
4934 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4937 if (get_param_value(buf
, sizeof(buf
),
4939 boot_devices_bitmap
|= parse_bootdevices(buf
);
4940 standard_boot_devices
= qemu_strdup(boot_devices
);
4941 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4942 qemu_register_reset(restore_boot_devices
,
4943 standard_boot_devices
);
4945 if (get_param_value(buf
, sizeof(buf
),
4947 if (!strcmp(buf
, "on")) {
4949 } else if (!strcmp(buf
, "off")) {
4953 "qemu: invalid option value '%s'\n",
4961 case QEMU_OPTION_fda
:
4962 case QEMU_OPTION_fdb
:
4963 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4966 case QEMU_OPTION_no_fd_bootchk
:
4970 case QEMU_OPTION_net
:
4971 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4972 fprintf(stderr
, "qemu: too many network clients\n");
4975 net_clients
[nb_net_clients
] = optarg
;
4979 case QEMU_OPTION_tftp
:
4980 legacy_tftp_prefix
= optarg
;
4982 case QEMU_OPTION_bootp
:
4983 legacy_bootp_filename
= optarg
;
4986 case QEMU_OPTION_smb
:
4987 net_slirp_smb(optarg
);
4990 case QEMU_OPTION_redir
:
4991 net_slirp_redir(optarg
);
4994 case QEMU_OPTION_bt
:
4995 add_device_config(DEV_BT
, optarg
);
4998 case QEMU_OPTION_audio_help
:
5002 case QEMU_OPTION_soundhw
:
5003 select_soundhw (optarg
);
5009 case QEMU_OPTION_version
:
5013 case QEMU_OPTION_m
: {
5017 value
= strtoul(optarg
, &ptr
, 10);
5019 case 0: case 'M': case 'm':
5026 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5030 /* On 32-bit hosts, QEMU is limited by virtual address space */
5031 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5032 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5035 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5036 fprintf(stderr
, "qemu: ram size too large\n");
5045 const CPULogItem
*item
;
5047 mask
= cpu_str_to_log_mask(optarg
);
5049 printf("Log items (comma separated):\n");
5050 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5051 printf("%-10s %s\n", item
->name
, item
->help
);
5059 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5061 case QEMU_OPTION_gdb
:
5062 gdbstub_dev
= optarg
;
5067 case QEMU_OPTION_bios
:
5070 case QEMU_OPTION_singlestep
:
5078 keyboard_layout
= optarg
;
5081 case QEMU_OPTION_localtime
:
5084 case QEMU_OPTION_vga
:
5085 select_vgahw (optarg
);
5087 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5093 w
= strtol(p
, (char **)&p
, 10);
5096 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5102 h
= strtol(p
, (char **)&p
, 10);
5107 depth
= strtol(p
, (char **)&p
, 10);
5108 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5109 depth
!= 24 && depth
!= 32)
5111 } else if (*p
== '\0') {
5112 depth
= graphic_depth
;
5119 graphic_depth
= depth
;
5123 case QEMU_OPTION_echr
:
5126 term_escape_char
= strtol(optarg
, &r
, 0);
5128 printf("Bad argument to echr\n");
5131 case QEMU_OPTION_monitor
:
5132 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5133 fprintf(stderr
, "qemu: too many monitor devices\n");
5136 monitor_devices
[monitor_device_index
] = optarg
;
5137 monitor_device_index
++;
5139 case QEMU_OPTION_chardev
:
5140 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5142 fprintf(stderr
, "parse error: %s\n", optarg
);
5145 if (NULL
== qemu_chr_open_opts(opts
, NULL
)) {
5149 case QEMU_OPTION_serial
:
5150 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5151 fprintf(stderr
, "qemu: too many serial ports\n");
5154 serial_devices
[serial_device_index
] = optarg
;
5155 serial_device_index
++;
5157 case QEMU_OPTION_watchdog
:
5160 "qemu: only one watchdog option may be given\n");
5165 case QEMU_OPTION_watchdog_action
:
5166 if (select_watchdog_action(optarg
) == -1) {
5167 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5171 case QEMU_OPTION_virtiocon
:
5172 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5173 fprintf(stderr
, "qemu: too many virtio consoles\n");
5176 virtio_consoles
[virtio_console_index
] = optarg
;
5177 virtio_console_index
++;
5179 case QEMU_OPTION_parallel
:
5180 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5181 fprintf(stderr
, "qemu: too many parallel ports\n");
5184 parallel_devices
[parallel_device_index
] = optarg
;
5185 parallel_device_index
++;
5187 case QEMU_OPTION_loadvm
:
5190 case QEMU_OPTION_full_screen
:
5194 case QEMU_OPTION_no_frame
:
5197 case QEMU_OPTION_alt_grab
:
5200 case QEMU_OPTION_no_quit
:
5203 case QEMU_OPTION_sdl
:
5204 display_type
= DT_SDL
;
5207 case QEMU_OPTION_pidfile
:
5211 case QEMU_OPTION_win2k_hack
:
5212 win2k_install_hack
= 1;
5214 case QEMU_OPTION_rtc_td_hack
:
5217 case QEMU_OPTION_acpitable
:
5218 if(acpi_table_add(optarg
) < 0) {
5219 fprintf(stderr
, "Wrong acpi table provided\n");
5223 case QEMU_OPTION_smbios
:
5224 if(smbios_entry_add(optarg
) < 0) {
5225 fprintf(stderr
, "Wrong smbios provided\n");
5231 case QEMU_OPTION_enable_kvm
:
5235 case QEMU_OPTION_usb
:
5238 case QEMU_OPTION_usbdevice
:
5240 add_device_config(DEV_USB
, optarg
);
5242 case QEMU_OPTION_device
:
5243 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5245 fprintf(stderr
, "parse error: %s\n", optarg
);
5249 case QEMU_OPTION_smp
:
5252 fprintf(stderr
, "Invalid number of CPUs\n");
5255 if (max_cpus
< smp_cpus
) {
5256 fprintf(stderr
, "maxcpus must be equal to or greater than "
5260 if (max_cpus
> 255) {
5261 fprintf(stderr
, "Unsupported number of maxcpus\n");
5265 case QEMU_OPTION_vnc
:
5266 display_type
= DT_VNC
;
5267 vnc_display
= optarg
;
5270 case QEMU_OPTION_no_acpi
:
5273 case QEMU_OPTION_no_hpet
:
5276 case QEMU_OPTION_balloon
:
5277 if (balloon_parse(optarg
) < 0) {
5278 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5283 case QEMU_OPTION_no_reboot
:
5286 case QEMU_OPTION_no_shutdown
:
5289 case QEMU_OPTION_show_cursor
:
5292 case QEMU_OPTION_uuid
:
5293 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5294 fprintf(stderr
, "Fail to parse UUID string."
5295 " Wrong format.\n");
5300 case QEMU_OPTION_daemonize
:
5304 case QEMU_OPTION_option_rom
:
5305 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5306 fprintf(stderr
, "Too many option ROMs\n");
5309 option_rom
[nb_option_roms
] = optarg
;
5312 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5313 case QEMU_OPTION_semihosting
:
5314 semihosting_enabled
= 1;
5317 case QEMU_OPTION_name
:
5318 qemu_name
= qemu_strdup(optarg
);
5320 char *p
= strchr(qemu_name
, ',');
5323 if (strncmp(p
, "process=", 8)) {
5324 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5332 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5333 case QEMU_OPTION_prom_env
:
5334 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5335 fprintf(stderr
, "Too many prom variables\n");
5338 prom_envs
[nb_prom_envs
] = optarg
;
5343 case QEMU_OPTION_old_param
:
5347 case QEMU_OPTION_clock
:
5348 configure_alarms(optarg
);
5350 case QEMU_OPTION_startdate
:
5353 time_t rtc_start_date
;
5354 if (!strcmp(optarg
, "now")) {
5355 rtc_date_offset
= -1;
5357 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5365 } else if (sscanf(optarg
, "%d-%d-%d",
5368 &tm
.tm_mday
) == 3) {
5377 rtc_start_date
= mktimegm(&tm
);
5378 if (rtc_start_date
== -1) {
5380 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5381 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5384 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5388 case QEMU_OPTION_tb_size
:
5389 tb_size
= strtol(optarg
, NULL
, 0);
5393 case QEMU_OPTION_icount
:
5395 if (strcmp(optarg
, "auto") == 0) {
5396 icount_time_shift
= -1;
5398 icount_time_shift
= strtol(optarg
, NULL
, 0);
5401 case QEMU_OPTION_incoming
:
5405 case QEMU_OPTION_chroot
:
5406 chroot_dir
= optarg
;
5408 case QEMU_OPTION_runas
:
5413 case QEMU_OPTION_xen_domid
:
5414 xen_domid
= atoi(optarg
);
5416 case QEMU_OPTION_xen_create
:
5417 xen_mode
= XEN_CREATE
;
5419 case QEMU_OPTION_xen_attach
:
5420 xen_mode
= XEN_ATTACH
;
5427 if (kvm_enabled()) {
5430 ret
= kvm_init(smp_cpus
);
5432 fprintf(stderr
, "failed to initialize KVM\n");
5437 /* If no data_dir is specified then try to find it relative to the
5440 data_dir
= find_datadir(argv
[0]);
5442 /* If all else fails use the install patch specified when building. */
5444 data_dir
= CONFIG_QEMU_SHAREDIR
;
5448 * Default to max_cpus = smp_cpus, in case the user doesn't
5449 * specify a max_cpus value.
5452 max_cpus
= smp_cpus
;
5454 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5455 if (smp_cpus
> machine
->max_cpus
) {
5456 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5457 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5462 if (display_type
== DT_NOGRAPHIC
) {
5463 if (serial_device_index
== 0)
5464 serial_devices
[0] = "stdio";
5465 if (parallel_device_index
== 0)
5466 parallel_devices
[0] = "null";
5467 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5468 monitor_devices
[0] = "stdio";
5476 if (pipe(fds
) == -1)
5487 len
= read(fds
[0], &status
, 1);
5488 if (len
== -1 && (errno
== EINTR
))
5493 else if (status
== 1) {
5494 fprintf(stderr
, "Could not acquire pidfile\n");
5511 signal(SIGTSTP
, SIG_IGN
);
5512 signal(SIGTTOU
, SIG_IGN
);
5513 signal(SIGTTIN
, SIG_IGN
);
5516 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5519 write(fds
[1], &status
, 1);
5521 fprintf(stderr
, "Could not acquire pid file\n");
5526 if (qemu_init_main_loop()) {
5527 fprintf(stderr
, "qemu_init_main_loop failed\n");
5530 linux_boot
= (kernel_filename
!= NULL
);
5532 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5533 fprintf(stderr
, "-append only allowed with -kernel option\n");
5537 if (!linux_boot
&& initrd_filename
!= NULL
) {
5538 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5543 /* Win32 doesn't support line-buffering and requires size >= 2 */
5544 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5548 if (init_timer_alarm() < 0) {
5549 fprintf(stderr
, "could not initialize alarm timer\n");
5552 if (use_icount
&& icount_time_shift
< 0) {
5554 /* 125MIPS seems a reasonable initial guess at the guest speed.
5555 It will be corrected fairly quickly anyway. */
5556 icount_time_shift
= 3;
5557 init_icount_adjust();
5564 /* init network clients */
5565 if (nb_net_clients
== 0) {
5566 /* if no clients, we use a default config */
5567 net_clients
[nb_net_clients
++] = "nic";
5569 net_clients
[nb_net_clients
++] = "user";
5573 for(i
= 0;i
< nb_net_clients
; i
++) {
5574 if (net_client_parse(net_clients
[i
]) < 0)
5578 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5579 net_set_boot_mask(net_boot
);
5583 /* init the bluetooth world */
5584 if (foreach_device_config(DEV_BT
, bt_parse
))
5587 /* init the memory */
5589 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5591 /* init the dynamic translator */
5592 cpu_exec_init_all(tb_size
* 1024 * 1024);
5596 /* we always create the cdrom drive, even if no disk is there */
5597 drive_add(NULL
, CDROM_ALIAS
);
5599 /* we always create at least one floppy */
5600 drive_add(NULL
, FD_ALIAS
, 0);
5602 /* we always create one sd slot, even if no card is in it */
5603 drive_add(NULL
, SD_ALIAS
);
5605 /* open the virtual block devices */
5607 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5608 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5611 vmstate_register(0, &vmstate_timers
,&timers_state
);
5612 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5614 /* Maintain compatibility with multiple stdio monitors */
5615 if (!strcmp(monitor_devices
[0],"stdio")) {
5616 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5617 const char *devname
= serial_devices
[i
];
5618 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5619 monitor_devices
[0] = NULL
;
5621 } else if (devname
&& !strcmp(devname
,"stdio")) {
5622 monitor_devices
[0] = NULL
;
5623 serial_devices
[i
] = "mon:stdio";
5629 if (nb_numa_nodes
> 0) {
5632 if (nb_numa_nodes
> smp_cpus
) {
5633 nb_numa_nodes
= smp_cpus
;
5636 /* If no memory size if given for any node, assume the default case
5637 * and distribute the available memory equally across all nodes
5639 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5640 if (node_mem
[i
] != 0)
5643 if (i
== nb_numa_nodes
) {
5644 uint64_t usedmem
= 0;
5646 /* On Linux, the each node's border has to be 8MB aligned,
5647 * the final node gets the rest.
5649 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5650 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5651 usedmem
+= node_mem
[i
];
5653 node_mem
[i
] = ram_size
- usedmem
;
5656 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5657 if (node_cpumask
[i
] != 0)
5660 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5661 * must cope with this anyway, because there are BIOSes out there in
5662 * real machines which also use this scheme.
5664 if (i
== nb_numa_nodes
) {
5665 for (i
= 0; i
< smp_cpus
; i
++) {
5666 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5671 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5672 const char *devname
= monitor_devices
[i
];
5673 if (devname
&& strcmp(devname
, "none")) {
5676 snprintf(label
, sizeof(label
), "monitor");
5678 snprintf(label
, sizeof(label
), "monitor%d", i
);
5680 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5681 if (!monitor_hds
[i
]) {
5682 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5689 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5690 const char *devname
= serial_devices
[i
];
5691 if (devname
&& strcmp(devname
, "none")) {
5693 snprintf(label
, sizeof(label
), "serial%d", i
);
5694 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5695 if (!serial_hds
[i
]) {
5696 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5703 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5704 const char *devname
= parallel_devices
[i
];
5705 if (devname
&& strcmp(devname
, "none")) {
5707 snprintf(label
, sizeof(label
), "parallel%d", i
);
5708 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5709 if (!parallel_hds
[i
]) {
5710 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5717 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5718 const char *devname
= virtio_consoles
[i
];
5719 if (devname
&& strcmp(devname
, "none")) {
5721 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5722 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5723 if (!virtcon_hds
[i
]) {
5724 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5731 module_call_init(MODULE_INIT_DEVICE
);
5734 i
= select_watchdog(watchdog
);
5736 exit (i
== 1 ? 1 : 0);
5739 if (machine
->compat_props
) {
5740 qdev_prop_register_compat(machine
->compat_props
);
5742 machine
->init(ram_size
, boot_devices
,
5743 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5747 /* must be after terminal init, SDL library changes signal handlers */
5751 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5752 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5753 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5759 current_machine
= machine
;
5761 /* init USB devices */
5763 foreach_device_config(DEV_USB
, usb_parse
);
5766 /* init generic devices */
5767 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5771 dumb_display_init();
5772 /* just use the first displaystate for the moment */
5775 if (display_type
== DT_DEFAULT
) {
5776 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5777 display_type
= DT_SDL
;
5779 display_type
= DT_VNC
;
5780 vnc_display
= "localhost:0,to=99";
5786 switch (display_type
) {
5789 #if defined(CONFIG_CURSES)
5791 curses_display_init(ds
, full_screen
);
5794 #if defined(CONFIG_SDL)
5796 sdl_display_init(ds
, full_screen
, no_frame
);
5798 #elif defined(CONFIG_COCOA)
5800 cocoa_display_init(ds
, full_screen
);
5804 vnc_display_init(ds
);
5805 if (vnc_display_open(ds
, vnc_display
) < 0)
5808 if (show_vnc_port
) {
5809 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5817 dcl
= ds
->listeners
;
5818 while (dcl
!= NULL
) {
5819 if (dcl
->dpy_refresh
!= NULL
) {
5820 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5821 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5826 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5827 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5828 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5831 text_consoles_set_display(display_state
);
5832 qemu_chr_initial_reset();
5834 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5835 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5836 monitor_init(monitor_hds
[i
],
5837 MONITOR_USE_READLINE
|
5838 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5842 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5843 const char *devname
= serial_devices
[i
];
5844 if (devname
&& strcmp(devname
, "none")) {
5845 if (strstart(devname
, "vc", 0))
5846 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5850 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5851 const char *devname
= parallel_devices
[i
];
5852 if (devname
&& strcmp(devname
, "none")) {
5853 if (strstart(devname
, "vc", 0))
5854 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5858 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5859 const char *devname
= virtio_consoles
[i
];
5860 if (virtcon_hds
[i
] && devname
) {
5861 if (strstart(devname
, "vc", 0))
5862 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5866 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5867 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5873 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5879 qemu_start_incoming_migration(incoming
);
5880 } else if (autostart
) {
5890 len
= write(fds
[1], &status
, 1);
5891 if (len
== -1 && (errno
== EINTR
))
5898 TFR(fd
= open("/dev/null", O_RDWR
));
5904 pwd
= getpwnam(run_as
);
5906 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5912 if (chroot(chroot_dir
) < 0) {
5913 fprintf(stderr
, "chroot failed\n");
5920 if (setgid(pwd
->pw_gid
) < 0) {
5921 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5924 if (setuid(pwd
->pw_uid
) < 0) {
5925 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5928 if (setuid(0) != -1) {
5929 fprintf(stderr
, "Dropping privileges failed\n");