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>
99 #if defined(__OpenBSD__)
103 #if defined(CONFIG_VDE)
104 #include <libvdeplug.h>
109 #include <mmsystem.h>
113 #if defined(__APPLE__) || defined(main)
115 int qemu_main(int argc
, char **argv
, char **envp
);
116 int main(int argc
, char **argv
)
118 return qemu_main(argc
, argv
, NULL
);
121 #define main qemu_main
123 #endif /* CONFIG_SDL */
127 #define main qemu_main
128 #endif /* CONFIG_COCOA */
131 #include "hw/boards.h"
133 #include "hw/pcmcia.h"
135 #include "hw/audiodev.h"
139 #include "hw/watchdog.h"
140 #include "hw/smbios.h"
149 #include "qemu-timer.h"
150 #include "qemu-char.h"
151 #include "cache-utils.h"
154 #include "audio/audio.h"
155 #include "migration.h"
158 #include "qemu-option.h"
159 #include "qemu-config.h"
160 #include "qemu-kvm.h"
161 #include "hw/device-assignment.h"
165 #include "exec-all.h"
167 #include "qemu_socket.h"
169 #include "slirp/libslirp.h"
171 #include "qemu-queue.h"
174 //#define DEBUG_SLIRP
176 #define DEFAULT_RAM_SIZE 128
178 /* Maximum number of monitor devices */
179 #define MAX_MONITOR_DEVICES 10
181 static const char *data_dir
;
182 const char *bios_name
= NULL
;
183 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
184 to store the VM snapshots */
185 struct drivelist drives
= QTAILQ_HEAD_INITIALIZER(drives
);
186 struct driveoptlist driveopts
= QTAILQ_HEAD_INITIALIZER(driveopts
);
187 DriveInfo
*extboot_drive
= NULL
;
188 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
189 static DisplayState
*display_state
;
190 DisplayType display_type
= DT_DEFAULT
;
191 const char* keyboard_layout
= NULL
;
194 NICInfo nd_table
[MAX_NICS
];
197 static int rtc_utc
= 1;
198 static int rtc_date_offset
= -1; /* -1 means no change */
199 int vga_interface_type
= VGA_CIRRUS
;
201 int graphic_width
= 1024;
202 int graphic_height
= 768;
203 int graphic_depth
= 8;
205 int graphic_width
= 800;
206 int graphic_height
= 600;
207 int graphic_depth
= 15;
209 static int full_screen
= 0;
211 static int no_frame
= 0;
214 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
215 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
216 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
218 int win2k_install_hack
= 0;
223 const char *assigned_devices
[MAX_DEV_ASSIGN_CMDLINE
];
224 int assigned_devices_index
;
229 const char *vnc_display
;
230 int acpi_enabled
= 1;
238 int graphic_rotate
= 0;
239 uint8_t irq0override
= 1;
243 const char *watchdog
;
244 const char *option_rom
[MAX_OPTION_ROMS
];
246 int semihosting_enabled
= 0;
247 int time_drift_fix
= 0;
248 unsigned int kvm_shadow_memory
= 0;
249 const char *mem_path
= NULL
;
251 int mem_prealloc
= 1; /* force preallocation of physical target memory */
256 const char *qemu_name
;
258 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
259 unsigned int nb_prom_envs
= 0;
260 const char *prom_envs
[MAX_PROM_ENVS
];
262 const char *nvram
= NULL
;
266 uint64_t node_mem
[MAX_NODES
];
267 uint64_t node_cpumask
[MAX_NODES
];
269 static CPUState
*cur_cpu
;
270 static CPUState
*next_cpu
;
271 static int timer_alarm_pending
= 1;
272 /* Conversion factor from emulated instructions to virtual clock ticks. */
273 static int icount_time_shift
;
274 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
275 #define MAX_ICOUNT_SHIFT 10
276 /* Compensate for varying guest execution speed. */
277 static int64_t qemu_icount_bias
;
278 static QEMUTimer
*icount_rt_timer
;
279 static QEMUTimer
*icount_vm_timer
;
280 static QEMUTimer
*nographic_timer
;
282 uint8_t qemu_uuid
[16];
284 static QEMUBootSetHandler
*boot_set_handler
;
285 static void *boot_set_opaque
;
287 /***********************************************************/
288 /* x86 ISA bus support */
290 target_phys_addr_t isa_mem_base
= 0;
293 /***********************************************************/
294 void hw_error(const char *fmt
, ...)
300 fprintf(stderr
, "qemu: hardware error: ");
301 vfprintf(stderr
, fmt
, ap
);
302 fprintf(stderr
, "\n");
303 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
304 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
306 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
308 cpu_dump_state(env
, stderr
, fprintf
, 0);
315 static void set_proc_name(const char *s
)
317 #if defined(__linux__) && defined(PR_SET_NAME)
321 name
[sizeof(name
) - 1] = 0;
322 strncpy(name
, s
, sizeof(name
));
323 /* Could rewrite argv[0] too, but that's a bit more complicated.
324 This simple way is enough for `top'. */
325 prctl(PR_SET_NAME
, name
);
332 static QEMUBalloonEvent
*qemu_balloon_event
;
333 void *qemu_balloon_event_opaque
;
335 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
337 qemu_balloon_event
= func
;
338 qemu_balloon_event_opaque
= opaque
;
341 void qemu_balloon(ram_addr_t target
)
343 if (qemu_balloon_event
)
344 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
347 ram_addr_t
qemu_balloon_status(void)
349 if (qemu_balloon_event
)
350 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
354 /***********************************************************/
357 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
358 static void *qemu_put_kbd_event_opaque
;
359 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
360 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
362 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
364 qemu_put_kbd_event_opaque
= opaque
;
365 qemu_put_kbd_event
= func
;
368 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
369 void *opaque
, int absolute
,
372 QEMUPutMouseEntry
*s
, *cursor
;
374 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
376 s
->qemu_put_mouse_event
= func
;
377 s
->qemu_put_mouse_event_opaque
= opaque
;
378 s
->qemu_put_mouse_event_absolute
= absolute
;
379 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
382 if (!qemu_put_mouse_event_head
) {
383 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
387 cursor
= qemu_put_mouse_event_head
;
388 while (cursor
->next
!= NULL
)
389 cursor
= cursor
->next
;
392 qemu_put_mouse_event_current
= s
;
397 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
399 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
401 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
404 cursor
= qemu_put_mouse_event_head
;
405 while (cursor
!= NULL
&& cursor
!= entry
) {
407 cursor
= cursor
->next
;
410 if (cursor
== NULL
) // does not exist or list empty
412 else if (prev
== NULL
) { // entry is head
413 qemu_put_mouse_event_head
= cursor
->next
;
414 if (qemu_put_mouse_event_current
== entry
)
415 qemu_put_mouse_event_current
= cursor
->next
;
416 qemu_free(entry
->qemu_put_mouse_event_name
);
421 prev
->next
= entry
->next
;
423 if (qemu_put_mouse_event_current
== entry
)
424 qemu_put_mouse_event_current
= prev
;
426 qemu_free(entry
->qemu_put_mouse_event_name
);
430 void kbd_put_keycode(int keycode
)
432 if (qemu_put_kbd_event
) {
433 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
437 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
439 QEMUPutMouseEvent
*mouse_event
;
440 void *mouse_event_opaque
;
443 if (!qemu_put_mouse_event_current
) {
448 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
450 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
453 if (graphic_rotate
) {
454 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
457 width
= graphic_width
- 1;
458 mouse_event(mouse_event_opaque
,
459 width
- dy
, dx
, dz
, buttons_state
);
461 mouse_event(mouse_event_opaque
,
462 dx
, dy
, dz
, buttons_state
);
466 int kbd_mouse_is_absolute(void)
468 if (!qemu_put_mouse_event_current
)
471 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
474 void do_info_mice(Monitor
*mon
)
476 QEMUPutMouseEntry
*cursor
;
479 if (!qemu_put_mouse_event_head
) {
480 monitor_printf(mon
, "No mouse devices connected\n");
484 monitor_printf(mon
, "Mouse devices available:\n");
485 cursor
= qemu_put_mouse_event_head
;
486 while (cursor
!= NULL
) {
487 monitor_printf(mon
, "%c Mouse #%d: %s\n",
488 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
489 index
, cursor
->qemu_put_mouse_event_name
);
491 cursor
= cursor
->next
;
495 void do_mouse_set(Monitor
*mon
, const QDict
*qdict
)
497 QEMUPutMouseEntry
*cursor
;
499 int index
= qdict_get_int(qdict
, "index");
501 if (!qemu_put_mouse_event_head
) {
502 monitor_printf(mon
, "No mouse devices connected\n");
506 cursor
= qemu_put_mouse_event_head
;
507 while (cursor
!= NULL
&& index
!= i
) {
509 cursor
= cursor
->next
;
513 qemu_put_mouse_event_current
= cursor
;
515 monitor_printf(mon
, "Mouse at given index not found\n");
518 /* compute with 96 bit intermediate result: (a*b)/c */
519 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
524 #ifdef HOST_WORDS_BIGENDIAN
534 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
535 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
538 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
542 /***********************************************************/
543 /* real time host monotonic timer */
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. */
600 gettimeofday(&tv
, NULL
);
601 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
606 /* Return the virtual CPU time, based on the instruction counter. */
607 static int64_t cpu_get_icount(void)
610 CPUState
*env
= cpu_single_env
;;
611 icount
= qemu_icount
;
614 fprintf(stderr
, "Bad clock read\n");
615 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
617 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
620 /***********************************************************/
621 /* guest cycle counter */
623 typedef struct TimersState
{
624 int64_t cpu_ticks_prev
;
625 int64_t cpu_ticks_offset
;
626 int64_t cpu_clock_offset
;
627 int32_t cpu_ticks_enabled
;
631 TimersState timers_state
;
633 /* return the host CPU cycle counter and handle stop/restart */
634 int64_t cpu_get_ticks(void)
637 return cpu_get_icount();
639 if (!timers_state
.cpu_ticks_enabled
) {
640 return timers_state
.cpu_ticks_offset
;
643 ticks
= cpu_get_real_ticks();
644 if (timers_state
.cpu_ticks_prev
> ticks
) {
645 /* Note: non increasing ticks may happen if the host uses
647 timers_state
.cpu_ticks_offset
+= timers_state
.cpu_ticks_prev
- ticks
;
649 timers_state
.cpu_ticks_prev
= ticks
;
650 return ticks
+ timers_state
.cpu_ticks_offset
;
654 /* return the host CPU monotonic timer and handle stop/restart */
655 static int64_t cpu_get_clock(void)
658 if (!timers_state
.cpu_ticks_enabled
) {
659 return timers_state
.cpu_clock_offset
;
662 return ti
+ timers_state
.cpu_clock_offset
;
666 /* enable cpu_get_ticks() */
667 void cpu_enable_ticks(void)
669 if (!timers_state
.cpu_ticks_enabled
) {
670 timers_state
.cpu_ticks_offset
-= cpu_get_real_ticks();
671 timers_state
.cpu_clock_offset
-= get_clock();
672 timers_state
.cpu_ticks_enabled
= 1;
676 /* disable cpu_get_ticks() : the clock is stopped. You must not call
677 cpu_get_ticks() after that. */
678 void cpu_disable_ticks(void)
680 if (timers_state
.cpu_ticks_enabled
) {
681 timers_state
.cpu_ticks_offset
= cpu_get_ticks();
682 timers_state
.cpu_clock_offset
= cpu_get_clock();
683 timers_state
.cpu_ticks_enabled
= 0;
687 /***********************************************************/
690 #define QEMU_TIMER_REALTIME 0
691 #define QEMU_TIMER_VIRTUAL 1
695 /* XXX: add frequency */
703 struct QEMUTimer
*next
;
706 struct qemu_alarm_timer
{
710 int (*start
)(struct qemu_alarm_timer
*t
);
711 void (*stop
)(struct qemu_alarm_timer
*t
);
712 void (*rearm
)(struct qemu_alarm_timer
*t
);
716 #define ALARM_FLAG_DYNTICKS 0x1
717 #define ALARM_FLAG_EXPIRED 0x2
719 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
721 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
724 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
726 if (!alarm_has_dynticks(t
))
732 /* TODO: MIN_TIMER_REARM_US should be optimized */
733 #define MIN_TIMER_REARM_US 250
735 static struct qemu_alarm_timer
*alarm_timer
;
739 struct qemu_alarm_win32
{
742 } alarm_win32_data
= {0, -1};
744 static int win32_start_timer(struct qemu_alarm_timer
*t
);
745 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
746 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
750 static int unix_start_timer(struct qemu_alarm_timer
*t
);
751 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
755 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
756 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
757 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
759 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
760 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
762 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
763 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
765 #endif /* __linux__ */
769 /* Correlation between real and virtual time is always going to be
770 fairly approximate, so ignore small variation.
771 When the guest is idle real and virtual time will be aligned in
773 #define ICOUNT_WOBBLE (get_ticks_per_sec() / 10)
775 static void icount_adjust(void)
780 static int64_t last_delta
;
781 /* If the VM is not running, then do nothing. */
785 cur_time
= cpu_get_clock();
786 cur_icount
= qemu_get_clock(vm_clock
);
787 delta
= cur_icount
- cur_time
;
788 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
790 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
791 && icount_time_shift
> 0) {
792 /* The guest is getting too far ahead. Slow time down. */
796 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
797 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
798 /* The guest is getting too far behind. Speed time up. */
802 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
805 static void icount_adjust_rt(void * opaque
)
807 qemu_mod_timer(icount_rt_timer
,
808 qemu_get_clock(rt_clock
) + 1000);
812 static void icount_adjust_vm(void * opaque
)
814 qemu_mod_timer(icount_vm_timer
,
815 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
819 static void init_icount_adjust(void)
821 /* Have both realtime and virtual time triggers for speed adjustment.
822 The realtime trigger catches emulated time passing too slowly,
823 the virtual time trigger catches emulated time passing too fast.
824 Realtime triggers occur even when idle, so use them less frequently
826 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
827 qemu_mod_timer(icount_rt_timer
,
828 qemu_get_clock(rt_clock
) + 1000);
829 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
830 qemu_mod_timer(icount_vm_timer
,
831 qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 10);
834 static struct qemu_alarm_timer alarm_timers
[] = {
837 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
838 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
839 /* HPET - if available - is preferred */
840 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
841 /* ...otherwise try RTC */
842 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
844 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
846 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
847 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
848 {"win32", 0, win32_start_timer
,
849 win32_stop_timer
, NULL
, &alarm_win32_data
},
854 static void show_available_alarms(void)
858 printf("Available alarm timers, in order of precedence:\n");
859 for (i
= 0; alarm_timers
[i
].name
; i
++)
860 printf("%s\n", alarm_timers
[i
].name
);
863 static void configure_alarms(char const *opt
)
867 int count
= ARRAY_SIZE(alarm_timers
) - 1;
870 struct qemu_alarm_timer tmp
;
872 if (!strcmp(opt
, "?")) {
873 show_available_alarms();
877 arg
= qemu_strdup(opt
);
879 /* Reorder the array */
880 name
= strtok(arg
, ",");
882 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
883 if (!strcmp(alarm_timers
[i
].name
, name
))
888 fprintf(stderr
, "Unknown clock %s\n", name
);
897 tmp
= alarm_timers
[i
];
898 alarm_timers
[i
] = alarm_timers
[cur
];
899 alarm_timers
[cur
] = tmp
;
903 name
= strtok(NULL
, ",");
909 /* Disable remaining timers */
910 for (i
= cur
; i
< count
; i
++)
911 alarm_timers
[i
].name
= NULL
;
913 show_available_alarms();
921 static QEMUTimer
*active_timers
[2];
923 static QEMUClock
*qemu_new_clock(int type
)
926 clock
= qemu_mallocz(sizeof(QEMUClock
));
931 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
935 ts
= qemu_mallocz(sizeof(QEMUTimer
));
942 void qemu_free_timer(QEMUTimer
*ts
)
947 /* stop a timer, but do not dealloc it */
948 void qemu_del_timer(QEMUTimer
*ts
)
952 /* NOTE: this code must be signal safe because
953 qemu_timer_expired() can be called from a signal. */
954 pt
= &active_timers
[ts
->clock
->type
];
967 /* modify the current timer so that it will be fired when current_time
968 >= expire_time. The corresponding callback will be called. */
969 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
975 /* add the timer in the sorted list */
976 /* NOTE: this code must be signal safe because
977 qemu_timer_expired() can be called from a signal. */
978 pt
= &active_timers
[ts
->clock
->type
];
983 if (t
->expire_time
> expire_time
)
987 ts
->expire_time
= expire_time
;
991 /* Rearm if necessary */
992 if (pt
== &active_timers
[ts
->clock
->type
]) {
993 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
994 qemu_rearm_alarm_timer(alarm_timer
);
996 /* Interrupt execution to force deadline recalculation. */
1002 int qemu_timer_pending(QEMUTimer
*ts
)
1005 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
1012 int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1016 return (timer_head
->expire_time
<= current_time
);
1019 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1025 if (!ts
|| ts
->expire_time
> current_time
)
1027 /* remove timer from the list before calling the callback */
1028 *ptimer_head
= ts
->next
;
1031 /* run the callback (the timer list can be modified) */
1036 int64_t qemu_get_clock(QEMUClock
*clock
)
1038 switch(clock
->type
) {
1039 case QEMU_TIMER_REALTIME
:
1040 return get_clock() / 1000000;
1042 case QEMU_TIMER_VIRTUAL
:
1044 return cpu_get_icount();
1046 return cpu_get_clock();
1051 static void init_timers(void)
1054 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1055 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1059 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1061 uint64_t expire_time
;
1063 if (qemu_timer_pending(ts
)) {
1064 expire_time
= ts
->expire_time
;
1068 qemu_put_be64(f
, expire_time
);
1071 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1073 uint64_t expire_time
;
1075 expire_time
= qemu_get_be64(f
);
1076 if (expire_time
!= -1) {
1077 qemu_mod_timer(ts
, expire_time
);
1083 static const VMStateDescription vmstate_timers
= {
1086 .minimum_version_id
= 1,
1087 .minimum_version_id_old
= 1,
1088 .fields
= (VMStateField
[]) {
1089 VMSTATE_INT64(cpu_ticks_offset
, TimersState
),
1090 VMSTATE_INT64(dummy
, TimersState
),
1091 VMSTATE_INT64_V(cpu_clock_offset
, TimersState
, 2),
1092 VMSTATE_END_OF_LIST()
1096 static void qemu_event_increment(void);
1099 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1100 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1103 static void host_alarm_handler(int host_signum
)
1107 #define DISP_FREQ 1000
1109 static int64_t delta_min
= INT64_MAX
;
1110 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1112 ti
= qemu_get_clock(vm_clock
);
1113 if (last_clock
!= 0) {
1114 delta
= ti
- last_clock
;
1115 if (delta
< delta_min
)
1117 if (delta
> delta_max
)
1120 if (++count
== DISP_FREQ
) {
1121 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1122 muldiv64(delta_min
, 1000000, get_ticks_per_sec()),
1123 muldiv64(delta_max
, 1000000, get_ticks_per_sec()),
1124 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, get_ticks_per_sec()),
1125 (double)get_ticks_per_sec() / ((double)delta_cum
/ DISP_FREQ
));
1127 delta_min
= INT64_MAX
;
1135 if (alarm_has_dynticks(alarm_timer
) ||
1137 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1138 qemu_get_clock(vm_clock
))) ||
1139 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1140 qemu_get_clock(rt_clock
))) {
1141 qemu_event_increment();
1142 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1144 #ifndef CONFIG_IOTHREAD
1146 /* stop the currently executing cpu because a timer occured */
1150 timer_alarm_pending
= 1;
1151 qemu_notify_event();
1155 static int64_t qemu_next_deadline(void)
1159 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1160 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1161 qemu_get_clock(vm_clock
);
1163 /* To avoid problems with overflow limit this to 2^32. */
1173 #if defined(__linux__) || defined(_WIN32)
1174 static uint64_t qemu_next_deadline_dyntick(void)
1182 delta
= (qemu_next_deadline() + 999) / 1000;
1184 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1185 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1186 qemu_get_clock(rt_clock
))*1000;
1187 if (rtdelta
< delta
)
1191 if (delta
< MIN_TIMER_REARM_US
)
1192 delta
= MIN_TIMER_REARM_US
;
1200 /* Sets a specific flag */
1201 static int fcntl_setfl(int fd
, int flag
)
1205 flags
= fcntl(fd
, F_GETFL
);
1209 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1215 #if defined(__linux__)
1217 #define RTC_FREQ 1024
1219 static void enable_sigio_timer(int fd
)
1221 struct sigaction act
;
1224 sigfillset(&act
.sa_mask
);
1226 act
.sa_handler
= host_alarm_handler
;
1228 sigaction(SIGIO
, &act
, NULL
);
1229 fcntl_setfl(fd
, O_ASYNC
);
1230 fcntl(fd
, F_SETOWN
, getpid());
1233 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1235 struct hpet_info info
;
1238 fd
= open("/dev/hpet", O_RDONLY
);
1243 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1245 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1246 "error, but for better emulation accuracy type:\n"
1247 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1251 /* Check capabilities */
1252 r
= ioctl(fd
, HPET_INFO
, &info
);
1256 /* Enable periodic mode */
1257 r
= ioctl(fd
, HPET_EPI
, 0);
1258 if (info
.hi_flags
&& (r
< 0))
1261 /* Enable interrupt */
1262 r
= ioctl(fd
, HPET_IE_ON
, 0);
1266 enable_sigio_timer(fd
);
1267 t
->priv
= (void *)(long)fd
;
1275 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1277 int fd
= (long)t
->priv
;
1282 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1285 unsigned long current_rtc_freq
= 0;
1287 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1290 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1291 if (current_rtc_freq
!= RTC_FREQ
&&
1292 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1293 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1294 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1295 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1298 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1304 enable_sigio_timer(rtc_fd
);
1306 t
->priv
= (void *)(long)rtc_fd
;
1311 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1313 int rtc_fd
= (long)t
->priv
;
1318 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1322 struct sigaction act
;
1324 sigfillset(&act
.sa_mask
);
1326 act
.sa_handler
= host_alarm_handler
;
1328 sigaction(SIGALRM
, &act
, NULL
);
1331 * Initialize ev struct to 0 to avoid valgrind complaining
1332 * about uninitialized data in timer_create call
1334 memset(&ev
, 0, sizeof(ev
));
1335 ev
.sigev_value
.sival_int
= 0;
1336 ev
.sigev_notify
= SIGEV_SIGNAL
;
1337 ev
.sigev_signo
= SIGALRM
;
1339 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1340 perror("timer_create");
1342 /* disable dynticks */
1343 fprintf(stderr
, "Dynamic Ticks disabled\n");
1348 t
->priv
= (void *)(long)host_timer
;
1353 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1355 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1357 timer_delete(host_timer
);
1360 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1362 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1363 struct itimerspec timeout
;
1364 int64_t nearest_delta_us
= INT64_MAX
;
1367 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1368 !active_timers
[QEMU_TIMER_VIRTUAL
])
1371 nearest_delta_us
= qemu_next_deadline_dyntick();
1373 /* check whether a timer is already running */
1374 if (timer_gettime(host_timer
, &timeout
)) {
1376 fprintf(stderr
, "Internal timer error: aborting\n");
1379 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1380 if (current_us
&& current_us
<= nearest_delta_us
)
1383 timeout
.it_interval
.tv_sec
= 0;
1384 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1385 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1386 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1387 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1389 fprintf(stderr
, "Internal timer error: aborting\n");
1394 #endif /* defined(__linux__) */
1396 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1398 struct sigaction act
;
1399 struct itimerval itv
;
1403 sigfillset(&act
.sa_mask
);
1405 act
.sa_handler
= host_alarm_handler
;
1407 sigaction(SIGALRM
, &act
, NULL
);
1409 itv
.it_interval
.tv_sec
= 0;
1410 /* for i386 kernel 2.6 to get 1 ms */
1411 itv
.it_interval
.tv_usec
= 999;
1412 itv
.it_value
.tv_sec
= 0;
1413 itv
.it_value
.tv_usec
= 10 * 1000;
1415 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1422 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1424 struct itimerval itv
;
1426 memset(&itv
, 0, sizeof(itv
));
1427 setitimer(ITIMER_REAL
, &itv
, NULL
);
1430 #endif /* !defined(_WIN32) */
1435 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1438 struct qemu_alarm_win32
*data
= t
->priv
;
1441 memset(&tc
, 0, sizeof(tc
));
1442 timeGetDevCaps(&tc
, sizeof(tc
));
1444 if (data
->period
< tc
.wPeriodMin
)
1445 data
->period
= tc
.wPeriodMin
;
1447 timeBeginPeriod(data
->period
);
1449 flags
= TIME_CALLBACK_FUNCTION
;
1450 if (alarm_has_dynticks(t
))
1451 flags
|= TIME_ONESHOT
;
1453 flags
|= TIME_PERIODIC
;
1455 data
->timerId
= timeSetEvent(1, // interval (ms)
1456 data
->period
, // resolution
1457 host_alarm_handler
, // function
1458 (DWORD
)t
, // parameter
1461 if (!data
->timerId
) {
1462 fprintf(stderr
, "Failed to initialize win32 alarm timer: %ld\n",
1464 timeEndPeriod(data
->period
);
1471 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1473 struct qemu_alarm_win32
*data
= t
->priv
;
1475 timeKillEvent(data
->timerId
);
1476 timeEndPeriod(data
->period
);
1479 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1481 struct qemu_alarm_win32
*data
= t
->priv
;
1482 uint64_t nearest_delta_us
;
1484 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1485 !active_timers
[QEMU_TIMER_VIRTUAL
])
1488 nearest_delta_us
= qemu_next_deadline_dyntick();
1489 nearest_delta_us
/= 1000;
1491 timeKillEvent(data
->timerId
);
1493 data
->timerId
= timeSetEvent(1,
1497 TIME_ONESHOT
| TIME_PERIODIC
);
1499 if (!data
->timerId
) {
1500 fprintf(stderr
, "Failed to re-arm win32 alarm timer %ld\n",
1503 timeEndPeriod(data
->period
);
1510 static int init_timer_alarm(void)
1512 struct qemu_alarm_timer
*t
= NULL
;
1515 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1516 t
= &alarm_timers
[i
];
1536 static void quit_timers(void)
1538 alarm_timer
->stop(alarm_timer
);
1542 /***********************************************************/
1543 /* host time/date access */
1544 void qemu_get_timedate(struct tm
*tm
, int offset
)
1551 if (rtc_date_offset
== -1) {
1555 ret
= localtime(&ti
);
1557 ti
-= rtc_date_offset
;
1561 memcpy(tm
, ret
, sizeof(struct tm
));
1564 int qemu_timedate_diff(struct tm
*tm
)
1568 if (rtc_date_offset
== -1)
1570 seconds
= mktimegm(tm
);
1572 seconds
= mktime(tm
);
1574 seconds
= mktimegm(tm
) + rtc_date_offset
;
1576 return seconds
- time(NULL
);
1580 static void socket_cleanup(void)
1585 static int socket_init(void)
1590 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1592 err
= WSAGetLastError();
1593 fprintf(stderr
, "WSAStartup: %d\n", err
);
1596 atexit(socket_cleanup
);
1601 /***********************************************************/
1602 /* Bluetooth support */
1605 static struct HCIInfo
*hci_table
[MAX_NICS
];
1607 static struct bt_vlan_s
{
1608 struct bt_scatternet_s net
;
1610 struct bt_vlan_s
*next
;
1613 /* find or alloc a new bluetooth "VLAN" */
1614 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1616 struct bt_vlan_s
**pvlan
, *vlan
;
1617 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1621 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1623 pvlan
= &first_bt_vlan
;
1624 while (*pvlan
!= NULL
)
1625 pvlan
= &(*pvlan
)->next
;
1630 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1634 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1639 static struct HCIInfo null_hci
= {
1640 .cmd_send
= null_hci_send
,
1641 .sco_send
= null_hci_send
,
1642 .acl_send
= null_hci_send
,
1643 .bdaddr_set
= null_hci_addr_set
,
1646 struct HCIInfo
*qemu_next_hci(void)
1648 if (cur_hci
== nb_hcis
)
1651 return hci_table
[cur_hci
++];
1654 static struct HCIInfo
*hci_init(const char *str
)
1657 struct bt_scatternet_s
*vlan
= 0;
1659 if (!strcmp(str
, "null"))
1662 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1664 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1665 else if (!strncmp(str
, "hci", 3)) {
1668 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1669 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1674 vlan
= qemu_find_bt_vlan(0);
1676 return bt_new_hci(vlan
);
1679 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1684 static int bt_hci_parse(const char *str
)
1686 struct HCIInfo
*hci
;
1689 if (nb_hcis
>= MAX_NICS
) {
1690 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1694 hci
= hci_init(str
);
1703 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1704 hci
->bdaddr_set(hci
, bdaddr
.b
);
1706 hci_table
[nb_hcis
++] = hci
;
1711 static void bt_vhci_add(int vlan_id
)
1713 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1716 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1717 "an empty scatternet %i\n", vlan_id
);
1719 bt_vhci_init(bt_new_hci(vlan
));
1722 static struct bt_device_s
*bt_device_add(const char *opt
)
1724 struct bt_scatternet_s
*vlan
;
1726 char *endp
= strstr(opt
, ",vlan=");
1727 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1730 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1733 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1735 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1740 vlan
= qemu_find_bt_vlan(vlan_id
);
1743 fprintf(stderr
, "qemu: warning: adding a slave device to "
1744 "an empty scatternet %i\n", vlan_id
);
1746 if (!strcmp(devname
, "keyboard"))
1747 return bt_keyboard_init(vlan
);
1749 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1753 static int bt_parse(const char *opt
)
1755 const char *endp
, *p
;
1758 if (strstart(opt
, "hci", &endp
)) {
1759 if (!*endp
|| *endp
== ',') {
1761 if (!strstart(endp
, ",vlan=", 0))
1764 return bt_hci_parse(opt
);
1766 } else if (strstart(opt
, "vhci", &endp
)) {
1767 if (!*endp
|| *endp
== ',') {
1769 if (strstart(endp
, ",vlan=", &p
)) {
1770 vlan
= strtol(p
, (char **) &endp
, 0);
1772 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1776 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1785 } else if (strstart(opt
, "device:", &endp
))
1786 return !bt_device_add(endp
);
1788 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1792 /***********************************************************/
1793 /* QEMU Block devices */
1795 #define HD_ALIAS "index=%d,media=disk"
1796 #define CDROM_ALIAS "index=2,media=cdrom"
1797 #define FD_ALIAS "index=%d,if=floppy"
1798 #define PFLASH_ALIAS "if=pflash"
1799 #define MTD_ALIAS "if=mtd"
1800 #define SD_ALIAS "index=0,if=sd"
1802 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1809 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1812 opts
= qemu_opts_parse(&qemu_drive_opts
, optstr
, NULL
);
1814 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1815 __FUNCTION__
, optstr
);
1819 qemu_opt_set(opts
, "file", file
);
1823 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1827 /* seek interface, bus and unit */
1829 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1830 if (dinfo
->type
== type
&&
1831 dinfo
->bus
== bus
&&
1832 dinfo
->unit
== unit
)
1839 DriveInfo
*drive_get_by_id(const char *id
)
1843 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1844 if (strcmp(id
, dinfo
->id
))
1851 int drive_get_max_bus(BlockInterfaceType type
)
1857 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1858 if(dinfo
->type
== type
&&
1859 dinfo
->bus
> max_bus
)
1860 max_bus
= dinfo
->bus
;
1865 const char *drive_get_serial(BlockDriverState
*bdrv
)
1869 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1870 if (dinfo
->bdrv
== bdrv
)
1871 return dinfo
->serial
;
1877 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1881 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1882 if (dinfo
->bdrv
== bdrv
)
1883 return dinfo
->onerror
;
1886 return BLOCK_ERR_STOP_ENOSPC
;
1889 static void bdrv_format_print(void *opaque
, const char *name
)
1891 fprintf(stderr
, " %s", name
);
1894 void drive_uninit(BlockDriverState
*bdrv
)
1898 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1899 if (dinfo
->bdrv
!= bdrv
)
1901 qemu_opts_del(dinfo
->opts
);
1902 QTAILQ_REMOVE(&drives
, dinfo
, next
);
1908 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1912 const char *file
= NULL
;
1915 const char *mediastr
= "";
1916 BlockInterfaceType type
;
1917 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1918 int bus_id
, unit_id
;
1919 int cyls
, heads
, secs
, translation
;
1920 BlockDriver
*drv
= NULL
;
1921 QEMUMachine
*machine
= opaque
;
1926 int bdrv_flags
, onerror
;
1927 const char *devaddr
;
1934 translation
= BIOS_ATA_TRANSLATION_AUTO
;
1937 if (machine
&& machine
->use_scsi
) {
1939 max_devs
= MAX_SCSI_DEVS
;
1940 pstrcpy(devname
, sizeof(devname
), "scsi");
1943 max_devs
= MAX_IDE_DEVS
;
1944 pstrcpy(devname
, sizeof(devname
), "ide");
1948 /* extract parameters */
1949 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
1950 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
1951 index
= qemu_opt_get_number(opts
, "index", -1);
1953 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
1954 heads
= qemu_opt_get_number(opts
, "heads", 0);
1955 secs
= qemu_opt_get_number(opts
, "secs", 0);
1957 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
1959 file
= qemu_opt_get(opts
, "file");
1960 serial
= qemu_opt_get(opts
, "serial");
1962 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
1963 pstrcpy(devname
, sizeof(devname
), buf
);
1964 if (!strcmp(buf
, "ide")) {
1966 max_devs
= MAX_IDE_DEVS
;
1967 } else if (!strcmp(buf
, "scsi")) {
1969 max_devs
= MAX_SCSI_DEVS
;
1970 } else if (!strcmp(buf
, "floppy")) {
1973 } else if (!strcmp(buf
, "pflash")) {
1976 } else if (!strcmp(buf
, "mtd")) {
1979 } else if (!strcmp(buf
, "sd")) {
1982 } else if (!strcmp(buf
, "virtio")) {
1985 } else if (!strcmp(buf
, "xen")) {
1988 } else if (!strcmp(buf
, "none")) {
1992 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
1997 if (cyls
|| heads
|| secs
) {
1998 if (cyls
< 1 || cyls
> 16383) {
1999 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2002 if (heads
< 1 || heads
> 16) {
2003 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2006 if (secs
< 1 || secs
> 63) {
2007 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2012 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2015 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2019 if (!strcmp(buf
, "none"))
2020 translation
= BIOS_ATA_TRANSLATION_NONE
;
2021 else if (!strcmp(buf
, "lba"))
2022 translation
= BIOS_ATA_TRANSLATION_LBA
;
2023 else if (!strcmp(buf
, "auto"))
2024 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2026 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2031 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2032 if (!strcmp(buf
, "disk")) {
2034 } else if (!strcmp(buf
, "cdrom")) {
2035 if (cyls
|| secs
|| heads
) {
2037 "qemu: '%s' invalid physical CHS format\n", buf
);
2040 media
= MEDIA_CDROM
;
2042 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2047 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2048 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2050 else if (!strcmp(buf
, "writethrough"))
2052 else if (!strcmp(buf
, "writeback"))
2055 fprintf(stderr
, "qemu: invalid cache option\n");
2060 #ifdef CONFIG_LINUX_AIO
2061 if ((buf
= qemu_opt_get(opts
, "aio")) != NULL
) {
2062 if (!strcmp(buf
, "threads"))
2064 else if (!strcmp(buf
, "native"))
2067 fprintf(stderr
, "qemu: invalid aio option\n");
2073 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2074 if (strcmp(buf
, "?") == 0) {
2075 fprintf(stderr
, "qemu: Supported formats:");
2076 bdrv_iterate_format(bdrv_format_print
, NULL
);
2077 fprintf(stderr
, "\n");
2080 drv
= bdrv_find_format(buf
);
2082 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2087 is_extboot
= qemu_opt_get_bool(opts
, "boot", 0);
2088 if (is_extboot
&& extboot_drive
) {
2089 fprintf(stderr
, "qemu: two bootable drives specified\n");
2093 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2094 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2095 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2096 fprintf(stderr
, "werror is no supported by this format\n");
2099 if (!strcmp(buf
, "ignore"))
2100 onerror
= BLOCK_ERR_IGNORE
;
2101 else if (!strcmp(buf
, "enospc"))
2102 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2103 else if (!strcmp(buf
, "stop"))
2104 onerror
= BLOCK_ERR_STOP_ANY
;
2105 else if (!strcmp(buf
, "report"))
2106 onerror
= BLOCK_ERR_REPORT
;
2108 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2113 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2114 if (type
!= IF_VIRTIO
) {
2115 fprintf(stderr
, "addr is not supported\n");
2120 /* compute bus and unit according index */
2123 if (bus_id
!= 0 || unit_id
!= -1) {
2125 "qemu: index cannot be used with bus and unit\n");
2133 unit_id
= index
% max_devs
;
2134 bus_id
= index
/ max_devs
;
2138 /* if user doesn't specify a unit_id,
2139 * try to find the first free
2142 if (unit_id
== -1) {
2144 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2146 if (max_devs
&& unit_id
>= max_devs
) {
2147 unit_id
-= max_devs
;
2155 if (max_devs
&& unit_id
>= max_devs
) {
2156 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2157 unit_id
, max_devs
- 1);
2162 * ignore multiple definitions
2165 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2172 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2173 if ((buf
= qemu_opts_id(opts
)) != NULL
) {
2174 dinfo
->id
= qemu_strdup(buf
);
2176 /* no id supplied -> create one */
2177 dinfo
->id
= qemu_mallocz(32);
2178 if (type
== IF_IDE
|| type
== IF_SCSI
)
2179 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2181 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2182 devname
, bus_id
, mediastr
, unit_id
);
2184 snprintf(dinfo
->id
, 32, "%s%s%i",
2185 devname
, mediastr
, unit_id
);
2187 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2188 dinfo
->devaddr
= devaddr
;
2190 dinfo
->bus
= bus_id
;
2191 dinfo
->unit
= unit_id
;
2192 dinfo
->onerror
= onerror
;
2195 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2196 QTAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2198 extboot_drive
= dinfo
;
2209 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2210 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2214 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2219 /* FIXME: This isn't really a floppy, but it's a reasonable
2222 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2228 /* add virtio block device */
2229 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
2230 qemu_opt_set(opts
, "driver", "virtio-blk-pci");
2231 qemu_opt_set(opts
, "drive", dinfo
->id
);
2233 qemu_opt_set(opts
, "addr", devaddr
);
2244 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2245 cache
= 2; /* always use write-back with snapshot */
2247 if (cache
== 0) /* no caching */
2248 bdrv_flags
|= BDRV_O_NOCACHE
;
2249 else if (cache
== 2) /* write-back */
2250 bdrv_flags
|= BDRV_O_CACHE_WB
;
2253 bdrv_flags
|= BDRV_O_NATIVE_AIO
;
2255 bdrv_flags
&= ~BDRV_O_NATIVE_AIO
;
2258 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2259 fprintf(stderr
, "qemu: could not open disk image %s\n",
2264 if (bdrv_key_required(dinfo
->bdrv
))
2270 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2272 QEMUMachine
*machine
= opaque
;
2273 int fatal_error
= 0;
2275 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2282 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2284 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2285 qemu_opt_set(opts
, "snapshot", "on");
2290 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2292 boot_set_handler
= func
;
2293 boot_set_opaque
= opaque
;
2296 int qemu_boot_set(const char *boot_devices
)
2298 if (!boot_set_handler
) {
2301 return boot_set_handler(boot_set_opaque
, boot_devices
);
2304 static int parse_bootdevices(char *devices
)
2306 /* We just do some generic consistency checks */
2310 for (p
= devices
; *p
!= '\0'; p
++) {
2311 /* Allowed boot devices are:
2312 * a-b: floppy disk drives
2313 * c-f: IDE disk drives
2314 * g-m: machine implementation dependant drives
2315 * n-p: network devices
2316 * It's up to each machine implementation to check if the given boot
2317 * devices match the actual hardware implementation and firmware
2320 if (*p
< 'a' || *p
> 'p') {
2321 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2324 if (bitmap
& (1 << (*p
- 'a'))) {
2325 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2328 bitmap
|= 1 << (*p
- 'a');
2333 static void restore_boot_devices(void *opaque
)
2335 char *standard_boot_devices
= opaque
;
2337 qemu_boot_set(standard_boot_devices
);
2339 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2340 qemu_free(standard_boot_devices
);
2343 static void numa_add(const char *optarg
)
2347 unsigned long long value
, endvalue
;
2350 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2351 if (!strcmp(option
, "node")) {
2352 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2353 nodenr
= nb_numa_nodes
;
2355 nodenr
= strtoull(option
, NULL
, 10);
2358 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2359 node_mem
[nodenr
] = 0;
2361 value
= strtoull(option
, &endptr
, 0);
2363 case 0: case 'M': case 'm':
2370 node_mem
[nodenr
] = value
;
2372 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2373 node_cpumask
[nodenr
] = 0;
2375 value
= strtoull(option
, &endptr
, 10);
2378 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2380 if (*endptr
== '-') {
2381 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2382 if (endvalue
>= 63) {
2385 "only 63 CPUs in NUMA mode supported.\n");
2387 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2392 node_cpumask
[nodenr
] = value
;
2399 static void smp_parse(const char *optarg
)
2401 int smp
, sockets
= 0, threads
= 0, cores
= 0;
2405 smp
= strtoul(optarg
, &endptr
, 10);
2406 if (endptr
!= optarg
) {
2407 if (*endptr
== ',') {
2411 if (get_param_value(option
, 128, "sockets", endptr
) != 0)
2412 sockets
= strtoull(option
, NULL
, 10);
2413 if (get_param_value(option
, 128, "cores", endptr
) != 0)
2414 cores
= strtoull(option
, NULL
, 10);
2415 if (get_param_value(option
, 128, "threads", endptr
) != 0)
2416 threads
= strtoull(option
, NULL
, 10);
2417 if (get_param_value(option
, 128, "maxcpus", endptr
) != 0)
2418 max_cpus
= strtoull(option
, NULL
, 10);
2420 /* compute missing values, prefer sockets over cores over threads */
2421 if (smp
== 0 || sockets
== 0) {
2422 sockets
= sockets
> 0 ? sockets
: 1;
2423 cores
= cores
> 0 ? cores
: 1;
2424 threads
= threads
> 0 ? threads
: 1;
2426 smp
= cores
* threads
* sockets
;
2428 sockets
= smp
/ (cores
* threads
);
2432 threads
= threads
> 0 ? threads
: 1;
2433 cores
= smp
/ (sockets
* threads
);
2436 sockets
= smp
/ (cores
* threads
);
2438 threads
= smp
/ (cores
* sockets
);
2443 smp_cores
= cores
> 0 ? cores
: 1;
2444 smp_threads
= threads
> 0 ? threads
: 1;
2446 max_cpus
= smp_cpus
;
2449 /***********************************************************/
2452 static void usb_msd_password_cb(void *opaque
, int err
)
2454 USBDevice
*dev
= opaque
;
2457 usb_device_attach(dev
);
2459 dev
->info
->handle_destroy(dev
);
2468 .qdev
= "QEMU USB Mouse",
2471 .qdev
= "QEMU USB Tablet",
2474 .qdev
= "QEMU USB Keyboard",
2476 .name
= "wacom-tablet",
2477 .qdev
= "QEMU PenPartner Tablet",
2481 static int usb_device_add(const char *devname
, int is_hotplug
)
2484 USBBus
*bus
= usb_bus_find(-1 /* any */);
2485 USBDevice
*dev
= NULL
;
2491 /* simple devices which don't need extra care */
2492 for (i
= 0; i
< ARRAY_SIZE(usbdevs
); i
++) {
2493 if (strcmp(devname
, usbdevs
[i
].name
) != 0)
2495 dev
= usb_create_simple(bus
, usbdevs
[i
].qdev
);
2499 /* the other ones */
2500 if (strstart(devname
, "host:", &p
)) {
2501 dev
= usb_host_device_open(p
);
2502 } else if (strstart(devname
, "disk:", &p
)) {
2503 BlockDriverState
*bs
;
2505 dev
= usb_msd_init(p
);
2508 bs
= usb_msd_get_bdrv(dev
);
2509 if (bdrv_key_required(bs
)) {
2512 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2517 } else if (strstart(devname
, "serial:", &p
)) {
2518 dev
= usb_serial_init(p
);
2519 #ifdef CONFIG_BRLAPI
2520 } else if (!strcmp(devname
, "braille")) {
2521 dev
= usb_baum_init();
2523 } else if (strstart(devname
, "net:", &p
)) {
2526 if (net_client_init(NULL
, "nic", p
) < 0)
2528 nd_table
[nic
].model
= "usb";
2529 dev
= usb_net_init(&nd_table
[nic
]);
2530 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2531 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2532 bt_new_hci(qemu_find_bt_vlan(0)));
2543 static int usb_device_del(const char *devname
)
2548 if (strstart(devname
, "host:", &p
))
2549 return usb_host_device_close(p
);
2554 p
= strchr(devname
, '.');
2557 bus_num
= strtoul(devname
, NULL
, 0);
2558 addr
= strtoul(p
+ 1, NULL
, 0);
2560 return usb_device_delete_addr(bus_num
, addr
);
2563 static int usb_parse(const char *cmdline
)
2565 return usb_device_add(cmdline
, 0);
2568 void do_usb_add(Monitor
*mon
, const QDict
*qdict
)
2570 usb_device_add(qdict_get_str(qdict
, "devname"), 1);
2573 void do_usb_del(Monitor
*mon
, const QDict
*qdict
)
2575 usb_device_del(qdict_get_str(qdict
, "devname"));
2578 /***********************************************************/
2579 /* PCMCIA/Cardbus */
2581 static struct pcmcia_socket_entry_s
{
2582 PCMCIASocket
*socket
;
2583 struct pcmcia_socket_entry_s
*next
;
2584 } *pcmcia_sockets
= 0;
2586 void pcmcia_socket_register(PCMCIASocket
*socket
)
2588 struct pcmcia_socket_entry_s
*entry
;
2590 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2591 entry
->socket
= socket
;
2592 entry
->next
= pcmcia_sockets
;
2593 pcmcia_sockets
= entry
;
2596 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2598 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2600 ptr
= &pcmcia_sockets
;
2601 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2602 if (entry
->socket
== socket
) {
2608 void pcmcia_info(Monitor
*mon
)
2610 struct pcmcia_socket_entry_s
*iter
;
2612 if (!pcmcia_sockets
)
2613 monitor_printf(mon
, "No PCMCIA sockets\n");
2615 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2616 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2617 iter
->socket
->attached
? iter
->socket
->card_string
:
2621 /***********************************************************/
2622 /* register display */
2624 struct DisplayAllocator default_allocator
= {
2625 defaultallocator_create_displaysurface
,
2626 defaultallocator_resize_displaysurface
,
2627 defaultallocator_free_displaysurface
2630 void register_displaystate(DisplayState
*ds
)
2640 DisplayState
*get_displaystate(void)
2642 return display_state
;
2645 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2647 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2648 return ds
->allocator
;
2653 static void dumb_display_init(void)
2655 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2656 ds
->allocator
= &default_allocator
;
2657 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2658 register_displaystate(ds
);
2661 /***********************************************************/
2664 typedef struct IOHandlerRecord
{
2666 IOCanRWHandler
*fd_read_poll
;
2668 IOHandler
*fd_write
;
2671 /* temporary data */
2673 struct IOHandlerRecord
*next
;
2676 static IOHandlerRecord
*first_io_handler
;
2678 /* XXX: fd_read_poll should be suppressed, but an API change is
2679 necessary in the character devices to suppress fd_can_read(). */
2680 int qemu_set_fd_handler2(int fd
,
2681 IOCanRWHandler
*fd_read_poll
,
2683 IOHandler
*fd_write
,
2686 IOHandlerRecord
**pioh
, *ioh
;
2688 if (!fd_read
&& !fd_write
) {
2689 pioh
= &first_io_handler
;
2694 if (ioh
->fd
== fd
) {
2701 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2705 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2706 ioh
->next
= first_io_handler
;
2707 first_io_handler
= ioh
;
2710 ioh
->fd_read_poll
= fd_read_poll
;
2711 ioh
->fd_read
= fd_read
;
2712 ioh
->fd_write
= fd_write
;
2713 ioh
->opaque
= opaque
;
2716 qemu_notify_event();
2720 int qemu_set_fd_handler(int fd
,
2722 IOHandler
*fd_write
,
2725 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2729 /***********************************************************/
2730 /* Polling handling */
2732 typedef struct PollingEntry
{
2735 struct PollingEntry
*next
;
2738 static PollingEntry
*first_polling_entry
;
2740 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2742 PollingEntry
**ppe
, *pe
;
2743 pe
= qemu_mallocz(sizeof(PollingEntry
));
2745 pe
->opaque
= opaque
;
2746 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2751 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2753 PollingEntry
**ppe
, *pe
;
2754 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2756 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2764 /***********************************************************/
2765 /* Wait objects support */
2766 typedef struct WaitObjects
{
2768 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2769 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2770 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2773 static WaitObjects wait_objects
= {0};
2775 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2777 WaitObjects
*w
= &wait_objects
;
2779 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2781 w
->events
[w
->num
] = handle
;
2782 w
->func
[w
->num
] = func
;
2783 w
->opaque
[w
->num
] = opaque
;
2788 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2791 WaitObjects
*w
= &wait_objects
;
2794 for (i
= 0; i
< w
->num
; i
++) {
2795 if (w
->events
[i
] == handle
)
2798 w
->events
[i
] = w
->events
[i
+ 1];
2799 w
->func
[i
] = w
->func
[i
+ 1];
2800 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2808 /***********************************************************/
2809 /* ram save/restore */
2811 #define RAM_SAVE_FLAG_FULL 0x01 /* Obsolete, not used anymore */
2812 #define RAM_SAVE_FLAG_COMPRESS 0x02
2813 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2814 #define RAM_SAVE_FLAG_PAGE 0x08
2815 #define RAM_SAVE_FLAG_EOS 0x10
2817 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2819 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2820 uint32_t *array
= (uint32_t *)page
;
2823 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2824 if (array
[i
] != val
)
2831 static int ram_save_block(QEMUFile
*f
)
2833 static ram_addr_t current_addr
= 0;
2834 ram_addr_t saved_addr
= current_addr
;
2835 ram_addr_t addr
= 0;
2838 while (addr
< last_ram_offset
) {
2839 if (kvm_enabled() && current_addr
== 0) {
2841 r
= kvm_update_dirty_pages_log();
2843 fprintf(stderr
, "%s: update dirty pages log failed %d\n", __FUNCTION__
, r
);
2844 qemu_file_set_error(f
);
2848 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2851 cpu_physical_memory_reset_dirty(current_addr
,
2852 current_addr
+ TARGET_PAGE_SIZE
,
2853 MIGRATION_DIRTY_FLAG
);
2855 p
= qemu_get_ram_ptr(current_addr
);
2857 if (is_dup_page(p
, *p
)) {
2858 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2859 qemu_put_byte(f
, *p
);
2861 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2862 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
2868 addr
+= TARGET_PAGE_SIZE
;
2869 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
2875 static uint64_t bytes_transferred
= 0;
2877 static ram_addr_t
ram_save_remaining(void)
2880 ram_addr_t count
= 0;
2882 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2883 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2890 uint64_t ram_bytes_remaining(void)
2892 return ram_save_remaining() * TARGET_PAGE_SIZE
;
2895 uint64_t ram_bytes_transferred(void)
2897 return bytes_transferred
;
2900 uint64_t ram_bytes_total(void)
2902 return last_ram_offset
;
2905 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
2908 uint64_t bytes_transferred_last
;
2910 uint64_t expected_time
= 0;
2912 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
2913 qemu_file_set_error(f
);
2918 /* Make sure all dirty bits are set */
2919 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
2920 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
2921 cpu_physical_memory_set_dirty(addr
);
2924 /* Enable dirty memory tracking */
2925 cpu_physical_memory_set_dirty_tracking(1);
2927 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
2930 bytes_transferred_last
= bytes_transferred
;
2931 bwidth
= get_clock();
2933 while (!qemu_file_rate_limit(f
)) {
2936 ret
= ram_save_block(f
);
2937 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
2938 if (ret
== 0) /* no more blocks */
2942 bwidth
= get_clock() - bwidth
;
2943 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
2945 /* if we haven't transferred anything this round, force expected_time to a
2946 * a very high value, but without crashing */
2950 /* try transferring iterative blocks of memory */
2954 /* flush all remaining blocks regardless of rate limiting */
2955 while (ram_save_block(f
) != 0) {
2956 bytes_transferred
+= TARGET_PAGE_SIZE
;
2958 cpu_physical_memory_set_dirty_tracking(0);
2961 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
2963 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
2965 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
2968 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
2973 if (version_id
!= 3)
2977 addr
= qemu_get_be64(f
);
2979 flags
= addr
& ~TARGET_PAGE_MASK
;
2980 addr
&= TARGET_PAGE_MASK
;
2982 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
2983 if (addr
!= last_ram_offset
)
2987 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
2988 uint8_t ch
= qemu_get_byte(f
);
2989 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
2992 (!kvm_enabled() || kvm_has_sync_mmu())) {
2993 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
2996 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
2997 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
2998 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3003 void qemu_service_io(void)
3005 qemu_notify_event();
3008 /***********************************************************/
3009 /* bottom halves (can be seen as timers which expire ASAP) */
3020 static QEMUBH
*first_bh
= NULL
;
3022 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3025 bh
= qemu_mallocz(sizeof(QEMUBH
));
3027 bh
->opaque
= opaque
;
3028 bh
->next
= first_bh
;
3033 int qemu_bh_poll(void)
3039 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3040 if (!bh
->deleted
&& bh
->scheduled
) {
3049 /* remove deleted bhs */
3063 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3071 void qemu_bh_schedule(QEMUBH
*bh
)
3077 /* stop the currently executing CPU to execute the BH ASAP */
3078 qemu_notify_event();
3081 void qemu_bh_cancel(QEMUBH
*bh
)
3086 void qemu_bh_delete(QEMUBH
*bh
)
3092 static void qemu_bh_update_timeout(int *timeout
)
3096 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3097 if (!bh
->deleted
&& bh
->scheduled
) {
3099 /* idle bottom halves will be polled at least
3101 *timeout
= MIN(10, *timeout
);
3103 /* non-idle bottom halves will be executed
3112 /***********************************************************/
3113 /* machine registration */
3115 static QEMUMachine
*first_machine
= NULL
;
3116 QEMUMachine
*current_machine
= NULL
;
3118 int qemu_register_machine(QEMUMachine
*m
)
3121 pm
= &first_machine
;
3129 static QEMUMachine
*find_machine(const char *name
)
3133 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3134 if (!strcmp(m
->name
, name
))
3136 if (m
->alias
&& !strcmp(m
->alias
, name
))
3142 static QEMUMachine
*find_default_machine(void)
3146 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3147 if (m
->is_default
) {
3154 /***********************************************************/
3155 /* main execution loop */
3157 static void gui_update(void *opaque
)
3159 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3160 DisplayState
*ds
= opaque
;
3161 DisplayChangeListener
*dcl
= ds
->listeners
;
3165 while (dcl
!= NULL
) {
3166 if (dcl
->gui_timer_interval
&&
3167 dcl
->gui_timer_interval
< interval
)
3168 interval
= dcl
->gui_timer_interval
;
3171 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3174 static void nographic_update(void *opaque
)
3176 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3178 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3181 struct vm_change_state_entry
{
3182 VMChangeStateHandler
*cb
;
3184 QLIST_ENTRY (vm_change_state_entry
) entries
;
3187 static QLIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3189 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3192 VMChangeStateEntry
*e
;
3194 e
= qemu_mallocz(sizeof (*e
));
3198 QLIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3202 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3204 QLIST_REMOVE (e
, entries
);
3208 static void vm_state_notify(int running
, int reason
)
3210 VMChangeStateEntry
*e
;
3212 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3213 e
->cb(e
->opaque
, running
, reason
);
3217 static void resume_all_vcpus(void);
3218 static void pause_all_vcpus(void);
3225 vm_state_notify(1, 0);
3226 qemu_rearm_alarm_timer(alarm_timer
);
3231 /* reset/shutdown handler */
3233 typedef struct QEMUResetEntry
{
3234 QTAILQ_ENTRY(QEMUResetEntry
) entry
;
3235 QEMUResetHandler
*func
;
3239 static QTAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3240 QTAILQ_HEAD_INITIALIZER(reset_handlers
);
3241 static int reset_requested
;
3242 static int shutdown_requested
;
3243 static int powerdown_requested
;
3244 static int debug_requested
;
3245 static int vmstop_requested
;
3247 int qemu_no_shutdown(void)
3249 int r
= no_shutdown
;
3254 int qemu_shutdown_requested(void)
3256 int r
= shutdown_requested
;
3257 shutdown_requested
= 0;
3261 int qemu_reset_requested(void)
3263 int r
= reset_requested
;
3264 reset_requested
= 0;
3268 int qemu_powerdown_requested(void)
3270 int r
= powerdown_requested
;
3271 powerdown_requested
= 0;
3275 static int qemu_debug_requested(void)
3277 int r
= debug_requested
;
3278 debug_requested
= 0;
3282 static int qemu_vmstop_requested(void)
3284 int r
= vmstop_requested
;
3285 vmstop_requested
= 0;
3289 static void do_vm_stop(int reason
)
3292 cpu_disable_ticks();
3295 vm_state_notify(0, reason
);
3299 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3301 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3304 re
->opaque
= opaque
;
3305 QTAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3308 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3312 QTAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3313 if (re
->func
== func
&& re
->opaque
== opaque
) {
3314 QTAILQ_REMOVE(&reset_handlers
, re
, entry
);
3321 void qemu_system_reset(void)
3323 QEMUResetEntry
*re
, *nre
;
3325 /* reset all devices */
3326 QTAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3327 re
->func(re
->opaque
);
3331 void qemu_system_reset_request(void)
3334 shutdown_requested
= 1;
3336 reset_requested
= 1;
3338 if (cpu_single_env
) {
3339 cpu_single_env
->stopped
= 1;
3341 qemu_notify_event();
3344 void qemu_system_shutdown_request(void)
3346 shutdown_requested
= 1;
3347 qemu_notify_event();
3350 void qemu_system_powerdown_request(void)
3352 powerdown_requested
= 1;
3353 qemu_notify_event();
3356 #ifdef CONFIG_IOTHREAD
3357 static void qemu_system_vmstop_request(int reason
)
3359 vmstop_requested
= reason
;
3360 qemu_notify_event();
3365 static int io_thread_fd
= -1;
3367 static void qemu_event_increment(void)
3369 static const char byte
= 0;
3371 if (io_thread_fd
== -1)
3374 write(io_thread_fd
, &byte
, sizeof(byte
));
3377 static void qemu_event_read(void *opaque
)
3379 int fd
= (unsigned long)opaque
;
3382 /* Drain the notify pipe */
3385 len
= read(fd
, buffer
, sizeof(buffer
));
3386 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3389 static int qemu_event_init(void)
3398 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3402 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3406 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3407 (void *)(unsigned long)fds
[0]);
3409 io_thread_fd
= fds
[1];
3418 HANDLE qemu_event_handle
;
3420 static void dummy_event_handler(void *opaque
)
3424 static int qemu_event_init(void)
3426 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3427 if (!qemu_event_handle
) {
3428 fprintf(stderr
, "Failed CreateEvent: %ld\n", GetLastError());
3431 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3435 static void qemu_event_increment(void)
3437 if (!SetEvent(qemu_event_handle
)) {
3438 fprintf(stderr
, "qemu_event_increment: SetEvent failed: %ld\n",
3445 static int cpu_can_run(CPUState
*env
)
3454 #ifndef CONFIG_IOTHREAD
3455 static int qemu_init_main_loop(void)
3457 return qemu_event_init();
3460 void qemu_init_vcpu(void *_env
)
3462 CPUState
*env
= _env
;
3466 env
->nr_cores
= smp_cores
;
3467 env
->nr_threads
= smp_threads
;
3471 int qemu_cpu_self(void *env
)
3476 static void resume_all_vcpus(void)
3480 static void pause_all_vcpus(void)
3484 void qemu_cpu_kick(void *env
)
3489 void qemu_notify_event(void)
3491 CPUState
*env
= cpu_single_env
;
3493 if (kvm_enabled()) {
3494 qemu_kvm_notify_work();
3503 #define qemu_mutex_lock_iothread() do { } while (0)
3504 #define qemu_mutex_unlock_iothread() do { } while (0)
3507 void vm_stop(int reason
)
3512 #else /* CONFIG_IOTHREAD */
3514 #include "qemu-thread.h"
3516 QemuMutex qemu_global_mutex
;
3517 static QemuMutex qemu_fair_mutex
;
3519 static QemuThread io_thread
;
3521 static QemuThread
*tcg_cpu_thread
;
3522 static QemuCond
*tcg_halt_cond
;
3524 static int qemu_system_ready
;
3526 static QemuCond qemu_cpu_cond
;
3528 static QemuCond qemu_system_cond
;
3529 static QemuCond qemu_pause_cond
;
3531 static void block_io_signals(void);
3532 static void unblock_io_signals(void);
3533 static int tcg_has_work(void);
3535 static int qemu_init_main_loop(void)
3539 ret
= qemu_event_init();
3543 qemu_cond_init(&qemu_pause_cond
);
3544 qemu_mutex_init(&qemu_fair_mutex
);
3545 qemu_mutex_init(&qemu_global_mutex
);
3546 qemu_mutex_lock(&qemu_global_mutex
);
3548 unblock_io_signals();
3549 qemu_thread_self(&io_thread
);
3554 static void qemu_wait_io_event(CPUState
*env
)
3556 while (!tcg_has_work())
3557 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3559 qemu_mutex_unlock(&qemu_global_mutex
);
3562 * Users of qemu_global_mutex can be starved, having no chance
3563 * to acquire it since this path will get to it first.
3564 * So use another lock to provide fairness.
3566 qemu_mutex_lock(&qemu_fair_mutex
);
3567 qemu_mutex_unlock(&qemu_fair_mutex
);
3569 qemu_mutex_lock(&qemu_global_mutex
);
3573 qemu_cond_signal(&qemu_pause_cond
);
3577 static int qemu_cpu_exec(CPUState
*env
);
3579 static void *kvm_cpu_thread_fn(void *arg
)
3581 CPUState
*env
= arg
;
3584 qemu_thread_self(env
->thread
);
3588 /* signal CPU creation */
3589 qemu_mutex_lock(&qemu_global_mutex
);
3591 qemu_cond_signal(&qemu_cpu_cond
);
3593 /* and wait for machine initialization */
3594 while (!qemu_system_ready
)
3595 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3598 if (cpu_can_run(env
))
3600 qemu_wait_io_event(env
);
3606 static void tcg_cpu_exec(void);
3608 static void *tcg_cpu_thread_fn(void *arg
)
3610 CPUState
*env
= arg
;
3613 qemu_thread_self(env
->thread
);
3615 /* signal CPU creation */
3616 qemu_mutex_lock(&qemu_global_mutex
);
3617 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3619 qemu_cond_signal(&qemu_cpu_cond
);
3621 /* and wait for machine initialization */
3622 while (!qemu_system_ready
)
3623 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3627 qemu_wait_io_event(cur_cpu
);
3633 void qemu_cpu_kick(void *_env
)
3635 CPUState
*env
= _env
;
3636 qemu_cond_broadcast(env
->halt_cond
);
3638 qemu_thread_signal(env
->thread
, SIGUSR1
);
3641 int qemu_cpu_self(void *env
)
3643 return (cpu_single_env
!= NULL
);
3646 static void cpu_signal(int sig
)
3649 cpu_exit(cpu_single_env
);
3652 static void block_io_signals(void)
3655 struct sigaction sigact
;
3658 sigaddset(&set
, SIGUSR2
);
3659 sigaddset(&set
, SIGIO
);
3660 sigaddset(&set
, SIGALRM
);
3661 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3664 sigaddset(&set
, SIGUSR1
);
3665 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3667 memset(&sigact
, 0, sizeof(sigact
));
3668 sigact
.sa_handler
= cpu_signal
;
3669 sigaction(SIGUSR1
, &sigact
, NULL
);
3672 static void unblock_io_signals(void)
3677 sigaddset(&set
, SIGUSR2
);
3678 sigaddset(&set
, SIGIO
);
3679 sigaddset(&set
, SIGALRM
);
3680 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3683 sigaddset(&set
, SIGUSR1
);
3684 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3687 static void qemu_signal_lock(unsigned int msecs
)
3689 qemu_mutex_lock(&qemu_fair_mutex
);
3691 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3692 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3693 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3696 qemu_mutex_unlock(&qemu_fair_mutex
);
3699 static void qemu_mutex_lock_iothread(void)
3701 if (kvm_enabled()) {
3702 qemu_mutex_lock(&qemu_fair_mutex
);
3703 qemu_mutex_lock(&qemu_global_mutex
);
3704 qemu_mutex_unlock(&qemu_fair_mutex
);
3706 qemu_signal_lock(100);
3709 static void qemu_mutex_unlock_iothread(void)
3711 qemu_mutex_unlock(&qemu_global_mutex
);
3714 static int all_vcpus_paused(void)
3716 CPUState
*penv
= first_cpu
;
3721 penv
= (CPUState
*)penv
->next_cpu
;
3727 static void pause_all_vcpus(void)
3729 CPUState
*penv
= first_cpu
;
3733 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3734 qemu_cpu_kick(penv
);
3735 penv
= (CPUState
*)penv
->next_cpu
;
3738 while (!all_vcpus_paused()) {
3739 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3742 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3743 penv
= (CPUState
*)penv
->next_cpu
;
3748 static void resume_all_vcpus(void)
3750 CPUState
*penv
= first_cpu
;
3755 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3756 qemu_cpu_kick(penv
);
3757 penv
= (CPUState
*)penv
->next_cpu
;
3761 static void tcg_init_vcpu(void *_env
)
3763 CPUState
*env
= _env
;
3764 /* share a single thread for all cpus with TCG */
3765 if (!tcg_cpu_thread
) {
3766 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3767 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3768 qemu_cond_init(env
->halt_cond
);
3769 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3770 while (env
->created
== 0)
3771 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3772 tcg_cpu_thread
= env
->thread
;
3773 tcg_halt_cond
= env
->halt_cond
;
3775 env
->thread
= tcg_cpu_thread
;
3776 env
->halt_cond
= tcg_halt_cond
;
3780 static void kvm_start_vcpu(CPUState
*env
)
3782 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3783 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3784 qemu_cond_init(env
->halt_cond
);
3785 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3786 while (env
->created
== 0)
3787 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3790 void qemu_init_vcpu(void *_env
)
3792 CPUState
*env
= _env
;
3795 kvm_start_vcpu(env
);
3798 env
->nr_cores
= smp_cores
;
3799 env
->nr_threads
= smp_threads
;
3802 void qemu_notify_event(void)
3804 qemu_event_increment();
3807 void vm_stop(int reason
)
3810 qemu_thread_self(&me
);
3812 if (!qemu_thread_equal(&me
, &io_thread
)) {
3813 qemu_system_vmstop_request(reason
);
3815 * FIXME: should not return to device code in case
3816 * vm_stop() has been requested.
3818 if (cpu_single_env
) {
3819 cpu_exit(cpu_single_env
);
3820 cpu_single_env
->stop
= 1;
3831 static void host_main_loop_wait(int *timeout
)
3837 /* XXX: need to suppress polling by better using win32 events */
3839 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3840 ret
|= pe
->func(pe
->opaque
);
3844 WaitObjects
*w
= &wait_objects
;
3846 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
3847 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
3848 if (w
->func
[ret
- WAIT_OBJECT_0
])
3849 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
3851 /* Check for additional signaled events */
3852 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
3854 /* Check if event is signaled */
3855 ret2
= WaitForSingleObject(w
->events
[i
], 0);
3856 if(ret2
== WAIT_OBJECT_0
) {
3858 w
->func
[i
](w
->opaque
[i
]);
3859 } else if (ret2
== WAIT_TIMEOUT
) {
3861 err
= GetLastError();
3862 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
3865 } else if (ret
== WAIT_TIMEOUT
) {
3867 err
= GetLastError();
3868 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
3875 static void host_main_loop_wait(int *timeout
)
3880 void main_loop_wait(int timeout
)
3882 IOHandlerRecord
*ioh
;
3883 fd_set rfds
, wfds
, xfds
;
3887 qemu_bh_update_timeout(&timeout
);
3889 host_main_loop_wait(&timeout
);
3891 /* poll any events */
3892 /* XXX: separate device handlers from system ones */
3897 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3901 (!ioh
->fd_read_poll
||
3902 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
3903 FD_SET(ioh
->fd
, &rfds
);
3907 if (ioh
->fd_write
) {
3908 FD_SET(ioh
->fd
, &wfds
);
3914 tv
.tv_sec
= timeout
/ 1000;
3915 tv
.tv_usec
= (timeout
% 1000) * 1000;
3917 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
3919 qemu_mutex_unlock_iothread();
3920 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
3921 qemu_mutex_lock_iothread();
3923 IOHandlerRecord
**pioh
;
3925 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
3926 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
3927 ioh
->fd_read(ioh
->opaque
);
3928 if (!(ioh
->fd_read_poll
&& ioh
->fd_read_poll(ioh
->opaque
)))
3929 FD_CLR(ioh
->fd
, &rfds
);
3931 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
3932 ioh
->fd_write(ioh
->opaque
);
3936 /* remove deleted IO handlers */
3937 pioh
= &first_io_handler
;
3948 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
3950 /* rearm timer, if not periodic */
3951 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
3952 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
3953 qemu_rearm_alarm_timer(alarm_timer
);
3956 /* vm time timers */
3958 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
3959 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
3960 qemu_get_clock(vm_clock
));
3963 /* real time timers */
3964 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
3965 qemu_get_clock(rt_clock
));
3967 /* Check bottom-halves last in case any of the earlier events triggered
3973 static int qemu_cpu_exec(CPUState
*env
)
3976 #ifdef CONFIG_PROFILER
3980 #ifdef CONFIG_PROFILER
3981 ti
= profile_getclock();
3986 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
3987 env
->icount_decr
.u16
.low
= 0;
3988 env
->icount_extra
= 0;
3989 count
= qemu_next_deadline();
3990 count
= (count
+ (1 << icount_time_shift
) - 1)
3991 >> icount_time_shift
;
3992 qemu_icount
+= count
;
3993 decr
= (count
> 0xffff) ? 0xffff : count
;
3995 env
->icount_decr
.u16
.low
= decr
;
3996 env
->icount_extra
= count
;
3998 ret
= cpu_exec(env
);
3999 #ifdef CONFIG_PROFILER
4000 qemu_time
+= profile_getclock() - ti
;
4003 /* Fold pending instructions back into the
4004 instruction counter, and clear the interrupt flag. */
4005 qemu_icount
-= (env
->icount_decr
.u16
.low
4006 + env
->icount_extra
);
4007 env
->icount_decr
.u32
= 0;
4008 env
->icount_extra
= 0;
4013 static void tcg_cpu_exec(void)
4017 if (next_cpu
== NULL
)
4018 next_cpu
= first_cpu
;
4019 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4020 CPUState
*env
= cur_cpu
= next_cpu
;
4024 if (timer_alarm_pending
) {
4025 timer_alarm_pending
= 0;
4028 if (cpu_can_run(env
))
4029 ret
= qemu_cpu_exec(env
);
4030 if (ret
== EXCP_DEBUG
) {
4031 gdb_set_stop_cpu(env
);
4032 debug_requested
= 1;
4038 static int cpu_has_work(CPUState
*env
)
4046 if (qemu_cpu_has_work(env
))
4051 static int tcg_has_work(void)
4055 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4056 if (cpu_has_work(env
))
4061 static int qemu_calculate_timeout(void)
4063 #ifndef CONFIG_IOTHREAD
4068 else if (tcg_has_work())
4070 else if (!use_icount
)
4073 /* XXX: use timeout computed from timers */
4076 /* Advance virtual time to the next event. */
4077 if (use_icount
== 1) {
4078 /* When not using an adaptive execution frequency
4079 we tend to get badly out of sync with real time,
4080 so just delay for a reasonable amount of time. */
4083 delta
= cpu_get_icount() - cpu_get_clock();
4086 /* If virtual time is ahead of real time then just
4088 timeout
= (delta
/ 1000000) + 1;
4090 /* Wait for either IO to occur or the next
4092 add
= qemu_next_deadline();
4093 /* We advance the timer before checking for IO.
4094 Limit the amount we advance so that early IO
4095 activity won't get the guest too far ahead. */
4099 add
= (add
+ (1 << icount_time_shift
) - 1)
4100 >> icount_time_shift
;
4102 timeout
= delta
/ 1000000;
4109 #else /* CONFIG_IOTHREAD */
4114 static int vm_can_run(void)
4116 if (powerdown_requested
)
4118 if (reset_requested
)
4120 if (shutdown_requested
)
4122 if (debug_requested
)
4127 qemu_irq qemu_system_powerdown
;
4129 static void main_loop(void)
4133 if (kvm_enabled()) {
4135 cpu_disable_ticks();
4139 #ifdef CONFIG_IOTHREAD
4140 qemu_system_ready
= 1;
4141 qemu_cond_broadcast(&qemu_system_cond
);
4146 #ifdef CONFIG_PROFILER
4149 #ifndef CONFIG_IOTHREAD
4152 #ifdef CONFIG_PROFILER
4153 ti
= profile_getclock();
4155 main_loop_wait(qemu_calculate_timeout());
4156 #ifdef CONFIG_PROFILER
4157 dev_time
+= profile_getclock() - ti
;
4159 } while (vm_can_run());
4161 if (qemu_debug_requested())
4162 vm_stop(EXCP_DEBUG
);
4163 if (qemu_shutdown_requested()) {
4170 if (qemu_reset_requested()) {
4172 qemu_system_reset();
4175 if (qemu_powerdown_requested()) {
4176 qemu_irq_raise(qemu_system_powerdown
);
4178 if ((r
= qemu_vmstop_requested()))
4184 static void version(void)
4186 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4189 static void help(int exitcode
)
4192 printf("usage: %s [options] [disk_image]\n"
4194 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4196 #define DEF(option, opt_arg, opt_enum, opt_help) \
4198 #define DEFHEADING(text) stringify(text) "\n"
4199 #include "qemu-options.h"
4204 "During emulation, the following keys are useful:\n"
4205 "ctrl-alt-f toggle full screen\n"
4206 "ctrl-alt-n switch to virtual console 'n'\n"
4207 "ctrl-alt toggle mouse and keyboard grab\n"
4209 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4214 DEFAULT_NETWORK_SCRIPT
,
4215 DEFAULT_NETWORK_DOWN_SCRIPT
,
4217 DEFAULT_GDBSTUB_PORT
,
4222 #define HAS_ARG 0x0001
4225 #define DEF(option, opt_arg, opt_enum, opt_help) \
4227 #define DEFHEADING(text)
4228 #include "qemu-options.h"
4234 typedef struct QEMUOption
{
4240 static const QEMUOption qemu_options
[] = {
4241 { "h", 0, QEMU_OPTION_h
},
4242 #define DEF(option, opt_arg, opt_enum, opt_help) \
4243 { option, opt_arg, opt_enum },
4244 #define DEFHEADING(text)
4245 #include "qemu-options.h"
4253 struct soundhw soundhw
[] = {
4254 #ifdef HAS_AUDIO_CHOICE
4255 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4261 { .init_isa
= pcspk_audio_init
}
4268 "Creative Sound Blaster 16",
4271 { .init_isa
= SB16_init
}
4275 #ifdef CONFIG_CS4231A
4281 { .init_isa
= cs4231a_init
}
4289 "Yamaha YMF262 (OPL3)",
4291 "Yamaha YM3812 (OPL2)",
4295 { .init_isa
= Adlib_init
}
4302 "Gravis Ultrasound GF1",
4305 { .init_isa
= GUS_init
}
4312 "Intel 82801AA AC97 Audio",
4315 { .init_pci
= ac97_init
}
4319 #ifdef CONFIG_ES1370
4322 "ENSONIQ AudioPCI ES1370",
4325 { .init_pci
= es1370_init
}
4329 #endif /* HAS_AUDIO_CHOICE */
4331 { NULL
, NULL
, 0, 0, { NULL
} }
4334 static void select_soundhw (const char *optarg
)
4338 if (*optarg
== '?') {
4341 printf ("Valid sound card names (comma separated):\n");
4342 for (c
= soundhw
; c
->name
; ++c
) {
4343 printf ("%-11s %s\n", c
->name
, c
->descr
);
4345 printf ("\n-soundhw all will enable all of the above\n");
4346 exit (*optarg
!= '?');
4354 if (!strcmp (optarg
, "all")) {
4355 for (c
= soundhw
; c
->name
; ++c
) {
4363 e
= strchr (p
, ',');
4364 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4366 for (c
= soundhw
; c
->name
; ++c
) {
4367 if (!strncmp (c
->name
, p
, l
) && !c
->name
[l
]) {
4376 "Unknown sound card name (too big to show)\n");
4379 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4384 p
+= l
+ (e
!= NULL
);
4388 goto show_valid_cards
;
4393 static void select_vgahw (const char *p
)
4397 vga_interface_type
= VGA_NONE
;
4398 if (strstart(p
, "std", &opts
)) {
4399 vga_interface_type
= VGA_STD
;
4400 } else if (strstart(p
, "cirrus", &opts
)) {
4401 vga_interface_type
= VGA_CIRRUS
;
4402 } else if (strstart(p
, "vmware", &opts
)) {
4403 vga_interface_type
= VGA_VMWARE
;
4404 } else if (strstart(p
, "xenfb", &opts
)) {
4405 vga_interface_type
= VGA_XENFB
;
4406 } else if (!strstart(p
, "none", &opts
)) {
4408 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4412 const char *nextopt
;
4414 if (strstart(opts
, ",retrace=", &nextopt
)) {
4416 if (strstart(opts
, "dumb", &nextopt
))
4417 vga_retrace_method
= VGA_RETRACE_DUMB
;
4418 else if (strstart(opts
, "precise", &nextopt
))
4419 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4420 else goto invalid_vga
;
4421 } else goto invalid_vga
;
4427 static int balloon_parse(const char *arg
)
4431 if (strcmp(arg
, "none") == 0) {
4435 if (!strncmp(arg
, "virtio", 6)) {
4436 if (arg
[6] == ',') {
4437 /* have params -> parse them */
4438 opts
= qemu_opts_parse(&qemu_device_opts
, arg
+7, NULL
);
4442 /* create empty opts */
4443 opts
= qemu_opts_create(&qemu_device_opts
, NULL
, 0);
4445 qemu_opt_set(opts
, "driver", "virtio-balloon-pci");
4454 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4456 exit(STATUS_CONTROL_C_EXIT
);
4461 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4465 if(strlen(str
) != 36)
4468 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4469 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4470 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4476 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4482 #define MAX_NET_CLIENTS 32
4486 static void termsig_handler(int signal
)
4488 qemu_system_shutdown_request();
4491 static void sigchld_handler(int signal
)
4493 waitpid(-1, NULL
, WNOHANG
);
4496 static void sighandler_setup(void)
4498 struct sigaction act
;
4500 memset(&act
, 0, sizeof(act
));
4501 act
.sa_handler
= termsig_handler
;
4502 sigaction(SIGINT
, &act
, NULL
);
4503 sigaction(SIGHUP
, &act
, NULL
);
4504 sigaction(SIGTERM
, &act
, NULL
);
4506 act
.sa_handler
= sigchld_handler
;
4507 act
.sa_flags
= SA_NOCLDSTOP
;
4508 sigaction(SIGCHLD
, &act
, NULL
);
4514 /* Look for support files in the same directory as the executable. */
4515 static char *find_datadir(const char *argv0
)
4521 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4528 while (p
!= buf
&& *p
!= '\\')
4531 if (access(buf
, R_OK
) == 0) {
4532 return qemu_strdup(buf
);
4538 /* Find a likely location for support files using the location of the binary.
4539 For installed binaries this will be "$bindir/../share/qemu". When
4540 running from the build tree this will be "$bindir/../pc-bios". */
4541 #define SHARE_SUFFIX "/share/qemu"
4542 #define BUILD_SUFFIX "/pc-bios"
4543 static char *find_datadir(const char *argv0
)
4551 #if defined(__linux__)
4554 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4560 #elif defined(__FreeBSD__)
4563 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4570 /* If we don't have any way of figuring out the actual executable
4571 location then try argv[0]. */
4573 p
= realpath(argv0
, buf
);
4581 max_len
= strlen(dir
) +
4582 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4583 res
= qemu_mallocz(max_len
);
4584 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4585 if (access(res
, R_OK
)) {
4586 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4587 if (access(res
, R_OK
)) {
4599 char *qemu_find_file(int type
, const char *name
)
4605 /* If name contains path separators then try it as a straight path. */
4606 if ((strchr(name
, '/') || strchr(name
, '\\'))
4607 && access(name
, R_OK
) == 0) {
4608 return qemu_strdup(name
);
4611 case QEMU_FILE_TYPE_BIOS
:
4614 case QEMU_FILE_TYPE_KEYMAP
:
4615 subdir
= "keymaps/";
4620 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4621 buf
= qemu_mallocz(len
);
4622 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4623 if (access(buf
, R_OK
)) {
4630 static int device_init_func(QemuOpts
*opts
, void *opaque
)
4634 dev
= qdev_device_add(opts
);
4640 struct device_config
{
4642 DEV_USB
, /* -usbdevice */
4645 const char *cmdline
;
4646 QTAILQ_ENTRY(device_config
) next
;
4648 QTAILQ_HEAD(, device_config
) device_configs
= QTAILQ_HEAD_INITIALIZER(device_configs
);
4650 static void add_device_config(int type
, const char *cmdline
)
4652 struct device_config
*conf
;
4654 conf
= qemu_mallocz(sizeof(*conf
));
4656 conf
->cmdline
= cmdline
;
4657 QTAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4660 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4662 struct device_config
*conf
;
4665 QTAILQ_FOREACH(conf
, &device_configs
, next
) {
4666 if (conf
->type
!= type
)
4668 rc
= func(conf
->cmdline
);
4675 int main(int argc
, char **argv
, char **envp
)
4677 const char *gdbstub_dev
= NULL
;
4678 uint32_t boot_devices_bitmap
= 0;
4680 int snapshot
, linux_boot
, net_boot
;
4681 const char *initrd_filename
;
4682 const char *kernel_filename
, *kernel_cmdline
;
4683 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4685 DisplayChangeListener
*dcl
;
4686 int cyls
, heads
, secs
, translation
;
4687 const char *net_clients
[MAX_NET_CLIENTS
];
4689 QemuOpts
*hda_opts
= NULL
, *opts
;
4691 const char *r
, *optarg
;
4692 CharDriverState
*monitor_hds
[MAX_MONITOR_DEVICES
];
4693 const char *monitor_devices
[MAX_MONITOR_DEVICES
];
4694 int monitor_device_index
;
4695 const char *serial_devices
[MAX_SERIAL_PORTS
];
4696 int serial_device_index
;
4697 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4698 int parallel_device_index
;
4699 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4700 int virtio_console_index
;
4701 const char *loadvm
= NULL
;
4702 QEMUMachine
*machine
;
4703 const char *cpu_model
;
4708 const char *pid_file
= NULL
;
4709 const char *incoming
= NULL
;
4712 struct passwd
*pwd
= NULL
;
4713 const char *chroot_dir
= NULL
;
4714 const char *run_as
= NULL
;
4717 int show_vnc_port
= 0;
4719 qemu_errors_to_file(stderr
);
4720 qemu_cache_utils_init(envp
);
4722 QLIST_INIT (&vm_change_state_head
);
4725 struct sigaction act
;
4726 sigfillset(&act
.sa_mask
);
4728 act
.sa_handler
= SIG_IGN
;
4729 sigaction(SIGPIPE
, &act
, NULL
);
4732 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4733 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4734 QEMU to run on a single CPU */
4739 h
= GetCurrentProcess();
4740 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4741 for(i
= 0; i
< 32; i
++) {
4742 if (mask
& (1 << i
))
4747 SetProcessAffinityMask(h
, mask
);
4753 module_call_init(MODULE_INIT_MACHINE
);
4754 machine
= find_default_machine();
4756 initrd_filename
= NULL
;
4759 kernel_filename
= NULL
;
4760 kernel_cmdline
= "";
4761 cyls
= heads
= secs
= 0;
4762 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4764 serial_devices
[0] = "vc:80Cx24C";
4765 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4766 serial_devices
[i
] = NULL
;
4767 serial_device_index
= 0;
4769 parallel_devices
[0] = "vc:80Cx24C";
4770 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4771 parallel_devices
[i
] = NULL
;
4772 parallel_device_index
= 0;
4774 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4775 virtio_consoles
[i
] = NULL
;
4776 virtio_console_index
= 0;
4778 monitor_devices
[0] = "vc:80Cx24C";
4779 for (i
= 1; i
< MAX_MONITOR_DEVICES
; i
++) {
4780 monitor_devices
[i
] = NULL
;
4782 monitor_device_index
= 0;
4784 for (i
= 0; i
< MAX_NODES
; i
++) {
4786 node_cpumask
[i
] = 0;
4789 assigned_devices_index
= 0;
4804 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4806 const QEMUOption
*popt
;
4809 /* Treat --foo the same as -foo. */
4812 popt
= qemu_options
;
4815 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4819 if (!strcmp(popt
->name
, r
+ 1))
4823 if (popt
->flags
& HAS_ARG
) {
4824 if (optind
>= argc
) {
4825 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4829 optarg
= argv
[optind
++];
4834 switch(popt
->index
) {
4836 machine
= find_machine(optarg
);
4839 printf("Supported machines are:\n");
4840 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4842 printf("%-10s %s (alias of %s)\n",
4843 m
->alias
, m
->desc
, m
->name
);
4844 printf("%-10s %s%s\n",
4846 m
->is_default
? " (default)" : "");
4848 exit(*optarg
!= '?');
4851 case QEMU_OPTION_cpu
:
4852 /* hw initialization will check this */
4853 if (*optarg
== '?') {
4854 /* XXX: implement xxx_cpu_list for targets that still miss it */
4855 #if defined(cpu_list)
4856 cpu_list(stdout
, &fprintf
);
4863 case QEMU_OPTION_initrd
:
4864 initrd_filename
= optarg
;
4866 case QEMU_OPTION_hda
:
4868 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
4870 hda_opts
= drive_add(optarg
, HD_ALIAS
4871 ",cyls=%d,heads=%d,secs=%d%s",
4872 0, cyls
, heads
, secs
,
4873 translation
== BIOS_ATA_TRANSLATION_LBA
?
4875 translation
== BIOS_ATA_TRANSLATION_NONE
?
4876 ",trans=none" : "");
4878 case QEMU_OPTION_hdb
:
4879 case QEMU_OPTION_hdc
:
4880 case QEMU_OPTION_hdd
:
4881 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
4883 case QEMU_OPTION_drive
:
4884 drive_add(NULL
, "%s", optarg
);
4886 case QEMU_OPTION_set
:
4887 if (qemu_set_option(optarg
) != 0)
4890 case QEMU_OPTION_mtdblock
:
4891 drive_add(optarg
, MTD_ALIAS
);
4893 case QEMU_OPTION_sd
:
4894 drive_add(optarg
, SD_ALIAS
);
4896 case QEMU_OPTION_pflash
:
4897 drive_add(optarg
, PFLASH_ALIAS
);
4899 case QEMU_OPTION_snapshot
:
4902 case QEMU_OPTION_hdachs
:
4906 cyls
= strtol(p
, (char **)&p
, 0);
4907 if (cyls
< 1 || cyls
> 16383)
4912 heads
= strtol(p
, (char **)&p
, 0);
4913 if (heads
< 1 || heads
> 16)
4918 secs
= strtol(p
, (char **)&p
, 0);
4919 if (secs
< 1 || secs
> 63)
4923 if (!strcmp(p
, "none"))
4924 translation
= BIOS_ATA_TRANSLATION_NONE
;
4925 else if (!strcmp(p
, "lba"))
4926 translation
= BIOS_ATA_TRANSLATION_LBA
;
4927 else if (!strcmp(p
, "auto"))
4928 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4931 } else if (*p
!= '\0') {
4933 fprintf(stderr
, "qemu: invalid physical CHS format\n");
4936 if (hda_opts
!= NULL
) {
4938 snprintf(num
, sizeof(num
), "%d", cyls
);
4939 qemu_opt_set(hda_opts
, "cyls", num
);
4940 snprintf(num
, sizeof(num
), "%d", heads
);
4941 qemu_opt_set(hda_opts
, "heads", num
);
4942 snprintf(num
, sizeof(num
), "%d", secs
);
4943 qemu_opt_set(hda_opts
, "secs", num
);
4944 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
4945 qemu_opt_set(hda_opts
, "trans", "lba");
4946 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
4947 qemu_opt_set(hda_opts
, "trans", "none");
4951 case QEMU_OPTION_numa
:
4952 if (nb_numa_nodes
>= MAX_NODES
) {
4953 fprintf(stderr
, "qemu: too many NUMA nodes\n");
4958 case QEMU_OPTION_nographic
:
4959 display_type
= DT_NOGRAPHIC
;
4961 #ifdef CONFIG_CURSES
4962 case QEMU_OPTION_curses
:
4963 display_type
= DT_CURSES
;
4966 case QEMU_OPTION_portrait
:
4969 case QEMU_OPTION_kernel
:
4970 kernel_filename
= optarg
;
4972 case QEMU_OPTION_append
:
4973 kernel_cmdline
= optarg
;
4975 case QEMU_OPTION_cdrom
:
4976 drive_add(optarg
, CDROM_ALIAS
);
4978 case QEMU_OPTION_boot
:
4980 static const char * const params
[] = {
4981 "order", "once", "menu", NULL
4983 char buf
[sizeof(boot_devices
)];
4984 char *standard_boot_devices
;
4987 if (!strchr(optarg
, '=')) {
4989 pstrcpy(buf
, sizeof(buf
), optarg
);
4990 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
4992 "qemu: unknown boot parameter '%s' in '%s'\n",
4998 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
4999 boot_devices_bitmap
= parse_bootdevices(buf
);
5000 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5003 if (get_param_value(buf
, sizeof(buf
),
5005 boot_devices_bitmap
|= parse_bootdevices(buf
);
5006 standard_boot_devices
= qemu_strdup(boot_devices
);
5007 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5008 qemu_register_reset(restore_boot_devices
,
5009 standard_boot_devices
);
5011 if (get_param_value(buf
, sizeof(buf
),
5013 if (!strcmp(buf
, "on")) {
5015 } else if (!strcmp(buf
, "off")) {
5019 "qemu: invalid option value '%s'\n",
5027 case QEMU_OPTION_fda
:
5028 case QEMU_OPTION_fdb
:
5029 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5032 case QEMU_OPTION_no_fd_bootchk
:
5036 case QEMU_OPTION_net
:
5037 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5038 fprintf(stderr
, "qemu: too many network clients\n");
5041 net_clients
[nb_net_clients
] = optarg
;
5045 case QEMU_OPTION_tftp
:
5046 legacy_tftp_prefix
= optarg
;
5048 case QEMU_OPTION_bootp
:
5049 legacy_bootp_filename
= optarg
;
5052 case QEMU_OPTION_smb
:
5053 net_slirp_smb(optarg
);
5056 case QEMU_OPTION_redir
:
5057 net_slirp_redir(optarg
);
5060 case QEMU_OPTION_bt
:
5061 add_device_config(DEV_BT
, optarg
);
5064 case QEMU_OPTION_audio_help
:
5068 case QEMU_OPTION_soundhw
:
5069 select_soundhw (optarg
);
5075 case QEMU_OPTION_version
:
5079 case QEMU_OPTION_m
: {
5083 value
= strtoul(optarg
, &ptr
, 10);
5085 case 0: case 'M': case 'm':
5092 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5096 /* On 32-bit hosts, QEMU is limited by virtual address space */
5097 if (value
> (2047 << 20) && HOST_LONG_BITS
== 32) {
5098 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5101 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5102 fprintf(stderr
, "qemu: ram size too large\n");
5111 const CPULogItem
*item
;
5113 mask
= cpu_str_to_log_mask(optarg
);
5115 printf("Log items (comma separated):\n");
5116 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5117 printf("%-10s %s\n", item
->name
, item
->help
);
5125 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5127 case QEMU_OPTION_gdb
:
5128 gdbstub_dev
= optarg
;
5133 case QEMU_OPTION_bios
:
5136 case QEMU_OPTION_singlestep
:
5144 keyboard_layout
= optarg
;
5147 case QEMU_OPTION_localtime
:
5150 case QEMU_OPTION_vga
:
5151 select_vgahw (optarg
);
5153 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5159 w
= strtol(p
, (char **)&p
, 10);
5162 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5168 h
= strtol(p
, (char **)&p
, 10);
5173 depth
= strtol(p
, (char **)&p
, 10);
5174 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5175 depth
!= 24 && depth
!= 32)
5177 } else if (*p
== '\0') {
5178 depth
= graphic_depth
;
5185 graphic_depth
= depth
;
5189 case QEMU_OPTION_echr
:
5192 term_escape_char
= strtol(optarg
, &r
, 0);
5194 printf("Bad argument to echr\n");
5197 case QEMU_OPTION_monitor
:
5198 if (monitor_device_index
>= MAX_MONITOR_DEVICES
) {
5199 fprintf(stderr
, "qemu: too many monitor devices\n");
5202 monitor_devices
[monitor_device_index
] = optarg
;
5203 monitor_device_index
++;
5205 case QEMU_OPTION_chardev
:
5206 opts
= qemu_opts_parse(&qemu_chardev_opts
, optarg
, "backend");
5208 fprintf(stderr
, "parse error: %s\n", optarg
);
5211 if (qemu_chr_open_opts(opts
, NULL
) == NULL
) {
5215 case QEMU_OPTION_serial
:
5216 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5217 fprintf(stderr
, "qemu: too many serial ports\n");
5220 serial_devices
[serial_device_index
] = optarg
;
5221 serial_device_index
++;
5223 case QEMU_OPTION_watchdog
:
5226 "qemu: only one watchdog option may be given\n");
5231 case QEMU_OPTION_watchdog_action
:
5232 if (select_watchdog_action(optarg
) == -1) {
5233 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5237 case QEMU_OPTION_virtiocon
:
5238 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5239 fprintf(stderr
, "qemu: too many virtio consoles\n");
5242 virtio_consoles
[virtio_console_index
] = optarg
;
5243 virtio_console_index
++;
5245 case QEMU_OPTION_parallel
:
5246 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5247 fprintf(stderr
, "qemu: too many parallel ports\n");
5250 parallel_devices
[parallel_device_index
] = optarg
;
5251 parallel_device_index
++;
5253 case QEMU_OPTION_loadvm
:
5256 case QEMU_OPTION_full_screen
:
5260 case QEMU_OPTION_no_frame
:
5263 case QEMU_OPTION_alt_grab
:
5266 case QEMU_OPTION_no_quit
:
5269 case QEMU_OPTION_sdl
:
5270 display_type
= DT_SDL
;
5273 case QEMU_OPTION_pidfile
:
5277 case QEMU_OPTION_win2k_hack
:
5278 win2k_install_hack
= 1;
5280 case QEMU_OPTION_rtc_td_hack
:
5283 case QEMU_OPTION_acpitable
:
5284 if(acpi_table_add(optarg
) < 0) {
5285 fprintf(stderr
, "Wrong acpi table provided\n");
5289 case QEMU_OPTION_smbios
:
5290 if(smbios_entry_add(optarg
) < 0) {
5291 fprintf(stderr
, "Wrong smbios provided\n");
5298 case QEMU_OPTION_enable_kvm
:
5302 case QEMU_OPTION_no_kvm
:
5305 case QEMU_OPTION_no_kvm_irqchip
: {
5310 case QEMU_OPTION_no_kvm_pit
: {
5314 case QEMU_OPTION_no_kvm_pit_reinjection
: {
5315 kvm_pit_reinject
= 0;
5318 case QEMU_OPTION_enable_nesting
: {
5322 #if defined(TARGET_I386) || defined(TARGET_X86_64) || defined(TARGET_IA64) || defined(__linux__)
5323 case QEMU_OPTION_pcidevice
:
5324 if (assigned_devices_index
>= MAX_DEV_ASSIGN_CMDLINE
) {
5325 fprintf(stderr
, "Too many assigned devices\n");
5328 assigned_devices
[assigned_devices_index
] = optarg
;
5329 assigned_devices_index
++;
5333 case QEMU_OPTION_usb
:
5336 case QEMU_OPTION_usbdevice
:
5338 add_device_config(DEV_USB
, optarg
);
5340 case QEMU_OPTION_device
:
5341 opts
= qemu_opts_parse(&qemu_device_opts
, optarg
, "driver");
5343 fprintf(stderr
, "parse error: %s\n", optarg
);
5347 case QEMU_OPTION_smp
:
5350 fprintf(stderr
, "Invalid number of CPUs\n");
5353 if (max_cpus
< smp_cpus
) {
5354 fprintf(stderr
, "maxcpus must be equal to or greater than "
5358 if (max_cpus
> 255) {
5359 fprintf(stderr
, "Unsupported number of maxcpus\n");
5363 case QEMU_OPTION_vnc
:
5364 display_type
= DT_VNC
;
5365 vnc_display
= optarg
;
5368 case QEMU_OPTION_no_acpi
:
5371 case QEMU_OPTION_no_hpet
:
5374 case QEMU_OPTION_balloon
:
5375 if (balloon_parse(optarg
) < 0) {
5376 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5381 case QEMU_OPTION_no_reboot
:
5384 case QEMU_OPTION_no_shutdown
:
5387 case QEMU_OPTION_show_cursor
:
5390 case QEMU_OPTION_uuid
:
5391 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5392 fprintf(stderr
, "Fail to parse UUID string."
5393 " Wrong format.\n");
5398 case QEMU_OPTION_daemonize
:
5402 case QEMU_OPTION_option_rom
:
5403 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5404 fprintf(stderr
, "Too many option ROMs\n");
5407 option_rom
[nb_option_roms
] = optarg
;
5410 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5411 case QEMU_OPTION_semihosting
:
5412 semihosting_enabled
= 1;
5415 case QEMU_OPTION_tdf
:
5418 case QEMU_OPTION_kvm_shadow_memory
:
5419 kvm_shadow_memory
= (int64_t)atoi(optarg
) * 1024 * 1024 / 4096;
5421 case QEMU_OPTION_mempath
:
5425 case QEMU_OPTION_mem_prealloc
:
5426 mem_prealloc
= !mem_prealloc
;
5429 case QEMU_OPTION_name
:
5430 qemu_name
= qemu_strdup(optarg
);
5432 char *p
= strchr(qemu_name
, ',');
5435 if (strncmp(p
, "process=", 8)) {
5436 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5444 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5445 case QEMU_OPTION_prom_env
:
5446 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5447 fprintf(stderr
, "Too many prom variables\n");
5450 prom_envs
[nb_prom_envs
] = optarg
;
5455 case QEMU_OPTION_old_param
:
5459 case QEMU_OPTION_clock
:
5460 configure_alarms(optarg
);
5462 case QEMU_OPTION_startdate
:
5465 time_t rtc_start_date
;
5466 if (!strcmp(optarg
, "now")) {
5467 rtc_date_offset
= -1;
5469 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5477 } else if (sscanf(optarg
, "%d-%d-%d",
5480 &tm
.tm_mday
) == 3) {
5489 rtc_start_date
= mktimegm(&tm
);
5490 if (rtc_start_date
== -1) {
5492 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5493 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5496 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5500 case QEMU_OPTION_tb_size
:
5501 tb_size
= strtol(optarg
, NULL
, 0);
5505 case QEMU_OPTION_icount
:
5507 if (strcmp(optarg
, "auto") == 0) {
5508 icount_time_shift
= -1;
5510 icount_time_shift
= strtol(optarg
, NULL
, 0);
5513 case QEMU_OPTION_incoming
:
5517 case QEMU_OPTION_chroot
:
5518 chroot_dir
= optarg
;
5520 case QEMU_OPTION_runas
:
5523 case QEMU_OPTION_nvram
:
5528 case QEMU_OPTION_xen_domid
:
5529 xen_domid
= atoi(optarg
);
5531 case QEMU_OPTION_xen_create
:
5532 xen_mode
= XEN_CREATE
;
5534 case QEMU_OPTION_xen_attach
:
5535 xen_mode
= XEN_ATTACH
;
5542 /* If no data_dir is specified then try to find it relative to the
5545 data_dir
= find_datadir(argv
[0]);
5547 /* If all else fails use the install patch specified when building. */
5549 data_dir
= CONFIG_QEMU_SHAREDIR
;
5553 * Default to max_cpus = smp_cpus, in case the user doesn't
5554 * specify a max_cpus value.
5557 max_cpus
= smp_cpus
;
5559 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5560 if (smp_cpus
> machine
->max_cpus
) {
5561 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5562 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5567 if (display_type
== DT_NOGRAPHIC
) {
5568 if (serial_device_index
== 0)
5569 serial_devices
[0] = "stdio";
5570 if (parallel_device_index
== 0)
5571 parallel_devices
[0] = "null";
5572 if (strncmp(monitor_devices
[0], "vc", 2) == 0) {
5573 monitor_devices
[0] = "stdio";
5581 if (pipe(fds
) == -1)
5592 len
= read(fds
[0], &status
, 1);
5593 if (len
== -1 && (errno
== EINTR
))
5598 else if (status
== 1) {
5599 fprintf(stderr
, "Could not acquire pidfile\n");
5616 signal(SIGTSTP
, SIG_IGN
);
5617 signal(SIGTTOU
, SIG_IGN
);
5618 signal(SIGTTIN
, SIG_IGN
);
5621 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5624 write(fds
[1], &status
, 1);
5626 fprintf(stderr
, "Could not acquire pid file\n");
5631 if (kvm_enabled()) {
5634 ret
= kvm_init(smp_cpus
);
5636 #if defined(KVM_UPSTREAM) || defined(CONFIG_NO_CPU_EMULATION)
5637 fprintf(stderr
, "failed to initialize KVM\n");
5640 fprintf(stderr
, "Could not initialize KVM, will disable KVM support\n");
5645 if (qemu_init_main_loop()) {
5646 fprintf(stderr
, "qemu_init_main_loop failed\n");
5649 linux_boot
= (kernel_filename
!= NULL
);
5651 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5652 fprintf(stderr
, "-append only allowed with -kernel option\n");
5656 if (!linux_boot
&& initrd_filename
!= NULL
) {
5657 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5662 /* Win32 doesn't support line-buffering and requires size >= 2 */
5663 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5667 if (init_timer_alarm() < 0) {
5668 fprintf(stderr
, "could not initialize alarm timer\n");
5671 if (use_icount
&& icount_time_shift
< 0) {
5673 /* 125MIPS seems a reasonable initial guess at the guest speed.
5674 It will be corrected fairly quickly anyway. */
5675 icount_time_shift
= 3;
5676 init_icount_adjust();
5683 /* init network clients */
5684 if (nb_net_clients
== 0) {
5685 /* if no clients, we use a default config */
5686 net_clients
[nb_net_clients
++] = "nic";
5688 net_clients
[nb_net_clients
++] = "user";
5692 for(i
= 0;i
< nb_net_clients
; i
++) {
5693 if (net_client_parse(net_clients
[i
]) < 0)
5697 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5698 net_set_boot_mask(net_boot
);
5702 /* init the bluetooth world */
5703 if (foreach_device_config(DEV_BT
, bt_parse
))
5706 /* init the memory */
5708 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5710 /* init the dynamic translator */
5711 cpu_exec_init_all(tb_size
* 1024 * 1024);
5715 /* we always create the cdrom drive, even if no disk is there */
5716 drive_add(NULL
, CDROM_ALIAS
);
5718 /* we always create at least one floppy */
5719 drive_add(NULL
, FD_ALIAS
, 0);
5721 /* we always create one sd slot, even if no card is in it */
5722 drive_add(NULL
, SD_ALIAS
);
5724 /* open the virtual block devices */
5726 qemu_opts_foreach(&qemu_drive_opts
, drive_enable_snapshot
, NULL
, 0);
5727 if (qemu_opts_foreach(&qemu_drive_opts
, drive_init_func
, machine
, 1) != 0)
5730 vmstate_register(0, &vmstate_timers
,&timers_state
);
5731 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5733 /* Maintain compatibility with multiple stdio monitors */
5734 if (!strcmp(monitor_devices
[0],"stdio")) {
5735 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5736 const char *devname
= serial_devices
[i
];
5737 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5738 monitor_devices
[0] = NULL
;
5740 } else if (devname
&& !strcmp(devname
,"stdio")) {
5741 monitor_devices
[0] = NULL
;
5742 serial_devices
[i
] = "mon:stdio";
5748 if (nb_numa_nodes
> 0) {
5751 if (nb_numa_nodes
> smp_cpus
) {
5752 nb_numa_nodes
= smp_cpus
;
5755 /* If no memory size if given for any node, assume the default case
5756 * and distribute the available memory equally across all nodes
5758 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5759 if (node_mem
[i
] != 0)
5762 if (i
== nb_numa_nodes
) {
5763 uint64_t usedmem
= 0;
5765 /* On Linux, the each node's border has to be 8MB aligned,
5766 * the final node gets the rest.
5768 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5769 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5770 usedmem
+= node_mem
[i
];
5772 node_mem
[i
] = ram_size
- usedmem
;
5775 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5776 if (node_cpumask
[i
] != 0)
5779 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5780 * must cope with this anyway, because there are BIOSes out there in
5781 * real machines which also use this scheme.
5783 if (i
== nb_numa_nodes
) {
5784 for (i
= 0; i
< smp_cpus
; i
++) {
5785 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5790 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5791 const char *devname
= monitor_devices
[i
];
5792 if (devname
&& strcmp(devname
, "none")) {
5795 snprintf(label
, sizeof(label
), "monitor");
5797 snprintf(label
, sizeof(label
), "monitor%d", i
);
5799 monitor_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5800 if (!monitor_hds
[i
]) {
5801 fprintf(stderr
, "qemu: could not open monitor device '%s'\n",
5808 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5809 const char *devname
= serial_devices
[i
];
5810 if (devname
&& strcmp(devname
, "none")) {
5812 snprintf(label
, sizeof(label
), "serial%d", i
);
5813 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5814 if (!serial_hds
[i
]) {
5815 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5822 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5823 const char *devname
= parallel_devices
[i
];
5824 if (devname
&& strcmp(devname
, "none")) {
5826 snprintf(label
, sizeof(label
), "parallel%d", i
);
5827 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5828 if (!parallel_hds
[i
]) {
5829 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5836 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5837 const char *devname
= virtio_consoles
[i
];
5838 if (devname
&& strcmp(devname
, "none")) {
5840 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5841 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5842 if (!virtcon_hds
[i
]) {
5843 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5850 module_call_init(MODULE_INIT_DEVICE
);
5853 i
= select_watchdog(watchdog
);
5855 exit (i
== 1 ? 1 : 0);
5858 if (machine
->compat_props
) {
5859 qdev_prop_register_compat(machine
->compat_props
);
5861 machine
->init(ram_size
, boot_devices
,
5862 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5866 /* must be after terminal init, SDL library changes signal handlers */
5870 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5871 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5872 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5878 current_machine
= machine
;
5880 /* init USB devices */
5882 foreach_device_config(DEV_USB
, usb_parse
);
5885 /* init generic devices */
5886 if (qemu_opts_foreach(&qemu_device_opts
, device_init_func
, NULL
, 1) != 0)
5890 dumb_display_init();
5891 /* just use the first displaystate for the moment */
5894 if (display_type
== DT_DEFAULT
) {
5895 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5896 display_type
= DT_SDL
;
5898 display_type
= DT_VNC
;
5899 vnc_display
= "localhost:0,to=99";
5905 switch (display_type
) {
5908 #if defined(CONFIG_CURSES)
5910 curses_display_init(ds
, full_screen
);
5913 #if defined(CONFIG_SDL)
5915 sdl_display_init(ds
, full_screen
, no_frame
);
5917 #elif defined(CONFIG_COCOA)
5919 cocoa_display_init(ds
, full_screen
);
5923 vnc_display_init(ds
);
5924 if (vnc_display_open(ds
, vnc_display
) < 0)
5927 if (show_vnc_port
) {
5928 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
5936 dcl
= ds
->listeners
;
5937 while (dcl
!= NULL
) {
5938 if (dcl
->dpy_refresh
!= NULL
) {
5939 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
5940 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
5945 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
5946 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
5947 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
5950 text_consoles_set_display(display_state
);
5951 qemu_chr_initial_reset();
5953 for (i
= 0; i
< MAX_MONITOR_DEVICES
; i
++) {
5954 if (monitor_devices
[i
] && monitor_hds
[i
]) {
5955 monitor_init(monitor_hds
[i
],
5956 MONITOR_USE_READLINE
|
5957 ((i
== 0) ? MONITOR_IS_DEFAULT
: 0));
5961 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5962 const char *devname
= serial_devices
[i
];
5963 if (devname
&& strcmp(devname
, "none")) {
5964 if (strstart(devname
, "vc", 0))
5965 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
5969 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5970 const char *devname
= parallel_devices
[i
];
5971 if (devname
&& strcmp(devname
, "none")) {
5972 if (strstart(devname
, "vc", 0))
5973 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
5977 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5978 const char *devname
= virtio_consoles
[i
];
5979 if (virtcon_hds
[i
] && devname
) {
5980 if (strstart(devname
, "vc", 0))
5981 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
5985 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
5986 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
5992 if (load_vmstate(cur_mon
, loadvm
) < 0) {
5998 qemu_start_incoming_migration(incoming
);
5999 } else if (autostart
) {
6009 len
= write(fds
[1], &status
, 1);
6010 if (len
== -1 && (errno
== EINTR
))
6017 TFR(fd
= open("/dev/null", O_RDWR
));
6023 pwd
= getpwnam(run_as
);
6025 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6031 if (chroot(chroot_dir
) < 0) {
6032 fprintf(stderr
, "chroot failed\n");
6039 if (setgid(pwd
->pw_gid
) < 0) {
6040 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6043 if (setuid(pwd
->pw_uid
) < 0) {
6044 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6047 if (setuid(0) != -1) {
6048 fprintf(stderr
, "Dropping privileges failed\n");