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;
225 const char *vnc_display
;
226 int acpi_enabled
= 1;
228 int virtio_balloon
= 1;
229 const char *virtio_balloon_devaddr
;
234 int graphic_rotate
= 0;
238 WatchdogTimerModel
*watchdog
= NULL
;
239 int watchdog_action
= WDT_RESET
;
240 const char *option_rom
[MAX_OPTION_ROMS
];
242 int semihosting_enabled
= 0;
246 const char *qemu_name
;
248 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
249 unsigned int nb_prom_envs
= 0;
250 const char *prom_envs
[MAX_PROM_ENVS
];
255 uint64_t node_mem
[MAX_NODES
];
256 uint64_t node_cpumask
[MAX_NODES
];
258 static CPUState
*cur_cpu
;
259 static CPUState
*next_cpu
;
260 static int timer_alarm_pending
= 1;
261 /* Conversion factor from emulated instructions to virtual clock ticks. */
262 static int icount_time_shift
;
263 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
264 #define MAX_ICOUNT_SHIFT 10
265 /* Compensate for varying guest execution speed. */
266 static int64_t qemu_icount_bias
;
267 static QEMUTimer
*icount_rt_timer
;
268 static QEMUTimer
*icount_vm_timer
;
269 static QEMUTimer
*nographic_timer
;
271 uint8_t qemu_uuid
[16];
273 static QEMUBootSetHandler
*boot_set_handler
;
274 static void *boot_set_opaque
;
276 /***********************************************************/
277 /* x86 ISA bus support */
279 target_phys_addr_t isa_mem_base
= 0;
282 /***********************************************************/
283 void hw_error(const char *fmt
, ...)
289 fprintf(stderr
, "qemu: hardware error: ");
290 vfprintf(stderr
, fmt
, ap
);
291 fprintf(stderr
, "\n");
292 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
293 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
295 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
297 cpu_dump_state(env
, stderr
, fprintf
, 0);
304 static void set_proc_name(const char *s
)
310 name
[sizeof(name
) - 1] = 0;
311 strncpy(name
, s
, sizeof(name
));
312 /* Could rewrite argv[0] too, but that's a bit more complicated.
313 This simple way is enough for `top'. */
314 prctl(PR_SET_NAME
, name
);
321 static QEMUBalloonEvent
*qemu_balloon_event
;
322 void *qemu_balloon_event_opaque
;
324 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
326 qemu_balloon_event
= func
;
327 qemu_balloon_event_opaque
= opaque
;
330 void qemu_balloon(ram_addr_t target
)
332 if (qemu_balloon_event
)
333 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
336 ram_addr_t
qemu_balloon_status(void)
338 if (qemu_balloon_event
)
339 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
343 /***********************************************************/
346 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
347 static void *qemu_put_kbd_event_opaque
;
348 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
349 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
351 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
353 qemu_put_kbd_event_opaque
= opaque
;
354 qemu_put_kbd_event
= func
;
357 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
358 void *opaque
, int absolute
,
361 QEMUPutMouseEntry
*s
, *cursor
;
363 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
365 s
->qemu_put_mouse_event
= func
;
366 s
->qemu_put_mouse_event_opaque
= opaque
;
367 s
->qemu_put_mouse_event_absolute
= absolute
;
368 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
371 if (!qemu_put_mouse_event_head
) {
372 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
376 cursor
= qemu_put_mouse_event_head
;
377 while (cursor
->next
!= NULL
)
378 cursor
= cursor
->next
;
381 qemu_put_mouse_event_current
= s
;
386 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
388 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
390 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
393 cursor
= qemu_put_mouse_event_head
;
394 while (cursor
!= NULL
&& cursor
!= entry
) {
396 cursor
= cursor
->next
;
399 if (cursor
== NULL
) // does not exist or list empty
401 else if (prev
== NULL
) { // entry is head
402 qemu_put_mouse_event_head
= cursor
->next
;
403 if (qemu_put_mouse_event_current
== entry
)
404 qemu_put_mouse_event_current
= cursor
->next
;
405 qemu_free(entry
->qemu_put_mouse_event_name
);
410 prev
->next
= entry
->next
;
412 if (qemu_put_mouse_event_current
== entry
)
413 qemu_put_mouse_event_current
= prev
;
415 qemu_free(entry
->qemu_put_mouse_event_name
);
419 void kbd_put_keycode(int keycode
)
421 if (qemu_put_kbd_event
) {
422 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
426 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
428 QEMUPutMouseEvent
*mouse_event
;
429 void *mouse_event_opaque
;
432 if (!qemu_put_mouse_event_current
) {
437 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
439 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
442 if (graphic_rotate
) {
443 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
446 width
= graphic_width
- 1;
447 mouse_event(mouse_event_opaque
,
448 width
- dy
, dx
, dz
, buttons_state
);
450 mouse_event(mouse_event_opaque
,
451 dx
, dy
, dz
, buttons_state
);
455 int kbd_mouse_is_absolute(void)
457 if (!qemu_put_mouse_event_current
)
460 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
463 void do_info_mice(Monitor
*mon
)
465 QEMUPutMouseEntry
*cursor
;
468 if (!qemu_put_mouse_event_head
) {
469 monitor_printf(mon
, "No mouse devices connected\n");
473 monitor_printf(mon
, "Mouse devices available:\n");
474 cursor
= qemu_put_mouse_event_head
;
475 while (cursor
!= NULL
) {
476 monitor_printf(mon
, "%c Mouse #%d: %s\n",
477 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
478 index
, cursor
->qemu_put_mouse_event_name
);
480 cursor
= cursor
->next
;
484 void do_mouse_set(Monitor
*mon
, int index
)
486 QEMUPutMouseEntry
*cursor
;
489 if (!qemu_put_mouse_event_head
) {
490 monitor_printf(mon
, "No mouse devices connected\n");
494 cursor
= qemu_put_mouse_event_head
;
495 while (cursor
!= NULL
&& index
!= i
) {
497 cursor
= cursor
->next
;
501 qemu_put_mouse_event_current
= cursor
;
503 monitor_printf(mon
, "Mouse at given index not found\n");
506 /* compute with 96 bit intermediate result: (a*b)/c */
507 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
512 #ifdef WORDS_BIGENDIAN
522 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
523 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
526 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
530 /***********************************************************/
531 /* real time host monotonic timer */
533 #define QEMU_TIMER_BASE 1000000000LL
537 static int64_t clock_freq
;
539 static void init_get_clock(void)
543 ret
= QueryPerformanceFrequency(&freq
);
545 fprintf(stderr
, "Could not calibrate ticks\n");
548 clock_freq
= freq
.QuadPart
;
551 static int64_t get_clock(void)
554 QueryPerformanceCounter(&ti
);
555 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
560 static int use_rt_clock
;
562 static void init_get_clock(void)
565 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
566 || defined(__DragonFly__)
569 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
576 static int64_t get_clock(void)
578 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
579 || defined(__DragonFly__)
582 clock_gettime(CLOCK_MONOTONIC
, &ts
);
583 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
587 /* XXX: using gettimeofday leads to problems if the date
588 changes, so it should be avoided. */
590 gettimeofday(&tv
, NULL
);
591 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
596 /* Return the virtual CPU time, based on the instruction counter. */
597 static int64_t cpu_get_icount(void)
600 CPUState
*env
= cpu_single_env
;;
601 icount
= qemu_icount
;
604 fprintf(stderr
, "Bad clock read\n");
605 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
607 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
610 /***********************************************************/
611 /* guest cycle counter */
613 static int64_t cpu_ticks_prev
;
614 static int64_t cpu_ticks_offset
;
615 static int64_t cpu_clock_offset
;
616 static int cpu_ticks_enabled
;
618 /* return the host CPU cycle counter and handle stop/restart */
619 int64_t cpu_get_ticks(void)
622 return cpu_get_icount();
624 if (!cpu_ticks_enabled
) {
625 return cpu_ticks_offset
;
628 ticks
= cpu_get_real_ticks();
629 if (cpu_ticks_prev
> ticks
) {
630 /* Note: non increasing ticks may happen if the host uses
632 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
634 cpu_ticks_prev
= ticks
;
635 return ticks
+ cpu_ticks_offset
;
639 /* return the host CPU monotonic timer and handle stop/restart */
640 static int64_t cpu_get_clock(void)
643 if (!cpu_ticks_enabled
) {
644 return cpu_clock_offset
;
647 return ti
+ cpu_clock_offset
;
651 /* enable cpu_get_ticks() */
652 void cpu_enable_ticks(void)
654 if (!cpu_ticks_enabled
) {
655 cpu_ticks_offset
-= cpu_get_real_ticks();
656 cpu_clock_offset
-= get_clock();
657 cpu_ticks_enabled
= 1;
661 /* disable cpu_get_ticks() : the clock is stopped. You must not call
662 cpu_get_ticks() after that. */
663 void cpu_disable_ticks(void)
665 if (cpu_ticks_enabled
) {
666 cpu_ticks_offset
= cpu_get_ticks();
667 cpu_clock_offset
= cpu_get_clock();
668 cpu_ticks_enabled
= 0;
672 /***********************************************************/
675 #define QEMU_TIMER_REALTIME 0
676 #define QEMU_TIMER_VIRTUAL 1
680 /* XXX: add frequency */
688 struct QEMUTimer
*next
;
691 struct qemu_alarm_timer
{
695 int (*start
)(struct qemu_alarm_timer
*t
);
696 void (*stop
)(struct qemu_alarm_timer
*t
);
697 void (*rearm
)(struct qemu_alarm_timer
*t
);
701 #define ALARM_FLAG_DYNTICKS 0x1
702 #define ALARM_FLAG_EXPIRED 0x2
704 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
706 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
709 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
711 if (!alarm_has_dynticks(t
))
717 /* TODO: MIN_TIMER_REARM_US should be optimized */
718 #define MIN_TIMER_REARM_US 250
720 static struct qemu_alarm_timer
*alarm_timer
;
724 struct qemu_alarm_win32
{
727 } alarm_win32_data
= {0, -1};
729 static int win32_start_timer(struct qemu_alarm_timer
*t
);
730 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
731 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
735 static int unix_start_timer(struct qemu_alarm_timer
*t
);
736 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
740 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
741 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
742 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
744 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
745 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
747 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
748 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
750 #endif /* __linux__ */
754 /* Correlation between real and virtual time is always going to be
755 fairly approximate, so ignore small variation.
756 When the guest is idle real and virtual time will be aligned in
758 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
760 static void icount_adjust(void)
765 static int64_t last_delta
;
766 /* If the VM is not running, then do nothing. */
770 cur_time
= cpu_get_clock();
771 cur_icount
= qemu_get_clock(vm_clock
);
772 delta
= cur_icount
- cur_time
;
773 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
775 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
776 && icount_time_shift
> 0) {
777 /* The guest is getting too far ahead. Slow time down. */
781 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
782 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
783 /* The guest is getting too far behind. Speed time up. */
787 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
790 static void icount_adjust_rt(void * opaque
)
792 qemu_mod_timer(icount_rt_timer
,
793 qemu_get_clock(rt_clock
) + 1000);
797 static void icount_adjust_vm(void * opaque
)
799 qemu_mod_timer(icount_vm_timer
,
800 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
804 static void init_icount_adjust(void)
806 /* Have both realtime and virtual time triggers for speed adjustment.
807 The realtime trigger catches emulated time passing too slowly,
808 the virtual time trigger catches emulated time passing too fast.
809 Realtime triggers occur even when idle, so use them less frequently
811 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
812 qemu_mod_timer(icount_rt_timer
,
813 qemu_get_clock(rt_clock
) + 1000);
814 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
815 qemu_mod_timer(icount_vm_timer
,
816 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
819 static struct qemu_alarm_timer alarm_timers
[] = {
822 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
823 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
824 /* HPET - if available - is preferred */
825 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
826 /* ...otherwise try RTC */
827 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
829 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
831 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
832 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
833 {"win32", 0, win32_start_timer
,
834 win32_stop_timer
, NULL
, &alarm_win32_data
},
839 static void show_available_alarms(void)
843 printf("Available alarm timers, in order of precedence:\n");
844 for (i
= 0; alarm_timers
[i
].name
; i
++)
845 printf("%s\n", alarm_timers
[i
].name
);
848 static void configure_alarms(char const *opt
)
852 int count
= ARRAY_SIZE(alarm_timers
) - 1;
855 struct qemu_alarm_timer tmp
;
857 if (!strcmp(opt
, "?")) {
858 show_available_alarms();
864 /* Reorder the array */
865 name
= strtok(arg
, ",");
867 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
868 if (!strcmp(alarm_timers
[i
].name
, name
))
873 fprintf(stderr
, "Unknown clock %s\n", name
);
882 tmp
= alarm_timers
[i
];
883 alarm_timers
[i
] = alarm_timers
[cur
];
884 alarm_timers
[cur
] = tmp
;
888 name
= strtok(NULL
, ",");
894 /* Disable remaining timers */
895 for (i
= cur
; i
< count
; i
++)
896 alarm_timers
[i
].name
= NULL
;
898 show_available_alarms();
906 static QEMUTimer
*active_timers
[2];
908 static QEMUClock
*qemu_new_clock(int type
)
911 clock
= qemu_mallocz(sizeof(QEMUClock
));
916 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
920 ts
= qemu_mallocz(sizeof(QEMUTimer
));
927 void qemu_free_timer(QEMUTimer
*ts
)
932 /* stop a timer, but do not dealloc it */
933 void qemu_del_timer(QEMUTimer
*ts
)
937 /* NOTE: this code must be signal safe because
938 qemu_timer_expired() can be called from a signal. */
939 pt
= &active_timers
[ts
->clock
->type
];
952 /* modify the current timer so that it will be fired when current_time
953 >= expire_time. The corresponding callback will be called. */
954 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
960 /* add the timer in the sorted list */
961 /* NOTE: this code must be signal safe because
962 qemu_timer_expired() can be called from a signal. */
963 pt
= &active_timers
[ts
->clock
->type
];
968 if (t
->expire_time
> expire_time
)
972 ts
->expire_time
= expire_time
;
976 /* Rearm if necessary */
977 if (pt
== &active_timers
[ts
->clock
->type
]) {
978 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
979 qemu_rearm_alarm_timer(alarm_timer
);
981 /* Interrupt execution to force deadline recalculation. */
987 int qemu_timer_pending(QEMUTimer
*ts
)
990 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
997 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
1001 return (timer_head
->expire_time
<= current_time
);
1004 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1010 if (!ts
|| ts
->expire_time
> current_time
)
1012 /* remove timer from the list before calling the callback */
1013 *ptimer_head
= ts
->next
;
1016 /* run the callback (the timer list can be modified) */
1021 int64_t qemu_get_clock(QEMUClock
*clock
)
1023 switch(clock
->type
) {
1024 case QEMU_TIMER_REALTIME
:
1025 return get_clock() / 1000000;
1027 case QEMU_TIMER_VIRTUAL
:
1029 return cpu_get_icount();
1031 return cpu_get_clock();
1036 static void init_timers(void)
1039 ticks_per_sec
= QEMU_TIMER_BASE
;
1040 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1041 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1045 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1047 uint64_t expire_time
;
1049 if (qemu_timer_pending(ts
)) {
1050 expire_time
= ts
->expire_time
;
1054 qemu_put_be64(f
, expire_time
);
1057 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1059 uint64_t expire_time
;
1061 expire_time
= qemu_get_be64(f
);
1062 if (expire_time
!= -1) {
1063 qemu_mod_timer(ts
, expire_time
);
1069 static void timer_save(QEMUFile
*f
, void *opaque
)
1071 if (cpu_ticks_enabled
) {
1072 hw_error("cannot save state if virtual timers are running");
1074 qemu_put_be64(f
, cpu_ticks_offset
);
1075 qemu_put_be64(f
, ticks_per_sec
);
1076 qemu_put_be64(f
, cpu_clock_offset
);
1079 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1081 if (version_id
!= 1 && version_id
!= 2)
1083 if (cpu_ticks_enabled
) {
1086 cpu_ticks_offset
=qemu_get_be64(f
);
1087 ticks_per_sec
=qemu_get_be64(f
);
1088 if (version_id
== 2) {
1089 cpu_clock_offset
=qemu_get_be64(f
);
1094 static void qemu_event_increment(void);
1097 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1098 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1101 static void host_alarm_handler(int host_signum
)
1105 #define DISP_FREQ 1000
1107 static int64_t delta_min
= INT64_MAX
;
1108 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1110 ti
= qemu_get_clock(vm_clock
);
1111 if (last_clock
!= 0) {
1112 delta
= ti
- last_clock
;
1113 if (delta
< delta_min
)
1115 if (delta
> delta_max
)
1118 if (++count
== DISP_FREQ
) {
1119 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1120 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1121 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1122 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1123 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1125 delta_min
= INT64_MAX
;
1133 if (alarm_has_dynticks(alarm_timer
) ||
1135 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1136 qemu_get_clock(vm_clock
))) ||
1137 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1138 qemu_get_clock(rt_clock
))) {
1139 qemu_event_increment();
1140 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1142 #ifndef CONFIG_IOTHREAD
1144 /* stop the currently executing cpu because a timer occured */
1147 if (next_cpu
->kqemu_enabled
) {
1148 kqemu_cpu_interrupt(next_cpu
);
1153 timer_alarm_pending
= 1;
1154 qemu_notify_event();
1158 static int64_t qemu_next_deadline(void)
1162 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1163 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1164 qemu_get_clock(vm_clock
);
1166 /* To avoid problems with overflow limit this to 2^32. */
1176 #if defined(__linux__) || defined(_WIN32)
1177 static uint64_t qemu_next_deadline_dyntick(void)
1185 delta
= (qemu_next_deadline() + 999) / 1000;
1187 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1188 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1189 qemu_get_clock(rt_clock
))*1000;
1190 if (rtdelta
< delta
)
1194 if (delta
< MIN_TIMER_REARM_US
)
1195 delta
= MIN_TIMER_REARM_US
;
1203 /* Sets a specific flag */
1204 static int fcntl_setfl(int fd
, int flag
)
1208 flags
= fcntl(fd
, F_GETFL
);
1212 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1218 #if defined(__linux__)
1220 #define RTC_FREQ 1024
1222 static void enable_sigio_timer(int fd
)
1224 struct sigaction act
;
1227 sigfillset(&act
.sa_mask
);
1229 act
.sa_handler
= host_alarm_handler
;
1231 sigaction(SIGIO
, &act
, NULL
);
1232 fcntl_setfl(fd
, O_ASYNC
);
1233 fcntl(fd
, F_SETOWN
, getpid());
1236 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1238 struct hpet_info info
;
1241 fd
= open("/dev/hpet", O_RDONLY
);
1246 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1248 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1249 "error, but for better emulation accuracy type:\n"
1250 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1254 /* Check capabilities */
1255 r
= ioctl(fd
, HPET_INFO
, &info
);
1259 /* Enable periodic mode */
1260 r
= ioctl(fd
, HPET_EPI
, 0);
1261 if (info
.hi_flags
&& (r
< 0))
1264 /* Enable interrupt */
1265 r
= ioctl(fd
, HPET_IE_ON
, 0);
1269 enable_sigio_timer(fd
);
1270 t
->priv
= (void *)(long)fd
;
1278 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1280 int fd
= (long)t
->priv
;
1285 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1288 unsigned long current_rtc_freq
= 0;
1290 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1293 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1294 if (current_rtc_freq
!= RTC_FREQ
&&
1295 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1296 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1297 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1298 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1301 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1307 enable_sigio_timer(rtc_fd
);
1309 t
->priv
= (void *)(long)rtc_fd
;
1314 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1316 int rtc_fd
= (long)t
->priv
;
1321 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1325 struct sigaction act
;
1327 sigfillset(&act
.sa_mask
);
1329 act
.sa_handler
= host_alarm_handler
;
1331 sigaction(SIGALRM
, &act
, NULL
);
1334 * Initialize ev struct to 0 to avoid valgrind complaining
1335 * about uninitialized data in timer_create call
1337 memset(&ev
, 0, sizeof(ev
));
1338 ev
.sigev_value
.sival_int
= 0;
1339 ev
.sigev_notify
= SIGEV_SIGNAL
;
1340 ev
.sigev_signo
= SIGALRM
;
1342 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1343 perror("timer_create");
1345 /* disable dynticks */
1346 fprintf(stderr
, "Dynamic Ticks disabled\n");
1351 t
->priv
= (void *)(long)host_timer
;
1356 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1358 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1360 timer_delete(host_timer
);
1363 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1365 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1366 struct itimerspec timeout
;
1367 int64_t nearest_delta_us
= INT64_MAX
;
1370 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1371 !active_timers
[QEMU_TIMER_VIRTUAL
])
1374 nearest_delta_us
= qemu_next_deadline_dyntick();
1376 /* check whether a timer is already running */
1377 if (timer_gettime(host_timer
, &timeout
)) {
1379 fprintf(stderr
, "Internal timer error: aborting\n");
1382 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1383 if (current_us
&& current_us
<= nearest_delta_us
)
1386 timeout
.it_interval
.tv_sec
= 0;
1387 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1388 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1389 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1390 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1392 fprintf(stderr
, "Internal timer error: aborting\n");
1397 #endif /* defined(__linux__) */
1399 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1401 struct sigaction act
;
1402 struct itimerval itv
;
1406 sigfillset(&act
.sa_mask
);
1408 act
.sa_handler
= host_alarm_handler
;
1410 sigaction(SIGALRM
, &act
, NULL
);
1412 itv
.it_interval
.tv_sec
= 0;
1413 /* for i386 kernel 2.6 to get 1 ms */
1414 itv
.it_interval
.tv_usec
= 999;
1415 itv
.it_value
.tv_sec
= 0;
1416 itv
.it_value
.tv_usec
= 10 * 1000;
1418 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1425 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1427 struct itimerval itv
;
1429 memset(&itv
, 0, sizeof(itv
));
1430 setitimer(ITIMER_REAL
, &itv
, NULL
);
1433 #endif /* !defined(_WIN32) */
1438 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1441 struct qemu_alarm_win32
*data
= t
->priv
;
1444 memset(&tc
, 0, sizeof(tc
));
1445 timeGetDevCaps(&tc
, sizeof(tc
));
1447 if (data
->period
< tc
.wPeriodMin
)
1448 data
->period
= tc
.wPeriodMin
;
1450 timeBeginPeriod(data
->period
);
1452 flags
= TIME_CALLBACK_FUNCTION
;
1453 if (alarm_has_dynticks(t
))
1454 flags
|= TIME_ONESHOT
;
1456 flags
|= TIME_PERIODIC
;
1458 data
->timerId
= timeSetEvent(1, // interval (ms)
1459 data
->period
, // resolution
1460 host_alarm_handler
, // function
1461 (DWORD
)t
, // parameter
1464 if (!data
->timerId
) {
1465 perror("Failed to initialize win32 alarm timer");
1466 timeEndPeriod(data
->period
);
1473 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1475 struct qemu_alarm_win32
*data
= t
->priv
;
1477 timeKillEvent(data
->timerId
);
1478 timeEndPeriod(data
->period
);
1481 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1483 struct qemu_alarm_win32
*data
= t
->priv
;
1484 uint64_t nearest_delta_us
;
1486 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1487 !active_timers
[QEMU_TIMER_VIRTUAL
])
1490 nearest_delta_us
= qemu_next_deadline_dyntick();
1491 nearest_delta_us
/= 1000;
1493 timeKillEvent(data
->timerId
);
1495 data
->timerId
= timeSetEvent(1,
1499 TIME_ONESHOT
| TIME_PERIODIC
);
1501 if (!data
->timerId
) {
1502 perror("Failed to re-arm win32 alarm timer");
1504 timeEndPeriod(data
->period
);
1511 static int init_timer_alarm(void)
1513 struct qemu_alarm_timer
*t
= NULL
;
1516 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1517 t
= &alarm_timers
[i
];
1537 static void quit_timers(void)
1539 alarm_timer
->stop(alarm_timer
);
1543 /***********************************************************/
1544 /* host time/date access */
1545 void qemu_get_timedate(struct tm
*tm
, int offset
)
1552 if (rtc_date_offset
== -1) {
1556 ret
= localtime(&ti
);
1558 ti
-= rtc_date_offset
;
1562 memcpy(tm
, ret
, sizeof(struct tm
));
1565 int qemu_timedate_diff(struct tm
*tm
)
1569 if (rtc_date_offset
== -1)
1571 seconds
= mktimegm(tm
);
1573 seconds
= mktime(tm
);
1575 seconds
= mktimegm(tm
) + rtc_date_offset
;
1577 return seconds
- time(NULL
);
1581 static void socket_cleanup(void)
1586 static int socket_init(void)
1591 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1593 err
= WSAGetLastError();
1594 fprintf(stderr
, "WSAStartup: %d\n", err
);
1597 atexit(socket_cleanup
);
1602 /***********************************************************/
1603 /* Bluetooth support */
1606 static struct HCIInfo
*hci_table
[MAX_NICS
];
1608 static struct bt_vlan_s
{
1609 struct bt_scatternet_s net
;
1611 struct bt_vlan_s
*next
;
1614 /* find or alloc a new bluetooth "VLAN" */
1615 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1617 struct bt_vlan_s
**pvlan
, *vlan
;
1618 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1622 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1624 pvlan
= &first_bt_vlan
;
1625 while (*pvlan
!= NULL
)
1626 pvlan
= &(*pvlan
)->next
;
1631 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1635 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1640 static struct HCIInfo null_hci
= {
1641 .cmd_send
= null_hci_send
,
1642 .sco_send
= null_hci_send
,
1643 .acl_send
= null_hci_send
,
1644 .bdaddr_set
= null_hci_addr_set
,
1647 struct HCIInfo
*qemu_next_hci(void)
1649 if (cur_hci
== nb_hcis
)
1652 return hci_table
[cur_hci
++];
1655 static struct HCIInfo
*hci_init(const char *str
)
1658 struct bt_scatternet_s
*vlan
= 0;
1660 if (!strcmp(str
, "null"))
1663 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1665 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1666 else if (!strncmp(str
, "hci", 3)) {
1669 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1670 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1675 vlan
= qemu_find_bt_vlan(0);
1677 return bt_new_hci(vlan
);
1680 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1685 static int bt_hci_parse(const char *str
)
1687 struct HCIInfo
*hci
;
1690 if (nb_hcis
>= MAX_NICS
) {
1691 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1695 hci
= hci_init(str
);
1704 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1705 hci
->bdaddr_set(hci
, bdaddr
.b
);
1707 hci_table
[nb_hcis
++] = hci
;
1712 static void bt_vhci_add(int vlan_id
)
1714 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1717 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1718 "an empty scatternet %i\n", vlan_id
);
1720 bt_vhci_init(bt_new_hci(vlan
));
1723 static struct bt_device_s
*bt_device_add(const char *opt
)
1725 struct bt_scatternet_s
*vlan
;
1727 char *endp
= strstr(opt
, ",vlan=");
1728 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1731 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1734 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1736 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1741 vlan
= qemu_find_bt_vlan(vlan_id
);
1744 fprintf(stderr
, "qemu: warning: adding a slave device to "
1745 "an empty scatternet %i\n", vlan_id
);
1747 if (!strcmp(devname
, "keyboard"))
1748 return bt_keyboard_init(vlan
);
1750 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1754 static int bt_parse(const char *opt
)
1756 const char *endp
, *p
;
1759 if (strstart(opt
, "hci", &endp
)) {
1760 if (!*endp
|| *endp
== ',') {
1762 if (!strstart(endp
, ",vlan=", 0))
1765 return bt_hci_parse(opt
);
1767 } else if (strstart(opt
, "vhci", &endp
)) {
1768 if (!*endp
|| *endp
== ',') {
1770 if (strstart(endp
, ",vlan=", &p
)) {
1771 vlan
= strtol(p
, (char **) &endp
, 0);
1773 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1777 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1786 } else if (strstart(opt
, "device:", &endp
))
1787 return !bt_device_add(endp
);
1789 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1793 /***********************************************************/
1794 /* QEMU Block devices */
1796 #define HD_ALIAS "index=%d,media=disk"
1797 #define CDROM_ALIAS "index=2,media=cdrom"
1798 #define FD_ALIAS "index=%d,if=floppy"
1799 #define PFLASH_ALIAS "if=pflash"
1800 #define MTD_ALIAS "if=mtd"
1801 #define SD_ALIAS "index=0,if=sd"
1803 static QemuOptsList drive_opt_list
= {
1805 .head
= TAILQ_HEAD_INITIALIZER(drive_opt_list
.head
),
1809 .type
= QEMU_OPT_NUMBER
,
1810 .help
= "bus number",
1813 .type
= QEMU_OPT_NUMBER
,
1814 .help
= "unit number (i.e. lun for scsi)",
1817 .type
= QEMU_OPT_STRING
,
1818 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1821 .type
= QEMU_OPT_NUMBER
,
1824 .type
= QEMU_OPT_NUMBER
,
1825 .help
= "number of cylinders (ide disk geometry)",
1828 .type
= QEMU_OPT_NUMBER
,
1829 .help
= "number of heads (ide disk geometry)",
1832 .type
= QEMU_OPT_NUMBER
,
1833 .help
= "number of sectors (ide disk geometry)",
1836 .type
= QEMU_OPT_STRING
,
1837 .help
= "chs translation (auto, lba. none)",
1840 .type
= QEMU_OPT_STRING
,
1841 .help
= "media type (disk, cdrom)",
1844 .type
= QEMU_OPT_BOOL
,
1847 .type
= QEMU_OPT_STRING
,
1848 .help
= "disk image",
1851 .type
= QEMU_OPT_STRING
,
1852 .help
= "host cache usage (none, writeback, writethrough)",
1855 .type
= QEMU_OPT_STRING
,
1856 .help
= "disk format (raw, qcow2, ...)",
1859 .type
= QEMU_OPT_STRING
,
1862 .type
= QEMU_OPT_STRING
,
1865 .type
= QEMU_OPT_STRING
,
1866 .help
= "pci address (virtio only)",
1868 { /* end if list */ }
1872 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1879 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1882 opts
= qemu_opts_parse(&drive_opt_list
, optstr
, NULL
);
1884 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1885 __FUNCTION__
, optstr
);
1889 qemu_opt_set(opts
, "file", file
);
1893 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1897 /* seek interface, bus and unit */
1899 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1900 if (dinfo
->type
== type
&&
1901 dinfo
->bus
== bus
&&
1902 dinfo
->unit
== unit
)
1909 DriveInfo
*drive_get_by_id(char *id
)
1913 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1914 if (strcmp(id
, dinfo
->id
))
1921 int drive_get_max_bus(BlockInterfaceType type
)
1927 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1928 if(dinfo
->type
== type
&&
1929 dinfo
->bus
> max_bus
)
1930 max_bus
= dinfo
->bus
;
1935 const char *drive_get_serial(BlockDriverState
*bdrv
)
1939 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1940 if (dinfo
->bdrv
== bdrv
)
1941 return dinfo
->serial
;
1947 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1951 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1952 if (dinfo
->bdrv
== bdrv
)
1953 return dinfo
->onerror
;
1956 return BLOCK_ERR_STOP_ENOSPC
;
1959 static void bdrv_format_print(void *opaque
, const char *name
)
1961 fprintf(stderr
, " %s", name
);
1964 void drive_uninit(BlockDriverState
*bdrv
)
1968 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1969 if (dinfo
->bdrv
!= bdrv
)
1971 qemu_opts_del(dinfo
->opts
);
1972 TAILQ_REMOVE(&drives
, dinfo
, next
);
1978 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1982 const char *file
= NULL
;
1985 const char *mediastr
= "";
1986 BlockInterfaceType type
;
1987 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1988 int bus_id
, unit_id
;
1989 int cyls
, heads
, secs
, translation
;
1990 BlockDriver
*drv
= NULL
;
1991 QEMUMachine
*machine
= opaque
;
1995 int bdrv_flags
, onerror
;
1996 const char *devaddr
;
2002 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2005 if (machine
->use_scsi
) {
2007 max_devs
= MAX_SCSI_DEVS
;
2008 pstrcpy(devname
, sizeof(devname
), "scsi");
2011 max_devs
= MAX_IDE_DEVS
;
2012 pstrcpy(devname
, sizeof(devname
), "ide");
2016 /* extract parameters */
2017 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2018 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2019 index
= qemu_opt_get_number(opts
, "index", -1);
2021 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2022 heads
= qemu_opt_get_number(opts
, "heads", 0);
2023 secs
= qemu_opt_get_number(opts
, "secs", 0);
2025 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2027 file
= qemu_opt_get(opts
, "file");
2028 serial
= qemu_opt_get(opts
, "serial");
2030 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2031 pstrcpy(devname
, sizeof(devname
), buf
);
2032 if (!strcmp(buf
, "ide")) {
2034 max_devs
= MAX_IDE_DEVS
;
2035 } else if (!strcmp(buf
, "scsi")) {
2037 max_devs
= MAX_SCSI_DEVS
;
2038 } else if (!strcmp(buf
, "floppy")) {
2041 } else if (!strcmp(buf
, "pflash")) {
2044 } else if (!strcmp(buf
, "mtd")) {
2047 } else if (!strcmp(buf
, "sd")) {
2050 } else if (!strcmp(buf
, "virtio")) {
2053 } else if (!strcmp(buf
, "xen")) {
2057 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2062 if (cyls
|| heads
|| secs
) {
2063 if (cyls
< 1 || cyls
> 16383) {
2064 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2067 if (heads
< 1 || heads
> 16) {
2068 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2071 if (secs
< 1 || secs
> 63) {
2072 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2077 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2080 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2084 if (!strcmp(buf
, "none"))
2085 translation
= BIOS_ATA_TRANSLATION_NONE
;
2086 else if (!strcmp(buf
, "lba"))
2087 translation
= BIOS_ATA_TRANSLATION_LBA
;
2088 else if (!strcmp(buf
, "auto"))
2089 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2091 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2096 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2097 if (!strcmp(buf
, "disk")) {
2099 } else if (!strcmp(buf
, "cdrom")) {
2100 if (cyls
|| secs
|| heads
) {
2102 "qemu: '%s' invalid physical CHS format\n", buf
);
2105 media
= MEDIA_CDROM
;
2107 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2112 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2113 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2115 else if (!strcmp(buf
, "writethrough"))
2117 else if (!strcmp(buf
, "writeback"))
2120 fprintf(stderr
, "qemu: invalid cache option\n");
2125 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2126 if (strcmp(buf
, "?") == 0) {
2127 fprintf(stderr
, "qemu: Supported formats:");
2128 bdrv_iterate_format(bdrv_format_print
, NULL
);
2129 fprintf(stderr
, "\n");
2132 drv
= bdrv_find_format(buf
);
2134 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2139 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2140 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2141 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2142 fprintf(stderr
, "werror is no supported by this format\n");
2145 if (!strcmp(buf
, "ignore"))
2146 onerror
= BLOCK_ERR_IGNORE
;
2147 else if (!strcmp(buf
, "enospc"))
2148 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2149 else if (!strcmp(buf
, "stop"))
2150 onerror
= BLOCK_ERR_STOP_ANY
;
2151 else if (!strcmp(buf
, "report"))
2152 onerror
= BLOCK_ERR_REPORT
;
2154 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2159 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2160 if (type
!= IF_VIRTIO
) {
2161 fprintf(stderr
, "addr is not supported\n");
2166 /* compute bus and unit according index */
2169 if (bus_id
!= 0 || unit_id
!= -1) {
2171 "qemu: index cannot be used with bus and unit\n");
2179 unit_id
= index
% max_devs
;
2180 bus_id
= index
/ max_devs
;
2184 /* if user doesn't specify a unit_id,
2185 * try to find the first free
2188 if (unit_id
== -1) {
2190 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2192 if (max_devs
&& unit_id
>= max_devs
) {
2193 unit_id
-= max_devs
;
2201 if (max_devs
&& unit_id
>= max_devs
) {
2202 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2203 unit_id
, max_devs
- 1);
2208 * ignore multiple definitions
2211 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2218 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2219 if ((buf
= qemu_opt_get(opts
, "id")) != NULL
) {
2220 dinfo
->id
= qemu_strdup(buf
);
2222 /* no id supplied -> create one */
2223 dinfo
->id
= qemu_mallocz(32);
2224 if (type
== IF_IDE
|| type
== IF_SCSI
)
2225 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2227 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2228 devname
, bus_id
, mediastr
, unit_id
);
2230 snprintf(dinfo
->id
, 32, "%s%s%i",
2231 devname
, mediastr
, unit_id
);
2233 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2234 dinfo
->devaddr
= devaddr
;
2236 dinfo
->bus
= bus_id
;
2237 dinfo
->unit
= unit_id
;
2238 dinfo
->onerror
= onerror
;
2241 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2242 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2251 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2252 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2256 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2261 /* FIXME: This isn't really a floppy, but it's a reasonable
2264 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2279 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2280 cache
= 2; /* always use write-back with snapshot */
2282 if (cache
== 0) /* no caching */
2283 bdrv_flags
|= BDRV_O_NOCACHE
;
2284 else if (cache
== 2) /* write-back */
2285 bdrv_flags
|= BDRV_O_CACHE_WB
;
2286 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2287 fprintf(stderr
, "qemu: could not open disk image %s\n",
2291 if (bdrv_key_required(dinfo
->bdrv
))
2297 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2299 QEMUMachine
*machine
= opaque
;
2300 int fatal_error
= 0;
2302 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2309 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2311 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2312 qemu_opt_set(opts
, "snapshot", "on");
2317 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2319 boot_set_handler
= func
;
2320 boot_set_opaque
= opaque
;
2323 int qemu_boot_set(const char *boot_devices
)
2325 if (!boot_set_handler
) {
2328 return boot_set_handler(boot_set_opaque
, boot_devices
);
2331 static int parse_bootdevices(char *devices
)
2333 /* We just do some generic consistency checks */
2337 for (p
= devices
; *p
!= '\0'; p
++) {
2338 /* Allowed boot devices are:
2339 * a-b: floppy disk drives
2340 * c-f: IDE disk drives
2341 * g-m: machine implementation dependant drives
2342 * n-p: network devices
2343 * It's up to each machine implementation to check if the given boot
2344 * devices match the actual hardware implementation and firmware
2347 if (*p
< 'a' || *p
> 'p') {
2348 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2351 if (bitmap
& (1 << (*p
- 'a'))) {
2352 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2355 bitmap
|= 1 << (*p
- 'a');
2360 static void restore_boot_devices(void *opaque
)
2362 char *standard_boot_devices
= opaque
;
2364 qemu_boot_set(standard_boot_devices
);
2366 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2367 qemu_free(standard_boot_devices
);
2370 static void numa_add(const char *optarg
)
2374 unsigned long long value
, endvalue
;
2377 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2378 if (!strcmp(option
, "node")) {
2379 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2380 nodenr
= nb_numa_nodes
;
2382 nodenr
= strtoull(option
, NULL
, 10);
2385 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2386 node_mem
[nodenr
] = 0;
2388 value
= strtoull(option
, &endptr
, 0);
2390 case 0: case 'M': case 'm':
2397 node_mem
[nodenr
] = value
;
2399 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2400 node_cpumask
[nodenr
] = 0;
2402 value
= strtoull(option
, &endptr
, 10);
2405 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2407 if (*endptr
== '-') {
2408 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2409 if (endvalue
>= 63) {
2412 "only 63 CPUs in NUMA mode supported.\n");
2414 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2419 node_cpumask
[nodenr
] = value
;
2426 /***********************************************************/
2429 static USBPort
*used_usb_ports
;
2430 static USBPort
*free_usb_ports
;
2432 /* ??? Maybe change this to register a hub to keep track of the topology. */
2433 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2434 usb_attachfn attach
)
2436 port
->opaque
= opaque
;
2437 port
->index
= index
;
2438 port
->attach
= attach
;
2439 port
->next
= free_usb_ports
;
2440 free_usb_ports
= port
;
2443 int usb_device_add_dev(USBDevice
*dev
)
2447 /* Find a USB port to add the device to. */
2448 port
= free_usb_ports
;
2452 /* Create a new hub and chain it on. */
2453 free_usb_ports
= NULL
;
2454 port
->next
= used_usb_ports
;
2455 used_usb_ports
= port
;
2457 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2458 usb_attach(port
, hub
);
2459 port
= free_usb_ports
;
2462 free_usb_ports
= port
->next
;
2463 port
->next
= used_usb_ports
;
2464 used_usb_ports
= port
;
2465 usb_attach(port
, dev
);
2469 static void usb_msd_password_cb(void *opaque
, int err
)
2471 USBDevice
*dev
= opaque
;
2474 usb_device_add_dev(dev
);
2476 dev
->handle_destroy(dev
);
2479 static int usb_device_add(const char *devname
, int is_hotplug
)
2484 if (!free_usb_ports
)
2487 if (strstart(devname
, "host:", &p
)) {
2488 dev
= usb_host_device_open(p
);
2489 } else if (!strcmp(devname
, "mouse")) {
2490 dev
= usb_mouse_init();
2491 } else if (!strcmp(devname
, "tablet")) {
2492 dev
= usb_tablet_init();
2493 } else if (!strcmp(devname
, "keyboard")) {
2494 dev
= usb_keyboard_init();
2495 } else if (strstart(devname
, "disk:", &p
)) {
2496 BlockDriverState
*bs
;
2498 dev
= usb_msd_init(p
);
2501 bs
= usb_msd_get_bdrv(dev
);
2502 if (bdrv_key_required(bs
)) {
2505 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2510 } else if (!strcmp(devname
, "wacom-tablet")) {
2511 dev
= usb_wacom_init();
2512 } else if (strstart(devname
, "serial:", &p
)) {
2513 dev
= usb_serial_init(p
);
2514 #ifdef CONFIG_BRLAPI
2515 } else if (!strcmp(devname
, "braille")) {
2516 dev
= usb_baum_init();
2518 } else if (strstart(devname
, "net:", &p
)) {
2521 if (net_client_init(NULL
, "nic", p
) < 0)
2523 nd_table
[nic
].model
= "usb";
2524 dev
= usb_net_init(&nd_table
[nic
]);
2525 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2526 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2527 bt_new_hci(qemu_find_bt_vlan(0)));
2534 return usb_device_add_dev(dev
);
2537 int usb_device_del_addr(int bus_num
, int addr
)
2543 if (!used_usb_ports
)
2549 lastp
= &used_usb_ports
;
2550 port
= used_usb_ports
;
2551 while (port
&& port
->dev
->addr
!= addr
) {
2552 lastp
= &port
->next
;
2560 *lastp
= port
->next
;
2561 usb_attach(port
, NULL
);
2562 dev
->handle_destroy(dev
);
2563 port
->next
= free_usb_ports
;
2564 free_usb_ports
= port
;
2568 static int usb_device_del(const char *devname
)
2573 if (strstart(devname
, "host:", &p
))
2574 return usb_host_device_close(p
);
2576 if (!used_usb_ports
)
2579 p
= strchr(devname
, '.');
2582 bus_num
= strtoul(devname
, NULL
, 0);
2583 addr
= strtoul(p
+ 1, NULL
, 0);
2585 return usb_device_del_addr(bus_num
, addr
);
2588 static int usb_parse(const char *cmdline
)
2590 return usb_device_add(cmdline
, 0);
2593 void do_usb_add(Monitor
*mon
, const char *devname
)
2595 usb_device_add(devname
, 1);
2598 void do_usb_del(Monitor
*mon
, const char *devname
)
2600 usb_device_del(devname
);
2603 void usb_info(Monitor
*mon
)
2607 const char *speed_str
;
2610 monitor_printf(mon
, "USB support not enabled\n");
2614 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2618 switch(dev
->speed
) {
2622 case USB_SPEED_FULL
:
2625 case USB_SPEED_HIGH
:
2632 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2633 0, dev
->addr
, speed_str
, dev
->devname
);
2637 /***********************************************************/
2638 /* PCMCIA/Cardbus */
2640 static struct pcmcia_socket_entry_s
{
2641 PCMCIASocket
*socket
;
2642 struct pcmcia_socket_entry_s
*next
;
2643 } *pcmcia_sockets
= 0;
2645 void pcmcia_socket_register(PCMCIASocket
*socket
)
2647 struct pcmcia_socket_entry_s
*entry
;
2649 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2650 entry
->socket
= socket
;
2651 entry
->next
= pcmcia_sockets
;
2652 pcmcia_sockets
= entry
;
2655 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2657 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2659 ptr
= &pcmcia_sockets
;
2660 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2661 if (entry
->socket
== socket
) {
2667 void pcmcia_info(Monitor
*mon
)
2669 struct pcmcia_socket_entry_s
*iter
;
2671 if (!pcmcia_sockets
)
2672 monitor_printf(mon
, "No PCMCIA sockets\n");
2674 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2675 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2676 iter
->socket
->attached
? iter
->socket
->card_string
:
2680 /***********************************************************/
2681 /* register display */
2683 struct DisplayAllocator default_allocator
= {
2684 defaultallocator_create_displaysurface
,
2685 defaultallocator_resize_displaysurface
,
2686 defaultallocator_free_displaysurface
2689 void register_displaystate(DisplayState
*ds
)
2699 DisplayState
*get_displaystate(void)
2701 return display_state
;
2704 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2706 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2707 return ds
->allocator
;
2712 static void dumb_display_init(void)
2714 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2715 ds
->allocator
= &default_allocator
;
2716 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2717 register_displaystate(ds
);
2720 /***********************************************************/
2723 typedef struct IOHandlerRecord
{
2725 IOCanRWHandler
*fd_read_poll
;
2727 IOHandler
*fd_write
;
2730 /* temporary data */
2732 struct IOHandlerRecord
*next
;
2735 static IOHandlerRecord
*first_io_handler
;
2737 /* XXX: fd_read_poll should be suppressed, but an API change is
2738 necessary in the character devices to suppress fd_can_read(). */
2739 int qemu_set_fd_handler2(int fd
,
2740 IOCanRWHandler
*fd_read_poll
,
2742 IOHandler
*fd_write
,
2745 IOHandlerRecord
**pioh
, *ioh
;
2747 if (!fd_read
&& !fd_write
) {
2748 pioh
= &first_io_handler
;
2753 if (ioh
->fd
== fd
) {
2760 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2764 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2765 ioh
->next
= first_io_handler
;
2766 first_io_handler
= ioh
;
2769 ioh
->fd_read_poll
= fd_read_poll
;
2770 ioh
->fd_read
= fd_read
;
2771 ioh
->fd_write
= fd_write
;
2772 ioh
->opaque
= opaque
;
2778 int qemu_set_fd_handler(int fd
,
2780 IOHandler
*fd_write
,
2783 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2787 /***********************************************************/
2788 /* Polling handling */
2790 typedef struct PollingEntry
{
2793 struct PollingEntry
*next
;
2796 static PollingEntry
*first_polling_entry
;
2798 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2800 PollingEntry
**ppe
, *pe
;
2801 pe
= qemu_mallocz(sizeof(PollingEntry
));
2803 pe
->opaque
= opaque
;
2804 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2809 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2811 PollingEntry
**ppe
, *pe
;
2812 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2814 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2822 /***********************************************************/
2823 /* Wait objects support */
2824 typedef struct WaitObjects
{
2826 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2827 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2828 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2831 static WaitObjects wait_objects
= {0};
2833 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2835 WaitObjects
*w
= &wait_objects
;
2837 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2839 w
->events
[w
->num
] = handle
;
2840 w
->func
[w
->num
] = func
;
2841 w
->opaque
[w
->num
] = opaque
;
2846 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2849 WaitObjects
*w
= &wait_objects
;
2852 for (i
= 0; i
< w
->num
; i
++) {
2853 if (w
->events
[i
] == handle
)
2856 w
->events
[i
] = w
->events
[i
+ 1];
2857 w
->func
[i
] = w
->func
[i
+ 1];
2858 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2866 /***********************************************************/
2867 /* ram save/restore */
2869 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2873 v
= qemu_get_byte(f
);
2876 if (qemu_get_buffer(f
, buf
, len
) != len
)
2880 v
= qemu_get_byte(f
);
2881 memset(buf
, v
, len
);
2887 if (qemu_file_has_error(f
))
2893 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2898 if (qemu_get_be32(f
) != last_ram_offset
)
2900 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2901 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2908 #define BDRV_HASH_BLOCK_SIZE 1024
2909 #define IOBUF_SIZE 4096
2910 #define RAM_CBLOCK_MAGIC 0xfabe
2912 typedef struct RamDecompressState
{
2915 uint8_t buf
[IOBUF_SIZE
];
2916 } RamDecompressState
;
2918 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2921 memset(s
, 0, sizeof(*s
));
2923 ret
= inflateInit(&s
->zstream
);
2929 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2933 s
->zstream
.avail_out
= len
;
2934 s
->zstream
.next_out
= buf
;
2935 while (s
->zstream
.avail_out
> 0) {
2936 if (s
->zstream
.avail_in
== 0) {
2937 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2939 clen
= qemu_get_be16(s
->f
);
2940 if (clen
> IOBUF_SIZE
)
2942 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2943 s
->zstream
.avail_in
= clen
;
2944 s
->zstream
.next_in
= s
->buf
;
2946 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2947 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2954 static void ram_decompress_close(RamDecompressState
*s
)
2956 inflateEnd(&s
->zstream
);
2959 #define RAM_SAVE_FLAG_FULL 0x01
2960 #define RAM_SAVE_FLAG_COMPRESS 0x02
2961 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2962 #define RAM_SAVE_FLAG_PAGE 0x08
2963 #define RAM_SAVE_FLAG_EOS 0x10
2965 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2967 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2968 uint32_t *array
= (uint32_t *)page
;
2971 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2972 if (array
[i
] != val
)
2979 static int ram_save_block(QEMUFile
*f
)
2981 static ram_addr_t current_addr
= 0;
2982 ram_addr_t saved_addr
= current_addr
;
2983 ram_addr_t addr
= 0;
2986 while (addr
< last_ram_offset
) {
2987 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2990 cpu_physical_memory_reset_dirty(current_addr
,
2991 current_addr
+ TARGET_PAGE_SIZE
,
2992 MIGRATION_DIRTY_FLAG
);
2994 p
= qemu_get_ram_ptr(current_addr
);
2996 if (is_dup_page(p
, *p
)) {
2997 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2998 qemu_put_byte(f
, *p
);
3000 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
3001 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3007 addr
+= TARGET_PAGE_SIZE
;
3008 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3014 static uint64_t bytes_transferred
= 0;
3016 static ram_addr_t
ram_save_remaining(void)
3019 ram_addr_t count
= 0;
3021 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3022 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3029 uint64_t ram_bytes_remaining(void)
3031 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3034 uint64_t ram_bytes_transferred(void)
3036 return bytes_transferred
;
3039 uint64_t ram_bytes_total(void)
3041 return last_ram_offset
;
3044 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3047 uint64_t bytes_transferred_last
;
3049 uint64_t expected_time
= 0;
3051 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3052 qemu_file_set_error(f
);
3057 /* Make sure all dirty bits are set */
3058 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3059 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3060 cpu_physical_memory_set_dirty(addr
);
3063 /* Enable dirty memory tracking */
3064 cpu_physical_memory_set_dirty_tracking(1);
3066 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3069 bytes_transferred_last
= bytes_transferred
;
3070 bwidth
= get_clock();
3072 while (!qemu_file_rate_limit(f
)) {
3075 ret
= ram_save_block(f
);
3076 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3077 if (ret
== 0) /* no more blocks */
3081 bwidth
= get_clock() - bwidth
;
3082 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3084 /* if we haven't transferred anything this round, force expected_time to a
3085 * a very high value, but without crashing */
3089 /* try transferring iterative blocks of memory */
3093 /* flush all remaining blocks regardless of rate limiting */
3094 while (ram_save_block(f
) != 0) {
3095 bytes_transferred
+= TARGET_PAGE_SIZE
;
3097 cpu_physical_memory_set_dirty_tracking(0);
3100 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3102 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3104 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3107 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3109 RamDecompressState s1
, *s
= &s1
;
3113 if (ram_decompress_open(s
, f
) < 0)
3115 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3116 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3117 fprintf(stderr
, "Error while reading ram block header\n");
3121 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3122 BDRV_HASH_BLOCK_SIZE
) < 0) {
3123 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3128 printf("Error block header\n");
3132 ram_decompress_close(s
);
3137 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3142 if (version_id
== 1)
3143 return ram_load_v1(f
, opaque
);
3145 if (version_id
== 2) {
3146 if (qemu_get_be32(f
) != last_ram_offset
)
3148 return ram_load_dead(f
, opaque
);
3151 if (version_id
!= 3)
3155 addr
= qemu_get_be64(f
);
3157 flags
= addr
& ~TARGET_PAGE_MASK
;
3158 addr
&= TARGET_PAGE_MASK
;
3160 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3161 if (addr
!= last_ram_offset
)
3165 if (flags
& RAM_SAVE_FLAG_FULL
) {
3166 if (ram_load_dead(f
, opaque
) < 0)
3170 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3171 uint8_t ch
= qemu_get_byte(f
);
3172 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3175 (!kvm_enabled() || kvm_has_sync_mmu())) {
3176 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3179 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3180 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3181 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3186 void qemu_service_io(void)
3188 qemu_notify_event();
3191 /***********************************************************/
3192 /* bottom halves (can be seen as timers which expire ASAP) */
3203 static QEMUBH
*first_bh
= NULL
;
3205 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3208 bh
= qemu_mallocz(sizeof(QEMUBH
));
3210 bh
->opaque
= opaque
;
3211 bh
->next
= first_bh
;
3216 int qemu_bh_poll(void)
3222 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3223 if (!bh
->deleted
&& bh
->scheduled
) {
3232 /* remove deleted bhs */
3246 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3254 void qemu_bh_schedule(QEMUBH
*bh
)
3260 /* stop the currently executing CPU to execute the BH ASAP */
3261 qemu_notify_event();
3264 void qemu_bh_cancel(QEMUBH
*bh
)
3269 void qemu_bh_delete(QEMUBH
*bh
)
3275 static void qemu_bh_update_timeout(int *timeout
)
3279 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3280 if (!bh
->deleted
&& bh
->scheduled
) {
3282 /* idle bottom halves will be polled at least
3284 *timeout
= MIN(10, *timeout
);
3286 /* non-idle bottom halves will be executed
3295 /***********************************************************/
3296 /* machine registration */
3298 static QEMUMachine
*first_machine
= NULL
;
3299 QEMUMachine
*current_machine
= NULL
;
3301 int qemu_register_machine(QEMUMachine
*m
)
3304 pm
= &first_machine
;
3312 static QEMUMachine
*find_machine(const char *name
)
3316 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3317 if (!strcmp(m
->name
, name
))
3319 if (m
->alias
&& !strcmp(m
->alias
, name
))
3325 static QEMUMachine
*find_default_machine(void)
3329 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3330 if (m
->is_default
) {
3337 /***********************************************************/
3338 /* main execution loop */
3340 static void gui_update(void *opaque
)
3342 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3343 DisplayState
*ds
= opaque
;
3344 DisplayChangeListener
*dcl
= ds
->listeners
;
3348 while (dcl
!= NULL
) {
3349 if (dcl
->gui_timer_interval
&&
3350 dcl
->gui_timer_interval
< interval
)
3351 interval
= dcl
->gui_timer_interval
;
3354 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3357 static void nographic_update(void *opaque
)
3359 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3361 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3364 struct vm_change_state_entry
{
3365 VMChangeStateHandler
*cb
;
3367 LIST_ENTRY (vm_change_state_entry
) entries
;
3370 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3372 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3375 VMChangeStateEntry
*e
;
3377 e
= qemu_mallocz(sizeof (*e
));
3381 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3385 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3387 LIST_REMOVE (e
, entries
);
3391 static void vm_state_notify(int running
, int reason
)
3393 VMChangeStateEntry
*e
;
3395 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3396 e
->cb(e
->opaque
, running
, reason
);
3400 static void resume_all_vcpus(void);
3401 static void pause_all_vcpus(void);
3408 vm_state_notify(1, 0);
3409 qemu_rearm_alarm_timer(alarm_timer
);
3414 /* reset/shutdown handler */
3416 typedef struct QEMUResetEntry
{
3417 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3418 QEMUResetHandler
*func
;
3422 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3423 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3424 static int reset_requested
;
3425 static int shutdown_requested
;
3426 static int powerdown_requested
;
3427 static int debug_requested
;
3428 static int vmstop_requested
;
3430 int qemu_shutdown_requested(void)
3432 int r
= shutdown_requested
;
3433 shutdown_requested
= 0;
3437 int qemu_reset_requested(void)
3439 int r
= reset_requested
;
3440 reset_requested
= 0;
3444 int qemu_powerdown_requested(void)
3446 int r
= powerdown_requested
;
3447 powerdown_requested
= 0;
3451 static int qemu_debug_requested(void)
3453 int r
= debug_requested
;
3454 debug_requested
= 0;
3458 static int qemu_vmstop_requested(void)
3460 int r
= vmstop_requested
;
3461 vmstop_requested
= 0;
3465 static void do_vm_stop(int reason
)
3468 cpu_disable_ticks();
3471 vm_state_notify(0, reason
);
3475 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3477 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3480 re
->opaque
= opaque
;
3481 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3484 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3488 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3489 if (re
->func
== func
&& re
->opaque
== opaque
) {
3490 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3497 void qemu_system_reset(void)
3499 QEMUResetEntry
*re
, *nre
;
3501 /* reset all devices */
3502 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3503 re
->func(re
->opaque
);
3507 void qemu_system_reset_request(void)
3510 shutdown_requested
= 1;
3512 reset_requested
= 1;
3514 qemu_notify_event();
3517 void qemu_system_shutdown_request(void)
3519 shutdown_requested
= 1;
3520 qemu_notify_event();
3523 void qemu_system_powerdown_request(void)
3525 powerdown_requested
= 1;
3526 qemu_notify_event();
3529 #ifdef CONFIG_IOTHREAD
3530 static void qemu_system_vmstop_request(int reason
)
3532 vmstop_requested
= reason
;
3533 qemu_notify_event();
3538 static int io_thread_fd
= -1;
3540 static void qemu_event_increment(void)
3542 static const char byte
= 0;
3544 if (io_thread_fd
== -1)
3547 write(io_thread_fd
, &byte
, sizeof(byte
));
3550 static void qemu_event_read(void *opaque
)
3552 int fd
= (unsigned long)opaque
;
3555 /* Drain the notify pipe */
3558 len
= read(fd
, buffer
, sizeof(buffer
));
3559 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3562 static int qemu_event_init(void)
3571 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3575 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3579 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3580 (void *)(unsigned long)fds
[0]);
3582 io_thread_fd
= fds
[1];
3591 HANDLE qemu_event_handle
;
3593 static void dummy_event_handler(void *opaque
)
3597 static int qemu_event_init(void)
3599 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3600 if (!qemu_event_handle
) {
3601 perror("Failed CreateEvent");
3604 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3608 static void qemu_event_increment(void)
3610 SetEvent(qemu_event_handle
);
3614 static int cpu_can_run(CPUState
*env
)
3623 #ifndef CONFIG_IOTHREAD
3624 static int qemu_init_main_loop(void)
3626 return qemu_event_init();
3629 void qemu_init_vcpu(void *_env
)
3631 CPUState
*env
= _env
;
3638 int qemu_cpu_self(void *env
)
3643 static void resume_all_vcpus(void)
3647 static void pause_all_vcpus(void)
3651 void qemu_cpu_kick(void *env
)
3656 void qemu_notify_event(void)
3658 CPUState
*env
= cpu_single_env
;
3663 if (env
->kqemu_enabled
)
3664 kqemu_cpu_interrupt(env
);
3669 #define qemu_mutex_lock_iothread() do { } while (0)
3670 #define qemu_mutex_unlock_iothread() do { } while (0)
3672 void vm_stop(int reason
)
3677 #else /* CONFIG_IOTHREAD */
3679 #include "qemu-thread.h"
3681 QemuMutex qemu_global_mutex
;
3682 static QemuMutex qemu_fair_mutex
;
3684 static QemuThread io_thread
;
3686 static QemuThread
*tcg_cpu_thread
;
3687 static QemuCond
*tcg_halt_cond
;
3689 static int qemu_system_ready
;
3691 static QemuCond qemu_cpu_cond
;
3693 static QemuCond qemu_system_cond
;
3694 static QemuCond qemu_pause_cond
;
3696 static void block_io_signals(void);
3697 static void unblock_io_signals(void);
3698 static int tcg_has_work(void);
3700 static int qemu_init_main_loop(void)
3704 ret
= qemu_event_init();
3708 qemu_cond_init(&qemu_pause_cond
);
3709 qemu_mutex_init(&qemu_fair_mutex
);
3710 qemu_mutex_init(&qemu_global_mutex
);
3711 qemu_mutex_lock(&qemu_global_mutex
);
3713 unblock_io_signals();
3714 qemu_thread_self(&io_thread
);
3719 static void qemu_wait_io_event(CPUState
*env
)
3721 while (!tcg_has_work())
3722 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3724 qemu_mutex_unlock(&qemu_global_mutex
);
3727 * Users of qemu_global_mutex can be starved, having no chance
3728 * to acquire it since this path will get to it first.
3729 * So use another lock to provide fairness.
3731 qemu_mutex_lock(&qemu_fair_mutex
);
3732 qemu_mutex_unlock(&qemu_fair_mutex
);
3734 qemu_mutex_lock(&qemu_global_mutex
);
3738 qemu_cond_signal(&qemu_pause_cond
);
3742 static int qemu_cpu_exec(CPUState
*env
);
3744 static void *kvm_cpu_thread_fn(void *arg
)
3746 CPUState
*env
= arg
;
3749 qemu_thread_self(env
->thread
);
3751 /* signal CPU creation */
3752 qemu_mutex_lock(&qemu_global_mutex
);
3754 qemu_cond_signal(&qemu_cpu_cond
);
3756 /* and wait for machine initialization */
3757 while (!qemu_system_ready
)
3758 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3761 if (cpu_can_run(env
))
3763 qemu_wait_io_event(env
);
3769 static void tcg_cpu_exec(void);
3771 static void *tcg_cpu_thread_fn(void *arg
)
3773 CPUState
*env
= arg
;
3776 qemu_thread_self(env
->thread
);
3778 /* signal CPU creation */
3779 qemu_mutex_lock(&qemu_global_mutex
);
3780 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3782 qemu_cond_signal(&qemu_cpu_cond
);
3784 /* and wait for machine initialization */
3785 while (!qemu_system_ready
)
3786 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3790 qemu_wait_io_event(cur_cpu
);
3796 void qemu_cpu_kick(void *_env
)
3798 CPUState
*env
= _env
;
3799 qemu_cond_broadcast(env
->halt_cond
);
3801 qemu_thread_signal(env
->thread
, SIGUSR1
);
3804 int qemu_cpu_self(void *env
)
3806 return (cpu_single_env
!= NULL
);
3809 static void cpu_signal(int sig
)
3812 cpu_exit(cpu_single_env
);
3815 static void block_io_signals(void)
3818 struct sigaction sigact
;
3821 sigaddset(&set
, SIGUSR2
);
3822 sigaddset(&set
, SIGIO
);
3823 sigaddset(&set
, SIGALRM
);
3824 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3827 sigaddset(&set
, SIGUSR1
);
3828 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3830 memset(&sigact
, 0, sizeof(sigact
));
3831 sigact
.sa_handler
= cpu_signal
;
3832 sigaction(SIGUSR1
, &sigact
, NULL
);
3835 static void unblock_io_signals(void)
3840 sigaddset(&set
, SIGUSR2
);
3841 sigaddset(&set
, SIGIO
);
3842 sigaddset(&set
, SIGALRM
);
3843 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3846 sigaddset(&set
, SIGUSR1
);
3847 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3850 static void qemu_signal_lock(unsigned int msecs
)
3852 qemu_mutex_lock(&qemu_fair_mutex
);
3854 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3855 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3856 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3859 qemu_mutex_unlock(&qemu_fair_mutex
);
3862 static void qemu_mutex_lock_iothread(void)
3864 if (kvm_enabled()) {
3865 qemu_mutex_lock(&qemu_fair_mutex
);
3866 qemu_mutex_lock(&qemu_global_mutex
);
3867 qemu_mutex_unlock(&qemu_fair_mutex
);
3869 qemu_signal_lock(100);
3872 static void qemu_mutex_unlock_iothread(void)
3874 qemu_mutex_unlock(&qemu_global_mutex
);
3877 static int all_vcpus_paused(void)
3879 CPUState
*penv
= first_cpu
;
3884 penv
= (CPUState
*)penv
->next_cpu
;
3890 static void pause_all_vcpus(void)
3892 CPUState
*penv
= first_cpu
;
3896 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3897 qemu_cpu_kick(penv
);
3898 penv
= (CPUState
*)penv
->next_cpu
;
3901 while (!all_vcpus_paused()) {
3902 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3905 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3906 penv
= (CPUState
*)penv
->next_cpu
;
3911 static void resume_all_vcpus(void)
3913 CPUState
*penv
= first_cpu
;
3918 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3919 qemu_cpu_kick(penv
);
3920 penv
= (CPUState
*)penv
->next_cpu
;
3924 static void tcg_init_vcpu(void *_env
)
3926 CPUState
*env
= _env
;
3927 /* share a single thread for all cpus with TCG */
3928 if (!tcg_cpu_thread
) {
3929 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3930 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3931 qemu_cond_init(env
->halt_cond
);
3932 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3933 while (env
->created
== 0)
3934 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3935 tcg_cpu_thread
= env
->thread
;
3936 tcg_halt_cond
= env
->halt_cond
;
3938 env
->thread
= tcg_cpu_thread
;
3939 env
->halt_cond
= tcg_halt_cond
;
3943 static void kvm_start_vcpu(CPUState
*env
)
3946 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3947 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3948 qemu_cond_init(env
->halt_cond
);
3949 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3950 while (env
->created
== 0)
3951 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3954 void qemu_init_vcpu(void *_env
)
3956 CPUState
*env
= _env
;
3959 kvm_start_vcpu(env
);
3964 void qemu_notify_event(void)
3966 qemu_event_increment();
3969 void vm_stop(int reason
)
3972 qemu_thread_self(&me
);
3974 if (!qemu_thread_equal(&me
, &io_thread
)) {
3975 qemu_system_vmstop_request(reason
);
3977 * FIXME: should not return to device code in case
3978 * vm_stop() has been requested.
3980 if (cpu_single_env
) {
3981 cpu_exit(cpu_single_env
);
3982 cpu_single_env
->stop
= 1;
3993 static void host_main_loop_wait(int *timeout
)
3999 /* XXX: need to suppress polling by better using win32 events */
4001 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
4002 ret
|= pe
->func(pe
->opaque
);
4006 WaitObjects
*w
= &wait_objects
;
4008 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4009 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4010 if (w
->func
[ret
- WAIT_OBJECT_0
])
4011 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4013 /* Check for additional signaled events */
4014 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4016 /* Check if event is signaled */
4017 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4018 if(ret2
== WAIT_OBJECT_0
) {
4020 w
->func
[i
](w
->opaque
[i
]);
4021 } else if (ret2
== WAIT_TIMEOUT
) {
4023 err
= GetLastError();
4024 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4027 } else if (ret
== WAIT_TIMEOUT
) {
4029 err
= GetLastError();
4030 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4037 static void host_main_loop_wait(int *timeout
)
4042 void main_loop_wait(int timeout
)
4044 IOHandlerRecord
*ioh
;
4045 fd_set rfds
, wfds
, xfds
;
4049 qemu_bh_update_timeout(&timeout
);
4051 host_main_loop_wait(&timeout
);
4053 /* poll any events */
4054 /* XXX: separate device handlers from system ones */
4059 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4063 (!ioh
->fd_read_poll
||
4064 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4065 FD_SET(ioh
->fd
, &rfds
);
4069 if (ioh
->fd_write
) {
4070 FD_SET(ioh
->fd
, &wfds
);
4076 tv
.tv_sec
= timeout
/ 1000;
4077 tv
.tv_usec
= (timeout
% 1000) * 1000;
4079 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4081 qemu_mutex_unlock_iothread();
4082 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4083 qemu_mutex_lock_iothread();
4085 IOHandlerRecord
**pioh
;
4087 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4088 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4089 ioh
->fd_read(ioh
->opaque
);
4091 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4092 ioh
->fd_write(ioh
->opaque
);
4096 /* remove deleted IO handlers */
4097 pioh
= &first_io_handler
;
4108 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4110 /* rearm timer, if not periodic */
4111 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4112 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4113 qemu_rearm_alarm_timer(alarm_timer
);
4116 /* vm time timers */
4118 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4119 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4120 qemu_get_clock(vm_clock
));
4123 /* real time timers */
4124 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4125 qemu_get_clock(rt_clock
));
4127 /* Check bottom-halves last in case any of the earlier events triggered
4133 static int qemu_cpu_exec(CPUState
*env
)
4136 #ifdef CONFIG_PROFILER
4140 #ifdef CONFIG_PROFILER
4141 ti
= profile_getclock();
4146 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4147 env
->icount_decr
.u16
.low
= 0;
4148 env
->icount_extra
= 0;
4149 count
= qemu_next_deadline();
4150 count
= (count
+ (1 << icount_time_shift
) - 1)
4151 >> icount_time_shift
;
4152 qemu_icount
+= count
;
4153 decr
= (count
> 0xffff) ? 0xffff : count
;
4155 env
->icount_decr
.u16
.low
= decr
;
4156 env
->icount_extra
= count
;
4158 ret
= cpu_exec(env
);
4159 #ifdef CONFIG_PROFILER
4160 qemu_time
+= profile_getclock() - ti
;
4163 /* Fold pending instructions back into the
4164 instruction counter, and clear the interrupt flag. */
4165 qemu_icount
-= (env
->icount_decr
.u16
.low
4166 + env
->icount_extra
);
4167 env
->icount_decr
.u32
= 0;
4168 env
->icount_extra
= 0;
4173 static void tcg_cpu_exec(void)
4177 if (next_cpu
== NULL
)
4178 next_cpu
= first_cpu
;
4179 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4180 CPUState
*env
= cur_cpu
= next_cpu
;
4184 if (timer_alarm_pending
) {
4185 timer_alarm_pending
= 0;
4188 if (cpu_can_run(env
))
4189 ret
= qemu_cpu_exec(env
);
4190 if (ret
== EXCP_DEBUG
) {
4191 gdb_set_stop_cpu(env
);
4192 debug_requested
= 1;
4198 static int cpu_has_work(CPUState
*env
)
4206 if (qemu_cpu_has_work(env
))
4211 static int tcg_has_work(void)
4215 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4216 if (cpu_has_work(env
))
4221 static int qemu_calculate_timeout(void)
4223 #ifndef CONFIG_IOTHREAD
4228 else if (tcg_has_work())
4230 else if (!use_icount
)
4233 /* XXX: use timeout computed from timers */
4236 /* Advance virtual time to the next event. */
4237 if (use_icount
== 1) {
4238 /* When not using an adaptive execution frequency
4239 we tend to get badly out of sync with real time,
4240 so just delay for a reasonable amount of time. */
4243 delta
= cpu_get_icount() - cpu_get_clock();
4246 /* If virtual time is ahead of real time then just
4248 timeout
= (delta
/ 1000000) + 1;
4250 /* Wait for either IO to occur or the next
4252 add
= qemu_next_deadline();
4253 /* We advance the timer before checking for IO.
4254 Limit the amount we advance so that early IO
4255 activity won't get the guest too far ahead. */
4259 add
= (add
+ (1 << icount_time_shift
) - 1)
4260 >> icount_time_shift
;
4262 timeout
= delta
/ 1000000;
4269 #else /* CONFIG_IOTHREAD */
4274 static int vm_can_run(void)
4276 if (powerdown_requested
)
4278 if (reset_requested
)
4280 if (shutdown_requested
)
4282 if (debug_requested
)
4287 static void main_loop(void)
4291 #ifdef CONFIG_IOTHREAD
4292 qemu_system_ready
= 1;
4293 qemu_cond_broadcast(&qemu_system_cond
);
4298 #ifdef CONFIG_PROFILER
4301 #ifndef CONFIG_IOTHREAD
4304 #ifdef CONFIG_PROFILER
4305 ti
= profile_getclock();
4307 main_loop_wait(qemu_calculate_timeout());
4308 #ifdef CONFIG_PROFILER
4309 dev_time
+= profile_getclock() - ti
;
4311 } while (vm_can_run());
4313 if (qemu_debug_requested())
4314 vm_stop(EXCP_DEBUG
);
4315 if (qemu_shutdown_requested()) {
4322 if (qemu_reset_requested()) {
4324 qemu_system_reset();
4327 if (qemu_powerdown_requested())
4328 qemu_system_powerdown();
4329 if ((r
= qemu_vmstop_requested()))
4335 static void version(void)
4337 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4340 static void help(int exitcode
)
4343 printf("usage: %s [options] [disk_image]\n"
4345 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4347 #define DEF(option, opt_arg, opt_enum, opt_help) \
4349 #define DEFHEADING(text) stringify(text) "\n"
4350 #include "qemu-options.h"
4355 "During emulation, the following keys are useful:\n"
4356 "ctrl-alt-f toggle full screen\n"
4357 "ctrl-alt-n switch to virtual console 'n'\n"
4358 "ctrl-alt toggle mouse and keyboard grab\n"
4360 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4365 DEFAULT_NETWORK_SCRIPT
,
4366 DEFAULT_NETWORK_DOWN_SCRIPT
,
4368 DEFAULT_GDBSTUB_PORT
,
4373 #define HAS_ARG 0x0001
4376 #define DEF(option, opt_arg, opt_enum, opt_help) \
4378 #define DEFHEADING(text)
4379 #include "qemu-options.h"
4385 typedef struct QEMUOption
{
4391 static const QEMUOption qemu_options
[] = {
4392 { "h", 0, QEMU_OPTION_h
},
4393 #define DEF(option, opt_arg, opt_enum, opt_help) \
4394 { option, opt_arg, opt_enum },
4395 #define DEFHEADING(text)
4396 #include "qemu-options.h"
4404 struct soundhw soundhw
[] = {
4405 #ifdef HAS_AUDIO_CHOICE
4406 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4412 { .init_isa
= pcspk_audio_init
}
4419 "Creative Sound Blaster 16",
4422 { .init_isa
= SB16_init
}
4426 #ifdef CONFIG_CS4231A
4432 { .init_isa
= cs4231a_init
}
4440 "Yamaha YMF262 (OPL3)",
4442 "Yamaha YM3812 (OPL2)",
4446 { .init_isa
= Adlib_init
}
4453 "Gravis Ultrasound GF1",
4456 { .init_isa
= GUS_init
}
4463 "Intel 82801AA AC97 Audio",
4466 { .init_pci
= ac97_init
}
4470 #ifdef CONFIG_ES1370
4473 "ENSONIQ AudioPCI ES1370",
4476 { .init_pci
= es1370_init
}
4480 #endif /* HAS_AUDIO_CHOICE */
4482 { NULL
, NULL
, 0, 0, { NULL
} }
4485 static void select_soundhw (const char *optarg
)
4489 if (*optarg
== '?') {
4492 printf ("Valid sound card names (comma separated):\n");
4493 for (c
= soundhw
; c
->name
; ++c
) {
4494 printf ("%-11s %s\n", c
->name
, c
->descr
);
4496 printf ("\n-soundhw all will enable all of the above\n");
4497 exit (*optarg
!= '?');
4505 if (!strcmp (optarg
, "all")) {
4506 for (c
= soundhw
; c
->name
; ++c
) {
4514 e
= strchr (p
, ',');
4515 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4517 for (c
= soundhw
; c
->name
; ++c
) {
4518 if (!strncmp (c
->name
, p
, l
)) {
4527 "Unknown sound card name (too big to show)\n");
4530 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4535 p
+= l
+ (e
!= NULL
);
4539 goto show_valid_cards
;
4544 static void select_vgahw (const char *p
)
4548 cirrus_vga_enabled
= 0;
4549 std_vga_enabled
= 0;
4552 if (strstart(p
, "std", &opts
)) {
4553 std_vga_enabled
= 1;
4554 } else if (strstart(p
, "cirrus", &opts
)) {
4555 cirrus_vga_enabled
= 1;
4556 } else if (strstart(p
, "vmware", &opts
)) {
4558 } else if (strstart(p
, "xenfb", &opts
)) {
4560 } else if (!strstart(p
, "none", &opts
)) {
4562 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4566 const char *nextopt
;
4568 if (strstart(opts
, ",retrace=", &nextopt
)) {
4570 if (strstart(opts
, "dumb", &nextopt
))
4571 vga_retrace_method
= VGA_RETRACE_DUMB
;
4572 else if (strstart(opts
, "precise", &nextopt
))
4573 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4574 else goto invalid_vga
;
4575 } else goto invalid_vga
;
4581 static int balloon_parse(const char *arg
)
4586 if (!strcmp(arg
, "none")) {
4588 } else if (!strncmp(arg
, "virtio", 6)) {
4590 if (arg
[6] == ',') {
4592 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4593 virtio_balloon_devaddr
= strdup(buf
);
4604 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4606 exit(STATUS_CONTROL_C_EXIT
);
4611 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4615 if(strlen(str
) != 36)
4618 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4619 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4620 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4626 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4632 #define MAX_NET_CLIENTS 32
4636 static void termsig_handler(int signal
)
4638 qemu_system_shutdown_request();
4641 static void sigchld_handler(int signal
)
4643 waitpid(-1, NULL
, WNOHANG
);
4646 static void sighandler_setup(void)
4648 struct sigaction act
;
4650 memset(&act
, 0, sizeof(act
));
4651 act
.sa_handler
= termsig_handler
;
4652 sigaction(SIGINT
, &act
, NULL
);
4653 sigaction(SIGHUP
, &act
, NULL
);
4654 sigaction(SIGTERM
, &act
, NULL
);
4656 act
.sa_handler
= sigchld_handler
;
4657 act
.sa_flags
= SA_NOCLDSTOP
;
4658 sigaction(SIGCHLD
, &act
, NULL
);
4664 /* Look for support files in the same directory as the executable. */
4665 static char *find_datadir(const char *argv0
)
4671 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4678 while (p
!= buf
&& *p
!= '\\')
4681 if (access(buf
, R_OK
) == 0) {
4682 return qemu_strdup(buf
);
4688 /* Find a likely location for support files using the location of the binary.
4689 For installed binaries this will be "$bindir/../share/qemu". When
4690 running from the build tree this will be "$bindir/../pc-bios". */
4691 #define SHARE_SUFFIX "/share/qemu"
4692 #define BUILD_SUFFIX "/pc-bios"
4693 static char *find_datadir(const char *argv0
)
4703 #if defined(__linux__)
4706 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4712 #elif defined(__FreeBSD__)
4715 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4722 /* If we don't have any way of figuring out the actual executable
4723 location then try argv[0]. */
4728 p
= realpath(argv0
, p
);
4736 max_len
= strlen(dir
) +
4737 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4738 res
= qemu_mallocz(max_len
);
4739 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4740 if (access(res
, R_OK
)) {
4741 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4742 if (access(res
, R_OK
)) {
4756 char *qemu_find_file(int type
, const char *name
)
4762 /* If name contains path separators then try it as a straight path. */
4763 if ((strchr(name
, '/') || strchr(name
, '\\'))
4764 && access(name
, R_OK
) == 0) {
4765 return strdup(name
);
4768 case QEMU_FILE_TYPE_BIOS
:
4771 case QEMU_FILE_TYPE_KEYMAP
:
4772 subdir
= "keymaps/";
4777 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4778 buf
= qemu_mallocz(len
);
4779 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4780 if (access(buf
, R_OK
)) {
4787 struct device_config
{
4789 DEV_GENERIC
, /* -device */
4790 DEV_USB
, /* -usbdevice */
4793 const char *cmdline
;
4794 TAILQ_ENTRY(device_config
) next
;
4796 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4798 static void add_device_config(int type
, const char *cmdline
)
4800 struct device_config
*conf
;
4802 conf
= qemu_mallocz(sizeof(*conf
));
4804 conf
->cmdline
= cmdline
;
4805 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4808 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4810 struct device_config
*conf
;
4813 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4814 if (conf
->type
!= type
)
4816 rc
= func(conf
->cmdline
);
4823 static int generic_parse(const char *cmdline
)
4827 dev
= qdev_device_add(cmdline
);
4833 int main(int argc
, char **argv
, char **envp
)
4835 const char *gdbstub_dev
= NULL
;
4836 uint32_t boot_devices_bitmap
= 0;
4838 int snapshot
, linux_boot
, net_boot
;
4839 const char *initrd_filename
;
4840 const char *kernel_filename
, *kernel_cmdline
;
4841 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4843 DisplayChangeListener
*dcl
;
4844 int cyls
, heads
, secs
, translation
;
4845 const char *net_clients
[MAX_NET_CLIENTS
];
4847 QemuOpts
*hda_opts
= NULL
;
4849 const char *r
, *optarg
;
4850 CharDriverState
*monitor_hd
= NULL
;
4851 const char *monitor_device
;
4852 const char *serial_devices
[MAX_SERIAL_PORTS
];
4853 int serial_device_index
;
4854 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4855 int parallel_device_index
;
4856 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4857 int virtio_console_index
;
4858 const char *loadvm
= NULL
;
4859 QEMUMachine
*machine
;
4860 const char *cpu_model
;
4865 const char *pid_file
= NULL
;
4866 const char *incoming
= NULL
;
4869 struct passwd
*pwd
= NULL
;
4870 const char *chroot_dir
= NULL
;
4871 const char *run_as
= NULL
;
4874 int show_vnc_port
= 0;
4876 qemu_cache_utils_init(envp
);
4878 LIST_INIT (&vm_change_state_head
);
4881 struct sigaction act
;
4882 sigfillset(&act
.sa_mask
);
4884 act
.sa_handler
= SIG_IGN
;
4885 sigaction(SIGPIPE
, &act
, NULL
);
4888 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4889 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4890 QEMU to run on a single CPU */
4895 h
= GetCurrentProcess();
4896 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4897 for(i
= 0; i
< 32; i
++) {
4898 if (mask
& (1 << i
))
4903 SetProcessAffinityMask(h
, mask
);
4909 module_call_init(MODULE_INIT_MACHINE
);
4910 machine
= find_default_machine();
4912 initrd_filename
= NULL
;
4915 kernel_filename
= NULL
;
4916 kernel_cmdline
= "";
4917 cyls
= heads
= secs
= 0;
4918 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4919 monitor_device
= "vc:80Cx24C";
4921 serial_devices
[0] = "vc:80Cx24C";
4922 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4923 serial_devices
[i
] = NULL
;
4924 serial_device_index
= 0;
4926 parallel_devices
[0] = "vc:80Cx24C";
4927 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4928 parallel_devices
[i
] = NULL
;
4929 parallel_device_index
= 0;
4931 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4932 virtio_consoles
[i
] = NULL
;
4933 virtio_console_index
= 0;
4935 for (i
= 0; i
< MAX_NODES
; i
++) {
4937 node_cpumask
[i
] = 0;
4947 register_watchdogs();
4955 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4957 const QEMUOption
*popt
;
4960 /* Treat --foo the same as -foo. */
4963 popt
= qemu_options
;
4966 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4970 if (!strcmp(popt
->name
, r
+ 1))
4974 if (popt
->flags
& HAS_ARG
) {
4975 if (optind
>= argc
) {
4976 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4980 optarg
= argv
[optind
++];
4985 switch(popt
->index
) {
4987 machine
= find_machine(optarg
);
4990 printf("Supported machines are:\n");
4991 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4993 printf("%-10s %s (alias of %s)\n",
4994 m
->alias
, m
->desc
, m
->name
);
4995 printf("%-10s %s%s\n",
4997 m
->is_default
? " (default)" : "");
4999 exit(*optarg
!= '?');
5002 case QEMU_OPTION_cpu
:
5003 /* hw initialization will check this */
5004 if (*optarg
== '?') {
5005 /* XXX: implement xxx_cpu_list for targets that still miss it */
5006 #if defined(cpu_list)
5007 cpu_list(stdout
, &fprintf
);
5014 case QEMU_OPTION_initrd
:
5015 initrd_filename
= optarg
;
5017 case QEMU_OPTION_hda
:
5019 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5021 hda_opts
= drive_add(optarg
, HD_ALIAS
5022 ",cyls=%d,heads=%d,secs=%d%s",
5023 0, cyls
, heads
, secs
,
5024 translation
== BIOS_ATA_TRANSLATION_LBA
?
5026 translation
== BIOS_ATA_TRANSLATION_NONE
?
5027 ",trans=none" : "");
5029 case QEMU_OPTION_hdb
:
5030 case QEMU_OPTION_hdc
:
5031 case QEMU_OPTION_hdd
:
5032 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5034 case QEMU_OPTION_drive
:
5035 drive_add(NULL
, "%s", optarg
);
5037 case QEMU_OPTION_mtdblock
:
5038 drive_add(optarg
, MTD_ALIAS
);
5040 case QEMU_OPTION_sd
:
5041 drive_add(optarg
, SD_ALIAS
);
5043 case QEMU_OPTION_pflash
:
5044 drive_add(optarg
, PFLASH_ALIAS
);
5046 case QEMU_OPTION_snapshot
:
5049 case QEMU_OPTION_hdachs
:
5053 cyls
= strtol(p
, (char **)&p
, 0);
5054 if (cyls
< 1 || cyls
> 16383)
5059 heads
= strtol(p
, (char **)&p
, 0);
5060 if (heads
< 1 || heads
> 16)
5065 secs
= strtol(p
, (char **)&p
, 0);
5066 if (secs
< 1 || secs
> 63)
5070 if (!strcmp(p
, "none"))
5071 translation
= BIOS_ATA_TRANSLATION_NONE
;
5072 else if (!strcmp(p
, "lba"))
5073 translation
= BIOS_ATA_TRANSLATION_LBA
;
5074 else if (!strcmp(p
, "auto"))
5075 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5078 } else if (*p
!= '\0') {
5080 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5083 if (hda_opts
!= NULL
) {
5085 snprintf(num
, sizeof(num
), "%d", cyls
);
5086 qemu_opt_set(hda_opts
, "cyls", num
);
5087 snprintf(num
, sizeof(num
), "%d", heads
);
5088 qemu_opt_set(hda_opts
, "heads", num
);
5089 snprintf(num
, sizeof(num
), "%d", secs
);
5090 qemu_opt_set(hda_opts
, "secs", num
);
5091 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5092 qemu_opt_set(hda_opts
, "trans", "lba");
5093 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5094 qemu_opt_set(hda_opts
, "trans", "none");
5098 case QEMU_OPTION_numa
:
5099 if (nb_numa_nodes
>= MAX_NODES
) {
5100 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5105 case QEMU_OPTION_nographic
:
5106 display_type
= DT_NOGRAPHIC
;
5108 #ifdef CONFIG_CURSES
5109 case QEMU_OPTION_curses
:
5110 display_type
= DT_CURSES
;
5113 case QEMU_OPTION_portrait
:
5116 case QEMU_OPTION_kernel
:
5117 kernel_filename
= optarg
;
5119 case QEMU_OPTION_append
:
5120 kernel_cmdline
= optarg
;
5122 case QEMU_OPTION_cdrom
:
5123 drive_add(optarg
, CDROM_ALIAS
);
5125 case QEMU_OPTION_boot
:
5127 static const char * const params
[] = {
5128 "order", "once", "menu", NULL
5130 char buf
[sizeof(boot_devices
)];
5131 char *standard_boot_devices
;
5134 if (!strchr(optarg
, '=')) {
5136 pstrcpy(buf
, sizeof(buf
), optarg
);
5137 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5139 "qemu: unknown boot parameter '%s' in '%s'\n",
5145 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5146 boot_devices_bitmap
= parse_bootdevices(buf
);
5147 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5150 if (get_param_value(buf
, sizeof(buf
),
5152 boot_devices_bitmap
|= parse_bootdevices(buf
);
5153 standard_boot_devices
= qemu_strdup(boot_devices
);
5154 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5155 qemu_register_reset(restore_boot_devices
,
5156 standard_boot_devices
);
5158 if (get_param_value(buf
, sizeof(buf
),
5160 if (!strcmp(buf
, "on")) {
5162 } else if (!strcmp(buf
, "off")) {
5166 "qemu: invalid option value '%s'\n",
5174 case QEMU_OPTION_fda
:
5175 case QEMU_OPTION_fdb
:
5176 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5179 case QEMU_OPTION_no_fd_bootchk
:
5183 case QEMU_OPTION_net
:
5184 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5185 fprintf(stderr
, "qemu: too many network clients\n");
5188 net_clients
[nb_net_clients
] = optarg
;
5192 case QEMU_OPTION_tftp
:
5193 legacy_tftp_prefix
= optarg
;
5195 case QEMU_OPTION_bootp
:
5196 legacy_bootp_filename
= optarg
;
5199 case QEMU_OPTION_smb
:
5200 net_slirp_smb(optarg
);
5203 case QEMU_OPTION_redir
:
5204 net_slirp_redir(optarg
);
5207 case QEMU_OPTION_bt
:
5208 add_device_config(DEV_BT
, optarg
);
5211 case QEMU_OPTION_audio_help
:
5215 case QEMU_OPTION_soundhw
:
5216 select_soundhw (optarg
);
5222 case QEMU_OPTION_version
:
5226 case QEMU_OPTION_m
: {
5230 value
= strtoul(optarg
, &ptr
, 10);
5232 case 0: case 'M': case 'm':
5239 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5243 /* On 32-bit hosts, QEMU is limited by virtual address space */
5244 if (value
> (2047 << 20)
5245 #ifndef CONFIG_KQEMU
5246 && HOST_LONG_BITS
== 32
5249 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5252 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5253 fprintf(stderr
, "qemu: ram size too large\n");
5262 const CPULogItem
*item
;
5264 mask
= cpu_str_to_log_mask(optarg
);
5266 printf("Log items (comma separated):\n");
5267 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5268 printf("%-10s %s\n", item
->name
, item
->help
);
5276 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5278 case QEMU_OPTION_gdb
:
5279 gdbstub_dev
= optarg
;
5284 case QEMU_OPTION_bios
:
5287 case QEMU_OPTION_singlestep
:
5295 keyboard_layout
= optarg
;
5298 case QEMU_OPTION_localtime
:
5301 case QEMU_OPTION_vga
:
5302 select_vgahw (optarg
);
5304 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5310 w
= strtol(p
, (char **)&p
, 10);
5313 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5319 h
= strtol(p
, (char **)&p
, 10);
5324 depth
= strtol(p
, (char **)&p
, 10);
5325 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5326 depth
!= 24 && depth
!= 32)
5328 } else if (*p
== '\0') {
5329 depth
= graphic_depth
;
5336 graphic_depth
= depth
;
5340 case QEMU_OPTION_echr
:
5343 term_escape_char
= strtol(optarg
, &r
, 0);
5345 printf("Bad argument to echr\n");
5348 case QEMU_OPTION_monitor
:
5349 monitor_device
= optarg
;
5351 case QEMU_OPTION_serial
:
5352 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5353 fprintf(stderr
, "qemu: too many serial ports\n");
5356 serial_devices
[serial_device_index
] = optarg
;
5357 serial_device_index
++;
5359 case QEMU_OPTION_watchdog
:
5360 i
= select_watchdog(optarg
);
5362 exit (i
== 1 ? 1 : 0);
5364 case QEMU_OPTION_watchdog_action
:
5365 if (select_watchdog_action(optarg
) == -1) {
5366 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5370 case QEMU_OPTION_virtiocon
:
5371 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5372 fprintf(stderr
, "qemu: too many virtio consoles\n");
5375 virtio_consoles
[virtio_console_index
] = optarg
;
5376 virtio_console_index
++;
5378 case QEMU_OPTION_parallel
:
5379 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5380 fprintf(stderr
, "qemu: too many parallel ports\n");
5383 parallel_devices
[parallel_device_index
] = optarg
;
5384 parallel_device_index
++;
5386 case QEMU_OPTION_loadvm
:
5389 case QEMU_OPTION_full_screen
:
5393 case QEMU_OPTION_no_frame
:
5396 case QEMU_OPTION_alt_grab
:
5399 case QEMU_OPTION_no_quit
:
5402 case QEMU_OPTION_sdl
:
5403 display_type
= DT_SDL
;
5406 case QEMU_OPTION_pidfile
:
5410 case QEMU_OPTION_win2k_hack
:
5411 win2k_install_hack
= 1;
5413 case QEMU_OPTION_rtc_td_hack
:
5416 case QEMU_OPTION_acpitable
:
5417 if(acpi_table_add(optarg
) < 0) {
5418 fprintf(stderr
, "Wrong acpi table provided\n");
5422 case QEMU_OPTION_smbios
:
5423 if(smbios_entry_add(optarg
) < 0) {
5424 fprintf(stderr
, "Wrong smbios provided\n");
5430 case QEMU_OPTION_enable_kqemu
:
5433 case QEMU_OPTION_kernel_kqemu
:
5438 case QEMU_OPTION_enable_kvm
:
5445 case QEMU_OPTION_usb
:
5448 case QEMU_OPTION_usbdevice
:
5450 add_device_config(DEV_USB
, optarg
);
5452 case QEMU_OPTION_device
:
5453 add_device_config(DEV_GENERIC
, optarg
);
5455 case QEMU_OPTION_smp
:
5456 smp_cpus
= atoi(optarg
);
5458 fprintf(stderr
, "Invalid number of CPUs\n");
5462 case QEMU_OPTION_vnc
:
5463 display_type
= DT_VNC
;
5464 vnc_display
= optarg
;
5467 case QEMU_OPTION_no_acpi
:
5470 case QEMU_OPTION_no_hpet
:
5473 case QEMU_OPTION_balloon
:
5474 if (balloon_parse(optarg
) < 0) {
5475 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5480 case QEMU_OPTION_no_reboot
:
5483 case QEMU_OPTION_no_shutdown
:
5486 case QEMU_OPTION_show_cursor
:
5489 case QEMU_OPTION_uuid
:
5490 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5491 fprintf(stderr
, "Fail to parse UUID string."
5492 " Wrong format.\n");
5497 case QEMU_OPTION_daemonize
:
5501 case QEMU_OPTION_option_rom
:
5502 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5503 fprintf(stderr
, "Too many option ROMs\n");
5506 option_rom
[nb_option_roms
] = optarg
;
5509 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5510 case QEMU_OPTION_semihosting
:
5511 semihosting_enabled
= 1;
5514 case QEMU_OPTION_name
:
5515 qemu_name
= qemu_strdup(optarg
);
5517 char *p
= strchr(qemu_name
, ',');
5520 if (strncmp(p
, "process=", 8)) {
5521 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5529 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5530 case QEMU_OPTION_prom_env
:
5531 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5532 fprintf(stderr
, "Too many prom variables\n");
5535 prom_envs
[nb_prom_envs
] = optarg
;
5540 case QEMU_OPTION_old_param
:
5544 case QEMU_OPTION_clock
:
5545 configure_alarms(optarg
);
5547 case QEMU_OPTION_startdate
:
5550 time_t rtc_start_date
;
5551 if (!strcmp(optarg
, "now")) {
5552 rtc_date_offset
= -1;
5554 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5562 } else if (sscanf(optarg
, "%d-%d-%d",
5565 &tm
.tm_mday
) == 3) {
5574 rtc_start_date
= mktimegm(&tm
);
5575 if (rtc_start_date
== -1) {
5577 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5578 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5581 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5585 case QEMU_OPTION_tb_size
:
5586 tb_size
= strtol(optarg
, NULL
, 0);
5590 case QEMU_OPTION_icount
:
5592 if (strcmp(optarg
, "auto") == 0) {
5593 icount_time_shift
= -1;
5595 icount_time_shift
= strtol(optarg
, NULL
, 0);
5598 case QEMU_OPTION_incoming
:
5602 case QEMU_OPTION_chroot
:
5603 chroot_dir
= optarg
;
5605 case QEMU_OPTION_runas
:
5610 case QEMU_OPTION_xen_domid
:
5611 xen_domid
= atoi(optarg
);
5613 case QEMU_OPTION_xen_create
:
5614 xen_mode
= XEN_CREATE
;
5616 case QEMU_OPTION_xen_attach
:
5617 xen_mode
= XEN_ATTACH
;
5624 /* If no data_dir is specified then try to find it relative to the
5627 data_dir
= find_datadir(argv
[0]);
5629 /* If all else fails use the install patch specified when building. */
5631 data_dir
= CONFIG_QEMU_SHAREDIR
;
5634 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5635 if (kvm_allowed
&& kqemu_allowed
) {
5637 "You can not enable both KVM and kqemu at the same time\n");
5642 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5643 if (smp_cpus
> machine
->max_cpus
) {
5644 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5645 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5650 if (display_type
== DT_NOGRAPHIC
) {
5651 if (serial_device_index
== 0)
5652 serial_devices
[0] = "stdio";
5653 if (parallel_device_index
== 0)
5654 parallel_devices
[0] = "null";
5655 if (strncmp(monitor_device
, "vc", 2) == 0)
5656 monitor_device
= "stdio";
5663 if (pipe(fds
) == -1)
5674 len
= read(fds
[0], &status
, 1);
5675 if (len
== -1 && (errno
== EINTR
))
5680 else if (status
== 1) {
5681 fprintf(stderr
, "Could not acquire pidfile\n");
5698 signal(SIGTSTP
, SIG_IGN
);
5699 signal(SIGTTOU
, SIG_IGN
);
5700 signal(SIGTTIN
, SIG_IGN
);
5703 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5706 write(fds
[1], &status
, 1);
5708 fprintf(stderr
, "Could not acquire pid file\n");
5717 if (qemu_init_main_loop()) {
5718 fprintf(stderr
, "qemu_init_main_loop failed\n");
5721 linux_boot
= (kernel_filename
!= NULL
);
5723 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5724 fprintf(stderr
, "-append only allowed with -kernel option\n");
5728 if (!linux_boot
&& initrd_filename
!= NULL
) {
5729 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5733 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5736 if (init_timer_alarm() < 0) {
5737 fprintf(stderr
, "could not initialize alarm timer\n");
5740 if (use_icount
&& icount_time_shift
< 0) {
5742 /* 125MIPS seems a reasonable initial guess at the guest speed.
5743 It will be corrected fairly quickly anyway. */
5744 icount_time_shift
= 3;
5745 init_icount_adjust();
5752 /* init network clients */
5753 if (nb_net_clients
== 0) {
5754 /* if no clients, we use a default config */
5755 net_clients
[nb_net_clients
++] = "nic";
5757 net_clients
[nb_net_clients
++] = "user";
5761 for(i
= 0;i
< nb_net_clients
; i
++) {
5762 if (net_client_parse(net_clients
[i
]) < 0)
5766 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5767 net_set_boot_mask(net_boot
);
5771 /* init the bluetooth world */
5772 if (foreach_device_config(DEV_BT
, bt_parse
))
5775 /* init the memory */
5777 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5780 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5781 guest ram allocation. It needs to go away. */
5782 if (kqemu_allowed
) {
5783 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5784 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5785 if (!kqemu_phys_ram_base
) {
5786 fprintf(stderr
, "Could not allocate physical memory\n");
5792 /* init the dynamic translator */
5793 cpu_exec_init_all(tb_size
* 1024 * 1024);
5797 /* we always create the cdrom drive, even if no disk is there */
5798 drive_add(NULL
, CDROM_ALIAS
);
5800 /* we always create at least one floppy */
5801 drive_add(NULL
, FD_ALIAS
, 0);
5803 /* we always create one sd slot, even if no card is in it */
5804 drive_add(NULL
, SD_ALIAS
);
5806 /* open the virtual block devices */
5808 qemu_opts_foreach(&drive_opt_list
, drive_enable_snapshot
, NULL
, 0);
5809 if (qemu_opts_foreach(&drive_opt_list
, drive_init_func
, machine
, 1) != 0)
5812 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5813 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5816 /* must be after terminal init, SDL library changes signal handlers */
5820 /* Maintain compatibility with multiple stdio monitors */
5821 if (!strcmp(monitor_device
,"stdio")) {
5822 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5823 const char *devname
= serial_devices
[i
];
5824 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5825 monitor_device
= NULL
;
5827 } else if (devname
&& !strcmp(devname
,"stdio")) {
5828 monitor_device
= NULL
;
5829 serial_devices
[i
] = "mon:stdio";
5835 if (nb_numa_nodes
> 0) {
5838 if (nb_numa_nodes
> smp_cpus
) {
5839 nb_numa_nodes
= smp_cpus
;
5842 /* If no memory size if given for any node, assume the default case
5843 * and distribute the available memory equally across all nodes
5845 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5846 if (node_mem
[i
] != 0)
5849 if (i
== nb_numa_nodes
) {
5850 uint64_t usedmem
= 0;
5852 /* On Linux, the each node's border has to be 8MB aligned,
5853 * the final node gets the rest.
5855 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5856 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5857 usedmem
+= node_mem
[i
];
5859 node_mem
[i
] = ram_size
- usedmem
;
5862 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5863 if (node_cpumask
[i
] != 0)
5866 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5867 * must cope with this anyway, because there are BIOSes out there in
5868 * real machines which also use this scheme.
5870 if (i
== nb_numa_nodes
) {
5871 for (i
= 0; i
< smp_cpus
; i
++) {
5872 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5877 if (kvm_enabled()) {
5880 ret
= kvm_init(smp_cpus
);
5882 fprintf(stderr
, "failed to initialize KVM\n");
5887 if (monitor_device
) {
5888 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5890 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5895 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5896 const char *devname
= serial_devices
[i
];
5897 if (devname
&& strcmp(devname
, "none")) {
5899 snprintf(label
, sizeof(label
), "serial%d", i
);
5900 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5901 if (!serial_hds
[i
]) {
5902 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5909 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5910 const char *devname
= parallel_devices
[i
];
5911 if (devname
&& strcmp(devname
, "none")) {
5913 snprintf(label
, sizeof(label
), "parallel%d", i
);
5914 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5915 if (!parallel_hds
[i
]) {
5916 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5923 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5924 const char *devname
= virtio_consoles
[i
];
5925 if (devname
&& strcmp(devname
, "none")) {
5927 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5928 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5929 if (!virtcon_hds
[i
]) {
5930 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5937 module_call_init(MODULE_INIT_DEVICE
);
5939 if (machine
->compat_props
) {
5940 qdev_prop_register_compat(machine
->compat_props
);
5942 machine
->init(ram_size
, boot_devices
,
5943 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5946 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5947 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5948 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5954 current_machine
= machine
;
5956 /* init USB devices */
5958 foreach_device_config(DEV_USB
, usb_parse
);
5961 /* init generic devices */
5962 if (foreach_device_config(DEV_GENERIC
, generic_parse
))
5966 dumb_display_init();
5967 /* just use the first displaystate for the moment */
5970 if (display_type
== DT_DEFAULT
) {
5971 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5972 display_type
= DT_SDL
;
5974 display_type
= DT_VNC
;
5975 vnc_display
= "localhost:0,to=99";
5981 switch (display_type
) {
5984 #if defined(CONFIG_CURSES)
5986 curses_display_init(ds
, full_screen
);
5989 #if defined(CONFIG_SDL)
5991 sdl_display_init(ds
, full_screen
, no_frame
);
5993 #elif defined(CONFIG_COCOA)
5995 cocoa_display_init(ds
, full_screen
);
5999 vnc_display_init(ds
);
6000 if (vnc_display_open(ds
, vnc_display
) < 0)
6003 if (show_vnc_port
) {
6004 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6012 dcl
= ds
->listeners
;
6013 while (dcl
!= NULL
) {
6014 if (dcl
->dpy_refresh
!= NULL
) {
6015 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6016 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6021 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6022 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6023 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6026 text_consoles_set_display(display_state
);
6027 qemu_chr_initial_reset();
6029 if (monitor_device
&& monitor_hd
)
6030 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6032 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6033 const char *devname
= serial_devices
[i
];
6034 if (devname
&& strcmp(devname
, "none")) {
6035 if (strstart(devname
, "vc", 0))
6036 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6040 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6041 const char *devname
= parallel_devices
[i
];
6042 if (devname
&& strcmp(devname
, "none")) {
6043 if (strstart(devname
, "vc", 0))
6044 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6048 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6049 const char *devname
= virtio_consoles
[i
];
6050 if (virtcon_hds
[i
] && devname
) {
6051 if (strstart(devname
, "vc", 0))
6052 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6056 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6057 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6063 do_loadvm(cur_mon
, loadvm
);
6066 qemu_start_incoming_migration(incoming
);
6077 len
= write(fds
[1], &status
, 1);
6078 if (len
== -1 && (errno
== EINTR
))
6085 TFR(fd
= open("/dev/null", O_RDWR
));
6091 pwd
= getpwnam(run_as
);
6093 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6099 if (chroot(chroot_dir
) < 0) {
6100 fprintf(stderr
, "chroot failed\n");
6107 if (setgid(pwd
->pw_gid
) < 0) {
6108 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6111 if (setuid(pwd
->pw_uid
) < 0) {
6112 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6115 if (setuid(0) != -1) {
6116 fprintf(stderr
, "Dropping privileges failed\n");