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
);
1585 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1587 time_t rtc_start_date
;
1590 if (!strcmp(startdate
, "now") && legacy
) {
1591 rtc_date_offset
= -1;
1593 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1601 } else if (sscanf(startdate
, "%d-%d-%d",
1604 &tm
.tm_mday
) == 3) {
1613 rtc_start_date
= mktimegm(&tm
);
1614 if (rtc_start_date
== -1) {
1616 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1617 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1620 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1624 static void configure_rtc(QemuOpts
*opts
)
1628 value
= qemu_opt_get(opts
, "base");
1630 if (!strcmp(value
, "utc")) {
1632 } else if (!strcmp(value
, "localtime")) {
1635 configure_rtc_date_offset(value
, 0);
1638 #ifdef CONFIG_TARGET_I386
1639 value
= qemu_opt_get(opts
, "driftfix");
1641 if (!strcmp(buf
, "slew")) {
1643 } else if (!strcmp(buf
, "none")) {
1646 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1654 static void socket_cleanup(void)
1659 static int socket_init(void)
1664 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1666 err
= WSAGetLastError();
1667 fprintf(stderr
, "WSAStartup: %d\n", err
);
1670 atexit(socket_cleanup
);
1675 /***********************************************************/
1676 /* Bluetooth support */
1679 static struct HCIInfo
*hci_table
[MAX_NICS
];
1681 static struct bt_vlan_s
{
1682 struct bt_scatternet_s net
;
1684 struct bt_vlan_s
*next
;
1687 /* find or alloc a new bluetooth "VLAN" */
1688 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1690 struct bt_vlan_s
**pvlan
, *vlan
;
1691 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1695 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1697 pvlan
= &first_bt_vlan
;
1698 while (*pvlan
!= NULL
)
1699 pvlan
= &(*pvlan
)->next
;
1704 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1708 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1713 static struct HCIInfo null_hci
= {
1714 .cmd_send
= null_hci_send
,
1715 .sco_send
= null_hci_send
,
1716 .acl_send
= null_hci_send
,
1717 .bdaddr_set
= null_hci_addr_set
,
1720 struct HCIInfo
*qemu_next_hci(void)
1722 if (cur_hci
== nb_hcis
)
1725 return hci_table
[cur_hci
++];
1728 static struct HCIInfo
*hci_init(const char *str
)
1731 struct bt_scatternet_s
*vlan
= 0;
1733 if (!strcmp(str
, "null"))
1736 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1738 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1739 else if (!strncmp(str
, "hci", 3)) {
1742 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1743 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1748 vlan
= qemu_find_bt_vlan(0);
1750 return bt_new_hci(vlan
);
1753 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1758 static int bt_hci_parse(const char *str
)
1760 struct HCIInfo
*hci
;
1763 if (nb_hcis
>= MAX_NICS
) {
1764 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1768 hci
= hci_init(str
);
1777 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1778 hci
->bdaddr_set(hci
, bdaddr
.b
);
1780 hci_table
[nb_hcis
++] = hci
;
1785 static void bt_vhci_add(int vlan_id
)
1787 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1790 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1791 "an empty scatternet %i\n", vlan_id
);
1793 bt_vhci_init(bt_new_hci(vlan
));
1796 static struct bt_device_s
*bt_device_add(const char *opt
)
1798 struct bt_scatternet_s
*vlan
;
1800 char *endp
= strstr(opt
, ",vlan=");
1801 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1804 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1807 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1809 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1814 vlan
= qemu_find_bt_vlan(vlan_id
);
1817 fprintf(stderr
, "qemu: warning: adding a slave device to "
1818 "an empty scatternet %i\n", vlan_id
);
1820 if (!strcmp(devname
, "keyboard"))
1821 return bt_keyboard_init(vlan
);
1823 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1827 static int bt_parse(const char *opt
)
1829 const char *endp
, *p
;
1832 if (strstart(opt
, "hci", &endp
)) {
1833 if (!*endp
|| *endp
== ',') {
1835 if (!strstart(endp
, ",vlan=", 0))
1838 return bt_hci_parse(opt
);
1840 } else if (strstart(opt
, "vhci", &endp
)) {
1841 if (!*endp
|| *endp
== ',') {
1843 if (strstart(endp
, ",vlan=", &p
)) {
1844 vlan
= strtol(p
, (char **) &endp
, 0);
1846 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1850 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1859 } else if (strstart(opt
, "device:", &endp
))
1860 return !bt_device_add(endp
);
1862 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1866 /***********************************************************/
1867 /* QEMU Block devices */
1869 #define HD_ALIAS "index=%d,media=disk"
1870 #define CDROM_ALIAS "index=2,media=cdrom"
1871 #define FD_ALIAS "index=%d,if=floppy"
1872 #define PFLASH_ALIAS "if=pflash"
1873 #define MTD_ALIAS "if=mtd"
1874 #define SD_ALIAS "index=0,if=sd"
1876 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1883 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1886 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1888 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1889 __FUNCTION__
, optstr
);
1893 qemu_opt_set(opts
, "file", file
);
1897 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1901 /* seek interface, bus and unit */
1903 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1904 if (dinfo
->type
== type
&&
1905 dinfo
->bus
== bus
&&
1906 dinfo
->unit
== unit
)
1913 DriveInfo
*drive_get_by_id(const char *id
)
1917 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1918 if (strcmp(id
, dinfo
->id
))
1925 int drive_get_max_bus(BlockInterfaceType type
)
1931 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1932 if(dinfo
->type
== type
&&
1933 dinfo
->bus
> max_bus
)
1934 max_bus
= dinfo
->bus
;
1939 const char *drive_get_serial(BlockDriverState
*bdrv
)
1943 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1944 if (dinfo
->bdrv
== bdrv
)
1945 return dinfo
->serial
;
1951 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1955 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1956 if (dinfo
->bdrv
== bdrv
)
1957 return dinfo
->onerror
;
1960 return BLOCK_ERR_STOP_ENOSPC
;
1963 static void bdrv_format_print(void *opaque
, const char *name
)
1965 fprintf(stderr
, " %s", name
);
1968 void drive_uninit(BlockDriverState
*bdrv
)
1972 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1973 if (dinfo
->bdrv
!= bdrv
)
1975 qemu_opts_del(dinfo
->opts
);
1976 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1982 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1986 const char *file
= NULL
;
1989 const char *mediastr
= "";
1990 BlockInterfaceType type
;
1991 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1992 int bus_id
, unit_id
;
1993 int cyls
, heads
, secs
, translation
;
1994 BlockDriver
*drv
= NULL
;
1995 QEMUMachine
*machine
= opaque
;
2000 int bdrv_flags
, onerror
;
2001 const char *devaddr
;
2007 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2010 if (machine
&& machine
->use_scsi
) {
2012 max_devs
= MAX_SCSI_DEVS
;
2013 pstrcpy(devname
, sizeof(devname
), "scsi");
2016 max_devs
= MAX_IDE_DEVS
;
2017 pstrcpy(devname
, sizeof(devname
), "ide");
2021 /* extract parameters */
2022 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2023 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2024 index
= qemu_opt_get_number(opts
, "index", -1);
2026 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2027 heads
= qemu_opt_get_number(opts
, "heads", 0);
2028 secs
= qemu_opt_get_number(opts
, "secs", 0);
2030 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2032 file
= qemu_opt_get(opts
, "file");
2033 serial
= qemu_opt_get(opts
, "serial");
2035 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2036 pstrcpy(devname
, sizeof(devname
), buf
);
2037 if (!strcmp(buf
, "ide")) {
2039 max_devs
= MAX_IDE_DEVS
;
2040 } else if (!strcmp(buf
, "scsi")) {
2042 max_devs
= MAX_SCSI_DEVS
;
2043 } else if (!strcmp(buf
, "floppy")) {
2046 } else if (!strcmp(buf
, "pflash")) {
2049 } else if (!strcmp(buf
, "mtd")) {
2052 } else if (!strcmp(buf
, "sd")) {
2055 } else if (!strcmp(buf
, "virtio")) {
2058 } else if (!strcmp(buf
, "xen")) {
2061 } else if (!strcmp(buf
, "none")) {
2065 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2070 if (cyls
|| heads
|| secs
) {
2071 if (cyls
< 1 || cyls
> 16383) {
2072 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2075 if (heads
< 1 || heads
> 16) {
2076 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2079 if (secs
< 1 || secs
> 63) {
2080 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2085 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2088 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2092 if (!strcmp(buf
, "none"))
2093 translation
= BIOS_ATA_TRANSLATION_NONE
;
2094 else if (!strcmp(buf
, "lba"))
2095 translation
= BIOS_ATA_TRANSLATION_LBA
;
2096 else if (!strcmp(buf
, "auto"))
2097 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2099 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2104 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2105 if (!strcmp(buf
, "disk")) {
2107 } else if (!strcmp(buf
, "cdrom")) {
2108 if (cyls
|| secs
|| heads
) {
2110 "qemu: '%s' invalid physical CHS format\n", buf
);
2113 media
= MEDIA_CDROM
;
2115 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2120 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2121 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2123 else if (!strcmp(buf
, "writethrough"))
2125 else if (!strcmp(buf
, "writeback"))
2128 fprintf(stderr
, "qemu: invalid cache option\n");
2133 #ifdef CONFIG_LINUX_AIO
2134 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2135 if (!strcmp(buf
, "threads"))
2137 else if (!strcmp(buf
, "native"))
2140 fprintf(stderr
, "qemu: invalid aio option\n");
2146 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2147 if (strcmp(buf
, "?") == 0) {
2148 fprintf(stderr
, "qemu: Supported formats:");
2149 bdrv_iterate_format(bdrv_format_print
, NULL
);
2150 fprintf(stderr
, "\n");
2153 drv
= bdrv_find_format(buf
);
2155 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2160 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2161 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2162 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2163 fprintf(stderr
, "werror is no supported by this format\n");
2166 if (!strcmp(buf
, "ignore"))
2167 onerror
= BLOCK_ERR_IGNORE
;
2168 else if (!strcmp(buf
, "enospc"))
2169 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2170 else if (!strcmp(buf
, "stop"))
2171 onerror
= BLOCK_ERR_STOP_ANY
;
2172 else if (!strcmp(buf
, "report"))
2173 onerror
= BLOCK_ERR_REPORT
;
2175 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2180 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2181 if (type
!= IF_VIRTIO
) {
2182 fprintf(stderr
, "addr is not supported\n");
2187 /* compute bus and unit according index */
2190 if (bus_id
!= 0 || unit_id
!= -1) {
2192 "qemu: index cannot be used with bus and unit\n");
2200 unit_id
= index
% max_devs
;
2201 bus_id
= index
/ max_devs
;
2205 /* if user doesn't specify a unit_id,
2206 * try to find the first free
2209 if (unit_id
== -1) {
2211 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2213 if (max_devs
&& unit_id
>= max_devs
) {
2214 unit_id
-= max_devs
;
2222 if (max_devs
&& unit_id
>= max_devs
) {
2223 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2224 unit_id
, max_devs
- 1);
2229 * ignore multiple definitions
2232 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2239 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2240 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2241 dinfo
->id
= qemu_strdup(buf
);
2243 /* no id supplied -> create one */
2244 dinfo
->id
= qemu_mallocz(32);
2245 if (type
== IF_IDE
|| type
== IF_SCSI
)
2246 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2248 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2249 devname
, bus_id
, mediastr
, unit_id
);
2251 snprintf(dinfo
->id
, 32, "%s%s%i",
2252 devname
, mediastr
, unit_id
);
2254 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2255 dinfo
->devaddr
= devaddr
;
2257 dinfo
->bus
= bus_id
;
2258 dinfo
->unit
= unit_id
;
2259 dinfo
->onerror
= onerror
;
2262 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2263 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2273 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2274 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2278 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2283 /* FIXME: This isn't really a floppy, but it's a reasonable
2286 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2292 /* add virtio block device */
2293 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2294 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2295 qemu_opt_set(opts
, "drive", dinfo
->id
);
2297 qemu_opt_set(opts
, "addr", devaddr
);
2308 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2309 cache
= 2; /* always use write-back with snapshot */
2311 if (cache
== 0) /* no caching */
2312 bdrv_flags
|= BDRV_O_NOCACHE
;
2313 else if (cache
== 2) /* write-back */
2314 bdrv_flags
|= BDRV_O_CACHE_WB
;
2317 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2319 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2322 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2323 fprintf(stderr
, "qemu: could not open disk image %s\n",
2328 if (bdrv_key_required(dinfo
->bdrv
))
2334 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2336 QEMUMachine
*machine
= opaque
;
2337 int fatal_error
= 0;
2339 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2346 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2348 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2349 qemu_opt_set(opts
, "snapshot", "on");
2354 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2356 boot_set_handler
= func
;
2357 boot_set_opaque
= opaque
;
2360 int qemu_boot_set(const char *boot_devices
)
2362 if (!boot_set_handler
) {
2365 return boot_set_handler(boot_set_opaque
, boot_devices
);
2368 static int parse_bootdevices(char *devices
)
2370 /* We just do some generic consistency checks */
2374 for (p
= devices
; *p
!= '\0'; p
++) {
2375 /* Allowed boot devices are:
2376 * a-b: floppy disk drives
2377 * c-f: IDE disk drives
2378 * g-m: machine implementation dependant drives
2379 * n-p: network devices
2380 * It's up to each machine implementation to check if the given boot
2381 * devices match the actual hardware implementation and firmware
2384 if (*p
< 'a' || *p
> 'p') {
2385 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2388 if (bitmap
& (1 << (*p
- 'a'))) {
2389 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2392 bitmap
|= 1 << (*p
- 'a');
2397 static void restore_boot_devices(void *opaque
)
2399 char *standard_boot_devices
= opaque
;
2401 qemu_boot_set(standard_boot_devices
);
2403 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2404 qemu_free(standard_boot_devices
);
2407 static void numa_add(const char *optarg
)
2411 unsigned long long value
, endvalue
;
2414 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2415 if (!strcmp(option
, "node")) {
2416 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2417 nodenr
= nb_numa_nodes
;
2419 nodenr
= strtoull(option
, NULL
, 10);
2422 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2423 node_mem
[nodenr
] = 0;
2425 value
= strtoull(option
, &endptr
, 0);
2427 case 0: case 'M': case 'm':
2434 node_mem
[nodenr
] = value
;
2436 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2437 node_cpumask
[nodenr
] = 0;
2439 value
= strtoull(option
, &endptr
, 10);
2442 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2444 if (*endptr
== '-') {
2445 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2446 if (endvalue
>= 63) {
2449 "only 63 CPUs in NUMA mode supported.\n");
2451 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2456 node_cpumask
[nodenr
] = value
;
2463 static void smp_parse(const char *optarg
)
2465 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2469 smp
= strtoul(optarg
, &endptr
, 10);
2470 if (endptr
!= optarg
) {
2471 if (*endptr
== ',') {
2475 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2476 sockets
= strtoull(option
, NULL
, 10);
2477 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2478 cores
= strtoull(option
, NULL
, 10);
2479 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2480 threads
= strtoull(option
, NULL
, 10);
2481 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2482 max_cpus
= strtoull(option
, NULL
, 10);
2484 /* compute missing values, prefer sockets over cores over threads */
2485 if (smp
== 0 || sockets
== 0) {
2486 sockets
= sockets
> 0 ? sockets
: 1;
2487 cores
= cores
> 0 ? cores
: 1;
2488 threads
= threads
> 0 ? threads
: 1;
2490 smp
= cores
* threads
* sockets
;
2492 sockets
= smp
/ (cores
* threads
);
2496 threads
= threads
> 0 ? threads
: 1;
2497 cores
= smp
/ (sockets
* threads
);
2500 sockets
= smp
/ (cores
* threads
);
2502 threads
= smp
/ (cores
* sockets
);
2507 smp_cores
= cores
> 0 ? cores
: 1;
2508 smp_threads
= threads
> 0 ? threads
: 1;
2510 max_cpus
= smp_cpus
;
2513 /***********************************************************/
2516 static void usb_msd_password_cb(void *opaque
, int err
)
2518 USBDevice
*dev
= opaque
;
2521 usb_device_attach(dev
);
2523 dev
->info
->handle_destroy(dev
);
2532 .qdev
= "QEMU USB Mouse",
2535 .qdev
= "QEMU USB Tablet",
2538 .qdev
= "QEMU USB Keyboard",
2540 .name
= "wacom-tablet",
2541 .qdev
= "QEMU PenPartner Tablet",
2545 static int usb_device_add(const char *devname
, int is_hotplug
)
2548 USBBus
*bus
= usb_bus_find(-1 /* any */);
2549 USBDevice
*dev
= NULL
;
2555 /* simple devices which don't need extra care */
2556 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2557 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2559 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2563 /* the other ones */
2564 if (strstart(devname
, "host:", &p
)) {
2565 dev
= usb_host_device_open(p
);
2566 } else if (strstart(devname
, "disk:", &p
)) {
2567 BlockDriverState
*bs
;
2569 dev
= usb_msd_init(p
);
2572 bs
= usb_msd_get_bdrv(dev
);
2573 if (bdrv_key_required(bs
)) {
2576 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2581 } else if (strstart(devname
, "serial:", &p
)) {
2582 dev
= usb_serial_init(p
);
2583 #ifdef CONFIG_BRLAPI
2584 } else if (!strcmp(devname
, "braille")) {
2585 dev
= usb_baum_init();
2587 } else if (strstart(devname
, "net:", &p
)) {
2590 if (net_client_init(NULL
, "nic", p
) < 0)
2592 nd_table
[nic
].model
= "usb";
2593 dev
= usb_net_init(&nd_table
[nic
]);
2594 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2595 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2596 bt_new_hci(qemu_find_bt_vlan(0)));
2607 static int usb_device_del(const char *devname
)
2612 if (strstart(devname
, "host:", &p
))
2613 return usb_host_device_close(p
);
2618 p
= strchr(devname
, '.');
2621 bus_num
= strtoul(devname
, NULL
, 0);
2622 addr
= strtoul(p
+ 1, NULL
, 0);
2624 return usb_device_delete_addr(bus_num
, addr
);
2627 static int usb_parse(const char *cmdline
)
2629 return usb_device_add(cmdline
, 0);
2632 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2634 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2637 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2639 usb_device_del(qdict_get_str(qdict
, "devname"));
2642 /***********************************************************/
2643 /* PCMCIA/Cardbus */
2645 static struct pcmcia_socket_entry_s
{
2646 PCMCIASocket
*socket
;
2647 struct pcmcia_socket_entry_s
*next
;
2648 } *pcmcia_sockets
= 0;
2650 void pcmcia_socket_register(PCMCIASocket
*socket
)
2652 struct pcmcia_socket_entry_s
*entry
;
2654 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2655 entry
->socket
= socket
;
2656 entry
->next
= pcmcia_sockets
;
2657 pcmcia_sockets
= entry
;
2660 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2662 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2664 ptr
= &pcmcia_sockets
;
2665 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2666 if (entry
->socket
== socket
) {
2672 void pcmcia_info(Monitor
*mon
)
2674 struct pcmcia_socket_entry_s
*iter
;
2676 if (!pcmcia_sockets
)
2677 monitor_printf(mon
, "No PCMCIA sockets\n");
2679 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2680 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2681 iter
->socket
->attached
? iter
->socket
->card_string
:
2685 /***********************************************************/
2686 /* register display */
2688 struct DisplayAllocator default_allocator
= {
2689 defaultallocator_create_displaysurface
,
2690 defaultallocator_resize_displaysurface
,
2691 defaultallocator_free_displaysurface
2694 void register_displaystate(DisplayState
*ds
)
2704 DisplayState
*get_displaystate(void)
2706 return display_state
;
2709 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2711 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2712 return ds
->allocator
;
2717 static void dumb_display_init(void)
2719 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2720 ds
->allocator
= &default_allocator
;
2721 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2722 register_displaystate(ds
);
2725 /***********************************************************/
2728 typedef struct IOHandlerRecord
{
2730 IOCanRWHandler
*fd_read_poll
;
2732 IOHandler
*fd_write
;
2735 /* temporary data */
2737 struct IOHandlerRecord
*next
;
2740 static IOHandlerRecord
*first_io_handler
;
2742 /* XXX: fd_read_poll should be suppressed, but an API change is
2743 necessary in the character devices to suppress fd_can_read(). */
2744 int qemu_set_fd_handler2(int fd
,
2745 IOCanRWHandler
*fd_read_poll
,
2747 IOHandler
*fd_write
,
2750 IOHandlerRecord
**pioh
, *ioh
;
2752 if (!fd_read
&& !fd_write
) {
2753 pioh
= &first_io_handler
;
2758 if (ioh
->fd
== fd
) {
2765 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2769 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2770 ioh
->next
= first_io_handler
;
2771 first_io_handler
= ioh
;
2774 ioh
->fd_read_poll
= fd_read_poll
;
2775 ioh
->fd_read
= fd_read
;
2776 ioh
->fd_write
= fd_write
;
2777 ioh
->opaque
= opaque
;
2783 int qemu_set_fd_handler(int fd
,
2785 IOHandler
*fd_write
,
2788 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2792 /***********************************************************/
2793 /* Polling handling */
2795 typedef struct PollingEntry
{
2798 struct PollingEntry
*next
;
2801 static PollingEntry
*first_polling_entry
;
2803 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2805 PollingEntry
**ppe
, *pe
;
2806 pe
= qemu_mallocz(sizeof(PollingEntry
));
2808 pe
->opaque
= opaque
;
2809 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2814 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2816 PollingEntry
**ppe
, *pe
;
2817 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2819 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2827 /***********************************************************/
2828 /* Wait objects support */
2829 typedef struct WaitObjects
{
2831 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2832 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2833 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2836 static WaitObjects wait_objects
= {0};
2838 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2840 WaitObjects
*w
= &wait_objects
;
2842 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2844 w
->events
[w
->num
] = handle
;
2845 w
->func
[w
->num
] = func
;
2846 w
->opaque
[w
->num
] = opaque
;
2851 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2854 WaitObjects
*w
= &wait_objects
;
2857 for (i
= 0; i
< w
->num
; i
++) {
2858 if (w
->events
[i
] == handle
)
2861 w
->events
[i
] = w
->events
[i
+ 1];
2862 w
->func
[i
] = w
->func
[i
+ 1];
2863 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2871 /***********************************************************/
2872 /* ram save/restore */
2874 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2875 #define RAM_SAVE_FLAG_COMPRESS 0x02
2876 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2877 #define RAM_SAVE_FLAG_PAGE 0x08
2878 #define RAM_SAVE_FLAG_EOS 0x10
2880 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2882 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2883 uint32_t *array
= (uint32_t *)page
;
2886 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2887 if (array
[i
] != val
)
2894 static int ram_save_block(QEMUFile
*f
)
2896 static ram_addr_t current_addr
= 0;
2897 ram_addr_t saved_addr
= current_addr
;
2898 ram_addr_t addr
= 0;
2901 while (addr
< last_ram_offset
) {
2902 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2905 cpu_physical_memory_reset_dirty(current_addr
,
2906 current_addr
+ TARGET_PAGE_SIZE
,
2907 MIGRATION_DIRTY_FLAG
);
2909 p
= qemu_get_ram_ptr(current_addr
);
2911 if (is_dup_page(p
, *p
)) {
2912 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2913 qemu_put_byte(f
, *p
);
2915 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2916 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2922 addr
+= TARGET_PAGE_SIZE
;
2923 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2929 static uint64_t bytes_transferred
= 0;
2931 static ram_addr_t
ram_save_remaining(void)
2934 ram_addr_t count
= 0;
2936 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2937 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2944 uint64_t ram_bytes_remaining(void)
2946 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2949 uint64_t ram_bytes_transferred(void)
2951 return bytes_transferred
;
2954 uint64_t ram_bytes_total(void)
2956 return last_ram_offset
;
2959 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2962 uint64_t bytes_transferred_last
;
2964 uint64_t expected_time
= 0;
2966 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2967 qemu_file_set_error(f
);
2972 /* Make sure all dirty bits are set */
2973 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2974 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2975 cpu_physical_memory_set_dirty(addr
);
2978 /* Enable dirty memory tracking */
2979 cpu_physical_memory_set_dirty_tracking(1);
2981 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2984 bytes_transferred_last
= bytes_transferred
;
2985 bwidth
= get_clock();
2987 while (!qemu_file_rate_limit(f
)) {
2990 ret
= ram_save_block(f
);
2991 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2992 if (ret
== 0) /* no more blocks */
2996 bwidth
= get_clock() - bwidth
;
2997 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2999 /* if we haven't transferred anything this round, force expected_time to a
3000 * a very high value, but without crashing */
3004 /* try transferring iterative blocks of memory */
3008 /* flush all remaining blocks regardless of rate limiting */
3009 while (ram_save_block(f
) != 0) {
3010 bytes_transferred
+= TARGET_PAGE_SIZE
;
3012 cpu_physical_memory_set_dirty_tracking(0);
3015 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3017 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3019 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3022 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3027 if (version_id
!= 3)
3031 addr
= qemu_get_be64(f
);
3033 flags
= addr
& ~TARGET_PAGE_MASK
;
3034 addr
&= TARGET_PAGE_MASK
;
3036 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3037 if (addr
!= last_ram_offset
)
3041 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3042 uint8_t ch
= qemu_get_byte(f
);
3043 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3046 (!kvm_enabled() || kvm_has_sync_mmu())) {
3047 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3050 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3051 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3052 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3057 void qemu_service_io(void)
3059 qemu_notify_event();
3062 /***********************************************************/
3063 /* bottom halves (can be seen as timers which expire ASAP) */
3074 static QEMUBH
*first_bh
= NULL
;
3076 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3079 bh
= qemu_mallocz(sizeof(QEMUBH
));
3081 bh
->opaque
= opaque
;
3082 bh
->next
= first_bh
;
3087 int qemu_bh_poll(void)
3093 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3094 if (!bh
->deleted
&& bh
->scheduled
) {
3103 /* remove deleted bhs */
3117 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3125 void qemu_bh_schedule(QEMUBH
*bh
)
3131 /* stop the currently executing CPU to execute the BH ASAP */
3132 qemu_notify_event();
3135 void qemu_bh_cancel(QEMUBH
*bh
)
3140 void qemu_bh_delete(QEMUBH
*bh
)
3146 static void qemu_bh_update_timeout(int *timeout
)
3150 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3151 if (!bh
->deleted
&& bh
->scheduled
) {
3153 /* idle bottom halves will be polled at least
3155 *timeout
= MIN(10, *timeout
);
3157 /* non-idle bottom halves will be executed
3166 /***********************************************************/
3167 /* machine registration */
3169 static QEMUMachine
*first_machine
= NULL
;
3170 QEMUMachine
*current_machine
= NULL
;
3172 int qemu_register_machine(QEMUMachine
*m
)
3175 pm
= &first_machine
;
3183 static QEMUMachine
*find_machine(const char *name
)
3187 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3188 if (!strcmp(m
->name
, name
))
3190 if (m
->alias
&& !strcmp(m
->alias
, name
))
3196 static QEMUMachine
*find_default_machine(void)
3200 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3201 if (m
->is_default
) {
3208 /***********************************************************/
3209 /* main execution loop */
3211 static void gui_update(void *opaque
)
3213 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3214 DisplayState
*ds
= opaque
;
3215 DisplayChangeListener
*dcl
= ds
->listeners
;
3219 while (dcl
!= NULL
) {
3220 if (dcl
->gui_timer_interval
&&
3221 dcl
->gui_timer_interval
< interval
)
3222 interval
= dcl
->gui_timer_interval
;
3225 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3228 static void nographic_update(void *opaque
)
3230 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3232 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3235 struct vm_change_state_entry
{
3236 VMChangeStateHandler
*cb
;
3238 QLIST_ENTRY (vm_change_state_entry
) entries
;
3241 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3243 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3246 VMChangeStateEntry
*e
;
3248 e
= qemu_mallocz(sizeof (*e
));
3252 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3256 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3258 QLIST_REMOVE (e
, entries
);
3262 static void vm_state_notify(int running
, int reason
)
3264 VMChangeStateEntry
*e
;
3266 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3267 e
->cb(e
->opaque
, running
, reason
);
3271 static void resume_all_vcpus(void);
3272 static void pause_all_vcpus(void);
3279 vm_state_notify(1, 0);
3280 qemu_rearm_alarm_timer(alarm_timer
);
3285 /* reset/shutdown handler */
3287 typedef struct QEMUResetEntry
{
3288 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3289 QEMUResetHandler
*func
;
3293 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3294 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3295 static int reset_requested
;
3296 static int shutdown_requested
;
3297 static int powerdown_requested
;
3298 static int debug_requested
;
3299 static int vmstop_requested
;
3301 int qemu_shutdown_requested(void)
3303 int r
= shutdown_requested
;
3304 shutdown_requested
= 0;
3308 int qemu_reset_requested(void)
3310 int r
= reset_requested
;
3311 reset_requested
= 0;
3315 int qemu_powerdown_requested(void)
3317 int r
= powerdown_requested
;
3318 powerdown_requested
= 0;
3322 static int qemu_debug_requested(void)
3324 int r
= debug_requested
;
3325 debug_requested
= 0;
3329 static int qemu_vmstop_requested(void)
3331 int r
= vmstop_requested
;
3332 vmstop_requested
= 0;
3336 static void do_vm_stop(int reason
)
3339 cpu_disable_ticks();
3342 vm_state_notify(0, reason
);
3346 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3348 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3351 re
->opaque
= opaque
;
3352 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3355 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3359 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3360 if (re
->func
== func
&& re
->opaque
== opaque
) {
3361 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3368 void qemu_system_reset(void)
3370 QEMUResetEntry
*re
, *nre
;
3372 /* reset all devices */
3373 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3374 re
->func(re
->opaque
);
3378 void qemu_system_reset_request(void)
3381 shutdown_requested
= 1;
3383 reset_requested
= 1;
3385 qemu_notify_event();
3388 void qemu_system_shutdown_request(void)
3390 shutdown_requested
= 1;
3391 qemu_notify_event();
3394 void qemu_system_powerdown_request(void)
3396 powerdown_requested
= 1;
3397 qemu_notify_event();
3400 #ifdef CONFIG_IOTHREAD
3401 static void qemu_system_vmstop_request(int reason
)
3403 vmstop_requested
= reason
;
3404 qemu_notify_event();
3409 static int io_thread_fd
= -1;
3411 static void qemu_event_increment(void)
3413 static const char byte
= 0;
3415 if (io_thread_fd
== -1)
3418 write(io_thread_fd
, &byte
, sizeof(byte
));
3421 static void qemu_event_read(void *opaque
)
3423 int fd
= (unsigned long)opaque
;
3426 /* Drain the notify pipe */
3429 len
= read(fd
, buffer
, sizeof(buffer
));
3430 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3433 static int qemu_event_init(void)
3442 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3446 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3450 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3451 (void *)(unsigned long)fds
[0]);
3453 io_thread_fd
= fds
[1];
3462 HANDLE qemu_event_handle
;
3464 static void dummy_event_handler(void *opaque
)
3468 static int qemu_event_init(void)
3470 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3471 if (!qemu_event_handle
) {
3472 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3475 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3479 static void qemu_event_increment(void)
3481 if (!SetEvent(qemu_event_handle
)) {
3482 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3489 static int cpu_can_run(CPUState
*env
)
3498 #ifndef CONFIG_IOTHREAD
3499 static int qemu_init_main_loop(void)
3501 return qemu_event_init();
3504 void qemu_init_vcpu(void *_env
)
3506 CPUState
*env
= _env
;
3510 env
->nr_cores
= smp_cores
;
3511 env
->nr_threads
= smp_threads
;
3515 int qemu_cpu_self(void *env
)
3520 static void resume_all_vcpus(void)
3524 static void pause_all_vcpus(void)
3528 void qemu_cpu_kick(void *env
)
3533 void qemu_notify_event(void)
3535 CPUState
*env
= cpu_single_env
;
3542 #define qemu_mutex_lock_iothread() do { } while (0)
3543 #define qemu_mutex_unlock_iothread() do { } while (0)
3545 void vm_stop(int reason
)
3550 #else /* CONFIG_IOTHREAD */
3552 #include "qemu-thread.h"
3554 QemuMutex qemu_global_mutex
;
3555 static QemuMutex qemu_fair_mutex
;
3557 static QemuThread io_thread
;
3559 static QemuThread
*tcg_cpu_thread
;
3560 static QemuCond
*tcg_halt_cond
;
3562 static int qemu_system_ready
;
3564 static QemuCond qemu_cpu_cond
;
3566 static QemuCond qemu_system_cond
;
3567 static QemuCond qemu_pause_cond
;
3569 static void block_io_signals(void);
3570 static void unblock_io_signals(void);
3571 static int tcg_has_work(void);
3573 static int qemu_init_main_loop(void)
3577 ret
= qemu_event_init();
3581 qemu_cond_init(&qemu_pause_cond
);
3582 qemu_mutex_init(&qemu_fair_mutex
);
3583 qemu_mutex_init(&qemu_global_mutex
);
3584 qemu_mutex_lock(&qemu_global_mutex
);
3586 unblock_io_signals();
3587 qemu_thread_self(&io_thread
);
3592 static void qemu_wait_io_event(CPUState
*env
)
3594 while (!tcg_has_work())
3595 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3597 qemu_mutex_unlock(&qemu_global_mutex
);
3600 * Users of qemu_global_mutex can be starved, having no chance
3601 * to acquire it since this path will get to it first.
3602 * So use another lock to provide fairness.
3604 qemu_mutex_lock(&qemu_fair_mutex
);
3605 qemu_mutex_unlock(&qemu_fair_mutex
);
3607 qemu_mutex_lock(&qemu_global_mutex
);
3611 qemu_cond_signal(&qemu_pause_cond
);
3615 static int qemu_cpu_exec(CPUState
*env
);
3617 static void *kvm_cpu_thread_fn(void *arg
)
3619 CPUState
*env
= arg
;
3622 qemu_thread_self(env
->thread
);
3626 /* signal CPU creation */
3627 qemu_mutex_lock(&qemu_global_mutex
);
3629 qemu_cond_signal(&qemu_cpu_cond
);
3631 /* and wait for machine initialization */
3632 while (!qemu_system_ready
)
3633 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3636 if (cpu_can_run(env
))
3638 qemu_wait_io_event(env
);
3644 static void tcg_cpu_exec(void);
3646 static void *tcg_cpu_thread_fn(void *arg
)
3648 CPUState
*env
= arg
;
3651 qemu_thread_self(env
->thread
);
3653 /* signal CPU creation */
3654 qemu_mutex_lock(&qemu_global_mutex
);
3655 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3657 qemu_cond_signal(&qemu_cpu_cond
);
3659 /* and wait for machine initialization */
3660 while (!qemu_system_ready
)
3661 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3665 qemu_wait_io_event(cur_cpu
);
3671 void qemu_cpu_kick(void *_env
)
3673 CPUState
*env
= _env
;
3674 qemu_cond_broadcast(env
->halt_cond
);
3676 qemu_thread_signal(env
->thread
, SIGUSR1
);
3679 int qemu_cpu_self(void *_env
)
3681 CPUState
*env
= _env
;
3684 qemu_thread_self(&this);
3686 return qemu_thread_equal(&this, env
->thread
);
3689 static void cpu_signal(int sig
)
3692 cpu_exit(cpu_single_env
);
3695 static void block_io_signals(void)
3698 struct sigaction sigact
;
3701 sigaddset(&set
, SIGUSR2
);
3702 sigaddset(&set
, SIGIO
);
3703 sigaddset(&set
, SIGALRM
);
3704 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3707 sigaddset(&set
, SIGUSR1
);
3708 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3710 memset(&sigact
, 0, sizeof(sigact
));
3711 sigact
.sa_handler
= cpu_signal
;
3712 sigaction(SIGUSR1
, &sigact
, NULL
);
3715 static void unblock_io_signals(void)
3720 sigaddset(&set
, SIGUSR2
);
3721 sigaddset(&set
, SIGIO
);
3722 sigaddset(&set
, SIGALRM
);
3723 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3726 sigaddset(&set
, SIGUSR1
);
3727 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3730 static void qemu_signal_lock(unsigned int msecs
)
3732 qemu_mutex_lock(&qemu_fair_mutex
);
3734 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3735 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3736 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3739 qemu_mutex_unlock(&qemu_fair_mutex
);
3742 static void qemu_mutex_lock_iothread(void)
3744 if (kvm_enabled()) {
3745 qemu_mutex_lock(&qemu_fair_mutex
);
3746 qemu_mutex_lock(&qemu_global_mutex
);
3747 qemu_mutex_unlock(&qemu_fair_mutex
);
3749 qemu_signal_lock(100);
3752 static void qemu_mutex_unlock_iothread(void)
3754 qemu_mutex_unlock(&qemu_global_mutex
);
3757 static int all_vcpus_paused(void)
3759 CPUState
*penv
= first_cpu
;
3764 penv
= (CPUState
*)penv
->next_cpu
;
3770 static void pause_all_vcpus(void)
3772 CPUState
*penv
= first_cpu
;
3776 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3777 qemu_cpu_kick(penv
);
3778 penv
= (CPUState
*)penv
->next_cpu
;
3781 while (!all_vcpus_paused()) {
3782 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3785 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3786 penv
= (CPUState
*)penv
->next_cpu
;
3791 static void resume_all_vcpus(void)
3793 CPUState
*penv
= first_cpu
;
3798 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3799 qemu_cpu_kick(penv
);
3800 penv
= (CPUState
*)penv
->next_cpu
;
3804 static void tcg_init_vcpu(void *_env
)
3806 CPUState
*env
= _env
;
3807 /* share a single thread for all cpus with TCG */
3808 if (!tcg_cpu_thread
) {
3809 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3810 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3811 qemu_cond_init(env
->halt_cond
);
3812 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3813 while (env
->created
== 0)
3814 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3815 tcg_cpu_thread
= env
->thread
;
3816 tcg_halt_cond
= env
->halt_cond
;
3818 env
->thread
= tcg_cpu_thread
;
3819 env
->halt_cond
= tcg_halt_cond
;
3823 static void kvm_start_vcpu(CPUState
*env
)
3825 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3826 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3827 qemu_cond_init(env
->halt_cond
);
3828 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3829 while (env
->created
== 0)
3830 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3833 void qemu_init_vcpu(void *_env
)
3835 CPUState
*env
= _env
;
3838 kvm_start_vcpu(env
);
3841 env
->nr_cores
= smp_cores
;
3842 env
->nr_threads
= smp_threads
;
3845 void qemu_notify_event(void)
3847 qemu_event_increment();
3850 void vm_stop(int reason
)
3853 qemu_thread_self(&me
);
3855 if (!qemu_thread_equal(&me
, &io_thread
)) {
3856 qemu_system_vmstop_request(reason
);
3858 * FIXME: should not return to device code in case
3859 * vm_stop() has been requested.
3861 if (cpu_single_env
) {
3862 cpu_exit(cpu_single_env
);
3863 cpu_single_env
->stop
= 1;
3874 static void host_main_loop_wait(int *timeout
)
3880 /* XXX: need to suppress polling by better using win32 events */
3882 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3883 ret
|= pe
->func(pe
->opaque
);
3887 WaitObjects
*w
= &wait_objects
;
3889 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3890 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3891 if (w
->func
[ret
- WAIT_OBJECT_0
])
3892 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3894 /* Check for additional signaled events */
3895 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3897 /* Check if event is signaled */
3898 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3899 if(ret2
== WAIT_OBJECT_0
) {
3901 w
->func
[i
](w
->opaque
[i
]);
3902 } else if (ret2
== WAIT_TIMEOUT
) {
3904 err
= GetLastError();
3905 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3908 } else if (ret
== WAIT_TIMEOUT
) {
3910 err
= GetLastError();
3911 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3918 static void host_main_loop_wait(int *timeout
)
3923 void main_loop_wait(int timeout
)
3925 IOHandlerRecord
*ioh
;
3926 fd_set rfds
, wfds
, xfds
;
3930 qemu_bh_update_timeout(&timeout
);
3932 host_main_loop_wait(&timeout
);
3934 /* poll any events */
3935 /* XXX: separate device handlers from system ones */
3940 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3944 (!ioh
->fd_read_poll
||
3945 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3946 FD_SET(ioh
->fd
, &rfds
);
3950 if (ioh
->fd_write
) {
3951 FD_SET(ioh
->fd
, &wfds
);
3957 tv
.tv_sec
= timeout
/ 1000;
3958 tv
.tv_usec
= (timeout
% 1000) * 1000;
3960 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3962 qemu_mutex_unlock_iothread();
3963 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3964 qemu_mutex_lock_iothread();
3966 IOHandlerRecord
**pioh
;
3968 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3969 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3970 ioh
->fd_read(ioh
->opaque
);
3972 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3973 ioh
->fd_write(ioh
->opaque
);
3977 /* remove deleted IO handlers */
3978 pioh
= &first_io_handler
;
3989 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3991 /* rearm timer, if not periodic */
3992 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3993 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3994 qemu_rearm_alarm_timer(alarm_timer
);
3997 /* vm time timers */
3999 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4000 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
4001 qemu_get_clock(vm_clock
));
4004 /* real time timers */
4005 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
4006 qemu_get_clock(rt_clock
));
4008 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
4009 qemu_get_clock(host_clock
));
4011 /* Check bottom-halves last in case any of the earlier events triggered
4017 static int qemu_cpu_exec(CPUState
*env
)
4020 #ifdef CONFIG_PROFILER
4024 #ifdef CONFIG_PROFILER
4025 ti
= profile_getclock();
4030 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4031 env
->icount_decr
.u16
.low
= 0;
4032 env
->icount_extra
= 0;
4033 count
= qemu_next_deadline();
4034 count
= (count
+ (1 << icount_time_shift
) - 1)
4035 >> icount_time_shift
;
4036 qemu_icount
+= count
;
4037 decr
= (count
> 0xffff) ? 0xffff : count
;
4039 env
->icount_decr
.u16
.low
= decr
;
4040 env
->icount_extra
= count
;
4042 ret
= cpu_exec(env
);
4043 #ifdef CONFIG_PROFILER
4044 qemu_time
+= profile_getclock() - ti
;
4047 /* Fold pending instructions back into the
4048 instruction counter, and clear the interrupt flag. */
4049 qemu_icount
-= (env
->icount_decr
.u16
.low
4050 + env
->icount_extra
);
4051 env
->icount_decr
.u32
= 0;
4052 env
->icount_extra
= 0;
4057 static void tcg_cpu_exec(void)
4061 if (next_cpu
== NULL
)
4062 next_cpu
= first_cpu
;
4063 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4064 CPUState
*env
= cur_cpu
= next_cpu
;
4068 if (timer_alarm_pending
) {
4069 timer_alarm_pending
= 0;
4072 if (cpu_can_run(env
))
4073 ret
= qemu_cpu_exec(env
);
4074 if (ret
== EXCP_DEBUG
) {
4075 gdb_set_stop_cpu(env
);
4076 debug_requested
= 1;
4082 static int cpu_has_work(CPUState
*env
)
4090 if (qemu_cpu_has_work(env
))
4095 static int tcg_has_work(void)
4099 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4100 if (cpu_has_work(env
))
4105 static int qemu_calculate_timeout(void)
4107 #ifndef CONFIG_IOTHREAD
4112 else if (tcg_has_work())
4114 else if (!use_icount
)
4117 /* XXX: use timeout computed from timers */
4120 /* Advance virtual time to the next event. */
4121 if (use_icount
== 1) {
4122 /* When not using an adaptive execution frequency
4123 we tend to get badly out of sync with real time,
4124 so just delay for a reasonable amount of time. */
4127 delta
= cpu_get_icount() - cpu_get_clock();
4130 /* If virtual time is ahead of real time then just
4132 timeout
= (delta
/ 1000000) + 1;
4134 /* Wait for either IO to occur or the next
4136 add
= qemu_next_deadline();
4137 /* We advance the timer before checking for IO.
4138 Limit the amount we advance so that early IO
4139 activity won't get the guest too far ahead. */
4143 add
= (add
+ (1 << icount_time_shift
) - 1)
4144 >> icount_time_shift
;
4146 timeout
= delta
/ 1000000;
4153 #else /* CONFIG_IOTHREAD */
4158 static int vm_can_run(void)
4160 if (powerdown_requested
)
4162 if (reset_requested
)
4164 if (shutdown_requested
)
4166 if (debug_requested
)
4171 qemu_irq qemu_system_powerdown
;
4173 static void main_loop(void)
4177 #ifdef CONFIG_IOTHREAD
4178 qemu_system_ready
= 1;
4179 qemu_cond_broadcast(&qemu_system_cond
);
4184 #ifdef CONFIG_PROFILER
4187 #ifndef CONFIG_IOTHREAD
4190 #ifdef CONFIG_PROFILER
4191 ti
= profile_getclock();
4193 main_loop_wait(qemu_calculate_timeout());
4194 #ifdef CONFIG_PROFILER
4195 dev_time
+= profile_getclock() - ti
;
4197 } while (vm_can_run());
4199 if (qemu_debug_requested())
4200 vm_stop(EXCP_DEBUG
);
4201 if (qemu_shutdown_requested()) {
4208 if (qemu_reset_requested()) {
4210 qemu_system_reset();
4213 if (qemu_powerdown_requested()) {
4214 qemu_irq_raise(qemu_system_powerdown
);
4216 if ((r
= qemu_vmstop_requested()))
4222 static void version(void)
4224 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4227 static void help(int exitcode
)
4230 printf("usage: %s [options] [disk_image]\n"
4232 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4234 #define DEF(option, opt_arg, opt_enum, opt_help) \
4236 #define DEFHEADING(text) stringify(text) "\n"
4237 #include "qemu-options.h"
4242 "During emulation, the following keys are useful:\n"
4243 "ctrl-alt-f toggle full screen\n"
4244 "ctrl-alt-n switch to virtual console 'n'\n"
4245 "ctrl-alt toggle mouse and keyboard grab\n"
4247 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4252 DEFAULT_NETWORK_SCRIPT
,
4253 DEFAULT_NETWORK_DOWN_SCRIPT
,
4255 DEFAULT_GDBSTUB_PORT
,
4260 #define HAS_ARG 0x0001
4263 #define DEF(option, opt_arg, opt_enum, opt_help) \
4265 #define DEFHEADING(text)
4266 #include "qemu-options.h"
4272 typedef struct QEMUOption
{
4278 static const QEMUOption qemu_options
[] = {
4279 { "h", 0, QEMU_OPTION_h
},
4280 #define DEF(option, opt_arg, opt_enum, opt_help) \
4281 { option, opt_arg, opt_enum },
4282 #define DEFHEADING(text)
4283 #include "qemu-options.h"
4291 struct soundhw soundhw
[] = {
4292 #ifdef HAS_AUDIO_CHOICE
4293 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4299 { .init_isa
= pcspk_audio_init
}
4306 "Creative Sound Blaster 16",
4309 { .init_isa
= SB16_init
}
4313 #ifdef CONFIG_CS4231A
4319 { .init_isa
= cs4231a_init
}
4327 "Yamaha YMF262 (OPL3)",
4329 "Yamaha YM3812 (OPL2)",
4333 { .init_isa
= Adlib_init
}
4340 "Gravis Ultrasound GF1",
4343 { .init_isa
= GUS_init
}
4350 "Intel 82801AA AC97 Audio",
4353 { .init_pci
= ac97_init
}
4357 #ifdef CONFIG_ES1370
4360 "ENSONIQ AudioPCI ES1370",
4363 { .init_pci
= es1370_init
}
4367 #endif /* HAS_AUDIO_CHOICE */
4369 { NULL
, NULL
, 0, 0, { NULL
} }
4372 static void select_soundhw (const char *optarg
)
4376 if (*optarg
== '?') {
4379 printf ("Valid sound card names (comma separated):\n");
4380 for (c
= soundhw
; c
->name
; ++c
) {
4381 printf ("%-11s %s\n", c
->name
, c
->descr
);
4383 printf ("\n-soundhw all will enable all of the above\n");
4384 exit (*optarg
!= '?');
4392 if (!strcmp (optarg
, "all")) {
4393 for (c
= soundhw
; c
->name
; ++c
) {
4401 e
= strchr (p
, ',');
4402 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4404 for (c
= soundhw
; c
->name
; ++c
) {
4405 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4414 "Unknown sound card name (too big to show)\n");
4417 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4422 p
+= l
+ (e
!= NULL
);
4426 goto show_valid_cards
;
4431 static void select_vgahw (const char *p
)
4435 vga_interface_type
= VGA_NONE
;
4436 if (strstart(p
, "std", &opts
)) {
4437 vga_interface_type
= VGA_STD
;
4438 } else if (strstart(p
, "cirrus", &opts
)) {
4439 vga_interface_type
= VGA_CIRRUS
;
4440 } else if (strstart(p
, "vmware", &opts
)) {
4441 vga_interface_type
= VGA_VMWARE
;
4442 } else if (strstart(p
, "xenfb", &opts
)) {
4443 vga_interface_type
= VGA_XENFB
;
4444 } else if (!strstart(p
, "none", &opts
)) {
4446 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4450 const char *nextopt
;
4452 if (strstart(opts
, ",retrace=", &nextopt
)) {
4454 if (strstart(opts
, "dumb", &nextopt
))
4455 vga_retrace_method
= VGA_RETRACE_DUMB
;
4456 else if (strstart(opts
, "precise", &nextopt
))
4457 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4458 else goto invalid_vga
;
4459 } else goto invalid_vga
;
4465 static int balloon_parse(const char *arg
)
4469 if (strcmp(arg
, "none") == 0) {
4473 if (!strncmp(arg
, "virtio", 6)) {
4474 if (arg
[6] == ',') {
4475 /* have params -> parse them */
4476 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4480 /* create empty opts */
4481 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4483 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4492 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4494 exit(STATUS_CONTROL_C_EXIT
);
4499 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4503 if(strlen(str
) != 36)
4506 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4507 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4508 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4514 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4520 #define MAX_NET_CLIENTS 32
4524 static void termsig_handler(int signal
)
4526 qemu_system_shutdown_request();
4529 static void sigchld_handler(int signal
)
4531 waitpid(-1, NULL
, WNOHANG
);
4534 static void sighandler_setup(void)
4536 struct sigaction act
;
4538 memset(&act
, 0, sizeof(act
));
4539 act
.sa_handler
= termsig_handler
;
4540 sigaction(SIGINT
, &act
, NULL
);
4541 sigaction(SIGHUP
, &act
, NULL
);
4542 sigaction(SIGTERM
, &act
, NULL
);
4544 act
.sa_handler
= sigchld_handler
;
4545 act
.sa_flags
= SA_NOCLDSTOP
;
4546 sigaction(SIGCHLD
, &act
, NULL
);
4552 /* Look for support files in the same directory as the executable. */
4553 static char *find_datadir(const char *argv0
)
4559 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4566 while (p
!= buf
&& *p
!= '\\')
4569 if (access(buf
, R_OK
) == 0) {
4570 return qemu_strdup(buf
);
4576 /* Find a likely location for support files using the location of the binary.
4577 For installed binaries this will be "$bindir/../share/qemu". When
4578 running from the build tree this will be "$bindir/../pc-bios". */
4579 #define SHARE_SUFFIX "/share/qemu"
4580 #define BUILD_SUFFIX "/pc-bios"
4581 static char *find_datadir(const char *argv0
)
4589 #if defined(__linux__)
4592 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4598 #elif defined(__FreeBSD__)
4601 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4608 /* If we don't have any way of figuring out the actual executable
4609 location then try argv[0]. */
4611 p
= realpath(argv0
, buf
);
4619 max_len
= strlen(dir
) +
4620 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4621 res
= qemu_mallocz(max_len
);
4622 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4623 if (access(res
, R_OK
)) {
4624 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4625 if (access(res
, R_OK
)) {
4637 char *qemu_find_file(int type
, const char *name
)
4643 /* If name contains path separators then try it as a straight path. */
4644 if ((strchr(name
, '/') || strchr(name
, '\\'))
4645 && access(name
, R_OK
) == 0) {
4646 return qemu_strdup(name
);
4649 case QEMU_FILE_TYPE_BIOS
:
4652 case QEMU_FILE_TYPE_KEYMAP
:
4653 subdir
= "keymaps/";
4658 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4659 buf
= qemu_mallocz(len
);
4660 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4661 if (access(buf
, R_OK
)) {
4668 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4672 dev
= qdev_device_add(opts
);
4678 struct device_config
{
4680 DEV_USB
, /* -usbdevice */
4683 const char *cmdline
;
4684 QTAILQ_ENTRY(device_config
) next
;
4686 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4688 static void add_device_config(int type
, const char *cmdline
)
4690 struct device_config
*conf
;
4692 conf
= qemu_mallocz(sizeof(*conf
));
4694 conf
->cmdline
= cmdline
;
4695 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4698 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4700 struct device_config
*conf
;
4703 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4704 if (conf
->type
!= type
)
4706 rc
= func(conf
->cmdline
);
4713 int main(int argc
, char **argv
, char **envp
)
4715 const char *gdbstub_dev
= NULL
;
4716 uint32_t boot_devices_bitmap
= 0;
4718 int snapshot
, linux_boot
, net_boot
;
4719 const char *initrd_filename
;
4720 const char *kernel_filename
, *kernel_cmdline
;
4721 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4723 DisplayChangeListener
*dcl
;
4724 int cyls
, heads
, secs
, translation
;
4725 const char *net_clients
[MAX_NET_CLIENTS
];
4727 QemuOpts
*hda_opts
= NULL
, *opts
;
4729 const char *r
, *optarg
;
4730 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4731 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4732 int monitor_device_index
;
4733 const char *serial_devices
[MAX_SERIAL_PORTS
];
4734 int serial_device_index
;
4735 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4736 int parallel_device_index
;
4737 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4738 int virtio_console_index
;
4739 const char *loadvm
= NULL
;
4740 QEMUMachine
*machine
;
4741 const char *cpu_model
;
4746 const char *pid_file
= NULL
;
4747 const char *incoming
= NULL
;
4750 struct passwd
*pwd
= NULL
;
4751 const char *chroot_dir
= NULL
;
4752 const char *run_as
= NULL
;
4755 int show_vnc_port
= 0;
4757 qemu_errors_to_file(stderr
);
4758 qemu_cache_utils_init(envp
);
4760 QLIST_INIT (&vm_change_state_head
);
4763 struct sigaction act
;
4764 sigfillset(&act
.sa_mask
);
4766 act
.sa_handler
= SIG_IGN
;
4767 sigaction(SIGPIPE
, &act
, NULL
);
4770 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4771 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4772 QEMU to run on a single CPU */
4777 h
= GetCurrentProcess();
4778 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4779 for(i
= 0; i
< 32; i
++) {
4780 if (mask
& (1 << i
))
4785 SetProcessAffinityMask(h
, mask
);
4791 module_call_init(MODULE_INIT_MACHINE
);
4792 machine
= find_default_machine();
4794 initrd_filename
= NULL
;
4797 kernel_filename
= NULL
;
4798 kernel_cmdline
= "";
4799 cyls
= heads
= secs
= 0;
4800 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4802 serial_devices
[0] = "vc:80Cx24C";
4803 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4804 serial_devices
[i
] = NULL
;
4805 serial_device_index
= 0;
4807 parallel_devices
[0] = "vc:80Cx24C";
4808 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4809 parallel_devices
[i
] = NULL
;
4810 parallel_device_index
= 0;
4812 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4813 virtio_consoles
[i
] = NULL
;
4814 virtio_console_index
= 0;
4816 monitor_devices
[0] = "vc:80Cx24C";
4817 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4818 monitor_devices
[i
] = NULL
;
4820 monitor_device_index
= 0;
4822 for (i
= 0; i
< MAX_NODES
; i
++) {
4824 node_cpumask
[i
] = 0;
4840 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4842 const QEMUOption
*popt
;
4845 /* Treat --foo the same as -foo. */
4848 popt
= qemu_options
;
4851 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4855 if (!strcmp(popt
->name
, r
+ 1))
4859 if (popt
->flags
& HAS_ARG
) {
4860 if (optind
>= argc
) {
4861 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4865 optarg
= argv
[optind
++];
4870 switch(popt
->index
) {
4872 machine
= find_machine(optarg
);
4875 printf("Supported machines are:\n");
4876 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4878 printf("%-10s %s (alias of %s)\n",
4879 m
->alias
, m
->desc
, m
->name
);
4880 printf("%-10s %s%s\n",
4882 m
->is_default
? " (default)" : "");
4884 exit(*optarg
!= '?');
4887 case QEMU_OPTION_cpu
:
4888 /* hw initialization will check this */
4889 if (*optarg
== '?') {
4890 /* XXX: implement xxx_cpu_list for targets that still miss it */
4891 #if defined(cpu_list)
4892 cpu_list(stdout
, &fprintf
);
4899 case QEMU_OPTION_initrd
:
4900 initrd_filename
= optarg
;
4902 case QEMU_OPTION_hda
:
4904 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4906 hda_opts
= drive_add(optarg
, HD_ALIAS
4907 ",cyls=%d,heads=%d,secs=%d%s",
4908 0, cyls
, heads
, secs
,
4909 translation
== BIOS_ATA_TRANSLATION_LBA
?
4911 translation
== BIOS_ATA_TRANSLATION_NONE
?
4912 ",trans=none" : "");
4914 case QEMU_OPTION_hdb
:
4915 case QEMU_OPTION_hdc
:
4916 case QEMU_OPTION_hdd
:
4917 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4919 case QEMU_OPTION_drive
:
4920 drive_add(NULL
, "%s", optarg
);
4922 case QEMU_OPTION_set
:
4923 if (qemu_set_option(optarg
) != 0)
4926 case QEMU_OPTION_mtdblock
:
4927 drive_add(optarg
, MTD_ALIAS
);
4929 case QEMU_OPTION_sd
:
4930 drive_add(optarg
, SD_ALIAS
);
4932 case QEMU_OPTION_pflash
:
4933 drive_add(optarg
, PFLASH_ALIAS
);
4935 case QEMU_OPTION_snapshot
:
4938 case QEMU_OPTION_hdachs
:
4942 cyls
= strtol(p
, (char **)&p
, 0);
4943 if (cyls
< 1 || cyls
> 16383)
4948 heads
= strtol(p
, (char **)&p
, 0);
4949 if (heads
< 1 || heads
> 16)
4954 secs
= strtol(p
, (char **)&p
, 0);
4955 if (secs
< 1 || secs
> 63)
4959 if (!strcmp(p
, "none"))
4960 translation
= BIOS_ATA_TRANSLATION_NONE
;
4961 else if (!strcmp(p
, "lba"))
4962 translation
= BIOS_ATA_TRANSLATION_LBA
;
4963 else if (!strcmp(p
, "auto"))
4964 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4967 } else if (*p
!= '\0') {
4969 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4972 if (hda_opts
!= NULL
) {
4974 snprintf(num
, sizeof(num
), "%d", cyls
);
4975 qemu_opt_set(hda_opts
, "cyls", num
);
4976 snprintf(num
, sizeof(num
), "%d", heads
);
4977 qemu_opt_set(hda_opts
, "heads", num
);
4978 snprintf(num
, sizeof(num
), "%d", secs
);
4979 qemu_opt_set(hda_opts
, "secs", num
);
4980 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4981 qemu_opt_set(hda_opts
, "trans", "lba");
4982 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4983 qemu_opt_set(hda_opts
, "trans", "none");
4987 case QEMU_OPTION_numa
:
4988 if (nb_numa_nodes
>= MAX_NODES
) {
4989 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4994 case QEMU_OPTION_nographic
:
4995 display_type
= DT_NOGRAPHIC
;
4997 #ifdef CONFIG_CURSES
4998 case QEMU_OPTION_curses
:
4999 display_type
= DT_CURSES
;
5002 case QEMU_OPTION_portrait
:
5005 case QEMU_OPTION_kernel
:
5006 kernel_filename
= optarg
;
5008 case QEMU_OPTION_append
:
5009 kernel_cmdline
= optarg
;
5011 case QEMU_OPTION_cdrom
:
5012 drive_add(optarg
, CDROM_ALIAS
);
5014 case QEMU_OPTION_boot
:
5016 static const char * const params
[] = {
5017 "order", "once", "menu", NULL
5019 char buf
[sizeof(boot_devices
)];
5020 char *standard_boot_devices
;
5023 if (!strchr(optarg
, '=')) {
5025 pstrcpy(buf
, sizeof(buf
), optarg
);
5026 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5028 "qemu: unknown boot parameter '%s' in '%s'\n",
5034 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5035 boot_devices_bitmap
= parse_bootdevices(buf
);
5036 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5039 if (get_param_value(buf
, sizeof(buf
),
5041 boot_devices_bitmap
|= parse_bootdevices(buf
);
5042 standard_boot_devices
= qemu_strdup(boot_devices
);
5043 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5044 qemu_register_reset(restore_boot_devices
,
5045 standard_boot_devices
);
5047 if (get_param_value(buf
, sizeof(buf
),
5049 if (!strcmp(buf
, "on")) {
5051 } else if (!strcmp(buf
, "off")) {
5055 "qemu: invalid option value '%s'\n",
5063 case QEMU_OPTION_fda
:
5064 case QEMU_OPTION_fdb
:
5065 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5068 case QEMU_OPTION_no_fd_bootchk
:
5072 case QEMU_OPTION_net
:
5073 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5074 fprintf(stderr
, "qemu: too many network clients\n");
5077 net_clients
[nb_net_clients
] = optarg
;
5081 case QEMU_OPTION_tftp
:
5082 legacy_tftp_prefix
= optarg
;
5084 case QEMU_OPTION_bootp
:
5085 legacy_bootp_filename
= optarg
;
5088 case QEMU_OPTION_smb
:
5089 net_slirp_smb(optarg
);
5092 case QEMU_OPTION_redir
:
5093 net_slirp_redir(optarg
);
5096 case QEMU_OPTION_bt
:
5097 add_device_config(DEV_BT
, optarg
);
5100 case QEMU_OPTION_audio_help
:
5104 case QEMU_OPTION_soundhw
:
5105 select_soundhw (optarg
);
5111 case QEMU_OPTION_version
:
5115 case QEMU_OPTION_m
: {
5119 value
= strtoul(optarg
, &ptr
, 10);
5121 case 0: case 'M': case 'm':
5128 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5132 /* On 32-bit hosts, QEMU is limited by virtual address space */
5133 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5134 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5137 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5138 fprintf(stderr
, "qemu: ram size too large\n");
5147 const CPULogItem
*item
;
5149 mask
= cpu_str_to_log_mask(optarg
);
5151 printf("Log items (comma separated):\n");
5152 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5153 printf("%-10s %s\n", item
->name
, item
->help
);
5161 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5163 case QEMU_OPTION_gdb
:
5164 gdbstub_dev
= optarg
;
5169 case QEMU_OPTION_bios
:
5172 case QEMU_OPTION_singlestep
:
5180 keyboard_layout
= optarg
;
5183 case QEMU_OPTION_localtime
:
5186 case QEMU_OPTION_vga
:
5187 select_vgahw (optarg
);
5189 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5195 w
= strtol(p
, (char **)&p
, 10);
5198 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5204 h
= strtol(p
, (char **)&p
, 10);
5209 depth
= strtol(p
, (char **)&p
, 10);
5210 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5211 depth
!= 24 && depth
!= 32)
5213 } else if (*p
== '\0') {
5214 depth
= graphic_depth
;
5221 graphic_depth
= depth
;
5225 case QEMU_OPTION_echr
:
5228 term_escape_char
= strtol(optarg
, &r
, 0);
5230 printf("Bad argument to echr\n");
5233 case QEMU_OPTION_monitor
:
5234 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5235 fprintf(stderr
, "qemu: too many monitor devices\n");
5238 monitor_devices
[monitor_device_index
] = optarg
;
5239 monitor_device_index
++;
5241 case QEMU_OPTION_chardev
:
5242 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5244 fprintf(stderr
, "parse error: %s\n", optarg
);
5247 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5251 case QEMU_OPTION_serial
:
5252 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5253 fprintf(stderr
, "qemu: too many serial ports\n");
5256 serial_devices
[serial_device_index
] = optarg
;
5257 serial_device_index
++;
5259 case QEMU_OPTION_watchdog
:
5262 "qemu: only one watchdog option may be given\n");
5267 case QEMU_OPTION_watchdog_action
:
5268 if (select_watchdog_action(optarg
) == -1) {
5269 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5273 case QEMU_OPTION_virtiocon
:
5274 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5275 fprintf(stderr
, "qemu: too many virtio consoles\n");
5278 virtio_consoles
[virtio_console_index
] = optarg
;
5279 virtio_console_index
++;
5281 case QEMU_OPTION_parallel
:
5282 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5283 fprintf(stderr
, "qemu: too many parallel ports\n");
5286 parallel_devices
[parallel_device_index
] = optarg
;
5287 parallel_device_index
++;
5289 case QEMU_OPTION_loadvm
:
5292 case QEMU_OPTION_full_screen
:
5296 case QEMU_OPTION_no_frame
:
5299 case QEMU_OPTION_alt_grab
:
5302 case QEMU_OPTION_no_quit
:
5305 case QEMU_OPTION_sdl
:
5306 display_type
= DT_SDL
;
5309 case QEMU_OPTION_pidfile
:
5313 case QEMU_OPTION_win2k_hack
:
5314 win2k_install_hack
= 1;
5316 case QEMU_OPTION_rtc_td_hack
:
5319 case QEMU_OPTION_acpitable
:
5320 if(acpi_table_add(optarg
) < 0) {
5321 fprintf(stderr
, "Wrong acpi table provided\n");
5325 case QEMU_OPTION_smbios
:
5326 if(smbios_entry_add(optarg
) < 0) {
5327 fprintf(stderr
, "Wrong smbios provided\n");
5333 case QEMU_OPTION_enable_kvm
:
5337 case QEMU_OPTION_usb
:
5340 case QEMU_OPTION_usbdevice
:
5342 add_device_config(DEV_USB
, optarg
);
5344 case QEMU_OPTION_device
:
5345 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5347 fprintf(stderr
, "parse error: %s\n", optarg
);
5351 case QEMU_OPTION_smp
:
5354 fprintf(stderr
, "Invalid number of CPUs\n");
5357 if (max_cpus
< smp_cpus
) {
5358 fprintf(stderr
, "maxcpus must be equal to or greater than "
5362 if (max_cpus
> 255) {
5363 fprintf(stderr
, "Unsupported number of maxcpus\n");
5367 case QEMU_OPTION_vnc
:
5368 display_type
= DT_VNC
;
5369 vnc_display
= optarg
;
5372 case QEMU_OPTION_no_acpi
:
5375 case QEMU_OPTION_no_hpet
:
5378 case QEMU_OPTION_balloon
:
5379 if (balloon_parse(optarg
) < 0) {
5380 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5385 case QEMU_OPTION_no_reboot
:
5388 case QEMU_OPTION_no_shutdown
:
5391 case QEMU_OPTION_show_cursor
:
5394 case QEMU_OPTION_uuid
:
5395 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5396 fprintf(stderr
, "Fail to parse UUID string."
5397 " Wrong format.\n");
5402 case QEMU_OPTION_daemonize
:
5406 case QEMU_OPTION_option_rom
:
5407 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5408 fprintf(stderr
, "Too many option ROMs\n");
5411 option_rom
[nb_option_roms
] = optarg
;
5414 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5415 case QEMU_OPTION_semihosting
:
5416 semihosting_enabled
= 1;
5419 case QEMU_OPTION_name
:
5420 qemu_name
= qemu_strdup(optarg
);
5422 char *p
= strchr(qemu_name
, ',');
5425 if (strncmp(p
, "process=", 8)) {
5426 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5434 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5435 case QEMU_OPTION_prom_env
:
5436 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5437 fprintf(stderr
, "Too many prom variables\n");
5440 prom_envs
[nb_prom_envs
] = optarg
;
5445 case QEMU_OPTION_old_param
:
5449 case QEMU_OPTION_clock
:
5450 configure_alarms(optarg
);
5452 case QEMU_OPTION_startdate
:
5453 configure_rtc_date_offset(optarg
, 1);
5455 case QEMU_OPTION_rtc
:
5456 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5458 fprintf(stderr
, "parse error: %s\n", optarg
);
5461 configure_rtc(opts
);
5463 case QEMU_OPTION_tb_size
:
5464 tb_size
= strtol(optarg
, NULL
, 0);
5468 case QEMU_OPTION_icount
:
5470 if (strcmp(optarg
, "auto") == 0) {
5471 icount_time_shift
= -1;
5473 icount_time_shift
= strtol(optarg
, NULL
, 0);
5476 case QEMU_OPTION_incoming
:
5480 case QEMU_OPTION_chroot
:
5481 chroot_dir
= optarg
;
5483 case QEMU_OPTION_runas
:
5488 case QEMU_OPTION_xen_domid
:
5489 xen_domid
= atoi(optarg
);
5491 case QEMU_OPTION_xen_create
:
5492 xen_mode
= XEN_CREATE
;
5494 case QEMU_OPTION_xen_attach
:
5495 xen_mode
= XEN_ATTACH
;
5502 /* If no data_dir is specified then try to find it relative to the
5505 data_dir
= find_datadir(argv
[0]);
5507 /* If all else fails use the install patch specified when building. */
5509 data_dir
= CONFIG_QEMU_SHAREDIR
;
5513 * Default to max_cpus = smp_cpus, in case the user doesn't
5514 * specify a max_cpus value.
5517 max_cpus
= smp_cpus
;
5519 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5520 if (smp_cpus
> machine
->max_cpus
) {
5521 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5522 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5527 if (display_type
== DT_NOGRAPHIC
) {
5528 if (serial_device_index
== 0)
5529 serial_devices
[0] = "stdio";
5530 if (parallel_device_index
== 0)
5531 parallel_devices
[0] = "null";
5532 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5533 monitor_devices
[0] = "stdio";
5541 if (pipe(fds
) == -1)
5552 len
= read(fds
[0], &status
, 1);
5553 if (len
== -1 && (errno
== EINTR
))
5558 else if (status
== 1) {
5559 fprintf(stderr
, "Could not acquire pidfile\n");
5576 signal(SIGTSTP
, SIG_IGN
);
5577 signal(SIGTTOU
, SIG_IGN
);
5578 signal(SIGTTIN
, SIG_IGN
);
5581 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5584 write(fds
[1], &status
, 1);
5586 fprintf(stderr
, "Could not acquire pid file\n");
5591 if (kvm_enabled()) {
5594 ret
= kvm_init(smp_cpus
);
5596 fprintf(stderr
, "failed to initialize KVM\n");
5601 if (qemu_init_main_loop()) {
5602 fprintf(stderr
, "qemu_init_main_loop failed\n");
5605 linux_boot
= (kernel_filename
!= NULL
);
5607 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5608 fprintf(stderr
, "-append only allowed with -kernel option\n");
5612 if (!linux_boot
&& initrd_filename
!= NULL
) {
5613 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5618 /* Win32 doesn't support line-buffering and requires size >= 2 */
5619 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5623 if (init_timer_alarm() < 0) {
5624 fprintf(stderr
, "could not initialize alarm timer\n");
5627 if (use_icount
&& icount_time_shift
< 0) {
5629 /* 125MIPS seems a reasonable initial guess at the guest speed.
5630 It will be corrected fairly quickly anyway. */
5631 icount_time_shift
= 3;
5632 init_icount_adjust();
5639 /* init network clients */
5640 if (nb_net_clients
== 0) {
5641 /* if no clients, we use a default config */
5642 net_clients
[nb_net_clients
++] = "nic";
5644 net_clients
[nb_net_clients
++] = "user";
5648 for(i
= 0;i
< nb_net_clients
; i
++) {
5649 if (net_client_parse(net_clients
[i
]) < 0)
5653 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5654 net_set_boot_mask(net_boot
);
5658 /* init the bluetooth world */
5659 if (foreach_device_config(DEV_BT
, bt_parse
))
5662 /* init the memory */
5664 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5666 /* init the dynamic translator */
5667 cpu_exec_init_all(tb_size
* 1024 * 1024);
5671 /* we always create the cdrom drive, even if no disk is there */
5672 drive_add(NULL
, CDROM_ALIAS
);
5674 /* we always create at least one floppy */
5675 drive_add(NULL
, FD_ALIAS
, 0);
5677 /* we always create one sd slot, even if no card is in it */
5678 drive_add(NULL
, SD_ALIAS
);
5680 /* open the virtual block devices */
5682 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5683 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5686 vmstate_register(0, &vmstate_timers
,&timers_state
);
5687 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5689 /* Maintain compatibility with multiple stdio monitors */
5690 if (!strcmp(monitor_devices
[0],"stdio")) {
5691 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5692 const char *devname
= serial_devices
[i
];
5693 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5694 monitor_devices
[0] = NULL
;
5696 } else if (devname
&& !strcmp(devname
,"stdio")) {
5697 monitor_devices
[0] = NULL
;
5698 serial_devices
[i
] = "mon:stdio";
5704 if (nb_numa_nodes
> 0) {
5707 if (nb_numa_nodes
> smp_cpus
) {
5708 nb_numa_nodes
= smp_cpus
;
5711 /* If no memory size if given for any node, assume the default case
5712 * and distribute the available memory equally across all nodes
5714 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5715 if (node_mem
[i
] != 0)
5718 if (i
== nb_numa_nodes
) {
5719 uint64_t usedmem
= 0;
5721 /* On Linux, the each node's border has to be 8MB aligned,
5722 * the final node gets the rest.
5724 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5725 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5726 usedmem
+= node_mem
[i
];
5728 node_mem
[i
] = ram_size
- usedmem
;
5731 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5732 if (node_cpumask
[i
] != 0)
5735 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5736 * must cope with this anyway, because there are BIOSes out there in
5737 * real machines which also use this scheme.
5739 if (i
== nb_numa_nodes
) {
5740 for (i
= 0; i
< smp_cpus
; i
++) {
5741 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5746 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5747 const char *devname
= monitor_devices
[i
];
5748 if (devname
&& strcmp(devname
, "none")) {
5751 snprintf(label
, sizeof(label
), "monitor");
5753 snprintf(label
, sizeof(label
), "monitor%d", i
);
5755 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5756 if (!monitor_hds
[i
]) {
5757 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5764 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5765 const char *devname
= serial_devices
[i
];
5766 if (devname
&& strcmp(devname
, "none")) {
5768 snprintf(label
, sizeof(label
), "serial%d", i
);
5769 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5770 if (!serial_hds
[i
]) {
5771 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5778 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5779 const char *devname
= parallel_devices
[i
];
5780 if (devname
&& strcmp(devname
, "none")) {
5782 snprintf(label
, sizeof(label
), "parallel%d", i
);
5783 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5784 if (!parallel_hds
[i
]) {
5785 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5792 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5793 const char *devname
= virtio_consoles
[i
];
5794 if (devname
&& strcmp(devname
, "none")) {
5796 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5797 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5798 if (!virtcon_hds
[i
]) {
5799 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5806 module_call_init(MODULE_INIT_DEVICE
);
5809 i
= select_watchdog(watchdog
);
5811 exit (i
== 1 ? 1 : 0);
5814 if (machine
->compat_props
) {
5815 qdev_prop_register_compat(machine
->compat_props
);
5817 machine
->init(ram_size
, boot_devices
,
5818 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5822 /* must be after terminal init, SDL library changes signal handlers */
5826 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5827 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5828 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5834 current_machine
= machine
;
5836 /* init USB devices */
5838 foreach_device_config(DEV_USB
, usb_parse
);
5841 /* init generic devices */
5842 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5846 dumb_display_init();
5847 /* just use the first displaystate for the moment */
5850 if (display_type
== DT_DEFAULT
) {
5851 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5852 display_type
= DT_SDL
;
5854 display_type
= DT_VNC
;
5855 vnc_display
= "localhost:0,to=99";
5861 switch (display_type
) {
5864 #if defined(CONFIG_CURSES)
5866 curses_display_init(ds
, full_screen
);
5869 #if defined(CONFIG_SDL)
5871 sdl_display_init(ds
, full_screen
, no_frame
);
5873 #elif defined(CONFIG_COCOA)
5875 cocoa_display_init(ds
, full_screen
);
5879 vnc_display_init(ds
);
5880 if (vnc_display_open(ds
, vnc_display
) < 0)
5883 if (show_vnc_port
) {
5884 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5892 dcl
= ds
->listeners
;
5893 while (dcl
!= NULL
) {
5894 if (dcl
->dpy_refresh
!= NULL
) {
5895 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5896 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5901 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5902 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5903 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5906 text_consoles_set_display(display_state
);
5907 qemu_chr_initial_reset();
5909 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5910 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5911 monitor_init(monitor_hds
[i
],
5912 MONITOR_USE_READLINE
|
5913 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5917 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5918 const char *devname
= serial_devices
[i
];
5919 if (devname
&& strcmp(devname
, "none")) {
5920 if (strstart(devname
, "vc", 0))
5921 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5925 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5926 const char *devname
= parallel_devices
[i
];
5927 if (devname
&& strcmp(devname
, "none")) {
5928 if (strstart(devname
, "vc", 0))
5929 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5933 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5934 const char *devname
= virtio_consoles
[i
];
5935 if (virtcon_hds
[i
] && devname
) {
5936 if (strstart(devname
, "vc", 0))
5937 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5941 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5942 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5948 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5954 qemu_start_incoming_migration(incoming
);
5955 } else if (autostart
) {
5965 len
= write(fds
[1], &status
, 1);
5966 if (len
== -1 && (errno
== EINTR
))
5973 TFR(fd
= open("/dev/null", O_RDWR
));
5979 pwd
= getpwnam(run_as
);
5981 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5987 if (chroot(chroot_dir
) < 0) {
5988 fprintf(stderr
, "chroot failed\n");
5995 if (setgid(pwd
->pw_gid
) < 0) {
5996 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
5999 if (setuid(pwd
->pw_uid
) < 0) {
6000 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6003 if (setuid(0) != -1) {
6004 fprintf(stderr
, "Dropping privileges failed\n");