4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #include <arpa/inet.h>
50 #include <sys/select.h>
53 #if defined(__FreeBSD__) || defined(__DragonFly__)
58 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
59 #include <freebsd/stdlib.h>
64 #include <linux/rtc.h>
65 #include <sys/prctl.h>
67 /* For the benefit of older linux systems which don't supply it,
68 we use a local copy of hpet.h. */
69 /* #include <linux/hpet.h> */
72 #include <linux/ppdev.h>
73 #include <linux/parport.h>
77 #include <sys/ethernet.h>
78 #include <sys/sockio.h>
79 #include <netinet/arp.h>
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #include <netinet/ip.h>
83 #include <netinet/ip_icmp.h> // must come after ip.h
84 #include <netinet/udp.h>
85 #include <netinet/tcp.h>
89 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
90 discussion about Solaris header problems */
91 extern int madvise(caddr_t
, size_t, int);
96 #if defined(__OpenBSD__)
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
106 #include <mmsystem.h>
110 #if defined(__APPLE__) || defined(main)
112 int qemu_main(int argc
, char **argv
, char **envp
);
113 int main(int argc
, char **argv
)
115 return qemu_main(argc
, argv
, NULL
);
118 #define main qemu_main
120 #endif /* CONFIG_SDL */
124 #define main qemu_main
125 #endif /* CONFIG_COCOA */
128 #include "hw/boards.h"
130 #include "hw/pcmcia.h"
132 #include "hw/audiodev.h"
136 #include "hw/watchdog.h"
137 #include "hw/smbios.h"
140 #include "hw/loader.h"
147 #include "qemu-timer.h"
148 #include "qemu-char.h"
149 #include "cache-utils.h"
151 #include "block_int.h"
152 #include "block-migration.h"
154 #include "audio/audio.h"
155 #include "migration.h"
158 #include "qemu-option.h"
159 #include "qemu-config.h"
163 #include "exec-all.h"
165 #include "qemu_socket.h"
167 #include "slirp/libslirp.h"
169 #include "qemu-queue.h"
172 //#define DEBUG_SLIRP
174 #define DEFAULT_RAM_SIZE 128
176 /* Maximum number of monitor devices */
177 #define MAX_MONITOR_DEVICES 10
179 static const char *data_dir
;
180 const char *bios_name
= NULL
;
181 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
182 to store the VM snapshots */
183 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
184 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
185 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
186 static DisplayState
*display_state
;
187 DisplayType display_type
= DT_DEFAULT
;
188 const char* keyboard_layout
= NULL
;
191 NICInfo nd_table
[MAX_NICS
];
194 static int rtc_utc
= 1;
195 static int rtc_date_offset
= -1; /* -1 means no change */
196 QEMUClock
*rtc_clock
;
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
;
247 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
248 unsigned int nb_prom_envs
= 0;
249 const char *prom_envs
[MAX_PROM_ENVS
];
254 uint64_t node_mem
[MAX_NODES
];
255 uint64_t node_cpumask
[MAX_NODES
];
257 static CPUState
*cur_cpu
;
258 static CPUState
*next_cpu
;
259 static int timer_alarm_pending
= 1;
260 /* Conversion factor from emulated instructions to virtual clock ticks. */
261 static int icount_time_shift
;
262 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
263 #define MAX_ICOUNT_SHIFT 10
264 /* Compensate for varying guest execution speed. */
265 static int64_t qemu_icount_bias
;
266 static QEMUTimer
*icount_rt_timer
;
267 static QEMUTimer
*icount_vm_timer
;
268 static QEMUTimer
*nographic_timer
;
270 uint8_t qemu_uuid
[16];
272 static QEMUBootSetHandler
*boot_set_handler
;
273 static void *boot_set_opaque
;
275 /***********************************************************/
276 /* x86 ISA bus support */
278 target_phys_addr_t isa_mem_base
= 0;
281 /***********************************************************/
282 void hw_error(const char *fmt
, ...)
288 fprintf(stderr
, "qemu: hardware error: ");
289 vfprintf(stderr
, fmt
, ap
);
290 fprintf(stderr
, "\n");
291 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
292 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
294 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
296 cpu_dump_state(env
, stderr
, fprintf
, 0);
303 static void set_proc_name(const char *s
)
305 #if defined(__linux__) && defined(PR_SET_NAME)
309 name
[sizeof(name
) - 1] = 0;
310 strncpy(name
, s
, sizeof(name
));
311 /* Could rewrite argv[0] too, but that's a bit more complicated.
312 This simple way is enough for `top'. */
313 prctl(PR_SET_NAME
, name
);
320 static QEMUBalloonEvent
*qemu_balloon_event
;
321 void *qemu_balloon_event_opaque
;
323 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
325 qemu_balloon_event
= func
;
326 qemu_balloon_event_opaque
= opaque
;
329 void qemu_balloon(ram_addr_t target
)
331 if (qemu_balloon_event
)
332 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
335 ram_addr_t
qemu_balloon_status(void)
337 if (qemu_balloon_event
)
338 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
342 /***********************************************************/
345 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
346 static void *qemu_put_kbd_event_opaque
;
347 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
348 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
350 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
352 qemu_put_kbd_event_opaque
= opaque
;
353 qemu_put_kbd_event
= func
;
356 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
357 void *opaque
, int absolute
,
360 QEMUPutMouseEntry
*s
, *cursor
;
362 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
364 s
->qemu_put_mouse_event
= func
;
365 s
->qemu_put_mouse_event_opaque
= opaque
;
366 s
->qemu_put_mouse_event_absolute
= absolute
;
367 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
370 if (!qemu_put_mouse_event_head
) {
371 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
375 cursor
= qemu_put_mouse_event_head
;
376 while (cursor
->next
!= NULL
)
377 cursor
= cursor
->next
;
380 qemu_put_mouse_event_current
= s
;
385 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
387 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
389 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
392 cursor
= qemu_put_mouse_event_head
;
393 while (cursor
!= NULL
&& cursor
!= entry
) {
395 cursor
= cursor
->next
;
398 if (cursor
== NULL
) // does not exist or list empty
400 else if (prev
== NULL
) { // entry is head
401 qemu_put_mouse_event_head
= cursor
->next
;
402 if (qemu_put_mouse_event_current
== entry
)
403 qemu_put_mouse_event_current
= cursor
->next
;
404 qemu_free(entry
->qemu_put_mouse_event_name
);
409 prev
->next
= entry
->next
;
411 if (qemu_put_mouse_event_current
== entry
)
412 qemu_put_mouse_event_current
= prev
;
414 qemu_free(entry
->qemu_put_mouse_event_name
);
418 void kbd_put_keycode(int keycode
)
420 if (qemu_put_kbd_event
) {
421 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
425 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
427 QEMUPutMouseEvent
*mouse_event
;
428 void *mouse_event_opaque
;
431 if (!qemu_put_mouse_event_current
) {
436 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
438 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
441 if (graphic_rotate
) {
442 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
445 width
= graphic_width
- 1;
446 mouse_event(mouse_event_opaque
,
447 width
- dy
, dx
, dz
, buttons_state
);
449 mouse_event(mouse_event_opaque
,
450 dx
, dy
, dz
, buttons_state
);
454 int kbd_mouse_is_absolute(void)
456 if (!qemu_put_mouse_event_current
)
459 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
462 void do_info_mice(Monitor
*mon
)
464 QEMUPutMouseEntry
*cursor
;
467 if (!qemu_put_mouse_event_head
) {
468 monitor_printf(mon
, "No mouse devices connected\n");
472 monitor_printf(mon
, "Mouse devices available:\n");
473 cursor
= qemu_put_mouse_event_head
;
474 while (cursor
!= NULL
) {
475 monitor_printf(mon
, "%c Mouse #%d: %s\n",
476 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
477 index
, cursor
->qemu_put_mouse_event_name
);
479 cursor
= cursor
->next
;
483 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
485 QEMUPutMouseEntry
*cursor
;
487 int index
= qdict_get_int(qdict
, "index");
489 if (!qemu_put_mouse_event_head
) {
490 monitor_printf(mon
, "No mouse devices connected\n");
494 cursor
= qemu_put_mouse_event_head
;
495 while (cursor
!= NULL
&& index
!= i
) {
497 cursor
= cursor
->next
;
501 qemu_put_mouse_event_current
= cursor
;
503 monitor_printf(mon
, "Mouse at given index not found\n");
506 /* compute with 96 bit intermediate result: (a*b)/c */
507 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
512 #ifdef HOST_WORDS_BIGENDIAN
522 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
523 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
526 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
530 /***********************************************************/
531 /* real time host monotonic timer */
533 static int64_t get_clock_realtime(void)
537 gettimeofday(&tv
, NULL
);
538 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
543 static int64_t clock_freq
;
545 static void init_get_clock(void)
549 ret
= QueryPerformanceFrequency(&freq
);
551 fprintf(stderr
, "Could not calibrate ticks\n");
554 clock_freq
= freq
.QuadPart
;
557 static int64_t get_clock(void)
560 QueryPerformanceCounter(&ti
);
561 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
566 static int use_rt_clock
;
568 static void init_get_clock(void)
571 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
572 || defined(__DragonFly__)
575 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
582 static int64_t get_clock(void)
584 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
585 || defined(__DragonFly__)
588 clock_gettime(CLOCK_MONOTONIC
, &ts
);
589 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
593 /* XXX: using gettimeofday leads to problems if the date
594 changes, so it should be avoided. */
595 return get_clock_realtime();
600 /* Return the virtual CPU time, based on the instruction counter. */
601 static int64_t cpu_get_icount(void)
604 CPUState
*env
= cpu_single_env
;;
605 icount
= qemu_icount
;
608 fprintf(stderr
, "Bad clock read\n");
609 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
611 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
614 /***********************************************************/
615 /* guest cycle counter */
617 typedef struct TimersState
{
618 int64_t cpu_ticks_prev
;
619 int64_t cpu_ticks_offset
;
620 int64_t cpu_clock_offset
;
621 int32_t cpu_ticks_enabled
;
625 TimersState timers_state
;
627 /* return the host CPU cycle counter and handle stop/restart */
628 int64_t cpu_get_ticks(void)
631 return cpu_get_icount();
633 if (!timers_state
.cpu_ticks_enabled
) {
634 return timers_state
.cpu_ticks_offset
;
637 ticks
= cpu_get_real_ticks();
638 if (timers_state
.cpu_ticks_prev
> ticks
) {
639 /* Note: non increasing ticks may happen if the host uses
641 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
643 timers_state
.cpu_ticks_prev
= ticks
;
644 return ticks
+ timers_state
.cpu_ticks_offset
;
648 /* return the host CPU monotonic timer and handle stop/restart */
649 static int64_t cpu_get_clock(void)
652 if (!timers_state
.cpu_ticks_enabled
) {
653 return timers_state
.cpu_clock_offset
;
656 return ti
+ timers_state
.cpu_clock_offset
;
660 /* enable cpu_get_ticks() */
661 void cpu_enable_ticks(void)
663 if (!timers_state
.cpu_ticks_enabled
) {
664 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
665 timers_state
.cpu_clock_offset
-= get_clock();
666 timers_state
.cpu_ticks_enabled
= 1;
670 /* disable cpu_get_ticks() : the clock is stopped. You must not call
671 cpu_get_ticks() after that. */
672 void cpu_disable_ticks(void)
674 if (timers_state
.cpu_ticks_enabled
) {
675 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
676 timers_state
.cpu_clock_offset
= cpu_get_clock();
677 timers_state
.cpu_ticks_enabled
= 0;
681 /***********************************************************/
684 #define QEMU_CLOCK_REALTIME 0
685 #define QEMU_CLOCK_VIRTUAL 1
686 #define QEMU_CLOCK_HOST 2
690 /* XXX: add frequency */
698 struct QEMUTimer
*next
;
701 struct qemu_alarm_timer
{
705 int (*start
)(struct qemu_alarm_timer
*t
);
706 void (*stop
)(struct qemu_alarm_timer
*t
);
707 void (*rearm
)(struct qemu_alarm_timer
*t
);
711 #define ALARM_FLAG_DYNTICKS 0x1
712 #define ALARM_FLAG_EXPIRED 0x2
714 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
716 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
719 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
721 if (!alarm_has_dynticks(t
))
727 /* TODO: MIN_TIMER_REARM_US should be optimized */
728 #define MIN_TIMER_REARM_US 250
730 static struct qemu_alarm_timer
*alarm_timer
;
734 struct qemu_alarm_win32
{
737 } alarm_win32_data
= {0, -1};
739 static int win32_start_timer(struct qemu_alarm_timer
*t
);
740 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
741 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
745 static int unix_start_timer(struct qemu_alarm_timer
*t
);
746 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
750 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
751 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
752 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
754 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
755 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
757 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
758 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
760 #endif /* __linux__ */
764 /* Correlation between real and virtual time is always going to be
765 fairly approximate, so ignore small variation.
766 When the guest is idle real and virtual time will be aligned in
768 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
770 static void icount_adjust(void)
775 static int64_t last_delta
;
776 /* If the VM is not running, then do nothing. */
780 cur_time
= cpu_get_clock();
781 cur_icount
= qemu_get_clock(vm_clock
);
782 delta
= cur_icount
- cur_time
;
783 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
785 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
786 && icount_time_shift
> 0) {
787 /* The guest is getting too far ahead. Slow time down. */
791 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
792 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
793 /* The guest is getting too far behind. Speed time up. */
797 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
800 static void icount_adjust_rt(void * opaque
)
802 qemu_mod_timer(icount_rt_timer
,
803 qemu_get_clock(rt_clock
) + 1000);
807 static void icount_adjust_vm(void * opaque
)
809 qemu_mod_timer(icount_vm_timer
,
810 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
814 static void init_icount_adjust(void)
816 /* Have both realtime and virtual time triggers for speed adjustment.
817 The realtime trigger catches emulated time passing too slowly,
818 the virtual time trigger catches emulated time passing too fast.
819 Realtime triggers occur even when idle, so use them less frequently
821 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
822 qemu_mod_timer(icount_rt_timer
,
823 qemu_get_clock(rt_clock
) + 1000);
824 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
825 qemu_mod_timer(icount_vm_timer
,
826 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
829 static struct qemu_alarm_timer alarm_timers
[] = {
832 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
833 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
834 /* HPET - if available - is preferred */
835 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
836 /* ...otherwise try RTC */
837 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
839 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
841 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
842 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
843 {"win32", 0, win32_start_timer
,
844 win32_stop_timer
, NULL
, &alarm_win32_data
},
849 static void show_available_alarms(void)
853 printf("Available alarm timers, in order of precedence:\n");
854 for (i
= 0; alarm_timers
[i
].name
; i
++)
855 printf("%s\n", alarm_timers
[i
].name
);
858 static void configure_alarms(char const *opt
)
862 int count
= ARRAY_SIZE(alarm_timers
) - 1;
865 struct qemu_alarm_timer tmp
;
867 if (!strcmp(opt
, "?")) {
868 show_available_alarms();
872 arg
= qemu_strdup(opt
);
874 /* Reorder the array */
875 name
= strtok(arg
, ",");
877 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
878 if (!strcmp(alarm_timers
[i
].name
, name
))
883 fprintf(stderr
, "Unknown clock %s\n", name
);
892 tmp
= alarm_timers
[i
];
893 alarm_timers
[i
] = alarm_timers
[cur
];
894 alarm_timers
[cur
] = tmp
;
898 name
= strtok(NULL
, ",");
904 /* Disable remaining timers */
905 for (i
= cur
; i
< count
; i
++)
906 alarm_timers
[i
].name
= NULL
;
908 show_available_alarms();
913 #define QEMU_NUM_CLOCKS 3
917 QEMUClock
*host_clock
;
919 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
921 static QEMUClock
*qemu_new_clock(int type
)
924 clock
= qemu_mallocz(sizeof(QEMUClock
));
929 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
933 ts
= qemu_mallocz(sizeof(QEMUTimer
));
940 void qemu_free_timer(QEMUTimer
*ts
)
945 /* stop a timer, but do not dealloc it */
946 void qemu_del_timer(QEMUTimer
*ts
)
950 /* NOTE: this code must be signal safe because
951 qemu_timer_expired() can be called from a signal. */
952 pt
= &active_timers
[ts
->clock
->type
];
965 /* modify the current timer so that it will be fired when current_time
966 >= expire_time. The corresponding callback will be called. */
967 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
973 /* add the timer in the sorted list */
974 /* NOTE: this code must be signal safe because
975 qemu_timer_expired() can be called from a signal. */
976 pt
= &active_timers
[ts
->clock
->type
];
981 if (t
->expire_time
> expire_time
)
985 ts
->expire_time
= expire_time
;
989 /* Rearm if necessary */
990 if (pt
== &active_timers
[ts
->clock
->type
]) {
991 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
992 qemu_rearm_alarm_timer(alarm_timer
);
994 /* Interrupt execution to force deadline recalculation. */
1000 int qemu_timer_pending(QEMUTimer
*ts
)
1003 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1010 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1014 return (timer_head
->expire_time
<= current_time
);
1017 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1023 if (!ts
|| ts
->expire_time
> current_time
)
1025 /* remove timer from the list before calling the callback */
1026 *ptimer_head
= ts
->next
;
1029 /* run the callback (the timer list can be modified) */
1034 int64_t qemu_get_clock(QEMUClock
*clock
)
1036 switch(clock
->type
) {
1037 case QEMU_CLOCK_REALTIME
:
1038 return get_clock() / 1000000;
1040 case QEMU_CLOCK_VIRTUAL
:
1042 return cpu_get_icount();
1044 return cpu_get_clock();
1046 case QEMU_CLOCK_HOST
:
1047 return get_clock_realtime();
1051 static void init_clocks(void)
1054 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1055 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1056 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1058 rtc_clock
= host_clock
;
1062 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1064 uint64_t expire_time
;
1066 if (qemu_timer_pending(ts
)) {
1067 expire_time
= ts
->expire_time
;
1071 qemu_put_be64(f
, expire_time
);
1074 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1076 uint64_t expire_time
;
1078 expire_time
= qemu_get_be64(f
);
1079 if (expire_time
!= -1) {
1080 qemu_mod_timer(ts
, expire_time
);
1086 static const VMStateDescription vmstate_timers
= {
1089 .minimum_version_id
= 1,
1090 .minimum_version_id_old
= 1,
1091 .fields
= (VMStateField
[]) {
1092 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1093 VMSTATE_INT64(dummy
, TimersState
),
1094 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1095 VMSTATE_END_OF_LIST()
1099 static void qemu_event_increment(void);
1102 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1103 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1106 static void host_alarm_handler(int host_signum
)
1110 #define DISP_FREQ 1000
1112 static int64_t delta_min
= INT64_MAX
;
1113 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1115 ti
= qemu_get_clock(vm_clock
);
1116 if (last_clock
!= 0) {
1117 delta
= ti
- last_clock
;
1118 if (delta
< delta_min
)
1120 if (delta
> delta_max
)
1123 if (++count
== DISP_FREQ
) {
1124 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1125 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1126 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1127 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1128 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1130 delta_min
= INT64_MAX
;
1138 if (alarm_has_dynticks(alarm_timer
) ||
1140 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1141 qemu_get_clock(vm_clock
))) ||
1142 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1143 qemu_get_clock(rt_clock
)) ||
1144 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1145 qemu_get_clock(host_clock
))) {
1146 qemu_event_increment();
1147 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1149 #ifndef CONFIG_IOTHREAD
1151 /* stop the currently executing cpu because a timer occured */
1155 timer_alarm_pending
= 1;
1156 qemu_notify_event();
1160 static int64_t qemu_next_deadline(void)
1162 /* To avoid problems with overflow limit this to 2^32. */
1163 int64_t delta
= INT32_MAX
;
1165 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1166 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1167 qemu_get_clock(vm_clock
);
1169 if (active_timers
[QEMU_CLOCK_HOST
]) {
1170 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1171 qemu_get_clock(host_clock
);
1182 #if defined(__linux__)
1183 static uint64_t qemu_next_deadline_dyntick(void)
1191 delta
= (qemu_next_deadline() + 999) / 1000;
1193 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1194 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1195 qemu_get_clock(rt_clock
))*1000;
1196 if (rtdelta
< delta
)
1200 if (delta
< MIN_TIMER_REARM_US
)
1201 delta
= MIN_TIMER_REARM_US
;
1209 /* Sets a specific flag */
1210 static int fcntl_setfl(int fd
, int flag
)
1214 flags
= fcntl(fd
, F_GETFL
);
1218 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1224 #if defined(__linux__)
1226 #define RTC_FREQ 1024
1228 static void enable_sigio_timer(int fd
)
1230 struct sigaction act
;
1233 sigfillset(&act
.sa_mask
);
1235 act
.sa_handler
= host_alarm_handler
;
1237 sigaction(SIGIO
, &act
, NULL
);
1238 fcntl_setfl(fd
, O_ASYNC
);
1239 fcntl(fd
, F_SETOWN
, getpid());
1242 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1244 struct hpet_info info
;
1247 fd
= open("/dev/hpet", O_RDONLY
);
1252 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1254 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1255 "error, but for better emulation accuracy type:\n"
1256 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1260 /* Check capabilities */
1261 r
= ioctl(fd
, HPET_INFO
, &info
);
1265 /* Enable periodic mode */
1266 r
= ioctl(fd
, HPET_EPI
, 0);
1267 if (info
.hi_flags
&& (r
< 0))
1270 /* Enable interrupt */
1271 r
= ioctl(fd
, HPET_IE_ON
, 0);
1275 enable_sigio_timer(fd
);
1276 t
->priv
= (void *)(long)fd
;
1284 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1286 int fd
= (long)t
->priv
;
1291 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1294 unsigned long current_rtc_freq
= 0;
1296 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1299 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1300 if (current_rtc_freq
!= RTC_FREQ
&&
1301 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1302 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1303 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1304 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1307 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1313 enable_sigio_timer(rtc_fd
);
1315 t
->priv
= (void *)(long)rtc_fd
;
1320 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1322 int rtc_fd
= (long)t
->priv
;
1327 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1331 struct sigaction act
;
1333 sigfillset(&act
.sa_mask
);
1335 act
.sa_handler
= host_alarm_handler
;
1337 sigaction(SIGALRM
, &act
, NULL
);
1340 * Initialize ev struct to 0 to avoid valgrind complaining
1341 * about uninitialized data in timer_create call
1343 memset(&ev
, 0, sizeof(ev
));
1344 ev
.sigev_value
.sival_int
= 0;
1345 ev
.sigev_notify
= SIGEV_SIGNAL
;
1346 ev
.sigev_signo
= SIGALRM
;
1348 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1349 perror("timer_create");
1351 /* disable dynticks */
1352 fprintf(stderr
, "Dynamic Ticks disabled\n");
1357 t
->priv
= (void *)(long)host_timer
;
1362 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1364 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1366 timer_delete(host_timer
);
1369 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1371 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1372 struct itimerspec timeout
;
1373 int64_t nearest_delta_us
= INT64_MAX
;
1376 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1377 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1378 !active_timers
[QEMU_CLOCK_HOST
])
1381 nearest_delta_us
= qemu_next_deadline_dyntick();
1383 /* check whether a timer is already running */
1384 if (timer_gettime(host_timer
, &timeout
)) {
1386 fprintf(stderr
, "Internal timer error: aborting\n");
1389 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1390 if (current_us
&& current_us
<= nearest_delta_us
)
1393 timeout
.it_interval
.tv_sec
= 0;
1394 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1395 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1396 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1397 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1399 fprintf(stderr
, "Internal timer error: aborting\n");
1404 #endif /* defined(__linux__) */
1406 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1408 struct sigaction act
;
1409 struct itimerval itv
;
1413 sigfillset(&act
.sa_mask
);
1415 act
.sa_handler
= host_alarm_handler
;
1417 sigaction(SIGALRM
, &act
, NULL
);
1419 itv
.it_interval
.tv_sec
= 0;
1420 /* for i386 kernel 2.6 to get 1 ms */
1421 itv
.it_interval
.tv_usec
= 999;
1422 itv
.it_value
.tv_sec
= 0;
1423 itv
.it_value
.tv_usec
= 10 * 1000;
1425 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1432 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1434 struct itimerval itv
;
1436 memset(&itv
, 0, sizeof(itv
));
1437 setitimer(ITIMER_REAL
, &itv
, NULL
);
1440 #endif /* !defined(_WIN32) */
1445 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1448 struct qemu_alarm_win32
*data
= t
->priv
;
1451 memset(&tc
, 0, sizeof(tc
));
1452 timeGetDevCaps(&tc
, sizeof(tc
));
1454 if (data
->period
< tc
.wPeriodMin
)
1455 data
->period
= tc
.wPeriodMin
;
1457 timeBeginPeriod(data
->period
);
1459 flags
= TIME_CALLBACK_FUNCTION
;
1460 if (alarm_has_dynticks(t
))
1461 flags
|= TIME_ONESHOT
;
1463 flags
|= TIME_PERIODIC
;
1465 data
->timerId
= timeSetEvent(1, // interval (ms)
1466 data
->period
, // resolution
1467 host_alarm_handler
, // function
1468 (DWORD
)t
, // parameter
1471 if (!data
->timerId
) {
1472 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1474 timeEndPeriod(data
->period
);
1481 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1483 struct qemu_alarm_win32
*data
= t
->priv
;
1485 timeKillEvent(data
->timerId
);
1486 timeEndPeriod(data
->period
);
1489 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1491 struct qemu_alarm_win32
*data
= t
->priv
;
1493 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1494 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1495 !active_timers
[QEMU_CLOCK_HOST
])
1498 timeKillEvent(data
->timerId
);
1500 data
->timerId
= timeSetEvent(1,
1504 TIME_ONESHOT
| TIME_PERIODIC
);
1506 if (!data
->timerId
) {
1507 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1510 timeEndPeriod(data
->period
);
1517 static int init_timer_alarm(void)
1519 struct qemu_alarm_timer
*t
= NULL
;
1522 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1523 t
= &alarm_timers
[i
];
1543 static void quit_timers(void)
1545 alarm_timer
->stop(alarm_timer
);
1549 /***********************************************************/
1550 /* host time/date access */
1551 void qemu_get_timedate(struct tm
*tm
, int offset
)
1558 if (rtc_date_offset
== -1) {
1562 ret
= localtime(&ti
);
1564 ti
-= rtc_date_offset
;
1568 memcpy(tm
, ret
, sizeof(struct tm
));
1571 int qemu_timedate_diff(struct tm
*tm
)
1575 if (rtc_date_offset
== -1)
1577 seconds
= mktimegm(tm
);
1579 seconds
= mktime(tm
);
1581 seconds
= mktimegm(tm
) + rtc_date_offset
;
1583 return seconds
- time(NULL
);
1586 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1588 time_t rtc_start_date
;
1591 if (!strcmp(startdate
, "now") && legacy
) {
1592 rtc_date_offset
= -1;
1594 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1602 } else if (sscanf(startdate
, "%d-%d-%d",
1605 &tm
.tm_mday
) == 3) {
1614 rtc_start_date
= mktimegm(&tm
);
1615 if (rtc_start_date
== -1) {
1617 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1618 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1621 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1625 static void configure_rtc(QemuOpts
*opts
)
1629 value
= qemu_opt_get(opts
, "base");
1631 if (!strcmp(value
, "utc")) {
1633 } else if (!strcmp(value
, "localtime")) {
1636 configure_rtc_date_offset(value
, 0);
1639 value
= qemu_opt_get(opts
, "clock");
1641 if (!strcmp(value
, "host")) {
1642 rtc_clock
= host_clock
;
1643 } else if (!strcmp(value
, "vm")) {
1644 rtc_clock
= vm_clock
;
1646 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1650 #ifdef CONFIG_TARGET_I386
1651 value
= qemu_opt_get(opts
, "driftfix");
1653 if (!strcmp(buf
, "slew")) {
1655 } else if (!strcmp(buf
, "none")) {
1658 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1666 static void socket_cleanup(void)
1671 static int socket_init(void)
1676 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1678 err
= WSAGetLastError();
1679 fprintf(stderr
, "WSAStartup: %d\n", err
);
1682 atexit(socket_cleanup
);
1687 /***********************************************************/
1688 /* Bluetooth support */
1691 static struct HCIInfo
*hci_table
[MAX_NICS
];
1693 static struct bt_vlan_s
{
1694 struct bt_scatternet_s net
;
1696 struct bt_vlan_s
*next
;
1699 /* find or alloc a new bluetooth "VLAN" */
1700 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1702 struct bt_vlan_s
**pvlan
, *vlan
;
1703 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1707 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1709 pvlan
= &first_bt_vlan
;
1710 while (*pvlan
!= NULL
)
1711 pvlan
= &(*pvlan
)->next
;
1716 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1720 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1725 static struct HCIInfo null_hci
= {
1726 .cmd_send
= null_hci_send
,
1727 .sco_send
= null_hci_send
,
1728 .acl_send
= null_hci_send
,
1729 .bdaddr_set
= null_hci_addr_set
,
1732 struct HCIInfo
*qemu_next_hci(void)
1734 if (cur_hci
== nb_hcis
)
1737 return hci_table
[cur_hci
++];
1740 static struct HCIInfo
*hci_init(const char *str
)
1743 struct bt_scatternet_s
*vlan
= 0;
1745 if (!strcmp(str
, "null"))
1748 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1750 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1751 else if (!strncmp(str
, "hci", 3)) {
1754 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1755 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1760 vlan
= qemu_find_bt_vlan(0);
1762 return bt_new_hci(vlan
);
1765 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1770 static int bt_hci_parse(const char *str
)
1772 struct HCIInfo
*hci
;
1775 if (nb_hcis
>= MAX_NICS
) {
1776 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1780 hci
= hci_init(str
);
1789 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1790 hci
->bdaddr_set(hci
, bdaddr
.b
);
1792 hci_table
[nb_hcis
++] = hci
;
1797 static void bt_vhci_add(int vlan_id
)
1799 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1802 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1803 "an empty scatternet %i\n", vlan_id
);
1805 bt_vhci_init(bt_new_hci(vlan
));
1808 static struct bt_device_s
*bt_device_add(const char *opt
)
1810 struct bt_scatternet_s
*vlan
;
1812 char *endp
= strstr(opt
, ",vlan=");
1813 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1816 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1819 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1821 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1826 vlan
= qemu_find_bt_vlan(vlan_id
);
1829 fprintf(stderr
, "qemu: warning: adding a slave device to "
1830 "an empty scatternet %i\n", vlan_id
);
1832 if (!strcmp(devname
, "keyboard"))
1833 return bt_keyboard_init(vlan
);
1835 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1839 static int bt_parse(const char *opt
)
1841 const char *endp
, *p
;
1844 if (strstart(opt
, "hci", &endp
)) {
1845 if (!*endp
|| *endp
== ',') {
1847 if (!strstart(endp
, ",vlan=", 0))
1850 return bt_hci_parse(opt
);
1852 } else if (strstart(opt
, "vhci", &endp
)) {
1853 if (!*endp
|| *endp
== ',') {
1855 if (strstart(endp
, ",vlan=", &p
)) {
1856 vlan
= strtol(p
, (char **) &endp
, 0);
1858 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1862 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1871 } else if (strstart(opt
, "device:", &endp
))
1872 return !bt_device_add(endp
);
1874 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1878 /***********************************************************/
1879 /* QEMU Block devices */
1881 #define HD_ALIAS "index=%d,media=disk"
1882 #define CDROM_ALIAS "index=2,media=cdrom"
1883 #define FD_ALIAS "index=%d,if=floppy"
1884 #define PFLASH_ALIAS "if=pflash"
1885 #define MTD_ALIAS "if=mtd"
1886 #define SD_ALIAS "index=0,if=sd"
1888 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1895 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1898 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1900 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1901 __FUNCTION__
, optstr
);
1905 qemu_opt_set(opts
, "file", file
);
1909 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1913 /* seek interface, bus and unit */
1915 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1916 if (dinfo
->type
== type
&&
1917 dinfo
->bus
== bus
&&
1918 dinfo
->unit
== unit
)
1925 DriveInfo
*drive_get_by_id(const char *id
)
1929 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1930 if (strcmp(id
, dinfo
->id
))
1937 int drive_get_max_bus(BlockInterfaceType type
)
1943 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1944 if(dinfo
->type
== type
&&
1945 dinfo
->bus
> max_bus
)
1946 max_bus
= dinfo
->bus
;
1951 const char *drive_get_serial(BlockDriverState
*bdrv
)
1955 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1956 if (dinfo
->bdrv
== bdrv
)
1957 return dinfo
->serial
;
1963 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1967 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1968 if (dinfo
->bdrv
== bdrv
)
1969 return dinfo
->onerror
;
1972 return BLOCK_ERR_STOP_ENOSPC
;
1975 static void bdrv_format_print(void *opaque
, const char *name
)
1977 fprintf(stderr
, " %s", name
);
1980 void drive_uninit(DriveInfo
*dinfo
)
1982 qemu_opts_del(dinfo
->opts
);
1983 bdrv_delete(dinfo
->bdrv
);
1984 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1988 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1992 const char *file
= NULL
;
1995 const char *mediastr
= "";
1996 BlockInterfaceType type
;
1997 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1998 int bus_id
, unit_id
;
1999 int cyls
, heads
, secs
, translation
;
2000 BlockDriver
*drv
= NULL
;
2001 QEMUMachine
*machine
= opaque
;
2007 int bdrv_flags
, onerror
;
2008 const char *devaddr
;
2014 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2017 if (machine
&& machine
->use_scsi
) {
2019 max_devs
= MAX_SCSI_DEVS
;
2020 pstrcpy(devname
, sizeof(devname
), "scsi");
2023 max_devs
= MAX_IDE_DEVS
;
2024 pstrcpy(devname
, sizeof(devname
), "ide");
2028 /* extract parameters */
2029 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2030 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2031 index
= qemu_opt_get_number(opts
, "index", -1);
2033 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2034 heads
= qemu_opt_get_number(opts
, "heads", 0);
2035 secs
= qemu_opt_get_number(opts
, "secs", 0);
2037 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2038 ro
= qemu_opt_get_bool(opts
, "readonly", 0);
2040 file
= qemu_opt_get(opts
, "file");
2041 serial
= qemu_opt_get(opts
, "serial");
2043 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2044 pstrcpy(devname
, sizeof(devname
), buf
);
2045 if (!strcmp(buf
, "ide")) {
2047 max_devs
= MAX_IDE_DEVS
;
2048 } else if (!strcmp(buf
, "scsi")) {
2050 max_devs
= MAX_SCSI_DEVS
;
2051 } else if (!strcmp(buf
, "floppy")) {
2054 } else if (!strcmp(buf
, "pflash")) {
2057 } else if (!strcmp(buf
, "mtd")) {
2060 } else if (!strcmp(buf
, "sd")) {
2063 } else if (!strcmp(buf
, "virtio")) {
2066 } else if (!strcmp(buf
, "xen")) {
2069 } else if (!strcmp(buf
, "none")) {
2073 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2078 if (cyls
|| heads
|| secs
) {
2079 if (cyls
< 1 || (type
== IF_IDE
&& cyls
> 16383)) {
2080 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2083 if (heads
< 1 || (type
== IF_IDE
&& heads
> 16)) {
2084 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2087 if (secs
< 1 || (type
== IF_IDE
&& secs
> 63)) {
2088 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2093 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2096 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2100 if (!strcmp(buf
, "none"))
2101 translation
= BIOS_ATA_TRANSLATION_NONE
;
2102 else if (!strcmp(buf
, "lba"))
2103 translation
= BIOS_ATA_TRANSLATION_LBA
;
2104 else if (!strcmp(buf
, "auto"))
2105 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2107 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2112 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2113 if (!strcmp(buf
, "disk")) {
2115 } else if (!strcmp(buf
, "cdrom")) {
2116 if (cyls
|| secs
|| heads
) {
2118 "qemu: '%s' invalid physical CHS format\n", buf
);
2121 media
= MEDIA_CDROM
;
2123 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2128 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2129 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2131 else if (!strcmp(buf
, "writethrough"))
2133 else if (!strcmp(buf
, "writeback"))
2136 fprintf(stderr
, "qemu: invalid cache option\n");
2141 #ifdef CONFIG_LINUX_AIO
2142 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2143 if (!strcmp(buf
, "threads"))
2145 else if (!strcmp(buf
, "native"))
2148 fprintf(stderr
, "qemu: invalid aio option\n");
2154 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2155 if (strcmp(buf
, "?") == 0) {
2156 fprintf(stderr
, "qemu: Supported formats:");
2157 bdrv_iterate_format(bdrv_format_print
, NULL
);
2158 fprintf(stderr
, "\n");
2161 drv
= bdrv_find_whitelisted_format(buf
);
2163 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2168 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2169 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2170 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2171 fprintf(stderr
, "werror is no supported by this format\n");
2174 if (!strcmp(buf
, "ignore"))
2175 onerror
= BLOCK_ERR_IGNORE
;
2176 else if (!strcmp(buf
, "enospc"))
2177 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2178 else if (!strcmp(buf
, "stop"))
2179 onerror
= BLOCK_ERR_STOP_ANY
;
2180 else if (!strcmp(buf
, "report"))
2181 onerror
= BLOCK_ERR_REPORT
;
2183 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2188 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2189 if (type
!= IF_VIRTIO
) {
2190 fprintf(stderr
, "addr is not supported\n");
2195 /* compute bus and unit according index */
2198 if (bus_id
!= 0 || unit_id
!= -1) {
2200 "qemu: index cannot be used with bus and unit\n");
2208 unit_id
= index
% max_devs
;
2209 bus_id
= index
/ max_devs
;
2213 /* if user doesn't specify a unit_id,
2214 * try to find the first free
2217 if (unit_id
== -1) {
2219 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2221 if (max_devs
&& unit_id
>= max_devs
) {
2222 unit_id
-= max_devs
;
2230 if (max_devs
&& unit_id
>= max_devs
) {
2231 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2232 unit_id
, max_devs
- 1);
2237 * ignore multiple definitions
2240 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2247 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2248 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2249 dinfo
->id
= qemu_strdup(buf
);
2251 /* no id supplied -> create one */
2252 dinfo
->id
= qemu_mallocz(32);
2253 if (type
== IF_IDE
|| type
== IF_SCSI
)
2254 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2256 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2257 devname
, bus_id
, mediastr
, unit_id
);
2259 snprintf(dinfo
->id
, 32, "%s%s%i",
2260 devname
, mediastr
, unit_id
);
2262 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2263 dinfo
->devaddr
= devaddr
;
2265 dinfo
->bus
= bus_id
;
2266 dinfo
->unit
= unit_id
;
2267 dinfo
->onerror
= onerror
;
2270 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2271 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2281 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2282 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2286 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2291 /* FIXME: This isn't really a floppy, but it's a reasonable
2294 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2300 /* add virtio block device */
2301 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2302 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2303 qemu_opt_set(opts
, "drive", dinfo
->id
);
2305 qemu_opt_set(opts
, "addr", devaddr
);
2316 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2317 cache
= 2; /* always use write-back with snapshot */
2319 if (cache
== 0) /* no caching */
2320 bdrv_flags
|= BDRV_O_NOCACHE
;
2321 else if (cache
== 2) /* write-back */
2322 bdrv_flags
|= BDRV_O_CACHE_WB
;
2325 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2327 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2331 if (type
== IF_IDE
) {
2332 fprintf(stderr
, "qemu: readonly flag not supported for drive with ide interface\n");
2335 (void)bdrv_set_read_only(dinfo
->bdrv
, 1);
2338 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2339 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2340 file
, strerror(errno
));
2344 if (bdrv_key_required(dinfo
->bdrv
))
2350 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2352 QEMUMachine
*machine
= opaque
;
2353 int fatal_error
= 0;
2355 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2362 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2364 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2365 qemu_opt_set(opts
, "snapshot", "on");
2370 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2372 boot_set_handler
= func
;
2373 boot_set_opaque
= opaque
;
2376 int qemu_boot_set(const char *boot_devices
)
2378 if (!boot_set_handler
) {
2381 return boot_set_handler(boot_set_opaque
, boot_devices
);
2384 static int parse_bootdevices(char *devices
)
2386 /* We just do some generic consistency checks */
2390 for (p
= devices
; *p
!= '\0'; p
++) {
2391 /* Allowed boot devices are:
2392 * a-b: floppy disk drives
2393 * c-f: IDE disk drives
2394 * g-m: machine implementation dependant drives
2395 * n-p: network devices
2396 * It's up to each machine implementation to check if the given boot
2397 * devices match the actual hardware implementation and firmware
2400 if (*p
< 'a' || *p
> 'p') {
2401 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2404 if (bitmap
& (1 << (*p
- 'a'))) {
2405 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2408 bitmap
|= 1 << (*p
- 'a');
2413 static void restore_boot_devices(void *opaque
)
2415 char *standard_boot_devices
= opaque
;
2417 qemu_boot_set(standard_boot_devices
);
2419 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2420 qemu_free(standard_boot_devices
);
2423 static void numa_add(const char *optarg
)
2427 unsigned long long value
, endvalue
;
2430 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2431 if (!strcmp(option
, "node")) {
2432 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2433 nodenr
= nb_numa_nodes
;
2435 nodenr
= strtoull(option
, NULL
, 10);
2438 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2439 node_mem
[nodenr
] = 0;
2441 value
= strtoull(option
, &endptr
, 0);
2443 case 0: case 'M': case 'm':
2450 node_mem
[nodenr
] = value
;
2452 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2453 node_cpumask
[nodenr
] = 0;
2455 value
= strtoull(option
, &endptr
, 10);
2458 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2460 if (*endptr
== '-') {
2461 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2462 if (endvalue
>= 63) {
2465 "only 63 CPUs in NUMA mode supported.\n");
2467 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2472 node_cpumask
[nodenr
] = value
;
2479 static void smp_parse(const char *optarg
)
2481 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2485 smp
= strtoul(optarg
, &endptr
, 10);
2486 if (endptr
!= optarg
) {
2487 if (*endptr
== ',') {
2491 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2492 sockets
= strtoull(option
, NULL
, 10);
2493 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2494 cores
= strtoull(option
, NULL
, 10);
2495 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2496 threads
= strtoull(option
, NULL
, 10);
2497 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2498 max_cpus
= strtoull(option
, NULL
, 10);
2500 /* compute missing values, prefer sockets over cores over threads */
2501 if (smp
== 0 || sockets
== 0) {
2502 sockets
= sockets
> 0 ? sockets
: 1;
2503 cores
= cores
> 0 ? cores
: 1;
2504 threads
= threads
> 0 ? threads
: 1;
2506 smp
= cores
* threads
* sockets
;
2508 sockets
= smp
/ (cores
* threads
);
2512 threads
= threads
> 0 ? threads
: 1;
2513 cores
= smp
/ (sockets
* threads
);
2516 sockets
= smp
/ (cores
* threads
);
2518 threads
= smp
/ (cores
* sockets
);
2523 smp_cores
= cores
> 0 ? cores
: 1;
2524 smp_threads
= threads
> 0 ? threads
: 1;
2526 max_cpus
= smp_cpus
;
2529 /***********************************************************/
2532 static int usb_device_add(const char *devname
, int is_hotplug
)
2535 USBDevice
*dev
= NULL
;
2540 /* drivers with .usbdevice_name entry in USBDeviceInfo */
2541 dev
= usbdevice_create(devname
);
2545 /* the other ones */
2546 if (strstart(devname
, "host:", &p
)) {
2547 dev
= usb_host_device_open(p
);
2548 } else if (strstart(devname
, "net:", &p
)) {
2552 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2557 qemu_opt_set(opts
, "type", "nic");
2558 qemu_opt_set(opts
, "model", "usb");
2560 idx
= net_client_init(NULL
, opts
, 0);
2565 dev
= usb_net_init(&nd_table
[idx
]);
2566 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2567 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2568 bt_new_hci(qemu_find_bt_vlan(0)));
2579 static int usb_device_del(const char *devname
)
2584 if (strstart(devname
, "host:", &p
))
2585 return usb_host_device_close(p
);
2590 p
= strchr(devname
, '.');
2593 bus_num
= strtoul(devname
, NULL
, 0);
2594 addr
= strtoul(p
+ 1, NULL
, 0);
2596 return usb_device_delete_addr(bus_num
, addr
);
2599 static int usb_parse(const char *cmdline
)
2601 return usb_device_add(cmdline
, 0);
2604 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2606 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2609 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2611 usb_device_del(qdict_get_str(qdict
, "devname"));
2614 /***********************************************************/
2615 /* PCMCIA/Cardbus */
2617 static struct pcmcia_socket_entry_s
{
2618 PCMCIASocket
*socket
;
2619 struct pcmcia_socket_entry_s
*next
;
2620 } *pcmcia_sockets
= 0;
2622 void pcmcia_socket_register(PCMCIASocket
*socket
)
2624 struct pcmcia_socket_entry_s
*entry
;
2626 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2627 entry
->socket
= socket
;
2628 entry
->next
= pcmcia_sockets
;
2629 pcmcia_sockets
= entry
;
2632 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2634 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2636 ptr
= &pcmcia_sockets
;
2637 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2638 if (entry
->socket
== socket
) {
2644 void pcmcia_info(Monitor
*mon
)
2646 struct pcmcia_socket_entry_s
*iter
;
2648 if (!pcmcia_sockets
)
2649 monitor_printf(mon
, "No PCMCIA sockets\n");
2651 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2652 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2653 iter
->socket
->attached
? iter
->socket
->card_string
:
2657 /***********************************************************/
2658 /* register display */
2660 struct DisplayAllocator default_allocator
= {
2661 defaultallocator_create_displaysurface
,
2662 defaultallocator_resize_displaysurface
,
2663 defaultallocator_free_displaysurface
2666 void register_displaystate(DisplayState
*ds
)
2676 DisplayState
*get_displaystate(void)
2678 return display_state
;
2681 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2683 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2684 return ds
->allocator
;
2689 static void dumb_display_init(void)
2691 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2692 ds
->allocator
= &default_allocator
;
2693 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2694 register_displaystate(ds
);
2697 /***********************************************************/
2700 typedef struct IOHandlerRecord
{
2702 IOCanRWHandler
*fd_read_poll
;
2704 IOHandler
*fd_write
;
2707 /* temporary data */
2709 struct IOHandlerRecord
*next
;
2712 static IOHandlerRecord
*first_io_handler
;
2714 /* XXX: fd_read_poll should be suppressed, but an API change is
2715 necessary in the character devices to suppress fd_can_read(). */
2716 int qemu_set_fd_handler2(int fd
,
2717 IOCanRWHandler
*fd_read_poll
,
2719 IOHandler
*fd_write
,
2722 IOHandlerRecord
**pioh
, *ioh
;
2724 if (!fd_read
&& !fd_write
) {
2725 pioh
= &first_io_handler
;
2730 if (ioh
->fd
== fd
) {
2737 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2741 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2742 ioh
->next
= first_io_handler
;
2743 first_io_handler
= ioh
;
2746 ioh
->fd_read_poll
= fd_read_poll
;
2747 ioh
->fd_read
= fd_read
;
2748 ioh
->fd_write
= fd_write
;
2749 ioh
->opaque
= opaque
;
2755 int qemu_set_fd_handler(int fd
,
2757 IOHandler
*fd_write
,
2760 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2764 /***********************************************************/
2765 /* Polling handling */
2767 typedef struct PollingEntry
{
2770 struct PollingEntry
*next
;
2773 static PollingEntry
*first_polling_entry
;
2775 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2777 PollingEntry
**ppe
, *pe
;
2778 pe
= qemu_mallocz(sizeof(PollingEntry
));
2780 pe
->opaque
= opaque
;
2781 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2786 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2788 PollingEntry
**ppe
, *pe
;
2789 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2791 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2799 /***********************************************************/
2800 /* Wait objects support */
2801 typedef struct WaitObjects
{
2803 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2804 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2805 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2808 static WaitObjects wait_objects
= {0};
2810 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2812 WaitObjects
*w
= &wait_objects
;
2814 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2816 w
->events
[w
->num
] = handle
;
2817 w
->func
[w
->num
] = func
;
2818 w
->opaque
[w
->num
] = opaque
;
2823 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2826 WaitObjects
*w
= &wait_objects
;
2829 for (i
= 0; i
< w
->num
; i
++) {
2830 if (w
->events
[i
] == handle
)
2833 w
->events
[i
] = w
->events
[i
+ 1];
2834 w
->func
[i
] = w
->func
[i
+ 1];
2835 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2843 /***********************************************************/
2844 /* ram save/restore */
2846 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2847 #define RAM_SAVE_FLAG_COMPRESS 0x02
2848 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2849 #define RAM_SAVE_FLAG_PAGE 0x08
2850 #define RAM_SAVE_FLAG_EOS 0x10
2852 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2854 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2855 uint32_t *array
= (uint32_t *)page
;
2858 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2859 if (array
[i
] != val
)
2866 static int ram_save_block(QEMUFile
*f
)
2868 static ram_addr_t current_addr
= 0;
2869 ram_addr_t saved_addr
= current_addr
;
2870 ram_addr_t addr
= 0;
2873 while (addr
< last_ram_offset
) {
2874 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2877 cpu_physical_memory_reset_dirty(current_addr
,
2878 current_addr
+ TARGET_PAGE_SIZE
,
2879 MIGRATION_DIRTY_FLAG
);
2881 p
= qemu_get_ram_ptr(current_addr
);
2883 if (is_dup_page(p
, *p
)) {
2884 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2885 qemu_put_byte(f
, *p
);
2887 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2888 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2894 addr
+= TARGET_PAGE_SIZE
;
2895 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2901 static uint64_t bytes_transferred
= 0;
2903 static ram_addr_t
ram_save_remaining(void)
2906 ram_addr_t count
= 0;
2908 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2909 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2916 uint64_t ram_bytes_remaining(void)
2918 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2921 uint64_t ram_bytes_transferred(void)
2923 return bytes_transferred
;
2926 uint64_t ram_bytes_total(void)
2928 return last_ram_offset
;
2931 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2934 uint64_t bytes_transferred_last
;
2936 uint64_t expected_time
= 0;
2938 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2939 qemu_file_set_error(f
);
2944 /* Make sure all dirty bits are set */
2945 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2946 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2947 cpu_physical_memory_set_dirty(addr
);
2950 /* Enable dirty memory tracking */
2951 cpu_physical_memory_set_dirty_tracking(1);
2953 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2956 bytes_transferred_last
= bytes_transferred
;
2957 bwidth
= get_clock();
2959 while (!qemu_file_rate_limit(f
)) {
2962 ret
= ram_save_block(f
);
2963 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2964 if (ret
== 0) /* no more blocks */
2968 bwidth
= get_clock() - bwidth
;
2969 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2971 /* if we haven't transferred anything this round, force expected_time to a
2972 * a very high value, but without crashing */
2976 /* try transferring iterative blocks of memory */
2978 /* flush all remaining blocks regardless of rate limiting */
2979 while (ram_save_block(f
) != 0) {
2980 bytes_transferred
+= TARGET_PAGE_SIZE
;
2982 cpu_physical_memory_set_dirty_tracking(0);
2985 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2987 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2989 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2992 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2997 if (version_id
!= 3)
3001 addr
= qemu_get_be64(f
);
3003 flags
= addr
& ~TARGET_PAGE_MASK
;
3004 addr
&= TARGET_PAGE_MASK
;
3006 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3007 if (addr
!= last_ram_offset
)
3011 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3012 uint8_t ch
= qemu_get_byte(f
);
3013 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3016 (!kvm_enabled() || kvm_has_sync_mmu())) {
3017 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3020 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3021 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3022 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3027 void qemu_service_io(void)
3029 qemu_notify_event();
3032 /***********************************************************/
3033 /* machine registration */
3035 static QEMUMachine
*first_machine
= NULL
;
3036 QEMUMachine
*current_machine
= NULL
;
3038 int qemu_register_machine(QEMUMachine
*m
)
3041 pm
= &first_machine
;
3049 static QEMUMachine
*find_machine(const char *name
)
3053 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3054 if (!strcmp(m
->name
, name
))
3056 if (m
->alias
&& !strcmp(m
->alias
, name
))
3062 static QEMUMachine
*find_default_machine(void)
3066 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3067 if (m
->is_default
) {
3074 /***********************************************************/
3075 /* main execution loop */
3077 static void gui_update(void *opaque
)
3079 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3080 DisplayState
*ds
= opaque
;
3081 DisplayChangeListener
*dcl
= ds
->listeners
;
3085 while (dcl
!= NULL
) {
3086 if (dcl
->gui_timer_interval
&&
3087 dcl
->gui_timer_interval
< interval
)
3088 interval
= dcl
->gui_timer_interval
;
3091 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3094 static void nographic_update(void *opaque
)
3096 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3098 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3101 struct vm_change_state_entry
{
3102 VMChangeStateHandler
*cb
;
3104 QLIST_ENTRY (vm_change_state_entry
) entries
;
3107 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3109 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3112 VMChangeStateEntry
*e
;
3114 e
= qemu_mallocz(sizeof (*e
));
3118 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3122 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3124 QLIST_REMOVE (e
, entries
);
3128 static void vm_state_notify(int running
, int reason
)
3130 VMChangeStateEntry
*e
;
3132 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3133 e
->cb(e
->opaque
, running
, reason
);
3137 static void resume_all_vcpus(void);
3138 static void pause_all_vcpus(void);
3145 vm_state_notify(1, 0);
3146 qemu_rearm_alarm_timer(alarm_timer
);
3151 /* reset/shutdown handler */
3153 typedef struct QEMUResetEntry
{
3154 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3155 QEMUResetHandler
*func
;
3159 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3160 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3161 static int reset_requested
;
3162 static int shutdown_requested
;
3163 static int powerdown_requested
;
3164 static int debug_requested
;
3165 static int vmstop_requested
;
3167 int qemu_shutdown_requested(void)
3169 int r
= shutdown_requested
;
3170 shutdown_requested
= 0;
3174 int qemu_reset_requested(void)
3176 int r
= reset_requested
;
3177 reset_requested
= 0;
3181 int qemu_powerdown_requested(void)
3183 int r
= powerdown_requested
;
3184 powerdown_requested
= 0;
3188 static int qemu_debug_requested(void)
3190 int r
= debug_requested
;
3191 debug_requested
= 0;
3195 static int qemu_vmstop_requested(void)
3197 int r
= vmstop_requested
;
3198 vmstop_requested
= 0;
3202 static void do_vm_stop(int reason
)
3205 cpu_disable_ticks();
3208 vm_state_notify(0, reason
);
3212 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3214 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3217 re
->opaque
= opaque
;
3218 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3221 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3225 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3226 if (re
->func
== func
&& re
->opaque
== opaque
) {
3227 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3234 void qemu_system_reset(void)
3236 QEMUResetEntry
*re
, *nre
;
3238 /* reset all devices */
3239 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3240 re
->func(re
->opaque
);
3244 void qemu_system_reset_request(void)
3247 shutdown_requested
= 1;
3249 reset_requested
= 1;
3251 qemu_notify_event();
3254 void qemu_system_shutdown_request(void)
3256 shutdown_requested
= 1;
3257 qemu_notify_event();
3260 void qemu_system_powerdown_request(void)
3262 powerdown_requested
= 1;
3263 qemu_notify_event();
3266 #ifdef CONFIG_IOTHREAD
3267 static void qemu_system_vmstop_request(int reason
)
3269 vmstop_requested
= reason
;
3270 qemu_notify_event();
3275 static int io_thread_fd
= -1;
3277 static void qemu_event_increment(void)
3279 static const char byte
= 0;
3281 if (io_thread_fd
== -1)
3284 write(io_thread_fd
, &byte
, sizeof(byte
));
3287 static void qemu_event_read(void *opaque
)
3289 int fd
= (unsigned long)opaque
;
3292 /* Drain the notify pipe */
3295 len
= read(fd
, buffer
, sizeof(buffer
));
3296 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3299 static int qemu_event_init(void)
3308 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3312 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3316 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3317 (void *)(unsigned long)fds
[0]);
3319 io_thread_fd
= fds
[1];
3328 HANDLE qemu_event_handle
;
3330 static void dummy_event_handler(void *opaque
)
3334 static int qemu_event_init(void)
3336 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3337 if (!qemu_event_handle
) {
3338 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3341 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3345 static void qemu_event_increment(void)
3347 if (!SetEvent(qemu_event_handle
)) {
3348 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3355 static int cpu_can_run(CPUState
*env
)
3364 #ifndef CONFIG_IOTHREAD
3365 static int qemu_init_main_loop(void)
3367 return qemu_event_init();
3370 void qemu_init_vcpu(void *_env
)
3372 CPUState
*env
= _env
;
3376 env
->nr_cores
= smp_cores
;
3377 env
->nr_threads
= smp_threads
;
3381 int qemu_cpu_self(void *env
)
3386 static void resume_all_vcpus(void)
3390 static void pause_all_vcpus(void)
3394 void qemu_cpu_kick(void *env
)
3399 void qemu_notify_event(void)
3401 CPUState
*env
= cpu_single_env
;
3408 void qemu_mutex_lock_iothread(void) {}
3409 void qemu_mutex_unlock_iothread(void) {}
3411 void vm_stop(int reason
)
3416 #else /* CONFIG_IOTHREAD */
3418 #include "qemu-thread.h"
3420 QemuMutex qemu_global_mutex
;
3421 static QemuMutex qemu_fair_mutex
;
3423 static QemuThread io_thread
;
3425 static QemuThread
*tcg_cpu_thread
;
3426 static QemuCond
*tcg_halt_cond
;
3428 static int qemu_system_ready
;
3430 static QemuCond qemu_cpu_cond
;
3432 static QemuCond qemu_system_cond
;
3433 static QemuCond qemu_pause_cond
;
3435 static void block_io_signals(void);
3436 static void unblock_io_signals(void);
3437 static int tcg_has_work(void);
3439 static int qemu_init_main_loop(void)
3443 ret
= qemu_event_init();
3447 qemu_cond_init(&qemu_pause_cond
);
3448 qemu_mutex_init(&qemu_fair_mutex
);
3449 qemu_mutex_init(&qemu_global_mutex
);
3450 qemu_mutex_lock(&qemu_global_mutex
);
3452 unblock_io_signals();
3453 qemu_thread_self(&io_thread
);
3458 static void qemu_wait_io_event(CPUState
*env
)
3460 while (!tcg_has_work())
3461 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3463 qemu_mutex_unlock(&qemu_global_mutex
);
3466 * Users of qemu_global_mutex can be starved, having no chance
3467 * to acquire it since this path will get to it first.
3468 * So use another lock to provide fairness.
3470 qemu_mutex_lock(&qemu_fair_mutex
);
3471 qemu_mutex_unlock(&qemu_fair_mutex
);
3473 qemu_mutex_lock(&qemu_global_mutex
);
3477 qemu_cond_signal(&qemu_pause_cond
);
3481 static int qemu_cpu_exec(CPUState
*env
);
3483 static void *kvm_cpu_thread_fn(void *arg
)
3485 CPUState
*env
= arg
;
3488 qemu_thread_self(env
->thread
);
3492 /* signal CPU creation */
3493 qemu_mutex_lock(&qemu_global_mutex
);
3495 qemu_cond_signal(&qemu_cpu_cond
);
3497 /* and wait for machine initialization */
3498 while (!qemu_system_ready
)
3499 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3502 if (cpu_can_run(env
))
3504 qemu_wait_io_event(env
);
3510 static void tcg_cpu_exec(void);
3512 static void *tcg_cpu_thread_fn(void *arg
)
3514 CPUState
*env
= arg
;
3517 qemu_thread_self(env
->thread
);
3519 /* signal CPU creation */
3520 qemu_mutex_lock(&qemu_global_mutex
);
3521 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3523 qemu_cond_signal(&qemu_cpu_cond
);
3525 /* and wait for machine initialization */
3526 while (!qemu_system_ready
)
3527 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3531 qemu_wait_io_event(cur_cpu
);
3537 void qemu_cpu_kick(void *_env
)
3539 CPUState
*env
= _env
;
3540 qemu_cond_broadcast(env
->halt_cond
);
3542 qemu_thread_signal(env
->thread
, SIGUSR1
);
3545 int qemu_cpu_self(void *_env
)
3547 CPUState
*env
= _env
;
3550 qemu_thread_self(&this);
3552 return qemu_thread_equal(&this, env
->thread
);
3555 static void cpu_signal(int sig
)
3558 cpu_exit(cpu_single_env
);
3561 static void block_io_signals(void)
3564 struct sigaction sigact
;
3567 sigaddset(&set
, SIGUSR2
);
3568 sigaddset(&set
, SIGIO
);
3569 sigaddset(&set
, SIGALRM
);
3570 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3573 sigaddset(&set
, SIGUSR1
);
3574 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3576 memset(&sigact
, 0, sizeof(sigact
));
3577 sigact
.sa_handler
= cpu_signal
;
3578 sigaction(SIGUSR1
, &sigact
, NULL
);
3581 static void unblock_io_signals(void)
3586 sigaddset(&set
, SIGUSR2
);
3587 sigaddset(&set
, SIGIO
);
3588 sigaddset(&set
, SIGALRM
);
3589 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3592 sigaddset(&set
, SIGUSR1
);
3593 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3596 static void qemu_signal_lock(unsigned int msecs
)
3598 qemu_mutex_lock(&qemu_fair_mutex
);
3600 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3601 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3602 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3605 qemu_mutex_unlock(&qemu_fair_mutex
);
3608 void qemu_mutex_lock_iothread(void)
3610 if (kvm_enabled()) {
3611 qemu_mutex_lock(&qemu_fair_mutex
);
3612 qemu_mutex_lock(&qemu_global_mutex
);
3613 qemu_mutex_unlock(&qemu_fair_mutex
);
3615 qemu_signal_lock(100);
3618 void qemu_mutex_unlock_iothread(void)
3620 qemu_mutex_unlock(&qemu_global_mutex
);
3623 static int all_vcpus_paused(void)
3625 CPUState
*penv
= first_cpu
;
3630 penv
= (CPUState
*)penv
->next_cpu
;
3636 static void pause_all_vcpus(void)
3638 CPUState
*penv
= first_cpu
;
3642 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3643 qemu_cpu_kick(penv
);
3644 penv
= (CPUState
*)penv
->next_cpu
;
3647 while (!all_vcpus_paused()) {
3648 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3651 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3652 penv
= (CPUState
*)penv
->next_cpu
;
3657 static void resume_all_vcpus(void)
3659 CPUState
*penv
= first_cpu
;
3664 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3665 qemu_cpu_kick(penv
);
3666 penv
= (CPUState
*)penv
->next_cpu
;
3670 static void tcg_init_vcpu(void *_env
)
3672 CPUState
*env
= _env
;
3673 /* share a single thread for all cpus with TCG */
3674 if (!tcg_cpu_thread
) {
3675 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3676 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3677 qemu_cond_init(env
->halt_cond
);
3678 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3679 while (env
->created
== 0)
3680 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3681 tcg_cpu_thread
= env
->thread
;
3682 tcg_halt_cond
= env
->halt_cond
;
3684 env
->thread
= tcg_cpu_thread
;
3685 env
->halt_cond
= tcg_halt_cond
;
3689 static void kvm_start_vcpu(CPUState
*env
)
3691 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3692 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3693 qemu_cond_init(env
->halt_cond
);
3694 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3695 while (env
->created
== 0)
3696 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3699 void qemu_init_vcpu(void *_env
)
3701 CPUState
*env
= _env
;
3704 kvm_start_vcpu(env
);
3707 env
->nr_cores
= smp_cores
;
3708 env
->nr_threads
= smp_threads
;
3711 void qemu_notify_event(void)
3713 qemu_event_increment();
3716 void vm_stop(int reason
)
3719 qemu_thread_self(&me
);
3721 if (!qemu_thread_equal(&me
, &io_thread
)) {
3722 qemu_system_vmstop_request(reason
);
3724 * FIXME: should not return to device code in case
3725 * vm_stop() has been requested.
3727 if (cpu_single_env
) {
3728 cpu_exit(cpu_single_env
);
3729 cpu_single_env
->stop
= 1;
3740 static void host_main_loop_wait(int *timeout
)
3746 /* XXX: need to suppress polling by better using win32 events */
3748 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3749 ret
|= pe
->func(pe
->opaque
);
3753 WaitObjects
*w
= &wait_objects
;
3755 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3756 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3757 if (w
->func
[ret
- WAIT_OBJECT_0
])
3758 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3760 /* Check for additional signaled events */
3761 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3763 /* Check if event is signaled */
3764 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3765 if(ret2
== WAIT_OBJECT_0
) {
3767 w
->func
[i
](w
->opaque
[i
]);
3768 } else if (ret2
== WAIT_TIMEOUT
) {
3770 err
= GetLastError();
3771 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3774 } else if (ret
== WAIT_TIMEOUT
) {
3776 err
= GetLastError();
3777 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3784 static void host_main_loop_wait(int *timeout
)
3789 void main_loop_wait(int timeout
)
3791 IOHandlerRecord
*ioh
;
3792 fd_set rfds
, wfds
, xfds
;
3796 qemu_bh_update_timeout(&timeout
);
3798 host_main_loop_wait(&timeout
);
3800 /* poll any events */
3801 /* XXX: separate device handlers from system ones */
3806 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3810 (!ioh
->fd_read_poll
||
3811 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3812 FD_SET(ioh
->fd
, &rfds
);
3816 if (ioh
->fd_write
) {
3817 FD_SET(ioh
->fd
, &wfds
);
3823 tv
.tv_sec
= timeout
/ 1000;
3824 tv
.tv_usec
= (timeout
% 1000) * 1000;
3826 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3828 qemu_mutex_unlock_iothread();
3829 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3830 qemu_mutex_lock_iothread();
3832 IOHandlerRecord
**pioh
;
3834 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3835 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3836 ioh
->fd_read(ioh
->opaque
);
3838 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3839 ioh
->fd_write(ioh
->opaque
);
3843 /* remove deleted IO handlers */
3844 pioh
= &first_io_handler
;
3855 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3857 /* rearm timer, if not periodic */
3858 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3859 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3860 qemu_rearm_alarm_timer(alarm_timer
);
3863 /* vm time timers */
3865 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3866 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3867 qemu_get_clock(vm_clock
));
3870 /* real time timers */
3871 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3872 qemu_get_clock(rt_clock
));
3874 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3875 qemu_get_clock(host_clock
));
3877 /* Check bottom-halves last in case any of the earlier events triggered
3883 static int qemu_cpu_exec(CPUState
*env
)
3886 #ifdef CONFIG_PROFILER
3890 #ifdef CONFIG_PROFILER
3891 ti
= profile_getclock();
3896 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3897 env
->icount_decr
.u16
.low
= 0;
3898 env
->icount_extra
= 0;
3899 count
= qemu_next_deadline();
3900 count
= (count
+ (1 << icount_time_shift
) - 1)
3901 >> icount_time_shift
;
3902 qemu_icount
+= count
;
3903 decr
= (count
> 0xffff) ? 0xffff : count
;
3905 env
->icount_decr
.u16
.low
= decr
;
3906 env
->icount_extra
= count
;
3908 ret
= cpu_exec(env
);
3909 #ifdef CONFIG_PROFILER
3910 qemu_time
+= profile_getclock() - ti
;
3913 /* Fold pending instructions back into the
3914 instruction counter, and clear the interrupt flag. */
3915 qemu_icount
-= (env
->icount_decr
.u16
.low
3916 + env
->icount_extra
);
3917 env
->icount_decr
.u32
= 0;
3918 env
->icount_extra
= 0;
3923 static void tcg_cpu_exec(void)
3927 if (next_cpu
== NULL
)
3928 next_cpu
= first_cpu
;
3929 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3930 CPUState
*env
= cur_cpu
= next_cpu
;
3934 if (timer_alarm_pending
) {
3935 timer_alarm_pending
= 0;
3938 if (cpu_can_run(env
))
3939 ret
= qemu_cpu_exec(env
);
3940 if (ret
== EXCP_DEBUG
) {
3941 gdb_set_stop_cpu(env
);
3942 debug_requested
= 1;
3948 static int cpu_has_work(CPUState
*env
)
3956 if (qemu_cpu_has_work(env
))
3961 static int tcg_has_work(void)
3965 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3966 if (cpu_has_work(env
))
3971 static int qemu_calculate_timeout(void)
3973 #ifndef CONFIG_IOTHREAD
3978 else if (tcg_has_work())
3980 else if (!use_icount
)
3983 /* XXX: use timeout computed from timers */
3986 /* Advance virtual time to the next event. */
3987 if (use_icount
== 1) {
3988 /* When not using an adaptive execution frequency
3989 we tend to get badly out of sync with real time,
3990 so just delay for a reasonable amount of time. */
3993 delta
= cpu_get_icount() - cpu_get_clock();
3996 /* If virtual time is ahead of real time then just
3998 timeout
= (delta
/ 1000000) + 1;
4000 /* Wait for either IO to occur or the next
4002 add
= qemu_next_deadline();
4003 /* We advance the timer before checking for IO.
4004 Limit the amount we advance so that early IO
4005 activity won't get the guest too far ahead. */
4009 add
= (add
+ (1 << icount_time_shift
) - 1)
4010 >> icount_time_shift
;
4012 timeout
= delta
/ 1000000;
4019 #else /* CONFIG_IOTHREAD */
4024 static int vm_can_run(void)
4026 if (powerdown_requested
)
4028 if (reset_requested
)
4030 if (shutdown_requested
)
4032 if (debug_requested
)
4037 qemu_irq qemu_system_powerdown
;
4039 static void main_loop(void)
4043 #ifdef CONFIG_IOTHREAD
4044 qemu_system_ready
= 1;
4045 qemu_cond_broadcast(&qemu_system_cond
);
4050 #ifdef CONFIG_PROFILER
4053 #ifndef CONFIG_IOTHREAD
4056 #ifdef CONFIG_PROFILER
4057 ti
= profile_getclock();
4059 main_loop_wait(qemu_calculate_timeout());
4060 #ifdef CONFIG_PROFILER
4061 dev_time
+= profile_getclock() - ti
;
4063 } while (vm_can_run());
4065 if (qemu_debug_requested())
4066 vm_stop(EXCP_DEBUG
);
4067 if (qemu_shutdown_requested()) {
4074 if (qemu_reset_requested()) {
4076 qemu_system_reset();
4079 if (qemu_powerdown_requested()) {
4080 qemu_irq_raise(qemu_system_powerdown
);
4082 if ((r
= qemu_vmstop_requested()))
4088 static void version(void)
4090 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4093 static void help(int exitcode
)
4096 printf("usage: %s [options] [disk_image]\n"
4098 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4100 #define DEF(option, opt_arg, opt_enum, opt_help) \
4102 #define DEFHEADING(text) stringify(text) "\n"
4103 #include "qemu-options.h"
4108 "During emulation, the following keys are useful:\n"
4109 "ctrl-alt-f toggle full screen\n"
4110 "ctrl-alt-n switch to virtual console 'n'\n"
4111 "ctrl-alt toggle mouse and keyboard grab\n"
4113 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4118 DEFAULT_NETWORK_SCRIPT
,
4119 DEFAULT_NETWORK_DOWN_SCRIPT
,
4121 DEFAULT_GDBSTUB_PORT
,
4126 #define HAS_ARG 0x0001
4129 #define DEF(option, opt_arg, opt_enum, opt_help) \
4131 #define DEFHEADING(text)
4132 #include "qemu-options.h"
4138 typedef struct QEMUOption
{
4144 static const QEMUOption qemu_options
[] = {
4145 { "h", 0, QEMU_OPTION_h
},
4146 #define DEF(option, opt_arg, opt_enum, opt_help) \
4147 { option, opt_arg, opt_enum },
4148 #define DEFHEADING(text)
4149 #include "qemu-options.h"
4157 struct soundhw soundhw
[] = {
4158 #ifdef HAS_AUDIO_CHOICE
4159 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4165 { .init_isa
= pcspk_audio_init
}
4172 "Creative Sound Blaster 16",
4175 { .init_isa
= SB16_init
}
4179 #ifdef CONFIG_CS4231A
4185 { .init_isa
= cs4231a_init
}
4193 "Yamaha YMF262 (OPL3)",
4195 "Yamaha YM3812 (OPL2)",
4199 { .init_isa
= Adlib_init
}
4206 "Gravis Ultrasound GF1",
4209 { .init_isa
= GUS_init
}
4216 "Intel 82801AA AC97 Audio",
4219 { .init_pci
= ac97_init
}
4223 #ifdef CONFIG_ES1370
4226 "ENSONIQ AudioPCI ES1370",
4229 { .init_pci
= es1370_init
}
4233 #endif /* HAS_AUDIO_CHOICE */
4235 { NULL
, NULL
, 0, 0, { NULL
} }
4238 static void select_soundhw (const char *optarg
)
4242 if (*optarg
== '?') {
4245 printf ("Valid sound card names (comma separated):\n");
4246 for (c
= soundhw
; c
->name
; ++c
) {
4247 printf ("%-11s %s\n", c
->name
, c
->descr
);
4249 printf ("\n-soundhw all will enable all of the above\n");
4250 exit (*optarg
!= '?');
4258 if (!strcmp (optarg
, "all")) {
4259 for (c
= soundhw
; c
->name
; ++c
) {
4267 e
= strchr (p
, ',');
4268 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4270 for (c
= soundhw
; c
->name
; ++c
) {
4271 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4280 "Unknown sound card name (too big to show)\n");
4283 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4288 p
+= l
+ (e
!= NULL
);
4292 goto show_valid_cards
;
4297 static void select_vgahw (const char *p
)
4301 vga_interface_type
= VGA_NONE
;
4302 if (strstart(p
, "std", &opts
)) {
4303 vga_interface_type
= VGA_STD
;
4304 } else if (strstart(p
, "cirrus", &opts
)) {
4305 vga_interface_type
= VGA_CIRRUS
;
4306 } else if (strstart(p
, "vmware", &opts
)) {
4307 vga_interface_type
= VGA_VMWARE
;
4308 } else if (strstart(p
, "xenfb", &opts
)) {
4309 vga_interface_type
= VGA_XENFB
;
4310 } else if (!strstart(p
, "none", &opts
)) {
4312 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4316 const char *nextopt
;
4318 if (strstart(opts
, ",retrace=", &nextopt
)) {
4320 if (strstart(opts
, "dumb", &nextopt
))
4321 vga_retrace_method
= VGA_RETRACE_DUMB
;
4322 else if (strstart(opts
, "precise", &nextopt
))
4323 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4324 else goto invalid_vga
;
4325 } else goto invalid_vga
;
4331 static int balloon_parse(const char *arg
)
4335 if (strcmp(arg
, "none") == 0) {
4339 if (!strncmp(arg
, "virtio", 6)) {
4340 if (arg
[6] == ',') {
4341 /* have params -> parse them */
4342 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4346 /* create empty opts */
4347 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4349 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4358 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4360 exit(STATUS_CONTROL_C_EXIT
);
4365 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4369 if(strlen(str
) != 36)
4372 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4373 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4374 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4380 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4388 static void termsig_handler(int signal
)
4390 qemu_system_shutdown_request();
4393 static void sigchld_handler(int signal
)
4395 waitpid(-1, NULL
, WNOHANG
);
4398 static void sighandler_setup(void)
4400 struct sigaction act
;
4402 memset(&act
, 0, sizeof(act
));
4403 act
.sa_handler
= termsig_handler
;
4404 sigaction(SIGINT
, &act
, NULL
);
4405 sigaction(SIGHUP
, &act
, NULL
);
4406 sigaction(SIGTERM
, &act
, NULL
);
4408 act
.sa_handler
= sigchld_handler
;
4409 act
.sa_flags
= SA_NOCLDSTOP
;
4410 sigaction(SIGCHLD
, &act
, NULL
);
4416 /* Look for support files in the same directory as the executable. */
4417 static char *find_datadir(const char *argv0
)
4423 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4430 while (p
!= buf
&& *p
!= '\\')
4433 if (access(buf
, R_OK
) == 0) {
4434 return qemu_strdup(buf
);
4440 /* Find a likely location for support files using the location of the binary.
4441 For installed binaries this will be "$bindir/../share/qemu". When
4442 running from the build tree this will be "$bindir/../pc-bios". */
4443 #define SHARE_SUFFIX "/share/qemu"
4444 #define BUILD_SUFFIX "/pc-bios"
4445 static char *find_datadir(const char *argv0
)
4453 #if defined(__linux__)
4456 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4462 #elif defined(__FreeBSD__)
4465 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4472 /* If we don't have any way of figuring out the actual executable
4473 location then try argv[0]. */
4475 p
= realpath(argv0
, buf
);
4483 max_len
= strlen(dir
) +
4484 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4485 res
= qemu_mallocz(max_len
);
4486 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4487 if (access(res
, R_OK
)) {
4488 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4489 if (access(res
, R_OK
)) {
4501 char *qemu_find_file(int type
, const char *name
)
4507 /* If name contains path separators then try it as a straight path. */
4508 if ((strchr(name
, '/') || strchr(name
, '\\'))
4509 && access(name
, R_OK
) == 0) {
4510 return qemu_strdup(name
);
4513 case QEMU_FILE_TYPE_BIOS
:
4516 case QEMU_FILE_TYPE_KEYMAP
:
4517 subdir
= "keymaps/";
4522 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4523 buf
= qemu_mallocz(len
);
4524 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4525 if (access(buf
, R_OK
)) {
4532 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4536 dev
= qdev_device_add(opts
);
4542 struct device_config
{
4544 DEV_USB
, /* -usbdevice */
4547 const char *cmdline
;
4548 QTAILQ_ENTRY(device_config
) next
;
4550 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4552 static void add_device_config(int type
, const char *cmdline
)
4554 struct device_config
*conf
;
4556 conf
= qemu_mallocz(sizeof(*conf
));
4558 conf
->cmdline
= cmdline
;
4559 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4562 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4564 struct device_config
*conf
;
4567 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4568 if (conf
->type
!= type
)
4570 rc
= func(conf
->cmdline
);
4577 int main(int argc
, char **argv
, char **envp
)
4579 const char *gdbstub_dev
= NULL
;
4580 uint32_t boot_devices_bitmap
= 0;
4582 int snapshot
, linux_boot
, net_boot
;
4583 const char *initrd_filename
;
4584 const char *kernel_filename
, *kernel_cmdline
;
4585 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4587 DisplayChangeListener
*dcl
;
4588 int cyls
, heads
, secs
, translation
;
4589 QemuOpts
*hda_opts
= NULL
, *opts
;
4591 const char *r
, *optarg
;
4592 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4593 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4594 int monitor_device_index
;
4595 const char *serial_devices
[MAX_SERIAL_PORTS
];
4596 int serial_device_index
;
4597 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4598 int parallel_device_index
;
4599 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4600 int virtio_console_index
;
4601 const char *loadvm
= NULL
;
4602 QEMUMachine
*machine
;
4603 const char *cpu_model
;
4608 const char *pid_file
= NULL
;
4609 const char *incoming
= NULL
;
4612 struct passwd
*pwd
= NULL
;
4613 const char *chroot_dir
= NULL
;
4614 const char *run_as
= NULL
;
4617 int show_vnc_port
= 0;
4621 qemu_errors_to_file(stderr
);
4622 qemu_cache_utils_init(envp
);
4624 QLIST_INIT (&vm_change_state_head
);
4627 struct sigaction act
;
4628 sigfillset(&act
.sa_mask
);
4630 act
.sa_handler
= SIG_IGN
;
4631 sigaction(SIGPIPE
, &act
, NULL
);
4634 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4635 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4636 QEMU to run on a single CPU */
4641 h
= GetCurrentProcess();
4642 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4643 for(i
= 0; i
< 32; i
++) {
4644 if (mask
& (1 << i
))
4649 SetProcessAffinityMask(h
, mask
);
4655 module_call_init(MODULE_INIT_MACHINE
);
4656 machine
= find_default_machine();
4658 initrd_filename
= NULL
;
4661 kernel_filename
= NULL
;
4662 kernel_cmdline
= "";
4663 cyls
= heads
= secs
= 0;
4664 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4666 serial_devices
[0] = "vc:80Cx24C";
4667 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4668 serial_devices
[i
] = NULL
;
4669 serial_device_index
= 0;
4671 parallel_devices
[0] = "vc:80Cx24C";
4672 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4673 parallel_devices
[i
] = NULL
;
4674 parallel_device_index
= 0;
4676 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4677 virtio_consoles
[i
] = NULL
;
4678 virtio_console_index
= 0;
4680 monitor_devices
[0] = "vc:80Cx24C";
4681 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4682 monitor_devices
[i
] = NULL
;
4684 monitor_device_index
= 0;
4686 for (i
= 0; i
< MAX_NODES
; i
++) {
4688 node_cpumask
[i
] = 0;
4703 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4705 const QEMUOption
*popt
;
4708 /* Treat --foo the same as -foo. */
4711 popt
= qemu_options
;
4714 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4718 if (!strcmp(popt
->name
, r
+ 1))
4722 if (popt
->flags
& HAS_ARG
) {
4723 if (optind
>= argc
) {
4724 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4728 optarg
= argv
[optind
++];
4733 switch(popt
->index
) {
4735 machine
= find_machine(optarg
);
4738 printf("Supported machines are:\n");
4739 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4741 printf("%-10s %s (alias of %s)\n",
4742 m
->alias
, m
->desc
, m
->name
);
4743 printf("%-10s %s%s\n",
4745 m
->is_default
? " (default)" : "");
4747 exit(*optarg
!= '?');
4750 case QEMU_OPTION_cpu
:
4751 /* hw initialization will check this */
4752 if (*optarg
== '?') {
4753 /* XXX: implement xxx_cpu_list for targets that still miss it */
4754 #if defined(cpu_list)
4755 cpu_list(stdout
, &fprintf
);
4762 case QEMU_OPTION_initrd
:
4763 initrd_filename
= optarg
;
4765 case QEMU_OPTION_hda
:
4767 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4769 hda_opts
= drive_add(optarg
, HD_ALIAS
4770 ",cyls=%d,heads=%d,secs=%d%s",
4771 0, cyls
, heads
, secs
,
4772 translation
== BIOS_ATA_TRANSLATION_LBA
?
4774 translation
== BIOS_ATA_TRANSLATION_NONE
?
4775 ",trans=none" : "");
4777 case QEMU_OPTION_hdb
:
4778 case QEMU_OPTION_hdc
:
4779 case QEMU_OPTION_hdd
:
4780 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4782 case QEMU_OPTION_drive
:
4783 drive_add(NULL
, "%s", optarg
);
4785 case QEMU_OPTION_set
:
4786 if (qemu_set_option(optarg
) != 0)
4789 case QEMU_OPTION_mtdblock
:
4790 drive_add(optarg
, MTD_ALIAS
);
4792 case QEMU_OPTION_sd
:
4793 drive_add(optarg
, SD_ALIAS
);
4795 case QEMU_OPTION_pflash
:
4796 drive_add(optarg
, PFLASH_ALIAS
);
4798 case QEMU_OPTION_snapshot
:
4801 case QEMU_OPTION_hdachs
:
4805 cyls
= strtol(p
, (char **)&p
, 0);
4806 if (cyls
< 1 || cyls
> 16383)
4811 heads
= strtol(p
, (char **)&p
, 0);
4812 if (heads
< 1 || heads
> 16)
4817 secs
= strtol(p
, (char **)&p
, 0);
4818 if (secs
< 1 || secs
> 63)
4822 if (!strcmp(p
, "none"))
4823 translation
= BIOS_ATA_TRANSLATION_NONE
;
4824 else if (!strcmp(p
, "lba"))
4825 translation
= BIOS_ATA_TRANSLATION_LBA
;
4826 else if (!strcmp(p
, "auto"))
4827 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4830 } else if (*p
!= '\0') {
4832 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4835 if (hda_opts
!= NULL
) {
4837 snprintf(num
, sizeof(num
), "%d", cyls
);
4838 qemu_opt_set(hda_opts
, "cyls", num
);
4839 snprintf(num
, sizeof(num
), "%d", heads
);
4840 qemu_opt_set(hda_opts
, "heads", num
);
4841 snprintf(num
, sizeof(num
), "%d", secs
);
4842 qemu_opt_set(hda_opts
, "secs", num
);
4843 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4844 qemu_opt_set(hda_opts
, "trans", "lba");
4845 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4846 qemu_opt_set(hda_opts
, "trans", "none");
4850 case QEMU_OPTION_numa
:
4851 if (nb_numa_nodes
>= MAX_NODES
) {
4852 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4857 case QEMU_OPTION_nographic
:
4858 display_type
= DT_NOGRAPHIC
;
4860 #ifdef CONFIG_CURSES
4861 case QEMU_OPTION_curses
:
4862 display_type
= DT_CURSES
;
4865 case QEMU_OPTION_portrait
:
4868 case QEMU_OPTION_kernel
:
4869 kernel_filename
= optarg
;
4871 case QEMU_OPTION_append
:
4872 kernel_cmdline
= optarg
;
4874 case QEMU_OPTION_cdrom
:
4875 drive_add(optarg
, CDROM_ALIAS
);
4877 case QEMU_OPTION_boot
:
4879 static const char * const params
[] = {
4880 "order", "once", "menu", NULL
4882 char buf
[sizeof(boot_devices
)];
4883 char *standard_boot_devices
;
4886 if (!strchr(optarg
, '=')) {
4888 pstrcpy(buf
, sizeof(buf
), optarg
);
4889 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4891 "qemu: unknown boot parameter '%s' in '%s'\n",
4897 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4898 boot_devices_bitmap
= parse_bootdevices(buf
);
4899 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4902 if (get_param_value(buf
, sizeof(buf
),
4904 boot_devices_bitmap
|= parse_bootdevices(buf
);
4905 standard_boot_devices
= qemu_strdup(boot_devices
);
4906 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4907 qemu_register_reset(restore_boot_devices
,
4908 standard_boot_devices
);
4910 if (get_param_value(buf
, sizeof(buf
),
4912 if (!strcmp(buf
, "on")) {
4914 } else if (!strcmp(buf
, "off")) {
4918 "qemu: invalid option value '%s'\n",
4926 case QEMU_OPTION_fda
:
4927 case QEMU_OPTION_fdb
:
4928 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
4931 case QEMU_OPTION_no_fd_bootchk
:
4935 case QEMU_OPTION_netdev
:
4936 if (net_client_parse(&qemu_netdev_opts
, optarg
) == -1) {
4940 case QEMU_OPTION_net
:
4941 if (net_client_parse(&qemu_net_opts
, optarg
) == -1) {
4946 case QEMU_OPTION_tftp
:
4947 legacy_tftp_prefix
= optarg
;
4949 case QEMU_OPTION_bootp
:
4950 legacy_bootp_filename
= optarg
;
4953 case QEMU_OPTION_smb
:
4954 if (net_slirp_smb(optarg
) < 0)
4958 case QEMU_OPTION_redir
:
4959 if (net_slirp_redir(optarg
) < 0)
4963 case QEMU_OPTION_bt
:
4964 add_device_config(DEV_BT
, optarg
);
4967 case QEMU_OPTION_audio_help
:
4971 case QEMU_OPTION_soundhw
:
4972 select_soundhw (optarg
);
4978 case QEMU_OPTION_version
:
4982 case QEMU_OPTION_m
: {
4986 value
= strtoul(optarg
, &ptr
, 10);
4988 case 0: case 'M': case 'm':
4995 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
4999 /* On 32-bit hosts, QEMU is limited by virtual address space */
5000 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5001 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5004 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5005 fprintf(stderr
, "qemu: ram size too large\n");
5014 const CPULogItem
*item
;
5016 mask
= cpu_str_to_log_mask(optarg
);
5018 printf("Log items (comma separated):\n");
5019 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5020 printf("%-10s %s\n", item
->name
, item
->help
);
5028 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5030 case QEMU_OPTION_gdb
:
5031 gdbstub_dev
= optarg
;
5036 case QEMU_OPTION_bios
:
5039 case QEMU_OPTION_singlestep
:
5047 keyboard_layout
= optarg
;
5050 case QEMU_OPTION_localtime
:
5053 case QEMU_OPTION_vga
:
5054 select_vgahw (optarg
);
5056 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5062 w
= strtol(p
, (char **)&p
, 10);
5065 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5071 h
= strtol(p
, (char **)&p
, 10);
5076 depth
= strtol(p
, (char **)&p
, 10);
5077 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5078 depth
!= 24 && depth
!= 32)
5080 } else if (*p
== '\0') {
5081 depth
= graphic_depth
;
5088 graphic_depth
= depth
;
5092 case QEMU_OPTION_echr
:
5095 term_escape_char
= strtol(optarg
, &r
, 0);
5097 printf("Bad argument to echr\n");
5100 case QEMU_OPTION_monitor
:
5101 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5102 fprintf(stderr
, "qemu: too many monitor devices\n");
5105 monitor_devices
[monitor_device_index
] = optarg
;
5106 monitor_device_index
++;
5108 case QEMU_OPTION_chardev
:
5109 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5111 fprintf(stderr
, "parse error: %s\n", optarg
);
5114 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5118 case QEMU_OPTION_serial
:
5119 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5120 fprintf(stderr
, "qemu: too many serial ports\n");
5123 serial_devices
[serial_device_index
] = optarg
;
5124 serial_device_index
++;
5126 case QEMU_OPTION_watchdog
:
5129 "qemu: only one watchdog option may be given\n");
5134 case QEMU_OPTION_watchdog_action
:
5135 if (select_watchdog_action(optarg
) == -1) {
5136 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5140 case QEMU_OPTION_virtiocon
:
5141 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5142 fprintf(stderr
, "qemu: too many virtio consoles\n");
5145 virtio_consoles
[virtio_console_index
] = optarg
;
5146 virtio_console_index
++;
5148 case QEMU_OPTION_parallel
:
5149 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5150 fprintf(stderr
, "qemu: too many parallel ports\n");
5153 parallel_devices
[parallel_device_index
] = optarg
;
5154 parallel_device_index
++;
5156 case QEMU_OPTION_loadvm
:
5159 case QEMU_OPTION_full_screen
:
5163 case QEMU_OPTION_no_frame
:
5166 case QEMU_OPTION_alt_grab
:
5169 case QEMU_OPTION_ctrl_grab
:
5172 case QEMU_OPTION_no_quit
:
5175 case QEMU_OPTION_sdl
:
5176 display_type
= DT_SDL
;
5179 case QEMU_OPTION_pidfile
:
5183 case QEMU_OPTION_win2k_hack
:
5184 win2k_install_hack
= 1;
5186 case QEMU_OPTION_rtc_td_hack
:
5189 case QEMU_OPTION_acpitable
:
5190 if(acpi_table_add(optarg
) < 0) {
5191 fprintf(stderr
, "Wrong acpi table provided\n");
5195 case QEMU_OPTION_smbios
:
5196 if(smbios_entry_add(optarg
) < 0) {
5197 fprintf(stderr
, "Wrong smbios provided\n");
5203 case QEMU_OPTION_enable_kvm
:
5207 case QEMU_OPTION_usb
:
5210 case QEMU_OPTION_usbdevice
:
5212 add_device_config(DEV_USB
, optarg
);
5214 case QEMU_OPTION_device
:
5215 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5219 case QEMU_OPTION_smp
:
5222 fprintf(stderr
, "Invalid number of CPUs\n");
5225 if (max_cpus
< smp_cpus
) {
5226 fprintf(stderr
, "maxcpus must be equal to or greater than "
5230 if (max_cpus
> 255) {
5231 fprintf(stderr
, "Unsupported number of maxcpus\n");
5235 case QEMU_OPTION_vnc
:
5236 display_type
= DT_VNC
;
5237 vnc_display
= optarg
;
5240 case QEMU_OPTION_no_acpi
:
5243 case QEMU_OPTION_no_hpet
:
5246 case QEMU_OPTION_balloon
:
5247 if (balloon_parse(optarg
) < 0) {
5248 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5253 case QEMU_OPTION_no_reboot
:
5256 case QEMU_OPTION_no_shutdown
:
5259 case QEMU_OPTION_show_cursor
:
5262 case QEMU_OPTION_uuid
:
5263 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5264 fprintf(stderr
, "Fail to parse UUID string."
5265 " Wrong format.\n");
5270 case QEMU_OPTION_daemonize
:
5274 case QEMU_OPTION_option_rom
:
5275 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5276 fprintf(stderr
, "Too many option ROMs\n");
5279 option_rom
[nb_option_roms
] = optarg
;
5282 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5283 case QEMU_OPTION_semihosting
:
5284 semihosting_enabled
= 1;
5287 case QEMU_OPTION_name
:
5288 qemu_name
= qemu_strdup(optarg
);
5290 char *p
= strchr(qemu_name
, ',');
5293 if (strncmp(p
, "process=", 8)) {
5294 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5302 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5303 case QEMU_OPTION_prom_env
:
5304 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5305 fprintf(stderr
, "Too many prom variables\n");
5308 prom_envs
[nb_prom_envs
] = optarg
;
5313 case QEMU_OPTION_old_param
:
5317 case QEMU_OPTION_clock
:
5318 configure_alarms(optarg
);
5320 case QEMU_OPTION_startdate
:
5321 configure_rtc_date_offset(optarg
, 1);
5323 case QEMU_OPTION_rtc
:
5324 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5326 fprintf(stderr
, "parse error: %s\n", optarg
);
5329 configure_rtc(opts
);
5331 case QEMU_OPTION_tb_size
:
5332 tb_size
= strtol(optarg
, NULL
, 0);
5336 case QEMU_OPTION_icount
:
5338 if (strcmp(optarg
, "auto") == 0) {
5339 icount_time_shift
= -1;
5341 icount_time_shift
= strtol(optarg
, NULL
, 0);
5344 case QEMU_OPTION_incoming
:
5348 case QEMU_OPTION_chroot
:
5349 chroot_dir
= optarg
;
5351 case QEMU_OPTION_runas
:
5356 case QEMU_OPTION_xen_domid
:
5357 xen_domid
= atoi(optarg
);
5359 case QEMU_OPTION_xen_create
:
5360 xen_mode
= XEN_CREATE
;
5362 case QEMU_OPTION_xen_attach
:
5363 xen_mode
= XEN_ATTACH
;
5366 case QEMU_OPTION_readconfig
:
5369 fp
= fopen(optarg
, "r");
5371 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5374 if (qemu_config_parse(fp
) != 0) {
5380 case QEMU_OPTION_writeconfig
:
5383 if (strcmp(optarg
, "-") == 0) {
5386 fp
= fopen(optarg
, "w");
5388 fprintf(stderr
, "open %s: %s\n", optarg
, strerror(errno
));
5392 qemu_config_write(fp
);
5400 /* If no data_dir is specified then try to find it relative to the
5403 data_dir
= find_datadir(argv
[0]);
5405 /* If all else fails use the install patch specified when building. */
5407 data_dir
= CONFIG_QEMU_SHAREDIR
;
5411 * Default to max_cpus = smp_cpus, in case the user doesn't
5412 * specify a max_cpus value.
5415 max_cpus
= smp_cpus
;
5417 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5418 if (smp_cpus
> machine
->max_cpus
) {
5419 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5420 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5425 if (display_type
== DT_NOGRAPHIC
) {
5426 if (serial_device_index
== 0)
5427 serial_devices
[0] = "stdio";
5428 if (parallel_device_index
== 0)
5429 parallel_devices
[0] = "null";
5430 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5431 monitor_devices
[0] = "stdio";
5439 if (pipe(fds
) == -1)
5450 len
= read(fds
[0], &status
, 1);
5451 if (len
== -1 && (errno
== EINTR
))
5456 else if (status
== 1) {
5457 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5474 signal(SIGTSTP
, SIG_IGN
);
5475 signal(SIGTTOU
, SIG_IGN
);
5476 signal(SIGTTIN
, SIG_IGN
);
5479 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5482 write(fds
[1], &status
, 1);
5484 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5489 if (kvm_enabled()) {
5492 ret
= kvm_init(smp_cpus
);
5494 fprintf(stderr
, "failed to initialize KVM\n");
5499 if (qemu_init_main_loop()) {
5500 fprintf(stderr
, "qemu_init_main_loop failed\n");
5503 linux_boot
= (kernel_filename
!= NULL
);
5505 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5506 fprintf(stderr
, "-append only allowed with -kernel option\n");
5510 if (!linux_boot
&& initrd_filename
!= NULL
) {
5511 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5516 /* Win32 doesn't support line-buffering and requires size >= 2 */
5517 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5520 if (init_timer_alarm() < 0) {
5521 fprintf(stderr
, "could not initialize alarm timer\n");
5524 if (use_icount
&& icount_time_shift
< 0) {
5526 /* 125MIPS seems a reasonable initial guess at the guest speed.
5527 It will be corrected fairly quickly anyway. */
5528 icount_time_shift
= 3;
5529 init_icount_adjust();
5536 if (net_init_clients() < 0) {
5540 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5541 net_set_boot_mask(net_boot
);
5543 /* init the bluetooth world */
5544 if (foreach_device_config(DEV_BT
, bt_parse
))
5547 /* init the memory */
5549 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5551 /* init the dynamic translator */
5552 cpu_exec_init_all(tb_size
* 1024 * 1024);
5554 bdrv_init_with_whitelist();
5558 /* we always create the cdrom drive, even if no disk is there */
5559 drive_add(NULL
, CDROM_ALIAS
);
5561 /* we always create at least one floppy */
5562 drive_add(NULL
, FD_ALIAS
, 0);
5564 /* we always create one sd slot, even if no card is in it */
5565 drive_add(NULL
, SD_ALIAS
);
5567 /* open the virtual block devices */
5569 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5570 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5573 vmstate_register(0, &vmstate_timers
,&timers_state
);
5574 register_savevm_live("ram", 0, 3, NULL
, ram_save_live
, NULL
,
5577 /* Maintain compatibility with multiple stdio monitors */
5578 if (!strcmp(monitor_devices
[0],"stdio")) {
5579 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5580 const char *devname
= serial_devices
[i
];
5581 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5582 monitor_devices
[0] = NULL
;
5584 } else if (devname
&& !strcmp(devname
,"stdio")) {
5585 monitor_devices
[0] = NULL
;
5586 serial_devices
[i
] = "mon:stdio";
5592 if (nb_numa_nodes
> 0) {
5595 if (nb_numa_nodes
> smp_cpus
) {
5596 nb_numa_nodes
= smp_cpus
;
5599 /* If no memory size if given for any node, assume the default case
5600 * and distribute the available memory equally across all nodes
5602 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5603 if (node_mem
[i
] != 0)
5606 if (i
== nb_numa_nodes
) {
5607 uint64_t usedmem
= 0;
5609 /* On Linux, the each node's border has to be 8MB aligned,
5610 * the final node gets the rest.
5612 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5613 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5614 usedmem
+= node_mem
[i
];
5616 node_mem
[i
] = ram_size
- usedmem
;
5619 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5620 if (node_cpumask
[i
] != 0)
5623 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5624 * must cope with this anyway, because there are BIOSes out there in
5625 * real machines which also use this scheme.
5627 if (i
== nb_numa_nodes
) {
5628 for (i
= 0; i
< smp_cpus
; i
++) {
5629 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5634 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5635 const char *devname
= monitor_devices
[i
];
5636 if (devname
&& strcmp(devname
, "none")) {
5639 snprintf(label
, sizeof(label
), "monitor");
5641 snprintf(label
, sizeof(label
), "monitor%d", i
);
5643 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5644 if (!monitor_hds
[i
]) {
5645 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5652 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5653 const char *devname
= serial_devices
[i
];
5654 if (devname
&& strcmp(devname
, "none")) {
5656 snprintf(label
, sizeof(label
), "serial%d", i
);
5657 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5658 if (!serial_hds
[i
]) {
5659 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
5660 devname
, strerror(errno
));
5666 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5667 const char *devname
= parallel_devices
[i
];
5668 if (devname
&& strcmp(devname
, "none")) {
5670 snprintf(label
, sizeof(label
), "parallel%d", i
);
5671 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5672 if (!parallel_hds
[i
]) {
5673 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5674 devname
, strerror(errno
));
5680 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5681 const char *devname
= virtio_consoles
[i
];
5682 if (devname
&& strcmp(devname
, "none")) {
5684 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5685 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5686 if (!virtcon_hds
[i
]) {
5687 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5688 devname
, strerror(errno
));
5694 module_call_init(MODULE_INIT_DEVICE
);
5697 i
= select_watchdog(watchdog
);
5699 exit (i
== 1 ? 1 : 0);
5702 if (machine
->compat_props
) {
5703 qdev_prop_register_compat(machine
->compat_props
);
5705 machine
->init(ram_size
, boot_devices
,
5706 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5710 /* must be after terminal init, SDL library changes signal handlers */
5714 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5715 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5716 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5722 current_machine
= machine
;
5724 /* init USB devices */
5726 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5730 /* init generic devices */
5731 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5735 dumb_display_init();
5736 /* just use the first displaystate for the moment */
5739 if (display_type
== DT_DEFAULT
) {
5740 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5741 display_type
= DT_SDL
;
5743 display_type
= DT_VNC
;
5744 vnc_display
= "localhost:0,to=99";
5750 switch (display_type
) {
5753 #if defined(CONFIG_CURSES)
5755 curses_display_init(ds
, full_screen
);
5758 #if defined(CONFIG_SDL)
5760 sdl_display_init(ds
, full_screen
, no_frame
);
5762 #elif defined(CONFIG_COCOA)
5764 cocoa_display_init(ds
, full_screen
);
5768 vnc_display_init(ds
);
5769 if (vnc_display_open(ds
, vnc_display
) < 0)
5772 if (show_vnc_port
) {
5773 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5781 dcl
= ds
->listeners
;
5782 while (dcl
!= NULL
) {
5783 if (dcl
->dpy_refresh
!= NULL
) {
5784 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5785 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5790 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5791 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5792 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5795 text_consoles_set_display(display_state
);
5797 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5798 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5799 monitor_init(monitor_hds
[i
],
5800 MONITOR_USE_READLINE
|
5801 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5805 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5806 const char *devname
= serial_devices
[i
];
5807 if (devname
&& strcmp(devname
, "none")) {
5808 if (strstart(devname
, "vc", 0))
5809 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5813 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5814 const char *devname
= parallel_devices
[i
];
5815 if (devname
&& strcmp(devname
, "none")) {
5816 if (strstart(devname
, "vc", 0))
5817 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5821 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5822 const char *devname
= virtio_consoles
[i
];
5823 if (virtcon_hds
[i
] && devname
) {
5824 if (strstart(devname
, "vc", 0))
5825 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5829 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5830 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5835 qdev_machine_creation_done();
5839 qemu_system_reset();
5841 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5847 qemu_start_incoming_migration(incoming
);
5848 } else if (autostart
) {
5858 len
= write(fds
[1], &status
, 1);
5859 if (len
== -1 && (errno
== EINTR
))
5866 TFR(fd
= open("/dev/null", O_RDWR
));
5872 pwd
= getpwnam(run_as
);
5874 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5880 if (chroot(chroot_dir
) < 0) {
5881 fprintf(stderr
, "chroot failed\n");
5888 if (setgid(pwd
->pw_gid
) < 0) {
5889 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5892 if (setuid(pwd
->pw_uid
) < 0) {
5893 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5896 if (setuid(0) != -1) {
5897 fprintf(stderr
, "Dropping privileges failed\n");