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 HOST_BSD etc. */
33 #include "config-host.h"
34 /* Needed early to override system queue definitions on BSD */
35 #include "sys-queue.h"
40 #include <sys/times.h>
44 #include <sys/ioctl.h>
45 #include <sys/resource.h>
46 #include <sys/socket.h>
47 #include <netinet/in.h>
49 #if defined(__NetBSD__)
50 #include <net/if_tap.h>
53 #include <linux/if_tun.h>
55 #include <arpa/inet.h>
58 #include <sys/select.h>
61 #if defined(__FreeBSD__) || defined(__DragonFly__)
66 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
67 #include <freebsd/stdlib.h>
72 #include <linux/rtc.h>
73 #include <sys/prctl.h>
75 /* For the benefit of older linux systems which don't supply it,
76 we use a local copy of hpet.h. */
77 /* #include <linux/hpet.h> */
80 #include <linux/ppdev.h>
81 #include <linux/parport.h>
85 #include <sys/ethernet.h>
86 #include <sys/sockio.h>
87 #include <netinet/arp.h>
88 #include <netinet/in.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/ip.h>
91 #include <netinet/ip_icmp.h> // must come after ip.h
92 #include <netinet/udp.h>
93 #include <netinet/tcp.h>
101 #if defined(__OpenBSD__)
105 #if defined(CONFIG_VDE)
106 #include <libvdeplug.h>
112 #include <sys/timeb.h>
113 #include <mmsystem.h>
114 #define getopt_long_only getopt_long
115 #define memalign(align, size) malloc(size)
119 #if defined(__APPLE__) || defined(main)
121 int qemu_main(int argc
, char **argv
, char **envp
);
122 int main(int argc
, char **argv
)
124 return qemu_main(argc
, argv
, NULL
);
127 #define main qemu_main
129 #endif /* CONFIG_SDL */
133 #define main qemu_main
134 #endif /* CONFIG_COCOA */
137 #include "hw/boards.h"
139 #include "hw/pcmcia.h"
141 #include "hw/audiodev.h"
145 #include "hw/watchdog.h"
146 #include "hw/smbios.h"
155 #include "qemu-timer.h"
156 #include "qemu-char.h"
157 #include "cache-utils.h"
160 #include "audio/audio.h"
161 #include "migration.h"
164 #include "qemu-option.h"
168 #include "exec-all.h"
170 #include "qemu_socket.h"
172 #include "slirp/libslirp.h"
175 //#define DEBUG_SLIRP
177 #define DEFAULT_RAM_SIZE 128
179 static const char *data_dir
;
180 const char *bios_name
= NULL
;
181 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
182 to store the VM snapshots */
183 struct drivelist drives
= TAILQ_HEAD_INITIALIZER(drives
);
184 struct driveoptlist driveopts
= TAILQ_HEAD_INITIALIZER(driveopts
);
185 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
186 static DisplayState
*display_state
;
187 DisplayType display_type
= DT_DEFAULT
;
188 const char* keyboard_layout
= NULL
;
189 int64_t ticks_per_sec
;
192 NICInfo nd_table
[MAX_NICS
];
194 static int autostart
;
195 static int rtc_utc
= 1;
196 static int rtc_date_offset
= -1; /* -1 means no change */
197 int cirrus_vga_enabled
= 1;
198 int std_vga_enabled
= 0;
199 int vmsvga_enabled
= 0;
200 int xenfb_enabled
= 0;
202 int graphic_width
= 1024;
203 int graphic_height
= 768;
204 int graphic_depth
= 8;
206 int graphic_width
= 800;
207 int graphic_height
= 600;
208 int graphic_depth
= 15;
210 static int full_screen
= 0;
212 static int no_frame
= 0;
215 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
216 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
217 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
219 int win2k_install_hack
= 0;
226 const char *vnc_display
;
227 int acpi_enabled
= 1;
229 int virtio_balloon
= 1;
230 const char *virtio_balloon_devaddr
;
235 int graphic_rotate
= 0;
239 WatchdogTimerModel
*watchdog
= NULL
;
240 int watchdog_action
= WDT_RESET
;
241 const char *option_rom
[MAX_OPTION_ROMS
];
243 int semihosting_enabled
= 0;
247 const char *qemu_name
;
249 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
250 unsigned int nb_prom_envs
= 0;
251 const char *prom_envs
[MAX_PROM_ENVS
];
256 uint64_t node_mem
[MAX_NODES
];
257 uint64_t node_cpumask
[MAX_NODES
];
259 static CPUState
*cur_cpu
;
260 static CPUState
*next_cpu
;
261 static int timer_alarm_pending
= 1;
262 /* Conversion factor from emulated instructions to virtual clock ticks. */
263 static int icount_time_shift
;
264 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
265 #define MAX_ICOUNT_SHIFT 10
266 /* Compensate for varying guest execution speed. */
267 static int64_t qemu_icount_bias
;
268 static QEMUTimer
*icount_rt_timer
;
269 static QEMUTimer
*icount_vm_timer
;
270 static QEMUTimer
*nographic_timer
;
272 uint8_t qemu_uuid
[16];
274 static QEMUBootSetHandler
*boot_set_handler
;
275 static void *boot_set_opaque
;
277 /***********************************************************/
278 /* x86 ISA bus support */
280 target_phys_addr_t isa_mem_base
= 0;
283 /***********************************************************/
284 void hw_error(const char *fmt
, ...)
290 fprintf(stderr
, "qemu: hardware error: ");
291 vfprintf(stderr
, fmt
, ap
);
292 fprintf(stderr
, "\n");
293 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
294 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
296 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
298 cpu_dump_state(env
, stderr
, fprintf
, 0);
305 static void set_proc_name(const char *s
)
311 name
[sizeof(name
) - 1] = 0;
312 strncpy(name
, s
, sizeof(name
));
313 /* Could rewrite argv[0] too, but that's a bit more complicated.
314 This simple way is enough for `top'. */
315 prctl(PR_SET_NAME
, name
);
322 static QEMUBalloonEvent
*qemu_balloon_event
;
323 void *qemu_balloon_event_opaque
;
325 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
327 qemu_balloon_event
= func
;
328 qemu_balloon_event_opaque
= opaque
;
331 void qemu_balloon(ram_addr_t target
)
333 if (qemu_balloon_event
)
334 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
337 ram_addr_t
qemu_balloon_status(void)
339 if (qemu_balloon_event
)
340 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
344 /***********************************************************/
347 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
348 static void *qemu_put_kbd_event_opaque
;
349 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
350 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
352 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
354 qemu_put_kbd_event_opaque
= opaque
;
355 qemu_put_kbd_event
= func
;
358 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
359 void *opaque
, int absolute
,
362 QEMUPutMouseEntry
*s
, *cursor
;
364 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
366 s
->qemu_put_mouse_event
= func
;
367 s
->qemu_put_mouse_event_opaque
= opaque
;
368 s
->qemu_put_mouse_event_absolute
= absolute
;
369 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
372 if (!qemu_put_mouse_event_head
) {
373 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
377 cursor
= qemu_put_mouse_event_head
;
378 while (cursor
->next
!= NULL
)
379 cursor
= cursor
->next
;
382 qemu_put_mouse_event_current
= s
;
387 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
389 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
391 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
394 cursor
= qemu_put_mouse_event_head
;
395 while (cursor
!= NULL
&& cursor
!= entry
) {
397 cursor
= cursor
->next
;
400 if (cursor
== NULL
) // does not exist or list empty
402 else if (prev
== NULL
) { // entry is head
403 qemu_put_mouse_event_head
= cursor
->next
;
404 if (qemu_put_mouse_event_current
== entry
)
405 qemu_put_mouse_event_current
= cursor
->next
;
406 qemu_free(entry
->qemu_put_mouse_event_name
);
411 prev
->next
= entry
->next
;
413 if (qemu_put_mouse_event_current
== entry
)
414 qemu_put_mouse_event_current
= prev
;
416 qemu_free(entry
->qemu_put_mouse_event_name
);
420 void kbd_put_keycode(int keycode
)
422 if (qemu_put_kbd_event
) {
423 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
427 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
429 QEMUPutMouseEvent
*mouse_event
;
430 void *mouse_event_opaque
;
433 if (!qemu_put_mouse_event_current
) {
438 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
440 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
443 if (graphic_rotate
) {
444 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
447 width
= graphic_width
- 1;
448 mouse_event(mouse_event_opaque
,
449 width
- dy
, dx
, dz
, buttons_state
);
451 mouse_event(mouse_event_opaque
,
452 dx
, dy
, dz
, buttons_state
);
456 int kbd_mouse_is_absolute(void)
458 if (!qemu_put_mouse_event_current
)
461 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
464 void do_info_mice(Monitor
*mon
)
466 QEMUPutMouseEntry
*cursor
;
469 if (!qemu_put_mouse_event_head
) {
470 monitor_printf(mon
, "No mouse devices connected\n");
474 monitor_printf(mon
, "Mouse devices available:\n");
475 cursor
= qemu_put_mouse_event_head
;
476 while (cursor
!= NULL
) {
477 monitor_printf(mon
, "%c Mouse #%d: %s\n",
478 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
479 index
, cursor
->qemu_put_mouse_event_name
);
481 cursor
= cursor
->next
;
485 void do_mouse_set(Monitor
*mon
, int index
)
487 QEMUPutMouseEntry
*cursor
;
490 if (!qemu_put_mouse_event_head
) {
491 monitor_printf(mon
, "No mouse devices connected\n");
495 cursor
= qemu_put_mouse_event_head
;
496 while (cursor
!= NULL
&& index
!= i
) {
498 cursor
= cursor
->next
;
502 qemu_put_mouse_event_current
= cursor
;
504 monitor_printf(mon
, "Mouse at given index not found\n");
507 /* compute with 96 bit intermediate result: (a*b)/c */
508 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
513 #ifdef WORDS_BIGENDIAN
523 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
524 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
527 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
531 /***********************************************************/
532 /* real time host monotonic timer */
534 #define QEMU_TIMER_BASE 1000000000LL
538 static int64_t clock_freq
;
540 static void init_get_clock(void)
544 ret
= QueryPerformanceFrequency(&freq
);
546 fprintf(stderr
, "Could not calibrate ticks\n");
549 clock_freq
= freq
.QuadPart
;
552 static int64_t get_clock(void)
555 QueryPerformanceCounter(&ti
);
556 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
561 static int use_rt_clock
;
563 static void init_get_clock(void)
566 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
567 || defined(__DragonFly__)
570 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
577 static int64_t get_clock(void)
579 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
580 || defined(__DragonFly__)
583 clock_gettime(CLOCK_MONOTONIC
, &ts
);
584 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
588 /* XXX: using gettimeofday leads to problems if the date
589 changes, so it should be avoided. */
591 gettimeofday(&tv
, NULL
);
592 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
597 /* Return the virtual CPU time, based on the instruction counter. */
598 static int64_t cpu_get_icount(void)
601 CPUState
*env
= cpu_single_env
;;
602 icount
= qemu_icount
;
605 fprintf(stderr
, "Bad clock read\n");
606 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
608 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
611 /***********************************************************/
612 /* guest cycle counter */
614 static int64_t cpu_ticks_prev
;
615 static int64_t cpu_ticks_offset
;
616 static int64_t cpu_clock_offset
;
617 static int cpu_ticks_enabled
;
619 /* return the host CPU cycle counter and handle stop/restart */
620 int64_t cpu_get_ticks(void)
623 return cpu_get_icount();
625 if (!cpu_ticks_enabled
) {
626 return cpu_ticks_offset
;
629 ticks
= cpu_get_real_ticks();
630 if (cpu_ticks_prev
> ticks
) {
631 /* Note: non increasing ticks may happen if the host uses
633 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
635 cpu_ticks_prev
= ticks
;
636 return ticks
+ cpu_ticks_offset
;
640 /* return the host CPU monotonic timer and handle stop/restart */
641 static int64_t cpu_get_clock(void)
644 if (!cpu_ticks_enabled
) {
645 return cpu_clock_offset
;
648 return ti
+ cpu_clock_offset
;
652 /* enable cpu_get_ticks() */
653 void cpu_enable_ticks(void)
655 if (!cpu_ticks_enabled
) {
656 cpu_ticks_offset
-= cpu_get_real_ticks();
657 cpu_clock_offset
-= get_clock();
658 cpu_ticks_enabled
= 1;
662 /* disable cpu_get_ticks() : the clock is stopped. You must not call
663 cpu_get_ticks() after that. */
664 void cpu_disable_ticks(void)
666 if (cpu_ticks_enabled
) {
667 cpu_ticks_offset
= cpu_get_ticks();
668 cpu_clock_offset
= cpu_get_clock();
669 cpu_ticks_enabled
= 0;
673 /***********************************************************/
676 #define QEMU_TIMER_REALTIME 0
677 #define QEMU_TIMER_VIRTUAL 1
681 /* XXX: add frequency */
689 struct QEMUTimer
*next
;
692 struct qemu_alarm_timer
{
696 int (*start
)(struct qemu_alarm_timer
*t
);
697 void (*stop
)(struct qemu_alarm_timer
*t
);
698 void (*rearm
)(struct qemu_alarm_timer
*t
);
702 #define ALARM_FLAG_DYNTICKS 0x1
703 #define ALARM_FLAG_EXPIRED 0x2
705 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
707 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
710 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
712 if (!alarm_has_dynticks(t
))
718 /* TODO: MIN_TIMER_REARM_US should be optimized */
719 #define MIN_TIMER_REARM_US 250
721 static struct qemu_alarm_timer
*alarm_timer
;
725 struct qemu_alarm_win32
{
728 } alarm_win32_data
= {0, -1};
730 static int win32_start_timer(struct qemu_alarm_timer
*t
);
731 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
732 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
736 static int unix_start_timer(struct qemu_alarm_timer
*t
);
737 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
741 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
742 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
743 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
745 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
746 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
748 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
749 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
751 #endif /* __linux__ */
755 /* Correlation between real and virtual time is always going to be
756 fairly approximate, so ignore small variation.
757 When the guest is idle real and virtual time will be aligned in
759 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
761 static void icount_adjust(void)
766 static int64_t last_delta
;
767 /* If the VM is not running, then do nothing. */
771 cur_time
= cpu_get_clock();
772 cur_icount
= qemu_get_clock(vm_clock
);
773 delta
= cur_icount
- cur_time
;
774 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
776 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
777 && icount_time_shift
> 0) {
778 /* The guest is getting too far ahead. Slow time down. */
782 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
783 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
784 /* The guest is getting too far behind. Speed time up. */
788 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
791 static void icount_adjust_rt(void * opaque
)
793 qemu_mod_timer(icount_rt_timer
,
794 qemu_get_clock(rt_clock
) + 1000);
798 static void icount_adjust_vm(void * opaque
)
800 qemu_mod_timer(icount_vm_timer
,
801 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
805 static void init_icount_adjust(void)
807 /* Have both realtime and virtual time triggers for speed adjustment.
808 The realtime trigger catches emulated time passing too slowly,
809 the virtual time trigger catches emulated time passing too fast.
810 Realtime triggers occur even when idle, so use them less frequently
812 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
813 qemu_mod_timer(icount_rt_timer
,
814 qemu_get_clock(rt_clock
) + 1000);
815 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
816 qemu_mod_timer(icount_vm_timer
,
817 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
820 static struct qemu_alarm_timer alarm_timers
[] = {
823 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
824 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
825 /* HPET - if available - is preferred */
826 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
827 /* ...otherwise try RTC */
828 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
830 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
832 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
833 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
834 {"win32", 0, win32_start_timer
,
835 win32_stop_timer
, NULL
, &alarm_win32_data
},
840 static void show_available_alarms(void)
844 printf("Available alarm timers, in order of precedence:\n");
845 for (i
= 0; alarm_timers
[i
].name
; i
++)
846 printf("%s\n", alarm_timers
[i
].name
);
849 static void configure_alarms(char const *opt
)
853 int count
= ARRAY_SIZE(alarm_timers
) - 1;
856 struct qemu_alarm_timer tmp
;
858 if (!strcmp(opt
, "?")) {
859 show_available_alarms();
865 /* Reorder the array */
866 name
= strtok(arg
, ",");
868 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
869 if (!strcmp(alarm_timers
[i
].name
, name
))
874 fprintf(stderr
, "Unknown clock %s\n", name
);
883 tmp
= alarm_timers
[i
];
884 alarm_timers
[i
] = alarm_timers
[cur
];
885 alarm_timers
[cur
] = tmp
;
889 name
= strtok(NULL
, ",");
895 /* Disable remaining timers */
896 for (i
= cur
; i
< count
; i
++)
897 alarm_timers
[i
].name
= NULL
;
899 show_available_alarms();
907 static QEMUTimer
*active_timers
[2];
909 static QEMUClock
*qemu_new_clock(int type
)
912 clock
= qemu_mallocz(sizeof(QEMUClock
));
917 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
921 ts
= qemu_mallocz(sizeof(QEMUTimer
));
928 void qemu_free_timer(QEMUTimer
*ts
)
933 /* stop a timer, but do not dealloc it */
934 void qemu_del_timer(QEMUTimer
*ts
)
938 /* NOTE: this code must be signal safe because
939 qemu_timer_expired() can be called from a signal. */
940 pt
= &active_timers
[ts
->clock
->type
];
953 /* modify the current timer so that it will be fired when current_time
954 >= expire_time. The corresponding callback will be called. */
955 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
961 /* add the timer in the sorted list */
962 /* NOTE: this code must be signal safe because
963 qemu_timer_expired() can be called from a signal. */
964 pt
= &active_timers
[ts
->clock
->type
];
969 if (t
->expire_time
> expire_time
)
973 ts
->expire_time
= expire_time
;
977 /* Rearm if necessary */
978 if (pt
== &active_timers
[ts
->clock
->type
]) {
979 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
980 qemu_rearm_alarm_timer(alarm_timer
);
982 /* Interrupt execution to force deadline recalculation. */
988 int qemu_timer_pending(QEMUTimer
*ts
)
991 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
998 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1002 return (timer_head
->expire_time
<= current_time
);
1005 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1011 if (!ts
|| ts
->expire_time
> current_time
)
1013 /* remove timer from the list before calling the callback */
1014 *ptimer_head
= ts
->next
;
1017 /* run the callback (the timer list can be modified) */
1022 int64_t qemu_get_clock(QEMUClock
*clock
)
1024 switch(clock
->type
) {
1025 case QEMU_TIMER_REALTIME
:
1026 return get_clock() / 1000000;
1028 case QEMU_TIMER_VIRTUAL
:
1030 return cpu_get_icount();
1032 return cpu_get_clock();
1037 static void init_timers(void)
1040 ticks_per_sec
= QEMU_TIMER_BASE
;
1041 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1042 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1046 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1048 uint64_t expire_time
;
1050 if (qemu_timer_pending(ts
)) {
1051 expire_time
= ts
->expire_time
;
1055 qemu_put_be64(f
, expire_time
);
1058 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1060 uint64_t expire_time
;
1062 expire_time
= qemu_get_be64(f
);
1063 if (expire_time
!= -1) {
1064 qemu_mod_timer(ts
, expire_time
);
1070 static void timer_save(QEMUFile
*f
, void *opaque
)
1072 if (cpu_ticks_enabled
) {
1073 hw_error("cannot save state if virtual timers are running");
1075 qemu_put_be64(f
, cpu_ticks_offset
);
1076 qemu_put_be64(f
, ticks_per_sec
);
1077 qemu_put_be64(f
, cpu_clock_offset
);
1080 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1082 if (version_id
!= 1 && version_id
!= 2)
1084 if (cpu_ticks_enabled
) {
1087 cpu_ticks_offset
=qemu_get_be64(f
);
1088 ticks_per_sec
=qemu_get_be64(f
);
1089 if (version_id
== 2) {
1090 cpu_clock_offset
=qemu_get_be64(f
);
1095 static void qemu_event_increment(void);
1098 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1099 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1102 static void host_alarm_handler(int host_signum
)
1106 #define DISP_FREQ 1000
1108 static int64_t delta_min
= INT64_MAX
;
1109 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1111 ti
= qemu_get_clock(vm_clock
);
1112 if (last_clock
!= 0) {
1113 delta
= ti
- last_clock
;
1114 if (delta
< delta_min
)
1116 if (delta
> delta_max
)
1119 if (++count
== DISP_FREQ
) {
1120 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1121 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1122 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1123 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1124 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1126 delta_min
= INT64_MAX
;
1134 if (alarm_has_dynticks(alarm_timer
) ||
1136 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1137 qemu_get_clock(vm_clock
))) ||
1138 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1139 qemu_get_clock(rt_clock
))) {
1140 qemu_event_increment();
1141 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1143 #ifndef CONFIG_IOTHREAD
1145 /* stop the currently executing cpu because a timer occured */
1148 if (next_cpu
->kqemu_enabled
) {
1149 kqemu_cpu_interrupt(next_cpu
);
1154 timer_alarm_pending
= 1;
1155 qemu_notify_event();
1159 static int64_t qemu_next_deadline(void)
1163 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1164 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1165 qemu_get_clock(vm_clock
);
1167 /* To avoid problems with overflow limit this to 2^32. */
1177 #if defined(__linux__) || defined(_WIN32)
1178 static uint64_t qemu_next_deadline_dyntick(void)
1186 delta
= (qemu_next_deadline() + 999) / 1000;
1188 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1189 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1190 qemu_get_clock(rt_clock
))*1000;
1191 if (rtdelta
< delta
)
1195 if (delta
< MIN_TIMER_REARM_US
)
1196 delta
= MIN_TIMER_REARM_US
;
1204 /* Sets a specific flag */
1205 static int fcntl_setfl(int fd
, int flag
)
1209 flags
= fcntl(fd
, F_GETFL
);
1213 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1219 #if defined(__linux__)
1221 #define RTC_FREQ 1024
1223 static void enable_sigio_timer(int fd
)
1225 struct sigaction act
;
1228 sigfillset(&act
.sa_mask
);
1230 act
.sa_handler
= host_alarm_handler
;
1232 sigaction(SIGIO
, &act
, NULL
);
1233 fcntl_setfl(fd
, O_ASYNC
);
1234 fcntl(fd
, F_SETOWN
, getpid());
1237 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1239 struct hpet_info info
;
1242 fd
= open("/dev/hpet", O_RDONLY
);
1247 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1249 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1250 "error, but for better emulation accuracy type:\n"
1251 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1255 /* Check capabilities */
1256 r
= ioctl(fd
, HPET_INFO
, &info
);
1260 /* Enable periodic mode */
1261 r
= ioctl(fd
, HPET_EPI
, 0);
1262 if (info
.hi_flags
&& (r
< 0))
1265 /* Enable interrupt */
1266 r
= ioctl(fd
, HPET_IE_ON
, 0);
1270 enable_sigio_timer(fd
);
1271 t
->priv
= (void *)(long)fd
;
1279 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1281 int fd
= (long)t
->priv
;
1286 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1289 unsigned long current_rtc_freq
= 0;
1291 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1294 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1295 if (current_rtc_freq
!= RTC_FREQ
&&
1296 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1297 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1298 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1299 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1302 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1308 enable_sigio_timer(rtc_fd
);
1310 t
->priv
= (void *)(long)rtc_fd
;
1315 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1317 int rtc_fd
= (long)t
->priv
;
1322 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1326 struct sigaction act
;
1328 sigfillset(&act
.sa_mask
);
1330 act
.sa_handler
= host_alarm_handler
;
1332 sigaction(SIGALRM
, &act
, NULL
);
1335 * Initialize ev struct to 0 to avoid valgrind complaining
1336 * about uninitialized data in timer_create call
1338 memset(&ev
, 0, sizeof(ev
));
1339 ev
.sigev_value
.sival_int
= 0;
1340 ev
.sigev_notify
= SIGEV_SIGNAL
;
1341 ev
.sigev_signo
= SIGALRM
;
1343 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1344 perror("timer_create");
1346 /* disable dynticks */
1347 fprintf(stderr
, "Dynamic Ticks disabled\n");
1352 t
->priv
= (void *)(long)host_timer
;
1357 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1359 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1361 timer_delete(host_timer
);
1364 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1366 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1367 struct itimerspec timeout
;
1368 int64_t nearest_delta_us
= INT64_MAX
;
1371 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1372 !active_timers
[QEMU_TIMER_VIRTUAL
])
1375 nearest_delta_us
= qemu_next_deadline_dyntick();
1377 /* check whether a timer is already running */
1378 if (timer_gettime(host_timer
, &timeout
)) {
1380 fprintf(stderr
, "Internal timer error: aborting\n");
1383 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1384 if (current_us
&& current_us
<= nearest_delta_us
)
1387 timeout
.it_interval
.tv_sec
= 0;
1388 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1389 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1390 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1391 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1393 fprintf(stderr
, "Internal timer error: aborting\n");
1398 #endif /* defined(__linux__) */
1400 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1402 struct sigaction act
;
1403 struct itimerval itv
;
1407 sigfillset(&act
.sa_mask
);
1409 act
.sa_handler
= host_alarm_handler
;
1411 sigaction(SIGALRM
, &act
, NULL
);
1413 itv
.it_interval
.tv_sec
= 0;
1414 /* for i386 kernel 2.6 to get 1 ms */
1415 itv
.it_interval
.tv_usec
= 999;
1416 itv
.it_value
.tv_sec
= 0;
1417 itv
.it_value
.tv_usec
= 10 * 1000;
1419 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1426 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1428 struct itimerval itv
;
1430 memset(&itv
, 0, sizeof(itv
));
1431 setitimer(ITIMER_REAL
, &itv
, NULL
);
1434 #endif /* !defined(_WIN32) */
1439 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1442 struct qemu_alarm_win32
*data
= t
->priv
;
1445 memset(&tc
, 0, sizeof(tc
));
1446 timeGetDevCaps(&tc
, sizeof(tc
));
1448 if (data
->period
< tc
.wPeriodMin
)
1449 data
->period
= tc
.wPeriodMin
;
1451 timeBeginPeriod(data
->period
);
1453 flags
= TIME_CALLBACK_FUNCTION
;
1454 if (alarm_has_dynticks(t
))
1455 flags
|= TIME_ONESHOT
;
1457 flags
|= TIME_PERIODIC
;
1459 data
->timerId
= timeSetEvent(1, // interval (ms)
1460 data
->period
, // resolution
1461 host_alarm_handler
, // function
1462 (DWORD
)t
, // parameter
1465 if (!data
->timerId
) {
1466 perror("Failed to initialize win32 alarm timer");
1467 timeEndPeriod(data
->period
);
1474 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1476 struct qemu_alarm_win32
*data
= t
->priv
;
1478 timeKillEvent(data
->timerId
);
1479 timeEndPeriod(data
->period
);
1482 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1484 struct qemu_alarm_win32
*data
= t
->priv
;
1485 uint64_t nearest_delta_us
;
1487 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1488 !active_timers
[QEMU_TIMER_VIRTUAL
])
1491 nearest_delta_us
= qemu_next_deadline_dyntick();
1492 nearest_delta_us
/= 1000;
1494 timeKillEvent(data
->timerId
);
1496 data
->timerId
= timeSetEvent(1,
1500 TIME_ONESHOT
| TIME_PERIODIC
);
1502 if (!data
->timerId
) {
1503 perror("Failed to re-arm win32 alarm timer");
1505 timeEndPeriod(data
->period
);
1512 static int init_timer_alarm(void)
1514 struct qemu_alarm_timer
*t
= NULL
;
1517 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1518 t
= &alarm_timers
[i
];
1538 static void quit_timers(void)
1540 alarm_timer
->stop(alarm_timer
);
1544 /***********************************************************/
1545 /* host time/date access */
1546 void qemu_get_timedate(struct tm
*tm
, int offset
)
1553 if (rtc_date_offset
== -1) {
1557 ret
= localtime(&ti
);
1559 ti
-= rtc_date_offset
;
1563 memcpy(tm
, ret
, sizeof(struct tm
));
1566 int qemu_timedate_diff(struct tm
*tm
)
1570 if (rtc_date_offset
== -1)
1572 seconds
= mktimegm(tm
);
1574 seconds
= mktime(tm
);
1576 seconds
= mktimegm(tm
) + rtc_date_offset
;
1578 return seconds
- time(NULL
);
1582 static void socket_cleanup(void)
1587 static int socket_init(void)
1592 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1594 err
= WSAGetLastError();
1595 fprintf(stderr
, "WSAStartup: %d\n", err
);
1598 atexit(socket_cleanup
);
1603 /***********************************************************/
1604 /* Bluetooth support */
1607 static struct HCIInfo
*hci_table
[MAX_NICS
];
1609 static struct bt_vlan_s
{
1610 struct bt_scatternet_s net
;
1612 struct bt_vlan_s
*next
;
1615 /* find or alloc a new bluetooth "VLAN" */
1616 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1618 struct bt_vlan_s
**pvlan
, *vlan
;
1619 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1623 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1625 pvlan
= &first_bt_vlan
;
1626 while (*pvlan
!= NULL
)
1627 pvlan
= &(*pvlan
)->next
;
1632 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1636 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1641 static struct HCIInfo null_hci
= {
1642 .cmd_send
= null_hci_send
,
1643 .sco_send
= null_hci_send
,
1644 .acl_send
= null_hci_send
,
1645 .bdaddr_set
= null_hci_addr_set
,
1648 struct HCIInfo
*qemu_next_hci(void)
1650 if (cur_hci
== nb_hcis
)
1653 return hci_table
[cur_hci
++];
1656 static struct HCIInfo
*hci_init(const char *str
)
1659 struct bt_scatternet_s
*vlan
= 0;
1661 if (!strcmp(str
, "null"))
1664 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1666 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1667 else if (!strncmp(str
, "hci", 3)) {
1670 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1671 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1676 vlan
= qemu_find_bt_vlan(0);
1678 return bt_new_hci(vlan
);
1681 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1686 static int bt_hci_parse(const char *str
)
1688 struct HCIInfo
*hci
;
1691 if (nb_hcis
>= MAX_NICS
) {
1692 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1696 hci
= hci_init(str
);
1705 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1706 hci
->bdaddr_set(hci
, bdaddr
.b
);
1708 hci_table
[nb_hcis
++] = hci
;
1713 static void bt_vhci_add(int vlan_id
)
1715 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1718 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1719 "an empty scatternet %i\n", vlan_id
);
1721 bt_vhci_init(bt_new_hci(vlan
));
1724 static struct bt_device_s
*bt_device_add(const char *opt
)
1726 struct bt_scatternet_s
*vlan
;
1728 char *endp
= strstr(opt
, ",vlan=");
1729 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1732 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1735 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1737 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1742 vlan
= qemu_find_bt_vlan(vlan_id
);
1745 fprintf(stderr
, "qemu: warning: adding a slave device to "
1746 "an empty scatternet %i\n", vlan_id
);
1748 if (!strcmp(devname
, "keyboard"))
1749 return bt_keyboard_init(vlan
);
1751 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1755 static int bt_parse(const char *opt
)
1757 const char *endp
, *p
;
1760 if (strstart(opt
, "hci", &endp
)) {
1761 if (!*endp
|| *endp
== ',') {
1763 if (!strstart(endp
, ",vlan=", 0))
1766 return bt_hci_parse(opt
);
1768 } else if (strstart(opt
, "vhci", &endp
)) {
1769 if (!*endp
|| *endp
== ',') {
1771 if (strstart(endp
, ",vlan=", &p
)) {
1772 vlan
= strtol(p
, (char **) &endp
, 0);
1774 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1778 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1787 } else if (strstart(opt
, "device:", &endp
))
1788 return !bt_device_add(endp
);
1790 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1794 /***********************************************************/
1795 /* QEMU Block devices */
1797 #define HD_ALIAS "index=%d,media=disk"
1798 #define CDROM_ALIAS "index=2,media=cdrom"
1799 #define FD_ALIAS "index=%d,if=floppy"
1800 #define PFLASH_ALIAS "if=pflash"
1801 #define MTD_ALIAS "if=mtd"
1802 #define SD_ALIAS "index=0,if=sd"
1804 static QemuOptsList drive_opt_list
= {
1806 .head
= TAILQ_HEAD_INITIALIZER(drive_opt_list
.head
),
1810 .type
= QEMU_OPT_NUMBER
,
1811 .help
= "bus number",
1814 .type
= QEMU_OPT_NUMBER
,
1815 .help
= "unit number (i.e. lun for scsi)",
1818 .type
= QEMU_OPT_STRING
,
1819 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1822 .type
= QEMU_OPT_NUMBER
,
1825 .type
= QEMU_OPT_NUMBER
,
1826 .help
= "number of cylinders (ide disk geometry)",
1829 .type
= QEMU_OPT_NUMBER
,
1830 .help
= "number of heads (ide disk geometry)",
1833 .type
= QEMU_OPT_NUMBER
,
1834 .help
= "number of sectors (ide disk geometry)",
1837 .type
= QEMU_OPT_STRING
,
1838 .help
= "chs translation (auto, lba. none)",
1841 .type
= QEMU_OPT_STRING
,
1842 .help
= "media type (disk, cdrom)",
1845 .type
= QEMU_OPT_BOOL
,
1848 .type
= QEMU_OPT_STRING
,
1849 .help
= "disk image",
1852 .type
= QEMU_OPT_STRING
,
1853 .help
= "host cache usage (none, writeback, writethrough)",
1856 .type
= QEMU_OPT_STRING
,
1857 .help
= "disk format (raw, qcow2, ...)",
1860 .type
= QEMU_OPT_STRING
,
1863 .type
= QEMU_OPT_STRING
,
1866 .type
= QEMU_OPT_STRING
,
1867 .help
= "pci address (virtio only)",
1869 { /* end if list */ }
1873 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1880 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1883 opts
= qemu_opts_parse(&drive_opt_list
, optstr
, NULL
);
1885 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1886 __FUNCTION__
, optstr
);
1890 qemu_opt_set(opts
, "file", file
);
1894 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1898 /* seek interface, bus and unit */
1900 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1901 if (dinfo
->type
== type
&&
1902 dinfo
->bus
== bus
&&
1903 dinfo
->unit
== unit
)
1910 DriveInfo
*drive_get_by_id(char *id
)
1914 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1915 if (strcmp(id
, dinfo
->id
))
1922 int drive_get_max_bus(BlockInterfaceType type
)
1928 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1929 if(dinfo
->type
== type
&&
1930 dinfo
->bus
> max_bus
)
1931 max_bus
= dinfo
->bus
;
1936 const char *drive_get_serial(BlockDriverState
*bdrv
)
1940 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1941 if (dinfo
->bdrv
== bdrv
)
1942 return dinfo
->serial
;
1948 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1952 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1953 if (dinfo
->bdrv
== bdrv
)
1954 return dinfo
->onerror
;
1957 return BLOCK_ERR_STOP_ENOSPC
;
1960 static void bdrv_format_print(void *opaque
, const char *name
)
1962 fprintf(stderr
, " %s", name
);
1965 void drive_uninit(BlockDriverState
*bdrv
)
1969 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1970 if (dinfo
->bdrv
!= bdrv
)
1972 qemu_opts_del(dinfo
->opts
);
1973 TAILQ_REMOVE(&drives
, dinfo
, next
);
1979 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1983 const char *file
= NULL
;
1986 const char *mediastr
= "";
1987 BlockInterfaceType type
;
1988 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1989 int bus_id
, unit_id
;
1990 int cyls
, heads
, secs
, translation
;
1991 BlockDriver
*drv
= NULL
;
1992 QEMUMachine
*machine
= opaque
;
1996 int bdrv_flags
, onerror
;
1997 const char *devaddr
;
2003 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2006 if (machine
->use_scsi
) {
2008 max_devs
= MAX_SCSI_DEVS
;
2009 pstrcpy(devname
, sizeof(devname
), "scsi");
2012 max_devs
= MAX_IDE_DEVS
;
2013 pstrcpy(devname
, sizeof(devname
), "ide");
2017 /* extract parameters */
2018 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2019 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2020 index
= qemu_opt_get_number(opts
, "index", -1);
2022 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2023 heads
= qemu_opt_get_number(opts
, "heads", 0);
2024 secs
= qemu_opt_get_number(opts
, "secs", 0);
2026 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2028 file
= qemu_opt_get(opts
, "file");
2029 serial
= qemu_opt_get(opts
, "serial");
2031 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2032 pstrcpy(devname
, sizeof(devname
), buf
);
2033 if (!strcmp(buf
, "ide")) {
2035 max_devs
= MAX_IDE_DEVS
;
2036 } else if (!strcmp(buf
, "scsi")) {
2038 max_devs
= MAX_SCSI_DEVS
;
2039 } else if (!strcmp(buf
, "floppy")) {
2042 } else if (!strcmp(buf
, "pflash")) {
2045 } else if (!strcmp(buf
, "mtd")) {
2048 } else if (!strcmp(buf
, "sd")) {
2051 } else if (!strcmp(buf
, "virtio")) {
2054 } else if (!strcmp(buf
, "xen")) {
2058 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2063 if (cyls
|| heads
|| secs
) {
2064 if (cyls
< 1 || cyls
> 16383) {
2065 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2068 if (heads
< 1 || heads
> 16) {
2069 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2072 if (secs
< 1 || secs
> 63) {
2073 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2078 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2081 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2085 if (!strcmp(buf
, "none"))
2086 translation
= BIOS_ATA_TRANSLATION_NONE
;
2087 else if (!strcmp(buf
, "lba"))
2088 translation
= BIOS_ATA_TRANSLATION_LBA
;
2089 else if (!strcmp(buf
, "auto"))
2090 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2092 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2097 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2098 if (!strcmp(buf
, "disk")) {
2100 } else if (!strcmp(buf
, "cdrom")) {
2101 if (cyls
|| secs
|| heads
) {
2103 "qemu: '%s' invalid physical CHS format\n", buf
);
2106 media
= MEDIA_CDROM
;
2108 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2113 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2114 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2116 else if (!strcmp(buf
, "writethrough"))
2118 else if (!strcmp(buf
, "writeback"))
2121 fprintf(stderr
, "qemu: invalid cache option\n");
2126 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2127 if (strcmp(buf
, "?") == 0) {
2128 fprintf(stderr
, "qemu: Supported formats:");
2129 bdrv_iterate_format(bdrv_format_print
, NULL
);
2130 fprintf(stderr
, "\n");
2133 drv
= bdrv_find_format(buf
);
2135 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2140 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2141 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2142 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2143 fprintf(stderr
, "werror is no supported by this format\n");
2146 if (!strcmp(buf
, "ignore"))
2147 onerror
= BLOCK_ERR_IGNORE
;
2148 else if (!strcmp(buf
, "enospc"))
2149 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2150 else if (!strcmp(buf
, "stop"))
2151 onerror
= BLOCK_ERR_STOP_ANY
;
2152 else if (!strcmp(buf
, "report"))
2153 onerror
= BLOCK_ERR_REPORT
;
2155 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2160 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2161 if (type
!= IF_VIRTIO
) {
2162 fprintf(stderr
, "addr is not supported\n");
2167 /* compute bus and unit according index */
2170 if (bus_id
!= 0 || unit_id
!= -1) {
2172 "qemu: index cannot be used with bus and unit\n");
2180 unit_id
= index
% max_devs
;
2181 bus_id
= index
/ max_devs
;
2185 /* if user doesn't specify a unit_id,
2186 * try to find the first free
2189 if (unit_id
== -1) {
2191 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2193 if (max_devs
&& unit_id
>= max_devs
) {
2194 unit_id
-= max_devs
;
2202 if (max_devs
&& unit_id
>= max_devs
) {
2203 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2204 unit_id
, max_devs
- 1);
2209 * ignore multiple definitions
2212 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2219 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2220 if ((buf
= qemu_opt_get(opts
, "id")) != NULL
) {
2221 dinfo
->id
= qemu_strdup(buf
);
2223 /* no id supplied -> create one */
2224 dinfo
->id
= qemu_mallocz(32);
2225 if (type
== IF_IDE
|| type
== IF_SCSI
)
2226 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2228 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2229 devname
, bus_id
, mediastr
, unit_id
);
2231 snprintf(dinfo
->id
, 32, "%s%s%i",
2232 devname
, mediastr
, unit_id
);
2234 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2235 dinfo
->devaddr
= devaddr
;
2237 dinfo
->bus
= bus_id
;
2238 dinfo
->unit
= unit_id
;
2239 dinfo
->onerror
= onerror
;
2242 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2243 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2252 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2253 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2257 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2262 /* FIXME: This isn't really a floppy, but it's a reasonable
2265 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2280 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2281 cache
= 2; /* always use write-back with snapshot */
2283 if (cache
== 0) /* no caching */
2284 bdrv_flags
|= BDRV_O_NOCACHE
;
2285 else if (cache
== 2) /* write-back */
2286 bdrv_flags
|= BDRV_O_CACHE_WB
;
2287 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2288 fprintf(stderr
, "qemu: could not open disk image %s\n",
2292 if (bdrv_key_required(dinfo
->bdrv
))
2298 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2300 QEMUMachine
*machine
= opaque
;
2301 int fatal_error
= 0;
2303 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2310 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2312 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2313 qemu_opt_set(opts
, "snapshot", "on");
2318 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2320 boot_set_handler
= func
;
2321 boot_set_opaque
= opaque
;
2324 int qemu_boot_set(const char *boot_devices
)
2326 if (!boot_set_handler
) {
2329 return boot_set_handler(boot_set_opaque
, boot_devices
);
2332 static int parse_bootdevices(char *devices
)
2334 /* We just do some generic consistency checks */
2338 for (p
= devices
; *p
!= '\0'; p
++) {
2339 /* Allowed boot devices are:
2340 * a-b: floppy disk drives
2341 * c-f: IDE disk drives
2342 * g-m: machine implementation dependant drives
2343 * n-p: network devices
2344 * It's up to each machine implementation to check if the given boot
2345 * devices match the actual hardware implementation and firmware
2348 if (*p
< 'a' || *p
> 'p') {
2349 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2352 if (bitmap
& (1 << (*p
- 'a'))) {
2353 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2356 bitmap
|= 1 << (*p
- 'a');
2361 static void restore_boot_devices(void *opaque
)
2363 char *standard_boot_devices
= opaque
;
2365 qemu_boot_set(standard_boot_devices
);
2367 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2368 qemu_free(standard_boot_devices
);
2371 static void numa_add(const char *optarg
)
2375 unsigned long long value
, endvalue
;
2378 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2379 if (!strcmp(option
, "node")) {
2380 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2381 nodenr
= nb_numa_nodes
;
2383 nodenr
= strtoull(option
, NULL
, 10);
2386 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2387 node_mem
[nodenr
] = 0;
2389 value
= strtoull(option
, &endptr
, 0);
2391 case 0: case 'M': case 'm':
2398 node_mem
[nodenr
] = value
;
2400 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2401 node_cpumask
[nodenr
] = 0;
2403 value
= strtoull(option
, &endptr
, 10);
2406 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2408 if (*endptr
== '-') {
2409 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2410 if (endvalue
>= 63) {
2413 "only 63 CPUs in NUMA mode supported.\n");
2415 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2420 node_cpumask
[nodenr
] = value
;
2427 /***********************************************************/
2430 static USBPort
*used_usb_ports
;
2431 static USBPort
*free_usb_ports
;
2433 /* ??? Maybe change this to register a hub to keep track of the topology. */
2434 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2435 usb_attachfn attach
)
2437 port
->opaque
= opaque
;
2438 port
->index
= index
;
2439 port
->attach
= attach
;
2440 port
->next
= free_usb_ports
;
2441 free_usb_ports
= port
;
2444 int usb_device_add_dev(USBDevice
*dev
)
2448 /* Find a USB port to add the device to. */
2449 port
= free_usb_ports
;
2453 /* Create a new hub and chain it on. */
2454 free_usb_ports
= NULL
;
2455 port
->next
= used_usb_ports
;
2456 used_usb_ports
= port
;
2458 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2459 usb_attach(port
, hub
);
2460 port
= free_usb_ports
;
2463 free_usb_ports
= port
->next
;
2464 port
->next
= used_usb_ports
;
2465 used_usb_ports
= port
;
2466 usb_attach(port
, dev
);
2470 static void usb_msd_password_cb(void *opaque
, int err
)
2472 USBDevice
*dev
= opaque
;
2475 usb_device_add_dev(dev
);
2477 dev
->handle_destroy(dev
);
2480 static int usb_device_add(const char *devname
, int is_hotplug
)
2485 if (!free_usb_ports
)
2488 if (strstart(devname
, "host:", &p
)) {
2489 dev
= usb_host_device_open(p
);
2490 } else if (!strcmp(devname
, "mouse")) {
2491 dev
= usb_mouse_init();
2492 } else if (!strcmp(devname
, "tablet")) {
2493 dev
= usb_tablet_init();
2494 } else if (!strcmp(devname
, "keyboard")) {
2495 dev
= usb_keyboard_init();
2496 } else if (strstart(devname
, "disk:", &p
)) {
2497 BlockDriverState
*bs
;
2499 dev
= usb_msd_init(p
);
2502 bs
= usb_msd_get_bdrv(dev
);
2503 if (bdrv_key_required(bs
)) {
2506 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2511 } else if (!strcmp(devname
, "wacom-tablet")) {
2512 dev
= usb_wacom_init();
2513 } else if (strstart(devname
, "serial:", &p
)) {
2514 dev
= usb_serial_init(p
);
2515 #ifdef CONFIG_BRLAPI
2516 } else if (!strcmp(devname
, "braille")) {
2517 dev
= usb_baum_init();
2519 } else if (strstart(devname
, "net:", &p
)) {
2522 if (net_client_init(NULL
, "nic", p
) < 0)
2524 nd_table
[nic
].model
= "usb";
2525 dev
= usb_net_init(&nd_table
[nic
]);
2526 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2527 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2528 bt_new_hci(qemu_find_bt_vlan(0)));
2535 return usb_device_add_dev(dev
);
2538 int usb_device_del_addr(int bus_num
, int addr
)
2544 if (!used_usb_ports
)
2550 lastp
= &used_usb_ports
;
2551 port
= used_usb_ports
;
2552 while (port
&& port
->dev
->addr
!= addr
) {
2553 lastp
= &port
->next
;
2561 *lastp
= port
->next
;
2562 usb_attach(port
, NULL
);
2563 dev
->handle_destroy(dev
);
2564 port
->next
= free_usb_ports
;
2565 free_usb_ports
= port
;
2569 static int usb_device_del(const char *devname
)
2574 if (strstart(devname
, "host:", &p
))
2575 return usb_host_device_close(p
);
2577 if (!used_usb_ports
)
2580 p
= strchr(devname
, '.');
2583 bus_num
= strtoul(devname
, NULL
, 0);
2584 addr
= strtoul(p
+ 1, NULL
, 0);
2586 return usb_device_del_addr(bus_num
, addr
);
2589 static int usb_parse(const char *cmdline
)
2591 return usb_device_add(cmdline
, 0);
2594 void do_usb_add(Monitor
*mon
, const char *devname
)
2596 usb_device_add(devname
, 1);
2599 void do_usb_del(Monitor
*mon
, const char *devname
)
2601 usb_device_del(devname
);
2604 void usb_info(Monitor
*mon
)
2608 const char *speed_str
;
2611 monitor_printf(mon
, "USB support not enabled\n");
2615 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2619 switch(dev
->speed
) {
2623 case USB_SPEED_FULL
:
2626 case USB_SPEED_HIGH
:
2633 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2634 0, dev
->addr
, speed_str
, dev
->devname
);
2638 /***********************************************************/
2639 /* PCMCIA/Cardbus */
2641 static struct pcmcia_socket_entry_s
{
2642 PCMCIASocket
*socket
;
2643 struct pcmcia_socket_entry_s
*next
;
2644 } *pcmcia_sockets
= 0;
2646 void pcmcia_socket_register(PCMCIASocket
*socket
)
2648 struct pcmcia_socket_entry_s
*entry
;
2650 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2651 entry
->socket
= socket
;
2652 entry
->next
= pcmcia_sockets
;
2653 pcmcia_sockets
= entry
;
2656 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2658 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2660 ptr
= &pcmcia_sockets
;
2661 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2662 if (entry
->socket
== socket
) {
2668 void pcmcia_info(Monitor
*mon
)
2670 struct pcmcia_socket_entry_s
*iter
;
2672 if (!pcmcia_sockets
)
2673 monitor_printf(mon
, "No PCMCIA sockets\n");
2675 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2676 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2677 iter
->socket
->attached
? iter
->socket
->card_string
:
2681 /***********************************************************/
2682 /* register display */
2684 struct DisplayAllocator default_allocator
= {
2685 defaultallocator_create_displaysurface
,
2686 defaultallocator_resize_displaysurface
,
2687 defaultallocator_free_displaysurface
2690 void register_displaystate(DisplayState
*ds
)
2700 DisplayState
*get_displaystate(void)
2702 return display_state
;
2705 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2707 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2708 return ds
->allocator
;
2713 static void dumb_display_init(void)
2715 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2716 ds
->allocator
= &default_allocator
;
2717 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2718 register_displaystate(ds
);
2721 /***********************************************************/
2724 typedef struct IOHandlerRecord
{
2726 IOCanRWHandler
*fd_read_poll
;
2728 IOHandler
*fd_write
;
2731 /* temporary data */
2733 struct IOHandlerRecord
*next
;
2736 static IOHandlerRecord
*first_io_handler
;
2738 /* XXX: fd_read_poll should be suppressed, but an API change is
2739 necessary in the character devices to suppress fd_can_read(). */
2740 int qemu_set_fd_handler2(int fd
,
2741 IOCanRWHandler
*fd_read_poll
,
2743 IOHandler
*fd_write
,
2746 IOHandlerRecord
**pioh
, *ioh
;
2748 if (!fd_read
&& !fd_write
) {
2749 pioh
= &first_io_handler
;
2754 if (ioh
->fd
== fd
) {
2761 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2765 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2766 ioh
->next
= first_io_handler
;
2767 first_io_handler
= ioh
;
2770 ioh
->fd_read_poll
= fd_read_poll
;
2771 ioh
->fd_read
= fd_read
;
2772 ioh
->fd_write
= fd_write
;
2773 ioh
->opaque
= opaque
;
2779 int qemu_set_fd_handler(int fd
,
2781 IOHandler
*fd_write
,
2784 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2788 /***********************************************************/
2789 /* Polling handling */
2791 typedef struct PollingEntry
{
2794 struct PollingEntry
*next
;
2797 static PollingEntry
*first_polling_entry
;
2799 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2801 PollingEntry
**ppe
, *pe
;
2802 pe
= qemu_mallocz(sizeof(PollingEntry
));
2804 pe
->opaque
= opaque
;
2805 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2810 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2812 PollingEntry
**ppe
, *pe
;
2813 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2815 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2823 /***********************************************************/
2824 /* Wait objects support */
2825 typedef struct WaitObjects
{
2827 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2828 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2829 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2832 static WaitObjects wait_objects
= {0};
2834 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2836 WaitObjects
*w
= &wait_objects
;
2838 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2840 w
->events
[w
->num
] = handle
;
2841 w
->func
[w
->num
] = func
;
2842 w
->opaque
[w
->num
] = opaque
;
2847 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2850 WaitObjects
*w
= &wait_objects
;
2853 for (i
= 0; i
< w
->num
; i
++) {
2854 if (w
->events
[i
] == handle
)
2857 w
->events
[i
] = w
->events
[i
+ 1];
2858 w
->func
[i
] = w
->func
[i
+ 1];
2859 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2867 /***********************************************************/
2868 /* ram save/restore */
2870 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2874 v
= qemu_get_byte(f
);
2877 if (qemu_get_buffer(f
, buf
, len
) != len
)
2881 v
= qemu_get_byte(f
);
2882 memset(buf
, v
, len
);
2888 if (qemu_file_has_error(f
))
2894 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2899 if (qemu_get_be32(f
) != last_ram_offset
)
2901 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2902 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2909 #define BDRV_HASH_BLOCK_SIZE 1024
2910 #define IOBUF_SIZE 4096
2911 #define RAM_CBLOCK_MAGIC 0xfabe
2913 typedef struct RamDecompressState
{
2916 uint8_t buf
[IOBUF_SIZE
];
2917 } RamDecompressState
;
2919 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2922 memset(s
, 0, sizeof(*s
));
2924 ret
= inflateInit(&s
->zstream
);
2930 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2934 s
->zstream
.avail_out
= len
;
2935 s
->zstream
.next_out
= buf
;
2936 while (s
->zstream
.avail_out
> 0) {
2937 if (s
->zstream
.avail_in
== 0) {
2938 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2940 clen
= qemu_get_be16(s
->f
);
2941 if (clen
> IOBUF_SIZE
)
2943 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2944 s
->zstream
.avail_in
= clen
;
2945 s
->zstream
.next_in
= s
->buf
;
2947 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2948 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2955 static void ram_decompress_close(RamDecompressState
*s
)
2957 inflateEnd(&s
->zstream
);
2960 #define RAM_SAVE_FLAG_FULL 0x01
2961 #define RAM_SAVE_FLAG_COMPRESS 0x02
2962 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2963 #define RAM_SAVE_FLAG_PAGE 0x08
2964 #define RAM_SAVE_FLAG_EOS 0x10
2966 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2968 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2969 uint32_t *array
= (uint32_t *)page
;
2972 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2973 if (array
[i
] != val
)
2980 static int ram_save_block(QEMUFile
*f
)
2982 static ram_addr_t current_addr
= 0;
2983 ram_addr_t saved_addr
= current_addr
;
2984 ram_addr_t addr
= 0;
2987 while (addr
< last_ram_offset
) {
2988 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2991 cpu_physical_memory_reset_dirty(current_addr
,
2992 current_addr
+ TARGET_PAGE_SIZE
,
2993 MIGRATION_DIRTY_FLAG
);
2995 p
= qemu_get_ram_ptr(current_addr
);
2997 if (is_dup_page(p
, *p
)) {
2998 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2999 qemu_put_byte(f
, *p
);
3001 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3002 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3008 addr
+= TARGET_PAGE_SIZE
;
3009 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3015 static uint64_t bytes_transferred
= 0;
3017 static ram_addr_t
ram_save_remaining(void)
3020 ram_addr_t count
= 0;
3022 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3023 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3030 uint64_t ram_bytes_remaining(void)
3032 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3035 uint64_t ram_bytes_transferred(void)
3037 return bytes_transferred
;
3040 uint64_t ram_bytes_total(void)
3042 return last_ram_offset
;
3045 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3048 uint64_t bytes_transferred_last
;
3050 uint64_t expected_time
= 0;
3052 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3053 qemu_file_set_error(f
);
3058 /* Make sure all dirty bits are set */
3059 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3060 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3061 cpu_physical_memory_set_dirty(addr
);
3064 /* Enable dirty memory tracking */
3065 cpu_physical_memory_set_dirty_tracking(1);
3067 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3070 bytes_transferred_last
= bytes_transferred
;
3071 bwidth
= get_clock();
3073 while (!qemu_file_rate_limit(f
)) {
3076 ret
= ram_save_block(f
);
3077 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3078 if (ret
== 0) /* no more blocks */
3082 bwidth
= get_clock() - bwidth
;
3083 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3085 /* if we haven't transferred anything this round, force expected_time to a
3086 * a very high value, but without crashing */
3090 /* try transferring iterative blocks of memory */
3094 /* flush all remaining blocks regardless of rate limiting */
3095 while (ram_save_block(f
) != 0) {
3096 bytes_transferred
+= TARGET_PAGE_SIZE
;
3098 cpu_physical_memory_set_dirty_tracking(0);
3101 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3103 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3105 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3108 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3110 RamDecompressState s1
, *s
= &s1
;
3114 if (ram_decompress_open(s
, f
) < 0)
3116 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3117 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3118 fprintf(stderr
, "Error while reading ram block header\n");
3122 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3123 BDRV_HASH_BLOCK_SIZE
) < 0) {
3124 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3129 printf("Error block header\n");
3133 ram_decompress_close(s
);
3138 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3143 if (version_id
== 1)
3144 return ram_load_v1(f
, opaque
);
3146 if (version_id
== 2) {
3147 if (qemu_get_be32(f
) != last_ram_offset
)
3149 return ram_load_dead(f
, opaque
);
3152 if (version_id
!= 3)
3156 addr
= qemu_get_be64(f
);
3158 flags
= addr
& ~TARGET_PAGE_MASK
;
3159 addr
&= TARGET_PAGE_MASK
;
3161 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3162 if (addr
!= last_ram_offset
)
3166 if (flags
& RAM_SAVE_FLAG_FULL
) {
3167 if (ram_load_dead(f
, opaque
) < 0)
3171 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3172 uint8_t ch
= qemu_get_byte(f
);
3173 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3176 (!kvm_enabled() || kvm_has_sync_mmu())) {
3177 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3180 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3181 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3182 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3187 void qemu_service_io(void)
3189 qemu_notify_event();
3192 /***********************************************************/
3193 /* bottom halves (can be seen as timers which expire ASAP) */
3204 static QEMUBH
*first_bh
= NULL
;
3206 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3209 bh
= qemu_mallocz(sizeof(QEMUBH
));
3211 bh
->opaque
= opaque
;
3212 bh
->next
= first_bh
;
3217 int qemu_bh_poll(void)
3223 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3224 if (!bh
->deleted
&& bh
->scheduled
) {
3233 /* remove deleted bhs */
3247 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3255 void qemu_bh_schedule(QEMUBH
*bh
)
3261 /* stop the currently executing CPU to execute the BH ASAP */
3262 qemu_notify_event();
3265 void qemu_bh_cancel(QEMUBH
*bh
)
3270 void qemu_bh_delete(QEMUBH
*bh
)
3276 static void qemu_bh_update_timeout(int *timeout
)
3280 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3281 if (!bh
->deleted
&& bh
->scheduled
) {
3283 /* idle bottom halves will be polled at least
3285 *timeout
= MIN(10, *timeout
);
3287 /* non-idle bottom halves will be executed
3296 /***********************************************************/
3297 /* machine registration */
3299 static QEMUMachine
*first_machine
= NULL
;
3300 QEMUMachine
*current_machine
= NULL
;
3302 int qemu_register_machine(QEMUMachine
*m
)
3305 pm
= &first_machine
;
3313 static QEMUMachine
*find_machine(const char *name
)
3317 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3318 if (!strcmp(m
->name
, name
))
3320 if (m
->alias
&& !strcmp(m
->alias
, name
))
3326 static QEMUMachine
*find_default_machine(void)
3330 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3331 if (m
->is_default
) {
3338 /***********************************************************/
3339 /* main execution loop */
3341 static void gui_update(void *opaque
)
3343 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3344 DisplayState
*ds
= opaque
;
3345 DisplayChangeListener
*dcl
= ds
->listeners
;
3349 while (dcl
!= NULL
) {
3350 if (dcl
->gui_timer_interval
&&
3351 dcl
->gui_timer_interval
< interval
)
3352 interval
= dcl
->gui_timer_interval
;
3355 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3358 static void nographic_update(void *opaque
)
3360 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3362 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3365 struct vm_change_state_entry
{
3366 VMChangeStateHandler
*cb
;
3368 LIST_ENTRY (vm_change_state_entry
) entries
;
3371 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3373 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3376 VMChangeStateEntry
*e
;
3378 e
= qemu_mallocz(sizeof (*e
));
3382 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3386 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3388 LIST_REMOVE (e
, entries
);
3392 static void vm_state_notify(int running
, int reason
)
3394 VMChangeStateEntry
*e
;
3396 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3397 e
->cb(e
->opaque
, running
, reason
);
3401 static void resume_all_vcpus(void);
3402 static void pause_all_vcpus(void);
3409 vm_state_notify(1, 0);
3410 qemu_rearm_alarm_timer(alarm_timer
);
3415 /* reset/shutdown handler */
3417 typedef struct QEMUResetEntry
{
3418 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3419 QEMUResetHandler
*func
;
3423 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3424 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3425 static int reset_requested
;
3426 static int shutdown_requested
;
3427 static int powerdown_requested
;
3428 static int debug_requested
;
3429 static int vmstop_requested
;
3431 int qemu_shutdown_requested(void)
3433 int r
= shutdown_requested
;
3434 shutdown_requested
= 0;
3438 int qemu_reset_requested(void)
3440 int r
= reset_requested
;
3441 reset_requested
= 0;
3445 int qemu_powerdown_requested(void)
3447 int r
= powerdown_requested
;
3448 powerdown_requested
= 0;
3452 static int qemu_debug_requested(void)
3454 int r
= debug_requested
;
3455 debug_requested
= 0;
3459 static int qemu_vmstop_requested(void)
3461 int r
= vmstop_requested
;
3462 vmstop_requested
= 0;
3466 static void do_vm_stop(int reason
)
3469 cpu_disable_ticks();
3472 vm_state_notify(0, reason
);
3476 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3478 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3481 re
->opaque
= opaque
;
3482 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3485 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3489 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3490 if (re
->func
== func
&& re
->opaque
== opaque
) {
3491 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3498 void qemu_system_reset(void)
3500 QEMUResetEntry
*re
, *nre
;
3502 /* reset all devices */
3503 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3504 re
->func(re
->opaque
);
3508 void qemu_system_reset_request(void)
3511 shutdown_requested
= 1;
3513 reset_requested
= 1;
3515 qemu_notify_event();
3518 void qemu_system_shutdown_request(void)
3520 shutdown_requested
= 1;
3521 qemu_notify_event();
3524 void qemu_system_powerdown_request(void)
3526 powerdown_requested
= 1;
3527 qemu_notify_event();
3530 #ifdef CONFIG_IOTHREAD
3531 static void qemu_system_vmstop_request(int reason
)
3533 vmstop_requested
= reason
;
3534 qemu_notify_event();
3539 static int io_thread_fd
= -1;
3541 static void qemu_event_increment(void)
3543 static const char byte
= 0;
3545 if (io_thread_fd
== -1)
3548 write(io_thread_fd
, &byte
, sizeof(byte
));
3551 static void qemu_event_read(void *opaque
)
3553 int fd
= (unsigned long)opaque
;
3556 /* Drain the notify pipe */
3559 len
= read(fd
, buffer
, sizeof(buffer
));
3560 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3563 static int qemu_event_init(void)
3572 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3576 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3580 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3581 (void *)(unsigned long)fds
[0]);
3583 io_thread_fd
= fds
[1];
3592 HANDLE qemu_event_handle
;
3594 static void dummy_event_handler(void *opaque
)
3598 static int qemu_event_init(void)
3600 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3601 if (!qemu_event_handle
) {
3602 perror("Failed CreateEvent");
3605 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3609 static void qemu_event_increment(void)
3611 SetEvent(qemu_event_handle
);
3615 static int cpu_can_run(CPUState
*env
)
3624 #ifndef CONFIG_IOTHREAD
3625 static int qemu_init_main_loop(void)
3627 return qemu_event_init();
3630 void qemu_init_vcpu(void *_env
)
3632 CPUState
*env
= _env
;
3639 int qemu_cpu_self(void *env
)
3644 static void resume_all_vcpus(void)
3648 static void pause_all_vcpus(void)
3652 void qemu_cpu_kick(void *env
)
3657 void qemu_notify_event(void)
3659 CPUState
*env
= cpu_single_env
;
3664 if (env
->kqemu_enabled
)
3665 kqemu_cpu_interrupt(env
);
3670 #define qemu_mutex_lock_iothread() do { } while (0)
3671 #define qemu_mutex_unlock_iothread() do { } while (0)
3673 void vm_stop(int reason
)
3678 #else /* CONFIG_IOTHREAD */
3680 #include "qemu-thread.h"
3682 QemuMutex qemu_global_mutex
;
3683 static QemuMutex qemu_fair_mutex
;
3685 static QemuThread io_thread
;
3687 static QemuThread
*tcg_cpu_thread
;
3688 static QemuCond
*tcg_halt_cond
;
3690 static int qemu_system_ready
;
3692 static QemuCond qemu_cpu_cond
;
3694 static QemuCond qemu_system_cond
;
3695 static QemuCond qemu_pause_cond
;
3697 static void block_io_signals(void);
3698 static void unblock_io_signals(void);
3699 static int tcg_has_work(void);
3701 static int qemu_init_main_loop(void)
3705 ret
= qemu_event_init();
3709 qemu_cond_init(&qemu_pause_cond
);
3710 qemu_mutex_init(&qemu_fair_mutex
);
3711 qemu_mutex_init(&qemu_global_mutex
);
3712 qemu_mutex_lock(&qemu_global_mutex
);
3714 unblock_io_signals();
3715 qemu_thread_self(&io_thread
);
3720 static void qemu_wait_io_event(CPUState
*env
)
3722 while (!tcg_has_work())
3723 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3725 qemu_mutex_unlock(&qemu_global_mutex
);
3728 * Users of qemu_global_mutex can be starved, having no chance
3729 * to acquire it since this path will get to it first.
3730 * So use another lock to provide fairness.
3732 qemu_mutex_lock(&qemu_fair_mutex
);
3733 qemu_mutex_unlock(&qemu_fair_mutex
);
3735 qemu_mutex_lock(&qemu_global_mutex
);
3739 qemu_cond_signal(&qemu_pause_cond
);
3743 static int qemu_cpu_exec(CPUState
*env
);
3745 static void *kvm_cpu_thread_fn(void *arg
)
3747 CPUState
*env
= arg
;
3750 qemu_thread_self(env
->thread
);
3752 /* signal CPU creation */
3753 qemu_mutex_lock(&qemu_global_mutex
);
3755 qemu_cond_signal(&qemu_cpu_cond
);
3757 /* and wait for machine initialization */
3758 while (!qemu_system_ready
)
3759 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3762 if (cpu_can_run(env
))
3764 qemu_wait_io_event(env
);
3770 static void tcg_cpu_exec(void);
3772 static void *tcg_cpu_thread_fn(void *arg
)
3774 CPUState
*env
= arg
;
3777 qemu_thread_self(env
->thread
);
3779 /* signal CPU creation */
3780 qemu_mutex_lock(&qemu_global_mutex
);
3781 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3783 qemu_cond_signal(&qemu_cpu_cond
);
3785 /* and wait for machine initialization */
3786 while (!qemu_system_ready
)
3787 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3791 qemu_wait_io_event(cur_cpu
);
3797 void qemu_cpu_kick(void *_env
)
3799 CPUState
*env
= _env
;
3800 qemu_cond_broadcast(env
->halt_cond
);
3802 qemu_thread_signal(env
->thread
, SIGUSR1
);
3805 int qemu_cpu_self(void *env
)
3807 return (cpu_single_env
!= NULL
);
3810 static void cpu_signal(int sig
)
3813 cpu_exit(cpu_single_env
);
3816 static void block_io_signals(void)
3819 struct sigaction sigact
;
3822 sigaddset(&set
, SIGUSR2
);
3823 sigaddset(&set
, SIGIO
);
3824 sigaddset(&set
, SIGALRM
);
3825 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3828 sigaddset(&set
, SIGUSR1
);
3829 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3831 memset(&sigact
, 0, sizeof(sigact
));
3832 sigact
.sa_handler
= cpu_signal
;
3833 sigaction(SIGUSR1
, &sigact
, NULL
);
3836 static void unblock_io_signals(void)
3841 sigaddset(&set
, SIGUSR2
);
3842 sigaddset(&set
, SIGIO
);
3843 sigaddset(&set
, SIGALRM
);
3844 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3847 sigaddset(&set
, SIGUSR1
);
3848 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3851 static void qemu_signal_lock(unsigned int msecs
)
3853 qemu_mutex_lock(&qemu_fair_mutex
);
3855 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3856 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3857 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3860 qemu_mutex_unlock(&qemu_fair_mutex
);
3863 static void qemu_mutex_lock_iothread(void)
3865 if (kvm_enabled()) {
3866 qemu_mutex_lock(&qemu_fair_mutex
);
3867 qemu_mutex_lock(&qemu_global_mutex
);
3868 qemu_mutex_unlock(&qemu_fair_mutex
);
3870 qemu_signal_lock(100);
3873 static void qemu_mutex_unlock_iothread(void)
3875 qemu_mutex_unlock(&qemu_global_mutex
);
3878 static int all_vcpus_paused(void)
3880 CPUState
*penv
= first_cpu
;
3885 penv
= (CPUState
*)penv
->next_cpu
;
3891 static void pause_all_vcpus(void)
3893 CPUState
*penv
= first_cpu
;
3897 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3898 qemu_cpu_kick(penv
);
3899 penv
= (CPUState
*)penv
->next_cpu
;
3902 while (!all_vcpus_paused()) {
3903 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3906 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3907 penv
= (CPUState
*)penv
->next_cpu
;
3912 static void resume_all_vcpus(void)
3914 CPUState
*penv
= first_cpu
;
3919 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3920 qemu_cpu_kick(penv
);
3921 penv
= (CPUState
*)penv
->next_cpu
;
3925 static void tcg_init_vcpu(void *_env
)
3927 CPUState
*env
= _env
;
3928 /* share a single thread for all cpus with TCG */
3929 if (!tcg_cpu_thread
) {
3930 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3931 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3932 qemu_cond_init(env
->halt_cond
);
3933 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3934 while (env
->created
== 0)
3935 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3936 tcg_cpu_thread
= env
->thread
;
3937 tcg_halt_cond
= env
->halt_cond
;
3939 env
->thread
= tcg_cpu_thread
;
3940 env
->halt_cond
= tcg_halt_cond
;
3944 static void kvm_start_vcpu(CPUState
*env
)
3947 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3948 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3949 qemu_cond_init(env
->halt_cond
);
3950 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3951 while (env
->created
== 0)
3952 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3955 void qemu_init_vcpu(void *_env
)
3957 CPUState
*env
= _env
;
3960 kvm_start_vcpu(env
);
3965 void qemu_notify_event(void)
3967 qemu_event_increment();
3970 void vm_stop(int reason
)
3973 qemu_thread_self(&me
);
3975 if (!qemu_thread_equal(&me
, &io_thread
)) {
3976 qemu_system_vmstop_request(reason
);
3978 * FIXME: should not return to device code in case
3979 * vm_stop() has been requested.
3981 if (cpu_single_env
) {
3982 cpu_exit(cpu_single_env
);
3983 cpu_single_env
->stop
= 1;
3994 static void host_main_loop_wait(int *timeout
)
4000 /* XXX: need to suppress polling by better using win32 events */
4002 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4003 ret
|= pe
->func(pe
->opaque
);
4007 WaitObjects
*w
= &wait_objects
;
4009 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4010 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4011 if (w
->func
[ret
- WAIT_OBJECT_0
])
4012 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4014 /* Check for additional signaled events */
4015 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4017 /* Check if event is signaled */
4018 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4019 if(ret2
== WAIT_OBJECT_0
) {
4021 w
->func
[i
](w
->opaque
[i
]);
4022 } else if (ret2
== WAIT_TIMEOUT
) {
4024 err
= GetLastError();
4025 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4028 } else if (ret
== WAIT_TIMEOUT
) {
4030 err
= GetLastError();
4031 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4038 static void host_main_loop_wait(int *timeout
)
4043 void main_loop_wait(int timeout
)
4045 IOHandlerRecord
*ioh
;
4046 fd_set rfds
, wfds
, xfds
;
4050 qemu_bh_update_timeout(&timeout
);
4052 host_main_loop_wait(&timeout
);
4054 /* poll any events */
4055 /* XXX: separate device handlers from system ones */
4060 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4064 (!ioh
->fd_read_poll
||
4065 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4066 FD_SET(ioh
->fd
, &rfds
);
4070 if (ioh
->fd_write
) {
4071 FD_SET(ioh
->fd
, &wfds
);
4077 tv
.tv_sec
= timeout
/ 1000;
4078 tv
.tv_usec
= (timeout
% 1000) * 1000;
4080 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4082 qemu_mutex_unlock_iothread();
4083 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4084 qemu_mutex_lock_iothread();
4086 IOHandlerRecord
**pioh
;
4088 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4089 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4090 ioh
->fd_read(ioh
->opaque
);
4092 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4093 ioh
->fd_write(ioh
->opaque
);
4097 /* remove deleted IO handlers */
4098 pioh
= &first_io_handler
;
4109 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4111 /* rearm timer, if not periodic */
4112 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4113 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4114 qemu_rearm_alarm_timer(alarm_timer
);
4117 /* vm time timers */
4119 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4120 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4121 qemu_get_clock(vm_clock
));
4124 /* real time timers */
4125 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4126 qemu_get_clock(rt_clock
));
4128 /* Check bottom-halves last in case any of the earlier events triggered
4134 static int qemu_cpu_exec(CPUState
*env
)
4137 #ifdef CONFIG_PROFILER
4141 #ifdef CONFIG_PROFILER
4142 ti
= profile_getclock();
4147 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4148 env
->icount_decr
.u16
.low
= 0;
4149 env
->icount_extra
= 0;
4150 count
= qemu_next_deadline();
4151 count
= (count
+ (1 << icount_time_shift
) - 1)
4152 >> icount_time_shift
;
4153 qemu_icount
+= count
;
4154 decr
= (count
> 0xffff) ? 0xffff : count
;
4156 env
->icount_decr
.u16
.low
= decr
;
4157 env
->icount_extra
= count
;
4159 ret
= cpu_exec(env
);
4160 #ifdef CONFIG_PROFILER
4161 qemu_time
+= profile_getclock() - ti
;
4164 /* Fold pending instructions back into the
4165 instruction counter, and clear the interrupt flag. */
4166 qemu_icount
-= (env
->icount_decr
.u16
.low
4167 + env
->icount_extra
);
4168 env
->icount_decr
.u32
= 0;
4169 env
->icount_extra
= 0;
4174 static void tcg_cpu_exec(void)
4178 if (next_cpu
== NULL
)
4179 next_cpu
= first_cpu
;
4180 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4181 CPUState
*env
= cur_cpu
= next_cpu
;
4185 if (timer_alarm_pending
) {
4186 timer_alarm_pending
= 0;
4189 if (cpu_can_run(env
))
4190 ret
= qemu_cpu_exec(env
);
4191 if (ret
== EXCP_DEBUG
) {
4192 gdb_set_stop_cpu(env
);
4193 debug_requested
= 1;
4199 static int cpu_has_work(CPUState
*env
)
4207 if (qemu_cpu_has_work(env
))
4212 static int tcg_has_work(void)
4216 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4217 if (cpu_has_work(env
))
4222 static int qemu_calculate_timeout(void)
4224 #ifndef CONFIG_IOTHREAD
4229 else if (tcg_has_work())
4231 else if (!use_icount
)
4234 /* XXX: use timeout computed from timers */
4237 /* Advance virtual time to the next event. */
4238 if (use_icount
== 1) {
4239 /* When not using an adaptive execution frequency
4240 we tend to get badly out of sync with real time,
4241 so just delay for a reasonable amount of time. */
4244 delta
= cpu_get_icount() - cpu_get_clock();
4247 /* If virtual time is ahead of real time then just
4249 timeout
= (delta
/ 1000000) + 1;
4251 /* Wait for either IO to occur or the next
4253 add
= qemu_next_deadline();
4254 /* We advance the timer before checking for IO.
4255 Limit the amount we advance so that early IO
4256 activity won't get the guest too far ahead. */
4260 add
= (add
+ (1 << icount_time_shift
) - 1)
4261 >> icount_time_shift
;
4263 timeout
= delta
/ 1000000;
4270 #else /* CONFIG_IOTHREAD */
4275 static int vm_can_run(void)
4277 if (powerdown_requested
)
4279 if (reset_requested
)
4281 if (shutdown_requested
)
4283 if (debug_requested
)
4288 static void main_loop(void)
4292 #ifdef CONFIG_IOTHREAD
4293 qemu_system_ready
= 1;
4294 qemu_cond_broadcast(&qemu_system_cond
);
4299 #ifdef CONFIG_PROFILER
4302 #ifndef CONFIG_IOTHREAD
4305 #ifdef CONFIG_PROFILER
4306 ti
= profile_getclock();
4308 main_loop_wait(qemu_calculate_timeout());
4309 #ifdef CONFIG_PROFILER
4310 dev_time
+= profile_getclock() - ti
;
4312 } while (vm_can_run());
4314 if (qemu_debug_requested())
4315 vm_stop(EXCP_DEBUG
);
4316 if (qemu_shutdown_requested()) {
4323 if (qemu_reset_requested()) {
4325 qemu_system_reset();
4328 if (qemu_powerdown_requested())
4329 qemu_system_powerdown();
4330 if ((r
= qemu_vmstop_requested()))
4336 static void version(void)
4338 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4341 static void help(int exitcode
)
4344 printf("usage: %s [options] [disk_image]\n"
4346 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4348 #define DEF(option, opt_arg, opt_enum, opt_help) \
4350 #define DEFHEADING(text) stringify(text) "\n"
4351 #include "qemu-options.h"
4356 "During emulation, the following keys are useful:\n"
4357 "ctrl-alt-f toggle full screen\n"
4358 "ctrl-alt-n switch to virtual console 'n'\n"
4359 "ctrl-alt toggle mouse and keyboard grab\n"
4361 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4366 DEFAULT_NETWORK_SCRIPT
,
4367 DEFAULT_NETWORK_DOWN_SCRIPT
,
4369 DEFAULT_GDBSTUB_PORT
,
4374 #define HAS_ARG 0x0001
4377 #define DEF(option, opt_arg, opt_enum, opt_help) \
4379 #define DEFHEADING(text)
4380 #include "qemu-options.h"
4386 typedef struct QEMUOption
{
4392 static const QEMUOption qemu_options
[] = {
4393 { "h", 0, QEMU_OPTION_h
},
4394 #define DEF(option, opt_arg, opt_enum, opt_help) \
4395 { option, opt_arg, opt_enum },
4396 #define DEFHEADING(text)
4397 #include "qemu-options.h"
4405 struct soundhw soundhw
[] = {
4406 #ifdef HAS_AUDIO_CHOICE
4407 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4413 { .init_isa
= pcspk_audio_init
}
4420 "Creative Sound Blaster 16",
4423 { .init_isa
= SB16_init
}
4427 #ifdef CONFIG_CS4231A
4433 { .init_isa
= cs4231a_init
}
4441 "Yamaha YMF262 (OPL3)",
4443 "Yamaha YM3812 (OPL2)",
4447 { .init_isa
= Adlib_init
}
4454 "Gravis Ultrasound GF1",
4457 { .init_isa
= GUS_init
}
4464 "Intel 82801AA AC97 Audio",
4467 { .init_pci
= ac97_init
}
4471 #ifdef CONFIG_ES1370
4474 "ENSONIQ AudioPCI ES1370",
4477 { .init_pci
= es1370_init
}
4481 #endif /* HAS_AUDIO_CHOICE */
4483 { NULL
, NULL
, 0, 0, { NULL
} }
4486 static void select_soundhw (const char *optarg
)
4490 if (*optarg
== '?') {
4493 printf ("Valid sound card names (comma separated):\n");
4494 for (c
= soundhw
; c
->name
; ++c
) {
4495 printf ("%-11s %s\n", c
->name
, c
->descr
);
4497 printf ("\n-soundhw all will enable all of the above\n");
4498 exit (*optarg
!= '?');
4506 if (!strcmp (optarg
, "all")) {
4507 for (c
= soundhw
; c
->name
; ++c
) {
4515 e
= strchr (p
, ',');
4516 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4518 for (c
= soundhw
; c
->name
; ++c
) {
4519 if (!strncmp (c
->name
, p
, l
)) {
4528 "Unknown sound card name (too big to show)\n");
4531 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4536 p
+= l
+ (e
!= NULL
);
4540 goto show_valid_cards
;
4545 static void select_vgahw (const char *p
)
4549 cirrus_vga_enabled
= 0;
4550 std_vga_enabled
= 0;
4553 if (strstart(p
, "std", &opts
)) {
4554 std_vga_enabled
= 1;
4555 } else if (strstart(p
, "cirrus", &opts
)) {
4556 cirrus_vga_enabled
= 1;
4557 } else if (strstart(p
, "vmware", &opts
)) {
4559 } else if (strstart(p
, "xenfb", &opts
)) {
4561 } else if (!strstart(p
, "none", &opts
)) {
4563 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4567 const char *nextopt
;
4569 if (strstart(opts
, ",retrace=", &nextopt
)) {
4571 if (strstart(opts
, "dumb", &nextopt
))
4572 vga_retrace_method
= VGA_RETRACE_DUMB
;
4573 else if (strstart(opts
, "precise", &nextopt
))
4574 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4575 else goto invalid_vga
;
4576 } else goto invalid_vga
;
4582 static int balloon_parse(const char *arg
)
4587 if (!strcmp(arg
, "none")) {
4589 } else if (!strncmp(arg
, "virtio", 6)) {
4591 if (arg
[6] == ',') {
4593 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4594 virtio_balloon_devaddr
= strdup(buf
);
4605 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4607 exit(STATUS_CONTROL_C_EXIT
);
4612 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4616 if(strlen(str
) != 36)
4619 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4620 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4621 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4627 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4633 #define MAX_NET_CLIENTS 32
4637 static void termsig_handler(int signal
)
4639 qemu_system_shutdown_request();
4642 static void sigchld_handler(int signal
)
4644 waitpid(-1, NULL
, WNOHANG
);
4647 static void sighandler_setup(void)
4649 struct sigaction act
;
4651 memset(&act
, 0, sizeof(act
));
4652 act
.sa_handler
= termsig_handler
;
4653 sigaction(SIGINT
, &act
, NULL
);
4654 sigaction(SIGHUP
, &act
, NULL
);
4655 sigaction(SIGTERM
, &act
, NULL
);
4657 act
.sa_handler
= sigchld_handler
;
4658 act
.sa_flags
= SA_NOCLDSTOP
;
4659 sigaction(SIGCHLD
, &act
, NULL
);
4665 /* Look for support files in the same directory as the executable. */
4666 static char *find_datadir(const char *argv0
)
4672 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4679 while (p
!= buf
&& *p
!= '\\')
4682 if (access(buf
, R_OK
) == 0) {
4683 return qemu_strdup(buf
);
4689 /* Find a likely location for support files using the location of the binary.
4690 For installed binaries this will be "$bindir/../share/qemu". When
4691 running from the build tree this will be "$bindir/../pc-bios". */
4692 #define SHARE_SUFFIX "/share/qemu"
4693 #define BUILD_SUFFIX "/pc-bios"
4694 static char *find_datadir(const char *argv0
)
4704 #if defined(__linux__)
4707 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4713 #elif defined(__FreeBSD__)
4716 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4723 /* If we don't have any way of figuring out the actual executable
4724 location then try argv[0]. */
4729 p
= realpath(argv0
, p
);
4737 max_len
= strlen(dir
) +
4738 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4739 res
= qemu_mallocz(max_len
);
4740 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4741 if (access(res
, R_OK
)) {
4742 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4743 if (access(res
, R_OK
)) {
4757 char *qemu_find_file(int type
, const char *name
)
4763 /* If name contains path separators then try it as a straight path. */
4764 if ((strchr(name
, '/') || strchr(name
, '\\'))
4765 && access(name
, R_OK
) == 0) {
4766 return strdup(name
);
4769 case QEMU_FILE_TYPE_BIOS
:
4772 case QEMU_FILE_TYPE_KEYMAP
:
4773 subdir
= "keymaps/";
4778 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4779 buf
= qemu_mallocz(len
);
4780 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4781 if (access(buf
, R_OK
)) {
4788 struct device_config
{
4790 DEV_GENERIC
, /* -device */
4791 DEV_USB
, /* -usbdevice */
4794 const char *cmdline
;
4795 TAILQ_ENTRY(device_config
) next
;
4797 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4799 static void add_device_config(int type
, const char *cmdline
)
4801 struct device_config
*conf
;
4803 conf
= qemu_mallocz(sizeof(*conf
));
4805 conf
->cmdline
= cmdline
;
4806 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4809 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4811 struct device_config
*conf
;
4814 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4815 if (conf
->type
!= type
)
4817 rc
= func(conf
->cmdline
);
4824 static int generic_parse(const char *cmdline
)
4828 dev
= qdev_device_add(cmdline
);
4834 int main(int argc
, char **argv
, char **envp
)
4836 const char *gdbstub_dev
= NULL
;
4837 uint32_t boot_devices_bitmap
= 0;
4839 int snapshot
, linux_boot
, net_boot
;
4840 const char *initrd_filename
;
4841 const char *kernel_filename
, *kernel_cmdline
;
4842 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4844 DisplayChangeListener
*dcl
;
4845 int cyls
, heads
, secs
, translation
;
4846 const char *net_clients
[MAX_NET_CLIENTS
];
4848 QemuOpts
*hda_opts
= NULL
;
4850 const char *r
, *optarg
;
4851 CharDriverState
*monitor_hd
= NULL
;
4852 const char *monitor_device
;
4853 const char *serial_devices
[MAX_SERIAL_PORTS
];
4854 int serial_device_index
;
4855 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4856 int parallel_device_index
;
4857 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4858 int virtio_console_index
;
4859 const char *loadvm
= NULL
;
4860 QEMUMachine
*machine
;
4861 const char *cpu_model
;
4866 const char *pid_file
= NULL
;
4867 const char *incoming
= NULL
;
4870 struct passwd
*pwd
= NULL
;
4871 const char *chroot_dir
= NULL
;
4872 const char *run_as
= NULL
;
4875 int show_vnc_port
= 0;
4877 qemu_cache_utils_init(envp
);
4879 LIST_INIT (&vm_change_state_head
);
4882 struct sigaction act
;
4883 sigfillset(&act
.sa_mask
);
4885 act
.sa_handler
= SIG_IGN
;
4886 sigaction(SIGPIPE
, &act
, NULL
);
4889 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4890 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4891 QEMU to run on a single CPU */
4896 h
= GetCurrentProcess();
4897 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4898 for(i
= 0; i
< 32; i
++) {
4899 if (mask
& (1 << i
))
4904 SetProcessAffinityMask(h
, mask
);
4910 module_call_init(MODULE_INIT_MACHINE
);
4911 machine
= find_default_machine();
4913 initrd_filename
= NULL
;
4916 kernel_filename
= NULL
;
4917 kernel_cmdline
= "";
4918 cyls
= heads
= secs
= 0;
4919 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4920 monitor_device
= "vc:80Cx24C";
4922 serial_devices
[0] = "vc:80Cx24C";
4923 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4924 serial_devices
[i
] = NULL
;
4925 serial_device_index
= 0;
4927 parallel_devices
[0] = "vc:80Cx24C";
4928 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4929 parallel_devices
[i
] = NULL
;
4930 parallel_device_index
= 0;
4932 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4933 virtio_consoles
[i
] = NULL
;
4934 virtio_console_index
= 0;
4936 for (i
= 0; i
< MAX_NODES
; i
++) {
4938 node_cpumask
[i
] = 0;
4948 register_watchdogs();
4956 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4958 const QEMUOption
*popt
;
4961 /* Treat --foo the same as -foo. */
4964 popt
= qemu_options
;
4967 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4971 if (!strcmp(popt
->name
, r
+ 1))
4975 if (popt
->flags
& HAS_ARG
) {
4976 if (optind
>= argc
) {
4977 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4981 optarg
= argv
[optind
++];
4986 switch(popt
->index
) {
4988 machine
= find_machine(optarg
);
4991 printf("Supported machines are:\n");
4992 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4994 printf("%-10s %s (alias of %s)\n",
4995 m
->alias
, m
->desc
, m
->name
);
4996 printf("%-10s %s%s\n",
4998 m
->is_default
? " (default)" : "");
5000 exit(*optarg
!= '?');
5003 case QEMU_OPTION_cpu
:
5004 /* hw initialization will check this */
5005 if (*optarg
== '?') {
5006 /* XXX: implement xxx_cpu_list for targets that still miss it */
5007 #if defined(cpu_list)
5008 cpu_list(stdout
, &fprintf
);
5015 case QEMU_OPTION_initrd
:
5016 initrd_filename
= optarg
;
5018 case QEMU_OPTION_hda
:
5020 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5022 hda_opts
= drive_add(optarg
, HD_ALIAS
5023 ",cyls=%d,heads=%d,secs=%d%s",
5024 0, cyls
, heads
, secs
,
5025 translation
== BIOS_ATA_TRANSLATION_LBA
?
5027 translation
== BIOS_ATA_TRANSLATION_NONE
?
5028 ",trans=none" : "");
5030 case QEMU_OPTION_hdb
:
5031 case QEMU_OPTION_hdc
:
5032 case QEMU_OPTION_hdd
:
5033 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5035 case QEMU_OPTION_drive
:
5036 drive_add(NULL
, "%s", optarg
);
5038 case QEMU_OPTION_mtdblock
:
5039 drive_add(optarg
, MTD_ALIAS
);
5041 case QEMU_OPTION_sd
:
5042 drive_add(optarg
, SD_ALIAS
);
5044 case QEMU_OPTION_pflash
:
5045 drive_add(optarg
, PFLASH_ALIAS
);
5047 case QEMU_OPTION_snapshot
:
5050 case QEMU_OPTION_hdachs
:
5054 cyls
= strtol(p
, (char **)&p
, 0);
5055 if (cyls
< 1 || cyls
> 16383)
5060 heads
= strtol(p
, (char **)&p
, 0);
5061 if (heads
< 1 || heads
> 16)
5066 secs
= strtol(p
, (char **)&p
, 0);
5067 if (secs
< 1 || secs
> 63)
5071 if (!strcmp(p
, "none"))
5072 translation
= BIOS_ATA_TRANSLATION_NONE
;
5073 else if (!strcmp(p
, "lba"))
5074 translation
= BIOS_ATA_TRANSLATION_LBA
;
5075 else if (!strcmp(p
, "auto"))
5076 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5079 } else if (*p
!= '\0') {
5081 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5084 if (hda_opts
!= NULL
) {
5086 snprintf(num
, sizeof(num
), "%d", cyls
);
5087 qemu_opt_set(hda_opts
, "cyls", num
);
5088 snprintf(num
, sizeof(num
), "%d", heads
);
5089 qemu_opt_set(hda_opts
, "heads", num
);
5090 snprintf(num
, sizeof(num
), "%d", secs
);
5091 qemu_opt_set(hda_opts
, "secs", num
);
5092 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5093 qemu_opt_set(hda_opts
, "trans", "lba");
5094 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5095 qemu_opt_set(hda_opts
, "trans", "none");
5099 case QEMU_OPTION_numa
:
5100 if (nb_numa_nodes
>= MAX_NODES
) {
5101 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5106 case QEMU_OPTION_nographic
:
5107 display_type
= DT_NOGRAPHIC
;
5109 #ifdef CONFIG_CURSES
5110 case QEMU_OPTION_curses
:
5111 display_type
= DT_CURSES
;
5114 case QEMU_OPTION_portrait
:
5117 case QEMU_OPTION_kernel
:
5118 kernel_filename
= optarg
;
5120 case QEMU_OPTION_append
:
5121 kernel_cmdline
= optarg
;
5123 case QEMU_OPTION_cdrom
:
5124 drive_add(optarg
, CDROM_ALIAS
);
5126 case QEMU_OPTION_boot
:
5128 static const char * const params
[] = {
5129 "order", "once", "menu", NULL
5131 char buf
[sizeof(boot_devices
)];
5132 char *standard_boot_devices
;
5135 if (!strchr(optarg
, '=')) {
5137 pstrcpy(buf
, sizeof(buf
), optarg
);
5138 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5140 "qemu: unknown boot parameter '%s' in '%s'\n",
5146 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5147 boot_devices_bitmap
= parse_bootdevices(buf
);
5148 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5151 if (get_param_value(buf
, sizeof(buf
),
5153 boot_devices_bitmap
|= parse_bootdevices(buf
);
5154 standard_boot_devices
= qemu_strdup(boot_devices
);
5155 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5156 qemu_register_reset(restore_boot_devices
,
5157 standard_boot_devices
);
5159 if (get_param_value(buf
, sizeof(buf
),
5161 if (!strcmp(buf
, "on")) {
5163 } else if (!strcmp(buf
, "off")) {
5167 "qemu: invalid option value '%s'\n",
5175 case QEMU_OPTION_fda
:
5176 case QEMU_OPTION_fdb
:
5177 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5180 case QEMU_OPTION_no_fd_bootchk
:
5184 case QEMU_OPTION_net
:
5185 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5186 fprintf(stderr
, "qemu: too many network clients\n");
5189 net_clients
[nb_net_clients
] = optarg
;
5193 case QEMU_OPTION_tftp
:
5194 legacy_tftp_prefix
= optarg
;
5196 case QEMU_OPTION_bootp
:
5197 legacy_bootp_filename
= optarg
;
5200 case QEMU_OPTION_smb
:
5201 net_slirp_smb(optarg
);
5204 case QEMU_OPTION_redir
:
5205 net_slirp_redir(optarg
);
5208 case QEMU_OPTION_bt
:
5209 add_device_config(DEV_BT
, optarg
);
5212 case QEMU_OPTION_audio_help
:
5216 case QEMU_OPTION_soundhw
:
5217 select_soundhw (optarg
);
5223 case QEMU_OPTION_version
:
5227 case QEMU_OPTION_m
: {
5231 value
= strtoul(optarg
, &ptr
, 10);
5233 case 0: case 'M': case 'm':
5240 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5244 /* On 32-bit hosts, QEMU is limited by virtual address space */
5245 if (value
> (2047 << 20)
5246 #ifndef CONFIG_KQEMU
5247 && HOST_LONG_BITS
== 32
5250 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5253 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5254 fprintf(stderr
, "qemu: ram size too large\n");
5263 const CPULogItem
*item
;
5265 mask
= cpu_str_to_log_mask(optarg
);
5267 printf("Log items (comma separated):\n");
5268 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5269 printf("%-10s %s\n", item
->name
, item
->help
);
5277 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5279 case QEMU_OPTION_gdb
:
5280 gdbstub_dev
= optarg
;
5285 case QEMU_OPTION_bios
:
5288 case QEMU_OPTION_singlestep
:
5296 keyboard_layout
= optarg
;
5299 case QEMU_OPTION_localtime
:
5302 case QEMU_OPTION_vga
:
5303 select_vgahw (optarg
);
5305 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5311 w
= strtol(p
, (char **)&p
, 10);
5314 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5320 h
= strtol(p
, (char **)&p
, 10);
5325 depth
= strtol(p
, (char **)&p
, 10);
5326 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5327 depth
!= 24 && depth
!= 32)
5329 } else if (*p
== '\0') {
5330 depth
= graphic_depth
;
5337 graphic_depth
= depth
;
5341 case QEMU_OPTION_echr
:
5344 term_escape_char
= strtol(optarg
, &r
, 0);
5346 printf("Bad argument to echr\n");
5349 case QEMU_OPTION_monitor
:
5350 monitor_device
= optarg
;
5352 case QEMU_OPTION_serial
:
5353 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5354 fprintf(stderr
, "qemu: too many serial ports\n");
5357 serial_devices
[serial_device_index
] = optarg
;
5358 serial_device_index
++;
5360 case QEMU_OPTION_watchdog
:
5361 i
= select_watchdog(optarg
);
5363 exit (i
== 1 ? 1 : 0);
5365 case QEMU_OPTION_watchdog_action
:
5366 if (select_watchdog_action(optarg
) == -1) {
5367 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5371 case QEMU_OPTION_virtiocon
:
5372 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5373 fprintf(stderr
, "qemu: too many virtio consoles\n");
5376 virtio_consoles
[virtio_console_index
] = optarg
;
5377 virtio_console_index
++;
5379 case QEMU_OPTION_parallel
:
5380 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5381 fprintf(stderr
, "qemu: too many parallel ports\n");
5384 parallel_devices
[parallel_device_index
] = optarg
;
5385 parallel_device_index
++;
5387 case QEMU_OPTION_loadvm
:
5390 case QEMU_OPTION_full_screen
:
5394 case QEMU_OPTION_no_frame
:
5397 case QEMU_OPTION_alt_grab
:
5400 case QEMU_OPTION_no_quit
:
5403 case QEMU_OPTION_sdl
:
5404 display_type
= DT_SDL
;
5407 case QEMU_OPTION_pidfile
:
5411 case QEMU_OPTION_win2k_hack
:
5412 win2k_install_hack
= 1;
5414 case QEMU_OPTION_rtc_td_hack
:
5417 case QEMU_OPTION_acpitable
:
5418 if(acpi_table_add(optarg
) < 0) {
5419 fprintf(stderr
, "Wrong acpi table provided\n");
5423 case QEMU_OPTION_smbios
:
5424 if(smbios_entry_add(optarg
) < 0) {
5425 fprintf(stderr
, "Wrong smbios provided\n");
5431 case QEMU_OPTION_enable_kqemu
:
5434 case QEMU_OPTION_kernel_kqemu
:
5439 case QEMU_OPTION_enable_kvm
:
5446 case QEMU_OPTION_usb
:
5449 case QEMU_OPTION_usbdevice
:
5451 add_device_config(DEV_USB
, optarg
);
5453 case QEMU_OPTION_device
:
5454 add_device_config(DEV_GENERIC
, optarg
);
5456 case QEMU_OPTION_smp
:
5460 smp_cpus
= strtol(optarg
, &p
, 10);
5462 fprintf(stderr
, "Invalid number of CPUs\n");
5467 if (get_param_value(option
, 128, "maxcpus", p
))
5468 max_cpus
= strtol(option
, NULL
, 0);
5469 if (max_cpus
< smp_cpus
) {
5470 fprintf(stderr
, "maxcpus must be equal to or greater than "
5474 if (max_cpus
> 255) {
5475 fprintf(stderr
, "Unsupported number of maxcpus\n");
5480 case QEMU_OPTION_vnc
:
5481 display_type
= DT_VNC
;
5482 vnc_display
= optarg
;
5485 case QEMU_OPTION_no_acpi
:
5488 case QEMU_OPTION_no_hpet
:
5491 case QEMU_OPTION_balloon
:
5492 if (balloon_parse(optarg
) < 0) {
5493 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5498 case QEMU_OPTION_no_reboot
:
5501 case QEMU_OPTION_no_shutdown
:
5504 case QEMU_OPTION_show_cursor
:
5507 case QEMU_OPTION_uuid
:
5508 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5509 fprintf(stderr
, "Fail to parse UUID string."
5510 " Wrong format.\n");
5515 case QEMU_OPTION_daemonize
:
5519 case QEMU_OPTION_option_rom
:
5520 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5521 fprintf(stderr
, "Too many option ROMs\n");
5524 option_rom
[nb_option_roms
] = optarg
;
5527 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5528 case QEMU_OPTION_semihosting
:
5529 semihosting_enabled
= 1;
5532 case QEMU_OPTION_name
:
5533 qemu_name
= qemu_strdup(optarg
);
5535 char *p
= strchr(qemu_name
, ',');
5538 if (strncmp(p
, "process=", 8)) {
5539 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5547 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5548 case QEMU_OPTION_prom_env
:
5549 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5550 fprintf(stderr
, "Too many prom variables\n");
5553 prom_envs
[nb_prom_envs
] = optarg
;
5558 case QEMU_OPTION_old_param
:
5562 case QEMU_OPTION_clock
:
5563 configure_alarms(optarg
);
5565 case QEMU_OPTION_startdate
:
5568 time_t rtc_start_date
;
5569 if (!strcmp(optarg
, "now")) {
5570 rtc_date_offset
= -1;
5572 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5580 } else if (sscanf(optarg
, "%d-%d-%d",
5583 &tm
.tm_mday
) == 3) {
5592 rtc_start_date
= mktimegm(&tm
);
5593 if (rtc_start_date
== -1) {
5595 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5596 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5599 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5603 case QEMU_OPTION_tb_size
:
5604 tb_size
= strtol(optarg
, NULL
, 0);
5608 case QEMU_OPTION_icount
:
5610 if (strcmp(optarg
, "auto") == 0) {
5611 icount_time_shift
= -1;
5613 icount_time_shift
= strtol(optarg
, NULL
, 0);
5616 case QEMU_OPTION_incoming
:
5620 case QEMU_OPTION_chroot
:
5621 chroot_dir
= optarg
;
5623 case QEMU_OPTION_runas
:
5628 case QEMU_OPTION_xen_domid
:
5629 xen_domid
= atoi(optarg
);
5631 case QEMU_OPTION_xen_create
:
5632 xen_mode
= XEN_CREATE
;
5634 case QEMU_OPTION_xen_attach
:
5635 xen_mode
= XEN_ATTACH
;
5642 /* If no data_dir is specified then try to find it relative to the
5645 data_dir
= find_datadir(argv
[0]);
5647 /* If all else fails use the install patch specified when building. */
5649 data_dir
= CONFIG_QEMU_SHAREDIR
;
5652 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5653 if (kvm_allowed
&& kqemu_allowed
) {
5655 "You can not enable both KVM and kqemu at the same time\n");
5661 * Default to max_cpus = smp_cpus, in case the user doesn't
5662 * specify a max_cpus value.
5665 max_cpus
= smp_cpus
;
5667 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5668 if (smp_cpus
> machine
->max_cpus
) {
5669 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5670 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5675 if (display_type
== DT_NOGRAPHIC
) {
5676 if (serial_device_index
== 0)
5677 serial_devices
[0] = "stdio";
5678 if (parallel_device_index
== 0)
5679 parallel_devices
[0] = "null";
5680 if (strncmp(monitor_device
, "vc", 2) == 0)
5681 monitor_device
= "stdio";
5688 if (pipe(fds
) == -1)
5699 len
= read(fds
[0], &status
, 1);
5700 if (len
== -1 && (errno
== EINTR
))
5705 else if (status
== 1) {
5706 fprintf(stderr
, "Could not acquire pidfile\n");
5723 signal(SIGTSTP
, SIG_IGN
);
5724 signal(SIGTTOU
, SIG_IGN
);
5725 signal(SIGTTIN
, SIG_IGN
);
5728 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5731 write(fds
[1], &status
, 1);
5733 fprintf(stderr
, "Could not acquire pid file\n");
5742 if (qemu_init_main_loop()) {
5743 fprintf(stderr
, "qemu_init_main_loop failed\n");
5746 linux_boot
= (kernel_filename
!= NULL
);
5748 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5749 fprintf(stderr
, "-append only allowed with -kernel option\n");
5753 if (!linux_boot
&& initrd_filename
!= NULL
) {
5754 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5758 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5761 if (init_timer_alarm() < 0) {
5762 fprintf(stderr
, "could not initialize alarm timer\n");
5765 if (use_icount
&& icount_time_shift
< 0) {
5767 /* 125MIPS seems a reasonable initial guess at the guest speed.
5768 It will be corrected fairly quickly anyway. */
5769 icount_time_shift
= 3;
5770 init_icount_adjust();
5777 /* init network clients */
5778 if (nb_net_clients
== 0) {
5779 /* if no clients, we use a default config */
5780 net_clients
[nb_net_clients
++] = "nic";
5782 net_clients
[nb_net_clients
++] = "user";
5786 for(i
= 0;i
< nb_net_clients
; i
++) {
5787 if (net_client_parse(net_clients
[i
]) < 0)
5791 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5792 net_set_boot_mask(net_boot
);
5796 /* init the bluetooth world */
5797 if (foreach_device_config(DEV_BT
, bt_parse
))
5800 /* init the memory */
5802 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5805 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5806 guest ram allocation. It needs to go away. */
5807 if (kqemu_allowed
) {
5808 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5809 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5810 if (!kqemu_phys_ram_base
) {
5811 fprintf(stderr
, "Could not allocate physical memory\n");
5817 /* init the dynamic translator */
5818 cpu_exec_init_all(tb_size
* 1024 * 1024);
5822 /* we always create the cdrom drive, even if no disk is there */
5823 drive_add(NULL
, CDROM_ALIAS
);
5825 /* we always create at least one floppy */
5826 drive_add(NULL
, FD_ALIAS
, 0);
5828 /* we always create one sd slot, even if no card is in it */
5829 drive_add(NULL
, SD_ALIAS
);
5831 /* open the virtual block devices */
5833 qemu_opts_foreach(&drive_opt_list
, drive_enable_snapshot
, NULL
, 0);
5834 if (qemu_opts_foreach(&drive_opt_list
, drive_init_func
, machine
, 1) != 0)
5837 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5838 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5841 /* must be after terminal init, SDL library changes signal handlers */
5845 /* Maintain compatibility with multiple stdio monitors */
5846 if (!strcmp(monitor_device
,"stdio")) {
5847 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5848 const char *devname
= serial_devices
[i
];
5849 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5850 monitor_device
= NULL
;
5852 } else if (devname
&& !strcmp(devname
,"stdio")) {
5853 monitor_device
= NULL
;
5854 serial_devices
[i
] = "mon:stdio";
5860 if (nb_numa_nodes
> 0) {
5863 if (nb_numa_nodes
> smp_cpus
) {
5864 nb_numa_nodes
= smp_cpus
;
5867 /* If no memory size if given for any node, assume the default case
5868 * and distribute the available memory equally across all nodes
5870 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5871 if (node_mem
[i
] != 0)
5874 if (i
== nb_numa_nodes
) {
5875 uint64_t usedmem
= 0;
5877 /* On Linux, the each node's border has to be 8MB aligned,
5878 * the final node gets the rest.
5880 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5881 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5882 usedmem
+= node_mem
[i
];
5884 node_mem
[i
] = ram_size
- usedmem
;
5887 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5888 if (node_cpumask
[i
] != 0)
5891 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5892 * must cope with this anyway, because there are BIOSes out there in
5893 * real machines which also use this scheme.
5895 if (i
== nb_numa_nodes
) {
5896 for (i
= 0; i
< smp_cpus
; i
++) {
5897 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5902 if (kvm_enabled()) {
5905 ret
= kvm_init(smp_cpus
);
5907 fprintf(stderr
, "failed to initialize KVM\n");
5912 if (monitor_device
) {
5913 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5915 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5920 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5921 const char *devname
= serial_devices
[i
];
5922 if (devname
&& strcmp(devname
, "none")) {
5924 snprintf(label
, sizeof(label
), "serial%d", i
);
5925 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5926 if (!serial_hds
[i
]) {
5927 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5934 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5935 const char *devname
= parallel_devices
[i
];
5936 if (devname
&& strcmp(devname
, "none")) {
5938 snprintf(label
, sizeof(label
), "parallel%d", i
);
5939 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5940 if (!parallel_hds
[i
]) {
5941 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5948 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5949 const char *devname
= virtio_consoles
[i
];
5950 if (devname
&& strcmp(devname
, "none")) {
5952 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5953 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5954 if (!virtcon_hds
[i
]) {
5955 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5962 module_call_init(MODULE_INIT_DEVICE
);
5964 if (machine
->compat_props
) {
5965 qdev_prop_register_compat(machine
->compat_props
);
5967 machine
->init(ram_size
, boot_devices
,
5968 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5971 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5972 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5973 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5979 current_machine
= machine
;
5981 /* init USB devices */
5983 foreach_device_config(DEV_USB
, usb_parse
);
5986 /* init generic devices */
5987 if (foreach_device_config(DEV_GENERIC
, generic_parse
))
5991 dumb_display_init();
5992 /* just use the first displaystate for the moment */
5995 if (display_type
== DT_DEFAULT
) {
5996 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5997 display_type
= DT_SDL
;
5999 display_type
= DT_VNC
;
6000 vnc_display
= "localhost:0,to=99";
6006 switch (display_type
) {
6009 #if defined(CONFIG_CURSES)
6011 curses_display_init(ds
, full_screen
);
6014 #if defined(CONFIG_SDL)
6016 sdl_display_init(ds
, full_screen
, no_frame
);
6018 #elif defined(CONFIG_COCOA)
6020 cocoa_display_init(ds
, full_screen
);
6024 vnc_display_init(ds
);
6025 if (vnc_display_open(ds
, vnc_display
) < 0)
6028 if (show_vnc_port
) {
6029 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6037 dcl
= ds
->listeners
;
6038 while (dcl
!= NULL
) {
6039 if (dcl
->dpy_refresh
!= NULL
) {
6040 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6041 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6046 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6047 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6048 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6051 text_consoles_set_display(display_state
);
6052 qemu_chr_initial_reset();
6054 if (monitor_device
&& monitor_hd
)
6055 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6057 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6058 const char *devname
= serial_devices
[i
];
6059 if (devname
&& strcmp(devname
, "none")) {
6060 if (strstart(devname
, "vc", 0))
6061 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6065 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6066 const char *devname
= parallel_devices
[i
];
6067 if (devname
&& strcmp(devname
, "none")) {
6068 if (strstart(devname
, "vc", 0))
6069 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6073 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6074 const char *devname
= virtio_consoles
[i
];
6075 if (virtcon_hds
[i
] && devname
) {
6076 if (strstart(devname
, "vc", 0))
6077 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6081 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6082 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6088 do_loadvm(cur_mon
, loadvm
);
6091 qemu_start_incoming_migration(incoming
);
6102 len
= write(fds
[1], &status
, 1);
6103 if (len
== -1 && (errno
== EINTR
))
6110 TFR(fd
= open("/dev/null", O_RDWR
));
6116 pwd
= getpwnam(run_as
);
6118 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6124 if (chroot(chroot_dir
) < 0) {
6125 fprintf(stderr
, "chroot failed\n");
6132 if (setgid(pwd
->pw_gid
) < 0) {
6133 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6136 if (setuid(pwd
->pw_uid
) < 0) {
6137 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6140 if (setuid(0) != -1) {
6141 fprintf(stderr
, "Dropping privileges failed\n");