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"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
111 #include <mmsystem.h>
115 #if defined(__APPLE__) || defined(main)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 return qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/watchdog.h"
142 #include "hw/smbios.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
156 #include "audio/audio.h"
157 #include "migration.h"
160 #include "qemu-option.h"
161 #include "qemu-config.h"
165 #include "exec-all.h"
167 #include "qemu_socket.h"
169 #include "slirp/libslirp.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
= TAILQ_HEAD_INITIALIZER(drives
);
184 struct driveoptlist driveopts
= TAILQ_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
;
189 static int64_t ticks_per_sec
;
192 NICInfo nd_table
[MAX_NICS
];
195 static int rtc_utc
= 1;
196 static int rtc_date_offset
= -1; /* -1 means no change */
197 int vga_interface_type
= VGA_CIRRUS
;
199 int graphic_width
= 1024;
200 int graphic_height
= 768;
201 int graphic_depth
= 8;
203 int graphic_width
= 800;
204 int graphic_height
= 600;
205 int graphic_depth
= 15;
207 static int full_screen
= 0;
209 static int no_frame
= 0;
212 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
213 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
214 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
216 int win2k_install_hack
= 0;
225 const char *vnc_display
;
226 int acpi_enabled
= 1;
232 int graphic_rotate
= 0;
233 uint8_t irq0override
= 1;
237 const char *watchdog
;
238 const char *option_rom
[MAX_OPTION_ROMS
];
240 int semihosting_enabled
= 0;
244 const char *qemu_name
;
246 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
247 unsigned int nb_prom_envs
= 0;
248 const char *prom_envs
[MAX_PROM_ENVS
];
253 uint64_t node_mem
[MAX_NODES
];
254 uint64_t node_cpumask
[MAX_NODES
];
256 static CPUState
*cur_cpu
;
257 static CPUState
*next_cpu
;
258 static int timer_alarm_pending
= 1;
259 /* Conversion factor from emulated instructions to virtual clock ticks. */
260 static int icount_time_shift
;
261 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
262 #define MAX_ICOUNT_SHIFT 10
263 /* Compensate for varying guest execution speed. */
264 static int64_t qemu_icount_bias
;
265 static QEMUTimer
*icount_rt_timer
;
266 static QEMUTimer
*icount_vm_timer
;
267 static QEMUTimer
*nographic_timer
;
269 uint8_t qemu_uuid
[16];
271 static QEMUBootSetHandler
*boot_set_handler
;
272 static void *boot_set_opaque
;
274 /***********************************************************/
275 /* x86 ISA bus support */
277 target_phys_addr_t isa_mem_base
= 0;
280 /***********************************************************/
281 void hw_error(const char *fmt
, ...)
287 fprintf(stderr
, "qemu: hardware error: ");
288 vfprintf(stderr
, fmt
, ap
);
289 fprintf(stderr
, "\n");
290 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
291 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
293 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
295 cpu_dump_state(env
, stderr
, fprintf
, 0);
302 static void set_proc_name(const char *s
)
304 #if defined(__linux__) && defined(PR_SET_NAME)
308 name
[sizeof(name
) - 1] = 0;
309 strncpy(name
, s
, sizeof(name
));
310 /* Could rewrite argv[0] too, but that's a bit more complicated.
311 This simple way is enough for `top'. */
312 prctl(PR_SET_NAME
, name
);
319 static QEMUBalloonEvent
*qemu_balloon_event
;
320 void *qemu_balloon_event_opaque
;
322 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
324 qemu_balloon_event
= func
;
325 qemu_balloon_event_opaque
= opaque
;
328 void qemu_balloon(ram_addr_t target
)
330 if (qemu_balloon_event
)
331 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
334 ram_addr_t
qemu_balloon_status(void)
336 if (qemu_balloon_event
)
337 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
341 /***********************************************************/
344 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
345 static void *qemu_put_kbd_event_opaque
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
347 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
349 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
351 qemu_put_kbd_event_opaque
= opaque
;
352 qemu_put_kbd_event
= func
;
355 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
356 void *opaque
, int absolute
,
359 QEMUPutMouseEntry
*s
, *cursor
;
361 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
363 s
->qemu_put_mouse_event
= func
;
364 s
->qemu_put_mouse_event_opaque
= opaque
;
365 s
->qemu_put_mouse_event_absolute
= absolute
;
366 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
369 if (!qemu_put_mouse_event_head
) {
370 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
374 cursor
= qemu_put_mouse_event_head
;
375 while (cursor
->next
!= NULL
)
376 cursor
= cursor
->next
;
379 qemu_put_mouse_event_current
= s
;
384 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
386 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
388 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
391 cursor
= qemu_put_mouse_event_head
;
392 while (cursor
!= NULL
&& cursor
!= entry
) {
394 cursor
= cursor
->next
;
397 if (cursor
== NULL
) // does not exist or list empty
399 else if (prev
== NULL
) { // entry is head
400 qemu_put_mouse_event_head
= cursor
->next
;
401 if (qemu_put_mouse_event_current
== entry
)
402 qemu_put_mouse_event_current
= cursor
->next
;
403 qemu_free(entry
->qemu_put_mouse_event_name
);
408 prev
->next
= entry
->next
;
410 if (qemu_put_mouse_event_current
== entry
)
411 qemu_put_mouse_event_current
= prev
;
413 qemu_free(entry
->qemu_put_mouse_event_name
);
417 void kbd_put_keycode(int keycode
)
419 if (qemu_put_kbd_event
) {
420 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
424 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
426 QEMUPutMouseEvent
*mouse_event
;
427 void *mouse_event_opaque
;
430 if (!qemu_put_mouse_event_current
) {
435 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
437 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
440 if (graphic_rotate
) {
441 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
444 width
= graphic_width
- 1;
445 mouse_event(mouse_event_opaque
,
446 width
- dy
, dx
, dz
, buttons_state
);
448 mouse_event(mouse_event_opaque
,
449 dx
, dy
, dz
, buttons_state
);
453 int kbd_mouse_is_absolute(void)
455 if (!qemu_put_mouse_event_current
)
458 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
461 void do_info_mice(Monitor
*mon
)
463 QEMUPutMouseEntry
*cursor
;
466 if (!qemu_put_mouse_event_head
) {
467 monitor_printf(mon
, "No mouse devices connected\n");
471 monitor_printf(mon
, "Mouse devices available:\n");
472 cursor
= qemu_put_mouse_event_head
;
473 while (cursor
!= NULL
) {
474 monitor_printf(mon
, "%c Mouse #%d: %s\n",
475 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
476 index
, cursor
->qemu_put_mouse_event_name
);
478 cursor
= cursor
->next
;
482 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
484 QEMUPutMouseEntry
*cursor
;
486 int index
= qdict_get_int(qdict
, "index");
488 if (!qemu_put_mouse_event_head
) {
489 monitor_printf(mon
, "No mouse devices connected\n");
493 cursor
= qemu_put_mouse_event_head
;
494 while (cursor
!= NULL
&& index
!= i
) {
496 cursor
= cursor
->next
;
500 qemu_put_mouse_event_current
= cursor
;
502 monitor_printf(mon
, "Mouse at given index not found\n");
505 /* compute with 96 bit intermediate result: (a*b)/c */
506 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
511 #ifdef HOST_WORDS_BIGENDIAN
521 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
522 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
525 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
529 /***********************************************************/
530 /* real time host monotonic timer */
532 #define QEMU_TIMER_BASE 1000000000LL
536 static int64_t clock_freq
;
538 static void init_get_clock(void)
542 ret
= QueryPerformanceFrequency(&freq
);
544 fprintf(stderr
, "Could not calibrate ticks\n");
547 clock_freq
= freq
.QuadPart
;
550 static int64_t get_clock(void)
553 QueryPerformanceCounter(&ti
);
554 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
559 static int use_rt_clock
;
561 static void init_get_clock(void)
564 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
565 || defined(__DragonFly__)
568 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
575 static int64_t get_clock(void)
577 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
578 || defined(__DragonFly__)
581 clock_gettime(CLOCK_MONOTONIC
, &ts
);
582 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
586 /* XXX: using gettimeofday leads to problems if the date
587 changes, so it should be avoided. */
589 gettimeofday(&tv
, NULL
);
590 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
595 /* Return the virtual CPU time, based on the instruction counter. */
596 static int64_t cpu_get_icount(void)
599 CPUState
*env
= cpu_single_env
;;
600 icount
= qemu_icount
;
603 fprintf(stderr
, "Bad clock read\n");
604 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
606 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
609 /***********************************************************/
610 /* guest cycle counter */
612 static int64_t cpu_ticks_prev
;
613 static int64_t cpu_ticks_offset
;
614 static int64_t cpu_clock_offset
;
615 static int cpu_ticks_enabled
;
617 /* return the host CPU cycle counter and handle stop/restart */
618 int64_t cpu_get_ticks(void)
621 return cpu_get_icount();
623 if (!cpu_ticks_enabled
) {
624 return cpu_ticks_offset
;
627 ticks
= cpu_get_real_ticks();
628 if (cpu_ticks_prev
> ticks
) {
629 /* Note: non increasing ticks may happen if the host uses
631 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
633 cpu_ticks_prev
= ticks
;
634 return ticks
+ cpu_ticks_offset
;
638 /* return the host CPU monotonic timer and handle stop/restart */
639 static int64_t cpu_get_clock(void)
642 if (!cpu_ticks_enabled
) {
643 return cpu_clock_offset
;
646 return ti
+ cpu_clock_offset
;
650 /* enable cpu_get_ticks() */
651 void cpu_enable_ticks(void)
653 if (!cpu_ticks_enabled
) {
654 cpu_ticks_offset
-= cpu_get_real_ticks();
655 cpu_clock_offset
-= get_clock();
656 cpu_ticks_enabled
= 1;
660 /* disable cpu_get_ticks() : the clock is stopped. You must not call
661 cpu_get_ticks() after that. */
662 void cpu_disable_ticks(void)
664 if (cpu_ticks_enabled
) {
665 cpu_ticks_offset
= cpu_get_ticks();
666 cpu_clock_offset
= cpu_get_clock();
667 cpu_ticks_enabled
= 0;
671 /***********************************************************/
674 #define QEMU_TIMER_REALTIME 0
675 #define QEMU_TIMER_VIRTUAL 1
679 /* XXX: add frequency */
687 struct QEMUTimer
*next
;
690 struct qemu_alarm_timer
{
694 int (*start
)(struct qemu_alarm_timer
*t
);
695 void (*stop
)(struct qemu_alarm_timer
*t
);
696 void (*rearm
)(struct qemu_alarm_timer
*t
);
700 #define ALARM_FLAG_DYNTICKS 0x1
701 #define ALARM_FLAG_EXPIRED 0x2
703 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
705 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
708 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
710 if (!alarm_has_dynticks(t
))
716 /* TODO: MIN_TIMER_REARM_US should be optimized */
717 #define MIN_TIMER_REARM_US 250
719 static struct qemu_alarm_timer
*alarm_timer
;
723 struct qemu_alarm_win32
{
726 } alarm_win32_data
= {0, -1};
728 static int win32_start_timer(struct qemu_alarm_timer
*t
);
729 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
730 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
734 static int unix_start_timer(struct qemu_alarm_timer
*t
);
735 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
739 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
740 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
741 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
743 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
744 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
746 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
747 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
749 #endif /* __linux__ */
753 /* Correlation between real and virtual time is always going to be
754 fairly approximate, so ignore small variation.
755 When the guest is idle real and virtual time will be aligned in
757 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
759 static void icount_adjust(void)
764 static int64_t last_delta
;
765 /* If the VM is not running, then do nothing. */
769 cur_time
= cpu_get_clock();
770 cur_icount
= qemu_get_clock(vm_clock
);
771 delta
= cur_icount
- cur_time
;
772 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
774 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
775 && icount_time_shift
> 0) {
776 /* The guest is getting too far ahead. Slow time down. */
780 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
781 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
782 /* The guest is getting too far behind. Speed time up. */
786 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
789 static void icount_adjust_rt(void * opaque
)
791 qemu_mod_timer(icount_rt_timer
,
792 qemu_get_clock(rt_clock
) + 1000);
796 static void icount_adjust_vm(void * opaque
)
798 qemu_mod_timer(icount_vm_timer
,
799 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
803 static void init_icount_adjust(void)
805 /* Have both realtime and virtual time triggers for speed adjustment.
806 The realtime trigger catches emulated time passing too slowly,
807 the virtual time trigger catches emulated time passing too fast.
808 Realtime triggers occur even when idle, so use them less frequently
810 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
811 qemu_mod_timer(icount_rt_timer
,
812 qemu_get_clock(rt_clock
) + 1000);
813 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
814 qemu_mod_timer(icount_vm_timer
,
815 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
818 static struct qemu_alarm_timer alarm_timers
[] = {
821 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
822 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
823 /* HPET - if available - is preferred */
824 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
825 /* ...otherwise try RTC */
826 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
828 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
830 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
831 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
832 {"win32", 0, win32_start_timer
,
833 win32_stop_timer
, NULL
, &alarm_win32_data
},
838 static void show_available_alarms(void)
842 printf("Available alarm timers, in order of precedence:\n");
843 for (i
= 0; alarm_timers
[i
].name
; i
++)
844 printf("%s\n", alarm_timers
[i
].name
);
847 static void configure_alarms(char const *opt
)
851 int count
= ARRAY_SIZE(alarm_timers
) - 1;
854 struct qemu_alarm_timer tmp
;
856 if (!strcmp(opt
, "?")) {
857 show_available_alarms();
861 arg
= qemu_strdup(opt
);
863 /* Reorder the array */
864 name
= strtok(arg
, ",");
866 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
867 if (!strcmp(alarm_timers
[i
].name
, name
))
872 fprintf(stderr
, "Unknown clock %s\n", name
);
881 tmp
= alarm_timers
[i
];
882 alarm_timers
[i
] = alarm_timers
[cur
];
883 alarm_timers
[cur
] = tmp
;
887 name
= strtok(NULL
, ",");
893 /* Disable remaining timers */
894 for (i
= cur
; i
< count
; i
++)
895 alarm_timers
[i
].name
= NULL
;
897 show_available_alarms();
905 static QEMUTimer
*active_timers
[2];
907 static QEMUClock
*qemu_new_clock(int type
)
910 clock
= qemu_mallocz(sizeof(QEMUClock
));
915 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
919 ts
= qemu_mallocz(sizeof(QEMUTimer
));
926 void qemu_free_timer(QEMUTimer
*ts
)
931 /* stop a timer, but do not dealloc it */
932 void qemu_del_timer(QEMUTimer
*ts
)
936 /* NOTE: this code must be signal safe because
937 qemu_timer_expired() can be called from a signal. */
938 pt
= &active_timers
[ts
->clock
->type
];
951 /* modify the current timer so that it will be fired when current_time
952 >= expire_time. The corresponding callback will be called. */
953 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
959 /* add the timer in the sorted list */
960 /* NOTE: this code must be signal safe because
961 qemu_timer_expired() can be called from a signal. */
962 pt
= &active_timers
[ts
->clock
->type
];
967 if (t
->expire_time
> expire_time
)
971 ts
->expire_time
= expire_time
;
975 /* Rearm if necessary */
976 if (pt
== &active_timers
[ts
->clock
->type
]) {
977 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
978 qemu_rearm_alarm_timer(alarm_timer
);
980 /* Interrupt execution to force deadline recalculation. */
986 int qemu_timer_pending(QEMUTimer
*ts
)
989 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
996 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1000 return (timer_head
->expire_time
<= current_time
);
1003 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1009 if (!ts
|| ts
->expire_time
> current_time
)
1011 /* remove timer from the list before calling the callback */
1012 *ptimer_head
= ts
->next
;
1015 /* run the callback (the timer list can be modified) */
1020 int64_t qemu_get_clock(QEMUClock
*clock
)
1022 switch(clock
->type
) {
1023 case QEMU_TIMER_REALTIME
:
1024 return get_clock() / 1000000;
1026 case QEMU_TIMER_VIRTUAL
:
1028 return cpu_get_icount();
1030 return cpu_get_clock();
1035 int64_t get_ticks_per_sec(void)
1037 return ticks_per_sec
;
1040 static void init_timers(void)
1043 ticks_per_sec
= QEMU_TIMER_BASE
;
1044 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1045 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1049 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1051 uint64_t expire_time
;
1053 if (qemu_timer_pending(ts
)) {
1054 expire_time
= ts
->expire_time
;
1058 qemu_put_be64(f
, expire_time
);
1061 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1063 uint64_t expire_time
;
1065 expire_time
= qemu_get_be64(f
);
1066 if (expire_time
!= -1) {
1067 qemu_mod_timer(ts
, expire_time
);
1073 static void timer_save(QEMUFile
*f
, void *opaque
)
1075 qemu_put_be64(f
, cpu_ticks_offset
);
1076 qemu_put_be64(f
, ticks_per_sec
);
1077 qemu_put_be64(f
, cpu_clock_offset
);
1080 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1082 if (version_id
!= 1 && version_id
!= 2)
1084 cpu_ticks_offset
=qemu_get_be64(f
);
1085 ticks_per_sec
=qemu_get_be64(f
);
1086 if (version_id
== 2) {
1087 cpu_clock_offset
=qemu_get_be64(f
);
1092 static void qemu_event_increment(void);
1095 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1096 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1099 static void host_alarm_handler(int host_signum
)
1103 #define DISP_FREQ 1000
1105 static int64_t delta_min
= INT64_MAX
;
1106 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1108 ti
= qemu_get_clock(vm_clock
);
1109 if (last_clock
!= 0) {
1110 delta
= ti
- last_clock
;
1111 if (delta
< delta_min
)
1113 if (delta
> delta_max
)
1116 if (++count
== DISP_FREQ
) {
1117 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1118 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1119 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1120 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1121 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1123 delta_min
= INT64_MAX
;
1131 if (alarm_has_dynticks(alarm_timer
) ||
1133 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1134 qemu_get_clock(vm_clock
))) ||
1135 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1136 qemu_get_clock(rt_clock
))) {
1137 qemu_event_increment();
1138 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1140 #ifndef CONFIG_IOTHREAD
1142 /* stop the currently executing cpu because a timer occured */
1146 timer_alarm_pending
= 1;
1147 qemu_notify_event();
1151 static int64_t qemu_next_deadline(void)
1155 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1156 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1157 qemu_get_clock(vm_clock
);
1159 /* To avoid problems with overflow limit this to 2^32. */
1169 #if defined(__linux__) || defined(_WIN32)
1170 static uint64_t qemu_next_deadline_dyntick(void)
1178 delta
= (qemu_next_deadline() + 999) / 1000;
1180 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1181 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1182 qemu_get_clock(rt_clock
))*1000;
1183 if (rtdelta
< delta
)
1187 if (delta
< MIN_TIMER_REARM_US
)
1188 delta
= MIN_TIMER_REARM_US
;
1196 /* Sets a specific flag */
1197 static int fcntl_setfl(int fd
, int flag
)
1201 flags
= fcntl(fd
, F_GETFL
);
1205 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1211 #if defined(__linux__)
1213 #define RTC_FREQ 1024
1215 static void enable_sigio_timer(int fd
)
1217 struct sigaction act
;
1220 sigfillset(&act
.sa_mask
);
1222 act
.sa_handler
= host_alarm_handler
;
1224 sigaction(SIGIO
, &act
, NULL
);
1225 fcntl_setfl(fd
, O_ASYNC
);
1226 fcntl(fd
, F_SETOWN
, getpid());
1229 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1231 struct hpet_info info
;
1234 fd
= open("/dev/hpet", O_RDONLY
);
1239 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1241 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1242 "error, but for better emulation accuracy type:\n"
1243 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1247 /* Check capabilities */
1248 r
= ioctl(fd
, HPET_INFO
, &info
);
1252 /* Enable periodic mode */
1253 r
= ioctl(fd
, HPET_EPI
, 0);
1254 if (info
.hi_flags
&& (r
< 0))
1257 /* Enable interrupt */
1258 r
= ioctl(fd
, HPET_IE_ON
, 0);
1262 enable_sigio_timer(fd
);
1263 t
->priv
= (void *)(long)fd
;
1271 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1273 int fd
= (long)t
->priv
;
1278 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1281 unsigned long current_rtc_freq
= 0;
1283 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1286 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1287 if (current_rtc_freq
!= RTC_FREQ
&&
1288 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1289 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1290 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1291 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1294 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1300 enable_sigio_timer(rtc_fd
);
1302 t
->priv
= (void *)(long)rtc_fd
;
1307 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1309 int rtc_fd
= (long)t
->priv
;
1314 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1318 struct sigaction act
;
1320 sigfillset(&act
.sa_mask
);
1322 act
.sa_handler
= host_alarm_handler
;
1324 sigaction(SIGALRM
, &act
, NULL
);
1327 * Initialize ev struct to 0 to avoid valgrind complaining
1328 * about uninitialized data in timer_create call
1330 memset(&ev
, 0, sizeof(ev
));
1331 ev
.sigev_value
.sival_int
= 0;
1332 ev
.sigev_notify
= SIGEV_SIGNAL
;
1333 ev
.sigev_signo
= SIGALRM
;
1335 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1336 perror("timer_create");
1338 /* disable dynticks */
1339 fprintf(stderr
, "Dynamic Ticks disabled\n");
1344 t
->priv
= (void *)(long)host_timer
;
1349 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1351 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1353 timer_delete(host_timer
);
1356 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1358 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1359 struct itimerspec timeout
;
1360 int64_t nearest_delta_us
= INT64_MAX
;
1363 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1364 !active_timers
[QEMU_TIMER_VIRTUAL
])
1367 nearest_delta_us
= qemu_next_deadline_dyntick();
1369 /* check whether a timer is already running */
1370 if (timer_gettime(host_timer
, &timeout
)) {
1372 fprintf(stderr
, "Internal timer error: aborting\n");
1375 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1376 if (current_us
&& current_us
<= nearest_delta_us
)
1379 timeout
.it_interval
.tv_sec
= 0;
1380 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1381 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1382 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1383 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1385 fprintf(stderr
, "Internal timer error: aborting\n");
1390 #endif /* defined(__linux__) */
1392 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1394 struct sigaction act
;
1395 struct itimerval itv
;
1399 sigfillset(&act
.sa_mask
);
1401 act
.sa_handler
= host_alarm_handler
;
1403 sigaction(SIGALRM
, &act
, NULL
);
1405 itv
.it_interval
.tv_sec
= 0;
1406 /* for i386 kernel 2.6 to get 1 ms */
1407 itv
.it_interval
.tv_usec
= 999;
1408 itv
.it_value
.tv_sec
= 0;
1409 itv
.it_value
.tv_usec
= 10 * 1000;
1411 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1418 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1420 struct itimerval itv
;
1422 memset(&itv
, 0, sizeof(itv
));
1423 setitimer(ITIMER_REAL
, &itv
, NULL
);
1426 #endif /* !defined(_WIN32) */
1431 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1434 struct qemu_alarm_win32
*data
= t
->priv
;
1437 memset(&tc
, 0, sizeof(tc
));
1438 timeGetDevCaps(&tc
, sizeof(tc
));
1440 if (data
->period
< tc
.wPeriodMin
)
1441 data
->period
= tc
.wPeriodMin
;
1443 timeBeginPeriod(data
->period
);
1445 flags
= TIME_CALLBACK_FUNCTION
;
1446 if (alarm_has_dynticks(t
))
1447 flags
|= TIME_ONESHOT
;
1449 flags
|= TIME_PERIODIC
;
1451 data
->timerId
= timeSetEvent(1, // interval (ms)
1452 data
->period
, // resolution
1453 host_alarm_handler
, // function
1454 (DWORD
)t
, // parameter
1457 if (!data
->timerId
) {
1458 perror("Failed to initialize win32 alarm timer");
1459 timeEndPeriod(data
->period
);
1466 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1468 struct qemu_alarm_win32
*data
= t
->priv
;
1470 timeKillEvent(data
->timerId
);
1471 timeEndPeriod(data
->period
);
1474 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1476 struct qemu_alarm_win32
*data
= t
->priv
;
1477 uint64_t nearest_delta_us
;
1479 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1480 !active_timers
[QEMU_TIMER_VIRTUAL
])
1483 nearest_delta_us
= qemu_next_deadline_dyntick();
1484 nearest_delta_us
/= 1000;
1486 timeKillEvent(data
->timerId
);
1488 data
->timerId
= timeSetEvent(1,
1492 TIME_ONESHOT
| TIME_PERIODIC
);
1494 if (!data
->timerId
) {
1495 perror("Failed to re-arm win32 alarm timer");
1497 timeEndPeriod(data
->period
);
1504 static int init_timer_alarm(void)
1506 struct qemu_alarm_timer
*t
= NULL
;
1509 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1510 t
= &alarm_timers
[i
];
1530 static void quit_timers(void)
1532 alarm_timer
->stop(alarm_timer
);
1536 /***********************************************************/
1537 /* host time/date access */
1538 void qemu_get_timedate(struct tm
*tm
, int offset
)
1545 if (rtc_date_offset
== -1) {
1549 ret
= localtime(&ti
);
1551 ti
-= rtc_date_offset
;
1555 memcpy(tm
, ret
, sizeof(struct tm
));
1558 int qemu_timedate_diff(struct tm
*tm
)
1562 if (rtc_date_offset
== -1)
1564 seconds
= mktimegm(tm
);
1566 seconds
= mktime(tm
);
1568 seconds
= mktimegm(tm
) + rtc_date_offset
;
1570 return seconds
- time(NULL
);
1574 static void socket_cleanup(void)
1579 static int socket_init(void)
1584 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1586 err
= WSAGetLastError();
1587 fprintf(stderr
, "WSAStartup: %d\n", err
);
1590 atexit(socket_cleanup
);
1595 /***********************************************************/
1596 /* Bluetooth support */
1599 static struct HCIInfo
*hci_table
[MAX_NICS
];
1601 static struct bt_vlan_s
{
1602 struct bt_scatternet_s net
;
1604 struct bt_vlan_s
*next
;
1607 /* find or alloc a new bluetooth "VLAN" */
1608 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1610 struct bt_vlan_s
**pvlan
, *vlan
;
1611 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1615 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1617 pvlan
= &first_bt_vlan
;
1618 while (*pvlan
!= NULL
)
1619 pvlan
= &(*pvlan
)->next
;
1624 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1628 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1633 static struct HCIInfo null_hci
= {
1634 .cmd_send
= null_hci_send
,
1635 .sco_send
= null_hci_send
,
1636 .acl_send
= null_hci_send
,
1637 .bdaddr_set
= null_hci_addr_set
,
1640 struct HCIInfo
*qemu_next_hci(void)
1642 if (cur_hci
== nb_hcis
)
1645 return hci_table
[cur_hci
++];
1648 static struct HCIInfo
*hci_init(const char *str
)
1651 struct bt_scatternet_s
*vlan
= 0;
1653 if (!strcmp(str
, "null"))
1656 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1658 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1659 else if (!strncmp(str
, "hci", 3)) {
1662 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1663 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1668 vlan
= qemu_find_bt_vlan(0);
1670 return bt_new_hci(vlan
);
1673 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1678 static int bt_hci_parse(const char *str
)
1680 struct HCIInfo
*hci
;
1683 if (nb_hcis
>= MAX_NICS
) {
1684 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1688 hci
= hci_init(str
);
1697 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1698 hci
->bdaddr_set(hci
, bdaddr
.b
);
1700 hci_table
[nb_hcis
++] = hci
;
1705 static void bt_vhci_add(int vlan_id
)
1707 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1710 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1711 "an empty scatternet %i\n", vlan_id
);
1713 bt_vhci_init(bt_new_hci(vlan
));
1716 static struct bt_device_s
*bt_device_add(const char *opt
)
1718 struct bt_scatternet_s
*vlan
;
1720 char *endp
= strstr(opt
, ",vlan=");
1721 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1724 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1727 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1729 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1734 vlan
= qemu_find_bt_vlan(vlan_id
);
1737 fprintf(stderr
, "qemu: warning: adding a slave device to "
1738 "an empty scatternet %i\n", vlan_id
);
1740 if (!strcmp(devname
, "keyboard"))
1741 return bt_keyboard_init(vlan
);
1743 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1747 static int bt_parse(const char *opt
)
1749 const char *endp
, *p
;
1752 if (strstart(opt
, "hci", &endp
)) {
1753 if (!*endp
|| *endp
== ',') {
1755 if (!strstart(endp
, ",vlan=", 0))
1758 return bt_hci_parse(opt
);
1760 } else if (strstart(opt
, "vhci", &endp
)) {
1761 if (!*endp
|| *endp
== ',') {
1763 if (strstart(endp
, ",vlan=", &p
)) {
1764 vlan
= strtol(p
, (char **) &endp
, 0);
1766 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1770 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1779 } else if (strstart(opt
, "device:", &endp
))
1780 return !bt_device_add(endp
);
1782 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1786 /***********************************************************/
1787 /* QEMU Block devices */
1789 #define HD_ALIAS "index=%d,media=disk"
1790 #define CDROM_ALIAS "index=2,media=cdrom"
1791 #define FD_ALIAS "index=%d,if=floppy"
1792 #define PFLASH_ALIAS "if=pflash"
1793 #define MTD_ALIAS "if=mtd"
1794 #define SD_ALIAS "index=0,if=sd"
1796 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1803 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1806 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1808 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1809 __FUNCTION__
, optstr
);
1813 qemu_opt_set(opts
, "file", file
);
1817 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1821 /* seek interface, bus and unit */
1823 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1824 if (dinfo
->type
== type
&&
1825 dinfo
->bus
== bus
&&
1826 dinfo
->unit
== unit
)
1833 DriveInfo
*drive_get_by_id(const char *id
)
1837 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1838 if (strcmp(id
, dinfo
->id
))
1845 int drive_get_max_bus(BlockInterfaceType type
)
1851 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1852 if(dinfo
->type
== type
&&
1853 dinfo
->bus
> max_bus
)
1854 max_bus
= dinfo
->bus
;
1859 const char *drive_get_serial(BlockDriverState
*bdrv
)
1863 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1864 if (dinfo
->bdrv
== bdrv
)
1865 return dinfo
->serial
;
1871 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1875 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1876 if (dinfo
->bdrv
== bdrv
)
1877 return dinfo
->onerror
;
1880 return BLOCK_ERR_STOP_ENOSPC
;
1883 static void bdrv_format_print(void *opaque
, const char *name
)
1885 fprintf(stderr
, " %s", name
);
1888 void drive_uninit(BlockDriverState
*bdrv
)
1892 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1893 if (dinfo
->bdrv
!= bdrv
)
1895 qemu_opts_del(dinfo
->opts
);
1896 TAILQ_REMOVE(&drives
, dinfo
, next
);
1902 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1906 const char *file
= NULL
;
1909 const char *mediastr
= "";
1910 BlockInterfaceType type
;
1911 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1912 int bus_id
, unit_id
;
1913 int cyls
, heads
, secs
, translation
;
1914 BlockDriver
*drv
= NULL
;
1915 QEMUMachine
*machine
= opaque
;
1920 int bdrv_flags
, onerror
;
1921 const char *devaddr
;
1927 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1930 if (machine
&& machine
->use_scsi
) {
1932 max_devs
= MAX_SCSI_DEVS
;
1933 pstrcpy(devname
, sizeof(devname
), "scsi");
1936 max_devs
= MAX_IDE_DEVS
;
1937 pstrcpy(devname
, sizeof(devname
), "ide");
1941 /* extract parameters */
1942 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1943 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1944 index
= qemu_opt_get_number(opts
, "index", -1);
1946 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1947 heads
= qemu_opt_get_number(opts
, "heads", 0);
1948 secs
= qemu_opt_get_number(opts
, "secs", 0);
1950 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1952 file
= qemu_opt_get(opts
, "file");
1953 serial
= qemu_opt_get(opts
, "serial");
1955 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1956 pstrcpy(devname
, sizeof(devname
), buf
);
1957 if (!strcmp(buf
, "ide")) {
1959 max_devs
= MAX_IDE_DEVS
;
1960 } else if (!strcmp(buf
, "scsi")) {
1962 max_devs
= MAX_SCSI_DEVS
;
1963 } else if (!strcmp(buf
, "floppy")) {
1966 } else if (!strcmp(buf
, "pflash")) {
1969 } else if (!strcmp(buf
, "mtd")) {
1972 } else if (!strcmp(buf
, "sd")) {
1975 } else if (!strcmp(buf
, "virtio")) {
1978 } else if (!strcmp(buf
, "xen")) {
1981 } else if (!strcmp(buf
, "none")) {
1985 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1990 if (cyls
|| heads
|| secs
) {
1991 if (cyls
< 1 || cyls
> 16383) {
1992 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
1995 if (heads
< 1 || heads
> 16) {
1996 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
1999 if (secs
< 1 || secs
> 63) {
2000 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2005 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2008 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2012 if (!strcmp(buf
, "none"))
2013 translation
= BIOS_ATA_TRANSLATION_NONE
;
2014 else if (!strcmp(buf
, "lba"))
2015 translation
= BIOS_ATA_TRANSLATION_LBA
;
2016 else if (!strcmp(buf
, "auto"))
2017 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2019 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2024 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2025 if (!strcmp(buf
, "disk")) {
2027 } else if (!strcmp(buf
, "cdrom")) {
2028 if (cyls
|| secs
|| heads
) {
2030 "qemu: '%s' invalid physical CHS format\n", buf
);
2033 media
= MEDIA_CDROM
;
2035 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2040 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2041 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2043 else if (!strcmp(buf
, "writethrough"))
2045 else if (!strcmp(buf
, "writeback"))
2048 fprintf(stderr
, "qemu: invalid cache option\n");
2053 #ifdef CONFIG_LINUX_AIO
2054 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2055 if (!strcmp(buf
, "threads"))
2057 else if (!strcmp(buf
, "native"))
2060 fprintf(stderr
, "qemu: invalid aio option\n");
2066 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2067 if (strcmp(buf
, "?") == 0) {
2068 fprintf(stderr
, "qemu: Supported formats:");
2069 bdrv_iterate_format(bdrv_format_print
, NULL
);
2070 fprintf(stderr
, "\n");
2073 drv
= bdrv_find_format(buf
);
2075 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2080 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2081 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2082 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2083 fprintf(stderr
, "werror is no supported by this format\n");
2086 if (!strcmp(buf
, "ignore"))
2087 onerror
= BLOCK_ERR_IGNORE
;
2088 else if (!strcmp(buf
, "enospc"))
2089 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2090 else if (!strcmp(buf
, "stop"))
2091 onerror
= BLOCK_ERR_STOP_ANY
;
2092 else if (!strcmp(buf
, "report"))
2093 onerror
= BLOCK_ERR_REPORT
;
2095 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2100 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2101 if (type
!= IF_VIRTIO
) {
2102 fprintf(stderr
, "addr is not supported\n");
2107 /* compute bus and unit according index */
2110 if (bus_id
!= 0 || unit_id
!= -1) {
2112 "qemu: index cannot be used with bus and unit\n");
2120 unit_id
= index
% max_devs
;
2121 bus_id
= index
/ max_devs
;
2125 /* if user doesn't specify a unit_id,
2126 * try to find the first free
2129 if (unit_id
== -1) {
2131 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2133 if (max_devs
&& unit_id
>= max_devs
) {
2134 unit_id
-= max_devs
;
2142 if (max_devs
&& unit_id
>= max_devs
) {
2143 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2144 unit_id
, max_devs
- 1);
2149 * ignore multiple definitions
2152 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2159 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2160 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2161 dinfo
->id
= qemu_strdup(buf
);
2163 /* no id supplied -> create one */
2164 dinfo
->id
= qemu_mallocz(32);
2165 if (type
== IF_IDE
|| type
== IF_SCSI
)
2166 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2168 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2169 devname
, bus_id
, mediastr
, unit_id
);
2171 snprintf(dinfo
->id
, 32, "%s%s%i",
2172 devname
, mediastr
, unit_id
);
2174 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2175 dinfo
->devaddr
= devaddr
;
2177 dinfo
->bus
= bus_id
;
2178 dinfo
->unit
= unit_id
;
2179 dinfo
->onerror
= onerror
;
2182 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2183 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2192 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2193 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2197 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2202 /* FIXME: This isn't really a floppy, but it's a reasonable
2205 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2212 /* add virtio block device */
2213 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2214 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2215 qemu_opt_set(opts
, "drive", dinfo
->id
);
2217 qemu_opt_set(opts
, "addr", devaddr
);
2228 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2229 cache
= 2; /* always use write-back with snapshot */
2231 if (cache
== 0) /* no caching */
2232 bdrv_flags
|= BDRV_O_NOCACHE
;
2233 else if (cache
== 2) /* write-back */
2234 bdrv_flags
|= BDRV_O_CACHE_WB
;
2237 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2239 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2242 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2243 fprintf(stderr
, "qemu: could not open disk image %s\n",
2248 if (bdrv_key_required(dinfo
->bdrv
))
2254 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2256 QEMUMachine
*machine
= opaque
;
2257 int fatal_error
= 0;
2259 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2266 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2268 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2269 qemu_opt_set(opts
, "snapshot", "on");
2274 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2276 boot_set_handler
= func
;
2277 boot_set_opaque
= opaque
;
2280 int qemu_boot_set(const char *boot_devices
)
2282 if (!boot_set_handler
) {
2285 return boot_set_handler(boot_set_opaque
, boot_devices
);
2288 static int parse_bootdevices(char *devices
)
2290 /* We just do some generic consistency checks */
2294 for (p
= devices
; *p
!= '\0'; p
++) {
2295 /* Allowed boot devices are:
2296 * a-b: floppy disk drives
2297 * c-f: IDE disk drives
2298 * g-m: machine implementation dependant drives
2299 * n-p: network devices
2300 * It's up to each machine implementation to check if the given boot
2301 * devices match the actual hardware implementation and firmware
2304 if (*p
< 'a' || *p
> 'p') {
2305 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2308 if (bitmap
& (1 << (*p
- 'a'))) {
2309 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2312 bitmap
|= 1 << (*p
- 'a');
2317 static void restore_boot_devices(void *opaque
)
2319 char *standard_boot_devices
= opaque
;
2321 qemu_boot_set(standard_boot_devices
);
2323 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2324 qemu_free(standard_boot_devices
);
2327 static void numa_add(const char *optarg
)
2331 unsigned long long value
, endvalue
;
2334 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2335 if (!strcmp(option
, "node")) {
2336 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2337 nodenr
= nb_numa_nodes
;
2339 nodenr
= strtoull(option
, NULL
, 10);
2342 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2343 node_mem
[nodenr
] = 0;
2345 value
= strtoull(option
, &endptr
, 0);
2347 case 0: case 'M': case 'm':
2354 node_mem
[nodenr
] = value
;
2356 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2357 node_cpumask
[nodenr
] = 0;
2359 value
= strtoull(option
, &endptr
, 10);
2362 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2364 if (*endptr
== '-') {
2365 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2366 if (endvalue
>= 63) {
2369 "only 63 CPUs in NUMA mode supported.\n");
2371 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2376 node_cpumask
[nodenr
] = value
;
2383 static void smp_parse(const char *optarg
)
2385 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2389 smp
= strtoul(optarg
, &endptr
, 10);
2390 if (endptr
!= optarg
) {
2391 if (*endptr
== ',') {
2395 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2396 sockets
= strtoull(option
, NULL
, 10);
2397 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2398 cores
= strtoull(option
, NULL
, 10);
2399 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2400 threads
= strtoull(option
, NULL
, 10);
2401 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2402 max_cpus
= strtoull(option
, NULL
, 10);
2404 /* compute missing values, prefer sockets over cores over threads */
2405 if (smp
== 0 || sockets
== 0) {
2406 sockets
= sockets
> 0 ? sockets
: 1;
2407 cores
= cores
> 0 ? cores
: 1;
2408 threads
= threads
> 0 ? threads
: 1;
2410 smp
= cores
* threads
* sockets
;
2412 sockets
= smp
/ (cores
* threads
);
2416 threads
= threads
> 0 ? threads
: 1;
2417 cores
= smp
/ (sockets
* threads
);
2420 sockets
= smp
/ (cores
* threads
);
2422 threads
= smp
/ (cores
* sockets
);
2427 smp_cores
= cores
> 0 ? cores
: 1;
2428 smp_threads
= threads
> 0 ? threads
: 1;
2430 max_cpus
= smp_cpus
;
2433 /***********************************************************/
2436 static void usb_msd_password_cb(void *opaque
, int err
)
2438 USBDevice
*dev
= opaque
;
2441 usb_device_attach(dev
);
2443 dev
->info
->handle_destroy(dev
);
2452 .qdev
= "QEMU USB Mouse",
2455 .qdev
= "QEMU USB Tablet",
2458 .qdev
= "QEMU USB Keyboard",
2460 .name
= "wacom-tablet",
2461 .qdev
= "QEMU PenPartner Tablet",
2465 static int usb_device_add(const char *devname
, int is_hotplug
)
2468 USBBus
*bus
= usb_bus_find(-1 /* any */);
2469 USBDevice
*dev
= NULL
;
2475 /* simple devices which don't need extra care */
2476 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2477 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2479 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2483 /* the other ones */
2484 if (strstart(devname
, "host:", &p
)) {
2485 dev
= usb_host_device_open(p
);
2486 } else if (strstart(devname
, "disk:", &p
)) {
2487 BlockDriverState
*bs
;
2489 dev
= usb_msd_init(p
);
2492 bs
= usb_msd_get_bdrv(dev
);
2493 if (bdrv_key_required(bs
)) {
2496 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2501 } else if (strstart(devname
, "serial:", &p
)) {
2502 dev
= usb_serial_init(p
);
2503 #ifdef CONFIG_BRLAPI
2504 } else if (!strcmp(devname
, "braille")) {
2505 dev
= usb_baum_init();
2507 } else if (strstart(devname
, "net:", &p
)) {
2510 if (net_client_init(NULL
, "nic", p
) < 0)
2512 nd_table
[nic
].model
= "usb";
2513 dev
= usb_net_init(&nd_table
[nic
]);
2514 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2515 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2516 bt_new_hci(qemu_find_bt_vlan(0)));
2527 static int usb_device_del(const char *devname
)
2532 if (strstart(devname
, "host:", &p
))
2533 return usb_host_device_close(p
);
2538 p
= strchr(devname
, '.');
2541 bus_num
= strtoul(devname
, NULL
, 0);
2542 addr
= strtoul(p
+ 1, NULL
, 0);
2544 return usb_device_delete_addr(bus_num
, addr
);
2547 static int usb_parse(const char *cmdline
)
2549 return usb_device_add(cmdline
, 0);
2552 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2554 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2557 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2559 usb_device_del(qdict_get_str(qdict
, "devname"));
2562 /***********************************************************/
2563 /* PCMCIA/Cardbus */
2565 static struct pcmcia_socket_entry_s
{
2566 PCMCIASocket
*socket
;
2567 struct pcmcia_socket_entry_s
*next
;
2568 } *pcmcia_sockets
= 0;
2570 void pcmcia_socket_register(PCMCIASocket
*socket
)
2572 struct pcmcia_socket_entry_s
*entry
;
2574 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2575 entry
->socket
= socket
;
2576 entry
->next
= pcmcia_sockets
;
2577 pcmcia_sockets
= entry
;
2580 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2582 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2584 ptr
= &pcmcia_sockets
;
2585 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2586 if (entry
->socket
== socket
) {
2592 void pcmcia_info(Monitor
*mon
)
2594 struct pcmcia_socket_entry_s
*iter
;
2596 if (!pcmcia_sockets
)
2597 monitor_printf(mon
, "No PCMCIA sockets\n");
2599 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2600 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2601 iter
->socket
->attached
? iter
->socket
->card_string
:
2605 /***********************************************************/
2606 /* register display */
2608 struct DisplayAllocator default_allocator
= {
2609 defaultallocator_create_displaysurface
,
2610 defaultallocator_resize_displaysurface
,
2611 defaultallocator_free_displaysurface
2614 void register_displaystate(DisplayState
*ds
)
2624 DisplayState
*get_displaystate(void)
2626 return display_state
;
2629 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2631 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2632 return ds
->allocator
;
2637 static void dumb_display_init(void)
2639 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2640 ds
->allocator
= &default_allocator
;
2641 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2642 register_displaystate(ds
);
2645 /***********************************************************/
2648 typedef struct IOHandlerRecord
{
2650 IOCanRWHandler
*fd_read_poll
;
2652 IOHandler
*fd_write
;
2655 /* temporary data */
2657 struct IOHandlerRecord
*next
;
2660 static IOHandlerRecord
*first_io_handler
;
2662 /* XXX: fd_read_poll should be suppressed, but an API change is
2663 necessary in the character devices to suppress fd_can_read(). */
2664 int qemu_set_fd_handler2(int fd
,
2665 IOCanRWHandler
*fd_read_poll
,
2667 IOHandler
*fd_write
,
2670 IOHandlerRecord
**pioh
, *ioh
;
2672 if (!fd_read
&& !fd_write
) {
2673 pioh
= &first_io_handler
;
2678 if (ioh
->fd
== fd
) {
2685 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2689 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2690 ioh
->next
= first_io_handler
;
2691 first_io_handler
= ioh
;
2694 ioh
->fd_read_poll
= fd_read_poll
;
2695 ioh
->fd_read
= fd_read
;
2696 ioh
->fd_write
= fd_write
;
2697 ioh
->opaque
= opaque
;
2703 int qemu_set_fd_handler(int fd
,
2705 IOHandler
*fd_write
,
2708 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2712 /***********************************************************/
2713 /* Polling handling */
2715 typedef struct PollingEntry
{
2718 struct PollingEntry
*next
;
2721 static PollingEntry
*first_polling_entry
;
2723 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2725 PollingEntry
**ppe
, *pe
;
2726 pe
= qemu_mallocz(sizeof(PollingEntry
));
2728 pe
->opaque
= opaque
;
2729 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2734 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2736 PollingEntry
**ppe
, *pe
;
2737 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2739 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2747 /***********************************************************/
2748 /* Wait objects support */
2749 typedef struct WaitObjects
{
2751 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2752 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2753 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2756 static WaitObjects wait_objects
= {0};
2758 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2760 WaitObjects
*w
= &wait_objects
;
2762 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2764 w
->events
[w
->num
] = handle
;
2765 w
->func
[w
->num
] = func
;
2766 w
->opaque
[w
->num
] = opaque
;
2771 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2774 WaitObjects
*w
= &wait_objects
;
2777 for (i
= 0; i
< w
->num
; i
++) {
2778 if (w
->events
[i
] == handle
)
2781 w
->events
[i
] = w
->events
[i
+ 1];
2782 w
->func
[i
] = w
->func
[i
+ 1];
2783 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2791 /***********************************************************/
2792 /* ram save/restore */
2794 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2795 #define RAM_SAVE_FLAG_COMPRESS 0x02
2796 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2797 #define RAM_SAVE_FLAG_PAGE 0x08
2798 #define RAM_SAVE_FLAG_EOS 0x10
2800 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2802 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2803 uint32_t *array
= (uint32_t *)page
;
2806 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2807 if (array
[i
] != val
)
2814 static int ram_save_block(QEMUFile
*f
)
2816 static ram_addr_t current_addr
= 0;
2817 ram_addr_t saved_addr
= current_addr
;
2818 ram_addr_t addr
= 0;
2821 while (addr
< last_ram_offset
) {
2822 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2825 cpu_physical_memory_reset_dirty(current_addr
,
2826 current_addr
+ TARGET_PAGE_SIZE
,
2827 MIGRATION_DIRTY_FLAG
);
2829 p
= qemu_get_ram_ptr(current_addr
);
2831 if (is_dup_page(p
, *p
)) {
2832 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2833 qemu_put_byte(f
, *p
);
2835 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2836 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2842 addr
+= TARGET_PAGE_SIZE
;
2843 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2849 static uint64_t bytes_transferred
= 0;
2851 static ram_addr_t
ram_save_remaining(void)
2854 ram_addr_t count
= 0;
2856 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2857 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2864 uint64_t ram_bytes_remaining(void)
2866 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2869 uint64_t ram_bytes_transferred(void)
2871 return bytes_transferred
;
2874 uint64_t ram_bytes_total(void)
2876 return last_ram_offset
;
2879 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2882 uint64_t bytes_transferred_last
;
2884 uint64_t expected_time
= 0;
2886 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2887 qemu_file_set_error(f
);
2892 /* Make sure all dirty bits are set */
2893 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2894 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2895 cpu_physical_memory_set_dirty(addr
);
2898 /* Enable dirty memory tracking */
2899 cpu_physical_memory_set_dirty_tracking(1);
2901 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2904 bytes_transferred_last
= bytes_transferred
;
2905 bwidth
= get_clock();
2907 while (!qemu_file_rate_limit(f
)) {
2910 ret
= ram_save_block(f
);
2911 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2912 if (ret
== 0) /* no more blocks */
2916 bwidth
= get_clock() - bwidth
;
2917 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2919 /* if we haven't transferred anything this round, force expected_time to a
2920 * a very high value, but without crashing */
2924 /* try transferring iterative blocks of memory */
2928 /* flush all remaining blocks regardless of rate limiting */
2929 while (ram_save_block(f
) != 0) {
2930 bytes_transferred
+= TARGET_PAGE_SIZE
;
2932 cpu_physical_memory_set_dirty_tracking(0);
2935 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2937 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2939 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2942 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2947 if (version_id
!= 3)
2951 addr
= qemu_get_be64(f
);
2953 flags
= addr
& ~TARGET_PAGE_MASK
;
2954 addr
&= TARGET_PAGE_MASK
;
2956 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2957 if (addr
!= last_ram_offset
)
2961 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2962 uint8_t ch
= qemu_get_byte(f
);
2963 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2966 (!kvm_enabled() || kvm_has_sync_mmu())) {
2967 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2970 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
2971 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2972 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2977 void qemu_service_io(void)
2979 qemu_notify_event();
2982 /***********************************************************/
2983 /* bottom halves (can be seen as timers which expire ASAP) */
2994 static QEMUBH
*first_bh
= NULL
;
2996 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
2999 bh
= qemu_mallocz(sizeof(QEMUBH
));
3001 bh
->opaque
= opaque
;
3002 bh
->next
= first_bh
;
3007 int qemu_bh_poll(void)
3013 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3014 if (!bh
->deleted
&& bh
->scheduled
) {
3023 /* remove deleted bhs */
3037 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3045 void qemu_bh_schedule(QEMUBH
*bh
)
3051 /* stop the currently executing CPU to execute the BH ASAP */
3052 qemu_notify_event();
3055 void qemu_bh_cancel(QEMUBH
*bh
)
3060 void qemu_bh_delete(QEMUBH
*bh
)
3066 static void qemu_bh_update_timeout(int *timeout
)
3070 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3071 if (!bh
->deleted
&& bh
->scheduled
) {
3073 /* idle bottom halves will be polled at least
3075 *timeout
= MIN(10, *timeout
);
3077 /* non-idle bottom halves will be executed
3086 /***********************************************************/
3087 /* machine registration */
3089 static QEMUMachine
*first_machine
= NULL
;
3090 QEMUMachine
*current_machine
= NULL
;
3092 int qemu_register_machine(QEMUMachine
*m
)
3095 pm
= &first_machine
;
3103 static QEMUMachine
*find_machine(const char *name
)
3107 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3108 if (!strcmp(m
->name
, name
))
3110 if (m
->alias
&& !strcmp(m
->alias
, name
))
3116 static QEMUMachine
*find_default_machine(void)
3120 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3121 if (m
->is_default
) {
3128 /***********************************************************/
3129 /* main execution loop */
3131 static void gui_update(void *opaque
)
3133 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3134 DisplayState
*ds
= opaque
;
3135 DisplayChangeListener
*dcl
= ds
->listeners
;
3139 while (dcl
!= NULL
) {
3140 if (dcl
->gui_timer_interval
&&
3141 dcl
->gui_timer_interval
< interval
)
3142 interval
= dcl
->gui_timer_interval
;
3145 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3148 static void nographic_update(void *opaque
)
3150 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3152 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3155 struct vm_change_state_entry
{
3156 VMChangeStateHandler
*cb
;
3158 LIST_ENTRY (vm_change_state_entry
) entries
;
3161 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3163 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3166 VMChangeStateEntry
*e
;
3168 e
= qemu_mallocz(sizeof (*e
));
3172 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3176 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3178 LIST_REMOVE (e
, entries
);
3182 static void vm_state_notify(int running
, int reason
)
3184 VMChangeStateEntry
*e
;
3186 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3187 e
->cb(e
->opaque
, running
, reason
);
3191 static void resume_all_vcpus(void);
3192 static void pause_all_vcpus(void);
3199 vm_state_notify(1, 0);
3200 qemu_rearm_alarm_timer(alarm_timer
);
3205 /* reset/shutdown handler */
3207 typedef struct QEMUResetEntry
{
3208 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3209 QEMUResetHandler
*func
;
3213 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3214 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3215 static int reset_requested
;
3216 static int shutdown_requested
;
3217 static int powerdown_requested
;
3218 static int debug_requested
;
3219 static int vmstop_requested
;
3221 int qemu_shutdown_requested(void)
3223 int r
= shutdown_requested
;
3224 shutdown_requested
= 0;
3228 int qemu_reset_requested(void)
3230 int r
= reset_requested
;
3231 reset_requested
= 0;
3235 int qemu_powerdown_requested(void)
3237 int r
= powerdown_requested
;
3238 powerdown_requested
= 0;
3242 static int qemu_debug_requested(void)
3244 int r
= debug_requested
;
3245 debug_requested
= 0;
3249 static int qemu_vmstop_requested(void)
3251 int r
= vmstop_requested
;
3252 vmstop_requested
= 0;
3256 static void do_vm_stop(int reason
)
3259 cpu_disable_ticks();
3262 vm_state_notify(0, reason
);
3266 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3268 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3271 re
->opaque
= opaque
;
3272 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3275 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3279 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3280 if (re
->func
== func
&& re
->opaque
== opaque
) {
3281 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3288 void qemu_system_reset(void)
3290 QEMUResetEntry
*re
, *nre
;
3292 /* reset all devices */
3293 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3294 re
->func(re
->opaque
);
3298 void qemu_system_reset_request(void)
3301 shutdown_requested
= 1;
3303 reset_requested
= 1;
3305 qemu_notify_event();
3308 void qemu_system_shutdown_request(void)
3310 shutdown_requested
= 1;
3311 qemu_notify_event();
3314 void qemu_system_powerdown_request(void)
3316 powerdown_requested
= 1;
3317 qemu_notify_event();
3320 #ifdef CONFIG_IOTHREAD
3321 static void qemu_system_vmstop_request(int reason
)
3323 vmstop_requested
= reason
;
3324 qemu_notify_event();
3329 static int io_thread_fd
= -1;
3331 static void qemu_event_increment(void)
3333 static const char byte
= 0;
3335 if (io_thread_fd
== -1)
3338 write(io_thread_fd
, &byte
, sizeof(byte
));
3341 static void qemu_event_read(void *opaque
)
3343 int fd
= (unsigned long)opaque
;
3346 /* Drain the notify pipe */
3349 len
= read(fd
, buffer
, sizeof(buffer
));
3350 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3353 static int qemu_event_init(void)
3362 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3366 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3370 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3371 (void *)(unsigned long)fds
[0]);
3373 io_thread_fd
= fds
[1];
3382 HANDLE qemu_event_handle
;
3384 static void dummy_event_handler(void *opaque
)
3388 static int qemu_event_init(void)
3390 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3391 if (!qemu_event_handle
) {
3392 perror("Failed CreateEvent");
3395 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3399 static void qemu_event_increment(void)
3401 SetEvent(qemu_event_handle
);
3405 static int cpu_can_run(CPUState
*env
)
3414 #ifndef CONFIG_IOTHREAD
3415 static int qemu_init_main_loop(void)
3417 return qemu_event_init();
3420 void qemu_init_vcpu(void *_env
)
3422 CPUState
*env
= _env
;
3426 env
->nr_cores
= smp_cores
;
3427 env
->nr_threads
= smp_threads
;
3431 int qemu_cpu_self(void *env
)
3436 static void resume_all_vcpus(void)
3440 static void pause_all_vcpus(void)
3444 void qemu_cpu_kick(void *env
)
3449 void qemu_notify_event(void)
3451 CPUState
*env
= cpu_single_env
;
3458 #define qemu_mutex_lock_iothread() do { } while (0)
3459 #define qemu_mutex_unlock_iothread() do { } while (0)
3461 void vm_stop(int reason
)
3466 #else /* CONFIG_IOTHREAD */
3468 #include "qemu-thread.h"
3470 QemuMutex qemu_global_mutex
;
3471 static QemuMutex qemu_fair_mutex
;
3473 static QemuThread io_thread
;
3475 static QemuThread
*tcg_cpu_thread
;
3476 static QemuCond
*tcg_halt_cond
;
3478 static int qemu_system_ready
;
3480 static QemuCond qemu_cpu_cond
;
3482 static QemuCond qemu_system_cond
;
3483 static QemuCond qemu_pause_cond
;
3485 static void block_io_signals(void);
3486 static void unblock_io_signals(void);
3487 static int tcg_has_work(void);
3489 static int qemu_init_main_loop(void)
3493 ret
= qemu_event_init();
3497 qemu_cond_init(&qemu_pause_cond
);
3498 qemu_mutex_init(&qemu_fair_mutex
);
3499 qemu_mutex_init(&qemu_global_mutex
);
3500 qemu_mutex_lock(&qemu_global_mutex
);
3502 unblock_io_signals();
3503 qemu_thread_self(&io_thread
);
3508 static void qemu_wait_io_event(CPUState
*env
)
3510 while (!tcg_has_work())
3511 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3513 qemu_mutex_unlock(&qemu_global_mutex
);
3516 * Users of qemu_global_mutex can be starved, having no chance
3517 * to acquire it since this path will get to it first.
3518 * So use another lock to provide fairness.
3520 qemu_mutex_lock(&qemu_fair_mutex
);
3521 qemu_mutex_unlock(&qemu_fair_mutex
);
3523 qemu_mutex_lock(&qemu_global_mutex
);
3527 qemu_cond_signal(&qemu_pause_cond
);
3531 static int qemu_cpu_exec(CPUState
*env
);
3533 static void *kvm_cpu_thread_fn(void *arg
)
3535 CPUState
*env
= arg
;
3538 qemu_thread_self(env
->thread
);
3542 /* signal CPU creation */
3543 qemu_mutex_lock(&qemu_global_mutex
);
3545 qemu_cond_signal(&qemu_cpu_cond
);
3547 /* and wait for machine initialization */
3548 while (!qemu_system_ready
)
3549 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3552 if (cpu_can_run(env
))
3554 qemu_wait_io_event(env
);
3560 static void tcg_cpu_exec(void);
3562 static void *tcg_cpu_thread_fn(void *arg
)
3564 CPUState
*env
= arg
;
3567 qemu_thread_self(env
->thread
);
3569 /* signal CPU creation */
3570 qemu_mutex_lock(&qemu_global_mutex
);
3571 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3573 qemu_cond_signal(&qemu_cpu_cond
);
3575 /* and wait for machine initialization */
3576 while (!qemu_system_ready
)
3577 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3581 qemu_wait_io_event(cur_cpu
);
3587 void qemu_cpu_kick(void *_env
)
3589 CPUState
*env
= _env
;
3590 qemu_cond_broadcast(env
->halt_cond
);
3592 qemu_thread_signal(env
->thread
, SIGUSR1
);
3595 int qemu_cpu_self(void *env
)
3597 return (cpu_single_env
!= NULL
);
3600 static void cpu_signal(int sig
)
3603 cpu_exit(cpu_single_env
);
3606 static void block_io_signals(void)
3609 struct sigaction sigact
;
3612 sigaddset(&set
, SIGUSR2
);
3613 sigaddset(&set
, SIGIO
);
3614 sigaddset(&set
, SIGALRM
);
3615 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3618 sigaddset(&set
, SIGUSR1
);
3619 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3621 memset(&sigact
, 0, sizeof(sigact
));
3622 sigact
.sa_handler
= cpu_signal
;
3623 sigaction(SIGUSR1
, &sigact
, NULL
);
3626 static void unblock_io_signals(void)
3631 sigaddset(&set
, SIGUSR2
);
3632 sigaddset(&set
, SIGIO
);
3633 sigaddset(&set
, SIGALRM
);
3634 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3637 sigaddset(&set
, SIGUSR1
);
3638 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3641 static void qemu_signal_lock(unsigned int msecs
)
3643 qemu_mutex_lock(&qemu_fair_mutex
);
3645 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3646 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3647 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3650 qemu_mutex_unlock(&qemu_fair_mutex
);
3653 static void qemu_mutex_lock_iothread(void)
3655 if (kvm_enabled()) {
3656 qemu_mutex_lock(&qemu_fair_mutex
);
3657 qemu_mutex_lock(&qemu_global_mutex
);
3658 qemu_mutex_unlock(&qemu_fair_mutex
);
3660 qemu_signal_lock(100);
3663 static void qemu_mutex_unlock_iothread(void)
3665 qemu_mutex_unlock(&qemu_global_mutex
);
3668 static int all_vcpus_paused(void)
3670 CPUState
*penv
= first_cpu
;
3675 penv
= (CPUState
*)penv
->next_cpu
;
3681 static void pause_all_vcpus(void)
3683 CPUState
*penv
= first_cpu
;
3687 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3688 qemu_cpu_kick(penv
);
3689 penv
= (CPUState
*)penv
->next_cpu
;
3692 while (!all_vcpus_paused()) {
3693 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3696 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3697 penv
= (CPUState
*)penv
->next_cpu
;
3702 static void resume_all_vcpus(void)
3704 CPUState
*penv
= first_cpu
;
3709 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3710 qemu_cpu_kick(penv
);
3711 penv
= (CPUState
*)penv
->next_cpu
;
3715 static void tcg_init_vcpu(void *_env
)
3717 CPUState
*env
= _env
;
3718 /* share a single thread for all cpus with TCG */
3719 if (!tcg_cpu_thread
) {
3720 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3721 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3722 qemu_cond_init(env
->halt_cond
);
3723 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3724 while (env
->created
== 0)
3725 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3726 tcg_cpu_thread
= env
->thread
;
3727 tcg_halt_cond
= env
->halt_cond
;
3729 env
->thread
= tcg_cpu_thread
;
3730 env
->halt_cond
= tcg_halt_cond
;
3734 static void kvm_start_vcpu(CPUState
*env
)
3736 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3737 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3738 qemu_cond_init(env
->halt_cond
);
3739 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3740 while (env
->created
== 0)
3741 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3744 void qemu_init_vcpu(void *_env
)
3746 CPUState
*env
= _env
;
3749 kvm_start_vcpu(env
);
3752 env
->nr_cores
= smp_cores
;
3753 env
->nr_threads
= smp_threads
;
3756 void qemu_notify_event(void)
3758 qemu_event_increment();
3761 void vm_stop(int reason
)
3764 qemu_thread_self(&me
);
3766 if (!qemu_thread_equal(&me
, &io_thread
)) {
3767 qemu_system_vmstop_request(reason
);
3769 * FIXME: should not return to device code in case
3770 * vm_stop() has been requested.
3772 if (cpu_single_env
) {
3773 cpu_exit(cpu_single_env
);
3774 cpu_single_env
->stop
= 1;
3785 static void host_main_loop_wait(int *timeout
)
3791 /* XXX: need to suppress polling by better using win32 events */
3793 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3794 ret
|= pe
->func(pe
->opaque
);
3798 WaitObjects
*w
= &wait_objects
;
3800 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3801 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3802 if (w
->func
[ret
- WAIT_OBJECT_0
])
3803 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3805 /* Check for additional signaled events */
3806 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3808 /* Check if event is signaled */
3809 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3810 if(ret2
== WAIT_OBJECT_0
) {
3812 w
->func
[i
](w
->opaque
[i
]);
3813 } else if (ret2
== WAIT_TIMEOUT
) {
3815 err
= GetLastError();
3816 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3819 } else if (ret
== WAIT_TIMEOUT
) {
3821 err
= GetLastError();
3822 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3829 static void host_main_loop_wait(int *timeout
)
3834 void main_loop_wait(int timeout
)
3836 IOHandlerRecord
*ioh
;
3837 fd_set rfds
, wfds
, xfds
;
3841 qemu_bh_update_timeout(&timeout
);
3843 host_main_loop_wait(&timeout
);
3845 /* poll any events */
3846 /* XXX: separate device handlers from system ones */
3851 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3855 (!ioh
->fd_read_poll
||
3856 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3857 FD_SET(ioh
->fd
, &rfds
);
3861 if (ioh
->fd_write
) {
3862 FD_SET(ioh
->fd
, &wfds
);
3868 tv
.tv_sec
= timeout
/ 1000;
3869 tv
.tv_usec
= (timeout
% 1000) * 1000;
3871 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3873 qemu_mutex_unlock_iothread();
3874 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3875 qemu_mutex_lock_iothread();
3877 IOHandlerRecord
**pioh
;
3879 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3880 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3881 ioh
->fd_read(ioh
->opaque
);
3883 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3884 ioh
->fd_write(ioh
->opaque
);
3888 /* remove deleted IO handlers */
3889 pioh
= &first_io_handler
;
3900 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3902 /* rearm timer, if not periodic */
3903 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3904 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3905 qemu_rearm_alarm_timer(alarm_timer
);
3908 /* vm time timers */
3910 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3911 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3912 qemu_get_clock(vm_clock
));
3915 /* real time timers */
3916 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3917 qemu_get_clock(rt_clock
));
3919 /* Check bottom-halves last in case any of the earlier events triggered
3925 static int qemu_cpu_exec(CPUState
*env
)
3928 #ifdef CONFIG_PROFILER
3932 #ifdef CONFIG_PROFILER
3933 ti
= profile_getclock();
3938 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3939 env
->icount_decr
.u16
.low
= 0;
3940 env
->icount_extra
= 0;
3941 count
= qemu_next_deadline();
3942 count
= (count
+ (1 << icount_time_shift
) - 1)
3943 >> icount_time_shift
;
3944 qemu_icount
+= count
;
3945 decr
= (count
> 0xffff) ? 0xffff : count
;
3947 env
->icount_decr
.u16
.low
= decr
;
3948 env
->icount_extra
= count
;
3950 ret
= cpu_exec(env
);
3951 #ifdef CONFIG_PROFILER
3952 qemu_time
+= profile_getclock() - ti
;
3955 /* Fold pending instructions back into the
3956 instruction counter, and clear the interrupt flag. */
3957 qemu_icount
-= (env
->icount_decr
.u16
.low
3958 + env
->icount_extra
);
3959 env
->icount_decr
.u32
= 0;
3960 env
->icount_extra
= 0;
3965 static void tcg_cpu_exec(void)
3969 if (next_cpu
== NULL
)
3970 next_cpu
= first_cpu
;
3971 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3972 CPUState
*env
= cur_cpu
= next_cpu
;
3976 if (timer_alarm_pending
) {
3977 timer_alarm_pending
= 0;
3980 if (cpu_can_run(env
))
3981 ret
= qemu_cpu_exec(env
);
3982 if (ret
== EXCP_DEBUG
) {
3983 gdb_set_stop_cpu(env
);
3984 debug_requested
= 1;
3990 static int cpu_has_work(CPUState
*env
)
3998 if (qemu_cpu_has_work(env
))
4003 static int tcg_has_work(void)
4007 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4008 if (cpu_has_work(env
))
4013 static int qemu_calculate_timeout(void)
4015 #ifndef CONFIG_IOTHREAD
4020 else if (tcg_has_work())
4022 else if (!use_icount
)
4025 /* XXX: use timeout computed from timers */
4028 /* Advance virtual time to the next event. */
4029 if (use_icount
== 1) {
4030 /* When not using an adaptive execution frequency
4031 we tend to get badly out of sync with real time,
4032 so just delay for a reasonable amount of time. */
4035 delta
= cpu_get_icount() - cpu_get_clock();
4038 /* If virtual time is ahead of real time then just
4040 timeout
= (delta
/ 1000000) + 1;
4042 /* Wait for either IO to occur or the next
4044 add
= qemu_next_deadline();
4045 /* We advance the timer before checking for IO.
4046 Limit the amount we advance so that early IO
4047 activity won't get the guest too far ahead. */
4051 add
= (add
+ (1 << icount_time_shift
) - 1)
4052 >> icount_time_shift
;
4054 timeout
= delta
/ 1000000;
4061 #else /* CONFIG_IOTHREAD */
4066 static int vm_can_run(void)
4068 if (powerdown_requested
)
4070 if (reset_requested
)
4072 if (shutdown_requested
)
4074 if (debug_requested
)
4079 qemu_irq qemu_system_powerdown
;
4081 static void main_loop(void)
4085 #ifdef CONFIG_IOTHREAD
4086 qemu_system_ready
= 1;
4087 qemu_cond_broadcast(&qemu_system_cond
);
4092 #ifdef CONFIG_PROFILER
4095 #ifndef CONFIG_IOTHREAD
4098 #ifdef CONFIG_PROFILER
4099 ti
= profile_getclock();
4101 main_loop_wait(qemu_calculate_timeout());
4102 #ifdef CONFIG_PROFILER
4103 dev_time
+= profile_getclock() - ti
;
4105 } while (vm_can_run());
4107 if (qemu_debug_requested())
4108 vm_stop(EXCP_DEBUG
);
4109 if (qemu_shutdown_requested()) {
4116 if (qemu_reset_requested()) {
4118 qemu_system_reset();
4121 if (qemu_powerdown_requested()) {
4122 qemu_irq_raise(qemu_system_powerdown
);
4124 if ((r
= qemu_vmstop_requested()))
4130 static void version(void)
4132 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4135 static void help(int exitcode
)
4138 printf("usage: %s [options] [disk_image]\n"
4140 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4142 #define DEF(option, opt_arg, opt_enum, opt_help) \
4144 #define DEFHEADING(text) stringify(text) "\n"
4145 #include "qemu-options.h"
4150 "During emulation, the following keys are useful:\n"
4151 "ctrl-alt-f toggle full screen\n"
4152 "ctrl-alt-n switch to virtual console 'n'\n"
4153 "ctrl-alt toggle mouse and keyboard grab\n"
4155 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4160 DEFAULT_NETWORK_SCRIPT
,
4161 DEFAULT_NETWORK_DOWN_SCRIPT
,
4163 DEFAULT_GDBSTUB_PORT
,
4168 #define HAS_ARG 0x0001
4171 #define DEF(option, opt_arg, opt_enum, opt_help) \
4173 #define DEFHEADING(text)
4174 #include "qemu-options.h"
4180 typedef struct QEMUOption
{
4186 static const QEMUOption qemu_options
[] = {
4187 { "h", 0, QEMU_OPTION_h
},
4188 #define DEF(option, opt_arg, opt_enum, opt_help) \
4189 { option, opt_arg, opt_enum },
4190 #define DEFHEADING(text)
4191 #include "qemu-options.h"
4199 struct soundhw soundhw
[] = {
4200 #ifdef HAS_AUDIO_CHOICE
4201 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4207 { .init_isa
= pcspk_audio_init
}
4214 "Creative Sound Blaster 16",
4217 { .init_isa
= SB16_init
}
4221 #ifdef CONFIG_CS4231A
4227 { .init_isa
= cs4231a_init
}
4235 "Yamaha YMF262 (OPL3)",
4237 "Yamaha YM3812 (OPL2)",
4241 { .init_isa
= Adlib_init
}
4248 "Gravis Ultrasound GF1",
4251 { .init_isa
= GUS_init
}
4258 "Intel 82801AA AC97 Audio",
4261 { .init_pci
= ac97_init
}
4265 #ifdef CONFIG_ES1370
4268 "ENSONIQ AudioPCI ES1370",
4271 { .init_pci
= es1370_init
}
4275 #endif /* HAS_AUDIO_CHOICE */
4277 { NULL
, NULL
, 0, 0, { NULL
} }
4280 static void select_soundhw (const char *optarg
)
4284 if (*optarg
== '?') {
4287 printf ("Valid sound card names (comma separated):\n");
4288 for (c
= soundhw
; c
->name
; ++c
) {
4289 printf ("%-11s %s\n", c
->name
, c
->descr
);
4291 printf ("\n-soundhw all will enable all of the above\n");
4292 exit (*optarg
!= '?');
4300 if (!strcmp (optarg
, "all")) {
4301 for (c
= soundhw
; c
->name
; ++c
) {
4309 e
= strchr (p
, ',');
4310 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4312 for (c
= soundhw
; c
->name
; ++c
) {
4313 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4322 "Unknown sound card name (too big to show)\n");
4325 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4330 p
+= l
+ (e
!= NULL
);
4334 goto show_valid_cards
;
4339 static void select_vgahw (const char *p
)
4343 vga_interface_type
= VGA_NONE
;
4344 if (strstart(p
, "std", &opts
)) {
4345 vga_interface_type
= VGA_STD
;
4346 } else if (strstart(p
, "cirrus", &opts
)) {
4347 vga_interface_type
= VGA_CIRRUS
;
4348 } else if (strstart(p
, "vmware", &opts
)) {
4349 vga_interface_type
= VGA_VMWARE
;
4350 } else if (strstart(p
, "xenfb", &opts
)) {
4351 vga_interface_type
= VGA_XENFB
;
4352 } else if (!strstart(p
, "none", &opts
)) {
4354 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4358 const char *nextopt
;
4360 if (strstart(opts
, ",retrace=", &nextopt
)) {
4362 if (strstart(opts
, "dumb", &nextopt
))
4363 vga_retrace_method
= VGA_RETRACE_DUMB
;
4364 else if (strstart(opts
, "precise", &nextopt
))
4365 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4366 else goto invalid_vga
;
4367 } else goto invalid_vga
;
4373 static int balloon_parse(const char *arg
)
4377 if (strcmp(arg
, "none") == 0) {
4381 if (!strncmp(arg
, "virtio", 6)) {
4382 if (arg
[6] == ',') {
4383 /* have params -> parse them */
4384 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4388 /* create empty opts */
4389 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4391 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4400 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4402 exit(STATUS_CONTROL_C_EXIT
);
4407 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4411 if(strlen(str
) != 36)
4414 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4415 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4416 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4422 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4428 #define MAX_NET_CLIENTS 32
4432 static void termsig_handler(int signal
)
4434 qemu_system_shutdown_request();
4437 static void sigchld_handler(int signal
)
4439 waitpid(-1, NULL
, WNOHANG
);
4442 static void sighandler_setup(void)
4444 struct sigaction act
;
4446 memset(&act
, 0, sizeof(act
));
4447 act
.sa_handler
= termsig_handler
;
4448 sigaction(SIGINT
, &act
, NULL
);
4449 sigaction(SIGHUP
, &act
, NULL
);
4450 sigaction(SIGTERM
, &act
, NULL
);
4452 act
.sa_handler
= sigchld_handler
;
4453 act
.sa_flags
= SA_NOCLDSTOP
;
4454 sigaction(SIGCHLD
, &act
, NULL
);
4460 /* Look for support files in the same directory as the executable. */
4461 static char *find_datadir(const char *argv0
)
4467 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4474 while (p
!= buf
&& *p
!= '\\')
4477 if (access(buf
, R_OK
) == 0) {
4478 return qemu_strdup(buf
);
4484 /* Find a likely location for support files using the location of the binary.
4485 For installed binaries this will be "$bindir/../share/qemu". When
4486 running from the build tree this will be "$bindir/../pc-bios". */
4487 #define SHARE_SUFFIX "/share/qemu"
4488 #define BUILD_SUFFIX "/pc-bios"
4489 static char *find_datadir(const char *argv0
)
4497 #if defined(__linux__)
4500 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4506 #elif defined(__FreeBSD__)
4509 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4516 /* If we don't have any way of figuring out the actual executable
4517 location then try argv[0]. */
4519 p
= realpath(argv0
, buf
);
4527 max_len
= strlen(dir
) +
4528 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4529 res
= qemu_mallocz(max_len
);
4530 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4531 if (access(res
, R_OK
)) {
4532 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4533 if (access(res
, R_OK
)) {
4545 char *qemu_find_file(int type
, const char *name
)
4551 /* If name contains path separators then try it as a straight path. */
4552 if ((strchr(name
, '/') || strchr(name
, '\\'))
4553 && access(name
, R_OK
) == 0) {
4554 return qemu_strdup(name
);
4557 case QEMU_FILE_TYPE_BIOS
:
4560 case QEMU_FILE_TYPE_KEYMAP
:
4561 subdir
= "keymaps/";
4566 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4567 buf
= qemu_mallocz(len
);
4568 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4569 if (access(buf
, R_OK
)) {
4576 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4580 dev
= qdev_device_add(opts
);
4586 struct device_config
{
4588 DEV_USB
, /* -usbdevice */
4591 const char *cmdline
;
4592 TAILQ_ENTRY(device_config
) next
;
4594 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4596 static void add_device_config(int type
, const char *cmdline
)
4598 struct device_config
*conf
;
4600 conf
= qemu_mallocz(sizeof(*conf
));
4602 conf
->cmdline
= cmdline
;
4603 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4606 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4608 struct device_config
*conf
;
4611 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4612 if (conf
->type
!= type
)
4614 rc
= func(conf
->cmdline
);
4621 int main(int argc
, char **argv
, char **envp
)
4623 const char *gdbstub_dev
= NULL
;
4624 uint32_t boot_devices_bitmap
= 0;
4626 int snapshot
, linux_boot
, net_boot
;
4627 const char *initrd_filename
;
4628 const char *kernel_filename
, *kernel_cmdline
;
4629 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4631 DisplayChangeListener
*dcl
;
4632 int cyls
, heads
, secs
, translation
;
4633 const char *net_clients
[MAX_NET_CLIENTS
];
4635 QemuOpts
*hda_opts
= NULL
, *opts
;
4637 const char *r
, *optarg
;
4638 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4639 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4640 int monitor_device_index
;
4641 const char *serial_devices
[MAX_SERIAL_PORTS
];
4642 int serial_device_index
;
4643 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4644 int parallel_device_index
;
4645 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4646 int virtio_console_index
;
4647 const char *loadvm
= NULL
;
4648 QEMUMachine
*machine
;
4649 const char *cpu_model
;
4654 const char *pid_file
= NULL
;
4655 const char *incoming
= NULL
;
4658 struct passwd
*pwd
= NULL
;
4659 const char *chroot_dir
= NULL
;
4660 const char *run_as
= NULL
;
4663 int show_vnc_port
= 0;
4665 qemu_errors_to_file(stderr
);
4666 qemu_cache_utils_init(envp
);
4668 LIST_INIT (&vm_change_state_head
);
4671 struct sigaction act
;
4672 sigfillset(&act
.sa_mask
);
4674 act
.sa_handler
= SIG_IGN
;
4675 sigaction(SIGPIPE
, &act
, NULL
);
4678 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4679 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4680 QEMU to run on a single CPU */
4685 h
= GetCurrentProcess();
4686 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4687 for(i
= 0; i
< 32; i
++) {
4688 if (mask
& (1 << i
))
4693 SetProcessAffinityMask(h
, mask
);
4699 module_call_init(MODULE_INIT_MACHINE
);
4700 machine
= find_default_machine();
4702 initrd_filename
= NULL
;
4705 kernel_filename
= NULL
;
4706 kernel_cmdline
= "";
4707 cyls
= heads
= secs
= 0;
4708 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4710 serial_devices
[0] = "vc:80Cx24C";
4711 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4712 serial_devices
[i
] = NULL
;
4713 serial_device_index
= 0;
4715 parallel_devices
[0] = "vc:80Cx24C";
4716 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4717 parallel_devices
[i
] = NULL
;
4718 parallel_device_index
= 0;
4720 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4721 virtio_consoles
[i
] = NULL
;
4722 virtio_console_index
= 0;
4724 monitor_devices
[0] = "vc:80Cx24C";
4725 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4726 monitor_devices
[i
] = NULL
;
4728 monitor_device_index
= 0;
4730 for (i
= 0; i
< MAX_NODES
; i
++) {
4732 node_cpumask
[i
] = 0;
4748 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4750 const QEMUOption
*popt
;
4753 /* Treat --foo the same as -foo. */
4756 popt
= qemu_options
;
4759 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4763 if (!strcmp(popt
->name
, r
+ 1))
4767 if (popt
->flags
& HAS_ARG
) {
4768 if (optind
>= argc
) {
4769 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4773 optarg
= argv
[optind
++];
4778 switch(popt
->index
) {
4780 machine
= find_machine(optarg
);
4783 printf("Supported machines are:\n");
4784 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4786 printf("%-10s %s (alias of %s)\n",
4787 m
->alias
, m
->desc
, m
->name
);
4788 printf("%-10s %s%s\n",
4790 m
->is_default
? " (default)" : "");
4792 exit(*optarg
!= '?');
4795 case QEMU_OPTION_cpu
:
4796 /* hw initialization will check this */
4797 if (*optarg
== '?') {
4798 /* XXX: implement xxx_cpu_list for targets that still miss it */
4799 #if defined(cpu_list)
4800 cpu_list(stdout
, &fprintf
);
4807 case QEMU_OPTION_initrd
:
4808 initrd_filename
= optarg
;
4810 case QEMU_OPTION_hda
:
4812 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4814 hda_opts
= drive_add(optarg
, HD_ALIAS
4815 ",cyls=%d,heads=%d,secs=%d%s",
4816 0, cyls
, heads
, secs
,
4817 translation
== BIOS_ATA_TRANSLATION_LBA
?
4819 translation
== BIOS_ATA_TRANSLATION_NONE
?
4820 ",trans=none" : "");
4822 case QEMU_OPTION_hdb
:
4823 case QEMU_OPTION_hdc
:
4824 case QEMU_OPTION_hdd
:
4825 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4827 case QEMU_OPTION_drive
:
4828 drive_add(NULL
, "%s", optarg
);
4830 case QEMU_OPTION_set
:
4831 if (qemu_set_option(optarg
) != 0)
4834 case QEMU_OPTION_mtdblock
:
4835 drive_add(optarg
, MTD_ALIAS
);
4837 case QEMU_OPTION_sd
:
4838 drive_add(optarg
, SD_ALIAS
);
4840 case QEMU_OPTION_pflash
:
4841 drive_add(optarg
, PFLASH_ALIAS
);
4843 case QEMU_OPTION_snapshot
:
4846 case QEMU_OPTION_hdachs
:
4850 cyls
= strtol(p
, (char **)&p
, 0);
4851 if (cyls
< 1 || cyls
> 16383)
4856 heads
= strtol(p
, (char **)&p
, 0);
4857 if (heads
< 1 || heads
> 16)
4862 secs
= strtol(p
, (char **)&p
, 0);
4863 if (secs
< 1 || secs
> 63)
4867 if (!strcmp(p
, "none"))
4868 translation
= BIOS_ATA_TRANSLATION_NONE
;
4869 else if (!strcmp(p
, "lba"))
4870 translation
= BIOS_ATA_TRANSLATION_LBA
;
4871 else if (!strcmp(p
, "auto"))
4872 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4875 } else if (*p
!= '\0') {
4877 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4880 if (hda_opts
!= NULL
) {
4882 snprintf(num
, sizeof(num
), "%d", cyls
);
4883 qemu_opt_set(hda_opts
, "cyls", num
);
4884 snprintf(num
, sizeof(num
), "%d", heads
);
4885 qemu_opt_set(hda_opts
, "heads", num
);
4886 snprintf(num
, sizeof(num
), "%d", secs
);
4887 qemu_opt_set(hda_opts
, "secs", num
);
4888 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4889 qemu_opt_set(hda_opts
, "trans", "lba");
4890 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4891 qemu_opt_set(hda_opts
, "trans", "none");
4895 case QEMU_OPTION_numa
:
4896 if (nb_numa_nodes
>= MAX_NODES
) {
4897 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4902 case QEMU_OPTION_nographic
:
4903 display_type
= DT_NOGRAPHIC
;
4905 #ifdef CONFIG_CURSES
4906 case QEMU_OPTION_curses
:
4907 display_type
= DT_CURSES
;
4910 case QEMU_OPTION_portrait
:
4913 case QEMU_OPTION_kernel
:
4914 kernel_filename
= optarg
;
4916 case QEMU_OPTION_append
:
4917 kernel_cmdline
= optarg
;
4919 case QEMU_OPTION_cdrom
:
4920 drive_add(optarg
, CDROM_ALIAS
);
4922 case QEMU_OPTION_boot
:
4924 static const char * const params
[] = {
4925 "order", "once", "menu", NULL
4927 char buf
[sizeof(boot_devices
)];
4928 char *standard_boot_devices
;
4931 if (!strchr(optarg
, '=')) {
4933 pstrcpy(buf
, sizeof(buf
), optarg
);
4934 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4936 "qemu: unknown boot parameter '%s' in '%s'\n",
4942 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4943 boot_devices_bitmap
= parse_bootdevices(buf
);
4944 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4947 if (get_param_value(buf
, sizeof(buf
),
4949 boot_devices_bitmap
|= parse_bootdevices(buf
);
4950 standard_boot_devices
= qemu_strdup(boot_devices
);
4951 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4952 qemu_register_reset(restore_boot_devices
,
4953 standard_boot_devices
);
4955 if (get_param_value(buf
, sizeof(buf
),
4957 if (!strcmp(buf
, "on")) {
4959 } else if (!strcmp(buf
, "off")) {
4963 "qemu: invalid option value '%s'\n",
4971 case QEMU_OPTION_fda
:
4972 case QEMU_OPTION_fdb
:
4973 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4976 case QEMU_OPTION_no_fd_bootchk
:
4980 case QEMU_OPTION_net
:
4981 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
4982 fprintf(stderr
, "qemu: too many network clients\n");
4985 net_clients
[nb_net_clients
] = optarg
;
4989 case QEMU_OPTION_tftp
:
4990 legacy_tftp_prefix
= optarg
;
4992 case QEMU_OPTION_bootp
:
4993 legacy_bootp_filename
= optarg
;
4996 case QEMU_OPTION_smb
:
4997 net_slirp_smb(optarg
);
5000 case QEMU_OPTION_redir
:
5001 net_slirp_redir(optarg
);
5004 case QEMU_OPTION_bt
:
5005 add_device_config(DEV_BT
, optarg
);
5008 case QEMU_OPTION_audio_help
:
5012 case QEMU_OPTION_soundhw
:
5013 select_soundhw (optarg
);
5019 case QEMU_OPTION_version
:
5023 case QEMU_OPTION_m
: {
5027 value
= strtoul(optarg
, &ptr
, 10);
5029 case 0: case 'M': case 'm':
5036 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5040 /* On 32-bit hosts, QEMU is limited by virtual address space */
5041 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5042 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5045 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5046 fprintf(stderr
, "qemu: ram size too large\n");
5055 const CPULogItem
*item
;
5057 mask
= cpu_str_to_log_mask(optarg
);
5059 printf("Log items (comma separated):\n");
5060 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5061 printf("%-10s %s\n", item
->name
, item
->help
);
5069 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5071 case QEMU_OPTION_gdb
:
5072 gdbstub_dev
= optarg
;
5077 case QEMU_OPTION_bios
:
5080 case QEMU_OPTION_singlestep
:
5088 keyboard_layout
= optarg
;
5091 case QEMU_OPTION_localtime
:
5094 case QEMU_OPTION_vga
:
5095 select_vgahw (optarg
);
5097 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5103 w
= strtol(p
, (char **)&p
, 10);
5106 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5112 h
= strtol(p
, (char **)&p
, 10);
5117 depth
= strtol(p
, (char **)&p
, 10);
5118 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5119 depth
!= 24 && depth
!= 32)
5121 } else if (*p
== '\0') {
5122 depth
= graphic_depth
;
5129 graphic_depth
= depth
;
5133 case QEMU_OPTION_echr
:
5136 term_escape_char
= strtol(optarg
, &r
, 0);
5138 printf("Bad argument to echr\n");
5141 case QEMU_OPTION_monitor
:
5142 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5143 fprintf(stderr
, "qemu: too many monitor devices\n");
5146 monitor_devices
[monitor_device_index
] = optarg
;
5147 monitor_device_index
++;
5149 case QEMU_OPTION_chardev
:
5150 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5152 fprintf(stderr
, "parse error: %s\n", optarg
);
5155 if (NULL
== qemu_chr_open_opts(opts
, NULL
)) {
5159 case QEMU_OPTION_serial
:
5160 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5161 fprintf(stderr
, "qemu: too many serial ports\n");
5164 serial_devices
[serial_device_index
] = optarg
;
5165 serial_device_index
++;
5167 case QEMU_OPTION_watchdog
:
5170 "qemu: only one watchdog option may be given\n");
5175 case QEMU_OPTION_watchdog_action
:
5176 if (select_watchdog_action(optarg
) == -1) {
5177 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5181 case QEMU_OPTION_virtiocon
:
5182 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5183 fprintf(stderr
, "qemu: too many virtio consoles\n");
5186 virtio_consoles
[virtio_console_index
] = optarg
;
5187 virtio_console_index
++;
5189 case QEMU_OPTION_parallel
:
5190 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5191 fprintf(stderr
, "qemu: too many parallel ports\n");
5194 parallel_devices
[parallel_device_index
] = optarg
;
5195 parallel_device_index
++;
5197 case QEMU_OPTION_loadvm
:
5200 case QEMU_OPTION_full_screen
:
5204 case QEMU_OPTION_no_frame
:
5207 case QEMU_OPTION_alt_grab
:
5210 case QEMU_OPTION_no_quit
:
5213 case QEMU_OPTION_sdl
:
5214 display_type
= DT_SDL
;
5217 case QEMU_OPTION_pidfile
:
5221 case QEMU_OPTION_win2k_hack
:
5222 win2k_install_hack
= 1;
5224 case QEMU_OPTION_rtc_td_hack
:
5227 case QEMU_OPTION_acpitable
:
5228 if(acpi_table_add(optarg
) < 0) {
5229 fprintf(stderr
, "Wrong acpi table provided\n");
5233 case QEMU_OPTION_smbios
:
5234 if(smbios_entry_add(optarg
) < 0) {
5235 fprintf(stderr
, "Wrong smbios provided\n");
5241 case QEMU_OPTION_enable_kvm
:
5245 case QEMU_OPTION_usb
:
5248 case QEMU_OPTION_usbdevice
:
5250 add_device_config(DEV_USB
, optarg
);
5252 case QEMU_OPTION_device
:
5253 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5255 fprintf(stderr
, "parse error: %s\n", optarg
);
5259 case QEMU_OPTION_smp
:
5262 fprintf(stderr
, "Invalid number of CPUs\n");
5265 if (max_cpus
< smp_cpus
) {
5266 fprintf(stderr
, "maxcpus must be equal to or greater than "
5270 if (max_cpus
> 255) {
5271 fprintf(stderr
, "Unsupported number of maxcpus\n");
5275 case QEMU_OPTION_vnc
:
5276 display_type
= DT_VNC
;
5277 vnc_display
= optarg
;
5280 case QEMU_OPTION_no_acpi
:
5283 case QEMU_OPTION_no_hpet
:
5286 case QEMU_OPTION_balloon
:
5287 if (balloon_parse(optarg
) < 0) {
5288 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5293 case QEMU_OPTION_no_reboot
:
5296 case QEMU_OPTION_no_shutdown
:
5299 case QEMU_OPTION_show_cursor
:
5302 case QEMU_OPTION_uuid
:
5303 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5304 fprintf(stderr
, "Fail to parse UUID string."
5305 " Wrong format.\n");
5310 case QEMU_OPTION_daemonize
:
5314 case QEMU_OPTION_option_rom
:
5315 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5316 fprintf(stderr
, "Too many option ROMs\n");
5319 option_rom
[nb_option_roms
] = optarg
;
5322 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5323 case QEMU_OPTION_semihosting
:
5324 semihosting_enabled
= 1;
5327 case QEMU_OPTION_name
:
5328 qemu_name
= qemu_strdup(optarg
);
5330 char *p
= strchr(qemu_name
, ',');
5333 if (strncmp(p
, "process=", 8)) {
5334 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5342 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5343 case QEMU_OPTION_prom_env
:
5344 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5345 fprintf(stderr
, "Too many prom variables\n");
5348 prom_envs
[nb_prom_envs
] = optarg
;
5353 case QEMU_OPTION_old_param
:
5357 case QEMU_OPTION_clock
:
5358 configure_alarms(optarg
);
5360 case QEMU_OPTION_startdate
:
5363 time_t rtc_start_date
;
5364 if (!strcmp(optarg
, "now")) {
5365 rtc_date_offset
= -1;
5367 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5375 } else if (sscanf(optarg
, "%d-%d-%d",
5378 &tm
.tm_mday
) == 3) {
5387 rtc_start_date
= mktimegm(&tm
);
5388 if (rtc_start_date
== -1) {
5390 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5391 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5394 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5398 case QEMU_OPTION_tb_size
:
5399 tb_size
= strtol(optarg
, NULL
, 0);
5403 case QEMU_OPTION_icount
:
5405 if (strcmp(optarg
, "auto") == 0) {
5406 icount_time_shift
= -1;
5408 icount_time_shift
= strtol(optarg
, NULL
, 0);
5411 case QEMU_OPTION_incoming
:
5415 case QEMU_OPTION_chroot
:
5416 chroot_dir
= optarg
;
5418 case QEMU_OPTION_runas
:
5423 case QEMU_OPTION_xen_domid
:
5424 xen_domid
= atoi(optarg
);
5426 case QEMU_OPTION_xen_create
:
5427 xen_mode
= XEN_CREATE
;
5429 case QEMU_OPTION_xen_attach
:
5430 xen_mode
= XEN_ATTACH
;
5437 if (kvm_enabled()) {
5440 ret
= kvm_init(smp_cpus
);
5442 fprintf(stderr
, "failed to initialize KVM\n");
5447 /* If no data_dir is specified then try to find it relative to the
5450 data_dir
= find_datadir(argv
[0]);
5452 /* If all else fails use the install patch specified when building. */
5454 data_dir
= CONFIG_QEMU_SHAREDIR
;
5458 * Default to max_cpus = smp_cpus, in case the user doesn't
5459 * specify a max_cpus value.
5462 max_cpus
= smp_cpus
;
5464 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5465 if (smp_cpus
> machine
->max_cpus
) {
5466 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5467 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5472 if (display_type
== DT_NOGRAPHIC
) {
5473 if (serial_device_index
== 0)
5474 serial_devices
[0] = "stdio";
5475 if (parallel_device_index
== 0)
5476 parallel_devices
[0] = "null";
5477 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5478 monitor_devices
[0] = "stdio";
5486 if (pipe(fds
) == -1)
5497 len
= read(fds
[0], &status
, 1);
5498 if (len
== -1 && (errno
== EINTR
))
5503 else if (status
== 1) {
5504 fprintf(stderr
, "Could not acquire pidfile\n");
5521 signal(SIGTSTP
, SIG_IGN
);
5522 signal(SIGTTOU
, SIG_IGN
);
5523 signal(SIGTTIN
, SIG_IGN
);
5526 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5529 write(fds
[1], &status
, 1);
5531 fprintf(stderr
, "Could not acquire pid file\n");
5536 if (qemu_init_main_loop()) {
5537 fprintf(stderr
, "qemu_init_main_loop failed\n");
5540 linux_boot
= (kernel_filename
!= NULL
);
5542 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5543 fprintf(stderr
, "-append only allowed with -kernel option\n");
5547 if (!linux_boot
&& initrd_filename
!= NULL
) {
5548 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5553 /* Win32 doesn't support line-buffering and requires size >= 2 */
5554 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5558 if (init_timer_alarm() < 0) {
5559 fprintf(stderr
, "could not initialize alarm timer\n");
5562 if (use_icount
&& icount_time_shift
< 0) {
5564 /* 125MIPS seems a reasonable initial guess at the guest speed.
5565 It will be corrected fairly quickly anyway. */
5566 icount_time_shift
= 3;
5567 init_icount_adjust();
5574 /* init network clients */
5575 if (nb_net_clients
== 0) {
5576 /* if no clients, we use a default config */
5577 net_clients
[nb_net_clients
++] = "nic";
5579 net_clients
[nb_net_clients
++] = "user";
5583 for(i
= 0;i
< nb_net_clients
; i
++) {
5584 if (net_client_parse(net_clients
[i
]) < 0)
5588 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5589 net_set_boot_mask(net_boot
);
5593 /* init the bluetooth world */
5594 if (foreach_device_config(DEV_BT
, bt_parse
))
5597 /* init the memory */
5599 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5601 /* init the dynamic translator */
5602 cpu_exec_init_all(tb_size
* 1024 * 1024);
5606 /* we always create the cdrom drive, even if no disk is there */
5607 drive_add(NULL
, CDROM_ALIAS
);
5609 /* we always create at least one floppy */
5610 drive_add(NULL
, FD_ALIAS
, 0);
5612 /* we always create one sd slot, even if no card is in it */
5613 drive_add(NULL
, SD_ALIAS
);
5615 /* open the virtual block devices */
5617 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5618 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5621 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5622 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5624 /* Maintain compatibility with multiple stdio monitors */
5625 if (!strcmp(monitor_devices
[0],"stdio")) {
5626 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5627 const char *devname
= serial_devices
[i
];
5628 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5629 monitor_devices
[0] = NULL
;
5631 } else if (devname
&& !strcmp(devname
,"stdio")) {
5632 monitor_devices
[0] = NULL
;
5633 serial_devices
[i
] = "mon:stdio";
5639 if (nb_numa_nodes
> 0) {
5642 if (nb_numa_nodes
> smp_cpus
) {
5643 nb_numa_nodes
= smp_cpus
;
5646 /* If no memory size if given for any node, assume the default case
5647 * and distribute the available memory equally across all nodes
5649 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5650 if (node_mem
[i
] != 0)
5653 if (i
== nb_numa_nodes
) {
5654 uint64_t usedmem
= 0;
5656 /* On Linux, the each node's border has to be 8MB aligned,
5657 * the final node gets the rest.
5659 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5660 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5661 usedmem
+= node_mem
[i
];
5663 node_mem
[i
] = ram_size
- usedmem
;
5666 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5667 if (node_cpumask
[i
] != 0)
5670 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5671 * must cope with this anyway, because there are BIOSes out there in
5672 * real machines which also use this scheme.
5674 if (i
== nb_numa_nodes
) {
5675 for (i
= 0; i
< smp_cpus
; i
++) {
5676 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5681 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5682 const char *devname
= monitor_devices
[i
];
5683 if (devname
&& strcmp(devname
, "none")) {
5686 snprintf(label
, sizeof(label
), "monitor");
5688 snprintf(label
, sizeof(label
), "monitor%d", i
);
5690 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5691 if (!monitor_hds
[i
]) {
5692 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5699 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5700 const char *devname
= serial_devices
[i
];
5701 if (devname
&& strcmp(devname
, "none")) {
5703 snprintf(label
, sizeof(label
), "serial%d", i
);
5704 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5705 if (!serial_hds
[i
]) {
5706 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5713 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5714 const char *devname
= parallel_devices
[i
];
5715 if (devname
&& strcmp(devname
, "none")) {
5717 snprintf(label
, sizeof(label
), "parallel%d", i
);
5718 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5719 if (!parallel_hds
[i
]) {
5720 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5727 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5728 const char *devname
= virtio_consoles
[i
];
5729 if (devname
&& strcmp(devname
, "none")) {
5731 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5732 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5733 if (!virtcon_hds
[i
]) {
5734 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5741 module_call_init(MODULE_INIT_DEVICE
);
5744 i
= select_watchdog(watchdog
);
5746 exit (i
== 1 ? 1 : 0);
5749 if (machine
->compat_props
) {
5750 qdev_prop_register_compat(machine
->compat_props
);
5752 machine
->init(ram_size
, boot_devices
,
5753 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5757 /* must be after terminal init, SDL library changes signal handlers */
5761 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5762 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5763 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5769 current_machine
= machine
;
5771 /* init USB devices */
5773 foreach_device_config(DEV_USB
, usb_parse
);
5776 /* init generic devices */
5777 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5781 dumb_display_init();
5782 /* just use the first displaystate for the moment */
5785 if (display_type
== DT_DEFAULT
) {
5786 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5787 display_type
= DT_SDL
;
5789 display_type
= DT_VNC
;
5790 vnc_display
= "localhost:0,to=99";
5796 switch (display_type
) {
5799 #if defined(CONFIG_CURSES)
5801 curses_display_init(ds
, full_screen
);
5804 #if defined(CONFIG_SDL)
5806 sdl_display_init(ds
, full_screen
, no_frame
);
5808 #elif defined(CONFIG_COCOA)
5810 cocoa_display_init(ds
, full_screen
);
5814 vnc_display_init(ds
);
5815 if (vnc_display_open(ds
, vnc_display
) < 0)
5818 if (show_vnc_port
) {
5819 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5827 dcl
= ds
->listeners
;
5828 while (dcl
!= NULL
) {
5829 if (dcl
->dpy_refresh
!= NULL
) {
5830 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5831 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5836 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5837 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5838 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5841 text_consoles_set_display(display_state
);
5842 qemu_chr_initial_reset();
5844 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5845 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5846 monitor_init(monitor_hds
[i
],
5847 MONITOR_USE_READLINE
|
5848 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5852 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5853 const char *devname
= serial_devices
[i
];
5854 if (devname
&& strcmp(devname
, "none")) {
5855 if (strstart(devname
, "vc", 0))
5856 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5860 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5861 const char *devname
= parallel_devices
[i
];
5862 if (devname
&& strcmp(devname
, "none")) {
5863 if (strstart(devname
, "vc", 0))
5864 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5868 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5869 const char *devname
= virtio_consoles
[i
];
5870 if (virtcon_hds
[i
] && devname
) {
5871 if (strstart(devname
, "vc", 0))
5872 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5876 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5877 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5883 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5889 qemu_start_incoming_migration(incoming
);
5890 } else if (autostart
) {
5900 len
= write(fds
[1], &status
, 1);
5901 if (len
== -1 && (errno
== EINTR
))
5908 TFR(fd
= open("/dev/null", O_RDWR
));
5914 pwd
= getpwnam(run_as
);
5916 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5922 if (chroot(chroot_dir
) < 0) {
5923 fprintf(stderr
, "chroot failed\n");
5930 if (setgid(pwd
->pw_gid
) < 0) {
5931 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5934 if (setuid(pwd
->pw_uid
) < 0) {
5935 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5938 if (setuid(0) != -1) {
5939 fprintf(stderr
, "Dropping privileges failed\n");