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"
146 #include "hw/loader.h"
153 #include "qemu-timer.h"
154 #include "qemu-char.h"
155 #include "cache-utils.h"
158 #include "audio/audio.h"
159 #include "migration.h"
162 #include "qemu-option.h"
163 #include "qemu-config.h"
167 #include "exec-all.h"
169 #include "qemu_socket.h"
171 #include "slirp/libslirp.h"
173 #include "qemu-queue.h"
176 //#define DEBUG_SLIRP
178 #define DEFAULT_RAM_SIZE 128
180 /* Maximum number of monitor devices */
181 #define MAX_MONITOR_DEVICES 10
183 static const char *data_dir
;
184 const char *bios_name
= NULL
;
185 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
186 to store the VM snapshots */
187 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
188 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
189 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
190 static DisplayState
*display_state
;
191 DisplayType display_type
= DT_DEFAULT
;
192 const char* keyboard_layout
= NULL
;
195 NICInfo nd_table
[MAX_NICS
];
198 static int rtc_utc
= 1;
199 static int rtc_date_offset
= -1; /* -1 means no change */
200 QEMUClock
*rtc_clock
;
201 int vga_interface_type
= VGA_CIRRUS
;
203 int graphic_width
= 1024;
204 int graphic_height
= 768;
205 int graphic_depth
= 8;
207 int graphic_width
= 800;
208 int graphic_height
= 600;
209 int graphic_depth
= 15;
211 static int full_screen
= 0;
213 static int no_frame
= 0;
216 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
217 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
218 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
220 int win2k_install_hack
= 0;
229 const char *vnc_display
;
230 int acpi_enabled
= 1;
236 int graphic_rotate
= 0;
237 uint8_t irq0override
= 1;
241 const char *watchdog
;
242 const char *option_rom
[MAX_OPTION_ROMS
];
244 int semihosting_enabled
= 0;
248 const char *qemu_name
;
251 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
252 unsigned int nb_prom_envs
= 0;
253 const char *prom_envs
[MAX_PROM_ENVS
];
258 uint64_t node_mem
[MAX_NODES
];
259 uint64_t node_cpumask
[MAX_NODES
];
261 static CPUState
*cur_cpu
;
262 static CPUState
*next_cpu
;
263 static int timer_alarm_pending
= 1;
264 /* Conversion factor from emulated instructions to virtual clock ticks. */
265 static int icount_time_shift
;
266 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
267 #define MAX_ICOUNT_SHIFT 10
268 /* Compensate for varying guest execution speed. */
269 static int64_t qemu_icount_bias
;
270 static QEMUTimer
*icount_rt_timer
;
271 static QEMUTimer
*icount_vm_timer
;
272 static QEMUTimer
*nographic_timer
;
274 uint8_t qemu_uuid
[16];
276 static QEMUBootSetHandler
*boot_set_handler
;
277 static void *boot_set_opaque
;
279 /***********************************************************/
280 /* x86 ISA bus support */
282 target_phys_addr_t isa_mem_base
= 0;
285 /***********************************************************/
286 void hw_error(const char *fmt
, ...)
292 fprintf(stderr
, "qemu: hardware error: ");
293 vfprintf(stderr
, fmt
, ap
);
294 fprintf(stderr
, "\n");
295 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
296 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
298 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
300 cpu_dump_state(env
, stderr
, fprintf
, 0);
307 static void set_proc_name(const char *s
)
309 #if defined(__linux__) && defined(PR_SET_NAME)
313 name
[sizeof(name
) - 1] = 0;
314 strncpy(name
, s
, sizeof(name
));
315 /* Could rewrite argv[0] too, but that's a bit more complicated.
316 This simple way is enough for `top'. */
317 prctl(PR_SET_NAME
, name
);
324 static QEMUBalloonEvent
*qemu_balloon_event
;
325 void *qemu_balloon_event_opaque
;
327 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
329 qemu_balloon_event
= func
;
330 qemu_balloon_event_opaque
= opaque
;
333 void qemu_balloon(ram_addr_t target
)
335 if (qemu_balloon_event
)
336 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
339 ram_addr_t
qemu_balloon_status(void)
341 if (qemu_balloon_event
)
342 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
346 /***********************************************************/
349 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
350 static void *qemu_put_kbd_event_opaque
;
351 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
352 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
354 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
356 qemu_put_kbd_event_opaque
= opaque
;
357 qemu_put_kbd_event
= func
;
360 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
361 void *opaque
, int absolute
,
364 QEMUPutMouseEntry
*s
, *cursor
;
366 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
368 s
->qemu_put_mouse_event
= func
;
369 s
->qemu_put_mouse_event_opaque
= opaque
;
370 s
->qemu_put_mouse_event_absolute
= absolute
;
371 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
374 if (!qemu_put_mouse_event_head
) {
375 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
379 cursor
= qemu_put_mouse_event_head
;
380 while (cursor
->next
!= NULL
)
381 cursor
= cursor
->next
;
384 qemu_put_mouse_event_current
= s
;
389 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
391 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
393 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
396 cursor
= qemu_put_mouse_event_head
;
397 while (cursor
!= NULL
&& cursor
!= entry
) {
399 cursor
= cursor
->next
;
402 if (cursor
== NULL
) // does not exist or list empty
404 else if (prev
== NULL
) { // entry is head
405 qemu_put_mouse_event_head
= cursor
->next
;
406 if (qemu_put_mouse_event_current
== entry
)
407 qemu_put_mouse_event_current
= cursor
->next
;
408 qemu_free(entry
->qemu_put_mouse_event_name
);
413 prev
->next
= entry
->next
;
415 if (qemu_put_mouse_event_current
== entry
)
416 qemu_put_mouse_event_current
= prev
;
418 qemu_free(entry
->qemu_put_mouse_event_name
);
422 void kbd_put_keycode(int keycode
)
424 if (qemu_put_kbd_event
) {
425 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
429 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
431 QEMUPutMouseEvent
*mouse_event
;
432 void *mouse_event_opaque
;
435 if (!qemu_put_mouse_event_current
) {
440 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
442 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
445 if (graphic_rotate
) {
446 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
449 width
= graphic_width
- 1;
450 mouse_event(mouse_event_opaque
,
451 width
- dy
, dx
, dz
, buttons_state
);
453 mouse_event(mouse_event_opaque
,
454 dx
, dy
, dz
, buttons_state
);
458 int kbd_mouse_is_absolute(void)
460 if (!qemu_put_mouse_event_current
)
463 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
466 void do_info_mice(Monitor
*mon
)
468 QEMUPutMouseEntry
*cursor
;
471 if (!qemu_put_mouse_event_head
) {
472 monitor_printf(mon
, "No mouse devices connected\n");
476 monitor_printf(mon
, "Mouse devices available:\n");
477 cursor
= qemu_put_mouse_event_head
;
478 while (cursor
!= NULL
) {
479 monitor_printf(mon
, "%c Mouse #%d: %s\n",
480 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
481 index
, cursor
->qemu_put_mouse_event_name
);
483 cursor
= cursor
->next
;
487 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
489 QEMUPutMouseEntry
*cursor
;
491 int index
= qdict_get_int(qdict
, "index");
493 if (!qemu_put_mouse_event_head
) {
494 monitor_printf(mon
, "No mouse devices connected\n");
498 cursor
= qemu_put_mouse_event_head
;
499 while (cursor
!= NULL
&& index
!= i
) {
501 cursor
= cursor
->next
;
505 qemu_put_mouse_event_current
= cursor
;
507 monitor_printf(mon
, "Mouse at given index not found\n");
510 /* compute with 96 bit intermediate result: (a*b)/c */
511 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
516 #ifdef HOST_WORDS_BIGENDIAN
526 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
527 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
530 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
534 /***********************************************************/
535 /* real time host monotonic timer */
537 static int64_t get_clock_realtime(void)
541 gettimeofday(&tv
, NULL
);
542 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
547 static int64_t clock_freq
;
549 static void init_get_clock(void)
553 ret
= QueryPerformanceFrequency(&freq
);
555 fprintf(stderr
, "Could not calibrate ticks\n");
558 clock_freq
= freq
.QuadPart
;
561 static int64_t get_clock(void)
564 QueryPerformanceCounter(&ti
);
565 return muldiv64(ti
.QuadPart
, get_ticks_per_sec(), clock_freq
);
570 static int use_rt_clock
;
572 static void init_get_clock(void)
575 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
576 || defined(__DragonFly__)
579 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
586 static int64_t get_clock(void)
588 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
589 || defined(__DragonFly__)
592 clock_gettime(CLOCK_MONOTONIC
, &ts
);
593 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
597 /* XXX: using gettimeofday leads to problems if the date
598 changes, so it should be avoided. */
599 return get_clock_realtime();
604 /* Return the virtual CPU time, based on the instruction counter. */
605 static int64_t cpu_get_icount(void)
608 CPUState
*env
= cpu_single_env
;;
609 icount
= qemu_icount
;
612 fprintf(stderr
, "Bad clock read\n");
613 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
615 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
618 /***********************************************************/
619 /* guest cycle counter */
621 typedef struct TimersState
{
622 int64_t cpu_ticks_prev
;
623 int64_t cpu_ticks_offset
;
624 int64_t cpu_clock_offset
;
625 int32_t cpu_ticks_enabled
;
629 TimersState timers_state
;
631 /* return the host CPU cycle counter and handle stop/restart */
632 int64_t cpu_get_ticks(void)
635 return cpu_get_icount();
637 if (!timers_state
.cpu_ticks_enabled
) {
638 return timers_state
.cpu_ticks_offset
;
641 ticks
= cpu_get_real_ticks();
642 if (timers_state
.cpu_ticks_prev
> ticks
) {
643 /* Note: non increasing ticks may happen if the host uses
645 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
647 timers_state
.cpu_ticks_prev
= ticks
;
648 return ticks
+ timers_state
.cpu_ticks_offset
;
652 /* return the host CPU monotonic timer and handle stop/restart */
653 static int64_t cpu_get_clock(void)
656 if (!timers_state
.cpu_ticks_enabled
) {
657 return timers_state
.cpu_clock_offset
;
660 return ti
+ timers_state
.cpu_clock_offset
;
664 /* enable cpu_get_ticks() */
665 void cpu_enable_ticks(void)
667 if (!timers_state
.cpu_ticks_enabled
) {
668 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
669 timers_state
.cpu_clock_offset
-= get_clock();
670 timers_state
.cpu_ticks_enabled
= 1;
674 /* disable cpu_get_ticks() : the clock is stopped. You must not call
675 cpu_get_ticks() after that. */
676 void cpu_disable_ticks(void)
678 if (timers_state
.cpu_ticks_enabled
) {
679 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
680 timers_state
.cpu_clock_offset
= cpu_get_clock();
681 timers_state
.cpu_ticks_enabled
= 0;
685 /***********************************************************/
688 #define QEMU_CLOCK_REALTIME 0
689 #define QEMU_CLOCK_VIRTUAL 1
690 #define QEMU_CLOCK_HOST 2
694 /* XXX: add frequency */
702 struct QEMUTimer
*next
;
705 struct qemu_alarm_timer
{
709 int (*start
)(struct qemu_alarm_timer
*t
);
710 void (*stop
)(struct qemu_alarm_timer
*t
);
711 void (*rearm
)(struct qemu_alarm_timer
*t
);
715 #define ALARM_FLAG_DYNTICKS 0x1
716 #define ALARM_FLAG_EXPIRED 0x2
718 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
720 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
723 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
725 if (!alarm_has_dynticks(t
))
731 /* TODO: MIN_TIMER_REARM_US should be optimized */
732 #define MIN_TIMER_REARM_US 250
734 static struct qemu_alarm_timer
*alarm_timer
;
738 struct qemu_alarm_win32
{
741 } alarm_win32_data
= {0, -1};
743 static int win32_start_timer(struct qemu_alarm_timer
*t
);
744 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
745 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
749 static int unix_start_timer(struct qemu_alarm_timer
*t
);
750 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
754 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
755 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
756 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
758 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
759 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
761 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
762 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
764 #endif /* __linux__ */
768 /* Correlation between real and virtual time is always going to be
769 fairly approximate, so ignore small variation.
770 When the guest is idle real and virtual time will be aligned in
772 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
774 static void icount_adjust(void)
779 static int64_t last_delta
;
780 /* If the VM is not running, then do nothing. */
784 cur_time
= cpu_get_clock();
785 cur_icount
= qemu_get_clock(vm_clock
);
786 delta
= cur_icount
- cur_time
;
787 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
789 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
790 && icount_time_shift
> 0) {
791 /* The guest is getting too far ahead. Slow time down. */
795 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
796 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
797 /* The guest is getting too far behind. Speed time up. */
801 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
804 static void icount_adjust_rt(void * opaque
)
806 qemu_mod_timer(icount_rt_timer
,
807 qemu_get_clock(rt_clock
) + 1000);
811 static void icount_adjust_vm(void * opaque
)
813 qemu_mod_timer(icount_vm_timer
,
814 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
818 static void init_icount_adjust(void)
820 /* Have both realtime and virtual time triggers for speed adjustment.
821 The realtime trigger catches emulated time passing too slowly,
822 the virtual time trigger catches emulated time passing too fast.
823 Realtime triggers occur even when idle, so use them less frequently
825 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
826 qemu_mod_timer(icount_rt_timer
,
827 qemu_get_clock(rt_clock
) + 1000);
828 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
829 qemu_mod_timer(icount_vm_timer
,
830 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
833 static struct qemu_alarm_timer alarm_timers
[] = {
836 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
837 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
838 /* HPET - if available - is preferred */
839 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
840 /* ...otherwise try RTC */
841 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
843 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
845 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
846 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
847 {"win32", 0, win32_start_timer
,
848 win32_stop_timer
, NULL
, &alarm_win32_data
},
853 static void show_available_alarms(void)
857 printf("Available alarm timers, in order of precedence:\n");
858 for (i
= 0; alarm_timers
[i
].name
; i
++)
859 printf("%s\n", alarm_timers
[i
].name
);
862 static void configure_alarms(char const *opt
)
866 int count
= ARRAY_SIZE(alarm_timers
) - 1;
869 struct qemu_alarm_timer tmp
;
871 if (!strcmp(opt
, "?")) {
872 show_available_alarms();
876 arg
= qemu_strdup(opt
);
878 /* Reorder the array */
879 name
= strtok(arg
, ",");
881 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
882 if (!strcmp(alarm_timers
[i
].name
, name
))
887 fprintf(stderr
, "Unknown clock %s\n", name
);
896 tmp
= alarm_timers
[i
];
897 alarm_timers
[i
] = alarm_timers
[cur
];
898 alarm_timers
[cur
] = tmp
;
902 name
= strtok(NULL
, ",");
908 /* Disable remaining timers */
909 for (i
= cur
; i
< count
; i
++)
910 alarm_timers
[i
].name
= NULL
;
912 show_available_alarms();
917 #define QEMU_NUM_CLOCKS 3
921 QEMUClock
*host_clock
;
923 static QEMUTimer
*active_timers
[QEMU_NUM_CLOCKS
];
925 static QEMUClock
*qemu_new_clock(int type
)
928 clock
= qemu_mallocz(sizeof(QEMUClock
));
933 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
937 ts
= qemu_mallocz(sizeof(QEMUTimer
));
944 void qemu_free_timer(QEMUTimer
*ts
)
949 /* stop a timer, but do not dealloc it */
950 void qemu_del_timer(QEMUTimer
*ts
)
954 /* NOTE: this code must be signal safe because
955 qemu_timer_expired() can be called from a signal. */
956 pt
= &active_timers
[ts
->clock
->type
];
969 /* modify the current timer so that it will be fired when current_time
970 >= expire_time. The corresponding callback will be called. */
971 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
977 /* add the timer in the sorted list */
978 /* NOTE: this code must be signal safe because
979 qemu_timer_expired() can be called from a signal. */
980 pt
= &active_timers
[ts
->clock
->type
];
985 if (t
->expire_time
> expire_time
)
989 ts
->expire_time
= expire_time
;
993 /* Rearm if necessary */
994 if (pt
== &active_timers
[ts
->clock
->type
]) {
995 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
996 qemu_rearm_alarm_timer(alarm_timer
);
998 /* Interrupt execution to force deadline recalculation. */
1000 qemu_notify_event();
1004 int qemu_timer_pending(QEMUTimer
*ts
)
1007 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1014 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1018 return (timer_head
->expire_time
<= current_time
);
1021 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1027 if (!ts
|| ts
->expire_time
> current_time
)
1029 /* remove timer from the list before calling the callback */
1030 *ptimer_head
= ts
->next
;
1033 /* run the callback (the timer list can be modified) */
1038 int64_t qemu_get_clock(QEMUClock
*clock
)
1040 switch(clock
->type
) {
1041 case QEMU_CLOCK_REALTIME
:
1042 return get_clock() / 1000000;
1044 case QEMU_CLOCK_VIRTUAL
:
1046 return cpu_get_icount();
1048 return cpu_get_clock();
1050 case QEMU_CLOCK_HOST
:
1051 return get_clock_realtime();
1055 static void init_clocks(void)
1058 rt_clock
= qemu_new_clock(QEMU_CLOCK_REALTIME
);
1059 vm_clock
= qemu_new_clock(QEMU_CLOCK_VIRTUAL
);
1060 host_clock
= qemu_new_clock(QEMU_CLOCK_HOST
);
1062 rtc_clock
= host_clock
;
1066 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1068 uint64_t expire_time
;
1070 if (qemu_timer_pending(ts
)) {
1071 expire_time
= ts
->expire_time
;
1075 qemu_put_be64(f
, expire_time
);
1078 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1080 uint64_t expire_time
;
1082 expire_time
= qemu_get_be64(f
);
1083 if (expire_time
!= -1) {
1084 qemu_mod_timer(ts
, expire_time
);
1090 static const VMStateDescription vmstate_timers
= {
1093 .minimum_version_id
= 1,
1094 .minimum_version_id_old
= 1,
1095 .fields
= (VMStateField
[]) {
1096 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1097 VMSTATE_INT64(dummy
, TimersState
),
1098 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1099 VMSTATE_END_OF_LIST()
1103 static void qemu_event_increment(void);
1106 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1107 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1110 static void host_alarm_handler(int host_signum
)
1114 #define DISP_FREQ 1000
1116 static int64_t delta_min
= INT64_MAX
;
1117 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1119 ti
= qemu_get_clock(vm_clock
);
1120 if (last_clock
!= 0) {
1121 delta
= ti
- last_clock
;
1122 if (delta
< delta_min
)
1124 if (delta
> delta_max
)
1127 if (++count
== DISP_FREQ
) {
1128 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1129 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1130 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1131 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1132 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1134 delta_min
= INT64_MAX
;
1142 if (alarm_has_dynticks(alarm_timer
) ||
1144 qemu_timer_expired(active_timers
[QEMU_CLOCK_VIRTUAL
],
1145 qemu_get_clock(vm_clock
))) ||
1146 qemu_timer_expired(active_timers
[QEMU_CLOCK_REALTIME
],
1147 qemu_get_clock(rt_clock
)) ||
1148 qemu_timer_expired(active_timers
[QEMU_CLOCK_HOST
],
1149 qemu_get_clock(host_clock
))) {
1150 qemu_event_increment();
1151 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1153 #ifndef CONFIG_IOTHREAD
1155 /* stop the currently executing cpu because a timer occured */
1159 timer_alarm_pending
= 1;
1160 qemu_notify_event();
1164 static int64_t qemu_next_deadline(void)
1166 /* To avoid problems with overflow limit this to 2^32. */
1167 int64_t delta
= INT32_MAX
;
1169 if (active_timers
[QEMU_CLOCK_VIRTUAL
]) {
1170 delta
= active_timers
[QEMU_CLOCK_VIRTUAL
]->expire_time
-
1171 qemu_get_clock(vm_clock
);
1173 if (active_timers
[QEMU_CLOCK_HOST
]) {
1174 int64_t hdelta
= active_timers
[QEMU_CLOCK_HOST
]->expire_time
-
1175 qemu_get_clock(host_clock
);
1186 #if defined(__linux__)
1187 static uint64_t qemu_next_deadline_dyntick(void)
1195 delta
= (qemu_next_deadline() + 999) / 1000;
1197 if (active_timers
[QEMU_CLOCK_REALTIME
]) {
1198 rtdelta
= (active_timers
[QEMU_CLOCK_REALTIME
]->expire_time
-
1199 qemu_get_clock(rt_clock
))*1000;
1200 if (rtdelta
< delta
)
1204 if (delta
< MIN_TIMER_REARM_US
)
1205 delta
= MIN_TIMER_REARM_US
;
1213 /* Sets a specific flag */
1214 static int fcntl_setfl(int fd
, int flag
)
1218 flags
= fcntl(fd
, F_GETFL
);
1222 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1228 #if defined(__linux__)
1230 #define RTC_FREQ 1024
1232 static void enable_sigio_timer(int fd
)
1234 struct sigaction act
;
1237 sigfillset(&act
.sa_mask
);
1239 act
.sa_handler
= host_alarm_handler
;
1241 sigaction(SIGIO
, &act
, NULL
);
1242 fcntl_setfl(fd
, O_ASYNC
);
1243 fcntl(fd
, F_SETOWN
, getpid());
1246 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1248 struct hpet_info info
;
1251 fd
= open("/dev/hpet", O_RDONLY
);
1256 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1258 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1259 "error, but for better emulation accuracy type:\n"
1260 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1264 /* Check capabilities */
1265 r
= ioctl(fd
, HPET_INFO
, &info
);
1269 /* Enable periodic mode */
1270 r
= ioctl(fd
, HPET_EPI
, 0);
1271 if (info
.hi_flags
&& (r
< 0))
1274 /* Enable interrupt */
1275 r
= ioctl(fd
, HPET_IE_ON
, 0);
1279 enable_sigio_timer(fd
);
1280 t
->priv
= (void *)(long)fd
;
1288 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1290 int fd
= (long)t
->priv
;
1295 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1298 unsigned long current_rtc_freq
= 0;
1300 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1303 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1304 if (current_rtc_freq
!= RTC_FREQ
&&
1305 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1306 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1307 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1308 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1311 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1317 enable_sigio_timer(rtc_fd
);
1319 t
->priv
= (void *)(long)rtc_fd
;
1324 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1326 int rtc_fd
= (long)t
->priv
;
1331 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1335 struct sigaction act
;
1337 sigfillset(&act
.sa_mask
);
1339 act
.sa_handler
= host_alarm_handler
;
1341 sigaction(SIGALRM
, &act
, NULL
);
1344 * Initialize ev struct to 0 to avoid valgrind complaining
1345 * about uninitialized data in timer_create call
1347 memset(&ev
, 0, sizeof(ev
));
1348 ev
.sigev_value
.sival_int
= 0;
1349 ev
.sigev_notify
= SIGEV_SIGNAL
;
1350 ev
.sigev_signo
= SIGALRM
;
1352 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1353 perror("timer_create");
1355 /* disable dynticks */
1356 fprintf(stderr
, "Dynamic Ticks disabled\n");
1361 t
->priv
= (void *)(long)host_timer
;
1366 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1368 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1370 timer_delete(host_timer
);
1373 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1375 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1376 struct itimerspec timeout
;
1377 int64_t nearest_delta_us
= INT64_MAX
;
1380 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1381 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1382 !active_timers
[QEMU_CLOCK_HOST
])
1385 nearest_delta_us
= qemu_next_deadline_dyntick();
1387 /* check whether a timer is already running */
1388 if (timer_gettime(host_timer
, &timeout
)) {
1390 fprintf(stderr
, "Internal timer error: aborting\n");
1393 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1394 if (current_us
&& current_us
<= nearest_delta_us
)
1397 timeout
.it_interval
.tv_sec
= 0;
1398 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1399 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1400 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1401 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1403 fprintf(stderr
, "Internal timer error: aborting\n");
1408 #endif /* defined(__linux__) */
1410 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1412 struct sigaction act
;
1413 struct itimerval itv
;
1417 sigfillset(&act
.sa_mask
);
1419 act
.sa_handler
= host_alarm_handler
;
1421 sigaction(SIGALRM
, &act
, NULL
);
1423 itv
.it_interval
.tv_sec
= 0;
1424 /* for i386 kernel 2.6 to get 1 ms */
1425 itv
.it_interval
.tv_usec
= 999;
1426 itv
.it_value
.tv_sec
= 0;
1427 itv
.it_value
.tv_usec
= 10 * 1000;
1429 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1436 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1438 struct itimerval itv
;
1440 memset(&itv
, 0, sizeof(itv
));
1441 setitimer(ITIMER_REAL
, &itv
, NULL
);
1444 #endif /* !defined(_WIN32) */
1449 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1452 struct qemu_alarm_win32
*data
= t
->priv
;
1455 memset(&tc
, 0, sizeof(tc
));
1456 timeGetDevCaps(&tc
, sizeof(tc
));
1458 if (data
->period
< tc
.wPeriodMin
)
1459 data
->period
= tc
.wPeriodMin
;
1461 timeBeginPeriod(data
->period
);
1463 flags
= TIME_CALLBACK_FUNCTION
;
1464 if (alarm_has_dynticks(t
))
1465 flags
|= TIME_ONESHOT
;
1467 flags
|= TIME_PERIODIC
;
1469 data
->timerId
= timeSetEvent(1, // interval (ms)
1470 data
->period
, // resolution
1471 host_alarm_handler
, // function
1472 (DWORD
)t
, // parameter
1475 if (!data
->timerId
) {
1476 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1478 timeEndPeriod(data
->period
);
1485 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1487 struct qemu_alarm_win32
*data
= t
->priv
;
1489 timeKillEvent(data
->timerId
);
1490 timeEndPeriod(data
->period
);
1493 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1495 struct qemu_alarm_win32
*data
= t
->priv
;
1497 if (!active_timers
[QEMU_CLOCK_REALTIME
] &&
1498 !active_timers
[QEMU_CLOCK_VIRTUAL
] &&
1499 !active_timers
[QEMU_CLOCK_HOST
])
1502 timeKillEvent(data
->timerId
);
1504 data
->timerId
= timeSetEvent(1,
1508 TIME_ONESHOT
| TIME_PERIODIC
);
1510 if (!data
->timerId
) {
1511 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1514 timeEndPeriod(data
->period
);
1521 static int init_timer_alarm(void)
1523 struct qemu_alarm_timer
*t
= NULL
;
1526 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1527 t
= &alarm_timers
[i
];
1547 static void quit_timers(void)
1549 alarm_timer
->stop(alarm_timer
);
1553 /***********************************************************/
1554 /* host time/date access */
1555 void qemu_get_timedate(struct tm
*tm
, int offset
)
1562 if (rtc_date_offset
== -1) {
1566 ret
= localtime(&ti
);
1568 ti
-= rtc_date_offset
;
1572 memcpy(tm
, ret
, sizeof(struct tm
));
1575 int qemu_timedate_diff(struct tm
*tm
)
1579 if (rtc_date_offset
== -1)
1581 seconds
= mktimegm(tm
);
1583 seconds
= mktime(tm
);
1585 seconds
= mktimegm(tm
) + rtc_date_offset
;
1587 return seconds
- time(NULL
);
1590 static void configure_rtc_date_offset(const char *startdate
, int legacy
)
1592 time_t rtc_start_date
;
1595 if (!strcmp(startdate
, "now") && legacy
) {
1596 rtc_date_offset
= -1;
1598 if (sscanf(startdate
, "%d-%d-%dT%d:%d:%d",
1606 } else if (sscanf(startdate
, "%d-%d-%d",
1609 &tm
.tm_mday
) == 3) {
1618 rtc_start_date
= mktimegm(&tm
);
1619 if (rtc_start_date
== -1) {
1621 fprintf(stderr
, "Invalid date format. Valid formats are:\n"
1622 "'2006-06-17T16:01:21' or '2006-06-17'\n");
1625 rtc_date_offset
= time(NULL
) - rtc_start_date
;
1629 static void configure_rtc(QemuOpts
*opts
)
1633 value
= qemu_opt_get(opts
, "base");
1635 if (!strcmp(value
, "utc")) {
1637 } else if (!strcmp(value
, "localtime")) {
1640 configure_rtc_date_offset(value
, 0);
1643 value
= qemu_opt_get(opts
, "clock");
1645 if (!strcmp(value
, "host")) {
1646 rtc_clock
= host_clock
;
1647 } else if (!strcmp(value
, "vm")) {
1648 rtc_clock
= vm_clock
;
1650 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1654 #ifdef CONFIG_TARGET_I386
1655 value
= qemu_opt_get(opts
, "driftfix");
1657 if (!strcmp(buf
, "slew")) {
1659 } else if (!strcmp(buf
, "none")) {
1662 fprintf(stderr
, "qemu: invalid option value '%s'\n", value
);
1670 static void socket_cleanup(void)
1675 static int socket_init(void)
1680 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1682 err
= WSAGetLastError();
1683 fprintf(stderr
, "WSAStartup: %d\n", err
);
1686 atexit(socket_cleanup
);
1691 /***********************************************************/
1692 /* Bluetooth support */
1695 static struct HCIInfo
*hci_table
[MAX_NICS
];
1697 static struct bt_vlan_s
{
1698 struct bt_scatternet_s net
;
1700 struct bt_vlan_s
*next
;
1703 /* find or alloc a new bluetooth "VLAN" */
1704 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1706 struct bt_vlan_s
**pvlan
, *vlan
;
1707 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1711 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1713 pvlan
= &first_bt_vlan
;
1714 while (*pvlan
!= NULL
)
1715 pvlan
= &(*pvlan
)->next
;
1720 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1724 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1729 static struct HCIInfo null_hci
= {
1730 .cmd_send
= null_hci_send
,
1731 .sco_send
= null_hci_send
,
1732 .acl_send
= null_hci_send
,
1733 .bdaddr_set
= null_hci_addr_set
,
1736 struct HCIInfo
*qemu_next_hci(void)
1738 if (cur_hci
== nb_hcis
)
1741 return hci_table
[cur_hci
++];
1744 static struct HCIInfo
*hci_init(const char *str
)
1747 struct bt_scatternet_s
*vlan
= 0;
1749 if (!strcmp(str
, "null"))
1752 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1754 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1755 else if (!strncmp(str
, "hci", 3)) {
1758 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1759 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1764 vlan
= qemu_find_bt_vlan(0);
1766 return bt_new_hci(vlan
);
1769 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1774 static int bt_hci_parse(const char *str
)
1776 struct HCIInfo
*hci
;
1779 if (nb_hcis
>= MAX_NICS
) {
1780 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1784 hci
= hci_init(str
);
1793 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1794 hci
->bdaddr_set(hci
, bdaddr
.b
);
1796 hci_table
[nb_hcis
++] = hci
;
1801 static void bt_vhci_add(int vlan_id
)
1803 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1806 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1807 "an empty scatternet %i\n", vlan_id
);
1809 bt_vhci_init(bt_new_hci(vlan
));
1812 static struct bt_device_s
*bt_device_add(const char *opt
)
1814 struct bt_scatternet_s
*vlan
;
1816 char *endp
= strstr(opt
, ",vlan=");
1817 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1820 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1823 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1825 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1830 vlan
= qemu_find_bt_vlan(vlan_id
);
1833 fprintf(stderr
, "qemu: warning: adding a slave device to "
1834 "an empty scatternet %i\n", vlan_id
);
1836 if (!strcmp(devname
, "keyboard"))
1837 return bt_keyboard_init(vlan
);
1839 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1843 static int bt_parse(const char *opt
)
1845 const char *endp
, *p
;
1848 if (strstart(opt
, "hci", &endp
)) {
1849 if (!*endp
|| *endp
== ',') {
1851 if (!strstart(endp
, ",vlan=", 0))
1854 return bt_hci_parse(opt
);
1856 } else if (strstart(opt
, "vhci", &endp
)) {
1857 if (!*endp
|| *endp
== ',') {
1859 if (strstart(endp
, ",vlan=", &p
)) {
1860 vlan
= strtol(p
, (char **) &endp
, 0);
1862 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1866 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1875 } else if (strstart(opt
, "device:", &endp
))
1876 return !bt_device_add(endp
);
1878 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1882 /***********************************************************/
1883 /* QEMU Block devices */
1885 #define HD_ALIAS "index=%d,media=disk"
1886 #define CDROM_ALIAS "index=2,media=cdrom"
1887 #define FD_ALIAS "index=%d,if=floppy"
1888 #define PFLASH_ALIAS "if=pflash"
1889 #define MTD_ALIAS "if=mtd"
1890 #define SD_ALIAS "index=0,if=sd"
1892 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1899 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1902 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1904 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1905 __FUNCTION__
, optstr
);
1909 qemu_opt_set(opts
, "file", file
);
1913 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1917 /* seek interface, bus and unit */
1919 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1920 if (dinfo
->type
== type
&&
1921 dinfo
->bus
== bus
&&
1922 dinfo
->unit
== unit
)
1929 DriveInfo
*drive_get_by_id(const char *id
)
1933 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1934 if (strcmp(id
, dinfo
->id
))
1941 int drive_get_max_bus(BlockInterfaceType type
)
1947 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1948 if(dinfo
->type
== type
&&
1949 dinfo
->bus
> max_bus
)
1950 max_bus
= dinfo
->bus
;
1955 const char *drive_get_serial(BlockDriverState
*bdrv
)
1959 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1960 if (dinfo
->bdrv
== bdrv
)
1961 return dinfo
->serial
;
1967 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1971 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1972 if (dinfo
->bdrv
== bdrv
)
1973 return dinfo
->onerror
;
1976 return BLOCK_ERR_STOP_ENOSPC
;
1979 static void bdrv_format_print(void *opaque
, const char *name
)
1981 fprintf(stderr
, " %s", name
);
1984 void drive_uninit(DriveInfo
*dinfo
)
1986 qemu_opts_del(dinfo
->opts
);
1987 bdrv_delete(dinfo
->bdrv
);
1988 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1992 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1996 const char *file
= NULL
;
1999 const char *mediastr
= "";
2000 BlockInterfaceType type
;
2001 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
2002 int bus_id
, unit_id
;
2003 int cyls
, heads
, secs
, translation
;
2004 BlockDriver
*drv
= NULL
;
2005 QEMUMachine
*machine
= opaque
;
2010 int bdrv_flags
, onerror
;
2011 const char *devaddr
;
2017 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2020 if (machine
&& machine
->use_scsi
) {
2022 max_devs
= MAX_SCSI_DEVS
;
2023 pstrcpy(devname
, sizeof(devname
), "scsi");
2026 max_devs
= MAX_IDE_DEVS
;
2027 pstrcpy(devname
, sizeof(devname
), "ide");
2031 /* extract parameters */
2032 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2033 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2034 index
= qemu_opt_get_number(opts
, "index", -1);
2036 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2037 heads
= qemu_opt_get_number(opts
, "heads", 0);
2038 secs
= qemu_opt_get_number(opts
, "secs", 0);
2040 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2042 file
= qemu_opt_get(opts
, "file");
2043 serial
= qemu_opt_get(opts
, "serial");
2045 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2046 pstrcpy(devname
, sizeof(devname
), buf
);
2047 if (!strcmp(buf
, "ide")) {
2049 max_devs
= MAX_IDE_DEVS
;
2050 } else if (!strcmp(buf
, "scsi")) {
2052 max_devs
= MAX_SCSI_DEVS
;
2053 } else if (!strcmp(buf
, "floppy")) {
2056 } else if (!strcmp(buf
, "pflash")) {
2059 } else if (!strcmp(buf
, "mtd")) {
2062 } else if (!strcmp(buf
, "sd")) {
2065 } else if (!strcmp(buf
, "virtio")) {
2068 } else if (!strcmp(buf
, "xen")) {
2071 } else if (!strcmp(buf
, "none")) {
2075 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2080 if (cyls
|| heads
|| secs
) {
2081 if (cyls
< 1 || cyls
> 16383) {
2082 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2085 if (heads
< 1 || heads
> 16) {
2086 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2089 if (secs
< 1 || secs
> 63) {
2090 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2095 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2098 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2102 if (!strcmp(buf
, "none"))
2103 translation
= BIOS_ATA_TRANSLATION_NONE
;
2104 else if (!strcmp(buf
, "lba"))
2105 translation
= BIOS_ATA_TRANSLATION_LBA
;
2106 else if (!strcmp(buf
, "auto"))
2107 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2109 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2114 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2115 if (!strcmp(buf
, "disk")) {
2117 } else if (!strcmp(buf
, "cdrom")) {
2118 if (cyls
|| secs
|| heads
) {
2120 "qemu: '%s' invalid physical CHS format\n", buf
);
2123 media
= MEDIA_CDROM
;
2125 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2130 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2131 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2133 else if (!strcmp(buf
, "writethrough"))
2135 else if (!strcmp(buf
, "writeback"))
2138 fprintf(stderr
, "qemu: invalid cache option\n");
2143 #ifdef CONFIG_LINUX_AIO
2144 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2145 if (!strcmp(buf
, "threads"))
2147 else if (!strcmp(buf
, "native"))
2150 fprintf(stderr
, "qemu: invalid aio option\n");
2156 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2157 if (strcmp(buf
, "?") == 0) {
2158 fprintf(stderr
, "qemu: Supported formats:");
2159 bdrv_iterate_format(bdrv_format_print
, NULL
);
2160 fprintf(stderr
, "\n");
2163 drv
= bdrv_find_format(buf
);
2165 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2170 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2171 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2172 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2173 fprintf(stderr
, "werror is no supported by this format\n");
2176 if (!strcmp(buf
, "ignore"))
2177 onerror
= BLOCK_ERR_IGNORE
;
2178 else if (!strcmp(buf
, "enospc"))
2179 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2180 else if (!strcmp(buf
, "stop"))
2181 onerror
= BLOCK_ERR_STOP_ANY
;
2182 else if (!strcmp(buf
, "report"))
2183 onerror
= BLOCK_ERR_REPORT
;
2185 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2190 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2191 if (type
!= IF_VIRTIO
) {
2192 fprintf(stderr
, "addr is not supported\n");
2197 /* compute bus and unit according index */
2200 if (bus_id
!= 0 || unit_id
!= -1) {
2202 "qemu: index cannot be used with bus and unit\n");
2210 unit_id
= index
% max_devs
;
2211 bus_id
= index
/ max_devs
;
2215 /* if user doesn't specify a unit_id,
2216 * try to find the first free
2219 if (unit_id
== -1) {
2221 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2223 if (max_devs
&& unit_id
>= max_devs
) {
2224 unit_id
-= max_devs
;
2232 if (max_devs
&& unit_id
>= max_devs
) {
2233 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2234 unit_id
, max_devs
- 1);
2239 * ignore multiple definitions
2242 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2249 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2250 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2251 dinfo
->id
= qemu_strdup(buf
);
2253 /* no id supplied -> create one */
2254 dinfo
->id
= qemu_mallocz(32);
2255 if (type
== IF_IDE
|| type
== IF_SCSI
)
2256 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2258 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2259 devname
, bus_id
, mediastr
, unit_id
);
2261 snprintf(dinfo
->id
, 32, "%s%s%i",
2262 devname
, mediastr
, unit_id
);
2264 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2265 dinfo
->devaddr
= devaddr
;
2267 dinfo
->bus
= bus_id
;
2268 dinfo
->unit
= unit_id
;
2269 dinfo
->onerror
= onerror
;
2272 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2273 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2283 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2284 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2288 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2293 /* FIXME: This isn't really a floppy, but it's a reasonable
2296 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2302 /* add virtio block device */
2303 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2304 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2305 qemu_opt_set(opts
, "drive", dinfo
->id
);
2307 qemu_opt_set(opts
, "addr", devaddr
);
2318 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2319 cache
= 2; /* always use write-back with snapshot */
2321 if (cache
== 0) /* no caching */
2322 bdrv_flags
|= BDRV_O_NOCACHE
;
2323 else if (cache
== 2) /* write-back */
2324 bdrv_flags
|= BDRV_O_CACHE_WB
;
2327 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2329 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2332 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2333 fprintf(stderr
, "qemu: could not open disk image %s: %s\n",
2334 file
, strerror(errno
));
2338 if (bdrv_key_required(dinfo
->bdrv
))
2344 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2346 QEMUMachine
*machine
= opaque
;
2347 int fatal_error
= 0;
2349 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2356 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2358 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2359 qemu_opt_set(opts
, "snapshot", "on");
2364 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2366 boot_set_handler
= func
;
2367 boot_set_opaque
= opaque
;
2370 int qemu_boot_set(const char *boot_devices
)
2372 if (!boot_set_handler
) {
2375 return boot_set_handler(boot_set_opaque
, boot_devices
);
2378 static int parse_bootdevices(char *devices
)
2380 /* We just do some generic consistency checks */
2384 for (p
= devices
; *p
!= '\0'; p
++) {
2385 /* Allowed boot devices are:
2386 * a-b: floppy disk drives
2387 * c-f: IDE disk drives
2388 * g-m: machine implementation dependant drives
2389 * n-p: network devices
2390 * It's up to each machine implementation to check if the given boot
2391 * devices match the actual hardware implementation and firmware
2394 if (*p
< 'a' || *p
> 'p') {
2395 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2398 if (bitmap
& (1 << (*p
- 'a'))) {
2399 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2402 bitmap
|= 1 << (*p
- 'a');
2407 static void restore_boot_devices(void *opaque
)
2409 char *standard_boot_devices
= opaque
;
2411 qemu_boot_set(standard_boot_devices
);
2413 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2414 qemu_free(standard_boot_devices
);
2417 static void numa_add(const char *optarg
)
2421 unsigned long long value
, endvalue
;
2424 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2425 if (!strcmp(option
, "node")) {
2426 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2427 nodenr
= nb_numa_nodes
;
2429 nodenr
= strtoull(option
, NULL
, 10);
2432 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2433 node_mem
[nodenr
] = 0;
2435 value
= strtoull(option
, &endptr
, 0);
2437 case 0: case 'M': case 'm':
2444 node_mem
[nodenr
] = value
;
2446 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2447 node_cpumask
[nodenr
] = 0;
2449 value
= strtoull(option
, &endptr
, 10);
2452 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2454 if (*endptr
== '-') {
2455 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2456 if (endvalue
>= 63) {
2459 "only 63 CPUs in NUMA mode supported.\n");
2461 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2466 node_cpumask
[nodenr
] = value
;
2473 static void smp_parse(const char *optarg
)
2475 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2479 smp
= strtoul(optarg
, &endptr
, 10);
2480 if (endptr
!= optarg
) {
2481 if (*endptr
== ',') {
2485 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2486 sockets
= strtoull(option
, NULL
, 10);
2487 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2488 cores
= strtoull(option
, NULL
, 10);
2489 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2490 threads
= strtoull(option
, NULL
, 10);
2491 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2492 max_cpus
= strtoull(option
, NULL
, 10);
2494 /* compute missing values, prefer sockets over cores over threads */
2495 if (smp
== 0 || sockets
== 0) {
2496 sockets
= sockets
> 0 ? sockets
: 1;
2497 cores
= cores
> 0 ? cores
: 1;
2498 threads
= threads
> 0 ? threads
: 1;
2500 smp
= cores
* threads
* sockets
;
2502 sockets
= smp
/ (cores
* threads
);
2506 threads
= threads
> 0 ? threads
: 1;
2507 cores
= smp
/ (sockets
* threads
);
2510 sockets
= smp
/ (cores
* threads
);
2512 threads
= smp
/ (cores
* sockets
);
2517 smp_cores
= cores
> 0 ? cores
: 1;
2518 smp_threads
= threads
> 0 ? threads
: 1;
2520 max_cpus
= smp_cpus
;
2523 /***********************************************************/
2526 static void usb_msd_password_cb(void *opaque
, int err
)
2528 USBDevice
*dev
= opaque
;
2531 usb_device_attach(dev
);
2533 dev
->info
->handle_destroy(dev
);
2542 .qdev
= "QEMU USB Mouse",
2545 .qdev
= "QEMU USB Tablet",
2548 .qdev
= "QEMU USB Keyboard",
2550 .name
= "wacom-tablet",
2551 .qdev
= "QEMU PenPartner Tablet",
2555 static int usb_device_add(const char *devname
, int is_hotplug
)
2558 USBBus
*bus
= usb_bus_find(-1 /* any */);
2559 USBDevice
*dev
= NULL
;
2565 /* simple devices which don't need extra care */
2566 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2567 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2569 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2573 /* the other ones */
2574 if (strstart(devname
, "host:", &p
)) {
2575 dev
= usb_host_device_open(p
);
2576 } else if (strstart(devname
, "disk:", &p
)) {
2577 BlockDriverState
*bs
;
2579 dev
= usb_msd_init(p
);
2582 bs
= usb_msd_get_bdrv(dev
);
2583 if (bdrv_key_required(bs
)) {
2586 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2591 } else if (strstart(devname
, "serial:", &p
)) {
2592 dev
= usb_serial_init(p
);
2593 #ifdef CONFIG_BRLAPI
2594 } else if (!strcmp(devname
, "braille")) {
2595 dev
= usb_baum_init();
2597 } else if (strstart(devname
, "net:", &p
)) {
2601 opts
= qemu_opts_parse(&qemu_net_opts
, p
, NULL
);
2606 qemu_opt_set(opts
, "type", "nic");
2607 qemu_opt_set(opts
, "model", "usb");
2609 idx
= net_client_init(NULL
, opts
);
2614 dev
= usb_net_init(&nd_table
[idx
]);
2615 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2616 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2617 bt_new_hci(qemu_find_bt_vlan(0)));
2628 static int usb_device_del(const char *devname
)
2633 if (strstart(devname
, "host:", &p
))
2634 return usb_host_device_close(p
);
2639 p
= strchr(devname
, '.');
2642 bus_num
= strtoul(devname
, NULL
, 0);
2643 addr
= strtoul(p
+ 1, NULL
, 0);
2645 return usb_device_delete_addr(bus_num
, addr
);
2648 static int usb_parse(const char *cmdline
)
2650 return usb_device_add(cmdline
, 0);
2653 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2655 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2658 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2660 usb_device_del(qdict_get_str(qdict
, "devname"));
2663 /***********************************************************/
2664 /* PCMCIA/Cardbus */
2666 static struct pcmcia_socket_entry_s
{
2667 PCMCIASocket
*socket
;
2668 struct pcmcia_socket_entry_s
*next
;
2669 } *pcmcia_sockets
= 0;
2671 void pcmcia_socket_register(PCMCIASocket
*socket
)
2673 struct pcmcia_socket_entry_s
*entry
;
2675 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2676 entry
->socket
= socket
;
2677 entry
->next
= pcmcia_sockets
;
2678 pcmcia_sockets
= entry
;
2681 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2683 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2685 ptr
= &pcmcia_sockets
;
2686 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2687 if (entry
->socket
== socket
) {
2693 void pcmcia_info(Monitor
*mon
)
2695 struct pcmcia_socket_entry_s
*iter
;
2697 if (!pcmcia_sockets
)
2698 monitor_printf(mon
, "No PCMCIA sockets\n");
2700 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2701 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2702 iter
->socket
->attached
? iter
->socket
->card_string
:
2706 /***********************************************************/
2707 /* register display */
2709 struct DisplayAllocator default_allocator
= {
2710 defaultallocator_create_displaysurface
,
2711 defaultallocator_resize_displaysurface
,
2712 defaultallocator_free_displaysurface
2715 void register_displaystate(DisplayState
*ds
)
2725 DisplayState
*get_displaystate(void)
2727 return display_state
;
2730 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2732 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2733 return ds
->allocator
;
2738 static void dumb_display_init(void)
2740 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2741 ds
->allocator
= &default_allocator
;
2742 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2743 register_displaystate(ds
);
2746 /***********************************************************/
2749 typedef struct IOHandlerRecord
{
2751 IOCanRWHandler
*fd_read_poll
;
2753 IOHandler
*fd_write
;
2756 /* temporary data */
2758 struct IOHandlerRecord
*next
;
2761 static IOHandlerRecord
*first_io_handler
;
2763 /* XXX: fd_read_poll should be suppressed, but an API change is
2764 necessary in the character devices to suppress fd_can_read(). */
2765 int qemu_set_fd_handler2(int fd
,
2766 IOCanRWHandler
*fd_read_poll
,
2768 IOHandler
*fd_write
,
2771 IOHandlerRecord
**pioh
, *ioh
;
2773 if (!fd_read
&& !fd_write
) {
2774 pioh
= &first_io_handler
;
2779 if (ioh
->fd
== fd
) {
2786 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2790 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2791 ioh
->next
= first_io_handler
;
2792 first_io_handler
= ioh
;
2795 ioh
->fd_read_poll
= fd_read_poll
;
2796 ioh
->fd_read
= fd_read
;
2797 ioh
->fd_write
= fd_write
;
2798 ioh
->opaque
= opaque
;
2804 int qemu_set_fd_handler(int fd
,
2806 IOHandler
*fd_write
,
2809 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2813 /***********************************************************/
2814 /* Polling handling */
2816 typedef struct PollingEntry
{
2819 struct PollingEntry
*next
;
2822 static PollingEntry
*first_polling_entry
;
2824 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2826 PollingEntry
**ppe
, *pe
;
2827 pe
= qemu_mallocz(sizeof(PollingEntry
));
2829 pe
->opaque
= opaque
;
2830 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2835 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2837 PollingEntry
**ppe
, *pe
;
2838 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2840 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2848 /***********************************************************/
2849 /* Wait objects support */
2850 typedef struct WaitObjects
{
2852 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2853 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2854 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2857 static WaitObjects wait_objects
= {0};
2859 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2861 WaitObjects
*w
= &wait_objects
;
2863 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2865 w
->events
[w
->num
] = handle
;
2866 w
->func
[w
->num
] = func
;
2867 w
->opaque
[w
->num
] = opaque
;
2872 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2875 WaitObjects
*w
= &wait_objects
;
2878 for (i
= 0; i
< w
->num
; i
++) {
2879 if (w
->events
[i
] == handle
)
2882 w
->events
[i
] = w
->events
[i
+ 1];
2883 w
->func
[i
] = w
->func
[i
+ 1];
2884 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2892 /***********************************************************/
2893 /* ram save/restore */
2895 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2896 #define RAM_SAVE_FLAG_COMPRESS 0x02
2897 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2898 #define RAM_SAVE_FLAG_PAGE 0x08
2899 #define RAM_SAVE_FLAG_EOS 0x10
2901 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2903 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2904 uint32_t *array
= (uint32_t *)page
;
2907 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2908 if (array
[i
] != val
)
2915 static int ram_save_block(QEMUFile
*f
)
2917 static ram_addr_t current_addr
= 0;
2918 ram_addr_t saved_addr
= current_addr
;
2919 ram_addr_t addr
= 0;
2922 while (addr
< last_ram_offset
) {
2923 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2926 cpu_physical_memory_reset_dirty(current_addr
,
2927 current_addr
+ TARGET_PAGE_SIZE
,
2928 MIGRATION_DIRTY_FLAG
);
2930 p
= qemu_get_ram_ptr(current_addr
);
2932 if (is_dup_page(p
, *p
)) {
2933 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2934 qemu_put_byte(f
, *p
);
2936 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2937 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2943 addr
+= TARGET_PAGE_SIZE
;
2944 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2950 static uint64_t bytes_transferred
= 0;
2952 static ram_addr_t
ram_save_remaining(void)
2955 ram_addr_t count
= 0;
2957 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2958 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2965 uint64_t ram_bytes_remaining(void)
2967 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2970 uint64_t ram_bytes_transferred(void)
2972 return bytes_transferred
;
2975 uint64_t ram_bytes_total(void)
2977 return last_ram_offset
;
2980 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2983 uint64_t bytes_transferred_last
;
2985 uint64_t expected_time
= 0;
2987 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2988 qemu_file_set_error(f
);
2993 /* Make sure all dirty bits are set */
2994 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2995 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2996 cpu_physical_memory_set_dirty(addr
);
2999 /* Enable dirty memory tracking */
3000 cpu_physical_memory_set_dirty_tracking(1);
3002 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3005 bytes_transferred_last
= bytes_transferred
;
3006 bwidth
= get_clock();
3008 while (!qemu_file_rate_limit(f
)) {
3011 ret
= ram_save_block(f
);
3012 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3013 if (ret
== 0) /* no more blocks */
3017 bwidth
= get_clock() - bwidth
;
3018 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3020 /* if we haven't transferred anything this round, force expected_time to a
3021 * a very high value, but without crashing */
3025 /* try transferring iterative blocks of memory */
3029 /* flush all remaining blocks regardless of rate limiting */
3030 while (ram_save_block(f
) != 0) {
3031 bytes_transferred
+= TARGET_PAGE_SIZE
;
3033 cpu_physical_memory_set_dirty_tracking(0);
3036 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3038 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3040 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3043 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3048 if (version_id
!= 3)
3052 addr
= qemu_get_be64(f
);
3054 flags
= addr
& ~TARGET_PAGE_MASK
;
3055 addr
&= TARGET_PAGE_MASK
;
3057 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3058 if (addr
!= last_ram_offset
)
3062 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3063 uint8_t ch
= qemu_get_byte(f
);
3064 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3067 (!kvm_enabled() || kvm_has_sync_mmu())) {
3068 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3071 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3072 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3073 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3078 void qemu_service_io(void)
3080 qemu_notify_event();
3083 /***********************************************************/
3084 /* bottom halves (can be seen as timers which expire ASAP) */
3095 static QEMUBH
*first_bh
= NULL
;
3097 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3100 bh
= qemu_mallocz(sizeof(QEMUBH
));
3102 bh
->opaque
= opaque
;
3103 bh
->next
= first_bh
;
3108 int qemu_bh_poll(void)
3114 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3115 if (!bh
->deleted
&& bh
->scheduled
) {
3124 /* remove deleted bhs */
3138 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3146 void qemu_bh_schedule(QEMUBH
*bh
)
3152 /* stop the currently executing CPU to execute the BH ASAP */
3153 qemu_notify_event();
3156 void qemu_bh_cancel(QEMUBH
*bh
)
3161 void qemu_bh_delete(QEMUBH
*bh
)
3167 static void qemu_bh_update_timeout(int *timeout
)
3171 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3172 if (!bh
->deleted
&& bh
->scheduled
) {
3174 /* idle bottom halves will be polled at least
3176 *timeout
= MIN(10, *timeout
);
3178 /* non-idle bottom halves will be executed
3187 /***********************************************************/
3188 /* machine registration */
3190 static QEMUMachine
*first_machine
= NULL
;
3191 QEMUMachine
*current_machine
= NULL
;
3193 int qemu_register_machine(QEMUMachine
*m
)
3196 pm
= &first_machine
;
3204 static QEMUMachine
*find_machine(const char *name
)
3208 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3209 if (!strcmp(m
->name
, name
))
3211 if (m
->alias
&& !strcmp(m
->alias
, name
))
3217 static QEMUMachine
*find_default_machine(void)
3221 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3222 if (m
->is_default
) {
3229 /***********************************************************/
3230 /* main execution loop */
3232 static void gui_update(void *opaque
)
3234 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3235 DisplayState
*ds
= opaque
;
3236 DisplayChangeListener
*dcl
= ds
->listeners
;
3240 while (dcl
!= NULL
) {
3241 if (dcl
->gui_timer_interval
&&
3242 dcl
->gui_timer_interval
< interval
)
3243 interval
= dcl
->gui_timer_interval
;
3246 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3249 static void nographic_update(void *opaque
)
3251 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3253 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3256 struct vm_change_state_entry
{
3257 VMChangeStateHandler
*cb
;
3259 QLIST_ENTRY (vm_change_state_entry
) entries
;
3262 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3264 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3267 VMChangeStateEntry
*e
;
3269 e
= qemu_mallocz(sizeof (*e
));
3273 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3277 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3279 QLIST_REMOVE (e
, entries
);
3283 static void vm_state_notify(int running
, int reason
)
3285 VMChangeStateEntry
*e
;
3287 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3288 e
->cb(e
->opaque
, running
, reason
);
3292 static void resume_all_vcpus(void);
3293 static void pause_all_vcpus(void);
3300 vm_state_notify(1, 0);
3301 qemu_rearm_alarm_timer(alarm_timer
);
3306 /* reset/shutdown handler */
3308 typedef struct QEMUResetEntry
{
3309 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3310 QEMUResetHandler
*func
;
3314 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3315 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3316 static int reset_requested
;
3317 static int shutdown_requested
;
3318 static int powerdown_requested
;
3319 static int debug_requested
;
3320 static int vmstop_requested
;
3322 int qemu_shutdown_requested(void)
3324 int r
= shutdown_requested
;
3325 shutdown_requested
= 0;
3329 int qemu_reset_requested(void)
3331 int r
= reset_requested
;
3332 reset_requested
= 0;
3336 int qemu_powerdown_requested(void)
3338 int r
= powerdown_requested
;
3339 powerdown_requested
= 0;
3343 static int qemu_debug_requested(void)
3345 int r
= debug_requested
;
3346 debug_requested
= 0;
3350 static int qemu_vmstop_requested(void)
3352 int r
= vmstop_requested
;
3353 vmstop_requested
= 0;
3357 static void do_vm_stop(int reason
)
3360 cpu_disable_ticks();
3363 vm_state_notify(0, reason
);
3367 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3369 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3372 re
->opaque
= opaque
;
3373 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3376 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3380 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3381 if (re
->func
== func
&& re
->opaque
== opaque
) {
3382 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3389 void qemu_system_reset(void)
3391 QEMUResetEntry
*re
, *nre
;
3393 /* reset all devices */
3394 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3395 re
->func(re
->opaque
);
3399 void qemu_system_reset_request(void)
3402 shutdown_requested
= 1;
3404 reset_requested
= 1;
3406 qemu_notify_event();
3409 void qemu_system_shutdown_request(void)
3411 shutdown_requested
= 1;
3412 qemu_notify_event();
3415 void qemu_system_powerdown_request(void)
3417 powerdown_requested
= 1;
3418 qemu_notify_event();
3421 #ifdef CONFIG_IOTHREAD
3422 static void qemu_system_vmstop_request(int reason
)
3424 vmstop_requested
= reason
;
3425 qemu_notify_event();
3430 static int io_thread_fd
= -1;
3432 static void qemu_event_increment(void)
3434 static const char byte
= 0;
3436 if (io_thread_fd
== -1)
3439 write(io_thread_fd
, &byte
, sizeof(byte
));
3442 static void qemu_event_read(void *opaque
)
3444 int fd
= (unsigned long)opaque
;
3447 /* Drain the notify pipe */
3450 len
= read(fd
, buffer
, sizeof(buffer
));
3451 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3454 static int qemu_event_init(void)
3463 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3467 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3471 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3472 (void *)(unsigned long)fds
[0]);
3474 io_thread_fd
= fds
[1];
3483 HANDLE qemu_event_handle
;
3485 static void dummy_event_handler(void *opaque
)
3489 static int qemu_event_init(void)
3491 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3492 if (!qemu_event_handle
) {
3493 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3496 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3500 static void qemu_event_increment(void)
3502 if (!SetEvent(qemu_event_handle
)) {
3503 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3510 static int cpu_can_run(CPUState
*env
)
3519 #ifndef CONFIG_IOTHREAD
3520 static int qemu_init_main_loop(void)
3522 return qemu_event_init();
3525 void qemu_init_vcpu(void *_env
)
3527 CPUState
*env
= _env
;
3531 env
->nr_cores
= smp_cores
;
3532 env
->nr_threads
= smp_threads
;
3536 int qemu_cpu_self(void *env
)
3541 static void resume_all_vcpus(void)
3545 static void pause_all_vcpus(void)
3549 void qemu_cpu_kick(void *env
)
3554 void qemu_notify_event(void)
3556 CPUState
*env
= cpu_single_env
;
3563 #define qemu_mutex_lock_iothread() do { } while (0)
3564 #define qemu_mutex_unlock_iothread() do { } while (0)
3566 void vm_stop(int reason
)
3571 #else /* CONFIG_IOTHREAD */
3573 #include "qemu-thread.h"
3575 QemuMutex qemu_global_mutex
;
3576 static QemuMutex qemu_fair_mutex
;
3578 static QemuThread io_thread
;
3580 static QemuThread
*tcg_cpu_thread
;
3581 static QemuCond
*tcg_halt_cond
;
3583 static int qemu_system_ready
;
3585 static QemuCond qemu_cpu_cond
;
3587 static QemuCond qemu_system_cond
;
3588 static QemuCond qemu_pause_cond
;
3590 static void block_io_signals(void);
3591 static void unblock_io_signals(void);
3592 static int tcg_has_work(void);
3594 static int qemu_init_main_loop(void)
3598 ret
= qemu_event_init();
3602 qemu_cond_init(&qemu_pause_cond
);
3603 qemu_mutex_init(&qemu_fair_mutex
);
3604 qemu_mutex_init(&qemu_global_mutex
);
3605 qemu_mutex_lock(&qemu_global_mutex
);
3607 unblock_io_signals();
3608 qemu_thread_self(&io_thread
);
3613 static void qemu_wait_io_event(CPUState
*env
)
3615 while (!tcg_has_work())
3616 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3618 qemu_mutex_unlock(&qemu_global_mutex
);
3621 * Users of qemu_global_mutex can be starved, having no chance
3622 * to acquire it since this path will get to it first.
3623 * So use another lock to provide fairness.
3625 qemu_mutex_lock(&qemu_fair_mutex
);
3626 qemu_mutex_unlock(&qemu_fair_mutex
);
3628 qemu_mutex_lock(&qemu_global_mutex
);
3632 qemu_cond_signal(&qemu_pause_cond
);
3636 static int qemu_cpu_exec(CPUState
*env
);
3638 static void *kvm_cpu_thread_fn(void *arg
)
3640 CPUState
*env
= arg
;
3643 qemu_thread_self(env
->thread
);
3647 /* signal CPU creation */
3648 qemu_mutex_lock(&qemu_global_mutex
);
3650 qemu_cond_signal(&qemu_cpu_cond
);
3652 /* and wait for machine initialization */
3653 while (!qemu_system_ready
)
3654 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3657 if (cpu_can_run(env
))
3659 qemu_wait_io_event(env
);
3665 static void tcg_cpu_exec(void);
3667 static void *tcg_cpu_thread_fn(void *arg
)
3669 CPUState
*env
= arg
;
3672 qemu_thread_self(env
->thread
);
3674 /* signal CPU creation */
3675 qemu_mutex_lock(&qemu_global_mutex
);
3676 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3678 qemu_cond_signal(&qemu_cpu_cond
);
3680 /* and wait for machine initialization */
3681 while (!qemu_system_ready
)
3682 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3686 qemu_wait_io_event(cur_cpu
);
3692 void qemu_cpu_kick(void *_env
)
3694 CPUState
*env
= _env
;
3695 qemu_cond_broadcast(env
->halt_cond
);
3697 qemu_thread_signal(env
->thread
, SIGUSR1
);
3700 int qemu_cpu_self(void *_env
)
3702 CPUState
*env
= _env
;
3705 qemu_thread_self(&this);
3707 return qemu_thread_equal(&this, env
->thread
);
3710 static void cpu_signal(int sig
)
3713 cpu_exit(cpu_single_env
);
3716 static void block_io_signals(void)
3719 struct sigaction sigact
;
3722 sigaddset(&set
, SIGUSR2
);
3723 sigaddset(&set
, SIGIO
);
3724 sigaddset(&set
, SIGALRM
);
3725 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3728 sigaddset(&set
, SIGUSR1
);
3729 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3731 memset(&sigact
, 0, sizeof(sigact
));
3732 sigact
.sa_handler
= cpu_signal
;
3733 sigaction(SIGUSR1
, &sigact
, NULL
);
3736 static void unblock_io_signals(void)
3741 sigaddset(&set
, SIGUSR2
);
3742 sigaddset(&set
, SIGIO
);
3743 sigaddset(&set
, SIGALRM
);
3744 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3747 sigaddset(&set
, SIGUSR1
);
3748 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3751 static void qemu_signal_lock(unsigned int msecs
)
3753 qemu_mutex_lock(&qemu_fair_mutex
);
3755 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3756 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3757 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3760 qemu_mutex_unlock(&qemu_fair_mutex
);
3763 static void qemu_mutex_lock_iothread(void)
3765 if (kvm_enabled()) {
3766 qemu_mutex_lock(&qemu_fair_mutex
);
3767 qemu_mutex_lock(&qemu_global_mutex
);
3768 qemu_mutex_unlock(&qemu_fair_mutex
);
3770 qemu_signal_lock(100);
3773 static void qemu_mutex_unlock_iothread(void)
3775 qemu_mutex_unlock(&qemu_global_mutex
);
3778 static int all_vcpus_paused(void)
3780 CPUState
*penv
= first_cpu
;
3785 penv
= (CPUState
*)penv
->next_cpu
;
3791 static void pause_all_vcpus(void)
3793 CPUState
*penv
= first_cpu
;
3797 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3798 qemu_cpu_kick(penv
);
3799 penv
= (CPUState
*)penv
->next_cpu
;
3802 while (!all_vcpus_paused()) {
3803 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3806 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3807 penv
= (CPUState
*)penv
->next_cpu
;
3812 static void resume_all_vcpus(void)
3814 CPUState
*penv
= first_cpu
;
3819 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3820 qemu_cpu_kick(penv
);
3821 penv
= (CPUState
*)penv
->next_cpu
;
3825 static void tcg_init_vcpu(void *_env
)
3827 CPUState
*env
= _env
;
3828 /* share a single thread for all cpus with TCG */
3829 if (!tcg_cpu_thread
) {
3830 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3831 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3832 qemu_cond_init(env
->halt_cond
);
3833 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3834 while (env
->created
== 0)
3835 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3836 tcg_cpu_thread
= env
->thread
;
3837 tcg_halt_cond
= env
->halt_cond
;
3839 env
->thread
= tcg_cpu_thread
;
3840 env
->halt_cond
= tcg_halt_cond
;
3844 static void kvm_start_vcpu(CPUState
*env
)
3846 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3847 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3848 qemu_cond_init(env
->halt_cond
);
3849 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3850 while (env
->created
== 0)
3851 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3854 void qemu_init_vcpu(void *_env
)
3856 CPUState
*env
= _env
;
3859 kvm_start_vcpu(env
);
3862 env
->nr_cores
= smp_cores
;
3863 env
->nr_threads
= smp_threads
;
3866 void qemu_notify_event(void)
3868 qemu_event_increment();
3871 void vm_stop(int reason
)
3874 qemu_thread_self(&me
);
3876 if (!qemu_thread_equal(&me
, &io_thread
)) {
3877 qemu_system_vmstop_request(reason
);
3879 * FIXME: should not return to device code in case
3880 * vm_stop() has been requested.
3882 if (cpu_single_env
) {
3883 cpu_exit(cpu_single_env
);
3884 cpu_single_env
->stop
= 1;
3895 static void host_main_loop_wait(int *timeout
)
3901 /* XXX: need to suppress polling by better using win32 events */
3903 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3904 ret
|= pe
->func(pe
->opaque
);
3908 WaitObjects
*w
= &wait_objects
;
3910 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3911 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3912 if (w
->func
[ret
- WAIT_OBJECT_0
])
3913 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3915 /* Check for additional signaled events */
3916 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3918 /* Check if event is signaled */
3919 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3920 if(ret2
== WAIT_OBJECT_0
) {
3922 w
->func
[i
](w
->opaque
[i
]);
3923 } else if (ret2
== WAIT_TIMEOUT
) {
3925 err
= GetLastError();
3926 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3929 } else if (ret
== WAIT_TIMEOUT
) {
3931 err
= GetLastError();
3932 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3939 static void host_main_loop_wait(int *timeout
)
3944 void main_loop_wait(int timeout
)
3946 IOHandlerRecord
*ioh
;
3947 fd_set rfds
, wfds
, xfds
;
3951 qemu_bh_update_timeout(&timeout
);
3953 host_main_loop_wait(&timeout
);
3955 /* poll any events */
3956 /* XXX: separate device handlers from system ones */
3961 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3965 (!ioh
->fd_read_poll
||
3966 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3967 FD_SET(ioh
->fd
, &rfds
);
3971 if (ioh
->fd_write
) {
3972 FD_SET(ioh
->fd
, &wfds
);
3978 tv
.tv_sec
= timeout
/ 1000;
3979 tv
.tv_usec
= (timeout
% 1000) * 1000;
3981 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3983 qemu_mutex_unlock_iothread();
3984 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3985 qemu_mutex_lock_iothread();
3987 IOHandlerRecord
**pioh
;
3989 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3990 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3991 ioh
->fd_read(ioh
->opaque
);
3993 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3994 ioh
->fd_write(ioh
->opaque
);
3998 /* remove deleted IO handlers */
3999 pioh
= &first_io_handler
;
4010 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4012 /* rearm timer, if not periodic */
4013 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4014 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4015 qemu_rearm_alarm_timer(alarm_timer
);
4018 /* vm time timers */
4020 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4021 qemu_run_timers(&active_timers
[QEMU_CLOCK_VIRTUAL
],
4022 qemu_get_clock(vm_clock
));
4025 /* real time timers */
4026 qemu_run_timers(&active_timers
[QEMU_CLOCK_REALTIME
],
4027 qemu_get_clock(rt_clock
));
4029 qemu_run_timers(&active_timers
[QEMU_CLOCK_HOST
],
4030 qemu_get_clock(host_clock
));
4032 /* Check bottom-halves last in case any of the earlier events triggered
4038 static int qemu_cpu_exec(CPUState
*env
)
4041 #ifdef CONFIG_PROFILER
4045 #ifdef CONFIG_PROFILER
4046 ti
= profile_getclock();
4051 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4052 env
->icount_decr
.u16
.low
= 0;
4053 env
->icount_extra
= 0;
4054 count
= qemu_next_deadline();
4055 count
= (count
+ (1 << icount_time_shift
) - 1)
4056 >> icount_time_shift
;
4057 qemu_icount
+= count
;
4058 decr
= (count
> 0xffff) ? 0xffff : count
;
4060 env
->icount_decr
.u16
.low
= decr
;
4061 env
->icount_extra
= count
;
4063 ret
= cpu_exec(env
);
4064 #ifdef CONFIG_PROFILER
4065 qemu_time
+= profile_getclock() - ti
;
4068 /* Fold pending instructions back into the
4069 instruction counter, and clear the interrupt flag. */
4070 qemu_icount
-= (env
->icount_decr
.u16
.low
4071 + env
->icount_extra
);
4072 env
->icount_decr
.u32
= 0;
4073 env
->icount_extra
= 0;
4078 static void tcg_cpu_exec(void)
4082 if (next_cpu
== NULL
)
4083 next_cpu
= first_cpu
;
4084 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4085 CPUState
*env
= cur_cpu
= next_cpu
;
4089 if (timer_alarm_pending
) {
4090 timer_alarm_pending
= 0;
4093 if (cpu_can_run(env
))
4094 ret
= qemu_cpu_exec(env
);
4095 if (ret
== EXCP_DEBUG
) {
4096 gdb_set_stop_cpu(env
);
4097 debug_requested
= 1;
4103 static int cpu_has_work(CPUState
*env
)
4111 if (qemu_cpu_has_work(env
))
4116 static int tcg_has_work(void)
4120 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4121 if (cpu_has_work(env
))
4126 static int qemu_calculate_timeout(void)
4128 #ifndef CONFIG_IOTHREAD
4133 else if (tcg_has_work())
4135 else if (!use_icount
)
4138 /* XXX: use timeout computed from timers */
4141 /* Advance virtual time to the next event. */
4142 if (use_icount
== 1) {
4143 /* When not using an adaptive execution frequency
4144 we tend to get badly out of sync with real time,
4145 so just delay for a reasonable amount of time. */
4148 delta
= cpu_get_icount() - cpu_get_clock();
4151 /* If virtual time is ahead of real time then just
4153 timeout
= (delta
/ 1000000) + 1;
4155 /* Wait for either IO to occur or the next
4157 add
= qemu_next_deadline();
4158 /* We advance the timer before checking for IO.
4159 Limit the amount we advance so that early IO
4160 activity won't get the guest too far ahead. */
4164 add
= (add
+ (1 << icount_time_shift
) - 1)
4165 >> icount_time_shift
;
4167 timeout
= delta
/ 1000000;
4174 #else /* CONFIG_IOTHREAD */
4179 static int vm_can_run(void)
4181 if (powerdown_requested
)
4183 if (reset_requested
)
4185 if (shutdown_requested
)
4187 if (debug_requested
)
4192 qemu_irq qemu_system_powerdown
;
4194 static void main_loop(void)
4198 #ifdef CONFIG_IOTHREAD
4199 qemu_system_ready
= 1;
4200 qemu_cond_broadcast(&qemu_system_cond
);
4205 #ifdef CONFIG_PROFILER
4208 #ifndef CONFIG_IOTHREAD
4211 #ifdef CONFIG_PROFILER
4212 ti
= profile_getclock();
4214 main_loop_wait(qemu_calculate_timeout());
4215 #ifdef CONFIG_PROFILER
4216 dev_time
+= profile_getclock() - ti
;
4218 } while (vm_can_run());
4220 if (qemu_debug_requested())
4221 vm_stop(EXCP_DEBUG
);
4222 if (qemu_shutdown_requested()) {
4229 if (qemu_reset_requested()) {
4231 qemu_system_reset();
4234 if (qemu_powerdown_requested()) {
4235 qemu_irq_raise(qemu_system_powerdown
);
4237 if ((r
= qemu_vmstop_requested()))
4243 static void version(void)
4245 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4248 static void help(int exitcode
)
4251 printf("usage: %s [options] [disk_image]\n"
4253 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4255 #define DEF(option, opt_arg, opt_enum, opt_help) \
4257 #define DEFHEADING(text) stringify(text) "\n"
4258 #include "qemu-options.h"
4263 "During emulation, the following keys are useful:\n"
4264 "ctrl-alt-f toggle full screen\n"
4265 "ctrl-alt-n switch to virtual console 'n'\n"
4266 "ctrl-alt toggle mouse and keyboard grab\n"
4268 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4273 DEFAULT_NETWORK_SCRIPT
,
4274 DEFAULT_NETWORK_DOWN_SCRIPT
,
4276 DEFAULT_GDBSTUB_PORT
,
4281 #define HAS_ARG 0x0001
4284 #define DEF(option, opt_arg, opt_enum, opt_help) \
4286 #define DEFHEADING(text)
4287 #include "qemu-options.h"
4293 typedef struct QEMUOption
{
4299 static const QEMUOption qemu_options
[] = {
4300 { "h", 0, QEMU_OPTION_h
},
4301 #define DEF(option, opt_arg, opt_enum, opt_help) \
4302 { option, opt_arg, opt_enum },
4303 #define DEFHEADING(text)
4304 #include "qemu-options.h"
4312 struct soundhw soundhw
[] = {
4313 #ifdef HAS_AUDIO_CHOICE
4314 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4320 { .init_isa
= pcspk_audio_init
}
4327 "Creative Sound Blaster 16",
4330 { .init_isa
= SB16_init
}
4334 #ifdef CONFIG_CS4231A
4340 { .init_isa
= cs4231a_init
}
4348 "Yamaha YMF262 (OPL3)",
4350 "Yamaha YM3812 (OPL2)",
4354 { .init_isa
= Adlib_init
}
4361 "Gravis Ultrasound GF1",
4364 { .init_isa
= GUS_init
}
4371 "Intel 82801AA AC97 Audio",
4374 { .init_pci
= ac97_init
}
4378 #ifdef CONFIG_ES1370
4381 "ENSONIQ AudioPCI ES1370",
4384 { .init_pci
= es1370_init
}
4388 #endif /* HAS_AUDIO_CHOICE */
4390 { NULL
, NULL
, 0, 0, { NULL
} }
4393 static void select_soundhw (const char *optarg
)
4397 if (*optarg
== '?') {
4400 printf ("Valid sound card names (comma separated):\n");
4401 for (c
= soundhw
; c
->name
; ++c
) {
4402 printf ("%-11s %s\n", c
->name
, c
->descr
);
4404 printf ("\n-soundhw all will enable all of the above\n");
4405 exit (*optarg
!= '?');
4413 if (!strcmp (optarg
, "all")) {
4414 for (c
= soundhw
; c
->name
; ++c
) {
4422 e
= strchr (p
, ',');
4423 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4425 for (c
= soundhw
; c
->name
; ++c
) {
4426 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4435 "Unknown sound card name (too big to show)\n");
4438 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4443 p
+= l
+ (e
!= NULL
);
4447 goto show_valid_cards
;
4452 static void select_vgahw (const char *p
)
4456 vga_interface_type
= VGA_NONE
;
4457 if (strstart(p
, "std", &opts
)) {
4458 vga_interface_type
= VGA_STD
;
4459 } else if (strstart(p
, "cirrus", &opts
)) {
4460 vga_interface_type
= VGA_CIRRUS
;
4461 } else if (strstart(p
, "vmware", &opts
)) {
4462 vga_interface_type
= VGA_VMWARE
;
4463 } else if (strstart(p
, "xenfb", &opts
)) {
4464 vga_interface_type
= VGA_XENFB
;
4465 } else if (!strstart(p
, "none", &opts
)) {
4467 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4471 const char *nextopt
;
4473 if (strstart(opts
, ",retrace=", &nextopt
)) {
4475 if (strstart(opts
, "dumb", &nextopt
))
4476 vga_retrace_method
= VGA_RETRACE_DUMB
;
4477 else if (strstart(opts
, "precise", &nextopt
))
4478 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4479 else goto invalid_vga
;
4480 } else goto invalid_vga
;
4486 static int balloon_parse(const char *arg
)
4490 if (strcmp(arg
, "none") == 0) {
4494 if (!strncmp(arg
, "virtio", 6)) {
4495 if (arg
[6] == ',') {
4496 /* have params -> parse them */
4497 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4501 /* create empty opts */
4502 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4504 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4513 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4515 exit(STATUS_CONTROL_C_EXIT
);
4520 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4524 if(strlen(str
) != 36)
4527 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4528 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4529 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4535 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4543 static void termsig_handler(int signal
)
4545 qemu_system_shutdown_request();
4548 static void sigchld_handler(int signal
)
4550 waitpid(-1, NULL
, WNOHANG
);
4553 static void sighandler_setup(void)
4555 struct sigaction act
;
4557 memset(&act
, 0, sizeof(act
));
4558 act
.sa_handler
= termsig_handler
;
4559 sigaction(SIGINT
, &act
, NULL
);
4560 sigaction(SIGHUP
, &act
, NULL
);
4561 sigaction(SIGTERM
, &act
, NULL
);
4563 act
.sa_handler
= sigchld_handler
;
4564 act
.sa_flags
= SA_NOCLDSTOP
;
4565 sigaction(SIGCHLD
, &act
, NULL
);
4571 /* Look for support files in the same directory as the executable. */
4572 static char *find_datadir(const char *argv0
)
4578 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4585 while (p
!= buf
&& *p
!= '\\')
4588 if (access(buf
, R_OK
) == 0) {
4589 return qemu_strdup(buf
);
4595 /* Find a likely location for support files using the location of the binary.
4596 For installed binaries this will be "$bindir/../share/qemu". When
4597 running from the build tree this will be "$bindir/../pc-bios". */
4598 #define SHARE_SUFFIX "/share/qemu"
4599 #define BUILD_SUFFIX "/pc-bios"
4600 static char *find_datadir(const char *argv0
)
4608 #if defined(__linux__)
4611 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4617 #elif defined(__FreeBSD__)
4620 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4627 /* If we don't have any way of figuring out the actual executable
4628 location then try argv[0]. */
4630 p
= realpath(argv0
, buf
);
4638 max_len
= strlen(dir
) +
4639 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4640 res
= qemu_mallocz(max_len
);
4641 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4642 if (access(res
, R_OK
)) {
4643 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4644 if (access(res
, R_OK
)) {
4656 char *qemu_find_file(int type
, const char *name
)
4662 /* If name contains path separators then try it as a straight path. */
4663 if ((strchr(name
, '/') || strchr(name
, '\\'))
4664 && access(name
, R_OK
) == 0) {
4665 return qemu_strdup(name
);
4668 case QEMU_FILE_TYPE_BIOS
:
4671 case QEMU_FILE_TYPE_KEYMAP
:
4672 subdir
= "keymaps/";
4677 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4678 buf
= qemu_mallocz(len
);
4679 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4680 if (access(buf
, R_OK
)) {
4687 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4691 dev
= qdev_device_add(opts
);
4697 struct device_config
{
4699 DEV_USB
, /* -usbdevice */
4702 const char *cmdline
;
4703 QTAILQ_ENTRY(device_config
) next
;
4705 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4707 static void add_device_config(int type
, const char *cmdline
)
4709 struct device_config
*conf
;
4711 conf
= qemu_mallocz(sizeof(*conf
));
4713 conf
->cmdline
= cmdline
;
4714 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4717 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4719 struct device_config
*conf
;
4722 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4723 if (conf
->type
!= type
)
4725 rc
= func(conf
->cmdline
);
4732 int main(int argc
, char **argv
, char **envp
)
4734 const char *gdbstub_dev
= NULL
;
4735 uint32_t boot_devices_bitmap
= 0;
4737 int snapshot
, linux_boot
, net_boot
;
4738 const char *initrd_filename
;
4739 const char *kernel_filename
, *kernel_cmdline
;
4740 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4742 DisplayChangeListener
*dcl
;
4743 int cyls
, heads
, secs
, translation
;
4744 QemuOpts
*hda_opts
= NULL
, *opts
;
4746 const char *r
, *optarg
;
4747 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4748 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4749 int monitor_device_index
;
4750 const char *serial_devices
[MAX_SERIAL_PORTS
];
4751 int serial_device_index
;
4752 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4753 int parallel_device_index
;
4754 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4755 int virtio_console_index
;
4756 const char *loadvm
= NULL
;
4757 QEMUMachine
*machine
;
4758 const char *cpu_model
;
4763 const char *pid_file
= NULL
;
4764 const char *incoming
= NULL
;
4767 struct passwd
*pwd
= NULL
;
4768 const char *chroot_dir
= NULL
;
4769 const char *run_as
= NULL
;
4772 int show_vnc_port
= 0;
4776 qemu_errors_to_file(stderr
);
4777 qemu_cache_utils_init(envp
);
4779 QLIST_INIT (&vm_change_state_head
);
4782 struct sigaction act
;
4783 sigfillset(&act
.sa_mask
);
4785 act
.sa_handler
= SIG_IGN
;
4786 sigaction(SIGPIPE
, &act
, NULL
);
4789 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4790 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4791 QEMU to run on a single CPU */
4796 h
= GetCurrentProcess();
4797 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4798 for(i
= 0; i
< 32; i
++) {
4799 if (mask
& (1 << i
))
4804 SetProcessAffinityMask(h
, mask
);
4810 module_call_init(MODULE_INIT_MACHINE
);
4811 machine
= find_default_machine();
4813 initrd_filename
= NULL
;
4816 kernel_filename
= NULL
;
4817 kernel_cmdline
= "";
4818 cyls
= heads
= secs
= 0;
4819 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4821 serial_devices
[0] = "vc:80Cx24C";
4822 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4823 serial_devices
[i
] = NULL
;
4824 serial_device_index
= 0;
4826 parallel_devices
[0] = "vc:80Cx24C";
4827 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4828 parallel_devices
[i
] = NULL
;
4829 parallel_device_index
= 0;
4831 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4832 virtio_consoles
[i
] = NULL
;
4833 virtio_console_index
= 0;
4835 monitor_devices
[0] = "vc:80Cx24C";
4836 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4837 monitor_devices
[i
] = NULL
;
4839 monitor_device_index
= 0;
4841 for (i
= 0; i
< MAX_NODES
; i
++) {
4843 node_cpumask
[i
] = 0;
4858 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4860 const QEMUOption
*popt
;
4863 /* Treat --foo the same as -foo. */
4866 popt
= qemu_options
;
4869 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4873 if (!strcmp(popt
->name
, r
+ 1))
4877 if (popt
->flags
& HAS_ARG
) {
4878 if (optind
>= argc
) {
4879 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4883 optarg
= argv
[optind
++];
4888 switch(popt
->index
) {
4890 machine
= find_machine(optarg
);
4893 printf("Supported machines are:\n");
4894 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4896 printf("%-10s %s (alias of %s)\n",
4897 m
->alias
, m
->desc
, m
->name
);
4898 printf("%-10s %s%s\n",
4900 m
->is_default
? " (default)" : "");
4902 exit(*optarg
!= '?');
4905 case QEMU_OPTION_cpu
:
4906 /* hw initialization will check this */
4907 if (*optarg
== '?') {
4908 /* XXX: implement xxx_cpu_list for targets that still miss it */
4909 #if defined(cpu_list)
4910 cpu_list(stdout
, &fprintf
);
4917 case QEMU_OPTION_initrd
:
4918 initrd_filename
= optarg
;
4920 case QEMU_OPTION_hda
:
4922 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4924 hda_opts
= drive_add(optarg
, HD_ALIAS
4925 ",cyls=%d,heads=%d,secs=%d%s",
4926 0, cyls
, heads
, secs
,
4927 translation
== BIOS_ATA_TRANSLATION_LBA
?
4929 translation
== BIOS_ATA_TRANSLATION_NONE
?
4930 ",trans=none" : "");
4932 case QEMU_OPTION_hdb
:
4933 case QEMU_OPTION_hdc
:
4934 case QEMU_OPTION_hdd
:
4935 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4937 case QEMU_OPTION_drive
:
4938 drive_add(NULL
, "%s", optarg
);
4940 case QEMU_OPTION_set
:
4941 if (qemu_set_option(optarg
) != 0)
4944 case QEMU_OPTION_mtdblock
:
4945 drive_add(optarg
, MTD_ALIAS
);
4947 case QEMU_OPTION_sd
:
4948 drive_add(optarg
, SD_ALIAS
);
4950 case QEMU_OPTION_pflash
:
4951 drive_add(optarg
, PFLASH_ALIAS
);
4953 case QEMU_OPTION_snapshot
:
4956 case QEMU_OPTION_hdachs
:
4960 cyls
= strtol(p
, (char **)&p
, 0);
4961 if (cyls
< 1 || cyls
> 16383)
4966 heads
= strtol(p
, (char **)&p
, 0);
4967 if (heads
< 1 || heads
> 16)
4972 secs
= strtol(p
, (char **)&p
, 0);
4973 if (secs
< 1 || secs
> 63)
4977 if (!strcmp(p
, "none"))
4978 translation
= BIOS_ATA_TRANSLATION_NONE
;
4979 else if (!strcmp(p
, "lba"))
4980 translation
= BIOS_ATA_TRANSLATION_LBA
;
4981 else if (!strcmp(p
, "auto"))
4982 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4985 } else if (*p
!= '\0') {
4987 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4990 if (hda_opts
!= NULL
) {
4992 snprintf(num
, sizeof(num
), "%d", cyls
);
4993 qemu_opt_set(hda_opts
, "cyls", num
);
4994 snprintf(num
, sizeof(num
), "%d", heads
);
4995 qemu_opt_set(hda_opts
, "heads", num
);
4996 snprintf(num
, sizeof(num
), "%d", secs
);
4997 qemu_opt_set(hda_opts
, "secs", num
);
4998 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4999 qemu_opt_set(hda_opts
, "trans", "lba");
5000 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5001 qemu_opt_set(hda_opts
, "trans", "none");
5005 case QEMU_OPTION_numa
:
5006 if (nb_numa_nodes
>= MAX_NODES
) {
5007 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5012 case QEMU_OPTION_nographic
:
5013 display_type
= DT_NOGRAPHIC
;
5015 #ifdef CONFIG_CURSES
5016 case QEMU_OPTION_curses
:
5017 display_type
= DT_CURSES
;
5020 case QEMU_OPTION_portrait
:
5023 case QEMU_OPTION_kernel
:
5024 kernel_filename
= optarg
;
5026 case QEMU_OPTION_append
:
5027 kernel_cmdline
= optarg
;
5029 case QEMU_OPTION_cdrom
:
5030 drive_add(optarg
, CDROM_ALIAS
);
5032 case QEMU_OPTION_boot
:
5034 static const char * const params
[] = {
5035 "order", "once", "menu", NULL
5037 char buf
[sizeof(boot_devices
)];
5038 char *standard_boot_devices
;
5041 if (!strchr(optarg
, '=')) {
5043 pstrcpy(buf
, sizeof(buf
), optarg
);
5044 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5046 "qemu: unknown boot parameter '%s' in '%s'\n",
5052 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5053 boot_devices_bitmap
= parse_bootdevices(buf
);
5054 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5057 if (get_param_value(buf
, sizeof(buf
),
5059 boot_devices_bitmap
|= parse_bootdevices(buf
);
5060 standard_boot_devices
= qemu_strdup(boot_devices
);
5061 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5062 qemu_register_reset(restore_boot_devices
,
5063 standard_boot_devices
);
5065 if (get_param_value(buf
, sizeof(buf
),
5067 if (!strcmp(buf
, "on")) {
5069 } else if (!strcmp(buf
, "off")) {
5073 "qemu: invalid option value '%s'\n",
5081 case QEMU_OPTION_fda
:
5082 case QEMU_OPTION_fdb
:
5083 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5086 case QEMU_OPTION_no_fd_bootchk
:
5090 case QEMU_OPTION_net
:
5091 if (net_client_parse(optarg
) == -1) {
5096 case QEMU_OPTION_tftp
:
5097 legacy_tftp_prefix
= optarg
;
5099 case QEMU_OPTION_bootp
:
5100 legacy_bootp_filename
= optarg
;
5103 case QEMU_OPTION_smb
:
5104 if (net_slirp_smb(optarg
) < 0)
5108 case QEMU_OPTION_redir
:
5109 if (net_slirp_redir(optarg
) < 0)
5113 case QEMU_OPTION_bt
:
5114 add_device_config(DEV_BT
, optarg
);
5117 case QEMU_OPTION_audio_help
:
5121 case QEMU_OPTION_soundhw
:
5122 select_soundhw (optarg
);
5128 case QEMU_OPTION_version
:
5132 case QEMU_OPTION_m
: {
5136 value
= strtoul(optarg
, &ptr
, 10);
5138 case 0: case 'M': case 'm':
5145 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5149 /* On 32-bit hosts, QEMU is limited by virtual address space */
5150 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5151 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5154 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5155 fprintf(stderr
, "qemu: ram size too large\n");
5164 const CPULogItem
*item
;
5166 mask
= cpu_str_to_log_mask(optarg
);
5168 printf("Log items (comma separated):\n");
5169 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5170 printf("%-10s %s\n", item
->name
, item
->help
);
5178 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5180 case QEMU_OPTION_gdb
:
5181 gdbstub_dev
= optarg
;
5186 case QEMU_OPTION_bios
:
5189 case QEMU_OPTION_singlestep
:
5197 keyboard_layout
= optarg
;
5200 case QEMU_OPTION_localtime
:
5203 case QEMU_OPTION_vga
:
5204 select_vgahw (optarg
);
5206 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5212 w
= strtol(p
, (char **)&p
, 10);
5215 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5221 h
= strtol(p
, (char **)&p
, 10);
5226 depth
= strtol(p
, (char **)&p
, 10);
5227 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5228 depth
!= 24 && depth
!= 32)
5230 } else if (*p
== '\0') {
5231 depth
= graphic_depth
;
5238 graphic_depth
= depth
;
5242 case QEMU_OPTION_echr
:
5245 term_escape_char
= strtol(optarg
, &r
, 0);
5247 printf("Bad argument to echr\n");
5250 case QEMU_OPTION_monitor
:
5251 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5252 fprintf(stderr
, "qemu: too many monitor devices\n");
5255 monitor_devices
[monitor_device_index
] = optarg
;
5256 monitor_device_index
++;
5258 case QEMU_OPTION_chardev
:
5259 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5261 fprintf(stderr
, "parse error: %s\n", optarg
);
5264 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5268 case QEMU_OPTION_serial
:
5269 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5270 fprintf(stderr
, "qemu: too many serial ports\n");
5273 serial_devices
[serial_device_index
] = optarg
;
5274 serial_device_index
++;
5276 case QEMU_OPTION_watchdog
:
5279 "qemu: only one watchdog option may be given\n");
5284 case QEMU_OPTION_watchdog_action
:
5285 if (select_watchdog_action(optarg
) == -1) {
5286 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5290 case QEMU_OPTION_virtiocon
:
5291 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5292 fprintf(stderr
, "qemu: too many virtio consoles\n");
5295 virtio_consoles
[virtio_console_index
] = optarg
;
5296 virtio_console_index
++;
5298 case QEMU_OPTION_parallel
:
5299 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5300 fprintf(stderr
, "qemu: too many parallel ports\n");
5303 parallel_devices
[parallel_device_index
] = optarg
;
5304 parallel_device_index
++;
5306 case QEMU_OPTION_loadvm
:
5309 case QEMU_OPTION_full_screen
:
5313 case QEMU_OPTION_no_frame
:
5316 case QEMU_OPTION_alt_grab
:
5319 case QEMU_OPTION_ctrl_grab
:
5322 case QEMU_OPTION_no_quit
:
5325 case QEMU_OPTION_sdl
:
5326 display_type
= DT_SDL
;
5329 case QEMU_OPTION_pidfile
:
5333 case QEMU_OPTION_win2k_hack
:
5334 win2k_install_hack
= 1;
5336 case QEMU_OPTION_rtc_td_hack
:
5339 case QEMU_OPTION_acpitable
:
5340 if(acpi_table_add(optarg
) < 0) {
5341 fprintf(stderr
, "Wrong acpi table provided\n");
5345 case QEMU_OPTION_smbios
:
5346 if(smbios_entry_add(optarg
) < 0) {
5347 fprintf(stderr
, "Wrong smbios provided\n");
5353 case QEMU_OPTION_enable_kvm
:
5357 case QEMU_OPTION_usb
:
5360 case QEMU_OPTION_usbdevice
:
5362 add_device_config(DEV_USB
, optarg
);
5364 case QEMU_OPTION_device
:
5365 if (!qemu_opts_parse(&qemu_device_opts
, optarg
, "driver")) {
5369 case QEMU_OPTION_smp
:
5372 fprintf(stderr
, "Invalid number of CPUs\n");
5375 if (max_cpus
< smp_cpus
) {
5376 fprintf(stderr
, "maxcpus must be equal to or greater than "
5380 if (max_cpus
> 255) {
5381 fprintf(stderr
, "Unsupported number of maxcpus\n");
5385 case QEMU_OPTION_vnc
:
5386 display_type
= DT_VNC
;
5387 vnc_display
= optarg
;
5390 case QEMU_OPTION_no_acpi
:
5393 case QEMU_OPTION_no_hpet
:
5396 case QEMU_OPTION_balloon
:
5397 if (balloon_parse(optarg
) < 0) {
5398 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5403 case QEMU_OPTION_no_reboot
:
5406 case QEMU_OPTION_no_shutdown
:
5409 case QEMU_OPTION_show_cursor
:
5412 case QEMU_OPTION_uuid
:
5413 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5414 fprintf(stderr
, "Fail to parse UUID string."
5415 " Wrong format.\n");
5420 case QEMU_OPTION_daemonize
:
5424 case QEMU_OPTION_option_rom
:
5425 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5426 fprintf(stderr
, "Too many option ROMs\n");
5429 option_rom
[nb_option_roms
] = optarg
;
5432 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5433 case QEMU_OPTION_semihosting
:
5434 semihosting_enabled
= 1;
5437 case QEMU_OPTION_name
:
5438 qemu_name
= qemu_strdup(optarg
);
5440 char *p
= strchr(qemu_name
, ',');
5443 if (strncmp(p
, "process=", 8)) {
5444 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5452 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5453 case QEMU_OPTION_prom_env
:
5454 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5455 fprintf(stderr
, "Too many prom variables\n");
5458 prom_envs
[nb_prom_envs
] = optarg
;
5463 case QEMU_OPTION_old_param
:
5467 case QEMU_OPTION_clock
:
5468 configure_alarms(optarg
);
5470 case QEMU_OPTION_startdate
:
5471 configure_rtc_date_offset(optarg
, 1);
5473 case QEMU_OPTION_rtc
:
5474 opts
= qemu_opts_parse(&qemu_rtc_opts
, optarg
, NULL
);
5476 fprintf(stderr
, "parse error: %s\n", optarg
);
5479 configure_rtc(opts
);
5481 case QEMU_OPTION_tb_size
:
5482 tb_size
= strtol(optarg
, NULL
, 0);
5486 case QEMU_OPTION_icount
:
5488 if (strcmp(optarg
, "auto") == 0) {
5489 icount_time_shift
= -1;
5491 icount_time_shift
= strtol(optarg
, NULL
, 0);
5494 case QEMU_OPTION_incoming
:
5498 case QEMU_OPTION_chroot
:
5499 chroot_dir
= optarg
;
5501 case QEMU_OPTION_runas
:
5506 case QEMU_OPTION_xen_domid
:
5507 xen_domid
= atoi(optarg
);
5509 case QEMU_OPTION_xen_create
:
5510 xen_mode
= XEN_CREATE
;
5512 case QEMU_OPTION_xen_attach
:
5513 xen_mode
= XEN_ATTACH
;
5520 /* If no data_dir is specified then try to find it relative to the
5523 data_dir
= find_datadir(argv
[0]);
5525 /* If all else fails use the install patch specified when building. */
5527 data_dir
= CONFIG_QEMU_SHAREDIR
;
5531 * Default to max_cpus = smp_cpus, in case the user doesn't
5532 * specify a max_cpus value.
5535 max_cpus
= smp_cpus
;
5537 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5538 if (smp_cpus
> machine
->max_cpus
) {
5539 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5540 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5545 if (display_type
== DT_NOGRAPHIC
) {
5546 if (serial_device_index
== 0)
5547 serial_devices
[0] = "stdio";
5548 if (parallel_device_index
== 0)
5549 parallel_devices
[0] = "null";
5550 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5551 monitor_devices
[0] = "stdio";
5559 if (pipe(fds
) == -1)
5570 len
= read(fds
[0], &status
, 1);
5571 if (len
== -1 && (errno
== EINTR
))
5576 else if (status
== 1) {
5577 fprintf(stderr
, "Could not acquire pidfile: %s\n", strerror(errno
));
5594 signal(SIGTSTP
, SIG_IGN
);
5595 signal(SIGTTOU
, SIG_IGN
);
5596 signal(SIGTTIN
, SIG_IGN
);
5599 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5602 write(fds
[1], &status
, 1);
5604 fprintf(stderr
, "Could not acquire pid file: %s\n", strerror(errno
));
5609 if (kvm_enabled()) {
5612 ret
= kvm_init(smp_cpus
);
5614 fprintf(stderr
, "failed to initialize KVM\n");
5619 if (qemu_init_main_loop()) {
5620 fprintf(stderr
, "qemu_init_main_loop failed\n");
5623 linux_boot
= (kernel_filename
!= NULL
);
5625 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5626 fprintf(stderr
, "-append only allowed with -kernel option\n");
5630 if (!linux_boot
&& initrd_filename
!= NULL
) {
5631 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5636 /* Win32 doesn't support line-buffering and requires size >= 2 */
5637 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5640 if (init_timer_alarm() < 0) {
5641 fprintf(stderr
, "could not initialize alarm timer\n");
5644 if (use_icount
&& icount_time_shift
< 0) {
5646 /* 125MIPS seems a reasonable initial guess at the guest speed.
5647 It will be corrected fairly quickly anyway. */
5648 icount_time_shift
= 3;
5649 init_icount_adjust();
5656 if (net_init_clients() < 0) {
5660 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5661 net_set_boot_mask(net_boot
);
5663 /* init the bluetooth world */
5664 if (foreach_device_config(DEV_BT
, bt_parse
))
5667 /* init the memory */
5669 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5671 /* init the dynamic translator */
5672 cpu_exec_init_all(tb_size
* 1024 * 1024);
5676 /* we always create the cdrom drive, even if no disk is there */
5677 drive_add(NULL
, CDROM_ALIAS
);
5679 /* we always create at least one floppy */
5680 drive_add(NULL
, FD_ALIAS
, 0);
5682 /* we always create one sd slot, even if no card is in it */
5683 drive_add(NULL
, SD_ALIAS
);
5685 /* open the virtual block devices */
5687 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5688 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5691 vmstate_register(0, &vmstate_timers
,&timers_state
);
5692 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5694 /* Maintain compatibility with multiple stdio monitors */
5695 if (!strcmp(monitor_devices
[0],"stdio")) {
5696 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5697 const char *devname
= serial_devices
[i
];
5698 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5699 monitor_devices
[0] = NULL
;
5701 } else if (devname
&& !strcmp(devname
,"stdio")) {
5702 monitor_devices
[0] = NULL
;
5703 serial_devices
[i
] = "mon:stdio";
5709 if (nb_numa_nodes
> 0) {
5712 if (nb_numa_nodes
> smp_cpus
) {
5713 nb_numa_nodes
= smp_cpus
;
5716 /* If no memory size if given for any node, assume the default case
5717 * and distribute the available memory equally across all nodes
5719 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5720 if (node_mem
[i
] != 0)
5723 if (i
== nb_numa_nodes
) {
5724 uint64_t usedmem
= 0;
5726 /* On Linux, the each node's border has to be 8MB aligned,
5727 * the final node gets the rest.
5729 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5730 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5731 usedmem
+= node_mem
[i
];
5733 node_mem
[i
] = ram_size
- usedmem
;
5736 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5737 if (node_cpumask
[i
] != 0)
5740 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5741 * must cope with this anyway, because there are BIOSes out there in
5742 * real machines which also use this scheme.
5744 if (i
== nb_numa_nodes
) {
5745 for (i
= 0; i
< smp_cpus
; i
++) {
5746 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5751 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5752 const char *devname
= monitor_devices
[i
];
5753 if (devname
&& strcmp(devname
, "none")) {
5756 snprintf(label
, sizeof(label
), "monitor");
5758 snprintf(label
, sizeof(label
), "monitor%d", i
);
5760 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5761 if (!monitor_hds
[i
]) {
5762 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5769 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5770 const char *devname
= serial_devices
[i
];
5771 if (devname
&& strcmp(devname
, "none")) {
5773 snprintf(label
, sizeof(label
), "serial%d", i
);
5774 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5775 if (!serial_hds
[i
]) {
5776 fprintf(stderr
, "qemu: could not open serial device '%s': %s\n",
5777 devname
, strerror(errno
));
5783 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5784 const char *devname
= parallel_devices
[i
];
5785 if (devname
&& strcmp(devname
, "none")) {
5787 snprintf(label
, sizeof(label
), "parallel%d", i
);
5788 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5789 if (!parallel_hds
[i
]) {
5790 fprintf(stderr
, "qemu: could not open parallel device '%s': %s\n",
5791 devname
, strerror(errno
));
5797 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5798 const char *devname
= virtio_consoles
[i
];
5799 if (devname
&& strcmp(devname
, "none")) {
5801 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5802 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5803 if (!virtcon_hds
[i
]) {
5804 fprintf(stderr
, "qemu: could not open virtio console '%s': %s\n",
5805 devname
, strerror(errno
));
5811 module_call_init(MODULE_INIT_DEVICE
);
5814 i
= select_watchdog(watchdog
);
5816 exit (i
== 1 ? 1 : 0);
5819 if (machine
->compat_props
) {
5820 qdev_prop_register_compat(machine
->compat_props
);
5822 machine
->init(ram_size
, boot_devices
,
5823 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5827 /* must be after terminal init, SDL library changes signal handlers */
5831 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5832 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5833 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5839 current_machine
= machine
;
5841 /* init USB devices */
5843 if (foreach_device_config(DEV_USB
, usb_parse
) < 0)
5847 /* init generic devices */
5848 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5852 dumb_display_init();
5853 /* just use the first displaystate for the moment */
5856 if (display_type
== DT_DEFAULT
) {
5857 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5858 display_type
= DT_SDL
;
5860 display_type
= DT_VNC
;
5861 vnc_display
= "localhost:0,to=99";
5867 switch (display_type
) {
5870 #if defined(CONFIG_CURSES)
5872 curses_display_init(ds
, full_screen
);
5875 #if defined(CONFIG_SDL)
5877 sdl_display_init(ds
, full_screen
, no_frame
);
5879 #elif defined(CONFIG_COCOA)
5881 cocoa_display_init(ds
, full_screen
);
5885 vnc_display_init(ds
);
5886 if (vnc_display_open(ds
, vnc_display
) < 0)
5889 if (show_vnc_port
) {
5890 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5898 dcl
= ds
->listeners
;
5899 while (dcl
!= NULL
) {
5900 if (dcl
->dpy_refresh
!= NULL
) {
5901 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5902 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5907 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5908 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5909 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5912 text_consoles_set_display(display_state
);
5913 qemu_chr_initial_reset();
5915 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5916 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5917 monitor_init(monitor_hds
[i
],
5918 MONITOR_USE_READLINE
|
5919 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5923 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5924 const char *devname
= serial_devices
[i
];
5925 if (devname
&& strcmp(devname
, "none")) {
5926 if (strstart(devname
, "vc", 0))
5927 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5931 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5932 const char *devname
= parallel_devices
[i
];
5933 if (devname
&& strcmp(devname
, "none")) {
5934 if (strstart(devname
, "vc", 0))
5935 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5939 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5940 const char *devname
= virtio_consoles
[i
];
5941 if (virtcon_hds
[i
] && devname
) {
5942 if (strstart(devname
, "vc", 0))
5943 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5947 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5948 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5953 qdev_machine_creation_done();
5958 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5964 qemu_start_incoming_migration(incoming
);
5965 } else if (autostart
) {
5975 len
= write(fds
[1], &status
, 1);
5976 if (len
== -1 && (errno
== EINTR
))
5983 TFR(fd
= open("/dev/null", O_RDWR
));
5989 pwd
= getpwnam(run_as
);
5991 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
5997 if (chroot(chroot_dir
) < 0) {
5998 fprintf(stderr
, "chroot failed\n");
6005 if (setgid(pwd
->pw_gid
) < 0) {
6006 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6009 if (setuid(pwd
->pw_uid
) < 0) {
6010 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6013 if (setuid(0) != -1) {
6014 fprintf(stderr
, "Dropping privileges failed\n");