4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
38 #include <sys/times.h>
42 #include <sys/ioctl.h>
43 #include <sys/resource.h>
44 #include <sys/socket.h>
45 #include <netinet/in.h>
47 #if defined(__NetBSD__)
48 #include <net/if_tap.h>
51 #include <linux/if_tun.h>
53 #include <arpa/inet.h>
56 #include <sys/select.h>
59 #if defined(__FreeBSD__) || defined(__DragonFly__)
64 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
65 #include <freebsd/stdlib.h>
70 #include <linux/rtc.h>
71 #include <sys/prctl.h>
73 /* For the benefit of older linux systems which don't supply it,
74 we use a local copy of hpet.h. */
75 /* #include <linux/hpet.h> */
78 #include <linux/ppdev.h>
79 #include <linux/parport.h>
83 #include <sys/ethernet.h>
84 #include <sys/sockio.h>
85 #include <netinet/arp.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_icmp.h> // must come after ip.h
90 #include <netinet/udp.h>
91 #include <netinet/tcp.h>
95 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
96 discussion about Solaris header problems */
97 extern int madvise(caddr_t
, size_t, int);
102 #if defined(__OpenBSD__)
106 #if defined(CONFIG_VDE)
107 #include <libvdeplug.h>
112 #include <mmsystem.h>
116 #if defined(__APPLE__) || defined(main)
118 int qemu_main(int argc
, char **argv
, char **envp
);
119 int main(int argc
, char **argv
)
121 return qemu_main(argc
, argv
, NULL
);
124 #define main qemu_main
126 #endif /* CONFIG_SDL */
130 #define main qemu_main
131 #endif /* CONFIG_COCOA */
134 #include "hw/boards.h"
136 #include "hw/pcmcia.h"
138 #include "hw/audiodev.h"
142 #include "hw/watchdog.h"
143 #include "hw/smbios.h"
152 #include "qemu-timer.h"
153 #include "qemu-char.h"
154 #include "cache-utils.h"
157 #include "audio/audio.h"
158 #include "migration.h"
161 #include "qemu-option.h"
162 #include "qemu-config.h"
166 #include "exec-all.h"
168 #include "qemu_socket.h"
170 #include "slirp/libslirp.h"
172 #include "qemu-queue.h"
175 //#define DEBUG_SLIRP
177 #define DEFAULT_RAM_SIZE 128
179 /* Maximum number of monitor devices */
180 #define MAX_MONITOR_DEVICES 10
182 static const char *data_dir
;
183 const char *bios_name
= NULL
;
184 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
185 to store the VM snapshots */
186 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
187 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
188 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
189 static DisplayState
*display_state
;
190 DisplayType display_type
= DT_DEFAULT
;
191 const char* keyboard_layout
= NULL
;
194 NICInfo nd_table
[MAX_NICS
];
197 static int rtc_utc
= 1;
198 static int rtc_date_offset
= -1; /* -1 means no change */
199 int vga_interface_type
= VGA_CIRRUS
;
201 int graphic_width
= 1024;
202 int graphic_height
= 768;
203 int graphic_depth
= 8;
205 int graphic_width
= 800;
206 int graphic_height
= 600;
207 int graphic_depth
= 15;
209 static int full_screen
= 0;
211 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
216 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
218 int win2k_install_hack
= 0;
227 const char *vnc_display
;
228 int acpi_enabled
= 1;
234 int graphic_rotate
= 0;
235 uint8_t irq0override
= 1;
239 const char *watchdog
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
248 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
249 unsigned int nb_prom_envs
= 0;
250 const char *prom_envs
[MAX_PROM_ENVS
];
255 uint64_t node_mem
[MAX_NODES
];
256 uint64_t node_cpumask
[MAX_NODES
];
258 static CPUState
*cur_cpu
;
259 static CPUState
*next_cpu
;
260 static int timer_alarm_pending
= 1;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
269 static QEMUTimer
*nographic_timer
;
271 uint8_t qemu_uuid
[16];
273 static QEMUBootSetHandler
*boot_set_handler
;
274 static void *boot_set_opaque
;
276 /***********************************************************/
277 /* x86 ISA bus support */
279 target_phys_addr_t isa_mem_base
= 0;
282 /***********************************************************/
283 void hw_error(const char *fmt
, ...)
289 fprintf(stderr
, "qemu: hardware error: ");
290 vfprintf(stderr
, fmt
, ap
);
291 fprintf(stderr
, "\n");
292 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
293 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
295 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
297 cpu_dump_state(env
, stderr
, fprintf
, 0);
304 static void set_proc_name(const char *s
)
306 #if defined(__linux__) && defined(PR_SET_NAME)
310 name
[sizeof(name
) - 1] = 0;
311 strncpy(name
, s
, sizeof(name
));
312 /* Could rewrite argv[0] too, but that's a bit more complicated.
313 This simple way is enough for `top'. */
314 prctl(PR_SET_NAME
, name
);
321 static QEMUBalloonEvent
*qemu_balloon_event
;
322 void *qemu_balloon_event_opaque
;
324 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
326 qemu_balloon_event
= func
;
327 qemu_balloon_event_opaque
= opaque
;
330 void qemu_balloon(ram_addr_t target
)
332 if (qemu_balloon_event
)
333 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
336 ram_addr_t
qemu_balloon_status(void)
338 if (qemu_balloon_event
)
339 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
343 /***********************************************************/
346 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
347 static void *qemu_put_kbd_event_opaque
;
348 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
349 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
351 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
353 qemu_put_kbd_event_opaque
= opaque
;
354 qemu_put_kbd_event
= func
;
357 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
358 void *opaque
, int absolute
,
361 QEMUPutMouseEntry
*s
, *cursor
;
363 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
365 s
->qemu_put_mouse_event
= func
;
366 s
->qemu_put_mouse_event_opaque
= opaque
;
367 s
->qemu_put_mouse_event_absolute
= absolute
;
368 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
371 if (!qemu_put_mouse_event_head
) {
372 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
376 cursor
= qemu_put_mouse_event_head
;
377 while (cursor
->next
!= NULL
)
378 cursor
= cursor
->next
;
381 qemu_put_mouse_event_current
= s
;
386 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
388 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
390 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
393 cursor
= qemu_put_mouse_event_head
;
394 while (cursor
!= NULL
&& cursor
!= entry
) {
396 cursor
= cursor
->next
;
399 if (cursor
== NULL
) // does not exist or list empty
401 else if (prev
== NULL
) { // entry is head
402 qemu_put_mouse_event_head
= cursor
->next
;
403 if (qemu_put_mouse_event_current
== entry
)
404 qemu_put_mouse_event_current
= cursor
->next
;
405 qemu_free(entry
->qemu_put_mouse_event_name
);
410 prev
->next
= entry
->next
;
412 if (qemu_put_mouse_event_current
== entry
)
413 qemu_put_mouse_event_current
= prev
;
415 qemu_free(entry
->qemu_put_mouse_event_name
);
419 void kbd_put_keycode(int keycode
)
421 if (qemu_put_kbd_event
) {
422 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
426 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
428 QEMUPutMouseEvent
*mouse_event
;
429 void *mouse_event_opaque
;
432 if (!qemu_put_mouse_event_current
) {
437 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
439 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
442 if (graphic_rotate
) {
443 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
446 width
= graphic_width
- 1;
447 mouse_event(mouse_event_opaque
,
448 width
- dy
, dx
, dz
, buttons_state
);
450 mouse_event(mouse_event_opaque
,
451 dx
, dy
, dz
, buttons_state
);
455 int kbd_mouse_is_absolute(void)
457 if (!qemu_put_mouse_event_current
)
460 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
463 void do_info_mice(Monitor
*mon
)
465 QEMUPutMouseEntry
*cursor
;
468 if (!qemu_put_mouse_event_head
) {
469 monitor_printf(mon
, "No mouse devices connected\n");
473 monitor_printf(mon
, "Mouse devices available:\n");
474 cursor
= qemu_put_mouse_event_head
;
475 while (cursor
!= NULL
) {
476 monitor_printf(mon
, "%c Mouse #%d: %s\n",
477 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
478 index
, cursor
->qemu_put_mouse_event_name
);
480 cursor
= cursor
->next
;
484 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
486 QEMUPutMouseEntry
*cursor
;
488 int index
= qdict_get_int(qdict
, "index");
490 if (!qemu_put_mouse_event_head
) {
491 monitor_printf(mon
, "No mouse devices connected\n");
495 cursor
= qemu_put_mouse_event_head
;
496 while (cursor
!= NULL
&& index
!= i
) {
498 cursor
= cursor
->next
;
502 qemu_put_mouse_event_current
= cursor
;
504 monitor_printf(mon
, "Mouse at given index not found\n");
507 /* compute with 96 bit intermediate result: (a*b)/c */
508 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
513 #ifdef HOST_WORDS_BIGENDIAN
523 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
524 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
527 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
531 /***********************************************************/
532 /* real time host monotonic timer */
534 static int64_t get_clock_realtime(void)
538 gettimeofday(&tv
, NULL
);
539 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
544 static int64_t clock_freq
;
546 static void init_get_clock(void)
550 ret
= QueryPerformanceFrequency(&freq
);
552 fprintf(stderr
, "Could not calibrate ticks\n");
555 clock_freq
= freq
.QuadPart
;
558 static int64_t get_clock(void)
561 QueryPerformanceCounter(&ti
);
562 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
567 static int use_rt_clock
;
569 static void init_get_clock(void)
572 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
573 || defined(__DragonFly__)
576 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
583 static int64_t get_clock(void)
585 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
586 || defined(__DragonFly__)
589 clock_gettime(CLOCK_MONOTONIC
, &ts
);
590 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
594 /* XXX: using gettimeofday leads to problems if the date
595 changes, so it should be avoided. */
596 return get_clock_realtime();
601 /* Return the virtual CPU time, based on the instruction counter. */
602 static int64_t cpu_get_icount(void)
605 CPUState
*env
= cpu_single_env
;;
606 icount
= qemu_icount
;
609 fprintf(stderr
, "Bad clock read\n");
610 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
612 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
615 /***********************************************************/
616 /* guest cycle counter */
618 typedef struct TimersState
{
619 int64_t cpu_ticks_prev
;
620 int64_t cpu_ticks_offset
;
621 int64_t cpu_clock_offset
;
622 int32_t cpu_ticks_enabled
;
626 TimersState timers_state
;
628 /* return the host CPU cycle counter and handle stop/restart */
629 int64_t cpu_get_ticks(void)
632 return cpu_get_icount();
634 if (!timers_state
.cpu_ticks_enabled
) {
635 return timers_state
.cpu_ticks_offset
;
638 ticks
= cpu_get_real_ticks();
639 if (timers_state
.cpu_ticks_prev
> ticks
) {
640 /* Note: non increasing ticks may happen if the host uses
642 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
644 timers_state
.cpu_ticks_prev
= ticks
;
645 return ticks
+ timers_state
.cpu_ticks_offset
;
649 /* return the host CPU monotonic timer and handle stop/restart */
650 static int64_t cpu_get_clock(void)
653 if (!timers_state
.cpu_ticks_enabled
) {
654 return timers_state
.cpu_clock_offset
;
657 return ti
+ timers_state
.cpu_clock_offset
;
661 /* enable cpu_get_ticks() */
662 void cpu_enable_ticks(void)
664 if (!timers_state
.cpu_ticks_enabled
) {
665 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
666 timers_state
.cpu_clock_offset
-= get_clock();
667 timers_state
.cpu_ticks_enabled
= 1;
671 /* disable cpu_get_ticks() : the clock is stopped. You must not call
672 cpu_get_ticks() after that. */
673 void cpu_disable_ticks(void)
675 if (timers_state
.cpu_ticks_enabled
) {
676 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
677 timers_state
.cpu_clock_offset
= cpu_get_clock();
678 timers_state
.cpu_ticks_enabled
= 0;
682 /***********************************************************/
685 #define QEMU_CLOCK_REALTIME 0
686 #define QEMU_CLOCK_VIRTUAL 1
687 #define QEMU_CLOCK_HOST 2
691 /* XXX: add frequency */
699 struct QEMUTimer
*next
;
702 struct qemu_alarm_timer
{
706 int (*start
)(struct qemu_alarm_timer
*t
);
707 void (*stop
)(struct qemu_alarm_timer
*t
);
708 void (*rearm
)(struct qemu_alarm_timer
*t
);
712 #define ALARM_FLAG_DYNTICKS 0x1
713 #define ALARM_FLAG_EXPIRED 0x2
715 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
717 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
720 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
722 if (!alarm_has_dynticks(t
))
728 /* TODO: MIN_TIMER_REARM_US should be optimized */
729 #define MIN_TIMER_REARM_US 250
731 static struct qemu_alarm_timer
*alarm_timer
;
735 struct qemu_alarm_win32
{
738 } alarm_win32_data
= {0, -1};
740 static int win32_start_timer(struct qemu_alarm_timer
*t
);
741 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
742 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
746 static int unix_start_timer(struct qemu_alarm_timer
*t
);
747 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
751 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
752 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
753 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
755 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
756 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
758 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
759 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
761 #endif /* __linux__ */
765 /* Correlation between real and virtual time is always going to be
766 fairly approximate, so ignore small variation.
767 When the guest is idle real and virtual time will be aligned in
769 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
771 static void icount_adjust(void)
776 static int64_t last_delta
;
777 /* If the VM is not running, then do nothing. */
781 cur_time
= cpu_get_clock();
782 cur_icount
= qemu_get_clock(vm_clock
);
783 delta
= cur_icount
- cur_time
;
784 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
786 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
787 && icount_time_shift
> 0) {
788 /* The guest is getting too far ahead. Slow time down. */
792 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
793 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
794 /* The guest is getting too far behind. Speed time up. */
798 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
801 static void icount_adjust_rt(void * opaque
)
803 qemu_mod_timer(icount_rt_timer
,
804 qemu_get_clock(rt_clock
) + 1000);
808 static void icount_adjust_vm(void * opaque
)
810 qemu_mod_timer(icount_vm_timer
,
811 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
815 static void init_icount_adjust(void)
817 /* Have both realtime and virtual time triggers for speed adjustment.
818 The realtime trigger catches emulated time passing too slowly,
819 the virtual time trigger catches emulated time passing too fast.
820 Realtime triggers occur even when idle, so use them less frequently
822 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
823 qemu_mod_timer(icount_rt_timer
,
824 qemu_get_clock(rt_clock
) + 1000);
825 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
826 qemu_mod_timer(icount_vm_timer
,
827 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
830 static struct qemu_alarm_timer alarm_timers
[] = {
833 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
834 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
835 /* HPET - if available - is preferred */
836 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
837 /* ...otherwise try RTC */
838 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
840 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
842 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
843 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
844 {"win32", 0, win32_start_timer
,
845 win32_stop_timer
, NULL
, &alarm_win32_data
},
850 static void show_available_alarms(void)
854 printf("Available alarm timers, in order of precedence:\n");
855 for (i
= 0; alarm_timers
[i
].name
; i
++)
856 printf("%s\n", alarm_timers
[i
].name
);
859 static void configure_alarms(char const *opt
)
863 int count
= ARRAY_SIZE(alarm_timers
) - 1;
866 struct qemu_alarm_timer tmp
;
868 if (!strcmp(opt
, "?")) {
869 show_available_alarms();
873 arg
= qemu_strdup(opt
);
875 /* Reorder the array */
876 name
= strtok(arg
, ",");
878 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
879 if (!strcmp(alarm_timers
[i
].name
, name
))
884 fprintf(stderr
, "Unknown clock %s\n", name
);
893 tmp
= alarm_timers
[i
];
894 alarm_timers
[i
] = alarm_timers
[cur
];
895 alarm_timers
[cur
] = tmp
;
899 name
= strtok(NULL
, ",");
905 /* Disable remaining timers */
906 for (i
= cur
; i
< count
; i
++)
907 alarm_timers
[i
].name
= NULL
;
909 show_available_alarms();
914 #define QEMU_NUM_CLOCKS 3
918 QEMUClock
*host_clock
;
920 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
922 static QEMUClock
*qemu_new_clock(int type
)
925 clock
= qemu_mallocz(sizeof(QEMUClock
));
930 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
934 ts
= qemu_mallocz(sizeof(QEMUTimer
));
941 void qemu_free_timer(QEMUTimer
*ts
)
946 /* stop a timer, but do not dealloc it */
947 void qemu_del_timer(QEMUTimer
*ts
)
951 /* NOTE: this code must be signal safe because
952 qemu_timer_expired() can be called from a signal. */
953 pt
= &active_timers
[ts
->clock
->type
];
966 /* modify the current timer so that it will be fired when current_time
967 >= expire_time. The corresponding callback will be called. */
968 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
974 /* add the timer in the sorted list */
975 /* NOTE: this code must be signal safe because
976 qemu_timer_expired() can be called from a signal. */
977 pt
= &active_timers
[ts
->clock
->type
];
982 if (t
->expire_time
> expire_time
)
986 ts
->expire_time
= expire_time
;
990 /* Rearm if necessary */
991 if (pt
== &active_timers
[ts
->clock
->type
]) {
992 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
993 qemu_rearm_alarm_timer(alarm_timer
);
995 /* Interrupt execution to force deadline recalculation. */
1001 int qemu_timer_pending(QEMUTimer
*ts
)
1004 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1011 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1015 return (timer_head
->expire_time
<= current_time
);
1018 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1024 if (!ts
|| ts
->expire_time
> current_time
)
1026 /* remove timer from the list before calling the callback */
1027 *ptimer_head
= ts
->next
;
1030 /* run the callback (the timer list can be modified) */
1035 int64_t qemu_get_clock(QEMUClock
*clock
)
1037 switch(clock
->type
) {
1038 case QEMU_CLOCK_REALTIME
:
1039 return get_clock() / 1000000;
1041 case QEMU_CLOCK_VIRTUAL
:
1043 return cpu_get_icount();
1045 return cpu_get_clock();
1047 case QEMU_CLOCK_HOST
:
1048 return get_clock_realtime();
1052 static void init_clocks(void)
1055 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1056 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1057 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1061 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1063 uint64_t expire_time
;
1065 if (qemu_timer_pending(ts
)) {
1066 expire_time
= ts
->expire_time
;
1070 qemu_put_be64(f
, expire_time
);
1073 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1075 uint64_t expire_time
;
1077 expire_time
= qemu_get_be64(f
);
1078 if (expire_time
!= -1) {
1079 qemu_mod_timer(ts
, expire_time
);
1085 static const VMStateDescription vmstate_timers
= {
1088 .minimum_version_id
= 1,
1089 .minimum_version_id_old
= 1,
1090 .fields
= (VMStateField
[]) {
1091 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1092 VMSTATE_INT64(dummy
, TimersState
),
1093 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1094 VMSTATE_END_OF_LIST()
1098 static void qemu_event_increment(void);
1101 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1102 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1105 static void host_alarm_handler(int host_signum
)
1109 #define DISP_FREQ 1000
1111 static int64_t delta_min
= INT64_MAX
;
1112 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1114 ti
= qemu_get_clock(vm_clock
);
1115 if (last_clock
!= 0) {
1116 delta
= ti
- last_clock
;
1117 if (delta
< delta_min
)
1119 if (delta
> delta_max
)
1122 if (++count
== DISP_FREQ
) {
1123 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1124 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1125 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1126 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1127 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1129 delta_min
= INT64_MAX
;
1137 if (alarm_has_dynticks(alarm_timer
) ||
1139 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1140 qemu_get_clock(vm_clock
))) ||
1141 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1142 qemu_get_clock(rt_clock
)) ||
1143 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1144 qemu_get_clock(host_clock
))) {
1145 qemu_event_increment();
1146 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1148 #ifndef CONFIG_IOTHREAD
1150 /* stop the currently executing cpu because a timer occured */
1154 timer_alarm_pending
= 1;
1155 qemu_notify_event();
1159 static int64_t qemu_next_deadline(void)
1161 /* To avoid problems with overflow limit this to 2^32. */
1162 int64_t delta
= INT32_MAX
;
1164 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1165 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1166 qemu_get_clock(vm_clock
);
1168 if (active_timers
[QEMU_CLOCK_HOST
]) {
1169 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1170 qemu_get_clock(host_clock
);
1181 #if defined(__linux__)
1182 static uint64_t qemu_next_deadline_dyntick(void)
1190 delta
= (qemu_next_deadline() + 999) / 1000;
1192 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1193 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1194 qemu_get_clock(rt_clock
))*1000;
1195 if (rtdelta
< delta
)
1199 if (delta
< MIN_TIMER_REARM_US
)
1200 delta
= MIN_TIMER_REARM_US
;
1208 /* Sets a specific flag */
1209 static int fcntl_setfl(int fd
, int flag
)
1213 flags
= fcntl(fd
, F_GETFL
);
1217 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1223 #if defined(__linux__)
1225 #define RTC_FREQ 1024
1227 static void enable_sigio_timer(int fd
)
1229 struct sigaction act
;
1232 sigfillset(&act
.sa_mask
);
1234 act
.sa_handler
= host_alarm_handler
;
1236 sigaction(SIGIO
, &act
, NULL
);
1237 fcntl_setfl(fd
, O_ASYNC
);
1238 fcntl(fd
, F_SETOWN
, getpid());
1241 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1243 struct hpet_info info
;
1246 fd
= open("/dev/hpet", O_RDONLY
);
1251 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1253 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1254 "error, but for better emulation accuracy type:\n"
1255 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1259 /* Check capabilities */
1260 r
= ioctl(fd
, HPET_INFO
, &info
);
1264 /* Enable periodic mode */
1265 r
= ioctl(fd
, HPET_EPI
, 0);
1266 if (info
.hi_flags
&& (r
< 0))
1269 /* Enable interrupt */
1270 r
= ioctl(fd
, HPET_IE_ON
, 0);
1274 enable_sigio_timer(fd
);
1275 t
->priv
= (void *)(long)fd
;
1283 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1285 int fd
= (long)t
->priv
;
1290 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1293 unsigned long current_rtc_freq
= 0;
1295 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1298 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1299 if (current_rtc_freq
!= RTC_FREQ
&&
1300 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1301 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1302 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1303 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1306 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1312 enable_sigio_timer(rtc_fd
);
1314 t
->priv
= (void *)(long)rtc_fd
;
1319 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1321 int rtc_fd
= (long)t
->priv
;
1326 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1330 struct sigaction act
;
1332 sigfillset(&act
.sa_mask
);
1334 act
.sa_handler
= host_alarm_handler
;
1336 sigaction(SIGALRM
, &act
, NULL
);
1339 * Initialize ev struct to 0 to avoid valgrind complaining
1340 * about uninitialized data in timer_create call
1342 memset(&ev
, 0, sizeof(ev
));
1343 ev
.sigev_value
.sival_int
= 0;
1344 ev
.sigev_notify
= SIGEV_SIGNAL
;
1345 ev
.sigev_signo
= SIGALRM
;
1347 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1348 perror("timer_create");
1350 /* disable dynticks */
1351 fprintf(stderr
, "Dynamic Ticks disabled\n");
1356 t
->priv
= (void *)(long)host_timer
;
1361 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1363 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1365 timer_delete(host_timer
);
1368 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1370 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1371 struct itimerspec timeout
;
1372 int64_t nearest_delta_us
= INT64_MAX
;
1375 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1376 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1377 !active_timers
[QEMU_CLOCK_HOST
])
1380 nearest_delta_us
= qemu_next_deadline_dyntick();
1382 /* check whether a timer is already running */
1383 if (timer_gettime(host_timer
, &timeout
)) {
1385 fprintf(stderr
, "Internal timer error: aborting\n");
1388 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1389 if (current_us
&& current_us
<= nearest_delta_us
)
1392 timeout
.it_interval
.tv_sec
= 0;
1393 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1394 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1395 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1396 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1398 fprintf(stderr
, "Internal timer error: aborting\n");
1403 #endif /* defined(__linux__) */
1405 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1407 struct sigaction act
;
1408 struct itimerval itv
;
1412 sigfillset(&act
.sa_mask
);
1414 act
.sa_handler
= host_alarm_handler
;
1416 sigaction(SIGALRM
, &act
, NULL
);
1418 itv
.it_interval
.tv_sec
= 0;
1419 /* for i386 kernel 2.6 to get 1 ms */
1420 itv
.it_interval
.tv_usec
= 999;
1421 itv
.it_value
.tv_sec
= 0;
1422 itv
.it_value
.tv_usec
= 10 * 1000;
1424 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1431 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1433 struct itimerval itv
;
1435 memset(&itv
, 0, sizeof(itv
));
1436 setitimer(ITIMER_REAL
, &itv
, NULL
);
1439 #endif /* !defined(_WIN32) */
1444 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1447 struct qemu_alarm_win32
*data
= t
->priv
;
1450 memset(&tc
, 0, sizeof(tc
));
1451 timeGetDevCaps(&tc
, sizeof(tc
));
1453 if (data
->period
< tc
.wPeriodMin
)
1454 data
->period
= tc
.wPeriodMin
;
1456 timeBeginPeriod(data
->period
);
1458 flags
= TIME_CALLBACK_FUNCTION
;
1459 if (alarm_has_dynticks(t
))
1460 flags
|= TIME_ONESHOT
;
1462 flags
|= TIME_PERIODIC
;
1464 data
->timerId
= timeSetEvent(1, // interval (ms)
1465 data
->period
, // resolution
1466 host_alarm_handler
, // function
1467 (DWORD
)t
, // parameter
1470 if (!data
->timerId
) {
1471 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1473 timeEndPeriod(data
->period
);
1480 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1482 struct qemu_alarm_win32
*data
= t
->priv
;
1484 timeKillEvent(data
->timerId
);
1485 timeEndPeriod(data
->period
);
1488 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1490 struct qemu_alarm_win32
*data
= t
->priv
;
1492 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1493 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1494 !active_timers
[QEMU_CLOCK_HOST
])
1497 timeKillEvent(data
->timerId
);
1499 data
->timerId
= timeSetEvent(1,
1503 TIME_ONESHOT
| TIME_PERIODIC
);
1505 if (!data
->timerId
) {
1506 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1509 timeEndPeriod(data
->period
);
1516 static int init_timer_alarm(void)
1518 struct qemu_alarm_timer
*t
= NULL
;
1521 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1522 t
= &alarm_timers
[i
];
1542 static void quit_timers(void)
1544 alarm_timer
->stop(alarm_timer
);
1548 /***********************************************************/
1549 /* host time/date access */
1550 void qemu_get_timedate(struct tm
*tm
, int offset
)
1557 if (rtc_date_offset
== -1) {
1561 ret
= localtime(&ti
);
1563 ti
-= rtc_date_offset
;
1567 memcpy(tm
, ret
, sizeof(struct tm
));
1570 int qemu_timedate_diff(struct tm
*tm
)
1574 if (rtc_date_offset
== -1)
1576 seconds
= mktimegm(tm
);
1578 seconds
= mktime(tm
);
1580 seconds
= mktimegm(tm
) + rtc_date_offset
;
1582 return seconds
- time(NULL
);
1586 static void socket_cleanup(void)
1591 static int socket_init(void)
1596 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1598 err
= WSAGetLastError();
1599 fprintf(stderr
, "WSAStartup: %d\n", err
);
1602 atexit(socket_cleanup
);
1607 /***********************************************************/
1608 /* Bluetooth support */
1611 static struct HCIInfo
*hci_table
[MAX_NICS
];
1613 static struct bt_vlan_s
{
1614 struct bt_scatternet_s net
;
1616 struct bt_vlan_s
*next
;
1619 /* find or alloc a new bluetooth "VLAN" */
1620 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1622 struct bt_vlan_s
**pvlan
, *vlan
;
1623 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1627 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1629 pvlan
= &first_bt_vlan
;
1630 while (*pvlan
!= NULL
)
1631 pvlan
= &(*pvlan
)->next
;
1636 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1640 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1645 static struct HCIInfo null_hci
= {
1646 .cmd_send
= null_hci_send
,
1647 .sco_send
= null_hci_send
,
1648 .acl_send
= null_hci_send
,
1649 .bdaddr_set
= null_hci_addr_set
,
1652 struct HCIInfo
*qemu_next_hci(void)
1654 if (cur_hci
== nb_hcis
)
1657 return hci_table
[cur_hci
++];
1660 static struct HCIInfo
*hci_init(const char *str
)
1663 struct bt_scatternet_s
*vlan
= 0;
1665 if (!strcmp(str
, "null"))
1668 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1670 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1671 else if (!strncmp(str
, "hci", 3)) {
1674 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1675 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1680 vlan
= qemu_find_bt_vlan(0);
1682 return bt_new_hci(vlan
);
1685 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1690 static int bt_hci_parse(const char *str
)
1692 struct HCIInfo
*hci
;
1695 if (nb_hcis
>= MAX_NICS
) {
1696 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1700 hci
= hci_init(str
);
1709 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1710 hci
->bdaddr_set(hci
, bdaddr
.b
);
1712 hci_table
[nb_hcis
++] = hci
;
1717 static void bt_vhci_add(int vlan_id
)
1719 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1722 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1723 "an empty scatternet %i\n", vlan_id
);
1725 bt_vhci_init(bt_new_hci(vlan
));
1728 static struct bt_device_s
*bt_device_add(const char *opt
)
1730 struct bt_scatternet_s
*vlan
;
1732 char *endp
= strstr(opt
, ",vlan=");
1733 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1736 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1739 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1741 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1746 vlan
= qemu_find_bt_vlan(vlan_id
);
1749 fprintf(stderr
, "qemu: warning: adding a slave device to "
1750 "an empty scatternet %i\n", vlan_id
);
1752 if (!strcmp(devname
, "keyboard"))
1753 return bt_keyboard_init(vlan
);
1755 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1759 static int bt_parse(const char *opt
)
1761 const char *endp
, *p
;
1764 if (strstart(opt
, "hci", &endp
)) {
1765 if (!*endp
|| *endp
== ',') {
1767 if (!strstart(endp
, ",vlan=", 0))
1770 return bt_hci_parse(opt
);
1772 } else if (strstart(opt
, "vhci", &endp
)) {
1773 if (!*endp
|| *endp
== ',') {
1775 if (strstart(endp
, ",vlan=", &p
)) {
1776 vlan
= strtol(p
, (char **) &endp
, 0);
1778 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1782 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1791 } else if (strstart(opt
, "device:", &endp
))
1792 return !bt_device_add(endp
);
1794 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1798 /***********************************************************/
1799 /* QEMU Block devices */
1801 #define HD_ALIAS "index=%d,media=disk"
1802 #define CDROM_ALIAS "index=2,media=cdrom"
1803 #define FD_ALIAS "index=%d,if=floppy"
1804 #define PFLASH_ALIAS "if=pflash"
1805 #define MTD_ALIAS "if=mtd"
1806 #define SD_ALIAS "index=0,if=sd"
1808 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1815 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1818 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1820 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1821 __FUNCTION__
, optstr
);
1825 qemu_opt_set(opts
, "file", file
);
1829 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1833 /* seek interface, bus and unit */
1835 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1836 if (dinfo
->type
== type
&&
1837 dinfo
->bus
== bus
&&
1838 dinfo
->unit
== unit
)
1845 DriveInfo
*drive_get_by_id(const char *id
)
1849 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1850 if (strcmp(id
, dinfo
->id
))
1857 int drive_get_max_bus(BlockInterfaceType type
)
1863 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1864 if(dinfo
->type
== type
&&
1865 dinfo
->bus
> max_bus
)
1866 max_bus
= dinfo
->bus
;
1871 const char *drive_get_serial(BlockDriverState
*bdrv
)
1875 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1876 if (dinfo
->bdrv
== bdrv
)
1877 return dinfo
->serial
;
1883 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1887 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1888 if (dinfo
->bdrv
== bdrv
)
1889 return dinfo
->onerror
;
1892 return BLOCK_ERR_STOP_ENOSPC
;
1895 static void bdrv_format_print(void *opaque
, const char *name
)
1897 fprintf(stderr
, " %s", name
);
1900 void drive_uninit(BlockDriverState
*bdrv
)
1904 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1905 if (dinfo
->bdrv
!= bdrv
)
1907 qemu_opts_del(dinfo
->opts
);
1908 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1914 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1918 const char *file
= NULL
;
1921 const char *mediastr
= "";
1922 BlockInterfaceType type
;
1923 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1924 int bus_id
, unit_id
;
1925 int cyls
, heads
, secs
, translation
;
1926 BlockDriver
*drv
= NULL
;
1927 QEMUMachine
*machine
= opaque
;
1932 int bdrv_flags
, onerror
;
1933 const char *devaddr
;
1939 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1942 if (machine
&& machine
->use_scsi
) {
1944 max_devs
= MAX_SCSI_DEVS
;
1945 pstrcpy(devname
, sizeof(devname
), "scsi");
1948 max_devs
= MAX_IDE_DEVS
;
1949 pstrcpy(devname
, sizeof(devname
), "ide");
1953 /* extract parameters */
1954 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1955 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1956 index
= qemu_opt_get_number(opts
, "index", -1);
1958 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1959 heads
= qemu_opt_get_number(opts
, "heads", 0);
1960 secs
= qemu_opt_get_number(opts
, "secs", 0);
1962 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1964 file
= qemu_opt_get(opts
, "file");
1965 serial
= qemu_opt_get(opts
, "serial");
1967 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1968 pstrcpy(devname
, sizeof(devname
), buf
);
1969 if (!strcmp(buf
, "ide")) {
1971 max_devs
= MAX_IDE_DEVS
;
1972 } else if (!strcmp(buf
, "scsi")) {
1974 max_devs
= MAX_SCSI_DEVS
;
1975 } else if (!strcmp(buf
, "floppy")) {
1978 } else if (!strcmp(buf
, "pflash")) {
1981 } else if (!strcmp(buf
, "mtd")) {
1984 } else if (!strcmp(buf
, "sd")) {
1987 } else if (!strcmp(buf
, "virtio")) {
1990 } else if (!strcmp(buf
, "xen")) {
1993 } else if (!strcmp(buf
, "none")) {
1997 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2002 if (cyls
|| heads
|| secs
) {
2003 if (cyls
< 1 || cyls
> 16383) {
2004 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2007 if (heads
< 1 || heads
> 16) {
2008 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2011 if (secs
< 1 || secs
> 63) {
2012 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2017 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2020 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2024 if (!strcmp(buf
, "none"))
2025 translation
= BIOS_ATA_TRANSLATION_NONE
;
2026 else if (!strcmp(buf
, "lba"))
2027 translation
= BIOS_ATA_TRANSLATION_LBA
;
2028 else if (!strcmp(buf
, "auto"))
2029 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2031 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2036 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2037 if (!strcmp(buf
, "disk")) {
2039 } else if (!strcmp(buf
, "cdrom")) {
2040 if (cyls
|| secs
|| heads
) {
2042 "qemu: '%s' invalid physical CHS format\n", buf
);
2045 media
= MEDIA_CDROM
;
2047 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2052 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2053 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2055 else if (!strcmp(buf
, "writethrough"))
2057 else if (!strcmp(buf
, "writeback"))
2060 fprintf(stderr
, "qemu: invalid cache option\n");
2065 #ifdef CONFIG_LINUX_AIO
2066 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2067 if (!strcmp(buf
, "threads"))
2069 else if (!strcmp(buf
, "native"))
2072 fprintf(stderr
, "qemu: invalid aio option\n");
2078 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2079 if (strcmp(buf
, "?") == 0) {
2080 fprintf(stderr
, "qemu: Supported formats:");
2081 bdrv_iterate_format(bdrv_format_print
, NULL
);
2082 fprintf(stderr
, "\n");
2085 drv
= bdrv_find_format(buf
);
2087 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2092 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2093 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2094 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2095 fprintf(stderr
, "werror is no supported by this format\n");
2098 if (!strcmp(buf
, "ignore"))
2099 onerror
= BLOCK_ERR_IGNORE
;
2100 else if (!strcmp(buf
, "enospc"))
2101 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2102 else if (!strcmp(buf
, "stop"))
2103 onerror
= BLOCK_ERR_STOP_ANY
;
2104 else if (!strcmp(buf
, "report"))
2105 onerror
= BLOCK_ERR_REPORT
;
2107 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2112 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2113 if (type
!= IF_VIRTIO
) {
2114 fprintf(stderr
, "addr is not supported\n");
2119 /* compute bus and unit according index */
2122 if (bus_id
!= 0 || unit_id
!= -1) {
2124 "qemu: index cannot be used with bus and unit\n");
2132 unit_id
= index
% max_devs
;
2133 bus_id
= index
/ max_devs
;
2137 /* if user doesn't specify a unit_id,
2138 * try to find the first free
2141 if (unit_id
== -1) {
2143 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2145 if (max_devs
&& unit_id
>= max_devs
) {
2146 unit_id
-= max_devs
;
2154 if (max_devs
&& unit_id
>= max_devs
) {
2155 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2156 unit_id
, max_devs
- 1);
2161 * ignore multiple definitions
2164 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2171 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2172 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2173 dinfo
->id
= qemu_strdup(buf
);
2175 /* no id supplied -> create one */
2176 dinfo
->id
= qemu_mallocz(32);
2177 if (type
== IF_IDE
|| type
== IF_SCSI
)
2178 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2180 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2181 devname
, bus_id
, mediastr
, unit_id
);
2183 snprintf(dinfo
->id
, 32, "%s%s%i",
2184 devname
, mediastr
, unit_id
);
2186 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2187 dinfo
->devaddr
= devaddr
;
2189 dinfo
->bus
= bus_id
;
2190 dinfo
->unit
= unit_id
;
2191 dinfo
->onerror
= onerror
;
2194 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2195 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2205 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2206 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2210 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2215 /* FIXME: This isn't really a floppy, but it's a reasonable
2218 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2224 /* add virtio block device */
2225 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2226 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2227 qemu_opt_set(opts
, "drive", dinfo
->id
);
2229 qemu_opt_set(opts
, "addr", devaddr
);
2240 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2241 cache
= 2; /* always use write-back with snapshot */
2243 if (cache
== 0) /* no caching */
2244 bdrv_flags
|= BDRV_O_NOCACHE
;
2245 else if (cache
== 2) /* write-back */
2246 bdrv_flags
|= BDRV_O_CACHE_WB
;
2249 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2251 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2254 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2255 fprintf(stderr
, "qemu: could not open disk image %s\n",
2260 if (bdrv_key_required(dinfo
->bdrv
))
2266 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2268 QEMUMachine
*machine
= opaque
;
2269 int fatal_error
= 0;
2271 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2278 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2280 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2281 qemu_opt_set(opts
, "snapshot", "on");
2286 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2288 boot_set_handler
= func
;
2289 boot_set_opaque
= opaque
;
2292 int qemu_boot_set(const char *boot_devices
)
2294 if (!boot_set_handler
) {
2297 return boot_set_handler(boot_set_opaque
, boot_devices
);
2300 static int parse_bootdevices(char *devices
)
2302 /* We just do some generic consistency checks */
2306 for (p
= devices
; *p
!= '\0'; p
++) {
2307 /* Allowed boot devices are:
2308 * a-b: floppy disk drives
2309 * c-f: IDE disk drives
2310 * g-m: machine implementation dependant drives
2311 * n-p: network devices
2312 * It's up to each machine implementation to check if the given boot
2313 * devices match the actual hardware implementation and firmware
2316 if (*p
< 'a' || *p
> 'p') {
2317 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2320 if (bitmap
& (1 << (*p
- 'a'))) {
2321 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2324 bitmap
|= 1 << (*p
- 'a');
2329 static void restore_boot_devices(void *opaque
)
2331 char *standard_boot_devices
= opaque
;
2333 qemu_boot_set(standard_boot_devices
);
2335 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2336 qemu_free(standard_boot_devices
);
2339 static void numa_add(const char *optarg
)
2343 unsigned long long value
, endvalue
;
2346 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2347 if (!strcmp(option
, "node")) {
2348 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2349 nodenr
= nb_numa_nodes
;
2351 nodenr
= strtoull(option
, NULL
, 10);
2354 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2355 node_mem
[nodenr
] = 0;
2357 value
= strtoull(option
, &endptr
, 0);
2359 case 0: case 'M': case 'm':
2366 node_mem
[nodenr
] = value
;
2368 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2369 node_cpumask
[nodenr
] = 0;
2371 value
= strtoull(option
, &endptr
, 10);
2374 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2376 if (*endptr
== '-') {
2377 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2378 if (endvalue
>= 63) {
2381 "only 63 CPUs in NUMA mode supported.\n");
2383 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2388 node_cpumask
[nodenr
] = value
;
2395 static void smp_parse(const char *optarg
)
2397 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2401 smp
= strtoul(optarg
, &endptr
, 10);
2402 if (endptr
!= optarg
) {
2403 if (*endptr
== ',') {
2407 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2408 sockets
= strtoull(option
, NULL
, 10);
2409 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2410 cores
= strtoull(option
, NULL
, 10);
2411 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2412 threads
= strtoull(option
, NULL
, 10);
2413 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2414 max_cpus
= strtoull(option
, NULL
, 10);
2416 /* compute missing values, prefer sockets over cores over threads */
2417 if (smp
== 0 || sockets
== 0) {
2418 sockets
= sockets
> 0 ? sockets
: 1;
2419 cores
= cores
> 0 ? cores
: 1;
2420 threads
= threads
> 0 ? threads
: 1;
2422 smp
= cores
* threads
* sockets
;
2424 sockets
= smp
/ (cores
* threads
);
2428 threads
= threads
> 0 ? threads
: 1;
2429 cores
= smp
/ (sockets
* threads
);
2432 sockets
= smp
/ (cores
* threads
);
2434 threads
= smp
/ (cores
* sockets
);
2439 smp_cores
= cores
> 0 ? cores
: 1;
2440 smp_threads
= threads
> 0 ? threads
: 1;
2442 max_cpus
= smp_cpus
;
2445 /***********************************************************/
2448 static void usb_msd_password_cb(void *opaque
, int err
)
2450 USBDevice
*dev
= opaque
;
2453 usb_device_attach(dev
);
2455 dev
->info
->handle_destroy(dev
);
2464 .qdev
= "QEMU USB Mouse",
2467 .qdev
= "QEMU USB Tablet",
2470 .qdev
= "QEMU USB Keyboard",
2472 .name
= "wacom-tablet",
2473 .qdev
= "QEMU PenPartner Tablet",
2477 static int usb_device_add(const char *devname
, int is_hotplug
)
2480 USBBus
*bus
= usb_bus_find(-1 /* any */);
2481 USBDevice
*dev
= NULL
;
2487 /* simple devices which don't need extra care */
2488 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2489 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2491 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2495 /* the other ones */
2496 if (strstart(devname
, "host:", &p
)) {
2497 dev
= usb_host_device_open(p
);
2498 } else if (strstart(devname
, "disk:", &p
)) {
2499 BlockDriverState
*bs
;
2501 dev
= usb_msd_init(p
);
2504 bs
= usb_msd_get_bdrv(dev
);
2505 if (bdrv_key_required(bs
)) {
2508 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2513 } else if (strstart(devname
, "serial:", &p
)) {
2514 dev
= usb_serial_init(p
);
2515 #ifdef CONFIG_BRLAPI
2516 } else if (!strcmp(devname
, "braille")) {
2517 dev
= usb_baum_init();
2519 } else if (strstart(devname
, "net:", &p
)) {
2522 if (net_client_init(NULL
, "nic", p
) < 0)
2524 nd_table
[nic
].model
= "usb";
2525 dev
= usb_net_init(&nd_table
[nic
]);
2526 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2527 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2528 bt_new_hci(qemu_find_bt_vlan(0)));
2539 static int usb_device_del(const char *devname
)
2544 if (strstart(devname
, "host:", &p
))
2545 return usb_host_device_close(p
);
2550 p
= strchr(devname
, '.');
2553 bus_num
= strtoul(devname
, NULL
, 0);
2554 addr
= strtoul(p
+ 1, NULL
, 0);
2556 return usb_device_delete_addr(bus_num
, addr
);
2559 static int usb_parse(const char *cmdline
)
2561 return usb_device_add(cmdline
, 0);
2564 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2566 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2569 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2571 usb_device_del(qdict_get_str(qdict
, "devname"));
2574 /***********************************************************/
2575 /* PCMCIA/Cardbus */
2577 static struct pcmcia_socket_entry_s
{
2578 PCMCIASocket
*socket
;
2579 struct pcmcia_socket_entry_s
*next
;
2580 } *pcmcia_sockets
= 0;
2582 void pcmcia_socket_register(PCMCIASocket
*socket
)
2584 struct pcmcia_socket_entry_s
*entry
;
2586 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2587 entry
->socket
= socket
;
2588 entry
->next
= pcmcia_sockets
;
2589 pcmcia_sockets
= entry
;
2592 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2594 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2596 ptr
= &pcmcia_sockets
;
2597 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2598 if (entry
->socket
== socket
) {
2604 void pcmcia_info(Monitor
*mon
)
2606 struct pcmcia_socket_entry_s
*iter
;
2608 if (!pcmcia_sockets
)
2609 monitor_printf(mon
, "No PCMCIA sockets\n");
2611 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2612 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2613 iter
->socket
->attached
? iter
->socket
->card_string
:
2617 /***********************************************************/
2618 /* register display */
2620 struct DisplayAllocator default_allocator
= {
2621 defaultallocator_create_displaysurface
,
2622 defaultallocator_resize_displaysurface
,
2623 defaultallocator_free_displaysurface
2626 void register_displaystate(DisplayState
*ds
)
2636 DisplayState
*get_displaystate(void)
2638 return display_state
;
2641 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2643 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2644 return ds
->allocator
;
2649 static void dumb_display_init(void)
2651 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2652 ds
->allocator
= &default_allocator
;
2653 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2654 register_displaystate(ds
);
2657 /***********************************************************/
2660 typedef struct IOHandlerRecord
{
2662 IOCanRWHandler
*fd_read_poll
;
2664 IOHandler
*fd_write
;
2667 /* temporary data */
2669 struct IOHandlerRecord
*next
;
2672 static IOHandlerRecord
*first_io_handler
;
2674 /* XXX: fd_read_poll should be suppressed, but an API change is
2675 necessary in the character devices to suppress fd_can_read(). */
2676 int qemu_set_fd_handler2(int fd
,
2677 IOCanRWHandler
*fd_read_poll
,
2679 IOHandler
*fd_write
,
2682 IOHandlerRecord
**pioh
, *ioh
;
2684 if (!fd_read
&& !fd_write
) {
2685 pioh
= &first_io_handler
;
2690 if (ioh
->fd
== fd
) {
2697 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2701 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2702 ioh
->next
= first_io_handler
;
2703 first_io_handler
= ioh
;
2706 ioh
->fd_read_poll
= fd_read_poll
;
2707 ioh
->fd_read
= fd_read
;
2708 ioh
->fd_write
= fd_write
;
2709 ioh
->opaque
= opaque
;
2715 int qemu_set_fd_handler(int fd
,
2717 IOHandler
*fd_write
,
2720 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2724 /***********************************************************/
2725 /* Polling handling */
2727 typedef struct PollingEntry
{
2730 struct PollingEntry
*next
;
2733 static PollingEntry
*first_polling_entry
;
2735 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2737 PollingEntry
**ppe
, *pe
;
2738 pe
= qemu_mallocz(sizeof(PollingEntry
));
2740 pe
->opaque
= opaque
;
2741 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2746 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2748 PollingEntry
**ppe
, *pe
;
2749 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2751 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2759 /***********************************************************/
2760 /* Wait objects support */
2761 typedef struct WaitObjects
{
2763 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2764 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2765 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2768 static WaitObjects wait_objects
= {0};
2770 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2772 WaitObjects
*w
= &wait_objects
;
2774 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2776 w
->events
[w
->num
] = handle
;
2777 w
->func
[w
->num
] = func
;
2778 w
->opaque
[w
->num
] = opaque
;
2783 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2786 WaitObjects
*w
= &wait_objects
;
2789 for (i
= 0; i
< w
->num
; i
++) {
2790 if (w
->events
[i
] == handle
)
2793 w
->events
[i
] = w
->events
[i
+ 1];
2794 w
->func
[i
] = w
->func
[i
+ 1];
2795 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2803 /***********************************************************/
2804 /* ram save/restore */
2806 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2807 #define RAM_SAVE_FLAG_COMPRESS 0x02
2808 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2809 #define RAM_SAVE_FLAG_PAGE 0x08
2810 #define RAM_SAVE_FLAG_EOS 0x10
2812 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2814 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2815 uint32_t *array
= (uint32_t *)page
;
2818 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2819 if (array
[i
] != val
)
2826 static int ram_save_block(QEMUFile
*f
)
2828 static ram_addr_t current_addr
= 0;
2829 ram_addr_t saved_addr
= current_addr
;
2830 ram_addr_t addr
= 0;
2833 while (addr
< last_ram_offset
) {
2834 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2837 cpu_physical_memory_reset_dirty(current_addr
,
2838 current_addr
+ TARGET_PAGE_SIZE
,
2839 MIGRATION_DIRTY_FLAG
);
2841 p
= qemu_get_ram_ptr(current_addr
);
2843 if (is_dup_page(p
, *p
)) {
2844 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2845 qemu_put_byte(f
, *p
);
2847 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2848 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2854 addr
+= TARGET_PAGE_SIZE
;
2855 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2861 static uint64_t bytes_transferred
= 0;
2863 static ram_addr_t
ram_save_remaining(void)
2866 ram_addr_t count
= 0;
2868 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2869 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2876 uint64_t ram_bytes_remaining(void)
2878 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2881 uint64_t ram_bytes_transferred(void)
2883 return bytes_transferred
;
2886 uint64_t ram_bytes_total(void)
2888 return last_ram_offset
;
2891 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2894 uint64_t bytes_transferred_last
;
2896 uint64_t expected_time
= 0;
2898 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2899 qemu_file_set_error(f
);
2904 /* Make sure all dirty bits are set */
2905 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2906 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2907 cpu_physical_memory_set_dirty(addr
);
2910 /* Enable dirty memory tracking */
2911 cpu_physical_memory_set_dirty_tracking(1);
2913 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2916 bytes_transferred_last
= bytes_transferred
;
2917 bwidth
= get_clock();
2919 while (!qemu_file_rate_limit(f
)) {
2922 ret
= ram_save_block(f
);
2923 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2924 if (ret
== 0) /* no more blocks */
2928 bwidth
= get_clock() - bwidth
;
2929 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2931 /* if we haven't transferred anything this round, force expected_time to a
2932 * a very high value, but without crashing */
2936 /* try transferring iterative blocks of memory */
2940 /* flush all remaining blocks regardless of rate limiting */
2941 while (ram_save_block(f
) != 0) {
2942 bytes_transferred
+= TARGET_PAGE_SIZE
;
2944 cpu_physical_memory_set_dirty_tracking(0);
2947 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2949 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2951 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2954 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2959 if (version_id
!= 3)
2963 addr
= qemu_get_be64(f
);
2965 flags
= addr
& ~TARGET_PAGE_MASK
;
2966 addr
&= TARGET_PAGE_MASK
;
2968 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2969 if (addr
!= last_ram_offset
)
2973 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2974 uint8_t ch
= qemu_get_byte(f
);
2975 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2978 (!kvm_enabled() || kvm_has_sync_mmu())) {
2979 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2982 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
2983 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2984 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
2989 void qemu_service_io(void)
2991 qemu_notify_event();
2994 /***********************************************************/
2995 /* bottom halves (can be seen as timers which expire ASAP) */
3006 static QEMUBH
*first_bh
= NULL
;
3008 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3011 bh
= qemu_mallocz(sizeof(QEMUBH
));
3013 bh
->opaque
= opaque
;
3014 bh
->next
= first_bh
;
3019 int qemu_bh_poll(void)
3025 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3026 if (!bh
->deleted
&& bh
->scheduled
) {
3035 /* remove deleted bhs */
3049 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3057 void qemu_bh_schedule(QEMUBH
*bh
)
3063 /* stop the currently executing CPU to execute the BH ASAP */
3064 qemu_notify_event();
3067 void qemu_bh_cancel(QEMUBH
*bh
)
3072 void qemu_bh_delete(QEMUBH
*bh
)
3078 static void qemu_bh_update_timeout(int *timeout
)
3082 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3083 if (!bh
->deleted
&& bh
->scheduled
) {
3085 /* idle bottom halves will be polled at least
3087 *timeout
= MIN(10, *timeout
);
3089 /* non-idle bottom halves will be executed
3098 /***********************************************************/
3099 /* machine registration */
3101 static QEMUMachine
*first_machine
= NULL
;
3102 QEMUMachine
*current_machine
= NULL
;
3104 int qemu_register_machine(QEMUMachine
*m
)
3107 pm
= &first_machine
;
3115 static QEMUMachine
*find_machine(const char *name
)
3119 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3120 if (!strcmp(m
->name
, name
))
3122 if (m
->alias
&& !strcmp(m
->alias
, name
))
3128 static QEMUMachine
*find_default_machine(void)
3132 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3133 if (m
->is_default
) {
3140 /***********************************************************/
3141 /* main execution loop */
3143 static void gui_update(void *opaque
)
3145 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3146 DisplayState
*ds
= opaque
;
3147 DisplayChangeListener
*dcl
= ds
->listeners
;
3151 while (dcl
!= NULL
) {
3152 if (dcl
->gui_timer_interval
&&
3153 dcl
->gui_timer_interval
< interval
)
3154 interval
= dcl
->gui_timer_interval
;
3157 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3160 static void nographic_update(void *opaque
)
3162 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3164 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3167 struct vm_change_state_entry
{
3168 VMChangeStateHandler
*cb
;
3170 QLIST_ENTRY (vm_change_state_entry
) entries
;
3173 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3175 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3178 VMChangeStateEntry
*e
;
3180 e
= qemu_mallocz(sizeof (*e
));
3184 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3188 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3190 QLIST_REMOVE (e
, entries
);
3194 static void vm_state_notify(int running
, int reason
)
3196 VMChangeStateEntry
*e
;
3198 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3199 e
->cb(e
->opaque
, running
, reason
);
3203 static void resume_all_vcpus(void);
3204 static void pause_all_vcpus(void);
3211 vm_state_notify(1, 0);
3212 qemu_rearm_alarm_timer(alarm_timer
);
3217 /* reset/shutdown handler */
3219 typedef struct QEMUResetEntry
{
3220 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3221 QEMUResetHandler
*func
;
3225 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3226 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3227 static int reset_requested
;
3228 static int shutdown_requested
;
3229 static int powerdown_requested
;
3230 static int debug_requested
;
3231 static int vmstop_requested
;
3233 int qemu_shutdown_requested(void)
3235 int r
= shutdown_requested
;
3236 shutdown_requested
= 0;
3240 int qemu_reset_requested(void)
3242 int r
= reset_requested
;
3243 reset_requested
= 0;
3247 int qemu_powerdown_requested(void)
3249 int r
= powerdown_requested
;
3250 powerdown_requested
= 0;
3254 static int qemu_debug_requested(void)
3256 int r
= debug_requested
;
3257 debug_requested
= 0;
3261 static int qemu_vmstop_requested(void)
3263 int r
= vmstop_requested
;
3264 vmstop_requested
= 0;
3268 static void do_vm_stop(int reason
)
3271 cpu_disable_ticks();
3274 vm_state_notify(0, reason
);
3278 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3280 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3283 re
->opaque
= opaque
;
3284 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3287 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3291 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3292 if (re
->func
== func
&& re
->opaque
== opaque
) {
3293 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3300 void qemu_system_reset(void)
3302 QEMUResetEntry
*re
, *nre
;
3304 /* reset all devices */
3305 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3306 re
->func(re
->opaque
);
3310 void qemu_system_reset_request(void)
3313 shutdown_requested
= 1;
3315 reset_requested
= 1;
3317 qemu_notify_event();
3320 void qemu_system_shutdown_request(void)
3322 shutdown_requested
= 1;
3323 qemu_notify_event();
3326 void qemu_system_powerdown_request(void)
3328 powerdown_requested
= 1;
3329 qemu_notify_event();
3332 #ifdef CONFIG_IOTHREAD
3333 static void qemu_system_vmstop_request(int reason
)
3335 vmstop_requested
= reason
;
3336 qemu_notify_event();
3341 static int io_thread_fd
= -1;
3343 static void qemu_event_increment(void)
3345 static const char byte
= 0;
3347 if (io_thread_fd
== -1)
3350 write(io_thread_fd
, &byte
, sizeof(byte
));
3353 static void qemu_event_read(void *opaque
)
3355 int fd
= (unsigned long)opaque
;
3358 /* Drain the notify pipe */
3361 len
= read(fd
, buffer
, sizeof(buffer
));
3362 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3365 static int qemu_event_init(void)
3374 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3378 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3382 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3383 (void *)(unsigned long)fds
[0]);
3385 io_thread_fd
= fds
[1];
3394 HANDLE qemu_event_handle
;
3396 static void dummy_event_handler(void *opaque
)
3400 static int qemu_event_init(void)
3402 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3403 if (!qemu_event_handle
) {
3404 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3407 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3411 static void qemu_event_increment(void)
3413 if (!SetEvent(qemu_event_handle
)) {
3414 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3421 static int cpu_can_run(CPUState
*env
)
3430 #ifndef CONFIG_IOTHREAD
3431 static int qemu_init_main_loop(void)
3433 return qemu_event_init();
3436 void qemu_init_vcpu(void *_env
)
3438 CPUState
*env
= _env
;
3442 env
->nr_cores
= smp_cores
;
3443 env
->nr_threads
= smp_threads
;
3447 int qemu_cpu_self(void *env
)
3452 static void resume_all_vcpus(void)
3456 static void pause_all_vcpus(void)
3460 void qemu_cpu_kick(void *env
)
3465 void qemu_notify_event(void)
3467 CPUState
*env
= cpu_single_env
;
3474 #define qemu_mutex_lock_iothread() do { } while (0)
3475 #define qemu_mutex_unlock_iothread() do { } while (0)
3477 void vm_stop(int reason
)
3482 #else /* CONFIG_IOTHREAD */
3484 #include "qemu-thread.h"
3486 QemuMutex qemu_global_mutex
;
3487 static QemuMutex qemu_fair_mutex
;
3489 static QemuThread io_thread
;
3491 static QemuThread
*tcg_cpu_thread
;
3492 static QemuCond
*tcg_halt_cond
;
3494 static int qemu_system_ready
;
3496 static QemuCond qemu_cpu_cond
;
3498 static QemuCond qemu_system_cond
;
3499 static QemuCond qemu_pause_cond
;
3501 static void block_io_signals(void);
3502 static void unblock_io_signals(void);
3503 static int tcg_has_work(void);
3505 static int qemu_init_main_loop(void)
3509 ret
= qemu_event_init();
3513 qemu_cond_init(&qemu_pause_cond
);
3514 qemu_mutex_init(&qemu_fair_mutex
);
3515 qemu_mutex_init(&qemu_global_mutex
);
3516 qemu_mutex_lock(&qemu_global_mutex
);
3518 unblock_io_signals();
3519 qemu_thread_self(&io_thread
);
3524 static void qemu_wait_io_event(CPUState
*env
)
3526 while (!tcg_has_work())
3527 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3529 qemu_mutex_unlock(&qemu_global_mutex
);
3532 * Users of qemu_global_mutex can be starved, having no chance
3533 * to acquire it since this path will get to it first.
3534 * So use another lock to provide fairness.
3536 qemu_mutex_lock(&qemu_fair_mutex
);
3537 qemu_mutex_unlock(&qemu_fair_mutex
);
3539 qemu_mutex_lock(&qemu_global_mutex
);
3543 qemu_cond_signal(&qemu_pause_cond
);
3547 static int qemu_cpu_exec(CPUState
*env
);
3549 static void *kvm_cpu_thread_fn(void *arg
)
3551 CPUState
*env
= arg
;
3554 qemu_thread_self(env
->thread
);
3558 /* signal CPU creation */
3559 qemu_mutex_lock(&qemu_global_mutex
);
3561 qemu_cond_signal(&qemu_cpu_cond
);
3563 /* and wait for machine initialization */
3564 while (!qemu_system_ready
)
3565 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3568 if (cpu_can_run(env
))
3570 qemu_wait_io_event(env
);
3576 static void tcg_cpu_exec(void);
3578 static void *tcg_cpu_thread_fn(void *arg
)
3580 CPUState
*env
= arg
;
3583 qemu_thread_self(env
->thread
);
3585 /* signal CPU creation */
3586 qemu_mutex_lock(&qemu_global_mutex
);
3587 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3589 qemu_cond_signal(&qemu_cpu_cond
);
3591 /* and wait for machine initialization */
3592 while (!qemu_system_ready
)
3593 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3597 qemu_wait_io_event(cur_cpu
);
3603 void qemu_cpu_kick(void *_env
)
3605 CPUState
*env
= _env
;
3606 qemu_cond_broadcast(env
->halt_cond
);
3608 qemu_thread_signal(env
->thread
, SIGUSR1
);
3611 int qemu_cpu_self(void *_env
)
3613 CPUState
*env
= _env
;
3616 qemu_thread_self(&this);
3618 return qemu_thread_equal(&this, env
->thread
);
3621 static void cpu_signal(int sig
)
3624 cpu_exit(cpu_single_env
);
3627 static void block_io_signals(void)
3630 struct sigaction sigact
;
3633 sigaddset(&set
, SIGUSR2
);
3634 sigaddset(&set
, SIGIO
);
3635 sigaddset(&set
, SIGALRM
);
3636 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3639 sigaddset(&set
, SIGUSR1
);
3640 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3642 memset(&sigact
, 0, sizeof(sigact
));
3643 sigact
.sa_handler
= cpu_signal
;
3644 sigaction(SIGUSR1
, &sigact
, NULL
);
3647 static void unblock_io_signals(void)
3652 sigaddset(&set
, SIGUSR2
);
3653 sigaddset(&set
, SIGIO
);
3654 sigaddset(&set
, SIGALRM
);
3655 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3658 sigaddset(&set
, SIGUSR1
);
3659 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3662 static void qemu_signal_lock(unsigned int msecs
)
3664 qemu_mutex_lock(&qemu_fair_mutex
);
3666 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3667 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3668 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3671 qemu_mutex_unlock(&qemu_fair_mutex
);
3674 static void qemu_mutex_lock_iothread(void)
3676 if (kvm_enabled()) {
3677 qemu_mutex_lock(&qemu_fair_mutex
);
3678 qemu_mutex_lock(&qemu_global_mutex
);
3679 qemu_mutex_unlock(&qemu_fair_mutex
);
3681 qemu_signal_lock(100);
3684 static void qemu_mutex_unlock_iothread(void)
3686 qemu_mutex_unlock(&qemu_global_mutex
);
3689 static int all_vcpus_paused(void)
3691 CPUState
*penv
= first_cpu
;
3696 penv
= (CPUState
*)penv
->next_cpu
;
3702 static void pause_all_vcpus(void)
3704 CPUState
*penv
= first_cpu
;
3708 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3709 qemu_cpu_kick(penv
);
3710 penv
= (CPUState
*)penv
->next_cpu
;
3713 while (!all_vcpus_paused()) {
3714 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3717 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3718 penv
= (CPUState
*)penv
->next_cpu
;
3723 static void resume_all_vcpus(void)
3725 CPUState
*penv
= first_cpu
;
3730 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3731 qemu_cpu_kick(penv
);
3732 penv
= (CPUState
*)penv
->next_cpu
;
3736 static void tcg_init_vcpu(void *_env
)
3738 CPUState
*env
= _env
;
3739 /* share a single thread for all cpus with TCG */
3740 if (!tcg_cpu_thread
) {
3741 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3742 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3743 qemu_cond_init(env
->halt_cond
);
3744 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3745 while (env
->created
== 0)
3746 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3747 tcg_cpu_thread
= env
->thread
;
3748 tcg_halt_cond
= env
->halt_cond
;
3750 env
->thread
= tcg_cpu_thread
;
3751 env
->halt_cond
= tcg_halt_cond
;
3755 static void kvm_start_vcpu(CPUState
*env
)
3757 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3758 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3759 qemu_cond_init(env
->halt_cond
);
3760 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3761 while (env
->created
== 0)
3762 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3765 void qemu_init_vcpu(void *_env
)
3767 CPUState
*env
= _env
;
3770 kvm_start_vcpu(env
);
3773 env
->nr_cores
= smp_cores
;
3774 env
->nr_threads
= smp_threads
;
3777 void qemu_notify_event(void)
3779 qemu_event_increment();
3782 void vm_stop(int reason
)
3785 qemu_thread_self(&me
);
3787 if (!qemu_thread_equal(&me
, &io_thread
)) {
3788 qemu_system_vmstop_request(reason
);
3790 * FIXME: should not return to device code in case
3791 * vm_stop() has been requested.
3793 if (cpu_single_env
) {
3794 cpu_exit(cpu_single_env
);
3795 cpu_single_env
->stop
= 1;
3806 static void host_main_loop_wait(int *timeout
)
3812 /* XXX: need to suppress polling by better using win32 events */
3814 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3815 ret
|= pe
->func(pe
->opaque
);
3819 WaitObjects
*w
= &wait_objects
;
3821 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3822 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3823 if (w
->func
[ret
- WAIT_OBJECT_0
])
3824 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3826 /* Check for additional signaled events */
3827 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3829 /* Check if event is signaled */
3830 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3831 if(ret2
== WAIT_OBJECT_0
) {
3833 w
->func
[i
](w
->opaque
[i
]);
3834 } else if (ret2
== WAIT_TIMEOUT
) {
3836 err
= GetLastError();
3837 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3840 } else if (ret
== WAIT_TIMEOUT
) {
3842 err
= GetLastError();
3843 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3850 static void host_main_loop_wait(int *timeout
)
3855 void main_loop_wait(int timeout
)
3857 IOHandlerRecord
*ioh
;
3858 fd_set rfds
, wfds
, xfds
;
3862 qemu_bh_update_timeout(&timeout
);
3864 host_main_loop_wait(&timeout
);
3866 /* poll any events */
3867 /* XXX: separate device handlers from system ones */
3872 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3876 (!ioh
->fd_read_poll
||
3877 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3878 FD_SET(ioh
->fd
, &rfds
);
3882 if (ioh
->fd_write
) {
3883 FD_SET(ioh
->fd
, &wfds
);
3889 tv
.tv_sec
= timeout
/ 1000;
3890 tv
.tv_usec
= (timeout
% 1000) * 1000;
3892 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3894 qemu_mutex_unlock_iothread();
3895 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3896 qemu_mutex_lock_iothread();
3898 IOHandlerRecord
**pioh
;
3900 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3901 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3902 ioh
->fd_read(ioh
->opaque
);
3904 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3905 ioh
->fd_write(ioh
->opaque
);
3909 /* remove deleted IO handlers */
3910 pioh
= &first_io_handler
;
3921 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3923 /* rearm timer, if not periodic */
3924 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3925 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3926 qemu_rearm_alarm_timer(alarm_timer
);
3929 /* vm time timers */
3931 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3932 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
3933 qemu_get_clock(vm_clock
));
3936 /* real time timers */
3937 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
3938 qemu_get_clock(rt_clock
));
3940 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
3941 qemu_get_clock(host_clock
));
3943 /* Check bottom-halves last in case any of the earlier events triggered
3949 static int qemu_cpu_exec(CPUState
*env
)
3952 #ifdef CONFIG_PROFILER
3956 #ifdef CONFIG_PROFILER
3957 ti
= profile_getclock();
3962 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3963 env
->icount_decr
.u16
.low
= 0;
3964 env
->icount_extra
= 0;
3965 count
= qemu_next_deadline();
3966 count
= (count
+ (1 << icount_time_shift
) - 1)
3967 >> icount_time_shift
;
3968 qemu_icount
+= count
;
3969 decr
= (count
> 0xffff) ? 0xffff : count
;
3971 env
->icount_decr
.u16
.low
= decr
;
3972 env
->icount_extra
= count
;
3974 ret
= cpu_exec(env
);
3975 #ifdef CONFIG_PROFILER
3976 qemu_time
+= profile_getclock() - ti
;
3979 /* Fold pending instructions back into the
3980 instruction counter, and clear the interrupt flag. */
3981 qemu_icount
-= (env
->icount_decr
.u16
.low
3982 + env
->icount_extra
);
3983 env
->icount_decr
.u32
= 0;
3984 env
->icount_extra
= 0;
3989 static void tcg_cpu_exec(void)
3993 if (next_cpu
== NULL
)
3994 next_cpu
= first_cpu
;
3995 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
3996 CPUState
*env
= cur_cpu
= next_cpu
;
4000 if (timer_alarm_pending
) {
4001 timer_alarm_pending
= 0;
4004 if (cpu_can_run(env
))
4005 ret
= qemu_cpu_exec(env
);
4006 if (ret
== EXCP_DEBUG
) {
4007 gdb_set_stop_cpu(env
);
4008 debug_requested
= 1;
4014 static int cpu_has_work(CPUState
*env
)
4022 if (qemu_cpu_has_work(env
))
4027 static int tcg_has_work(void)
4031 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4032 if (cpu_has_work(env
))
4037 static int qemu_calculate_timeout(void)
4039 #ifndef CONFIG_IOTHREAD
4044 else if (tcg_has_work())
4046 else if (!use_icount
)
4049 /* XXX: use timeout computed from timers */
4052 /* Advance virtual time to the next event. */
4053 if (use_icount
== 1) {
4054 /* When not using an adaptive execution frequency
4055 we tend to get badly out of sync with real time,
4056 so just delay for a reasonable amount of time. */
4059 delta
= cpu_get_icount() - cpu_get_clock();
4062 /* If virtual time is ahead of real time then just
4064 timeout
= (delta
/ 1000000) + 1;
4066 /* Wait for either IO to occur or the next
4068 add
= qemu_next_deadline();
4069 /* We advance the timer before checking for IO.
4070 Limit the amount we advance so that early IO
4071 activity won't get the guest too far ahead. */
4075 add
= (add
+ (1 << icount_time_shift
) - 1)
4076 >> icount_time_shift
;
4078 timeout
= delta
/ 1000000;
4085 #else /* CONFIG_IOTHREAD */
4090 static int vm_can_run(void)
4092 if (powerdown_requested
)
4094 if (reset_requested
)
4096 if (shutdown_requested
)
4098 if (debug_requested
)
4103 qemu_irq qemu_system_powerdown
;
4105 static void main_loop(void)
4109 #ifdef CONFIG_IOTHREAD
4110 qemu_system_ready
= 1;
4111 qemu_cond_broadcast(&qemu_system_cond
);
4116 #ifdef CONFIG_PROFILER
4119 #ifndef CONFIG_IOTHREAD
4122 #ifdef CONFIG_PROFILER
4123 ti
= profile_getclock();
4125 main_loop_wait(qemu_calculate_timeout());
4126 #ifdef CONFIG_PROFILER
4127 dev_time
+= profile_getclock() - ti
;
4129 } while (vm_can_run());
4131 if (qemu_debug_requested())
4132 vm_stop(EXCP_DEBUG
);
4133 if (qemu_shutdown_requested()) {
4140 if (qemu_reset_requested()) {
4142 qemu_system_reset();
4145 if (qemu_powerdown_requested()) {
4146 qemu_irq_raise(qemu_system_powerdown
);
4148 if ((r
= qemu_vmstop_requested()))
4154 static void version(void)
4156 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4159 static void help(int exitcode
)
4162 printf("usage: %s [options] [disk_image]\n"
4164 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4166 #define DEF(option, opt_arg, opt_enum, opt_help) \
4168 #define DEFHEADING(text) stringify(text) "\n"
4169 #include "qemu-options.h"
4174 "During emulation, the following keys are useful:\n"
4175 "ctrl-alt-f toggle full screen\n"
4176 "ctrl-alt-n switch to virtual console 'n'\n"
4177 "ctrl-alt toggle mouse and keyboard grab\n"
4179 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4184 DEFAULT_NETWORK_SCRIPT
,
4185 DEFAULT_NETWORK_DOWN_SCRIPT
,
4187 DEFAULT_GDBSTUB_PORT
,
4192 #define HAS_ARG 0x0001
4195 #define DEF(option, opt_arg, opt_enum, opt_help) \
4197 #define DEFHEADING(text)
4198 #include "qemu-options.h"
4204 typedef struct QEMUOption
{
4210 static const QEMUOption qemu_options
[] = {
4211 { "h", 0, QEMU_OPTION_h
},
4212 #define DEF(option, opt_arg, opt_enum, opt_help) \
4213 { option, opt_arg, opt_enum },
4214 #define DEFHEADING(text)
4215 #include "qemu-options.h"
4223 struct soundhw soundhw
[] = {
4224 #ifdef HAS_AUDIO_CHOICE
4225 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4231 { .init_isa
= pcspk_audio_init
}
4238 "Creative Sound Blaster 16",
4241 { .init_isa
= SB16_init
}
4245 #ifdef CONFIG_CS4231A
4251 { .init_isa
= cs4231a_init
}
4259 "Yamaha YMF262 (OPL3)",
4261 "Yamaha YM3812 (OPL2)",
4265 { .init_isa
= Adlib_init
}
4272 "Gravis Ultrasound GF1",
4275 { .init_isa
= GUS_init
}
4282 "Intel 82801AA AC97 Audio",
4285 { .init_pci
= ac97_init
}
4289 #ifdef CONFIG_ES1370
4292 "ENSONIQ AudioPCI ES1370",
4295 { .init_pci
= es1370_init
}
4299 #endif /* HAS_AUDIO_CHOICE */
4301 { NULL
, NULL
, 0, 0, { NULL
} }
4304 static void select_soundhw (const char *optarg
)
4308 if (*optarg
== '?') {
4311 printf ("Valid sound card names (comma separated):\n");
4312 for (c
= soundhw
; c
->name
; ++c
) {
4313 printf ("%-11s %s\n", c
->name
, c
->descr
);
4315 printf ("\n-soundhw all will enable all of the above\n");
4316 exit (*optarg
!= '?');
4324 if (!strcmp (optarg
, "all")) {
4325 for (c
= soundhw
; c
->name
; ++c
) {
4333 e
= strchr (p
, ',');
4334 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4336 for (c
= soundhw
; c
->name
; ++c
) {
4337 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4346 "Unknown sound card name (too big to show)\n");
4349 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4354 p
+= l
+ (e
!= NULL
);
4358 goto show_valid_cards
;
4363 static void select_vgahw (const char *p
)
4367 vga_interface_type
= VGA_NONE
;
4368 if (strstart(p
, "std", &opts
)) {
4369 vga_interface_type
= VGA_STD
;
4370 } else if (strstart(p
, "cirrus", &opts
)) {
4371 vga_interface_type
= VGA_CIRRUS
;
4372 } else if (strstart(p
, "vmware", &opts
)) {
4373 vga_interface_type
= VGA_VMWARE
;
4374 } else if (strstart(p
, "xenfb", &opts
)) {
4375 vga_interface_type
= VGA_XENFB
;
4376 } else if (!strstart(p
, "none", &opts
)) {
4378 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4382 const char *nextopt
;
4384 if (strstart(opts
, ",retrace=", &nextopt
)) {
4386 if (strstart(opts
, "dumb", &nextopt
))
4387 vga_retrace_method
= VGA_RETRACE_DUMB
;
4388 else if (strstart(opts
, "precise", &nextopt
))
4389 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4390 else goto invalid_vga
;
4391 } else goto invalid_vga
;
4397 static int balloon_parse(const char *arg
)
4401 if (strcmp(arg
, "none") == 0) {
4405 if (!strncmp(arg
, "virtio", 6)) {
4406 if (arg
[6] == ',') {
4407 /* have params -> parse them */
4408 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4412 /* create empty opts */
4413 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4415 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4424 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4426 exit(STATUS_CONTROL_C_EXIT
);
4431 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4435 if(strlen(str
) != 36)
4438 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4439 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4440 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4446 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4452 #define MAX_NET_CLIENTS 32
4456 static void termsig_handler(int signal
)
4458 qemu_system_shutdown_request();
4461 static void sigchld_handler(int signal
)
4463 waitpid(-1, NULL
, WNOHANG
);
4466 static void sighandler_setup(void)
4468 struct sigaction act
;
4470 memset(&act
, 0, sizeof(act
));
4471 act
.sa_handler
= termsig_handler
;
4472 sigaction(SIGINT
, &act
, NULL
);
4473 sigaction(SIGHUP
, &act
, NULL
);
4474 sigaction(SIGTERM
, &act
, NULL
);
4476 act
.sa_handler
= sigchld_handler
;
4477 act
.sa_flags
= SA_NOCLDSTOP
;
4478 sigaction(SIGCHLD
, &act
, NULL
);
4484 /* Look for support files in the same directory as the executable. */
4485 static char *find_datadir(const char *argv0
)
4491 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4498 while (p
!= buf
&& *p
!= '\\')
4501 if (access(buf
, R_OK
) == 0) {
4502 return qemu_strdup(buf
);
4508 /* Find a likely location for support files using the location of the binary.
4509 For installed binaries this will be "$bindir/../share/qemu". When
4510 running from the build tree this will be "$bindir/../pc-bios". */
4511 #define SHARE_SUFFIX "/share/qemu"
4512 #define BUILD_SUFFIX "/pc-bios"
4513 static char *find_datadir(const char *argv0
)
4521 #if defined(__linux__)
4524 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4530 #elif defined(__FreeBSD__)
4533 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4540 /* If we don't have any way of figuring out the actual executable
4541 location then try argv[0]. */
4543 p
= realpath(argv0
, buf
);
4551 max_len
= strlen(dir
) +
4552 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4553 res
= qemu_mallocz(max_len
);
4554 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4555 if (access(res
, R_OK
)) {
4556 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4557 if (access(res
, R_OK
)) {
4569 char *qemu_find_file(int type
, const char *name
)
4575 /* If name contains path separators then try it as a straight path. */
4576 if ((strchr(name
, '/') || strchr(name
, '\\'))
4577 && access(name
, R_OK
) == 0) {
4578 return qemu_strdup(name
);
4581 case QEMU_FILE_TYPE_BIOS
:
4584 case QEMU_FILE_TYPE_KEYMAP
:
4585 subdir
= "keymaps/";
4590 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4591 buf
= qemu_mallocz(len
);
4592 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4593 if (access(buf
, R_OK
)) {
4600 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4604 dev
= qdev_device_add(opts
);
4610 struct device_config
{
4612 DEV_USB
, /* -usbdevice */
4615 const char *cmdline
;
4616 QTAILQ_ENTRY(device_config
) next
;
4618 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4620 static void add_device_config(int type
, const char *cmdline
)
4622 struct device_config
*conf
;
4624 conf
= qemu_mallocz(sizeof(*conf
));
4626 conf
->cmdline
= cmdline
;
4627 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4630 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4632 struct device_config
*conf
;
4635 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4636 if (conf
->type
!= type
)
4638 rc
= func(conf
->cmdline
);
4645 int main(int argc
, char **argv
, char **envp
)
4647 const char *gdbstub_dev
= NULL
;
4648 uint32_t boot_devices_bitmap
= 0;
4650 int snapshot
, linux_boot
, net_boot
;
4651 const char *initrd_filename
;
4652 const char *kernel_filename
, *kernel_cmdline
;
4653 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4655 DisplayChangeListener
*dcl
;
4656 int cyls
, heads
, secs
, translation
;
4657 const char *net_clients
[MAX_NET_CLIENTS
];
4659 QemuOpts
*hda_opts
= NULL
, *opts
;
4661 const char *r
, *optarg
;
4662 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4663 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4664 int monitor_device_index
;
4665 const char *serial_devices
[MAX_SERIAL_PORTS
];
4666 int serial_device_index
;
4667 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4668 int parallel_device_index
;
4669 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4670 int virtio_console_index
;
4671 const char *loadvm
= NULL
;
4672 QEMUMachine
*machine
;
4673 const char *cpu_model
;
4678 const char *pid_file
= NULL
;
4679 const char *incoming
= NULL
;
4682 struct passwd
*pwd
= NULL
;
4683 const char *chroot_dir
= NULL
;
4684 const char *run_as
= NULL
;
4687 int show_vnc_port
= 0;
4689 qemu_errors_to_file(stderr
);
4690 qemu_cache_utils_init(envp
);
4692 QLIST_INIT (&vm_change_state_head
);
4695 struct sigaction act
;
4696 sigfillset(&act
.sa_mask
);
4698 act
.sa_handler
= SIG_IGN
;
4699 sigaction(SIGPIPE
, &act
, NULL
);
4702 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4703 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4704 QEMU to run on a single CPU */
4709 h
= GetCurrentProcess();
4710 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4711 for(i
= 0; i
< 32; i
++) {
4712 if (mask
& (1 << i
))
4717 SetProcessAffinityMask(h
, mask
);
4723 module_call_init(MODULE_INIT_MACHINE
);
4724 machine
= find_default_machine();
4726 initrd_filename
= NULL
;
4729 kernel_filename
= NULL
;
4730 kernel_cmdline
= "";
4731 cyls
= heads
= secs
= 0;
4732 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4734 serial_devices
[0] = "vc:80Cx24C";
4735 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4736 serial_devices
[i
] = NULL
;
4737 serial_device_index
= 0;
4739 parallel_devices
[0] = "vc:80Cx24C";
4740 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4741 parallel_devices
[i
] = NULL
;
4742 parallel_device_index
= 0;
4744 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4745 virtio_consoles
[i
] = NULL
;
4746 virtio_console_index
= 0;
4748 monitor_devices
[0] = "vc:80Cx24C";
4749 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4750 monitor_devices
[i
] = NULL
;
4752 monitor_device_index
= 0;
4754 for (i
= 0; i
< MAX_NODES
; i
++) {
4756 node_cpumask
[i
] = 0;
4772 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4774 const QEMUOption
*popt
;
4777 /* Treat --foo the same as -foo. */
4780 popt
= qemu_options
;
4783 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4787 if (!strcmp(popt
->name
, r
+ 1))
4791 if (popt
->flags
& HAS_ARG
) {
4792 if (optind
>= argc
) {
4793 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4797 optarg
= argv
[optind
++];
4802 switch(popt
->index
) {
4804 machine
= find_machine(optarg
);
4807 printf("Supported machines are:\n");
4808 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4810 printf("%-10s %s (alias of %s)\n",
4811 m
->alias
, m
->desc
, m
->name
);
4812 printf("%-10s %s%s\n",
4814 m
->is_default
? " (default)" : "");
4816 exit(*optarg
!= '?');
4819 case QEMU_OPTION_cpu
:
4820 /* hw initialization will check this */
4821 if (*optarg
== '?') {
4822 /* XXX: implement xxx_cpu_list for targets that still miss it */
4823 #if defined(cpu_list)
4824 cpu_list(stdout
, &fprintf
);
4831 case QEMU_OPTION_initrd
:
4832 initrd_filename
= optarg
;
4834 case QEMU_OPTION_hda
:
4836 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4838 hda_opts
= drive_add(optarg
, HD_ALIAS
4839 ",cyls=%d,heads=%d,secs=%d%s",
4840 0, cyls
, heads
, secs
,
4841 translation
== BIOS_ATA_TRANSLATION_LBA
?
4843 translation
== BIOS_ATA_TRANSLATION_NONE
?
4844 ",trans=none" : "");
4846 case QEMU_OPTION_hdb
:
4847 case QEMU_OPTION_hdc
:
4848 case QEMU_OPTION_hdd
:
4849 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4851 case QEMU_OPTION_drive
:
4852 drive_add(NULL
, "%s", optarg
);
4854 case QEMU_OPTION_set
:
4855 if (qemu_set_option(optarg
) != 0)
4858 case QEMU_OPTION_mtdblock
:
4859 drive_add(optarg
, MTD_ALIAS
);
4861 case QEMU_OPTION_sd
:
4862 drive_add(optarg
, SD_ALIAS
);
4864 case QEMU_OPTION_pflash
:
4865 drive_add(optarg
, PFLASH_ALIAS
);
4867 case QEMU_OPTION_snapshot
:
4870 case QEMU_OPTION_hdachs
:
4874 cyls
= strtol(p
, (char **)&p
, 0);
4875 if (cyls
< 1 || cyls
> 16383)
4880 heads
= strtol(p
, (char **)&p
, 0);
4881 if (heads
< 1 || heads
> 16)
4886 secs
= strtol(p
, (char **)&p
, 0);
4887 if (secs
< 1 || secs
> 63)
4891 if (!strcmp(p
, "none"))
4892 translation
= BIOS_ATA_TRANSLATION_NONE
;
4893 else if (!strcmp(p
, "lba"))
4894 translation
= BIOS_ATA_TRANSLATION_LBA
;
4895 else if (!strcmp(p
, "auto"))
4896 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4899 } else if (*p
!= '\0') {
4901 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4904 if (hda_opts
!= NULL
) {
4906 snprintf(num
, sizeof(num
), "%d", cyls
);
4907 qemu_opt_set(hda_opts
, "cyls", num
);
4908 snprintf(num
, sizeof(num
), "%d", heads
);
4909 qemu_opt_set(hda_opts
, "heads", num
);
4910 snprintf(num
, sizeof(num
), "%d", secs
);
4911 qemu_opt_set(hda_opts
, "secs", num
);
4912 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4913 qemu_opt_set(hda_opts
, "trans", "lba");
4914 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4915 qemu_opt_set(hda_opts
, "trans", "none");
4919 case QEMU_OPTION_numa
:
4920 if (nb_numa_nodes
>= MAX_NODES
) {
4921 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4926 case QEMU_OPTION_nographic
:
4927 display_type
= DT_NOGRAPHIC
;
4929 #ifdef CONFIG_CURSES
4930 case QEMU_OPTION_curses
:
4931 display_type
= DT_CURSES
;
4934 case QEMU_OPTION_portrait
:
4937 case QEMU_OPTION_kernel
:
4938 kernel_filename
= optarg
;
4940 case QEMU_OPTION_append
:
4941 kernel_cmdline
= optarg
;
4943 case QEMU_OPTION_cdrom
:
4944 drive_add(optarg
, CDROM_ALIAS
);
4946 case QEMU_OPTION_boot
:
4948 static const char * const params
[] = {
4949 "order", "once", "menu", NULL
4951 char buf
[sizeof(boot_devices
)];
4952 char *standard_boot_devices
;
4955 if (!strchr(optarg
, '=')) {
4957 pstrcpy(buf
, sizeof(buf
), optarg
);
4958 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4960 "qemu: unknown boot parameter '%s' in '%s'\n",
4966 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4967 boot_devices_bitmap
= parse_bootdevices(buf
);
4968 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4971 if (get_param_value(buf
, sizeof(buf
),
4973 boot_devices_bitmap
|= parse_bootdevices(buf
);
4974 standard_boot_devices
= qemu_strdup(boot_devices
);
4975 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
4976 qemu_register_reset(restore_boot_devices
,
4977 standard_boot_devices
);
4979 if (get_param_value(buf
, sizeof(buf
),
4981 if (!strcmp(buf
, "on")) {
4983 } else if (!strcmp(buf
, "off")) {
4987 "qemu: invalid option value '%s'\n",
4995 case QEMU_OPTION_fda
:
4996 case QEMU_OPTION_fdb
:
4997 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5000 case QEMU_OPTION_no_fd_bootchk
:
5004 case QEMU_OPTION_net
:
5005 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5006 fprintf(stderr
, "qemu: too many network clients\n");
5009 net_clients
[nb_net_clients
] = optarg
;
5013 case QEMU_OPTION_tftp
:
5014 legacy_tftp_prefix
= optarg
;
5016 case QEMU_OPTION_bootp
:
5017 legacy_bootp_filename
= optarg
;
5020 case QEMU_OPTION_smb
:
5021 net_slirp_smb(optarg
);
5024 case QEMU_OPTION_redir
:
5025 net_slirp_redir(optarg
);
5028 case QEMU_OPTION_bt
:
5029 add_device_config(DEV_BT
, optarg
);
5032 case QEMU_OPTION_audio_help
:
5036 case QEMU_OPTION_soundhw
:
5037 select_soundhw (optarg
);
5043 case QEMU_OPTION_version
:
5047 case QEMU_OPTION_m
: {
5051 value
= strtoul(optarg
, &ptr
, 10);
5053 case 0: case 'M': case 'm':
5060 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5064 /* On 32-bit hosts, QEMU is limited by virtual address space */
5065 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5066 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5069 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5070 fprintf(stderr
, "qemu: ram size too large\n");
5079 const CPULogItem
*item
;
5081 mask
= cpu_str_to_log_mask(optarg
);
5083 printf("Log items (comma separated):\n");
5084 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5085 printf("%-10s %s\n", item
->name
, item
->help
);
5093 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5095 case QEMU_OPTION_gdb
:
5096 gdbstub_dev
= optarg
;
5101 case QEMU_OPTION_bios
:
5104 case QEMU_OPTION_singlestep
:
5112 keyboard_layout
= optarg
;
5115 case QEMU_OPTION_localtime
:
5118 case QEMU_OPTION_vga
:
5119 select_vgahw (optarg
);
5121 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5127 w
= strtol(p
, (char **)&p
, 10);
5130 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5136 h
= strtol(p
, (char **)&p
, 10);
5141 depth
= strtol(p
, (char **)&p
, 10);
5142 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5143 depth
!= 24 && depth
!= 32)
5145 } else if (*p
== '\0') {
5146 depth
= graphic_depth
;
5153 graphic_depth
= depth
;
5157 case QEMU_OPTION_echr
:
5160 term_escape_char
= strtol(optarg
, &r
, 0);
5162 printf("Bad argument to echr\n");
5165 case QEMU_OPTION_monitor
:
5166 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5167 fprintf(stderr
, "qemu: too many monitor devices\n");
5170 monitor_devices
[monitor_device_index
] = optarg
;
5171 monitor_device_index
++;
5173 case QEMU_OPTION_chardev
:
5174 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5176 fprintf(stderr
, "parse error: %s\n", optarg
);
5179 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5183 case QEMU_OPTION_serial
:
5184 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5185 fprintf(stderr
, "qemu: too many serial ports\n");
5188 serial_devices
[serial_device_index
] = optarg
;
5189 serial_device_index
++;
5191 case QEMU_OPTION_watchdog
:
5194 "qemu: only one watchdog option may be given\n");
5199 case QEMU_OPTION_watchdog_action
:
5200 if (select_watchdog_action(optarg
) == -1) {
5201 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5205 case QEMU_OPTION_virtiocon
:
5206 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5207 fprintf(stderr
, "qemu: too many virtio consoles\n");
5210 virtio_consoles
[virtio_console_index
] = optarg
;
5211 virtio_console_index
++;
5213 case QEMU_OPTION_parallel
:
5214 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5215 fprintf(stderr
, "qemu: too many parallel ports\n");
5218 parallel_devices
[parallel_device_index
] = optarg
;
5219 parallel_device_index
++;
5221 case QEMU_OPTION_loadvm
:
5224 case QEMU_OPTION_full_screen
:
5228 case QEMU_OPTION_no_frame
:
5231 case QEMU_OPTION_alt_grab
:
5234 case QEMU_OPTION_no_quit
:
5237 case QEMU_OPTION_sdl
:
5238 display_type
= DT_SDL
;
5241 case QEMU_OPTION_pidfile
:
5245 case QEMU_OPTION_win2k_hack
:
5246 win2k_install_hack
= 1;
5248 case QEMU_OPTION_rtc_td_hack
:
5251 case QEMU_OPTION_acpitable
:
5252 if(acpi_table_add(optarg
) < 0) {
5253 fprintf(stderr
, "Wrong acpi table provided\n");
5257 case QEMU_OPTION_smbios
:
5258 if(smbios_entry_add(optarg
) < 0) {
5259 fprintf(stderr
, "Wrong smbios provided\n");
5265 case QEMU_OPTION_enable_kvm
:
5269 case QEMU_OPTION_usb
:
5272 case QEMU_OPTION_usbdevice
:
5274 add_device_config(DEV_USB
, optarg
);
5276 case QEMU_OPTION_device
:
5277 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5279 fprintf(stderr
, "parse error: %s\n", optarg
);
5283 case QEMU_OPTION_smp
:
5286 fprintf(stderr
, "Invalid number of CPUs\n");
5289 if (max_cpus
< smp_cpus
) {
5290 fprintf(stderr
, "maxcpus must be equal to or greater than "
5294 if (max_cpus
> 255) {
5295 fprintf(stderr
, "Unsupported number of maxcpus\n");
5299 case QEMU_OPTION_vnc
:
5300 display_type
= DT_VNC
;
5301 vnc_display
= optarg
;
5304 case QEMU_OPTION_no_acpi
:
5307 case QEMU_OPTION_no_hpet
:
5310 case QEMU_OPTION_balloon
:
5311 if (balloon_parse(optarg
) < 0) {
5312 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5317 case QEMU_OPTION_no_reboot
:
5320 case QEMU_OPTION_no_shutdown
:
5323 case QEMU_OPTION_show_cursor
:
5326 case QEMU_OPTION_uuid
:
5327 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5328 fprintf(stderr
, "Fail to parse UUID string."
5329 " Wrong format.\n");
5334 case QEMU_OPTION_daemonize
:
5338 case QEMU_OPTION_option_rom
:
5339 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5340 fprintf(stderr
, "Too many option ROMs\n");
5343 option_rom
[nb_option_roms
] = optarg
;
5346 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5347 case QEMU_OPTION_semihosting
:
5348 semihosting_enabled
= 1;
5351 case QEMU_OPTION_name
:
5352 qemu_name
= qemu_strdup(optarg
);
5354 char *p
= strchr(qemu_name
, ',');
5357 if (strncmp(p
, "process=", 8)) {
5358 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5366 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5367 case QEMU_OPTION_prom_env
:
5368 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5369 fprintf(stderr
, "Too many prom variables\n");
5372 prom_envs
[nb_prom_envs
] = optarg
;
5377 case QEMU_OPTION_old_param
:
5381 case QEMU_OPTION_clock
:
5382 configure_alarms(optarg
);
5384 case QEMU_OPTION_startdate
:
5387 time_t rtc_start_date
;
5388 if (!strcmp(optarg
, "now")) {
5389 rtc_date_offset
= -1;
5391 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5399 } else if (sscanf(optarg
, "%d-%d-%d",
5402 &tm
.tm_mday
) == 3) {
5411 rtc_start_date
= mktimegm(&tm
);
5412 if (rtc_start_date
== -1) {
5414 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5415 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5418 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5422 case QEMU_OPTION_tb_size
:
5423 tb_size
= strtol(optarg
, NULL
, 0);
5427 case QEMU_OPTION_icount
:
5429 if (strcmp(optarg
, "auto") == 0) {
5430 icount_time_shift
= -1;
5432 icount_time_shift
= strtol(optarg
, NULL
, 0);
5435 case QEMU_OPTION_incoming
:
5439 case QEMU_OPTION_chroot
:
5440 chroot_dir
= optarg
;
5442 case QEMU_OPTION_runas
:
5447 case QEMU_OPTION_xen_domid
:
5448 xen_domid
= atoi(optarg
);
5450 case QEMU_OPTION_xen_create
:
5451 xen_mode
= XEN_CREATE
;
5453 case QEMU_OPTION_xen_attach
:
5454 xen_mode
= XEN_ATTACH
;
5461 /* If no data_dir is specified then try to find it relative to the
5464 data_dir
= find_datadir(argv
[0]);
5466 /* If all else fails use the install patch specified when building. */
5468 data_dir
= CONFIG_QEMU_SHAREDIR
;
5472 * Default to max_cpus = smp_cpus, in case the user doesn't
5473 * specify a max_cpus value.
5476 max_cpus
= smp_cpus
;
5478 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5479 if (smp_cpus
> machine
->max_cpus
) {
5480 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5481 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5486 if (display_type
== DT_NOGRAPHIC
) {
5487 if (serial_device_index
== 0)
5488 serial_devices
[0] = "stdio";
5489 if (parallel_device_index
== 0)
5490 parallel_devices
[0] = "null";
5491 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5492 monitor_devices
[0] = "stdio";
5500 if (pipe(fds
) == -1)
5511 len
= read(fds
[0], &status
, 1);
5512 if (len
== -1 && (errno
== EINTR
))
5517 else if (status
== 1) {
5518 fprintf(stderr
, "Could not acquire pidfile\n");
5535 signal(SIGTSTP
, SIG_IGN
);
5536 signal(SIGTTOU
, SIG_IGN
);
5537 signal(SIGTTIN
, SIG_IGN
);
5540 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5543 write(fds
[1], &status
, 1);
5545 fprintf(stderr
, "Could not acquire pid file\n");
5550 if (kvm_enabled()) {
5553 ret
= kvm_init(smp_cpus
);
5555 fprintf(stderr
, "failed to initialize KVM\n");
5560 if (qemu_init_main_loop()) {
5561 fprintf(stderr
, "qemu_init_main_loop failed\n");
5564 linux_boot
= (kernel_filename
!= NULL
);
5566 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5567 fprintf(stderr
, "-append only allowed with -kernel option\n");
5571 if (!linux_boot
&& initrd_filename
!= NULL
) {
5572 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5577 /* Win32 doesn't support line-buffering and requires size >= 2 */
5578 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5582 if (init_timer_alarm() < 0) {
5583 fprintf(stderr
, "could not initialize alarm timer\n");
5586 if (use_icount
&& icount_time_shift
< 0) {
5588 /* 125MIPS seems a reasonable initial guess at the guest speed.
5589 It will be corrected fairly quickly anyway. */
5590 icount_time_shift
= 3;
5591 init_icount_adjust();
5598 /* init network clients */
5599 if (nb_net_clients
== 0) {
5600 /* if no clients, we use a default config */
5601 net_clients
[nb_net_clients
++] = "nic";
5603 net_clients
[nb_net_clients
++] = "user";
5607 for(i
= 0;i
< nb_net_clients
; i
++) {
5608 if (net_client_parse(net_clients
[i
]) < 0)
5612 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5613 net_set_boot_mask(net_boot
);
5617 /* init the bluetooth world */
5618 if (foreach_device_config(DEV_BT
, bt_parse
))
5621 /* init the memory */
5623 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5625 /* init the dynamic translator */
5626 cpu_exec_init_all(tb_size
* 1024 * 1024);
5630 /* we always create the cdrom drive, even if no disk is there */
5631 drive_add(NULL
, CDROM_ALIAS
);
5633 /* we always create at least one floppy */
5634 drive_add(NULL
, FD_ALIAS
, 0);
5636 /* we always create one sd slot, even if no card is in it */
5637 drive_add(NULL
, SD_ALIAS
);
5639 /* open the virtual block devices */
5641 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5642 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5645 vmstate_register(0, &vmstate_timers
,&timers_state
);
5646 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5648 /* Maintain compatibility with multiple stdio monitors */
5649 if (!strcmp(monitor_devices
[0],"stdio")) {
5650 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5651 const char *devname
= serial_devices
[i
];
5652 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5653 monitor_devices
[0] = NULL
;
5655 } else if (devname
&& !strcmp(devname
,"stdio")) {
5656 monitor_devices
[0] = NULL
;
5657 serial_devices
[i
] = "mon:stdio";
5663 if (nb_numa_nodes
> 0) {
5666 if (nb_numa_nodes
> smp_cpus
) {
5667 nb_numa_nodes
= smp_cpus
;
5670 /* If no memory size if given for any node, assume the default case
5671 * and distribute the available memory equally across all nodes
5673 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5674 if (node_mem
[i
] != 0)
5677 if (i
== nb_numa_nodes
) {
5678 uint64_t usedmem
= 0;
5680 /* On Linux, the each node's border has to be 8MB aligned,
5681 * the final node gets the rest.
5683 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5684 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5685 usedmem
+= node_mem
[i
];
5687 node_mem
[i
] = ram_size
- usedmem
;
5690 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5691 if (node_cpumask
[i
] != 0)
5694 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5695 * must cope with this anyway, because there are BIOSes out there in
5696 * real machines which also use this scheme.
5698 if (i
== nb_numa_nodes
) {
5699 for (i
= 0; i
< smp_cpus
; i
++) {
5700 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5705 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5706 const char *devname
= monitor_devices
[i
];
5707 if (devname
&& strcmp(devname
, "none")) {
5710 snprintf(label
, sizeof(label
), "monitor");
5712 snprintf(label
, sizeof(label
), "monitor%d", i
);
5714 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5715 if (!monitor_hds
[i
]) {
5716 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5723 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5724 const char *devname
= serial_devices
[i
];
5725 if (devname
&& strcmp(devname
, "none")) {
5727 snprintf(label
, sizeof(label
), "serial%d", i
);
5728 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5729 if (!serial_hds
[i
]) {
5730 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5737 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5738 const char *devname
= parallel_devices
[i
];
5739 if (devname
&& strcmp(devname
, "none")) {
5741 snprintf(label
, sizeof(label
), "parallel%d", i
);
5742 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5743 if (!parallel_hds
[i
]) {
5744 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5751 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5752 const char *devname
= virtio_consoles
[i
];
5753 if (devname
&& strcmp(devname
, "none")) {
5755 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5756 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5757 if (!virtcon_hds
[i
]) {
5758 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5765 module_call_init(MODULE_INIT_DEVICE
);
5768 i
= select_watchdog(watchdog
);
5770 exit (i
== 1 ? 1 : 0);
5773 if (machine
->compat_props
) {
5774 qdev_prop_register_compat(machine
->compat_props
);
5776 machine
->init(ram_size
, boot_devices
,
5777 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5781 /* must be after terminal init, SDL library changes signal handlers */
5785 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5786 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5787 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5793 current_machine
= machine
;
5795 /* init USB devices */
5797 foreach_device_config(DEV_USB
, usb_parse
);
5800 /* init generic devices */
5801 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5805 dumb_display_init();
5806 /* just use the first displaystate for the moment */
5809 if (display_type
== DT_DEFAULT
) {
5810 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5811 display_type
= DT_SDL
;
5813 display_type
= DT_VNC
;
5814 vnc_display
= "localhost:0,to=99";
5820 switch (display_type
) {
5823 #if defined(CONFIG_CURSES)
5825 curses_display_init(ds
, full_screen
);
5828 #if defined(CONFIG_SDL)
5830 sdl_display_init(ds
, full_screen
, no_frame
);
5832 #elif defined(CONFIG_COCOA)
5834 cocoa_display_init(ds
, full_screen
);
5838 vnc_display_init(ds
);
5839 if (vnc_display_open(ds
, vnc_display
) < 0)
5842 if (show_vnc_port
) {
5843 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5851 dcl
= ds
->listeners
;
5852 while (dcl
!= NULL
) {
5853 if (dcl
->dpy_refresh
!= NULL
) {
5854 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5855 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5860 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5861 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5862 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5865 text_consoles_set_display(display_state
);
5866 qemu_chr_initial_reset();
5868 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5869 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5870 monitor_init(monitor_hds
[i
],
5871 MONITOR_USE_READLINE
|
5872 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5876 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5877 const char *devname
= serial_devices
[i
];
5878 if (devname
&& strcmp(devname
, "none")) {
5879 if (strstart(devname
, "vc", 0))
5880 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5884 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5885 const char *devname
= parallel_devices
[i
];
5886 if (devname
&& strcmp(devname
, "none")) {
5887 if (strstart(devname
, "vc", 0))
5888 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5892 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5893 const char *devname
= virtio_consoles
[i
];
5894 if (virtcon_hds
[i
] && devname
) {
5895 if (strstart(devname
, "vc", 0))
5896 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5900 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5901 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5907 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5913 qemu_start_incoming_migration(incoming
);
5914 } else if (autostart
) {
5924 len
= write(fds
[1], &status
, 1);
5925 if (len
== -1 && (errno
== EINTR
))
5932 TFR(fd
= open("/dev/null", O_RDWR
));
5938 pwd
= getpwnam(run_as
);
5940 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5946 if (chroot(chroot_dir
) < 0) {
5947 fprintf(stderr
, "chroot failed\n");
5954 if (setgid(pwd
->pw_gid
) < 0) {
5955 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5958 if (setuid(pwd
->pw_uid
) < 0) {
5959 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
5962 if (setuid(0) != -1) {
5963 fprintf(stderr
, "Dropping privileges failed\n");