4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
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>
111 #include <mmsystem.h>
115 #if defined(__APPLE__) || defined(main)
117 int qemu_main(int argc
, char **argv
, char **envp
);
118 int main(int argc
, char **argv
)
120 return qemu_main(argc
, argv
, NULL
);
123 #define main qemu_main
125 #endif /* CONFIG_SDL */
129 #define main qemu_main
130 #endif /* CONFIG_COCOA */
133 #include "hw/boards.h"
135 #include "hw/pcmcia.h"
137 #include "hw/audiodev.h"
141 #include "hw/watchdog.h"
142 #include "hw/smbios.h"
151 #include "qemu-timer.h"
152 #include "qemu-char.h"
153 #include "cache-utils.h"
156 #include "audio/audio.h"
157 #include "migration.h"
160 #include "qemu-option.h"
164 #include "exec-all.h"
166 #include "qemu_socket.h"
168 #include "slirp/libslirp.h"
171 //#define DEBUG_SLIRP
173 #define DEFAULT_RAM_SIZE 128
175 static const char *data_dir
;
176 const char *bios_name
= NULL
;
177 /* Note: drives_table[MAX_DRIVES] is a dummy block driver if none available
178 to store the VM snapshots */
179 struct drivelist drives
= TAILQ_HEAD_INITIALIZER(drives
);
180 struct driveoptlist driveopts
= TAILQ_HEAD_INITIALIZER(driveopts
);
181 enum vga_retrace_method vga_retrace_method
= VGA_RETRACE_DUMB
;
182 static DisplayState
*display_state
;
183 DisplayType display_type
= DT_DEFAULT
;
184 const char* keyboard_layout
= NULL
;
185 int64_t ticks_per_sec
;
188 NICInfo nd_table
[MAX_NICS
];
190 static int autostart
;
191 static int rtc_utc
= 1;
192 static int rtc_date_offset
= -1; /* -1 means no change */
193 int cirrus_vga_enabled
= 1;
194 int std_vga_enabled
= 0;
195 int vmsvga_enabled
= 0;
196 int xenfb_enabled
= 0;
198 int graphic_width
= 1024;
199 int graphic_height
= 768;
200 int graphic_depth
= 8;
202 int graphic_width
= 800;
203 int graphic_height
= 600;
204 int graphic_depth
= 15;
206 static int full_screen
= 0;
208 static int no_frame
= 0;
211 CharDriverState
*serial_hds
[MAX_SERIAL_PORTS
];
212 CharDriverState
*parallel_hds
[MAX_PARALLEL_PORTS
];
213 CharDriverState
*virtcon_hds
[MAX_VIRTIO_CONSOLES
];
215 int win2k_install_hack
= 0;
222 const char *vnc_display
;
223 int acpi_enabled
= 1;
225 int virtio_balloon
= 1;
226 const char *virtio_balloon_devaddr
;
231 int graphic_rotate
= 0;
235 WatchdogTimerModel
*watchdog
= NULL
;
236 int watchdog_action
= WDT_RESET
;
237 const char *option_rom
[MAX_OPTION_ROMS
];
239 int semihosting_enabled
= 0;
243 const char *qemu_name
;
245 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
246 unsigned int nb_prom_envs
= 0;
247 const char *prom_envs
[MAX_PROM_ENVS
];
252 uint64_t node_mem
[MAX_NODES
];
253 uint64_t node_cpumask
[MAX_NODES
];
255 static CPUState
*cur_cpu
;
256 static CPUState
*next_cpu
;
257 static int timer_alarm_pending
= 1;
258 /* Conversion factor from emulated instructions to virtual clock ticks. */
259 static int icount_time_shift
;
260 /* Arbitrarily pick 1MIPS as the minimum allowable speed. */
261 #define MAX_ICOUNT_SHIFT 10
262 /* Compensate for varying guest execution speed. */
263 static int64_t qemu_icount_bias
;
264 static QEMUTimer
*icount_rt_timer
;
265 static QEMUTimer
*icount_vm_timer
;
266 static QEMUTimer
*nographic_timer
;
268 uint8_t qemu_uuid
[16];
270 static QEMUBootSetHandler
*boot_set_handler
;
271 static void *boot_set_opaque
;
273 /***********************************************************/
274 /* x86 ISA bus support */
276 target_phys_addr_t isa_mem_base
= 0;
279 /***********************************************************/
280 void hw_error(const char *fmt
, ...)
286 fprintf(stderr
, "qemu: hardware error: ");
287 vfprintf(stderr
, fmt
, ap
);
288 fprintf(stderr
, "\n");
289 for(env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
290 fprintf(stderr
, "CPU #%d:\n", env
->cpu_index
);
292 cpu_dump_state(env
, stderr
, fprintf
, X86_DUMP_FPU
);
294 cpu_dump_state(env
, stderr
, fprintf
, 0);
301 static void set_proc_name(const char *s
)
307 name
[sizeof(name
) - 1] = 0;
308 strncpy(name
, s
, sizeof(name
));
309 /* Could rewrite argv[0] too, but that's a bit more complicated.
310 This simple way is enough for `top'. */
311 prctl(PR_SET_NAME
, name
);
318 static QEMUBalloonEvent
*qemu_balloon_event
;
319 void *qemu_balloon_event_opaque
;
321 void qemu_add_balloon_handler(QEMUBalloonEvent
*func
, void *opaque
)
323 qemu_balloon_event
= func
;
324 qemu_balloon_event_opaque
= opaque
;
327 void qemu_balloon(ram_addr_t target
)
329 if (qemu_balloon_event
)
330 qemu_balloon_event(qemu_balloon_event_opaque
, target
);
333 ram_addr_t
qemu_balloon_status(void)
335 if (qemu_balloon_event
)
336 return qemu_balloon_event(qemu_balloon_event_opaque
, 0);
340 /***********************************************************/
343 static QEMUPutKBDEvent
*qemu_put_kbd_event
;
344 static void *qemu_put_kbd_event_opaque
;
345 static QEMUPutMouseEntry
*qemu_put_mouse_event_head
;
346 static QEMUPutMouseEntry
*qemu_put_mouse_event_current
;
348 void qemu_add_kbd_event_handler(QEMUPutKBDEvent
*func
, void *opaque
)
350 qemu_put_kbd_event_opaque
= opaque
;
351 qemu_put_kbd_event
= func
;
354 QEMUPutMouseEntry
*qemu_add_mouse_event_handler(QEMUPutMouseEvent
*func
,
355 void *opaque
, int absolute
,
358 QEMUPutMouseEntry
*s
, *cursor
;
360 s
= qemu_mallocz(sizeof(QEMUPutMouseEntry
));
362 s
->qemu_put_mouse_event
= func
;
363 s
->qemu_put_mouse_event_opaque
= opaque
;
364 s
->qemu_put_mouse_event_absolute
= absolute
;
365 s
->qemu_put_mouse_event_name
= qemu_strdup(name
);
368 if (!qemu_put_mouse_event_head
) {
369 qemu_put_mouse_event_head
= qemu_put_mouse_event_current
= s
;
373 cursor
= qemu_put_mouse_event_head
;
374 while (cursor
->next
!= NULL
)
375 cursor
= cursor
->next
;
378 qemu_put_mouse_event_current
= s
;
383 void qemu_remove_mouse_event_handler(QEMUPutMouseEntry
*entry
)
385 QEMUPutMouseEntry
*prev
= NULL
, *cursor
;
387 if (!qemu_put_mouse_event_head
|| entry
== NULL
)
390 cursor
= qemu_put_mouse_event_head
;
391 while (cursor
!= NULL
&& cursor
!= entry
) {
393 cursor
= cursor
->next
;
396 if (cursor
== NULL
) // does not exist or list empty
398 else if (prev
== NULL
) { // entry is head
399 qemu_put_mouse_event_head
= cursor
->next
;
400 if (qemu_put_mouse_event_current
== entry
)
401 qemu_put_mouse_event_current
= cursor
->next
;
402 qemu_free(entry
->qemu_put_mouse_event_name
);
407 prev
->next
= entry
->next
;
409 if (qemu_put_mouse_event_current
== entry
)
410 qemu_put_mouse_event_current
= prev
;
412 qemu_free(entry
->qemu_put_mouse_event_name
);
416 void kbd_put_keycode(int keycode
)
418 if (qemu_put_kbd_event
) {
419 qemu_put_kbd_event(qemu_put_kbd_event_opaque
, keycode
);
423 void kbd_mouse_event(int dx
, int dy
, int dz
, int buttons_state
)
425 QEMUPutMouseEvent
*mouse_event
;
426 void *mouse_event_opaque
;
429 if (!qemu_put_mouse_event_current
) {
434 qemu_put_mouse_event_current
->qemu_put_mouse_event
;
436 qemu_put_mouse_event_current
->qemu_put_mouse_event_opaque
;
439 if (graphic_rotate
) {
440 if (qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
)
443 width
= graphic_width
- 1;
444 mouse_event(mouse_event_opaque
,
445 width
- dy
, dx
, dz
, buttons_state
);
447 mouse_event(mouse_event_opaque
,
448 dx
, dy
, dz
, buttons_state
);
452 int kbd_mouse_is_absolute(void)
454 if (!qemu_put_mouse_event_current
)
457 return qemu_put_mouse_event_current
->qemu_put_mouse_event_absolute
;
460 void do_info_mice(Monitor
*mon
)
462 QEMUPutMouseEntry
*cursor
;
465 if (!qemu_put_mouse_event_head
) {
466 monitor_printf(mon
, "No mouse devices connected\n");
470 monitor_printf(mon
, "Mouse devices available:\n");
471 cursor
= qemu_put_mouse_event_head
;
472 while (cursor
!= NULL
) {
473 monitor_printf(mon
, "%c Mouse #%d: %s\n",
474 (cursor
== qemu_put_mouse_event_current
? '*' : ' '),
475 index
, cursor
->qemu_put_mouse_event_name
);
477 cursor
= cursor
->next
;
481 void do_mouse_set(Monitor
*mon
, int index
)
483 QEMUPutMouseEntry
*cursor
;
486 if (!qemu_put_mouse_event_head
) {
487 monitor_printf(mon
, "No mouse devices connected\n");
491 cursor
= qemu_put_mouse_event_head
;
492 while (cursor
!= NULL
&& index
!= i
) {
494 cursor
= cursor
->next
;
498 qemu_put_mouse_event_current
= cursor
;
500 monitor_printf(mon
, "Mouse at given index not found\n");
503 /* compute with 96 bit intermediate result: (a*b)/c */
504 uint64_t muldiv64(uint64_t a
, uint32_t b
, uint32_t c
)
509 #ifdef HOST_WORDS_BIGENDIAN
519 rl
= (uint64_t)u
.l
.low
* (uint64_t)b
;
520 rh
= (uint64_t)u
.l
.high
* (uint64_t)b
;
523 res
.l
.low
= (((rh
% c
) << 32) + (rl
& 0xffffffff)) / c
;
527 /***********************************************************/
528 /* real time host monotonic timer */
530 #define QEMU_TIMER_BASE 1000000000LL
534 static int64_t clock_freq
;
536 static void init_get_clock(void)
540 ret
= QueryPerformanceFrequency(&freq
);
542 fprintf(stderr
, "Could not calibrate ticks\n");
545 clock_freq
= freq
.QuadPart
;
548 static int64_t get_clock(void)
551 QueryPerformanceCounter(&ti
);
552 return muldiv64(ti
.QuadPart
, QEMU_TIMER_BASE
, clock_freq
);
557 static int use_rt_clock
;
559 static void init_get_clock(void)
562 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
563 || defined(__DragonFly__)
566 if (clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0) {
573 static int64_t get_clock(void)
575 #if defined(__linux__) || (defined(__FreeBSD__) && __FreeBSD_version >= 500000) \
576 || defined(__DragonFly__)
579 clock_gettime(CLOCK_MONOTONIC
, &ts
);
580 return ts
.tv_sec
* 1000000000LL + ts
.tv_nsec
;
584 /* XXX: using gettimeofday leads to problems if the date
585 changes, so it should be avoided. */
587 gettimeofday(&tv
, NULL
);
588 return tv
.tv_sec
* 1000000000LL + (tv
.tv_usec
* 1000);
593 /* Return the virtual CPU time, based on the instruction counter. */
594 static int64_t cpu_get_icount(void)
597 CPUState
*env
= cpu_single_env
;;
598 icount
= qemu_icount
;
601 fprintf(stderr
, "Bad clock read\n");
602 icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
604 return qemu_icount_bias
+ (icount
<< icount_time_shift
);
607 /***********************************************************/
608 /* guest cycle counter */
610 static int64_t cpu_ticks_prev
;
611 static int64_t cpu_ticks_offset
;
612 static int64_t cpu_clock_offset
;
613 static int cpu_ticks_enabled
;
615 /* return the host CPU cycle counter and handle stop/restart */
616 int64_t cpu_get_ticks(void)
619 return cpu_get_icount();
621 if (!cpu_ticks_enabled
) {
622 return cpu_ticks_offset
;
625 ticks
= cpu_get_real_ticks();
626 if (cpu_ticks_prev
> ticks
) {
627 /* Note: non increasing ticks may happen if the host uses
629 cpu_ticks_offset
+= cpu_ticks_prev
- ticks
;
631 cpu_ticks_prev
= ticks
;
632 return ticks
+ cpu_ticks_offset
;
636 /* return the host CPU monotonic timer and handle stop/restart */
637 static int64_t cpu_get_clock(void)
640 if (!cpu_ticks_enabled
) {
641 return cpu_clock_offset
;
644 return ti
+ cpu_clock_offset
;
648 /* enable cpu_get_ticks() */
649 void cpu_enable_ticks(void)
651 if (!cpu_ticks_enabled
) {
652 cpu_ticks_offset
-= cpu_get_real_ticks();
653 cpu_clock_offset
-= get_clock();
654 cpu_ticks_enabled
= 1;
658 /* disable cpu_get_ticks() : the clock is stopped. You must not call
659 cpu_get_ticks() after that. */
660 void cpu_disable_ticks(void)
662 if (cpu_ticks_enabled
) {
663 cpu_ticks_offset
= cpu_get_ticks();
664 cpu_clock_offset
= cpu_get_clock();
665 cpu_ticks_enabled
= 0;
669 /***********************************************************/
672 #define QEMU_TIMER_REALTIME 0
673 #define QEMU_TIMER_VIRTUAL 1
677 /* XXX: add frequency */
685 struct QEMUTimer
*next
;
688 struct qemu_alarm_timer
{
692 int (*start
)(struct qemu_alarm_timer
*t
);
693 void (*stop
)(struct qemu_alarm_timer
*t
);
694 void (*rearm
)(struct qemu_alarm_timer
*t
);
698 #define ALARM_FLAG_DYNTICKS 0x1
699 #define ALARM_FLAG_EXPIRED 0x2
701 static inline int alarm_has_dynticks(struct qemu_alarm_timer
*t
)
703 return t
&& (t
->flags
& ALARM_FLAG_DYNTICKS
);
706 static void qemu_rearm_alarm_timer(struct qemu_alarm_timer
*t
)
708 if (!alarm_has_dynticks(t
))
714 /* TODO: MIN_TIMER_REARM_US should be optimized */
715 #define MIN_TIMER_REARM_US 250
717 static struct qemu_alarm_timer
*alarm_timer
;
721 struct qemu_alarm_win32
{
724 } alarm_win32_data
= {0, -1};
726 static int win32_start_timer(struct qemu_alarm_timer
*t
);
727 static void win32_stop_timer(struct qemu_alarm_timer
*t
);
728 static void win32_rearm_timer(struct qemu_alarm_timer
*t
);
732 static int unix_start_timer(struct qemu_alarm_timer
*t
);
733 static void unix_stop_timer(struct qemu_alarm_timer
*t
);
737 static int dynticks_start_timer(struct qemu_alarm_timer
*t
);
738 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
);
739 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
);
741 static int hpet_start_timer(struct qemu_alarm_timer
*t
);
742 static void hpet_stop_timer(struct qemu_alarm_timer
*t
);
744 static int rtc_start_timer(struct qemu_alarm_timer
*t
);
745 static void rtc_stop_timer(struct qemu_alarm_timer
*t
);
747 #endif /* __linux__ */
751 /* Correlation between real and virtual time is always going to be
752 fairly approximate, so ignore small variation.
753 When the guest is idle real and virtual time will be aligned in
755 #define ICOUNT_WOBBLE (QEMU_TIMER_BASE / 10)
757 static void icount_adjust(void)
762 static int64_t last_delta
;
763 /* If the VM is not running, then do nothing. */
767 cur_time
= cpu_get_clock();
768 cur_icount
= qemu_get_clock(vm_clock
);
769 delta
= cur_icount
- cur_time
;
770 /* FIXME: This is a very crude algorithm, somewhat prone to oscillation. */
772 && last_delta
+ ICOUNT_WOBBLE
< delta
* 2
773 && icount_time_shift
> 0) {
774 /* The guest is getting too far ahead. Slow time down. */
778 && last_delta
- ICOUNT_WOBBLE
> delta
* 2
779 && icount_time_shift
< MAX_ICOUNT_SHIFT
) {
780 /* The guest is getting too far behind. Speed time up. */
784 qemu_icount_bias
= cur_icount
- (qemu_icount
<< icount_time_shift
);
787 static void icount_adjust_rt(void * opaque
)
789 qemu_mod_timer(icount_rt_timer
,
790 qemu_get_clock(rt_clock
) + 1000);
794 static void icount_adjust_vm(void * opaque
)
796 qemu_mod_timer(icount_vm_timer
,
797 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
801 static void init_icount_adjust(void)
803 /* Have both realtime and virtual time triggers for speed adjustment.
804 The realtime trigger catches emulated time passing too slowly,
805 the virtual time trigger catches emulated time passing too fast.
806 Realtime triggers occur even when idle, so use them less frequently
808 icount_rt_timer
= qemu_new_timer(rt_clock
, icount_adjust_rt
, NULL
);
809 qemu_mod_timer(icount_rt_timer
,
810 qemu_get_clock(rt_clock
) + 1000);
811 icount_vm_timer
= qemu_new_timer(vm_clock
, icount_adjust_vm
, NULL
);
812 qemu_mod_timer(icount_vm_timer
,
813 qemu_get_clock(vm_clock
) + QEMU_TIMER_BASE
/ 10);
816 static struct qemu_alarm_timer alarm_timers
[] = {
819 {"dynticks", ALARM_FLAG_DYNTICKS
, dynticks_start_timer
,
820 dynticks_stop_timer
, dynticks_rearm_timer
, NULL
},
821 /* HPET - if available - is preferred */
822 {"hpet", 0, hpet_start_timer
, hpet_stop_timer
, NULL
, NULL
},
823 /* ...otherwise try RTC */
824 {"rtc", 0, rtc_start_timer
, rtc_stop_timer
, NULL
, NULL
},
826 {"unix", 0, unix_start_timer
, unix_stop_timer
, NULL
, NULL
},
828 {"dynticks", ALARM_FLAG_DYNTICKS
, win32_start_timer
,
829 win32_stop_timer
, win32_rearm_timer
, &alarm_win32_data
},
830 {"win32", 0, win32_start_timer
,
831 win32_stop_timer
, NULL
, &alarm_win32_data
},
836 static void show_available_alarms(void)
840 printf("Available alarm timers, in order of precedence:\n");
841 for (i
= 0; alarm_timers
[i
].name
; i
++)
842 printf("%s\n", alarm_timers
[i
].name
);
845 static void configure_alarms(char const *opt
)
849 int count
= ARRAY_SIZE(alarm_timers
) - 1;
852 struct qemu_alarm_timer tmp
;
854 if (!strcmp(opt
, "?")) {
855 show_available_alarms();
861 /* Reorder the array */
862 name
= strtok(arg
, ",");
864 for (i
= 0; i
< count
&& alarm_timers
[i
].name
; i
++) {
865 if (!strcmp(alarm_timers
[i
].name
, name
))
870 fprintf(stderr
, "Unknown clock %s\n", name
);
879 tmp
= alarm_timers
[i
];
880 alarm_timers
[i
] = alarm_timers
[cur
];
881 alarm_timers
[cur
] = tmp
;
885 name
= strtok(NULL
, ",");
891 /* Disable remaining timers */
892 for (i
= cur
; i
< count
; i
++)
893 alarm_timers
[i
].name
= NULL
;
895 show_available_alarms();
903 static QEMUTimer
*active_timers
[2];
905 static QEMUClock
*qemu_new_clock(int type
)
908 clock
= qemu_mallocz(sizeof(QEMUClock
));
913 QEMUTimer
*qemu_new_timer(QEMUClock
*clock
, QEMUTimerCB
*cb
, void *opaque
)
917 ts
= qemu_mallocz(sizeof(QEMUTimer
));
924 void qemu_free_timer(QEMUTimer
*ts
)
929 /* stop a timer, but do not dealloc it */
930 void qemu_del_timer(QEMUTimer
*ts
)
934 /* NOTE: this code must be signal safe because
935 qemu_timer_expired() can be called from a signal. */
936 pt
= &active_timers
[ts
->clock
->type
];
949 /* modify the current timer so that it will be fired when current_time
950 >= expire_time. The corresponding callback will be called. */
951 void qemu_mod_timer(QEMUTimer
*ts
, int64_t expire_time
)
957 /* add the timer in the sorted list */
958 /* NOTE: this code must be signal safe because
959 qemu_timer_expired() can be called from a signal. */
960 pt
= &active_timers
[ts
->clock
->type
];
965 if (t
->expire_time
> expire_time
)
969 ts
->expire_time
= expire_time
;
973 /* Rearm if necessary */
974 if (pt
== &active_timers
[ts
->clock
->type
]) {
975 if ((alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) == 0) {
976 qemu_rearm_alarm_timer(alarm_timer
);
978 /* Interrupt execution to force deadline recalculation. */
984 int qemu_timer_pending(QEMUTimer
*ts
)
987 for(t
= active_timers
[ts
->clock
->type
]; t
!= NULL
; t
= t
->next
) {
994 static inline int qemu_timer_expired(QEMUTimer
*timer_head
, int64_t current_time
)
998 return (timer_head
->expire_time
<= current_time
);
1001 static void qemu_run_timers(QEMUTimer
**ptimer_head
, int64_t current_time
)
1007 if (!ts
|| ts
->expire_time
> current_time
)
1009 /* remove timer from the list before calling the callback */
1010 *ptimer_head
= ts
->next
;
1013 /* run the callback (the timer list can be modified) */
1018 int64_t qemu_get_clock(QEMUClock
*clock
)
1020 switch(clock
->type
) {
1021 case QEMU_TIMER_REALTIME
:
1022 return get_clock() / 1000000;
1024 case QEMU_TIMER_VIRTUAL
:
1026 return cpu_get_icount();
1028 return cpu_get_clock();
1033 static void init_timers(void)
1036 ticks_per_sec
= QEMU_TIMER_BASE
;
1037 rt_clock
= qemu_new_clock(QEMU_TIMER_REALTIME
);
1038 vm_clock
= qemu_new_clock(QEMU_TIMER_VIRTUAL
);
1042 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1044 uint64_t expire_time
;
1046 if (qemu_timer_pending(ts
)) {
1047 expire_time
= ts
->expire_time
;
1051 qemu_put_be64(f
, expire_time
);
1054 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
1056 uint64_t expire_time
;
1058 expire_time
= qemu_get_be64(f
);
1059 if (expire_time
!= -1) {
1060 qemu_mod_timer(ts
, expire_time
);
1066 static void timer_save(QEMUFile
*f
, void *opaque
)
1068 if (cpu_ticks_enabled
) {
1069 hw_error("cannot save state if virtual timers are running");
1071 qemu_put_be64(f
, cpu_ticks_offset
);
1072 qemu_put_be64(f
, ticks_per_sec
);
1073 qemu_put_be64(f
, cpu_clock_offset
);
1076 static int timer_load(QEMUFile
*f
, void *opaque
, int version_id
)
1078 if (version_id
!= 1 && version_id
!= 2)
1080 if (cpu_ticks_enabled
) {
1083 cpu_ticks_offset
=qemu_get_be64(f
);
1084 ticks_per_sec
=qemu_get_be64(f
);
1085 if (version_id
== 2) {
1086 cpu_clock_offset
=qemu_get_be64(f
);
1091 static void qemu_event_increment(void);
1094 static void CALLBACK
host_alarm_handler(UINT uTimerID
, UINT uMsg
,
1095 DWORD_PTR dwUser
, DWORD_PTR dw1
,
1098 static void host_alarm_handler(int host_signum
)
1102 #define DISP_FREQ 1000
1104 static int64_t delta_min
= INT64_MAX
;
1105 static int64_t delta_max
, delta_cum
, last_clock
, delta
, ti
;
1107 ti
= qemu_get_clock(vm_clock
);
1108 if (last_clock
!= 0) {
1109 delta
= ti
- last_clock
;
1110 if (delta
< delta_min
)
1112 if (delta
> delta_max
)
1115 if (++count
== DISP_FREQ
) {
1116 printf("timer: min=%" PRId64
" us max=%" PRId64
" us avg=%" PRId64
" us avg_freq=%0.3f Hz\n",
1117 muldiv64(delta_min
, 1000000, ticks_per_sec
),
1118 muldiv64(delta_max
, 1000000, ticks_per_sec
),
1119 muldiv64(delta_cum
, 1000000 / DISP_FREQ
, ticks_per_sec
),
1120 (double)ticks_per_sec
/ ((double)delta_cum
/ DISP_FREQ
));
1122 delta_min
= INT64_MAX
;
1130 if (alarm_has_dynticks(alarm_timer
) ||
1132 qemu_timer_expired(active_timers
[QEMU_TIMER_VIRTUAL
],
1133 qemu_get_clock(vm_clock
))) ||
1134 qemu_timer_expired(active_timers
[QEMU_TIMER_REALTIME
],
1135 qemu_get_clock(rt_clock
))) {
1136 qemu_event_increment();
1137 if (alarm_timer
) alarm_timer
->flags
|= ALARM_FLAG_EXPIRED
;
1139 #ifndef CONFIG_IOTHREAD
1141 /* stop the currently executing cpu because a timer occured */
1144 if (next_cpu
->kqemu_enabled
) {
1145 kqemu_cpu_interrupt(next_cpu
);
1150 timer_alarm_pending
= 1;
1151 qemu_notify_event();
1155 static int64_t qemu_next_deadline(void)
1159 if (active_timers
[QEMU_TIMER_VIRTUAL
]) {
1160 delta
= active_timers
[QEMU_TIMER_VIRTUAL
]->expire_time
-
1161 qemu_get_clock(vm_clock
);
1163 /* To avoid problems with overflow limit this to 2^32. */
1173 #if defined(__linux__) || defined(_WIN32)
1174 static uint64_t qemu_next_deadline_dyntick(void)
1182 delta
= (qemu_next_deadline() + 999) / 1000;
1184 if (active_timers
[QEMU_TIMER_REALTIME
]) {
1185 rtdelta
= (active_timers
[QEMU_TIMER_REALTIME
]->expire_time
-
1186 qemu_get_clock(rt_clock
))*1000;
1187 if (rtdelta
< delta
)
1191 if (delta
< MIN_TIMER_REARM_US
)
1192 delta
= MIN_TIMER_REARM_US
;
1200 /* Sets a specific flag */
1201 static int fcntl_setfl(int fd
, int flag
)
1205 flags
= fcntl(fd
, F_GETFL
);
1209 if (fcntl(fd
, F_SETFL
, flags
| flag
) == -1)
1215 #if defined(__linux__)
1217 #define RTC_FREQ 1024
1219 static void enable_sigio_timer(int fd
)
1221 struct sigaction act
;
1224 sigfillset(&act
.sa_mask
);
1226 act
.sa_handler
= host_alarm_handler
;
1228 sigaction(SIGIO
, &act
, NULL
);
1229 fcntl_setfl(fd
, O_ASYNC
);
1230 fcntl(fd
, F_SETOWN
, getpid());
1233 static int hpet_start_timer(struct qemu_alarm_timer
*t
)
1235 struct hpet_info info
;
1238 fd
= open("/dev/hpet", O_RDONLY
);
1243 r
= ioctl(fd
, HPET_IRQFREQ
, RTC_FREQ
);
1245 fprintf(stderr
, "Could not configure '/dev/hpet' to have a 1024Hz timer. This is not a fatal\n"
1246 "error, but for better emulation accuracy type:\n"
1247 "'echo 1024 > /proc/sys/dev/hpet/max-user-freq' as root.\n");
1251 /* Check capabilities */
1252 r
= ioctl(fd
, HPET_INFO
, &info
);
1256 /* Enable periodic mode */
1257 r
= ioctl(fd
, HPET_EPI
, 0);
1258 if (info
.hi_flags
&& (r
< 0))
1261 /* Enable interrupt */
1262 r
= ioctl(fd
, HPET_IE_ON
, 0);
1266 enable_sigio_timer(fd
);
1267 t
->priv
= (void *)(long)fd
;
1275 static void hpet_stop_timer(struct qemu_alarm_timer
*t
)
1277 int fd
= (long)t
->priv
;
1282 static int rtc_start_timer(struct qemu_alarm_timer
*t
)
1285 unsigned long current_rtc_freq
= 0;
1287 TFR(rtc_fd
= open("/dev/rtc", O_RDONLY
));
1290 ioctl(rtc_fd
, RTC_IRQP_READ
, ¤t_rtc_freq
);
1291 if (current_rtc_freq
!= RTC_FREQ
&&
1292 ioctl(rtc_fd
, RTC_IRQP_SET
, RTC_FREQ
) < 0) {
1293 fprintf(stderr
, "Could not configure '/dev/rtc' to have a 1024 Hz timer. This is not a fatal\n"
1294 "error, but for better emulation accuracy either use a 2.6 host Linux kernel or\n"
1295 "type 'echo 1024 > /proc/sys/dev/rtc/max-user-freq' as root.\n");
1298 if (ioctl(rtc_fd
, RTC_PIE_ON
, 0) < 0) {
1304 enable_sigio_timer(rtc_fd
);
1306 t
->priv
= (void *)(long)rtc_fd
;
1311 static void rtc_stop_timer(struct qemu_alarm_timer
*t
)
1313 int rtc_fd
= (long)t
->priv
;
1318 static int dynticks_start_timer(struct qemu_alarm_timer
*t
)
1322 struct sigaction act
;
1324 sigfillset(&act
.sa_mask
);
1326 act
.sa_handler
= host_alarm_handler
;
1328 sigaction(SIGALRM
, &act
, NULL
);
1331 * Initialize ev struct to 0 to avoid valgrind complaining
1332 * about uninitialized data in timer_create call
1334 memset(&ev
, 0, sizeof(ev
));
1335 ev
.sigev_value
.sival_int
= 0;
1336 ev
.sigev_notify
= SIGEV_SIGNAL
;
1337 ev
.sigev_signo
= SIGALRM
;
1339 if (timer_create(CLOCK_REALTIME
, &ev
, &host_timer
)) {
1340 perror("timer_create");
1342 /* disable dynticks */
1343 fprintf(stderr
, "Dynamic Ticks disabled\n");
1348 t
->priv
= (void *)(long)host_timer
;
1353 static void dynticks_stop_timer(struct qemu_alarm_timer
*t
)
1355 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1357 timer_delete(host_timer
);
1360 static void dynticks_rearm_timer(struct qemu_alarm_timer
*t
)
1362 timer_t host_timer
= (timer_t
)(long)t
->priv
;
1363 struct itimerspec timeout
;
1364 int64_t nearest_delta_us
= INT64_MAX
;
1367 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1368 !active_timers
[QEMU_TIMER_VIRTUAL
])
1371 nearest_delta_us
= qemu_next_deadline_dyntick();
1373 /* check whether a timer is already running */
1374 if (timer_gettime(host_timer
, &timeout
)) {
1376 fprintf(stderr
, "Internal timer error: aborting\n");
1379 current_us
= timeout
.it_value
.tv_sec
* 1000000 + timeout
.it_value
.tv_nsec
/1000;
1380 if (current_us
&& current_us
<= nearest_delta_us
)
1383 timeout
.it_interval
.tv_sec
= 0;
1384 timeout
.it_interval
.tv_nsec
= 0; /* 0 for one-shot timer */
1385 timeout
.it_value
.tv_sec
= nearest_delta_us
/ 1000000;
1386 timeout
.it_value
.tv_nsec
= (nearest_delta_us
% 1000000) * 1000;
1387 if (timer_settime(host_timer
, 0 /* RELATIVE */, &timeout
, NULL
)) {
1389 fprintf(stderr
, "Internal timer error: aborting\n");
1394 #endif /* defined(__linux__) */
1396 static int unix_start_timer(struct qemu_alarm_timer
*t
)
1398 struct sigaction act
;
1399 struct itimerval itv
;
1403 sigfillset(&act
.sa_mask
);
1405 act
.sa_handler
= host_alarm_handler
;
1407 sigaction(SIGALRM
, &act
, NULL
);
1409 itv
.it_interval
.tv_sec
= 0;
1410 /* for i386 kernel 2.6 to get 1 ms */
1411 itv
.it_interval
.tv_usec
= 999;
1412 itv
.it_value
.tv_sec
= 0;
1413 itv
.it_value
.tv_usec
= 10 * 1000;
1415 err
= setitimer(ITIMER_REAL
, &itv
, NULL
);
1422 static void unix_stop_timer(struct qemu_alarm_timer
*t
)
1424 struct itimerval itv
;
1426 memset(&itv
, 0, sizeof(itv
));
1427 setitimer(ITIMER_REAL
, &itv
, NULL
);
1430 #endif /* !defined(_WIN32) */
1435 static int win32_start_timer(struct qemu_alarm_timer
*t
)
1438 struct qemu_alarm_win32
*data
= t
->priv
;
1441 memset(&tc
, 0, sizeof(tc
));
1442 timeGetDevCaps(&tc
, sizeof(tc
));
1444 if (data
->period
< tc
.wPeriodMin
)
1445 data
->period
= tc
.wPeriodMin
;
1447 timeBeginPeriod(data
->period
);
1449 flags
= TIME_CALLBACK_FUNCTION
;
1450 if (alarm_has_dynticks(t
))
1451 flags
|= TIME_ONESHOT
;
1453 flags
|= TIME_PERIODIC
;
1455 data
->timerId
= timeSetEvent(1, // interval (ms)
1456 data
->period
, // resolution
1457 host_alarm_handler
, // function
1458 (DWORD
)t
, // parameter
1461 if (!data
->timerId
) {
1462 perror("Failed to initialize win32 alarm timer");
1463 timeEndPeriod(data
->period
);
1470 static void win32_stop_timer(struct qemu_alarm_timer
*t
)
1472 struct qemu_alarm_win32
*data
= t
->priv
;
1474 timeKillEvent(data
->timerId
);
1475 timeEndPeriod(data
->period
);
1478 static void win32_rearm_timer(struct qemu_alarm_timer
*t
)
1480 struct qemu_alarm_win32
*data
= t
->priv
;
1481 uint64_t nearest_delta_us
;
1483 if (!active_timers
[QEMU_TIMER_REALTIME
] &&
1484 !active_timers
[QEMU_TIMER_VIRTUAL
])
1487 nearest_delta_us
= qemu_next_deadline_dyntick();
1488 nearest_delta_us
/= 1000;
1490 timeKillEvent(data
->timerId
);
1492 data
->timerId
= timeSetEvent(1,
1496 TIME_ONESHOT
| TIME_PERIODIC
);
1498 if (!data
->timerId
) {
1499 perror("Failed to re-arm win32 alarm timer");
1501 timeEndPeriod(data
->period
);
1508 static int init_timer_alarm(void)
1510 struct qemu_alarm_timer
*t
= NULL
;
1513 for (i
= 0; alarm_timers
[i
].name
; i
++) {
1514 t
= &alarm_timers
[i
];
1534 static void quit_timers(void)
1536 alarm_timer
->stop(alarm_timer
);
1540 /***********************************************************/
1541 /* host time/date access */
1542 void qemu_get_timedate(struct tm
*tm
, int offset
)
1549 if (rtc_date_offset
== -1) {
1553 ret
= localtime(&ti
);
1555 ti
-= rtc_date_offset
;
1559 memcpy(tm
, ret
, sizeof(struct tm
));
1562 int qemu_timedate_diff(struct tm
*tm
)
1566 if (rtc_date_offset
== -1)
1568 seconds
= mktimegm(tm
);
1570 seconds
= mktime(tm
);
1572 seconds
= mktimegm(tm
) + rtc_date_offset
;
1574 return seconds
- time(NULL
);
1578 static void socket_cleanup(void)
1583 static int socket_init(void)
1588 ret
= WSAStartup(MAKEWORD(2,2), &Data
);
1590 err
= WSAGetLastError();
1591 fprintf(stderr
, "WSAStartup: %d\n", err
);
1594 atexit(socket_cleanup
);
1599 /***********************************************************/
1600 /* Bluetooth support */
1603 static struct HCIInfo
*hci_table
[MAX_NICS
];
1605 static struct bt_vlan_s
{
1606 struct bt_scatternet_s net
;
1608 struct bt_vlan_s
*next
;
1611 /* find or alloc a new bluetooth "VLAN" */
1612 static struct bt_scatternet_s
*qemu_find_bt_vlan(int id
)
1614 struct bt_vlan_s
**pvlan
, *vlan
;
1615 for (vlan
= first_bt_vlan
; vlan
!= NULL
; vlan
= vlan
->next
) {
1619 vlan
= qemu_mallocz(sizeof(struct bt_vlan_s
));
1621 pvlan
= &first_bt_vlan
;
1622 while (*pvlan
!= NULL
)
1623 pvlan
= &(*pvlan
)->next
;
1628 static void null_hci_send(struct HCIInfo
*hci
, const uint8_t *data
, int len
)
1632 static int null_hci_addr_set(struct HCIInfo
*hci
, const uint8_t *bd_addr
)
1637 static struct HCIInfo null_hci
= {
1638 .cmd_send
= null_hci_send
,
1639 .sco_send
= null_hci_send
,
1640 .acl_send
= null_hci_send
,
1641 .bdaddr_set
= null_hci_addr_set
,
1644 struct HCIInfo
*qemu_next_hci(void)
1646 if (cur_hci
== nb_hcis
)
1649 return hci_table
[cur_hci
++];
1652 static struct HCIInfo
*hci_init(const char *str
)
1655 struct bt_scatternet_s
*vlan
= 0;
1657 if (!strcmp(str
, "null"))
1660 else if (!strncmp(str
, "host", 4) && (str
[4] == '\0' || str
[4] == ':'))
1662 return bt_host_hci(str
[4] ? str
+ 5 : "hci0");
1663 else if (!strncmp(str
, "hci", 3)) {
1666 if (!strncmp(str
+ 3, ",vlan=", 6)) {
1667 vlan
= qemu_find_bt_vlan(strtol(str
+ 9, &endp
, 0));
1672 vlan
= qemu_find_bt_vlan(0);
1674 return bt_new_hci(vlan
);
1677 fprintf(stderr
, "qemu: Unknown bluetooth HCI `%s'.\n", str
);
1682 static int bt_hci_parse(const char *str
)
1684 struct HCIInfo
*hci
;
1687 if (nb_hcis
>= MAX_NICS
) {
1688 fprintf(stderr
, "qemu: Too many bluetooth HCIs (max %i).\n", MAX_NICS
);
1692 hci
= hci_init(str
);
1701 bdaddr
.b
[5] = 0x56 + nb_hcis
;
1702 hci
->bdaddr_set(hci
, bdaddr
.b
);
1704 hci_table
[nb_hcis
++] = hci
;
1709 static void bt_vhci_add(int vlan_id
)
1711 struct bt_scatternet_s
*vlan
= qemu_find_bt_vlan(vlan_id
);
1714 fprintf(stderr
, "qemu: warning: adding a VHCI to "
1715 "an empty scatternet %i\n", vlan_id
);
1717 bt_vhci_init(bt_new_hci(vlan
));
1720 static struct bt_device_s
*bt_device_add(const char *opt
)
1722 struct bt_scatternet_s
*vlan
;
1724 char *endp
= strstr(opt
, ",vlan=");
1725 int len
= (endp
? endp
- opt
: strlen(opt
)) + 1;
1728 pstrcpy(devname
, MIN(sizeof(devname
), len
), opt
);
1731 vlan_id
= strtol(endp
+ 6, &endp
, 0);
1733 fprintf(stderr
, "qemu: unrecognised bluetooth vlan Id\n");
1738 vlan
= qemu_find_bt_vlan(vlan_id
);
1741 fprintf(stderr
, "qemu: warning: adding a slave device to "
1742 "an empty scatternet %i\n", vlan_id
);
1744 if (!strcmp(devname
, "keyboard"))
1745 return bt_keyboard_init(vlan
);
1747 fprintf(stderr
, "qemu: unsupported bluetooth device `%s'\n", devname
);
1751 static int bt_parse(const char *opt
)
1753 const char *endp
, *p
;
1756 if (strstart(opt
, "hci", &endp
)) {
1757 if (!*endp
|| *endp
== ',') {
1759 if (!strstart(endp
, ",vlan=", 0))
1762 return bt_hci_parse(opt
);
1764 } else if (strstart(opt
, "vhci", &endp
)) {
1765 if (!*endp
|| *endp
== ',') {
1767 if (strstart(endp
, ",vlan=", &p
)) {
1768 vlan
= strtol(p
, (char **) &endp
, 0);
1770 fprintf(stderr
, "qemu: bad scatternet '%s'\n", p
);
1774 fprintf(stderr
, "qemu: bad parameter '%s'\n", endp
+ 1);
1783 } else if (strstart(opt
, "device:", &endp
))
1784 return !bt_device_add(endp
);
1786 fprintf(stderr
, "qemu: bad bluetooth parameter '%s'\n", opt
);
1790 /***********************************************************/
1791 /* QEMU Block devices */
1793 #define HD_ALIAS "index=%d,media=disk"
1794 #define CDROM_ALIAS "index=2,media=cdrom"
1795 #define FD_ALIAS "index=%d,if=floppy"
1796 #define PFLASH_ALIAS "if=pflash"
1797 #define MTD_ALIAS "if=mtd"
1798 #define SD_ALIAS "index=0,if=sd"
1800 static QemuOptsList drive_opt_list
= {
1802 .head
= TAILQ_HEAD_INITIALIZER(drive_opt_list
.head
),
1806 .type
= QEMU_OPT_NUMBER
,
1807 .help
= "bus number",
1810 .type
= QEMU_OPT_NUMBER
,
1811 .help
= "unit number (i.e. lun for scsi)",
1814 .type
= QEMU_OPT_STRING
,
1815 .help
= "interface (ide, scsi, sd, mtd, floppy, pflash, virtio)",
1818 .type
= QEMU_OPT_NUMBER
,
1821 .type
= QEMU_OPT_NUMBER
,
1822 .help
= "number of cylinders (ide disk geometry)",
1825 .type
= QEMU_OPT_NUMBER
,
1826 .help
= "number of heads (ide disk geometry)",
1829 .type
= QEMU_OPT_NUMBER
,
1830 .help
= "number of sectors (ide disk geometry)",
1833 .type
= QEMU_OPT_STRING
,
1834 .help
= "chs translation (auto, lba. none)",
1837 .type
= QEMU_OPT_STRING
,
1838 .help
= "media type (disk, cdrom)",
1841 .type
= QEMU_OPT_BOOL
,
1844 .type
= QEMU_OPT_STRING
,
1845 .help
= "disk image",
1848 .type
= QEMU_OPT_STRING
,
1849 .help
= "host cache usage (none, writeback, writethrough)",
1852 .type
= QEMU_OPT_STRING
,
1853 .help
= "disk format (raw, qcow2, ...)",
1856 .type
= QEMU_OPT_STRING
,
1859 .type
= QEMU_OPT_STRING
,
1862 .type
= QEMU_OPT_STRING
,
1863 .help
= "pci address (virtio only)",
1865 { /* end if list */ }
1869 QemuOpts
*drive_add(const char *file
, const char *fmt
, ...)
1876 vsnprintf(optstr
, sizeof(optstr
), fmt
, ap
);
1879 opts
= qemu_opts_parse(&drive_opt_list
, optstr
, NULL
);
1881 fprintf(stderr
, "%s: huh? duplicate? (%s)\n",
1882 __FUNCTION__
, optstr
);
1886 qemu_opt_set(opts
, "file", file
);
1890 DriveInfo
*drive_get(BlockInterfaceType type
, int bus
, int unit
)
1894 /* seek interface, bus and unit */
1896 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1897 if (dinfo
->type
== type
&&
1898 dinfo
->bus
== bus
&&
1899 dinfo
->unit
== unit
)
1906 DriveInfo
*drive_get_by_id(char *id
)
1910 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1911 if (strcmp(id
, dinfo
->id
))
1918 int drive_get_max_bus(BlockInterfaceType type
)
1924 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1925 if(dinfo
->type
== type
&&
1926 dinfo
->bus
> max_bus
)
1927 max_bus
= dinfo
->bus
;
1932 const char *drive_get_serial(BlockDriverState
*bdrv
)
1936 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1937 if (dinfo
->bdrv
== bdrv
)
1938 return dinfo
->serial
;
1944 BlockInterfaceErrorAction
drive_get_onerror(BlockDriverState
*bdrv
)
1948 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1949 if (dinfo
->bdrv
== bdrv
)
1950 return dinfo
->onerror
;
1953 return BLOCK_ERR_STOP_ENOSPC
;
1956 static void bdrv_format_print(void *opaque
, const char *name
)
1958 fprintf(stderr
, " %s", name
);
1961 void drive_uninit(BlockDriverState
*bdrv
)
1965 TAILQ_FOREACH(dinfo
, &drives
, next
) {
1966 if (dinfo
->bdrv
!= bdrv
)
1968 qemu_opts_del(dinfo
->opts
);
1969 TAILQ_REMOVE(&drives
, dinfo
, next
);
1975 DriveInfo
*drive_init(QemuOpts
*opts
, void *opaque
,
1979 const char *file
= NULL
;
1982 const char *mediastr
= "";
1983 BlockInterfaceType type
;
1984 enum { MEDIA_DISK
, MEDIA_CDROM
} media
;
1985 int bus_id
, unit_id
;
1986 int cyls
, heads
, secs
, translation
;
1987 BlockDriver
*drv
= NULL
;
1988 QEMUMachine
*machine
= opaque
;
1992 int bdrv_flags
, onerror
;
1993 const char *devaddr
;
1999 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2002 if (machine
->use_scsi
) {
2004 max_devs
= MAX_SCSI_DEVS
;
2005 pstrcpy(devname
, sizeof(devname
), "scsi");
2008 max_devs
= MAX_IDE_DEVS
;
2009 pstrcpy(devname
, sizeof(devname
), "ide");
2013 /* extract parameters */
2014 bus_id
= qemu_opt_get_number(opts
, "bus", 0);
2015 unit_id
= qemu_opt_get_number(opts
, "unit", -1);
2016 index
= qemu_opt_get_number(opts
, "index", -1);
2018 cyls
= qemu_opt_get_number(opts
, "cyls", 0);
2019 heads
= qemu_opt_get_number(opts
, "heads", 0);
2020 secs
= qemu_opt_get_number(opts
, "secs", 0);
2022 snapshot
= qemu_opt_get_bool(opts
, "snapshot", 0);
2024 file
= qemu_opt_get(opts
, "file");
2025 serial
= qemu_opt_get(opts
, "serial");
2027 if ((buf
= qemu_opt_get(opts
, "if")) != NULL
) {
2028 pstrcpy(devname
, sizeof(devname
), buf
);
2029 if (!strcmp(buf
, "ide")) {
2031 max_devs
= MAX_IDE_DEVS
;
2032 } else if (!strcmp(buf
, "scsi")) {
2034 max_devs
= MAX_SCSI_DEVS
;
2035 } else if (!strcmp(buf
, "floppy")) {
2038 } else if (!strcmp(buf
, "pflash")) {
2041 } else if (!strcmp(buf
, "mtd")) {
2044 } else if (!strcmp(buf
, "sd")) {
2047 } else if (!strcmp(buf
, "virtio")) {
2050 } else if (!strcmp(buf
, "xen")) {
2054 fprintf(stderr
, "qemu: unsupported bus type '%s'\n", buf
);
2059 if (cyls
|| heads
|| secs
) {
2060 if (cyls
< 1 || cyls
> 16383) {
2061 fprintf(stderr
, "qemu: '%s' invalid physical cyls number\n", buf
);
2064 if (heads
< 1 || heads
> 16) {
2065 fprintf(stderr
, "qemu: '%s' invalid physical heads number\n", buf
);
2068 if (secs
< 1 || secs
> 63) {
2069 fprintf(stderr
, "qemu: '%s' invalid physical secs number\n", buf
);
2074 if ((buf
= qemu_opt_get(opts
, "trans")) != NULL
) {
2077 "qemu: '%s' trans must be used with cyls,heads and secs\n",
2081 if (!strcmp(buf
, "none"))
2082 translation
= BIOS_ATA_TRANSLATION_NONE
;
2083 else if (!strcmp(buf
, "lba"))
2084 translation
= BIOS_ATA_TRANSLATION_LBA
;
2085 else if (!strcmp(buf
, "auto"))
2086 translation
= BIOS_ATA_TRANSLATION_AUTO
;
2088 fprintf(stderr
, "qemu: '%s' invalid translation type\n", buf
);
2093 if ((buf
= qemu_opt_get(opts
, "media")) != NULL
) {
2094 if (!strcmp(buf
, "disk")) {
2096 } else if (!strcmp(buf
, "cdrom")) {
2097 if (cyls
|| secs
|| heads
) {
2099 "qemu: '%s' invalid physical CHS format\n", buf
);
2102 media
= MEDIA_CDROM
;
2104 fprintf(stderr
, "qemu: '%s' invalid media\n", buf
);
2109 if ((buf
= qemu_opt_get(opts
, "cache")) != NULL
) {
2110 if (!strcmp(buf
, "off") || !strcmp(buf
, "none"))
2112 else if (!strcmp(buf
, "writethrough"))
2114 else if (!strcmp(buf
, "writeback"))
2117 fprintf(stderr
, "qemu: invalid cache option\n");
2122 if ((buf
= qemu_opt_get(opts
, "format")) != NULL
) {
2123 if (strcmp(buf
, "?") == 0) {
2124 fprintf(stderr
, "qemu: Supported formats:");
2125 bdrv_iterate_format(bdrv_format_print
, NULL
);
2126 fprintf(stderr
, "\n");
2129 drv
= bdrv_find_format(buf
);
2131 fprintf(stderr
, "qemu: '%s' invalid format\n", buf
);
2136 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2137 if ((buf
= qemu_opt_get(opts
, "werror")) != NULL
) {
2138 if (type
!= IF_IDE
&& type
!= IF_SCSI
&& type
!= IF_VIRTIO
) {
2139 fprintf(stderr
, "werror is no supported by this format\n");
2142 if (!strcmp(buf
, "ignore"))
2143 onerror
= BLOCK_ERR_IGNORE
;
2144 else if (!strcmp(buf
, "enospc"))
2145 onerror
= BLOCK_ERR_STOP_ENOSPC
;
2146 else if (!strcmp(buf
, "stop"))
2147 onerror
= BLOCK_ERR_STOP_ANY
;
2148 else if (!strcmp(buf
, "report"))
2149 onerror
= BLOCK_ERR_REPORT
;
2151 fprintf(stderr
, "qemu: '%s' invalid write error action\n", buf
);
2156 if ((devaddr
= qemu_opt_get(opts
, "addr")) != NULL
) {
2157 if (type
!= IF_VIRTIO
) {
2158 fprintf(stderr
, "addr is not supported\n");
2163 /* compute bus and unit according index */
2166 if (bus_id
!= 0 || unit_id
!= -1) {
2168 "qemu: index cannot be used with bus and unit\n");
2176 unit_id
= index
% max_devs
;
2177 bus_id
= index
/ max_devs
;
2181 /* if user doesn't specify a unit_id,
2182 * try to find the first free
2185 if (unit_id
== -1) {
2187 while (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2189 if (max_devs
&& unit_id
>= max_devs
) {
2190 unit_id
-= max_devs
;
2198 if (max_devs
&& unit_id
>= max_devs
) {
2199 fprintf(stderr
, "qemu: unit %d too big (max is %d)\n",
2200 unit_id
, max_devs
- 1);
2205 * ignore multiple definitions
2208 if (drive_get(type
, bus_id
, unit_id
) != NULL
) {
2215 dinfo
= qemu_mallocz(sizeof(*dinfo
));
2216 if ((buf
= qemu_opt_get(opts
, "id")) != NULL
) {
2217 dinfo
->id
= qemu_strdup(buf
);
2219 /* no id supplied -> create one */
2220 dinfo
->id
= qemu_mallocz(32);
2221 if (type
== IF_IDE
|| type
== IF_SCSI
)
2222 mediastr
= (media
== MEDIA_CDROM
) ? "-cd" : "-hd";
2224 snprintf(dinfo
->id
, 32, "%s%i%s%i",
2225 devname
, bus_id
, mediastr
, unit_id
);
2227 snprintf(dinfo
->id
, 32, "%s%s%i",
2228 devname
, mediastr
, unit_id
);
2230 dinfo
->bdrv
= bdrv_new(dinfo
->id
);
2231 dinfo
->devaddr
= devaddr
;
2233 dinfo
->bus
= bus_id
;
2234 dinfo
->unit
= unit_id
;
2235 dinfo
->onerror
= onerror
;
2238 strncpy(dinfo
->serial
, serial
, sizeof(serial
));
2239 TAILQ_INSERT_TAIL(&drives
, dinfo
, next
);
2248 bdrv_set_geometry_hint(dinfo
->bdrv
, cyls
, heads
, secs
);
2249 bdrv_set_translation_hint(dinfo
->bdrv
, translation
);
2253 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_CDROM
);
2258 /* FIXME: This isn't really a floppy, but it's a reasonable
2261 bdrv_set_type_hint(dinfo
->bdrv
, BDRV_TYPE_FLOPPY
);
2276 bdrv_flags
|= BDRV_O_SNAPSHOT
;
2277 cache
= 2; /* always use write-back with snapshot */
2279 if (cache
== 0) /* no caching */
2280 bdrv_flags
|= BDRV_O_NOCACHE
;
2281 else if (cache
== 2) /* write-back */
2282 bdrv_flags
|= BDRV_O_CACHE_WB
;
2283 if (bdrv_open2(dinfo
->bdrv
, file
, bdrv_flags
, drv
) < 0) {
2284 fprintf(stderr
, "qemu: could not open disk image %s\n",
2288 if (bdrv_key_required(dinfo
->bdrv
))
2294 static int drive_init_func(QemuOpts
*opts
, void *opaque
)
2296 QEMUMachine
*machine
= opaque
;
2297 int fatal_error
= 0;
2299 if (drive_init(opts
, machine
, &fatal_error
) == NULL
) {
2306 static int drive_enable_snapshot(QemuOpts
*opts
, void *opaque
)
2308 if (NULL
== qemu_opt_get(opts
, "snapshot")) {
2309 qemu_opt_set(opts
, "snapshot", "on");
2314 void qemu_register_boot_set(QEMUBootSetHandler
*func
, void *opaque
)
2316 boot_set_handler
= func
;
2317 boot_set_opaque
= opaque
;
2320 int qemu_boot_set(const char *boot_devices
)
2322 if (!boot_set_handler
) {
2325 return boot_set_handler(boot_set_opaque
, boot_devices
);
2328 static int parse_bootdevices(char *devices
)
2330 /* We just do some generic consistency checks */
2334 for (p
= devices
; *p
!= '\0'; p
++) {
2335 /* Allowed boot devices are:
2336 * a-b: floppy disk drives
2337 * c-f: IDE disk drives
2338 * g-m: machine implementation dependant drives
2339 * n-p: network devices
2340 * It's up to each machine implementation to check if the given boot
2341 * devices match the actual hardware implementation and firmware
2344 if (*p
< 'a' || *p
> 'p') {
2345 fprintf(stderr
, "Invalid boot device '%c'\n", *p
);
2348 if (bitmap
& (1 << (*p
- 'a'))) {
2349 fprintf(stderr
, "Boot device '%c' was given twice\n", *p
);
2352 bitmap
|= 1 << (*p
- 'a');
2357 static void restore_boot_devices(void *opaque
)
2359 char *standard_boot_devices
= opaque
;
2361 qemu_boot_set(standard_boot_devices
);
2363 qemu_unregister_reset(restore_boot_devices
, standard_boot_devices
);
2364 qemu_free(standard_boot_devices
);
2367 static void numa_add(const char *optarg
)
2371 unsigned long long value
, endvalue
;
2374 optarg
= get_opt_name(option
, 128, optarg
, ',') + 1;
2375 if (!strcmp(option
, "node")) {
2376 if (get_param_value(option
, 128, "nodeid", optarg
) == 0) {
2377 nodenr
= nb_numa_nodes
;
2379 nodenr
= strtoull(option
, NULL
, 10);
2382 if (get_param_value(option
, 128, "mem", optarg
) == 0) {
2383 node_mem
[nodenr
] = 0;
2385 value
= strtoull(option
, &endptr
, 0);
2387 case 0: case 'M': case 'm':
2394 node_mem
[nodenr
] = value
;
2396 if (get_param_value(option
, 128, "cpus", optarg
) == 0) {
2397 node_cpumask
[nodenr
] = 0;
2399 value
= strtoull(option
, &endptr
, 10);
2402 fprintf(stderr
, "only 64 CPUs in NUMA mode supported.\n");
2404 if (*endptr
== '-') {
2405 endvalue
= strtoull(endptr
+1, &endptr
, 10);
2406 if (endvalue
>= 63) {
2409 "only 63 CPUs in NUMA mode supported.\n");
2411 value
= (1 << (endvalue
+ 1)) - (1 << value
);
2416 node_cpumask
[nodenr
] = value
;
2423 /***********************************************************/
2426 static USBPort
*used_usb_ports
;
2427 static USBPort
*free_usb_ports
;
2429 /* ??? Maybe change this to register a hub to keep track of the topology. */
2430 void qemu_register_usb_port(USBPort
*port
, void *opaque
, int index
,
2431 usb_attachfn attach
)
2433 port
->opaque
= opaque
;
2434 port
->index
= index
;
2435 port
->attach
= attach
;
2436 port
->next
= free_usb_ports
;
2437 free_usb_ports
= port
;
2440 int usb_device_add_dev(USBDevice
*dev
)
2444 /* Find a USB port to add the device to. */
2445 port
= free_usb_ports
;
2449 /* Create a new hub and chain it on. */
2450 free_usb_ports
= NULL
;
2451 port
->next
= used_usb_ports
;
2452 used_usb_ports
= port
;
2454 hub
= usb_hub_init(VM_USB_HUB_SIZE
);
2455 usb_attach(port
, hub
);
2456 port
= free_usb_ports
;
2459 free_usb_ports
= port
->next
;
2460 port
->next
= used_usb_ports
;
2461 used_usb_ports
= port
;
2462 usb_attach(port
, dev
);
2466 static void usb_msd_password_cb(void *opaque
, int err
)
2468 USBDevice
*dev
= opaque
;
2471 usb_device_add_dev(dev
);
2473 dev
->handle_destroy(dev
);
2476 static int usb_device_add(const char *devname
, int is_hotplug
)
2481 if (!free_usb_ports
)
2484 if (strstart(devname
, "host:", &p
)) {
2485 dev
= usb_host_device_open(p
);
2486 } else if (!strcmp(devname
, "mouse")) {
2487 dev
= usb_mouse_init();
2488 } else if (!strcmp(devname
, "tablet")) {
2489 dev
= usb_tablet_init();
2490 } else if (!strcmp(devname
, "keyboard")) {
2491 dev
= usb_keyboard_init();
2492 } else if (strstart(devname
, "disk:", &p
)) {
2493 BlockDriverState
*bs
;
2495 dev
= usb_msd_init(p
);
2498 bs
= usb_msd_get_bdrv(dev
);
2499 if (bdrv_key_required(bs
)) {
2502 monitor_read_bdrv_key_start(cur_mon
, bs
, usb_msd_password_cb
,
2507 } else if (!strcmp(devname
, "wacom-tablet")) {
2508 dev
= usb_wacom_init();
2509 } else if (strstart(devname
, "serial:", &p
)) {
2510 dev
= usb_serial_init(p
);
2511 #ifdef CONFIG_BRLAPI
2512 } else if (!strcmp(devname
, "braille")) {
2513 dev
= usb_baum_init();
2515 } else if (strstart(devname
, "net:", &p
)) {
2518 if (net_client_init(NULL
, "nic", p
) < 0)
2520 nd_table
[nic
].model
= "usb";
2521 dev
= usb_net_init(&nd_table
[nic
]);
2522 } else if (!strcmp(devname
, "bt") || strstart(devname
, "bt:", &p
)) {
2523 dev
= usb_bt_init(devname
[2] ? hci_init(p
) :
2524 bt_new_hci(qemu_find_bt_vlan(0)));
2531 return usb_device_add_dev(dev
);
2534 int usb_device_del_addr(int bus_num
, int addr
)
2540 if (!used_usb_ports
)
2546 lastp
= &used_usb_ports
;
2547 port
= used_usb_ports
;
2548 while (port
&& port
->dev
->addr
!= addr
) {
2549 lastp
= &port
->next
;
2557 *lastp
= port
->next
;
2558 usb_attach(port
, NULL
);
2559 dev
->handle_destroy(dev
);
2560 port
->next
= free_usb_ports
;
2561 free_usb_ports
= port
;
2565 static int usb_device_del(const char *devname
)
2570 if (strstart(devname
, "host:", &p
))
2571 return usb_host_device_close(p
);
2573 if (!used_usb_ports
)
2576 p
= strchr(devname
, '.');
2579 bus_num
= strtoul(devname
, NULL
, 0);
2580 addr
= strtoul(p
+ 1, NULL
, 0);
2582 return usb_device_del_addr(bus_num
, addr
);
2585 static int usb_parse(const char *cmdline
)
2587 return usb_device_add(cmdline
, 0);
2590 void do_usb_add(Monitor
*mon
, const char *devname
)
2592 usb_device_add(devname
, 1);
2595 void do_usb_del(Monitor
*mon
, const char *devname
)
2597 usb_device_del(devname
);
2600 void usb_info(Monitor
*mon
)
2604 const char *speed_str
;
2607 monitor_printf(mon
, "USB support not enabled\n");
2611 for (port
= used_usb_ports
; port
; port
= port
->next
) {
2615 switch(dev
->speed
) {
2619 case USB_SPEED_FULL
:
2622 case USB_SPEED_HIGH
:
2629 monitor_printf(mon
, " Device %d.%d, Speed %s Mb/s, Product %s\n",
2630 0, dev
->addr
, speed_str
, dev
->devname
);
2634 /***********************************************************/
2635 /* PCMCIA/Cardbus */
2637 static struct pcmcia_socket_entry_s
{
2638 PCMCIASocket
*socket
;
2639 struct pcmcia_socket_entry_s
*next
;
2640 } *pcmcia_sockets
= 0;
2642 void pcmcia_socket_register(PCMCIASocket
*socket
)
2644 struct pcmcia_socket_entry_s
*entry
;
2646 entry
= qemu_malloc(sizeof(struct pcmcia_socket_entry_s
));
2647 entry
->socket
= socket
;
2648 entry
->next
= pcmcia_sockets
;
2649 pcmcia_sockets
= entry
;
2652 void pcmcia_socket_unregister(PCMCIASocket
*socket
)
2654 struct pcmcia_socket_entry_s
*entry
, **ptr
;
2656 ptr
= &pcmcia_sockets
;
2657 for (entry
= *ptr
; entry
; ptr
= &entry
->next
, entry
= *ptr
)
2658 if (entry
->socket
== socket
) {
2664 void pcmcia_info(Monitor
*mon
)
2666 struct pcmcia_socket_entry_s
*iter
;
2668 if (!pcmcia_sockets
)
2669 monitor_printf(mon
, "No PCMCIA sockets\n");
2671 for (iter
= pcmcia_sockets
; iter
; iter
= iter
->next
)
2672 monitor_printf(mon
, "%s: %s\n", iter
->socket
->slot_string
,
2673 iter
->socket
->attached
? iter
->socket
->card_string
:
2677 /***********************************************************/
2678 /* register display */
2680 struct DisplayAllocator default_allocator
= {
2681 defaultallocator_create_displaysurface
,
2682 defaultallocator_resize_displaysurface
,
2683 defaultallocator_free_displaysurface
2686 void register_displaystate(DisplayState
*ds
)
2696 DisplayState
*get_displaystate(void)
2698 return display_state
;
2701 DisplayAllocator
*register_displayallocator(DisplayState
*ds
, DisplayAllocator
*da
)
2703 if(ds
->allocator
== &default_allocator
) ds
->allocator
= da
;
2704 return ds
->allocator
;
2709 static void dumb_display_init(void)
2711 DisplayState
*ds
= qemu_mallocz(sizeof(DisplayState
));
2712 ds
->allocator
= &default_allocator
;
2713 ds
->surface
= qemu_create_displaysurface(ds
, 640, 480);
2714 register_displaystate(ds
);
2717 /***********************************************************/
2720 typedef struct IOHandlerRecord
{
2722 IOCanRWHandler
*fd_read_poll
;
2724 IOHandler
*fd_write
;
2727 /* temporary data */
2729 struct IOHandlerRecord
*next
;
2732 static IOHandlerRecord
*first_io_handler
;
2734 /* XXX: fd_read_poll should be suppressed, but an API change is
2735 necessary in the character devices to suppress fd_can_read(). */
2736 int qemu_set_fd_handler2(int fd
,
2737 IOCanRWHandler
*fd_read_poll
,
2739 IOHandler
*fd_write
,
2742 IOHandlerRecord
**pioh
, *ioh
;
2744 if (!fd_read
&& !fd_write
) {
2745 pioh
= &first_io_handler
;
2750 if (ioh
->fd
== fd
) {
2757 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
2761 ioh
= qemu_mallocz(sizeof(IOHandlerRecord
));
2762 ioh
->next
= first_io_handler
;
2763 first_io_handler
= ioh
;
2766 ioh
->fd_read_poll
= fd_read_poll
;
2767 ioh
->fd_read
= fd_read
;
2768 ioh
->fd_write
= fd_write
;
2769 ioh
->opaque
= opaque
;
2775 int qemu_set_fd_handler(int fd
,
2777 IOHandler
*fd_write
,
2780 return qemu_set_fd_handler2(fd
, NULL
, fd_read
, fd_write
, opaque
);
2784 /***********************************************************/
2785 /* Polling handling */
2787 typedef struct PollingEntry
{
2790 struct PollingEntry
*next
;
2793 static PollingEntry
*first_polling_entry
;
2795 int qemu_add_polling_cb(PollingFunc
*func
, void *opaque
)
2797 PollingEntry
**ppe
, *pe
;
2798 pe
= qemu_mallocz(sizeof(PollingEntry
));
2800 pe
->opaque
= opaque
;
2801 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
);
2806 void qemu_del_polling_cb(PollingFunc
*func
, void *opaque
)
2808 PollingEntry
**ppe
, *pe
;
2809 for(ppe
= &first_polling_entry
; *ppe
!= NULL
; ppe
= &(*ppe
)->next
) {
2811 if (pe
->func
== func
&& pe
->opaque
== opaque
) {
2819 /***********************************************************/
2820 /* Wait objects support */
2821 typedef struct WaitObjects
{
2823 HANDLE events
[MAXIMUM_WAIT_OBJECTS
+ 1];
2824 WaitObjectFunc
*func
[MAXIMUM_WAIT_OBJECTS
+ 1];
2825 void *opaque
[MAXIMUM_WAIT_OBJECTS
+ 1];
2828 static WaitObjects wait_objects
= {0};
2830 int qemu_add_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2832 WaitObjects
*w
= &wait_objects
;
2834 if (w
->num
>= MAXIMUM_WAIT_OBJECTS
)
2836 w
->events
[w
->num
] = handle
;
2837 w
->func
[w
->num
] = func
;
2838 w
->opaque
[w
->num
] = opaque
;
2843 void qemu_del_wait_object(HANDLE handle
, WaitObjectFunc
*func
, void *opaque
)
2846 WaitObjects
*w
= &wait_objects
;
2849 for (i
= 0; i
< w
->num
; i
++) {
2850 if (w
->events
[i
] == handle
)
2853 w
->events
[i
] = w
->events
[i
+ 1];
2854 w
->func
[i
] = w
->func
[i
+ 1];
2855 w
->opaque
[i
] = w
->opaque
[i
+ 1];
2863 /***********************************************************/
2864 /* ram save/restore */
2866 static int ram_get_page(QEMUFile
*f
, uint8_t *buf
, int len
)
2870 v
= qemu_get_byte(f
);
2873 if (qemu_get_buffer(f
, buf
, len
) != len
)
2877 v
= qemu_get_byte(f
);
2878 memset(buf
, v
, len
);
2884 if (qemu_file_has_error(f
))
2890 static int ram_load_v1(QEMUFile
*f
, void *opaque
)
2895 if (qemu_get_be32(f
) != last_ram_offset
)
2897 for(i
= 0; i
< last_ram_offset
; i
+= TARGET_PAGE_SIZE
) {
2898 ret
= ram_get_page(f
, qemu_get_ram_ptr(i
), TARGET_PAGE_SIZE
);
2905 #define BDRV_HASH_BLOCK_SIZE 1024
2906 #define IOBUF_SIZE 4096
2907 #define RAM_CBLOCK_MAGIC 0xfabe
2909 typedef struct RamDecompressState
{
2912 uint8_t buf
[IOBUF_SIZE
];
2913 } RamDecompressState
;
2915 static int ram_decompress_open(RamDecompressState
*s
, QEMUFile
*f
)
2918 memset(s
, 0, sizeof(*s
));
2920 ret
= inflateInit(&s
->zstream
);
2926 static int ram_decompress_buf(RamDecompressState
*s
, uint8_t *buf
, int len
)
2930 s
->zstream
.avail_out
= len
;
2931 s
->zstream
.next_out
= buf
;
2932 while (s
->zstream
.avail_out
> 0) {
2933 if (s
->zstream
.avail_in
== 0) {
2934 if (qemu_get_be16(s
->f
) != RAM_CBLOCK_MAGIC
)
2936 clen
= qemu_get_be16(s
->f
);
2937 if (clen
> IOBUF_SIZE
)
2939 qemu_get_buffer(s
->f
, s
->buf
, clen
);
2940 s
->zstream
.avail_in
= clen
;
2941 s
->zstream
.next_in
= s
->buf
;
2943 ret
= inflate(&s
->zstream
, Z_PARTIAL_FLUSH
);
2944 if (ret
!= Z_OK
&& ret
!= Z_STREAM_END
) {
2951 static void ram_decompress_close(RamDecompressState
*s
)
2953 inflateEnd(&s
->zstream
);
2956 #define RAM_SAVE_FLAG_FULL 0x01
2957 #define RAM_SAVE_FLAG_COMPRESS 0x02
2958 #define RAM_SAVE_FLAG_MEM_SIZE 0x04
2959 #define RAM_SAVE_FLAG_PAGE 0x08
2960 #define RAM_SAVE_FLAG_EOS 0x10
2962 static int is_dup_page(uint8_t *page
, uint8_t ch
)
2964 uint32_t val
= ch
<< 24 | ch
<< 16 | ch
<< 8 | ch
;
2965 uint32_t *array
= (uint32_t *)page
;
2968 for (i
= 0; i
< (TARGET_PAGE_SIZE
/ 4); i
++) {
2969 if (array
[i
] != val
)
2976 static int ram_save_block(QEMUFile
*f
)
2978 static ram_addr_t current_addr
= 0;
2979 ram_addr_t saved_addr
= current_addr
;
2980 ram_addr_t addr
= 0;
2983 while (addr
< last_ram_offset
) {
2984 if (cpu_physical_memory_get_dirty(current_addr
, MIGRATION_DIRTY_FLAG
)) {
2987 cpu_physical_memory_reset_dirty(current_addr
,
2988 current_addr
+ TARGET_PAGE_SIZE
,
2989 MIGRATION_DIRTY_FLAG
);
2991 p
= qemu_get_ram_ptr(current_addr
);
2993 if (is_dup_page(p
, *p
)) {
2994 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_COMPRESS
);
2995 qemu_put_byte(f
, *p
);
2997 qemu_put_be64(f
, current_addr
| RAM_SAVE_FLAG_PAGE
);
2998 qemu_put_buffer(f
, p
, TARGET_PAGE_SIZE
);
3004 addr
+= TARGET_PAGE_SIZE
;
3005 current_addr
= (saved_addr
+ addr
) % last_ram_offset
;
3011 static uint64_t bytes_transferred
= 0;
3013 static ram_addr_t
ram_save_remaining(void)
3016 ram_addr_t count
= 0;
3018 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3019 if (cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3026 uint64_t ram_bytes_remaining(void)
3028 return ram_save_remaining() * TARGET_PAGE_SIZE
;
3031 uint64_t ram_bytes_transferred(void)
3033 return bytes_transferred
;
3036 uint64_t ram_bytes_total(void)
3038 return last_ram_offset
;
3041 static int ram_save_live(QEMUFile
*f
, int stage
, void *opaque
)
3044 uint64_t bytes_transferred_last
;
3046 uint64_t expected_time
= 0;
3048 if (cpu_physical_sync_dirty_bitmap(0, TARGET_PHYS_ADDR_MAX
) != 0) {
3049 qemu_file_set_error(f
);
3054 /* Make sure all dirty bits are set */
3055 for (addr
= 0; addr
< last_ram_offset
; addr
+= TARGET_PAGE_SIZE
) {
3056 if (!cpu_physical_memory_get_dirty(addr
, MIGRATION_DIRTY_FLAG
))
3057 cpu_physical_memory_set_dirty(addr
);
3060 /* Enable dirty memory tracking */
3061 cpu_physical_memory_set_dirty_tracking(1);
3063 qemu_put_be64(f
, last_ram_offset
| RAM_SAVE_FLAG_MEM_SIZE
);
3066 bytes_transferred_last
= bytes_transferred
;
3067 bwidth
= get_clock();
3069 while (!qemu_file_rate_limit(f
)) {
3072 ret
= ram_save_block(f
);
3073 bytes_transferred
+= ret
* TARGET_PAGE_SIZE
;
3074 if (ret
== 0) /* no more blocks */
3078 bwidth
= get_clock() - bwidth
;
3079 bwidth
= (bytes_transferred
- bytes_transferred_last
) / bwidth
;
3081 /* if we haven't transferred anything this round, force expected_time to a
3082 * a very high value, but without crashing */
3086 /* try transferring iterative blocks of memory */
3090 /* flush all remaining blocks regardless of rate limiting */
3091 while (ram_save_block(f
) != 0) {
3092 bytes_transferred
+= TARGET_PAGE_SIZE
;
3094 cpu_physical_memory_set_dirty_tracking(0);
3097 qemu_put_be64(f
, RAM_SAVE_FLAG_EOS
);
3099 expected_time
= ram_save_remaining() * TARGET_PAGE_SIZE
/ bwidth
;
3101 return (stage
== 2) && (expected_time
<= migrate_max_downtime());
3104 static int ram_load_dead(QEMUFile
*f
, void *opaque
)
3106 RamDecompressState s1
, *s
= &s1
;
3110 if (ram_decompress_open(s
, f
) < 0)
3112 for(i
= 0; i
< last_ram_offset
; i
+= BDRV_HASH_BLOCK_SIZE
) {
3113 if (ram_decompress_buf(s
, buf
, 1) < 0) {
3114 fprintf(stderr
, "Error while reading ram block header\n");
3118 if (ram_decompress_buf(s
, qemu_get_ram_ptr(i
),
3119 BDRV_HASH_BLOCK_SIZE
) < 0) {
3120 fprintf(stderr
, "Error while reading ram block address=0x%08" PRIx64
, (uint64_t)i
);
3125 printf("Error block header\n");
3129 ram_decompress_close(s
);
3134 static int ram_load(QEMUFile
*f
, void *opaque
, int version_id
)
3139 if (version_id
== 1)
3140 return ram_load_v1(f
, opaque
);
3142 if (version_id
== 2) {
3143 if (qemu_get_be32(f
) != last_ram_offset
)
3145 return ram_load_dead(f
, opaque
);
3148 if (version_id
!= 3)
3152 addr
= qemu_get_be64(f
);
3154 flags
= addr
& ~TARGET_PAGE_MASK
;
3155 addr
&= TARGET_PAGE_MASK
;
3157 if (flags
& RAM_SAVE_FLAG_MEM_SIZE
) {
3158 if (addr
!= last_ram_offset
)
3162 if (flags
& RAM_SAVE_FLAG_FULL
) {
3163 if (ram_load_dead(f
, opaque
) < 0)
3167 if (flags
& RAM_SAVE_FLAG_COMPRESS
) {
3168 uint8_t ch
= qemu_get_byte(f
);
3169 memset(qemu_get_ram_ptr(addr
), ch
, TARGET_PAGE_SIZE
);
3172 (!kvm_enabled() || kvm_has_sync_mmu())) {
3173 madvise(qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
, MADV_DONTNEED
);
3176 } else if (flags
& RAM_SAVE_FLAG_PAGE
)
3177 qemu_get_buffer(f
, qemu_get_ram_ptr(addr
), TARGET_PAGE_SIZE
);
3178 } while (!(flags
& RAM_SAVE_FLAG_EOS
));
3183 void qemu_service_io(void)
3185 qemu_notify_event();
3188 /***********************************************************/
3189 /* bottom halves (can be seen as timers which expire ASAP) */
3200 static QEMUBH
*first_bh
= NULL
;
3202 QEMUBH
*qemu_bh_new(QEMUBHFunc
*cb
, void *opaque
)
3205 bh
= qemu_mallocz(sizeof(QEMUBH
));
3207 bh
->opaque
= opaque
;
3208 bh
->next
= first_bh
;
3213 int qemu_bh_poll(void)
3219 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3220 if (!bh
->deleted
&& bh
->scheduled
) {
3229 /* remove deleted bhs */
3243 void qemu_bh_schedule_idle(QEMUBH
*bh
)
3251 void qemu_bh_schedule(QEMUBH
*bh
)
3257 /* stop the currently executing CPU to execute the BH ASAP */
3258 qemu_notify_event();
3261 void qemu_bh_cancel(QEMUBH
*bh
)
3266 void qemu_bh_delete(QEMUBH
*bh
)
3272 static void qemu_bh_update_timeout(int *timeout
)
3276 for (bh
= first_bh
; bh
; bh
= bh
->next
) {
3277 if (!bh
->deleted
&& bh
->scheduled
) {
3279 /* idle bottom halves will be polled at least
3281 *timeout
= MIN(10, *timeout
);
3283 /* non-idle bottom halves will be executed
3292 /***********************************************************/
3293 /* machine registration */
3295 static QEMUMachine
*first_machine
= NULL
;
3296 QEMUMachine
*current_machine
= NULL
;
3298 int qemu_register_machine(QEMUMachine
*m
)
3301 pm
= &first_machine
;
3309 static QEMUMachine
*find_machine(const char *name
)
3313 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3314 if (!strcmp(m
->name
, name
))
3316 if (m
->alias
&& !strcmp(m
->alias
, name
))
3322 static QEMUMachine
*find_default_machine(void)
3326 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
3327 if (m
->is_default
) {
3334 /***********************************************************/
3335 /* main execution loop */
3337 static void gui_update(void *opaque
)
3339 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3340 DisplayState
*ds
= opaque
;
3341 DisplayChangeListener
*dcl
= ds
->listeners
;
3345 while (dcl
!= NULL
) {
3346 if (dcl
->gui_timer_interval
&&
3347 dcl
->gui_timer_interval
< interval
)
3348 interval
= dcl
->gui_timer_interval
;
3351 qemu_mod_timer(ds
->gui_timer
, interval
+ qemu_get_clock(rt_clock
));
3354 static void nographic_update(void *opaque
)
3356 uint64_t interval
= GUI_REFRESH_INTERVAL
;
3358 qemu_mod_timer(nographic_timer
, interval
+ qemu_get_clock(rt_clock
));
3361 struct vm_change_state_entry
{
3362 VMChangeStateHandler
*cb
;
3364 LIST_ENTRY (vm_change_state_entry
) entries
;
3367 static LIST_HEAD(vm_change_state_head
, vm_change_state_entry
) vm_change_state_head
;
3369 VMChangeStateEntry
*qemu_add_vm_change_state_handler(VMChangeStateHandler
*cb
,
3372 VMChangeStateEntry
*e
;
3374 e
= qemu_mallocz(sizeof (*e
));
3378 LIST_INSERT_HEAD(&vm_change_state_head
, e
, entries
);
3382 void qemu_del_vm_change_state_handler(VMChangeStateEntry
*e
)
3384 LIST_REMOVE (e
, entries
);
3388 static void vm_state_notify(int running
, int reason
)
3390 VMChangeStateEntry
*e
;
3392 for (e
= vm_change_state_head
.lh_first
; e
; e
= e
->entries
.le_next
) {
3393 e
->cb(e
->opaque
, running
, reason
);
3397 static void resume_all_vcpus(void);
3398 static void pause_all_vcpus(void);
3405 vm_state_notify(1, 0);
3406 qemu_rearm_alarm_timer(alarm_timer
);
3411 /* reset/shutdown handler */
3413 typedef struct QEMUResetEntry
{
3414 TAILQ_ENTRY(QEMUResetEntry
) entry
;
3415 QEMUResetHandler
*func
;
3419 static TAILQ_HEAD(reset_handlers
, QEMUResetEntry
) reset_handlers
=
3420 TAILQ_HEAD_INITIALIZER(reset_handlers
);
3421 static int reset_requested
;
3422 static int shutdown_requested
;
3423 static int powerdown_requested
;
3424 static int debug_requested
;
3425 static int vmstop_requested
;
3427 int qemu_shutdown_requested(void)
3429 int r
= shutdown_requested
;
3430 shutdown_requested
= 0;
3434 int qemu_reset_requested(void)
3436 int r
= reset_requested
;
3437 reset_requested
= 0;
3441 int qemu_powerdown_requested(void)
3443 int r
= powerdown_requested
;
3444 powerdown_requested
= 0;
3448 static int qemu_debug_requested(void)
3450 int r
= debug_requested
;
3451 debug_requested
= 0;
3455 static int qemu_vmstop_requested(void)
3457 int r
= vmstop_requested
;
3458 vmstop_requested
= 0;
3462 static void do_vm_stop(int reason
)
3465 cpu_disable_ticks();
3468 vm_state_notify(0, reason
);
3472 void qemu_register_reset(QEMUResetHandler
*func
, void *opaque
)
3474 QEMUResetEntry
*re
= qemu_mallocz(sizeof(QEMUResetEntry
));
3477 re
->opaque
= opaque
;
3478 TAILQ_INSERT_TAIL(&reset_handlers
, re
, entry
);
3481 void qemu_unregister_reset(QEMUResetHandler
*func
, void *opaque
)
3485 TAILQ_FOREACH(re
, &reset_handlers
, entry
) {
3486 if (re
->func
== func
&& re
->opaque
== opaque
) {
3487 TAILQ_REMOVE(&reset_handlers
, re
, entry
);
3494 void qemu_system_reset(void)
3496 QEMUResetEntry
*re
, *nre
;
3498 /* reset all devices */
3499 TAILQ_FOREACH_SAFE(re
, &reset_handlers
, entry
, nre
) {
3500 re
->func(re
->opaque
);
3504 void qemu_system_reset_request(void)
3507 shutdown_requested
= 1;
3509 reset_requested
= 1;
3511 qemu_notify_event();
3514 void qemu_system_shutdown_request(void)
3516 shutdown_requested
= 1;
3517 qemu_notify_event();
3520 void qemu_system_powerdown_request(void)
3522 powerdown_requested
= 1;
3523 qemu_notify_event();
3526 #ifdef CONFIG_IOTHREAD
3527 static void qemu_system_vmstop_request(int reason
)
3529 vmstop_requested
= reason
;
3530 qemu_notify_event();
3535 static int io_thread_fd
= -1;
3537 static void qemu_event_increment(void)
3539 static const char byte
= 0;
3541 if (io_thread_fd
== -1)
3544 write(io_thread_fd
, &byte
, sizeof(byte
));
3547 static void qemu_event_read(void *opaque
)
3549 int fd
= (unsigned long)opaque
;
3552 /* Drain the notify pipe */
3555 len
= read(fd
, buffer
, sizeof(buffer
));
3556 } while ((len
== -1 && errno
== EINTR
) || len
> 0);
3559 static int qemu_event_init(void)
3568 err
= fcntl_setfl(fds
[0], O_NONBLOCK
);
3572 err
= fcntl_setfl(fds
[1], O_NONBLOCK
);
3576 qemu_set_fd_handler2(fds
[0], NULL
, qemu_event_read
, NULL
,
3577 (void *)(unsigned long)fds
[0]);
3579 io_thread_fd
= fds
[1];
3588 HANDLE qemu_event_handle
;
3590 static void dummy_event_handler(void *opaque
)
3594 static int qemu_event_init(void)
3596 qemu_event_handle
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
3597 if (!qemu_event_handle
) {
3598 perror("Failed CreateEvent");
3601 qemu_add_wait_object(qemu_event_handle
, dummy_event_handler
, NULL
);
3605 static void qemu_event_increment(void)
3607 SetEvent(qemu_event_handle
);
3611 static int cpu_can_run(CPUState
*env
)
3620 #ifndef CONFIG_IOTHREAD
3621 static int qemu_init_main_loop(void)
3623 return qemu_event_init();
3626 void qemu_init_vcpu(void *_env
)
3628 CPUState
*env
= _env
;
3635 int qemu_cpu_self(void *env
)
3640 static void resume_all_vcpus(void)
3644 static void pause_all_vcpus(void)
3648 void qemu_cpu_kick(void *env
)
3653 void qemu_notify_event(void)
3655 CPUState
*env
= cpu_single_env
;
3660 if (env
->kqemu_enabled
)
3661 kqemu_cpu_interrupt(env
);
3666 #define qemu_mutex_lock_iothread() do { } while (0)
3667 #define qemu_mutex_unlock_iothread() do { } while (0)
3669 void vm_stop(int reason
)
3674 #else /* CONFIG_IOTHREAD */
3676 #include "qemu-thread.h"
3678 QemuMutex qemu_global_mutex
;
3679 static QemuMutex qemu_fair_mutex
;
3681 static QemuThread io_thread
;
3683 static QemuThread
*tcg_cpu_thread
;
3684 static QemuCond
*tcg_halt_cond
;
3686 static int qemu_system_ready
;
3688 static QemuCond qemu_cpu_cond
;
3690 static QemuCond qemu_system_cond
;
3691 static QemuCond qemu_pause_cond
;
3693 static void block_io_signals(void);
3694 static void unblock_io_signals(void);
3695 static int tcg_has_work(void);
3697 static int qemu_init_main_loop(void)
3701 ret
= qemu_event_init();
3705 qemu_cond_init(&qemu_pause_cond
);
3706 qemu_mutex_init(&qemu_fair_mutex
);
3707 qemu_mutex_init(&qemu_global_mutex
);
3708 qemu_mutex_lock(&qemu_global_mutex
);
3710 unblock_io_signals();
3711 qemu_thread_self(&io_thread
);
3716 static void qemu_wait_io_event(CPUState
*env
)
3718 while (!tcg_has_work())
3719 qemu_cond_timedwait(env
->halt_cond
, &qemu_global_mutex
, 1000);
3721 qemu_mutex_unlock(&qemu_global_mutex
);
3724 * Users of qemu_global_mutex can be starved, having no chance
3725 * to acquire it since this path will get to it first.
3726 * So use another lock to provide fairness.
3728 qemu_mutex_lock(&qemu_fair_mutex
);
3729 qemu_mutex_unlock(&qemu_fair_mutex
);
3731 qemu_mutex_lock(&qemu_global_mutex
);
3735 qemu_cond_signal(&qemu_pause_cond
);
3739 static int qemu_cpu_exec(CPUState
*env
);
3741 static void *kvm_cpu_thread_fn(void *arg
)
3743 CPUState
*env
= arg
;
3746 qemu_thread_self(env
->thread
);
3748 /* signal CPU creation */
3749 qemu_mutex_lock(&qemu_global_mutex
);
3751 qemu_cond_signal(&qemu_cpu_cond
);
3753 /* and wait for machine initialization */
3754 while (!qemu_system_ready
)
3755 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3758 if (cpu_can_run(env
))
3760 qemu_wait_io_event(env
);
3766 static void tcg_cpu_exec(void);
3768 static void *tcg_cpu_thread_fn(void *arg
)
3770 CPUState
*env
= arg
;
3773 qemu_thread_self(env
->thread
);
3775 /* signal CPU creation */
3776 qemu_mutex_lock(&qemu_global_mutex
);
3777 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
3779 qemu_cond_signal(&qemu_cpu_cond
);
3781 /* and wait for machine initialization */
3782 while (!qemu_system_ready
)
3783 qemu_cond_timedwait(&qemu_system_cond
, &qemu_global_mutex
, 100);
3787 qemu_wait_io_event(cur_cpu
);
3793 void qemu_cpu_kick(void *_env
)
3795 CPUState
*env
= _env
;
3796 qemu_cond_broadcast(env
->halt_cond
);
3798 qemu_thread_signal(env
->thread
, SIGUSR1
);
3801 int qemu_cpu_self(void *env
)
3803 return (cpu_single_env
!= NULL
);
3806 static void cpu_signal(int sig
)
3809 cpu_exit(cpu_single_env
);
3812 static void block_io_signals(void)
3815 struct sigaction sigact
;
3818 sigaddset(&set
, SIGUSR2
);
3819 sigaddset(&set
, SIGIO
);
3820 sigaddset(&set
, SIGALRM
);
3821 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3824 sigaddset(&set
, SIGUSR1
);
3825 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3827 memset(&sigact
, 0, sizeof(sigact
));
3828 sigact
.sa_handler
= cpu_signal
;
3829 sigaction(SIGUSR1
, &sigact
, NULL
);
3832 static void unblock_io_signals(void)
3837 sigaddset(&set
, SIGUSR2
);
3838 sigaddset(&set
, SIGIO
);
3839 sigaddset(&set
, SIGALRM
);
3840 pthread_sigmask(SIG_UNBLOCK
, &set
, NULL
);
3843 sigaddset(&set
, SIGUSR1
);
3844 pthread_sigmask(SIG_BLOCK
, &set
, NULL
);
3847 static void qemu_signal_lock(unsigned int msecs
)
3849 qemu_mutex_lock(&qemu_fair_mutex
);
3851 while (qemu_mutex_trylock(&qemu_global_mutex
)) {
3852 qemu_thread_signal(tcg_cpu_thread
, SIGUSR1
);
3853 if (!qemu_mutex_timedlock(&qemu_global_mutex
, msecs
))
3856 qemu_mutex_unlock(&qemu_fair_mutex
);
3859 static void qemu_mutex_lock_iothread(void)
3861 if (kvm_enabled()) {
3862 qemu_mutex_lock(&qemu_fair_mutex
);
3863 qemu_mutex_lock(&qemu_global_mutex
);
3864 qemu_mutex_unlock(&qemu_fair_mutex
);
3866 qemu_signal_lock(100);
3869 static void qemu_mutex_unlock_iothread(void)
3871 qemu_mutex_unlock(&qemu_global_mutex
);
3874 static int all_vcpus_paused(void)
3876 CPUState
*penv
= first_cpu
;
3881 penv
= (CPUState
*)penv
->next_cpu
;
3887 static void pause_all_vcpus(void)
3889 CPUState
*penv
= first_cpu
;
3893 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3894 qemu_cpu_kick(penv
);
3895 penv
= (CPUState
*)penv
->next_cpu
;
3898 while (!all_vcpus_paused()) {
3899 qemu_cond_timedwait(&qemu_pause_cond
, &qemu_global_mutex
, 100);
3902 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3903 penv
= (CPUState
*)penv
->next_cpu
;
3908 static void resume_all_vcpus(void)
3910 CPUState
*penv
= first_cpu
;
3915 qemu_thread_signal(penv
->thread
, SIGUSR1
);
3916 qemu_cpu_kick(penv
);
3917 penv
= (CPUState
*)penv
->next_cpu
;
3921 static void tcg_init_vcpu(void *_env
)
3923 CPUState
*env
= _env
;
3924 /* share a single thread for all cpus with TCG */
3925 if (!tcg_cpu_thread
) {
3926 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3927 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3928 qemu_cond_init(env
->halt_cond
);
3929 qemu_thread_create(env
->thread
, tcg_cpu_thread_fn
, env
);
3930 while (env
->created
== 0)
3931 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3932 tcg_cpu_thread
= env
->thread
;
3933 tcg_halt_cond
= env
->halt_cond
;
3935 env
->thread
= tcg_cpu_thread
;
3936 env
->halt_cond
= tcg_halt_cond
;
3940 static void kvm_start_vcpu(CPUState
*env
)
3943 env
->thread
= qemu_mallocz(sizeof(QemuThread
));
3944 env
->halt_cond
= qemu_mallocz(sizeof(QemuCond
));
3945 qemu_cond_init(env
->halt_cond
);
3946 qemu_thread_create(env
->thread
, kvm_cpu_thread_fn
, env
);
3947 while (env
->created
== 0)
3948 qemu_cond_timedwait(&qemu_cpu_cond
, &qemu_global_mutex
, 100);
3951 void qemu_init_vcpu(void *_env
)
3953 CPUState
*env
= _env
;
3956 kvm_start_vcpu(env
);
3961 void qemu_notify_event(void)
3963 qemu_event_increment();
3966 void vm_stop(int reason
)
3969 qemu_thread_self(&me
);
3971 if (!qemu_thread_equal(&me
, &io_thread
)) {
3972 qemu_system_vmstop_request(reason
);
3974 * FIXME: should not return to device code in case
3975 * vm_stop() has been requested.
3977 if (cpu_single_env
) {
3978 cpu_exit(cpu_single_env
);
3979 cpu_single_env
->stop
= 1;
3990 static void host_main_loop_wait(int *timeout
)
3996 /* XXX: need to suppress polling by better using win32 events */
3998 for(pe
= first_polling_entry
; pe
!= NULL
; pe
= pe
->next
) {
3999 ret
|= pe
->func(pe
->opaque
);
4003 WaitObjects
*w
= &wait_objects
;
4005 ret
= WaitForMultipleObjects(w
->num
, w
->events
, FALSE
, *timeout
);
4006 if (WAIT_OBJECT_0
+ 0 <= ret
&& ret
<= WAIT_OBJECT_0
+ w
->num
- 1) {
4007 if (w
->func
[ret
- WAIT_OBJECT_0
])
4008 w
->func
[ret
- WAIT_OBJECT_0
](w
->opaque
[ret
- WAIT_OBJECT_0
]);
4010 /* Check for additional signaled events */
4011 for(i
= (ret
- WAIT_OBJECT_0
+ 1); i
< w
->num
; i
++) {
4013 /* Check if event is signaled */
4014 ret2
= WaitForSingleObject(w
->events
[i
], 0);
4015 if(ret2
== WAIT_OBJECT_0
) {
4017 w
->func
[i
](w
->opaque
[i
]);
4018 } else if (ret2
== WAIT_TIMEOUT
) {
4020 err
= GetLastError();
4021 fprintf(stderr
, "WaitForSingleObject error %d %d\n", i
, err
);
4024 } else if (ret
== WAIT_TIMEOUT
) {
4026 err
= GetLastError();
4027 fprintf(stderr
, "WaitForMultipleObjects error %d %d\n", ret
, err
);
4034 static void host_main_loop_wait(int *timeout
)
4039 void main_loop_wait(int timeout
)
4041 IOHandlerRecord
*ioh
;
4042 fd_set rfds
, wfds
, xfds
;
4046 qemu_bh_update_timeout(&timeout
);
4048 host_main_loop_wait(&timeout
);
4050 /* poll any events */
4051 /* XXX: separate device handlers from system ones */
4056 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4060 (!ioh
->fd_read_poll
||
4061 ioh
->fd_read_poll(ioh
->opaque
) != 0)) {
4062 FD_SET(ioh
->fd
, &rfds
);
4066 if (ioh
->fd_write
) {
4067 FD_SET(ioh
->fd
, &wfds
);
4073 tv
.tv_sec
= timeout
/ 1000;
4074 tv
.tv_usec
= (timeout
% 1000) * 1000;
4076 slirp_select_fill(&nfds
, &rfds
, &wfds
, &xfds
);
4078 qemu_mutex_unlock_iothread();
4079 ret
= select(nfds
+ 1, &rfds
, &wfds
, &xfds
, &tv
);
4080 qemu_mutex_lock_iothread();
4082 IOHandlerRecord
**pioh
;
4084 for(ioh
= first_io_handler
; ioh
!= NULL
; ioh
= ioh
->next
) {
4085 if (!ioh
->deleted
&& ioh
->fd_read
&& FD_ISSET(ioh
->fd
, &rfds
)) {
4086 ioh
->fd_read(ioh
->opaque
);
4088 if (!ioh
->deleted
&& ioh
->fd_write
&& FD_ISSET(ioh
->fd
, &wfds
)) {
4089 ioh
->fd_write(ioh
->opaque
);
4093 /* remove deleted IO handlers */
4094 pioh
= &first_io_handler
;
4105 slirp_select_poll(&rfds
, &wfds
, &xfds
, (ret
< 0));
4107 /* rearm timer, if not periodic */
4108 if (alarm_timer
->flags
& ALARM_FLAG_EXPIRED
) {
4109 alarm_timer
->flags
&= ~ALARM_FLAG_EXPIRED
;
4110 qemu_rearm_alarm_timer(alarm_timer
);
4113 /* vm time timers */
4115 if (!cur_cpu
|| likely(!(cur_cpu
->singlestep_enabled
& SSTEP_NOTIMER
)))
4116 qemu_run_timers(&active_timers
[QEMU_TIMER_VIRTUAL
],
4117 qemu_get_clock(vm_clock
));
4120 /* real time timers */
4121 qemu_run_timers(&active_timers
[QEMU_TIMER_REALTIME
],
4122 qemu_get_clock(rt_clock
));
4124 /* Check bottom-halves last in case any of the earlier events triggered
4130 static int qemu_cpu_exec(CPUState
*env
)
4133 #ifdef CONFIG_PROFILER
4137 #ifdef CONFIG_PROFILER
4138 ti
= profile_getclock();
4143 qemu_icount
-= (env
->icount_decr
.u16
.low
+ env
->icount_extra
);
4144 env
->icount_decr
.u16
.low
= 0;
4145 env
->icount_extra
= 0;
4146 count
= qemu_next_deadline();
4147 count
= (count
+ (1 << icount_time_shift
) - 1)
4148 >> icount_time_shift
;
4149 qemu_icount
+= count
;
4150 decr
= (count
> 0xffff) ? 0xffff : count
;
4152 env
->icount_decr
.u16
.low
= decr
;
4153 env
->icount_extra
= count
;
4155 ret
= cpu_exec(env
);
4156 #ifdef CONFIG_PROFILER
4157 qemu_time
+= profile_getclock() - ti
;
4160 /* Fold pending instructions back into the
4161 instruction counter, and clear the interrupt flag. */
4162 qemu_icount
-= (env
->icount_decr
.u16
.low
4163 + env
->icount_extra
);
4164 env
->icount_decr
.u32
= 0;
4165 env
->icount_extra
= 0;
4170 static void tcg_cpu_exec(void)
4174 if (next_cpu
== NULL
)
4175 next_cpu
= first_cpu
;
4176 for (; next_cpu
!= NULL
; next_cpu
= next_cpu
->next_cpu
) {
4177 CPUState
*env
= cur_cpu
= next_cpu
;
4181 if (timer_alarm_pending
) {
4182 timer_alarm_pending
= 0;
4185 if (cpu_can_run(env
))
4186 ret
= qemu_cpu_exec(env
);
4187 if (ret
== EXCP_DEBUG
) {
4188 gdb_set_stop_cpu(env
);
4189 debug_requested
= 1;
4195 static int cpu_has_work(CPUState
*env
)
4203 if (qemu_cpu_has_work(env
))
4208 static int tcg_has_work(void)
4212 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
4213 if (cpu_has_work(env
))
4218 static int qemu_calculate_timeout(void)
4220 #ifndef CONFIG_IOTHREAD
4225 else if (tcg_has_work())
4227 else if (!use_icount
)
4230 /* XXX: use timeout computed from timers */
4233 /* Advance virtual time to the next event. */
4234 if (use_icount
== 1) {
4235 /* When not using an adaptive execution frequency
4236 we tend to get badly out of sync with real time,
4237 so just delay for a reasonable amount of time. */
4240 delta
= cpu_get_icount() - cpu_get_clock();
4243 /* If virtual time is ahead of real time then just
4245 timeout
= (delta
/ 1000000) + 1;
4247 /* Wait for either IO to occur or the next
4249 add
= qemu_next_deadline();
4250 /* We advance the timer before checking for IO.
4251 Limit the amount we advance so that early IO
4252 activity won't get the guest too far ahead. */
4256 add
= (add
+ (1 << icount_time_shift
) - 1)
4257 >> icount_time_shift
;
4259 timeout
= delta
/ 1000000;
4266 #else /* CONFIG_IOTHREAD */
4271 static int vm_can_run(void)
4273 if (powerdown_requested
)
4275 if (reset_requested
)
4277 if (shutdown_requested
)
4279 if (debug_requested
)
4284 static void main_loop(void)
4288 #ifdef CONFIG_IOTHREAD
4289 qemu_system_ready
= 1;
4290 qemu_cond_broadcast(&qemu_system_cond
);
4295 #ifdef CONFIG_PROFILER
4298 #ifndef CONFIG_IOTHREAD
4301 #ifdef CONFIG_PROFILER
4302 ti
= profile_getclock();
4304 main_loop_wait(qemu_calculate_timeout());
4305 #ifdef CONFIG_PROFILER
4306 dev_time
+= profile_getclock() - ti
;
4308 } while (vm_can_run());
4310 if (qemu_debug_requested())
4311 vm_stop(EXCP_DEBUG
);
4312 if (qemu_shutdown_requested()) {
4319 if (qemu_reset_requested()) {
4321 qemu_system_reset();
4324 if (qemu_powerdown_requested())
4325 qemu_system_powerdown();
4326 if ((r
= qemu_vmstop_requested()))
4332 static void version(void)
4334 printf("QEMU PC emulator version " QEMU_VERSION QEMU_PKGVERSION
", Copyright (c) 2003-2008 Fabrice Bellard\n");
4337 static void help(int exitcode
)
4340 printf("usage: %s [options] [disk_image]\n"
4342 "'disk_image' is a raw hard image image for IDE hard disk 0\n"
4344 #define DEF(option, opt_arg, opt_enum, opt_help) \
4346 #define DEFHEADING(text) stringify(text) "\n"
4347 #include "qemu-options.h"
4352 "During emulation, the following keys are useful:\n"
4353 "ctrl-alt-f toggle full screen\n"
4354 "ctrl-alt-n switch to virtual console 'n'\n"
4355 "ctrl-alt toggle mouse and keyboard grab\n"
4357 "When using -nographic, press 'ctrl-a h' to get some help.\n"
4362 DEFAULT_NETWORK_SCRIPT
,
4363 DEFAULT_NETWORK_DOWN_SCRIPT
,
4365 DEFAULT_GDBSTUB_PORT
,
4370 #define HAS_ARG 0x0001
4373 #define DEF(option, opt_arg, opt_enum, opt_help) \
4375 #define DEFHEADING(text)
4376 #include "qemu-options.h"
4382 typedef struct QEMUOption
{
4388 static const QEMUOption qemu_options
[] = {
4389 { "h", 0, QEMU_OPTION_h
},
4390 #define DEF(option, opt_arg, opt_enum, opt_help) \
4391 { option, opt_arg, opt_enum },
4392 #define DEFHEADING(text)
4393 #include "qemu-options.h"
4401 struct soundhw soundhw
[] = {
4402 #ifdef HAS_AUDIO_CHOICE
4403 #if defined(TARGET_I386) || defined(TARGET_MIPS)
4409 { .init_isa
= pcspk_audio_init
}
4416 "Creative Sound Blaster 16",
4419 { .init_isa
= SB16_init
}
4423 #ifdef CONFIG_CS4231A
4429 { .init_isa
= cs4231a_init
}
4437 "Yamaha YMF262 (OPL3)",
4439 "Yamaha YM3812 (OPL2)",
4443 { .init_isa
= Adlib_init
}
4450 "Gravis Ultrasound GF1",
4453 { .init_isa
= GUS_init
}
4460 "Intel 82801AA AC97 Audio",
4463 { .init_pci
= ac97_init
}
4467 #ifdef CONFIG_ES1370
4470 "ENSONIQ AudioPCI ES1370",
4473 { .init_pci
= es1370_init
}
4477 #endif /* HAS_AUDIO_CHOICE */
4479 { NULL
, NULL
, 0, 0, { NULL
} }
4482 static void select_soundhw (const char *optarg
)
4486 if (*optarg
== '?') {
4489 printf ("Valid sound card names (comma separated):\n");
4490 for (c
= soundhw
; c
->name
; ++c
) {
4491 printf ("%-11s %s\n", c
->name
, c
->descr
);
4493 printf ("\n-soundhw all will enable all of the above\n");
4494 exit (*optarg
!= '?');
4502 if (!strcmp (optarg
, "all")) {
4503 for (c
= soundhw
; c
->name
; ++c
) {
4511 e
= strchr (p
, ',');
4512 l
= !e
? strlen (p
) : (size_t) (e
- p
);
4514 for (c
= soundhw
; c
->name
; ++c
) {
4515 if (!strncmp (c
->name
, p
, l
)) {
4524 "Unknown sound card name (too big to show)\n");
4527 fprintf (stderr
, "Unknown sound card name `%.*s'\n",
4532 p
+= l
+ (e
!= NULL
);
4536 goto show_valid_cards
;
4541 static void select_vgahw (const char *p
)
4545 cirrus_vga_enabled
= 0;
4546 std_vga_enabled
= 0;
4549 if (strstart(p
, "std", &opts
)) {
4550 std_vga_enabled
= 1;
4551 } else if (strstart(p
, "cirrus", &opts
)) {
4552 cirrus_vga_enabled
= 1;
4553 } else if (strstart(p
, "vmware", &opts
)) {
4555 } else if (strstart(p
, "xenfb", &opts
)) {
4557 } else if (!strstart(p
, "none", &opts
)) {
4559 fprintf(stderr
, "Unknown vga type: %s\n", p
);
4563 const char *nextopt
;
4565 if (strstart(opts
, ",retrace=", &nextopt
)) {
4567 if (strstart(opts
, "dumb", &nextopt
))
4568 vga_retrace_method
= VGA_RETRACE_DUMB
;
4569 else if (strstart(opts
, "precise", &nextopt
))
4570 vga_retrace_method
= VGA_RETRACE_PRECISE
;
4571 else goto invalid_vga
;
4572 } else goto invalid_vga
;
4578 static int balloon_parse(const char *arg
)
4583 if (!strcmp(arg
, "none")) {
4585 } else if (!strncmp(arg
, "virtio", 6)) {
4587 if (arg
[6] == ',') {
4589 if (get_param_value(buf
, sizeof(buf
), "addr", p
)) {
4590 virtio_balloon_devaddr
= strdup(buf
);
4601 static BOOL WINAPI
qemu_ctrl_handler(DWORD type
)
4603 exit(STATUS_CONTROL_C_EXIT
);
4608 int qemu_uuid_parse(const char *str
, uint8_t *uuid
)
4612 if(strlen(str
) != 36)
4615 ret
= sscanf(str
, UUID_FMT
, &uuid
[0], &uuid
[1], &uuid
[2], &uuid
[3],
4616 &uuid
[4], &uuid
[5], &uuid
[6], &uuid
[7], &uuid
[8], &uuid
[9],
4617 &uuid
[10], &uuid
[11], &uuid
[12], &uuid
[13], &uuid
[14], &uuid
[15]);
4623 smbios_add_field(1, offsetof(struct smbios_type_1
, uuid
), 16, uuid
);
4629 #define MAX_NET_CLIENTS 32
4633 static void termsig_handler(int signal
)
4635 qemu_system_shutdown_request();
4638 static void sigchld_handler(int signal
)
4640 waitpid(-1, NULL
, WNOHANG
);
4643 static void sighandler_setup(void)
4645 struct sigaction act
;
4647 memset(&act
, 0, sizeof(act
));
4648 act
.sa_handler
= termsig_handler
;
4649 sigaction(SIGINT
, &act
, NULL
);
4650 sigaction(SIGHUP
, &act
, NULL
);
4651 sigaction(SIGTERM
, &act
, NULL
);
4653 act
.sa_handler
= sigchld_handler
;
4654 act
.sa_flags
= SA_NOCLDSTOP
;
4655 sigaction(SIGCHLD
, &act
, NULL
);
4661 /* Look for support files in the same directory as the executable. */
4662 static char *find_datadir(const char *argv0
)
4668 len
= GetModuleFileName(NULL
, buf
, sizeof(buf
) - 1);
4675 while (p
!= buf
&& *p
!= '\\')
4678 if (access(buf
, R_OK
) == 0) {
4679 return qemu_strdup(buf
);
4685 /* Find a likely location for support files using the location of the binary.
4686 For installed binaries this will be "$bindir/../share/qemu". When
4687 running from the build tree this will be "$bindir/../pc-bios". */
4688 #define SHARE_SUFFIX "/share/qemu"
4689 #define BUILD_SUFFIX "/pc-bios"
4690 static char *find_datadir(const char *argv0
)
4700 #if defined(__linux__)
4703 len
= readlink("/proc/self/exe", buf
, sizeof(buf
) - 1);
4709 #elif defined(__FreeBSD__)
4712 len
= readlink("/proc/curproc/file", buf
, sizeof(buf
) - 1);
4719 /* If we don't have any way of figuring out the actual executable
4720 location then try argv[0]. */
4725 p
= realpath(argv0
, p
);
4733 max_len
= strlen(dir
) +
4734 MAX(strlen(SHARE_SUFFIX
), strlen(BUILD_SUFFIX
)) + 1;
4735 res
= qemu_mallocz(max_len
);
4736 snprintf(res
, max_len
, "%s%s", dir
, SHARE_SUFFIX
);
4737 if (access(res
, R_OK
)) {
4738 snprintf(res
, max_len
, "%s%s", dir
, BUILD_SUFFIX
);
4739 if (access(res
, R_OK
)) {
4753 char *qemu_find_file(int type
, const char *name
)
4759 /* If name contains path separators then try it as a straight path. */
4760 if ((strchr(name
, '/') || strchr(name
, '\\'))
4761 && access(name
, R_OK
) == 0) {
4762 return strdup(name
);
4765 case QEMU_FILE_TYPE_BIOS
:
4768 case QEMU_FILE_TYPE_KEYMAP
:
4769 subdir
= "keymaps/";
4774 len
= strlen(data_dir
) + strlen(name
) + strlen(subdir
) + 2;
4775 buf
= qemu_mallocz(len
);
4776 snprintf(buf
, len
, "%s/%s%s", data_dir
, subdir
, name
);
4777 if (access(buf
, R_OK
)) {
4784 struct device_config
{
4786 DEV_GENERIC
, /* -device */
4787 DEV_USB
, /* -usbdevice */
4790 const char *cmdline
;
4791 TAILQ_ENTRY(device_config
) next
;
4793 TAILQ_HEAD(, device_config
) device_configs
= TAILQ_HEAD_INITIALIZER(device_configs
);
4795 static void add_device_config(int type
, const char *cmdline
)
4797 struct device_config
*conf
;
4799 conf
= qemu_mallocz(sizeof(*conf
));
4801 conf
->cmdline
= cmdline
;
4802 TAILQ_INSERT_TAIL(&device_configs
, conf
, next
);
4805 static int foreach_device_config(int type
, int (*func
)(const char *cmdline
))
4807 struct device_config
*conf
;
4810 TAILQ_FOREACH(conf
, &device_configs
, next
) {
4811 if (conf
->type
!= type
)
4813 rc
= func(conf
->cmdline
);
4820 static int generic_parse(const char *cmdline
)
4824 dev
= qdev_device_add(cmdline
);
4830 int main(int argc
, char **argv
, char **envp
)
4832 const char *gdbstub_dev
= NULL
;
4833 uint32_t boot_devices_bitmap
= 0;
4835 int snapshot
, linux_boot
, net_boot
;
4836 const char *initrd_filename
;
4837 const char *kernel_filename
, *kernel_cmdline
;
4838 char boot_devices
[33] = "cad"; /* default to HD->floppy->CD-ROM */
4840 DisplayChangeListener
*dcl
;
4841 int cyls
, heads
, secs
, translation
;
4842 const char *net_clients
[MAX_NET_CLIENTS
];
4844 QemuOpts
*hda_opts
= NULL
;
4846 const char *r
, *optarg
;
4847 CharDriverState
*monitor_hd
= NULL
;
4848 const char *monitor_device
;
4849 const char *serial_devices
[MAX_SERIAL_PORTS
];
4850 int serial_device_index
;
4851 const char *parallel_devices
[MAX_PARALLEL_PORTS
];
4852 int parallel_device_index
;
4853 const char *virtio_consoles
[MAX_VIRTIO_CONSOLES
];
4854 int virtio_console_index
;
4855 const char *loadvm
= NULL
;
4856 QEMUMachine
*machine
;
4857 const char *cpu_model
;
4862 const char *pid_file
= NULL
;
4863 const char *incoming
= NULL
;
4866 struct passwd
*pwd
= NULL
;
4867 const char *chroot_dir
= NULL
;
4868 const char *run_as
= NULL
;
4871 int show_vnc_port
= 0;
4873 qemu_cache_utils_init(envp
);
4875 LIST_INIT (&vm_change_state_head
);
4878 struct sigaction act
;
4879 sigfillset(&act
.sa_mask
);
4881 act
.sa_handler
= SIG_IGN
;
4882 sigaction(SIGPIPE
, &act
, NULL
);
4885 SetConsoleCtrlHandler(qemu_ctrl_handler
, TRUE
);
4886 /* Note: cpu_interrupt() is currently not SMP safe, so we force
4887 QEMU to run on a single CPU */
4892 h
= GetCurrentProcess();
4893 if (GetProcessAffinityMask(h
, &mask
, &smask
)) {
4894 for(i
= 0; i
< 32; i
++) {
4895 if (mask
& (1 << i
))
4900 SetProcessAffinityMask(h
, mask
);
4906 module_call_init(MODULE_INIT_MACHINE
);
4907 machine
= find_default_machine();
4909 initrd_filename
= NULL
;
4912 kernel_filename
= NULL
;
4913 kernel_cmdline
= "";
4914 cyls
= heads
= secs
= 0;
4915 translation
= BIOS_ATA_TRANSLATION_AUTO
;
4916 monitor_device
= "vc:80Cx24C";
4918 serial_devices
[0] = "vc:80Cx24C";
4919 for(i
= 1; i
< MAX_SERIAL_PORTS
; i
++)
4920 serial_devices
[i
] = NULL
;
4921 serial_device_index
= 0;
4923 parallel_devices
[0] = "vc:80Cx24C";
4924 for(i
= 1; i
< MAX_PARALLEL_PORTS
; i
++)
4925 parallel_devices
[i
] = NULL
;
4926 parallel_device_index
= 0;
4928 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++)
4929 virtio_consoles
[i
] = NULL
;
4930 virtio_console_index
= 0;
4932 for (i
= 0; i
< MAX_NODES
; i
++) {
4934 node_cpumask
[i
] = 0;
4944 register_watchdogs();
4952 hda_opts
= drive_add(argv
[optind
++], HD_ALIAS
, 0);
4954 const QEMUOption
*popt
;
4957 /* Treat --foo the same as -foo. */
4960 popt
= qemu_options
;
4963 fprintf(stderr
, "%s: invalid option -- '%s'\n",
4967 if (!strcmp(popt
->name
, r
+ 1))
4971 if (popt
->flags
& HAS_ARG
) {
4972 if (optind
>= argc
) {
4973 fprintf(stderr
, "%s: option '%s' requires an argument\n",
4977 optarg
= argv
[optind
++];
4982 switch(popt
->index
) {
4984 machine
= find_machine(optarg
);
4987 printf("Supported machines are:\n");
4988 for(m
= first_machine
; m
!= NULL
; m
= m
->next
) {
4990 printf("%-10s %s (alias of %s)\n",
4991 m
->alias
, m
->desc
, m
->name
);
4992 printf("%-10s %s%s\n",
4994 m
->is_default
? " (default)" : "");
4996 exit(*optarg
!= '?');
4999 case QEMU_OPTION_cpu
:
5000 /* hw initialization will check this */
5001 if (*optarg
== '?') {
5002 /* XXX: implement xxx_cpu_list for targets that still miss it */
5003 #if defined(cpu_list)
5004 cpu_list(stdout
, &fprintf
);
5011 case QEMU_OPTION_initrd
:
5012 initrd_filename
= optarg
;
5014 case QEMU_OPTION_hda
:
5016 hda_opts
= drive_add(optarg
, HD_ALIAS
, 0);
5018 hda_opts
= drive_add(optarg
, HD_ALIAS
5019 ",cyls=%d,heads=%d,secs=%d%s",
5020 0, cyls
, heads
, secs
,
5021 translation
== BIOS_ATA_TRANSLATION_LBA
?
5023 translation
== BIOS_ATA_TRANSLATION_NONE
?
5024 ",trans=none" : "");
5026 case QEMU_OPTION_hdb
:
5027 case QEMU_OPTION_hdc
:
5028 case QEMU_OPTION_hdd
:
5029 drive_add(optarg
, HD_ALIAS
, popt
->index
- QEMU_OPTION_hda
);
5031 case QEMU_OPTION_drive
:
5032 drive_add(NULL
, "%s", optarg
);
5034 case QEMU_OPTION_mtdblock
:
5035 drive_add(optarg
, MTD_ALIAS
);
5037 case QEMU_OPTION_sd
:
5038 drive_add(optarg
, SD_ALIAS
);
5040 case QEMU_OPTION_pflash
:
5041 drive_add(optarg
, PFLASH_ALIAS
);
5043 case QEMU_OPTION_snapshot
:
5046 case QEMU_OPTION_hdachs
:
5050 cyls
= strtol(p
, (char **)&p
, 0);
5051 if (cyls
< 1 || cyls
> 16383)
5056 heads
= strtol(p
, (char **)&p
, 0);
5057 if (heads
< 1 || heads
> 16)
5062 secs
= strtol(p
, (char **)&p
, 0);
5063 if (secs
< 1 || secs
> 63)
5067 if (!strcmp(p
, "none"))
5068 translation
= BIOS_ATA_TRANSLATION_NONE
;
5069 else if (!strcmp(p
, "lba"))
5070 translation
= BIOS_ATA_TRANSLATION_LBA
;
5071 else if (!strcmp(p
, "auto"))
5072 translation
= BIOS_ATA_TRANSLATION_AUTO
;
5075 } else if (*p
!= '\0') {
5077 fprintf(stderr
, "qemu: invalid physical CHS format\n");
5080 if (hda_opts
!= NULL
) {
5082 snprintf(num
, sizeof(num
), "%d", cyls
);
5083 qemu_opt_set(hda_opts
, "cyls", num
);
5084 snprintf(num
, sizeof(num
), "%d", heads
);
5085 qemu_opt_set(hda_opts
, "heads", num
);
5086 snprintf(num
, sizeof(num
), "%d", secs
);
5087 qemu_opt_set(hda_opts
, "secs", num
);
5088 if (translation
== BIOS_ATA_TRANSLATION_LBA
)
5089 qemu_opt_set(hda_opts
, "trans", "lba");
5090 if (translation
== BIOS_ATA_TRANSLATION_NONE
)
5091 qemu_opt_set(hda_opts
, "trans", "none");
5095 case QEMU_OPTION_numa
:
5096 if (nb_numa_nodes
>= MAX_NODES
) {
5097 fprintf(stderr
, "qemu: too many NUMA nodes\n");
5102 case QEMU_OPTION_nographic
:
5103 display_type
= DT_NOGRAPHIC
;
5105 #ifdef CONFIG_CURSES
5106 case QEMU_OPTION_curses
:
5107 display_type
= DT_CURSES
;
5110 case QEMU_OPTION_portrait
:
5113 case QEMU_OPTION_kernel
:
5114 kernel_filename
= optarg
;
5116 case QEMU_OPTION_append
:
5117 kernel_cmdline
= optarg
;
5119 case QEMU_OPTION_cdrom
:
5120 drive_add(optarg
, CDROM_ALIAS
);
5122 case QEMU_OPTION_boot
:
5124 static const char * const params
[] = {
5125 "order", "once", "menu", NULL
5127 char buf
[sizeof(boot_devices
)];
5128 char *standard_boot_devices
;
5131 if (!strchr(optarg
, '=')) {
5133 pstrcpy(buf
, sizeof(buf
), optarg
);
5134 } else if (check_params(buf
, sizeof(buf
), params
, optarg
) < 0) {
5136 "qemu: unknown boot parameter '%s' in '%s'\n",
5142 get_param_value(buf
, sizeof(buf
), "order", optarg
)) {
5143 boot_devices_bitmap
= parse_bootdevices(buf
);
5144 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5147 if (get_param_value(buf
, sizeof(buf
),
5149 boot_devices_bitmap
|= parse_bootdevices(buf
);
5150 standard_boot_devices
= qemu_strdup(boot_devices
);
5151 pstrcpy(boot_devices
, sizeof(boot_devices
), buf
);
5152 qemu_register_reset(restore_boot_devices
,
5153 standard_boot_devices
);
5155 if (get_param_value(buf
, sizeof(buf
),
5157 if (!strcmp(buf
, "on")) {
5159 } else if (!strcmp(buf
, "off")) {
5163 "qemu: invalid option value '%s'\n",
5171 case QEMU_OPTION_fda
:
5172 case QEMU_OPTION_fdb
:
5173 drive_add(optarg
, FD_ALIAS
, popt
->index
- QEMU_OPTION_fda
);
5176 case QEMU_OPTION_no_fd_bootchk
:
5180 case QEMU_OPTION_net
:
5181 if (nb_net_clients
>= MAX_NET_CLIENTS
) {
5182 fprintf(stderr
, "qemu: too many network clients\n");
5185 net_clients
[nb_net_clients
] = optarg
;
5189 case QEMU_OPTION_tftp
:
5190 legacy_tftp_prefix
= optarg
;
5192 case QEMU_OPTION_bootp
:
5193 legacy_bootp_filename
= optarg
;
5196 case QEMU_OPTION_smb
:
5197 net_slirp_smb(optarg
);
5200 case QEMU_OPTION_redir
:
5201 net_slirp_redir(optarg
);
5204 case QEMU_OPTION_bt
:
5205 add_device_config(DEV_BT
, optarg
);
5208 case QEMU_OPTION_audio_help
:
5212 case QEMU_OPTION_soundhw
:
5213 select_soundhw (optarg
);
5219 case QEMU_OPTION_version
:
5223 case QEMU_OPTION_m
: {
5227 value
= strtoul(optarg
, &ptr
, 10);
5229 case 0: case 'M': case 'm':
5236 fprintf(stderr
, "qemu: invalid ram size: %s\n", optarg
);
5240 /* On 32-bit hosts, QEMU is limited by virtual address space */
5241 if (value
> (2047 << 20)
5242 #ifndef CONFIG_KQEMU
5243 && HOST_LONG_BITS
== 32
5246 fprintf(stderr
, "qemu: at most 2047 MB RAM can be simulated\n");
5249 if (value
!= (uint64_t)(ram_addr_t
)value
) {
5250 fprintf(stderr
, "qemu: ram size too large\n");
5259 const CPULogItem
*item
;
5261 mask
= cpu_str_to_log_mask(optarg
);
5263 printf("Log items (comma separated):\n");
5264 for(item
= cpu_log_items
; item
->mask
!= 0; item
++) {
5265 printf("%-10s %s\n", item
->name
, item
->help
);
5273 gdbstub_dev
= "tcp::" DEFAULT_GDBSTUB_PORT
;
5275 case QEMU_OPTION_gdb
:
5276 gdbstub_dev
= optarg
;
5281 case QEMU_OPTION_bios
:
5284 case QEMU_OPTION_singlestep
:
5292 keyboard_layout
= optarg
;
5295 case QEMU_OPTION_localtime
:
5298 case QEMU_OPTION_vga
:
5299 select_vgahw (optarg
);
5301 #if defined(TARGET_PPC) || defined(TARGET_SPARC)
5307 w
= strtol(p
, (char **)&p
, 10);
5310 fprintf(stderr
, "qemu: invalid resolution or depth\n");
5316 h
= strtol(p
, (char **)&p
, 10);
5321 depth
= strtol(p
, (char **)&p
, 10);
5322 if (depth
!= 8 && depth
!= 15 && depth
!= 16 &&
5323 depth
!= 24 && depth
!= 32)
5325 } else if (*p
== '\0') {
5326 depth
= graphic_depth
;
5333 graphic_depth
= depth
;
5337 case QEMU_OPTION_echr
:
5340 term_escape_char
= strtol(optarg
, &r
, 0);
5342 printf("Bad argument to echr\n");
5345 case QEMU_OPTION_monitor
:
5346 monitor_device
= optarg
;
5348 case QEMU_OPTION_serial
:
5349 if (serial_device_index
>= MAX_SERIAL_PORTS
) {
5350 fprintf(stderr
, "qemu: too many serial ports\n");
5353 serial_devices
[serial_device_index
] = optarg
;
5354 serial_device_index
++;
5356 case QEMU_OPTION_watchdog
:
5357 i
= select_watchdog(optarg
);
5359 exit (i
== 1 ? 1 : 0);
5361 case QEMU_OPTION_watchdog_action
:
5362 if (select_watchdog_action(optarg
) == -1) {
5363 fprintf(stderr
, "Unknown -watchdog-action parameter\n");
5367 case QEMU_OPTION_virtiocon
:
5368 if (virtio_console_index
>= MAX_VIRTIO_CONSOLES
) {
5369 fprintf(stderr
, "qemu: too many virtio consoles\n");
5372 virtio_consoles
[virtio_console_index
] = optarg
;
5373 virtio_console_index
++;
5375 case QEMU_OPTION_parallel
:
5376 if (parallel_device_index
>= MAX_PARALLEL_PORTS
) {
5377 fprintf(stderr
, "qemu: too many parallel ports\n");
5380 parallel_devices
[parallel_device_index
] = optarg
;
5381 parallel_device_index
++;
5383 case QEMU_OPTION_loadvm
:
5386 case QEMU_OPTION_full_screen
:
5390 case QEMU_OPTION_no_frame
:
5393 case QEMU_OPTION_alt_grab
:
5396 case QEMU_OPTION_no_quit
:
5399 case QEMU_OPTION_sdl
:
5400 display_type
= DT_SDL
;
5403 case QEMU_OPTION_pidfile
:
5407 case QEMU_OPTION_win2k_hack
:
5408 win2k_install_hack
= 1;
5410 case QEMU_OPTION_rtc_td_hack
:
5413 case QEMU_OPTION_acpitable
:
5414 if(acpi_table_add(optarg
) < 0) {
5415 fprintf(stderr
, "Wrong acpi table provided\n");
5419 case QEMU_OPTION_smbios
:
5420 if(smbios_entry_add(optarg
) < 0) {
5421 fprintf(stderr
, "Wrong smbios provided\n");
5427 case QEMU_OPTION_enable_kqemu
:
5430 case QEMU_OPTION_kernel_kqemu
:
5435 case QEMU_OPTION_enable_kvm
:
5442 case QEMU_OPTION_usb
:
5445 case QEMU_OPTION_usbdevice
:
5447 add_device_config(DEV_USB
, optarg
);
5449 case QEMU_OPTION_device
:
5450 add_device_config(DEV_GENERIC
, optarg
);
5452 case QEMU_OPTION_smp
:
5456 smp_cpus
= strtol(optarg
, &p
, 10);
5458 fprintf(stderr
, "Invalid number of CPUs\n");
5463 if (get_param_value(option
, 128, "maxcpus", p
))
5464 max_cpus
= strtol(option
, NULL
, 0);
5465 if (max_cpus
< smp_cpus
) {
5466 fprintf(stderr
, "maxcpus must be equal to or greater than "
5470 if (max_cpus
> 255) {
5471 fprintf(stderr
, "Unsupported number of maxcpus\n");
5476 case QEMU_OPTION_vnc
:
5477 display_type
= DT_VNC
;
5478 vnc_display
= optarg
;
5481 case QEMU_OPTION_no_acpi
:
5484 case QEMU_OPTION_no_hpet
:
5487 case QEMU_OPTION_balloon
:
5488 if (balloon_parse(optarg
) < 0) {
5489 fprintf(stderr
, "Unknown -balloon argument %s\n", optarg
);
5494 case QEMU_OPTION_no_reboot
:
5497 case QEMU_OPTION_no_shutdown
:
5500 case QEMU_OPTION_show_cursor
:
5503 case QEMU_OPTION_uuid
:
5504 if(qemu_uuid_parse(optarg
, qemu_uuid
) < 0) {
5505 fprintf(stderr
, "Fail to parse UUID string."
5506 " Wrong format.\n");
5511 case QEMU_OPTION_daemonize
:
5515 case QEMU_OPTION_option_rom
:
5516 if (nb_option_roms
>= MAX_OPTION_ROMS
) {
5517 fprintf(stderr
, "Too many option ROMs\n");
5520 option_rom
[nb_option_roms
] = optarg
;
5523 #if defined(TARGET_ARM) || defined(TARGET_M68K)
5524 case QEMU_OPTION_semihosting
:
5525 semihosting_enabled
= 1;
5528 case QEMU_OPTION_name
:
5529 qemu_name
= qemu_strdup(optarg
);
5531 char *p
= strchr(qemu_name
, ',');
5534 if (strncmp(p
, "process=", 8)) {
5535 fprintf(stderr
, "Unknown subargument %s to -name", p
);
5543 #if defined(TARGET_SPARC) || defined(TARGET_PPC)
5544 case QEMU_OPTION_prom_env
:
5545 if (nb_prom_envs
>= MAX_PROM_ENVS
) {
5546 fprintf(stderr
, "Too many prom variables\n");
5549 prom_envs
[nb_prom_envs
] = optarg
;
5554 case QEMU_OPTION_old_param
:
5558 case QEMU_OPTION_clock
:
5559 configure_alarms(optarg
);
5561 case QEMU_OPTION_startdate
:
5564 time_t rtc_start_date
;
5565 if (!strcmp(optarg
, "now")) {
5566 rtc_date_offset
= -1;
5568 if (sscanf(optarg
, "%d-%d-%dT%d:%d:%d",
5576 } else if (sscanf(optarg
, "%d-%d-%d",
5579 &tm
.tm_mday
) == 3) {
5588 rtc_start_date
= mktimegm(&tm
);
5589 if (rtc_start_date
== -1) {
5591 fprintf(stderr
, "Invalid date format. Valid format are:\n"
5592 "'now' or '2006-06-17T16:01:21' or '2006-06-17'\n");
5595 rtc_date_offset
= time(NULL
) - rtc_start_date
;
5599 case QEMU_OPTION_tb_size
:
5600 tb_size
= strtol(optarg
, NULL
, 0);
5604 case QEMU_OPTION_icount
:
5606 if (strcmp(optarg
, "auto") == 0) {
5607 icount_time_shift
= -1;
5609 icount_time_shift
= strtol(optarg
, NULL
, 0);
5612 case QEMU_OPTION_incoming
:
5616 case QEMU_OPTION_chroot
:
5617 chroot_dir
= optarg
;
5619 case QEMU_OPTION_runas
:
5624 case QEMU_OPTION_xen_domid
:
5625 xen_domid
= atoi(optarg
);
5627 case QEMU_OPTION_xen_create
:
5628 xen_mode
= XEN_CREATE
;
5630 case QEMU_OPTION_xen_attach
:
5631 xen_mode
= XEN_ATTACH
;
5638 /* If no data_dir is specified then try to find it relative to the
5641 data_dir
= find_datadir(argv
[0]);
5643 /* If all else fails use the install patch specified when building. */
5645 data_dir
= CONFIG_QEMU_SHAREDIR
;
5648 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5649 if (kvm_allowed
&& kqemu_allowed
) {
5651 "You can not enable both KVM and kqemu at the same time\n");
5657 * Default to max_cpus = smp_cpus, in case the user doesn't
5658 * specify a max_cpus value.
5661 max_cpus
= smp_cpus
;
5663 machine
->max_cpus
= machine
->max_cpus
?: 1; /* Default to UP */
5664 if (smp_cpus
> machine
->max_cpus
) {
5665 fprintf(stderr
, "Number of SMP cpus requested (%d), exceeds max cpus "
5666 "supported by machine `%s' (%d)\n", smp_cpus
, machine
->name
,
5671 if (display_type
== DT_NOGRAPHIC
) {
5672 if (serial_device_index
== 0)
5673 serial_devices
[0] = "stdio";
5674 if (parallel_device_index
== 0)
5675 parallel_devices
[0] = "null";
5676 if (strncmp(monitor_device
, "vc", 2) == 0)
5677 monitor_device
= "stdio";
5684 if (pipe(fds
) == -1)
5695 len
= read(fds
[0], &status
, 1);
5696 if (len
== -1 && (errno
== EINTR
))
5701 else if (status
== 1) {
5702 fprintf(stderr
, "Could not acquire pidfile\n");
5719 signal(SIGTSTP
, SIG_IGN
);
5720 signal(SIGTTOU
, SIG_IGN
);
5721 signal(SIGTTIN
, SIG_IGN
);
5724 if (pid_file
&& qemu_create_pidfile(pid_file
) != 0) {
5727 write(fds
[1], &status
, 1);
5729 fprintf(stderr
, "Could not acquire pid file\n");
5738 if (qemu_init_main_loop()) {
5739 fprintf(stderr
, "qemu_init_main_loop failed\n");
5742 linux_boot
= (kernel_filename
!= NULL
);
5744 if (!linux_boot
&& *kernel_cmdline
!= '\0') {
5745 fprintf(stderr
, "-append only allowed with -kernel option\n");
5749 if (!linux_boot
&& initrd_filename
!= NULL
) {
5750 fprintf(stderr
, "-initrd only allowed with -kernel option\n");
5755 /* Win32 doesn't support line-buffering and requires size >= 2 */
5756 setvbuf(stdout
, NULL
, _IOLBF
, 0);
5760 if (init_timer_alarm() < 0) {
5761 fprintf(stderr
, "could not initialize alarm timer\n");
5764 if (use_icount
&& icount_time_shift
< 0) {
5766 /* 125MIPS seems a reasonable initial guess at the guest speed.
5767 It will be corrected fairly quickly anyway. */
5768 icount_time_shift
= 3;
5769 init_icount_adjust();
5776 /* init network clients */
5777 if (nb_net_clients
== 0) {
5778 /* if no clients, we use a default config */
5779 net_clients
[nb_net_clients
++] = "nic";
5781 net_clients
[nb_net_clients
++] = "user";
5785 for(i
= 0;i
< nb_net_clients
; i
++) {
5786 if (net_client_parse(net_clients
[i
]) < 0)
5790 net_boot
= (boot_devices_bitmap
>> ('n' - 'a')) & 0xF;
5791 net_set_boot_mask(net_boot
);
5795 /* init the bluetooth world */
5796 if (foreach_device_config(DEV_BT
, bt_parse
))
5799 /* init the memory */
5801 ram_size
= DEFAULT_RAM_SIZE
* 1024 * 1024;
5804 /* FIXME: This is a nasty hack because kqemu can't cope with dynamic
5805 guest ram allocation. It needs to go away. */
5806 if (kqemu_allowed
) {
5807 kqemu_phys_ram_size
= ram_size
+ 8 * 1024 * 1024 + 4 * 1024 * 1024;
5808 kqemu_phys_ram_base
= qemu_vmalloc(kqemu_phys_ram_size
);
5809 if (!kqemu_phys_ram_base
) {
5810 fprintf(stderr
, "Could not allocate physical memory\n");
5816 /* init the dynamic translator */
5817 cpu_exec_init_all(tb_size
* 1024 * 1024);
5821 /* we always create the cdrom drive, even if no disk is there */
5822 drive_add(NULL
, CDROM_ALIAS
);
5824 /* we always create at least one floppy */
5825 drive_add(NULL
, FD_ALIAS
, 0);
5827 /* we always create one sd slot, even if no card is in it */
5828 drive_add(NULL
, SD_ALIAS
);
5830 /* open the virtual block devices */
5832 qemu_opts_foreach(&drive_opt_list
, drive_enable_snapshot
, NULL
, 0);
5833 if (qemu_opts_foreach(&drive_opt_list
, drive_init_func
, machine
, 1) != 0)
5836 register_savevm("timer", 0, 2, timer_save
, timer_load
, NULL
);
5837 register_savevm_live("ram", 0, 3, ram_save_live
, NULL
, ram_load
, NULL
);
5840 /* must be after terminal init, SDL library changes signal handlers */
5844 /* Maintain compatibility with multiple stdio monitors */
5845 if (!strcmp(monitor_device
,"stdio")) {
5846 for (i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5847 const char *devname
= serial_devices
[i
];
5848 if (devname
&& !strcmp(devname
,"mon:stdio")) {
5849 monitor_device
= NULL
;
5851 } else if (devname
&& !strcmp(devname
,"stdio")) {
5852 monitor_device
= NULL
;
5853 serial_devices
[i
] = "mon:stdio";
5859 if (nb_numa_nodes
> 0) {
5862 if (nb_numa_nodes
> smp_cpus
) {
5863 nb_numa_nodes
= smp_cpus
;
5866 /* If no memory size if given for any node, assume the default case
5867 * and distribute the available memory equally across all nodes
5869 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5870 if (node_mem
[i
] != 0)
5873 if (i
== nb_numa_nodes
) {
5874 uint64_t usedmem
= 0;
5876 /* On Linux, the each node's border has to be 8MB aligned,
5877 * the final node gets the rest.
5879 for (i
= 0; i
< nb_numa_nodes
- 1; i
++) {
5880 node_mem
[i
] = (ram_size
/ nb_numa_nodes
) & ~((1 << 23UL) - 1);
5881 usedmem
+= node_mem
[i
];
5883 node_mem
[i
] = ram_size
- usedmem
;
5886 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5887 if (node_cpumask
[i
] != 0)
5890 /* assigning the VCPUs round-robin is easier to implement, guest OSes
5891 * must cope with this anyway, because there are BIOSes out there in
5892 * real machines which also use this scheme.
5894 if (i
== nb_numa_nodes
) {
5895 for (i
= 0; i
< smp_cpus
; i
++) {
5896 node_cpumask
[i
% nb_numa_nodes
] |= 1 << i
;
5901 if (kvm_enabled()) {
5904 ret
= kvm_init(smp_cpus
);
5906 fprintf(stderr
, "failed to initialize KVM\n");
5911 if (monitor_device
) {
5912 monitor_hd
= qemu_chr_open("monitor", monitor_device
, NULL
);
5914 fprintf(stderr
, "qemu: could not open monitor device '%s'\n", monitor_device
);
5919 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
5920 const char *devname
= serial_devices
[i
];
5921 if (devname
&& strcmp(devname
, "none")) {
5923 snprintf(label
, sizeof(label
), "serial%d", i
);
5924 serial_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5925 if (!serial_hds
[i
]) {
5926 fprintf(stderr
, "qemu: could not open serial device '%s'\n",
5933 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
5934 const char *devname
= parallel_devices
[i
];
5935 if (devname
&& strcmp(devname
, "none")) {
5937 snprintf(label
, sizeof(label
), "parallel%d", i
);
5938 parallel_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5939 if (!parallel_hds
[i
]) {
5940 fprintf(stderr
, "qemu: could not open parallel device '%s'\n",
5947 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
5948 const char *devname
= virtio_consoles
[i
];
5949 if (devname
&& strcmp(devname
, "none")) {
5951 snprintf(label
, sizeof(label
), "virtcon%d", i
);
5952 virtcon_hds
[i
] = qemu_chr_open(label
, devname
, NULL
);
5953 if (!virtcon_hds
[i
]) {
5954 fprintf(stderr
, "qemu: could not open virtio console '%s'\n",
5961 module_call_init(MODULE_INIT_DEVICE
);
5963 if (machine
->compat_props
) {
5964 qdev_prop_register_compat(machine
->compat_props
);
5966 machine
->init(ram_size
, boot_devices
,
5967 kernel_filename
, kernel_cmdline
, initrd_filename
, cpu_model
);
5970 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
5971 for (i
= 0; i
< nb_numa_nodes
; i
++) {
5972 if (node_cpumask
[i
] & (1 << env
->cpu_index
)) {
5978 current_machine
= machine
;
5980 /* init USB devices */
5982 foreach_device_config(DEV_USB
, usb_parse
);
5985 /* init generic devices */
5986 if (foreach_device_config(DEV_GENERIC
, generic_parse
))
5990 dumb_display_init();
5991 /* just use the first displaystate for the moment */
5994 if (display_type
== DT_DEFAULT
) {
5995 #if defined(CONFIG_SDL) || defined(CONFIG_COCOA)
5996 display_type
= DT_SDL
;
5998 display_type
= DT_VNC
;
5999 vnc_display
= "localhost:0,to=99";
6005 switch (display_type
) {
6008 #if defined(CONFIG_CURSES)
6010 curses_display_init(ds
, full_screen
);
6013 #if defined(CONFIG_SDL)
6015 sdl_display_init(ds
, full_screen
, no_frame
);
6017 #elif defined(CONFIG_COCOA)
6019 cocoa_display_init(ds
, full_screen
);
6023 vnc_display_init(ds
);
6024 if (vnc_display_open(ds
, vnc_display
) < 0)
6027 if (show_vnc_port
) {
6028 printf("VNC server running on `%s'\n", vnc_display_local_addr(ds
));
6036 dcl
= ds
->listeners
;
6037 while (dcl
!= NULL
) {
6038 if (dcl
->dpy_refresh
!= NULL
) {
6039 ds
->gui_timer
= qemu_new_timer(rt_clock
, gui_update
, ds
);
6040 qemu_mod_timer(ds
->gui_timer
, qemu_get_clock(rt_clock
));
6045 if (display_type
== DT_NOGRAPHIC
|| display_type
== DT_VNC
) {
6046 nographic_timer
= qemu_new_timer(rt_clock
, nographic_update
, NULL
);
6047 qemu_mod_timer(nographic_timer
, qemu_get_clock(rt_clock
));
6050 text_consoles_set_display(display_state
);
6051 qemu_chr_initial_reset();
6053 if (monitor_device
&& monitor_hd
)
6054 monitor_init(monitor_hd
, MONITOR_USE_READLINE
| MONITOR_IS_DEFAULT
);
6056 for(i
= 0; i
< MAX_SERIAL_PORTS
; i
++) {
6057 const char *devname
= serial_devices
[i
];
6058 if (devname
&& strcmp(devname
, "none")) {
6059 if (strstart(devname
, "vc", 0))
6060 qemu_chr_printf(serial_hds
[i
], "serial%d console\r\n", i
);
6064 for(i
= 0; i
< MAX_PARALLEL_PORTS
; i
++) {
6065 const char *devname
= parallel_devices
[i
];
6066 if (devname
&& strcmp(devname
, "none")) {
6067 if (strstart(devname
, "vc", 0))
6068 qemu_chr_printf(parallel_hds
[i
], "parallel%d console\r\n", i
);
6072 for(i
= 0; i
< MAX_VIRTIO_CONSOLES
; i
++) {
6073 const char *devname
= virtio_consoles
[i
];
6074 if (virtcon_hds
[i
] && devname
) {
6075 if (strstart(devname
, "vc", 0))
6076 qemu_chr_printf(virtcon_hds
[i
], "virtio console%d\r\n", i
);
6080 if (gdbstub_dev
&& gdbserver_start(gdbstub_dev
) < 0) {
6081 fprintf(stderr
, "qemu: could not open gdbserver on device '%s'\n",
6087 do_loadvm(cur_mon
, loadvm
);
6091 qemu_start_incoming_migration(incoming
);
6103 len
= write(fds
[1], &status
, 1);
6104 if (len
== -1 && (errno
== EINTR
))
6111 TFR(fd
= open("/dev/null", O_RDWR
));
6117 pwd
= getpwnam(run_as
);
6119 fprintf(stderr
, "User \"%s\" doesn't exist\n", run_as
);
6125 if (chroot(chroot_dir
) < 0) {
6126 fprintf(stderr
, "chroot failed\n");
6133 if (setgid(pwd
->pw_gid
) < 0) {
6134 fprintf(stderr
, "Failed to setgid(%d)\n", pwd
->pw_gid
);
6137 if (setuid(pwd
->pw_uid
) < 0) {
6138 fprintf(stderr
, "Failed to setuid(%d)\n", pwd
->pw_uid
);
6141 if (setuid(0) != -1) {
6142 fprintf(stderr
, "Dropping privileges failed\n");